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
29 * $FreeBSD: src/usr.sbin/ypbind/ypbind.c,v 1.30.2.2 2002/02/15 00:46:59 des Exp $
30 * $DragonFly: src/usr.sbin/ypbind/ypbind.c,v 1.3 2004/01/08 18:39:19 asmodai Exp $
33 #include <sys/param.h>
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/signal.h>
38 #include <sys/socket.h>
40 #include <sys/fcntl.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_prot.h>
61 #include <rpc/pmap_rmt.h>
62 #include <rpc/rpc_com.h>
63 #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);
110 struct _dom_binding *ypbindlist;
111 static struct _dom_binding *broad_domain;
114 #define YPSET_LOCAL 1
116 int ypsetmode = YPSET_NO;
117 int ypsecuremode = 0;
121 * Special restricted mode variables: when in restricted mode, only the
122 * specified restricted_domain will be bound, and only the servers listed
123 * in restricted_addrs will be used for binding.
125 #define RESTRICTED_SERVERS 10
126 int yp_restricted = 0;
128 struct in_addr restricted_addrs[RESTRICTED_SERVERS];
130 /* No more than MAX_CHILDREN child broadcasters at a time. */
132 #define MAX_CHILDREN 5
134 /* No more than MAX_DOMAINS simultaneous domains */
136 #define MAX_DOMAINS 200
138 /* RPC timeout value */
139 #ifndef FAIL_THRESHOLD
140 #define FAIL_THRESHOLD 20
143 /* Number of times to fish for a response froma particular set of hosts */
145 #define MAX_RETRIES 30
154 SVCXPRT *udptransp, *tcptransp;
157 ypbindproc_null_2_yp(transp, argp, clnt)
164 bzero((char *)&res, sizeof(res));
169 ypbindproc_domain_2_yp(transp, argp, clnt)
174 static struct ypbind_resp res;
175 struct _dom_binding *ypdb;
176 char path[MAXPATHLEN];
178 bzero((char *)&res, sizeof res);
179 res.ypbind_status = YPBIND_FAIL_VAL;
180 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
182 if (strchr(*argp, '/')) {
183 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
188 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
189 if (strcmp(ypdb->dom_domain, *argp) == 0)
195 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
199 if (domains >= MAX_DOMAINS) {
200 syslog(LOG_WARNING, "domain limit (%d) exceeded",
202 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
205 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
207 syslog(LOG_WARNING, "malloc: %m");
208 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
211 bzero((char *)ypdb, sizeof *ypdb);
212 strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
213 ypdb->dom_vers = YPVERS;
215 ypdb->dom_default = 0;
216 ypdb->dom_lockfd = -1;
217 sprintf(path, "%s/%s.%ld", BINDINGDIR,
218 ypdb->dom_domain, ypdb->dom_vers);
220 ypdb->dom_pnext = ypbindlist;
229 res.ypbind_status = YPBIND_SUCC_VAL;
230 res.ypbind_resp_u.ypbind_error = 0; /* Success */
231 *(u_int32_t *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr =
232 ypdb->dom_server_addr.sin_addr.s_addr;
233 *(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port =
234 ypdb->dom_server_addr.sin_port;
235 /*printf("domain %s at %s/%d\n", ypdb->dom_domain,
236 inet_ntoa(ypdb->dom_server_addr.sin_addr),
237 ntohs(ypdb->dom_server_addr.sin_port));*/
242 ypbindproc_setdom_2_yp(transp, argp, clnt)
247 struct sockaddr_in *fromsin, bindsin;
248 static char *result = NULL;
250 if (strchr(argp->ypsetdom_domain, '/')) {
251 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
252 rejecting.", argp->ypsetdom_domain);
255 fromsin = svc_getcaller(transp);
259 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
260 svcerr_noprog(transp);
268 svcerr_noprog(transp);
272 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
273 svcerr_noprog(transp);
277 if (argp->ypsetdom_vers != YPVERS) {
278 svcerr_noprog(transp);
282 bzero((char *)&bindsin, sizeof bindsin);
283 bindsin.sin_family = AF_INET;
284 bindsin.sin_addr.s_addr = *(u_int32_t *)argp->ypsetdom_binding.ypbind_binding_addr;
285 bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
286 rpc_received(argp->ypsetdom_domain, &bindsin, 1);
288 return((void *) &result);
292 ypbindprog_2(rqstp, transp)
293 struct svc_req *rqstp;
294 register SVCXPRT *transp;
297 domainname ypbindproc_domain_2_arg;
298 struct ypbind_setdom ypbindproc_setdom_2_arg;
300 struct authunix_parms *creds;
302 bool_t (*xdr_argument)(), (*xdr_result)();
305 switch (rqstp->rq_proc) {
306 case YPBINDPROC_NULL:
307 xdr_argument = xdr_void;
308 xdr_result = xdr_void;
309 local = (char *(*)()) ypbindproc_null_2_yp;
312 case YPBINDPROC_DOMAIN:
313 xdr_argument = xdr_domainname;
314 xdr_result = xdr_ypbind_resp;
315 local = (char *(*)()) ypbindproc_domain_2_yp;
318 case YPBINDPROC_SETDOM:
319 switch (rqstp->rq_cred.oa_flavor) {
321 creds = (struct authunix_parms *)rqstp->rq_clntcred;
322 if (creds->aup_uid != 0) {
323 svcerr_auth(transp, AUTH_BADCRED);
328 svcerr_auth(transp, AUTH_TOOWEAK);
332 xdr_argument = xdr_ypbind_setdom;
333 xdr_result = xdr_void;
334 local = (char *(*)()) ypbindproc_setdom_2_yp;
338 svcerr_noproc(transp);
341 bzero((char *)&argument, sizeof(argument));
342 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
343 svcerr_decode(transp);
346 result = (*local)(transp, &argument, rqstp);
347 if (result != NULL && !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);
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 overriden 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+1]);
418 else if (strcmp("-m", argv[i]) == 0)
422 /* blow away everything in BINDINGDIR (if it exists) */
424 if ((dird = opendir(BINDINGDIR)) != NULL) {
425 char path[MAXPATHLEN];
426 while ((dirp = readdir(dird)) != NULL)
427 if (strcmp(dirp->d_name, ".") &&
428 strcmp(dirp->d_name, "..")) {
429 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
440 pmap_unset(YPBINDPROG, YPBINDVERS);
442 udptransp = svcudp_create(RPC_ANYSOCK);
443 if (udptransp == NULL)
444 errx(1, "cannot create udp service");
445 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
447 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
449 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
450 if (tcptransp == NULL)
451 errx(1, "cannot create tcp service");
453 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
455 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
457 /* build initial domain binding, make it "unsuccessful" */
458 ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
459 if (ypbindlist == NULL)
461 bzero((char *)ypbindlist, sizeof *ypbindlist);
462 strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
463 ypbindlist->dom_vers = YPVERS;
464 ypbindlist->dom_alive = 0;
465 ypbindlist->dom_lockfd = -1;
466 ypbindlist->dom_default = 1;
469 signal(SIGCHLD, reaper);
470 signal(SIGTERM, terminate);
472 ppid = getpid(); /* Remember who we are. */
474 openlog(argv[0], LOG_PID, LOG_DAEMON);
476 /* Kick off the default domain */
477 broadcast(ypbindlist);
485 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
491 syslog(LOG_WARNING, "select: %m");
494 for (ypdb = ypbindlist; ypdb; ypdb = next) {
495 next = ypdb->dom_pnext;
496 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
497 handle_children(ypdb);
498 if (children == (MAX_CHILDREN - 1))
502 svc_getreqset(&fdsr);
514 struct _dom_binding *ypdb;
516 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
520 /* The clnt_broadcast() callback mechanism sucks. */
523 * Receive results from broadcaster. Don't worry about passing
524 * bogus info to rpc_received() -- it can handle it. Note that we
525 * must be sure to invalidate the dom_pipe_fds descriptors here:
526 * since descriptors can be re-used, we have to make sure we
527 * don't mistake one of the RPC descriptors for one of the pipes.
528 * What's weird is that forgetting to invalidate the pipe descriptors
529 * doesn't always result in an error (otherwise I would have caught
530 * the mistake much sooner), even though logically it should.
532 void handle_children(ypdb)
533 struct _dom_binding *ypdb;
535 char buf[YPMAXDOMAIN + 1];
536 struct sockaddr_in addr;
538 struct _dom_binding *y, *prev = NULL;
539 char path[MAXPATHLEN];
541 if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
542 syslog(LOG_WARNING, "could not read from child: %m");
544 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
545 syslog(LOG_WARNING, "could not read from child: %m");
548 FD_CLR(READFD, &fdsr);
549 FD_CLR(READFD, &svc_fdset);
550 READFD = WRITEFD = -1;
552 rpc_received((char *)&buf, &addr, 0);
554 for (y = ypbindlist; y; y = y->dom_pnext) {
559 switch (ypdb->dom_default) {
562 ypbindlist = y->dom_pnext;
564 prev->dom_pnext = y->dom_pnext;
565 sprintf(path, "%s/%s.%ld", BINDINGDIR,
566 ypdb->dom_domain, YPVERS);
567 close(ypdb->dom_lockfd);
573 ypdb->dom_broadcast_pid = 0;
586 * Send our dying words back to our parent before we perish.
589 tell_parent(dom, addr)
591 struct sockaddr_in *addr;
593 char buf[YPMAXDOMAIN + 1];
594 struct timeval timeout;
600 sprintf(buf, "%s", broad_domain->dom_domain);
601 if (write(BROADFD, &buf, sizeof(buf)) < 0)
605 * Stay in sync with parent: wait for it to read our first
606 * message before sending the second.
610 FD_SET(BROADFD, &fds);
611 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
613 if (FD_ISSET(BROADFD, &fds)) {
614 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
624 bool_t broadcast_result(out, addr)
626 struct sockaddr_in *addr;
628 if (retries >= MAX_RETRIES) {
629 bzero((char *)addr, sizeof(struct sockaddr_in));
630 if (tell_parent(broad_domain->dom_domain, addr))
631 syslog(LOG_WARNING, "lost connection to parent");
635 if (yp_restricted && verify(addr->sin_addr)) {
637 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
640 if (tell_parent(broad_domain->dom_domain, addr))
641 syslog(LOG_WARNING, "lost connection to parent");
647 * The right way to send RPC broadcasts.
648 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
649 * blocks while waiting for replies, so we have to fork off separate
650 * broadcaster processes that do the waiting and then transmit their
651 * results back to the parent for processing. We also have to remember
652 * to save the name of the domain we're trying to bind in a global
653 * variable since clnt_broadcast() provides no way to pass things to
654 * the 'eachresult' callback function.
658 struct _dom_binding *ypdb;
663 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
666 if (pipe(ypdb->dom_pipe_fds) < 0) {
667 syslog(LOG_WARNING, "pipe: %m");
671 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
672 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
673 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
676 flock(ypdb->dom_lockfd, LOCK_UN);
678 switch ((ypdb->dom_broadcast_pid = fork())) {
681 signal(SIGCHLD, SIG_DFL);
682 signal(SIGTERM, SIG_DFL);
685 syslog(LOG_WARNING, "fork: %m");
691 FD_SET(READFD, &svc_fdset);
696 /* Release all locks before doing anything else. */
698 close(ypbindlist->dom_lockfd);
699 ypbindlist = ypbindlist->dom_pnext;
704 * Special 'many-cast' behavior. If we're in restricted mode,
705 * we have a list of possible server addresses to try. What
706 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
707 * procedure and time the replies. Whoever replies fastest
708 * gets to be our server. Note that this is not a broadcast
709 * operation: we transmit uni-cast datagrams only.
711 if (yp_restricted && yp_manycast) {
714 struct sockaddr_in sin;
716 i = __yp_ping(restricted_addrs, yp_restricted,
717 ypdb->dom_domain, &port);
719 bzero((char *)&ypdb->dom_server_addr,
720 sizeof(struct sockaddr_in));
721 if (tell_parent(ypdb->dom_domain,
722 &ypdb->dom_server_addr))
723 syslog(LOG_WARNING, "lost connection to parent");
725 bzero((char *)&sin, sizeof(struct sockaddr_in));
726 bcopy((char *)&restricted_addrs[i],
727 (char *)&sin.sin_addr, sizeof(struct in_addr));
728 sin.sin_family = AF_INET;
730 if (tell_parent(broad_domain->dom_domain, &sin))
732 "lost connection to parent");
742 ptr = (char *)&ypdb->dom_domain;
743 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
744 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
748 if (stat != RPC_SUCCESS) {
749 bzero((char *)&ypdb->dom_server_addr,
750 sizeof(struct sockaddr_in));
751 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
752 syslog(LOG_WARNING, "lost connection to parent");
759 * The right way to check if a server is alive.
760 * Attempt to get a client handle pointing to the server and send a
761 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
762 * we invalidate this binding entry and send out a broadcast to try to
763 * establish a new binding. Note that we treat non-default domains
764 * specially: once bound, we keep tabs on our server, but if it
765 * goes away and fails to respond after one round of broadcasting, we
766 * abandon it until a client specifically references it again. We make
767 * every effort to keep our default domain bound, however, since we
768 * need it to keep the system on its feet.
772 struct _dom_binding *ypdb;
775 struct timeval interval, timeout;
777 int rpcsock = RPC_ANYSOCK;
778 CLIENT *client_handle;
780 interval.tv_sec = FAIL_THRESHOLD;
781 interval.tv_usec = 0;
782 timeout.tv_sec = FAIL_THRESHOLD;
785 if (ypdb->dom_broadcast_pid)
788 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
789 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
790 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
791 /* Can't get a handle: we're dead. */
801 ptr = (char *)&ypdb->dom_domain;
803 if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
804 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
805 timeout)) != RPC_SUCCESS || out == FALSE) {
808 clnt_destroy(client_handle);
814 clnt_destroy(client_handle);
818 void rpc_received(dom, raddrp, force)
820 struct sockaddr_in *raddrp;
823 struct _dom_binding *ypdb, *prev = NULL;
825 struct ypbind_resp ybr;
826 char path[MAXPATHLEN];
829 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
830 ntohs(raddrp->sin_port), dom);*/
835 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
836 if (strcmp(ypdb->dom_domain, dom) == 0)
842 if (ypdb->dom_broadcast_pid) {
843 kill(ypdb->dom_broadcast_pid, SIGINT);
845 FD_CLR(READFD, &fdsr);
846 FD_CLR(READFD, &svc_fdset);
847 READFD = WRITEFD = -1;
851 /* if in secure mode, check originating port number */
852 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
853 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
854 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
857 ypdb->dom_broadcast_pid = 0;
863 if (raddrp->sin_addr.s_addr == (long)0) {
864 switch (ypdb->dom_default) {
867 ypbindlist = ypdb->dom_pnext;
869 prev->dom_pnext = ypdb->dom_pnext;
870 sprintf(path, "%s/%s.%ld", BINDINGDIR,
871 ypdb->dom_domain, YPVERS);
872 close(ypdb->dom_lockfd);
878 ypdb->dom_broadcast_pid = 0;
890 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
892 syslog(LOG_WARNING, "malloc: %m");
895 bzero((char *)ypdb, sizeof *ypdb);
896 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
897 ypdb->dom_lockfd = -1;
898 ypdb->dom_default = 0;
899 ypdb->dom_pnext = ypbindlist;
903 /* We've recovered from a crash: inform the world. */
904 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr)
905 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
906 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
908 bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
909 sizeof ypdb->dom_server_addr);
911 ypdb->dom_vers = YPVERS;
913 ypdb->dom_broadcast_pid = 0;
915 if (ypdb->dom_lockfd != -1)
916 close(ypdb->dom_lockfd);
918 sprintf(path, "%s/%s.%ld", BINDINGDIR,
919 ypdb->dom_domain, ypdb->dom_vers);
921 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
922 (void)mkdir(BINDINGDIR, 0755);
923 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
927 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
928 (void)mkdir(BINDINGDIR, 0755);
929 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
936 * ok, if BINDINGDIR exists, and we can create the binding file,
939 ypdb->dom_lockfd = fd;
941 iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
942 iov[0].iov_len = sizeof udptransp->xp_port;
943 iov[1].iov_base = (caddr_t)&ybr;
944 iov[1].iov_len = sizeof ybr;
946 bzero(&ybr, sizeof ybr);
947 ybr.ypbind_status = YPBIND_SUCC_VAL;
948 *(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
949 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
951 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
952 syslog(LOG_WARNING, "write: %m");
953 close(ypdb->dom_lockfd);
954 ypdb->dom_lockfd = -1;
960 * Check address against list of allowed servers. Return 0 if okay,
969 for (i = 0; i < RESTRICTED_SERVERS; i++)
970 if (!bcmp((char *)&addr, (char *)&restricted_addrs[i],
971 sizeof(struct in_addr)))
978 * Try to set restricted mode. We default to normal mode if we can't
979 * resolve the specified hostnames.
982 yp_restricted_mode(args)
989 /* Find the restricted domain. */
990 if ((s = strsep(&args, ",")) == NULL)
994 /* Get the addresses of the servers. */
995 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
996 if ((h = gethostbyname(s)) == NULL)
998 bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i],
999 sizeof(struct in_addr));
1003 /* ypset and ypsetme not allowed with restricted mode */
1004 ypsetmode = YPSET_NO;