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
36 RCSID("$Id: rcp.c,v 1.52.2.1 2000/06/23 02:35:16 assar Exp $");
39 static char dst_realm_buf[REALM_SZ];
40 static char *dest_realm = NULL;
41 static int use_kerberos = 1;
43 static int doencrypt = 0;
44 #define OPTIONS "dfKk:prtxl:"
46 static char *user_name = NULL; /* Given as -l option. */
49 static struct passwd *pwd;
52 static int pflag, iamremote, iamrecursive, targetshouldbedirectory;
55 static char **argv_copy;
58 static char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
60 void rsource(char *name, struct stat *statp);
62 #define SERVICE_NAME "rcmd"
66 struct sockaddr_in foreign_addr, local_addr;
67 Key_schedule schedule;
73 send_auth(char *h, char *r)
75 int lslen, fslen, status;
78 lslen = sizeof(struct sockaddr_in);
79 if (getsockname(rem, (struct sockaddr *)&local_addr, &lslen) < 0)
80 err(1, "getsockname");
81 fslen = sizeof(struct sockaddr_in);
82 if (getpeername(rem, (struct sockaddr *)&foreign_addr, &fslen) < 0)
83 err(1, "getpeername");
84 if ((r == NULL) || (*r == '\0'))
85 r = krb_realmofhost(h);
86 opts = KOPT_DO_MUTUAL;
87 if ((status = krb_sendauth(opts, rem, &ticket, SERVICE_NAME, h, r,
88 (unsigned long)getpid(), &msg_data, &cred,
89 schedule, &local_addr,
90 &foreign_addr, "KCMDV0.1")) != KSUCCESS)
91 errx(1, "krb_sendauth failure: %s", krb_get_err_text(status));
97 int lslen, fslen, status;
99 char inst[INST_SZ], v[9];
101 lslen = sizeof(struct sockaddr_in);
102 if (getsockname(rem, (struct sockaddr *)&local_addr, &lslen) < 0)
103 err(1, "getsockname");
104 fslen = sizeof(struct sockaddr_in);
105 if(getpeername(rem, (struct sockaddr *)&foreign_addr, &fslen) < 0)
106 err(1, "getperrname");
107 k_getsockinst(rem, inst, sizeof(inst));
108 opts = KOPT_DO_MUTUAL;
109 if ((status = krb_recvauth(opts, rem, &ticket, SERVICE_NAME, inst,
110 &foreign_addr, &local_addr,
111 &kdata, "", schedule, v)) != KSUCCESS)
112 errx(1, "krb_recvauth failure: %s", krb_get_err_text(status));
116 des_read(int fd, char *buf, int len)
119 return(des_enc_read(fd, buf, len, schedule,
120 (iamremote? &kdata.session : &cred.session)));
122 return(read(fd, buf, len));
126 des_write(int fd, char *buf, int len)
129 return(des_enc_write(fd, buf, len, schedule,
130 (iamremote? &kdata.session : &cred.session)));
132 return(write(fd, buf, len));
135 static void run_err(const char *fmt, ...)
137 __attribute__ ((format (printf, 1, 2)))
143 run_err(const char *fmt, ...)
150 #define RCPERR "\001rcp: "
151 strlcpy (errbuf, RCPERR, sizeof(errbuf));
152 vsnprintf (errbuf + strlen(errbuf),
153 sizeof(errbuf) - strlen(errbuf),
155 strlcat (errbuf, "\n", sizeof(errbuf));
156 des_write (rem, errbuf, strlen(errbuf));
167 if (!stat(cp, &stb)) {
168 if (S_ISDIR(stb.st_mode))
172 run_err("%s: %s", cp, strerror(errno));
176 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
179 allocbuf(BUF *bp, int fd, int blksize)
184 if (fstat(fd, &stb) < 0) {
185 run_err("fstat: %s", strerror(errno));
188 #ifdef HAVE_ST_BLKSIZE
189 size = ROUNDUP(stb.st_blksize, blksize);
198 bp->buf = malloc(size);
200 bp->buf = realloc(bp->buf, size);
201 if (bp->buf == NULL) {
203 run_err("%s", strerror(errno));
213 fprintf(stderr, "%s\n\t%s\n",
214 "usage: rcp [-Kpx] [-k realm] f1 f2",
215 "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
222 char *rstar_no_warn = getenv("RSTAR_NO_WARN");
223 if (rstar_no_warn == 0)
225 if (strncmp(rstar_no_warn, "yes", 3) != 0)
226 warnx("%s, using standard rcp", s);
233 warnx("lost connection");
240 char ch, *cp, resp, rbuf[BUFSIZ];
242 if (des_read(rem, &resp, sizeof(resp)) != sizeof(resp))
252 case 1: /* error, followed by error msg */
253 case 2: /* fatal error, "" */
255 if (des_read(rem, &ch, sizeof(ch)) != sizeof(ch))
258 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
261 write(STDERR_FILENO, rbuf, cp - rbuf);
271 source(int argc, char **argv)
277 int amt, fd, haderr, indx, result;
278 char *last, *name, buf[BUFSIZ];
280 for (indx = 0; indx < argc; ++indx) {
282 if ((fd = open(name, O_RDONLY, 0)) < 0)
284 if (fstat(fd, &stb)) {
285 syserr: run_err("%s: %s", name, strerror(errno));
288 switch (stb.st_mode & S_IFMT) {
298 run_err("%s: not a regular file", name);
301 if ((last = strrchr(name, '/')) == NULL)
307 * Make it compatible with possible future
308 * versions expecting microseconds.
310 snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
311 (long)stb.st_mtime, (long)stb.st_atime);
312 des_write(rem, buf, strlen(buf));
316 snprintf(buf, sizeof(buf), "C%04o %ld %s\n",
317 (int)stb.st_mode & MODEMASK, (long) stb.st_size, last);
318 des_write(rem, buf, strlen(buf));
321 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
326 /* Keep writing after an error so that we stay sync'd up. */
327 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
329 if (i + amt > stb.st_size)
330 amt = stb.st_size - i;
332 result = read(fd, bp->buf, amt);
334 haderr = result >= 0 ? EIO : errno;
337 des_write(rem, bp->buf, amt);
339 result = des_write(rem, bp->buf, amt);
341 haderr = result >= 0 ? EIO : errno;
344 if (close(fd) && !haderr)
347 des_write(rem, "", 1);
349 run_err("%s: %s", name, strerror(haderr));
355 rsource(char *name, struct stat *statp)
359 char *last, *vect[1], path[MaxPathLen];
362 if (!(dirp = opendir(name))) {
363 run_err("%s: %s", name, strerror(errno));
366 for (p = name + strlen(name) - 1; p >= name && *p == '/'; --p)
369 last = strrchr(name, '/');
375 snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
376 (long)statp->st_mtime, (long)statp->st_atime);
377 des_write(rem, path, strlen(path));
378 if (response() < 0) {
383 snprintf(path, sizeof(path),
384 "D%04o %d %s\n", (int)statp->st_mode & MODEMASK, 0, last);
385 des_write(rem, path, strlen(path));
386 if (response() < 0) {
390 while ((dp = readdir(dirp))) {
393 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
395 if (strlen(name) + 1 + strlen(dp->d_name) >= MaxPathLen - 1) {
396 run_err("%s/%s: name too long", name, dp->d_name);
399 if (snprintf(path, sizeof(path),
400 "%s/%s", name, dp->d_name) >= sizeof(path)) {
401 run_err("%s/%s: name too long", name, dp->d_name);
408 des_write(rem, "E\n", 2);
413 kerberos(char **host, char *bp, char *locuser, char *user)
418 paranoid_setuid(getuid());
421 if (dest_realm == NULL)
422 dest_realm = krb_realmofhost(*host);
425 rem = krcmd(host, port, user, bp, 0, dest_realm);
431 NULL, /* locuser not used */
438 (CREDENTIALS *) NULL, /* credentials not used */
439 0, /* key schedule not used */
440 (MSG_DAT *) NULL, /* MSG_DAT not used */
441 (struct sockaddr_in *) NULL, /* local addr not used */
442 (struct sockaddr_in *) NULL, /* foreign addr not used */
444 if (err > KSUCCESS && err < MAX_KRB_ERRORS) {
445 warnx("kcmd: %s", krb_get_err_text(err));
453 if (errno == ECONNREFUSED)
454 oldw("remote host doesn't support Kerberos");
455 else if (errno == ENOENT)
456 oldw("can't provide Kerberos authentication data");
457 execv(_PATH_RCP, argv_copy);
462 "the -x option requires Kerberos authentication");
463 if (geteuid() != 0) {
464 errx(1, "not installed setuid root, "
465 "only root may use non kerberized rcp");
467 rem = rcmd(host, port, locuser, user, bp, 0);
473 toremote(char *targ, int argc, char **argv)
479 char *bp, *host, *src, *suser, *thost, *tuser;
485 if ((thost = strchr(argv[argc - 1], '@'))) {
488 tuser = argv[argc - 1];
491 else if (!okname(tuser))
494 thost = argv[argc - 1];
498 for (i = 0; i < argc - 1; i++) {
499 src = colon(argv[i]);
500 if (src) { /* remote to remote */
504 host = strchr(argv[i], '@');
509 suser = pwd->pw_name;
510 else if (!okname(suser))
512 asprintf(&bp, "%s %s -l %s -n %s %s '%s%s%s:%s'",
513 _PATH_RSH, host, suser, cmd, src,
514 tuser ? tuser : "", tuser ? "@" : "",
517 asprintf(&bp, "exec %s %s -n %s %s '%s%s%s:%s'",
518 _PATH_RSH, argv[i], cmd, src,
519 tuser ? tuser : "", tuser ? "@" : "",
522 errx(1, "out of memory");
523 susystem(bp, userid);
525 } else { /* local to remote */
527 len = strlen(targ) + CMDNEEDS + 20;
528 if (!(bp = malloc(len)))
530 snprintf(bp, len, "%s -t %s", cmd, targ);
533 rem = kerberos(&host, bp,
539 tuser ? tuser : pwd->pw_name);
541 rem = rcmd(&host, port,
547 tuser ? tuser : pwd->pw_name,
551 #if defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
552 tos = IPTOS_THROUGHPUT;
553 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
554 (void *)&tos, sizeof(int)) < 0)
555 warn("TOS (ignored)");
558 send_auth(host, dest_realm);
562 paranoid_setuid(userid);
570 sink(int argc, char **argv)
574 struct timeval tv[2];
575 enum { YES, NO, DISPLAYED } wrerr;
578 int amt, count, exists, first, mask, mode, ofd, omode;
579 int setimes, size, targisdir, wrerrno=0;
580 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
584 #define SCREWUP(str) { why = str; goto screwup; }
586 setimes = targisdir = 0;
591 run_err("ambiguous target");
595 if (targetshouldbedirectory)
597 des_write(rem, "", 1);
598 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
600 for (first = 1;; first = 0) {
602 if (des_read(rem, cp, 1) <= 0)
605 SCREWUP("unexpected <newline>");
607 if (des_read(rem, &ch, sizeof(ch)) != sizeof(ch))
608 SCREWUP("lost connection");
610 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
613 if (buf[0] == '\01' || buf[0] == '\02') {
616 buf + 1, strlen(buf + 1));
623 des_write(rem, "", 1);
633 while (isdigit((unsigned char)*cp)) \
634 (t) = (t) * 10 + (*cp++ - '0'); \
641 getnum(mtime.tv_sec);
643 SCREWUP("mtime.sec not delimited");
644 getnum(mtime.tv_usec);
646 SCREWUP("mtime.usec not delimited");
647 getnum(atime.tv_sec);
649 SCREWUP("atime.sec not delimited");
650 getnum(atime.tv_usec);
652 SCREWUP("atime.usec not delimited");
653 des_write(rem, "", 1);
656 if (*cp != 'C' && *cp != 'D') {
658 * Check for the case "rcp remote:foo\* local:bar".
659 * In this case, the line "No match." can be returned
660 * by the shell before the rcp command on the remote is
661 * executed so the ^Aerror_message convention isn't
668 SCREWUP("expected control record");
671 for (++cp; cp < buf + 5; cp++) {
672 if (*cp < '0' || *cp > '7')
674 mode = (mode << 3) | (*cp - '0');
677 SCREWUP("mode not delimited");
679 for (size = 0; isdigit((unsigned char)*cp);)
680 size = size * 10 + (*cp++ - '0');
682 SCREWUP("size not delimited");
684 static char *namebuf;
688 need = strlen(targ) + strlen(cp) + 250;
689 if (need > cursize) {
690 if (!(namebuf = malloc(need)))
691 run_err("%s", strerror(errno));
693 snprintf(namebuf, need, "%s%s%s", targ,
694 *targ ? "/" : "", cp);
698 exists = stat(np, &stb) == 0;
700 int mod_flag = pflag;
702 if (!S_ISDIR(stb.st_mode)) {
709 /* Handle copying from a read-only directory */
711 if (mkdir(np, mode | S_IRWXU) < 0)
717 struct utimbuf times;
718 times.actime = atime.tv_sec;
719 times.modtime = mtime.tv_sec;
721 if (utime(np, ×) < 0)
722 run_err("%s: set times: %s",
723 np, strerror(errno));
731 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
732 bad: run_err("%s: %s", np, strerror(errno));
735 des_write(rem, "", 1);
736 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
742 for (count = i = 0; i < size; i += BUFSIZ) {
748 j = des_read(rem, cp, amt);
750 run_err("%s", j ? strerror(errno) :
751 "dropped connection");
757 if (count == bp->cnt) {
758 /* Keep reading so we stay sync'd up. */
760 j = write(ofd, bp->buf, count);
763 wrerrno = j >= 0 ? EIO : errno;
770 if (count != 0 && wrerr == NO &&
771 (j = write(ofd, bp->buf, count)) != count) {
773 wrerrno = j >= 0 ? EIO : errno;
775 if (ftruncate(ofd, size)) {
776 run_err("%s: truncate: %s", np, strerror(errno));
780 if (exists || omode != mode)
782 if (fchmod(ofd, omode))
784 if (chmod(np, omode))
786 run_err("%s: set mode: %s",
787 np, strerror(errno));
789 if (!exists && omode != mode)
791 if (fchmod(ofd, omode & ~mask))
793 if (chmod(np, omode & ~mask))
795 run_err("%s: set mode: %s",
796 np, strerror(errno));
800 if (setimes && wrerr == NO) {
801 struct utimbuf times;
802 times.actime = atime.tv_sec;
803 times.modtime = mtime.tv_sec;
805 if (utime(np, ×) < 0) {
806 run_err("%s: set times: %s",
807 np, strerror(errno));
813 run_err("%s: %s", np, strerror(wrerrno));
816 des_write(rem, "", 1);
823 run_err("protocol error: %s", why);
828 tolocal(int argc, char **argv)
834 char *bp, *host, *src, *suser;
836 for (i = 0; i < argc - 1; i++) {
837 if (!(src = colon(argv[i]))) { /* Local to local. */
838 len = strlen(_PATH_CP) + strlen(argv[i]) +
839 strlen(argv[argc - 1]) + 20;
840 if (!(bp = malloc(len)))
842 snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
843 iamrecursive ? " -r" : "", pflag ? " -p" : "",
844 argv[i], argv[argc - 1]);
845 if (susystem(bp, userid))
853 if ((host = strchr(argv[i], '@')) == NULL) {
855 errx (1, "Sorry, you need to specify the username");
858 suser = pwd->pw_name;
867 errx (1, "Sorry, you need to specify the username");
869 suser = pwd->pw_name;
871 else if (!okname(suser))
874 len = strlen(src) + CMDNEEDS + 20;
875 if ((bp = malloc(len)) == NULL)
877 snprintf(bp, len, "%s -f %s", cmd, src);
900 #if defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
901 tos = IPTOS_THROUGHPUT;
902 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (void *)&tos,
904 warn("TOS (ignored)");
907 send_auth(host, dest_realm);
908 sink(1, argv + argc - 1);
917 main(int argc, char **argv)
919 int ch, fflag, tflag;
923 set_progname(argv[0]);
926 * Prepare for execing ourselves.
929 argc_copy = argc + 1;
930 argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
931 if (argv_copy == NULL)
933 argv_copy[0] = argv[0];
935 for(i = 1; i < argc; ++i) {
936 argv_copy[i + 1] = strdup(argv[i]);
937 if (argv_copy[i + 1] == NULL)
938 errx(1, "strdup: out of memory");
940 argv_copy[argc + 1] = NULL;
944 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
945 switch(ch) { /* User-visible flags. */
950 dest_realm = dst_realm_buf;
951 strlcpy(dst_realm_buf, optarg, REALM_SZ);
963 /* Server options. */
965 targetshouldbedirectory = 1;
967 case 'f': /* "from" */
985 /* Rcp implements encrypted file transfer without using the
986 * kshell service, pass 0 for no encryption */
987 port = get_shell_port(use_kerberos, 0);
992 if ((pwd = k_getpwuid(userid)) == NULL)
993 errx(1, "unknown user %d", (int)userid);
996 rem = STDIN_FILENO; /* XXX */
998 if (fflag || tflag) {
1003 if(do_osfc2_magic(pwd->pw_uid))
1005 paranoid_setuid(userid);
1007 /* Sometimes we will need cell specific tokens
1008 * to be able to read and write files, thus,
1009 * the token stuff done in rshd might not
1013 if (k_afs_cell_of_file(pwd->pw_dir,
1014 cell, sizeof(cell)) == 0)
1015 krb_afslog(cell, 0);
1028 targetshouldbedirectory = 1;
1031 /* Command to be executed on remote system using "rsh". */
1032 snprintf(cmd, sizeof(cmd),
1033 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
1034 (doencrypt && use_kerberos ? " -x" : ""),
1035 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
1037 signal(SIGPIPE, lostconn);
1039 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
1040 toremote(targ, argc, argv);
1042 tolocal(argc, argv); /* Dest is local host. */
1043 if (targetshouldbedirectory)
1044 verifydir(argv[argc - 1]);