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", NULL
},
62 { NULL
, '5', arg_flag
, &usekrb5
, "use Kerberos 5 authentication", NULL
},
63 { NULL
, 'F', arg_flag
, &forwardtkt
, "forward credentials", NULL
},
64 { NULL
, 'K', arg_flag
, &usebroken
, "use BSD authentication",
66 { NULL
, 'P', arg_string
, &port
, "non-default port", "port" },
67 { NULL
, 'p', arg_flag
, &pflag
, "preserve file permissions",
69 { NULL
, 'r', arg_flag
, &iamrecursive
, "recursive mode", NULL
},
70 { NULL
, 'x', arg_flag
, &doencrypt
, "use encryption", NULL
},
71 { NULL
, 'z', arg_flag
, &noencrypt
, "don't encrypt", NULL
},
72 { NULL
, 'd', arg_flag
, &targetshouldbedirectory
, NULL
, NULL
},
73 { NULL
, 'e', arg_flag
, &eflag
, "passed to rsh", NULL
},
74 { NULL
, 'f', arg_flag
, &fflag
, NULL
, NULL
},
75 { NULL
, 't', arg_flag
, &tflag
, NULL
, NULL
},
76 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
77 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
84 sizeof(args
) / sizeof(args
[0]),
86 "file1 file2|file... directory");
91 main(int argc
, char **argv
)
97 if (getarg (args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
,
103 print_version (NULL
);
107 iamremote
= (fflag
|| tflag
);
112 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
113 errx(1, "unknown user %d", (int)userid
);
115 remin
= STDIN_FILENO
; /* XXX */
116 remout
= STDOUT_FILENO
;
118 if (fflag
) { /* Follow "protocol", send data. */
124 if (tflag
) { /* Receive data. */
132 targetshouldbedirectory
= 1;
135 /* Command to be executed on remote system using "rsh". */
136 snprintf(cmd
, sizeof(cmd
),
137 "rcp%s%s%s", iamrecursive
? " -r" : "",
138 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
140 signal(SIGPIPE
, lostconn
);
142 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
143 toremote(targ
, argc
, argv
);
145 tolocal(argc
, argv
); /* Dest is local host. */
146 if (targetshouldbedirectory
)
147 verifydir(argv
[argc
- 1]);
153 toremote(char *targ
, int argc
, char **argv
)
156 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
162 if ((thost
= strchr(argv
[argc
- 1], '@')) != NULL
) {
165 tuser
= argv
[argc
- 1];
168 else if (!okname(tuser
))
171 thost
= argv
[argc
- 1];
174 thost
= unbracket(thost
);
176 for (i
= 0; i
< argc
- 1; i
++) {
177 src
= colon(argv
[i
]);
178 if (src
) { /* remote to remote */
183 host
= strchr(argv
[i
], '@');
186 host
= unbracket(host
);
189 suser
= pwd
->pw_name
;
190 else if (!okname(suser
))
193 "%s%s %s -l %s -n %s %s '%s%s%s:%s'",
194 _PATH_RSH
, eflag
? " -e" : "",
195 host
, suser
, cmd
, src
,
196 tuser
? tuser
: "", tuser
? "@" : "",
199 host
= unbracket(argv
[i
]);
201 "exec %s%s %s -n %s %s '%s%s%s:%s'",
202 _PATH_RSH
, eflag
? " -e" : "",
204 tuser
? tuser
: "", tuser
? "@" : "",
211 } else { /* local to remote */
213 if (asprintf(&bp
, "%s -t %s", cmd
, targ
) == -1)
217 if (do_cmd(host
, tuser
, bp
, &remin
, &remout
) < 0)
230 tolocal(int argc
, char **argv
)
233 char *bp
, *host
, *src
, *suser
;
235 for (i
= 0; i
< argc
- 1; i
++) {
238 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
239 ret
= asprintf(&bp
, "exec %s%s%s %s %s", _PATH_CP
,
240 iamrecursive
? " -PR" : "", pflag
? " -p" : "",
241 argv
[i
], argv
[argc
- 1]);
252 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
254 suser
= pwd
->pw_name
;
259 suser
= pwd
->pw_name
;
260 else if (!okname(suser
))
263 ret
= asprintf(&bp
, "%s -f %s", cmd
, src
);
266 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
272 sink(1, argv
+ argc
- 1);
279 source(int argc
, char **argv
)
286 int fd
, haderr
, indx
, result
;
287 char *last
, *name
, buf
[BUFSIZ
];
289 for (indx
= 0; indx
< argc
; ++indx
) {
291 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
293 if (fstat(fd
, &stb
)) {
294 syserr
: run_err("%s: %s", name
, strerror(errno
));
297 if (S_ISDIR(stb
.st_mode
) && iamrecursive
) {
300 } else if (!S_ISREG(stb
.st_mode
)) {
301 run_err("%s: not a regular file", name
);
304 if ((last
= strrchr(name
, '/')) == NULL
)
310 * Make it compatible with possible future
311 * versions expecting microseconds.
313 snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
316 write(remout
, buf
, strlen(buf
));
321 #define MODEMASK (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
322 snprintf(buf
, sizeof(buf
), "C%04o %lu %s\n",
323 (unsigned int)(stb
.st_mode
& MODEMASK
),
324 (unsigned long)stb
.st_size
,
326 write(remout
, buf
, strlen(buf
));
329 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
334 /* Keep writing after an error so that we stay sync'd up. */
335 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
337 if (i
+ amt
> stb
.st_size
)
338 amt
= stb
.st_size
- i
;
340 result
= read(fd
, bp
->buf
, (size_t)amt
);
342 haderr
= result
>= 0 ? EIO
: errno
;
345 write(remout
, bp
->buf
, amt
);
347 result
= write(remout
, bp
->buf
, (size_t)amt
);
349 haderr
= result
>= 0 ? EIO
: errno
;
352 if (close(fd
) && !haderr
)
355 write(remout
, "", 1);
357 run_err("%s: %s", name
, strerror(haderr
));
363 rsource(char *name
, struct stat
*statp
)
367 char *last
, *vect
[1], path
[MAXPATHLEN
];
369 if (!(dirp
= opendir(name
))) {
370 run_err("%s: %s", name
, strerror(errno
));
373 last
= strrchr(name
, '/');
379 snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
380 (long)statp
->st_mtime
,
381 (long)statp
->st_atime
);
382 write(remout
, path
, strlen(path
));
383 if (response() < 0) {
388 snprintf(path
, sizeof(path
),
390 (unsigned int)(statp
->st_mode
& MODEMASK
), 0, last
);
391 write(remout
, path
, strlen(path
));
392 if (response() < 0) {
396 while ((dp
= readdir(dirp
)) != NULL
) {
399 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
401 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
402 run_err("%s/%s: name too long", name
, dp
->d_name
);
405 snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
410 write(remout
, "E\n", 2);
415 sink(int argc
, char **argv
)
419 struct timeval tv
[2];
420 enum { YES
, NO
, DISPLAYED
} wrerr
;
423 int amt
, count
, exists
, first
, mask
, mode
, ofd
, omode
;
424 int setimes
, targisdir
, wrerrno
= 0;
425 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
];
429 #define SCREWUP(str) { why = str; goto screwup; }
431 setimes
= targisdir
= 0;
436 run_err("ambiguous target");
440 if (targetshouldbedirectory
)
442 write(remout
, "", 1);
443 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
445 for (first
= 1;; first
= 0) {
447 if (read(remin
, cp
, 1) <= 0)
450 SCREWUP("unexpected <newline>");
452 if (read(remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
453 SCREWUP("lost connection");
455 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
458 if (buf
[0] == '\01' || buf
[0] == '\02') {
461 buf
+ 1, strlen(buf
+ 1));
468 write(remout
, "", 1);
479 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
480 if (!cp
|| *cp
++ != ' ')
481 SCREWUP("mtime.sec not delimited");
482 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
483 if (!cp
|| *cp
++ != ' ')
484 SCREWUP("mtime.usec not delimited");
485 atime
.tv_sec
= strtol(cp
, &cp
, 10);
486 if (!cp
|| *cp
++ != ' ')
487 SCREWUP("atime.sec not delimited");
488 atime
.tv_usec
= strtol(cp
, &cp
, 10);
489 if (!cp
|| *cp
++ != '\0')
490 SCREWUP("atime.usec not delimited");
491 write(remout
, "", 1);
494 if (*cp
!= 'C' && *cp
!= 'D') {
496 * Check for the case "rcp remote:foo\* local:bar".
497 * In this case, the line "No match." can be returned
498 * by the shell before the rcp command on the remote is
499 * executed so the ^Aerror_message convention isn't
506 SCREWUP("expected control record");
509 for (++cp
; cp
< buf
+ 5; cp
++) {
510 if (*cp
< '0' || *cp
> '7')
512 mode
= (mode
<< 3) | (*cp
- '0');
515 SCREWUP("mode not delimited");
517 for (size
= 0; isdigit((unsigned char)*cp
);)
518 size
= size
* 10 + (*cp
++ - '0');
520 SCREWUP("size not delimited");
522 static char *namebuf
;
526 need
= strlen(targ
) + strlen(cp
) + 250;
527 if (need
> cursize
) {
528 if (!(namebuf
= malloc(need
)))
529 run_err("%s", strerror(errno
));
531 snprintf(namebuf
, need
, "%s%s%s", targ
,
532 *targ
? "/" : "", cp
);
536 exists
= stat(np
, &stb
) == 0;
538 int mod_flag
= pflag
;
540 if (!S_ISDIR(stb
.st_mode
)) {
547 /* Handle copying from a read-only directory */
549 if (mkdir(np
, mode
| S_IRWXU
) < 0)
556 if (utimes(np
, tv
) < 0)
557 run_err("%s: set times: %s",
558 np
, strerror(errno
));
566 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
567 bad
: run_err("%s: %s", np
, strerror(errno
));
570 write(remout
, "", 1);
571 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
577 for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
582 if((j
= net_read(remin
, cp
, amt
)) != amt
) {
583 run_err("%s", j
? strerror(errno
) :
584 "dropped connection");
589 if (count
== bp
->cnt
) {
590 /* Keep reading so we stay sync'd up. */
592 j
= write(ofd
, bp
->buf
, (size_t)count
);
595 wrerrno
= j
>= 0 ? EIO
: errno
;
602 if (count
!= 0 && wrerr
== NO
&&
603 (j
= write(ofd
, bp
->buf
, (size_t)count
)) != count
) {
605 wrerrno
= j
>= 0 ? EIO
: errno
;
607 if (ftruncate(ofd
, size
)) {
608 run_err("%s: truncate: %s", np
, strerror(errno
));
612 if (exists
|| omode
!= mode
)
613 if (fchmod(ofd
, omode
))
614 run_err("%s: set mode: %s",
615 np
, strerror(errno
));
617 if (!exists
&& omode
!= mode
)
618 if (fchmod(ofd
, omode
& ~mask
))
619 run_err("%s: set mode: %s",
620 np
, strerror(errno
));
624 if (setimes
&& wrerr
== NO
) {
626 if (utimes(np
, tv
) < 0) {
627 run_err("%s: set times: %s",
628 np
, strerror(errno
));
634 run_err("%s: %s", np
, strerror(wrerrno
));
637 write(remout
, "", 1);
644 run_err("protocol error: %s", why
);
651 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
653 if (read(remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
663 case 1: /* error, followed by error msg */
664 case 2: /* fatal error, "" */
666 if (read(remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
669 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
672 write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
684 run_err(const char *fmt
, ...)
690 if (fp
== NULL
&& !(fp
= fdopen(remout
, "w")))
693 fprintf(fp
, "%c", 0x01);
694 fprintf(fp
, "rcp: ");
695 vfprintf(fp
, fmt
, ap
);
708 * This function executes the given command as the specified user on the
709 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
710 * assigns the input and output file descriptors on success.
712 * If it cannot create necessary pipes it exits with error message.
716 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
718 int pin
[2], pout
[2], reserved
[2];
721 * Reserve two descriptors so that the real pipes won't get
722 * descriptors 0 and 1 because that will screw up dup2 below.
726 /* Create a socket pair for communicating with rsh. */
731 if (pipe(pout
) < 0) {
736 /* Free the reserved descriptors. */
740 /* For a child to execute the command on the remote host using rsh. */
754 args
[i
++] = RSH_PROGRAM
;
773 if (remuser
!= NULL
) {
781 execvp(RSH_PROGRAM
, args
);
785 /* Parent. Close the other side, and return the local side. */