1 /* $NetBSD: rcp.c,v 1.30 2002/11/22 21:46:02 ragge Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1990, 1992, 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 __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1992, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94"; 45 #else 46 __RCSID("$NetBSD: rcp.c,v 1.30 2002/11/22 21:46:02 ragge Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 #include <sys/time.h> 53 #include <sys/socket.h> 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/ip.h> 57 58 #include <ctype.h> 59 #include <dirent.h> 60 #include <err.h> 61 #include <errno.h> 62 #include <fcntl.h> 63 #include <netdb.h> 64 #include <pwd.h> 65 #include <signal.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <string.h> 70 #include <unistd.h> 71 72 #include "pathnames.h" 73 #include "extern.h" 74 75 #ifdef KERBEROS 76 #include <kerberosIV/des.h> 77 #include <kerberosIV/krb.h> 78 #include "krb.h" 79 80 char dst_realm_buf[REALM_SZ]; 81 char *dest_realm = NULL; 82 int use_kerberos = 1; 83 CREDENTIALS cred; 84 Key_schedule schedule; 85 #ifdef CRYPT 86 int doencrypt = 0; 87 #define OPTIONS "dfKk:prtx" 88 #else 89 #define OPTIONS "dfKk:prt" 90 #endif 91 #else 92 #define OPTIONS "dfprt" 93 #endif 94 95 struct passwd *pwd; 96 char *pwname; 97 u_short port; 98 uid_t userid; 99 int errs, rem; 100 int pflag, iamremote, iamrecursive, targetshouldbedirectory; 101 102 #define CMDNEEDS 64 103 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ 104 105 #ifdef KERBEROS 106 int kerberos __P((char **, char *, char *, char *)); 107 void oldw __P((const char *, ...)); 108 #endif 109 int response __P((void)); 110 void rsource __P((char *, struct stat *)); 111 void sink __P((int, char *[])); 112 void source __P((int, char *[])); 113 void tolocal __P((int, char *[])); 114 void toremote __P((char *, int, char *[])); 115 void usage __P((void)); 116 int main __P((int, char *[])); 117 118 int 119 main(argc, argv) 120 int argc; 121 char *argv[]; 122 { 123 struct servent *sp; 124 int ch, fflag, tflag; 125 char *targ, *shell; 126 127 fflag = tflag = 0; 128 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 129 switch(ch) { /* User-visible flags. */ 130 case 'K': 131 #ifdef KERBEROS 132 use_kerberos = 0; 133 #endif 134 break; 135 #ifdef KERBEROS 136 case 'k': 137 dest_realm = dst_realm_buf; 138 (void)strncpy(dst_realm_buf, optarg, REALM_SZ); 139 break; 140 #ifdef CRYPT 141 case 'x': 142 doencrypt = 1; 143 /* des_set_key(cred.session, schedule); */ 144 break; 145 #endif 146 #endif 147 case 'p': 148 pflag = 1; 149 break; 150 case 'r': 151 iamrecursive = 1; 152 break; 153 /* Server options. */ 154 case 'd': 155 targetshouldbedirectory = 1; 156 break; 157 case 'f': /* "from" */ 158 iamremote = 1; 159 fflag = 1; 160 break; 161 case 't': /* "to" */ 162 iamremote = 1; 163 tflag = 1; 164 break; 165 case '?': 166 default: 167 usage(); 168 } 169 argc -= optind; 170 argv += optind; 171 172 #ifdef KERBEROS 173 if (use_kerberos) { 174 #ifdef CRYPT 175 shell = doencrypt ? "ekshell" : "kshell"; 176 #else 177 shell = "kshell"; 178 #endif 179 if ((sp = getservbyname(shell, "tcp")) == NULL) { 180 use_kerberos = 0; 181 oldw("can't get entry for %s/tcp service", shell); 182 sp = getservbyname(shell = "shell", "tcp"); 183 } 184 } else 185 sp = getservbyname(shell = "shell", "tcp"); 186 #else 187 sp = getservbyname(shell = "shell", "tcp"); 188 #endif 189 if (sp == NULL) 190 errx(1, "%s/tcp: unknown service", shell); 191 port = sp->s_port; 192 193 if ((pwd = getpwuid(userid = getuid())) == NULL) 194 errx(1, "unknown user %d", (int)userid); 195 196 if ((pwname = strdup(pwd->pw_name)) == NULL) 197 err(1, NULL); 198 199 rem = STDIN_FILENO; /* XXX */ 200 201 if (fflag) { /* Follow "protocol", send data. */ 202 (void)response(); 203 source(argc, argv); 204 exit(errs); 205 } 206 207 if (tflag) { /* Receive data. */ 208 sink(argc, argv); 209 exit(errs); 210 } 211 212 if (argc < 2) 213 usage(); 214 if (argc > 2) 215 targetshouldbedirectory = 1; 216 217 rem = -1; 218 /* Command to be executed on remote system using "rsh". */ 219 #ifdef KERBEROS 220 (void)snprintf(cmd, sizeof(cmd), 221 "rcp%s%s%s%s", iamrecursive ? " -r" : "", 222 #ifdef CRYPT 223 (doencrypt && use_kerberos ? " -x" : ""), 224 #else 225 "", 226 #endif 227 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : ""); 228 #else 229 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s", 230 iamrecursive ? " -r" : "", pflag ? " -p" : "", 231 targetshouldbedirectory ? " -d" : ""); 232 #endif 233 234 (void)signal(SIGPIPE, lostconn); 235 236 if ((targ = colon(argv[argc - 1])) != NULL)/* Dest is remote host. */ 237 toremote(targ, argc, argv); 238 else { 239 tolocal(argc, argv); /* Dest is local host. */ 240 if (targetshouldbedirectory) 241 verifydir(argv[argc - 1]); 242 } 243 exit(errs); 244 /* NOTREACHED */ 245 } 246 247 void 248 toremote(targ, argc, argv) 249 char *targ, *argv[]; 250 int argc; 251 { 252 int i, len; 253 char *bp, *host, *src, *suser, *thost, *tuser; 254 255 *targ++ = 0; 256 if (*targ == 0) 257 targ = "."; 258 259 if ((thost = strchr(argv[argc - 1], '@')) != NULL) { 260 /* user@host */ 261 *thost++ = 0; 262 tuser = argv[argc - 1]; 263 if (*tuser == '\0') 264 tuser = NULL; 265 else if (!okname(tuser)) 266 exit(1); 267 } else { 268 thost = argv[argc - 1]; 269 tuser = NULL; 270 } 271 272 for (i = 0; i < argc - 1; i++) { 273 src = colon(argv[i]); 274 if (src) { /* remote to remote */ 275 *src++ = 0; 276 if (*src == 0) 277 src = "."; 278 host = strchr(argv[i], '@'); 279 len = strlen(_PATH_RSH) + strlen(argv[i]) + 280 strlen(src) + (tuser ? strlen(tuser) : 0) + 281 strlen(thost) + strlen(targ) + CMDNEEDS + 20; 282 if (!(bp = malloc(len))) 283 err(1, NULL); 284 if (host) { 285 *host++ = 0; 286 suser = argv[i]; 287 if (*suser == '\0') 288 suser = pwname; 289 else if (!okname(suser)) 290 continue; 291 (void)snprintf(bp, len, 292 "%s %s -l %s -n %s %s '%s%s%s:%s'", 293 _PATH_RSH, host, suser, cmd, src, 294 tuser ? tuser : "", tuser ? "@" : "", 295 thost, targ); 296 } else 297 (void)snprintf(bp, len, 298 "exec %s %s -n %s %s '%s%s%s:%s'", 299 _PATH_RSH, argv[i], cmd, src, 300 tuser ? tuser : "", tuser ? "@" : "", 301 thost, targ); 302 (void)susystem(bp); 303 (void)free(bp); 304 } else { /* local to remote */ 305 if (rem == -1) { 306 len = strlen(targ) + CMDNEEDS + 20; 307 if (!(bp = malloc(len))) 308 err(1, NULL); 309 (void)snprintf(bp, len, "%s -t %s", cmd, targ); 310 host = thost; 311 #ifdef KERBEROS 312 if (use_kerberos) 313 rem = kerberos(&host, bp, pwname, 314 tuser ? tuser : pwname); 315 else 316 #endif 317 rem = rcmd(&host, port, pwname, 318 tuser ? tuser : pwname, 319 bp, 0); 320 if (rem < 0) 321 exit(1); 322 if (response() < 0) 323 exit(1); 324 (void)free(bp); 325 } 326 source(1, argv+i); 327 } 328 } 329 } 330 331 void 332 tolocal(argc, argv) 333 int argc; 334 char *argv[]; 335 { 336 int i, len; 337 char *bp, *host, *src, *suser; 338 339 for (i = 0; i < argc - 1; i++) { 340 if (!(src = colon(argv[i]))) { /* Local to local. */ 341 len = strlen(_PATH_CP) + strlen(argv[i]) + 342 strlen(argv[argc - 1]) + 20; 343 if (!(bp = malloc(len))) 344 err(1, NULL); 345 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP, 346 iamrecursive ? " -r" : "", pflag ? " -p" : "", 347 argv[i], argv[argc - 1]); 348 if (susystem(bp)) 349 ++errs; 350 (void)free(bp); 351 continue; 352 } 353 *src++ = 0; 354 if (*src == 0) 355 src = "."; 356 if ((host = strchr(argv[i], '@')) == NULL) { 357 host = argv[i]; 358 suser = pwname; 359 } else { 360 *host++ = 0; 361 suser = argv[i]; 362 if (*suser == '\0') 363 suser = pwname; 364 else if (!okname(suser)) 365 continue; 366 } 367 len = strlen(src) + CMDNEEDS + 20; 368 if ((bp = malloc(len)) == NULL) 369 err(1, NULL); 370 (void)snprintf(bp, len, "%s -f %s", cmd, src); 371 rem = 372 #ifdef KERBEROS 373 use_kerberos ? 374 kerberos(&host, bp, pwname, suser) : 375 #endif 376 rcmd(&host, port, pwname, suser, bp, 0); 377 (void)free(bp); 378 if (rem < 0) { 379 ++errs; 380 continue; 381 } 382 sink(1, argv + argc - 1); 383 (void)close(rem); 384 rem = -1; 385 } 386 } 387 388 void 389 source(argc, argv) 390 int argc; 391 char *argv[]; 392 { 393 struct stat stb; 394 static BUF buffer; 395 BUF *bp; 396 off_t i; 397 int amt, fd, haderr, indx, result; 398 char *last, *name, buf[BUFSIZ]; 399 400 #ifdef __GNUC__ 401 /* This outrageous construct just to shut up a GCC warning. */ 402 (void) &i; 403 #endif 404 405 for (indx = 0; indx < argc; ++indx) { 406 name = argv[indx]; 407 if ((fd = open(name, O_RDONLY, 0)) < 0) 408 goto syserr; 409 if (fstat(fd, &stb)) { 410 syserr: run_err("%s: %s", name, strerror(errno)); 411 goto next; 412 } 413 switch (stb.st_mode & S_IFMT) { 414 case S_IFREG: 415 break; 416 case S_IFDIR: 417 if (iamrecursive) { 418 rsource(name, &stb); 419 goto next; 420 } 421 /* FALLTHROUGH */ 422 default: 423 run_err("%s: not a regular file", name); 424 goto next; 425 } 426 if ((last = strrchr(name, '/')) == NULL) 427 last = name; 428 else 429 ++last; 430 if (pflag) { 431 /* 432 * Make it compatible with possible future 433 * versions expecting microseconds. 434 */ 435 (void)snprintf(buf, sizeof(buf), "T%ld %ld %ld %ld\n", 436 (long)stb.st_mtimespec.tv_sec, 437 (long)stb.st_mtimespec.tv_nsec / 1000, 438 (long)stb.st_atimespec.tv_sec, 439 (long)stb.st_atimespec.tv_nsec / 1000); 440 (void)write(rem, buf, strlen(buf)); 441 if (response() < 0) 442 goto next; 443 } 444 #define RCPMODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) 445 (void)snprintf(buf, sizeof(buf), "C%04o %lld %s\n", 446 stb.st_mode & RCPMODEMASK, (long long)stb.st_size, last); 447 (void)write(rem, buf, strlen(buf)); 448 if (response() < 0) 449 goto next; 450 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) { 451 next: (void)close(fd); 452 continue; 453 } 454 455 /* Keep writing after an error so that we stay sync'd up. */ 456 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { 457 amt = bp->cnt; 458 if (i + amt > stb.st_size) 459 amt = stb.st_size - i; 460 if (!haderr) { 461 result = read(fd, bp->buf, amt); 462 if (result != amt) 463 haderr = result >= 0 ? EIO : errno; 464 } 465 if (haderr) 466 (void)write(rem, bp->buf, amt); 467 else { 468 result = write(rem, bp->buf, amt); 469 if (result != amt) 470 haderr = result >= 0 ? EIO : errno; 471 } 472 } 473 if (close(fd) && !haderr) 474 haderr = errno; 475 if (!haderr) 476 (void)write(rem, "", 1); 477 else 478 run_err("%s: %s", name, strerror(haderr)); 479 (void)response(); 480 } 481 } 482 483 void 484 rsource(name, statp) 485 char *name; 486 struct stat *statp; 487 { 488 DIR *dirp; 489 struct dirent *dp; 490 char *last, *vect[1], path[MAXPATHLEN]; 491 492 if (!(dirp = opendir(name))) { 493 run_err("%s: %s", name, strerror(errno)); 494 return; 495 } 496 last = strrchr(name, '/'); 497 if (last == 0) 498 last = name; 499 else 500 last++; 501 if (pflag) { 502 (void)snprintf(path, sizeof(path), "T%ld %ld %ld %ld\n", 503 (long)statp->st_mtimespec.tv_sec, 504 (long)statp->st_mtimespec.tv_nsec / 1000, 505 (long)statp->st_atimespec.tv_sec, 506 (long)statp->st_atimespec.tv_nsec / 1000); 507 (void)write(rem, path, strlen(path)); 508 if (response() < 0) { 509 closedir(dirp); 510 return; 511 } 512 } 513 (void)snprintf(path, sizeof(path), 514 "D%04o %d %s\n", statp->st_mode & RCPMODEMASK, 0, last); 515 (void)write(rem, path, strlen(path)); 516 if (response() < 0) { 517 closedir(dirp); 518 return; 519 } 520 while ((dp = readdir(dirp)) != NULL) { 521 if (dp->d_ino == 0) 522 continue; 523 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) 524 continue; 525 if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) { 526 run_err("%s/%s: name too long", name, dp->d_name); 527 continue; 528 } 529 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name); 530 vect[0] = path; 531 source(1, vect); 532 } 533 (void)closedir(dirp); 534 (void)write(rem, "E\n", 2); 535 (void)response(); 536 } 537 538 void 539 sink(argc, argv) 540 int argc; 541 char *argv[]; 542 { 543 static BUF buffer; 544 struct stat stb; 545 struct timeval tv[2]; 546 enum { YES, NO, DISPLAYED } wrerr; 547 BUF *bp; 548 off_t i, j; 549 int amt, count, exists, first, mask, mode, ofd, omode; 550 int setimes, targisdir; 551 int wrerrno = 0; /* pacify gcc */ 552 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ]; 553 off_t size; 554 555 #define atime tv[0] 556 #define mtime tv[1] 557 #define SCREWUP(str) { why = str; goto screwup; } 558 559 #ifdef __GNUC__ 560 /* This outrageous construct just to shut up a GCC warning. */ 561 (void) &i; 562 #endif 563 564 setimes = targisdir = 0; 565 mask = umask(0); 566 if (!pflag) 567 (void)umask(mask); 568 if (argc != 1) { 569 run_err("ambiguous target"); 570 exit(1); 571 } 572 targ = *argv; 573 if (targetshouldbedirectory) 574 verifydir(targ); 575 (void)write(rem, "", 1); 576 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) 577 targisdir = 1; 578 for (first = 1;; first = 0) { 579 cp = buf; 580 if (read(rem, cp, 1) <= 0) 581 return; 582 if (*cp++ == '\n') 583 SCREWUP("unexpected <newline>"); 584 do { 585 if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 586 SCREWUP("lost connection"); 587 *cp++ = ch; 588 } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 589 *cp = 0; 590 591 if (buf[0] == '\01' || buf[0] == '\02') { 592 if (iamremote == 0) 593 (void)write(STDERR_FILENO, 594 buf + 1, strlen(buf + 1)); 595 if (buf[0] == '\02') 596 exit(1); 597 ++errs; 598 continue; 599 } 600 if (buf[0] == 'E') { 601 (void)write(rem, "", 1); 602 return; 603 } 604 605 if (ch == '\n') 606 *--cp = 0; 607 608 #define getnum(t) (t) = 0; while (isdigit((unsigned char)*cp)) (t) = (t) * 10 + (*cp++ - '0'); 609 cp = buf; 610 if (*cp == 'T') { 611 setimes++; 612 cp++; 613 getnum(mtime.tv_sec); 614 if (*cp++ != ' ') 615 SCREWUP("mtime.sec not delimited"); 616 getnum(mtime.tv_usec); 617 if (*cp++ != ' ') 618 SCREWUP("mtime.usec not delimited"); 619 getnum(atime.tv_sec); 620 if (*cp++ != ' ') 621 SCREWUP("atime.sec not delimited"); 622 getnum(atime.tv_usec); 623 if (*cp++ != '\0') 624 SCREWUP("atime.usec not delimited"); 625 (void)write(rem, "", 1); 626 continue; 627 } 628 if (*cp != 'C' && *cp != 'D') { 629 /* 630 * Check for the case "rcp remote:foo\* local:bar". 631 * In this case, the line "No match." can be returned 632 * by the shell before the rcp command on the remote is 633 * executed so the ^Aerror_message convention isn't 634 * followed. 635 */ 636 if (first) { 637 run_err("%s", cp); 638 exit(1); 639 } 640 SCREWUP("expected control record"); 641 } 642 mode = 0; 643 for (++cp; cp < buf + 5; cp++) { 644 if (*cp < '0' || *cp > '7') 645 SCREWUP("bad mode"); 646 mode = (mode << 3) | (*cp - '0'); 647 } 648 if (*cp++ != ' ') 649 SCREWUP("mode not delimited"); 650 651 for (size = 0; isdigit((unsigned char)*cp);) 652 size = size * 10 + (*cp++ - '0'); 653 if (*cp++ != ' ') 654 SCREWUP("size not delimited"); 655 if (targisdir) { 656 static char *namebuf; 657 static int cursize; 658 size_t need; 659 660 need = strlen(targ) + strlen(cp) + 250; 661 if (need > cursize) { 662 if (!(namebuf = malloc(need))) 663 run_err("%s", strerror(errno)); 664 } 665 (void)snprintf(namebuf, need, "%s%s%s", targ, 666 *targ ? "/" : "", cp); 667 np = namebuf; 668 } else 669 np = targ; 670 exists = stat(np, &stb) == 0; 671 if (buf[0] == 'D') { 672 int mod_flag = pflag; 673 if (exists) { 674 if (!S_ISDIR(stb.st_mode)) { 675 errno = ENOTDIR; 676 goto bad; 677 } 678 if (pflag) 679 (void)chmod(np, mode); 680 } else { 681 /* Handle copying from a read-only directory */ 682 mod_flag = 1; 683 if (mkdir(np, mode | S_IRWXU) < 0) 684 goto bad; 685 } 686 vect[0] = np; 687 sink(1, vect); 688 if (setimes) { 689 setimes = 0; 690 if (utimes(np, tv) < 0) 691 run_err("%s: set times: %s", 692 np, strerror(errno)); 693 } 694 if (mod_flag) 695 (void)chmod(np, mode); 696 continue; 697 } 698 omode = mode; 699 mode |= S_IWRITE; 700 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 701 bad: run_err("%s: %s", np, strerror(errno)); 702 continue; 703 } 704 (void)write(rem, "", 1); 705 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) { 706 (void)close(ofd); 707 continue; 708 } 709 cp = bp->buf; 710 wrerr = NO; 711 for (count = i = 0; i < size; i += BUFSIZ) { 712 amt = BUFSIZ; 713 if (i + amt > size) 714 amt = size - i; 715 count += amt; 716 do { 717 j = read(rem, cp, amt); 718 if (j <= 0) { 719 run_err("%s", j ? strerror(errno) : 720 "dropped connection"); 721 exit(1); 722 } 723 amt -= j; 724 cp += j; 725 } while (amt > 0); 726 if (count == bp->cnt) { 727 /* Keep reading so we stay sync'd up. */ 728 if (wrerr == NO) { 729 j = write(ofd, bp->buf, count); 730 if (j != count) { 731 wrerr = YES; 732 wrerrno = j >= 0 ? EIO : errno; 733 } 734 } 735 count = 0; 736 cp = bp->buf; 737 } 738 } 739 if (count != 0 && wrerr == NO && 740 (j = write(ofd, bp->buf, count)) != count) { 741 wrerr = YES; 742 wrerrno = j >= 0 ? EIO : errno; 743 } 744 if (ftruncate(ofd, size)) { 745 run_err("%s: truncate: %s", np, strerror(errno)); 746 wrerr = DISPLAYED; 747 } 748 if (pflag) { 749 if (exists || omode != mode) 750 if (fchmod(ofd, omode)) 751 run_err("%s: set mode: %s", 752 np, strerror(errno)); 753 } else { 754 if (!exists && omode != mode) 755 if (fchmod(ofd, omode & ~mask)) 756 run_err("%s: set mode: %s", 757 np, strerror(errno)); 758 } 759 #ifndef __SVR4 760 if (setimes && wrerr == NO) { 761 setimes = 0; 762 if (futimes(ofd, tv) < 0) { 763 run_err("%s: set times: %s", 764 np, strerror(errno)); 765 wrerr = DISPLAYED; 766 } 767 } 768 #endif 769 (void)close(ofd); 770 #ifdef __SVR4 771 if (setimes && wrerr == NO) { 772 setimes = 0; 773 if (utimes(np, tv) < 0) { 774 run_err("%s: set times: %s", 775 np, strerror(errno)); 776 wrerr = DISPLAYED; 777 } 778 } 779 #endif 780 (void)response(); 781 switch(wrerr) { 782 case YES: 783 run_err("%s: write: %s", np, strerror(wrerrno)); 784 break; 785 case NO: 786 (void)write(rem, "", 1); 787 break; 788 case DISPLAYED: 789 break; 790 } 791 } 792 screwup: 793 run_err("protocol error: %s", why); 794 exit(1); 795 /* NOTREACHED */ 796 } 797 798 #ifdef KERBEROS 799 int 800 kerberos(host, bp, locuser, user) 801 char **host, *bp, *locuser, *user; 802 { 803 struct servent *sp; 804 805 again: 806 if (use_kerberos) { 807 rem = KSUCCESS; 808 errno = 0; 809 if (dest_realm == NULL) 810 dest_realm = krb_realmofhost(*host); 811 rem = 812 #ifdef CRYPT 813 doencrypt ? 814 krcmd_mutual(host, 815 port, user, bp, 0, dest_realm, &cred, schedule) : 816 #endif 817 krcmd(host, port, user, bp, 0, dest_realm); 818 819 if (rem < 0) { 820 use_kerberos = 0; 821 if ((sp = getservbyname("shell", "tcp")) == NULL) 822 errx(1, "unknown service shell/tcp"); 823 if (errno == ECONNREFUSED) 824 oldw("remote host doesn't support Kerberos"); 825 else if (errno == ENOENT) 826 oldw("can't provide Kerberos authentication data"); 827 port = sp->s_port; 828 goto again; 829 } 830 } else { 831 #ifdef CRYPT 832 if (doencrypt) 833 errx(1, 834 "the -x option requires Kerberos authentication"); 835 #endif 836 rem = rcmd(host, port, locuser, user, bp, 0); 837 } 838 return (rem); 839 } 840 #endif /* KERBEROS */ 841 842 int 843 response() 844 { 845 char ch, *cp, resp, rbuf[BUFSIZ]; 846 847 if (read(rem, &resp, sizeof(resp)) != sizeof(resp)) 848 lostconn(0); 849 850 cp = rbuf; 851 switch(resp) { 852 case 0: /* ok */ 853 return (0); 854 default: 855 *cp++ = resp; 856 /* FALLTHROUGH */ 857 case 1: /* error, followed by error msg */ 858 case 2: /* fatal error, "" */ 859 do { 860 if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 861 lostconn(0); 862 *cp++ = ch; 863 } while (cp < &rbuf[BUFSIZ] && ch != '\n'); 864 865 if (!iamremote) 866 (void)write(STDERR_FILENO, rbuf, cp - rbuf); 867 ++errs; 868 if (resp == 1) 869 return (-1); 870 exit(1); 871 } 872 /* NOTREACHED */ 873 } 874 875 void 876 usage() 877 { 878 #ifdef KERBEROS 879 #ifdef CRYPT 880 (void)fprintf(stderr, "%s\n\t%s\n", 881 "usage: rcp [-Kpx] [-k realm] f1 f2", 882 "or: rcp [-Kprx] [-k realm] f1 ... fn directory"); 883 #else 884 (void)fprintf(stderr, "%s\n\t%s\n", 885 "usage: rcp [-Kp] [-k realm] f1 f2", 886 "or: rcp [-Kpr] [-k realm] f1 ... fn directory"); 887 #endif 888 #else 889 (void)fprintf(stderr, 890 "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n"); 891 #endif 892 exit(1); 893 /* NOTREACHED */ 894 } 895 896 #include <stdarg.h> 897 898 #ifdef KERBEROS 899 void 900 oldw(const char *fmt, ...) 901 { 902 va_list ap; 903 904 va_start(ap, fmt); 905 (void)fprintf(stderr, "rcp: "); 906 (void)vfprintf(stderr, fmt, ap); 907 (void)fprintf(stderr, ", using standard rcp\n"); 908 va_end(ap); 909 } 910 #endif 911 912 void 913 run_err(const char *fmt, ...) 914 { 915 static FILE *fp; 916 va_list ap; 917 918 ++errs; 919 if (fp == NULL && !(fp = fdopen(rem, "w"))) 920 return; 921 922 va_start(ap, fmt); 923 924 (void)fprintf(fp, "%c", 0x01); 925 (void)fprintf(fp, "rcp: "); 926 (void)vfprintf(fp, fmt, ap); 927 (void)fprintf(fp, "\n"); 928 (void)fflush(fp); 929 va_end(ap); 930 931 if (!iamremote) { 932 va_start(ap, fmt); 933 vwarnx(fmt, ap); 934 va_end(ap); 935 } 936 } 937