2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
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]
93 void *ypbindproc_null_2_yp(SVCXPRT *, void *, CLIENT *);
94 void *ypbindproc_setdom_2_yp(SVCXPRT *, struct ypbind_setdom *, CLIENT *);
95 void rpc_received(char *, struct sockaddr_in *, int);
96 void broadcast(struct _dom_binding *);
97 int ping(struct _dom_binding *);
98 int tell_parent(char *, struct sockaddr_in *);
99 void handle_children(struct _dom_binding *);
102 void yp_restricted_mode(char *);
103 int verify(struct in_addr);
105 static char *domain_name;
106 static struct _dom_binding *ypbindlist;
107 static struct _dom_binding *broad_domain;
110 #define YPSET_LOCAL 1
112 static int ypsetmode = YPSET_NO;
113 static int ypsecuremode = 0;
116 #define NOT_RESPONDING_HYSTERESIS 10
117 static int not_responding_count = 0;
120 * Special restricted mode variables: when in restricted mode, only the
121 * specified restricted_domain will be bound, and only the servers listed
122 * in restricted_addrs will be used for binding.
124 #define RESTRICTED_SERVERS 10
125 static int yp_restricted = 0;
126 static int yp_manycast = 0;
127 static struct in_addr restricted_addrs[RESTRICTED_SERVERS];
129 /* No more than MAX_CHILDREN child broadcasters at a time. */
131 #define MAX_CHILDREN 5
133 /* No more than MAX_DOMAINS simultaneous domains */
135 #define MAX_DOMAINS 200
137 /* RPC timeout value */
138 #ifndef FAIL_THRESHOLD
139 #define FAIL_THRESHOLD 20
142 /* Number of times to fish for a response froma particular set of hosts */
144 #define MAX_RETRIES 30
147 static int retries = 0;
148 static int children = 0;
149 static int domains = 0;
153 static SVCXPRT *udptransp, *tcptransp;
156 ypbindproc_null_2_yp(SVCXPRT *transp, void *argp, CLIENT *clnt)
160 bzero(&res, sizeof(res));
164 static struct ypbind_resp *
165 ypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt)
167 static struct ypbind_resp res;
168 struct _dom_binding *ypdb;
169 char path[MAXPATHLEN];
171 bzero(&res, sizeof res);
172 res.ypbind_status = YPBIND_FAIL_VAL;
173 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
175 if (strchr(*argp, '/')) {
176 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
181 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
182 if (strcmp(ypdb->dom_domain, *argp) == 0)
188 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
192 if (domains >= MAX_DOMAINS) {
193 syslog(LOG_WARNING, "domain limit (%d) exceeded",
195 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
198 if (strlen(*argp) > YPMAXDOMAIN) {
199 syslog(LOG_WARNING, "domain %s too long", *argp);
200 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
203 ypdb = malloc(sizeof *ypdb);
205 syslog(LOG_WARNING, "malloc: %m");
206 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
209 bzero(ypdb, sizeof *ypdb);
210 strlcpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
211 ypdb->dom_vers = YPVERS;
213 ypdb->dom_default = 0;
214 ypdb->dom_lockfd = -1;
215 sprintf(path, "%s/%s.%ld", BINDINGDIR,
216 ypdb->dom_domain, ypdb->dom_vers);
218 ypdb->dom_pnext = ypbindlist;
227 res.ypbind_status = YPBIND_SUCC_VAL;
228 res.ypbind_resp_u.ypbind_error = 0; /* Success */
229 memcpy(&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
230 &ypdb->dom_server_addr.sin_addr.s_addr, sizeof(u_int32_t));
231 memcpy(&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
232 &ypdb->dom_server_addr.sin_port, sizeof(u_short));
233 /*printf("domain %s at %s/%d\n", ypdb->dom_domain,
234 inet_ntoa(ypdb->dom_server_addr.sin_addr),
235 ntohs(ypdb->dom_server_addr.sin_port));*/
240 ypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt)
242 struct sockaddr_in *fromsin, bindsin;
243 static char *result = NULL;
245 if (strchr(argp->ypsetdom_domain, '/')) {
246 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
247 rejecting.", argp->ypsetdom_domain);
250 fromsin = svc_getcaller(transp);
254 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
255 svcerr_noprog(transp);
263 svcerr_noprog(transp);
267 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
268 svcerr_noprog(transp);
272 if (argp->ypsetdom_vers != YPVERS) {
273 svcerr_noprog(transp);
277 bzero(&bindsin, sizeof bindsin);
278 bindsin.sin_family = AF_INET;
279 memcpy(&bindsin.sin_addr.s_addr,
280 &argp->ypsetdom_binding.ypbind_binding_addr,
282 memcpy(&bindsin.sin_port,
283 &argp->ypsetdom_binding.ypbind_binding_port,
285 rpc_received(argp->ypsetdom_domain, &bindsin, 1);
287 return((void *) &result);
291 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
294 domainname ypbindproc_domain_2_arg;
295 struct ypbind_setdom ypbindproc_setdom_2_arg;
297 struct authunix_parms *creds;
299 xdrproc_t xdr_argument, xdr_result;
300 typedef void *(svc_cb)(SVCXPRT *transp, void *arg,
301 struct svc_req *rqstp);
304 switch (rqstp->rq_proc) {
305 case YPBINDPROC_NULL:
306 xdr_argument = (xdrproc_t)xdr_void;
307 xdr_result = (xdrproc_t)xdr_void;
308 local = (svc_cb *)ypbindproc_null_2_yp;
311 case YPBINDPROC_DOMAIN:
312 xdr_argument = (xdrproc_t)xdr_domainname;
313 xdr_result = (xdrproc_t)xdr_ypbind_resp;
314 local = (svc_cb *)ypbindproc_domain_2_yp;
317 case YPBINDPROC_SETDOM:
318 switch (rqstp->rq_cred.oa_flavor) {
320 creds = (struct authunix_parms *)rqstp->rq_clntcred;
321 if (creds->aup_uid != 0) {
322 svcerr_auth(transp, AUTH_BADCRED);
327 svcerr_auth(transp, AUTH_TOOWEAK);
331 xdr_argument = (xdrproc_t)xdr_ypbind_setdom;
332 xdr_result = (xdrproc_t)xdr_void;
333 local = (svc_cb *)ypbindproc_setdom_2_yp;
337 svcerr_noproc(transp);
340 bzero(&argument, sizeof(argument));
341 if (!svc_getargs(transp, xdr_argument, &argument)) {
342 svcerr_decode(transp);
345 result = (*local)(transp, &argument, rqstp);
346 if (result != NULL &&
347 !svc_sendreply(transp, xdr_result, result)) {
348 svcerr_systemerr(transp);
353 /* Jack the reaper */
359 while (wait3(&st, WNOHANG, NULL) > 0)
366 struct _dom_binding *ypdb;
367 char path[MAXPATHLEN];
369 if (ppid != getpid())
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);
382 pmap_unset(YPBINDPROG, YPBINDVERS);
387 main(int argc, char *argv[])
393 struct _dom_binding *ypdb, *next;
395 /* Check that another ypbind isn't already running. */
396 if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
397 err(1, "%s", YPBINDLOCK);
399 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
400 errx(1, "another ypbind is already running. Aborting");
402 /* XXX domainname will be overridden if we use restricted mode */
403 yp_get_default_domain(&domain_name);
404 if (domain_name[0] == '\0')
405 errx(1, "domainname not set. Aborting");
407 for (i = 1; i<argc; i++) {
408 if (strcmp("-ypset", argv[i]) == 0)
409 ypsetmode = YPSET_ALL;
410 else if (strcmp("-ypsetme", argv[i]) == 0)
411 ypsetmode = YPSET_LOCAL;
412 else if (strcmp("-s", argv[i]) == 0)
414 else if (strcmp("-S", argv[i]) == 0 && argc > i)
415 yp_restricted_mode(argv[++i]);
416 else if (strcmp("-m", argv[i]) == 0)
419 errx(1, "unknown option: %s", argv[i]);
422 if (strlen(domain_name) > YPMAXDOMAIN)
423 warnx("truncating domain name %s", domain_name);
425 /* blow away everything in BINDINGDIR (if it exists) */
427 if ((dird = opendir(BINDINGDIR)) != NULL) {
428 char path[MAXPATHLEN];
429 while ((dirp = readdir(dird)) != NULL)
430 if (strcmp(dirp->d_name, ".") &&
431 strcmp(dirp->d_name, "..")) {
432 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
443 pmap_unset(YPBINDPROG, YPBINDVERS);
445 udptransp = svcudp_create(RPC_ANYSOCK);
446 if (udptransp == NULL)
447 errx(1, "cannot create udp service");
448 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
450 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
452 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
453 if (tcptransp == NULL)
454 errx(1, "cannot create tcp service");
456 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
458 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
460 /* build initial domain binding, make it "unsuccessful" */
461 ypbindlist = malloc(sizeof *ypbindlist);
462 if (ypbindlist == NULL)
464 bzero(ypbindlist, sizeof *ypbindlist);
465 strlcpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
466 ypbindlist->dom_vers = YPVERS;
467 ypbindlist->dom_alive = 0;
468 ypbindlist->dom_lockfd = -1;
469 ypbindlist->dom_default = 1;
472 signal(SIGCHLD, reaper);
473 signal(SIGTERM, terminate);
475 ppid = getpid(); /* Remember who we are. */
477 openlog(argv[0], LOG_PID, LOG_DAEMON);
479 if (madvise(NULL, 0, MADV_PROTECT) != 0)
480 syslog(LOG_WARNING, "madvise(): %m");
482 /* Kick off the default domain */
483 broadcast(ypbindlist);
491 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
497 syslog(LOG_WARNING, "select: %m");
500 for (ypdb = ypbindlist; ypdb; ypdb = next) {
501 next = ypdb->dom_pnext;
502 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
503 handle_children(ypdb);
504 if (children == (MAX_CHILDREN - 1))
508 svc_getreqset(&fdsr);
520 struct _dom_binding *ypdb;
522 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
526 /* The clnt_broadcast() callback mechanism sucks. */
529 * Receive results from broadcaster. Don't worry about passing
530 * bogus info to rpc_received() -- it can handle it. Note that we
531 * must be sure to invalidate the dom_pipe_fds descriptors here:
532 * since descriptors can be re-used, we have to make sure we
533 * don't mistake one of the RPC descriptors for one of the pipes.
534 * What's weird is that forgetting to invalidate the pipe descriptors
535 * doesn't always result in an error (otherwise I would have caught
536 * the mistake much sooner), even though logically it should.
539 handle_children(struct _dom_binding *ypdb)
541 char buf[YPMAXDOMAIN + 1];
542 struct sockaddr_in addr;
544 struct _dom_binding *y, *prev = NULL;
545 char path[MAXPATHLEN];
547 if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
548 syslog(LOG_WARNING, "could not read from child: %m");
550 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
551 syslog(LOG_WARNING, "could not read from child: %m");
554 FD_CLR(READFD, &fdsr);
555 FD_CLR(READFD, &svc_fdset);
556 READFD = WRITEFD = -1;
558 rpc_received(buf, &addr, 0);
560 for (y = ypbindlist; y; y = y->dom_pnext) {
565 switch (ypdb->dom_default) {
568 ypbindlist = y->dom_pnext;
570 prev->dom_pnext = y->dom_pnext;
571 sprintf(path, "%s/%s.%ld", BINDINGDIR,
572 ypdb->dom_domain, YPVERS);
573 close(ypdb->dom_lockfd);
579 ypdb->dom_broadcast_pid = 0;
592 * Send our dying words back to our parent before we perish.
595 tell_parent(char *dom, struct sockaddr_in *addr)
597 char buf[YPMAXDOMAIN + 1];
598 struct timeval timeout;
604 sprintf(buf, "%s", broad_domain->dom_domain);
605 if (write(BROADFD, &buf, sizeof(buf)) < 0)
609 * Stay in sync with parent: wait for it to read our first
610 * message before sending the second.
614 FD_SET(BROADFD, &fds);
615 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
617 if (FD_ISSET(BROADFD, &fds)) {
618 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
629 broadcast_result(bool_t *out, struct sockaddr_in *addr)
631 if (retries >= MAX_RETRIES) {
632 bzero(addr, sizeof(struct sockaddr_in));
633 if (tell_parent(broad_domain->dom_domain, addr))
634 syslog(LOG_WARNING, "lost connection to parent");
638 if (yp_restricted && verify(addr->sin_addr)) {
640 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
643 if (tell_parent(broad_domain->dom_domain, addr))
644 syslog(LOG_WARNING, "lost connection to parent");
650 * The right way to send RPC broadcasts.
651 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
652 * blocks while waiting for replies, so we have to fork off separate
653 * broadcaster processes that do the waiting and then transmit their
654 * results back to the parent for processing. We also have to remember
655 * to save the name of the domain we're trying to bind in a global
656 * variable since clnt_broadcast() provides no way to pass things to
657 * the 'eachresult' callback function.
660 broadcast(struct _dom_binding *ypdb)
665 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
668 if (pipe(ypdb->dom_pipe_fds) < 0) {
669 syslog(LOG_WARNING, "pipe: %m");
673 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr) {
674 if (not_responding_count++ >= NOT_RESPONDING_HYSTERESIS) {
675 not_responding_count = NOT_RESPONDING_HYSTERESIS;
676 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
677 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
682 flock(ypdb->dom_lockfd, LOCK_UN);
684 switch ((ypdb->dom_broadcast_pid = fork())) {
687 signal(SIGCHLD, SIG_DFL);
688 signal(SIGTERM, SIG_DFL);
691 syslog(LOG_WARNING, "fork: %m");
697 FD_SET(READFD, &svc_fdset);
702 /* Release all locks before doing anything else. */
704 close(ypbindlist->dom_lockfd);
705 ypbindlist = ypbindlist->dom_pnext;
710 * Special 'many-cast' behavior. If we're in restricted mode,
711 * we have a list of possible server addresses to try. What
712 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
713 * procedure and time the replies. Whoever replies fastest
714 * gets to be our server. Note that this is not a broadcast
715 * operation: we transmit uni-cast datagrams only.
717 if (yp_restricted && yp_manycast) {
720 struct sockaddr_in sin;
722 i = __yp_ping(restricted_addrs, yp_restricted,
723 ypdb->dom_domain, &port);
725 bzero(&ypdb->dom_server_addr,
726 sizeof(struct sockaddr_in));
727 if (tell_parent(ypdb->dom_domain,
728 &ypdb->dom_server_addr))
729 syslog(LOG_WARNING, "lost connection to parent");
731 bzero(&sin, sizeof(struct sockaddr_in));
732 bcopy(&restricted_addrs[i],
733 &sin.sin_addr, sizeof(struct in_addr));
734 sin.sin_family = AF_INET;
736 if (tell_parent(broad_domain->dom_domain, &sin))
738 "lost connection to parent");
748 ptr = ypdb->dom_domain;
749 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
750 (xdrproc_t)xdr_domainname, &ptr,
751 (xdrproc_t)xdr_bool, &out,
752 (resultproc_t)broadcast_result);
755 if (stat != RPC_SUCCESS) {
756 bzero(&ypdb->dom_server_addr,
757 sizeof(struct sockaddr_in));
758 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
759 syslog(LOG_WARNING, "lost connection to parent");
766 * The right way to check if a server is alive.
767 * Attempt to get a client handle pointing to the server and send a
768 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
769 * we invalidate this binding entry and send out a broadcast to try to
770 * establish a new binding. Note that we treat non-default domains
771 * specially: once bound, we keep tabs on our server, but if it
772 * goes away and fails to respond after one round of broadcasting, we
773 * abandon it until a client specifically references it again. We make
774 * every effort to keep our default domain bound, however, since we
775 * need it to keep the system on its feet.
778 ping(struct _dom_binding *ypdb)
781 struct timeval interval, timeout;
783 int rpcsock = RPC_ANYSOCK;
784 CLIENT *client_handle;
786 interval.tv_sec = FAIL_THRESHOLD;
787 interval.tv_usec = 0;
788 timeout.tv_sec = FAIL_THRESHOLD;
791 if (ypdb->dom_broadcast_pid)
794 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
795 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
796 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
797 /* Can't get a handle: we're dead. */
807 ptr = ypdb->dom_domain;
809 stat = clnt_call(client_handle, YPPROC_DOMAIN,
810 (xdrproc_t)xdr_domainname, &ptr,
811 (xdrproc_t)xdr_bool, &out, timeout);
812 if (stat != RPC_SUCCESS || out == FALSE) {
815 clnt_destroy(client_handle);
821 clnt_destroy(client_handle);
826 rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
828 struct _dom_binding *ypdb, *prev = NULL;
830 struct ypbind_resp ybr;
831 char path[MAXPATHLEN];
834 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
835 ntohs(raddrp->sin_port), dom);*/
840 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
841 if (strcmp(ypdb->dom_domain, dom) == 0)
847 if (ypdb->dom_broadcast_pid) {
848 kill(ypdb->dom_broadcast_pid, SIGINT);
850 FD_CLR(READFD, &fdsr);
851 FD_CLR(READFD, &svc_fdset);
852 READFD = WRITEFD = -1;
856 /* if in secure mode, check originating port number */
857 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
858 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
859 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
862 ypdb->dom_broadcast_pid = 0;
868 if (raddrp->sin_addr.s_addr == (long)0) {
869 switch (ypdb->dom_default) {
872 ypbindlist = ypdb->dom_pnext;
874 prev->dom_pnext = ypdb->dom_pnext;
875 sprintf(path, "%s/%s.%ld", BINDINGDIR,
876 ypdb->dom_domain, YPVERS);
877 close(ypdb->dom_lockfd);
883 ypdb->dom_broadcast_pid = 0;
895 if (strlen(dom) > YPMAXDOMAIN) {
896 syslog(LOG_WARNING, "domain %s too long", dom);
899 ypdb = malloc(sizeof *ypdb);
901 syslog(LOG_WARNING, "malloc: %m");
904 bzero(ypdb, sizeof *ypdb);
905 strlcpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
906 ypdb->dom_lockfd = -1;
907 ypdb->dom_default = 0;
908 ypdb->dom_pnext = ypbindlist;
912 /* We've recovered from a crash: inform the world. */
913 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr) {
914 if (not_responding_count >= NOT_RESPONDING_HYSTERESIS) {
915 not_responding_count = 0;
916 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
917 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
921 bcopy(raddrp, &ypdb->dom_server_addr,
922 sizeof ypdb->dom_server_addr);
924 ypdb->dom_vers = YPVERS;
926 ypdb->dom_broadcast_pid = 0;
928 if (ypdb->dom_lockfd != -1)
929 close(ypdb->dom_lockfd);
931 sprintf(path, "%s/%s.%ld", BINDINGDIR,
932 ypdb->dom_domain, ypdb->dom_vers);
934 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
935 (void)mkdir(BINDINGDIR, 0755);
936 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
940 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
941 (void)mkdir(BINDINGDIR, 0755);
942 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
949 * ok, if BINDINGDIR exists, and we can create the binding file,
952 ypdb->dom_lockfd = fd;
954 iov[0].iov_base = (char *)&(udptransp->xp_port);
955 iov[0].iov_len = sizeof udptransp->xp_port;
956 iov[1].iov_base = (char *)&ybr;
957 iov[1].iov_len = sizeof ybr;
959 bzero(&ybr, sizeof ybr);
960 ybr.ypbind_status = YPBIND_SUCC_VAL;
961 memcpy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
962 &raddrp->sin_addr.s_addr, sizeof(u_int32_t));
963 memcpy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
964 &raddrp->sin_port, sizeof(u_short));
966 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
967 syslog(LOG_WARNING, "write: %m");
968 close(ypdb->dom_lockfd);
969 ypdb->dom_lockfd = -1;
975 * Check address against list of allowed servers. Return 0 if okay,
979 verify(struct in_addr addr)
983 for (i = 0; i < RESTRICTED_SERVERS; i++)
984 if (!bcmp(&addr, &restricted_addrs[i], sizeof(struct in_addr)))
991 * Try to set restricted mode. We default to normal mode if we can't
992 * resolve the specified hostnames.
995 yp_restricted_mode(char *args)
1001 /* Find the restricted domain. */
1002 if ((s = strsep(&args, ",")) == NULL)
1006 /* Get the addresses of the servers. */
1007 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
1008 if ((h = gethostbyname(s)) == NULL)
1010 bcopy (h->h_addr_list[0], &restricted_addrs[i],
1011 sizeof(struct in_addr));
1015 /* ypset and ypsetme not allowed with restricted mode */
1016 ypsetmode = YPSET_NO;