1 /* 2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/types.h> 35 #include <sys/wait.h> 36 #include <sys/ioctl.h> 37 #include <sys/signal.h> 38 #include <sys/socket.h> 39 #include <sys/file.h> 40 #include <sys/fcntl.h> 41 #include <sys/stat.h> 42 #include <sys/uio.h> 43 #include <ctype.h> 44 #include <dirent.h> 45 #include <err.h> 46 #include <errno.h> 47 #include <netdb.h> 48 #include <signal.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <syslog.h> 53 #include <unistd.h> 54 #include <rpc/rpc.h> 55 #include <rpc/xdr.h> 56 #include <net/if.h> 57 #include <netinet/in.h> 58 #include <arpa/inet.h> 59 #include <rpc/pmap_clnt.h> 60 #include <rpc/pmap_prot.h> 61 #include <rpc/pmap_rmt.h> 62 #include <rpc/rpc_com.h> 63 #include <rpcsvc/yp.h> 64 struct dom_binding{}; 65 #include <rpcsvc/ypclnt.h> 66 #include "yp_ping.h" 67 68 #ifndef BINDINGDIR 69 #define BINDINGDIR "/var/yp/binding" 70 #endif 71 72 #ifndef YPBINDLOCK 73 #define YPBINDLOCK "/var/run/ypbind.lock" 74 #endif 75 76 struct _dom_binding { 77 struct _dom_binding *dom_pnext; 78 char dom_domain[YPMAXDOMAIN + 1]; 79 struct sockaddr_in dom_server_addr; 80 long int dom_vers; 81 int dom_lockfd; 82 int dom_alive; 83 int dom_broadcast_pid; 84 int dom_pipe_fds[2]; 85 int dom_default; 86 }; 87 88 #define READFD ypdb->dom_pipe_fds[0] 89 #define WRITEFD ypdb->dom_pipe_fds[1] 90 #define BROADFD broad_domain->dom_pipe_fds[1] 91 92 extern bool_t xdr_domainname(), xdr_ypbind_resp(); 93 extern bool_t xdr_ypreq_key(), xdr_ypresp_val(); 94 extern bool_t xdr_ypbind_setdom(); 95 96 void checkwork(void); 97 void *ypbindproc_null_2_yp(SVCXPRT *, void *, CLIENT *); 98 void *ypbindproc_setdom_2_yp(SVCXPRT *, struct ypbind_setdom *, CLIENT *); 99 void rpc_received(char *, struct sockaddr_in *, int); 100 void broadcast(struct _dom_binding *); 101 int ping(struct _dom_binding *); 102 int tell_parent(char *, struct sockaddr_in *); 103 void handle_children(struct _dom_binding *); 104 void reaper(int); 105 void terminate(int); 106 void yp_restricted_mode(char *); 107 int verify(struct in_addr); 108 109 char *domain_name; 110 struct _dom_binding *ypbindlist; 111 static struct _dom_binding *broad_domain; 112 113 #define YPSET_NO 0 114 #define YPSET_LOCAL 1 115 #define YPSET_ALL 2 116 int ypsetmode = YPSET_NO; 117 int ypsecuremode = 0; 118 int ppid; 119 120 /* 121 * Special restricted mode variables: when in restricted mode, only the 122 * specified restricted_domain will be bound, and only the servers listed 123 * in restricted_addrs will be used for binding. 124 */ 125 #define RESTRICTED_SERVERS 10 126 int yp_restricted = 0; 127 int yp_manycast = 0; 128 struct in_addr restricted_addrs[RESTRICTED_SERVERS]; 129 130 /* No more than MAX_CHILDREN child broadcasters at a time. */ 131 #ifndef MAX_CHILDREN 132 #define MAX_CHILDREN 5 133 #endif 134 /* No more than MAX_DOMAINS simultaneous domains */ 135 #ifndef MAX_DOMAINS 136 #define MAX_DOMAINS 200 137 #endif 138 /* RPC timeout value */ 139 #ifndef FAIL_THRESHOLD 140 #define FAIL_THRESHOLD 20 141 #endif 142 143 /* Number of times to fish for a response froma particular set of hosts */ 144 #ifndef MAX_RETRIES 145 #define MAX_RETRIES 30 146 #endif 147 148 int retries = 0; 149 int children = 0; 150 int domains = 0; 151 int yplockfd; 152 fd_set fdsr; 153 154 SVCXPRT *udptransp, *tcptransp; 155 156 void * 157 ypbindproc_null_2_yp(SVCXPRT *transp, void *argp, CLIENT *clnt) 158 { 159 static char res; 160 161 bzero(&res, sizeof(res)); 162 return &res; 163 } 164 165 struct ypbind_resp * 166 ypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt) 167 { 168 static struct ypbind_resp res; 169 struct _dom_binding *ypdb; 170 char path[MAXPATHLEN]; 171 172 bzero(&res, sizeof res); 173 res.ypbind_status = YPBIND_FAIL_VAL; 174 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV; 175 176 if (strchr(*argp, '/')) { 177 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \ 178 rejecting.", *argp); 179 return(&res); 180 } 181 182 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) { 183 if (strcmp(ypdb->dom_domain, *argp) == 0) 184 break; 185 } 186 187 if (ypdb == NULL) { 188 if (yp_restricted) { 189 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp); 190 return (&res); 191 } 192 193 if (domains >= MAX_DOMAINS) { 194 syslog(LOG_WARNING, "domain limit (%d) exceeded", 195 MAX_DOMAINS); 196 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC; 197 return (&res); 198 } 199 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb); 200 if (ypdb == NULL) { 201 syslog(LOG_WARNING, "malloc: %m"); 202 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC; 203 return (&res); 204 } 205 bzero(ypdb, sizeof *ypdb); 206 strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain); 207 ypdb->dom_vers = YPVERS; 208 ypdb->dom_alive = 0; 209 ypdb->dom_default = 0; 210 ypdb->dom_lockfd = -1; 211 sprintf(path, "%s/%s.%ld", BINDINGDIR, 212 ypdb->dom_domain, ypdb->dom_vers); 213 unlink(path); 214 ypdb->dom_pnext = ypbindlist; 215 ypbindlist = ypdb; 216 domains++; 217 } 218 219 if (ping(ypdb)) { 220 return (&res); 221 } 222 223 res.ypbind_status = YPBIND_SUCC_VAL; 224 res.ypbind_resp_u.ypbind_error = 0; /* Success */ 225 *(u_int32_t *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = 226 ypdb->dom_server_addr.sin_addr.s_addr; 227 *(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = 228 ypdb->dom_server_addr.sin_port; 229 /*printf("domain %s at %s/%d\n", ypdb->dom_domain, 230 inet_ntoa(ypdb->dom_server_addr.sin_addr), 231 ntohs(ypdb->dom_server_addr.sin_port));*/ 232 return (&res); 233 } 234 235 void * 236 ypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt) 237 { 238 struct sockaddr_in *fromsin, bindsin; 239 static char *result = NULL; 240 241 if (strchr(argp->ypsetdom_domain, '/')) { 242 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \ 243 rejecting.", argp->ypsetdom_domain); 244 return(NULL); 245 } 246 fromsin = svc_getcaller(transp); 247 248 switch (ypsetmode) { 249 case YPSET_LOCAL: 250 if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { 251 svcerr_noprog(transp); 252 return(NULL); 253 } 254 break; 255 case YPSET_ALL: 256 break; 257 case YPSET_NO: 258 default: 259 svcerr_noprog(transp); 260 return(NULL); 261 } 262 263 if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) { 264 svcerr_noprog(transp); 265 return(NULL); 266 } 267 268 if (argp->ypsetdom_vers != YPVERS) { 269 svcerr_noprog(transp); 270 return(NULL); 271 } 272 273 bzero(&bindsin, sizeof bindsin); 274 bindsin.sin_family = AF_INET; 275 bindsin.sin_addr.s_addr = *(u_int32_t *)argp->ypsetdom_binding.ypbind_binding_addr; 276 bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port; 277 rpc_received(argp->ypsetdom_domain, &bindsin, 1); 278 279 return((void *) &result); 280 } 281 282 void 283 ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp) 284 { 285 union { 286 domainname ypbindproc_domain_2_arg; 287 struct ypbind_setdom ypbindproc_setdom_2_arg; 288 } argument; 289 struct authunix_parms *creds; 290 char *result; 291 bool_t (*xdr_argument)(), (*xdr_result)(); 292 char *(*local)(); 293 294 switch (rqstp->rq_proc) { 295 case YPBINDPROC_NULL: 296 xdr_argument = xdr_void; 297 xdr_result = xdr_void; 298 local = (char *(*)()) ypbindproc_null_2_yp; 299 break; 300 301 case YPBINDPROC_DOMAIN: 302 xdr_argument = xdr_domainname; 303 xdr_result = xdr_ypbind_resp; 304 local = (char *(*)()) ypbindproc_domain_2_yp; 305 break; 306 307 case YPBINDPROC_SETDOM: 308 switch (rqstp->rq_cred.oa_flavor) { 309 case AUTH_UNIX: 310 creds = (struct authunix_parms *)rqstp->rq_clntcred; 311 if (creds->aup_uid != 0) { 312 svcerr_auth(transp, AUTH_BADCRED); 313 return; 314 } 315 break; 316 default: 317 svcerr_auth(transp, AUTH_TOOWEAK); 318 return; 319 } 320 321 xdr_argument = xdr_ypbind_setdom; 322 xdr_result = xdr_void; 323 local = (char *(*)()) ypbindproc_setdom_2_yp; 324 break; 325 326 default: 327 svcerr_noproc(transp); 328 return; 329 } 330 bzero(&argument, sizeof(argument)); 331 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) { 332 svcerr_decode(transp); 333 return; 334 } 335 result = (*local)(transp, &argument, rqstp); 336 if (result != NULL && 337 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) { 338 svcerr_systemerr(transp); 339 } 340 return; 341 } 342 343 /* Jack the reaper */ 344 void 345 reaper(int sig) 346 { 347 int st; 348 349 while (wait3(&st, WNOHANG, NULL) > 0) 350 children--; 351 } 352 353 void 354 terminate(int sig) 355 { 356 struct _dom_binding *ypdb; 357 char path[MAXPATHLEN]; 358 359 if (ppid != getpid()) 360 exit(0); 361 362 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) { 363 close(ypdb->dom_lockfd); 364 if (ypdb->dom_broadcast_pid) 365 kill(ypdb->dom_broadcast_pid, SIGINT); 366 sprintf(path, "%s/%s.%ld", BINDINGDIR, 367 ypdb->dom_domain, ypdb->dom_vers); 368 unlink(path); 369 } 370 close(yplockfd); 371 unlink(YPBINDLOCK); 372 pmap_unset(YPBINDPROG, YPBINDVERS); 373 exit(0); 374 } 375 376 int 377 main(int argc, char *argv[]) 378 { 379 struct timeval tv; 380 int i; 381 DIR *dird; 382 struct dirent *dirp; 383 struct _dom_binding *ypdb, *next; 384 385 /* Check that another ypbind isn't already running. */ 386 if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) 387 err(1, "%s", YPBINDLOCK); 388 389 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) 390 errx(1, "another ypbind is already running. Aborting"); 391 392 /* XXX domainname will be overriden if we use restricted mode */ 393 yp_get_default_domain(&domain_name); 394 if (domain_name[0] == '\0') 395 errx(1, "domainname not set. Aborting"); 396 397 for (i = 1; i<argc; i++) { 398 if (strcmp("-ypset", argv[i]) == 0) 399 ypsetmode = YPSET_ALL; 400 else if (strcmp("-ypsetme", argv[i]) == 0) 401 ypsetmode = YPSET_LOCAL; 402 else if (strcmp("-s", argv[i]) == 0) 403 ypsecuremode++; 404 else if (strcmp("-S", argv[i]) == 0 && argc > i) 405 yp_restricted_mode(argv[++i]); 406 else if (strcmp("-m", argv[i]) == 0) 407 yp_manycast++; 408 else 409 errx(1, "unknown option: %s", argv[i]); 410 } 411 412 /* blow away everything in BINDINGDIR (if it exists) */ 413 414 if ((dird = opendir(BINDINGDIR)) != NULL) { 415 char path[MAXPATHLEN]; 416 while ((dirp = readdir(dird)) != NULL) 417 if (strcmp(dirp->d_name, ".") && 418 strcmp(dirp->d_name, "..")) { 419 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name); 420 unlink(path); 421 } 422 closedir(dird); 423 } 424 425 #ifdef DAEMON 426 if (daemon(0,0)) 427 err(1, "fork"); 428 #endif 429 430 pmap_unset(YPBINDPROG, YPBINDVERS); 431 432 udptransp = svcudp_create(RPC_ANYSOCK); 433 if (udptransp == NULL) 434 errx(1, "cannot create udp service"); 435 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2, 436 IPPROTO_UDP)) 437 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)"); 438 439 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0); 440 if (tcptransp == NULL) 441 errx(1, "cannot create tcp service"); 442 443 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2, 444 IPPROTO_TCP)) 445 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)"); 446 447 /* build initial domain binding, make it "unsuccessful" */ 448 ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist); 449 if (ypbindlist == NULL) 450 errx(1, "malloc"); 451 bzero(ypbindlist, sizeof *ypbindlist); 452 strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain); 453 ypbindlist->dom_vers = YPVERS; 454 ypbindlist->dom_alive = 0; 455 ypbindlist->dom_lockfd = -1; 456 ypbindlist->dom_default = 1; 457 domains++; 458 459 signal(SIGCHLD, reaper); 460 signal(SIGTERM, terminate); 461 462 ppid = getpid(); /* Remember who we are. */ 463 464 openlog(argv[0], LOG_PID, LOG_DAEMON); 465 466 /* Kick off the default domain */ 467 broadcast(ypbindlist); 468 469 while (1) { 470 fdsr = svc_fdset; 471 472 tv.tv_sec = 60; 473 tv.tv_usec = 0; 474 475 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) { 476 case 0: 477 checkwork(); 478 break; 479 case -1: 480 if (errno != EINTR) 481 syslog(LOG_WARNING, "select: %m"); 482 break; 483 default: 484 for (ypdb = ypbindlist; ypdb; ypdb = next) { 485 next = ypdb->dom_pnext; 486 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) { 487 handle_children(ypdb); 488 if (children == (MAX_CHILDREN - 1)) 489 checkwork(); 490 } 491 } 492 svc_getreqset(&fdsr); 493 break; 494 } 495 } 496 497 /* NOTREACHED */ 498 exit(1); 499 } 500 501 void 502 checkwork(void) 503 { 504 struct _dom_binding *ypdb; 505 506 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) 507 ping(ypdb); 508 } 509 510 /* The clnt_broadcast() callback mechanism sucks. */ 511 512 /* 513 * Receive results from broadcaster. Don't worry about passing 514 * bogus info to rpc_received() -- it can handle it. Note that we 515 * must be sure to invalidate the dom_pipe_fds descriptors here: 516 * since descriptors can be re-used, we have to make sure we 517 * don't mistake one of the RPC descriptors for one of the pipes. 518 * What's weird is that forgetting to invalidate the pipe descriptors 519 * doesn't always result in an error (otherwise I would have caught 520 * the mistake much sooner), even though logically it should. 521 */ 522 void 523 handle_children(struct _dom_binding *ypdb) 524 { 525 char buf[YPMAXDOMAIN + 1]; 526 struct sockaddr_in addr; 527 int d = 0, a = 0; 528 struct _dom_binding *y, *prev = NULL; 529 char path[MAXPATHLEN]; 530 531 if ((d = read(READFD, &buf, sizeof(buf))) <= 0) 532 syslog(LOG_WARNING, "could not read from child: %m"); 533 534 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0) 535 syslog(LOG_WARNING, "could not read from child: %m"); 536 537 close(READFD); 538 FD_CLR(READFD, &fdsr); 539 FD_CLR(READFD, &svc_fdset); 540 READFD = WRITEFD = -1; 541 if (d > 0 && a > 0) 542 rpc_received(buf, &addr, 0); 543 else { 544 for (y = ypbindlist; y; y = y->dom_pnext) { 545 if (y == ypdb) 546 break; 547 prev = y; 548 } 549 switch (ypdb->dom_default) { 550 case 0: 551 if (prev == NULL) 552 ypbindlist = y->dom_pnext; 553 else 554 prev->dom_pnext = y->dom_pnext; 555 sprintf(path, "%s/%s.%ld", BINDINGDIR, 556 ypdb->dom_domain, YPVERS); 557 close(ypdb->dom_lockfd); 558 unlink(path); 559 free(ypdb); 560 domains--; 561 return; 562 case 1: 563 ypdb->dom_broadcast_pid = 0; 564 ypdb->dom_alive = 0; 565 broadcast(ypdb); 566 return; 567 default: 568 break; 569 } 570 } 571 572 return; 573 } 574 575 /* 576 * Send our dying words back to our parent before we perish. 577 */ 578 int 579 tell_parent(char *dom, struct sockaddr_in *addr) 580 { 581 char buf[YPMAXDOMAIN + 1]; 582 struct timeval timeout; 583 fd_set fds; 584 585 timeout.tv_sec = 5; 586 timeout.tv_usec = 0; 587 588 sprintf(buf, "%s", broad_domain->dom_domain); 589 if (write(BROADFD, &buf, sizeof(buf)) < 0) 590 return(1); 591 592 /* 593 * Stay in sync with parent: wait for it to read our first 594 * message before sending the second. 595 */ 596 597 FD_ZERO(&fds); 598 FD_SET(BROADFD, &fds); 599 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1) 600 return(1); 601 if (FD_ISSET(BROADFD, &fds)) { 602 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0) 603 return(1); 604 } else { 605 return(1); 606 } 607 608 close(BROADFD); 609 return (0); 610 } 611 612 bool_t broadcast_result(out, addr) 613 bool_t *out; 614 struct sockaddr_in *addr; 615 { 616 if (retries >= MAX_RETRIES) { 617 bzero(addr, sizeof(struct sockaddr_in)); 618 if (tell_parent(broad_domain->dom_domain, addr)) 619 syslog(LOG_WARNING, "lost connection to parent"); 620 return (TRUE); 621 } 622 623 if (yp_restricted && verify(addr->sin_addr)) { 624 retries++; 625 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr)); 626 return (FALSE); 627 } else { 628 if (tell_parent(broad_domain->dom_domain, addr)) 629 syslog(LOG_WARNING, "lost connection to parent"); 630 return (TRUE); 631 } 632 } 633 634 /* 635 * The right way to send RPC broadcasts. 636 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast() 637 * blocks while waiting for replies, so we have to fork off separate 638 * broadcaster processes that do the waiting and then transmit their 639 * results back to the parent for processing. We also have to remember 640 * to save the name of the domain we're trying to bind in a global 641 * variable since clnt_broadcast() provides no way to pass things to 642 * the 'eachresult' callback function. 643 */ 644 void 645 broadcast(struct _dom_binding *ypdb) 646 { 647 bool_t out = FALSE; 648 enum clnt_stat stat; 649 650 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid) 651 return; 652 653 if (pipe(ypdb->dom_pipe_fds) < 0) { 654 syslog(LOG_WARNING, "pipe: %m"); 655 return; 656 } 657 658 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr) 659 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding", 660 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain); 661 662 broad_domain = ypdb; 663 flock(ypdb->dom_lockfd, LOCK_UN); 664 665 switch ((ypdb->dom_broadcast_pid = fork())) { 666 case 0: 667 close(READFD); 668 signal(SIGCHLD, SIG_DFL); 669 signal(SIGTERM, SIG_DFL); 670 break; 671 case -1: 672 syslog(LOG_WARNING, "fork: %m"); 673 close(READFD); 674 close(WRITEFD); 675 return; 676 default: 677 close(WRITEFD); 678 FD_SET(READFD, &svc_fdset); 679 children++; 680 return; 681 } 682 683 /* Release all locks before doing anything else. */ 684 while (ypbindlist) { 685 close(ypbindlist->dom_lockfd); 686 ypbindlist = ypbindlist->dom_pnext; 687 } 688 close(yplockfd); 689 690 /* 691 * Special 'many-cast' behavior. If we're in restricted mode, 692 * we have a list of possible server addresses to try. What 693 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK 694 * procedure and time the replies. Whoever replies fastest 695 * gets to be our server. Note that this is not a broadcast 696 * operation: we transmit uni-cast datagrams only. 697 */ 698 if (yp_restricted && yp_manycast) { 699 short port; 700 int i; 701 struct sockaddr_in sin; 702 703 i = __yp_ping(restricted_addrs, yp_restricted, 704 ypdb->dom_domain, &port); 705 if (i == -1) { 706 bzero(&ypdb->dom_server_addr, 707 sizeof(struct sockaddr_in)); 708 if (tell_parent(ypdb->dom_domain, 709 &ypdb->dom_server_addr)) 710 syslog(LOG_WARNING, "lost connection to parent"); 711 } else { 712 bzero(&sin, sizeof(struct sockaddr_in)); 713 bcopy(&restricted_addrs[i], 714 &sin.sin_addr, sizeof(struct in_addr)); 715 sin.sin_family = AF_INET; 716 sin.sin_port = port; 717 if (tell_parent(broad_domain->dom_domain, &sin)) 718 syslog(LOG_WARNING, 719 "lost connection to parent"); 720 } 721 _exit(0); 722 } 723 724 retries = 0; 725 726 { 727 char *ptr; 728 729 ptr = ypdb->dom_domain; 730 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK, 731 (xdrproc_t)xdr_domainname, &ptr, 732 (xdrproc_t)xdr_bool, &out, 733 (resultproc_t)broadcast_result); 734 } 735 736 if (stat != RPC_SUCCESS) { 737 bzero(&ypdb->dom_server_addr, 738 sizeof(struct sockaddr_in)); 739 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr)) 740 syslog(LOG_WARNING, "lost connection to parent"); 741 } 742 743 _exit(0); 744 } 745 746 /* 747 * The right way to check if a server is alive. 748 * Attempt to get a client handle pointing to the server and send a 749 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE, 750 * we invalidate this binding entry and send out a broadcast to try to 751 * establish a new binding. Note that we treat non-default domains 752 * specially: once bound, we keep tabs on our server, but if it 753 * goes away and fails to respond after one round of broadcasting, we 754 * abandon it until a client specifically references it again. We make 755 * every effort to keep our default domain bound, however, since we 756 * need it to keep the system on its feet. 757 */ 758 int 759 ping(struct _dom_binding *ypdb) 760 { 761 bool_t out; 762 struct timeval interval, timeout; 763 enum clnt_stat stat; 764 int rpcsock = RPC_ANYSOCK; 765 CLIENT *client_handle; 766 767 interval.tv_sec = FAIL_THRESHOLD; 768 interval.tv_usec = 0; 769 timeout.tv_sec = FAIL_THRESHOLD; 770 timeout.tv_usec = 0; 771 772 if (ypdb->dom_broadcast_pid) 773 return(1); 774 775 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr, 776 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE, 777 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) { 778 /* Can't get a handle: we're dead. */ 779 ypdb->dom_alive = 0; 780 ypdb->dom_vers = -1; 781 broadcast(ypdb); 782 return(1); 783 } 784 785 { 786 char *ptr; 787 788 ptr = ypdb->dom_domain; 789 790 stat = clnt_call(client_handle, YPPROC_DOMAIN, 791 (xdrproc_t)xdr_domainname, &ptr, 792 (xdrproc_t)xdr_bool, &out, timeout); 793 if (stat != RPC_SUCCESS || out == FALSE) { 794 ypdb->dom_alive = 0; 795 ypdb->dom_vers = -1; 796 clnt_destroy(client_handle); 797 broadcast(ypdb); 798 return(1); 799 } 800 } 801 802 clnt_destroy(client_handle); 803 return(0); 804 } 805 806 void 807 rpc_received(char *dom, struct sockaddr_in *raddrp, int force) 808 { 809 struct _dom_binding *ypdb, *prev = NULL; 810 struct iovec iov[2]; 811 struct ypbind_resp ybr; 812 char path[MAXPATHLEN]; 813 int fd; 814 815 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr), 816 ntohs(raddrp->sin_port), dom);*/ 817 818 if (dom == NULL) 819 return; 820 821 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) { 822 if (strcmp(ypdb->dom_domain, dom) == 0) 823 break; 824 prev = ypdb; 825 } 826 827 if (ypdb && force) { 828 if (ypdb->dom_broadcast_pid) { 829 kill(ypdb->dom_broadcast_pid, SIGINT); 830 close(READFD); 831 FD_CLR(READFD, &fdsr); 832 FD_CLR(READFD, &svc_fdset); 833 READFD = WRITEFD = -1; 834 } 835 } 836 837 /* if in secure mode, check originating port number */ 838 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) { 839 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.", 840 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port), 841 dom); 842 if (ypdb != NULL) { 843 ypdb->dom_broadcast_pid = 0; 844 ypdb->dom_alive = 0; 845 } 846 return; 847 } 848 849 if (raddrp->sin_addr.s_addr == (long)0) { 850 switch (ypdb->dom_default) { 851 case 0: 852 if (prev == NULL) 853 ypbindlist = ypdb->dom_pnext; 854 else 855 prev->dom_pnext = ypdb->dom_pnext; 856 sprintf(path, "%s/%s.%ld", BINDINGDIR, 857 ypdb->dom_domain, YPVERS); 858 close(ypdb->dom_lockfd); 859 unlink(path); 860 free(ypdb); 861 domains--; 862 return; 863 case 1: 864 ypdb->dom_broadcast_pid = 0; 865 ypdb->dom_alive = 0; 866 broadcast(ypdb); 867 return; 868 default: 869 break; 870 } 871 } 872 873 if (ypdb == NULL) { 874 if (force == 0) 875 return; 876 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb); 877 if (ypdb == NULL) { 878 syslog(LOG_WARNING, "malloc: %m"); 879 return; 880 } 881 bzero(ypdb, sizeof *ypdb); 882 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain); 883 ypdb->dom_lockfd = -1; 884 ypdb->dom_default = 0; 885 ypdb->dom_pnext = ypbindlist; 886 ypbindlist = ypdb; 887 } 888 889 /* We've recovered from a crash: inform the world. */ 890 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr) 891 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK", 892 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain); 893 894 bcopy(raddrp, &ypdb->dom_server_addr, 895 sizeof ypdb->dom_server_addr); 896 897 ypdb->dom_vers = YPVERS; 898 ypdb->dom_alive = 1; 899 ypdb->dom_broadcast_pid = 0; 900 901 if (ypdb->dom_lockfd != -1) 902 close(ypdb->dom_lockfd); 903 904 sprintf(path, "%s/%s.%ld", BINDINGDIR, 905 ypdb->dom_domain, ypdb->dom_vers); 906 #ifdef O_SHLOCK 907 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) { 908 (void)mkdir(BINDINGDIR, 0755); 909 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) 910 return; 911 } 912 #else 913 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) { 914 (void)mkdir(BINDINGDIR, 0755); 915 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) 916 return; 917 } 918 flock(fd, LOCK_SH); 919 #endif 920 921 /* 922 * ok, if BINDINGDIR exists, and we can create the binding file, 923 * then write to it.. 924 */ 925 ypdb->dom_lockfd = fd; 926 927 iov[0].iov_base = (char *)&(udptransp->xp_port); 928 iov[0].iov_len = sizeof udptransp->xp_port; 929 iov[1].iov_base = (char *)&ybr; 930 iov[1].iov_len = sizeof ybr; 931 932 bzero(&ybr, sizeof ybr); 933 ybr.ypbind_status = YPBIND_SUCC_VAL; 934 *(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr; 935 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port; 936 937 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) { 938 syslog(LOG_WARNING, "write: %m"); 939 close(ypdb->dom_lockfd); 940 ypdb->dom_lockfd = -1; 941 return; 942 } 943 } 944 945 /* 946 * Check address against list of allowed servers. Return 0 if okay, 947 * 1 if not matched. 948 */ 949 int 950 verify(struct in_addr addr) 951 { 952 int i; 953 954 for (i = 0; i < RESTRICTED_SERVERS; i++) 955 if (!bcmp(&addr, &restricted_addrs[i], sizeof(struct in_addr))) 956 return(0); 957 958 return(1); 959 } 960 961 /* 962 * Try to set restricted mode. We default to normal mode if we can't 963 * resolve the specified hostnames. 964 */ 965 void 966 yp_restricted_mode(char *args) 967 { 968 struct hostent *h; 969 int i = 0; 970 char *s; 971 972 /* Find the restricted domain. */ 973 if ((s = strsep(&args, ",")) == NULL) 974 return; 975 domain_name = s; 976 977 /* Get the addresses of the servers. */ 978 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) { 979 if ((h = gethostbyname(s)) == NULL) 980 return; 981 bcopy (h->h_addr_list[0], &restricted_addrs[i], 982 sizeof(struct in_addr)); 983 i++; 984 } 985 986 /* ypset and ypsetme not allowed with restricted mode */ 987 ypsetmode = YPSET_NO; 988 989 yp_restricted = i; 990 return; 991 } 992