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