* Copyright (c) 1983, 1990 The Regents of the University of California.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)rcp.c 5.29 (Berkeley) %G%";
#include <netinet/in_systm.h>
#include <kerberosIV/des.h>
#include <kerberosIV/krb.h>
char dst_realm_buf
[REALM_SZ
];
int use_kerberos
= 1, encrypt
= 0;
extern char *krb_realmofhost();
#define OPTIONS "dfkprtx"
int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
struct passwd
*getpwuid();
while ((ch
= getopt(argc
, argv
, OPTIONS
)) != EOF
)
case 'p': /* preserve access/mod times */
strncpy(dst_realm_buf
, ++argv
, REALM_SZ
);
dest_realm
= dst_realm_buf
;
/* des_set_key(cred.session, schedule); */
/* rshd-invoked options (server) */
targetshouldbedirectory
= 1;
sp
= getservbyname((encrypt
? "ekshell" : "kshell"), "tcp");
(void) sprintf(msgbuf
, "can't get entry for %s/tcp service",
(encrypt
? "ekshell" : "kshell"));
sp
= getservbyname("shell", "tcp");
sp
= getservbyname("shell", "tcp");
(void)fprintf(stderr
, "rcp: shell/tcp: unknown service\n");
if (!(pwd
= getpwuid(userid
= getuid()))) {
(void)fprintf(stderr
, "rcp: unknown user %d.\n", userid
);
/* follow "protocol", send data */
targetshouldbedirectory
= 1;
/* command to be executed on remote system using "rsh" */
(void)sprintf(cmd
, "rcp%s%s%s%s", iamrecursive
? " -r" : "",
((encrypt
&& use_kerberos
) ? " -x" : ""),
pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
(void)sprintf(cmd
, "rcp%s%s%s", iamrecursive
? " -r" : "",
pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
(void)signal(SIGPIPE
, lostconn
);
if (targ
= colon(argv
[argc
- 1]))
toremote(targ
, argc
, argv
); /* destination is remote host */
tolocal(argc
, argv
); /* destination is local host */
if (targetshouldbedirectory
)
verifydir(argv
[argc
- 1]);
toremote(targ
, argc
, argv
)
char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
char *colon(), *malloc();
if (thost
= index(argv
[argc
- 1], '@')) {
for (i
= 0; i
< argc
- 1; i
++) {
if (src
) { /* remote to remote */
host
= index(argv
[i
], '@');
if (!(bp
= malloc((u_int
)(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
? "@" : "",
(void)sprintf(bp
, "%s %s -n %s %s '%s%s%s:%s'",
_PATH_RSH
, argv
[i
], cmd
, src
,
tuser
? tuser
: "", tuser
? "@" : "",
} else { /* local to remote */
if (!(bp
= malloc((u_int
)strlen(targ
) +
(void)sprintf(bp
, "%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 *)&tos
, sizeof(int)) < 0)
perror("rcp: setsockopt TOS (ignored)");
char *bp
, *host
, *src
, *suser
;
char *colon(), *malloc();
for (i
= 0; i
< argc
- 1; i
++) {
if (!(src
= colon(argv
[i
]))) { /* local to local */
if (!(bp
= malloc((u_int
)(strlen(_PATH_CP
) +
strlen(argv
[i
]) + strlen(argv
[argc
- 1])) + 20)))
(void)sprintf(bp
, "%s%s%s %s %s", _PATH_CP
,
iamrecursive
? " -r" : "", pflag
? " -p" : "",
argv
[i
], argv
[argc
- 1]);
host
= index(argv
[i
], '@');
if (!(bp
= malloc((u_int
)(strlen(src
)) + CMDNEEDS
+ 20)))
(void)sprintf(bp
, "%s -f %s", cmd
, src
);
rem
= kerberos(&host
, bp
, pwd
->pw_name
, suser
);
rem
= rcmd(&host
, port
, pwd
->pw_name
, suser
, bp
, 0);
if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
,
(char *)&tos
, sizeof(int)) < 0)
perror("rcp: setsockopt TOS (ignored)");
sink(1, argv
+ argc
- 1);
if (stat(cp
, &stb
) >= 0) {
if ((stb
.st_mode
& S_IFMT
) == S_IFDIR
)
error("rcp: %s: %s.\n", cp
, strerror(errno
));
if (!isalpha(c
) && !isdigit(c
) && c
!= '_' && c
!= '-')
(void)fprintf(stderr
, "rcp: invalid user name %s\n", cp0
);
register sig_t istat
, qstat
;
if ((pid
= vfork()) == 0) {
execl(_PATH_BSHELL
, "sh", "-c", s
, (char *)0);
istat
= signal(SIGINT
, SIG_IGN
);
qstat
= signal(SIGQUIT
, SIG_IGN
);
while ((w
= wait(&status
)) != pid
&& w
!= -1)
(void)signal(SIGINT
, istat
);
(void)signal(SIGQUIT
, qstat
);
char *last
, *name
, buf
[BUFSIZ
];
for (x
= 0; x
< argc
; x
++) {
if ((f
= open(name
, O_RDONLY
, 0)) < 0) {
error("rcp: %s: %s\n", name
, strerror(errno
));
switch (stb
.st_mode
&S_IFMT
) {
error("rcp: %s: not a plain file\n", name
);
last
= rindex(name
, '/');
* Make it compatible with possible future
* versions expecting microseconds.
(void)sprintf(buf
, "T%ld 0 %ld 0\n", stb
.st_mtime
,
(void)write(rem
, buf
, strlen(buf
));
(void)sprintf(buf
, "C%04o %ld %s\n", stb
.st_mode
&07777,
(void)write(rem
, buf
, strlen(buf
));
if ((bp
= allocbuf(&buffer
, f
, BUFSIZ
)) == 0) {
for (i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
if (i
+ amt
> stb
.st_size
)
if (readerr
== 0 && read(f
, bp
->buf
, amt
) != amt
)
(void)write(rem
, bp
->buf
, amt
);
error("rcp: %s: %s\n", name
, strerror(readerr
));
char *last
, *vect
[1], path
[MAXPATHLEN
];
if (!(d
= opendir(name
))) {
error("rcp: %s: %s\n", name
, strerror(errno
));
last
= rindex(name
, '/');
(void)sprintf(path
, "T%ld 0 %ld 0\n", statp
->st_mtime
,
(void)write(rem
, path
, strlen(path
));
(void)sprintf(path
, "D%04o %d %s\n", statp
->st_mode
&07777, 0, last
);
(void)write(rem
, path
, strlen(path
));
while (dp
= readdir(d
)) {
if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
error("%s/%s: name too long.\n", name
, dp
->d_name
);
(void)sprintf(path
, "%s/%s", name
, dp
->d_name
);
(void)write(rem
, "E\n", 2);
char ch
, resp
, rbuf
[BUFSIZ
];
if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
case 1: /* error, followed by err msg */
case 2: /* fatal error, "" */
if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
} while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
(void)write(2, rbuf
, cp
- rbuf
);
(void)fprintf(stderr
, "rcp: lost connection\n");
enum { YES
, NO
, DISPLAYED
} wrerr
;
int amt
, count
, exists
, first
, mask
, mode
;
int ofd
, setimes
, size
, targisdir
;
char *np
, *vect
[1], buf
[BUFSIZ
], *malloc();
#define SCREWUP(str) { why = str; goto screwup; }
error("rcp: ambiguous target\n");
if (targetshouldbedirectory
)
if (stat(targ
, &stb
) == 0 && (stb
.st_mode
& S_IFMT
) == S_IFDIR
)
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(2, 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");
size
= size
* 10 + (*cp
++ - '0');
SCREWUP("size not delimited");
need
= strlen(targ
) + strlen(cp
) + 250;
if (!(namebuf
= malloc((u_int
)need
)))
error("out of memory\n");
(void)sprintf(namebuf
, "%s%s%s", targ
,
exists
= stat(np
, &stb
) == 0;
if ((stb
.st_mode
&S_IFMT
) != S_IFDIR
) {
} else if (mkdir(np
, mode
) < 0)
error("rcp: can't set times on %s: %s\n",
if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
bad
: error("rcp: %s: %s\n", np
, strerror(errno
));
if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == 0) {
for (i
= 0; i
< size
; i
+= BUFSIZ
) {
write(ofd
, bp
->buf
, count
) != count
)
if (count
!= 0 && wrerr
== NO
&&
write(ofd
, bp
->buf
, count
) != count
)
if (ftruncate(ofd
, size
)) {
error("rcp: can't truncate %s: %s\n", np
,
if (setimes
&& wrerr
== NO
) {
if (utimes(np
, tv
) < 0) {
error("rcp: can't set times on %s: %s\n",
error("rcp: %s: %s\n", np
, strerror(errno
));
error("rcp: protocol screwup: %s\n", why
);
allocbuf(bp
, fd
, blksize
)
if (fstat(fd
, &stb
) < 0) {
error("rcp: fstat: %s\n", strerror(errno
));
size
= roundup(stb
.st_blksize
, blksize
);
bp
->buf
= (char *)malloc((u_int
)size
);
error("rcp: malloc: out of memory\n");
if (!fp
&& !(fp
= fdopen(rem
, "w")))
(void)fprintf(fp
, "%c", 0x01);
(void)fprintf(fp
, fmt
, a1
, a2
, a3
);
(void)fprintf(stderr
, fmt
, a1
, a2
, a3
);
(void)fprintf(stderr
, "rcp: out of memory.\n");
(void)fprintf(stderr
, "%s\n\t%s\n",
"usage: rcp [-k realm] [-px] f1 f2",
"or: rcp [-k realm] [-rpx] f1 ... fn directory");
"usage: rcp [-p] f1 f2; or: rcp [-rp] f1 ... fn directory\n");
(void)fprintf(stderr
, "rcp: warning: %s, using standard rcp\n", str
);
kerberos(host
, bp
, locuser
, user
)
char **host
, *bp
, *locuser
, *user
;
dest_realm
= krb_realmofhost(*host
);
user
, bp
, 0, dest_realm
);
sp
= getservbyname("shell", "tcp");
"rcp: unknown service shell/tcp\n");
if (errno
== ECONNREFUSED
)
"remote host doesn't support Kerberos");
"Can't provide Kerberos auth data");
"The -x option requires Kerberos authentication\n");
rem
= rcmd(host
, sp
->s_port
, locuser
, user
, bp
, 0);