xref: /titanic_41/usr/src/cmd/cmd-inet/common/kcmd.c (revision 0b4fd3b107539afdb6ac991e02328c2176598b78)
17c478bd9Sstevel@tonic-gate /*
2*0b4fd3b1SSurya Prakki  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
167c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
197c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
207c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /* derived from @(#)rcmd.c	5.17 (Berkeley) 6/27/88 */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <unistd.h>
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <pwd.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <sys/socket.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
427c478bd9Sstevel@tonic-gate #include <netdb.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <syslog.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include <com_err.h>
487c478bd9Sstevel@tonic-gate #include <k5-int.h>
497c478bd9Sstevel@tonic-gate #include <kcmd.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char *default_service = "host";
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	KCMD_BUFSIZ	102400
541509e105Spk193450 #define	KCMD8_BUFSIZ	(4096 - 256)
551509e105Spk193450 /*
561509e105Spk193450  * For compatibility with earlier versions of Solaris and other OS
571509e105Spk193450  * (kerborized rsh uses 4KB of RSH_BUFSIZE)- 256 to make sure
581509e105Spk193450  * there is room
591509e105Spk193450  */
601509e105Spk193450 static int deswrite_compat(int, char *, int, int);
611509e105Spk193450 
627c478bd9Sstevel@tonic-gate #define	KCMD_KEYUSAGE	1026
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static char storage[KCMD_BUFSIZ];
657c478bd9Sstevel@tonic-gate static int nstored = 0;
667c478bd9Sstevel@tonic-gate static int MAXSIZE = (KCMD_BUFSIZ + 8);
677c478bd9Sstevel@tonic-gate static char *store_ptr = storage;
687c478bd9Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static boolean_t encrypt_flag = B_FALSE;
717c478bd9Sstevel@tonic-gate static krb5_context kcmd_context;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* XXX Overloaded: use_ivecs!=0 -> new protocol, inband signalling, etc.  */
747c478bd9Sstevel@tonic-gate static boolean_t use_ivecs = B_FALSE;
757c478bd9Sstevel@tonic-gate static krb5_data encivec_i[2], encivec_o[2];
767c478bd9Sstevel@tonic-gate static krb5_keyusage enc_keyusage_i[2], enc_keyusage_o[2];
777c478bd9Sstevel@tonic-gate static krb5_enctype final_enctype;
787c478bd9Sstevel@tonic-gate static krb5_keyblock *skey;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* ARGSUSED */
817c478bd9Sstevel@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)827c478bd9Sstevel@tonic-gate kcmd(int *sock, char **ahost, ushort_t rport,
837c478bd9Sstevel@tonic-gate 	char *locuser, char *remuser,
847c478bd9Sstevel@tonic-gate 	char *cmd, int *fd2p, char *service, char *realm,
857c478bd9Sstevel@tonic-gate 	krb5_context bsd_context, krb5_auth_context *authconp,
867c478bd9Sstevel@tonic-gate 	krb5_creds **cred, krb5_int32 *seqno, krb5_int32 *server_seqno,
877c478bd9Sstevel@tonic-gate 	krb5_flags authopts,
887c478bd9Sstevel@tonic-gate 	int anyport, enum kcmd_proto *protonump)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	int s = -1;
917c478bd9Sstevel@tonic-gate 	sigset_t oldmask, urgmask;
927c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
937c478bd9Sstevel@tonic-gate 	struct sockaddr_storage from;
947c478bd9Sstevel@tonic-gate 	krb5_creds *get_cred = NULL;
957c478bd9Sstevel@tonic-gate 	krb5_creds *ret_cred = NULL;
967c478bd9Sstevel@tonic-gate 	char c;
977c478bd9Sstevel@tonic-gate 	struct hostent *hp;
987c478bd9Sstevel@tonic-gate 	int rc;
997c478bd9Sstevel@tonic-gate 	char *host_save = NULL;
1007c478bd9Sstevel@tonic-gate 	krb5_error_code status;
1017c478bd9Sstevel@tonic-gate 	krb5_ap_rep_enc_part *rep_ret;
1027c478bd9Sstevel@tonic-gate 	krb5_error	*error = 0;
1037c478bd9Sstevel@tonic-gate 	krb5_ccache cc;
1047c478bd9Sstevel@tonic-gate 	krb5_data outbuf;
1057c478bd9Sstevel@tonic-gate 	krb5_flags options = authopts;
1067c478bd9Sstevel@tonic-gate 	krb5_auth_context auth_context = NULL;
1077c478bd9Sstevel@tonic-gate 	char *cksumbuf;
1087c478bd9Sstevel@tonic-gate 	krb5_data cksumdat;
1097c478bd9Sstevel@tonic-gate 	int bsize = 0;
1107c478bd9Sstevel@tonic-gate 	char *kcmd_version;
1117c478bd9Sstevel@tonic-gate 	enum kcmd_proto protonum = *protonump;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	bsize = strlen(cmd) + strlen(remuser) + 64;
1147c478bd9Sstevel@tonic-gate 	if ((cksumbuf = malloc(bsize)) == 0) {
1157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Unable to allocate"
1167c478bd9Sstevel@tonic-gate 					    " memory for checksum buffer.\n"));
1177c478bd9Sstevel@tonic-gate 		return (-1);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	(void) snprintf(cksumbuf, bsize, "%u:", ntohs(rport));
1207c478bd9Sstevel@tonic-gate 	if (strlcat(cksumbuf, cmd, bsize) >= bsize) {
1217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cmd buffer too long.\n"));
1227c478bd9Sstevel@tonic-gate 		free(cksumbuf);
1237c478bd9Sstevel@tonic-gate 		return (-1);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	if (strlcat(cksumbuf, remuser, bsize) >= bsize) {
1267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("remuser too long.\n"));
1277c478bd9Sstevel@tonic-gate 		free(cksumbuf);
1287c478bd9Sstevel@tonic-gate 		return (-1);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	cksumdat.data = cksumbuf;
1317c478bd9Sstevel@tonic-gate 	cksumdat.length = strlen(cksumbuf);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	hp = gethostbyname(*ahost);
1347c478bd9Sstevel@tonic-gate 	if (hp == 0) {
1357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1367c478bd9Sstevel@tonic-gate 			    gettext("%s: unknown host\n"), *ahost);
1377c478bd9Sstevel@tonic-gate 		return (-1);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((host_save = (char *)strdup(hp->h_name)) == NULL) {
1417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("kcmd: no memory\n"));
1427c478bd9Sstevel@tonic-gate 		return (-1);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/* If no service is given set to the default service */
1467c478bd9Sstevel@tonic-gate 	if (!service) service = default_service;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (!(get_cred = (krb5_creds *)calloc(1, sizeof (krb5_creds)))) {
1497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("kcmd: no memory\n"));
1507c478bd9Sstevel@tonic-gate 		return (-1);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&urgmask);
1537c478bd9Sstevel@tonic-gate 	(void) sigaddset(&urgmask, SIGURG);
1547c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &urgmask, &oldmask);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	status = krb5_sname_to_principal(bsd_context, host_save, service,
1577c478bd9Sstevel@tonic-gate 					KRB5_NT_SRV_HST, &get_cred->server);
1587c478bd9Sstevel@tonic-gate 	if (status) {
1597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1607c478bd9Sstevel@tonic-gate 			    gettext("kcmd: "
1617c478bd9Sstevel@tonic-gate 				    "krb5_sname_to_principal failed: %s\n"),
1627c478bd9Sstevel@tonic-gate 			    error_message(status));
1637c478bd9Sstevel@tonic-gate 		status = -1;
1647c478bd9Sstevel@tonic-gate 		goto bad;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (realm && *realm) {
1687c478bd9Sstevel@tonic-gate 		(void) krb5_xfree(
1697c478bd9Sstevel@tonic-gate 			krb5_princ_realm(bsd_context, get_cred->server)->data);
1707c478bd9Sstevel@tonic-gate 		krb5_princ_set_realm_length(bsd_context, get_cred->server,
1717c478bd9Sstevel@tonic-gate 					strlen(realm));
1727c478bd9Sstevel@tonic-gate 		krb5_princ_set_realm_data(bsd_context, get_cred->server,
1737c478bd9Sstevel@tonic-gate 						strdup(realm));
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	s = socket(AF_INET, SOCK_STREAM, 0);
1777c478bd9Sstevel@tonic-gate 	if (s < 0) {
1787c478bd9Sstevel@tonic-gate 		perror(gettext("Error creating socket"));
1797c478bd9Sstevel@tonic-gate 		status = -1;
1807c478bd9Sstevel@tonic-gate 		goto bad;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Kerberos only supports IPv4 addresses for now.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	if (hp->h_addrtype == AF_INET) {
1867c478bd9Sstevel@tonic-gate 		sin.sin_family = hp->h_addrtype;
1877c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)&sin.sin_addr,
1887c478bd9Sstevel@tonic-gate 			    hp->h_addr, hp->h_length);
1897c478bd9Sstevel@tonic-gate 		sin.sin_port = rport;
1907c478bd9Sstevel@tonic-gate 	} else {
1917c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Address type %d not supported for "
1927c478bd9Sstevel@tonic-gate 		    "Kerberos", hp->h_addrtype);
1937c478bd9Sstevel@tonic-gate 		status = -1;
1947c478bd9Sstevel@tonic-gate 		goto bad;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
1987c478bd9Sstevel@tonic-gate 		perror(host_save);
1997c478bd9Sstevel@tonic-gate 		status = -1;
2007c478bd9Sstevel@tonic-gate 		goto bad;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (fd2p == 0) {
2047c478bd9Sstevel@tonic-gate 		(void) write(s, "", 1);
2057c478bd9Sstevel@tonic-gate 	} else {
2067c478bd9Sstevel@tonic-gate 		char num[16];
2077c478bd9Sstevel@tonic-gate 		int s2;
2087c478bd9Sstevel@tonic-gate 		int s3;
2097c478bd9Sstevel@tonic-gate 		struct sockaddr_storage sname;
2107c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sp;
2117c478bd9Sstevel@tonic-gate 		int len = sizeof (struct sockaddr_storage);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		s2 = socket(AF_INET, SOCK_STREAM, 0);
2147c478bd9Sstevel@tonic-gate 		if (s2 < 0) {
2157c478bd9Sstevel@tonic-gate 			status = -1;
2167c478bd9Sstevel@tonic-gate 			goto bad;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 		(void) memset((char *)&sin, 0, sizeof (sin));
2197c478bd9Sstevel@tonic-gate 		sin.sin_family = AF_INET;
2207c478bd9Sstevel@tonic-gate 		sin.sin_addr.s_addr = INADDR_ANY;
2217c478bd9Sstevel@tonic-gate 		sin.sin_port = 0;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		if (bind(s2, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2247c478bd9Sstevel@tonic-gate 			perror(gettext("error binding socket"));
2257c478bd9Sstevel@tonic-gate 			(void) close(s2);
2267c478bd9Sstevel@tonic-gate 			status = -1;
2277c478bd9Sstevel@tonic-gate 			goto bad;
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		if (getsockname(s2, (struct sockaddr *)&sname, &len) < 0) {
2307c478bd9Sstevel@tonic-gate 			perror(gettext("getsockname error"));
2317c478bd9Sstevel@tonic-gate 			(void) close(s2);
2327c478bd9Sstevel@tonic-gate 			status = -1;
2337c478bd9Sstevel@tonic-gate 			goto bad;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		sp = (struct sockaddr_in *)&sname;
2367c478bd9Sstevel@tonic-gate 		(void) listen(s2, 1);
2377c478bd9Sstevel@tonic-gate 		(void) snprintf(num, sizeof (num), "%d",
2387c478bd9Sstevel@tonic-gate 			    htons((ushort_t)sp->sin_port));
2397c478bd9Sstevel@tonic-gate 		if (write(s, num, strlen(num)+1) != strlen(num)+1) {
2407c478bd9Sstevel@tonic-gate 			perror(gettext("write: error setting up stderr"));
2417c478bd9Sstevel@tonic-gate 			(void) close(s2);
2427c478bd9Sstevel@tonic-gate 			status = -1;
2437c478bd9Sstevel@tonic-gate 			goto bad;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		s3 = accept(s2, (struct sockaddr *)&from, &len);
2477c478bd9Sstevel@tonic-gate 		(void) close(s2);
2487c478bd9Sstevel@tonic-gate 		if (s3 < 0) {
2497c478bd9Sstevel@tonic-gate 			perror(gettext("accept"));
2507c478bd9Sstevel@tonic-gate 			status = -1;
2517c478bd9Sstevel@tonic-gate 			goto bad;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		*fd2p = s3;
2547c478bd9Sstevel@tonic-gate 		if (SOCK_FAMILY(from) == AF_INET) {
2557c478bd9Sstevel@tonic-gate 			if (!anyport && SOCK_PORT(from) >= IPPORT_RESERVED) {
2567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2577c478bd9Sstevel@tonic-gate 				    gettext("socket: protocol "
2587c478bd9Sstevel@tonic-gate 					    "failure in circuit setup.\n"));
2597c478bd9Sstevel@tonic-gate 				status = -1;
2607c478bd9Sstevel@tonic-gate 				goto bad2;
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 		} else {
2637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2647c478bd9Sstevel@tonic-gate 				    gettext("Kerberos does not support "
2657c478bd9Sstevel@tonic-gate 					    "address type %d\n"),
2667c478bd9Sstevel@tonic-gate 				    SOCK_FAMILY(from));
2677c478bd9Sstevel@tonic-gate 			status = -1;
2687c478bd9Sstevel@tonic-gate 			goto bad2;
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (status = krb5_cc_default(bsd_context, &cc))
2737c478bd9Sstevel@tonic-gate 		goto bad2;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	status = krb5_cc_get_principal(bsd_context, cc, &get_cred->client);
2767c478bd9Sstevel@tonic-gate 	if (status) {
2777c478bd9Sstevel@tonic-gate 		(void) krb5_cc_close(bsd_context, cc);
2787c478bd9Sstevel@tonic-gate 		goto bad2;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/* Get ticket from credentials cache or kdc */
2827c478bd9Sstevel@tonic-gate 	status = krb5_get_credentials(bsd_context, 0, cc, get_cred, &ret_cred);
2837c478bd9Sstevel@tonic-gate 	(void) krb5_cc_close(bsd_context, cc);
2847c478bd9Sstevel@tonic-gate 	if (status) goto bad2;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/* Reset internal flags; these should not be sent. */
2877c478bd9Sstevel@tonic-gate 	authopts &= (~OPTS_FORWARD_CREDS);
2887c478bd9Sstevel@tonic-gate 	authopts &= (~OPTS_FORWARDABLE_CREDS);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if ((status = krb5_auth_con_init(bsd_context, &auth_context)))
2917c478bd9Sstevel@tonic-gate 		goto bad2;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	if ((status = krb5_auth_con_setflags(bsd_context, auth_context,
2947c478bd9Sstevel@tonic-gate 					KRB5_AUTH_CONTEXT_RET_TIME)))
2957c478bd9Sstevel@tonic-gate 		goto bad2;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* Only need local address for mk_cred() to send to krlogind */
2987c478bd9Sstevel@tonic-gate 	if ((status = krb5_auth_con_genaddrs(bsd_context, auth_context, s,
2997c478bd9Sstevel@tonic-gate 			KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR)))
3007c478bd9Sstevel@tonic-gate 		goto bad2;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (protonum == KCMD_PROTOCOL_COMPAT_HACK) {
3037c478bd9Sstevel@tonic-gate 		krb5_boolean is_des;
3047c478bd9Sstevel@tonic-gate 		status = krb5_c_enctype_compare(bsd_context,
3057c478bd9Sstevel@tonic-gate 						ENCTYPE_DES_CBC_CRC,
3067c478bd9Sstevel@tonic-gate 						ret_cred->keyblock.enctype,
3077c478bd9Sstevel@tonic-gate 						&is_des);
3087c478bd9Sstevel@tonic-gate 		if (status)
3097c478bd9Sstevel@tonic-gate 			goto bad2;
3107c478bd9Sstevel@tonic-gate 		protonum = is_des ? KCMD_OLD_PROTOCOL : KCMD_NEW_PROTOCOL;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	switch (protonum) {
3147c478bd9Sstevel@tonic-gate 	case KCMD_NEW_PROTOCOL:
3157c478bd9Sstevel@tonic-gate 		authopts |= AP_OPTS_USE_SUBKEY;
3167c478bd9Sstevel@tonic-gate 		kcmd_version = "KCMDV0.2";
3177c478bd9Sstevel@tonic-gate 		break;
3187c478bd9Sstevel@tonic-gate 	case KCMD_OLD_PROTOCOL:
3197c478bd9Sstevel@tonic-gate 		kcmd_version = "KCMDV0.1";
3207c478bd9Sstevel@tonic-gate 		break;
3217c478bd9Sstevel@tonic-gate 	default:
3227c478bd9Sstevel@tonic-gate 		status = -1;
3237c478bd9Sstevel@tonic-gate 		goto bad2;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Call the Kerberos library routine to obtain an authenticator,
3287c478bd9Sstevel@tonic-gate 	 * pass it over the socket to the server, and obtain mutual
3297c478bd9Sstevel@tonic-gate 	 * authentication.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	status = krb5_sendauth(bsd_context, &auth_context, (krb5_pointer) &s,
3327c478bd9Sstevel@tonic-gate 			kcmd_version, ret_cred->client, ret_cred->server,
3337c478bd9Sstevel@tonic-gate 			authopts, &cksumdat, ret_cred, 0, &error,
3347c478bd9Sstevel@tonic-gate 			&rep_ret, NULL);
3357c478bd9Sstevel@tonic-gate 	krb5_xfree(cksumdat.data);
3367c478bd9Sstevel@tonic-gate 	if (status) {
3377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Couldn't authenticate"
3387c478bd9Sstevel@tonic-gate 					    " to server: %s\n"),
3397c478bd9Sstevel@tonic-gate 			    error_message(status));
3407c478bd9Sstevel@tonic-gate 		if (error) {
3417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Server returned error"
3427c478bd9Sstevel@tonic-gate 						" code %d (%s)\n"),
3437c478bd9Sstevel@tonic-gate 				error->error,
3447c478bd9Sstevel@tonic-gate 				error_message(ERROR_TABLE_BASE_krb5 +
3457c478bd9Sstevel@tonic-gate 					    error->error));
3467c478bd9Sstevel@tonic-gate 			if (error->text.length)
3477c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3487c478bd9Sstevel@tonic-gate 					    gettext("Error text"
3497c478bd9Sstevel@tonic-gate 						    " sent from server: %s\n"),
3507c478bd9Sstevel@tonic-gate 					    error->text.data);
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 		if (error) {
3537c478bd9Sstevel@tonic-gate 			krb5_free_error(bsd_context, error);
3547c478bd9Sstevel@tonic-gate 			error = 0;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		goto bad2;
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	if (rep_ret && server_seqno) {
3597c478bd9Sstevel@tonic-gate 		*server_seqno = rep_ret->seq_number;
3607c478bd9Sstevel@tonic-gate 		krb5_free_ap_rep_enc_part(bsd_context, rep_ret);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	(void) write(s, remuser, strlen(remuser)+1);
3647c478bd9Sstevel@tonic-gate 	(void) write(s, cmd, strlen(cmd)+1);
3657c478bd9Sstevel@tonic-gate 	if (locuser)
3667c478bd9Sstevel@tonic-gate 		(void) write(s, locuser, strlen(locuser)+1);
3677c478bd9Sstevel@tonic-gate 	else
3687c478bd9Sstevel@tonic-gate 		(void) write(s, "", 1);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (options & OPTS_FORWARD_CREDS) {   /* Forward credentials */
3717c478bd9Sstevel@tonic-gate 		if (status = krb5_fwd_tgt_creds(bsd_context, auth_context,
3727c478bd9Sstevel@tonic-gate 					host_save,
3737c478bd9Sstevel@tonic-gate 					ret_cred->client, ret_cred->server,
3747c478bd9Sstevel@tonic-gate 					0, options & OPTS_FORWARDABLE_CREDS,
3757c478bd9Sstevel@tonic-gate 					&outbuf)) {
3767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3777c478bd9Sstevel@tonic-gate 				    gettext("kcmd: Error getting"
3787c478bd9Sstevel@tonic-gate 					    " forwarded creds\n"));
3797c478bd9Sstevel@tonic-gate 			goto bad2;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		/* Send forwarded credentials */
3827c478bd9Sstevel@tonic-gate 		if (status = krb5_write_message(bsd_context, (krb5_pointer)&s,
3837c478bd9Sstevel@tonic-gate 						&outbuf))
3847c478bd9Sstevel@tonic-gate 			goto bad2;
3857c478bd9Sstevel@tonic-gate 	} else { /* Dummy write to signal no forwarding */
3867c478bd9Sstevel@tonic-gate 		outbuf.length = 0;
3877c478bd9Sstevel@tonic-gate 		if (status = krb5_write_message(bsd_context,
3887c478bd9Sstevel@tonic-gate 						(krb5_pointer)&s, &outbuf))
3897c478bd9Sstevel@tonic-gate 			goto bad2;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if ((rc = read(s, &c, 1)) != 1) {
3937c478bd9Sstevel@tonic-gate 		if (rc == -1) {
3947c478bd9Sstevel@tonic-gate 			perror(*ahost);
3957c478bd9Sstevel@tonic-gate 		} else {
3967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("kcmd: bad connection "
3977c478bd9Sstevel@tonic-gate 					"with remote host\n"));
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 		status = -1;
4007c478bd9Sstevel@tonic-gate 		goto bad2;
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	if (c != 0) {
4037c478bd9Sstevel@tonic-gate 		while (read(s, &c, 1) == 1) {
4047c478bd9Sstevel@tonic-gate 			(void) write(2, &c, 1);
4057c478bd9Sstevel@tonic-gate 			if (c == '\n')
4067c478bd9Sstevel@tonic-gate 				break;
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 		status = -1;
4097c478bd9Sstevel@tonic-gate 		goto bad2;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
4127c478bd9Sstevel@tonic-gate 	*sock = s;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/* pass back credentials if wanted */
415*0b4fd3b1SSurya Prakki 	if (cred) (void) krb5_copy_creds(bsd_context, ret_cred, cred);
4167c478bd9Sstevel@tonic-gate 		krb5_free_creds(bsd_context, ret_cred);
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * Initialize *authconp to auth_context, so
4197c478bd9Sstevel@tonic-gate 	 * that the clients can make use of it
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	*authconp = auth_context;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	return (0);
4247c478bd9Sstevel@tonic-gate bad2:
4257c478bd9Sstevel@tonic-gate 	if (fd2p != NULL)
4267c478bd9Sstevel@tonic-gate 		(void) close(*fd2p);
4277c478bd9Sstevel@tonic-gate bad:
4287c478bd9Sstevel@tonic-gate 	if (s > 0)
4297c478bd9Sstevel@tonic-gate 		(void) close(s);
4307c478bd9Sstevel@tonic-gate 	if (get_cred)
4317c478bd9Sstevel@tonic-gate 		krb5_free_creds(bsd_context, get_cred);
4327c478bd9Sstevel@tonic-gate 	if (ret_cred)
4337c478bd9Sstevel@tonic-gate 		krb5_free_creds(bsd_context, ret_cred);
4347c478bd9Sstevel@tonic-gate 	if (host_save)
4357c478bd9Sstevel@tonic-gate 		free(host_save);
4367c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
4377c478bd9Sstevel@tonic-gate 	return (status);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * Strsave was a routine in the version 4 krb library: we put it here
4427c478bd9Sstevel@tonic-gate  * for compatablilty with version 5 krb library, since kcmd.o is linked
4437c478bd9Sstevel@tonic-gate  * into all programs.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate char *
strsave(char * sp)4477c478bd9Sstevel@tonic-gate strsave(char *sp)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	char *ret;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if ((ret = (char *)strdup(sp)) == NULL) {
4527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("no memory for saving args\n"));
4537c478bd9Sstevel@tonic-gate 		exit(1);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	return (ret);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate  * This routine is to initialize the desinbuf, desoutbuf and the session key
4617c478bd9Sstevel@tonic-gate  * structures to carry out desread()'s and deswrite()'s successfully
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@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)4647c478bd9Sstevel@tonic-gate init_encrypt(int enc, krb5_context ctxt, enum kcmd_proto protonum,
4657c478bd9Sstevel@tonic-gate 	krb5_data *inbuf, krb5_data *outbuf,
4667c478bd9Sstevel@tonic-gate 	int amclient, krb5_encrypt_block *block)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	krb5_error_code statuscode;
4697c478bd9Sstevel@tonic-gate 	size_t blocksize;
4707c478bd9Sstevel@tonic-gate 	int i;
4717c478bd9Sstevel@tonic-gate 	krb5_error_code ret;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	kcmd_context = ctxt;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (enc > 0) {
4767c478bd9Sstevel@tonic-gate 		desinbuf.data = inbuf->data;
4777c478bd9Sstevel@tonic-gate 		desoutbuf.data = outbuf->data + 4;
4787c478bd9Sstevel@tonic-gate 		desinbuf.length = inbuf->length;
4797c478bd9Sstevel@tonic-gate 		desoutbuf.length = outbuf->length + 4;
4807c478bd9Sstevel@tonic-gate 		encrypt_flag = B_TRUE;
4817c478bd9Sstevel@tonic-gate 	} else {
4827c478bd9Sstevel@tonic-gate 		encrypt_flag = B_FALSE;
4837c478bd9Sstevel@tonic-gate 		return;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	skey = block->key;
4877c478bd9Sstevel@tonic-gate 	final_enctype = skey->enctype;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	enc_keyusage_i[0] = KCMD_KEYUSAGE;
4907c478bd9Sstevel@tonic-gate 	enc_keyusage_i[1] = KCMD_KEYUSAGE;
4917c478bd9Sstevel@tonic-gate 	enc_keyusage_o[0] = KCMD_KEYUSAGE;
4927c478bd9Sstevel@tonic-gate 	enc_keyusage_o[1] = KCMD_KEYUSAGE;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (protonum == KCMD_OLD_PROTOCOL) {
4957c478bd9Sstevel@tonic-gate 		use_ivecs = B_FALSE;
4967c478bd9Sstevel@tonic-gate 		return;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	use_ivecs = B_TRUE;
5007c478bd9Sstevel@tonic-gate 	switch (skey->enctype) {
5017c478bd9Sstevel@tonic-gate 	/*
5027c478bd9Sstevel@tonic-gate 	 * For the DES-based enctypes and the 3DES enctype we
5037c478bd9Sstevel@tonic-gate 	 * want to use a non-zero  IV because that's what we did.
5047c478bd9Sstevel@tonic-gate 	 * In the future we use different keyusage for each
5057c478bd9Sstevel@tonic-gate 	 * channel and direction and a fresh cipher state.
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_CRC:
5087c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_MD4:
5097c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_MD5:
5107c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES3_CBC_SHA1:
5117c478bd9Sstevel@tonic-gate 		statuscode = krb5_c_block_size(kcmd_context, final_enctype,
5127c478bd9Sstevel@tonic-gate 				&blocksize);
5137c478bd9Sstevel@tonic-gate 		if (statuscode) {
5147c478bd9Sstevel@tonic-gate 			/* XXX what do I do? */
5157c478bd9Sstevel@tonic-gate 			abort();
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		encivec_i[0].length = encivec_i[1].length =
5197c478bd9Sstevel@tonic-gate 		encivec_o[0].length = encivec_o[1].length = blocksize;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		if ((encivec_i[0].data = malloc(encivec_i[0].length * 4))
5227c478bd9Sstevel@tonic-gate 			== NULL) {
5237c478bd9Sstevel@tonic-gate 			/* XXX what do I do? */
5247c478bd9Sstevel@tonic-gate 			abort();
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		encivec_i[1].data = encivec_i[0].data + encivec_i[0].length;
5277c478bd9Sstevel@tonic-gate 		encivec_o[0].data = encivec_i[1].data + encivec_i[0].length;
5287c478bd9Sstevel@tonic-gate 		encivec_o[1].data = encivec_o[0].data + encivec_i[0].length;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		/* is there a better way to initialize this? */
5317c478bd9Sstevel@tonic-gate 		(void) memset(encivec_i[0].data, amclient, blocksize);
5327c478bd9Sstevel@tonic-gate 		(void) memset(encivec_o[0].data, 1 - amclient, blocksize);
5337c478bd9Sstevel@tonic-gate 		(void) memset(encivec_i[1].data, 2 | amclient, blocksize);
5347c478bd9Sstevel@tonic-gate 		(void) memset(encivec_o[1].data, 2 | (1 - amclient), blocksize);
5357c478bd9Sstevel@tonic-gate 		break;
5367c478bd9Sstevel@tonic-gate 	default:
5377c478bd9Sstevel@tonic-gate 		if (amclient) {
5387c478bd9Sstevel@tonic-gate 			enc_keyusage_i[0] = 1028;
5397c478bd9Sstevel@tonic-gate 			enc_keyusage_i[1] = 1030;
5407c478bd9Sstevel@tonic-gate 			enc_keyusage_o[0] = 1032;
5417c478bd9Sstevel@tonic-gate 			enc_keyusage_o[1] = 1034;
5427c478bd9Sstevel@tonic-gate 		} else { /* amclient */
5437c478bd9Sstevel@tonic-gate 			enc_keyusage_i[0] = 1032;
5447c478bd9Sstevel@tonic-gate 			enc_keyusage_i[1] = 1034;
5457c478bd9Sstevel@tonic-gate 			enc_keyusage_o[0] = 1028;
5467c478bd9Sstevel@tonic-gate 			enc_keyusage_o[1] = 1030;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		for (i = 0; i < 2; i++) {
5497c478bd9Sstevel@tonic-gate 			ret = krb5_c_init_state(ctxt,
5507c478bd9Sstevel@tonic-gate 				skey, enc_keyusage_i[i],
5517c478bd9Sstevel@tonic-gate 				&encivec_i[i]);
5527c478bd9Sstevel@tonic-gate 			if (ret)
5537c478bd9Sstevel@tonic-gate 				goto fail;
5547c478bd9Sstevel@tonic-gate 			ret = krb5_c_init_state(ctxt,
5557c478bd9Sstevel@tonic-gate 				skey, enc_keyusage_o[i],
5567c478bd9Sstevel@tonic-gate 				&encivec_o[i]);
5577c478bd9Sstevel@tonic-gate 			if (ret)
5587c478bd9Sstevel@tonic-gate 				goto fail;
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 		break;
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 	return;
5637c478bd9Sstevel@tonic-gate fail:
5647c478bd9Sstevel@tonic-gate 	abort();
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate int
desread(int fd,char * buf,int len,int secondary)5687c478bd9Sstevel@tonic-gate desread(int fd, char *buf, int len, int secondary)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	int nreturned = 0;
5717c478bd9Sstevel@tonic-gate 	long net_len, rd_len;
5727c478bd9Sstevel@tonic-gate 	int cc;
5737c478bd9Sstevel@tonic-gate 	size_t ret = 0;
5747c478bd9Sstevel@tonic-gate 	unsigned char len_buf[4];
5757c478bd9Sstevel@tonic-gate 	krb5_enc_data inputd;
5767c478bd9Sstevel@tonic-gate 	krb5_data outputd;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (!encrypt_flag)
5797c478bd9Sstevel@tonic-gate 		return (read(fd, buf, len));
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * If there is stored data from a previous read,
5837c478bd9Sstevel@tonic-gate 	 * put it into the output buffer and return it now.
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 	if (nstored >= len) {
5867c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, store_ptr, len);
5877c478bd9Sstevel@tonic-gate 		store_ptr += len;
5887c478bd9Sstevel@tonic-gate 		nstored -= len;
5897c478bd9Sstevel@tonic-gate 		return (len);
5907c478bd9Sstevel@tonic-gate 	} else if (nstored) {
5917c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, store_ptr, nstored);
5927c478bd9Sstevel@tonic-gate 		nreturned += nstored;
5937c478bd9Sstevel@tonic-gate 		buf += nstored;
5947c478bd9Sstevel@tonic-gate 		len -= nstored;
5957c478bd9Sstevel@tonic-gate 		nstored = 0;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if ((cc = krb5_net_read(kcmd_context, fd, (char *)len_buf, 4)) != 4) {
5997c478bd9Sstevel@tonic-gate 		if ((cc < 0) && ((errno == EWOULDBLOCK) || (errno == EAGAIN)))
6007c478bd9Sstevel@tonic-gate 			return (cc);
6017c478bd9Sstevel@tonic-gate 		/* XXX can't read enough, pipe must have closed */
6027c478bd9Sstevel@tonic-gate 		return (0);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	rd_len = ((len_buf[0] << 24) | (len_buf[1] << 16) |
6057c478bd9Sstevel@tonic-gate 		    (len_buf[2] << 8) | len_buf[3]);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (krb5_c_encrypt_length(kcmd_context, final_enctype,
6087c478bd9Sstevel@tonic-gate 				use_ivecs ? (size_t)rd_len + 4 : (size_t)rd_len,
6097c478bd9Sstevel@tonic-gate 				&ret))
6107c478bd9Sstevel@tonic-gate 		net_len = ((size_t)-1);
6117c478bd9Sstevel@tonic-gate 	else
6127c478bd9Sstevel@tonic-gate 		net_len = ret;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if ((net_len <= 0) || (net_len > desinbuf.length)) {
6157c478bd9Sstevel@tonic-gate 		/*
6167c478bd9Sstevel@tonic-gate 		 * preposterous length; assume out-of-sync; only recourse
6177c478bd9Sstevel@tonic-gate 		 * is to close connection, so return 0
6187c478bd9Sstevel@tonic-gate 		 */
6197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Read size problem.\n"));
6207c478bd9Sstevel@tonic-gate 		return (0);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if ((cc = krb5_net_read(kcmd_context, fd, desinbuf.data, net_len))
6247c478bd9Sstevel@tonic-gate 	    != net_len) {
6257c478bd9Sstevel@tonic-gate 		/* pipe must have closed, return 0 */
6267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6277c478bd9Sstevel@tonic-gate 			    gettext("Read error: length received %d "
6287c478bd9Sstevel@tonic-gate 				    "!= expected %d.\n"),
6297c478bd9Sstevel@tonic-gate 			    cc, net_len);
6307c478bd9Sstevel@tonic-gate 		return (0);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * Decrypt information
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 	inputd.enctype = ENCTYPE_UNKNOWN;
6377c478bd9Sstevel@tonic-gate 	inputd.ciphertext.length = net_len;
6387c478bd9Sstevel@tonic-gate 	inputd.ciphertext.data = (krb5_pointer)desinbuf.data;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	outputd.length = sizeof (storage);
6417c478bd9Sstevel@tonic-gate 	outputd.data = (krb5_pointer)storage;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * data is decrypted into the "storage" buffer, which
6457c478bd9Sstevel@tonic-gate 	 * had better be large enough!
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	cc = krb5_c_decrypt(kcmd_context, skey,
6487c478bd9Sstevel@tonic-gate 				enc_keyusage_i[secondary],
6497c478bd9Sstevel@tonic-gate 				use_ivecs ? encivec_i + secondary : 0,
6507c478bd9Sstevel@tonic-gate 				&inputd, &outputd);
6517c478bd9Sstevel@tonic-gate 	if (cc) {
6527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Cannot decrypt data "
6537c478bd9Sstevel@tonic-gate 			"from network\n"));
6547c478bd9Sstevel@tonic-gate 		return (0);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	store_ptr = storage;
6587c478bd9Sstevel@tonic-gate 	nstored = rd_len;
6597c478bd9Sstevel@tonic-gate 	if (use_ivecs == B_TRUE) {
6607c478bd9Sstevel@tonic-gate 		int rd_len2;
6617c478bd9Sstevel@tonic-gate 		rd_len2 = storage[0] & 0xff;
6627c478bd9Sstevel@tonic-gate 		rd_len2 <<= 8; rd_len2 |= storage[1] & 0xff;
6637c478bd9Sstevel@tonic-gate 		rd_len2 <<= 8; rd_len2 |= storage[2] & 0xff;
6647c478bd9Sstevel@tonic-gate 		rd_len2 <<= 8; rd_len2 |= storage[3] & 0xff;
6657c478bd9Sstevel@tonic-gate 		if (rd_len2 != rd_len) {
6667c478bd9Sstevel@tonic-gate 			/* cleartext length trashed? */
6677c478bd9Sstevel@tonic-gate 			errno = EIO;
6687c478bd9Sstevel@tonic-gate 			return (-1);
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		store_ptr += 4;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	/*
6737c478bd9Sstevel@tonic-gate 	 * Copy only as much data as the input buffer will allow.
6747c478bd9Sstevel@tonic-gate 	 * The rest is kept in the 'storage' pointer for the next
6757c478bd9Sstevel@tonic-gate 	 * read.
6767c478bd9Sstevel@tonic-gate 	 */
6777c478bd9Sstevel@tonic-gate 	if (nstored > len) {
6787c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, store_ptr, len);
6797c478bd9Sstevel@tonic-gate 		nreturned += len;
6807c478bd9Sstevel@tonic-gate 		store_ptr += len;
6817c478bd9Sstevel@tonic-gate 		nstored -= len;
6827c478bd9Sstevel@tonic-gate 	} else {
6837c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, store_ptr, nstored);
6847c478bd9Sstevel@tonic-gate 		nreturned += nstored;
6857c478bd9Sstevel@tonic-gate 		nstored = 0;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	return (nreturned);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate int
deswrite(int fd,char * buf,int len,int secondary)6917c478bd9Sstevel@tonic-gate deswrite(int fd, char *buf, int len, int secondary)
6927c478bd9Sstevel@tonic-gate {
6931509e105Spk193450 	int bytes_written;
6941509e105Spk193450 	int r;
6951509e105Spk193450 	int outlen;
6961509e105Spk193450 	char *p;
6971509e105Spk193450 	if (!encrypt_flag)
6981509e105Spk193450 		return (write(fd, buf, len));
6991509e105Spk193450 
7001509e105Spk193450 	bytes_written = 0;
7011509e105Spk193450 	while (len > 0) {
7021509e105Spk193450 		p = buf + bytes_written;
7031509e105Spk193450 		if (len > KCMD8_BUFSIZ)
7041509e105Spk193450 			outlen = KCMD8_BUFSIZ;
7051509e105Spk193450 		else
7061509e105Spk193450 			outlen = len;
7071509e105Spk193450 		r = deswrite_compat(fd, p, outlen, secondary);
7081509e105Spk193450 		if (r == -1)
7091509e105Spk193450 			return (r);
7101509e105Spk193450 		bytes_written += r;
7111509e105Spk193450 		len -= r;
7121509e105Spk193450 	}
7131509e105Spk193450 	return (bytes_written);
7141509e105Spk193450 }
7151509e105Spk193450 static int
deswrite_compat(int fd,char * buf,int len,int secondary)7161509e105Spk193450 deswrite_compat(int fd, char *buf, int len, int secondary)
7171509e105Spk193450 {
7187c478bd9Sstevel@tonic-gate 	int cc;
7197c478bd9Sstevel@tonic-gate 	size_t ret = 0;
7207c478bd9Sstevel@tonic-gate 	krb5_data inputd;
7217c478bd9Sstevel@tonic-gate 	krb5_enc_data outputd;
7227c478bd9Sstevel@tonic-gate 	char tmpbuf[KCMD_BUFSIZ + 8];
7237c478bd9Sstevel@tonic-gate 	char encrbuf[KCMD_BUFSIZ + 8];
7247c478bd9Sstevel@tonic-gate 	unsigned char *len_buf = (unsigned char *)tmpbuf;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (use_ivecs == B_TRUE) {
7277c478bd9Sstevel@tonic-gate 		unsigned char *lenbuf2 = (unsigned char *)tmpbuf;
7287c478bd9Sstevel@tonic-gate 		if (len + 4 > sizeof (tmpbuf))
7297c478bd9Sstevel@tonic-gate 			abort();
7307c478bd9Sstevel@tonic-gate 		lenbuf2[0] = (len & 0xff000000) >> 24;
7317c478bd9Sstevel@tonic-gate 		lenbuf2[1] = (len & 0xff0000) >> 16;
7327c478bd9Sstevel@tonic-gate 		lenbuf2[2] = (len & 0xff00) >> 8;
7337c478bd9Sstevel@tonic-gate 		lenbuf2[3] = (len & 0xff);
7347c478bd9Sstevel@tonic-gate 		(void) memcpy(tmpbuf + 4, buf, len);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		inputd.data = (krb5_pointer)tmpbuf;
7377c478bd9Sstevel@tonic-gate 		inputd.length = len + 4;
7387c478bd9Sstevel@tonic-gate 	} else {
7397c478bd9Sstevel@tonic-gate 		inputd.data = (krb5_pointer)buf;
7407c478bd9Sstevel@tonic-gate 		inputd.length = len;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	desoutbuf.data = encrbuf;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	if (krb5_c_encrypt_length(kcmd_context, final_enctype,
7467c478bd9Sstevel@tonic-gate 			use_ivecs ? (size_t)len + 4 : (size_t)len, &ret)) {
7477c478bd9Sstevel@tonic-gate 		desoutbuf.length = ((size_t)-1);
7487c478bd9Sstevel@tonic-gate 		goto err;
7497c478bd9Sstevel@tonic-gate 	} else {
7507c478bd9Sstevel@tonic-gate 		desoutbuf.length = ret;
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (desoutbuf.length > MAXSIZE) {
7547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Write size problem.\n"));
7557c478bd9Sstevel@tonic-gate 		return (-1);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/*
7597c478bd9Sstevel@tonic-gate 	 * Encrypt information
7607c478bd9Sstevel@tonic-gate 	 */
7617c478bd9Sstevel@tonic-gate 	outputd.ciphertext.length = desoutbuf.length;
7627c478bd9Sstevel@tonic-gate 	outputd.ciphertext.data = (krb5_pointer)desoutbuf.data;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	cc = krb5_c_encrypt(kcmd_context, skey,
7657c478bd9Sstevel@tonic-gate 			enc_keyusage_o[secondary],
7667c478bd9Sstevel@tonic-gate 			use_ivecs ? encivec_o + secondary : 0,
7677c478bd9Sstevel@tonic-gate 			&inputd, &outputd);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if (cc) {
7707c478bd9Sstevel@tonic-gate err:
7717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Write encrypt problem.\n"));
7727c478bd9Sstevel@tonic-gate 		return (-1);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	len_buf[0] = (len & 0xff000000) >> 24;
7767c478bd9Sstevel@tonic-gate 	len_buf[1] = (len & 0xff0000) >> 16;
7777c478bd9Sstevel@tonic-gate 	len_buf[2] = (len & 0xff00) >> 8;
7787c478bd9Sstevel@tonic-gate 	len_buf[3] = (len & 0xff);
7797c478bd9Sstevel@tonic-gate 	(void) write(fd, len_buf, 4);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if (write(fd, desoutbuf.data, desoutbuf.length) != desoutbuf.length) {
7827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Could not write "
7837c478bd9Sstevel@tonic-gate 			"out all data.\n"));
7847c478bd9Sstevel@tonic-gate 		return (-1);
7857c478bd9Sstevel@tonic-gate 	} else {
7867c478bd9Sstevel@tonic-gate 		return (len);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate }
789