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, 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, '5', arg_flag, &usekrb5, "use Kerberos 5 authentication" },
65 { NULL, 'F', arg_flag, &forwardtkt, "forward credentials" },
66 { NULL, 'K', arg_flag, &usebroken, "use BSD authentication" },
67 { NULL, 'P', arg_string, &port, "non-default port", "port" },
68 { NULL, 'p', arg_flag, &pflag, "preserve file permissions" },
69 { NULL, 'r', arg_flag, &iamrecursive, "recursive mode" },
70 { NULL, 'x', arg_flag, &doencrypt, "use encryption" },
71 { NULL, 'z', arg_flag, &noencrypt, "don't encrypt" },
72 { NULL, 'd', arg_flag, &targetshouldbedirectory },
73 { NULL, 'f', arg_flag, &fflag },
74 { NULL, 't', arg_flag, &tflag },
75 { "version", 0, arg_flag, &version_flag },
76 { "help", 0, arg_flag, &help_flag }
83 sizeof(args) / sizeof(args[0]),
85 "file1 file2|file... directory");
90 main(int argc, char **argv)
96 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
102 print_version (NULL);
106 iamremote = (fflag || tflag);
111 if ((pwd = getpwuid(userid = getuid())) == NULL)
112 errx(1, "unknown user %d", (int)userid);
114 remin = STDIN_FILENO; /* XXX */
115 remout = STDOUT_FILENO;
117 if (fflag) { /* Follow "protocol", send data. */
124 if (tflag) { /* Receive data. */
133 targetshouldbedirectory = 1;
136 /* Command to be executed on remote system using "rsh". */
137 snprintf(cmd, sizeof(cmd),
138 "rcp%s%s%s", iamrecursive ? " -r" : "",
139 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
141 signal(SIGPIPE, lostconn);
143 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
144 toremote(targ, argc, argv);
146 tolocal(argc, argv); /* Dest is local host. */
147 if (targetshouldbedirectory)
148 verifydir(argv[argc - 1]);
154 toremote(char *targ, int argc, char **argv)
157 char *bp, *host, *src, *suser, *thost, *tuser;
163 if ((thost = strchr(argv[argc - 1], '@'))) {
166 tuser = argv[argc - 1];
169 else if (!okname(tuser))
172 thost = argv[argc - 1];
176 for (i = 0; i < argc - 1; i++) {
177 src = colon(argv[i]);
178 if (src) { /* remote to remote */
182 host = strchr(argv[i], '@');
187 suser = pwd->pw_name;
188 else if (!okname(suser))
191 "%s %s -l %s -n %s %s '%s%s%s:%s'",
192 _PATH_RSH, host, suser, cmd, src,
193 tuser ? tuser : "", tuser ? "@" : "",
197 "exec %s %s -n %s %s '%s%s%s:%s'",
198 _PATH_RSH, argv[i], cmd, src,
199 tuser ? tuser : "", tuser ? "@" : "",
204 susystem(bp, userid);
206 } else { /* local to remote */
208 asprintf(&bp, "%s -t %s", cmd, targ);
213 if (do_cmd(host, tuser, bp, &remin, &remout) < 0)
227 tolocal(int argc, char **argv)
230 char *bp, *host, *src, *suser;
232 for (i = 0; i < argc - 1; i++) {
233 if (!(src = colon(argv[i]))) { /* Local to local. */
234 asprintf(&bp, "exec %s%s%s %s %s", _PATH_CP,
235 iamrecursive ? " -PR" : "", pflag ? " -p" : "",
236 argv[i], argv[argc - 1]);
239 if (susystem(bp, userid))
247 if ((host = strchr(argv[i], '@')) == NULL) {
249 suser = pwd->pw_name;
254 suser = pwd->pw_name;
255 else if (!okname(suser))
258 asprintf(&bp, "%s -f %s", cmd, src);
261 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
267 sink(1, argv + argc - 1);
275 source(int argc, char **argv)
281 int amt, fd, haderr, indx, result;
282 char *last, *name, buf[BUFSIZ];
284 for (indx = 0; indx < argc; ++indx) {
286 if ((fd = open(name, O_RDONLY, 0)) < 0)
288 if (fstat(fd, &stb)) {
289 syserr: run_err("%s: %s", name, strerror(errno));
292 switch (stb.st_mode & S_IFMT) {
302 run_err("%s: not a regular file", name);
305 if ((last = strrchr(name, '/')) == NULL)
311 * Make it compatible with possible future
312 * versions expecting microseconds.
314 snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
317 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 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, amt);
342 haderr = result >= 0 ? EIO : errno;
345 write(remout, bp->buf, amt);
347 result = write(remout, bp->buf, 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),
389 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
390 write(remout, path, strlen(path));
391 if (response() < 0) {
395 while ((dp = readdir(dirp))) {
398 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
400 if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) {
401 run_err("%s/%s: name too long", name, dp->d_name);
404 snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
409 write(remout, "E\n", 2);
414 sink(int argc, char **argv)
418 struct timeval tv[2];
419 enum { YES, NO, DISPLAYED } wrerr;
422 int amt, count, exists, first, mask, mode, ofd, omode;
423 int setimes, targisdir, wrerrno = 0;
424 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
428 #define SCREWUP(str) { why = str; goto screwup; }
430 setimes = targisdir = 0;
435 run_err("ambiguous target");
439 if (targetshouldbedirectory)
441 write(remout, "", 1);
442 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
444 for (first = 1;; first = 0) {
446 if (read(remin, cp, 1) <= 0)
449 SCREWUP("unexpected <newline>");
451 if (read(remin, &ch, sizeof(ch)) != sizeof(ch))
452 SCREWUP("lost connection");
454 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
457 if (buf[0] == '\01' || buf[0] == '\02') {
460 buf + 1, strlen(buf + 1));
467 write(remout, "", 1);
478 mtime.tv_sec = strtol(cp, &cp, 10);
479 if (!cp || *cp++ != ' ')
480 SCREWUP("mtime.sec not delimited");
481 mtime.tv_usec = strtol(cp, &cp, 10);
482 if (!cp || *cp++ != ' ')
483 SCREWUP("mtime.usec not delimited");
484 atime.tv_sec = strtol(cp, &cp, 10);
485 if (!cp || *cp++ != ' ')
486 SCREWUP("atime.sec not delimited");
487 atime.tv_usec = strtol(cp, &cp, 10);
488 if (!cp || *cp++ != '\0')
489 SCREWUP("atime.usec not delimited");
490 write(remout, "", 1);
493 if (*cp != 'C' && *cp != 'D') {
495 * Check for the case "rcp remote:foo\* local:bar".
496 * In this case, the line "No match." can be returned
497 * by the shell before the rcp command on the remote is
498 * executed so the ^Aerror_message convention isn't
505 SCREWUP("expected control record");
508 for (++cp; cp < buf + 5; cp++) {
509 if (*cp < '0' || *cp > '7')
511 mode = (mode << 3) | (*cp - '0');
514 SCREWUP("mode not delimited");
516 for (size = 0; isdigit((unsigned char)*cp);)
517 size = size * 10 + (*cp++ - '0');
519 SCREWUP("size not delimited");
521 static char *namebuf;
525 need = strlen(targ) + strlen(cp) + 250;
526 if (need > cursize) {
527 if (!(namebuf = malloc(need)))
528 run_err("%s", strerror(errno));
530 snprintf(namebuf, need, "%s%s%s", targ,
531 *targ ? "/" : "", cp);
535 exists = stat(np, &stb) == 0;
537 int mod_flag = pflag;
539 if (!S_ISDIR(stb.st_mode)) {
546 /* Handle copying from a read-only directory */
548 if (mkdir(np, mode | S_IRWXU) < 0)
555 if (utimes(np, tv) < 0)
556 run_err("%s: set times: %s",
557 np, strerror(errno));
565 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
566 bad: run_err("%s: %s", np, strerror(errno));
569 write(remout, "", 1);
570 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
576 for (count = i = 0; i < size; i += BUFSIZ) {
581 if((j = net_read(remin, cp, amt)) != amt) {
582 run_err("%s", j ? strerror(errno) :
583 "dropped connection");
588 if (count == bp->cnt) {
589 /* Keep reading so we stay sync'd up. */
591 j = write(ofd, bp->buf, count);
594 wrerrno = j >= 0 ? EIO : errno;
601 if (count != 0 && wrerr == NO &&
602 (j = write(ofd, bp->buf, count)) != count) {
604 wrerrno = j >= 0 ? EIO : errno;
606 if (ftruncate(ofd, size)) {
607 run_err("%s: truncate: %s", np, strerror(errno));
611 if (exists || omode != mode)
612 if (fchmod(ofd, omode))
613 run_err("%s: set mode: %s",
614 np, strerror(errno));
616 if (!exists && omode != mode)
617 if (fchmod(ofd, omode & ~mask))
618 run_err("%s: set mode: %s",
619 np, strerror(errno));
623 if (setimes && wrerr == NO) {
625 if (utimes(np, tv) < 0) {
626 run_err("%s: set times: %s",
627 np, strerror(errno));
633 run_err("%s: %s", np, strerror(wrerrno));
636 write(remout, "", 1);
643 run_err("protocol error: %s", why);
650 char ch, *cp, resp, rbuf[BUFSIZ];
652 if (read(remin, &resp, sizeof(resp)) != sizeof(resp))
662 case 1: /* error, followed by error msg */
663 case 2: /* fatal error, "" */
665 if (read(remin, &ch, sizeof(ch)) != sizeof(ch))
668 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
671 write(STDERR_FILENO, rbuf, cp - rbuf);
683 run_err(const char *fmt, ...)
689 if (fp == NULL && !(fp = fdopen(remout, "w")))
692 fprintf(fp, "%c", 0x01);
693 fprintf(fp, "rcp: ");
694 vfprintf(fp, fmt, ap);
707 * This function executes the given command as the specified user on the
708 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
709 * assigns the input and output file descriptors on success.
711 * If it cannot create necessary pipes it exits with error message.
715 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
717 int pin[2], pout[2], reserved[2];
720 * Reserve two descriptors so that the real pipes won't get
721 * descriptors 0 and 1 because that will screw up dup2 below.
725 /* Create a socket pair for communicating with rsh. */
730 if (pipe(pout) < 0) {
735 /* Free the reserved descriptors. */
739 /* For a child to execute the command on the remote host using rsh. */
753 args[i++] = RSH_PROGRAM;
768 if (remuser != NULL) {
776 execvp(RSH_PROGRAM, args);
780 /* Parent. Close the other side, and return the local side. */