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