xref: /onnv-gate/usr/src/cmd/krb5/kwarn/kwarnd.c (revision 4271:dda1ded496b7)
10Sstevel@tonic-gate /*
21812Sgtb  * CDDL HEADER START
31812Sgtb  *
41812Sgtb  * The contents of this file are subject to the terms of the
51812Sgtb  * Common Development and Distribution License (the "License").
61812Sgtb  * You may not use this file except in compliance with the License.
71812Sgtb  *
81812Sgtb  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91812Sgtb  * or http://www.opensolaris.org/os/licensing.
101812Sgtb  * See the License for the specific language governing permissions
111812Sgtb  * and limitations under the License.
121812Sgtb  *
131812Sgtb  * When distributing Covered Code, include this CDDL HEADER in each
141812Sgtb  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151812Sgtb  * If applicable, add the following below this CDDL HEADER, with the
161812Sgtb  * fields enclosed by brackets "[]" replaced with your own identifying
171812Sgtb  * information: Portions Copyright [yyyy] [name of copyright owner]
181812Sgtb  *
191812Sgtb  * CDDL HEADER END
201812Sgtb  */
211812Sgtb /*
22*4271Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23358Sgtb  * Use is subject to license terms.
24358Sgtb  */
25358Sgtb 
26358Sgtb /*
270Sstevel@tonic-gate  * Usermode daemon which is responsible for sending kerberos credentials
280Sstevel@tonic-gate  * expiration warnings to the user, syslog or snmp (eventually), depending
290Sstevel@tonic-gate  * on how it is configured through /etc/krb5/warn.conf.
300Sstevel@tonic-gate  * the code in this file was borrowed from gssd.c
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <rpc/rpc.h>
370Sstevel@tonic-gate #include <sys/syslog.h>
380Sstevel@tonic-gate #include <sys/termios.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <sys/resource.h>
410Sstevel@tonic-gate #include <sys/utsname.h>
420Sstevel@tonic-gate #include <sys/systeminfo.h>
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <stropts.h>
450Sstevel@tonic-gate #include <fcntl.h>
460Sstevel@tonic-gate #include <strings.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate #include <thread.h>
491812Sgtb #include <netdb.h>
501812Sgtb #include <libgen.h>
510Sstevel@tonic-gate #include "kwarnd.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	MAXTHREADS 64
540Sstevel@tonic-gate 
550Sstevel@tonic-gate int kwarnd_debug = 0;		/* enable debugging printfs */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate extern void kwarnprog_1(struct svc_req *, register SVCXPRT *);
580Sstevel@tonic-gate static void usage(void);
590Sstevel@tonic-gate static void detachfromtty(void);
600Sstevel@tonic-gate extern int svc_create_local_service(void (*) (),
611812Sgtb 					ulong_t, ulong_t, char *, char *);
620Sstevel@tonic-gate extern void kwarnd_check_warning_list(void);
630Sstevel@tonic-gate extern bool_t loadConfigFile(void);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /* following declarations needed in rpcgen-generated code */
660Sstevel@tonic-gate int _rpcpmstart = 0;		/* Started by a port monitor ? */
670Sstevel@tonic-gate int _rpcfdtype;			/* Whether Stream or Datagram ? */
680Sstevel@tonic-gate int _rpcsvcdirty;		/* Still serving ? */
690Sstevel@tonic-gate 
701812Sgtb char myhostname[MAXHOSTNAMELEN] = {0};
711812Sgtb char progname[MAXNAMELEN] = {0};
721812Sgtb 
731812Sgtb 
740Sstevel@tonic-gate int
main(argc,argv)750Sstevel@tonic-gate main(argc, argv)
760Sstevel@tonic-gate int argc;
770Sstevel@tonic-gate char **argv;
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	register SVCXPRT *transp;
800Sstevel@tonic-gate 	extern int optind;
810Sstevel@tonic-gate 	int c;
820Sstevel@tonic-gate 	char mname[FMNAMESZ + 1];
830Sstevel@tonic-gate 	int rpc_svc_mode = RPC_SVC_MT_AUTO;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	/* set locale and domain for internationalization */
860Sstevel@tonic-gate 	setlocale(LC_ALL, "");
870Sstevel@tonic-gate 
881812Sgtb #if !defined(TEXT_DOMAIN)
891812Sgtb #define	TEXT_DOMAIN "SYS_TEST"
901812Sgtb #endif
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
930Sstevel@tonic-gate 
941812Sgtb 	(void) strlcpy(progname, basename(argv[0]), sizeof (progname));
951812Sgtb 
960Sstevel@tonic-gate 	/*
97*4271Srie 	 * Take special note that "getuid()" is called here.  This call is used
98*4271Srie 	 * rather that app_krb5_user_uid(), to ensure ktkt_warnd(1M) is running
990Sstevel@tonic-gate 	 * as root.
1001812Sgtb 	 */
1010Sstevel@tonic-gate #ifdef DEBUG
1020Sstevel@tonic-gate 	(void) setuid(0);		/* DEBUG: set ruid to root */
103358Sgtb #endif /* DEBUG */
104*4271Srie 	if (getuid()) {
1050Sstevel@tonic-gate 		(void) fprintf(stderr,
106*4271Srie 			gettext("[%s] must be run as root\n"), argv[0]);
1070Sstevel@tonic-gate #ifdef DEBUG
1080Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(" warning only\n"));
109358Sgtb #else /* !DEBUG */
1100Sstevel@tonic-gate 		exit(1);
111358Sgtb #endif /* DEBUG */
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != -1)
1150Sstevel@tonic-gate 		switch (c) {
1160Sstevel@tonic-gate 		    case 'd':
1170Sstevel@tonic-gate 			/* turn on debugging */
1180Sstevel@tonic-gate 			kwarnd_debug = 1;
1190Sstevel@tonic-gate 			break;
1200Sstevel@tonic-gate 		    default:
1210Sstevel@tonic-gate 			usage();
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (optind != argc) {
1250Sstevel@tonic-gate 		usage();
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1281812Sgtb 	(void) gethostname(myhostname, sizeof (myhostname));
1291812Sgtb 
1300Sstevel@tonic-gate 	/*
1310Sstevel@tonic-gate 	 * Started by inetd if name of module just below stream
1320Sstevel@tonic-gate 	 * head is either a sockmod or timod.
1330Sstevel@tonic-gate 	 */
1340Sstevel@tonic-gate 	if (!ioctl(0, I_LOOK, mname) &&
1350Sstevel@tonic-gate 		((strcmp(mname, "sockmod") == 0) ||
1360Sstevel@tonic-gate 			(strcmp(mname, "timod") == 0))) {
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		char *netid;
1390Sstevel@tonic-gate 		struct netconfig *nconf;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		openlog("kwarnd", LOG_PID, LOG_DAEMON);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) ==  NULL) {
1440Sstevel@tonic-gate 			netid = "ticotsord";
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		if ((nconf = getnetconfigent(netid)) == NULL) {
1480Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("cannot get transport info"));
1490Sstevel@tonic-gate 			exit(1);
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		if (strcmp(mname, "sockmod") == 0) {
1530Sstevel@tonic-gate 			if (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, "timod")) {
1540Sstevel@tonic-gate 				syslog(LOG_ERR,
1550Sstevel@tonic-gate 					gettext("could not get the "
1560Sstevel@tonic-gate 						"right module"));
1570Sstevel@tonic-gate 				exit(1);
1580Sstevel@tonic-gate 			}
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		/* XXX - is nconf even needed here? */
1620Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
1630Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("cannot create server handle"));
1640Sstevel@tonic-gate 			exit(1);
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		/*
1680Sstevel@tonic-gate 		 * We use a NULL nconf because KWARNPROG has already been
1690Sstevel@tonic-gate 		 * registered with rpcbind.
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		if (!svc_reg(transp, KWARNPROG, KWARNVERS, kwarnprog_1, NULL)) {
1720Sstevel@tonic-gate 			syslog(LOG_ERR,
1730Sstevel@tonic-gate 				gettext("unable to register "
1740Sstevel@tonic-gate 					"(KWARNPROG, KWARNVERS)"));
1750Sstevel@tonic-gate 			exit(1);
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		if (nconf)
1790Sstevel@tonic-gate 			freenetconfigent(nconf);
1800Sstevel@tonic-gate 	} else {
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		if (!kwarnd_debug)
1830Sstevel@tonic-gate 			detachfromtty();
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		openlog("kwarnd", LOG_PID, LOG_DAEMON);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		if (svc_create_local_service(kwarnprog_1, KWARNPROG, KWARNVERS,
1880Sstevel@tonic-gate 		    "netpath", "kwarnd") == 0) {
1890Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("unable to create service"));
1900Sstevel@tonic-gate 			exit(1);
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (kwarnd_debug) {
1960Sstevel@tonic-gate 		fprintf(stderr,
1970Sstevel@tonic-gate 		    gettext("kwarnd start: \n"));
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2001812Sgtb 	(void) signal(SIGCHLD, SIG_IGN);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (thr_create(NULL, 0,
2030Sstevel@tonic-gate 			(void *(*)(void *))kwarnd_check_warning_list, NULL,
2040Sstevel@tonic-gate 			THR_DETACHED | THR_DAEMON | THR_NEW_LWP,
2050Sstevel@tonic-gate 			NULL)) {
2060Sstevel@tonic-gate 		syslog(LOG_ERR,
2070Sstevel@tonic-gate 			gettext("unable to create cache_cleanup thread"));
2080Sstevel@tonic-gate 		exit(1);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (!loadConfigFile()) {
2120Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("could not read config file\n"));
2130Sstevel@tonic-gate 		exit(1);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_MTMODE_SET, &rpc_svc_mode)) {
2170Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("unable to set automatic MT mode"));
2180Sstevel@tonic-gate 		exit(1);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	svc_run();
2220Sstevel@tonic-gate 	abort();
2230Sstevel@tonic-gate 	/*NOTREACHED*/
2240Sstevel@tonic-gate #ifdef	lint
2250Sstevel@tonic-gate 	return (1);
2260Sstevel@tonic-gate #endif
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate static void
usage(void)2300Sstevel@tonic-gate usage(void)
2310Sstevel@tonic-gate {
2321812Sgtb 	(void) fprintf(stderr, gettext("usage: %s [-d]\n"), progname);
2330Sstevel@tonic-gate 	exit(1);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * detach from tty
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate static void
detachfromtty(void)2410Sstevel@tonic-gate detachfromtty(void)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	switch (fork()) {
2440Sstevel@tonic-gate 	case -1:
2450Sstevel@tonic-gate 		perror(gettext("kwarnd: can not fork"));
2460Sstevel@tonic-gate 		exit(1);
2470Sstevel@tonic-gate 		/*NOTREACHED*/
2480Sstevel@tonic-gate 	case 0:
2490Sstevel@tonic-gate 		break;
2500Sstevel@tonic-gate 	default:
2510Sstevel@tonic-gate 		exit(0);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * Close existing file descriptors, open "/dev/null" as
2560Sstevel@tonic-gate 	 * standard input, output, and error, and detach from
2570Sstevel@tonic-gate 	 * controlling terminal.
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	closefrom(0);
2600Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
2610Sstevel@tonic-gate 	(void) open("/dev/null", O_WRONLY);
2620Sstevel@tonic-gate 	(void) dup(1);
2630Sstevel@tonic-gate 	(void) setsid();
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*ARGSUSED*/
2670Sstevel@tonic-gate int
kwarnprog_1_freeresult(SVCXPRT * transport,xdrproc_t xdr_res,caddr_t res)2680Sstevel@tonic-gate kwarnprog_1_freeresult(SVCXPRT *transport, xdrproc_t xdr_res, caddr_t res)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	xdr_free(xdr_res, res);
2710Sstevel@tonic-gate 	return (1);
2720Sstevel@tonic-gate }
273