xref: /dragonfly/usr.sbin/ypserv/yp_main.c (revision 0db87cb7)
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  * $FreeBSD: src/usr.sbin/ypserv/yp_main.c,v 1.29 2008/02/03 17:39:37 matteo Exp $
33  * $DragonFly: src/usr.sbin/ypserv/yp_main.c,v 1.5 2005/11/24 22:23:02 swildner Exp $
34  */
35 
36 /*
37  * ypserv startup function.
38  * We need out own main() since we have to do some additional work
39  * that rpcgen won't do for us. Most of this file was generated using
40  * rpcgen.new, and later modified.
41  */
42 
43 #include "yp.h"
44 #include <err.h>
45 #include <errno.h>
46 #include <memory.h>
47 #include <stdio.h>
48 #include <signal.h>
49 #include <stdlib.h> /* getenv, exit */
50 #include <string.h> /* strcmp */
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <rpc/pmap_clnt.h> /* for pmap_unset */
54 #ifdef __cplusplus
55 #include <sysent.h> /* getdtablesize, open */
56 #endif /* __cplusplus */
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <sys/wait.h>
60 #include "yp_extern.h"
61 #include <rpc/rpc.h>
62 
63 #ifndef SIG_PF
64 #define	SIG_PF void(*)(int)
65 #endif
66 
67 #define	_RPCSVC_CLOSEDOWN 120
68 int _rpcpmstart;		/* Started by a port monitor ? */
69 static int _rpcfdtype;
70 		 /* Whether Stream or Datagram ? */
71 	/* States a server can be in wrt request */
72 
73 #define	_IDLE 0
74 #define	_SERVED 1
75 #define	_SERVING 2
76 
77 extern void ypprog_1(struct svc_req *, SVCXPRT *);
78 extern void ypprog_2(struct svc_req *, SVCXPRT *);
79 extern int _rpc_dtablesize(void);
80 extern int _rpcsvcstate;	 /* Set when a request is serviced */
81 char *progname = "ypserv";
82 const char *yp_dir = _PATH_YP;
83 /*int debug = 0;*/
84 int do_dns = 0;
85 int resfd;
86 
87 struct socktype {
88 	const char *st_name;
89 	int	   st_type;
90 };
91 static struct socktype stlist[] = {
92 	{ "tcp", SOCK_STREAM },
93 	{ "udp", SOCK_DGRAM },
94 	{ NULL, 0 }
95 };
96 
97 static void
98 _msgout(char *msg)
99 {
100 	if (debug) {
101 		if (_rpcpmstart)
102 			syslog(LOG_ERR, "%s", msg);
103 		else
104 			warnx("%s", msg);
105 	} else
106 		syslog(LOG_ERR, "%s", msg);
107 }
108 
109 pid_t	yp_pid;
110 
111 static void
112 yp_svc_run(void)
113 {
114 #ifdef FD_SETSIZE
115 	fd_set readfds;
116 #else
117 	int readfds;
118 #endif /* def FD_SETSIZE */
119 	int fd_setsize = _rpc_dtablesize();
120 	struct timeval timeout;
121 
122 	/* Establish the identity of the parent ypserv process. */
123 	yp_pid = getpid();
124 
125 	for (;;) {
126 #ifdef FD_SETSIZE
127 		readfds = svc_fdset;
128 #else
129 		readfds = svc_fds;
130 #endif /* def FD_SETSIZE */
131 
132 		FD_SET(resfd, &readfds);
133 
134 		timeout.tv_sec = RESOLVER_TIMEOUT;
135 		timeout.tv_usec = 0;
136 		switch (select(fd_setsize, &readfds, NULL, NULL,
137 			       &timeout)) {
138 		case -1:
139 			if (errno == EINTR) {
140 				continue;
141 			}
142 			warn("svc_run: - select failed");
143 			return;
144 		case 0:
145 			if (getpid() == yp_pid)
146 				yp_prune_dnsq();
147 			break;
148 		default:
149 			if (getpid() == yp_pid) {
150 				if (FD_ISSET(resfd, &readfds)) {
151 					yp_run_dnsq();
152 					FD_CLR(resfd, &readfds);
153 				}
154 				svc_getreqset(&readfds);
155 			}
156 		}
157 		if (yp_pid != getpid())
158 			_exit(0);
159 	}
160 }
161 
162 static void
163 unregister(void)
164 {
165 	pmap_unset(YPPROG, YPVERS);
166 	pmap_unset(YPPROG, YPOLDVERS);
167 }
168 
169 static void
170 reaper(int sig)
171 {
172 	int status;
173 	int saved_errno;
174 
175 	saved_errno = errno;
176 
177 	if (sig == SIGHUP) {
178 		load_securenets();
179 #ifdef DB_CACHE
180 		yp_flush_all();
181 #endif
182 		errno = saved_errno;
183 		return;
184 	}
185 
186 	if (sig == SIGCHLD) {
187 		while (wait3(&status, WNOHANG, NULL) > 0)
188 			children--;
189 	} else {
190 		unregister();
191 		exit(0);
192 	}
193 	errno = saved_errno;
194 	return;
195 }
196 
197 static void
198 usage(void)
199 {
200 	fprintf(stderr, "usage: ypserv [-h] [-d] [-n] [-p path] [-P port]\n");
201 	exit(1);
202 }
203 
204 static void
205 closedown(int sig)
206 {
207 	if (_rpcsvcstate == _IDLE) {
208 		extern fd_set svc_fdset;
209 		static int size;
210 		int i, openfd;
211 
212 		if (_rpcfdtype == SOCK_DGRAM) {
213 			unregister();
214 			exit(0);
215 		}
216 		if (size == 0) {
217 			size = getdtablesize();
218 		}
219 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
220 			if (FD_ISSET(i, &svc_fdset))
221 				openfd++;
222 		if (openfd <= 1) {
223 			unregister();
224 			exit(0);
225 		}
226 	}
227 	if (_rpcsvcstate == _SERVED)
228 		_rpcsvcstate = _IDLE;
229 
230 	signal(SIGALRM, (SIG_PF)closedown);
231 	alarm(_RPCSVC_CLOSEDOWN / 2);
232 }
233 
234 int
235 main(int argc, char **argv)
236 {
237 	SVCXPRT *transp = NULL;
238 	int sock;
239 	int proto = 0;
240 	struct sockaddr_in saddr;
241 	socklen_t asize = sizeof (saddr);
242 	int ch;
243 	in_port_t yp_port = 0;
244 	char *errstr;
245 	struct socktype *st;
246 
247 	while ((ch = getopt(argc, argv, "hdnp:P:")) != -1) {
248 		switch (ch) {
249 		case 'd':
250 			debug = ypdb_debug = 1;
251 			break;
252 		case 'n':
253 			do_dns = 1;
254 			break;
255 		case 'p':
256 			yp_dir = optarg;
257 			break;
258 		case 'P':
259 			yp_port = (in_port_t)strtonum(optarg, 1, 65535,
260 			    (const char **)&errstr);
261 			if (yp_port == 0 && errstr != NULL) {
262 				_msgout("invalid port number provided");
263 				exit(1);
264 			}
265 			break;
266 		case 'h':
267 		default:
268 			usage();
269 		}
270 	}
271 
272 	load_securenets();
273 	yp_init_resolver();
274 #ifdef DB_CACHE
275 	yp_init_dbs();
276 #endif
277 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
278 		int ssize = sizeof (int);
279 
280 		if (saddr.sin_family != AF_INET)
281 			exit(1);
282 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
283 				(char *)&_rpcfdtype, &ssize) == -1)
284 			exit(1);
285 		sock = 0;
286 		_rpcpmstart = 1;
287 		proto = 0;
288 		openlog("ypserv", LOG_PID, LOG_DAEMON);
289 	} else {
290 		if (!debug) {
291 			if (daemon(0,0)) {
292 				err(1,"cannot fork");
293 			}
294 			openlog("ypserv", LOG_PID, LOG_DAEMON);
295 		}
296 		sock = RPC_ANYSOCK;
297 		pmap_unset(YPPROG, YPVERS);
298 		pmap_unset(YPPROG, 1);
299 	}
300 
301 	/*
302 	 * Initialize TCP/UDP sockets.
303 	 */
304 	memset((char *)&saddr, 0, sizeof(saddr));
305 	saddr.sin_family = AF_INET;
306 	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
307 	saddr.sin_port = htons(yp_port);
308 	for (st = stlist; st->st_name != NULL; st++) {
309 		/* Do not bind the socket if the user didn't specify a port */
310 		if (yp_port == 0)
311 			break;
312 
313 		sock = socket(AF_INET, st->st_type, 0);
314 		if (sock == -1) {
315 			if ((asprintf(&errstr, "cannot create a %s socket",
316 			    st->st_name)) == -1)
317 				err(1, "unexpected failure in asprintf()");
318 			_msgout(errstr);
319 			free((void *)errstr);
320 			exit(1);
321 		}
322 		if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr))
323 		    == -1) {
324 			if ((asprintf(&errstr, "cannot bind %s socket",
325 			    st->st_name)) == -1)
326 				err(1, "unexpected failure in asprintf()");
327 			_msgout(errstr);
328 			free((void *)errstr);
329 			exit(1);
330 		}
331 		errstr = NULL;
332 	}
333 
334 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
335 		transp = svcudp_create(sock);
336 		if (transp == NULL) {
337 			_msgout("cannot create udp service");
338 			exit(1);
339 		}
340 		if (!_rpcpmstart)
341 			proto = IPPROTO_UDP;
342 		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
343 			_msgout("unable to register (YPPROG, YPOLDVERS, udp)");
344 			exit(1);
345 		}
346 		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
347 			_msgout("unable to register (YPPROG, YPVERS, udp)");
348 			exit(1);
349 		}
350 	}
351 
352 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
353 		transp = svctcp_create(sock, 0, 0);
354 		if (transp == NULL) {
355 			_msgout("cannot create tcp service");
356 			exit(1);
357 		}
358 		if (!_rpcpmstart)
359 			proto = IPPROTO_TCP;
360 		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
361 			_msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
362 			exit(1);
363 		}
364 		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
365 			_msgout("unable to register (YPPROG, YPVERS, tcp)");
366 			exit(1);
367 		}
368 	}
369 
370 	if (transp == NULL) {
371 		_msgout("could not create a handle");
372 		exit(1);
373 	}
374 	if (_rpcpmstart) {
375 		signal(SIGALRM, (SIG_PF)closedown);
376 		alarm(_RPCSVC_CLOSEDOWN / 2);
377 	}
378 /*
379  * Make sure SIGPIPE doesn't blow us away while servicing TCP
380  * connections.
381  */
382 	signal(SIGPIPE, SIG_IGN);
383 	signal(SIGCHLD, (SIG_PF) reaper);
384 	signal(SIGTERM, (SIG_PF) reaper);
385 	signal(SIGINT, (SIG_PF) reaper);
386 	signal(SIGHUP, (SIG_PF) reaper);
387 	yp_svc_run();
388 	_msgout("svc_run returned");
389 	exit(1);
390 	/* NOTREACHED */
391 }
392