1 /* 2 * Copyright (c) 1995 3 * Bill Paul <wpaul@ctr.columbia.edu>. 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD: src/usr.sbin/yppush/yppush_main.c,v 1.11.2.2 2002/02/15 00:46:59 des Exp $ 33 * $DragonFly: src/usr.sbin/yppush/yppush_main.c,v 1.2 2003/06/17 04:30:04 dillon Exp $ 34 */ 35 36 #include <errno.h> 37 #include <signal.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <time.h> 42 #include <unistd.h> 43 #include <sys/socket.h> 44 #include <sys/fcntl.h> 45 #include <sys/wait.h> 46 #include <sys/param.h> 47 #include <rpc/rpc.h> 48 #include <rpc/clnt.h> 49 #include <rpc/pmap_clnt.h> 50 #include <rpcsvc/yp.h> 51 struct dom_binding {}; 52 #include <rpcsvc/ypclnt.h> 53 #include "ypxfr_extern.h" 54 #include "yppush_extern.h" 55 56 char *progname = "yppush"; 57 int debug = 1; 58 int _rpcpmstart = 0; 59 char *yp_dir = _PATH_YP; 60 61 char *yppush_mapname = NULL; /* Map to transfer. */ 62 char *yppush_domain = NULL; /* Domain in which map resides. */ 63 char *yppush_master = NULL; /* Master NIS server for said domain. */ 64 int verbose = 0; /* Toggle verbose mode. */ 65 unsigned long yppush_transid = 0; 66 int yppush_timeout = 80; /* Default timeout. */ 67 int yppush_jobs = 0; /* Number of allowed concurrent jobs. */ 68 int yppush_running_jobs = 0; /* Number of currently running jobs. */ 69 int yppush_alarm_tripped = 0; 70 71 /* Structure for holding information about a running job. */ 72 struct jobs { 73 unsigned long tid; 74 int sock; 75 int port; 76 ypxfrstat stat; 77 unsigned long prognum; 78 char *server; 79 char *map; 80 int polled; 81 struct jobs *next; 82 }; 83 84 struct jobs *yppush_joblist; /* Linked list of running jobs. */ 85 86 /* 87 * Local error messages. 88 */ 89 static char *yppusherr_string(err) 90 int err; 91 { 92 switch (err) { 93 case YPPUSH_TIMEDOUT: return("transfer or callback timed out"); 94 case YPPUSH_YPSERV: return("failed to contact ypserv"); 95 case YPPUSH_NOHOST: return("no such host"); 96 case YPPUSH_PMAP: return("portmapper failure"); 97 default: return("unknown error code"); 98 } 99 } 100 101 /* 102 * Report state of a job. 103 */ 104 static int yppush_show_status(status, tid) 105 ypxfrstat status; 106 unsigned long tid; 107 { 108 struct jobs *job; 109 110 job = yppush_joblist; 111 112 while (job) { 113 if (job->tid == tid) 114 break; 115 job = job->next; 116 } 117 118 if (job->polled) { 119 return(0); 120 } 121 122 if (verbose > 1) 123 yp_error("checking return status: transaction ID: %lu", 124 job->tid); 125 if (status != YPPUSH_SUCC || verbose) { 126 yp_error("transfer of map %s to server %s %s", 127 job->map, job->server, status == YPPUSH_SUCC ? 128 "succeeded" : "failed"); 129 yp_error("status returned by ypxfr: %s", status > YPPUSH_AGE ? 130 yppusherr_string(status) : 131 ypxfrerr_string(status)); 132 } 133 134 job->polled = 1; 135 136 svc_unregister(job->prognum, 1); 137 138 yppush_running_jobs--; 139 return(0); 140 } 141 142 /* Exit routine. */ 143 static void yppush_exit(now) 144 int now; 145 { 146 struct jobs *jptr; 147 int still_pending = 1; 148 149 /* Let all the information trickle in. */ 150 while (!now && still_pending) { 151 jptr = yppush_joblist; 152 still_pending = 0; 153 while (jptr) { 154 if (jptr->polled == 0) { 155 still_pending++; 156 if (verbose > 1) 157 yp_error("%s has not responded", 158 jptr->server); 159 } else { 160 if (verbose > 1) 161 yp_error("%s has responded", 162 jptr->server); 163 } 164 jptr = jptr->next; 165 } 166 if (still_pending) { 167 if (verbose > 1) 168 yp_error("%d transfer%sstill pending", 169 still_pending, 170 still_pending > 1 ? "s " : " "); 171 yppush_alarm_tripped = 0; 172 alarm(YPPUSH_RESPONSE_TIMEOUT); 173 pause(); 174 alarm(0); 175 if (yppush_alarm_tripped == 1) { 176 yp_error("timed out"); 177 now = 1; 178 } 179 } else { 180 if (verbose) 181 yp_error("all transfers complete"); 182 break; 183 } 184 } 185 186 187 /* All stats collected and reported -- kill all the stragglers. */ 188 jptr = yppush_joblist; 189 while (jptr) { 190 if (!jptr->polled) 191 yp_error("warning: exiting with transfer \ 192 to %s (transid = %lu) still pending", jptr->server, jptr->tid); 193 svc_unregister(jptr->prognum, 1); 194 jptr = jptr->next; 195 } 196 197 exit(0); 198 } 199 200 /* 201 * Handler for 'normal' signals. 202 */ 203 204 static void handler(sig) 205 int sig; 206 { 207 if (sig == SIGTERM || sig == SIGINT || sig == SIGABRT) { 208 yppush_jobs = 0; 209 yppush_exit(1); 210 } 211 212 if (sig == SIGALRM) { 213 alarm(0); 214 yppush_alarm_tripped++; 215 } 216 217 return; 218 } 219 220 /* 221 * Dispatch loop for callback RPC services. 222 */ 223 static void yppush_svc_run() 224 { 225 #ifdef FD_SETSIZE 226 fd_set readfds; 227 #else 228 int readfds; 229 #endif /* def FD_SETSIZE */ 230 struct timeval timeout; 231 232 timeout.tv_usec = 0; 233 timeout.tv_sec = 5; 234 235 retry: 236 #ifdef FD_SETSIZE 237 readfds = svc_fdset; 238 #else 239 readfds = svc_fds; 240 #endif /* def FD_SETSIZE */ 241 switch (select(_rpc_dtablesize(), &readfds, NULL, NULL, &timeout)) { 242 case -1: 243 if (errno == EINTR) 244 goto retry; 245 yp_error("select failed: %s", strerror(errno)); 246 break; 247 case 0: 248 yp_error("select() timed out"); 249 break; 250 default: 251 svc_getreqset(&readfds); 252 break; 253 } 254 return; 255 } 256 257 /* 258 * Special handler for asynchronous socket I/O. We mark the 259 * sockets of the callback handlers as O_ASYNC and handle SIGIO 260 * events here, which will occur when the callback handler has 261 * something interesting to tell us. 262 */ 263 static void async_handler(sig) 264 int sig; 265 { 266 yppush_svc_run(); 267 268 /* reset any pending alarms. */ 269 alarm(0); 270 yppush_alarm_tripped++; 271 kill(getpid(), SIGALRM); 272 return; 273 } 274 275 /* 276 * RPC service routines for callbacks. 277 */ 278 void * 279 yppushproc_null_1_svc(void *argp, struct svc_req *rqstp) 280 { 281 static char * result; 282 /* Do nothing -- RPC conventions call for all a null proc. */ 283 return((void *) &result); 284 } 285 286 void * 287 yppushproc_xfrresp_1_svc(yppushresp_xfr *argp, struct svc_req *rqstp) 288 { 289 static char * result; 290 yppush_show_status(argp->status, argp->transid); 291 return((void *) &result); 292 } 293 294 /* 295 * Transmit a YPPROC_XFR request to ypserv. 296 */ 297 static int yppush_send_xfr(job) 298 struct jobs *job; 299 { 300 ypreq_xfr req; 301 /* ypresp_xfr *resp; */ 302 DBT key, data; 303 CLIENT *clnt; 304 struct rpc_err err; 305 struct timeval timeout; 306 307 timeout.tv_usec = 0; 308 timeout.tv_sec = 0; 309 310 /* 311 * The ypreq_xfr structure has a member of type map_parms, 312 * which seems to require the order number of the map. 313 * It isn't actually used at the other end (at least the 314 * FreeBSD ypserv doesn't use it) but we fill it in here 315 * for the sake of completeness. 316 */ 317 key.data = "YP_LAST_MODIFIED"; 318 key.size = sizeof ("YP_LAST_MODIFIED") - 1; 319 320 if (yp_get_record(yppush_domain, yppush_mapname, &key, &data, 321 1) != YP_TRUE) { 322 yp_error("failed to read order number from %s: %s: %s", 323 yppush_mapname, yperr_string(yp_errno), 324 strerror(errno)); 325 return(1); 326 } 327 328 /* Fill in the request arguments */ 329 req.map_parms.ordernum = atoi(data.data); 330 req.map_parms.domain = yppush_domain; 331 req.map_parms.peer = yppush_master; 332 req.map_parms.map = job->map; 333 req.transid = job->tid; 334 req.prog = job->prognum; 335 req.port = job->port; 336 337 /* Get a handle to the remote ypserv. */ 338 if ((clnt = clnt_create(job->server, YPPROG, YPVERS, "udp")) == NULL) { 339 yp_error("%s: %s",job->server,clnt_spcreateerror("couldn't \ 340 create udp handle to NIS server")); 341 switch (rpc_createerr.cf_stat) { 342 case RPC_UNKNOWNHOST: 343 job->stat = YPPUSH_NOHOST; 344 break; 345 case RPC_PMAPFAILURE: 346 job->stat = YPPUSH_PMAP; 347 break; 348 default: 349 job->stat = YPPUSH_RPC; 350 break; 351 } 352 return(1); 353 } 354 355 /* 356 * Reduce timeout to nothing since we may not 357 * get a response from ypserv and we don't want to block. 358 */ 359 if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE) 360 yp_error("failed to set timeout on ypproc_xfr call"); 361 362 /* Invoke the ypproc_xfr service. */ 363 if (ypproc_xfr_2(&req, clnt) == NULL) { 364 clnt_geterr(clnt, &err); 365 if (err.re_status != RPC_SUCCESS && 366 err.re_status != RPC_TIMEDOUT) { 367 yp_error("%s: %s", job->server, clnt_sperror(clnt, 368 "yp_xfr failed")); 369 job->stat = YPPUSH_YPSERV; 370 clnt_destroy(clnt); 371 return(1); 372 } 373 } 374 375 clnt_destroy(clnt); 376 377 return(0); 378 } 379 380 /* 381 * Main driver function. Register the callback service, add the transfer 382 * request to the internal list, send the YPPROC_XFR request to ypserv 383 * do other magic things. 384 */ 385 int yp_push(server, map, tid) 386 char *server; 387 char *map; 388 unsigned long tid; 389 { 390 unsigned long prognum; 391 int sock = RPC_ANYSOCK; 392 SVCXPRT *xprt; 393 struct jobs *job; 394 395 /* 396 * Register the callback service on the first free 397 * transient program number. 398 */ 399 xprt = svcudp_create(sock); 400 for (prognum = 0x40000000; prognum < 0x5FFFFFFF; prognum++) { 401 if (svc_register(xprt, prognum, 1, 402 yppush_xfrrespprog_1, IPPROTO_UDP) == TRUE) 403 break; 404 } 405 406 /* Register the job in our linked list of jobs. */ 407 if ((job = (struct jobs *)malloc(sizeof (struct jobs))) == NULL) { 408 yp_error("malloc failed"); 409 yppush_exit(1); 410 } 411 412 /* Initialize the info for this job. */ 413 job->stat = 0; 414 job->tid = tid; 415 job->port = xprt->xp_port; 416 job->sock = xprt->xp_sock; /*XXX: Evil!! EEEEEEEVIL!!! */ 417 job->server = strdup(server); 418 job->map = strdup(map); 419 job->prognum = prognum; 420 job->polled = 0; 421 job->next = yppush_joblist; 422 yppush_joblist = job; 423 424 /* 425 * Set the RPC sockets to asynchronous mode. This will 426 * cause the system to smack us with a SIGIO when an RPC 427 * callback is delivered. This in turn allows us to handle 428 * the callback even though we may be in the middle of doing 429 * something else at the time. 430 * 431 * XXX This is a horrible thing to do for two reasons, 432 * both of which have to do with portability: 433 * 1) We really ought not to be sticking our grubby mits 434 * into the RPC service transport handle like this. 435 * 2) Even in this day and age, there are still some *NIXes 436 * that don't support async socket I/O. 437 */ 438 if (fcntl(xprt->xp_sock, F_SETOWN, getpid()) == -1 || 439 fcntl(xprt->xp_sock, F_SETFL, O_ASYNC) == -1) { 440 yp_error("failed to set async I/O mode: %s", 441 strerror(errno)); 442 yppush_exit(1); 443 } 444 445 if (verbose) { 446 yp_error("initiating transfer: %s -> %s (transid = %lu)", 447 yppush_mapname, server, tid); 448 } 449 450 /* 451 * Send the XFR request to ypserv. We don't have to wait for 452 * a response here since we can handle them asynchronously. 453 */ 454 455 if (yppush_send_xfr(job)){ 456 /* Transfer request blew up. */ 457 yppush_show_status(job->stat ? job->stat : 458 YPPUSH_YPSERV,job->tid); 459 } else { 460 if (verbose > 1) 461 yp_error("%s has been called", server); 462 } 463 464 return(0); 465 } 466 467 /* 468 * Called for each entry in the ypservers map from yp_get_map(), which 469 * is our private yp_all() routine. 470 */ 471 int yppush_foreach(status, key, keylen, val, vallen, data) 472 int status; 473 char *key; 474 int keylen; 475 char *val; 476 int vallen; 477 char *data; 478 { 479 char server[YPMAXRECORD + 2]; 480 481 if (status != YP_TRUE) 482 return (status); 483 484 snprintf(server, sizeof(server), "%.*s", vallen, val); 485 486 /* 487 * Restrict the number of concurrent jobs. If yppush_jobs number 488 * of jobs have already been dispatched and are still pending, 489 * wait for one of them to finish so we can reuse its slot. 490 */ 491 if (yppush_jobs <= 1) { 492 yppush_alarm_tripped = 0; 493 while (!yppush_alarm_tripped && yppush_running_jobs) { 494 alarm(yppush_timeout); 495 yppush_alarm_tripped = 0; 496 pause(); 497 alarm(0); 498 } 499 } else { 500 yppush_alarm_tripped = 0; 501 while (!yppush_alarm_tripped && yppush_running_jobs >= yppush_jobs) { 502 alarm(yppush_timeout); 503 yppush_alarm_tripped = 0; 504 pause(); 505 alarm(0); 506 } 507 } 508 509 /* Cleared for takeoff: set everything in motion. */ 510 if (yp_push(&server, yppush_mapname, yppush_transid)) 511 return(yp_errno); 512 513 /* Bump the job counter and transaction ID. */ 514 yppush_running_jobs++; 515 yppush_transid++; 516 return (0); 517 } 518 519 static void usage() 520 { 521 fprintf (stderr, "%s\n%s\n", 522 "usage: yppush [-d domain] [-t timeout] [-j #parallel jobs] [-h host]", 523 " [-p path] mapname"); 524 exit(1); 525 } 526 527 /* 528 * Entry point. (About time!) 529 */ 530 int 531 main(argc,argv) 532 int argc; 533 char *argv[]; 534 { 535 int ch; 536 DBT key, data; 537 char myname[MAXHOSTNAMELEN]; 538 struct hostlist { 539 char *name; 540 struct hostlist *next; 541 }; 542 struct hostlist *yppush_hostlist = NULL; 543 struct hostlist *tmp; 544 struct sigaction sa; 545 546 while ((ch = getopt(argc, argv, "d:j:p:h:t:v")) != -1) { 547 switch (ch) { 548 case 'd': 549 yppush_domain = optarg; 550 break; 551 case 'j': 552 yppush_jobs = atoi(optarg); 553 if (yppush_jobs <= 0) 554 yppush_jobs = 1; 555 break; 556 case 'p': 557 yp_dir = optarg; 558 break; 559 case 'h': /* we can handle multiple hosts */ 560 if ((tmp = (struct hostlist *)malloc(sizeof(struct hostlist))) == NULL) { 561 yp_error("malloc failed"); 562 yppush_exit(1); 563 } 564 tmp->name = strdup(optarg); 565 tmp->next = yppush_hostlist; 566 yppush_hostlist = tmp; 567 break; 568 case 't': 569 yppush_timeout = atoi(optarg); 570 break; 571 case 'v': 572 verbose++; 573 break; 574 default: 575 usage(); 576 break; 577 } 578 } 579 580 argc -= optind; 581 argv += optind; 582 583 yppush_mapname = argv[0]; 584 585 if (yppush_mapname == NULL) { 586 /* "No guts, no glory." */ 587 usage(); 588 } 589 590 /* 591 * If no domain was specified, try to find the default 592 * domain. If we can't find that, we're doomed and must bail. 593 */ 594 if (yppush_domain == NULL) { 595 char *yppush_check_domain; 596 if (!yp_get_default_domain(&yppush_check_domain) && 597 !_yp_check(&yppush_check_domain)) { 598 yp_error("no domain specified and NIS not running"); 599 usage(); 600 } else 601 yp_get_default_domain(&yppush_domain); 602 } 603 604 /* Check to see that we are the master for this map. */ 605 606 if (gethostname ((char *)&myname, sizeof(myname))) { 607 yp_error("failed to get name of local host: %s", 608 strerror(errno)); 609 yppush_exit(1); 610 } 611 612 key.data = "YP_MASTER_NAME"; 613 key.size = sizeof("YP_MASTER_NAME") - 1; 614 615 if (yp_get_record(yppush_domain, yppush_mapname, 616 &key, &data, 1) != YP_TRUE) { 617 yp_error("couldn't open %s map: %s", yppush_mapname, 618 strerror(errno)); 619 yppush_exit(1); 620 } 621 622 if (strncmp(myname, data.data, data.size)) { 623 yp_error("warning: this host is not the master for %s", 624 yppush_mapname); 625 #ifdef NITPICKY 626 yppush_exit(1); 627 #endif 628 } 629 630 yppush_master = malloc(data.size + 1); 631 strncpy(yppush_master, data.data, data.size); 632 yppush_master[data.size] = '\0'; 633 634 /* Install some handy handlers. */ 635 signal(SIGALRM, handler); 636 signal(SIGTERM, handler); 637 signal(SIGINT, handler); 638 signal(SIGABRT, handler); 639 640 /* 641 * Set up the SIGIO handler. Make sure that some of the 642 * other signals are blocked while the handler is running so 643 * select() doesn't get interrupted. 644 */ 645 sigemptyset(&sa.sa_mask); 646 sigaddset(&sa.sa_mask, SIGIO); /* Goes without saying. */ 647 sigaddset(&sa.sa_mask, SIGPIPE); 648 sigaddset(&sa.sa_mask, SIGCHLD); 649 sigaddset(&sa.sa_mask, SIGALRM); 650 sigaddset(&sa.sa_mask, SIGINT); 651 sa.sa_handler = async_handler; 652 sa.sa_flags = 0; 653 654 sigaction(SIGIO, &sa, NULL); 655 656 /* set initial transaction ID */ 657 yppush_transid = time((time_t *)NULL); 658 659 if (yppush_hostlist) { 660 /* 661 * Host list was specified on the command line: 662 * kick off the transfers by hand. 663 */ 664 tmp = yppush_hostlist; 665 while (tmp) { 666 yppush_foreach(YP_TRUE, NULL, 0, tmp->name, 667 strlen(tmp->name)); 668 tmp = tmp->next; 669 } 670 } else { 671 /* 672 * Do a yp_all() on the ypservers map and initiate a ypxfr 673 * for each one. 674 */ 675 ypxfr_get_map("ypservers", yppush_domain, 676 "localhost", yppush_foreach); 677 } 678 679 if (verbose > 1) 680 yp_error("all jobs dispatched"); 681 682 /* All done -- normal exit. */ 683 yppush_exit(0); 684 685 /* Just in case. */ 686 exit(0); 687 } 688