Home | History | Annotate | Line # | Download | only in ypserv
      1  1.27  christos /*	$NetBSD: ypserv.c,v 1.27 2021/03/07 15:09:13 christos Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  *
     16   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17   1.1   thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18   1.1   thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20   1.1   thorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1   thorpej  * SUCH DAMAGE.
     27   1.1   thorpej  */
     28   1.1   thorpej 
     29   1.7     lukem #include <sys/cdefs.h>
     30   1.7     lukem #ifndef lint
     31  1.27  christos __RCSID("$NetBSD: ypserv.c,v 1.27 2021/03/07 15:09:13 christos Exp $");
     32   1.7     lukem #endif
     33   1.7     lukem 
     34   1.1   thorpej #include <sys/types.h>
     35   1.1   thorpej #include <sys/socket.h>
     36   1.1   thorpej #include <sys/wait.h>
     37   1.1   thorpej 
     38   1.5   thorpej #include <err.h>
     39   1.1   thorpej #include <netdb.h>
     40   1.1   thorpej #include <signal.h>
     41   1.1   thorpej #include <stdio.h>
     42   1.1   thorpej #include <stdlib.h>
     43   1.1   thorpej #include <string.h>
     44   1.5   thorpej #include <syslog.h>
     45   1.5   thorpej #include <unistd.h>
     46  1.10   thorpej #include <util.h>
     47  1.20  christos #include <stdarg.h>
     48  1.20  christos #include <errno.h>
     49   1.1   thorpej 
     50   1.1   thorpej #include <rpc/rpc.h>
     51   1.1   thorpej #include <rpc/xdr.h>
     52   1.1   thorpej #include <rpc/pmap_clnt.h>
     53   1.1   thorpej 
     54   1.1   thorpej #include <rpcsvc/yp_prot.h>
     55   1.1   thorpej 
     56   1.1   thorpej #include "ypdef.h"
     57   1.1   thorpej #include "ypserv.h"
     58   1.1   thorpej 
     59   1.8   thorpej #ifdef LIBWRAP
     60   1.8   thorpej #include <tcpd.h>
     61   1.8   thorpej 
     62   1.8   thorpej int allow_severity = LOG_DAEMON | LOG_INFO;
     63   1.8   thorpej int deny_severity = LOG_DAEMON | LOG_WARNING;
     64   1.8   thorpej 
     65   1.8   thorpej /* XXX For ypserv_proc.c -- NOT THREAD SAFE!  (like any of this code is) */
     66   1.8   thorpej const char *clientstr;
     67   1.8   thorpej const char *svcname;
     68   1.8   thorpej #endif /* LIBWRAP */
     69   1.1   thorpej 
     70   1.1   thorpej int	usedns;
     71   1.9       abs #ifdef DEBUG
     72  1.20  christos static int	foreground = 1;
     73   1.9       abs #else
     74  1.20  christos static int	foreground;
     75   1.9       abs #endif
     76   1.9       abs 
     77   1.8   thorpej #ifdef LIBWRAP
     78   1.8   thorpej int	lflag;
     79   1.8   thorpej #endif
     80   1.1   thorpej 
     81  1.20  christos static struct bindsock {
     82  1.20  christos 	sa_family_t family;
     83  1.20  christos 	int type;
     84  1.20  christos 	int proto;
     85  1.20  christos 	const char *name;
     86  1.20  christos } socklist[] = {
     87  1.20  christos 	{ AF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp" },
     88  1.20  christos 	{ AF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp" },
     89  1.20  christos 	{ AF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp6" },
     90  1.20  christos 	{ AF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp6" },
     91  1.20  christos };
     92   1.1   thorpej 
     93  1.21     perry static void	usage(void) __dead;
     94  1.20  christos static int	bind_resv_port(int, sa_family_t, in_port_t);
     95  1.22     chuck void		ypserv_sock_hostname(struct host_info *host);
     96  1.20  christos 
     97  1.26     joerg static __printflike(2, 3) void
     98  1.20  christos _msgout(int level, const char *msg, ...)
     99   1.1   thorpej {
    100  1.20  christos 	va_list ap;
    101  1.20  christos 	va_start(ap, msg);
    102  1.12   thorpej 	if (foreground)
    103  1.20  christos                 vwarnx(msg, ap);
    104   1.9       abs         else
    105  1.20  christos 		vsyslog(level, msg, ap);
    106  1.20  christos 	va_end(ap);
    107   1.1   thorpej }
    108   1.1   thorpej 
    109  1.22     chuck void ypserv_sock_hostname(struct host_info *host)
    110  1.22     chuck {
    111  1.22     chuck 	host->name[0] = 0;
    112  1.22     chuck }
    113  1.22     chuck 
    114   1.1   thorpej static void
    115   1.6     lukem ypprog_2(struct svc_req *rqstp, SVCXPRT *transp)
    116   1.1   thorpej {
    117   1.1   thorpej 	union {
    118   1.1   thorpej 		char * ypproc_domain_2_arg;
    119   1.1   thorpej 		char * ypproc_domain_nonack_2_arg;
    120   1.1   thorpej 		struct ypreq_key ypproc_match_2_arg;
    121   1.1   thorpej 		struct ypreq_nokey ypproc_first_2_arg;
    122   1.1   thorpej 		struct ypreq_key ypproc_next_2_arg;
    123   1.1   thorpej 		struct ypreq_xfr ypproc_xfr_2_arg;
    124   1.1   thorpej 		struct ypreq_nokey ypproc_all_2_arg;
    125   1.1   thorpej 		struct ypreq_nokey ypproc_master_2_arg;
    126   1.1   thorpej 		struct ypreq_nokey ypproc_order_2_arg;
    127   1.1   thorpej 		char * ypproc_maplist_2_arg;
    128   1.1   thorpej 	} argument;
    129  1.20  christos 	void *argp = &argument;
    130   1.1   thorpej 	char *result;
    131   1.1   thorpej 	xdrproc_t xdr_argument, xdr_result;
    132  1.16       wiz 	void *(*local)(void *, struct svc_req *);
    133   1.8   thorpej #ifdef LIBWRAP
    134   1.8   thorpej 	struct request_info req;
    135  1.12   thorpej 	struct sockaddr *caller;
    136   1.8   thorpej #define	SVCNAME(x)	svcname = x
    137   1.8   thorpej #else
    138   1.8   thorpej #define	SVCNAME(x)	/* nothing */
    139   1.8   thorpej #endif
    140   1.1   thorpej 
    141   1.8   thorpej #ifdef LIBWRAP
    142  1.12   thorpej 	caller = svc_getrpccaller(transp)->buf;
    143  1.20  christos 	(void)request_init(&req, RQ_DAEMON, getprogname(), RQ_CLIENT_SIN,
    144  1.27  christos 	    caller, RQ_FILE, transp->xp_fd, NULL);
    145   1.8   thorpej 	sock_methods(&req);
    146  1.22     chuck 
    147  1.22     chuck 	/*
    148  1.22     chuck 	 * Do not do hostname lookups!  This avoids possible delays due
    149  1.22     chuck 	 * to DNS, preventing a possible DoS attack, as well as possible
    150  1.22     chuck 	 * circular lookups (e.g. a hostname lookup requiring a request
    151  1.22     chuck 	 * to ourselves).
    152  1.22     chuck 	 */
    153  1.22     chuck 	req.hostname = ypserv_sock_hostname;
    154   1.8   thorpej #endif
    155   1.8   thorpej 
    156   1.1   thorpej 	switch (rqstp->rq_proc) {
    157   1.1   thorpej 	case YPPROC_NULL:
    158  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_void;
    159  1.25    plunky 		xdr_result = (xdrproc_t)xdr_void;
    160   1.1   thorpej 		local = ypproc_null_2_svc;
    161   1.8   thorpej 		SVCNAME("null_2");
    162   1.1   thorpej 		break;
    163   1.1   thorpej 
    164   1.1   thorpej 	case YPPROC_DOMAIN:
    165  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypdomain_wrap_string;
    166  1.25    plunky 		xdr_result = (xdrproc_t)xdr_bool;
    167   1.1   thorpej 		local = ypproc_domain_2_svc;
    168   1.8   thorpej 		SVCNAME("domain_2");
    169   1.1   thorpej 		break;
    170   1.1   thorpej 
    171   1.1   thorpej 	case YPPROC_DOMAIN_NONACK:
    172  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypdomain_wrap_string;
    173  1.25    plunky 		xdr_result = (xdrproc_t)xdr_bool;
    174   1.1   thorpej 		local = ypproc_domain_nonack_2_svc;
    175   1.8   thorpej 		SVCNAME("domain_nonack_2");
    176   1.1   thorpej 		break;
    177   1.1   thorpej 
    178   1.1   thorpej 	case YPPROC_MATCH:
    179  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypreq_key;
    180  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_val;
    181   1.1   thorpej 		local = ypproc_match_2_svc;
    182   1.8   thorpej 		SVCNAME("match_2");
    183   1.1   thorpej 		break;
    184   1.1   thorpej 
    185   1.1   thorpej 	case YPPROC_FIRST:
    186  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypreq_nokey;
    187  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_key_val;
    188   1.1   thorpej 		local = ypproc_first_2_svc;
    189   1.8   thorpej 		SVCNAME("first_2");
    190   1.1   thorpej 		break;
    191   1.1   thorpej 
    192   1.1   thorpej 	case YPPROC_NEXT:
    193  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypreq_key;
    194  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_key_val;
    195   1.1   thorpej 		local = ypproc_next_2_svc;
    196   1.8   thorpej 		SVCNAME("next_2");
    197   1.1   thorpej 		break;
    198   1.1   thorpej 
    199   1.1   thorpej 	case YPPROC_XFR:
    200  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypreq_xfr;
    201  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_xfr;
    202   1.1   thorpej 		local = ypproc_xfr_2_svc;
    203   1.8   thorpej 		SVCNAME("xfer_2");
    204   1.1   thorpej 		break;
    205   1.1   thorpej 
    206   1.1   thorpej 	case YPPROC_CLEAR:
    207  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_void;
    208  1.25    plunky 		xdr_result = (xdrproc_t)xdr_void;
    209   1.1   thorpej 		local = ypproc_clear_2_svc;
    210   1.8   thorpej 		SVCNAME("clear_2");
    211   1.1   thorpej 		break;
    212   1.1   thorpej 
    213   1.1   thorpej 	case YPPROC_ALL:
    214  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypreq_nokey;
    215  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_all;
    216   1.1   thorpej 		local = ypproc_all_2_svc;
    217   1.8   thorpej 		SVCNAME("all_2");
    218   1.1   thorpej 		break;
    219   1.1   thorpej 
    220   1.1   thorpej 	case YPPROC_MASTER:
    221  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypreq_nokey;
    222  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_master;
    223   1.1   thorpej 		local = ypproc_master_2_svc;
    224   1.8   thorpej 		SVCNAME("master_2");
    225   1.1   thorpej 		break;
    226   1.1   thorpej 
    227   1.1   thorpej 	case YPPROC_ORDER:
    228  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypreq_nokey;
    229  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_order;
    230   1.1   thorpej 		local = ypproc_order_2_svc;
    231   1.8   thorpej 		SVCNAME("order_2");
    232   1.1   thorpej 		break;
    233   1.1   thorpej 
    234   1.1   thorpej 	case YPPROC_MAPLIST:
    235  1.25    plunky 		xdr_argument = (xdrproc_t)xdr_ypdomain_wrap_string;
    236  1.25    plunky 		xdr_result = (xdrproc_t)xdr_ypresp_maplist;
    237   1.1   thorpej 		local = ypproc_maplist_2_svc;
    238   1.8   thorpej 		SVCNAME("maplist_2");
    239   1.1   thorpej 		break;
    240   1.1   thorpej 
    241   1.1   thorpej 	default:
    242   1.1   thorpej 		svcerr_noproc(transp);
    243   1.1   thorpej 		return;
    244   1.1   thorpej 	}
    245   1.8   thorpej 
    246   1.8   thorpej #ifdef LIBWRAP
    247   1.8   thorpej 	clientstr = eval_client(&req);
    248   1.8   thorpej 
    249   1.8   thorpej 	if (hosts_access(&req) == 0) {
    250   1.8   thorpej 		syslog(deny_severity,
    251   1.8   thorpej 		    "%s: refused request from %.500s", svcname, clientstr);
    252   1.8   thorpej 		svcerr_auth(transp, AUTH_FAILED);
    253   1.8   thorpej 		return;
    254   1.8   thorpej 	}
    255   1.8   thorpej #endif
    256   1.8   thorpej 
    257  1.20  christos 	(void)memset(&argument, 0, sizeof (argument));
    258  1.20  christos 	if (!svc_getargs(transp, xdr_argument, argp)) {
    259   1.1   thorpej 		svcerr_decode(transp);
    260   1.1   thorpej 		return;
    261   1.1   thorpej 	}
    262   1.1   thorpej 	result = (*local)(&argument, rqstp);
    263   1.1   thorpej 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    264   1.1   thorpej 		svcerr_systemerr(transp);
    265   1.1   thorpej 	}
    266  1.20  christos 	if (!svc_freeargs(transp, xdr_argument, argp)) {
    267  1.12   thorpej 		_msgout(LOG_ERR, "unable to free arguments");
    268   1.1   thorpej 		exit(1);
    269   1.1   thorpej 	}
    270   1.1   thorpej 	return;
    271   1.1   thorpej }
    272   1.1   thorpej 
    273   1.4  christos /*
    274   1.4  christos  * limited NIS version 1 support: the null, domain, and domain_nonack
    275   1.4  christos  * request/reply format is identical between v1 and v2.  SunOS4's ypbind
    276   1.4  christos  * makes v1 domain_nonack calls.
    277   1.4  christos  */
    278   1.4  christos static void
    279   1.6     lukem ypprog_1(struct svc_req *rqstp, SVCXPRT *transp)
    280   1.4  christos {
    281   1.4  christos 	switch (rqstp->rq_proc) {
    282   1.4  christos 	case YPPROC_NULL:
    283   1.4  christos 	case YPPROC_DOMAIN:
    284   1.4  christos 	case YPPROC_DOMAIN_NONACK:
    285   1.4  christos 		ypprog_2(rqstp, transp);
    286   1.4  christos 		return;
    287   1.4  christos 
    288   1.4  christos 	default:
    289   1.4  christos 		svcerr_noproc(transp);
    290   1.4  christos 		return;
    291   1.4  christos 	}
    292   1.4  christos }
    293   1.4  christos 
    294   1.1   thorpej int
    295  1.18       wiz main(int argc, char *argv[])
    296   1.1   thorpej {
    297  1.20  christos 	SVCXPRT *xprt;
    298  1.20  christos 	struct netconfig *cfg = NULL;
    299  1.20  christos 	int s;
    300   1.5   thorpej 	struct sigaction sa;
    301  1.20  christos 	struct bindsock *bs;
    302  1.20  christos 	in_port_t port = 0;
    303  1.12   thorpej 	int ch, xcreated = 0, one = 1;
    304   1.5   thorpej 
    305  1.19  christos 	setprogname(argv[0]);
    306  1.19  christos 
    307   1.8   thorpej #ifdef LIBWRAP
    308  1.20  christos #define	GETOPTSTR	"dflp:"
    309   1.8   thorpej #else
    310  1.20  christos #define	GETOPTSTR	"dfp:"
    311   1.8   thorpej #endif
    312   1.8   thorpej 	while ((ch = getopt(argc, argv, GETOPTSTR)) != -1) {
    313   1.1   thorpej 		switch (ch) {
    314   1.1   thorpej 		case 'd':
    315   1.1   thorpej 			usedns = 1;
    316   1.1   thorpej 			break;
    317   1.9       abs 		case 'f':
    318   1.9       abs 			foreground = 1;
    319   1.9       abs 			break;
    320  1.20  christos 		case 'p':
    321  1.20  christos 			port = atoi(optarg);
    322  1.20  christos 			break;
    323   1.8   thorpej #ifdef LIBWRAP
    324   1.8   thorpej 		case 'l':
    325   1.8   thorpej 			lflag = 1;
    326   1.1   thorpej 			break;
    327   1.8   thorpej #endif
    328   1.1   thorpej 		default:
    329   1.1   thorpej 			usage();
    330   1.1   thorpej 		}
    331   1.1   thorpej 	}
    332   1.1   thorpej 
    333   1.8   thorpej #undef GETOPTSTR
    334   1.8   thorpej 
    335   1.1   thorpej 	/* This program must be run by root. */
    336   1.1   thorpej 	if (geteuid() != 0)
    337   1.1   thorpej 		errx(1, "must run as root");
    338   1.1   thorpej 
    339  1.12   thorpej 	if (foreground == 0 && daemon(0, 0))
    340   1.3     mikel 		err(1, "can't detach");
    341   1.3     mikel 
    342  1.13     lukem 	openlog("ypserv", LOG_PID, LOG_DAEMON);
    343   1.8   thorpej 	syslog(LOG_INFO, "starting");
    344  1.20  christos 	(void)pidfile(NULL);
    345   1.1   thorpej 
    346  1.20  christos 	(void) rpcb_unset((u_int)YPPROG, (u_int)YPVERS, NULL);
    347  1.20  christos 	(void) rpcb_unset((u_int)YPPROG, (u_int)YPVERS_ORIG, NULL);
    348  1.12   thorpej 
    349   1.1   thorpej 
    350   1.1   thorpej 	ypdb_init();	/* init db stuff */
    351   1.1   thorpej 
    352  1.19  christos 	sa.sa_handler = SIG_IGN;
    353  1.19  christos 	sa.sa_flags = SA_NOCLDWAIT;
    354   1.8   thorpej 	if (sigemptyset(&sa.sa_mask)) {
    355  1.20  christos 		_msgout(LOG_ERR, "sigemptyset: %s", strerror(errno));
    356   1.8   thorpej 		exit(1);
    357   1.8   thorpej 	}
    358   1.8   thorpej 	if (sigaction(SIGCHLD, &sa, NULL)) {
    359  1.20  christos 		_msgout(LOG_ERR, "sigaction: %s", strerror(errno));
    360   1.8   thorpej 		exit(1);
    361   1.8   thorpej 	}
    362   1.1   thorpej 
    363  1.20  christos 	for (bs = socklist;
    364  1.20  christos 	    bs < &socklist[sizeof(socklist) / sizeof(socklist[0])]; bs++) {
    365  1.20  christos 
    366  1.20  christos 		if ((s = socket(bs->family, bs->type, bs->proto)) == -1)
    367  1.20  christos 			continue;
    368  1.20  christos 
    369  1.20  christos 		if (bs->family == AF_INET6) {
    370  1.20  christos 			/*
    371  1.20  christos 			 * We're doing host-based access checks here, so don't
    372  1.20  christos 			 * allow v4-in-v6 to confuse things.
    373  1.20  christos 			 */
    374  1.20  christos 			if (setsockopt(s, IPPROTO_IPV6,
    375  1.20  christos 			    IPV6_V6ONLY, &one, sizeof(one)) == -1) {
    376  1.20  christos 				_msgout(LOG_ERR,
    377  1.20  christos 				    "can't disable v4-in-v6 on %s socket",
    378  1.20  christos 				    bs->name);
    379  1.20  christos 				exit(1);
    380  1.20  christos 			}
    381  1.20  christos 		}
    382  1.20  christos 
    383  1.20  christos 		if ((cfg = getnetconfigent(bs->name)) == NULL) {
    384  1.20  christos 			_msgout(LOG_ERR,
    385  1.20  christos 			    "unable to get network configuration for %s port",
    386  1.20  christos 			    bs->name);
    387  1.20  christos 			goto out;
    388  1.20  christos 		}
    389  1.20  christos 
    390  1.20  christos 		if (bind_resv_port(s, bs->family, port) != 0)
    391  1.20  christos 			goto out;
    392  1.20  christos 
    393  1.20  christos 		if (bs->type == SOCK_STREAM) {
    394  1.20  christos 			(void)listen(s, SOMAXCONN);
    395  1.20  christos 			xprt = svc_vc_create(s, 0, 0);
    396  1.20  christos 		} else {
    397  1.20  christos 			xprt = svc_dg_create(s, 0, 0);
    398  1.20  christos 		}
    399  1.20  christos 
    400  1.20  christos 		if (xprt == NULL) {
    401  1.20  christos 			_msgout(LOG_WARNING, "unable to create %s service",
    402  1.20  christos 			    bs->name);
    403  1.20  christos 			goto out;
    404  1.20  christos 		}
    405  1.20  christos 		if (svc_reg(xprt, (u_int)YPPROG, (u_int)YPVERS_ORIG, ypprog_1,
    406  1.20  christos 		    cfg) == 0 ||
    407  1.20  christos 		    svc_reg(xprt, (u_int)YPPROG, (u_int)YPVERS, ypprog_2,
    408  1.20  christos 		    cfg) == 0) {
    409  1.20  christos 			_msgout(LOG_WARNING, "unable to register %s service",
    410  1.20  christos 			    bs->name);
    411  1.20  christos 			goto out;
    412  1.20  christos 		}
    413  1.20  christos 		xcreated++;
    414  1.20  christos 		freenetconfigent(cfg);
    415  1.20  christos 		continue;
    416  1.20  christos out:
    417  1.20  christos 		if (s != -1)
    418  1.20  christos 			(void)close(s);
    419  1.20  christos 		if (cfg) {
    420  1.20  christos 			freenetconfigent(cfg);
    421  1.20  christos 			cfg = NULL;
    422  1.20  christos 		}
    423   1.1   thorpej 	}
    424   1.1   thorpej 
    425  1.12   thorpej 	if (xcreated == 0) {
    426  1.12   thorpej 		_msgout(LOG_ERR, "unable to create any services");
    427  1.12   thorpej 		exit(1);
    428   1.1   thorpej 	}
    429   1.1   thorpej 
    430   1.1   thorpej 	svc_run();
    431  1.12   thorpej 	_msgout(LOG_ERR, "svc_run returned");
    432   1.1   thorpej 	exit(1);
    433   1.1   thorpej 	/* NOTREACHED */
    434   1.1   thorpej }
    435   1.1   thorpej 
    436  1.20  christos static void
    437  1.18       wiz usage(void)
    438   1.1   thorpej {
    439   1.1   thorpej 
    440   1.8   thorpej #ifdef LIBWRAP
    441  1.24       wiz #define	USAGESTR	"Usage: %s [-dfl] [-p <port>]\n"
    442   1.8   thorpej #else
    443  1.24       wiz #define	USAGESTR	"Usage: %s [-df] [-p <port>]\n"
    444   1.8   thorpej #endif
    445   1.8   thorpej 
    446  1.20  christos 	(void)fprintf(stderr, USAGESTR, getprogname());
    447   1.1   thorpej 	exit(1);
    448   1.8   thorpej 
    449   1.8   thorpej #undef USAGESTR
    450   1.7     lukem }
    451   1.7     lukem 
    452   1.7     lukem /*
    453   1.7     lukem  * _yp_invalid_map: check if given map name isn't legal.
    454   1.7     lukem  * returns non-zero if invalid
    455   1.7     lukem  *
    456   1.7     lukem  * XXX: this probably should be in libc/yp/yplib.c
    457   1.7     lukem  */
    458   1.7     lukem int
    459  1.18       wiz _yp_invalid_map(const char *map)
    460   1.7     lukem {
    461   1.7     lukem 	if (map == NULL || *map == '\0')
    462   1.7     lukem 		return 1;
    463   1.7     lukem 
    464   1.7     lukem 	if (strlen(map) > YPMAXMAP)
    465   1.7     lukem 		return 1;
    466   1.7     lukem 
    467   1.7     lukem 	if (strchr(map, '/') != NULL)
    468   1.7     lukem 		return 1;
    469   1.7     lukem 
    470   1.7     lukem 	return 0;
    471   1.1   thorpej }
    472  1.20  christos 
    473  1.20  christos static int
    474  1.20  christos bind_resv_port(int sock, sa_family_t family, in_port_t port)
    475  1.20  christos {
    476  1.20  christos 	struct sockaddr *sa;
    477  1.20  christos 	struct sockaddr_in sasin;
    478  1.20  christos 	struct sockaddr_in6 sasin6;
    479  1.20  christos 
    480  1.20  christos 	switch (family) {
    481  1.20  christos 	case AF_INET:
    482  1.20  christos 		(void)memset(&sasin, 0, sizeof(sasin));
    483  1.20  christos 		sasin.sin_len = sizeof(sasin);
    484  1.20  christos 		sasin.sin_family = family;
    485  1.20  christos 		sasin.sin_port = htons(port);
    486  1.20  christos 		sa = (struct sockaddr *)(void *)&sasin;
    487  1.20  christos 		break;
    488  1.20  christos 	case AF_INET6:
    489  1.20  christos 		(void)memset(&sasin6, 0, sizeof(sasin6));
    490  1.20  christos 		sasin6.sin6_len = sizeof(sasin6);
    491  1.20  christos 		sasin6.sin6_family = family;
    492  1.20  christos 		sasin6.sin6_port = htons(port);
    493  1.20  christos 		sa = (struct sockaddr *)(void *)&sasin6;
    494  1.20  christos 		break;
    495  1.20  christos 	default:
    496  1.20  christos 		_msgout(LOG_ERR, "Unsupported address family %d", family);
    497  1.20  christos 		return -1;
    498  1.20  christos 	}
    499  1.20  christos 	if (bindresvport_sa(sock, sa) == -1) {
    500  1.20  christos 		_msgout(LOG_ERR, "Cannot bind to reserved port %d (%s)", port,
    501  1.20  christos 		    strerror(errno));
    502  1.20  christos 		return -1;
    503  1.20  christos 	}
    504  1.20  christos 	return 0;
    505  1.20  christos }
    506