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
31 static const char rcsid[] =
35 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/signal.h>
40 #include <sys/socket.h>
42 #include <sys/fcntl.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>
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);
112 struct _dom_binding *ypbindlist;
113 static struct _dom_binding *broad_domain;
116 #define YPSET_LOCAL 1
118 int ypsetmode = YPSET_NO;
119 int ypsecuremode = 0;
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.
127 #define RESTRICTED_SERVERS 10
128 int yp_restricted = 0;
130 struct in_addr restricted_addrs[RESTRICTED_SERVERS];
132 /* No more than MAX_CHILDREN child broadcasters at a time. */
134 #define MAX_CHILDREN 5
136 /* No more than MAX_DOMAINS simultaneous domains */
138 #define MAX_DOMAINS 200
140 /* RPC timeout value */
141 #ifndef FAIL_THRESHOLD
142 #define FAIL_THRESHOLD 20
145 /* Number of times to fish for a response froma particular set of hosts */
147 #define MAX_RETRIES 30
156 SVCXPRT *udptransp, *tcptransp;
159 ypbindproc_null_2_yp(SVCXPRT *transp, void *argp, CLIENT *clnt)
163 bzero(&res, sizeof(res));
168 ypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt)
170 static struct ypbind_resp res;
171 struct _dom_binding *ypdb;
172 char path[MAXPATHLEN];
174 bzero(&res, sizeof res);
175 res.ypbind_status = YPBIND_FAIL_VAL;
176 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
178 if (strchr(*argp, '/')) {
179 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
184 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
185 if (strcmp(ypdb->dom_domain, *argp) == 0)
191 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
195 if (domains >= MAX_DOMAINS) {
196 syslog(LOG_WARNING, "domain limit (%d) exceeded",
198 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
201 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
203 syslog(LOG_WARNING, "malloc: %m");
204 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
207 bzero(ypdb, sizeof *ypdb);
208 strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
209 ypdb->dom_vers = YPVERS;
211 ypdb->dom_default = 0;
212 ypdb->dom_lockfd = -1;
213 sprintf(path, "%s/%s.%ld", BINDINGDIR,
214 ypdb->dom_domain, ypdb->dom_vers);
216 ypdb->dom_pnext = ypbindlist;
225 res.ypbind_status = YPBIND_SUCC_VAL;
226 res.ypbind_resp_u.ypbind_error = 0; /* Success */
227 *(u_int32_t *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr =
228 ypdb->dom_server_addr.sin_addr.s_addr;
229 *(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port =
230 ypdb->dom_server_addr.sin_port;
231 /*printf("domain %s at %s/%d\n", ypdb->dom_domain,
232 inet_ntoa(ypdb->dom_server_addr.sin_addr),
233 ntohs(ypdb->dom_server_addr.sin_port));*/
238 ypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt)
240 struct sockaddr_in *fromsin, bindsin;
241 static char *result = NULL;
243 if (strchr(argp->ypsetdom_domain, '/')) {
244 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
245 rejecting.", argp->ypsetdom_domain);
248 fromsin = svc_getcaller(transp);
252 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
253 svcerr_noprog(transp);
261 svcerr_noprog(transp);
265 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
266 svcerr_noprog(transp);
270 if (argp->ypsetdom_vers != YPVERS) {
271 svcerr_noprog(transp);
275 bzero(&bindsin, sizeof bindsin);
276 bindsin.sin_family = AF_INET;
277 bindsin.sin_addr.s_addr = *(u_int32_t *)argp->ypsetdom_binding.ypbind_binding_addr;
278 bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
279 rpc_received(argp->ypsetdom_domain, &bindsin, 1);
281 return((void *) &result);
285 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
288 domainname ypbindproc_domain_2_arg;
289 struct ypbind_setdom ypbindproc_setdom_2_arg;
291 struct authunix_parms *creds;
293 bool_t (*xdr_argument)(), (*xdr_result)();
296 switch (rqstp->rq_proc) {
297 case YPBINDPROC_NULL:
298 xdr_argument = xdr_void;
299 xdr_result = xdr_void;
300 local = (char *(*)()) ypbindproc_null_2_yp;
303 case YPBINDPROC_DOMAIN:
304 xdr_argument = xdr_domainname;
305 xdr_result = xdr_ypbind_resp;
306 local = (char *(*)()) ypbindproc_domain_2_yp;
309 case YPBINDPROC_SETDOM:
310 switch (rqstp->rq_cred.oa_flavor) {
312 creds = (struct authunix_parms *)rqstp->rq_clntcred;
313 if (creds->aup_uid != 0) {
314 svcerr_auth(transp, AUTH_BADCRED);
319 svcerr_auth(transp, AUTH_TOOWEAK);
323 xdr_argument = xdr_ypbind_setdom;
324 xdr_result = xdr_void;
325 local = (char *(*)()) ypbindproc_setdom_2_yp;
329 svcerr_noproc(transp);
332 bzero(&argument, sizeof(argument));
333 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
334 svcerr_decode(transp);
337 result = (*local)(transp, &argument, rqstp);
338 if (result != NULL &&
339 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
340 svcerr_systemerr(transp);
345 /* Jack the reaper */
351 while (wait3(&st, WNOHANG, NULL) > 0)
358 struct _dom_binding *ypdb;
359 char path[MAXPATHLEN];
361 if (ppid != getpid())
364 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
365 close(ypdb->dom_lockfd);
366 if (ypdb->dom_broadcast_pid)
367 kill(ypdb->dom_broadcast_pid, SIGINT);
368 sprintf(path, "%s/%s.%ld", BINDINGDIR,
369 ypdb->dom_domain, ypdb->dom_vers);
374 pmap_unset(YPBINDPROG, YPBINDVERS);
379 main(int argc, char *argv[])
385 struct _dom_binding *ypdb, *next;
387 /* Check that another ypbind isn't already running. */
388 if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
389 err(1, "%s", YPBINDLOCK);
391 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
392 errx(1, "another ypbind is already running. Aborting");
394 /* XXX domainname will be overriden if we use restricted mode */
395 yp_get_default_domain(&domain_name);
396 if (domain_name[0] == '\0')
397 errx(1, "domainname not set. Aborting");
399 for (i = 1; i<argc; i++) {
400 if (strcmp("-ypset", argv[i]) == 0)
401 ypsetmode = YPSET_ALL;
402 else if (strcmp("-ypsetme", argv[i]) == 0)
403 ypsetmode = YPSET_LOCAL;
404 else if (strcmp("-s", argv[i]) == 0)
406 else if (strcmp("-S", argv[i]) == 0 && argc > i)
407 yp_restricted_mode(argv[++i]);
408 else if (strcmp("-m", argv[i]) == 0)
411 errx(1, "unknown option: %s", argv[i]);
414 /* blow away everything in BINDINGDIR (if it exists) */
416 if ((dird = opendir(BINDINGDIR)) != NULL) {
417 char path[MAXPATHLEN];
418 while ((dirp = readdir(dird)) != NULL)
419 if (strcmp(dirp->d_name, ".") &&
420 strcmp(dirp->d_name, "..")) {
421 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
432 pmap_unset(YPBINDPROG, YPBINDVERS);
434 udptransp = svcudp_create(RPC_ANYSOCK);
435 if (udptransp == NULL)
436 errx(1, "cannot create udp service");
437 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
439 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
441 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
442 if (tcptransp == NULL)
443 errx(1, "cannot create tcp service");
445 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
447 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
449 /* build initial domain binding, make it "unsuccessful" */
450 ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
451 if (ypbindlist == NULL)
453 bzero(ypbindlist, sizeof *ypbindlist);
454 strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
455 ypbindlist->dom_vers = YPVERS;
456 ypbindlist->dom_alive = 0;
457 ypbindlist->dom_lockfd = -1;
458 ypbindlist->dom_default = 1;
461 signal(SIGCHLD, reaper);
462 signal(SIGTERM, terminate);
464 ppid = getpid(); /* Remember who we are. */
466 openlog(argv[0], LOG_PID, LOG_DAEMON);
468 /* Kick off the default domain */
469 broadcast(ypbindlist);
477 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
483 syslog(LOG_WARNING, "select: %m");
486 for (ypdb = ypbindlist; ypdb; ypdb = next) {
487 next = ypdb->dom_pnext;
488 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
489 handle_children(ypdb);
490 if (children == (MAX_CHILDREN - 1))
494 svc_getreqset(&fdsr);
506 struct _dom_binding *ypdb;
508 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
512 /* The clnt_broadcast() callback mechanism sucks. */
515 * Receive results from broadcaster. Don't worry about passing
516 * bogus info to rpc_received() -- it can handle it. Note that we
517 * must be sure to invalidate the dom_pipe_fds descriptors here:
518 * since descriptors can be re-used, we have to make sure we
519 * don't mistake one of the RPC descriptors for one of the pipes.
520 * What's weird is that forgetting to invalidate the pipe descriptors
521 * doesn't always result in an error (otherwise I would have caught
522 * the mistake much sooner), even though logically it should.
525 handle_children(struct _dom_binding *ypdb)
527 char buf[YPMAXDOMAIN + 1];
528 struct sockaddr_in addr;
530 struct _dom_binding *y, *prev = NULL;
531 char path[MAXPATHLEN];
533 if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
534 syslog(LOG_WARNING, "could not read from child: %m");
536 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
537 syslog(LOG_WARNING, "could not read from child: %m");
540 FD_CLR(READFD, &fdsr);
541 FD_CLR(READFD, &svc_fdset);
542 READFD = WRITEFD = -1;
544 rpc_received(buf, &addr, 0);
546 for (y = ypbindlist; y; y = y->dom_pnext) {
551 switch (ypdb->dom_default) {
554 ypbindlist = y->dom_pnext;
556 prev->dom_pnext = y->dom_pnext;
557 sprintf(path, "%s/%s.%ld", BINDINGDIR,
558 ypdb->dom_domain, YPVERS);
559 close(ypdb->dom_lockfd);
565 ypdb->dom_broadcast_pid = 0;
578 * Send our dying words back to our parent before we perish.
581 tell_parent(char *dom, struct sockaddr_in *addr)
583 char buf[YPMAXDOMAIN + 1];
584 struct timeval timeout;
590 sprintf(buf, "%s", broad_domain->dom_domain);
591 if (write(BROADFD, &buf, sizeof(buf)) < 0)
595 * Stay in sync with parent: wait for it to read our first
596 * message before sending the second.
600 FD_SET(BROADFD, &fds);
601 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
603 if (FD_ISSET(BROADFD, &fds)) {
604 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
614 bool_t broadcast_result(out, addr)
616 struct sockaddr_in *addr;
618 if (retries >= MAX_RETRIES) {
619 bzero(addr, sizeof(struct sockaddr_in));
620 if (tell_parent(broad_domain->dom_domain, addr))
621 syslog(LOG_WARNING, "lost connection to parent");
625 if (yp_restricted && verify(addr->sin_addr)) {
627 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
630 if (tell_parent(broad_domain->dom_domain, addr))
631 syslog(LOG_WARNING, "lost connection to parent");
637 * The right way to send RPC broadcasts.
638 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
639 * blocks while waiting for replies, so we have to fork off separate
640 * broadcaster processes that do the waiting and then transmit their
641 * results back to the parent for processing. We also have to remember
642 * to save the name of the domain we're trying to bind in a global
643 * variable since clnt_broadcast() provides no way to pass things to
644 * the 'eachresult' callback function.
647 broadcast(struct _dom_binding *ypdb)
652 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
655 if (pipe(ypdb->dom_pipe_fds) < 0) {
656 syslog(LOG_WARNING, "pipe: %m");
660 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
661 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
662 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
665 flock(ypdb->dom_lockfd, LOCK_UN);
667 switch ((ypdb->dom_broadcast_pid = fork())) {
670 signal(SIGCHLD, SIG_DFL);
671 signal(SIGTERM, SIG_DFL);
674 syslog(LOG_WARNING, "fork: %m");
680 FD_SET(READFD, &svc_fdset);
685 /* Release all locks before doing anything else. */
687 close(ypbindlist->dom_lockfd);
688 ypbindlist = ypbindlist->dom_pnext;
693 * Special 'many-cast' behavior. If we're in restricted mode,
694 * we have a list of possible server addresses to try. What
695 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
696 * procedure and time the replies. Whoever replies fastest
697 * gets to be our server. Note that this is not a broadcast
698 * operation: we transmit uni-cast datagrams only.
700 if (yp_restricted && yp_manycast) {
703 struct sockaddr_in sin;
705 i = __yp_ping(restricted_addrs, yp_restricted,
706 ypdb->dom_domain, &port);
708 bzero(&ypdb->dom_server_addr,
709 sizeof(struct sockaddr_in));
710 if (tell_parent(ypdb->dom_domain,
711 &ypdb->dom_server_addr))
712 syslog(LOG_WARNING, "lost connection to parent");
714 bzero(&sin, sizeof(struct sockaddr_in));
715 bcopy(&restricted_addrs[i],
716 &sin.sin_addr, sizeof(struct in_addr));
717 sin.sin_family = AF_INET;
719 if (tell_parent(broad_domain->dom_domain, &sin))
721 "lost connection to parent");
731 ptr = ypdb->dom_domain;
732 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
733 (xdrproc_t)xdr_domainname, &ptr,
734 (xdrproc_t)xdr_bool, &out,
735 (resultproc_t)broadcast_result);
738 if (stat != RPC_SUCCESS) {
739 bzero(&ypdb->dom_server_addr,
740 sizeof(struct sockaddr_in));
741 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
742 syslog(LOG_WARNING, "lost connection to parent");
749 * The right way to check if a server is alive.
750 * Attempt to get a client handle pointing to the server and send a
751 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
752 * we invalidate this binding entry and send out a broadcast to try to
753 * establish a new binding. Note that we treat non-default domains
754 * specially: once bound, we keep tabs on our server, but if it
755 * goes away and fails to respond after one round of broadcasting, we
756 * abandon it until a client specifically references it again. We make
757 * every effort to keep our default domain bound, however, since we
758 * need it to keep the system on its feet.
761 ping(struct _dom_binding *ypdb)
764 struct timeval interval, timeout;
766 int rpcsock = RPC_ANYSOCK;
767 CLIENT *client_handle;
769 interval.tv_sec = FAIL_THRESHOLD;
770 interval.tv_usec = 0;
771 timeout.tv_sec = FAIL_THRESHOLD;
774 if (ypdb->dom_broadcast_pid)
777 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
778 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
779 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
780 /* Can't get a handle: we're dead. */
790 ptr = ypdb->dom_domain;
792 stat = clnt_call(client_handle, YPPROC_DOMAIN,
793 (xdrproc_t)xdr_domainname, &ptr,
794 (xdrproc_t)xdr_bool, &out, timeout);
795 if (stat != RPC_SUCCESS || out == FALSE) {
798 clnt_destroy(client_handle);
804 clnt_destroy(client_handle);
809 rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
811 struct _dom_binding *ypdb, *prev = NULL;
813 struct ypbind_resp ybr;
814 char path[MAXPATHLEN];
817 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
818 ntohs(raddrp->sin_port), dom);*/
823 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
824 if (strcmp(ypdb->dom_domain, dom) == 0)
830 if (ypdb->dom_broadcast_pid) {
831 kill(ypdb->dom_broadcast_pid, SIGINT);
833 FD_CLR(READFD, &fdsr);
834 FD_CLR(READFD, &svc_fdset);
835 READFD = WRITEFD = -1;
839 /* if in secure mode, check originating port number */
840 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
841 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
842 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
845 ypdb->dom_broadcast_pid = 0;
851 if (raddrp->sin_addr.s_addr == (long)0) {
852 switch (ypdb->dom_default) {
855 ypbindlist = ypdb->dom_pnext;
857 prev->dom_pnext = ypdb->dom_pnext;
858 sprintf(path, "%s/%s.%ld", BINDINGDIR,
859 ypdb->dom_domain, YPVERS);
860 close(ypdb->dom_lockfd);
866 ypdb->dom_broadcast_pid = 0;
878 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
880 syslog(LOG_WARNING, "malloc: %m");
883 bzero(ypdb, sizeof *ypdb);
884 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
885 ypdb->dom_lockfd = -1;
886 ypdb->dom_default = 0;
887 ypdb->dom_pnext = ypbindlist;
891 /* We've recovered from a crash: inform the world. */
892 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr)
893 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
894 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
896 bcopy(raddrp, &ypdb->dom_server_addr,
897 sizeof ypdb->dom_server_addr);
899 ypdb->dom_vers = YPVERS;
901 ypdb->dom_broadcast_pid = 0;
903 if (ypdb->dom_lockfd != -1)
904 close(ypdb->dom_lockfd);
906 sprintf(path, "%s/%s.%ld", BINDINGDIR,
907 ypdb->dom_domain, ypdb->dom_vers);
909 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
910 (void)mkdir(BINDINGDIR, 0755);
911 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
915 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
916 (void)mkdir(BINDINGDIR, 0755);
917 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
924 * ok, if BINDINGDIR exists, and we can create the binding file,
927 ypdb->dom_lockfd = fd;
929 iov[0].iov_base = (char *)&(udptransp->xp_port);
930 iov[0].iov_len = sizeof udptransp->xp_port;
931 iov[1].iov_base = (char *)&ybr;
932 iov[1].iov_len = sizeof ybr;
934 bzero(&ybr, sizeof ybr);
935 ybr.ypbind_status = YPBIND_SUCC_VAL;
936 *(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
937 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
939 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
940 syslog(LOG_WARNING, "write: %m");
941 close(ypdb->dom_lockfd);
942 ypdb->dom_lockfd = -1;
948 * Check address against list of allowed servers. Return 0 if okay,
952 verify(struct in_addr addr)
956 for (i = 0; i < RESTRICTED_SERVERS; i++)
957 if (!bcmp(&addr, &restricted_addrs[i], sizeof(struct in_addr)))
964 * Try to set restricted mode. We default to normal mode if we can't
965 * resolve the specified hostnames.
968 yp_restricted_mode(char *args)
974 /* Find the restricted domain. */
975 if ((s = strsep(&args, ",")) == NULL)
979 /* Get the addresses of the servers. */
980 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
981 if ((h = gethostbyname(s)) == NULL)
983 bcopy (h->h_addr_list[0], &restricted_addrs[i],
984 sizeof(struct in_addr));
988 /* ypset and ypsetme not allowed with restricted mode */
989 ypsetmode = YPSET_NO;