1 /* $NetBSD: docmd.c,v 1.18 1998/12/19 20:32:17 christos 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.18 1998/12/19 20:32:17 christos 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 (void) unlink(tempfile); 207 for (; ihead != NULL; ihead = ihead->nextp) { 208 free(ihead); 209 if ((opts & IGNLNKS) || ihead->count == 0) 210 continue; 211 if (lfp) 212 log(lfp, "%s: Warning: missing links\n", 213 ihead->pathname); 214 } 215 } 216 } 217 218 /* 219 * Create a connection to the rdist server on the machine rhost. 220 */ 221 static int 222 makeconn(rhost) 223 char *rhost; 224 { 225 char *ruser, *cp; 226 static char *cur_host = NULL; 227 static int port = -1; 228 char tuser[20]; 229 int n; 230 extern char user[]; 231 232 if (debug) 233 printf("makeconn(%s)\n", rhost); 234 235 if (cur_host != NULL && rem >= 0) { 236 if (strcmp(cur_host, rhost) == 0) 237 return(1); 238 closeconn(); 239 } 240 cur_host = rhost; 241 cp = strchr(rhost, '@'); 242 if (cp != NULL) { 243 char c = *cp; 244 245 *cp = '\0'; 246 strncpy(tuser, rhost, sizeof(tuser)-1); 247 *cp = c; 248 rhost = cp + 1; 249 ruser = tuser; 250 if (*ruser == '\0') 251 ruser = user; 252 else if (!okname(ruser)) 253 return(0); 254 } else 255 ruser = user; 256 if (!qflag) 257 printf("updating host %s\n", rhost); 258 (void) snprintf(buf, sizeof(buf), "%s -Server%s", _PATH_RDIST, 259 qflag ? " -q" : ""); 260 if (port < 0) { 261 struct servent *sp; 262 263 if ((sp = getservbyname("shell", "tcp")) == NULL) 264 fatal("shell/tcp: unknown service"); 265 port = sp->s_port; 266 } 267 268 if (debug) { 269 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser); 270 printf("buf = %s\n", buf); 271 } 272 273 fflush(stdout); 274 seteuid(0); 275 rem = rcmd(&rhost, port, user, ruser, buf, &remerr); 276 seteuid(userid); 277 if (rem < 0) 278 return(0); 279 cp = buf; 280 if (read(rem, cp, 1) != 1) 281 lostconn(0); 282 if (*cp == 'V') { 283 do { 284 if (read(rem, cp, 1) != 1) 285 lostconn(0); 286 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]); 287 *--cp = '\0'; 288 cp = buf; 289 n = 0; 290 while (*cp >= '0' && *cp <= '9') 291 n = (n * 10) + (*cp++ - '0'); 292 if (*cp == '\0' && n == VERSION) 293 return(1); 294 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n); 295 } else { 296 error("connection failed: version numbers don't match\n"); 297 error("got unexpected input:"); 298 do { 299 error("%c", *cp); 300 } while (*cp != '\n' && read(rem, cp, 1) == 1); 301 } 302 closeconn(); 303 return(0); 304 } 305 306 /* 307 * Signal end of previous connection. 308 */ 309 static void 310 closeconn() 311 { 312 if (debug) 313 printf("closeconn()\n"); 314 315 if (rem >= 0) { 316 (void) write(rem, "\2\n", 2); 317 (void) close(rem); 318 (void) close(remerr); 319 rem = -1; 320 remerr = -1; 321 } 322 } 323 324 void 325 lostconn(signo) 326 int signo; 327 { 328 char buf[BUFSIZ]; 329 int nr = -1; 330 331 if (remerr != -1) 332 if (ioctl(remerr, FIONREAD, &nr) != -1) { 333 if (nr >= sizeof(buf)) 334 nr = sizeof(buf) - 1; 335 if ((nr = read(remerr, buf, nr)) > 0) { 336 buf[nr] = '\0'; 337 if (buf[nr - 1] == '\n') 338 buf[--nr] = '\0'; 339 } 340 } 341 342 if (nr <= 0) 343 (void) strcpy(buf, "lost connection"); 344 345 if (iamremote) 346 cleanup(0); 347 if (lfp) 348 log(lfp, "rdist: %s\n", buf); 349 else 350 error("%s\n", buf); 351 longjmp(env, 1); 352 } 353 354 static int 355 okname(name) 356 char *name; 357 { 358 char *cp = name; 359 int c; 360 361 do { 362 c = *cp; 363 if (c & 0200) 364 goto bad; 365 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 366 goto bad; 367 cp++; 368 } while (*cp); 369 return(1); 370 bad: 371 error("invalid user name %s\n", name); 372 return(0); 373 } 374 375 time_t lastmod; 376 FILE *tfp; 377 extern char target[], *tp; 378 379 /* 380 * Process commands for comparing files to time stamp files. 381 */ 382 static void 383 dodcolon(filev, files, stamp, cmds) 384 char **filev; 385 struct namelist *files; 386 char *stamp; 387 struct subcmd *cmds; 388 { 389 struct subcmd *sc; 390 struct namelist *f; 391 char **cpp; 392 struct timeval tv[2]; 393 struct stat stb; 394 395 if (debug) 396 printf("dodcolon()\n"); 397 398 if (files == NULL) { 399 error("no files to be updated\n"); 400 return; 401 } 402 if (stat(stamp, &stb) < 0) { 403 error("%s: %s\n", stamp, strerror(errno)); 404 return; 405 } 406 if (debug) 407 printf("%s: %lu\n", stamp, (u_long)stb.st_mtime); 408 409 subcmds = cmds; 410 lastmod = stb.st_mtime; 411 if (nflag || (options & VERIFY)) 412 tfp = NULL; 413 else { 414 if ((tfp = fopen(tempfile, "w")) == NULL) { 415 error("%s: %s\n", tempfile, strerror(errno)); 416 return; 417 } 418 (void) gettimeofday(&tv[0], (struct timezone *)0); 419 tv[1] = tv[0]; 420 (void) utimes(stamp, tv); 421 } 422 423 for (f = files; f != NULL; f = f->n_next) { 424 if (filev) { 425 for (cpp = filev; *cpp; cpp++) 426 if (strcmp(f->n_name, *cpp) == 0) 427 goto found; 428 continue; 429 } 430 found: 431 tp = NULL; 432 cmptime(f->n_name); 433 } 434 435 if (tfp != NULL) 436 (void) fclose(tfp); 437 for (sc = cmds; sc != NULL; sc = sc->sc_next) 438 if (sc->sc_type == NOTIFY) 439 notify(tempfile, NULL, sc->sc_args, lastmod); 440 if (!nflag && !(options & VERIFY)) 441 (void) unlink(tempfile); 442 } 443 444 /* 445 * Compare the mtime of file to the list of time stamps. 446 */ 447 static void 448 cmptime(name) 449 char *name; 450 { 451 struct stat stb; 452 453 if (debug) 454 printf("cmptime(%s)\n", name); 455 456 if (except(name)) 457 return; 458 459 if (nflag) { 460 printf("comparing dates: %s\n", name); 461 return; 462 } 463 464 /* 465 * first time cmptime() is called? 466 */ 467 if (tp == NULL) { 468 if (exptilde(target, name) == NULL) 469 return; 470 tp = name = target; 471 while (*tp) 472 tp++; 473 } 474 if (access(name, 4) < 0 || stat(name, &stb) < 0) { 475 error("%s: %s\n", name, strerror(errno)); 476 return; 477 } 478 479 switch (stb.st_mode & S_IFMT) { 480 case S_IFREG: 481 break; 482 483 case S_IFDIR: 484 rcmptime(&stb); 485 return; 486 487 default: 488 error("%s: not a plain file\n", name); 489 return; 490 } 491 492 if (stb.st_mtime > lastmod) 493 log(tfp, "new: %s\n", name); 494 } 495 496 static void 497 rcmptime(st) 498 struct stat *st; 499 { 500 DIR *d; 501 struct dirent *dp; 502 char *cp; 503 char *otp; 504 int len; 505 506 if (debug) 507 printf("rcmptime(%lx)\n", (long)st); 508 509 if ((d = opendir(target)) == NULL) { 510 error("%s: %s\n", target, strerror(errno)); 511 return; 512 } 513 otp = tp; 514 len = tp - target; 515 while ((dp = readdir(d)) != NULL) { 516 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) 517 continue; 518 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) { 519 error("%s/%s: Name too long\n", target, dp->d_name); 520 continue; 521 } 522 tp = otp; 523 *tp++ = '/'; 524 cp = dp->d_name; 525 while ((*tp++ = *cp++) != 0) 526 ; 527 tp--; 528 cmptime(target); 529 } 530 closedir(d); 531 tp = otp; 532 *tp = '\0'; 533 } 534 535 /* 536 * Notify the list of people the changes that were made. 537 * rhost == NULL if we are mailing a list of changes compared to at time 538 * stamp file. 539 */ 540 static void 541 notify(file, rhost, to, lmod) 542 char *file, *rhost; 543 struct namelist *to; 544 time_t lmod; 545 { 546 int fd, len; 547 struct stat stb; 548 FILE *pf; 549 550 if ((options & VERIFY) || to == NULL) 551 return; 552 if (!qflag) { 553 printf("notify "); 554 if (rhost) 555 printf("@%s ", rhost); 556 prnames(to); 557 } 558 if (nflag) 559 return; 560 561 if ((fd = open(file, 0)) < 0) { 562 error("%s: %s\n", file, strerror(errno)); 563 return; 564 } 565 if (fstat(fd, &stb) < 0) { 566 error("%s: %s\n", file, strerror(errno)); 567 (void) close(fd); 568 return; 569 } 570 if (stb.st_size == 0) { 571 (void) close(fd); 572 return; 573 } 574 /* 575 * Create a pipe to mailling program. 576 */ 577 (void)snprintf(buf, sizeof(buf), "%s -oi -t", _PATH_SENDMAIL); 578 pf = popen(buf, "w"); 579 if (pf == NULL) { 580 error("notify: \"%s\" failed\n", _PATH_SENDMAIL); 581 (void) close(fd); 582 return; 583 } 584 /* 585 * Output the proper header information. 586 */ 587 fprintf(pf, "From: rdist (Remote distribution program)\n"); 588 fprintf(pf, "To:"); 589 if (!any('@', to->n_name) && rhost != NULL) 590 fprintf(pf, " %s@%s", to->n_name, rhost); 591 else 592 fprintf(pf, " %s", to->n_name); 593 to = to->n_next; 594 while (to != NULL) { 595 if (!any('@', to->n_name) && rhost != NULL) 596 fprintf(pf, ", %s@%s", to->n_name, rhost); 597 else 598 fprintf(pf, ", %s", to->n_name); 599 to = to->n_next; 600 } 601 putc('\n', pf); 602 if (rhost != NULL) 603 fprintf(pf, "Subject: files updated by rdist from %s to %s\n", 604 host, rhost); 605 else 606 fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod)); 607 putc('\n', pf); 608 609 while ((len = read(fd, buf, BUFSIZ)) > 0) 610 (void) fwrite(buf, 1, len, pf); 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