1 /* 2 * Copyright (c) 1997 - 2002 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "krb5_locl.h" 35 36 RCSID("$Id: verify_init.c 15555 2005-07-06 00:48:16Z lha $"); 37 38 void KRB5_LIB_FUNCTION 39 krb5_verify_init_creds_opt_init(krb5_verify_init_creds_opt *options) 40 { 41 memset (options, 0, sizeof(*options)); 42 } 43 44 void KRB5_LIB_FUNCTION 45 krb5_verify_init_creds_opt_set_ap_req_nofail(krb5_verify_init_creds_opt *options, 46 int ap_req_nofail) 47 { 48 options->flags |= KRB5_VERIFY_INIT_CREDS_OPT_AP_REQ_NOFAIL; 49 options->ap_req_nofail = ap_req_nofail; 50 } 51 52 /* 53 * 54 */ 55 56 static krb5_boolean 57 fail_verify_is_ok (krb5_context context, 58 krb5_verify_init_creds_opt *options) 59 { 60 if ((options->flags & KRB5_VERIFY_INIT_CREDS_OPT_AP_REQ_NOFAIL 61 && options->ap_req_nofail != 0) 62 || krb5_config_get_bool (context, 63 NULL, 64 "libdefaults", 65 "verify_ap_req_nofail", 66 NULL)) 67 return FALSE; 68 else 69 return TRUE; 70 } 71 72 krb5_error_code KRB5_LIB_FUNCTION 73 krb5_verify_init_creds(krb5_context context, 74 krb5_creds *creds, 75 krb5_principal ap_req_server, 76 krb5_keytab ap_req_keytab, 77 krb5_ccache *ccache, 78 krb5_verify_init_creds_opt *options) 79 { 80 krb5_error_code ret; 81 krb5_data req; 82 krb5_ccache local_ccache = NULL; 83 krb5_creds *new_creds = NULL; 84 krb5_auth_context auth_context = NULL; 85 krb5_principal server = NULL; 86 krb5_keytab keytab = NULL; 87 88 krb5_data_zero (&req); 89 90 if (ap_req_server == NULL) { 91 char local_hostname[MAXHOSTNAMELEN]; 92 93 if (gethostname (local_hostname, sizeof(local_hostname)) < 0) { 94 ret = errno; 95 krb5_set_error_string (context, "gethostname: %s", 96 strerror(ret)); 97 return ret; 98 } 99 100 ret = krb5_sname_to_principal (context, 101 local_hostname, 102 "host", 103 KRB5_NT_SRV_HST, 104 &server); 105 if (ret) 106 goto cleanup; 107 } else 108 server = ap_req_server; 109 110 if (ap_req_keytab == NULL) { 111 ret = krb5_kt_default (context, &keytab); 112 if (ret) 113 goto cleanup; 114 } else 115 keytab = ap_req_keytab; 116 117 if (ccache && *ccache) 118 local_ccache = *ccache; 119 else { 120 ret = krb5_cc_gen_new (context, &krb5_mcc_ops, &local_ccache); 121 if (ret) 122 goto cleanup; 123 ret = krb5_cc_initialize (context, 124 local_ccache, 125 creds->client); 126 if (ret) 127 goto cleanup; 128 ret = krb5_cc_store_cred (context, 129 local_ccache, 130 creds); 131 if (ret) 132 goto cleanup; 133 } 134 135 if (!krb5_principal_compare (context, server, creds->server)) { 136 krb5_creds match_cred; 137 138 memset (&match_cred, 0, sizeof(match_cred)); 139 140 match_cred.client = creds->client; 141 match_cred.server = server; 142 143 ret = krb5_get_credentials (context, 144 0, 145 local_ccache, 146 &match_cred, 147 &new_creds); 148 if (ret) { 149 if (fail_verify_is_ok (context, options)) 150 ret = 0; 151 goto cleanup; 152 } 153 creds = new_creds; 154 } 155 156 ret = krb5_mk_req_extended (context, 157 &auth_context, 158 0, 159 NULL, 160 creds, 161 &req); 162 163 krb5_auth_con_free (context, auth_context); 164 auth_context = NULL; 165 166 if (ret) 167 goto cleanup; 168 169 ret = krb5_rd_req (context, 170 &auth_context, 171 &req, 172 server, 173 keytab, 174 0, 175 NULL); 176 177 if (ret == KRB5_KT_NOTFOUND && fail_verify_is_ok (context, options)) 178 ret = 0; 179 cleanup: 180 if (auth_context) 181 krb5_auth_con_free (context, auth_context); 182 krb5_data_free (&req); 183 if (new_creds != NULL) 184 krb5_free_creds (context, new_creds); 185 if (ap_req_server == NULL && server) 186 krb5_free_principal (context, server); 187 if (ap_req_keytab == NULL && keytab) 188 krb5_kt_close (context, keytab); 189 if (local_ccache != NULL 190 && 191 (ccache == NULL 192 || (ret != 0 && *ccache == NULL))) 193 krb5_cc_destroy (context, local_ccache); 194 195 if (ret == 0 && ccache != NULL && *ccache == NULL) 196 *ccache = local_ccache; 197 198 return ret; 199 } 200