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 * $FreeBSD: src/usr.sbin/ypbind/ypbind.c,v 1.30.2.2 2002/02/15 00:46:59 des Exp $ 30 * $DragonFly: src/usr.sbin/ypbind/ypbind.c,v 1.4 2004/03/30 01:14:22 cpressey Exp $ 31 */ 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((char *)&res, sizeof(res)); 162 return (void *)&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((char *)&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((char *)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((char *)&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 static void 283 ypbindprog_2(struct svc_req *rqstp, 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((char *)&argument, sizeof(argument)); 331 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) { 332 svcerr_decode(transp); 333 return; 334 } 335 result = (*local)(transp, &argument, rqstp); 336 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) { 337 svcerr_systemerr(transp); 338 } 339 return; 340 } 341 342 /* Jack the reaper */ 343 void reaper(int sig) 344 { 345 int st; 346 347 while (wait3(&st, WNOHANG, NULL) > 0) 348 children--; 349 } 350 351 void terminate(int sig) 352 { 353 struct _dom_binding *ypdb; 354 char path[MAXPATHLEN]; 355 356 if (ppid != getpid()) 357 exit(0); 358 359 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) { 360 close(ypdb->dom_lockfd); 361 if (ypdb->dom_broadcast_pid) 362 kill(ypdb->dom_broadcast_pid, SIGINT); 363 sprintf(path, "%s/%s.%ld", BINDINGDIR, 364 ypdb->dom_domain, ypdb->dom_vers); 365 unlink(path); 366 } 367 close(yplockfd); 368 unlink(YPBINDLOCK); 369 pmap_unset(YPBINDPROG, YPBINDVERS); 370 exit(0); 371 } 372 373 int 374 main(int argc, char **argv) 375 { 376 struct timeval tv; 377 int i; 378 DIR *dird; 379 struct dirent *dirp; 380 struct _dom_binding *ypdb, *next; 381 382 /* Check that another ypbind isn't already running. */ 383 if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) 384 err(1, "%s", YPBINDLOCK); 385 386 if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) 387 errx(1, "another ypbind is already running. Aborting"); 388 389 /* XXX domainname will be overriden if we use restricted mode */ 390 yp_get_default_domain(&domain_name); 391 if (domain_name[0] == '\0') 392 errx(1, "domainname not set. Aborting"); 393 394 for (i = 1; i<argc; i++) { 395 if (strcmp("-ypset", argv[i]) == 0) 396 ypsetmode = YPSET_ALL; 397 else if (strcmp("-ypsetme", argv[i]) == 0) 398 ypsetmode = YPSET_LOCAL; 399 else if (strcmp("-s", argv[i]) == 0) 400 ypsecuremode++; 401 else if (strcmp("-S", argv[i]) == 0 && argc > i) 402 yp_restricted_mode(argv[i+1]); 403 else if (strcmp("-m", argv[i]) == 0) 404 yp_manycast++; 405 } 406 407 /* blow away everything in BINDINGDIR (if it exists) */ 408 409 if ((dird = opendir(BINDINGDIR)) != NULL) { 410 char path[MAXPATHLEN]; 411 while ((dirp = readdir(dird)) != NULL) 412 if (strcmp(dirp->d_name, ".") && 413 strcmp(dirp->d_name, "..")) { 414 sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name); 415 unlink(path); 416 } 417 closedir(dird); 418 } 419 420 #ifdef DAEMON 421 if (daemon(0,0)) 422 err(1, "fork"); 423 #endif 424 425 pmap_unset(YPBINDPROG, YPBINDVERS); 426 427 udptransp = svcudp_create(RPC_ANYSOCK); 428 if (udptransp == NULL) 429 errx(1, "cannot create udp service"); 430 if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2, 431 IPPROTO_UDP)) 432 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)"); 433 434 tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0); 435 if (tcptransp == NULL) 436 errx(1, "cannot create tcp service"); 437 438 if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2, 439 IPPROTO_TCP)) 440 errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)"); 441 442 /* build initial domain binding, make it "unsuccessful" */ 443 ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist); 444 if (ypbindlist == NULL) 445 errx(1, "malloc"); 446 bzero((char *)ypbindlist, sizeof *ypbindlist); 447 strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain); 448 ypbindlist->dom_vers = YPVERS; 449 ypbindlist->dom_alive = 0; 450 ypbindlist->dom_lockfd = -1; 451 ypbindlist->dom_default = 1; 452 domains++; 453 454 signal(SIGCHLD, reaper); 455 signal(SIGTERM, terminate); 456 457 ppid = getpid(); /* Remember who we are. */ 458 459 openlog(argv[0], LOG_PID, LOG_DAEMON); 460 461 /* Kick off the default domain */ 462 broadcast(ypbindlist); 463 464 while (1) { 465 fdsr = svc_fdset; 466 467 tv.tv_sec = 60; 468 tv.tv_usec = 0; 469 470 switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) { 471 case 0: 472 checkwork(); 473 break; 474 case -1: 475 if (errno != EINTR) 476 syslog(LOG_WARNING, "select: %m"); 477 break; 478 default: 479 for (ypdb = ypbindlist; ypdb; ypdb = next) { 480 next = ypdb->dom_pnext; 481 if (READFD > 0 && FD_ISSET(READFD, &fdsr)) { 482 handle_children(ypdb); 483 if (children == (MAX_CHILDREN - 1)) 484 checkwork(); 485 } 486 } 487 svc_getreqset(&fdsr); 488 break; 489 } 490 } 491 492 /* NOTREACHED */ 493 exit(1); 494 } 495 496 void 497 checkwork(void) 498 { 499 struct _dom_binding *ypdb; 500 501 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) 502 ping(ypdb); 503 } 504 505 /* The clnt_broadcast() callback mechanism sucks. */ 506 507 /* 508 * Receive results from broadcaster. Don't worry about passing 509 * bogus info to rpc_received() -- it can handle it. Note that we 510 * must be sure to invalidate the dom_pipe_fds descriptors here: 511 * since descriptors can be re-used, we have to make sure we 512 * don't mistake one of the RPC descriptors for one of the pipes. 513 * What's weird is that forgetting to invalidate the pipe descriptors 514 * doesn't always result in an error (otherwise I would have caught 515 * the mistake much sooner), even though logically it should. 516 */ 517 void handle_children(struct _dom_binding *ypdb) 518 { 519 char buf[YPMAXDOMAIN + 1]; 520 struct sockaddr_in addr; 521 int d = 0, a = 0; 522 struct _dom_binding *y, *prev = NULL; 523 char path[MAXPATHLEN]; 524 525 if ((d = read(READFD, &buf, sizeof(buf))) <= 0) 526 syslog(LOG_WARNING, "could not read from child: %m"); 527 528 if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0) 529 syslog(LOG_WARNING, "could not read from child: %m"); 530 531 close(READFD); 532 FD_CLR(READFD, &fdsr); 533 FD_CLR(READFD, &svc_fdset); 534 READFD = WRITEFD = -1; 535 if (d > 0 && a > 0) 536 rpc_received((char *)&buf, &addr, 0); 537 else { 538 for (y = ypbindlist; y; y = y->dom_pnext) { 539 if (y == ypdb) 540 break; 541 prev = y; 542 } 543 switch (ypdb->dom_default) { 544 case 0: 545 if (prev == NULL) 546 ypbindlist = y->dom_pnext; 547 else 548 prev->dom_pnext = y->dom_pnext; 549 sprintf(path, "%s/%s.%ld", BINDINGDIR, 550 ypdb->dom_domain, YPVERS); 551 close(ypdb->dom_lockfd); 552 unlink(path); 553 free(ypdb); 554 domains--; 555 return; 556 case 1: 557 ypdb->dom_broadcast_pid = 0; 558 ypdb->dom_alive = 0; 559 broadcast(ypdb); 560 return; 561 default: 562 break; 563 } 564 } 565 566 return; 567 } 568 569 /* 570 * Send our dying words back to our parent before we perish. 571 */ 572 int 573 tell_parent(char *dom, struct sockaddr_in *addr) 574 { 575 char buf[YPMAXDOMAIN + 1]; 576 struct timeval timeout; 577 fd_set fds; 578 579 timeout.tv_sec = 5; 580 timeout.tv_usec = 0; 581 582 sprintf(buf, "%s", broad_domain->dom_domain); 583 if (write(BROADFD, &buf, sizeof(buf)) < 0) 584 return(1); 585 586 /* 587 * Stay in sync with parent: wait for it to read our first 588 * message before sending the second. 589 */ 590 591 FD_ZERO(&fds); 592 FD_SET(BROADFD, &fds); 593 if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1) 594 return(1); 595 if (FD_ISSET(BROADFD, &fds)) { 596 if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0) 597 return(1); 598 } else { 599 return(1); 600 } 601 602 close(BROADFD); 603 return (0); 604 } 605 606 bool_t broadcast_result(caddr_t out, struct sockaddr_in *addr) 607 { 608 if (retries >= MAX_RETRIES) { 609 bzero((char *)addr, sizeof(struct sockaddr_in)); 610 if (tell_parent(broad_domain->dom_domain, addr)) 611 syslog(LOG_WARNING, "lost connection to parent"); 612 return (TRUE); 613 } 614 615 if (yp_restricted && verify(addr->sin_addr)) { 616 retries++; 617 syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr)); 618 return (FALSE); 619 } else { 620 if (tell_parent(broad_domain->dom_domain, addr)) 621 syslog(LOG_WARNING, "lost connection to parent"); 622 return (TRUE); 623 } 624 } 625 626 /* 627 * The right way to send RPC broadcasts. 628 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast() 629 * blocks while waiting for replies, so we have to fork off separate 630 * broadcaster processes that do the waiting and then transmit their 631 * results back to the parent for processing. We also have to remember 632 * to save the name of the domain we're trying to bind in a global 633 * variable since clnt_broadcast() provides no way to pass things to 634 * the 'eachresult' callback function. 635 */ 636 void 637 broadcast(struct _dom_binding *ypdb) 638 { 639 bool_t out = FALSE; 640 enum clnt_stat stat; 641 642 if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid) 643 return; 644 645 if (pipe(ypdb->dom_pipe_fds) < 0) { 646 syslog(LOG_WARNING, "pipe: %m"); 647 return; 648 } 649 650 if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr) 651 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding", 652 inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain); 653 654 broad_domain = ypdb; 655 flock(ypdb->dom_lockfd, LOCK_UN); 656 657 switch ((ypdb->dom_broadcast_pid = fork())) { 658 case 0: 659 close(READFD); 660 signal(SIGCHLD, SIG_DFL); 661 signal(SIGTERM, SIG_DFL); 662 break; 663 case -1: 664 syslog(LOG_WARNING, "fork: %m"); 665 close(READFD); 666 close(WRITEFD); 667 return; 668 default: 669 close(WRITEFD); 670 FD_SET(READFD, &svc_fdset); 671 children++; 672 return; 673 } 674 675 /* Release all locks before doing anything else. */ 676 while (ypbindlist) { 677 close(ypbindlist->dom_lockfd); 678 ypbindlist = ypbindlist->dom_pnext; 679 } 680 close(yplockfd); 681 682 /* 683 * Special 'many-cast' behavior. If we're in restricted mode, 684 * we have a list of possible server addresses to try. What 685 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK 686 * procedure and time the replies. Whoever replies fastest 687 * gets to be our server. Note that this is not a broadcast 688 * operation: we transmit uni-cast datagrams only. 689 */ 690 if (yp_restricted && yp_manycast) { 691 short port; 692 int i; 693 struct sockaddr_in sin; 694 695 i = __yp_ping(restricted_addrs, yp_restricted, 696 ypdb->dom_domain, &port); 697 if (i == -1) { 698 bzero((char *)&ypdb->dom_server_addr, 699 sizeof(struct sockaddr_in)); 700 if (tell_parent(ypdb->dom_domain, 701 &ypdb->dom_server_addr)) 702 syslog(LOG_WARNING, "lost connection to parent"); 703 } else { 704 bzero((char *)&sin, sizeof(struct sockaddr_in)); 705 bcopy((char *)&restricted_addrs[i], 706 (char *)&sin.sin_addr, sizeof(struct in_addr)); 707 sin.sin_family = AF_INET; 708 sin.sin_port = port; 709 if (tell_parent(broad_domain->dom_domain, &sin)) 710 syslog(LOG_WARNING, 711 "lost connection to parent"); 712 } 713 _exit(0); 714 } 715 716 retries = 0; 717 718 { 719 char *ptr; 720 721 ptr = (char *)&ypdb->dom_domain; 722 stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK, 723 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out, 724 broadcast_result); 725 } 726 727 if (stat != RPC_SUCCESS) { 728 bzero((char *)&ypdb->dom_server_addr, 729 sizeof(struct sockaddr_in)); 730 if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr)) 731 syslog(LOG_WARNING, "lost connection to parent"); 732 } 733 734 _exit(0); 735 } 736 737 /* 738 * The right way to check if a server is alive. 739 * Attempt to get a client handle pointing to the server and send a 740 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE, 741 * we invalidate this binding entry and send out a broadcast to try to 742 * establish a new binding. Note that we treat non-default domains 743 * specially: once bound, we keep tabs on our server, but if it 744 * goes away and fails to respond after one round of broadcasting, we 745 * abandon it until a client specifically references it again. We make 746 * every effort to keep our default domain bound, however, since we 747 * need it to keep the system on its feet. 748 */ 749 int 750 ping(struct _dom_binding *ypdb) 751 { 752 bool_t out; 753 struct timeval interval, timeout; 754 enum clnt_stat stat; 755 int rpcsock = RPC_ANYSOCK; 756 CLIENT *client_handle; 757 758 interval.tv_sec = FAIL_THRESHOLD; 759 interval.tv_usec = 0; 760 timeout.tv_sec = FAIL_THRESHOLD; 761 timeout.tv_usec = 0; 762 763 if (ypdb->dom_broadcast_pid) 764 return(1); 765 766 if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr, 767 YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE, 768 RPCSMALLMSGSIZE)) == (CLIENT *)NULL) { 769 /* Can't get a handle: we're dead. */ 770 ypdb->dom_alive = 0; 771 ypdb->dom_vers = -1; 772 broadcast(ypdb); 773 return(1); 774 } 775 776 { 777 char *ptr; 778 779 ptr = (char *)&ypdb->dom_domain; 780 781 if ((stat = clnt_call(client_handle, YPPROC_DOMAIN, 782 xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out, 783 timeout)) != RPC_SUCCESS || out == FALSE) { 784 ypdb->dom_alive = 0; 785 ypdb->dom_vers = -1; 786 clnt_destroy(client_handle); 787 broadcast(ypdb); 788 return(1); 789 } 790 } 791 792 clnt_destroy(client_handle); 793 return(0); 794 } 795 796 void rpc_received(char *dom, struct sockaddr_in *raddrp, int force) 797 { 798 struct _dom_binding *ypdb, *prev = NULL; 799 struct iovec iov[2]; 800 struct ypbind_resp ybr; 801 char path[MAXPATHLEN]; 802 int fd; 803 804 /*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr), 805 ntohs(raddrp->sin_port), dom);*/ 806 807 if (dom == NULL) 808 return; 809 810 for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) { 811 if (strcmp(ypdb->dom_domain, dom) == 0) 812 break; 813 prev = ypdb; 814 } 815 816 if (ypdb && force) { 817 if (ypdb->dom_broadcast_pid) { 818 kill(ypdb->dom_broadcast_pid, SIGINT); 819 close(READFD); 820 FD_CLR(READFD, &fdsr); 821 FD_CLR(READFD, &svc_fdset); 822 READFD = WRITEFD = -1; 823 } 824 } 825 826 /* if in secure mode, check originating port number */ 827 if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) { 828 syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.", 829 inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port), 830 dom); 831 if (ypdb != NULL) { 832 ypdb->dom_broadcast_pid = 0; 833 ypdb->dom_alive = 0; 834 } 835 return; 836 } 837 838 if (raddrp->sin_addr.s_addr == (long)0) { 839 switch (ypdb->dom_default) { 840 case 0: 841 if (prev == NULL) 842 ypbindlist = ypdb->dom_pnext; 843 else 844 prev->dom_pnext = ypdb->dom_pnext; 845 sprintf(path, "%s/%s.%ld", BINDINGDIR, 846 ypdb->dom_domain, YPVERS); 847 close(ypdb->dom_lockfd); 848 unlink(path); 849 free(ypdb); 850 domains--; 851 return; 852 case 1: 853 ypdb->dom_broadcast_pid = 0; 854 ypdb->dom_alive = 0; 855 broadcast(ypdb); 856 return; 857 default: 858 break; 859 } 860 } 861 862 if (ypdb == NULL) { 863 if (force == 0) 864 return; 865 ypdb = (struct _dom_binding *)malloc(sizeof *ypdb); 866 if (ypdb == NULL) { 867 syslog(LOG_WARNING, "malloc: %m"); 868 return; 869 } 870 bzero((char *)ypdb, sizeof *ypdb); 871 strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain); 872 ypdb->dom_lockfd = -1; 873 ypdb->dom_default = 0; 874 ypdb->dom_pnext = ypbindlist; 875 ypbindlist = ypdb; 876 } 877 878 /* We've recovered from a crash: inform the world. */ 879 if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr) 880 syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK", 881 inet_ntoa(raddrp->sin_addr), ypdb->dom_domain); 882 883 bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr, 884 sizeof ypdb->dom_server_addr); 885 886 ypdb->dom_vers = YPVERS; 887 ypdb->dom_alive = 1; 888 ypdb->dom_broadcast_pid = 0; 889 890 if (ypdb->dom_lockfd != -1) 891 close(ypdb->dom_lockfd); 892 893 sprintf(path, "%s/%s.%ld", BINDINGDIR, 894 ypdb->dom_domain, ypdb->dom_vers); 895 #ifdef O_SHLOCK 896 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) { 897 (void)mkdir(BINDINGDIR, 0755); 898 if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) 899 return; 900 } 901 #else 902 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) { 903 (void)mkdir(BINDINGDIR, 0755); 904 if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) 905 return; 906 } 907 flock(fd, LOCK_SH); 908 #endif 909 910 /* 911 * ok, if BINDINGDIR exists, and we can create the binding file, 912 * then write to it.. 913 */ 914 ypdb->dom_lockfd = fd; 915 916 iov[0].iov_base = (caddr_t)&(udptransp->xp_port); 917 iov[0].iov_len = sizeof udptransp->xp_port; 918 iov[1].iov_base = (caddr_t)&ybr; 919 iov[1].iov_len = sizeof ybr; 920 921 bzero(&ybr, sizeof ybr); 922 ybr.ypbind_status = YPBIND_SUCC_VAL; 923 *(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr; 924 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port; 925 926 if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) { 927 syslog(LOG_WARNING, "write: %m"); 928 close(ypdb->dom_lockfd); 929 ypdb->dom_lockfd = -1; 930 return; 931 } 932 } 933 934 /* 935 * Check address against list of allowed servers. Return 0 if okay, 936 * 1 if not matched. 937 */ 938 int 939 verify(struct in_addr addr) 940 { 941 int i; 942 943 for (i = 0; i < RESTRICTED_SERVERS; i++) 944 if (!bcmp((char *)&addr, (char *)&restricted_addrs[i], 945 sizeof(struct in_addr))) 946 return(0); 947 948 return(1); 949 } 950 951 /* 952 * Try to set restricted mode. We default to normal mode if we can't 953 * resolve the specified hostnames. 954 */ 955 void 956 yp_restricted_mode(char *args) 957 { 958 struct hostent *h; 959 int i = 0; 960 char *s; 961 962 /* Find the restricted domain. */ 963 if ((s = strsep(&args, ",")) == NULL) 964 return; 965 domain_name = s; 966 967 /* Get the addresses of the servers. */ 968 while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) { 969 if ((h = gethostbyname(s)) == NULL) 970 return; 971 bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i], 972 sizeof(struct in_addr)); 973 i++; 974 } 975 976 /* ypset and ypsetme not allowed with restricted mode */ 977 ypsetmode = YPSET_NO; 978 979 yp_restricted = i; 980 return; 981 } 982