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
42 static char const copyright[] =
43 "@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
44 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: src/bin/rcp/rcp.c,v 1.26.2.5 2002/08/16 20:06:34 ume Exp $");
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 #include <openssl/des.h>
87 char dst_realm_buf[REALM_SZ];
88 char *dest_realm = NULL;
91 Key_schedule schedule;
92 extern char *krb_realmofhost();
95 #define OPTIONS "46dfKk:prtx"
97 #define OPTIONS "46dfKk:prt"
100 #define OPTIONS "46dfprt"
107 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
108 int family = PF_UNSPEC;
110 static int argc_copy;
111 static char **argv_copy;
114 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
117 int kerberos(char **, char *, char *, char *);
118 void oldw(const char *, ...) __printflike(1, 2);
121 void rsource(char *, struct stat *);
122 void run_err(const char *, ...) __printflike(1, 2);
123 void sink(int, char *[]);
124 void source(int, char *[]);
125 void tolocal(int, char *[]);
126 void toremote(char *, int, char *[]);
130 main(int argc, char *argv[])
133 int ch, fflag, i, tflag;
140 * Prepare for execing ourselves.
142 argc_copy = argc + 1;
143 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
144 if (argv_copy == NULL)
146 argv_copy[0] = argv[0];
148 for (i = 1; i < argc; ++i) {
149 argv_copy[i + 1] = strdup(argv[i]);
150 if (argv_copy[i + 1] == NULL)
151 errx(1, "strdup: out of memory");
153 argv_copy[argc + 1] = NULL;
156 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
157 switch(ch) { /* User-visible flags. */
173 dest_realm = dst_realm_buf;
174 (void)strncpy(dst_realm_buf, optarg, REALM_SZ - 1);
175 dst_realm_buf[REALM_SZ - 1] = '\0';
180 /* des_set_key(cred.session, schedule); */
190 /* Server options. */
192 targetshouldbedirectory = 1;
194 case 'f': /* "from" */
210 k = auth_getval("auth_list");
211 if (k && !strstr(k, "kerberos"))
215 shell = doencrypt ? "ekshell" : "kshell";
219 if ((sp = getservbyname(shell, "tcp")) == NULL) {
221 oldw("can't get entry for %s/tcp service", shell);
222 sp = getservbyname(shell = "shell", "tcp");
225 sp = getservbyname(shell = "shell", "tcp");
227 sp = getservbyname(shell = "shell", "tcp");
230 errx(1, "%s/tcp: unknown service", shell);
233 if ((pwd = getpwuid(userid = getuid())) == NULL)
234 errx(1, "unknown user %d", (int)userid);
236 rem = STDIN_FILENO; /* XXX */
238 if (fflag) { /* Follow "protocol", send data. */
240 (void)setuid(userid);
245 if (tflag) { /* Receive data. */
246 (void)setuid(userid);
254 targetshouldbedirectory = 1;
257 /* Command to be executed on remote system using "rsh". */
259 (void)snprintf(cmd, sizeof(cmd),
260 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
262 (doencrypt && use_kerberos ? " -x" : ""),
266 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
268 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
269 iamrecursive ? " -r" : "", pflag ? " -p" : "",
270 targetshouldbedirectory ? " -d" : "");
273 (void)signal(SIGPIPE, lostconn);
275 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
276 toremote(targ, argc, argv);
278 tolocal(argc, argv); /* Dest is local host. */
279 if (targetshouldbedirectory)
280 verifydir(argv[argc - 1]);
286 toremote(char *targ, int argc, char *argv[])
289 char *bp, *host, *src, *suser, *thost, *tuser;
295 if ((thost = strchr(argv[argc - 1], '@'))) {
298 tuser = argv[argc - 1];
301 else if (!okname(tuser))
304 thost = argv[argc - 1];
308 for (i = 0; i < argc - 1; i++) {
309 src = colon(argv[i]);
310 if (src) { /* remote to remote */
314 host = strchr(argv[i], '@');
315 len = strlen(_PATH_RSH) + strlen(argv[i]) +
316 strlen(src) + (tuser ? strlen(tuser) : 0) +
317 strlen(thost) + strlen(targ) + CMDNEEDS + 20;
318 if (!(bp = malloc(len)))
324 suser = pwd->pw_name;
325 else if (!okname(suser)) {
329 (void)snprintf(bp, len,
330 "%s %s -l %s -n %s %s '%s%s%s:%s'",
331 _PATH_RSH, host, suser, cmd, src,
332 tuser ? tuser : "", tuser ? "@" : "",
335 (void)snprintf(bp, len,
336 "exec %s %s -n %s %s '%s%s%s:%s'",
337 _PATH_RSH, argv[i], cmd, src,
338 tuser ? tuser : "", tuser ? "@" : "",
340 (void)susystem(bp, userid);
342 } else { /* local to remote */
344 len = strlen(targ) + CMDNEEDS + 20;
345 if (!(bp = malloc(len)))
347 (void)snprintf(bp, len, "%s -t %s", cmd, targ);
351 rem = kerberos(&host, bp,
353 tuser ? tuser : pwd->pw_name);
356 rem = rcmd_af(&host, port,
358 tuser ? tuser : pwd->pw_name,
362 if (family == PF_INET) {
363 tos = IPTOS_THROUGHPUT;
364 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
365 &tos, sizeof(int)) < 0)
366 warn("TOS (ignored)");
371 (void)setuid(userid);
379 tolocal(int argc, char *argv[])
382 char *bp, *host, *src, *suser;
384 for (i = 0; i < argc - 1; i++) {
385 if (!(src = colon(argv[i]))) { /* Local to local. */
386 len = strlen(_PATH_CP) + strlen(argv[i]) +
387 strlen(argv[argc - 1]) + 20;
388 if (!(bp = malloc(len)))
390 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
391 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
392 argv[i], argv[argc - 1]);
393 if (susystem(bp, userid))
401 if ((host = strchr(argv[i], '@')) == NULL) {
403 suser = pwd->pw_name;
408 suser = pwd->pw_name;
409 else if (!okname(suser)) {
414 len = strlen(src) + CMDNEEDS + 20;
415 if ((bp = malloc(len)) == NULL)
417 (void)snprintf(bp, len, "%s -f %s", cmd, src);
421 kerberos(&host, bp, pwd->pw_name, suser) :
423 rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
430 (void)seteuid(userid);
431 if (family == PF_INET) {
432 tos = IPTOS_THROUGHPUT;
433 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos,
435 warn("TOS (ignored)");
437 sink(1, argv + argc - 1);
445 source(int argc, char *argv[])
451 int amt, fd, haderr, indx, result;
452 char *last, *name, buf[BUFSIZ];
454 for (indx = 0; indx < argc; ++indx) {
456 if ((fd = open(name, O_RDONLY, 0)) < 0)
458 if (fstat(fd, &stb)) {
459 syserr: run_err("%s: %s", name, strerror(errno));
462 switch (stb.st_mode & S_IFMT) {
472 run_err("%s: not a regular file", name);
475 if ((last = strrchr(name, '/')) == NULL)
481 * Make it compatible with possible future
482 * versions expecting microseconds.
484 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
485 (long)stb.st_mtimespec.tv_sec,
486 (long)stb.st_atimespec.tv_sec);
487 (void)write(rem, buf, strlen(buf));
491 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
492 (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
493 stb.st_mode & MODEMASK, stb.st_size, last);
494 (void)write(rem, buf, strlen(buf));
497 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
498 next: (void)close(fd);
502 /* Keep writing after an error so that we stay sync'd up. */
503 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
505 if (i + amt > stb.st_size)
506 amt = stb.st_size - i;
508 result = read(fd, bp->buf, amt);
510 haderr = result >= 0 ? EIO : errno;
513 (void)write(rem, bp->buf, amt);
515 result = write(rem, bp->buf, amt);
517 haderr = result >= 0 ? EIO : errno;
520 if (close(fd) && !haderr)
523 (void)write(rem, "", 1);
525 run_err("%s: %s", name, strerror(haderr));
531 rsource(char *name, struct stat *statp)
535 char *last, *vect[1], path[PATH_MAX];
537 if (!(dirp = opendir(name))) {
538 run_err("%s: %s", name, strerror(errno));
541 last = strrchr(name, '/');
547 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
548 (long)statp->st_mtimespec.tv_sec,
549 (long)statp->st_atimespec.tv_sec);
550 (void)write(rem, path, strlen(path));
551 if (response() < 0) {
556 (void)snprintf(path, sizeof(path),
557 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
558 (void)write(rem, path, strlen(path));
559 if (response() < 0) {
563 while ((dp = readdir(dirp))) {
566 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
568 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
569 run_err("%s/%s: name too long", name, dp->d_name);
572 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
576 (void)closedir(dirp);
577 (void)write(rem, "E\n", 2);
582 sink(int argc, char *argv[])
586 struct timeval tv[2];
587 enum { YES, NO, DISPLAYED } wrerr;
590 int amt, count, exists, first, mask, mode, ofd, omode;
591 int setimes, targisdir, wrerrno = 0;
592 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
596 #define SCREWUP(str) { why = str; goto screwup; }
598 setimes = targisdir = 0;
603 run_err("ambiguous target");
607 if (targetshouldbedirectory)
609 (void)write(rem, "", 1);
610 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
612 for (first = 1;; first = 0) {
614 if (read(rem, cp, 1) <= 0)
617 SCREWUP("unexpected <newline>");
619 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
620 SCREWUP("lost connection");
622 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
625 if (buf[0] == '\01' || buf[0] == '\02') {
627 (void)write(STDERR_FILENO,
628 buf + 1, strlen(buf + 1));
635 (void)write(rem, "", 1);
646 mtime.tv_sec = strtol(cp, &cp, 10);
647 if (!cp || *cp++ != ' ')
648 SCREWUP("mtime.sec not delimited");
649 mtime.tv_usec = strtol(cp, &cp, 10);
650 if (!cp || *cp++ != ' ')
651 SCREWUP("mtime.usec not delimited");
652 atime.tv_sec = strtol(cp, &cp, 10);
653 if (!cp || *cp++ != ' ')
654 SCREWUP("atime.sec not delimited");
655 atime.tv_usec = strtol(cp, &cp, 10);
656 if (!cp || *cp++ != '\0')
657 SCREWUP("atime.usec not delimited");
658 (void)write(rem, "", 1);
661 if (*cp != 'C' && *cp != 'D') {
663 * Check for the case "rcp remote:foo\* local:bar".
664 * In this case, the line "No match." can be returned
665 * by the shell before the rcp command on the remote is
666 * executed so the ^Aerror_message convention isn't
673 SCREWUP("expected control record");
676 for (++cp; cp < buf + 5; cp++) {
677 if (*cp < '0' || *cp > '7')
679 mode = (mode << 3) | (*cp - '0');
682 SCREWUP("mode not delimited");
684 for (size = 0; isdigit(*cp);)
685 size = size * 10 + (*cp++ - '0');
687 SCREWUP("size not delimited");
689 static char *namebuf = NULL;
690 static size_t cursize;
693 need = strlen(targ) + strlen(cp) + 250;
694 if (need > cursize) {
697 if (!(namebuf = malloc(need)))
698 run_err("%s", strerror(errno));
701 (void)snprintf(namebuf, need, "%s%s%s", targ,
702 *targ ? "/" : "", cp);
706 exists = stat(np, &stb) == 0;
708 int mod_flag = pflag;
710 if (!S_ISDIR(stb.st_mode)) {
715 (void)chmod(np, mode);
717 /* Handle copying from a read-only directory */
719 if (mkdir(np, mode | S_IRWXU) < 0)
726 if (utimes(np, tv) < 0)
727 run_err("%s: set times: %s",
728 np, strerror(errno));
731 (void)chmod(np, mode);
736 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
737 bad: run_err("%s: %s", np, strerror(errno));
740 (void)write(rem, "", 1);
741 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
747 for (count = i = 0; i < size; i += BUFSIZ) {
753 j = read(rem, cp, amt);
755 run_err("%s", j ? strerror(errno) :
756 "dropped connection");
762 if (count == bp->cnt) {
763 /* Keep reading so we stay sync'd up. */
765 j = write(ofd, bp->buf, count);
768 wrerrno = j >= 0 ? EIO : errno;
775 if (count != 0 && wrerr == NO &&
776 (j = write(ofd, bp->buf, count)) != count) {
778 wrerrno = j >= 0 ? EIO : errno;
780 if (ftruncate(ofd, size)) {
781 run_err("%s: truncate: %s", np, strerror(errno));
785 if (exists || omode != mode)
786 if (fchmod(ofd, omode))
787 run_err("%s: set mode: %s",
788 np, strerror(errno));
790 if (!exists && omode != mode)
791 if (fchmod(ofd, omode & ~mask))
792 run_err("%s: set mode: %s",
793 np, strerror(errno));
797 if (setimes && wrerr == NO) {
799 if (utimes(np, tv) < 0) {
800 run_err("%s: set times: %s",
801 np, strerror(errno));
807 run_err("%s: %s", np, strerror(wrerrno));
810 (void)write(rem, "", 1);
817 run_err("protocol error: %s", why);
823 kerberos(char **host, char *bp, char *locuser, char *user)
829 if (dest_realm == NULL)
830 dest_realm = krb_realmofhost(*host);
835 port, user, bp, 0, dest_realm, &cred, schedule) :
837 krcmd(host, port, user, bp, 0, dest_realm);
840 if (errno == ECONNREFUSED)
841 oldw("remote host doesn't support Kerberos");
842 else if (errno == ENOENT)
843 oldw("can't provide Kerberos authentication data");
844 execv(_PATH_RCP, argv_copy);
845 err(1, "execv: %s", _PATH_RCP);
851 "the -x option requires Kerberos authentication");
853 rem = rcmd_af(host, port, locuser, user, bp, 0, family);
857 #endif /* KERBEROS */
862 char ch, *cp, resp, rbuf[BUFSIZ];
864 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
874 case 1: /* error, followed by error msg */
875 case 2: /* fatal error, "" */
877 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
880 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
883 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
897 (void)fprintf(stderr, "%s\n%s\n",
898 "usage: rcp [-46Kpx] [-k realm] f1 f2",
899 " rcp [-46Kprx] [-k realm] f1 ... fn directory");
901 (void)fprintf(stderr, "%s\n%s\n",
902 "usage: rcp [-46Kp] [-k realm] f1 f2",
903 " rcp [-46Kpr] [-k realm] f1 ... fn directory");
906 (void)fprintf(stderr, "%s\n%s\n",
907 "usage: rcp [-46p] f1 f2",
908 " rcp [-46pr] f1 ... fn directory");
917 oldw(const char *fmt, ...)
921 (void)fprintf(stderr, "rcp: ");
922 (void)vfprintf(stderr, fmt, ap);
923 (void)fprintf(stderr, ", using standard rcp\n");
929 run_err(const char *fmt, ...)
936 if (fp == NULL && !(fp = fdopen(rem, "w")))
938 (void)fprintf(fp, "%c", 0x01);
939 (void)fprintf(fp, "rcp: ");
940 (void)vfprintf(fp, fmt, ap);
941 (void)fprintf(fp, "\n");