Home | History | Annotate | Line # | Download | only in ypbind
ypbind.c revision 1.83
      1 /*	$NetBSD: ypbind.c,v 1.83 2011/05/24 07:01:24 dholland Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef LINT
     31 __RCSID("$NetBSD: ypbind.c,v 1.83 2011/05/24 07:01:24 dholland Exp $");
     32 #endif
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/file.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/signal.h>
     39 #include <sys/socket.h>
     40 #include <sys/stat.h>
     41 #include <sys/syslog.h>
     42 #include <sys/uio.h>
     43 #include <arpa/inet.h>
     44 #include <net/if.h>
     45 #include <ctype.h>
     46 #include <dirent.h>
     47 #include <err.h>
     48 #include <errno.h>
     49 #include <fcntl.h>
     50 #include <ifaddrs.h>
     51 #include <limits.h>
     52 #include <netdb.h>
     53 #include <stdarg.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <syslog.h>
     58 #include <unistd.h>
     59 #include <util.h>
     60 
     61 #include <rpc/rpc.h>
     62 #include <rpc/xdr.h>
     63 #include <rpc/pmap_clnt.h>
     64 #include <rpc/pmap_prot.h>
     65 #include <rpc/pmap_rmt.h>
     66 #include <rpcsvc/yp_prot.h>
     67 #include <rpcsvc/ypclnt.h>
     68 
     69 #include "pathnames.h"
     70 
     71 #define YPSERVERSSUFF	".ypservers"
     72 #define BINDINGDIR	(_PATH_VAR_YP "binding")
     73 
     74 #ifndef O_SHLOCK
     75 #define O_SHLOCK 0
     76 #endif
     77 
     78 int _yp_invalid_domain(const char *);		/* XXX libc internal */
     79 
     80 ////////////////////////////////////////////////////////////
     81 // types and globals
     82 
     83 typedef enum {
     84 	YPBIND_DIRECT, YPBIND_BROADCAST,
     85 } ypbind_mode_t;
     86 
     87 struct domain {
     88 	struct domain *dom_next;
     89 
     90 	char dom_name[YPMAXDOMAIN + 1];
     91 	struct sockaddr_in dom_server_addr;
     92 	int dom_socket;
     93 	CLIENT *dom_client;
     94 	long dom_vers;
     95 	time_t dom_checktime;
     96 	time_t dom_asktime;
     97 	int dom_lockfd;
     98 	int dom_alive;
     99 	uint32_t dom_xid;
    100 	ypbind_mode_t dom_ypbindmode;	/* broadcast or direct */
    101 	int dom_been_ypset;		/* ypset been done on this domain? */
    102 };
    103 
    104 #define BUFSIZE		1400
    105 
    106 static char *domainname;
    107 
    108 static struct domain *domains;
    109 static int check;
    110 
    111 static ypbind_mode_t default_ypbindmode;
    112 
    113 static int allow_local_ypset = 0, allow_any_ypset = 0;
    114 static int insecure;
    115 static int rpcsock, pingsock;
    116 static struct rmtcallargs rmtca;
    117 static struct rmtcallres rmtcr;
    118 static bool_t rmtcr_outval;
    119 static unsigned long rmtcr_port;
    120 static SVCXPRT *udptransp, *tcptransp;
    121 
    122 ////////////////////////////////////////////////////////////
    123 // utilities
    124 
    125 static int
    126 open_locked(const char *path, int flags, mode_t mode)
    127 {
    128 	int fd;
    129 
    130 	fd = open(path, flags|O_SHLOCK, mode);
    131 	if (fd < 0) {
    132 		return -1;
    133 	}
    134 #if O_SHLOCK == 0
    135 	/* dholland 20110522 wouldn't it be better to check this for error? */
    136 	(void)flock(fd, LOCK_SH);
    137 #endif
    138 	return fd;
    139 }
    140 
    141 ////////////////////////////////////////////////////////////
    142 // logging
    143 
    144 #ifdef DEBUG
    145 #define DPRINTF(...) (debug ? (void)printf(__VA_ARGS__) : (void)0)
    146 static int debug;
    147 #else
    148 #define DPRINTF(...)
    149 #endif
    150 
    151 static void yp_log(int, const char *, ...) __printflike(2, 3);
    152 
    153 static void
    154 yp_log(int pri, const char *fmt, ...)
    155 {
    156 	va_list ap;
    157 
    158 	va_start(ap, fmt);
    159 
    160 #if defined(DEBUG)
    161 	if (debug) {
    162 		(void)vprintf(fmt, ap);
    163 		(void)printf("\n");
    164 	} else
    165 #endif
    166 		vsyslog(pri, fmt, ap);
    167 	va_end(ap);
    168 }
    169 
    170 ////////////////////////////////////////////////////////////
    171 // ypservers file
    172 
    173 /*
    174  * Get pathname for the ypservers file for a given domain
    175  * (/var/yp/binding/DOMAIN.ypservers)
    176  */
    177 static const char *
    178 ypservers_filename(const char *domain)
    179 {
    180 	static char ret[PATH_MAX];
    181 
    182 	(void)snprintf(ret, sizeof(ret), "%s/%s%s",
    183 			BINDINGDIR, domain, YPSERVERSSUFF);
    184 	return ret;
    185 }
    186 
    187 ////////////////////////////////////////////////////////////
    188 // struct domain
    189 
    190 static struct domain *
    191 domain_find(uint32_t xid)
    192 {
    193 	struct domain *dom;
    194 
    195 	for (dom = domains; dom != NULL; dom = dom->dom_next)
    196 		if (dom->dom_xid == xid)
    197 			break;
    198 	return dom;
    199 }
    200 
    201 static uint32_t
    202 unique_xid(struct domain *dom)
    203 {
    204 	uint32_t tmp_xid;
    205 
    206 	tmp_xid = ((uint32_t)(unsigned long)dom) & 0xffffffff;
    207 	while (domain_find(tmp_xid) != NULL)
    208 		tmp_xid++;
    209 
    210 	return tmp_xid;
    211 }
    212 
    213 static struct domain *
    214 domain_create(const char *name)
    215 {
    216 	struct domain *dom;
    217 	const char *pathname;
    218 	struct stat st;
    219 
    220 	dom = malloc(sizeof *dom);
    221 	if (dom == NULL) {
    222 		yp_log(LOG_ERR, "domain_create: Out of memory");
    223 		exit(1);
    224 	}
    225 
    226 	dom->dom_next = NULL;
    227 
    228 	(void)strlcpy(dom->dom_name, name, sizeof(dom->dom_name));
    229 	(void)memset(&dom->dom_server_addr, 0, sizeof(dom->dom_server_addr));
    230 	dom->dom_socket = -1;
    231 	dom->dom_client = NULL;
    232 	dom->dom_vers = YPVERS;
    233 	dom->dom_checktime = 0;
    234 	dom->dom_asktime = 0;
    235 	dom->dom_lockfd = -1;
    236 	dom->dom_alive = 0;
    237 	dom->dom_xid = unique_xid(dom);
    238 
    239 	/*
    240 	 * Per traditional ypbind(8) semantics, if a ypservers
    241 	 * file does not exist, we revert to broadcast mode.
    242 	 *
    243 	 * The sysadmin can force broadcast mode by passing the
    244 	 * -broadcast flag. There is currently no way to fail and
    245 	 * reject domains for which there is no ypservers file.
    246 	 */
    247 	dom->dom_ypbindmode = default_ypbindmode;
    248 	if (dom->dom_ypbindmode == YPBIND_DIRECT) {
    249 		pathname = ypservers_filename(domainname);
    250 		if (stat(pathname, &st) < 0) {
    251 			/* XXX syslog a warning here? */
    252 			DPRINTF("%s does not exist, defaulting to broadcast\n",
    253 				pathname);
    254 			dom->dom_ypbindmode = YPBIND_BROADCAST;
    255 		}
    256 	}
    257 
    258 	/* add to global list */
    259 	dom->dom_next = domains;
    260 	domains = dom;
    261 
    262 	return dom;
    263 }
    264 
    265 ////////////////////////////////////////////////////////////
    266 // locks
    267 
    268 static int
    269 makelock(struct domain *dom)
    270 {
    271 	int fd;
    272 	char path[MAXPATHLEN];
    273 
    274 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    275 	    dom->dom_name, dom->dom_vers);
    276 
    277 	fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
    278 	if (fd == -1) {
    279 		(void)mkdir(BINDINGDIR, 0755);
    280 		fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
    281 		if (fd == -1) {
    282 			return -1;
    283 		}
    284 	}
    285 
    286 	return fd;
    287 }
    288 
    289 static void
    290 removelock(struct domain *dom)
    291 {
    292 	char path[MAXPATHLEN];
    293 
    294 	(void)snprintf(path, sizeof(path), "%s/%s.%ld",
    295 	    BINDINGDIR, dom->dom_name, dom->dom_vers);
    296 	(void)unlink(path);
    297 }
    298 
    299 /*
    300  * purge_bindingdir: remove old binding files (i.e. "rm BINDINGDIR\/\*.[0-9]")
    301  *
    302  * local YP functions [e.g. yp_master()] will fail without even talking
    303  * to ypbind if there is a stale (non-flock'd) binding file present.
    304  * we have to scan the entire BINDINGDIR for binding files, because
    305  * ypbind may bind more than just the yp_get_default_domain() domain.
    306  */
    307 static int
    308 purge_bindingdir(const char *dirpath)
    309 {
    310 	DIR *dirp;
    311 	int unlinkedfiles, l;
    312 	struct dirent *dp;
    313 	char pathname[MAXPATHLEN];
    314 
    315 	if ((dirp = opendir(dirpath)) == NULL)
    316 		return(-1);   /* at this point, shouldn't ever happen */
    317 
    318 	do {
    319 		unlinkedfiles = 0;
    320 		while ((dp = readdir(dirp)) != NULL) {
    321 			l = dp->d_namlen;
    322 			/* 'rm *.[0-9]' */
    323 			if (l > 2 && dp->d_name[l-2] == '.' &&
    324 			    dp->d_name[l-1] >= '0' && dp->d_name[l-1] <= '9') {
    325 				(void)snprintf(pathname, sizeof(pathname),
    326 					"%s/%s", dirpath, dp->d_name);
    327 				if (unlink(pathname) < 0 && errno != ENOENT)
    328 					return(-1);
    329 				unlinkedfiles++;
    330 			}
    331 		}
    332 
    333 		/* rescan dir if we removed it */
    334 		if (unlinkedfiles)
    335 			rewinddir(dirp);
    336 
    337 	} while (unlinkedfiles);
    338 
    339 	closedir(dirp);
    340 	return(0);
    341 }
    342 
    343 ////////////////////////////////////////////////////////////
    344 // sunrpc twaddle
    345 
    346 /*
    347  * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
    348  */
    349 static void
    350 rpc_received(char *dom_name, struct sockaddr_in *raddrp, int force,
    351 	     int is_ypset)
    352 {
    353 	struct domain *dom;
    354 	struct iovec iov[2];
    355 	struct ypbind_resp ybr;
    356 	ssize_t result;
    357 	int fd;
    358 
    359 	DPRINTF("returned from %s about %s\n",
    360 		inet_ntoa(raddrp->sin_addr), dom_name);
    361 
    362 	if (dom_name == NULL)
    363 		return;
    364 
    365 	if (_yp_invalid_domain(dom_name))
    366 		return;
    367 
    368 		/* don't support insecure servers by default */
    369 	if (!insecure && ntohs(raddrp->sin_port) >= IPPORT_RESERVED)
    370 		return;
    371 
    372 	for (dom = domains; dom != NULL; dom = dom->dom_next)
    373 		if (!strcmp(dom->dom_name, dom_name))
    374 			break;
    375 
    376 	if (dom == NULL) {
    377 		if (force == 0)
    378 			return;
    379 		dom = domain_create(dom_name);
    380 	}
    381 
    382 	if (is_ypset) {
    383 		dom->dom_been_ypset = 1;
    384 	}
    385 
    386 	/* soft update, alive */
    387 	if (dom->dom_alive == 1 && force == 0) {
    388 		if (!memcmp(&dom->dom_server_addr, raddrp,
    389 			    sizeof(dom->dom_server_addr))) {
    390 			dom->dom_alive = 1;
    391 			/* recheck binding in 60 sec */
    392 			dom->dom_checktime = time(NULL) + 60;
    393 		}
    394 		return;
    395 	}
    396 
    397 	(void)memcpy(&dom->dom_server_addr, raddrp,
    398 	    sizeof(dom->dom_server_addr));
    399 	/* recheck binding in 60 seconds */
    400 	dom->dom_checktime = time(NULL) + 60;
    401 	dom->dom_vers = YPVERS;
    402 	dom->dom_alive = 1;
    403 
    404 	if (dom->dom_lockfd != -1)
    405 		(void)close(dom->dom_lockfd);
    406 
    407 	if ((fd = makelock(dom)) == -1)
    408 		return;
    409 
    410 	/*
    411 	 * ok, if BINDINGDIR exists, and we can create the binding file,
    412 	 * then write to it..
    413 	 */
    414 	dom->dom_lockfd = fd;
    415 
    416 	iov[0].iov_base = &(udptransp->xp_port);
    417 	iov[0].iov_len = sizeof udptransp->xp_port;
    418 	iov[1].iov_base = &ybr;
    419 	iov[1].iov_len = sizeof ybr;
    420 
    421 	(void)memset(&ybr, 0, sizeof ybr);
    422 	ybr.ypbind_status = YPBIND_SUCC_VAL;
    423 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
    424 	    raddrp->sin_addr;
    425 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    426 	    raddrp->sin_port;
    427 
    428 	result = writev(dom->dom_lockfd, iov, 2);
    429 	if (result < 0 || (size_t)result != iov[0].iov_len + iov[1].iov_len) {
    430 		if (result < 0)
    431 			yp_log(LOG_WARNING, "writev: %s", strerror(errno));
    432 		else
    433 			yp_log(LOG_WARNING, "writev: short count");
    434 		(void)close(dom->dom_lockfd);
    435 		removelock(dom);
    436 		dom->dom_lockfd = -1;
    437 	}
    438 }
    439 
    440 static void *
    441 /*ARGSUSED*/
    442 ypbindproc_null_2(SVCXPRT *transp, void *argp)
    443 {
    444 	static char res;
    445 
    446 	DPRINTF("ypbindproc_null_2\n");
    447 	(void)memset(&res, 0, sizeof(res));
    448 	return (void *)&res;
    449 }
    450 
    451 static void *
    452 /*ARGSUSED*/
    453 ypbindproc_domain_2(SVCXPRT *transp, void *argp)
    454 {
    455 	static struct ypbind_resp res;
    456 	struct domain *dom;
    457 	char *arg = *(char **) argp;
    458 	time_t now;
    459 	int count;
    460 
    461 	DPRINTF("ypbindproc_domain_2 %s\n", arg);
    462 	if (_yp_invalid_domain(arg))
    463 		return NULL;
    464 
    465 	(void)memset(&res, 0, sizeof res);
    466 	res.ypbind_status = YPBIND_FAIL_VAL;
    467 
    468 	for (count = 0, dom = domains;
    469 	    dom != NULL;
    470 	    dom = dom->dom_next, count++) {
    471 		if (count > 100)
    472 			return NULL;		/* prevent denial of service */
    473 		if (!strcmp(dom->dom_name, arg))
    474 			break;
    475 	}
    476 
    477 	if (dom == NULL) {
    478 		dom = domain_create(arg);
    479 		removelock(dom);
    480 		check++;
    481 		DPRINTF("unknown domain %s\n", arg);
    482 		return NULL;
    483 	}
    484 
    485 	if (dom->dom_alive == 0) {
    486 		DPRINTF("dead domain %s\n", arg);
    487 		return NULL;
    488 	}
    489 
    490 #ifdef HEURISTIC
    491 	(void)time(&now);
    492 	if (now < dom->dom_asktime + 5) {
    493 		/*
    494 		 * Hmm. More than 2 requests in 5 seconds have indicated
    495 		 * that my binding is possibly incorrect.
    496 		 * Ok, do an immediate poll of the server.
    497 		 */
    498 		if (dom->dom_checktime >= now) {
    499 			/* don't flood it */
    500 			dom->dom_checktime = 0;
    501 			check++;
    502 		}
    503 	}
    504 	dom->dom_asktime = now;
    505 #endif
    506 
    507 	res.ypbind_status = YPBIND_SUCC_VAL;
    508 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
    509 		dom->dom_server_addr.sin_addr.s_addr;
    510 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    511 		dom->dom_server_addr.sin_port;
    512 	DPRINTF("domain %s at %s/%d\n", dom->dom_name,
    513 		inet_ntoa(dom->dom_server_addr.sin_addr),
    514 		ntohs(dom->dom_server_addr.sin_port));
    515 	return &res;
    516 }
    517 
    518 static void *
    519 ypbindproc_setdom_2(SVCXPRT *transp, void *argp)
    520 {
    521 	struct ypbind_setdom *sd = argp;
    522 	struct sockaddr_in *fromsin, bindsin;
    523 	static bool_t res;
    524 
    525 	DPRINTF("ypbindproc_setdom_2 %s\n", inet_ntoa(bindsin.sin_addr));
    526 	(void)memset(&res, 0, sizeof(res));
    527 	fromsin = svc_getcaller(transp);
    528 
    529 	if (allow_any_ypset) {
    530 		/* nothing */
    531 	} else if (allow_local_ypset) {
    532 		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
    533 			DPRINTF("ypset denied from %s\n",
    534 				inet_ntoa(fromsin->sin_addr));
    535 			return NULL;
    536 		}
    537 	} else {
    538 		DPRINTF("ypset denied\n");
    539 		return NULL;
    540 	}
    541 
    542 	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
    543 		DPRINTF("ypset from unprivileged port denied\n");
    544 		return &res;
    545 	}
    546 
    547 	if (sd->ypsetdom_vers != YPVERS) {
    548 		DPRINTF("ypset with wrong version denied\n");
    549 		return &res;
    550 	}
    551 
    552 	(void)memset(&bindsin, 0, sizeof bindsin);
    553 	bindsin.sin_family = AF_INET;
    554 	bindsin.sin_len = sizeof(bindsin);
    555 	bindsin.sin_addr = sd->ypsetdom_addr;
    556 	bindsin.sin_port = sd->ypsetdom_port;
    557 	rpc_received(sd->ypsetdom_domain, &bindsin, 1, 1);
    558 
    559 	DPRINTF("ypset to %s succeeded\n", inet_ntoa(bindsin.sin_addr));
    560 	res = 1;
    561 	return &res;
    562 }
    563 
    564 static void
    565 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
    566 {
    567 	union {
    568 		char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
    569 		struct ypbind_setdom ypbindproc_setdom_2_arg;
    570 		void *alignment;
    571 	} argument;
    572 	struct authunix_parms *creds;
    573 	char *result;
    574 	xdrproc_t xdr_argument, xdr_result;
    575 	void *(*local)(SVCXPRT *, void *);
    576 
    577 	switch (rqstp->rq_proc) {
    578 	case YPBINDPROC_NULL:
    579 		xdr_argument = xdr_void;
    580 		xdr_result = xdr_void;
    581 		local = ypbindproc_null_2;
    582 		break;
    583 
    584 	case YPBINDPROC_DOMAIN:
    585 		xdr_argument = xdr_ypdomain_wrap_string;
    586 		xdr_result = xdr_ypbind_resp;
    587 		local = ypbindproc_domain_2;
    588 		break;
    589 
    590 	case YPBINDPROC_SETDOM:
    591 		switch (rqstp->rq_cred.oa_flavor) {
    592 		case AUTH_UNIX:
    593 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
    594 			if (creds->aup_uid != 0) {
    595 				svcerr_auth(transp, AUTH_BADCRED);
    596 				return;
    597 			}
    598 			break;
    599 		default:
    600 			svcerr_auth(transp, AUTH_TOOWEAK);
    601 			return;
    602 		}
    603 
    604 		xdr_argument = xdr_ypbind_setdom;
    605 		xdr_result = xdr_void;
    606 		local = ypbindproc_setdom_2;
    607 		break;
    608 
    609 	default:
    610 		svcerr_noproc(transp);
    611 		return;
    612 	}
    613 	(void)memset(&argument, 0, sizeof(argument));
    614 	if (!svc_getargs(transp, xdr_argument, (caddr_t)(void *)&argument)) {
    615 		svcerr_decode(transp);
    616 		return;
    617 	}
    618 	result = (*local)(transp, &argument);
    619 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    620 		svcerr_systemerr(transp);
    621 	}
    622 	return;
    623 }
    624 
    625 static void
    626 sunrpc_setup(void)
    627 {
    628 	int one;
    629 
    630 	(void)pmap_unset(YPBINDPROG, YPBINDVERS);
    631 
    632 	udptransp = svcudp_create(RPC_ANYSOCK);
    633 	if (udptransp == NULL)
    634 		errx(1, "Cannot create udp service.");
    635 
    636 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    637 	    IPPROTO_UDP))
    638 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
    639 
    640 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
    641 	if (tcptransp == NULL)
    642 		errx(1, "Cannot create tcp service.");
    643 
    644 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    645 	    IPPROTO_TCP))
    646 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
    647 
    648 	/* XXX use SOCK_STREAM for direct queries? */
    649 	if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    650 		err(1, "rpc socket");
    651 	if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    652 		err(1, "ping socket");
    653 
    654 	(void)fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    655 	(void)fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
    656 
    657 	one = 1;
    658 	(void)setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one,
    659 	    (socklen_t)sizeof(one));
    660 	rmtca.prog = YPPROG;
    661 	rmtca.vers = YPVERS;
    662 	rmtca.proc = YPPROC_DOMAIN_NONACK;
    663 	rmtca.xdr_args = NULL;		/* set at call time */
    664 	rmtca.args_ptr = NULL;		/* set at call time */
    665 	rmtcr.port_ptr = &rmtcr_port;
    666 	rmtcr.xdr_results = xdr_bool;
    667 	rmtcr.results_ptr = (caddr_t)(void *)&rmtcr_outval;
    668 }
    669 
    670 ////////////////////////////////////////////////////////////
    671 // operational logic
    672 
    673 static int
    674 broadcast(char *buf, int outlen)
    675 {
    676 	struct ifaddrs *ifap, *ifa;
    677 	struct sockaddr_in bindsin;
    678 	struct in_addr in;
    679 
    680 	(void)memset(&bindsin, 0, sizeof bindsin);
    681 	bindsin.sin_family = AF_INET;
    682 	bindsin.sin_len = sizeof(bindsin);
    683 	bindsin.sin_port = htons(PMAPPORT);
    684 
    685 	if (getifaddrs(&ifap) != 0) {
    686 		yp_log(LOG_WARNING, "broadcast: getifaddrs: %s",
    687 		       strerror(errno));
    688 		return (-1);
    689 	}
    690 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    691 		if (ifa->ifa_addr->sa_family != AF_INET)
    692 			continue;
    693 		if ((ifa->ifa_flags & IFF_UP) == 0)
    694 			continue;
    695 
    696 		switch (ifa->ifa_flags & (IFF_LOOPBACK | IFF_BROADCAST)) {
    697 		case IFF_BROADCAST:
    698 			if (!ifa->ifa_broadaddr)
    699 				continue;
    700 			if (ifa->ifa_broadaddr->sa_family != AF_INET)
    701 				continue;
    702 			in = ((struct sockaddr_in *)(void *)ifa->ifa_broadaddr)->sin_addr;
    703 			break;
    704 		case IFF_LOOPBACK:
    705 			in = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr;
    706 			break;
    707 		default:
    708 			continue;
    709 		}
    710 
    711 		bindsin.sin_addr = in;
    712 		DPRINTF("broadcast %x\n", bindsin.sin_addr.s_addr);
    713 		if (sendto(rpcsock, buf, outlen, 0,
    714 		    (struct sockaddr *)(void *)&bindsin,
    715 		    (socklen_t)bindsin.sin_len) == -1)
    716 			yp_log(LOG_WARNING, "broadcast: sendto: %s",
    717 			       strerror(errno));
    718 	}
    719 	freeifaddrs(ifap);
    720 	return (0);
    721 }
    722 
    723 static int
    724 direct(char *buf, int outlen)
    725 {
    726 	/*
    727 	 * XXX I don't see how this can work if we're binding multiple domains.
    728 	 * Also, what if someone's editor unlinks and replaces the file?
    729 	 */
    730 	static FILE *df;
    731 	static char ypservers_path[MAXPATHLEN];
    732 
    733 	const char *path;
    734 	char line[_POSIX2_LINE_MAX];
    735 	char *p;
    736 	struct hostent *hp;
    737 	struct sockaddr_in bindsin;
    738 	int i, count = 0;
    739 
    740 	if (df)
    741 		rewind(df);
    742 	else {
    743 		path = ypservers_filename(domainname);
    744 		strcpy(ypservers_path, path);
    745 		df = fopen(ypservers_path, "r");
    746 		if (df == NULL) {
    747 			yp_log(LOG_ERR, "%s: %s", ypservers_path,
    748 			       strerror(errno));
    749 			exit(1);
    750 		}
    751 	}
    752 
    753 	(void)memset(&bindsin, 0, sizeof bindsin);
    754 	bindsin.sin_family = AF_INET;
    755 	bindsin.sin_len = sizeof(bindsin);
    756 	bindsin.sin_port = htons(PMAPPORT);
    757 
    758 	while(fgets(line, (int)sizeof(line), df) != NULL) {
    759 		/* skip lines that are too big */
    760 		p = strchr(line, '\n');
    761 		if (p == NULL) {
    762 			int c;
    763 
    764 			while ((c = getc(df)) != '\n' && c != EOF)
    765 				;
    766 			continue;
    767 		}
    768 		*p = '\0';
    769 		p = line;
    770 		while (isspace((unsigned char)*p))
    771 			p++;
    772 		if (*p == '#')
    773 			continue;
    774 		hp = gethostbyname(p);
    775 		if (!hp) {
    776 			yp_log(LOG_WARNING, "%s: %s", p, hstrerror(h_errno));
    777 			continue;
    778 		}
    779 		/* step through all addresses in case first is unavailable */
    780 		for (i = 0; hp->h_addr_list[i]; i++) {
    781 			(void)memcpy(&bindsin.sin_addr, hp->h_addr_list[0],
    782 			    hp->h_length);
    783 			if (sendto(rpcsock, buf, outlen, 0,
    784 			    (struct sockaddr *)(void *)&bindsin,
    785 			    (socklen_t)sizeof(bindsin)) < 0) {
    786 				yp_log(LOG_WARNING, "direct: sendto: %s",
    787 				       strerror(errno));
    788 				continue;
    789 			} else
    790 				count++;
    791 		}
    792 	}
    793 	if (!count) {
    794 		yp_log(LOG_WARNING, "No contactable servers found in %s",
    795 		    ypservers_path);
    796 		return -1;
    797 	}
    798 	return 0;
    799 }
    800 
    801 static int
    802 direct_set(char *buf, int outlen, struct domain *dom)
    803 {
    804 	struct sockaddr_in bindsin;
    805 	char path[MAXPATHLEN];
    806 	struct iovec iov[2];
    807 	struct ypbind_resp ybr;
    808 	SVCXPRT dummy_svc;
    809 	int fd;
    810 	ssize_t bytes;
    811 
    812 	/*
    813 	 * Gack, we lose if binding file went away.  We reset
    814 	 * "been_set" if this happens, otherwise we'll never
    815 	 * bind again.
    816 	 */
    817 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    818 	    dom->dom_name, dom->dom_vers);
    819 
    820 	fd = open_locked(path, O_RDONLY, 0644);
    821 	if (fd == -1) {
    822 		yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
    823 		dom->dom_been_ypset = 0;
    824 		return -1;
    825 	}
    826 
    827 	/* Read the binding file... */
    828 	iov[0].iov_base = &(dummy_svc.xp_port);
    829 	iov[0].iov_len = sizeof(dummy_svc.xp_port);
    830 	iov[1].iov_base = &ybr;
    831 	iov[1].iov_len = sizeof(ybr);
    832 	bytes = readv(fd, iov, 2);
    833 	(void)close(fd);
    834 	if (bytes <0 || (size_t)bytes != (iov[0].iov_len + iov[1].iov_len)) {
    835 		/* Binding file corrupt? */
    836 		if (bytes < 0)
    837 			yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
    838 		else
    839 			yp_log(LOG_WARNING, "%s: short read", path);
    840 		dom->dom_been_ypset = 0;
    841 		return -1;
    842 	}
    843 
    844 	bindsin.sin_addr =
    845 	    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
    846 
    847 	if (sendto(rpcsock, buf, outlen, 0,
    848 	    (struct sockaddr *)(void *)&bindsin,
    849 	    (socklen_t)sizeof(bindsin)) < 0) {
    850 		yp_log(LOG_WARNING, "direct_set: sendto: %s", strerror(errno));
    851 		return -1;
    852 	}
    853 
    854 	return 0;
    855 }
    856 
    857 static enum clnt_stat
    858 handle_replies(void)
    859 {
    860 	char buf[BUFSIZE];
    861 	socklen_t fromlen;
    862 	ssize_t inlen;
    863 	struct domain *dom;
    864 	struct sockaddr_in raddr;
    865 	struct rpc_msg msg;
    866 	XDR xdr;
    867 
    868 recv_again:
    869 	DPRINTF("handle_replies receiving\n");
    870 	(void)memset(&xdr, 0, sizeof(xdr));
    871 	(void)memset(&msg, 0, sizeof(msg));
    872 	msg.acpted_rply.ar_verf = _null_auth;
    873 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&rmtcr;
    874 	msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
    875 
    876 try_again:
    877 	fromlen = sizeof(struct sockaddr);
    878 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
    879 		(struct sockaddr *)(void *)&raddr, &fromlen);
    880 	if (inlen < 0) {
    881 		if (errno == EINTR)
    882 			goto try_again;
    883 		DPRINTF("handle_replies: recvfrom failed (%s)\n",
    884 			strerror(errno));
    885 		return RPC_CANTRECV;
    886 	}
    887 	if ((size_t)inlen < sizeof(uint32_t))
    888 		goto recv_again;
    889 
    890 	/*
    891 	 * see if reply transaction id matches sent id.
    892 	 * If so, decode the results.
    893 	 */
    894 	xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
    895 	if (xdr_replymsg(&xdr, &msg)) {
    896 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    897 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    898 			raddr.sin_port = htons((uint16_t)rmtcr_port);
    899 			dom = domain_find(msg.rm_xid);
    900 			if (dom != NULL)
    901 				rpc_received(dom->dom_name, &raddr, 0, 0);
    902 		}
    903 	}
    904 	xdr.x_op = XDR_FREE;
    905 	msg.acpted_rply.ar_results.proc = xdr_void;
    906 	xdr_destroy(&xdr);
    907 
    908 	return RPC_SUCCESS;
    909 }
    910 
    911 static enum clnt_stat
    912 handle_ping(void)
    913 {
    914 	char buf[BUFSIZE];
    915 	socklen_t fromlen;
    916 	ssize_t inlen;
    917 	struct domain *dom;
    918 	struct sockaddr_in raddr;
    919 	struct rpc_msg msg;
    920 	XDR xdr;
    921 	bool_t res;
    922 
    923 recv_again:
    924 	DPRINTF("handle_ping receiving\n");
    925 	(void)memset(&xdr, 0, sizeof(xdr));
    926 	(void)memset(&msg, 0, sizeof(msg));
    927 	msg.acpted_rply.ar_verf = _null_auth;
    928 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&res;
    929 	msg.acpted_rply.ar_results.proc = xdr_bool;
    930 
    931 try_again:
    932 	fromlen = sizeof (struct sockaddr);
    933 	inlen = recvfrom(pingsock, buf, sizeof buf, 0,
    934 	    (struct sockaddr *)(void *)&raddr, &fromlen);
    935 	if (inlen < 0) {
    936 		if (errno == EINTR)
    937 			goto try_again;
    938 		DPRINTF("handle_ping: recvfrom failed (%s)\n",
    939 			strerror(errno));
    940 		return RPC_CANTRECV;
    941 	}
    942 	if ((size_t)inlen < sizeof(uint32_t))
    943 		goto recv_again;
    944 
    945 	/*
    946 	 * see if reply transaction id matches sent id.
    947 	 * If so, decode the results.
    948 	 */
    949 	xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
    950 	if (xdr_replymsg(&xdr, &msg)) {
    951 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    952 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    953 			dom = domain_find(msg.rm_xid);
    954 			if (dom != NULL)
    955 				rpc_received(dom->dom_name, &raddr, 0, 0);
    956 		}
    957 	}
    958 	xdr.x_op = XDR_FREE;
    959 	msg.acpted_rply.ar_results.proc = xdr_void;
    960 	xdr_destroy(&xdr);
    961 
    962 	return RPC_SUCCESS;
    963 }
    964 
    965 static int
    966 nag_servers(struct domain *dom)
    967 {
    968 	char *dom_name = dom->dom_name;
    969 	struct rpc_msg msg;
    970 	char buf[BUFSIZE];
    971 	enum clnt_stat st;
    972 	int outlen;
    973 	AUTH *rpcua;
    974 	XDR xdr;
    975 
    976 	DPRINTF("nag_servers\n");
    977 	rmtca.xdr_args = xdr_ypdomain_wrap_string;
    978 	rmtca.args_ptr = (caddr_t)(void *)&dom_name;
    979 
    980 	(void)memset(&xdr, 0, sizeof xdr);
    981 	(void)memset(&msg, 0, sizeof msg);
    982 
    983 	rpcua = authunix_create_default();
    984 	if (rpcua == NULL) {
    985 		DPRINTF("cannot get unix auth\n");
    986 		return RPC_SYSTEMERROR;
    987 	}
    988 	msg.rm_direction = CALL;
    989 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    990 	msg.rm_call.cb_prog = PMAPPROG;
    991 	msg.rm_call.cb_vers = PMAPVERS;
    992 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
    993 	msg.rm_call.cb_cred = rpcua->ah_cred;
    994 	msg.rm_call.cb_verf = rpcua->ah_verf;
    995 
    996 	msg.rm_xid = dom->dom_xid;
    997 	xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
    998 	if (!xdr_callmsg(&xdr, &msg)) {
    999 		st = RPC_CANTENCODEARGS;
   1000 		AUTH_DESTROY(rpcua);
   1001 		return st;
   1002 	}
   1003 	if (!xdr_rmtcall_args(&xdr, &rmtca)) {
   1004 		st = RPC_CANTENCODEARGS;
   1005 		AUTH_DESTROY(rpcua);
   1006 		return st;
   1007 	}
   1008 	outlen = (int)xdr_getpos(&xdr);
   1009 	xdr_destroy(&xdr);
   1010 	if (outlen < 1) {
   1011 		st = RPC_CANTENCODEARGS;
   1012 		AUTH_DESTROY(rpcua);
   1013 		return st;
   1014 	}
   1015 	AUTH_DESTROY(rpcua);
   1016 
   1017 	if (dom->dom_lockfd != -1) {
   1018 		(void)close(dom->dom_lockfd);
   1019 		dom->dom_lockfd = -1;
   1020 		removelock(dom);
   1021 	}
   1022 
   1023 	if (dom->dom_alive == 2) {
   1024 		/*
   1025 		 * This resolves the following situation:
   1026 		 * ypserver on other subnet was once bound,
   1027 		 * but rebooted and is now using a different port
   1028 		 */
   1029 		struct sockaddr_in bindsin;
   1030 
   1031 		(void)memset(&bindsin, 0, sizeof bindsin);
   1032 		bindsin.sin_family = AF_INET;
   1033 		bindsin.sin_len = sizeof(bindsin);
   1034 		bindsin.sin_port = htons(PMAPPORT);
   1035 		bindsin.sin_addr = dom->dom_server_addr.sin_addr;
   1036 
   1037 		if (sendto(rpcsock, buf, outlen, 0,
   1038 		    (struct sockaddr *)(void *)&bindsin,
   1039 		    (socklen_t)sizeof bindsin) == -1)
   1040 			yp_log(LOG_WARNING, "nag_servers: sendto: %s",
   1041 			       strerror(errno));
   1042 	}
   1043 
   1044 	switch (dom->dom_ypbindmode) {
   1045 	case YPBIND_BROADCAST:
   1046 		if (dom->dom_been_ypset) {
   1047 			return direct_set(buf, outlen, dom);
   1048 		}
   1049 		return broadcast(buf, outlen);
   1050 
   1051 	case YPBIND_DIRECT:
   1052 		return direct(buf, outlen);
   1053 	}
   1054 	/*NOTREACHED*/
   1055 	return -1;
   1056 }
   1057 
   1058 static int
   1059 ping(struct domain *dom)
   1060 {
   1061 	char *dom_name = dom->dom_name;
   1062 	struct rpc_msg msg;
   1063 	char buf[BUFSIZE];
   1064 	enum clnt_stat st;
   1065 	int outlen;
   1066 	AUTH *rpcua;
   1067 	XDR xdr;
   1068 
   1069 	(void)memset(&xdr, 0, sizeof xdr);
   1070 	(void)memset(&msg, 0, sizeof msg);
   1071 
   1072 	rpcua = authunix_create_default();
   1073 	if (rpcua == NULL) {
   1074 		DPRINTF("cannot get unix auth\n");
   1075 		return RPC_SYSTEMERROR;
   1076 	}
   1077 
   1078 	msg.rm_direction = CALL;
   1079 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
   1080 	msg.rm_call.cb_prog = YPPROG;
   1081 	msg.rm_call.cb_vers = YPVERS;
   1082 	msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
   1083 	msg.rm_call.cb_cred = rpcua->ah_cred;
   1084 	msg.rm_call.cb_verf = rpcua->ah_verf;
   1085 
   1086 	msg.rm_xid = dom->dom_xid;
   1087 	xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
   1088 	if (!xdr_callmsg(&xdr, &msg)) {
   1089 		st = RPC_CANTENCODEARGS;
   1090 		AUTH_DESTROY(rpcua);
   1091 		return st;
   1092 	}
   1093 	if (!xdr_ypdomain_wrap_string(&xdr, &dom_name)) {
   1094 		st = RPC_CANTENCODEARGS;
   1095 		AUTH_DESTROY(rpcua);
   1096 		return st;
   1097 	}
   1098 	outlen = (int)xdr_getpos(&xdr);
   1099 	xdr_destroy(&xdr);
   1100 	if (outlen < 1) {
   1101 		st = RPC_CANTENCODEARGS;
   1102 		AUTH_DESTROY(rpcua);
   1103 		return st;
   1104 	}
   1105 	AUTH_DESTROY(rpcua);
   1106 
   1107 	dom->dom_alive = 2;
   1108 	DPRINTF("ping %x\n", dom->dom_server_addr.sin_addr.s_addr);
   1109 
   1110 	if (sendto(pingsock, buf, outlen, 0,
   1111 	    (struct sockaddr *)(void *)&dom->dom_server_addr,
   1112 	    (socklen_t)(sizeof dom->dom_server_addr)) == -1)
   1113 		yp_log(LOG_WARNING, "ping: sendto: %s", strerror(errno));
   1114 	return 0;
   1115 
   1116 }
   1117 
   1118 /*
   1119  * State transition is done like this:
   1120  *
   1121  * STATE	EVENT		ACTION			NEWSTATE	TIMEOUT
   1122  * no binding	timeout		broadcast 		no binding	5 sec
   1123  * no binding	answer		--			binding		60 sec
   1124  * binding	timeout		ping server		checking	5 sec
   1125  * checking	timeout		ping server + broadcast	checking	5 sec
   1126  * checking	answer		--			binding		60 sec
   1127  */
   1128 static void
   1129 checkwork(void)
   1130 {
   1131 	struct domain *dom;
   1132 	time_t t;
   1133 
   1134 	check = 0;
   1135 
   1136 	(void)time(&t);
   1137 	for (dom = domains; dom != NULL; dom = dom->dom_next) {
   1138 		if (dom->dom_checktime < t) {
   1139 			if (dom->dom_alive == 1)
   1140 				(void)ping(dom);
   1141 			else
   1142 				(void)nag_servers(dom);
   1143 			(void)time(&t);
   1144 			dom->dom_checktime = t + 5;
   1145 		}
   1146 	}
   1147 }
   1148 
   1149 ////////////////////////////////////////////////////////////
   1150 // main
   1151 
   1152 static void
   1153 usage(void)
   1154 {
   1155 	const char *opt = "";
   1156 #ifdef DEBUG
   1157 	opt = " [-d]";
   1158 #endif
   1159 
   1160 	(void)fprintf(stderr,
   1161 	    "Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme]%s\n",
   1162 	    getprogname(), opt);
   1163 	exit(1);
   1164 }
   1165 
   1166 int
   1167 main(int argc, char *argv[])
   1168 {
   1169 	struct timeval tv;
   1170 	fd_set fdsr;
   1171 	int width, lockfd;
   1172 	int evil = 0;
   1173 
   1174 	setprogname(argv[0]);
   1175 	(void)yp_get_default_domain(&domainname);
   1176 	if (domainname[0] == '\0')
   1177 		errx(1, "Domainname not set. Aborting.");
   1178 	if (_yp_invalid_domain(domainname))
   1179 		errx(1, "Invalid domainname: %s", domainname);
   1180 
   1181 	default_ypbindmode = YPBIND_DIRECT;
   1182 
   1183 	while (--argc) {
   1184 		++argv;
   1185 		if (!strcmp("-insecure", *argv)) {
   1186 			insecure = 1;
   1187 		} else if (!strcmp("-ypset", *argv)) {
   1188 			allow_any_ypset = 1;
   1189 			allow_local_ypset = 1;
   1190 		} else if (!strcmp("-ypsetme", *argv)) {
   1191 			allow_any_ypset = 0;
   1192 			allow_local_ypset = 1;
   1193 		} else if (!strcmp("-broadcast", *argv)) {
   1194 			default_ypbindmode = YPBIND_BROADCAST;
   1195 #ifdef DEBUG
   1196 		} else if (!strcmp("-d", *argv)) {
   1197 			debug = 1;
   1198 #endif
   1199 		} else {
   1200 			usage();
   1201 		}
   1202 	}
   1203 
   1204 	/* initialise syslog */
   1205 	openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON);
   1206 
   1207 	/* acquire ypbind.lock */
   1208 	lockfd = open_locked(_PATH_YPBIND_LOCK, O_CREAT|O_RDWR|O_TRUNC, 0644);
   1209 	if (lockfd == -1)
   1210 		err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
   1211 
   1212 	/* initialize sunrpc stuff */
   1213 	sunrpc_setup();
   1214 
   1215 	/* blow away old bindings in BINDINGDIR */
   1216 	if (purge_bindingdir(BINDINGDIR) < 0)
   1217 		errx(1, "unable to purge old bindings from %s", BINDINGDIR);
   1218 
   1219 	/* build initial domain binding, make it "unsuccessful" */
   1220 	domains = domain_create(domainname);
   1221 	removelock(domains);
   1222 
   1223 	checkwork();
   1224 
   1225 	for (;;) {
   1226 		width = svc_maxfd;
   1227 		if (rpcsock > width)
   1228 			width = rpcsock;
   1229 		if (pingsock > width)
   1230 			width = pingsock;
   1231 		width++;
   1232 		fdsr = svc_fdset;
   1233 		FD_SET(rpcsock, &fdsr);
   1234 		FD_SET(pingsock, &fdsr);
   1235 		tv.tv_sec = 1;
   1236 		tv.tv_usec = 0;
   1237 
   1238 		switch (select(width, &fdsr, NULL, NULL, &tv)) {
   1239 		case 0:
   1240 			checkwork();
   1241 			break;
   1242 		case -1:
   1243 			yp_log(LOG_WARNING, "select: %s", strerror(errno));
   1244 			break;
   1245 		default:
   1246 			if (FD_ISSET(rpcsock, &fdsr))
   1247 				(void)handle_replies();
   1248 			if (FD_ISSET(pingsock, &fdsr))
   1249 				(void)handle_ping();
   1250 			svc_getreqset(&fdsr);
   1251 			if (check)
   1252 				checkwork();
   1253 			break;
   1254 		}
   1255 
   1256 		if (!evil && domains->dom_alive) {
   1257 			evil = 1;
   1258 #ifdef DEBUG
   1259 			if (!debug)
   1260 #endif
   1261 				(void)daemon(0, 0);
   1262 			(void)pidfile(NULL);
   1263 		}
   1264 	}
   1265 }
   1266