2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5/gsskrb5_locl.h"
36 RCSID("$Id: acquire_cred.c 21221 2007-06-20 08:42:10Z lha $");
39 __gsskrb5_ccache_lifetime(OM_uint32 *minor_status,
42 krb5_principal principal,
45 krb5_creds in_cred, *out_cred;
46 krb5_const_realm realm;
49 memset(&in_cred, 0, sizeof(in_cred));
50 in_cred.client = principal;
52 realm = krb5_principal_get_realm(context, principal);
54 _gsskrb5_clear_status ();
55 *minor_status = KRB5_PRINC_NOMATCH; /* XXX */
59 kret = krb5_make_principal(context, &in_cred.server,
60 realm, KRB5_TGS_NAME, realm, NULL);
66 kret = krb5_get_credentials(context, 0,
67 id, &in_cred, &out_cred);
68 krb5_free_principal(context, in_cred.server);
74 *lifetime = out_cred->times.endtime;
75 krb5_free_creds(context, out_cred);
77 return GSS_S_COMPLETE;
83 static krb5_error_code
84 get_keytab(krb5_context context, krb5_keytab *keytab)
89 HEIMDAL_MUTEX_lock(&gssapi_keytab_mutex);
91 if (_gsskrb5_keytab != NULL) {
92 kret = krb5_kt_get_name(context,
94 kt_name, sizeof(kt_name));
96 kret = krb5_kt_resolve(context, kt_name, keytab);
98 kret = krb5_kt_default(context, keytab);
100 HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
105 static OM_uint32 acquire_initiator_cred
106 (OM_uint32 * minor_status,
107 krb5_context context,
108 const gss_name_t desired_name,
110 const gss_OID_set desired_mechs,
111 gss_cred_usage_t cred_usage,
113 gss_OID_set * actual_mechs,
119 krb5_principal def_princ;
120 krb5_get_init_creds_opt *opt;
123 krb5_error_code kret;
129 memset(&cred, 0, sizeof(cred));
131 /* If we have a preferred principal, lets try to find it in all
132 * caches, otherwise, fall back to default cache. Ignore
134 if (handle->principal)
135 kret = krb5_cc_cache_match (context,
140 if (ccache == NULL) {
141 kret = krb5_cc_default(context, &ccache);
145 kret = krb5_cc_get_principal(context, ccache,
148 /* we'll try to use a keytab below */
149 krb5_cc_destroy(context, ccache);
152 } else if (handle->principal == NULL) {
153 kret = krb5_copy_principal(context, def_princ,
157 } else if (handle->principal != NULL &&
158 krb5_principal_compare(context, handle->principal,
159 def_princ) == FALSE) {
160 /* Before failing, lets check the keytab */
161 krb5_free_principal(context, def_princ);
164 if (def_princ == NULL) {
165 /* We have no existing credentials cache,
166 * so attempt to get a TGT using a keytab.
168 if (handle->principal == NULL) {
169 kret = krb5_get_default_principal(context,
174 kret = get_keytab(context, &keytab);
177 kret = krb5_get_init_creds_opt_alloc(context, &opt);
180 kret = krb5_get_init_creds_keytab(context, &cred,
181 handle->principal, keytab, 0, NULL, opt);
182 krb5_get_init_creds_opt_free(context, opt);
185 kret = krb5_cc_gen_new(context, &krb5_mcc_ops,
189 kret = krb5_cc_initialize(context, ccache, cred.client);
192 kret = krb5_cc_store_cred(context, ccache, &cred);
195 handle->lifetime = cred.times.endtime;
196 handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
199 ret = __gsskrb5_ccache_lifetime(minor_status,
204 if (ret != GSS_S_COMPLETE)
209 handle->ccache = ccache;
210 ret = GSS_S_COMPLETE;
213 if (cred.client != NULL)
214 krb5_free_cred_contents(context, &cred);
215 if (def_princ != NULL)
216 krb5_free_principal(context, def_princ);
218 krb5_kt_close(context, keytab);
219 if (ret != GSS_S_COMPLETE) {
221 krb5_cc_close(context, ccache);
223 *minor_status = kret;
229 static OM_uint32 acquire_acceptor_cred
230 (OM_uint32 * minor_status,
231 krb5_context context,
232 const gss_name_t desired_name,
234 const gss_OID_set desired_mechs,
235 gss_cred_usage_t cred_usage,
237 gss_OID_set * actual_mechs,
242 krb5_error_code kret;
246 kret = get_keytab(context, &handle->keytab);
250 /* check that the requested principal exists in the keytab */
251 if (handle->principal) {
252 krb5_keytab_entry entry;
254 kret = krb5_kt_get_entry(context, handle->keytab,
255 handle->principal, 0, 0, &entry);
258 krb5_kt_free_entry(context, &entry);
259 ret = GSS_S_COMPLETE;
263 if (ret != GSS_S_COMPLETE) {
264 if (handle->keytab != NULL)
265 krb5_kt_close(context, handle->keytab);
267 *minor_status = kret;
273 OM_uint32 _gsskrb5_acquire_cred
274 (OM_uint32 * minor_status,
275 const gss_name_t desired_name,
277 const gss_OID_set desired_mechs,
278 gss_cred_usage_t cred_usage,
279 gss_cred_id_t * output_cred_handle,
280 gss_OID_set * actual_mechs,
284 krb5_context context;
288 if (cred_usage != GSS_C_ACCEPT && cred_usage != GSS_C_INITIATE && cred_usage != GSS_C_BOTH) {
289 *minor_status = GSS_KRB5_S_G_BAD_USAGE;
290 return GSS_S_FAILURE;
293 GSSAPI_KRB5_INIT(&context);
295 *output_cred_handle = NULL;
299 *actual_mechs = GSS_C_NO_OID_SET;
304 ret = gss_test_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
305 desired_mechs, &present);
310 return GSS_S_BAD_MECH;
314 handle = calloc(1, sizeof(*handle));
315 if (handle == NULL) {
316 *minor_status = ENOMEM;
317 return (GSS_S_FAILURE);
320 HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
322 if (desired_name != GSS_C_NO_NAME) {
323 krb5_principal name = (krb5_principal)desired_name;
324 ret = krb5_copy_principal(context, name, &handle->principal);
326 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
329 return GSS_S_FAILURE;
332 if (cred_usage == GSS_C_INITIATE || cred_usage == GSS_C_BOTH) {
333 ret = acquire_initiator_cred(minor_status, context,
334 desired_name, time_req,
335 desired_mechs, cred_usage, handle,
336 actual_mechs, time_rec);
337 if (ret != GSS_S_COMPLETE) {
338 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
339 krb5_free_principal(context, handle->principal);
344 if (cred_usage == GSS_C_ACCEPT || cred_usage == GSS_C_BOTH) {
345 ret = acquire_acceptor_cred(minor_status, context,
346 desired_name, time_req,
347 desired_mechs, cred_usage, handle, actual_mechs, time_rec);
348 if (ret != GSS_S_COMPLETE) {
349 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
350 krb5_free_principal(context, handle->principal);
355 ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
356 if (ret == GSS_S_COMPLETE)
357 ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
358 &handle->mechanisms);
359 if (ret == GSS_S_COMPLETE)
360 ret = _gsskrb5_inquire_cred(minor_status, (gss_cred_id_t)handle,
361 NULL, time_rec, NULL, actual_mechs);
362 if (ret != GSS_S_COMPLETE) {
363 if (handle->mechanisms != NULL)
364 gss_release_oid_set(NULL, &handle->mechanisms);
365 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
366 krb5_free_principal(context, handle->principal);
372 ret = _gsskrb5_lifetime_left(minor_status,
380 handle->usage = cred_usage;
381 *output_cred_handle = (gss_cred_id_t)handle;
382 return (GSS_S_COMPLETE);