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>
77 #define OPTIONS "46dfprt"
79 static struct passwd *pwd;
84 static int pflag, iamrecursive, targetshouldbedirectory;
85 static int family = PF_UNSPEC;
88 static const char **argv_copy;
90 static char period[] = ".";
93 static char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
96 void rsource(char *, struct stat *);
97 void sink(int, char *[]);
98 void source(int, char *[]);
99 void tolocal(int, char *[]);
100 void toremote(char *, int, char *[]);
104 main(int argc, char *argv[])
107 int ch, fflag, i, tflag;
111 * Prepare for execing ourselves.
113 argc_copy = argc + 1;
114 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
115 if (argv_copy == NULL)
117 argv_copy[0] = argv[0];
119 for (i = 1; i < argc; ++i) {
120 argv_copy[i + 1] = strdup(argv[i]);
121 if (argv_copy[i + 1] == NULL)
122 errx(1, "strdup: out of memory");
124 argv_copy[argc + 1] = NULL;
127 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
128 switch(ch) { /* User-visible flags. */
143 /* Server options. */
145 targetshouldbedirectory = 1;
147 case 'f': /* "from" */
162 sp = getservbyname("shell", "tcp");
164 errx(1, "shell/tcp: unknown service");
167 if ((pwd = getpwuid(userid = getuid())) == NULL)
168 errx(1, "unknown user %d", (int)userid);
170 rem = STDIN_FILENO; /* XXX */
172 if (fflag) { /* Follow "protocol", send data. */
174 (void)setuid(userid);
179 if (tflag) { /* Receive data. */
180 (void)setuid(userid);
188 targetshouldbedirectory = 1;
191 /* Command to be executed on remote system using "rsh". */
192 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
193 iamrecursive ? " -r" : "", pflag ? " -p" : "",
194 targetshouldbedirectory ? " -d" : "");
196 (void)signal(SIGPIPE, lostconn);
198 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
199 toremote(targ, argc, argv);
201 tolocal(argc, argv); /* Dest is local host. */
202 if (targetshouldbedirectory)
203 verifydir(argv[argc - 1]);
209 toremote(char *targ, int argc, char *argv[])
212 char *bp, *host, *src, *suser, *thost, *tuser;
218 if ((thost = strchr(argv[argc - 1], '@'))) {
221 tuser = argv[argc - 1];
224 else if (!okname(tuser))
227 thost = argv[argc - 1];
231 for (i = 0; i < argc - 1; i++) {
232 src = colon(argv[i]);
233 if (src) { /* remote to remote */
237 host = strchr(argv[i], '@');
242 suser = pwd->pw_name;
243 else if (!okname(suser)) {
248 "%s %s -l %s -n %s %s '%s%s%s:%s'",
249 _PATH_RSH, host, suser, cmd, src,
250 tuser ? tuser : "", tuser ? "@" : "",
255 "exec %s %s -n %s %s '%s%s%s:%s'",
256 _PATH_RSH, argv[i], cmd, src,
257 tuser ? tuser : "", tuser ? "@" : "",
260 (void)susystem(bp, userid);
262 } else { /* local to remote */
264 if (asprintf(&bp, "%s -t %s", cmd, targ)
268 rem = rcmd_af(&host, port,
270 tuser ? tuser : pwd->pw_name,
274 if (family == PF_INET) {
275 tos = IPTOS_THROUGHPUT;
276 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
277 &tos, sizeof(int)) < 0)
278 warn("TOS (ignored)");
283 (void)setuid(userid);
291 tolocal(int argc, char *argv[])
294 char *bp, *host, *src, *suser;
296 for (i = 0; i < argc - 1; i++) {
297 if (!(src = colon(argv[i]))) { /* Local to local. */
298 len = strlen(_PATH_CP) + strlen(argv[i]) +
299 strlen(argv[argc - 1]) + 20;
300 if (!(bp = malloc(len)))
302 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
303 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
304 argv[i], argv[argc - 1]);
305 if (susystem(bp, userid))
313 if ((host = strchr(argv[i], '@')) == NULL) {
315 suser = pwd->pw_name;
320 suser = pwd->pw_name;
321 else if (!okname(suser)) {
326 len = strlen(src) + CMDNEEDS + 20;
327 if ((bp = malloc(len)) == NULL)
329 (void)snprintf(bp, len, "%s -f %s", cmd, src);
330 rem = rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
337 (void)seteuid(userid);
338 if (family == PF_INET) {
339 tos = IPTOS_THROUGHPUT;
340 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos,
342 warn("TOS (ignored)");
344 sink(1, argv + argc - 1);
352 source(int argc, char *argv[])
358 int amt, fd, haderr, indx, result;
359 char *last, *name, buf[BUFSIZ];
361 for (indx = 0; indx < argc; ++indx) {
363 if ((fd = open(name, O_RDONLY, 0)) < 0)
365 if (fstat(fd, &stb)) {
366 syserr: run_err("%s: %s", name, strerror(errno));
369 switch (stb.st_mode & S_IFMT) {
379 run_err("%s: not a regular file", name);
382 if ((last = strrchr(name, '/')) == NULL)
388 * Make it compatible with possible future
389 * versions expecting microseconds.
391 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
392 (long)stb.st_mtim.tv_sec,
393 (long)stb.st_atim.tv_sec);
394 (void)write(rem, buf, strlen(buf));
398 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
399 (void)snprintf(buf, sizeof(buf), "C%04o %jd %s\n",
400 stb.st_mode & MODEMASK, (intmax_t)stb.st_size, last);
401 (void)write(rem, buf, strlen(buf));
404 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
410 /* Keep writing after an error so that we stay sync'd up. */
411 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
413 if (i + amt > stb.st_size)
414 amt = stb.st_size - i;
416 result = read(fd, bp->buf, amt);
418 haderr = result >= 0 ? EIO : errno;
421 (void)write(rem, bp->buf, amt);
423 result = write(rem, bp->buf, amt);
425 haderr = result >= 0 ? EIO : errno;
428 if (close(fd) && !haderr)
431 (void)write(rem, "", 1);
433 run_err("%s: %s", name, strerror(haderr));
439 rsource(char *name, struct stat *statp)
443 char *last, *vect[1], path[PATH_MAX];
445 if (!(dirp = opendir(name))) {
446 run_err("%s: %s", name, strerror(errno));
449 last = strrchr(name, '/');
455 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
456 (long)statp->st_mtim.tv_sec,
457 (long)statp->st_atim.tv_sec);
458 (void)write(rem, path, strlen(path));
459 if (response() < 0) {
464 (void)snprintf(path, sizeof(path),
465 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
466 (void)write(rem, path, strlen(path));
467 if (response() < 0) {
471 while ((dp = readdir(dirp))) {
474 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
476 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
477 run_err("%s/%s: name too long", name, dp->d_name);
480 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
484 (void)closedir(dirp);
485 (void)write(rem, "E\n", 2);
490 sink(int argc, char *argv[])
494 struct timeval tv[2];
495 enum { YES, NO, DISPLAYED } wrerr;
498 int amt, exists, first, mask, mode, ofd, omode;
500 int setimes, targisdir, wrerrno = 0;
501 char ch, *cp, *np, *targ, *vect[1], buf[BUFSIZ], path[PATH_MAX];
506 #define SCREWUP(str) { why = str; goto screwup; }
508 setimes = targisdir = 0;
513 run_err("ambiguous target");
517 if (targetshouldbedirectory)
519 (void)write(rem, "", 1);
520 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
522 for (first = 1;; first = 0) {
524 if (read(rem, cp, 1) <= 0)
527 SCREWUP("unexpected <newline>");
529 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
530 SCREWUP("lost connection");
532 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
535 if (buf[0] == '\01' || buf[0] == '\02') {
537 (void)write(STDERR_FILENO,
538 buf + 1, strlen(buf + 1));
545 (void)write(rem, "", 1);
556 mtime.tv_sec = strtol(cp, &cp, 10);
557 if (!cp || *cp++ != ' ')
558 SCREWUP("mtime.sec not delimited");
559 mtime.tv_usec = strtol(cp, &cp, 10);
560 if (!cp || *cp++ != ' ')
561 SCREWUP("mtime.usec not delimited");
562 atime.tv_sec = strtol(cp, &cp, 10);
563 if (!cp || *cp++ != ' ')
564 SCREWUP("atime.sec not delimited");
565 atime.tv_usec = strtol(cp, &cp, 10);
566 if (!cp || *cp++ != '\0')
567 SCREWUP("atime.usec not delimited");
568 (void)write(rem, "", 1);
571 if (*cp != 'C' && *cp != 'D') {
573 * Check for the case "rcp remote:foo\* local:bar".
574 * In this case, the line "No match." can be returned
575 * by the shell before the rcp command on the remote is
576 * executed so the ^Aerror_message convention isn't
583 SCREWUP("expected control record");
586 for (++cp; cp < buf + 5; cp++) {
587 if (*cp < '0' || *cp > '7')
589 mode = (mode << 3) | (*cp - '0');
592 SCREWUP("mode not delimited");
594 for (size = 0; isdigit(*cp);)
595 size = size * 10 + (*cp++ - '0');
597 SCREWUP("size not delimited");
599 if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp)
601 run_err("%s%s%s: name too long", targ,
602 *targ ? "/" : "", cp);
605 (void)snprintf(path, sizeof(path), "%s%s%s", targ,
606 *targ ? "/" : "", cp);
610 exists = stat(np, &stb) == 0;
612 int mod_flag = pflag;
614 if (!S_ISDIR(stb.st_mode)) {
619 (void)chmod(np, mode);
621 /* Handle copying from a read-only directory */
623 if (mkdir(np, mode | S_IRWXU) < 0)
630 if (utimes(np, tv) < 0)
631 run_err("%s: set times: %s",
632 np, strerror(errno));
635 (void)chmod(np, mode);
640 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
641 bad: run_err("%s: %s", np, strerror(errno));
644 (void)write(rem, "", 1);
645 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
651 for (count = i = 0; i < size; i += BUFSIZ) {
657 j = read(rem, cp, amt);
659 run_err("%s", j ? strerror(errno) :
660 "dropped connection");
666 if (count == bp->cnt) {
667 /* Keep reading so we stay sync'd up. */
669 j = write(ofd, bp->buf, count);
670 if (j != (off_t)count) {
672 wrerrno = j >= 0 ? EIO : errno;
679 if (count != 0 && wrerr == NO &&
680 (j = write(ofd, bp->buf, count)) != (off_t)count) {
682 wrerrno = j >= 0 ? EIO : errno;
684 if (ftruncate(ofd, size)) {
685 run_err("%s: truncate: %s", np, strerror(errno));
689 if (exists || omode != mode)
690 if (fchmod(ofd, omode))
691 run_err("%s: set mode: %s",
692 np, strerror(errno));
694 if (!exists && omode != mode)
695 if (fchmod(ofd, omode & ~mask))
696 run_err("%s: set mode: %s",
697 np, strerror(errno));
701 if (setimes && wrerr == NO) {
703 if (utimes(np, tv) < 0) {
704 run_err("%s: set times: %s",
705 np, strerror(errno));
711 run_err("%s: %s", np, strerror(wrerrno));
714 (void)write(rem, "", 1);
721 run_err("protocol error: %s", why);
728 char ch, *cp, resp, rbuf[BUFSIZ];
730 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
740 case 1: /* error, followed by error msg */
741 case 2: /* fatal error, "" */
743 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
746 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
749 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
761 (void)fprintf(stderr, "%s\n%s\n",
762 "usage: rcp [-46p] file1 file2",
763 " rcp [-46pr] file ... directory");
770 run_err(const char *fmt, ...)
776 if (fp == NULL && !(fp = fdopen(rem, "w")))
778 (void)fprintf(fp, "%c", 0x01);
779 (void)fprintf(fp, "rcp: ");
781 (void)vfprintf(fp, fmt, ap);
783 (void)fprintf(fp, "\n");