xref: /freebsd/usr.sbin/ypbind/ypbind.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef LINT
31 static char rcsid[] = "$Id: ypbind.c,v 1.1 1994/08/08 01:03:58 wollman Exp $";
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/signal.h>
38 #include <sys/socket.h>
39 #include <sys/file.h>
40 #include <sys/fcntl.h>
41 #include <sys/uio.h>
42 #include <sys/syslog.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <netdb.h>
48 #include <string.h>
49 #include <rpc/rpc.h>
50 #include <rpc/xdr.h>
51 #include <net/if.h>
52 #include <arpa/inet.h>
53 #include <rpc/pmap_clnt.h>
54 #include <rpc/pmap_prot.h>
55 #include <rpc/pmap_rmt.h>
56 #include <unistd.h>
57 #include <rpcsvc/yp_prot.h>
58 #include <rpcsvc/ypclnt.h>
59 
60 #ifndef BINDINGDIR
61 #define BINDINGDIR "/var/yp/binding"
62 #endif
63 
64 struct _dom_binding {
65 	struct _dom_binding *dom_pnext;
66 	char dom_domain[YPMAXDOMAIN + 1];
67 	struct sockaddr_in dom_server_addr;
68 	unsigned short int dom_server_port;
69 	int dom_socket;
70 	CLIENT *dom_client;
71 	long int dom_vers;
72 	time_t dom_check_t;
73 	int dom_lockfd;
74 	int dom_alive;
75 };
76 
77 extern bool_t xdr_domainname(), xdr_ypbind_resp();
78 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
79 extern bool_t xdr_ypbind_setdom();
80 
81 char *domainname;
82 
83 struct _dom_binding *ypbindlist;
84 int check;
85 
86 #define YPSET_NO	0
87 #define YPSET_LOCAL	1
88 #define YPSET_ALL	2
89 int ypsetmode = YPSET_NO;
90 
91 int rpcsock;
92 struct rmtcallargs rmtca;
93 struct rmtcallres rmtcr;
94 char rmtcr_outval;
95 u_long rmtcr_port;
96 SVCXPRT *udptransp, *tcptransp;
97 
98 void *
99 ypbindproc_null_2(transp, argp, clnt)
100 SVCXPRT *transp;
101 void *argp;
102 CLIENT *clnt;
103 {
104 	static char res;
105 
106 	bzero((char *)&res, sizeof(res));
107 	return (void *)&res;
108 }
109 
110 struct ypbind_resp *
111 ypbindproc_domain_2(transp, argp, clnt)
112 SVCXPRT *transp;
113 char *argp;
114 CLIENT *clnt;
115 {
116 	static struct ypbind_resp res;
117 	struct _dom_binding *ypdb;
118 	char path[MAXPATHLEN];
119 
120 	bzero((char *)&res, sizeof res);
121 	res.ypbind_status = YPBIND_FAIL_VAL;
122 
123 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
124 		if( strcmp(ypdb->dom_domain, argp) == 0)
125 			break;
126 
127 	if(ypdb==NULL) {
128 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
129 		bzero((char *)ypdb, sizeof *ypdb);
130 		strncpy(ypdb->dom_domain, argp, sizeof ypdb->dom_domain);
131 		ypdb->dom_vers = YPVERS;
132 		ypdb->dom_alive = 0;
133 		ypdb->dom_lockfd = -1;
134 		sprintf(path, "%s/%s.%d", BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
135 		unlink(path);
136 		ypdb->dom_pnext = ypbindlist;
137 		ypbindlist = ypdb;
138 		check++;
139 		return NULL;
140 	}
141 
142 	if(ypdb->dom_alive==0)
143 		return NULL;
144 
145 #if 0
146 	delta = ypdb->dom_check_t - ypdb->dom_ask_t;
147 	if( !(ypdb->dom_ask_t==0 || delta > 5)) {
148 		ypdb->dom_ask_t = time(NULL);
149 		/*
150 		 * Hmm. More than 2 requests in 5 seconds have indicated that my
151 		 * binding is possibly incorrect. Ok, make myself unalive, and
152 		 * find out what the actual state is.
153 		 */
154 		if(ypdb->dom_lockfd!=-1)
155 			close(ypdb->dom_lockfd);
156 		ypdb->dom_lockfd = -1;
157 		ypdb->dom_alive = 0;
158 		ypdb->dom_lockfd = -1;
159 		sprintf(path, "%s/%s.%d", BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
160 		unlink(path);
161 		check++;
162 		return NULL;
163 	}
164 #endif
165 
166 answer:
167 	res.ypbind_status = YPBIND_SUCC_VAL;
168 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
169 		ypdb->dom_server_addr.sin_addr.s_addr;
170 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
171 		ypdb->dom_server_port;
172 	/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
173 		inet_ntoa(ypdb->dom_server_addr.sin_addr),
174 		ntohs(ypdb->dom_server_addr.sin_port));*/
175 	return &res;
176 }
177 
178 bool_t *
179 ypbindproc_setdom_2(transp, argp, clnt)
180 SVCXPRT *transp;
181 struct ypbind_setdom *argp;
182 CLIENT *clnt;
183 {
184 	struct sockaddr_in *fromsin, bindsin;
185 	static char res;
186 
187 	bzero((char *)&res, sizeof(res));
188 	fromsin = svc_getcaller(transp);
189 
190 	switch(ypsetmode) {
191 	case YPSET_LOCAL:
192 		if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
193 			return (void *)NULL;
194 		break;
195 	case YPSET_ALL:
196 		break;
197 	case YPSET_NO:
198 	default:
199 		return (void *)NULL;
200 	}
201 
202 	if(ntohs(fromsin->sin_port) >= IPPORT_RESERVED)
203 		return (void *)&res;
204 
205 	if(argp->ypsetdom_vers != YPVERS)
206 		return (void *)&res;
207 
208 	bzero((char *)&bindsin, sizeof bindsin);
209 	bindsin.sin_family = AF_INET;
210 	bindsin.sin_addr.s_addr = argp->ypsetdom_addr.s_addr;
211 	bindsin.sin_port = argp->ypsetdom_port;
212 	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
213 
214 	res = 1;
215 	return (void *)&res;
216 }
217 
218 static void
219 ypbindprog_2(rqstp, transp)
220 struct svc_req *rqstp;
221 register SVCXPRT *transp;
222 {
223 	union {
224 		char ypbindproc_domain_2_arg[MAXHOSTNAMELEN];
225 		struct ypbind_setdom ypbindproc_setdom_2_arg;
226 	} argument;
227 	struct authunix_parms *creds;
228 	char *result;
229 	bool_t (*xdr_argument)(), (*xdr_result)();
230 	char *(*local)();
231 
232 	switch (rqstp->rq_proc) {
233 	case YPBINDPROC_NULL:
234 		xdr_argument = xdr_void;
235 		xdr_result = xdr_void;
236 		local = (char *(*)()) ypbindproc_null_2;
237 		break;
238 
239 	case YPBINDPROC_DOMAIN:
240 		xdr_argument = xdr_domainname;
241 		xdr_result = xdr_ypbind_resp;
242 		local = (char *(*)()) ypbindproc_domain_2;
243 		break;
244 
245 	case YPBINDPROC_SETDOM:
246 		switch(rqstp->rq_cred.oa_flavor) {
247 		case AUTH_UNIX:
248 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
249 			if( creds->aup_uid != 0) {
250 				svcerr_auth(transp, AUTH_BADCRED);
251 				return;
252 			}
253 			break;
254 		default:
255 			svcerr_auth(transp, AUTH_TOOWEAK);
256 			return;
257 		}
258 
259 		xdr_argument = xdr_ypbind_setdom;
260 		xdr_result = xdr_void;
261 		local = (char *(*)()) ypbindproc_setdom_2;
262 		break;
263 
264 	default:
265 		svcerr_noproc(transp);
266 		return;
267 	}
268 	bzero((char *)&argument, sizeof(argument));
269 	if (!svc_getargs(transp, xdr_argument, &argument)) {
270 		svcerr_decode(transp);
271 		return;
272 	}
273 	result = (*local)(transp, &argument, rqstp);
274 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
275 		svcerr_systemerr(transp);
276 	}
277 	return;
278 }
279 
280 main(argc, argv)
281 char **argv;
282 {
283 	char path[MAXPATHLEN];
284 	struct timeval tv;
285 	fd_set fdsr;
286 	int width;
287 	int i;
288 
289 	yp_get_default_domain(&domainname);
290 	if( domainname[0] == '\0') {
291 		fprintf(stderr, "domainname not set. Aborting.\n");
292 		exit(1);
293 	}
294 
295 	for(i=1; i<argc; i++) {
296 		if( strcmp("-ypset", argv[i]) == 0)
297 			ypsetmode = YPSET_ALL;
298 		else if (strcmp("-ypsetme", argv[i]) == 0)
299 			ypsetmode = YPSET_LOCAL;
300 	}
301 
302 	/* blow away everything in BINDINGDIR */
303 
304 
305 
306 #ifdef DAEMON
307 	switch(fork()) {
308 	case 0:
309 		break;
310 	case -1:
311 		perror("fork");
312 		exit(1);
313 	default:
314 		exit(0);
315 	}
316 	setsid();
317 #endif
318 
319 	pmap_unset(YPBINDPROG, YPBINDVERS);
320 
321 	udptransp = svcudp_create(RPC_ANYSOCK);
322 	if (udptransp == NULL) {
323 		fprintf(stderr, "cannot create udp service.");
324 		exit(1);
325 	}
326 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
327 	    IPPROTO_UDP)) {
328 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).");
329 		exit(1);
330 	}
331 
332 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
333 	if (tcptransp == NULL) {
334 		fprintf(stderr, "cannot create tcp service.");
335 		exit(1);
336 	}
337 
338 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
339 	    IPPROTO_TCP)) {
340 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).");
341 		exit(1);
342 	}
343 
344 	if( (rpcsock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
345 		perror("socket");
346 		return -1;
347 	}
348 
349 	fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
350 	i = 1;
351 	setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
352 	rmtca.prog = YPPROG;
353 	rmtca.vers = YPVERS;
354 	rmtca.proc = YPPROC_DOMAIN_NONACK;
355 	rmtca.xdr_args = NULL;		/* set at call time */
356 	rmtca.args_ptr = NULL;		/* set at call time */
357 	rmtcr.port_ptr = &rmtcr_port;
358 	rmtcr.xdr_results = xdr_bool;
359 	rmtcr.results_ptr = (caddr_t)&rmtcr_outval;
360 
361 	/* build initial domain binding, make it "unsuccessful" */
362 	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
363 	bzero((char *)ypbindlist, sizeof *ypbindlist);
364 	strncpy(ypbindlist->dom_domain, domainname, sizeof ypbindlist->dom_domain);
365 	ypbindlist->dom_vers = YPVERS;
366 	ypbindlist->dom_alive = 0;
367 	ypbindlist->dom_lockfd = -1;
368 	sprintf(path, "%s/%s.%d", BINDINGDIR, ypbindlist->dom_domain,
369 		ypbindlist->dom_vers);
370 	(void)unlink(path);
371 
372 	width = getdtablesize();
373 	while(1) {
374 		fdsr = svc_fdset;
375 		FD_SET(rpcsock, &fdsr);
376 		tv.tv_sec = 1;
377 		tv.tv_usec = 0;
378 
379 		switch(select(width, &fdsr, NULL, NULL, &tv)) {
380 		case 0:
381 			checkwork();
382 			break;
383 		case -1:
384 			perror("select\n");
385 			break;
386 		default:
387 			if(FD_ISSET(rpcsock, &fdsr)) {
388 				FD_CLR(rpcsock, &fdsr);
389 				handle_replies();
390 			}
391 			svc_getreqset(&fdsr);
392 			if(check)
393 				checkwork();
394 			break;
395 		}
396 	}
397 }
398 
399 /*
400  * change to do something like this:
401  *
402  * STATE	TIME		ACTION		NEWTIME	NEWSTATE
403  * no binding	t==*		broadcast 	t=2	no binding
404  * binding	t==60		check server	t=10	binding
405  * binding	t=10		broadcast	t=2	no binding
406  */
407 checkwork()
408 {
409 	struct _dom_binding *ypdb;
410 	time_t t;
411 
412 	check = 0;
413 
414 	time(&t);
415 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
416 		if(ypdb->dom_alive==0 || ypdb->dom_check_t < t) {
417 			broadcast(ypdb->dom_domain);
418 			time(&t);
419 			ypdb->dom_check_t = t + 5;
420 		}
421 	}
422 }
423 
424 broadcast(dom)
425 char *dom;
426 {
427 	struct rpc_msg rpcmsg;
428 	char buf[1400], inbuf[8192];
429 	enum clnt_stat st;
430 	struct timeval tv;
431 	int outlen, i, sock, len;
432 	struct sockaddr_in bsin;
433 	struct ifconf ifc;
434 	struct ifreq ifreq, *ifr;
435 	struct in_addr in;
436 	AUTH *rpcua;
437 	XDR rpcxdr;
438 
439 	rmtca.xdr_args = xdr_domainname;
440 	rmtca.args_ptr = dom;
441 
442 	bzero((char *)&bsin, sizeof bsin);
443 	bsin.sin_family = AF_INET;
444 	bsin.sin_port = htons(PMAPPORT);
445 
446 	bzero((char *)&rpcxdr, sizeof rpcxdr);
447 	bzero((char *)&rpcmsg, sizeof rpcmsg);
448 
449 	rpcua = authunix_create_default();
450 	if( rpcua == (AUTH *)NULL) {
451 		/*printf("cannot get unix auth\n");*/
452 		return RPC_SYSTEMERROR;
453 	}
454 	rpcmsg.rm_direction = CALL;
455 	rpcmsg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
456 	rpcmsg.rm_call.cb_prog = PMAPPROG;
457 	rpcmsg.rm_call.cb_vers = PMAPVERS;
458 	rpcmsg.rm_call.cb_proc = PMAPPROC_CALLIT;
459 	rpcmsg.rm_call.cb_cred = rpcua->ah_cred;
460 	rpcmsg.rm_call.cb_verf = rpcua->ah_verf;
461 
462 	gettimeofday(&tv, (struct timezone *)0);
463 	rpcmsg.rm_xid = (int)dom;
464 	tv.tv_usec = 0;
465 	xdrmem_create(&rpcxdr, buf, sizeof buf, XDR_ENCODE);
466 	if( (!xdr_callmsg(&rpcxdr, &rpcmsg)) ) {
467 		st = RPC_CANTENCODEARGS;
468 		AUTH_DESTROY(rpcua);
469 		return st;
470 	}
471 	if( (!xdr_rmtcall_args(&rpcxdr, &rmtca)) ) {
472 		st = RPC_CANTENCODEARGS;
473 		AUTH_DESTROY(rpcua);
474 		return st;
475 	}
476 	outlen = (int)xdr_getpos(&rpcxdr);
477 	xdr_destroy(&rpcxdr);
478 	if(outlen<1) {
479 		AUTH_DESTROY(rpcua);
480 		return st;
481 	}
482 	AUTH_DESTROY(rpcua);
483 
484 	/* find all networks and send the RPC packet out them all */
485 	if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
486 		perror("socket");
487 		return -1;
488 	}
489 
490 	ifc.ifc_len = sizeof inbuf;
491 	ifc.ifc_buf = inbuf;
492 	if( ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
493 		close(sock);
494 		perror("ioctl(SIOCGIFCONF)");
495 		return -1;
496 	}
497 	ifr = ifc.ifc_req;
498 	ifreq.ifr_name[0] = '\0';
499 	for(i=0; i<ifc.ifc_len; i+=len, ifr=(struct ifreq *)((caddr_t)ifr+len)) {
500 #if defined(BSD) && BSD >= 199103
501 		len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
502 #else
503 		len = sizeof ifc.ifc_len / sizeof(struct ifreq);
504 #endif
505 		ifreq = *ifr;
506 		if( ifreq.ifr_addr.sa_family != AF_INET)
507 			continue;
508 		if( ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
509 			perror("ioctl(SIOCGIFFLAGS)");
510 			continue;
511 		}
512 		if( (ifreq.ifr_flags & IFF_UP) == 0)
513 			continue;
514 
515 		ifreq.ifr_flags &= (IFF_LOOPBACK | IFF_BROADCAST);
516 		if( ifreq.ifr_flags==IFF_BROADCAST ) {
517 			if( ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0 ) {
518 				perror("ioctl(SIOCGIFBRDADDR)");
519 				continue;
520 			}
521 		} else if( ifreq.ifr_flags==IFF_LOOPBACK ) {
522 			if( ioctl(sock, SIOCGIFADDR, &ifreq) < 0 ) {
523 				perror("ioctl(SIOCGIFADDR)");
524 				continue;
525 			}
526 		} else
527 			continue;
528 
529 		in = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
530 		bsin.sin_addr = in;
531 		if( sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bsin,
532 		   sizeof bsin) < 0 )
533 			perror("sendto");
534 	}
535 	close(sock);
536 	return 0;
537 }
538 
539 /*enum clnt_stat*/
540 handle_replies()
541 {
542 	char buf[1400];
543 	int fromlen, inlen;
544 	struct sockaddr_in raddr;
545 	struct rpc_msg msg;
546 	XDR xdr;
547 
548 recv_again:
549 	bzero((char *)&xdr, sizeof(xdr));
550 	bzero((char *)&msg, sizeof(msg));
551 	msg.acpted_rply.ar_verf = _null_auth;
552 	msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr;
553 	msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
554 
555 try_again:
556 	fromlen = sizeof (struct sockaddr);
557 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
558 		(struct sockaddr *)&raddr, &fromlen);
559 	if(inlen<0) {
560 		if(errno==EINTR)
561 			goto try_again;
562 		return RPC_CANTRECV;
563 	}
564 	if(inlen<sizeof(u_long))
565 		goto recv_again;
566 
567 	/*
568 	 * see if reply transaction id matches sent id.
569 	 * If so, decode the results.
570 	 */
571 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
572 	if( xdr_replymsg(&xdr, &msg)) {
573 		if( (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
574 		   (msg.acpted_rply.ar_stat == SUCCESS)) {
575 			raddr.sin_port = htons((u_short)rmtcr_port);
576 			rpc_received(msg.rm_xid, &raddr, 0);
577 		}
578 	}
579 	xdr.x_op = XDR_FREE;
580 	msg.acpted_rply.ar_results.proc = xdr_void;
581 	xdr_destroy(&xdr);
582 
583 	return RPC_SUCCESS;
584 }
585 
586 /*
587  * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
588  */
589 rpc_received(dom, raddrp, force)
590 char *dom;
591 struct sockaddr_in *raddrp;
592 int force;
593 {
594 	struct _dom_binding *ypdb;
595 	struct iovec iov[2];
596 	struct ypbind_resp ybr;
597 	char path[MAXPATHLEN];
598 	int fd;
599 
600 	/*printf("returned from %s about %s\n", inet_ntoa(raddrp->sin_addr), dom);*/
601 
602 	if(dom==NULL)
603 		return;
604 
605 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
606 		if( strcmp(ypdb->dom_domain, dom) == 0)
607 			break;
608 
609 	if(ypdb==NULL) {
610 		if(force==0)
611 			return;
612 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
613 		bzero((char *)ypdb, sizeof *ypdb);
614 		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
615 		ypdb->dom_lockfd = -1;
616 		ypdb->dom_pnext = ypbindlist;
617 		ypbindlist = ypdb;
618 	}
619 
620 	/* soft update, alive, less than 30 seconds old */
621 	if(ypdb->dom_alive==1 && force==0 && ypdb->dom_check_t<time(NULL)+30)
622 		return;
623 
624 	bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
625 		sizeof ypdb->dom_server_addr);
626 	ypdb->dom_check_t = time(NULL) + 60;	/* recheck binding in 60 seconds */
627 	ypdb->dom_vers = YPVERS;
628 	ypdb->dom_alive = 1;
629 
630 	if(ypdb->dom_lockfd != -1)
631 		close(ypdb->dom_lockfd);
632 
633 	sprintf(path, "%s/%s.%d", BINDINGDIR,
634 		ypdb->dom_domain, ypdb->dom_vers);
635 #ifdef O_SHLOCK
636 	if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
637 		(void)mkdir(BINDINGDIR, 0755);
638 		if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
639 			return;
640 	}
641 #else
642 	if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
643 		(void)mkdir(BINDINGDIR, 0755);
644 		if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
645 			return;
646 	}
647 	flock(fd, LOCK_SH);
648 #endif
649 
650 	/*
651 	 * ok, if BINDINGDIR exists, and we can create the binding file,
652 	 * then write to it..
653 	 */
654 	ypdb->dom_lockfd = fd;
655 
656 	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
657 	iov[0].iov_len = sizeof udptransp->xp_port;
658 	iov[1].iov_base = (caddr_t)&ybr;
659 	iov[1].iov_len = sizeof ybr;
660 
661 	bzero(&ybr, sizeof ybr);
662 	ybr.ypbind_status = YPBIND_SUCC_VAL;
663 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr;
664 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
665 
666 	if( writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
667 		perror("write");
668 		close(ypdb->dom_lockfd);
669 		ypdb->dom_lockfd = -1;
670 		return;
671 	}
672 }
673