1 /* $NetBSD: acquire.c,v 1.2 2017/01/28 21:31:44 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2005, PADL Software Pty Ltd. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * 3. Neither the name of PADL Software nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include "kcm_locl.h" 36 37 /* 38 * Get a new ticket using a keytab/cached key and swap it into 39 * an existing redentials cache 40 */ 41 42 krb5_error_code 43 kcm_ccache_acquire(krb5_context context, 44 kcm_ccache ccache, 45 krb5_creds **credp) 46 { 47 krb5_error_code ret = 0; 48 krb5_creds cred; 49 krb5_const_realm realm; 50 krb5_get_init_creds_opt *opt = NULL; 51 krb5_ccache_data ccdata; 52 char *in_tkt_service = NULL; 53 const char *estr; 54 55 *credp = NULL; 56 memset(&cred, 0, sizeof(cred)); 57 58 KCM_ASSERT_VALID(ccache); 59 60 /* We need a cached key or keytab to acquire credentials */ 61 if (ccache->flags & KCM_FLAGS_USE_CACHED_KEY) { 62 if (ccache->key.keyblock.keyvalue.length == 0) 63 krb5_abortx(context, 64 "kcm_ccache_acquire: KCM_FLAGS_USE_CACHED_KEY without key"); 65 } else if (ccache->flags & KCM_FLAGS_USE_KEYTAB) { 66 if (ccache->key.keytab == NULL) 67 krb5_abortx(context, 68 "kcm_ccache_acquire: KCM_FLAGS_USE_KEYTAB without keytab"); 69 } else { 70 kcm_log(0, "Cannot acquire initial credentials for cache %s without key", 71 ccache->name); 72 return KRB5_FCC_INTERNAL; 73 } 74 75 HEIMDAL_MUTEX_lock(&ccache->mutex); 76 77 /* Fake up an internal ccache */ 78 kcm_internal_ccache(context, ccache, &ccdata); 79 80 /* Now, actually acquire the creds */ 81 if (ccache->server != NULL) { 82 ret = krb5_unparse_name(context, ccache->server, &in_tkt_service); 83 if (ret) { 84 estr = krb5_get_error_message(context, ret); 85 kcm_log(0, "Failed to unparse service principal name for cache %s: %s", 86 ccache->name, estr); 87 krb5_free_error_message(context, estr); 88 goto out; 89 } 90 } 91 92 realm = krb5_principal_get_realm(context, ccache->client); 93 94 ret = krb5_get_init_creds_opt_alloc(context, &opt); 95 if (ret) 96 goto out; 97 krb5_get_init_creds_opt_set_default_flags(context, "kcm", realm, opt); 98 if (ccache->tkt_life != 0) 99 krb5_get_init_creds_opt_set_tkt_life(opt, ccache->tkt_life); 100 if (ccache->renew_life != 0) 101 krb5_get_init_creds_opt_set_renew_life(opt, ccache->renew_life); 102 103 if (ccache->flags & KCM_FLAGS_USE_CACHED_KEY) { 104 ret = krb5_get_init_creds_keyblock(context, 105 &cred, 106 ccache->client, 107 &ccache->key.keyblock, 108 0, 109 in_tkt_service, 110 opt); 111 } else { 112 /* loosely based on lib/krb5/init_creds_pw.c */ 113 ret = krb5_get_init_creds_keytab(context, 114 &cred, 115 ccache->client, 116 ccache->key.keytab, 117 0, 118 in_tkt_service, 119 opt); 120 } 121 122 if (ret) { 123 estr = krb5_get_error_message(context, ret); 124 kcm_log(0, "Failed to acquire credentials for cache %s: %s", 125 ccache->name, estr); 126 krb5_free_error_message(context, estr); 127 goto out; 128 } 129 130 /* Swap them in */ 131 kcm_ccache_remove_creds_internal(context, ccache); 132 133 ret = kcm_ccache_store_cred_internal(context, ccache, &cred, 0, credp); 134 if (ret) { 135 estr = krb5_get_error_message(context, ret); 136 kcm_log(0, "Failed to store credentials for cache %s: %s", 137 ccache->name, estr); 138 krb5_free_error_message(context, estr); 139 krb5_free_cred_contents(context, &cred); 140 goto out; 141 } 142 143 out: 144 free(in_tkt_service); 145 if (opt) 146 krb5_get_init_creds_opt_free(context, opt); 147 148 HEIMDAL_MUTEX_unlock(&ccache->mutex); 149 150 return ret; 151 } 152