xref: /illumos-gate/usr/src/cmd/ypcmd/ypxfrd_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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*a506a34cSth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This source was formally rpcgen generated, but has been
307c478bd9Sstevel@tonic-gate  * checked in.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "ypxfrd.h"
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h> /* for pmap_unset */
387c478bd9Sstevel@tonic-gate #include <string.h> /* strcmp */
397c478bd9Sstevel@tonic-gate #include <unistd.h> /* setsid */
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <memory.h>
427c478bd9Sstevel@tonic-gate #include <stropts.h>
437c478bd9Sstevel@tonic-gate #include <netconfig.h>
447c478bd9Sstevel@tonic-gate #include <sys/resource.h> /* rlimit */
457c478bd9Sstevel@tonic-gate #include <syslog.h>
467c478bd9Sstevel@tonic-gate #include <ndbm.h>
477c478bd9Sstevel@tonic-gate #include "shim.h"
487c478bd9Sstevel@tonic-gate #include "yptol.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifndef SIG_PF
517c478bd9Sstevel@tonic-gate #define	SIG_PF void(*)(int)
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef DEBUG
557c478bd9Sstevel@tonic-gate #define	RPC_SVC_FG
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	_RPCSVC_CLOSEDOWN 120
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Copyr 1989 Sun Micro
627c478bd9Sstevel@tonic-gate  * #ident	"@(#)ypxfrd.x	1.2	00/05/01 SMI"
637c478bd9Sstevel@tonic-gate  * This is NOT source code!
647c478bd9Sstevel@tonic-gate  * DO NOT EDIT THIS FILE!
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static int _rpcpmstart;		/* Started by a port monitor ? */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* States a server can be in wrt request */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	_IDLE 0
717c478bd9Sstevel@tonic-gate #define	_SERVED 1
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static int _rpcsvcstate = _IDLE;	/* Set when a request is serviced */
747c478bd9Sstevel@tonic-gate static int _rpcsvccount = 0;		/* Number of requests being serviced */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static void
_msgout(char * msg)777c478bd9Sstevel@tonic-gate _msgout(char *msg)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate #ifdef RPC_SVC_FG
807c478bd9Sstevel@tonic-gate 	if (_rpcpmstart)
817c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s", msg);
827c478bd9Sstevel@tonic-gate 	else
837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msg);
847c478bd9Sstevel@tonic-gate #else
857c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "%s", msg);
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static void
closedown(int sig)907c478bd9Sstevel@tonic-gate closedown(int sig)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	if (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {
937c478bd9Sstevel@tonic-gate 		int size;
947c478bd9Sstevel@tonic-gate 		int i, openfd = 0;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		size = svc_max_pollfd;
977c478bd9Sstevel@tonic-gate 		for (i = 0; i < size && openfd < 2; i++)
987c478bd9Sstevel@tonic-gate 			if (svc_pollfd[i].fd >= 0)
997c478bd9Sstevel@tonic-gate 				openfd++;
1007c478bd9Sstevel@tonic-gate 		if (openfd <= 1)
1017c478bd9Sstevel@tonic-gate 			exit(0);
1027c478bd9Sstevel@tonic-gate 	} else
1037c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _IDLE;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, (SIG_PF) closedown);
1067c478bd9Sstevel@tonic-gate 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static void
ypxfrd_1(struct svc_req * rqstp,register SVCXPRT * transp)1107c478bd9Sstevel@tonic-gate ypxfrd_1(struct svc_req *rqstp, register SVCXPRT *transp)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	union {
1137c478bd9Sstevel@tonic-gate 		hosereq getdbm_1_arg;
1147c478bd9Sstevel@tonic-gate 	} argument;
1157c478bd9Sstevel@tonic-gate 	char *result;
1167c478bd9Sstevel@tonic-gate 	xdrproc_t _xdr_argument, _xdr_result;
1177c478bd9Sstevel@tonic-gate 	char *(*local)(char *, struct svc_req *);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	_rpcsvccount++;
1207c478bd9Sstevel@tonic-gate 	switch (rqstp->rq_proc) {
1217c478bd9Sstevel@tonic-gate 	case NULLPROC:
1227c478bd9Sstevel@tonic-gate 		(void) svc_sendreply(transp,
1237c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_void, (char *)NULL);
1247c478bd9Sstevel@tonic-gate 		_rpcsvccount--;
1257c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _SERVED;
1267c478bd9Sstevel@tonic-gate 		return;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	case getdbm:
1297c478bd9Sstevel@tonic-gate 		_xdr_argument = (xdrproc_t)xdr_hosereq;
1307c478bd9Sstevel@tonic-gate 		_xdr_result = (xdrproc_t)xdr_dbmfyl;
1317c478bd9Sstevel@tonic-gate 		local = (char *(*)(char *, struct svc_req *)) getdbm_1_svc;
1327c478bd9Sstevel@tonic-gate 		break;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	default:
1357c478bd9Sstevel@tonic-gate 		svcerr_noproc(transp);
1367c478bd9Sstevel@tonic-gate 		_rpcsvccount--;
1377c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _SERVED;
1387c478bd9Sstevel@tonic-gate 		return;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	(void) memset((char *)&argument, 0, sizeof (argument));
1417c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, _xdr_argument, (caddr_t)&argument)) {
1427c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
1437c478bd9Sstevel@tonic-gate 		_rpcsvccount--;
1447c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _SERVED;
1457c478bd9Sstevel@tonic-gate 		return;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	result = (*local)((char *)&argument, rqstp);
1487c478bd9Sstevel@tonic-gate 	if (_xdr_result && result != NULL &&
1497c478bd9Sstevel@tonic-gate 		!svc_sendreply(transp, _xdr_result, result)) {
1507c478bd9Sstevel@tonic-gate 		svcerr_systemerr(transp);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	if (!svc_freeargs(transp, _xdr_argument, (caddr_t)&argument)) {
1537c478bd9Sstevel@tonic-gate 		_msgout("unable to free arguments");
1547c478bd9Sstevel@tonic-gate 		exit(1);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	_rpcsvccount--;
1577c478bd9Sstevel@tonic-gate 	_rpcsvcstate = _SERVED;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
160*a506a34cSth int
main()1617c478bd9Sstevel@tonic-gate main()
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	pid_t pid;
1647c478bd9Sstevel@tonic-gate 	int i;
1657c478bd9Sstevel@tonic-gate 	int stat;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * If stdin looks like a TLI endpoint, we assume
1717c478bd9Sstevel@tonic-gate 	 * that we were started by a port monitor. If
1727c478bd9Sstevel@tonic-gate 	 * t_getstate fails with TBADF, this is not a
1737c478bd9Sstevel@tonic-gate 	 * TLI endpoint.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if (t_getstate(0) != -1 || t_errno != TBADF) {
1767c478bd9Sstevel@tonic-gate 		char *netid;
1777c478bd9Sstevel@tonic-gate 		struct netconfig *nconf = NULL;
1787c478bd9Sstevel@tonic-gate 		SVCXPRT *transp;
1797c478bd9Sstevel@tonic-gate 		int pmclose;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		_rpcpmstart = 1;
1827c478bd9Sstevel@tonic-gate 		openlog("ypxfrd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
1857c478bd9Sstevel@tonic-gate 		/* started from inetd */
1867c478bd9Sstevel@tonic-gate 			pmclose = 1;
1877c478bd9Sstevel@tonic-gate 		} else {
1887c478bd9Sstevel@tonic-gate 			if ((nconf = getnetconfigent(netid)) == NULL)
1897c478bd9Sstevel@tonic-gate 				_msgout("cannot get transport info");
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 			pmclose = (t_getstate(0) != T_DATAXFER);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
1947c478bd9Sstevel@tonic-gate 			_msgout("cannot create server handle");
1957c478bd9Sstevel@tonic-gate 			exit(1);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 		if (nconf)
1987c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
1997c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, YPXFRD, V1, ypxfrd_1, 0)) {
2007c478bd9Sstevel@tonic-gate 			_msgout("unable to register (YPXFRD, V1).");
2017c478bd9Sstevel@tonic-gate 			exit(1);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		if (pmclose) {
2047c478bd9Sstevel@tonic-gate 			(void) signal(SIGALRM, (SIG_PF) closedown);
2057c478bd9Sstevel@tonic-gate 			(void) alarm(_RPCSVC_CLOSEDOWN/2);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if (yptol_mode) {
2097c478bd9Sstevel@tonic-gate 			stat = parseConfig(NULL, NTOL_MAP_FILE);
2107c478bd9Sstevel@tonic-gate 			if (stat == 1) {
2117c478bd9Sstevel@tonic-gate 				_msgout("NIS to LDAP mapping inactive.");
2127c478bd9Sstevel@tonic-gate 			} else if (stat != 0) {
2137c478bd9Sstevel@tonic-gate 				_msgout("Aborting after NIS to LDAP "
2147c478bd9Sstevel@tonic-gate 					"mapping error.");
2157c478bd9Sstevel@tonic-gate 				exit(1);
2167c478bd9Sstevel@tonic-gate 			}
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		svc_run();
2207c478bd9Sstevel@tonic-gate 		exit(1);
2217c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2227c478bd9Sstevel@tonic-gate 	}	else {
2237c478bd9Sstevel@tonic-gate #ifndef RPC_SVC_FG
2247c478bd9Sstevel@tonic-gate #pragma weak closefrom
2257c478bd9Sstevel@tonic-gate 		extern void closefrom();
2267c478bd9Sstevel@tonic-gate 		int size;
2277c478bd9Sstevel@tonic-gate 		struct rlimit rl;
2287c478bd9Sstevel@tonic-gate 		pid = fork();
2297c478bd9Sstevel@tonic-gate 		if (pid < 0) {
2307c478bd9Sstevel@tonic-gate 			perror("cannot fork");
2317c478bd9Sstevel@tonic-gate 			exit(1);
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 		if (pid)
2347c478bd9Sstevel@tonic-gate 			exit(0);
2357c478bd9Sstevel@tonic-gate 		closelog();
2367c478bd9Sstevel@tonic-gate 		if (closefrom != NULL)
2377c478bd9Sstevel@tonic-gate 			closefrom(0);
2387c478bd9Sstevel@tonic-gate 		else {
2397c478bd9Sstevel@tonic-gate 			rl.rlim_max = 0;
2407c478bd9Sstevel@tonic-gate 			getrlimit(RLIMIT_NOFILE, &rl);
2417c478bd9Sstevel@tonic-gate 			if ((size = rl.rlim_max) == 0)
2427c478bd9Sstevel@tonic-gate 				exit(1);
2437c478bd9Sstevel@tonic-gate 			for (i = 0; i < size; i++)
2447c478bd9Sstevel@tonic-gate 				(void) close(i);
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 		i = open("/dev/null", 2);
2477c478bd9Sstevel@tonic-gate 		(void) dup2(i, 1);
2487c478bd9Sstevel@tonic-gate 		(void) dup2(i, 2);
2497c478bd9Sstevel@tonic-gate 		openlog("ypxfrd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
2507c478bd9Sstevel@tonic-gate 		setsid();
2517c478bd9Sstevel@tonic-gate #endif
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (yptol_mode) {
2557c478bd9Sstevel@tonic-gate 		stat = parseConfig(NULL, NTOL_MAP_FILE);
2567c478bd9Sstevel@tonic-gate 		if (stat == 1) {
2577c478bd9Sstevel@tonic-gate 			_msgout("NIS to LDAP mapping inactive.");
2587c478bd9Sstevel@tonic-gate 		} else if (stat != 0) {
2597c478bd9Sstevel@tonic-gate 			_msgout("Aborting after NIS to LDAP mapping error.");
2607c478bd9Sstevel@tonic-gate 			exit(1);
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (!svc_create(ypxfrd_1, YPXFRD, V1, "visible")) {
2657c478bd9Sstevel@tonic-gate 		_msgout("unable to create (YPXFRD, V1) for visible.");
2667c478bd9Sstevel@tonic-gate 		exit(1);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	svc_run();
2707c478bd9Sstevel@tonic-gate 	_msgout("svc_run returned");
2717c478bd9Sstevel@tonic-gate 	exit(1);
2727c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2737c478bd9Sstevel@tonic-gate }
274