1b528cefcSMark Murray /* 25e9cd1aeSAssar Westerlund * Copyright (c) 1997-2001 Kungliga Tekniska H�gskolan 3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden). 4b528cefcSMark Murray * All rights reserved. 5b528cefcSMark Murray * 6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without 7b528cefcSMark Murray * modification, are permitted provided that the following conditions 8b528cefcSMark Murray * are met: 9b528cefcSMark Murray * 10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright 11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer. 12b528cefcSMark Murray * 13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright 14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the 15b528cefcSMark Murray * documentation and/or other materials provided with the distribution. 16b528cefcSMark Murray * 17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors 18b528cefcSMark Murray * may be used to endorse or promote products derived from this software 19b528cefcSMark Murray * without specific prior written permission. 20b528cefcSMark Murray * 21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31b528cefcSMark Murray * SUCH DAMAGE. 32b528cefcSMark Murray */ 33b528cefcSMark Murray 34b528cefcSMark Murray #include "krb5_locl.h" 35b528cefcSMark Murray 36adb0ddaeSAssar Westerlund RCSID("$Id: verify_user.c,v 1.14 2001/05/14 09:06:53 joda Exp $"); 37b528cefcSMark Murray 38b528cefcSMark Murray static krb5_error_code 39b528cefcSMark Murray verify_common (krb5_context context, 40b528cefcSMark Murray krb5_principal principal, 41b528cefcSMark Murray krb5_ccache ccache, 42adb0ddaeSAssar Westerlund krb5_keytab keytab, 43b528cefcSMark Murray krb5_boolean secure, 44b528cefcSMark Murray const char *service, 45b528cefcSMark Murray krb5_creds cred) 46b528cefcSMark Murray { 47b528cefcSMark Murray krb5_error_code ret; 48b528cefcSMark Murray krb5_principal server; 49b528cefcSMark Murray krb5_verify_init_creds_opt vopt; 50b528cefcSMark Murray krb5_ccache id; 51b528cefcSMark Murray 52b528cefcSMark Murray ret = krb5_sname_to_principal (context, NULL, service, KRB5_NT_SRV_HST, 53b528cefcSMark Murray &server); 54adb0ddaeSAssar Westerlund if(ret) 55adb0ddaeSAssar Westerlund return ret; 56b528cefcSMark Murray 57b528cefcSMark Murray krb5_verify_init_creds_opt_init(&vopt); 58b528cefcSMark Murray krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, secure); 59b528cefcSMark Murray 60b528cefcSMark Murray ret = krb5_verify_init_creds(context, 61b528cefcSMark Murray &cred, 62b528cefcSMark Murray server, 63adb0ddaeSAssar Westerlund keytab, 64b528cefcSMark Murray NULL, 65b528cefcSMark Murray &vopt); 66b528cefcSMark Murray krb5_free_principal(context, server); 67adb0ddaeSAssar Westerlund if(ret) 68adb0ddaeSAssar Westerlund return ret; 69b528cefcSMark Murray if(ccache == NULL) 70b528cefcSMark Murray ret = krb5_cc_default (context, &id); 71b528cefcSMark Murray else 72b528cefcSMark Murray id = ccache; 73b528cefcSMark Murray if(ret == 0){ 74b528cefcSMark Murray ret = krb5_cc_initialize(context, id, principal); 75b528cefcSMark Murray if(ret == 0){ 76b528cefcSMark Murray ret = krb5_cc_store_cred(context, id, &cred); 77b528cefcSMark Murray } 78b528cefcSMark Murray if(ccache == NULL) 79b528cefcSMark Murray krb5_cc_close(context, id); 80b528cefcSMark Murray } 81b528cefcSMark Murray krb5_free_creds_contents(context, &cred); 82b528cefcSMark Murray return ret; 83b528cefcSMark Murray } 84b528cefcSMark Murray 85b528cefcSMark Murray /* 86b528cefcSMark Murray * Verify user `principal' with `password'. 87b528cefcSMark Murray * 88b528cefcSMark Murray * If `secure', also verify against local service key for `service'. 89b528cefcSMark Murray * 90b528cefcSMark Murray * As a side effect, fresh tickets are obtained and stored in `ccache'. 91b528cefcSMark Murray */ 92b528cefcSMark Murray 93adb0ddaeSAssar Westerlund void 94adb0ddaeSAssar Westerlund krb5_verify_opt_init(krb5_verify_opt *opt) 95b528cefcSMark Murray { 96adb0ddaeSAssar Westerlund memset(opt, 0, sizeof(*opt)); 97adb0ddaeSAssar Westerlund opt->secure = TRUE; 98adb0ddaeSAssar Westerlund opt->service = "host"; 99adb0ddaeSAssar Westerlund } 100b528cefcSMark Murray 101adb0ddaeSAssar Westerlund void 102adb0ddaeSAssar Westerlund krb5_verify_opt_set_ccache(krb5_verify_opt *opt, krb5_ccache ccache) 103adb0ddaeSAssar Westerlund { 104adb0ddaeSAssar Westerlund opt->ccache = ccache; 105adb0ddaeSAssar Westerlund } 106adb0ddaeSAssar Westerlund 107adb0ddaeSAssar Westerlund void 108adb0ddaeSAssar Westerlund krb5_verify_opt_set_keytab(krb5_verify_opt *opt, krb5_keytab keytab) 109adb0ddaeSAssar Westerlund { 110adb0ddaeSAssar Westerlund opt->keytab = keytab; 111adb0ddaeSAssar Westerlund } 112adb0ddaeSAssar Westerlund 113adb0ddaeSAssar Westerlund void 114adb0ddaeSAssar Westerlund krb5_verify_opt_set_secure(krb5_verify_opt *opt, krb5_boolean secure) 115adb0ddaeSAssar Westerlund { 116adb0ddaeSAssar Westerlund opt->secure = secure; 117adb0ddaeSAssar Westerlund } 118adb0ddaeSAssar Westerlund 119adb0ddaeSAssar Westerlund void 120adb0ddaeSAssar Westerlund krb5_verify_opt_set_service(krb5_verify_opt *opt, const char *service) 121adb0ddaeSAssar Westerlund { 122adb0ddaeSAssar Westerlund opt->service = service; 123adb0ddaeSAssar Westerlund } 124adb0ddaeSAssar Westerlund 125adb0ddaeSAssar Westerlund void 126adb0ddaeSAssar Westerlund krb5_verify_opt_set_flags(krb5_verify_opt *opt, unsigned int flags) 127adb0ddaeSAssar Westerlund { 128adb0ddaeSAssar Westerlund opt->flags |= flags; 129adb0ddaeSAssar Westerlund } 130adb0ddaeSAssar Westerlund 131adb0ddaeSAssar Westerlund static krb5_error_code 132adb0ddaeSAssar Westerlund verify_user_opt_int(krb5_context context, 133adb0ddaeSAssar Westerlund krb5_principal principal, 134adb0ddaeSAssar Westerlund const char *password, 135adb0ddaeSAssar Westerlund krb5_verify_opt *vopt) 136adb0ddaeSAssar Westerlund 137adb0ddaeSAssar Westerlund { 138b528cefcSMark Murray krb5_error_code ret; 139b528cefcSMark Murray krb5_get_init_creds_opt opt; 140b528cefcSMark Murray krb5_creds cred; 141b528cefcSMark Murray 142b528cefcSMark Murray krb5_get_init_creds_opt_init (&opt); 1435e9cd1aeSAssar Westerlund krb5_get_init_creds_opt_set_default_flags(context, NULL, 1445e9cd1aeSAssar Westerlund *krb5_princ_realm(context, principal), 1455e9cd1aeSAssar Westerlund &opt); 146b528cefcSMark Murray ret = krb5_get_init_creds_password (context, 147b528cefcSMark Murray &cred, 148b528cefcSMark Murray principal, 149b528cefcSMark Murray (char*)password, 150b528cefcSMark Murray krb5_prompter_posix, 151b528cefcSMark Murray NULL, 152b528cefcSMark Murray 0, 153b528cefcSMark Murray NULL, 154b528cefcSMark Murray &opt); 155b528cefcSMark Murray if(ret) 156b528cefcSMark Murray return ret; 157adb0ddaeSAssar Westerlund #define OPT(V, D) ((vopt && (vopt->V)) ? (vopt->V) : (D)) 158adb0ddaeSAssar Westerlund return verify_common (context, principal, OPT(ccache, NULL), 159adb0ddaeSAssar Westerlund OPT(keytab, NULL), vopt ? vopt->secure : TRUE, 160adb0ddaeSAssar Westerlund OPT(service, "host"), cred); 161adb0ddaeSAssar Westerlund #undef OPT 162adb0ddaeSAssar Westerlund } 163adb0ddaeSAssar Westerlund 164adb0ddaeSAssar Westerlund krb5_error_code 165adb0ddaeSAssar Westerlund krb5_verify_user_opt(krb5_context context, 166adb0ddaeSAssar Westerlund krb5_principal principal, 167adb0ddaeSAssar Westerlund const char *password, 168adb0ddaeSAssar Westerlund krb5_verify_opt *opt) 169adb0ddaeSAssar Westerlund { 170adb0ddaeSAssar Westerlund krb5_error_code ret; 171adb0ddaeSAssar Westerlund 172adb0ddaeSAssar Westerlund if(opt && (opt->flags & KRB5_VERIFY_LREALMS)) { 173adb0ddaeSAssar Westerlund krb5_realm *realms, *r; 174adb0ddaeSAssar Westerlund ret = krb5_get_default_realms (context, &realms); 175adb0ddaeSAssar Westerlund if (ret) 176adb0ddaeSAssar Westerlund return ret; 177adb0ddaeSAssar Westerlund ret = KRB5_CONFIG_NODEFREALM; 178adb0ddaeSAssar Westerlund 179adb0ddaeSAssar Westerlund for (r = realms; *r != NULL && ret != 0; ++r) { 180adb0ddaeSAssar Westerlund char *tmp = strdup (*r); 181adb0ddaeSAssar Westerlund 182adb0ddaeSAssar Westerlund if (tmp == NULL) { 183adb0ddaeSAssar Westerlund krb5_free_host_realm (context, realms); 184adb0ddaeSAssar Westerlund krb5_set_error_string (context, "malloc: out of memory"); 185adb0ddaeSAssar Westerlund return ENOMEM; 186adb0ddaeSAssar Westerlund } 187adb0ddaeSAssar Westerlund free (*krb5_princ_realm (context, principal)); 188adb0ddaeSAssar Westerlund krb5_princ_set_realm (context, principal, &tmp); 189adb0ddaeSAssar Westerlund 190adb0ddaeSAssar Westerlund ret = verify_user_opt_int(context, principal, password, opt); 191adb0ddaeSAssar Westerlund } 192adb0ddaeSAssar Westerlund krb5_free_host_realm (context, realms); 193adb0ddaeSAssar Westerlund if(ret) 194adb0ddaeSAssar Westerlund return ret; 195adb0ddaeSAssar Westerlund } else 196adb0ddaeSAssar Westerlund ret = verify_user_opt_int(context, principal, password, opt); 197adb0ddaeSAssar Westerlund return ret; 198adb0ddaeSAssar Westerlund } 199adb0ddaeSAssar Westerlund 200adb0ddaeSAssar Westerlund /* compat function that calls above */ 201adb0ddaeSAssar Westerlund 202adb0ddaeSAssar Westerlund krb5_error_code 203adb0ddaeSAssar Westerlund krb5_verify_user(krb5_context context, 204adb0ddaeSAssar Westerlund krb5_principal principal, 205adb0ddaeSAssar Westerlund krb5_ccache ccache, 206adb0ddaeSAssar Westerlund const char *password, 207adb0ddaeSAssar Westerlund krb5_boolean secure, 208adb0ddaeSAssar Westerlund const char *service) 209adb0ddaeSAssar Westerlund { 210adb0ddaeSAssar Westerlund krb5_verify_opt opt; 211adb0ddaeSAssar Westerlund 212adb0ddaeSAssar Westerlund krb5_verify_opt_init(&opt); 213adb0ddaeSAssar Westerlund 214adb0ddaeSAssar Westerlund krb5_verify_opt_set_ccache(&opt, ccache); 215adb0ddaeSAssar Westerlund krb5_verify_opt_set_secure(&opt, secure); 216adb0ddaeSAssar Westerlund krb5_verify_opt_set_service(&opt, service); 217adb0ddaeSAssar Westerlund 218adb0ddaeSAssar Westerlund return krb5_verify_user_opt(context, principal, password, &opt); 219b528cefcSMark Murray } 220b528cefcSMark Murray 221b528cefcSMark Murray /* 222b528cefcSMark Murray * A variant of `krb5_verify_user'. The realm of `principal' is 223b528cefcSMark Murray * ignored and all the local realms are tried. 224b528cefcSMark Murray */ 225b528cefcSMark Murray 226b528cefcSMark Murray krb5_error_code 227b528cefcSMark Murray krb5_verify_user_lrealm(krb5_context context, 228b528cefcSMark Murray krb5_principal principal, 229b528cefcSMark Murray krb5_ccache ccache, 230b528cefcSMark Murray const char *password, 231b528cefcSMark Murray krb5_boolean secure, 232b528cefcSMark Murray const char *service) 233b528cefcSMark Murray { 234adb0ddaeSAssar Westerlund krb5_verify_opt opt; 235b528cefcSMark Murray 236adb0ddaeSAssar Westerlund krb5_verify_opt_init(&opt); 237b528cefcSMark Murray 238adb0ddaeSAssar Westerlund krb5_verify_opt_set_ccache(&opt, ccache); 239adb0ddaeSAssar Westerlund krb5_verify_opt_set_secure(&opt, secure); 240adb0ddaeSAssar Westerlund krb5_verify_opt_set_service(&opt, service); 241adb0ddaeSAssar Westerlund krb5_verify_opt_set_flags(&opt, KRB5_VERIFY_LREALMS); 242b528cefcSMark Murray 243adb0ddaeSAssar Westerlund return krb5_verify_user_opt(context, principal, password, &opt); 244b528cefcSMark Murray } 245