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. 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
36 * @(#) Copyright (c) 1983, 1990, 1992, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)rcp.c 8.2 (Berkeley) 4/2/94
38 * $FreeBSD: src/bin/rcp/rcp.c,v 1.26.2.6 2004/09/16 12:16:10 delphij Exp $
41 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
69 #include <openssl/des.h>
73 static char dst_realm_buf
[REALM_SZ
];
74 char *dest_realm
= NULL
;
77 Key_schedule schedule
;
78 extern char *krb_realmofhost();
81 #define OPTIONS "46dfKk:prtx"
83 #define OPTIONS "46dfKk:prt"
86 #define OPTIONS "46dfprt"
89 static struct passwd
*pwd
;
93 static int pflag
, targetshouldbedirectory
, iamrecursive
;
95 static int family
= PF_UNSPEC
;
98 static char **argv_copy
;
101 static char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
104 int kerberos(char **, char *, char *, char *);
105 void oldw(const char *, ...) __printflike(1, 2);
108 void rsource(char *, struct stat
*);
109 void sink(int, char *[]);
110 void source(int, char *[]);
111 void tolocal(int, char *[]);
112 void toremote(char *, int, char *[]);
116 main(int argc
, char *argv
[])
119 int ch
, fflag
, i
, tflag
;
126 * Prepare for execing ourselves.
128 argc_copy
= argc
+ 1;
129 argv_copy
= malloc((argc_copy
+ 1) * sizeof(*argv_copy
));
130 if (argv_copy
== NULL
)
132 argv_copy
[0] = argv
[0];
133 argv_copy
[1] = __DECONST(char *, "-K");
134 for (i
= 1; i
< argc
; ++i
) {
135 argv_copy
[i
+ 1] = strdup(argv
[i
]);
136 if (argv_copy
[i
+ 1] == NULL
)
137 errx(1, "strdup: out of memory");
139 argv_copy
[argc
+ 1] = NULL
;
142 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1)
143 switch(ch
) { /* User-visible flags. */
159 dest_realm
= dst_realm_buf
;
160 strncpy(dst_realm_buf
, optarg
, REALM_SZ
- 1);
161 dst_realm_buf
[REALM_SZ
- 1] = '\0';
166 /* des_set_key(cred.session, schedule); */
176 /* Server options. */
178 targetshouldbedirectory
= 1;
180 case 'f': /* "from" */
196 k
= auth_getval("auth_list");
197 if (k
&& !strstr(k
, "kerberos"))
201 shell
= doencrypt
? __DECONST(char *, "ekshell") :
202 __DECONST(char *, "kshell");
204 shell
= __DECONST(char *, "kshell");
206 if ((sp
= getservbyname(shell
, "tcp")) == NULL
) {
208 oldw("can't get entry for %s/tcp service", shell
);
209 sp
= getservbyname(shell
= "shell", "tcp");
212 shell
= __DECONST(char *, "shell");
213 sp
= getservbyname(shell
, "tcp");
215 shell
= __DECONST(char *, "shell");
216 sp
= getservbyname(shell
, "tcp");
219 errx(1, "%s/tcp: unknown service", shell
);
222 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
223 errx(1, "unknown user %d", (int)userid
);
225 rem
= STDIN_FILENO
; /* XXX */
227 if (fflag
) { /* Follow "protocol", send data. */
234 if (tflag
) { /* Receive data. */
243 targetshouldbedirectory
= 1;
246 /* Command to be executed on remote system using "rsh". */
248 snprintf(cmd
, sizeof(cmd
),
249 "rcp%s%s%s%s", iamrecursive
? " -r" : "",
251 (doencrypt
&& use_kerberos
? " -x" : ""),
255 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
257 snprintf(cmd
, sizeof(cmd
), "rcp%s%s%s",
258 iamrecursive
? " -r" : "", pflag
? " -p" : "",
259 targetshouldbedirectory
? " -d" : "");
262 signal(SIGPIPE
, lostconn
);
264 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
265 toremote(targ
, argc
, argv
);
267 tolocal(argc
, argv
); /* Dest is local host. */
268 if (targetshouldbedirectory
)
269 verifydir(argv
[argc
- 1]);
275 toremote(char *targ
, int argc
, char *argv
[])
278 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
282 targ
= __DECONST(char *, ".");
284 if ((thost
= strchr(argv
[argc
- 1], '@'))) {
287 tuser
= argv
[argc
- 1];
290 else if (!okname(tuser
))
293 thost
= argv
[argc
- 1];
297 for (i
= 0; i
< argc
- 1; i
++) {
298 src
= colon(argv
[i
]);
299 if (src
) { /* remote to remote */
302 src
= __DECONST(char *, ".");
303 host
= strchr(argv
[i
], '@');
304 len
= strlen(_PATH_RSH
) + strlen(argv
[i
]) +
305 strlen(src
) + (tuser
? strlen(tuser
) : 0) +
306 strlen(thost
) + strlen(targ
) + CMDNEEDS
+ 20;
307 if (!(bp
= malloc(len
)))
313 suser
= pwd
->pw_name
;
314 else if (!okname(suser
)) {
319 "%s %s -l %s -n %s %s '%s%s%s:%s'",
320 _PATH_RSH
, host
, suser
, cmd
, src
,
321 tuser
? tuser
: "", tuser
? "@" : "",
325 "exec %s %s -n %s %s '%s%s%s:%s'",
326 _PATH_RSH
, argv
[i
], cmd
, src
,
327 tuser
? tuser
: "", tuser
? "@" : "",
329 susystem(bp
, userid
);
331 } else { /* local to remote */
333 len
= strlen(targ
) + CMDNEEDS
+ 20;
334 if (!(bp
= malloc(len
)))
336 snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
340 rem
= kerberos(&host
, bp
,
342 tuser
? tuser
: pwd
->pw_name
);
345 rem
= rcmd_af(&host
, port
,
347 tuser
? tuser
: pwd
->pw_name
,
351 if (family
== PF_INET
) {
352 tos
= IPTOS_THROUGHPUT
;
353 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
,
354 &tos
, sizeof(int)) < 0)
355 warn("TOS (ignored)");
368 tolocal(int argc
, char *argv
[])
371 char *bp
, *host
, *src
, *suser
;
373 for (i
= 0; i
< argc
- 1; i
++) {
374 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
375 len
= strlen(_PATH_CP
) + strlen(argv
[i
]) +
376 strlen(argv
[argc
- 1]) + 20;
377 if (!(bp
= malloc(len
)))
379 snprintf(bp
, len
, "exec %s%s%s %s %s", _PATH_CP
,
380 iamrecursive
? " -PR" : "", pflag
? " -p" : "",
381 argv
[i
], argv
[argc
- 1]);
382 if (susystem(bp
, userid
))
389 src
= __DECONST(char *, ".");
390 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
392 suser
= pwd
->pw_name
;
397 suser
= pwd
->pw_name
;
398 else if (!okname(suser
)) {
403 len
= strlen(src
) + CMDNEEDS
+ 20;
404 if ((bp
= malloc(len
)) == NULL
)
406 snprintf(bp
, len
, "%s -f %s", cmd
, src
);
410 kerberos(&host
, bp
, pwd
->pw_name
, suser
) :
412 rcmd_af(&host
, port
, pwd
->pw_name
, suser
, bp
, 0,
420 if (family
== PF_INET
) {
421 tos
= IPTOS_THROUGHPUT
;
422 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
, &tos
,
424 warn("TOS (ignored)");
426 sink(1, argv
+ argc
- 1);
434 source(int argc
, char *argv
[])
440 int amt
, fd
, haderr
, indx
, result
;
441 char *last
, *name
, buf
[BUFSIZ
];
443 for (indx
= 0; indx
< argc
; ++indx
) {
445 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
447 if (fstat(fd
, &stb
)) {
448 syserr
: run_err("%s: %s", name
, strerror(errno
));
451 switch (stb
.st_mode
& S_IFMT
) {
461 run_err("%s: not a regular file", name
);
464 if ((last
= strrchr(name
, '/')) == NULL
)
470 * Make it compatible with possible future
471 * versions expecting microseconds.
473 snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
474 (long)stb
.st_mtimespec
.tv_sec
,
475 (long)stb
.st_atimespec
.tv_sec
);
476 write(rem
, buf
, strlen(buf
));
480 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
481 snprintf(buf
, sizeof(buf
), "C%04o %jd %s\n",
482 stb
.st_mode
& MODEMASK
, (intmax_t)stb
.st_size
, last
);
483 write(rem
, buf
, strlen(buf
));
486 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
487 next
: (void)close(fd
);
491 /* Keep writing after an error so that we stay sync'd up. */
492 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
494 if (i
+ amt
> stb
.st_size
)
495 amt
= stb
.st_size
- i
;
497 result
= read(fd
, bp
->buf
, amt
);
499 haderr
= result
>= 0 ? EIO
: errno
;
502 write(rem
, bp
->buf
, amt
);
504 result
= write(rem
, bp
->buf
, amt
);
506 haderr
= result
>= 0 ? EIO
: errno
;
509 if (close(fd
) && !haderr
)
514 run_err("%s: %s", name
, strerror(haderr
));
520 rsource(char *name
, struct stat
*statp
)
524 char *last
, *vect
[1], path
[PATH_MAX
];
526 if (!(dirp
= opendir(name
))) {
527 run_err("%s: %s", name
, strerror(errno
));
530 last
= strrchr(name
, '/');
536 snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
537 (long)statp
->st_mtimespec
.tv_sec
,
538 (long)statp
->st_atimespec
.tv_sec
);
539 write(rem
, path
, strlen(path
));
540 if (response() < 0) {
545 snprintf(path
, sizeof(path
),
546 "D%04o %d %s\n", statp
->st_mode
& MODEMASK
, 0, last
);
547 write(rem
, path
, strlen(path
));
548 if (response() < 0) {
552 while ((dp
= readdir(dirp
))) {
555 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
557 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
)) {
558 run_err("%s/%s: name too long", name
, dp
->d_name
);
561 snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
566 write(rem
, "E\n", 2);
571 sink(int argc
, char *argv
[])
575 struct timeval tv
[2];
576 enum { YES
, NO
, DISPLAYED
} wrerr
;
580 int amt
, exists
, first
, mask
, mode
, ofd
, omode
;
581 int setimes
, targisdir
, wrerrno
= 0;
582 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
], path
[PATH_MAX
];
586 #define SCREWUP(str) { why = __DECONST(char *, str); goto screwup; }
588 setimes
= targisdir
= 0;
593 run_err("ambiguous target");
597 if (targetshouldbedirectory
)
600 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
602 for (first
= 1;; first
= 0) {
604 if (read(rem
, cp
, 1) <= 0)
607 SCREWUP("unexpected <newline>");
609 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
610 SCREWUP("lost connection");
612 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
615 if (buf
[0] == '\01' || buf
[0] == '\02') {
618 buf
+ 1, strlen(buf
+ 1));
636 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
637 if (!cp
|| *cp
++ != ' ')
638 SCREWUP("mtime.sec not delimited");
639 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
640 if (!cp
|| *cp
++ != ' ')
641 SCREWUP("mtime.usec not delimited");
642 atime
.tv_sec
= strtol(cp
, &cp
, 10);
643 if (!cp
|| *cp
++ != ' ')
644 SCREWUP("atime.sec not delimited");
645 atime
.tv_usec
= strtol(cp
, &cp
, 10);
646 if (!cp
|| *cp
++ != '\0')
647 SCREWUP("atime.usec not delimited");
651 if (*cp
!= 'C' && *cp
!= 'D') {
653 * Check for the case "rcp remote:foo\* local:bar".
654 * In this case, the line "No match." can be returned
655 * by the shell before the rcp command on the remote is
656 * executed so the ^Aerror_message convention isn't
663 SCREWUP("expected control record");
666 for (++cp
; cp
< buf
+ 5; cp
++) {
667 if (*cp
< '0' || *cp
> '7')
669 mode
= (mode
<< 3) | (*cp
- '0');
672 SCREWUP("mode not delimited");
674 for (size
= 0; isdigit(*cp
);)
675 size
= size
* 10 + (*cp
++ - '0');
677 SCREWUP("size not delimited");
679 if (strlen(targ
) + (*targ
? 1 : 0) + strlen(cp
)
681 run_err("%s%s%s: name too long", targ
,
682 *targ
? "/" : "", cp
);
685 snprintf(path
, sizeof(path
), "%s%s%s", targ
,
686 *targ
? "/" : "", cp
);
690 exists
= stat(np
, &stb
) == 0;
692 int mod_flag
= pflag
;
694 if (!S_ISDIR(stb
.st_mode
)) {
701 /* Handle copying from a read-only directory */
703 if (mkdir(np
, mode
| S_IRWXU
) < 0)
710 if (utimes(np
, tv
) < 0)
711 run_err("%s: set times: %s",
712 np
, strerror(errno
));
720 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
721 bad
: run_err("%s: %s", np
, strerror(errno
));
725 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
731 for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
737 j
= read(rem
, cp
, amt
);
739 run_err("%s", j
? strerror(errno
) :
740 "dropped connection");
746 if (count
== bp
->cnt
) {
747 /* Keep reading so we stay sync'd up. */
749 j
= write(ofd
, bp
->buf
, count
);
750 if (j
!= (off_t
)count
) {
752 wrerrno
= j
>= 0 ? EIO
: errno
;
759 if (count
!= 0 && wrerr
== NO
&&
760 (j
= write(ofd
, bp
->buf
, count
)) != (off_t
)count
) {
762 wrerrno
= j
>= 0 ? EIO
: errno
;
764 if (ftruncate(ofd
, size
)) {
765 run_err("%s: truncate: %s", np
, strerror(errno
));
769 if (exists
|| omode
!= mode
)
770 if (fchmod(ofd
, omode
))
771 run_err("%s: set mode: %s",
772 np
, strerror(errno
));
774 if (!exists
&& omode
!= mode
)
775 if (fchmod(ofd
, omode
& ~mask
))
776 run_err("%s: set mode: %s",
777 np
, strerror(errno
));
781 if (setimes
&& wrerr
== NO
) {
783 if (utimes(np
, tv
) < 0) {
784 run_err("%s: set times: %s",
785 np
, strerror(errno
));
791 run_err("%s: %s", np
, strerror(wrerrno
));
801 run_err("protocol error: %s", why
);
807 kerberos(char **host
, char *bp
, char *locuser
, char *user
)
813 if (dest_realm
== NULL
)
814 dest_realm
= krb_realmofhost(*host
);
819 port
, user
, bp
, 0, dest_realm
, &cred
, schedule
) :
821 krcmd(host
, port
, user
, bp
, 0, dest_realm
);
824 if (errno
== ECONNREFUSED
)
825 oldw("remote host doesn't support Kerberos");
826 else if (errno
== ENOENT
)
827 oldw("can't provide Kerberos authentication data");
828 execv(_PATH_RCP
, argv_copy
);
829 err(1, "execv: %s", _PATH_RCP
);
835 "the -x option requires Kerberos authentication");
837 rem
= rcmd_af(host
, port
, locuser
, user
, bp
, 0, family
);
841 #endif /* KERBEROS */
846 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
848 if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
858 case 1: /* error, followed by error msg */
859 case 2: /* fatal error, "" */
861 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
864 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
867 write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
881 fprintf(stderr
, "%s\n%s\n",
882 "usage: rcp [-46Kpx] [-k realm] f1 f2",
883 " rcp [-46Kprx] [-k realm] f1 ... fn directory");
885 fprintf(stderr
, "%s\n%s\n",
886 "usage: rcp [-46Kp] [-k realm] f1 f2",
887 " rcp [-46Kpr] [-k realm] f1 ... fn directory");
890 fprintf(stderr
, "%s\n%s\n",
891 "usage: rcp [-46p] f1 f2",
892 " rcp [-46pr] f1 ... fn directory");
901 oldw(const char *fmt
, ...)
905 fprintf(stderr
, "rcp: ");
906 vfprintf(stderr
, fmt
, ap
);
907 fprintf(stderr
, ", using standard rcp\n");
913 run_err(const char *fmt
, ...)
920 if (fp
== NULL
&& !(fp
= fdopen(rem
, "w")))
922 fprintf(fp
, "%c", 0x01);
923 fprintf(fp
, "rcp: ");
924 vfprintf(fp
, fmt
, ap
);