1 /* $NetBSD: docmd.c,v 1.20 2000/06/12 04:43:11 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)docmd.c 8.1 (Berkeley) 6/9/93"; 40 #else 41 __RCSID("$NetBSD: docmd.c,v 1.20 2000/06/12 04:43:11 mrg Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 #include <sys/ioctl.h> 47 48 #include <errno.h> 49 #include <netdb.h> 50 #include <regex.h> 51 #include <setjmp.h> 52 #include <fcntl.h> 53 54 #include "defs.h" 55 56 FILE *lfp; /* log file for recording files updated */ 57 struct subcmd *subcmds; /* list of sub-commands for current cmd */ 58 jmp_buf env; 59 60 static int remerr = -1; /* Remote stderr */ 61 62 static int makeconn __P((char *)); 63 static int okname __P((char *)); 64 static void closeconn __P((void)); 65 static void cmptime __P((char *)); 66 static void doarrow __P((char **, 67 struct namelist *, char *, struct subcmd *)); 68 static void dodcolon __P((char **, 69 struct namelist *, char *, struct subcmd *)); 70 static void notify __P((char *, char *, struct namelist *, time_t)); 71 static void rcmptime __P((struct stat *)); 72 73 /* 74 * Do the commands in cmds (initialized by yyparse). 75 */ 76 void 77 docmds(dhosts, argc, argv) 78 char **dhosts; 79 int argc; 80 char **argv; 81 { 82 struct cmd *c; 83 struct namelist *f; 84 char **cpp; 85 extern struct cmd *cmds; 86 87 signal(SIGHUP, cleanup); 88 signal(SIGINT, cleanup); 89 signal(SIGQUIT, cleanup); 90 signal(SIGTERM, cleanup); 91 92 for (c = cmds; c != NULL; c = c->c_next) { 93 if (dhosts != NULL && *dhosts != NULL) { 94 for (cpp = dhosts; *cpp; cpp++) 95 if (strcmp(c->c_name, *cpp) == 0) 96 goto fndhost; 97 continue; 98 } 99 fndhost: 100 if (argc) { 101 for (cpp = argv; *cpp; cpp++) { 102 if (c->c_label != NULL && 103 strcmp(c->c_label, *cpp) == 0) { 104 cpp = NULL; 105 goto found; 106 } 107 for (f = c->c_files; f != NULL; f = f->n_next) 108 if (strcmp(f->n_name, *cpp) == 0) 109 goto found; 110 } 111 continue; 112 } else 113 cpp = NULL; 114 found: 115 switch (c->c_type) { 116 case ARROW: 117 doarrow(cpp, c->c_files, c->c_name, c->c_cmds); 118 break; 119 case DCOLON: 120 dodcolon(cpp, c->c_files, c->c_name, c->c_cmds); 121 break; 122 default: 123 fatal("illegal command type %d\n", c->c_type); 124 } 125 } 126 closeconn(); 127 } 128 129 /* 130 * Process commands for sending files to other machines. 131 */ 132 static void 133 doarrow(filev, files, rhost, cmds) 134 char **filev; 135 struct namelist *files; 136 char *rhost; 137 struct subcmd *cmds; 138 { 139 struct namelist *f; 140 struct subcmd *sc; 141 char **cpp; 142 int n, ddir, opts = options; 143 144 #if __GNUC__ /* XXX borken compiler alert! */ 145 (void)&ddir; 146 (void)&opts; 147 #endif 148 149 if (debug) 150 printf("doarrow(%lx, %s, %lx)\n", 151 (long)files, rhost, (long)cmds); 152 153 if (files == NULL) { 154 error("no files to be updated\n"); 155 return; 156 } 157 158 subcmds = cmds; 159 ddir = files->n_next != NULL; /* destination is a directory */ 160 if (nflag) 161 printf("updating host %s\n", rhost); 162 else { 163 if (setjmp(env)) 164 goto done; 165 signal(SIGPIPE, lostconn); 166 if (!makeconn(rhost)) 167 return; 168 if ((lfp = fopen(tempfile, "w")) == NULL) { 169 fatal("cannot open %s\n", tempfile); 170 exit(1); 171 } 172 } 173 for (f = files; f != NULL; f = f->n_next) { 174 if (filev) { 175 for (cpp = filev; *cpp; cpp++) 176 if (strcmp(f->n_name, *cpp) == 0) 177 goto found; 178 if (!nflag && lfp) 179 (void) fclose(lfp); 180 continue; 181 } 182 found: 183 n = 0; 184 for (sc = cmds; sc != NULL; sc = sc->sc_next) { 185 if (sc->sc_type != INSTALL) 186 continue; 187 n++; 188 install(f->n_name, sc->sc_name, 189 sc->sc_name == NULL ? 0 : ddir, sc->sc_options); 190 opts = sc->sc_options; 191 } 192 if (n == 0) 193 install(f->n_name, NULL, 0, options); 194 } 195 done: 196 if (!nflag) { 197 (void) signal(SIGPIPE, cleanup); 198 if (lfp) 199 (void) fclose(lfp); 200 lfp = NULL; 201 } 202 for (sc = cmds; sc != NULL; sc = sc->sc_next) 203 if (sc->sc_type == NOTIFY) 204 notify(tempfile, rhost, sc->sc_args, 0); 205 if (!nflag) { 206 for (; ihead != NULL; ihead = ihead->nextp) { 207 free(ihead); 208 if ((opts & IGNLNKS) || ihead->count == 0) 209 continue; 210 if (lfp) 211 log(lfp, "%s: Warning: missing links\n", 212 ihead->pathname); 213 } 214 } 215 } 216 217 /* 218 * Create a connection to the rdist server on the machine rhost. 219 */ 220 static int 221 makeconn(rhost) 222 char *rhost; 223 { 224 char *ruser, *cp; 225 static char *cur_host = NULL; 226 static int port = -1; 227 char tuser[20]; 228 int n; 229 extern char user[]; 230 231 if (debug) 232 printf("makeconn(%s)\n", rhost); 233 234 if (cur_host != NULL && rem >= 0) { 235 if (strcmp(cur_host, rhost) == 0) 236 return(1); 237 closeconn(); 238 } 239 cur_host = rhost; 240 cp = strchr(rhost, '@'); 241 if (cp != NULL) { 242 char c = *cp; 243 244 *cp = '\0'; 245 strncpy(tuser, rhost, sizeof(tuser)-1); 246 *cp = c; 247 rhost = cp + 1; 248 ruser = tuser; 249 if (*ruser == '\0') 250 ruser = user; 251 else if (!okname(ruser)) 252 return(0); 253 } else 254 ruser = user; 255 if (!qflag) 256 printf("updating host %s\n", rhost); 257 (void) snprintf(buf, sizeof(buf), "%s -Server%s", _PATH_RDIST, 258 qflag ? " -q" : ""); 259 if (port < 0) { 260 struct servent *sp; 261 262 if ((sp = getservbyname("shell", "tcp")) == NULL) 263 fatal("shell/tcp: unknown service"); 264 port = sp->s_port; 265 } 266 267 if (debug) { 268 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser); 269 printf("buf = %s\n", buf); 270 } 271 272 fflush(stdout); 273 seteuid(0); 274 rem = rcmd(&rhost, port, user, ruser, buf, &remerr); 275 seteuid(userid); 276 if (rem < 0) 277 return(0); 278 cp = buf; 279 if (read(rem, cp, 1) != 1) 280 lostconn(0); 281 if (*cp == 'V') { 282 do { 283 if (read(rem, cp, 1) != 1) 284 lostconn(0); 285 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]); 286 *--cp = '\0'; 287 cp = buf; 288 n = 0; 289 while (*cp >= '0' && *cp <= '9') 290 n = (n * 10) + (*cp++ - '0'); 291 if (*cp == '\0' && n == VERSION) 292 return(1); 293 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n); 294 } else { 295 error("connection failed: version numbers don't match\n"); 296 error("got unexpected input:"); 297 do { 298 error("%c", *cp); 299 } while (*cp != '\n' && read(rem, cp, 1) == 1); 300 } 301 closeconn(); 302 return(0); 303 } 304 305 /* 306 * Signal end of previous connection. 307 */ 308 static void 309 closeconn() 310 { 311 if (debug) 312 printf("closeconn()\n"); 313 314 if (rem >= 0) { 315 if (write(rem, "\2\n", 2) < 0 && debug) 316 printf("write failed on fd %d: %s\n", rem, 317 strerror(errno)); 318 (void) close(rem); 319 (void) close(remerr); 320 rem = -1; 321 remerr = -1; 322 } 323 } 324 325 void 326 lostconn(signo) 327 int signo; 328 { 329 char buf[BUFSIZ]; 330 int nr = -1; 331 332 if (remerr != -1) 333 if (ioctl(remerr, FIONREAD, &nr) != -1) { 334 if (nr >= sizeof(buf)) 335 nr = sizeof(buf) - 1; 336 if ((nr = read(remerr, buf, nr)) > 0) { 337 buf[nr] = '\0'; 338 if (buf[nr - 1] == '\n') 339 buf[--nr] = '\0'; 340 } 341 } 342 343 if (nr <= 0) 344 (void) strcpy(buf, "lost connection"); 345 346 if (iamremote) 347 cleanup(0); 348 if (lfp) 349 log(lfp, "rdist: %s\n", buf); 350 else 351 error("%s\n", buf); 352 longjmp(env, 1); 353 } 354 355 static int 356 okname(name) 357 char *name; 358 { 359 char *cp = name; 360 int c; 361 362 do { 363 c = *cp; 364 if (c & 0200) 365 goto bad; 366 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 367 goto bad; 368 cp++; 369 } while (*cp); 370 return(1); 371 bad: 372 error("invalid user name %s\n", name); 373 return(0); 374 } 375 376 time_t lastmod; 377 FILE *tfp; 378 extern char target[], *tp; 379 380 /* 381 * Process commands for comparing files to time stamp files. 382 */ 383 static void 384 dodcolon(filev, files, stamp, cmds) 385 char **filev; 386 struct namelist *files; 387 char *stamp; 388 struct subcmd *cmds; 389 { 390 struct subcmd *sc; 391 struct namelist *f; 392 char **cpp; 393 struct timeval tv[2]; 394 struct stat stb; 395 396 if (debug) 397 printf("dodcolon()\n"); 398 399 if (files == NULL) { 400 error("no files to be updated\n"); 401 return; 402 } 403 if (stat(stamp, &stb) < 0) { 404 error("%s: %s\n", stamp, strerror(errno)); 405 return; 406 } 407 if (debug) 408 printf("%s: %lu\n", stamp, (u_long)stb.st_mtime); 409 410 subcmds = cmds; 411 lastmod = stb.st_mtime; 412 if (nflag || (options & VERIFY)) 413 tfp = NULL; 414 else { 415 if ((tfp = fopen(tempfile, "w")) == NULL) { 416 error("%s: %s\n", tempfile, strerror(errno)); 417 return; 418 } 419 (void) gettimeofday(&tv[0], (struct timezone *)0); 420 tv[1] = tv[0]; 421 (void) utimes(stamp, tv); 422 } 423 424 for (f = files; f != NULL; f = f->n_next) { 425 if (filev) { 426 for (cpp = filev; *cpp; cpp++) 427 if (strcmp(f->n_name, *cpp) == 0) 428 goto found; 429 continue; 430 } 431 found: 432 tp = NULL; 433 cmptime(f->n_name); 434 } 435 436 if (tfp != NULL) 437 (void) fclose(tfp); 438 for (sc = cmds; sc != NULL; sc = sc->sc_next) 439 if (sc->sc_type == NOTIFY) 440 notify(tempfile, NULL, sc->sc_args, lastmod); 441 } 442 443 /* 444 * Compare the mtime of file to the list of time stamps. 445 */ 446 static void 447 cmptime(name) 448 char *name; 449 { 450 struct stat stb; 451 452 if (debug) 453 printf("cmptime(%s)\n", name); 454 455 if (except(name)) 456 return; 457 458 if (nflag) { 459 printf("comparing dates: %s\n", name); 460 return; 461 } 462 463 /* 464 * first time cmptime() is called? 465 */ 466 if (tp == NULL) { 467 if (exptilde(target, name) == NULL) 468 return; 469 tp = name = target; 470 while (*tp) 471 tp++; 472 } 473 if (access(name, 4) < 0 || stat(name, &stb) < 0) { 474 error("%s: %s\n", name, strerror(errno)); 475 return; 476 } 477 478 switch (stb.st_mode & S_IFMT) { 479 case S_IFREG: 480 break; 481 482 case S_IFDIR: 483 rcmptime(&stb); 484 return; 485 486 default: 487 error("%s: not a plain file\n", name); 488 return; 489 } 490 491 if (stb.st_mtime > lastmod) 492 log(tfp, "new: %s\n", name); 493 } 494 495 static void 496 rcmptime(st) 497 struct stat *st; 498 { 499 DIR *d; 500 struct dirent *dp; 501 char *cp; 502 char *otp; 503 int len; 504 505 if (debug) 506 printf("rcmptime(%lx)\n", (long)st); 507 508 if ((d = opendir(target)) == NULL) { 509 error("%s: %s\n", target, strerror(errno)); 510 return; 511 } 512 otp = tp; 513 len = tp - target; 514 while ((dp = readdir(d)) != NULL) { 515 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) 516 continue; 517 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) { 518 error("%s/%s: Name too long\n", target, dp->d_name); 519 continue; 520 } 521 tp = otp; 522 *tp++ = '/'; 523 cp = dp->d_name; 524 while ((*tp++ = *cp++) != 0) 525 ; 526 tp--; 527 cmptime(target); 528 } 529 closedir(d); 530 tp = otp; 531 *tp = '\0'; 532 } 533 534 /* 535 * Notify the list of people the changes that were made. 536 * rhost == NULL if we are mailing a list of changes compared to at time 537 * stamp file. 538 */ 539 static void 540 notify(file, rhost, to, lmod) 541 char *file, *rhost; 542 struct namelist *to; 543 time_t lmod; 544 { 545 int fd, len; 546 struct stat stb; 547 FILE *pf; 548 549 if ((options & VERIFY) || to == NULL) 550 return; 551 if (!qflag) { 552 printf("notify "); 553 if (rhost) 554 printf("@%s ", rhost); 555 prnames(to); 556 } 557 if (nflag) 558 return; 559 560 if ((fd = open(file, 0)) < 0) { 561 error("%s: %s\n", file, strerror(errno)); 562 return; 563 } 564 if (fstat(fd, &stb) < 0) { 565 error("%s: %s\n", file, strerror(errno)); 566 (void) close(fd); 567 return; 568 } 569 if (stb.st_size == 0) { 570 (void) close(fd); 571 return; 572 } 573 /* 574 * Create a pipe to mailling program. 575 */ 576 (void)snprintf(buf, sizeof(buf), "%s -oi -t", _PATH_SENDMAIL); 577 pf = popen(buf, "w"); 578 if (pf == NULL) { 579 error("notify: \"%s\" failed\n", _PATH_SENDMAIL); 580 (void) close(fd); 581 return; 582 } 583 /* 584 * Output the proper header information. 585 */ 586 fprintf(pf, "From: rdist (Remote distribution program)\n"); 587 fprintf(pf, "To:"); 588 if (!any('@', to->n_name) && rhost != NULL) 589 fprintf(pf, " %s@%s", to->n_name, rhost); 590 else 591 fprintf(pf, " %s", to->n_name); 592 to = to->n_next; 593 while (to != NULL) { 594 if (!any('@', to->n_name) && rhost != NULL) 595 fprintf(pf, ", %s@%s", to->n_name, rhost); 596 else 597 fprintf(pf, ", %s", to->n_name); 598 to = to->n_next; 599 } 600 putc('\n', pf); 601 if (rhost != NULL) 602 fprintf(pf, "Subject: files updated by rdist from %s to %s\n", 603 host, rhost); 604 else 605 fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod)); 606 putc('\n', pf); 607 608 while ((len = read(fd, buf, BUFSIZ)) > 0) 609 if (fwrite(buf, 1, len, pf) < 1) 610 error("%s: %s\n", file, strerror(errno)); 611 (void) close(fd); 612 (void) pclose(pf); 613 } 614 615 /* 616 * Return true if name is in the list. 617 */ 618 int 619 inlist(list, file) 620 struct namelist *list; 621 char *file; 622 { 623 struct namelist *nl; 624 625 for (nl = list; nl != NULL; nl = nl->n_next) 626 if (!strcmp(file, nl->n_name)) 627 return(1); 628 return(0); 629 } 630 631 /* 632 * Return TRUE if file is in the exception list. 633 */ 634 int 635 except(file) 636 char *file; 637 { 638 struct subcmd *sc; 639 struct namelist *nl; 640 int err; 641 regex_t s; 642 643 if (debug) 644 printf("except(%s)\n", file); 645 646 for (sc = subcmds; sc != NULL; sc = sc->sc_next) { 647 if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN) 648 continue; 649 for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) { 650 if (sc->sc_type == EXCEPT) { 651 if (!strcmp(file, nl->n_name)) 652 return(1); 653 continue; 654 } 655 if ((err = regcomp(&s, nl->n_name, 0)) != 0) { 656 char ebuf[BUFSIZ]; 657 (void) regerror(err, &s, ebuf, sizeof(ebuf)); 658 error("%s: %s\n", nl->n_name, ebuf); 659 } 660 if (regexec(&s, file, 0, NULL, 0) == 0) { 661 regfree(&s); 662 return(1); 663 } 664 regfree(&s); 665 } 666 } 667 return(0); 668 } 669 670 char * 671 colon(cp) 672 char *cp; 673 { 674 675 while (*cp) { 676 if (*cp == ':') 677 return(cp); 678 if (*cp == '/') 679 return(0); 680 cp++; 681 } 682 return(0); 683 } 684