10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*4271Srie * Common Development and Distribution License (the "License").
6*4271Srie * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*4271Srie * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Usermode daemon which assists the kernel when handling gssapi calls.
300Sstevel@tonic-gate * It is gssd that actually implements all gssapi calls.
310Sstevel@tonic-gate * Some calls, such as gss_sign, are implemented in the kernel on a per
320Sstevel@tonic-gate * mechanism basis.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <rpc/rpc.h>
370Sstevel@tonic-gate #include <rpc/rpc_com.h>
380Sstevel@tonic-gate #include <sys/syslog.h>
390Sstevel@tonic-gate #include <sys/termios.h>
400Sstevel@tonic-gate #include <unistd.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 <signal.h>
480Sstevel@tonic-gate #include <syslog.h>
490Sstevel@tonic-gate #include "gssd.h"
500Sstevel@tonic-gate
510Sstevel@tonic-gate int gssd_debug = 0; /* enable debugging printfs */
520Sstevel@tonic-gate extern void gsscred_set_options(void);
530Sstevel@tonic-gate
540Sstevel@tonic-gate void gssprog_1();
550Sstevel@tonic-gate void gssd_setup(char *);
560Sstevel@tonic-gate static void usage(void);
570Sstevel@tonic-gate static void daemonize_start();
580Sstevel@tonic-gate static void daemonize_ready(unsigned char status);
590Sstevel@tonic-gate extern int svc_create_local_service();
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* following declarations needed in rpcgen-generated code */
620Sstevel@tonic-gate int _rpcpmstart = 0; /* Started by a port monitor ? */
630Sstevel@tonic-gate int _rpcfdtype; /* Whether Stream or Datagram ? */
640Sstevel@tonic-gate int _rpcsvcdirty; /* Still serving ? */
650Sstevel@tonic-gate
660Sstevel@tonic-gate
670Sstevel@tonic-gate static void
680Sstevel@tonic-gate /* LINTED */
catch_hup(int sig_num)690Sstevel@tonic-gate catch_hup(int sig_num)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate sigset_t mask_set; /* used to set a signal masking set. */
720Sstevel@tonic-gate sigset_t old_set; /* used to store the old mask set. */
730Sstevel@tonic-gate
740Sstevel@tonic-gate /* re-set the signal handler again to catch_hup, for next time */
750Sstevel@tonic-gate (void) signal(SIGHUP, catch_hup);
760Sstevel@tonic-gate /* mask any further signals while we're inside the handler. */
770Sstevel@tonic-gate (void) sigfillset(&mask_set);
780Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &mask_set, &old_set);
790Sstevel@tonic-gate
800Sstevel@tonic-gate gsscred_set_options();
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* let admin know the sighup was caught and conf file re-read */
830Sstevel@tonic-gate syslog(LOG_INFO,
840Sstevel@tonic-gate "catch_hup: read gsscred.conf opts");
850Sstevel@tonic-gate if (gssd_debug)
860Sstevel@tonic-gate (void) fprintf(stderr,
870Sstevel@tonic-gate "catch_hup: read gsscred.conf opts");
880Sstevel@tonic-gate
890Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &old_set, NULL);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate
930Sstevel@tonic-gate int
main(argc,argv)940Sstevel@tonic-gate main(argc, argv)
950Sstevel@tonic-gate int argc;
960Sstevel@tonic-gate char **argv;
970Sstevel@tonic-gate {
980Sstevel@tonic-gate register SVCXPRT *transp;
990Sstevel@tonic-gate int maxrecsz = RPC_MAXDATASIZE;
1000Sstevel@tonic-gate extern int optind;
1010Sstevel@tonic-gate int c;
1020Sstevel@tonic-gate char mname[FMNAMESZ + 1];
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /* set locale and domain for internationalization */
1050Sstevel@tonic-gate setlocale(LC_ALL, "");
1060Sstevel@tonic-gate textdomain(TEXT_DOMAIN);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
110*4271Srie * Take special note that "getuid()" is called here. This call is used
111*4271Srie * rather than app_krb5_user_uid(), to ensure gssd(1M) is running as
112*4271Srie * root.
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate #ifdef DEBUG
1150Sstevel@tonic-gate (void) setuid(0); /* DEBUG: set ruid to root */
1160Sstevel@tonic-gate #endif /* DEBUG */
117*4271Srie if (getuid()) {
1180Sstevel@tonic-gate (void) fprintf(stderr,
119*4271Srie gettext("[%s] must be run as root\n"), argv[0]);
1200Sstevel@tonic-gate #ifdef DEBUG
1210Sstevel@tonic-gate (void) fprintf(stderr, gettext(" warning only\n"));
1220Sstevel@tonic-gate #else /* DEBUG */
1230Sstevel@tonic-gate exit(1);
1240Sstevel@tonic-gate #endif /* DEBUG */
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate gssd_setup(argv[0]);
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate while ((c = getopt(argc, argv, "d")) != -1)
1300Sstevel@tonic-gate switch (c) {
1310Sstevel@tonic-gate case 'd':
1320Sstevel@tonic-gate /* turn on debugging */
1330Sstevel@tonic-gate gssd_debug = 1;
1340Sstevel@tonic-gate break;
1350Sstevel@tonic-gate default:
1360Sstevel@tonic-gate usage();
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (optind != argc) {
1400Sstevel@tonic-gate usage();
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate gsscred_set_options();
1440Sstevel@tonic-gate (void) signal(SIGHUP, catch_hup);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * Started by inetd if name of module just below stream
1480Sstevel@tonic-gate * head is either a sockmod or timod.
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate if (!ioctl(0, I_LOOK, mname) &&
1510Sstevel@tonic-gate ((strcmp(mname, "sockmod") == 0) ||
1520Sstevel@tonic-gate (strcmp(mname, "timod") == 0))) {
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate char *netid;
1550Sstevel@tonic-gate struct netconfig *nconf;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate openlog("gssd", LOG_PID, LOG_DAEMON);
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if ((netid = getenv("NLSPROVIDER")) == NULL) {
1600Sstevel@tonic-gate netid = "ticotsord";
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if ((nconf = getnetconfigent(netid)) == NULL) {
1640Sstevel@tonic-gate syslog(LOG_ERR, gettext("cannot get transport info"));
1650Sstevel@tonic-gate exit(1);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (strcmp(mname, "sockmod") == 0) {
1690Sstevel@tonic-gate if (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, "timod")) {
1700Sstevel@tonic-gate syslog(LOG_ERR,
1710Sstevel@tonic-gate gettext("could not get the "
1720Sstevel@tonic-gate "right module"));
1730Sstevel@tonic-gate exit(1);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) {
1770Sstevel@tonic-gate syslog(LOG_ERR,
1780Sstevel@tonic-gate gettext("unable to set RPC max record size"));
1790Sstevel@tonic-gate exit(1);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate /* XXX - is nconf even needed here? */
1820Sstevel@tonic-gate if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
1830Sstevel@tonic-gate syslog(LOG_ERR, gettext("cannot create server handle"));
1840Sstevel@tonic-gate exit(1);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate * We use a NULL nconf because GSSPROG has already been
1890Sstevel@tonic-gate * registered with rpcbind.
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate if (!svc_reg(transp, GSSPROG, GSSVERS, gssprog_1, NULL)) {
1920Sstevel@tonic-gate syslog(LOG_ERR,
1930Sstevel@tonic-gate gettext("unable to register "
1940Sstevel@tonic-gate "(GSSPROG, GSSVERS)"));
1950Sstevel@tonic-gate exit(1);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (nconf)
1990Sstevel@tonic-gate freenetconfigent(nconf);
2000Sstevel@tonic-gate } else {
2010Sstevel@tonic-gate if (!gssd_debug)
2020Sstevel@tonic-gate daemonize_start();
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate openlog("gssd", LOG_PID, LOG_DAEMON);
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate if (svc_create_local_service(gssprog_1, GSSPROG, GSSVERS,
2070Sstevel@tonic-gate "netpath", "gssd") == 0) {
2080Sstevel@tonic-gate syslog(LOG_ERR, gettext("unable to create service"));
2090Sstevel@tonic-gate exit(1);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /* service created, now the daemon parent can exit */
2130Sstevel@tonic-gate daemonize_ready(0);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate if (gssd_debug) {
2180Sstevel@tonic-gate fprintf(stderr,
2190Sstevel@tonic-gate gettext("gssd start: \n"));
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 {
2320Sstevel@tonic-gate (void) fprintf(stderr, gettext("usage: gssd [-dg]\n"));
2330Sstevel@tonic-gate exit(1);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate * Fork, detach from tty, etc...
2390Sstevel@tonic-gate */
2400Sstevel@tonic-gate static int write_pipe_fd = -1;
2410Sstevel@tonic-gate static
2420Sstevel@tonic-gate void
daemonize_start()2430Sstevel@tonic-gate daemonize_start()
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate int pipe_fds[2];
2460Sstevel@tonic-gate unsigned char status = 1;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate closefrom(0);
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /* Open stdin/out/err, chdir, get a pipe */
2510Sstevel@tonic-gate if (open("/dev/null", O_RDONLY) < 0 ||
2520Sstevel@tonic-gate open("/dev/null", O_WRONLY) < 0 || dup(1) < 0 ||
2530Sstevel@tonic-gate chdir("/") < 0 || pipe(pipe_fds) < 0)
2540Sstevel@tonic-gate exit(1);
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /* For daemonize_ready() */
2570Sstevel@tonic-gate write_pipe_fd = pipe_fds[1];
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate switch (fork()) {
2600Sstevel@tonic-gate case -1:
2610Sstevel@tonic-gate exit(1);
2620Sstevel@tonic-gate /* NOTREACHED */
2630Sstevel@tonic-gate case 0:
2640Sstevel@tonic-gate break;
2650Sstevel@tonic-gate default:
2660Sstevel@tonic-gate /* Wait for child to be ready befor exiting */
2670Sstevel@tonic-gate (void) close(pipe_fds[1]);
2680Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_DFL);
2690Sstevel@tonic-gate (void) read(pipe_fds[0], &status, sizeof (status));
2700Sstevel@tonic-gate exit(status);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate (void) close(pipe_fds[0]);
2740Sstevel@tonic-gate (void) setsid();
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate static
2780Sstevel@tonic-gate void
daemonize_ready(unsigned char status)2790Sstevel@tonic-gate daemonize_ready(unsigned char status)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate if (write_pipe_fd == -1)
2820Sstevel@tonic-gate return;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate (void) write(write_pipe_fd, &status, sizeof (status));
2850Sstevel@tonic-gate (void) close(write_pipe_fd);
2860Sstevel@tonic-gate write_pipe_fd = -1;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /*ARGSUSED*/
2900Sstevel@tonic-gate int
gssprog_1_freeresult(SVCXPRT * transport,xdrproc_t xdr_res,caddr_t res)2910Sstevel@tonic-gate gssprog_1_freeresult(SVCXPRT *transport, xdrproc_t xdr_res, caddr_t res)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate xdr_free(xdr_res, res);
2940Sstevel@tonic-gate return (1);
2950Sstevel@tonic-gate }
296