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