xref: /titanic_50/usr/src/cmd/gss/gssd/gssd.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*24da5b34Srie  * Common Development and Distribution License (the "License").
6*24da5b34Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*24da5b34Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Usermode daemon which assists the kernel when handling gssapi calls.
307c478bd9Sstevel@tonic-gate  * It is gssd that actually implements all gssapi calls.
317c478bd9Sstevel@tonic-gate  * Some calls, such as gss_sign, are implemented in the kernel on a per
327c478bd9Sstevel@tonic-gate  * mechanism basis.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
377c478bd9Sstevel@tonic-gate #include <rpc/rpc_com.h>
387c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
397c478bd9Sstevel@tonic-gate #include <sys/termios.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
427c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <stropts.h>
457c478bd9Sstevel@tonic-gate #include <fcntl.h>
467c478bd9Sstevel@tonic-gate #include <strings.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <syslog.h>
497c478bd9Sstevel@tonic-gate #include "gssd.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int gssd_debug = 0;		/* enable debugging printfs */
527c478bd9Sstevel@tonic-gate extern void gsscred_set_options(void);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate void gssprog_1();
557c478bd9Sstevel@tonic-gate void gssd_setup(char *);
567c478bd9Sstevel@tonic-gate static void usage(void);
577c478bd9Sstevel@tonic-gate static void daemonize_start();
587c478bd9Sstevel@tonic-gate static void daemonize_ready(unsigned char status);
597c478bd9Sstevel@tonic-gate extern int  svc_create_local_service();
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* following declarations needed in rpcgen-generated code */
627c478bd9Sstevel@tonic-gate int _rpcpmstart = 0;		/* Started by a port monitor ? */
637c478bd9Sstevel@tonic-gate int _rpcfdtype;			/* Whether Stream or Datagram ? */
647c478bd9Sstevel@tonic-gate int _rpcsvcdirty;		/* Still serving ? */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static void
687c478bd9Sstevel@tonic-gate /* LINTED */
catch_hup(int sig_num)697c478bd9Sstevel@tonic-gate catch_hup(int sig_num)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	sigset_t mask_set;  /* used to set a signal masking set. */
727c478bd9Sstevel@tonic-gate 	sigset_t old_set;   /* used to store the old mask set.   */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/* re-set the signal handler again to catch_hup, for next time */
757c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, catch_hup);
767c478bd9Sstevel@tonic-gate 	/* mask any further signals while we're inside the handler. */
777c478bd9Sstevel@tonic-gate 	(void) sigfillset(&mask_set);
787c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &mask_set, &old_set);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	gsscred_set_options();
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* let admin know the sighup was caught and conf file re-read */
837c478bd9Sstevel@tonic-gate 	syslog(LOG_INFO,
847c478bd9Sstevel@tonic-gate 	    "catch_hup: read gsscred.conf opts");
857c478bd9Sstevel@tonic-gate 	if (gssd_debug)
867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
877c478bd9Sstevel@tonic-gate 			"catch_hup: read gsscred.conf opts");
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &old_set, NULL);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate int
main(argc,argv)947c478bd9Sstevel@tonic-gate main(argc, argv)
957c478bd9Sstevel@tonic-gate int argc;
967c478bd9Sstevel@tonic-gate char **argv;
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	register SVCXPRT *transp;
997c478bd9Sstevel@tonic-gate 	int maxrecsz = RPC_MAXDATASIZE;
1007c478bd9Sstevel@tonic-gate 	extern int optind;
1017c478bd9Sstevel@tonic-gate 	int c;
1027c478bd9Sstevel@tonic-gate 	char mname[FMNAMESZ + 1];
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	/* set locale and domain for internationalization */
1057c478bd9Sstevel@tonic-gate 	setlocale(LC_ALL, "");
1067c478bd9Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
110*24da5b34Srie 	 * Take special note that "getuid()" is called here.  This call is used
111*24da5b34Srie 	 * rather than app_krb5_user_uid(), to ensure gssd(1M) is running as
112*24da5b34Srie 	 * root.
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate #ifdef DEBUG
1157c478bd9Sstevel@tonic-gate 	(void) setuid(0);		/* DEBUG: set ruid to root */
1167c478bd9Sstevel@tonic-gate #endif /* DEBUG */
117*24da5b34Srie 	if (getuid()) {
1187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1197c478bd9Sstevel@tonic-gate 			gettext("[%s] must be run as root\n"), argv[0]);
1207c478bd9Sstevel@tonic-gate #ifdef DEBUG
1217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(" warning only\n"));
1227c478bd9Sstevel@tonic-gate #else /* DEBUG */
1237c478bd9Sstevel@tonic-gate 		exit(1);
1247c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	gssd_setup(argv[0]);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != -1)
1307c478bd9Sstevel@tonic-gate 		switch (c) {
1317c478bd9Sstevel@tonic-gate 		    case 'd':
1327c478bd9Sstevel@tonic-gate 			/* turn on debugging */
1337c478bd9Sstevel@tonic-gate 			gssd_debug = 1;
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		    default:
1367c478bd9Sstevel@tonic-gate 			usage();
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (optind != argc) {
1407c478bd9Sstevel@tonic-gate 		usage();
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	gsscred_set_options();
1447c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, catch_hup);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Started by inetd if name of module just below stream
1487c478bd9Sstevel@tonic-gate 	 * head is either a sockmod or timod.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	if (!ioctl(0, I_LOOK, mname) &&
1517c478bd9Sstevel@tonic-gate 		((strcmp(mname, "sockmod") == 0) ||
1527c478bd9Sstevel@tonic-gate 			(strcmp(mname, "timod") == 0))) {
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		char *netid;
1557c478bd9Sstevel@tonic-gate 		struct netconfig *nconf;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		openlog("gssd", LOG_PID, LOG_DAEMON);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) ==  NULL) {
1607c478bd9Sstevel@tonic-gate 			netid = "ticotsord";
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		if ((nconf = getnetconfigent(netid)) == NULL) {
1647c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("cannot get transport info"));
1657c478bd9Sstevel@tonic-gate 			exit(1);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		if (strcmp(mname, "sockmod") == 0) {
1697c478bd9Sstevel@tonic-gate 			if (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, "timod")) {
1707c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
1717c478bd9Sstevel@tonic-gate 					gettext("could not get the "
1727c478bd9Sstevel@tonic-gate 						"right module"));
1737c478bd9Sstevel@tonic-gate 				exit(1);
1747c478bd9Sstevel@tonic-gate 			}
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 		if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) {
1777c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
1787c478bd9Sstevel@tonic-gate 				gettext("unable to set RPC max record size"));
1797c478bd9Sstevel@tonic-gate 			exit(1);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 		/* XXX - is nconf even needed here? */
1827c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
1837c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("cannot create server handle"));
1847c478bd9Sstevel@tonic-gate 			exit(1);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		/*
1887c478bd9Sstevel@tonic-gate 		 * We use a NULL nconf because GSSPROG has already been
1897c478bd9Sstevel@tonic-gate 		 * registered with rpcbind.
1907c478bd9Sstevel@tonic-gate 		 */
1917c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, GSSPROG, GSSVERS, gssprog_1, NULL)) {
1927c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
1937c478bd9Sstevel@tonic-gate 				gettext("unable to register "
1947c478bd9Sstevel@tonic-gate 					"(GSSPROG, GSSVERS)"));
1957c478bd9Sstevel@tonic-gate 			exit(1);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		if (nconf)
1997c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
2007c478bd9Sstevel@tonic-gate 	} else {
2017c478bd9Sstevel@tonic-gate 		if (!gssd_debug)
2027c478bd9Sstevel@tonic-gate 			daemonize_start();
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		openlog("gssd", LOG_PID, LOG_DAEMON);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		if (svc_create_local_service(gssprog_1, GSSPROG, GSSVERS,
2077c478bd9Sstevel@tonic-gate 		    "netpath", "gssd") == 0) {
2087c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("unable to create service"));
2097c478bd9Sstevel@tonic-gate 			exit(1);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/* service created, now the daemon parent can exit */
2137c478bd9Sstevel@tonic-gate 		daemonize_ready(0);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (gssd_debug) {
2187c478bd9Sstevel@tonic-gate 		fprintf(stderr,
2197c478bd9Sstevel@tonic-gate 		    gettext("gssd start: \n"));
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	svc_run();
2227c478bd9Sstevel@tonic-gate 	abort();
2237c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2247c478bd9Sstevel@tonic-gate #ifdef	lint
2257c478bd9Sstevel@tonic-gate 	return (1);
2267c478bd9Sstevel@tonic-gate #endif
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static void
usage(void)2307c478bd9Sstevel@tonic-gate usage(void)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: gssd [-dg]\n"));
2337c478bd9Sstevel@tonic-gate 	exit(1);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * Fork, detach from tty, etc...
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate static int write_pipe_fd = -1;
2417c478bd9Sstevel@tonic-gate static
2427c478bd9Sstevel@tonic-gate void
daemonize_start()2437c478bd9Sstevel@tonic-gate daemonize_start()
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	int pipe_fds[2];
2467c478bd9Sstevel@tonic-gate 	unsigned char status = 1;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	closefrom(0);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* Open stdin/out/err, chdir, get a pipe */
2517c478bd9Sstevel@tonic-gate 	if (open("/dev/null", O_RDONLY) < 0 ||
2527c478bd9Sstevel@tonic-gate 	    open("/dev/null", O_WRONLY) < 0 || dup(1) < 0 ||
2537c478bd9Sstevel@tonic-gate 	    chdir("/") < 0 || pipe(pipe_fds) < 0)
2547c478bd9Sstevel@tonic-gate 		exit(1);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/* For daemonize_ready() */
2577c478bd9Sstevel@tonic-gate 	write_pipe_fd = pipe_fds[1];
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	switch (fork()) {
2607c478bd9Sstevel@tonic-gate 	case -1:
2617c478bd9Sstevel@tonic-gate 		exit(1);
2627c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2637c478bd9Sstevel@tonic-gate 	case 0:
2647c478bd9Sstevel@tonic-gate 		break;
2657c478bd9Sstevel@tonic-gate 	default:
2667c478bd9Sstevel@tonic-gate 		/* Wait for child to be ready befor exiting */
2677c478bd9Sstevel@tonic-gate 		(void) close(pipe_fds[1]);
2687c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, SIG_DFL);
2697c478bd9Sstevel@tonic-gate 		(void) read(pipe_fds[0], &status, sizeof (status));
2707c478bd9Sstevel@tonic-gate 		exit(status);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	(void) close(pipe_fds[0]);
2747c478bd9Sstevel@tonic-gate 	(void) setsid();
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static
2787c478bd9Sstevel@tonic-gate void
daemonize_ready(unsigned char status)2797c478bd9Sstevel@tonic-gate daemonize_ready(unsigned char status)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	if (write_pipe_fd == -1)
2827c478bd9Sstevel@tonic-gate 		return;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	(void) write(write_pipe_fd, &status, sizeof (status));
2857c478bd9Sstevel@tonic-gate 	(void) close(write_pipe_fd);
2867c478bd9Sstevel@tonic-gate 	write_pipe_fd = -1;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2907c478bd9Sstevel@tonic-gate int
gssprog_1_freeresult(SVCXPRT * transport,xdrproc_t xdr_res,caddr_t res)2917c478bd9Sstevel@tonic-gate gssprog_1_freeresult(SVCXPRT *transport, xdrproc_t xdr_res, caddr_t res)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	xdr_free(xdr_res, res);
2947c478bd9Sstevel@tonic-gate 	return (1);
2957c478bd9Sstevel@tonic-gate }
296