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