10Sstevel@tonic-gate /*
2*11415SSurya.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 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 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
190Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
200Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
210Sstevel@tonic-gate */
220Sstevel@tonic-gate
230Sstevel@tonic-gate /* derived from @(#)rcmd.c 5.17 (Berkeley) 6/27/88 */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <unistd.h>
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <ctype.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <pwd.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <signal.h>
360Sstevel@tonic-gate #include <sys/file.h>
370Sstevel@tonic-gate #include <sys/socket.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <arpa/inet.h>
420Sstevel@tonic-gate #include <netdb.h>
430Sstevel@tonic-gate #include <locale.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <errno.h>
470Sstevel@tonic-gate #include <com_err.h>
480Sstevel@tonic-gate #include <k5-int.h>
490Sstevel@tonic-gate #include <kcmd.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate static char *default_service = "host";
520Sstevel@tonic-gate
530Sstevel@tonic-gate #define KCMD_BUFSIZ 102400
543518Spk193450 #define KCMD8_BUFSIZ (4096 - 256)
553518Spk193450 /*
563518Spk193450 * For compatibility with earlier versions of Solaris and other OS
573518Spk193450 * (kerborized rsh uses 4KB of RSH_BUFSIZE)- 256 to make sure
583518Spk193450 * there is room
593518Spk193450 */
603518Spk193450 static int deswrite_compat(int, char *, int, int);
613518Spk193450
620Sstevel@tonic-gate #define KCMD_KEYUSAGE 1026
630Sstevel@tonic-gate
640Sstevel@tonic-gate static char storage[KCMD_BUFSIZ];
650Sstevel@tonic-gate static int nstored = 0;
660Sstevel@tonic-gate static int MAXSIZE = (KCMD_BUFSIZ + 8);
670Sstevel@tonic-gate static char *store_ptr = storage;
680Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf;
690Sstevel@tonic-gate
700Sstevel@tonic-gate static boolean_t encrypt_flag = B_FALSE;
710Sstevel@tonic-gate static krb5_context kcmd_context;
720Sstevel@tonic-gate
730Sstevel@tonic-gate /* XXX Overloaded: use_ivecs!=0 -> new protocol, inband signalling, etc. */
740Sstevel@tonic-gate static boolean_t use_ivecs = B_FALSE;
750Sstevel@tonic-gate static krb5_data encivec_i[2], encivec_o[2];
760Sstevel@tonic-gate static krb5_keyusage enc_keyusage_i[2], enc_keyusage_o[2];
770Sstevel@tonic-gate static krb5_enctype final_enctype;
780Sstevel@tonic-gate static krb5_keyblock *skey;
790Sstevel@tonic-gate
800Sstevel@tonic-gate /* ARGSUSED */
810Sstevel@tonic-gate int
kcmd(int * sock,char ** ahost,ushort_t rport,char * locuser,char * remuser,char * cmd,int * fd2p,char * service,char * realm,krb5_context bsd_context,krb5_auth_context * authconp,krb5_creds ** cred,krb5_int32 * seqno,krb5_int32 * server_seqno,krb5_flags authopts,int anyport,enum kcmd_proto * protonump)820Sstevel@tonic-gate kcmd(int *sock, char **ahost, ushort_t rport,
830Sstevel@tonic-gate char *locuser, char *remuser,
840Sstevel@tonic-gate char *cmd, int *fd2p, char *service, char *realm,
850Sstevel@tonic-gate krb5_context bsd_context, krb5_auth_context *authconp,
860Sstevel@tonic-gate krb5_creds **cred, krb5_int32 *seqno, krb5_int32 *server_seqno,
870Sstevel@tonic-gate krb5_flags authopts,
880Sstevel@tonic-gate int anyport, enum kcmd_proto *protonump)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate int s = -1;
910Sstevel@tonic-gate sigset_t oldmask, urgmask;
920Sstevel@tonic-gate struct sockaddr_in sin;
930Sstevel@tonic-gate struct sockaddr_storage from;
940Sstevel@tonic-gate krb5_creds *get_cred = NULL;
950Sstevel@tonic-gate krb5_creds *ret_cred = NULL;
960Sstevel@tonic-gate char c;
970Sstevel@tonic-gate struct hostent *hp;
980Sstevel@tonic-gate int rc;
990Sstevel@tonic-gate char *host_save = NULL;
1000Sstevel@tonic-gate krb5_error_code status;
1010Sstevel@tonic-gate krb5_ap_rep_enc_part *rep_ret;
1020Sstevel@tonic-gate krb5_error *error = 0;
1030Sstevel@tonic-gate krb5_ccache cc;
1040Sstevel@tonic-gate krb5_data outbuf;
1050Sstevel@tonic-gate krb5_flags options = authopts;
1060Sstevel@tonic-gate krb5_auth_context auth_context = NULL;
1070Sstevel@tonic-gate char *cksumbuf;
1080Sstevel@tonic-gate krb5_data cksumdat;
1090Sstevel@tonic-gate int bsize = 0;
1100Sstevel@tonic-gate char *kcmd_version;
1110Sstevel@tonic-gate enum kcmd_proto protonum = *protonump;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate bsize = strlen(cmd) + strlen(remuser) + 64;
1140Sstevel@tonic-gate if ((cksumbuf = malloc(bsize)) == 0) {
1150Sstevel@tonic-gate (void) fprintf(stderr, gettext("Unable to allocate"
1160Sstevel@tonic-gate " memory for checksum buffer.\n"));
1170Sstevel@tonic-gate return (-1);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate (void) snprintf(cksumbuf, bsize, "%u:", ntohs(rport));
1200Sstevel@tonic-gate if (strlcat(cksumbuf, cmd, bsize) >= bsize) {
1210Sstevel@tonic-gate (void) fprintf(stderr, gettext("cmd buffer too long.\n"));
1220Sstevel@tonic-gate free(cksumbuf);
1230Sstevel@tonic-gate return (-1);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate if (strlcat(cksumbuf, remuser, bsize) >= bsize) {
1260Sstevel@tonic-gate (void) fprintf(stderr, gettext("remuser too long.\n"));
1270Sstevel@tonic-gate free(cksumbuf);
1280Sstevel@tonic-gate return (-1);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate cksumdat.data = cksumbuf;
1310Sstevel@tonic-gate cksumdat.length = strlen(cksumbuf);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate hp = gethostbyname(*ahost);
1340Sstevel@tonic-gate if (hp == 0) {
1350Sstevel@tonic-gate (void) fprintf(stderr,
1360Sstevel@tonic-gate gettext("%s: unknown host\n"), *ahost);
1370Sstevel@tonic-gate return (-1);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate if ((host_save = (char *)strdup(hp->h_name)) == NULL) {
1410Sstevel@tonic-gate (void) fprintf(stderr, gettext("kcmd: no memory\n"));
1420Sstevel@tonic-gate return (-1);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /* If no service is given set to the default service */
1460Sstevel@tonic-gate if (!service) service = default_service;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if (!(get_cred = (krb5_creds *)calloc(1, sizeof (krb5_creds)))) {
1490Sstevel@tonic-gate (void) fprintf(stderr, gettext("kcmd: no memory\n"));
1500Sstevel@tonic-gate return (-1);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate (void) sigemptyset(&urgmask);
1530Sstevel@tonic-gate (void) sigaddset(&urgmask, SIGURG);
1540Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &urgmask, &oldmask);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate status = krb5_sname_to_principal(bsd_context, host_save, service,
1570Sstevel@tonic-gate KRB5_NT_SRV_HST, &get_cred->server);
1580Sstevel@tonic-gate if (status) {
1590Sstevel@tonic-gate (void) fprintf(stderr,
1600Sstevel@tonic-gate gettext("kcmd: "
1610Sstevel@tonic-gate "krb5_sname_to_principal failed: %s\n"),
1620Sstevel@tonic-gate error_message(status));
1630Sstevel@tonic-gate status = -1;
1640Sstevel@tonic-gate goto bad;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate if (realm && *realm) {
1680Sstevel@tonic-gate (void) krb5_xfree(
1690Sstevel@tonic-gate krb5_princ_realm(bsd_context, get_cred->server)->data);
1700Sstevel@tonic-gate krb5_princ_set_realm_length(bsd_context, get_cred->server,
1710Sstevel@tonic-gate strlen(realm));
1720Sstevel@tonic-gate krb5_princ_set_realm_data(bsd_context, get_cred->server,
1730Sstevel@tonic-gate strdup(realm));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate s = socket(AF_INET, SOCK_STREAM, 0);
1770Sstevel@tonic-gate if (s < 0) {
1780Sstevel@tonic-gate perror(gettext("Error creating socket"));
1790Sstevel@tonic-gate status = -1;
1800Sstevel@tonic-gate goto bad;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate * Kerberos only supports IPv4 addresses for now.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate if (hp->h_addrtype == AF_INET) {
1860Sstevel@tonic-gate sin.sin_family = hp->h_addrtype;
1870Sstevel@tonic-gate (void) memcpy((void *)&sin.sin_addr,
1880Sstevel@tonic-gate hp->h_addr, hp->h_length);
1890Sstevel@tonic-gate sin.sin_port = rport;
1900Sstevel@tonic-gate } else {
1910Sstevel@tonic-gate syslog(LOG_ERR, "Address type %d not supported for "
1920Sstevel@tonic-gate "Kerberos", hp->h_addrtype);
1930Sstevel@tonic-gate status = -1;
1940Sstevel@tonic-gate goto bad;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
1980Sstevel@tonic-gate perror(host_save);
1990Sstevel@tonic-gate status = -1;
2000Sstevel@tonic-gate goto bad;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (fd2p == 0) {
2040Sstevel@tonic-gate (void) write(s, "", 1);
2050Sstevel@tonic-gate } else {
2060Sstevel@tonic-gate char num[16];
2070Sstevel@tonic-gate int s2;
2080Sstevel@tonic-gate int s3;
2090Sstevel@tonic-gate struct sockaddr_storage sname;
2100Sstevel@tonic-gate struct sockaddr_in *sp;
2110Sstevel@tonic-gate int len = sizeof (struct sockaddr_storage);
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate s2 = socket(AF_INET, SOCK_STREAM, 0);
2140Sstevel@tonic-gate if (s2 < 0) {
2150Sstevel@tonic-gate status = -1;
2160Sstevel@tonic-gate goto bad;
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate (void) memset((char *)&sin, 0, sizeof (sin));
2190Sstevel@tonic-gate sin.sin_family = AF_INET;
2200Sstevel@tonic-gate sin.sin_addr.s_addr = INADDR_ANY;
2210Sstevel@tonic-gate sin.sin_port = 0;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate if (bind(s2, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2240Sstevel@tonic-gate perror(gettext("error binding socket"));
2250Sstevel@tonic-gate (void) close(s2);
2260Sstevel@tonic-gate status = -1;
2270Sstevel@tonic-gate goto bad;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate if (getsockname(s2, (struct sockaddr *)&sname, &len) < 0) {
2300Sstevel@tonic-gate perror(gettext("getsockname error"));
2310Sstevel@tonic-gate (void) close(s2);
2320Sstevel@tonic-gate status = -1;
2330Sstevel@tonic-gate goto bad;
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate sp = (struct sockaddr_in *)&sname;
2360Sstevel@tonic-gate (void) listen(s2, 1);
2370Sstevel@tonic-gate (void) snprintf(num, sizeof (num), "%d",
2380Sstevel@tonic-gate htons((ushort_t)sp->sin_port));
2390Sstevel@tonic-gate if (write(s, num, strlen(num)+1) != strlen(num)+1) {
2400Sstevel@tonic-gate perror(gettext("write: error setting up stderr"));
2410Sstevel@tonic-gate (void) close(s2);
2420Sstevel@tonic-gate status = -1;
2430Sstevel@tonic-gate goto bad;
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate s3 = accept(s2, (struct sockaddr *)&from, &len);
2470Sstevel@tonic-gate (void) close(s2);
2480Sstevel@tonic-gate if (s3 < 0) {
2490Sstevel@tonic-gate perror(gettext("accept"));
2500Sstevel@tonic-gate status = -1;
2510Sstevel@tonic-gate goto bad;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate *fd2p = s3;
2540Sstevel@tonic-gate if (SOCK_FAMILY(from) == AF_INET) {
2550Sstevel@tonic-gate if (!anyport && SOCK_PORT(from) >= IPPORT_RESERVED) {
2560Sstevel@tonic-gate (void) fprintf(stderr,
2570Sstevel@tonic-gate gettext("socket: protocol "
2580Sstevel@tonic-gate "failure in circuit setup.\n"));
2590Sstevel@tonic-gate status = -1;
2600Sstevel@tonic-gate goto bad2;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate } else {
2630Sstevel@tonic-gate (void) fprintf(stderr,
2640Sstevel@tonic-gate gettext("Kerberos does not support "
2650Sstevel@tonic-gate "address type %d\n"),
2660Sstevel@tonic-gate SOCK_FAMILY(from));
2670Sstevel@tonic-gate status = -1;
2680Sstevel@tonic-gate goto bad2;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (status = krb5_cc_default(bsd_context, &cc))
2730Sstevel@tonic-gate goto bad2;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate status = krb5_cc_get_principal(bsd_context, cc, &get_cred->client);
2760Sstevel@tonic-gate if (status) {
2770Sstevel@tonic-gate (void) krb5_cc_close(bsd_context, cc);
2780Sstevel@tonic-gate goto bad2;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /* Get ticket from credentials cache or kdc */
2820Sstevel@tonic-gate status = krb5_get_credentials(bsd_context, 0, cc, get_cred, &ret_cred);
2830Sstevel@tonic-gate (void) krb5_cc_close(bsd_context, cc);
2840Sstevel@tonic-gate if (status) goto bad2;
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate /* Reset internal flags; these should not be sent. */
2870Sstevel@tonic-gate authopts &= (~OPTS_FORWARD_CREDS);
2880Sstevel@tonic-gate authopts &= (~OPTS_FORWARDABLE_CREDS);
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate if ((status = krb5_auth_con_init(bsd_context, &auth_context)))
2910Sstevel@tonic-gate goto bad2;
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate if ((status = krb5_auth_con_setflags(bsd_context, auth_context,
2940Sstevel@tonic-gate KRB5_AUTH_CONTEXT_RET_TIME)))
2950Sstevel@tonic-gate goto bad2;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate /* Only need local address for mk_cred() to send to krlogind */
2980Sstevel@tonic-gate if ((status = krb5_auth_con_genaddrs(bsd_context, auth_context, s,
2990Sstevel@tonic-gate KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)))
3000Sstevel@tonic-gate goto bad2;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate if (protonum == KCMD_PROTOCOL_COMPAT_HACK) {
3030Sstevel@tonic-gate krb5_boolean is_des;
3040Sstevel@tonic-gate status = krb5_c_enctype_compare(bsd_context,
3050Sstevel@tonic-gate ENCTYPE_DES_CBC_CRC,
3060Sstevel@tonic-gate ret_cred->keyblock.enctype,
3070Sstevel@tonic-gate &is_des);
3080Sstevel@tonic-gate if (status)
3090Sstevel@tonic-gate goto bad2;
3100Sstevel@tonic-gate protonum = is_des ? KCMD_OLD_PROTOCOL : KCMD_NEW_PROTOCOL;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate switch (protonum) {
3140Sstevel@tonic-gate case KCMD_NEW_PROTOCOL:
3150Sstevel@tonic-gate authopts |= AP_OPTS_USE_SUBKEY;
3160Sstevel@tonic-gate kcmd_version = "KCMDV0.2";
3170Sstevel@tonic-gate break;
3180Sstevel@tonic-gate case KCMD_OLD_PROTOCOL:
3190Sstevel@tonic-gate kcmd_version = "KCMDV0.1";
3200Sstevel@tonic-gate break;
3210Sstevel@tonic-gate default:
3220Sstevel@tonic-gate status = -1;
3230Sstevel@tonic-gate goto bad2;
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * Call the Kerberos library routine to obtain an authenticator,
3280Sstevel@tonic-gate * pass it over the socket to the server, and obtain mutual
3290Sstevel@tonic-gate * authentication.
3300Sstevel@tonic-gate */
3310Sstevel@tonic-gate status = krb5_sendauth(bsd_context, &auth_context, (krb5_pointer) &s,
3320Sstevel@tonic-gate kcmd_version, ret_cred->client, ret_cred->server,
3330Sstevel@tonic-gate authopts, &cksumdat, ret_cred, 0, &error,
3340Sstevel@tonic-gate &rep_ret, NULL);
3350Sstevel@tonic-gate krb5_xfree(cksumdat.data);
3360Sstevel@tonic-gate if (status) {
3370Sstevel@tonic-gate (void) fprintf(stderr, gettext("Couldn't authenticate"
3380Sstevel@tonic-gate " to server: %s\n"),
3390Sstevel@tonic-gate error_message(status));
3400Sstevel@tonic-gate if (error) {
3410Sstevel@tonic-gate (void) fprintf(stderr, gettext("Server returned error"
3420Sstevel@tonic-gate " code %d (%s)\n"),
3430Sstevel@tonic-gate error->error,
3440Sstevel@tonic-gate error_message(ERROR_TABLE_BASE_krb5 +
3450Sstevel@tonic-gate error->error));
3460Sstevel@tonic-gate if (error->text.length)
3470Sstevel@tonic-gate (void) fprintf(stderr,
3480Sstevel@tonic-gate gettext("Error text"
3490Sstevel@tonic-gate " sent from server: %s\n"),
3500Sstevel@tonic-gate error->text.data);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate if (error) {
3530Sstevel@tonic-gate krb5_free_error(bsd_context, error);
3540Sstevel@tonic-gate error = 0;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate goto bad2;
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate if (rep_ret && server_seqno) {
3590Sstevel@tonic-gate *server_seqno = rep_ret->seq_number;
3600Sstevel@tonic-gate krb5_free_ap_rep_enc_part(bsd_context, rep_ret);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate (void) write(s, remuser, strlen(remuser)+1);
3640Sstevel@tonic-gate (void) write(s, cmd, strlen(cmd)+1);
3650Sstevel@tonic-gate if (locuser)
3660Sstevel@tonic-gate (void) write(s, locuser, strlen(locuser)+1);
3670Sstevel@tonic-gate else
3680Sstevel@tonic-gate (void) write(s, "", 1);
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if (options & OPTS_FORWARD_CREDS) { /* Forward credentials */
3710Sstevel@tonic-gate if (status = krb5_fwd_tgt_creds(bsd_context, auth_context,
3720Sstevel@tonic-gate host_save,
3730Sstevel@tonic-gate ret_cred->client, ret_cred->server,
3740Sstevel@tonic-gate 0, options & OPTS_FORWARDABLE_CREDS,
3750Sstevel@tonic-gate &outbuf)) {
3760Sstevel@tonic-gate (void) fprintf(stderr,
3770Sstevel@tonic-gate gettext("kcmd: Error getting"
3780Sstevel@tonic-gate " forwarded creds\n"));
3790Sstevel@tonic-gate goto bad2;
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate /* Send forwarded credentials */
3820Sstevel@tonic-gate if (status = krb5_write_message(bsd_context, (krb5_pointer)&s,
3830Sstevel@tonic-gate &outbuf))
3840Sstevel@tonic-gate goto bad2;
3850Sstevel@tonic-gate } else { /* Dummy write to signal no forwarding */
3860Sstevel@tonic-gate outbuf.length = 0;
3870Sstevel@tonic-gate if (status = krb5_write_message(bsd_context,
3880Sstevel@tonic-gate (krb5_pointer)&s, &outbuf))
3890Sstevel@tonic-gate goto bad2;
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate if ((rc = read(s, &c, 1)) != 1) {
3930Sstevel@tonic-gate if (rc == -1) {
3940Sstevel@tonic-gate perror(*ahost);
3950Sstevel@tonic-gate } else {
3960Sstevel@tonic-gate (void) fprintf(stderr, gettext("kcmd: bad connection "
3970Sstevel@tonic-gate "with remote host\n"));
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate status = -1;
4000Sstevel@tonic-gate goto bad2;
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate if (c != 0) {
4030Sstevel@tonic-gate while (read(s, &c, 1) == 1) {
4040Sstevel@tonic-gate (void) write(2, &c, 1);
4050Sstevel@tonic-gate if (c == '\n')
4060Sstevel@tonic-gate break;
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate status = -1;
4090Sstevel@tonic-gate goto bad2;
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
4120Sstevel@tonic-gate *sock = s;
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate /* pass back credentials if wanted */
415*11415SSurya.Prakki@Sun.COM if (cred) (void) krb5_copy_creds(bsd_context, ret_cred, cred);
4160Sstevel@tonic-gate krb5_free_creds(bsd_context, ret_cred);
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate * Initialize *authconp to auth_context, so
4190Sstevel@tonic-gate * that the clients can make use of it
4200Sstevel@tonic-gate */
4210Sstevel@tonic-gate *authconp = auth_context;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate return (0);
4240Sstevel@tonic-gate bad2:
4250Sstevel@tonic-gate if (fd2p != NULL)
4260Sstevel@tonic-gate (void) close(*fd2p);
4270Sstevel@tonic-gate bad:
4280Sstevel@tonic-gate if (s > 0)
4290Sstevel@tonic-gate (void) close(s);
4300Sstevel@tonic-gate if (get_cred)
4310Sstevel@tonic-gate krb5_free_creds(bsd_context, get_cred);
4320Sstevel@tonic-gate if (ret_cred)
4330Sstevel@tonic-gate krb5_free_creds(bsd_context, ret_cred);
4340Sstevel@tonic-gate if (host_save)
4350Sstevel@tonic-gate free(host_save);
4360Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
4370Sstevel@tonic-gate return (status);
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate * Strsave was a routine in the version 4 krb library: we put it here
4420Sstevel@tonic-gate * for compatablilty with version 5 krb library, since kcmd.o is linked
4430Sstevel@tonic-gate * into all programs.
4440Sstevel@tonic-gate */
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate char *
strsave(char * sp)4470Sstevel@tonic-gate strsave(char *sp)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate char *ret;
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate if ((ret = (char *)strdup(sp)) == NULL) {
4520Sstevel@tonic-gate (void) fprintf(stderr, gettext("no memory for saving args\n"));
4530Sstevel@tonic-gate exit(1);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate return (ret);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate * This routine is to initialize the desinbuf, desoutbuf and the session key
4610Sstevel@tonic-gate * structures to carry out desread()'s and deswrite()'s successfully
4620Sstevel@tonic-gate */
4630Sstevel@tonic-gate void
init_encrypt(int enc,krb5_context ctxt,enum kcmd_proto protonum,krb5_data * inbuf,krb5_data * outbuf,int amclient,krb5_encrypt_block * block)4640Sstevel@tonic-gate init_encrypt(int enc, krb5_context ctxt, enum kcmd_proto protonum,
4650Sstevel@tonic-gate krb5_data *inbuf, krb5_data *outbuf,
4660Sstevel@tonic-gate int amclient, krb5_encrypt_block *block)
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate krb5_error_code statuscode;
4690Sstevel@tonic-gate size_t blocksize;
4700Sstevel@tonic-gate int i;
4710Sstevel@tonic-gate krb5_error_code ret;
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate kcmd_context = ctxt;
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate if (enc > 0) {
4760Sstevel@tonic-gate desinbuf.data = inbuf->data;
4770Sstevel@tonic-gate desoutbuf.data = outbuf->data + 4;
4780Sstevel@tonic-gate desinbuf.length = inbuf->length;
4790Sstevel@tonic-gate desoutbuf.length = outbuf->length + 4;
4800Sstevel@tonic-gate encrypt_flag = B_TRUE;
4810Sstevel@tonic-gate } else {
4820Sstevel@tonic-gate encrypt_flag = B_FALSE;
4830Sstevel@tonic-gate return;
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate skey = block->key;
4870Sstevel@tonic-gate final_enctype = skey->enctype;
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate enc_keyusage_i[0] = KCMD_KEYUSAGE;
4900Sstevel@tonic-gate enc_keyusage_i[1] = KCMD_KEYUSAGE;
4910Sstevel@tonic-gate enc_keyusage_o[0] = KCMD_KEYUSAGE;
4920Sstevel@tonic-gate enc_keyusage_o[1] = KCMD_KEYUSAGE;
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate if (protonum == KCMD_OLD_PROTOCOL) {
4950Sstevel@tonic-gate use_ivecs = B_FALSE;
4960Sstevel@tonic-gate return;
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate use_ivecs = B_TRUE;
5000Sstevel@tonic-gate switch (skey->enctype) {
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate * For the DES-based enctypes and the 3DES enctype we
5030Sstevel@tonic-gate * want to use a non-zero IV because that's what we did.
5040Sstevel@tonic-gate * In the future we use different keyusage for each
5050Sstevel@tonic-gate * channel and direction and a fresh cipher state.
5060Sstevel@tonic-gate */
5070Sstevel@tonic-gate case ENCTYPE_DES_CBC_CRC:
5080Sstevel@tonic-gate case ENCTYPE_DES_CBC_MD4:
5090Sstevel@tonic-gate case ENCTYPE_DES_CBC_MD5:
5100Sstevel@tonic-gate case ENCTYPE_DES3_CBC_SHA1:
5110Sstevel@tonic-gate statuscode = krb5_c_block_size(kcmd_context, final_enctype,
5120Sstevel@tonic-gate &blocksize);
5130Sstevel@tonic-gate if (statuscode) {
5140Sstevel@tonic-gate /* XXX what do I do? */
5150Sstevel@tonic-gate abort();
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate encivec_i[0].length = encivec_i[1].length =
5190Sstevel@tonic-gate encivec_o[0].length = encivec_o[1].length = blocksize;
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate if ((encivec_i[0].data = malloc(encivec_i[0].length * 4))
5220Sstevel@tonic-gate == NULL) {
5230Sstevel@tonic-gate /* XXX what do I do? */
5240Sstevel@tonic-gate abort();
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate encivec_i[1].data = encivec_i[0].data + encivec_i[0].length;
5270Sstevel@tonic-gate encivec_o[0].data = encivec_i[1].data + encivec_i[0].length;
5280Sstevel@tonic-gate encivec_o[1].data = encivec_o[0].data + encivec_i[0].length;
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate /* is there a better way to initialize this? */
5310Sstevel@tonic-gate (void) memset(encivec_i[0].data, amclient, blocksize);
5320Sstevel@tonic-gate (void) memset(encivec_o[0].data, 1 - amclient, blocksize);
5330Sstevel@tonic-gate (void) memset(encivec_i[1].data, 2 | amclient, blocksize);
5340Sstevel@tonic-gate (void) memset(encivec_o[1].data, 2 | (1 - amclient), blocksize);
5350Sstevel@tonic-gate break;
5360Sstevel@tonic-gate default:
5370Sstevel@tonic-gate if (amclient) {
5380Sstevel@tonic-gate enc_keyusage_i[0] = 1028;
5390Sstevel@tonic-gate enc_keyusage_i[1] = 1030;
5400Sstevel@tonic-gate enc_keyusage_o[0] = 1032;
5410Sstevel@tonic-gate enc_keyusage_o[1] = 1034;
5420Sstevel@tonic-gate } else { /* amclient */
5430Sstevel@tonic-gate enc_keyusage_i[0] = 1032;
5440Sstevel@tonic-gate enc_keyusage_i[1] = 1034;
5450Sstevel@tonic-gate enc_keyusage_o[0] = 1028;
5460Sstevel@tonic-gate enc_keyusage_o[1] = 1030;
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate for (i = 0; i < 2; i++) {
5490Sstevel@tonic-gate ret = krb5_c_init_state(ctxt,
5500Sstevel@tonic-gate skey, enc_keyusage_i[i],
5510Sstevel@tonic-gate &encivec_i[i]);
5520Sstevel@tonic-gate if (ret)
5530Sstevel@tonic-gate goto fail;
5540Sstevel@tonic-gate ret = krb5_c_init_state(ctxt,
5550Sstevel@tonic-gate skey, enc_keyusage_o[i],
5560Sstevel@tonic-gate &encivec_o[i]);
5570Sstevel@tonic-gate if (ret)
5580Sstevel@tonic-gate goto fail;
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate break;
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate return;
5630Sstevel@tonic-gate fail:
5640Sstevel@tonic-gate abort();
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate int
desread(int fd,char * buf,int len,int secondary)5680Sstevel@tonic-gate desread(int fd, char *buf, int len, int secondary)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate int nreturned = 0;
5710Sstevel@tonic-gate long net_len, rd_len;
5720Sstevel@tonic-gate int cc;
5730Sstevel@tonic-gate size_t ret = 0;
5740Sstevel@tonic-gate unsigned char len_buf[4];
5750Sstevel@tonic-gate krb5_enc_data inputd;
5760Sstevel@tonic-gate krb5_data outputd;
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate if (!encrypt_flag)
5790Sstevel@tonic-gate return (read(fd, buf, len));
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate * If there is stored data from a previous read,
5830Sstevel@tonic-gate * put it into the output buffer and return it now.
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate if (nstored >= len) {
5860Sstevel@tonic-gate (void) memcpy(buf, store_ptr, len);
5870Sstevel@tonic-gate store_ptr += len;
5880Sstevel@tonic-gate nstored -= len;
5890Sstevel@tonic-gate return (len);
5900Sstevel@tonic-gate } else if (nstored) {
5910Sstevel@tonic-gate (void) memcpy(buf, store_ptr, nstored);
5920Sstevel@tonic-gate nreturned += nstored;
5930Sstevel@tonic-gate buf += nstored;
5940Sstevel@tonic-gate len -= nstored;
5950Sstevel@tonic-gate nstored = 0;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate if ((cc = krb5_net_read(kcmd_context, fd, (char *)len_buf, 4)) != 4) {
5990Sstevel@tonic-gate if ((cc < 0) && ((errno == EWOULDBLOCK) || (errno == EAGAIN)))
6000Sstevel@tonic-gate return (cc);
6010Sstevel@tonic-gate /* XXX can't read enough, pipe must have closed */
6020Sstevel@tonic-gate return (0);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate rd_len = ((len_buf[0] << 24) | (len_buf[1] << 16) |
6050Sstevel@tonic-gate (len_buf[2] << 8) | len_buf[3]);
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate if (krb5_c_encrypt_length(kcmd_context, final_enctype,
6080Sstevel@tonic-gate use_ivecs ? (size_t)rd_len + 4 : (size_t)rd_len,
6090Sstevel@tonic-gate &ret))
6100Sstevel@tonic-gate net_len = ((size_t)-1);
6110Sstevel@tonic-gate else
6120Sstevel@tonic-gate net_len = ret;
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate if ((net_len <= 0) || (net_len > desinbuf.length)) {
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate * preposterous length; assume out-of-sync; only recourse
6170Sstevel@tonic-gate * is to close connection, so return 0
6180Sstevel@tonic-gate */
6190Sstevel@tonic-gate (void) fprintf(stderr, gettext("Read size problem.\n"));
6200Sstevel@tonic-gate return (0);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate if ((cc = krb5_net_read(kcmd_context, fd, desinbuf.data, net_len))
6240Sstevel@tonic-gate != net_len) {
6250Sstevel@tonic-gate /* pipe must have closed, return 0 */
6260Sstevel@tonic-gate (void) fprintf(stderr,
6270Sstevel@tonic-gate gettext("Read error: length received %d "
6280Sstevel@tonic-gate "!= expected %d.\n"),
6290Sstevel@tonic-gate cc, net_len);
6300Sstevel@tonic-gate return (0);
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate /*
6340Sstevel@tonic-gate * Decrypt information
6350Sstevel@tonic-gate */
6360Sstevel@tonic-gate inputd.enctype = ENCTYPE_UNKNOWN;
6370Sstevel@tonic-gate inputd.ciphertext.length = net_len;
6380Sstevel@tonic-gate inputd.ciphertext.data = (krb5_pointer)desinbuf.data;
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate outputd.length = sizeof (storage);
6410Sstevel@tonic-gate outputd.data = (krb5_pointer)storage;
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate * data is decrypted into the "storage" buffer, which
6450Sstevel@tonic-gate * had better be large enough!
6460Sstevel@tonic-gate */
6470Sstevel@tonic-gate cc = krb5_c_decrypt(kcmd_context, skey,
6480Sstevel@tonic-gate enc_keyusage_i[secondary],
6490Sstevel@tonic-gate use_ivecs ? encivec_i + secondary : 0,
6500Sstevel@tonic-gate &inputd, &outputd);
6510Sstevel@tonic-gate if (cc) {
6520Sstevel@tonic-gate (void) fprintf(stderr, gettext("Cannot decrypt data "
6530Sstevel@tonic-gate "from network\n"));
6540Sstevel@tonic-gate return (0);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate store_ptr = storage;
6580Sstevel@tonic-gate nstored = rd_len;
6590Sstevel@tonic-gate if (use_ivecs == B_TRUE) {
6600Sstevel@tonic-gate int rd_len2;
6610Sstevel@tonic-gate rd_len2 = storage[0] & 0xff;
6620Sstevel@tonic-gate rd_len2 <<= 8; rd_len2 |= storage[1] & 0xff;
6630Sstevel@tonic-gate rd_len2 <<= 8; rd_len2 |= storage[2] & 0xff;
6640Sstevel@tonic-gate rd_len2 <<= 8; rd_len2 |= storage[3] & 0xff;
6650Sstevel@tonic-gate if (rd_len2 != rd_len) {
6660Sstevel@tonic-gate /* cleartext length trashed? */
6670Sstevel@tonic-gate errno = EIO;
6680Sstevel@tonic-gate return (-1);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate store_ptr += 4;
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate * Copy only as much data as the input buffer will allow.
6740Sstevel@tonic-gate * The rest is kept in the 'storage' pointer for the next
6750Sstevel@tonic-gate * read.
6760Sstevel@tonic-gate */
6770Sstevel@tonic-gate if (nstored > len) {
6780Sstevel@tonic-gate (void) memcpy(buf, store_ptr, len);
6790Sstevel@tonic-gate nreturned += len;
6800Sstevel@tonic-gate store_ptr += len;
6810Sstevel@tonic-gate nstored -= len;
6820Sstevel@tonic-gate } else {
6830Sstevel@tonic-gate (void) memcpy(buf, store_ptr, nstored);
6840Sstevel@tonic-gate nreturned += nstored;
6850Sstevel@tonic-gate nstored = 0;
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate return (nreturned);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate int
deswrite(int fd,char * buf,int len,int secondary)6910Sstevel@tonic-gate deswrite(int fd, char *buf, int len, int secondary)
6920Sstevel@tonic-gate {
6933518Spk193450 int bytes_written;
6943518Spk193450 int r;
6953518Spk193450 int outlen;
6963518Spk193450 char *p;
6973518Spk193450 if (!encrypt_flag)
6983518Spk193450 return (write(fd, buf, len));
6993518Spk193450
7003518Spk193450 bytes_written = 0;
7013518Spk193450 while (len > 0) {
7023518Spk193450 p = buf + bytes_written;
7033518Spk193450 if (len > KCMD8_BUFSIZ)
7043518Spk193450 outlen = KCMD8_BUFSIZ;
7053518Spk193450 else
7063518Spk193450 outlen = len;
7073518Spk193450 r = deswrite_compat(fd, p, outlen, secondary);
7083518Spk193450 if (r == -1)
7093518Spk193450 return (r);
7103518Spk193450 bytes_written += r;
7113518Spk193450 len -= r;
7123518Spk193450 }
7133518Spk193450 return (bytes_written);
7143518Spk193450 }
7153518Spk193450 static int
deswrite_compat(int fd,char * buf,int len,int secondary)7163518Spk193450 deswrite_compat(int fd, char *buf, int len, int secondary)
7173518Spk193450 {
7180Sstevel@tonic-gate int cc;
7190Sstevel@tonic-gate size_t ret = 0;
7200Sstevel@tonic-gate krb5_data inputd;
7210Sstevel@tonic-gate krb5_enc_data outputd;
7220Sstevel@tonic-gate char tmpbuf[KCMD_BUFSIZ + 8];
7230Sstevel@tonic-gate char encrbuf[KCMD_BUFSIZ + 8];
7240Sstevel@tonic-gate unsigned char *len_buf = (unsigned char *)tmpbuf;
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate if (use_ivecs == B_TRUE) {
7270Sstevel@tonic-gate unsigned char *lenbuf2 = (unsigned char *)tmpbuf;
7280Sstevel@tonic-gate if (len + 4 > sizeof (tmpbuf))
7290Sstevel@tonic-gate abort();
7300Sstevel@tonic-gate lenbuf2[0] = (len & 0xff000000) >> 24;
7310Sstevel@tonic-gate lenbuf2[1] = (len & 0xff0000) >> 16;
7320Sstevel@tonic-gate lenbuf2[2] = (len & 0xff00) >> 8;
7330Sstevel@tonic-gate lenbuf2[3] = (len & 0xff);
7340Sstevel@tonic-gate (void) memcpy(tmpbuf + 4, buf, len);
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate inputd.data = (krb5_pointer)tmpbuf;
7370Sstevel@tonic-gate inputd.length = len + 4;
7380Sstevel@tonic-gate } else {
7390Sstevel@tonic-gate inputd.data = (krb5_pointer)buf;
7400Sstevel@tonic-gate inputd.length = len;
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate desoutbuf.data = encrbuf;
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate if (krb5_c_encrypt_length(kcmd_context, final_enctype,
7460Sstevel@tonic-gate use_ivecs ? (size_t)len + 4 : (size_t)len, &ret)) {
7470Sstevel@tonic-gate desoutbuf.length = ((size_t)-1);
7480Sstevel@tonic-gate goto err;
7490Sstevel@tonic-gate } else {
7500Sstevel@tonic-gate desoutbuf.length = ret;
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate if (desoutbuf.length > MAXSIZE) {
7540Sstevel@tonic-gate (void) fprintf(stderr, gettext("Write size problem.\n"));
7550Sstevel@tonic-gate return (-1);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate * Encrypt information
7600Sstevel@tonic-gate */
7610Sstevel@tonic-gate outputd.ciphertext.length = desoutbuf.length;
7620Sstevel@tonic-gate outputd.ciphertext.data = (krb5_pointer)desoutbuf.data;
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate cc = krb5_c_encrypt(kcmd_context, skey,
7650Sstevel@tonic-gate enc_keyusage_o[secondary],
7660Sstevel@tonic-gate use_ivecs ? encivec_o + secondary : 0,
7670Sstevel@tonic-gate &inputd, &outputd);
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate if (cc) {
7700Sstevel@tonic-gate err:
7710Sstevel@tonic-gate (void) fprintf(stderr, gettext("Write encrypt problem.\n"));
7720Sstevel@tonic-gate return (-1);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate len_buf[0] = (len & 0xff000000) >> 24;
7760Sstevel@tonic-gate len_buf[1] = (len & 0xff0000) >> 16;
7770Sstevel@tonic-gate len_buf[2] = (len & 0xff00) >> 8;
7780Sstevel@tonic-gate len_buf[3] = (len & 0xff);
7790Sstevel@tonic-gate (void) write(fd, len_buf, 4);
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate if (write(fd, desoutbuf.data, desoutbuf.length) != desoutbuf.length) {
7820Sstevel@tonic-gate (void) fprintf(stderr, gettext("Could not write "
7830Sstevel@tonic-gate "out all data.\n"));
7840Sstevel@tonic-gate return (-1);
7850Sstevel@tonic-gate } else {
7860Sstevel@tonic-gate return (len);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate }
789