Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[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  * Copyright (c) 2016, Intel Corporation.
13  *
14  * All rights reserved, all wrongs reversed.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 #include "config.h"
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/socket.h>
48 #include <fcntl.h>
49 #include <errno.h>
50
51 #include <unistd.h>
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <signal.h>
57 #include <dirent.h>
58 #include "svcgssd.h"
59 #include "gss_util.h"
60 #include "err_util.h"
61 #include "lsupport.h"
62 #include <linux/lustre/lustre_ver.h>
63
64 int null_enabled;
65 int krb_enabled;
66 int sk_enabled;
67
68 static void
69 closeall(int min)
70 {
71         DIR *dir = opendir("/proc/self/fd");
72         if (dir != NULL) {
73                 int dfd = dirfd(dir);
74                 struct dirent *d;
75
76                 while ((d = readdir(dir)) != NULL) {
77                         char *endp;
78                         long n = strtol(d->d_name, &endp, 10);
79                         if (*endp != '\0' && n >= min && n != dfd)
80                                 (void) close(n);
81                 }
82                 closedir(dir);
83         } else {
84                 int fd = sysconf(_SC_OPEN_MAX);
85                 while (--fd >= min)
86                         (void) close(fd);
87         }
88 }
89
90 /*
91  * mydaemon creates a pipe between the partent and child
92  * process. The parent process will wait until the
93  * child dies or writes a '1' on the pipe signaling
94  * that it started successfully.
95  */
96 int pipefds[2] = { -1, -1};
97
98 static void
99 mydaemon(int nochdir, int noclose)
100 {
101         int pid, status, tempfd;
102
103         if (pipe(pipefds) < 0) {
104                 printerr(LL_ERR, "%s: pipe() failed: errno %d (%s)\n",
105                          __func__, errno, strerror(errno));
106                 exit(1);
107         }
108         if ((pid = fork ()) < 0) {
109                 printerr(LL_ERR, "%s: fork() failed: errno %d (%s)\n",
110                          __func__, errno, strerror(errno));
111                 exit(1);
112         }
113
114         if (pid != 0) {
115                 /*
116                  * Parent. Wait for status from child.
117                  */
118                 close(pipefds[1]);
119                 if (read(pipefds[0], &status, 1) != 1)
120                         exit(1);
121                 exit (0);
122         }
123         /* Child.       */
124         close(pipefds[0]);
125         setsid ();
126         if (nochdir == 0) {
127                 if (chdir ("/") == -1) {
128                         printerr(LL_ERR,
129                                  "%s: chdir() failed: errno %d (%s)\n",
130                                  __func__, errno, strerror(errno));
131                         exit(1);
132                 }
133         }
134
135         while (pipefds[1] <= 2) {
136                 pipefds[1] = dup(pipefds[1]);
137                 if (pipefds[1] < 0) {
138                         printerr(LL_ERR, "%s: dup() failed: errno %d (%s)\n",
139                                  __func__, errno, strerror(errno));
140                         exit(1);
141                 }
142         }
143
144         if (noclose == 0) {
145                 tempfd = open("/dev/null", O_RDWR);
146                 if (tempfd < 0) {
147                         printerr(LL_ERR, "%s: open() failed: errno %d (%s)\n",
148                                  __func__, errno, strerror(errno));
149                         exit(1);
150                 }
151                 dup2(tempfd, 0);
152                 dup2(tempfd, 1);
153                 dup2(tempfd, 2);
154                 closeall(3);
155         }
156 }
157
158 static void
159 release_parent()
160 {
161         int status;
162         ssize_t sret __attribute__ ((unused));
163
164         if (pipefds[1] > 0) {
165                 sret = write(pipefds[1], &status, 1);
166                 close(pipefds[1]);
167                 pipefds[1] = -1;
168         }
169 }
170
171 static void
172 sig_die(int signal)
173 {
174         /* destroy krb5 machine creds */
175         cleanup_mapping();
176         /* cleanup allocated strings for realms */
177         gssd_cleanup_realms();
178         /* remove socket */
179         unlink(GSS_SOCKET_PATH);
180         printerr(LL_WARN, "exiting on signal %d\n", signal);
181         if (signal == SIGTERM)
182                 exit(EXIT_SUCCESS);
183         else
184                 exit(EXIT_FAILURE);
185 }
186
187 static void
188 sig_hup(int signal)
189 {
190         /* don't exit on SIGHUP */
191         printerr(LL_WARN, "Received SIGHUP... Ignoring.\n");
192 }
193
194 static void
195 usage(FILE *fp, char *progname)
196 {
197         fprintf(fp, "usage: %s [ -fnvmogkRsz ]\n",
198                 progname);
199         fprintf(stderr, "-f             - Run in foreground\n");
200         fprintf(stderr, "-g             - Service MGS\n");
201         fprintf(stderr, "-k             - Enable kerberos support\n");
202         fprintf(stderr, "-m             - Service MDS\n");
203         fprintf(stderr, "-n             - Don't establish kerberos credentials\n");
204         fprintf(stderr, "-o             - Service OSS\n");
205         fprintf(stderr, "-R REALM       - Kerberos Realm to use, instead of default\n");
206 #ifdef HAVE_OPENSSL_SSK
207         fprintf(stderr, "-s             - Enable shared secret key support\n");
208 #endif
209         fprintf(stderr, "-v             - Verbosity\n");
210         fprintf(stderr, "-z             - Enable gssnull support\n");
211
212         exit(fp == stderr);
213 }
214
215 int
216 main(int argc, char *argv[])
217 {
218         int get_creds = 1;
219         int fg = 0;
220         int verbosity = 0;
221         int opt;
222         int must_srv_mds = 0, must_srv_oss = 0, must_srv_mgs = 0;
223         char *realm = NULL;
224         char *progname;
225
226         while ((opt = getopt(argc, argv, "fgkmnoR:svz")) != -1) {
227                 switch (opt) {
228                 case 'f':
229                         fg = 1;
230                         break;
231                 case 'g':
232                         get_creds = 1;
233                         must_srv_mgs = 1;
234                         break;
235                 case 'h':
236                         usage(stdout, argv[0]);
237                         break;
238                 case 'k':
239                         krb_enabled = 1;
240                         break;
241                 case 'm':
242                         get_creds = 1;
243                         must_srv_mds = 1;
244                         break;
245                 case 'n':
246                         get_creds = 0;
247                         break;
248                 case 'o':
249                         get_creds = 1;
250                         must_srv_oss = 1;
251                         break;
252                 case 'R':
253                         realm = optarg;
254                         break;
255                 case 's':
256 #ifdef HAVE_OPENSSL_SSK
257                         sk_enabled = 1;
258 #else
259                         fprintf(stderr, "error: request for SSK but service "
260                                 "support not enabled\n");
261                         usage(stderr, argv[0]);
262 #endif
263                         break;
264                 case 'v':
265                         verbosity++;
266                         break;
267                 case 'z':
268                         null_enabled = 1;
269                         break;
270                 default:
271                         usage(stderr, argv[0]);
272                         break;
273                 }
274         }
275
276         if ((progname = strrchr(argv[0], '/')))
277                 progname++;
278         else
279                 progname = argv[0];
280
281         if (!sk_enabled && !krb_enabled && !null_enabled) {
282 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
283                 fprintf(stderr, "warning: no -k, -s, or -z option given, "
284                         "assume -k for backward compatibility\n");
285                 krb_enabled = 1;
286 #else
287                 fprintf(stderr, "error: need one of -k, -s, or -z options\n");
288                 usage(stderr, argv[0]);
289
290 #endif
291         }
292
293         if (realm && !krb_enabled) {
294                 fprintf(stderr, "error: need -k option if -R is used\n");
295                 usage(stderr, argv[0]);
296         }
297
298         initerr(progname, verbosity, fg);
299
300         /* For kerberos use gss mechanisms but ignore for sk and null */
301         if (krb_enabled) {
302                 int ret;
303
304                 if (gssd_check_mechs()) {
305                         printerr(LL_ERR,
306                                  "ERROR: problem with gssapi library\n");
307                         exit(1);
308                 }
309                 ret = gss_get_realm(realm);
310                 if (ret) {
311                         printerr(LL_ERR, "ERROR: no Kerberos realm: %s\n",
312                                  error_message(ret));
313                         exit(1);
314                 }
315                 printerr(LL_WARN, "Kerberos realm: %s\n", krb5_this_realm);
316                 if (get_creds &&
317                     gssd_prepare_creds(must_srv_mgs, must_srv_mds,
318                                        must_srv_oss)) {
319                         printerr(LL_ERR,
320                                  "unable to obtain root (machine) credentials\n");
321                         printerr(LL_ERR,
322                                  "do you have a keytab entry for <lustre_xxs>/<your.host>@<YOUR.REALM> in /etc/krb5.keytab?\n");
323                         exit(1);
324                 }
325         }
326
327         if (!fg)
328                 mydaemon(0, 0);
329
330         /*
331          * XXX: There is risk of memory leak for missing call
332          *      cleanup_mapping() for SIGKILL and SIGSTOP.
333          */
334         signal(SIGINT, sig_die);
335         signal(SIGTERM, sig_die);
336         signal(SIGHUP, sig_hup);
337
338         if (!fg)
339                 release_parent();
340
341         gssd_init_unique(GSSD_SVC);
342
343         svcgssd_run();
344         cleanup_mapping();
345         gssd_cleanup_realms();
346         printerr(LL_ERR, "svcgssd_run returned!\n");
347         abort();
348 }