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