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