2 * Copyright (c) 1983, 1990, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static char const copyright[] =
43 "@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
44 The Regents of the University of California. All rights reserved.\n";
49 static char sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94";
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
83 #include <openssl/des.h>
87 char dst_realm_buf[REALM_SZ];
88 char *dest_realm = NULL;
91 Key_schedule schedule;
92 extern char *krb_realmofhost();
95 #define OPTIONS "dfKk:prtx"
97 #define OPTIONS "dfKk:prt"
100 #define OPTIONS "dfprt"
107 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
109 static int argc_copy;
110 static char **argv_copy;
113 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
116 int kerberos(char **, char *, char *, char *);
117 void oldw(const char *, ...) __printflike(1, 2);
120 void rsource(char *, struct stat *);
121 void run_err(const char *, ...) __printflike(1, 2);
122 void sink(int, char *[]);
123 void source(int, char *[]);
124 void tolocal(int, char *[]);
125 void toremote(char *, int, char *[]);
129 main(int argc, char *argv[])
132 int ch, fflag, i, tflag;
139 * Prepare for execing ourselves.
141 argc_copy = argc + 1;
142 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
143 if (argv_copy == NULL)
145 argv_copy[0] = argv[0];
147 for (i = 1; i < argc; ++i) {
148 argv_copy[i + 1] = strdup(argv[i]);
149 if (argv_copy[i + 1] == NULL)
150 errx(1, "strdup: out of memory");
152 argv_copy[argc + 1] = NULL;
155 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
156 switch(ch) { /* User-visible flags. */
164 dest_realm = dst_realm_buf;
165 (void)strncpy(dst_realm_buf, optarg, REALM_SZ - 1);
166 dst_realm_buf[REALM_SZ - 1] = '\0';
171 /* des_set_key(cred.session, schedule); */
181 /* Server options. */
183 targetshouldbedirectory = 1;
185 case 'f': /* "from" */
201 k = auth_getval("auth_list");
202 if (k && !strstr(k, "kerberos"))
206 shell = doencrypt ? "ekshell" : "kshell";
210 if ((sp = getservbyname(shell, "tcp")) == NULL) {
212 oldw("can't get entry for %s/tcp service", shell);
213 sp = getservbyname(shell = "shell", "tcp");
216 sp = getservbyname(shell = "shell", "tcp");
218 sp = getservbyname(shell = "shell", "tcp");
221 errx(1, "%s/tcp: unknown service", shell);
224 if ((pwd = getpwuid(userid = getuid())) == NULL)
225 errx(1, "unknown user %d", (int)userid);
227 rem = STDIN_FILENO; /* XXX */
229 if (fflag) { /* Follow "protocol", send data. */
231 (void)setuid(userid);
236 if (tflag) { /* Receive data. */
237 (void)setuid(userid);
245 targetshouldbedirectory = 1;
248 /* Command to be executed on remote system using "rsh". */
250 (void)snprintf(cmd, sizeof(cmd),
251 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
253 (doencrypt && use_kerberos ? " -x" : ""),
257 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
259 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
260 iamrecursive ? " -r" : "", pflag ? " -p" : "",
261 targetshouldbedirectory ? " -d" : "");
264 (void)signal(SIGPIPE, lostconn);
266 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
267 toremote(targ, argc, argv);
269 tolocal(argc, argv); /* Dest is local host. */
270 if (targetshouldbedirectory)
271 verifydir(argv[argc - 1]);
277 toremote(char *targ, int argc, char *argv[])
280 char *bp, *host, *src, *suser, *thost, *tuser;
286 if ((thost = strchr(argv[argc - 1], '@'))) {
289 tuser = argv[argc - 1];
292 else if (!okname(tuser))
295 thost = argv[argc - 1];
299 for (i = 0; i < argc - 1; i++) {
300 src = colon(argv[i]);
301 if (src) { /* remote to remote */
305 host = strchr(argv[i], '@');
306 len = strlen(_PATH_RSH) + strlen(argv[i]) +
307 strlen(src) + (tuser ? strlen(tuser) : 0) +
308 strlen(thost) + strlen(targ) + CMDNEEDS + 20;
309 if (!(bp = malloc(len)))
315 suser = pwd->pw_name;
316 else if (!okname(suser)) {
320 (void)snprintf(bp, len,
321 "%s %s -l %s -n %s %s '%s%s%s:%s'",
322 _PATH_RSH, host, suser, cmd, src,
323 tuser ? tuser : "", tuser ? "@" : "",
326 (void)snprintf(bp, len,
327 "exec %s %s -n %s %s '%s%s%s:%s'",
328 _PATH_RSH, argv[i], cmd, src,
329 tuser ? tuser : "", tuser ? "@" : "",
331 (void)susystem(bp, userid);
333 } else { /* local to remote */
335 len = strlen(targ) + CMDNEEDS + 20;
336 if (!(bp = malloc(len)))
338 (void)snprintf(bp, len, "%s -t %s", cmd, targ);
342 rem = kerberos(&host, bp,
344 tuser ? tuser : pwd->pw_name);
347 rem = rcmd(&host, port, pwd->pw_name,
348 tuser ? tuser : pwd->pw_name,
352 tos = IPTOS_THROUGHPUT;
353 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
354 &tos, sizeof(int)) < 0)
355 warn("TOS (ignored)");
359 (void)setuid(userid);
367 tolocal(int argc, char *argv[])
370 char *bp, *host, *src, *suser;
372 for (i = 0; i < argc - 1; i++) {
373 if (!(src = colon(argv[i]))) { /* Local to local. */
374 len = strlen(_PATH_CP) + strlen(argv[i]) +
375 strlen(argv[argc - 1]) + 20;
376 if (!(bp = malloc(len)))
378 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
379 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
380 argv[i], argv[argc - 1]);
381 if (susystem(bp, userid))
389 if ((host = strchr(argv[i], '@')) == NULL) {
391 suser = pwd->pw_name;
396 suser = pwd->pw_name;
397 else if (!okname(suser)) {
402 len = strlen(src) + CMDNEEDS + 20;
403 if ((bp = malloc(len)) == NULL)
405 (void)snprintf(bp, len, "%s -f %s", cmd, src);
409 kerberos(&host, bp, pwd->pw_name, suser) :
411 rcmd(&host, port, pwd->pw_name, suser, bp, 0);
417 (void)seteuid(userid);
418 tos = IPTOS_THROUGHPUT;
419 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
420 warn("TOS (ignored)");
421 sink(1, argv + argc - 1);
429 source(int argc, char *argv[])
435 int amt, fd, haderr, indx, result;
436 char *last, *name, buf[BUFSIZ];
438 for (indx = 0; indx < argc; ++indx) {
440 if ((fd = open(name, O_RDONLY, 0)) < 0)
442 if (fstat(fd, &stb)) {
443 syserr: run_err("%s: %s", name, strerror(errno));
446 switch (stb.st_mode & S_IFMT) {
456 run_err("%s: not a regular file", name);
459 if ((last = strrchr(name, '/')) == NULL)
465 * Make it compatible with possible future
466 * versions expecting microseconds.
468 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
469 (long)stb.st_mtimespec.tv_sec,
470 (long)stb.st_atimespec.tv_sec);
471 (void)write(rem, buf, strlen(buf));
475 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
476 (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
477 stb.st_mode & MODEMASK, stb.st_size, last);
478 (void)write(rem, buf, strlen(buf));
481 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
482 next: (void)close(fd);
486 /* Keep writing after an error so that we stay sync'd up. */
487 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
489 if (i + amt > stb.st_size)
490 amt = stb.st_size - i;
492 result = read(fd, bp->buf, amt);
494 haderr = result >= 0 ? EIO : errno;
497 (void)write(rem, bp->buf, amt);
499 result = write(rem, bp->buf, amt);
501 haderr = result >= 0 ? EIO : errno;
504 if (close(fd) && !haderr)
507 (void)write(rem, "", 1);
509 run_err("%s: %s", name, strerror(haderr));
515 rsource(char *name, struct stat *statp)
519 char *last, *vect[1], path[PATH_MAX];
521 if (!(dirp = opendir(name))) {
522 run_err("%s: %s", name, strerror(errno));
525 last = strrchr(name, '/');
531 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
532 (long)statp->st_mtimespec.tv_sec,
533 (long)statp->st_atimespec.tv_sec);
534 (void)write(rem, path, strlen(path));
535 if (response() < 0) {
540 (void)snprintf(path, sizeof(path),
541 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
542 (void)write(rem, path, strlen(path));
543 if (response() < 0) {
547 while ((dp = readdir(dirp))) {
550 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
552 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
553 run_err("%s/%s: name too long", name, dp->d_name);
556 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
560 (void)closedir(dirp);
561 (void)write(rem, "E\n", 2);
566 sink(int argc, char *argv[])
570 struct timeval tv[2];
571 enum { YES, NO, DISPLAYED } wrerr;
574 int amt, count, exists, first, mask, mode, ofd, omode;
575 int setimes, targisdir, wrerrno = 0;
576 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
580 #define SCREWUP(str) { why = str; goto screwup; }
582 setimes = targisdir = 0;
587 run_err("ambiguous target");
591 if (targetshouldbedirectory)
593 (void)write(rem, "", 1);
594 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
596 for (first = 1;; first = 0) {
598 if (read(rem, cp, 1) <= 0)
601 SCREWUP("unexpected <newline>");
603 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
604 SCREWUP("lost connection");
606 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
609 if (buf[0] == '\01' || buf[0] == '\02') {
611 (void)write(STDERR_FILENO,
612 buf + 1, strlen(buf + 1));
619 (void)write(rem, "", 1);
630 mtime.tv_sec = strtol(cp, &cp, 10);
631 if (!cp || *cp++ != ' ')
632 SCREWUP("mtime.sec not delimited");
633 mtime.tv_usec = strtol(cp, &cp, 10);
634 if (!cp || *cp++ != ' ')
635 SCREWUP("mtime.usec not delimited");
636 atime.tv_sec = strtol(cp, &cp, 10);
637 if (!cp || *cp++ != ' ')
638 SCREWUP("atime.sec not delimited");
639 atime.tv_usec = strtol(cp, &cp, 10);
640 if (!cp || *cp++ != '\0')
641 SCREWUP("atime.usec not delimited");
642 (void)write(rem, "", 1);
645 if (*cp != 'C' && *cp != 'D') {
647 * Check for the case "rcp remote:foo\* local:bar".
648 * In this case, the line "No match." can be returned
649 * by the shell before the rcp command on the remote is
650 * executed so the ^Aerror_message convention isn't
657 SCREWUP("expected control record");
660 for (++cp; cp < buf + 5; cp++) {
661 if (*cp < '0' || *cp > '7')
663 mode = (mode << 3) | (*cp - '0');
666 SCREWUP("mode not delimited");
668 for (size = 0; isdigit(*cp);)
669 size = size * 10 + (*cp++ - '0');
671 SCREWUP("size not delimited");
673 static char *namebuf = NULL;
674 static size_t cursize;
677 need = strlen(targ) + strlen(cp) + 250;
678 if (need > cursize) {
681 if (!(namebuf = malloc(need)))
682 run_err("%s", strerror(errno));
685 (void)snprintf(namebuf, need, "%s%s%s", targ,
686 *targ ? "/" : "", cp);
690 exists = stat(np, &stb) == 0;
692 int mod_flag = pflag;
694 if (!S_ISDIR(stb.st_mode)) {
699 (void)chmod(np, mode);
701 /* Handle copying from a read-only directory */
703 if (mkdir(np, mode | S_IRWXU) < 0)
710 if (utimes(np, tv) < 0)
711 run_err("%s: set times: %s",
712 np, strerror(errno));
715 (void)chmod(np, mode);
720 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
721 bad: run_err("%s: %s", np, strerror(errno));
724 (void)write(rem, "", 1);
725 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
731 for (count = i = 0; i < size; i += BUFSIZ) {
737 j = read(rem, cp, amt);
739 run_err("%s", j ? strerror(errno) :
740 "dropped connection");
746 if (count == bp->cnt) {
747 /* Keep reading so we stay sync'd up. */
749 j = write(ofd, bp->buf, count);
752 wrerrno = j >= 0 ? EIO : errno;
759 if (count != 0 && wrerr == NO &&
760 (j = write(ofd, bp->buf, count)) != count) {
762 wrerrno = j >= 0 ? EIO : errno;
764 if (ftruncate(ofd, size)) {
765 run_err("%s: truncate: %s", np, strerror(errno));
769 if (exists || omode != mode)
770 if (fchmod(ofd, omode))
771 run_err("%s: set mode: %s",
772 np, strerror(errno));
774 if (!exists && omode != mode)
775 if (fchmod(ofd, omode & ~mask))
776 run_err("%s: set mode: %s",
777 np, strerror(errno));
781 if (setimes && wrerr == NO) {
783 if (utimes(np, tv) < 0) {
784 run_err("%s: set times: %s",
785 np, strerror(errno));
791 run_err("%s: %s", np, strerror(wrerrno));
794 (void)write(rem, "", 1);
801 run_err("protocol error: %s", why);
807 kerberos(char **host, char *bp, char *locuser, char *user)
813 if (dest_realm == NULL)
814 dest_realm = krb_realmofhost(*host);
819 port, user, bp, 0, dest_realm, &cred, schedule) :
821 krcmd(host, port, user, bp, 0, dest_realm);
824 if (errno == ECONNREFUSED)
825 oldw("remote host doesn't support Kerberos");
826 else if (errno == ENOENT)
827 oldw("can't provide Kerberos authentication data");
828 execv(_PATH_RCP, argv_copy);
829 err(1, "execv: %s", _PATH_RCP);
835 "the -x option requires Kerberos authentication");
837 rem = rcmd(host, port, locuser, user, bp, 0);
841 #endif /* KERBEROS */
846 char ch, *cp, resp, rbuf[BUFSIZ];
848 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
858 case 1: /* error, followed by error msg */
859 case 2: /* fatal error, "" */
861 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
864 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
867 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
881 (void)fprintf(stderr, "%s\n%s\n",
882 "usage: rcp [-Kpx] [-k realm] f1 f2",
883 " rcp [-Kprx] [-k realm] f1 ... fn directory");
885 (void)fprintf(stderr, "%s\n%s\n",
886 "usage: rcp [-Kp] [-k realm] f1 f2",
887 " rcp [-Kpr] [-k realm] f1 ... fn directory");
890 (void)fprintf(stderr, "%s\n%s\n",
891 "usage: rcp [-p] f1 f2",
892 " rcp [-pr] f1 ... fn directory");
901 oldw(const char *fmt, ...)
905 (void)fprintf(stderr, "rcp: ");
906 (void)vfprintf(stderr, fmt, ap);
907 (void)fprintf(stderr, ", using standard rcp\n");
913 run_err(const char *fmt, ...)
920 if (fp == NULL && !(fp = fdopen(rem, "w")))
922 (void)fprintf(fp, "%c", 0x01);
923 (void)fprintf(fp, "rcp: ");
924 (void)vfprintf(fp, fmt, ap);
925 (void)fprintf(fp, "\n");