4 * Copyright (c) 2000 The Regents of the University of Michigan.
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>.
12 * Copyright (c) 2016, Intel Corporation.
14 * All rights reserved, all wrongs reversed.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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.
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.
44 #include <sys/param.h>
45 #include <sys/types.h>
47 #include <sys/socket.h>
62 #include <linux/lustre/lustre_ver.h>
71 DIR *dir = opendir("/proc/self/fd");
76 while ((d = readdir(dir)) != NULL) {
78 long n = strtol(d->d_name, &endp, 10);
79 if (*endp != '\0' && n >= min && n != dfd)
84 int fd = sysconf(_SC_OPEN_MAX);
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.
96 int pipefds[2] = { -1, -1};
99 mydaemon(int nochdir, int noclose)
101 int pid, status, tempfd;
103 if (pipe(pipefds) < 0) {
104 printerr(1, "mydaemon: pipe() failed: errno %d (%s)\n",
105 errno, strerror(errno));
108 if ((pid = fork ()) < 0) {
109 printerr(1, "mydaemon: fork() failed: errno %d (%s)\n",
110 errno, strerror(errno));
116 * Parent. Wait for status from child.
119 if (read(pipefds[0], &status, 1) != 1)
127 if (chdir ("/") == -1) {
128 printerr(1, "mydaemon: chdir() failed: errno %d (%s)\n",
129 errno, strerror(errno));
134 while (pipefds[1] <= 2) {
135 pipefds[1] = dup(pipefds[1]);
136 if (pipefds[1] < 0) {
137 printerr(1, "mydaemon: dup() failed: errno %d (%s)\n",
138 errno, strerror(errno));
144 tempfd = open("/dev/null", O_RDWR);
158 ssize_t sret __attribute__ ((unused));
160 if (pipefds[1] > 0) {
161 sret = write(pipefds[1], &status, 1);
170 /* destroy krb5 machine creds */
172 printerr(1, "exiting on signal %d\n", signal);
179 /* don't exit on SIGHUP */
180 printerr(1, "Received SIGHUP... Ignoring.\n");
185 usage(FILE *fp, char *progname)
187 fprintf(fp, "usage: %s [ -fnvmogk ]\n",
189 fprintf(stderr, "-f - Run in foreground\n");
190 fprintf(stderr, "-n - Don't establish kerberos credentials\n");
191 fprintf(stderr, "-v - Verbosity\n");
192 fprintf(stderr, "-m - Service MDS\n");
193 fprintf(stderr, "-o - Service OSS\n");
194 fprintf(stderr, "-g - Service MGS\n");
195 fprintf(stderr, "-k - Enable kerberos support\n");
196 #ifdef HAVE_OPENSSL_SSK
197 fprintf(stderr, "-s - Enable shared secret key support\n");
199 fprintf(stderr, "-z - Enable gssnull support\n");
205 main(int argc, char *argv[])
211 int must_srv_mds = 0, must_srv_oss = 0, must_srv_mgs = 0;
214 while ((opt = getopt(argc, argv, "fnvmogksz")) != -1) {
241 usage(stdout, argv[0]);
244 #ifdef HAVE_OPENSSL_SSK
247 fprintf(stderr, "error: request for SSK but service "
248 "support not enabled\n");
249 usage(stderr, argv[0]);
256 usage(stderr, argv[0]);
261 if ((progname = strrchr(argv[0], '/')))
266 if (!sk_enabled && !krb_enabled && !null_enabled) {
267 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
268 fprintf(stderr, "warning: no -k, -s, or -z option given, "
269 "assume -k for backward compatibility\n");
272 fprintf(stderr, "error: need one of -k, -s, or -z options\n");
273 usage(stderr, argv[0]);
277 initerr(progname, verbosity, fg);
279 /* For kerberos use gss mechanisms but ignore for sk and null */
281 if (gssd_check_mechs()) {
282 printerr(0, "ERROR: problem with gssapi library\n");
285 if (gssd_get_local_realm()) {
286 printerr(0, "ERROR: Can't get Local Kerberos realm\n");
291 gssd_prepare_creds(must_srv_mgs, must_srv_mds,
293 printerr(0, "unable to obtain root (machine) "
295 printerr(0, "do you have a keytab entry for "
296 "<lustre_xxs>/<your.host>@<YOUR.REALM> in "
297 "/etc/krb5.keytab?\n");
306 * XXX: There is risk of memory leak for missing call
307 * cleanup_mapping() for SIGKILL and SIGSTOP.
309 signal(SIGINT, sig_die);
310 signal(SIGTERM, sig_die);
311 signal(SIGHUP, sig_hup);
316 gssd_init_unique(GSSD_SVC);
320 printerr(0, "gssd_run returned!\n");