Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / ypbind / ypbind.c
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: src/usr.sbin/ypbind/ypbind.c,v 1.30.2.2 2002/02/15 00:46:59 des Exp $";
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(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 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+1]);
420                 else if (strcmp("-m", argv[i]) == 0)
421                         yp_manycast++;
422         }
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 = (struct _dom_binding *)malloc(sizeof *ypbindlist);
461         if (ypbindlist == NULL)
462                 errx(1, "malloc");
463         bzero((char *)ypbindlist, sizeof *ypbindlist);
464         strncpy(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         /* Kick off the default domain */
479         broadcast(ypbindlist);
480
481         while (1) {
482                 fdsr = svc_fdset;
483
484                 tv.tv_sec = 60;
485                 tv.tv_usec = 0;
486
487                 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
488                 case 0:
489                         checkwork();
490                         break;
491                 case -1:
492                         if (errno != EINTR)
493                                 syslog(LOG_WARNING, "select: %m");
494                         break;
495                 default:
496                         for (ypdb = ypbindlist; ypdb; ypdb = next) {
497                                 next = ypdb->dom_pnext;
498                                 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
499                                         handle_children(ypdb);
500                                         if (children == (MAX_CHILDREN - 1))
501                                                 checkwork();
502                                 }
503                         }
504                         svc_getreqset(&fdsr);
505                         break;
506                 }
507         }
508
509         /* NOTREACHED */
510         exit(1);
511 }
512
513 void
514 checkwork()
515 {
516         struct _dom_binding *ypdb;
517
518         for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
519                 ping(ypdb);
520 }
521
522 /* The clnt_broadcast() callback mechanism sucks. */
523
524 /*
525  * Receive results from broadcaster. Don't worry about passing
526  * bogus info to rpc_received() -- it can handle it. Note that we
527  * must be sure to invalidate the dom_pipe_fds descriptors here:
528  * since descriptors can be re-used, we have to make sure we
529  * don't mistake one of the RPC descriptors for one of the pipes.
530  * What's weird is that forgetting to invalidate the pipe descriptors
531  * doesn't always result in an error (otherwise I would have caught
532  * the mistake much sooner), even though logically it should.
533  */
534 void handle_children(ypdb)
535 struct _dom_binding *ypdb;
536 {
537         char buf[YPMAXDOMAIN + 1];
538         struct sockaddr_in addr;
539         int d = 0, a = 0;
540         struct _dom_binding *y, *prev = NULL;
541         char path[MAXPATHLEN];
542
543         if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
544                 syslog(LOG_WARNING, "could not read from child: %m");
545
546         if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
547                 syslog(LOG_WARNING, "could not read from child: %m");
548
549         close(READFD);
550         FD_CLR(READFD, &fdsr);
551         FD_CLR(READFD, &svc_fdset);
552         READFD = WRITEFD = -1;
553         if (d > 0 && a > 0)
554                 rpc_received((char *)&buf, &addr, 0);
555         else {
556                 for (y = ypbindlist; y; y = y->dom_pnext) {
557                         if (y == ypdb)
558                                 break;
559                         prev = y;
560                 }
561                 switch (ypdb->dom_default) {
562                 case 0:
563                         if (prev == NULL)
564                                 ypbindlist = y->dom_pnext;
565                         else
566                                 prev->dom_pnext = y->dom_pnext;
567                         sprintf(path, "%s/%s.%ld", BINDINGDIR,
568                                 ypdb->dom_domain, YPVERS);
569                         close(ypdb->dom_lockfd);
570                         unlink(path);
571                         free(ypdb);
572                         domains--;
573                         return;
574                 case 1:
575                         ypdb->dom_broadcast_pid = 0;
576                         ypdb->dom_alive = 0;
577                         broadcast(ypdb);
578                         return;
579                 default:
580                         break;
581                 }
582         }
583
584         return;
585 }
586
587 /*
588  * Send our dying words back to our parent before we perish.
589  */
590 int
591 tell_parent(dom, addr)
592 char *dom;
593 struct sockaddr_in *addr;
594 {
595         char buf[YPMAXDOMAIN + 1];
596         struct timeval timeout;
597         fd_set fds;
598
599         timeout.tv_sec = 5;
600         timeout.tv_usec = 0;
601
602         sprintf(buf, "%s", broad_domain->dom_domain);
603         if (write(BROADFD, &buf, sizeof(buf)) < 0)
604                 return(1);
605
606         /*
607          * Stay in sync with parent: wait for it to read our first
608          * message before sending the second.
609          */
610
611         FD_ZERO(&fds);
612         FD_SET(BROADFD, &fds);
613         if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
614                 return(1);
615         if (FD_ISSET(BROADFD, &fds)) {
616                 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
617                         return(1);
618         } else {
619                 return(1);
620         }
621
622         close(BROADFD);
623         return (0);
624 }
625
626 bool_t broadcast_result(out, addr)
627 bool_t *out;
628 struct sockaddr_in *addr;
629 {
630         if (retries >= MAX_RETRIES) {
631                 bzero((char *)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 seperate
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
659 broadcast(ypdb)
660 struct _dom_binding *ypdb;
661 {
662         bool_t out = FALSE;
663         enum clnt_stat stat;
664
665         if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
666                 return;
667
668         if (pipe(ypdb->dom_pipe_fds) < 0) {
669                 syslog(LOG_WARNING, "pipe: %m");
670                 return;
671         }
672
673         if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
674                 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
675                 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
676
677         broad_domain = ypdb;
678         flock(ypdb->dom_lockfd, LOCK_UN);
679
680         switch ((ypdb->dom_broadcast_pid = fork())) {
681         case 0:
682                 close(READFD);
683                 signal(SIGCHLD, SIG_DFL);
684                 signal(SIGTERM, SIG_DFL);
685                 break;
686         case -1:
687                 syslog(LOG_WARNING, "fork: %m");
688                 close(READFD);
689                 close(WRITEFD);
690                 return;
691         default:
692                 close(WRITEFD);
693                 FD_SET(READFD, &svc_fdset);
694                 children++;
695                 return;
696         }
697
698         /* Release all locks before doing anything else. */
699         while (ypbindlist) {
700                 close(ypbindlist->dom_lockfd);
701                 ypbindlist = ypbindlist->dom_pnext;
702         }
703         close(yplockfd);
704
705         /*
706          * Special 'many-cast' behavior. If we're in restricted mode,
707          * we have a list of possible server addresses to try. What
708          * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
709          * procedure and time the replies. Whoever replies fastest
710          * gets to be our server. Note that this is not a broadcast
711          * operation: we transmit uni-cast datagrams only.
712          */
713         if (yp_restricted && yp_manycast) {
714                 short                   port;
715                 int                     i;
716                 struct sockaddr_in      sin;
717
718                 i = __yp_ping(restricted_addrs, yp_restricted,
719                                 ypdb->dom_domain, &port);
720                 if (i == -1) {
721                         bzero((char *)&ypdb->dom_server_addr,
722                                                 sizeof(struct sockaddr_in));
723                         if (tell_parent(ypdb->dom_domain,
724                                         &ypdb->dom_server_addr))
725                         syslog(LOG_WARNING, "lost connection to parent");
726                 } else {
727                         bzero((char *)&sin, sizeof(struct sockaddr_in));
728                         bcopy((char *)&restricted_addrs[i],
729                                 (char *)&sin.sin_addr, sizeof(struct in_addr));
730                         sin.sin_family = AF_INET;
731                         sin.sin_port = port;
732                         if (tell_parent(broad_domain->dom_domain, &sin))
733                                 syslog(LOG_WARNING,
734                                         "lost connection to parent");
735                 }
736                 _exit(0);
737         }
738
739         retries = 0;
740
741         {
742                 char *ptr;
743
744                 ptr = (char *)&ypdb->dom_domain;
745                 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
746                         xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
747                         broadcast_result);
748         }
749
750         if (stat != RPC_SUCCESS) {
751                 bzero((char *)&ypdb->dom_server_addr,
752                                                 sizeof(struct sockaddr_in));
753                 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
754                         syslog(LOG_WARNING, "lost connection to parent");
755         }
756
757         _exit(0);
758 }
759
760 /*
761  * The right way to check if a server is alive.
762  * Attempt to get a client handle pointing to the server and send a
763  * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
764  * we invalidate this binding entry and send out a broadcast to try to
765  * establish a new binding. Note that we treat non-default domains
766  * specially: once bound, we keep tabs on our server, but if it
767  * goes away and fails to respond after one round of broadcasting, we
768  * abandon it until a client specifically references it again. We make
769  * every effort to keep our default domain bound, however, since we
770  * need it to keep the system on its feet.
771  */
772 int
773 ping(ypdb)
774 struct _dom_binding *ypdb;
775 {
776         bool_t out;
777         struct timeval interval, timeout;
778         enum clnt_stat stat;
779         int rpcsock = RPC_ANYSOCK;
780         CLIENT *client_handle;
781
782         interval.tv_sec = FAIL_THRESHOLD;
783         interval.tv_usec = 0;
784         timeout.tv_sec = FAIL_THRESHOLD;
785         timeout.tv_usec = 0;
786
787         if (ypdb->dom_broadcast_pid)
788                 return(1);
789
790         if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
791                 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
792                 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
793                 /* Can't get a handle: we're dead. */
794                 ypdb->dom_alive = 0;
795                 ypdb->dom_vers = -1;
796                 broadcast(ypdb);
797                 return(1);
798         }
799
800         {
801                 char *ptr;
802
803                 ptr = (char *)&ypdb->dom_domain;
804
805                 if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
806                         xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
807                         timeout)) != RPC_SUCCESS || out == FALSE) {
808                         ypdb->dom_alive = 0;
809                         ypdb->dom_vers = -1;
810                         clnt_destroy(client_handle);
811                         broadcast(ypdb);
812                         return(1);
813                 }
814         }
815
816         clnt_destroy(client_handle);
817         return(0);
818 }
819
820 void rpc_received(dom, raddrp, force)
821 char *dom;
822 struct sockaddr_in *raddrp;
823 int force;
824 {
825         struct _dom_binding *ypdb, *prev = NULL;
826         struct iovec iov[2];
827         struct ypbind_resp ybr;
828         char path[MAXPATHLEN];
829         int fd;
830
831         /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
832                ntohs(raddrp->sin_port), dom);*/
833
834         if (dom == NULL)
835                 return;
836
837         for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
838                 if (strcmp(ypdb->dom_domain, dom) == 0)
839                         break;
840                 prev = ypdb;
841         }
842
843         if (ypdb && force) {
844                 if (ypdb->dom_broadcast_pid) {
845                         kill(ypdb->dom_broadcast_pid, SIGINT);
846                         close(READFD);
847                         FD_CLR(READFD, &fdsr);
848                         FD_CLR(READFD, &svc_fdset);
849                         READFD = WRITEFD = -1;
850                 }
851         }
852
853         /* if in secure mode, check originating port number */
854         if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
855             syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
856                    inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
857                    dom);
858             if (ypdb != NULL) {
859                 ypdb->dom_broadcast_pid = 0;
860                 ypdb->dom_alive = 0;
861             }
862             return;
863         }
864
865         if (raddrp->sin_addr.s_addr == (long)0) {
866                 switch (ypdb->dom_default) {
867                 case 0:
868                         if (prev == NULL)
869                                 ypbindlist = ypdb->dom_pnext;
870                         else
871                                 prev->dom_pnext = ypdb->dom_pnext;
872                         sprintf(path, "%s/%s.%ld", BINDINGDIR,
873                                 ypdb->dom_domain, YPVERS);
874                         close(ypdb->dom_lockfd);
875                         unlink(path);
876                         free(ypdb);
877                         domains--;
878                         return;
879                 case 1:
880                         ypdb->dom_broadcast_pid = 0;
881                         ypdb->dom_alive = 0;
882                         broadcast(ypdb);
883                         return;
884                 default:
885                         break;
886                 }
887         }
888
889         if (ypdb == NULL) {
890                 if (force == 0)
891                         return;
892                 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
893                 if (ypdb == NULL) {
894                         syslog(LOG_WARNING, "malloc: %m");
895                         return;
896                 }
897                 bzero((char *)ypdb, sizeof *ypdb);
898                 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
899                 ypdb->dom_lockfd = -1;
900                 ypdb->dom_default = 0;
901                 ypdb->dom_pnext = ypbindlist;
902                 ypbindlist = ypdb;
903         }
904
905         /* We've recovered from a crash: inform the world. */
906         if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr)
907                 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
908                 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
909
910         bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
911                 sizeof ypdb->dom_server_addr);
912
913         ypdb->dom_vers = YPVERS;
914         ypdb->dom_alive = 1;
915         ypdb->dom_broadcast_pid = 0;
916
917         if (ypdb->dom_lockfd != -1)
918                 close(ypdb->dom_lockfd);
919
920         sprintf(path, "%s/%s.%ld", BINDINGDIR,
921                 ypdb->dom_domain, ypdb->dom_vers);
922 #ifdef O_SHLOCK
923         if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
924                 (void)mkdir(BINDINGDIR, 0755);
925                 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
926                         return;
927         }
928 #else
929         if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
930                 (void)mkdir(BINDINGDIR, 0755);
931                 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
932                         return;
933         }
934         flock(fd, LOCK_SH);
935 #endif
936
937         /*
938          * ok, if BINDINGDIR exists, and we can create the binding file,
939          * then write to it..
940          */
941         ypdb->dom_lockfd = fd;
942
943         iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
944         iov[0].iov_len = sizeof udptransp->xp_port;
945         iov[1].iov_base = (caddr_t)&ybr;
946         iov[1].iov_len = sizeof ybr;
947
948         bzero(&ybr, sizeof ybr);
949         ybr.ypbind_status = YPBIND_SUCC_VAL;
950         *(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
951         *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
952
953         if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
954                 syslog(LOG_WARNING, "write: %m");
955                 close(ypdb->dom_lockfd);
956                 ypdb->dom_lockfd = -1;
957                 return;
958         }
959 }
960
961 /*
962  * Check address against list of allowed servers. Return 0 if okay,
963  * 1 if not matched.
964  */
965 int
966 verify(addr)
967 struct in_addr addr;
968 {
969         int i;
970
971         for (i = 0; i < RESTRICTED_SERVERS; i++)
972                 if (!bcmp((char *)&addr, (char *)&restricted_addrs[i],
973                         sizeof(struct in_addr)))
974                         return(0);
975
976         return(1);
977 }
978
979 /*
980  * Try to set restricted mode. We default to normal mode if we can't
981  * resolve the specified hostnames.
982  */
983 void
984 yp_restricted_mode(args)
985 char *args;
986 {
987         struct hostent *h;
988         int i = 0;
989         char *s;
990
991         /* Find the restricted domain. */
992         if ((s = strsep(&args, ",")) == NULL)
993                 return;
994         domain_name = s;
995
996         /* Get the addresses of the servers. */
997         while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
998                 if ((h = gethostbyname(s)) == NULL)
999                         return;
1000                 bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i],
1001                         sizeof(struct in_addr));
1002         i++;
1003         }
1004
1005         /* ypset and ypsetme not allowed with restricted mode */
1006         ypsetmode = YPSET_NO;
1007
1008         yp_restricted = i;
1009         return;
1010 }