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 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
40 #include <sys/signal.h>
41 #include <sys/socket.h>
43 #include <sys/fcntl.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <rpc/pmap_clnt.h>
63 #include <rpc/pmap_prot.h>
64 #include <rpc/pmap_rmt.h>
65 #include <rpc/rpc_com.h>
66 #include <rpcsvc/yp.h>
67 #include <rpcsvc/ypclnt.h>
71 #define BINDINGDIR "/var/yp/binding"
75 #define YPBINDLOCK "/var/run/ypbind.lock"
79 struct _dom_binding *dom_pnext;
80 char dom_domain[YPMAXDOMAIN + 1];
81 struct sockaddr_in dom_server_addr;
85 int dom_broadcast_pid;
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]
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();
99 void *ypbindproc_null_2_yp(SVCXPRT *, void *, CLIENT *);
100 void *ypbindproc_setdom_2_yp(SVCXPRT *, struct ypbind_setdom *, CLIENT *);
101 void rpc_received(char *, struct sockaddr_in *, int);
102 void broadcast(struct _dom_binding *);
103 int ping(struct _dom_binding *);
104 int tell_parent(char *, struct sockaddr_in *);
105 void handle_children(struct _dom_binding *);
108 void yp_restricted_mode(char *);
109 int verify(struct in_addr);
111 static char *domain_name;
112 static struct _dom_binding *ypbindlist;
113 static struct _dom_binding *broad_domain;
116 #define YPSET_LOCAL 1
118 static int ypsetmode = YPSET_NO;
119 static int ypsecuremode = 0;
122 #define NOT_RESPONDING_HYSTERESIS 10
123 static int not_responding_count = 0;
126 * Special restricted mode variables: when in restricted mode, only the
127 * specified restricted_domain will be bound, and only the servers listed
128 * in restricted_addrs will be used for binding.
130 #define RESTRICTED_SERVERS 10
131 static int yp_restricted = 0;
132 static int yp_manycast = 0;
133 static struct in_addr restricted_addrs[RESTRICTED_SERVERS];
135 /* No more than MAX_CHILDREN child broadcasters at a time. */
137 #define MAX_CHILDREN 5
139 /* No more than MAX_DOMAINS simultaneous domains */
141 #define MAX_DOMAINS 200
143 /* RPC timeout value */
144 #ifndef FAIL_THRESHOLD
145 #define FAIL_THRESHOLD 20
148 /* Number of times to fish for a response froma particular set of hosts */
150 #define MAX_RETRIES 30
153 static int retries = 0;
154 static int children = 0;
155 static int domains = 0;
159 static SVCXPRT *udptransp, *tcptransp;
162 ypbindproc_null_2_yp(SVCXPRT *transp, void *argp, CLIENT *clnt)
166 bzero(&res, sizeof(res));
170 static struct ypbind_resp *
171 ypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt)
173 static struct ypbind_resp res;
174 struct _dom_binding *ypdb;
175 char path[MAXPATHLEN];
177 bzero(&res, sizeof res);
178 res.ypbind_status = YPBIND_FAIL_VAL;
179 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
181 if (strchr(*argp, '/')) {
182 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
187 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
188 if (strcmp(ypdb->dom_domain, *argp) == 0)
194 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
198 if (domains >= MAX_DOMAINS) {
199 syslog(LOG_WARNING, "domain limit (%d) exceeded",
201 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
204 if (strlen(*argp) > YPMAXDOMAIN) {
205 syslog(LOG_WARNING, "domain %s too long", *argp);
206 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
209 ypdb = malloc(sizeof *ypdb);
211 syslog(LOG_WARNING, "malloc: %m");
212 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
215 bzero(ypdb, sizeof *ypdb);
216 strlcpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
217 ypdb->dom_vers = YPVERS;
219 ypdb->dom_default = 0;
220 ypdb->dom_lockfd = -1;
221 sprintf(path, "%s/%s.%ld", BINDINGDIR,
222 ypdb->dom_domain, ypdb->dom_vers);
224 ypdb->dom_pnext = ypbindlist;
233 res.ypbind_status = YPBIND_SUCC_VAL;
234 res.ypbind_resp_u.ypbind_error = 0; /* Success */
235 memcpy(&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
236 &ypdb->dom_server_addr.sin_addr.s_addr, sizeof(u_int32_t));
237 memcpy(&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
238 &ypdb->dom_server_addr.sin_port, sizeof(u_short));
239 /*printf("domain %s at %s/%d\n", ypdb->dom_domain,
240 inet_ntoa(ypdb->dom_server_addr.sin_addr),
241 ntohs(ypdb->dom_server_addr.sin_port));*/
246 ypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt)
248 struct sockaddr_in *fromsin, bindsin;
249 static char *result = NULL;
251 if (strchr(argp->ypsetdom_domain, '/')) {
252 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
253 rejecting.", argp->ypsetdom_domain);
256 fromsin = svc_getcaller(transp);
260 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
261 svcerr_noprog(transp);
269 svcerr_noprog(transp);
273 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
274 svcerr_noprog(transp);
278 if (argp->ypsetdom_vers != YPVERS) {
279 svcerr_noprog(transp);
283 bzero(&bindsin, sizeof bindsin);
284 bindsin.sin_family = AF_INET;
285 memcpy(&bindsin.sin_addr.s_addr,
286 &argp->ypsetdom_binding.ypbind_binding_addr,
288 memcpy(&bindsin.sin_port,
289 &argp->ypsetdom_binding.ypbind_binding_port,
291 rpc_received(argp->ypsetdom_domain, &bindsin, 1);
293 return((void *) &result);
297 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
300 domainname ypbindproc_domain_2_arg;
301 struct ypbind_setdom ypbindproc_setdom_2_arg;
303 struct authunix_parms *creds;
305 bool_t (*xdr_argument)(), (*xdr_result)();
308 switch (rqstp->rq_proc) {
309 case YPBINDPROC_NULL:
310 xdr_argument = xdr_void;
311 xdr_result = xdr_void;
312 local = (char *(*)()) ypbindproc_null_2_yp;
315 case YPBINDPROC_DOMAIN:
316 xdr_argument = xdr_domainname;
317 xdr_result = xdr_ypbind_resp;
318 local = (char *(*)()) ypbindproc_domain_2_yp;
321 case YPBINDPROC_SETDOM:
322 switch (rqstp->rq_cred.oa_flavor) {
324 creds = (struct authunix_parms *)rqstp->rq_clntcred;
325 if (creds->aup_uid != 0) {
326 svcerr_auth(transp, AUTH_BADCRED);
331 svcerr_auth(transp, AUTH_TOOWEAK);
335 xdr_argument = xdr_ypbind_setdom;
336 xdr_result = xdr_void;
337 local = (char *(*)()) ypbindproc_setdom_2_yp;
341 svcerr_noproc(transp);
344 bzero(&argument, sizeof(argument));
345 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
346 svcerr_decode(transp);
349 result = (*local)(transp, &argument, rqstp);
350 if (result != NULL &&
351 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
352 svcerr_systemerr(transp);
357 /* Jack the reaper */
363 while (wait3(&st, WNOHANG, NULL) > 0)
370 struct _dom_binding *ypdb;
371 char path[MAXPATHLEN];
373 if (ppid != getpid())
376 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
377 close(ypdb->dom_lockfd);
378 if (ypdb->dom_broadcast_pid)
379 kill(ypdb->dom_broadcast_pid, SIGINT);
380 sprintf(path, "%s/%s.%ld", BINDINGDIR,
381 ypdb->dom_domain, ypdb->dom_vers);
386 pmap_unset(YPBINDPROG, YPBINDVERS);
391 main(int argc, char *argv[])
397 struct _dom_binding *ypdb, *next;
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);
403 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
404 errx(1, "another ypbind is already running. Aborting");
406 /* XXX domainname will be overridden 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");
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)
418 else if (strcmp("-S", argv[i]) == 0 && argc > i)
419 yp_restricted_mode(argv[++i]);
420 else if (strcmp("-m", argv[i]) == 0)
423 errx(1, "unknown option: %s", argv[i]);
426 if (strlen(domain_name) > YPMAXDOMAIN)
427 warnx("truncating domain name %s", domain_name);
429 /* blow away everything in BINDINGDIR (if it exists) */
431 if ((dird = opendir(BINDINGDIR)) != NULL) {
432 char path[MAXPATHLEN];
433 while ((dirp = readdir(dird)) != NULL)
434 if (strcmp(dirp->d_name, ".") &&
435 strcmp(dirp->d_name, "..")) {
436 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
447 pmap_unset(YPBINDPROG, YPBINDVERS);
449 udptransp = svcudp_create(RPC_ANYSOCK);
450 if (udptransp == NULL)
451 errx(1, "cannot create udp service");
452 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
454 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
456 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
457 if (tcptransp == NULL)
458 errx(1, "cannot create tcp service");
460 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
462 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
464 /* build initial domain binding, make it "unsuccessful" */
465 ypbindlist = malloc(sizeof *ypbindlist);
466 if (ypbindlist == NULL)
468 bzero(ypbindlist, sizeof *ypbindlist);
469 strlcpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
470 ypbindlist->dom_vers = YPVERS;
471 ypbindlist->dom_alive = 0;
472 ypbindlist->dom_lockfd = -1;
473 ypbindlist->dom_default = 1;
476 signal(SIGCHLD, reaper);
477 signal(SIGTERM, terminate);
479 ppid = getpid(); /* Remember who we are. */
481 openlog(argv[0], LOG_PID, LOG_DAEMON);
483 if (madvise(NULL, 0, MADV_PROTECT) != 0)
484 syslog(LOG_WARNING, "madvise(): %m");
486 /* Kick off the default domain */
487 broadcast(ypbindlist);
495 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
501 syslog(LOG_WARNING, "select: %m");
504 for (ypdb = ypbindlist; ypdb; ypdb = next) {
505 next = ypdb->dom_pnext;
506 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
507 handle_children(ypdb);
508 if (children == (MAX_CHILDREN - 1))
512 svc_getreqset(&fdsr);
524 struct _dom_binding *ypdb;
526 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
530 /* The clnt_broadcast() callback mechanism sucks. */
533 * Receive results from broadcaster. Don't worry about passing
534 * bogus info to rpc_received() -- it can handle it. Note that we
535 * must be sure to invalidate the dom_pipe_fds descriptors here:
536 * since descriptors can be re-used, we have to make sure we
537 * don't mistake one of the RPC descriptors for one of the pipes.
538 * What's weird is that forgetting to invalidate the pipe descriptors
539 * doesn't always result in an error (otherwise I would have caught
540 * the mistake much sooner), even though logically it should.
543 handle_children(struct _dom_binding *ypdb)
545 char buf[YPMAXDOMAIN + 1];
546 struct sockaddr_in addr;
548 struct _dom_binding *y, *prev = NULL;
549 char path[MAXPATHLEN];
551 if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
552 syslog(LOG_WARNING, "could not read from child: %m");
554 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
555 syslog(LOG_WARNING, "could not read from child: %m");
558 FD_CLR(READFD, &fdsr);
559 FD_CLR(READFD, &svc_fdset);
560 READFD = WRITEFD = -1;
562 rpc_received(buf, &addr, 0);
564 for (y = ypbindlist; y; y = y->dom_pnext) {
569 switch (ypdb->dom_default) {
572 ypbindlist = y->dom_pnext;
574 prev->dom_pnext = y->dom_pnext;
575 sprintf(path, "%s/%s.%ld", BINDINGDIR,
576 ypdb->dom_domain, YPVERS);
577 close(ypdb->dom_lockfd);
583 ypdb->dom_broadcast_pid = 0;
596 * Send our dying words back to our parent before we perish.
599 tell_parent(char *dom, struct sockaddr_in *addr)
601 char buf[YPMAXDOMAIN + 1];
602 struct timeval timeout;
608 sprintf(buf, "%s", broad_domain->dom_domain);
609 if (write(BROADFD, &buf, sizeof(buf)) < 0)
613 * Stay in sync with parent: wait for it to read our first
614 * message before sending the second.
618 FD_SET(BROADFD, &fds);
619 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
621 if (FD_ISSET(BROADFD, &fds)) {
622 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
633 broadcast_result(bool_t *out, struct sockaddr_in *addr)
635 if (retries >= MAX_RETRIES) {
636 bzero(addr, sizeof(struct sockaddr_in));
637 if (tell_parent(broad_domain->dom_domain, addr))
638 syslog(LOG_WARNING, "lost connection to parent");
642 if (yp_restricted && verify(addr->sin_addr)) {
644 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
647 if (tell_parent(broad_domain->dom_domain, addr))
648 syslog(LOG_WARNING, "lost connection to parent");
654 * The right way to send RPC broadcasts.
655 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
656 * blocks while waiting for replies, so we have to fork off separate
657 * broadcaster processes that do the waiting and then transmit their
658 * results back to the parent for processing. We also have to remember
659 * to save the name of the domain we're trying to bind in a global
660 * variable since clnt_broadcast() provides no way to pass things to
661 * the 'eachresult' callback function.
664 broadcast(struct _dom_binding *ypdb)
669 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
672 if (pipe(ypdb->dom_pipe_fds) < 0) {
673 syslog(LOG_WARNING, "pipe: %m");
677 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr) {
678 if (not_responding_count++ >= NOT_RESPONDING_HYSTERESIS) {
679 not_responding_count = NOT_RESPONDING_HYSTERESIS;
680 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
681 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
686 flock(ypdb->dom_lockfd, LOCK_UN);
688 switch ((ypdb->dom_broadcast_pid = fork())) {
691 signal(SIGCHLD, SIG_DFL);
692 signal(SIGTERM, SIG_DFL);
695 syslog(LOG_WARNING, "fork: %m");
701 FD_SET(READFD, &svc_fdset);
706 /* Release all locks before doing anything else. */
708 close(ypbindlist->dom_lockfd);
709 ypbindlist = ypbindlist->dom_pnext;
714 * Special 'many-cast' behavior. If we're in restricted mode,
715 * we have a list of possible server addresses to try. What
716 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
717 * procedure and time the replies. Whoever replies fastest
718 * gets to be our server. Note that this is not a broadcast
719 * operation: we transmit uni-cast datagrams only.
721 if (yp_restricted && yp_manycast) {
724 struct sockaddr_in sin;
726 i = __yp_ping(restricted_addrs, yp_restricted,
727 ypdb->dom_domain, &port);
729 bzero(&ypdb->dom_server_addr,
730 sizeof(struct sockaddr_in));
731 if (tell_parent(ypdb->dom_domain,
732 &ypdb->dom_server_addr))
733 syslog(LOG_WARNING, "lost connection to parent");
735 bzero(&sin, sizeof(struct sockaddr_in));
736 bcopy(&restricted_addrs[i],
737 &sin.sin_addr, sizeof(struct in_addr));
738 sin.sin_family = AF_INET;
740 if (tell_parent(broad_domain->dom_domain, &sin))
742 "lost connection to parent");
752 ptr = ypdb->dom_domain;
753 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
754 (xdrproc_t)xdr_domainname, &ptr,
755 (xdrproc_t)xdr_bool, &out,
756 (resultproc_t)broadcast_result);
759 if (stat != RPC_SUCCESS) {
760 bzero(&ypdb->dom_server_addr,
761 sizeof(struct sockaddr_in));
762 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
763 syslog(LOG_WARNING, "lost connection to parent");
770 * The right way to check if a server is alive.
771 * Attempt to get a client handle pointing to the server and send a
772 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
773 * we invalidate this binding entry and send out a broadcast to try to
774 * establish a new binding. Note that we treat non-default domains
775 * specially: once bound, we keep tabs on our server, but if it
776 * goes away and fails to respond after one round of broadcasting, we
777 * abandon it until a client specifically references it again. We make
778 * every effort to keep our default domain bound, however, since we
779 * need it to keep the system on its feet.
782 ping(struct _dom_binding *ypdb)
785 struct timeval interval, timeout;
787 int rpcsock = RPC_ANYSOCK;
788 CLIENT *client_handle;
790 interval.tv_sec = FAIL_THRESHOLD;
791 interval.tv_usec = 0;
792 timeout.tv_sec = FAIL_THRESHOLD;
795 if (ypdb->dom_broadcast_pid)
798 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
799 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
800 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
801 /* Can't get a handle: we're dead. */
811 ptr = ypdb->dom_domain;
813 stat = clnt_call(client_handle, YPPROC_DOMAIN,
814 (xdrproc_t)xdr_domainname, &ptr,
815 (xdrproc_t)xdr_bool, &out, timeout);
816 if (stat != RPC_SUCCESS || out == FALSE) {
819 clnt_destroy(client_handle);
825 clnt_destroy(client_handle);
830 rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
832 struct _dom_binding *ypdb, *prev = NULL;
834 struct ypbind_resp ybr;
835 char path[MAXPATHLEN];
838 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
839 ntohs(raddrp->sin_port), dom);*/
844 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
845 if (strcmp(ypdb->dom_domain, dom) == 0)
851 if (ypdb->dom_broadcast_pid) {
852 kill(ypdb->dom_broadcast_pid, SIGINT);
854 FD_CLR(READFD, &fdsr);
855 FD_CLR(READFD, &svc_fdset);
856 READFD = WRITEFD = -1;
860 /* if in secure mode, check originating port number */
861 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
862 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
863 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
866 ypdb->dom_broadcast_pid = 0;
872 if (raddrp->sin_addr.s_addr == (long)0) {
873 switch (ypdb->dom_default) {
876 ypbindlist = ypdb->dom_pnext;
878 prev->dom_pnext = ypdb->dom_pnext;
879 sprintf(path, "%s/%s.%ld", BINDINGDIR,
880 ypdb->dom_domain, YPVERS);
881 close(ypdb->dom_lockfd);
887 ypdb->dom_broadcast_pid = 0;
899 if (strlen(dom) > YPMAXDOMAIN) {
900 syslog(LOG_WARNING, "domain %s too long", dom);
903 ypdb = malloc(sizeof *ypdb);
905 syslog(LOG_WARNING, "malloc: %m");
908 bzero(ypdb, sizeof *ypdb);
909 strlcpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
910 ypdb->dom_lockfd = -1;
911 ypdb->dom_default = 0;
912 ypdb->dom_pnext = ypbindlist;
916 /* We've recovered from a crash: inform the world. */
917 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr) {
918 if (not_responding_count >= NOT_RESPONDING_HYSTERESIS) {
919 not_responding_count = 0;
920 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
921 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
925 bcopy(raddrp, &ypdb->dom_server_addr,
926 sizeof ypdb->dom_server_addr);
928 ypdb->dom_vers = YPVERS;
930 ypdb->dom_broadcast_pid = 0;
932 if (ypdb->dom_lockfd != -1)
933 close(ypdb->dom_lockfd);
935 sprintf(path, "%s/%s.%ld", BINDINGDIR,
936 ypdb->dom_domain, ypdb->dom_vers);
938 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
939 (void)mkdir(BINDINGDIR, 0755);
940 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
944 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
945 (void)mkdir(BINDINGDIR, 0755);
946 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
953 * ok, if BINDINGDIR exists, and we can create the binding file,
956 ypdb->dom_lockfd = fd;
958 iov[0].iov_base = (char *)&(udptransp->xp_port);
959 iov[0].iov_len = sizeof udptransp->xp_port;
960 iov[1].iov_base = (char *)&ybr;
961 iov[1].iov_len = sizeof ybr;
963 bzero(&ybr, sizeof ybr);
964 ybr.ypbind_status = YPBIND_SUCC_VAL;
965 memcpy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
966 &raddrp->sin_addr.s_addr, sizeof(u_int32_t));
967 memcpy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
968 &raddrp->sin_port, sizeof(u_short));
970 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
971 syslog(LOG_WARNING, "write: %m");
972 close(ypdb->dom_lockfd);
973 ypdb->dom_lockfd = -1;
979 * Check address against list of allowed servers. Return 0 if okay,
983 verify(struct in_addr addr)
987 for (i = 0; i < RESTRICTED_SERVERS; i++)
988 if (!bcmp(&addr, &restricted_addrs[i], sizeof(struct in_addr)))
995 * Try to set restricted mode. We default to normal mode if we can't
996 * resolve the specified hostnames.
999 yp_restricted_mode(char *args)
1005 /* Find the restricted domain. */
1006 if ((s = strsep(&args, ",")) == NULL)
1010 /* Get the addresses of the servers. */
1011 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
1012 if ((h = gethostbyname(s)) == NULL)
1014 bcopy (h->h_addr_list[0], &restricted_addrs[i],
1015 sizeof(struct in_addr));
1019 /* ypset and ypsetme not allowed with restricted mode */
1020 ypsetmode = YPSET_NO;