1 /* 2 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* 7 * Copyright (c) 1983 Regents of the University of California. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms are permitted 11 * provided that the above copyright notice and this paragraph are 12 * duplicated in all such forms and that any documentation, 13 * advertising materials, and other materials related to such 14 * distribution and use acknowledge that the software was developed 15 * by the University of California, Berkeley. The name of the 16 * University may not be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 */ 19 20 #include "defs.h" 21 #include <signal.h> 22 #include <string.h> 23 #include <errno.h> 24 #include <limits.h> 25 #include <ctype.h> 26 #include <krb5defs.h> 27 #include <stdarg.h> 28 29 /* 30 * If we want to write *to* the client rdist program, *from* the server 31 * side (server-side child `rdist -Server' process exec'ed off of in.rshd), 32 * we write to stdout/stderr, since there is a pipe connecting stdout/stderr 33 * to the outside world (which is why we use `wrem' and not `rem'). 34 */ 35 int wrem = 1; 36 37 #define ack() (void) write(wrem, "\0\n", 2) 38 #define err() (void) write(wrem, "\1\n", 2) 39 40 /* 41 * Set when a desread() is reqd. in response() 42 */ 43 44 struct linkbuf *ihead; /* list of files with more than one link */ 45 extern char buf[RDIST_BUFSIZ]; /* general purpose buffer */ 46 char source[RDIST_BUFSIZ]; /* base source directory name */ 47 char destination[RDIST_BUFSIZ]; /* base destination directory name */ 48 char target[RDIST_BUFSIZ]; /* target/source directory name */ 49 char *tp; /* pointer to end of target name */ 50 char *Tdest; /* pointer to last T dest */ 51 int catname; /* cat name to target name */ 52 char *stp[32]; /* stack of saved tp's for directories */ 53 int oumask; /* old umask for creating files */ 54 55 extern FILE *lfp; /* log file for mailing changes */ 56 57 void cleanup(int); 58 struct linkbuf *savelink(struct stat *, int); 59 char *strsub(char *, char *, char *); 60 61 static void comment(char *); 62 static void note(char *, ...); 63 static void hardlink(char *cmd); 64 void error(char *, ...); 65 void log(FILE *, char *, ...); 66 static void recursive_remove(struct stat *stp); 67 static void recvf(char *cmd, int type); 68 static void query(char *name); 69 static void sendf(char *rname, int opts); 70 static void rmchk(int opts); 71 static void dospecial(char *cmd); 72 static void clean(char *cp); 73 74 /* 75 * Server routine to read requests and process them. 76 * Commands are: 77 * Tname - Transmit file if out of date 78 * Vname - Verify if file out of date or not 79 * Qname - Query if file exists. Return mtime & size if it does. 80 */ 81 void 82 server(void) 83 { 84 char cmdbuf[RDIST_BUFSIZ]; 85 char *cp; 86 87 signal(SIGHUP, cleanup); 88 signal(SIGINT, cleanup); 89 signal(SIGQUIT, cleanup); 90 signal(SIGTERM, cleanup); 91 signal(SIGPIPE, cleanup); 92 93 rem = 0; 94 oumask = umask(0); 95 96 (void) sprintf(buf, "V%d\n", VERSION); 97 (void) write(wrem, buf, strlen(buf)); 98 99 for (;;) { 100 cp = cmdbuf; 101 if (read(rem, cp, 1) <= 0) 102 return; 103 if (*cp++ == '\n') { 104 error("server: expected control record\n"); 105 continue; 106 } 107 do { 108 if (read(rem, cp, 1) != 1) 109 cleanup(0); 110 } while (*cp++ != '\n' && cp < &cmdbuf[RDIST_BUFSIZ]); 111 *--cp = '\0'; 112 cp = cmdbuf; 113 switch (*cp++) { 114 case 'T': /* init target file/directory name */ 115 catname = 1; /* target should be directory */ 116 goto dotarget; 117 118 case 't': /* init target file/directory name */ 119 catname = 0; 120 dotarget: 121 if (exptilde(target, sizeof (target), cp) == NULL) 122 continue; 123 tp = target; 124 while (*tp) 125 tp++; 126 ack(); 127 continue; 128 129 case 'R': /* Transfer a regular file. */ 130 recvf(cp, S_IFREG); 131 continue; 132 133 case 'D': /* Transfer a directory. */ 134 recvf(cp, S_IFDIR); 135 continue; 136 137 case 'K': /* Transfer symbolic link. */ 138 recvf(cp, S_IFLNK); 139 continue; 140 141 case 'k': /* Transfer hard link. */ 142 hardlink(cp); 143 continue; 144 145 case 'E': /* End. (of directory) */ 146 *tp = '\0'; 147 if (catname <= 0) { 148 error("server: too many 'E's\n"); 149 continue; 150 } 151 tp = stp[--catname]; 152 *tp = '\0'; 153 ack(); 154 continue; 155 156 case 'C': /* Clean. Cleanup a directory */ 157 clean(cp); 158 continue; 159 160 case 'Q': /* Query. Does the file/directory exist? */ 161 query(cp); 162 continue; 163 164 case 'S': /* Special. Execute commands */ 165 dospecial(cp); 166 continue; 167 168 #ifdef notdef 169 /* 170 * These entries are reserved but not currently used. 171 * The intent is to allow remote hosts to have master copies. 172 * Currently, only the host rdist runs on can have masters. 173 */ 174 case 'X': /* start a new list of files to exclude */ 175 except = bp = NULL; 176 case 'x': /* add name to list of files to exclude */ 177 if (*cp == '\0') { 178 ack(); 179 continue; 180 } 181 if (*cp == '~') { 182 if (exptilde(buf, sizeof (buf), cp) == NULL) 183 continue; 184 cp = buf; 185 } 186 if (bp == NULL) 187 except = bp = expand(makeblock(NAME, cp), 188 E_VARS); 189 else 190 bp->b_next = expand(makeblock(NAME, cp), 191 E_VARS); 192 while (bp->b_next != NULL) 193 bp = bp->b_next; 194 ack(); 195 continue; 196 197 case 'I': /* Install. Transfer file if out of date. */ 198 opts = 0; 199 while (*cp >= '0' && *cp <= '7') 200 opts = (opts << 3) | (*cp++ - '0'); 201 if (*cp++ != ' ') { 202 error("server: options not delimited\n"); 203 return; 204 } 205 install(cp, opts); 206 continue; 207 208 case 'L': /* Log. save message in log file */ 209 log(lfp, cp); 210 continue; 211 #endif 212 213 case '\1': 214 nerrs++; 215 continue; 216 217 case '\2': 218 return; 219 220 default: 221 error("server: unknown command '%s'\n", cp); 222 continue; 223 case '\0': 224 continue; 225 } 226 } 227 } 228 229 /* 230 * Update the file(s) if they are different. 231 * destdir = 1 if destination should be a directory 232 * (i.e., more than one source is being copied to the same destination). 233 */ 234 void 235 install(char *src, char *dest, int destdir, int opts) 236 { 237 char *rname; 238 char destcopy[RDIST_BUFSIZ]; 239 240 if (dest == NULL) { 241 opts &= ~WHOLE; /* WHOLE mode only useful if renaming */ 242 dest = src; 243 } 244 245 if (nflag || debug) { 246 printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install", 247 opts & WHOLE ? " -w" : "", 248 opts & YOUNGER ? " -y" : "", 249 opts & COMPARE ? " -b" : "", 250 opts & REMOVE ? " -R" : "", src, dest); 251 if (nflag) 252 return; 253 } 254 255 rname = exptilde(target, sizeof (target), src); 256 if (rname == NULL) 257 return; 258 tp = target; 259 while (*tp) 260 tp++; 261 /* 262 * If we are renaming a directory and we want to preserve 263 * the directory heirarchy (-w), we must strip off the leading 264 * directory name and preserve the rest. 265 */ 266 if (opts & WHOLE) { 267 while (*rname == '/') 268 rname++; 269 destdir = 1; 270 } else { 271 rname = rindex(target, '/'); 272 if (rname == NULL) 273 rname = target; 274 else 275 rname++; 276 } 277 if (debug) 278 printf("target = %s, rname = %s\n", target, rname); 279 /* 280 * Pass the destination file/directory name to remote. 281 */ 282 if (snprintf(buf, sizeof (buf), "%c%s\n", destdir ? 'T' : 't', dest) >= 283 sizeof (buf)) { 284 error("%s: Name too long\n", dest); 285 return; 286 } 287 if (debug) 288 printf("buf = %s", buf); 289 (void) deswrite(rem, buf, strlen(buf), 0); 290 291 if (response() < 0) 292 return; 293 294 strcpy(source, src); 295 if (destdir) { 296 strcpy(destcopy, dest); 297 Tdest = destcopy; 298 strcpy(destination, rname); 299 } else { 300 strcpy(destination, dest); 301 } 302 sendf(rname, opts); 303 Tdest = 0; 304 } 305 306 #define protoname() (pw ? pw->pw_name : user) 307 #define protogroup() (gr ? gr->gr_name : group) 308 /* 309 * Transfer the file or directory in target[]. 310 * rname is the name of the file on the remote host. 311 */ 312 void 313 sendf(char *rname, int opts) 314 { 315 struct subcmd *sc; 316 struct stat stb; 317 int sizerr, f, u, len; 318 off_t i; 319 DIR *d; 320 struct dirent *dp; 321 char *otp, *cp; 322 extern struct subcmd *subcmds; 323 static char user[15], group[15]; 324 325 if (debug) 326 printf("sendf(%s, %x%s)\n", rname, opts, printb(opts, OBITS)); 327 328 if (except(target)) 329 return; 330 if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) { 331 error("%s: %s\n", target, strerror(errno)); 332 return; 333 } 334 if (index(rname, '\n')) { 335 error("file name '%s' contains an embedded newline - " 336 "can't update\n", rname); 337 return; 338 } 339 if ((u = update(rname, opts, &stb)) == 0) { 340 if ((stb.st_mode & S_IFMT) == S_IFREG && stb.st_nlink > 1) 341 (void) savelink(&stb, opts); 342 return; 343 } 344 345 if (pw == NULL || pw->pw_uid != stb.st_uid) 346 if ((pw = getpwuid(stb.st_uid)) == NULL) { 347 log(lfp, "%s: no password entry for uid %d \n", 348 target, stb.st_uid); 349 pw = NULL; 350 sprintf(user, ":%d", stb.st_uid); 351 } 352 if (gr == NULL || gr->gr_gid != stb.st_gid) 353 if ((gr = getgrgid(stb.st_gid)) == NULL) { 354 log(lfp, "%s: no name for group %d\n", 355 target, stb.st_gid); 356 gr = NULL; 357 sprintf(group, ":%d", stb.st_gid); 358 } 359 if (u == 1) { 360 if (opts & VERIFY) { 361 log(lfp, "need to install: %s\n", target); 362 goto dospecial; 363 } 364 log(lfp, "installing: %s\n", target); 365 opts &= ~(COMPARE|REMOVE); 366 } 367 368 switch (stb.st_mode & S_IFMT) { 369 case S_IFDIR: 370 if ((d = opendir(target)) == NULL) { 371 error("%s: %s\n", target, strerror(errno)); 372 return; 373 } 374 if (snprintf(buf, sizeof (buf), "D%o %04o 0 0 %s %s %s\n", 375 opts, stb.st_mode & 07777, protoname(), protogroup(), 376 rname) >= sizeof (buf)) { 377 error("%s: Name too long\n", rname); 378 closedir(d); 379 return; 380 } 381 if (debug) 382 printf("buf = %s", buf); 383 (void) deswrite(rem, buf, strlen(buf), 0); 384 if (response() < 0) { 385 closedir(d); 386 return; 387 } 388 389 if (opts & REMOVE) 390 rmchk(opts); 391 392 otp = tp; 393 len = tp - target; 394 while (dp = readdir(d)) { 395 if ((strcmp(dp->d_name, ".") == 0)|| 396 (strcmp(dp->d_name, "..") == 0)) 397 continue; 398 if ((int)(len + 1 + strlen(dp->d_name)) >= 399 (int)(RDIST_BUFSIZ - 1)) { 400 error("%.*s/%s: Name too long\n", len, target, 401 dp->d_name); 402 continue; 403 } 404 tp = otp; 405 *tp++ = '/'; 406 cp = dp->d_name; 407 while (*tp++ = *cp++) 408 ; 409 tp--; 410 sendf(dp->d_name, opts); 411 } 412 closedir(d); 413 (void) deswrite(rem, "E\n", 2, 0); 414 (void) response(); 415 tp = otp; 416 *tp = '\0'; 417 return; 418 419 case S_IFLNK: 420 if (u != 1) 421 opts |= COMPARE; 422 if (stb.st_nlink > 1) { 423 struct linkbuf *lp; 424 425 if ((lp = savelink(&stb, opts)) != NULL) { 426 /* install link */ 427 if (*lp->target == 0) 428 len = snprintf(buf, sizeof (buf), 429 "k%o %s %s\n", opts, lp->pathname, 430 rname); 431 else 432 len = snprintf(buf, sizeof (buf), 433 "k%o %s/%s %s\n", opts, lp->target, 434 lp->pathname, rname); 435 if (len >= sizeof (buf)) { 436 error("%s: Name too long\n", rname); 437 return; 438 } 439 if (debug) 440 printf("buf = %s", buf); 441 (void) deswrite(rem, buf, strlen(buf), 0); 442 (void) response(); 443 return; 444 } 445 } 446 (void) snprintf(buf, sizeof (buf), "K%o %o %ld %ld %s %s %s\n", 447 opts, stb.st_mode & 07777, stb.st_size, stb.st_mtime, 448 protoname(), protogroup(), rname); 449 if (debug) 450 printf("buf = %s", buf); 451 (void) deswrite(rem, buf, strlen(buf), 0); 452 if (response() < 0) 453 return; 454 sizerr = (readlink(target, buf, RDIST_BUFSIZ) != stb.st_size); 455 (void) deswrite(rem, buf, stb.st_size, 0); 456 if (debug) 457 printf("readlink = %.*s\n", (int)stb.st_size, buf); 458 goto done; 459 460 case S_IFREG: 461 break; 462 463 default: 464 error("%s: not a file or directory\n", target); 465 return; 466 } 467 468 if (u == 2) { 469 if (opts & VERIFY) { 470 log(lfp, "need to update: %s\n", target); 471 goto dospecial; 472 } 473 log(lfp, "updating: %s\n", target); 474 } 475 476 if (stb.st_nlink > 1) { 477 struct linkbuf *lp; 478 479 if ((lp = savelink(&stb, opts)) != NULL) { 480 /* install link */ 481 if (*lp->target == 0) 482 len = snprintf(buf, sizeof (buf), "k%o %s %s\n", 483 opts, lp->pathname, rname); 484 else 485 len = snprintf(buf, sizeof (buf), 486 "k%o %s/%s %s\n", opts, lp->target, 487 lp->pathname, rname); 488 if (len >= sizeof (buf)) { 489 error("%s: Name too long\n", rname); 490 return; 491 } 492 if (debug) 493 printf("buf = %s", buf); 494 (void) deswrite(rem, buf, strlen(buf), 0); 495 (void) response(); 496 return; 497 } 498 } 499 500 if ((f = open(target, 0)) < 0) { 501 error("%s: %s\n", target, strerror(errno)); 502 return; 503 } 504 (void) snprintf(buf, sizeof (buf), "R%o %o %ld %ld %s %s %s\n", opts, 505 stb.st_mode & 07777, stb.st_size, stb.st_mtime, 506 protoname(), protogroup(), rname); 507 if (debug) 508 printf("buf = %s", buf); 509 (void) deswrite(rem, buf, strlen(buf), 0); 510 511 if (response() < 0) { 512 (void) close(f); 513 return; 514 } 515 516 sizerr = 0; 517 518 for (i = 0; i < stb.st_size; i += RDIST_BUFSIZ) { 519 int amt = RDIST_BUFSIZ; 520 if (i + amt > stb.st_size) 521 amt = stb.st_size - i; 522 if (sizerr == 0 && read(f, buf, amt) != amt) 523 sizerr = 1; 524 (void) deswrite(rem, buf, amt, 0); 525 } 526 (void) close(f); 527 done: 528 if (sizerr) { 529 error("%s: file changed size\n", target); 530 (void) deswrite(rem, "\1\n", 2, 0); 531 } else 532 (void) deswrite(rem, "\0\n", 2, 0); 533 f = response(); 534 535 if (f < 0 || f == 0 && (opts & COMPARE)) 536 return; 537 dospecial: 538 for (sc = subcmds; sc != NULL; sc = sc->sc_next) { 539 if (sc->sc_type != SPECIAL) 540 continue; 541 if (sc->sc_args != NULL && !inlist(sc->sc_args, target)) 542 continue; 543 log(lfp, "special \"%s\"\n", sc->sc_name); 544 if (opts & VERIFY) 545 continue; 546 (void) snprintf(buf, sizeof (buf), "SFILE=%s;%s\n", target, 547 sc->sc_name); 548 if (debug) 549 printf("buf = %s", buf); 550 (void) deswrite(rem, buf, strlen(buf), 0); 551 while (response() > 0) 552 ; 553 } 554 } 555 556 struct linkbuf * 557 savelink(struct stat *stp, int opts) 558 { 559 struct linkbuf *lp; 560 561 for (lp = ihead; lp != NULL; lp = lp->nextp) 562 if (lp->inum == stp->st_ino && lp->devnum == stp->st_dev) { 563 lp->count--; 564 return (lp); 565 } 566 lp = (struct linkbuf *)malloc(sizeof (*lp)); 567 if (lp == NULL) 568 log(lfp, "out of memory, link information lost\n"); 569 else { 570 lp->nextp = ihead; 571 ihead = lp; 572 lp->inum = stp->st_ino; 573 lp->devnum = stp->st_dev; 574 lp->count = stp->st_nlink - 1; 575 576 if (strlcpy(lp->pathname, 577 opts & WHOLE ? target : strsub(source, destination, target), 578 sizeof (lp->pathname)) >= sizeof (lp->pathname)) { 579 error("%s: target name too long\n", target); 580 } 581 582 if (Tdest) { 583 if (strlcpy(lp->target, Tdest, 584 sizeof (lp->target)) >= sizeof (lp->target)) 585 error("%s: target name too long\n", Tdest); 586 } else 587 *lp->target = 0; 588 } 589 return (NULL); 590 } 591 592 /* 593 * Check to see if file needs to be updated on the remote machine. 594 * Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date 595 * and 3 if comparing binaries to determine if out of date. 596 */ 597 int 598 update(char *rname, int opts, struct stat *stp) 599 { 600 char *cp, *s; 601 off_t size; 602 time_t mtime; 603 604 if (debug) 605 printf("update(%s, %x%s, %x)\n", rname, opts, 606 printb(opts, OBITS), stp); 607 608 /* 609 * Check to see if the file exists on the remote machine. 610 */ 611 if (snprintf(buf, sizeof (buf), "Q%s\n", rname) >= sizeof (buf)) { 612 error("%s: Name too long\n", rname); 613 return (0); 614 } 615 if (debug) 616 printf("buf = %s", buf); 617 (void) deswrite(rem, buf, strlen(buf), 0); 618 again: 619 cp = s = buf; 620 more: 621 do { 622 if (desread(rem, cp, 1, 0) != 1) 623 lostconn(); 624 } while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]); 625 626 if (cp < &buf[RDIST_BUFSIZ]) 627 *cp = '\0'; 628 if (debug) { 629 printf("update reply: "); 630 switch (*s) { 631 case 'Y': 632 case 'N': 633 putchar(*s); 634 break; 635 default: 636 if (iscntrl(*s)) { 637 putchar('^'); 638 putchar('A' + *s - 1); 639 } else 640 printf("%#x", *s & 0xff); 641 break; 642 } 643 printf("%s", &s[1]); 644 } 645 646 switch (*s++) { 647 case 'Y': 648 break; 649 650 case 'N': /* file doesn't exist so install it */ 651 return (1); 652 653 case '\1': 654 nerrs++; 655 if (*s != '\n') { 656 if (!iamremote) { 657 fflush(stdout); 658 (void) write(2, s, cp - s); 659 } 660 if (lfp != NULL) 661 (void) fwrite(s, 1, cp - s, lfp); 662 } 663 if (cp == &buf[RDIST_BUFSIZ] && *(cp - 1) != '\n') { 664 /* preserve status code */ 665 cp = s; 666 s = buf; 667 goto more; 668 } 669 return (0); 670 671 case '\3': 672 *--cp = '\0'; 673 if (lfp != NULL) 674 log(lfp, "update: note: %s\n", s); 675 goto again; 676 677 default: 678 *--cp = '\0'; 679 error("update: unexpected response '%s'\n", s); 680 return (0); 681 } 682 683 if (*s == '\n') 684 return (2); 685 686 if (opts & COMPARE) 687 return (3); 688 689 size = 0; 690 while (isdigit(*s)) 691 size = size * 10 + (*s++ - '0'); 692 if (*s++ != ' ') { 693 error("update: size not delimited\n"); 694 return (0); 695 } 696 mtime = 0; 697 while (isdigit(*s)) 698 mtime = mtime * 10 + (*s++ - '0'); 699 if (*s != '\n') { 700 error("update: mtime not delimited\n"); 701 return (0); 702 } 703 /* 704 * File needs to be updated? 705 */ 706 if (opts & YOUNGER) { 707 if (stp->st_mtime == mtime) 708 return (0); 709 if (stp->st_mtime < mtime) { 710 log(lfp, "Warning: %s: remote copy is newer\n", target); 711 return (0); 712 } 713 } else if (stp->st_mtime == mtime && stp->st_size == size) 714 return (0); 715 return (2); 716 } 717 718 /* 719 * Query. Check to see if file exists. Return one of the following: 720 * N\n - doesn't exist 721 * Ysize mtime\n - exists and its a regular file (size & mtime of file) 722 * Y\n - exists and its a directory or symbolic link 723 * ^Aerror message\n 724 */ 725 static void 726 query(char *name) 727 { 728 struct stat stb; 729 730 if (catname) { 731 if (sizeof (target) - (tp - target) >= strlen(name) + 2) { 732 (void) sprintf(tp, "/%s", name); 733 } else { 734 error("%.*s/%s: Name too long\n", tp - target, 735 target, name); 736 return; 737 } 738 } 739 740 if (lstat(target, &stb) < 0) { 741 if (errno == ENOENT) 742 (void) write(wrem, "N\n", 2); 743 else 744 error("%s:%s: %s\n", host, target, strerror(errno)); 745 *tp = '\0'; 746 return; 747 } 748 749 switch (stb.st_mode & S_IFMT) { 750 case S_IFREG: 751 (void) sprintf(buf, "Y%ld %ld\n", stb.st_size, stb.st_mtime); 752 (void) write(wrem, buf, strlen(buf)); 753 break; 754 755 case S_IFLNK: 756 case S_IFDIR: 757 (void) write(wrem, "Y\n", 2); 758 break; 759 760 default: 761 error("%s: not a file or directory\n", name); 762 break; 763 } 764 *tp = '\0'; 765 } 766 767 static void 768 recvf(char *cmd, int type) 769 { 770 char *cp; 771 int f, mode, opts, wrerr, olderrno; 772 off_t i, size; 773 time_t mtime; 774 struct stat stb; 775 struct timeval tvp[2]; 776 char *owner, *group; 777 char new[RDIST_BUFSIZ]; 778 extern char *tmpname; 779 780 cp = cmd; 781 opts = 0; 782 while (*cp >= '0' && *cp <= '7') 783 opts = (opts << 3) | (*cp++ - '0'); 784 if (*cp++ != ' ') { 785 error("recvf: options not delimited\n"); 786 return; 787 } 788 mode = 0; 789 while (*cp >= '0' && *cp <= '7') 790 mode = (mode << 3) | (*cp++ - '0'); 791 if (*cp++ != ' ') { 792 error("recvf: mode not delimited\n"); 793 return; 794 } 795 size = 0; 796 while (isdigit(*cp)) 797 size = size * 10 + (*cp++ - '0'); 798 if (*cp++ != ' ') { 799 error("recvf: size not delimited\n"); 800 return; 801 } 802 mtime = 0; 803 while (isdigit(*cp)) 804 mtime = mtime * 10 + (*cp++ - '0'); 805 if (*cp++ != ' ') { 806 error("recvf: mtime not delimited\n"); 807 return; 808 } 809 owner = cp; 810 while (*cp && *cp != ' ') 811 cp++; 812 if (*cp != ' ') { 813 error("recvf: owner name not delimited\n"); 814 return; 815 } 816 *cp++ = '\0'; 817 group = cp; 818 while (*cp && *cp != ' ') 819 cp++; 820 if (*cp != ' ') { 821 error("recvf: group name not delimited\n"); 822 return; 823 } 824 *cp++ = '\0'; 825 826 if (type == S_IFDIR) { 827 int isdot; 828 829 if (strcmp(cp, ".") == 0) 830 isdot = 1; 831 else 832 isdot = 0; 833 if (catname >= sizeof (stp) / sizeof (stp[0])) { 834 error("%s:%s: too many directory levels\n", 835 host, target); 836 return; 837 } 838 stp[catname] = tp; 839 if (catname++) { 840 *tp++ = '/'; 841 while (*tp++ = *cp++) 842 ; 843 tp--; 844 } 845 if (opts & VERIFY) { 846 ack(); 847 return; 848 } 849 if (lstat(target, &stb) == 0) { 850 if (ISDIR(stb.st_mode)) { 851 if ((stb.st_mode & 07777) == mode) { 852 ack(); 853 return; 854 } 855 sendrem("%s: Warning: remote mode %o != " 856 "local mode %o", target, 857 stb.st_mode & 07777, mode); 858 return; 859 } 860 errno = ENOTDIR; 861 } else if (errno == ENOENT && (mkdir(target, mode) == 0 || 862 chkparent(target) == 0 && 863 (isdot == 1 || mkdir(target, mode) == 0))) { 864 if (chog(target, owner, group, mode) == 0) 865 ack(); 866 return; 867 } 868 error("%s:%s: %s\n", host, target, strerror(errno)); 869 tp = stp[--catname]; 870 *tp = '\0'; 871 return; 872 } 873 874 if (catname) { 875 if (sizeof (target) - (tp - target) >= strlen(cp) + 2) { 876 (void) sprintf(tp, "/%s", cp); 877 } else { 878 error("%.*s/%s: Name too long\n", tp - target, 879 target, cp); 880 return; 881 } 882 } 883 cp = rindex(target, '/'); 884 if (cp == NULL) 885 strcpy(new, tmpname); 886 else if (cp == target) 887 (void) sprintf(new, "/%s", tmpname); 888 else { 889 *cp = '\0'; 890 891 /* 892 * sizeof (target) = RDIST_BUFSIZ and sizeof (tmpname) = 11 893 * RDIST_BUFSIZ = 50*1024 is much greater than PATH_MAX that is 894 * allowed by the kernel, so it's safe to call snprintf() here 895 */ 896 (void) snprintf(new, sizeof (new), "%s/%s", target, tmpname); 897 *cp = '/'; 898 } 899 900 if (type == S_IFLNK) { 901 int j; 902 903 ack(); 904 cp = buf; 905 for (i = 0; i < size; i += j) { 906 if ((j = read(rem, cp, size - i)) <= 0) 907 cleanup(0); 908 cp += j; 909 } 910 *cp = '\0'; 911 if (response() < 0) { 912 err(); 913 return; 914 } 915 if (symlink(buf, new) < 0) { 916 if (errno != ENOENT || chkparent(new) < 0 || 917 symlink(buf, new) < 0) 918 goto badn; 919 } 920 mode &= 0777; 921 if (opts & COMPARE) { 922 char tbuf[MAXPATHLEN]; 923 924 if ((i = readlink(target, tbuf, MAXPATHLEN)) >= 0 && 925 i == size && strncmp(buf, tbuf, size) == 0) { 926 (void) unlink(new); 927 ack(); 928 return; 929 } 930 if (opts & VERIFY) 931 goto differ; 932 } 933 goto fixup; 934 } 935 936 if ((f = creat(new, mode & ~06000)) < 0) { 937 if (errno != ENOENT || chkparent(new) < 0 || 938 (f = creat(new, mode & ~06000)) < 0) 939 goto badn; 940 } 941 942 ack(); 943 wrerr = 0; 944 for (i = 0; i < size; i += RDIST_BUFSIZ) { 945 int amt = RDIST_BUFSIZ; 946 947 cp = buf; 948 if (i + amt > size) 949 amt = size - i; 950 do { 951 int j = read(rem, cp, amt); 952 if (j <= 0) { 953 (void) close(f); 954 (void) unlink(new); 955 cleanup(0); 956 } 957 amt -= j; 958 cp += j; 959 } while (amt > 0); 960 amt = RDIST_BUFSIZ; 961 if (i + amt > size) 962 amt = size - i; 963 if (wrerr == 0 && write(f, buf, amt) != amt) { 964 olderrno = errno; 965 wrerr++; 966 } 967 } 968 (void) close(f); 969 970 if (response() < 0) { 971 err(); 972 (void) unlink(new); 973 return; 974 } 975 if (wrerr) { 976 error("%s:%s: %s\n", host, new, strerror(olderrno)); 977 (void) unlink(new); 978 return; 979 } 980 if (opts & COMPARE) { 981 FILE *f1, *f2; 982 int c; 983 984 if ((f1 = fopen(target, "r")) == NULL) 985 goto badt; 986 if ((f2 = fopen(new, "r")) == NULL) { 987 badn: 988 error("%s:%s: %s\n", host, new, strerror(errno)); 989 (void) unlink(new); 990 return; 991 } 992 while ((c = getc(f1)) == getc(f2)) 993 if (c == EOF) { 994 (void) fclose(f1); 995 (void) fclose(f2); 996 (void) unlink(new); 997 ack(); 998 return; 999 } 1000 (void) fclose(f1); 1001 (void) fclose(f2); 1002 if (opts & VERIFY) { 1003 differ: 1004 (void) unlink(new); 1005 sendrem("need to update: %s", target); 1006 return; 1007 } 1008 } 1009 1010 /* 1011 * Set last modified time. For type == S_IFDIR, the lstat above filled 1012 * in stb. Otherwise, do it now. 1013 */ 1014 if (type != S_IFDIR) 1015 (void) lstat(new, &stb); 1016 tvp[0].tv_sec = stb.st_atime; /* old atime from target */ 1017 tvp[0].tv_usec = 0; 1018 tvp[1].tv_sec = mtime; 1019 tvp[1].tv_usec = 0; 1020 if (utimes(new, tvp) < 0) { 1021 note("%s:utimes failed %s: %s", host, new, strerror(errno)); 1022 } 1023 if (chog(new, owner, group, mode) < 0) { 1024 (void) unlink(new); 1025 return; 1026 } 1027 fixup: 1028 if (rename(new, target) < 0) { 1029 badt: 1030 error("%s:%s: %s\n", host, target, strerror(errno)); 1031 (void) unlink(new); 1032 return; 1033 } 1034 if (opts & COMPARE) { 1035 sendrem("updated %s", target); 1036 } else 1037 ack(); 1038 } 1039 1040 /* 1041 * Creat a hard link to existing file. 1042 */ 1043 static void 1044 hardlink(char *cmd) 1045 { 1046 char *cp; 1047 struct stat stb; 1048 char *oldname; 1049 int opts, exists = 0; 1050 char oldnamebuf[RDIST_BUFSIZ]; 1051 1052 cp = cmd; 1053 opts = 0; 1054 while (*cp >= '0' && *cp <= '7') 1055 opts = (opts << 3) | (*cp++ - '0'); 1056 if (*cp++ != ' ') { 1057 error("hardlink: options not delimited\n"); 1058 return; 1059 } 1060 oldname = cp; 1061 while (*cp && *cp != ' ') 1062 cp++; 1063 if (*cp != ' ') { 1064 error("hardlink: oldname name not delimited\n"); 1065 return; 1066 } 1067 *cp++ = '\0'; 1068 1069 if (catname) { 1070 if (sizeof (target) - (tp - target) >= strlen(cp) + 2) { 1071 (void) sprintf(tp, "/%s", cp); 1072 } else { 1073 error("%.*s/%s: Name too long\n", tp - target, 1074 target, cp); 1075 return; 1076 } 1077 } 1078 if (lstat(target, &stb) == 0) { 1079 int mode = stb.st_mode & S_IFMT; 1080 if (mode != S_IFREG && mode != S_IFLNK) { 1081 error("%s:%s: not a regular file\n", host, target); 1082 return; 1083 } 1084 exists = 1; 1085 } 1086 if (chkparent(target) < 0) { 1087 error("%s:%s: %s (no parent)\n", 1088 host, target, strerror(errno)); 1089 return; 1090 } 1091 if (opts & VERIFY) { 1092 struct stat nstb; 1093 1094 if (exists && lstat(oldname, &nstb) == 0 && 1095 nstb.st_mode == stb.st_mode && 1096 nstb.st_ino == stb.st_ino && 1097 nstb.st_dev == stb.st_dev) { 1098 ack(); 1099 return; 1100 } else { 1101 sendrem("need to update: %s", target); 1102 return; 1103 } 1104 } 1105 if (exists && (unlink(target) < 0)) { 1106 error("%s:%s: %s (unlink)\n", 1107 host, target, strerror(errno)); 1108 return; 1109 } 1110 if (*oldname == '~') 1111 oldname = exptilde(oldnamebuf, sizeof (oldnamebuf), oldname); 1112 if (link(oldname, target) < 0) { 1113 error("%s:can't link %s to %s\n", 1114 host, target, oldname); 1115 return; 1116 } 1117 ack(); 1118 } 1119 1120 /* 1121 * Check to see if parent directory exists and create one if not. 1122 */ 1123 int 1124 chkparent(char *name) 1125 { 1126 char *cp; 1127 struct stat stb; 1128 1129 cp = rindex(name, '/'); 1130 if (cp == NULL || cp == name) 1131 return (0); 1132 *cp = '\0'; 1133 if (lstat(name, &stb) < 0) { 1134 if (errno == ENOENT && chkparent(name) >= 0 && 1135 mkdir(name, 0777 & ~oumask) >= 0) { 1136 *cp = '/'; 1137 return (0); 1138 } 1139 } else if (ISDIR(stb.st_mode)) { 1140 *cp = '/'; 1141 return (0); 1142 } 1143 *cp = '/'; 1144 return (-1); 1145 } 1146 1147 /* 1148 * Change owner, group and mode of file. 1149 */ 1150 int 1151 chog(char *file, char *owner, char *group, int mode) 1152 { 1153 int i; 1154 uid_t uid, gid; 1155 extern char user[]; 1156 1157 /* 1158 * by default, set uid of file to the uid of the person running 1159 * this program. 1160 */ 1161 uid = getuid(); 1162 1163 /* 1164 * We'll use available privileges so we just try to do what 1165 * the client specifies. If the chown() fails we'll not 1166 * add the set-[ug]id bits; and if we want to add the set-[ug]id 1167 * bits and we're not permitted to do so, the OS will prevent us 1168 * from doing so. 1169 */ 1170 if (*owner == ':') { 1171 uid = atoi(owner + 1); 1172 } else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) { 1173 if ((pw = getpwnam(owner)) == NULL) { 1174 if (mode & 04000) { 1175 note("%s:%s: unknown login name, " 1176 "clearing setuid", host, owner); 1177 mode &= ~04000; 1178 } 1179 } else { 1180 uid = pw->pw_uid; 1181 } 1182 } else { 1183 uid = pw->pw_uid; 1184 } 1185 1186 if (*group == ':') { 1187 gid = atoi(group + 1); 1188 goto ok; 1189 } 1190 1191 gid = -1; 1192 if (gr == NULL || strcmp(group, gr->gr_name) != 0) { 1193 if ((*group == ':' && 1194 (getgrgid(gid = atoi(group + 1)) == NULL)) || 1195 ((gr = getgrnam(group)) == NULL)) { 1196 if (mode & 02000) { 1197 note("%s:%s: unknown group", host, group); 1198 mode &= ~02000; 1199 } 1200 } else 1201 gid = gr->gr_gid; 1202 } else 1203 gid = gr->gr_gid; 1204 ok: 1205 if (chown(file, uid, gid) < 0 || 1206 (mode & 07000) && chmod(file, mode) < 0) { 1207 note("%s: chown or chmod failed: file %s: %s", 1208 host, file, strerror(errno)); 1209 } 1210 return (0); 1211 } 1212 1213 /* 1214 * Check for files on the machine being updated that are not on the master 1215 * machine and remove them. 1216 */ 1217 static void 1218 rmchk(int opts) 1219 { 1220 char *cp, *s; 1221 struct stat stb; 1222 1223 if (debug) 1224 printf("rmchk()\n"); 1225 1226 /* 1227 * Tell the remote to clean the files from the last directory sent. 1228 */ 1229 (void) sprintf(buf, "C%o\n", opts & VERIFY); 1230 if (debug) 1231 printf("buf = %s", buf); 1232 (void) deswrite(rem, buf, strlen(buf), 0); 1233 if (response() < 0) 1234 return; 1235 for (;;) { 1236 cp = s = buf; 1237 do { 1238 if (desread(rem, cp, 1, 0) != 1) 1239 lostconn(); 1240 } while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]); 1241 1242 switch (*s++) { 1243 case 'Q': /* Query if file should be removed */ 1244 /* 1245 * Return the following codes to remove query. 1246 * N\n -- file exists - DON'T remove. 1247 * Y\n -- file doesn't exist - REMOVE. 1248 */ 1249 *--cp = '\0'; 1250 (void) sprintf(tp, "/%s", s); 1251 if (debug) 1252 printf("check %s\n", target); 1253 if (except(target)) 1254 (void) deswrite(rem, "N\n", 2, 0); 1255 else if (lstat(target, &stb) < 0) 1256 (void) deswrite(rem, "Y\n", 2, 0); 1257 else 1258 (void) deswrite(rem, "N\n", 2, 0); 1259 break; 1260 1261 case '\0': 1262 *--cp = '\0'; 1263 if (*s != '\0') 1264 log(lfp, "%s\n", s); 1265 break; 1266 1267 case 'E': 1268 *tp = '\0'; 1269 (void) deswrite(rem, "\0\n", 2, 0); 1270 return; 1271 1272 case '\1': 1273 case '\2': 1274 nerrs++; 1275 if (*s != '\n') { 1276 if (!iamremote) { 1277 fflush(stdout); 1278 (void) write(2, s, cp - s); 1279 } 1280 if (lfp != NULL) 1281 (void) fwrite(s, 1, cp - s, lfp); 1282 } 1283 if (buf[0] == '\2') 1284 lostconn(); 1285 break; 1286 1287 default: 1288 error("rmchk: unexpected response '%s'\n", buf); 1289 (void) deswrite(rem, "\1\n", 2, 0); 1290 } 1291 } 1292 } 1293 1294 /* 1295 * Check the current directory (initialized by the 'T' command to server()) 1296 * for extraneous files and remove them. 1297 */ 1298 static void 1299 clean(char *cp) 1300 { 1301 DIR *d; 1302 struct dirent *dp; 1303 struct stat stb; 1304 char *otp; 1305 int len, opts; 1306 1307 opts = 0; 1308 while (*cp >= '0' && *cp <= '7') 1309 opts = (opts << 3) | (*cp++ - '0'); 1310 if (*cp != '\0') { 1311 error("clean: options not delimited\n"); 1312 return; 1313 } 1314 if ((d = opendir(target)) == NULL) { 1315 error("%s:%s: %s\n", host, target, strerror(errno)); 1316 return; 1317 } 1318 ack(); 1319 1320 otp = tp; 1321 len = tp - target; 1322 while (dp = readdir(d)) { 1323 if ((strcmp(dp->d_name, ".") == 0) || 1324 (strcmp(dp->d_name, "..") == 0)) 1325 continue; 1326 if ((int)(len + 1 + strlen(dp->d_name)) >= 1327 (int)(RDIST_BUFSIZ - 1)) { 1328 error("%s:%s/%s: Name too long\n", 1329 host, target, dp->d_name); 1330 continue; 1331 } 1332 tp = otp; 1333 *tp++ = '/'; 1334 cp = dp->d_name; 1335 while (*tp++ = *cp++) 1336 ; 1337 tp--; 1338 if (lstat(target, &stb) < 0) { 1339 error("%s:%s: %s\n", host, target, strerror(errno)); 1340 continue; 1341 } 1342 (void) snprintf(buf, sizeof (buf), "Q%s\n", dp->d_name); 1343 (void) write(wrem, buf, strlen(buf)); 1344 cp = buf; 1345 do { 1346 if (read(rem, cp, 1) != 1) 1347 cleanup(0); 1348 } while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]); 1349 *--cp = '\0'; 1350 cp = buf; 1351 if (*cp != 'Y') 1352 continue; 1353 if (opts & VERIFY) { 1354 sendrem("need to remove: %s", target); 1355 } else 1356 (void) recursive_remove(&stb); 1357 } 1358 closedir(d); 1359 (void) write(wrem, "E\n", 2); 1360 (void) response(); 1361 tp = otp; 1362 *tp = '\0'; 1363 } 1364 1365 /* 1366 * Remove a file or directory (recursively) and send back an acknowledge 1367 * or an error message. 1368 */ 1369 static void 1370 recursive_remove(struct stat *stp) 1371 { 1372 DIR *d; 1373 struct dirent *dp; 1374 char *cp; 1375 struct stat stb; 1376 char *otp; 1377 int len; 1378 1379 switch (stp->st_mode & S_IFMT) { 1380 case S_IFREG: 1381 case S_IFLNK: 1382 if (unlink(target) < 0) 1383 goto bad; 1384 goto removed; 1385 1386 case S_IFDIR: 1387 break; 1388 1389 default: 1390 error("%s:%s: not a plain file\n", host, target); 1391 return; 1392 } 1393 1394 if ((d = opendir(target)) == NULL) 1395 goto bad; 1396 1397 otp = tp; 1398 len = tp - target; 1399 while (dp = readdir(d)) { 1400 if ((strcmp(dp->d_name, ".") == 0) || 1401 (strcmp(dp->d_name, "..") == 0)) 1402 continue; 1403 if ((int)(len + 1 + strlen(dp->d_name)) >= 1404 (int)(RDIST_BUFSIZ - 1)) { 1405 error("%s:%s/%s: Name too long\n", 1406 host, target, dp->d_name); 1407 continue; 1408 } 1409 tp = otp; 1410 *tp++ = '/'; 1411 cp = dp->d_name; 1412 while (*tp++ = *cp++) 1413 ; 1414 tp--; 1415 if (lstat(target, &stb) < 0) { 1416 error("%s:%s: %s\n", host, target, strerror(errno)); 1417 continue; 1418 } 1419 recursive_remove(&stb); 1420 } 1421 closedir(d); 1422 tp = otp; 1423 *tp = '\0'; 1424 if (rmdir(target) < 0) { 1425 bad: 1426 error("%s:%s: %s\n", host, target, strerror(errno)); 1427 return; 1428 } 1429 removed: 1430 sendrem("removed %s", target); 1431 } 1432 1433 /* 1434 * Execute a shell command to handle special cases. 1435 */ 1436 static void 1437 dospecial(char *cmd) 1438 { 1439 int fd[2], status, pid, i; 1440 char *cp, *s; 1441 char sbuf[RDIST_BUFSIZ]; 1442 1443 if (pipe(fd) < 0) { 1444 error("%s\n", strerror(errno)); 1445 return; 1446 } 1447 if ((pid = fork()) == 0) { 1448 /* 1449 * Return everything the shell commands print. 1450 */ 1451 (void) close(0); 1452 (void) close(1); 1453 (void) close(2); 1454 (void) open("/dev/null", 0); 1455 (void) dup(fd[1]); 1456 (void) dup(fd[1]); 1457 (void) close(fd[0]); 1458 (void) close(fd[1]); 1459 execl("/bin/sh", "sh", "-c", cmd, 0); 1460 _exit(127); 1461 } 1462 (void) close(fd[1]); 1463 s = sbuf; 1464 *s++ = '\0'; 1465 while ((i = read(fd[0], buf, RDIST_BUFSIZ)) > 0) { 1466 cp = buf; 1467 do { 1468 *s++ = *cp++; 1469 if (cp[-1] != '\n') { 1470 if (s < &sbuf[RDIST_BUFSIZ - 1]) 1471 continue; 1472 *s++ = '\n'; 1473 } 1474 /* 1475 * Throw away blank lines. 1476 */ 1477 if (s == &sbuf[2]) { 1478 s--; 1479 continue; 1480 } 1481 (void) write(wrem, sbuf, s - sbuf); 1482 s = &sbuf[1]; 1483 } while (--i); 1484 } 1485 if (s > &sbuf[1]) { 1486 *s++ = '\n'; 1487 (void) write(wrem, sbuf, s - sbuf); 1488 } 1489 while ((i = wait(&status)) != pid && i != -1) 1490 ; 1491 if (i == -1) 1492 status = -1; 1493 (void) close(fd[0]); 1494 if (status) 1495 error("shell returned %d\n", status); 1496 else 1497 ack(); 1498 } 1499 1500 void 1501 log(FILE *fp, char *fmt, ...) 1502 { 1503 va_list ap; 1504 1505 /* Print changes locally if not quiet mode */ 1506 if (!qflag) { 1507 va_start(ap, fmt); 1508 vprintf(fmt, ap); 1509 va_end(ap); 1510 } 1511 1512 /* Save changes (for mailing) if really updating files */ 1513 va_start(ap, fmt); 1514 if (!(options & VERIFY) && fp != NULL) 1515 vfprintf(fp, fmt, ap); 1516 va_end(ap); 1517 } 1518 1519 void 1520 error(char *fmt, ...) 1521 { 1522 va_list ap; 1523 static FILE *fp; 1524 1525 nerrs++; 1526 if (iamremote) { 1527 if (!fp && !(fp = fdopen(rem, "w"))) 1528 return; 1529 va_start(ap, fmt); 1530 (void) fprintf(fp, "%crdist: ", 0x01); 1531 (void) vfprintf(fp, fmt, ap); 1532 fflush(fp); 1533 va_end(ap); 1534 } else { 1535 va_start(ap, fmt); 1536 fflush(stdout); 1537 (void) fprintf(stderr, "rdist: "); 1538 (void) vfprintf(stderr, fmt, ap); 1539 fflush(stderr); 1540 va_end(ap); 1541 } 1542 if (lfp != NULL) { 1543 va_start(ap, fmt); 1544 (void) fprintf(lfp, "rdist: "); 1545 (void) vfprintf(lfp, fmt, ap); 1546 fflush(lfp); 1547 va_end(ap); 1548 } 1549 } 1550 1551 void 1552 fatal(char *fmt, ...) 1553 { 1554 va_list ap; 1555 static FILE *fp; 1556 1557 nerrs++; 1558 if (iamremote) { 1559 if (!fp && !(fp = fdopen(rem, "w"))) 1560 return; 1561 va_start(ap, fmt); 1562 (void) fprintf(fp, "%crdist: ", 0x02); 1563 (void) vfprintf(fp, fmt, ap); 1564 fflush(fp); 1565 va_end(ap); 1566 } else { 1567 va_start(ap, fmt); 1568 fflush(stdout); 1569 (void) fprintf(stderr, "rdist: "); 1570 (void) vfprintf(stderr, fmt, ap); 1571 fflush(stderr); 1572 va_end(ap); 1573 } 1574 if (lfp != NULL) { 1575 va_start(ap, fmt); 1576 (void) fprintf(lfp, "rdist: "); 1577 (void) vfprintf(lfp, fmt, ap); 1578 fflush(lfp); 1579 va_end(ap); 1580 } 1581 cleanup(0); 1582 } 1583 1584 int 1585 response(void) 1586 { 1587 char *cp, *s; 1588 char resp[RDIST_BUFSIZ]; 1589 1590 if (debug) 1591 printf("response()\n"); 1592 1593 cp = s = resp; 1594 more: 1595 do { 1596 if (desread(rem, cp, 1, 0) != 1) 1597 lostconn(); 1598 } while (*cp++ != '\n' && cp < &resp[RDIST_BUFSIZ]); 1599 1600 switch (*s++) { 1601 case '\0': 1602 *--cp = '\0'; 1603 if (*s != '\0') { 1604 log(lfp, "%s\n", s); 1605 return (1); 1606 } 1607 return (0); 1608 case '\3': 1609 *--cp = '\0'; 1610 log(lfp, "Note: %s\n", s); 1611 return (response()); 1612 1613 default: 1614 s--; 1615 /* FALLTHROUGH */ 1616 case '\1': 1617 case '\2': 1618 nerrs++; 1619 if (*s != '\n') { 1620 if (!iamremote) { 1621 fflush(stdout); 1622 (void) write(2, s, cp - s); 1623 } 1624 if (lfp != NULL) 1625 (void) fwrite(s, 1, cp - s, lfp); 1626 } 1627 if (cp == &resp[RDIST_BUFSIZ] && *(cp - 1) != '\n') { 1628 /* preserve status code */ 1629 cp = s; 1630 s = resp; 1631 goto more; 1632 } 1633 if (resp[0] == '\2') 1634 lostconn(); 1635 return (-1); 1636 } 1637 } 1638 1639 /* 1640 * Remove temporary files and do any cleanup operations before exiting. 1641 */ 1642 void 1643 cleanup(int arg __unused) 1644 { 1645 (void) unlink(Tmpfile); 1646 exit(1); 1647 } 1648 1649 static void 1650 note(char *fmt, ...) 1651 { 1652 va_list ap; 1653 static char buf[RDIST_BUFSIZ]; 1654 1655 va_start(ap, fmt); 1656 (void) vsnprintf(buf, sizeof (buf) - 1, fmt, ap); 1657 comment(buf); 1658 va_end(ap); 1659 } 1660 1661 static void 1662 comment(char *s) 1663 { 1664 char three = '\3'; 1665 char nl = '\n'; 1666 struct iovec iov[3]; 1667 1668 iov[0].iov_base = &three; 1669 iov[0].iov_len = sizeof (char); 1670 iov[1].iov_base = s; 1671 iov[1].iov_len = strlen(s); 1672 iov[2].iov_base = &nl; 1673 iov[2].iov_len = sizeof (char); 1674 (void) writev(rem, iov, 3); 1675 } 1676 1677 /* 1678 * Send message to other end. 1679 * N.B.: uses buf[]. 1680 */ 1681 void 1682 sendrem(char *fmt, int a1, int a2, int a3) 1683 { 1684 int len; 1685 1686 buf[0] = '\0'; 1687 len = snprintf(buf + 1, sizeof (buf) - 1, fmt, a1, a2, a3) + 2; 1688 if (len > sizeof (buf)) 1689 len = sizeof (buf); 1690 buf[len - 1] = '\n'; 1691 (void) write(wrem, buf, len); 1692 } 1693 1694 /* 1695 * strsub(old, new, s) 1696 * 1697 * Return a pointer to a new string created by replacing substring old 1698 * with substring new in string s. String s is assumed to begin with 1699 * substring old. 1700 */ 1701 char * 1702 strsub(char *old, char *new, char *s) 1703 { 1704 static char pbuf[PATH_MAX]; 1705 char *p, *q, *r, *plim; 1706 1707 /* prepend new to pbuf */ 1708 for (p = pbuf, q = new, plim = pbuf + sizeof (pbuf) - 1; 1709 /* CSTYLED */ 1710 *q && (p < plim);) 1711 *p++ = *q++; 1712 /* p now points to the byte in pbuf where more copying should begin */ 1713 1714 /* skip over the part of s which begins with old */ 1715 for (r = old, q = s; *r; q++, r++) 1716 ; 1717 /* q now points to the byte in s where more copying should begin */ 1718 1719 while (*q && (p < plim)) 1720 *p++ = *q++; 1721 *p = '\0'; 1722 1723 return (pbuf); 1724 } 1725