Whamcloud - gitweb
LU-6179 llite: Implement ladvise lockahead
[fs/lustre-release.git] / lustre / utils / gss / svcgssd.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  * Copyright (c) 2002 J. Bruce Fields <bfields@UMICH.EDU>.
11  *
12  * All rights reserved, all wrongs reversed.
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 #include "config.h"
41
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/socket.h>
46 #include <fcntl.h>
47 #include <errno.h>
48
49 #include <unistd.h>
50 #include <err.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <signal.h>
55 #include <dirent.h>
56 #include "svcgssd.h"
57 #include "gss_util.h"
58 #include "err_util.h"
59 #include "lsupport.h"
60 #include <linux/lustre/lustre_ver.h>
61
62 int null_enabled;
63 int krb_enabled;
64 int sk_enabled;
65
66 void
67 closeall(int min)
68 {
69         DIR *dir = opendir("/proc/self/fd");
70         if (dir != NULL) {
71                 int dfd = dirfd(dir);
72                 struct dirent *d;
73
74                 while ((d = readdir(dir)) != NULL) {
75                         char *endp;
76                         long n = strtol(d->d_name, &endp, 10);
77                         if (*endp != '\0' && n >= min && n != dfd)
78                                 (void) close(n);
79                 }
80                 closedir(dir);
81         } else {
82                 int fd = sysconf(_SC_OPEN_MAX);
83                 while (--fd >= min)
84                         (void) close(fd);
85         }
86 }
87
88 /*
89  * mydaemon creates a pipe between the partent and child
90  * process. The parent process will wait until the
91  * child dies or writes a '1' on the pipe signaling
92  * that it started successfully.
93  */
94 int pipefds[2] = { -1, -1};
95
96 static void
97 mydaemon(int nochdir, int noclose)
98 {
99         int pid, status, tempfd;
100
101         if (pipe(pipefds) < 0) {
102                 printerr(1, "mydaemon: pipe() failed: errno %d (%s)\n",
103                         errno, strerror(errno));
104                 exit(1);
105         }
106         if ((pid = fork ()) < 0) {
107                 printerr(1, "mydaemon: fork() failed: errno %d (%s)\n",
108                         errno, strerror(errno));
109                 exit(1);
110         }
111
112         if (pid != 0) {
113                 /*
114                  * Parent. Wait for status from child.
115                  */
116                 close(pipefds[1]);
117                 if (read(pipefds[0], &status, 1) != 1)
118                         exit(1);
119                 exit (0);
120         }
121         /* Child.       */
122         close(pipefds[0]);
123         setsid ();
124         if (nochdir == 0) {
125                 if (chdir ("/") == -1) {
126                         printerr(1, "mydaemon: chdir() failed: errno %d (%s)\n",
127                                 errno, strerror(errno));
128                         exit(1);
129                 }
130         }
131
132         while (pipefds[1] <= 2) {
133                 pipefds[1] = dup(pipefds[1]);
134                 if (pipefds[1] < 0) {
135                         printerr(1, "mydaemon: dup() failed: errno %d (%s)\n",
136                                 errno, strerror(errno));
137                         exit(1);
138                 }
139         }
140
141         if (noclose == 0) {
142                 tempfd = open("/dev/null", O_RDWR);
143                 dup2(tempfd, 0);
144                 dup2(tempfd, 1);
145                 dup2(tempfd, 2);
146                 closeall(3);
147         }
148
149         return;
150 }
151
152 static void
153 release_parent()
154 {
155         int status;
156         ssize_t sret __attribute__ ((unused));
157
158         if (pipefds[1] > 0) {
159                 sret = write(pipefds[1], &status, 1);
160                 close(pipefds[1]);
161                 pipefds[1] = -1;
162         }
163 }
164
165 void
166 sig_die(int signal)
167 {
168         /* destroy krb5 machine creds */
169         cleanup_mapping();
170         printerr(1, "exiting on signal %d\n", signal);
171         exit(1);
172 }
173
174 void
175 sig_hup(int signal)
176 {
177         /* don't exit on SIGHUP */
178         printerr(1, "Received SIGHUP... Ignoring.\n");
179         return;
180 }
181
182 static void
183 usage(FILE *fp, char *progname)
184 {
185         fprintf(fp, "usage: %s [ -fnvmogk ]\n",
186                 progname);
187         fprintf(stderr, "-f      - Run in foreground\n");
188         fprintf(stderr, "-n      - Don't establish kerberos credentials\n");
189         fprintf(stderr, "-v      - Verbosity\n");
190         fprintf(stderr, "-m      - Service MDS\n");
191         fprintf(stderr, "-o      - Service OSS\n");
192         fprintf(stderr, "-g      - Service MGS\n");
193         fprintf(stderr, "-k      - Enable kerberos support\n");
194 #ifdef HAVE_OPENSSL_SSK
195         fprintf(stderr, "-s      - Enable shared secret key support\n");
196 #endif
197         fprintf(stderr, "-z      - Enable gssnull support\n");
198
199         exit(fp == stderr);
200 }
201
202 int
203 main(int argc, char *argv[])
204 {
205         int get_creds = 1;
206         int fg = 0;
207         int verbosity = 0;
208         int opt;
209         int must_srv_mds = 0, must_srv_oss = 0, must_srv_mgs = 0;
210         char *progname;
211
212         while ((opt = getopt(argc, argv, "fnvmogksz")) != -1) {
213                 switch (opt) {
214                 case 'f':
215                         fg = 1;
216                         break;
217                 case 'n':
218                         get_creds = 0;
219                         break;
220                 case 'v':
221                         verbosity++;
222                         break;
223                 case 'm':
224                         get_creds = 1;
225                         must_srv_mds = 1;
226                         break;
227                 case 'o':
228                         get_creds = 1;
229                         must_srv_oss = 1;
230                         break;
231                 case 'g':
232                         get_creds = 1;
233                         must_srv_mgs = 1;
234                         break;
235                 case 'k':
236                         krb_enabled = 1;
237                         break;
238                 case 'h':
239                         usage(stdout, argv[0]);
240                         break;
241                 case 's':
242 #ifdef HAVE_OPENSSL_SSK
243                         sk_enabled = 1;
244 #else
245                         fprintf(stderr, "error: request for SSK but service "
246                                 "support not enabled\n");
247                         usage(stderr, argv[0]);
248 #endif
249                         break;
250                 case 'z':
251                         null_enabled = 1;
252                         break;
253                 default:
254                         usage(stderr, argv[0]);
255                         break;
256                 }
257         }
258
259         if ((progname = strrchr(argv[0], '/')))
260                 progname++;
261         else
262                 progname = argv[0];
263
264         if (!sk_enabled && !krb_enabled && !null_enabled) {
265 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
266                 fprintf(stderr, "warning: no -k, -s, or -z option given, "
267                         "assume -k for backward compatibility\n");
268                 krb_enabled = 1;
269 #else
270                 fprintf(stderr, "error: need one of -k, -s, or -z options\n");
271                 usage(stderr, argv[0]);
272
273 #endif
274         }
275         initerr(progname, verbosity, fg);
276
277         /* For kerberos use gss mechanisms but ignore for sk and null */
278         if (krb_enabled) {
279                 if (gssd_check_mechs()) {
280                         printerr(0, "ERROR: problem with gssapi library\n");
281                         exit(1);
282                 }
283                 if (gssd_get_local_realm()) {
284                         printerr(0, "ERROR: Can't get Local Kerberos realm\n");
285                         exit(1);
286                 }
287
288                 if (get_creds &&
289                     gssd_prepare_creds(must_srv_mgs, must_srv_mds,
290                                        must_srv_oss)) {
291                         printerr(0, "unable to obtain root (machine) "
292                                  "credentials\n");
293                         printerr(0, "do you have a keytab entry for "
294                                  "<lustre_xxs>/<your.host>@<YOUR.REALM> in "
295                                  "/etc/krb5.keytab?\n");
296                         exit(1);
297                 }
298         }
299
300         if (!fg)
301                 mydaemon(0, 0);
302
303         /*
304          * XXX: There is risk of memory leak for missing call
305          *      cleanup_mapping() for SIGKILL and SIGSTOP.
306          */
307         signal(SIGINT, sig_die);
308         signal(SIGTERM, sig_die);
309         signal(SIGHUP, sig_hup);
310
311         if (!fg)
312                 release_parent();
313
314         gssd_init_unique(GSSD_SVC);
315
316         svcgssd_run();
317         cleanup_mapping();
318         printerr(0, "gssd_run returned!\n");
319         abort();
320 }