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.40 2004/10/17 19:33:33 stefanf Exp $
32 #include <sys/param.h>
33 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/signal.h>
37 #include <sys/socket.h>
39 #include <sys/fcntl.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <rpc/pmap_clnt.h>
59 #include <rpc/pmap_prot.h>
60 #include <rpc/pmap_rmt.h>
61 #include <rpc/rpc_com.h>
62 #include <rpcsvc/yp.h>
63 #include <rpcsvc/ypclnt.h>
67 #define BINDINGDIR "/var/yp/binding"
71 #define YPBINDLOCK "/var/run/ypbind.lock"
75 struct _dom_binding *dom_pnext;
76 char dom_domain[YPMAXDOMAIN + 1];
77 struct sockaddr_in dom_server_addr;
81 int dom_broadcast_pid;
86 #define READFD ypdb->dom_pipe_fds[0]
87 #define WRITEFD ypdb->dom_pipe_fds[1]
88 #define BROADFD broad_domain->dom_pipe_fds[1]
90 extern bool_t xdr_domainname(), xdr_ypbind_resp();
91 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
92 extern bool_t xdr_ypbind_setdom();
95 void *ypbindproc_null_2_yp(SVCXPRT *, void *, CLIENT *);
96 void *ypbindproc_setdom_2_yp(SVCXPRT *, struct ypbind_setdom *, CLIENT *);
97 void rpc_received(char *, struct sockaddr_in *, int);
98 void broadcast(struct _dom_binding *);
99 int ping(struct _dom_binding *);
100 int tell_parent(char *, struct sockaddr_in *);
101 void handle_children(struct _dom_binding *);
103 void terminate(int) __dead2;
104 void yp_restricted_mode(char *);
105 int verify(struct in_addr);
108 struct _dom_binding *ypbindlist;
109 static struct _dom_binding *broad_domain;
112 #define YPSET_LOCAL 1
114 int ypsetmode = YPSET_NO;
115 int ypsecuremode = 0;
119 * Special restricted mode variables: when in restricted mode, only the
120 * specified restricted_domain will be bound, and only the servers listed
121 * in restricted_addrs will be used for binding.
123 #define RESTRICTED_SERVERS 10
124 int yp_restricted = 0;
126 struct in_addr restricted_addrs[RESTRICTED_SERVERS];
128 /* No more than MAX_CHILDREN child broadcasters at a time. */
130 #define MAX_CHILDREN 5
132 /* No more than MAX_DOMAINS simultaneous domains */
134 #define MAX_DOMAINS 200
136 /* RPC timeout value */
137 #ifndef FAIL_THRESHOLD
138 #define FAIL_THRESHOLD 20
141 /* Number of times to fish for a response froma particular set of hosts */
143 #define MAX_RETRIES 30
152 SVCXPRT *udptransp, *tcptransp;
155 ypbindproc_null_2_yp(SVCXPRT *transp, void *argp, CLIENT *clnt)
159 bzero(&res, sizeof(res));
164 ypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt)
166 static struct ypbind_resp res;
167 struct _dom_binding *ypdb;
168 char path[MAXPATHLEN];
170 bzero(&res, sizeof res);
171 res.ypbind_status = YPBIND_FAIL_VAL;
172 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
174 if (strchr(*argp, '/')) {
175 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
180 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
181 if (strcmp(ypdb->dom_domain, *argp) == 0)
187 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
191 if (domains >= MAX_DOMAINS) {
192 syslog(LOG_WARNING, "domain limit (%d) exceeded",
194 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
197 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
199 syslog(LOG_WARNING, "malloc: %m");
200 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
203 bzero(ypdb, sizeof *ypdb);
204 strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
205 ypdb->dom_vers = YPVERS;
207 ypdb->dom_default = 0;
208 ypdb->dom_lockfd = -1;
209 sprintf(path, "%s/%s.%ld", BINDINGDIR,
210 ypdb->dom_domain, ypdb->dom_vers);
212 ypdb->dom_pnext = ypbindlist;
221 res.ypbind_status = YPBIND_SUCC_VAL;
222 res.ypbind_resp_u.ypbind_error = 0; /* Success */
223 *(u_int32_t *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr =
224 ypdb->dom_server_addr.sin_addr.s_addr;
225 *(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port =
226 ypdb->dom_server_addr.sin_port;
227 /*printf("domain %s at %s/%d\n", ypdb->dom_domain,
228 inet_ntoa(ypdb->dom_server_addr.sin_addr),
229 ntohs(ypdb->dom_server_addr.sin_port));*/
234 ypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt)
236 struct sockaddr_in *fromsin, bindsin;
237 static char *result = NULL;
239 if (strchr(argp->ypsetdom_domain, '/')) {
240 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
241 rejecting.", argp->ypsetdom_domain);
244 fromsin = svc_getcaller(transp);
248 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
249 svcerr_noprog(transp);
257 svcerr_noprog(transp);
261 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
262 svcerr_noprog(transp);
266 if (argp->ypsetdom_vers != YPVERS) {
267 svcerr_noprog(transp);
271 bzero(&bindsin, sizeof bindsin);
272 bindsin.sin_family = AF_INET;
273 bindsin.sin_addr.s_addr = *(u_int32_t *)argp->ypsetdom_binding.ypbind_binding_addr;
274 bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
275 rpc_received(argp->ypsetdom_domain, &bindsin, 1);
277 return((void *) &result);
281 ypbindprog_2(struct svc_req *rqstp, SVCXPRT *transp)
284 domainname ypbindproc_domain_2_arg;
285 struct ypbind_setdom ypbindproc_setdom_2_arg;
287 struct authunix_parms *creds;
289 bool_t (*xdr_argument)(), (*xdr_result)();
292 switch (rqstp->rq_proc) {
293 case YPBINDPROC_NULL:
294 xdr_argument = xdr_void;
295 xdr_result = xdr_void;
296 local = (char *(*)()) ypbindproc_null_2_yp;
299 case YPBINDPROC_DOMAIN:
300 xdr_argument = xdr_domainname;
301 xdr_result = xdr_ypbind_resp;
302 local = (char *(*)()) ypbindproc_domain_2_yp;
305 case YPBINDPROC_SETDOM:
306 switch (rqstp->rq_cred.oa_flavor) {
308 creds = (struct authunix_parms *)rqstp->rq_clntcred;
309 if (creds->aup_uid != 0) {
310 svcerr_auth(transp, AUTH_BADCRED);
315 svcerr_auth(transp, AUTH_TOOWEAK);
319 xdr_argument = xdr_ypbind_setdom;
320 xdr_result = xdr_void;
321 local = (char *(*)()) ypbindproc_setdom_2_yp;
325 svcerr_noproc(transp);
328 bzero(&argument, sizeof(argument));
329 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
330 svcerr_decode(transp);
333 result = (*local)(transp, &argument, rqstp);
334 if (result != NULL &&
335 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
336 svcerr_systemerr(transp);
341 /* Jack the reaper */
347 while (wait3(&st, WNOHANG, NULL) > 0)
354 struct _dom_binding *ypdb;
355 char path[MAXPATHLEN];
357 if (ppid != getpid())
360 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
361 close(ypdb->dom_lockfd);
362 if (ypdb->dom_broadcast_pid)
363 kill(ypdb->dom_broadcast_pid, SIGINT);
364 sprintf(path, "%s/%s.%ld", BINDINGDIR,
365 ypdb->dom_domain, ypdb->dom_vers);
370 pmap_unset(YPBINDPROG, YPBINDVERS);
375 main(int argc, char **argv)
381 struct _dom_binding *ypdb, *next;
383 /* Check that another ypbind isn't already running. */
384 if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
385 err(1, "%s", YPBINDLOCK);
387 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
388 errx(1, "another ypbind is already running. Aborting");
390 /* XXX domainname will be overridden if we use restricted mode */
391 yp_get_default_domain(&domain_name);
392 if (domain_name[0] == '\0')
393 errx(1, "domainname not set. Aborting");
395 for (i = 1; i<argc; i++) {
396 if (strcmp("-ypset", argv[i]) == 0)
397 ypsetmode = YPSET_ALL;
398 else if (strcmp("-ypsetme", argv[i]) == 0)
399 ypsetmode = YPSET_LOCAL;
400 else if (strcmp("-s", argv[i]) == 0)
402 else if (strcmp("-S", argv[i]) == 0 && argc > i)
403 yp_restricted_mode(argv[++i]);
404 else if (strcmp("-m", argv[i]) == 0)
407 errx(1, "unknown option: %s", argv[i]);
410 /* blow away everything in BINDINGDIR (if it exists) */
412 if ((dird = opendir(BINDINGDIR)) != NULL) {
413 char path[MAXPATHLEN];
414 while ((dirp = readdir(dird)) != NULL)
415 if (strcmp(dirp->d_name, ".") &&
416 strcmp(dirp->d_name, "..")) {
417 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
428 pmap_unset(YPBINDPROG, YPBINDVERS);
430 udptransp = svcudp_create(RPC_ANYSOCK);
431 if (udptransp == NULL)
432 errx(1, "cannot create udp service");
433 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
435 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
437 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
438 if (tcptransp == NULL)
439 errx(1, "cannot create tcp service");
441 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
443 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
445 /* build initial domain binding, make it "unsuccessful" */
446 ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
447 if (ypbindlist == NULL)
449 bzero(ypbindlist, sizeof *ypbindlist);
450 strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
451 ypbindlist->dom_vers = YPVERS;
452 ypbindlist->dom_alive = 0;
453 ypbindlist->dom_lockfd = -1;
454 ypbindlist->dom_default = 1;
457 signal(SIGCHLD, reaper);
458 signal(SIGTERM, terminate);
460 ppid = getpid(); /* Remember who we are. */
462 openlog(argv[0], LOG_PID, LOG_DAEMON);
464 /* Kick off the default domain */
465 broadcast(ypbindlist);
473 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
479 syslog(LOG_WARNING, "select: %m");
482 for (ypdb = ypbindlist; ypdb; ypdb = next) {
483 next = ypdb->dom_pnext;
484 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
485 handle_children(ypdb);
486 if (children == (MAX_CHILDREN - 1))
490 svc_getreqset(&fdsr);
502 struct _dom_binding *ypdb;
504 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
508 /* The clnt_broadcast() callback mechanism sucks. */
511 * Receive results from broadcaster. Don't worry about passing
512 * bogus info to rpc_received() -- it can handle it. Note that we
513 * must be sure to invalidate the dom_pipe_fds descriptors here:
514 * since descriptors can be re-used, we have to make sure we
515 * don't mistake one of the RPC descriptors for one of the pipes.
516 * What's weird is that forgetting to invalidate the pipe descriptors
517 * doesn't always result in an error (otherwise I would have caught
518 * the mistake much sooner), even though logically it should.
521 handle_children(struct _dom_binding *ypdb)
523 char buf[YPMAXDOMAIN + 1];
524 struct sockaddr_in addr;
526 struct _dom_binding *y, *prev = NULL;
527 char path[MAXPATHLEN];
529 if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
530 syslog(LOG_WARNING, "could not read from child: %m");
532 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
533 syslog(LOG_WARNING, "could not read from child: %m");
536 FD_CLR(READFD, &fdsr);
537 FD_CLR(READFD, &svc_fdset);
538 READFD = WRITEFD = -1;
540 rpc_received(buf, &addr, 0);
542 for (y = ypbindlist; y; y = y->dom_pnext) {
547 switch (ypdb->dom_default) {
550 ypbindlist = y->dom_pnext;
552 prev->dom_pnext = y->dom_pnext;
553 sprintf(path, "%s/%s.%ld", BINDINGDIR,
554 ypdb->dom_domain, YPVERS);
555 close(ypdb->dom_lockfd);
561 ypdb->dom_broadcast_pid = 0;
574 * Send our dying words back to our parent before we perish.
577 tell_parent(char *dom, struct sockaddr_in *addr)
579 char buf[YPMAXDOMAIN + 1];
580 struct timeval timeout;
586 sprintf(buf, "%s", broad_domain->dom_domain);
587 if (write(BROADFD, &buf, sizeof(buf)) < 0)
591 * Stay in sync with parent: wait for it to read our first
592 * message before sending the second.
596 FD_SET(BROADFD, &fds);
597 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
599 if (FD_ISSET(BROADFD, &fds)) {
600 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
611 broadcast_result(bool_t *out, struct sockaddr_in *addr)
613 if (retries >= MAX_RETRIES) {
614 bzero(addr, sizeof(struct sockaddr_in));
615 if (tell_parent(broad_domain->dom_domain, addr))
616 syslog(LOG_WARNING, "lost connection to parent");
620 if (yp_restricted && verify(addr->sin_addr)) {
622 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
625 if (tell_parent(broad_domain->dom_domain, addr))
626 syslog(LOG_WARNING, "lost connection to parent");
632 * The right way to send RPC broadcasts.
633 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
634 * blocks while waiting for replies, so we have to fork off separate
635 * broadcaster processes that do the waiting and then transmit their
636 * results back to the parent for processing. We also have to remember
637 * to save the name of the domain we're trying to bind in a global
638 * variable since clnt_broadcast() provides no way to pass things to
639 * the 'eachresult' callback function.
642 broadcast(struct _dom_binding *ypdb)
647 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
650 if (pipe(ypdb->dom_pipe_fds) < 0) {
651 syslog(LOG_WARNING, "pipe: %m");
655 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
656 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
657 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
660 flock(ypdb->dom_lockfd, LOCK_UN);
662 switch ((ypdb->dom_broadcast_pid = fork())) {
665 signal(SIGCHLD, SIG_DFL);
666 signal(SIGTERM, SIG_DFL);
669 syslog(LOG_WARNING, "fork: %m");
675 FD_SET(READFD, &svc_fdset);
680 /* Release all locks before doing anything else. */
682 close(ypbindlist->dom_lockfd);
683 ypbindlist = ypbindlist->dom_pnext;
688 * Special 'many-cast' behavior. If we're in restricted mode,
689 * we have a list of possible server addresses to try. What
690 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
691 * procedure and time the replies. Whoever replies fastest
692 * gets to be our server. Note that this is not a broadcast
693 * operation: we transmit uni-cast datagrams only.
695 if (yp_restricted && yp_manycast) {
698 struct sockaddr_in sin;
700 i = __yp_ping(restricted_addrs, yp_restricted,
701 ypdb->dom_domain, &port);
703 bzero(&ypdb->dom_server_addr,
704 sizeof(struct sockaddr_in));
705 if (tell_parent(ypdb->dom_domain,
706 &ypdb->dom_server_addr)) {
708 "lost connection to parent");
711 bzero(&sin, sizeof(struct sockaddr_in));
712 bcopy(&restricted_addrs[i],
713 &sin.sin_addr, sizeof(struct in_addr));
714 sin.sin_family = AF_INET;
716 if (tell_parent(broad_domain->dom_domain, &sin))
718 "lost connection to parent");
728 ptr = ypdb->dom_domain;
729 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
730 (xdrproc_t)xdr_domainname, &ptr,
731 (xdrproc_t)xdr_bool, &out,
732 (resultproc_t)broadcast_result);
735 if (stat != RPC_SUCCESS) {
736 bzero(&ypdb->dom_server_addr, sizeof(struct sockaddr_in));
737 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
738 syslog(LOG_WARNING, "lost connection to parent");
745 * The right way to check if a server is alive.
746 * Attempt to get a client handle pointing to the server and send a
747 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
748 * we invalidate this binding entry and send out a broadcast to try to
749 * establish a new binding. Note that we treat non-default domains
750 * specially: once bound, we keep tabs on our server, but if it
751 * goes away and fails to respond after one round of broadcasting, we
752 * abandon it until a client specifically references it again. We make
753 * every effort to keep our default domain bound, however, since we
754 * need it to keep the system on its feet.
757 ping(struct _dom_binding *ypdb)
760 struct timeval interval, timeout;
762 int rpcsock = RPC_ANYSOCK;
763 CLIENT *client_handle;
765 interval.tv_sec = FAIL_THRESHOLD;
766 interval.tv_usec = 0;
767 timeout.tv_sec = FAIL_THRESHOLD;
770 if (ypdb->dom_broadcast_pid)
773 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
774 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
775 RPCSMALLMSGSIZE)) == NULL) {
776 /* Can't get a handle: we're dead. */
786 ptr = ypdb->dom_domain;
788 stat = clnt_call(client_handle, YPPROC_DOMAIN,
789 (xdrproc_t)xdr_domainname, &ptr,
790 (xdrproc_t)xdr_bool, &out, timeout);
791 if (stat != RPC_SUCCESS || out == FALSE) {
794 clnt_destroy(client_handle);
800 clnt_destroy(client_handle);
805 rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
807 struct _dom_binding *ypdb, *prev = NULL;
809 struct ypbind_resp ybr;
810 char path[MAXPATHLEN];
813 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
814 ntohs(raddrp->sin_port), dom);*/
819 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
820 if (strcmp(ypdb->dom_domain, dom) == 0)
826 if (ypdb->dom_broadcast_pid) {
827 kill(ypdb->dom_broadcast_pid, SIGINT);
829 FD_CLR(READFD, &fdsr);
830 FD_CLR(READFD, &svc_fdset);
831 READFD = WRITEFD = -1;
835 /* if in secure mode, check originating port number */
836 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
837 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
838 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
841 ypdb->dom_broadcast_pid = 0;
847 if (raddrp->sin_addr.s_addr == (long)0) {
848 switch (ypdb->dom_default) {
851 ypbindlist = ypdb->dom_pnext;
853 prev->dom_pnext = ypdb->dom_pnext;
854 sprintf(path, "%s/%s.%ld", BINDINGDIR,
855 ypdb->dom_domain, YPVERS);
856 close(ypdb->dom_lockfd);
862 ypdb->dom_broadcast_pid = 0;
874 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
876 syslog(LOG_WARNING, "malloc: %m");
879 bzero(ypdb, sizeof *ypdb);
880 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
881 ypdb->dom_lockfd = -1;
882 ypdb->dom_default = 0;
883 ypdb->dom_pnext = ypbindlist;
887 /* We've recovered from a crash: inform the world. */
888 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr)
889 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
890 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
892 bcopy(raddrp, &ypdb->dom_server_addr, sizeof ypdb->dom_server_addr);
894 ypdb->dom_vers = YPVERS;
896 ypdb->dom_broadcast_pid = 0;
898 if (ypdb->dom_lockfd != -1)
899 close(ypdb->dom_lockfd);
901 sprintf(path, "%s/%s.%ld", BINDINGDIR,
902 ypdb->dom_domain, ypdb->dom_vers);
904 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
905 mkdir(BINDINGDIR, 0755);
906 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
910 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
911 mkdir(BINDINGDIR, 0755);
912 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
919 * ok, if BINDINGDIR exists, and we can create the binding file,
922 ypdb->dom_lockfd = fd;
924 iov[0].iov_base = (char *)&(udptransp->xp_port);
925 iov[0].iov_len = sizeof udptransp->xp_port;
926 iov[1].iov_base = (char *)&ybr;
927 iov[1].iov_len = sizeof ybr;
929 bzero(&ybr, sizeof ybr);
930 ybr.ypbind_status = YPBIND_SUCC_VAL;
931 *(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
932 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
934 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
935 syslog(LOG_WARNING, "write: %m");
936 close(ypdb->dom_lockfd);
937 ypdb->dom_lockfd = -1;
943 * Check address against list of allowed servers. Return 0 if okay,
947 verify(struct in_addr addr)
951 for (i = 0; i < RESTRICTED_SERVERS; i++)
952 if (!bcmp(&addr, &restricted_addrs[i], sizeof(struct in_addr)))
959 * Try to set restricted mode. We default to normal mode if we can't
960 * resolve the specified hostnames.
963 yp_restricted_mode(char *args)
969 /* Find the restricted domain. */
970 if ((s = strsep(&args, ",")) == NULL)
974 /* Get the addresses of the servers. */
975 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
976 if ((h = gethostbyname(s)) == NULL)
978 bcopy (h->h_addr_list[0], &restricted_addrs[i],
979 sizeof(struct in_addr));
983 /* ypset and ypsetme not allowed with restricted mode */
984 ypsetmode = YPSET_NO;