xref: /freebsd/crypto/heimdal/lib/gssapi/krb5/copy_ccache.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1c19800e8SDoug Rabson /*
2*ae771770SStanislav Sedov  * Copyright (c) 2000 - 2001, 2003 Kungliga Tekniska Högskolan
3c19800e8SDoug Rabson  * (Royal Institute of Technology, Stockholm, Sweden).
4c19800e8SDoug Rabson  * All rights reserved.
5c19800e8SDoug Rabson  *
6c19800e8SDoug Rabson  * Redistribution and use in source and binary forms, with or without
7c19800e8SDoug Rabson  * modification, are permitted provided that the following conditions
8c19800e8SDoug Rabson  * are met:
9c19800e8SDoug Rabson  *
10c19800e8SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
11c19800e8SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
12c19800e8SDoug Rabson  *
13c19800e8SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
14c19800e8SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
15c19800e8SDoug Rabson  *    documentation and/or other materials provided with the distribution.
16c19800e8SDoug Rabson  *
17c19800e8SDoug Rabson  * 3. Neither the name of the Institute nor the names of its contributors
18c19800e8SDoug Rabson  *    may be used to endorse or promote products derived from this software
19c19800e8SDoug Rabson  *    without specific prior written permission.
20c19800e8SDoug Rabson  *
21c19800e8SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22c19800e8SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23c19800e8SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24c19800e8SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25c19800e8SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26c19800e8SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27c19800e8SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28c19800e8SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29c19800e8SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30c19800e8SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31c19800e8SDoug Rabson  * SUCH DAMAGE.
32c19800e8SDoug Rabson  */
33c19800e8SDoug Rabson 
34*ae771770SStanislav Sedov #include "gsskrb5_locl.h"
35c19800e8SDoug Rabson 
36c19800e8SDoug Rabson #if 0
37c19800e8SDoug Rabson OM_uint32
38c19800e8SDoug Rabson gss_krb5_copy_ccache(OM_uint32 *minor_status,
39c19800e8SDoug Rabson 		     krb5_context context,
40c19800e8SDoug Rabson 		     gss_cred_id_t cred,
41c19800e8SDoug Rabson 		     krb5_ccache out)
42c19800e8SDoug Rabson {
43c19800e8SDoug Rabson     krb5_error_code kret;
44c19800e8SDoug Rabson 
45c19800e8SDoug Rabson     HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);
46c19800e8SDoug Rabson 
47c19800e8SDoug Rabson     if (cred->ccache == NULL) {
48c19800e8SDoug Rabson 	HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
49c19800e8SDoug Rabson 	*minor_status = EINVAL;
50c19800e8SDoug Rabson 	return GSS_S_FAILURE;
51c19800e8SDoug Rabson     }
52c19800e8SDoug Rabson 
53c19800e8SDoug Rabson     kret = krb5_cc_copy_cache(context, cred->ccache, out);
54c19800e8SDoug Rabson     HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
55c19800e8SDoug Rabson     if (kret) {
56c19800e8SDoug Rabson 	*minor_status = kret;
57c19800e8SDoug Rabson 	return GSS_S_FAILURE;
58c19800e8SDoug Rabson     }
59c19800e8SDoug Rabson     *minor_status = 0;
60c19800e8SDoug Rabson     return GSS_S_COMPLETE;
61c19800e8SDoug Rabson }
62c19800e8SDoug Rabson #endif
63c19800e8SDoug Rabson 
64c19800e8SDoug Rabson 
65c19800e8SDoug Rabson 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)66*ae771770SStanislav Sedov _gsskrb5_krb5_import_cred(OM_uint32 *minor_status,
67c19800e8SDoug Rabson 			  krb5_ccache id,
68c19800e8SDoug Rabson 			  krb5_principal keytab_principal,
69c19800e8SDoug Rabson 			  krb5_keytab keytab,
70c19800e8SDoug Rabson 			  gss_cred_id_t *cred)
71c19800e8SDoug Rabson {
72c19800e8SDoug Rabson     krb5_context context;
73c19800e8SDoug Rabson     krb5_error_code kret;
74c19800e8SDoug Rabson     gsskrb5_cred handle;
75c19800e8SDoug Rabson     OM_uint32 ret;
76c19800e8SDoug Rabson 
77c19800e8SDoug Rabson     *cred = NULL;
78c19800e8SDoug Rabson 
79c19800e8SDoug Rabson     GSSAPI_KRB5_INIT (&context);
80c19800e8SDoug Rabson 
81c19800e8SDoug Rabson     handle = calloc(1, sizeof(*handle));
82c19800e8SDoug Rabson     if (handle == NULL) {
83c19800e8SDoug Rabson 	_gsskrb5_clear_status ();
84c19800e8SDoug Rabson 	*minor_status = ENOMEM;
85c19800e8SDoug Rabson         return (GSS_S_FAILURE);
86c19800e8SDoug Rabson     }
87c19800e8SDoug Rabson     HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
88c19800e8SDoug Rabson 
89c19800e8SDoug Rabson     handle->usage = 0;
90c19800e8SDoug Rabson 
91c19800e8SDoug Rabson     if (id) {
92c19800e8SDoug Rabson 	char *str;
93c19800e8SDoug Rabson 
94c19800e8SDoug Rabson 	handle->usage |= GSS_C_INITIATE;
95c19800e8SDoug Rabson 
96c19800e8SDoug Rabson 	kret = krb5_cc_get_principal(context, id,
97c19800e8SDoug Rabson 				     &handle->principal);
98c19800e8SDoug Rabson 	if (kret) {
99c19800e8SDoug Rabson 	    free(handle);
100c19800e8SDoug Rabson 	    *minor_status = kret;
101c19800e8SDoug Rabson 	    return GSS_S_FAILURE;
102c19800e8SDoug Rabson 	}
103c19800e8SDoug Rabson 
104c19800e8SDoug Rabson 	if (keytab_principal) {
105c19800e8SDoug Rabson 	    krb5_boolean match;
106c19800e8SDoug Rabson 
107c19800e8SDoug Rabson 	    match = krb5_principal_compare(context,
108c19800e8SDoug Rabson 					   handle->principal,
109c19800e8SDoug Rabson 					   keytab_principal);
110c19800e8SDoug Rabson 	    if (match == FALSE) {
111c19800e8SDoug Rabson 		krb5_free_principal(context, handle->principal);
112c19800e8SDoug Rabson 		free(handle);
113c19800e8SDoug Rabson 		_gsskrb5_clear_status ();
114c19800e8SDoug Rabson 		*minor_status = EINVAL;
115c19800e8SDoug Rabson 		return GSS_S_FAILURE;
116c19800e8SDoug Rabson 	    }
117c19800e8SDoug Rabson 	}
118c19800e8SDoug Rabson 
119c19800e8SDoug Rabson 	ret = __gsskrb5_ccache_lifetime(minor_status,
120c19800e8SDoug Rabson 					context,
121c19800e8SDoug Rabson 					id,
122c19800e8SDoug Rabson 					handle->principal,
123c19800e8SDoug Rabson 					&handle->lifetime);
124c19800e8SDoug Rabson 	if (ret != GSS_S_COMPLETE) {
125c19800e8SDoug Rabson 	    krb5_free_principal(context, handle->principal);
126c19800e8SDoug Rabson 	    free(handle);
127c19800e8SDoug Rabson 	    return ret;
128c19800e8SDoug Rabson 	}
129c19800e8SDoug Rabson 
130c19800e8SDoug Rabson 
131c19800e8SDoug Rabson 	kret = krb5_cc_get_full_name(context, id, &str);
132c19800e8SDoug Rabson 	if (kret)
133c19800e8SDoug Rabson 	    goto out;
134c19800e8SDoug Rabson 
135c19800e8SDoug Rabson 	kret = krb5_cc_resolve(context, str, &handle->ccache);
136c19800e8SDoug Rabson 	free(str);
137c19800e8SDoug Rabson 	if (kret)
138c19800e8SDoug Rabson 	    goto out;
139c19800e8SDoug Rabson     }
140c19800e8SDoug Rabson 
141c19800e8SDoug Rabson 
142c19800e8SDoug Rabson     if (keytab) {
143c19800e8SDoug Rabson 	char *str;
144c19800e8SDoug Rabson 
145c19800e8SDoug Rabson 	handle->usage |= GSS_C_ACCEPT;
146c19800e8SDoug Rabson 
147c19800e8SDoug Rabson 	if (keytab_principal && handle->principal == NULL) {
148c19800e8SDoug Rabson 	    kret = krb5_copy_principal(context,
149c19800e8SDoug Rabson 				       keytab_principal,
150c19800e8SDoug Rabson 				       &handle->principal);
151c19800e8SDoug Rabson 	    if (kret)
152c19800e8SDoug Rabson 		goto out;
153c19800e8SDoug Rabson 	}
154c19800e8SDoug Rabson 
155c19800e8SDoug Rabson 	kret = krb5_kt_get_full_name(context, keytab, &str);
156c19800e8SDoug Rabson 	if (kret)
157c19800e8SDoug Rabson 	    goto out;
158c19800e8SDoug Rabson 
159c19800e8SDoug Rabson 	kret = krb5_kt_resolve(context, str, &handle->keytab);
160c19800e8SDoug Rabson 	free(str);
161c19800e8SDoug Rabson 	if (kret)
162c19800e8SDoug Rabson 	    goto out;
163c19800e8SDoug Rabson     }
164c19800e8SDoug Rabson 
165c19800e8SDoug Rabson 
166c19800e8SDoug Rabson     if (id || keytab) {
167c19800e8SDoug Rabson 	ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
168c19800e8SDoug Rabson 	if (ret == GSS_S_COMPLETE)
169c19800e8SDoug Rabson 	    ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
170c19800e8SDoug Rabson 					 &handle->mechanisms);
171c19800e8SDoug Rabson 	if (ret != GSS_S_COMPLETE) {
172c19800e8SDoug Rabson 	    kret = *minor_status;
173c19800e8SDoug Rabson 	    goto out;
174c19800e8SDoug Rabson 	}
175c19800e8SDoug Rabson     }
176c19800e8SDoug Rabson 
177c19800e8SDoug Rabson     *minor_status = 0;
178c19800e8SDoug Rabson     *cred = (gss_cred_id_t)handle;
179c19800e8SDoug Rabson     return GSS_S_COMPLETE;
180c19800e8SDoug Rabson 
181c19800e8SDoug Rabson out:
182c19800e8SDoug Rabson     gss_release_oid_set(minor_status, &handle->mechanisms);
183c19800e8SDoug Rabson     if (handle->ccache)
184c19800e8SDoug Rabson 	krb5_cc_close(context, handle->ccache);
185c19800e8SDoug Rabson     if (handle->keytab)
186c19800e8SDoug Rabson 	krb5_kt_close(context, handle->keytab);
187c19800e8SDoug Rabson     if (handle->principal)
188c19800e8SDoug Rabson 	krb5_free_principal(context, handle->principal);
189c19800e8SDoug Rabson     HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
190c19800e8SDoug Rabson     free(handle);
191c19800e8SDoug Rabson     *minor_status = kret;
192c19800e8SDoug Rabson     return GSS_S_FAILURE;
193c19800e8SDoug Rabson }
194