rcp.c revision 114509
11556Srgrimes/*
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 * 3. All advertising materials mentioning features or use of this software
211556Srgrimes *    must display the following acknowledgement:
221556Srgrimes *	This product includes software developed by the University of
231556Srgrimes *	California, Berkeley and its contributors.
241556Srgrimes * 4. Neither the name of the University nor the names of its contributors
251556Srgrimes *    may be used to endorse or promote products derived from this software
261556Srgrimes *    without specific prior written permission.
271556Srgrimes *
281556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
301556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
331556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
341556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381556Srgrimes * SUCH DAMAGE.
391556Srgrimes */
401556Srgrimes
41114433Sobrien#if 0
421556Srgrimes#ifndef lint
4320420Sstevestatic char const copyright[] =
441556Srgrimes"@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
451556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
461556Srgrimes#endif /* not lint */
471556Srgrimes
481556Srgrimes#ifndef lint
4936149Scharnierstatic char sccsid[] = "@(#)rcp.c	8.2 (Berkeley) 4/2/94";
50104559Scharnier#endif /* not lint */
5136149Scharnier#endif
5299110Sobrien#include <sys/cdefs.h>
5399110Sobrien__FBSDID("$FreeBSD: head/bin/rcp/rcp.c 114509 2003-05-02 06:36:52Z obrien $");
541556Srgrimes
551556Srgrimes#include <sys/param.h>
561556Srgrimes#include <sys/stat.h>
571556Srgrimes#include <sys/time.h>
581556Srgrimes#include <sys/socket.h>
591556Srgrimes#include <netinet/in.h>
601556Srgrimes#include <netinet/in_systm.h>
611556Srgrimes#include <netinet/ip.h>
621556Srgrimes
631556Srgrimes#include <ctype.h>
641556Srgrimes#include <dirent.h>
651556Srgrimes#include <err.h>
661556Srgrimes#include <errno.h>
671556Srgrimes#include <fcntl.h>
6877462Simp#include <libutil.h>
6977462Simp#include <limits.h>
701556Srgrimes#include <netdb.h>
7196196Sdes#include <paths.h>
721556Srgrimes#include <pwd.h>
731556Srgrimes#include <signal.h>
74104549Stjr#include <stdint.h>
751556Srgrimes#include <stdio.h>
761556Srgrimes#include <stdlib.h>
771556Srgrimes#include <string.h>
781556Srgrimes#include <string.h>
791556Srgrimes#include <unistd.h>
801556Srgrimes
811556Srgrimes#include "extern.h"
821556Srgrimes
83101591Sume#define	OPTIONS "46dfprt"
841556Srgrimes
851556Srgrimesstruct passwd *pwd;
861556Srgrimesu_short	port;
871556Srgrimesuid_t	userid;
881556Srgrimesint errs, rem;
891556Srgrimesint pflag, iamremote, iamrecursive, targetshouldbedirectory;
90101591Sumeint family = PF_UNSPEC;
911556Srgrimes
9234898Smarkmstatic int argc_copy;
9334898Smarkmstatic char **argv_copy;
9434898Smarkm
951556Srgrimes#define	CMDNEEDS	64
961556Srgrimeschar cmd[CMDNEEDS];		/* must hold "rcp -r -p -d\0" */
971556Srgrimes
9890110Simpint	 response(void);
9990110Simpvoid	 rsource(char *, struct stat *);
10090110Simpvoid	 run_err(const char *, ...) __printflike(1, 2);
10190110Simpvoid	 sink(int, char *[]);
10290110Simpvoid	 source(int, char *[]);
10390110Simpvoid	 tolocal(int, char *[]);
10490110Simpvoid	 toremote(char *, int, char *[]);
10590110Simpvoid	 usage(void);
1061556Srgrimes
1071556Srgrimesint
10890110Simpmain(int argc, char *argv[])
1091556Srgrimes{
1101556Srgrimes	struct servent *sp;
11148560Sbde	int ch, fflag, i, tflag;
1121556Srgrimes	char *targ, *shell;
1131556Srgrimes
11434898Smarkm	/*
11534898Smarkm	 * Prepare for execing ourselves.
11634898Smarkm	 */
11734898Smarkm	argc_copy = argc + 1;
11834898Smarkm	argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
11934898Smarkm	if (argv_copy == NULL)
12034898Smarkm		err(1, "malloc");
12134898Smarkm	argv_copy[0] = argv[0];
12234898Smarkm	argv_copy[1] = "-K";
12348560Sbde	for (i = 1; i < argc; ++i) {
12434898Smarkm		argv_copy[i + 1] = strdup(argv[i]);
12534898Smarkm		if (argv_copy[i + 1] == NULL)
12634898Smarkm			errx(1, "strdup: out of memory");
12734898Smarkm	}
12834898Smarkm	argv_copy[argc + 1] = NULL;
12934898Smarkm
1301556Srgrimes	fflag = tflag = 0;
13124348Simp	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
1321556Srgrimes		switch(ch) {			/* User-visible flags. */
133101591Sume		case '4':
134101591Sume			family = PF_INET;
135101591Sume			break;
136101591Sume
137101591Sume		case '6':
138101591Sume			family = PF_INET6;
139101591Sume			break;
140101591Sume
1411556Srgrimes		case 'p':
1421556Srgrimes			pflag = 1;
1431556Srgrimes			break;
1441556Srgrimes		case 'r':
1451556Srgrimes			iamrecursive = 1;
1461556Srgrimes			break;
1471556Srgrimes						/* Server options. */
1481556Srgrimes		case 'd':
1491556Srgrimes			targetshouldbedirectory = 1;
1501556Srgrimes			break;
1511556Srgrimes		case 'f':			/* "from" */
1521556Srgrimes			iamremote = 1;
1531556Srgrimes			fflag = 1;
1541556Srgrimes			break;
1551556Srgrimes		case 't':			/* "to" */
1561556Srgrimes			iamremote = 1;
1571556Srgrimes			tflag = 1;
1581556Srgrimes			break;
1591556Srgrimes		case '?':
1601556Srgrimes		default:
1611556Srgrimes			usage();
1621556Srgrimes		}
1631556Srgrimes	argc -= optind;
1641556Srgrimes	argv += optind;
1651556Srgrimes
1661556Srgrimes	sp = getservbyname(shell = "shell", "tcp");
1671556Srgrimes	if (sp == NULL)
1681556Srgrimes		errx(1, "%s/tcp: unknown service", shell);
1691556Srgrimes	port = sp->s_port;
1701556Srgrimes
1711556Srgrimes	if ((pwd = getpwuid(userid = getuid())) == NULL)
1721556Srgrimes		errx(1, "unknown user %d", (int)userid);
1731556Srgrimes
1741556Srgrimes	rem = STDIN_FILENO;		/* XXX */
1751556Srgrimes
1761556Srgrimes	if (fflag) {			/* Follow "protocol", send data. */
1771556Srgrimes		(void)response();
1781556Srgrimes		(void)setuid(userid);
1791556Srgrimes		source(argc, argv);
1801556Srgrimes		exit(errs);
1811556Srgrimes	}
1821556Srgrimes
1831556Srgrimes	if (tflag) {			/* Receive data. */
1841556Srgrimes		(void)setuid(userid);
1851556Srgrimes		sink(argc, argv);
1861556Srgrimes		exit(errs);
1871556Srgrimes	}
1881556Srgrimes
1891556Srgrimes	if (argc < 2)
1901556Srgrimes		usage();
1911556Srgrimes	if (argc > 2)
1921556Srgrimes		targetshouldbedirectory = 1;
1931556Srgrimes
1941556Srgrimes	rem = -1;
1951556Srgrimes	/* Command to be executed on remote system using "rsh". */
1961556Srgrimes	(void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
1971556Srgrimes	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
1981556Srgrimes	    targetshouldbedirectory ? " -d" : "");
1991556Srgrimes
2001556Srgrimes	(void)signal(SIGPIPE, lostconn);
2011556Srgrimes
2027165Sjoerg	if ((targ = colon(argv[argc - 1])))	/* Dest is remote host. */
2031556Srgrimes		toremote(targ, argc, argv);
2041556Srgrimes	else {
2051556Srgrimes		tolocal(argc, argv);		/* Dest is local host. */
2061556Srgrimes		if (targetshouldbedirectory)
2071556Srgrimes			verifydir(argv[argc - 1]);
2081556Srgrimes	}
2091556Srgrimes	exit(errs);
2101556Srgrimes}
2111556Srgrimes
2121556Srgrimesvoid
21390110Simptoremote(char *targ, int argc, char *argv[])
2141556Srgrimes{
2151556Srgrimes	int i, len, tos;
2161556Srgrimes	char *bp, *host, *src, *suser, *thost, *tuser;
2171556Srgrimes
2181556Srgrimes	*targ++ = 0;
2191556Srgrimes	if (*targ == 0)
2201556Srgrimes		targ = ".";
2211556Srgrimes
2227165Sjoerg	if ((thost = strchr(argv[argc - 1], '@'))) {
2231556Srgrimes		/* user@host */
2241556Srgrimes		*thost++ = 0;
2251556Srgrimes		tuser = argv[argc - 1];
2261556Srgrimes		if (*tuser == '\0')
2271556Srgrimes			tuser = NULL;
2281556Srgrimes		else if (!okname(tuser))
2291556Srgrimes			exit(1);
2301556Srgrimes	} else {
2311556Srgrimes		thost = argv[argc - 1];
2321556Srgrimes		tuser = NULL;
2331556Srgrimes	}
2341556Srgrimes
2351556Srgrimes	for (i = 0; i < argc - 1; i++) {
2361556Srgrimes		src = colon(argv[i]);
2371556Srgrimes		if (src) {			/* remote to remote */
2381556Srgrimes			*src++ = 0;
2391556Srgrimes			if (*src == 0)
2401556Srgrimes				src = ".";
2411556Srgrimes			host = strchr(argv[i], '@');
2421556Srgrimes			len = strlen(_PATH_RSH) + strlen(argv[i]) +
2431556Srgrimes			    strlen(src) + (tuser ? strlen(tuser) : 0) +
2441556Srgrimes			    strlen(thost) + strlen(targ) + CMDNEEDS + 20;
2451556Srgrimes			if (!(bp = malloc(len)))
24699744Sdillon				err(1, "malloc");
2471556Srgrimes			if (host) {
2481556Srgrimes				*host++ = 0;
2491556Srgrimes				suser = argv[i];
2501556Srgrimes				if (*suser == '\0')
2511556Srgrimes					suser = pwd->pw_name;
25277491Spirzyk				else if (!okname(suser)) {
25377491Spirzyk					++errs;
2541556Srgrimes					continue;
25577491Spirzyk				}
2561556Srgrimes				(void)snprintf(bp, len,
2571556Srgrimes				    "%s %s -l %s -n %s %s '%s%s%s:%s'",
2581556Srgrimes				    _PATH_RSH, host, suser, cmd, src,
2591556Srgrimes				    tuser ? tuser : "", tuser ? "@" : "",
2601556Srgrimes				    thost, targ);
2611556Srgrimes			} else
2621556Srgrimes				(void)snprintf(bp, len,
2631556Srgrimes				    "exec %s %s -n %s %s '%s%s%s:%s'",
2641556Srgrimes				    _PATH_RSH, argv[i], cmd, src,
2651556Srgrimes				    tuser ? tuser : "", tuser ? "@" : "",
2661556Srgrimes				    thost, targ);
2671556Srgrimes			(void)susystem(bp, userid);
2681556Srgrimes			(void)free(bp);
2691556Srgrimes		} else {			/* local to remote */
2701556Srgrimes			if (rem == -1) {
2711556Srgrimes				len = strlen(targ) + CMDNEEDS + 20;
2721556Srgrimes				if (!(bp = malloc(len)))
27399744Sdillon					err(1, "malloc");
2741556Srgrimes				(void)snprintf(bp, len, "%s -t %s", cmd, targ);
2751556Srgrimes				host = thost;
276105269Smarkm				rem = rcmd_af(&host, port,
277105269Smarkm				    pwd->pw_name,
278105269Smarkm				    tuser ? tuser : pwd->pw_name,
279105269Smarkm				    bp, 0, family);
2801556Srgrimes				if (rem < 0)
2811556Srgrimes					exit(1);
282101591Sume				if (family == PF_INET) {
283101591Sume					tos = IPTOS_THROUGHPUT;
284101591Sume					if (setsockopt(rem, IPPROTO_IP, IP_TOS,
285101591Sume					    &tos, sizeof(int)) < 0)
286101591Sume						warn("TOS (ignored)");
287101591Sume				}
2881556Srgrimes				if (response() < 0)
2891556Srgrimes					exit(1);
2901556Srgrimes				(void)free(bp);
2911556Srgrimes				(void)setuid(userid);
2921556Srgrimes			}
2931556Srgrimes			source(1, argv+i);
2941556Srgrimes		}
2951556Srgrimes	}
2961556Srgrimes}
2971556Srgrimes
2981556Srgrimesvoid
29990110Simptolocal(int argc, char *argv[])
3001556Srgrimes{
3011556Srgrimes	int i, len, tos;
3021556Srgrimes	char *bp, *host, *src, *suser;
3031556Srgrimes
3041556Srgrimes	for (i = 0; i < argc - 1; i++) {
3051556Srgrimes		if (!(src = colon(argv[i]))) {		/* Local to local. */
3061556Srgrimes			len = strlen(_PATH_CP) + strlen(argv[i]) +
3071556Srgrimes			    strlen(argv[argc - 1]) + 20;
3081556Srgrimes			if (!(bp = malloc(len)))
30999744Sdillon				err(1, "malloc");
3101556Srgrimes			(void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
31131633Swosch			    iamrecursive ? " -PR" : "", pflag ? " -p" : "",
3121556Srgrimes			    argv[i], argv[argc - 1]);
3131556Srgrimes			if (susystem(bp, userid))
3141556Srgrimes				++errs;
3151556Srgrimes			(void)free(bp);
3161556Srgrimes			continue;
3171556Srgrimes		}
3181556Srgrimes		*src++ = 0;
3191556Srgrimes		if (*src == 0)
3201556Srgrimes			src = ".";
3211556Srgrimes		if ((host = strchr(argv[i], '@')) == NULL) {
3221556Srgrimes			host = argv[i];
3231556Srgrimes			suser = pwd->pw_name;
3241556Srgrimes		} else {
3251556Srgrimes			*host++ = 0;
3261556Srgrimes			suser = argv[i];
3271556Srgrimes			if (*suser == '\0')
3281556Srgrimes				suser = pwd->pw_name;
32977491Spirzyk			else if (!okname(suser)) {
33077491Spirzyk				++errs;
3311556Srgrimes				continue;
33277491Spirzyk			}
3331556Srgrimes		}
3341556Srgrimes		len = strlen(src) + CMDNEEDS + 20;
3351556Srgrimes		if ((bp = malloc(len)) == NULL)
33699744Sdillon			err(1, "malloc");
3371556Srgrimes		(void)snprintf(bp, len, "%s -f %s", cmd, src);
338105269Smarkm		rem = rcmd_af(&host, port, pwd->pw_name, suser, bp, 0,
339101591Sume			    family);
3401556Srgrimes		(void)free(bp);
3411556Srgrimes		if (rem < 0) {
3421556Srgrimes			++errs;
3431556Srgrimes			continue;
3441556Srgrimes		}
3451556Srgrimes		(void)seteuid(userid);
346101591Sume		if (family == PF_INET) {
347101591Sume			tos = IPTOS_THROUGHPUT;
348101591Sume			if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos,
349101591Sume			    sizeof(int)) < 0)
350101591Sume				warn("TOS (ignored)");
351101591Sume		}
3521556Srgrimes		sink(1, argv + argc - 1);
3531556Srgrimes		(void)seteuid(0);
3541556Srgrimes		(void)close(rem);
3551556Srgrimes		rem = -1;
3561556Srgrimes	}
3571556Srgrimes}
3581556Srgrimes
3591556Srgrimesvoid
36090110Simpsource(int argc, char *argv[])
3611556Srgrimes{
3621556Srgrimes	struct stat stb;
3631556Srgrimes	static BUF buffer;
3641556Srgrimes	BUF *bp;
3651556Srgrimes	off_t i;
3661556Srgrimes	int amt, fd, haderr, indx, result;
3671556Srgrimes	char *last, *name, buf[BUFSIZ];
3681556Srgrimes
3691556Srgrimes	for (indx = 0; indx < argc; ++indx) {
37096196Sdes		name = argv[indx];
3711556Srgrimes		if ((fd = open(name, O_RDONLY, 0)) < 0)
3721556Srgrimes			goto syserr;
3731556Srgrimes		if (fstat(fd, &stb)) {
3741556Srgrimessyserr:			run_err("%s: %s", name, strerror(errno));
3751556Srgrimes			goto next;
3761556Srgrimes		}
3771556Srgrimes		switch (stb.st_mode & S_IFMT) {
3781556Srgrimes		case S_IFREG:
3791556Srgrimes			break;
3801556Srgrimes		case S_IFDIR:
3811556Srgrimes			if (iamrecursive) {
3821556Srgrimes				rsource(name, &stb);
3831556Srgrimes				goto next;
3841556Srgrimes			}
3851556Srgrimes			/* FALLTHROUGH */
3861556Srgrimes		default:
3871556Srgrimes			run_err("%s: not a regular file", name);
3881556Srgrimes			goto next;
3891556Srgrimes		}
3901556Srgrimes		if ((last = strrchr(name, '/')) == NULL)
3911556Srgrimes			last = name;
3921556Srgrimes		else
3931556Srgrimes			++last;
3941556Srgrimes		if (pflag) {
3951556Srgrimes			/*
3961556Srgrimes			 * Make it compatible with possible future
3971556Srgrimes			 * versions expecting microseconds.
3981556Srgrimes			 */
3991556Srgrimes			(void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
40038018Sbde			    (long)stb.st_mtimespec.tv_sec,
40138018Sbde			    (long)stb.st_atimespec.tv_sec);
4021556Srgrimes			(void)write(rem, buf, strlen(buf));
4031556Srgrimes			if (response() < 0)
4041556Srgrimes				goto next;
4051556Srgrimes		}
4061556Srgrimes#define	MODEMASK	(S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
407104549Stjr		(void)snprintf(buf, sizeof(buf), "C%04o %jd %s\n",
408104549Stjr		    stb.st_mode & MODEMASK, (intmax_t)stb.st_size, last);
4091556Srgrimes		(void)write(rem, buf, strlen(buf));
4101556Srgrimes		if (response() < 0)
4111556Srgrimes			goto next;
4121556Srgrimes		if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
4131556Srgrimesnext:			(void)close(fd);
4141556Srgrimes			continue;
4151556Srgrimes		}
4161556Srgrimes
4171556Srgrimes		/* Keep writing after an error so that we stay sync'd up. */
4181556Srgrimes		for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
4191556Srgrimes			amt = bp->cnt;
4201556Srgrimes			if (i + amt > stb.st_size)
4211556Srgrimes				amt = stb.st_size - i;
4221556Srgrimes			if (!haderr) {
4231556Srgrimes				result = read(fd, bp->buf, amt);
4241556Srgrimes				if (result != amt)
4251556Srgrimes					haderr = result >= 0 ? EIO : errno;
4261556Srgrimes			}
4271556Srgrimes			if (haderr)
4281556Srgrimes				(void)write(rem, bp->buf, amt);
4291556Srgrimes			else {
4301556Srgrimes				result = write(rem, bp->buf, amt);
4311556Srgrimes				if (result != amt)
4321556Srgrimes					haderr = result >= 0 ? EIO : errno;
4331556Srgrimes			}
4341556Srgrimes		}
4351556Srgrimes		if (close(fd) && !haderr)
4361556Srgrimes			haderr = errno;
4371556Srgrimes		if (!haderr)
4381556Srgrimes			(void)write(rem, "", 1);
4391556Srgrimes		else
4401556Srgrimes			run_err("%s: %s", name, strerror(haderr));
4411556Srgrimes		(void)response();
4421556Srgrimes	}
4431556Srgrimes}
4441556Srgrimes
4451556Srgrimesvoid
44690110Simprsource(char *name, struct stat *statp)
4471556Srgrimes{
4481556Srgrimes	DIR *dirp;
4491556Srgrimes	struct dirent *dp;
45077462Simp	char *last, *vect[1], path[PATH_MAX];
4511556Srgrimes
4521556Srgrimes	if (!(dirp = opendir(name))) {
4531556Srgrimes		run_err("%s: %s", name, strerror(errno));
4541556Srgrimes		return;
4551556Srgrimes	}
4561556Srgrimes	last = strrchr(name, '/');
4571556Srgrimes	if (last == 0)
4581556Srgrimes		last = name;
4591556Srgrimes	else
4601556Srgrimes		last++;
4611556Srgrimes	if (pflag) {
4621556Srgrimes		(void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
46338018Sbde		    (long)statp->st_mtimespec.tv_sec,
46438018Sbde		    (long)statp->st_atimespec.tv_sec);
4651556Srgrimes		(void)write(rem, path, strlen(path));
4661556Srgrimes		if (response() < 0) {
4671556Srgrimes			closedir(dirp);
4681556Srgrimes			return;
4691556Srgrimes		}
4701556Srgrimes	}
4711556Srgrimes	(void)snprintf(path, sizeof(path),
4721556Srgrimes	    "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
4731556Srgrimes	(void)write(rem, path, strlen(path));
4741556Srgrimes	if (response() < 0) {
4751556Srgrimes		closedir(dirp);
4761556Srgrimes		return;
4771556Srgrimes	}
4787165Sjoerg	while ((dp = readdir(dirp))) {
4791556Srgrimes		if (dp->d_ino == 0)
4801556Srgrimes			continue;
4811556Srgrimes		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
4821556Srgrimes			continue;
48377462Simp		if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
4841556Srgrimes			run_err("%s/%s: name too long", name, dp->d_name);
4851556Srgrimes			continue;
4861556Srgrimes		}
4871556Srgrimes		(void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
4881556Srgrimes		vect[0] = path;
4891556Srgrimes		source(1, vect);
4901556Srgrimes	}
4911556Srgrimes	(void)closedir(dirp);
4921556Srgrimes	(void)write(rem, "E\n", 2);
4931556Srgrimes	(void)response();
4941556Srgrimes}
4951556Srgrimes
4961556Srgrimesvoid
49790110Simpsink(int argc, char *argv[])
4981556Srgrimes{
4991556Srgrimes	static BUF buffer;
5001556Srgrimes	struct stat stb;
5011556Srgrimes	struct timeval tv[2];
5021556Srgrimes	enum { YES, NO, DISPLAYED } wrerr;
5031556Srgrimes	BUF *bp;
50446057Sdt	off_t i, j, size;
505114509Sobrien	int amt, exists, first, mask, mode, ofd, omode;
506114509Sobrien	size_t count;
50746057Sdt	int setimes, targisdir, wrerrno = 0;
508104559Scharnier	char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ], path[PATH_MAX];
5091556Srgrimes
5101556Srgrimes#define	atime	tv[0]
5111556Srgrimes#define	mtime	tv[1]
5121556Srgrimes#define	SCREWUP(str)	{ why = str; goto screwup; }
5131556Srgrimes
5141556Srgrimes	setimes = targisdir = 0;
5151556Srgrimes	mask = umask(0);
5161556Srgrimes	if (!pflag)
5171556Srgrimes		(void)umask(mask);
5181556Srgrimes	if (argc != 1) {
5191556Srgrimes		run_err("ambiguous target");
5201556Srgrimes		exit(1);
5211556Srgrimes	}
5221556Srgrimes	targ = *argv;
5231556Srgrimes	if (targetshouldbedirectory)
5241556Srgrimes		verifydir(targ);
5251556Srgrimes	(void)write(rem, "", 1);
5261556Srgrimes	if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
5271556Srgrimes		targisdir = 1;
5281556Srgrimes	for (first = 1;; first = 0) {
5291556Srgrimes		cp = buf;
5301556Srgrimes		if (read(rem, cp, 1) <= 0)
5311556Srgrimes			return;
5321556Srgrimes		if (*cp++ == '\n')
5331556Srgrimes			SCREWUP("unexpected <newline>");
5341556Srgrimes		do {
5351556Srgrimes			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
5361556Srgrimes				SCREWUP("lost connection");
5371556Srgrimes			*cp++ = ch;
5381556Srgrimes		} while (cp < &buf[BUFSIZ - 1] && ch != '\n');
5391556Srgrimes		*cp = 0;
5401556Srgrimes
5411556Srgrimes		if (buf[0] == '\01' || buf[0] == '\02') {
5421556Srgrimes			if (iamremote == 0)
5431556Srgrimes				(void)write(STDERR_FILENO,
5441556Srgrimes				    buf + 1, strlen(buf + 1));
5451556Srgrimes			if (buf[0] == '\02')
5461556Srgrimes				exit(1);
5471556Srgrimes			++errs;
5481556Srgrimes			continue;
5491556Srgrimes		}
5501556Srgrimes		if (buf[0] == 'E') {
5511556Srgrimes			(void)write(rem, "", 1);
5521556Srgrimes			return;
5531556Srgrimes		}
5541556Srgrimes
5551556Srgrimes		if (ch == '\n')
5561556Srgrimes			*--cp = 0;
5571556Srgrimes
5581556Srgrimes		cp = buf;
5591556Srgrimes		if (*cp == 'T') {
5601556Srgrimes			setimes++;
5611556Srgrimes			cp++;
56213978Spst			mtime.tv_sec = strtol(cp, &cp, 10);
56313978Spst			if (!cp || *cp++ != ' ')
5641556Srgrimes				SCREWUP("mtime.sec not delimited");
56513978Spst			mtime.tv_usec = strtol(cp, &cp, 10);
56613978Spst			if (!cp || *cp++ != ' ')
5671556Srgrimes				SCREWUP("mtime.usec not delimited");
56813978Spst			atime.tv_sec = strtol(cp, &cp, 10);
56913978Spst			if (!cp || *cp++ != ' ')
5701556Srgrimes				SCREWUP("atime.sec not delimited");
57113978Spst			atime.tv_usec = strtol(cp, &cp, 10);
57213978Spst			if (!cp || *cp++ != '\0')
5731556Srgrimes				SCREWUP("atime.usec not delimited");
5741556Srgrimes			(void)write(rem, "", 1);
5751556Srgrimes			continue;
5761556Srgrimes		}
5771556Srgrimes		if (*cp != 'C' && *cp != 'D') {
5781556Srgrimes			/*
5791556Srgrimes			 * Check for the case "rcp remote:foo\* local:bar".
5801556Srgrimes			 * In this case, the line "No match." can be returned
5811556Srgrimes			 * by the shell before the rcp command on the remote is
5821556Srgrimes			 * executed so the ^Aerror_message convention isn't
5831556Srgrimes			 * followed.
5841556Srgrimes			 */
5851556Srgrimes			if (first) {
5861556Srgrimes				run_err("%s", cp);
5871556Srgrimes				exit(1);
5881556Srgrimes			}
5891556Srgrimes			SCREWUP("expected control record");
5901556Srgrimes		}
5911556Srgrimes		mode = 0;
5921556Srgrimes		for (++cp; cp < buf + 5; cp++) {
5931556Srgrimes			if (*cp < '0' || *cp > '7')
5941556Srgrimes				SCREWUP("bad mode");
5951556Srgrimes			mode = (mode << 3) | (*cp - '0');
5961556Srgrimes		}
5971556Srgrimes		if (*cp++ != ' ')
5981556Srgrimes			SCREWUP("mode not delimited");
5991556Srgrimes
6001556Srgrimes		for (size = 0; isdigit(*cp);)
6011556Srgrimes			size = size * 10 + (*cp++ - '0');
6021556Srgrimes		if (*cp++ != ' ')
6031556Srgrimes			SCREWUP("size not delimited");
6041556Srgrimes		if (targisdir) {
605104559Scharnier			if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp)
606104559Scharnier					>= sizeof(path)) {
607104559Scharnier				run_err("%s%s%s: name too long", targ,
608104559Scharnier					*targ ? "/" : "", cp);
609104559Scharnier				exit(1);
6101556Srgrimes			}
611104559Scharnier			(void)snprintf(path, sizeof(path), "%s%s%s", targ,
6121556Srgrimes			    *targ ? "/" : "", cp);
613104559Scharnier			np = path;
6141556Srgrimes		} else
6151556Srgrimes			np = targ;
6161556Srgrimes		exists = stat(np, &stb) == 0;
6171556Srgrimes		if (buf[0] == 'D') {
6181556Srgrimes			int mod_flag = pflag;
6191556Srgrimes			if (exists) {
6201556Srgrimes				if (!S_ISDIR(stb.st_mode)) {
6211556Srgrimes					errno = ENOTDIR;
6221556Srgrimes					goto bad;
6231556Srgrimes				}
6241556Srgrimes				if (pflag)
6251556Srgrimes					(void)chmod(np, mode);
6261556Srgrimes			} else {
6271556Srgrimes				/* Handle copying from a read-only directory */
6281556Srgrimes				mod_flag = 1;
6291556Srgrimes				if (mkdir(np, mode | S_IRWXU) < 0)
6301556Srgrimes					goto bad;
6311556Srgrimes			}
6321556Srgrimes			vect[0] = np;
6331556Srgrimes			sink(1, vect);
6341556Srgrimes			if (setimes) {
6351556Srgrimes				setimes = 0;
6361556Srgrimes				if (utimes(np, tv) < 0)
6371556Srgrimes				    run_err("%s: set times: %s",
6381556Srgrimes					np, strerror(errno));
6391556Srgrimes			}
6401556Srgrimes			if (mod_flag)
6411556Srgrimes				(void)chmod(np, mode);
6421556Srgrimes			continue;
6431556Srgrimes		}
6441556Srgrimes		omode = mode;
6451556Srgrimes		mode |= S_IWRITE;
6461556Srgrimes		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
6471556Srgrimesbad:			run_err("%s: %s", np, strerror(errno));
6481556Srgrimes			continue;
6491556Srgrimes		}
6501556Srgrimes		(void)write(rem, "", 1);
6511556Srgrimes		if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
6521556Srgrimes			(void)close(ofd);
6531556Srgrimes			continue;
6541556Srgrimes		}
6551556Srgrimes		cp = bp->buf;
6561556Srgrimes		wrerr = NO;
6571556Srgrimes		for (count = i = 0; i < size; i += BUFSIZ) {
6581556Srgrimes			amt = BUFSIZ;
6591556Srgrimes			if (i + amt > size)
6601556Srgrimes				amt = size - i;
6611556Srgrimes			count += amt;
6621556Srgrimes			do {
6631556Srgrimes				j = read(rem, cp, amt);
6641556Srgrimes				if (j <= 0) {
6651556Srgrimes					run_err("%s", j ? strerror(errno) :
6661556Srgrimes					    "dropped connection");
6671556Srgrimes					exit(1);
6681556Srgrimes				}
6691556Srgrimes				amt -= j;
6701556Srgrimes				cp += j;
6711556Srgrimes			} while (amt > 0);
6721556Srgrimes			if (count == bp->cnt) {
6731556Srgrimes				/* Keep reading so we stay sync'd up. */
6741556Srgrimes				if (wrerr == NO) {
6751556Srgrimes					j = write(ofd, bp->buf, count);
6761556Srgrimes					if (j != count) {
6771556Srgrimes						wrerr = YES;
6788855Srgrimes						wrerrno = j >= 0 ? EIO : errno;
6791556Srgrimes					}
6801556Srgrimes				}
6811556Srgrimes				count = 0;
6821556Srgrimes				cp = bp->buf;
6831556Srgrimes			}
6841556Srgrimes		}
6851556Srgrimes		if (count != 0 && wrerr == NO &&
6861556Srgrimes		    (j = write(ofd, bp->buf, count)) != count) {
6871556Srgrimes			wrerr = YES;
6888855Srgrimes			wrerrno = j >= 0 ? EIO : errno;
6891556Srgrimes		}
6901556Srgrimes		if (ftruncate(ofd, size)) {
6911556Srgrimes			run_err("%s: truncate: %s", np, strerror(errno));
6921556Srgrimes			wrerr = DISPLAYED;
6931556Srgrimes		}
6941556Srgrimes		if (pflag) {
6951556Srgrimes			if (exists || omode != mode)
6961556Srgrimes				if (fchmod(ofd, omode))
6971556Srgrimes					run_err("%s: set mode: %s",
6981556Srgrimes					    np, strerror(errno));
6991556Srgrimes		} else {
7001556Srgrimes			if (!exists && omode != mode)
7011556Srgrimes				if (fchmod(ofd, omode & ~mask))
7021556Srgrimes					run_err("%s: set mode: %s",
7031556Srgrimes					    np, strerror(errno));
7041556Srgrimes		}
7051556Srgrimes		(void)close(ofd);
7061556Srgrimes		(void)response();
7071556Srgrimes		if (setimes && wrerr == NO) {
7081556Srgrimes			setimes = 0;
7091556Srgrimes			if (utimes(np, tv) < 0) {
7101556Srgrimes				run_err("%s: set times: %s",
7111556Srgrimes				    np, strerror(errno));
7121556Srgrimes				wrerr = DISPLAYED;
7131556Srgrimes			}
7141556Srgrimes		}
7151556Srgrimes		switch(wrerr) {
7161556Srgrimes		case YES:
7171556Srgrimes			run_err("%s: %s", np, strerror(wrerrno));
7181556Srgrimes			break;
7191556Srgrimes		case NO:
7201556Srgrimes			(void)write(rem, "", 1);
7211556Srgrimes			break;
7221556Srgrimes		case DISPLAYED:
7231556Srgrimes			break;
7241556Srgrimes		}
7251556Srgrimes	}
7261556Srgrimesscrewup:
7271556Srgrimes	run_err("protocol error: %s", why);
7281556Srgrimes	exit(1);
7291556Srgrimes}
7301556Srgrimes
7311556Srgrimesint
73290110Simpresponse(void)
7331556Srgrimes{
7341556Srgrimes	char ch, *cp, resp, rbuf[BUFSIZ];
7351556Srgrimes
7361556Srgrimes	if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
7371556Srgrimes		lostconn(0);
7381556Srgrimes
7391556Srgrimes	cp = rbuf;
7401556Srgrimes	switch(resp) {
7411556Srgrimes	case 0:				/* ok */
7421556Srgrimes		return (0);
7431556Srgrimes	default:
7441556Srgrimes		*cp++ = resp;
7451556Srgrimes		/* FALLTHROUGH */
7461556Srgrimes	case 1:				/* error, followed by error msg */
7471556Srgrimes	case 2:				/* fatal error, "" */
7481556Srgrimes		do {
7491556Srgrimes			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
7501556Srgrimes				lostconn(0);
7511556Srgrimes			*cp++ = ch;
7521556Srgrimes		} while (cp < &rbuf[BUFSIZ] && ch != '\n');
7531556Srgrimes
7541556Srgrimes		if (!iamremote)
7551556Srgrimes			(void)write(STDERR_FILENO, rbuf, cp - rbuf);
7561556Srgrimes		++errs;
7571556Srgrimes		if (resp == 1)
7581556Srgrimes			return (-1);
7591556Srgrimes		exit(1);
7601556Srgrimes	}
7611556Srgrimes	/* NOTREACHED */
7621556Srgrimes}
7631556Srgrimes
7641556Srgrimesvoid
76590110Simpusage(void)
7661556Srgrimes{
76726466Scharnier	(void)fprintf(stderr, "%s\n%s\n",
768101591Sume	    "usage: rcp [-46p] f1 f2",
769101591Sume	    "       rcp [-46pr] f1 ... fn directory");
7701556Srgrimes	exit(1);
7711556Srgrimes}
7721556Srgrimes
7731556Srgrimes#include <stdarg.h>
7741556Srgrimes
7751556Srgrimesvoid
7761556Srgrimesrun_err(const char *fmt, ...)
7771556Srgrimes{
7781556Srgrimes	static FILE *fp;
7791556Srgrimes	va_list ap;
7801556Srgrimes
7811556Srgrimes	++errs;
7821556Srgrimes	if (fp == NULL && !(fp = fdopen(rem, "w")))
7831556Srgrimes		return;
7841556Srgrimes	(void)fprintf(fp, "%c", 0x01);
7851556Srgrimes	(void)fprintf(fp, "rcp: ");
786104563Stjr	va_start(ap, fmt);
7871556Srgrimes	(void)vfprintf(fp, fmt, ap);
788104563Stjr	va_end(ap);
7891556Srgrimes	(void)fprintf(fp, "\n");
7901556Srgrimes	(void)fflush(fp);
7911556Srgrimes
792104563Stjr	if (!iamremote) {
793104563Stjr		va_start(ap, fmt);
7941556Srgrimes		vwarnx(fmt, ap);
795104563Stjr		va_end(ap);
796104563Stjr	}
7971556Srgrimes
7981556Srgrimes	va_end(ap);
7991556Srgrimes}
800