xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/rcp.c (revision ad8ef92a)
17c478bd9Sstevel@tonic-gate /*
20b4fd3b1SSurya Prakki  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 The Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
167c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  */
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #define	_FILE_OFFSET_BITS  64
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * rcp
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/file.h>
287c478bd9Sstevel@tonic-gate #include <sys/stat.h>
297c478bd9Sstevel@tonic-gate #include <sys/time.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
327c478bd9Sstevel@tonic-gate #include <sys/acl.h>
337c478bd9Sstevel@tonic-gate #include <dirent.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
377c478bd9Sstevel@tonic-gate #include <pwd.h>
387c478bd9Sstevel@tonic-gate #include <netdb.h>
397c478bd9Sstevel@tonic-gate #include <wchar.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate #include <strings.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <priv_utils.h>
507c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
527c478bd9Sstevel@tonic-gate #include <sys/wait.h>
53fa9e4066Sahrens #include <aclutils.h>
5440164e4fSsn #include <sys/varargs.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * It seems like Berkeley got these from pathnames.h?
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate #define	_PATH_RSH	"/usr/bin/rsh"
607c478bd9Sstevel@tonic-gate #define	_PATH_CP	"/usr/bin/cp"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	ACL_FAIL	1
637c478bd9Sstevel@tonic-gate #define	ACL_OK		0
647c478bd9Sstevel@tonic-gate #define	RCP_BUFSIZE	(64 * 1024)
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	RCP_ACL	"/usr/lib/sunw,rcp"
677c478bd9Sstevel@tonic-gate 		/* see PSARC/1993/004/opinion */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate typedef struct _buf {
707c478bd9Sstevel@tonic-gate 	int	cnt;
717c478bd9Sstevel@tonic-gate 	char	*buf;
727c478bd9Sstevel@tonic-gate } BUF;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static char *cmd_sunw;
757c478bd9Sstevel@tonic-gate static struct passwd *pwd;
767c478bd9Sstevel@tonic-gate static int errs;
777c478bd9Sstevel@tonic-gate static int pflag;
787c478bd9Sstevel@tonic-gate static uid_t userid;
797c478bd9Sstevel@tonic-gate static int rem;
807c478bd9Sstevel@tonic-gate static int zflag;
817c478bd9Sstevel@tonic-gate static int iamremote;
827c478bd9Sstevel@tonic-gate static int iamrecursive;
837c478bd9Sstevel@tonic-gate static int targetshouldbedirectory;
847c478bd9Sstevel@tonic-gate static int aclflag;
85fa9e4066Sahrens static int acl_aclflag;
867c478bd9Sstevel@tonic-gate static int retval = 0;
877c478bd9Sstevel@tonic-gate static int portnumber = 0;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static void lostconn(void);
907c478bd9Sstevel@tonic-gate static char *search_char(unsigned char *, unsigned char);
917c478bd9Sstevel@tonic-gate static char *removebrackets(char *);
927c478bd9Sstevel@tonic-gate static char *colon(char *);
937c478bd9Sstevel@tonic-gate static int response(void);
947c478bd9Sstevel@tonic-gate static void usage(void);
957c478bd9Sstevel@tonic-gate static void source(int, char **);
967c478bd9Sstevel@tonic-gate static void sink(int, char **);
977c478bd9Sstevel@tonic-gate static void toremote(char *, int, char **);
987c478bd9Sstevel@tonic-gate static void tolocal(int, char **);
997c478bd9Sstevel@tonic-gate static void verifydir(char *);
1007c478bd9Sstevel@tonic-gate static int okname(char *);
10140164e4fSsn static int susystem(char *, char **);
1027c478bd9Sstevel@tonic-gate static void rsource(char *, struct stat *);
1037c478bd9Sstevel@tonic-gate static int sendacl(int);
1047c478bd9Sstevel@tonic-gate static int recvacl(int, int, int);
1057c478bd9Sstevel@tonic-gate static int zwrite(int, char *, int);
1067c478bd9Sstevel@tonic-gate static void zopen(int, int);
1077c478bd9Sstevel@tonic-gate static int zclose(int);
1087c478bd9Sstevel@tonic-gate static int notzero(char *, int);
1097c478bd9Sstevel@tonic-gate static BUF *allocbuf(BUF *, int, int);
1107c478bd9Sstevel@tonic-gate static void error(char *fmt, ...);
11140164e4fSsn static void addargs(char **, ...);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * As a 32 bit application, we can only transfer (2gb - 1) i.e 0x7FFFFFFF
1157c478bd9Sstevel@tonic-gate  * bytes of data. We would like the size to be aligned to the nearest
1167c478bd9Sstevel@tonic-gate  * MAXBOFFSET (8192) boundary for optimal performance.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate #define	SENDFILE_SIZE	0x7FFFE000
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #include <k5-int.h>
1217c478bd9Sstevel@tonic-gate #include <profile/prof_int.h>
1227c478bd9Sstevel@tonic-gate #include <com_err.h>
1237c478bd9Sstevel@tonic-gate #include <kcmd.h>
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	NULLBUF	(BUF *) 0
12640164e4fSsn #define	MAXARGS	10	/* Number of arguments passed to execv() */
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 */
1413ca4cacdSPeter Shoults static krb5_context bsd_context = NULL;
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 */
1633ca4cacdSPeter Shoults static profile_options_boolean autologin_option[] = {
1643ca4cacdSPeter Shoults 	{ "autologin", &krb5auth_flag, 0 },
1653ca4cacdSPeter Shoults 	{ NULL, NULL, 0 }
1663ca4cacdSPeter Shoults };
1673ca4cacdSPeter Shoults static int no_krb5auth_flag = 0;
1683ca4cacdSPeter Shoults 
1697c478bd9Sstevel@tonic-gate static int encrypt_flag = 0;	/* Flag set, when encryption is enabled */
1707c478bd9Sstevel@tonic-gate static int encrypt_done = 0;	/* Flag set, if "-x" is specified */
1717c478bd9Sstevel@tonic-gate static enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
1747c478bd9Sstevel@tonic-gate static boolean_t rcmdoption_done = B_FALSE;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static profile_options_boolean option[] = {
1777c478bd9Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
1787c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static char *rcmdproto = NULL;
1827c478bd9Sstevel@tonic-gate static profile_option_strings rcmdversion[] = {
1837c478bd9Sstevel@tonic-gate 	{ "rcmd_protocol", &rcmdproto, 0 },
1847c478bd9Sstevel@tonic-gate 	{ NULL, NULL, 0 }
1857c478bd9Sstevel@tonic-gate };
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, "rcp", NULL };
1887c478bd9Sstevel@tonic-gate static char *appdef[] = { "appdefaults", "rcp", NULL };
1897c478bd9Sstevel@tonic-gate static char **prev_argv;
1907c478bd9Sstevel@tonic-gate static int prev_argc;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1937c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	int ch, fflag, tflag;
1967c478bd9Sstevel@tonic-gate 	char *targ;
1977c478bd9Sstevel@tonic-gate 	size_t cmdsiz;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], RCP_ACL) == 0)
2027c478bd9Sstevel@tonic-gate 		aclflag = 1;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (!(pwd = getpwuid(userid = getuid()))) {
2057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rcp: unknown user %d.\n",
2067c478bd9Sstevel@tonic-gate 		    (uint_t)userid);
2077c478bd9Sstevel@tonic-gate 		return (1);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	fflag = tflag = 0;
2113ca4cacdSPeter Shoults 	while ((ch = getopt(argc, argv, "axdfprtz:D:k:P:ZK")) != EOF) {
2127c478bd9Sstevel@tonic-gate 		switch (ch) {
2137c478bd9Sstevel@tonic-gate 		case 'd':
2147c478bd9Sstevel@tonic-gate 			targetshouldbedirectory = 1;
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		case 'f':			/* "from" */
2177c478bd9Sstevel@tonic-gate 			fflag = 1;
218fa9e4066Sahrens 			if (aclflag | acl_aclflag)
2197c478bd9Sstevel@tonic-gate 				/* ok response */
2207c478bd9Sstevel@tonic-gate 				(void) desrcpwrite(rem, "", 1);
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		case 'p':			/* preserve access/mod times */
2237c478bd9Sstevel@tonic-gate 			++pflag;
2247c478bd9Sstevel@tonic-gate 			break;
2257c478bd9Sstevel@tonic-gate 		case 'r':
2267c478bd9Sstevel@tonic-gate 			++iamrecursive;
2277c478bd9Sstevel@tonic-gate 			break;
2287c478bd9Sstevel@tonic-gate 		case 't':			/* "to" */
2297c478bd9Sstevel@tonic-gate 			tflag = 1;
2307c478bd9Sstevel@tonic-gate 			break;
231fa9e4066Sahrens 		case 'Z':
232fa9e4066Sahrens 			acl_aclflag++;
233fa9e4066Sahrens 			break;
2343ca4cacdSPeter Shoults 		case 'K':
2353ca4cacdSPeter Shoults 			no_krb5auth_flag++;
2363ca4cacdSPeter Shoults 			break;
2377c478bd9Sstevel@tonic-gate 		case 'x':
2387c478bd9Sstevel@tonic-gate 			if (!krb5_privacy_allowed()) {
2397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp: "
240e800c2e3SMilan Jurik 				    "Encryption not supported.\n"));
2417c478bd9Sstevel@tonic-gate 				return (1);
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate 			encrypt_flag++;
2447c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2457c478bd9Sstevel@tonic-gate 			encrypt_done++;
2467c478bd9Sstevel@tonic-gate 			break;
2477c478bd9Sstevel@tonic-gate 		case 'k':
2487c478bd9Sstevel@tonic-gate 			if ((krb_realm = (char *)strdup(optarg)) == NULL) {
2497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp:"
250e800c2e3SMilan Jurik 				    " Cannot malloc.\n"));
2517c478bd9Sstevel@tonic-gate 				return (1);
2527c478bd9Sstevel@tonic-gate 			}
2537c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2547c478bd9Sstevel@tonic-gate 			break;
2557c478bd9Sstevel@tonic-gate 		case 'P':
2567c478bd9Sstevel@tonic-gate 			if (strncmp(optarg, "O", 1) == 0) {
2577c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
2587c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rcp: "
259e800c2e3SMilan Jurik 					    "Only one of -PN and -PO "
260e800c2e3SMilan Jurik 					    "allowed.\n"));
2617c478bd9Sstevel@tonic-gate 					usage();
2627c478bd9Sstevel@tonic-gate 				}
2637c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
2647c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
2657c478bd9Sstevel@tonic-gate 			} else if (strncmp(optarg, "N", 1) == 0) {
2667c478bd9Sstevel@tonic-gate 				if (rcmdoption_done == B_TRUE) {
2677c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("rcp: "
268e800c2e3SMilan Jurik 					    "Only one of -PN and -PO "
269e800c2e3SMilan Jurik 					    "allowed.\n"));
2707c478bd9Sstevel@tonic-gate 					usage();
2717c478bd9Sstevel@tonic-gate 				}
2727c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
2737c478bd9Sstevel@tonic-gate 				rcmdoption_done = B_TRUE;
2747c478bd9Sstevel@tonic-gate 			} else {
2757c478bd9Sstevel@tonic-gate 				usage();
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		case 'a':
2807c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate #ifdef DEBUG
2837c478bd9Sstevel@tonic-gate 		case 'D':
2847c478bd9Sstevel@tonic-gate 			portnumber = htons(atoi(optarg));
2857c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2887c478bd9Sstevel@tonic-gate 		case '?':
2897c478bd9Sstevel@tonic-gate 		default:
2907c478bd9Sstevel@tonic-gate 			usage();
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	argc -= optind;
2947c478bd9Sstevel@tonic-gate 	argv += optind;
2957c478bd9Sstevel@tonic-gate 
2963ca4cacdSPeter Shoults 	/*
2973ca4cacdSPeter Shoults 	 * if the user disables krb5 on the cmdline (-K), then skip
2983ca4cacdSPeter Shoults 	 * all krb5 setup.
2993ca4cacdSPeter Shoults 	 *
3003ca4cacdSPeter Shoults 	 * if the user does not disable krb5 or enable krb5 on the
3013ca4cacdSPeter Shoults 	 * cmdline, check krb5.conf to see if it should be enabled.
3023ca4cacdSPeter Shoults 	 */
3033ca4cacdSPeter Shoults 
3043ca4cacdSPeter Shoults 	if (no_krb5auth_flag) {
3053ca4cacdSPeter Shoults 		krb5auth_flag = 0;
3063ca4cacdSPeter Shoults 		fflag = encrypt_flag = 0;
3073ca4cacdSPeter Shoults 	} else if (!krb5auth_flag) {
3083ca4cacdSPeter Shoults 		/* is autologin set in krb5.conf? */
3097c478bd9Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
3103ca4cacdSPeter Shoults 		/* don't sweat failure here */
3113ca4cacdSPeter Shoults 		if (!status) {
3123ca4cacdSPeter Shoults 			/*
3133ca4cacdSPeter Shoults 			 * note that the call to profile_get_options_boolean
3143ca4cacdSPeter Shoults 			 * with autologin_option can affect value of
3153ca4cacdSPeter Shoults 			 * krb5auth_flag
3163ca4cacdSPeter Shoults 			 */
3173ca4cacdSPeter Shoults 			(void) profile_get_options_boolean(bsd_context->profile,
318e800c2e3SMilan Jurik 			    appdef,
319e800c2e3SMilan Jurik 			    autologin_option);
3203ca4cacdSPeter Shoults 		}
3213ca4cacdSPeter Shoults 	}
3223ca4cacdSPeter Shoults 
3233ca4cacdSPeter Shoults 	if (krb5auth_flag > 0) {
3243ca4cacdSPeter Shoults 		if (!bsd_context) {
3253ca4cacdSPeter Shoults 			status = krb5_init_context(&bsd_context);
3263ca4cacdSPeter Shoults 			if (status) {
3273ca4cacdSPeter Shoults 				com_err("rcp", status,
3283ca4cacdSPeter Shoults 				    gettext("while initializing krb5"));
3293ca4cacdSPeter Shoults 				return (1);
3303ca4cacdSPeter Shoults 			}
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		/*
3347c478bd9Sstevel@tonic-gate 		 * Set up buffers for desread and deswrite.
3357c478bd9Sstevel@tonic-gate 		 */
3367c478bd9Sstevel@tonic-gate 		desinbuf.data = des_inbuf;
3377c478bd9Sstevel@tonic-gate 		desoutbuf.data = des_outbuf;
3387c478bd9Sstevel@tonic-gate 		desinbuf.length = sizeof (des_inbuf);
3397c478bd9Sstevel@tonic-gate 		desoutbuf.length = sizeof (des_outbuf);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (fflag || tflag)
3437c478bd9Sstevel@tonic-gate 		if (encrypt_flag > 0)
3447c478bd9Sstevel@tonic-gate 			(void) answer_auth(krb_config, krb_cache);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (fflag) {
3477c478bd9Sstevel@tonic-gate 		iamremote = 1;
3487c478bd9Sstevel@tonic-gate 		(void) response();
3497c478bd9Sstevel@tonic-gate 		(void) setuid(userid);
3507c478bd9Sstevel@tonic-gate 		source(argc, argv);
3517c478bd9Sstevel@tonic-gate 		return (errs);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (tflag) {
3557c478bd9Sstevel@tonic-gate 		iamremote = 1;
3567c478bd9Sstevel@tonic-gate 		(void) setuid(userid);
3577c478bd9Sstevel@tonic-gate 		sink(argc, argv);
3587c478bd9Sstevel@tonic-gate 		return (errs);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (argc < 2)
3627c478bd9Sstevel@tonic-gate 		usage();
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/* This will make "rcmd_af()" magically get the proper privilege */
3657c478bd9Sstevel@tonic-gate 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL) == -1) {
3667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rcp: must be set-uid root\n");
3677c478bd9Sstevel@tonic-gate 		exit(1);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
3717c478bd9Sstevel@tonic-gate 		/*
3727c478bd9Sstevel@tonic-gate 		 * Get our local realm to look up local realm options.
3737c478bd9Sstevel@tonic-gate 		 */
3747c478bd9Sstevel@tonic-gate 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
3757c478bd9Sstevel@tonic-gate 		if (status) {
3767c478bd9Sstevel@tonic-gate 			com_err("rcp", status,
377e800c2e3SMilan Jurik 			    gettext("while getting default realm"));
3787c478bd9Sstevel@tonic-gate 			return (1);
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		/*
3817c478bd9Sstevel@tonic-gate 		 * See if encryption should be done for this realm
3827c478bd9Sstevel@tonic-gate 		 */
3830b4fd3b1SSurya Prakki 		(void) profile_get_options_boolean(bsd_context->profile,
3840b4fd3b1SSurya Prakki 		    realmdef, option);
3857c478bd9Sstevel@tonic-gate 		/*
3867c478bd9Sstevel@tonic-gate 		 * Check the appdefaults section
3877c478bd9Sstevel@tonic-gate 		 */
3880b4fd3b1SSurya Prakki 		(void) profile_get_options_boolean(bsd_context->profile,
3890b4fd3b1SSurya Prakki 		    appdef, option);
3900b4fd3b1SSurya Prakki 		(void) profile_get_options_string(bsd_context->profile,
3910b4fd3b1SSurya Prakki 		    appdef, rcmdversion);
3927c478bd9Sstevel@tonic-gate 		if ((encrypt_done > 0) || (encrypt_flag > 0)) {
3937c478bd9Sstevel@tonic-gate 			if (krb5_privacy_allowed() == TRUE) {
3947c478bd9Sstevel@tonic-gate 				encrypt_flag++;
3957c478bd9Sstevel@tonic-gate 			} else {
3967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rcp: Encryption"
397e800c2e3SMilan Jurik 				    " not supported.\n"));
3987c478bd9Sstevel@tonic-gate 				return (1);
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		if ((rcmdoption_done == B_FALSE) && (rcmdproto != NULL)) {
4037c478bd9Sstevel@tonic-gate 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
4047c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
4057c478bd9Sstevel@tonic-gate 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
4067c478bd9Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
4077c478bd9Sstevel@tonic-gate 			} else {
4087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Unrecognized "
409e800c2e3SMilan Jurik 				    "KCMD protocol (%s)"), rcmdproto);
4107c478bd9Sstevel@tonic-gate 				return (1);
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (argc > 2)
4167c478bd9Sstevel@tonic-gate 		targetshouldbedirectory = 1;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	rem = -1;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (portnumber == 0) {
4217c478bd9Sstevel@tonic-gate 		if (krb5auth_flag > 0) {
4227c478bd9Sstevel@tonic-gate 			retval = init_service(krb5auth_flag);
4237c478bd9Sstevel@tonic-gate 			if (!retval) {
4247c478bd9Sstevel@tonic-gate 				/*
4257c478bd9Sstevel@tonic-gate 				 * Connecting to the kshell service failed,
4267c478bd9Sstevel@tonic-gate 				 * fallback to normal rcp & reset KRB5 flags.
4277c478bd9Sstevel@tonic-gate 				 */
4287c478bd9Sstevel@tonic-gate 				krb5auth_flag = encrypt_flag = 0;
4297c478bd9Sstevel@tonic-gate 				encrypt_done = 0;
4307c478bd9Sstevel@tonic-gate 				(void) init_service(krb5auth_flag);
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 		else
4347c478bd9Sstevel@tonic-gate 			(void) init_service(krb5auth_flag);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate #ifdef DEBUG
4387c478bd9Sstevel@tonic-gate 	if (retval || krb5auth_flag) {
4397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Kerberized rcp session, "
440e800c2e3SMilan Jurik 		    "port %d in use "), portnumber);
4417c478bd9Sstevel@tonic-gate 		if (kcmd_proto == KCMD_OLD_PROTOCOL)
4427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("[kcmd ver.1]\n"));
4437c478bd9Sstevel@tonic-gate 		else
4447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("[kcmd ver.2]\n"));
4457c478bd9Sstevel@tonic-gate 	} else {
4467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Normal rcp session, port %d "
447e800c2e3SMilan Jurik 		    "in use.\n"), portnumber);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate #endif /* DEBUG */
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
4527c478bd9Sstevel@tonic-gate 		/*
4537c478bd9Sstevel@tonic-gate 		 * We calculate here a buffer size that can be used in the
4547c478bd9Sstevel@tonic-gate 		 * allocation of the three buffers cmd, cmd_orig and
4557c478bd9Sstevel@tonic-gate 		 * cmd_sunw_orig that are used to hold different incantations
4567c478bd9Sstevel@tonic-gate 		 * of rcp.
4577c478bd9Sstevel@tonic-gate 		 */
45840164e4fSsn 		cmdsiz = MAX(sizeof ("-x rcp -r -p -d -k ") +
4597c478bd9Sstevel@tonic-gate 		    strlen(krb_realm != NULL ? krb_realm : ""),
4607c478bd9Sstevel@tonic-gate 		    sizeof (RCP_ACL " -r -p -z -d"));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		if (((cmd = (char *)malloc(cmdsiz)) == NULL) ||
463e800c2e3SMilan Jurik 		    ((cmd_sunw_orig = (char *)malloc(cmdsiz)) == NULL) ||
464e800c2e3SMilan Jurik 		    ((cmd_orig = (char *)malloc(cmdsiz)) == NULL)) {
4657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rcp: Cannot "
466e800c2e3SMilan Jurik 			    "malloc.\n"));
4677c478bd9Sstevel@tonic-gate 			return (1);
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, cmdsiz, "%srcp %s%s%s%s%s",
471e800c2e3SMilan Jurik 		    encrypt_flag ? "-x " : "",
472e800c2e3SMilan Jurik 		    iamrecursive ? " -r" : "", pflag ? " -p" : "",
473e800c2e3SMilan Jurik 		    targetshouldbedirectory ? " -d" : "",
474e800c2e3SMilan Jurik 		    krb_realm != NULL ? " -k " : "",
475e800c2e3SMilan Jurik 		    krb_realm != NULL ? krb_realm : "");
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		/*
4787c478bd9Sstevel@tonic-gate 		 * We would use cmd-orig as the 'cmd-buffer' if kerberized
4797c478bd9Sstevel@tonic-gate 		 * rcp fails, in which case we fallback to normal rcp. We also
4807c478bd9Sstevel@tonic-gate 		 * save argc & argv for the same purpose
4817c478bd9Sstevel@tonic-gate 		 */
4827c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd_orig, cmdsiz, "rcp%s%s%s%s",
483e800c2e3SMilan Jurik 		    iamrecursive ? " -r" : "",
484e800c2e3SMilan Jurik 		    pflag ? " -p" : "",
485e800c2e3SMilan Jurik 		    zflag ? " -z" : "",
486e800c2e3SMilan Jurik 		    targetshouldbedirectory ? " -d" : "");
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd_sunw_orig, cmdsiz, "%s%s%s%s%s", RCP_ACL,
489e800c2e3SMilan Jurik 		    iamrecursive ? " -r" : "",
490e800c2e3SMilan Jurik 		    pflag ? " -p" : "",
491e800c2e3SMilan Jurik 		    zflag ? " -z" : "",
492e800c2e3SMilan Jurik 		    targetshouldbedirectory ? " -d" : "");
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		prev_argc = argc;
4957c478bd9Sstevel@tonic-gate 		prev_argv = save_argv(argc, argv);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	} else {
4987c478bd9Sstevel@tonic-gate 		cmdsiz = sizeof ("rcp -r -p -z -d");
4997c478bd9Sstevel@tonic-gate 		if (((cmd = (char *)malloc(cmdsiz)) == NULL)) {
5007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rcp: Cannot "
501e800c2e3SMilan Jurik 			    "malloc.\n"));
5027c478bd9Sstevel@tonic-gate 			return (1);
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, cmdsiz, "rcp%s%s%s%s",
506e800c2e3SMilan Jurik 		    iamrecursive ? " -r" : "",
507e800c2e3SMilan Jurik 		    pflag ? " -p" : "",
508e800c2e3SMilan Jurik 		    zflag ? " -z" : "",
509e800c2e3SMilan Jurik 		    targetshouldbedirectory ? " -d" : "");
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	cmdsiz = sizeof (RCP_ACL " -r -p -z -d");
5137c478bd9Sstevel@tonic-gate 	if ((cmd_sunw = (char *)malloc(cmdsiz)) == NULL) {
5147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rcp: Cannot malloc.\n"));
5157c478bd9Sstevel@tonic-gate 		return (1);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	(void) snprintf(cmd_sunw, cmdsiz, "%s%s%s%s%s", RCP_ACL,
5197c478bd9Sstevel@tonic-gate 	    iamrecursive ? " -r" : "",
5207c478bd9Sstevel@tonic-gate 	    pflag ? " -p" : "",
5217c478bd9Sstevel@tonic-gate 	    zflag ? " -z" : "",
5227c478bd9Sstevel@tonic-gate 	    targetshouldbedirectory ? " -d" : "");
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, (void (*)(int))lostconn);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (targ = colon(argv[argc - 1]))
5277c478bd9Sstevel@tonic-gate 		toremote(targ, argc, argv);
5287c478bd9Sstevel@tonic-gate 	else {
5297c478bd9Sstevel@tonic-gate 		tolocal(argc, argv);
5307c478bd9Sstevel@tonic-gate 		if (targetshouldbedirectory)
5317c478bd9Sstevel@tonic-gate 			verifydir(argv[argc - 1]);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	return (errs > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate static void
toremote(char * targ,int argc,char * argv[])5397c478bd9Sstevel@tonic-gate toremote(char *targ, int argc, char *argv[])
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 	int i;
5427c478bd9Sstevel@tonic-gate 	char *host, *src, *suser, *thost, *tuser;
5437c478bd9Sstevel@tonic-gate 	char resp;
5447c478bd9Sstevel@tonic-gate 	size_t buffersize;
5457c478bd9Sstevel@tonic-gate 	char bp[RCP_BUFSIZE];
5467c478bd9Sstevel@tonic-gate 	krb5_creds *cred;
54740164e4fSsn 	char *arglist[MAXARGS+1];
5487c478bd9Sstevel@tonic-gate 	buffersize = RCP_BUFSIZE;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	*targ++ = 0;
5517c478bd9Sstevel@tonic-gate 	if (*targ == 0)
5527c478bd9Sstevel@tonic-gate 		targ = ".";
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (thost = search_char((unsigned char *)argv[argc - 1], '@')) {
5557c478bd9Sstevel@tonic-gate 		*thost++ = 0;
5567c478bd9Sstevel@tonic-gate 		tuser = argv[argc - 1];
5577c478bd9Sstevel@tonic-gate 		if (*tuser == '\0')
5587c478bd9Sstevel@tonic-gate 			tuser = NULL;
5597c478bd9Sstevel@tonic-gate 		else if (!okname(tuser))
5607c478bd9Sstevel@tonic-gate 			exit(1);
5617c478bd9Sstevel@tonic-gate 	} else {
5627c478bd9Sstevel@tonic-gate 		thost = argv[argc - 1];
5637c478bd9Sstevel@tonic-gate 		tuser = NULL;
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	thost = removebrackets(thost);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
5687c478bd9Sstevel@tonic-gate 		src = colon(argv[i]);
5697c478bd9Sstevel@tonic-gate 		if (src) {			/* remote to remote */
5707c478bd9Sstevel@tonic-gate 			*src++ = 0;
5717c478bd9Sstevel@tonic-gate 			if (*src == 0)
5727c478bd9Sstevel@tonic-gate 				src = ".";
5737c478bd9Sstevel@tonic-gate 			host = search_char((unsigned char *)argv[i], '@');
5747c478bd9Sstevel@tonic-gate 			if (host) {
5757c478bd9Sstevel@tonic-gate 				*host++ = 0;
5767c478bd9Sstevel@tonic-gate 				host = removebrackets(host);
5777c478bd9Sstevel@tonic-gate 				suser = argv[i];
5787c478bd9Sstevel@tonic-gate 				if (*suser == '\0') {
5797c478bd9Sstevel@tonic-gate 					suser = pwd->pw_name;
5807c478bd9Sstevel@tonic-gate 				} else if (!okname(suser)) {
5817c478bd9Sstevel@tonic-gate 					errs++;
5827c478bd9Sstevel@tonic-gate 					continue;
5837c478bd9Sstevel@tonic-gate 				}
58440164e4fSsn 				(void) snprintf(bp, buffersize, "'%s%s%s:%s'",
5857c478bd9Sstevel@tonic-gate 				    tuser ? tuser : "", tuser ? "@" : "",
5867c478bd9Sstevel@tonic-gate 				    thost, targ);
58740164e4fSsn 				(void) addargs(arglist, "rsh", host, "-l",
58840164e4fSsn 				    suser, "-n", cmd, src, bp, (char *)NULL);
5897c478bd9Sstevel@tonic-gate 			} else {
5907c478bd9Sstevel@tonic-gate 				host = removebrackets(argv[i]);
59140164e4fSsn 				(void) snprintf(bp, buffersize, "'%s%s%s:%s'",
59240164e4fSsn 				    tuser ? tuser : "", tuser ? "@" : "",
59340164e4fSsn 				    thost, targ);
59440164e4fSsn 				(void) addargs(arglist, "rsh", host, "-n", cmd,
59540164e4fSsn 				    src, bp, (char *)NULL);
5967c478bd9Sstevel@tonic-gate 			}
59740164e4fSsn 			if (susystem(_PATH_RSH, arglist) == -1)
5987c478bd9Sstevel@tonic-gate 				errs++;
5997c478bd9Sstevel@tonic-gate 		} else {			/* local to remote */
6007c478bd9Sstevel@tonic-gate 			if (rem == -1) {
6017c478bd9Sstevel@tonic-gate 				host = thost;
6027c478bd9Sstevel@tonic-gate 				if (krb5auth_flag > 0) {
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 				(void) snprintf(bp, buffersize,
605e800c2e3SMilan Jurik 				    "%s -t %s", cmd, targ);
6067c478bd9Sstevel@tonic-gate 				authopts = AP_OPTS_MUTUAL_REQUIRED;
6077c478bd9Sstevel@tonic-gate 				status = kcmd(&sock, &host,
608e800c2e3SMilan Jurik 				    portnumber,
609e800c2e3SMilan Jurik 				    pwd->pw_name,
610e800c2e3SMilan Jurik 				    tuser ? tuser :
611e800c2e3SMilan Jurik 				    pwd->pw_name,
612e800c2e3SMilan Jurik 				    bp,
613e800c2e3SMilan Jurik 				    0,
614e800c2e3SMilan Jurik 				    "host",
615e800c2e3SMilan Jurik 				    krb_realm,
616e800c2e3SMilan Jurik 				    bsd_context,
617e800c2e3SMilan Jurik 				    &auth_context,
618e800c2e3SMilan Jurik 				    &cred,
619e800c2e3SMilan Jurik 				    0,	/* No seq # */
620e800c2e3SMilan Jurik 				    0,	/* No server seq # */
621e800c2e3SMilan Jurik 				    authopts,
622e800c2e3SMilan Jurik 				    0,	/* Not any port # */
623e800c2e3SMilan Jurik 				    &kcmd_proto);
6247c478bd9Sstevel@tonic-gate 				if (status) {
6257c478bd9Sstevel@tonic-gate 					/*
6267c478bd9Sstevel@tonic-gate 					 * If new protocol requested, we dont
6277c478bd9Sstevel@tonic-gate 					 * fallback to less secure ones.
6287c478bd9Sstevel@tonic-gate 					 */
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 					if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6317c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
632e800c2e3SMilan Jurik 						    gettext("rcp: kcmdv2 "
633e800c2e3SMilan Jurik 						    "to host %s failed - %s"
634e800c2e3SMilan Jurik 						    "\nFallback to normal "
635e800c2e3SMilan Jurik 						    "rcp denied."), host,
636e800c2e3SMilan Jurik 						    error_message(status));
6377c478bd9Sstevel@tonic-gate 						exit(1);
6387c478bd9Sstevel@tonic-gate 					}
6397c478bd9Sstevel@tonic-gate 					if (status != -1) {
6407c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
641e800c2e3SMilan Jurik 						    gettext("rcp: kcmd to host "
642e800c2e3SMilan Jurik 						    "%s failed - %s,\n"
643e800c2e3SMilan Jurik 						    "trying normal rcp...\n\n"),
644e800c2e3SMilan Jurik 						    host,
645e800c2e3SMilan Jurik 						    error_message(status));
6467c478bd9Sstevel@tonic-gate 					} else {
6477c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
648e800c2e3SMilan Jurik 						    gettext("trying normal"
649e800c2e3SMilan Jurik 						    " rcp...\n"));
6507c478bd9Sstevel@tonic-gate 					}
6517c478bd9Sstevel@tonic-gate 					/*
6527c478bd9Sstevel@tonic-gate 					 * kcmd() failed, so we have to
6537c478bd9Sstevel@tonic-gate 					 * fallback to normal rcp
6547c478bd9Sstevel@tonic-gate 					 */
6557c478bd9Sstevel@tonic-gate 					try_normal_rcp(prev_argc, prev_argv);
6567c478bd9Sstevel@tonic-gate 				} else {
6577c478bd9Sstevel@tonic-gate 					rem = sock;
6587c478bd9Sstevel@tonic-gate 					session_key = &cred->keyblock;
6597c478bd9Sstevel@tonic-gate 					if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6607c478bd9Sstevel@tonic-gate 						/* CSTYLED */
6617c478bd9Sstevel@tonic-gate 						status = krb5_auth_con_getlocalsubkey(bsd_context, auth_context, &session_key);
6627c478bd9Sstevel@tonic-gate 						if (status) {
6637c478bd9Sstevel@tonic-gate 							com_err("rcp", status,
664e800c2e3SMilan Jurik 							    "determining "
665e800c2e3SMilan Jurik 							    "subkey for "
666e800c2e3SMilan Jurik 							    "session");
6677c478bd9Sstevel@tonic-gate 							exit(1);
6687c478bd9Sstevel@tonic-gate 						}
6697c478bd9Sstevel@tonic-gate 						if (!session_key) {
6707c478bd9Sstevel@tonic-gate 							com_err("rcp", 0,
671e800c2e3SMilan Jurik 							    "no subkey "
672e800c2e3SMilan Jurik 							    "negotiated for"
673e800c2e3SMilan Jurik 							    " connection");
6747c478bd9Sstevel@tonic-gate 							exit(1);
6757c478bd9Sstevel@tonic-gate 						}
6767c478bd9Sstevel@tonic-gate 					}
6777c478bd9Sstevel@tonic-gate 					eblock.crypto_entry =
678e800c2e3SMilan Jurik 					    session_key->enctype;
6797c478bd9Sstevel@tonic-gate 					eblock.key =
680e800c2e3SMilan Jurik 					    (krb5_keyblock *)session_key;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 					init_encrypt(encrypt_flag,
683e800c2e3SMilan Jurik 					    bsd_context, kcmd_proto,
684e800c2e3SMilan Jurik 					    &desinbuf, &desoutbuf, CLIENT,
685e800c2e3SMilan Jurik 					    &eblock);
6867c478bd9Sstevel@tonic-gate 					if (encrypt_flag > 0) {
6877c478bd9Sstevel@tonic-gate 						char *s = gettext("This rcp "
688e800c2e3SMilan Jurik 						    "session is using "
689e800c2e3SMilan Jurik 						    "encryption for all "
690e800c2e3SMilan Jurik 						    "data transmissions."
691e800c2e3SMilan Jurik 						    "\r\n");
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 						(void) write(2, s, strlen(s));
6947c478bd9Sstevel@tonic-gate 					}
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 				if (response() < 0)
6977c478bd9Sstevel@tonic-gate 					exit(1);
6987c478bd9Sstevel@tonic-gate 
699fa9e4066Sahrens 				} else {
7007c478bd9Sstevel@tonic-gate 
701e800c2e3SMilan Jurik 					/*
702e800c2e3SMilan Jurik 					 * ACL support: try to find out if the
703e800c2e3SMilan Jurik 					 * remote site is running acl cognizant
704e800c2e3SMilan Jurik 					 * version of rcp. A special binary
705e800c2e3SMilan Jurik 					 * name is used for this purpose.
706e800c2e3SMilan Jurik 					 */
707e800c2e3SMilan Jurik 					aclflag = 1;
708e800c2e3SMilan Jurik 					acl_aclflag = 1;
7097c478bd9Sstevel@tonic-gate 
710e800c2e3SMilan Jurik 					/*
711e800c2e3SMilan Jurik 					 * First see if the remote side will
712e800c2e3SMilan Jurik 					 * support both aclent_t and ace_t
713e800c2e3SMilan Jurik 					 * acl's?
714e800c2e3SMilan Jurik 					 */
7157c478bd9Sstevel@tonic-gate 					(void) snprintf(bp, buffersize,
716e800c2e3SMilan Jurik 					    "%s -tZ %s",
717e800c2e3SMilan Jurik 					    cmd_sunw, targ);
7187c478bd9Sstevel@tonic-gate 					rem = rcmd_af(&host, portnumber,
719fa9e4066Sahrens 					    pwd->pw_name,
720fa9e4066Sahrens 					    tuser ? tuser : pwd->pw_name,
721fa9e4066Sahrens 					    bp, 0, AF_INET6);
7227c478bd9Sstevel@tonic-gate 					if (rem < 0)
7237c478bd9Sstevel@tonic-gate 						exit(1);
724fa9e4066Sahrens 
725e800c2e3SMilan Jurik 					/*
726e800c2e3SMilan Jurik 					 * This is similar to routine
727e800c2e3SMilan Jurik 					 * response(). If response is not ok,
728e800c2e3SMilan Jurik 					 * treat the other side as non-acl rcp.
729e800c2e3SMilan Jurik 					 */
730fa9e4066Sahrens 					if (read(rem, &resp, sizeof (resp))
731fa9e4066Sahrens 					    != sizeof (resp))
732fa9e4066Sahrens 						lostconn();
733fa9e4066Sahrens 					if (resp != 0) {
734e800c2e3SMilan Jurik 						acl_aclflag = 0;
735fa9e4066Sahrens 						(void) snprintf(bp, buffersize,
736e800c2e3SMilan Jurik 						    "%s -t %s", cmd_sunw, targ);
737e800c2e3SMilan Jurik 
738fa9e4066Sahrens 						(void) close(rem);
739fa9e4066Sahrens 						host = thost;
740fa9e4066Sahrens 						rem = rcmd_af(&host, portnumber,
741fa9e4066Sahrens 						    pwd->pw_name,
742fa9e4066Sahrens 						    tuser ? tuser :
743e800c2e3SMilan Jurik 						    pwd->pw_name,
744e800c2e3SMilan Jurik 						    bp, 0, AF_INET6);
745fa9e4066Sahrens 						if (rem < 0)
746fa9e4066Sahrens 							exit(1);
747e800c2e3SMilan Jurik 
748e800c2e3SMilan Jurik 						if (read(rem, &resp,
749e800c2e3SMilan Jurik 						    sizeof (resp))
750e800c2e3SMilan Jurik 						    != sizeof (resp))
751e800c2e3SMilan Jurik 							lostconn();
752e800c2e3SMilan Jurik 						if (resp != 0) {
753e800c2e3SMilan Jurik 							/*
754e800c2e3SMilan Jurik 							 * Not OK:
755e800c2e3SMilan Jurik 							 * The other side is
756e800c2e3SMilan Jurik 							 * running non-acl rcp.
757e800c2e3SMilan Jurik 							 * Try again with
758e800c2e3SMilan Jurik 							 * normal stuff.
759e800c2e3SMilan Jurik 							 */
760e800c2e3SMilan Jurik 							aclflag = 0;
761e800c2e3SMilan Jurik 							(void) snprintf(bp,
762e800c2e3SMilan Jurik 							    buffersize,
763e800c2e3SMilan Jurik 							    "%s -t %s", cmd,
764e800c2e3SMilan Jurik 							    targ);
765e800c2e3SMilan Jurik 							(void) close(rem);
766e800c2e3SMilan Jurik 							host = thost;
767e800c2e3SMilan Jurik 							rem = rcmd_af(&host,
768e800c2e3SMilan Jurik 							    portnumber,
769e800c2e3SMilan Jurik 							    pwd->pw_name,
770e800c2e3SMilan Jurik 							    tuser ? tuser :
771e800c2e3SMilan Jurik 							    pwd->pw_name, bp, 0,
772e800c2e3SMilan Jurik 							    AF_INET6);
773e800c2e3SMilan Jurik 							if (rem < 0)
774e800c2e3SMilan Jurik 								exit(1);
775e800c2e3SMilan Jurik 							if (response() < 0)
776e800c2e3SMilan Jurik 								exit(1);
777e800c2e3SMilan Jurik 						}
778fa9e4066Sahrens 					}
779e800c2e3SMilan Jurik 					/* everything should be fine now */
780e800c2e3SMilan Jurik 					(void) setuid(userid);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 				}
7837c478bd9Sstevel@tonic-gate 			}
7847c478bd9Sstevel@tonic-gate 			source(1, argv + i);
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate static void
tolocal(int argc,char * argv[])7907c478bd9Sstevel@tonic-gate tolocal(int argc, char *argv[])
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate 	int i;
7937c478bd9Sstevel@tonic-gate 	char *host, *src, *suser, *lhost;
7947c478bd9Sstevel@tonic-gate 	char resp;
7957c478bd9Sstevel@tonic-gate 	size_t buffersize;
7967c478bd9Sstevel@tonic-gate 	char bp[RCP_BUFSIZE];
7977c478bd9Sstevel@tonic-gate 	krb5_creds *cred;
79840164e4fSsn 	char *arglist[MAXARGS+1];
7997c478bd9Sstevel@tonic-gate 	buffersize = RCP_BUFSIZE;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
8027c478bd9Sstevel@tonic-gate 		if (!(src = colon(argv[i]))) {	/* local to local */
80340164e4fSsn 			(void) addargs(arglist, "cp",
80440164e4fSsn 			    iamrecursive ? "-r" : "", pflag ? "-p" : "",
80540164e4fSsn 			    zflag ? "-z" : "", argv[i], argv[argc - 1],
80640164e4fSsn 			    (char *)NULL);
80740164e4fSsn 			if (susystem(_PATH_CP, arglist) == -1)
8087c478bd9Sstevel@tonic-gate 				errs++;
8097c478bd9Sstevel@tonic-gate 			continue;
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 		*src++ = 0;
8127c478bd9Sstevel@tonic-gate 		if (*src == 0)
8137c478bd9Sstevel@tonic-gate 			src = ".";
8147c478bd9Sstevel@tonic-gate 		host = search_char((unsigned char *)argv[i], '@');
8157c478bd9Sstevel@tonic-gate 		if (host) {
8167c478bd9Sstevel@tonic-gate 			*host++ = 0;
8177c478bd9Sstevel@tonic-gate 			suser = argv[i];
8187c478bd9Sstevel@tonic-gate 			if (*suser == '\0') {
8197c478bd9Sstevel@tonic-gate 				suser = pwd->pw_name;
8207c478bd9Sstevel@tonic-gate 			} else if (!okname(suser)) {
8217c478bd9Sstevel@tonic-gate 				errs++;
8227c478bd9Sstevel@tonic-gate 				continue;
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 		} else {
8257c478bd9Sstevel@tonic-gate 			host = argv[i];
8267c478bd9Sstevel@tonic-gate 			suser = pwd->pw_name;
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 		host = removebrackets(host);
8297c478bd9Sstevel@tonic-gate 		lhost = host;
8307c478bd9Sstevel@tonic-gate 		if (krb5auth_flag > 0) {
8317c478bd9Sstevel@tonic-gate 
832e800c2e3SMilan Jurik 			(void) snprintf(bp, buffersize, "%s -f %s", cmd, src);
833e800c2e3SMilan Jurik 			authopts = AP_OPTS_MUTUAL_REQUIRED;
834e800c2e3SMilan Jurik 			status = kcmd(&sock, &host,
835e800c2e3SMilan Jurik 			    portnumber,
836e800c2e3SMilan Jurik 			    pwd->pw_name, suser,
837e800c2e3SMilan Jurik 			    bp,
838e800c2e3SMilan Jurik 			    0,	/* &rfd2 */
839e800c2e3SMilan Jurik 			    "host",
840e800c2e3SMilan Jurik 			    krb_realm,
841e800c2e3SMilan Jurik 			    bsd_context,
842e800c2e3SMilan Jurik 			    &auth_context,
843e800c2e3SMilan Jurik 			    &cred,
844e800c2e3SMilan Jurik 			    0,	/* No seq # */
845e800c2e3SMilan Jurik 			    0,	/* No server seq # */
846e800c2e3SMilan Jurik 			    authopts,
847e800c2e3SMilan Jurik 			    1,	/* Not any port # */
848e800c2e3SMilan Jurik 			    &kcmd_proto);
849e800c2e3SMilan Jurik 			if (status) {
850e800c2e3SMilan Jurik 				/*
851e800c2e3SMilan Jurik 				 * If new protocol requested, we dont
852e800c2e3SMilan Jurik 				 * fallback to less secure ones.
853e800c2e3SMilan Jurik 				 */
854e800c2e3SMilan Jurik 				if (kcmd_proto == KCMD_NEW_PROTOCOL) {
855e800c2e3SMilan Jurik 					(void) fprintf(stderr,
856e800c2e3SMilan Jurik 					    gettext("rcp: kcmdv2 "
857e800c2e3SMilan Jurik 					    "to host %s failed - %s\n"
858e800c2e3SMilan Jurik 					    "Fallback to normal rcp denied."),
859e800c2e3SMilan Jurik 					    host, error_message(status));
8607c478bd9Sstevel@tonic-gate 					exit(1);
8617c478bd9Sstevel@tonic-gate 				}
862e800c2e3SMilan Jurik 				if (status != -1) {
863e800c2e3SMilan Jurik 					(void) fprintf(stderr,
864e800c2e3SMilan Jurik 					    gettext("rcp: kcmd "
865e800c2e3SMilan Jurik 					    "to host %s failed - %s,\n"
866e800c2e3SMilan Jurik 					    "trying normal rcp...\n\n"),
867e800c2e3SMilan Jurik 					    host, error_message(status));
868e800c2e3SMilan Jurik 				} else {
869e800c2e3SMilan Jurik 					(void) fprintf(stderr,
870e800c2e3SMilan Jurik 					    gettext("trying normal rcp...\n"));
871e800c2e3SMilan Jurik 				}
872e800c2e3SMilan Jurik 				/*
873e800c2e3SMilan Jurik 				 * kcmd() failed, so we have to
874e800c2e3SMilan Jurik 				 * fallback to normal rcp
875e800c2e3SMilan Jurik 				 */
876e800c2e3SMilan Jurik 				try_normal_rcp(prev_argc, prev_argv);
877e800c2e3SMilan Jurik 			} else {
878e800c2e3SMilan Jurik 				rem = sock;
879e800c2e3SMilan Jurik 				session_key = &cred->keyblock;
880e800c2e3SMilan Jurik 				if (kcmd_proto == KCMD_NEW_PROTOCOL) {
881e800c2e3SMilan Jurik 					status = krb5_auth_con_getlocalsubkey(
882e800c2e3SMilan Jurik 					    bsd_context, auth_context,
883e800c2e3SMilan Jurik 					    &session_key);
884e800c2e3SMilan Jurik 					if (status) {
885e800c2e3SMilan Jurik 						com_err("rcp", status,
886e800c2e3SMilan Jurik 						    "determining "
887e800c2e3SMilan Jurik 						    "subkey for session");
888e800c2e3SMilan Jurik 						exit(1);
889e800c2e3SMilan Jurik 					}
890e800c2e3SMilan Jurik 					if (!session_key) {
891e800c2e3SMilan Jurik 						com_err("rcp", 0,
892e800c2e3SMilan Jurik 						    "no subkey negotiated"
893e800c2e3SMilan Jurik 						    " for connection");
894e800c2e3SMilan Jurik 						exit(1);
895e800c2e3SMilan Jurik 					}
896e800c2e3SMilan Jurik 				}
897e800c2e3SMilan Jurik 				eblock.crypto_entry = session_key->enctype;
898e800c2e3SMilan Jurik 				eblock.key = (krb5_keyblock *)session_key;
899e800c2e3SMilan Jurik 
900e800c2e3SMilan Jurik 				init_encrypt(encrypt_flag, bsd_context,
901e800c2e3SMilan Jurik 				    kcmd_proto,
902e800c2e3SMilan Jurik 				    &desinbuf, &desoutbuf, CLIENT,
903e800c2e3SMilan Jurik 				    &eblock);
904e800c2e3SMilan Jurik 				if (encrypt_flag > 0) {
905e800c2e3SMilan Jurik 					char *s = gettext("This rcp "
906e800c2e3SMilan Jurik 					    "session is using DES "
907e800c2e3SMilan Jurik 					    "encryption for all "
908e800c2e3SMilan Jurik 					    "data transmissions."
909e800c2e3SMilan Jurik 					    "\r\n");
910e800c2e3SMilan Jurik 
911e800c2e3SMilan Jurik 					(void) write(2, s, strlen(s));
9127c478bd9Sstevel@tonic-gate 				}
9137c478bd9Sstevel@tonic-gate 			}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		}
9167c478bd9Sstevel@tonic-gate 		else
9177c478bd9Sstevel@tonic-gate 		{
9187c478bd9Sstevel@tonic-gate 
919fa9e4066Sahrens 			/*
920e800c2e3SMilan Jurik 			 * ACL support: try to find out if the remote site is
921e800c2e3SMilan Jurik 			 * running acl cognizant version of rcp.
922fa9e4066Sahrens 			 */
923e800c2e3SMilan Jurik 			aclflag = 1;
924e800c2e3SMilan Jurik 			acl_aclflag = 1;
925e800c2e3SMilan Jurik 
926e800c2e3SMilan Jurik 			(void) snprintf(bp, buffersize, "%s -Zf %s", cmd_sunw,
927e800c2e3SMilan Jurik 			    src);
928fa9e4066Sahrens 			rem = rcmd_af(&host, portnumber, pwd->pw_name, suser,
929fa9e4066Sahrens 			    bp, 0, AF_INET6);
930fa9e4066Sahrens 
931fa9e4066Sahrens 			if (rem < 0) {
932fa9e4066Sahrens 				++errs;
933fa9e4066Sahrens 				continue;
934fa9e4066Sahrens 			}
935fa9e4066Sahrens 
936e800c2e3SMilan Jurik 			/*
937e800c2e3SMilan Jurik 			 * The remote system is supposed to send an ok response.
938e800c2e3SMilan Jurik 			 * If there are any data other than "ok", it must be
939e800c2e3SMilan Jurik 			 * error messages from the remote system. We can assume
940e800c2e3SMilan Jurik 			 * the remote system is running non-acl version rcp.
941e800c2e3SMilan Jurik 			 */
942fa9e4066Sahrens 			if (read(rem, &resp, sizeof (resp)) != sizeof (resp))
943fa9e4066Sahrens 				lostconn();
944fa9e4066Sahrens 
945e800c2e3SMilan Jurik 			if (resp != 0) {
946e800c2e3SMilan Jurik 
947e800c2e3SMilan Jurik 				/*
948e800c2e3SMilan Jurik 				 * Try again without ace_acl support
949e800c2e3SMilan Jurik 				 */
950e800c2e3SMilan Jurik 				acl_aclflag = 0;
951e800c2e3SMilan Jurik 				(void) snprintf(bp, buffersize, "%s -f %s",
952e800c2e3SMilan Jurik 				    cmd_sunw, src);
9537c478bd9Sstevel@tonic-gate 				(void) close(rem);
9547c478bd9Sstevel@tonic-gate 				rem = rcmd_af(&host, portnumber, pwd->pw_name,
955e800c2e3SMilan Jurik 				    suser, bp, 0, AF_INET6);
956e800c2e3SMilan Jurik 
957e800c2e3SMilan Jurik 				if (rem < 0) {
958e800c2e3SMilan Jurik 					++errs;
959e800c2e3SMilan Jurik 					continue;
960e800c2e3SMilan Jurik 				}
961e800c2e3SMilan Jurik 
962e800c2e3SMilan Jurik 				if (read(rem, &resp,
963e800c2e3SMilan Jurik 				    sizeof (resp)) != sizeof (resp))
964e800c2e3SMilan Jurik 					lostconn();
965e800c2e3SMilan Jurik 
966e800c2e3SMilan Jurik 				if (resp != 0) {
967e800c2e3SMilan Jurik 					/*
968e800c2e3SMilan Jurik 					 * NOT ok:
969e800c2e3SMilan Jurik 					 * The other side is running non-acl
970e800c2e3SMilan Jurik 					 * rcp. Try again with normal stuff.
971e800c2e3SMilan Jurik 					 */
972e800c2e3SMilan Jurik 					aclflag = 0;
973e800c2e3SMilan Jurik 					(void) snprintf(bp, buffersize,
974e800c2e3SMilan Jurik 					    "%s -f %s", cmd, src);
975e800c2e3SMilan Jurik 					(void) close(rem);
976e800c2e3SMilan Jurik 					host = lhost;
977e800c2e3SMilan Jurik 					rem = rcmd_af(&host, portnumber,
978e800c2e3SMilan Jurik 					    pwd->pw_name, suser, bp, 0,
979e800c2e3SMilan Jurik 					    AF_INET6);
980e800c2e3SMilan Jurik 					if (rem < 0) {
981e800c2e3SMilan Jurik 						++errs;
982e800c2e3SMilan Jurik 						continue;
983e800c2e3SMilan Jurik 					}
984e800c2e3SMilan Jurik 				}
9857c478bd9Sstevel@tonic-gate 			}
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		sink(1, argv + argc - 1);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		(void) close(rem);
9917c478bd9Sstevel@tonic-gate 		rem = -1;
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate static void
verifydir(char * cp)9977c478bd9Sstevel@tonic-gate verifydir(char *cp)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	struct stat stb;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (stat(cp, &stb) >= 0) {
10027c478bd9Sstevel@tonic-gate 		if ((stb.st_mode & S_IFMT) == S_IFDIR)
10037c478bd9Sstevel@tonic-gate 			return;
10047c478bd9Sstevel@tonic-gate 		errno = ENOTDIR;
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 	error("rcp: %s: %s.\n", cp, strerror(errno));
10077c478bd9Sstevel@tonic-gate 	exit(1);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate static char *
colon(char * cp)10117c478bd9Sstevel@tonic-gate colon(char *cp)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	boolean_t is_bracket_open = B_FALSE;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	for (; *cp; ++cp) {
10167c478bd9Sstevel@tonic-gate 		if (*cp == '[')
10177c478bd9Sstevel@tonic-gate 			is_bracket_open = B_TRUE;
10187c478bd9Sstevel@tonic-gate 		else if (*cp == ']')
10197c478bd9Sstevel@tonic-gate 			is_bracket_open = B_FALSE;
10207c478bd9Sstevel@tonic-gate 		else if (*cp == ':' && !is_bracket_open)
10217c478bd9Sstevel@tonic-gate 			return (cp);
10227c478bd9Sstevel@tonic-gate 		else if (*cp == '/')
10237c478bd9Sstevel@tonic-gate 			return (0);
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 	return (0);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate static int
okname(char * cp0)10297c478bd9Sstevel@tonic-gate okname(char *cp0)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	register char *cp = cp0;
10327c478bd9Sstevel@tonic-gate 	register int c;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	do {
10357c478bd9Sstevel@tonic-gate 		c = *cp;
10367c478bd9Sstevel@tonic-gate 		if (c & 0200)
10377c478bd9Sstevel@tonic-gate 			goto bad;
10387c478bd9Sstevel@tonic-gate 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
10397c478bd9Sstevel@tonic-gate 			goto bad;
10407c478bd9Sstevel@tonic-gate 	} while (*++cp);
10417c478bd9Sstevel@tonic-gate 	return (1);
10427c478bd9Sstevel@tonic-gate bad:
10437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "rcp: invalid user name %s\n", cp0);
10447c478bd9Sstevel@tonic-gate 	return (0);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate static char *
removebrackets(char * str)10497c478bd9Sstevel@tonic-gate removebrackets(char *str)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	char *newstr = str;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
10547c478bd9Sstevel@tonic-gate 		newstr = str + 1;
10557c478bd9Sstevel@tonic-gate 		str[strlen(str) - 1] = '\0';
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 	return (newstr);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate static int
susystem(char * path,char ** arglist)106140164e4fSsn susystem(char *path, char **arglist)
10627c478bd9Sstevel@tonic-gate {
10637c478bd9Sstevel@tonic-gate 	int status, pid, w;
10647c478bd9Sstevel@tonic-gate 	register void (*istat)(), (*qstat)();
10657c478bd9Sstevel@tonic-gate 	int pfds[2];
10667c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
10677c478bd9Sstevel@tonic-gate 	int cnt;
10687c478bd9Sstevel@tonic-gate 	boolean_t seen_stderr_traffic;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	/*
10717c478bd9Sstevel@tonic-gate 	 * Due to the fact that rcp uses rsh to copy between 2 remote
10727c478bd9Sstevel@tonic-gate 	 * machines, rsh doesn't return the exit status of the remote
10737c478bd9Sstevel@tonic-gate 	 * command, and we can't modify the rcmd protocol used by rsh
10747c478bd9Sstevel@tonic-gate 	 * (for interoperability reasons) we use the hack of using any
10757c478bd9Sstevel@tonic-gate 	 * output on stderr as indication that an error occurred and
10767c478bd9Sstevel@tonic-gate 	 * that we should return a non-zero error code.
10777c478bd9Sstevel@tonic-gate 	 */
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	if (pipe(pfds) == -1) {
10807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Couldn't create pipe: %s\n",
10817c478bd9Sstevel@tonic-gate 		    strerror(errno));
10827c478bd9Sstevel@tonic-gate 		return (-1);
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) < 0) {
10867c478bd9Sstevel@tonic-gate 		(void) close(pfds[0]);
10877c478bd9Sstevel@tonic-gate 		(void) close(pfds[1]);
10887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Couldn't fork child process: %s\n",
10897c478bd9Sstevel@tonic-gate 		    strerror(errno));
10907c478bd9Sstevel@tonic-gate 		return (-1);
10917c478bd9Sstevel@tonic-gate 	} else if (pid == 0) {
10927c478bd9Sstevel@tonic-gate 		/*
10937c478bd9Sstevel@tonic-gate 		 * Child.
10947c478bd9Sstevel@tonic-gate 		 */
10957c478bd9Sstevel@tonic-gate 		(void) close(pfds[0]);
10967c478bd9Sstevel@tonic-gate 		/*
10977c478bd9Sstevel@tonic-gate 		 * Send stderr messages down the pipe so that we can detect
10987c478bd9Sstevel@tonic-gate 		 * them in the parent process.
10997c478bd9Sstevel@tonic-gate 		 */
11007c478bd9Sstevel@tonic-gate 		if (pfds[1] != STDERR_FILENO) {
11017c478bd9Sstevel@tonic-gate 			(void) dup2(pfds[1], STDERR_FILENO);
11027c478bd9Sstevel@tonic-gate 			(void) close(pfds[1]);
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 		/*
11057c478bd9Sstevel@tonic-gate 		 * This shell does not inherit the additional privilege
11067c478bd9Sstevel@tonic-gate 		 * we have in our Permitted set.
11077c478bd9Sstevel@tonic-gate 		 */
110840164e4fSsn 		(void) execv(path, arglist);
11097c478bd9Sstevel@tonic-gate 		_exit(127);
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * Parent.
11137c478bd9Sstevel@tonic-gate 	 */
11147c478bd9Sstevel@tonic-gate 	istat = signal(SIGINT, SIG_IGN);
11157c478bd9Sstevel@tonic-gate 	qstat = signal(SIGQUIT, SIG_IGN);
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	(void) close(pfds[1]);
11187c478bd9Sstevel@tonic-gate 	seen_stderr_traffic = B_FALSE;
11197c478bd9Sstevel@tonic-gate 	while ((cnt = read(pfds[0], buf, sizeof (buf))) > 0) {
11207c478bd9Sstevel@tonic-gate 		/*
11217c478bd9Sstevel@tonic-gate 		 * If any data is read from the pipe the child process
11227c478bd9Sstevel@tonic-gate 		 * has output something on stderr so we set the boolean
11237c478bd9Sstevel@tonic-gate 		 * 'seen_stderr_traffic' to true, which will cause the
11247c478bd9Sstevel@tonic-gate 		 * function to return -1.
11257c478bd9Sstevel@tonic-gate 		 */
11267c478bd9Sstevel@tonic-gate 		(void) write(STDERR_FILENO, buf, cnt);
11277c478bd9Sstevel@tonic-gate 		seen_stderr_traffic = B_TRUE;
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	(void) close(pfds[0]);
11307c478bd9Sstevel@tonic-gate 	while ((w = wait(&status)) != pid && w != -1)
11317c478bd9Sstevel@tonic-gate 		;
11327c478bd9Sstevel@tonic-gate 	if (w == -1)
11337c478bd9Sstevel@tonic-gate 		status = -1;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, istat);
11367c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, qstat);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	return (seen_stderr_traffic ? -1 : status);
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate static void
source(int argc,char * argv[])11427c478bd9Sstevel@tonic-gate source(int argc, char *argv[])
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	struct stat stb;
11457c478bd9Sstevel@tonic-gate 	static BUF buffer;
11467c478bd9Sstevel@tonic-gate 	BUF *bp;
11477c478bd9Sstevel@tonic-gate 	int x, readerr, f, amt;
11487c478bd9Sstevel@tonic-gate 	char *last, *name, buf[RCP_BUFSIZE];
11497c478bd9Sstevel@tonic-gate 	off_t off, size, i;
11507c478bd9Sstevel@tonic-gate 	ssize_t cnt;
11516d2b079bSjs 	struct linger lingerbuf;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	for (x = 0; x < argc; x++) {
11547c478bd9Sstevel@tonic-gate 		name = argv[x];
11557c478bd9Sstevel@tonic-gate 		if ((f = open(name, O_RDONLY, 0)) < 0) {
11567c478bd9Sstevel@tonic-gate 			error("rcp: %s: %s\n", name, strerror(errno));
11577c478bd9Sstevel@tonic-gate 			continue;
11587c478bd9Sstevel@tonic-gate 		}
11597c478bd9Sstevel@tonic-gate 		if (fstat(f, &stb) < 0)
11607c478bd9Sstevel@tonic-gate 			goto notreg;
11617c478bd9Sstevel@tonic-gate 		switch (stb.st_mode&S_IFMT) {
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		case S_IFREG:
11647c478bd9Sstevel@tonic-gate 			break;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		case S_IFDIR:
11677c478bd9Sstevel@tonic-gate 			if (iamrecursive) {
11687c478bd9Sstevel@tonic-gate 				(void) close(f);
11697c478bd9Sstevel@tonic-gate 				rsource(name, &stb);
11707c478bd9Sstevel@tonic-gate 				continue;
11717c478bd9Sstevel@tonic-gate 			}
11727c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
11737c478bd9Sstevel@tonic-gate 		default:
11747c478bd9Sstevel@tonic-gate notreg:
11757c478bd9Sstevel@tonic-gate 			(void) close(f);
11767c478bd9Sstevel@tonic-gate 			error("rcp: %s: not a plain file\n", name);
1177