rcp.c revision 169848
1139965Simp/*- 21556Srgrimes * Copyright (c) 1983, 1990, 1992, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 496196Sdes * Copyright (c) 2002 Networks Associates Technology, Inc. 596196Sdes * All rights reserved. 61556Srgrimes * 796196Sdes * Portions of this software were developed for the FreeBSD Project by 896196Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network 996196Sdes * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 1096196Sdes * ("CBOSS"), as part of the DARPA CHATS research program. 1196196Sdes * 121556Srgrimes * Redistribution and use in source and binary forms, with or without 131556Srgrimes * modification, are permitted provided that the following conditions 141556Srgrimes * are met: 151556Srgrimes * 1. Redistributions of source code must retain the above copyright 161556Srgrimes * notice, this list of conditions and the following disclaimer. 171556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 181556Srgrimes * notice, this list of conditions and the following disclaimer in the 191556Srgrimes * documentation and/or other materials provided with the distribution. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 37114433Sobrien#if 0 381556Srgrimes#ifndef lint 3920420Sstevestatic char const copyright[] = 401556Srgrimes"@(#) Copyright (c) 1983, 1990, 1992, 1993\n\ 411556Srgrimes The Regents of the University of California. All rights reserved.\n"; 421556Srgrimes#endif /* not lint */ 431556Srgrimes 441556Srgrimes#ifndef lint 4536149Scharnierstatic char sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94"; 46104559Scharnier#endif /* not lint */ 4736149Scharnier#endif 4899110Sobrien#include <sys/cdefs.h> 4999110Sobrien__FBSDID("$FreeBSD: head/bin/rcp/rcp.c 169848 2007-05-22 04:21:00Z cperciva $"); 501556Srgrimes 511556Srgrimes#include <sys/param.h> 521556Srgrimes#include <sys/stat.h> 531556Srgrimes#include <sys/time.h> 541556Srgrimes#include <sys/socket.h> 551556Srgrimes#include <netinet/in.h> 561556Srgrimes#include <netinet/in_systm.h> 571556Srgrimes#include <netinet/ip.h> 581556Srgrimes 591556Srgrimes#include <ctype.h> 601556Srgrimes#include <dirent.h> 611556Srgrimes#include <err.h> 621556Srgrimes#include <errno.h> 631556Srgrimes#include <fcntl.h> 6477462Simp#include <libutil.h> 6577462Simp#include <limits.h> 661556Srgrimes#include <netdb.h> 6796196Sdes#include <paths.h> 681556Srgrimes#include <pwd.h> 691556Srgrimes#include <signal.h> 70104549Stjr#include <stdint.h> 711556Srgrimes#include <stdio.h> 721556Srgrimes#include <stdlib.h> 731556Srgrimes#include <string.h> 741556Srgrimes#include <string.h> 751556Srgrimes#include <unistd.h> 761556Srgrimes 771556Srgrimes#include "extern.h" 781556Srgrimes 79101591Sume#define OPTIONS "46dfprt" 801556Srgrimes 811556Srgrimesstruct passwd *pwd; 821556Srgrimesu_short port; 831556Srgrimesuid_t userid; 841556Srgrimesint errs, rem; 851556Srgrimesint pflag, iamremote, iamrecursive, targetshouldbedirectory; 86101591Sumeint family = PF_UNSPEC; 871556Srgrimes 8834898Smarkmstatic int argc_copy; 89114583Smarkmstatic const char **argv_copy; 9034898Smarkm 91114583Smarkmstatic char period[] = "."; 92114583Smarkm 931556Srgrimes#define CMDNEEDS 64 941556Srgrimeschar cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ 951556Srgrimes 9690110Simpint response(void); 9790110Simpvoid rsource(char *, struct stat *); 9890110Simpvoid sink(int, char *[]); 9990110Simpvoid source(int, char *[]); 10090110Simpvoid tolocal(int, char *[]); 10190110Simpvoid toremote(char *, int, char *[]); 10290110Simpvoid usage(void); 1031556Srgrimes 1041556Srgrimesint 10590110Simpmain(int argc, char *argv[]) 1061556Srgrimes{ 1071556Srgrimes struct servent *sp; 10848560Sbde int ch, fflag, i, tflag; 109114583Smarkm char *targ; 1101556Srgrimes 11134898Smarkm /* 11234898Smarkm * Prepare for execing ourselves. 11334898Smarkm */ 11434898Smarkm argc_copy = argc + 1; 11534898Smarkm argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy)); 11634898Smarkm if (argv_copy == NULL) 11734898Smarkm err(1, "malloc"); 11834898Smarkm argv_copy[0] = argv[0]; 11934898Smarkm argv_copy[1] = "-K"; 12048560Sbde for (i = 1; i < argc; ++i) { 12134898Smarkm argv_copy[i + 1] = strdup(argv[i]); 12234898Smarkm if (argv_copy[i + 1] == NULL) 12334898Smarkm errx(1, "strdup: out of memory"); 12434898Smarkm } 12534898Smarkm argv_copy[argc + 1] = NULL; 12634898Smarkm 1271556Srgrimes fflag = tflag = 0; 12824348Simp while ((ch = getopt(argc, argv, OPTIONS)) != -1) 1291556Srgrimes switch(ch) { /* User-visible flags. */ 130101591Sume case '4': 131101591Sume family = PF_INET; 132101591Sume break; 133101591Sume 134101591Sume case '6': 135101591Sume family = PF_INET6; 136101591Sume break; 137101591Sume 1381556Srgrimes case 'p': 1391556Srgrimes pflag = 1; 1401556Srgrimes break; 1411556Srgrimes case 'r': 1421556Srgrimes iamrecursive = 1; 1431556Srgrimes break; 1441556Srgrimes /* Server options. */ 1451556Srgrimes case 'd': 1461556Srgrimes targetshouldbedirectory = 1; 1471556Srgrimes break; 1481556Srgrimes case 'f': /* "from" */ 1491556Srgrimes iamremote = 1; 1501556Srgrimes fflag = 1; 1511556Srgrimes break; 1521556Srgrimes case 't': /* "to" */ 1531556Srgrimes iamremote = 1; 1541556Srgrimes tflag = 1; 1551556Srgrimes break; 1561556Srgrimes case '?': 1571556Srgrimes default: 1581556Srgrimes usage(); 1591556Srgrimes } 1601556Srgrimes argc -= optind; 1611556Srgrimes argv += optind; 1621556Srgrimes 163114583Smarkm sp = getservbyname("shell", "tcp"); 1641556Srgrimes if (sp == NULL) 165114583Smarkm errx(1, "shell/tcp: unknown service"); 1661556Srgrimes port = sp->s_port; 1671556Srgrimes 1681556Srgrimes if ((pwd = getpwuid(userid = getuid())) == NULL) 1691556Srgrimes errx(1, "unknown user %d", (int)userid); 1701556Srgrimes 1711556Srgrimes rem = STDIN_FILENO; /* XXX */ 1721556Srgrimes 1731556Srgrimes if (fflag) { /* Follow "protocol", send data. */ 1741556Srgrimes (void)response(); 1751556Srgrimes (void)setuid(userid); 1761556Srgrimes source(argc, argv); 1771556Srgrimes exit(errs); 1781556Srgrimes } 1791556Srgrimes 1801556Srgrimes if (tflag) { /* Receive data. */ 1811556Srgrimes (void)setuid(userid); 1821556Srgrimes sink(argc, argv); 1831556Srgrimes exit(errs); 1841556Srgrimes } 1851556Srgrimes 1861556Srgrimes if (argc < 2) 1871556Srgrimes usage(); 1881556Srgrimes if (argc > 2) 1891556Srgrimes targetshouldbedirectory = 1; 1901556Srgrimes 1911556Srgrimes rem = -1; 1921556Srgrimes /* Command to be executed on remote system using "rsh". */ 1931556Srgrimes (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s", 1941556Srgrimes iamrecursive ? " -r" : "", pflag ? " -p" : "", 1951556Srgrimes targetshouldbedirectory ? " -d" : ""); 1961556Srgrimes 1971556Srgrimes (void)signal(SIGPIPE, lostconn); 1981556Srgrimes 1997165Sjoerg if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ 2001556Srgrimes toremote(targ, argc, argv); 2011556Srgrimes else { 2021556Srgrimes tolocal(argc, argv); /* Dest is local host. */ 2031556Srgrimes if (targetshouldbedirectory) 2041556Srgrimes verifydir(argv[argc - 1]); 2051556Srgrimes } 2061556Srgrimes exit(errs); 2071556Srgrimes} 2081556Srgrimes 2091556Srgrimesvoid 21090110Simptoremote(char *targ, int argc, char *argv[]) 2111556Srgrimes{ 212121095Smarkm int i, tos; 2131556Srgrimes char *bp, *host, *src, *suser, *thost, *tuser; 2141556Srgrimes 2151556Srgrimes *targ++ = 0; 2161556Srgrimes if (*targ == 0) 217114583Smarkm targ = period; 2181556Srgrimes 2197165Sjoerg if ((thost = strchr(argv[argc - 1], '@'))) { 2201556Srgrimes /* user@host */ 2211556Srgrimes *thost++ = 0; 2221556Srgrimes tuser = argv[argc - 1]; 2231556Srgrimes if (*tuser == '\0') 2241556Srgrimes tuser = NULL; 2251556Srgrimes else if (!okname(tuser)) 2261556Srgrimes exit(1); 2271556Srgrimes } else { 2281556Srgrimes thost = argv[argc - 1]; 2291556Srgrimes tuser = NULL; 2301556Srgrimes } 2311556Srgrimes 2321556Srgrimes for (i = 0; i < argc - 1; i++) { 2331556Srgrimes src = colon(argv[i]); 2341556Srgrimes if (src) { /* remote to remote */ 2351556Srgrimes *src++ = 0; 2361556Srgrimes if (*src == 0) 237114583Smarkm src = period; 2381556Srgrimes host = strchr(argv[i], '@'); 2391556Srgrimes if (host) { 2401556Srgrimes *host++ = 0; 2411556Srgrimes suser = argv[i]; 2421556Srgrimes if (*suser == '\0') 2431556Srgrimes suser = pwd->pw_name; 24477491Spirzyk else if (!okname(suser)) { 24577491Spirzyk ++errs; 2461556Srgrimes continue; 24777491Spirzyk } 248121095Smarkm if (asprintf(&bp, 2491556Srgrimes "%s %s -l %s -n %s %s '%s%s%s:%s'", 2501556Srgrimes _PATH_RSH, host, suser, cmd, src, 2511556Srgrimes tuser ? tuser : "", tuser ? "@" : "", 252121095Smarkm thost, targ) == -1) 253121095Smarkm err(1, "asprintf"); 2541556Srgrimes } else 255121095Smarkm if (asprintf(&bp, 2561556Srgrimes "exec %s %s -n %s %s '%s%s%s:%s'", 2571556Srgrimes _PATH_RSH, argv[i], cmd, src, 2581556Srgrimes tuser ? tuser : "", tuser ? "@" : "", 259121095Smarkm thost, targ) == -1) 260121095Smarkm err(1, "asprintf"); 2611556Srgrimes (void)susystem(bp, userid); 2621556Srgrimes (void)free(bp); 2631556Srgrimes } else { /* local to remote */ 2641556Srgrimes if (rem == -1) { 265121095Smarkm if (asprintf(&bp, "%s -t %s", cmd, targ) 266121095Smarkm == -1) 267121095Smarkm err(1, "asprintf"); 2681556Srgrimes host = thost; 269105269Smarkm rem = rcmd_af(&host, port, 270105269Smarkm pwd->pw_name, 271105269Smarkm tuser ? tuser : pwd->pw_name, 272105269Smarkm bp, 0, family); 2731556Srgrimes if (rem < 0) 2741556Srgrimes exit(1); 275101591Sume if (family == PF_INET) { 276101591Sume tos = IPTOS_THROUGHPUT; 277101591Sume if (setsockopt(rem, IPPROTO_IP, IP_TOS, 278101591Sume &tos, sizeof(int)) < 0) 279101591Sume warn("TOS (ignored)"); 280101591Sume } 2811556Srgrimes if (response() < 0) 2821556Srgrimes exit(1); 2831556Srgrimes (void)free(bp); 2841556Srgrimes (void)setuid(userid); 2851556Srgrimes } 2861556Srgrimes source(1, argv+i); 2871556Srgrimes } 2881556Srgrimes } 2891556Srgrimes} 2901556Srgrimes 2911556Srgrimesvoid 29290110Simptolocal(int argc, char *argv[]) 2931556Srgrimes{ 2941556Srgrimes int i, len, tos; 2951556Srgrimes char *bp, *host, *src, *suser; 2961556Srgrimes 2971556Srgrimes for (i = 0; i < argc - 1; i++) { 2981556Srgrimes if (!(src = colon(argv[i]))) { /* Local to local. */ 2991556Srgrimes len = strlen(_PATH_CP) + strlen(argv[i]) + 3001556Srgrimes strlen(argv[argc - 1]) + 20; 3011556Srgrimes if (!(bp = malloc(len))) 30299744Sdillon err(1, "malloc"); 3031556Srgrimes (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP, 30431633Swosch iamrecursive ? " -PR" : "", pflag ? " -p" : "", 3051556Srgrimes argv[i], argv[argc - 1]); 3061556Srgrimes if (susystem(bp, userid)) 3071556Srgrimes ++errs; 3081556Srgrimes (void)free(bp); 3091556Srgrimes continue; 3101556Srgrimes } 3111556Srgrimes *src++ = 0; 3121556Srgrimes if (*src == 0) 313114583Smarkm src = period; 3141556Srgrimes if ((host = strchr(argv[i], '@')) == NULL) { 3151556Srgrimes host = argv[i]; 3161556Srgrimes suser = pwd->pw_name; 3171556Srgrimes } else { 3181556Srgrimes *host++ = 0; 3191556Srgrimes suser = argv[i]; 3201556Srgrimes if (*suser == '\0') 3211556Srgrimes suser = pwd->pw_name; 32277491Spirzyk else if (!okname(suser)) { 32377491Spirzyk ++errs; 3241556Srgrimes continue; 32577491Spirzyk } 3261556Srgrimes } 3271556Srgrimes len = strlen(src) + CMDNEEDS + 20; 3281556Srgrimes if ((bp = malloc(len)) == NULL) 32999744Sdillon err(1, "malloc"); 3301556Srgrimes (void)snprintf(bp, len, "%s -f %s", cmd, src); 331105269Smarkm rem = rcmd_af(&host, port, pwd->pw_name, suser, bp, 0, 332101591Sume family); 3331556Srgrimes (void)free(bp); 3341556Srgrimes if (rem < 0) { 3351556Srgrimes ++errs; 3361556Srgrimes continue; 3371556Srgrimes } 3381556Srgrimes (void)seteuid(userid); 339101591Sume if (family == PF_INET) { 340101591Sume tos = IPTOS_THROUGHPUT; 341101591Sume if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, 342101591Sume sizeof(int)) < 0) 343101591Sume warn("TOS (ignored)"); 344101591Sume } 3451556Srgrimes sink(1, argv + argc - 1); 3461556Srgrimes (void)seteuid(0); 3471556Srgrimes (void)close(rem); 3481556Srgrimes rem = -1; 3491556Srgrimes } 3501556Srgrimes} 3511556Srgrimes 3521556Srgrimesvoid 35390110Simpsource(int argc, char *argv[]) 3541556Srgrimes{ 3551556Srgrimes struct stat stb; 3561556Srgrimes static BUF buffer; 3571556Srgrimes BUF *bp; 3581556Srgrimes off_t i; 3591556Srgrimes int amt, fd, haderr, indx, result; 3601556Srgrimes char *last, *name, buf[BUFSIZ]; 3611556Srgrimes 3621556Srgrimes for (indx = 0; indx < argc; ++indx) { 36396196Sdes name = argv[indx]; 3641556Srgrimes if ((fd = open(name, O_RDONLY, 0)) < 0) 3651556Srgrimes goto syserr; 3661556Srgrimes if (fstat(fd, &stb)) { 3671556Srgrimessyserr: run_err("%s: %s", name, strerror(errno)); 3681556Srgrimes goto next; 3691556Srgrimes } 3701556Srgrimes switch (stb.st_mode & S_IFMT) { 3711556Srgrimes case S_IFREG: 3721556Srgrimes break; 3731556Srgrimes case S_IFDIR: 3741556Srgrimes if (iamrecursive) { 3751556Srgrimes rsource(name, &stb); 3761556Srgrimes goto next; 3771556Srgrimes } 3781556Srgrimes /* FALLTHROUGH */ 3791556Srgrimes default: 3801556Srgrimes run_err("%s: not a regular file", name); 3811556Srgrimes goto next; 3821556Srgrimes } 3831556Srgrimes if ((last = strrchr(name, '/')) == NULL) 3841556Srgrimes last = name; 3851556Srgrimes else 3861556Srgrimes ++last; 3871556Srgrimes if (pflag) { 3881556Srgrimes /* 3891556Srgrimes * Make it compatible with possible future 3901556Srgrimes * versions expecting microseconds. 3911556Srgrimes */ 3921556Srgrimes (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n", 39338018Sbde (long)stb.st_mtimespec.tv_sec, 39438018Sbde (long)stb.st_atimespec.tv_sec); 3951556Srgrimes (void)write(rem, buf, strlen(buf)); 3961556Srgrimes if (response() < 0) 3971556Srgrimes goto next; 3981556Srgrimes } 3991556Srgrimes#define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) 400104549Stjr (void)snprintf(buf, sizeof(buf), "C%04o %jd %s\n", 401104549Stjr stb.st_mode & MODEMASK, (intmax_t)stb.st_size, last); 4021556Srgrimes (void)write(rem, buf, strlen(buf)); 4031556Srgrimes if (response() < 0) 4041556Srgrimes goto next; 4051556Srgrimes if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) { 406169848Scpercivanext: if (fd >= 0) 407169848Scperciva (void)close(fd); 4081556Srgrimes continue; 4091556Srgrimes } 4101556Srgrimes 4111556Srgrimes /* Keep writing after an error so that we stay sync'd up. */ 4121556Srgrimes for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { 4131556Srgrimes amt = bp->cnt; 4141556Srgrimes if (i + amt > stb.st_size) 4151556Srgrimes amt = stb.st_size - i; 4161556Srgrimes if (!haderr) { 4171556Srgrimes result = read(fd, bp->buf, amt); 4181556Srgrimes if (result != amt) 4191556Srgrimes haderr = result >= 0 ? EIO : errno; 4201556Srgrimes } 4211556Srgrimes if (haderr) 4221556Srgrimes (void)write(rem, bp->buf, amt); 4231556Srgrimes else { 4241556Srgrimes result = write(rem, bp->buf, amt); 4251556Srgrimes if (result != amt) 4261556Srgrimes haderr = result >= 0 ? EIO : errno; 4271556Srgrimes } 4281556Srgrimes } 4291556Srgrimes if (close(fd) && !haderr) 4301556Srgrimes haderr = errno; 4311556Srgrimes if (!haderr) 4321556Srgrimes (void)write(rem, "", 1); 4331556Srgrimes else 4341556Srgrimes run_err("%s: %s", name, strerror(haderr)); 4351556Srgrimes (void)response(); 4361556Srgrimes } 4371556Srgrimes} 4381556Srgrimes 4391556Srgrimesvoid 44090110Simprsource(char *name, struct stat *statp) 4411556Srgrimes{ 4421556Srgrimes DIR *dirp; 4431556Srgrimes struct dirent *dp; 44477462Simp char *last, *vect[1], path[PATH_MAX]; 4451556Srgrimes 4461556Srgrimes if (!(dirp = opendir(name))) { 4471556Srgrimes run_err("%s: %s", name, strerror(errno)); 4481556Srgrimes return; 4491556Srgrimes } 4501556Srgrimes last = strrchr(name, '/'); 4511556Srgrimes if (last == 0) 4521556Srgrimes last = name; 4531556Srgrimes else 4541556Srgrimes last++; 4551556Srgrimes if (pflag) { 4561556Srgrimes (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n", 45738018Sbde (long)statp->st_mtimespec.tv_sec, 45838018Sbde (long)statp->st_atimespec.tv_sec); 4591556Srgrimes (void)write(rem, path, strlen(path)); 4601556Srgrimes if (response() < 0) { 4611556Srgrimes closedir(dirp); 4621556Srgrimes return; 4631556Srgrimes } 4641556Srgrimes } 4651556Srgrimes (void)snprintf(path, sizeof(path), 4661556Srgrimes "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last); 4671556Srgrimes (void)write(rem, path, strlen(path)); 4681556Srgrimes if (response() < 0) { 4691556Srgrimes closedir(dirp); 4701556Srgrimes return; 4711556Srgrimes } 4727165Sjoerg while ((dp = readdir(dirp))) { 4731556Srgrimes if (dp->d_ino == 0) 4741556Srgrimes continue; 4751556Srgrimes if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) 4761556Srgrimes continue; 47777462Simp if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) { 4781556Srgrimes run_err("%s/%s: name too long", name, dp->d_name); 4791556Srgrimes continue; 4801556Srgrimes } 4811556Srgrimes (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name); 4821556Srgrimes vect[0] = path; 4831556Srgrimes source(1, vect); 4841556Srgrimes } 4851556Srgrimes (void)closedir(dirp); 4861556Srgrimes (void)write(rem, "E\n", 2); 4871556Srgrimes (void)response(); 4881556Srgrimes} 4891556Srgrimes 4901556Srgrimesvoid 49190110Simpsink(int argc, char *argv[]) 4921556Srgrimes{ 4931556Srgrimes static BUF buffer; 4941556Srgrimes struct stat stb; 4951556Srgrimes struct timeval tv[2]; 4961556Srgrimes enum { YES, NO, DISPLAYED } wrerr; 4971556Srgrimes BUF *bp; 49846057Sdt off_t i, j, size; 499114509Sobrien int amt, exists, first, mask, mode, ofd, omode; 500114509Sobrien size_t count; 50146057Sdt int setimes, targisdir, wrerrno = 0; 502114583Smarkm char ch, *cp, *np, *targ, *vect[1], buf[BUFSIZ], path[PATH_MAX]; 503114583Smarkm const char *why; 5041556Srgrimes 5051556Srgrimes#define atime tv[0] 5061556Srgrimes#define mtime tv[1] 5071556Srgrimes#define SCREWUP(str) { why = str; goto screwup; } 5081556Srgrimes 5091556Srgrimes setimes = targisdir = 0; 5101556Srgrimes mask = umask(0); 5111556Srgrimes if (!pflag) 5121556Srgrimes (void)umask(mask); 5131556Srgrimes if (argc != 1) { 5141556Srgrimes run_err("ambiguous target"); 5151556Srgrimes exit(1); 5161556Srgrimes } 5171556Srgrimes targ = *argv; 5181556Srgrimes if (targetshouldbedirectory) 5191556Srgrimes verifydir(targ); 5201556Srgrimes (void)write(rem, "", 1); 5211556Srgrimes if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) 5221556Srgrimes targisdir = 1; 5231556Srgrimes for (first = 1;; first = 0) { 5241556Srgrimes cp = buf; 5251556Srgrimes if (read(rem, cp, 1) <= 0) 5261556Srgrimes return; 5271556Srgrimes if (*cp++ == '\n') 5281556Srgrimes SCREWUP("unexpected <newline>"); 5291556Srgrimes do { 5301556Srgrimes if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 5311556Srgrimes SCREWUP("lost connection"); 5321556Srgrimes *cp++ = ch; 5331556Srgrimes } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 5341556Srgrimes *cp = 0; 5351556Srgrimes 5361556Srgrimes if (buf[0] == '\01' || buf[0] == '\02') { 5371556Srgrimes if (iamremote == 0) 5381556Srgrimes (void)write(STDERR_FILENO, 5391556Srgrimes buf + 1, strlen(buf + 1)); 5401556Srgrimes if (buf[0] == '\02') 5411556Srgrimes exit(1); 5421556Srgrimes ++errs; 5431556Srgrimes continue; 5441556Srgrimes } 5451556Srgrimes if (buf[0] == 'E') { 5461556Srgrimes (void)write(rem, "", 1); 5471556Srgrimes return; 5481556Srgrimes } 5491556Srgrimes 5501556Srgrimes if (ch == '\n') 5511556Srgrimes *--cp = 0; 5521556Srgrimes 5531556Srgrimes cp = buf; 5541556Srgrimes if (*cp == 'T') { 5551556Srgrimes setimes++; 5561556Srgrimes cp++; 55713978Spst mtime.tv_sec = strtol(cp, &cp, 10); 55813978Spst if (!cp || *cp++ != ' ') 5591556Srgrimes SCREWUP("mtime.sec not delimited"); 56013978Spst mtime.tv_usec = strtol(cp, &cp, 10); 56113978Spst if (!cp || *cp++ != ' ') 5621556Srgrimes SCREWUP("mtime.usec not delimited"); 56313978Spst atime.tv_sec = strtol(cp, &cp, 10); 56413978Spst if (!cp || *cp++ != ' ') 5651556Srgrimes SCREWUP("atime.sec not delimited"); 56613978Spst atime.tv_usec = strtol(cp, &cp, 10); 56713978Spst if (!cp || *cp++ != '\0') 5681556Srgrimes SCREWUP("atime.usec not delimited"); 5691556Srgrimes (void)write(rem, "", 1); 5701556Srgrimes continue; 5711556Srgrimes } 5721556Srgrimes if (*cp != 'C' && *cp != 'D') { 5731556Srgrimes /* 5741556Srgrimes * Check for the case "rcp remote:foo\* local:bar". 5751556Srgrimes * In this case, the line "No match." can be returned 5761556Srgrimes * by the shell before the rcp command on the remote is 5771556Srgrimes * executed so the ^Aerror_message convention isn't 5781556Srgrimes * followed. 5791556Srgrimes */ 5801556Srgrimes if (first) { 5811556Srgrimes run_err("%s", cp); 5821556Srgrimes exit(1); 5831556Srgrimes } 5841556Srgrimes SCREWUP("expected control record"); 5851556Srgrimes } 5861556Srgrimes mode = 0; 5871556Srgrimes for (++cp; cp < buf + 5; cp++) { 5881556Srgrimes if (*cp < '0' || *cp > '7') 5891556Srgrimes SCREWUP("bad mode"); 5901556Srgrimes mode = (mode << 3) | (*cp - '0'); 5911556Srgrimes } 5921556Srgrimes if (*cp++ != ' ') 5931556Srgrimes SCREWUP("mode not delimited"); 5941556Srgrimes 5951556Srgrimes for (size = 0; isdigit(*cp);) 5961556Srgrimes size = size * 10 + (*cp++ - '0'); 5971556Srgrimes if (*cp++ != ' ') 5981556Srgrimes SCREWUP("size not delimited"); 5991556Srgrimes if (targisdir) { 600104559Scharnier if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp) 601104559Scharnier >= sizeof(path)) { 602104559Scharnier run_err("%s%s%s: name too long", targ, 603104559Scharnier *targ ? "/" : "", cp); 604104559Scharnier exit(1); 6051556Srgrimes } 606104559Scharnier (void)snprintf(path, sizeof(path), "%s%s%s", targ, 6071556Srgrimes *targ ? "/" : "", cp); 608104559Scharnier np = path; 6091556Srgrimes } else 6101556Srgrimes np = targ; 6111556Srgrimes exists = stat(np, &stb) == 0; 6121556Srgrimes if (buf[0] == 'D') { 6131556Srgrimes int mod_flag = pflag; 6141556Srgrimes if (exists) { 6151556Srgrimes if (!S_ISDIR(stb.st_mode)) { 6161556Srgrimes errno = ENOTDIR; 6171556Srgrimes goto bad; 6181556Srgrimes } 6191556Srgrimes if (pflag) 6201556Srgrimes (void)chmod(np, mode); 6211556Srgrimes } else { 6221556Srgrimes /* Handle copying from a read-only directory */ 6231556Srgrimes mod_flag = 1; 6241556Srgrimes if (mkdir(np, mode | S_IRWXU) < 0) 6251556Srgrimes goto bad; 6261556Srgrimes } 6271556Srgrimes vect[0] = np; 6281556Srgrimes sink(1, vect); 6291556Srgrimes if (setimes) { 6301556Srgrimes setimes = 0; 6311556Srgrimes if (utimes(np, tv) < 0) 6321556Srgrimes run_err("%s: set times: %s", 6331556Srgrimes np, strerror(errno)); 6341556Srgrimes } 6351556Srgrimes if (mod_flag) 6361556Srgrimes (void)chmod(np, mode); 6371556Srgrimes continue; 6381556Srgrimes } 6391556Srgrimes omode = mode; 6401556Srgrimes mode |= S_IWRITE; 6411556Srgrimes if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 6421556Srgrimesbad: run_err("%s: %s", np, strerror(errno)); 6431556Srgrimes continue; 6441556Srgrimes } 6451556Srgrimes (void)write(rem, "", 1); 6461556Srgrimes if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) { 6471556Srgrimes (void)close(ofd); 6481556Srgrimes continue; 6491556Srgrimes } 6501556Srgrimes cp = bp->buf; 6511556Srgrimes wrerr = NO; 6521556Srgrimes for (count = i = 0; i < size; i += BUFSIZ) { 6531556Srgrimes amt = BUFSIZ; 6541556Srgrimes if (i + amt > size) 6551556Srgrimes amt = size - i; 6561556Srgrimes count += amt; 6571556Srgrimes do { 6581556Srgrimes j = read(rem, cp, amt); 6591556Srgrimes if (j <= 0) { 6601556Srgrimes run_err("%s", j ? strerror(errno) : 6611556Srgrimes "dropped connection"); 6621556Srgrimes exit(1); 6631556Srgrimes } 6641556Srgrimes amt -= j; 6651556Srgrimes cp += j; 6661556Srgrimes } while (amt > 0); 6671556Srgrimes if (count == bp->cnt) { 6681556Srgrimes /* Keep reading so we stay sync'd up. */ 6691556Srgrimes if (wrerr == NO) { 6701556Srgrimes j = write(ofd, bp->buf, count); 671114583Smarkm if (j != (off_t)count) { 6721556Srgrimes wrerr = YES; 6738855Srgrimes wrerrno = j >= 0 ? EIO : errno; 6741556Srgrimes } 6751556Srgrimes } 6761556Srgrimes count = 0; 6771556Srgrimes cp = bp->buf; 6781556Srgrimes } 6791556Srgrimes } 6801556Srgrimes if (count != 0 && wrerr == NO && 681114583Smarkm (j = write(ofd, bp->buf, count)) != (off_t)count) { 6821556Srgrimes wrerr = YES; 6838855Srgrimes wrerrno = j >= 0 ? EIO : errno; 6841556Srgrimes } 6851556Srgrimes if (ftruncate(ofd, size)) { 6861556Srgrimes run_err("%s: truncate: %s", np, strerror(errno)); 6871556Srgrimes wrerr = DISPLAYED; 6881556Srgrimes } 6891556Srgrimes if (pflag) { 6901556Srgrimes if (exists || omode != mode) 6911556Srgrimes if (fchmod(ofd, omode)) 6921556Srgrimes run_err("%s: set mode: %s", 6931556Srgrimes np, strerror(errno)); 6941556Srgrimes } else { 6951556Srgrimes if (!exists && omode != mode) 6961556Srgrimes if (fchmod(ofd, omode & ~mask)) 6971556Srgrimes run_err("%s: set mode: %s", 6981556Srgrimes np, strerror(errno)); 6991556Srgrimes } 7001556Srgrimes (void)close(ofd); 7011556Srgrimes (void)response(); 7021556Srgrimes if (setimes && wrerr == NO) { 7031556Srgrimes setimes = 0; 7041556Srgrimes if (utimes(np, tv) < 0) { 7051556Srgrimes run_err("%s: set times: %s", 7061556Srgrimes np, strerror(errno)); 7071556Srgrimes wrerr = DISPLAYED; 7081556Srgrimes } 7091556Srgrimes } 7101556Srgrimes switch(wrerr) { 7111556Srgrimes case YES: 7121556Srgrimes run_err("%s: %s", np, strerror(wrerrno)); 7131556Srgrimes break; 7141556Srgrimes case NO: 7151556Srgrimes (void)write(rem, "", 1); 7161556Srgrimes break; 7171556Srgrimes case DISPLAYED: 7181556Srgrimes break; 7191556Srgrimes } 7201556Srgrimes } 7211556Srgrimesscrewup: 7221556Srgrimes run_err("protocol error: %s", why); 7231556Srgrimes exit(1); 7241556Srgrimes} 7251556Srgrimes 7261556Srgrimesint 72790110Simpresponse(void) 7281556Srgrimes{ 7291556Srgrimes char ch, *cp, resp, rbuf[BUFSIZ]; 7301556Srgrimes 7311556Srgrimes if (read(rem, &resp, sizeof(resp)) != sizeof(resp)) 7321556Srgrimes lostconn(0); 7331556Srgrimes 7341556Srgrimes cp = rbuf; 7351556Srgrimes switch(resp) { 7361556Srgrimes case 0: /* ok */ 7371556Srgrimes return (0); 7381556Srgrimes default: 7391556Srgrimes *cp++ = resp; 7401556Srgrimes /* FALLTHROUGH */ 7411556Srgrimes case 1: /* error, followed by error msg */ 7421556Srgrimes case 2: /* fatal error, "" */ 7431556Srgrimes do { 7441556Srgrimes if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 7451556Srgrimes lostconn(0); 7461556Srgrimes *cp++ = ch; 7471556Srgrimes } while (cp < &rbuf[BUFSIZ] && ch != '\n'); 7481556Srgrimes 7491556Srgrimes if (!iamremote) 7501556Srgrimes (void)write(STDERR_FILENO, rbuf, cp - rbuf); 7511556Srgrimes ++errs; 7521556Srgrimes if (resp == 1) 7531556Srgrimes return (-1); 7541556Srgrimes exit(1); 7551556Srgrimes } 7561556Srgrimes /* NOTREACHED */ 7571556Srgrimes} 7581556Srgrimes 7591556Srgrimesvoid 76090110Simpusage(void) 7611556Srgrimes{ 76226466Scharnier (void)fprintf(stderr, "%s\n%s\n", 763141578Sru "usage: rcp [-46p] file1 file2", 764141578Sru " rcp [-46pr] file ... directory"); 7651556Srgrimes exit(1); 7661556Srgrimes} 7671556Srgrimes 7681556Srgrimes#include <stdarg.h> 7691556Srgrimes 7701556Srgrimesvoid 7711556Srgrimesrun_err(const char *fmt, ...) 7721556Srgrimes{ 7731556Srgrimes static FILE *fp; 7741556Srgrimes va_list ap; 7751556Srgrimes 7761556Srgrimes ++errs; 7771556Srgrimes if (fp == NULL && !(fp = fdopen(rem, "w"))) 7781556Srgrimes return; 7791556Srgrimes (void)fprintf(fp, "%c", 0x01); 7801556Srgrimes (void)fprintf(fp, "rcp: "); 781104563Stjr va_start(ap, fmt); 7821556Srgrimes (void)vfprintf(fp, fmt, ap); 783104563Stjr va_end(ap); 7841556Srgrimes (void)fprintf(fp, "\n"); 7851556Srgrimes (void)fflush(fp); 7861556Srgrimes 787104563Stjr if (!iamremote) { 788104563Stjr va_start(ap, fmt); 7891556Srgrimes vwarnx(fmt, ap); 790104563Stjr va_end(ap); 791104563Stjr } 7921556Srgrimes 7931556Srgrimes va_end(ap); 7941556Srgrimes} 795