2 * Copyright (c) 1983, 1990, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #define RSH_PROGRAM "rsh"
37 int errs
, remin
, remout
;
38 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
39 int doencrypt
, noencrypt
;
40 int usebroken
, usekrb4
, usekrb5
, forwardtkt
;
45 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
48 void rsource (char *, struct stat
*);
49 void sink (int, char *[]);
50 void source (int, char *[]);
51 void tolocal (int, char *[]);
52 void toremote (char *, int, char *[]);
54 int do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
);
56 static int fflag
, tflag
;
58 static int version_flag
, help_flag
;
60 struct getargs args
[] = {
61 { NULL
, '4', arg_flag
, &usekrb4
, "use Kerberos 4 authentication" },
62 { NULL
, '5', arg_flag
, &usekrb5
, "use Kerberos 5 authentication" },
63 { NULL
, 'F', arg_flag
, &forwardtkt
, "forward credentials" },
64 { NULL
, 'K', arg_flag
, &usebroken
, "use BSD authentication" },
65 { NULL
, 'P', arg_string
, &port
, "non-default port", "port" },
66 { NULL
, 'p', arg_flag
, &pflag
, "preserve file permissions" },
67 { NULL
, 'r', arg_flag
, &iamrecursive
, "recursive mode" },
68 { NULL
, 'x', arg_flag
, &doencrypt
, "use encryption" },
69 { NULL
, 'z', arg_flag
, &noencrypt
, "don't encrypt" },
70 { NULL
, 'd', arg_flag
, &targetshouldbedirectory
},
71 { NULL
, 'e', arg_flag
, &eflag
, "passed to rsh" },
72 { NULL
, 'f', arg_flag
, &fflag
},
73 { NULL
, 't', arg_flag
, &tflag
},
74 { "version", 0, arg_flag
, &version_flag
},
75 { "help", 0, arg_flag
, &help_flag
}
82 sizeof(args
) / sizeof(args
[0]),
84 "file1 file2|file... directory");
89 main(int argc
, char **argv
)
95 if (getarg (args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
,
101 print_version (NULL
);
105 iamremote
= (fflag
|| tflag
);
110 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
111 errx(1, "unknown user %d", (int)userid
);
113 remin
= STDIN_FILENO
; /* XXX */
114 remout
= STDOUT_FILENO
;
116 if (fflag
) { /* Follow "protocol", send data. */
122 if (tflag
) { /* Receive data. */
130 targetshouldbedirectory
= 1;
133 /* Command to be executed on remote system using "rsh". */
134 snprintf(cmd
, sizeof(cmd
),
135 "rcp%s%s%s", iamrecursive
? " -r" : "",
136 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
138 signal(SIGPIPE
, lostconn
);
140 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
141 toremote(targ
, argc
, argv
);
143 tolocal(argc
, argv
); /* Dest is local host. */
144 if (targetshouldbedirectory
)
145 verifydir(argv
[argc
- 1]);
151 toremote(char *targ
, int argc
, char **argv
)
154 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
160 if ((thost
= strchr(argv
[argc
- 1], '@')) != NULL
) {
163 tuser
= argv
[argc
- 1];
166 else if (!okname(tuser
))
169 thost
= argv
[argc
- 1];
172 thost
= unbracket(thost
);
174 for (i
= 0; i
< argc
- 1; i
++) {
175 src
= colon(argv
[i
]);
176 if (src
) { /* remote to remote */
181 host
= strchr(argv
[i
], '@');
184 host
= unbracket(host
);
187 suser
= pwd
->pw_name
;
188 else if (!okname(suser
))
191 "%s%s %s -l %s -n %s %s '%s%s%s:%s'",
192 _PATH_RSH
, eflag
? " -e" : "",
193 host
, suser
, cmd
, src
,
194 tuser
? tuser
: "", tuser
? "@" : "",
197 host
= unbracket(argv
[i
]);
199 "exec %s%s %s -n %s %s '%s%s%s:%s'",
200 _PATH_RSH
, eflag
? " -e" : "",
202 tuser
? tuser
: "", tuser
? "@" : "",
209 } else { /* local to remote */
211 if (asprintf(&bp
, "%s -t %s", cmd
, targ
) == -1)
215 if (do_cmd(host
, tuser
, bp
, &remin
, &remout
) < 0)
228 tolocal(int argc
, char **argv
)
231 char *bp
, *host
, *src
, *suser
;
233 for (i
= 0; i
< argc
- 1; i
++) {
236 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
237 ret
= asprintf(&bp
, "exec %s%s%s %s %s", _PATH_CP
,
238 iamrecursive
? " -PR" : "", pflag
? " -p" : "",
239 argv
[i
], argv
[argc
- 1]);
250 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
252 suser
= pwd
->pw_name
;
257 suser
= pwd
->pw_name
;
258 else if (!okname(suser
))
261 ret
= asprintf(&bp
, "%s -f %s", cmd
, src
);
264 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
270 sink(1, argv
+ argc
- 1);
277 source(int argc
, char **argv
)
284 int fd
, haderr
, indx
, result
;
285 char *last
, *name
, buf
[BUFSIZ
];
287 for (indx
= 0; indx
< argc
; ++indx
) {
289 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
291 if (fstat(fd
, &stb
)) {
292 syserr
: run_err("%s: %s", name
, strerror(errno
));
295 if (S_ISDIR(stb
.st_mode
) && iamrecursive
) {
298 } else if (!S_ISREG(stb
.st_mode
)) {
299 run_err("%s: not a regular file", name
);
302 if ((last
= strrchr(name
, '/')) == NULL
)
308 * Make it compatible with possible future
309 * versions expecting microseconds.
311 snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
314 write(remout
, buf
, strlen(buf
));
319 #define MODEMASK (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
320 snprintf(buf
, sizeof(buf
), "C%04o %lu %s\n",
321 (unsigned int)(stb
.st_mode
& MODEMASK
),
322 (unsigned long)stb
.st_size
,
324 write(remout
, buf
, strlen(buf
));
327 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
332 /* Keep writing after an error so that we stay sync'd up. */
333 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
335 if (i
+ amt
> stb
.st_size
)
336 amt
= stb
.st_size
- i
;
338 result
= read(fd
, bp
->buf
, (size_t)amt
);
340 haderr
= result
>= 0 ? EIO
: errno
;
343 write(remout
, bp
->buf
, amt
);
345 result
= write(remout
, bp
->buf
, (size_t)amt
);
347 haderr
= result
>= 0 ? EIO
: errno
;
350 if (close(fd
) && !haderr
)
353 write(remout
, "", 1);
355 run_err("%s: %s", name
, strerror(haderr
));
361 rsource(char *name
, struct stat
*statp
)
365 char *last
, *vect
[1], path
[MAXPATHLEN
];
367 if (!(dirp
= opendir(name
))) {
368 run_err("%s: %s", name
, strerror(errno
));
371 last
= strrchr(name
, '/');
377 snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
378 (long)statp
->st_mtime
,
379 (long)statp
->st_atime
);
380 write(remout
, path
, strlen(path
));
381 if (response() < 0) {
386 snprintf(path
, sizeof(path
),
388 (unsigned int)(statp
->st_mode
& MODEMASK
), 0, last
);
389 write(remout
, path
, strlen(path
));
390 if (response() < 0) {
394 while ((dp
= readdir(dirp
)) != NULL
) {
397 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
399 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
400 run_err("%s/%s: name too long", name
, dp
->d_name
);
403 snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
408 write(remout
, "E\n", 2);
413 sink(int argc
, char **argv
)
417 struct timeval tv
[2];
418 enum { YES
, NO
, DISPLAYED
} wrerr
;
421 int amt
, count
, exists
, first
, mask
, mode
, ofd
, omode
;
422 int setimes
, targisdir
, wrerrno
= 0;
423 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
];
427 #define SCREWUP(str) { why = str; goto screwup; }
429 setimes
= targisdir
= 0;
434 run_err("ambiguous target");
438 if (targetshouldbedirectory
)
440 write(remout
, "", 1);
441 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
443 for (first
= 1;; first
= 0) {
445 if (read(remin
, cp
, 1) <= 0)
448 SCREWUP("unexpected <newline>");
450 if (read(remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
451 SCREWUP("lost connection");
453 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
456 if (buf
[0] == '\01' || buf
[0] == '\02') {
459 buf
+ 1, strlen(buf
+ 1));
466 write(remout
, "", 1);
477 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
478 if (!cp
|| *cp
++ != ' ')
479 SCREWUP("mtime.sec not delimited");
480 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
481 if (!cp
|| *cp
++ != ' ')
482 SCREWUP("mtime.usec not delimited");
483 atime
.tv_sec
= strtol(cp
, &cp
, 10);
484 if (!cp
|| *cp
++ != ' ')
485 SCREWUP("atime.sec not delimited");
486 atime
.tv_usec
= strtol(cp
, &cp
, 10);
487 if (!cp
|| *cp
++ != '\0')
488 SCREWUP("atime.usec not delimited");
489 write(remout
, "", 1);
492 if (*cp
!= 'C' && *cp
!= 'D') {
494 * Check for the case "rcp remote:foo\* local:bar".
495 * In this case, the line "No match." can be returned
496 * by the shell before the rcp command on the remote is
497 * executed so the ^Aerror_message convention isn't
504 SCREWUP("expected control record");
507 for (++cp
; cp
< buf
+ 5; cp
++) {
508 if (*cp
< '0' || *cp
> '7')
510 mode
= (mode
<< 3) | (*cp
- '0');
513 SCREWUP("mode not delimited");
515 for (size
= 0; isdigit((unsigned char)*cp
);)
516 size
= size
* 10 + (*cp
++ - '0');
518 SCREWUP("size not delimited");
520 static char *namebuf
;
524 need
= strlen(targ
) + strlen(cp
) + 250;
525 if (need
> cursize
) {
526 if (!(namebuf
= malloc(need
)))
527 run_err("%s", strerror(errno
));
529 snprintf(namebuf
, need
, "%s%s%s", targ
,
530 *targ
? "/" : "", cp
);
534 exists
= stat(np
, &stb
) == 0;
536 int mod_flag
= pflag
;
538 if (!S_ISDIR(stb
.st_mode
)) {
545 /* Handle copying from a read-only directory */
547 if (mkdir(np
, mode
| S_IRWXU
) < 0)
554 if (utimes(np
, tv
) < 0)
555 run_err("%s: set times: %s",
556 np
, strerror(errno
));
564 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
565 bad
: run_err("%s: %s", np
, strerror(errno
));
568 write(remout
, "", 1);
569 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
575 for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
580 if((j
= net_read(remin
, cp
, amt
)) != amt
) {
581 run_err("%s", j
? strerror(errno
) :
582 "dropped connection");
587 if (count
== bp
->cnt
) {
588 /* Keep reading so we stay sync'd up. */
590 j
= write(ofd
, bp
->buf
, (size_t)count
);
593 wrerrno
= j
>= 0 ? EIO
: errno
;
600 if (count
!= 0 && wrerr
== NO
&&
601 (j
= write(ofd
, bp
->buf
, (size_t)count
)) != count
) {
603 wrerrno
= j
>= 0 ? EIO
: errno
;
605 if (ftruncate(ofd
, size
)) {
606 run_err("%s: truncate: %s", np
, strerror(errno
));
610 if (exists
|| omode
!= mode
)
611 if (fchmod(ofd
, omode
))
612 run_err("%s: set mode: %s",
613 np
, strerror(errno
));
615 if (!exists
&& omode
!= mode
)
616 if (fchmod(ofd
, omode
& ~mask
))
617 run_err("%s: set mode: %s",
618 np
, strerror(errno
));
622 if (setimes
&& wrerr
== NO
) {
624 if (utimes(np
, tv
) < 0) {
625 run_err("%s: set times: %s",
626 np
, strerror(errno
));
632 run_err("%s: %s", np
, strerror(wrerrno
));
635 write(remout
, "", 1);
642 run_err("protocol error: %s", why
);
649 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
651 if (read(remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
661 case 1: /* error, followed by error msg */
662 case 2: /* fatal error, "" */
664 if (read(remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
667 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
670 write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
682 run_err(const char *fmt
, ...)
688 if (fp
== NULL
&& !(fp
= fdopen(remout
, "w")))
691 fprintf(fp
, "%c", 0x01);
692 fprintf(fp
, "rcp: ");
693 vfprintf(fp
, fmt
, ap
);
706 * This function executes the given command as the specified user on the
707 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
708 * assigns the input and output file descriptors on success.
710 * If it cannot create necessary pipes it exits with error message.
714 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
716 int pin
[2], pout
[2], reserved
[2];
719 * Reserve two descriptors so that the real pipes won't get
720 * descriptors 0 and 1 because that will screw up dup2 below.
724 /* Create a socket pair for communicating with rsh. */
729 if (pipe(pout
) < 0) {
734 /* Free the reserved descriptors. */
738 /* For a child to execute the command on the remote host using rsh. */
752 args
[i
++] = RSH_PROGRAM
;
771 if (remuser
!= NULL
) {
779 execvp(RSH_PROGRAM
, args
);
783 /* Parent. Close the other side, and return the local side. */