xref: /freebsd/usr.sbin/ypbind/ypbind.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
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.16 1995/07/15 23:27:27 wpaul Exp $";
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <sys/ioctl.h>
38 #include <sys/signal.h>
39 #include <sys/socket.h>
40 #include <sys/file.h>
41 #include <sys/fcntl.h>
42 #include <sys/stat.h>
43 #include <sys/uio.h>
44 #include <syslog.h>
45 #include <stdio.h>
46 #include <errno.h>
47 #include <ctype.h>
48 #include <dirent.h>
49 #include <netdb.h>
50 #include <string.h>
51 #include <rpc/rpc.h>
52 #include <rpc/xdr.h>
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpc/pmap_prot.h>
58 #include <rpc/pmap_rmt.h>
59 #include <unistd.h>
60 #include <stdlib.h>
61 #include <rpcsvc/yp_prot.h>
62 #include <rpcsvc/ypclnt.h>
63 
64 #ifndef BINDINGDIR
65 #define BINDINGDIR "/var/yp/binding"
66 #endif
67 
68 #ifndef YPBINDLOCK
69 #define YPBINDLOCK "/var/run/ypbind.lock"
70 #endif
71 
72 struct _dom_binding {
73 	struct _dom_binding *dom_pnext;
74 	char dom_domain[YPMAXDOMAIN + 1];
75 	struct sockaddr_in dom_server_addr;
76 	long int dom_vers;
77 	int dom_lockfd;
78 	int dom_alive;
79 	int dom_broadcast_pid;
80 	int dom_pipe_fds[2];
81 	int dom_default;
82 };
83 
84 #define READFD ypdb->dom_pipe_fds[0]
85 #define WRITEFD ypdb->dom_pipe_fds[1]
86 #define BROADFD broad_domain->dom_pipe_fds[1]
87 
88 extern bool_t xdr_domainname(), xdr_ypbind_resp();
89 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
90 extern bool_t xdr_ypbind_setdom();
91 
92 void	checkwork __P((void));
93 void	*ypbindproc_null_2 __P((SVCXPRT *, void *, CLIENT *));
94 void	*ypbindproc_setdom_2 __P((SVCXPRT *, struct ypbind_setdom *, CLIENT *));
95 void	rpc_received __P((char *, struct sockaddr_in *, int ));
96 void	broadcast __P((struct _dom_binding *));
97 int	ping __P((struct _dom_binding *));
98 int	tell_parent __P((char *, struct sockaddr_in *));
99 void	handle_children __P(( struct _dom_binding * ));
100 void	reaper __P((int));
101 void	terminate __P((int));
102 void	yp_restricted_mode __P((char *));
103 int	verify __P((struct in_addr));
104 
105 char *domainname;
106 struct _dom_binding *ypbindlist;
107 static struct _dom_binding *broad_domain;
108 
109 #define YPSET_NO	0
110 #define YPSET_LOCAL	1
111 #define YPSET_ALL	2
112 int ypsetmode = YPSET_NO;
113 int ypsecuremode = 0;
114 
115 /*
116  * Special restricted mode variables: when in restricted mode, only the
117  * specified restricted_domain will be bound, and only the servers listed
118  * in restricted_addrs will be used for binding.
119  */
120 #define RESTRICTED_SERVERS 10
121 int yp_restricted = 0;
122 struct in_addr restricted_addrs[RESTRICTED_SERVERS];
123 
124 /* No more than MAX_CHILDREN child broadcasters at a time. */
125 #ifndef MAX_CHILDREN
126 #define MAX_CHILDREN 5
127 #endif
128 /* No more than MAX_DOMAINS simultaneous domains */
129 #ifndef MAX_DOMAINS
130 #define MAX_DOMAINS 200
131 #endif
132 /* RPC timeout value */
133 #ifndef FAIL_THRESHOLD
134 #define FAIL_THRESHOLD 10
135 #endif
136 
137 /* Number of times to fish for a response froma particular set of hosts */
138 #ifndef MAX_RETRIES
139 #define MAX_RETRIES 30
140 #endif
141 
142 int retries = 0;
143 int children = 0;
144 int domains = 0;
145 int yplockfd;
146 fd_set fdsr;
147 
148 SVCXPRT *udptransp, *tcptransp;
149 
150 void *
151 ypbindproc_null_2(transp, argp, clnt)
152 SVCXPRT *transp;
153 void *argp;
154 CLIENT *clnt;
155 {
156 	static char res;
157 
158 	bzero((char *)&res, sizeof(res));
159 	return (void *)&res;
160 }
161 
162 struct ypbind_resp *
163 ypbindproc_domain_2(transp, argp, clnt)
164 SVCXPRT *transp;
165 char *argp;
166 CLIENT *clnt;
167 {
168 	static struct ypbind_resp res;
169 	struct _dom_binding *ypdb;
170 	char path[MAXPATHLEN];
171 
172 	bzero((char *)&res, sizeof res);
173 	res.ypbind_status = YPBIND_FAIL_VAL;
174 	res.ypbind_respbody.ypbind_error = YPBIND_ERR_NOSERV;
175 
176 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
177 		if( strcmp(ypdb->dom_domain, argp) == 0)
178 			break;
179 		}
180 
181 	if(ypdb==NULL) {
182 		if (yp_restricted) {
183 			syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", argp);
184 			return &res;
185 		}
186 
187 		if (domains >= MAX_DOMAINS) {
188 			syslog(LOG_WARNING, "domain limit (%d) exceeded",
189 							MAX_DOMAINS);
190 			res.ypbind_respbody.ypbind_error = YPBIND_ERR_RESC;
191 			return &res;
192 		}
193 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
194 		if (ypdb == NULL) {
195 			syslog(LOG_WARNING, "malloc: %s", strerror(errno));
196 			res.ypbind_respbody.ypbind_error = YPBIND_ERR_RESC;
197 			return &res;
198 		}
199 		bzero((char *)ypdb, sizeof *ypdb);
200 		strncpy(ypdb->dom_domain, argp, sizeof ypdb->dom_domain);
201 		ypdb->dom_vers = YPVERS;
202 		ypdb->dom_alive = 0;
203 		ypdb->dom_default = 0;
204 		ypdb->dom_lockfd = -1;
205 		sprintf(path, "%s/%s.%ld", BINDINGDIR,
206 					ypdb->dom_domain, ypdb->dom_vers);
207 		unlink(path);
208 		ypdb->dom_pnext = ypbindlist;
209 		ypbindlist = ypdb;
210 		domains++;
211 	}
212 
213 	if (ping(ypdb)) {
214 		return &res;
215 	}
216 
217 	res.ypbind_status = YPBIND_SUCC_VAL;
218 	res.ypbind_respbody.ypbind_error = 0; /* Success */
219 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
220 		ypdb->dom_server_addr.sin_addr.s_addr;
221 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
222 		ypdb->dom_server_addr.sin_port;
223 	/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
224 		inet_ntoa(ypdb->dom_server_addr.sin_addr),
225 		ntohs(ypdb->dom_server_addr.sin_port));*/
226 	return &res;
227 }
228 
229 void *
230 ypbindproc_setdom_2(transp, argp, clnt)
231 SVCXPRT *transp;
232 struct ypbind_setdom *argp;
233 CLIENT *clnt;
234 {
235 	struct sockaddr_in *fromsin, bindsin;
236 
237 	fromsin = svc_getcaller(transp);
238 
239 	switch(ypsetmode) {
240 	case YPSET_LOCAL:
241 		if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
242 			svcerr_noprog(transp);
243 			return;
244 		}
245 		break;
246 	case YPSET_ALL:
247 		break;
248 	case YPSET_NO:
249 	default:
250 		svcerr_noprog(transp);
251 		return;
252 	}
253 
254 	if(ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
255 		svcerr_noprog(transp);
256 		return;
257 	}
258 
259 	if(argp->ypsetdom_vers != YPVERS) {
260 		svcerr_noprog(transp);
261 		return;
262 	}
263 
264 	bzero((char *)&bindsin, sizeof bindsin);
265 	bindsin.sin_family = AF_INET;
266 	bindsin.sin_addr.s_addr = argp->ypsetdom_addr.s_addr;
267 	bindsin.sin_port = argp->ypsetdom_port;
268 	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
269 
270 	return;
271 }
272 
273 static void
274 ypbindprog_2(rqstp, transp)
275 struct svc_req *rqstp;
276 register SVCXPRT *transp;
277 {
278 	union {
279 		char ypbindproc_domain_2_arg[MAXHOSTNAMELEN];
280 		struct ypbind_setdom ypbindproc_setdom_2_arg;
281 	} argument;
282 	struct authunix_parms *creds;
283 	char *result;
284 	bool_t (*xdr_argument)(), (*xdr_result)();
285 	char *(*local)();
286 
287 	switch (rqstp->rq_proc) {
288 	case YPBINDPROC_NULL:
289 		xdr_argument = xdr_void;
290 		xdr_result = xdr_void;
291 		local = (char *(*)()) ypbindproc_null_2;
292 		break;
293 
294 	case YPBINDPROC_DOMAIN:
295 		xdr_argument = xdr_domainname;
296 		xdr_result = xdr_ypbind_resp;
297 		local = (char *(*)()) ypbindproc_domain_2;
298 		break;
299 
300 	case YPBINDPROC_SETDOM:
301 		switch(rqstp->rq_cred.oa_flavor) {
302 		case AUTH_UNIX:
303 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
304 			if( creds->aup_uid != 0) {
305 				svcerr_auth(transp, AUTH_BADCRED);
306 				return;
307 			}
308 			break;
309 		default:
310 			svcerr_auth(transp, AUTH_TOOWEAK);
311 			return;
312 		}
313 
314 		xdr_argument = xdr_ypbind_setdom;
315 		xdr_result = xdr_void;
316 		local = (char *(*)()) ypbindproc_setdom_2;
317 		break;
318 
319 	default:
320 		svcerr_noproc(transp);
321 		return;
322 	}
323 	bzero((char *)&argument, sizeof(argument));
324 	if (!svc_getargs(transp, xdr_argument, &argument)) {
325 		svcerr_decode(transp);
326 		return;
327 	}
328 	result = (*local)(transp, &argument, rqstp);
329 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
330 		svcerr_systemerr(transp);
331 	}
332 	return;
333 }
334 
335 /* Jack the reaper */
336 void reaper(sig)
337 int sig;
338 {
339 	int st;
340 
341 	while(wait3(&st, WNOHANG, NULL) > 0)
342 		children--;
343 }
344 
345 void terminate(sig)
346 int sig;
347 {
348 	struct _dom_binding *ypdb;
349 	char path[MAXPATHLEN];
350 
351 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
352 		close(ypdb->dom_lockfd);
353 		if (ypdb->dom_broadcast_pid)
354 			kill(ypdb->dom_broadcast_pid, SIGINT);
355 		sprintf(path, "%s/%s.%ld", BINDINGDIR,
356 			ypdb->dom_domain, ypdb->dom_vers);
357 		unlink(path);
358 	}
359 	close(yplockfd);
360 	unlink(YPBINDLOCK);
361 	pmap_unset(YPBINDPROG, YPBINDVERS);
362 	exit(0);
363 }
364 
365 void
366 main(argc, argv)
367 int argc;
368 char **argv;
369 {
370 	char path[MAXPATHLEN];
371 	struct timeval tv;
372 	int i;
373 	DIR *dird;
374 	struct dirent *dirp;
375 	struct _dom_binding *ypdb;
376 
377 	/* Check that another ypbind isn't already running. */
378 	if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) {
379 		perror(YPBINDLOCK);
380 		exit(1);
381 	}
382 
383 	if(flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
384 		fprintf (stderr, "Another ypbind is already running. Aborting.\n");
385 		exit(1);
386 	}
387 
388 	/* XXX domainname will be overriden if we use restricted mode */
389 	yp_get_default_domain(&domainname);
390 	if( domainname[0] == '\0') {
391 		fprintf(stderr, "domainname not set. Aborting.\n");
392 		exit(1);
393 	}
394 
395 	for(i=1; i<argc; i++) {
396 		if( strcmp("-ypset", argv[i]) == 0)
397 			ypsetmode = YPSET_ALL;
398 		else if (strcmp("-ypsetme", argv[i]) == 0)
399 		        ypsetmode = YPSET_LOCAL;
400 		else if (strcmp("-s", argv[i]) == 0)
401 		        ypsecuremode++;
402 		else if (strcmp("-S", argv[i]) == 0 && argc > i)
403 			yp_restricted_mode(argv[i+1]);
404 	}
405 
406 	/* blow away everything in BINDINGDIR (if it exists) */
407 
408 	if ((dird = opendir(BINDINGDIR)) != NULL) {
409 		char path[MAXPATHLEN];
410 		while ((dirp = readdir(dird)) != NULL)
411 			if (strcmp(dirp->d_name, ".") &&
412 			    strcmp(dirp->d_name, "..")) {
413 				sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
414 				unlink(path);
415 			}
416 		closedir(dird);
417 	}
418 
419 #ifdef DAEMON
420 	if (daemon(0,0)) {
421 		perror("fork");
422 		exit(1);
423 	}
424 #endif
425 
426 	pmap_unset(YPBINDPROG, YPBINDVERS);
427 
428 	udptransp = svcudp_create(RPC_ANYSOCK);
429 	if (udptransp == NULL) {
430 		fprintf(stderr, "cannot create udp service.\n");
431 		exit(1);
432 	}
433 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
434 	    IPPROTO_UDP)) {
435 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).\n");
436 		exit(1);
437 	}
438 
439 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
440 	if (tcptransp == NULL) {
441 		fprintf(stderr, "cannot create tcp service.\n");
442 		exit(1);
443 	}
444 
445 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
446 	    IPPROTO_TCP)) {
447 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).\n");
448 		exit(1);
449 	}
450 
451 	/* build initial domain binding, make it "unsuccessful" */
452 	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
453 	if (ypbindlist == NULL) {
454 		perror("malloc");
455 		exit(1);
456 	}
457 	bzero((char *)ypbindlist, sizeof *ypbindlist);
458 	strncpy(ypbindlist->dom_domain, domainname, sizeof ypbindlist->dom_domain);
459 	ypbindlist->dom_vers = YPVERS;
460 	ypbindlist->dom_alive = 0;
461 	ypbindlist->dom_lockfd = -1;
462 	ypbindlist->dom_default = 1;
463 	domains++;
464 
465 	signal(SIGCHLD, reaper);
466 	signal(SIGTERM, terminate);
467 
468 	openlog(argv[0], LOG_PID, LOG_DAEMON);
469 
470 	/* Kick off the default domain */
471 	broadcast(ypbindlist);
472 
473 	while(1) {
474 		fdsr = svc_fdset;
475 
476 		tv.tv_sec = 60;
477 		tv.tv_usec = 0;
478 
479 		switch(select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
480 		case 0:
481 			checkwork();
482 			break;
483 		case -1:
484 			if (errno != EINTR)
485 				syslog(LOG_WARNING, "select: %s", strerror(errno));
486 			break;
487 		default:
488 			for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
489 				if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
490 					handle_children(ypdb);
491 					if (children == (MAX_CHILDREN - 1))
492 						checkwork();
493 				}
494 			}
495 			svc_getreqset(&fdsr);
496 			break;
497 		}
498 	}
499 }
500 
501 void
502 checkwork()
503 {
504 	struct _dom_binding *ypdb;
505 
506 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
507 		ping(ypdb);
508 }
509 
510 /* The clnt_broadcast() callback mechanism sucks. */
511 
512 /*
513  * Receive results from broadcaster. Don't worry about passing
514  * bogus info to rpc_received() -- it can handle it. Note that we
515  * must be sure to invalidate the dom_pipe_fds descriptors here:
516  * since descriptors can be re-used, we have to make sure we
517  * don't mistake one of the RPC descriptors for one of the pipes.
518  * What's weird is that forgetting to invalidate the pipe descriptors
519  * doesn't always result in an error (otherwise I would have caught
520  * the mistake much sooner), even though logically it should.
521  */
522 void handle_children(ypdb)
523 struct _dom_binding *ypdb;
524 {
525 	char buf[YPMAXDOMAIN + 1];
526 	struct sockaddr_in addr;
527 
528 	if (read(READFD, &buf, sizeof(buf)) < 0)
529 		syslog(LOG_WARNING, "could not read from child: %s", strerror(errno));
530 	if (read(READFD, &addr, sizeof(struct sockaddr_in)) < 0)
531 		syslog(LOG_WARNING, "could not read from child: %s", strerror(errno));
532 
533 	close(READFD);
534 	FD_CLR(READFD, &fdsr);
535 	FD_CLR(READFD, &svc_fdset);
536 	READFD = WRITEFD = -1;
537 	rpc_received((char *)&buf, &addr, 0);
538 }
539 
540 /*
541  * Send our dying words back to our parent before we perish.
542  */
543 int
544 tell_parent(dom, addr)
545 char *dom;
546 struct sockaddr_in *addr;
547 {
548 	char buf[YPMAXDOMAIN + 1];
549 	struct timeval timeout;
550 	fd_set fds;
551 
552 	timeout.tv_sec = 5;
553 	timeout.tv_usec = 0;
554 
555 	sprintf(buf, "%s", broad_domain->dom_domain);
556 	if (write(BROADFD, &buf, sizeof(buf)) < 0)
557 		return(1);
558 
559 	/*
560 	 * Stay in sync with parent: wait for it to read our first
561 	 * message before sending the second.
562 	 */
563 
564 	FD_ZERO(&fds);
565 	FD_SET(BROADFD, &fds);
566 	if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
567 		return(1);
568 	if (FD_ISSET(BROADFD, &fds)) {
569 		if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
570 			return(1);
571 	} else {
572 		return(1);
573 	}
574 
575 	close(BROADFD);
576 	return (0);
577 }
578 
579 bool_t broadcast_result(out, addr)
580 bool_t *out;
581 struct sockaddr_in *addr;
582 {
583 	if (retries >= MAX_RETRIES) {
584 		bzero((char *)addr, sizeof(struct sockaddr_in));
585 		if (tell_parent(broad_domain->dom_domain, addr))
586 			syslog(LOG_WARNING, "lost connection to parent");
587 		return TRUE;
588 	}
589 
590 	if (yp_restricted && verify(addr->sin_addr)) {
591 		retries++;
592 		syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
593 		return FALSE;
594 	} else {
595 		if (tell_parent(broad_domain->dom_domain, addr))
596 			syslog(LOG_WARNING, "lost connection to parent");
597 		return TRUE;
598 	}
599 }
600 
601 /*
602  * The right way to send RPC broadcasts.
603  * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
604  * blocks while waiting for replies, so we have to fork off seperate
605  * broadcaster processes that do the waiting and then transmit their
606  * results back to the parent for processing. We also have to remember
607  * to save the name of the domain we're trying to bind in a global
608  * variable since clnt_broadcast() provides no way to pass things to
609  * the 'eachresult' callback function.
610  */
611 void
612 broadcast(ypdb)
613 struct _dom_binding *ypdb;
614 {
615 	bool_t out = FALSE;
616 	enum clnt_stat stat;
617 
618 	if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
619 		return;
620 
621 	if (pipe(ypdb->dom_pipe_fds) < 0) {
622 		syslog(LOG_WARNING, "pipe: %s",strerror(errno));
623 		return;
624 	}
625 
626 	if (ypdb->dom_vers = -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
627 		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
628 		inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
629 
630 	broad_domain = ypdb;
631 	flock(ypdb->dom_lockfd, LOCK_UN);
632 
633 	switch((ypdb->dom_broadcast_pid = fork())) {
634 	case 0:
635 		close(READFD);
636 		break;
637 	case -1:
638 		syslog(LOG_WARNING, "fork: %s", strerror(errno));
639 		close(READFD);
640 		close(WRITEFD);
641 		return;
642 	default:
643 		close(WRITEFD);
644 		FD_SET(READFD, &svc_fdset);
645 		children++;
646 		return;
647 	}
648 
649 	/* Release all locks before doing anything else. */
650 	while(ypbindlist) {
651 		close(ypbindlist->dom_lockfd);
652 		ypbindlist = ypbindlist->dom_pnext;
653 	}
654 	close(yplockfd);
655 
656 	retries = 0;
657 
658 	stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
659 	    xdr_domainname, (char *)ypdb->dom_domain, xdr_bool, (char *)&out,
660 	    broadcast_result);
661 
662 	if (stat != RPC_SUCCESS) {
663 		bzero((char *)&ypdb->dom_server_addr,
664 						sizeof(struct sockaddr_in));
665 		if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
666 			syslog(LOG_WARNING, "lost connection to parent");
667 	}
668 
669 	exit(0);
670 }
671 
672 /*
673  * The right way to check if a server is alive.
674  * Attempt to get a client handle pointing to the server and send a
675  * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
676  * we invalidate this binding entry and send out a broadcast to try to
677  * establish a new binding. Note that we treat non-default domains
678  * specially: once bound, we keep tabs on our server, but if it
679  * goes away and fails to respond after one round of broadcasting, we
680  * abandon it until a client specifically references it again. We make
681  * every effort to keep our default domain bound, however, since we
682  * need it to keep the system on its feet.
683  */
684 int
685 ping(ypdb)
686 struct _dom_binding *ypdb;
687 {
688 	bool_t out;
689 	struct timeval interval, timeout;
690 	enum clnt_stat stat;
691 	int rpcsock = RPC_ANYSOCK;
692 	CLIENT *client_handle;
693 	time_t t;
694 
695 	interval.tv_sec = FAIL_THRESHOLD;
696 	interval.tv_usec = 0;
697 	timeout.tv_sec = FAIL_THRESHOLD;
698 	timeout.tv_usec = 0;
699 
700 	if (ypdb->dom_broadcast_pid)
701 		return(1);
702 
703 	if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
704 		YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
705 		RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
706 		/* Can't get a handle: we're dead. */
707 		ypdb->dom_alive = 0;
708 		ypdb->dom_vers = -1;
709 		broadcast(ypdb);
710 		return(1);
711 	}
712 
713 	if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
714 		xdr_domainname, (char *)ypdb->dom_domain, xdr_bool,
715 		(char *)&out, timeout)) != RPC_SUCCESS || out == FALSE) {
716 		ypdb->dom_alive = 0;
717 		ypdb->dom_vers = -1;
718 		clnt_destroy(client_handle);
719 		broadcast(ypdb);
720 		return(1);
721 	}
722 
723 	clnt_destroy(client_handle);
724 	return(0);
725 }
726 
727 void rpc_received(dom, raddrp, force)
728 char *dom;
729 struct sockaddr_in *raddrp;
730 int force;
731 {
732 	struct _dom_binding *ypdb, *prev = NULL;
733 	struct iovec iov[2];
734 	struct ypbind_resp ybr;
735 	char path[MAXPATHLEN];
736 	int fd;
737 
738 	/*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
739 	       ntohs(raddrp->sin_port), dom);*/
740 
741 	if(dom==NULL)
742 		return;
743 
744 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
745 		if( strcmp(ypdb->dom_domain, dom) == 0)
746 			break;
747 		prev = ypdb;
748 	}
749 
750 	if (ypdb && force) {
751 		if (ypdb->dom_broadcast_pid) {
752 			kill(ypdb->dom_broadcast_pid, SIGINT);
753 			close(READFD);
754 			FD_CLR(READFD, &fdsr);
755 			FD_CLR(READFD, &svc_fdset);
756 			READFD = WRITEFD = -1;
757 		}
758 	}
759 
760 	/* if in secure mode, check originating port number */
761 	if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
762 	    syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
763 		   inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
764 		   dom);
765 	    if (ypdb != NULL) {
766 		ypdb->dom_broadcast_pid = 0;
767 		ypdb->dom_alive = 0;
768 	    }
769 	    return;
770 	}
771 
772 	if (raddrp->sin_addr.s_addr == (long)0) {
773 		switch(ypdb->dom_default) {
774 		case 0:
775 			if (prev == NULL)
776 				ypbindlist = ypdb->dom_pnext;
777 			else
778 				prev->dom_pnext = ypdb->dom_pnext;
779 			sprintf(path, "%s/%s.%ld", BINDINGDIR,
780 				ypdb->dom_domain, YPVERS);
781 			close(ypdb->dom_lockfd);
782 			unlink(path);
783 			free(ypdb);
784 			domains--;
785 			return;
786 		case 1:
787 			ypdb->dom_broadcast_pid = 0;
788 			ypdb->dom_alive = 0;
789 			broadcast(ypdb);
790 			return;
791 		default:
792 			break;
793 		}
794 	}
795 
796 	if(ypdb==NULL) {
797 		if (force == 0)
798 			return;
799 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
800 		if (ypdb == NULL) {
801 			syslog(LOG_WARNING, "malloc: %s", strerror(errno));
802 			return;
803 		}
804 		bzero((char *)ypdb, sizeof *ypdb);
805 		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
806 		ypdb->dom_lockfd = -1;
807 		ypdb->dom_default = 0;
808 		ypdb->dom_pnext = ypbindlist;
809 		ypbindlist = ypdb;
810 	}
811 
812 	/* We've recovered from a crash: inform the world. */
813 	if (ypdb->dom_vers = -1 && ypdb->dom_server_addr.sin_addr.s_addr)
814 		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
815 		inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
816 
817 	bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
818 		sizeof ypdb->dom_server_addr);
819 
820 	ypdb->dom_vers = YPVERS;
821 	ypdb->dom_alive = 1;
822 	ypdb->dom_broadcast_pid = 0;
823 
824 	if(ypdb->dom_lockfd != -1)
825 		close(ypdb->dom_lockfd);
826 
827 	sprintf(path, "%s/%s.%ld", BINDINGDIR,
828 		ypdb->dom_domain, ypdb->dom_vers);
829 #ifdef O_SHLOCK
830 	if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
831 		(void)mkdir(BINDINGDIR, 0755);
832 		if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
833 			return;
834 	}
835 #else
836 	if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
837 		(void)mkdir(BINDINGDIR, 0755);
838 		if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
839 			return;
840 	}
841 	flock(fd, LOCK_SH);
842 #endif
843 
844 	/*
845 	 * ok, if BINDINGDIR exists, and we can create the binding file,
846 	 * then write to it..
847 	 */
848 	ypdb->dom_lockfd = fd;
849 
850 	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
851 	iov[0].iov_len = sizeof udptransp->xp_port;
852 	iov[1].iov_base = (caddr_t)&ybr;
853 	iov[1].iov_len = sizeof ybr;
854 
855 	bzero(&ybr, sizeof ybr);
856 	ybr.ypbind_status = YPBIND_SUCC_VAL;
857 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr;
858 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
859 
860 	if( writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
861 		syslog(LOG_WARNING, "write: %s", strerror(errno));
862 		close(ypdb->dom_lockfd);
863 		ypdb->dom_lockfd = -1;
864 		return;
865 	}
866 }
867 
868 /*
869  * Check address against list of allowed servers. Return 0 if okay,
870  * 1 if not matched.
871  */
872 int
873 verify(addr)
874 struct in_addr addr;
875 {
876 	int i;
877 
878 	for (i = 0; i < RESTRICTED_SERVERS; i++)
879 		if (!bcmp((char *)&addr, (char *)&restricted_addrs[i],
880 			sizeof(struct in_addr)))
881 			return(0);
882 
883 	return(1);
884 }
885 
886 /*
887  * Try to set restricted mode. We default to normal mode if we can't
888  * resolve the specified hostnames.
889  */
890 void
891 yp_restricted_mode(args)
892 char *args;
893 {
894 	struct hostent *h;
895 	int i = 0;
896 	char *s;
897 
898 	/* Find the restricted domain. */
899 	if ((s = strsep(&args, ",")) == NULL)
900 		return;
901 	domainname = s;
902 
903 	/* Get the addresses of the servers. */
904 	while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
905 		if ((h = gethostbyname(s)) == NULL)
906 			return;
907 		bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i],
908 			sizeof(struct in_addr));
909 	i++;
910 	}
911 
912 	/* ypset and ypsetme not allowed with restricted mode */
913 	ypsetmode = YPSET_NO;
914 
915 	yp_restricted = 1;
916 	return;
917 }
918