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