1*ebfedea0SLionel Sambuc /* $NetBSD: sendauth.c,v 1.1.1.1 2011/04/13 18:15:38 elric Exp $ */ 2*ebfedea0SLionel Sambuc 3*ebfedea0SLionel Sambuc /* 4*ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan 5*ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden). 6*ebfedea0SLionel Sambuc * All rights reserved. 7*ebfedea0SLionel Sambuc * 8*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 9*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 10*ebfedea0SLionel Sambuc * are met: 11*ebfedea0SLionel Sambuc * 12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 14*ebfedea0SLionel Sambuc * 15*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 16*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 17*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution. 18*ebfedea0SLionel Sambuc * 19*ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors 20*ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software 21*ebfedea0SLionel Sambuc * without specific prior written permission. 22*ebfedea0SLionel Sambuc * 23*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33*ebfedea0SLionel Sambuc * SUCH DAMAGE. 34*ebfedea0SLionel Sambuc */ 35*ebfedea0SLionel Sambuc 36*ebfedea0SLionel Sambuc #include "krb5_locl.h" 37*ebfedea0SLionel Sambuc 38*ebfedea0SLionel Sambuc /* 39*ebfedea0SLionel Sambuc * The format seems to be: 40*ebfedea0SLionel Sambuc * client -> server 41*ebfedea0SLionel Sambuc * 42*ebfedea0SLionel Sambuc * 4 bytes - length 43*ebfedea0SLionel Sambuc * KRB5_SENDAUTH_V1.0 (including zero) 44*ebfedea0SLionel Sambuc * 4 bytes - length 45*ebfedea0SLionel Sambuc * protocol string (with terminating zero) 46*ebfedea0SLionel Sambuc * 47*ebfedea0SLionel Sambuc * server -> client 48*ebfedea0SLionel Sambuc * 1 byte - (0 = OK, else some kind of error) 49*ebfedea0SLionel Sambuc * 50*ebfedea0SLionel Sambuc * client -> server 51*ebfedea0SLionel Sambuc * 4 bytes - length 52*ebfedea0SLionel Sambuc * AP-REQ 53*ebfedea0SLionel Sambuc * 54*ebfedea0SLionel Sambuc * server -> client 55*ebfedea0SLionel Sambuc * 4 bytes - length (0 = OK, else length of error) 56*ebfedea0SLionel Sambuc * (error) 57*ebfedea0SLionel Sambuc * 58*ebfedea0SLionel Sambuc * if(mutual) { 59*ebfedea0SLionel Sambuc * server -> client 60*ebfedea0SLionel Sambuc * 4 bytes - length 61*ebfedea0SLionel Sambuc * AP-REP 62*ebfedea0SLionel Sambuc * } 63*ebfedea0SLionel Sambuc */ 64*ebfedea0SLionel Sambuc 65*ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 66*ebfedea0SLionel Sambuc krb5_sendauth(krb5_context context, 67*ebfedea0SLionel Sambuc krb5_auth_context *auth_context, 68*ebfedea0SLionel Sambuc krb5_pointer p_fd, 69*ebfedea0SLionel Sambuc const char *appl_version, 70*ebfedea0SLionel Sambuc krb5_principal client, 71*ebfedea0SLionel Sambuc krb5_principal server, 72*ebfedea0SLionel Sambuc krb5_flags ap_req_options, 73*ebfedea0SLionel Sambuc krb5_data *in_data, 74*ebfedea0SLionel Sambuc krb5_creds *in_creds, 75*ebfedea0SLionel Sambuc krb5_ccache ccache, 76*ebfedea0SLionel Sambuc krb5_error **ret_error, 77*ebfedea0SLionel Sambuc krb5_ap_rep_enc_part **rep_result, 78*ebfedea0SLionel Sambuc krb5_creds **out_creds) 79*ebfedea0SLionel Sambuc { 80*ebfedea0SLionel Sambuc krb5_error_code ret; 81*ebfedea0SLionel Sambuc uint32_t len, net_len; 82*ebfedea0SLionel Sambuc const char *version = KRB5_SENDAUTH_VERSION; 83*ebfedea0SLionel Sambuc u_char repl; 84*ebfedea0SLionel Sambuc krb5_data ap_req, error_data; 85*ebfedea0SLionel Sambuc krb5_creds this_cred; 86*ebfedea0SLionel Sambuc krb5_principal this_client = NULL; 87*ebfedea0SLionel Sambuc krb5_creds *creds; 88*ebfedea0SLionel Sambuc ssize_t sret; 89*ebfedea0SLionel Sambuc krb5_boolean my_ccache = FALSE; 90*ebfedea0SLionel Sambuc 91*ebfedea0SLionel Sambuc len = strlen(version) + 1; 92*ebfedea0SLionel Sambuc net_len = htonl(len); 93*ebfedea0SLionel Sambuc if (krb5_net_write (context, p_fd, &net_len, 4) != 4 94*ebfedea0SLionel Sambuc || krb5_net_write (context, p_fd, version, len) != len) { 95*ebfedea0SLionel Sambuc ret = errno; 96*ebfedea0SLionel Sambuc krb5_set_error_message (context, ret, "write: %s", strerror(ret)); 97*ebfedea0SLionel Sambuc return ret; 98*ebfedea0SLionel Sambuc } 99*ebfedea0SLionel Sambuc 100*ebfedea0SLionel Sambuc len = strlen(appl_version) + 1; 101*ebfedea0SLionel Sambuc net_len = htonl(len); 102*ebfedea0SLionel Sambuc if (krb5_net_write (context, p_fd, &net_len, 4) != 4 103*ebfedea0SLionel Sambuc || krb5_net_write (context, p_fd, appl_version, len) != len) { 104*ebfedea0SLionel Sambuc ret = errno; 105*ebfedea0SLionel Sambuc krb5_set_error_message (context, ret, "write: %s", strerror(ret)); 106*ebfedea0SLionel Sambuc return ret; 107*ebfedea0SLionel Sambuc } 108*ebfedea0SLionel Sambuc 109*ebfedea0SLionel Sambuc sret = krb5_net_read (context, p_fd, &repl, sizeof(repl)); 110*ebfedea0SLionel Sambuc if (sret < 0) { 111*ebfedea0SLionel Sambuc ret = errno; 112*ebfedea0SLionel Sambuc krb5_set_error_message (context, ret, "read: %s", strerror(ret)); 113*ebfedea0SLionel Sambuc return ret; 114*ebfedea0SLionel Sambuc } else if (sret != sizeof(repl)) { 115*ebfedea0SLionel Sambuc krb5_clear_error_message (context); 116*ebfedea0SLionel Sambuc return KRB5_SENDAUTH_BADRESPONSE; 117*ebfedea0SLionel Sambuc } 118*ebfedea0SLionel Sambuc 119*ebfedea0SLionel Sambuc if (repl != 0) { 120*ebfedea0SLionel Sambuc krb5_clear_error_message (context); 121*ebfedea0SLionel Sambuc return KRB5_SENDAUTH_REJECTED; 122*ebfedea0SLionel Sambuc } 123*ebfedea0SLionel Sambuc 124*ebfedea0SLionel Sambuc if (in_creds == NULL) { 125*ebfedea0SLionel Sambuc if (ccache == NULL) { 126*ebfedea0SLionel Sambuc ret = krb5_cc_default (context, &ccache); 127*ebfedea0SLionel Sambuc if (ret) 128*ebfedea0SLionel Sambuc return ret; 129*ebfedea0SLionel Sambuc my_ccache = TRUE; 130*ebfedea0SLionel Sambuc } 131*ebfedea0SLionel Sambuc 132*ebfedea0SLionel Sambuc if (client == NULL) { 133*ebfedea0SLionel Sambuc ret = krb5_cc_get_principal (context, ccache, &this_client); 134*ebfedea0SLionel Sambuc if (ret) { 135*ebfedea0SLionel Sambuc if(my_ccache) 136*ebfedea0SLionel Sambuc krb5_cc_close(context, ccache); 137*ebfedea0SLionel Sambuc return ret; 138*ebfedea0SLionel Sambuc } 139*ebfedea0SLionel Sambuc client = this_client; 140*ebfedea0SLionel Sambuc } 141*ebfedea0SLionel Sambuc memset(&this_cred, 0, sizeof(this_cred)); 142*ebfedea0SLionel Sambuc this_cred.client = client; 143*ebfedea0SLionel Sambuc this_cred.server = server; 144*ebfedea0SLionel Sambuc this_cred.times.endtime = 0; 145*ebfedea0SLionel Sambuc this_cred.ticket.length = 0; 146*ebfedea0SLionel Sambuc in_creds = &this_cred; 147*ebfedea0SLionel Sambuc } 148*ebfedea0SLionel Sambuc if (in_creds->ticket.length == 0) { 149*ebfedea0SLionel Sambuc ret = krb5_get_credentials (context, 0, ccache, in_creds, &creds); 150*ebfedea0SLionel Sambuc if (ret) { 151*ebfedea0SLionel Sambuc if(my_ccache) 152*ebfedea0SLionel Sambuc krb5_cc_close(context, ccache); 153*ebfedea0SLionel Sambuc return ret; 154*ebfedea0SLionel Sambuc } 155*ebfedea0SLionel Sambuc } else { 156*ebfedea0SLionel Sambuc creds = in_creds; 157*ebfedea0SLionel Sambuc } 158*ebfedea0SLionel Sambuc if(my_ccache) 159*ebfedea0SLionel Sambuc krb5_cc_close(context, ccache); 160*ebfedea0SLionel Sambuc ret = krb5_mk_req_extended (context, 161*ebfedea0SLionel Sambuc auth_context, 162*ebfedea0SLionel Sambuc ap_req_options, 163*ebfedea0SLionel Sambuc in_data, 164*ebfedea0SLionel Sambuc creds, 165*ebfedea0SLionel Sambuc &ap_req); 166*ebfedea0SLionel Sambuc 167*ebfedea0SLionel Sambuc if (out_creds) 168*ebfedea0SLionel Sambuc *out_creds = creds; 169*ebfedea0SLionel Sambuc else 170*ebfedea0SLionel Sambuc krb5_free_creds(context, creds); 171*ebfedea0SLionel Sambuc if(this_client) 172*ebfedea0SLionel Sambuc krb5_free_principal(context, this_client); 173*ebfedea0SLionel Sambuc 174*ebfedea0SLionel Sambuc if (ret) 175*ebfedea0SLionel Sambuc return ret; 176*ebfedea0SLionel Sambuc 177*ebfedea0SLionel Sambuc ret = krb5_write_message (context, 178*ebfedea0SLionel Sambuc p_fd, 179*ebfedea0SLionel Sambuc &ap_req); 180*ebfedea0SLionel Sambuc if (ret) 181*ebfedea0SLionel Sambuc return ret; 182*ebfedea0SLionel Sambuc 183*ebfedea0SLionel Sambuc krb5_data_free (&ap_req); 184*ebfedea0SLionel Sambuc 185*ebfedea0SLionel Sambuc ret = krb5_read_message (context, p_fd, &error_data); 186*ebfedea0SLionel Sambuc if (ret) 187*ebfedea0SLionel Sambuc return ret; 188*ebfedea0SLionel Sambuc 189*ebfedea0SLionel Sambuc if (error_data.length != 0) { 190*ebfedea0SLionel Sambuc KRB_ERROR error; 191*ebfedea0SLionel Sambuc 192*ebfedea0SLionel Sambuc ret = krb5_rd_error (context, &error_data, &error); 193*ebfedea0SLionel Sambuc krb5_data_free (&error_data); 194*ebfedea0SLionel Sambuc if (ret == 0) { 195*ebfedea0SLionel Sambuc ret = krb5_error_from_rd_error(context, &error, NULL); 196*ebfedea0SLionel Sambuc if (ret_error != NULL) { 197*ebfedea0SLionel Sambuc *ret_error = malloc (sizeof(krb5_error)); 198*ebfedea0SLionel Sambuc if (*ret_error == NULL) { 199*ebfedea0SLionel Sambuc krb5_free_error_contents (context, &error); 200*ebfedea0SLionel Sambuc } else { 201*ebfedea0SLionel Sambuc **ret_error = error; 202*ebfedea0SLionel Sambuc } 203*ebfedea0SLionel Sambuc } else { 204*ebfedea0SLionel Sambuc krb5_free_error_contents (context, &error); 205*ebfedea0SLionel Sambuc } 206*ebfedea0SLionel Sambuc return ret; 207*ebfedea0SLionel Sambuc } else { 208*ebfedea0SLionel Sambuc krb5_clear_error_message(context); 209*ebfedea0SLionel Sambuc return ret; 210*ebfedea0SLionel Sambuc } 211*ebfedea0SLionel Sambuc } else 212*ebfedea0SLionel Sambuc krb5_data_free (&error_data); 213*ebfedea0SLionel Sambuc 214*ebfedea0SLionel Sambuc if (ap_req_options & AP_OPTS_MUTUAL_REQUIRED) { 215*ebfedea0SLionel Sambuc krb5_data ap_rep; 216*ebfedea0SLionel Sambuc krb5_ap_rep_enc_part *ignore; 217*ebfedea0SLionel Sambuc 218*ebfedea0SLionel Sambuc krb5_data_zero (&ap_rep); 219*ebfedea0SLionel Sambuc ret = krb5_read_message (context, 220*ebfedea0SLionel Sambuc p_fd, 221*ebfedea0SLionel Sambuc &ap_rep); 222*ebfedea0SLionel Sambuc if (ret) 223*ebfedea0SLionel Sambuc return ret; 224*ebfedea0SLionel Sambuc 225*ebfedea0SLionel Sambuc ret = krb5_rd_rep (context, *auth_context, &ap_rep, 226*ebfedea0SLionel Sambuc rep_result ? rep_result : &ignore); 227*ebfedea0SLionel Sambuc krb5_data_free (&ap_rep); 228*ebfedea0SLionel Sambuc if (ret) 229*ebfedea0SLionel Sambuc return ret; 230*ebfedea0SLionel Sambuc if (rep_result == NULL) 231*ebfedea0SLionel Sambuc krb5_free_ap_rep_enc_part (context, ignore); 232*ebfedea0SLionel Sambuc } 233*ebfedea0SLionel Sambuc return 0; 234*ebfedea0SLionel Sambuc } 235