Whamcloud - gitweb
LU-930 doc: add man pages to make file
[fs/lustre-release.git] / lustre / utils / gss / gssd.c
1 /*
2   gssd.c
3
4   Copyright (c) 2000 The Regents of the University of Michigan.
5   All rights reserved.
6
7   Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
8   Copyright (c) 2002 Andy Adamson <andros@UMICH.EDU>.
9   Copyright (c) 2002 Marius Aamodt Eriksen <marius@UMICH.EDU>.
10   All rights reserved, all wrongs reversed.
11
12   Copyright (c) 2014, Intel Corporation.
13
14   Redistribution and use in source and binary forms, with or without
15   modification, are permitted provided that the following conditions
16   are met:
17
18   1. Redistributions of source code must retain the above copyright
19      notice, this list of conditions and the following disclaimer.
20   2. Redistributions in binary form must reproduce the above copyright
21      notice, this list of conditions and the following disclaimer in the
22      documentation and/or other materials provided with the distribution.
23   3. Neither the name of the University nor the names of its
24      contributors may be used to endorse or promote products derived
25      from this software without specific prior written permission.
26
27   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
28   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 */
40
41 #include "config.h"
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/wait.h>
47 #include <sys/ipc.h>
48 #include <sys/sem.h>
49
50 #include <unistd.h>
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <signal.h>
56 #include <errno.h>
57 #include <libcfs/util/string.h>
58
59 #include "gssd.h"
60 #include "err_util.h"
61 #include "gss_util.h"
62 #include "krb5_util.h"
63 #include "lsupport.h"
64
65 char *pipefs_dir = GSSD_PIPEFS_DIR;
66 char pipefs_nfsdir[PATH_MAX] = GSSD_PIPEFS_DIR;
67 char *keytabfile = GSSD_DEFAULT_KEYTAB_FILE;
68 char *ccachedir = GSSD_DEFAULT_CRED_DIR;
69 int  use_memcache = 0;
70 int  lgssd_mutex_downcall = -1;
71
72 static int lgssd_create_mutex(int *semid)
73 {
74         int             id;
75         int             arg;
76
77         id = semget(IPC_PRIVATE, 1, IPC_CREAT);
78         if (id == -1) {
79                 printerr(0, "semget: %s\n", strerror(errno));
80                 return -1;
81         }
82
83         arg = 1;
84         if (semctl(id, 0, SETVAL, arg) != 0) {
85                 printerr(0, "semctl: %s\n", strerror(errno));
86                 semctl(id, 1, IPC_RMID, arg);
87                 return -1;
88         }
89
90         *semid = id;
91         return 0;
92 }
93
94 void lgssd_init_mutexs(void)
95 {
96         if (lgssd_create_mutex(&lgssd_mutex_downcall)) {
97                 printerr(0, "can't create downcall mutex\n");
98                 exit(1);
99         }
100 }
101
102 void lgssd_fini_mutexs(void)
103 {
104         int     arg = 0;
105
106         if (lgssd_mutex_downcall != -1)
107                 semctl(lgssd_mutex_downcall, 1, IPC_RMID, arg);
108 }
109
110 void lgssd_mutex_get(int semid)
111 {
112         struct sembuf op = {
113                 .sem_op = -1,
114                 .sem_flag = SEM_UNDO
115         };
116         int rc;
117
118         rc = semop(semid, &op, 1);
119         if (rc != 0) {
120                 printerr(0, "exit on mutex_get err %d: %s\n",
121                          rc, strerror(errno));
122                 exit(1);
123         }
124 }
125
126 void lgssd_mutex_put(int semid)
127 {
128         struct sembuf op = {
129                 .sem_op = 1
130         };
131         int rc;
132
133         rc = semop(semid, &op, 1);
134         if (rc != 0) {
135                 printerr(0, "ignore mutex_put err %d: %s\n",
136                          rc, strerror(errno));
137         }
138 }
139
140 static void lgssd_cleanup(void)
141 {
142         pid_t   child_pid;
143
144         /* make sure all children finished */
145         while (1) {
146                 child_pid = waitpid(-1, NULL, 0);
147                 if (child_pid < 0)
148                         break;
149
150                 printerr(3, "cleanup: child %d terminated\n", child_pid);
151         }
152
153         lgssd_fini_mutexs();
154
155         /* destroy krb5 machine creds */
156         gssd_destroy_krb5_machine_creds();
157 }
158
159 void
160 sig_die(int signal)
161 {
162         printerr(1, "exiting on signal %d\n", signal);
163         lgssd_cleanup();
164         exit(1);
165 }
166
167 void
168 sig_hup(int signal)
169 {
170         /* don't exit on SIGHUP */
171         printerr(1, "Received SIGHUP... Ignoring.\n");
172 }
173
174 static void
175 usage(char *progname)
176 {
177         fprintf(stderr, "usage: %s [-f] [-v] [-p pipefsdir] [-k keytab] [-d ccachedir]\n",
178                 progname);
179         exit(1);
180 }
181
182 int
183 main(int argc, char *argv[])
184 {
185         int fg = 0;
186         int verbosity = 0;
187         int opt;
188         extern char *optarg;
189         char *progname;
190
191         while ((opt = getopt(argc, argv, "fvrmMp:k:d:")) != -1) {
192                 switch (opt) {
193                         case 'f':
194                                 fg = 1;
195                                 break;
196                         case 'M':
197                                 use_memcache = 1;
198                                 break;
199                         case 'v':
200                                 verbosity++;
201                                 break;
202                         case 'p':
203                                 pipefs_dir = strdup(optarg);
204                                 if (!pipe_dir)
205                                         errx(1, "pipefs path name not aquired");
206                                 break;
207                         case 'k':
208                                 keytabfile = strdup(optarg);
209                                 if (!keytab_file)
210                                         errx(1, "keytab path name not aquired");
211                                 break;
212                         case 'd':
213                                 ccachedir = strdup(optarg);
214                                 if (!ccachedir)
215                                         errx(1, "ccachedir path name not aquired");
216                                 break;
217                         default:
218                                 usage(argv[0]);
219                                 break;
220                 }
221         }
222
223         if ((progname = strrchr(argv[0], '/')))
224                 progname++;
225         else
226                 progname = argv[0];
227
228         initerr(progname, verbosity, fg);
229
230         if (gssd_check_mechs() != 0)
231                 errx(1, "Problem with gssapi library");
232
233         if (gssd_get_local_realm())
234                 errx(1, "get local realm");
235
236         if (!fg && daemon(0, 0) < 0)
237                 errx(1, "fork");
238
239         /* This should be checked _after_ daemon(), because we need to own
240          * the undo-able semaphore by this process
241          */
242         gssd_init_unique(GSSD_CLI);
243
244         /* Process keytab file and get machine credentials. This will modify
245          * disk status so do it after we are sure we are the only instance
246          */
247         if (gssd_refresh_krb5_machine_creds())
248                 return -1;
249
250         signal(SIGINT, sig_die);
251         signal(SIGTERM, sig_die);
252         signal(SIGHUP, sig_hup);
253
254         lgssd_init_mutexs();
255
256         printerr(0, "lgssd initialized and ready to serve\n");
257         lgssd_run();
258
259         lgssd_cleanup();
260         printerr(0, "lgssd exiting\n");
261         return 0;
262 }