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