Home | History | Annotate | Line # | Download | only in ypbind
ypbind.c revision 1.23
      1 /*	$NetBSD: ypbind.c,v 1.23 1996/06/03 20:44:51 thorpej 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 #ifndef LINT
     36 static char rcsid[] = "$NetBSD: ypbind.c,v 1.23 1996/06/03 20:44:51 thorpej Exp $";
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/types.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/signal.h>
     43 #include <sys/socket.h>
     44 #include <sys/file.h>
     45 #include <sys/fcntl.h>
     46 #include <sys/uio.h>
     47 #include <sys/syslog.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <errno.h>
     51 #include <ctype.h>
     52 #include <dirent.h>
     53 #include <netdb.h>
     54 #include <string.h>
     55 #include <rpc/rpc.h>
     56 #include <rpc/xdr.h>
     57 #include <net/if.h>
     58 #include <arpa/inet.h>
     59 #include <rpc/pmap_clnt.h>
     60 #include <rpc/pmap_prot.h>
     61 #include <rpc/pmap_rmt.h>
     62 #include <unistd.h>
     63 #include <rpcsvc/yp_prot.h>
     64 #include <rpcsvc/ypclnt.h>
     65 
     66 #define BINDINGDIR	"/var/yp/binding"
     67 #define YPBINDLOCK	"/var/run/ypbind.lock"
     68 
     69 struct _dom_binding {
     70 	struct _dom_binding *dom_pnext;
     71 	char dom_domain[YPMAXDOMAIN + 1];
     72 	struct sockaddr_in dom_server_addr;
     73 	unsigned short int dom_server_port;
     74 	int dom_socket;
     75 	CLIENT *dom_client;
     76 	long int dom_vers;
     77 	time_t dom_check_t;
     78 	time_t dom_ask_t;
     79 	int dom_lockfd;
     80 	int dom_alive;
     81 	int dom_xid;
     82 };
     83 
     84 char *domainname;
     85 
     86 struct _dom_binding *ypbindlist;
     87 int check;
     88 
     89 #define YPSET_NO	0
     90 #define YPSET_LOCAL	1
     91 #define YPSET_ALL	2
     92 int ypsetmode = YPSET_NO;
     93 
     94 int rpcsock, pingsock;
     95 struct rmtcallargs rmtca;
     96 struct rmtcallres rmtcr;
     97 char rmtcr_outval;
     98 u_long rmtcr_port;
     99 SVCXPRT *udptransp, *tcptransp;
    100 
    101 struct _dom_binding *xid2ypdb __P((int xid));
    102 int unique_xid __P((struct _dom_binding *ypdb));
    103 
    104 void *
    105 ypbindproc_null_2(transp, argp, clnt)
    106 	SVCXPRT *transp;
    107 	void *argp;
    108 	CLIENT *clnt;
    109 {
    110 	static char res;
    111 
    112 	memset(&res, 0, sizeof(res));
    113 	return (void *)&res;
    114 }
    115 
    116 struct ypbind_resp *
    117 ypbindproc_domain_2(transp, argp, clnt)
    118 	SVCXPRT *transp;
    119 	char **argp;
    120 	CLIENT *clnt;
    121 {
    122 	static struct ypbind_resp res;
    123 	struct _dom_binding *ypdb;
    124 	char path[MAXPATHLEN];
    125 	char *arg = *argp;
    126 	time_t now;
    127 
    128 	memset(&res, 0, sizeof res);
    129 	res.ypbind_status = YPBIND_FAIL_VAL;
    130 
    131 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    132 		if (!strcmp(ypdb->dom_domain, arg))
    133 			break;
    134 
    135 	if (ypdb == NULL) {
    136 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
    137 		memset(ypdb, 0, sizeof *ypdb);
    138 		strncpy(ypdb->dom_domain, arg, sizeof ypdb->dom_domain);
    139 		ypdb->dom_vers = YPVERS;
    140 		ypdb->dom_alive = 0;
    141 		ypdb->dom_lockfd = -1;
    142 		sprintf(path, "%s/%s.%d", BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
    143 		unlink(path);
    144 		ypdb->dom_xid = unique_xid(ypdb);
    145 		ypdb->dom_pnext = ypbindlist;
    146 		ypbindlist = ypdb;
    147 		check++;
    148 		return NULL;
    149 	}
    150 
    151 	if (ypdb->dom_alive == 0)
    152 		return NULL;
    153 
    154 #ifdef HEURISTIC
    155 	time(&now);
    156 	if (now < ypdb->dom_ask_t + 5) {
    157 		/*
    158 		 * Hmm. More than 2 requests in 5 seconds have indicated
    159 		 * that my binding is possibly incorrect.
    160 		 * Ok, do an immediate poll of the server.
    161 		 */
    162 		if (ypdb->dom_check_t >= now) {
    163 			/* don't flood it */
    164 			ypdb->dom_check_t = 0;
    165 			check++;
    166 		}
    167 	}
    168 	ypdb->dom_ask_t = now;
    169 #endif
    170 
    171 answer:
    172 	res.ypbind_status = YPBIND_SUCC_VAL;
    173 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
    174 		ypdb->dom_server_addr.sin_addr.s_addr;
    175 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    176 		ypdb->dom_server_port;
    177 	/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
    178 		inet_ntoa(ypdb->dom_server_addr.sin_addr),
    179 		ntohs(ypdb->dom_server_addr.sin_port));*/
    180 	return &res;
    181 }
    182 
    183 bool_t *
    184 ypbindproc_setdom_2(transp, argp, clnt)
    185 	SVCXPRT *transp;
    186 	struct ypbind_setdom *argp;
    187 	CLIENT *clnt;
    188 {
    189 	struct sockaddr_in *fromsin, bindsin;
    190 	static bool_t res;
    191 
    192 	memset(&res, 0, sizeof(res));
    193 	fromsin = svc_getcaller(transp);
    194 
    195 	switch (ypsetmode) {
    196 	case YPSET_LOCAL:
    197 		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
    198 			return (bool_t *)NULL;
    199 		break;
    200 	case YPSET_ALL:
    201 		break;
    202 	case YPSET_NO:
    203 	default:
    204 		return (bool_t *)NULL;
    205 	}
    206 
    207 	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED)
    208 		return &res;
    209 
    210 	if (argp->ypsetdom_vers != YPVERS)
    211 		return &res;
    212 
    213 	memset(&bindsin, 0, sizeof bindsin);
    214 	bindsin.sin_family = AF_INET;
    215 	bindsin.sin_len = sizeof(bindsin);
    216 	bindsin.sin_addr = argp->ypsetdom_addr;
    217 	bindsin.sin_port = argp->ypsetdom_port;
    218 	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
    219 
    220 	res = 1;
    221 	return &res;
    222 }
    223 
    224 static void
    225 ypbindprog_2(rqstp, transp)
    226 	struct svc_req *rqstp;
    227 	register SVCXPRT *transp;
    228 {
    229 	union {
    230 		char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
    231 		struct ypbind_setdom ypbindproc_setdom_2_arg;
    232 	} argument;
    233 	struct authunix_parms *creds;
    234 	char *result;
    235 	bool_t (*xdr_argument)(), (*xdr_result)();
    236 	char *(*local)();
    237 
    238 	switch (rqstp->rq_proc) {
    239 	case YPBINDPROC_NULL:
    240 		xdr_argument = xdr_void;
    241 		xdr_result = xdr_void;
    242 		local = (char *(*)()) ypbindproc_null_2;
    243 		break;
    244 
    245 	case YPBINDPROC_DOMAIN:
    246 		xdr_argument = xdr_ypdomain_wrap_string;
    247 		xdr_result = xdr_ypbind_resp;
    248 		local = (char *(*)()) ypbindproc_domain_2;
    249 		break;
    250 
    251 	case YPBINDPROC_SETDOM:
    252 		switch (rqstp->rq_cred.oa_flavor) {
    253 		case AUTH_UNIX:
    254 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
    255 			if (creds->aup_uid != 0) {
    256 				svcerr_auth(transp, AUTH_BADCRED);
    257 				return;
    258 			}
    259 			break;
    260 		default:
    261 			svcerr_auth(transp, AUTH_TOOWEAK);
    262 			return;
    263 		}
    264 
    265 		xdr_argument = xdr_ypbind_setdom;
    266 		xdr_result = xdr_void;
    267 		local = (char *(*)()) ypbindproc_setdom_2;
    268 		break;
    269 
    270 	default:
    271 		svcerr_noproc(transp);
    272 		return;
    273 	}
    274 	memset(&argument, 0, sizeof(argument));
    275 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    276 		svcerr_decode(transp);
    277 		return;
    278 	}
    279 	result = (*local)(transp, &argument, rqstp);
    280 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    281 		svcerr_systemerr(transp);
    282 	}
    283 	return;
    284 }
    285 
    286 main(argc, argv)
    287 	int argc;
    288 	char *argv[];
    289 {
    290 	char path[MAXPATHLEN];
    291 	struct timeval tv;
    292 	fd_set fdsr;
    293 	int width, lockfd;
    294 	int evil = 0, one;
    295 
    296 	yp_get_default_domain(&domainname);
    297 	if (domainname[0] == '\0') {
    298 		fprintf(stderr, "domainname not set. Aborting.\n");
    299 		exit(1);
    300 	}
    301 
    302 	while (--argc) {
    303 		++argv;
    304 		if (!strcmp("-ypset", *argv))
    305 			ypsetmode = YPSET_ALL;
    306 		else if (!strcmp("-ypsetme", *argv))
    307 			ypsetmode = YPSET_LOCAL;
    308 	}
    309 
    310 	/* blow away everything in BINDINGDIR */
    311 
    312 #ifdef O_SHLOCK
    313 	if ((lockfd = open(YPBINDLOCK, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
    314 		fprintf(stderr, "ypbind: cannot create %s\n", YPBINDLOCK);
    315 		exit(1);
    316 	}
    317 #else
    318 	if ((lockfd = open(YPBINDLOCK, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
    319 		fprintf(stderr, "ypbind: cannot create %s\n", YPBINDLOCK);
    320 		exit(1);
    321 	}
    322 	flock(lockfd, LOCK_SH);
    323 #endif
    324 
    325 	(void)pmap_unset(YPBINDPROG, YPBINDVERS);
    326 
    327 	udptransp = svcudp_create(RPC_ANYSOCK);
    328 	if (udptransp == NULL) {
    329 		fprintf(stderr, "cannot create udp service.");
    330 		exit(1);
    331 	}
    332 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    333 	    IPPROTO_UDP)) {
    334 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).");
    335 		exit(1);
    336 	}
    337 
    338 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
    339 	if (tcptransp == NULL) {
    340 		fprintf(stderr, "cannot create tcp service.");
    341 		exit(1);
    342 	}
    343 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    344 	    IPPROTO_TCP)) {
    345 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).");
    346 		exit(1);
    347 	}
    348 
    349 	if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    350 		perror("socket");
    351 		return -1;
    352 	}
    353 	if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    354 		perror("socket");
    355 		return -1;
    356 	}
    357 
    358 	fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    359 	fcntl(pingsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    360 	one = 1;
    361 	setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
    362 	rmtca.prog = YPPROG;
    363 	rmtca.vers = YPVERS;
    364 	rmtca.proc = YPPROC_DOMAIN_NONACK;
    365 	rmtca.xdr_args = NULL;		/* set at call time */
    366 	rmtca.args_ptr = NULL;		/* set at call time */
    367 	rmtcr.port_ptr = &rmtcr_port;
    368 	rmtcr.xdr_results = xdr_bool;
    369 	rmtcr.results_ptr = (caddr_t)&rmtcr_outval;
    370 
    371 	/* build initial domain binding, make it "unsuccessful" */
    372 	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
    373 	memset(ypbindlist, 0, sizeof *ypbindlist);
    374 	strncpy(ypbindlist->dom_domain, domainname, sizeof ypbindlist->dom_domain);
    375 	ypbindlist->dom_vers = YPVERS;
    376 	ypbindlist->dom_alive = 0;
    377 	ypbindlist->dom_lockfd = -1;
    378 	sprintf(path, "%s/%s.%d", BINDINGDIR, ypbindlist->dom_domain,
    379 		ypbindlist->dom_vers);
    380 	(void)unlink(path);
    381 
    382 	checkwork();
    383 
    384 	while (1) {
    385 		width = svc_maxfd;
    386 		if (rpcsock > width)
    387 			width = rpcsock;
    388 		if (pingsock > width)
    389 			width = pingsock;
    390 		width++;
    391 
    392 		fdsr = svc_fdset;
    393 		FD_SET(rpcsock, &fdsr);
    394 		FD_SET(pingsock, &fdsr);
    395 		tv.tv_sec = 1;
    396 		tv.tv_usec = 0;
    397 
    398 		switch (select(width, &fdsr, NULL, NULL, &tv)) {
    399 		case 0:
    400 			checkwork();
    401 			break;
    402 		case -1:
    403 			perror("select\n");
    404 			break;
    405 		default:
    406 			if (FD_ISSET(rpcsock, &fdsr))
    407 				handle_replies();
    408 			if (FD_ISSET(pingsock, &fdsr))
    409 				handle_ping();
    410 			svc_getreqset(&fdsr);
    411 			if (check)
    412 				checkwork();
    413 			break;
    414 		}
    415 
    416 		if (!evil && ypbindlist->dom_alive) {
    417 			evil = 1;
    418 			daemon(0, 0);
    419 		}
    420 	}
    421 }
    422 
    423 /*
    424  * State transition is done like this:
    425  *
    426  * STATE	EVENT		ACTION			NEWSTATE	TIMEOUT
    427  * no binding	timeout		broadcast 		no binding	5 sec
    428  * no binding	answer		--			binding		60 sec
    429  * binding	timeout		ping server		checking	5 sec
    430  * checking	timeout		ping server + broadcast	checking	5 sec
    431  * checking	answer		--			binding		60 sec
    432  */
    433 checkwork()
    434 {
    435 	struct _dom_binding *ypdb;
    436 	time_t t;
    437 
    438 	check = 0;
    439 
    440 	time(&t);
    441 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
    442 		if (ypdb->dom_check_t < t) {
    443 			if (ypdb->dom_alive == 1)
    444 				ping(ypdb);
    445 			else
    446 				broadcast(ypdb);
    447 			time(&t);
    448 			ypdb->dom_check_t = t + 5;
    449 		}
    450 	}
    451 }
    452 
    453 ping(ypdb)
    454 	struct _dom_binding *ypdb;
    455 {
    456 	char *dom = ypdb->dom_domain;
    457 	struct rpc_msg msg;
    458 	char buf[1400];
    459 	enum clnt_stat st;
    460 	int outlen;
    461 	AUTH *rpcua;
    462 	XDR xdr;
    463 
    464 	memset(&xdr, 0, sizeof xdr);
    465 	memset(&msg, 0, sizeof msg);
    466 
    467 	rpcua = authunix_create_default();
    468 	if (rpcua == (AUTH *)NULL) {
    469 		/*printf("cannot get unix auth\n");*/
    470 		return RPC_SYSTEMERROR;
    471 	}
    472 	msg.rm_direction = CALL;
    473 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    474 	msg.rm_call.cb_prog = YPPROG;
    475 	msg.rm_call.cb_vers = YPVERS;
    476 	msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
    477 	msg.rm_call.cb_cred = rpcua->ah_cred;
    478 	msg.rm_call.cb_verf = rpcua->ah_verf;
    479 
    480 	msg.rm_xid = ypdb->dom_xid;
    481 	xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
    482 	if (!xdr_callmsg(&xdr, &msg)) {
    483 		st = RPC_CANTENCODEARGS;
    484 		AUTH_DESTROY(rpcua);
    485 		return st;
    486 	}
    487 	if (!xdr_ypdomain_wrap_string(&xdr, &dom)) {
    488 		st = RPC_CANTENCODEARGS;
    489 		AUTH_DESTROY(rpcua);
    490 		return st;
    491 	}
    492 	outlen = (int)xdr_getpos(&xdr);
    493 	xdr_destroy(&xdr);
    494 	if (outlen < 1) {
    495 		st = RPC_CANTENCODEARGS;
    496 		AUTH_DESTROY(rpcua);
    497 		return st;
    498 	}
    499 	AUTH_DESTROY(rpcua);
    500 
    501 	ypdb->dom_alive = 2;
    502 	if (sendto(pingsock, buf, outlen, 0,
    503 		   (struct sockaddr *)&ypdb->dom_server_addr,
    504 		   sizeof ypdb->dom_server_addr) < 0)
    505 		perror("sendto");
    506 	return 0;
    507 
    508 }
    509 
    510 broadcast(ypdb)
    511 	struct _dom_binding *ypdb;
    512 {
    513 	char *dom = ypdb->dom_domain;
    514 	struct rpc_msg msg;
    515 	char buf[1400], inbuf[8192];
    516 	char path[MAXPATHLEN];
    517 	enum clnt_stat st;
    518 	int outlen, i, sock, len;
    519 	struct sockaddr_in bindsin;
    520 	struct ifconf ifc;
    521 	struct ifreq ifreq, *ifr;
    522 	struct in_addr in;
    523 	AUTH *rpcua;
    524 	XDR xdr;
    525 
    526 	rmtca.xdr_args = xdr_ypdomain_wrap_string;
    527 	rmtca.args_ptr = (char *)&dom;
    528 
    529 	memset(&xdr, 0, sizeof xdr);
    530 	memset(&msg, 0, sizeof msg);
    531 
    532 	rpcua = authunix_create_default();
    533 	if (rpcua == (AUTH *)NULL) {
    534 		/*printf("cannot get unix auth\n");*/
    535 		return RPC_SYSTEMERROR;
    536 	}
    537 	msg.rm_direction = CALL;
    538 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    539 	msg.rm_call.cb_prog = PMAPPROG;
    540 	msg.rm_call.cb_vers = PMAPVERS;
    541 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
    542 	msg.rm_call.cb_cred = rpcua->ah_cred;
    543 	msg.rm_call.cb_verf = rpcua->ah_verf;
    544 
    545 	msg.rm_xid = ypdb->dom_xid;
    546 	xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
    547 	if (!xdr_callmsg(&xdr, &msg)) {
    548 		st = RPC_CANTENCODEARGS;
    549 		AUTH_DESTROY(rpcua);
    550 		return st;
    551 	}
    552 	if (!xdr_rmtcall_args(&xdr, &rmtca)) {
    553 		st = RPC_CANTENCODEARGS;
    554 		AUTH_DESTROY(rpcua);
    555 		return st;
    556 	}
    557 	outlen = (int)xdr_getpos(&xdr);
    558 	xdr_destroy(&xdr);
    559 	if (outlen < 1) {
    560 		st = RPC_CANTENCODEARGS;
    561 		AUTH_DESTROY(rpcua);
    562 		return st;
    563 	}
    564 	AUTH_DESTROY(rpcua);
    565 
    566 	if (ypdb->dom_lockfd != -1) {
    567 		close(ypdb->dom_lockfd);
    568 		ypdb->dom_lockfd = -1;
    569 		sprintf(path, "%s/%s.%d", BINDINGDIR,
    570 			ypdb->dom_domain, ypdb->dom_vers);
    571 		unlink(path);
    572 	}
    573 
    574 	memset(&bindsin, 0, sizeof bindsin);
    575 	bindsin.sin_family = AF_INET;
    576 	bindsin.sin_len = sizeof(bindsin);
    577 	bindsin.sin_port = htons(PMAPPORT);
    578 
    579 	if (ypdb->dom_alive == 2) {
    580 		/*
    581 		 * This resolves the following situation:
    582 		 * ypserver on other subnet was once bound,
    583 		 * but rebooted and is now using a different port
    584 		 */
    585 		bindsin.sin_addr = ypdb->dom_server_addr.sin_addr;
    586 		if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
    587 			   sizeof bindsin) < 0)
    588 			perror("sendto");
    589 	}
    590 	/* find all networks and send the RPC packet out them all */
    591 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    592 		perror("socket");
    593 		return -1;
    594 	}
    595 
    596 	ifc.ifc_len = sizeof inbuf;
    597 	ifc.ifc_buf = inbuf;
    598 	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
    599 		close(sock);
    600 		perror("ioctl(SIOCGIFCONF)");
    601 		return -1;
    602 	}
    603 	ifr = ifc.ifc_req;
    604 	ifreq.ifr_name[0] = '\0';
    605 	for (i = 0; i < ifc.ifc_len; i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
    606 #if defined(BSD) && BSD >= 199103
    607 		len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
    608 #else
    609 		len = sizeof ifc.ifc_len / sizeof(struct ifreq);
    610 #endif
    611 		ifreq = *ifr;
    612 		if (ifreq.ifr_addr.sa_family != AF_INET)
    613 			continue;
    614 		if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
    615 			perror("ioctl(SIOCGIFFLAGS)");
    616 			continue;
    617 		}
    618 		if ((ifreq.ifr_flags & IFF_UP) == 0)
    619 			continue;
    620 
    621 		ifreq.ifr_flags &= (IFF_LOOPBACK | IFF_BROADCAST);
    622 		if (ifreq.ifr_flags == IFF_BROADCAST) {
    623 			if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
    624 				perror("ioctl(SIOCGIFBRDADDR)");
    625 				continue;
    626 			}
    627 		} else if (ifreq.ifr_flags == IFF_LOOPBACK) {
    628 			if (ioctl(sock, SIOCGIFADDR, &ifreq) < 0) {
    629 				perror("ioctl(SIOCGIFADDR)");
    630 				continue;
    631 			}
    632 		} else
    633 			continue;
    634 
    635 		in = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
    636 		bindsin.sin_addr = in;
    637 		if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
    638 			   sizeof bindsin) < 0)
    639 			perror("sendto");
    640 	}
    641 	close(sock);
    642 	return 0;
    643 }
    644 
    645 /*enum clnt_stat*/
    646 handle_replies()
    647 {
    648 	char buf[1400];
    649 	int fromlen, inlen;
    650 	struct _dom_binding *ypdb;
    651 	struct sockaddr_in raddr;
    652 	struct rpc_msg msg;
    653 	XDR xdr;
    654 
    655 recv_again:
    656 	memset(&xdr, 0, sizeof(xdr));
    657 	memset(&msg, 0, sizeof(msg));
    658 	msg.acpted_rply.ar_verf = _null_auth;
    659 	msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr;
    660 	msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
    661 
    662 try_again:
    663 	fromlen = sizeof (struct sockaddr);
    664 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
    665 		(struct sockaddr *)&raddr, &fromlen);
    666 	if (inlen < 0) {
    667 		if (errno == EINTR)
    668 			goto try_again;
    669 		return RPC_CANTRECV;
    670 	}
    671 	if (inlen < sizeof(u_int32_t))
    672 		goto recv_again;
    673 
    674 	/*
    675 	 * see if reply transaction id matches sent id.
    676 	 * If so, decode the results.
    677 	 */
    678 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
    679 	if (xdr_replymsg(&xdr, &msg)) {
    680 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    681 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    682 			raddr.sin_port = htons((u_short)rmtcr_port);
    683 			ypdb = xid2ypdb(msg.rm_xid);
    684 			if (ypdb != NULL)
    685 				rpc_received(ypdb->dom_domain, &raddr, 0);
    686 		}
    687 	}
    688 	xdr.x_op = XDR_FREE;
    689 	msg.acpted_rply.ar_results.proc = xdr_void;
    690 	xdr_destroy(&xdr);
    691 
    692 	return RPC_SUCCESS;
    693 }
    694 
    695 /*enum clnt_stat*/
    696 handle_ping()
    697 {
    698 	char buf[1400];
    699 	int fromlen, inlen;
    700 	struct _dom_binding *ypdb;
    701 	struct sockaddr_in raddr;
    702 	struct rpc_msg msg;
    703 	XDR xdr;
    704 	bool_t res;
    705 
    706 recv_again:
    707 	memset(&xdr, 0, sizeof(xdr));
    708 	memset(&msg, 0, sizeof(msg));
    709 	msg.acpted_rply.ar_verf = _null_auth;
    710 	msg.acpted_rply.ar_results.where = (caddr_t)&res;
    711 	msg.acpted_rply.ar_results.proc = xdr_bool;
    712 
    713 try_again:
    714 	fromlen = sizeof (struct sockaddr);
    715 	inlen = recvfrom(pingsock, buf, sizeof buf, 0,
    716 		(struct sockaddr *)&raddr, &fromlen);
    717 	if (inlen < 0) {
    718 		if (errno == EINTR)
    719 			goto try_again;
    720 		return RPC_CANTRECV;
    721 	}
    722 	if (inlen < sizeof(u_int32_t))
    723 		goto recv_again;
    724 
    725 	/*
    726 	 * see if reply transaction id matches sent id.
    727 	 * If so, decode the results.
    728 	 */
    729 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
    730 	if (xdr_replymsg(&xdr, &msg)) {
    731 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    732 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    733 			ypdb = xid2ypdb(msg.rm_xid);
    734 			if (ypdb != NULL)
    735 				rpc_received(ypdb->dom_domain, &raddr, 0);
    736 		}
    737 	}
    738 	xdr.x_op = XDR_FREE;
    739 	msg.acpted_rply.ar_results.proc = xdr_void;
    740 	xdr_destroy(&xdr);
    741 
    742 	return RPC_SUCCESS;
    743 }
    744 
    745 /*
    746  * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
    747  */
    748 rpc_received(dom, raddrp, force)
    749 char *dom;
    750 struct sockaddr_in *raddrp;
    751 int force;
    752 {
    753 	struct _dom_binding *ypdb;
    754 	struct iovec iov[2];
    755 	struct ypbind_resp ybr;
    756 	char path[MAXPATHLEN];
    757 	int fd;
    758 
    759 	/*printf("returned from %s about %s\n", inet_ntoa(raddrp->sin_addr), dom);*/
    760 
    761 	if (dom == NULL)
    762 		return;
    763 
    764 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    765 		if (!strcmp(ypdb->dom_domain, dom))
    766 			break;
    767 
    768 	if (ypdb == NULL) {
    769 		if (force == 0)
    770 			return;
    771 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
    772 		memset(ypdb, 0, sizeof *ypdb);
    773 		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
    774 		ypdb->dom_lockfd = -1;
    775 		ypdb->dom_pnext = ypbindlist;
    776 		ypbindlist = ypdb;
    777 	}
    778 
    779 	/* soft update, alive */
    780 	if (ypdb->dom_alive == 1 && force == 0) {
    781 		if (!memcmp(&ypdb->dom_server_addr, raddrp,
    782 			    sizeof ypdb->dom_server_addr)) {
    783 			ypdb->dom_alive = 1;
    784 			ypdb->dom_check_t = time(NULL) + 60; /* recheck binding in 60 sec */
    785 		}
    786 		return;
    787 	}
    788 
    789 	memcpy(&ypdb->dom_server_addr, raddrp, sizeof ypdb->dom_server_addr);
    790 	ypdb->dom_check_t = time(NULL) + 60;	/* recheck binding in 60 seconds */
    791 	ypdb->dom_vers = YPVERS;
    792 	ypdb->dom_alive = 1;
    793 
    794 	if (ypdb->dom_lockfd != -1)
    795 		close(ypdb->dom_lockfd);
    796 
    797 	sprintf(path, "%s/%s.%d", BINDINGDIR,
    798 		ypdb->dom_domain, ypdb->dom_vers);
    799 #ifdef O_SHLOCK
    800 	if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
    801 		(void)mkdir(BINDINGDIR, 0755);
    802 		if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
    803 			return;
    804 	}
    805 #else
    806 	if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
    807 		(void)mkdir(BINDINGDIR, 0755);
    808 		if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
    809 			return;
    810 	}
    811 	flock(fd, LOCK_SH);
    812 #endif
    813 
    814 	/*
    815 	 * ok, if BINDINGDIR exists, and we can create the binding file,
    816 	 * then write to it..
    817 	 */
    818 	ypdb->dom_lockfd = fd;
    819 
    820 	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
    821 	iov[0].iov_len = sizeof udptransp->xp_port;
    822 	iov[1].iov_base = (caddr_t)&ybr;
    823 	iov[1].iov_len = sizeof ybr;
    824 
    825 	memset(&ybr, 0, sizeof ybr);
    826 	ybr.ypbind_status = YPBIND_SUCC_VAL;
    827 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr;
    828 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
    829 
    830 	if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
    831 		perror("write");
    832 		close(ypdb->dom_lockfd);
    833 		unlink(path);
    834 		ypdb->dom_lockfd = -1;
    835 		return;
    836 	}
    837 }
    838 
    839 struct _dom_binding *
    840 xid2ypdb(xid)
    841 	int xid;
    842 {
    843 	struct _dom_binding *ypdb;
    844 
    845 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    846 		if (ypdb->dom_xid == xid)
    847 			break;
    848 	return (ypdb);
    849 }
    850 
    851 int
    852 unique_xid(ypdb)
    853 	struct _dom_binding *ypdb;
    854 {
    855 	int tmp_xid;
    856 
    857 	tmp_xid = (long)ypdb & 0xffffffff;
    858 	while (xid2ypdb(tmp_xid) != NULL)
    859 		tmp_xid++;
    860 
    861 	return tmp_xid;
    862 }
    863