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
;
48 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
51 void rsource (char *, struct stat
*);
52 void sink (int, char *[]);
53 void source (int, char *[]);
54 void tolocal (int, char *[]);
55 void toremote (char *, int, char *[]);
57 int do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
);
59 static int fflag
, tflag
;
61 static int version_flag
, help_flag
;
63 struct getargs args
[] = {
64 { NULL
, '4', arg_flag
, &usekrb4
, "use Kerberos 4 authentication" },
65 { NULL
, '5', arg_flag
, &usekrb5
, "use Kerberos 5 authentication" },
66 { NULL
, 'F', arg_flag
, &forwardtkt
, "forward credentials" },
67 { NULL
, 'K', arg_flag
, &usebroken
, "use BSD authentication" },
68 { NULL
, 'P', arg_string
, &port
, "non-default port", "port" },
69 { NULL
, 'p', arg_flag
, &pflag
, "preserve file permissions" },
70 { NULL
, 'r', arg_flag
, &iamrecursive
, "recursive mode" },
71 { NULL
, 'x', arg_flag
, &doencrypt
, "use encryption" },
72 { NULL
, 'z', arg_flag
, &noencrypt
, "don't encrypt" },
73 { NULL
, 'd', arg_flag
, &targetshouldbedirectory
},
74 { NULL
, 'f', arg_flag
, &fflag
},
75 { NULL
, 't', arg_flag
, &tflag
},
76 { "version", 0, arg_flag
, &version_flag
},
77 { "help", 0, arg_flag
, &help_flag
}
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. */
125 if (tflag
) { /* Receive data. */
134 targetshouldbedirectory
= 1;
137 /* Command to be executed on remote system using "rsh". */
138 snprintf(cmd
, sizeof(cmd
),
139 "rcp%s%s%s", iamrecursive
? " -r" : "",
140 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
142 signal(SIGPIPE
, lostconn
);
144 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
145 toremote(targ
, argc
, argv
);
147 tolocal(argc
, argv
); /* Dest is local host. */
148 if (targetshouldbedirectory
)
149 verifydir(argv
[argc
- 1]);
155 toremote(char *targ
, int argc
, char **argv
)
158 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
164 if ((thost
= strchr(argv
[argc
- 1], '@'))) {
167 tuser
= argv
[argc
- 1];
170 else if (!okname(tuser
))
173 thost
= argv
[argc
- 1];
177 for (i
= 0; i
< argc
- 1; i
++) {
178 src
= colon(argv
[i
]);
179 if (src
) { /* remote to remote */
183 host
= strchr(argv
[i
], '@');
188 suser
= pwd
->pw_name
;
189 else if (!okname(suser
))
192 "%s %s -l %s -n %s %s '%s%s%s:%s'",
193 _PATH_RSH
, host
, suser
, cmd
, src
,
194 tuser
? tuser
: "", tuser
? "@" : "",
198 "exec %s %s -n %s %s '%s%s%s:%s'",
199 _PATH_RSH
, argv
[i
], cmd
, src
,
200 tuser
? tuser
: "", tuser
? "@" : "",
205 susystem(bp
, userid
);
207 } else { /* local to remote */
209 asprintf(&bp
, "%s -t %s", cmd
, targ
);
214 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
++) {
234 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
235 asprintf(&bp
, "exec %s%s%s %s %s", _PATH_CP
,
236 iamrecursive
? " -PR" : "", pflag
? " -p" : "",
237 argv
[i
], argv
[argc
- 1]);
240 if (susystem(bp
, userid
))
248 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
250 suser
= pwd
->pw_name
;
255 suser
= pwd
->pw_name
;
256 else if (!okname(suser
))
259 asprintf(&bp
, "%s -f %s", cmd
, src
);
262 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
268 sink(1, argv
+ argc
- 1);
276 source(int argc
, char **argv
)
282 int amt
, fd
, haderr
, indx
, result
;
283 char *last
, *name
, buf
[BUFSIZ
];
285 for (indx
= 0; indx
< argc
; ++indx
) {
287 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
289 if (fstat(fd
, &stb
)) {
290 syserr
: run_err("%s: %s", name
, strerror(errno
));
293 switch (stb
.st_mode
& S_IFMT
) {
303 run_err("%s: not a regular file", name
);
306 if ((last
= strrchr(name
, '/')) == NULL
)
312 * Make it compatible with possible future
313 * versions expecting microseconds.
315 snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
318 write(remout
, buf
, strlen(buf
));
322 #define MODEMASK (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
323 snprintf(buf
, sizeof(buf
), "C%04o %lu %s\n",
324 stb
.st_mode
& MODEMASK
,
325 (unsigned long)stb
.st_size
,
327 write(remout
, buf
, strlen(buf
));
330 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
335 /* Keep writing after an error so that we stay sync'd up. */
336 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
338 if (i
+ amt
> stb
.st_size
)
339 amt
= stb
.st_size
- i
;
341 result
= read(fd
, bp
->buf
, amt
);
343 haderr
= result
>= 0 ? EIO
: errno
;
346 write(remout
, bp
->buf
, amt
);
348 result
= write(remout
, bp
->buf
, amt
);
350 haderr
= result
>= 0 ? EIO
: errno
;
353 if (close(fd
) && !haderr
)
356 write(remout
, "", 1);
358 run_err("%s: %s", name
, strerror(haderr
));
364 rsource(char *name
, struct stat
*statp
)
368 char *last
, *vect
[1], path
[MAXPATHLEN
];
370 if (!(dirp
= opendir(name
))) {
371 run_err("%s: %s", name
, strerror(errno
));
374 last
= strrchr(name
, '/');
380 snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
381 (long)statp
->st_mtime
,
382 (long)statp
->st_atime
);
383 write(remout
, path
, strlen(path
));
384 if (response() < 0) {
389 snprintf(path
, sizeof(path
),
390 "D%04o %d %s\n", statp
->st_mode
& MODEMASK
, 0, last
);
391 write(remout
, path
, strlen(path
));
392 if (response() < 0) {
396 while ((dp
= readdir(dirp
))) {
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
, count
);
595 wrerrno
= j
>= 0 ? EIO
: errno
;
602 if (count
!= 0 && wrerr
== NO
&&
603 (j
= write(ofd
, bp
->buf
, 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
;
771 if (remuser
!= NULL
) {
779 execvp(RSH_PROGRAM
, args
);
783 /* Parent. Close the other side, and return the local side. */