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