]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/svcgssd/svcgssd.c
Add gss support from citi @ umich
[nfs-utils.git] / utils / svcgssd / 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   All rights reserved, all wrongs reversed.
12
13   Redistribution and use in source and binary forms, with or without
14   modification, are permitted provided that the following conditions
15   are met:
16
17   1. Redistributions of source code must retain the above copyright
18      notice, this list of conditions and the following disclaimer.
19   2. Redistributions in binary form must reproduce the above copyright
20      notice, this list of conditions and the following disclaimer in the
21      documentation and/or other materials provided with the distribution.
22   3. Neither the name of the University nor the names of its
23      contributors may be used to endorse or promote products derived
24      from this software without specific prior written permission.
25
26   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 */
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/socket.h>
44 #include <rpc/rpc.h>
45 #include <fcntl.h>
46 #include <errno.h>
47
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 "svcgssd.h"
56 #include "gss_util.h"
57 #include "err_util.h"
58
59 /*
60  * mydaemon creates a pipe between the partent and child
61  * process. The parent process will wait until the
62  * child dies or writes a '1' on the pipe signaling
63  * that it started successfully.
64  */
65 int pipefds[2] = { -1, -1};
66
67 static void
68 mydaemon(int nochdir, int noclose)
69 {
70         int pid, status, tempfd, fdmax, filedes;
71
72         if (pipe(pipefds) < 0) {
73                 printerr(1, "mydaemon: pipe() failed: errno %d (%s)\n",
74                         errno, strerror(errno));
75                 exit(1);
76         }
77         if ((pid = fork ()) < 0) {
78                 printerr(1, "mydaemon: fork() failed: errno %d (%s)\n",
79                         errno, strerror(errno));
80                 exit(1);
81         }
82
83         if (pid != 0) {
84                 /*
85                  * Parent. Wait for status from child.
86                  */
87                 close(pipefds[1]);
88                 if (read(pipefds[0], &status, 1) != 1)
89                         exit(1);
90                 exit (0);
91         }
92         /* Child.       */
93         close(pipefds[0]);
94         setsid ();
95         if (nochdir == 0) {
96                 if (chdir ("/") == -1) {
97                         printerr(1, "mydaemon: chdir() failed: errno %d (%s)\n",
98                                 errno, strerror(errno));
99                         exit(1);
100                 }
101         }
102
103         while (pipefds[1] <= 2) {
104                 pipefds[1] = dup(pipefds[1]);
105                 if (pipefds[1] < 0) {
106                         printerr(1, "mydaemon: dup() failed: errno %d (%s)\n",
107                                 errno, strerror(errno));
108                         exit(1);
109                 }
110         }
111
112         if (noclose == 0) {
113                 tempfd = open("/dev/null", O_RDWR);
114                 close(0); dup2(tempfd, 0);
115                 close(1); dup2(tempfd, 1);
116                 close(2); dup2(tempfd, 2);
117                 fdmax = sysconf (_SC_OPEN_MAX);
118                 for (filedes = 3; filedes < fdmax; filedes++)
119                         if (filedes != pipefds[1])
120                                 close (filedes);
121         }
122
123         return;
124 }
125
126 static void
127 release_parent()
128 {
129         int status;
130
131         if (pipefds[1] > 0) {
132                 write(pipefds[1], &status, 1);
133                 close(pipefds[1]);
134                 pipefds[1] = -1;
135         }
136 }
137
138 void
139 sig_die(int signal)
140 {
141         /* destroy krb5 machine creds */
142         printerr(1, "exiting on signal %d\n", signal);
143         exit(1);
144 }
145
146 static void
147 usage(char *progname)
148 {
149         fprintf(stderr, "usage: %s [-n] [-f] [-v]\n",
150                 progname);
151         exit(1);
152 }
153
154 int
155 main(int argc, char *argv[])
156 {
157         int get_creds = 1;
158         int fg = 0;
159         int verbosity = 0;
160         int opt;
161         extern char *optarg;
162         char *progname;
163
164         while ((opt = getopt(argc, argv, "fvnp:")) != -1) {
165                 switch (opt) {
166                         case 'f':
167                                 fg = 1;
168                                 break;
169                         case 'n':
170                                 get_creds = 0;
171                                 break;
172                         case 'v':
173                                 verbosity++;
174                                 break;
175                         default:
176                                 usage(argv[0]);
177                                 break;
178                 }
179         }
180
181         if ((progname = strrchr(argv[0], '/')))
182                 progname++;
183         else
184                 progname = argv[0];
185
186         initerr(progname, verbosity, fg);
187
188         if (!fg)
189                 mydaemon(0, 0);
190
191         signal(SIGINT, sig_die);
192         signal(SIGTERM, sig_die);
193         signal(SIGHUP, sig_die);
194
195         if (get_creds && !gssd_acquire_cred(GSSD_SERVICE_NAME)) {
196                 printerr(0, "unable to obtain root (machine) credentials\n");
197                 printerr(0, "do you have a keytab entry for "
198                             "nfs/<your.host>@<YOUR.REALM> in "
199                             "/etc/krb5.keytab?\n");
200                 exit(1);
201         }
202
203         if (!fg)
204                 release_parent();
205
206         gssd_run();
207         printerr(0, "gssd_run returned!\n");
208         abort();
209 }