2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
38 #include <sys/signal.h>
39 #include <sys/socket.h>
41 #include <sys/fcntl.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 #include <rpc/pmap_clnt.h>
61 #include <rpc/pmap_prot.h>
62 #include <rpc/pmap_rmt.h>
63 #include <rpc/rpc_com.h>
64 #include <rpcsvc/yp.h>
65 #include <rpcsvc/ypclnt.h>
69 #define BINDINGDIR "/var/yp/binding"
73 #define YPBINDLOCK "/var/run/ypbind.lock"
77 struct _dom_binding *dom_pnext;
78 char dom_domain[YPMAXDOMAIN + 1];
79 struct sockaddr_in dom_server_addr;
83 int dom_broadcast_pid;
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]
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();
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 *);
106 void yp_restricted_mode(char *);
107 int verify(struct in_addr);
109 static char *domain_name;
110 static struct _dom_binding *ypbindlist;
111 static struct _dom_binding *broad_domain;
114 #define YPSET_LOCAL 1
116 static int ypsetmode = YPSET_NO;
117 static int ypsecuremode = 0;
120 #define NOT_RESPONDING_HYSTERESIS 10
121 static int not_responding_count = 0;
124 * Special restricted mode variables: when in restricted mode, only the
125 * specified restricted_domain will be bound, and only the servers listed
126 * in restricted_addrs will be used for binding.
128 #define RESTRICTED_SERVERS 10
129 static int yp_restricted = 0;
130 static int yp_manycast = 0;
131 static struct in_addr restricted_addrs[RESTRICTED_SERVERS];
133 /* No more than MAX_CHILDREN child broadcasters at a time. */
135 #define MAX_CHILDREN 5
137 /* No more than MAX_DOMAINS simultaneous domains */
139 #define MAX_DOMAINS 200
141 /* RPC timeout value */
142 #ifndef FAIL_THRESHOLD
143 #define FAIL_THRESHOLD 20
146 /* Number of times to fish for a response froma particular set of hosts */
148 #define MAX_RETRIES 30
151 static int retries = 0;
152 static int children = 0;
153 static int domains = 0;
157 static SVCXPRT *udptransp, *tcptransp;
160 ypbindproc_null_2_yp(SVCXPRT *transp, void *argp, CLIENT *clnt)
164 bzero(&res, sizeof(res));
168 static struct ypbind_resp *
169 ypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt)
171 static struct ypbind_resp res;
172 struct _dom_binding *ypdb;
173 char path[MAXPATHLEN];
175 bzero(&res, sizeof res);
176 res.ypbind_status = YPBIND_FAIL_VAL;
177 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
179 if (strchr(*argp, '/')) {
180 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
185 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
186 if (strcmp(ypdb->dom_domain, *argp) == 0)
192 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
196 if (domains >= MAX_DOMAINS) {
197 syslog(LOG_WARNING, "domain limit (%d) exceeded",
199 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
202 if (strlen(*argp) > YPMAXDOMAIN) {
203 syslog(LOG_WARNING, "domain %s too long", *argp);
204 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
207 ypdb = malloc(sizeof *ypdb);
209 syslog(LOG_WARNING, "malloc: %m");
210 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
213 bzero(ypdb, sizeof *ypdb);
214 strlcpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
215 ypdb->dom_vers = YPVERS;
217 ypdb->dom_default = 0;
218 ypdb->dom_lockfd = -1;
219 sprintf(path, "%s/%s.%ld", BINDINGDIR,
220 ypdb->dom_domain, ypdb->dom_vers);
222 ypdb->dom_pnext = ypbindlist;
231 res.ypbind_status = YPBIND_SUCC_VAL;
232 res.ypbind_resp_u.ypbind_error = 0; /* Success */
233 memcpy(&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
234 &ypdb->dom_server_addr.sin_addr.s_addr, sizeof(u_int32_t));
235 memcpy(&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
236 &ypdb->dom_server_addr.sin_port, sizeof(u_short));
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));*/
244 ypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt)
246 struct sockaddr_in *fromsin, bindsin;
247 static char *result = NULL;
249 if (strchr(argp->ypsetdom_domain, '/')) {
250 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
251 rejecting.", argp->ypsetdom_domain);
254 fromsin = svc_getcaller(transp);
258 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
259 svcerr_noprog(transp);
267 svcerr_noprog(transp);
271 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
272 svcerr_noprog(transp);
276 if (argp->ypsetdom_vers != YPVERS) {
277 svcerr_noprog(transp);
281 bzero(&bindsin, sizeof bindsin);
282 bindsin.sin_family = AF_INET;
283 memcpy(&bindsin.sin_addr.s_addr,
284 &argp->ypsetdom_binding.ypbind_binding_addr,
286 memcpy(&bindsin.sin_port,
287 &argp->ypsetdom_binding.ypbind_binding_port,
289 rpc_received(argp->ypsetdom_domain, &bindsin, 1);
291 return((void *) &result);
295 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
298 domainname ypbindproc_domain_2_arg;
299 struct ypbind_setdom ypbindproc_setdom_2_arg;
301 struct authunix_parms *creds;
303 bool_t (*xdr_argument)(), (*xdr_result)();
306 switch (rqstp->rq_proc) {
307 case YPBINDPROC_NULL:
308 xdr_argument = xdr_void;
309 xdr_result = xdr_void;
310 local = (char *(*)()) ypbindproc_null_2_yp;
313 case YPBINDPROC_DOMAIN:
314 xdr_argument = xdr_domainname;
315 xdr_result = xdr_ypbind_resp;
316 local = (char *(*)()) ypbindproc_domain_2_yp;
319 case YPBINDPROC_SETDOM:
320 switch (rqstp->rq_cred.oa_flavor) {
322 creds = (struct authunix_parms *)rqstp->rq_clntcred;
323 if (creds->aup_uid != 0) {
324 svcerr_auth(transp, AUTH_BADCRED);
329 svcerr_auth(transp, AUTH_TOOWEAK);
333 xdr_argument = xdr_ypbind_setdom;
334 xdr_result = xdr_void;
335 local = (char *(*)()) ypbindproc_setdom_2_yp;
339 svcerr_noproc(transp);
342 bzero(&argument, sizeof(argument));
343 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
344 svcerr_decode(transp);
347 result = (*local)(transp, &argument, rqstp);
348 if (result != NULL &&
349 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
350 svcerr_systemerr(transp);
355 /* Jack the reaper */
361 while (wait3(&st, WNOHANG, NULL) > 0)
368 struct _dom_binding *ypdb;
369 char path[MAXPATHLEN];
371 if (ppid != getpid())
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);
384 pmap_unset(YPBINDPROG, YPBINDVERS);
389 main(int argc, char *argv[])
395 struct _dom_binding *ypdb, *next;
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);
401 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
402 errx(1, "another ypbind is already running. Aborting");
404 /* XXX domainname will be overridden 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");
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)
416 else if (strcmp("-S", argv[i]) == 0 && argc > i)
417 yp_restricted_mode(argv[++i]);
418 else if (strcmp("-m", argv[i]) == 0)
421 errx(1, "unknown option: %s", argv[i]);
424 if (strlen(domain_name) > YPMAXDOMAIN)
425 warnx("truncating domain name %s", domain_name);
427 /* blow away everything in BINDINGDIR (if it exists) */
429 if ((dird = opendir(BINDINGDIR)) != NULL) {
430 char path[MAXPATHLEN];
431 while ((dirp = readdir(dird)) != NULL)
432 if (strcmp(dirp->d_name, ".") &&
433 strcmp(dirp->d_name, "..")) {
434 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
445 pmap_unset(YPBINDPROG, YPBINDVERS);
447 udptransp = svcudp_create(RPC_ANYSOCK);
448 if (udptransp == NULL)
449 errx(1, "cannot create udp service");
450 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
452 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
454 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
455 if (tcptransp == NULL)
456 errx(1, "cannot create tcp service");
458 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
460 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
462 /* build initial domain binding, make it "unsuccessful" */
463 ypbindlist = malloc(sizeof *ypbindlist);
464 if (ypbindlist == NULL)
466 bzero(ypbindlist, sizeof *ypbindlist);
467 strlcpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
468 ypbindlist->dom_vers = YPVERS;
469 ypbindlist->dom_alive = 0;
470 ypbindlist->dom_lockfd = -1;
471 ypbindlist->dom_default = 1;
474 signal(SIGCHLD, reaper);
475 signal(SIGTERM, terminate);
477 ppid = getpid(); /* Remember who we are. */
479 openlog(argv[0], LOG_PID, LOG_DAEMON);
481 if (madvise(NULL, 0, MADV_PROTECT) != 0)
482 syslog(LOG_WARNING, "madvise(): %m");
484 /* Kick off the default domain */
485 broadcast(ypbindlist);
493 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
499 syslog(LOG_WARNING, "select: %m");
502 for (ypdb = ypbindlist; ypdb; ypdb = next) {
503 next = ypdb->dom_pnext;
504 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
505 handle_children(ypdb);
506 if (children == (MAX_CHILDREN - 1))
510 svc_getreqset(&fdsr);
522 struct _dom_binding *ypdb;
524 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
528 /* The clnt_broadcast() callback mechanism sucks. */
531 * Receive results from broadcaster. Don't worry about passing
532 * bogus info to rpc_received() -- it can handle it. Note that we
533 * must be sure to invalidate the dom_pipe_fds descriptors here:
534 * since descriptors can be re-used, we have to make sure we
535 * don't mistake one of the RPC descriptors for one of the pipes.
536 * What's weird is that forgetting to invalidate the pipe descriptors
537 * doesn't always result in an error (otherwise I would have caught
538 * the mistake much sooner), even though logically it should.
541 handle_children(struct _dom_binding *ypdb)
543 char buf[YPMAXDOMAIN + 1];
544 struct sockaddr_in addr;
546 struct _dom_binding *y, *prev = NULL;
547 char path[MAXPATHLEN];
549 if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
550 syslog(LOG_WARNING, "could not read from child: %m");
552 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
553 syslog(LOG_WARNING, "could not read from child: %m");
556 FD_CLR(READFD, &fdsr);
557 FD_CLR(READFD, &svc_fdset);
558 READFD = WRITEFD = -1;
560 rpc_received(buf, &addr, 0);
562 for (y = ypbindlist; y; y = y->dom_pnext) {
567 switch (ypdb->dom_default) {
570 ypbindlist = y->dom_pnext;
572 prev->dom_pnext = y->dom_pnext;
573 sprintf(path, "%s/%s.%ld", BINDINGDIR,
574 ypdb->dom_domain, YPVERS);
575 close(ypdb->dom_lockfd);
581 ypdb->dom_broadcast_pid = 0;
594 * Send our dying words back to our parent before we perish.
597 tell_parent(char *dom, struct sockaddr_in *addr)
599 char buf[YPMAXDOMAIN + 1];
600 struct timeval timeout;
606 sprintf(buf, "%s", broad_domain->dom_domain);
607 if (write(BROADFD, &buf, sizeof(buf)) < 0)
611 * Stay in sync with parent: wait for it to read our first
612 * message before sending the second.
616 FD_SET(BROADFD, &fds);
617 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
619 if (FD_ISSET(BROADFD, &fds)) {
620 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
631 broadcast_result(bool_t *out, struct sockaddr_in *addr)
633 if (retries >= MAX_RETRIES) {
634 bzero(addr, sizeof(struct sockaddr_in));
635 if (tell_parent(broad_domain->dom_domain, addr))
636 syslog(LOG_WARNING, "lost connection to parent");
640 if (yp_restricted && verify(addr->sin_addr)) {
642 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
645 if (tell_parent(broad_domain->dom_domain, addr))
646 syslog(LOG_WARNING, "lost connection to parent");
652 * The right way to send RPC broadcasts.
653 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
654 * blocks while waiting for replies, so we have to fork off separate
655 * broadcaster processes that do the waiting and then transmit their
656 * results back to the parent for processing. We also have to remember
657 * to save the name of the domain we're trying to bind in a global
658 * variable since clnt_broadcast() provides no way to pass things to
659 * the 'eachresult' callback function.
662 broadcast(struct _dom_binding *ypdb)
667 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
670 if (pipe(ypdb->dom_pipe_fds) < 0) {
671 syslog(LOG_WARNING, "pipe: %m");
675 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr) {
676 if (not_responding_count++ >= NOT_RESPONDING_HYSTERESIS) {
677 not_responding_count = NOT_RESPONDING_HYSTERESIS;
678 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
679 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
684 flock(ypdb->dom_lockfd, LOCK_UN);
686 switch ((ypdb->dom_broadcast_pid = fork())) {
689 signal(SIGCHLD, SIG_DFL);
690 signal(SIGTERM, SIG_DFL);
693 syslog(LOG_WARNING, "fork: %m");
699 FD_SET(READFD, &svc_fdset);
704 /* Release all locks before doing anything else. */
706 close(ypbindlist->dom_lockfd);
707 ypbindlist = ypbindlist->dom_pnext;
712 * Special 'many-cast' behavior. If we're in restricted mode,
713 * we have a list of possible server addresses to try. What
714 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
715 * procedure and time the replies. Whoever replies fastest
716 * gets to be our server. Note that this is not a broadcast
717 * operation: we transmit uni-cast datagrams only.
719 if (yp_restricted && yp_manycast) {
722 struct sockaddr_in sin;
724 i = __yp_ping(restricted_addrs, yp_restricted,
725 ypdb->dom_domain, &port);
727 bzero(&ypdb->dom_server_addr,
728 sizeof(struct sockaddr_in));
729 if (tell_parent(ypdb->dom_domain,
730 &ypdb->dom_server_addr))
731 syslog(LOG_WARNING, "lost connection to parent");
733 bzero(&sin, sizeof(struct sockaddr_in));
734 bcopy(&restricted_addrs[i],
735 &sin.sin_addr, sizeof(struct in_addr));
736 sin.sin_family = AF_INET;
738 if (tell_parent(broad_domain->dom_domain, &sin))
740 "lost connection to parent");
750 ptr = ypdb->dom_domain;
751 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
752 (xdrproc_t)xdr_domainname, &ptr,
753 (xdrproc_t)xdr_bool, &out,
754 (resultproc_t)broadcast_result);
757 if (stat != RPC_SUCCESS) {
758 bzero(&ypdb->dom_server_addr,
759 sizeof(struct sockaddr_in));
760 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
761 syslog(LOG_WARNING, "lost connection to parent");
768 * The right way to check if a server is alive.
769 * Attempt to get a client handle pointing to the server and send a
770 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
771 * we invalidate this binding entry and send out a broadcast to try to
772 * establish a new binding. Note that we treat non-default domains
773 * specially: once bound, we keep tabs on our server, but if it
774 * goes away and fails to respond after one round of broadcasting, we
775 * abandon it until a client specifically references it again. We make
776 * every effort to keep our default domain bound, however, since we
777 * need it to keep the system on its feet.
780 ping(struct _dom_binding *ypdb)
783 struct timeval interval, timeout;
785 int rpcsock = RPC_ANYSOCK;
786 CLIENT *client_handle;
788 interval.tv_sec = FAIL_THRESHOLD;
789 interval.tv_usec = 0;
790 timeout.tv_sec = FAIL_THRESHOLD;
793 if (ypdb->dom_broadcast_pid)
796 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
797 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
798 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
799 /* Can't get a handle: we're dead. */
809 ptr = ypdb->dom_domain;
811 stat = clnt_call(client_handle, YPPROC_DOMAIN,
812 (xdrproc_t)xdr_domainname, &ptr,
813 (xdrproc_t)xdr_bool, &out, timeout);
814 if (stat != RPC_SUCCESS || out == FALSE) {
817 clnt_destroy(client_handle);
823 clnt_destroy(client_handle);
828 rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
830 struct _dom_binding *ypdb, *prev = NULL;
832 struct ypbind_resp ybr;
833 char path[MAXPATHLEN];
836 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
837 ntohs(raddrp->sin_port), dom);*/
842 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
843 if (strcmp(ypdb->dom_domain, dom) == 0)
849 if (ypdb->dom_broadcast_pid) {
850 kill(ypdb->dom_broadcast_pid, SIGINT);
852 FD_CLR(READFD, &fdsr);
853 FD_CLR(READFD, &svc_fdset);
854 READFD = WRITEFD = -1;
858 /* if in secure mode, check originating port number */
859 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
860 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
861 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
864 ypdb->dom_broadcast_pid = 0;
870 if (raddrp->sin_addr.s_addr == (long)0) {
871 switch (ypdb->dom_default) {
874 ypbindlist = ypdb->dom_pnext;
876 prev->dom_pnext = ypdb->dom_pnext;
877 sprintf(path, "%s/%s.%ld", BINDINGDIR,
878 ypdb->dom_domain, YPVERS);
879 close(ypdb->dom_lockfd);
885 ypdb->dom_broadcast_pid = 0;
897 if (strlen(dom) > YPMAXDOMAIN) {
898 syslog(LOG_WARNING, "domain %s too long", dom);
901 ypdb = malloc(sizeof *ypdb);
903 syslog(LOG_WARNING, "malloc: %m");
906 bzero(ypdb, sizeof *ypdb);
907 strlcpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
908 ypdb->dom_lockfd = -1;
909 ypdb->dom_default = 0;
910 ypdb->dom_pnext = ypbindlist;
914 /* We've recovered from a crash: inform the world. */
915 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr) {
916 if (not_responding_count >= NOT_RESPONDING_HYSTERESIS) {
917 not_responding_count = 0;
918 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
919 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
923 bcopy(raddrp, &ypdb->dom_server_addr,
924 sizeof ypdb->dom_server_addr);
926 ypdb->dom_vers = YPVERS;
928 ypdb->dom_broadcast_pid = 0;
930 if (ypdb->dom_lockfd != -1)
931 close(ypdb->dom_lockfd);
933 sprintf(path, "%s/%s.%ld", BINDINGDIR,
934 ypdb->dom_domain, ypdb->dom_vers);
936 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
937 (void)mkdir(BINDINGDIR, 0755);
938 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
942 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
943 (void)mkdir(BINDINGDIR, 0755);
944 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
951 * ok, if BINDINGDIR exists, and we can create the binding file,
954 ypdb->dom_lockfd = fd;
956 iov[0].iov_base = (char *)&(udptransp->xp_port);
957 iov[0].iov_len = sizeof udptransp->xp_port;
958 iov[1].iov_base = (char *)&ybr;
959 iov[1].iov_len = sizeof ybr;
961 bzero(&ybr, sizeof ybr);
962 ybr.ypbind_status = YPBIND_SUCC_VAL;
963 memcpy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
964 &raddrp->sin_addr.s_addr, sizeof(u_int32_t));
965 memcpy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
966 &raddrp->sin_port, sizeof(u_short));
968 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
969 syslog(LOG_WARNING, "write: %m");
970 close(ypdb->dom_lockfd);
971 ypdb->dom_lockfd = -1;
977 * Check address against list of allowed servers. Return 0 if okay,
981 verify(struct in_addr addr)
985 for (i = 0; i < RESTRICTED_SERVERS; i++)
986 if (!bcmp(&addr, &restricted_addrs[i], sizeof(struct in_addr)))
993 * Try to set restricted mode. We default to normal mode if we can't
994 * resolve the specified hostnames.
997 yp_restricted_mode(char *args)
1003 /* Find the restricted domain. */
1004 if ((s = strsep(&args, ",")) == NULL)
1008 /* Get the addresses of the servers. */
1009 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
1010 if ((h = gethostbyname(s)) == NULL)
1012 bcopy (h->h_addr_list[0], &restricted_addrs[i],
1013 sizeof(struct in_addr));
1017 /* ypset and ypsetme not allowed with restricted mode */
1018 ypsetmode = YPSET_NO;