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
43 static char const copyright[] =
44 "@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
45 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 #define OPTIONS "46dfprt"
89 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
90 int family = PF_UNSPEC;
93 static const char **argv_copy;
95 static char period[] = ".";
98 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
101 void rsource(char *, struct stat *);
102 void sink(int, char *[]);
103 void source(int, char *[]);
104 void tolocal(int, char *[]);
105 void toremote(char *, int, char *[]);
109 main(int argc, char *argv[])
112 int ch, fflag, i, tflag;
116 * Prepare for execing ourselves.
118 argc_copy = argc + 1;
119 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
120 if (argv_copy == NULL)
122 argv_copy[0] = argv[0];
124 for (i = 1; i < argc; ++i) {
125 argv_copy[i + 1] = strdup(argv[i]);
126 if (argv_copy[i + 1] == NULL)
127 errx(1, "strdup: out of memory");
129 argv_copy[argc + 1] = NULL;
132 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
133 switch(ch) { /* User-visible flags. */
148 /* Server options. */
150 targetshouldbedirectory = 1;
152 case 'f': /* "from" */
167 sp = getservbyname("shell", "tcp");
169 errx(1, "shell/tcp: unknown service");
172 if ((pwd = getpwuid(userid = getuid())) == NULL)
173 errx(1, "unknown user %d", (int)userid);
175 rem = STDIN_FILENO; /* XXX */
177 if (fflag) { /* Follow "protocol", send data. */
179 (void)setuid(userid);
184 if (tflag) { /* Receive data. */
185 (void)setuid(userid);
193 targetshouldbedirectory = 1;
196 /* Command to be executed on remote system using "rsh". */
197 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
198 iamrecursive ? " -r" : "", pflag ? " -p" : "",
199 targetshouldbedirectory ? " -d" : "");
201 (void)signal(SIGPIPE, lostconn);
203 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
204 toremote(targ, argc, argv);
206 tolocal(argc, argv); /* Dest is local host. */
207 if (targetshouldbedirectory)
208 verifydir(argv[argc - 1]);
214 toremote(char *targ, int argc, char *argv[])
217 char *bp, *host, *src, *suser, *thost, *tuser;
223 if ((thost = strchr(argv[argc - 1], '@'))) {
226 tuser = argv[argc - 1];
229 else if (!okname(tuser))
232 thost = argv[argc - 1];
236 for (i = 0; i < argc - 1; i++) {
237 src = colon(argv[i]);
238 if (src) { /* remote to remote */
242 host = strchr(argv[i], '@');
243 len = strlen(_PATH_RSH) + strlen(argv[i]) +
244 strlen(src) + (tuser ? strlen(tuser) : 0) +
245 strlen(thost) + strlen(targ) + CMDNEEDS + 20;
246 if (!(bp = malloc(len)))
252 suser = pwd->pw_name;
253 else if (!okname(suser)) {
257 (void)snprintf(bp, len,
258 "%s %s -l %s -n %s %s '%s%s%s:%s'",
259 _PATH_RSH, host, suser, cmd, src,
260 tuser ? tuser : "", tuser ? "@" : "",
263 (void)snprintf(bp, len,
264 "exec %s %s -n %s %s '%s%s%s:%s'",
265 _PATH_RSH, argv[i], cmd, src,
266 tuser ? tuser : "", tuser ? "@" : "",
268 (void)susystem(bp, userid);
270 } else { /* local to remote */
272 len = strlen(targ) + CMDNEEDS + 20;
273 if (!(bp = malloc(len)))
275 (void)snprintf(bp, len, "%s -t %s", cmd, targ);
277 rem = rcmd_af(&host, port,
279 tuser ? tuser : pwd->pw_name,
283 if (family == PF_INET) {
284 tos = IPTOS_THROUGHPUT;
285 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
286 &tos, sizeof(int)) < 0)
287 warn("TOS (ignored)");
292 (void)setuid(userid);
300 tolocal(int argc, char *argv[])
303 char *bp, *host, *src, *suser;
305 for (i = 0; i < argc - 1; i++) {
306 if (!(src = colon(argv[i]))) { /* Local to local. */
307 len = strlen(_PATH_CP) + strlen(argv[i]) +
308 strlen(argv[argc - 1]) + 20;
309 if (!(bp = malloc(len)))
311 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
312 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
313 argv[i], argv[argc - 1]);
314 if (susystem(bp, userid))
322 if ((host = strchr(argv[i], '@')) == NULL) {
324 suser = pwd->pw_name;
329 suser = pwd->pw_name;
330 else if (!okname(suser)) {
335 len = strlen(src) + CMDNEEDS + 20;
336 if ((bp = malloc(len)) == NULL)
338 (void)snprintf(bp, len, "%s -f %s", cmd, src);
339 rem = rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
346 (void)seteuid(userid);
347 if (family == PF_INET) {
348 tos = IPTOS_THROUGHPUT;
349 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos,
351 warn("TOS (ignored)");
353 sink(1, argv + argc - 1);
361 source(int argc, char *argv[])
367 int amt, fd, haderr, indx, result;
368 char *last, *name, buf[BUFSIZ];
370 for (indx = 0; indx < argc; ++indx) {
372 if ((fd = open(name, O_RDONLY, 0)) < 0)
374 if (fstat(fd, &stb)) {
375 syserr: run_err("%s: %s", name, strerror(errno));
378 switch (stb.st_mode & S_IFMT) {
388 run_err("%s: not a regular file", name);
391 if ((last = strrchr(name, '/')) == NULL)
397 * Make it compatible with possible future
398 * versions expecting microseconds.
400 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
401 (long)stb.st_mtimespec.tv_sec,
402 (long)stb.st_atimespec.tv_sec);
403 (void)write(rem, buf, strlen(buf));
407 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
408 (void)snprintf(buf, sizeof(buf), "C%04o %jd %s\n",
409 stb.st_mode & MODEMASK, (intmax_t)stb.st_size, last);
410 (void)write(rem, buf, strlen(buf));
413 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
414 next: (void)close(fd);
418 /* Keep writing after an error so that we stay sync'd up. */
419 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
421 if (i + amt > stb.st_size)
422 amt = stb.st_size - i;
424 result = read(fd, bp->buf, amt);
426 haderr = result >= 0 ? EIO : errno;
429 (void)write(rem, bp->buf, amt);
431 result = write(rem, bp->buf, amt);
433 haderr = result >= 0 ? EIO : errno;
436 if (close(fd) && !haderr)
439 (void)write(rem, "", 1);
441 run_err("%s: %s", name, strerror(haderr));
447 rsource(char *name, struct stat *statp)
451 char *last, *vect[1], path[PATH_MAX];
453 if (!(dirp = opendir(name))) {
454 run_err("%s: %s", name, strerror(errno));
457 last = strrchr(name, '/');
463 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
464 (long)statp->st_mtimespec.tv_sec,
465 (long)statp->st_atimespec.tv_sec);
466 (void)write(rem, path, strlen(path));
467 if (response() < 0) {
472 (void)snprintf(path, sizeof(path),
473 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
474 (void)write(rem, path, strlen(path));
475 if (response() < 0) {
479 while ((dp = readdir(dirp))) {
482 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
484 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
485 run_err("%s/%s: name too long", name, dp->d_name);
488 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
492 (void)closedir(dirp);
493 (void)write(rem, "E\n", 2);
498 sink(int argc, char *argv[])
502 struct timeval tv[2];
503 enum { YES, NO, DISPLAYED } wrerr;
506 int amt, exists, first, mask, mode, ofd, omode;
508 int setimes, targisdir, wrerrno = 0;
509 char ch, *cp, *np, *targ, *vect[1], buf[BUFSIZ], path[PATH_MAX];
514 #define SCREWUP(str) { why = str; goto screwup; }
516 setimes = targisdir = 0;
521 run_err("ambiguous target");
525 if (targetshouldbedirectory)
527 (void)write(rem, "", 1);
528 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
530 for (first = 1;; first = 0) {
532 if (read(rem, cp, 1) <= 0)
535 SCREWUP("unexpected <newline>");
537 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
538 SCREWUP("lost connection");
540 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
543 if (buf[0] == '\01' || buf[0] == '\02') {
545 (void)write(STDERR_FILENO,
546 buf + 1, strlen(buf + 1));
553 (void)write(rem, "", 1);
564 mtime.tv_sec = strtol(cp, &cp, 10);
565 if (!cp || *cp++ != ' ')
566 SCREWUP("mtime.sec not delimited");
567 mtime.tv_usec = strtol(cp, &cp, 10);
568 if (!cp || *cp++ != ' ')
569 SCREWUP("mtime.usec not delimited");
570 atime.tv_sec = strtol(cp, &cp, 10);
571 if (!cp || *cp++ != ' ')
572 SCREWUP("atime.sec not delimited");
573 atime.tv_usec = strtol(cp, &cp, 10);
574 if (!cp || *cp++ != '\0')
575 SCREWUP("atime.usec not delimited");
576 (void)write(rem, "", 1);
579 if (*cp != 'C' && *cp != 'D') {
581 * Check for the case "rcp remote:foo\* local:bar".
582 * In this case, the line "No match." can be returned
583 * by the shell before the rcp command on the remote is
584 * executed so the ^Aerror_message convention isn't
591 SCREWUP("expected control record");
594 for (++cp; cp < buf + 5; cp++) {
595 if (*cp < '0' || *cp > '7')
597 mode = (mode << 3) | (*cp - '0');
600 SCREWUP("mode not delimited");
602 for (size = 0; isdigit(*cp);)
603 size = size * 10 + (*cp++ - '0');
605 SCREWUP("size not delimited");
607 if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp)
609 run_err("%s%s%s: name too long", targ,
610 *targ ? "/" : "", cp);
613 (void)snprintf(path, sizeof(path), "%s%s%s", targ,
614 *targ ? "/" : "", cp);
618 exists = stat(np, &stb) == 0;
620 int mod_flag = pflag;
622 if (!S_ISDIR(stb.st_mode)) {
627 (void)chmod(np, mode);
629 /* Handle copying from a read-only directory */
631 if (mkdir(np, mode | S_IRWXU) < 0)
638 if (utimes(np, tv) < 0)
639 run_err("%s: set times: %s",
640 np, strerror(errno));
643 (void)chmod(np, mode);
648 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
649 bad: run_err("%s: %s", np, strerror(errno));
652 (void)write(rem, "", 1);
653 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
659 for (count = i = 0; i < size; i += BUFSIZ) {
665 j = read(rem, cp, amt);
667 run_err("%s", j ? strerror(errno) :
668 "dropped connection");
674 if (count == bp->cnt) {
675 /* Keep reading so we stay sync'd up. */
677 j = write(ofd, bp->buf, count);
678 if (j != (off_t)count) {
680 wrerrno = j >= 0 ? EIO : errno;
687 if (count != 0 && wrerr == NO &&
688 (j = write(ofd, bp->buf, count)) != (off_t)count) {
690 wrerrno = j >= 0 ? EIO : errno;
692 if (ftruncate(ofd, size)) {
693 run_err("%s: truncate: %s", np, strerror(errno));
697 if (exists || omode != mode)
698 if (fchmod(ofd, omode))
699 run_err("%s: set mode: %s",
700 np, strerror(errno));
702 if (!exists && omode != mode)
703 if (fchmod(ofd, omode & ~mask))
704 run_err("%s: set mode: %s",
705 np, strerror(errno));
709 if (setimes && wrerr == NO) {
711 if (utimes(np, tv) < 0) {
712 run_err("%s: set times: %s",
713 np, strerror(errno));
719 run_err("%s: %s", np, strerror(wrerrno));
722 (void)write(rem, "", 1);
729 run_err("protocol error: %s", why);
736 char ch, *cp, resp, rbuf[BUFSIZ];
738 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
748 case 1: /* error, followed by error msg */
749 case 2: /* fatal error, "" */
751 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
754 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
757 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
769 (void)fprintf(stderr, "%s\n%s\n",
770 "usage: rcp [-46p] f1 f2",
771 " rcp [-46pr] f1 ... fn directory");
778 run_err(const char *fmt, ...)
784 if (fp == NULL && !(fp = fdopen(rem, "w")))
786 (void)fprintf(fp, "%c", 0x01);
787 (void)fprintf(fp, "rcp: ");
789 (void)vfprintf(fp, fmt, ap);
791 (void)fprintf(fp, "\n");