xref: /freebsd/usr.sbin/ypserv/yp_main.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 
33 #ifndef lint
34 static const char rcsid[] =
35 	"$Id: yp_main.c,v 1.19 1999/02/10 16:16:14 wpaul Exp $";
36 #endif /* not lint */
37 
38 /*
39  * ypserv startup function.
40  * We need out own main() since we have to do some additional work
41  * that rpcgen won't do for us. Most of this file was generated using
42  * rpcgen.new, and later modified.
43  */
44 
45 #include "yp.h"
46 #include <err.h>
47 #include <errno.h>
48 #include <memory.h>
49 #include <stdio.h>
50 #include <signal.h>
51 #include <stdlib.h> /* getenv, exit */
52 #include <string.h> /* strcmp */
53 #include <syslog.h>
54 #include <unistd.h>
55 #include <rpc/pmap_clnt.h> /* for pmap_unset */
56 #include <sys/ttycom.h> /* TIOCNOTTY */
57 #ifdef __cplusplus
58 #include <sysent.h> /* getdtablesize, open */
59 #endif /* __cplusplus */
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <sys/wait.h>
63 #include "yp_extern.h"
64 #include <rpc/rpc.h>
65 
66 #ifndef SIG_PF
67 #define	SIG_PF void(*)(int)
68 #endif
69 
70 #define	_RPCSVC_CLOSEDOWN 120
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 int resfd;
89 
90 static
91 void _msgout(char* msg)
92 {
93 	if (debug) {
94 		if (_rpcpmstart)
95 			syslog(LOG_ERR, msg);
96 		else
97 			warnx("%s", msg);
98 	} else
99 		syslog(LOG_ERR, msg);
100 }
101 
102 pid_t	yp_pid;
103 
104 static void
105 yp_svc_run()
106 {
107 #ifdef FD_SETSIZE
108 	fd_set readfds;
109 #else
110 	int readfds;
111 #endif /* def FD_SETSIZE */
112 	extern int forked;
113 	int fd_setsize = _rpc_dtablesize();
114 	struct timeval timeout;
115 
116 	/* Establish the identity of the parent ypserv process. */
117 	yp_pid = getpid();
118 
119 	for (;;) {
120 #ifdef FD_SETSIZE
121 		readfds = svc_fdset;
122 #else
123 		readfds = svc_fds;
124 #endif /* def FD_SETSIZE */
125 
126 		FD_SET(resfd, &readfds);
127 
128 		timeout.tv_sec = RESOLVER_TIMEOUT;
129 		timeout.tv_usec = 0;
130 		switch (select(fd_setsize, &readfds, NULL, NULL,
131 			       &timeout)) {
132 		case -1:
133 			if (errno == EINTR) {
134 				continue;
135 			}
136 			warn("svc_run: - select failed");
137 			return;
138 		case 0:
139 			if (getpid() == yp_pid)
140 				yp_prune_dnsq();
141 			break;
142 		default:
143 			if (getpid() == yp_pid) {
144 				if (FD_ISSET(resfd, &readfds)) {
145 					yp_run_dnsq();
146 					FD_CLR(resfd, &readfds);
147 				}
148 				svc_getreqset(&readfds);
149 			}
150 		}
151 		if (yp_pid != getpid())
152 			_exit(0);
153 	}
154 }
155 
156 static void unregister()
157 {
158 	(void) pmap_unset(YPPROG, YPVERS);
159 	(void) pmap_unset(YPPROG, YPOLDVERS);
160 }
161 
162 static void reaper(sig)
163 	int sig;
164 {
165 	int			status;
166 	int			saved_errno;
167 
168 	saved_errno = errno;
169 
170 	if (sig == SIGHUP) {
171 		load_securenets();
172 #ifdef DB_CACHE
173 		yp_flush_all();
174 #endif
175 		errno = saved_errno;
176 		return;
177 	}
178 
179 	if (sig == SIGCHLD) {
180 		while (wait3(&status, WNOHANG, NULL) > 0)
181 			children--;
182 	} else {
183 		unregister();
184 		exit(0);
185 	}
186 	errno = saved_errno;
187 	return;
188 }
189 
190 static void usage()
191 {
192 	fprintf(stderr, "usage: ypserv [-h] [-d] [-n] [-p path]\n");
193 	exit(1);
194 }
195 
196 static void
197 closedown(int sig)
198 {
199 	if (_rpcsvcstate == _IDLE) {
200 		extern fd_set svc_fdset;
201 		static int size;
202 		int i, openfd;
203 
204 		if (_rpcfdtype == SOCK_DGRAM) {
205 			unregister();
206 			exit(0);
207 		}
208 		if (size == 0) {
209 			size = getdtablesize();
210 		}
211 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
212 			if (FD_ISSET(i, &svc_fdset))
213 				openfd++;
214 		if (openfd <= 1) {
215 			unregister();
216 			exit(0);
217 		}
218 	}
219 	if (_rpcsvcstate == _SERVED)
220 		_rpcsvcstate = _IDLE;
221 
222 	(void) signal(SIGALRM, (SIG_PF) closedown);
223 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
224 }
225 
226 int
227 main(argc, argv)
228 	int argc;
229 	char *argv[];
230 {
231 	register SVCXPRT *transp = NULL;
232 	int sock;
233 	int proto = 0;
234 	struct sockaddr_in saddr;
235 	int asize = sizeof (saddr);
236 	int ch;
237 
238 	while ((ch = getopt(argc, argv, "hdnp:")) != -1) {
239 		switch(ch) {
240 		case 'd':
241 			debug = ypdb_debug = 1;
242 			break;
243 		case 'n':
244 			do_dns = 1;
245 			break;
246 		case 'p':
247 			yp_dir = optarg;
248 			break;
249 		case 'h':
250 		default:
251 			usage();
252 		}
253 	}
254 
255 	load_securenets();
256 	yp_init_resolver();
257 #ifdef DB_CACHE
258 	yp_init_dbs();
259 #endif
260 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
261 		int ssize = sizeof (int);
262 
263 		if (saddr.sin_family != AF_INET)
264 			exit(1);
265 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
266 				(char *)&_rpcfdtype, &ssize) == -1)
267 			exit(1);
268 		sock = 0;
269 		_rpcpmstart = 1;
270 		proto = 0;
271 		openlog("ypserv", LOG_PID, LOG_DAEMON);
272 	} else {
273 		if (!debug) {
274 			if (daemon(0,0)) {
275 				err(1,"cannot fork");
276 			}
277 			openlog("ypserv", LOG_PID, LOG_DAEMON);
278 		}
279 		sock = RPC_ANYSOCK;
280 		(void) pmap_unset(YPPROG, YPVERS);
281 		(void) pmap_unset(YPPROG, 1);
282 	}
283 
284 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
285 		transp = svcudp_create(sock);
286 		if (transp == NULL) {
287 			_msgout("cannot create udp service");
288 			exit(1);
289 		}
290 		if (!_rpcpmstart)
291 			proto = IPPROTO_UDP;
292 		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
293 			_msgout("unable to register (YPPROG, YPOLDVERS, udp)");
294 			exit(1);
295 		}
296 		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
297 			_msgout("unable to register (YPPROG, YPVERS, udp)");
298 			exit(1);
299 		}
300 	}
301 
302 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
303 		transp = svctcp_create(sock, 0, 0);
304 		if (transp == NULL) {
305 			_msgout("cannot create tcp service");
306 			exit(1);
307 		}
308 		if (!_rpcpmstart)
309 			proto = IPPROTO_TCP;
310 		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
311 			_msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
312 			exit(1);
313 		}
314 		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
315 			_msgout("unable to register (YPPROG, YPVERS, tcp)");
316 			exit(1);
317 		}
318 	}
319 
320 	if (transp == (SVCXPRT *)NULL) {
321 		_msgout("could not create a handle");
322 		exit(1);
323 	}
324 	if (_rpcpmstart) {
325 		(void) signal(SIGALRM, (SIG_PF) closedown);
326 		(void) alarm(_RPCSVC_CLOSEDOWN/2);
327 	}
328 /*
329  * Make sure SIGPIPE doesn't blow us away while servicing TCP
330  * connections.
331  */
332 	(void) signal(SIGPIPE, SIG_IGN);
333 	(void) signal(SIGCHLD, (SIG_PF) reaper);
334 	(void) signal(SIGTERM, (SIG_PF) reaper);
335 	(void) signal(SIGINT, (SIG_PF) reaper);
336 	(void) signal(SIGHUP, (SIG_PF) reaper);
337 	yp_svc_run();
338 	_msgout("svc_run returned");
339 	exit(1);
340 	/* NOTREACHED */
341 }
342