1/* 2 * Copyright (c) 1997-2004 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 36static krb5_error_code 37verify_common (krb5_context context, 38 krb5_principal principal, 39 krb5_principal server_principal, 40 krb5_ccache ccache, 41 krb5_keytab keytab, 42 krb5_boolean secure, 43 const char *service, 44 krb5_creds *cred) 45{ 46 krb5_error_code ret; 47 krb5_verify_init_creds_opt vopt; 48 krb5_ccache id; 49 50 krb5_verify_init_creds_opt_init(&vopt); 51 krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, secure); 52 krb5_verify_init_creds_opt_set_service(&vopt, service); 53 54 ret = krb5_verify_init_creds(context, 55 cred, 56 server_principal, 57 keytab, 58 NULL, 59 &vopt); 60 if(ret) 61 return ret; 62 if(ccache == NULL) 63 ret = krb5_cc_default (context, &id); 64 else 65 id = ccache; 66 if(ret == 0){ 67 ret = krb5_cc_initialize(context, id, principal); 68 if(ret == 0){ 69 ret = krb5_cc_store_cred(context, id, cred); 70 } 71 if(ccache == NULL) 72 krb5_cc_close(context, id); 73 } 74 return ret; 75} 76 77struct krb5_verify_opt { 78 unsigned int flags; 79 krb5_ccache ccache; 80 krb5_keytab keytab; 81 krb5_boolean secure; 82 const char *service; 83 krb5_principal server; 84 krb5_prompter_fct prompter; 85 void *prompter_data; 86}; 87 88/* 89 * Verify user `principal' with `password'. 90 * 91 * If `secure', also verify against local service key for `service'. 92 * 93 * As a side effect, fresh tickets are obtained and stored in `ccache'. 94 */ 95 96KRB5_LIB_FUNCTION void KRB5_LIB_CALL 97krb5_verify_opt_init(krb5_verify_opt *opt) 98{ 99 memset(opt, 0, sizeof(*opt)); 100 opt->secure = TRUE; 101 opt->service = "host"; 102 opt->prompter = krb5_prompter_posix; 103} 104 105KRB5_LIB_FUNCTION int KRB5_LIB_CALL 106krb5_verify_opt_alloc(krb5_context context, krb5_verify_opt **opt) 107{ 108 *opt = calloc(1, sizeof(**opt)); 109 if ((*opt) == NULL) { 110 krb5_set_error_message(context, ENOMEM, 111 N_("malloc: out of memory", "")); 112 return ENOMEM; 113 } 114 krb5_verify_opt_init(*opt); 115 return 0; 116} 117 118KRB5_LIB_FUNCTION void KRB5_LIB_CALL 119krb5_verify_opt_free(krb5_verify_opt *opt) 120{ 121 free(opt); 122} 123 124KRB5_LIB_FUNCTION void KRB5_LIB_CALL 125krb5_verify_opt_set_ccache(krb5_verify_opt *opt, krb5_ccache ccache) 126{ 127 opt->ccache = ccache; 128} 129 130KRB5_LIB_FUNCTION void KRB5_LIB_CALL 131krb5_verify_opt_set_server(krb5_verify_opt *opt, krb5_principal server) 132{ 133 opt->server = server; 134} 135 136KRB5_LIB_FUNCTION void KRB5_LIB_CALL 137krb5_verify_opt_set_keytab(krb5_verify_opt *opt, krb5_keytab keytab) 138{ 139 opt->keytab = keytab; 140} 141 142KRB5_LIB_FUNCTION void KRB5_LIB_CALL 143krb5_verify_opt_set_secure(krb5_verify_opt *opt, krb5_boolean secure) 144{ 145 opt->secure = secure; 146} 147 148KRB5_LIB_FUNCTION void KRB5_LIB_CALL 149krb5_verify_opt_set_service(krb5_verify_opt *opt, const char *service) 150{ 151 opt->service = service; 152} 153 154KRB5_LIB_FUNCTION void KRB5_LIB_CALL 155krb5_verify_opt_set_flags(krb5_verify_opt *opt, unsigned int flags) 156{ 157 opt->flags |= flags; 158} 159 160KRB5_LIB_FUNCTION void KRB5_LIB_CALL 161krb5_verify_opt_set_prompter(krb5_verify_opt *opt, 162 krb5_prompter_fct prompter, 163 void *prompter_data) 164{ 165 opt->prompter = prompter; 166 opt->prompter_data = prompter_data; 167} 168 169 170static krb5_error_code 171verify_user_opt_int(krb5_context context, 172 krb5_principal principal, 173 const char *password, 174 krb5_verify_opt *vopt) 175 176{ 177 krb5_error_code ret; 178 krb5_get_init_creds_opt *opt; 179 krb5_creds cred; 180 181 memset(&cred, 0, sizeof(cred)); 182 183 ret = krb5_get_init_creds_opt_alloc(context, &opt); 184 if (ret) 185 return ret; 186 krb5_get_init_creds_opt_set_default_flags(context, NULL, 187 krb5_principal_get_realm(context, principal), 188 opt); 189 190 ret = krb5_get_init_creds_password(context, 191 &cred, 192 principal, 193 password, 194 vopt->prompter, 195 vopt->prompter_data, 196 0, 197 NULL, 198 opt); 199 if (ret) { 200 krb5_get_init_creds_opt_free(context, opt); 201 return ret; 202 } 203#define OPT(V, D) ((vopt && (vopt->V)) ? (vopt->V) : (D)) 204 ret = verify_common(context, principal, OPT(server, NULL), 205 OPT(ccache, NULL), 206 OPT(keytab, NULL), 207 vopt ? vopt->secure : TRUE, 208 OPT(service, "host"), &cred); 209#undef OPT 210 krb5_free_cred_contents(context, &cred); 211 krb5_get_init_creds_opt_free(context, opt); 212 return ret; 213} 214 215KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 216krb5_verify_user_opt(krb5_context context, 217 krb5_principal principal, 218 const char *password, 219 krb5_verify_opt *opt) 220{ 221 krb5_error_code ret; 222 223 if(opt && (opt->flags & KRB5_VERIFY_LREALMS)) { 224 krb5_realm *realms, *r; 225 ret = krb5_get_default_realms (context, &realms); 226 if (ret) 227 return ret; 228 ret = KRB5_CONFIG_NODEFREALM; 229 230 for (r = realms; *r != NULL && ret != 0; ++r) { 231 ret = krb5_principal_set_realm(context, principal, *r); 232 if (ret) { 233 krb5_free_host_realm (context, realms); 234 return ret; 235 } 236 237 ret = verify_user_opt_int(context, principal, password, opt); 238 } 239 krb5_free_host_realm (context, realms); 240 if(ret) 241 return ret; 242 } else 243 ret = verify_user_opt_int(context, principal, password, opt); 244 return ret; 245} 246 247/* compat function that calls above */ 248 249KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 250krb5_verify_user(krb5_context context, 251 krb5_principal principal, 252 krb5_ccache ccache, 253 const char *password, 254 krb5_boolean secure, 255 const char *service) 256{ 257 krb5_verify_opt opt; 258 259 krb5_verify_opt_init(&opt); 260 261 krb5_verify_opt_set_ccache(&opt, ccache); 262 krb5_verify_opt_set_secure(&opt, secure); 263 krb5_verify_opt_set_service(&opt, service); 264 265 return krb5_verify_user_opt(context, principal, password, &opt); 266} 267 268/* 269 * A variant of `krb5_verify_user'. The realm of `principal' is 270 * ignored and all the local realms are tried. 271 */ 272 273KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 274krb5_verify_user_lrealm(krb5_context context, 275 krb5_principal principal, 276 krb5_ccache ccache, 277 const char *password, 278 krb5_boolean secure, 279 const char *service) 280{ 281 krb5_verify_opt opt; 282 283 krb5_verify_opt_init(&opt); 284 285 krb5_verify_opt_set_ccache(&opt, ccache); 286 krb5_verify_opt_set_secure(&opt, secure); 287 krb5_verify_opt_set_service(&opt, service); 288 krb5_verify_opt_set_flags(&opt, KRB5_VERIFY_LREALMS); 289 290 return krb5_verify_user_opt(context, principal, password, &opt); 291} 292