xref: /titanic_44/usr/src/cmd/ypcmd/ypxfrd_svc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * This source was formally rpcgen generated, but has been
31*7c478bd9Sstevel@tonic-gate  * checked in.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "ypxfrd.h"
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */
37*7c478bd9Sstevel@tonic-gate #include <signal.h>
38*7c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h> /* for pmap_unset */
39*7c478bd9Sstevel@tonic-gate #include <string.h> /* strcmp */
40*7c478bd9Sstevel@tonic-gate #include <unistd.h> /* setsid */
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <memory.h>
43*7c478bd9Sstevel@tonic-gate #include <stropts.h>
44*7c478bd9Sstevel@tonic-gate #include <netconfig.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/resource.h> /* rlimit */
46*7c478bd9Sstevel@tonic-gate #include <syslog.h>
47*7c478bd9Sstevel@tonic-gate #include <ndbm.h>
48*7c478bd9Sstevel@tonic-gate #include "shim.h"
49*7c478bd9Sstevel@tonic-gate #include "yptol.h"
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #ifndef SIG_PF
52*7c478bd9Sstevel@tonic-gate #define	SIG_PF void(*)(int)
53*7c478bd9Sstevel@tonic-gate #endif
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
56*7c478bd9Sstevel@tonic-gate #define	RPC_SVC_FG
57*7c478bd9Sstevel@tonic-gate #endif
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #define	_RPCSVC_CLOSEDOWN 120
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * Copyr 1989 Sun Micro
63*7c478bd9Sstevel@tonic-gate  * #ident	"@(#)ypxfrd.x	1.2	00/05/01 SMI"
64*7c478bd9Sstevel@tonic-gate  * This is NOT source code!
65*7c478bd9Sstevel@tonic-gate  * DO NOT EDIT THIS FILE!
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate static int _rpcpmstart;		/* Started by a port monitor ? */
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate /* States a server can be in wrt request */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #define	_IDLE 0
72*7c478bd9Sstevel@tonic-gate #define	_SERVED 1
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static int _rpcsvcstate = _IDLE;	/* Set when a request is serviced */
75*7c478bd9Sstevel@tonic-gate static int _rpcsvccount = 0;		/* Number of requests being serviced */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static void
78*7c478bd9Sstevel@tonic-gate _msgout(char *msg)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate #ifdef RPC_SVC_FG
81*7c478bd9Sstevel@tonic-gate 	if (_rpcpmstart)
82*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s", msg);
83*7c478bd9Sstevel@tonic-gate 	else
84*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msg);
85*7c478bd9Sstevel@tonic-gate #else
86*7c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "%s", msg);
87*7c478bd9Sstevel@tonic-gate #endif
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate static void
91*7c478bd9Sstevel@tonic-gate closedown(int sig)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	if (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {
94*7c478bd9Sstevel@tonic-gate 		int size;
95*7c478bd9Sstevel@tonic-gate 		int i, openfd = 0;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 		size = svc_max_pollfd;
98*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < size && openfd < 2; i++)
99*7c478bd9Sstevel@tonic-gate 			if (svc_pollfd[i].fd >= 0)
100*7c478bd9Sstevel@tonic-gate 				openfd++;
101*7c478bd9Sstevel@tonic-gate 		if (openfd <= 1)
102*7c478bd9Sstevel@tonic-gate 			exit(0);
103*7c478bd9Sstevel@tonic-gate 	} else
104*7c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _IDLE;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, (SIG_PF) closedown);
107*7c478bd9Sstevel@tonic-gate 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static void
111*7c478bd9Sstevel@tonic-gate ypxfrd_1(struct svc_req *rqstp, register SVCXPRT *transp)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	union {
114*7c478bd9Sstevel@tonic-gate 		hosereq getdbm_1_arg;
115*7c478bd9Sstevel@tonic-gate 	} argument;
116*7c478bd9Sstevel@tonic-gate 	char *result;
117*7c478bd9Sstevel@tonic-gate 	xdrproc_t _xdr_argument, _xdr_result;
118*7c478bd9Sstevel@tonic-gate 	char *(*local)(char *, struct svc_req *);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	_rpcsvccount++;
121*7c478bd9Sstevel@tonic-gate 	switch (rqstp->rq_proc) {
122*7c478bd9Sstevel@tonic-gate 	case NULLPROC:
123*7c478bd9Sstevel@tonic-gate 		(void) svc_sendreply(transp,
124*7c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_void, (char *)NULL);
125*7c478bd9Sstevel@tonic-gate 		_rpcsvccount--;
126*7c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _SERVED;
127*7c478bd9Sstevel@tonic-gate 		return;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	case getdbm:
130*7c478bd9Sstevel@tonic-gate 		_xdr_argument = (xdrproc_t)xdr_hosereq;
131*7c478bd9Sstevel@tonic-gate 		_xdr_result = (xdrproc_t)xdr_dbmfyl;
132*7c478bd9Sstevel@tonic-gate 		local = (char *(*)(char *, struct svc_req *)) getdbm_1_svc;
133*7c478bd9Sstevel@tonic-gate 		break;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	default:
136*7c478bd9Sstevel@tonic-gate 		svcerr_noproc(transp);
137*7c478bd9Sstevel@tonic-gate 		_rpcsvccount--;
138*7c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _SERVED;
139*7c478bd9Sstevel@tonic-gate 		return;
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	(void) memset((char *)&argument, 0, sizeof (argument));
142*7c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, _xdr_argument, (caddr_t)&argument)) {
143*7c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
144*7c478bd9Sstevel@tonic-gate 		_rpcsvccount--;
145*7c478bd9Sstevel@tonic-gate 		_rpcsvcstate = _SERVED;
146*7c478bd9Sstevel@tonic-gate 		return;
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 	result = (*local)((char *)&argument, rqstp);
149*7c478bd9Sstevel@tonic-gate 	if (_xdr_result && result != NULL &&
150*7c478bd9Sstevel@tonic-gate 		!svc_sendreply(transp, _xdr_result, result)) {
151*7c478bd9Sstevel@tonic-gate 		svcerr_systemerr(transp);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 	if (!svc_freeargs(transp, _xdr_argument, (caddr_t)&argument)) {
154*7c478bd9Sstevel@tonic-gate 		_msgout("unable to free arguments");
155*7c478bd9Sstevel@tonic-gate 		exit(1);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 	_rpcsvccount--;
158*7c478bd9Sstevel@tonic-gate 	_rpcsvcstate = _SERVED;
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate main()
162*7c478bd9Sstevel@tonic-gate {
163*7c478bd9Sstevel@tonic-gate 	pid_t pid;
164*7c478bd9Sstevel@tonic-gate 	int i;
165*7c478bd9Sstevel@tonic-gate 	int stat;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, SIG_IGN);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	/*
170*7c478bd9Sstevel@tonic-gate 	 * If stdin looks like a TLI endpoint, we assume
171*7c478bd9Sstevel@tonic-gate 	 * that we were started by a port monitor. If
172*7c478bd9Sstevel@tonic-gate 	 * t_getstate fails with TBADF, this is not a
173*7c478bd9Sstevel@tonic-gate 	 * TLI endpoint.
174*7c478bd9Sstevel@tonic-gate 	 */
175*7c478bd9Sstevel@tonic-gate 	if (t_getstate(0) != -1 || t_errno != TBADF) {
176*7c478bd9Sstevel@tonic-gate 		char *netid;
177*7c478bd9Sstevel@tonic-gate 		struct netconfig *nconf = NULL;
178*7c478bd9Sstevel@tonic-gate 		SVCXPRT *transp;
179*7c478bd9Sstevel@tonic-gate 		int pmclose;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 		_rpcpmstart = 1;
182*7c478bd9Sstevel@tonic-gate 		openlog("ypxfrd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 		if ((netid = getenv("NLSPROVIDER")) == NULL) {
185*7c478bd9Sstevel@tonic-gate 		/* started from inetd */
186*7c478bd9Sstevel@tonic-gate 			pmclose = 1;
187*7c478bd9Sstevel@tonic-gate 		} else {
188*7c478bd9Sstevel@tonic-gate 			if ((nconf = getnetconfigent(netid)) == NULL)
189*7c478bd9Sstevel@tonic-gate 				_msgout("cannot get transport info");
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 			pmclose = (t_getstate(0) != T_DATAXFER);
192*7c478bd9Sstevel@tonic-gate 		}
193*7c478bd9Sstevel@tonic-gate 		if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
194*7c478bd9Sstevel@tonic-gate 			_msgout("cannot create server handle");
195*7c478bd9Sstevel@tonic-gate 			exit(1);
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 		if (nconf)
198*7c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
199*7c478bd9Sstevel@tonic-gate 		if (!svc_reg(transp, YPXFRD, V1, ypxfrd_1, 0)) {
200*7c478bd9Sstevel@tonic-gate 			_msgout("unable to register (YPXFRD, V1).");
201*7c478bd9Sstevel@tonic-gate 			exit(1);
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 		if (pmclose) {
204*7c478bd9Sstevel@tonic-gate 			(void) signal(SIGALRM, (SIG_PF) closedown);
205*7c478bd9Sstevel@tonic-gate 			(void) alarm(_RPCSVC_CLOSEDOWN/2);
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 		if (yptol_mode) {
209*7c478bd9Sstevel@tonic-gate 			stat = parseConfig(NULL, NTOL_MAP_FILE);
210*7c478bd9Sstevel@tonic-gate 			if (stat == 1) {
211*7c478bd9Sstevel@tonic-gate 				_msgout("NIS to LDAP mapping inactive.");
212*7c478bd9Sstevel@tonic-gate 			} else if (stat != 0) {
213*7c478bd9Sstevel@tonic-gate 				_msgout("Aborting after NIS to LDAP "
214*7c478bd9Sstevel@tonic-gate 					"mapping error.");
215*7c478bd9Sstevel@tonic-gate 				exit(1);
216*7c478bd9Sstevel@tonic-gate 			}
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		svc_run();
220*7c478bd9Sstevel@tonic-gate 		exit(1);
221*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
222*7c478bd9Sstevel@tonic-gate 	}	else {
223*7c478bd9Sstevel@tonic-gate #ifndef RPC_SVC_FG
224*7c478bd9Sstevel@tonic-gate #pragma weak closefrom
225*7c478bd9Sstevel@tonic-gate 		extern void closefrom();
226*7c478bd9Sstevel@tonic-gate 		int size;
227*7c478bd9Sstevel@tonic-gate 		struct rlimit rl;
228*7c478bd9Sstevel@tonic-gate 		pid = fork();
229*7c478bd9Sstevel@tonic-gate 		if (pid < 0) {
230*7c478bd9Sstevel@tonic-gate 			perror("cannot fork");
231*7c478bd9Sstevel@tonic-gate 			exit(1);
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate 		if (pid)
234*7c478bd9Sstevel@tonic-gate 			exit(0);
235*7c478bd9Sstevel@tonic-gate 		closelog();
236*7c478bd9Sstevel@tonic-gate 		if (closefrom != NULL)
237*7c478bd9Sstevel@tonic-gate 			closefrom(0);
238*7c478bd9Sstevel@tonic-gate 		else {
239*7c478bd9Sstevel@tonic-gate 			rl.rlim_max = 0;
240*7c478bd9Sstevel@tonic-gate 			getrlimit(RLIMIT_NOFILE, &rl);
241*7c478bd9Sstevel@tonic-gate 			if ((size = rl.rlim_max) == 0)
242*7c478bd9Sstevel@tonic-gate 				exit(1);
243*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < size; i++)
244*7c478bd9Sstevel@tonic-gate 				(void) close(i);
245*7c478bd9Sstevel@tonic-gate 		}
246*7c478bd9Sstevel@tonic-gate 		i = open("/dev/null", 2);
247*7c478bd9Sstevel@tonic-gate 		(void) dup2(i, 1);
248*7c478bd9Sstevel@tonic-gate 		(void) dup2(i, 2);
249*7c478bd9Sstevel@tonic-gate 		openlog("ypxfrd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
250*7c478bd9Sstevel@tonic-gate 		setsid();
251*7c478bd9Sstevel@tonic-gate #endif
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	if (yptol_mode) {
255*7c478bd9Sstevel@tonic-gate 		stat = parseConfig(NULL, NTOL_MAP_FILE);
256*7c478bd9Sstevel@tonic-gate 		if (stat == 1) {
257*7c478bd9Sstevel@tonic-gate 			_msgout("NIS to LDAP mapping inactive.");
258*7c478bd9Sstevel@tonic-gate 		} else if (stat != 0) {
259*7c478bd9Sstevel@tonic-gate 			_msgout("Aborting after NIS to LDAP mapping error.");
260*7c478bd9Sstevel@tonic-gate 			exit(1);
261*7c478bd9Sstevel@tonic-gate 		}
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	if (!svc_create(ypxfrd_1, YPXFRD, V1, "visible")) {
265*7c478bd9Sstevel@tonic-gate 		_msgout("unable to create (YPXFRD, V1) for visible.");
266*7c478bd9Sstevel@tonic-gate 		exit(1);
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	svc_run();
270*7c478bd9Sstevel@tonic-gate 	_msgout("svc_run returned");
271*7c478bd9Sstevel@tonic-gate 	exit(1);
272*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
273*7c478bd9Sstevel@tonic-gate }
274