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], '@');
247 suser = pwd->pw_name;
248 else if (!okname(suser)) {
253 "%s %s -l %s -n %s %s '%s%s%s:%s'",
254 _PATH_RSH, host, suser, cmd, src,
255 tuser ? tuser : "", tuser ? "@" : "",
260 "exec %s %s -n %s %s '%s%s%s:%s'",
261 _PATH_RSH, argv[i], cmd, src,
262 tuser ? tuser : "", tuser ? "@" : "",
265 (void)susystem(bp, userid);
267 } else { /* local to remote */
269 if (asprintf(&bp, "%s -t %s", cmd, targ)
273 rem = rcmd_af(&host, port,
275 tuser ? tuser : pwd->pw_name,
279 if (family == PF_INET) {
280 tos = IPTOS_THROUGHPUT;
281 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
282 &tos, sizeof(int)) < 0)
283 warn("TOS (ignored)");
288 (void)setuid(userid);
296 tolocal(int argc, char *argv[])
299 char *bp, *host, *src, *suser;
301 for (i = 0; i < argc - 1; i++) {
302 if (!(src = colon(argv[i]))) { /* Local to local. */
303 len = strlen(_PATH_CP) + strlen(argv[i]) +
304 strlen(argv[argc - 1]) + 20;
305 if (!(bp = malloc(len)))
307 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
308 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
309 argv[i], argv[argc - 1]);
310 if (susystem(bp, userid))
318 if ((host = strchr(argv[i], '@')) == NULL) {
320 suser = pwd->pw_name;
325 suser = pwd->pw_name;
326 else if (!okname(suser)) {
331 len = strlen(src) + CMDNEEDS + 20;
332 if ((bp = malloc(len)) == NULL)
334 (void)snprintf(bp, len, "%s -f %s", cmd, src);
335 rem = rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
342 (void)seteuid(userid);
343 if (family == PF_INET) {
344 tos = IPTOS_THROUGHPUT;
345 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos,
347 warn("TOS (ignored)");
349 sink(1, argv + argc - 1);
357 source(int argc, char *argv[])
363 int amt, fd, haderr, indx, result;
364 char *last, *name, buf[BUFSIZ];
366 for (indx = 0; indx < argc; ++indx) {
368 if ((fd = open(name, O_RDONLY, 0)) < 0)
370 if (fstat(fd, &stb)) {
371 syserr: run_err("%s: %s", name, strerror(errno));
374 switch (stb.st_mode & S_IFMT) {
384 run_err("%s: not a regular file", name);
387 if ((last = strrchr(name, '/')) == NULL)
393 * Make it compatible with possible future
394 * versions expecting microseconds.
396 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
397 (long)stb.st_mtimespec.tv_sec,
398 (long)stb.st_atimespec.tv_sec);
399 (void)write(rem, buf, strlen(buf));
403 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
404 (void)snprintf(buf, sizeof(buf), "C%04o %jd %s\n",
405 stb.st_mode & MODEMASK, (intmax_t)stb.st_size, last);
406 (void)write(rem, buf, strlen(buf));
409 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
410 next: (void)close(fd);
414 /* Keep writing after an error so that we stay sync'd up. */
415 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
417 if (i + amt > stb.st_size)
418 amt = stb.st_size - i;
420 result = read(fd, bp->buf, amt);
422 haderr = result >= 0 ? EIO : errno;
425 (void)write(rem, bp->buf, amt);
427 result = write(rem, bp->buf, amt);
429 haderr = result >= 0 ? EIO : errno;
432 if (close(fd) && !haderr)
435 (void)write(rem, "", 1);
437 run_err("%s: %s", name, strerror(haderr));
443 rsource(char *name, struct stat *statp)
447 char *last, *vect[1], path[PATH_MAX];
449 if (!(dirp = opendir(name))) {
450 run_err("%s: %s", name, strerror(errno));
453 last = strrchr(name, '/');
459 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
460 (long)statp->st_mtimespec.tv_sec,
461 (long)statp->st_atimespec.tv_sec);
462 (void)write(rem, path, strlen(path));
463 if (response() < 0) {
468 (void)snprintf(path, sizeof(path),
469 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
470 (void)write(rem, path, strlen(path));
471 if (response() < 0) {
475 while ((dp = readdir(dirp))) {
478 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
480 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
481 run_err("%s/%s: name too long", name, dp->d_name);
484 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
488 (void)closedir(dirp);
489 (void)write(rem, "E\n", 2);
494 sink(int argc, char *argv[])
498 struct timeval tv[2];
499 enum { YES, NO, DISPLAYED } wrerr;
502 int amt, exists, first, mask, mode, ofd, omode;
504 int setimes, targisdir, wrerrno = 0;
505 char ch, *cp, *np, *targ, *vect[1], buf[BUFSIZ], path[PATH_MAX];
510 #define SCREWUP(str) { why = str; goto screwup; }
512 setimes = targisdir = 0;
517 run_err("ambiguous target");
521 if (targetshouldbedirectory)
523 (void)write(rem, "", 1);
524 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
526 for (first = 1;; first = 0) {
528 if (read(rem, cp, 1) <= 0)
531 SCREWUP("unexpected <newline>");
533 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
534 SCREWUP("lost connection");
536 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
539 if (buf[0] == '\01' || buf[0] == '\02') {
541 (void)write(STDERR_FILENO,
542 buf + 1, strlen(buf + 1));
549 (void)write(rem, "", 1);
560 mtime.tv_sec = strtol(cp, &cp, 10);
561 if (!cp || *cp++ != ' ')
562 SCREWUP("mtime.sec not delimited");
563 mtime.tv_usec = strtol(cp, &cp, 10);
564 if (!cp || *cp++ != ' ')
565 SCREWUP("mtime.usec not delimited");
566 atime.tv_sec = strtol(cp, &cp, 10);
567 if (!cp || *cp++ != ' ')
568 SCREWUP("atime.sec not delimited");
569 atime.tv_usec = strtol(cp, &cp, 10);
570 if (!cp || *cp++ != '\0')
571 SCREWUP("atime.usec not delimited");
572 (void)write(rem, "", 1);
575 if (*cp != 'C' && *cp != 'D') {
577 * Check for the case "rcp remote:foo\* local:bar".
578 * In this case, the line "No match." can be returned
579 * by the shell before the rcp command on the remote is
580 * executed so the ^Aerror_message convention isn't
587 SCREWUP("expected control record");
590 for (++cp; cp < buf + 5; cp++) {
591 if (*cp < '0' || *cp > '7')
593 mode = (mode << 3) | (*cp - '0');
596 SCREWUP("mode not delimited");
598 for (size = 0; isdigit(*cp);)
599 size = size * 10 + (*cp++ - '0');
601 SCREWUP("size not delimited");
603 if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp)
605 run_err("%s%s%s: name too long", targ,
606 *targ ? "/" : "", cp);
609 (void)snprintf(path, sizeof(path), "%s%s%s", targ,
610 *targ ? "/" : "", cp);
614 exists = stat(np, &stb) == 0;
616 int mod_flag = pflag;
618 if (!S_ISDIR(stb.st_mode)) {
623 (void)chmod(np, mode);
625 /* Handle copying from a read-only directory */
627 if (mkdir(np, mode | S_IRWXU) < 0)
634 if (utimes(np, tv) < 0)
635 run_err("%s: set times: %s",
636 np, strerror(errno));
639 (void)chmod(np, mode);
644 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
645 bad: run_err("%s: %s", np, strerror(errno));
648 (void)write(rem, "", 1);
649 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
655 for (count = i = 0; i < size; i += BUFSIZ) {
661 j = read(rem, cp, amt);
663 run_err("%s", j ? strerror(errno) :
664 "dropped connection");
670 if (count == bp->cnt) {
671 /* Keep reading so we stay sync'd up. */
673 j = write(ofd, bp->buf, count);
674 if (j != (off_t)count) {
676 wrerrno = j >= 0 ? EIO : errno;
683 if (count != 0 && wrerr == NO &&
684 (j = write(ofd, bp->buf, count)) != (off_t)count) {
686 wrerrno = j >= 0 ? EIO : errno;
688 if (ftruncate(ofd, size)) {
689 run_err("%s: truncate: %s", np, strerror(errno));
693 if (exists || omode != mode)
694 if (fchmod(ofd, omode))
695 run_err("%s: set mode: %s",
696 np, strerror(errno));
698 if (!exists && omode != mode)
699 if (fchmod(ofd, omode & ~mask))
700 run_err("%s: set mode: %s",
701 np, strerror(errno));
705 if (setimes && wrerr == NO) {
707 if (utimes(np, tv) < 0) {
708 run_err("%s: set times: %s",
709 np, strerror(errno));
715 run_err("%s: %s", np, strerror(wrerrno));
718 (void)write(rem, "", 1);
725 run_err("protocol error: %s", why);
732 char ch, *cp, resp, rbuf[BUFSIZ];
734 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
744 case 1: /* error, followed by error msg */
745 case 2: /* fatal error, "" */
747 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
750 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
753 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
765 (void)fprintf(stderr, "%s\n%s\n",
766 "usage: rcp [-46p] f1 f2",
767 " rcp [-46pr] f1 ... fn directory");
774 run_err(const char *fmt, ...)
780 if (fp == NULL && !(fp = fdopen(rem, "w")))
782 (void)fprintf(fp, "%c", 0x01);
783 (void)fprintf(fp, "rcp: ");
785 (void)vfprintf(fp, fmt, ap);
787 (void)fprintf(fp, "\n");