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 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char const copyright[] =
40 "@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
79 #define OPTIONS "46dfprt"
85 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
86 int family = PF_UNSPEC;
89 static const char **argv_copy;
91 static char period[] = ".";
94 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
97 void rsource(char *, struct stat *);
98 void sink(int, char *[]);
99 void source(int, char *[]);
100 void tolocal(int, char *[]);
101 void toremote(char *, int, char *[]);
105 main(int argc, char *argv[])
108 int ch, fflag, i, tflag;
112 * Prepare for execing ourselves.
114 argc_copy = argc + 1;
115 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
116 if (argv_copy == NULL)
118 argv_copy[0] = argv[0];
120 for (i = 1; i < argc; ++i) {
121 argv_copy[i + 1] = strdup(argv[i]);
122 if (argv_copy[i + 1] == NULL)
123 errx(1, "strdup: out of memory");
125 argv_copy[argc + 1] = NULL;
128 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
129 switch(ch) { /* User-visible flags. */
144 /* Server options. */
146 targetshouldbedirectory = 1;
148 case 'f': /* "from" */
163 sp = getservbyname("shell", "tcp");
165 errx(1, "shell/tcp: unknown service");
168 if ((pwd = getpwuid(userid = getuid())) == NULL)
169 errx(1, "unknown user %d", (int)userid);
171 rem = STDIN_FILENO; /* XXX */
173 if (fflag) { /* Follow "protocol", send data. */
175 (void)setuid(userid);
180 if (tflag) { /* Receive data. */
181 (void)setuid(userid);
189 targetshouldbedirectory = 1;
192 /* Command to be executed on remote system using "rsh". */
193 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
194 iamrecursive ? " -r" : "", pflag ? " -p" : "",
195 targetshouldbedirectory ? " -d" : "");
197 (void)signal(SIGPIPE, lostconn);
199 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
200 toremote(targ, argc, argv);
202 tolocal(argc, argv); /* Dest is local host. */
203 if (targetshouldbedirectory)
204 verifydir(argv[argc - 1]);
210 toremote(char *targ, int argc, char *argv[])
213 char *bp, *host, *src, *suser, *thost, *tuser;
219 if ((thost = strchr(argv[argc - 1], '@'))) {
222 tuser = argv[argc - 1];
225 else if (!okname(tuser))
228 thost = argv[argc - 1];
232 for (i = 0; i < argc - 1; i++) {
233 src = colon(argv[i]);
234 if (src) { /* remote to remote */
238 host = strchr(argv[i], '@');
243 suser = pwd->pw_name;
244 else if (!okname(suser)) {
249 "%s %s -l %s -n %s %s '%s%s%s:%s'",
250 _PATH_RSH, host, suser, cmd, src,
251 tuser ? tuser : "", tuser ? "@" : "",
256 "exec %s %s -n %s %s '%s%s%s:%s'",
257 _PATH_RSH, argv[i], cmd, src,
258 tuser ? tuser : "", tuser ? "@" : "",
261 (void)susystem(bp, userid);
263 } else { /* local to remote */
265 if (asprintf(&bp, "%s -t %s", cmd, targ)
269 rem = rcmd_af(&host, port,
271 tuser ? tuser : pwd->pw_name,
275 if (family == PF_INET) {
276 tos = IPTOS_THROUGHPUT;
277 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
278 &tos, sizeof(int)) < 0)
279 warn("TOS (ignored)");
284 (void)setuid(userid);
292 tolocal(int argc, char *argv[])
295 char *bp, *host, *src, *suser;
297 for (i = 0; i < argc - 1; i++) {
298 if (!(src = colon(argv[i]))) { /* Local to local. */
299 len = strlen(_PATH_CP) + strlen(argv[i]) +
300 strlen(argv[argc - 1]) + 20;
301 if (!(bp = malloc(len)))
303 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
304 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
305 argv[i], argv[argc - 1]);
306 if (susystem(bp, userid))
314 if ((host = strchr(argv[i], '@')) == NULL) {
316 suser = pwd->pw_name;
321 suser = pwd->pw_name;
322 else if (!okname(suser)) {
327 len = strlen(src) + CMDNEEDS + 20;
328 if ((bp = malloc(len)) == NULL)
330 (void)snprintf(bp, len, "%s -f %s", cmd, src);
331 rem = rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
338 (void)seteuid(userid);
339 if (family == PF_INET) {
340 tos = IPTOS_THROUGHPUT;
341 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos,
343 warn("TOS (ignored)");
345 sink(1, argv + argc - 1);
353 source(int argc, char *argv[])
359 int amt, fd, haderr, indx, result;
360 char *last, *name, buf[BUFSIZ];
362 for (indx = 0; indx < argc; ++indx) {
364 if ((fd = open(name, O_RDONLY, 0)) < 0)
366 if (fstat(fd, &stb)) {
367 syserr: run_err("%s: %s", name, strerror(errno));
370 switch (stb.st_mode & S_IFMT) {
380 run_err("%s: not a regular file", name);
383 if ((last = strrchr(name, '/')) == NULL)
389 * Make it compatible with possible future
390 * versions expecting microseconds.
392 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
393 (long)stb.st_mtimespec.tv_sec,
394 (long)stb.st_atimespec.tv_sec);
395 (void)write(rem, buf, strlen(buf));
399 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
400 (void)snprintf(buf, sizeof(buf), "C%04o %jd %s\n",
401 stb.st_mode & MODEMASK, (intmax_t)stb.st_size, last);
402 (void)write(rem, buf, strlen(buf));
405 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
411 /* Keep writing after an error so that we stay sync'd up. */
412 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
414 if (i + amt > stb.st_size)
415 amt = stb.st_size - i;
417 result = read(fd, bp->buf, amt);
419 haderr = result >= 0 ? EIO : errno;
422 (void)write(rem, bp->buf, amt);
424 result = write(rem, bp->buf, amt);
426 haderr = result >= 0 ? EIO : errno;
429 if (close(fd) && !haderr)
432 (void)write(rem, "", 1);
434 run_err("%s: %s", name, strerror(haderr));
440 rsource(char *name, struct stat *statp)
444 char *last, *vect[1], path[PATH_MAX];
446 if (!(dirp = opendir(name))) {
447 run_err("%s: %s", name, strerror(errno));
450 last = strrchr(name, '/');
456 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
457 (long)statp->st_mtimespec.tv_sec,
458 (long)statp->st_atimespec.tv_sec);
459 (void)write(rem, path, strlen(path));
460 if (response() < 0) {
465 (void)snprintf(path, sizeof(path),
466 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
467 (void)write(rem, path, strlen(path));
468 if (response() < 0) {
472 while ((dp = readdir(dirp))) {
475 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
477 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
478 run_err("%s/%s: name too long", name, dp->d_name);
481 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
485 (void)closedir(dirp);
486 (void)write(rem, "E\n", 2);
491 sink(int argc, char *argv[])
495 struct timeval tv[2];
496 enum { YES, NO, DISPLAYED } wrerr;
499 int amt, exists, first, mask, mode, ofd, omode;
501 int setimes, targisdir, wrerrno = 0;
502 char ch, *cp, *np, *targ, *vect[1], buf[BUFSIZ], path[PATH_MAX];
507 #define SCREWUP(str) { why = str; goto screwup; }
509 setimes = targisdir = 0;
514 run_err("ambiguous target");
518 if (targetshouldbedirectory)
520 (void)write(rem, "", 1);
521 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
523 for (first = 1;; first = 0) {
525 if (read(rem, cp, 1) <= 0)
528 SCREWUP("unexpected <newline>");
530 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
531 SCREWUP("lost connection");
533 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
536 if (buf[0] == '\01' || buf[0] == '\02') {
538 (void)write(STDERR_FILENO,
539 buf + 1, strlen(buf + 1));
546 (void)write(rem, "", 1);
557 mtime.tv_sec = strtol(cp, &cp, 10);
558 if (!cp || *cp++ != ' ')
559 SCREWUP("mtime.sec not delimited");
560 mtime.tv_usec = strtol(cp, &cp, 10);
561 if (!cp || *cp++ != ' ')
562 SCREWUP("mtime.usec not delimited");
563 atime.tv_sec = strtol(cp, &cp, 10);
564 if (!cp || *cp++ != ' ')
565 SCREWUP("atime.sec not delimited");
566 atime.tv_usec = strtol(cp, &cp, 10);
567 if (!cp || *cp++ != '\0')
568 SCREWUP("atime.usec not delimited");
569 (void)write(rem, "", 1);
572 if (*cp != 'C' && *cp != 'D') {
574 * Check for the case "rcp remote:foo\* local:bar".
575 * In this case, the line "No match." can be returned
576 * by the shell before the rcp command on the remote is
577 * executed so the ^Aerror_message convention isn't
584 SCREWUP("expected control record");
587 for (++cp; cp < buf + 5; cp++) {
588 if (*cp < '0' || *cp > '7')
590 mode = (mode << 3) | (*cp - '0');
593 SCREWUP("mode not delimited");
595 for (size = 0; isdigit(*cp);)
596 size = size * 10 + (*cp++ - '0');
598 SCREWUP("size not delimited");
600 if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp)
602 run_err("%s%s%s: name too long", targ,
603 *targ ? "/" : "", cp);
606 (void)snprintf(path, sizeof(path), "%s%s%s", targ,
607 *targ ? "/" : "", cp);
611 exists = stat(np, &stb) == 0;
613 int mod_flag = pflag;
615 if (!S_ISDIR(stb.st_mode)) {
620 (void)chmod(np, mode);
622 /* Handle copying from a read-only directory */
624 if (mkdir(np, mode | S_IRWXU) < 0)
631 if (utimes(np, tv) < 0)
632 run_err("%s: set times: %s",
633 np, strerror(errno));
636 (void)chmod(np, mode);
641 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
642 bad: run_err("%s: %s", np, strerror(errno));
645 (void)write(rem, "", 1);
646 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
652 for (count = i = 0; i < size; i += BUFSIZ) {
658 j = read(rem, cp, amt);
660 run_err("%s", j ? strerror(errno) :
661 "dropped connection");
667 if (count == bp->cnt) {
668 /* Keep reading so we stay sync'd up. */
670 j = write(ofd, bp->buf, count);
671 if (j != (off_t)count) {
673 wrerrno = j >= 0 ? EIO : errno;
680 if (count != 0 && wrerr == NO &&
681 (j = write(ofd, bp->buf, count)) != (off_t)count) {
683 wrerrno = j >= 0 ? EIO : errno;
685 if (ftruncate(ofd, size)) {
686 run_err("%s: truncate: %s", np, strerror(errno));
690 if (exists || omode != mode)
691 if (fchmod(ofd, omode))
692 run_err("%s: set mode: %s",
693 np, strerror(errno));
695 if (!exists && omode != mode)
696 if (fchmod(ofd, omode & ~mask))
697 run_err("%s: set mode: %s",
698 np, strerror(errno));
702 if (setimes && wrerr == NO) {
704 if (utimes(np, tv) < 0) {
705 run_err("%s: set times: %s",
706 np, strerror(errno));
712 run_err("%s: %s", np, strerror(wrerrno));
715 (void)write(rem, "", 1);
722 run_err("protocol error: %s", why);
729 char ch, *cp, resp, rbuf[BUFSIZ];
731 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
741 case 1: /* error, followed by error msg */
742 case 2: /* fatal error, "" */
744 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
747 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
750 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
762 (void)fprintf(stderr, "%s\n%s\n",
763 "usage: rcp [-46p] file1 file2",
764 " rcp [-46pr] file ... directory");
771 run_err(const char *fmt, ...)
777 if (fp == NULL && !(fp = fdopen(rem, "w")))
779 (void)fprintf(fp, "%c", 0x01);
780 (void)fprintf(fp, "rcp: ");
782 (void)vfprintf(fp, fmt, ap);
784 (void)fprintf(fp, "\n");