10Sstevel@tonic-gate /* 23370Sjs198686 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 70Sstevel@tonic-gate 80Sstevel@tonic-gate /* 90Sstevel@tonic-gate * Copyright (c) 1983 The Regents of the University of California. 100Sstevel@tonic-gate * All rights reserved. 110Sstevel@tonic-gate * 120Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted 130Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are 140Sstevel@tonic-gate * duplicated in all such forms and that any documentation, 150Sstevel@tonic-gate * advertising materials, and other materials related to such 160Sstevel@tonic-gate * distribution and use acknowledge that the software was developed 170Sstevel@tonic-gate * by the University of California, Berkeley. The name of the 180Sstevel@tonic-gate * University may not be used to endorse or promote products derived 190Sstevel@tonic-gate * from this software without specific prior written permission. 200Sstevel@tonic-gate * 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate 230Sstevel@tonic-gate #define _FILE_OFFSET_BITS 64 240Sstevel@tonic-gate 250Sstevel@tonic-gate /* 260Sstevel@tonic-gate * rcp 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate #include <sys/param.h> 290Sstevel@tonic-gate #include <sys/file.h> 300Sstevel@tonic-gate #include <sys/stat.h> 310Sstevel@tonic-gate #include <sys/time.h> 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/ioctl.h> 340Sstevel@tonic-gate #include <sys/acl.h> 350Sstevel@tonic-gate #include <dirent.h> 360Sstevel@tonic-gate #include <signal.h> 370Sstevel@tonic-gate #include <sys/socket.h> 380Sstevel@tonic-gate #include <netinet/in.h> 390Sstevel@tonic-gate #include <pwd.h> 400Sstevel@tonic-gate #include <netdb.h> 410Sstevel@tonic-gate #include <wchar.h> 420Sstevel@tonic-gate #include <stdlib.h> 430Sstevel@tonic-gate #include <errno.h> 440Sstevel@tonic-gate #include <locale.h> 450Sstevel@tonic-gate #include <strings.h> 460Sstevel@tonic-gate #include <stdio.h> 470Sstevel@tonic-gate #include <ctype.h> 480Sstevel@tonic-gate #include <fcntl.h> 490Sstevel@tonic-gate #include <unistd.h> 500Sstevel@tonic-gate #include <limits.h> 510Sstevel@tonic-gate #include <priv_utils.h> 520Sstevel@tonic-gate #include <sys/sendfile.h> 530Sstevel@tonic-gate #include <sys/sysmacros.h> 540Sstevel@tonic-gate #include <sys/wait.h> 55789Sahrens #include <aclutils.h> 56*4619Ssn199410 #include <sys/varargs.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * It seems like Berkeley got these from pathnames.h? 600Sstevel@tonic-gate */ 610Sstevel@tonic-gate #define _PATH_RSH "/usr/bin/rsh" 620Sstevel@tonic-gate #define _PATH_CP "/usr/bin/cp" 630Sstevel@tonic-gate 640Sstevel@tonic-gate #define ACL_FAIL 1 650Sstevel@tonic-gate #define ACL_OK 0 660Sstevel@tonic-gate #define RCP_BUFSIZE (64 * 1024) 670Sstevel@tonic-gate 680Sstevel@tonic-gate #define RCP_ACL "/usr/lib/sunw,rcp" 690Sstevel@tonic-gate /* see PSARC/1993/004/opinion */ 700Sstevel@tonic-gate 710Sstevel@tonic-gate typedef struct _buf { 720Sstevel@tonic-gate int cnt; 730Sstevel@tonic-gate char *buf; 740Sstevel@tonic-gate } BUF; 750Sstevel@tonic-gate 760Sstevel@tonic-gate static char *cmd_sunw; 770Sstevel@tonic-gate static struct passwd *pwd; 780Sstevel@tonic-gate static int errs; 790Sstevel@tonic-gate static int pflag; 800Sstevel@tonic-gate static uid_t userid; 810Sstevel@tonic-gate static int rem; 820Sstevel@tonic-gate static int zflag; 830Sstevel@tonic-gate static int iamremote; 840Sstevel@tonic-gate static int iamrecursive; 850Sstevel@tonic-gate static int targetshouldbedirectory; 860Sstevel@tonic-gate static int aclflag; 87789Sahrens static int acl_aclflag; 880Sstevel@tonic-gate static int retval = 0; 890Sstevel@tonic-gate static int portnumber = 0; 900Sstevel@tonic-gate 910Sstevel@tonic-gate static void lostconn(void); 920Sstevel@tonic-gate static char *search_char(unsigned char *, unsigned char); 930Sstevel@tonic-gate static char *removebrackets(char *); 940Sstevel@tonic-gate static char *colon(char *); 950Sstevel@tonic-gate static int response(void); 960Sstevel@tonic-gate static void usage(void); 970Sstevel@tonic-gate static void source(int, char **); 980Sstevel@tonic-gate static void sink(int, char **); 990Sstevel@tonic-gate static void toremote(char *, int, char **); 1000Sstevel@tonic-gate static void tolocal(int, char **); 1010Sstevel@tonic-gate static void verifydir(char *); 1020Sstevel@tonic-gate static int okname(char *); 103*4619Ssn199410 static int susystem(char *, char **); 1040Sstevel@tonic-gate static void rsource(char *, struct stat *); 1050Sstevel@tonic-gate static int sendacl(int); 1060Sstevel@tonic-gate static int recvacl(int, int, int); 1070Sstevel@tonic-gate static int zwrite(int, char *, int); 1080Sstevel@tonic-gate static void zopen(int, int); 1090Sstevel@tonic-gate static int zclose(int); 1100Sstevel@tonic-gate static int notzero(char *, int); 1110Sstevel@tonic-gate static BUF *allocbuf(BUF *, int, int); 1120Sstevel@tonic-gate static void error(char *fmt, ...); 113*4619Ssn199410 static void addargs(char **, ...); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate /* 1160Sstevel@tonic-gate * As a 32 bit application, we can only transfer (2gb - 1) i.e 0x7FFFFFFF 1170Sstevel@tonic-gate * bytes of data. We would like the size to be aligned to the nearest 1180Sstevel@tonic-gate * MAXBOFFSET (8192) boundary for optimal performance. 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate #define SENDFILE_SIZE 0x7FFFE000 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate #include <k5-int.h> 1230Sstevel@tonic-gate #include <profile/prof_int.h> 1240Sstevel@tonic-gate #include <com_err.h> 1250Sstevel@tonic-gate #include <kcmd.h> 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #define NULLBUF (BUF *) 0 128*4619Ssn199410 #define MAXARGS 10 /* Number of arguments passed to execv() */ 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate static int sock; 1310Sstevel@tonic-gate static char *cmd, *cmd_orig, *cmd_sunw_orig; 1320Sstevel@tonic-gate static char *krb_realm = NULL; 1330Sstevel@tonic-gate static char *krb_cache = NULL; 1340Sstevel@tonic-gate static char *krb_config = NULL; 1350Sstevel@tonic-gate static char des_inbuf[2 * RCP_BUFSIZE]; 1360Sstevel@tonic-gate /* needs to be > largest read size */ 1370Sstevel@tonic-gate static char des_outbuf[2 * RCP_BUFSIZE]; 1380Sstevel@tonic-gate /* needs to be > largest write size */ 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf; 1410Sstevel@tonic-gate static krb5_encrypt_block eblock; /* eblock for encrypt/decrypt */ 1420Sstevel@tonic-gate static krb5_keyblock *session_key; /* static key for session */ 1430Sstevel@tonic-gate static krb5_context bsd_context; 1440Sstevel@tonic-gate static krb5_auth_context auth_context; 1450Sstevel@tonic-gate static krb5_flags authopts; 1460Sstevel@tonic-gate static krb5_error_code status; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate static void try_normal_rcp(int, char **); 1490Sstevel@tonic-gate static int init_service(int); 1500Sstevel@tonic-gate static char **save_argv(int, char **); 1510Sstevel@tonic-gate static void answer_auth(char *, char *); 1520Sstevel@tonic-gate static int desrcpwrite(int, char *, int); 1530Sstevel@tonic-gate static int desrcpread(int, char *, int); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * Not sure why these two don't have their own header file declarations, but 1570Sstevel@tonic-gate * lint complains about absent declarations so place some here. Sigh. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate extern errcode_t profile_get_options_boolean(profile_t, char **, 1600Sstevel@tonic-gate profile_options_boolean *); 1610Sstevel@tonic-gate extern errcode_t profile_get_options_string(profile_t, char **, 1620Sstevel@tonic-gate profile_option_strings *); 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static int krb5auth_flag = 0; /* Flag set, when KERBEROS is enabled */ 1650Sstevel@tonic-gate static int encrypt_flag = 0; /* Flag set, when encryption is enabled */ 1660Sstevel@tonic-gate static int encrypt_done = 0; /* Flag set, if "-x" is specified */ 1670Sstevel@tonic-gate static enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */ 1700Sstevel@tonic-gate static boolean_t rcmdoption_done = B_FALSE; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate static profile_options_boolean option[] = { 1730Sstevel@tonic-gate { "encrypt", &encrypt_flag, 0 }, 1740Sstevel@tonic-gate { NULL, NULL, 0 } 1750Sstevel@tonic-gate }; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate static char *rcmdproto = NULL; 1780Sstevel@tonic-gate static profile_option_strings rcmdversion[] = { 1790Sstevel@tonic-gate { "rcmd_protocol", &rcmdproto, 0 }, 1800Sstevel@tonic-gate { NULL, NULL, 0 } 1810Sstevel@tonic-gate }; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, "rcp", NULL }; 1840Sstevel@tonic-gate static char *appdef[] = { "appdefaults", "rcp", NULL }; 1850Sstevel@tonic-gate static char **prev_argv; 1860Sstevel@tonic-gate static int prev_argc; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate int 1890Sstevel@tonic-gate main(int argc, char *argv[]) 1900Sstevel@tonic-gate { 1910Sstevel@tonic-gate int ch, fflag, tflag; 1920Sstevel@tonic-gate char *targ; 1930Sstevel@tonic-gate size_t cmdsiz; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate if (strcmp(argv[0], RCP_ACL) == 0) 1980Sstevel@tonic-gate aclflag = 1; 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate if (!(pwd = getpwuid(userid = getuid()))) { 2010Sstevel@tonic-gate (void) fprintf(stderr, "rcp: unknown user %d.\n", 2020Sstevel@tonic-gate (uint_t)userid); 2030Sstevel@tonic-gate return (1); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate fflag = tflag = 0; 207789Sahrens while ((ch = getopt(argc, argv, "axdfprtz:D:k:P:Z")) != EOF) { 2080Sstevel@tonic-gate switch (ch) { 2090Sstevel@tonic-gate case 'd': 2100Sstevel@tonic-gate targetshouldbedirectory = 1; 2110Sstevel@tonic-gate break; 2120Sstevel@tonic-gate case 'f': /* "from" */ 2130Sstevel@tonic-gate fflag = 1; 214789Sahrens if (aclflag | acl_aclflag) 2150Sstevel@tonic-gate /* ok response */ 2160Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 2170Sstevel@tonic-gate break; 2180Sstevel@tonic-gate case 'p': /* preserve access/mod times */ 2190Sstevel@tonic-gate ++pflag; 2200Sstevel@tonic-gate break; 2210Sstevel@tonic-gate case 'r': 2220Sstevel@tonic-gate ++iamrecursive; 2230Sstevel@tonic-gate break; 2240Sstevel@tonic-gate case 't': /* "to" */ 2250Sstevel@tonic-gate tflag = 1; 2260Sstevel@tonic-gate break; 227789Sahrens case 'Z': 228789Sahrens acl_aclflag++; 229789Sahrens break; 2300Sstevel@tonic-gate case 'x': 2310Sstevel@tonic-gate if (!krb5_privacy_allowed()) { 2320Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: " 2330Sstevel@tonic-gate "Encryption not supported.\n")); 2340Sstevel@tonic-gate return (1); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate encrypt_flag++; 2370Sstevel@tonic-gate krb5auth_flag++; 2380Sstevel@tonic-gate encrypt_done++; 2390Sstevel@tonic-gate break; 2400Sstevel@tonic-gate case 'k': 2410Sstevel@tonic-gate if ((krb_realm = (char *)strdup(optarg)) == NULL) { 2420Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp:" 2430Sstevel@tonic-gate " Cannot malloc.\n")); 2440Sstevel@tonic-gate return (1); 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate krb5auth_flag++; 2470Sstevel@tonic-gate break; 2480Sstevel@tonic-gate case 'P': 2490Sstevel@tonic-gate if (strncmp(optarg, "O", 1) == 0) { 2500Sstevel@tonic-gate if (rcmdoption_done == B_TRUE) { 2510Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: " 2520Sstevel@tonic-gate "Only one of -PN and -PO " 2530Sstevel@tonic-gate "allowed.\n")); 2540Sstevel@tonic-gate usage(); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate kcmd_proto = KCMD_OLD_PROTOCOL; 2570Sstevel@tonic-gate rcmdoption_done = B_TRUE; 2580Sstevel@tonic-gate } else if (strncmp(optarg, "N", 1) == 0) { 2590Sstevel@tonic-gate if (rcmdoption_done == B_TRUE) { 2600Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: " 2610Sstevel@tonic-gate "Only one of -PN and -PO " 2620Sstevel@tonic-gate "allowed.\n")); 2630Sstevel@tonic-gate usage(); 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate kcmd_proto = KCMD_NEW_PROTOCOL; 2660Sstevel@tonic-gate rcmdoption_done = B_TRUE; 2670Sstevel@tonic-gate } else { 2680Sstevel@tonic-gate usage(); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate krb5auth_flag++; 2710Sstevel@tonic-gate break; 2720Sstevel@tonic-gate case 'a': 2730Sstevel@tonic-gate krb5auth_flag++; 2740Sstevel@tonic-gate break; 2750Sstevel@tonic-gate #ifdef DEBUG 2760Sstevel@tonic-gate case 'D': 2770Sstevel@tonic-gate portnumber = htons(atoi(optarg)); 2780Sstevel@tonic-gate krb5auth_flag++; 2790Sstevel@tonic-gate break; 2800Sstevel@tonic-gate #endif /* DEBUG */ 2810Sstevel@tonic-gate case '?': 2820Sstevel@tonic-gate default: 2830Sstevel@tonic-gate usage(); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate argc -= optind; 2870Sstevel@tonic-gate argv += optind; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate if (krb5auth_flag > 0) { 2900Sstevel@tonic-gate status = krb5_init_context(&bsd_context); 2910Sstevel@tonic-gate if (status) { 2920Sstevel@tonic-gate com_err("rcp", status, 2930Sstevel@tonic-gate gettext("while initializing krb5")); 2940Sstevel@tonic-gate return (1); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* 2980Sstevel@tonic-gate * Set up buffers for desread and deswrite. 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate desinbuf.data = des_inbuf; 3010Sstevel@tonic-gate desoutbuf.data = des_outbuf; 3020Sstevel@tonic-gate desinbuf.length = sizeof (des_inbuf); 3030Sstevel@tonic-gate desoutbuf.length = sizeof (des_outbuf); 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate if (fflag || tflag) 3070Sstevel@tonic-gate if (encrypt_flag > 0) 3080Sstevel@tonic-gate (void) answer_auth(krb_config, krb_cache); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (fflag) { 3110Sstevel@tonic-gate iamremote = 1; 3120Sstevel@tonic-gate (void) response(); 3130Sstevel@tonic-gate (void) setuid(userid); 3140Sstevel@tonic-gate source(argc, argv); 3150Sstevel@tonic-gate return (errs); 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate if (tflag) { 3190Sstevel@tonic-gate iamremote = 1; 3200Sstevel@tonic-gate (void) setuid(userid); 3210Sstevel@tonic-gate sink(argc, argv); 3220Sstevel@tonic-gate return (errs); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if (argc < 2) 3260Sstevel@tonic-gate usage(); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* This will make "rcmd_af()" magically get the proper privilege */ 3290Sstevel@tonic-gate if (__init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL) == -1) { 3300Sstevel@tonic-gate (void) fprintf(stderr, "rcp: must be set-uid root\n"); 3310Sstevel@tonic-gate exit(1); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate if (krb5auth_flag > 0) { 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * Get our local realm to look up local realm options. 3370Sstevel@tonic-gate */ 3380Sstevel@tonic-gate status = krb5_get_default_realm(bsd_context, &realmdef[1]); 3390Sstevel@tonic-gate if (status) { 3400Sstevel@tonic-gate com_err("rcp", status, 3410Sstevel@tonic-gate gettext("while getting default realm")); 3420Sstevel@tonic-gate return (1); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * See if encryption should be done for this realm 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate profile_get_options_boolean(bsd_context->profile, realmdef, 3480Sstevel@tonic-gate option); 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * Check the appdefaults section 3510Sstevel@tonic-gate */ 3520Sstevel@tonic-gate profile_get_options_boolean(bsd_context->profile, appdef, 3530Sstevel@tonic-gate option); 3540Sstevel@tonic-gate profile_get_options_string(bsd_context->profile, appdef, 3550Sstevel@tonic-gate rcmdversion); 3560Sstevel@tonic-gate if ((encrypt_done > 0) || (encrypt_flag > 0)) { 3570Sstevel@tonic-gate if (krb5_privacy_allowed() == TRUE) { 3580Sstevel@tonic-gate encrypt_flag++; 3590Sstevel@tonic-gate } else { 3600Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Encryption" 3610Sstevel@tonic-gate " not supported.\n")); 3620Sstevel@tonic-gate return (1); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if ((rcmdoption_done == B_FALSE) && (rcmdproto != NULL)) { 3670Sstevel@tonic-gate if (strncmp(rcmdproto, "rcmdv2", 6) == 0) { 3680Sstevel@tonic-gate kcmd_proto = KCMD_NEW_PROTOCOL; 3690Sstevel@tonic-gate } else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) { 3700Sstevel@tonic-gate kcmd_proto = KCMD_OLD_PROTOCOL; 3710Sstevel@tonic-gate } else { 3720Sstevel@tonic-gate (void) fprintf(stderr, gettext("Unrecognized " 3730Sstevel@tonic-gate "KCMD protocol (%s)"), rcmdproto); 3740Sstevel@tonic-gate return (1); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate } 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate if (argc > 2) 3800Sstevel@tonic-gate targetshouldbedirectory = 1; 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate rem = -1; 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate if (portnumber == 0) { 3850Sstevel@tonic-gate if (krb5auth_flag > 0) { 3860Sstevel@tonic-gate retval = init_service(krb5auth_flag); 3870Sstevel@tonic-gate if (!retval) { 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * Connecting to the kshell service failed, 3900Sstevel@tonic-gate * fallback to normal rcp & reset KRB5 flags. 3910Sstevel@tonic-gate */ 3920Sstevel@tonic-gate krb5auth_flag = encrypt_flag = 0; 3930Sstevel@tonic-gate encrypt_done = 0; 3940Sstevel@tonic-gate (void) init_service(krb5auth_flag); 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate else 3980Sstevel@tonic-gate (void) init_service(krb5auth_flag); 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate #ifdef DEBUG 4020Sstevel@tonic-gate if (retval || krb5auth_flag) { 4030Sstevel@tonic-gate (void) fprintf(stderr, gettext("Kerberized rcp session, " 4040Sstevel@tonic-gate "port %d in use "), portnumber); 4050Sstevel@tonic-gate if (kcmd_proto == KCMD_OLD_PROTOCOL) 4060Sstevel@tonic-gate (void) fprintf(stderr, gettext("[kcmd ver.1]\n")); 4070Sstevel@tonic-gate else 4080Sstevel@tonic-gate (void) fprintf(stderr, gettext("[kcmd ver.2]\n")); 4090Sstevel@tonic-gate } else { 4100Sstevel@tonic-gate (void) fprintf(stderr, gettext("Normal rcp session, port %d " 4110Sstevel@tonic-gate "in use.\n"), portnumber); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate #endif /* DEBUG */ 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate if (krb5auth_flag > 0) { 4160Sstevel@tonic-gate /* 4170Sstevel@tonic-gate * We calculate here a buffer size that can be used in the 4180Sstevel@tonic-gate * allocation of the three buffers cmd, cmd_orig and 4190Sstevel@tonic-gate * cmd_sunw_orig that are used to hold different incantations 4200Sstevel@tonic-gate * of rcp. 4210Sstevel@tonic-gate */ 422*4619Ssn199410 cmdsiz = MAX(sizeof ("-x rcp -r -p -d -k ") + 4230Sstevel@tonic-gate strlen(krb_realm != NULL ? krb_realm : ""), 4240Sstevel@tonic-gate sizeof (RCP_ACL " -r -p -z -d")); 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate if (((cmd = (char *)malloc(cmdsiz)) == NULL) || 4270Sstevel@tonic-gate ((cmd_sunw_orig = (char *)malloc(cmdsiz)) == NULL) || 4280Sstevel@tonic-gate ((cmd_orig = (char *)malloc(cmdsiz)) == NULL)) { 4290Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot " 4300Sstevel@tonic-gate "malloc.\n")); 4310Sstevel@tonic-gate return (1); 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate (void) snprintf(cmd, cmdsiz, "%srcp %s%s%s%s%s", 4350Sstevel@tonic-gate encrypt_flag ? "-x " : "", 4360Sstevel@tonic-gate iamrecursive ? " -r" : "", pflag ? " -p" : "", 4370Sstevel@tonic-gate targetshouldbedirectory ? " -d" : "", 4380Sstevel@tonic-gate krb_realm != NULL ? " -k " : "", 4390Sstevel@tonic-gate krb_realm != NULL ? krb_realm : ""); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * We would use cmd-orig as the 'cmd-buffer' if kerberized 4430Sstevel@tonic-gate * rcp fails, in which case we fallback to normal rcp. We also 4440Sstevel@tonic-gate * save argc & argv for the same purpose 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate (void) snprintf(cmd_orig, cmdsiz, "rcp%s%s%s%s", 4470Sstevel@tonic-gate iamrecursive ? " -r" : "", 4480Sstevel@tonic-gate pflag ? " -p" : "", 4490Sstevel@tonic-gate zflag ? " -z" : "", 4500Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate (void) snprintf(cmd_sunw_orig, cmdsiz, "%s%s%s%s%s", RCP_ACL, 4530Sstevel@tonic-gate iamrecursive ? " -r" : "", 4540Sstevel@tonic-gate pflag ? " -p" : "", 4550Sstevel@tonic-gate zflag ? " -z" : "", 4560Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate prev_argc = argc; 4590Sstevel@tonic-gate prev_argv = save_argv(argc, argv); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate } else { 4620Sstevel@tonic-gate cmdsiz = sizeof ("rcp -r -p -z -d"); 4630Sstevel@tonic-gate if (((cmd = (char *)malloc(cmdsiz)) == NULL)) { 4640Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot " 4650Sstevel@tonic-gate "malloc.\n")); 4660Sstevel@tonic-gate return (1); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate (void) snprintf(cmd, cmdsiz, "rcp%s%s%s%s", 4700Sstevel@tonic-gate iamrecursive ? " -r" : "", 4710Sstevel@tonic-gate pflag ? " -p" : "", 4720Sstevel@tonic-gate zflag ? " -z" : "", 4730Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate cmdsiz = sizeof (RCP_ACL " -r -p -z -d"); 4770Sstevel@tonic-gate if ((cmd_sunw = (char *)malloc(cmdsiz)) == NULL) { 4780Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot malloc.\n")); 4790Sstevel@tonic-gate return (1); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate (void) snprintf(cmd_sunw, cmdsiz, "%s%s%s%s%s", RCP_ACL, 4830Sstevel@tonic-gate iamrecursive ? " -r" : "", 4840Sstevel@tonic-gate pflag ? " -p" : "", 4850Sstevel@tonic-gate zflag ? " -z" : "", 4860Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate (void) signal(SIGPIPE, (void (*)(int))lostconn); 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate if (targ = colon(argv[argc - 1])) 4910Sstevel@tonic-gate toremote(targ, argc, argv); 4920Sstevel@tonic-gate else { 4930Sstevel@tonic-gate tolocal(argc, argv); 4940Sstevel@tonic-gate if (targetshouldbedirectory) 4950Sstevel@tonic-gate verifydir(argv[argc - 1]); 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate return (errs > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate static void 5030Sstevel@tonic-gate toremote(char *targ, int argc, char *argv[]) 5040Sstevel@tonic-gate { 5050Sstevel@tonic-gate int i; 5060Sstevel@tonic-gate char *host, *src, *suser, *thost, *tuser; 5070Sstevel@tonic-gate char resp; 5080Sstevel@tonic-gate size_t buffersize; 5090Sstevel@tonic-gate char bp[RCP_BUFSIZE]; 5100Sstevel@tonic-gate krb5_creds *cred; 511*4619Ssn199410 char *arglist[MAXARGS+1]; 5120Sstevel@tonic-gate buffersize = RCP_BUFSIZE; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate *targ++ = 0; 5150Sstevel@tonic-gate if (*targ == 0) 5160Sstevel@tonic-gate targ = "."; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate if (thost = search_char((unsigned char *)argv[argc - 1], '@')) { 5190Sstevel@tonic-gate *thost++ = 0; 5200Sstevel@tonic-gate tuser = argv[argc - 1]; 5210Sstevel@tonic-gate if (*tuser == '\0') 5220Sstevel@tonic-gate tuser = NULL; 5230Sstevel@tonic-gate else if (!okname(tuser)) 5240Sstevel@tonic-gate exit(1); 5250Sstevel@tonic-gate } else { 5260Sstevel@tonic-gate thost = argv[argc - 1]; 5270Sstevel@tonic-gate tuser = NULL; 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate thost = removebrackets(thost); 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) { 5320Sstevel@tonic-gate src = colon(argv[i]); 5330Sstevel@tonic-gate if (src) { /* remote to remote */ 5340Sstevel@tonic-gate *src++ = 0; 5350Sstevel@tonic-gate if (*src == 0) 5360Sstevel@tonic-gate src = "."; 5370Sstevel@tonic-gate host = search_char((unsigned char *)argv[i], '@'); 5380Sstevel@tonic-gate if (host) { 5390Sstevel@tonic-gate *host++ = 0; 5400Sstevel@tonic-gate host = removebrackets(host); 5410Sstevel@tonic-gate suser = argv[i]; 5420Sstevel@tonic-gate if (*suser == '\0') { 5430Sstevel@tonic-gate suser = pwd->pw_name; 5440Sstevel@tonic-gate } else if (!okname(suser)) { 5450Sstevel@tonic-gate errs++; 5460Sstevel@tonic-gate continue; 5470Sstevel@tonic-gate } 548*4619Ssn199410 (void) snprintf(bp, buffersize, "'%s%s%s:%s'", 5490Sstevel@tonic-gate tuser ? tuser : "", tuser ? "@" : "", 5500Sstevel@tonic-gate thost, targ); 551*4619Ssn199410 (void) addargs(arglist, "rsh", host, "-l", 552*4619Ssn199410 suser, "-n", cmd, src, bp, (char *)NULL); 5530Sstevel@tonic-gate } else { 5540Sstevel@tonic-gate host = removebrackets(argv[i]); 555*4619Ssn199410 (void) snprintf(bp, buffersize, "'%s%s%s:%s'", 556*4619Ssn199410 tuser ? tuser : "", tuser ? "@" : "", 557*4619Ssn199410 thost, targ); 558*4619Ssn199410 (void) addargs(arglist, "rsh", host, "-n", cmd, 559*4619Ssn199410 src, bp, (char *)NULL); 5600Sstevel@tonic-gate } 561*4619Ssn199410 if (susystem(_PATH_RSH, arglist) == -1) 5620Sstevel@tonic-gate errs++; 5630Sstevel@tonic-gate } else { /* local to remote */ 5640Sstevel@tonic-gate if (rem == -1) { 5650Sstevel@tonic-gate host = thost; 5660Sstevel@tonic-gate if (krb5auth_flag > 0) { 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate (void) snprintf(bp, buffersize, 5690Sstevel@tonic-gate "%s -t %s", cmd, targ); 5700Sstevel@tonic-gate authopts = AP_OPTS_MUTUAL_REQUIRED; 5710Sstevel@tonic-gate status = kcmd(&sock, &host, 5720Sstevel@tonic-gate portnumber, 5730Sstevel@tonic-gate pwd->pw_name, 5740Sstevel@tonic-gate tuser ? tuser : 5750Sstevel@tonic-gate pwd->pw_name, 5760Sstevel@tonic-gate bp, 5770Sstevel@tonic-gate 0, 5780Sstevel@tonic-gate "host", 5790Sstevel@tonic-gate krb_realm, 5800Sstevel@tonic-gate bsd_context, 5810Sstevel@tonic-gate &auth_context, 5820Sstevel@tonic-gate &cred, 5830Sstevel@tonic-gate 0, /* No seq # */ 5840Sstevel@tonic-gate 0, /* No server seq # */ 5850Sstevel@tonic-gate authopts, 5860Sstevel@tonic-gate 0, /* Not any port # */ 5870Sstevel@tonic-gate &kcmd_proto); 5880Sstevel@tonic-gate if (status) { 5890Sstevel@tonic-gate /* 5900Sstevel@tonic-gate * If new protocol requested, we dont 5910Sstevel@tonic-gate * fallback to less secure ones. 5920Sstevel@tonic-gate */ 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 5950Sstevel@tonic-gate (void) fprintf(stderr, 5960Sstevel@tonic-gate gettext("rcp: kcmdv2 " 5970Sstevel@tonic-gate "to host %s failed - %s" 5980Sstevel@tonic-gate "\nFallback to normal " 5990Sstevel@tonic-gate "rcp denied."), host, 6000Sstevel@tonic-gate error_message(status)); 6010Sstevel@tonic-gate exit(1); 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate if (status != -1) { 6040Sstevel@tonic-gate (void) fprintf(stderr, 6050Sstevel@tonic-gate gettext("rcp: kcmd to host " 6060Sstevel@tonic-gate "%s failed - %s,\n" 6070Sstevel@tonic-gate "trying normal rcp...\n\n"), 6080Sstevel@tonic-gate host, error_message(status)); 6090Sstevel@tonic-gate } else { 6100Sstevel@tonic-gate (void) fprintf(stderr, 6110Sstevel@tonic-gate gettext("trying normal" 6120Sstevel@tonic-gate " rcp...\n")); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate /* 6150Sstevel@tonic-gate * kcmd() failed, so we have to 6160Sstevel@tonic-gate * fallback to normal rcp 6170Sstevel@tonic-gate */ 6180Sstevel@tonic-gate try_normal_rcp(prev_argc, prev_argv); 6190Sstevel@tonic-gate } else { 6200Sstevel@tonic-gate rem = sock; 6210Sstevel@tonic-gate session_key = &cred->keyblock; 6220Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 6230Sstevel@tonic-gate /* CSTYLED */ 6240Sstevel@tonic-gate status = krb5_auth_con_getlocalsubkey(bsd_context, auth_context, &session_key); 6250Sstevel@tonic-gate if (status) { 6260Sstevel@tonic-gate com_err("rcp", status, 6270Sstevel@tonic-gate "determining " 6280Sstevel@tonic-gate "subkey for " 6290Sstevel@tonic-gate "session"); 6300Sstevel@tonic-gate exit(1); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate if (!session_key) { 6330Sstevel@tonic-gate com_err("rcp", 0, 6340Sstevel@tonic-gate "no subkey " 6350Sstevel@tonic-gate "negotiated for" 6360Sstevel@tonic-gate " connection"); 6370Sstevel@tonic-gate exit(1); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate eblock.crypto_entry = 6410Sstevel@tonic-gate session_key->enctype; 6420Sstevel@tonic-gate eblock.key = 6430Sstevel@tonic-gate (krb5_keyblock *)session_key; 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate init_encrypt(encrypt_flag, 6460Sstevel@tonic-gate bsd_context, kcmd_proto, 6470Sstevel@tonic-gate &desinbuf, &desoutbuf, CLIENT, 6480Sstevel@tonic-gate &eblock); 6490Sstevel@tonic-gate if (encrypt_flag > 0) { 6500Sstevel@tonic-gate char *s = gettext("This rcp " 6510Sstevel@tonic-gate "session is using " 6520Sstevel@tonic-gate "encryption for all " 6530Sstevel@tonic-gate "data transmissions." 6540Sstevel@tonic-gate "\r\n"); 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate (void) write(2, s, strlen(s)); 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate if (response() < 0) 6600Sstevel@tonic-gate exit(1); 6610Sstevel@tonic-gate 662789Sahrens } else { 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate /* 6650Sstevel@tonic-gate * ACL support: try to find out if the remote 6660Sstevel@tonic-gate * site is running acl cognizant version of 6670Sstevel@tonic-gate * rcp. A special binary name is used for this 6680Sstevel@tonic-gate * purpose. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate aclflag = 1; 671789Sahrens acl_aclflag = 1; 6720Sstevel@tonic-gate 673789Sahrens /* 674789Sahrens * First see if the remote side will support 675789Sahrens * both aclent_t and ace_t acl's? 676789Sahrens */ 677789Sahrens (void) snprintf(bp, buffersize, "%s -tZ %s", 6780Sstevel@tonic-gate cmd_sunw, targ); 6790Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, pwd->pw_name, 6800Sstevel@tonic-gate tuser ? tuser : pwd->pw_name, 6810Sstevel@tonic-gate bp, 0, AF_INET6); 6820Sstevel@tonic-gate if (rem < 0) 6830Sstevel@tonic-gate exit(1); 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate /* 6860Sstevel@tonic-gate * This is similar to routine response(). 6870Sstevel@tonic-gate * If response is not ok, treat the other 6880Sstevel@tonic-gate * side as non-acl rcp. 6890Sstevel@tonic-gate */ 6900Sstevel@tonic-gate if (read(rem, &resp, sizeof (resp)) 6910Sstevel@tonic-gate != sizeof (resp)) 6920Sstevel@tonic-gate lostconn(); 6930Sstevel@tonic-gate if (resp != 0) { 694789Sahrens acl_aclflag = 0; 6950Sstevel@tonic-gate (void) snprintf(bp, buffersize, 696789Sahrens "%s -t %s", cmd_sunw, targ); 697789Sahrens 6980Sstevel@tonic-gate (void) close(rem); 6990Sstevel@tonic-gate host = thost; 7000Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, 701789Sahrens pwd->pw_name, 702789Sahrens tuser ? tuser : pwd->pw_name, 703789Sahrens bp, 0, AF_INET6); 7040Sstevel@tonic-gate if (rem < 0) 7050Sstevel@tonic-gate exit(1); 706789Sahrens 707789Sahrens if (read(rem, &resp, sizeof (resp)) 708789Sahrens != sizeof (resp)) 709789Sahrens lostconn(); 710789Sahrens if (resp != 0) { 711789Sahrens /* 712789Sahrens * Not OK: 713789Sahrens * The other side is running 714789Sahrens * non-acl rcp. Try again with 715789Sahrens * normal stuff 716789Sahrens */ 717789Sahrens aclflag = 0; 718789Sahrens (void) snprintf(bp, buffersize, 719789Sahrens "%s -t %s", cmd, targ); 720789Sahrens (void) close(rem); 721789Sahrens host = thost; 722789Sahrens rem = rcmd_af(&host, portnumber, 723789Sahrens pwd->pw_name, 724789Sahrens tuser ? tuser : 725789Sahrens pwd->pw_name, bp, 0, 726789Sahrens AF_INET6); 727789Sahrens if (rem < 0) 728789Sahrens exit(1); 729789Sahrens if (response() < 0) 730789Sahrens exit(1); 731789Sahrens } 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate /* everything should be fine now */ 7340Sstevel@tonic-gate (void) setuid(userid); 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate source(1, argv + i); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate static void 7440Sstevel@tonic-gate tolocal(int argc, char *argv[]) 7450Sstevel@tonic-gate { 7460Sstevel@tonic-gate int i; 7470Sstevel@tonic-gate char *host, *src, *suser, *lhost; 7480Sstevel@tonic-gate char resp; 7490Sstevel@tonic-gate size_t buffersize; 7500Sstevel@tonic-gate char bp[RCP_BUFSIZE]; 7510Sstevel@tonic-gate krb5_creds *cred; 752*4619Ssn199410 char *arglist[MAXARGS+1]; 7530Sstevel@tonic-gate buffersize = RCP_BUFSIZE; 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) { 7560Sstevel@tonic-gate if (!(src = colon(argv[i]))) { /* local to local */ 757*4619Ssn199410 (void) addargs(arglist, "cp", 758*4619Ssn199410 iamrecursive ? "-r" : "", pflag ? "-p" : "", 759*4619Ssn199410 zflag ? "-z" : "", argv[i], argv[argc - 1], 760*4619Ssn199410 (char *)NULL); 761*4619Ssn199410 if (susystem(_PATH_CP, arglist) == -1) 7620Sstevel@tonic-gate errs++; 7630Sstevel@tonic-gate continue; 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate *src++ = 0; 7660Sstevel@tonic-gate if (*src == 0) 7670Sstevel@tonic-gate src = "."; 7680Sstevel@tonic-gate host = search_char((unsigned char *)argv[i], '@'); 7690Sstevel@tonic-gate if (host) { 7700Sstevel@tonic-gate *host++ = 0; 7710Sstevel@tonic-gate suser = argv[i]; 7720Sstevel@tonic-gate if (*suser == '\0') { 7730Sstevel@tonic-gate suser = pwd->pw_name; 7740Sstevel@tonic-gate } else if (!okname(suser)) { 7750Sstevel@tonic-gate errs++; 7760Sstevel@tonic-gate continue; 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate } else { 7790Sstevel@tonic-gate host = argv[i]; 7800Sstevel@tonic-gate suser = pwd->pw_name; 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate host = removebrackets(host); 7830Sstevel@tonic-gate lhost = host; 7840Sstevel@tonic-gate if (krb5auth_flag > 0) { 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate (void) snprintf(bp, buffersize, "%s -f %s", cmd, src); 7870Sstevel@tonic-gate authopts = AP_OPTS_MUTUAL_REQUIRED; 7880Sstevel@tonic-gate status = kcmd(&sock, &host, 7890Sstevel@tonic-gate portnumber, 7900Sstevel@tonic-gate pwd->pw_name, suser, 7910Sstevel@tonic-gate bp, 7920Sstevel@tonic-gate 0, /* &rfd2 */ 7930Sstevel@tonic-gate "host", 7940Sstevel@tonic-gate krb_realm, 7950Sstevel@tonic-gate bsd_context, 7960Sstevel@tonic-gate &auth_context, 7970Sstevel@tonic-gate &cred, 7980Sstevel@tonic-gate 0, /* No seq # */ 7990Sstevel@tonic-gate 0, /* No server seq # */ 8000Sstevel@tonic-gate authopts, 8010Sstevel@tonic-gate 1, /* Not any port # */ 8020Sstevel@tonic-gate &kcmd_proto); 8030Sstevel@tonic-gate if (status) { 8040Sstevel@tonic-gate /* 8050Sstevel@tonic-gate * If new protocol requested, we dont 8060Sstevel@tonic-gate * fallback to less secure ones. 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 8090Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: kcmdv2 " 8100Sstevel@tonic-gate "to host %s failed - %s\n" 8110Sstevel@tonic-gate "Fallback to normal rcp denied."), 8120Sstevel@tonic-gate host, error_message(status)); 8130Sstevel@tonic-gate exit(1); 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate if (status != -1) { 8160Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: kcmd " 8170Sstevel@tonic-gate "to host %s failed - %s,\n" 8180Sstevel@tonic-gate "trying normal rcp...\n\n"), 8190Sstevel@tonic-gate host, error_message(status)); 8200Sstevel@tonic-gate } else { 8210Sstevel@tonic-gate (void) fprintf(stderr, 8220Sstevel@tonic-gate gettext("trying normal rcp...\n")); 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate /* 8250Sstevel@tonic-gate * kcmd() failed, so we have to 8260Sstevel@tonic-gate * fallback to normal rcp 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate try_normal_rcp(prev_argc, prev_argv); 8290Sstevel@tonic-gate } else { 8300Sstevel@tonic-gate rem = sock; 8310Sstevel@tonic-gate session_key = &cred->keyblock; 8320Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 8330Sstevel@tonic-gate status = krb5_auth_con_getlocalsubkey( 8340Sstevel@tonic-gate bsd_context, auth_context, 8350Sstevel@tonic-gate &session_key); 8360Sstevel@tonic-gate if (status) { 8370Sstevel@tonic-gate com_err("rcp", status, "determining " 8380Sstevel@tonic-gate "subkey for session"); 8390Sstevel@tonic-gate exit(1); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate if (!session_key) { 8420Sstevel@tonic-gate com_err("rcp", 0, "no subkey negotiated" 8430Sstevel@tonic-gate " for connection"); 8440Sstevel@tonic-gate exit(1); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate eblock.crypto_entry = session_key->enctype; 8480Sstevel@tonic-gate eblock.key = (krb5_keyblock *)session_key; 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate init_encrypt(encrypt_flag, bsd_context, kcmd_proto, 8510Sstevel@tonic-gate &desinbuf, &desoutbuf, CLIENT, 8520Sstevel@tonic-gate &eblock); 8530Sstevel@tonic-gate if (encrypt_flag > 0) { 8540Sstevel@tonic-gate char *s = gettext("This rcp " 8550Sstevel@tonic-gate "session is using DES " 8560Sstevel@tonic-gate "encryption for all " 8570Sstevel@tonic-gate "data transmissions." 8580Sstevel@tonic-gate "\r\n"); 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate (void) write(2, s, strlen(s)); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate else 8660Sstevel@tonic-gate { 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /* 8690Sstevel@tonic-gate * ACL support: try to find out if the remote site is 8700Sstevel@tonic-gate * running acl cognizant version of rcp. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate aclflag = 1; 873789Sahrens acl_aclflag = 1; 8740Sstevel@tonic-gate 875789Sahrens (void) snprintf(bp, buffersize, "%s -Zf %s", cmd_sunw, src); 8760Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, pwd->pw_name, suser, 8770Sstevel@tonic-gate bp, 0, AF_INET6); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate if (rem < 0) { 8800Sstevel@tonic-gate ++errs; 8810Sstevel@tonic-gate continue; 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * The remote system is supposed to send an ok response. 8860Sstevel@tonic-gate * If there are any data other than "ok", it must be error 8870Sstevel@tonic-gate * messages from the remote system. We can assume the 8880Sstevel@tonic-gate * remote system is running non-acl version rcp. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate if (read(rem, &resp, sizeof (resp)) != sizeof (resp)) 8910Sstevel@tonic-gate lostconn(); 8920Sstevel@tonic-gate if (resp != 0) { 893789Sahrens 894789Sahrens /* 895789Sahrens * Try again without ace_acl support 896789Sahrens */ 897789Sahrens acl_aclflag = 0; 898789Sahrens (void) snprintf(bp, buffersize, "%s -f %s", 899789Sahrens cmd_sunw, src); 900789Sahrens rem = rcmd_af(&host, portnumber, pwd->pw_name, suser, 901789Sahrens bp, 0, AF_INET6); 902789Sahrens 903789Sahrens if (rem < 0) { 904789Sahrens ++errs; 905789Sahrens continue; 906789Sahrens } 907789Sahrens 908789Sahrens if (read(rem, &resp, sizeof (resp)) != sizeof (resp)) 909789Sahrens lostconn(); 910789Sahrens 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * NOT ok: 9130Sstevel@tonic-gate * The other side is running non-acl rcp. 9140Sstevel@tonic-gate * Try again with normal stuff 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate aclflag = 0; 9170Sstevel@tonic-gate (void) snprintf(bp, buffersize, "%s -f %s", cmd, src); 9180Sstevel@tonic-gate (void) close(rem); 9190Sstevel@tonic-gate host = lhost; 9200Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, pwd->pw_name, 9210Sstevel@tonic-gate suser, bp, 0, AF_INET6); 9220Sstevel@tonic-gate if (rem < 0) { 9230Sstevel@tonic-gate ++errs; 9240Sstevel@tonic-gate continue; 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate sink(1, argv + argc - 1); 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate (void) close(rem); 9320Sstevel@tonic-gate rem = -1; 9330Sstevel@tonic-gate } 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate static void 9380Sstevel@tonic-gate verifydir(char *cp) 9390Sstevel@tonic-gate { 9400Sstevel@tonic-gate struct stat stb; 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate if (stat(cp, &stb) >= 0) { 9430Sstevel@tonic-gate if ((stb.st_mode & S_IFMT) == S_IFDIR) 9440Sstevel@tonic-gate return; 9450Sstevel@tonic-gate errno = ENOTDIR; 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate error("rcp: %s: %s.\n", cp, strerror(errno)); 9480Sstevel@tonic-gate exit(1); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate static char * 9520Sstevel@tonic-gate colon(char *cp) 9530Sstevel@tonic-gate { 9540Sstevel@tonic-gate boolean_t is_bracket_open = B_FALSE; 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate for (; *cp; ++cp) { 9570Sstevel@tonic-gate if (*cp == '[') 9580Sstevel@tonic-gate is_bracket_open = B_TRUE; 9590Sstevel@tonic-gate else if (*cp == ']') 9600Sstevel@tonic-gate is_bracket_open = B_FALSE; 9610Sstevel@tonic-gate else if (*cp == ':' && !is_bracket_open) 9620Sstevel@tonic-gate return (cp); 9630Sstevel@tonic-gate else if (*cp == '/') 9640Sstevel@tonic-gate return (0); 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate return (0); 9670Sstevel@tonic-gate } 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate static int 9700Sstevel@tonic-gate okname(char *cp0) 9710Sstevel@tonic-gate { 9720Sstevel@tonic-gate register char *cp = cp0; 9730Sstevel@tonic-gate register int c; 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate do { 9760Sstevel@tonic-gate c = *cp; 9770Sstevel@tonic-gate if (c & 0200) 9780Sstevel@tonic-gate goto bad; 9790Sstevel@tonic-gate if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 9800Sstevel@tonic-gate goto bad; 9810Sstevel@tonic-gate } while (*++cp); 9820Sstevel@tonic-gate return (1); 9830Sstevel@tonic-gate bad: 9840Sstevel@tonic-gate (void) fprintf(stderr, "rcp: invalid user name %s\n", cp0); 9850Sstevel@tonic-gate return (0); 9860Sstevel@tonic-gate } 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate static char * 9900Sstevel@tonic-gate removebrackets(char *str) 9910Sstevel@tonic-gate { 9920Sstevel@tonic-gate char *newstr = str; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) { 9950Sstevel@tonic-gate newstr = str + 1; 9960Sstevel@tonic-gate str[strlen(str) - 1] = '\0'; 9970Sstevel@tonic-gate } 9980Sstevel@tonic-gate return (newstr); 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate static int 1002*4619Ssn199410 susystem(char *path, char **arglist) 10030Sstevel@tonic-gate { 10040Sstevel@tonic-gate int status, pid, w; 10050Sstevel@tonic-gate register void (*istat)(), (*qstat)(); 10060Sstevel@tonic-gate int pfds[2]; 10070Sstevel@tonic-gate char buf[BUFSIZ]; 10080Sstevel@tonic-gate int cnt; 10090Sstevel@tonic-gate boolean_t seen_stderr_traffic; 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10120Sstevel@tonic-gate * Due to the fact that rcp uses rsh to copy between 2 remote 10130Sstevel@tonic-gate * machines, rsh doesn't return the exit status of the remote 10140Sstevel@tonic-gate * command, and we can't modify the rcmd protocol used by rsh 10150Sstevel@tonic-gate * (for interoperability reasons) we use the hack of using any 10160Sstevel@tonic-gate * output on stderr as indication that an error occurred and 10170Sstevel@tonic-gate * that we should return a non-zero error code. 10180Sstevel@tonic-gate */ 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate if (pipe(pfds) == -1) { 10210Sstevel@tonic-gate (void) fprintf(stderr, "Couldn't create pipe: %s\n", 10220Sstevel@tonic-gate strerror(errno)); 10230Sstevel@tonic-gate return (-1); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate if ((pid = vfork()) < 0) { 10270Sstevel@tonic-gate (void) close(pfds[0]); 10280Sstevel@tonic-gate (void) close(pfds[1]); 10290Sstevel@tonic-gate (void) fprintf(stderr, "Couldn't fork child process: %s\n", 10300Sstevel@tonic-gate strerror(errno)); 10310Sstevel@tonic-gate return (-1); 10320Sstevel@tonic-gate } else if (pid == 0) { 10330Sstevel@tonic-gate /* 10340Sstevel@tonic-gate * Child. 10350Sstevel@tonic-gate */ 10360Sstevel@tonic-gate (void) close(pfds[0]); 10370Sstevel@tonic-gate /* 10380Sstevel@tonic-gate * Send stderr messages down the pipe so that we can detect 10390Sstevel@tonic-gate * them in the parent process. 10400Sstevel@tonic-gate */ 10410Sstevel@tonic-gate if (pfds[1] != STDERR_FILENO) { 10420Sstevel@tonic-gate (void) dup2(pfds[1], STDERR_FILENO); 10430Sstevel@tonic-gate (void) close(pfds[1]); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * This shell does not inherit the additional privilege 10470Sstevel@tonic-gate * we have in our Permitted set. 10480Sstevel@tonic-gate */ 1049*4619Ssn199410 (void) execv(path, arglist); 10500Sstevel@tonic-gate _exit(127); 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate /* 10530Sstevel@tonic-gate * Parent. 10540Sstevel@tonic-gate */ 10550Sstevel@tonic-gate istat = signal(SIGINT, SIG_IGN); 10560Sstevel@tonic-gate qstat = signal(SIGQUIT, SIG_IGN); 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate (void) close(pfds[1]); 10590Sstevel@tonic-gate seen_stderr_traffic = B_FALSE; 10600Sstevel@tonic-gate while ((cnt = read(pfds[0], buf, sizeof (buf))) > 0) { 10610Sstevel@tonic-gate /* 10620Sstevel@tonic-gate * If any data is read from the pipe the child process 10630Sstevel@tonic-gate * has output something on stderr so we set the boolean 10640Sstevel@tonic-gate * 'seen_stderr_traffic' to true, which will cause the 10650Sstevel@tonic-gate * function to return -1. 10660Sstevel@tonic-gate */ 10670Sstevel@tonic-gate (void) write(STDERR_FILENO, buf, cnt); 10680Sstevel@tonic-gate seen_stderr_traffic = B_TRUE; 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate (void) close(pfds[0]); 10710Sstevel@tonic-gate while ((w = wait(&status)) != pid && w != -1) 10720Sstevel@tonic-gate ; 10730Sstevel@tonic-gate if (w == -1) 10740Sstevel@tonic-gate status = -1; 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate (void) signal(SIGINT, istat); 10770Sstevel@tonic-gate (void) signal(SIGQUIT, qstat); 10780Sstevel@tonic-gate 10790Sstevel@tonic-gate return (seen_stderr_traffic ? -1 : status); 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate static void 10830Sstevel@tonic-gate source(int argc, char *argv[]) 10840Sstevel@tonic-gate { 10850Sstevel@tonic-gate struct stat stb; 10860Sstevel@tonic-gate static BUF buffer; 10870Sstevel@tonic-gate BUF *bp; 10880Sstevel@tonic-gate int x, readerr, f, amt; 10890Sstevel@tonic-gate char *last, *name, buf[RCP_BUFSIZE]; 10900Sstevel@tonic-gate off_t off, size, i; 10910Sstevel@tonic-gate ssize_t cnt; 10923370Sjs198686 struct linger lingerbuf; 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate for (x = 0; x < argc; x++) { 10950Sstevel@tonic-gate name = argv[x]; 10960Sstevel@tonic-gate if ((f = open(name, O_RDONLY, 0)) < 0) { 10970Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno)); 10980Sstevel@tonic-gate continue; 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate if (fstat(f, &stb) < 0) 11010Sstevel@tonic-gate goto notreg; 11020Sstevel@tonic-gate switch (stb.st_mode&S_IFMT) { 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate case S_IFREG: 11050Sstevel@tonic-gate break; 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate case S_IFDIR: 11080Sstevel@tonic-gate if (iamrecursive) { 11090Sstevel@tonic-gate (void) close(f); 11100Sstevel@tonic-gate rsource(name, &stb); 11110Sstevel@tonic-gate continue; 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate /* FALLTHROUGH */ 11140Sstevel@tonic-gate default: 11150Sstevel@tonic-gate notreg: 11160Sstevel@tonic-gate (void) close(f); 11170Sstevel@tonic-gate error("rcp: %s: not a plain file\n", name); 11180Sstevel@tonic-gate continue; 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate last = rindex(name, '/'); 11210Sstevel@tonic-gate if (last == 0) 11220Sstevel@tonic-gate last = name; 11230Sstevel@tonic-gate else 11240Sstevel@tonic-gate last++; 11250Sstevel@tonic-gate if (pflag) { 11260Sstevel@tonic-gate time_t mtime, atime; 11270Sstevel@tonic-gate time_t now; 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate /* 11300Sstevel@tonic-gate * Make it compatible with possible future 11310Sstevel@tonic-gate * versions expecting microseconds. 11320Sstevel@tonic-gate */ 11330Sstevel@tonic-gate mtime = stb.st_mtime; 11340Sstevel@tonic-gate atime = stb.st_atime; 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate if ((mtime < 0) || (atime < 0)) { 11370Sstevel@tonic-gate now = time(NULL); 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate if (mtime < 0) { 11400Sstevel@tonic-gate mtime = now; 11410Sstevel@tonic-gate error("negative modification time on " 11420Sstevel@tonic-gate "%s; not preserving\n", name); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate if (atime < 0) { 11450Sstevel@tonic-gate atime = now; 11460Sstevel@tonic-gate error("negative access time on " 11470Sstevel@tonic-gate "%s; not preserving\n", name); 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "T%ld 0 %ld 0\n", 11510Sstevel@tonic-gate mtime, atime); 11520Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 11530Sstevel@tonic-gate if (response() < 0) { 11540Sstevel@tonic-gate (void) close(f); 11550Sstevel@tonic-gate continue; 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "C%04o %lld %s\n", 11590Sstevel@tonic-gate (uint_t)(stb.st_mode & 07777), (longlong_t)stb.st_size, 11600Sstevel@tonic-gate last); 11610Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 11620Sstevel@tonic-gate if (response() < 0) { 11630Sstevel@tonic-gate (void) close(f); 11640Sstevel@tonic-gate continue; 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate /* ACL support: send */ 1168789Sahrens if (aclflag | acl_aclflag) { 11690Sstevel@tonic-gate /* get acl from f and send it over */ 11700Sstevel@tonic-gate if (sendacl(f) == ACL_FAIL) { 11710Sstevel@tonic-gate (void) close(f); 11720Sstevel@tonic-gate continue; 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate } 11750Sstevel@tonic-gate if ((krb5auth_flag > 0) || (iamremote == 1)) { 11760Sstevel@tonic-gate bp = allocbuf(&buffer, f, RCP_BUFSIZE); 11770Sstevel@tonic-gate if (bp == NULLBUF) { 11780Sstevel@tonic-gate (void) close(f); 11790Sstevel@tonic-gate continue; 11800Sstevel@tonic-gate } 11810Sstevel@tonic-gate readerr = 0; 11820Sstevel@tonic-gate for (i = 0; i < stb.st_size; i += bp->cnt) { 11830Sstevel@tonic-gate amt = bp->cnt; 11840Sstevel@tonic-gate if (i + amt > stb.st_size) 11850Sstevel@tonic-gate amt = stb.st_size - i; 11860Sstevel@tonic-gate if (readerr == 0 && 11870Sstevel@tonic-gate read(f, bp->buf, amt) != amt) 11880Sstevel@tonic-gate readerr = errno; 11890Sstevel@tonic-gate (void) desrcpwrite(rem, bp->buf, amt); 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate (void) close(f); 11920Sstevel@tonic-gate if (readerr == 0) 11930Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 11940Sstevel@tonic-gate else 11950Sstevel@tonic-gate error("rcp: %s: %s\n", name, 11960Sstevel@tonic-gate error_message(readerr)); 11970Sstevel@tonic-gate } else { 11980Sstevel@tonic-gate cnt = off = 0; 11990Sstevel@tonic-gate size = stb.st_size; 12000Sstevel@tonic-gate while (size != 0) { 12010Sstevel@tonic-gate amt = MIN(size, SENDFILE_SIZE); 12020Sstevel@tonic-gate cnt = sendfile(rem, f, &off, amt); 12032039Sblu if (cnt == -1) { 12042039Sblu if (errno == EINTR) { 12052039Sblu continue; 12062039Sblu } else { 12072039Sblu break; 12082039Sblu } 12092039Sblu } 12103370Sjs198686 if (cnt == 0) 12113370Sjs198686 break; 12120Sstevel@tonic-gate size -= cnt; 12130Sstevel@tonic-gate } 12143370Sjs198686 if (cnt < 0) { 12150Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno)); 12163370Sjs198686 } else if (cnt == 0 && size != 0) { 12173370Sjs198686 error("rcp: %s: unexpected end of file\n", 12183370Sjs198686 name); 12193370Sjs198686 lingerbuf.l_onoff = 1; 12203370Sjs198686 lingerbuf.l_linger = 0; 12213370Sjs198686 (void) setsockopt(rem, SOL_SOCKET, SO_LINGER, 12223370Sjs198686 &lingerbuf, sizeof (lingerbuf)); 12233370Sjs198686 /* 12243370Sjs198686 * When response() (see below) is invoked it 12253370Sjs198686 * tries to read data from closed handle which 12263370Sjs198686 * triggers error and lostconn() function. 12273370Sjs198686 * lostconn() terminates the program with 12283370Sjs198686 * appropriate message. 12293370Sjs198686 */ 12303370Sjs198686 (void) close(rem); 12313370Sjs198686 rem = -1; 12320Sstevel@tonic-gate } else { 12330Sstevel@tonic-gate (void) write(rem, "", 1); 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate (void) close(f); 12360Sstevel@tonic-gate } 12370Sstevel@tonic-gate (void) response(); 12380Sstevel@tonic-gate } 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate 12420Sstevel@tonic-gate static void 12430Sstevel@tonic-gate rsource(char *name, struct stat *statp) 12440Sstevel@tonic-gate { 12450Sstevel@tonic-gate DIR *d; 12460Sstevel@tonic-gate struct dirent *dp; 12470Sstevel@tonic-gate char *last, *vect[1]; 12480Sstevel@tonic-gate char path[MAXPATHLEN]; 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate if (!(d = opendir(name))) { 12510Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno)); 12520Sstevel@tonic-gate return; 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate last = rindex(name, '/'); 12550Sstevel@tonic-gate if (last == 0) 12560Sstevel@tonic-gate last = name; 12570Sstevel@tonic-gate else 12580Sstevel@tonic-gate last++; 12590Sstevel@tonic-gate if (pflag) { 12600Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "T%ld 0 %ld 0\n", 12610Sstevel@tonic-gate statp->st_mtime, statp->st_atime); 12620Sstevel@tonic-gate (void) desrcpwrite(rem, path, strlen(path)); 12630Sstevel@tonic-gate if (response() < 0) { 12640Sstevel@tonic-gate (void) closedir(d); 12650Sstevel@tonic-gate return; 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "D%04o %d %s\n", 12690Sstevel@tonic-gate (uint_t)(statp->st_mode & 07777), 0, last); 12700Sstevel@tonic-gate (void) desrcpwrite(rem, path, strlen(path)); 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate /* acl support for directory */ 12730Sstevel@tonic-gate if (aclflag) { 12740Sstevel@tonic-gate /* get acl from f and send it over */ 12750Sstevel@tonic-gate if (sendacl(d->dd_fd) == ACL_FAIL) { 12760Sstevel@tonic-gate (void) closedir(d); 12770Sstevel@tonic-gate return; 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate if (response() < 0) { 12820Sstevel@tonic-gate (void) closedir(d); 12830Sstevel@tonic-gate return; 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate while (dp = readdir(d)) { 12870Sstevel@tonic-gate if (dp->d_ino == 0) 12880Sstevel@tonic-gate continue; 12890Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 12900Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 12910Sstevel@tonic-gate continue; 12920Sstevel@tonic-gate if ((uint_t)strlen(name) + 1 + strlen(dp->d_name) >= 12930Sstevel@tonic-gate MAXPATHLEN - 1) { 12940Sstevel@tonic-gate error("%s/%s: name too long.\n", name, dp->d_name); 12950Sstevel@tonic-gate continue; 12960Sstevel@tonic-gate } 12970Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s/%s", 12980Sstevel@tonic-gate name, dp->d_name); 12990Sstevel@tonic-gate vect[0] = path; 13000Sstevel@tonic-gate source(1, vect); 13010Sstevel@tonic-gate } 13020Sstevel@tonic-gate (void) closedir(d); 13030Sstevel@tonic-gate (void) desrcpwrite(rem, "E\n", 2); 13040Sstevel@tonic-gate (void) response(); 13050Sstevel@tonic-gate } 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate static int 13080Sstevel@tonic-gate response(void) 13090Sstevel@tonic-gate { 13100Sstevel@tonic-gate register char *cp; 13110Sstevel@tonic-gate char ch, resp, rbuf[RCP_BUFSIZE]; 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate if (desrcpread(rem, &resp, 1) != 1) 13140Sstevel@tonic-gate lostconn(); 13150Sstevel@tonic-gate cp = rbuf; 13160Sstevel@tonic-gate switch (resp) { 13170Sstevel@tonic-gate case 0: /* ok */ 13180Sstevel@tonic-gate return (0); 13190Sstevel@tonic-gate default: 13200Sstevel@tonic-gate *cp++ = resp; 13210Sstevel@tonic-gate /* FALLTHROUGH */ 13220Sstevel@tonic-gate case 1: /* error, followed by err msg */ 13230Sstevel@tonic-gate case 2: /* fatal error, "" */ 13240Sstevel@tonic-gate do { 13250Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) 13260Sstevel@tonic-gate lostconn(); 13270Sstevel@tonic-gate *cp++ = ch; 13280Sstevel@tonic-gate } while (cp < &rbuf[RCP_BUFSIZE] && ch != '\n'); 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate if (!iamremote) 13310Sstevel@tonic-gate (void) write(STDERR_FILENO, rbuf, cp - rbuf); 13320Sstevel@tonic-gate ++errs; 13330Sstevel@tonic-gate if (resp == 1) 13340Sstevel@tonic-gate return (-1); 13350Sstevel@tonic-gate exit(1); 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate /*NOTREACHED*/ 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate static void 13410Sstevel@tonic-gate lostconn(void) 13420Sstevel@tonic-gate { 13430Sstevel@tonic-gate if (!iamremote) 13440Sstevel@tonic-gate (void) fprintf(stderr, "rcp: lost connection\n"); 13450Sstevel@tonic-gate exit(1); 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate static void 13500Sstevel@tonic-gate sink(int argc, char *argv[]) 13510Sstevel@tonic-gate { 13520Sstevel@tonic-gate char *cp; 13530Sstevel@tonic-gate static BUF buffer; 13540Sstevel@tonic-gate struct stat stb; 13550Sstevel@tonic-gate struct timeval tv[2]; 13560Sstevel@tonic-gate BUF *bp; 13570Sstevel@tonic-gate off_t i, j; 13580Sstevel@tonic-gate char ch, *targ, *why; 13590Sstevel@tonic-gate int amt, count, exists, first, mask, mode; 13600Sstevel@tonic-gate off_t size; 13610Sstevel@tonic-gate int ofd, setimes, targisdir, wrerr; 13620Sstevel@tonic-gate char *np, *vect[1], buf[RCP_BUFSIZE]; 13630Sstevel@tonic-gate char *namebuf = NULL; 13640Sstevel@tonic-gate size_t namebuf_sz = 0; 13650Sstevel@tonic-gate size_t need; 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate #define atime tv[0] 13680Sstevel@tonic-gate #define mtime tv[1] 13690Sstevel@tonic-gate #define SCREWUP(str) { why = str; goto screwup; } 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate setimes = targisdir = 0; 13720Sstevel@tonic-gate mask = umask(0); 13730Sstevel@tonic-gate if (!pflag) 13740Sstevel@tonic-gate (void) umask(mask); 13750Sstevel@tonic-gate if (argc != 1) { 13760Sstevel@tonic-gate error("rcp: ambiguous target\n"); 13770Sstevel@tonic-gate exit(1); 13780Sstevel@tonic-gate } 13790Sstevel@tonic-gate targ = *argv; 13800Sstevel@tonic-gate if (targetshouldbedirectory) 13810Sstevel@tonic-gate verifydir(targ); 13820Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate if (stat(targ, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR) 13850Sstevel@tonic-gate targisdir = 1; 13860Sstevel@tonic-gate for (first = 1; ; first = 0) { 13870Sstevel@tonic-gate cp = buf; 13880Sstevel@tonic-gate if (desrcpread(rem, cp, 1) <= 0) { 13890Sstevel@tonic-gate if (namebuf != NULL) 13900Sstevel@tonic-gate free(namebuf); 13910Sstevel@tonic-gate return; 13920Sstevel@tonic-gate } 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate if (*cp++ == '\n') 13950Sstevel@tonic-gate SCREWUP("unexpected <newline>"); 13960Sstevel@tonic-gate do { 13970Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) 13980Sstevel@tonic-gate SCREWUP("lost connection"); 13990Sstevel@tonic-gate *cp++ = ch; 14000Sstevel@tonic-gate } while (cp < &buf[RCP_BUFSIZE - 1] && ch != '\n'); 14010Sstevel@tonic-gate *cp = 0; 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate if (buf[0] == '\01' || buf[0] == '\02') { 14040Sstevel@tonic-gate if (iamremote == 0) 14050Sstevel@tonic-gate (void) write(STDERR_FILENO, buf + 1, 14060Sstevel@tonic-gate strlen(buf + 1)); 14070Sstevel@tonic-gate if (buf[0] == '\02') 14080Sstevel@tonic-gate exit(1); 14090Sstevel@tonic-gate errs++; 14100Sstevel@tonic-gate continue; 14110Sstevel@tonic-gate } 14120Sstevel@tonic-gate if (buf[0] == 'E') { 14130Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 14140Sstevel@tonic-gate if (namebuf != NULL) 14150Sstevel@tonic-gate free(namebuf); 14160Sstevel@tonic-gate return; 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate if (ch == '\n') 14200Sstevel@tonic-gate *--cp = 0; 14210Sstevel@tonic-gate cp = buf; 14220Sstevel@tonic-gate if (*cp == 'T') { 14230Sstevel@tonic-gate setimes++; 14240Sstevel@tonic-gate cp++; 14250Sstevel@tonic-gate mtime.tv_sec = strtol(cp, &cp, 0); 14260Sstevel@tonic-gate if (*cp++ != ' ') 14270Sstevel@tonic-gate SCREWUP("mtime.sec not delimited"); 14280Sstevel@tonic-gate mtime.tv_usec = strtol(cp, &cp, 0); 14290Sstevel@tonic-gate if (*cp++ != ' ') 14300Sstevel@tonic-gate SCREWUP("mtime.usec not delimited"); 14310Sstevel@tonic-gate atime.tv_sec = strtol(cp, &cp, 0); 14320Sstevel@tonic-gate if (*cp++ != ' ') 14330Sstevel@tonic-gate SCREWUP("atime.sec not delimited"); 14340Sstevel@tonic-gate atime.tv_usec = strtol(cp, &cp, 0); 14350Sstevel@tonic-gate if (*cp++ != '\0') 14360Sstevel@tonic-gate SCREWUP("atime.usec not delimited"); 14370Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 14380Sstevel@tonic-gate continue; 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate if (*cp != 'C' && *cp != 'D') { 14410Sstevel@tonic-gate /* 14420Sstevel@tonic-gate * Check for the case "rcp remote:foo\* local:bar". 14430Sstevel@tonic-gate * In this case, the line "No match." can be returned 14440Sstevel@tonic-gate * by the shell before the rcp command on the remote is 14450Sstevel@tonic-gate * executed so the ^Aerror_message convention isn't 14460Sstevel@tonic-gate * followed. 14470Sstevel@tonic-gate */ 14480Sstevel@tonic-gate if (first) { 14490Sstevel@tonic-gate error("%s\n", cp); 14500Sstevel@tonic-gate exit(1); 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate SCREWUP("expected control record"); 14530Sstevel@tonic-gate } 14540Sstevel@tonic-gate mode = 0; 14550Sstevel@tonic-gate for (++cp; cp < buf + 5; cp++) { 14560Sstevel@tonic-gate if (*cp < '0' || *cp > '7') 14570Sstevel@tonic-gate SCREWUP("bad mode"); 14580Sstevel@tonic-gate mode = (mode << 3) | (*cp - '0'); 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate if (*cp++ != ' ') 14610Sstevel@tonic-gate SCREWUP("mode not delimited"); 14620Sstevel@tonic-gate size = 0; 14630Sstevel@tonic-gate while (isdigit(*cp)) 14640Sstevel@tonic-gate size = size * 10 + (*cp++ - '0'); 14650Sstevel@tonic-gate if (*cp++ != ' ') 14660Sstevel@tonic-gate SCREWUP("size not delimited"); 14670Sstevel@tonic-gate if (targisdir) { 14680Sstevel@tonic-gate need = strlen(targ) + sizeof ("/") + strlen(cp); 14690Sstevel@tonic-gate if (need > namebuf_sz) { 14700Sstevel@tonic-gate if ((namebuf = realloc(namebuf, need)) == NULL) { 14710Sstevel@tonic-gate error("rcp: out of memory\n"); 14720Sstevel@tonic-gate exit(1); 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate namebuf_sz = need; 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate (void) snprintf(namebuf, need, "%s%s%s", targ, 14770Sstevel@tonic-gate *targ ? "/" : "", cp); 14780Sstevel@tonic-gate np = namebuf; 14790Sstevel@tonic-gate } else { 14800Sstevel@tonic-gate np = targ; 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate exists = stat(np, &stb) == 0; 14840Sstevel@tonic-gate if (buf[0] == 'D') { 14850Sstevel@tonic-gate if (exists) { 14860Sstevel@tonic-gate if ((stb.st_mode&S_IFMT) != S_IFDIR) { 1487789Sahrens if (aclflag | acl_aclflag) { 14880Sstevel@tonic-gate /* 14890Sstevel@tonic-gate * consume acl in the pipe 14900Sstevel@tonic-gate * fd = -1 to indicate the 14910Sstevel@tonic-gate * special case 14920Sstevel@tonic-gate */ 14930Sstevel@tonic-gate if (recvacl(-1, exists, pflag) 14940Sstevel@tonic-gate == ACL_FAIL) { 14950Sstevel@tonic-gate goto bad; 14960Sstevel@tonic-gate } 14970Sstevel@tonic-gate } 14980Sstevel@tonic-gate errno = ENOTDIR; 14990Sstevel@tonic-gate goto bad; 15000Sstevel@tonic-gate } 15010Sstevel@tonic-gate if (pflag) 15020Sstevel@tonic-gate (void) chmod(np, mode); 15030Sstevel@tonic-gate } else if (mkdir(np, mode) < 0) { 15040Sstevel@tonic-gate if (aclflag) { 15050Sstevel@tonic-gate /* consume acl in the pipe */ 15060Sstevel@tonic-gate (void) recvacl(-1, exists, pflag); 15070Sstevel@tonic-gate } 15080Sstevel@tonic-gate goto bad; 15090Sstevel@tonic-gate } 15100Sstevel@tonic-gate 15110Sstevel@tonic-gate /* acl support for directories */ 1512789Sahrens if (aclflag | acl_aclflag) { 15130Sstevel@tonic-gate int dfd; 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate if ((dfd = open(np, O_RDONLY)) == -1) 15160Sstevel@tonic-gate goto bad; 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate /* get acl and set it to ofd */ 15190Sstevel@tonic-gate if (recvacl(dfd, exists, pflag) == ACL_FAIL) { 15200Sstevel@tonic-gate (void) close(dfd); 15210Sstevel@tonic-gate if (!exists) 15220Sstevel@tonic-gate (void) rmdir(np); 15230Sstevel@tonic-gate goto bad; 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate (void) close(dfd); 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate vect[0] = np; 15290Sstevel@tonic-gate sink(1, vect); 15300Sstevel@tonic-gate if (setimes) { 15310Sstevel@tonic-gate setimes = 0; 15320Sstevel@tonic-gate if (utimes(np, tv) < 0) 15330Sstevel@tonic-gate error("rcp: can't set times on %s: %s\n", 15340Sstevel@tonic-gate np, strerror(errno)); 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate continue; 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 15400Sstevel@tonic-gate bad: 15410Sstevel@tonic-gate error("rcp: %s: %s\n", np, strerror(errno)); 15420Sstevel@tonic-gate continue; 15430Sstevel@tonic-gate } 15440Sstevel@tonic-gate 15450Sstevel@tonic-gate /* 15460Sstevel@tonic-gate * If the output file exists we have to force zflag off 15470Sstevel@tonic-gate * to avoid erroneously seeking past old data. 15480Sstevel@tonic-gate */ 15490Sstevel@tonic-gate zopen(ofd, zflag && !exists); 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate if (exists && pflag) 15520Sstevel@tonic-gate (void) fchmod(ofd, mode); 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate /* 15570Sstevel@tonic-gate * ACL support: receiving 15580Sstevel@tonic-gate */ 1559789Sahrens if (aclflag | acl_aclflag) { 15600Sstevel@tonic-gate /* get acl and set it to ofd */ 15610Sstevel@tonic-gate if (recvacl(ofd, exists, pflag) == ACL_FAIL) { 15620Sstevel@tonic-gate (void) close(ofd); 15630Sstevel@tonic-gate if (!exists) 15640Sstevel@tonic-gate (void) unlink(np); 15650Sstevel@tonic-gate continue; 15660Sstevel@tonic-gate } 15670Sstevel@tonic-gate } 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate if ((bp = allocbuf(&buffer, ofd, RCP_BUFSIZE)) == 0) { 15700Sstevel@tonic-gate (void) close(ofd); 15710Sstevel@tonic-gate continue; 15720Sstevel@tonic-gate } 15730Sstevel@tonic-gate cp = bp->buf; 15740Sstevel@tonic-gate count = 0; 15750Sstevel@tonic-gate wrerr = 0; 15760Sstevel@tonic-gate for (i = 0; i < size; i += RCP_BUFSIZE) { 15770Sstevel@tonic-gate amt = RCP_BUFSIZE; 15780Sstevel@tonic-gate if (i + amt > size) 15790Sstevel@tonic-gate amt = size - i; 15800Sstevel@tonic-gate count += amt; 15810Sstevel@tonic-gate do { 15820Sstevel@tonic-gate j = desrcpread(rem, cp, amt); 15830Sstevel@tonic-gate if (j <= 0) { 15840Sstevel@tonic-gate int sverrno = errno; 15850Sstevel@tonic-gate 15860Sstevel@tonic-gate /* 15870Sstevel@tonic-gate * Connection to supplier lost. 15880Sstevel@tonic-gate * Truncate file to correspond 15890Sstevel@tonic-gate * to amount already transferred. 15900Sstevel@tonic-gate * 15910Sstevel@tonic-gate * Note that we must call ftruncate() 15920Sstevel@tonic-gate * before any call to error() (which 15930Sstevel@tonic-gate * might result in a SIGPIPE and 15940Sstevel@tonic-gate * sudden death before we have a chance 15950Sstevel@tonic-gate * to correct the file's size). 15960Sstevel@tonic-gate */ 15970Sstevel@tonic-gate size = lseek(ofd, 0, SEEK_CUR); 15980Sstevel@tonic-gate if ((ftruncate(ofd, size) == -1) && 15990Sstevel@tonic-gate (errno != EINVAL) && 16000Sstevel@tonic-gate (errno != EACCES)) 16010Sstevel@tonic-gate #define TRUNCERR "rcp: can't truncate %s: %s\n" 16020Sstevel@tonic-gate error(TRUNCERR, np, 16030Sstevel@tonic-gate strerror(errno)); 16040Sstevel@tonic-gate error("rcp: %s\n", 16050Sstevel@tonic-gate j ? strerror(sverrno) : 16060Sstevel@tonic-gate "dropped connection"); 16070Sstevel@tonic-gate (void) close(ofd); 16080Sstevel@tonic-gate exit(1); 16090Sstevel@tonic-gate } 16100Sstevel@tonic-gate amt -= j; 16110Sstevel@tonic-gate cp += j; 16120Sstevel@tonic-gate } while (amt > 0); 16130Sstevel@tonic-gate if (count == bp->cnt) { 16140Sstevel@tonic-gate cp = bp->buf; 16150Sstevel@tonic-gate if (wrerr == 0 && 16160Sstevel@tonic-gate zwrite(ofd, cp, count) < 0) 16170Sstevel@tonic-gate wrerr++; 16180Sstevel@tonic-gate count = 0; 16190Sstevel@tonic-gate } 16200Sstevel@tonic-gate } 16210Sstevel@tonic-gate if (count != 0 && wrerr == 0 && 16220Sstevel@tonic-gate zwrite(ofd, bp->buf, count) < 0) 16230Sstevel@tonic-gate wrerr++; 16240Sstevel@tonic-gate if (zclose(ofd) < 0) 16250Sstevel@tonic-gate wrerr++; 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate if ((ftruncate(ofd, size) == -1) && (errno != EINVAL) && 16290Sstevel@tonic-gate (errno != EACCES)) { 16300Sstevel@tonic-gate error(TRUNCERR, np, strerror(errno)); 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate (void) close(ofd); 16330Sstevel@tonic-gate (void) response(); 16340Sstevel@tonic-gate if (setimes) { 16350Sstevel@tonic-gate setimes = 0; 16360Sstevel@tonic-gate if (utimes(np, tv) < 0) 16370Sstevel@tonic-gate error("rcp: can't set times on %s: %s\n", 16380Sstevel@tonic-gate np, strerror(errno)); 16390Sstevel@tonic-gate } 16400Sstevel@tonic-gate if (wrerr) 16410Sstevel@tonic-gate error("rcp: %s: %s\n", np, strerror(errno)); 16420Sstevel@tonic-gate else 16430Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate screwup: 16460Sstevel@tonic-gate error("rcp: protocol screwup: %s\n", why); 16470Sstevel@tonic-gate exit(1); 16480Sstevel@tonic-gate } 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate #ifndef roundup 16510Sstevel@tonic-gate #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 16520Sstevel@tonic-gate #endif /* !roundup */ 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate static BUF * 16550Sstevel@tonic-gate allocbuf(BUF *bp, int fd, int blksize) 16560Sstevel@tonic-gate { 16570Sstevel@tonic-gate struct stat stb; 16580Sstevel@tonic-gate int size; 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate if (fstat(fd, &stb) < 0) { 16610Sstevel@tonic-gate error("rcp: fstat: %s\n", strerror(errno)); 16620Sstevel@tonic-gate return (0); 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate size = roundup(stb.st_blksize, blksize); 16650Sstevel@tonic-gate if (size == 0) 16660Sstevel@tonic-gate size = blksize; 16670Sstevel@tonic-gate if (bp->cnt < size) { 16680Sstevel@tonic-gate if (bp->buf != 0) 16690Sstevel@tonic-gate free(bp->buf); 16700Sstevel@tonic-gate bp->buf = (char *)malloc((uint_t)size); 16710Sstevel@tonic-gate if (!bp->buf) { 16720Sstevel@tonic-gate error("rcp: malloc: out of memory\n"); 16730Sstevel@tonic-gate return (0); 16740Sstevel@tonic-gate } 16750Sstevel@tonic-gate } 16760Sstevel@tonic-gate bp->cnt = size; 16770Sstevel@tonic-gate return (bp); 16780Sstevel@tonic-gate } 16790Sstevel@tonic-gate 16800Sstevel@tonic-gate static void 16810Sstevel@tonic-gate usage(void) 16820Sstevel@tonic-gate { 16830Sstevel@tonic-gate (void) fprintf(stderr, "%s: \t%s\t%s", gettext("Usage"), 16840Sstevel@tonic-gate gettext("\trcp [-p] [-a] [-x] [-k realm] [-PN / -PO] " 16850Sstevel@tonic-gate #ifdef DEBUG 16860Sstevel@tonic-gate "[-D port] " 16870Sstevel@tonic-gate #endif /* DEBUG */ 16880Sstevel@tonic-gate "f1 f2; or:\n"), 16890Sstevel@tonic-gate gettext("\trcp [-r] [-p] [-a] [-x] " 16900Sstevel@tonic-gate #ifdef DEBUG 16910Sstevel@tonic-gate "[-D port] " 16920Sstevel@tonic-gate #endif /* DEBUG */ 16930Sstevel@tonic-gate "[-k realm] [-PN / -PO] f1...fn d2\n")); 16940Sstevel@tonic-gate exit(1); 16950Sstevel@tonic-gate } 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate /* 16990Sstevel@tonic-gate * sparse file support 17000Sstevel@tonic-gate */ 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate static off_t zbsize; 17030Sstevel@tonic-gate static off_t zlastseek; 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate /* is it ok to try to create holes? */ 17060Sstevel@tonic-gate static void 17070Sstevel@tonic-gate zopen(int fd, int flag) 17080Sstevel@tonic-gate { 17090Sstevel@tonic-gate struct stat st; 17100Sstevel@tonic-gate 17110Sstevel@tonic-gate zbsize = 0; 17120Sstevel@tonic-gate zlastseek = 0; 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate if (flag && 17150Sstevel@tonic-gate fstat(fd, &st) == 0 && 17160Sstevel@tonic-gate (st.st_mode & S_IFMT) == S_IFREG) 17170Sstevel@tonic-gate zbsize = st.st_blksize; 17180Sstevel@tonic-gate } 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate /* write and/or seek */ 17210Sstevel@tonic-gate static int 17220Sstevel@tonic-gate zwrite(int fd, char *buf, int nbytes) 17230Sstevel@tonic-gate { 17240Sstevel@tonic-gate off_t block = zbsize ? zbsize : nbytes; 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate do { 17270Sstevel@tonic-gate if (block > nbytes) 17280Sstevel@tonic-gate block = nbytes; 17290Sstevel@tonic-gate nbytes -= block; 17300Sstevel@tonic-gate 17310Sstevel@tonic-gate if (!zbsize || notzero(buf, block)) { 17320Sstevel@tonic-gate register int n, count = block; 17330Sstevel@tonic-gate 17340Sstevel@tonic-gate do { 17350Sstevel@tonic-gate if ((n = write(fd, buf, count)) < 0) 17360Sstevel@tonic-gate return (-1); 17370Sstevel@tonic-gate buf += n; 17380Sstevel@tonic-gate } while ((count -= n) > 0); 17390Sstevel@tonic-gate zlastseek = 0; 17400Sstevel@tonic-gate } else { 17410Sstevel@tonic-gate if (lseek(fd, (off_t)block, SEEK_CUR) < 0) 17420Sstevel@tonic-gate return (-1); 17430Sstevel@tonic-gate buf += block; 17440Sstevel@tonic-gate zlastseek = 1; 17450Sstevel@tonic-gate } 17460Sstevel@tonic-gate } while (nbytes > 0); 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate return (0); 17490Sstevel@tonic-gate } 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate /* write last byte of file if necessary */ 17520Sstevel@tonic-gate static int 17530Sstevel@tonic-gate zclose(int fd) 17540Sstevel@tonic-gate { 17550Sstevel@tonic-gate zbsize = 0; 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate if (zlastseek && (lseek(fd, (off_t)-1, SEEK_CUR) < 0 || 17580Sstevel@tonic-gate zwrite(fd, "", 1) < 0)) 17590Sstevel@tonic-gate return (-1); 17600Sstevel@tonic-gate else 17610Sstevel@tonic-gate return (0); 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate /* return true if buffer is not all zeros */ 17650Sstevel@tonic-gate static int 17660Sstevel@tonic-gate notzero(char *p, int n) 17670Sstevel@tonic-gate { 17680Sstevel@tonic-gate register int result = 0; 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate while ((int)p & 3 && --n >= 0) 17710Sstevel@tonic-gate result |= *p++; 17720Sstevel@tonic-gate 17730Sstevel@tonic-gate while ((n -= 4 * sizeof (int)) >= 0) { 17740Sstevel@tonic-gate /* LINTED */ 17750Sstevel@tonic-gate result |= ((int *)p)[0]; 17760Sstevel@tonic-gate /* LINTED */ 17770Sstevel@tonic-gate result |= ((int *)p)[1]; 17780Sstevel@tonic-gate /* LINTED */ 17790Sstevel@tonic-gate result |= ((int *)p)[2]; 17800Sstevel@tonic-gate /* LINTED */ 17810Sstevel@tonic-gate result |= ((int *)p)[3]; 17820Sstevel@tonic-gate if (result) 17830Sstevel@tonic-gate return (result); 17840Sstevel@tonic-gate p += 4 * sizeof (int); 17850Sstevel@tonic-gate } 17860Sstevel@tonic-gate n += 4 * sizeof (int); 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate while (--n >= 0) 17890Sstevel@tonic-gate result |= *p++; 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate return (result); 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate /* 17950Sstevel@tonic-gate * New functions to support ACLs 17960Sstevel@tonic-gate */ 17970Sstevel@tonic-gate 17980Sstevel@tonic-gate /* 17990Sstevel@tonic-gate * Get acl from f and send it over. 18000Sstevel@tonic-gate * ACL record includes acl entry count, acl text length, and acl text. 18010Sstevel@tonic-gate */ 18020Sstevel@tonic-gate static int 18030Sstevel@tonic-gate sendacl(int f) 18040Sstevel@tonic-gate { 18050Sstevel@tonic-gate int aclcnt; 18060Sstevel@tonic-gate char *acltext; 18070Sstevel@tonic-gate char buf[BUFSIZ]; 1808789Sahrens acl_t *aclp; 1809789Sahrens char acltype; 1810789Sahrens int aclerror; 1811789Sahrens int trivial; 18120Sstevel@tonic-gate 1813789Sahrens 1814789Sahrens aclerror = facl_get(f, ACL_NO_TRIVIAL, &aclp); 1815789Sahrens if (aclerror != 0) { 1816789Sahrens error("can't retrieve ACL: %s \n", acl_strerror(aclerror)); 18170Sstevel@tonic-gate return (ACL_FAIL); 18180Sstevel@tonic-gate } 18190Sstevel@tonic-gate 1820789Sahrens /* 1821789Sahrens * if acl type is not ACLENT_T and were operating in acl_aclflag == 0 1822789Sahrens * then don't do the malloc and facl(fd, getcntcmd,...); 1823789Sahrens * since the remote side doesn't support alternate style ACL's. 1824789Sahrens */ 1825789Sahrens 1826789Sahrens if (aclp && (acl_type(aclp) != ACLENT_T) && (acl_aclflag == 0)) { 1827789Sahrens aclcnt = MIN_ACL_ENTRIES; 1828789Sahrens acltype = 'A'; 1829789Sahrens trivial = ACL_IS_TRIVIAL; 1830789Sahrens } else { 1831789Sahrens 1832789Sahrens aclcnt = (aclp != NULL) ? acl_cnt(aclp) : 0; 1833789Sahrens 1834789Sahrens if (aclp) { 1835789Sahrens acltype = (acl_type(aclp) != ACLENT_T) ? 'Z' : 'A'; 1836789Sahrens aclcnt = acl_cnt(aclp); 1837789Sahrens trivial = (acl_flags(aclp) & ACL_IS_TRIVIAL); 1838789Sahrens } else { 1839789Sahrens acltype = 'A'; 1840789Sahrens aclcnt = MIN_ACL_ENTRIES; 1841789Sahrens trivial = ACL_IS_TRIVIAL; 1842789Sahrens } 1843789Sahrens 1844789Sahrens } 1845789Sahrens 18460Sstevel@tonic-gate /* send the acl count over */ 1847789Sahrens (void) snprintf(buf, sizeof (buf), "%c%d\n", acltype, aclcnt); 18480Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 18490Sstevel@tonic-gate 1850789Sahrens /* 1851789Sahrens * only send acl when we have an aclp, which would 1852789Sahrens * imply its not trivial. 1853789Sahrens */ 1854789Sahrens if (aclp && (trivial != ACL_IS_TRIVIAL)) { 18551420Smarks acltext = acl_totext(aclp, 0); 18560Sstevel@tonic-gate if (acltext == NULL) { 18570Sstevel@tonic-gate error("rcp: failed to convert to text\n"); 1858789Sahrens acl_free(aclp); 18590Sstevel@tonic-gate return (ACL_FAIL); 18600Sstevel@tonic-gate } 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate /* send ACLs over: send the length first */ 1863789Sahrens (void) snprintf(buf, sizeof (buf), "%c%d\n", 1864789Sahrens acltype, strlen(acltext)); 18650Sstevel@tonic-gate 18660Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 18670Sstevel@tonic-gate (void) desrcpwrite(rem, acltext, strlen(acltext)); 18680Sstevel@tonic-gate free(acltext); 1869789Sahrens if (response() < 0) { 1870789Sahrens acl_free(aclp); 18710Sstevel@tonic-gate return (ACL_FAIL); 1872789Sahrens } 18730Sstevel@tonic-gate 18740Sstevel@tonic-gate } 1875789Sahrens 1876789Sahrens if (aclp) 1877789Sahrens acl_free(aclp); 18780Sstevel@tonic-gate return (ACL_OK); 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate /* 18820Sstevel@tonic-gate * Use this routine to get acl entry count and acl text size (in bytes) 18830Sstevel@tonic-gate */ 18840Sstevel@tonic-gate static int 1885789Sahrens getaclinfo(int *cnt, int *acltype) 18860Sstevel@tonic-gate { 18870Sstevel@tonic-gate char buf[BUFSIZ]; 18880Sstevel@tonic-gate char *cp; 18890Sstevel@tonic-gate char ch; 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate /* get acl count */ 18920Sstevel@tonic-gate cp = buf; 18930Sstevel@tonic-gate if (desrcpread(rem, cp, 1) <= 0) 18940Sstevel@tonic-gate return (ACL_FAIL); 1895789Sahrens 1896789Sahrens switch (*cp++) { 1897789Sahrens case 'A': 1898789Sahrens *acltype = 0; 1899789Sahrens break; 1900789Sahrens case 'Z': 1901789Sahrens *acltype = 1; 1902789Sahrens break; 1903789Sahrens default: 19040Sstevel@tonic-gate error("rcp: expect an ACL record, but got %c\n", *cp); 19050Sstevel@tonic-gate return (ACL_FAIL); 19060Sstevel@tonic-gate } 19070Sstevel@tonic-gate do { 19080Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) { 19090Sstevel@tonic-gate error("rcp: lost connection ..\n"); 19100Sstevel@tonic-gate return (ACL_FAIL); 19110Sstevel@tonic-gate } 19120Sstevel@tonic-gate *cp++ = ch; 19130Sstevel@tonic-gate } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 19140Sstevel@tonic-gate if (ch != '\n') { 19150Sstevel@tonic-gate error("rcp: ACL record corrupted \n"); 19160Sstevel@tonic-gate return (ACL_FAIL); 19170Sstevel@tonic-gate } 19180Sstevel@tonic-gate cp = &buf[1]; 19190Sstevel@tonic-gate *cnt = strtol(cp, &cp, 0); 19200Sstevel@tonic-gate if (*cp != '\n') { 19210Sstevel@tonic-gate error("rcp: ACL record corrupted \n"); 19220Sstevel@tonic-gate return (ACL_FAIL); 19230Sstevel@tonic-gate } 19240Sstevel@tonic-gate return (ACL_OK); 19250Sstevel@tonic-gate } 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate 19280Sstevel@tonic-gate /* 19290Sstevel@tonic-gate * Receive acl from the pipe and set it to f 19300Sstevel@tonic-gate */ 19310Sstevel@tonic-gate static int 19320Sstevel@tonic-gate recvacl(int f, int exists, int preserve) 19330Sstevel@tonic-gate { 19340Sstevel@tonic-gate int aclcnt; /* acl entry count */ 19350Sstevel@tonic-gate int aclsize; /* acl text length */ 19360Sstevel@tonic-gate int j; 19370Sstevel@tonic-gate char *tp; 19380Sstevel@tonic-gate char *acltext; /* external format */ 1939789Sahrens acl_t *aclp; 1940789Sahrens int acltype; 1941789Sahrens int min_entries; 1942789Sahrens int aclerror; 19430Sstevel@tonic-gate 19440Sstevel@tonic-gate /* get acl count */ 1945789Sahrens if (getaclinfo(&aclcnt, &acltype) != ACL_OK) 19460Sstevel@tonic-gate return (ACL_FAIL); 19470Sstevel@tonic-gate 1948789Sahrens if (acltype == 0) { 1949789Sahrens min_entries = MIN_ACL_ENTRIES; 1950789Sahrens } else { 1951789Sahrens min_entries = 1; 1952789Sahrens } 1953789Sahrens 1954789Sahrens if (aclcnt > min_entries) { 19550Sstevel@tonic-gate /* get acl text size */ 1956789Sahrens if (getaclinfo(&aclsize, &acltype) != ACL_OK) 19570Sstevel@tonic-gate return (ACL_FAIL); 19580Sstevel@tonic-gate if ((acltext = malloc(aclsize + 1)) == NULL) { 19590Sstevel@tonic-gate error("rcp: cant allocate memory: %d\n", aclsize); 19600Sstevel@tonic-gate return (ACL_FAIL); 19610Sstevel@tonic-gate } 19620Sstevel@tonic-gate 19630Sstevel@tonic-gate tp = acltext; 19640Sstevel@tonic-gate do { 19650Sstevel@tonic-gate j = desrcpread(rem, tp, aclsize); 19660Sstevel@tonic-gate if (j <= 0) { 19670Sstevel@tonic-gate error("rcp: %s\n", j ? strerror(errno) : 19680Sstevel@tonic-gate "dropped connection"); 19690Sstevel@tonic-gate exit(1); 19700Sstevel@tonic-gate } 19710Sstevel@tonic-gate aclsize -= j; 19720Sstevel@tonic-gate tp += j; 19730Sstevel@tonic-gate } while (aclsize > 0); 19740Sstevel@tonic-gate *tp = '\0'; 19750Sstevel@tonic-gate 19760Sstevel@tonic-gate if (preserve || !exists) { 1977789Sahrens aclerror = acl_fromtext(acltext, &aclp); 1978789Sahrens if (aclerror != 0) { 1979789Sahrens error("rcp: failed to parse acl : %s\n", 1980789Sahrens acl_strerror(aclerror)); 19813226Smp204432 free(acltext); 19820Sstevel@tonic-gate return (ACL_FAIL); 19830Sstevel@tonic-gate } 1984789Sahrens 19850Sstevel@tonic-gate if (f != -1) { 1986789Sahrens if (facl_set(f, aclp) < 0) { 19870Sstevel@tonic-gate error("rcp: failed to set acl\n"); 19883226Smp204432 acl_free(aclp); 19893226Smp204432 free(acltext); 19900Sstevel@tonic-gate return (ACL_FAIL); 19910Sstevel@tonic-gate } 19920Sstevel@tonic-gate } 19930Sstevel@tonic-gate /* -1 means that just consume the data in the pipe */ 1994789Sahrens acl_free(aclp); 19950Sstevel@tonic-gate } 19960Sstevel@tonic-gate free(acltext); 19970Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate return (ACL_OK); 20000Sstevel@tonic-gate } 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate 20030Sstevel@tonic-gate static char * 20040Sstevel@tonic-gate search_char(unsigned char *cp, unsigned char chr) 20050Sstevel@tonic-gate { 20060Sstevel@tonic-gate int len; 20070Sstevel@tonic-gate 20080Sstevel@tonic-gate while (*cp) { 20090Sstevel@tonic-gate if (*cp == chr) 20100Sstevel@tonic-gate return ((char *)cp); 20110Sstevel@tonic-gate if ((len = mblen((char *)cp, MB_CUR_MAX)) <= 0) 20120Sstevel@tonic-gate len = 1; 20130Sstevel@tonic-gate cp += len; 20140Sstevel@tonic-gate } 20150Sstevel@tonic-gate return (0); 20160Sstevel@tonic-gate } 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate 20190Sstevel@tonic-gate static int 20200Sstevel@tonic-gate desrcpread(int fd, char *buf, int len) 20210Sstevel@tonic-gate { 20220Sstevel@tonic-gate return ((int)desread(fd, buf, len, 0)); 20230Sstevel@tonic-gate } 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate static int 20260Sstevel@tonic-gate desrcpwrite(int fd, char *buf, int len) 20270Sstevel@tonic-gate { 20280Sstevel@tonic-gate /* 20290Sstevel@tonic-gate * Note that rcp depends on the same file descriptor being both 20300Sstevel@tonic-gate * input and output to the remote side. This is bogus, especially 20310Sstevel@tonic-gate * when rcp is being run by a rsh that pipes. Fix it here because 20320Sstevel@tonic-gate * it would require significantly more work in other places. 20330Sstevel@tonic-gate * --hartmans 1/96 20340Sstevel@tonic-gate */ 20350Sstevel@tonic-gate 20360Sstevel@tonic-gate if (fd == 0) 20370Sstevel@tonic-gate fd = 1; 20380Sstevel@tonic-gate return ((int)deswrite(fd, buf, len, 0)); 20390Sstevel@tonic-gate } 20400Sstevel@tonic-gate 20410Sstevel@tonic-gate static char ** 20420Sstevel@tonic-gate save_argv(int argc, char **argv) 20430Sstevel@tonic-gate { 20440Sstevel@tonic-gate int i; 20450Sstevel@tonic-gate 20460Sstevel@tonic-gate char **local_argv = (char **)calloc((unsigned)argc + 1, 20470Sstevel@tonic-gate (unsigned)sizeof (char *)); 20480Sstevel@tonic-gate 20490Sstevel@tonic-gate /* 20500Sstevel@tonic-gate * allocate an extra pointer, so that it is initialized to NULL and 20510Sstevel@tonic-gate * execv() will work 20520Sstevel@tonic-gate */ 20530Sstevel@tonic-gate for (i = 0; i < argc; i++) { 20540Sstevel@tonic-gate local_argv[i] = strsave(argv[i]); 20550Sstevel@tonic-gate } 20560Sstevel@tonic-gate 20570Sstevel@tonic-gate return (local_argv); 20580Sstevel@tonic-gate } 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate #define SIZEOF_INADDR sizeof (struct in_addr) 20610Sstevel@tonic-gate 20620Sstevel@tonic-gate static void 20630Sstevel@tonic-gate answer_auth(char *config_file, char *ccache_file) 20640Sstevel@tonic-gate { 20650Sstevel@tonic-gate krb5_data pname_data, msg; 20660Sstevel@tonic-gate krb5_creds creds, *new_creds; 20670Sstevel@tonic-gate krb5_ccache cc; 20680Sstevel@tonic-gate krb5_auth_context auth_context = NULL; 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate if (config_file) { 20710Sstevel@tonic-gate const char *filenames[2]; 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate filenames[1] = NULL; 20740Sstevel@tonic-gate filenames[0] = config_file; 20750Sstevel@tonic-gate if (krb5_set_config_files(bsd_context, filenames)) 20760Sstevel@tonic-gate exit(1); 20770Sstevel@tonic-gate } 20780Sstevel@tonic-gate (void) memset((char *)&creds, 0, sizeof (creds)); 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate if (krb5_read_message(bsd_context, (krb5_pointer) &rem, &pname_data)) 20810Sstevel@tonic-gate exit(1); 20820Sstevel@tonic-gate 20830Sstevel@tonic-gate if (krb5_read_message(bsd_context, (krb5_pointer) &rem, 20840Sstevel@tonic-gate &creds.second_ticket)) 20850Sstevel@tonic-gate exit(1); 20860Sstevel@tonic-gate 20870Sstevel@tonic-gate if (ccache_file == NULL) { 20880Sstevel@tonic-gate if (krb5_cc_default(bsd_context, &cc)) 20890Sstevel@tonic-gate exit(1); 20900Sstevel@tonic-gate } else { 20910Sstevel@tonic-gate if (krb5_cc_resolve(bsd_context, ccache_file, &cc)) 20920Sstevel@tonic-gate exit(1); 20930Sstevel@tonic-gate } 20940Sstevel@tonic-gate 20950Sstevel@tonic-gate if (krb5_cc_get_principal(bsd_context, cc, &creds.client)) 20960Sstevel@tonic-gate exit(1); 20970Sstevel@tonic-gate 20980Sstevel@tonic-gate if (krb5_parse_name(bsd_context, pname_data.data, &creds.server)) 20990Sstevel@tonic-gate exit(1); 21000Sstevel@tonic-gate 21010Sstevel@tonic-gate krb5_xfree(pname_data.data); 21020Sstevel@tonic-gate if (krb5_get_credentials(bsd_context, KRB5_GC_USER_USER, cc, &creds, 21030Sstevel@tonic-gate &new_creds)) 21040Sstevel@tonic-gate exit(1); 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate if (krb5_mk_req_extended(bsd_context, &auth_context, 21070Sstevel@tonic-gate AP_OPTS_USE_SESSION_KEY, NULL, new_creds, &msg)) 21080Sstevel@tonic-gate exit(1); 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate if (krb5_write_message(bsd_context, (krb5_pointer) & rem, &msg)) { 21110Sstevel@tonic-gate krb5_xfree(msg.data); 21120Sstevel@tonic-gate exit(1); 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate /* setup eblock for des_read and write */ 21150Sstevel@tonic-gate krb5_copy_keyblock(bsd_context, &new_creds->keyblock, &session_key); 21160Sstevel@tonic-gate 21170Sstevel@tonic-gate /* OK process key */ 21180Sstevel@tonic-gate eblock.crypto_entry = session_key->enctype; 21190Sstevel@tonic-gate eblock.key = (krb5_keyblock *)session_key; 21200Sstevel@tonic-gate 21210Sstevel@tonic-gate init_encrypt(encrypt_flag, bsd_context, KCMD_OLD_PROTOCOL, 21220Sstevel@tonic-gate &desinbuf, &desoutbuf, CLIENT, &eblock); 21230Sstevel@tonic-gate /* cleanup */ 21240Sstevel@tonic-gate krb5_free_cred_contents(bsd_context, &creds); 21250Sstevel@tonic-gate krb5_free_creds(bsd_context, new_creds); 21260Sstevel@tonic-gate krb5_xfree(msg.data); 21270Sstevel@tonic-gate } 21280Sstevel@tonic-gate 21290Sstevel@tonic-gate 21300Sstevel@tonic-gate static void 21310Sstevel@tonic-gate try_normal_rcp(int cur_argc, char **cur_argv) 21320Sstevel@tonic-gate { 21330Sstevel@tonic-gate char *target; 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate /* 21360Sstevel@tonic-gate * Reset all KRB5 relevant flags and set the 21370Sstevel@tonic-gate * cmd-buffer so that normal rcp works 21380Sstevel@tonic-gate */ 21390Sstevel@tonic-gate krb5auth_flag = encrypt_flag = encrypt_done = 0; 21400Sstevel@tonic-gate cmd = cmd_orig; 21410Sstevel@tonic-gate cmd_sunw = cmd_sunw_orig; 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate if (cur_argc < 2) 21440Sstevel@tonic-gate usage(); 21450Sstevel@tonic-gate 21460Sstevel@tonic-gate if (cur_argc > 2) 21470Sstevel@tonic-gate targetshouldbedirectory = 1; 21480Sstevel@tonic-gate 21490Sstevel@tonic-gate rem = -1; 21500Sstevel@tonic-gate 21510Sstevel@tonic-gate prev_argc = cur_argc; 21520Sstevel@tonic-gate prev_argv = save_argv(cur_argc, cur_argv); 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate (void) init_service(krb5auth_flag); 21550Sstevel@tonic-gate 21560Sstevel@tonic-gate if (target = colon(cur_argv[cur_argc - 1])) { 21570Sstevel@tonic-gate toremote(target, cur_argc, cur_argv); 21580Sstevel@tonic-gate } else { 21590Sstevel@tonic-gate tolocal(cur_argc, cur_argv); 21600Sstevel@tonic-gate if (targetshouldbedirectory) 21610Sstevel@tonic-gate verifydir(cur_argv[cur_argc - 1]); 21620Sstevel@tonic-gate } 21630Sstevel@tonic-gate exit(errs); 21640Sstevel@tonic-gate /* NOTREACHED */ 21650Sstevel@tonic-gate } 21660Sstevel@tonic-gate 21670Sstevel@tonic-gate 21680Sstevel@tonic-gate static int 21690Sstevel@tonic-gate init_service(int krb5flag) 21700Sstevel@tonic-gate { 21710Sstevel@tonic-gate struct servent *sp; 21720Sstevel@tonic-gate boolean_t success = B_FALSE; 21730Sstevel@tonic-gate 21740Sstevel@tonic-gate if (krb5flag > 0) { 21750Sstevel@tonic-gate sp = getservbyname("kshell", "tcp"); 21760Sstevel@tonic-gate if (sp == NULL) { 21770Sstevel@tonic-gate (void) fprintf(stderr, 21780Sstevel@tonic-gate gettext("rcp: kshell/tcp: unknown service.\n" 21790Sstevel@tonic-gate "trying normal shell/tcp service\n")); 21800Sstevel@tonic-gate } else { 21810Sstevel@tonic-gate portnumber = sp->s_port; 21820Sstevel@tonic-gate success = B_TRUE; 21830Sstevel@tonic-gate } 21840Sstevel@tonic-gate } else { 21850Sstevel@tonic-gate portnumber = htons(IPPORT_CMDSERVER); 21860Sstevel@tonic-gate success = B_TRUE; 21870Sstevel@tonic-gate } 21880Sstevel@tonic-gate return (success); 21890Sstevel@tonic-gate } 21900Sstevel@tonic-gate 21910Sstevel@tonic-gate /*PRINTFLIKE1*/ 21920Sstevel@tonic-gate static void 21930Sstevel@tonic-gate error(char *fmt, ...) 21940Sstevel@tonic-gate { 21950Sstevel@tonic-gate va_list ap; 21960Sstevel@tonic-gate char buf[RCP_BUFSIZE]; 21970Sstevel@tonic-gate char *cp = buf; 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate va_start(ap, fmt); 22000Sstevel@tonic-gate errs++; 22010Sstevel@tonic-gate *cp++ = 1; 22020Sstevel@tonic-gate (void) vsnprintf(cp, sizeof (buf) - 1, fmt, ap); 22030Sstevel@tonic-gate va_end(ap); 22040Sstevel@tonic-gate 22050Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 22060Sstevel@tonic-gate if (iamremote == 0) 22070Sstevel@tonic-gate (void) write(2, buf + 1, strlen(buf + 1)); 22080Sstevel@tonic-gate } 2209*4619Ssn199410 2210*4619Ssn199410 static void 2211*4619Ssn199410 addargs(char **arglist, ...) 2212*4619Ssn199410 { 2213*4619Ssn199410 va_list ap; 2214*4619Ssn199410 int i = 0; 2215*4619Ssn199410 char *pm; 2216*4619Ssn199410 2217*4619Ssn199410 va_start(ap, arglist); 2218*4619Ssn199410 while (i < MAXARGS && (pm = va_arg(ap, char *)) != NULL) 2219*4619Ssn199410 if (strcmp(pm, "")) 2220*4619Ssn199410 arglist[i++] = pm; 2221*4619Ssn199410 arglist[i] = NULL; 2222*4619Ssn199410 va_end(ap); 2223*4619Ssn199410 } 2224