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