Whamcloud - gitweb
LU-6068 misc: update Intel copyright messages 2014
[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 "gssd.h"
58 #include "err_util.h"
59 #include "gss_util.h"
60 #include "krb5_util.h"
61 #include "lsupport.h"
62
63 char pipefs_dir[PATH_MAX] = GSSD_PIPEFS_DIR;
64 char pipefs_nfsdir[PATH_MAX] = GSSD_PIPEFS_DIR;
65 char keytabfile[PATH_MAX] = GSSD_DEFAULT_KEYTAB_FILE;
66 char ccachedir[PATH_MAX] = GSSD_DEFAULT_CRED_DIR;
67 int  use_memcache = 0;
68 int  lgssd_mutex_downcall = -1;
69
70 static int lgssd_create_mutex(int *semid)
71 {
72         int             id;
73         int             arg;
74
75         id = semget(IPC_PRIVATE, 1, IPC_CREAT);
76         if (id == -1) {
77                 printerr(0, "semget: %s\n", strerror(errno));
78                 return -1;
79         }
80
81         arg = 1;
82         if (semctl(id, 0, SETVAL, arg) != 0) {
83                 printerr(0, "semctl: %s\n", strerror(errno));
84                 semctl(id, 1, IPC_RMID, arg);
85                 return -1;
86         }
87
88         *semid = id;
89         return 0;
90 }
91
92 void lgssd_init_mutexs(void)
93 {
94         if (lgssd_create_mutex(&lgssd_mutex_downcall)) {
95                 printerr(0, "can't create downcall mutex\n");
96                 exit(1);
97         }
98 }
99
100 void lgssd_fini_mutexs(void)
101 {
102         int     arg = 0;
103
104         if (lgssd_mutex_downcall != -1)
105                 semctl(lgssd_mutex_downcall, 1, IPC_RMID, arg);
106 }
107
108 void lgssd_mutex_get(int semid)
109 {
110         struct sembuf   op[1] = { {0, -1, SEM_UNDO} };
111         int             rc;
112
113         rc = semop(semid, op, 1);
114         if (rc != 0) {
115                 printerr(0, "exit on mutex_get err %d: %s\n",
116                          rc, strerror(errno));
117                 exit(1);
118         }
119 }
120
121 void lgssd_mutex_put(int semid)
122 {
123         struct sembuf   op[1] = { {0, 1, 0} };
124         int             rc;
125
126         rc = semop(semid, op, 1);
127         if (rc != 0) {
128                 printerr(0, "ignore mutex_put err %d: %s\n",
129                          rc, strerror(errno));
130         }
131 }
132
133 static void lgssd_cleanup(void)
134 {
135         pid_t   child_pid;
136
137         /* make sure all children finished */
138         while (1) {
139                 child_pid = waitpid(-1, NULL, 0);
140                 if (child_pid < 0)
141                         break;
142
143                 printerr(3, "cleanup: child %d terminated\n", child_pid);
144         }
145
146         lgssd_fini_mutexs();
147
148         /* destroy krb5 machine creds */
149         gssd_destroy_krb5_machine_creds();
150 }
151
152 void
153 sig_die(int signal)
154 {
155         printerr(1, "exiting on signal %d\n", signal);
156         lgssd_cleanup();
157         exit(1);
158 }
159
160 void
161 sig_hup(int signal)
162 {
163         /* don't exit on SIGHUP */
164         printerr(1, "Received SIGHUP... Ignoring.\n");
165         return;
166 }
167
168 static void
169 usage(char *progname)
170 {
171         fprintf(stderr, "usage: %s [-f] [-v] [-p pipefsdir] [-k keytab] [-d ccachedir]\n",
172                 progname);
173         exit(1);
174 }
175
176 int
177 main(int argc, char *argv[])
178 {
179         int fg = 0;
180         int verbosity = 0;
181         int opt;
182         extern char *optarg;
183         char *progname;
184
185         while ((opt = getopt(argc, argv, "fvrmMp:k:d:")) != -1) {
186                 switch (opt) {
187                         case 'f':
188                                 fg = 1;
189                                 break;
190                         case 'M':
191                                 use_memcache = 1;
192                                 break;
193                         case 'v':
194                                 verbosity++;
195                                 break;
196                         case 'p':
197                                 strlcpy(pipefs_dir, optarg, sizeof(pipefs_dir));
198                                 if (pipefs_dir[sizeof(pipefs_dir)-1] != '\0')
199                                         errx(1, "pipefs path name too long");
200                                 break;
201                         case 'k':
202                                 strlcpy(keytabfile, optarg, sizeof(keytabfile));
203                                 if (keytabfile[sizeof(keytabfile)-1] != '\0')
204                                         errx(1, "keytab path name too long");
205                                 break;
206                         case 'd':
207                                 strlcpy(ccachedir, optarg, sizeof(ccachedir));
208                                 if (ccachedir[sizeof(ccachedir)-1] != '\0')
209                                         errx(1, "ccachedir path name too long");
210                                 break;
211                         default:
212                                 usage(argv[0]);
213                                 break;
214                 }
215         }
216
217         if ((progname = strrchr(argv[0], '/')))
218                 progname++;
219         else
220                 progname = argv[0];
221
222         initerr(progname, verbosity, fg);
223
224         if (gssd_check_mechs() != 0)
225                 errx(1, "Problem with gssapi library");
226
227         if (gssd_get_local_realm())
228                 errx(1, "get local realm");
229
230         if (!fg && daemon(0, 0) < 0)
231                 errx(1, "fork");
232
233         /* This should be checked _after_ daemon(), because we need to own
234          * the undo-able semaphore by this process
235          */
236         gssd_init_unique(GSSD_CLI);
237
238         /* Process keytab file and get machine credentials. This will modify
239          * disk status so do it after we are sure we are the only instance
240          */
241         if (gssd_refresh_krb5_machine_creds())
242                 return -1;
243
244         signal(SIGINT, sig_die);
245         signal(SIGTERM, sig_die);
246         signal(SIGHUP, sig_hup);
247
248 #if 0
249         /* Determine Kerberos information from the kernel */
250         gssd_obtain_kernel_krb5_info();
251 #endif
252
253         lgssd_init_mutexs();
254
255         printerr(0, "lgssd initialized and ready to serve\n");
256         lgssd_run();
257
258         lgssd_cleanup();
259         printerr(0, "lgssd exiting\n");
260         return 0;
261 }