rcp.c revision 24348
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1983, 1990, 1992, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
323044Sdg *
3324348Simp *	$Id: rcp.c,v 1.11 1997/02/22 14:05:22 peter Exp $
341556Srgrimes */
351556Srgrimes
361556Srgrimes#ifndef lint
3720420Sstevestatic char const copyright[] =
381556Srgrimes"@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
391556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
401556Srgrimes#endif /* not lint */
411556Srgrimes
421556Srgrimes#ifndef lint
4320420Sstevestatic char const sccsid[] = "@(#)rcp.c	8.2 (Berkeley) 4/2/94";
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <sys/param.h>
471556Srgrimes#include <sys/stat.h>
481556Srgrimes#include <sys/time.h>
491556Srgrimes#include <sys/socket.h>
501556Srgrimes#include <netinet/in.h>
511556Srgrimes#include <netinet/in_systm.h>
521556Srgrimes#include <netinet/ip.h>
531556Srgrimes
541556Srgrimes#include <ctype.h>
551556Srgrimes#include <dirent.h>
561556Srgrimes#include <err.h>
571556Srgrimes#include <errno.h>
581556Srgrimes#include <fcntl.h>
591556Srgrimes#include <netdb.h>
601556Srgrimes#include <pwd.h>
611556Srgrimes#include <signal.h>
621556Srgrimes#include <stdio.h>
631556Srgrimes#include <stdlib.h>
641556Srgrimes#include <string.h>
651556Srgrimes#include <string.h>
661556Srgrimes#include <unistd.h>
671556Srgrimes
681556Srgrimes#include "pathnames.h"
691556Srgrimes#include "extern.h"
701556Srgrimes
711556Srgrimes#ifdef KERBEROS
7214024Smarkm#include <des.h>
731556Srgrimes#include <kerberosIV/krb.h>
741556Srgrimes
7510663Smarkm#include "../../usr.bin/rlogin/krb.h"
7610663Smarkm
771556Srgrimeschar	dst_realm_buf[REALM_SZ];
781556Srgrimeschar	*dest_realm = NULL;
791556Srgrimesint	use_kerberos = 1;
801556SrgrimesCREDENTIALS 	cred;
811556SrgrimesKey_schedule	schedule;
821556Srgrimesextern	char	*krb_realmofhost();
831556Srgrimes#ifdef CRYPT
841556Srgrimesint	doencrypt = 0;
851556Srgrimes#define	OPTIONS	"dfKk:prtx"
861556Srgrimes#else
871556Srgrimes#define	OPTIONS	"dfKk:prt"
881556Srgrimes#endif
891556Srgrimes#else
901556Srgrimes#define	OPTIONS "dfprt"
911556Srgrimes#endif
921556Srgrimes
931556Srgrimesstruct passwd *pwd;
941556Srgrimesu_short	port;
951556Srgrimesuid_t	userid;
961556Srgrimesint errs, rem;
971556Srgrimesint pflag, iamremote, iamrecursive, targetshouldbedirectory;
981556Srgrimes
991556Srgrimes#define	CMDNEEDS	64
1001556Srgrimeschar cmd[CMDNEEDS];		/* must hold "rcp -r -p -d\0" */
1011556Srgrimes
1021556Srgrimes#ifdef KERBEROS
1031556Srgrimesint	 kerberos __P((char **, char *, char *, char *));
1041556Srgrimesvoid	 oldw __P((const char *, ...));
1051556Srgrimes#endif
1061556Srgrimesint	 response __P((void));
1071556Srgrimesvoid	 rsource __P((char *, struct stat *));
1081556Srgrimesvoid	 sink __P((int, char *[]));
1091556Srgrimesvoid	 source __P((int, char *[]));
1101556Srgrimesvoid	 tolocal __P((int, char *[]));
1111556Srgrimesvoid	 toremote __P((char *, int, char *[]));
1121556Srgrimesvoid	 usage __P((void));
1131556Srgrimes
1141556Srgrimesint
1151556Srgrimesmain(argc, argv)
1161556Srgrimes	int argc;
1171556Srgrimes	char *argv[];
1181556Srgrimes{
1191556Srgrimes	struct servent *sp;
1201556Srgrimes	int ch, fflag, tflag;
1211556Srgrimes	char *targ, *shell;
1221556Srgrimes
1231556Srgrimes	fflag = tflag = 0;
12424348Simp	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
1251556Srgrimes		switch(ch) {			/* User-visible flags. */
1261556Srgrimes		case 'K':
1271556Srgrimes#ifdef KERBEROS
1281556Srgrimes			use_kerberos = 0;
1291556Srgrimes#endif
1301556Srgrimes			break;
1311556Srgrimes#ifdef	KERBEROS
1321556Srgrimes		case 'k':
1331556Srgrimes			dest_realm = dst_realm_buf;
1341556Srgrimes			(void)strncpy(dst_realm_buf, optarg, REALM_SZ);
1351556Srgrimes			break;
1361556Srgrimes#ifdef CRYPT
1371556Srgrimes		case 'x':
1381556Srgrimes			doencrypt = 1;
1391556Srgrimes			/* des_set_key(cred.session, schedule); */
1401556Srgrimes			break;
1411556Srgrimes#endif
1421556Srgrimes#endif
1431556Srgrimes		case 'p':
1441556Srgrimes			pflag = 1;
1451556Srgrimes			break;
1461556Srgrimes		case 'r':
1471556Srgrimes			iamrecursive = 1;
1481556Srgrimes			break;
1491556Srgrimes						/* Server options. */
1501556Srgrimes		case 'd':
1511556Srgrimes			targetshouldbedirectory = 1;
1521556Srgrimes			break;
1531556Srgrimes		case 'f':			/* "from" */
1541556Srgrimes			iamremote = 1;
1551556Srgrimes			fflag = 1;
1561556Srgrimes			break;
1571556Srgrimes		case 't':			/* "to" */
1581556Srgrimes			iamremote = 1;
1591556Srgrimes			tflag = 1;
1601556Srgrimes			break;
1611556Srgrimes		case '?':
1621556Srgrimes		default:
1631556Srgrimes			usage();
1641556Srgrimes		}
1651556Srgrimes	argc -= optind;
1661556Srgrimes	argv += optind;
1671556Srgrimes
1681556Srgrimes#ifdef KERBEROS
1691556Srgrimes	if (use_kerberos) {
1701556Srgrimes#ifdef CRYPT
1711556Srgrimes		shell = doencrypt ? "ekshell" : "kshell";
1721556Srgrimes#else
1731556Srgrimes		shell = "kshell";
1741556Srgrimes#endif
1751556Srgrimes		if ((sp = getservbyname(shell, "tcp")) == NULL) {
1761556Srgrimes			use_kerberos = 0;
1771556Srgrimes			oldw("can't get entry for %s/tcp service", shell);
1781556Srgrimes			sp = getservbyname(shell = "shell", "tcp");
1791556Srgrimes		}
1801556Srgrimes	} else
1811556Srgrimes		sp = getservbyname(shell = "shell", "tcp");
1821556Srgrimes#else
1831556Srgrimes	sp = getservbyname(shell = "shell", "tcp");
1841556Srgrimes#endif
1851556Srgrimes	if (sp == NULL)
1861556Srgrimes		errx(1, "%s/tcp: unknown service", shell);
1871556Srgrimes	port = sp->s_port;
1881556Srgrimes
1891556Srgrimes	if ((pwd = getpwuid(userid = getuid())) == NULL)
1901556Srgrimes		errx(1, "unknown user %d", (int)userid);
1911556Srgrimes
1921556Srgrimes	rem = STDIN_FILENO;		/* XXX */
1931556Srgrimes
1941556Srgrimes	if (fflag) {			/* Follow "protocol", send data. */
1951556Srgrimes		(void)response();
1961556Srgrimes		(void)setuid(userid);
1971556Srgrimes		source(argc, argv);
1981556Srgrimes		exit(errs);
1991556Srgrimes	}
2001556Srgrimes
2011556Srgrimes	if (tflag) {			/* Receive data. */
2021556Srgrimes		(void)setuid(userid);
2031556Srgrimes		sink(argc, argv);
2041556Srgrimes		exit(errs);
2051556Srgrimes	}
2061556Srgrimes
2071556Srgrimes	if (argc < 2)
2081556Srgrimes		usage();
2091556Srgrimes	if (argc > 2)
2101556Srgrimes		targetshouldbedirectory = 1;
2111556Srgrimes
2121556Srgrimes	rem = -1;
2131556Srgrimes	/* Command to be executed on remote system using "rsh". */
2141556Srgrimes#ifdef	KERBEROS
2151556Srgrimes	(void)snprintf(cmd, sizeof(cmd),
2161556Srgrimes	    "rcp%s%s%s%s", iamrecursive ? " -r" : "",
2171556Srgrimes#ifdef CRYPT
2181556Srgrimes	    (doencrypt && use_kerberos ? " -x" : ""),
2191556Srgrimes#else
2201556Srgrimes	    "",
2211556Srgrimes#endif
2221556Srgrimes	    pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
2231556Srgrimes#else
2241556Srgrimes	(void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
2251556Srgrimes	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
2261556Srgrimes	    targetshouldbedirectory ? " -d" : "");
2271556Srgrimes#endif
2281556Srgrimes
2291556Srgrimes	(void)signal(SIGPIPE, lostconn);
2301556Srgrimes
2317165Sjoerg	if ((targ = colon(argv[argc - 1])))	/* Dest is remote host. */
2321556Srgrimes		toremote(targ, argc, argv);
2331556Srgrimes	else {
2341556Srgrimes		tolocal(argc, argv);		/* Dest is local host. */
2351556Srgrimes		if (targetshouldbedirectory)
2361556Srgrimes			verifydir(argv[argc - 1]);
2371556Srgrimes	}
2381556Srgrimes	exit(errs);
2391556Srgrimes}
2401556Srgrimes
2411556Srgrimesvoid
2421556Srgrimestoremote(targ, argc, argv)
2431556Srgrimes	char *targ, *argv[];
2441556Srgrimes	int argc;
2451556Srgrimes{
2461556Srgrimes	int i, len, tos;
2471556Srgrimes	char *bp, *host, *src, *suser, *thost, *tuser;
2481556Srgrimes
2491556Srgrimes	*targ++ = 0;
2501556Srgrimes	if (*targ == 0)
2511556Srgrimes		targ = ".";
2521556Srgrimes
2537165Sjoerg	if ((thost = strchr(argv[argc - 1], '@'))) {
2541556Srgrimes		/* user@host */
2551556Srgrimes		*thost++ = 0;
2561556Srgrimes		tuser = argv[argc - 1];
2571556Srgrimes		if (*tuser == '\0')
2581556Srgrimes			tuser = NULL;
2591556Srgrimes		else if (!okname(tuser))
2601556Srgrimes			exit(1);
2611556Srgrimes	} else {
2621556Srgrimes		thost = argv[argc - 1];
2631556Srgrimes		tuser = NULL;
2641556Srgrimes	}
2651556Srgrimes
2661556Srgrimes	for (i = 0; i < argc - 1; i++) {
2671556Srgrimes		src = colon(argv[i]);
2681556Srgrimes		if (src) {			/* remote to remote */
2691556Srgrimes			*src++ = 0;
2701556Srgrimes			if (*src == 0)
2711556Srgrimes				src = ".";
2721556Srgrimes			host = strchr(argv[i], '@');
2731556Srgrimes			len = strlen(_PATH_RSH) + strlen(argv[i]) +
2741556Srgrimes			    strlen(src) + (tuser ? strlen(tuser) : 0) +
2751556Srgrimes			    strlen(thost) + strlen(targ) + CMDNEEDS + 20;
2761556Srgrimes			if (!(bp = malloc(len)))
2771556Srgrimes				err(1, NULL);
2781556Srgrimes			if (host) {
2791556Srgrimes				*host++ = 0;
2801556Srgrimes				suser = argv[i];
2811556Srgrimes				if (*suser == '\0')
2821556Srgrimes					suser = pwd->pw_name;
2831556Srgrimes				else if (!okname(suser))
2841556Srgrimes					continue;
2851556Srgrimes				(void)snprintf(bp, len,
2861556Srgrimes				    "%s %s -l %s -n %s %s '%s%s%s:%s'",
2871556Srgrimes				    _PATH_RSH, host, suser, cmd, src,
2881556Srgrimes				    tuser ? tuser : "", tuser ? "@" : "",
2891556Srgrimes				    thost, targ);
2901556Srgrimes			} else
2911556Srgrimes				(void)snprintf(bp, len,
2921556Srgrimes				    "exec %s %s -n %s %s '%s%s%s:%s'",
2931556Srgrimes				    _PATH_RSH, argv[i], cmd, src,
2941556Srgrimes				    tuser ? tuser : "", tuser ? "@" : "",
2951556Srgrimes				    thost, targ);
2961556Srgrimes			(void)susystem(bp, userid);
2971556Srgrimes			(void)free(bp);
2981556Srgrimes		} else {			/* local to remote */
2991556Srgrimes			if (rem == -1) {
3001556Srgrimes				len = strlen(targ) + CMDNEEDS + 20;
3011556Srgrimes				if (!(bp = malloc(len)))
3021556Srgrimes					err(1, NULL);
3031556Srgrimes				(void)snprintf(bp, len, "%s -t %s", cmd, targ);
3041556Srgrimes				host = thost;
3051556Srgrimes#ifdef KERBEROS
3061556Srgrimes				if (use_kerberos)
3071556Srgrimes					rem = kerberos(&host, bp,
3081556Srgrimes					    pwd->pw_name,
3091556Srgrimes					    tuser ? tuser : pwd->pw_name);
3101556Srgrimes				else
3111556Srgrimes#endif
3121556Srgrimes					rem = rcmd(&host, port, pwd->pw_name,
3131556Srgrimes					    tuser ? tuser : pwd->pw_name,
3141556Srgrimes					    bp, 0);
3151556Srgrimes				if (rem < 0)
3161556Srgrimes					exit(1);
3171556Srgrimes				tos = IPTOS_THROUGHPUT;
3181556Srgrimes				if (setsockopt(rem, IPPROTO_IP, IP_TOS,
3191556Srgrimes				    &tos, sizeof(int)) < 0)
3201556Srgrimes					warn("TOS (ignored)");
3211556Srgrimes				if (response() < 0)
3221556Srgrimes					exit(1);
3231556Srgrimes				(void)free(bp);
3241556Srgrimes				(void)setuid(userid);
3251556Srgrimes			}
3261556Srgrimes			source(1, argv+i);
3271556Srgrimes		}
3281556Srgrimes	}
3291556Srgrimes}
3301556Srgrimes
3311556Srgrimesvoid
3321556Srgrimestolocal(argc, argv)
3331556Srgrimes	int argc;
3341556Srgrimes	char *argv[];
3351556Srgrimes{
3361556Srgrimes	int i, len, tos;
3371556Srgrimes	char *bp, *host, *src, *suser;
3381556Srgrimes
3391556Srgrimes	for (i = 0; i < argc - 1; i++) {
3401556Srgrimes		if (!(src = colon(argv[i]))) {		/* Local to local. */
3411556Srgrimes			len = strlen(_PATH_CP) + strlen(argv[i]) +
3421556Srgrimes			    strlen(argv[argc - 1]) + 20;
3431556Srgrimes			if (!(bp = malloc(len)))
3441556Srgrimes				err(1, NULL);
3451556Srgrimes			(void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
3461556Srgrimes			    iamrecursive ? " -r" : "", pflag ? " -p" : "",
3471556Srgrimes			    argv[i], argv[argc - 1]);
3481556Srgrimes			if (susystem(bp, userid))
3491556Srgrimes				++errs;
3501556Srgrimes			(void)free(bp);
3511556Srgrimes			continue;
3521556Srgrimes		}
3531556Srgrimes		*src++ = 0;
3541556Srgrimes		if (*src == 0)
3551556Srgrimes			src = ".";
3561556Srgrimes		if ((host = strchr(argv[i], '@')) == NULL) {
3571556Srgrimes			host = argv[i];
3581556Srgrimes			suser = pwd->pw_name;
3591556Srgrimes		} else {
3601556Srgrimes			*host++ = 0;
3611556Srgrimes			suser = argv[i];
3621556Srgrimes			if (*suser == '\0')
3631556Srgrimes				suser = pwd->pw_name;
3641556Srgrimes			else if (!okname(suser))
3651556Srgrimes				continue;
3661556Srgrimes		}
3671556Srgrimes		len = strlen(src) + CMDNEEDS + 20;
3681556Srgrimes		if ((bp = malloc(len)) == NULL)
3691556Srgrimes			err(1, NULL);
3701556Srgrimes		(void)snprintf(bp, len, "%s -f %s", cmd, src);
3718855Srgrimes		rem =
3721556Srgrimes#ifdef KERBEROS
3738855Srgrimes		    use_kerberos ?
3748855Srgrimes			kerberos(&host, bp, pwd->pw_name, suser) :
3751556Srgrimes#endif
3761556Srgrimes			rcmd(&host, port, pwd->pw_name, suser, bp, 0);
3771556Srgrimes		(void)free(bp);
3781556Srgrimes		if (rem < 0) {
3791556Srgrimes			++errs;
3801556Srgrimes			continue;
3811556Srgrimes		}
3821556Srgrimes		(void)seteuid(userid);
3831556Srgrimes		tos = IPTOS_THROUGHPUT;
3841556Srgrimes		if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
3851556Srgrimes			warn("TOS (ignored)");
3861556Srgrimes		sink(1, argv + argc - 1);
3871556Srgrimes		(void)seteuid(0);
3881556Srgrimes		(void)close(rem);
3891556Srgrimes		rem = -1;
3901556Srgrimes	}
3911556Srgrimes}
3921556Srgrimes
3931556Srgrimesvoid
3941556Srgrimessource(argc, argv)
3951556Srgrimes	int argc;
3961556Srgrimes	char *argv[];
3971556Srgrimes{
3981556Srgrimes	struct stat stb;
3991556Srgrimes	static BUF buffer;
4001556Srgrimes	BUF *bp;
4011556Srgrimes	off_t i;
4021556Srgrimes	int amt, fd, haderr, indx, result;
4031556Srgrimes	char *last, *name, buf[BUFSIZ];
4041556Srgrimes
4051556Srgrimes	for (indx = 0; indx < argc; ++indx) {
4061556Srgrimes                name = argv[indx];
4071556Srgrimes		if ((fd = open(name, O_RDONLY, 0)) < 0)
4081556Srgrimes			goto syserr;
4091556Srgrimes		if (fstat(fd, &stb)) {
4101556Srgrimessyserr:			run_err("%s: %s", name, strerror(errno));
4111556Srgrimes			goto next;
4121556Srgrimes		}
4131556Srgrimes		switch (stb.st_mode & S_IFMT) {
4141556Srgrimes		case S_IFREG:
4151556Srgrimes			break;
4161556Srgrimes		case S_IFDIR:
4171556Srgrimes			if (iamrecursive) {
4181556Srgrimes				rsource(name, &stb);
4191556Srgrimes				goto next;
4201556Srgrimes			}
4211556Srgrimes			/* FALLTHROUGH */
4221556Srgrimes		default:
4231556Srgrimes			run_err("%s: not a regular file", name);
4241556Srgrimes			goto next;
4251556Srgrimes		}
4261556Srgrimes		if ((last = strrchr(name, '/')) == NULL)
4271556Srgrimes			last = name;
4281556Srgrimes		else
4291556Srgrimes			++last;
4301556Srgrimes		if (pflag) {
4311556Srgrimes			/*
4321556Srgrimes			 * Make it compatible with possible future
4331556Srgrimes			 * versions expecting microseconds.
4341556Srgrimes			 */
4351556Srgrimes			(void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
43618409Snate			    stb.st_mtimespec.tv_sec, stb.st_atimespec.tv_sec);
4371556Srgrimes			(void)write(rem, buf, strlen(buf));
4381556Srgrimes			if (response() < 0)
4391556Srgrimes				goto next;
4401556Srgrimes		}
4411556Srgrimes#define	MODEMASK	(S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
4421556Srgrimes		(void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
4431556Srgrimes		    stb.st_mode & MODEMASK, stb.st_size, last);
4441556Srgrimes		(void)write(rem, buf, strlen(buf));
4451556Srgrimes		if (response() < 0)
4461556Srgrimes			goto next;
4471556Srgrimes		if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
4481556Srgrimesnext:			(void)close(fd);
4491556Srgrimes			continue;
4501556Srgrimes		}
4511556Srgrimes
4521556Srgrimes		/* Keep writing after an error so that we stay sync'd up. */
4531556Srgrimes		for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
4541556Srgrimes			amt = bp->cnt;
4551556Srgrimes			if (i + amt > stb.st_size)
4561556Srgrimes				amt = stb.st_size - i;
4571556Srgrimes			if (!haderr) {
4581556Srgrimes				result = read(fd, bp->buf, amt);
4591556Srgrimes				if (result != amt)
4601556Srgrimes					haderr = result >= 0 ? EIO : errno;
4611556Srgrimes			}
4621556Srgrimes			if (haderr)
4631556Srgrimes				(void)write(rem, bp->buf, amt);
4641556Srgrimes			else {
4651556Srgrimes				result = write(rem, bp->buf, amt);
4661556Srgrimes				if (result != amt)
4671556Srgrimes					haderr = result >= 0 ? EIO : errno;
4681556Srgrimes			}
4691556Srgrimes		}
4701556Srgrimes		if (close(fd) && !haderr)
4711556Srgrimes			haderr = errno;
4721556Srgrimes		if (!haderr)
4731556Srgrimes			(void)write(rem, "", 1);
4741556Srgrimes		else
4751556Srgrimes			run_err("%s: %s", name, strerror(haderr));
4761556Srgrimes		(void)response();
4771556Srgrimes	}
4781556Srgrimes}
4791556Srgrimes
4801556Srgrimesvoid
4811556Srgrimesrsource(name, statp)
4821556Srgrimes	char *name;
4831556Srgrimes	struct stat *statp;
4841556Srgrimes{
4851556Srgrimes	DIR *dirp;
4861556Srgrimes	struct dirent *dp;
4871556Srgrimes	char *last, *vect[1], path[MAXPATHLEN];
4881556Srgrimes
4891556Srgrimes	if (!(dirp = opendir(name))) {
4901556Srgrimes		run_err("%s: %s", name, strerror(errno));
4911556Srgrimes		return;
4921556Srgrimes	}
4931556Srgrimes	last = strrchr(name, '/');
4941556Srgrimes	if (last == 0)
4951556Srgrimes		last = name;
4961556Srgrimes	else
4971556Srgrimes		last++;
4981556Srgrimes	if (pflag) {
4991556Srgrimes		(void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
50018409Snate		    statp->st_mtimespec.tv_sec, statp->st_atimespec.tv_sec);
5011556Srgrimes		(void)write(rem, path, strlen(path));
5021556Srgrimes		if (response() < 0) {
5031556Srgrimes			closedir(dirp);
5041556Srgrimes			return;
5051556Srgrimes		}
5061556Srgrimes	}
5071556Srgrimes	(void)snprintf(path, sizeof(path),
5081556Srgrimes	    "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
5091556Srgrimes	(void)write(rem, path, strlen(path));
5101556Srgrimes	if (response() < 0) {
5111556Srgrimes		closedir(dirp);
5121556Srgrimes		return;
5131556Srgrimes	}
5147165Sjoerg	while ((dp = readdir(dirp))) {
5151556Srgrimes		if (dp->d_ino == 0)
5161556Srgrimes			continue;
5171556Srgrimes		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
5181556Srgrimes			continue;
5191556Srgrimes		if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) {
5201556Srgrimes			run_err("%s/%s: name too long", name, dp->d_name);
5211556Srgrimes			continue;
5221556Srgrimes		}
5231556Srgrimes		(void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
5241556Srgrimes		vect[0] = path;
5251556Srgrimes		source(1, vect);
5261556Srgrimes	}
5271556Srgrimes	(void)closedir(dirp);
5281556Srgrimes	(void)write(rem, "E\n", 2);
5291556Srgrimes	(void)response();
5301556Srgrimes}
5311556Srgrimes
5321556Srgrimesvoid
5331556Srgrimessink(argc, argv)
5341556Srgrimes	int argc;
5351556Srgrimes	char *argv[];
5361556Srgrimes{
5371556Srgrimes	static BUF buffer;
5381556Srgrimes	struct stat stb;
5391556Srgrimes	struct timeval tv[2];
5401556Srgrimes	enum { YES, NO, DISPLAYED } wrerr;
5411556Srgrimes	BUF *bp;
5421556Srgrimes	off_t i, j;
5431556Srgrimes	int amt, count, exists, first, mask, mode, ofd, omode;
5447165Sjoerg	int setimes, size, targisdir, wrerrno = 0;
5451556Srgrimes	char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
5461556Srgrimes
5471556Srgrimes#define	atime	tv[0]
5481556Srgrimes#define	mtime	tv[1]
5491556Srgrimes#define	SCREWUP(str)	{ why = str; goto screwup; }
5501556Srgrimes
5511556Srgrimes	setimes = targisdir = 0;
5521556Srgrimes	mask = umask(0);
5531556Srgrimes	if (!pflag)
5541556Srgrimes		(void)umask(mask);
5551556Srgrimes	if (argc != 1) {
5561556Srgrimes		run_err("ambiguous target");
5571556Srgrimes		exit(1);
5581556Srgrimes	}
5591556Srgrimes	targ = *argv;
5601556Srgrimes	if (targetshouldbedirectory)
5611556Srgrimes		verifydir(targ);
5621556Srgrimes	(void)write(rem, "", 1);
5631556Srgrimes	if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
5641556Srgrimes		targisdir = 1;
5651556Srgrimes	for (first = 1;; first = 0) {
5661556Srgrimes		cp = buf;
5671556Srgrimes		if (read(rem, cp, 1) <= 0)
5681556Srgrimes			return;
5691556Srgrimes		if (*cp++ == '\n')
5701556Srgrimes			SCREWUP("unexpected <newline>");
5711556Srgrimes		do {
5721556Srgrimes			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
5731556Srgrimes				SCREWUP("lost connection");
5741556Srgrimes			*cp++ = ch;
5751556Srgrimes		} while (cp < &buf[BUFSIZ - 1] && ch != '\n');
5761556Srgrimes		*cp = 0;
5771556Srgrimes
5781556Srgrimes		if (buf[0] == '\01' || buf[0] == '\02') {
5791556Srgrimes			if (iamremote == 0)
5801556Srgrimes				(void)write(STDERR_FILENO,
5811556Srgrimes				    buf + 1, strlen(buf + 1));
5821556Srgrimes			if (buf[0] == '\02')
5831556Srgrimes				exit(1);
5841556Srgrimes			++errs;
5851556Srgrimes			continue;
5861556Srgrimes		}
5871556Srgrimes		if (buf[0] == 'E') {
5881556Srgrimes			(void)write(rem, "", 1);
5891556Srgrimes			return;
5901556Srgrimes		}
5911556Srgrimes
5921556Srgrimes		if (ch == '\n')
5931556Srgrimes			*--cp = 0;
5941556Srgrimes
5951556Srgrimes		cp = buf;
5961556Srgrimes		if (*cp == 'T') {
5971556Srgrimes			setimes++;
5981556Srgrimes			cp++;
59913978Spst			mtime.tv_sec = strtol(cp, &cp, 10);
60013978Spst			if (!cp || *cp++ != ' ')
6011556Srgrimes				SCREWUP("mtime.sec not delimited");
60213978Spst			mtime.tv_usec = strtol(cp, &cp, 10);
60313978Spst			if (!cp || *cp++ != ' ')
6041556Srgrimes				SCREWUP("mtime.usec not delimited");
60513978Spst			atime.tv_sec = strtol(cp, &cp, 10);
60613978Spst			if (!cp || *cp++ != ' ')
6071556Srgrimes				SCREWUP("atime.sec not delimited");
60813978Spst			atime.tv_usec = strtol(cp, &cp, 10);
60913978Spst			if (!cp || *cp++ != '\0')
6101556Srgrimes				SCREWUP("atime.usec not delimited");
6111556Srgrimes			(void)write(rem, "", 1);
6121556Srgrimes			continue;
6131556Srgrimes		}
6141556Srgrimes		if (*cp != 'C' && *cp != 'D') {
6151556Srgrimes			/*
6161556Srgrimes			 * Check for the case "rcp remote:foo\* local:bar".
6171556Srgrimes			 * In this case, the line "No match." can be returned
6181556Srgrimes			 * by the shell before the rcp command on the remote is
6191556Srgrimes			 * executed so the ^Aerror_message convention isn't
6201556Srgrimes			 * followed.
6211556Srgrimes			 */
6221556Srgrimes			if (first) {
6231556Srgrimes				run_err("%s", cp);
6241556Srgrimes				exit(1);
6251556Srgrimes			}
6261556Srgrimes			SCREWUP("expected control record");
6271556Srgrimes		}
6281556Srgrimes		mode = 0;
6291556Srgrimes		for (++cp; cp < buf + 5; cp++) {
6301556Srgrimes			if (*cp < '0' || *cp > '7')
6311556Srgrimes				SCREWUP("bad mode");
6321556Srgrimes			mode = (mode << 3) | (*cp - '0');
6331556Srgrimes		}
6341556Srgrimes		if (*cp++ != ' ')
6351556Srgrimes			SCREWUP("mode not delimited");
6361556Srgrimes
6371556Srgrimes		for (size = 0; isdigit(*cp);)
6381556Srgrimes			size = size * 10 + (*cp++ - '0');
6391556Srgrimes		if (*cp++ != ' ')
6401556Srgrimes			SCREWUP("size not delimited");
6411556Srgrimes		if (targisdir) {
6421556Srgrimes			static char *namebuf;
6431556Srgrimes			static int cursize;
6441556Srgrimes			size_t need;
6451556Srgrimes
6461556Srgrimes			need = strlen(targ) + strlen(cp) + 250;
6471556Srgrimes			if (need > cursize) {
6481556Srgrimes				if (!(namebuf = malloc(need)))
6491556Srgrimes					run_err("%s", strerror(errno));
6501556Srgrimes			}
6511556Srgrimes			(void)snprintf(namebuf, need, "%s%s%s", targ,
6521556Srgrimes			    *targ ? "/" : "", cp);
6531556Srgrimes			np = namebuf;
6541556Srgrimes		} else
6551556Srgrimes			np = targ;
6561556Srgrimes		exists = stat(np, &stb) == 0;
6571556Srgrimes		if (buf[0] == 'D') {
6581556Srgrimes			int mod_flag = pflag;
6591556Srgrimes			if (exists) {
6601556Srgrimes				if (!S_ISDIR(stb.st_mode)) {
6611556Srgrimes					errno = ENOTDIR;
6621556Srgrimes					goto bad;
6631556Srgrimes				}
6641556Srgrimes				if (pflag)
6651556Srgrimes					(void)chmod(np, mode);
6661556Srgrimes			} else {
6671556Srgrimes				/* Handle copying from a read-only directory */
6681556Srgrimes				mod_flag = 1;
6691556Srgrimes				if (mkdir(np, mode | S_IRWXU) < 0)
6701556Srgrimes					goto bad;
6711556Srgrimes			}
6721556Srgrimes			vect[0] = np;
6731556Srgrimes			sink(1, vect);
6741556Srgrimes			if (setimes) {
6751556Srgrimes				setimes = 0;
6761556Srgrimes				if (utimes(np, tv) < 0)
6771556Srgrimes				    run_err("%s: set times: %s",
6781556Srgrimes					np, strerror(errno));
6791556Srgrimes			}
6801556Srgrimes			if (mod_flag)
6811556Srgrimes				(void)chmod(np, mode);
6821556Srgrimes			continue;
6831556Srgrimes		}
6841556Srgrimes		omode = mode;
6851556Srgrimes		mode |= S_IWRITE;
6861556Srgrimes		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
6871556Srgrimesbad:			run_err("%s: %s", np, strerror(errno));
6881556Srgrimes			continue;
6891556Srgrimes		}
6901556Srgrimes		(void)write(rem, "", 1);
6911556Srgrimes		if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
6921556Srgrimes			(void)close(ofd);
6931556Srgrimes			continue;
6941556Srgrimes		}
6951556Srgrimes		cp = bp->buf;
6961556Srgrimes		wrerr = NO;
6971556Srgrimes		for (count = i = 0; i < size; i += BUFSIZ) {
6981556Srgrimes			amt = BUFSIZ;
6991556Srgrimes			if (i + amt > size)
7001556Srgrimes				amt = size - i;
7011556Srgrimes			count += amt;
7021556Srgrimes			do {
7031556Srgrimes				j = read(rem, cp, amt);
7041556Srgrimes				if (j <= 0) {
7051556Srgrimes					run_err("%s", j ? strerror(errno) :
7061556Srgrimes					    "dropped connection");
7071556Srgrimes					exit(1);
7081556Srgrimes				}
7091556Srgrimes				amt -= j;
7101556Srgrimes				cp += j;
7111556Srgrimes			} while (amt > 0);
7121556Srgrimes			if (count == bp->cnt) {
7131556Srgrimes				/* Keep reading so we stay sync'd up. */
7141556Srgrimes				if (wrerr == NO) {
7151556Srgrimes					j = write(ofd, bp->buf, count);
7161556Srgrimes					if (j != count) {
7171556Srgrimes						wrerr = YES;
7188855Srgrimes						wrerrno = j >= 0 ? EIO : errno;
7191556Srgrimes					}
7201556Srgrimes				}
7211556Srgrimes				count = 0;
7221556Srgrimes				cp = bp->buf;
7231556Srgrimes			}
7241556Srgrimes		}
7251556Srgrimes		if (count != 0 && wrerr == NO &&
7261556Srgrimes		    (j = write(ofd, bp->buf, count)) != count) {
7271556Srgrimes			wrerr = YES;
7288855Srgrimes			wrerrno = j >= 0 ? EIO : errno;
7291556Srgrimes		}
7301556Srgrimes		if (ftruncate(ofd, size)) {
7311556Srgrimes			run_err("%s: truncate: %s", np, strerror(errno));
7321556Srgrimes			wrerr = DISPLAYED;
7331556Srgrimes		}
7341556Srgrimes		if (pflag) {
7351556Srgrimes			if (exists || omode != mode)
7361556Srgrimes				if (fchmod(ofd, omode))
7371556Srgrimes					run_err("%s: set mode: %s",
7381556Srgrimes					    np, strerror(errno));
7391556Srgrimes		} else {
7401556Srgrimes			if (!exists && omode != mode)
7411556Srgrimes				if (fchmod(ofd, omode & ~mask))
7421556Srgrimes					run_err("%s: set mode: %s",
7431556Srgrimes					    np, strerror(errno));
7441556Srgrimes		}
7451556Srgrimes		(void)close(ofd);
7461556Srgrimes		(void)response();
7471556Srgrimes		if (setimes && wrerr == NO) {
7481556Srgrimes			setimes = 0;
7491556Srgrimes			if (utimes(np, tv) < 0) {
7501556Srgrimes				run_err("%s: set times: %s",
7511556Srgrimes				    np, strerror(errno));
7521556Srgrimes				wrerr = DISPLAYED;
7531556Srgrimes			}
7541556Srgrimes		}
7551556Srgrimes		switch(wrerr) {
7561556Srgrimes		case YES:
7571556Srgrimes			run_err("%s: %s", np, strerror(wrerrno));
7581556Srgrimes			break;
7591556Srgrimes		case NO:
7601556Srgrimes			(void)write(rem, "", 1);
7611556Srgrimes			break;
7621556Srgrimes		case DISPLAYED:
7631556Srgrimes			break;
7641556Srgrimes		}
7651556Srgrimes	}
7661556Srgrimesscrewup:
7671556Srgrimes	run_err("protocol error: %s", why);
7681556Srgrimes	exit(1);
7691556Srgrimes}
7701556Srgrimes
7711556Srgrimes#ifdef KERBEROS
7721556Srgrimesint
7731556Srgrimeskerberos(host, bp, locuser, user)
7741556Srgrimes	char **host, *bp, *locuser, *user;
7751556Srgrimes{
7761556Srgrimes	struct servent *sp;
7771556Srgrimes
7781556Srgrimesagain:
7791556Srgrimes	if (use_kerberos) {
7801556Srgrimes		rem = KSUCCESS;
7811556Srgrimes		errno = 0;
7821556Srgrimes		if (dest_realm == NULL)
7831556Srgrimes			dest_realm = krb_realmofhost(*host);
7848855Srgrimes		rem =
7851556Srgrimes#ifdef CRYPT
7868855Srgrimes		    doencrypt ?
7871556Srgrimes			krcmd_mutual(host,
7881556Srgrimes			    port, user, bp, 0, dest_realm, &cred, schedule) :
7891556Srgrimes#endif
7901556Srgrimes			krcmd(host, port, user, bp, 0, dest_realm);
7911556Srgrimes
7921556Srgrimes		if (rem < 0) {
7931556Srgrimes			use_kerberos = 0;
7941556Srgrimes			if ((sp = getservbyname("shell", "tcp")) == NULL)
7951556Srgrimes				errx(1, "unknown service shell/tcp");
7961556Srgrimes			if (errno == ECONNREFUSED)
7971556Srgrimes			    oldw("remote host doesn't support Kerberos");
7981556Srgrimes			else if (errno == ENOENT)
7991556Srgrimes			    oldw("can't provide Kerberos authentication data");
8001556Srgrimes			port = sp->s_port;
8011556Srgrimes			goto again;
8021556Srgrimes		}
8031556Srgrimes	} else {
8041556Srgrimes#ifdef CRYPT
8051556Srgrimes		if (doencrypt)
8061556Srgrimes			errx(1,
8071556Srgrimes			   "the -x option requires Kerberos authentication");
8081556Srgrimes#endif
8091556Srgrimes		rem = rcmd(host, port, locuser, user, bp, 0);
8101556Srgrimes	}
8111556Srgrimes	return (rem);
8121556Srgrimes}
8131556Srgrimes#endif /* KERBEROS */
8141556Srgrimes
8151556Srgrimesint
8161556Srgrimesresponse()
8171556Srgrimes{
8181556Srgrimes	char ch, *cp, resp, rbuf[BUFSIZ];
8191556Srgrimes
8201556Srgrimes	if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
8211556Srgrimes		lostconn(0);
8221556Srgrimes
8231556Srgrimes	cp = rbuf;
8241556Srgrimes	switch(resp) {
8251556Srgrimes	case 0:				/* ok */
8261556Srgrimes		return (0);
8271556Srgrimes	default:
8281556Srgrimes		*cp++ = resp;
8291556Srgrimes		/* FALLTHROUGH */
8301556Srgrimes	case 1:				/* error, followed by error msg */
8311556Srgrimes	case 2:				/* fatal error, "" */
8321556Srgrimes		do {
8331556Srgrimes			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
8341556Srgrimes				lostconn(0);
8351556Srgrimes			*cp++ = ch;
8361556Srgrimes		} while (cp < &rbuf[BUFSIZ] && ch != '\n');
8371556Srgrimes
8381556Srgrimes		if (!iamremote)
8391556Srgrimes			(void)write(STDERR_FILENO, rbuf, cp - rbuf);
8401556Srgrimes		++errs;
8411556Srgrimes		if (resp == 1)
8421556Srgrimes			return (-1);
8431556Srgrimes		exit(1);
8441556Srgrimes	}
8451556Srgrimes	/* NOTREACHED */
8461556Srgrimes}
8471556Srgrimes
8481556Srgrimesvoid
8491556Srgrimesusage()
8501556Srgrimes{
8511556Srgrimes#ifdef KERBEROS
8521556Srgrimes#ifdef CRYPT
8531556Srgrimes	(void)fprintf(stderr, "%s\n\t%s\n",
8541556Srgrimes	    "usage: rcp [-Kpx] [-k realm] f1 f2",
8551556Srgrimes	    "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
8561556Srgrimes#else
8571556Srgrimes	(void)fprintf(stderr, "%s\n\t%s\n",
8581556Srgrimes	    "usage: rcp [-Kp] [-k realm] f1 f2",
8591556Srgrimes	    "or: rcp [-Kpr] [-k realm] f1 ... fn directory");
8601556Srgrimes#endif
8611556Srgrimes#else
8621556Srgrimes	(void)fprintf(stderr,
8631556Srgrimes	    "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
8641556Srgrimes#endif
8651556Srgrimes	exit(1);
8661556Srgrimes}
8671556Srgrimes
8681556Srgrimes#if __STDC__
8691556Srgrimes#include <stdarg.h>
8701556Srgrimes#else
8711556Srgrimes#include <varargs.h>
8721556Srgrimes#endif
8731556Srgrimes
8741556Srgrimes#ifdef KERBEROS
8751556Srgrimesvoid
8761556Srgrimes#if __STDC__
8771556Srgrimesoldw(const char *fmt, ...)
8781556Srgrimes#else
8791556Srgrimesoldw(fmt, va_alist)
8801556Srgrimes	char *fmt;
8811556Srgrimes        va_dcl
8821556Srgrimes#endif
8831556Srgrimes{
8841556Srgrimes	va_list ap;
8851556Srgrimes#if __STDC__
8861556Srgrimes	va_start(ap, fmt);
8871556Srgrimes#else
8881556Srgrimes	va_start(ap);
8891556Srgrimes#endif
8901556Srgrimes	(void)fprintf(stderr, "rcp: ");
8911556Srgrimes	(void)vfprintf(stderr, fmt, ap);
8921556Srgrimes	(void)fprintf(stderr, ", using standard rcp\n");
8931556Srgrimes	va_end(ap);
8941556Srgrimes}
8951556Srgrimes#endif
8961556Srgrimes
8971556Srgrimesvoid
8981556Srgrimes#if __STDC__
8991556Srgrimesrun_err(const char *fmt, ...)
9001556Srgrimes#else
9011556Srgrimesrun_err(fmt, va_alist)
9021556Srgrimes	char *fmt;
9031556Srgrimes        va_dcl
9041556Srgrimes#endif
9051556Srgrimes{
9061556Srgrimes	static FILE *fp;
9071556Srgrimes	va_list ap;
9081556Srgrimes#if __STDC__
9091556Srgrimes	va_start(ap, fmt);
9101556Srgrimes#else
9111556Srgrimes	va_start(ap);
9121556Srgrimes#endif
9131556Srgrimes
9141556Srgrimes	++errs;
9151556Srgrimes	if (fp == NULL && !(fp = fdopen(rem, "w")))
9161556Srgrimes		return;
9171556Srgrimes	(void)fprintf(fp, "%c", 0x01);
9181556Srgrimes	(void)fprintf(fp, "rcp: ");
9191556Srgrimes	(void)vfprintf(fp, fmt, ap);
9201556Srgrimes	(void)fprintf(fp, "\n");
9211556Srgrimes	(void)fflush(fp);
9221556Srgrimes
9231556Srgrimes	if (!iamremote)
9241556Srgrimes		vwarnx(fmt, ap);
9251556Srgrimes
9261556Srgrimes	va_end(ap);
9271556Srgrimes}
928