yp_main.c revision 223340
1155192Srwatson/*
2155192Srwatson * Copyright (c) 1995
3155192Srwatson *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4155192Srwatson *
5155192Srwatson * Redistribution and use in source and binary forms, with or without
6155192Srwatson * modification, are permitted provided that the following conditions
7155192Srwatson * are met:
8155192Srwatson * 1. Redistributions of source code must retain the above copyright
9155192Srwatson *    notice, this list of conditions and the following disclaimer.
10155192Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11155192Srwatson *    notice, this list of conditions and the following disclaimer in the
12155192Srwatson *    documentation and/or other materials provided with the distribution.
13155192Srwatson * 3. All advertising materials mentioning features or use of this software
14155192Srwatson *    must display the following acknowledgement:
15155192Srwatson *	This product includes software developed by Bill Paul.
16155192Srwatson * 4. Neither the name of the author nor the names of any co-contributors
17155192Srwatson *    may be used to endorse or promote products derived from this software
18155192Srwatson *    without specific prior written permission.
19155192Srwatson *
20155192Srwatson * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23155192Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24155192Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28155192Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29155192Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30155192Srwatson * SUCH DAMAGE.
31155192Srwatson */
32155192Srwatson
33155192Srwatson#include <sys/cdefs.h>
34155192Srwatson__FBSDID("$FreeBSD: head/usr.sbin/ypserv/yp_main.c 223340 2011-06-20 15:22:47Z kuriyama $");
35155192Srwatson
36155192Srwatson/*
37156882Srwatson * ypserv startup function.
38156882Srwatson * We need out own main() since we have to do some additional work
39155192Srwatson * that rpcgen won't do for us. Most of this file was generated using
40155192Srwatson * rpcgen.new, and later modified.
41155192Srwatson */
42155192Srwatson
43155192Srwatson#include <sys/types.h>
44155192Srwatson#include <sys/queue.h>
45155192Srwatson#include <sys/socket.h>
46155192Srwatson#include <sys/wait.h>
47155192Srwatson#include "yp.h"
48155192Srwatson#include <err.h>
49155192Srwatson#include <errno.h>
50155192Srwatson#include <memory.h>
51155192Srwatson#include <stdio.h>
52155192Srwatson#include <signal.h>
53155192Srwatson#include <stdarg.h>
54155192Srwatson#include <stdlib.h> /* getenv, exit */
55155192Srwatson#include <string.h> /* strcmp */
56155192Srwatson#include <syslog.h>
57155192Srwatson#include <unistd.h>
58155192Srwatson#ifdef __cplusplus
59155192Srwatson#include <sysent.h> /* getdtablesize, open */
60155192Srwatson#endif /* __cplusplus */
61155192Srwatson#include <netinet/in.h>
62155192Srwatson#include <netdb.h>
63155192Srwatson#include "yp_extern.h"
64155192Srwatson#include <netconfig.h>
65155192Srwatson#include <rpc/rpc.h>
66155192Srwatson#include <rpc/rpc_com.h>
67155192Srwatson
68155192Srwatson#ifndef SIG_PF
69155192Srwatson#define	SIG_PF void(*)(int)
70155192Srwatson#endif
71155192Srwatson
72155192Srwatson#define	_RPCSVC_CLOSEDOWN 120
73155192Srwatsonint _rpcpmstart;		/* Started by a port monitor ? */
74155192Srwatsonstatic int _rpcfdtype;
75155192Srwatson		 /* Whether Stream or Datagram ? */
76155192Srwatsonstatic int _rpcaf;
77155192Srwatsonstatic int _rpcfd;
78155192Srwatson
79155192Srwatson	/* States a server can be in wrt request */
80155192Srwatson
81155192Srwatson#define	_IDLE 0
82155192Srwatson#define	_SERVED 1
83155192Srwatson#define	_SERVING 2
84155192Srwatson
85155192Srwatsonextern void ypprog_1(struct svc_req *, SVCXPRT *);
86155192Srwatsonextern void ypprog_2(struct svc_req *, SVCXPRT *);
87155192Srwatsonextern int _rpc_dtablesize(void);
88155192Srwatsonextern int _rpcsvcstate;	 /* Set when a request is serviced */
89155192Srwatsonchar *progname = "ypserv";
90155192Srwatsonchar *yp_dir = _PATH_YP;
91155192Srwatson/*int debug = 0;*/
92155192Srwatsonint do_dns = 0;
93155192Srwatsonint resfd;
94155192Srwatson
95155192Srwatsonstruct socklistent {
96155192Srwatson	int				sle_sock;
97155192Srwatson	struct sockaddr_storage		sle_ss;
98155192Srwatson	SLIST_ENTRY(socklistent)	sle_next;
99155192Srwatson};
100155192Srwatsonstatic SLIST_HEAD(, socklistent) sle_head =
101155192Srwatson	SLIST_HEAD_INITIALIZER(sle_head);
102155192Srwatson
103155192Srwatsonstruct bindaddrlistent {
104155192Srwatson	const char			*ble_hostname;
105155192Srwatson	SLIST_ENTRY(bindaddrlistent)	ble_next;
106155192Srwatson};
107155192Srwatsonstatic SLIST_HEAD(, bindaddrlistent) ble_head =
108155192Srwatson	SLIST_HEAD_INITIALIZER(ble_head);
109155192Srwatson
110155192Srwatsonstatic char *servname = "0";
111155192Srwatson
112155192Srwatsonstatic
113155192Srwatsonvoid _msgout(char* msg, ...)
114155192Srwatson{
115155192Srwatson	va_list ap;
116155192Srwatson
117155192Srwatson	va_start(ap, msg);
118155192Srwatson	if (debug) {
119155192Srwatson		if (_rpcpmstart)
120155192Srwatson			vsyslog(LOG_ERR, msg, ap);
121155192Srwatson		else
122155192Srwatson			vwarnx(msg, ap);
123155192Srwatson	} else
124155192Srwatson		vsyslog(LOG_ERR, msg, ap);
125155192Srwatson	va_end(ap);
126155192Srwatson}
127155192Srwatson
128155192Srwatsonpid_t	yp_pid;
129155192Srwatson
130155192Srwatsonstatic void
131155192Srwatsonyp_svc_run(void)
132155192Srwatson{
133155192Srwatson#ifdef FD_SETSIZE
134155192Srwatson	fd_set readfds;
135155192Srwatson#else
136155192Srwatson	int readfds;
137155192Srwatson#endif /* def FD_SETSIZE */
138155192Srwatson	int fd_setsize = _rpc_dtablesize();
139155192Srwatson	struct timeval timeout;
140155192Srwatson
141155192Srwatson	/* Establish the identity of the parent ypserv process. */
142155192Srwatson	yp_pid = getpid();
143155192Srwatson
144155192Srwatson	for (;;) {
145155192Srwatson#ifdef FD_SETSIZE
146155192Srwatson		readfds = svc_fdset;
147155192Srwatson#else
148155192Srwatson		readfds = svc_fds;
149155192Srwatson#endif /* def FD_SETSIZE */
150155192Srwatson
151155192Srwatson		FD_SET(resfd, &readfds);
152155192Srwatson
153155192Srwatson		timeout.tv_sec = RESOLVER_TIMEOUT;
154155192Srwatson		timeout.tv_usec = 0;
155155192Srwatson		switch (select(fd_setsize, &readfds, NULL, NULL,
156155192Srwatson			       &timeout)) {
157155192Srwatson		case -1:
158155192Srwatson			if (errno == EINTR) {
159155192Srwatson				continue;
160155192Srwatson			}
161155192Srwatson			warn("svc_run: - select failed");
162155192Srwatson			return;
163155192Srwatson		case 0:
164155192Srwatson			if (getpid() == yp_pid)
165155192Srwatson				yp_prune_dnsq();
166155192Srwatson			break;
167155192Srwatson		default:
168155192Srwatson			if (getpid() == yp_pid) {
169155192Srwatson				if (FD_ISSET(resfd, &readfds)) {
170155192Srwatson					yp_run_dnsq();
171155192Srwatson					FD_CLR(resfd, &readfds);
172155192Srwatson				}
173155192Srwatson				svc_getreqset(&readfds);
174155192Srwatson			}
175155192Srwatson		}
176155192Srwatson		if (yp_pid != getpid())
177155192Srwatson			_exit(0);
178155192Srwatson	}
179155192Srwatson}
180155192Srwatson
181155192Srwatsonstatic void
182155192Srwatsonunregister(void)
183155192Srwatson{
184155192Srwatson	(void)svc_unreg(YPPROG, YPVERS);
185155192Srwatson	(void)svc_unreg(YPPROG, YPOLDVERS);
186155192Srwatson}
187155192Srwatson
188155192Srwatsonstatic void
189155192Srwatsonreaper(int sig)
190155192Srwatson{
191155192Srwatson	int			status;
192155192Srwatson	int			saved_errno;
193155192Srwatson
194155192Srwatson	saved_errno = errno;
195155192Srwatson
196155192Srwatson	if (sig == SIGHUP) {
197155192Srwatson		load_securenets();
198155192Srwatson#ifdef DB_CACHE
199155192Srwatson		yp_flush_all();
200155192Srwatson#endif
201155192Srwatson		errno = saved_errno;
202155192Srwatson		return;
203155192Srwatson	}
204155192Srwatson
205155192Srwatson	if (sig == SIGCHLD) {
206155192Srwatson		while (wait3(&status, WNOHANG, NULL) > 0)
207155192Srwatson			children--;
208155192Srwatson	} else {
209155192Srwatson		unregister();
210155192Srwatson		exit(0);
211155192Srwatson	}
212155192Srwatson	errno = saved_errno;
213155192Srwatson	return;
214155192Srwatson}
215155192Srwatson
216155192Srwatsonstatic void
217155192Srwatsonusage(void)
218155192Srwatson{
219155192Srwatson	fprintf(stderr, "usage: ypserv [-h addr] [-d] [-n] [-p path] [-P port]\n");
220155192Srwatson	exit(1);
221155192Srwatson}
222155192Srwatson
223155192Srwatsonstatic void
224155192Srwatsonclosedown(int sig)
225155192Srwatson{
226155192Srwatson	if (_rpcsvcstate == _IDLE) {
227155192Srwatson		extern fd_set svc_fdset;
228155192Srwatson		static int size;
229155192Srwatson		int i, openfd;
230155192Srwatson
231155192Srwatson		if (_rpcfdtype == SOCK_DGRAM) {
232155192Srwatson			unregister();
233155192Srwatson			exit(0);
234155192Srwatson		}
235155192Srwatson		if (size == 0) {
236156291Srwatson			size = getdtablesize();
237155192Srwatson		}
238155192Srwatson		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
239155192Srwatson			if (FD_ISSET(i, &svc_fdset))
240155192Srwatson				openfd++;
241155192Srwatson		if (openfd <= 1) {
242155192Srwatson			unregister();
243155192Srwatson			exit(0);
244155192Srwatson		}
245155192Srwatson	}
246155192Srwatson	if (_rpcsvcstate == _SERVED)
247155192Srwatson		_rpcsvcstate = _IDLE;
248155192Srwatson
249155192Srwatson	(void) signal(SIGALRM, (SIG_PF) closedown);
250155192Srwatson	(void) alarm(_RPCSVC_CLOSEDOWN/2);
251155192Srwatson}
252155192Srwatson
253155192Srwatsonstatic int
254155192Srwatsoncreate_service(const int sock, const struct netconfig *nconf,
255155192Srwatson	const struct __rpc_sockinfo *si)
256155192Srwatson{
257155192Srwatson	int error;
258155192Srwatson
259155192Srwatson	SVCXPRT *transp;
260155192Srwatson	struct addrinfo hints, *res, *res0;
261155192Srwatson	struct socklistent *slep;
262155192Srwatson	struct bindaddrlistent *blep;
263155192Srwatson	struct netbuf svcaddr;
264155192Srwatson
265155192Srwatson	SLIST_INIT(&sle_head);
266155192Srwatson	memset(&hints, 0, sizeof(hints));
267155192Srwatson	memset(&svcaddr, 0, sizeof(svcaddr));
268155192Srwatson
269155192Srwatson	hints.ai_family = si->si_af;
270155192Srwatson	hints.ai_socktype = si->si_socktype;
271155192Srwatson	hints.ai_protocol = si->si_proto;
272155192Srwatson
273155192Srwatson	/*
274155192Srwatson	 * Build socketlist from bindaddrlist.
275155192Srwatson	 */
276155192Srwatson	if (sock == RPC_ANYFD) {
277155192Srwatson		SLIST_FOREACH(blep, &ble_head, ble_next) {
278155192Srwatson			if (blep->ble_hostname == NULL)
279155192Srwatson				hints.ai_flags = AI_PASSIVE;
280155192Srwatson			else
281155192Srwatson				hints.ai_flags = 0;
282155192Srwatson			error = getaddrinfo(blep->ble_hostname, servname,
283155192Srwatson				    &hints, &res0);
284155192Srwatson			if (error) {
285155192Srwatson				_msgout("getaddrinfo(): %s",
286155192Srwatson				    gai_strerror(error));
287155192Srwatson				return -1;
288155192Srwatson			}
289155192Srwatson			for (res = res0; res; res = res->ai_next) {
290156845Srwatson				int s;
291155192Srwatson
292155192Srwatson				s = __rpc_nconf2fd(nconf);
293155192Srwatson				if (s < 0) {
294155192Srwatson					if (errno == EPROTONOSUPPORT)
295155192Srwatson						_msgout("unsupported"
296155192Srwatson						    " transport: %s",
297155192Srwatson						    nconf->nc_netid);
298155192Srwatson					else
299155192Srwatson						_msgout("cannot create"
300155408Srwatson						    " %s socket: %s",
301155408Srwatson						    nconf->nc_netid,
302155408Srwatson						    strerror(errno));
303155408Srwatson					freeaddrinfo(res0);
304155408Srwatson					return -1;
305156882Srwatson				}
306				if (bindresvport_sa(s, res->ai_addr) == -1) {
307					if ((errno != EPERM) ||
308					    (bind(s, res->ai_addr,
309					    res->ai_addrlen) == -1)) {
310						_msgout("cannot bind "
311						    "%s socket: %s",
312						    nconf->nc_netid,
313						strerror(errno));
314						freeaddrinfo(res0);
315						close(sock);
316						return -1;
317					}
318				}
319				if (nconf->nc_semantics != NC_TPI_CLTS)
320					listen(s, SOMAXCONN);
321
322				slep = malloc(sizeof(*slep));
323				if (slep == NULL) {
324					_msgout("malloc failed: %s",
325					    strerror(errno));
326					freeaddrinfo(res0);
327					close(s);
328					return -1;
329				}
330				memset(slep, 0, sizeof(*slep));
331				memcpy(&slep->sle_ss,
332				    (struct sockaddr *)(res->ai_addr),
333				    sizeof(res->ai_addr));
334				slep->sle_sock = s;
335				SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
336
337				/*
338				 * If servname == "0", redefine it by using
339				 * the bound socket.
340				 */
341				if (strncmp("0", servname, 1) == 0) {
342					struct sockaddr *sap;
343					socklen_t slen;
344					char *sname;
345
346					sname = malloc(NI_MAXSERV);
347					if (sname == NULL) {
348						_msgout("malloc(): %s",
349						    strerror(errno));
350						freeaddrinfo(res0);
351						close(s);
352						return -1;
353					}
354					memset(sname, 0, NI_MAXSERV);
355
356					sap = (struct sockaddr *)&slep->sle_ss;
357					slen = sizeof(*sap);
358					error = getsockname(s, sap, &slen);
359					if (error) {
360						_msgout("getsockname(): %s",
361						    strerror(errno));
362						freeaddrinfo(res0);
363						close(s);
364						return -1;
365					}
366					error = getnameinfo(sap, slen,
367					    NULL, 0,
368					    sname, NI_MAXSERV,
369					    NI_NUMERICHOST | NI_NUMERICSERV);
370					if (error) {
371						_msgout("getnameinfo(): %s",
372						    strerror(errno));
373						freeaddrinfo(res0);
374						close(s);
375						return -1;
376					}
377					servname = sname;
378				}
379			}
380			freeaddrinfo(res0);
381		}
382	} else {
383		slep = malloc(sizeof(*slep));
384		if (slep == NULL) {
385			_msgout("malloc failed: %s", strerror(errno));
386			return -1;
387		}
388		memset(slep, 0, sizeof(*slep));
389		slep->sle_sock = sock;
390		SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
391	}
392
393	/*
394	 * Traverse socketlist and create rpc service handles for each socket.
395	 */
396	SLIST_FOREACH(slep, &sle_head, sle_next) {
397		if (nconf->nc_semantics == NC_TPI_CLTS)
398			transp = svc_dg_create(slep->sle_sock, 0, 0);
399		else
400			transp = svc_vc_create(slep->sle_sock, RPC_MAXDATASIZE,
401			    RPC_MAXDATASIZE);
402		if (transp == NULL) {
403			_msgout("unable to create service: %s",
404			    nconf->nc_netid);
405			continue;
406		}
407		if (!svc_reg(transp, YPPROG, YPOLDVERS, ypprog_1, NULL)) {
408			svc_destroy(transp);
409			close(slep->sle_sock);
410			_msgout("unable to register (YPPROG, YPOLDVERS, %s):"
411			    " %s", nconf->nc_netid, strerror(errno));
412			continue;
413		}
414		if (!svc_reg(transp, YPPROG, YPVERS, ypprog_2, NULL)) {
415			svc_destroy(transp);
416			close(slep->sle_sock);
417			_msgout("unable to register (YPPROG, YPVERS, %s): %s",
418			    nconf->nc_netid, strerror(errno));
419			continue;
420		}
421	}
422	while(!(SLIST_EMPTY(&sle_head)))
423		SLIST_REMOVE_HEAD(&sle_head, sle_next);
424
425	/*
426	 * Register RPC service to rpcbind by using AI_PASSIVE address.
427	 */
428	hints.ai_flags = AI_PASSIVE;
429	error = getaddrinfo(NULL, servname, &hints, &res0);
430	if (error) {
431		_msgout("getaddrinfo(): %s", gai_strerror(error));
432		return -1;
433	}
434	svcaddr.buf = res0->ai_addr;
435	svcaddr.len = res0->ai_addrlen;
436
437	if (si->si_af == AF_INET) {
438		/* XXX: ignore error intentionally */
439		rpcb_set(YPPROG, YPOLDVERS, nconf, &svcaddr);
440	}
441	/* XXX: ignore error intentionally */
442	rpcb_set(YPPROG, YPVERS, nconf, &svcaddr);
443
444	freeaddrinfo(res0);
445	return 0;
446}
447
448int
449main(int argc, char *argv[])
450{
451	int ch;
452	int error;
453	int ntrans;
454
455	void *nc_handle;
456	struct netconfig *nconf;
457	struct __rpc_sockinfo si;
458	struct bindaddrlistent *blep;
459
460	memset(&si, 0, sizeof(si));
461	SLIST_INIT(&ble_head);
462
463	while ((ch = getopt(argc, argv, "dh:np:P:")) != -1) {
464		switch (ch) {
465		case 'd':
466			debug = ypdb_debug = 1;
467			break;
468		case 'h':
469			blep = malloc(sizeof(*blep));
470			if (blep == NULL)
471				err(1, "malloc() failed: -h %s", optarg);
472			blep->ble_hostname = optarg;
473			SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
474			break;
475		case 'n':
476			do_dns = 1;
477			break;
478		case 'p':
479			yp_dir = optarg;
480			break;
481		case 'P':
482			servname = optarg;
483			break;
484		default:
485			usage();
486		}
487	}
488	/*
489	 * Add "anyaddr" entry if no -h is specified.
490	 */
491	if (SLIST_EMPTY(&ble_head)) {
492		blep = malloc(sizeof(*blep));
493		if (blep == NULL)
494			err(1, "malloc() failed");
495		memset(blep, 0, sizeof(*blep));
496		SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
497	}
498
499	load_securenets();
500	yp_init_resolver();
501#ifdef DB_CACHE
502	yp_init_dbs();
503#endif
504	nc_handle = setnetconfig();
505	if (nc_handle == NULL)
506		err(1, "cannot read %s", NETCONFIG);
507	if (__rpc_fd2sockinfo(0, &si) != 0) {
508		/* invoked from inetd */
509		_rpcpmstart = 1;
510		_rpcfdtype = si.si_socktype;
511		_rpcaf = si.si_af;
512		_rpcfd = 0;
513		openlog("ypserv", LOG_PID, LOG_DAEMON);
514	} else {
515		/* standalone mode */
516		if (!debug) {
517			if (daemon(0,0)) {
518				err(1,"cannot fork");
519			}
520			openlog("ypserv", LOG_PID, LOG_DAEMON);
521		}
522		_rpcpmstart = 0;
523		_rpcaf = AF_INET;
524		_rpcfd = RPC_ANYFD;
525		unregister();
526	}
527
528	/*
529	 * Create RPC service for each transport.
530	 */
531	ntrans = 0;
532	while((nconf = getnetconfig(nc_handle))) {
533		if ((nconf->nc_flag & NC_VISIBLE)) {
534			if (__rpc_nconf2sockinfo(nconf, &si) == 0) {
535				_msgout("cannot get information for %s.  "
536				    "Ignored.", nconf->nc_netid);
537				continue;
538			}
539			if (_rpcpmstart) {
540				if (si.si_socktype != _rpcfdtype ||
541				    si.si_af != _rpcaf)
542					continue;
543			} else if (si.si_af != _rpcaf)
544					continue;
545			error = create_service(_rpcfd, nconf, &si);
546			if (error) {
547				endnetconfig(nc_handle);
548				exit(1);
549			}
550			ntrans++;
551		}
552	}
553	endnetconfig(nc_handle);
554	while(!(SLIST_EMPTY(&ble_head)))
555		SLIST_REMOVE_HEAD(&ble_head, ble_next);
556	if (ntrans == 0) {
557		_msgout("no transport is available.  Aborted.");
558		exit(1);
559	}
560	if (_rpcpmstart) {
561		(void) signal(SIGALRM, (SIG_PF) closedown);
562		(void) alarm(_RPCSVC_CLOSEDOWN/2);
563	}
564/*
565 * Make sure SIGPIPE doesn't blow us away while servicing TCP
566 * connections.
567 */
568	(void) signal(SIGPIPE, SIG_IGN);
569	(void) signal(SIGCHLD, (SIG_PF) reaper);
570	(void) signal(SIGTERM, (SIG_PF) reaper);
571	(void) signal(SIGINT, (SIG_PF) reaper);
572	(void) signal(SIGHUP, (SIG_PF) reaper);
573	yp_svc_run();
574	_msgout("svc_run returned");
575	exit(1);
576	/* NOTREACHED */
577}
578