Home | History | Annotate | Line # | Download | only in rcp
rcp.c revision 1.1.1.2
      1      1.1      cgd /*
      2  1.1.1.2  mycroft  * Copyright (c) 1983, 1990, 1992, 1993
      3  1.1.1.2  mycroft  *	The Regents of the University of California.  All rights reserved.
      4      1.1      cgd  *
      5      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1      cgd  * modification, are permitted provided that the following conditions
      7      1.1      cgd  * are met:
      8      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1      cgd  *    must display the following acknowledgement:
     15      1.1      cgd  *	This product includes software developed by the University of
     16      1.1      cgd  *	California, Berkeley and its contributors.
     17      1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1      cgd  *    may be used to endorse or promote products derived from this software
     19      1.1      cgd  *    without specific prior written permission.
     20      1.1      cgd  *
     21      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1      cgd  * SUCH DAMAGE.
     32      1.1      cgd  */
     33      1.1      cgd 
     34      1.1      cgd #ifndef lint
     35  1.1.1.2  mycroft static char copyright[] =
     36  1.1.1.2  mycroft "@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
     37  1.1.1.2  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1      cgd #endif /* not lint */
     39      1.1      cgd 
     40      1.1      cgd #ifndef lint
     41  1.1.1.2  mycroft static char sccsid[] = "@(#)rcp.c	8.2 (Berkeley) 4/2/94";
     42      1.1      cgd #endif /* not lint */
     43      1.1      cgd 
     44      1.1      cgd #include <sys/param.h>
     45      1.1      cgd #include <sys/stat.h>
     46      1.1      cgd #include <sys/time.h>
     47      1.1      cgd #include <sys/socket.h>
     48      1.1      cgd #include <netinet/in.h>
     49      1.1      cgd #include <netinet/in_systm.h>
     50      1.1      cgd #include <netinet/ip.h>
     51  1.1.1.2  mycroft 
     52  1.1.1.2  mycroft #include <ctype.h>
     53      1.1      cgd #include <dirent.h>
     54  1.1.1.2  mycroft #include <err.h>
     55  1.1.1.2  mycroft #include <errno.h>
     56      1.1      cgd #include <fcntl.h>
     57      1.1      cgd #include <netdb.h>
     58  1.1.1.2  mycroft #include <pwd.h>
     59  1.1.1.2  mycroft #include <signal.h>
     60      1.1      cgd #include <stdio.h>
     61      1.1      cgd #include <stdlib.h>
     62      1.1      cgd #include <string.h>
     63  1.1.1.2  mycroft #include <string.h>
     64  1.1.1.2  mycroft #include <unistd.h>
     65  1.1.1.2  mycroft 
     66      1.1      cgd #include "pathnames.h"
     67  1.1.1.2  mycroft #include "extern.h"
     68      1.1      cgd 
     69      1.1      cgd #ifdef KERBEROS
     70      1.1      cgd #include <kerberosIV/des.h>
     71      1.1      cgd #include <kerberosIV/krb.h>
     72  1.1.1.2  mycroft 
     73      1.1      cgd char	dst_realm_buf[REALM_SZ];
     74      1.1      cgd char	*dest_realm = NULL;
     75      1.1      cgd int	use_kerberos = 1;
     76      1.1      cgd CREDENTIALS 	cred;
     77      1.1      cgd Key_schedule	schedule;
     78      1.1      cgd extern	char	*krb_realmofhost();
     79      1.1      cgd #ifdef CRYPT
     80      1.1      cgd int	doencrypt = 0;
     81  1.1.1.2  mycroft #define	OPTIONS	"dfKk:prtx"
     82      1.1      cgd #else
     83  1.1.1.2  mycroft #define	OPTIONS	"dfKk:prt"
     84      1.1      cgd #endif
     85      1.1      cgd #else
     86      1.1      cgd #define	OPTIONS "dfprt"
     87      1.1      cgd #endif
     88      1.1      cgd 
     89      1.1      cgd struct passwd *pwd;
     90      1.1      cgd u_short	port;
     91      1.1      cgd uid_t	userid;
     92      1.1      cgd int errs, rem;
     93      1.1      cgd int pflag, iamremote, iamrecursive, targetshouldbedirectory;
     94      1.1      cgd 
     95      1.1      cgd #define	CMDNEEDS	64
     96      1.1      cgd char cmd[CMDNEEDS];		/* must hold "rcp -r -p -d\0" */
     97      1.1      cgd 
     98  1.1.1.2  mycroft #ifdef KERBEROS
     99  1.1.1.2  mycroft int	 kerberos __P((char **, char *, char *, char *));
    100  1.1.1.2  mycroft void	 oldw __P((const char *, ...));
    101  1.1.1.2  mycroft #endif
    102  1.1.1.2  mycroft int	 response __P((void));
    103  1.1.1.2  mycroft void	 rsource __P((char *, struct stat *));
    104  1.1.1.2  mycroft void	 sink __P((int, char *[]));
    105  1.1.1.2  mycroft void	 source __P((int, char *[]));
    106  1.1.1.2  mycroft void	 tolocal __P((int, char *[]));
    107  1.1.1.2  mycroft void	 toremote __P((char *, int, char *[]));
    108  1.1.1.2  mycroft void	 usage __P((void));
    109      1.1      cgd 
    110  1.1.1.2  mycroft int
    111      1.1      cgd main(argc, argv)
    112      1.1      cgd 	int argc;
    113  1.1.1.2  mycroft 	char *argv[];
    114      1.1      cgd {
    115      1.1      cgd 	struct servent *sp;
    116      1.1      cgd 	int ch, fflag, tflag;
    117  1.1.1.2  mycroft 	char *targ, *shell;
    118      1.1      cgd 
    119      1.1      cgd 	fflag = tflag = 0;
    120      1.1      cgd 	while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
    121  1.1.1.2  mycroft 		switch(ch) {			/* User-visible flags. */
    122  1.1.1.2  mycroft 		case 'K':
    123  1.1.1.2  mycroft #ifdef KERBEROS
    124  1.1.1.2  mycroft 			use_kerberos = 0;
    125  1.1.1.2  mycroft #endif
    126      1.1      cgd 			break;
    127      1.1      cgd #ifdef	KERBEROS
    128      1.1      cgd 		case 'k':
    129      1.1      cgd 			dest_realm = dst_realm_buf;
    130  1.1.1.2  mycroft 			(void)strncpy(dst_realm_buf, optarg, REALM_SZ);
    131      1.1      cgd 			break;
    132      1.1      cgd #ifdef CRYPT
    133      1.1      cgd 		case 'x':
    134      1.1      cgd 			doencrypt = 1;
    135      1.1      cgd 			/* des_set_key(cred.session, schedule); */
    136      1.1      cgd 			break;
    137      1.1      cgd #endif
    138      1.1      cgd #endif
    139  1.1.1.2  mycroft 		case 'p':
    140  1.1.1.2  mycroft 			pflag = 1;
    141  1.1.1.2  mycroft 			break;
    142  1.1.1.2  mycroft 		case 'r':
    143  1.1.1.2  mycroft 			iamrecursive = 1;
    144  1.1.1.2  mycroft 			break;
    145  1.1.1.2  mycroft 						/* Server options. */
    146      1.1      cgd 		case 'd':
    147      1.1      cgd 			targetshouldbedirectory = 1;
    148      1.1      cgd 			break;
    149      1.1      cgd 		case 'f':			/* "from" */
    150      1.1      cgd 			iamremote = 1;
    151      1.1      cgd 			fflag = 1;
    152      1.1      cgd 			break;
    153      1.1      cgd 		case 't':			/* "to" */
    154      1.1      cgd 			iamremote = 1;
    155      1.1      cgd 			tflag = 1;
    156      1.1      cgd 			break;
    157      1.1      cgd 		case '?':
    158      1.1      cgd 		default:
    159      1.1      cgd 			usage();
    160      1.1      cgd 		}
    161      1.1      cgd 	argc -= optind;
    162      1.1      cgd 	argv += optind;
    163      1.1      cgd 
    164      1.1      cgd #ifdef KERBEROS
    165  1.1.1.2  mycroft 	if (use_kerberos) {
    166      1.1      cgd #ifdef CRYPT
    167  1.1.1.2  mycroft 		shell = doencrypt ? "ekshell" : "kshell";
    168      1.1      cgd #else
    169  1.1.1.2  mycroft 		shell = "kshell";
    170      1.1      cgd #endif
    171  1.1.1.2  mycroft 		if ((sp = getservbyname(shell, "tcp")) == NULL) {
    172  1.1.1.2  mycroft 			use_kerberos = 0;
    173  1.1.1.2  mycroft 			oldw("can't get entry for %s/tcp service", shell);
    174  1.1.1.2  mycroft 			sp = getservbyname(shell = "shell", "tcp");
    175  1.1.1.2  mycroft 		}
    176  1.1.1.2  mycroft 	} else
    177      1.1      cgd 		sp = getservbyname(shell = "shell", "tcp");
    178      1.1      cgd #else
    179      1.1      cgd 	sp = getservbyname(shell = "shell", "tcp");
    180      1.1      cgd #endif
    181  1.1.1.2  mycroft 	if (sp == NULL)
    182  1.1.1.2  mycroft 		errx(1, "%s/tcp: unknown service", shell);
    183      1.1      cgd 	port = sp->s_port;
    184      1.1      cgd 
    185  1.1.1.2  mycroft 	if ((pwd = getpwuid(userid = getuid())) == NULL)
    186  1.1.1.2  mycroft 		errx(1, "unknown user %d", (int)userid);
    187      1.1      cgd 
    188  1.1.1.2  mycroft 	rem = STDIN_FILENO;		/* XXX */
    189  1.1.1.2  mycroft 
    190  1.1.1.2  mycroft 	if (fflag) {			/* Follow "protocol", send data. */
    191      1.1      cgd 		(void)response();
    192      1.1      cgd 		(void)setuid(userid);
    193      1.1      cgd 		source(argc, argv);
    194      1.1      cgd 		exit(errs);
    195      1.1      cgd 	}
    196      1.1      cgd 
    197  1.1.1.2  mycroft 	if (tflag) {			/* Receive data. */
    198      1.1      cgd 		(void)setuid(userid);
    199      1.1      cgd 		sink(argc, argv);
    200      1.1      cgd 		exit(errs);
    201      1.1      cgd 	}
    202      1.1      cgd 
    203      1.1      cgd 	if (argc < 2)
    204      1.1      cgd 		usage();
    205      1.1      cgd 	if (argc > 2)
    206      1.1      cgd 		targetshouldbedirectory = 1;
    207      1.1      cgd 
    208      1.1      cgd 	rem = -1;
    209  1.1.1.2  mycroft 	/* Command to be executed on remote system using "rsh". */
    210      1.1      cgd #ifdef	KERBEROS
    211      1.1      cgd 	(void)snprintf(cmd, sizeof(cmd),
    212      1.1      cgd 	    "rcp%s%s%s%s", iamrecursive ? " -r" : "",
    213      1.1      cgd #ifdef CRYPT
    214  1.1.1.2  mycroft 	    (doencrypt && use_kerberos ? " -x" : ""),
    215      1.1      cgd #else
    216      1.1      cgd 	    "",
    217      1.1      cgd #endif
    218      1.1      cgd 	    pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
    219      1.1      cgd #else
    220      1.1      cgd 	(void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
    221      1.1      cgd 	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
    222      1.1      cgd 	    targetshouldbedirectory ? " -d" : "");
    223      1.1      cgd #endif
    224      1.1      cgd 
    225      1.1      cgd 	(void)signal(SIGPIPE, lostconn);
    226      1.1      cgd 
    227  1.1.1.2  mycroft 	if (targ = colon(argv[argc - 1]))	/* Dest is remote host. */
    228  1.1.1.2  mycroft 		toremote(targ, argc, argv);
    229      1.1      cgd 	else {
    230  1.1.1.2  mycroft 		tolocal(argc, argv);		/* Dest is local host. */
    231      1.1      cgd 		if (targetshouldbedirectory)
    232      1.1      cgd 			verifydir(argv[argc - 1]);
    233      1.1      cgd 	}
    234      1.1      cgd 	exit(errs);
    235      1.1      cgd }
    236      1.1      cgd 
    237  1.1.1.2  mycroft void
    238      1.1      cgd toremote(targ, argc, argv)
    239  1.1.1.2  mycroft 	char *targ, *argv[];
    240      1.1      cgd 	int argc;
    241      1.1      cgd {
    242      1.1      cgd 	int i, len, tos;
    243      1.1      cgd 	char *bp, *host, *src, *suser, *thost, *tuser;
    244      1.1      cgd 
    245      1.1      cgd 	*targ++ = 0;
    246      1.1      cgd 	if (*targ == 0)
    247      1.1      cgd 		targ = ".";
    248      1.1      cgd 
    249  1.1.1.2  mycroft 	if (thost = strchr(argv[argc - 1], '@')) {
    250      1.1      cgd 		/* user@host */
    251      1.1      cgd 		*thost++ = 0;
    252      1.1      cgd 		tuser = argv[argc - 1];
    253      1.1      cgd 		if (*tuser == '\0')
    254      1.1      cgd 			tuser = NULL;
    255      1.1      cgd 		else if (!okname(tuser))
    256      1.1      cgd 			exit(1);
    257      1.1      cgd 	} else {
    258      1.1      cgd 		thost = argv[argc - 1];
    259      1.1      cgd 		tuser = NULL;
    260      1.1      cgd 	}
    261      1.1      cgd 
    262      1.1      cgd 	for (i = 0; i < argc - 1; i++) {
    263      1.1      cgd 		src = colon(argv[i]);
    264      1.1      cgd 		if (src) {			/* remote to remote */
    265      1.1      cgd 			*src++ = 0;
    266      1.1      cgd 			if (*src == 0)
    267      1.1      cgd 				src = ".";
    268  1.1.1.2  mycroft 			host = strchr(argv[i], '@');
    269      1.1      cgd 			len = strlen(_PATH_RSH) + strlen(argv[i]) +
    270      1.1      cgd 			    strlen(src) + (tuser ? strlen(tuser) : 0) +
    271      1.1      cgd 			    strlen(thost) + strlen(targ) + CMDNEEDS + 20;
    272      1.1      cgd 			if (!(bp = malloc(len)))
    273  1.1.1.2  mycroft 				err(1, NULL);
    274      1.1      cgd 			if (host) {
    275      1.1      cgd 				*host++ = 0;
    276      1.1      cgd 				suser = argv[i];
    277      1.1      cgd 				if (*suser == '\0')
    278      1.1      cgd 					suser = pwd->pw_name;
    279      1.1      cgd 				else if (!okname(suser))
    280      1.1      cgd 					continue;
    281      1.1      cgd 				(void)snprintf(bp, len,
    282      1.1      cgd 				    "%s %s -l %s -n %s %s '%s%s%s:%s'",
    283      1.1      cgd 				    _PATH_RSH, host, suser, cmd, src,
    284      1.1      cgd 				    tuser ? tuser : "", tuser ? "@" : "",
    285      1.1      cgd 				    thost, targ);
    286      1.1      cgd 			} else
    287      1.1      cgd 				(void)snprintf(bp, len,
    288  1.1.1.2  mycroft 				    "exec %s %s -n %s %s '%s%s%s:%s'",
    289      1.1      cgd 				    _PATH_RSH, argv[i], cmd, src,
    290      1.1      cgd 				    tuser ? tuser : "", tuser ? "@" : "",
    291      1.1      cgd 				    thost, targ);
    292  1.1.1.2  mycroft 			(void)susystem(bp, userid);
    293      1.1      cgd 			(void)free(bp);
    294      1.1      cgd 		} else {			/* local to remote */
    295      1.1      cgd 			if (rem == -1) {
    296      1.1      cgd 				len = strlen(targ) + CMDNEEDS + 20;
    297      1.1      cgd 				if (!(bp = malloc(len)))
    298  1.1.1.2  mycroft 					err(1, NULL);
    299      1.1      cgd 				(void)snprintf(bp, len, "%s -t %s", cmd, targ);
    300      1.1      cgd 				host = thost;
    301      1.1      cgd #ifdef KERBEROS
    302      1.1      cgd 				if (use_kerberos)
    303      1.1      cgd 					rem = kerberos(&host, bp,
    304      1.1      cgd 					    pwd->pw_name,
    305      1.1      cgd 					    tuser ? tuser : pwd->pw_name);
    306      1.1      cgd 				else
    307      1.1      cgd #endif
    308      1.1      cgd 					rem = rcmd(&host, port, pwd->pw_name,
    309      1.1      cgd 					    tuser ? tuser : pwd->pw_name,
    310      1.1      cgd 					    bp, 0);
    311      1.1      cgd 				if (rem < 0)
    312      1.1      cgd 					exit(1);
    313      1.1      cgd 				tos = IPTOS_THROUGHPUT;
    314      1.1      cgd 				if (setsockopt(rem, IPPROTO_IP, IP_TOS,
    315  1.1.1.2  mycroft 				    &tos, sizeof(int)) < 0)
    316  1.1.1.2  mycroft 					warn("TOS (ignored)");
    317      1.1      cgd 				if (response() < 0)
    318      1.1      cgd 					exit(1);
    319      1.1      cgd 				(void)free(bp);
    320      1.1      cgd 				(void)setuid(userid);
    321      1.1      cgd 			}
    322      1.1      cgd 			source(1, argv+i);
    323      1.1      cgd 		}
    324      1.1      cgd 	}
    325      1.1      cgd }
    326      1.1      cgd 
    327  1.1.1.2  mycroft void
    328      1.1      cgd tolocal(argc, argv)
    329      1.1      cgd 	int argc;
    330  1.1.1.2  mycroft 	char *argv[];
    331      1.1      cgd {
    332      1.1      cgd 	int i, len, tos;
    333      1.1      cgd 	char *bp, *host, *src, *suser;
    334      1.1      cgd 
    335      1.1      cgd 	for (i = 0; i < argc - 1; i++) {
    336  1.1.1.2  mycroft 		if (!(src = colon(argv[i]))) {		/* Local to local. */
    337      1.1      cgd 			len = strlen(_PATH_CP) + strlen(argv[i]) +
    338      1.1      cgd 			    strlen(argv[argc - 1]) + 20;
    339      1.1      cgd 			if (!(bp = malloc(len)))
    340  1.1.1.2  mycroft 				err(1, NULL);
    341  1.1.1.2  mycroft 			(void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
    342      1.1      cgd 			    iamrecursive ? " -r" : "", pflag ? " -p" : "",
    343      1.1      cgd 			    argv[i], argv[argc - 1]);
    344  1.1.1.2  mycroft 			if (susystem(bp, userid))
    345  1.1.1.2  mycroft 				++errs;
    346      1.1      cgd 			(void)free(bp);
    347      1.1      cgd 			continue;
    348      1.1      cgd 		}
    349      1.1      cgd 		*src++ = 0;
    350      1.1      cgd 		if (*src == 0)
    351      1.1      cgd 			src = ".";
    352  1.1.1.2  mycroft 		if ((host = strchr(argv[i], '@')) == NULL) {
    353  1.1.1.2  mycroft 			host = argv[i];
    354  1.1.1.2  mycroft 			suser = pwd->pw_name;
    355  1.1.1.2  mycroft 		} else {
    356      1.1      cgd 			*host++ = 0;
    357      1.1      cgd 			suser = argv[i];
    358      1.1      cgd 			if (*suser == '\0')
    359      1.1      cgd 				suser = pwd->pw_name;
    360      1.1      cgd 			else if (!okname(suser))
    361      1.1      cgd 				continue;
    362      1.1      cgd 		}
    363      1.1      cgd 		len = strlen(src) + CMDNEEDS + 20;
    364  1.1.1.2  mycroft 		if ((bp = malloc(len)) == NULL)
    365  1.1.1.2  mycroft 			err(1, NULL);
    366      1.1      cgd 		(void)snprintf(bp, len, "%s -f %s", cmd, src);
    367  1.1.1.2  mycroft 		rem =
    368      1.1      cgd #ifdef KERBEROS
    369  1.1.1.2  mycroft 		    use_kerberos ?
    370  1.1.1.2  mycroft 			kerberos(&host, bp, pwd->pw_name, suser) :
    371      1.1      cgd #endif
    372  1.1.1.2  mycroft 			rcmd(&host, port, pwd->pw_name, suser, bp, 0);
    373      1.1      cgd 		(void)free(bp);
    374  1.1.1.2  mycroft 		if (rem < 0) {
    375  1.1.1.2  mycroft 			++errs;
    376      1.1      cgd 			continue;
    377  1.1.1.2  mycroft 		}
    378      1.1      cgd 		(void)seteuid(userid);
    379      1.1      cgd 		tos = IPTOS_THROUGHPUT;
    380  1.1.1.2  mycroft 		if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
    381  1.1.1.2  mycroft 			warn("TOS (ignored)");
    382      1.1      cgd 		sink(1, argv + argc - 1);
    383      1.1      cgd 		(void)seteuid(0);
    384      1.1      cgd 		(void)close(rem);
    385      1.1      cgd 		rem = -1;
    386      1.1      cgd 	}
    387      1.1      cgd }
    388      1.1      cgd 
    389  1.1.1.2  mycroft void
    390      1.1      cgd source(argc, argv)
    391      1.1      cgd 	int argc;
    392  1.1.1.2  mycroft 	char *argv[];
    393      1.1      cgd {
    394      1.1      cgd 	struct stat stb;
    395      1.1      cgd 	static BUF buffer;
    396      1.1      cgd 	BUF *bp;
    397      1.1      cgd 	off_t i;
    398  1.1.1.2  mycroft 	int amt, fd, haderr, indx, result;
    399      1.1      cgd 	char *last, *name, buf[BUFSIZ];
    400      1.1      cgd 
    401  1.1.1.2  mycroft 	for (indx = 0; indx < argc; ++indx) {
    402  1.1.1.2  mycroft                 name = argv[indx];
    403  1.1.1.2  mycroft 		if ((fd = open(name, O_RDONLY, 0)) < 0)
    404  1.1.1.2  mycroft 			goto syserr;
    405  1.1.1.2  mycroft 		if (fstat(fd, &stb)) {
    406  1.1.1.2  mycroft syserr:			run_err("%s: %s", name, strerror(errno));
    407  1.1.1.2  mycroft 			goto next;
    408      1.1      cgd 		}
    409  1.1.1.2  mycroft 		switch (stb.st_mode & S_IFMT) {
    410      1.1      cgd 		case S_IFREG:
    411      1.1      cgd 			break;
    412      1.1      cgd 		case S_IFDIR:
    413      1.1      cgd 			if (iamrecursive) {
    414      1.1      cgd 				rsource(name, &stb);
    415  1.1.1.2  mycroft 				goto next;
    416      1.1      cgd 			}
    417      1.1      cgd 			/* FALLTHROUGH */
    418      1.1      cgd 		default:
    419  1.1.1.2  mycroft 			run_err("%s: not a regular file", name);
    420  1.1.1.2  mycroft 			goto next;
    421      1.1      cgd 		}
    422  1.1.1.2  mycroft 		if ((last = strrchr(name, '/')) == NULL)
    423      1.1      cgd 			last = name;
    424      1.1      cgd 		else
    425  1.1.1.2  mycroft 			++last;
    426      1.1      cgd 		if (pflag) {
    427      1.1      cgd 			/*
    428      1.1      cgd 			 * Make it compatible with possible future
    429      1.1      cgd 			 * versions expecting microseconds.
    430      1.1      cgd 			 */
    431  1.1.1.2  mycroft 			(void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
    432  1.1.1.2  mycroft 			    stb.st_mtimespec.ts_sec, stb.st_atimespec.ts_sec);
    433  1.1.1.2  mycroft 			(void)write(rem, buf, strlen(buf));
    434  1.1.1.2  mycroft 			if (response() < 0)
    435  1.1.1.2  mycroft 				goto next;
    436  1.1.1.2  mycroft 		}
    437  1.1.1.2  mycroft #define	MODEMASK	(S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
    438  1.1.1.2  mycroft 		(void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
    439  1.1.1.2  mycroft 		    stb.st_mode & MODEMASK, stb.st_size, last);
    440  1.1.1.2  mycroft 		(void)write(rem, buf, strlen(buf));
    441  1.1.1.2  mycroft 		if (response() < 0)
    442  1.1.1.2  mycroft 			goto next;
    443  1.1.1.2  mycroft 		if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
    444  1.1.1.2  mycroft next:			(void)close(fd);
    445      1.1      cgd 			continue;
    446      1.1      cgd 		}
    447  1.1.1.2  mycroft 
    448  1.1.1.2  mycroft 		/* Keep writing after an error so that we stay sync'd up. */
    449  1.1.1.2  mycroft 		for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
    450      1.1      cgd 			amt = bp->cnt;
    451      1.1      cgd 			if (i + amt > stb.st_size)
    452      1.1      cgd 				amt = stb.st_size - i;
    453  1.1.1.2  mycroft 			if (!haderr) {
    454  1.1.1.2  mycroft 				result = read(fd, bp->buf, amt);
    455  1.1.1.2  mycroft 				if (result != amt)
    456  1.1.1.2  mycroft 					haderr = result >= 0 ? EIO : errno;
    457  1.1.1.2  mycroft 			}
    458  1.1.1.2  mycroft 			if (haderr)
    459  1.1.1.2  mycroft 				(void)write(rem, bp->buf, amt);
    460  1.1.1.2  mycroft 			else {
    461  1.1.1.2  mycroft 				result = write(rem, bp->buf, amt);
    462  1.1.1.2  mycroft 				if (result != amt)
    463  1.1.1.2  mycroft 					haderr = result >= 0 ? EIO : errno;
    464  1.1.1.2  mycroft 			}
    465      1.1      cgd 		}
    466  1.1.1.2  mycroft 		if (close(fd) && !haderr)
    467  1.1.1.2  mycroft 			haderr = errno;
    468  1.1.1.2  mycroft 		if (!haderr)
    469      1.1      cgd 			(void)write(rem, "", 1);
    470      1.1      cgd 		else
    471  1.1.1.2  mycroft 			run_err("%s: %s", name, strerror(haderr));
    472      1.1      cgd 		(void)response();
    473      1.1      cgd 	}
    474      1.1      cgd }
    475      1.1      cgd 
    476  1.1.1.2  mycroft void
    477      1.1      cgd rsource(name, statp)
    478      1.1      cgd 	char *name;
    479      1.1      cgd 	struct stat *statp;
    480      1.1      cgd {
    481      1.1      cgd 	DIR *dirp;
    482      1.1      cgd 	struct dirent *dp;
    483      1.1      cgd 	char *last, *vect[1], path[MAXPATHLEN];
    484      1.1      cgd 
    485      1.1      cgd 	if (!(dirp = opendir(name))) {
    486  1.1.1.2  mycroft 		run_err("%s: %s", name, strerror(errno));
    487      1.1      cgd 		return;
    488      1.1      cgd 	}
    489  1.1.1.2  mycroft 	last = strrchr(name, '/');
    490      1.1      cgd 	if (last == 0)
    491      1.1      cgd 		last = name;
    492      1.1      cgd 	else
    493      1.1      cgd 		last++;
    494      1.1      cgd 	if (pflag) {
    495  1.1.1.2  mycroft 		(void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
    496  1.1.1.2  mycroft 		    statp->st_mtimespec.ts_sec, statp->st_atimespec.ts_sec);
    497  1.1.1.2  mycroft 		(void)write(rem, path, strlen(path));
    498      1.1      cgd 		if (response() < 0) {
    499      1.1      cgd 			closedir(dirp);
    500      1.1      cgd 			return;
    501      1.1      cgd 		}
    502      1.1      cgd 	}
    503      1.1      cgd 	(void)snprintf(path, sizeof(path),
    504  1.1.1.2  mycroft 	    "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
    505  1.1.1.2  mycroft 	(void)write(rem, path, strlen(path));
    506      1.1      cgd 	if (response() < 0) {
    507      1.1      cgd 		closedir(dirp);
    508      1.1      cgd 		return;
    509      1.1      cgd 	}
    510      1.1      cgd 	while (dp = readdir(dirp)) {
    511      1.1      cgd 		if (dp->d_ino == 0)
    512      1.1      cgd 			continue;
    513      1.1      cgd 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    514      1.1      cgd 			continue;
    515      1.1      cgd 		if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) {
    516  1.1.1.2  mycroft 			run_err("%s/%s: name too long", name, dp->d_name);
    517      1.1      cgd 			continue;
    518      1.1      cgd 		}
    519      1.1      cgd 		(void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
    520      1.1      cgd 		vect[0] = path;
    521      1.1      cgd 		source(1, vect);
    522      1.1      cgd 	}
    523  1.1.1.2  mycroft 	(void)closedir(dirp);
    524      1.1      cgd 	(void)write(rem, "E\n", 2);
    525      1.1      cgd 	(void)response();
    526      1.1      cgd }
    527      1.1      cgd 
    528      1.1      cgd void
    529      1.1      cgd sink(argc, argv)
    530      1.1      cgd 	int argc;
    531  1.1.1.2  mycroft 	char *argv[];
    532      1.1      cgd {
    533      1.1      cgd 	static BUF buffer;
    534      1.1      cgd 	struct stat stb;
    535      1.1      cgd 	struct timeval tv[2];
    536      1.1      cgd 	enum { YES, NO, DISPLAYED } wrerr;
    537  1.1.1.2  mycroft 	BUF *bp;
    538      1.1      cgd 	off_t i, j;
    539  1.1.1.2  mycroft 	int amt, count, exists, first, mask, mode, ofd, omode;
    540  1.1.1.2  mycroft 	int setimes, size, targisdir, wrerrno;
    541  1.1.1.2  mycroft 	char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
    542      1.1      cgd 
    543      1.1      cgd #define	atime	tv[0]
    544      1.1      cgd #define	mtime	tv[1]
    545      1.1      cgd #define	SCREWUP(str)	{ why = str; goto screwup; }
    546      1.1      cgd 
    547      1.1      cgd 	setimes = targisdir = 0;
    548      1.1      cgd 	mask = umask(0);
    549      1.1      cgd 	if (!pflag)
    550      1.1      cgd 		(void)umask(mask);
    551      1.1      cgd 	if (argc != 1) {
    552  1.1.1.2  mycroft 		run_err("ambiguous target");
    553      1.1      cgd 		exit(1);
    554      1.1      cgd 	}
    555      1.1      cgd 	targ = *argv;
    556      1.1      cgd 	if (targetshouldbedirectory)
    557      1.1      cgd 		verifydir(targ);
    558      1.1      cgd 	(void)write(rem, "", 1);
    559  1.1.1.2  mycroft 	if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
    560      1.1      cgd 		targisdir = 1;
    561      1.1      cgd 	for (first = 1;; first = 0) {
    562      1.1      cgd 		cp = buf;
    563      1.1      cgd 		if (read(rem, cp, 1) <= 0)
    564      1.1      cgd 			return;
    565      1.1      cgd 		if (*cp++ == '\n')
    566      1.1      cgd 			SCREWUP("unexpected <newline>");
    567      1.1      cgd 		do {
    568      1.1      cgd 			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
    569      1.1      cgd 				SCREWUP("lost connection");
    570      1.1      cgd 			*cp++ = ch;
    571      1.1      cgd 		} while (cp < &buf[BUFSIZ - 1] && ch != '\n');
    572      1.1      cgd 		*cp = 0;
    573      1.1      cgd 
    574      1.1      cgd 		if (buf[0] == '\01' || buf[0] == '\02') {
    575      1.1      cgd 			if (iamremote == 0)
    576  1.1.1.2  mycroft 				(void)write(STDERR_FILENO,
    577  1.1.1.2  mycroft 				    buf + 1, strlen(buf + 1));
    578      1.1      cgd 			if (buf[0] == '\02')
    579      1.1      cgd 				exit(1);
    580  1.1.1.2  mycroft 			++errs;
    581      1.1      cgd 			continue;
    582      1.1      cgd 		}
    583      1.1      cgd 		if (buf[0] == 'E') {
    584      1.1      cgd 			(void)write(rem, "", 1);
    585      1.1      cgd 			return;
    586      1.1      cgd 		}
    587      1.1      cgd 
    588      1.1      cgd 		if (ch == '\n')
    589      1.1      cgd 			*--cp = 0;
    590      1.1      cgd 
    591      1.1      cgd #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
    592      1.1      cgd 		cp = buf;
    593      1.1      cgd 		if (*cp == 'T') {
    594      1.1      cgd 			setimes++;
    595      1.1      cgd 			cp++;
    596      1.1      cgd 			getnum(mtime.tv_sec);
    597      1.1      cgd 			if (*cp++ != ' ')
    598      1.1      cgd 				SCREWUP("mtime.sec not delimited");
    599      1.1      cgd 			getnum(mtime.tv_usec);
    600      1.1      cgd 			if (*cp++ != ' ')
    601      1.1      cgd 				SCREWUP("mtime.usec not delimited");
    602      1.1      cgd 			getnum(atime.tv_sec);
    603      1.1      cgd 			if (*cp++ != ' ')
    604      1.1      cgd 				SCREWUP("atime.sec not delimited");
    605      1.1      cgd 			getnum(atime.tv_usec);
    606      1.1      cgd 			if (*cp++ != '\0')
    607      1.1      cgd 				SCREWUP("atime.usec not delimited");
    608      1.1      cgd 			(void)write(rem, "", 1);
    609      1.1      cgd 			continue;
    610      1.1      cgd 		}
    611      1.1      cgd 		if (*cp != 'C' && *cp != 'D') {
    612      1.1      cgd 			/*
    613      1.1      cgd 			 * Check for the case "rcp remote:foo\* local:bar".
    614      1.1      cgd 			 * In this case, the line "No match." can be returned
    615      1.1      cgd 			 * by the shell before the rcp command on the remote is
    616      1.1      cgd 			 * executed so the ^Aerror_message convention isn't
    617      1.1      cgd 			 * followed.
    618      1.1      cgd 			 */
    619      1.1      cgd 			if (first) {
    620  1.1.1.2  mycroft 				run_err("%s", cp);
    621      1.1      cgd 				exit(1);
    622      1.1      cgd 			}
    623      1.1      cgd 			SCREWUP("expected control record");
    624      1.1      cgd 		}
    625      1.1      cgd 		mode = 0;
    626      1.1      cgd 		for (++cp; cp < buf + 5; cp++) {
    627      1.1      cgd 			if (*cp < '0' || *cp > '7')
    628      1.1      cgd 				SCREWUP("bad mode");
    629      1.1      cgd 			mode = (mode << 3) | (*cp - '0');
    630      1.1      cgd 		}
    631      1.1      cgd 		if (*cp++ != ' ')
    632      1.1      cgd 			SCREWUP("mode not delimited");
    633  1.1.1.2  mycroft 
    634  1.1.1.2  mycroft 		for (size = 0; isdigit(*cp);)
    635      1.1      cgd 			size = size * 10 + (*cp++ - '0');
    636      1.1      cgd 		if (*cp++ != ' ')
    637      1.1      cgd 			SCREWUP("size not delimited");
    638      1.1      cgd 		if (targisdir) {
    639      1.1      cgd 			static char *namebuf;
    640      1.1      cgd 			static int cursize;
    641      1.1      cgd 			size_t need;
    642      1.1      cgd 
    643      1.1      cgd 			need = strlen(targ) + strlen(cp) + 250;
    644      1.1      cgd 			if (need > cursize) {
    645      1.1      cgd 				if (!(namebuf = malloc(need)))
    646  1.1.1.2  mycroft 					run_err("%s", strerror(errno));
    647      1.1      cgd 			}
    648      1.1      cgd 			(void)snprintf(namebuf, need, "%s%s%s", targ,
    649      1.1      cgd 			    *targ ? "/" : "", cp);
    650      1.1      cgd 			np = namebuf;
    651  1.1.1.2  mycroft 		} else
    652      1.1      cgd 			np = targ;
    653      1.1      cgd 		exists = stat(np, &stb) == 0;
    654      1.1      cgd 		if (buf[0] == 'D') {
    655  1.1.1.2  mycroft 			int mod_flag = pflag;
    656      1.1      cgd 			if (exists) {
    657  1.1.1.2  mycroft 				if (!S_ISDIR(stb.st_mode)) {
    658      1.1      cgd 					errno = ENOTDIR;
    659      1.1      cgd 					goto bad;
    660      1.1      cgd 				}
    661      1.1      cgd 				if (pflag)
    662      1.1      cgd 					(void)chmod(np, mode);
    663  1.1.1.2  mycroft 			} else {
    664  1.1.1.2  mycroft 				/* Handle copying from a read-only directory */
    665  1.1.1.2  mycroft 				mod_flag = 1;
    666  1.1.1.2  mycroft 				if (mkdir(np, mode | S_IRWXU) < 0)
    667  1.1.1.2  mycroft 					goto bad;
    668  1.1.1.2  mycroft 			}
    669      1.1      cgd 			vect[0] = np;
    670      1.1      cgd 			sink(1, vect);
    671      1.1      cgd 			if (setimes) {
    672      1.1      cgd 				setimes = 0;
    673      1.1      cgd 				if (utimes(np, tv) < 0)
    674  1.1.1.2  mycroft 				    run_err("%s: set times: %s",
    675      1.1      cgd 					np, strerror(errno));
    676      1.1      cgd 			}
    677  1.1.1.2  mycroft 			if (mod_flag)
    678  1.1.1.2  mycroft 				(void)chmod(np, mode);
    679      1.1      cgd 			continue;
    680      1.1      cgd 		}
    681  1.1.1.2  mycroft 		omode = mode;
    682  1.1.1.2  mycroft 		mode |= S_IWRITE;
    683      1.1      cgd 		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
    684  1.1.1.2  mycroft bad:			run_err("%s: %s", np, strerror(errno));
    685      1.1      cgd 			continue;
    686      1.1      cgd 		}
    687      1.1      cgd 		(void)write(rem, "", 1);
    688  1.1.1.2  mycroft 		if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
    689      1.1      cgd 			(void)close(ofd);
    690      1.1      cgd 			continue;
    691      1.1      cgd 		}
    692      1.1      cgd 		cp = bp->buf;
    693      1.1      cgd 		wrerr = NO;
    694  1.1.1.2  mycroft 		for (count = i = 0; i < size; i += BUFSIZ) {
    695      1.1      cgd 			amt = BUFSIZ;
    696      1.1      cgd 			if (i + amt > size)
    697      1.1      cgd 				amt = size - i;
    698      1.1      cgd 			count += amt;
    699      1.1      cgd 			do {
    700      1.1      cgd 				j = read(rem, cp, amt);
    701      1.1      cgd 				if (j <= 0) {
    702  1.1.1.2  mycroft 					run_err("%s", j ? strerror(errno) :
    703      1.1      cgd 					    "dropped connection");
    704      1.1      cgd 					exit(1);
    705      1.1      cgd 				}
    706      1.1      cgd 				amt -= j;
    707      1.1      cgd 				cp += j;
    708      1.1      cgd 			} while (amt > 0);
    709      1.1      cgd 			if (count == bp->cnt) {
    710  1.1.1.2  mycroft 				/* Keep reading so we stay sync'd up. */
    711  1.1.1.2  mycroft 				if (wrerr == NO) {
    712  1.1.1.2  mycroft 					j = write(ofd, bp->buf, count);
    713  1.1.1.2  mycroft 					if (j != count) {
    714  1.1.1.2  mycroft 						wrerr = YES;
    715  1.1.1.2  mycroft 						wrerrno = j >= 0 ? EIO : errno;
    716  1.1.1.2  mycroft 					}
    717  1.1.1.2  mycroft 				}
    718      1.1      cgd 				count = 0;
    719      1.1      cgd 				cp = bp->buf;
    720      1.1      cgd 			}
    721      1.1      cgd 		}
    722      1.1      cgd 		if (count != 0 && wrerr == NO &&
    723  1.1.1.2  mycroft 		    (j = write(ofd, bp->buf, count)) != count) {
    724      1.1      cgd 			wrerr = YES;
    725  1.1.1.2  mycroft 			wrerrno = j >= 0 ? EIO : errno;
    726  1.1.1.2  mycroft 		}
    727      1.1      cgd 		if (ftruncate(ofd, size)) {
    728  1.1.1.2  mycroft 			run_err("%s: truncate: %s", np, strerror(errno));
    729      1.1      cgd 			wrerr = DISPLAYED;
    730      1.1      cgd 		}
    731  1.1.1.2  mycroft 		if (pflag) {
    732  1.1.1.2  mycroft 			if (exists || omode != mode)
    733  1.1.1.2  mycroft 				if (fchmod(ofd, omode))
    734  1.1.1.2  mycroft 					run_err("%s: set mode: %s",
    735  1.1.1.2  mycroft 					    np, strerror(errno));
    736  1.1.1.2  mycroft 		} else {
    737  1.1.1.2  mycroft 			if (!exists && omode != mode)
    738  1.1.1.2  mycroft 				if (fchmod(ofd, omode & ~mask))
    739  1.1.1.2  mycroft 					run_err("%s: set mode: %s",
    740  1.1.1.2  mycroft 					    np, strerror(errno));
    741  1.1.1.2  mycroft 		}
    742      1.1      cgd 		(void)close(ofd);
    743      1.1      cgd 		(void)response();
    744      1.1      cgd 		if (setimes && wrerr == NO) {
    745      1.1      cgd 			setimes = 0;
    746      1.1      cgd 			if (utimes(np, tv) < 0) {
    747  1.1.1.2  mycroft 				run_err("%s: set times: %s",
    748      1.1      cgd 				    np, strerror(errno));
    749      1.1      cgd 				wrerr = DISPLAYED;
    750      1.1      cgd 			}
    751      1.1      cgd 		}
    752      1.1      cgd 		switch(wrerr) {
    753      1.1      cgd 		case YES:
    754  1.1.1.2  mycroft 			run_err("%s: %s", np, strerror(wrerrno));
    755      1.1      cgd 			break;
    756      1.1      cgd 		case NO:
    757      1.1      cgd 			(void)write(rem, "", 1);
    758      1.1      cgd 			break;
    759      1.1      cgd 		case DISPLAYED:
    760      1.1      cgd 			break;
    761      1.1      cgd 		}
    762      1.1      cgd 	}
    763      1.1      cgd screwup:
    764  1.1.1.2  mycroft 	run_err("protocol error: %s", why);
    765      1.1      cgd 	exit(1);
    766      1.1      cgd }
    767      1.1      cgd 
    768  1.1.1.2  mycroft #ifdef KERBEROS
    769  1.1.1.2  mycroft int
    770  1.1.1.2  mycroft kerberos(host, bp, locuser, user)
    771  1.1.1.2  mycroft 	char **host, *bp, *locuser, *user;
    772      1.1      cgd {
    773  1.1.1.2  mycroft 	struct servent *sp;
    774  1.1.1.2  mycroft 
    775  1.1.1.2  mycroft again:
    776  1.1.1.2  mycroft 	if (use_kerberos) {
    777  1.1.1.2  mycroft 		rem = KSUCCESS;
    778  1.1.1.2  mycroft 		errno = 0;
    779  1.1.1.2  mycroft 		if (dest_realm == NULL)
    780  1.1.1.2  mycroft 			dest_realm = krb_realmofhost(*host);
    781  1.1.1.2  mycroft 		rem =
    782  1.1.1.2  mycroft #ifdef CRYPT
    783  1.1.1.2  mycroft 		    doencrypt ?
    784  1.1.1.2  mycroft 			krcmd_mutual(host,
    785  1.1.1.2  mycroft 			    port, user, bp, 0, dest_realm, &cred, schedule) :
    786  1.1.1.2  mycroft #endif
    787  1.1.1.2  mycroft 			krcmd(host, port, user, bp, 0, dest_realm);
    788      1.1      cgd 
    789  1.1.1.2  mycroft 		if (rem < 0) {
    790  1.1.1.2  mycroft 			use_kerberos = 0;
    791  1.1.1.2  mycroft 			if ((sp = getservbyname("shell", "tcp")) == NULL)
    792  1.1.1.2  mycroft 				errx(1, "unknown service shell/tcp");
    793  1.1.1.2  mycroft 			if (errno == ECONNREFUSED)
    794  1.1.1.2  mycroft 			    oldw("remote host doesn't support Kerberos");
    795  1.1.1.2  mycroft 			else if (errno == ENOENT)
    796  1.1.1.2  mycroft 			    oldw("can't provide Kerberos authentication data");
    797  1.1.1.2  mycroft 			port = sp->s_port;
    798  1.1.1.2  mycroft 			goto again;
    799      1.1      cgd 		}
    800  1.1.1.2  mycroft 	} else {
    801  1.1.1.2  mycroft #ifdef CRYPT
    802  1.1.1.2  mycroft 		if (doencrypt)
    803  1.1.1.2  mycroft 			errx(1,
    804  1.1.1.2  mycroft 			   "the -x option requires Kerberos authentication");
    805  1.1.1.2  mycroft #endif
    806  1.1.1.2  mycroft 		rem = rcmd(host, port, locuser, user, bp, 0);
    807      1.1      cgd 	}
    808  1.1.1.2  mycroft 	return (rem);
    809      1.1      cgd }
    810  1.1.1.2  mycroft #endif /* KERBEROS */
    811      1.1      cgd 
    812  1.1.1.2  mycroft int
    813  1.1.1.2  mycroft response()
    814      1.1      cgd {
    815  1.1.1.2  mycroft 	char ch, *cp, resp, rbuf[BUFSIZ];
    816      1.1      cgd 
    817  1.1.1.2  mycroft 	if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
    818  1.1.1.2  mycroft 		lostconn(0);
    819      1.1      cgd 
    820  1.1.1.2  mycroft 	cp = rbuf;
    821  1.1.1.2  mycroft 	switch(resp) {
    822  1.1.1.2  mycroft 	case 0:				/* ok */
    823  1.1.1.2  mycroft 		return (0);
    824  1.1.1.2  mycroft 	default:
    825  1.1.1.2  mycroft 		*cp++ = resp;
    826  1.1.1.2  mycroft 		/* FALLTHROUGH */
    827  1.1.1.2  mycroft 	case 1:				/* error, followed by error msg */
    828  1.1.1.2  mycroft 	case 2:				/* fatal error, "" */
    829  1.1.1.2  mycroft 		do {
    830  1.1.1.2  mycroft 			if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
    831  1.1.1.2  mycroft 				lostconn(0);
    832  1.1.1.2  mycroft 			*cp++ = ch;
    833  1.1.1.2  mycroft 		} while (cp < &rbuf[BUFSIZ] && ch != '\n');
    834      1.1      cgd 
    835  1.1.1.2  mycroft 		if (!iamremote)
    836  1.1.1.2  mycroft 			(void)write(STDERR_FILENO, rbuf, cp - rbuf);
    837  1.1.1.2  mycroft 		++errs;
    838  1.1.1.2  mycroft 		if (resp == 1)
    839  1.1.1.2  mycroft 			return (-1);
    840  1.1.1.2  mycroft 		exit(1);
    841  1.1.1.2  mycroft 	}
    842  1.1.1.2  mycroft 	/* NOTREACHED */
    843  1.1.1.2  mycroft }
    844      1.1      cgd 
    845  1.1.1.2  mycroft void
    846      1.1      cgd usage()
    847      1.1      cgd {
    848      1.1      cgd #ifdef KERBEROS
    849      1.1      cgd #ifdef CRYPT
    850      1.1      cgd 	(void)fprintf(stderr, "%s\n\t%s\n",
    851  1.1.1.2  mycroft 	    "usage: rcp [-Kpx] [-k realm] f1 f2",
    852  1.1.1.2  mycroft 	    "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
    853      1.1      cgd #else
    854      1.1      cgd 	(void)fprintf(stderr, "%s\n\t%s\n",
    855  1.1.1.2  mycroft 	    "usage: rcp [-Kp] [-k realm] f1 f2",
    856  1.1.1.2  mycroft 	    "or: rcp [-Kpr] [-k realm] f1 ... fn directory");
    857      1.1      cgd #endif
    858      1.1      cgd #else
    859      1.1      cgd 	(void)fprintf(stderr,
    860  1.1.1.2  mycroft 	    "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
    861      1.1      cgd #endif
    862      1.1      cgd 	exit(1);
    863      1.1      cgd }
    864      1.1      cgd 
    865  1.1.1.2  mycroft #if __STDC__
    866  1.1.1.2  mycroft #include <stdarg.h>
    867  1.1.1.2  mycroft #else
    868  1.1.1.2  mycroft #include <varargs.h>
    869  1.1.1.2  mycroft #endif
    870  1.1.1.2  mycroft 
    871      1.1      cgd #ifdef KERBEROS
    872  1.1.1.2  mycroft void
    873  1.1.1.2  mycroft #if __STDC__
    874  1.1.1.2  mycroft oldw(const char *fmt, ...)
    875  1.1.1.2  mycroft #else
    876  1.1.1.2  mycroft oldw(fmt, va_alist)
    877  1.1.1.2  mycroft 	char *fmt;
    878  1.1.1.2  mycroft         va_dcl
    879  1.1.1.2  mycroft #endif
    880      1.1      cgd {
    881  1.1.1.2  mycroft 	va_list ap;
    882  1.1.1.2  mycroft #if __STDC__
    883  1.1.1.2  mycroft 	va_start(ap, fmt);
    884  1.1.1.2  mycroft #else
    885  1.1.1.2  mycroft 	va_start(ap);
    886  1.1.1.2  mycroft #endif
    887  1.1.1.2  mycroft 	(void)fprintf(stderr, "rcp: ");
    888  1.1.1.2  mycroft 	(void)vfprintf(stderr, fmt, ap);
    889  1.1.1.2  mycroft 	(void)fprintf(stderr, ", using standard rcp\n");
    890  1.1.1.2  mycroft 	va_end(ap);
    891      1.1      cgd }
    892  1.1.1.2  mycroft #endif
    893      1.1      cgd 
    894  1.1.1.2  mycroft void
    895  1.1.1.2  mycroft #if __STDC__
    896  1.1.1.2  mycroft run_err(const char *fmt, ...)
    897  1.1.1.2  mycroft #else
    898  1.1.1.2  mycroft run_err(fmt, va_alist)
    899  1.1.1.2  mycroft 	char *fmt;
    900  1.1.1.2  mycroft         va_dcl
    901  1.1.1.2  mycroft #endif
    902      1.1      cgd {
    903  1.1.1.2  mycroft 	static FILE *fp;
    904  1.1.1.2  mycroft 	va_list ap;
    905  1.1.1.2  mycroft #if __STDC__
    906  1.1.1.2  mycroft 	va_start(ap, fmt);
    907  1.1.1.2  mycroft #else
    908  1.1.1.2  mycroft 	va_start(ap);
    909      1.1      cgd #endif
    910      1.1      cgd 
    911  1.1.1.2  mycroft 	++errs;
    912  1.1.1.2  mycroft 	if (fp == NULL && !(fp = fdopen(rem, "w")))
    913  1.1.1.2  mycroft 		return;
    914  1.1.1.2  mycroft 	(void)fprintf(fp, "%c", 0x01);
    915  1.1.1.2  mycroft 	(void)fprintf(fp, "rcp: ");
    916  1.1.1.2  mycroft 	(void)vfprintf(fp, fmt, ap);
    917  1.1.1.2  mycroft 	(void)fprintf(fp, "\n");
    918  1.1.1.2  mycroft 	(void)fflush(fp);
    919      1.1      cgd 
    920  1.1.1.2  mycroft 	if (!iamremote)
    921  1.1.1.2  mycroft 		vwarnx(fmt, ap);
    922  1.1.1.2  mycroft 
    923  1.1.1.2  mycroft 	va_end(ap);
    924      1.1      cgd }
    925