Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / cmd / gss / gssd / gssd.c
blobf787f3f0da748e1c5cd2a7285fabc6a19d02ed4e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Usermode daemon which assists the kernel when handling gssapi calls.
30 * It is gssd that actually implements all gssapi calls.
31 * Some calls, such as gss_sign, are implemented in the kernel on a per
32 * mechanism basis.
35 #include <stdio.h>
36 #include <rpc/rpc.h>
37 #include <rpc/rpc_com.h>
38 #include <sys/syslog.h>
39 #include <sys/termios.h>
40 #include <unistd.h>
41 #include <sys/utsname.h>
42 #include <sys/systeminfo.h>
43 #include <stdlib.h>
44 #include <stropts.h>
45 #include <fcntl.h>
46 #include <strings.h>
47 #include <signal.h>
48 #include <syslog.h>
49 #include "gssd.h"
51 int gssd_debug = 0; /* enable debugging printfs */
52 extern void gsscred_set_options(void);
54 void gssprog_1();
55 void gssd_setup(char *);
56 static void usage(void);
57 static void daemonize_start();
58 static void daemonize_ready(unsigned char status);
59 extern int svc_create_local_service();
61 /* following declarations needed in rpcgen-generated code */
62 int _rpcpmstart = 0; /* Started by a port monitor ? */
63 int _rpcfdtype; /* Whether Stream or Datagram ? */
64 int _rpcsvcdirty; /* Still serving ? */
67 static void
68 /* LINTED */
69 catch_hup(int sig_num)
71 sigset_t mask_set; /* used to set a signal masking set. */
72 sigset_t old_set; /* used to store the old mask set. */
74 /* re-set the signal handler again to catch_hup, for next time */
75 (void) signal(SIGHUP, catch_hup);
76 /* mask any further signals while we're inside the handler. */
77 (void) sigfillset(&mask_set);
78 (void) sigprocmask(SIG_SETMASK, &mask_set, &old_set);
80 gsscred_set_options();
82 /* let admin know the sighup was caught and conf file re-read */
83 syslog(LOG_INFO,
84 "catch_hup: read gsscred.conf opts");
85 if (gssd_debug)
86 (void) fprintf(stderr,
87 "catch_hup: read gsscred.conf opts");
89 (void) sigprocmask(SIG_SETMASK, &old_set, NULL);
93 int
94 main(argc, argv)
95 int argc;
96 char **argv;
98 register SVCXPRT *transp;
99 int maxrecsz = RPC_MAXDATASIZE;
100 extern int optind;
101 int c;
102 char mname[FMNAMESZ + 1];
104 /* set locale and domain for internationalization */
105 setlocale(LC_ALL, "");
106 textdomain(TEXT_DOMAIN);
110 * Take special note that "getuid()" is called here. This call is used
111 * rather than app_krb5_user_uid(), to ensure gssd(8) is running as
112 * root.
114 #ifdef DEBUG
115 (void) setuid(0); /* DEBUG: set ruid to root */
116 #endif /* DEBUG */
117 if (getuid()) {
118 (void) fprintf(stderr,
119 gettext("[%s] must be run as root\n"), argv[0]);
120 #ifdef DEBUG
121 (void) fprintf(stderr, gettext(" warning only\n"));
122 #else /* DEBUG */
123 exit(1);
124 #endif /* DEBUG */
127 gssd_setup(argv[0]);
129 while ((c = getopt(argc, argv, "d")) != -1)
130 switch (c) {
131 case 'd':
132 /* turn on debugging */
133 gssd_debug = 1;
134 break;
135 default:
136 usage();
139 if (optind != argc) {
140 usage();
143 gsscred_set_options();
144 (void) signal(SIGHUP, catch_hup);
147 * Started by inetd if name of module just below stream
148 * head is either a sockmod or timod.
150 if (!ioctl(0, I_LOOK, mname) &&
151 ((strcmp(mname, "sockmod") == 0) ||
152 (strcmp(mname, "timod") == 0))) {
154 char *netid;
155 struct netconfig *nconf;
157 openlog("gssd", LOG_PID, LOG_DAEMON);
159 if ((netid = getenv("NLSPROVIDER")) == NULL) {
160 netid = "ticotsord";
163 if ((nconf = getnetconfigent(netid)) == NULL) {
164 syslog(LOG_ERR, gettext("cannot get transport info"));
165 exit(1);
168 if (strcmp(mname, "sockmod") == 0) {
169 if (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, "timod")) {
170 syslog(LOG_ERR,
171 gettext("could not get the "
172 "right module"));
173 exit(1);
176 if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) {
177 syslog(LOG_ERR,
178 gettext("unable to set RPC max record size"));
179 exit(1);
181 /* XXX - is nconf even needed here? */
182 if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
183 syslog(LOG_ERR, gettext("cannot create server handle"));
184 exit(1);
188 * We use a NULL nconf because GSSPROG has already been
189 * registered with rpcbind.
191 if (!svc_reg(transp, GSSPROG, GSSVERS, gssprog_1, NULL)) {
192 syslog(LOG_ERR,
193 gettext("unable to register "
194 "(GSSPROG, GSSVERS)"));
195 exit(1);
198 if (nconf)
199 freenetconfigent(nconf);
200 } else {
201 if (!gssd_debug)
202 daemonize_start();
204 openlog("gssd", LOG_PID, LOG_DAEMON);
206 if (svc_create_local_service(gssprog_1, GSSPROG, GSSVERS,
207 "netpath", "gssd") == 0) {
208 syslog(LOG_ERR, gettext("unable to create service"));
209 exit(1);
212 /* service created, now the daemon parent can exit */
213 daemonize_ready(0);
217 if (gssd_debug) {
218 fprintf(stderr,
219 gettext("gssd start: \n"));
221 svc_run();
222 abort();
223 /*NOTREACHED*/
226 static void
227 usage(void)
229 (void) fprintf(stderr, gettext("usage: gssd [-dg]\n"));
230 exit(1);
235 * Fork, detach from tty, etc...
237 static int write_pipe_fd = -1;
238 static
239 void
240 daemonize_start()
242 int pipe_fds[2];
243 unsigned char status = 1;
245 closefrom(0);
247 /* Open stdin/out/err, chdir, get a pipe */
248 if (open("/dev/null", O_RDONLY) < 0 ||
249 open("/dev/null", O_WRONLY) < 0 || dup(1) < 0 ||
250 chdir("/") < 0 || pipe(pipe_fds) < 0)
251 exit(1);
253 /* For daemonize_ready() */
254 write_pipe_fd = pipe_fds[1];
256 switch (fork()) {
257 case -1:
258 exit(1);
259 /* NOTREACHED */
260 case 0:
261 break;
262 default:
263 /* Wait for child to be ready befor exiting */
264 (void) close(pipe_fds[1]);
265 (void) signal(SIGPIPE, SIG_DFL);
266 (void) read(pipe_fds[0], &status, sizeof (status));
267 exit(status);
270 (void) close(pipe_fds[0]);
271 (void) setsid();
274 static
275 void
276 daemonize_ready(unsigned char status)
278 if (write_pipe_fd == -1)
279 return;
281 (void) write(write_pipe_fd, &status, sizeof (status));
282 (void) close(write_pipe_fd);
283 write_pipe_fd = -1;
286 /*ARGSUSED*/
288 gssprog_1_freeresult(SVCXPRT *transport, xdrproc_t xdr_res, caddr_t res)
290 xdr_free(xdr_res, res);
291 return (1);