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