* Copyright (c) 1983, 1990 The Regents of the University of California.
* 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
"@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)rcp.c 5.32 (Berkeley) 2/25/91";
#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 "dfk:prtx"
#define OPTIONS "dfk:prt"
int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
char *targ
, *shell
, *colon();
while ((ch
= getopt(argc
, argv
, OPTIONS
)) != EOF
)
case 'p': /* preserve access/mod times */
strncpy(dst_realm_buf
, optarg
, REALM_SZ
);
dest_realm
= dst_realm_buf
;
/* des_set_key(cred.session, schedule); */
/* rshd-invoked options (server) */
targetshouldbedirectory
= 1;
shell
= doencrypt
? "ekshell" : "kshell";
sp
= getservbyname(shell
, "tcp");
(void)snprintf(msgbuf
, sizeof(msgbuf
),
"can't get entry for %s/tcp service", shell
);
sp
= getservbyname(shell
= "shell", "tcp");
sp
= getservbyname(shell
= "shell", "tcp");
(void)fprintf(stderr
, "rcp: %s/tcp: unknown service\n", shell
);
if (!(pwd
= getpwuid(userid
= getuid()))) {
(void)fprintf(stderr
, "rcp: unknown user %d.\n", (int)userid
);
/* follow "protocol", send 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]))
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
;
if (thost
= index(argv
[argc
- 1], '@')) {
for (i
= 0; i
< argc
- 1; i
++) {
if (src
) { /* remote to remote */
host
= index(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
? "@" : "",
} 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 *)&tos
, sizeof(int)) < 0)
perror("rcp: setsockopt TOS (ignored)");
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]);
host
= index(argv
[i
], '@');
len
= strlen(src
) + CMDNEEDS
+ 20;
(void)snprintf(bp
, len
, "%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)snprintf(buf
, sizeof(buf
),
"T%ld 0 %ld 0\n", stb
.st_mtime
, stb
.st_atime
);
(void)write(rem
, buf
, (int)strlen(buf
));
(void)snprintf(buf
, sizeof(buf
),
"C%04o %ld %s\n", stb
.st_mode
&07777, stb
.st_size
, last
);
(void)write(rem
, buf
, (int)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 (!(dirp
= opendir(name
))) {
error("rcp: %s: %s\n", name
, strerror(errno
));
last
= rindex(name
, '/');
(void)snprintf(path
, sizeof(path
),
"T%ld 0 %ld 0\n", statp
->st_mtime
, statp
->st_atime
);
(void)write(rem
, path
, (int)strlen(path
));
(void)snprintf(path
, sizeof(path
),
"D%04o %d %s\n", statp
->st_mode
&07777, 0, last
);
(void)write(rem
, path
, (int)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) {
error("%s/%s: name too long.\n", name
, dp
->d_name
);
(void)snprintf(path
, sizeof(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
];
#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, (int)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(need
)))
error("out of memory\n");
(void)snprintf(namebuf
, need
, "%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
);
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");
(void)fprintf(stderr
, "%s\n\t%s\n",
"usage: rcp [-k realm] [-p] f1 f2",
"or: rcp [-k realm] [-rp] 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);