1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate /* 9*0Sstevel@tonic-gate * Copyright (c) 1983 The Regents of the University of California. 10*0Sstevel@tonic-gate * All rights reserved. 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted 13*0Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are 14*0Sstevel@tonic-gate * duplicated in all such forms and that any documentation, 15*0Sstevel@tonic-gate * advertising materials, and other materials related to such 16*0Sstevel@tonic-gate * distribution and use acknowledge that the software was developed 17*0Sstevel@tonic-gate * by the University of California, Berkeley. The name of the 18*0Sstevel@tonic-gate * University may not be used to endorse or promote products derived 19*0Sstevel@tonic-gate * from this software without specific prior written permission. 20*0Sstevel@tonic-gate * 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate #define _FILE_OFFSET_BITS 64 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate /* 26*0Sstevel@tonic-gate * rcp 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate #include <sys/param.h> 29*0Sstevel@tonic-gate #include <sys/file.h> 30*0Sstevel@tonic-gate #include <sys/stat.h> 31*0Sstevel@tonic-gate #include <sys/time.h> 32*0Sstevel@tonic-gate #include <sys/types.h> 33*0Sstevel@tonic-gate #include <sys/ioctl.h> 34*0Sstevel@tonic-gate #include <sys/acl.h> 35*0Sstevel@tonic-gate #include <dirent.h> 36*0Sstevel@tonic-gate #include <signal.h> 37*0Sstevel@tonic-gate #include <sys/socket.h> 38*0Sstevel@tonic-gate #include <netinet/in.h> 39*0Sstevel@tonic-gate #include <pwd.h> 40*0Sstevel@tonic-gate #include <netdb.h> 41*0Sstevel@tonic-gate #include <wchar.h> 42*0Sstevel@tonic-gate #include <stdlib.h> 43*0Sstevel@tonic-gate #include <errno.h> 44*0Sstevel@tonic-gate #include <locale.h> 45*0Sstevel@tonic-gate #include <strings.h> 46*0Sstevel@tonic-gate #include <stdio.h> 47*0Sstevel@tonic-gate #include <ctype.h> 48*0Sstevel@tonic-gate #include <fcntl.h> 49*0Sstevel@tonic-gate #include <unistd.h> 50*0Sstevel@tonic-gate #include <limits.h> 51*0Sstevel@tonic-gate #include <priv_utils.h> 52*0Sstevel@tonic-gate #include <sys/sendfile.h> 53*0Sstevel@tonic-gate #include <sys/sysmacros.h> 54*0Sstevel@tonic-gate #include <sys/wait.h> 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate /* 57*0Sstevel@tonic-gate * It seems like Berkeley got these from pathnames.h? 58*0Sstevel@tonic-gate */ 59*0Sstevel@tonic-gate #define _PATH_RSH "/usr/bin/rsh" 60*0Sstevel@tonic-gate #define _PATH_CP "/usr/bin/cp" 61*0Sstevel@tonic-gate #define _PATH_BSHELL "/usr/bin/sh" 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate #define ACL_FAIL 1 64*0Sstevel@tonic-gate #define ACL_OK 0 65*0Sstevel@tonic-gate #define RCP_BUFSIZE (64 * 1024) 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate #define RCP_ACL "/usr/lib/sunw,rcp" 68*0Sstevel@tonic-gate /* see PSARC/1993/004/opinion */ 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate typedef struct _buf { 71*0Sstevel@tonic-gate int cnt; 72*0Sstevel@tonic-gate char *buf; 73*0Sstevel@tonic-gate } BUF; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate static char *cmd_sunw; 76*0Sstevel@tonic-gate static struct passwd *pwd; 77*0Sstevel@tonic-gate static int errs; 78*0Sstevel@tonic-gate static int pflag; 79*0Sstevel@tonic-gate static uid_t userid; 80*0Sstevel@tonic-gate static int rem; 81*0Sstevel@tonic-gate static int zflag; 82*0Sstevel@tonic-gate static int iamremote; 83*0Sstevel@tonic-gate static int iamrecursive; 84*0Sstevel@tonic-gate static int targetshouldbedirectory; 85*0Sstevel@tonic-gate static int aclflag; 86*0Sstevel@tonic-gate static int retval = 0; 87*0Sstevel@tonic-gate static int portnumber = 0; 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate static void lostconn(void); 90*0Sstevel@tonic-gate static char *search_char(unsigned char *, unsigned char); 91*0Sstevel@tonic-gate static char *removebrackets(char *); 92*0Sstevel@tonic-gate static char *colon(char *); 93*0Sstevel@tonic-gate static int response(void); 94*0Sstevel@tonic-gate static void usage(void); 95*0Sstevel@tonic-gate static void source(int, char **); 96*0Sstevel@tonic-gate static void sink(int, char **); 97*0Sstevel@tonic-gate static void toremote(char *, int, char **); 98*0Sstevel@tonic-gate static void tolocal(int, char **); 99*0Sstevel@tonic-gate static void verifydir(char *); 100*0Sstevel@tonic-gate static int okname(char *); 101*0Sstevel@tonic-gate static int susystem(char *); 102*0Sstevel@tonic-gate static void rsource(char *, struct stat *); 103*0Sstevel@tonic-gate static int sendacl(int); 104*0Sstevel@tonic-gate static int recvacl(int, int, int); 105*0Sstevel@tonic-gate static int zwrite(int, char *, int); 106*0Sstevel@tonic-gate static void zopen(int, int); 107*0Sstevel@tonic-gate static int zclose(int); 108*0Sstevel@tonic-gate static int notzero(char *, int); 109*0Sstevel@tonic-gate static BUF *allocbuf(BUF *, int, int); 110*0Sstevel@tonic-gate static void error(char *fmt, ...); 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate /* 113*0Sstevel@tonic-gate * As a 32 bit application, we can only transfer (2gb - 1) i.e 0x7FFFFFFF 114*0Sstevel@tonic-gate * bytes of data. We would like the size to be aligned to the nearest 115*0Sstevel@tonic-gate * MAXBOFFSET (8192) boundary for optimal performance. 116*0Sstevel@tonic-gate */ 117*0Sstevel@tonic-gate #define SENDFILE_SIZE 0x7FFFE000 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate #include <k5-int.h> 120*0Sstevel@tonic-gate #include <profile/prof_int.h> 121*0Sstevel@tonic-gate #include <com_err.h> 122*0Sstevel@tonic-gate #include <kcmd.h> 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate #define NULLBUF (BUF *) 0 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate static int sock; 127*0Sstevel@tonic-gate static char *cmd, *cmd_orig, *cmd_sunw_orig; 128*0Sstevel@tonic-gate static char *krb_realm = NULL; 129*0Sstevel@tonic-gate static char *krb_cache = NULL; 130*0Sstevel@tonic-gate static char *krb_config = NULL; 131*0Sstevel@tonic-gate static char des_inbuf[2 * RCP_BUFSIZE]; 132*0Sstevel@tonic-gate /* needs to be > largest read size */ 133*0Sstevel@tonic-gate static char des_outbuf[2 * RCP_BUFSIZE]; 134*0Sstevel@tonic-gate /* needs to be > largest write size */ 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf; 137*0Sstevel@tonic-gate static krb5_encrypt_block eblock; /* eblock for encrypt/decrypt */ 138*0Sstevel@tonic-gate static krb5_keyblock *session_key; /* static key for session */ 139*0Sstevel@tonic-gate static krb5_context bsd_context; 140*0Sstevel@tonic-gate static krb5_auth_context auth_context; 141*0Sstevel@tonic-gate static krb5_flags authopts; 142*0Sstevel@tonic-gate static krb5_error_code status; 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate static void try_normal_rcp(int, char **); 145*0Sstevel@tonic-gate static int init_service(int); 146*0Sstevel@tonic-gate static char **save_argv(int, char **); 147*0Sstevel@tonic-gate static void answer_auth(char *, char *); 148*0Sstevel@tonic-gate static int desrcpwrite(int, char *, int); 149*0Sstevel@tonic-gate static int desrcpread(int, char *, int); 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate /* 152*0Sstevel@tonic-gate * Not sure why these two don't have their own header file declarations, but 153*0Sstevel@tonic-gate * lint complains about absent declarations so place some here. Sigh. 154*0Sstevel@tonic-gate */ 155*0Sstevel@tonic-gate extern errcode_t profile_get_options_boolean(profile_t, char **, 156*0Sstevel@tonic-gate profile_options_boolean *); 157*0Sstevel@tonic-gate extern errcode_t profile_get_options_string(profile_t, char **, 158*0Sstevel@tonic-gate profile_option_strings *); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate static int krb5auth_flag = 0; /* Flag set, when KERBEROS is enabled */ 161*0Sstevel@tonic-gate static int encrypt_flag = 0; /* Flag set, when encryption is enabled */ 162*0Sstevel@tonic-gate static int encrypt_done = 0; /* Flag set, if "-x" is specified */ 163*0Sstevel@tonic-gate static enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */ 166*0Sstevel@tonic-gate static boolean_t rcmdoption_done = B_FALSE; 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate static profile_options_boolean option[] = { 169*0Sstevel@tonic-gate { "encrypt", &encrypt_flag, 0 }, 170*0Sstevel@tonic-gate { NULL, NULL, 0 } 171*0Sstevel@tonic-gate }; 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate static char *rcmdproto = NULL; 174*0Sstevel@tonic-gate static profile_option_strings rcmdversion[] = { 175*0Sstevel@tonic-gate { "rcmd_protocol", &rcmdproto, 0 }, 176*0Sstevel@tonic-gate { NULL, NULL, 0 } 177*0Sstevel@tonic-gate }; 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, "rcp", NULL }; 180*0Sstevel@tonic-gate static char *appdef[] = { "appdefaults", "rcp", NULL }; 181*0Sstevel@tonic-gate static char **prev_argv; 182*0Sstevel@tonic-gate static int prev_argc; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate int 185*0Sstevel@tonic-gate main(int argc, char *argv[]) 186*0Sstevel@tonic-gate { 187*0Sstevel@tonic-gate int ch, fflag, tflag; 188*0Sstevel@tonic-gate char *targ; 189*0Sstevel@tonic-gate size_t cmdsiz; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate if (strcmp(argv[0], RCP_ACL) == 0) 194*0Sstevel@tonic-gate aclflag = 1; 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate if (!(pwd = getpwuid(userid = getuid()))) { 197*0Sstevel@tonic-gate (void) fprintf(stderr, "rcp: unknown user %d.\n", 198*0Sstevel@tonic-gate (uint_t)userid); 199*0Sstevel@tonic-gate return (1); 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate fflag = tflag = 0; 203*0Sstevel@tonic-gate while ((ch = getopt(argc, argv, "axdfprtz:D:k:P:")) != EOF) { 204*0Sstevel@tonic-gate switch (ch) { 205*0Sstevel@tonic-gate case 'd': 206*0Sstevel@tonic-gate targetshouldbedirectory = 1; 207*0Sstevel@tonic-gate break; 208*0Sstevel@tonic-gate case 'f': /* "from" */ 209*0Sstevel@tonic-gate fflag = 1; 210*0Sstevel@tonic-gate if (aclflag) 211*0Sstevel@tonic-gate /* ok response */ 212*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 213*0Sstevel@tonic-gate break; 214*0Sstevel@tonic-gate case 'p': /* preserve access/mod times */ 215*0Sstevel@tonic-gate ++pflag; 216*0Sstevel@tonic-gate break; 217*0Sstevel@tonic-gate case 'r': 218*0Sstevel@tonic-gate ++iamrecursive; 219*0Sstevel@tonic-gate break; 220*0Sstevel@tonic-gate case 't': /* "to" */ 221*0Sstevel@tonic-gate tflag = 1; 222*0Sstevel@tonic-gate break; 223*0Sstevel@tonic-gate case 'x': 224*0Sstevel@tonic-gate if (!krb5_privacy_allowed()) { 225*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: " 226*0Sstevel@tonic-gate "Encryption not supported.\n")); 227*0Sstevel@tonic-gate return (1); 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate encrypt_flag++; 230*0Sstevel@tonic-gate krb5auth_flag++; 231*0Sstevel@tonic-gate encrypt_done++; 232*0Sstevel@tonic-gate break; 233*0Sstevel@tonic-gate case 'k': 234*0Sstevel@tonic-gate if ((krb_realm = (char *)strdup(optarg)) == NULL) { 235*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp:" 236*0Sstevel@tonic-gate " Cannot malloc.\n")); 237*0Sstevel@tonic-gate return (1); 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate krb5auth_flag++; 240*0Sstevel@tonic-gate break; 241*0Sstevel@tonic-gate case 'P': 242*0Sstevel@tonic-gate if (strncmp(optarg, "O", 1) == 0) { 243*0Sstevel@tonic-gate if (rcmdoption_done == B_TRUE) { 244*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: " 245*0Sstevel@tonic-gate "Only one of -PN and -PO " 246*0Sstevel@tonic-gate "allowed.\n")); 247*0Sstevel@tonic-gate usage(); 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate kcmd_proto = KCMD_OLD_PROTOCOL; 250*0Sstevel@tonic-gate rcmdoption_done = B_TRUE; 251*0Sstevel@tonic-gate } else if (strncmp(optarg, "N", 1) == 0) { 252*0Sstevel@tonic-gate if (rcmdoption_done == B_TRUE) { 253*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: " 254*0Sstevel@tonic-gate "Only one of -PN and -PO " 255*0Sstevel@tonic-gate "allowed.\n")); 256*0Sstevel@tonic-gate usage(); 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate kcmd_proto = KCMD_NEW_PROTOCOL; 259*0Sstevel@tonic-gate rcmdoption_done = B_TRUE; 260*0Sstevel@tonic-gate } else { 261*0Sstevel@tonic-gate usage(); 262*0Sstevel@tonic-gate } 263*0Sstevel@tonic-gate krb5auth_flag++; 264*0Sstevel@tonic-gate break; 265*0Sstevel@tonic-gate case 'a': 266*0Sstevel@tonic-gate krb5auth_flag++; 267*0Sstevel@tonic-gate break; 268*0Sstevel@tonic-gate #ifdef DEBUG 269*0Sstevel@tonic-gate case 'D': 270*0Sstevel@tonic-gate portnumber = htons(atoi(optarg)); 271*0Sstevel@tonic-gate krb5auth_flag++; 272*0Sstevel@tonic-gate break; 273*0Sstevel@tonic-gate #endif /* DEBUG */ 274*0Sstevel@tonic-gate case '?': 275*0Sstevel@tonic-gate default: 276*0Sstevel@tonic-gate usage(); 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate argc -= optind; 280*0Sstevel@tonic-gate argv += optind; 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate if (krb5auth_flag > 0) { 283*0Sstevel@tonic-gate status = krb5_init_context(&bsd_context); 284*0Sstevel@tonic-gate if (status) { 285*0Sstevel@tonic-gate com_err("rcp", status, 286*0Sstevel@tonic-gate gettext("while initializing krb5")); 287*0Sstevel@tonic-gate return (1); 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate /* 291*0Sstevel@tonic-gate * Set up buffers for desread and deswrite. 292*0Sstevel@tonic-gate */ 293*0Sstevel@tonic-gate desinbuf.data = des_inbuf; 294*0Sstevel@tonic-gate desoutbuf.data = des_outbuf; 295*0Sstevel@tonic-gate desinbuf.length = sizeof (des_inbuf); 296*0Sstevel@tonic-gate desoutbuf.length = sizeof (des_outbuf); 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate if (fflag || tflag) 300*0Sstevel@tonic-gate if (encrypt_flag > 0) 301*0Sstevel@tonic-gate (void) answer_auth(krb_config, krb_cache); 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate if (fflag) { 304*0Sstevel@tonic-gate iamremote = 1; 305*0Sstevel@tonic-gate (void) response(); 306*0Sstevel@tonic-gate (void) setuid(userid); 307*0Sstevel@tonic-gate source(argc, argv); 308*0Sstevel@tonic-gate return (errs); 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate if (tflag) { 312*0Sstevel@tonic-gate iamremote = 1; 313*0Sstevel@tonic-gate (void) setuid(userid); 314*0Sstevel@tonic-gate sink(argc, argv); 315*0Sstevel@tonic-gate return (errs); 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate if (argc < 2) 319*0Sstevel@tonic-gate usage(); 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate /* This will make "rcmd_af()" magically get the proper privilege */ 322*0Sstevel@tonic-gate if (__init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL) == -1) { 323*0Sstevel@tonic-gate (void) fprintf(stderr, "rcp: must be set-uid root\n"); 324*0Sstevel@tonic-gate exit(1); 325*0Sstevel@tonic-gate } 326*0Sstevel@tonic-gate 327*0Sstevel@tonic-gate if (krb5auth_flag > 0) { 328*0Sstevel@tonic-gate /* 329*0Sstevel@tonic-gate * Get our local realm to look up local realm options. 330*0Sstevel@tonic-gate */ 331*0Sstevel@tonic-gate status = krb5_get_default_realm(bsd_context, &realmdef[1]); 332*0Sstevel@tonic-gate if (status) { 333*0Sstevel@tonic-gate com_err("rcp", status, 334*0Sstevel@tonic-gate gettext("while getting default realm")); 335*0Sstevel@tonic-gate return (1); 336*0Sstevel@tonic-gate } 337*0Sstevel@tonic-gate /* 338*0Sstevel@tonic-gate * See if encryption should be done for this realm 339*0Sstevel@tonic-gate */ 340*0Sstevel@tonic-gate profile_get_options_boolean(bsd_context->profile, realmdef, 341*0Sstevel@tonic-gate option); 342*0Sstevel@tonic-gate /* 343*0Sstevel@tonic-gate * Check the appdefaults section 344*0Sstevel@tonic-gate */ 345*0Sstevel@tonic-gate profile_get_options_boolean(bsd_context->profile, appdef, 346*0Sstevel@tonic-gate option); 347*0Sstevel@tonic-gate profile_get_options_string(bsd_context->profile, appdef, 348*0Sstevel@tonic-gate rcmdversion); 349*0Sstevel@tonic-gate if ((encrypt_done > 0) || (encrypt_flag > 0)) { 350*0Sstevel@tonic-gate if (krb5_privacy_allowed() == TRUE) { 351*0Sstevel@tonic-gate encrypt_flag++; 352*0Sstevel@tonic-gate } else { 353*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Encryption" 354*0Sstevel@tonic-gate " not supported.\n")); 355*0Sstevel@tonic-gate return (1); 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate } 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate if ((rcmdoption_done == B_FALSE) && (rcmdproto != NULL)) { 360*0Sstevel@tonic-gate if (strncmp(rcmdproto, "rcmdv2", 6) == 0) { 361*0Sstevel@tonic-gate kcmd_proto = KCMD_NEW_PROTOCOL; 362*0Sstevel@tonic-gate } else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) { 363*0Sstevel@tonic-gate kcmd_proto = KCMD_OLD_PROTOCOL; 364*0Sstevel@tonic-gate } else { 365*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Unrecognized " 366*0Sstevel@tonic-gate "KCMD protocol (%s)"), rcmdproto); 367*0Sstevel@tonic-gate return (1); 368*0Sstevel@tonic-gate } 369*0Sstevel@tonic-gate } 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate if (argc > 2) 373*0Sstevel@tonic-gate targetshouldbedirectory = 1; 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate rem = -1; 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate if (portnumber == 0) { 378*0Sstevel@tonic-gate if (krb5auth_flag > 0) { 379*0Sstevel@tonic-gate retval = init_service(krb5auth_flag); 380*0Sstevel@tonic-gate if (!retval) { 381*0Sstevel@tonic-gate /* 382*0Sstevel@tonic-gate * Connecting to the kshell service failed, 383*0Sstevel@tonic-gate * fallback to normal rcp & reset KRB5 flags. 384*0Sstevel@tonic-gate */ 385*0Sstevel@tonic-gate krb5auth_flag = encrypt_flag = 0; 386*0Sstevel@tonic-gate encrypt_done = 0; 387*0Sstevel@tonic-gate (void) init_service(krb5auth_flag); 388*0Sstevel@tonic-gate } 389*0Sstevel@tonic-gate } 390*0Sstevel@tonic-gate else 391*0Sstevel@tonic-gate (void) init_service(krb5auth_flag); 392*0Sstevel@tonic-gate } 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate #ifdef DEBUG 395*0Sstevel@tonic-gate if (retval || krb5auth_flag) { 396*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Kerberized rcp session, " 397*0Sstevel@tonic-gate "port %d in use "), portnumber); 398*0Sstevel@tonic-gate if (kcmd_proto == KCMD_OLD_PROTOCOL) 399*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("[kcmd ver.1]\n")); 400*0Sstevel@tonic-gate else 401*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("[kcmd ver.2]\n")); 402*0Sstevel@tonic-gate } else { 403*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("Normal rcp session, port %d " 404*0Sstevel@tonic-gate "in use.\n"), portnumber); 405*0Sstevel@tonic-gate } 406*0Sstevel@tonic-gate #endif /* DEBUG */ 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate if (krb5auth_flag > 0) { 409*0Sstevel@tonic-gate /* 410*0Sstevel@tonic-gate * We calculate here a buffer size that can be used in the 411*0Sstevel@tonic-gate * allocation of the three buffers cmd, cmd_orig and 412*0Sstevel@tonic-gate * cmd_sunw_orig that are used to hold different incantations 413*0Sstevel@tonic-gate * of rcp. 414*0Sstevel@tonic-gate */ 415*0Sstevel@tonic-gate cmdsiz = MAX(sizeof ("-x rcp -r -p -d -k ") + 416*0Sstevel@tonic-gate strlen(krb_realm != NULL ? krb_realm : ""), 417*0Sstevel@tonic-gate sizeof (RCP_ACL " -r -p -z -d")); 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate if (((cmd = (char *)malloc(cmdsiz)) == NULL) || 420*0Sstevel@tonic-gate ((cmd_sunw_orig = (char *)malloc(cmdsiz)) == NULL) || 421*0Sstevel@tonic-gate ((cmd_orig = (char *)malloc(cmdsiz)) == NULL)) { 422*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot " 423*0Sstevel@tonic-gate "malloc.\n")); 424*0Sstevel@tonic-gate return (1); 425*0Sstevel@tonic-gate } 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate (void) snprintf(cmd, cmdsiz, "%srcp %s%s%s%s%s", 428*0Sstevel@tonic-gate encrypt_flag ? "-x " : "", 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate iamrecursive ? " -r" : "", pflag ? " -p" : "", 431*0Sstevel@tonic-gate targetshouldbedirectory ? " -d" : "", 432*0Sstevel@tonic-gate krb_realm != NULL ? " -k " : "", 433*0Sstevel@tonic-gate krb_realm != NULL ? krb_realm : ""); 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gate /* 436*0Sstevel@tonic-gate * We would use cmd-orig as the 'cmd-buffer' if kerberized 437*0Sstevel@tonic-gate * rcp fails, in which case we fallback to normal rcp. We also 438*0Sstevel@tonic-gate * save argc & argv for the same purpose 439*0Sstevel@tonic-gate */ 440*0Sstevel@tonic-gate (void) snprintf(cmd_orig, cmdsiz, "rcp%s%s%s%s", 441*0Sstevel@tonic-gate iamrecursive ? " -r" : "", 442*0Sstevel@tonic-gate pflag ? " -p" : "", 443*0Sstevel@tonic-gate zflag ? " -z" : "", 444*0Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gate (void) snprintf(cmd_sunw_orig, cmdsiz, "%s%s%s%s%s", RCP_ACL, 447*0Sstevel@tonic-gate iamrecursive ? " -r" : "", 448*0Sstevel@tonic-gate pflag ? " -p" : "", 449*0Sstevel@tonic-gate zflag ? " -z" : "", 450*0Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate prev_argc = argc; 453*0Sstevel@tonic-gate prev_argv = save_argv(argc, argv); 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gate } else { 456*0Sstevel@tonic-gate cmdsiz = sizeof ("rcp -r -p -z -d"); 457*0Sstevel@tonic-gate if (((cmd = (char *)malloc(cmdsiz)) == NULL)) { 458*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot " 459*0Sstevel@tonic-gate "malloc.\n")); 460*0Sstevel@tonic-gate return (1); 461*0Sstevel@tonic-gate } 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate (void) snprintf(cmd, cmdsiz, "rcp%s%s%s%s", 464*0Sstevel@tonic-gate iamrecursive ? " -r" : "", 465*0Sstevel@tonic-gate pflag ? " -p" : "", 466*0Sstevel@tonic-gate zflag ? " -z" : "", 467*0Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 468*0Sstevel@tonic-gate } 469*0Sstevel@tonic-gate 470*0Sstevel@tonic-gate cmdsiz = sizeof (RCP_ACL " -r -p -z -d"); 471*0Sstevel@tonic-gate if ((cmd_sunw = (char *)malloc(cmdsiz)) == NULL) { 472*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot malloc.\n")); 473*0Sstevel@tonic-gate return (1); 474*0Sstevel@tonic-gate } 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate (void) snprintf(cmd_sunw, cmdsiz, "%s%s%s%s%s", RCP_ACL, 477*0Sstevel@tonic-gate iamrecursive ? " -r" : "", 478*0Sstevel@tonic-gate pflag ? " -p" : "", 479*0Sstevel@tonic-gate zflag ? " -z" : "", 480*0Sstevel@tonic-gate targetshouldbedirectory ? " -d" : ""); 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate (void) signal(SIGPIPE, (void (*)(int))lostconn); 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gate if (targ = colon(argv[argc - 1])) 485*0Sstevel@tonic-gate toremote(targ, argc, argv); 486*0Sstevel@tonic-gate else { 487*0Sstevel@tonic-gate tolocal(argc, argv); 488*0Sstevel@tonic-gate if (targetshouldbedirectory) 489*0Sstevel@tonic-gate verifydir(argv[argc - 1]); 490*0Sstevel@tonic-gate } 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate return (errs > 0 ? EXIT_FAILURE : EXIT_SUCCESS); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate static void 497*0Sstevel@tonic-gate toremote(char *targ, int argc, char *argv[]) 498*0Sstevel@tonic-gate { 499*0Sstevel@tonic-gate int i; 500*0Sstevel@tonic-gate char *host, *src, *suser, *thost, *tuser; 501*0Sstevel@tonic-gate char resp; 502*0Sstevel@tonic-gate size_t buffersize; 503*0Sstevel@tonic-gate char bp[RCP_BUFSIZE]; 504*0Sstevel@tonic-gate krb5_creds *cred; 505*0Sstevel@tonic-gate buffersize = RCP_BUFSIZE; 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate *targ++ = 0; 508*0Sstevel@tonic-gate if (*targ == 0) 509*0Sstevel@tonic-gate targ = "."; 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate if (thost = search_char((unsigned char *)argv[argc - 1], '@')) { 512*0Sstevel@tonic-gate *thost++ = 0; 513*0Sstevel@tonic-gate tuser = argv[argc - 1]; 514*0Sstevel@tonic-gate if (*tuser == '\0') 515*0Sstevel@tonic-gate tuser = NULL; 516*0Sstevel@tonic-gate else if (!okname(tuser)) 517*0Sstevel@tonic-gate exit(1); 518*0Sstevel@tonic-gate } else { 519*0Sstevel@tonic-gate thost = argv[argc - 1]; 520*0Sstevel@tonic-gate tuser = NULL; 521*0Sstevel@tonic-gate } 522*0Sstevel@tonic-gate thost = removebrackets(thost); 523*0Sstevel@tonic-gate 524*0Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) { 525*0Sstevel@tonic-gate src = colon(argv[i]); 526*0Sstevel@tonic-gate if (src) { /* remote to remote */ 527*0Sstevel@tonic-gate *src++ = 0; 528*0Sstevel@tonic-gate if (*src == 0) 529*0Sstevel@tonic-gate src = "."; 530*0Sstevel@tonic-gate host = search_char((unsigned char *)argv[i], '@'); 531*0Sstevel@tonic-gate if (host) { 532*0Sstevel@tonic-gate *host++ = 0; 533*0Sstevel@tonic-gate host = removebrackets(host); 534*0Sstevel@tonic-gate suser = argv[i]; 535*0Sstevel@tonic-gate if (*suser == '\0') { 536*0Sstevel@tonic-gate suser = pwd->pw_name; 537*0Sstevel@tonic-gate } else if (!okname(suser)) { 538*0Sstevel@tonic-gate errs++; 539*0Sstevel@tonic-gate continue; 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, 542*0Sstevel@tonic-gate "%s %s -l %s -n %s %s '%s%s%s:%s'", 543*0Sstevel@tonic-gate _PATH_RSH, host, suser, cmd, src, 544*0Sstevel@tonic-gate tuser ? tuser : "", tuser ? "@" : "", 545*0Sstevel@tonic-gate thost, targ); 546*0Sstevel@tonic-gate } else { 547*0Sstevel@tonic-gate host = removebrackets(argv[i]); 548*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, 549*0Sstevel@tonic-gate "%s %s -n %s %s '%s%s%s:%s'", 550*0Sstevel@tonic-gate _PATH_RSH, host, cmd, src, 551*0Sstevel@tonic-gate tuser ? tuser : "", tuser ? "@" : "", 552*0Sstevel@tonic-gate thost, targ); 553*0Sstevel@tonic-gate } 554*0Sstevel@tonic-gate if (susystem(bp) == -1) 555*0Sstevel@tonic-gate errs++; 556*0Sstevel@tonic-gate } else { /* local to remote */ 557*0Sstevel@tonic-gate if (rem == -1) { 558*0Sstevel@tonic-gate host = thost; 559*0Sstevel@tonic-gate if (krb5auth_flag > 0) { 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, 562*0Sstevel@tonic-gate "%s -t %s", cmd, targ); 563*0Sstevel@tonic-gate authopts = AP_OPTS_MUTUAL_REQUIRED; 564*0Sstevel@tonic-gate status = kcmd(&sock, &host, 565*0Sstevel@tonic-gate portnumber, 566*0Sstevel@tonic-gate pwd->pw_name, 567*0Sstevel@tonic-gate tuser ? tuser : 568*0Sstevel@tonic-gate pwd->pw_name, 569*0Sstevel@tonic-gate bp, 570*0Sstevel@tonic-gate 0, 571*0Sstevel@tonic-gate "host", 572*0Sstevel@tonic-gate krb_realm, 573*0Sstevel@tonic-gate bsd_context, 574*0Sstevel@tonic-gate &auth_context, 575*0Sstevel@tonic-gate &cred, 576*0Sstevel@tonic-gate 0, /* No seq # */ 577*0Sstevel@tonic-gate 0, /* No server seq # */ 578*0Sstevel@tonic-gate authopts, 579*0Sstevel@tonic-gate 0, /* Not any port # */ 580*0Sstevel@tonic-gate &kcmd_proto); 581*0Sstevel@tonic-gate if (status) { 582*0Sstevel@tonic-gate /* 583*0Sstevel@tonic-gate * If new protocol requested, we dont 584*0Sstevel@tonic-gate * fallback to less secure ones. 585*0Sstevel@tonic-gate */ 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 588*0Sstevel@tonic-gate (void) fprintf(stderr, 589*0Sstevel@tonic-gate gettext("rcp: kcmdv2 " 590*0Sstevel@tonic-gate "to host %s failed - %s" 591*0Sstevel@tonic-gate "\nFallback to normal " 592*0Sstevel@tonic-gate "rcp denied."), host, 593*0Sstevel@tonic-gate error_message(status)); 594*0Sstevel@tonic-gate exit(1); 595*0Sstevel@tonic-gate } 596*0Sstevel@tonic-gate if (status != -1) { 597*0Sstevel@tonic-gate (void) fprintf(stderr, 598*0Sstevel@tonic-gate gettext("rcp: kcmd to host " 599*0Sstevel@tonic-gate "%s failed - %s,\n" 600*0Sstevel@tonic-gate "trying normal rcp...\n\n"), 601*0Sstevel@tonic-gate host, error_message(status)); 602*0Sstevel@tonic-gate } else { 603*0Sstevel@tonic-gate (void) fprintf(stderr, 604*0Sstevel@tonic-gate gettext("trying normal" 605*0Sstevel@tonic-gate " rcp...\n")); 606*0Sstevel@tonic-gate } 607*0Sstevel@tonic-gate /* 608*0Sstevel@tonic-gate * kcmd() failed, so we have to 609*0Sstevel@tonic-gate * fallback to normal rcp 610*0Sstevel@tonic-gate */ 611*0Sstevel@tonic-gate try_normal_rcp(prev_argc, prev_argv); 612*0Sstevel@tonic-gate } else { 613*0Sstevel@tonic-gate rem = sock; 614*0Sstevel@tonic-gate session_key = &cred->keyblock; 615*0Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 616*0Sstevel@tonic-gate /* CSTYLED */ 617*0Sstevel@tonic-gate status = krb5_auth_con_getlocalsubkey(bsd_context, auth_context, &session_key); 618*0Sstevel@tonic-gate if (status) { 619*0Sstevel@tonic-gate com_err("rcp", status, 620*0Sstevel@tonic-gate "determining " 621*0Sstevel@tonic-gate "subkey for " 622*0Sstevel@tonic-gate "session"); 623*0Sstevel@tonic-gate exit(1); 624*0Sstevel@tonic-gate } 625*0Sstevel@tonic-gate if (!session_key) { 626*0Sstevel@tonic-gate com_err("rcp", 0, 627*0Sstevel@tonic-gate "no subkey " 628*0Sstevel@tonic-gate "negotiated for" 629*0Sstevel@tonic-gate " connection"); 630*0Sstevel@tonic-gate exit(1); 631*0Sstevel@tonic-gate } 632*0Sstevel@tonic-gate } 633*0Sstevel@tonic-gate eblock.crypto_entry = 634*0Sstevel@tonic-gate session_key->enctype; 635*0Sstevel@tonic-gate eblock.key = 636*0Sstevel@tonic-gate (krb5_keyblock *)session_key; 637*0Sstevel@tonic-gate 638*0Sstevel@tonic-gate init_encrypt(encrypt_flag, 639*0Sstevel@tonic-gate bsd_context, kcmd_proto, 640*0Sstevel@tonic-gate &desinbuf, &desoutbuf, CLIENT, 641*0Sstevel@tonic-gate &eblock); 642*0Sstevel@tonic-gate if (encrypt_flag > 0) { 643*0Sstevel@tonic-gate char *s = gettext("This rcp " 644*0Sstevel@tonic-gate "session is using " 645*0Sstevel@tonic-gate "encryption for all " 646*0Sstevel@tonic-gate "data transmissions." 647*0Sstevel@tonic-gate "\r\n"); 648*0Sstevel@tonic-gate 649*0Sstevel@tonic-gate (void) write(2, s, strlen(s)); 650*0Sstevel@tonic-gate } 651*0Sstevel@tonic-gate } 652*0Sstevel@tonic-gate if (response() < 0) 653*0Sstevel@tonic-gate exit(1); 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate } 656*0Sstevel@tonic-gate else 657*0Sstevel@tonic-gate { 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate /* 660*0Sstevel@tonic-gate * ACL support: try to find out if the remote 661*0Sstevel@tonic-gate * site is running acl cognizant version of 662*0Sstevel@tonic-gate * rcp. A special binary name is used for this 663*0Sstevel@tonic-gate * purpose. 664*0Sstevel@tonic-gate */ 665*0Sstevel@tonic-gate aclflag = 1; 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, "%s -t %s", 668*0Sstevel@tonic-gate cmd_sunw, targ); 669*0Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, pwd->pw_name, 670*0Sstevel@tonic-gate tuser ? tuser : pwd->pw_name, 671*0Sstevel@tonic-gate bp, 0, AF_INET6); 672*0Sstevel@tonic-gate if (rem < 0) 673*0Sstevel@tonic-gate exit(1); 674*0Sstevel@tonic-gate 675*0Sstevel@tonic-gate /* 676*0Sstevel@tonic-gate * This is similar to routine response(). 677*0Sstevel@tonic-gate * If response is not ok, treat the other 678*0Sstevel@tonic-gate * side as non-acl rcp. 679*0Sstevel@tonic-gate */ 680*0Sstevel@tonic-gate if (read(rem, &resp, sizeof (resp)) 681*0Sstevel@tonic-gate != sizeof (resp)) 682*0Sstevel@tonic-gate lostconn(); 683*0Sstevel@tonic-gate if (resp != 0) { 684*0Sstevel@tonic-gate /* 685*0Sstevel@tonic-gate * Not OK: 686*0Sstevel@tonic-gate * The other side is running 687*0Sstevel@tonic-gate * non-acl rcp. Try again with 688*0Sstevel@tonic-gate * normal stuff 689*0Sstevel@tonic-gate */ 690*0Sstevel@tonic-gate aclflag = 0; 691*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, 692*0Sstevel@tonic-gate "%s -t %s", cmd, targ); 693*0Sstevel@tonic-gate (void) close(rem); 694*0Sstevel@tonic-gate host = thost; 695*0Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, 696*0Sstevel@tonic-gate pwd->pw_name, 697*0Sstevel@tonic-gate tuser ? tuser : 698*0Sstevel@tonic-gate pwd->pw_name, bp, 0, 699*0Sstevel@tonic-gate AF_INET6); 700*0Sstevel@tonic-gate if (rem < 0) 701*0Sstevel@tonic-gate exit(1); 702*0Sstevel@tonic-gate if (response() < 0) 703*0Sstevel@tonic-gate exit(1); 704*0Sstevel@tonic-gate } 705*0Sstevel@tonic-gate /* everything should be fine now */ 706*0Sstevel@tonic-gate (void) setuid(userid); 707*0Sstevel@tonic-gate 708*0Sstevel@tonic-gate } 709*0Sstevel@tonic-gate 710*0Sstevel@tonic-gate } 711*0Sstevel@tonic-gate source(1, argv + i); 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate } 714*0Sstevel@tonic-gate } 715*0Sstevel@tonic-gate 716*0Sstevel@tonic-gate static void 717*0Sstevel@tonic-gate tolocal(int argc, char *argv[]) 718*0Sstevel@tonic-gate { 719*0Sstevel@tonic-gate int i; 720*0Sstevel@tonic-gate char *host, *src, *suser, *lhost; 721*0Sstevel@tonic-gate char resp; 722*0Sstevel@tonic-gate size_t buffersize; 723*0Sstevel@tonic-gate char bp[RCP_BUFSIZE]; 724*0Sstevel@tonic-gate krb5_creds *cred; 725*0Sstevel@tonic-gate buffersize = RCP_BUFSIZE; 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) { 728*0Sstevel@tonic-gate if (!(src = colon(argv[i]))) { /* local to local */ 729*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, "%s%s%s%s %s %s", 730*0Sstevel@tonic-gate _PATH_CP, iamrecursive ? " -r" : "", 731*0Sstevel@tonic-gate pflag ? " -p" : "", 732*0Sstevel@tonic-gate zflag ? " -z" : "", 733*0Sstevel@tonic-gate argv[i], argv[argc - 1]); 734*0Sstevel@tonic-gate if (susystem(bp) == -1) 735*0Sstevel@tonic-gate errs++; 736*0Sstevel@tonic-gate continue; 737*0Sstevel@tonic-gate } 738*0Sstevel@tonic-gate *src++ = 0; 739*0Sstevel@tonic-gate if (*src == 0) 740*0Sstevel@tonic-gate src = "."; 741*0Sstevel@tonic-gate host = search_char((unsigned char *)argv[i], '@'); 742*0Sstevel@tonic-gate if (host) { 743*0Sstevel@tonic-gate *host++ = 0; 744*0Sstevel@tonic-gate suser = argv[i]; 745*0Sstevel@tonic-gate if (*suser == '\0') { 746*0Sstevel@tonic-gate suser = pwd->pw_name; 747*0Sstevel@tonic-gate } else if (!okname(suser)) { 748*0Sstevel@tonic-gate errs++; 749*0Sstevel@tonic-gate continue; 750*0Sstevel@tonic-gate } 751*0Sstevel@tonic-gate } else { 752*0Sstevel@tonic-gate host = argv[i]; 753*0Sstevel@tonic-gate suser = pwd->pw_name; 754*0Sstevel@tonic-gate } 755*0Sstevel@tonic-gate host = removebrackets(host); 756*0Sstevel@tonic-gate lhost = host; 757*0Sstevel@tonic-gate if (krb5auth_flag > 0) { 758*0Sstevel@tonic-gate 759*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, "%s -f %s", cmd, src); 760*0Sstevel@tonic-gate authopts = AP_OPTS_MUTUAL_REQUIRED; 761*0Sstevel@tonic-gate status = kcmd(&sock, &host, 762*0Sstevel@tonic-gate portnumber, 763*0Sstevel@tonic-gate pwd->pw_name, suser, 764*0Sstevel@tonic-gate bp, 765*0Sstevel@tonic-gate 0, /* &rfd2 */ 766*0Sstevel@tonic-gate "host", 767*0Sstevel@tonic-gate krb_realm, 768*0Sstevel@tonic-gate bsd_context, 769*0Sstevel@tonic-gate &auth_context, 770*0Sstevel@tonic-gate &cred, 771*0Sstevel@tonic-gate 0, /* No seq # */ 772*0Sstevel@tonic-gate 0, /* No server seq # */ 773*0Sstevel@tonic-gate authopts, 774*0Sstevel@tonic-gate 1, /* Not any port # */ 775*0Sstevel@tonic-gate &kcmd_proto); 776*0Sstevel@tonic-gate if (status) { 777*0Sstevel@tonic-gate /* 778*0Sstevel@tonic-gate * If new protocol requested, we dont 779*0Sstevel@tonic-gate * fallback to less secure ones. 780*0Sstevel@tonic-gate */ 781*0Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 782*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: kcmdv2 " 783*0Sstevel@tonic-gate "to host %s failed - %s\n" 784*0Sstevel@tonic-gate "Fallback to normal rcp denied."), 785*0Sstevel@tonic-gate host, error_message(status)); 786*0Sstevel@tonic-gate exit(1); 787*0Sstevel@tonic-gate } 788*0Sstevel@tonic-gate if (status != -1) { 789*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: kcmd " 790*0Sstevel@tonic-gate "to host %s failed - %s,\n" 791*0Sstevel@tonic-gate "trying normal rcp...\n\n"), 792*0Sstevel@tonic-gate host, error_message(status)); 793*0Sstevel@tonic-gate } else { 794*0Sstevel@tonic-gate (void) fprintf(stderr, 795*0Sstevel@tonic-gate gettext("trying normal rcp...\n")); 796*0Sstevel@tonic-gate } 797*0Sstevel@tonic-gate /* 798*0Sstevel@tonic-gate * kcmd() failed, so we have to 799*0Sstevel@tonic-gate * fallback to normal rcp 800*0Sstevel@tonic-gate */ 801*0Sstevel@tonic-gate try_normal_rcp(prev_argc, prev_argv); 802*0Sstevel@tonic-gate } else { 803*0Sstevel@tonic-gate rem = sock; 804*0Sstevel@tonic-gate session_key = &cred->keyblock; 805*0Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) { 806*0Sstevel@tonic-gate status = krb5_auth_con_getlocalsubkey( 807*0Sstevel@tonic-gate bsd_context, auth_context, 808*0Sstevel@tonic-gate &session_key); 809*0Sstevel@tonic-gate if (status) { 810*0Sstevel@tonic-gate com_err("rcp", status, "determining " 811*0Sstevel@tonic-gate "subkey for session"); 812*0Sstevel@tonic-gate exit(1); 813*0Sstevel@tonic-gate } 814*0Sstevel@tonic-gate if (!session_key) { 815*0Sstevel@tonic-gate com_err("rcp", 0, "no subkey negotiated" 816*0Sstevel@tonic-gate " for connection"); 817*0Sstevel@tonic-gate exit(1); 818*0Sstevel@tonic-gate } 819*0Sstevel@tonic-gate } 820*0Sstevel@tonic-gate eblock.crypto_entry = session_key->enctype; 821*0Sstevel@tonic-gate eblock.key = (krb5_keyblock *)session_key; 822*0Sstevel@tonic-gate 823*0Sstevel@tonic-gate init_encrypt(encrypt_flag, bsd_context, kcmd_proto, 824*0Sstevel@tonic-gate &desinbuf, &desoutbuf, CLIENT, 825*0Sstevel@tonic-gate &eblock); 826*0Sstevel@tonic-gate if (encrypt_flag > 0) { 827*0Sstevel@tonic-gate char *s = gettext("This rcp " 828*0Sstevel@tonic-gate "session is using DES " 829*0Sstevel@tonic-gate "encryption for all " 830*0Sstevel@tonic-gate "data transmissions." 831*0Sstevel@tonic-gate "\r\n"); 832*0Sstevel@tonic-gate 833*0Sstevel@tonic-gate (void) write(2, s, strlen(s)); 834*0Sstevel@tonic-gate } 835*0Sstevel@tonic-gate } 836*0Sstevel@tonic-gate 837*0Sstevel@tonic-gate } 838*0Sstevel@tonic-gate else 839*0Sstevel@tonic-gate { 840*0Sstevel@tonic-gate 841*0Sstevel@tonic-gate /* 842*0Sstevel@tonic-gate * ACL support: try to find out if the remote site is 843*0Sstevel@tonic-gate * running acl cognizant version of rcp. 844*0Sstevel@tonic-gate */ 845*0Sstevel@tonic-gate aclflag = 1; 846*0Sstevel@tonic-gate 847*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, "%s -f %s", cmd_sunw, src); 848*0Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, pwd->pw_name, suser, 849*0Sstevel@tonic-gate bp, 0, AF_INET6); 850*0Sstevel@tonic-gate 851*0Sstevel@tonic-gate if (rem < 0) { 852*0Sstevel@tonic-gate ++errs; 853*0Sstevel@tonic-gate continue; 854*0Sstevel@tonic-gate } 855*0Sstevel@tonic-gate 856*0Sstevel@tonic-gate /* 857*0Sstevel@tonic-gate * The remote system is supposed to send an ok response. 858*0Sstevel@tonic-gate * If there are any data other than "ok", it must be error 859*0Sstevel@tonic-gate * messages from the remote system. We can assume the 860*0Sstevel@tonic-gate * remote system is running non-acl version rcp. 861*0Sstevel@tonic-gate */ 862*0Sstevel@tonic-gate if (read(rem, &resp, sizeof (resp)) != sizeof (resp)) 863*0Sstevel@tonic-gate lostconn(); 864*0Sstevel@tonic-gate if (resp != 0) { 865*0Sstevel@tonic-gate /* 866*0Sstevel@tonic-gate * NOT ok: 867*0Sstevel@tonic-gate * The other side is running non-acl rcp. 868*0Sstevel@tonic-gate * Try again with normal stuff 869*0Sstevel@tonic-gate */ 870*0Sstevel@tonic-gate aclflag = 0; 871*0Sstevel@tonic-gate (void) snprintf(bp, buffersize, "%s -f %s", cmd, src); 872*0Sstevel@tonic-gate (void) close(rem); 873*0Sstevel@tonic-gate host = lhost; 874*0Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, pwd->pw_name, 875*0Sstevel@tonic-gate suser, bp, 0, AF_INET6); 876*0Sstevel@tonic-gate if (rem < 0) { 877*0Sstevel@tonic-gate ++errs; 878*0Sstevel@tonic-gate continue; 879*0Sstevel@tonic-gate } 880*0Sstevel@tonic-gate } 881*0Sstevel@tonic-gate } 882*0Sstevel@tonic-gate 883*0Sstevel@tonic-gate sink(1, argv + argc - 1); 884*0Sstevel@tonic-gate 885*0Sstevel@tonic-gate (void) close(rem); 886*0Sstevel@tonic-gate rem = -1; 887*0Sstevel@tonic-gate } 888*0Sstevel@tonic-gate } 889*0Sstevel@tonic-gate 890*0Sstevel@tonic-gate 891*0Sstevel@tonic-gate static void 892*0Sstevel@tonic-gate verifydir(char *cp) 893*0Sstevel@tonic-gate { 894*0Sstevel@tonic-gate struct stat stb; 895*0Sstevel@tonic-gate 896*0Sstevel@tonic-gate if (stat(cp, &stb) >= 0) { 897*0Sstevel@tonic-gate if ((stb.st_mode & S_IFMT) == S_IFDIR) 898*0Sstevel@tonic-gate return; 899*0Sstevel@tonic-gate errno = ENOTDIR; 900*0Sstevel@tonic-gate } 901*0Sstevel@tonic-gate error("rcp: %s: %s.\n", cp, strerror(errno)); 902*0Sstevel@tonic-gate exit(1); 903*0Sstevel@tonic-gate } 904*0Sstevel@tonic-gate 905*0Sstevel@tonic-gate static char * 906*0Sstevel@tonic-gate colon(char *cp) 907*0Sstevel@tonic-gate { 908*0Sstevel@tonic-gate boolean_t is_bracket_open = B_FALSE; 909*0Sstevel@tonic-gate 910*0Sstevel@tonic-gate for (; *cp; ++cp) { 911*0Sstevel@tonic-gate if (*cp == '[') 912*0Sstevel@tonic-gate is_bracket_open = B_TRUE; 913*0Sstevel@tonic-gate else if (*cp == ']') 914*0Sstevel@tonic-gate is_bracket_open = B_FALSE; 915*0Sstevel@tonic-gate else if (*cp == ':' && !is_bracket_open) 916*0Sstevel@tonic-gate return (cp); 917*0Sstevel@tonic-gate else if (*cp == '/') 918*0Sstevel@tonic-gate return (0); 919*0Sstevel@tonic-gate } 920*0Sstevel@tonic-gate return (0); 921*0Sstevel@tonic-gate } 922*0Sstevel@tonic-gate 923*0Sstevel@tonic-gate static int 924*0Sstevel@tonic-gate okname(char *cp0) 925*0Sstevel@tonic-gate { 926*0Sstevel@tonic-gate register char *cp = cp0; 927*0Sstevel@tonic-gate register int c; 928*0Sstevel@tonic-gate 929*0Sstevel@tonic-gate do { 930*0Sstevel@tonic-gate c = *cp; 931*0Sstevel@tonic-gate if (c & 0200) 932*0Sstevel@tonic-gate goto bad; 933*0Sstevel@tonic-gate if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') 934*0Sstevel@tonic-gate goto bad; 935*0Sstevel@tonic-gate } while (*++cp); 936*0Sstevel@tonic-gate return (1); 937*0Sstevel@tonic-gate bad: 938*0Sstevel@tonic-gate (void) fprintf(stderr, "rcp: invalid user name %s\n", cp0); 939*0Sstevel@tonic-gate return (0); 940*0Sstevel@tonic-gate } 941*0Sstevel@tonic-gate 942*0Sstevel@tonic-gate 943*0Sstevel@tonic-gate static char * 944*0Sstevel@tonic-gate removebrackets(char *str) 945*0Sstevel@tonic-gate { 946*0Sstevel@tonic-gate char *newstr = str; 947*0Sstevel@tonic-gate 948*0Sstevel@tonic-gate if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) { 949*0Sstevel@tonic-gate newstr = str + 1; 950*0Sstevel@tonic-gate str[strlen(str) - 1] = '\0'; 951*0Sstevel@tonic-gate } 952*0Sstevel@tonic-gate return (newstr); 953*0Sstevel@tonic-gate } 954*0Sstevel@tonic-gate 955*0Sstevel@tonic-gate static int 956*0Sstevel@tonic-gate susystem(char *s) 957*0Sstevel@tonic-gate { 958*0Sstevel@tonic-gate int status, pid, w; 959*0Sstevel@tonic-gate register void (*istat)(), (*qstat)(); 960*0Sstevel@tonic-gate int pfds[2]; 961*0Sstevel@tonic-gate char buf[BUFSIZ]; 962*0Sstevel@tonic-gate int cnt; 963*0Sstevel@tonic-gate boolean_t seen_stderr_traffic; 964*0Sstevel@tonic-gate 965*0Sstevel@tonic-gate /* 966*0Sstevel@tonic-gate * Due to the fact that rcp uses rsh to copy between 2 remote 967*0Sstevel@tonic-gate * machines, rsh doesn't return the exit status of the remote 968*0Sstevel@tonic-gate * command, and we can't modify the rcmd protocol used by rsh 969*0Sstevel@tonic-gate * (for interoperability reasons) we use the hack of using any 970*0Sstevel@tonic-gate * output on stderr as indication that an error occurred and 971*0Sstevel@tonic-gate * that we should return a non-zero error code. 972*0Sstevel@tonic-gate */ 973*0Sstevel@tonic-gate 974*0Sstevel@tonic-gate if (pipe(pfds) == -1) { 975*0Sstevel@tonic-gate (void) fprintf(stderr, "Couldn't create pipe: %s\n", 976*0Sstevel@tonic-gate strerror(errno)); 977*0Sstevel@tonic-gate return (-1); 978*0Sstevel@tonic-gate } 979*0Sstevel@tonic-gate 980*0Sstevel@tonic-gate if ((pid = vfork()) < 0) { 981*0Sstevel@tonic-gate (void) close(pfds[0]); 982*0Sstevel@tonic-gate (void) close(pfds[1]); 983*0Sstevel@tonic-gate (void) fprintf(stderr, "Couldn't fork child process: %s\n", 984*0Sstevel@tonic-gate strerror(errno)); 985*0Sstevel@tonic-gate return (-1); 986*0Sstevel@tonic-gate } else if (pid == 0) { 987*0Sstevel@tonic-gate /* 988*0Sstevel@tonic-gate * Child. 989*0Sstevel@tonic-gate */ 990*0Sstevel@tonic-gate (void) close(pfds[0]); 991*0Sstevel@tonic-gate /* 992*0Sstevel@tonic-gate * Send stderr messages down the pipe so that we can detect 993*0Sstevel@tonic-gate * them in the parent process. 994*0Sstevel@tonic-gate */ 995*0Sstevel@tonic-gate if (pfds[1] != STDERR_FILENO) { 996*0Sstevel@tonic-gate (void) dup2(pfds[1], STDERR_FILENO); 997*0Sstevel@tonic-gate (void) close(pfds[1]); 998*0Sstevel@tonic-gate } 999*0Sstevel@tonic-gate /* 1000*0Sstevel@tonic-gate * This shell does not inherit the additional privilege 1001*0Sstevel@tonic-gate * we have in our Permitted set. 1002*0Sstevel@tonic-gate */ 1003*0Sstevel@tonic-gate (void) execl(_PATH_BSHELL, "sh", "-c", s, (char *)0); 1004*0Sstevel@tonic-gate _exit(127); 1005*0Sstevel@tonic-gate } 1006*0Sstevel@tonic-gate /* 1007*0Sstevel@tonic-gate * Parent. 1008*0Sstevel@tonic-gate */ 1009*0Sstevel@tonic-gate istat = signal(SIGINT, SIG_IGN); 1010*0Sstevel@tonic-gate qstat = signal(SIGQUIT, SIG_IGN); 1011*0Sstevel@tonic-gate 1012*0Sstevel@tonic-gate (void) close(pfds[1]); 1013*0Sstevel@tonic-gate seen_stderr_traffic = B_FALSE; 1014*0Sstevel@tonic-gate while ((cnt = read(pfds[0], buf, sizeof (buf))) > 0) { 1015*0Sstevel@tonic-gate /* 1016*0Sstevel@tonic-gate * If any data is read from the pipe the child process 1017*0Sstevel@tonic-gate * has output something on stderr so we set the boolean 1018*0Sstevel@tonic-gate * 'seen_stderr_traffic' to true, which will cause the 1019*0Sstevel@tonic-gate * function to return -1. 1020*0Sstevel@tonic-gate */ 1021*0Sstevel@tonic-gate (void) write(STDERR_FILENO, buf, cnt); 1022*0Sstevel@tonic-gate seen_stderr_traffic = B_TRUE; 1023*0Sstevel@tonic-gate } 1024*0Sstevel@tonic-gate (void) close(pfds[0]); 1025*0Sstevel@tonic-gate while ((w = wait(&status)) != pid && w != -1) 1026*0Sstevel@tonic-gate ; 1027*0Sstevel@tonic-gate if (w == -1) 1028*0Sstevel@tonic-gate status = -1; 1029*0Sstevel@tonic-gate 1030*0Sstevel@tonic-gate (void) signal(SIGINT, istat); 1031*0Sstevel@tonic-gate (void) signal(SIGQUIT, qstat); 1032*0Sstevel@tonic-gate 1033*0Sstevel@tonic-gate return (seen_stderr_traffic ? -1 : status); 1034*0Sstevel@tonic-gate } 1035*0Sstevel@tonic-gate 1036*0Sstevel@tonic-gate static void 1037*0Sstevel@tonic-gate source(int argc, char *argv[]) 1038*0Sstevel@tonic-gate { 1039*0Sstevel@tonic-gate struct stat stb; 1040*0Sstevel@tonic-gate static BUF buffer; 1041*0Sstevel@tonic-gate BUF *bp; 1042*0Sstevel@tonic-gate int x, readerr, f, amt; 1043*0Sstevel@tonic-gate char *last, *name, buf[RCP_BUFSIZE]; 1044*0Sstevel@tonic-gate off_t off, size, i; 1045*0Sstevel@tonic-gate ssize_t cnt; 1046*0Sstevel@tonic-gate 1047*0Sstevel@tonic-gate for (x = 0; x < argc; x++) { 1048*0Sstevel@tonic-gate name = argv[x]; 1049*0Sstevel@tonic-gate if ((f = open(name, O_RDONLY, 0)) < 0) { 1050*0Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno)); 1051*0Sstevel@tonic-gate continue; 1052*0Sstevel@tonic-gate } 1053*0Sstevel@tonic-gate if (fstat(f, &stb) < 0) 1054*0Sstevel@tonic-gate goto notreg; 1055*0Sstevel@tonic-gate switch (stb.st_mode&S_IFMT) { 1056*0Sstevel@tonic-gate 1057*0Sstevel@tonic-gate case S_IFREG: 1058*0Sstevel@tonic-gate break; 1059*0Sstevel@tonic-gate 1060*0Sstevel@tonic-gate case S_IFDIR: 1061*0Sstevel@tonic-gate if (iamrecursive) { 1062*0Sstevel@tonic-gate (void) close(f); 1063*0Sstevel@tonic-gate rsource(name, &stb); 1064*0Sstevel@tonic-gate continue; 1065*0Sstevel@tonic-gate } 1066*0Sstevel@tonic-gate /* FALLTHROUGH */ 1067*0Sstevel@tonic-gate default: 1068*0Sstevel@tonic-gate notreg: 1069*0Sstevel@tonic-gate (void) close(f); 1070*0Sstevel@tonic-gate error("rcp: %s: not a plain file\n", name); 1071*0Sstevel@tonic-gate continue; 1072*0Sstevel@tonic-gate } 1073*0Sstevel@tonic-gate last = rindex(name, '/'); 1074*0Sstevel@tonic-gate if (last == 0) 1075*0Sstevel@tonic-gate last = name; 1076*0Sstevel@tonic-gate else 1077*0Sstevel@tonic-gate last++; 1078*0Sstevel@tonic-gate if (pflag) { 1079*0Sstevel@tonic-gate time_t mtime, atime; 1080*0Sstevel@tonic-gate time_t now; 1081*0Sstevel@tonic-gate 1082*0Sstevel@tonic-gate /* 1083*0Sstevel@tonic-gate * Make it compatible with possible future 1084*0Sstevel@tonic-gate * versions expecting microseconds. 1085*0Sstevel@tonic-gate */ 1086*0Sstevel@tonic-gate mtime = stb.st_mtime; 1087*0Sstevel@tonic-gate atime = stb.st_atime; 1088*0Sstevel@tonic-gate 1089*0Sstevel@tonic-gate if ((mtime < 0) || (atime < 0)) { 1090*0Sstevel@tonic-gate now = time(NULL); 1091*0Sstevel@tonic-gate 1092*0Sstevel@tonic-gate if (mtime < 0) { 1093*0Sstevel@tonic-gate mtime = now; 1094*0Sstevel@tonic-gate error("negative modification time on " 1095*0Sstevel@tonic-gate "%s; not preserving\n", name); 1096*0Sstevel@tonic-gate } 1097*0Sstevel@tonic-gate if (atime < 0) { 1098*0Sstevel@tonic-gate atime = now; 1099*0Sstevel@tonic-gate error("negative access time on " 1100*0Sstevel@tonic-gate "%s; not preserving\n", name); 1101*0Sstevel@tonic-gate } 1102*0Sstevel@tonic-gate } 1103*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "T%ld 0 %ld 0\n", 1104*0Sstevel@tonic-gate mtime, atime); 1105*0Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 1106*0Sstevel@tonic-gate if (response() < 0) { 1107*0Sstevel@tonic-gate (void) close(f); 1108*0Sstevel@tonic-gate continue; 1109*0Sstevel@tonic-gate } 1110*0Sstevel@tonic-gate } 1111*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "C%04o %lld %s\n", 1112*0Sstevel@tonic-gate (uint_t)(stb.st_mode & 07777), (longlong_t)stb.st_size, 1113*0Sstevel@tonic-gate last); 1114*0Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 1115*0Sstevel@tonic-gate if (response() < 0) { 1116*0Sstevel@tonic-gate (void) close(f); 1117*0Sstevel@tonic-gate continue; 1118*0Sstevel@tonic-gate } 1119*0Sstevel@tonic-gate 1120*0Sstevel@tonic-gate /* ACL support: send */ 1121*0Sstevel@tonic-gate if (aclflag) { 1122*0Sstevel@tonic-gate /* get acl from f and send it over */ 1123*0Sstevel@tonic-gate if (sendacl(f) == ACL_FAIL) { 1124*0Sstevel@tonic-gate (void) close(f); 1125*0Sstevel@tonic-gate continue; 1126*0Sstevel@tonic-gate } 1127*0Sstevel@tonic-gate } 1128*0Sstevel@tonic-gate if ((krb5auth_flag > 0) || (iamremote == 1)) { 1129*0Sstevel@tonic-gate bp = allocbuf(&buffer, f, RCP_BUFSIZE); 1130*0Sstevel@tonic-gate if (bp == NULLBUF) { 1131*0Sstevel@tonic-gate (void) close(f); 1132*0Sstevel@tonic-gate continue; 1133*0Sstevel@tonic-gate } 1134*0Sstevel@tonic-gate readerr = 0; 1135*0Sstevel@tonic-gate for (i = 0; i < stb.st_size; i += bp->cnt) { 1136*0Sstevel@tonic-gate amt = bp->cnt; 1137*0Sstevel@tonic-gate if (i + amt > stb.st_size) 1138*0Sstevel@tonic-gate amt = stb.st_size - i; 1139*0Sstevel@tonic-gate if (readerr == 0 && 1140*0Sstevel@tonic-gate read(f, bp->buf, amt) != amt) 1141*0Sstevel@tonic-gate readerr = errno; 1142*0Sstevel@tonic-gate (void) desrcpwrite(rem, bp->buf, amt); 1143*0Sstevel@tonic-gate } 1144*0Sstevel@tonic-gate (void) close(f); 1145*0Sstevel@tonic-gate if (readerr == 0) 1146*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 1147*0Sstevel@tonic-gate else 1148*0Sstevel@tonic-gate error("rcp: %s: %s\n", name, 1149*0Sstevel@tonic-gate error_message(readerr)); 1150*0Sstevel@tonic-gate } else { 1151*0Sstevel@tonic-gate cnt = off = 0; 1152*0Sstevel@tonic-gate size = stb.st_size; 1153*0Sstevel@tonic-gate while (size != 0) { 1154*0Sstevel@tonic-gate amt = MIN(size, SENDFILE_SIZE); 1155*0Sstevel@tonic-gate cnt = sendfile(rem, f, &off, amt); 1156*0Sstevel@tonic-gate if (cnt == -1) 1157*0Sstevel@tonic-gate break; 1158*0Sstevel@tonic-gate size -= cnt; 1159*0Sstevel@tonic-gate } 1160*0Sstevel@tonic-gate if (cnt == -1) { 1161*0Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno)); 1162*0Sstevel@tonic-gate } else { 1163*0Sstevel@tonic-gate (void) write(rem, "", 1); 1164*0Sstevel@tonic-gate } 1165*0Sstevel@tonic-gate (void) close(f); 1166*0Sstevel@tonic-gate } 1167*0Sstevel@tonic-gate (void) response(); 1168*0Sstevel@tonic-gate } 1169*0Sstevel@tonic-gate } 1170*0Sstevel@tonic-gate 1171*0Sstevel@tonic-gate 1172*0Sstevel@tonic-gate static void 1173*0Sstevel@tonic-gate rsource(char *name, struct stat *statp) 1174*0Sstevel@tonic-gate { 1175*0Sstevel@tonic-gate DIR *d; 1176*0Sstevel@tonic-gate struct dirent *dp; 1177*0Sstevel@tonic-gate char *last, *vect[1]; 1178*0Sstevel@tonic-gate char path[MAXPATHLEN]; 1179*0Sstevel@tonic-gate 1180*0Sstevel@tonic-gate if (!(d = opendir(name))) { 1181*0Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno)); 1182*0Sstevel@tonic-gate return; 1183*0Sstevel@tonic-gate } 1184*0Sstevel@tonic-gate last = rindex(name, '/'); 1185*0Sstevel@tonic-gate if (last == 0) 1186*0Sstevel@tonic-gate last = name; 1187*0Sstevel@tonic-gate else 1188*0Sstevel@tonic-gate last++; 1189*0Sstevel@tonic-gate if (pflag) { 1190*0Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "T%ld 0 %ld 0\n", 1191*0Sstevel@tonic-gate statp->st_mtime, statp->st_atime); 1192*0Sstevel@tonic-gate (void) desrcpwrite(rem, path, strlen(path)); 1193*0Sstevel@tonic-gate if (response() < 0) { 1194*0Sstevel@tonic-gate (void) closedir(d); 1195*0Sstevel@tonic-gate return; 1196*0Sstevel@tonic-gate } 1197*0Sstevel@tonic-gate } 1198*0Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "D%04o %d %s\n", 1199*0Sstevel@tonic-gate (uint_t)(statp->st_mode & 07777), 0, last); 1200*0Sstevel@tonic-gate (void) desrcpwrite(rem, path, strlen(path)); 1201*0Sstevel@tonic-gate 1202*0Sstevel@tonic-gate /* acl support for directory */ 1203*0Sstevel@tonic-gate if (aclflag) { 1204*0Sstevel@tonic-gate /* get acl from f and send it over */ 1205*0Sstevel@tonic-gate if (sendacl(d->dd_fd) == ACL_FAIL) { 1206*0Sstevel@tonic-gate (void) closedir(d); 1207*0Sstevel@tonic-gate return; 1208*0Sstevel@tonic-gate } 1209*0Sstevel@tonic-gate } 1210*0Sstevel@tonic-gate 1211*0Sstevel@tonic-gate if (response() < 0) { 1212*0Sstevel@tonic-gate (void) closedir(d); 1213*0Sstevel@tonic-gate return; 1214*0Sstevel@tonic-gate } 1215*0Sstevel@tonic-gate 1216*0Sstevel@tonic-gate while (dp = readdir(d)) { 1217*0Sstevel@tonic-gate if (dp->d_ino == 0) 1218*0Sstevel@tonic-gate continue; 1219*0Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 1220*0Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 1221*0Sstevel@tonic-gate continue; 1222*0Sstevel@tonic-gate if ((uint_t)strlen(name) + 1 + strlen(dp->d_name) >= 1223*0Sstevel@tonic-gate MAXPATHLEN - 1) { 1224*0Sstevel@tonic-gate error("%s/%s: name too long.\n", name, dp->d_name); 1225*0Sstevel@tonic-gate continue; 1226*0Sstevel@tonic-gate } 1227*0Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s/%s", 1228*0Sstevel@tonic-gate name, dp->d_name); 1229*0Sstevel@tonic-gate vect[0] = path; 1230*0Sstevel@tonic-gate source(1, vect); 1231*0Sstevel@tonic-gate } 1232*0Sstevel@tonic-gate (void) closedir(d); 1233*0Sstevel@tonic-gate (void) desrcpwrite(rem, "E\n", 2); 1234*0Sstevel@tonic-gate (void) response(); 1235*0Sstevel@tonic-gate } 1236*0Sstevel@tonic-gate 1237*0Sstevel@tonic-gate static int 1238*0Sstevel@tonic-gate response(void) 1239*0Sstevel@tonic-gate { 1240*0Sstevel@tonic-gate register char *cp; 1241*0Sstevel@tonic-gate char ch, resp, rbuf[RCP_BUFSIZE]; 1242*0Sstevel@tonic-gate 1243*0Sstevel@tonic-gate if (desrcpread(rem, &resp, 1) != 1) 1244*0Sstevel@tonic-gate lostconn(); 1245*0Sstevel@tonic-gate cp = rbuf; 1246*0Sstevel@tonic-gate switch (resp) { 1247*0Sstevel@tonic-gate case 0: /* ok */ 1248*0Sstevel@tonic-gate return (0); 1249*0Sstevel@tonic-gate default: 1250*0Sstevel@tonic-gate *cp++ = resp; 1251*0Sstevel@tonic-gate /* FALLTHROUGH */ 1252*0Sstevel@tonic-gate case 1: /* error, followed by err msg */ 1253*0Sstevel@tonic-gate case 2: /* fatal error, "" */ 1254*0Sstevel@tonic-gate do { 1255*0Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) 1256*0Sstevel@tonic-gate lostconn(); 1257*0Sstevel@tonic-gate *cp++ = ch; 1258*0Sstevel@tonic-gate } while (cp < &rbuf[RCP_BUFSIZE] && ch != '\n'); 1259*0Sstevel@tonic-gate 1260*0Sstevel@tonic-gate if (!iamremote) 1261*0Sstevel@tonic-gate (void) write(STDERR_FILENO, rbuf, cp - rbuf); 1262*0Sstevel@tonic-gate ++errs; 1263*0Sstevel@tonic-gate if (resp == 1) 1264*0Sstevel@tonic-gate return (-1); 1265*0Sstevel@tonic-gate exit(1); 1266*0Sstevel@tonic-gate } 1267*0Sstevel@tonic-gate /*NOTREACHED*/ 1268*0Sstevel@tonic-gate } 1269*0Sstevel@tonic-gate 1270*0Sstevel@tonic-gate static void 1271*0Sstevel@tonic-gate lostconn(void) 1272*0Sstevel@tonic-gate { 1273*0Sstevel@tonic-gate if (!iamremote) 1274*0Sstevel@tonic-gate (void) fprintf(stderr, "rcp: lost connection\n"); 1275*0Sstevel@tonic-gate exit(1); 1276*0Sstevel@tonic-gate } 1277*0Sstevel@tonic-gate 1278*0Sstevel@tonic-gate 1279*0Sstevel@tonic-gate static void 1280*0Sstevel@tonic-gate sink(int argc, char *argv[]) 1281*0Sstevel@tonic-gate { 1282*0Sstevel@tonic-gate char *cp; 1283*0Sstevel@tonic-gate static BUF buffer; 1284*0Sstevel@tonic-gate struct stat stb; 1285*0Sstevel@tonic-gate struct timeval tv[2]; 1286*0Sstevel@tonic-gate BUF *bp; 1287*0Sstevel@tonic-gate off_t i, j; 1288*0Sstevel@tonic-gate char ch, *targ, *why; 1289*0Sstevel@tonic-gate int amt, count, exists, first, mask, mode; 1290*0Sstevel@tonic-gate off_t size; 1291*0Sstevel@tonic-gate int ofd, setimes, targisdir, wrerr; 1292*0Sstevel@tonic-gate char *np, *vect[1], buf[RCP_BUFSIZE]; 1293*0Sstevel@tonic-gate char *namebuf = NULL; 1294*0Sstevel@tonic-gate size_t namebuf_sz = 0; 1295*0Sstevel@tonic-gate size_t need; 1296*0Sstevel@tonic-gate 1297*0Sstevel@tonic-gate #define atime tv[0] 1298*0Sstevel@tonic-gate #define mtime tv[1] 1299*0Sstevel@tonic-gate #define SCREWUP(str) { why = str; goto screwup; } 1300*0Sstevel@tonic-gate 1301*0Sstevel@tonic-gate setimes = targisdir = 0; 1302*0Sstevel@tonic-gate mask = umask(0); 1303*0Sstevel@tonic-gate if (!pflag) 1304*0Sstevel@tonic-gate (void) umask(mask); 1305*0Sstevel@tonic-gate if (argc != 1) { 1306*0Sstevel@tonic-gate error("rcp: ambiguous target\n"); 1307*0Sstevel@tonic-gate exit(1); 1308*0Sstevel@tonic-gate } 1309*0Sstevel@tonic-gate targ = *argv; 1310*0Sstevel@tonic-gate if (targetshouldbedirectory) 1311*0Sstevel@tonic-gate verifydir(targ); 1312*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 1313*0Sstevel@tonic-gate 1314*0Sstevel@tonic-gate if (stat(targ, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR) 1315*0Sstevel@tonic-gate targisdir = 1; 1316*0Sstevel@tonic-gate for (first = 1; ; first = 0) { 1317*0Sstevel@tonic-gate cp = buf; 1318*0Sstevel@tonic-gate if (desrcpread(rem, cp, 1) <= 0) { 1319*0Sstevel@tonic-gate if (namebuf != NULL) 1320*0Sstevel@tonic-gate free(namebuf); 1321*0Sstevel@tonic-gate return; 1322*0Sstevel@tonic-gate } 1323*0Sstevel@tonic-gate 1324*0Sstevel@tonic-gate if (*cp++ == '\n') 1325*0Sstevel@tonic-gate SCREWUP("unexpected <newline>"); 1326*0Sstevel@tonic-gate do { 1327*0Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) 1328*0Sstevel@tonic-gate SCREWUP("lost connection"); 1329*0Sstevel@tonic-gate *cp++ = ch; 1330*0Sstevel@tonic-gate } while (cp < &buf[RCP_BUFSIZE - 1] && ch != '\n'); 1331*0Sstevel@tonic-gate *cp = 0; 1332*0Sstevel@tonic-gate 1333*0Sstevel@tonic-gate if (buf[0] == '\01' || buf[0] == '\02') { 1334*0Sstevel@tonic-gate if (iamremote == 0) 1335*0Sstevel@tonic-gate (void) write(STDERR_FILENO, buf + 1, 1336*0Sstevel@tonic-gate strlen(buf + 1)); 1337*0Sstevel@tonic-gate if (buf[0] == '\02') 1338*0Sstevel@tonic-gate exit(1); 1339*0Sstevel@tonic-gate errs++; 1340*0Sstevel@tonic-gate continue; 1341*0Sstevel@tonic-gate } 1342*0Sstevel@tonic-gate if (buf[0] == 'E') { 1343*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 1344*0Sstevel@tonic-gate if (namebuf != NULL) 1345*0Sstevel@tonic-gate free(namebuf); 1346*0Sstevel@tonic-gate return; 1347*0Sstevel@tonic-gate } 1348*0Sstevel@tonic-gate 1349*0Sstevel@tonic-gate if (ch == '\n') 1350*0Sstevel@tonic-gate *--cp = 0; 1351*0Sstevel@tonic-gate cp = buf; 1352*0Sstevel@tonic-gate if (*cp == 'T') { 1353*0Sstevel@tonic-gate setimes++; 1354*0Sstevel@tonic-gate cp++; 1355*0Sstevel@tonic-gate mtime.tv_sec = strtol(cp, &cp, 0); 1356*0Sstevel@tonic-gate if (*cp++ != ' ') 1357*0Sstevel@tonic-gate SCREWUP("mtime.sec not delimited"); 1358*0Sstevel@tonic-gate mtime.tv_usec = strtol(cp, &cp, 0); 1359*0Sstevel@tonic-gate if (*cp++ != ' ') 1360*0Sstevel@tonic-gate SCREWUP("mtime.usec not delimited"); 1361*0Sstevel@tonic-gate atime.tv_sec = strtol(cp, &cp, 0); 1362*0Sstevel@tonic-gate if (*cp++ != ' ') 1363*0Sstevel@tonic-gate SCREWUP("atime.sec not delimited"); 1364*0Sstevel@tonic-gate atime.tv_usec = strtol(cp, &cp, 0); 1365*0Sstevel@tonic-gate if (*cp++ != '\0') 1366*0Sstevel@tonic-gate SCREWUP("atime.usec not delimited"); 1367*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 1368*0Sstevel@tonic-gate continue; 1369*0Sstevel@tonic-gate } 1370*0Sstevel@tonic-gate if (*cp != 'C' && *cp != 'D') { 1371*0Sstevel@tonic-gate /* 1372*0Sstevel@tonic-gate * Check for the case "rcp remote:foo\* local:bar". 1373*0Sstevel@tonic-gate * In this case, the line "No match." can be returned 1374*0Sstevel@tonic-gate * by the shell before the rcp command on the remote is 1375*0Sstevel@tonic-gate * executed so the ^Aerror_message convention isn't 1376*0Sstevel@tonic-gate * followed. 1377*0Sstevel@tonic-gate */ 1378*0Sstevel@tonic-gate if (first) { 1379*0Sstevel@tonic-gate error("%s\n", cp); 1380*0Sstevel@tonic-gate exit(1); 1381*0Sstevel@tonic-gate } 1382*0Sstevel@tonic-gate SCREWUP("expected control record"); 1383*0Sstevel@tonic-gate } 1384*0Sstevel@tonic-gate mode = 0; 1385*0Sstevel@tonic-gate for (++cp; cp < buf + 5; cp++) { 1386*0Sstevel@tonic-gate if (*cp < '0' || *cp > '7') 1387*0Sstevel@tonic-gate SCREWUP("bad mode"); 1388*0Sstevel@tonic-gate mode = (mode << 3) | (*cp - '0'); 1389*0Sstevel@tonic-gate } 1390*0Sstevel@tonic-gate if (*cp++ != ' ') 1391*0Sstevel@tonic-gate SCREWUP("mode not delimited"); 1392*0Sstevel@tonic-gate size = 0; 1393*0Sstevel@tonic-gate while (isdigit(*cp)) 1394*0Sstevel@tonic-gate size = size * 10 + (*cp++ - '0'); 1395*0Sstevel@tonic-gate if (*cp++ != ' ') 1396*0Sstevel@tonic-gate SCREWUP("size not delimited"); 1397*0Sstevel@tonic-gate if (targisdir) { 1398*0Sstevel@tonic-gate need = strlen(targ) + sizeof ("/") + strlen(cp); 1399*0Sstevel@tonic-gate if (need > namebuf_sz) { 1400*0Sstevel@tonic-gate if ((namebuf = realloc(namebuf, need)) == NULL) { 1401*0Sstevel@tonic-gate error("rcp: out of memory\n"); 1402*0Sstevel@tonic-gate exit(1); 1403*0Sstevel@tonic-gate } 1404*0Sstevel@tonic-gate namebuf_sz = need; 1405*0Sstevel@tonic-gate } 1406*0Sstevel@tonic-gate (void) snprintf(namebuf, need, "%s%s%s", targ, 1407*0Sstevel@tonic-gate *targ ? "/" : "", cp); 1408*0Sstevel@tonic-gate np = namebuf; 1409*0Sstevel@tonic-gate } else { 1410*0Sstevel@tonic-gate np = targ; 1411*0Sstevel@tonic-gate } 1412*0Sstevel@tonic-gate 1413*0Sstevel@tonic-gate exists = stat(np, &stb) == 0; 1414*0Sstevel@tonic-gate if (buf[0] == 'D') { 1415*0Sstevel@tonic-gate if (exists) { 1416*0Sstevel@tonic-gate if ((stb.st_mode&S_IFMT) != S_IFDIR) { 1417*0Sstevel@tonic-gate if (aclflag) { 1418*0Sstevel@tonic-gate /* 1419*0Sstevel@tonic-gate * consume acl in the pipe 1420*0Sstevel@tonic-gate * fd = -1 to indicate the 1421*0Sstevel@tonic-gate * special case 1422*0Sstevel@tonic-gate */ 1423*0Sstevel@tonic-gate if (recvacl(-1, exists, pflag) 1424*0Sstevel@tonic-gate == ACL_FAIL) { 1425*0Sstevel@tonic-gate goto bad; 1426*0Sstevel@tonic-gate } 1427*0Sstevel@tonic-gate } 1428*0Sstevel@tonic-gate errno = ENOTDIR; 1429*0Sstevel@tonic-gate goto bad; 1430*0Sstevel@tonic-gate } 1431*0Sstevel@tonic-gate if (pflag) 1432*0Sstevel@tonic-gate (void) chmod(np, mode); 1433*0Sstevel@tonic-gate } else if (mkdir(np, mode) < 0) { 1434*0Sstevel@tonic-gate if (aclflag) { 1435*0Sstevel@tonic-gate /* consume acl in the pipe */ 1436*0Sstevel@tonic-gate (void) recvacl(-1, exists, pflag); 1437*0Sstevel@tonic-gate } 1438*0Sstevel@tonic-gate goto bad; 1439*0Sstevel@tonic-gate } 1440*0Sstevel@tonic-gate 1441*0Sstevel@tonic-gate /* acl support for directories */ 1442*0Sstevel@tonic-gate if (aclflag) { 1443*0Sstevel@tonic-gate int dfd; 1444*0Sstevel@tonic-gate 1445*0Sstevel@tonic-gate if ((dfd = open(np, O_RDONLY)) == -1) 1446*0Sstevel@tonic-gate goto bad; 1447*0Sstevel@tonic-gate 1448*0Sstevel@tonic-gate /* get acl and set it to ofd */ 1449*0Sstevel@tonic-gate if (recvacl(dfd, exists, pflag) == ACL_FAIL) { 1450*0Sstevel@tonic-gate (void) close(dfd); 1451*0Sstevel@tonic-gate if (!exists) 1452*0Sstevel@tonic-gate (void) rmdir(np); 1453*0Sstevel@tonic-gate goto bad; 1454*0Sstevel@tonic-gate } 1455*0Sstevel@tonic-gate (void) close(dfd); 1456*0Sstevel@tonic-gate } 1457*0Sstevel@tonic-gate 1458*0Sstevel@tonic-gate vect[0] = np; 1459*0Sstevel@tonic-gate sink(1, vect); 1460*0Sstevel@tonic-gate if (setimes) { 1461*0Sstevel@tonic-gate setimes = 0; 1462*0Sstevel@tonic-gate if (utimes(np, tv) < 0) 1463*0Sstevel@tonic-gate error("rcp: can't set times on %s: %s\n", 1464*0Sstevel@tonic-gate np, strerror(errno)); 1465*0Sstevel@tonic-gate } 1466*0Sstevel@tonic-gate continue; 1467*0Sstevel@tonic-gate } 1468*0Sstevel@tonic-gate 1469*0Sstevel@tonic-gate if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 1470*0Sstevel@tonic-gate bad: 1471*0Sstevel@tonic-gate error("rcp: %s: %s\n", np, strerror(errno)); 1472*0Sstevel@tonic-gate continue; 1473*0Sstevel@tonic-gate } 1474*0Sstevel@tonic-gate 1475*0Sstevel@tonic-gate /* 1476*0Sstevel@tonic-gate * If the output file exists we have to force zflag off 1477*0Sstevel@tonic-gate * to avoid erroneously seeking past old data. 1478*0Sstevel@tonic-gate */ 1479*0Sstevel@tonic-gate zopen(ofd, zflag && !exists); 1480*0Sstevel@tonic-gate 1481*0Sstevel@tonic-gate if (exists && pflag) 1482*0Sstevel@tonic-gate (void) fchmod(ofd, mode); 1483*0Sstevel@tonic-gate 1484*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 1485*0Sstevel@tonic-gate 1486*0Sstevel@tonic-gate /* 1487*0Sstevel@tonic-gate * ACL support: receiving 1488*0Sstevel@tonic-gate */ 1489*0Sstevel@tonic-gate if (aclflag) { 1490*0Sstevel@tonic-gate /* get acl and set it to ofd */ 1491*0Sstevel@tonic-gate if (recvacl(ofd, exists, pflag) == ACL_FAIL) { 1492*0Sstevel@tonic-gate (void) close(ofd); 1493*0Sstevel@tonic-gate if (!exists) 1494*0Sstevel@tonic-gate (void) unlink(np); 1495*0Sstevel@tonic-gate continue; 1496*0Sstevel@tonic-gate } 1497*0Sstevel@tonic-gate } 1498*0Sstevel@tonic-gate 1499*0Sstevel@tonic-gate if ((bp = allocbuf(&buffer, ofd, RCP_BUFSIZE)) == 0) { 1500*0Sstevel@tonic-gate (void) close(ofd); 1501*0Sstevel@tonic-gate continue; 1502*0Sstevel@tonic-gate } 1503*0Sstevel@tonic-gate cp = bp->buf; 1504*0Sstevel@tonic-gate count = 0; 1505*0Sstevel@tonic-gate wrerr = 0; 1506*0Sstevel@tonic-gate for (i = 0; i < size; i += RCP_BUFSIZE) { 1507*0Sstevel@tonic-gate amt = RCP_BUFSIZE; 1508*0Sstevel@tonic-gate if (i + amt > size) 1509*0Sstevel@tonic-gate amt = size - i; 1510*0Sstevel@tonic-gate count += amt; 1511*0Sstevel@tonic-gate do { 1512*0Sstevel@tonic-gate j = desrcpread(rem, cp, amt); 1513*0Sstevel@tonic-gate if (j <= 0) { 1514*0Sstevel@tonic-gate int sverrno = errno; 1515*0Sstevel@tonic-gate 1516*0Sstevel@tonic-gate /* 1517*0Sstevel@tonic-gate * Connection to supplier lost. 1518*0Sstevel@tonic-gate * Truncate file to correspond 1519*0Sstevel@tonic-gate * to amount already transferred. 1520*0Sstevel@tonic-gate * 1521*0Sstevel@tonic-gate * Note that we must call ftruncate() 1522*0Sstevel@tonic-gate * before any call to error() (which 1523*0Sstevel@tonic-gate * might result in a SIGPIPE and 1524*0Sstevel@tonic-gate * sudden death before we have a chance 1525*0Sstevel@tonic-gate * to correct the file's size). 1526*0Sstevel@tonic-gate */ 1527*0Sstevel@tonic-gate size = lseek(ofd, 0, SEEK_CUR); 1528*0Sstevel@tonic-gate if ((ftruncate(ofd, size) == -1) && 1529*0Sstevel@tonic-gate (errno != EINVAL) && 1530*0Sstevel@tonic-gate (errno != EACCES)) 1531*0Sstevel@tonic-gate #define TRUNCERR "rcp: can't truncate %s: %s\n" 1532*0Sstevel@tonic-gate error(TRUNCERR, np, 1533*0Sstevel@tonic-gate strerror(errno)); 1534*0Sstevel@tonic-gate error("rcp: %s\n", 1535*0Sstevel@tonic-gate j ? strerror(sverrno) : 1536*0Sstevel@tonic-gate "dropped connection"); 1537*0Sstevel@tonic-gate (void) close(ofd); 1538*0Sstevel@tonic-gate exit(1); 1539*0Sstevel@tonic-gate } 1540*0Sstevel@tonic-gate amt -= j; 1541*0Sstevel@tonic-gate cp += j; 1542*0Sstevel@tonic-gate } while (amt > 0); 1543*0Sstevel@tonic-gate if (count == bp->cnt) { 1544*0Sstevel@tonic-gate cp = bp->buf; 1545*0Sstevel@tonic-gate if (wrerr == 0 && 1546*0Sstevel@tonic-gate zwrite(ofd, cp, count) < 0) 1547*0Sstevel@tonic-gate wrerr++; 1548*0Sstevel@tonic-gate count = 0; 1549*0Sstevel@tonic-gate } 1550*0Sstevel@tonic-gate } 1551*0Sstevel@tonic-gate if (count != 0 && wrerr == 0 && 1552*0Sstevel@tonic-gate zwrite(ofd, bp->buf, count) < 0) 1553*0Sstevel@tonic-gate wrerr++; 1554*0Sstevel@tonic-gate if (zclose(ofd) < 0) 1555*0Sstevel@tonic-gate wrerr++; 1556*0Sstevel@tonic-gate 1557*0Sstevel@tonic-gate 1558*0Sstevel@tonic-gate if ((ftruncate(ofd, size) == -1) && (errno != EINVAL) && 1559*0Sstevel@tonic-gate (errno != EACCES)) { 1560*0Sstevel@tonic-gate error(TRUNCERR, np, strerror(errno)); 1561*0Sstevel@tonic-gate } 1562*0Sstevel@tonic-gate (void) close(ofd); 1563*0Sstevel@tonic-gate (void) response(); 1564*0Sstevel@tonic-gate if (setimes) { 1565*0Sstevel@tonic-gate setimes = 0; 1566*0Sstevel@tonic-gate if (utimes(np, tv) < 0) 1567*0Sstevel@tonic-gate error("rcp: can't set times on %s: %s\n", 1568*0Sstevel@tonic-gate np, strerror(errno)); 1569*0Sstevel@tonic-gate } 1570*0Sstevel@tonic-gate if (wrerr) 1571*0Sstevel@tonic-gate error("rcp: %s: %s\n", np, strerror(errno)); 1572*0Sstevel@tonic-gate else 1573*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 1574*0Sstevel@tonic-gate } 1575*0Sstevel@tonic-gate screwup: 1576*0Sstevel@tonic-gate error("rcp: protocol screwup: %s\n", why); 1577*0Sstevel@tonic-gate exit(1); 1578*0Sstevel@tonic-gate } 1579*0Sstevel@tonic-gate 1580*0Sstevel@tonic-gate #ifndef roundup 1581*0Sstevel@tonic-gate #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 1582*0Sstevel@tonic-gate #endif /* !roundup */ 1583*0Sstevel@tonic-gate 1584*0Sstevel@tonic-gate static BUF * 1585*0Sstevel@tonic-gate allocbuf(BUF *bp, int fd, int blksize) 1586*0Sstevel@tonic-gate { 1587*0Sstevel@tonic-gate struct stat stb; 1588*0Sstevel@tonic-gate int size; 1589*0Sstevel@tonic-gate 1590*0Sstevel@tonic-gate if (fstat(fd, &stb) < 0) { 1591*0Sstevel@tonic-gate error("rcp: fstat: %s\n", strerror(errno)); 1592*0Sstevel@tonic-gate return (0); 1593*0Sstevel@tonic-gate } 1594*0Sstevel@tonic-gate size = roundup(stb.st_blksize, blksize); 1595*0Sstevel@tonic-gate if (size == 0) 1596*0Sstevel@tonic-gate size = blksize; 1597*0Sstevel@tonic-gate if (bp->cnt < size) { 1598*0Sstevel@tonic-gate if (bp->buf != 0) 1599*0Sstevel@tonic-gate free(bp->buf); 1600*0Sstevel@tonic-gate bp->buf = (char *)malloc((uint_t)size); 1601*0Sstevel@tonic-gate if (!bp->buf) { 1602*0Sstevel@tonic-gate error("rcp: malloc: out of memory\n"); 1603*0Sstevel@tonic-gate return (0); 1604*0Sstevel@tonic-gate } 1605*0Sstevel@tonic-gate } 1606*0Sstevel@tonic-gate bp->cnt = size; 1607*0Sstevel@tonic-gate return (bp); 1608*0Sstevel@tonic-gate } 1609*0Sstevel@tonic-gate 1610*0Sstevel@tonic-gate static void 1611*0Sstevel@tonic-gate usage(void) 1612*0Sstevel@tonic-gate { 1613*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: \t%s\t%s", gettext("Usage"), 1614*0Sstevel@tonic-gate gettext("\trcp [-p] [-a] [-x] [-k realm] [-PN / -PO] " 1615*0Sstevel@tonic-gate #ifdef DEBUG 1616*0Sstevel@tonic-gate "[-D port] " 1617*0Sstevel@tonic-gate #endif /* DEBUG */ 1618*0Sstevel@tonic-gate "f1 f2; or:\n"), 1619*0Sstevel@tonic-gate gettext("\trcp [-r] [-p] [-a] [-x] " 1620*0Sstevel@tonic-gate #ifdef DEBUG 1621*0Sstevel@tonic-gate "[-D port] " 1622*0Sstevel@tonic-gate #endif /* DEBUG */ 1623*0Sstevel@tonic-gate "[-k realm] [-PN / -PO] f1...fn d2\n")); 1624*0Sstevel@tonic-gate exit(1); 1625*0Sstevel@tonic-gate } 1626*0Sstevel@tonic-gate 1627*0Sstevel@tonic-gate 1628*0Sstevel@tonic-gate /* 1629*0Sstevel@tonic-gate * sparse file support 1630*0Sstevel@tonic-gate */ 1631*0Sstevel@tonic-gate 1632*0Sstevel@tonic-gate static off_t zbsize; 1633*0Sstevel@tonic-gate static off_t zlastseek; 1634*0Sstevel@tonic-gate 1635*0Sstevel@tonic-gate /* is it ok to try to create holes? */ 1636*0Sstevel@tonic-gate static void 1637*0Sstevel@tonic-gate zopen(int fd, int flag) 1638*0Sstevel@tonic-gate { 1639*0Sstevel@tonic-gate struct stat st; 1640*0Sstevel@tonic-gate 1641*0Sstevel@tonic-gate zbsize = 0; 1642*0Sstevel@tonic-gate zlastseek = 0; 1643*0Sstevel@tonic-gate 1644*0Sstevel@tonic-gate if (flag && 1645*0Sstevel@tonic-gate fstat(fd, &st) == 0 && 1646*0Sstevel@tonic-gate (st.st_mode & S_IFMT) == S_IFREG) 1647*0Sstevel@tonic-gate zbsize = st.st_blksize; 1648*0Sstevel@tonic-gate } 1649*0Sstevel@tonic-gate 1650*0Sstevel@tonic-gate /* write and/or seek */ 1651*0Sstevel@tonic-gate static int 1652*0Sstevel@tonic-gate zwrite(int fd, char *buf, int nbytes) 1653*0Sstevel@tonic-gate { 1654*0Sstevel@tonic-gate off_t block = zbsize ? zbsize : nbytes; 1655*0Sstevel@tonic-gate 1656*0Sstevel@tonic-gate do { 1657*0Sstevel@tonic-gate if (block > nbytes) 1658*0Sstevel@tonic-gate block = nbytes; 1659*0Sstevel@tonic-gate nbytes -= block; 1660*0Sstevel@tonic-gate 1661*0Sstevel@tonic-gate if (!zbsize || notzero(buf, block)) { 1662*0Sstevel@tonic-gate register int n, count = block; 1663*0Sstevel@tonic-gate 1664*0Sstevel@tonic-gate do { 1665*0Sstevel@tonic-gate if ((n = write(fd, buf, count)) < 0) 1666*0Sstevel@tonic-gate return (-1); 1667*0Sstevel@tonic-gate buf += n; 1668*0Sstevel@tonic-gate } while ((count -= n) > 0); 1669*0Sstevel@tonic-gate zlastseek = 0; 1670*0Sstevel@tonic-gate } else { 1671*0Sstevel@tonic-gate if (lseek(fd, (off_t)block, SEEK_CUR) < 0) 1672*0Sstevel@tonic-gate return (-1); 1673*0Sstevel@tonic-gate buf += block; 1674*0Sstevel@tonic-gate zlastseek = 1; 1675*0Sstevel@tonic-gate } 1676*0Sstevel@tonic-gate } while (nbytes > 0); 1677*0Sstevel@tonic-gate 1678*0Sstevel@tonic-gate return (0); 1679*0Sstevel@tonic-gate } 1680*0Sstevel@tonic-gate 1681*0Sstevel@tonic-gate /* write last byte of file if necessary */ 1682*0Sstevel@tonic-gate static int 1683*0Sstevel@tonic-gate zclose(int fd) 1684*0Sstevel@tonic-gate { 1685*0Sstevel@tonic-gate zbsize = 0; 1686*0Sstevel@tonic-gate 1687*0Sstevel@tonic-gate if (zlastseek && (lseek(fd, (off_t)-1, SEEK_CUR) < 0 || 1688*0Sstevel@tonic-gate zwrite(fd, "", 1) < 0)) 1689*0Sstevel@tonic-gate return (-1); 1690*0Sstevel@tonic-gate else 1691*0Sstevel@tonic-gate return (0); 1692*0Sstevel@tonic-gate } 1693*0Sstevel@tonic-gate 1694*0Sstevel@tonic-gate /* return true if buffer is not all zeros */ 1695*0Sstevel@tonic-gate static int 1696*0Sstevel@tonic-gate notzero(char *p, int n) 1697*0Sstevel@tonic-gate { 1698*0Sstevel@tonic-gate register int result = 0; 1699*0Sstevel@tonic-gate 1700*0Sstevel@tonic-gate while ((int)p & 3 && --n >= 0) 1701*0Sstevel@tonic-gate result |= *p++; 1702*0Sstevel@tonic-gate 1703*0Sstevel@tonic-gate while ((n -= 4 * sizeof (int)) >= 0) { 1704*0Sstevel@tonic-gate /* LINTED */ 1705*0Sstevel@tonic-gate result |= ((int *)p)[0]; 1706*0Sstevel@tonic-gate /* LINTED */ 1707*0Sstevel@tonic-gate result |= ((int *)p)[1]; 1708*0Sstevel@tonic-gate /* LINTED */ 1709*0Sstevel@tonic-gate result |= ((int *)p)[2]; 1710*0Sstevel@tonic-gate /* LINTED */ 1711*0Sstevel@tonic-gate result |= ((int *)p)[3]; 1712*0Sstevel@tonic-gate if (result) 1713*0Sstevel@tonic-gate return (result); 1714*0Sstevel@tonic-gate p += 4 * sizeof (int); 1715*0Sstevel@tonic-gate } 1716*0Sstevel@tonic-gate n += 4 * sizeof (int); 1717*0Sstevel@tonic-gate 1718*0Sstevel@tonic-gate while (--n >= 0) 1719*0Sstevel@tonic-gate result |= *p++; 1720*0Sstevel@tonic-gate 1721*0Sstevel@tonic-gate return (result); 1722*0Sstevel@tonic-gate } 1723*0Sstevel@tonic-gate 1724*0Sstevel@tonic-gate /* 1725*0Sstevel@tonic-gate * New functions to support ACLs 1726*0Sstevel@tonic-gate */ 1727*0Sstevel@tonic-gate 1728*0Sstevel@tonic-gate /* 1729*0Sstevel@tonic-gate * Get acl from f and send it over. 1730*0Sstevel@tonic-gate * ACL record includes acl entry count, acl text length, and acl text. 1731*0Sstevel@tonic-gate */ 1732*0Sstevel@tonic-gate static int 1733*0Sstevel@tonic-gate sendacl(int f) 1734*0Sstevel@tonic-gate { 1735*0Sstevel@tonic-gate int aclcnt; 1736*0Sstevel@tonic-gate aclent_t *aclbufp; 1737*0Sstevel@tonic-gate int aclsize; 1738*0Sstevel@tonic-gate char *acltext; 1739*0Sstevel@tonic-gate char buf[BUFSIZ]; 1740*0Sstevel@tonic-gate 1741*0Sstevel@tonic-gate if ((aclcnt = facl(f, GETACLCNT, 0, NULL)) < 0) { 1742*0Sstevel@tonic-gate error("can't get acl count \n"); 1743*0Sstevel@tonic-gate return (ACL_FAIL); 1744*0Sstevel@tonic-gate } 1745*0Sstevel@tonic-gate 1746*0Sstevel@tonic-gate /* send the acl count over */ 1747*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "A%d\n", aclcnt); 1748*0Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 1749*0Sstevel@tonic-gate 1750*0Sstevel@tonic-gate /* only send acl when it is non-trivial */ 1751*0Sstevel@tonic-gate if (aclcnt > MIN_ACL_ENTRIES) { 1752*0Sstevel@tonic-gate aclsize = aclcnt * sizeof (aclent_t); 1753*0Sstevel@tonic-gate if ((aclbufp = (aclent_t *)malloc(aclsize)) == NULL) { 1754*0Sstevel@tonic-gate error("rcp: cant allocate memory: aclcnt %d\n", 1755*0Sstevel@tonic-gate aclcnt); 1756*0Sstevel@tonic-gate exit(1); 1757*0Sstevel@tonic-gate } 1758*0Sstevel@tonic-gate if (facl(f, GETACL, aclcnt, aclbufp) < 0) { 1759*0Sstevel@tonic-gate error("rcp: failed to get acl\n"); 1760*0Sstevel@tonic-gate return (ACL_FAIL); 1761*0Sstevel@tonic-gate } 1762*0Sstevel@tonic-gate acltext = acltotext(aclbufp, aclcnt); 1763*0Sstevel@tonic-gate if (acltext == NULL) { 1764*0Sstevel@tonic-gate error("rcp: failed to convert to text\n"); 1765*0Sstevel@tonic-gate return (ACL_FAIL); 1766*0Sstevel@tonic-gate } 1767*0Sstevel@tonic-gate 1768*0Sstevel@tonic-gate /* send ACLs over: send the length first */ 1769*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "A%d\n", strlen(acltext)); 1770*0Sstevel@tonic-gate 1771*0Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 1772*0Sstevel@tonic-gate (void) desrcpwrite(rem, acltext, strlen(acltext)); 1773*0Sstevel@tonic-gate free(acltext); 1774*0Sstevel@tonic-gate free(aclbufp); 1775*0Sstevel@tonic-gate if (response() < 0) 1776*0Sstevel@tonic-gate return (ACL_FAIL); 1777*0Sstevel@tonic-gate 1778*0Sstevel@tonic-gate } 1779*0Sstevel@tonic-gate return (ACL_OK); 1780*0Sstevel@tonic-gate } 1781*0Sstevel@tonic-gate 1782*0Sstevel@tonic-gate /* 1783*0Sstevel@tonic-gate * Use this routine to get acl entry count and acl text size (in bytes) 1784*0Sstevel@tonic-gate */ 1785*0Sstevel@tonic-gate static int 1786*0Sstevel@tonic-gate getaclinfo(int *cnt) 1787*0Sstevel@tonic-gate { 1788*0Sstevel@tonic-gate char buf[BUFSIZ]; 1789*0Sstevel@tonic-gate char *cp; 1790*0Sstevel@tonic-gate char ch; 1791*0Sstevel@tonic-gate 1792*0Sstevel@tonic-gate /* get acl count */ 1793*0Sstevel@tonic-gate cp = buf; 1794*0Sstevel@tonic-gate if (desrcpread(rem, cp, 1) <= 0) 1795*0Sstevel@tonic-gate return (ACL_FAIL); 1796*0Sstevel@tonic-gate if (*cp++ != 'A') { 1797*0Sstevel@tonic-gate error("rcp: expect an ACL record, but got %c\n", *cp); 1798*0Sstevel@tonic-gate return (ACL_FAIL); 1799*0Sstevel@tonic-gate } 1800*0Sstevel@tonic-gate do { 1801*0Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) { 1802*0Sstevel@tonic-gate error("rcp: lost connection ..\n"); 1803*0Sstevel@tonic-gate return (ACL_FAIL); 1804*0Sstevel@tonic-gate } 1805*0Sstevel@tonic-gate *cp++ = ch; 1806*0Sstevel@tonic-gate } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 1807*0Sstevel@tonic-gate if (ch != '\n') { 1808*0Sstevel@tonic-gate error("rcp: ACL record corrupted \n"); 1809*0Sstevel@tonic-gate return (ACL_FAIL); 1810*0Sstevel@tonic-gate } 1811*0Sstevel@tonic-gate cp = &buf[1]; 1812*0Sstevel@tonic-gate *cnt = strtol(cp, &cp, 0); 1813*0Sstevel@tonic-gate if (*cp != '\n') { 1814*0Sstevel@tonic-gate error("rcp: ACL record corrupted \n"); 1815*0Sstevel@tonic-gate return (ACL_FAIL); 1816*0Sstevel@tonic-gate } 1817*0Sstevel@tonic-gate return (ACL_OK); 1818*0Sstevel@tonic-gate } 1819*0Sstevel@tonic-gate 1820*0Sstevel@tonic-gate 1821*0Sstevel@tonic-gate /* 1822*0Sstevel@tonic-gate * Receive acl from the pipe and set it to f 1823*0Sstevel@tonic-gate */ 1824*0Sstevel@tonic-gate static int 1825*0Sstevel@tonic-gate recvacl(int f, int exists, int preserve) 1826*0Sstevel@tonic-gate { 1827*0Sstevel@tonic-gate int aclcnt; /* acl entry count */ 1828*0Sstevel@tonic-gate int aclsize; /* acl text length */ 1829*0Sstevel@tonic-gate int j; 1830*0Sstevel@tonic-gate char *tp; 1831*0Sstevel@tonic-gate char *acltext; /* external format */ 1832*0Sstevel@tonic-gate aclent_t *aclbufp; /* internal format */ 1833*0Sstevel@tonic-gate 1834*0Sstevel@tonic-gate /* get acl count */ 1835*0Sstevel@tonic-gate if (getaclinfo(&aclcnt) != ACL_OK) 1836*0Sstevel@tonic-gate return (ACL_FAIL); 1837*0Sstevel@tonic-gate 1838*0Sstevel@tonic-gate if (aclcnt > MIN_ACL_ENTRIES) { 1839*0Sstevel@tonic-gate /* get acl text size */ 1840*0Sstevel@tonic-gate if (getaclinfo(&aclsize) != ACL_OK) 1841*0Sstevel@tonic-gate return (ACL_FAIL); 1842*0Sstevel@tonic-gate if ((acltext = malloc(aclsize + 1)) == NULL) { 1843*0Sstevel@tonic-gate error("rcp: cant allocate memory: %d\n", aclsize); 1844*0Sstevel@tonic-gate return (ACL_FAIL); 1845*0Sstevel@tonic-gate } 1846*0Sstevel@tonic-gate 1847*0Sstevel@tonic-gate tp = acltext; 1848*0Sstevel@tonic-gate do { 1849*0Sstevel@tonic-gate j = desrcpread(rem, tp, aclsize); 1850*0Sstevel@tonic-gate if (j <= 0) { 1851*0Sstevel@tonic-gate error("rcp: %s\n", j ? strerror(errno) : 1852*0Sstevel@tonic-gate "dropped connection"); 1853*0Sstevel@tonic-gate exit(1); 1854*0Sstevel@tonic-gate } 1855*0Sstevel@tonic-gate aclsize -= j; 1856*0Sstevel@tonic-gate tp += j; 1857*0Sstevel@tonic-gate } while (aclsize > 0); 1858*0Sstevel@tonic-gate *tp = '\0'; 1859*0Sstevel@tonic-gate 1860*0Sstevel@tonic-gate if (preserve || !exists) { 1861*0Sstevel@tonic-gate aclbufp = aclfromtext(acltext, &aclcnt); 1862*0Sstevel@tonic-gate if (aclbufp == NULL) { 1863*0Sstevel@tonic-gate error("rcp: failed to parse acl\n"); 1864*0Sstevel@tonic-gate return (ACL_FAIL); 1865*0Sstevel@tonic-gate } 1866*0Sstevel@tonic-gate if (f != -1) { 1867*0Sstevel@tonic-gate if (facl(f, SETACL, aclcnt, aclbufp) < 0) { 1868*0Sstevel@tonic-gate error("rcp: failed to set acl\n"); 1869*0Sstevel@tonic-gate return (ACL_FAIL); 1870*0Sstevel@tonic-gate } 1871*0Sstevel@tonic-gate } 1872*0Sstevel@tonic-gate /* -1 means that just consume the data in the pipe */ 1873*0Sstevel@tonic-gate free(aclbufp); 1874*0Sstevel@tonic-gate } 1875*0Sstevel@tonic-gate free(acltext); 1876*0Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1); 1877*0Sstevel@tonic-gate } 1878*0Sstevel@tonic-gate return (ACL_OK); 1879*0Sstevel@tonic-gate } 1880*0Sstevel@tonic-gate 1881*0Sstevel@tonic-gate 1882*0Sstevel@tonic-gate static char * 1883*0Sstevel@tonic-gate search_char(unsigned char *cp, unsigned char chr) 1884*0Sstevel@tonic-gate { 1885*0Sstevel@tonic-gate int len; 1886*0Sstevel@tonic-gate 1887*0Sstevel@tonic-gate while (*cp) { 1888*0Sstevel@tonic-gate if (*cp == chr) 1889*0Sstevel@tonic-gate return ((char *)cp); 1890*0Sstevel@tonic-gate if ((len = mblen((char *)cp, MB_CUR_MAX)) <= 0) 1891*0Sstevel@tonic-gate len = 1; 1892*0Sstevel@tonic-gate cp += len; 1893*0Sstevel@tonic-gate } 1894*0Sstevel@tonic-gate return (0); 1895*0Sstevel@tonic-gate } 1896*0Sstevel@tonic-gate 1897*0Sstevel@tonic-gate 1898*0Sstevel@tonic-gate static int 1899*0Sstevel@tonic-gate desrcpread(int fd, char *buf, int len) 1900*0Sstevel@tonic-gate { 1901*0Sstevel@tonic-gate return ((int)desread(fd, buf, len, 0)); 1902*0Sstevel@tonic-gate } 1903*0Sstevel@tonic-gate 1904*0Sstevel@tonic-gate static int 1905*0Sstevel@tonic-gate desrcpwrite(int fd, char *buf, int len) 1906*0Sstevel@tonic-gate { 1907*0Sstevel@tonic-gate /* 1908*0Sstevel@tonic-gate * Note that rcp depends on the same file descriptor being both 1909*0Sstevel@tonic-gate * input and output to the remote side. This is bogus, especially 1910*0Sstevel@tonic-gate * when rcp is being run by a rsh that pipes. Fix it here because 1911*0Sstevel@tonic-gate * it would require significantly more work in other places. 1912*0Sstevel@tonic-gate * --hartmans 1/96 1913*0Sstevel@tonic-gate */ 1914*0Sstevel@tonic-gate 1915*0Sstevel@tonic-gate if (fd == 0) 1916*0Sstevel@tonic-gate fd = 1; 1917*0Sstevel@tonic-gate return ((int)deswrite(fd, buf, len, 0)); 1918*0Sstevel@tonic-gate } 1919*0Sstevel@tonic-gate 1920*0Sstevel@tonic-gate static char ** 1921*0Sstevel@tonic-gate save_argv(int argc, char **argv) 1922*0Sstevel@tonic-gate { 1923*0Sstevel@tonic-gate int i; 1924*0Sstevel@tonic-gate 1925*0Sstevel@tonic-gate char **local_argv = (char **)calloc((unsigned)argc + 1, 1926*0Sstevel@tonic-gate (unsigned)sizeof (char *)); 1927*0Sstevel@tonic-gate 1928*0Sstevel@tonic-gate /* 1929*0Sstevel@tonic-gate * allocate an extra pointer, so that it is initialized to NULL and 1930*0Sstevel@tonic-gate * execv() will work 1931*0Sstevel@tonic-gate */ 1932*0Sstevel@tonic-gate for (i = 0; i < argc; i++) { 1933*0Sstevel@tonic-gate local_argv[i] = strsave(argv[i]); 1934*0Sstevel@tonic-gate } 1935*0Sstevel@tonic-gate 1936*0Sstevel@tonic-gate return (local_argv); 1937*0Sstevel@tonic-gate } 1938*0Sstevel@tonic-gate 1939*0Sstevel@tonic-gate #define SIZEOF_INADDR sizeof (struct in_addr) 1940*0Sstevel@tonic-gate 1941*0Sstevel@tonic-gate static void 1942*0Sstevel@tonic-gate answer_auth(char *config_file, char *ccache_file) 1943*0Sstevel@tonic-gate { 1944*0Sstevel@tonic-gate krb5_data pname_data, msg; 1945*0Sstevel@tonic-gate krb5_creds creds, *new_creds; 1946*0Sstevel@tonic-gate krb5_ccache cc; 1947*0Sstevel@tonic-gate krb5_auth_context auth_context = NULL; 1948*0Sstevel@tonic-gate 1949*0Sstevel@tonic-gate if (config_file) { 1950*0Sstevel@tonic-gate const char *filenames[2]; 1951*0Sstevel@tonic-gate 1952*0Sstevel@tonic-gate filenames[1] = NULL; 1953*0Sstevel@tonic-gate filenames[0] = config_file; 1954*0Sstevel@tonic-gate if (krb5_set_config_files(bsd_context, filenames)) 1955*0Sstevel@tonic-gate exit(1); 1956*0Sstevel@tonic-gate } 1957*0Sstevel@tonic-gate (void) memset((char *)&creds, 0, sizeof (creds)); 1958*0Sstevel@tonic-gate 1959*0Sstevel@tonic-gate if (krb5_read_message(bsd_context, (krb5_pointer) &rem, &pname_data)) 1960*0Sstevel@tonic-gate exit(1); 1961*0Sstevel@tonic-gate 1962*0Sstevel@tonic-gate if (krb5_read_message(bsd_context, (krb5_pointer) &rem, 1963*0Sstevel@tonic-gate &creds.second_ticket)) 1964*0Sstevel@tonic-gate exit(1); 1965*0Sstevel@tonic-gate 1966*0Sstevel@tonic-gate if (ccache_file == NULL) { 1967*0Sstevel@tonic-gate if (krb5_cc_default(bsd_context, &cc)) 1968*0Sstevel@tonic-gate exit(1); 1969*0Sstevel@tonic-gate } else { 1970*0Sstevel@tonic-gate if (krb5_cc_resolve(bsd_context, ccache_file, &cc)) 1971*0Sstevel@tonic-gate exit(1); 1972*0Sstevel@tonic-gate } 1973*0Sstevel@tonic-gate 1974*0Sstevel@tonic-gate if (krb5_cc_get_principal(bsd_context, cc, &creds.client)) 1975*0Sstevel@tonic-gate exit(1); 1976*0Sstevel@tonic-gate 1977*0Sstevel@tonic-gate if (krb5_parse_name(bsd_context, pname_data.data, &creds.server)) 1978*0Sstevel@tonic-gate exit(1); 1979*0Sstevel@tonic-gate 1980*0Sstevel@tonic-gate krb5_xfree(pname_data.data); 1981*0Sstevel@tonic-gate if (krb5_get_credentials(bsd_context, KRB5_GC_USER_USER, cc, &creds, 1982*0Sstevel@tonic-gate &new_creds)) 1983*0Sstevel@tonic-gate exit(1); 1984*0Sstevel@tonic-gate 1985*0Sstevel@tonic-gate if (krb5_mk_req_extended(bsd_context, &auth_context, 1986*0Sstevel@tonic-gate AP_OPTS_USE_SESSION_KEY, NULL, new_creds, &msg)) 1987*0Sstevel@tonic-gate exit(1); 1988*0Sstevel@tonic-gate 1989*0Sstevel@tonic-gate if (krb5_write_message(bsd_context, (krb5_pointer) & rem, &msg)) { 1990*0Sstevel@tonic-gate krb5_xfree(msg.data); 1991*0Sstevel@tonic-gate exit(1); 1992*0Sstevel@tonic-gate } 1993*0Sstevel@tonic-gate /* setup eblock for des_read and write */ 1994*0Sstevel@tonic-gate krb5_copy_keyblock(bsd_context, &new_creds->keyblock, &session_key); 1995*0Sstevel@tonic-gate 1996*0Sstevel@tonic-gate /* OK process key */ 1997*0Sstevel@tonic-gate eblock.crypto_entry = session_key->enctype; 1998*0Sstevel@tonic-gate eblock.key = (krb5_keyblock *)session_key; 1999*0Sstevel@tonic-gate 2000*0Sstevel@tonic-gate init_encrypt(encrypt_flag, bsd_context, KCMD_OLD_PROTOCOL, 2001*0Sstevel@tonic-gate &desinbuf, &desoutbuf, CLIENT, &eblock); 2002*0Sstevel@tonic-gate /* cleanup */ 2003*0Sstevel@tonic-gate krb5_free_cred_contents(bsd_context, &creds); 2004*0Sstevel@tonic-gate krb5_free_creds(bsd_context, new_creds); 2005*0Sstevel@tonic-gate krb5_xfree(msg.data); 2006*0Sstevel@tonic-gate } 2007*0Sstevel@tonic-gate 2008*0Sstevel@tonic-gate 2009*0Sstevel@tonic-gate static void 2010*0Sstevel@tonic-gate try_normal_rcp(int cur_argc, char **cur_argv) 2011*0Sstevel@tonic-gate { 2012*0Sstevel@tonic-gate char *target; 2013*0Sstevel@tonic-gate 2014*0Sstevel@tonic-gate /* 2015*0Sstevel@tonic-gate * Reset all KRB5 relevant flags and set the 2016*0Sstevel@tonic-gate * cmd-buffer so that normal rcp works 2017*0Sstevel@tonic-gate */ 2018*0Sstevel@tonic-gate krb5auth_flag = encrypt_flag = encrypt_done = 0; 2019*0Sstevel@tonic-gate cmd = cmd_orig; 2020*0Sstevel@tonic-gate cmd_sunw = cmd_sunw_orig; 2021*0Sstevel@tonic-gate 2022*0Sstevel@tonic-gate if (cur_argc < 2) 2023*0Sstevel@tonic-gate usage(); 2024*0Sstevel@tonic-gate 2025*0Sstevel@tonic-gate if (cur_argc > 2) 2026*0Sstevel@tonic-gate targetshouldbedirectory = 1; 2027*0Sstevel@tonic-gate 2028*0Sstevel@tonic-gate rem = -1; 2029*0Sstevel@tonic-gate 2030*0Sstevel@tonic-gate prev_argc = cur_argc; 2031*0Sstevel@tonic-gate prev_argv = save_argv(cur_argc, cur_argv); 2032*0Sstevel@tonic-gate 2033*0Sstevel@tonic-gate (void) init_service(krb5auth_flag); 2034*0Sstevel@tonic-gate 2035*0Sstevel@tonic-gate if (target = colon(cur_argv[cur_argc - 1])) { 2036*0Sstevel@tonic-gate toremote(target, cur_argc, cur_argv); 2037*0Sstevel@tonic-gate } else { 2038*0Sstevel@tonic-gate tolocal(cur_argc, cur_argv); 2039*0Sstevel@tonic-gate if (targetshouldbedirectory) 2040*0Sstevel@tonic-gate verifydir(cur_argv[cur_argc - 1]); 2041*0Sstevel@tonic-gate } 2042*0Sstevel@tonic-gate exit(errs); 2043*0Sstevel@tonic-gate /* NOTREACHED */ 2044*0Sstevel@tonic-gate } 2045*0Sstevel@tonic-gate 2046*0Sstevel@tonic-gate 2047*0Sstevel@tonic-gate static int 2048*0Sstevel@tonic-gate init_service(int krb5flag) 2049*0Sstevel@tonic-gate { 2050*0Sstevel@tonic-gate struct servent *sp; 2051*0Sstevel@tonic-gate boolean_t success = B_FALSE; 2052*0Sstevel@tonic-gate 2053*0Sstevel@tonic-gate if (krb5flag > 0) { 2054*0Sstevel@tonic-gate sp = getservbyname("kshell", "tcp"); 2055*0Sstevel@tonic-gate if (sp == NULL) { 2056*0Sstevel@tonic-gate (void) fprintf(stderr, 2057*0Sstevel@tonic-gate gettext("rcp: kshell/tcp: unknown service.\n" 2058*0Sstevel@tonic-gate "trying normal shell/tcp service\n")); 2059*0Sstevel@tonic-gate } else { 2060*0Sstevel@tonic-gate portnumber = sp->s_port; 2061*0Sstevel@tonic-gate success = B_TRUE; 2062*0Sstevel@tonic-gate } 2063*0Sstevel@tonic-gate } else { 2064*0Sstevel@tonic-gate portnumber = htons(IPPORT_CMDSERVER); 2065*0Sstevel@tonic-gate success = B_TRUE; 2066*0Sstevel@tonic-gate } 2067*0Sstevel@tonic-gate return (success); 2068*0Sstevel@tonic-gate } 2069*0Sstevel@tonic-gate 2070*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 2071*0Sstevel@tonic-gate static void 2072*0Sstevel@tonic-gate error(char *fmt, ...) 2073*0Sstevel@tonic-gate { 2074*0Sstevel@tonic-gate va_list ap; 2075*0Sstevel@tonic-gate char buf[RCP_BUFSIZE]; 2076*0Sstevel@tonic-gate char *cp = buf; 2077*0Sstevel@tonic-gate 2078*0Sstevel@tonic-gate va_start(ap, fmt); 2079*0Sstevel@tonic-gate errs++; 2080*0Sstevel@tonic-gate *cp++ = 1; 2081*0Sstevel@tonic-gate (void) vsnprintf(cp, sizeof (buf) - 1, fmt, ap); 2082*0Sstevel@tonic-gate va_end(ap); 2083*0Sstevel@tonic-gate 2084*0Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf)); 2085*0Sstevel@tonic-gate if (iamremote == 0) 2086*0Sstevel@tonic-gate (void) write(2, buf + 1, strlen(buf + 1)); 2087*0Sstevel@tonic-gate } 2088