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