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