]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/gssd/gssd.c
Add option to allow root to use credentials other than machine credentials
[nfs-utils.git] / utils / gssd / 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   Redistribution and use in source and binary forms, with or without
13   modification, are permitted provided that the following conditions
14   are met:
15
16   1. Redistributions of source code must retain the above copyright
17      notice, this list of conditions and the following disclaimer.
18   2. Redistributions in binary form must reproduce the above copyright
19      notice, this list of conditions and the following disclaimer in the
20      documentation and/or other materials provided with the distribution.
21   3. Neither the name of the University nor the names of its
22      contributors may be used to endorse or promote products derived
23      from this software without specific prior written permission.
24
25   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37 */
38
39 #include "config.h"
40
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <rpc/rpc.h>
44
45 #include <unistd.h>
46 #include <err.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <signal.h>
51 #include "gssd.h"
52 #include "err_util.h"
53 #include "gss_util.h"
54 #include "krb5_util.h"
55
56 char pipefsdir[PATH_MAX] = GSSD_PIPEFS_DIR;
57 char keytabfile[PATH_MAX] = GSSD_DEFAULT_KEYTAB_FILE;
58 char ccachedir[PATH_MAX] = GSSD_DEFAULT_CRED_DIR;
59 int  use_memcache = 0;
60 int  root_uses_machine_creds = 1;
61
62 void
63 sig_die(int signal)
64 {
65         /* destroy krb5 machine creds */
66         if (root_uses_machine_creds)
67                 gssd_destroy_krb5_machine_creds();
68         printerr(1, "exiting on signal %d\n", signal);
69         exit(1);
70 }
71
72 void
73 sig_hup(int signal)
74 {
75         /* don't exit on SIGHUP */
76         printerr(1, "Received SIGHUP... Ignoring.\n");
77         return;
78 }
79
80 static void
81 usage(char *progname)
82 {
83         fprintf(stderr, "usage: %s [-f] [-n] [-v] [-r] [-p pipefsdir] [-k keytab] [-d ccachedir]\n",
84                 progname);
85         exit(1);
86 }
87
88 int
89 main(int argc, char *argv[])
90 {
91         int fg = 0;
92         int verbosity = 0;
93         int rpc_verbosity = 0;
94         int opt;
95         extern char *optarg;
96         char *progname;
97
98         while ((opt = getopt(argc, argv, "fvrmnMp:k:d:")) != -1) {
99                 switch (opt) {
100                         case 'f':
101                                 fg = 1;
102                                 break;
103                         case 'm':
104                                 /* Accept but ignore this. Now the default. */
105                                 break;
106                         case 'M':
107                                 use_memcache = 1;
108                                 break;
109                         case 'n':
110                                 root_uses_machine_creds = 0;
111                                 break;
112                         case 'v':
113                                 verbosity++;
114                                 break;
115                         case 'r':
116                                 rpc_verbosity++;
117                                 break;
118                         case 'p':
119                                 strncpy(pipefsdir, optarg, sizeof(pipefsdir));
120                                 if (pipefsdir[sizeof(pipefsdir)-1] != '\0')
121                                         errx(1, "pipefs path name too long");
122                                 break;
123                         case 'k':
124                                 strncpy(keytabfile, optarg, sizeof(keytabfile));
125                                 if (keytabfile[sizeof(keytabfile)-1] != '\0')
126                                         errx(1, "keytab path name too long");
127                                 break;
128                         case 'd':
129                                 strncpy(ccachedir, optarg, sizeof(ccachedir));
130                                 if (ccachedir[sizeof(ccachedir)-1] != '\0')
131                                         errx(1, "ccachedir path name too long");
132                                 break;
133                         default:
134                                 usage(argv[0]);
135                                 break;
136                 }
137         }
138         strncat(pipefsdir + strlen(pipefsdir), "/" GSSD_SERVICE_NAME,
139                 sizeof(pipefsdir)-strlen(pipefsdir));
140         if (pipefsdir[sizeof(pipefsdir)-1] != '\0')
141                 errx(1, "pipefs path name too long");
142
143         if ((progname = strrchr(argv[0], '/')))
144                 progname++;
145         else
146                 progname = argv[0];
147
148         initerr(progname, verbosity, fg);
149 #ifdef HAVE_AUTHGSS_SET_DEBUG_LEVEL
150         authgss_set_debug_level(rpc_verbosity);
151 #else
152         if (rpc_verbosity > 0)
153                 printerr(0, "Warning: rpcsec_gss library does not "
154                             "support setting debug level\n");
155 #endif
156
157         if (gssd_check_mechs() != 0)
158                 errx(1, "Problem with gssapi library");
159
160         if (!fg && daemon(0, 0) < 0)
161                 errx(1, "fork");
162
163         signal(SIGINT, sig_die);
164         signal(SIGTERM, sig_die);
165         signal(SIGHUP, sig_hup);
166
167         /* Process keytab file and get machine credentials */
168         if (root_uses_machine_creds)
169                 gssd_refresh_krb5_machine_creds();
170
171         gssd_run();
172         printerr(0, "gssd_run returned!\n");
173         abort();
174 }