xref: /titanic_54/usr/src/cmd/cmd-inet/usr.bin/rcp.c (revision 40164e4f23d0b187824ba6db3a6b41f6347a749c)
17c478bd9Sstevel@tonic-gate /*
26d2b079bSjs198686  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 The Regents of the University of California.
107c478bd9Sstevel@tonic-gate  * All rights reserved.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
137c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
147c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
157c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
167c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
177c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
187c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
197c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #define	_FILE_OFFSET_BITS  64
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * rcp
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/file.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/acl.h>
357c478bd9Sstevel@tonic-gate #include <dirent.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/socket.h>
387c478bd9Sstevel@tonic-gate #include <netinet/in.h>
397c478bd9Sstevel@tonic-gate #include <pwd.h>
407c478bd9Sstevel@tonic-gate #include <netdb.h>
417c478bd9Sstevel@tonic-gate #include <wchar.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <strings.h>
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <fcntl.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <limits.h>
517c478bd9Sstevel@tonic-gate #include <priv_utils.h>
527c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
537c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
547c478bd9Sstevel@tonic-gate #include <sys/wait.h>
55fa9e4066Sahrens #include <aclutils.h>
56*40164e4fSsn199410 #include <sys/varargs.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * It seems like Berkeley got these from pathnames.h?
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate #define	_PATH_RSH	"/usr/bin/rsh"
627c478bd9Sstevel@tonic-gate #define	_PATH_CP	"/usr/bin/cp"
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	ACL_FAIL	1
657c478bd9Sstevel@tonic-gate #define	ACL_OK		0
667c478bd9Sstevel@tonic-gate #define	RCP_BUFSIZE	(64 * 1024)
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	RCP_ACL	"/usr/lib/sunw,rcp"
697c478bd9Sstevel@tonic-gate 		/* see PSARC/1993/004/opinion */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate typedef struct _buf {
727c478bd9Sstevel@tonic-gate 	int	cnt;
737c478bd9Sstevel@tonic-gate 	char	*buf;
747c478bd9Sstevel@tonic-gate } BUF;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static char *cmd_sunw;
777c478bd9Sstevel@tonic-gate static struct passwd *pwd;
787c478bd9Sstevel@tonic-gate static int errs;
797c478bd9Sstevel@tonic-gate static int pflag;
807c478bd9Sstevel@tonic-gate static uid_t userid;
817c478bd9Sstevel@tonic-gate static int rem;
827c478bd9Sstevel@tonic-gate static int zflag;
837c478bd9Sstevel@tonic-gate static int iamremote;
847c478bd9Sstevel@tonic-gate static int iamrecursive;
857c478bd9Sstevel@tonic-gate static int targetshouldbedirectory;
867c478bd9Sstevel@tonic-gate static int aclflag;
87fa9e4066Sahrens static int acl_aclflag;
887c478bd9Sstevel@tonic-gate static int retval = 0;
897c478bd9Sstevel@tonic-gate static int portnumber = 0;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static void lostconn(void);
927c478bd9Sstevel@tonic-gate static char *search_char(unsigned char *, unsigned char);
937c478bd9Sstevel@tonic-gate static char *removebrackets(char *);
947c478bd9Sstevel@tonic-gate static char *colon(char *);
957c478bd9Sstevel@tonic-gate static int response(void);
967c478bd9Sstevel@tonic-gate static void usage(void);
977c478bd9Sstevel@tonic-gate static void source(int, char **);
987c478bd9Sstevel@tonic-gate static void sink(int, char **);
997c478bd9Sstevel@tonic-gate static void toremote(char *, int, char **);
1007c478bd9Sstevel@tonic-gate static void tolocal(int, char **);
1017c478bd9Sstevel@tonic-gate static void verifydir(char *);
1027c478bd9Sstevel@tonic-gate static int okname(char *);
103*40164e4fSsn199410 static int susystem(char *, char **);
1047c478bd9Sstevel@tonic-gate static void rsource(char *, struct stat *);
1057c478bd9Sstevel@tonic-gate static int sendacl(int);
1067c478bd9Sstevel@tonic-gate static int recvacl(int, int, int);
1077c478bd9Sstevel@tonic-gate static int zwrite(int, char *, int);
1087c478bd9Sstevel@tonic-gate static void zopen(int, int);
1097c478bd9Sstevel@tonic-gate static int zclose(int);
1107c478bd9Sstevel@tonic-gate static int notzero(char *, int);
1117c478bd9Sstevel@tonic-gate static BUF *allocbuf(BUF *, int, int);
1127c478bd9Sstevel@tonic-gate static void error(char *fmt, ...);
113*40164e4fSsn199410 static void addargs(char **, ...);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * As a 32 bit application, we can only transfer (2gb - 1) i.e 0x7FFFFFFF
1177c478bd9Sstevel@tonic-gate  * bytes of data. We would like the size to be aligned to the nearest
1187c478bd9Sstevel@tonic-gate  * MAXBOFFSET (8192) boundary for optimal performance.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate #define	SENDFILE_SIZE	0x7FFFE000
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #include <k5-int.h>
1237c478bd9Sstevel@tonic-gate #include <profile/prof_int.h>
1247c478bd9Sstevel@tonic-gate #include <com_err.h>
1257c478bd9Sstevel@tonic-gate #include <kcmd.h>
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	NULLBUF	(BUF *) 0
128*40164e4fSsn199410 #define	MAXARGS	10	/* Number of arguments passed to execv() */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static int sock;
1317c478bd9Sstevel@tonic-gate static char *cmd, *cmd_orig, *cmd_sunw_orig;
1327c478bd9Sstevel@tonic-gate static char *krb_realm = NULL;
1337c478bd9Sstevel@tonic-gate static char *krb_cache = NULL;
1347c478bd9Sstevel@tonic-gate static char *krb_config = NULL;
1357c478bd9Sstevel@tonic-gate static char des_inbuf[2 * RCP_BUFSIZE];
1367c478bd9Sstevel@tonic-gate 				/* needs to be > largest read size */
1377c478bd9Sstevel@tonic-gate static char des_outbuf[2 * RCP_BUFSIZE];
1387c478bd9Sstevel@tonic-gate 				/* needs to be > largest write size */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf;
1417c478bd9Sstevel@tonic-gate static krb5_encrypt_block eblock;	/* eblock for encrypt/decrypt */
1427c478bd9Sstevel@tonic-gate static krb5_keyblock *session_key;	/* static key for session */
1437c478bd9Sstevel@tonic-gate static krb5_context bsd_context;
1447c478bd9Sstevel@tonic-gate static krb5_auth_context auth_context;
1457c478bd9Sstevel@tonic-gate static krb5_flags authopts;
1467c478bd9Sstevel@tonic-gate static krb5_error_code status;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static void try_normal_rcp(int, char **);
1497c478bd9Sstevel@tonic-gate static int init_service(int);
1507c478bd9Sstevel@tonic-gate static char **save_argv(int, char **);
1517c478bd9Sstevel@tonic-gate static void answer_auth(char *, char *);
1527c478bd9Sstevel@tonic-gate static int desrcpwrite(int, char *, int);
1537c478bd9Sstevel@tonic-gate static int desrcpread(int, char *, int);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Not sure why these two don't have their own header file declarations, but
1577c478bd9Sstevel@tonic-gate  * lint complains about absent declarations so place some here. Sigh.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate extern errcode_t	profile_get_options_boolean(profile_t, char **,
1607c478bd9Sstevel@tonic-gate     profile_options_boolean *);
1617c478bd9Sstevel@tonic-gate extern errcode_t	profile_get_options_string(profile_t, char **,
1627c478bd9Sstevel@tonic-gate     profile_option_strings *);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static int krb5auth_flag = 0;	/* Flag set, when KERBEROS is enabled */
1657c478bd9Sstevel@tonic-gate static int encrypt_flag = 0;	/* Flag set, when encryption is enabled */
1667c478bd9Sstevel@tonic-gate static int encrypt_done = 0;	/* Flag set, if "-x" is specified */
1677c478bd9Sstevel@tonic-gate static enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
1707c478bd9Sstevel@tonic-gate static boolean_t rcmdoption_done = B_FALSE;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static profile_options_boolean option[] = {
1737c478bd9Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
1747c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1757c478bd9Sstevel@tonic-gate };
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static char *rcmdproto = NULL;
1787c478bd9Sstevel@tonic-gate static profile_option_strings rcmdversion[] = {
1797c478bd9Sstevel@tonic-gate 	{ "rcmd_protocol", &rcmdproto, 0 },
1807c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, "rcp", NULL };
1847c478bd9Sstevel@tonic-gate static char *appdef[] = { "appdefaults", "rcp", NULL };
1857c478bd9Sstevel@tonic-gate static char **prev_argv;
1867c478bd9Sstevel@tonic-gate static int prev_argc;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate int
1897c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	int ch, fflag, tflag;
1927c478bd9Sstevel@tonic-gate 	char *targ;
1937c478bd9Sstevel@tonic-gate 	size_t cmdsiz;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], RCP_ACL) == 0)
1987c478bd9Sstevel@tonic-gate 		aclflag = 1;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (!(pwd = getpwuid(userid = getuid()))) {
2017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rcp: unknown user %d.\n",
2027c478bd9Sstevel@tonic-gate 		    (uint_t)userid);
2037c478bd9Sstevel@tonic-gate 		return (1);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	fflag = tflag = 0;
207fa9e4066Sahrens 	while ((ch = getopt(argc, argv, "axdfprtz:D:k:P:Z")) != EOF) {
2087c478bd9Sstevel@tonic-gate 		switch (ch) {
2097c478bd9Sstevel@tonic-gate 		case 'd':
2107c478bd9Sstevel@tonic-gate 			targetshouldbedirectory = 1;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 		case 'f':			/* "from" */
2137c478bd9Sstevel@tonic-gate 			fflag = 1;
214fa9e4066Sahrens 			if (aclflag | acl_aclflag)
2157c478bd9Sstevel@tonic-gate 				/* ok response */
2167c478bd9Sstevel@tonic-gate 				(void) desrcpwrite(rem, "", 1);
2177c478bd9Sstevel@tonic-gate 			break;
2187c478bd9Sstevel@tonic-gate 		case 'p':			/* preserve access/mod times */
2197c478bd9Sstevel@tonic-gate 			++pflag;
2207c478bd9Sstevel@tonic-gate 			break;
2217c478bd9Sstevel@tonic-gate 		case 'r':
2227c478bd9Sstevel@tonic-gate 			++iamrecursive;
2237c478bd9Sstevel@tonic-gate 			break;
2247c478bd9Sstevel@tonic-gate 		case 't':			/* "to" */
2257c478bd9Sstevel@tonic-gate 			tflag = 1;
2267c478bd9Sstevel@tonic-gate 			break;
227fa9e4066Sahrens 		case 'Z':
228fa9e4066Sahrens 			acl_aclflag++;
229fa9e4066Sahrens 			break;
2307c478bd9Sstevel@tonic-gate 		case 'x':
2317c478bd9Sstevel@tonic-gate 			if (!krb5_privacy_allowed()) {
2327c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp: "
2337c478bd9Sstevel@tonic-gate 					"Encryption not supported.\n"));
2347c478bd9Sstevel@tonic-gate 				return (1);
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 			encrypt_flag++;
2377c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2387c478bd9Sstevel@tonic-gate 			encrypt_done++;
2397c478bd9Sstevel@tonic-gate 			break;
2407c478bd9Sstevel@tonic-gate 		case 'k':
2417c478bd9Sstevel@tonic-gate 			if ((krb_realm = (char *)strdup(optarg)) == NULL) {
2427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp:"
2437c478bd9Sstevel@tonic-gate 					" Cannot malloc.\n"));
2447c478bd9Sstevel@tonic-gate 				return (1);
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 		case 'P':
2497c478bd9Sstevel@tonic-gate 			if (strncmp(optarg, "O", 1) == 0) {
2507c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
2517c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rcp: "
2527c478bd9Sstevel@tonic-gate 						"Only one of -PN and -PO "
2537c478bd9Sstevel@tonic-gate 						"allowed.\n"));
2547c478bd9Sstevel@tonic-gate 					usage();
2557c478bd9Sstevel@tonic-gate 				}
2567c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
2577c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
2587c478bd9Sstevel@tonic-gate 			} else if (strncmp(optarg, "N", 1) == 0) {
2597c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
2607c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rcp: "
2617c478bd9Sstevel@tonic-gate 						"Only one of -PN and -PO "
2627c478bd9Sstevel@tonic-gate 						"allowed.\n"));
2637c478bd9Sstevel@tonic-gate 					usage();
2647c478bd9Sstevel@tonic-gate 				}
2657c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
2667c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
2677c478bd9Sstevel@tonic-gate 			} else {
2687c478bd9Sstevel@tonic-gate 				usage();
2697c478bd9Sstevel@tonic-gate 			}
2707c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2717c478bd9Sstevel@tonic-gate 			break;
2727c478bd9Sstevel@tonic-gate 		case 'a':
2737c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate #ifdef DEBUG
2767c478bd9Sstevel@tonic-gate 		case 'D':
2777c478bd9Sstevel@tonic-gate 			portnumber = htons(atoi(optarg));
2787c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2817c478bd9Sstevel@tonic-gate 		case '?':
2827c478bd9Sstevel@tonic-gate 		default:
2837c478bd9Sstevel@tonic-gate 			usage();
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	argc -= optind;
2877c478bd9Sstevel@tonic-gate 	argv += optind;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
2907c478bd9Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
2917c478bd9Sstevel@tonic-gate 		if (status) {
2927c478bd9Sstevel@tonic-gate 			com_err("rcp", status,
2937c478bd9Sstevel@tonic-gate 				gettext("while initializing krb5"));
2947c478bd9Sstevel@tonic-gate 			return (1);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		/*
2987c478bd9Sstevel@tonic-gate 		 * Set up buffers for desread and deswrite.
2997c478bd9Sstevel@tonic-gate 		 */
3007c478bd9Sstevel@tonic-gate 		desinbuf.data = des_inbuf;
3017c478bd9Sstevel@tonic-gate 		desoutbuf.data = des_outbuf;
3027c478bd9Sstevel@tonic-gate 		desinbuf.length = sizeof (des_inbuf);
3037c478bd9Sstevel@tonic-gate 		desoutbuf.length = sizeof (des_outbuf);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (fflag || tflag)
3077c478bd9Sstevel@tonic-gate 		if (encrypt_flag > 0)
3087c478bd9Sstevel@tonic-gate 			(void) answer_auth(krb_config, krb_cache);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if (fflag) {
3117c478bd9Sstevel@tonic-gate 		iamremote = 1;
3127c478bd9Sstevel@tonic-gate 		(void) response();
3137c478bd9Sstevel@tonic-gate 		(void) setuid(userid);
3147c478bd9Sstevel@tonic-gate 		source(argc, argv);
3157c478bd9Sstevel@tonic-gate 		return (errs);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (tflag) {
3197c478bd9Sstevel@tonic-gate 		iamremote = 1;
3207c478bd9Sstevel@tonic-gate 		(void) setuid(userid);
3217c478bd9Sstevel@tonic-gate 		sink(argc, argv);
3227c478bd9Sstevel@tonic-gate 		return (errs);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (argc < 2)
3267c478bd9Sstevel@tonic-gate 		usage();
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/* This will make "rcmd_af()" magically get the proper privilege */
3297c478bd9Sstevel@tonic-gate 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL) == -1) {
3307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rcp: must be set-uid root\n");
3317c478bd9Sstevel@tonic-gate 		exit(1);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
3357c478bd9Sstevel@tonic-gate 		/*
3367c478bd9Sstevel@tonic-gate 		 * Get our local realm to look up local realm options.
3377c478bd9Sstevel@tonic-gate 		 */
3387c478bd9Sstevel@tonic-gate 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
3397c478bd9Sstevel@tonic-gate 		if (status) {
3407c478bd9Sstevel@tonic-gate 			com_err("rcp", status,
3417c478bd9Sstevel@tonic-gate 				gettext("while getting default realm"));
3427c478bd9Sstevel@tonic-gate 			return (1);
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 		/*
3457c478bd9Sstevel@tonic-gate 		 * See if encryption should be done for this realm
3467c478bd9Sstevel@tonic-gate 		 */
3477c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, realmdef,
3487c478bd9Sstevel@tonic-gate 						option);
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * Check the appdefaults section
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, appdef,
3537c478bd9Sstevel@tonic-gate 						option);
3547c478bd9Sstevel@tonic-gate 		profile_get_options_string(bsd_context->profile, appdef,
3557c478bd9Sstevel@tonic-gate 						rcmdversion);
3567c478bd9Sstevel@tonic-gate 		if ((encrypt_done > 0) || (encrypt_flag > 0)) {
3577c478bd9Sstevel@tonic-gate 			if (krb5_privacy_allowed() == TRUE) {
3587c478bd9Sstevel@tonic-gate 				encrypt_flag++;
3597c478bd9Sstevel@tonic-gate 			} else {
3607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp: Encryption"
3617c478bd9Sstevel@tonic-gate 							" not supported.\n"));
3627c478bd9Sstevel@tonic-gate 				return (1);
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		if ((rcmdoption_done == B_FALSE) && (rcmdproto != NULL)) {
3677c478bd9Sstevel@tonic-gate 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
3687c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
3697c478bd9Sstevel@tonic-gate 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
3707c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
3717c478bd9Sstevel@tonic-gate 			} else {
3727c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Unrecognized "
3737c478bd9Sstevel@tonic-gate 					"KCMD protocol (%s)"), rcmdproto);
3747c478bd9Sstevel@tonic-gate 				return (1);
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (argc > 2)
3807c478bd9Sstevel@tonic-gate 		targetshouldbedirectory = 1;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	rem = -1;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (portnumber == 0) {
3857c478bd9Sstevel@tonic-gate 		if (krb5auth_flag > 0) {
3867c478bd9Sstevel@tonic-gate 			retval = init_service(krb5auth_flag);
3877c478bd9Sstevel@tonic-gate 			if (!retval) {
3887c478bd9Sstevel@tonic-gate 				/*
3897c478bd9Sstevel@tonic-gate 				 * Connecting to the kshell service failed,
3907c478bd9Sstevel@tonic-gate 				 * fallback to normal rcp & reset KRB5 flags.
3917c478bd9Sstevel@tonic-gate 				 */
3927c478bd9Sstevel@tonic-gate 				krb5auth_flag = encrypt_flag = 0;
3937c478bd9Sstevel@tonic-gate 				encrypt_done = 0;
3947c478bd9Sstevel@tonic-gate 				(void) init_service(krb5auth_flag);
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 		else
3987c478bd9Sstevel@tonic-gate 			(void) init_service(krb5auth_flag);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate #ifdef DEBUG
4027c478bd9Sstevel@tonic-gate 	if (retval || krb5auth_flag) {
4037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Kerberized rcp session, "
4047c478bd9Sstevel@tonic-gate 				"port %d in use "), portnumber);
4057c478bd9Sstevel@tonic-gate 		if (kcmd_proto == KCMD_OLD_PROTOCOL)
4067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("[kcmd ver.1]\n"));
4077c478bd9Sstevel@tonic-gate 		else
4087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("[kcmd ver.2]\n"));
4097c478bd9Sstevel@tonic-gate 	} else {
4107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Normal rcp session, port %d "
4117c478bd9Sstevel@tonic-gate 				"in use.\n"), portnumber);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate #endif /* DEBUG */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
4167c478bd9Sstevel@tonic-gate 		/*
4177c478bd9Sstevel@tonic-gate 		 * We calculate here a buffer size that can be used in the
4187c478bd9Sstevel@tonic-gate 		 * allocation of the three buffers cmd, cmd_orig and
4197c478bd9Sstevel@tonic-gate 		 * cmd_sunw_orig that are used to hold different incantations
4207c478bd9Sstevel@tonic-gate 		 * of rcp.
4217c478bd9Sstevel@tonic-gate 		 */
4227c478bd9Sstevel@tonic-gate 		cmdsiz = MAX(sizeof ("-x rcp -r -p -d -k ") +
4237c478bd9Sstevel@tonic-gate 		    strlen(krb_realm != NULL ? krb_realm : ""),
4247c478bd9Sstevel@tonic-gate 		    sizeof (RCP_ACL " -r -p -z -d"));
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		if (((cmd = (char *)malloc(cmdsiz)) == NULL) ||
4277c478bd9Sstevel@tonic-gate 			((cmd_sunw_orig = (char *)malloc(cmdsiz)) == NULL) ||
4287c478bd9Sstevel@tonic-gate 			((cmd_orig = (char *)malloc(cmdsiz)) == NULL)) {
4297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rcp: Cannot "
4307c478bd9Sstevel@tonic-gate 					"malloc.\n"));
4317c478bd9Sstevel@tonic-gate 			return (1);
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, cmdsiz, "%srcp %s%s%s%s%s",
4357c478bd9Sstevel@tonic-gate 			encrypt_flag ? "-x " : "",
4367c478bd9Sstevel@tonic-gate 			iamrecursive ? " -r" : "", pflag ? " -p" : "",
4377c478bd9Sstevel@tonic-gate 			targetshouldbedirectory ? " -d" : "",
4387c478bd9Sstevel@tonic-gate 			krb_realm != NULL ? " -k " : "",
4397c478bd9Sstevel@tonic-gate 			krb_realm != NULL ? krb_realm : "");
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		/*
4427c478bd9Sstevel@tonic-gate 		 * We would use cmd-orig as the 'cmd-buffer' if kerberized
4437c478bd9Sstevel@tonic-gate 		 * rcp fails, in which case we fallback to normal rcp. We also
4447c478bd9Sstevel@tonic-gate 		 * save argc & argv for the same purpose
4457c478bd9Sstevel@tonic-gate 		 */
4467c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd_orig, cmdsiz, "rcp%s%s%s%s",
4477c478bd9Sstevel@tonic-gate 			iamrecursive ? " -r" : "",
4487c478bd9Sstevel@tonic-gate 			pflag ? " -p" : "",
4497c478bd9Sstevel@tonic-gate 			zflag ? " -z" : "",
4507c478bd9Sstevel@tonic-gate 			targetshouldbedirectory ? " -d" : "");
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd_sunw_orig, cmdsiz, "%s%s%s%s%s", RCP_ACL,
4537c478bd9Sstevel@tonic-gate 			iamrecursive ? " -r" : "",
4547c478bd9Sstevel@tonic-gate 			pflag ? " -p" : "",
4557c478bd9Sstevel@tonic-gate 			zflag ? " -z" : "",
4567c478bd9Sstevel@tonic-gate 			targetshouldbedirectory ? " -d" : "");
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		prev_argc = argc;
4597c478bd9Sstevel@tonic-gate 		prev_argv = save_argv(argc, argv);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	} else {
4627c478bd9Sstevel@tonic-gate 		cmdsiz = sizeof ("rcp -r -p -z -d");
4637c478bd9Sstevel@tonic-gate 		if (((cmd = (char *)malloc(cmdsiz)) == NULL)) {
4647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rcp: Cannot "
4657c478bd9Sstevel@tonic-gate 					"malloc.\n"));
4667c478bd9Sstevel@tonic-gate 			return (1);
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, cmdsiz, "rcp%s%s%s%s",
4707c478bd9Sstevel@tonic-gate 			iamrecursive ? " -r" : "",
4717c478bd9Sstevel@tonic-gate 			pflag ? " -p" : "",
4727c478bd9Sstevel@tonic-gate 			zflag ? " -z" : "",
4737c478bd9Sstevel@tonic-gate 			targetshouldbedirectory ? " -d" : "");
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	cmdsiz = sizeof (RCP_ACL " -r -p -z -d");
4777c478bd9Sstevel@tonic-gate 	if ((cmd_sunw = (char *)malloc(cmdsiz)) == NULL) {
4787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rcp: Cannot malloc.\n"));
4797c478bd9Sstevel@tonic-gate 		return (1);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	(void) snprintf(cmd_sunw, cmdsiz, "%s%s%s%s%s", RCP_ACL,
4837c478bd9Sstevel@tonic-gate 	    iamrecursive ? " -r" : "",
4847c478bd9Sstevel@tonic-gate 	    pflag ? " -p" : "",
4857c478bd9Sstevel@tonic-gate 	    zflag ? " -z" : "",
4867c478bd9Sstevel@tonic-gate 	    targetshouldbedirectory ? " -d" : "");
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, (void (*)(int))lostconn);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (targ = colon(argv[argc - 1]))
4917c478bd9Sstevel@tonic-gate 		toremote(targ, argc, argv);
4927c478bd9Sstevel@tonic-gate 	else {
4937c478bd9Sstevel@tonic-gate 		tolocal(argc, argv);
4947c478bd9Sstevel@tonic-gate 		if (targetshouldbedirectory)
4957c478bd9Sstevel@tonic-gate 			verifydir(argv[argc - 1]);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	return (errs > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static void
5037c478bd9Sstevel@tonic-gate toremote(char *targ, int argc, char *argv[])
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	int i;
5067c478bd9Sstevel@tonic-gate 	char *host, *src, *suser, *thost, *tuser;
5077c478bd9Sstevel@tonic-gate 	char resp;
5087c478bd9Sstevel@tonic-gate 	size_t buffersize;
5097c478bd9Sstevel@tonic-gate 	char bp[RCP_BUFSIZE];
5107c478bd9Sstevel@tonic-gate 	krb5_creds *cred;
511*40164e4fSsn199410 	char *arglist[MAXARGS+1];
5127c478bd9Sstevel@tonic-gate 	buffersize = RCP_BUFSIZE;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	*targ++ = 0;
5157c478bd9Sstevel@tonic-gate 	if (*targ == 0)
5167c478bd9Sstevel@tonic-gate 		targ = ".";
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	if (thost = search_char((unsigned char *)argv[argc - 1], '@')) {
5197c478bd9Sstevel@tonic-gate 		*thost++ = 0;
5207c478bd9Sstevel@tonic-gate 		tuser = argv[argc - 1];
5217c478bd9Sstevel@tonic-gate 		if (*tuser == '\0')
5227c478bd9Sstevel@tonic-gate 			tuser = NULL;
5237c478bd9Sstevel@tonic-gate 		else if (!okname(tuser))
5247c478bd9Sstevel@tonic-gate 			exit(1);
5257c478bd9Sstevel@tonic-gate 	} else {
5267c478bd9Sstevel@tonic-gate 		thost = argv[argc - 1];
5277c478bd9Sstevel@tonic-gate 		tuser = NULL;
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	thost = removebrackets(thost);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
5327c478bd9Sstevel@tonic-gate 		src = colon(argv[i]);
5337c478bd9Sstevel@tonic-gate 		if (src) {			/* remote to remote */
5347c478bd9Sstevel@tonic-gate 			*src++ = 0;
5357c478bd9Sstevel@tonic-gate 			if (*src == 0)
5367c478bd9Sstevel@tonic-gate 				src = ".";
5377c478bd9Sstevel@tonic-gate 			host = search_char((unsigned char *)argv[i], '@');
5387c478bd9Sstevel@tonic-gate 			if (host) {
5397c478bd9Sstevel@tonic-gate 				*host++ = 0;
5407c478bd9Sstevel@tonic-gate 				host = removebrackets(host);
5417c478bd9Sstevel@tonic-gate 				suser = argv[i];
5427c478bd9Sstevel@tonic-gate 				if (*suser == '\0') {
5437c478bd9Sstevel@tonic-gate 					suser = pwd->pw_name;
5447c478bd9Sstevel@tonic-gate 				} else if (!okname(suser)) {
5457c478bd9Sstevel@tonic-gate 					errs++;
5467c478bd9Sstevel@tonic-gate 					continue;
5477c478bd9Sstevel@tonic-gate 				}
548*40164e4fSsn199410 				(void) snprintf(bp, buffersize, "'%s%s%s:%s'",
5497c478bd9Sstevel@tonic-gate 				    tuser ? tuser : "", tuser ? "@" : "",
5507c478bd9Sstevel@tonic-gate 				    thost, targ);
551*40164e4fSsn199410 				(void) addargs(arglist, "rsh", host, "-l",
552*40164e4fSsn199410 				    suser, "-n", cmd, src, bp, (char *)NULL);
5537c478bd9Sstevel@tonic-gate 			} else {
5547c478bd9Sstevel@tonic-gate 				host = removebrackets(argv[i]);
555*40164e4fSsn199410 				(void) snprintf(bp, buffersize, "'%s%s%s:%s'",
5567c478bd9Sstevel@tonic-gate 				    tuser ? tuser : "", tuser ? "@" : "",
5577c478bd9Sstevel@tonic-gate 				    thost, targ);
558*40164e4fSsn199410 				(void) addargs(arglist, "rsh", host, "-n", cmd,
559*40164e4fSsn199410 				    src, bp, (char *)NULL);
5607c478bd9Sstevel@tonic-gate 			}
561*40164e4fSsn199410 			if (susystem(_PATH_RSH, arglist) == -1)
5627c478bd9Sstevel@tonic-gate 				errs++;
5637c478bd9Sstevel@tonic-gate 		} else {			/* local to remote */
5647c478bd9Sstevel@tonic-gate 			if (rem == -1) {
5657c478bd9Sstevel@tonic-gate 				host = thost;
5667c478bd9Sstevel@tonic-gate 				if (krb5auth_flag > 0) {
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 				(void) snprintf(bp, buffersize,
5697c478bd9Sstevel@tonic-gate 						"%s -t %s", cmd, targ);
5707c478bd9Sstevel@tonic-gate 				authopts = AP_OPTS_MUTUAL_REQUIRED;
5717c478bd9Sstevel@tonic-gate 				status = kcmd(&sock, &host,
5727c478bd9Sstevel@tonic-gate 					    portnumber,
5737c478bd9Sstevel@tonic-gate 					    pwd->pw_name,
5747c478bd9Sstevel@tonic-gate 					    tuser ? tuser :
5757c478bd9Sstevel@tonic-gate 					    pwd->pw_name,
5767c478bd9Sstevel@tonic-gate 					    bp,
5777c478bd9Sstevel@tonic-gate 					    0,
5787c478bd9Sstevel@tonic-gate 					    "host",
5797c478bd9Sstevel@tonic-gate 					    krb_realm,
5807c478bd9Sstevel@tonic-gate 					    bsd_context,
5817c478bd9Sstevel@tonic-gate 					    &auth_context,
5827c478bd9Sstevel@tonic-gate 					    &cred,
5837c478bd9Sstevel@tonic-gate 					    0,	/* No seq # */
5847c478bd9Sstevel@tonic-gate 					    0,	/* No server seq # */
5857c478bd9Sstevel@tonic-gate 					    authopts,
5867c478bd9Sstevel@tonic-gate 					    0,	/* Not any port # */
5877c478bd9Sstevel@tonic-gate 					    &kcmd_proto);
5887c478bd9Sstevel@tonic-gate 				if (status) {
5897c478bd9Sstevel@tonic-gate 					/*
5907c478bd9Sstevel@tonic-gate 					 * If new protocol requested, we dont
5917c478bd9Sstevel@tonic-gate 					 * fallback to less secure ones.
5927c478bd9Sstevel@tonic-gate 					 */
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 					if (kcmd_proto == KCMD_NEW_PROTOCOL) {
5957c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5967c478bd9Sstevel@tonic-gate 							gettext("rcp: kcmdv2 "
5977c478bd9Sstevel@tonic-gate 							"to host %s failed - %s"
5987c478bd9Sstevel@tonic-gate 							"\nFallback to normal "
5997c478bd9Sstevel@tonic-gate 							"rcp denied."), host,
6007c478bd9Sstevel@tonic-gate 							error_message(status));
6017c478bd9Sstevel@tonic-gate 						exit(1);
6027c478bd9Sstevel@tonic-gate 					}
6037c478bd9Sstevel@tonic-gate 					if (status != -1) {
6047c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6057c478bd9Sstevel@tonic-gate 						gettext("rcp: kcmd to host "
6067c478bd9Sstevel@tonic-gate 						"%s failed - %s,\n"
6077c478bd9Sstevel@tonic-gate 						"trying normal rcp...\n\n"),
6087c478bd9Sstevel@tonic-gate 						host, error_message(status));
6097c478bd9Sstevel@tonic-gate 					} else {
6107c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6117c478bd9Sstevel@tonic-gate 							gettext("trying normal"
6127c478bd9Sstevel@tonic-gate 							" rcp...\n"));
6137c478bd9Sstevel@tonic-gate 					}
6147c478bd9Sstevel@tonic-gate 					/*
6157c478bd9Sstevel@tonic-gate 					 * kcmd() failed, so we have to
6167c478bd9Sstevel@tonic-gate 					 * fallback to normal rcp
6177c478bd9Sstevel@tonic-gate 					 */
6187c478bd9Sstevel@tonic-gate 					try_normal_rcp(prev_argc, prev_argv);
6197c478bd9Sstevel@tonic-gate 				} else {
6207c478bd9Sstevel@tonic-gate 					rem = sock;
6217c478bd9Sstevel@tonic-gate 					session_key = &cred->keyblock;
6227c478bd9Sstevel@tonic-gate 					if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6237c478bd9Sstevel@tonic-gate 						/* CSTYLED */
6247c478bd9Sstevel@tonic-gate 						status = krb5_auth_con_getlocalsubkey(bsd_context, auth_context, &session_key);
6257c478bd9Sstevel@tonic-gate 						if (status) {
6267c478bd9Sstevel@tonic-gate 							com_err("rcp", status,
6277c478bd9Sstevel@tonic-gate 								"determining "
6287c478bd9Sstevel@tonic-gate 								"subkey for "
6297c478bd9Sstevel@tonic-gate 								"session");
6307c478bd9Sstevel@tonic-gate 							exit(1);
6317c478bd9Sstevel@tonic-gate 						}
6327c478bd9Sstevel@tonic-gate 						if (!session_key) {
6337c478bd9Sstevel@tonic-gate 							com_err("rcp", 0,
6347c478bd9Sstevel@tonic-gate 								"no subkey "
6357c478bd9Sstevel@tonic-gate 								"negotiated for"
6367c478bd9Sstevel@tonic-gate 								" connection");
6377c478bd9Sstevel@tonic-gate 							exit(1);
6387c478bd9Sstevel@tonic-gate 						}
6397c478bd9Sstevel@tonic-gate 					}
6407c478bd9Sstevel@tonic-gate 					eblock.crypto_entry =
6417c478bd9Sstevel@tonic-gate 						session_key->enctype;
6427c478bd9Sstevel@tonic-gate 					eblock.key =
6437c478bd9Sstevel@tonic-gate 						(krb5_keyblock *)session_key;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 					init_encrypt(encrypt_flag,
6467c478bd9Sstevel@tonic-gate 						bsd_context, kcmd_proto,
6477c478bd9Sstevel@tonic-gate 						&desinbuf, &desoutbuf, CLIENT,
6487c478bd9Sstevel@tonic-gate 						&eblock);
6497c478bd9Sstevel@tonic-gate 					if (encrypt_flag > 0) {
6507c478bd9Sstevel@tonic-gate 						char *s = gettext("This rcp "
6517c478bd9Sstevel@tonic-gate 							"session is using "
6527c478bd9Sstevel@tonic-gate 							"encryption for all "
6537c478bd9Sstevel@tonic-gate 							"data transmissions."
6547c478bd9Sstevel@tonic-gate 							"\r\n");
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 						(void) write(2, s, strlen(s));
6577c478bd9Sstevel@tonic-gate 					}
6587c478bd9Sstevel@tonic-gate 				}
6597c478bd9Sstevel@tonic-gate 				if (response() < 0)
6607c478bd9Sstevel@tonic-gate 					exit(1);
6617c478bd9Sstevel@tonic-gate 
662fa9e4066Sahrens 				} else {
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 				/*
6657c478bd9Sstevel@tonic-gate 				 * ACL support: try to find out if the remote
6667c478bd9Sstevel@tonic-gate 				 * site is running acl cognizant version of
6677c478bd9Sstevel@tonic-gate 				 * rcp. A special binary name is used for this
6687c478bd9Sstevel@tonic-gate 				 * purpose.
6697c478bd9Sstevel@tonic-gate 				 */
6707c478bd9Sstevel@tonic-gate 				aclflag = 1;
671fa9e4066Sahrens 				acl_aclflag = 1;
6727c478bd9Sstevel@tonic-gate 
673fa9e4066Sahrens 				/*
674fa9e4066Sahrens 				 * First see if the remote side will support
675fa9e4066Sahrens 				 * both aclent_t and ace_t acl's?
676fa9e4066Sahrens 				 */
677fa9e4066Sahrens 				(void) snprintf(bp, buffersize, "%s -tZ %s",
6787c478bd9Sstevel@tonic-gate 							cmd_sunw, targ);
6797c478bd9Sstevel@tonic-gate 				rem = rcmd_af(&host, portnumber, pwd->pw_name,
6807c478bd9Sstevel@tonic-gate 					    tuser ? tuser : pwd->pw_name,
6817c478bd9Sstevel@tonic-gate 					    bp, 0, AF_INET6);
6827c478bd9Sstevel@tonic-gate 				if (rem < 0)
6837c478bd9Sstevel@tonic-gate 					exit(1);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 				/*
6867c478bd9Sstevel@tonic-gate 				 * This is similar to routine response().
6877c478bd9Sstevel@tonic-gate 				 * If response is not ok, treat the other
6887c478bd9Sstevel@tonic-gate 				 * side as non-acl rcp.
6897c478bd9Sstevel@tonic-gate 				 */
6907c478bd9Sstevel@tonic-gate 				if (read(rem, &resp, sizeof (resp))
6917c478bd9Sstevel@tonic-gate 				    != sizeof (resp))
6927c478bd9Sstevel@tonic-gate 					lostconn();
6937c478bd9Sstevel@tonic-gate 				if (resp != 0) {
694fa9e4066Sahrens 					acl_aclflag = 0;
695fa9e4066Sahrens 					(void) snprintf(bp, buffersize,
696fa9e4066Sahrens 					    "%s -t %s", cmd_sunw, targ);
697fa9e4066Sahrens 
698fa9e4066Sahrens 					(void) close(rem);
699fa9e4066Sahrens 					host = thost;
700fa9e4066Sahrens 					rem = rcmd_af(&host, portnumber,
701fa9e4066Sahrens 					    pwd->pw_name,
702fa9e4066Sahrens 					    tuser ? tuser : pwd->pw_name,
703fa9e4066Sahrens 					    bp, 0, AF_INET6);
704fa9e4066Sahrens 					if (rem < 0)
705fa9e4066Sahrens 						exit(1);
706fa9e4066Sahrens 
707fa9e4066Sahrens 					if (read(rem, &resp, sizeof (resp))
708fa9e4066Sahrens 					    != sizeof (resp))
709fa9e4066Sahrens 						lostconn();
710fa9e4066Sahrens 					if (resp != 0) {
7117c478bd9Sstevel@tonic-gate 						/*
7127c478bd9Sstevel@tonic-gate 						 * Not OK:
7137c478bd9Sstevel@tonic-gate 						 * The other side is running
7147c478bd9Sstevel@tonic-gate 						 * non-acl rcp. Try again with
7157c478bd9Sstevel@tonic-gate 						 * normal stuff
7167c478bd9Sstevel@tonic-gate 						 */
7177c478bd9Sstevel@tonic-gate 						aclflag = 0;
7187c478bd9Sstevel@tonic-gate 						(void) snprintf(bp, buffersize,
7197c478bd9Sstevel@tonic-gate 						    "%s -t %s", cmd, targ);
7207c478bd9Sstevel@tonic-gate 						(void) close(rem);
7217c478bd9Sstevel@tonic-gate 						host = thost;
7227c478bd9Sstevel@tonic-gate 						rem = rcmd_af(&host, portnumber,
7237c478bd9Sstevel@tonic-gate 						    pwd->pw_name,
7247c478bd9Sstevel@tonic-gate 						    tuser ? tuser :
7257c478bd9Sstevel@tonic-gate 						    pwd->pw_name, bp, 0,
7267c478bd9Sstevel@tonic-gate 						    AF_INET6);
7277c478bd9Sstevel@tonic-gate 						if (rem < 0)
7287c478bd9Sstevel@tonic-gate 							exit(1);
7297c478bd9Sstevel@tonic-gate 						if (response() < 0)
7307c478bd9Sstevel@tonic-gate 						    exit(1);
7317c478bd9Sstevel@tonic-gate 					}
732fa9e4066Sahrens 				}
7337c478bd9Sstevel@tonic-gate 				/* everything should be fine now */
7347c478bd9Sstevel@tonic-gate 				(void) setuid(userid);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 				}
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 			source(1, argv + i);
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate static void
7447c478bd9Sstevel@tonic-gate tolocal(int argc, char *argv[])
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	int i;
7477c478bd9Sstevel@tonic-gate 	char *host, *src, *suser, *lhost;
7487c478bd9Sstevel@tonic-gate 	char resp;
7497c478bd9Sstevel@tonic-gate 	size_t buffersize;
7507c478bd9Sstevel@tonic-gate 	char bp[RCP_BUFSIZE];
7517c478bd9Sstevel@tonic-gate 	krb5_creds *cred;
752*40164e4fSsn199410 	char *arglist[MAXARGS+1];
7537c478bd9Sstevel@tonic-gate 	buffersize = RCP_BUFSIZE;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
7567c478bd9Sstevel@tonic-gate 		if (!(src = colon(argv[i]))) {	/* local to local */
757*40164e4fSsn199410 			(void) addargs(arglist, "cp",
758*40164e4fSsn199410 			    iamrecursive ? "-r" : "", pflag ? "-p" : "",
759*40164e4fSsn199410 			    zflag ? "-z" : "", argv[i], argv[argc - 1],
760*40164e4fSsn199410 			    (char *)NULL);
761*40164e4fSsn199410 			if (susystem(_PATH_CP, arglist) == -1)
7627c478bd9Sstevel@tonic-gate 				errs++;
7637c478bd9Sstevel@tonic-gate 			continue;
7647c478bd9Sstevel@tonic-gate 		}
7657c478bd9Sstevel@tonic-gate 		*src++ = 0;
7667c478bd9Sstevel@tonic-gate 		if (*src == 0)
7677c478bd9Sstevel@tonic-gate 			src = ".";
7687c478bd9Sstevel@tonic-gate 		host = search_char((unsigned char *)argv[i], '@');
7697c478bd9Sstevel@tonic-gate 		if (host) {
7707c478bd9Sstevel@tonic-gate 			*host++ = 0;
7717c478bd9Sstevel@tonic-gate 			suser = argv[i];
7727c478bd9Sstevel@tonic-gate 			if (*suser == '\0') {
7737c478bd9Sstevel@tonic-gate 				suser = pwd->pw_name;
7747c478bd9Sstevel@tonic-gate 			} else if (!okname(suser)) {
7757c478bd9Sstevel@tonic-gate 				errs++;
7767c478bd9Sstevel@tonic-gate 				continue;
7777c478bd9Sstevel@tonic-gate 			}
7787c478bd9Sstevel@tonic-gate 		} else {
7797c478bd9Sstevel@tonic-gate 			host = argv[i];
7807c478bd9Sstevel@tonic-gate 			suser = pwd->pw_name;
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 		host = removebrackets(host);
7837c478bd9Sstevel@tonic-gate 		lhost = host;
7847c478bd9Sstevel@tonic-gate 		if (krb5auth_flag > 0) {
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		(void) snprintf(bp, buffersize, "%s -f %s", cmd, src);
7877c478bd9Sstevel@tonic-gate 		authopts = AP_OPTS_MUTUAL_REQUIRED;
7887c478bd9Sstevel@tonic-gate 		status = kcmd(&sock, &host,
7897c478bd9Sstevel@tonic-gate 				portnumber,
7907c478bd9Sstevel@tonic-gate 				pwd->pw_name, suser,
7917c478bd9Sstevel@tonic-gate 				bp,
7927c478bd9Sstevel@tonic-gate 				0,	/* &rfd2 */
7937c478bd9Sstevel@tonic-gate 				"host",
7947c478bd9Sstevel@tonic-gate 				krb_realm,
7957c478bd9Sstevel@tonic-gate 				bsd_context,
7967c478bd9Sstevel@tonic-gate 				&auth_context,
7977c478bd9Sstevel@tonic-gate 				&cred,
7987c478bd9Sstevel@tonic-gate 				0,	/* No seq # */
7997c478bd9Sstevel@tonic-gate 				0,	/* No server seq # */
8007c478bd9Sstevel@tonic-gate 				authopts,
8017c478bd9Sstevel@tonic-gate 				1,	/* Not any port # */
8027c478bd9Sstevel@tonic-gate 				&kcmd_proto);
8037c478bd9Sstevel@tonic-gate 		if (status) {
8047c478bd9Sstevel@tonic-gate 			/*
8057c478bd9Sstevel@tonic-gate 			 * If new protocol requested, we dont
8067c478bd9Sstevel@tonic-gate 			 * fallback to less secure ones.
8077c478bd9Sstevel@tonic-gate 			 */
8087c478bd9Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
8097c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp: kcmdv2 "
8107c478bd9Sstevel@tonic-gate 					"to host %s failed - %s\n"
8117c478bd9Sstevel@tonic-gate 					"Fallback to normal rcp denied."),
8127c478bd9Sstevel@tonic-gate 					host, error_message(status));
8137c478bd9Sstevel@tonic-gate 				exit(1);
8147c478bd9Sstevel@tonic-gate 			}
8157c478bd9Sstevel@tonic-gate 			if (status != -1) {
8167c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp: kcmd "
8177c478bd9Sstevel@tonic-gate 						"to host %s failed - %s,\n"
8187c478bd9Sstevel@tonic-gate 						"trying normal rcp...\n\n"),
8197c478bd9Sstevel@tonic-gate 						host, error_message(status));
8207c478bd9Sstevel@tonic-gate 			} else {
8217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8227c478bd9Sstevel@tonic-gate 					gettext("trying normal rcp...\n"));
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 			/*
8257c478bd9Sstevel@tonic-gate 			 * kcmd() failed, so we have to
8267c478bd9Sstevel@tonic-gate 			 * fallback to normal rcp
8277c478bd9Sstevel@tonic-gate 			 */
8287c478bd9Sstevel@tonic-gate 			try_normal_rcp(prev_argc, prev_argv);
8297c478bd9Sstevel@tonic-gate 		} else {
8307c478bd9Sstevel@tonic-gate 			rem = sock;
8317c478bd9Sstevel@tonic-gate 			session_key = &cred->keyblock;
8327c478bd9Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
8337c478bd9Sstevel@tonic-gate 				status = krb5_auth_con_getlocalsubkey(
8347c478bd9Sstevel@tonic-gate 						bsd_context, auth_context,
8357c478bd9Sstevel@tonic-gate 						&session_key);
8367c478bd9Sstevel@tonic-gate 				if (status) {
8377c478bd9Sstevel@tonic-gate 					com_err("rcp", status, "determining "
8387c478bd9Sstevel@tonic-gate 						"subkey for session");
8397c478bd9Sstevel@tonic-gate 					exit(1);
8407c478bd9Sstevel@tonic-gate 				}
8417c478bd9Sstevel@tonic-gate 				if (!session_key) {
8427c478bd9Sstevel@tonic-gate 					com_err("rcp", 0, "no subkey negotiated"
8437c478bd9Sstevel@tonic-gate 						" for connection");
8447c478bd9Sstevel@tonic-gate 					exit(1);
8457c478bd9Sstevel@tonic-gate 				}
8467c478bd9Sstevel@tonic-gate 			}
8477c478bd9Sstevel@tonic-gate 			eblock.crypto_entry = session_key->enctype;
8487c478bd9Sstevel@tonic-gate 			eblock.key = (krb5_keyblock *)session_key;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 			init_encrypt(encrypt_flag, bsd_context, kcmd_proto,
8517c478bd9Sstevel@tonic-gate 					&desinbuf, &desoutbuf, CLIENT,
8527c478bd9Sstevel@tonic-gate 					&eblock);
8537c478bd9Sstevel@tonic-gate 			if (encrypt_flag > 0) {
8547c478bd9Sstevel@tonic-gate 				char *s = gettext("This rcp "
8557c478bd9Sstevel@tonic-gate 					"session is using DES "
8567c478bd9Sstevel@tonic-gate 					"encryption for all "
8577c478bd9Sstevel@tonic-gate 					"data transmissions."
8587c478bd9Sstevel@tonic-gate 					"\r\n");
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 				(void) write(2, s, strlen(s));
8617c478bd9Sstevel@tonic-gate 			}
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 		else
8667c478bd9Sstevel@tonic-gate 		{
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		/*
8697c478bd9Sstevel@tonic-gate 		 * ACL support: try to find out if the remote site is
8707c478bd9Sstevel@tonic-gate 		 * running acl cognizant version of rcp.
8717c478bd9Sstevel@tonic-gate 		 */
8727c478bd9Sstevel@tonic-gate 		aclflag = 1;
873fa9e4066Sahrens 		acl_aclflag = 1;
8747c478bd9Sstevel@tonic-gate 
875fa9e4066Sahrens 		(void) snprintf(bp, buffersize, "%s -Zf %s", cmd_sunw, src);
8767c478bd9Sstevel@tonic-gate 		rem = rcmd_af(&host, portnumber, pwd->pw_name, suser,
8777c478bd9Sstevel@tonic-gate 			    bp, 0, AF_INET6);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		if (rem < 0) {
8807c478bd9Sstevel@tonic-gate 			++errs;
8817c478bd9Sstevel@tonic-gate 			continue;
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		/*
8857c478bd9Sstevel@tonic-gate 		 * The remote system is supposed to send an ok response.
8867c478bd9Sstevel@tonic-gate 		 * If there are any data other than "ok", it must be error
8877c478bd9Sstevel@tonic-gate 		 * messages from the remote system. We can assume the
8887c478bd9Sstevel@tonic-gate 		 * remote system is running non-acl version rcp.
8897c478bd9Sstevel@tonic-gate 		 */
8907c478bd9Sstevel@tonic-gate 		if (read(rem, &resp, sizeof (resp)) != sizeof (resp))
8917c478bd9Sstevel@tonic-gate 			lostconn();
8927c478bd9Sstevel@tonic-gate 		if (resp != 0) {
893fa9e4066Sahrens 
894fa9e4066Sahrens 			/*
895fa9e4066Sahrens 			 * Try again without ace_acl support
896fa9e4066Sahrens 			 */
897fa9e4066Sahrens 			acl_aclflag = 0;
898fa9e4066Sahrens 			(void) snprintf(bp, buffersize, "%s -f %s",
899fa9e4066Sahrens 			    cmd_sunw, src);
900fa9e4066Sahrens 			rem = rcmd_af(&host, portnumber, pwd->pw_name, suser,
901fa9e4066Sahrens 			    bp, 0, AF_INET6);
902fa9e4066Sahrens 
903fa9e4066Sahrens 			if (rem < 0) {
904fa9e4066Sahrens 				++errs;
905fa9e4066Sahrens 				continue;
906fa9e4066Sahrens 			}
907fa9e4066Sahrens 
908fa9e4066Sahrens 			if (read(rem, &resp, sizeof (resp)) != sizeof (resp))
909fa9e4066Sahrens 				lostconn();
910fa9e4066Sahrens 
9117c478bd9Sstevel@tonic-gate 			/*
9127c478bd9Sstevel@tonic-gate 			 * NOT ok:
9137c478bd9Sstevel@tonic-gate 			 * The other side is running non-acl rcp.
9147c478bd9Sstevel@tonic-gate 			 * Try again with normal stuff
9157c478bd9Sstevel@tonic-gate 			 */
9167c478bd9Sstevel@tonic-gate 			aclflag = 0;
9177c478bd9Sstevel@tonic-gate 			(void) snprintf(bp, buffersize, "%s -f %s", cmd, src);
9187c478bd9Sstevel@tonic-gate 				(void) close(rem);
9197c478bd9Sstevel@tonic-gate 				host = lhost;
9207c478bd9Sstevel@tonic-gate 				rem = rcmd_af(&host, portnumber, pwd->pw_name,
9217c478bd9Sstevel@tonic-gate 						suser, bp, 0, AF_INET6);
9227c478bd9Sstevel@tonic-gate 			if (rem < 0) {
9237c478bd9Sstevel@tonic-gate 				++errs;
9247c478bd9Sstevel@tonic-gate 				continue;
9257c478bd9Sstevel@tonic-gate 			}
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 		sink(1, argv + argc - 1);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		(void) close(rem);
9327c478bd9Sstevel@tonic-gate 		rem = -1;
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate static void
9387c478bd9Sstevel@tonic-gate verifydir(char *cp)
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	struct stat stb;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if (stat(cp, &stb) >= 0) {
9437c478bd9Sstevel@tonic-gate 		if ((stb.st_mode & S_IFMT) == S_IFDIR)
9447c478bd9Sstevel@tonic-gate 			return;
9457c478bd9Sstevel@tonic-gate 		errno = ENOTDIR;
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	error("rcp: %s: %s.\n", cp, strerror(errno));
9487c478bd9Sstevel@tonic-gate 	exit(1);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate static char *
9527c478bd9Sstevel@tonic-gate colon(char *cp)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	boolean_t is_bracket_open = B_FALSE;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	for (; *cp; ++cp) {
9577c478bd9Sstevel@tonic-gate 		if (*cp == '[')
9587c478bd9Sstevel@tonic-gate 			is_bracket_open = B_TRUE;
9597c478bd9Sstevel@tonic-gate 		else if (*cp == ']')
9607c478bd9Sstevel@tonic-gate 			is_bracket_open = B_FALSE;
9617c478bd9Sstevel@tonic-gate 		else if (*cp == ':' && !is_bracket_open)
9627c478bd9Sstevel@tonic-gate 			return (cp);
9637c478bd9Sstevel@tonic-gate 		else if (*cp == '/')
9647c478bd9Sstevel@tonic-gate 			return (0);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	return (0);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate static int
9707c478bd9Sstevel@tonic-gate okname(char *cp0)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	register char *cp = cp0;
9737c478bd9Sstevel@tonic-gate 	register int c;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	do {
9767c478bd9Sstevel@tonic-gate 		c = *cp;
9777c478bd9Sstevel@tonic-gate 		if (c & 0200)
9787c478bd9Sstevel@tonic-gate 			goto bad;
9797c478bd9Sstevel@tonic-gate 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
9807c478bd9Sstevel@tonic-gate 			goto bad;
9817c478bd9Sstevel@tonic-gate 	} while (*++cp);
9827c478bd9Sstevel@tonic-gate 	return (1);
9837c478bd9Sstevel@tonic-gate bad:
9847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "rcp: invalid user name %s\n", cp0);
9857c478bd9Sstevel@tonic-gate 	return (0);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate static char *
9907c478bd9Sstevel@tonic-gate removebrackets(char *str)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	char *newstr = str;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
9957c478bd9Sstevel@tonic-gate 		newstr = str + 1;
9967c478bd9Sstevel@tonic-gate 		str[strlen(str) - 1] = '\0';
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 	return (newstr);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate static int
1002*40164e4fSsn199410 susystem(char *path, char **arglist)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate 	int status, pid, w;
10057c478bd9Sstevel@tonic-gate 	register void (*istat)(), (*qstat)();
10067c478bd9Sstevel@tonic-gate 	int pfds[2];
10077c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
10087c478bd9Sstevel@tonic-gate 	int cnt;
10097c478bd9Sstevel@tonic-gate 	boolean_t seen_stderr_traffic;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/*
10127c478bd9Sstevel@tonic-gate 	 * Due to the fact that rcp uses rsh to copy between 2 remote
10137c478bd9Sstevel@tonic-gate 	 * machines, rsh doesn't return the exit status of the remote
10147c478bd9Sstevel@tonic-gate 	 * command, and we can't modify the rcmd protocol used by rsh
10157c478bd9Sstevel@tonic-gate 	 * (for interoperability reasons) we use the hack of using any
10167c478bd9Sstevel@tonic-gate 	 * output on stderr as indication that an error occurred and
10177c478bd9Sstevel@tonic-gate 	 * that we should return a non-zero error code.
10187c478bd9Sstevel@tonic-gate 	 */
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (pipe(pfds) == -1) {
10217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Couldn't create pipe: %s\n",
10227c478bd9Sstevel@tonic-gate 		    strerror(errno));
10237c478bd9Sstevel@tonic-gate 		return (-1);
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) < 0) {
10277c478bd9Sstevel@tonic-gate 		(void) close(pfds[0]);
10287c478bd9Sstevel@tonic-gate 		(void) close(pfds[1]);
10297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Couldn't fork child process: %s\n",
10307c478bd9Sstevel@tonic-gate 		    strerror(errno));
10317c478bd9Sstevel@tonic-gate 		return (-1);
10327c478bd9Sstevel@tonic-gate 	} else if (pid == 0) {
10337c478bd9Sstevel@tonic-gate 		/*
10347c478bd9Sstevel@tonic-gate 		 * Child.
10357c478bd9Sstevel@tonic-gate 		 */
10367c478bd9Sstevel@tonic-gate 		(void) close(pfds[0]);
10377c478bd9Sstevel@tonic-gate 		/*
10387c478bd9Sstevel@tonic-gate 		 * Send stderr messages down the pipe so that we can detect
10397c478bd9Sstevel@tonic-gate 		 * them in the parent process.
10407c478bd9Sstevel@tonic-gate 		 */
10417c478bd9Sstevel@tonic-gate 		if (pfds[1] != STDERR_FILENO) {
10427c478bd9Sstevel@tonic-gate 			(void) dup2(pfds[1], STDERR_FILENO);
10437c478bd9Sstevel@tonic-gate 			(void) close(pfds[1]);
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 		/*
10467c478bd9Sstevel@tonic-gate 		 * This shell does not inherit the additional privilege
10477c478bd9Sstevel@tonic-gate 		 * we have in our Permitted set.
10487c478bd9Sstevel@tonic-gate 		 */
1049*40164e4fSsn199410 		(void) execv(path, arglist);
10507c478bd9Sstevel@tonic-gate 		_exit(127);
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 	/*
10537c478bd9Sstevel@tonic-gate 	 * Parent.
10547c478bd9Sstevel@tonic-gate 	 */
10557c478bd9Sstevel@tonic-gate 	istat = signal(SIGINT, SIG_IGN);
10567c478bd9Sstevel@tonic-gate 	qstat = signal(SIGQUIT, SIG_IGN);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	(void) close(pfds[1]);
10597c478bd9Sstevel@tonic-gate 	seen_stderr_traffic = B_FALSE;
10607c478bd9Sstevel@tonic-gate 	while ((cnt = read(pfds[0], buf, sizeof (buf))) > 0) {
10617c478bd9Sstevel@tonic-gate 		/*
10627c478bd9Sstevel@tonic-gate 		 * If any data is read from the pipe the child process
10637c478bd9Sstevel@tonic-gate 		 * has output something on stderr so we set the boolean
10647c478bd9Sstevel@tonic-gate 		 * 'seen_stderr_traffic' to true, which will cause the
10657c478bd9Sstevel@tonic-gate 		 * function to return -1.
10667c478bd9Sstevel@tonic-gate 		 */
10677c478bd9Sstevel@tonic-gate 		(void) write(STDERR_FILENO, buf, cnt);
10687c478bd9Sstevel@tonic-gate 		seen_stderr_traffic = B_TRUE;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	(void) close(pfds[0]);
10717c478bd9Sstevel@tonic-gate 	while ((w = wait(&status)) != pid && w != -1)
10727c478bd9Sstevel@tonic-gate 		;
10737c478bd9Sstevel@tonic-gate 	if (w == -1)
10747c478bd9Sstevel@tonic-gate 		status = -1;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, istat);
10777c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, qstat);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	return (seen_stderr_traffic ? -1 : status);
10807c478bd9Sstevel@tonic-gate }
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate static void
10837c478bd9Sstevel@tonic-gate source(int argc, char *argv[])
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate 	struct stat stb;
10867c478bd9Sstevel@tonic-gate 	static BUF buffer;
10877c478bd9Sstevel@tonic-gate 	BUF *bp;
10887c478bd9Sstevel@tonic-gate 	int x, readerr, f, amt;
10897c478bd9Sstevel@tonic-gate 	char *last, *name, buf[RCP_BUFSIZE];
10907c478bd9Sstevel@tonic-gate 	off_t off, size, i;
10917c478bd9Sstevel@tonic-gate 	ssize_t cnt;
10926d2b079bSjs198686 	struct linger lingerbuf;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	for (x = 0; x < argc; x++) {
10957c478bd9Sstevel@tonic-gate 		name = argv[x];
10967c478bd9Sstevel@tonic-gate 		if ((f = open(name, O_RDONLY, 0)) < 0) {
10977c478bd9Sstevel@tonic-gate 			error("rcp: %s: %s\n", name, strerror(errno));
10987c478bd9Sstevel@tonic-gate 			continue;
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 		if (fstat(f, &stb) < 0)
11017c478bd9Sstevel@tonic-gate 			goto notreg;
11027c478bd9Sstevel@tonic-gate 		switch (stb.st_mode&S_IFMT) {
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 		case S_IFREG:
11057c478bd9Sstevel@tonic-gate 			break;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		case S_IFDIR:
11087c478bd9Sstevel@tonic-gate 			if (iamrecursive) {
11097c478bd9Sstevel@tonic-gate 				(void) close(f);
11107c478bd9Sstevel@tonic-gate 				rsource(name, &stb);
11117c478bd9Sstevel@tonic-gate 				continue;
11127c478bd9Sstevel@tonic-gate 			}
11137c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
11147c478bd9Sstevel@tonic-gate 		default:
11157c478bd9Sstevel@tonic-gate notreg:
11167c478bd9Sstevel@tonic-gate 			(void) close(f);
11177c478bd9Sstevel@tonic-gate 			error("rcp: %s: not a plain file\n", name);
11187c478bd9Sstevel@tonic-gate 			continue;
11197c478bd9Sstevel@tonic-gate 		}
11207c478bd9Sstevel@tonic-gate 		last = rindex(name, '/');
11217c478bd9Sstevel@tonic-gate 		if (last == 0)
11227c478bd9Sstevel@tonic-gate 			last = name;
11237c478bd9Sstevel@tonic-gate 		else
11247c478bd9Sstevel@tonic-gate 			last++;
11257c478bd9Sstevel@tonic-gate 		if (pflag) {
11267c478bd9Sstevel@tonic-gate 			time_t mtime, atime;
11277c478bd9Sstevel@tonic-gate 			time_t now;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 			/*
11307c478bd9Sstevel@tonic-gate 			 * Make it compatible with possible future
11317c478bd9Sstevel@tonic-gate 			 * versions expecting microseconds.
11327c478bd9Sstevel@tonic-gate 			 */
11337c478bd9Sstevel@tonic-gate 			mtime = stb.st_mtime;
11347c478bd9Sstevel@tonic-gate 			atime = stb.st_atime;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 			if ((mtime < 0) || (atime < 0)) {
11377c478bd9Sstevel@tonic-gate 				now = time(NULL);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 				if (mtime < 0) {
11407c478bd9Sstevel@tonic-gate 					mtime = now;
11417c478bd9Sstevel@tonic-gate 					error("negative modification time on "
11427c478bd9Sstevel@tonic-gate 					    "%s; not preserving\n", name);
11437c478bd9Sstevel@tonic-gate 				}
11447c478bd9Sstevel@tonic-gate 				if (atime < 0) {
11457c478bd9Sstevel@tonic-gate 					atime = now;
11467c478bd9Sstevel@tonic-gate 					error("negative access time on "
11477c478bd9Sstevel@tonic-gate 					    "%s; not preserving\n", name);
11487c478bd9Sstevel@tonic-gate 				}
11497c478bd9Sstevel@tonic-gate 			}
11507c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "T%ld 0 %ld 0\n",
11517c478bd9Sstevel@tonic-gate 							mtime, atime);
11527c478bd9Sstevel@tonic-gate 			(void) desrcpwrite(rem, buf, strlen(buf));
11537c478bd9Sstevel@tonic-gate 			if (response() < 0) {
11547c478bd9Sstevel@tonic-gate 				(void) close(f);
11557c478bd9Sstevel@tonic-gate 				continue;
11567c478bd9Sstevel@tonic-gate 			}
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "C%04o %lld %s\n",
11597c478bd9Sstevel@tonic-gate 			(uint_t)(stb.st_mode & 07777), (longlong_t)stb.st_size,
11607c478bd9Sstevel@tonic-gate 			last);
11617c478bd9Sstevel@tonic-gate 		(void) desrcpwrite(rem, buf, strlen(buf));
11627c478bd9Sstevel@tonic-gate 		if (response() < 0) {
11637c478bd9Sstevel@tonic-gate 			(void) close(f);
11647c478bd9Sstevel@tonic-gate 			continue;
11657c478bd9Sstevel@tonic-gate 		}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		/* ACL support: send */
1168fa9e4066Sahrens 		if (aclflag | acl_aclflag) {
11697c478bd9Sstevel@tonic-gate 			/* get acl from f and send it over */
11707c478bd9Sstevel@tonic-gate 			if (sendacl(f) == ACL_FAIL) {
11717c478bd9Sstevel@tonic-gate 				(void) close(f);
11727c478bd9Sstevel@tonic-gate 				continue;
11737c478bd9Sstevel@tonic-gate 			}
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 		if ((krb5auth_flag > 0) || (iamremote == 1)) {
11767c478bd9Sstevel@tonic-gate 			bp = allocbuf(&buffer, f, RCP_BUFSIZE);
11777c478bd9Sstevel@tonic-gate 			if (bp == NULLBUF) {
11787c478bd9Sstevel@tonic-gate 				(void) close(f);
11797c478bd9Sstevel@tonic-gate 				continue;
11807c478bd9Sstevel@tonic-gate 			}
11817c478bd9Sstevel@tonic-gate 			readerr = 0;
11827c478bd9Sstevel@tonic-gate 			for (i = 0; i < stb.st_size; i += bp->cnt) {
11837c478bd9Sstevel@tonic-gate 				amt = bp->cnt;
11847c478bd9Sstevel@tonic-gate 				if (i + amt > stb.st_size)
11857c478bd9Sstevel@tonic-gate 					amt = stb.st_size - i;
11867c478bd9Sstevel@tonic-gate 				if (readerr == 0 &&
11877c478bd9Sstevel@tonic-gate 				    read(f, bp->buf, amt) != amt)
11887c478bd9Sstevel@tonic-gate 					readerr = errno;
11897c478bd9Sstevel@tonic-gate 				(void) desrcpwrite(rem, bp->buf, amt);
11907c478bd9Sstevel@tonic-gate 			}
11917c478bd9Sstevel@tonic-gate 			(void) close(f);
11927c478bd9Sstevel@tonic-gate 			if (readerr == 0)
11937c478bd9Sstevel@tonic-gate 				(void) desrcpwrite(rem, "", 1);
11947c478bd9Sstevel@tonic-gate 			else
11957c478bd9Sstevel@tonic-gate 				error("rcp: %s: %s\n", name,
11967c478bd9Sstevel@tonic-gate 				    error_message(readerr));
11977c478bd9Sstevel@tonic-gate 		} else {
11987c478bd9Sstevel@tonic-gate 			cnt = off = 0;
11997c478bd9Sstevel@tonic-gate 			size = stb.st_size;
12007c478bd9Sstevel@tonic-gate 			while (size != 0) {
12017c478bd9Sstevel@tonic-gate 				amt = MIN(size, SENDFILE_SIZE);
12027c478bd9Sstevel@tonic-gate 				cnt = sendfile(rem, f, &off, amt);
12039760e1c4Sblu 				if (cnt == -1) {
12049760e1c4Sblu 					if (errno == EINTR) {
12059760e1c4Sblu 						continue;
12069760e1c4Sblu 					} else {
12077c478bd9Sstevel@tonic-gate 						break;
12089760e1c4Sblu 					}
12099760e1c4Sblu 				}
12106d2b079bSjs198686 				if (cnt == 0)
12116d2b079bSjs198686 					break;
12127c478bd9Sstevel@tonic-gate 				size -= cnt;
12137c478bd9Sstevel@tonic-gate 			}
12146d2b079bSjs198686 			if (cnt < 0) {
12157c478bd9Sstevel@tonic-gate 				error("rcp: %s: %s\n", name, strerror(errno));
12166d2b079bSjs198686 			} else if (cnt == 0 && size != 0) {
12176d2b079bSjs198686 				error("rcp: %s: unexpected end of file\n",
12186d2b079bSjs198686 					name);
12196d2b079bSjs198686 				lingerbuf.l_onoff = 1;
12206d2b079bSjs198686 				lingerbuf.l_linger = 0;
12216d2b079bSjs198686 				(void) setsockopt(rem, SOL_SOCKET, SO_LINGER,
12226d2b079bSjs198686 					&lingerbuf, sizeof (lingerbuf));
12236d2b079bSjs198686 				/*
12246d2b079bSjs198686 				 * When response() (see below) is invoked it
12256d2b079bSjs198686 				 * tries to read data from closed handle which
12266d2b079bSjs198686 				 * triggers error and lostconn() function.
12276d2b079bSjs198686 				 * lostconn() terminates the program with
12286d2b079bSjs198686 				 * appropriate message.
12296d2b079bSjs198686 				 */
12306d2b079bSjs198686 				(void) close(rem);
12316d2b079bSjs198686 				rem = -1;
12327c478bd9Sstevel@tonic-gate 			} else {
12337c478bd9Sstevel@tonic-gate 				(void) write(rem, "", 1);
12347c478bd9Sstevel@tonic-gate 			}
12357c478bd9Sstevel@tonic-gate 			(void) close(f);
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 		(void) response();
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate static void
12437c478bd9Sstevel@tonic-gate rsource(char *name, struct stat *statp)
12447c478bd9Sstevel@tonic-gate {
12457c478bd9Sstevel@tonic-gate 	DIR *d;
12467c478bd9Sstevel@tonic-gate 	struct dirent *dp;
12477c478bd9Sstevel@tonic-gate 	char *last, *vect[1];
12487c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (!(d = opendir(name))) {
12517c478bd9Sstevel@tonic-gate 		error("rcp: %s: %s\n", name, strerror(errno));
12527c478bd9Sstevel@tonic-gate 		return;
12537c478bd9Sstevel@tonic-gate 	}
12547c478bd9Sstevel@tonic-gate 	last = rindex(name, '/');
12557c478bd9Sstevel@tonic-gate 	if (last == 0)
12567c478bd9Sstevel@tonic-gate 		last = name;
12577c478bd9Sstevel@tonic-gate 	else
12587c478bd9Sstevel@tonic-gate 		last++;
12597c478bd9Sstevel@tonic-gate 	if (pflag) {
12607c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "T%ld 0 %ld 0\n",
12617c478bd9Sstevel@tonic-gate 				statp->st_mtime, statp->st_atime);
12627c478bd9Sstevel@tonic-gate 		(void) desrcpwrite(rem, path, strlen(path));
12637c478bd9Sstevel@tonic-gate 		if (response() < 0) {
12647c478bd9Sstevel@tonic-gate 			(void) closedir(d);
12657c478bd9Sstevel@tonic-gate 			return;
12667c478bd9Sstevel@tonic-gate 		}
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "D%04o %d %s\n",
12697c478bd9Sstevel@tonic-gate 	    (uint_t)(statp->st_mode & 07777), 0, last);
12707c478bd9Sstevel@tonic-gate 	(void) desrcpwrite(rem, path, strlen(path));
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	/* acl support for directory */
12737c478bd9Sstevel@tonic-gate 	if (aclflag) {
12747c478bd9Sstevel@tonic-gate 		/* get acl from f and send it over */
12757c478bd9Sstevel@tonic-gate 		if (sendacl(d->dd_fd) == ACL_FAIL) {
12767c478bd9Sstevel@tonic-gate 			(void) closedir(d);
12777c478bd9Sstevel@tonic-gate 			return;
12787c478bd9Sstevel@tonic-gate 		}
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	if (response() < 0) {
12827c478bd9Sstevel@tonic-gate 		(void) closedir(d);
12837c478bd9Sstevel@tonic-gate 		return;
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	while (dp = readdir(d)) {
12877c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
12887c478bd9Sstevel@tonic-gate 			continue;
12897c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
12907c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
12917c478bd9Sstevel@tonic-gate 			continue;
12927c478bd9Sstevel@tonic-gate 		if ((uint_t)strlen(name) + 1 + strlen(dp->d_name) >=
12937c478bd9Sstevel@tonic-gate 			MAXPATHLEN - 1) {
12947c478bd9Sstevel@tonic-gate 			error("%s/%s: name too long.\n", name, dp->d_name);
12957c478bd9Sstevel@tonic-gate 			continue;
12967c478bd9Sstevel@tonic-gate 		}
12977c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s/%s",
12987c478bd9Sstevel@tonic-gate 					name, dp->d_name);
12997c478bd9Sstevel@tonic-gate 		vect[0] = path;
13007c478bd9Sstevel@tonic-gate 		source(1, vect);
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 	(void) closedir(d);
13037c478bd9Sstevel@tonic-gate 	(void) desrcpwrite(rem, "E\n", 2);
13047c478bd9Sstevel@tonic-gate 	(void) response();
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate static int
13087c478bd9Sstevel@tonic-gate response(void)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	register char *cp;
13117c478bd9Sstevel@tonic-gate 	char ch, resp, rbuf[RCP_BUFSIZE];
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if (desrcpread(rem, &resp, 1) != 1)
13147c478bd9Sstevel@tonic-gate 		lostconn();
13157c478bd9Sstevel@tonic-gate 	cp = rbuf;
13167c478bd9Sstevel@tonic-gate 	switch (resp) {
13177c478bd9Sstevel@tonic-gate 	case 0:				/* ok */
13187c478bd9Sstevel@tonic-gate 		return (0);
13197c478bd9Sstevel@tonic-gate 	default:
13207c478bd9Sstevel@tonic-gate 		*cp++ = resp;
13217c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
13227c478bd9Sstevel@tonic-gate 	case 1:				/* error, followed by err msg */
13237c478bd9Sstevel@tonic-gate 	case 2:				/* fatal error, "" */
13247c478bd9Sstevel@tonic-gate 		do {
13257c478bd9Sstevel@tonic-gate 			if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch))
13267c478bd9Sstevel@tonic-gate 				lostconn();
13277c478bd9Sstevel@tonic-gate 			*cp++ = ch;
13287c478bd9Sstevel@tonic-gate 		} while (cp < &rbuf[RCP_BUFSIZE] && ch != '\n');
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 		if (!iamremote)
13317c478bd9Sstevel@tonic-gate 			(void) write(STDERR_FILENO, rbuf, cp - rbuf);
13327c478bd9Sstevel@tonic-gate 		++errs;
13337c478bd9Sstevel@tonic-gate 		if (resp == 1)
13347c478bd9Sstevel@tonic-gate 			return (-1);
13357c478bd9Sstevel@tonic-gate 		exit(1);
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate static void
13417c478bd9Sstevel@tonic-gate lostconn(void)
13427c478bd9Sstevel@tonic-gate {
13437c478bd9Sstevel@tonic-gate 	if (!iamremote)
13447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rcp: lost connection\n");
13457c478bd9Sstevel@tonic-gate 	exit(1);
13467c478bd9Sstevel@tonic-gate }
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate static void
13507c478bd9Sstevel@tonic-gate sink(int argc, char *argv[])
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	char *cp;
13537c478bd9Sstevel@tonic-gate 	static BUF buffer;
13547c478bd9Sstevel@tonic-gate 	struct stat stb;
13557c478bd9Sstevel@tonic-gate 	struct timeval tv[2];
13567c478bd9Sstevel@tonic-gate 	BUF *bp;
13577c478bd9Sstevel@tonic-gate 	off_t i, j;
13587c478bd9Sstevel@tonic-gate 	char ch, *targ, *why;
13597c478bd9Sstevel@tonic-gate 	int amt, count, exists, first, mask, mode;
13607c478bd9Sstevel@tonic-gate 	off_t size;
13617c478bd9Sstevel@tonic-gate 	int ofd, setimes, targisdir, wrerr;
13627c478bd9Sstevel@tonic-gate 	char *np, *vect[1], buf[RCP_BUFSIZE];
13637c478bd9Sstevel@tonic-gate 	char *namebuf = NULL;
13647c478bd9Sstevel@tonic-gate 	size_t namebuf_sz = 0;
13657c478bd9Sstevel@tonic-gate 	size_t need;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate #define	atime	tv[0]
13687c478bd9Sstevel@tonic-gate #define	mtime	tv[1]
13697c478bd9Sstevel@tonic-gate #define	SCREWUP(str)	{ why = str; goto screwup; }
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	setimes = targisdir = 0;
13727c478bd9Sstevel@tonic-gate 	mask = umask(0);
13737c478bd9Sstevel@tonic-gate 	if (!pflag)
13747c478bd9Sstevel@tonic-gate 		(void) umask(mask);
13757c478bd9Sstevel@tonic-gate 	if (argc != 1) {
13767c478bd9Sstevel@tonic-gate 		error("rcp: ambiguous target\n");
13777c478bd9Sstevel@tonic-gate 		exit(1);
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 	targ = *argv;
13807c478bd9Sstevel@tonic-gate 	if (targetshouldbedirectory)
13817c478bd9Sstevel@tonic-gate 		verifydir(targ);
13827c478bd9Sstevel@tonic-gate 	(void) desrcpwrite(rem, "", 1);
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	if (stat(targ, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR)
13857c478bd9Sstevel@tonic-gate 		targisdir = 1;
13867c478bd9Sstevel@tonic-gate 	for (first = 1; ; first = 0) {
13877c478bd9Sstevel@tonic-gate 		cp = buf;
13887c478bd9Sstevel@tonic-gate 		if (desrcpread(rem, cp, 1) <= 0) {
13897c478bd9Sstevel@tonic-gate 			if (namebuf != NULL)
13907c478bd9Sstevel@tonic-gate 				free(namebuf);
13917c478bd9Sstevel@tonic-gate 			return;
13927c478bd9Sstevel@tonic-gate 		}
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 		if (*cp++ == '\n')
13957c478bd9Sstevel@tonic-gate 			SCREWUP("unexpected <newline>");
13967c478bd9Sstevel@tonic-gate 		do {
13977c478bd9Sstevel@tonic-gate 			if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch))
13987c478bd9Sstevel@tonic-gate 				SCREWUP("lost connection");
13997c478bd9Sstevel@tonic-gate 			*cp++ = ch;
14007c478bd9Sstevel@tonic-gate 		} while (cp < &buf[RCP_BUFSIZE - 1] && ch != '\n');
14017c478bd9Sstevel@tonic-gate 		*cp = 0;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 		if (buf[0] == '\01' || buf[0] == '\02') {
14047c478bd9Sstevel@tonic-gate 			if (iamremote == 0)
14057c478bd9Sstevel@tonic-gate 				(void) write(STDERR_FILENO, buf + 1,
14067c478bd9Sstevel@tonic-gate 				    strlen(buf + 1));
14077c478bd9Sstevel@tonic-gate 			if (buf[0] == '\02')
14087c478bd9Sstevel@tonic-gate 				exit(1);
14097c478bd9Sstevel@tonic-gate 			errs++;
14107c478bd9Sstevel@tonic-gate 			continue;
14117c478bd9Sstevel@tonic-gate 		}
14127c478bd9Sstevel@tonic-gate 		if (buf[0] == 'E') {
14137c478bd9Sstevel@tonic-gate 			(void) desrcpwrite(rem, "", 1);
14147c478bd9Sstevel@tonic-gate 			if (namebuf != NULL)
14157c478bd9Sstevel@tonic-gate 				free(namebuf);
14167c478bd9Sstevel@tonic-gate 			return;
14177c478bd9Sstevel@tonic-gate 		}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 		if (ch == '\n')
14207c478bd9Sstevel@tonic-gate 			*--cp = 0;
14217c478bd9Sstevel@tonic-gate 		cp = buf;
14227c478bd9Sstevel@tonic-gate 		if (*cp == 'T') {
14237c478bd9Sstevel@tonic-gate 			setimes++;
14247c478bd9Sstevel@tonic-gate 			cp++;
14257c478bd9Sstevel@tonic-gate 			mtime.tv_sec = strtol(cp, &cp, 0);
14267c478bd9Sstevel@tonic-gate 			if (*cp++ != ' ')
14277c478bd9Sstevel@tonic-gate 				SCREWUP("mtime.sec not delimited");
14287c478bd9Sstevel@tonic-gate 			mtime.tv_usec = strtol(cp, &cp, 0);
14297c478bd9Sstevel@tonic-gate 			if (*cp++ != ' ')
14307c478bd9Sstevel@tonic-gate 				SCREWUP("mtime.usec not delimited");
14317c478bd9Sstevel@tonic-gate 			atime.tv_sec = strtol(cp, &cp, 0);
14327c478bd9Sstevel@tonic-gate 			if (*cp++ != ' ')
14337c478bd9Sstevel@tonic-gate 				SCREWUP("atime.sec not delimited");
14347c478bd9Sstevel@tonic-gate 			atime.tv_usec = strtol(cp, &cp, 0);
14357c478bd9Sstevel@tonic-gate 			if (*cp++ != '\0')
14367c478bd9Sstevel@tonic-gate 				SCREWUP("atime.usec not delimited");
14377c478bd9Sstevel@tonic-gate 			(void) desrcpwrite(rem, "", 1);
14387c478bd9Sstevel@tonic-gate 			continue;
14397c478bd9Sstevel@tonic-gate 		}
14407c478bd9Sstevel@tonic-gate 		if (*cp != 'C' && *cp != 'D') {
14417c478bd9Sstevel@tonic-gate 			/*
14427c478bd9Sstevel@tonic-gate 			 * Check for the case "rcp remote:foo\* local:bar".
14437c478bd9Sstevel@tonic-gate 			 * In this case, the line "No match." can be returned
14447c478bd9Sstevel@tonic-gate 			 * by the shell before the rcp command on the remote is
14457c478bd9Sstevel@tonic-gate 			 * executed so the ^Aerror_message convention isn't
14467c478bd9Sstevel@tonic-gate 			 * followed.
14477c478bd9Sstevel@tonic-gate 			 */
14487c478bd9Sstevel@tonic-gate 			if (first) {
14497c478bd9Sstevel@tonic-gate 				error("%s\n", cp);
14507c478bd9Sstevel@tonic-gate 				exit(1);
14517c478bd9Sstevel@tonic-gate 			}
14527c478bd9Sstevel@tonic-gate 			SCREWUP("expected control record");
14537c478bd9Sstevel@tonic-gate 		}
14547c478bd9Sstevel@tonic-gate 		mode = 0;
14557c478bd9Sstevel@tonic-gate 		for (++cp; cp < buf + 5; cp++) {
14567c478bd9Sstevel@tonic-gate 			if (*cp < '0' || *cp > '7')
14577c478bd9Sstevel@tonic-gate 				SCREWUP("bad mode");
14587c478bd9Sstevel@tonic-gate 			mode = (mode << 3) | (*cp - '0');
14597c478bd9Sstevel@tonic-gate 		}
14607c478bd9Sstevel@tonic-gate 		if (*cp++ != ' ')
14617c478bd9Sstevel@tonic-gate 			SCREWUP("mode not delimited");
14627c478bd9Sstevel@tonic-gate 		size = 0;
14637c478bd9Sstevel@tonic-gate 		while (isdigit(*cp))
14647c478bd9Sstevel@tonic-gate 			size = size * 10 + (*cp++ - '0');
14657c478bd9Sstevel@tonic-gate 		if (*cp++ != ' ')
14667c478bd9Sstevel@tonic-gate 			SCREWUP("size not delimited");
14677c478bd9Sstevel@tonic-gate 		if (targisdir) {
14687c478bd9Sstevel@tonic-gate 			need = strlen(targ) + sizeof ("/") + strlen(cp);
14697c478bd9Sstevel@tonic-gate 			if (need > namebuf_sz) {
14707c478bd9Sstevel@tonic-gate 			    if ((namebuf = realloc(namebuf, need)) == NULL) {
14717c478bd9Sstevel@tonic-gate 					error("rcp: out of memory\n");
14727c478bd9Sstevel@tonic-gate 					exit(1);
14737c478bd9Sstevel@tonic-gate 			    }
14747c478bd9Sstevel@tonic-gate 			    namebuf_sz = need;
14757c478bd9Sstevel@tonic-gate 			}
14767c478bd9Sstevel@tonic-gate 			(void) snprintf(namebuf, need, "%s%s%s", targ,
14777c478bd9Sstevel@tonic-gate 			    *targ ? "/" : "", cp);
14787c478bd9Sstevel@tonic-gate 			np = namebuf;
14797c478bd9Sstevel@tonic-gate 		} else {
14807c478bd9Sstevel@tonic-gate 			np = targ;
14817c478bd9Sstevel@tonic-gate 		}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		exists = stat(np, &stb) == 0;
14847c478bd9Sstevel@tonic-gate 		if (buf[0] == 'D') {
14857c478bd9Sstevel@tonic-gate 			if (exists) {
14867c478bd9Sstevel@tonic-gate 				if ((stb.st_mode&S_IFMT) != S_IFDIR) {
1487fa9e4066Sahrens 					if (aclflag | acl_aclflag) {
14887c478bd9Sstevel@tonic-gate 						/*
14897c478bd9Sstevel@tonic-gate 						 * consume acl in the pipe
14907c478bd9Sstevel@tonic-gate 						 * fd = -1 to indicate the
14917c478bd9Sstevel@tonic-gate 						 * special case
14927c478bd9Sstevel@tonic-gate 						 */
14937c478bd9Sstevel@tonic-gate 						if (recvacl(-1, exists, pflag)
14947c478bd9Sstevel@tonic-gate 						    == ACL_FAIL) {
14957c478bd9Sstevel@tonic-gate 							goto bad;
14967c478bd9Sstevel@tonic-gate 						}
14977c478bd9Sstevel@tonic-gate 					}
14987c478bd9Sstevel@tonic-gate 					errno = ENOTDIR;
14997c478bd9Sstevel@tonic-gate 					goto bad;
15007c478bd9Sstevel@tonic-gate 				}
15017c478bd9Sstevel@tonic-gate 				if (pflag)
15027c478bd9Sstevel@tonic-gate 					(void) chmod(np, mode);
15037c478bd9Sstevel@tonic-gate 			} else if (mkdir(np, mode) < 0) {
15047c478bd9Sstevel@tonic-gate 				if (aclflag) {
15057c478bd9Sstevel@tonic-gate 					/* consume acl in the pipe */
15067c478bd9Sstevel@tonic-gate 					(void) recvacl(-1, exists, pflag);
15077c478bd9Sstevel@tonic-gate 				}
15087c478bd9Sstevel@tonic-gate 				goto bad;
15097c478bd9Sstevel@tonic-gate 			}
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 			/* acl support for directories */
1512fa9e4066Sahrens 			if (aclflag | acl_aclflag) {
15137c478bd9Sstevel@tonic-gate 				int dfd;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 				if ((dfd = open(np, O_RDONLY)) == -1)
15167c478bd9Sstevel@tonic-gate 					goto bad;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 				/* get acl and set it to ofd */
15197c478bd9Sstevel@tonic-gate 				if (recvacl(dfd, exists, pflag) == ACL_FAIL) {
15207c478bd9Sstevel@tonic-gate 					(void) close(dfd);
15217c478bd9Sstevel@tonic-gate 					if (!exists)
15227c478bd9Sstevel@tonic-gate 						(void) rmdir(np);
15237c478bd9Sstevel@tonic-gate 					goto bad;
15247c478bd9Sstevel@tonic-gate 				}
15257c478bd9Sstevel@tonic-gate 				(void) close(dfd);
15267c478bd9Sstevel@tonic-gate 			}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 			vect[0] = np;
15297c478bd9Sstevel@tonic-gate 			sink(1, vect);
15307c478bd9Sstevel@tonic-gate 			if (setimes) {
15317c478bd9Sstevel@tonic-gate 				setimes = 0;
15327c478bd9Sstevel@tonic-gate 				if (utimes(np, tv) < 0)
15337c478bd9Sstevel@tonic-gate 				    error("rcp: can't set times on %s: %s\n",
15347c478bd9Sstevel@tonic-gate 					np, strerror(errno));
15357c478bd9Sstevel@tonic-gate 			}
15367c478bd9Sstevel@tonic-gate 			continue;
15377c478bd9Sstevel@tonic-gate 		}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
15407c478bd9Sstevel@tonic-gate bad:
15417c478bd9Sstevel@tonic-gate 			error("rcp: %s: %s\n", np, strerror(errno));
15427c478bd9Sstevel@tonic-gate 			continue;
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 		/*
15467c478bd9Sstevel@tonic-gate 		 * If the output file exists we have to force zflag off
15477c478bd9Sstevel@tonic-gate 		 * to avoid erroneously seeking past old data.
15487c478bd9Sstevel@tonic-gate 		 */
15497c478bd9Sstevel@tonic-gate 		zopen(ofd, zflag && !exists);
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 		if (exists && pflag)
15527c478bd9Sstevel@tonic-gate 			(void) fchmod(ofd, mode);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 		(void) desrcpwrite(rem, "", 1);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		/*
15577c478bd9Sstevel@tonic-gate 		 * ACL support: receiving
15587c478bd9Sstevel@tonic-gate 		 */
1559fa9e4066Sahrens 		if (aclflag | acl_aclflag) {
15607c478bd9Sstevel@tonic-gate 			/* get acl and set it to ofd */
15617c478bd9Sstevel@tonic-gate 			if (recvacl(ofd, exists, pflag) == ACL_FAIL) {
15627c478bd9Sstevel@tonic-gate 				(void) close(ofd);
15637c478bd9Sstevel@tonic-gate 				if (!exists)
15647c478bd9Sstevel@tonic-gate 					(void) unlink(np);
15657c478bd9Sstevel@tonic-gate 				continue;
15667c478bd9Sstevel@tonic-gate 			}
15677c478bd9Sstevel@tonic-gate 		}
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 		if ((bp = allocbuf(&buffer, ofd, RCP_BUFSIZE)) == 0) {
15707c478bd9Sstevel@tonic-gate 			(void) close(ofd);
15717c478bd9Sstevel@tonic-gate 			continue;
15727c478bd9Sstevel@tonic-gate 		}
15737c478bd9Sstevel@tonic-gate 		cp = bp->buf;
15747c478bd9Sstevel@tonic-gate 		count = 0;
15757c478bd9Sstevel@tonic-gate 		wrerr = 0;
15767c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i += RCP_BUFSIZE) {
15777c478bd9Sstevel@tonic-gate 			amt = RCP_BUFSIZE;
15787c478bd9Sstevel@tonic-gate 			if (i + amt > size)
15797c478bd9Sstevel@tonic-gate 				amt = size - i;
15807c478bd9Sstevel@tonic-gate 			count += amt;
15817c478bd9Sstevel@tonic-gate 			do {
15827c478bd9Sstevel@tonic-gate 				j = desrcpread(rem, cp, amt);
15837c478bd9Sstevel@tonic-gate 				if (j <= 0) {
15847c478bd9Sstevel@tonic-gate 					int sverrno = errno;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 					/*
15877c478bd9Sstevel@tonic-gate 					 * Connection to supplier lost.
15887c478bd9Sstevel@tonic-gate 					 * Truncate file to correspond
15897c478bd9Sstevel@tonic-gate 					 * to amount already transferred.
15907c478bd9Sstevel@tonic-gate 					 *
15917c478bd9Sstevel@tonic-gate 					 * Note that we must call ftruncate()
15927c478bd9Sstevel@tonic-gate 					 * before any call to error() (which
15937c478bd9Sstevel@tonic-gate 					 * might result in a SIGPIPE and
15947c478bd9Sstevel@tonic-gate 					 * sudden death before we have a chance
15957c478bd9Sstevel@tonic-gate 					 * to correct the file's size).
15967c478bd9Sstevel@tonic-gate 					 */
15977c478bd9Sstevel@tonic-gate 					size = lseek(ofd, 0, SEEK_CUR);
15987c478bd9Sstevel@tonic-gate 					if ((ftruncate(ofd, size)  == -1) &&
15997c478bd9Sstevel@tonic-gate 					    (errno != EINVAL) &&
16007c478bd9Sstevel@tonic-gate 					    (errno != EACCES))
16017c478bd9Sstevel@tonic-gate #define		TRUNCERR	"rcp: can't truncate %s: %s\n"
16027c478bd9Sstevel@tonic-gate 						error(TRUNCERR, np,
16037c478bd9Sstevel@tonic-gate 						    strerror(errno));
16047c478bd9Sstevel@tonic-gate 					error("rcp: %s\n",
16057c478bd9Sstevel@tonic-gate 					    j ? strerror(sverrno) :
16067c478bd9Sstevel@tonic-gate 					    "dropped connection");
16077c478bd9Sstevel@tonic-gate 					(void) close(ofd);
16087c478bd9Sstevel@tonic-gate 					exit(1);
16097c478bd9Sstevel@tonic-gate 				}
16107c478bd9Sstevel@tonic-gate 				amt -= j;
16117c478bd9Sstevel@tonic-gate 				cp += j;
16127c478bd9Sstevel@tonic-gate 			} while (amt > 0);
16137c478bd9Sstevel@tonic-gate 			if (count == bp->cnt) {
16147c478bd9Sstevel@tonic-gate 				cp = bp->buf;
16157c478bd9Sstevel@tonic-gate 				if (wrerr == 0 &&
16167c478bd9Sstevel@tonic-gate 				    zwrite(ofd, cp, count) < 0)
16177c478bd9Sstevel@tonic-gate 					wrerr++;
16187c478bd9Sstevel@tonic-gate 				count = 0;
16197c478bd9Sstevel@tonic-gate 			}
16207c478bd9Sstevel@tonic-gate 		}
16217c478bd9Sstevel@tonic-gate 		if (count != 0 && wrerr == 0 &&
16227c478bd9Sstevel@tonic-gate 		    zwrite(ofd, bp->buf, count) < 0)
16237c478bd9Sstevel@tonic-gate 			wrerr++;
16247c478bd9Sstevel@tonic-gate 		if (zclose(ofd) < 0)
16257c478bd9Sstevel@tonic-gate 			wrerr++;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 		if ((ftruncate(ofd, size)  == -1) && (errno != EINVAL) &&
16297c478bd9Sstevel@tonic-gate 		    (errno != EACCES)) {
16307c478bd9Sstevel@tonic-gate 			error(TRUNCERR, np, strerror(errno));
16317c478bd9Sstevel@tonic-gate 		}
16327c478bd9Sstevel@tonic-gate 		(void) close(ofd);
16337c478bd9Sstevel@tonic-gate 		(void) response();
16347c478bd9Sstevel@tonic-gate 		if (setimes) {
16357c478bd9Sstevel@tonic-gate 			setimes = 0;
16367c478bd9Sstevel@tonic-gate 			if (utimes(np, tv) < 0)
16377c478bd9Sstevel@tonic-gate 				error("rcp: can't set times on %s: %s\n",
16387c478bd9Sstevel@tonic-gate 				    np, strerror(errno));
16397c478bd9Sstevel@tonic-gate 		}
16407c478bd9Sstevel@tonic-gate 		if (wrerr)
16417c478bd9Sstevel@tonic-gate 			error("rcp: %s: %s\n", np, strerror(errno));
16427c478bd9Sstevel@tonic-gate 		else
16437c478bd9Sstevel@tonic-gate 			(void) desrcpwrite(rem, "", 1);
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate screwup:
16467c478bd9Sstevel@tonic-gate 	error("rcp: protocol screwup: %s\n", why);
16477c478bd9Sstevel@tonic-gate 	exit(1);
16487c478bd9Sstevel@tonic-gate }
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate #ifndef roundup
16517c478bd9Sstevel@tonic-gate #define	roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
16527c478bd9Sstevel@tonic-gate #endif /* !roundup */
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate static BUF *
16557c478bd9Sstevel@tonic-gate allocbuf(BUF *bp, int fd, int blksize)
16567c478bd9Sstevel@tonic-gate {
16577c478bd9Sstevel@tonic-gate 	struct stat stb;
16587c478bd9Sstevel@tonic-gate 	int size;
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stb) < 0) {
16617c478bd9Sstevel@tonic-gate 		error("rcp: fstat: %s\n", strerror(errno));
16627c478bd9Sstevel@tonic-gate 		return (0);
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 	size = roundup(stb.st_blksize, blksize);
16657c478bd9Sstevel@tonic-gate 	if (size == 0)
16667c478bd9Sstevel@tonic-gate 		size = blksize;
16677c478bd9Sstevel@tonic-gate 	if (bp->cnt < size) {
16687c478bd9Sstevel@tonic-gate 		if (bp->buf != 0)
16697c478bd9Sstevel@tonic-gate 			free(bp->buf);
16707c478bd9Sstevel@tonic-gate 		bp->buf = (char *)malloc((uint_t)size);
16717c478bd9Sstevel@tonic-gate 		if (!bp->buf) {
16727c478bd9Sstevel@tonic-gate 			error("rcp: malloc: out of memory\n");
16737c478bd9Sstevel@tonic-gate 			return (0);
16747c478bd9Sstevel@tonic-gate 		}
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 	bp->cnt = size;
16777c478bd9Sstevel@tonic-gate 	return (bp);
16787c478bd9Sstevel@tonic-gate }
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate static void
16817c478bd9Sstevel@tonic-gate usage(void)
16827c478bd9Sstevel@tonic-gate {
16837c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: \t%s\t%s", gettext("Usage"),
16847c478bd9Sstevel@tonic-gate 		gettext("\trcp [-p] [-a] [-x] [-k realm] [-PN / -PO] "
16857c478bd9Sstevel@tonic-gate #ifdef DEBUG
16867c478bd9Sstevel@tonic-gate 			"[-D port] "
16877c478bd9Sstevel@tonic-gate #endif /* DEBUG */
16887c478bd9Sstevel@tonic-gate 			"f1 f2; or:\n"),
16897c478bd9Sstevel@tonic-gate 		gettext("\trcp [-r] [-p] [-a] [-x] "
16907c478bd9Sstevel@tonic-gate #ifdef DEBUG
16917c478bd9Sstevel@tonic-gate 			"[-D port] "
16927c478bd9Sstevel@tonic-gate #endif /* DEBUG */
16937c478bd9Sstevel@tonic-gate 			"[-k realm] [-PN / -PO] f1...fn d2\n"));
16947c478bd9Sstevel@tonic-gate 	exit(1);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /*
16997c478bd9Sstevel@tonic-gate  * sparse file support
17007c478bd9Sstevel@tonic-gate  */
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate static off_t zbsize;
17037c478bd9Sstevel@tonic-gate static off_t zlastseek;
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate /* is it ok to try to create holes? */
17067c478bd9Sstevel@tonic-gate static void
17077c478bd9Sstevel@tonic-gate zopen(int fd, int flag)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	struct stat st;
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	zbsize = 0;
17127c478bd9Sstevel@tonic-gate 	zlastseek = 0;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if (flag &&
17157c478bd9Sstevel@tonic-gate 		fstat(fd, &st) == 0 &&
17167c478bd9Sstevel@tonic-gate 		(st.st_mode & S_IFMT) == S_IFREG)
17177c478bd9Sstevel@tonic-gate 		zbsize = st.st_blksize;
17187c478bd9Sstevel@tonic-gate }
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate /* write and/or seek */
17217c478bd9Sstevel@tonic-gate static int
17227c478bd9Sstevel@tonic-gate zwrite(int fd, char *buf, int nbytes)
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	off_t block = zbsize ? zbsize : nbytes;
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	do {
17277c478bd9Sstevel@tonic-gate 		if (block > nbytes)
17287c478bd9Sstevel@tonic-gate 			block = nbytes;
17297c478bd9Sstevel@tonic-gate 		nbytes -= block;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 		if (!zbsize || notzero(buf, block)) {
17327c478bd9Sstevel@tonic-gate 			register int n, count = block;
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 			do {
17357c478bd9Sstevel@tonic-gate 				if ((n = write(fd, buf, count)) < 0)
17367c478bd9Sstevel@tonic-gate 					return (-1);
17377c478bd9Sstevel@tonic-gate 				buf += n;
17387c478bd9Sstevel@tonic-gate 			} while ((count -= n) > 0);
17397c478bd9Sstevel@tonic-gate 			zlastseek = 0;
17407c478bd9Sstevel@tonic-gate 		} else {
17417c478bd9Sstevel@tonic-gate 			if (lseek(fd, (off_t)block, SEEK_CUR) < 0)
17427c478bd9Sstevel@tonic-gate 				return (-1);
17437c478bd9Sstevel@tonic-gate 			buf += block;
17447c478bd9Sstevel@tonic-gate 			zlastseek = 1;
17457c478bd9Sstevel@tonic-gate 		}
17467c478bd9Sstevel@tonic-gate 	} while (nbytes > 0);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	return (0);
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate /* write last byte of file if necessary */
17527c478bd9Sstevel@tonic-gate static int
17537c478bd9Sstevel@tonic-gate zclose(int fd)
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate 	zbsize = 0;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	if (zlastseek && (lseek(fd, (off_t)-1, SEEK_CUR) < 0 ||
17587c478bd9Sstevel@tonic-gate 		zwrite(fd, "", 1) < 0))
17597c478bd9Sstevel@tonic-gate 		return (-1);
17607c478bd9Sstevel@tonic-gate 	else
17617c478bd9Sstevel@tonic-gate 		return (0);
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate /* return true if buffer is not all zeros */
17657c478bd9Sstevel@tonic-gate static int
17667c478bd9Sstevel@tonic-gate notzero(char *p, int n)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate 	register int result = 0;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	while ((int)p & 3 && --n >= 0)
17717c478bd9Sstevel@tonic-gate 		result |= *p++;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	while ((n -= 4 * sizeof (int)) >= 0) {
17747c478bd9Sstevel@tonic-gate 		/* LINTED */
17757c478bd9Sstevel@tonic-gate 		result |= ((int *)p)[0];
17767c478bd9Sstevel@tonic-gate 		/* LINTED */
17777c478bd9Sstevel@tonic-gate 		result |= ((int *)p)[1];
17787c478bd9Sstevel@tonic-gate 		/* LINTED */
17797c478bd9Sstevel@tonic-gate 		result |= ((int *)p)[2];
17807c478bd9Sstevel@tonic-gate 		/* LINTED */
17817c478bd9Sstevel@tonic-gate 		result |= ((int *)p)[3];
17827c478bd9Sstevel@tonic-gate 		if (result)
17837c478bd9Sstevel@tonic-gate 			return (result);
17847c478bd9Sstevel@tonic-gate 		p += 4 * sizeof (int);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 	n += 4 * sizeof (int);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	while (--n >= 0)
17897c478bd9Sstevel@tonic-gate 		result |= *p++;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	return (result);
17927c478bd9Sstevel@tonic-gate }
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate /*
17957c478bd9Sstevel@tonic-gate  * New functions to support ACLs
17967c478bd9Sstevel@tonic-gate  */
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate /*
17997c478bd9Sstevel@tonic-gate  * Get acl from f and send it over.
18007c478bd9Sstevel@tonic-gate  * ACL record includes acl entry count, acl text length, and acl text.
18017c478bd9Sstevel@tonic-gate  */
18027c478bd9Sstevel@tonic-gate static int
18037c478bd9Sstevel@tonic-gate sendacl(int f)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	int		aclcnt;
18067c478bd9Sstevel@tonic-gate 	char		*acltext;
18077c478bd9Sstevel@tonic-gate 	char		buf[BUFSIZ];
1808fa9e4066Sahrens 	acl_t		*aclp;
1809fa9e4066Sahrens 	char		acltype;
1810fa9e4066Sahrens 	int		aclerror;
1811fa9e4066Sahrens 	int		trivial;
18127c478bd9Sstevel@tonic-gate 
1813fa9e4066Sahrens 
1814fa9e4066Sahrens 	aclerror = facl_get(f, ACL_NO_TRIVIAL, &aclp);
1815fa9e4066Sahrens 	if (aclerror != 0) {
1816fa9e4066Sahrens 		error("can't retrieve ACL: %s \n", acl_strerror(aclerror));
18177c478bd9Sstevel@tonic-gate 		return (ACL_FAIL);
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 
1820fa9e4066Sahrens 	/*
1821fa9e4066Sahrens 	 * if acl type is not ACLENT_T and were operating in acl_aclflag == 0
1822fa9e4066Sahrens 	 * then don't do the malloc and facl(fd, getcntcmd,...);
1823fa9e4066Sahrens 	 * since the remote side doesn't support alternate style ACL's.
1824fa9e4066Sahrens 	 */
1825fa9e4066Sahrens 
1826fa9e4066Sahrens 	if (aclp && (acl_type(aclp) != ACLENT_T) && (acl_aclflag == 0)) {
1827fa9e4066Sahrens 		aclcnt = MIN_ACL_ENTRIES;
1828fa9e4066Sahrens 		acltype = 'A';
1829fa9e4066Sahrens 		trivial = ACL_IS_TRIVIAL;
1830fa9e4066Sahrens 	} else {
1831fa9e4066Sahrens 
1832fa9e4066Sahrens 		aclcnt = (aclp != NULL) ? acl_cnt(aclp) : 0;
1833fa9e4066Sahrens 
1834fa9e4066Sahrens 		if (aclp) {
1835fa9e4066Sahrens 			acltype = (acl_type(aclp) != ACLENT_T) ? 'Z' : 'A';
1836fa9e4066Sahrens 			aclcnt = acl_cnt(aclp);
1837fa9e4066Sahrens 			trivial = (acl_flags(aclp) & ACL_IS_TRIVIAL);
1838fa9e4066Sahrens 		} else {
1839fa9e4066Sahrens 			acltype = 'A';
1840fa9e4066Sahrens 			aclcnt = MIN_ACL_ENTRIES;
1841fa9e4066Sahrens 			trivial = ACL_IS_TRIVIAL;
1842fa9e4066Sahrens 		}
1843fa9e4066Sahrens 
1844fa9e4066Sahrens 	}
1845fa9e4066Sahrens 
18467c478bd9Sstevel@tonic-gate 	/* send the acl count over */
1847fa9e4066Sahrens 	(void) snprintf(buf, sizeof (buf), "%c%d\n", acltype, aclcnt);
18487c478bd9Sstevel@tonic-gate 	(void) desrcpwrite(rem, buf, strlen(buf));
18497c478bd9Sstevel@tonic-gate 
1850fa9e4066Sahrens 	/*
1851fa9e4066Sahrens 	 * only send acl when we have an aclp, which would
1852fa9e4066Sahrens 	 * imply its not trivial.
1853fa9e4066Sahrens 	 */
1854fa9e4066Sahrens 	if (aclp && (trivial != ACL_IS_TRIVIAL)) {
18555a5eeccaSmarks 		acltext = acl_totext(aclp, 0);
18567c478bd9Sstevel@tonic-gate 		if (acltext == NULL) {
18577c478bd9Sstevel@tonic-gate 			error("rcp: failed to convert to text\n");
1858fa9e4066Sahrens 			acl_free(aclp);
18597c478bd9Sstevel@tonic-gate 			return (ACL_FAIL);
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 		/* send ACLs over: send the length first */
1863fa9e4066Sahrens 		(void) snprintf(buf, sizeof (buf), "%c%d\n",
1864fa9e4066Sahrens 		    acltype, strlen(acltext));
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 		(void) desrcpwrite(rem, buf, strlen(buf));
18677c478bd9Sstevel@tonic-gate 		(void) desrcpwrite(rem, acltext, strlen(acltext));
18687c478bd9Sstevel@tonic-gate 		free(acltext);
1869fa9e4066Sahrens 		if (response() < 0) {
1870fa9e4066Sahrens 			acl_free(aclp);
18717c478bd9Sstevel@tonic-gate 			return (ACL_FAIL);
1872fa9e4066Sahrens 		}
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	}
1875fa9e4066Sahrens 
1876fa9e4066Sahrens 	if (aclp)
1877fa9e4066Sahrens 		acl_free(aclp);
18787c478bd9Sstevel@tonic-gate 	return (ACL_OK);
18797c478bd9Sstevel@tonic-gate }
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate /*
18827c478bd9Sstevel@tonic-gate  * Use this routine to get acl entry count and acl text size (in bytes)
18837c478bd9Sstevel@tonic-gate  */
18847c478bd9Sstevel@tonic-gate static int
1885fa9e4066Sahrens getaclinfo(int *cnt, int *acltype)
18867c478bd9Sstevel@tonic-gate {
18877c478bd9Sstevel@tonic-gate 	char		buf[BUFSIZ];
18887c478bd9Sstevel@tonic-gate 	char		*cp;
18897c478bd9Sstevel@tonic-gate 	char		ch;
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/* get acl count */
18927c478bd9Sstevel@tonic-gate 	cp = buf;
18937c478bd9Sstevel@tonic-gate 	if (desrcpread(rem, cp, 1) <= 0)
18947c478bd9Sstevel@tonic-gate 		return (ACL_FAIL);
1895fa9e4066Sahrens 
1896fa9e4066Sahrens 	switch (*cp++) {
1897fa9e4066Sahrens 	case 'A':
1898fa9e4066Sahrens 		*acltype = 0;
1899fa9e4066Sahrens 		break;
1900fa9e4066Sahrens 	case 'Z':
1901fa9e4066Sahrens 		*acltype = 1;
1902fa9e4066Sahrens 		break;
1903fa9e4066Sahrens 	default:
19047c478bd9Sstevel@tonic-gate 		error("rcp: expect an ACL record, but got %c\n", *cp);
19057c478bd9Sstevel@tonic-gate 		return (ACL_FAIL);
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate 	do {
19087c478bd9Sstevel@tonic-gate 		if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) {
19097c478bd9Sstevel@tonic-gate 			error("rcp: lost connection ..\n");
19107c478bd9Sstevel@tonic-gate 			return (ACL_FAIL);
19117c478bd9Sstevel@tonic-gate 		}
19127c478bd9Sstevel@tonic-gate 		*cp++ = ch;
19137c478bd9Sstevel@tonic-gate 	} while (cp < &buf[BUFSIZ - 1] && ch != '\n');
19147c478bd9Sstevel@tonic-gate 	if (ch != '\n') {
19157c478bd9Sstevel@tonic-gate 		error("rcp: ACL record corrupted \n");
19167c478bd9Sstevel@tonic-gate 		return (ACL_FAIL);
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 	cp = &buf[1];
19197c478bd9Sstevel@tonic-gate 	*cnt = strtol(cp, &cp, 0);
19207c478bd9Sstevel@tonic-gate 	if (*cp != '\n') {
19217c478bd9Sstevel@tonic-gate 		error("rcp: ACL record corrupted \n");
19227c478bd9Sstevel@tonic-gate 		return (ACL_FAIL);
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 	return (ACL_OK);
19257c478bd9Sstevel@tonic-gate }
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate /*
19297c478bd9Sstevel@tonic-gate  * Receive acl from the pipe and set it to f
19307c478bd9Sstevel@tonic-gate  */
19317c478bd9Sstevel@tonic-gate static int
19327c478bd9Sstevel@tonic-gate recvacl(int f, int exists, int preserve)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate 	int		aclcnt;		/* acl entry count */
19357c478bd9Sstevel@tonic-gate 	int		aclsize;	/* acl text length */
19367c478bd9Sstevel@tonic-gate 	int		j;
19377c478bd9Sstevel@tonic-gate 	char		*tp;
19387c478bd9Sstevel@tonic-gate 	char		*acltext;	/* external format */
1939fa9e4066Sahrens 	acl_t		*aclp;
1940fa9e4066Sahrens 	int		acltype;
1941fa9e4066Sahrens 	int		min_entries;
1942fa9e4066Sahrens 	int		aclerror;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	/* get acl count */
1945fa9e4066Sahrens 	if (getaclinfo(&aclcnt, &acltype) != ACL_OK)
19467c478bd9Sstevel@tonic-gate 		return (ACL_FAIL);
19477c478bd9Sstevel@tonic-gate 
1948fa9e4066Sahrens 	if (acltype == 0) {
1949fa9e4066Sahrens 		min_entries = MIN_ACL_ENTRIES;
1950fa9e4066Sahrens 	} else {
1951fa9e4066Sahrens 		min_entries = 1;
1952fa9e4066Sahrens 	}
1953fa9e4066Sahrens 
1954fa9e4066Sahrens 	if (aclcnt > min_entries) {
19557c478bd9Sstevel@tonic-gate 		/* get acl text size */
1956fa9e4066Sahrens 		if (getaclinfo(&aclsize, &acltype) != ACL_OK)
19577c478bd9Sstevel@tonic-gate 			return (ACL_FAIL);
19587c478bd9Sstevel@tonic-gate 		if ((acltext = malloc(aclsize + 1)) == NULL) {
19597c478bd9Sstevel@tonic-gate 			error("rcp: cant allocate memory: %d\n", aclsize);
19607c478bd9Sstevel@tonic-gate 			return (ACL_FAIL);
19617c478bd9Sstevel@tonic-gate 		}
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 		tp = acltext;
19647c478bd9Sstevel@tonic-gate 		do {
19657c478bd9Sstevel@tonic-gate 			j = desrcpread(rem, tp, aclsize);
19667c478bd9Sstevel@tonic-gate 			if (j <= 0) {
19677c478bd9Sstevel@tonic-gate 				error("rcp: %s\n", j ? strerror(errno) :
19687c478bd9Sstevel@tonic-gate 				    "dropped connection");
19697c478bd9Sstevel@tonic-gate 				exit(1);
19707c478bd9Sstevel@tonic-gate 			}
19717c478bd9Sstevel@tonic-gate 			aclsize -= j;
19727c478bd9Sstevel@tonic-gate 			tp += j;
19737c478bd9Sstevel@tonic-gate 		} while (aclsize > 0);
19747c478bd9Sstevel@tonic-gate 		*tp = '\0';
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 		if (preserve || !exists) {
1977fa9e4066Sahrens 			aclerror = acl_fromtext(acltext, &aclp);
1978fa9e4066Sahrens 			if (aclerror != 0) {
1979fa9e4066Sahrens 				error("rcp: failed to parse acl : %s\n",
1980fa9e4066Sahrens 				    acl_strerror(aclerror));
1981da3b6814Smp204432 				free(acltext);
19827c478bd9Sstevel@tonic-gate 				return (ACL_FAIL);
19837c478bd9Sstevel@tonic-gate 			}
1984fa9e4066Sahrens 
19857c478bd9Sstevel@tonic-gate 			if (f != -1) {
1986fa9e4066Sahrens 				if (facl_set(f, aclp) < 0) {
19877c478bd9Sstevel@tonic-gate 					error("rcp: failed to set acl\n");
1988da3b6814Smp204432 					acl_free(aclp);
1989da3b6814Smp204432 					free(acltext);
19907c478bd9Sstevel@tonic-gate 					return (ACL_FAIL);
19917c478bd9Sstevel@tonic-gate 				}
19927c478bd9Sstevel@tonic-gate 			}
19937c478bd9Sstevel@tonic-gate 			/* -1 means that just consume the data in the pipe */
1994fa9e4066Sahrens 			acl_free(aclp);
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 		free(acltext);
19977c478bd9Sstevel@tonic-gate 		(void) desrcpwrite(rem, "", 1);
19987c478bd9Sstevel@tonic-gate 	}
19997c478bd9Sstevel@tonic-gate 	return (ACL_OK);
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate static char *
20047c478bd9Sstevel@tonic-gate search_char(unsigned char *cp, unsigned char chr)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	int	len;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	while (*cp) {
20097c478bd9Sstevel@tonic-gate 		if (*cp == chr)
20107c478bd9Sstevel@tonic-gate 			return ((char *)cp);
20117c478bd9Sstevel@tonic-gate 		if ((len = mblen((char *)cp, MB_CUR_MAX)) <= 0)
20127c478bd9Sstevel@tonic-gate 			len = 1;
20137c478bd9Sstevel@tonic-gate 		cp += len;
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 	return (0);
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate static int
20207c478bd9Sstevel@tonic-gate desrcpread(int fd, char *buf, int len)
20217c478bd9Sstevel@tonic-gate {
20227c478bd9Sstevel@tonic-gate 	return ((int)desread(fd, buf, len, 0));
20237c478bd9Sstevel@tonic-gate }
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate static int
20267c478bd9Sstevel@tonic-gate desrcpwrite(int fd, char *buf, int len)
20277c478bd9Sstevel@tonic-gate {
20287c478bd9Sstevel@tonic-gate 	/*
20297c478bd9Sstevel@tonic-gate 	 * Note that rcp depends on the same file descriptor being both
20307c478bd9Sstevel@tonic-gate 	 * input and output to the remote side.  This is bogus, especially
20317c478bd9Sstevel@tonic-gate 	 * when rcp is being run by a rsh that pipes. Fix it here because
20327c478bd9Sstevel@tonic-gate 	 * it would require significantly more work in other places.
20337c478bd9Sstevel@tonic-gate 	 * --hartmans 1/96
20347c478bd9Sstevel@tonic-gate 	 */
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	if (fd == 0)
20377c478bd9Sstevel@tonic-gate 		fd = 1;
20387c478bd9Sstevel@tonic-gate 	return ((int)deswrite(fd, buf, len, 0));
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate static char **
20427c478bd9Sstevel@tonic-gate save_argv(int argc, char **argv)
20437c478bd9Sstevel@tonic-gate {
20447c478bd9Sstevel@tonic-gate 	int i;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	char **local_argv = (char **)calloc((unsigned)argc + 1,
20477c478bd9Sstevel@tonic-gate 	    (unsigned)sizeof (char *));
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	/*
20507c478bd9Sstevel@tonic-gate 	 * allocate an extra pointer, so that it is initialized to NULL and
20517c478bd9Sstevel@tonic-gate 	 * execv() will work
20527c478bd9Sstevel@tonic-gate 	 */
20537c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
20547c478bd9Sstevel@tonic-gate 		local_argv[i] = strsave(argv[i]);
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	return (local_argv);
20587c478bd9Sstevel@tonic-gate }
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate #define	SIZEOF_INADDR sizeof (struct in_addr)
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate static void
20637c478bd9Sstevel@tonic-gate answer_auth(char *config_file, char *ccache_file)
20647c478bd9Sstevel@tonic-gate {
20657c478bd9Sstevel@tonic-gate 	krb5_data pname_data, msg;
20667c478bd9Sstevel@tonic-gate 	krb5_creds creds, *new_creds;
20677c478bd9Sstevel@tonic-gate 	krb5_ccache cc;
20687c478bd9Sstevel@tonic-gate 	krb5_auth_context auth_context = NULL;
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	if (config_file) {
20717c478bd9Sstevel@tonic-gate 		const char *filenames[2];
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 		filenames[1] = NULL;
20747c478bd9Sstevel@tonic-gate 		filenames[0] = config_file;
20757c478bd9Sstevel@tonic-gate 		if (krb5_set_config_files(bsd_context, filenames))
20767c478bd9Sstevel@tonic-gate 			exit(1);
20777c478bd9Sstevel@tonic-gate 	}
20787c478bd9Sstevel@tonic-gate 	(void) memset((char *)&creds, 0, sizeof (creds));
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	if (krb5_read_message(bsd_context, (krb5_pointer) &rem, &pname_data))
20817c478bd9Sstevel@tonic-gate 		exit(1);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	if (krb5_read_message(bsd_context, (krb5_pointer) &rem,
20847c478bd9Sstevel@tonic-gate 	    &creds.second_ticket))
20857c478bd9Sstevel@tonic-gate 		exit(1);
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	if (ccache_file == NULL) {
20887c478bd9Sstevel@tonic-gate 		if (krb5_cc_default(bsd_context, &cc))
20897c478bd9Sstevel@tonic-gate 			exit(1);
20907c478bd9Sstevel@tonic-gate 	} else {
20917c478bd9Sstevel@tonic-gate 		if (krb5_cc_resolve(bsd_context, ccache_file, &cc))
20927c478bd9Sstevel@tonic-gate 			exit(1);
20937c478bd9Sstevel@tonic-gate 	}
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	if (krb5_cc_get_principal(bsd_context, cc, &creds.client))
20967c478bd9Sstevel@tonic-gate 		exit(1);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	if (krb5_parse_name(bsd_context, pname_data.data, &creds.server))
20997c478bd9Sstevel@tonic-gate 		exit(1);
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	krb5_xfree(pname_data.data);
21027c478bd9Sstevel@tonic-gate 	if (krb5_get_credentials(bsd_context, KRB5_GC_USER_USER, cc, &creds,
21037c478bd9Sstevel@tonic-gate 	    &new_creds))
21047c478bd9Sstevel@tonic-gate 		exit(1);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	if (krb5_mk_req_extended(bsd_context, &auth_context,
21077c478bd9Sstevel@tonic-gate 	    AP_OPTS_USE_SESSION_KEY, NULL, new_creds, &msg))
21087c478bd9Sstevel@tonic-gate 		exit(1);
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	if (krb5_write_message(bsd_context, (krb5_pointer) & rem, &msg)) {
21117c478bd9Sstevel@tonic-gate 		krb5_xfree(msg.data);
21127c478bd9Sstevel@tonic-gate 		exit(1);
21137c478bd9Sstevel@tonic-gate 	}
21147c478bd9Sstevel@tonic-gate 	/* setup eblock for des_read and write */
21157c478bd9Sstevel@tonic-gate 	krb5_copy_keyblock(bsd_context, &new_creds->keyblock, &session_key);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	/* OK process key */
21187c478bd9Sstevel@tonic-gate 	eblock.crypto_entry = session_key->enctype;
21197c478bd9Sstevel@tonic-gate 	eblock.key = (krb5_keyblock *)session_key;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	init_encrypt(encrypt_flag, bsd_context, KCMD_OLD_PROTOCOL,
21227c478bd9Sstevel@tonic-gate 	    &desinbuf, &desoutbuf, CLIENT, &eblock);
21237c478bd9Sstevel@tonic-gate 	/* cleanup */
21247c478bd9Sstevel@tonic-gate 	krb5_free_cred_contents(bsd_context, &creds);
21257c478bd9Sstevel@tonic-gate 	krb5_free_creds(bsd_context, new_creds);
21267c478bd9Sstevel@tonic-gate 	krb5_xfree(msg.data);
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate static void
21317c478bd9Sstevel@tonic-gate try_normal_rcp(int cur_argc, char **cur_argv)
21327c478bd9Sstevel@tonic-gate {
21337c478bd9Sstevel@tonic-gate 	char *target;
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	/*
21367c478bd9Sstevel@tonic-gate 	 * Reset all KRB5 relevant flags and set the
21377c478bd9Sstevel@tonic-gate 	 * cmd-buffer so that normal rcp works
21387c478bd9Sstevel@tonic-gate 	 */
21397c478bd9Sstevel@tonic-gate 	krb5auth_flag = encrypt_flag = encrypt_done = 0;
21407c478bd9Sstevel@tonic-gate 	cmd = cmd_orig;
21417c478bd9Sstevel@tonic-gate 	cmd_sunw = cmd_sunw_orig;
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	if (cur_argc < 2)
21447c478bd9Sstevel@tonic-gate 		usage();
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	if (cur_argc > 2)
21477c478bd9Sstevel@tonic-gate 		targetshouldbedirectory = 1;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	rem = -1;
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	prev_argc = cur_argc;
21527c478bd9Sstevel@tonic-gate 	prev_argv = save_argv(cur_argc, cur_argv);
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 	(void) init_service(krb5auth_flag);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	if (target = colon(cur_argv[cur_argc - 1])) {
21577c478bd9Sstevel@tonic-gate 		toremote(target, cur_argc, cur_argv);
21587c478bd9Sstevel@tonic-gate 	} else {
21597c478bd9Sstevel@tonic-gate 		tolocal(cur_argc, cur_argv);
21607c478bd9Sstevel@tonic-gate 		if (targetshouldbedirectory)
21617c478bd9Sstevel@tonic-gate 			verifydir(cur_argv[cur_argc - 1]);
21627c478bd9Sstevel@tonic-gate 	}
21637c478bd9Sstevel@tonic-gate 	exit(errs);
21647c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
21657c478bd9Sstevel@tonic-gate }
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate static int
21697c478bd9Sstevel@tonic-gate init_service(int krb5flag)
21707c478bd9Sstevel@tonic-gate {
21717c478bd9Sstevel@tonic-gate 	struct servent *sp;
21727c478bd9Sstevel@tonic-gate 	boolean_t success = B_FALSE;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	if (krb5flag > 0) {
21757c478bd9Sstevel@tonic-gate 		sp = getservbyname("kshell", "tcp");
21767c478bd9Sstevel@tonic-gate 		if (sp == NULL) {
21777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
21787c478bd9Sstevel@tonic-gate 				gettext("rcp: kshell/tcp: unknown service.\n"
21797c478bd9Sstevel@tonic-gate 				"trying normal shell/tcp service\n"));
21807c478bd9Sstevel@tonic-gate 		} else {
21817c478bd9Sstevel@tonic-gate 			portnumber = sp->s_port;
21827c478bd9Sstevel@tonic-gate 			success = B_TRUE;
21837c478bd9Sstevel@tonic-gate 		}
21847c478bd9Sstevel@tonic-gate 	} else {
21857c478bd9Sstevel@tonic-gate 		portnumber = htons(IPPORT_CMDSERVER);
21867c478bd9Sstevel@tonic-gate 		success = B_TRUE;
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 	return (success);
21897c478bd9Sstevel@tonic-gate }
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
21927c478bd9Sstevel@tonic-gate static void
21937c478bd9Sstevel@tonic-gate error(char *fmt, ...)
21947c478bd9Sstevel@tonic-gate {
21957c478bd9Sstevel@tonic-gate 	va_list ap;
21967c478bd9Sstevel@tonic-gate 	char buf[RCP_BUFSIZE];
21977c478bd9Sstevel@tonic-gate 	char *cp = buf;
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
22007c478bd9Sstevel@tonic-gate 	errs++;
22017c478bd9Sstevel@tonic-gate 	*cp++ = 1;
22027c478bd9Sstevel@tonic-gate 	(void) vsnprintf(cp, sizeof (buf) - 1, fmt, ap);
22037c478bd9Sstevel@tonic-gate 	va_end(ap);
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	(void) desrcpwrite(rem, buf, strlen(buf));
22067c478bd9Sstevel@tonic-gate 	if (iamremote == 0)
22077c478bd9Sstevel@tonic-gate 		(void) write(2, buf + 1, strlen(buf + 1));
22087c478bd9Sstevel@tonic-gate }
2209*40164e4fSsn199410 
2210*40164e4fSsn199410 static void
2211*40164e4fSsn199410 addargs(char **arglist, ...)
2212*40164e4fSsn199410 {
2213*40164e4fSsn199410 	va_list ap;
2214*40164e4fSsn199410 	int i = 0;
2215*40164e4fSsn199410 	char *pm;
2216*40164e4fSsn199410 
2217*40164e4fSsn199410 	va_start(ap, arglist);
2218*40164e4fSsn199410 	while (i < MAXARGS && (pm = va_arg(ap, char *)) != NULL)
2219*40164e4fSsn199410 		if (strcmp(pm, ""))
2220*40164e4fSsn199410 			arglist[i++] = pm;
2221*40164e4fSsn199410 	arglist[i] = NULL;
2222*40164e4fSsn199410 	va_end(ap);
2223*40164e4fSsn199410 }
2224