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
40 * @(#) Copyright (c) 1983, 1990, 1992, 1993 The Regents of the University of California. All rights reserved.
41 * @(#)rcp.c 8.2 (Berkeley) 4/2/94
42 * $FreeBSD: src/bin/rcp/rcp.c,v 1.26.2.6 2004/09/16 12:16:10 delphij Exp $
43 * $DragonFly: src/bin/rcp/rcp.c,v 1.6 2005/01/07 20:30:21 dillon Exp $
46 #include <sys/cdefs.h>
47 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
75 #include <openssl/des.h>
79 char dst_realm_buf[REALM_SZ];
80 char *dest_realm = NULL;
83 Key_schedule schedule;
84 extern char *krb_realmofhost();
87 #define OPTIONS "46dfKk:prtx"
89 #define OPTIONS "46dfKk:prt"
92 #define OPTIONS "46dfprt"
99 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
100 int family = PF_UNSPEC;
102 static int argc_copy;
103 static char **argv_copy;
106 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
109 int kerberos(char **, char *, char *, char *);
110 void oldw(const char *, ...) __printflike(1, 2);
113 void rsource(char *, struct stat *);
114 void sink(int, char *[]);
115 void source(int, char *[]);
116 void tolocal(int, char *[]);
117 void toremote(char *, int, char *[]);
121 main(int argc, char *argv[])
124 int ch, fflag, i, tflag;
131 * Prepare for execing ourselves.
133 argc_copy = argc + 1;
134 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
135 if (argv_copy == NULL)
137 argv_copy[0] = argv[0];
138 argv_copy[1] = __DECONST(char *, "-K");
139 for (i = 1; i < argc; ++i) {
140 argv_copy[i + 1] = strdup(argv[i]);
141 if (argv_copy[i + 1] == NULL)
142 errx(1, "strdup: out of memory");
144 argv_copy[argc + 1] = NULL;
147 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
148 switch(ch) { /* User-visible flags. */
164 dest_realm = dst_realm_buf;
165 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 ? __DECONST(char *, "ekshell") :
207 __DECONST(char *, "kshell");
209 shell = __DECONST(char *, "kshell");
211 if ((sp = getservbyname(shell, "tcp")) == NULL) {
213 oldw("can't get entry for %s/tcp service", shell);
214 sp = getservbyname(shell = "shell", "tcp");
217 shell = __DECONST(char *, "shell");
218 sp = getservbyname(shell, "tcp");
220 shell = __DECONST(char *, "shell");
221 sp = getservbyname(shell, "tcp");
224 errx(1, "%s/tcp: unknown service", shell);
227 if ((pwd = getpwuid(userid = getuid())) == NULL)
228 errx(1, "unknown user %d", (int)userid);
230 rem = STDIN_FILENO; /* XXX */
232 if (fflag) { /* Follow "protocol", send data. */
239 if (tflag) { /* Receive data. */
248 targetshouldbedirectory = 1;
251 /* Command to be executed on remote system using "rsh". */
253 snprintf(cmd, sizeof(cmd),
254 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
256 (doencrypt && use_kerberos ? " -x" : ""),
260 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
262 snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
263 iamrecursive ? " -r" : "", pflag ? " -p" : "",
264 targetshouldbedirectory ? " -d" : "");
267 signal(SIGPIPE, lostconn);
269 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
270 toremote(targ, argc, argv);
272 tolocal(argc, argv); /* Dest is local host. */
273 if (targetshouldbedirectory)
274 verifydir(argv[argc - 1]);
280 toremote(char *targ, int argc, char *argv[])
283 char *bp, *host, *src, *suser, *thost, *tuser;
287 targ = __DECONST(char *, ".");
289 if ((thost = strchr(argv[argc - 1], '@'))) {
292 tuser = argv[argc - 1];
295 else if (!okname(tuser))
298 thost = argv[argc - 1];
302 for (i = 0; i < argc - 1; i++) {
303 src = colon(argv[i]);
304 if (src) { /* remote to remote */
307 src = __DECONST(char *, ".");
308 host = strchr(argv[i], '@');
309 len = strlen(_PATH_RSH) + strlen(argv[i]) +
310 strlen(src) + (tuser ? strlen(tuser) : 0) +
311 strlen(thost) + strlen(targ) + CMDNEEDS + 20;
312 if (!(bp = malloc(len)))
318 suser = pwd->pw_name;
319 else if (!okname(suser)) {
324 "%s %s -l %s -n %s %s '%s%s%s:%s'",
325 _PATH_RSH, host, suser, cmd, src,
326 tuser ? tuser : "", tuser ? "@" : "",
330 "exec %s %s -n %s %s '%s%s%s:%s'",
331 _PATH_RSH, argv[i], cmd, src,
332 tuser ? tuser : "", tuser ? "@" : "",
334 susystem(bp, userid);
336 } else { /* local to remote */
338 len = strlen(targ) + CMDNEEDS + 20;
339 if (!(bp = malloc(len)))
341 snprintf(bp, len, "%s -t %s", cmd, targ);
345 rem = kerberos(&host, bp,
347 tuser ? tuser : pwd->pw_name);
350 rem = rcmd_af(&host, port,
352 tuser ? tuser : pwd->pw_name,
356 if (family == PF_INET) {
357 tos = IPTOS_THROUGHPUT;
358 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
359 &tos, sizeof(int)) < 0)
360 warn("TOS (ignored)");
373 tolocal(int argc, char *argv[])
376 char *bp, *host, *src, *suser;
378 for (i = 0; i < argc - 1; i++) {
379 if (!(src = colon(argv[i]))) { /* Local to local. */
380 len = strlen(_PATH_CP) + strlen(argv[i]) +
381 strlen(argv[argc - 1]) + 20;
382 if (!(bp = malloc(len)))
384 snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
385 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
386 argv[i], argv[argc - 1]);
387 if (susystem(bp, userid))
394 src = __DECONST(char *, ".");
395 if ((host = strchr(argv[i], '@')) == NULL) {
397 suser = pwd->pw_name;
402 suser = pwd->pw_name;
403 else if (!okname(suser)) {
408 len = strlen(src) + CMDNEEDS + 20;
409 if ((bp = malloc(len)) == NULL)
411 snprintf(bp, len, "%s -f %s", cmd, src);
415 kerberos(&host, bp, pwd->pw_name, suser) :
417 rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
425 if (family == PF_INET) {
426 tos = IPTOS_THROUGHPUT;
427 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos,
429 warn("TOS (ignored)");
431 sink(1, argv + argc - 1);
439 source(int argc, char *argv[])
445 int amt, fd, haderr, indx, result;
446 char *last, *name, buf[BUFSIZ];
448 for (indx = 0; indx < argc; ++indx) {
450 if ((fd = open(name, O_RDONLY, 0)) < 0)
452 if (fstat(fd, &stb)) {
453 syserr: run_err("%s: %s", name, strerror(errno));
456 switch (stb.st_mode & S_IFMT) {
466 run_err("%s: not a regular file", name);
469 if ((last = strrchr(name, '/')) == NULL)
475 * Make it compatible with possible future
476 * versions expecting microseconds.
478 snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
479 (long)stb.st_mtimespec.tv_sec,
480 (long)stb.st_atimespec.tv_sec);
481 write(rem, buf, strlen(buf));
485 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
486 snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
487 stb.st_mode & MODEMASK, stb.st_size, last);
488 write(rem, buf, strlen(buf));
491 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
492 next: (void)close(fd);
496 /* Keep writing after an error so that we stay sync'd up. */
497 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
499 if (i + amt > stb.st_size)
500 amt = stb.st_size - i;
502 result = read(fd, bp->buf, amt);
504 haderr = result >= 0 ? EIO : errno;
507 write(rem, bp->buf, amt);
509 result = write(rem, bp->buf, amt);
511 haderr = result >= 0 ? EIO : errno;
514 if (close(fd) && !haderr)
519 run_err("%s: %s", name, strerror(haderr));
525 rsource(char *name, struct stat *statp)
529 char *last, *vect[1], path[PATH_MAX];
531 if (!(dirp = opendir(name))) {
532 run_err("%s: %s", name, strerror(errno));
535 last = strrchr(name, '/');
541 snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
542 (long)statp->st_mtimespec.tv_sec,
543 (long)statp->st_atimespec.tv_sec);
544 write(rem, path, strlen(path));
545 if (response() < 0) {
550 snprintf(path, sizeof(path),
551 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
552 write(rem, path, strlen(path));
553 if (response() < 0) {
557 while ((dp = readdir(dirp))) {
560 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
562 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
563 run_err("%s/%s: name too long", name, dp->d_name);
566 snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
571 write(rem, "E\n", 2);
576 sink(int argc, char *argv[])
580 struct timeval tv[2];
581 enum { YES, NO, DISPLAYED } wrerr;
585 int exists, first, mask, mode, ofd, omode;
586 int setimes, targisdir, wrerrno = 0;
587 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ], path[PATH_MAX];
591 #define SCREWUP(str) { why = __DECONST(char *, str); goto screwup; }
593 setimes = targisdir = 0;
598 run_err("ambiguous target");
602 if (targetshouldbedirectory)
605 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
607 for (first = 1;; first = 0) {
609 if (read(rem, cp, 1) <= 0)
612 SCREWUP("unexpected <newline>");
614 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
615 SCREWUP("lost connection");
617 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
620 if (buf[0] == '\01' || buf[0] == '\02') {
623 buf + 1, strlen(buf + 1));
641 mtime.tv_sec = strtol(cp, &cp, 10);
642 if (!cp || *cp++ != ' ')
643 SCREWUP("mtime.sec not delimited");
644 mtime.tv_usec = strtol(cp, &cp, 10);
645 if (!cp || *cp++ != ' ')
646 SCREWUP("mtime.usec not delimited");
647 atime.tv_sec = strtol(cp, &cp, 10);
648 if (!cp || *cp++ != ' ')
649 SCREWUP("atime.sec not delimited");
650 atime.tv_usec = strtol(cp, &cp, 10);
651 if (!cp || *cp++ != '\0')
652 SCREWUP("atime.usec not delimited");
656 if (*cp != 'C' && *cp != 'D') {
658 * Check for the case "rcp remote:foo\* local:bar".
659 * In this case, the line "No match." can be returned
660 * by the shell before the rcp command on the remote is
661 * executed so the ^Aerror_message convention isn't
668 SCREWUP("expected control record");
671 for (++cp; cp < buf + 5; cp++) {
672 if (*cp < '0' || *cp > '7')
674 mode = (mode << 3) | (*cp - '0');
677 SCREWUP("mode not delimited");
679 for (size = 0; isdigit(*cp);)
680 size = size * 10 + (*cp++ - '0');
682 SCREWUP("size not delimited");
684 if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp)
686 run_err("%s%s%s: name too long", targ,
687 *targ ? "/" : "", cp);
690 snprintf(path, sizeof(path), "%s%s%s", targ,
691 *targ ? "/" : "", cp);
695 exists = stat(np, &stb) == 0;
697 int mod_flag = pflag;
699 if (!S_ISDIR(stb.st_mode)) {
706 /* Handle copying from a read-only directory */
708 if (mkdir(np, mode | S_IRWXU) < 0)
715 if (utimes(np, tv) < 0)
716 run_err("%s: set times: %s",
717 np, strerror(errno));
725 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
726 bad: run_err("%s: %s", np, strerror(errno));
730 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
736 for (count = i = 0; i < size; i += BUFSIZ) {
742 j = read(rem, cp, amt);
744 run_err("%s", j ? strerror(errno) :
745 "dropped connection");
751 if (count == bp->cnt) {
752 /* Keep reading so we stay sync'd up. */
754 j = write(ofd, bp->buf, count);
757 wrerrno = j >= 0 ? EIO : errno;
764 if (count != 0 && wrerr == NO &&
765 (j = write(ofd, bp->buf, count)) != count) {
767 wrerrno = j >= 0 ? EIO : errno;
769 if (ftruncate(ofd, size)) {
770 run_err("%s: truncate: %s", np, strerror(errno));
774 if (exists || omode != mode)
775 if (fchmod(ofd, omode))
776 run_err("%s: set mode: %s",
777 np, strerror(errno));
779 if (!exists && omode != mode)
780 if (fchmod(ofd, omode & ~mask))
781 run_err("%s: set mode: %s",
782 np, strerror(errno));
786 if (setimes && wrerr == NO) {
788 if (utimes(np, tv) < 0) {
789 run_err("%s: set times: %s",
790 np, strerror(errno));
796 run_err("%s: %s", np, strerror(wrerrno));
806 run_err("protocol error: %s", why);
812 kerberos(char **host, char *bp, char *locuser, char *user)
818 if (dest_realm == NULL)
819 dest_realm = krb_realmofhost(*host);
824 port, user, bp, 0, dest_realm, &cred, schedule) :
826 krcmd(host, port, user, bp, 0, dest_realm);
829 if (errno == ECONNREFUSED)
830 oldw("remote host doesn't support Kerberos");
831 else if (errno == ENOENT)
832 oldw("can't provide Kerberos authentication data");
833 execv(_PATH_RCP, argv_copy);
834 err(1, "execv: %s", _PATH_RCP);
840 "the -x option requires Kerberos authentication");
842 rem = rcmd_af(host, port, locuser, user, bp, 0, family);
846 #endif /* KERBEROS */
851 char ch, *cp, resp, rbuf[BUFSIZ];
853 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
863 case 1: /* error, followed by error msg */
864 case 2: /* fatal error, "" */
866 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
869 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
872 write(STDERR_FILENO, rbuf, cp - rbuf);
886 fprintf(stderr, "%s\n%s\n",
887 "usage: rcp [-46Kpx] [-k realm] f1 f2",
888 " rcp [-46Kprx] [-k realm] f1 ... fn directory");
890 fprintf(stderr, "%s\n%s\n",
891 "usage: rcp [-46Kp] [-k realm] f1 f2",
892 " rcp [-46Kpr] [-k realm] f1 ... fn directory");
895 fprintf(stderr, "%s\n%s\n",
896 "usage: rcp [-46p] f1 f2",
897 " rcp [-46pr] f1 ... fn directory");
906 oldw(const char *fmt, ...)
910 fprintf(stderr, "rcp: ");
911 vfprintf(stderr, fmt, ap);
912 fprintf(stderr, ", using standard rcp\n");
918 run_err(const char *fmt, ...)
925 if (fp == NULL && !(fp = fdopen(rem, "w")))
927 fprintf(fp, "%c", 0x01);
928 fprintf(fp, "rcp: ");
929 vfprintf(fp, fmt, ap);