1 /* $NetBSD: rcp.c,v 1.19 1997/10/21 13:47:17 christos 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.19 1997/10/21 13:47:17 christos 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 79 char dst_realm_buf[REALM_SZ]; 80 char *dest_realm = NULL; 81 int use_kerberos = 1; 82 CREDENTIALS cred; 83 Key_schedule schedule; 84 extern char *krb_realmofhost(); 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, "%s", ""); 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 } 245 246 void 247 toremote(targ, argc, argv) 248 char *targ, *argv[]; 249 int argc; 250 { 251 int i, len; 252 char *bp, *host, *src, *suser, *thost, *tuser; 253 254 *targ++ = 0; 255 if (*targ == 0) 256 targ = "."; 257 258 if ((thost = strchr(argv[argc - 1], '@')) != NULL) { 259 /* user@host */ 260 *thost++ = 0; 261 tuser = argv[argc - 1]; 262 if (*tuser == '\0') 263 tuser = NULL; 264 else if (!okname(tuser)) 265 exit(1); 266 } else { 267 thost = argv[argc - 1]; 268 tuser = NULL; 269 } 270 271 for (i = 0; i < argc - 1; i++) { 272 src = colon(argv[i]); 273 if (src) { /* remote to remote */ 274 *src++ = 0; 275 if (*src == 0) 276 src = "."; 277 host = strchr(argv[i], '@'); 278 len = strlen(_PATH_RSH) + strlen(argv[i]) + 279 strlen(src) + (tuser ? strlen(tuser) : 0) + 280 strlen(thost) + strlen(targ) + CMDNEEDS + 20; 281 if (!(bp = malloc(len))) 282 err(1, "%s", ""); 283 if (host) { 284 *host++ = 0; 285 suser = argv[i]; 286 if (*suser == '\0') 287 suser = pwname; 288 else if (!okname(suser)) 289 continue; 290 (void)snprintf(bp, len, 291 "%s %s -l %s -n %s %s '%s%s%s:%s'", 292 _PATH_RSH, host, suser, cmd, src, 293 tuser ? tuser : "", tuser ? "@" : "", 294 thost, targ); 295 } else 296 (void)snprintf(bp, len, 297 "exec %s %s -n %s %s '%s%s%s:%s'", 298 _PATH_RSH, argv[i], cmd, src, 299 tuser ? tuser : "", tuser ? "@" : "", 300 thost, targ); 301 (void)susystem(bp); 302 (void)free(bp); 303 } else { /* local to remote */ 304 if (rem == -1) { 305 len = strlen(targ) + CMDNEEDS + 20; 306 if (!(bp = malloc(len))) 307 err(1, "%s", ""); 308 (void)snprintf(bp, len, "%s -t %s", cmd, targ); 309 host = thost; 310 #ifdef KERBEROS 311 if (use_kerberos) 312 rem = kerberos(&host, bp, pwname, 313 tuser ? tuser : pwname); 314 else 315 #endif 316 rem = rcmd(&host, port, pwname, 317 tuser ? tuser : pwname, 318 bp, 0); 319 if (rem < 0) 320 exit(1); 321 if (response() < 0) 322 exit(1); 323 (void)free(bp); 324 } 325 source(1, argv+i); 326 } 327 } 328 } 329 330 void 331 tolocal(argc, argv) 332 int argc; 333 char *argv[]; 334 { 335 int i, len; 336 char *bp, *host, *src, *suser; 337 338 for (i = 0; i < argc - 1; i++) { 339 if (!(src = colon(argv[i]))) { /* Local to local. */ 340 len = strlen(_PATH_CP) + strlen(argv[i]) + 341 strlen(argv[argc - 1]) + 20; 342 if (!(bp = malloc(len))) 343 err(1, "%s", ""); 344 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP, 345 iamrecursive ? " -r" : "", pflag ? " -p" : "", 346 argv[i], argv[argc - 1]); 347 if (susystem(bp)) 348 ++errs; 349 (void)free(bp); 350 continue; 351 } 352 *src++ = 0; 353 if (*src == 0) 354 src = "."; 355 if ((host = strchr(argv[i], '@')) == NULL) { 356 host = argv[i]; 357 suser = pwname; 358 } else { 359 *host++ = 0; 360 suser = argv[i]; 361 if (*suser == '\0') 362 suser = pwname; 363 else if (!okname(suser)) 364 continue; 365 } 366 len = strlen(src) + CMDNEEDS + 20; 367 if ((bp = malloc(len)) == NULL) 368 err(1, "%s", ""); 369 (void)snprintf(bp, len, "%s -f %s", cmd, src); 370 rem = 371 #ifdef KERBEROS 372 use_kerberos ? 373 kerberos(&host, bp, pwname, suser) : 374 #endif 375 rcmd(&host, port, pwname, suser, bp, 0); 376 (void)free(bp); 377 if (rem < 0) { 378 ++errs; 379 continue; 380 } 381 sink(1, argv + argc - 1); 382 (void)close(rem); 383 rem = -1; 384 } 385 } 386 387 void 388 source(argc, argv) 389 int argc; 390 char *argv[]; 391 { 392 struct stat stb; 393 static BUF buffer; 394 BUF *bp; 395 off_t i; 396 int amt, fd, haderr, indx, result; 397 char *last, *name, buf[BUFSIZ]; 398 399 for (indx = 0; indx < argc; ++indx) { 400 name = argv[indx]; 401 if ((fd = open(name, O_RDONLY, 0)) < 0) 402 goto syserr; 403 if (fstat(fd, &stb)) { 404 syserr: run_err("%s: %s", name, strerror(errno)); 405 goto next; 406 } 407 switch (stb.st_mode & S_IFMT) { 408 case S_IFREG: 409 break; 410 case S_IFDIR: 411 if (iamrecursive) { 412 rsource(name, &stb); 413 goto next; 414 } 415 /* FALLTHROUGH */ 416 default: 417 run_err("%s: not a regular file", name); 418 goto next; 419 } 420 if ((last = strrchr(name, '/')) == NULL) 421 last = name; 422 else 423 ++last; 424 if (pflag) { 425 /* 426 * Make it compatible with possible future 427 * versions expecting microseconds. 428 */ 429 (void)snprintf(buf, sizeof(buf), "T%ld %ld %ld %ld\n", 430 (long)stb.st_mtimespec.tv_sec, 431 (long)stb.st_mtimespec.tv_nsec / 1000, 432 (long)stb.st_atimespec.tv_sec, 433 (long)stb.st_atimespec.tv_nsec / 1000); 434 (void)write(rem, buf, strlen(buf)); 435 if (response() < 0) 436 goto next; 437 } 438 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) 439 (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n", 440 stb.st_mode & MODEMASK, (long long)stb.st_size, last); 441 (void)write(rem, buf, strlen(buf)); 442 if (response() < 0) 443 goto next; 444 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) { 445 next: (void)close(fd); 446 continue; 447 } 448 449 /* Keep writing after an error so that we stay sync'd up. */ 450 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { 451 amt = bp->cnt; 452 if (i + amt > stb.st_size) 453 amt = stb.st_size - i; 454 if (!haderr) { 455 result = read(fd, bp->buf, amt); 456 if (result != amt) 457 haderr = result >= 0 ? EIO : errno; 458 } 459 if (haderr) 460 (void)write(rem, bp->buf, amt); 461 else { 462 result = write(rem, bp->buf, amt); 463 if (result != amt) 464 haderr = result >= 0 ? EIO : errno; 465 } 466 } 467 if (close(fd) && !haderr) 468 haderr = errno; 469 if (!haderr) 470 (void)write(rem, "", 1); 471 else 472 run_err("%s: %s", name, strerror(haderr)); 473 (void)response(); 474 } 475 } 476 477 void 478 rsource(name, statp) 479 char *name; 480 struct stat *statp; 481 { 482 DIR *dirp; 483 struct dirent *dp; 484 char *last, *vect[1], path[MAXPATHLEN]; 485 486 if (!(dirp = opendir(name))) { 487 run_err("%s: %s", name, strerror(errno)); 488 return; 489 } 490 last = strrchr(name, '/'); 491 if (last == 0) 492 last = name; 493 else 494 last++; 495 if (pflag) { 496 (void)snprintf(path, sizeof(path), "T%ld %ld %ld %ld\n", 497 (long)statp->st_mtimespec.tv_sec, 498 (long)statp->st_mtimespec.tv_nsec / 1000, 499 (long)statp->st_atimespec.tv_sec, 500 (long)statp->st_atimespec.tv_nsec / 1000); 501 (void)write(rem, path, strlen(path)); 502 if (response() < 0) { 503 closedir(dirp); 504 return; 505 } 506 } 507 (void)snprintf(path, sizeof(path), 508 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last); 509 (void)write(rem, path, strlen(path)); 510 if (response() < 0) { 511 closedir(dirp); 512 return; 513 } 514 while ((dp = readdir(dirp)) != NULL) { 515 if (dp->d_ino == 0) 516 continue; 517 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) 518 continue; 519 if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) { 520 run_err("%s/%s: name too long", name, dp->d_name); 521 continue; 522 } 523 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name); 524 vect[0] = path; 525 source(1, vect); 526 } 527 (void)closedir(dirp); 528 (void)write(rem, "E\n", 2); 529 (void)response(); 530 } 531 532 void 533 sink(argc, argv) 534 int argc; 535 char *argv[]; 536 { 537 static BUF buffer; 538 struct stat stb; 539 struct timeval tv[2]; 540 enum { YES, NO, DISPLAYED } wrerr; 541 BUF *bp; 542 off_t i, j; 543 int amt, count, exists, first, mask, mode, ofd, omode; 544 int setimes, size, targisdir; 545 int wrerrno = 0; /* pacify gcc */ 546 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ]; 547 548 #define atime tv[0] 549 #define mtime tv[1] 550 #define SCREWUP(str) { why = str; goto screwup; } 551 552 setimes = targisdir = 0; 553 mask = umask(0); 554 if (!pflag) 555 (void)umask(mask); 556 if (argc != 1) { 557 run_err("ambiguous target"); 558 exit(1); 559 } 560 targ = *argv; 561 if (targetshouldbedirectory) 562 verifydir(targ); 563 (void)write(rem, "", 1); 564 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) 565 targisdir = 1; 566 for (first = 1;; first = 0) { 567 cp = buf; 568 if (read(rem, cp, 1) <= 0) 569 return; 570 if (*cp++ == '\n') 571 SCREWUP("unexpected <newline>"); 572 do { 573 if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 574 SCREWUP("lost connection"); 575 *cp++ = ch; 576 } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 577 *cp = 0; 578 579 if (buf[0] == '\01' || buf[0] == '\02') { 580 if (iamremote == 0) 581 (void)write(STDERR_FILENO, 582 buf + 1, strlen(buf + 1)); 583 if (buf[0] == '\02') 584 exit(1); 585 ++errs; 586 continue; 587 } 588 if (buf[0] == 'E') { 589 (void)write(rem, "", 1); 590 return; 591 } 592 593 if (ch == '\n') 594 *--cp = 0; 595 596 #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0'); 597 cp = buf; 598 if (*cp == 'T') { 599 setimes++; 600 cp++; 601 getnum(mtime.tv_sec); 602 if (*cp++ != ' ') 603 SCREWUP("mtime.sec not delimited"); 604 getnum(mtime.tv_usec); 605 if (*cp++ != ' ') 606 SCREWUP("mtime.usec not delimited"); 607 getnum(atime.tv_sec); 608 if (*cp++ != ' ') 609 SCREWUP("atime.sec not delimited"); 610 getnum(atime.tv_usec); 611 if (*cp++ != '\0') 612 SCREWUP("atime.usec not delimited"); 613 (void)write(rem, "", 1); 614 continue; 615 } 616 if (*cp != 'C' && *cp != 'D') { 617 /* 618 * Check for the case "rcp remote:foo\* local:bar". 619 * In this case, the line "No match." can be returned 620 * by the shell before the rcp command on the remote is 621 * executed so the ^Aerror_message convention isn't 622 * followed. 623 */ 624 if (first) { 625 run_err("%s", cp); 626 exit(1); 627 } 628 SCREWUP("expected control record"); 629 } 630 mode = 0; 631 for (++cp; cp < buf + 5; cp++) { 632 if (*cp < '0' || *cp > '7') 633 SCREWUP("bad mode"); 634 mode = (mode << 3) | (*cp - '0'); 635 } 636 if (*cp++ != ' ') 637 SCREWUP("mode not delimited"); 638 639 for (size = 0; isdigit(*cp);) 640 size = size * 10 + (*cp++ - '0'); 641 if (*cp++ != ' ') 642 SCREWUP("size not delimited"); 643 if (targisdir) { 644 static char *namebuf; 645 static int cursize; 646 size_t need; 647 648 need = strlen(targ) + strlen(cp) + 250; 649 if (need > cursize) { 650 if (!(namebuf = malloc(need))) 651 run_err("%s", strerror(errno)); 652 } 653 (void)snprintf(namebuf, need, "%s%s%s", targ, 654 *targ ? "/" : "", cp); 655 np = namebuf; 656 } else 657 np = targ; 658 exists = stat(np, &stb) == 0; 659 if (buf[0] == 'D') { 660 int mod_flag = pflag; 661 if (exists) { 662 if (!S_ISDIR(stb.st_mode)) { 663 errno = ENOTDIR; 664 goto bad; 665 } 666 if (pflag) 667 (void)chmod(np, mode); 668 } else { 669 /* Handle copying from a read-only directory */ 670 mod_flag = 1; 671 if (mkdir(np, mode | S_IRWXU) < 0) 672 goto bad; 673 } 674 vect[0] = np; 675 sink(1, vect); 676 if (setimes) { 677 setimes = 0; 678 if (utimes(np, tv) < 0) 679 run_err("%s: set times: %s", 680 np, strerror(errno)); 681 } 682 if (mod_flag) 683 (void)chmod(np, mode); 684 continue; 685 } 686 omode = mode; 687 mode |= S_IWRITE; 688 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 689 bad: run_err("%s: %s", np, strerror(errno)); 690 continue; 691 } 692 (void)write(rem, "", 1); 693 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) { 694 (void)close(ofd); 695 continue; 696 } 697 cp = bp->buf; 698 wrerr = NO; 699 for (count = i = 0; i < size; i += BUFSIZ) { 700 amt = BUFSIZ; 701 if (i + amt > size) 702 amt = size - i; 703 count += amt; 704 do { 705 j = read(rem, cp, amt); 706 if (j <= 0) { 707 run_err("%s", j ? strerror(errno) : 708 "dropped connection"); 709 exit(1); 710 } 711 amt -= j; 712 cp += j; 713 } while (amt > 0); 714 if (count == bp->cnt) { 715 /* Keep reading so we stay sync'd up. */ 716 if (wrerr == NO) { 717 j = write(ofd, bp->buf, count); 718 if (j != count) { 719 wrerr = YES; 720 wrerrno = j >= 0 ? EIO : errno; 721 } 722 } 723 count = 0; 724 cp = bp->buf; 725 } 726 } 727 if (count != 0 && wrerr == NO && 728 (j = write(ofd, bp->buf, count)) != count) { 729 wrerr = YES; 730 wrerrno = j >= 0 ? EIO : errno; 731 } 732 if (ftruncate(ofd, size)) { 733 run_err("%s: truncate: %s", np, strerror(errno)); 734 wrerr = DISPLAYED; 735 } 736 if (pflag) { 737 if (exists || omode != mode) 738 if (fchmod(ofd, omode)) 739 run_err("%s: set mode: %s", 740 np, strerror(errno)); 741 } else { 742 if (!exists && omode != mode) 743 if (fchmod(ofd, omode & ~mask)) 744 run_err("%s: set mode: %s", 745 np, strerror(errno)); 746 } 747 if (setimes && wrerr == NO) { 748 setimes = 0; 749 if (futimes(ofd, tv) < 0) { 750 run_err("%s: set times: %s", 751 np, strerror(errno)); 752 wrerr = DISPLAYED; 753 } 754 } 755 (void)close(ofd); 756 (void)response(); 757 switch(wrerr) { 758 case YES: 759 run_err("%s: write: %s", np, strerror(wrerrno)); 760 break; 761 case NO: 762 (void)write(rem, "", 1); 763 break; 764 case DISPLAYED: 765 break; 766 } 767 } 768 screwup: 769 run_err("protocol error: %s", why); 770 exit(1); 771 } 772 773 #ifdef KERBEROS 774 int 775 kerberos(host, bp, locuser, user) 776 char **host, *bp, *locuser, *user; 777 { 778 struct servent *sp; 779 780 again: 781 if (use_kerberos) { 782 rem = KSUCCESS; 783 errno = 0; 784 if (dest_realm == NULL) 785 dest_realm = krb_realmofhost(*host); 786 rem = 787 #ifdef CRYPT 788 doencrypt ? 789 krcmd_mutual(host, 790 port, user, bp, 0, dest_realm, &cred, schedule) : 791 #endif 792 krcmd(host, port, user, bp, 0, dest_realm); 793 794 if (rem < 0) { 795 use_kerberos = 0; 796 if ((sp = getservbyname("shell", "tcp")) == NULL) 797 errx(1, "unknown service shell/tcp"); 798 if (errno == ECONNREFUSED) 799 oldw("remote host doesn't support Kerberos"); 800 else if (errno == ENOENT) 801 oldw("can't provide Kerberos authentication data"); 802 port = sp->s_port; 803 goto again; 804 } 805 } else { 806 #ifdef CRYPT 807 if (doencrypt) 808 errx(1, 809 "the -x option requires Kerberos authentication"); 810 #endif 811 rem = rcmd(host, port, locuser, user, bp, 0); 812 } 813 return (rem); 814 } 815 #endif /* KERBEROS */ 816 817 int 818 response() 819 { 820 char ch, *cp, resp, rbuf[BUFSIZ]; 821 822 if (read(rem, &resp, sizeof(resp)) != sizeof(resp)) 823 lostconn(0); 824 825 cp = rbuf; 826 switch(resp) { 827 case 0: /* ok */ 828 return (0); 829 default: 830 *cp++ = resp; 831 /* FALLTHROUGH */ 832 case 1: /* error, followed by error msg */ 833 case 2: /* fatal error, "" */ 834 do { 835 if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 836 lostconn(0); 837 *cp++ = ch; 838 } while (cp < &rbuf[BUFSIZ] && ch != '\n'); 839 840 if (!iamremote) 841 (void)write(STDERR_FILENO, rbuf, cp - rbuf); 842 ++errs; 843 if (resp == 1) 844 return (-1); 845 exit(1); 846 } 847 /* NOTREACHED */ 848 } 849 850 void 851 usage() 852 { 853 #ifdef KERBEROS 854 #ifdef CRYPT 855 (void)fprintf(stderr, "%s\n\t%s\n", 856 "usage: rcp [-Kpx] [-k realm] f1 f2", 857 "or: rcp [-Kprx] [-k realm] f1 ... fn directory"); 858 #else 859 (void)fprintf(stderr, "%s\n\t%s\n", 860 "usage: rcp [-Kp] [-k realm] f1 f2", 861 "or: rcp [-Kpr] [-k realm] f1 ... fn directory"); 862 #endif 863 #else 864 (void)fprintf(stderr, 865 "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n"); 866 #endif 867 exit(1); 868 } 869 870 #if __STDC__ 871 #include <stdarg.h> 872 #else 873 #include <varargs.h> 874 #endif 875 876 #ifdef KERBEROS 877 void 878 #if __STDC__ 879 oldw(const char *fmt, ...) 880 #else 881 oldw(fmt, va_alist) 882 char *fmt; 883 va_dcl 884 #endif 885 { 886 va_list ap; 887 #if __STDC__ 888 va_start(ap, fmt); 889 #else 890 va_start(ap); 891 #endif 892 (void)fprintf(stderr, "rcp: "); 893 (void)vfprintf(stderr, fmt, ap); 894 (void)fprintf(stderr, ", using standard rcp\n"); 895 va_end(ap); 896 } 897 #endif 898 899 void 900 #if __STDC__ 901 run_err(const char *fmt, ...) 902 #else 903 run_err(fmt, va_alist) 904 char *fmt; 905 va_dcl 906 #endif 907 { 908 static FILE *fp; 909 va_list ap; 910 #if __STDC__ 911 va_start(ap, fmt); 912 #else 913 va_start(ap); 914 #endif 915 916 ++errs; 917 if (fp == NULL && !(fp = fdopen(rem, "w"))) 918 return; 919 (void)fprintf(fp, "%c", 0x01); 920 (void)fprintf(fp, "rcp: "); 921 (void)vfprintf(fp, fmt, ap); 922 (void)fprintf(fp, "\n"); 923 (void)fflush(fp); 924 925 if (!iamremote) 926 vwarnx(fmt, ap); 927 928 va_end(ap); 929 } 930