1*0a6a1f1dSLionel Sambuc /* $NetBSD: copy_ccache.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2000 - 2001, 2003 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 #if 0
39ebfedea0SLionel Sambuc OM_uint32
40ebfedea0SLionel Sambuc gss_krb5_copy_ccache(OM_uint32 *minor_status,
41ebfedea0SLionel Sambuc krb5_context context,
42ebfedea0SLionel Sambuc gss_cred_id_t cred,
43ebfedea0SLionel Sambuc krb5_ccache out)
44ebfedea0SLionel Sambuc {
45ebfedea0SLionel Sambuc krb5_error_code kret;
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);
48ebfedea0SLionel Sambuc
49ebfedea0SLionel Sambuc if (cred->ccache == NULL) {
50ebfedea0SLionel Sambuc HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
51ebfedea0SLionel Sambuc *minor_status = EINVAL;
52ebfedea0SLionel Sambuc return GSS_S_FAILURE;
53ebfedea0SLionel Sambuc }
54ebfedea0SLionel Sambuc
55ebfedea0SLionel Sambuc kret = krb5_cc_copy_cache(context, cred->ccache, out);
56ebfedea0SLionel Sambuc HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
57ebfedea0SLionel Sambuc if (kret) {
58ebfedea0SLionel Sambuc *minor_status = kret;
59ebfedea0SLionel Sambuc return GSS_S_FAILURE;
60ebfedea0SLionel Sambuc }
61ebfedea0SLionel Sambuc *minor_status = 0;
62ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
63ebfedea0SLionel Sambuc }
64ebfedea0SLionel Sambuc #endif
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc OM_uint32
_gsskrb5_krb5_import_cred(OM_uint32 * minor_status,krb5_ccache id,krb5_principal keytab_principal,krb5_keytab keytab,gss_cred_id_t * cred)68ebfedea0SLionel Sambuc _gsskrb5_krb5_import_cred(OM_uint32 *minor_status,
69ebfedea0SLionel Sambuc krb5_ccache id,
70ebfedea0SLionel Sambuc krb5_principal keytab_principal,
71ebfedea0SLionel Sambuc krb5_keytab keytab,
72ebfedea0SLionel Sambuc gss_cred_id_t *cred)
73ebfedea0SLionel Sambuc {
74ebfedea0SLionel Sambuc krb5_context context;
75ebfedea0SLionel Sambuc krb5_error_code kret;
76ebfedea0SLionel Sambuc gsskrb5_cred handle;
77ebfedea0SLionel Sambuc OM_uint32 ret;
78ebfedea0SLionel Sambuc
79ebfedea0SLionel Sambuc *cred = NULL;
80ebfedea0SLionel Sambuc
81ebfedea0SLionel Sambuc GSSAPI_KRB5_INIT (&context);
82ebfedea0SLionel Sambuc
83ebfedea0SLionel Sambuc handle = calloc(1, sizeof(*handle));
84ebfedea0SLionel Sambuc if (handle == NULL) {
85ebfedea0SLionel Sambuc _gsskrb5_clear_status ();
86ebfedea0SLionel Sambuc *minor_status = ENOMEM;
87ebfedea0SLionel Sambuc return (GSS_S_FAILURE);
88ebfedea0SLionel Sambuc }
89ebfedea0SLionel Sambuc HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
90ebfedea0SLionel Sambuc
91ebfedea0SLionel Sambuc handle->usage = 0;
92ebfedea0SLionel Sambuc
93ebfedea0SLionel Sambuc if (id) {
94ebfedea0SLionel Sambuc char *str;
95ebfedea0SLionel Sambuc
96ebfedea0SLionel Sambuc handle->usage |= GSS_C_INITIATE;
97ebfedea0SLionel Sambuc
98ebfedea0SLionel Sambuc kret = krb5_cc_get_principal(context, id,
99ebfedea0SLionel Sambuc &handle->principal);
100ebfedea0SLionel Sambuc if (kret) {
101ebfedea0SLionel Sambuc free(handle);
102ebfedea0SLionel Sambuc *minor_status = kret;
103ebfedea0SLionel Sambuc return GSS_S_FAILURE;
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc
106ebfedea0SLionel Sambuc if (keytab_principal) {
107ebfedea0SLionel Sambuc krb5_boolean match;
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc match = krb5_principal_compare(context,
110ebfedea0SLionel Sambuc handle->principal,
111ebfedea0SLionel Sambuc keytab_principal);
112ebfedea0SLionel Sambuc if (match == FALSE) {
113ebfedea0SLionel Sambuc krb5_free_principal(context, handle->principal);
114ebfedea0SLionel Sambuc free(handle);
115ebfedea0SLionel Sambuc _gsskrb5_clear_status ();
116ebfedea0SLionel Sambuc *minor_status = EINVAL;
117ebfedea0SLionel Sambuc return GSS_S_FAILURE;
118ebfedea0SLionel Sambuc }
119ebfedea0SLionel Sambuc }
120ebfedea0SLionel Sambuc
121ebfedea0SLionel Sambuc ret = __gsskrb5_ccache_lifetime(minor_status,
122ebfedea0SLionel Sambuc context,
123ebfedea0SLionel Sambuc id,
124ebfedea0SLionel Sambuc handle->principal,
125ebfedea0SLionel Sambuc &handle->lifetime);
126ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE) {
127ebfedea0SLionel Sambuc krb5_free_principal(context, handle->principal);
128ebfedea0SLionel Sambuc free(handle);
129ebfedea0SLionel Sambuc return ret;
130ebfedea0SLionel Sambuc }
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc
133ebfedea0SLionel Sambuc kret = krb5_cc_get_full_name(context, id, &str);
134ebfedea0SLionel Sambuc if (kret)
135ebfedea0SLionel Sambuc goto out;
136ebfedea0SLionel Sambuc
137ebfedea0SLionel Sambuc kret = krb5_cc_resolve(context, str, &handle->ccache);
138ebfedea0SLionel Sambuc free(str);
139ebfedea0SLionel Sambuc if (kret)
140ebfedea0SLionel Sambuc goto out;
141ebfedea0SLionel Sambuc }
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc
144ebfedea0SLionel Sambuc if (keytab) {
145ebfedea0SLionel Sambuc char *str;
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc handle->usage |= GSS_C_ACCEPT;
148ebfedea0SLionel Sambuc
149ebfedea0SLionel Sambuc if (keytab_principal && handle->principal == NULL) {
150ebfedea0SLionel Sambuc kret = krb5_copy_principal(context,
151ebfedea0SLionel Sambuc keytab_principal,
152ebfedea0SLionel Sambuc &handle->principal);
153ebfedea0SLionel Sambuc if (kret)
154ebfedea0SLionel Sambuc goto out;
155ebfedea0SLionel Sambuc }
156ebfedea0SLionel Sambuc
157ebfedea0SLionel Sambuc kret = krb5_kt_get_full_name(context, keytab, &str);
158ebfedea0SLionel Sambuc if (kret)
159ebfedea0SLionel Sambuc goto out;
160ebfedea0SLionel Sambuc
161ebfedea0SLionel Sambuc kret = krb5_kt_resolve(context, str, &handle->keytab);
162ebfedea0SLionel Sambuc free(str);
163ebfedea0SLionel Sambuc if (kret)
164ebfedea0SLionel Sambuc goto out;
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc
168ebfedea0SLionel Sambuc if (id || keytab) {
169ebfedea0SLionel Sambuc ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
170ebfedea0SLionel Sambuc if (ret == GSS_S_COMPLETE)
171ebfedea0SLionel Sambuc ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
172ebfedea0SLionel Sambuc &handle->mechanisms);
173ebfedea0SLionel Sambuc if (ret != GSS_S_COMPLETE) {
174ebfedea0SLionel Sambuc kret = *minor_status;
175ebfedea0SLionel Sambuc goto out;
176ebfedea0SLionel Sambuc }
177ebfedea0SLionel Sambuc }
178ebfedea0SLionel Sambuc
179ebfedea0SLionel Sambuc *minor_status = 0;
180ebfedea0SLionel Sambuc *cred = (gss_cred_id_t)handle;
181ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
182ebfedea0SLionel Sambuc
183ebfedea0SLionel Sambuc out:
184ebfedea0SLionel Sambuc gss_release_oid_set(minor_status, &handle->mechanisms);
185ebfedea0SLionel Sambuc if (handle->ccache)
186ebfedea0SLionel Sambuc krb5_cc_close(context, handle->ccache);
187ebfedea0SLionel Sambuc if (handle->keytab)
188ebfedea0SLionel Sambuc krb5_kt_close(context, handle->keytab);
189ebfedea0SLionel Sambuc if (handle->principal)
190ebfedea0SLionel Sambuc krb5_free_principal(context, handle->principal);
191ebfedea0SLionel Sambuc HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
192ebfedea0SLionel Sambuc free(handle);
193ebfedea0SLionel Sambuc *minor_status = kret;
194ebfedea0SLionel Sambuc return GSS_S_FAILURE;
195ebfedea0SLionel Sambuc }
196