rcp.c revision 99744
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
411556Srgrimes#ifndef lint
4220420Sstevestatic char const copyright[] =
431556Srgrimes"@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
441556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
451556Srgrimes#endif /* not lint */
461556Srgrimes
471556Srgrimes#ifndef lint
4836149Scharnier#if 0
4936149Scharnierstatic char sccsid[] = "@(#)rcp.c	8.2 (Berkeley) 4/2/94";
5036149Scharnier#endif
511556Srgrimes#endif /* not lint */
5299110Sobrien#include <sys/cdefs.h>
5399110Sobrien__FBSDID("$FreeBSD: head/bin/rcp/rcp.c 99744 2002-07-10 20:44:55Z dillon $");
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>
741556Srgrimes#include <stdio.h>
751556Srgrimes#include <stdlib.h>
761556Srgrimes#include <string.h>
771556Srgrimes#include <string.h>
781556Srgrimes#include <unistd.h>
791556Srgrimes
801556Srgrimes#include "extern.h"
811556Srgrimes
821556Srgrimes#ifdef KERBEROS
8357454Smarkm#include <openssl/des.h>
8429914Smarkm#include <krb.h>
8551434Smarkm#include "bsd_locl.h"
861556Srgrimes
871556Srgrimeschar	dst_realm_buf[REALM_SZ];
881556Srgrimeschar	*dest_realm = NULL;
891556Srgrimesint	use_kerberos = 1;
9096196SdesCREDENTIALS	cred;
911556SrgrimesKey_schedule	schedule;
921556Srgrimesextern	char	*krb_realmofhost();
931556Srgrimes#ifdef CRYPT
941556Srgrimesint	doencrypt = 0;
951556Srgrimes#define	OPTIONS	"dfKk:prtx"
961556Srgrimes#else
971556Srgrimes#define	OPTIONS	"dfKk:prt"
981556Srgrimes#endif
991556Srgrimes#else
1001556Srgrimes#define	OPTIONS "dfprt"
1011556Srgrimes#endif
1021556Srgrimes
1031556Srgrimesstruct passwd *pwd;
1041556Srgrimesu_short	port;
1051556Srgrimesuid_t	userid;
1061556Srgrimesint errs, rem;
1071556Srgrimesint pflag, iamremote, iamrecursive, targetshouldbedirectory;
1081556Srgrimes
10934898Smarkmstatic int argc_copy;
11034898Smarkmstatic char **argv_copy;
11134898Smarkm
1121556Srgrimes#define	CMDNEEDS	64
1131556Srgrimeschar cmd[CMDNEEDS];		/* must hold "rcp -r -p -d\0" */
1141556Srgrimes
1151556Srgrimes#ifdef KERBEROS
11690110Simpint	 kerberos(char **, char *, char *, char *);
11790110Simpvoid	 oldw(const char *, ...) __printflike(1, 2);
1181556Srgrimes#endif
11990110Simpint	 response(void);
12090110Simpvoid	 rsource(char *, struct stat *);
12190110Simpvoid	 run_err(const char *, ...) __printflike(1, 2);
12290110Simpvoid	 sink(int, char *[]);
12390110Simpvoid	 source(int, char *[]);
12490110Simpvoid	 tolocal(int, char *[]);
12590110Simpvoid	 toremote(char *, int, char *[]);
12690110Simpvoid	 usage(void);
1271556Srgrimes
1281556Srgrimesint
12990110Simpmain(int argc, char *argv[])
1301556Srgrimes{
1311556Srgrimes	struct servent *sp;
13248560Sbde	int ch, fflag, i, tflag;
1331556Srgrimes	char *targ, *shell;
13440101Smarkm#ifdef KERBEROS
13540101Smarkm	char *k;
13640101Smarkm#endif
1371556Srgrimes
13834898Smarkm	/*
13934898Smarkm	 * Prepare for execing ourselves.
14034898Smarkm	 */
14134898Smarkm	argc_copy = argc + 1;
14234898Smarkm	argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy));
14334898Smarkm	if (argv_copy == NULL)
14434898Smarkm		err(1, "malloc");
14534898Smarkm	argv_copy[0] = argv[0];
14634898Smarkm	argv_copy[1] = "-K";
14748560Sbde	for (i = 1; i < argc; ++i) {
14834898Smarkm		argv_copy[i + 1] = strdup(argv[i]);
14934898Smarkm		if (argv_copy[i + 1] == NULL)
15034898Smarkm			errx(1, "strdup: out of memory");
15134898Smarkm	}
15234898Smarkm	argv_copy[argc + 1] = NULL;
15334898Smarkm
1541556Srgrimes	fflag = tflag = 0;
15524348Simp	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
1561556Srgrimes		switch(ch) {			/* User-visible flags. */
1571556Srgrimes		case 'K':
1581556Srgrimes#ifdef KERBEROS
1591556Srgrimes			use_kerberos = 0;
1601556Srgrimes#endif
1611556Srgrimes			break;
1621556Srgrimes#ifdef	KERBEROS
1631556Srgrimes		case 'k':
1641556Srgrimes			dest_realm = dst_realm_buf;
16525612Sjoerg			(void)strncpy(dst_realm_buf, optarg, REALM_SZ - 1);
16625612Sjoerg			dst_realm_buf[REALM_SZ - 1] = '\0';
1671556Srgrimes			break;
1681556Srgrimes#ifdef CRYPT
1691556Srgrimes		case 'x':
1701556Srgrimes			doencrypt = 1;
1711556Srgrimes			/* des_set_key(cred.session, schedule); */
1721556Srgrimes			break;
1731556Srgrimes#endif
1741556Srgrimes#endif
1751556Srgrimes		case 'p':
1761556Srgrimes			pflag = 1;
1771556Srgrimes			break;
1781556Srgrimes		case 'r':
1791556Srgrimes			iamrecursive = 1;
1801556Srgrimes			break;
1811556Srgrimes						/* Server options. */
1821556Srgrimes		case 'd':
1831556Srgrimes			targetshouldbedirectory = 1;
1841556Srgrimes			break;
1851556Srgrimes		case 'f':			/* "from" */
1861556Srgrimes			iamremote = 1;
1871556Srgrimes			fflag = 1;
1881556Srgrimes			break;
1891556Srgrimes		case 't':			/* "to" */
1901556Srgrimes			iamremote = 1;
1911556Srgrimes			tflag = 1;
1921556Srgrimes			break;
1931556Srgrimes		case '?':
1941556Srgrimes		default:
1951556Srgrimes			usage();
1961556Srgrimes		}
1971556Srgrimes	argc -= optind;
1981556Srgrimes	argv += optind;
1991556Srgrimes
2001556Srgrimes#ifdef KERBEROS
20140101Smarkm	k = auth_getval("auth_list");
20240101Smarkm	if (k && !strstr(k, "kerberos"))
20340101Smarkm	    use_kerberos = 0;
2041556Srgrimes	if (use_kerberos) {
2051556Srgrimes#ifdef CRYPT
2061556Srgrimes		shell = doencrypt ? "ekshell" : "kshell";
2071556Srgrimes#else
2081556Srgrimes		shell = "kshell";
2091556Srgrimes#endif
2101556Srgrimes		if ((sp = getservbyname(shell, "tcp")) == NULL) {
2111556Srgrimes			use_kerberos = 0;
2121556Srgrimes			oldw("can't get entry for %s/tcp service", shell);
2131556Srgrimes			sp = getservbyname(shell = "shell", "tcp");
2141556Srgrimes		}
2151556Srgrimes	} else
2161556Srgrimes		sp = getservbyname(shell = "shell", "tcp");
2171556Srgrimes#else
2181556Srgrimes	sp = getservbyname(shell = "shell", "tcp");
2191556Srgrimes#endif
2201556Srgrimes	if (sp == NULL)
2211556Srgrimes		errx(1, "%s/tcp: unknown service", shell);
2221556Srgrimes	port = sp->s_port;
2231556Srgrimes
2241556Srgrimes	if ((pwd = getpwuid(userid = getuid())) == NULL)
2251556Srgrimes		errx(1, "unknown user %d", (int)userid);
2261556Srgrimes
2271556Srgrimes	rem = STDIN_FILENO;		/* XXX */
2281556Srgrimes
2291556Srgrimes	if (fflag) {			/* Follow "protocol", send data. */
2301556Srgrimes		(void)response();
2311556Srgrimes		(void)setuid(userid);
2321556Srgrimes		source(argc, argv);
2331556Srgrimes		exit(errs);
2341556Srgrimes	}
2351556Srgrimes
2361556Srgrimes	if (tflag) {			/* Receive data. */
2371556Srgrimes		(void)setuid(userid);
2381556Srgrimes		sink(argc, argv);
2391556Srgrimes		exit(errs);
2401556Srgrimes	}
2411556Srgrimes
2421556Srgrimes	if (argc < 2)
2431556Srgrimes		usage();
2441556Srgrimes	if (argc > 2)
2451556Srgrimes		targetshouldbedirectory = 1;
2461556Srgrimes
2471556Srgrimes	rem = -1;
2481556Srgrimes	/* Command to be executed on remote system using "rsh". */
2491556Srgrimes#ifdef	KERBEROS
2501556Srgrimes	(void)snprintf(cmd, sizeof(cmd),
2511556Srgrimes	    "rcp%s%s%s%s", iamrecursive ? " -r" : "",
2521556Srgrimes#ifdef CRYPT
2531556Srgrimes	    (doencrypt && use_kerberos ? " -x" : ""),
2541556Srgrimes#else
2551556Srgrimes	    "",
2561556Srgrimes#endif
2571556Srgrimes	    pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
2581556Srgrimes#else
2591556Srgrimes	(void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
2601556Srgrimes	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
2611556Srgrimes	    targetshouldbedirectory ? " -d" : "");
2621556Srgrimes#endif
2631556Srgrimes
2641556Srgrimes	(void)signal(SIGPIPE, lostconn);
2651556Srgrimes
2667165Sjoerg	if ((targ = colon(argv[argc - 1])))	/* Dest is remote host. */
2671556Srgrimes		toremote(targ, argc, argv);
2681556Srgrimes	else {
2691556Srgrimes		tolocal(argc, argv);		/* Dest is local host. */
2701556Srgrimes		if (targetshouldbedirectory)
2711556Srgrimes			verifydir(argv[argc - 1]);
2721556Srgrimes	}
2731556Srgrimes	exit(errs);
2741556Srgrimes}
2751556Srgrimes
2761556Srgrimesvoid
27790110Simptoremote(char *targ, int argc, char *argv[])
2781556Srgrimes{
2791556Srgrimes	int i, len, tos;
2801556Srgrimes	char *bp, *host, *src, *suser, *thost, *tuser;
2811556Srgrimes
2821556Srgrimes	*targ++ = 0;
2831556Srgrimes	if (*targ == 0)
2841556Srgrimes		targ = ".";
2851556Srgrimes
2867165Sjoerg	if ((thost = strchr(argv[argc - 1], '@'))) {
2871556Srgrimes		/* user@host */
2881556Srgrimes		*thost++ = 0;
2891556Srgrimes		tuser = argv[argc - 1];
2901556Srgrimes		if (*tuser == '\0')
2911556Srgrimes			tuser = NULL;
2921556Srgrimes		else if (!okname(tuser))
2931556Srgrimes			exit(1);
2941556Srgrimes	} else {
2951556Srgrimes		thost = argv[argc - 1];
2961556Srgrimes		tuser = NULL;
2971556Srgrimes	}
2981556Srgrimes
2991556Srgrimes	for (i = 0; i < argc - 1; i++) {
3001556Srgrimes		src = colon(argv[i]);
3011556Srgrimes		if (src) {			/* remote to remote */
3021556Srgrimes			*src++ = 0;
3031556Srgrimes			if (*src == 0)
3041556Srgrimes				src = ".";
3051556Srgrimes			host = strchr(argv[i], '@');
3061556Srgrimes			len = strlen(_PATH_RSH) + strlen(argv[i]) +
3071556Srgrimes			    strlen(src) + (tuser ? strlen(tuser) : 0) +
3081556Srgrimes			    strlen(thost) + strlen(targ) + CMDNEEDS + 20;
3091556Srgrimes			if (!(bp = malloc(len)))
31099744Sdillon				err(1, "malloc");
3111556Srgrimes			if (host) {
3121556Srgrimes				*host++ = 0;
3131556Srgrimes				suser = argv[i];
3141556Srgrimes				if (*suser == '\0')
3151556Srgrimes					suser = pwd->pw_name;
31677491Spirzyk				else if (!okname(suser)) {
31777491Spirzyk					++errs;
3181556Srgrimes					continue;
31977491Spirzyk				}
3201556Srgrimes				(void)snprintf(bp, len,
3211556Srgrimes				    "%s %s -l %s -n %s %s '%s%s%s:%s'",
3221556Srgrimes				    _PATH_RSH, host, suser, cmd, src,
3231556Srgrimes				    tuser ? tuser : "", tuser ? "@" : "",
3241556Srgrimes				    thost, targ);
3251556Srgrimes			} else
3261556Srgrimes				(void)snprintf(bp, len,
3271556Srgrimes				    "exec %s %s -n %s %s '%s%s%s:%s'",
3281556Srgrimes				    _PATH_RSH, argv[i], cmd, src,
3291556Srgrimes				    tuser ? tuser : "", tuser ? "@" : "",
3301556Srgrimes				    thost, targ);
3311556Srgrimes			(void)susystem(bp, userid);
3321556Srgrimes			(void)free(bp);
3331556Srgrimes		} else {			/* local to remote */
3341556Srgrimes			if (rem == -1) {
3351556Srgrimes				len = strlen(targ) + CMDNEEDS + 20;
3361556Srgrimes				if (!(bp = malloc(len)))
33799744Sdillon					err(1, "malloc");
3381556Srgrimes				(void)snprintf(bp, len, "%s -t %s", cmd, targ);
3391556Srgrimes				host = thost;
3401556Srgrimes#ifdef KERBEROS
3411556Srgrimes				if (use_kerberos)
3421556Srgrimes					rem = kerberos(&host, bp,
3431556Srgrimes					    pwd->pw_name,
3441556Srgrimes					    tuser ? tuser : pwd->pw_name);
3451556Srgrimes				else
3461556Srgrimes#endif
3471556Srgrimes					rem = rcmd(&host, port, pwd->pw_name,
3481556Srgrimes					    tuser ? tuser : pwd->pw_name,
3491556Srgrimes					    bp, 0);
3501556Srgrimes				if (rem < 0)
3511556Srgrimes					exit(1);
3521556Srgrimes				tos = IPTOS_THROUGHPUT;
3531556Srgrimes				if (setsockopt(rem, IPPROTO_IP, IP_TOS,
3541556Srgrimes				    &tos, sizeof(int)) < 0)
3551556Srgrimes					warn("TOS (ignored)");
3561556Srgrimes				if (response() < 0)
3571556Srgrimes					exit(1);
3581556Srgrimes				(void)free(bp);
3591556Srgrimes				(void)setuid(userid);
3601556Srgrimes			}
3611556Srgrimes			source(1, argv+i);
3621556Srgrimes		}
3631556Srgrimes	}
3641556Srgrimes}
3651556Srgrimes
3661556Srgrimesvoid
36790110Simptolocal(int argc, char *argv[])
3681556Srgrimes{
3691556Srgrimes	int i, len, tos;
3701556Srgrimes	char *bp, *host, *src, *suser;
3711556Srgrimes
3721556Srgrimes	for (i = 0; i < argc - 1; i++) {
3731556Srgrimes		if (!(src = colon(argv[i]))) {		/* Local to local. */
3741556Srgrimes			len = strlen(_PATH_CP) + strlen(argv[i]) +
3751556Srgrimes			    strlen(argv[argc - 1]) + 20;
3761556Srgrimes			if (!(bp = malloc(len)))
37799744Sdillon				err(1, "malloc");
3781556Srgrimes			(void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
37931633Swosch			    iamrecursive ? " -PR" : "", pflag ? " -p" : "",
3801556Srgrimes			    argv[i], argv[argc - 1]);
3811556Srgrimes			if (susystem(bp, userid))
3821556Srgrimes				++errs;
3831556Srgrimes			(void)free(bp);
3841556Srgrimes			continue;
3851556Srgrimes		}
3861556Srgrimes		*src++ = 0;
3871556Srgrimes		if (*src == 0)
3881556Srgrimes			src = ".";
3891556Srgrimes		if ((host = strchr(argv[i], '@')) == NULL) {
3901556Srgrimes			host = argv[i];
3911556Srgrimes			suser = pwd->pw_name;
3921556Srgrimes		} else {
3931556Srgrimes			*host++ = 0;
3941556Srgrimes			suser = argv[i];
3951556Srgrimes			if (*suser == '\0')
3961556Srgrimes				suser = pwd->pw_name;
39777491Spirzyk			else if (!okname(suser)) {
39877491Spirzyk				++errs;
3991556Srgrimes				continue;
40077491Spirzyk			}
4011556Srgrimes		}
4021556Srgrimes		len = strlen(src) + CMDNEEDS + 20;
4031556Srgrimes		if ((bp = malloc(len)) == NULL)
40499744Sdillon			err(1, "malloc");
4051556Srgrimes		(void)snprintf(bp, len, "%s -f %s", cmd, src);
4068855Srgrimes		rem =
4071556Srgrimes#ifdef KERBEROS
4088855Srgrimes		    use_kerberos ?
4098855Srgrimes			kerberos(&host, bp, pwd->pw_name, suser) :
4101556Srgrimes#endif
4111556Srgrimes			rcmd(&host, port, pwd->pw_name, suser, bp, 0);
4121556Srgrimes		(void)free(bp);
4131556Srgrimes		if (rem < 0) {
4141556Srgrimes			++errs;
4151556Srgrimes			continue;
4161556Srgrimes		}
4171556Srgrimes		(void)seteuid(userid);
4181556Srgrimes		tos = IPTOS_THROUGHPUT;
4191556Srgrimes		if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
4201556Srgrimes			warn("TOS (ignored)");
4211556Srgrimes		sink(1, argv + argc - 1);
4221556Srgrimes		(void)seteuid(0);
4231556Srgrimes		(void)close(rem);
4241556Srgrimes		rem = -1;
4251556Srgrimes	}
4261556Srgrimes}
4271556Srgrimes
4281556Srgrimesvoid
42990110Simpsource(int argc, char *argv[])
4301556Srgrimes{
4311556Srgrimes	struct stat stb;
4321556Srgrimes	static BUF buffer;
4331556Srgrimes	BUF *bp;
4341556Srgrimes	off_t i;
4351556Srgrimes	int amt, fd, haderr, indx, result;
4361556Srgrimes	char *last, *name, buf[BUFSIZ];
4371556Srgrimes
4381556Srgrimes	for (indx = 0; indx < argc; ++indx) {
43996196Sdes		name = argv[indx];
4401556Srgrimes		if ((fd = open(name, O_RDONLY, 0)) < 0)
4411556Srgrimes			goto syserr;
4421556Srgrimes		if (fstat(fd, &stb)) {
4431556Srgrimessyserr:			run_err("%s: %s", name, strerror(errno));
4441556Srgrimes			goto next;
4451556Srgrimes		}
4461556Srgrimes		switch (stb.st_mode & S_IFMT) {
4471556Srgrimes		case S_IFREG:
4481556Srgrimes			break;
4491556Srgrimes		case S_IFDIR:
4501556Srgrimes			if (iamrecursive) {
4511556Srgrimes				rsource(name, &stb);
4521556Srgrimes				goto next;
4531556Srgrimes			}
4541556Srgrimes			/* FALLTHROUGH */
4551556Srgrimes		default:
4561556Srgrimes			run_err("%s: not a regular file", name);
4571556Srgrimes			goto next;
4581556Srgrimes		}
4591556Srgrimes		if ((last = strrchr(name, '/')) == NULL)
4601556Srgrimes			last = name;
4611556Srgrimes		else
4621556Srgrimes			++last;
4631556Srgrimes		if (pflag) {
4641556Srgrimes			/*
4651556Srgrimes			 * Make it compatible with possible future
4661556Srgrimes			 * versions expecting microseconds.
4671556Srgrimes			 */
4681556Srgrimes			(void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
46938018Sbde			    (long)stb.st_mtimespec.tv_sec,
47038018Sbde			    (long)stb.st_atimespec.tv_sec);
4711556Srgrimes			(void)write(rem, buf, strlen(buf));
4721556Srgrimes			if (response() < 0)
4731556Srgrimes				goto next;
4741556Srgrimes		}
4751556Srgrimes#define	MODEMASK	(S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
4761556Srgrimes		(void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
4771556Srgrimes		    stb.st_mode & MODEMASK, stb.st_size, last);
4781556Srgrimes		(void)write(rem, buf, strlen(buf));
4791556Srgrimes		if (response() < 0)
4801556Srgrimes			goto next;
4811556Srgrimes		if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
4821556Srgrimesnext:			(void)close(fd);
4831556Srgrimes			continue;
4841556Srgrimes		}
4851556Srgrimes
4861556Srgrimes		/* Keep writing after an error so that we stay sync'd up. */
4871556Srgrimes		for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
4881556Srgrimes			amt = bp->cnt;
4891556Srgrimes			if (i + amt > stb.st_size)
4901556Srgrimes				amt = stb.st_size - i;
4911556Srgrimes			if (!haderr) {
4921556Srgrimes				result = read(fd, bp->buf, amt);
4931556Srgrimes				if (result != amt)
4941556Srgrimes					haderr = result >= 0 ? EIO : errno;
4951556Srgrimes			}
4961556Srgrimes			if (haderr)
4971556Srgrimes				(void)write(rem, bp->buf, amt);
4981556Srgrimes			else {
4991556Srgrimes				result = write(rem, bp->buf, amt);
5001556Srgrimes				if (result != amt)
5011556Srgrimes					haderr = result >= 0 ? EIO : errno;
5021556Srgrimes			}
5031556Srgrimes		}
5041556Srgrimes		if (close(fd) && !haderr)
5051556Srgrimes			haderr = errno;
5061556Srgrimes		if (!haderr)
5071556Srgrimes			(void)write(rem, "", 1);
5081556Srgrimes		else
5091556Srgrimes			run_err("%s: %s", name, strerror(haderr));
5101556Srgrimes		(void)response();
5111556Srgrimes	}
5121556Srgrimes}
5131556Srgrimes
5141556Srgrimesvoid
51590110Simprsource(char *name, struct stat *statp)
5161556Srgrimes{
5171556Srgrimes	DIR *dirp;
5181556Srgrimes	struct dirent *dp;
51977462Simp	char *last, *vect[1], path[PATH_MAX];
5201556Srgrimes
5211556Srgrimes	if (!(dirp = opendir(name))) {
5221556Srgrimes		run_err("%s: %s", name, strerror(errno));
5231556Srgrimes		return;
5241556Srgrimes	}
5251556Srgrimes	last = strrchr(name, '/');
5261556Srgrimes	if (last == 0)
5271556Srgrimes		last = name;
5281556Srgrimes	else
5291556Srgrimes		last++;
5301556Srgrimes	if (pflag) {
5311556Srgrimes		(void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
53238018Sbde		    (long)statp->st_mtimespec.tv_sec,
53338018Sbde		    (long)statp->st_atimespec.tv_sec);
5341556Srgrimes		(void)write(rem, path, strlen(path));
5351556Srgrimes		if (response() < 0) {
5361556Srgrimes			closedir(dirp);
5371556Srgrimes			return;
5381556Srgrimes		}
5391556Srgrimes	}
5401556Srgrimes	(void)snprintf(path, sizeof(path),
5411556Srgrimes	    "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
5421556Srgrimes	(void)write(rem, path, strlen(path));
5431556Srgrimes	if (response() < 0) {
5441556Srgrimes		closedir(dirp);
5451556Srgrimes		return;
5461556Srgrimes	}
5477165Sjoerg	while ((dp = readdir(dirp))) {
5481556Srgrimes		if (dp->d_ino == 0)
5491556Srgrimes			continue;
5501556Srgrimes		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
5511556Srgrimes			continue;
55277462Simp		if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) {
5531556Srgrimes			run_err("%s/%s: name too long", name, dp->d_name);
5541556Srgrimes			continue;
5551556Srgrimes		}
5561556Srgrimes		(void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
5571556Srgrimes		vect[0] = path;
5581556Srgrimes		source(1, vect);
5591556Srgrimes	}
5601556Srgrimes	(void)closedir(dirp);
5611556Srgrimes	(void)write(rem, "E\n", 2);
5621556Srgrimes	(void)response();
5631556Srgrimes}
5641556Srgrimes
5651556Srgrimesvoid
56690110Simpsink(int argc, char *argv[])
5671556Srgrimes{
5681556Srgrimes	static BUF buffer;
5691556Srgrimes	struct stat stb;
5701556Srgrimes	struct timeval tv[2];
5711556Srgrimes	enum { YES, NO, DISPLAYED } wrerr;
5721556Srgrimes	BUF *bp;
57346057Sdt	off_t i, j, size;
5741556Srgrimes	int amt, count, exists, first, mask, mode, ofd, omode;
57546057Sdt	int setimes, targisdir, wrerrno = 0;
5761556Srgrimes	char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
5771556Srgrimes
5781556Srgrimes#define	atime	tv[0]
5791556Srgrimes#define	mtime	tv[1]
5801556Srgrimes#define	SCREWUP(str)	{ why = str; goto screwup; }
5811556Srgrimes
5821556Srgrimes	setimes = targisdir = 0;
5831556Srgrimes	mask = umask(0);
5841556Srgrimes	if (!pflag)
5851556Srgrimes		(void)umask(mask);
5861556Srgrimes	if (argc != 1) {
5871556Srgrimes		run_err("ambiguous target");
5881556Srgrimes		exit(1);
5891556Srgrimes	}
5901556Srgrimes	targ = *argv;
5911556Srgrimes	if (targetshouldbedirectory)
5921556Srgrimes		verifydir(targ);
5931556Srgrimes	(void)write(rem, "", 1);
5941556Srgrimes	if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
5951556Srgrimes		targisdir = 1;
5961556Srgrimes	for (first = 1;; first = 0) {
5971556Srgrimes		cp = buf;
5981556Srgrimes		if (read(rem, cp, 1) <= 0)
5991556Srgrimes			return;
6001556Srgrimes		if (*cp++ == '\n')
6011556Srgrimes			SCREWUP("unexpected <newline>");
6021556Srgrimes		do {
6031556Srgrimes			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
6041556Srgrimes				SCREWUP("lost connection");
6051556Srgrimes			*cp++ = ch;
6061556Srgrimes		} while (cp < &buf[BUFSIZ - 1] && ch != '\n');
6071556Srgrimes		*cp = 0;
6081556Srgrimes
6091556Srgrimes		if (buf[0] == '\01' || buf[0] == '\02') {
6101556Srgrimes			if (iamremote == 0)
6111556Srgrimes				(void)write(STDERR_FILENO,
6121556Srgrimes				    buf + 1, strlen(buf + 1));
6131556Srgrimes			if (buf[0] == '\02')
6141556Srgrimes				exit(1);
6151556Srgrimes			++errs;
6161556Srgrimes			continue;
6171556Srgrimes		}
6181556Srgrimes		if (buf[0] == 'E') {
6191556Srgrimes			(void)write(rem, "", 1);
6201556Srgrimes			return;
6211556Srgrimes		}
6221556Srgrimes
6231556Srgrimes		if (ch == '\n')
6241556Srgrimes			*--cp = 0;
6251556Srgrimes
6261556Srgrimes		cp = buf;
6271556Srgrimes		if (*cp == 'T') {
6281556Srgrimes			setimes++;
6291556Srgrimes			cp++;
63013978Spst			mtime.tv_sec = strtol(cp, &cp, 10);
63113978Spst			if (!cp || *cp++ != ' ')
6321556Srgrimes				SCREWUP("mtime.sec not delimited");
63313978Spst			mtime.tv_usec = strtol(cp, &cp, 10);
63413978Spst			if (!cp || *cp++ != ' ')
6351556Srgrimes				SCREWUP("mtime.usec not delimited");
63613978Spst			atime.tv_sec = strtol(cp, &cp, 10);
63713978Spst			if (!cp || *cp++ != ' ')
6381556Srgrimes				SCREWUP("atime.sec not delimited");
63913978Spst			atime.tv_usec = strtol(cp, &cp, 10);
64013978Spst			if (!cp || *cp++ != '\0')
6411556Srgrimes				SCREWUP("atime.usec not delimited");
6421556Srgrimes			(void)write(rem, "", 1);
6431556Srgrimes			continue;
6441556Srgrimes		}
6451556Srgrimes		if (*cp != 'C' && *cp != 'D') {
6461556Srgrimes			/*
6471556Srgrimes			 * Check for the case "rcp remote:foo\* local:bar".
6481556Srgrimes			 * In this case, the line "No match." can be returned
6491556Srgrimes			 * by the shell before the rcp command on the remote is
6501556Srgrimes			 * executed so the ^Aerror_message convention isn't
6511556Srgrimes			 * followed.
6521556Srgrimes			 */
6531556Srgrimes			if (first) {
6541556Srgrimes				run_err("%s", cp);
6551556Srgrimes				exit(1);
6561556Srgrimes			}
6571556Srgrimes			SCREWUP("expected control record");
6581556Srgrimes		}
6591556Srgrimes		mode = 0;
6601556Srgrimes		for (++cp; cp < buf + 5; cp++) {
6611556Srgrimes			if (*cp < '0' || *cp > '7')
6621556Srgrimes				SCREWUP("bad mode");
6631556Srgrimes			mode = (mode << 3) | (*cp - '0');
6641556Srgrimes		}
6651556Srgrimes		if (*cp++ != ' ')
6661556Srgrimes			SCREWUP("mode not delimited");
6671556Srgrimes
6681556Srgrimes		for (size = 0; isdigit(*cp);)
6691556Srgrimes			size = size * 10 + (*cp++ - '0');
6701556Srgrimes		if (*cp++ != ' ')
6711556Srgrimes			SCREWUP("size not delimited");
6721556Srgrimes		if (targisdir) {
67398468Sjmallett			static char *namebuf = NULL;
67498468Sjmallett			static size_t cursize;
6751556Srgrimes			size_t need;
6761556Srgrimes
6771556Srgrimes			need = strlen(targ) + strlen(cp) + 250;
6781556Srgrimes			if (need > cursize) {
67998468Sjmallett				if (namebuf != NULL)
68098468Sjmallett					free(namebuf);
6811556Srgrimes				if (!(namebuf = malloc(need)))
6821556Srgrimes					run_err("%s", strerror(errno));
68398468Sjmallett				cursize = need;
6841556Srgrimes			}
6851556Srgrimes			(void)snprintf(namebuf, need, "%s%s%s", targ,
6861556Srgrimes			    *targ ? "/" : "", cp);
6871556Srgrimes			np = namebuf;
6881556Srgrimes		} else
6891556Srgrimes			np = targ;
6901556Srgrimes		exists = stat(np, &stb) == 0;
6911556Srgrimes		if (buf[0] == 'D') {
6921556Srgrimes			int mod_flag = pflag;
6931556Srgrimes			if (exists) {
6941556Srgrimes				if (!S_ISDIR(stb.st_mode)) {
6951556Srgrimes					errno = ENOTDIR;
6961556Srgrimes					goto bad;
6971556Srgrimes				}
6981556Srgrimes				if (pflag)
6991556Srgrimes					(void)chmod(np, mode);
7001556Srgrimes			} else {
7011556Srgrimes				/* Handle copying from a read-only directory */
7021556Srgrimes				mod_flag = 1;
7031556Srgrimes				if (mkdir(np, mode | S_IRWXU) < 0)
7041556Srgrimes					goto bad;
7051556Srgrimes			}
7061556Srgrimes			vect[0] = np;
7071556Srgrimes			sink(1, vect);
7081556Srgrimes			if (setimes) {
7091556Srgrimes				setimes = 0;
7101556Srgrimes				if (utimes(np, tv) < 0)
7111556Srgrimes				    run_err("%s: set times: %s",
7121556Srgrimes					np, strerror(errno));
7131556Srgrimes			}
7141556Srgrimes			if (mod_flag)
7151556Srgrimes				(void)chmod(np, mode);
7161556Srgrimes			continue;
7171556Srgrimes		}
7181556Srgrimes		omode = mode;
7191556Srgrimes		mode |= S_IWRITE;
7201556Srgrimes		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
7211556Srgrimesbad:			run_err("%s: %s", np, strerror(errno));
7221556Srgrimes			continue;
7231556Srgrimes		}
7241556Srgrimes		(void)write(rem, "", 1);
7251556Srgrimes		if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
7261556Srgrimes			(void)close(ofd);
7271556Srgrimes			continue;
7281556Srgrimes		}
7291556Srgrimes		cp = bp->buf;
7301556Srgrimes		wrerr = NO;
7311556Srgrimes		for (count = i = 0; i < size; i += BUFSIZ) {
7321556Srgrimes			amt = BUFSIZ;
7331556Srgrimes			if (i + amt > size)
7341556Srgrimes				amt = size - i;
7351556Srgrimes			count += amt;
7361556Srgrimes			do {
7371556Srgrimes				j = read(rem, cp, amt);
7381556Srgrimes				if (j <= 0) {
7391556Srgrimes					run_err("%s", j ? strerror(errno) :
7401556Srgrimes					    "dropped connection");
7411556Srgrimes					exit(1);
7421556Srgrimes				}
7431556Srgrimes				amt -= j;
7441556Srgrimes				cp += j;
7451556Srgrimes			} while (amt > 0);
7461556Srgrimes			if (count == bp->cnt) {
7471556Srgrimes				/* Keep reading so we stay sync'd up. */
7481556Srgrimes				if (wrerr == NO) {
7491556Srgrimes					j = write(ofd, bp->buf, count);
7501556Srgrimes					if (j != count) {
7511556Srgrimes						wrerr = YES;
7528855Srgrimes						wrerrno = j >= 0 ? EIO : errno;
7531556Srgrimes					}
7541556Srgrimes				}
7551556Srgrimes				count = 0;
7561556Srgrimes				cp = bp->buf;
7571556Srgrimes			}
7581556Srgrimes		}
7591556Srgrimes		if (count != 0 && wrerr == NO &&
7601556Srgrimes		    (j = write(ofd, bp->buf, count)) != count) {
7611556Srgrimes			wrerr = YES;
7628855Srgrimes			wrerrno = j >= 0 ? EIO : errno;
7631556Srgrimes		}
7641556Srgrimes		if (ftruncate(ofd, size)) {
7651556Srgrimes			run_err("%s: truncate: %s", np, strerror(errno));
7661556Srgrimes			wrerr = DISPLAYED;
7671556Srgrimes		}
7681556Srgrimes		if (pflag) {
7691556Srgrimes			if (exists || omode != mode)
7701556Srgrimes				if (fchmod(ofd, omode))
7711556Srgrimes					run_err("%s: set mode: %s",
7721556Srgrimes					    np, strerror(errno));
7731556Srgrimes		} else {
7741556Srgrimes			if (!exists && omode != mode)
7751556Srgrimes				if (fchmod(ofd, omode & ~mask))
7761556Srgrimes					run_err("%s: set mode: %s",
7771556Srgrimes					    np, strerror(errno));
7781556Srgrimes		}
7791556Srgrimes		(void)close(ofd);
7801556Srgrimes		(void)response();
7811556Srgrimes		if (setimes && wrerr == NO) {
7821556Srgrimes			setimes = 0;
7831556Srgrimes			if (utimes(np, tv) < 0) {
7841556Srgrimes				run_err("%s: set times: %s",
7851556Srgrimes				    np, strerror(errno));
7861556Srgrimes				wrerr = DISPLAYED;
7871556Srgrimes			}
7881556Srgrimes		}
7891556Srgrimes		switch(wrerr) {
7901556Srgrimes		case YES:
7911556Srgrimes			run_err("%s: %s", np, strerror(wrerrno));
7921556Srgrimes			break;
7931556Srgrimes		case NO:
7941556Srgrimes			(void)write(rem, "", 1);
7951556Srgrimes			break;
7961556Srgrimes		case DISPLAYED:
7971556Srgrimes			break;
7981556Srgrimes		}
7991556Srgrimes	}
8001556Srgrimesscrewup:
8011556Srgrimes	run_err("protocol error: %s", why);
8021556Srgrimes	exit(1);
8031556Srgrimes}
8041556Srgrimes
8051556Srgrimes#ifdef KERBEROS
8061556Srgrimesint
80790110Simpkerberos(char **host, char *bp, char *locuser, char *user)
8081556Srgrimes{
8091556Srgrimes	if (use_kerberos) {
81034898Smarkm		setuid(getuid());
8111556Srgrimes		rem = KSUCCESS;
8121556Srgrimes		errno = 0;
8131556Srgrimes		if (dest_realm == NULL)
8141556Srgrimes			dest_realm = krb_realmofhost(*host);
8158855Srgrimes		rem =
8161556Srgrimes#ifdef CRYPT
8178855Srgrimes		    doencrypt ?
8181556Srgrimes			krcmd_mutual(host,
8191556Srgrimes			    port, user, bp, 0, dest_realm, &cred, schedule) :
8201556Srgrimes#endif
8211556Srgrimes			krcmd(host, port, user, bp, 0, dest_realm);
8221556Srgrimes
8231556Srgrimes		if (rem < 0) {
8241556Srgrimes			if (errno == ECONNREFUSED)
8251556Srgrimes			    oldw("remote host doesn't support Kerberos");
8261556Srgrimes			else if (errno == ENOENT)
8271556Srgrimes			    oldw("can't provide Kerberos authentication data");
82834898Smarkm			execv(_PATH_RCP, argv_copy);
82948560Sbde			err(1, "execv: %s", _PATH_RCP);
8301556Srgrimes		}
8311556Srgrimes	} else {
8321556Srgrimes#ifdef CRYPT
8331556Srgrimes		if (doencrypt)
8341556Srgrimes			errx(1,
8351556Srgrimes			   "the -x option requires Kerberos authentication");
8361556Srgrimes#endif
8371556Srgrimes		rem = rcmd(host, port, locuser, user, bp, 0);
8381556Srgrimes	}
8391556Srgrimes	return (rem);
8401556Srgrimes}
8411556Srgrimes#endif /* KERBEROS */
8421556Srgrimes
8431556Srgrimesint
84490110Simpresponse(void)
8451556Srgrimes{
8461556Srgrimes	char ch, *cp, resp, rbuf[BUFSIZ];
8471556Srgrimes
8481556Srgrimes	if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
8491556Srgrimes		lostconn(0);
8501556Srgrimes
8511556Srgrimes	cp = rbuf;
8521556Srgrimes	switch(resp) {
8531556Srgrimes	case 0:				/* ok */
8541556Srgrimes		return (0);
8551556Srgrimes	default:
8561556Srgrimes		*cp++ = resp;
8571556Srgrimes		/* FALLTHROUGH */
8581556Srgrimes	case 1:				/* error, followed by error msg */
8591556Srgrimes	case 2:				/* fatal error, "" */
8601556Srgrimes		do {
8611556Srgrimes			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
8621556Srgrimes				lostconn(0);
8631556Srgrimes			*cp++ = ch;
8641556Srgrimes		} while (cp < &rbuf[BUFSIZ] && ch != '\n');
8651556Srgrimes
8661556Srgrimes		if (!iamremote)
8671556Srgrimes			(void)write(STDERR_FILENO, rbuf, cp - rbuf);
8681556Srgrimes		++errs;
8691556Srgrimes		if (resp == 1)
8701556Srgrimes			return (-1);
8711556Srgrimes		exit(1);
8721556Srgrimes	}
8731556Srgrimes	/* NOTREACHED */
8741556Srgrimes}
8751556Srgrimes
8761556Srgrimesvoid
87790110Simpusage(void)
8781556Srgrimes{
8791556Srgrimes#ifdef KERBEROS
8801556Srgrimes#ifdef CRYPT
88126466Scharnier	(void)fprintf(stderr, "%s\n%s\n",
8821556Srgrimes	    "usage: rcp [-Kpx] [-k realm] f1 f2",
88326466Scharnier	    "       rcp [-Kprx] [-k realm] f1 ... fn directory");
8841556Srgrimes#else
88526466Scharnier	(void)fprintf(stderr, "%s\n%s\n",
8861556Srgrimes	    "usage: rcp [-Kp] [-k realm] f1 f2",
88726466Scharnier	    "       rcp [-Kpr] [-k realm] f1 ... fn directory");
8881556Srgrimes#endif
8891556Srgrimes#else
89026466Scharnier	(void)fprintf(stderr, "%s\n%s\n",
89126466Scharnier	    "usage: rcp [-p] f1 f2",
89226466Scharnier	    "       rcp [-pr] f1 ... fn directory");
8931556Srgrimes#endif
8941556Srgrimes	exit(1);
8951556Srgrimes}
8961556Srgrimes
8971556Srgrimes#include <stdarg.h>
8981556Srgrimes
8991556Srgrimes#ifdef KERBEROS
9001556Srgrimesvoid
9011556Srgrimesoldw(const char *fmt, ...)
9021556Srgrimes{
9031556Srgrimes	va_list ap;
9041556Srgrimes	va_start(ap, fmt);
9051556Srgrimes	(void)fprintf(stderr, "rcp: ");
9061556Srgrimes	(void)vfprintf(stderr, fmt, ap);
9071556Srgrimes	(void)fprintf(stderr, ", using standard rcp\n");
9081556Srgrimes	va_end(ap);
9091556Srgrimes}
9101556Srgrimes#endif
9111556Srgrimes
9121556Srgrimesvoid
9131556Srgrimesrun_err(const char *fmt, ...)
9141556Srgrimes{
9151556Srgrimes	static FILE *fp;
9161556Srgrimes	va_list ap;
9171556Srgrimes	va_start(ap, fmt);
9181556Srgrimes
9191556Srgrimes	++errs;
9201556Srgrimes	if (fp == NULL && !(fp = fdopen(rem, "w")))
9211556Srgrimes		return;
9221556Srgrimes	(void)fprintf(fp, "%c", 0x01);
9231556Srgrimes	(void)fprintf(fp, "rcp: ");
9241556Srgrimes	(void)vfprintf(fp, fmt, ap);
9251556Srgrimes	(void)fprintf(fp, "\n");
9261556Srgrimes	(void)fflush(fp);
9271556Srgrimes
9281556Srgrimes	if (!iamremote)
9291556Srgrimes		vwarnx(fmt, ap);
9301556Srgrimes
9311556Srgrimes	va_end(ap);
9321556Srgrimes}
933