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