xref: /illumos-gate/usr/src/cmd/ypcmd/yp_b_svc.c (revision 2a8bcb4e)
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*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * 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*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
36*004388ebScasper #include <stdio_ext.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
407c478bd9Sstevel@tonic-gate #include <memory.h>
417c478bd9Sstevel@tonic-gate #include <netconfig.h>
427c478bd9Sstevel@tonic-gate #include <syslog.h>
437c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
447c478bd9Sstevel@tonic-gate #include "yp_b.h"
457c478bd9Sstevel@tonic-gate #include <sys/resource.h>
467c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <tiuser.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #ifdef DEBUG
547c478bd9Sstevel@tonic-gate #define	RPC_SVC_FG
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	_RPCSVC_CLOSEDOWN 120
587c478bd9Sstevel@tonic-gate #define	YPBIND_ERR_ERR 1		/* Internal error */
597c478bd9Sstevel@tonic-gate #define	YPBIND_ERR_NOSERV 2		/* No bound server for passed domain */
607c478bd9Sstevel@tonic-gate #define	YPBIND_ERR_RESC 3		/* System resource allocation failure */
617c478bd9Sstevel@tonic-gate #define	YPBIND_ERR_NODOMAIN 4		/* Domain doesn't exist */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static	int _rpcpmstart;	/* Started by a port monitor ? */
647c478bd9Sstevel@tonic-gate static	int _rpcsvcdirty;	/* Still serving ? */
657c478bd9Sstevel@tonic-gate int	setok = YPSETNONE;	/* who is allowed to ypset */
667c478bd9Sstevel@tonic-gate int	broadcast = 0;
677c478bd9Sstevel@tonic-gate int	cache_okay = 0;		/* if set, then bindings are cached in files */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate extern int sigcld_event;
707c478bd9Sstevel@tonic-gate extern void broadcast_proc_exit();
717c478bd9Sstevel@tonic-gate extern int __rpc_negotiate_uid();
727c478bd9Sstevel@tonic-gate extern bool_t __rpcbind_is_up();
737c478bd9Sstevel@tonic-gate extern void ypbind_init_default();
747c478bd9Sstevel@tonic-gate static void set_signal_handlers();
757c478bd9Sstevel@tonic-gate static void clear_bindings();
767c478bd9Sstevel@tonic-gate static void unregister(int);
777c478bd9Sstevel@tonic-gate static int void_close(void *, int);
787c478bd9Sstevel@tonic-gate void closedown();
797c478bd9Sstevel@tonic-gate void ypbindprog_3();
807c478bd9Sstevel@tonic-gate void ypbindprog_2();
817c478bd9Sstevel@tonic-gate void msgout();
827c478bd9Sstevel@tonic-gate extern void cache_transport();
837c478bd9Sstevel@tonic-gate extern void clean_cache();
847c478bd9Sstevel@tonic-gate 
85a506a34cSth int
main(argc,argv)867c478bd9Sstevel@tonic-gate main(argc, argv)
877c478bd9Sstevel@tonic-gate 	int argc;
887c478bd9Sstevel@tonic-gate 	char **argv;
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	pid_t pid;
917c478bd9Sstevel@tonic-gate 	int pfd[2];
927c478bd9Sstevel@tonic-gate 	char domain[256], servers[300];
937c478bd9Sstevel@tonic-gate 	char **Argv = argv;
947c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
957c478bd9Sstevel@tonic-gate 	void *nc_handle;
967c478bd9Sstevel@tonic-gate 	int loopback_found = 0, udp_found = 0;
977c478bd9Sstevel@tonic-gate 	int pipe_closed = 0;
987c478bd9Sstevel@tonic-gate 	struct rlimit rl;
997c478bd9Sstevel@tonic-gate 	int connmaxrec = RPC_MAXDATASIZE;
1007c478bd9Sstevel@tonic-gate 	uint32_t inet_tpts = 0, inet6_tpts = 0;
1017c478bd9Sstevel@tonic-gate 	uint32_t inet_desired_tpts = 0, inet6_desired_tpts = 0;
1027c478bd9Sstevel@tonic-gate 	bool_t exclbind = TRUE;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "must be root to run %s\n", argv[0]);
1067c478bd9Sstevel@tonic-gate 		exit(1);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	argc--;
1107c478bd9Sstevel@tonic-gate 	argv++;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	while (argc > 0) {
1137c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-ypset") == 0) {
1147c478bd9Sstevel@tonic-gate 			setok = YPSETALL;
1157c478bd9Sstevel@tonic-gate 		} else if (strcmp(*argv, "-ypsetme") == 0) {
1167c478bd9Sstevel@tonic-gate 			setok = YPSETLOCAL;
1177c478bd9Sstevel@tonic-gate 		} else if (strcmp(*argv, "-broadcast") == 0) {
1187c478bd9Sstevel@tonic-gate 			broadcast = TRUE;
1197c478bd9Sstevel@tonic-gate 		} else {
1207c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1217c478bd9Sstevel@tonic-gate 		"usage: ypbind [-broadcast] [-ypset] [-ypsetme]\n");
1227c478bd9Sstevel@tonic-gate 			exit(1);
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 		argc--,
1257c478bd9Sstevel@tonic-gate 		argv++;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (setok == YPSETALL) {
1297c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1307c478bd9Sstevel@tonic-gate 	"ypbind -ypset: allowing ypset! (this is REALLY insecure)\n");
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	if (setok == YPSETLOCAL) {
1337c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1347c478bd9Sstevel@tonic-gate 	"ypbind -ypsetme: allowing local ypset! (this is insecure)\n");
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	if (broadcast == TRUE) {
1377c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1387c478bd9Sstevel@tonic-gate 			"ypbind -broadcast: allowing broadcast! \
1397c478bd9Sstevel@tonic-gate (insecure and transport dependent)\n");
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (getdomainname(domain, sizeof (domain)) == 0) {
1437c478bd9Sstevel@tonic-gate 		sprintf(servers, "%s/%s/ypservers", BINDING, domain);
1447c478bd9Sstevel@tonic-gate 		if (!broadcast && access(servers, R_OK) != 0) {
1457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1467c478bd9Sstevel@tonic-gate 		"%s: no info on servers - run ypinit -c\n", Argv[0]);
1477c478bd9Sstevel@tonic-gate 			exit(1);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 	} else {
1507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: domainname not set - exiting\n",
1517c478bd9Sstevel@tonic-gate 			Argv[0]);
1527c478bd9Sstevel@tonic-gate 		exit(1);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	getrlimit(RLIMIT_NOFILE, &rl);
1567c478bd9Sstevel@tonic-gate 	rl.rlim_cur = rl.rlim_max;
1577c478bd9Sstevel@tonic-gate 	setrlimit(RLIMIT_NOFILE, &rl);
1587c478bd9Sstevel@tonic-gate 
159*004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
160*004388ebScasper 
1617c478bd9Sstevel@tonic-gate 	openlog("ypbind", LOG_PID, LOG_DAEMON);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * If stdin looks like a TLI endpoint, we assume
1657c478bd9Sstevel@tonic-gate 	 * that we were started by a port monitor. If
1667c478bd9Sstevel@tonic-gate 	 * t_getstate fails with TBADF, this is not a
1677c478bd9Sstevel@tonic-gate 	 * TLI endpoint.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	_rpcpmstart = (t_getstate(0) != -1 || t_errno != TBADF);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (!__rpcbind_is_up()) {
1727c478bd9Sstevel@tonic-gate 		msgout("terminating: rpcbind is not running");
1737c478bd9Sstevel@tonic-gate 		exit(1);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (_rpcpmstart) {
1777c478bd9Sstevel@tonic-gate 		/*
1787c478bd9Sstevel@tonic-gate 		 * We were invoked by ypbind with the request on stdin.
1797c478bd9Sstevel@tonic-gate 		 *
1807c478bd9Sstevel@tonic-gate 		 * XXX - This is not the normal way ypbind is used
1817c478bd9Sstevel@tonic-gate 		 * and has never been tested.
1827c478bd9Sstevel@tonic-gate 		 */
1837c478bd9Sstevel@tonic-gate 		char *netid;
1847c478bd9Sstevel@tonic-gate 		struct netconfig *nconf = NULL;
1857c478bd9Sstevel@tonic-gate 		SVCXPRT *transp;
1867c478bd9Sstevel@tonic-gate 		int pmclose;
1877c478bd9Sstevel@tonic-gate 		extern char *getenv();
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		/*
1907c478bd9Sstevel@tonic-gate 		 * Set non-blocking mode and maximum record size for
1917c478bd9Sstevel@tonic-gate 		 * connection oriented RPC transports.
1927c478bd9Sstevel@tonic-gate 		 */
1937c478bd9Sstevel@tonic-gate 		if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
1947c478bd9Sstevel@tonic-gate 			msgout("unable to set maximum RPC record size");
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		clear_bindings();
1987c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
1997c478bd9Sstevel@tonic-gate #ifdef DEBUG
2007c478bd9Sstevel@tonic-gate 			msgout("cannot get transport name");
2017c478bd9Sstevel@tonic-gate #endif
2027c478bd9Sstevel@tonic-gate 		} else if ((nconf = getnetconfigent(netid)) == NULL) {
2037c478bd9Sstevel@tonic-gate #ifdef DEBUG
2047c478bd9Sstevel@tonic-gate 			msgout("cannot get transport info");
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		pmclose = (t_getstate(0) != T_DATAXFER);
2097c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
2107c478bd9Sstevel@tonic-gate 			msgout("cannot create server handle");
2117c478bd9Sstevel@tonic-gate 			exit(1);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
2157c478bd9Sstevel@tonic-gate 			if ((setok != YPSETNONE) &&
2167c478bd9Sstevel@tonic-gate 				__rpc_negotiate_uid(transp->xp_fd)) {
2177c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
2187c478bd9Sstevel@tonic-gate 				"could not negotiate with loopback tranport %s",
2197c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 		if (nconf)
2237c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
2247c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, YPBINDPROG, YPBINDVERS, ypbindprog_3, 0)) {
2257c478bd9Sstevel@tonic-gate 			msgout("unable to register (YPBINDPROG, YPBINDVERS).");
2267c478bd9Sstevel@tonic-gate 			exit(1);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_2,
2297c478bd9Sstevel@tonic-gate 		    ypbindprog_2, 0)) {
2307c478bd9Sstevel@tonic-gate 			msgout(
2317c478bd9Sstevel@tonic-gate 			    "unable to register (YPBINDPROG, YPBINDVERS_2).");
2327c478bd9Sstevel@tonic-gate 			exit(1);
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		/* version 2 and version 1 are the same as far as we care */
2357c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_1,
2367c478bd9Sstevel@tonic-gate 		    ypbindprog_2, 0)) {
2377c478bd9Sstevel@tonic-gate 			msgout(
2387c478bd9Sstevel@tonic-gate 			    "unable to register (YPBINDPROG, YPBINDVERS_1).");
2397c478bd9Sstevel@tonic-gate 			exit(1);
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 		set_signal_handlers();
2427c478bd9Sstevel@tonic-gate 		if (pmclose) {
2437c478bd9Sstevel@tonic-gate 			(void) signal(SIGALRM, closedown);
2447c478bd9Sstevel@tonic-gate 			(void) alarm(_RPCSVC_CLOSEDOWN);
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate #ifdef INIT_DEFAULT
2477c478bd9Sstevel@tonic-gate 	ypbind_init_default();
2487c478bd9Sstevel@tonic-gate #endif
2497c478bd9Sstevel@tonic-gate 		svc_run();
2507c478bd9Sstevel@tonic-gate 		msgout("svc_run returned");
2517c478bd9Sstevel@tonic-gate 		exit(1);
2527c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate #ifndef RPC_SVC_FG
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 *  In normal operation, ypbind forks a child to do all the work
2577c478bd9Sstevel@tonic-gate 	 *  so that it can run in background.  But, if the parent exits
2587c478bd9Sstevel@tonic-gate 	 *  too soon during system startup, clients will start trying to
2597c478bd9Sstevel@tonic-gate 	 *  talk to the child ypbind before it is ready.  This can cause
2607c478bd9Sstevel@tonic-gate 	 *  spurious client errors.
2617c478bd9Sstevel@tonic-gate 	 *
2627c478bd9Sstevel@tonic-gate 	 *  To prevent these problems, the parent process creates a pipe,
2637c478bd9Sstevel@tonic-gate 	 *  which is inherited by the child, and waits for the child to
2647c478bd9Sstevel@tonic-gate 	 *  close its end.  This happens explicitly before the child goes
2657c478bd9Sstevel@tonic-gate 	 *  into svc_run(), or as a side-effect of exiting.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	if (pipe(pfd) == -1) {
2687c478bd9Sstevel@tonic-gate 		perror("pipe");
2697c478bd9Sstevel@tonic-gate 		exit(1);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	pid = fork();
2727c478bd9Sstevel@tonic-gate 	if (pid < 0) {
2737c478bd9Sstevel@tonic-gate 		perror("cannot fork");
2747c478bd9Sstevel@tonic-gate 		exit(1);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	if (pid) {
2777c478bd9Sstevel@tonic-gate 		/*
2787c478bd9Sstevel@tonic-gate 		 *  The parent waits for the child to close its end of
2797c478bd9Sstevel@tonic-gate 		 *  the pipe (to indicate that it is ready to process
2807c478bd9Sstevel@tonic-gate 		 *  requests).  The read blocks until the child does
2817c478bd9Sstevel@tonic-gate 		 *  a close (the "domain" array is just a handy buffer).
2827c478bd9Sstevel@tonic-gate 		 */
2837c478bd9Sstevel@tonic-gate 		close(pfd[1]);
2847c478bd9Sstevel@tonic-gate 		read(pfd[0], domain, sizeof (domain));
2857c478bd9Sstevel@tonic-gate 		exit(0);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 	/* close all files except pfd[1] */
2887c478bd9Sstevel@tonic-gate 	(void) fdwalk(void_close, &pfd[1]);
2897c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
2907c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_WRONLY);
2917c478bd9Sstevel@tonic-gate 	(void) dup(1);
2927c478bd9Sstevel@tonic-gate 	setsid();
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate 	clean_cache();    /* make sure there are no left-over files */
2957c478bd9Sstevel@tonic-gate 	cache_okay = cache_check();
2967c478bd9Sstevel@tonic-gate 	cache_pid();
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/*
2997c478bd9Sstevel@tonic-gate 	 * Set non-blocking mode and maximum record size for
3007c478bd9Sstevel@tonic-gate 	 * connection oriented RPC transports.
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
3037c478bd9Sstevel@tonic-gate 		msgout("unable to set maximum RPC record size");
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Prevent our non-priv udp and tcp ports bound w/wildcard addr
3087c478bd9Sstevel@tonic-gate 	 * from being hijacked by a bind to a more specific addr.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	if (!rpc_control(__RPC_SVC_EXCLBIND_SET, &exclbind)) {
3117c478bd9Sstevel@tonic-gate 		msgout("warning: unable to set udp/tcp EXCLBIND");
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate #ifdef INIT_DEFAULT
3157c478bd9Sstevel@tonic-gate 	ypbind_init_default();
3167c478bd9Sstevel@tonic-gate #endif
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	nc_handle = __rpc_setconf("netpath");	/* open netconfig file */
3197c478bd9Sstevel@tonic-gate 	if (nc_handle == NULL) {
3207c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not read /etc/netconfig, exiting..");
3217c478bd9Sstevel@tonic-gate 		exit(1);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 *  The parent waits for the child to close its end of
3267c478bd9Sstevel@tonic-gate 	 *  the pipe (to indicate that it is ready to process
3277c478bd9Sstevel@tonic-gate 	 *  requests). Now the non-diskless client will wait because the
3287c478bd9Sstevel@tonic-gate 	 *  cache file is valid.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	if (cache_okay) {
3317c478bd9Sstevel@tonic-gate 		close(pfd[1]);
3327c478bd9Sstevel@tonic-gate 		pipe_closed = 1;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	clear_bindings();
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	while (nconf = __rpc_getconf(nc_handle)) {
3387c478bd9Sstevel@tonic-gate 		SVCXPRT *xprt;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		if (!__rpcbind_is_up()) {
3417c478bd9Sstevel@tonic-gate 			msgout("terminating: rpcbind is not running");
3427c478bd9Sstevel@tonic-gate 			exit(1);
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 		if ((xprt = svc_tp_create(ypbindprog_3,
3457c478bd9Sstevel@tonic-gate 			YPBINDPROG, YPBINDVERS, nconf)) == NULL) {
3467c478bd9Sstevel@tonic-gate 			msgout("terminating: cannot create rpcbind handle");
3477c478bd9Sstevel@tonic-gate 			exit(1);
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		cache_transport(nconf, xprt, YPBINDVERS);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		/* support ypbind V2 and V1, but only on udp/tcp transports */
3537c478bd9Sstevel@tonic-gate 		if (((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
3547c478bd9Sstevel@tonic-gate 			(strcmp(nconf->nc_protofmly, NC_INET6))) &&
3557c478bd9Sstevel@tonic-gate 			((nconf->nc_semantics == NC_TPI_CLTS) ||
3567c478bd9Sstevel@tonic-gate 				(nconf->nc_semantics == NC_TPI_COTS_ORD))) {
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET)) {
3597c478bd9Sstevel@tonic-gate 				inet_desired_tpts |= 1 >> nconf->nc_semantics;
3607c478bd9Sstevel@tonic-gate 			} else {
3617c478bd9Sstevel@tonic-gate 				inet6_desired_tpts |= 1 >> nconf->nc_semantics;
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			(void) rpcb_unset(YPBINDPROG, YPBINDVERS_2, nconf);
3657c478bd9Sstevel@tonic-gate 			if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_2,
3667c478bd9Sstevel@tonic-gate 			    ypbindprog_2, nconf)) {
3677c478bd9Sstevel@tonic-gate 				syslog(LOG_INFO,
3687c478bd9Sstevel@tonic-gate 		    "unable to register (YPBINDPROG, YPBINDVERS_2) [%s]",
3697c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
3707c478bd9Sstevel@tonic-gate 				continue;
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			cache_transport(nconf, xprt, YPBINDVERS_2);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 			/* For NC_INET, register v1 as well; error is fatal */
3767c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
3777c478bd9Sstevel@tonic-gate 				(void) rpcb_unset(YPBINDPROG, YPBINDVERS_1,
3787c478bd9Sstevel@tonic-gate 						nconf);
3797c478bd9Sstevel@tonic-gate 				if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_1,
3807c478bd9Sstevel@tonic-gate 						ypbindprog_2, nconf)) {
3817c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
3827c478bd9Sstevel@tonic-gate 			    "unable to register (YPBINDPROG, YPBINDVERS_1).");
3837c478bd9Sstevel@tonic-gate 					exit(1);
3847c478bd9Sstevel@tonic-gate 				}
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 			cache_transport(nconf, xprt, YPBINDVERS_1);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 			if (nconf->nc_semantics == NC_TPI_CLTS)
3907c478bd9Sstevel@tonic-gate 				udp_found++;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET)) {
3937c478bd9Sstevel@tonic-gate 				inet_tpts |= 1 >> nconf->nc_semantics;
3947c478bd9Sstevel@tonic-gate 			} else {
3957c478bd9Sstevel@tonic-gate 				inet6_tpts |= 1 >> nconf->nc_semantics;
3967c478bd9Sstevel@tonic-gate 			}
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
3997c478bd9Sstevel@tonic-gate 			loopback_found++;
4007c478bd9Sstevel@tonic-gate 			if ((setok != YPSETNONE) &&
4017c478bd9Sstevel@tonic-gate 				__rpc_negotiate_uid(xprt->xp_fd)) {
4027c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
4037c478bd9Sstevel@tonic-gate 				"could not negotiate with loopback tranport %s",
4047c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
4057c478bd9Sstevel@tonic-gate 			}
4067c478bd9Sstevel@tonic-gate 			/*
4077c478bd9Sstevel@tonic-gate 			 *  On a diskless client:
4087c478bd9Sstevel@tonic-gate 			 *  The parent waits for the child to close its end of
4097c478bd9Sstevel@tonic-gate 			 *  the pipe (to indicate that it is ready to process
4107c478bd9Sstevel@tonic-gate 			 *  requests). Now the diskless client will wait
4117c478bd9Sstevel@tonic-gate 			 *  only if ypbind is registered on the loopback.
4127c478bd9Sstevel@tonic-gate 			 */
4137c478bd9Sstevel@tonic-gate 			if ((!pipe_closed) &&
4147c478bd9Sstevel@tonic-gate 				((nconf->nc_semantics == NC_TPI_COTS) ||
4157c478bd9Sstevel@tonic-gate 				(nconf->nc_semantics == NC_TPI_COTS_ORD))) {
4167c478bd9Sstevel@tonic-gate 				close(pfd[1]);
4177c478bd9Sstevel@tonic-gate 				pipe_closed = 1;
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/* Did we manage to register all IPv4 or all IPv6 transports ? */
4237c478bd9Sstevel@tonic-gate 	if (inet_tpts != 0 && inet_tpts != inet_desired_tpts) {
4247c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4257c478bd9Sstevel@tonic-gate 			"unable to register all %s transports, exiting..",
4267c478bd9Sstevel@tonic-gate 			NC_INET);
4277c478bd9Sstevel@tonic-gate 		exit(1);
4287c478bd9Sstevel@tonic-gate 	} else if (inet6_tpts != 0 && inet6_tpts != inet6_desired_tpts) {
4297c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4307c478bd9Sstevel@tonic-gate 			"unable to register all %s transports, exiting..",
4317c478bd9Sstevel@tonic-gate 			NC_INET6);
4327c478bd9Sstevel@tonic-gate 		exit(1);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if (!pipe_closed) {
4367c478bd9Sstevel@tonic-gate 		close(pfd[1]);
4377c478bd9Sstevel@tonic-gate 		pipe_closed = 1;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	__rpc_endconf(nc_handle);
4407c478bd9Sstevel@tonic-gate 	if (!loopback_found) {
4417c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4427c478bd9Sstevel@tonic-gate 			"could not find loopback transports, exiting..");
4437c478bd9Sstevel@tonic-gate 		exit(1);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	if (!udp_found) {
4467c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4477c478bd9Sstevel@tonic-gate 			"could not find inet-clts (udp) transport, exiting..");
4487c478bd9Sstevel@tonic-gate 		exit(1);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	set_signal_handlers();
4517c478bd9Sstevel@tonic-gate 	svc_run();
4527c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "svc_run returned, exiting..");
4537c478bd9Sstevel@tonic-gate 	exit(1);
4547c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Callback function for fdwalk() to close all files.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate static int
void_close(void * pfdp,int fd)4617c478bd9Sstevel@tonic-gate void_close(void *pfdp, int fd)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	if (fd != *(int *)pfdp)
4647c478bd9Sstevel@tonic-gate 		(void) close(fd);
4657c478bd9Sstevel@tonic-gate 	return (0);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate void
ypbindprog_3(rqstp,transp)4697c478bd9Sstevel@tonic-gate ypbindprog_3(rqstp, transp)
4707c478bd9Sstevel@tonic-gate 	struct svc_req *rqstp;
4717c478bd9Sstevel@tonic-gate 	register SVCXPRT *transp;
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	union {
4747c478bd9Sstevel@tonic-gate 		ypbind_domain ypbindproc_domain_3_arg;
4757c478bd9Sstevel@tonic-gate 		ypbind_setdom ypbindproc_setdom_3_arg;
4767c478bd9Sstevel@tonic-gate 	} argument;
4777c478bd9Sstevel@tonic-gate 	char *result;
4787c478bd9Sstevel@tonic-gate 	bool_t (*xdr_argument)(), (*xdr_result)();
4797c478bd9Sstevel@tonic-gate 	char *(*local)();
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (sigcld_event)
4827c478bd9Sstevel@tonic-gate 		broadcast_proc_exit();
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	_rpcsvcdirty = 1;
4857c478bd9Sstevel@tonic-gate 	switch (rqstp->rq_proc) {
4867c478bd9Sstevel@tonic-gate 	case YPBINDPROC_NULL:
4877c478bd9Sstevel@tonic-gate 		xdr_argument = xdr_void;
4887c478bd9Sstevel@tonic-gate 		xdr_result = xdr_void;
4897c478bd9Sstevel@tonic-gate 		local = (char *(*)()) ypbindproc_null_3;
4907c478bd9Sstevel@tonic-gate 		break;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	case YPBINDPROC_DOMAIN:
4937c478bd9Sstevel@tonic-gate 		xdr_argument = xdr_ypbind_domain;
4947c478bd9Sstevel@tonic-gate 		xdr_result = xdr_ypbind_resp;
4957c478bd9Sstevel@tonic-gate 		local = (char *(*)()) ypbindproc_domain_3;
4967c478bd9Sstevel@tonic-gate 		break;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	case YPBINDPROC_SETDOM:
4997c478bd9Sstevel@tonic-gate 		xdr_argument = xdr_ypbind_setdom;
5007c478bd9Sstevel@tonic-gate 		xdr_result = xdr_void;
5017c478bd9Sstevel@tonic-gate 		local = (char *(*)()) ypbindproc_setdom_3;
5027c478bd9Sstevel@tonic-gate 		break;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	default:
5057c478bd9Sstevel@tonic-gate 		svcerr_noproc(transp);
5067c478bd9Sstevel@tonic-gate 		_rpcsvcdirty = 0;
5077c478bd9Sstevel@tonic-gate 		return;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	(void) memset((char *)&argument, 0, sizeof (argument));
5107c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5117c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
5127c478bd9Sstevel@tonic-gate 		_rpcsvcdirty = 0;
5137c478bd9Sstevel@tonic-gate 		return;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	if (rqstp->rq_proc == YPBINDPROC_SETDOM)
5167c478bd9Sstevel@tonic-gate 		result = (*local)(&argument, rqstp, transp);
5177c478bd9Sstevel@tonic-gate 	else
5187c478bd9Sstevel@tonic-gate 		result = (*local)(&argument, rqstp);
5197c478bd9Sstevel@tonic-gate 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
5207c478bd9Sstevel@tonic-gate 		svcerr_systemerr(transp);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5237c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "unable to free arguments");
5247c478bd9Sstevel@tonic-gate 		exit(1);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	_rpcsvcdirty = 0;
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate void
ypbindprog_2(rqstp,transp)5307c478bd9Sstevel@tonic-gate ypbindprog_2(rqstp, transp)
5317c478bd9Sstevel@tonic-gate 	struct svc_req *rqstp;
5327c478bd9Sstevel@tonic-gate 	register SVCXPRT *transp;
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	union {
5357c478bd9Sstevel@tonic-gate 		domainname_2 ypbindproc_domain_2_arg;
5367c478bd9Sstevel@tonic-gate 		ypbind_setdom_2 ypbindproc_setdom_2_arg;
5377c478bd9Sstevel@tonic-gate 	} argument;
5387c478bd9Sstevel@tonic-gate 	char *result;
5397c478bd9Sstevel@tonic-gate 	bool_t (*xdr_argument)(), (*xdr_result)();
5407c478bd9Sstevel@tonic-gate 	char *(*local)();
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if (sigcld_event)
5437c478bd9Sstevel@tonic-gate 		broadcast_proc_exit();
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	_rpcsvcdirty = 1;
5467c478bd9Sstevel@tonic-gate 	switch (rqstp->rq_proc) {
5477c478bd9Sstevel@tonic-gate 	case YPBINDPROC_NULL:
5487c478bd9Sstevel@tonic-gate 		xdr_argument = xdr_void;
5497c478bd9Sstevel@tonic-gate 		xdr_result = xdr_void;
5507c478bd9Sstevel@tonic-gate 		/* XXX - don't need two null procedures */
5517c478bd9Sstevel@tonic-gate 		local = (char *(*)()) ypbindproc_null_3;
5527c478bd9Sstevel@tonic-gate 		break;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	case YPBINDPROC_DOMAIN:
5557c478bd9Sstevel@tonic-gate 		xdr_argument = (bool_t (*)())xdr_ypdomain_wrap_string;
5567c478bd9Sstevel@tonic-gate 		xdr_result = xdr_ypbind_resp_2;
5577c478bd9Sstevel@tonic-gate 		local = (char *(*)()) ypbindproc_domain_2;
5587c478bd9Sstevel@tonic-gate 		break;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	case YPBINDPROC_SETDOM:	/* not supported, fall through to error */
5617c478bd9Sstevel@tonic-gate 	default:
5627c478bd9Sstevel@tonic-gate 		svcerr_noproc(transp);
5637c478bd9Sstevel@tonic-gate 		_rpcsvcdirty = 0;
5647c478bd9Sstevel@tonic-gate 		return;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	(void) memset((char *)&argument, 0, sizeof (argument));
5677c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5687c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
5697c478bd9Sstevel@tonic-gate 		_rpcsvcdirty = 0;
5707c478bd9Sstevel@tonic-gate 		return;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	result = (*local)(&argument, rqstp);
5737c478bd9Sstevel@tonic-gate 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
5747c478bd9Sstevel@tonic-gate 		svcerr_systemerr(transp);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5777c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "unable to free arguments");
5787c478bd9Sstevel@tonic-gate 		exit(1);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 	_rpcsvcdirty = 0;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  *  We clear out any old bindings that might have been
5857c478bd9Sstevel@tonic-gate  *  left behind.  If there is already a ypbind running,
5867c478bd9Sstevel@tonic-gate  *  it will no longer get requests.  We are in control
5877c478bd9Sstevel@tonic-gate  *  now.  We ignore the error from rpcb_unset() because
5887c478bd9Sstevel@tonic-gate  *  this is just a "best effort".  If the rpcb_unset()
5897c478bd9Sstevel@tonic-gate  *  does fail, we will get an error in svc_reg().  By
5907c478bd9Sstevel@tonic-gate  *  using 0 for the last argument we are telling the
5917c478bd9Sstevel@tonic-gate  *  portmapper to remove the bindings for all transports.
5927c478bd9Sstevel@tonic-gate  */
5937c478bd9Sstevel@tonic-gate static
5947c478bd9Sstevel@tonic-gate void
clear_bindings()5957c478bd9Sstevel@tonic-gate clear_bindings()
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	rpcb_unset(YPBINDPROG, YPBINDVERS, 0);
5987c478bd9Sstevel@tonic-gate 	rpcb_unset(YPBINDPROG, YPBINDVERS_2, 0);
5997c478bd9Sstevel@tonic-gate 	rpcb_unset(YPBINDPROG, YPBINDVERS_1, 0);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  *  This routine is called when we are killed (by most signals).
6047c478bd9Sstevel@tonic-gate  *  It first tries to unregister with the portmapper.  Then it
6057c478bd9Sstevel@tonic-gate  *  resets the signal handler to the default so that if we get
6067c478bd9Sstevel@tonic-gate  *  the same signal, we will just go away.  We clean up our
6077c478bd9Sstevel@tonic-gate  *  children by doing a hold in SIGTERM and then killing the
6087c478bd9Sstevel@tonic-gate  *  process group (-getpid()) with SIGTERM.  Finally, we redeliver
6097c478bd9Sstevel@tonic-gate  *  the signal to ourselves (the handler was reset to the default)
6107c478bd9Sstevel@tonic-gate  *  so that we will do the normal handling (e.g., coredump).
6117c478bd9Sstevel@tonic-gate  *  If we can't kill ourselves, we get drastic and just exit
6127c478bd9Sstevel@tonic-gate  *  after sleeping for a couple of seconds.
6137c478bd9Sstevel@tonic-gate  *
6147c478bd9Sstevel@tonic-gate  *  This code was taken from the SunOS version of ypbind.
6157c478bd9Sstevel@tonic-gate  */
6167c478bd9Sstevel@tonic-gate static
6177c478bd9Sstevel@tonic-gate void
unregister(int code)6187c478bd9Sstevel@tonic-gate unregister(int code)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	clear_bindings();
6217c478bd9Sstevel@tonic-gate 	clean_cache();
6227c478bd9Sstevel@tonic-gate 	signal(code, SIG_DFL);    /* to prevent recursive calls to unregister */
6237c478bd9Sstevel@tonic-gate 	fprintf(stderr, "ypbind: goind down on signal %d\n", code);
6247c478bd9Sstevel@tonic-gate 	sighold(SIGCHLD);
6257c478bd9Sstevel@tonic-gate 	sighold(SIGTERM);
6267c478bd9Sstevel@tonic-gate 	kill(-getpid(), SIGTERM); /* kill process group (i.e., children) */
6277c478bd9Sstevel@tonic-gate 	sigrelse(SIGTERM);
6287c478bd9Sstevel@tonic-gate 	kill(getpid(), code);	  /* throw signal again */
6297c478bd9Sstevel@tonic-gate 	sleep(2);
6307c478bd9Sstevel@tonic-gate 	exit(-1);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate static
6347c478bd9Sstevel@tonic-gate void
set_signal_handlers()6357c478bd9Sstevel@tonic-gate set_signal_handlers()
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	int i;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	for (i = 1; i <= SIGTERM; i++) {
6407c478bd9Sstevel@tonic-gate 		if (i == SIGCHLD)
6417c478bd9Sstevel@tonic-gate 			continue;
6427c478bd9Sstevel@tonic-gate 		else if (i == SIGHUP)
6437c478bd9Sstevel@tonic-gate 			signal(i, SIG_IGN);
6447c478bd9Sstevel@tonic-gate 		else
6457c478bd9Sstevel@tonic-gate 			signal(i, unregister);
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate void
msgout(msg)6507c478bd9Sstevel@tonic-gate msgout(msg)
6517c478bd9Sstevel@tonic-gate 	char *msg;
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate #ifdef RPC_SVC_FG
6547c478bd9Sstevel@tonic-gate 	if (_rpcpmstart)
6557c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, msg);
6567c478bd9Sstevel@tonic-gate 	else
6577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msg);
6587c478bd9Sstevel@tonic-gate #else
6597c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, msg);
6607c478bd9Sstevel@tonic-gate #endif
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate void
closedown()6647c478bd9Sstevel@tonic-gate closedown()
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	if (_rpcsvcdirty == 0) {
6677c478bd9Sstevel@tonic-gate 		int i, openfd;
6687c478bd9Sstevel@tonic-gate 		struct t_info tinfo;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		if (t_getinfo(0, &tinfo) || (tinfo.servtype == T_CLTS))
6717c478bd9Sstevel@tonic-gate 			exit(0);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		for (i = 0, openfd = 0; i < svc_max_pollfd && openfd < 2; i++)
6747c478bd9Sstevel@tonic-gate 			if (svc_pollfd[i].fd >= 0)
6757c478bd9Sstevel@tonic-gate 				openfd++;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		if (openfd <= 1)
6787c478bd9Sstevel@tonic-gate 			exit(0);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 	(void) alarm(_RPCSVC_CLOSEDOWN);
6817c478bd9Sstevel@tonic-gate }
682