1 /* $NetBSD: ypbind.c,v 1.56 2006/10/05 09:18:11 tron Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@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.56 2006/10/05 09:18:11 tron 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)strlcpy(ypdb->dom_domain, dm, sizeof ypdb->dom_domain); 190 return ypdb; 191 } 192 193 static int 194 makelock(struct _dom_binding *ypdb) 195 { 196 int fd; 197 char path[MAXPATHLEN]; 198 199 (void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR, 200 ypdb->dom_domain, ypdb->dom_vers); 201 202 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) { 203 (void)mkdir(BINDINGDIR, 0755); 204 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) 205 return -1; 206 } 207 208 #if O_SHLOCK == 0 209 (void)flock(fd, LOCK_SH); 210 #endif 211 return fd; 212 } 213 214 static void 215 removelock(struct _dom_binding *ypdb) 216 { 217 char path[MAXPATHLEN]; 218 219 (void)snprintf(path, sizeof(path), "%s/%s.%ld", 220 BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers); 221 (void)unlink(path); 222 } 223 224 static void * 225 ypbindproc_null_2(SVCXPRT *transp, void *argp) 226 { 227 static char res; 228 229 #ifdef DEBUG 230 if (debug) 231 printf("ypbindproc_null_2\n"); 232 #endif 233 (void)memset(&res, 0, sizeof(res)); 234 return (void *)&res; 235 } 236 237 static void * 238 ypbindproc_domain_2(SVCXPRT *transp, void *argp) 239 { 240 static struct ypbind_resp res; 241 struct _dom_binding *ypdb; 242 char *arg = *(char **) argp; 243 time_t now; 244 int count; 245 246 #ifdef DEBUG 247 if (debug) 248 printf("ypbindproc_domain_2 %s\n", arg); 249 #endif 250 if (_yp_invalid_domain(arg)) 251 return NULL; 252 253 (void)memset(&res, 0, sizeof res); 254 res.ypbind_status = YPBIND_FAIL_VAL; 255 256 for (count = 0, ypdb = ypbindlist; 257 ypdb != NULL; 258 ypdb = ypdb->dom_pnext, count++) { 259 if (count > 100) 260 return NULL; /* prevent denial of service */ 261 if (!strcmp(ypdb->dom_domain, arg)) 262 break; 263 } 264 265 if (ypdb == NULL) { 266 ypdb = makebinding(arg); 267 ypdb->dom_vers = YPVERS; 268 ypdb->dom_alive = 0; 269 ypdb->dom_lockfd = -1; 270 removelock(ypdb); 271 ypdb->dom_xid = unique_xid(ypdb); 272 ypdb->dom_pnext = ypbindlist; 273 ypbindlist = ypdb; 274 check++; 275 #ifdef DEBUG 276 if (debug) 277 printf("unknown domain %s\n", arg); 278 #endif 279 return NULL; 280 } 281 282 if (ypdb->dom_alive == 0) { 283 #ifdef DEBUG 284 if (debug) 285 printf("dead domain %s\n", arg); 286 #endif 287 return NULL; 288 } 289 290 #ifdef HEURISTIC 291 time(&now); 292 if (now < ypdb->dom_ask_t + 5) { 293 /* 294 * Hmm. More than 2 requests in 5 seconds have indicated 295 * that my binding is possibly incorrect. 296 * Ok, do an immediate poll of the server. 297 */ 298 if (ypdb->dom_check_t >= now) { 299 /* don't flood it */ 300 ypdb->dom_check_t = 0; 301 check++; 302 } 303 } 304 ypdb->dom_ask_t = now; 305 #endif 306 307 res.ypbind_status = YPBIND_SUCC_VAL; 308 res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr = 309 ypdb->dom_server_addr.sin_addr.s_addr; 310 res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port = 311 ypdb->dom_server_addr.sin_port; 312 #ifdef DEBUG 313 if (debug) 314 printf("domain %s at %s/%d\n", ypdb->dom_domain, 315 inet_ntoa(ypdb->dom_server_addr.sin_addr), 316 ntohs(ypdb->dom_server_addr.sin_port)); 317 #endif 318 return &res; 319 } 320 321 static void * 322 ypbindproc_setdom_2(SVCXPRT *transp, void *argp) 323 { 324 struct ypbind_setdom *sd = argp; 325 struct sockaddr_in *fromsin, bindsin; 326 static bool_t res; 327 328 #ifdef DEBUG 329 if (debug) 330 printf("ypbindproc_setdom_2 %s\n", inet_ntoa(bindsin.sin_addr)); 331 #endif 332 (void)memset(&res, 0, sizeof(res)); 333 fromsin = svc_getcaller(transp); 334 335 switch (ypbindmode) { 336 case YPBIND_SETLOCAL: 337 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { 338 #ifdef DEBUG 339 if (debug) 340 printf("ypset from %s denied\n", 341 inet_ntoa(fromsin->sin_addr)); 342 #endif 343 return NULL; 344 } 345 /* FALLTHROUGH */ 346 347 case YPBIND_SETALL: 348 been_ypset = 1; 349 break; 350 351 case YPBIND_DIRECT: 352 case YPBIND_BROADCAST: 353 default: 354 #ifdef DEBUG 355 if (debug) 356 printf("ypset denied\n"); 357 #endif 358 return NULL; 359 } 360 361 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) { 362 #ifdef DEBUG 363 if (debug) 364 printf("ypset from unprivileged port denied\n"); 365 #endif 366 return &res; 367 } 368 369 if (sd->ypsetdom_vers != YPVERS) { 370 #ifdef DEBUG 371 if (debug) 372 printf("ypset with wrong version denied\n"); 373 #endif 374 return &res; 375 } 376 377 (void)memset(&bindsin, 0, sizeof bindsin); 378 bindsin.sin_family = AF_INET; 379 bindsin.sin_len = sizeof(bindsin); 380 bindsin.sin_addr = sd->ypsetdom_addr; 381 bindsin.sin_port = sd->ypsetdom_port; 382 rpc_received(sd->ypsetdom_domain, &bindsin, 1); 383 384 #ifdef DEBUG 385 if (debug) 386 printf("ypset to %s succeeded\n", inet_ntoa(bindsin.sin_addr)); 387 #endif 388 res = 1; 389 return &res; 390 } 391 392 static void 393 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp) 394 { 395 union { 396 char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1]; 397 struct ypbind_setdom ypbindproc_setdom_2_arg; 398 void *alignment; 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, "nag_servers: 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((unsigned char)*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 socklen_t fromlen; 972 int inlen; 973 struct _dom_binding *ypdb; 974 struct sockaddr_in raddr; 975 struct rpc_msg msg; 976 XDR xdr; 977 978 recv_again: 979 (void)memset(&xdr, 0, sizeof(xdr)); 980 (void)memset(&msg, 0, sizeof(msg)); 981 msg.acpted_rply.ar_verf = _null_auth; 982 msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr; 983 msg.acpted_rply.ar_results.proc = xdr_rmtcallres; 984 985 try_again: 986 fromlen = sizeof(struct sockaddr); 987 inlen = recvfrom(rpcsock, buf, sizeof buf, 0, 988 (struct sockaddr *)&raddr, &fromlen); 989 if (inlen < 0) { 990 if (errno == EINTR) 991 goto try_again; 992 return RPC_CANTRECV; 993 } 994 if (inlen < sizeof(u_int32_t)) 995 goto recv_again; 996 997 /* 998 * see if reply transaction id matches sent id. 999 * If so, decode the results. 1000 */ 1001 xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE); 1002 if (xdr_replymsg(&xdr, &msg)) { 1003 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) && 1004 (msg.acpted_rply.ar_stat == SUCCESS)) { 1005 raddr.sin_port = htons((u_short)rmtcr_port); 1006 ypdb = xid2ypdb(msg.rm_xid); 1007 if (ypdb != NULL) 1008 rpc_received(ypdb->dom_domain, &raddr, 0); 1009 } 1010 } 1011 xdr.x_op = XDR_FREE; 1012 msg.acpted_rply.ar_results.proc = xdr_void; 1013 xdr_destroy(&xdr); 1014 1015 return RPC_SUCCESS; 1016 } 1017 1018 static enum clnt_stat 1019 handle_ping(void) 1020 { 1021 char buf[BUFSIZE]; 1022 socklen_t fromlen; 1023 int inlen; 1024 struct _dom_binding *ypdb; 1025 struct sockaddr_in raddr; 1026 struct rpc_msg msg; 1027 XDR xdr; 1028 bool_t res; 1029 1030 recv_again: 1031 (void)memset(&xdr, 0, sizeof(xdr)); 1032 (void)memset(&msg, 0, sizeof(msg)); 1033 msg.acpted_rply.ar_verf = _null_auth; 1034 msg.acpted_rply.ar_results.where = (caddr_t)&res; 1035 msg.acpted_rply.ar_results.proc = xdr_bool; 1036 1037 try_again: 1038 fromlen = sizeof (struct sockaddr); 1039 inlen = recvfrom(pingsock, buf, sizeof buf, 0, 1040 (struct sockaddr *)&raddr, &fromlen); 1041 if (inlen < 0) { 1042 if (errno == EINTR) 1043 goto try_again; 1044 return RPC_CANTRECV; 1045 } 1046 if (inlen < sizeof(u_int32_t)) 1047 goto recv_again; 1048 1049 /* 1050 * see if reply transaction id matches sent id. 1051 * If so, decode the results. 1052 */ 1053 xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE); 1054 if (xdr_replymsg(&xdr, &msg)) { 1055 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) && 1056 (msg.acpted_rply.ar_stat == SUCCESS)) { 1057 ypdb = xid2ypdb(msg.rm_xid); 1058 if (ypdb != NULL) 1059 rpc_received(ypdb->dom_domain, &raddr, 0); 1060 } 1061 } 1062 xdr.x_op = XDR_FREE; 1063 msg.acpted_rply.ar_results.proc = xdr_void; 1064 xdr_destroy(&xdr); 1065 1066 return RPC_SUCCESS; 1067 } 1068 1069 /* 1070 * LOOPBACK IS MORE IMPORTANT: PUT IN HACK 1071 */ 1072 void 1073 rpc_received(char *dom, struct sockaddr_in *raddrp, int force) 1074 { 1075 struct _dom_binding *ypdb; 1076 struct iovec iov[2]; 1077 struct ypbind_resp ybr; 1078 int fd; 1079 1080 #ifdef DEBUG 1081 if (debug) 1082 printf("returned from %s about %s\n", 1083 inet_ntoa(raddrp->sin_addr), dom); 1084 #endif 1085 1086 if (dom == NULL) 1087 return; 1088 1089 if (_yp_invalid_domain(dom)) 1090 return; 1091 1092 /* don't support insecure servers by default */ 1093 if (!insecure && ntohs(raddrp->sin_port) >= IPPORT_RESERVED) 1094 return; 1095 1096 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) 1097 if (!strcmp(ypdb->dom_domain, dom)) 1098 break; 1099 1100 if (ypdb == NULL) { 1101 if (force == 0) 1102 return; 1103 ypdb = makebinding(dom); 1104 ypdb->dom_lockfd = -1; 1105 ypdb->dom_pnext = ypbindlist; 1106 ypbindlist = ypdb; 1107 } 1108 1109 /* soft update, alive */ 1110 if (ypdb->dom_alive == 1 && force == 0) { 1111 if (!memcmp(&ypdb->dom_server_addr, raddrp, 1112 sizeof ypdb->dom_server_addr)) { 1113 ypdb->dom_alive = 1; 1114 /* recheck binding in 60 sec */ 1115 ypdb->dom_check_t = time(NULL) + 60; 1116 } 1117 return; 1118 } 1119 1120 (void)memcpy(&ypdb->dom_server_addr, raddrp, 1121 sizeof ypdb->dom_server_addr); 1122 /* recheck binding in 60 seconds */ 1123 ypdb->dom_check_t = time(NULL) + 60; 1124 ypdb->dom_vers = YPVERS; 1125 ypdb->dom_alive = 1; 1126 1127 if (ypdb->dom_lockfd != -1) 1128 (void)close(ypdb->dom_lockfd); 1129 1130 if ((fd = makelock(ypdb)) == -1) 1131 return; 1132 1133 /* 1134 * ok, if BINDINGDIR exists, and we can create the binding file, 1135 * then write to it.. 1136 */ 1137 ypdb->dom_lockfd = fd; 1138 1139 iov[0].iov_base = (caddr_t)&(udptransp->xp_port); 1140 iov[0].iov_len = sizeof udptransp->xp_port; 1141 iov[1].iov_base = (caddr_t)&ybr; 1142 iov[1].iov_len = sizeof ybr; 1143 1144 (void)memset(&ybr, 0, sizeof ybr); 1145 ybr.ypbind_status = YPBIND_SUCC_VAL; 1146 ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr = 1147 raddrp->sin_addr; 1148 ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port = 1149 raddrp->sin_port; 1150 1151 if (writev(ypdb->dom_lockfd, iov, 2) != 1152 iov[0].iov_len + iov[1].iov_len) { 1153 yp_log(LOG_WARNING, "writev: %m"); 1154 (void)close(ypdb->dom_lockfd); 1155 removelock(ypdb); 1156 ypdb->dom_lockfd = -1; 1157 } 1158 } 1159 1160 static struct _dom_binding * 1161 xid2ypdb(u_int32_t xid) 1162 { 1163 struct _dom_binding *ypdb; 1164 1165 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) 1166 if (ypdb->dom_xid == xid) 1167 break; 1168 return (ypdb); 1169 } 1170 1171 static u_int32_t 1172 unique_xid(struct _dom_binding *ypdb) 1173 { 1174 u_int32_t tmp_xid; 1175 1176 tmp_xid = (u_int32_t)(((u_long)ypdb) & 0xffffffff); 1177 while (xid2ypdb(tmp_xid) != NULL) 1178 tmp_xid++; 1179 1180 return tmp_xid; 1181 } 1182