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