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