* Copyright (c) 1983, 1990, 1992, 1993
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char copyright
[] =
"@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)rcp.c 8.1 (Berkeley) 5/31/93";
#include <netinet/in_systm.h>
#include <kerberosIV/des.h>
#include <kerberosIV/krb.h>
char dst_realm_buf
[REALM_SZ
];
extern char *krb_realmofhost();
#define OPTIONS "dfKk:prtx"
#define OPTIONS "dfKk:prt"
int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
int kerberos
__P((char **, char *, char *, char *));
void oldw
__P((const char *, ...));
int response
__P((void));
void rsource
__P((char *, struct stat
*));
void sink
__P((int, char *[]));
void source
__P((int, char *[]));
void tolocal
__P((int, char *[]));
void toremote
__P((char *, int, char *[]));
while ((ch
= getopt(argc
, argv
, OPTIONS
)) != EOF
)
switch(ch
) { /* User-visible flags. */
dest_realm
= dst_realm_buf
;
(void)strncpy(dst_realm_buf
, optarg
, REALM_SZ
);
/* des_set_key(cred.session, schedule); */
targetshouldbedirectory
= 1;
shell
= doencrypt
? "ekshell" : "kshell";
if ((sp
= getservbyname(shell
, "tcp")) == NULL
) {
oldw("can't get entry for %s/tcp service", shell
);
sp
= getservbyname(shell
= "shell", "tcp");
sp
= getservbyname(shell
= "shell", "tcp");
sp
= getservbyname(shell
= "shell", "tcp");
errx(1, "%s/tcp: unknown service", shell
);
if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
errx(1, "unknown user %d", (int)userid
);
rem
= STDIN_FILENO
; /* XXX */
if (fflag
) { /* Follow "protocol", send data. */
if (tflag
) { /* Receive data. */
targetshouldbedirectory
= 1;
/* Command to be executed on remote system using "rsh". */
(void)snprintf(cmd
, sizeof(cmd
),
"rcp%s%s%s%s", iamrecursive
? " -r" : "",
(doencrypt
&& use_kerberos
? " -x" : ""),
pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
(void)snprintf(cmd
, sizeof(cmd
), "rcp%s%s%s",
iamrecursive
? " -r" : "", pflag
? " -p" : "",
targetshouldbedirectory
? " -d" : "");
(void)signal(SIGPIPE
, lostconn
);
if (targ
= colon(argv
[argc
- 1])) /* Dest is remote host. */
toremote(targ
, argc
, argv
);
tolocal(argc
, argv
); /* Dest is local host. */
if (targetshouldbedirectory
)
verifydir(argv
[argc
- 1]);
toremote(targ
, argc
, argv
)
char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
if (thost
= strchr(argv
[argc
- 1], '@')) {
for (i
= 0; i
< argc
- 1; i
++) {
if (src
) { /* remote to remote */
host
= strchr(argv
[i
], '@');
len
= strlen(_PATH_RSH
) + strlen(argv
[i
]) +
strlen(src
) + (tuser
? strlen(tuser
) : 0) +
strlen(thost
) + strlen(targ
) + CMDNEEDS
+ 20;
"%s %s -l %s -n %s %s '%s%s%s:%s'",
_PATH_RSH
, host
, suser
, cmd
, src
,
tuser
? tuser
: "", tuser
? "@" : "",
"%s %s -n %s %s '%s%s%s:%s'",
_PATH_RSH
, argv
[i
], cmd
, src
,
tuser
? tuser
: "", tuser
? "@" : "",
(void)susystem(bp
, userid
);
} else { /* local to remote */
len
= strlen(targ
) + CMDNEEDS
+ 20;
(void)snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
rem
= kerberos(&host
, bp
,
tuser
? tuser
: pwd
->pw_name
);
rem
= rcmd(&host
, port
, pwd
->pw_name
,
tuser
? tuser
: pwd
->pw_name
,
if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
,
char *bp
, *host
, *src
, *suser
;
for (i
= 0; i
< argc
- 1; i
++) {
if (!(src
= colon(argv
[i
]))) { /* Local to local. */
len
= strlen(_PATH_CP
) + strlen(argv
[i
]) +
strlen(argv
[argc
- 1]) + 20;
(void)snprintf(bp
, len
, "%s%s%s %s %s", _PATH_CP
,
iamrecursive
? " -r" : "", pflag
? " -p" : "",
argv
[i
], argv
[argc
- 1]);
if (susystem(bp
, userid
))
if ((host
= strchr(argv
[i
], '@')) == NULL
) {
len
= strlen(src
) + CMDNEEDS
+ 20;
if ((bp
= malloc(len
)) == NULL
)
(void)snprintf(bp
, len
, "%s -f %s", cmd
, src
);
kerberos(&host
, bp
, pwd
->pw_name
, suser
) :
rcmd(&host
, port
, pwd
->pw_name
, suser
, bp
, 0);
if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(int)) < 0)
sink(1, argv
+ argc
- 1);
int amt
, fd
, haderr
, indx
, result
;
char *last
, *name
, buf
[BUFSIZ
];
for (indx
= 0; indx
< argc
; ++indx
) {
if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
syserr
: run_err("%s: %s", name
, strerror(errno
));
switch (stb
.st_mode
& S_IFMT
) {
run_err("%s: not a regular file", name
);
if ((last
= strrchr(name
, '/')) == NULL
)
* Make it compatible with possible future
* versions expecting microseconds.
(void)snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
stb
.st_mtimespec
.ts_sec
, stb
.st_atimespec
.ts_sec
);
(void)write(rem
, buf
, strlen(buf
));
#define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
(void)snprintf(buf
, sizeof(buf
), "C%04o %qd %s\n",
stb
.st_mode
& MODEMASK
, stb
.st_size
, last
);
(void)write(rem
, buf
, strlen(buf
));
if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
/* Keep writing after an error so that we stay sync'd up. */
for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
if (i
+ amt
> stb
.st_size
)
result
= read(fd
, bp
->buf
, amt
);
haderr
= result
>= 0 ? EIO
: errno
;
(void)write(rem
, bp
->buf
, amt
);
result
= write(rem
, bp
->buf
, amt
);
haderr
= result
>= 0 ? EIO
: errno
;
if (close(fd
) && !haderr
)
run_err("%s: %s", name
, strerror(haderr
));
char *last
, *vect
[1], path
[MAXPATHLEN
];
if (!(dirp
= opendir(name
))) {
run_err("%s: %s", name
, strerror(errno
));
last
= strrchr(name
, '/');
(void)snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
statp
->st_mtimespec
.ts_sec
, statp
->st_atimespec
.ts_sec
);
(void)write(rem
, path
, strlen(path
));
(void)snprintf(path
, sizeof(path
),
"D%04o %d %s\n", statp
->st_mode
& MODEMASK
, 0, last
);
(void)write(rem
, path
, strlen(path
));
while (dp
= readdir(dirp
)) {
if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
run_err("%s/%s: name too long", name
, dp
->d_name
);
(void)snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
(void)write(rem
, "E\n", 2);
enum { YES
, NO
, DISPLAYED
} wrerr
;
int amt
, count
, exists
, first
, mask
, mode
, ofd
, omode
;
int setimes
, size
, targisdir
, wrerrno
;
char ch
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
];
#define SCREWUP(str) { why = str; goto screwup; }
run_err("ambiguous target");
if (targetshouldbedirectory
)
if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
for (first
= 1;; first
= 0) {
if (read(rem
, cp
, 1) <= 0)
SCREWUP("unexpected <newline>");
if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
SCREWUP("lost connection");
} while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
if (buf
[0] == '\01' || buf
[0] == '\02') {
(void)write(STDERR_FILENO
,
buf
+ 1, strlen(buf
+ 1));
#define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
SCREWUP("mtime.sec not delimited");
SCREWUP("mtime.usec not delimited");
SCREWUP("atime.sec not delimited");
SCREWUP("atime.usec not delimited");
if (*cp
!= 'C' && *cp
!= 'D') {
* Check for the case "rcp remote:foo\* local:bar".
* In this case, the line "No match." can be returned
* by the shell before the rcp command on the remote is
* executed so the ^Aerror_message convention isn't
SCREWUP("expected control record");
for (++cp
; cp
< buf
+ 5; cp
++) {
if (*cp
< '0' || *cp
> '7')
mode
= (mode
<< 3) | (*cp
- '0');
SCREWUP("mode not delimited");
for (size
= 0; isdigit(*cp
);)
size
= size
* 10 + (*cp
++ - '0');
SCREWUP("size not delimited");
need
= strlen(targ
) + strlen(cp
) + 250;
if (!(namebuf
= malloc(need
)))
run_err("%s", strerror(errno
));
(void)snprintf(namebuf
, need
, "%s%s%s", targ
,
exists
= stat(np
, &stb
) == 0;
if (!S_ISDIR(stb
.st_mode
)) {
/* Handle copying from a read-only directory */
if (mkdir(np
, mode
| S_IRWXU
) < 0)
run_err("%s: set times: %s",
if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
bad
: run_err("%s: %s", np
, strerror(errno
));
if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
run_err("%s", j
? strerror(errno
) :
/* Keep reading so we stay sync'd up. */
j
= write(ofd
, bp
->buf
, count
);
wrerrno
= j
>= 0 ? EIO
: errno
;
if (count
!= 0 && wrerr
== NO
&&
(j
= write(ofd
, bp
->buf
, count
)) != count
) {
wrerrno
= j
>= 0 ? EIO
: errno
;
if (ftruncate(ofd
, size
)) {
run_err("%s: truncate: %s", np
, strerror(errno
));
if (exists
|| omode
!= mode
)
run_err("%s: set mode: %s",
if (!exists
&& omode
!= mode
)
if (fchmod(ofd
, omode
& ~mask
))
run_err("%s: set mode: %s",
if (setimes
&& wrerr
== NO
) {
if (utimes(np
, tv
) < 0) {
run_err("%s: set times: %s",
run_err("%s: %s", np
, strerror(wrerrno
));
run_err("protocol error: %s", why
);
kerberos(host
, bp
, locuser
, user
)
char **host
, *bp
, *locuser
, *user
;
dest_realm
= krb_realmofhost(*host
);
port
, user
, bp
, 0, dest_realm
, &cred
, schedule
) :
krcmd(host
, port
, user
, bp
, 0, dest_realm
);
if ((sp
= getservbyname("shell", "tcp")) == NULL
)
errx(1, "unknown service shell/tcp");
if (errno
== ECONNREFUSED
)
oldw("remote host doesn't support Kerberos");
else if (errno
== ENOENT
)
oldw("can't provide Kerberos authentication data");
"the -x option requires Kerberos authentication");
rem
= rcmd(host
, port
, locuser
, user
, bp
, 0);
char ch
, resp
, rbuf
[BUFSIZ
];
if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
case 1: /* error, followed by error msg */
case 2: /* fatal error, "" */
if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
} while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
(void)write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
(void)fprintf(stderr
, "%s\n\t%s\n",
"usage: rcp [-Kpx] [-k realm] f1 f2",
"or: rcp [-Kprx] [-k realm] f1 ... fn directory");
(void)fprintf(stderr
, "%s\n\t%s\n",
"usage: rcp [-Kp] [-k realm] f1 f2",
"or: rcp [-Kpr] [-k realm] f1 ... fn directory");
"usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
oldw(const char *fmt
, ...)
(void)fprintf(stderr
, "rcp: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, ", using standard rcp\n");
run_err(const char *fmt
, ...)
if (fp
== NULL
&& !(fp
= fdopen(rem
, "w")))
(void)fprintf(fp
, "%c", 0x01);
(void)fprintf(fp
, "rcp: ");
(void)vfprintf(fp
, fmt
, ap
);