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.5 2004/12/18 22:48:14 swildner 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(SVCXPRT *transp, void *argp, CLIENT *clnt)
161 bzero((char *)&res, sizeof(res));
166 ypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt)
168 static struct ypbind_resp res;
169 struct _dom_binding *ypdb;
170 char path[MAXPATHLEN];
172 bzero((char *)&res, sizeof res);
173 res.ypbind_status = YPBIND_FAIL_VAL;
174 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
176 if (strchr(*argp, '/')) {
177 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
182 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
183 if (strcmp(ypdb->dom_domain, *argp) == 0)
189 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
193 if (domains >= MAX_DOMAINS) {
194 syslog(LOG_WARNING, "domain limit (%d) exceeded",
196 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
199 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
201 syslog(LOG_WARNING, "malloc: %m");
202 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
205 bzero((char *)ypdb, sizeof *ypdb);
206 strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
207 ypdb->dom_vers = YPVERS;
209 ypdb->dom_default = 0;
210 ypdb->dom_lockfd = -1;
211 sprintf(path, "%s/%s.%ld", BINDINGDIR,
212 ypdb->dom_domain, ypdb->dom_vers);
214 ypdb->dom_pnext = ypbindlist;
223 res.ypbind_status = YPBIND_SUCC_VAL;
224 res.ypbind_resp_u.ypbind_error = 0; /* Success */
225 *(u_int32_t *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr =
226 ypdb->dom_server_addr.sin_addr.s_addr;
227 *(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port =
228 ypdb->dom_server_addr.sin_port;
229 /*printf("domain %s at %s/%d\n", ypdb->dom_domain,
230 inet_ntoa(ypdb->dom_server_addr.sin_addr),
231 ntohs(ypdb->dom_server_addr.sin_port));*/
236 ypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt)
238 struct sockaddr_in *fromsin, bindsin;
239 static char *result = NULL;
241 if (strchr(argp->ypsetdom_domain, '/')) {
242 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
243 rejecting.", argp->ypsetdom_domain);
246 fromsin = svc_getcaller(transp);
250 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
251 svcerr_noprog(transp);
259 svcerr_noprog(transp);
263 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
264 svcerr_noprog(transp);
268 if (argp->ypsetdom_vers != YPVERS) {
269 svcerr_noprog(transp);
273 bzero((char *)&bindsin, sizeof bindsin);
274 bindsin.sin_family = AF_INET;
275 bindsin.sin_addr.s_addr = *(u_int32_t *)argp->ypsetdom_binding.ypbind_binding_addr;
276 bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
277 rpc_received(argp->ypsetdom_domain, &bindsin, 1);
279 return((void *) &result);
283 ypbindprog_2(struct svc_req *rqstp, SVCXPRT *transp)
286 domainname ypbindproc_domain_2_arg;
287 struct ypbind_setdom ypbindproc_setdom_2_arg;
289 struct authunix_parms *creds;
291 bool_t (*xdr_argument)(), (*xdr_result)();
294 switch (rqstp->rq_proc) {
295 case YPBINDPROC_NULL:
296 xdr_argument = xdr_void;
297 xdr_result = xdr_void;
298 local = (char *(*)()) ypbindproc_null_2_yp;
301 case YPBINDPROC_DOMAIN:
302 xdr_argument = xdr_domainname;
303 xdr_result = xdr_ypbind_resp;
304 local = (char *(*)()) ypbindproc_domain_2_yp;
307 case YPBINDPROC_SETDOM:
308 switch (rqstp->rq_cred.oa_flavor) {
310 creds = (struct authunix_parms *)rqstp->rq_clntcred;
311 if (creds->aup_uid != 0) {
312 svcerr_auth(transp, AUTH_BADCRED);
317 svcerr_auth(transp, AUTH_TOOWEAK);
321 xdr_argument = xdr_ypbind_setdom;
322 xdr_result = xdr_void;
323 local = (char *(*)()) ypbindproc_setdom_2_yp;
327 svcerr_noproc(transp);
330 bzero((char *)&argument, sizeof(argument));
331 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
332 svcerr_decode(transp);
335 result = (*local)(transp, &argument, rqstp);
336 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
337 svcerr_systemerr(transp);
342 /* Jack the reaper */
347 while (wait3(&st, WNOHANG, NULL) > 0)
351 void terminate(int sig)
353 struct _dom_binding *ypdb;
354 char path[MAXPATHLEN];
356 if (ppid != getpid())
359 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
360 close(ypdb->dom_lockfd);
361 if (ypdb->dom_broadcast_pid)
362 kill(ypdb->dom_broadcast_pid, SIGINT);
363 sprintf(path, "%s/%s.%ld", BINDINGDIR,
364 ypdb->dom_domain, ypdb->dom_vers);
369 pmap_unset(YPBINDPROG, YPBINDVERS);
374 main(int argc, char **argv)
380 struct _dom_binding *ypdb, *next;
382 /* Check that another ypbind isn't already running. */
383 if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
384 err(1, "%s", YPBINDLOCK);
386 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
387 errx(1, "another ypbind is already running. Aborting");
389 /* XXX domainname will be overriden if we use restricted mode */
390 yp_get_default_domain(&domain_name);
391 if (domain_name[0] == '\0')
392 errx(1, "domainname not set. Aborting");
394 for (i = 1; i<argc; i++) {
395 if (strcmp("-ypset", argv[i]) == 0)
396 ypsetmode = YPSET_ALL;
397 else if (strcmp("-ypsetme", argv[i]) == 0)
398 ypsetmode = YPSET_LOCAL;
399 else if (strcmp("-s", argv[i]) == 0)
401 else if (strcmp("-S", argv[i]) == 0 && argc > i)
402 yp_restricted_mode(argv[i+1]);
403 else if (strcmp("-m", argv[i]) == 0)
407 /* blow away everything in BINDINGDIR (if it exists) */
409 if ((dird = opendir(BINDINGDIR)) != NULL) {
410 char path[MAXPATHLEN];
411 while ((dirp = readdir(dird)) != NULL)
412 if (strcmp(dirp->d_name, ".") &&
413 strcmp(dirp->d_name, "..")) {
414 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
425 pmap_unset(YPBINDPROG, YPBINDVERS);
427 udptransp = svcudp_create(RPC_ANYSOCK);
428 if (udptransp == NULL)
429 errx(1, "cannot create udp service");
430 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
432 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
434 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
435 if (tcptransp == NULL)
436 errx(1, "cannot create tcp service");
438 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
440 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
442 /* build initial domain binding, make it "unsuccessful" */
443 ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
444 if (ypbindlist == NULL)
446 bzero((char *)ypbindlist, sizeof *ypbindlist);
447 strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
448 ypbindlist->dom_vers = YPVERS;
449 ypbindlist->dom_alive = 0;
450 ypbindlist->dom_lockfd = -1;
451 ypbindlist->dom_default = 1;
454 signal(SIGCHLD, reaper);
455 signal(SIGTERM, terminate);
457 ppid = getpid(); /* Remember who we are. */
459 openlog(argv[0], LOG_PID, LOG_DAEMON);
461 /* Kick off the default domain */
462 broadcast(ypbindlist);
470 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
476 syslog(LOG_WARNING, "select: %m");
479 for (ypdb = ypbindlist; ypdb; ypdb = next) {
480 next = ypdb->dom_pnext;
481 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
482 handle_children(ypdb);
483 if (children == (MAX_CHILDREN - 1))
487 svc_getreqset(&fdsr);
499 struct _dom_binding *ypdb;
501 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
505 /* The clnt_broadcast() callback mechanism sucks. */
508 * Receive results from broadcaster. Don't worry about passing
509 * bogus info to rpc_received() -- it can handle it. Note that we
510 * must be sure to invalidate the dom_pipe_fds descriptors here:
511 * since descriptors can be re-used, we have to make sure we
512 * don't mistake one of the RPC descriptors for one of the pipes.
513 * What's weird is that forgetting to invalidate the pipe descriptors
514 * doesn't always result in an error (otherwise I would have caught
515 * the mistake much sooner), even though logically it should.
517 void handle_children(struct _dom_binding *ypdb)
519 char buf[YPMAXDOMAIN + 1];
520 struct sockaddr_in addr;
522 struct _dom_binding *y, *prev = NULL;
523 char path[MAXPATHLEN];
525 if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
526 syslog(LOG_WARNING, "could not read from child: %m");
528 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
529 syslog(LOG_WARNING, "could not read from child: %m");
532 FD_CLR(READFD, &fdsr);
533 FD_CLR(READFD, &svc_fdset);
534 READFD = WRITEFD = -1;
536 rpc_received((char *)&buf, &addr, 0);
538 for (y = ypbindlist; y; y = y->dom_pnext) {
543 switch (ypdb->dom_default) {
546 ypbindlist = y->dom_pnext;
548 prev->dom_pnext = y->dom_pnext;
549 sprintf(path, "%s/%s.%ld", BINDINGDIR,
550 ypdb->dom_domain, YPVERS);
551 close(ypdb->dom_lockfd);
557 ypdb->dom_broadcast_pid = 0;
570 * Send our dying words back to our parent before we perish.
573 tell_parent(char *dom, struct sockaddr_in *addr)
575 char buf[YPMAXDOMAIN + 1];
576 struct timeval timeout;
582 sprintf(buf, "%s", broad_domain->dom_domain);
583 if (write(BROADFD, &buf, sizeof(buf)) < 0)
587 * Stay in sync with parent: wait for it to read our first
588 * message before sending the second.
592 FD_SET(BROADFD, &fds);
593 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
595 if (FD_ISSET(BROADFD, &fds)) {
596 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
606 bool_t broadcast_result(caddr_t out, struct sockaddr_in *addr)
608 if (retries >= MAX_RETRIES) {
609 bzero((char *)addr, sizeof(struct sockaddr_in));
610 if (tell_parent(broad_domain->dom_domain, addr))
611 syslog(LOG_WARNING, "lost connection to parent");
615 if (yp_restricted && verify(addr->sin_addr)) {
617 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
620 if (tell_parent(broad_domain->dom_domain, addr))
621 syslog(LOG_WARNING, "lost connection to parent");
627 * The right way to send RPC broadcasts.
628 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
629 * blocks while waiting for replies, so we have to fork off separate
630 * broadcaster processes that do the waiting and then transmit their
631 * results back to the parent for processing. We also have to remember
632 * to save the name of the domain we're trying to bind in a global
633 * variable since clnt_broadcast() provides no way to pass things to
634 * the 'eachresult' callback function.
637 broadcast(struct _dom_binding *ypdb)
642 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
645 if (pipe(ypdb->dom_pipe_fds) < 0) {
646 syslog(LOG_WARNING, "pipe: %m");
650 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
651 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
652 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
655 flock(ypdb->dom_lockfd, LOCK_UN);
657 switch ((ypdb->dom_broadcast_pid = fork())) {
660 signal(SIGCHLD, SIG_DFL);
661 signal(SIGTERM, SIG_DFL);
664 syslog(LOG_WARNING, "fork: %m");
670 FD_SET(READFD, &svc_fdset);
675 /* Release all locks before doing anything else. */
677 close(ypbindlist->dom_lockfd);
678 ypbindlist = ypbindlist->dom_pnext;
683 * Special 'many-cast' behavior. If we're in restricted mode,
684 * we have a list of possible server addresses to try. What
685 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
686 * procedure and time the replies. Whoever replies fastest
687 * gets to be our server. Note that this is not a broadcast
688 * operation: we transmit uni-cast datagrams only.
690 if (yp_restricted && yp_manycast) {
693 struct sockaddr_in sin;
695 i = __yp_ping(restricted_addrs, yp_restricted,
696 ypdb->dom_domain, &port);
698 bzero((char *)&ypdb->dom_server_addr,
699 sizeof(struct sockaddr_in));
700 if (tell_parent(ypdb->dom_domain,
701 &ypdb->dom_server_addr))
702 syslog(LOG_WARNING, "lost connection to parent");
704 bzero((char *)&sin, sizeof(struct sockaddr_in));
705 bcopy((char *)&restricted_addrs[i],
706 (char *)&sin.sin_addr, sizeof(struct in_addr));
707 sin.sin_family = AF_INET;
709 if (tell_parent(broad_domain->dom_domain, &sin))
711 "lost connection to parent");
721 ptr = (char *)&ypdb->dom_domain;
722 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
723 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
727 if (stat != RPC_SUCCESS) {
728 bzero((char *)&ypdb->dom_server_addr,
729 sizeof(struct sockaddr_in));
730 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
731 syslog(LOG_WARNING, "lost connection to parent");
738 * The right way to check if a server is alive.
739 * Attempt to get a client handle pointing to the server and send a
740 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
741 * we invalidate this binding entry and send out a broadcast to try to
742 * establish a new binding. Note that we treat non-default domains
743 * specially: once bound, we keep tabs on our server, but if it
744 * goes away and fails to respond after one round of broadcasting, we
745 * abandon it until a client specifically references it again. We make
746 * every effort to keep our default domain bound, however, since we
747 * need it to keep the system on its feet.
750 ping(struct _dom_binding *ypdb)
753 struct timeval interval, timeout;
755 int rpcsock = RPC_ANYSOCK;
756 CLIENT *client_handle;
758 interval.tv_sec = FAIL_THRESHOLD;
759 interval.tv_usec = 0;
760 timeout.tv_sec = FAIL_THRESHOLD;
763 if (ypdb->dom_broadcast_pid)
766 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
767 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
768 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
769 /* Can't get a handle: we're dead. */
779 ptr = (char *)&ypdb->dom_domain;
781 if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
782 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
783 timeout)) != RPC_SUCCESS || out == FALSE) {
786 clnt_destroy(client_handle);
792 clnt_destroy(client_handle);
796 void rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
798 struct _dom_binding *ypdb, *prev = NULL;
800 struct ypbind_resp ybr;
801 char path[MAXPATHLEN];
804 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
805 ntohs(raddrp->sin_port), dom);*/
810 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
811 if (strcmp(ypdb->dom_domain, dom) == 0)
817 if (ypdb->dom_broadcast_pid) {
818 kill(ypdb->dom_broadcast_pid, SIGINT);
820 FD_CLR(READFD, &fdsr);
821 FD_CLR(READFD, &svc_fdset);
822 READFD = WRITEFD = -1;
826 /* if in secure mode, check originating port number */
827 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
828 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
829 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
832 ypdb->dom_broadcast_pid = 0;
838 if (raddrp->sin_addr.s_addr == (long)0) {
839 switch (ypdb->dom_default) {
842 ypbindlist = ypdb->dom_pnext;
844 prev->dom_pnext = ypdb->dom_pnext;
845 sprintf(path, "%s/%s.%ld", BINDINGDIR,
846 ypdb->dom_domain, YPVERS);
847 close(ypdb->dom_lockfd);
853 ypdb->dom_broadcast_pid = 0;
865 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
867 syslog(LOG_WARNING, "malloc: %m");
870 bzero((char *)ypdb, sizeof *ypdb);
871 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
872 ypdb->dom_lockfd = -1;
873 ypdb->dom_default = 0;
874 ypdb->dom_pnext = ypbindlist;
878 /* We've recovered from a crash: inform the world. */
879 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr)
880 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
881 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
883 bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
884 sizeof ypdb->dom_server_addr);
886 ypdb->dom_vers = YPVERS;
888 ypdb->dom_broadcast_pid = 0;
890 if (ypdb->dom_lockfd != -1)
891 close(ypdb->dom_lockfd);
893 sprintf(path, "%s/%s.%ld", BINDINGDIR,
894 ypdb->dom_domain, ypdb->dom_vers);
896 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
897 mkdir(BINDINGDIR, 0755);
898 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
902 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
903 mkdir(BINDINGDIR, 0755);
904 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
911 * ok, if BINDINGDIR exists, and we can create the binding file,
914 ypdb->dom_lockfd = fd;
916 iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
917 iov[0].iov_len = sizeof udptransp->xp_port;
918 iov[1].iov_base = (caddr_t)&ybr;
919 iov[1].iov_len = sizeof ybr;
921 bzero(&ybr, sizeof ybr);
922 ybr.ypbind_status = YPBIND_SUCC_VAL;
923 *(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
924 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
926 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
927 syslog(LOG_WARNING, "write: %m");
928 close(ypdb->dom_lockfd);
929 ypdb->dom_lockfd = -1;
935 * Check address against list of allowed servers. Return 0 if okay,
939 verify(struct in_addr addr)
943 for (i = 0; i < RESTRICTED_SERVERS; i++)
944 if (!bcmp((char *)&addr, (char *)&restricted_addrs[i],
945 sizeof(struct in_addr)))
952 * Try to set restricted mode. We default to normal mode if we can't
953 * resolve the specified hostnames.
956 yp_restricted_mode(char *args)
962 /* Find the restricted domain. */
963 if ((s = strsep(&args, ",")) == NULL)
967 /* Get the addresses of the servers. */
968 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
969 if ((h = gethostbyname(s)) == NULL)
971 bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i],
972 sizeof(struct in_addr));
976 /* ypset and ypsetme not allowed with restricted mode */
977 ypsetmode = YPSET_NO;