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