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