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