2 * Copyright (c) 1983, 1990, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char const copyright[] =
36 "@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94";
44 static const char rcsid[] =
48 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
72 #include "pathnames.h"
76 #include <openssl/des.h>
80 char dst_realm_buf[REALM_SZ];
81 char *dest_realm = NULL;
84 Key_schedule schedule;
85 extern char *krb_realmofhost();
88 #define OPTIONS "dfKk:prtx"
90 #define OPTIONS "dfKk:prt"
93 #define OPTIONS "dfprt"
100 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
102 static int argc_copy;
103 static char **argv_copy;
106 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
109 int kerberos(char **, char *, char *, char *);
110 void oldw(const char *, ...) __printflike(1, 2);
113 void rsource(char *, struct stat *);
114 void run_err(const char *, ...) __printflike(1, 2);
115 void sink(int, char *[]);
116 void source(int, char *[]);
117 void tolocal(int, char *[]);
118 void toremote(char *, int, char *[]);
122 main(int argc, char *argv[])
125 int ch, fflag, i, tflag;
132 * Prepare for execing ourselves.
134 argc_copy = argc + 1;
135 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
136 if (argv_copy == NULL)
138 argv_copy[0] = argv[0];
140 for (i = 1; i < argc; ++i) {
141 argv_copy[i + 1] = strdup(argv[i]);
142 if (argv_copy[i + 1] == NULL)
143 errx(1, "strdup: out of memory");
145 argv_copy[argc + 1] = NULL;
148 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
149 switch(ch) { /* User-visible flags. */
157 dest_realm = dst_realm_buf;
158 (void)strncpy(dst_realm_buf, optarg, REALM_SZ - 1);
159 dst_realm_buf[REALM_SZ - 1] = '\0';
164 /* des_set_key(cred.session, schedule); */
174 /* Server options. */
176 targetshouldbedirectory = 1;
178 case 'f': /* "from" */
194 k = auth_getval("auth_list");
195 if (k && !strstr(k, "kerberos"))
199 shell = doencrypt ? "ekshell" : "kshell";
203 if ((sp = getservbyname(shell, "tcp")) == NULL) {
205 oldw("can't get entry for %s/tcp service", shell);
206 sp = getservbyname(shell = "shell", "tcp");
209 sp = getservbyname(shell = "shell", "tcp");
211 sp = getservbyname(shell = "shell", "tcp");
214 errx(1, "%s/tcp: unknown service", shell);
217 if ((pwd = getpwuid(userid = getuid())) == NULL)
218 errx(1, "unknown user %d", (int)userid);
220 rem = STDIN_FILENO; /* XXX */
222 if (fflag) { /* Follow "protocol", send data. */
224 (void)setuid(userid);
229 if (tflag) { /* Receive data. */
230 (void)setuid(userid);
238 targetshouldbedirectory = 1;
241 /* Command to be executed on remote system using "rsh". */
243 (void)snprintf(cmd, sizeof(cmd),
244 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
246 (doencrypt && use_kerberos ? " -x" : ""),
250 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
252 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
253 iamrecursive ? " -r" : "", pflag ? " -p" : "",
254 targetshouldbedirectory ? " -d" : "");
257 (void)signal(SIGPIPE, lostconn);
259 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
260 toremote(targ, argc, argv);
262 tolocal(argc, argv); /* Dest is local host. */
263 if (targetshouldbedirectory)
264 verifydir(argv[argc - 1]);
270 toremote(char *targ, int argc, char *argv[])
273 char *bp, *host, *src, *suser, *thost, *tuser;
279 if ((thost = strchr(argv[argc - 1], '@'))) {
282 tuser = argv[argc - 1];
285 else if (!okname(tuser))
288 thost = argv[argc - 1];
292 for (i = 0; i < argc - 1; i++) {
293 src = colon(argv[i]);
294 if (src) { /* remote to remote */
298 host = strchr(argv[i], '@');
299 len = strlen(_PATH_RSH) + strlen(argv[i]) +
300 strlen(src) + (tuser ? strlen(tuser) : 0) +
301 strlen(thost) + strlen(targ) + CMDNEEDS + 20;
302 if (!(bp = malloc(len)))
308 suser = pwd->pw_name;
309 else if (!okname(suser)) {
313 (void)snprintf(bp, len,
314 "%s %s -l %s -n %s %s '%s%s%s:%s'",
315 _PATH_RSH, host, suser, cmd, src,
316 tuser ? tuser : "", tuser ? "@" : "",
319 (void)snprintf(bp, len,
320 "exec %s %s -n %s %s '%s%s%s:%s'",
321 _PATH_RSH, argv[i], cmd, src,
322 tuser ? tuser : "", tuser ? "@" : "",
324 (void)susystem(bp, userid);
326 } else { /* local to remote */
328 len = strlen(targ) + CMDNEEDS + 20;
329 if (!(bp = malloc(len)))
331 (void)snprintf(bp, len, "%s -t %s", cmd, targ);
335 rem = kerberos(&host, bp,
337 tuser ? tuser : pwd->pw_name);
340 rem = rcmd(&host, port, pwd->pw_name,
341 tuser ? tuser : pwd->pw_name,
345 tos = IPTOS_THROUGHPUT;
346 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
347 &tos, sizeof(int)) < 0)
348 warn("TOS (ignored)");
352 (void)setuid(userid);
360 tolocal(int argc, char *argv[])
363 char *bp, *host, *src, *suser;
365 for (i = 0; i < argc - 1; i++) {
366 if (!(src = colon(argv[i]))) { /* Local to local. */
367 len = strlen(_PATH_CP) + strlen(argv[i]) +
368 strlen(argv[argc - 1]) + 20;
369 if (!(bp = malloc(len)))
371 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
372 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
373 argv[i], argv[argc - 1]);
374 if (susystem(bp, userid))
382 if ((host = strchr(argv[i], '@')) == NULL) {
384 suser = pwd->pw_name;
389 suser = pwd->pw_name;
390 else if (!okname(suser)) {
395 len = strlen(src) + CMDNEEDS + 20;
396 if ((bp = malloc(len)) == NULL)
398 (void)snprintf(bp, len, "%s -f %s", cmd, src);
402 kerberos(&host, bp, pwd->pw_name, suser) :
404 rcmd(&host, port, pwd->pw_name, suser, bp, 0);
410 (void)seteuid(userid);
411 tos = IPTOS_THROUGHPUT;
412 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
413 warn("TOS (ignored)");
414 sink(1, argv + argc - 1);
422 source(int argc, char *argv[])
428 int amt, fd, haderr, indx, result;
429 char *last, *name, buf[BUFSIZ];
431 for (indx = 0; indx < argc; ++indx) {
433 if ((fd = open(name, O_RDONLY, 0)) < 0)
435 if (fstat(fd, &stb)) {
436 syserr: run_err("%s: %s", name, strerror(errno));
439 switch (stb.st_mode & S_IFMT) {
449 run_err("%s: not a regular file", name);
452 if ((last = strrchr(name, '/')) == NULL)
458 * Make it compatible with possible future
459 * versions expecting microseconds.
461 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
462 (long)stb.st_mtimespec.tv_sec,
463 (long)stb.st_atimespec.tv_sec);
464 (void)write(rem, buf, strlen(buf));
468 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
469 (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
470 stb.st_mode & MODEMASK, stb.st_size, last);
471 (void)write(rem, buf, strlen(buf));
474 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
475 next: (void)close(fd);
479 /* Keep writing after an error so that we stay sync'd up. */
480 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
482 if (i + amt > stb.st_size)
483 amt = stb.st_size - i;
485 result = read(fd, bp->buf, amt);
487 haderr = result >= 0 ? EIO : errno;
490 (void)write(rem, bp->buf, amt);
492 result = write(rem, bp->buf, amt);
494 haderr = result >= 0 ? EIO : errno;
497 if (close(fd) && !haderr)
500 (void)write(rem, "", 1);
502 run_err("%s: %s", name, strerror(haderr));
508 rsource(char *name, struct stat *statp)
512 char *last, *vect[1], path[PATH_MAX];
514 if (!(dirp = opendir(name))) {
515 run_err("%s: %s", name, strerror(errno));
518 last = strrchr(name, '/');
524 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
525 (long)statp->st_mtimespec.tv_sec,
526 (long)statp->st_atimespec.tv_sec);
527 (void)write(rem, path, strlen(path));
528 if (response() < 0) {
533 (void)snprintf(path, sizeof(path),
534 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
535 (void)write(rem, path, strlen(path));
536 if (response() < 0) {
540 while ((dp = readdir(dirp))) {
543 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
545 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
546 run_err("%s/%s: name too long", name, dp->d_name);
549 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
553 (void)closedir(dirp);
554 (void)write(rem, "E\n", 2);
559 sink(int argc, char *argv[])
563 struct timeval tv[2];
564 enum { YES, NO, DISPLAYED } wrerr;
567 int amt, count, exists, first, mask, mode, ofd, omode;
568 int setimes, targisdir, wrerrno = 0;
569 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
573 #define SCREWUP(str) { why = str; goto screwup; }
575 setimes = targisdir = 0;
580 run_err("ambiguous target");
584 if (targetshouldbedirectory)
586 (void)write(rem, "", 1);
587 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
589 for (first = 1;; first = 0) {
591 if (read(rem, cp, 1) <= 0)
594 SCREWUP("unexpected <newline>");
596 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
597 SCREWUP("lost connection");
599 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
602 if (buf[0] == '\01' || buf[0] == '\02') {
604 (void)write(STDERR_FILENO,
605 buf + 1, strlen(buf + 1));
612 (void)write(rem, "", 1);
623 mtime.tv_sec = strtol(cp, &cp, 10);
624 if (!cp || *cp++ != ' ')
625 SCREWUP("mtime.sec not delimited");
626 mtime.tv_usec = strtol(cp, &cp, 10);
627 if (!cp || *cp++ != ' ')
628 SCREWUP("mtime.usec not delimited");
629 atime.tv_sec = strtol(cp, &cp, 10);
630 if (!cp || *cp++ != ' ')
631 SCREWUP("atime.sec not delimited");
632 atime.tv_usec = strtol(cp, &cp, 10);
633 if (!cp || *cp++ != '\0')
634 SCREWUP("atime.usec not delimited");
635 (void)write(rem, "", 1);
638 if (*cp != 'C' && *cp != 'D') {
640 * Check for the case "rcp remote:foo\* local:bar".
641 * In this case, the line "No match." can be returned
642 * by the shell before the rcp command on the remote is
643 * executed so the ^Aerror_message convention isn't
650 SCREWUP("expected control record");
653 for (++cp; cp < buf + 5; cp++) {
654 if (*cp < '0' || *cp > '7')
656 mode = (mode << 3) | (*cp - '0');
659 SCREWUP("mode not delimited");
661 for (size = 0; isdigit(*cp);)
662 size = size * 10 + (*cp++ - '0');
664 SCREWUP("size not delimited");
666 static char *namebuf;
670 need = strlen(targ) + strlen(cp) + 250;
671 if (need > cursize) {
672 if (!(namebuf = malloc(need)))
673 run_err("%s", strerror(errno));
675 (void)snprintf(namebuf, need, "%s%s%s", targ,
676 *targ ? "/" : "", cp);
680 exists = stat(np, &stb) == 0;
682 int mod_flag = pflag;
684 if (!S_ISDIR(stb.st_mode)) {
689 (void)chmod(np, mode);
691 /* Handle copying from a read-only directory */
693 if (mkdir(np, mode | S_IRWXU) < 0)
700 if (utimes(np, tv) < 0)
701 run_err("%s: set times: %s",
702 np, strerror(errno));
705 (void)chmod(np, mode);
710 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
711 bad: run_err("%s: %s", np, strerror(errno));
714 (void)write(rem, "", 1);
715 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
721 for (count = i = 0; i < size; i += BUFSIZ) {
727 j = read(rem, cp, amt);
729 run_err("%s", j ? strerror(errno) :
730 "dropped connection");
736 if (count == bp->cnt) {
737 /* Keep reading so we stay sync'd up. */
739 j = write(ofd, bp->buf, count);
742 wrerrno = j >= 0 ? EIO : errno;
749 if (count != 0 && wrerr == NO &&
750 (j = write(ofd, bp->buf, count)) != count) {
752 wrerrno = j >= 0 ? EIO : errno;
754 if (ftruncate(ofd, size)) {
755 run_err("%s: truncate: %s", np, strerror(errno));
759 if (exists || omode != mode)
760 if (fchmod(ofd, omode))
761 run_err("%s: set mode: %s",
762 np, strerror(errno));
764 if (!exists && omode != mode)
765 if (fchmod(ofd, omode & ~mask))
766 run_err("%s: set mode: %s",
767 np, strerror(errno));
771 if (setimes && wrerr == NO) {
773 if (utimes(np, tv) < 0) {
774 run_err("%s: set times: %s",
775 np, strerror(errno));
781 run_err("%s: %s", np, strerror(wrerrno));
784 (void)write(rem, "", 1);
791 run_err("protocol error: %s", why);
797 kerberos(char **host, char *bp, char *locuser, char *user)
803 if (dest_realm == NULL)
804 dest_realm = krb_realmofhost(*host);
809 port, user, bp, 0, dest_realm, &cred, schedule) :
811 krcmd(host, port, user, bp, 0, dest_realm);
814 if (errno == ECONNREFUSED)
815 oldw("remote host doesn't support Kerberos");
816 else if (errno == ENOENT)
817 oldw("can't provide Kerberos authentication data");
818 execv(_PATH_RCP, argv_copy);
819 err(1, "execv: %s", _PATH_RCP);
825 "the -x option requires Kerberos authentication");
827 rem = rcmd(host, port, locuser, user, bp, 0);
831 #endif /* KERBEROS */
836 char ch, *cp, resp, rbuf[BUFSIZ];
838 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
848 case 1: /* error, followed by error msg */
849 case 2: /* fatal error, "" */
851 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
854 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
857 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
871 (void)fprintf(stderr, "%s\n%s\n",
872 "usage: rcp [-Kpx] [-k realm] f1 f2",
873 " rcp [-Kprx] [-k realm] f1 ... fn directory");
875 (void)fprintf(stderr, "%s\n%s\n",
876 "usage: rcp [-Kp] [-k realm] f1 f2",
877 " rcp [-Kpr] [-k realm] f1 ... fn directory");
880 (void)fprintf(stderr, "%s\n%s\n",
881 "usage: rcp [-p] f1 f2",
882 " rcp [-pr] f1 ... fn directory");
891 oldw(const char *fmt, ...)
895 (void)fprintf(stderr, "rcp: ");
896 (void)vfprintf(stderr, fmt, ap);
897 (void)fprintf(stderr, ", using standard rcp\n");
903 run_err(const char *fmt, ...)
910 if (fp == NULL && !(fp = fdopen(rem, "w")))
912 (void)fprintf(fp, "%c", 0x01);
913 (void)fprintf(fp, "rcp: ");
914 (void)vfprintf(fp, fmt, ap);
915 (void)fprintf(fp, "\n");