10Sstevel@tonic-gate /*
211415SSurya.Prakki@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 1983 The Regents of the University of California.
80Sstevel@tonic-gate * All rights reserved.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
110Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are
120Sstevel@tonic-gate * duplicated in all such forms and that any documentation,
130Sstevel@tonic-gate * advertising materials, and other materials related to such
140Sstevel@tonic-gate * distribution and use acknowledge that the software was developed
150Sstevel@tonic-gate * by the University of California, Berkeley. The name of the
160Sstevel@tonic-gate * University may not be used to endorse or promote products derived
170Sstevel@tonic-gate * from this software without specific prior written permission.
180Sstevel@tonic-gate *
190Sstevel@tonic-gate */
200Sstevel@tonic-gate
210Sstevel@tonic-gate #define _FILE_OFFSET_BITS 64
220Sstevel@tonic-gate
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate * rcp
250Sstevel@tonic-gate */
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/file.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/time.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/ioctl.h>
320Sstevel@tonic-gate #include <sys/acl.h>
330Sstevel@tonic-gate #include <dirent.h>
340Sstevel@tonic-gate #include <signal.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <netinet/in.h>
370Sstevel@tonic-gate #include <pwd.h>
380Sstevel@tonic-gate #include <netdb.h>
390Sstevel@tonic-gate #include <wchar.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <locale.h>
430Sstevel@tonic-gate #include <strings.h>
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <ctype.h>
460Sstevel@tonic-gate #include <fcntl.h>
470Sstevel@tonic-gate #include <unistd.h>
480Sstevel@tonic-gate #include <limits.h>
490Sstevel@tonic-gate #include <priv_utils.h>
500Sstevel@tonic-gate #include <sys/sendfile.h>
510Sstevel@tonic-gate #include <sys/sysmacros.h>
520Sstevel@tonic-gate #include <sys/wait.h>
53789Sahrens #include <aclutils.h>
544619Ssn199410 #include <sys/varargs.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * It seems like Berkeley got these from pathnames.h?
580Sstevel@tonic-gate */
590Sstevel@tonic-gate #define _PATH_RSH "/usr/bin/rsh"
600Sstevel@tonic-gate #define _PATH_CP "/usr/bin/cp"
610Sstevel@tonic-gate
620Sstevel@tonic-gate #define ACL_FAIL 1
630Sstevel@tonic-gate #define ACL_OK 0
640Sstevel@tonic-gate #define RCP_BUFSIZE (64 * 1024)
650Sstevel@tonic-gate
660Sstevel@tonic-gate #define RCP_ACL "/usr/lib/sunw,rcp"
670Sstevel@tonic-gate /* see PSARC/1993/004/opinion */
680Sstevel@tonic-gate
690Sstevel@tonic-gate typedef struct _buf {
700Sstevel@tonic-gate int cnt;
710Sstevel@tonic-gate char *buf;
720Sstevel@tonic-gate } BUF;
730Sstevel@tonic-gate
740Sstevel@tonic-gate static char *cmd_sunw;
750Sstevel@tonic-gate static struct passwd *pwd;
760Sstevel@tonic-gate static int errs;
770Sstevel@tonic-gate static int pflag;
780Sstevel@tonic-gate static uid_t userid;
790Sstevel@tonic-gate static int rem;
800Sstevel@tonic-gate static int zflag;
810Sstevel@tonic-gate static int iamremote;
820Sstevel@tonic-gate static int iamrecursive;
830Sstevel@tonic-gate static int targetshouldbedirectory;
840Sstevel@tonic-gate static int aclflag;
85789Sahrens static int acl_aclflag;
860Sstevel@tonic-gate static int retval = 0;
870Sstevel@tonic-gate static int portnumber = 0;
880Sstevel@tonic-gate
890Sstevel@tonic-gate static void lostconn(void);
900Sstevel@tonic-gate static char *search_char(unsigned char *, unsigned char);
910Sstevel@tonic-gate static char *removebrackets(char *);
920Sstevel@tonic-gate static char *colon(char *);
930Sstevel@tonic-gate static int response(void);
940Sstevel@tonic-gate static void usage(void);
950Sstevel@tonic-gate static void source(int, char **);
960Sstevel@tonic-gate static void sink(int, char **);
970Sstevel@tonic-gate static void toremote(char *, int, char **);
980Sstevel@tonic-gate static void tolocal(int, char **);
990Sstevel@tonic-gate static void verifydir(char *);
1000Sstevel@tonic-gate static int okname(char *);
1014619Ssn199410 static int susystem(char *, char **);
1020Sstevel@tonic-gate static void rsource(char *, struct stat *);
1030Sstevel@tonic-gate static int sendacl(int);
1040Sstevel@tonic-gate static int recvacl(int, int, int);
1050Sstevel@tonic-gate static int zwrite(int, char *, int);
1060Sstevel@tonic-gate static void zopen(int, int);
1070Sstevel@tonic-gate static int zclose(int);
1080Sstevel@tonic-gate static int notzero(char *, int);
1090Sstevel@tonic-gate static BUF *allocbuf(BUF *, int, int);
1100Sstevel@tonic-gate static void error(char *fmt, ...);
1114619Ssn199410 static void addargs(char **, ...);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * As a 32 bit application, we can only transfer (2gb - 1) i.e 0x7FFFFFFF
1150Sstevel@tonic-gate * bytes of data. We would like the size to be aligned to the nearest
1160Sstevel@tonic-gate * MAXBOFFSET (8192) boundary for optimal performance.
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate #define SENDFILE_SIZE 0x7FFFE000
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate #include <k5-int.h>
1210Sstevel@tonic-gate #include <profile/prof_int.h>
1220Sstevel@tonic-gate #include <com_err.h>
1230Sstevel@tonic-gate #include <kcmd.h>
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate #define NULLBUF (BUF *) 0
1264619Ssn199410 #define MAXARGS 10 /* Number of arguments passed to execv() */
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate static int sock;
1290Sstevel@tonic-gate static char *cmd, *cmd_orig, *cmd_sunw_orig;
1300Sstevel@tonic-gate static char *krb_realm = NULL;
1310Sstevel@tonic-gate static char *krb_cache = NULL;
1320Sstevel@tonic-gate static char *krb_config = NULL;
1330Sstevel@tonic-gate static char des_inbuf[2 * RCP_BUFSIZE];
1340Sstevel@tonic-gate /* needs to be > largest read size */
1350Sstevel@tonic-gate static char des_outbuf[2 * RCP_BUFSIZE];
1360Sstevel@tonic-gate /* needs to be > largest write size */
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf;
1390Sstevel@tonic-gate static krb5_encrypt_block eblock; /* eblock for encrypt/decrypt */
1400Sstevel@tonic-gate static krb5_keyblock *session_key; /* static key for session */
1418175SPeter.Shoults@Sun.COM static krb5_context bsd_context = NULL;
1420Sstevel@tonic-gate static krb5_auth_context auth_context;
1430Sstevel@tonic-gate static krb5_flags authopts;
1440Sstevel@tonic-gate static krb5_error_code status;
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate static void try_normal_rcp(int, char **);
1470Sstevel@tonic-gate static int init_service(int);
1480Sstevel@tonic-gate static char **save_argv(int, char **);
1490Sstevel@tonic-gate static void answer_auth(char *, char *);
1500Sstevel@tonic-gate static int desrcpwrite(int, char *, int);
1510Sstevel@tonic-gate static int desrcpread(int, char *, int);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Not sure why these two don't have their own header file declarations, but
1550Sstevel@tonic-gate * lint complains about absent declarations so place some here. Sigh.
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate extern errcode_t profile_get_options_boolean(profile_t, char **,
1580Sstevel@tonic-gate profile_options_boolean *);
1590Sstevel@tonic-gate extern errcode_t profile_get_options_string(profile_t, char **,
1600Sstevel@tonic-gate profile_option_strings *);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate static int krb5auth_flag = 0; /* Flag set, when KERBEROS is enabled */
1638175SPeter.Shoults@Sun.COM static profile_options_boolean autologin_option[] = {
1648175SPeter.Shoults@Sun.COM { "autologin", &krb5auth_flag, 0 },
1658175SPeter.Shoults@Sun.COM { NULL, NULL, 0 }
1668175SPeter.Shoults@Sun.COM };
1678175SPeter.Shoults@Sun.COM static int no_krb5auth_flag = 0;
1688175SPeter.Shoults@Sun.COM
1690Sstevel@tonic-gate static int encrypt_flag = 0; /* Flag set, when encryption is enabled */
1700Sstevel@tonic-gate static int encrypt_done = 0; /* Flag set, if "-x" is specified */
1710Sstevel@tonic-gate static enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
1740Sstevel@tonic-gate static boolean_t rcmdoption_done = B_FALSE;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate static profile_options_boolean option[] = {
1770Sstevel@tonic-gate { "encrypt", &encrypt_flag, 0 },
1780Sstevel@tonic-gate { NULL, NULL, 0 }
1790Sstevel@tonic-gate };
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate static char *rcmdproto = NULL;
1820Sstevel@tonic-gate static profile_option_strings rcmdversion[] = {
1830Sstevel@tonic-gate { "rcmd_protocol", &rcmdproto, 0 },
1840Sstevel@tonic-gate { NULL, NULL, 0 }
1850Sstevel@tonic-gate };
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, "rcp", NULL };
1880Sstevel@tonic-gate static char *appdef[] = { "appdefaults", "rcp", NULL };
1890Sstevel@tonic-gate static char **prev_argv;
1900Sstevel@tonic-gate static int prev_argc;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate int
main(int argc,char * argv[])1930Sstevel@tonic-gate main(int argc, char *argv[])
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate int ch, fflag, tflag;
1960Sstevel@tonic-gate char *targ;
1970Sstevel@tonic-gate size_t cmdsiz;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (strcmp(argv[0], RCP_ACL) == 0)
2020Sstevel@tonic-gate aclflag = 1;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate if (!(pwd = getpwuid(userid = getuid()))) {
2050Sstevel@tonic-gate (void) fprintf(stderr, "rcp: unknown user %d.\n",
2060Sstevel@tonic-gate (uint_t)userid);
2070Sstevel@tonic-gate return (1);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate fflag = tflag = 0;
2118175SPeter.Shoults@Sun.COM while ((ch = getopt(argc, argv, "axdfprtz:D:k:P:ZK")) != EOF) {
2120Sstevel@tonic-gate switch (ch) {
2130Sstevel@tonic-gate case 'd':
2140Sstevel@tonic-gate targetshouldbedirectory = 1;
2150Sstevel@tonic-gate break;
2160Sstevel@tonic-gate case 'f': /* "from" */
2170Sstevel@tonic-gate fflag = 1;
218789Sahrens if (aclflag | acl_aclflag)
2190Sstevel@tonic-gate /* ok response */
2200Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
2210Sstevel@tonic-gate break;
2220Sstevel@tonic-gate case 'p': /* preserve access/mod times */
2230Sstevel@tonic-gate ++pflag;
2240Sstevel@tonic-gate break;
2250Sstevel@tonic-gate case 'r':
2260Sstevel@tonic-gate ++iamrecursive;
2270Sstevel@tonic-gate break;
2280Sstevel@tonic-gate case 't': /* "to" */
2290Sstevel@tonic-gate tflag = 1;
2300Sstevel@tonic-gate break;
231789Sahrens case 'Z':
232789Sahrens acl_aclflag++;
233789Sahrens break;
2348175SPeter.Shoults@Sun.COM case 'K':
2358175SPeter.Shoults@Sun.COM no_krb5auth_flag++;
2368175SPeter.Shoults@Sun.COM break;
2370Sstevel@tonic-gate case 'x':
2380Sstevel@tonic-gate if (!krb5_privacy_allowed()) {
2390Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: "
240*11851SMilan.Jurik@Sun.COM "Encryption not supported.\n"));
2410Sstevel@tonic-gate return (1);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate encrypt_flag++;
2440Sstevel@tonic-gate krb5auth_flag++;
2450Sstevel@tonic-gate encrypt_done++;
2460Sstevel@tonic-gate break;
2470Sstevel@tonic-gate case 'k':
2480Sstevel@tonic-gate if ((krb_realm = (char *)strdup(optarg)) == NULL) {
2490Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp:"
250*11851SMilan.Jurik@Sun.COM " Cannot malloc.\n"));
2510Sstevel@tonic-gate return (1);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate krb5auth_flag++;
2540Sstevel@tonic-gate break;
2550Sstevel@tonic-gate case 'P':
2560Sstevel@tonic-gate if (strncmp(optarg, "O", 1) == 0) {
2570Sstevel@tonic-gate if (rcmdoption_done == B_TRUE) {
2580Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: "
259*11851SMilan.Jurik@Sun.COM "Only one of -PN and -PO "
260*11851SMilan.Jurik@Sun.COM "allowed.\n"));
2610Sstevel@tonic-gate usage();
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate kcmd_proto = KCMD_OLD_PROTOCOL;
2640Sstevel@tonic-gate rcmdoption_done = B_TRUE;
2650Sstevel@tonic-gate } else if (strncmp(optarg, "N", 1) == 0) {
2660Sstevel@tonic-gate if (rcmdoption_done == B_TRUE) {
2670Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: "
268*11851SMilan.Jurik@Sun.COM "Only one of -PN and -PO "
269*11851SMilan.Jurik@Sun.COM "allowed.\n"));
2700Sstevel@tonic-gate usage();
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate kcmd_proto = KCMD_NEW_PROTOCOL;
2730Sstevel@tonic-gate rcmdoption_done = B_TRUE;
2740Sstevel@tonic-gate } else {
2750Sstevel@tonic-gate usage();
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate krb5auth_flag++;
2780Sstevel@tonic-gate break;
2790Sstevel@tonic-gate case 'a':
2800Sstevel@tonic-gate krb5auth_flag++;
2810Sstevel@tonic-gate break;
2820Sstevel@tonic-gate #ifdef DEBUG
2830Sstevel@tonic-gate case 'D':
2840Sstevel@tonic-gate portnumber = htons(atoi(optarg));
2850Sstevel@tonic-gate krb5auth_flag++;
2860Sstevel@tonic-gate break;
2870Sstevel@tonic-gate #endif /* DEBUG */
2880Sstevel@tonic-gate case '?':
2890Sstevel@tonic-gate default:
2900Sstevel@tonic-gate usage();
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate argc -= optind;
2940Sstevel@tonic-gate argv += optind;
2950Sstevel@tonic-gate
2968175SPeter.Shoults@Sun.COM /*
2978175SPeter.Shoults@Sun.COM * if the user disables krb5 on the cmdline (-K), then skip
2988175SPeter.Shoults@Sun.COM * all krb5 setup.
2998175SPeter.Shoults@Sun.COM *
3008175SPeter.Shoults@Sun.COM * if the user does not disable krb5 or enable krb5 on the
3018175SPeter.Shoults@Sun.COM * cmdline, check krb5.conf to see if it should be enabled.
3028175SPeter.Shoults@Sun.COM */
3038175SPeter.Shoults@Sun.COM
3048175SPeter.Shoults@Sun.COM if (no_krb5auth_flag) {
3058175SPeter.Shoults@Sun.COM krb5auth_flag = 0;
3068175SPeter.Shoults@Sun.COM fflag = encrypt_flag = 0;
3078175SPeter.Shoults@Sun.COM } else if (!krb5auth_flag) {
3088175SPeter.Shoults@Sun.COM /* is autologin set in krb5.conf? */
3090Sstevel@tonic-gate status = krb5_init_context(&bsd_context);
3108175SPeter.Shoults@Sun.COM /* don't sweat failure here */
3118175SPeter.Shoults@Sun.COM if (!status) {
3128175SPeter.Shoults@Sun.COM /*
3138175SPeter.Shoults@Sun.COM * note that the call to profile_get_options_boolean
3148175SPeter.Shoults@Sun.COM * with autologin_option can affect value of
3158175SPeter.Shoults@Sun.COM * krb5auth_flag
3168175SPeter.Shoults@Sun.COM */
3178175SPeter.Shoults@Sun.COM (void) profile_get_options_boolean(bsd_context->profile,
318*11851SMilan.Jurik@Sun.COM appdef,
319*11851SMilan.Jurik@Sun.COM autologin_option);
3208175SPeter.Shoults@Sun.COM }
3218175SPeter.Shoults@Sun.COM }
3228175SPeter.Shoults@Sun.COM
3238175SPeter.Shoults@Sun.COM if (krb5auth_flag > 0) {
3248175SPeter.Shoults@Sun.COM if (!bsd_context) {
3258175SPeter.Shoults@Sun.COM status = krb5_init_context(&bsd_context);
3268175SPeter.Shoults@Sun.COM if (status) {
3278175SPeter.Shoults@Sun.COM com_err("rcp", status,
3288175SPeter.Shoults@Sun.COM gettext("while initializing krb5"));
3298175SPeter.Shoults@Sun.COM return (1);
3308175SPeter.Shoults@Sun.COM }
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate * Set up buffers for desread and deswrite.
3350Sstevel@tonic-gate */
3360Sstevel@tonic-gate desinbuf.data = des_inbuf;
3370Sstevel@tonic-gate desoutbuf.data = des_outbuf;
3380Sstevel@tonic-gate desinbuf.length = sizeof (des_inbuf);
3390Sstevel@tonic-gate desoutbuf.length = sizeof (des_outbuf);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate if (fflag || tflag)
3430Sstevel@tonic-gate if (encrypt_flag > 0)
3440Sstevel@tonic-gate (void) answer_auth(krb_config, krb_cache);
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate if (fflag) {
3470Sstevel@tonic-gate iamremote = 1;
3480Sstevel@tonic-gate (void) response();
3490Sstevel@tonic-gate (void) setuid(userid);
3500Sstevel@tonic-gate source(argc, argv);
3510Sstevel@tonic-gate return (errs);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if (tflag) {
3550Sstevel@tonic-gate iamremote = 1;
3560Sstevel@tonic-gate (void) setuid(userid);
3570Sstevel@tonic-gate sink(argc, argv);
3580Sstevel@tonic-gate return (errs);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate if (argc < 2)
3620Sstevel@tonic-gate usage();
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate /* This will make "rcmd_af()" magically get the proper privilege */
3650Sstevel@tonic-gate if (__init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL) == -1) {
3660Sstevel@tonic-gate (void) fprintf(stderr, "rcp: must be set-uid root\n");
3670Sstevel@tonic-gate exit(1);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if (krb5auth_flag > 0) {
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate * Get our local realm to look up local realm options.
3730Sstevel@tonic-gate */
3740Sstevel@tonic-gate status = krb5_get_default_realm(bsd_context, &realmdef[1]);
3750Sstevel@tonic-gate if (status) {
3760Sstevel@tonic-gate com_err("rcp", status,
377*11851SMilan.Jurik@Sun.COM gettext("while getting default realm"));
3780Sstevel@tonic-gate return (1);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate * See if encryption should be done for this realm
3820Sstevel@tonic-gate */
38311415SSurya.Prakki@Sun.COM (void) profile_get_options_boolean(bsd_context->profile,
38411415SSurya.Prakki@Sun.COM realmdef, option);
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate * Check the appdefaults section
3870Sstevel@tonic-gate */
38811415SSurya.Prakki@Sun.COM (void) profile_get_options_boolean(bsd_context->profile,
38911415SSurya.Prakki@Sun.COM appdef, option);
39011415SSurya.Prakki@Sun.COM (void) profile_get_options_string(bsd_context->profile,
39111415SSurya.Prakki@Sun.COM appdef, rcmdversion);
3920Sstevel@tonic-gate if ((encrypt_done > 0) || (encrypt_flag > 0)) {
3930Sstevel@tonic-gate if (krb5_privacy_allowed() == TRUE) {
3940Sstevel@tonic-gate encrypt_flag++;
3950Sstevel@tonic-gate } else {
3960Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Encryption"
397*11851SMilan.Jurik@Sun.COM " not supported.\n"));
3980Sstevel@tonic-gate return (1);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate if ((rcmdoption_done == B_FALSE) && (rcmdproto != NULL)) {
4030Sstevel@tonic-gate if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
4040Sstevel@tonic-gate kcmd_proto = KCMD_NEW_PROTOCOL;
4050Sstevel@tonic-gate } else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
4060Sstevel@tonic-gate kcmd_proto = KCMD_OLD_PROTOCOL;
4070Sstevel@tonic-gate } else {
4080Sstevel@tonic-gate (void) fprintf(stderr, gettext("Unrecognized "
409*11851SMilan.Jurik@Sun.COM "KCMD protocol (%s)"), rcmdproto);
4100Sstevel@tonic-gate return (1);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate if (argc > 2)
4160Sstevel@tonic-gate targetshouldbedirectory = 1;
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate rem = -1;
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate if (portnumber == 0) {
4210Sstevel@tonic-gate if (krb5auth_flag > 0) {
4220Sstevel@tonic-gate retval = init_service(krb5auth_flag);
4230Sstevel@tonic-gate if (!retval) {
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate * Connecting to the kshell service failed,
4260Sstevel@tonic-gate * fallback to normal rcp & reset KRB5 flags.
4270Sstevel@tonic-gate */
4280Sstevel@tonic-gate krb5auth_flag = encrypt_flag = 0;
4290Sstevel@tonic-gate encrypt_done = 0;
4300Sstevel@tonic-gate (void) init_service(krb5auth_flag);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate else
4340Sstevel@tonic-gate (void) init_service(krb5auth_flag);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate #ifdef DEBUG
4380Sstevel@tonic-gate if (retval || krb5auth_flag) {
4390Sstevel@tonic-gate (void) fprintf(stderr, gettext("Kerberized rcp session, "
440*11851SMilan.Jurik@Sun.COM "port %d in use "), portnumber);
4410Sstevel@tonic-gate if (kcmd_proto == KCMD_OLD_PROTOCOL)
4420Sstevel@tonic-gate (void) fprintf(stderr, gettext("[kcmd ver.1]\n"));
4430Sstevel@tonic-gate else
4440Sstevel@tonic-gate (void) fprintf(stderr, gettext("[kcmd ver.2]\n"));
4450Sstevel@tonic-gate } else {
4460Sstevel@tonic-gate (void) fprintf(stderr, gettext("Normal rcp session, port %d "
447*11851SMilan.Jurik@Sun.COM "in use.\n"), portnumber);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate #endif /* DEBUG */
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate if (krb5auth_flag > 0) {
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate * We calculate here a buffer size that can be used in the
4540Sstevel@tonic-gate * allocation of the three buffers cmd, cmd_orig and
4550Sstevel@tonic-gate * cmd_sunw_orig that are used to hold different incantations
4560Sstevel@tonic-gate * of rcp.
4570Sstevel@tonic-gate */
4584619Ssn199410 cmdsiz = MAX(sizeof ("-x rcp -r -p -d -k ") +
4590Sstevel@tonic-gate strlen(krb_realm != NULL ? krb_realm : ""),
4600Sstevel@tonic-gate sizeof (RCP_ACL " -r -p -z -d"));
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate if (((cmd = (char *)malloc(cmdsiz)) == NULL) ||
463*11851SMilan.Jurik@Sun.COM ((cmd_sunw_orig = (char *)malloc(cmdsiz)) == NULL) ||
464*11851SMilan.Jurik@Sun.COM ((cmd_orig = (char *)malloc(cmdsiz)) == NULL)) {
4650Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot "
466*11851SMilan.Jurik@Sun.COM "malloc.\n"));
4670Sstevel@tonic-gate return (1);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate (void) snprintf(cmd, cmdsiz, "%srcp %s%s%s%s%s",
471*11851SMilan.Jurik@Sun.COM encrypt_flag ? "-x " : "",
472*11851SMilan.Jurik@Sun.COM iamrecursive ? " -r" : "", pflag ? " -p" : "",
473*11851SMilan.Jurik@Sun.COM targetshouldbedirectory ? " -d" : "",
474*11851SMilan.Jurik@Sun.COM krb_realm != NULL ? " -k " : "",
475*11851SMilan.Jurik@Sun.COM krb_realm != NULL ? krb_realm : "");
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * We would use cmd-orig as the 'cmd-buffer' if kerberized
4790Sstevel@tonic-gate * rcp fails, in which case we fallback to normal rcp. We also
4800Sstevel@tonic-gate * save argc & argv for the same purpose
4810Sstevel@tonic-gate */
4820Sstevel@tonic-gate (void) snprintf(cmd_orig, cmdsiz, "rcp%s%s%s%s",
483*11851SMilan.Jurik@Sun.COM iamrecursive ? " -r" : "",
484*11851SMilan.Jurik@Sun.COM pflag ? " -p" : "",
485*11851SMilan.Jurik@Sun.COM zflag ? " -z" : "",
486*11851SMilan.Jurik@Sun.COM targetshouldbedirectory ? " -d" : "");
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate (void) snprintf(cmd_sunw_orig, cmdsiz, "%s%s%s%s%s", RCP_ACL,
489*11851SMilan.Jurik@Sun.COM iamrecursive ? " -r" : "",
490*11851SMilan.Jurik@Sun.COM pflag ? " -p" : "",
491*11851SMilan.Jurik@Sun.COM zflag ? " -z" : "",
492*11851SMilan.Jurik@Sun.COM targetshouldbedirectory ? " -d" : "");
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate prev_argc = argc;
4950Sstevel@tonic-gate prev_argv = save_argv(argc, argv);
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate } else {
4980Sstevel@tonic-gate cmdsiz = sizeof ("rcp -r -p -z -d");
4990Sstevel@tonic-gate if (((cmd = (char *)malloc(cmdsiz)) == NULL)) {
5000Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot "
501*11851SMilan.Jurik@Sun.COM "malloc.\n"));
5020Sstevel@tonic-gate return (1);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate (void) snprintf(cmd, cmdsiz, "rcp%s%s%s%s",
506*11851SMilan.Jurik@Sun.COM iamrecursive ? " -r" : "",
507*11851SMilan.Jurik@Sun.COM pflag ? " -p" : "",
508*11851SMilan.Jurik@Sun.COM zflag ? " -z" : "",
509*11851SMilan.Jurik@Sun.COM targetshouldbedirectory ? " -d" : "");
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate cmdsiz = sizeof (RCP_ACL " -r -p -z -d");
5130Sstevel@tonic-gate if ((cmd_sunw = (char *)malloc(cmdsiz)) == NULL) {
5140Sstevel@tonic-gate (void) fprintf(stderr, gettext("rcp: Cannot malloc.\n"));
5150Sstevel@tonic-gate return (1);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate (void) snprintf(cmd_sunw, cmdsiz, "%s%s%s%s%s", RCP_ACL,
5190Sstevel@tonic-gate iamrecursive ? " -r" : "",
5200Sstevel@tonic-gate pflag ? " -p" : "",
5210Sstevel@tonic-gate zflag ? " -z" : "",
5220Sstevel@tonic-gate targetshouldbedirectory ? " -d" : "");
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate (void) signal(SIGPIPE, (void (*)(int))lostconn);
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate if (targ = colon(argv[argc - 1]))
5270Sstevel@tonic-gate toremote(targ, argc, argv);
5280Sstevel@tonic-gate else {
5290Sstevel@tonic-gate tolocal(argc, argv);
5300Sstevel@tonic-gate if (targetshouldbedirectory)
5310Sstevel@tonic-gate verifydir(argv[argc - 1]);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate return (errs > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate static void
toremote(char * targ,int argc,char * argv[])5390Sstevel@tonic-gate toremote(char *targ, int argc, char *argv[])
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate int i;
5420Sstevel@tonic-gate char *host, *src, *suser, *thost, *tuser;
5430Sstevel@tonic-gate char resp;
5440Sstevel@tonic-gate size_t buffersize;
5450Sstevel@tonic-gate char bp[RCP_BUFSIZE];
5460Sstevel@tonic-gate krb5_creds *cred;
5474619Ssn199410 char *arglist[MAXARGS+1];
5480Sstevel@tonic-gate buffersize = RCP_BUFSIZE;
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate *targ++ = 0;
5510Sstevel@tonic-gate if (*targ == 0)
5520Sstevel@tonic-gate targ = ".";
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate if (thost = search_char((unsigned char *)argv[argc - 1], '@')) {
5550Sstevel@tonic-gate *thost++ = 0;
5560Sstevel@tonic-gate tuser = argv[argc - 1];
5570Sstevel@tonic-gate if (*tuser == '\0')
5580Sstevel@tonic-gate tuser = NULL;
5590Sstevel@tonic-gate else if (!okname(tuser))
5600Sstevel@tonic-gate exit(1);
5610Sstevel@tonic-gate } else {
5620Sstevel@tonic-gate thost = argv[argc - 1];
5630Sstevel@tonic-gate tuser = NULL;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate thost = removebrackets(thost);
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) {
5680Sstevel@tonic-gate src = colon(argv[i]);
5690Sstevel@tonic-gate if (src) { /* remote to remote */
5700Sstevel@tonic-gate *src++ = 0;
5710Sstevel@tonic-gate if (*src == 0)
5720Sstevel@tonic-gate src = ".";
5730Sstevel@tonic-gate host = search_char((unsigned char *)argv[i], '@');
5740Sstevel@tonic-gate if (host) {
5750Sstevel@tonic-gate *host++ = 0;
5760Sstevel@tonic-gate host = removebrackets(host);
5770Sstevel@tonic-gate suser = argv[i];
5780Sstevel@tonic-gate if (*suser == '\0') {
5790Sstevel@tonic-gate suser = pwd->pw_name;
5800Sstevel@tonic-gate } else if (!okname(suser)) {
5810Sstevel@tonic-gate errs++;
5820Sstevel@tonic-gate continue;
5830Sstevel@tonic-gate }
5844619Ssn199410 (void) snprintf(bp, buffersize, "'%s%s%s:%s'",
5850Sstevel@tonic-gate tuser ? tuser : "", tuser ? "@" : "",
5860Sstevel@tonic-gate thost, targ);
5874619Ssn199410 (void) addargs(arglist, "rsh", host, "-l",
5884619Ssn199410 suser, "-n", cmd, src, bp, (char *)NULL);
5890Sstevel@tonic-gate } else {
5900Sstevel@tonic-gate host = removebrackets(argv[i]);
5914619Ssn199410 (void) snprintf(bp, buffersize, "'%s%s%s:%s'",
5924619Ssn199410 tuser ? tuser : "", tuser ? "@" : "",
5934619Ssn199410 thost, targ);
5944619Ssn199410 (void) addargs(arglist, "rsh", host, "-n", cmd,
5954619Ssn199410 src, bp, (char *)NULL);
5960Sstevel@tonic-gate }
5974619Ssn199410 if (susystem(_PATH_RSH, arglist) == -1)
5980Sstevel@tonic-gate errs++;
5990Sstevel@tonic-gate } else { /* local to remote */
6000Sstevel@tonic-gate if (rem == -1) {
6010Sstevel@tonic-gate host = thost;
6020Sstevel@tonic-gate if (krb5auth_flag > 0) {
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate (void) snprintf(bp, buffersize,
605*11851SMilan.Jurik@Sun.COM "%s -t %s", cmd, targ);
6060Sstevel@tonic-gate authopts = AP_OPTS_MUTUAL_REQUIRED;
6070Sstevel@tonic-gate status = kcmd(&sock, &host,
608*11851SMilan.Jurik@Sun.COM portnumber,
609*11851SMilan.Jurik@Sun.COM pwd->pw_name,
610*11851SMilan.Jurik@Sun.COM tuser ? tuser :
611*11851SMilan.Jurik@Sun.COM pwd->pw_name,
612*11851SMilan.Jurik@Sun.COM bp,
613*11851SMilan.Jurik@Sun.COM 0,
614*11851SMilan.Jurik@Sun.COM "host",
615*11851SMilan.Jurik@Sun.COM krb_realm,
616*11851SMilan.Jurik@Sun.COM bsd_context,
617*11851SMilan.Jurik@Sun.COM &auth_context,
618*11851SMilan.Jurik@Sun.COM &cred,
619*11851SMilan.Jurik@Sun.COM 0, /* No seq # */
620*11851SMilan.Jurik@Sun.COM 0, /* No server seq # */
621*11851SMilan.Jurik@Sun.COM authopts,
622*11851SMilan.Jurik@Sun.COM 0, /* Not any port # */
623*11851SMilan.Jurik@Sun.COM &kcmd_proto);
6240Sstevel@tonic-gate if (status) {
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate * If new protocol requested, we dont
6270Sstevel@tonic-gate * fallback to less secure ones.
6280Sstevel@tonic-gate */
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6310Sstevel@tonic-gate (void) fprintf(stderr,
632*11851SMilan.Jurik@Sun.COM gettext("rcp: kcmdv2 "
633*11851SMilan.Jurik@Sun.COM "to host %s failed - %s"
634*11851SMilan.Jurik@Sun.COM "\nFallback to normal "
635*11851SMilan.Jurik@Sun.COM "rcp denied."), host,
636*11851SMilan.Jurik@Sun.COM error_message(status));
6370Sstevel@tonic-gate exit(1);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate if (status != -1) {
6400Sstevel@tonic-gate (void) fprintf(stderr,
641*11851SMilan.Jurik@Sun.COM gettext("rcp: kcmd to host "
642*11851SMilan.Jurik@Sun.COM "%s failed - %s,\n"
643*11851SMilan.Jurik@Sun.COM "trying normal rcp...\n\n"),
644*11851SMilan.Jurik@Sun.COM host,
645*11851SMilan.Jurik@Sun.COM error_message(status));
6460Sstevel@tonic-gate } else {
6470Sstevel@tonic-gate (void) fprintf(stderr,
648*11851SMilan.Jurik@Sun.COM gettext("trying normal"
649*11851SMilan.Jurik@Sun.COM " rcp...\n"));
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate * kcmd() failed, so we have to
6530Sstevel@tonic-gate * fallback to normal rcp
6540Sstevel@tonic-gate */
6550Sstevel@tonic-gate try_normal_rcp(prev_argc, prev_argv);
6560Sstevel@tonic-gate } else {
6570Sstevel@tonic-gate rem = sock;
6580Sstevel@tonic-gate session_key = &cred->keyblock;
6590Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) {
6600Sstevel@tonic-gate /* CSTYLED */
6610Sstevel@tonic-gate status = krb5_auth_con_getlocalsubkey(bsd_context, auth_context, &session_key);
6620Sstevel@tonic-gate if (status) {
6630Sstevel@tonic-gate com_err("rcp", status,
664*11851SMilan.Jurik@Sun.COM "determining "
665*11851SMilan.Jurik@Sun.COM "subkey for "
666*11851SMilan.Jurik@Sun.COM "session");
6670Sstevel@tonic-gate exit(1);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate if (!session_key) {
6700Sstevel@tonic-gate com_err("rcp", 0,
671*11851SMilan.Jurik@Sun.COM "no subkey "
672*11851SMilan.Jurik@Sun.COM "negotiated for"
673*11851SMilan.Jurik@Sun.COM " connection");
6740Sstevel@tonic-gate exit(1);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate eblock.crypto_entry =
678*11851SMilan.Jurik@Sun.COM session_key->enctype;
6790Sstevel@tonic-gate eblock.key =
680*11851SMilan.Jurik@Sun.COM (krb5_keyblock *)session_key;
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate init_encrypt(encrypt_flag,
683*11851SMilan.Jurik@Sun.COM bsd_context, kcmd_proto,
684*11851SMilan.Jurik@Sun.COM &desinbuf, &desoutbuf, CLIENT,
685*11851SMilan.Jurik@Sun.COM &eblock);
6860Sstevel@tonic-gate if (encrypt_flag > 0) {
6870Sstevel@tonic-gate char *s = gettext("This rcp "
688*11851SMilan.Jurik@Sun.COM "session is using "
689*11851SMilan.Jurik@Sun.COM "encryption for all "
690*11851SMilan.Jurik@Sun.COM "data transmissions."
691*11851SMilan.Jurik@Sun.COM "\r\n");
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate (void) write(2, s, strlen(s));
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate if (response() < 0)
6970Sstevel@tonic-gate exit(1);
6980Sstevel@tonic-gate
699789Sahrens } else {
7000Sstevel@tonic-gate
701*11851SMilan.Jurik@Sun.COM /*
702*11851SMilan.Jurik@Sun.COM * ACL support: try to find out if the
703*11851SMilan.Jurik@Sun.COM * remote site is running acl cognizant
704*11851SMilan.Jurik@Sun.COM * version of rcp. A special binary
705*11851SMilan.Jurik@Sun.COM * name is used for this purpose.
706*11851SMilan.Jurik@Sun.COM */
707*11851SMilan.Jurik@Sun.COM aclflag = 1;
708*11851SMilan.Jurik@Sun.COM acl_aclflag = 1;
7090Sstevel@tonic-gate
710*11851SMilan.Jurik@Sun.COM /*
711*11851SMilan.Jurik@Sun.COM * First see if the remote side will
712*11851SMilan.Jurik@Sun.COM * support both aclent_t and ace_t
713*11851SMilan.Jurik@Sun.COM * acl's?
714*11851SMilan.Jurik@Sun.COM */
7150Sstevel@tonic-gate (void) snprintf(bp, buffersize,
716*11851SMilan.Jurik@Sun.COM "%s -tZ %s",
717*11851SMilan.Jurik@Sun.COM cmd_sunw, targ);
7180Sstevel@tonic-gate rem = rcmd_af(&host, portnumber,
719789Sahrens pwd->pw_name,
720789Sahrens tuser ? tuser : pwd->pw_name,
721789Sahrens bp, 0, AF_INET6);
7220Sstevel@tonic-gate if (rem < 0)
7230Sstevel@tonic-gate exit(1);
724789Sahrens
725*11851SMilan.Jurik@Sun.COM /*
726*11851SMilan.Jurik@Sun.COM * This is similar to routine
727*11851SMilan.Jurik@Sun.COM * response(). If response is not ok,
728*11851SMilan.Jurik@Sun.COM * treat the other side as non-acl rcp.
729*11851SMilan.Jurik@Sun.COM */
730789Sahrens if (read(rem, &resp, sizeof (resp))
731789Sahrens != sizeof (resp))
732789Sahrens lostconn();
733789Sahrens if (resp != 0) {
734*11851SMilan.Jurik@Sun.COM acl_aclflag = 0;
735789Sahrens (void) snprintf(bp, buffersize,
736*11851SMilan.Jurik@Sun.COM "%s -t %s", cmd_sunw, targ);
737*11851SMilan.Jurik@Sun.COM
738789Sahrens (void) close(rem);
739789Sahrens host = thost;
740789Sahrens rem = rcmd_af(&host, portnumber,
741789Sahrens pwd->pw_name,
742789Sahrens tuser ? tuser :
743*11851SMilan.Jurik@Sun.COM pwd->pw_name,
744*11851SMilan.Jurik@Sun.COM bp, 0, AF_INET6);
745789Sahrens if (rem < 0)
746789Sahrens exit(1);
747*11851SMilan.Jurik@Sun.COM
748*11851SMilan.Jurik@Sun.COM if (read(rem, &resp,
749*11851SMilan.Jurik@Sun.COM sizeof (resp))
750*11851SMilan.Jurik@Sun.COM != sizeof (resp))
751*11851SMilan.Jurik@Sun.COM lostconn();
752*11851SMilan.Jurik@Sun.COM if (resp != 0) {
753*11851SMilan.Jurik@Sun.COM /*
754*11851SMilan.Jurik@Sun.COM * Not OK:
755*11851SMilan.Jurik@Sun.COM * The other side is
756*11851SMilan.Jurik@Sun.COM * running non-acl rcp.
757*11851SMilan.Jurik@Sun.COM * Try again with
758*11851SMilan.Jurik@Sun.COM * normal stuff.
759*11851SMilan.Jurik@Sun.COM */
760*11851SMilan.Jurik@Sun.COM aclflag = 0;
761*11851SMilan.Jurik@Sun.COM (void) snprintf(bp,
762*11851SMilan.Jurik@Sun.COM buffersize,
763*11851SMilan.Jurik@Sun.COM "%s -t %s", cmd,
764*11851SMilan.Jurik@Sun.COM targ);
765*11851SMilan.Jurik@Sun.COM (void) close(rem);
766*11851SMilan.Jurik@Sun.COM host = thost;
767*11851SMilan.Jurik@Sun.COM rem = rcmd_af(&host,
768*11851SMilan.Jurik@Sun.COM portnumber,
769*11851SMilan.Jurik@Sun.COM pwd->pw_name,
770*11851SMilan.Jurik@Sun.COM tuser ? tuser :
771*11851SMilan.Jurik@Sun.COM pwd->pw_name, bp, 0,
772*11851SMilan.Jurik@Sun.COM AF_INET6);
773*11851SMilan.Jurik@Sun.COM if (rem < 0)
774*11851SMilan.Jurik@Sun.COM exit(1);
775*11851SMilan.Jurik@Sun.COM if (response() < 0)
776*11851SMilan.Jurik@Sun.COM exit(1);
777*11851SMilan.Jurik@Sun.COM }
778789Sahrens }
779*11851SMilan.Jurik@Sun.COM /* everything should be fine now */
780*11851SMilan.Jurik@Sun.COM (void) setuid(userid);
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate source(1, argv + i);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate static void
tolocal(int argc,char * argv[])7900Sstevel@tonic-gate tolocal(int argc, char *argv[])
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate int i;
7930Sstevel@tonic-gate char *host, *src, *suser, *lhost;
7940Sstevel@tonic-gate char resp;
7950Sstevel@tonic-gate size_t buffersize;
7960Sstevel@tonic-gate char bp[RCP_BUFSIZE];
7970Sstevel@tonic-gate krb5_creds *cred;
7984619Ssn199410 char *arglist[MAXARGS+1];
7990Sstevel@tonic-gate buffersize = RCP_BUFSIZE;
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) {
8020Sstevel@tonic-gate if (!(src = colon(argv[i]))) { /* local to local */
8034619Ssn199410 (void) addargs(arglist, "cp",
8044619Ssn199410 iamrecursive ? "-r" : "", pflag ? "-p" : "",
8054619Ssn199410 zflag ? "-z" : "", argv[i], argv[argc - 1],
8064619Ssn199410 (char *)NULL);
8074619Ssn199410 if (susystem(_PATH_CP, arglist) == -1)
8080Sstevel@tonic-gate errs++;
8090Sstevel@tonic-gate continue;
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate *src++ = 0;
8120Sstevel@tonic-gate if (*src == 0)
8130Sstevel@tonic-gate src = ".";
8140Sstevel@tonic-gate host = search_char((unsigned char *)argv[i], '@');
8150Sstevel@tonic-gate if (host) {
8160Sstevel@tonic-gate *host++ = 0;
8170Sstevel@tonic-gate suser = argv[i];
8180Sstevel@tonic-gate if (*suser == '\0') {
8190Sstevel@tonic-gate suser = pwd->pw_name;
8200Sstevel@tonic-gate } else if (!okname(suser)) {
8210Sstevel@tonic-gate errs++;
8220Sstevel@tonic-gate continue;
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate } else {
8250Sstevel@tonic-gate host = argv[i];
8260Sstevel@tonic-gate suser = pwd->pw_name;
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate host = removebrackets(host);
8290Sstevel@tonic-gate lhost = host;
8300Sstevel@tonic-gate if (krb5auth_flag > 0) {
8310Sstevel@tonic-gate
832*11851SMilan.Jurik@Sun.COM (void) snprintf(bp, buffersize, "%s -f %s", cmd, src);
833*11851SMilan.Jurik@Sun.COM authopts = AP_OPTS_MUTUAL_REQUIRED;
834*11851SMilan.Jurik@Sun.COM status = kcmd(&sock, &host,
835*11851SMilan.Jurik@Sun.COM portnumber,
836*11851SMilan.Jurik@Sun.COM pwd->pw_name, suser,
837*11851SMilan.Jurik@Sun.COM bp,
838*11851SMilan.Jurik@Sun.COM 0, /* &rfd2 */
839*11851SMilan.Jurik@Sun.COM "host",
840*11851SMilan.Jurik@Sun.COM krb_realm,
841*11851SMilan.Jurik@Sun.COM bsd_context,
842*11851SMilan.Jurik@Sun.COM &auth_context,
843*11851SMilan.Jurik@Sun.COM &cred,
844*11851SMilan.Jurik@Sun.COM 0, /* No seq # */
845*11851SMilan.Jurik@Sun.COM 0, /* No server seq # */
846*11851SMilan.Jurik@Sun.COM authopts,
847*11851SMilan.Jurik@Sun.COM 1, /* Not any port # */
848*11851SMilan.Jurik@Sun.COM &kcmd_proto);
849*11851SMilan.Jurik@Sun.COM if (status) {
850*11851SMilan.Jurik@Sun.COM /*
851*11851SMilan.Jurik@Sun.COM * If new protocol requested, we dont
852*11851SMilan.Jurik@Sun.COM * fallback to less secure ones.
853*11851SMilan.Jurik@Sun.COM */
854*11851SMilan.Jurik@Sun.COM if (kcmd_proto == KCMD_NEW_PROTOCOL) {
855*11851SMilan.Jurik@Sun.COM (void) fprintf(stderr,
856*11851SMilan.Jurik@Sun.COM gettext("rcp: kcmdv2 "
857*11851SMilan.Jurik@Sun.COM "to host %s failed - %s\n"
858*11851SMilan.Jurik@Sun.COM "Fallback to normal rcp denied."),
859*11851SMilan.Jurik@Sun.COM host, error_message(status));
8600Sstevel@tonic-gate exit(1);
8610Sstevel@tonic-gate }
862*11851SMilan.Jurik@Sun.COM if (status != -1) {
863*11851SMilan.Jurik@Sun.COM (void) fprintf(stderr,
864*11851SMilan.Jurik@Sun.COM gettext("rcp: kcmd "
865*11851SMilan.Jurik@Sun.COM "to host %s failed - %s,\n"
866*11851SMilan.Jurik@Sun.COM "trying normal rcp...\n\n"),
867*11851SMilan.Jurik@Sun.COM host, error_message(status));
868*11851SMilan.Jurik@Sun.COM } else {
869*11851SMilan.Jurik@Sun.COM (void) fprintf(stderr,
870*11851SMilan.Jurik@Sun.COM gettext("trying normal rcp...\n"));
871*11851SMilan.Jurik@Sun.COM }
872*11851SMilan.Jurik@Sun.COM /*
873*11851SMilan.Jurik@Sun.COM * kcmd() failed, so we have to
874*11851SMilan.Jurik@Sun.COM * fallback to normal rcp
875*11851SMilan.Jurik@Sun.COM */
876*11851SMilan.Jurik@Sun.COM try_normal_rcp(prev_argc, prev_argv);
877*11851SMilan.Jurik@Sun.COM } else {
878*11851SMilan.Jurik@Sun.COM rem = sock;
879*11851SMilan.Jurik@Sun.COM session_key = &cred->keyblock;
880*11851SMilan.Jurik@Sun.COM if (kcmd_proto == KCMD_NEW_PROTOCOL) {
881*11851SMilan.Jurik@Sun.COM status = krb5_auth_con_getlocalsubkey(
882*11851SMilan.Jurik@Sun.COM bsd_context, auth_context,
883*11851SMilan.Jurik@Sun.COM &session_key);
884*11851SMilan.Jurik@Sun.COM if (status) {
885*11851SMilan.Jurik@Sun.COM com_err("rcp", status,
886*11851SMilan.Jurik@Sun.COM "determining "
887*11851SMilan.Jurik@Sun.COM "subkey for session");
888*11851SMilan.Jurik@Sun.COM exit(1);
889*11851SMilan.Jurik@Sun.COM }
890*11851SMilan.Jurik@Sun.COM if (!session_key) {
891*11851SMilan.Jurik@Sun.COM com_err("rcp", 0,
892*11851SMilan.Jurik@Sun.COM "no subkey negotiated"
893*11851SMilan.Jurik@Sun.COM " for connection");
894*11851SMilan.Jurik@Sun.COM exit(1);
895*11851SMilan.Jurik@Sun.COM }
896*11851SMilan.Jurik@Sun.COM }
897*11851SMilan.Jurik@Sun.COM eblock.crypto_entry = session_key->enctype;
898*11851SMilan.Jurik@Sun.COM eblock.key = (krb5_keyblock *)session_key;
899*11851SMilan.Jurik@Sun.COM
900*11851SMilan.Jurik@Sun.COM init_encrypt(encrypt_flag, bsd_context,
901*11851SMilan.Jurik@Sun.COM kcmd_proto,
902*11851SMilan.Jurik@Sun.COM &desinbuf, &desoutbuf, CLIENT,
903*11851SMilan.Jurik@Sun.COM &eblock);
904*11851SMilan.Jurik@Sun.COM if (encrypt_flag > 0) {
905*11851SMilan.Jurik@Sun.COM char *s = gettext("This rcp "
906*11851SMilan.Jurik@Sun.COM "session is using DES "
907*11851SMilan.Jurik@Sun.COM "encryption for all "
908*11851SMilan.Jurik@Sun.COM "data transmissions."
909*11851SMilan.Jurik@Sun.COM "\r\n");
910*11851SMilan.Jurik@Sun.COM
911*11851SMilan.Jurik@Sun.COM (void) write(2, s, strlen(s));
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate else
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate
919*11851SMilan.Jurik@Sun.COM /*
920*11851SMilan.Jurik@Sun.COM * ACL support: try to find out if the remote site is
921*11851SMilan.Jurik@Sun.COM * running acl cognizant version of rcp.
922*11851SMilan.Jurik@Sun.COM */
923*11851SMilan.Jurik@Sun.COM aclflag = 1;
924*11851SMilan.Jurik@Sun.COM acl_aclflag = 1;
9250Sstevel@tonic-gate
926*11851SMilan.Jurik@Sun.COM (void) snprintf(bp, buffersize, "%s -Zf %s", cmd_sunw,
927*11851SMilan.Jurik@Sun.COM src);
928789Sahrens rem = rcmd_af(&host, portnumber, pwd->pw_name, suser,
929789Sahrens bp, 0, AF_INET6);
930789Sahrens
931789Sahrens if (rem < 0) {
932789Sahrens ++errs;
933789Sahrens continue;
934789Sahrens }
935789Sahrens
936*11851SMilan.Jurik@Sun.COM /*
937*11851SMilan.Jurik@Sun.COM * The remote system is supposed to send an ok response.
938*11851SMilan.Jurik@Sun.COM * If there are any data other than "ok", it must be
939*11851SMilan.Jurik@Sun.COM * error messages from the remote system. We can assume
940*11851SMilan.Jurik@Sun.COM * the remote system is running non-acl version rcp.
941*11851SMilan.Jurik@Sun.COM */
942789Sahrens if (read(rem, &resp, sizeof (resp)) != sizeof (resp))
943789Sahrens lostconn();
944789Sahrens
945*11851SMilan.Jurik@Sun.COM if (resp != 0) {
946*11851SMilan.Jurik@Sun.COM
947*11851SMilan.Jurik@Sun.COM /*
948*11851SMilan.Jurik@Sun.COM * Try again without ace_acl support
949*11851SMilan.Jurik@Sun.COM */
950*11851SMilan.Jurik@Sun.COM acl_aclflag = 0;
951*11851SMilan.Jurik@Sun.COM (void) snprintf(bp, buffersize, "%s -f %s",
952*11851SMilan.Jurik@Sun.COM cmd_sunw, src);
9530Sstevel@tonic-gate (void) close(rem);
9540Sstevel@tonic-gate rem = rcmd_af(&host, portnumber, pwd->pw_name,
955*11851SMilan.Jurik@Sun.COM suser, bp, 0, AF_INET6);
956*11851SMilan.Jurik@Sun.COM
957*11851SMilan.Jurik@Sun.COM if (rem < 0) {
958*11851SMilan.Jurik@Sun.COM ++errs;
959*11851SMilan.Jurik@Sun.COM continue;
960*11851SMilan.Jurik@Sun.COM }
961*11851SMilan.Jurik@Sun.COM
962*11851SMilan.Jurik@Sun.COM if (read(rem, &resp,
963*11851SMilan.Jurik@Sun.COM sizeof (resp)) != sizeof (resp))
964*11851SMilan.Jurik@Sun.COM lostconn();
965*11851SMilan.Jurik@Sun.COM
966*11851SMilan.Jurik@Sun.COM if (resp != 0) {
967*11851SMilan.Jurik@Sun.COM /*
968*11851SMilan.Jurik@Sun.COM * NOT ok:
969*11851SMilan.Jurik@Sun.COM * The other side is running non-acl
970*11851SMilan.Jurik@Sun.COM * rcp. Try again with normal stuff.
971*11851SMilan.Jurik@Sun.COM */
972*11851SMilan.Jurik@Sun.COM aclflag = 0;
973*11851SMilan.Jurik@Sun.COM (void) snprintf(bp, buffersize,
974*11851SMilan.Jurik@Sun.COM "%s -f %s", cmd, src);
975*11851SMilan.Jurik@Sun.COM (void) close(rem);
976*11851SMilan.Jurik@Sun.COM host = lhost;
977*11851SMilan.Jurik@Sun.COM rem = rcmd_af(&host, portnumber,
978*11851SMilan.Jurik@Sun.COM pwd->pw_name, suser, bp, 0,
979*11851SMilan.Jurik@Sun.COM AF_INET6);
980*11851SMilan.Jurik@Sun.COM if (rem < 0) {
981*11851SMilan.Jurik@Sun.COM ++errs;
982*11851SMilan.Jurik@Sun.COM continue;
983*11851SMilan.Jurik@Sun.COM }
984*11851SMilan.Jurik@Sun.COM }
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate sink(1, argv + argc - 1);
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate (void) close(rem);
9910Sstevel@tonic-gate rem = -1;
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate static void
verifydir(char * cp)9970Sstevel@tonic-gate verifydir(char *cp)
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate struct stat stb;
10000Sstevel@tonic-gate
10010Sstevel@tonic-gate if (stat(cp, &stb) >= 0) {
10020Sstevel@tonic-gate if ((stb.st_mode & S_IFMT) == S_IFDIR)
10030Sstevel@tonic-gate return;
10040Sstevel@tonic-gate errno = ENOTDIR;
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate error("rcp: %s: %s.\n", cp, strerror(errno));
10070Sstevel@tonic-gate exit(1);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate
10100Sstevel@tonic-gate static char *
colon(char * cp)10110Sstevel@tonic-gate colon(char *cp)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate boolean_t is_bracket_open = B_FALSE;
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate for (; *cp; ++cp) {
10160Sstevel@tonic-gate if (*cp == '[')
10170Sstevel@tonic-gate is_bracket_open = B_TRUE;
10180Sstevel@tonic-gate else if (*cp == ']')
10190Sstevel@tonic-gate is_bracket_open = B_FALSE;
10200Sstevel@tonic-gate else if (*cp == ':' && !is_bracket_open)
10210Sstevel@tonic-gate return (cp);
10220Sstevel@tonic-gate else if (*cp == '/')
10230Sstevel@tonic-gate return (0);
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate return (0);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate static int
okname(char * cp0)10290Sstevel@tonic-gate okname(char *cp0)
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate register char *cp = cp0;
10320Sstevel@tonic-gate register int c;
10330Sstevel@tonic-gate
10340Sstevel@tonic-gate do {
10350Sstevel@tonic-gate c = *cp;
10360Sstevel@tonic-gate if (c & 0200)
10370Sstevel@tonic-gate goto bad;
10380Sstevel@tonic-gate if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
10390Sstevel@tonic-gate goto bad;
10400Sstevel@tonic-gate } while (*++cp);
10410Sstevel@tonic-gate return (1);
10420Sstevel@tonic-gate bad:
10430Sstevel@tonic-gate (void) fprintf(stderr, "rcp: invalid user name %s\n", cp0);
10440Sstevel@tonic-gate return (0);
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate static char *
removebrackets(char * str)10490Sstevel@tonic-gate removebrackets(char *str)
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate char *newstr = str;
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
10540Sstevel@tonic-gate newstr = str + 1;
10550Sstevel@tonic-gate str[strlen(str) - 1] = '\0';
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate return (newstr);
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate static int
susystem(char * path,char ** arglist)10614619Ssn199410 susystem(char *path, char **arglist)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate int status, pid, w;
10640Sstevel@tonic-gate register void (*istat)(), (*qstat)();
10650Sstevel@tonic-gate int pfds[2];
10660Sstevel@tonic-gate char buf[BUFSIZ];
10670Sstevel@tonic-gate int cnt;
10680Sstevel@tonic-gate boolean_t seen_stderr_traffic;
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate * Due to the fact that rcp uses rsh to copy between 2 remote
10720Sstevel@tonic-gate * machines, rsh doesn't return the exit status of the remote
10730Sstevel@tonic-gate * command, and we can't modify the rcmd protocol used by rsh
10740Sstevel@tonic-gate * (for interoperability reasons) we use the hack of using any
10750Sstevel@tonic-gate * output on stderr as indication that an error occurred and
10760Sstevel@tonic-gate * that we should return a non-zero error code.
10770Sstevel@tonic-gate */
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate if (pipe(pfds) == -1) {
10800Sstevel@tonic-gate (void) fprintf(stderr, "Couldn't create pipe: %s\n",
10810Sstevel@tonic-gate strerror(errno));
10820Sstevel@tonic-gate return (-1);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate if ((pid = vfork()) < 0) {
10860Sstevel@tonic-gate (void) close(pfds[0]);
10870Sstevel@tonic-gate (void) close(pfds[1]);
10880Sstevel@tonic-gate (void) fprintf(stderr, "Couldn't fork child process: %s\n",
10890Sstevel@tonic-gate strerror(errno));
10900Sstevel@tonic-gate return (-1);
10910Sstevel@tonic-gate } else if (pid == 0) {
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate * Child.
10940Sstevel@tonic-gate */
10950Sstevel@tonic-gate (void) close(pfds[0]);
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate * Send stderr messages down the pipe so that we can detect
10980Sstevel@tonic-gate * them in the parent process.
10990Sstevel@tonic-gate */
11000Sstevel@tonic-gate if (pfds[1] != STDERR_FILENO) {
11010Sstevel@tonic-gate (void) dup2(pfds[1], STDERR_FILENO);
11020Sstevel@tonic-gate (void) close(pfds[1]);
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate * This shell does not inherit the additional privilege
11060Sstevel@tonic-gate * we have in our Permitted set.
11070Sstevel@tonic-gate */
11084619Ssn199410 (void) execv(path, arglist);
11090Sstevel@tonic-gate _exit(127);
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate /*
11120Sstevel@tonic-gate * Parent.
11130Sstevel@tonic-gate */
11140Sstevel@tonic-gate istat = signal(SIGINT, SIG_IGN);
11150Sstevel@tonic-gate qstat = signal(SIGQUIT, SIG_IGN);
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate (void) close(pfds[1]);
11180Sstevel@tonic-gate seen_stderr_traffic = B_FALSE;
11190Sstevel@tonic-gate while ((cnt = read(pfds[0], buf, sizeof (buf))) > 0) {
11200Sstevel@tonic-gate /*
11210Sstevel@tonic-gate * If any data is read from the pipe the child process
11220Sstevel@tonic-gate * has output something on stderr so we set the boolean
11230Sstevel@tonic-gate * 'seen_stderr_traffic' to true, which will cause the
11240Sstevel@tonic-gate * function to return -1.
11250Sstevel@tonic-gate */
11260Sstevel@tonic-gate (void) write(STDERR_FILENO, buf, cnt);
11270Sstevel@tonic-gate seen_stderr_traffic = B_TRUE;
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate (void) close(pfds[0]);
11300Sstevel@tonic-gate while ((w = wait(&status)) != pid && w != -1)
11310Sstevel@tonic-gate ;
11320Sstevel@tonic-gate if (w == -1)
11330Sstevel@tonic-gate status = -1;
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate (void) signal(SIGINT, istat);
11360Sstevel@tonic-gate (void) signal(SIGQUIT, qstat);
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate return (seen_stderr_traffic ? -1 : status);
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate static void
source(int argc,char * argv[])11420Sstevel@tonic-gate source(int argc, char *argv[])
11430Sstevel@tonic-gate {
11440Sstevel@tonic-gate struct stat stb;
11450Sstevel@tonic-gate static BUF buffer;
11460Sstevel@tonic-gate BUF *bp;
11470Sstevel@tonic-gate int x, readerr, f, amt;
11480Sstevel@tonic-gate char *last, *name, buf[RCP_BUFSIZE];
11490Sstevel@tonic-gate off_t off, size, i;
11500Sstevel@tonic-gate ssize_t cnt;
11513370Sjs198686 struct linger lingerbuf;
11520Sstevel@tonic-gate
11530Sstevel@tonic-gate for (x = 0; x < argc; x++) {
11540Sstevel@tonic-gate name = argv[x];
11550Sstevel@tonic-gate if ((f = open(name, O_RDONLY, 0)) < 0) {
11560Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno));
11570Sstevel@tonic-gate continue;
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate if (fstat(f, &stb) < 0)
11600Sstevel@tonic-gate goto notreg;
11610Sstevel@tonic-gate switch (stb.st_mode&S_IFMT) {
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate case S_IFREG:
11640Sstevel@tonic-gate break;
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate case S_IFDIR:
11670Sstevel@tonic-gate if (iamrecursive) {
11680Sstevel@tonic-gate (void) close(f);
11690Sstevel@tonic-gate rsource(name, &stb);
11700Sstevel@tonic-gate continue;
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate /* FALLTHROUGH */
11730Sstevel@tonic-gate default:
11740Sstevel@tonic-gate notreg:
11750Sstevel@tonic-gate (void) close(f);
11760Sstevel@tonic-gate error("rcp: %s: not a plain file\n", name);
11770Sstevel@tonic-gate continue;
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate last = rindex(name, '/');
11800Sstevel@tonic-gate if (last == 0)
11810Sstevel@tonic-gate last = name;
11820Sstevel@tonic-gate else
11830Sstevel@tonic-gate last++;
11840Sstevel@tonic-gate if (pflag) {
11850Sstevel@tonic-gate time_t mtime, atime;
11860Sstevel@tonic-gate time_t now;
11870Sstevel@tonic-gate
11880Sstevel@tonic-gate /*
11890Sstevel@tonic-gate * Make it compatible with possible future
11900Sstevel@tonic-gate * versions expecting microseconds.
11910Sstevel@tonic-gate */
11920Sstevel@tonic-gate mtime = stb.st_mtime;
11930Sstevel@tonic-gate atime = stb.st_atime;
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate if ((mtime < 0) || (atime < 0)) {
11960Sstevel@tonic-gate now = time(NULL);
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate if (mtime < 0) {
11990Sstevel@tonic-gate mtime = now;
12000Sstevel@tonic-gate error("negative modification time on "
12010Sstevel@tonic-gate "%s; not preserving\n", name);
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate if (atime < 0) {
12040Sstevel@tonic-gate atime = now;
12050Sstevel@tonic-gate error("negative access time on "
12060Sstevel@tonic-gate "%s; not preserving\n", name);
12070Sstevel@tonic-gate }
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "T%ld 0 %ld 0\n",
1210*11851SMilan.Jurik@Sun.COM mtime, atime);
12110Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf));
12120Sstevel@tonic-gate if (response() < 0) {
12130Sstevel@tonic-gate (void) close(f);
12140Sstevel@tonic-gate continue;
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "C%04o %lld %s\n",
1218*11851SMilan.Jurik@Sun.COM (uint_t)(stb.st_mode & 07777), (longlong_t)stb.st_size,
1219*11851SMilan.Jurik@Sun.COM last);
12200Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf));
12210Sstevel@tonic-gate if (response() < 0) {
12220Sstevel@tonic-gate (void) close(f);
12230Sstevel@tonic-gate continue;
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate /* ACL support: send */
1227789Sahrens if (aclflag | acl_aclflag) {
12280Sstevel@tonic-gate /* get acl from f and send it over */
12290Sstevel@tonic-gate if (sendacl(f) == ACL_FAIL) {
12300Sstevel@tonic-gate (void) close(f);
12310Sstevel@tonic-gate continue;
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate if ((krb5auth_flag > 0) || (iamremote == 1)) {
12350Sstevel@tonic-gate bp = allocbuf(&buffer, f, RCP_BUFSIZE);
12360Sstevel@tonic-gate if (bp == NULLBUF) {
12370Sstevel@tonic-gate (void) close(f);
12380Sstevel@tonic-gate continue;
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate readerr = 0;
12410Sstevel@tonic-gate for (i = 0; i < stb.st_size; i += bp->cnt) {
12420Sstevel@tonic-gate amt = bp->cnt;
12430Sstevel@tonic-gate if (i + amt > stb.st_size)
12440Sstevel@tonic-gate amt = stb.st_size - i;
12450Sstevel@tonic-gate if (readerr == 0 &&
12460Sstevel@tonic-gate read(f, bp->buf, amt) != amt)
12470Sstevel@tonic-gate readerr = errno;
12480Sstevel@tonic-gate (void) desrcpwrite(rem, bp->buf, amt);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate (void) close(f);
12510Sstevel@tonic-gate if (readerr == 0)
12520Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
12530Sstevel@tonic-gate else
12540Sstevel@tonic-gate error("rcp: %s: %s\n", name,
12550Sstevel@tonic-gate error_message(readerr));
12560Sstevel@tonic-gate } else {
12570Sstevel@tonic-gate cnt = off = 0;
12580Sstevel@tonic-gate size = stb.st_size;
12590Sstevel@tonic-gate while (size != 0) {
12600Sstevel@tonic-gate amt = MIN(size, SENDFILE_SIZE);
12610Sstevel@tonic-gate cnt = sendfile(rem, f, &off, amt);
12622039Sblu if (cnt == -1) {
12632039Sblu if (errno == EINTR) {
12642039Sblu continue;
12652039Sblu } else {
12662039Sblu break;
12672039Sblu }
12682039Sblu }
12693370Sjs198686 if (cnt == 0)
12703370Sjs198686 break;
12710Sstevel@tonic-gate size -= cnt;
12720Sstevel@tonic-gate }
12733370Sjs198686 if (cnt < 0) {
12740Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno));
12753370Sjs198686 } else if (cnt == 0 && size != 0) {
12763370Sjs198686 error("rcp: %s: unexpected end of file\n",
1277*11851SMilan.Jurik@Sun.COM name);
12783370Sjs198686 lingerbuf.l_onoff = 1;
12793370Sjs198686 lingerbuf.l_linger = 0;
12803370Sjs198686 (void) setsockopt(rem, SOL_SOCKET, SO_LINGER,
1281*11851SMilan.Jurik@Sun.COM &lingerbuf, sizeof (lingerbuf));
12823370Sjs198686 /*
12833370Sjs198686 * When response() (see below) is invoked it
12843370Sjs198686 * tries to read data from closed handle which
12853370Sjs198686 * triggers error and lostconn() function.
12863370Sjs198686 * lostconn() terminates the program with
12873370Sjs198686 * appropriate message.
12883370Sjs198686 */
12893370Sjs198686 (void) close(rem);
12903370Sjs198686 rem = -1;
12910Sstevel@tonic-gate } else {
12920Sstevel@tonic-gate (void) write(rem, "", 1);
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate (void) close(f);
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate (void) response();
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate static void
rsource(char * name,struct stat * statp)13020Sstevel@tonic-gate rsource(char *name, struct stat *statp)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate DIR *d;
13050Sstevel@tonic-gate struct dirent *dp;
13060Sstevel@tonic-gate char *last, *vect[1];
13070Sstevel@tonic-gate char path[MAXPATHLEN];
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate if (!(d = opendir(name))) {
13100Sstevel@tonic-gate error("rcp: %s: %s\n", name, strerror(errno));
13110Sstevel@tonic-gate return;
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate last = rindex(name, '/');
13140Sstevel@tonic-gate if (last == 0)
13150Sstevel@tonic-gate last = name;
13160Sstevel@tonic-gate else
13170Sstevel@tonic-gate last++;
13180Sstevel@tonic-gate if (pflag) {
13190Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "T%ld 0 %ld 0\n",
1320*11851SMilan.Jurik@Sun.COM statp->st_mtime, statp->st_atime);
13210Sstevel@tonic-gate (void) desrcpwrite(rem, path, strlen(path));
13220Sstevel@tonic-gate if (response() < 0) {
13230Sstevel@tonic-gate (void) closedir(d);
13240Sstevel@tonic-gate return;
13250Sstevel@tonic-gate }
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "D%04o %d %s\n",
13280Sstevel@tonic-gate (uint_t)(statp->st_mode & 07777), 0, last);
13290Sstevel@tonic-gate (void) desrcpwrite(rem, path, strlen(path));
13300Sstevel@tonic-gate
13310Sstevel@tonic-gate /* acl support for directory */
13320Sstevel@tonic-gate if (aclflag) {
13330Sstevel@tonic-gate /* get acl from f and send it over */
13340Sstevel@tonic-gate if (sendacl(d->dd_fd) == ACL_FAIL) {
13350Sstevel@tonic-gate (void) closedir(d);
13360Sstevel@tonic-gate return;
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate if (response() < 0) {
13410Sstevel@tonic-gate (void) closedir(d);
13420Sstevel@tonic-gate return;
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate while (dp = readdir(d)) {
13460Sstevel@tonic-gate if (dp->d_ino == 0)
13470Sstevel@tonic-gate continue;
13480Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) ||
13490Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0))
13500Sstevel@tonic-gate continue;
13510Sstevel@tonic-gate if ((uint_t)strlen(name) + 1 + strlen(dp->d_name) >=
1352*11851SMilan.Jurik@Sun.COM MAXPATHLEN - 1) {
13530Sstevel@tonic-gate error("%s/%s: name too long.\n", name, dp->d_name);
13540Sstevel@tonic-gate continue;
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s/%s",
1357*11851SMilan.Jurik@Sun.COM name, dp->d_name);
13580Sstevel@tonic-gate vect[0] = path;
13590Sstevel@tonic-gate source(1, vect);
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate (void) closedir(d);
13620Sstevel@tonic-gate (void) desrcpwrite(rem, "E\n", 2);
13630Sstevel@tonic-gate (void) response();
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate static int
response(void)13670Sstevel@tonic-gate response(void)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate register char *cp;
13700Sstevel@tonic-gate char ch, resp, rbuf[RCP_BUFSIZE];
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate if (desrcpread(rem, &resp, 1) != 1)
13730Sstevel@tonic-gate lostconn();
13740Sstevel@tonic-gate cp = rbuf;
13750Sstevel@tonic-gate switch (resp) {
13760Sstevel@tonic-gate case 0: /* ok */
13770Sstevel@tonic-gate return (0);
13780Sstevel@tonic-gate default:
13790Sstevel@tonic-gate *cp++ = resp;
13800Sstevel@tonic-gate /* FALLTHROUGH */
13810Sstevel@tonic-gate case 1: /* error, followed by err msg */
13820Sstevel@tonic-gate case 2: /* fatal error, "" */
13830Sstevel@tonic-gate do {
13840Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch))
13850Sstevel@tonic-gate lostconn();
13860Sstevel@tonic-gate *cp++ = ch;
13870Sstevel@tonic-gate } while (cp < &rbuf[RCP_BUFSIZE] && ch != '\n');
13880Sstevel@tonic-gate
13890Sstevel@tonic-gate if (!iamremote)
13900Sstevel@tonic-gate (void) write(STDERR_FILENO, rbuf, cp - rbuf);
13910Sstevel@tonic-gate ++errs;
13920Sstevel@tonic-gate if (resp == 1)
13930Sstevel@tonic-gate return (-1);
13940Sstevel@tonic-gate exit(1);
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate /*NOTREACHED*/
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate static void
lostconn(void)14000Sstevel@tonic-gate lostconn(void)
14010Sstevel@tonic-gate {
14020Sstevel@tonic-gate if (!iamremote)
14030Sstevel@tonic-gate (void) fprintf(stderr, "rcp: lost connection\n");
14040Sstevel@tonic-gate exit(1);
14050Sstevel@tonic-gate }
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate static void
sink(int argc,char * argv[])14090Sstevel@tonic-gate sink(int argc, char *argv[])
14100Sstevel@tonic-gate {
14110Sstevel@tonic-gate char *cp;
14120Sstevel@tonic-gate static BUF buffer;
14130Sstevel@tonic-gate struct stat stb;
14140Sstevel@tonic-gate struct timeval tv[2];
14150Sstevel@tonic-gate BUF *bp;
14160Sstevel@tonic-gate off_t i, j;
14170Sstevel@tonic-gate char ch, *targ, *why;
14180Sstevel@tonic-gate int amt, count, exists, first, mask, mode;
14190Sstevel@tonic-gate off_t size;
14200Sstevel@tonic-gate int ofd, setimes, targisdir, wrerr;
14210Sstevel@tonic-gate char *np, *vect[1], buf[RCP_BUFSIZE];
14220Sstevel@tonic-gate char *namebuf = NULL;
14230Sstevel@tonic-gate size_t namebuf_sz = 0;
14240Sstevel@tonic-gate size_t need;
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate #define atime tv[0]
14270Sstevel@tonic-gate #define mtime tv[1]
14280Sstevel@tonic-gate #define SCREWUP(str) { why = str; goto screwup; }
14290Sstevel@tonic-gate
14300Sstevel@tonic-gate setimes = targisdir = 0;
14310Sstevel@tonic-gate mask = umask(0);
14320Sstevel@tonic-gate if (!pflag)
14330Sstevel@tonic-gate (void) umask(mask);
14340Sstevel@tonic-gate if (argc != 1) {
14350Sstevel@tonic-gate error("rcp: ambiguous target\n");
14360Sstevel@tonic-gate exit(1);
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate targ = *argv;
14390Sstevel@tonic-gate if (targetshouldbedirectory)
14400Sstevel@tonic-gate verifydir(targ);
14410Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
14420Sstevel@tonic-gate
14430Sstevel@tonic-gate if (stat(targ, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR)
14440Sstevel@tonic-gate targisdir = 1;
14450Sstevel@tonic-gate for (first = 1; ; first = 0) {
14460Sstevel@tonic-gate cp = buf;
14470Sstevel@tonic-gate if (desrcpread(rem, cp, 1) <= 0) {
14480Sstevel@tonic-gate if (namebuf != NULL)
14490Sstevel@tonic-gate free(namebuf);
14500Sstevel@tonic-gate return;
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate
14530Sstevel@tonic-gate if (*cp++ == '\n')
14540Sstevel@tonic-gate SCREWUP("unexpected <newline>");
14550Sstevel@tonic-gate do {
14560Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch))
14570Sstevel@tonic-gate SCREWUP("lost connection");
14580Sstevel@tonic-gate *cp++ = ch;
14590Sstevel@tonic-gate } while (cp < &buf[RCP_BUFSIZE - 1] && ch != '\n');
14600Sstevel@tonic-gate *cp = 0;
14610Sstevel@tonic-gate
14620Sstevel@tonic-gate if (buf[0] == '\01' || buf[0] == '\02') {
14630Sstevel@tonic-gate if (iamremote == 0)
14640Sstevel@tonic-gate (void) write(STDERR_FILENO, buf + 1,
14650Sstevel@tonic-gate strlen(buf + 1));
14660Sstevel@tonic-gate if (buf[0] == '\02')
14670Sstevel@tonic-gate exit(1);
14680Sstevel@tonic-gate errs++;
14690Sstevel@tonic-gate continue;
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate if (buf[0] == 'E') {
14720Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
14730Sstevel@tonic-gate if (namebuf != NULL)
14740Sstevel@tonic-gate free(namebuf);
14750Sstevel@tonic-gate return;
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate
14780Sstevel@tonic-gate if (ch == '\n')
14790Sstevel@tonic-gate *--cp = 0;
14800Sstevel@tonic-gate cp = buf;
14810Sstevel@tonic-gate if (*cp == 'T') {
14820Sstevel@tonic-gate setimes++;
14830Sstevel@tonic-gate cp++;
14840Sstevel@tonic-gate mtime.tv_sec = strtol(cp, &cp, 0);
14850Sstevel@tonic-gate if (*cp++ != ' ')
14860Sstevel@tonic-gate SCREWUP("mtime.sec not delimited");
14870Sstevel@tonic-gate mtime.tv_usec = strtol(cp, &cp, 0);
14880Sstevel@tonic-gate if (*cp++ != ' ')
14890Sstevel@tonic-gate SCREWUP("mtime.usec not delimited");
14900Sstevel@tonic-gate atime.tv_sec = strtol(cp, &cp, 0);
14910Sstevel@tonic-gate if (*cp++ != ' ')
14920Sstevel@tonic-gate SCREWUP("atime.sec not delimited");
14930Sstevel@tonic-gate atime.tv_usec = strtol(cp, &cp, 0);
14940Sstevel@tonic-gate if (*cp++ != '\0')
14950Sstevel@tonic-gate SCREWUP("atime.usec not delimited");
14960Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
14970Sstevel@tonic-gate continue;
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate if (*cp != 'C' && *cp != 'D') {
15000Sstevel@tonic-gate /*
15010Sstevel@tonic-gate * Check for the case "rcp remote:foo\* local:bar".
15020Sstevel@tonic-gate * In this case, the line "No match." can be returned
15030Sstevel@tonic-gate * by the shell before the rcp command on the remote is
15040Sstevel@tonic-gate * executed so the ^Aerror_message convention isn't
15050Sstevel@tonic-gate * followed.
15060Sstevel@tonic-gate */
15070Sstevel@tonic-gate if (first) {
15080Sstevel@tonic-gate error("%s\n", cp);
15090Sstevel@tonic-gate exit(1);
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate SCREWUP("expected control record");
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate mode = 0;
15140Sstevel@tonic-gate for (++cp; cp < buf + 5; cp++) {
15150Sstevel@tonic-gate if (*cp < '0' || *cp > '7')
15160Sstevel@tonic-gate SCREWUP("bad mode");
15170Sstevel@tonic-gate mode = (mode << 3) | (*cp - '0');
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate if (*cp++ != ' ')
15200Sstevel@tonic-gate SCREWUP("mode not delimited");
15210Sstevel@tonic-gate size = 0;
15220Sstevel@tonic-gate while (isdigit(*cp))
15230Sstevel@tonic-gate size = size * 10 + (*cp++ - '0');
15240Sstevel@tonic-gate if (*cp++ != ' ')
15250Sstevel@tonic-gate SCREWUP("size not delimited");
15260Sstevel@tonic-gate if (targisdir) {
15270Sstevel@tonic-gate need = strlen(targ) + sizeof ("/") + strlen(cp);
15280Sstevel@tonic-gate if (need > namebuf_sz) {
1529*11851SMilan.Jurik@Sun.COM if ((namebuf = realloc(namebuf, need)) ==
1530*11851SMilan.Jurik@Sun.COM NULL) {
15310Sstevel@tonic-gate error("rcp: out of memory\n");
15320Sstevel@tonic-gate exit(1);
1533*11851SMilan.Jurik@Sun.COM }
1534*11851SMilan.Jurik@Sun.COM namebuf_sz = need;
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate (void) snprintf(namebuf, need, "%s%s%s", targ,
15370Sstevel@tonic-gate *targ ? "/" : "", cp);
15380Sstevel@tonic-gate np = namebuf;
15390Sstevel@tonic-gate } else {
15400Sstevel@tonic-gate np = targ;
15410Sstevel@tonic-gate }
15420Sstevel@tonic-gate
15430Sstevel@tonic-gate exists = stat(np, &stb) == 0;
15440Sstevel@tonic-gate if (buf[0] == 'D') {
15450Sstevel@tonic-gate if (exists) {
15460Sstevel@tonic-gate if ((stb.st_mode&S_IFMT) != S_IFDIR) {
1547789Sahrens if (aclflag | acl_aclflag) {
15480Sstevel@tonic-gate /*
15490Sstevel@tonic-gate * consume acl in the pipe
15500Sstevel@tonic-gate * fd = -1 to indicate the
15510Sstevel@tonic-gate * special case
15520Sstevel@tonic-gate */
15530Sstevel@tonic-gate if (recvacl(-1, exists, pflag)
15540Sstevel@tonic-gate == ACL_FAIL) {
15550Sstevel@tonic-gate goto bad;
15560Sstevel@tonic-gate }
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate errno = ENOTDIR;
15590Sstevel@tonic-gate goto bad;
15600Sstevel@tonic-gate }
15610Sstevel@tonic-gate if (pflag)
15620Sstevel@tonic-gate (void) chmod(np, mode);
15630Sstevel@tonic-gate } else if (mkdir(np, mode) < 0) {
15640Sstevel@tonic-gate if (aclflag) {
15650Sstevel@tonic-gate /* consume acl in the pipe */
15660Sstevel@tonic-gate (void) recvacl(-1, exists, pflag);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate goto bad;
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate
15710Sstevel@tonic-gate /* acl support for directories */
1572789Sahrens if (aclflag | acl_aclflag) {
15730Sstevel@tonic-gate int dfd;
15740Sstevel@tonic-gate
15750Sstevel@tonic-gate if ((dfd = open(np, O_RDONLY)) == -1)
15760Sstevel@tonic-gate goto bad;
15770Sstevel@tonic-gate
15780Sstevel@tonic-gate /* get acl and set it to ofd */
15790Sstevel@tonic-gate if (recvacl(dfd, exists, pflag) == ACL_FAIL) {
15800Sstevel@tonic-gate (void) close(dfd);
15810Sstevel@tonic-gate if (!exists)
15820Sstevel@tonic-gate (void) rmdir(np);
15830Sstevel@tonic-gate goto bad;
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate (void) close(dfd);
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate
15880Sstevel@tonic-gate vect[0] = np;
15890Sstevel@tonic-gate sink(1, vect);
15900Sstevel@tonic-gate if (setimes) {
15910Sstevel@tonic-gate setimes = 0;
15920Sstevel@tonic-gate if (utimes(np, tv) < 0)
1593*11851SMilan.Jurik@Sun.COM error("rcp: can't set "
1594*11851SMilan.Jurik@Sun.COM "times on %s: %s\n",
1595*11851SMilan.Jurik@Sun.COM np, strerror(errno));
15960Sstevel@tonic-gate }
15970Sstevel@tonic-gate continue;
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
16010Sstevel@tonic-gate bad:
16020Sstevel@tonic-gate error("rcp: %s: %s\n", np, strerror(errno));
16030Sstevel@tonic-gate continue;
16040Sstevel@tonic-gate }
16050Sstevel@tonic-gate
16060Sstevel@tonic-gate /*
16070Sstevel@tonic-gate * If the output file exists we have to force zflag off
16080Sstevel@tonic-gate * to avoid erroneously seeking past old data.
16090Sstevel@tonic-gate */
16100Sstevel@tonic-gate zopen(ofd, zflag && !exists);
16110Sstevel@tonic-gate
16120Sstevel@tonic-gate if (exists && pflag)
16130Sstevel@tonic-gate (void) fchmod(ofd, mode);
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
16160Sstevel@tonic-gate
16170Sstevel@tonic-gate /*
16180Sstevel@tonic-gate * ACL support: receiving
16190Sstevel@tonic-gate */
1620789Sahrens if (aclflag | acl_aclflag) {
16210Sstevel@tonic-gate /* get acl and set it to ofd */
16220Sstevel@tonic-gate if (recvacl(ofd, exists, pflag) == ACL_FAIL) {
16230Sstevel@tonic-gate (void) close(ofd);
16240Sstevel@tonic-gate if (!exists)
16250Sstevel@tonic-gate (void) unlink(np);
16260Sstevel@tonic-gate continue;
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate }
16290Sstevel@tonic-gate
16300Sstevel@tonic-gate if ((bp = allocbuf(&buffer, ofd, RCP_BUFSIZE)) == 0) {
16310Sstevel@tonic-gate (void) close(ofd);
16320Sstevel@tonic-gate continue;
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate cp = bp->buf;
16350Sstevel@tonic-gate count = 0;
16360Sstevel@tonic-gate wrerr = 0;
16370Sstevel@tonic-gate for (i = 0; i < size; i += RCP_BUFSIZE) {
16380Sstevel@tonic-gate amt = RCP_BUFSIZE;
16390Sstevel@tonic-gate if (i + amt > size)
16400Sstevel@tonic-gate amt = size - i;
16410Sstevel@tonic-gate count += amt;
16420Sstevel@tonic-gate do {
16430Sstevel@tonic-gate j = desrcpread(rem, cp, amt);
16440Sstevel@tonic-gate if (j <= 0) {
16450Sstevel@tonic-gate int sverrno = errno;
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate /*
16480Sstevel@tonic-gate * Connection to supplier lost.
16490Sstevel@tonic-gate * Truncate file to correspond
16500Sstevel@tonic-gate * to amount already transferred.
16510Sstevel@tonic-gate *
16520Sstevel@tonic-gate * Note that we must call ftruncate()
16530Sstevel@tonic-gate * before any call to error() (which
16540Sstevel@tonic-gate * might result in a SIGPIPE and
16550Sstevel@tonic-gate * sudden death before we have a chance
16560Sstevel@tonic-gate * to correct the file's size).
16570Sstevel@tonic-gate */
16580Sstevel@tonic-gate size = lseek(ofd, 0, SEEK_CUR);
16590Sstevel@tonic-gate if ((ftruncate(ofd, size) == -1) &&
16600Sstevel@tonic-gate (errno != EINVAL) &&
16610Sstevel@tonic-gate (errno != EACCES))
16620Sstevel@tonic-gate #define TRUNCERR "rcp: can't truncate %s: %s\n"
16630Sstevel@tonic-gate error(TRUNCERR, np,
16640Sstevel@tonic-gate strerror(errno));
16650Sstevel@tonic-gate error("rcp: %s\n",
16660Sstevel@tonic-gate j ? strerror(sverrno) :
16670Sstevel@tonic-gate "dropped connection");
16680Sstevel@tonic-gate (void) close(ofd);
16690Sstevel@tonic-gate exit(1);
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate amt -= j;
16720Sstevel@tonic-gate cp += j;
16730Sstevel@tonic-gate } while (amt > 0);
16740Sstevel@tonic-gate if (count == bp->cnt) {
16750Sstevel@tonic-gate cp = bp->buf;
16760Sstevel@tonic-gate if (wrerr == 0 &&
16770Sstevel@tonic-gate zwrite(ofd, cp, count) < 0)
16780Sstevel@tonic-gate wrerr++;
16790Sstevel@tonic-gate count = 0;
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate if (count != 0 && wrerr == 0 &&
16830Sstevel@tonic-gate zwrite(ofd, bp->buf, count) < 0)
16840Sstevel@tonic-gate wrerr++;
16850Sstevel@tonic-gate if (zclose(ofd) < 0)
16860Sstevel@tonic-gate wrerr++;
16870Sstevel@tonic-gate
16880Sstevel@tonic-gate if ((ftruncate(ofd, size) == -1) && (errno != EINVAL) &&
16890Sstevel@tonic-gate (errno != EACCES)) {
16900Sstevel@tonic-gate error(TRUNCERR, np, strerror(errno));
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate (void) close(ofd);
16930Sstevel@tonic-gate (void) response();
16940Sstevel@tonic-gate if (setimes) {
16950Sstevel@tonic-gate setimes = 0;
16960Sstevel@tonic-gate if (utimes(np, tv) < 0)
16970Sstevel@tonic-gate error("rcp: can't set times on %s: %s\n",
16980Sstevel@tonic-gate np, strerror(errno));
16990Sstevel@tonic-gate }
17000Sstevel@tonic-gate if (wrerr)
17010Sstevel@tonic-gate error("rcp: %s: %s\n", np, strerror(errno));
17020Sstevel@tonic-gate else
17030Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate screwup:
17060Sstevel@tonic-gate error("rcp: protocol screwup: %s\n", why);
17070Sstevel@tonic-gate exit(1);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate
17100Sstevel@tonic-gate #ifndef roundup
17110Sstevel@tonic-gate #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
17120Sstevel@tonic-gate #endif /* !roundup */
17130Sstevel@tonic-gate
17140Sstevel@tonic-gate static BUF *
allocbuf(BUF * bp,int fd,int blksize)17150Sstevel@tonic-gate allocbuf(BUF *bp, int fd, int blksize)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate struct stat stb;
17180Sstevel@tonic-gate int size;
17190Sstevel@tonic-gate
17200Sstevel@tonic-gate if (fstat(fd, &stb) < 0) {
17210Sstevel@tonic-gate error("rcp: fstat: %s\n", strerror(errno));
17220Sstevel@tonic-gate return (0);
17230Sstevel@tonic-gate }
17240Sstevel@tonic-gate size = roundup(stb.st_blksize, blksize);
17250Sstevel@tonic-gate if (size == 0)
17260Sstevel@tonic-gate size = blksize;
17270Sstevel@tonic-gate if (bp->cnt < size) {
17280Sstevel@tonic-gate if (bp->buf != 0)
17290Sstevel@tonic-gate free(bp->buf);
17300Sstevel@tonic-gate bp->buf = (char *)malloc((uint_t)size);
17310Sstevel@tonic-gate if (!bp->buf) {
17320Sstevel@tonic-gate error("rcp: malloc: out of memory\n");
17330Sstevel@tonic-gate return (0);
17340Sstevel@tonic-gate }
17350Sstevel@tonic-gate }
17360Sstevel@tonic-gate bp->cnt = size;
17370Sstevel@tonic-gate return (bp);
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate static void
usage(void)17410Sstevel@tonic-gate usage(void)
17420Sstevel@tonic-gate {
17430Sstevel@tonic-gate (void) fprintf(stderr, "%s: \t%s\t%s", gettext("Usage"),
1744*11851SMilan.Jurik@Sun.COM gettext("\trcp [-p] [-a] [-x] [-k realm] [-PN / -PO] "
17450Sstevel@tonic-gate #ifdef DEBUG
1746*11851SMilan.Jurik@Sun.COM "[-D port] "
17470Sstevel@tonic-gate #endif /* DEBUG */
1748*11851SMilan.Jurik@Sun.COM "f1 f2; or:\n"),
1749*11851SMilan.Jurik@Sun.COM gettext("\trcp [-r] [-p] [-a] [-x] "
17500Sstevel@tonic-gate #ifdef DEBUG
1751*11851SMilan.Jurik@Sun.COM "[-D port] "
17520Sstevel@tonic-gate #endif /* DEBUG */
1753*11851SMilan.Jurik@Sun.COM "[-k realm] [-PN / -PO] f1...fn d2\n"));
17540Sstevel@tonic-gate exit(1);
17550Sstevel@tonic-gate }
17560Sstevel@tonic-gate
17570Sstevel@tonic-gate
17580Sstevel@tonic-gate /*
17590Sstevel@tonic-gate * sparse file support
17600Sstevel@tonic-gate */
17610Sstevel@tonic-gate
17620Sstevel@tonic-gate static off_t zbsize;
17630Sstevel@tonic-gate static off_t zlastseek;
17640Sstevel@tonic-gate
17650Sstevel@tonic-gate /* is it ok to try to create holes? */
17660Sstevel@tonic-gate static void
zopen(int fd,int flag)17670Sstevel@tonic-gate zopen(int fd, int flag)
17680Sstevel@tonic-gate {
17690Sstevel@tonic-gate struct stat st;
17700Sstevel@tonic-gate
17710Sstevel@tonic-gate zbsize = 0;
17720Sstevel@tonic-gate zlastseek = 0;
17730Sstevel@tonic-gate
17740Sstevel@tonic-gate if (flag &&
1775*11851SMilan.Jurik@Sun.COM fstat(fd, &st) == 0 &&
1776*11851SMilan.Jurik@Sun.COM (st.st_mode & S_IFMT) == S_IFREG)
17770Sstevel@tonic-gate zbsize = st.st_blksize;
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate
17800Sstevel@tonic-gate /* write and/or seek */
17810Sstevel@tonic-gate static int
zwrite(int fd,char * buf,int nbytes)17820Sstevel@tonic-gate zwrite(int fd, char *buf, int nbytes)
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate off_t block = zbsize ? zbsize : nbytes;
17850Sstevel@tonic-gate
17860Sstevel@tonic-gate do {
17870Sstevel@tonic-gate if (block > nbytes)
17880Sstevel@tonic-gate block = nbytes;
17890Sstevel@tonic-gate nbytes -= block;
17900Sstevel@tonic-gate
17910Sstevel@tonic-gate if (!zbsize || notzero(buf, block)) {
17920Sstevel@tonic-gate register int n, count = block;
17930Sstevel@tonic-gate
17940Sstevel@tonic-gate do {
17950Sstevel@tonic-gate if ((n = write(fd, buf, count)) < 0)
17960Sstevel@tonic-gate return (-1);
17970Sstevel@tonic-gate buf += n;
17980Sstevel@tonic-gate } while ((count -= n) > 0);
17990Sstevel@tonic-gate zlastseek = 0;
18000Sstevel@tonic-gate } else {
18010Sstevel@tonic-gate if (lseek(fd, (off_t)block, SEEK_CUR) < 0)
18020Sstevel@tonic-gate return (-1);
18030Sstevel@tonic-gate buf += block;
18040Sstevel@tonic-gate zlastseek = 1;
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate } while (nbytes > 0);
18070Sstevel@tonic-gate
18080Sstevel@tonic-gate return (0);
18090Sstevel@tonic-gate }
18100Sstevel@tonic-gate
18110Sstevel@tonic-gate /* write last byte of file if necessary */
18120Sstevel@tonic-gate static int
zclose(int fd)18130Sstevel@tonic-gate zclose(int fd)
18140Sstevel@tonic-gate {
18150Sstevel@tonic-gate zbsize = 0;
18160Sstevel@tonic-gate
18170Sstevel@tonic-gate if (zlastseek && (lseek(fd, (off_t)-1, SEEK_CUR) < 0 ||
1818*11851SMilan.Jurik@Sun.COM zwrite(fd, "", 1) < 0))
18190Sstevel@tonic-gate return (-1);
18200Sstevel@tonic-gate else
18210Sstevel@tonic-gate return (0);
18220Sstevel@tonic-gate }
18230Sstevel@tonic-gate
18240Sstevel@tonic-gate /* return true if buffer is not all zeros */
18250Sstevel@tonic-gate static int
notzero(char * p,int n)18260Sstevel@tonic-gate notzero(char *p, int n)
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate register int result = 0;
18290Sstevel@tonic-gate
18300Sstevel@tonic-gate while ((int)p & 3 && --n >= 0)
18310Sstevel@tonic-gate result |= *p++;
18320Sstevel@tonic-gate
18330Sstevel@tonic-gate while ((n -= 4 * sizeof (int)) >= 0) {
18340Sstevel@tonic-gate /* LINTED */
18350Sstevel@tonic-gate result |= ((int *)p)[0];
18360Sstevel@tonic-gate /* LINTED */
18370Sstevel@tonic-gate result |= ((int *)p)[1];
18380Sstevel@tonic-gate /* LINTED */
18390Sstevel@tonic-gate result |= ((int *)p)[2];
18400Sstevel@tonic-gate /* LINTED */
18410Sstevel@tonic-gate result |= ((int *)p)[3];
18420Sstevel@tonic-gate if (result)
18430Sstevel@tonic-gate return (result);
18440Sstevel@tonic-gate p += 4 * sizeof (int);
18450Sstevel@tonic-gate }
18460Sstevel@tonic-gate n += 4 * sizeof (int);
18470Sstevel@tonic-gate
18480Sstevel@tonic-gate while (--n >= 0)
18490Sstevel@tonic-gate result |= *p++;
18500Sstevel@tonic-gate
18510Sstevel@tonic-gate return (result);
18520Sstevel@tonic-gate }
18530Sstevel@tonic-gate
18540Sstevel@tonic-gate /*
18550Sstevel@tonic-gate * New functions to support ACLs
18560Sstevel@tonic-gate */
18570Sstevel@tonic-gate
18580Sstevel@tonic-gate /*
18590Sstevel@tonic-gate * Get acl from f and send it over.
18600Sstevel@tonic-gate * ACL record includes acl entry count, acl text length, and acl text.
18610Sstevel@tonic-gate */
18620Sstevel@tonic-gate static int
sendacl(int f)18630Sstevel@tonic-gate sendacl(int f)
18640Sstevel@tonic-gate {
18650Sstevel@tonic-gate int aclcnt;
18660Sstevel@tonic-gate char *acltext;
18670Sstevel@tonic-gate char buf[BUFSIZ];
1868789Sahrens acl_t *aclp;
1869789Sahrens char acltype;
1870789Sahrens int aclerror;
1871789Sahrens int trivial;
18720Sstevel@tonic-gate
1873789Sahrens
1874789Sahrens aclerror = facl_get(f, ACL_NO_TRIVIAL, &aclp);
1875789Sahrens if (aclerror != 0) {
1876789Sahrens error("can't retrieve ACL: %s \n", acl_strerror(aclerror));
18770Sstevel@tonic-gate return (ACL_FAIL);
18780Sstevel@tonic-gate }
18790Sstevel@tonic-gate
1880789Sahrens /*
1881789Sahrens * if acl type is not ACLENT_T and were operating in acl_aclflag == 0
1882789Sahrens * then don't do the malloc and facl(fd, getcntcmd,...);
1883789Sahrens * since the remote side doesn't support alternate style ACL's.
1884789Sahrens */
1885789Sahrens
1886789Sahrens if (aclp && (acl_type(aclp) != ACLENT_T) && (acl_aclflag == 0)) {
1887789Sahrens aclcnt = MIN_ACL_ENTRIES;
1888789Sahrens acltype = 'A';
1889789Sahrens trivial = ACL_IS_TRIVIAL;
1890789Sahrens } else {
1891789Sahrens
1892789Sahrens aclcnt = (aclp != NULL) ? acl_cnt(aclp) : 0;
1893789Sahrens
1894789Sahrens if (aclp) {
1895789Sahrens acltype = (acl_type(aclp) != ACLENT_T) ? 'Z' : 'A';
1896789Sahrens aclcnt = acl_cnt(aclp);
1897789Sahrens trivial = (acl_flags(aclp) & ACL_IS_TRIVIAL);
1898789Sahrens } else {
1899789Sahrens acltype = 'A';
1900789Sahrens aclcnt = MIN_ACL_ENTRIES;
1901789Sahrens trivial = ACL_IS_TRIVIAL;
1902789Sahrens }
1903789Sahrens
1904789Sahrens }
1905789Sahrens
19060Sstevel@tonic-gate /* send the acl count over */
1907789Sahrens (void) snprintf(buf, sizeof (buf), "%c%d\n", acltype, aclcnt);
19080Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf));
19090Sstevel@tonic-gate
1910789Sahrens /*
1911789Sahrens * only send acl when we have an aclp, which would
1912789Sahrens * imply its not trivial.
1913789Sahrens */
1914789Sahrens if (aclp && (trivial != ACL_IS_TRIVIAL)) {
19151420Smarks acltext = acl_totext(aclp, 0);
19160Sstevel@tonic-gate if (acltext == NULL) {
19170Sstevel@tonic-gate error("rcp: failed to convert to text\n");
1918789Sahrens acl_free(aclp);
19190Sstevel@tonic-gate return (ACL_FAIL);
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate
19220Sstevel@tonic-gate /* send ACLs over: send the length first */
1923789Sahrens (void) snprintf(buf, sizeof (buf), "%c%d\n",
1924789Sahrens acltype, strlen(acltext));
19250Sstevel@tonic-gate
19260Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf));
19270Sstevel@tonic-gate (void) desrcpwrite(rem, acltext, strlen(acltext));
19280Sstevel@tonic-gate free(acltext);
1929789Sahrens if (response() < 0) {
1930789Sahrens acl_free(aclp);
19310Sstevel@tonic-gate return (ACL_FAIL);
1932789Sahrens }
19330Sstevel@tonic-gate
19340Sstevel@tonic-gate }
1935789Sahrens
1936789Sahrens if (aclp)
1937789Sahrens acl_free(aclp);
19380Sstevel@tonic-gate return (ACL_OK);
19390Sstevel@tonic-gate }
19400Sstevel@tonic-gate
19410Sstevel@tonic-gate /*
19420Sstevel@tonic-gate * Use this routine to get acl entry count and acl text size (in bytes)
19430Sstevel@tonic-gate */
19440Sstevel@tonic-gate static int
getaclinfo(int * cnt,int * acltype)1945789Sahrens getaclinfo(int *cnt, int *acltype)
19460Sstevel@tonic-gate {
19470Sstevel@tonic-gate char buf[BUFSIZ];
19480Sstevel@tonic-gate char *cp;
19490Sstevel@tonic-gate char ch;
19500Sstevel@tonic-gate
19510Sstevel@tonic-gate /* get acl count */
19520Sstevel@tonic-gate cp = buf;
19530Sstevel@tonic-gate if (desrcpread(rem, cp, 1) <= 0)
19540Sstevel@tonic-gate return (ACL_FAIL);
1955789Sahrens
1956789Sahrens switch (*cp++) {
1957789Sahrens case 'A':
1958789Sahrens *acltype = 0;
1959789Sahrens break;
1960789Sahrens case 'Z':
1961789Sahrens *acltype = 1;
1962789Sahrens break;
1963789Sahrens default:
19640Sstevel@tonic-gate error("rcp: expect an ACL record, but got %c\n", *cp);
19650Sstevel@tonic-gate return (ACL_FAIL);
19660Sstevel@tonic-gate }
19670Sstevel@tonic-gate do {
19680Sstevel@tonic-gate if (desrcpread(rem, &ch, sizeof (ch)) != sizeof (ch)) {
19690Sstevel@tonic-gate error("rcp: lost connection ..\n");
19700Sstevel@tonic-gate return (ACL_FAIL);
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate *cp++ = ch;
19730Sstevel@tonic-gate } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
19740Sstevel@tonic-gate if (ch != '\n') {
19750Sstevel@tonic-gate error("rcp: ACL record corrupted \n");
19760Sstevel@tonic-gate return (ACL_FAIL);
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate cp = &buf[1];
19790Sstevel@tonic-gate *cnt = strtol(cp, &cp, 0);
19800Sstevel@tonic-gate if (*cp != '\n') {
19810Sstevel@tonic-gate error("rcp: ACL record corrupted \n");
19820Sstevel@tonic-gate return (ACL_FAIL);
19830Sstevel@tonic-gate }
19840Sstevel@tonic-gate return (ACL_OK);
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate
19870Sstevel@tonic-gate
19880Sstevel@tonic-gate /*
19890Sstevel@tonic-gate * Receive acl from the pipe and set it to f
19900Sstevel@tonic-gate */
19910Sstevel@tonic-gate static int
recvacl(int f,int exists,int preserve)19920Sstevel@tonic-gate recvacl(int f, int exists, int preserve)
19930Sstevel@tonic-gate {
19940Sstevel@tonic-gate int aclcnt; /* acl entry count */
19950Sstevel@tonic-gate int aclsize; /* acl text length */
19960Sstevel@tonic-gate int j;
19970Sstevel@tonic-gate char *tp;
19980Sstevel@tonic-gate char *acltext; /* external format */
1999789Sahrens acl_t *aclp;
2000789Sahrens int acltype;
2001789Sahrens int min_entries;
2002789Sahrens int aclerror;
20030Sstevel@tonic-gate
20040Sstevel@tonic-gate /* get acl count */
2005789Sahrens if (getaclinfo(&aclcnt, &acltype) != ACL_OK)
20060Sstevel@tonic-gate return (ACL_FAIL);
20070Sstevel@tonic-gate
2008789Sahrens if (acltype == 0) {
2009789Sahrens min_entries = MIN_ACL_ENTRIES;
2010789Sahrens } else {
2011789Sahrens min_entries = 1;
2012789Sahrens }
2013789Sahrens
2014789Sahrens if (aclcnt > min_entries) {
20150Sstevel@tonic-gate /* get acl text size */
2016789Sahrens if (getaclinfo(&aclsize, &acltype) != ACL_OK)
20170Sstevel@tonic-gate return (ACL_FAIL);
20180Sstevel@tonic-gate if ((acltext = malloc(aclsize + 1)) == NULL) {
20190Sstevel@tonic-gate error("rcp: cant allocate memory: %d\n", aclsize);
20200Sstevel@tonic-gate return (ACL_FAIL);
20210Sstevel@tonic-gate }
20220Sstevel@tonic-gate
20230Sstevel@tonic-gate tp = acltext;
20240Sstevel@tonic-gate do {
20250Sstevel@tonic-gate j = desrcpread(rem, tp, aclsize);
20260Sstevel@tonic-gate if (j <= 0) {
20270Sstevel@tonic-gate error("rcp: %s\n", j ? strerror(errno) :
20280Sstevel@tonic-gate "dropped connection");
20290Sstevel@tonic-gate exit(1);
20300Sstevel@tonic-gate }
20310Sstevel@tonic-gate aclsize -= j;
20320Sstevel@tonic-gate tp += j;
20330Sstevel@tonic-gate } while (aclsize > 0);
20340Sstevel@tonic-gate *tp = '\0';
20350Sstevel@tonic-gate
20360Sstevel@tonic-gate if (preserve || !exists) {
2037789Sahrens aclerror = acl_fromtext(acltext, &aclp);
2038789Sahrens if (aclerror != 0) {
2039789Sahrens error("rcp: failed to parse acl : %s\n",
2040789Sahrens acl_strerror(aclerror));
20413226Smp204432 free(acltext);
20420Sstevel@tonic-gate return (ACL_FAIL);
20430Sstevel@tonic-gate }
2044789Sahrens
20450Sstevel@tonic-gate if (f != -1) {
2046789Sahrens if (facl_set(f, aclp) < 0) {
20470Sstevel@tonic-gate error("rcp: failed to set acl\n");
20483226Smp204432 acl_free(aclp);
20493226Smp204432 free(acltext);
20500Sstevel@tonic-gate return (ACL_FAIL);
20510Sstevel@tonic-gate }
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate /* -1 means that just consume the data in the pipe */
2054789Sahrens acl_free(aclp);
20550Sstevel@tonic-gate }
20560Sstevel@tonic-gate free(acltext);
20570Sstevel@tonic-gate (void) desrcpwrite(rem, "", 1);
20580Sstevel@tonic-gate }
20590Sstevel@tonic-gate return (ACL_OK);
20600Sstevel@tonic-gate }
20610Sstevel@tonic-gate
20620Sstevel@tonic-gate
20630Sstevel@tonic-gate static char *
search_char(unsigned char * cp,unsigned char chr)20640Sstevel@tonic-gate search_char(unsigned char *cp, unsigned char chr)
20650Sstevel@tonic-gate {
20660Sstevel@tonic-gate int len;
20670Sstevel@tonic-gate
20680Sstevel@tonic-gate while (*cp) {
20690Sstevel@tonic-gate if (*cp == chr)
20700Sstevel@tonic-gate return ((char *)cp);
20710Sstevel@tonic-gate if ((len = mblen((char *)cp, MB_CUR_MAX)) <= 0)
20720Sstevel@tonic-gate len = 1;
20730Sstevel@tonic-gate cp += len;
20740Sstevel@tonic-gate }
20750Sstevel@tonic-gate return (0);
20760Sstevel@tonic-gate }
20770Sstevel@tonic-gate
20780Sstevel@tonic-gate
20790Sstevel@tonic-gate static int
desrcpread(int fd,char * buf,int len)20800Sstevel@tonic-gate desrcpread(int fd, char *buf, int len)
20810Sstevel@tonic-gate {
20820Sstevel@tonic-gate return ((int)desread(fd, buf, len, 0));
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate
20850Sstevel@tonic-gate static int
desrcpwrite(int fd,char * buf,int len)20860Sstevel@tonic-gate desrcpwrite(int fd, char *buf, int len)
20870Sstevel@tonic-gate {
20880Sstevel@tonic-gate /*
20890Sstevel@tonic-gate * Note that rcp depends on the same file descriptor being both
20900Sstevel@tonic-gate * input and output to the remote side. This is bogus, especially
20910Sstevel@tonic-gate * when rcp is being run by a rsh that pipes. Fix it here because
20920Sstevel@tonic-gate * it would require significantly more work in other places.
20930Sstevel@tonic-gate * --hartmans 1/96
20940Sstevel@tonic-gate */
20950Sstevel@tonic-gate
20960Sstevel@tonic-gate if (fd == 0)
20970Sstevel@tonic-gate fd = 1;
20980Sstevel@tonic-gate return ((int)deswrite(fd, buf, len, 0));
20990Sstevel@tonic-gate }
21000Sstevel@tonic-gate
21010Sstevel@tonic-gate static char **
save_argv(int argc,char ** argv)21020Sstevel@tonic-gate save_argv(int argc, char **argv)
21030Sstevel@tonic-gate {
21040Sstevel@tonic-gate int i;
21050Sstevel@tonic-gate
21060Sstevel@tonic-gate char **local_argv = (char **)calloc((unsigned)argc + 1,
21070Sstevel@tonic-gate (unsigned)sizeof (char *));
21080Sstevel@tonic-gate
21090Sstevel@tonic-gate /*
21100Sstevel@tonic-gate * allocate an extra pointer, so that it is initialized to NULL and
21110Sstevel@tonic-gate * execv() will work
21120Sstevel@tonic-gate */
21130Sstevel@tonic-gate for (i = 0; i < argc; i++) {
21140Sstevel@tonic-gate local_argv[i] = strsave(argv[i]);
21150Sstevel@tonic-gate }
21160Sstevel@tonic-gate
21170Sstevel@tonic-gate return (local_argv);
21180Sstevel@tonic-gate }
21190Sstevel@tonic-gate
21200Sstevel@tonic-gate #define SIZEOF_INADDR sizeof (struct in_addr)
21210Sstevel@tonic-gate
21220Sstevel@tonic-gate static void
answer_auth(char * config_file,char * ccache_file)21230Sstevel@tonic-gate answer_auth(char *config_file, char *ccache_file)
21240Sstevel@tonic-gate {
21250Sstevel@tonic-gate krb5_data pname_data, msg;
21260Sstevel@tonic-gate krb5_creds creds, *new_creds;
21270Sstevel@tonic-gate krb5_ccache cc;
21280Sstevel@tonic-gate krb5_auth_context auth_context = NULL;
21290Sstevel@tonic-gate
21300Sstevel@tonic-gate if (config_file) {
21310Sstevel@tonic-gate const char *filenames[2];
21320Sstevel@tonic-gate
21330Sstevel@tonic-gate filenames[1] = NULL;
21340Sstevel@tonic-gate filenames[0] = config_file;
21350Sstevel@tonic-gate if (krb5_set_config_files(bsd_context, filenames))
21360Sstevel@tonic-gate exit(1);
21370Sstevel@tonic-gate }
21380Sstevel@tonic-gate (void) memset((char *)&creds, 0, sizeof (creds));
21390Sstevel@tonic-gate
21400Sstevel@tonic-gate if (krb5_read_message(bsd_context, (krb5_pointer) &rem, &pname_data))
21410Sstevel@tonic-gate exit(1);
21420Sstevel@tonic-gate
21430Sstevel@tonic-gate if (krb5_read_message(bsd_context, (krb5_pointer) &rem,
21440Sstevel@tonic-gate &creds.second_ticket))
21450Sstevel@tonic-gate exit(1);
21460Sstevel@tonic-gate
21470Sstevel@tonic-gate if (ccache_file == NULL) {
21480Sstevel@tonic-gate if (krb5_cc_default(bsd_context, &cc))
21490Sstevel@tonic-gate exit(1);
21500Sstevel@tonic-gate } else {
21510Sstevel@tonic-gate if (krb5_cc_resolve(bsd_context, ccache_file, &cc))
21520Sstevel@tonic-gate exit(1);
21530Sstevel@tonic-gate }
21540Sstevel@tonic-gate
21550Sstevel@tonic-gate if (krb5_cc_get_principal(bsd_context, cc, &creds.client))
21560Sstevel@tonic-gate exit(1);
21570Sstevel@tonic-gate
21580Sstevel@tonic-gate if (krb5_parse_name(bsd_context, pname_data.data, &creds.server))
21590Sstevel@tonic-gate exit(1);
21600Sstevel@tonic-gate
21610Sstevel@tonic-gate krb5_xfree(pname_data.data);
21620Sstevel@tonic-gate if (krb5_get_credentials(bsd_context, KRB5_GC_USER_USER, cc, &creds,
21630Sstevel@tonic-gate &new_creds))
21640Sstevel@tonic-gate exit(1);
21650Sstevel@tonic-gate
21660Sstevel@tonic-gate if (krb5_mk_req_extended(bsd_context, &auth_context,
21670Sstevel@tonic-gate AP_OPTS_USE_SESSION_KEY, NULL, new_creds, &msg))
21680Sstevel@tonic-gate exit(1);
21690Sstevel@tonic-gate
21700Sstevel@tonic-gate if (krb5_write_message(bsd_context, (krb5_pointer) & rem, &msg)) {
21710Sstevel@tonic-gate krb5_xfree(msg.data);
21720Sstevel@tonic-gate exit(1);
21730Sstevel@tonic-gate }
21740Sstevel@tonic-gate /* setup eblock for des_read and write */
217511415SSurya.Prakki@Sun.COM (void) krb5_copy_keyblock(bsd_context,
217611415SSurya.Prakki@Sun.COM &new_creds->keyblock, &session_key);
21770Sstevel@tonic-gate
21780Sstevel@tonic-gate /* OK process key */
21790Sstevel@tonic-gate eblock.crypto_entry = session_key->enctype;
21800Sstevel@tonic-gate eblock.key = (krb5_keyblock *)session_key;
21810Sstevel@tonic-gate
21820Sstevel@tonic-gate init_encrypt(encrypt_flag, bsd_context, KCMD_OLD_PROTOCOL,
21830Sstevel@tonic-gate &desinbuf, &desoutbuf, CLIENT, &eblock);
21840Sstevel@tonic-gate /* cleanup */
21850Sstevel@tonic-gate krb5_free_cred_contents(bsd_context, &creds);
21860Sstevel@tonic-gate krb5_free_creds(bsd_context, new_creds);
21870Sstevel@tonic-gate krb5_xfree(msg.data);
21880Sstevel@tonic-gate }
21890Sstevel@tonic-gate
21900Sstevel@tonic-gate
21910Sstevel@tonic-gate static void
try_normal_rcp(int cur_argc,char ** cur_argv)21920Sstevel@tonic-gate try_normal_rcp(int cur_argc, char **cur_argv)
21930Sstevel@tonic-gate {
21940Sstevel@tonic-gate char *target;
21950Sstevel@tonic-gate
21960Sstevel@tonic-gate /*
21970Sstevel@tonic-gate * Reset all KRB5 relevant flags and set the
21980Sstevel@tonic-gate * cmd-buffer so that normal rcp works
21990Sstevel@tonic-gate */
22000Sstevel@tonic-gate krb5auth_flag = encrypt_flag = encrypt_done = 0;
22010Sstevel@tonic-gate cmd = cmd_orig;
22020Sstevel@tonic-gate cmd_sunw = cmd_sunw_orig;
22030Sstevel@tonic-gate
22040Sstevel@tonic-gate if (cur_argc < 2)
22050Sstevel@tonic-gate usage();
22060Sstevel@tonic-gate
22070Sstevel@tonic-gate if (cur_argc > 2)
22080Sstevel@tonic-gate targetshouldbedirectory = 1;
22090Sstevel@tonic-gate
22100Sstevel@tonic-gate rem = -1;
22110Sstevel@tonic-gate
22120Sstevel@tonic-gate prev_argc = cur_argc;
22130Sstevel@tonic-gate prev_argv = save_argv(cur_argc, cur_argv);
22140Sstevel@tonic-gate
22150Sstevel@tonic-gate (void) init_service(krb5auth_flag);
22160Sstevel@tonic-gate
22170Sstevel@tonic-gate if (target = colon(cur_argv[cur_argc - 1])) {
22180Sstevel@tonic-gate toremote(target, cur_argc, cur_argv);
22190Sstevel@tonic-gate } else {
22200Sstevel@tonic-gate tolocal(cur_argc, cur_argv);
22210Sstevel@tonic-gate if (targetshouldbedirectory)
22220Sstevel@tonic-gate verifydir(cur_argv[cur_argc - 1]);
22230Sstevel@tonic-gate }
22240Sstevel@tonic-gate exit(errs);
22250Sstevel@tonic-gate /* NOTREACHED */
22260Sstevel@tonic-gate }
22270Sstevel@tonic-gate
22280Sstevel@tonic-gate
22290Sstevel@tonic-gate static int
init_service(int krb5flag)22300Sstevel@tonic-gate init_service(int krb5flag)
22310Sstevel@tonic-gate {
22320Sstevel@tonic-gate struct servent *sp;
22330Sstevel@tonic-gate boolean_t success = B_FALSE;
22340Sstevel@tonic-gate
22350Sstevel@tonic-gate if (krb5flag > 0) {
22360Sstevel@tonic-gate sp = getservbyname("kshell", "tcp");
22370Sstevel@tonic-gate if (sp == NULL) {
22380Sstevel@tonic-gate (void) fprintf(stderr,
2239*11851SMilan.Jurik@Sun.COM gettext("rcp: kshell/tcp: unknown service.\n"
2240*11851SMilan.Jurik@Sun.COM "trying normal shell/tcp service\n"));
22410Sstevel@tonic-gate } else {
22420Sstevel@tonic-gate portnumber = sp->s_port;
22430Sstevel@tonic-gate success = B_TRUE;
22440Sstevel@tonic-gate }
22450Sstevel@tonic-gate } else {
22460Sstevel@tonic-gate portnumber = htons(IPPORT_CMDSERVER);
22470Sstevel@tonic-gate success = B_TRUE;
22480Sstevel@tonic-gate }
22490Sstevel@tonic-gate return (success);
22500Sstevel@tonic-gate }
22510Sstevel@tonic-gate
22520Sstevel@tonic-gate /*PRINTFLIKE1*/
22530Sstevel@tonic-gate static void
error(char * fmt,...)22540Sstevel@tonic-gate error(char *fmt, ...)
22550Sstevel@tonic-gate {
22560Sstevel@tonic-gate va_list ap;
22570Sstevel@tonic-gate char buf[RCP_BUFSIZE];
22580Sstevel@tonic-gate char *cp = buf;
22590Sstevel@tonic-gate
22600Sstevel@tonic-gate va_start(ap, fmt);
22610Sstevel@tonic-gate errs++;
22620Sstevel@tonic-gate *cp++ = 1;
22630Sstevel@tonic-gate (void) vsnprintf(cp, sizeof (buf) - 1, fmt, ap);
22640Sstevel@tonic-gate va_end(ap);
22650Sstevel@tonic-gate
22660Sstevel@tonic-gate (void) desrcpwrite(rem, buf, strlen(buf));
22670Sstevel@tonic-gate if (iamremote == 0)
22680Sstevel@tonic-gate (void) write(2, buf + 1, strlen(buf + 1));
22690Sstevel@tonic-gate }
22704619Ssn199410
22714619Ssn199410 static void
addargs(char ** arglist,...)22724619Ssn199410 addargs(char **arglist, ...)
22734619Ssn199410 {
22744619Ssn199410 va_list ap;
22754619Ssn199410 int i = 0;
22764619Ssn199410 char *pm;
22774619Ssn199410
22784619Ssn199410 va_start(ap, arglist);
22794619Ssn199410 while (i < MAXARGS && (pm = va_arg(ap, char *)) != NULL)
22804619Ssn199410 if (strcmp(pm, ""))
22814619Ssn199410 arglist[i++] = pm;
22824619Ssn199410 arglist[i] = NULL;
22834619Ssn199410 va_end(ap);
22844619Ssn199410 }
2285