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