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