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