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