xref: /freebsd/usr.sbin/ypserv/yp_main.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Copyright (c) 1995
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	$Id: yp_main.c,v 1.3 1996/05/01 02:39:34 wpaul Exp wpaul $
33  */
34 
35 /*
36  * ypserv startup function.
37  * We need out own main() since we have to do some additional work
38  * that rpcgen won't do for us. Most of this file was generated using
39  * rpcgen.new, and later modified.
40  */
41 
42 #include "yp.h"
43 #include <stdio.h>
44 #include <stdlib.h> /* getenv, exit */
45 #include <rpc/pmap_clnt.h> /* for pmap_unset */
46 #include <string.h> /* strcmp */
47 #include <signal.h>
48 #include <sys/ttycom.h> /* TIOCNOTTY */
49 #ifdef __cplusplus
50 #include <sysent.h> /* getdtablesize, open */
51 #endif /* __cplusplus */
52 #include <memory.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <syslog.h>
56 #include <sys/wait.h>
57 #include "yp_extern.h"
58 #include <unistd.h>
59 #include <rpc/rpc.h>
60 #include <errno.h>
61 #include <err.h>
62 
63 #ifndef SIG_PF
64 #define	SIG_PF void(*)(int)
65 #endif
66 
67 #define	_RPCSVC_CLOSEDOWN 120
68 #ifndef lint
69 static const char rcsid[] = "$Id: yp_main.c,v 1.3 1996/05/01 02:39:34 wpaul Exp wpaul $";
70 #endif /* not lint */
71 int _rpcpmstart;		/* Started by a port monitor ? */
72 static int _rpcfdtype;
73 		 /* Whether Stream or Datagram ? */
74 	/* States a server can be in wrt request */
75 
76 #define	_IDLE 0
77 #define	_SERVED 1
78 #define	_SERVING 2
79 
80 extern void ypprog_1 __P((struct svc_req, register SVCXPRT));
81 extern void ypprog_2 __P((struct svc_req, register SVCXPRT));
82 extern int _rpc_dtablesize __P((void));
83 extern int _rpcsvcstate;	 /* Set when a request is serviced */
84 char *progname = "ypserv";
85 char *yp_dir = _PATH_YP;
86 int debug = 0;
87 int do_dns = 0;
88 
89 static
90 void _msgout(char* msg)
91 {
92 	if (debug) {
93 		if (_rpcpmstart)
94 			syslog(LOG_ERR, msg);
95 		else
96 			(void) fprintf(stderr, "%s\n", msg);
97 	} else
98 		syslog(LOG_ERR, msg);
99 }
100 
101 static void
102 yp_svc_run()
103 {
104 #ifdef FD_SETSIZE
105 	fd_set readfds;
106 #else
107 	int readfds;
108 #endif /* def FD_SETSIZE */
109 	extern int forked;
110 	int pid;
111 	int fd_setsize = _rpc_dtablesize();
112 
113 	/* Establish the identity of the parent ypserv process. */
114 	pid = getpid();
115 
116 	for (;;) {
117 #ifdef FD_SETSIZE
118 		readfds = svc_fdset;
119 #else
120 		readfds = svc_fds;
121 #endif /* def FD_SETSIZE */
122 		switch (select(fd_setsize, &readfds, NULL, NULL,
123 			       (struct timeval *)0)) {
124 		case -1:
125 			if (errno == EINTR) {
126 				continue;
127 			}
128 			perror("svc_run: - select failed");
129 			return;
130 		case 0:
131 			continue;
132 		default:
133 			svc_getreqset(&readfds);
134 			if (forked && pid != getpid())
135 				exit(0);
136 		}
137 	}
138 }
139 
140 static void unregister()
141 {
142 	(void) pmap_unset(YPPROG, YPVERS);
143 	(void) pmap_unset(YPPROG, YPOLDVERS);
144 }
145 
146 static void reaper(sig)
147 	int sig;
148 {
149 	int status;
150 
151 	if (sig == SIGHUP) {
152 		load_securenets();
153 #ifdef DB_CACHE
154 		yp_flush_all();
155 #endif
156 		return;
157 	}
158 
159 	if (sig == SIGCHLD) {
160 		while (wait3(&status, WNOHANG, NULL) > 0)
161 			children--;
162 	} else {
163 		unregister();
164 		exit(0);
165 	}
166 }
167 
168 static void usage()
169 {
170 	fprintf(stderr, "Usage: %s [-h] [-d] [-n] [-p path]\n", progname);
171 	exit(1);
172 }
173 
174 static void
175 closedown(int sig)
176 {
177 	if (_rpcsvcstate == _IDLE) {
178 		extern fd_set svc_fdset;
179 		static int size;
180 		int i, openfd;
181 
182 		if (_rpcfdtype == SOCK_DGRAM) {
183 			unregister();
184 			exit(0);
185 		}
186 		if (size == 0) {
187 			size = getdtablesize();
188 		}
189 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
190 			if (FD_ISSET(i, &svc_fdset))
191 				openfd++;
192 		if (openfd <= 1) {
193 			unregister();
194 			exit(0);
195 		}
196 	}
197 	if (_rpcsvcstate == _SERVED)
198 		_rpcsvcstate = _IDLE;
199 
200 	(void) signal(SIGALRM, (SIG_PF) closedown);
201 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
202 }
203 
204 main(argc, argv)
205 	int argc;
206 	char *argv[];
207 {
208 	register SVCXPRT *transp = NULL;
209 	int sock;
210 	int proto = 0;
211 	struct sockaddr_in saddr;
212 	int asize = sizeof (saddr);
213 	int ch;
214 
215 	while ((ch = getopt(argc, argv, "hdnp:")) != EOF) {
216 		switch(ch) {
217 		case 'd':
218 			debug = ypdb_debug = 1;
219 			break;
220 		case 'n':
221 			do_dns = 1;
222 			break;
223 		case 'p':
224 			yp_dir = optarg;
225 			break;
226 		case 'h':
227 		default:
228 			usage();
229 		}
230 	}
231 
232 	load_securenets();
233 #ifdef DB_CACHE
234 	yp_init_dbs();
235 #endif
236 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
237 		int ssize = sizeof (int);
238 
239 		if (saddr.sin_family != AF_INET)
240 			exit(1);
241 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
242 				(char *)&_rpcfdtype, &ssize) == -1)
243 			exit(1);
244 		sock = 0;
245 		_rpcpmstart = 1;
246 		proto = 0;
247 		openlog(progname, LOG_PID, LOG_DAEMON);
248 	} else {
249 		if (!debug) {
250 			if (daemon(0,0)) {
251 				err(1,"cannot fork");
252 			}
253 			openlog(progname, LOG_PID, LOG_DAEMON);
254 		}
255 		sock = RPC_ANYSOCK;
256 		(void) pmap_unset(YPPROG, YPVERS);
257 		(void) pmap_unset(YPPROG, 1);
258 	}
259 
260 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
261 		transp = svcudp_create(sock);
262 		if (transp == NULL) {
263 			_msgout("cannot create udp service.");
264 			exit(1);
265 		}
266 		if (!_rpcpmstart)
267 			proto = IPPROTO_UDP;
268 		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
269 			_msgout("unable to register (YPPROG, YPOLDVERS, udp).");
270 			exit(1);
271 		}
272 		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
273 			_msgout("unable to register (YPPROG, YPVERS, udp).");
274 			exit(1);
275 		}
276 	}
277 
278 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
279 		transp = svctcp_create(sock, 0, 0);
280 		if (transp == NULL) {
281 			_msgout("cannot create tcp service.");
282 			exit(1);
283 		}
284 		if (!_rpcpmstart)
285 			proto = IPPROTO_TCP;
286 		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
287 			_msgout("unable to register (YPPROG, YPOLDVERS, tcp).");
288 			exit(1);
289 		}
290 		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
291 			_msgout("unable to register (YPPROG, YPVERS, tcp).");
292 			exit(1);
293 		}
294 	}
295 
296 	if (transp == (SVCXPRT *)NULL) {
297 		_msgout("could not create a handle");
298 		exit(1);
299 	}
300 	if (_rpcpmstart) {
301 		(void) signal(SIGALRM, (SIG_PF) closedown);
302 		(void) alarm(_RPCSVC_CLOSEDOWN/2);
303 	}
304 /*
305  * Make sure SIGPIPE doesn't blow us away while servicing TCP
306  * connections.
307  */
308 	(void) signal(SIGPIPE, SIG_IGN);
309 	(void) signal(SIGCHLD, (SIG_PF) reaper);
310 	(void) signal(SIGTERM, (SIG_PF) reaper);
311 	(void) signal(SIGINT, (SIG_PF) reaper);
312 	(void) signal(SIGHUP, (SIG_PF) reaper);
313 	yp_svc_run();
314 	_msgout("svc_run returned");
315 	exit(1);
316 	/* NOTREACHED */
317 }
318