xref: /minix3/crypto/external/bsd/heimdal/dist/kcm/glue.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: glue.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2005, PADL Software Pty Ltd.
5ebfedea0SLionel Sambuc  * All rights reserved.
6ebfedea0SLionel Sambuc  *
7ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
9ebfedea0SLionel Sambuc  * are met:
10ebfedea0SLionel Sambuc  *
11ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
16ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
17ebfedea0SLionel Sambuc  *
18ebfedea0SLionel Sambuc  * 3. Neither the name of PADL Software nor the names of its contributors
19ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
20ebfedea0SLionel Sambuc  *    without specific prior written permission.
21ebfedea0SLionel Sambuc  *
22ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ebfedea0SLionel Sambuc  * SUCH DAMAGE.
33ebfedea0SLionel Sambuc  */
34ebfedea0SLionel Sambuc 
35ebfedea0SLionel Sambuc #include "kcm_locl.h"
36ebfedea0SLionel Sambuc 
37*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
38ebfedea0SLionel Sambuc 
39ebfedea0SLionel Sambuc /*
40ebfedea0SLionel Sambuc  * Server-side loopback glue for credentials cache operations; this
41ebfedea0SLionel Sambuc  * must be initialized with kcm_internal_ccache(), it is not for real
42ebfedea0SLionel Sambuc  * use. This entire file assumes the cache is locked, it does not do
43ebfedea0SLionel Sambuc  * any concurrency checking for multithread applications.
44ebfedea0SLionel Sambuc  */
45ebfedea0SLionel Sambuc 
46ebfedea0SLionel Sambuc #define KCMCACHE(X)	((kcm_ccache)(X)->data.data)
47ebfedea0SLionel Sambuc #define CACHENAME(X)	(KCMCACHE(X)->name)
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc static const char *
kcmss_get_name(krb5_context context,krb5_ccache id)50ebfedea0SLionel Sambuc kcmss_get_name(krb5_context context,
51ebfedea0SLionel Sambuc 	       krb5_ccache id)
52ebfedea0SLionel Sambuc {
53ebfedea0SLionel Sambuc     return CACHENAME(id);
54ebfedea0SLionel Sambuc }
55ebfedea0SLionel Sambuc 
56ebfedea0SLionel Sambuc static krb5_error_code
kcmss_resolve(krb5_context context,krb5_ccache * id,const char * res)57ebfedea0SLionel Sambuc kcmss_resolve(krb5_context context, krb5_ccache *id, const char *res)
58ebfedea0SLionel Sambuc {
59ebfedea0SLionel Sambuc     return KRB5_FCC_INTERNAL;
60ebfedea0SLionel Sambuc }
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc static krb5_error_code
kcmss_gen_new(krb5_context context,krb5_ccache * id)63ebfedea0SLionel Sambuc kcmss_gen_new(krb5_context context, krb5_ccache *id)
64ebfedea0SLionel Sambuc {
65ebfedea0SLionel Sambuc     return KRB5_FCC_INTERNAL;
66ebfedea0SLionel Sambuc }
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc static krb5_error_code
kcmss_initialize(krb5_context context,krb5_ccache id,krb5_principal primary_principal)69ebfedea0SLionel Sambuc kcmss_initialize(krb5_context context,
70ebfedea0SLionel Sambuc 		 krb5_ccache id,
71ebfedea0SLionel Sambuc 		 krb5_principal primary_principal)
72ebfedea0SLionel Sambuc {
73ebfedea0SLionel Sambuc     krb5_error_code ret;
74ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
77ebfedea0SLionel Sambuc 
78ebfedea0SLionel Sambuc     ret = kcm_zero_ccache_data_internal(context, c);
79ebfedea0SLionel Sambuc     if (ret)
80ebfedea0SLionel Sambuc 	return ret;
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc     ret = krb5_copy_principal(context, primary_principal,
83ebfedea0SLionel Sambuc 			      &c->client);
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc     return ret;
86ebfedea0SLionel Sambuc }
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc static krb5_error_code
kcmss_close(krb5_context context,krb5_ccache id)89ebfedea0SLionel Sambuc kcmss_close(krb5_context context,
90ebfedea0SLionel Sambuc 	    krb5_ccache id)
91ebfedea0SLionel Sambuc {
92ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc     id->data.data = NULL;
97ebfedea0SLionel Sambuc     id->data.length = 0;
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc     return 0;
100ebfedea0SLionel Sambuc }
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc static krb5_error_code
kcmss_destroy(krb5_context context,krb5_ccache id)103ebfedea0SLionel Sambuc kcmss_destroy(krb5_context context,
104ebfedea0SLionel Sambuc 	      krb5_ccache id)
105ebfedea0SLionel Sambuc {
106ebfedea0SLionel Sambuc     krb5_error_code ret;
107ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     ret = kcm_ccache_destroy(context, CACHENAME(id));
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc     return ret;
114ebfedea0SLionel Sambuc }
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc static krb5_error_code
kcmss_store_cred(krb5_context context,krb5_ccache id,krb5_creds * creds)117ebfedea0SLionel Sambuc kcmss_store_cred(krb5_context context,
118ebfedea0SLionel Sambuc 		 krb5_ccache id,
119ebfedea0SLionel Sambuc 		 krb5_creds *creds)
120ebfedea0SLionel Sambuc {
121ebfedea0SLionel Sambuc     krb5_error_code ret;
122ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
123ebfedea0SLionel Sambuc     krb5_creds *tmp;
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc     ret = kcm_ccache_store_cred_internal(context, c, creds, 1, &tmp);
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc     return ret;
130ebfedea0SLionel Sambuc }
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc static krb5_error_code
kcmss_retrieve(krb5_context context,krb5_ccache id,krb5_flags which,const krb5_creds * mcred,krb5_creds * creds)133ebfedea0SLionel Sambuc kcmss_retrieve(krb5_context context,
134ebfedea0SLionel Sambuc 	       krb5_ccache id,
135ebfedea0SLionel Sambuc 	       krb5_flags which,
136ebfedea0SLionel Sambuc 	       const krb5_creds *mcred,
137ebfedea0SLionel Sambuc 	       krb5_creds *creds)
138ebfedea0SLionel Sambuc {
139ebfedea0SLionel Sambuc     krb5_error_code ret;
140ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
141ebfedea0SLionel Sambuc     krb5_creds *credp;
142ebfedea0SLionel Sambuc 
143ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc     ret = kcm_ccache_retrieve_cred_internal(context, c, which,
146ebfedea0SLionel Sambuc 					    mcred, &credp);
147ebfedea0SLionel Sambuc     if (ret)
148ebfedea0SLionel Sambuc 	return ret;
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc     ret = krb5_copy_creds_contents(context, credp, creds);
151ebfedea0SLionel Sambuc     if (ret)
152ebfedea0SLionel Sambuc 	return ret;
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc     return 0;
155ebfedea0SLionel Sambuc }
156ebfedea0SLionel Sambuc 
157ebfedea0SLionel Sambuc static krb5_error_code
kcmss_get_principal(krb5_context context,krb5_ccache id,krb5_principal * principal)158ebfedea0SLionel Sambuc kcmss_get_principal(krb5_context context,
159ebfedea0SLionel Sambuc 		    krb5_ccache id,
160ebfedea0SLionel Sambuc 		    krb5_principal *principal)
161ebfedea0SLionel Sambuc {
162ebfedea0SLionel Sambuc     krb5_error_code ret;
163ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
164ebfedea0SLionel Sambuc 
165ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc     ret = krb5_copy_principal(context, c->client,
168ebfedea0SLionel Sambuc 			      principal);
169ebfedea0SLionel Sambuc 
170ebfedea0SLionel Sambuc     return ret;
171ebfedea0SLionel Sambuc }
172ebfedea0SLionel Sambuc 
173ebfedea0SLionel Sambuc static krb5_error_code
kcmss_get_first(krb5_context context,krb5_ccache id,krb5_cc_cursor * cursor)174ebfedea0SLionel Sambuc kcmss_get_first (krb5_context context,
175ebfedea0SLionel Sambuc 		 krb5_ccache id,
176ebfedea0SLionel Sambuc 		 krb5_cc_cursor *cursor)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
181ebfedea0SLionel Sambuc 
182ebfedea0SLionel Sambuc     *cursor = c->creds;
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc     return (*cursor == NULL) ? KRB5_CC_END : 0;
185ebfedea0SLionel Sambuc }
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc static krb5_error_code
kcmss_get_next(krb5_context context,krb5_ccache id,krb5_cc_cursor * cursor,krb5_creds * creds)188ebfedea0SLionel Sambuc kcmss_get_next (krb5_context context,
189ebfedea0SLionel Sambuc 		krb5_ccache id,
190ebfedea0SLionel Sambuc 		krb5_cc_cursor *cursor,
191ebfedea0SLionel Sambuc 		krb5_creds *creds)
192ebfedea0SLionel Sambuc {
193ebfedea0SLionel Sambuc     krb5_error_code ret;
194ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
197ebfedea0SLionel Sambuc 
198ebfedea0SLionel Sambuc     ret = krb5_copy_creds_contents(context,
199ebfedea0SLionel Sambuc 				   &((struct kcm_creds *)cursor)->cred,
200ebfedea0SLionel Sambuc 				   creds);
201ebfedea0SLionel Sambuc     if (ret)
202ebfedea0SLionel Sambuc 	return ret;
203ebfedea0SLionel Sambuc 
204ebfedea0SLionel Sambuc     *cursor = ((struct kcm_creds *)cursor)->next;
205ebfedea0SLionel Sambuc     if (*cursor == 0)
206ebfedea0SLionel Sambuc 	ret = KRB5_CC_END;
207ebfedea0SLionel Sambuc 
208ebfedea0SLionel Sambuc     return ret;
209ebfedea0SLionel Sambuc }
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc static krb5_error_code
kcmss_end_get(krb5_context context,krb5_ccache id,krb5_cc_cursor * cursor)212ebfedea0SLionel Sambuc kcmss_end_get (krb5_context context,
213ebfedea0SLionel Sambuc 	       krb5_ccache id,
214ebfedea0SLionel Sambuc 	       krb5_cc_cursor *cursor)
215ebfedea0SLionel Sambuc {
216ebfedea0SLionel Sambuc     *cursor = NULL;
217ebfedea0SLionel Sambuc     return 0;
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc 
220ebfedea0SLionel Sambuc static krb5_error_code
kcmss_remove_cred(krb5_context context,krb5_ccache id,krb5_flags which,krb5_creds * cred)221ebfedea0SLionel Sambuc kcmss_remove_cred(krb5_context context,
222ebfedea0SLionel Sambuc 		  krb5_ccache id,
223ebfedea0SLionel Sambuc 		  krb5_flags which,
224ebfedea0SLionel Sambuc 		  krb5_creds *cred)
225ebfedea0SLionel Sambuc {
226ebfedea0SLionel Sambuc     krb5_error_code ret;
227ebfedea0SLionel Sambuc     kcm_ccache c = KCMCACHE(id);
228ebfedea0SLionel Sambuc 
229ebfedea0SLionel Sambuc     KCM_ASSERT_VALID(c);
230ebfedea0SLionel Sambuc 
231ebfedea0SLionel Sambuc     ret = kcm_ccache_remove_cred_internal(context, c, which, cred);
232ebfedea0SLionel Sambuc 
233ebfedea0SLionel Sambuc     return ret;
234ebfedea0SLionel Sambuc }
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc static krb5_error_code
kcmss_set_flags(krb5_context context,krb5_ccache id,krb5_flags flags)237ebfedea0SLionel Sambuc kcmss_set_flags(krb5_context context,
238ebfedea0SLionel Sambuc 		krb5_ccache id,
239ebfedea0SLionel Sambuc 		krb5_flags flags)
240ebfedea0SLionel Sambuc {
241ebfedea0SLionel Sambuc     return 0;
242ebfedea0SLionel Sambuc }
243ebfedea0SLionel Sambuc 
244ebfedea0SLionel Sambuc static krb5_error_code
kcmss_get_version(krb5_context context,krb5_ccache id)245ebfedea0SLionel Sambuc kcmss_get_version(krb5_context context,
246ebfedea0SLionel Sambuc 		  krb5_ccache id)
247ebfedea0SLionel Sambuc {
248ebfedea0SLionel Sambuc     return 0;
249ebfedea0SLionel Sambuc }
250ebfedea0SLionel Sambuc 
251ebfedea0SLionel Sambuc static const krb5_cc_ops krb5_kcmss_ops = {
252ebfedea0SLionel Sambuc     KRB5_CC_OPS_VERSION,
253ebfedea0SLionel Sambuc     "KCM",
254ebfedea0SLionel Sambuc     kcmss_get_name,
255ebfedea0SLionel Sambuc     kcmss_resolve,
256ebfedea0SLionel Sambuc     kcmss_gen_new,
257ebfedea0SLionel Sambuc     kcmss_initialize,
258ebfedea0SLionel Sambuc     kcmss_destroy,
259ebfedea0SLionel Sambuc     kcmss_close,
260ebfedea0SLionel Sambuc     kcmss_store_cred,
261ebfedea0SLionel Sambuc     kcmss_retrieve,
262ebfedea0SLionel Sambuc     kcmss_get_principal,
263ebfedea0SLionel Sambuc     kcmss_get_first,
264ebfedea0SLionel Sambuc     kcmss_get_next,
265ebfedea0SLionel Sambuc     kcmss_end_get,
266ebfedea0SLionel Sambuc     kcmss_remove_cred,
267ebfedea0SLionel Sambuc     kcmss_set_flags,
268ebfedea0SLionel Sambuc     kcmss_get_version
269ebfedea0SLionel Sambuc };
270ebfedea0SLionel Sambuc 
271ebfedea0SLionel Sambuc krb5_error_code
kcm_internal_ccache(krb5_context context,kcm_ccache c,krb5_ccache id)272ebfedea0SLionel Sambuc kcm_internal_ccache(krb5_context context,
273ebfedea0SLionel Sambuc 		    kcm_ccache c,
274ebfedea0SLionel Sambuc 		    krb5_ccache id)
275ebfedea0SLionel Sambuc {
276ebfedea0SLionel Sambuc     id->ops = &krb5_kcmss_ops;
277ebfedea0SLionel Sambuc     id->data.length = sizeof(*c);
278ebfedea0SLionel Sambuc     id->data.data = c;
279ebfedea0SLionel Sambuc 
280ebfedea0SLionel Sambuc     return 0;
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc 
283