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 const char rcsid[] = 32 "$Id: ypbind.c,v 1.26 1997/10/27 07:45:47 charnier Exp $"; 33 #endif /* not lint */ 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 #include <sys/wait.h> 38 #include <sys/ioctl.h> 39 #include <sys/signal.h> 40 #include <sys/socket.h> 41 #include <sys/file.h> 42 #include <sys/fcntl.h> 43 #include <sys/stat.h> 44 #include <sys/uio.h> 45 #include <ctype.h> 46 #include <dirent.h> 47 #include <err.h> 48 #include <errno.h> 49 #include <netdb.h> 50 #include <signal.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <syslog.h> 55 #include <unistd.h> 56 #include <rpc/rpc.h> 57 #include <rpc/xdr.h> 58 #include <net/if.h> 59 #include <netinet/in.h> 60 #include <arpa/inet.h> 61 #include <rpc/pmap_clnt.h> 62 #include <rpc/pmap_prot.h> 63 #include <rpc/pmap_rmt.h> 64 #include <rpc/rpc_com.h> 65 #include <rpcsvc/yp.h> 66 struct dom_binding{}; 67 #include <rpcsvc/ypclnt.h> 68 #include "yp_ping.h" 69 70 #ifndef BINDINGDIR 71 #define BINDINGDIR "/var/yp/binding" 72 #endif 73 74 #ifndef YPBINDLOCK 75 #define YPBINDLOCK "/var/run/ypbind.lock" 76 #endif 77 78 struct _dom_binding { 79 struct _dom_binding *dom_pnext; 80 char dom_domain[YPMAXDOMAIN + 1]; 81 struct sockaddr_in dom_server_addr; 82 long int dom_vers; 83 int dom_lockfd; 84 int dom_alive; 85 int dom_broadcast_pid; 86 int dom_pipe_fds[2]; 87 int dom_default; 88 }; 89 90 #define READFD ypdb->dom_pipe_fds[0] 91 #define WRITEFD ypdb->dom_pipe_fds[1] 92 #define BROADFD broad_domain->dom_pipe_fds[1] 93 94 extern bool_t xdr_domainname(), xdr_ypbind_resp(); 95 extern bool_t xdr_ypreq_key(), xdr_ypresp_val(); 96 extern bool_t xdr_ypbind_setdom(); 97 98 void checkwork __P((void)); 99 void *ypbindproc_null_2_yp __P((SVCXPRT *, void *, CLIENT *)); 100 void *ypbindproc_setdom_2_yp __P((SVCXPRT *, struct ypbind_setdom *, CLIENT *)); 101 void rpc_received __P((char *, struct sockaddr_in *, int )); 102 void broadcast __P((struct _dom_binding *)); 103 int ping __P((struct _dom_binding *)); 104 int tell_parent __P((char *, struct sockaddr_in *)); 105 void handle_children __P(( struct _dom_binding * )); 106 void reaper __P((int)); 107 void terminate __P((int)); 108 void yp_restricted_mode __P((char *)); 109 int verify __P((struct in_addr)); 110 111 char *domain_name; 112 struct _dom_binding *ypbindlist; 113 static struct _dom_binding *broad_domain; 114 115 #define YPSET_NO 0 116 #define YPSET_LOCAL 1 117 #define YPSET_ALL 2 118 int ypsetmode = YPSET_NO; 119 int ypsecuremode = 0; 120 int ppid; 121 122 /* 123 * Special restricted mode variables: when in restricted mode, only the 124 * specified restricted_domain will be bound, and only the servers listed 125 * in restricted_addrs will be used for binding. 126 */ 127 #define RESTRICTED_SERVERS 10 128 int yp_restricted = 0; 129 int yp_manycast = 0; 130 struct in_addr restricted_addrs[RESTRICTED_SERVERS]; 131 132 /* No more than MAX_CHILDREN child broadcasters at a time. */ 133 #ifndef MAX_CHILDREN 134 #define MAX_CHILDREN 5 135 #endif 136 /* No more than MAX_DOMAINS simultaneous domains */ 137 #ifndef MAX_DOMAINS 138 #define MAX_DOMAINS 200 139 #endif 140 /* RPC timeout value */ 141 #ifndef FAIL_THRESHOLD 142 #define FAIL_THRESHOLD 20 143 #endif 144 145 /* Number of times to fish for a response froma particular set of hosts */ 146 #ifndef MAX_RETRIES 147 #define MAX_RETRIES 30 148 #endif 149 150 int retries = 0; 151 int children = 0; 152 int domains = 0; 153 int yplockfd; 154 fd_set fdsr; 155 156 SVCXPRT *udptransp, *tcptransp; 157 158 void * 159 ypbindproc_null_2_yp(transp, argp, clnt) 160 SVCXPRT *transp; 161 void *argp; 162 CLIENT *clnt; 163 { 164 static char res; 165 166 bzero((char *)&res, sizeof(res)); 167 return (void *)&res; 168 } 169 170 struct ypbind_resp * 171 ypbindproc_domain_2_yp(transp, argp, clnt) 172 SVCXPRT *transp; 173 domainname *argp; 174 CLIENT *clnt; 175 { 176 static struct ypbind_resp res; 177 struct _dom_binding *ypdb; 178 char path[MAXPATHLEN]; 179 180 bzero((char *)&res, sizeof res); 181 res.ypbind_status = YPBIND_FAIL_VAL; 182 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV; 183 184 if (strchr(*argp, '/')) { 185 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \ 186 rejecting.", *argp); 187 return(&res); 188 } 189 190 for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) { 191 if( strcmp(ypdb->dom_domain, *argp) == 0) 192 break; 193 } 194 195 if(ypdb==NULL) { 196 if (yp_restricted) { 197 syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp); 198 return &res; 199 } 200 201 if (domains >= MAX_DOMAINS) { 202 syslog(LOG_WARNING, "domain limit (%d) exceeded", 203 MAX_DOMAINS); 204 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC; 205 return &res; 206 } 207 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb); 208 if (ypdb == NULL) { 209 syslog(LOG_WARNING, "malloc: %m"); 210 res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC; 211 return &res; 212 } 213 bzero((char *)ypdb, sizeof *ypdb); 214 strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain); 215 ypdb->dom_vers = YPVERS; 216 ypdb->dom_alive = 0; 217 ypdb->dom_default = 0; 218 ypdb->dom_lockfd = -1; 219 sprintf(path, "%s/%s.%ld", BINDINGDIR, 220 ypdb->dom_domain, ypdb->dom_vers); 221 unlink(path); 222 ypdb->dom_pnext = ypbindlist; 223 ypbindlist = ypdb; 224 domains++; 225 } 226 227 if (ping(ypdb)) { 228 return &res; 229 } 230 231 res.ypbind_status = YPBIND_SUCC_VAL; 232 res.ypbind_resp_u.ypbind_error = 0; /* Success */ 233 *(u_long *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = 234 ypdb->dom_server_addr.sin_addr.s_addr; 235 *(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = 236 ypdb->dom_server_addr.sin_port; 237 /*printf("domain %s at %s/%d\n", ypdb->dom_domain, 238 inet_ntoa(ypdb->dom_server_addr.sin_addr), 239 ntohs(ypdb->dom_server_addr.sin_port));*/ 240 return &res; 241 } 242 243 void * 244 ypbindproc_setdom_2_yp(transp, argp, clnt) 245 SVCXPRT *transp; 246 ypbind_setdom *argp; 247 CLIENT *clnt; 248 { 249 struct sockaddr_in *fromsin, bindsin; 250 251 if (strchr(argp->ypsetdom_domain, '/')) { 252 syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \ 253 rejecting.", argp->ypsetdom_domain); 254 return(NULL); 255 } 256 fromsin = svc_getcaller(transp); 257 258 switch(ypsetmode) { 259 case YPSET_LOCAL: 260 if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { 261 svcerr_noprog(transp); 262 return(NULL); 263 } 264 break; 265 case YPSET_ALL: 266 break; 267 case YPSET_NO: 268 default: 269 svcerr_noprog(transp); 270 return(NULL); 271 } 272 273 if(ntohs(fromsin->sin_port) >= IPPORT_RESERVED) { 274 svcerr_noprog(transp); 275 return(NULL); 276 } 277 278 if(argp->ypsetdom_vers != YPVERS) { 279 svcerr_noprog(transp); 280 return(NULL); 281 } 282 283 bzero((char *)&bindsin, sizeof bindsin); 284 bindsin.sin_family = AF_INET; 285 bindsin.sin_addr.s_addr = *(u_long *)argp->ypsetdom_binding.ypbind_binding_addr; 286 bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port; 287 rpc_received(argp->ypsetdom_domain, &bindsin, 1); 288 289 return(NULL); 290 } 291 292 static void 293 ypbindprog_2(rqstp, transp) 294 struct svc_req *rqstp; 295 register SVCXPRT *transp; 296 { 297 union { 298 domainname ypbindproc_domain_2_arg; 299 struct ypbind_setdom ypbindproc_setdom_2_arg; 300 } argument; 301 struct authunix_parms *creds; 302 char *result; 303 bool_t (*xdr_argument)(), (*xdr_result)(); 304 char *(*local)(); 305 306 switch (rqstp->rq_proc) { 307 case YPBINDPROC_NULL: 308 xdr_argument = xdr_void; 309 xdr_result = xdr_void; 310 local = (char *(*)()) ypbindproc_null_2_yp; 311 break; 312 313 case YPBINDPROC_DOMAIN: 314 xdr_argument = xdr_domainname; 315 xdr_result = xdr_ypbind_resp; 316 local = (char *(*)()) ypbindproc_domain_2_yp; 317 break; 318 319 case YPBINDPROC_SETDOM: 320 switch(rqstp->rq_cred.oa_flavor) { 321 case AUTH_UNIX: 322 creds = (struct authunix_parms *)rqstp->rq_clntcred; 323 if( creds->aup_uid != 0) { 324 svcerr_auth(transp, AUTH_BADCRED); 325 return; 326 } 327 break; 328 default: 329 svcerr_auth(transp, AUTH_TOOWEAK); 330 return; 331 } 332 333 xdr_argument = xdr_ypbind_setdom; 334 xdr_result = xdr_void; 335 local = (char *(*)()) ypbindproc_setdom_2_yp; 336 break; 337 338 default: 339 svcerr_noproc(transp); 340 return; 341 } 342 bzero((char *)&argument, sizeof(argument)); 343 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) { 344 svcerr_decode(transp); 345 return; 346 } 347 result = (*local)(transp, &argument, rqstp); 348 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) { 349 svcerr_systemerr(transp); 350 } 351 return; 352 } 353 354 /* Jack the reaper */ 355 void reaper(sig) 356 int sig; 357 { 358 int st; 359 360 while(wait3(&st, WNOHANG, NULL) > 0) 361 children--; 362 } 363 364 void terminate(sig) 365 int sig; 366 { 367 struct _dom_binding *ypdb; 368 char path[MAXPATHLEN]; 369 370 if (ppid != getpid()) 371 exit(0); 372 373 for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) { 374 close(ypdb->dom_lockfd); 375 if (ypdb->dom_broadcast_pid) 376 kill(ypdb->dom_broadcast_pid, SIGINT); 377 sprintf(path, "%s/%s.%ld", BINDINGDIR, 378 ypdb->dom_domain, ypdb->dom_vers); 379 unlink(path); 380 } 381 close(yplockfd); 382 unlink(YPBINDLOCK); 383 pmap_unset(YPBINDPROG, YPBINDVERS); 384 exit(0); 385 } 386 387 int 388 main(argc, argv) 389 int argc; 390 char **argv; 391 { 392 struct timeval tv; 393 int i; 394 DIR *dird; 395 struct dirent *dirp; 396 struct _dom_binding *ypdb; 397 398 /* Check that another ypbind isn't already running. */ 399 if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) 400 err(1, "%s", YPBINDLOCK); 401 402 if(flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) 403 errx(1, "another ypbind is already running. Aborting"); 404 405 /* XXX domainname will be overriden if we use restricted mode */ 406 yp_get_default_domain(&domain_name); 407 if( domain_name[0] == '\0') 408 errx(1, "domainname not set. Aborting"); 409 410 for(i=1; i<argc; i++) { 411 if( strcmp("-ypset", argv[i]) == 0) 412 ypsetmode = YPSET_ALL; 413 else if (strcmp("-ypsetme", argv[i]) == 0) 414 ypsetmode = YPSET_LOCAL; 415 else if (strcmp("-s", argv[i]) == 0) 416 ypsecuremode++; 417 else if (strcmp("-S", argv[i]) == 0 && argc > i) 418 yp_restricted_mode(argv[i+1]); 419 else if (strcmp("-m", argv[i]) == 0) 420 yp_manycast++; 421 } 422 423 /* blow away everything in BINDINGDIR (if it exists) */ 424 425 if ((dird = opendir(BINDINGDIR)) != NULL) { 426 char path[MAXPATHLEN]; 427 while ((dirp = readdir(dird)) != NULL) 428 if (strcmp(dirp->d_name, ".") && 429 strcmp(dirp->d_name, "..")) { 430 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name); 431 unlink(path); 432 } 433 closedir(dird); 434 } 435 436 #ifdef DAEMON 437 if (daemon(0,0)) 438 err(1, "fork"); 439 #endif 440 441 pmap_unset(YPBINDPROG, YPBINDVERS); 442 443 udptransp = svcudp_create(RPC_ANYSOCK); 444 if (udptransp == NULL) 445 errx(1, "cannot create udp service"); 446 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2, 447 IPPROTO_UDP)) 448 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)"); 449 450 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0); 451 if (tcptransp == NULL) 452 errx(1, "cannot create tcp service"); 453 454 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2, 455 IPPROTO_TCP)) 456 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)"); 457 458 /* build initial domain binding, make it "unsuccessful" */ 459 ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist); 460 if (ypbindlist == NULL) 461 errx(1, "malloc"); 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 /* 704 * Special 'many-cast' behavior. If we're in restricted mode, 705 * we have a list of possible server addresses to try. What 706 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK 707 * procedure and time the replies. Whoever replies fastest 708 * gets to be our server. Note that this is not a broadcast 709 * operation: we transmit uni-cast datagrams only. 710 */ 711 if (yp_restricted && yp_manycast) { 712 short port; 713 int i; 714 struct sockaddr_in sin; 715 716 i = __yp_ping(restricted_addrs, yp_restricted, 717 ypdb->dom_domain, &port); 718 if (i == -1) { 719 bzero((char *)&ypdb->dom_server_addr, 720 sizeof(struct sockaddr_in)); 721 if (tell_parent(ypdb->dom_domain, 722 &ypdb->dom_server_addr)) 723 syslog(LOG_WARNING, "lost connection to parent"); 724 } else { 725 bzero((char *)&sin, sizeof(struct sockaddr_in)); 726 bcopy((char *)&restricted_addrs[i], 727 (char *)&sin.sin_addr, sizeof(struct in_addr)); 728 sin.sin_family = AF_INET; 729 sin.sin_port = port; 730 if (tell_parent(broad_domain->dom_domain, &sin)) 731 syslog(LOG_WARNING, 732 "lost connection to parent"); 733 } 734 _exit(0); 735 } 736 737 retries = 0; 738 739 { 740 char *ptr; 741 742 ptr = (char *)&ypdb->dom_domain; 743 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK, 744 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out, 745 broadcast_result); 746 } 747 748 if (stat != RPC_SUCCESS) { 749 bzero((char *)&ypdb->dom_server_addr, 750 sizeof(struct sockaddr_in)); 751 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr)) 752 syslog(LOG_WARNING, "lost connection to parent"); 753 } 754 755 _exit(0); 756 } 757 758 /* 759 * The right way to check if a server is alive. 760 * Attempt to get a client handle pointing to the server and send a 761 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE, 762 * we invalidate this binding entry and send out a broadcast to try to 763 * establish a new binding. Note that we treat non-default domains 764 * specially: once bound, we keep tabs on our server, but if it 765 * goes away and fails to respond after one round of broadcasting, we 766 * abandon it until a client specifically references it again. We make 767 * every effort to keep our default domain bound, however, since we 768 * need it to keep the system on its feet. 769 */ 770 int 771 ping(ypdb) 772 struct _dom_binding *ypdb; 773 { 774 bool_t out; 775 struct timeval interval, timeout; 776 enum clnt_stat stat; 777 int rpcsock = RPC_ANYSOCK; 778 CLIENT *client_handle; 779 780 interval.tv_sec = FAIL_THRESHOLD; 781 interval.tv_usec = 0; 782 timeout.tv_sec = FAIL_THRESHOLD; 783 timeout.tv_usec = 0; 784 785 if (ypdb->dom_broadcast_pid) 786 return(1); 787 788 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr, 789 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE, 790 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) { 791 /* Can't get a handle: we're dead. */ 792 ypdb->dom_alive = 0; 793 ypdb->dom_vers = -1; 794 broadcast(ypdb); 795 return(1); 796 } 797 798 { 799 char *ptr; 800 801 ptr = (char *)&ypdb->dom_domain; 802 803 if ((stat = clnt_call(client_handle, YPPROC_DOMAIN, 804 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out, 805 timeout)) != RPC_SUCCESS || out == FALSE) { 806 ypdb->dom_alive = 0; 807 ypdb->dom_vers = -1; 808 clnt_destroy(client_handle); 809 broadcast(ypdb); 810 return(1); 811 } 812 } 813 814 clnt_destroy(client_handle); 815 return(0); 816 } 817 818 void rpc_received(dom, raddrp, force) 819 char *dom; 820 struct sockaddr_in *raddrp; 821 int force; 822 { 823 struct _dom_binding *ypdb, *prev = NULL; 824 struct iovec iov[2]; 825 struct ypbind_resp ybr; 826 char path[MAXPATHLEN]; 827 int fd; 828 829 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr), 830 ntohs(raddrp->sin_port), dom);*/ 831 832 if(dom==NULL) 833 return; 834 835 for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) { 836 if( strcmp(ypdb->dom_domain, dom) == 0) 837 break; 838 prev = ypdb; 839 } 840 841 if (ypdb && force) { 842 if (ypdb->dom_broadcast_pid) { 843 kill(ypdb->dom_broadcast_pid, SIGINT); 844 close(READFD); 845 FD_CLR(READFD, &fdsr); 846 FD_CLR(READFD, &svc_fdset); 847 READFD = WRITEFD = -1; 848 } 849 } 850 851 /* if in secure mode, check originating port number */ 852 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) { 853 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.", 854 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port), 855 dom); 856 if (ypdb != NULL) { 857 ypdb->dom_broadcast_pid = 0; 858 ypdb->dom_alive = 0; 859 } 860 return; 861 } 862 863 if (raddrp->sin_addr.s_addr == (long)0) { 864 switch(ypdb->dom_default) { 865 case 0: 866 if (prev == NULL) 867 ypbindlist = ypdb->dom_pnext; 868 else 869 prev->dom_pnext = ypdb->dom_pnext; 870 sprintf(path, "%s/%s.%ld", BINDINGDIR, 871 ypdb->dom_domain, YPVERS); 872 close(ypdb->dom_lockfd); 873 unlink(path); 874 free(ypdb); 875 domains--; 876 return; 877 case 1: 878 ypdb->dom_broadcast_pid = 0; 879 ypdb->dom_alive = 0; 880 broadcast(ypdb); 881 return; 882 default: 883 break; 884 } 885 } 886 887 if(ypdb==NULL) { 888 if (force == 0) 889 return; 890 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb); 891 if (ypdb == NULL) { 892 syslog(LOG_WARNING, "malloc: %m"); 893 return; 894 } 895 bzero((char *)ypdb, sizeof *ypdb); 896 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain); 897 ypdb->dom_lockfd = -1; 898 ypdb->dom_default = 0; 899 ypdb->dom_pnext = ypbindlist; 900 ypbindlist = ypdb; 901 } 902 903 /* We've recovered from a crash: inform the world. */ 904 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr) 905 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK", 906 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain); 907 908 bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr, 909 sizeof ypdb->dom_server_addr); 910 911 ypdb->dom_vers = YPVERS; 912 ypdb->dom_alive = 1; 913 ypdb->dom_broadcast_pid = 0; 914 915 if(ypdb->dom_lockfd != -1) 916 close(ypdb->dom_lockfd); 917 918 sprintf(path, "%s/%s.%ld", BINDINGDIR, 919 ypdb->dom_domain, ypdb->dom_vers); 920 #ifdef O_SHLOCK 921 if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) { 922 (void)mkdir(BINDINGDIR, 0755); 923 if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) 924 return; 925 } 926 #else 927 if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) { 928 (void)mkdir(BINDINGDIR, 0755); 929 if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) 930 return; 931 } 932 flock(fd, LOCK_SH); 933 #endif 934 935 /* 936 * ok, if BINDINGDIR exists, and we can create the binding file, 937 * then write to it.. 938 */ 939 ypdb->dom_lockfd = fd; 940 941 iov[0].iov_base = (caddr_t)&(udptransp->xp_port); 942 iov[0].iov_len = sizeof udptransp->xp_port; 943 iov[1].iov_base = (caddr_t)&ybr; 944 iov[1].iov_len = sizeof ybr; 945 946 bzero(&ybr, sizeof ybr); 947 ybr.ypbind_status = YPBIND_SUCC_VAL; 948 *(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr; 949 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port; 950 951 if( writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) { 952 syslog(LOG_WARNING, "write: %m"); 953 close(ypdb->dom_lockfd); 954 ypdb->dom_lockfd = -1; 955 return; 956 } 957 } 958 959 /* 960 * Check address against list of allowed servers. Return 0 if okay, 961 * 1 if not matched. 962 */ 963 int 964 verify(addr) 965 struct in_addr addr; 966 { 967 int i; 968 969 for (i = 0; i < RESTRICTED_SERVERS; i++) 970 if (!bcmp((char *)&addr, (char *)&restricted_addrs[i], 971 sizeof(struct in_addr))) 972 return(0); 973 974 return(1); 975 } 976 977 /* 978 * Try to set restricted mode. We default to normal mode if we can't 979 * resolve the specified hostnames. 980 */ 981 void 982 yp_restricted_mode(args) 983 char *args; 984 { 985 struct hostent *h; 986 int i = 0; 987 char *s; 988 989 /* Find the restricted domain. */ 990 if ((s = strsep(&args, ",")) == NULL) 991 return; 992 domain_name = s; 993 994 /* Get the addresses of the servers. */ 995 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) { 996 if ((h = gethostbyname(s)) == NULL) 997 return; 998 bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i], 999 sizeof(struct in_addr)); 1000 i++; 1001 } 1002 1003 /* ypset and ypsetme not allowed with restricted mode */ 1004 ypsetmode = YPSET_NO; 1005 1006 yp_restricted = i; 1007 return; 1008 } 1009