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