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 All rights reserved, all wrongs reversed.
12 Copyright (c) 2014, Intel Corporation.
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions
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.
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.
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
57 #include <libcfs/util/string.h>
62 #include "krb5_util.h"
65 char pipefs_dir[PATH_MAX] = GSSD_PIPEFS_DIR;
66 char pipefs_nfsdir[PATH_MAX] = GSSD_PIPEFS_DIR;
67 char keytabfile[PATH_MAX] = GSSD_DEFAULT_KEYTAB_FILE;
68 char ccachedir[PATH_MAX] = GSSD_DEFAULT_CRED_DIR;
70 int lgssd_mutex_downcall = -1;
72 static int lgssd_create_mutex(int *semid)
77 id = semget(IPC_PRIVATE, 1, IPC_CREAT);
79 printerr(0, "semget: %s\n", strerror(errno));
84 if (semctl(id, 0, SETVAL, arg) != 0) {
85 printerr(0, "semctl: %s\n", strerror(errno));
86 semctl(id, 1, IPC_RMID, arg);
94 void lgssd_init_mutexs(void)
96 if (lgssd_create_mutex(&lgssd_mutex_downcall)) {
97 printerr(0, "can't create downcall mutex\n");
102 void lgssd_fini_mutexs(void)
106 if (lgssd_mutex_downcall != -1)
107 semctl(lgssd_mutex_downcall, 1, IPC_RMID, arg);
110 void lgssd_mutex_get(int semid)
112 struct sembuf op[1] = { {0, -1, SEM_UNDO} };
115 rc = semop(semid, op, 1);
117 printerr(0, "exit on mutex_get err %d: %s\n",
118 rc, strerror(errno));
123 void lgssd_mutex_put(int semid)
125 struct sembuf op[1] = { {0, 1, 0} };
128 rc = semop(semid, op, 1);
130 printerr(0, "ignore mutex_put err %d: %s\n",
131 rc, strerror(errno));
135 static void lgssd_cleanup(void)
139 /* make sure all children finished */
141 child_pid = waitpid(-1, NULL, 0);
145 printerr(3, "cleanup: child %d terminated\n", child_pid);
150 /* destroy krb5 machine creds */
151 gssd_destroy_krb5_machine_creds();
157 printerr(1, "exiting on signal %d\n", signal);
165 /* don't exit on SIGHUP */
166 printerr(1, "Received SIGHUP... Ignoring.\n");
171 usage(char *progname)
173 fprintf(stderr, "usage: %s [-f] [-v] [-p pipefsdir] [-k keytab] [-d ccachedir]\n",
179 main(int argc, char *argv[])
187 while ((opt = getopt(argc, argv, "fvrmMp:k:d:")) != -1) {
199 strlcpy(pipefs_dir, optarg, sizeof(pipefs_dir));
200 if (pipefs_dir[sizeof(pipefs_dir)-1] != '\0')
201 errx(1, "pipefs path name too long");
204 strlcpy(keytabfile, optarg, sizeof(keytabfile));
205 if (keytabfile[sizeof(keytabfile)-1] != '\0')
206 errx(1, "keytab path name too long");
209 strlcpy(ccachedir, optarg, sizeof(ccachedir));
210 if (ccachedir[sizeof(ccachedir)-1] != '\0')
211 errx(1, "ccachedir path name too long");
219 if ((progname = strrchr(argv[0], '/')))
224 initerr(progname, verbosity, fg);
226 if (gssd_check_mechs() != 0)
227 errx(1, "Problem with gssapi library");
229 if (gssd_get_local_realm())
230 errx(1, "get local realm");
232 if (!fg && daemon(0, 0) < 0)
235 /* This should be checked _after_ daemon(), because we need to own
236 * the undo-able semaphore by this process
238 gssd_init_unique(GSSD_CLI);
240 /* Process keytab file and get machine credentials. This will modify
241 * disk status so do it after we are sure we are the only instance
243 if (gssd_refresh_krb5_machine_creds())
246 signal(SIGINT, sig_die);
247 signal(SIGTERM, sig_die);
248 signal(SIGHUP, sig_hup);
251 /* Determine Kerberos information from the kernel */
252 gssd_obtain_kernel_krb5_info();
257 printerr(0, "lgssd initialized and ready to serve\n");
261 printerr(0, "lgssd exiting\n");