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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #define RSH_PROGRAM "rsh"
41 int errs
, remin
, remout
;
42 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
43 int doencrypt
, noencrypt
;
44 int usebroken
, usekrb4
, usekrb5
, forwardtkt
;
49 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
52 void rsource (char *, struct stat
*);
53 void sink (int, char *[]);
54 void source (int, char *[]);
55 void tolocal (int, char *[]);
56 void toremote (char *, int, char *[]);
58 int do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
);
60 static int fflag
, tflag
;
62 static int version_flag
, help_flag
;
64 struct getargs args
[] = {
65 { NULL
, '4', arg_flag
, &usekrb4
, "use Kerberos 4 authentication" },
66 { NULL
, '5', arg_flag
, &usekrb5
, "use Kerberos 5 authentication" },
67 { NULL
, 'F', arg_flag
, &forwardtkt
, "forward credentials" },
68 { NULL
, 'K', arg_flag
, &usebroken
, "use BSD authentication" },
69 { NULL
, 'P', arg_string
, &port
, "non-default port", "port" },
70 { NULL
, 'p', arg_flag
, &pflag
, "preserve file permissions" },
71 { NULL
, 'r', arg_flag
, &iamrecursive
, "recursive mode" },
72 { NULL
, 'x', arg_flag
, &doencrypt
, "use encryption" },
73 { NULL
, 'z', arg_flag
, &noencrypt
, "don't encrypt" },
74 { NULL
, 'd', arg_flag
, &targetshouldbedirectory
},
75 { NULL
, 'e', arg_flag
, &eflag
, "passed to rsh" },
76 { NULL
, 'f', arg_flag
, &fflag
},
77 { NULL
, 't', arg_flag
, &tflag
},
78 { "version", 0, arg_flag
, &version_flag
},
79 { "help", 0, arg_flag
, &help_flag
}
86 sizeof(args
) / sizeof(args
[0]),
88 "file1 file2|file... directory");
93 main(int argc
, char **argv
)
99 if (getarg (args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
,
105 print_version (NULL
);
109 iamremote
= (fflag
|| tflag
);
114 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
115 errx(1, "unknown user %d", (int)userid
);
117 remin
= STDIN_FILENO
; /* XXX */
118 remout
= STDOUT_FILENO
;
120 if (fflag
) { /* Follow "protocol", send data. */
122 if (setuid(userid
) < 0)
123 errx(1, "setuid failed");
128 if (tflag
) { /* Receive data. */
129 if (setuid(userid
) < 0)
130 errx(1, "setuid failed");
138 targetshouldbedirectory
= 1;
141 /* Command to be executed on remote system using "rsh". */
142 snprintf(cmd
, sizeof(cmd
),
143 "rcp%s%s%s", iamrecursive
? " -r" : "",
144 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
146 signal(SIGPIPE
, lostconn
);
148 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
149 toremote(targ
, argc
, argv
);
151 tolocal(argc
, argv
); /* Dest is local host. */
152 if (targetshouldbedirectory
)
153 verifydir(argv
[argc
- 1]);
159 toremote(char *targ
, int argc
, char **argv
)
162 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
168 if ((thost
= strchr(argv
[argc
- 1], '@'))) {
171 tuser
= argv
[argc
- 1];
174 else if (!okname(tuser
))
177 thost
= argv
[argc
- 1];
181 for (i
= 0; i
< argc
- 1; i
++) {
182 src
= colon(argv
[i
]);
183 if (src
) { /* remote to remote */
188 host
= strchr(argv
[i
], '@');
193 suser
= pwd
->pw_name
;
194 else if (!okname(suser
))
197 "%s%s %s -l %s -n %s %s '%s%s%s:%s'",
198 _PATH_RSH
, eflag
? " -e" : "",
199 host
, suser
, cmd
, src
,
200 tuser
? tuser
: "", tuser
? "@" : "",
204 "exec %s%s %s -n %s %s '%s%s%s:%s'",
205 _PATH_RSH
, eflag
? " -e" : "",
207 tuser
? tuser
: "", tuser
? "@" : "",
212 susystem(bp
, userid
);
214 } else { /* local to remote */
216 if (asprintf(&bp
, "%s -t %s", cmd
, targ
) == -1)
220 if (do_cmd(host
, tuser
, bp
, &remin
, &remout
) < 0)
226 if (setuid(userid
) < 0)
227 errx(1, "setuid failed");
235 tolocal(int argc
, char **argv
)
238 char *bp
, *host
, *src
, *suser
;
240 for (i
= 0; i
< argc
- 1; i
++) {
243 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
244 ret
= asprintf(&bp
, "exec %s%s%s %s %s", _PATH_CP
,
245 iamrecursive
? " -PR" : "", pflag
? " -p" : "",
246 argv
[i
], argv
[argc
- 1]);
249 if (susystem(bp
, userid
))
257 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
259 suser
= pwd
->pw_name
;
264 suser
= pwd
->pw_name
;
265 else if (!okname(suser
))
268 ret
= asprintf(&bp
, "%s -f %s", cmd
, src
);
271 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
277 sink(1, argv
+ argc
- 1);
286 source(int argc
, char **argv
)
292 int amt
, fd
, haderr
, indx
, result
;
293 char *last
, *name
, buf
[BUFSIZ
];
295 for (indx
= 0; indx
< argc
; ++indx
) {
297 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
299 if (fstat(fd
, &stb
)) {
300 syserr
: run_err("%s: %s", name
, strerror(errno
));
303 switch (stb
.st_mode
& S_IFMT
) {
313 run_err("%s: not a regular file", name
);
316 if ((last
= strrchr(name
, '/')) == NULL
)
322 * Make it compatible with possible future
323 * versions expecting microseconds.
325 snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
328 write(remout
, buf
, strlen(buf
));
333 #define MODEMASK (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
334 snprintf(buf
, sizeof(buf
), "C%04o %lu %s\n",
335 stb
.st_mode
& MODEMASK
,
336 (unsigned long)stb
.st_size
,
338 write(remout
, buf
, strlen(buf
));
341 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
346 /* Keep writing after an error so that we stay sync'd up. */
347 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
349 if (i
+ amt
> stb
.st_size
)
350 amt
= stb
.st_size
- i
;
352 result
= read(fd
, bp
->buf
, amt
);
354 haderr
= result
>= 0 ? EIO
: errno
;
357 write(remout
, bp
->buf
, amt
);
359 result
= write(remout
, bp
->buf
, amt
);
361 haderr
= result
>= 0 ? EIO
: errno
;
364 if (close(fd
) && !haderr
)
367 write(remout
, "", 1);
369 run_err("%s: %s", name
, strerror(haderr
));
375 rsource(char *name
, struct stat
*statp
)
379 char *last
, *vect
[1], path
[MAXPATHLEN
];
381 if (!(dirp
= opendir(name
))) {
382 run_err("%s: %s", name
, strerror(errno
));
385 last
= strrchr(name
, '/');
391 snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
392 (long)statp
->st_mtime
,
393 (long)statp
->st_atime
);
394 write(remout
, path
, strlen(path
));
395 if (response() < 0) {
400 snprintf(path
, sizeof(path
),
401 "D%04o %d %s\n", statp
->st_mode
& MODEMASK
, 0, last
);
402 write(remout
, path
, strlen(path
));
403 if (response() < 0) {
407 while ((dp
= readdir(dirp
))) {
410 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
412 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
413 run_err("%s/%s: name too long", name
, dp
->d_name
);
416 snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
421 write(remout
, "E\n", 2);
426 sink(int argc
, char **argv
)
430 struct timeval tv
[2];
431 enum { YES
, NO
, DISPLAYED
} wrerr
;
434 int amt
, count
, exists
, first
, mask
, mode
, ofd
, omode
;
435 int setimes
, targisdir
, wrerrno
= 0;
436 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
];
440 #define SCREWUP(str) { why = str; goto screwup; }
442 setimes
= targisdir
= 0;
447 run_err("ambiguous target");
451 if (targetshouldbedirectory
)
453 write(remout
, "", 1);
454 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
456 for (first
= 1;; first
= 0) {
458 if (read(remin
, cp
, 1) <= 0)
461 SCREWUP("unexpected <newline>");
463 if (read(remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
464 SCREWUP("lost connection");
466 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
469 if (buf
[0] == '\01' || buf
[0] == '\02') {
472 buf
+ 1, strlen(buf
+ 1));
479 write(remout
, "", 1);
490 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
491 if (!cp
|| *cp
++ != ' ')
492 SCREWUP("mtime.sec not delimited");
493 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
494 if (!cp
|| *cp
++ != ' ')
495 SCREWUP("mtime.usec not delimited");
496 atime
.tv_sec
= strtol(cp
, &cp
, 10);
497 if (!cp
|| *cp
++ != ' ')
498 SCREWUP("atime.sec not delimited");
499 atime
.tv_usec
= strtol(cp
, &cp
, 10);
500 if (!cp
|| *cp
++ != '\0')
501 SCREWUP("atime.usec not delimited");
502 write(remout
, "", 1);
505 if (*cp
!= 'C' && *cp
!= 'D') {
507 * Check for the case "rcp remote:foo\* local:bar".
508 * In this case, the line "No match." can be returned
509 * by the shell before the rcp command on the remote is
510 * executed so the ^Aerror_message convention isn't
517 SCREWUP("expected control record");
520 for (++cp
; cp
< buf
+ 5; cp
++) {
521 if (*cp
< '0' || *cp
> '7')
523 mode
= (mode
<< 3) | (*cp
- '0');
526 SCREWUP("mode not delimited");
528 for (size
= 0; isdigit((unsigned char)*cp
);)
529 size
= size
* 10 + (*cp
++ - '0');
531 SCREWUP("size not delimited");
533 static char *namebuf
;
537 need
= strlen(targ
) + strlen(cp
) + 250;
538 if (need
> cursize
) {
539 if (!(namebuf
= malloc(need
)))
540 run_err("%s", strerror(errno
));
542 snprintf(namebuf
, need
, "%s%s%s", targ
,
543 *targ
? "/" : "", cp
);
547 exists
= stat(np
, &stb
) == 0;
549 int mod_flag
= pflag
;
551 if (!S_ISDIR(stb
.st_mode
)) {
558 /* Handle copying from a read-only directory */
560 if (mkdir(np
, mode
| S_IRWXU
) < 0)
567 if (utimes(np
, tv
) < 0)
568 run_err("%s: set times: %s",
569 np
, strerror(errno
));
577 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
578 bad
: run_err("%s: %s", np
, strerror(errno
));
581 write(remout
, "", 1);
582 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
588 for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
593 if((j
= net_read(remin
, cp
, amt
)) != amt
) {
594 run_err("%s", j
? strerror(errno
) :
595 "dropped connection");
600 if (count
== bp
->cnt
) {
601 /* Keep reading so we stay sync'd up. */
603 j
= write(ofd
, bp
->buf
, count
);
606 wrerrno
= j
>= 0 ? EIO
: errno
;
613 if (count
!= 0 && wrerr
== NO
&&
614 (j
= write(ofd
, bp
->buf
, count
)) != count
) {
616 wrerrno
= j
>= 0 ? EIO
: errno
;
618 if (ftruncate(ofd
, size
)) {
619 run_err("%s: truncate: %s", np
, strerror(errno
));
623 if (exists
|| omode
!= mode
)
624 if (fchmod(ofd
, omode
))
625 run_err("%s: set mode: %s",
626 np
, strerror(errno
));
628 if (!exists
&& omode
!= mode
)
629 if (fchmod(ofd
, omode
& ~mask
))
630 run_err("%s: set mode: %s",
631 np
, strerror(errno
));
635 if (setimes
&& wrerr
== NO
) {
637 if (utimes(np
, tv
) < 0) {
638 run_err("%s: set times: %s",
639 np
, strerror(errno
));
645 run_err("%s: %s", np
, strerror(wrerrno
));
648 write(remout
, "", 1);
655 run_err("protocol error: %s", why
);
662 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
664 if (read(remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
674 case 1: /* error, followed by error msg */
675 case 2: /* fatal error, "" */
677 if (read(remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
680 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
683 write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
695 run_err(const char *fmt
, ...)
701 if (fp
== NULL
&& !(fp
= fdopen(remout
, "w")))
704 fprintf(fp
, "%c", 0x01);
705 fprintf(fp
, "rcp: ");
706 vfprintf(fp
, fmt
, ap
);
719 * This function executes the given command as the specified user on the
720 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
721 * assigns the input and output file descriptors on success.
723 * If it cannot create necessary pipes it exits with error message.
727 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
729 int pin
[2], pout
[2], reserved
[2];
732 * Reserve two descriptors so that the real pipes won't get
733 * descriptors 0 and 1 because that will screw up dup2 below.
737 /* Create a socket pair for communicating with rsh. */
742 if (pipe(pout
) < 0) {
747 /* Free the reserved descriptors. */
751 /* For a child to execute the command on the remote host using rsh. */
765 args
[i
++] = RSH_PROGRAM
;
784 if (remuser
!= NULL
) {
792 execvp(RSH_PROGRAM
, args
);
796 /* Parent. Close the other side, and return the local side. */