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