1 /* $NetBSD: rcp.c,v 1.29 2002/05/25 23:29:17 wiz 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.29 2002/05/25 23:29:17 wiz 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, size, targisdir; 551 int wrerrno = 0; /* pacify gcc */ 552 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ]; 553 554 #define atime tv[0] 555 #define mtime tv[1] 556 #define SCREWUP(str) { why = str; goto screwup; } 557 558 #ifdef __GNUC__ 559 /* This outrageous construct just to shut up a GCC warning. */ 560 (void) &i; 561 #endif 562 563 setimes = targisdir = 0; 564 mask = umask(0); 565 if (!pflag) 566 (void)umask(mask); 567 if (argc != 1) { 568 run_err("ambiguous target"); 569 exit(1); 570 } 571 targ = *argv; 572 if (targetshouldbedirectory) 573 verifydir(targ); 574 (void)write(rem, "", 1); 575 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) 576 targisdir = 1; 577 for (first = 1;; first = 0) { 578 cp = buf; 579 if (read(rem, cp, 1) <= 0) 580 return; 581 if (*cp++ == '\n') 582 SCREWUP("unexpected <newline>"); 583 do { 584 if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 585 SCREWUP("lost connection"); 586 *cp++ = ch; 587 } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 588 *cp = 0; 589 590 if (buf[0] == '\01' || buf[0] == '\02') { 591 if (iamremote == 0) 592 (void)write(STDERR_FILENO, 593 buf + 1, strlen(buf + 1)); 594 if (buf[0] == '\02') 595 exit(1); 596 ++errs; 597 continue; 598 } 599 if (buf[0] == 'E') { 600 (void)write(rem, "", 1); 601 return; 602 } 603 604 if (ch == '\n') 605 *--cp = 0; 606 607 #define getnum(t) (t) = 0; while (isdigit((unsigned char)*cp)) (t) = (t) * 10 + (*cp++ - '0'); 608 cp = buf; 609 if (*cp == 'T') { 610 setimes++; 611 cp++; 612 getnum(mtime.tv_sec); 613 if (*cp++ != ' ') 614 SCREWUP("mtime.sec not delimited"); 615 getnum(mtime.tv_usec); 616 if (*cp++ != ' ') 617 SCREWUP("mtime.usec not delimited"); 618 getnum(atime.tv_sec); 619 if (*cp++ != ' ') 620 SCREWUP("atime.sec not delimited"); 621 getnum(atime.tv_usec); 622 if (*cp++ != '\0') 623 SCREWUP("atime.usec not delimited"); 624 (void)write(rem, "", 1); 625 continue; 626 } 627 if (*cp != 'C' && *cp != 'D') { 628 /* 629 * Check for the case "rcp remote:foo\* local:bar". 630 * In this case, the line "No match." can be returned 631 * by the shell before the rcp command on the remote is 632 * executed so the ^Aerror_message convention isn't 633 * followed. 634 */ 635 if (first) { 636 run_err("%s", cp); 637 exit(1); 638 } 639 SCREWUP("expected control record"); 640 } 641 mode = 0; 642 for (++cp; cp < buf + 5; cp++) { 643 if (*cp < '0' || *cp > '7') 644 SCREWUP("bad mode"); 645 mode = (mode << 3) | (*cp - '0'); 646 } 647 if (*cp++ != ' ') 648 SCREWUP("mode not delimited"); 649 650 for (size = 0; isdigit((unsigned char)*cp);) 651 size = size * 10 + (*cp++ - '0'); 652 if (*cp++ != ' ') 653 SCREWUP("size not delimited"); 654 if (targisdir) { 655 static char *namebuf; 656 static int cursize; 657 size_t need; 658 659 need = strlen(targ) + strlen(cp) + 250; 660 if (need > cursize) { 661 if (!(namebuf = malloc(need))) 662 run_err("%s", strerror(errno)); 663 } 664 (void)snprintf(namebuf, need, "%s%s%s", targ, 665 *targ ? "/" : "", cp); 666 np = namebuf; 667 } else 668 np = targ; 669 exists = stat(np, &stb) == 0; 670 if (buf[0] == 'D') { 671 int mod_flag = pflag; 672 if (exists) { 673 if (!S_ISDIR(stb.st_mode)) { 674 errno = ENOTDIR; 675 goto bad; 676 } 677 if (pflag) 678 (void)chmod(np, mode); 679 } else { 680 /* Handle copying from a read-only directory */ 681 mod_flag = 1; 682 if (mkdir(np, mode | S_IRWXU) < 0) 683 goto bad; 684 } 685 vect[0] = np; 686 sink(1, vect); 687 if (setimes) { 688 setimes = 0; 689 if (utimes(np, tv) < 0) 690 run_err("%s: set times: %s", 691 np, strerror(errno)); 692 } 693 if (mod_flag) 694 (void)chmod(np, mode); 695 continue; 696 } 697 omode = mode; 698 mode |= S_IWRITE; 699 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 700 bad: run_err("%s: %s", np, strerror(errno)); 701 continue; 702 } 703 (void)write(rem, "", 1); 704 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) { 705 (void)close(ofd); 706 continue; 707 } 708 cp = bp->buf; 709 wrerr = NO; 710 for (count = i = 0; i < size; i += BUFSIZ) { 711 amt = BUFSIZ; 712 if (i + amt > size) 713 amt = size - i; 714 count += amt; 715 do { 716 j = read(rem, cp, amt); 717 if (j <= 0) { 718 run_err("%s", j ? strerror(errno) : 719 "dropped connection"); 720 exit(1); 721 } 722 amt -= j; 723 cp += j; 724 } while (amt > 0); 725 if (count == bp->cnt) { 726 /* Keep reading so we stay sync'd up. */ 727 if (wrerr == NO) { 728 j = write(ofd, bp->buf, count); 729 if (j != count) { 730 wrerr = YES; 731 wrerrno = j >= 0 ? EIO : errno; 732 } 733 } 734 count = 0; 735 cp = bp->buf; 736 } 737 } 738 if (count != 0 && wrerr == NO && 739 (j = write(ofd, bp->buf, count)) != count) { 740 wrerr = YES; 741 wrerrno = j >= 0 ? EIO : errno; 742 } 743 if (ftruncate(ofd, size)) { 744 run_err("%s: truncate: %s", np, strerror(errno)); 745 wrerr = DISPLAYED; 746 } 747 if (pflag) { 748 if (exists || omode != mode) 749 if (fchmod(ofd, omode)) 750 run_err("%s: set mode: %s", 751 np, strerror(errno)); 752 } else { 753 if (!exists && omode != mode) 754 if (fchmod(ofd, omode & ~mask)) 755 run_err("%s: set mode: %s", 756 np, strerror(errno)); 757 } 758 #ifndef __SVR4 759 if (setimes && wrerr == NO) { 760 setimes = 0; 761 if (futimes(ofd, tv) < 0) { 762 run_err("%s: set times: %s", 763 np, strerror(errno)); 764 wrerr = DISPLAYED; 765 } 766 } 767 #endif 768 (void)close(ofd); 769 #ifdef __SVR4 770 if (setimes && wrerr == NO) { 771 setimes = 0; 772 if (utimes(np, tv) < 0) { 773 run_err("%s: set times: %s", 774 np, strerror(errno)); 775 wrerr = DISPLAYED; 776 } 777 } 778 #endif 779 (void)response(); 780 switch(wrerr) { 781 case YES: 782 run_err("%s: write: %s", np, strerror(wrerrno)); 783 break; 784 case NO: 785 (void)write(rem, "", 1); 786 break; 787 case DISPLAYED: 788 break; 789 } 790 } 791 screwup: 792 run_err("protocol error: %s", why); 793 exit(1); 794 /* NOTREACHED */ 795 } 796 797 #ifdef KERBEROS 798 int 799 kerberos(host, bp, locuser, user) 800 char **host, *bp, *locuser, *user; 801 { 802 struct servent *sp; 803 804 again: 805 if (use_kerberos) { 806 rem = KSUCCESS; 807 errno = 0; 808 if (dest_realm == NULL) 809 dest_realm = krb_realmofhost(*host); 810 rem = 811 #ifdef CRYPT 812 doencrypt ? 813 krcmd_mutual(host, 814 port, user, bp, 0, dest_realm, &cred, schedule) : 815 #endif 816 krcmd(host, port, user, bp, 0, dest_realm); 817 818 if (rem < 0) { 819 use_kerberos = 0; 820 if ((sp = getservbyname("shell", "tcp")) == NULL) 821 errx(1, "unknown service shell/tcp"); 822 if (errno == ECONNREFUSED) 823 oldw("remote host doesn't support Kerberos"); 824 else if (errno == ENOENT) 825 oldw("can't provide Kerberos authentication data"); 826 port = sp->s_port; 827 goto again; 828 } 829 } else { 830 #ifdef CRYPT 831 if (doencrypt) 832 errx(1, 833 "the -x option requires Kerberos authentication"); 834 #endif 835 rem = rcmd(host, port, locuser, user, bp, 0); 836 } 837 return (rem); 838 } 839 #endif /* KERBEROS */ 840 841 int 842 response() 843 { 844 char ch, *cp, resp, rbuf[BUFSIZ]; 845 846 if (read(rem, &resp, sizeof(resp)) != sizeof(resp)) 847 lostconn(0); 848 849 cp = rbuf; 850 switch(resp) { 851 case 0: /* ok */ 852 return (0); 853 default: 854 *cp++ = resp; 855 /* FALLTHROUGH */ 856 case 1: /* error, followed by error msg */ 857 case 2: /* fatal error, "" */ 858 do { 859 if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 860 lostconn(0); 861 *cp++ = ch; 862 } while (cp < &rbuf[BUFSIZ] && ch != '\n'); 863 864 if (!iamremote) 865 (void)write(STDERR_FILENO, rbuf, cp - rbuf); 866 ++errs; 867 if (resp == 1) 868 return (-1); 869 exit(1); 870 } 871 /* NOTREACHED */ 872 } 873 874 void 875 usage() 876 { 877 #ifdef KERBEROS 878 #ifdef CRYPT 879 (void)fprintf(stderr, "%s\n\t%s\n", 880 "usage: rcp [-Kpx] [-k realm] f1 f2", 881 "or: rcp [-Kprx] [-k realm] f1 ... fn directory"); 882 #else 883 (void)fprintf(stderr, "%s\n\t%s\n", 884 "usage: rcp [-Kp] [-k realm] f1 f2", 885 "or: rcp [-Kpr] [-k realm] f1 ... fn directory"); 886 #endif 887 #else 888 (void)fprintf(stderr, 889 "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n"); 890 #endif 891 exit(1); 892 /* NOTREACHED */ 893 } 894 895 #include <stdarg.h> 896 897 #ifdef KERBEROS 898 void 899 oldw(const char *fmt, ...) 900 { 901 va_list ap; 902 903 va_start(ap, fmt); 904 (void)fprintf(stderr, "rcp: "); 905 (void)vfprintf(stderr, fmt, ap); 906 (void)fprintf(stderr, ", using standard rcp\n"); 907 va_end(ap); 908 } 909 #endif 910 911 void 912 run_err(const char *fmt, ...) 913 { 914 static FILE *fp; 915 va_list ap; 916 917 ++errs; 918 if (fp == NULL && !(fp = fdopen(rem, "w"))) 919 return; 920 921 va_start(ap, fmt); 922 923 (void)fprintf(fp, "%c", 0x01); 924 (void)fprintf(fp, "rcp: "); 925 (void)vfprintf(fp, fmt, ap); 926 (void)fprintf(fp, "\n"); 927 (void)fflush(fp); 928 va_end(ap); 929 930 if (!iamremote) { 931 va_start(ap, fmt); 932 vwarnx(fmt, ap); 933 va_end(ap); 934 } 935 } 936