ypbind.c revision 1.91 1 /* $NetBSD: ypbind.c,v 1.91 2014/06/10 17:18:02 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #ifndef LINT
31 __RCSID("$NetBSD: ypbind.c,v 1.91 2014/06/10 17:18:02 dholland Exp $");
32 #endif
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/file.h>
37 #include <sys/ioctl.h>
38 #include <sys/signal.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/syslog.h>
42 #include <sys/uio.h>
43 #include <arpa/inet.h>
44 #include <net/if.h>
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <ifaddrs.h>
51 #include <limits.h>
52 #include <netdb.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <syslog.h>
58 #include <unistd.h>
59 #include <util.h>
60
61 #include <rpc/rpc.h>
62 #include <rpc/xdr.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #include <rpc/pmap_rmt.h>
66 #include <rpcsvc/yp_prot.h>
67 #include <rpcsvc/ypclnt.h>
68
69 #include "pathnames.h"
70
71 #define YPSERVERSSUFF ".ypservers"
72 #define BINDINGDIR (_PATH_VAR_YP "binding")
73
74 #ifndef O_SHLOCK
75 #define O_SHLOCK 0
76 #endif
77
78 int _yp_invalid_domain(const char *); /* XXX libc internal */
79
80 ////////////////////////////////////////////////////////////
81 // types and globals
82
83 typedef enum {
84 YPBIND_DIRECT, YPBIND_BROADCAST,
85 } ypbind_mode_t;
86
87 struct domain {
88 struct domain *dom_next;
89
90 char dom_name[YPMAXDOMAIN + 1];
91 struct sockaddr_in dom_server_addr;
92 long dom_vers;
93 time_t dom_checktime;
94 time_t dom_asktime;
95 int dom_lockfd;
96 int dom_alive;
97 uint32_t dom_xid;
98 FILE *dom_serversfile; /* /var/yp/binding/foo.ypservers */
99 int dom_been_ypset; /* ypset been done on this domain? */
100 ypbind_mode_t dom_ypbindmode; /* broadcast or direct */
101 };
102
103 #define BUFSIZE 1400
104
105 static struct domain *domains;
106 static int check;
107
108 static ypbind_mode_t default_ypbindmode;
109
110 static int allow_local_ypset = 0, allow_any_ypset = 0;
111 static int insecure;
112
113 static int rpcsock, pingsock;
114 static struct rmtcallargs rmtca;
115 static struct rmtcallres rmtcr;
116 static bool_t rmtcr_outval;
117 static unsigned long rmtcr_port;
118 static SVCXPRT *udptransp, *tcptransp;
119
120 ////////////////////////////////////////////////////////////
121 // utilities
122
123 static int
124 open_locked(const char *path, int flags, mode_t mode)
125 {
126 int fd;
127
128 fd = open(path, flags|O_SHLOCK, mode);
129 if (fd < 0) {
130 return -1;
131 }
132 #if O_SHLOCK == 0
133 /* dholland 20110522 wouldn't it be better to check this for error? */
134 (void)flock(fd, LOCK_SH);
135 #endif
136 return fd;
137 }
138
139 ////////////////////////////////////////////////////////////
140 // logging
141
142 #ifdef DEBUG
143 #define DPRINTF(...) (debug ? (void)printf(__VA_ARGS__) : (void)0)
144 static int debug;
145 #else
146 #define DPRINTF(...)
147 #endif
148
149 static void yp_log(int, const char *, ...) __printflike(2, 3);
150
151 static void
152 yp_log(int pri, const char *fmt, ...)
153 {
154 va_list ap;
155
156 va_start(ap, fmt);
157
158 #if defined(DEBUG)
159 if (debug) {
160 (void)vprintf(fmt, ap);
161 (void)printf("\n");
162 } else
163 #endif
164 vsyslog(pri, fmt, ap);
165 va_end(ap);
166 }
167
168 ////////////////////////////////////////////////////////////
169 // ypservers file
170
171 /*
172 * Get pathname for the ypservers file for a given domain
173 * (/var/yp/binding/DOMAIN.ypservers)
174 */
175 static const char *
176 ypservers_filename(const char *domain)
177 {
178 static char ret[PATH_MAX];
179
180 (void)snprintf(ret, sizeof(ret), "%s/%s%s",
181 BINDINGDIR, domain, YPSERVERSSUFF);
182 return ret;
183 }
184
185 ////////////////////////////////////////////////////////////
186 // struct domain
187
188 static struct domain *
189 domain_find(uint32_t xid)
190 {
191 struct domain *dom;
192
193 for (dom = domains; dom != NULL; dom = dom->dom_next)
194 if (dom->dom_xid == xid)
195 break;
196 return dom;
197 }
198
199 static uint32_t
200 unique_xid(struct domain *dom)
201 {
202 uint32_t tmp_xid;
203
204 tmp_xid = ((uint32_t)(unsigned long)dom) & 0xffffffff;
205 while (domain_find(tmp_xid) != NULL)
206 tmp_xid++;
207
208 return tmp_xid;
209 }
210
211 static struct domain *
212 domain_create(const char *name)
213 {
214 struct domain *dom;
215 const char *pathname;
216 struct stat st;
217
218 dom = malloc(sizeof *dom);
219 if (dom == NULL) {
220 yp_log(LOG_ERR, "domain_create: Out of memory");
221 exit(1);
222 }
223
224 dom->dom_next = NULL;
225
226 (void)strlcpy(dom->dom_name, name, sizeof(dom->dom_name));
227 (void)memset(&dom->dom_server_addr, 0, sizeof(dom->dom_server_addr));
228 dom->dom_vers = YPVERS;
229 dom->dom_checktime = 0;
230 dom->dom_asktime = 0;
231 dom->dom_lockfd = -1;
232 dom->dom_alive = 0;
233 dom->dom_xid = unique_xid(dom);
234 dom->dom_been_ypset = 0;
235 dom->dom_serversfile = NULL;
236
237 /*
238 * Per traditional ypbind(8) semantics, if a ypservers
239 * file does not exist, we revert to broadcast mode.
240 *
241 * The sysadmin can force broadcast mode by passing the
242 * -broadcast flag. There is currently no way to fail and
243 * reject domains for which there is no ypservers file.
244 */
245 dom->dom_ypbindmode = default_ypbindmode;
246 if (dom->dom_ypbindmode == YPBIND_DIRECT) {
247 pathname = ypservers_filename(dom->dom_name);
248 if (stat(pathname, &st) < 0) {
249 /* XXX syslog a warning here? */
250 DPRINTF("%s does not exist, defaulting to broadcast\n",
251 pathname);
252 dom->dom_ypbindmode = YPBIND_BROADCAST;
253 }
254 }
255
256 /* add to global list */
257 dom->dom_next = domains;
258 domains = dom;
259
260 return dom;
261 }
262
263 ////////////////////////////////////////////////////////////
264 // locks
265
266 static int
267 makelock(struct domain *dom)
268 {
269 int fd;
270 char path[MAXPATHLEN];
271
272 (void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
273 dom->dom_name, dom->dom_vers);
274
275 fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
276 if (fd == -1) {
277 (void)mkdir(BINDINGDIR, 0755);
278 fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
279 if (fd == -1) {
280 return -1;
281 }
282 }
283
284 return fd;
285 }
286
287 static void
288 removelock(struct domain *dom)
289 {
290 char path[MAXPATHLEN];
291
292 (void)snprintf(path, sizeof(path), "%s/%s.%ld",
293 BINDINGDIR, dom->dom_name, dom->dom_vers);
294 (void)unlink(path);
295 }
296
297 /*
298 * purge_bindingdir: remove old binding files (i.e. "rm BINDINGDIR\/\*.[0-9]")
299 *
300 * local YP functions [e.g. yp_master()] will fail without even talking
301 * to ypbind if there is a stale (non-flock'd) binding file present.
302 * we have to scan the entire BINDINGDIR for binding files, because
303 * ypbind may bind more than just the yp_get_default_domain() domain.
304 */
305 static int
306 purge_bindingdir(const char *dirpath)
307 {
308 DIR *dirp;
309 int unlinkedfiles, l;
310 struct dirent *dp;
311 char pathname[MAXPATHLEN];
312
313 if ((dirp = opendir(dirpath)) == NULL)
314 return(-1); /* at this point, shouldn't ever happen */
315
316 do {
317 unlinkedfiles = 0;
318 while ((dp = readdir(dirp)) != NULL) {
319 l = dp->d_namlen;
320 /* 'rm *.[0-9]' */
321 if (l > 2 && dp->d_name[l-2] == '.' &&
322 dp->d_name[l-1] >= '0' && dp->d_name[l-1] <= '9') {
323 (void)snprintf(pathname, sizeof(pathname),
324 "%s/%s", dirpath, dp->d_name);
325 if (unlink(pathname) < 0 && errno != ENOENT)
326 return(-1);
327 unlinkedfiles++;
328 }
329 }
330
331 /* rescan dir if we removed it */
332 if (unlinkedfiles)
333 rewinddir(dirp);
334
335 } while (unlinkedfiles);
336
337 closedir(dirp);
338 return(0);
339 }
340
341 ////////////////////////////////////////////////////////////
342 // sunrpc twaddle
343
344 /*
345 * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
346 */
347 static void
348 rpc_received(char *dom_name, struct sockaddr_in *raddrp, int force,
349 int is_ypset)
350 {
351 struct domain *dom;
352 struct iovec iov[2];
353 struct ypbind_resp ybr;
354 ssize_t result;
355 int fd;
356
357 DPRINTF("returned from %s about %s\n",
358 inet_ntoa(raddrp->sin_addr), dom_name);
359
360 if (dom_name == NULL)
361 return;
362
363 if (_yp_invalid_domain(dom_name))
364 return;
365
366 /* don't support insecure servers by default */
367 if (!insecure && ntohs(raddrp->sin_port) >= IPPORT_RESERVED)
368 return;
369
370 for (dom = domains; dom != NULL; dom = dom->dom_next)
371 if (!strcmp(dom->dom_name, dom_name))
372 break;
373
374 if (dom == NULL) {
375 if (force == 0)
376 return;
377 dom = domain_create(dom_name);
378 }
379
380 if (is_ypset) {
381 dom->dom_been_ypset = 1;
382 }
383
384 /* soft update, alive */
385 if (dom->dom_alive == 1 && force == 0) {
386 if (!memcmp(&dom->dom_server_addr, raddrp,
387 sizeof(dom->dom_server_addr))) {
388 dom->dom_alive = 1;
389 /* recheck binding in 60 sec */
390 dom->dom_checktime = time(NULL) + 60;
391 }
392 return;
393 }
394
395 (void)memcpy(&dom->dom_server_addr, raddrp,
396 sizeof(dom->dom_server_addr));
397 /* recheck binding in 60 seconds */
398 dom->dom_checktime = time(NULL) + 60;
399 dom->dom_alive = 1;
400
401 if (dom->dom_lockfd != -1)
402 (void)close(dom->dom_lockfd);
403
404 if ((fd = makelock(dom)) == -1)
405 return;
406
407 /*
408 * ok, if BINDINGDIR exists, and we can create the binding file,
409 * then write to it..
410 */
411 dom->dom_lockfd = fd;
412
413 iov[0].iov_base = &(udptransp->xp_port);
414 iov[0].iov_len = sizeof udptransp->xp_port;
415 iov[1].iov_base = &ybr;
416 iov[1].iov_len = sizeof ybr;
417
418 (void)memset(&ybr, 0, sizeof ybr);
419 ybr.ypbind_status = YPBIND_SUCC_VAL;
420 ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
421 raddrp->sin_addr;
422 ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
423 raddrp->sin_port;
424
425 result = writev(dom->dom_lockfd, iov, 2);
426 if (result < 0 || (size_t)result != iov[0].iov_len + iov[1].iov_len) {
427 if (result < 0)
428 yp_log(LOG_WARNING, "writev: %s", strerror(errno));
429 else
430 yp_log(LOG_WARNING, "writev: short count");
431 (void)close(dom->dom_lockfd);
432 removelock(dom);
433 dom->dom_lockfd = -1;
434 }
435 }
436
437 static void *
438 /*ARGSUSED*/
439 ypbindproc_null_2(SVCXPRT *transp, void *argp)
440 {
441 static char res;
442
443 DPRINTF("ypbindproc_null_2\n");
444 (void)memset(&res, 0, sizeof(res));
445 return (void *)&res;
446 }
447
448 static void *
449 /*ARGSUSED*/
450 ypbindproc_domain_2(SVCXPRT *transp, void *argp)
451 {
452 static struct ypbind_resp res;
453 struct domain *dom;
454 char *arg = *(char **) argp;
455 time_t now;
456 int count;
457
458 DPRINTF("ypbindproc_domain_2 %s\n", arg);
459 if (_yp_invalid_domain(arg))
460 return NULL;
461
462 (void)memset(&res, 0, sizeof res);
463 res.ypbind_status = YPBIND_FAIL_VAL;
464
465 for (count = 0, dom = domains;
466 dom != NULL;
467 dom = dom->dom_next, count++) {
468 if (count > 100)
469 return NULL; /* prevent denial of service */
470 if (!strcmp(dom->dom_name, arg))
471 break;
472 }
473
474 if (dom == NULL) {
475 dom = domain_create(arg);
476 removelock(dom);
477 check++;
478 DPRINTF("unknown domain %s\n", arg);
479 return NULL;
480 }
481
482 if (dom->dom_alive == 0) {
483 DPRINTF("dead domain %s\n", arg);
484 return NULL;
485 }
486
487 #ifdef HEURISTIC
488 (void)time(&now);
489 if (now < dom->dom_asktime + 5) {
490 /*
491 * Hmm. More than 2 requests in 5 seconds have indicated
492 * that my binding is possibly incorrect.
493 * Ok, do an immediate poll of the server.
494 */
495 if (dom->dom_checktime >= now) {
496 /* don't flood it */
497 dom->dom_checktime = 0;
498 check++;
499 }
500 }
501 dom->dom_asktime = now;
502 #endif
503
504 res.ypbind_status = YPBIND_SUCC_VAL;
505 res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
506 dom->dom_server_addr.sin_addr.s_addr;
507 res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
508 dom->dom_server_addr.sin_port;
509 DPRINTF("domain %s at %s/%d\n", dom->dom_name,
510 inet_ntoa(dom->dom_server_addr.sin_addr),
511 ntohs(dom->dom_server_addr.sin_port));
512 return &res;
513 }
514
515 static void *
516 ypbindproc_setdom_2(SVCXPRT *transp, void *argp)
517 {
518 struct ypbind_setdom *sd = argp;
519 struct sockaddr_in *fromsin, bindsin;
520 static bool_t res;
521
522 (void)memset(&res, 0, sizeof(res));
523 fromsin = svc_getcaller(transp);
524 DPRINTF("ypbindproc_setdom_2 from %s\n", inet_ntoa(fromsin->sin_addr));
525
526 if (allow_any_ypset) {
527 /* nothing */
528 } else if (allow_local_ypset) {
529 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
530 DPRINTF("ypset denied from %s\n",
531 inet_ntoa(fromsin->sin_addr));
532 return NULL;
533 }
534 } else {
535 DPRINTF("ypset denied\n");
536 return NULL;
537 }
538
539 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
540 DPRINTF("ypset from unprivileged port denied\n");
541 return &res;
542 }
543
544 if (sd->ypsetdom_vers != YPVERS) {
545 DPRINTF("ypset with wrong version denied\n");
546 return &res;
547 }
548
549 (void)memset(&bindsin, 0, sizeof bindsin);
550 bindsin.sin_family = AF_INET;
551 bindsin.sin_len = sizeof(bindsin);
552 bindsin.sin_addr = sd->ypsetdom_addr;
553 bindsin.sin_port = sd->ypsetdom_port;
554 rpc_received(sd->ypsetdom_domain, &bindsin, 1, 1);
555
556 DPRINTF("ypset to %s for domain %s succeeded\n",
557 inet_ntoa(bindsin.sin_addr), sd->ypsetdom_domain);
558 res = 1;
559 return &res;
560 }
561
562 static void
563 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
564 {
565 union {
566 char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
567 struct ypbind_setdom ypbindproc_setdom_2_arg;
568 void *alignment;
569 } argument;
570 struct authunix_parms *creds;
571 char *result;
572 xdrproc_t xdr_argument, xdr_result;
573 void *(*local)(SVCXPRT *, void *);
574
575 switch (rqstp->rq_proc) {
576 case YPBINDPROC_NULL:
577 xdr_argument = (xdrproc_t)xdr_void;
578 xdr_result = (xdrproc_t)xdr_void;
579 local = ypbindproc_null_2;
580 break;
581
582 case YPBINDPROC_DOMAIN:
583 xdr_argument = (xdrproc_t)xdr_ypdomain_wrap_string;
584 xdr_result = (xdrproc_t)xdr_ypbind_resp;
585 local = ypbindproc_domain_2;
586 break;
587
588 case YPBINDPROC_SETDOM:
589 switch (rqstp->rq_cred.oa_flavor) {
590 case AUTH_UNIX:
591 creds = (struct authunix_parms *)rqstp->rq_clntcred;
592 if (creds->aup_uid != 0) {
593 svcerr_auth(transp, AUTH_BADCRED);
594 return;
595 }
596 break;
597 default:
598 svcerr_auth(transp, AUTH_TOOWEAK);
599 return;
600 }
601
602 xdr_argument = (xdrproc_t)xdr_ypbind_setdom;
603 xdr_result = (xdrproc_t)xdr_void;
604 local = ypbindproc_setdom_2;
605 break;
606
607 default:
608 svcerr_noproc(transp);
609 return;
610 }
611 (void)memset(&argument, 0, sizeof(argument));
612 if (!svc_getargs(transp, xdr_argument, (caddr_t)(void *)&argument)) {
613 svcerr_decode(transp);
614 return;
615 }
616 result = (*local)(transp, &argument);
617 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
618 svcerr_systemerr(transp);
619 }
620 return;
621 }
622
623 static void
624 sunrpc_setup(void)
625 {
626 int one;
627
628 (void)pmap_unset(YPBINDPROG, YPBINDVERS);
629
630 udptransp = svcudp_create(RPC_ANYSOCK);
631 if (udptransp == NULL)
632 errx(1, "Cannot create udp service.");
633
634 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
635 IPPROTO_UDP))
636 errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
637
638 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
639 if (tcptransp == NULL)
640 errx(1, "Cannot create tcp service.");
641
642 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
643 IPPROTO_TCP))
644 errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
645
646 /* XXX use SOCK_STREAM for direct queries? */
647 if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
648 err(1, "rpc socket");
649 if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
650 err(1, "ping socket");
651
652 (void)fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
653 (void)fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
654
655 one = 1;
656 (void)setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one,
657 (socklen_t)sizeof(one));
658 rmtca.prog = YPPROG;
659 rmtca.vers = YPVERS;
660 rmtca.proc = YPPROC_DOMAIN_NONACK;
661 rmtca.xdr_args = NULL; /* set at call time */
662 rmtca.args_ptr = NULL; /* set at call time */
663 rmtcr.port_ptr = &rmtcr_port;
664 rmtcr.xdr_results = (xdrproc_t)xdr_bool;
665 rmtcr.results_ptr = (caddr_t)(void *)&rmtcr_outval;
666 }
667
668 ////////////////////////////////////////////////////////////
669 // operational logic
670
671 static int
672 broadcast(char *buf, int outlen)
673 {
674 struct ifaddrs *ifap, *ifa;
675 struct sockaddr_in bindsin;
676 struct in_addr in;
677
678 (void)memset(&bindsin, 0, sizeof bindsin);
679 bindsin.sin_family = AF_INET;
680 bindsin.sin_len = sizeof(bindsin);
681 bindsin.sin_port = htons(PMAPPORT);
682
683 if (getifaddrs(&ifap) != 0) {
684 yp_log(LOG_WARNING, "broadcast: getifaddrs: %s",
685 strerror(errno));
686 return (-1);
687 }
688 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
689 if (ifa->ifa_addr->sa_family != AF_INET)
690 continue;
691 if ((ifa->ifa_flags & IFF_UP) == 0)
692 continue;
693
694 switch (ifa->ifa_flags & (IFF_LOOPBACK | IFF_BROADCAST)) {
695 case IFF_BROADCAST:
696 if (!ifa->ifa_broadaddr)
697 continue;
698 if (ifa->ifa_broadaddr->sa_family != AF_INET)
699 continue;
700 in = ((struct sockaddr_in *)(void *)ifa->ifa_broadaddr)->sin_addr;
701 break;
702 case IFF_LOOPBACK:
703 in = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr;
704 break;
705 default:
706 continue;
707 }
708
709 bindsin.sin_addr = in;
710 DPRINTF("broadcast %x\n", bindsin.sin_addr.s_addr);
711 if (sendto(rpcsock, buf, outlen, 0,
712 (struct sockaddr *)(void *)&bindsin,
713 (socklen_t)bindsin.sin_len) == -1)
714 yp_log(LOG_WARNING, "broadcast: sendto: %s",
715 strerror(errno));
716 }
717 freeifaddrs(ifap);
718 return (0);
719 }
720
721 static int
722 direct(char *buf, int outlen, struct domain *dom)
723 {
724 const char *path;
725 char line[_POSIX2_LINE_MAX];
726 char *p;
727 struct hostent *hp;
728 struct sockaddr_in bindsin;
729 int i, count = 0;
730
731 /*
732 * XXX what happens if someone's editor unlinks and replaces
733 * the servers file?
734 */
735
736 if (dom->dom_serversfile != NULL) {
737 rewind(dom->dom_serversfile);
738 } else {
739 path = ypservers_filename(dom->dom_name);
740 dom->dom_serversfile = fopen(path, "r");
741 if (dom->dom_serversfile == NULL) {
742 /*
743 * XXX there should be a time restriction on
744 * this (and/or on trying the open) so we
745 * don't flood the log. Or should we fall back
746 * to broadcast mode?
747 */
748 yp_log(LOG_ERR, "%s: %s", path,
749 strerror(errno));
750 return -1;
751 }
752 }
753
754 (void)memset(&bindsin, 0, sizeof bindsin);
755 bindsin.sin_family = AF_INET;
756 bindsin.sin_len = sizeof(bindsin);
757 bindsin.sin_port = htons(PMAPPORT);
758
759 while (fgets(line, (int)sizeof(line), dom->dom_serversfile) != NULL) {
760 /* skip lines that are too big */
761 p = strchr(line, '\n');
762 if (p == NULL) {
763 int c;
764
765 while ((c = getc(dom->dom_serversfile)) != '\n' && c != EOF)
766 ;
767 continue;
768 }
769 *p = '\0';
770 p = line;
771 while (isspace((unsigned char)*p))
772 p++;
773 if (*p == '#')
774 continue;
775 hp = gethostbyname(p);
776 if (!hp) {
777 yp_log(LOG_WARNING, "%s: %s", p, hstrerror(h_errno));
778 continue;
779 }
780 /* step through all addresses in case first is unavailable */
781 for (i = 0; hp->h_addr_list[i]; i++) {
782 (void)memcpy(&bindsin.sin_addr, hp->h_addr_list[0],
783 hp->h_length);
784 if (sendto(rpcsock, buf, outlen, 0,
785 (struct sockaddr *)(void *)&bindsin,
786 (socklen_t)sizeof(bindsin)) < 0) {
787 yp_log(LOG_WARNING, "direct: sendto: %s",
788 strerror(errno));
789 continue;
790 } else
791 count++;
792 }
793 }
794 if (!count) {
795 yp_log(LOG_WARNING, "No contactable servers found in %s",
796 ypservers_filename(dom->dom_name));
797 return -1;
798 }
799 return 0;
800 }
801
802 static int
803 direct_set(char *buf, int outlen, struct domain *dom)
804 {
805 struct sockaddr_in bindsin;
806 char path[MAXPATHLEN];
807 struct iovec iov[2];
808 struct ypbind_resp ybr;
809 SVCXPRT dummy_svc;
810 int fd;
811 ssize_t bytes;
812
813 /*
814 * Gack, we lose if binding file went away. We reset
815 * "been_set" if this happens, otherwise we'll never
816 * bind again.
817 */
818 (void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
819 dom->dom_name, dom->dom_vers);
820
821 fd = open_locked(path, O_RDONLY, 0644);
822 if (fd == -1) {
823 yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
824 dom->dom_been_ypset = 0;
825 return -1;
826 }
827
828 /* Read the binding file... */
829 iov[0].iov_base = &(dummy_svc.xp_port);
830 iov[0].iov_len = sizeof(dummy_svc.xp_port);
831 iov[1].iov_base = &ybr;
832 iov[1].iov_len = sizeof(ybr);
833 bytes = readv(fd, iov, 2);
834 (void)close(fd);
835 if (bytes <0 || (size_t)bytes != (iov[0].iov_len + iov[1].iov_len)) {
836 /* Binding file corrupt? */
837 if (bytes < 0)
838 yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
839 else
840 yp_log(LOG_WARNING, "%s: short read", path);
841 dom->dom_been_ypset = 0;
842 return -1;
843 }
844
845 bindsin.sin_addr =
846 ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
847
848 if (sendto(rpcsock, buf, outlen, 0,
849 (struct sockaddr *)(void *)&bindsin,
850 (socklen_t)sizeof(bindsin)) < 0) {
851 yp_log(LOG_WARNING, "direct_set: sendto: %s", strerror(errno));
852 return -1;
853 }
854
855 return 0;
856 }
857
858 static enum clnt_stat
859 handle_replies(void)
860 {
861 char buf[BUFSIZE];
862 socklen_t fromlen;
863 ssize_t inlen;
864 struct domain *dom;
865 struct sockaddr_in raddr;
866 struct rpc_msg msg;
867 XDR xdr;
868
869 recv_again:
870 DPRINTF("handle_replies receiving\n");
871 (void)memset(&xdr, 0, sizeof(xdr));
872 (void)memset(&msg, 0, sizeof(msg));
873 msg.acpted_rply.ar_verf = _null_auth;
874 msg.acpted_rply.ar_results.where = (caddr_t)(void *)&rmtcr;
875 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
876
877 try_again:
878 fromlen = sizeof(struct sockaddr);
879 inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
880 (struct sockaddr *)(void *)&raddr, &fromlen);
881 if (inlen < 0) {
882 if (errno == EINTR)
883 goto try_again;
884 DPRINTF("handle_replies: recvfrom failed (%s)\n",
885 strerror(errno));
886 return RPC_CANTRECV;
887 }
888 if ((size_t)inlen < sizeof(uint32_t))
889 goto recv_again;
890
891 /*
892 * see if reply transaction id matches sent id.
893 * If so, decode the results.
894 */
895 xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
896 if (xdr_replymsg(&xdr, &msg)) {
897 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
898 (msg.acpted_rply.ar_stat == SUCCESS)) {
899 raddr.sin_port = htons((uint16_t)rmtcr_port);
900 dom = domain_find(msg.rm_xid);
901 if (dom != NULL)
902 rpc_received(dom->dom_name, &raddr, 0, 0);
903 }
904 }
905 xdr.x_op = XDR_FREE;
906 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
907 xdr_destroy(&xdr);
908
909 return RPC_SUCCESS;
910 }
911
912 static enum clnt_stat
913 handle_ping(void)
914 {
915 char buf[BUFSIZE];
916 socklen_t fromlen;
917 ssize_t inlen;
918 struct domain *dom;
919 struct sockaddr_in raddr;
920 struct rpc_msg msg;
921 XDR xdr;
922 bool_t res;
923
924 recv_again:
925 DPRINTF("handle_ping receiving\n");
926 (void)memset(&xdr, 0, sizeof(xdr));
927 (void)memset(&msg, 0, sizeof(msg));
928 msg.acpted_rply.ar_verf = _null_auth;
929 msg.acpted_rply.ar_results.where = (caddr_t)(void *)&res;
930 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_bool;
931
932 try_again:
933 fromlen = sizeof (struct sockaddr);
934 inlen = recvfrom(pingsock, buf, sizeof buf, 0,
935 (struct sockaddr *)(void *)&raddr, &fromlen);
936 if (inlen < 0) {
937 if (errno == EINTR)
938 goto try_again;
939 DPRINTF("handle_ping: recvfrom failed (%s)\n",
940 strerror(errno));
941 return RPC_CANTRECV;
942 }
943 if ((size_t)inlen < sizeof(uint32_t))
944 goto recv_again;
945
946 /*
947 * see if reply transaction id matches sent id.
948 * If so, decode the results.
949 */
950 xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
951 if (xdr_replymsg(&xdr, &msg)) {
952 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
953 (msg.acpted_rply.ar_stat == SUCCESS)) {
954 dom = domain_find(msg.rm_xid);
955 if (dom != NULL)
956 rpc_received(dom->dom_name, &raddr, 0, 0);
957 }
958 }
959 xdr.x_op = XDR_FREE;
960 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
961 xdr_destroy(&xdr);
962
963 return RPC_SUCCESS;
964 }
965
966 static int
967 nag_servers(struct domain *dom)
968 {
969 char *dom_name = dom->dom_name;
970 struct rpc_msg msg;
971 char buf[BUFSIZE];
972 enum clnt_stat st;
973 int outlen;
974 AUTH *rpcua;
975 XDR xdr;
976
977 DPRINTF("nag_servers\n");
978 rmtca.xdr_args = (xdrproc_t)xdr_ypdomain_wrap_string;
979 rmtca.args_ptr = (caddr_t)(void *)&dom_name;
980
981 (void)memset(&xdr, 0, sizeof xdr);
982 (void)memset(&msg, 0, sizeof msg);
983
984 rpcua = authunix_create_default();
985 if (rpcua == NULL) {
986 DPRINTF("cannot get unix auth\n");
987 return RPC_SYSTEMERROR;
988 }
989 msg.rm_direction = CALL;
990 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
991 msg.rm_call.cb_prog = PMAPPROG;
992 msg.rm_call.cb_vers = PMAPVERS;
993 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
994 msg.rm_call.cb_cred = rpcua->ah_cred;
995 msg.rm_call.cb_verf = rpcua->ah_verf;
996
997 msg.rm_xid = dom->dom_xid;
998 xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
999 if (!xdr_callmsg(&xdr, &msg)) {
1000 st = RPC_CANTENCODEARGS;
1001 AUTH_DESTROY(rpcua);
1002 return st;
1003 }
1004 if (!xdr_rmtcall_args(&xdr, &rmtca)) {
1005 st = RPC_CANTENCODEARGS;
1006 AUTH_DESTROY(rpcua);
1007 return st;
1008 }
1009 outlen = (int)xdr_getpos(&xdr);
1010 xdr_destroy(&xdr);
1011 if (outlen < 1) {
1012 st = RPC_CANTENCODEARGS;
1013 AUTH_DESTROY(rpcua);
1014 return st;
1015 }
1016 AUTH_DESTROY(rpcua);
1017
1018 if (dom->dom_lockfd != -1) {
1019 (void)close(dom->dom_lockfd);
1020 dom->dom_lockfd = -1;
1021 removelock(dom);
1022 }
1023
1024 if (dom->dom_alive == 2) {
1025 /*
1026 * This resolves the following situation:
1027 * ypserver on other subnet was once bound,
1028 * but rebooted and is now using a different port
1029 */
1030 struct sockaddr_in bindsin;
1031
1032 (void)memset(&bindsin, 0, sizeof bindsin);
1033 bindsin.sin_family = AF_INET;
1034 bindsin.sin_len = sizeof(bindsin);
1035 bindsin.sin_port = htons(PMAPPORT);
1036 bindsin.sin_addr = dom->dom_server_addr.sin_addr;
1037
1038 if (sendto(rpcsock, buf, outlen, 0,
1039 (struct sockaddr *)(void *)&bindsin,
1040 (socklen_t)sizeof bindsin) == -1)
1041 yp_log(LOG_WARNING, "nag_servers: sendto: %s",
1042 strerror(errno));
1043 }
1044
1045 switch (dom->dom_ypbindmode) {
1046 case YPBIND_BROADCAST:
1047 if (dom->dom_been_ypset) {
1048 return direct_set(buf, outlen, dom);
1049 }
1050 return broadcast(buf, outlen);
1051
1052 case YPBIND_DIRECT:
1053 return direct(buf, outlen, dom);
1054 }
1055 /*NOTREACHED*/
1056 return -1;
1057 }
1058
1059 static int
1060 ping(struct domain *dom)
1061 {
1062 char *dom_name = dom->dom_name;
1063 struct rpc_msg msg;
1064 char buf[BUFSIZE];
1065 enum clnt_stat st;
1066 int outlen;
1067 AUTH *rpcua;
1068 XDR xdr;
1069
1070 (void)memset(&xdr, 0, sizeof xdr);
1071 (void)memset(&msg, 0, sizeof msg);
1072
1073 rpcua = authunix_create_default();
1074 if (rpcua == NULL) {
1075 DPRINTF("cannot get unix auth\n");
1076 return RPC_SYSTEMERROR;
1077 }
1078
1079 msg.rm_direction = CALL;
1080 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
1081 msg.rm_call.cb_prog = YPPROG;
1082 msg.rm_call.cb_vers = YPVERS;
1083 msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
1084 msg.rm_call.cb_cred = rpcua->ah_cred;
1085 msg.rm_call.cb_verf = rpcua->ah_verf;
1086
1087 msg.rm_xid = dom->dom_xid;
1088 xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
1089 if (!xdr_callmsg(&xdr, &msg)) {
1090 st = RPC_CANTENCODEARGS;
1091 AUTH_DESTROY(rpcua);
1092 return st;
1093 }
1094 if (!xdr_ypdomain_wrap_string(&xdr, &dom_name)) {
1095 st = RPC_CANTENCODEARGS;
1096 AUTH_DESTROY(rpcua);
1097 return st;
1098 }
1099 outlen = (int)xdr_getpos(&xdr);
1100 xdr_destroy(&xdr);
1101 if (outlen < 1) {
1102 st = RPC_CANTENCODEARGS;
1103 AUTH_DESTROY(rpcua);
1104 return st;
1105 }
1106 AUTH_DESTROY(rpcua);
1107
1108 dom->dom_alive = 2;
1109 DPRINTF("ping %x\n", dom->dom_server_addr.sin_addr.s_addr);
1110
1111 if (sendto(pingsock, buf, outlen, 0,
1112 (struct sockaddr *)(void *)&dom->dom_server_addr,
1113 (socklen_t)(sizeof dom->dom_server_addr)) == -1)
1114 yp_log(LOG_WARNING, "ping: sendto: %s", strerror(errno));
1115 return 0;
1116
1117 }
1118
1119 /*
1120 * State transition is done like this:
1121 *
1122 * STATE EVENT ACTION NEWSTATE TIMEOUT
1123 * no binding timeout broadcast no binding 5 sec
1124 * no binding answer -- binding 60 sec
1125 * binding timeout ping server checking 5 sec
1126 * checking timeout ping server + broadcast checking 5 sec
1127 * checking answer -- binding 60 sec
1128 */
1129 static void
1130 checkwork(void)
1131 {
1132 struct domain *dom;
1133 time_t t;
1134
1135 check = 0;
1136
1137 (void)time(&t);
1138 for (dom = domains; dom != NULL; dom = dom->dom_next) {
1139 if (dom->dom_checktime < t) {
1140 if (dom->dom_alive == 1)
1141 (void)ping(dom);
1142 else
1143 (void)nag_servers(dom);
1144 (void)time(&t);
1145 dom->dom_checktime = t + 5;
1146 }
1147 }
1148 }
1149
1150 ////////////////////////////////////////////////////////////
1151 // main
1152
1153 __dead static void
1154 usage(void)
1155 {
1156 const char *opt = "";
1157 #ifdef DEBUG
1158 opt = " [-d]";
1159 #endif
1160
1161 (void)fprintf(stderr,
1162 "Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme]%s\n",
1163 getprogname(), opt);
1164 exit(1);
1165 }
1166
1167 int
1168 main(int argc, char *argv[])
1169 {
1170 struct timeval tv;
1171 fd_set fdsr;
1172 int width, lockfd;
1173 int evil = 0;
1174 char *domainname;
1175
1176 setprogname(argv[0]);
1177 (void)yp_get_default_domain(&domainname);
1178 if (domainname[0] == '\0')
1179 errx(1, "Domainname not set. Aborting.");
1180 if (_yp_invalid_domain(domainname))
1181 errx(1, "Invalid domainname: %s", domainname);
1182
1183 default_ypbindmode = YPBIND_DIRECT;
1184
1185 while (--argc) {
1186 ++argv;
1187 if (!strcmp("-insecure", *argv)) {
1188 insecure = 1;
1189 } else if (!strcmp("-ypset", *argv)) {
1190 allow_any_ypset = 1;
1191 allow_local_ypset = 1;
1192 } else if (!strcmp("-ypsetme", *argv)) {
1193 allow_any_ypset = 0;
1194 allow_local_ypset = 1;
1195 } else if (!strcmp("-broadcast", *argv)) {
1196 default_ypbindmode = YPBIND_BROADCAST;
1197 #ifdef DEBUG
1198 } else if (!strcmp("-d", *argv)) {
1199 debug = 1;
1200 #endif
1201 } else {
1202 usage();
1203 }
1204 }
1205
1206 /* initialise syslog */
1207 openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON);
1208
1209 /* acquire ypbind.lock */
1210 lockfd = open_locked(_PATH_YPBIND_LOCK, O_CREAT|O_RDWR|O_TRUNC, 0644);
1211 if (lockfd == -1)
1212 err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
1213
1214 /* initialize sunrpc stuff */
1215 sunrpc_setup();
1216
1217 /* blow away old bindings in BINDINGDIR */
1218 if (purge_bindingdir(BINDINGDIR) < 0)
1219 errx(1, "unable to purge old bindings from %s", BINDINGDIR);
1220
1221 /* build initial domain binding, make it "unsuccessful" */
1222 domains = domain_create(domainname);
1223 removelock(domains);
1224
1225 checkwork();
1226
1227 for (;;) {
1228 width = svc_maxfd;
1229 if (rpcsock > width)
1230 width = rpcsock;
1231 if (pingsock > width)
1232 width = pingsock;
1233 width++;
1234 fdsr = svc_fdset;
1235 FD_SET(rpcsock, &fdsr);
1236 FD_SET(pingsock, &fdsr);
1237 tv.tv_sec = 1;
1238 tv.tv_usec = 0;
1239
1240 switch (select(width, &fdsr, NULL, NULL, &tv)) {
1241 case 0:
1242 checkwork();
1243 break;
1244 case -1:
1245 yp_log(LOG_WARNING, "select: %s", strerror(errno));
1246 break;
1247 default:
1248 if (FD_ISSET(rpcsock, &fdsr))
1249 (void)handle_replies();
1250 if (FD_ISSET(pingsock, &fdsr))
1251 (void)handle_ping();
1252 svc_getreqset(&fdsr);
1253 if (check)
1254 checkwork();
1255 break;
1256 }
1257
1258 if (!evil && domains->dom_alive) {
1259 evil = 1;
1260 #ifdef DEBUG
1261 if (!debug)
1262 #endif
1263 (void)daemon(0, 0);
1264 (void)pidfile(NULL);
1265 }
1266 }
1267 }
1268