1*0a6a1f1dSLionel Sambuc /* $NetBSD: acquire_cred.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "gsskrb5_locl.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc OM_uint32
__gsskrb5_ccache_lifetime(OM_uint32 * minor_status,krb5_context context,krb5_ccache id,krb5_principal principal,OM_uint32 * lifetime)39ebfedea0SLionel Sambuc __gsskrb5_ccache_lifetime(OM_uint32 *minor_status,
40ebfedea0SLionel Sambuc krb5_context context,
41ebfedea0SLionel Sambuc krb5_ccache id,
42ebfedea0SLionel Sambuc krb5_principal principal,
43ebfedea0SLionel Sambuc OM_uint32 *lifetime)
44ebfedea0SLionel Sambuc {
45ebfedea0SLionel Sambuc krb5_creds in_cred, out_cred;
46ebfedea0SLionel Sambuc krb5_const_realm realm;
47ebfedea0SLionel Sambuc krb5_error_code kret;
48ebfedea0SLionel Sambuc
49ebfedea0SLionel Sambuc memset(&in_cred, 0, sizeof(in_cred));
50ebfedea0SLionel Sambuc in_cred.client = principal;
51ebfedea0SLionel Sambuc
52ebfedea0SLionel Sambuc realm = krb5_principal_get_realm(context, principal);
53ebfedea0SLionel Sambuc if (realm == NULL) {
54ebfedea0SLionel Sambuc _gsskrb5_clear_status ();
55ebfedea0SLionel Sambuc *minor_status = KRB5_PRINC_NOMATCH; /* XXX */
56ebfedea0SLionel Sambuc return GSS_S_FAILURE;
57ebfedea0SLionel Sambuc }
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc kret = krb5_make_principal(context, &in_cred.server,
60ebfedea0SLionel Sambuc realm, KRB5_TGS_NAME, realm, NULL);
61ebfedea0SLionel Sambuc if (kret) {
62ebfedea0SLionel Sambuc *minor_status = kret;
63ebfedea0SLionel Sambuc return GSS_S_FAILURE;
64ebfedea0SLionel Sambuc }
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc kret = krb5_cc_retrieve_cred(context, id, 0, &in_cred, &out_cred);
67ebfedea0SLionel Sambuc krb5_free_principal(context, in_cred.server);
68ebfedea0SLionel Sambuc if (kret) {
69ebfedea0SLionel Sambuc *minor_status = 0;
70ebfedea0SLionel Sambuc *lifetime = 0;
71ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
72ebfedea0SLionel Sambuc }
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc *lifetime = out_cred.times.endtime;
75ebfedea0SLionel Sambuc krb5_free_cred_contents(context, &out_cred);
76ebfedea0SLionel Sambuc
77ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc
80ebfedea0SLionel Sambuc
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc
83ebfedea0SLionel Sambuc static krb5_error_code
get_keytab(krb5_context context,krb5_keytab * keytab)84ebfedea0SLionel Sambuc get_keytab(krb5_context context, krb5_keytab *keytab)
85ebfedea0SLionel Sambuc {
86ebfedea0SLionel Sambuc krb5_error_code kret;
87ebfedea0SLionel Sambuc
88ebfedea0SLionel Sambuc HEIMDAL_MUTEX_lock(&gssapi_keytab_mutex);
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc if (_gsskrb5_keytab != NULL) {
91ebfedea0SLionel Sambuc char *name = NULL;
92ebfedea0SLionel Sambuc
93ebfedea0SLionel Sambuc kret = krb5_kt_get_full_name(context, _gsskrb5_keytab, &name);
94ebfedea0SLionel Sambuc if (kret == 0) {
95ebfedea0SLionel Sambuc kret = krb5_kt_resolve(context, name, keytab);
96ebfedea0SLionel Sambuc krb5_xfree(name);
97ebfedea0SLionel Sambuc }
98ebfedea0SLionel Sambuc } else
99ebfedea0SLionel Sambuc kret = krb5_kt_default(context, keytab);
100ebfedea0SLionel Sambuc
101ebfedea0SLionel Sambuc HEIMDAL_MUTEX_unlock(&gssapi_keytab_mutex);
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc return (kret);
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc
acquire_initiator_cred(OM_uint32 * minor_status,krb5_context context,gss_const_OID credential_type,const void * credential_data,const gss_name_t desired_name,OM_uint32 time_req,gss_const_OID desired_mech,gss_cred_usage_t cred_usage,gsskrb5_cred handle)106ebfedea0SLionel Sambuc static OM_uint32 acquire_initiator_cred
107ebfedea0SLionel Sambuc (OM_uint32 * minor_status,
108ebfedea0SLionel Sambuc krb5_context context,
109*0a6a1f1dSLionel Sambuc gss_const_OID credential_type,
110*0a6a1f1dSLionel Sambuc const void *credential_data,
111ebfedea0SLionel Sambuc const gss_name_t desired_name,
112ebfedea0SLionel Sambuc OM_uint32 time_req,
113*0a6a1f1dSLionel Sambuc gss_const_OID desired_mech,
114ebfedea0SLionel Sambuc gss_cred_usage_t cred_usage,
115*0a6a1f1dSLionel Sambuc gsskrb5_cred handle
116ebfedea0SLionel Sambuc )
117ebfedea0SLionel Sambuc {
118ebfedea0SLionel Sambuc OM_uint32 ret;
119ebfedea0SLionel Sambuc krb5_creds cred;
120ebfedea0SLionel Sambuc krb5_principal def_princ;
121ebfedea0SLionel Sambuc krb5_get_init_creds_opt *opt;
122ebfedea0SLionel Sambuc krb5_ccache ccache;
123ebfedea0SLionel Sambuc krb5_keytab keytab;
124ebfedea0SLionel Sambuc krb5_error_code kret;
125ebfedea0SLionel Sambuc
126ebfedea0SLionel Sambuc keytab = NULL;
127ebfedea0SLionel Sambuc ccache = NULL;
128ebfedea0SLionel Sambuc def_princ = NULL;
129ebfedea0SLionel Sambuc ret = GSS_S_FAILURE;
130ebfedea0SLionel Sambuc memset(&cred, 0, sizeof(cred));
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc /*
133ebfedea0SLionel Sambuc * If we have a preferred principal, lets try to find it in all
134ebfedea0SLionel Sambuc * caches, otherwise, fall back to default cache, ignore all
135ebfedea0SLionel Sambuc * errors while searching.
136ebfedea0SLionel Sambuc */
137ebfedea0SLionel Sambuc
138*0a6a1f1dSLionel Sambuc if (credential_type != GSS_C_NO_OID &&
139*0a6a1f1dSLionel Sambuc !gss_oid_equal(credential_type, GSS_C_CRED_PASSWORD)) {
140*0a6a1f1dSLionel Sambuc kret = KRB5_NOCREDS_SUPPLIED; /* XXX */
141*0a6a1f1dSLionel Sambuc goto end;
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc
144ebfedea0SLionel Sambuc if (handle->principal) {
145ebfedea0SLionel Sambuc kret = krb5_cc_cache_match (context,
146ebfedea0SLionel Sambuc handle->principal,
147ebfedea0SLionel Sambuc &ccache);
148ebfedea0SLionel Sambuc if (kret == 0) {
149ebfedea0SLionel Sambuc ret = GSS_S_COMPLETE;
150ebfedea0SLionel Sambuc goto found;
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc
154ebfedea0SLionel Sambuc if (ccache == NULL) {
155ebfedea0SLionel Sambuc kret = krb5_cc_default(context, &ccache);
156ebfedea0SLionel Sambuc if (kret)
157ebfedea0SLionel Sambuc goto end;
158ebfedea0SLionel Sambuc }
159ebfedea0SLionel Sambuc kret = krb5_cc_get_principal(context, ccache, &def_princ);
160ebfedea0SLionel Sambuc if (kret != 0) {
161ebfedea0SLionel Sambuc /* we'll try to use a keytab below */
162ebfedea0SLionel Sambuc krb5_cc_close(context, ccache);
163ebfedea0SLionel Sambuc def_princ = NULL;
164ebfedea0SLionel Sambuc kret = 0;
165ebfedea0SLionel Sambuc } else if (handle->principal == NULL) {
166ebfedea0SLionel Sambuc kret = krb5_copy_principal(context, def_princ, &handle->principal);
167ebfedea0SLionel Sambuc if (kret)
168ebfedea0SLionel Sambuc goto end;
169ebfedea0SLionel Sambuc } else if (handle->principal != NULL &&
170ebfedea0SLionel Sambuc krb5_principal_compare(context, handle->principal,
171ebfedea0SLionel Sambuc def_princ) == FALSE) {
172ebfedea0SLionel Sambuc krb5_free_principal(context, def_princ);
173ebfedea0SLionel Sambuc def_princ = NULL;
174ebfedea0SLionel Sambuc krb5_cc_close(context, ccache);
175ebfedea0SLionel Sambuc ccache = NULL;
176ebfedea0SLionel Sambuc }
177ebfedea0SLionel Sambuc if (def_princ == NULL) {
178ebfedea0SLionel Sambuc /* We have no existing credentials cache,
179ebfedea0SLionel Sambuc * so attempt to get a TGT using a keytab.
180ebfedea0SLionel Sambuc */
181ebfedea0SLionel Sambuc if (handle->principal == NULL) {
182ebfedea0SLionel Sambuc kret = krb5_get_default_principal(context, &handle->principal);
183ebfedea0SLionel Sambuc if (kret)
184ebfedea0SLionel Sambuc goto end;
185ebfedea0SLionel Sambuc }
186ebfedea0SLionel Sambuc kret = krb5_get_init_creds_opt_alloc(context, &opt);
187ebfedea0SLionel Sambuc if (kret)
188ebfedea0SLionel Sambuc goto end;
189*0a6a1f1dSLionel Sambuc if (credential_type != GSS_C_NO_OID &&
190*0a6a1f1dSLionel Sambuc gss_oid_equal(credential_type, GSS_C_CRED_PASSWORD)) {
191*0a6a1f1dSLionel Sambuc gss_buffer_t password = (gss_buffer_t)credential_data;
192*0a6a1f1dSLionel Sambuc
193*0a6a1f1dSLionel Sambuc /* XXX are we requiring password to be NUL terminated? */
194*0a6a1f1dSLionel Sambuc
195*0a6a1f1dSLionel Sambuc kret = krb5_get_init_creds_password(context, &cred,
196*0a6a1f1dSLionel Sambuc handle->principal,
197*0a6a1f1dSLionel Sambuc password->value,
198*0a6a1f1dSLionel Sambuc NULL, NULL, 0, NULL, opt);
199*0a6a1f1dSLionel Sambuc } else {
200*0a6a1f1dSLionel Sambuc kret = get_keytab(context, &keytab);
201*0a6a1f1dSLionel Sambuc if (kret) {
202*0a6a1f1dSLionel Sambuc krb5_get_init_creds_opt_free(context, opt);
203*0a6a1f1dSLionel Sambuc goto end;
204*0a6a1f1dSLionel Sambuc }
205ebfedea0SLionel Sambuc kret = krb5_get_init_creds_keytab(context, &cred,
206*0a6a1f1dSLionel Sambuc handle->principal, keytab,
207*0a6a1f1dSLionel Sambuc 0, NULL, opt);
208*0a6a1f1dSLionel Sambuc }
209ebfedea0SLionel Sambuc krb5_get_init_creds_opt_free(context, opt);
210ebfedea0SLionel Sambuc if (kret)
211ebfedea0SLionel Sambuc goto end;
212ebfedea0SLionel Sambuc kret = krb5_cc_new_unique(context, krb5_cc_type_memory,
213ebfedea0SLionel Sambuc NULL, &ccache);
214ebfedea0SLionel Sambuc if (kret)
215ebfedea0SLionel Sambuc goto end;
216ebfedea0SLionel Sambuc kret = krb5_cc_initialize(context, ccache, cred.client);
217ebfedea0SLionel Sambuc if (kret) {
218ebfedea0SLionel Sambuc krb5_cc_destroy(context, ccache);
219ebfedea0SLionel Sambuc goto end;
220ebfedea0SLionel Sambuc }
221ebfedea0SLionel Sambuc kret = krb5_cc_store_cred(context, ccache, &cred);
222ebfedea0SLionel Sambuc if (kret) {
223ebfedea0SLionel Sambuc krb5_cc_destroy(context, ccache);
224ebfedea0SLionel Sambuc goto end;
225ebfedea0SLionel Sambuc }
226ebfedea0SLionel Sambuc handle->lifetime = cred.times.endtime;
227ebfedea0SLionel Sambuc handle->cred_flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
228ebfedea0SLionel Sambuc } else {
229ebfedea0SLionel Sambuc
230ebfedea0SLionel Sambuc ret = __gsskrb5_ccache_lifetime(minor_status,
231ebfedea0SLionel Sambuc context,
232ebfedea0SLionel Sambuc ccache,
233ebfedea0SLionel Sambuc handle->principal,
234ebfedea0SLionel Sambuc &handle->lifetime);
235ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE) {
236ebfedea0SLionel Sambuc krb5_cc_close(context, ccache);
237ebfedea0SLionel Sambuc goto end;
238ebfedea0SLionel Sambuc }
239ebfedea0SLionel Sambuc kret = 0;
240ebfedea0SLionel Sambuc }
241ebfedea0SLionel Sambuc found:
242ebfedea0SLionel Sambuc handle->ccache = ccache;
243ebfedea0SLionel Sambuc ret = GSS_S_COMPLETE;
244ebfedea0SLionel Sambuc
245ebfedea0SLionel Sambuc end:
246ebfedea0SLionel Sambuc if (cred.client != NULL)
247ebfedea0SLionel Sambuc krb5_free_cred_contents(context, &cred);
248ebfedea0SLionel Sambuc if (def_princ != NULL)
249ebfedea0SLionel Sambuc krb5_free_principal(context, def_princ);
250ebfedea0SLionel Sambuc if (keytab != NULL)
251ebfedea0SLionel Sambuc krb5_kt_close(context, keytab);
252ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE && kret != 0)
253ebfedea0SLionel Sambuc *minor_status = kret;
254ebfedea0SLionel Sambuc return (ret);
255ebfedea0SLionel Sambuc }
256ebfedea0SLionel Sambuc
acquire_acceptor_cred(OM_uint32 * minor_status,krb5_context context,gss_const_OID credential_type,const void * credential_data,const gss_name_t desired_name,OM_uint32 time_req,gss_const_OID desired_mech,gss_cred_usage_t cred_usage,gsskrb5_cred handle)257ebfedea0SLionel Sambuc static OM_uint32 acquire_acceptor_cred
258ebfedea0SLionel Sambuc (OM_uint32 * minor_status,
259ebfedea0SLionel Sambuc krb5_context context,
260*0a6a1f1dSLionel Sambuc gss_const_OID credential_type,
261*0a6a1f1dSLionel Sambuc const void *credential_data,
262ebfedea0SLionel Sambuc const gss_name_t desired_name,
263ebfedea0SLionel Sambuc OM_uint32 time_req,
264*0a6a1f1dSLionel Sambuc gss_const_OID desired_mech,
265ebfedea0SLionel Sambuc gss_cred_usage_t cred_usage,
266*0a6a1f1dSLionel Sambuc gsskrb5_cred handle
267ebfedea0SLionel Sambuc )
268ebfedea0SLionel Sambuc {
269ebfedea0SLionel Sambuc OM_uint32 ret;
270ebfedea0SLionel Sambuc krb5_error_code kret;
271ebfedea0SLionel Sambuc
272ebfedea0SLionel Sambuc ret = GSS_S_FAILURE;
273*0a6a1f1dSLionel Sambuc
274*0a6a1f1dSLionel Sambuc if (credential_type != GSS_C_NO_OID) {
275*0a6a1f1dSLionel Sambuc kret = EINVAL;
276*0a6a1f1dSLionel Sambuc goto end;
277*0a6a1f1dSLionel Sambuc }
278*0a6a1f1dSLionel Sambuc
279ebfedea0SLionel Sambuc kret = get_keytab(context, &handle->keytab);
280ebfedea0SLionel Sambuc if (kret)
281ebfedea0SLionel Sambuc goto end;
282ebfedea0SLionel Sambuc
283ebfedea0SLionel Sambuc /* check that the requested principal exists in the keytab */
284ebfedea0SLionel Sambuc if (handle->principal) {
285ebfedea0SLionel Sambuc krb5_keytab_entry entry;
286ebfedea0SLionel Sambuc
287ebfedea0SLionel Sambuc kret = krb5_kt_get_entry(context, handle->keytab,
288ebfedea0SLionel Sambuc handle->principal, 0, 0, &entry);
289ebfedea0SLionel Sambuc if (kret)
290ebfedea0SLionel Sambuc goto end;
291ebfedea0SLionel Sambuc krb5_kt_free_entry(context, &entry);
292ebfedea0SLionel Sambuc ret = GSS_S_COMPLETE;
293ebfedea0SLionel Sambuc } else {
294ebfedea0SLionel Sambuc /*
295ebfedea0SLionel Sambuc * Check if there is at least one entry in the keytab before
296ebfedea0SLionel Sambuc * declaring it as an useful keytab.
297ebfedea0SLionel Sambuc */
298ebfedea0SLionel Sambuc krb5_keytab_entry tmp;
299ebfedea0SLionel Sambuc krb5_kt_cursor c;
300ebfedea0SLionel Sambuc
301ebfedea0SLionel Sambuc kret = krb5_kt_start_seq_get (context, handle->keytab, &c);
302ebfedea0SLionel Sambuc if (kret)
303ebfedea0SLionel Sambuc goto end;
304ebfedea0SLionel Sambuc if (krb5_kt_next_entry(context, handle->keytab, &tmp, &c) == 0) {
305ebfedea0SLionel Sambuc krb5_kt_free_entry(context, &tmp);
306ebfedea0SLionel Sambuc ret = GSS_S_COMPLETE; /* ok found one entry */
307ebfedea0SLionel Sambuc }
308ebfedea0SLionel Sambuc krb5_kt_end_seq_get (context, handle->keytab, &c);
309ebfedea0SLionel Sambuc }
310ebfedea0SLionel Sambuc end:
311ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE) {
312ebfedea0SLionel Sambuc if (handle->keytab != NULL)
313ebfedea0SLionel Sambuc krb5_kt_close(context, handle->keytab);
314ebfedea0SLionel Sambuc if (kret != 0) {
315ebfedea0SLionel Sambuc *minor_status = kret;
316ebfedea0SLionel Sambuc }
317ebfedea0SLionel Sambuc }
318ebfedea0SLionel Sambuc return (ret);
319ebfedea0SLionel Sambuc }
320ebfedea0SLionel Sambuc
_gsskrb5_acquire_cred(OM_uint32 * minor_status,const gss_name_t desired_name,OM_uint32 time_req,const gss_OID_set desired_mechs,gss_cred_usage_t cred_usage,gss_cred_id_t * output_cred_handle,gss_OID_set * actual_mechs,OM_uint32 * time_rec)321ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV _gsskrb5_acquire_cred
322ebfedea0SLionel Sambuc (OM_uint32 * minor_status,
323ebfedea0SLionel Sambuc const gss_name_t desired_name,
324ebfedea0SLionel Sambuc OM_uint32 time_req,
325ebfedea0SLionel Sambuc const gss_OID_set desired_mechs,
326ebfedea0SLionel Sambuc gss_cred_usage_t cred_usage,
327ebfedea0SLionel Sambuc gss_cred_id_t * output_cred_handle,
328ebfedea0SLionel Sambuc gss_OID_set * actual_mechs,
329ebfedea0SLionel Sambuc OM_uint32 * time_rec
330ebfedea0SLionel Sambuc )
331ebfedea0SLionel Sambuc {
332ebfedea0SLionel Sambuc OM_uint32 ret;
333ebfedea0SLionel Sambuc
334ebfedea0SLionel Sambuc if (desired_mechs) {
335ebfedea0SLionel Sambuc int present = 0;
336ebfedea0SLionel Sambuc
337ebfedea0SLionel Sambuc ret = gss_test_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
338ebfedea0SLionel Sambuc desired_mechs, &present);
339ebfedea0SLionel Sambuc if (ret)
340ebfedea0SLionel Sambuc return ret;
341ebfedea0SLionel Sambuc if (!present) {
342ebfedea0SLionel Sambuc *minor_status = 0;
343ebfedea0SLionel Sambuc return GSS_S_BAD_MECH;
344ebfedea0SLionel Sambuc }
345ebfedea0SLionel Sambuc }
346ebfedea0SLionel Sambuc
347*0a6a1f1dSLionel Sambuc ret = _gsskrb5_acquire_cred_ext(minor_status,
348*0a6a1f1dSLionel Sambuc desired_name,
349*0a6a1f1dSLionel Sambuc GSS_C_NO_OID,
350*0a6a1f1dSLionel Sambuc NULL,
351*0a6a1f1dSLionel Sambuc time_req,
352*0a6a1f1dSLionel Sambuc GSS_KRB5_MECHANISM,
353*0a6a1f1dSLionel Sambuc cred_usage,
354*0a6a1f1dSLionel Sambuc output_cred_handle);
355*0a6a1f1dSLionel Sambuc if (ret)
356*0a6a1f1dSLionel Sambuc return ret;
357*0a6a1f1dSLionel Sambuc
358*0a6a1f1dSLionel Sambuc
359*0a6a1f1dSLionel Sambuc ret = _gsskrb5_inquire_cred(minor_status, *output_cred_handle,
360*0a6a1f1dSLionel Sambuc NULL, time_rec, NULL, actual_mechs);
361*0a6a1f1dSLionel Sambuc if (ret) {
362*0a6a1f1dSLionel Sambuc OM_uint32 tmp;
363*0a6a1f1dSLionel Sambuc _gsskrb5_release_cred(&tmp, output_cred_handle);
364*0a6a1f1dSLionel Sambuc }
365*0a6a1f1dSLionel Sambuc
366*0a6a1f1dSLionel Sambuc return ret;
367*0a6a1f1dSLionel Sambuc }
368*0a6a1f1dSLionel Sambuc
_gsskrb5_acquire_cred_ext(OM_uint32 * minor_status,const gss_name_t desired_name,gss_const_OID credential_type,const void * credential_data,OM_uint32 time_req,gss_const_OID desired_mech,gss_cred_usage_t cred_usage,gss_cred_id_t * output_cred_handle)369*0a6a1f1dSLionel Sambuc OM_uint32 GSSAPI_CALLCONV _gsskrb5_acquire_cred_ext
370*0a6a1f1dSLionel Sambuc (OM_uint32 * minor_status,
371*0a6a1f1dSLionel Sambuc const gss_name_t desired_name,
372*0a6a1f1dSLionel Sambuc gss_const_OID credential_type,
373*0a6a1f1dSLionel Sambuc const void *credential_data,
374*0a6a1f1dSLionel Sambuc OM_uint32 time_req,
375*0a6a1f1dSLionel Sambuc gss_const_OID desired_mech,
376*0a6a1f1dSLionel Sambuc gss_cred_usage_t cred_usage,
377*0a6a1f1dSLionel Sambuc gss_cred_id_t * output_cred_handle
378*0a6a1f1dSLionel Sambuc )
379*0a6a1f1dSLionel Sambuc {
380*0a6a1f1dSLionel Sambuc krb5_context context;
381*0a6a1f1dSLionel Sambuc gsskrb5_cred handle;
382*0a6a1f1dSLionel Sambuc OM_uint32 ret;
383*0a6a1f1dSLionel Sambuc
384*0a6a1f1dSLionel Sambuc cred_usage &= GSS_C_OPTION_MASK;
385*0a6a1f1dSLionel Sambuc
386*0a6a1f1dSLionel Sambuc if (cred_usage != GSS_C_ACCEPT && cred_usage != GSS_C_INITIATE && cred_usage != GSS_C_BOTH) {
387*0a6a1f1dSLionel Sambuc *minor_status = GSS_KRB5_S_G_BAD_USAGE;
388*0a6a1f1dSLionel Sambuc return GSS_S_FAILURE;
389*0a6a1f1dSLionel Sambuc }
390*0a6a1f1dSLionel Sambuc
391*0a6a1f1dSLionel Sambuc GSSAPI_KRB5_INIT(&context);
392*0a6a1f1dSLionel Sambuc
393*0a6a1f1dSLionel Sambuc *output_cred_handle = NULL;
394*0a6a1f1dSLionel Sambuc
395ebfedea0SLionel Sambuc handle = calloc(1, sizeof(*handle));
396ebfedea0SLionel Sambuc if (handle == NULL) {
397ebfedea0SLionel Sambuc *minor_status = ENOMEM;
398ebfedea0SLionel Sambuc return (GSS_S_FAILURE);
399ebfedea0SLionel Sambuc }
400ebfedea0SLionel Sambuc
401ebfedea0SLionel Sambuc HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
402ebfedea0SLionel Sambuc
403ebfedea0SLionel Sambuc if (desired_name != GSS_C_NO_NAME) {
404ebfedea0SLionel Sambuc ret = _gsskrb5_canon_name(minor_status, context, 1, NULL,
405ebfedea0SLionel Sambuc desired_name, &handle->principal);
406ebfedea0SLionel Sambuc if (ret) {
407ebfedea0SLionel Sambuc HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
408ebfedea0SLionel Sambuc free(handle);
409ebfedea0SLionel Sambuc return ret;
410ebfedea0SLionel Sambuc }
411ebfedea0SLionel Sambuc }
412ebfedea0SLionel Sambuc if (cred_usage == GSS_C_INITIATE || cred_usage == GSS_C_BOTH) {
413ebfedea0SLionel Sambuc ret = acquire_initiator_cred(minor_status, context,
414*0a6a1f1dSLionel Sambuc credential_type, credential_data,
415ebfedea0SLionel Sambuc desired_name, time_req,
416*0a6a1f1dSLionel Sambuc desired_mech, cred_usage, handle);
417ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE) {
418ebfedea0SLionel Sambuc HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
419ebfedea0SLionel Sambuc krb5_free_principal(context, handle->principal);
420ebfedea0SLionel Sambuc free(handle);
421ebfedea0SLionel Sambuc return (ret);
422ebfedea0SLionel Sambuc }
423ebfedea0SLionel Sambuc }
424ebfedea0SLionel Sambuc if (cred_usage == GSS_C_ACCEPT || cred_usage == GSS_C_BOTH) {
425ebfedea0SLionel Sambuc ret = acquire_acceptor_cred(minor_status, context,
426*0a6a1f1dSLionel Sambuc credential_type, credential_data,
427ebfedea0SLionel Sambuc desired_name, time_req,
428*0a6a1f1dSLionel Sambuc desired_mech, cred_usage, handle);
429ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE) {
430ebfedea0SLionel Sambuc HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
431ebfedea0SLionel Sambuc krb5_free_principal(context, handle->principal);
432ebfedea0SLionel Sambuc free(handle);
433ebfedea0SLionel Sambuc return (ret);
434ebfedea0SLionel Sambuc }
435ebfedea0SLionel Sambuc }
436ebfedea0SLionel Sambuc ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
437ebfedea0SLionel Sambuc if (ret == GSS_S_COMPLETE)
438ebfedea0SLionel Sambuc ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
439ebfedea0SLionel Sambuc &handle->mechanisms);
440ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE) {
441ebfedea0SLionel Sambuc if (handle->mechanisms != NULL)
442ebfedea0SLionel Sambuc gss_release_oid_set(NULL, &handle->mechanisms);
443ebfedea0SLionel Sambuc HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
444ebfedea0SLionel Sambuc krb5_free_principal(context, handle->principal);
445ebfedea0SLionel Sambuc free(handle);
446ebfedea0SLionel Sambuc return (ret);
447ebfedea0SLionel Sambuc }
448ebfedea0SLionel Sambuc handle->usage = cred_usage;
449*0a6a1f1dSLionel Sambuc *minor_status = 0;
450ebfedea0SLionel Sambuc *output_cred_handle = (gss_cred_id_t)handle;
451ebfedea0SLionel Sambuc return (GSS_S_COMPLETE);
452ebfedea0SLionel Sambuc }
453