xref: /freebsd/crypto/krb5/src/ccapi/server/ccs_cache_collection.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* ccapi/server/ccs_cache_collection.c */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright 2006, 2007 Massachusetts Institute of Technology.
4*7f2fe78bSCy Schubert  * All Rights Reserved.
5*7f2fe78bSCy Schubert  *
6*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
7*7f2fe78bSCy Schubert  * require a specific license from the United States Government.
8*7f2fe78bSCy Schubert  * It is the responsibility of any person or organization contemplating
9*7f2fe78bSCy Schubert  * export to obtain such a license before exporting.
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
13*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
14*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
15*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
16*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
17*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
18*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
19*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
20*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
21*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
22*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
23*7f2fe78bSCy Schubert  * or implied warranty.
24*7f2fe78bSCy Schubert  */
25*7f2fe78bSCy Schubert 
26*7f2fe78bSCy Schubert #include "k5-platform.h"	/* pull in asprintf decl/defn */
27*7f2fe78bSCy Schubert #include "ccs_common.h"
28*7f2fe78bSCy Schubert #include "ccs_os_notify.h"
29*7f2fe78bSCy Schubert 
30*7f2fe78bSCy Schubert struct ccs_cache_collection_d {
31*7f2fe78bSCy Schubert     cc_time_t last_changed_time;
32*7f2fe78bSCy Schubert     cc_uint64 next_unique_name;
33*7f2fe78bSCy Schubert     cci_identifier_t identifier;
34*7f2fe78bSCy Schubert     ccs_lock_state_t lock_state;
35*7f2fe78bSCy Schubert     ccs_ccache_list_t ccaches;
36*7f2fe78bSCy Schubert     ccs_callback_array_t change_callbacks;
37*7f2fe78bSCy Schubert };
38*7f2fe78bSCy Schubert 
39*7f2fe78bSCy Schubert struct ccs_cache_collection_d ccs_cache_collection_initializer = { 0, 0, NULL, NULL, NULL, NULL };
40*7f2fe78bSCy Schubert 
41*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
42*7f2fe78bSCy Schubert 
ccs_cache_collection_new(ccs_cache_collection_t * out_cache_collection)43*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_new (ccs_cache_collection_t *out_cache_collection)
44*7f2fe78bSCy Schubert {
45*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
46*7f2fe78bSCy Schubert     ccs_cache_collection_t cache_collection = NULL;
47*7f2fe78bSCy Schubert 
48*7f2fe78bSCy Schubert     if (!out_cache_collection) { err = cci_check_error (ccErrBadParam); }
49*7f2fe78bSCy Schubert 
50*7f2fe78bSCy Schubert     if (!err) {
51*7f2fe78bSCy Schubert         cache_collection = malloc (sizeof (*cache_collection));
52*7f2fe78bSCy Schubert         if (cache_collection) {
53*7f2fe78bSCy Schubert             *cache_collection = ccs_cache_collection_initializer;
54*7f2fe78bSCy Schubert         } else {
55*7f2fe78bSCy Schubert             err = cci_check_error (ccErrNoMem);
56*7f2fe78bSCy Schubert         }
57*7f2fe78bSCy Schubert     }
58*7f2fe78bSCy Schubert 
59*7f2fe78bSCy Schubert     if (!err) {
60*7f2fe78bSCy Schubert         err = ccs_server_new_identifier (&cache_collection->identifier);
61*7f2fe78bSCy Schubert     }
62*7f2fe78bSCy Schubert 
63*7f2fe78bSCy Schubert     if (!err) {
64*7f2fe78bSCy Schubert         err = ccs_lock_state_new (&cache_collection->lock_state,
65*7f2fe78bSCy Schubert                                   ccErrInvalidContext,
66*7f2fe78bSCy Schubert                                   ccErrContextLocked,
67*7f2fe78bSCy Schubert                                   ccErrContextUnlocked);
68*7f2fe78bSCy Schubert     }
69*7f2fe78bSCy Schubert 
70*7f2fe78bSCy Schubert     if (!err) {
71*7f2fe78bSCy Schubert         err = ccs_ccache_list_new (&cache_collection->ccaches);
72*7f2fe78bSCy Schubert     }
73*7f2fe78bSCy Schubert 
74*7f2fe78bSCy Schubert     if (!err) {
75*7f2fe78bSCy Schubert         err = ccs_callback_array_new (&cache_collection->change_callbacks);
76*7f2fe78bSCy Schubert     }
77*7f2fe78bSCy Schubert 
78*7f2fe78bSCy Schubert     if (!err) {
79*7f2fe78bSCy Schubert         err = ccs_cache_collection_changed (cache_collection);
80*7f2fe78bSCy Schubert     }
81*7f2fe78bSCy Schubert 
82*7f2fe78bSCy Schubert     if (!err) {
83*7f2fe78bSCy Schubert         *out_cache_collection = cache_collection;
84*7f2fe78bSCy Schubert         cache_collection = NULL;
85*7f2fe78bSCy Schubert     }
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert     ccs_cache_collection_release (cache_collection);
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert     return cci_check_error (err);
90*7f2fe78bSCy Schubert }
91*7f2fe78bSCy Schubert 
92*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
93*7f2fe78bSCy Schubert 
ccs_cache_collection_release(ccs_cache_collection_t io_cache_collection)94*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_release (ccs_cache_collection_t io_cache_collection)
95*7f2fe78bSCy Schubert {
96*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
97*7f2fe78bSCy Schubert 
98*7f2fe78bSCy Schubert     if (!err && io_cache_collection) {
99*7f2fe78bSCy Schubert         cci_identifier_release (io_cache_collection->identifier);
100*7f2fe78bSCy Schubert         ccs_lock_state_release (io_cache_collection->lock_state);
101*7f2fe78bSCy Schubert         ccs_ccache_list_release (io_cache_collection->ccaches);
102*7f2fe78bSCy Schubert         ccs_callback_array_release (io_cache_collection->change_callbacks);
103*7f2fe78bSCy Schubert         free (io_cache_collection);
104*7f2fe78bSCy Schubert     }
105*7f2fe78bSCy Schubert 
106*7f2fe78bSCy Schubert     return cci_check_error (err);
107*7f2fe78bSCy Schubert }
108*7f2fe78bSCy Schubert 
109*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
110*7f2fe78bSCy Schubert 
ccs_cache_collection_compare_identifier(ccs_cache_collection_t in_cache_collection,cci_identifier_t in_identifier,cc_uint32 * out_equal)111*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_compare_identifier (ccs_cache_collection_t  in_cache_collection,
112*7f2fe78bSCy Schubert                                                   cci_identifier_t        in_identifier,
113*7f2fe78bSCy Schubert                                                   cc_uint32              *out_equal)
114*7f2fe78bSCy Schubert {
115*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
116*7f2fe78bSCy Schubert 
117*7f2fe78bSCy Schubert     if (!in_cache_collection) { err = cci_check_error (ccErrBadParam); }
118*7f2fe78bSCy Schubert     if (!in_identifier      ) { err = cci_check_error (ccErrBadParam); }
119*7f2fe78bSCy Schubert     if (!out_equal          ) { err = cci_check_error (ccErrBadParam); }
120*7f2fe78bSCy Schubert 
121*7f2fe78bSCy Schubert     if (!err) {
122*7f2fe78bSCy Schubert         err = cci_identifier_compare (in_cache_collection->identifier,
123*7f2fe78bSCy Schubert                                       in_identifier,
124*7f2fe78bSCy Schubert                                       out_equal);
125*7f2fe78bSCy Schubert     }
126*7f2fe78bSCy Schubert 
127*7f2fe78bSCy Schubert     return cci_check_error (err);
128*7f2fe78bSCy Schubert }
129*7f2fe78bSCy Schubert 
130*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
131*7f2fe78bSCy Schubert #pragma mark -
132*7f2fe78bSCy Schubert #endif
133*7f2fe78bSCy Schubert 
134*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
135*7f2fe78bSCy Schubert 
ccs_cache_collection_changed(ccs_cache_collection_t io_cache_collection)136*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_changed (ccs_cache_collection_t io_cache_collection)
137*7f2fe78bSCy Schubert {
138*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
139*7f2fe78bSCy Schubert     k5_ipc_stream reply_data = NULL;
140*7f2fe78bSCy Schubert 
141*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
142*7f2fe78bSCy Schubert 
143*7f2fe78bSCy Schubert     if (!err) {
144*7f2fe78bSCy Schubert         cc_time_t now = time (NULL);
145*7f2fe78bSCy Schubert 
146*7f2fe78bSCy Schubert         if (io_cache_collection->last_changed_time < now) {
147*7f2fe78bSCy Schubert             io_cache_collection->last_changed_time = now;
148*7f2fe78bSCy Schubert         } else {
149*7f2fe78bSCy Schubert             io_cache_collection->last_changed_time++;
150*7f2fe78bSCy Schubert         }
151*7f2fe78bSCy Schubert     }
152*7f2fe78bSCy Schubert 
153*7f2fe78bSCy Schubert     if (!err) {
154*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_new (&reply_data);
155*7f2fe78bSCy Schubert     }
156*7f2fe78bSCy Schubert 
157*7f2fe78bSCy Schubert     if (!err) {
158*7f2fe78bSCy Schubert 	err = krb5int_ipc_stream_write_time (reply_data, io_cache_collection->last_changed_time);
159*7f2fe78bSCy Schubert     }
160*7f2fe78bSCy Schubert 
161*7f2fe78bSCy Schubert     if (!err) {
162*7f2fe78bSCy Schubert 	/* Loop over callbacks sending messages to them */
163*7f2fe78bSCy Schubert 	cc_uint64 i;
164*7f2fe78bSCy Schubert         cc_uint64 count = ccs_callback_array_count (io_cache_collection->change_callbacks);
165*7f2fe78bSCy Schubert 
166*7f2fe78bSCy Schubert         for (i = 0; !err && i < count; i++) {
167*7f2fe78bSCy Schubert             ccs_callback_t callback = ccs_callback_array_object_at_index (io_cache_collection->change_callbacks, i);
168*7f2fe78bSCy Schubert 
169*7f2fe78bSCy Schubert 	    err = ccs_callback_reply_to_client (callback, reply_data);
170*7f2fe78bSCy Schubert 
171*7f2fe78bSCy Schubert 	    if (!err) {
172*7f2fe78bSCy Schubert 		cci_debug_printf ("%s: Removing callback reference %p.", __FUNCTION__, callback);
173*7f2fe78bSCy Schubert 		err = ccs_callback_array_remove (io_cache_collection->change_callbacks, i);
174*7f2fe78bSCy Schubert 		break;
175*7f2fe78bSCy Schubert 	    }
176*7f2fe78bSCy Schubert         }
177*7f2fe78bSCy Schubert     }
178*7f2fe78bSCy Schubert 
179*7f2fe78bSCy Schubert     if (!err) {
180*7f2fe78bSCy Schubert         err = ccs_os_notify_cache_collection_changed (io_cache_collection);
181*7f2fe78bSCy Schubert     }
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     krb5int_ipc_stream_release (reply_data);
184*7f2fe78bSCy Schubert 
185*7f2fe78bSCy Schubert     return cci_check_error (err);
186*7f2fe78bSCy Schubert }
187*7f2fe78bSCy Schubert 
188*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
189*7f2fe78bSCy Schubert 
ccs_cache_collection_invalidate_change_callback(ccs_callback_owner_t io_cache_collection,ccs_callback_t in_callback)190*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_invalidate_change_callback (ccs_callback_owner_t io_cache_collection,
191*7f2fe78bSCy Schubert 								 ccs_callback_t       in_callback)
192*7f2fe78bSCy Schubert {
193*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
194*7f2fe78bSCy Schubert 
195*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
196*7f2fe78bSCy Schubert     if (!in_callback        ) { err = cci_check_error (ccErrBadParam); }
197*7f2fe78bSCy Schubert 
198*7f2fe78bSCy Schubert     if (!err) {
199*7f2fe78bSCy Schubert 	/* Remove callback */
200*7f2fe78bSCy Schubert 	ccs_cache_collection_t cache_collection = (ccs_cache_collection_t) io_cache_collection;
201*7f2fe78bSCy Schubert 	cc_uint64 i;
202*7f2fe78bSCy Schubert         cc_uint64 count = ccs_callback_array_count (cache_collection->change_callbacks);
203*7f2fe78bSCy Schubert 
204*7f2fe78bSCy Schubert         for (i = 0; !err && i < count; i++) {
205*7f2fe78bSCy Schubert             ccs_callback_t callback = ccs_callback_array_object_at_index (cache_collection->change_callbacks, i);
206*7f2fe78bSCy Schubert 
207*7f2fe78bSCy Schubert 	    if (callback == in_callback) {
208*7f2fe78bSCy Schubert 		cci_debug_printf ("%s: Removing callback reference %p.", __FUNCTION__, callback);
209*7f2fe78bSCy Schubert 		err = ccs_callback_array_remove (cache_collection->change_callbacks, i);
210*7f2fe78bSCy Schubert 		break;
211*7f2fe78bSCy Schubert 	    }
212*7f2fe78bSCy Schubert         }
213*7f2fe78bSCy Schubert     }
214*7f2fe78bSCy Schubert 
215*7f2fe78bSCy Schubert     return cci_check_error (err);
216*7f2fe78bSCy Schubert }
217*7f2fe78bSCy Schubert 
218*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
219*7f2fe78bSCy Schubert #pragma mark -
220*7f2fe78bSCy Schubert #endif
221*7f2fe78bSCy Schubert 
222*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
223*7f2fe78bSCy Schubert 
ccs_cache_collection_find_ccache_by_name(ccs_cache_collection_t in_cache_collection,const char * in_name,ccs_ccache_t * out_ccache)224*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_find_ccache_by_name (ccs_cache_collection_t  in_cache_collection,
225*7f2fe78bSCy Schubert                                                           const char             *in_name,
226*7f2fe78bSCy Schubert                                                           ccs_ccache_t           *out_ccache)
227*7f2fe78bSCy Schubert {
228*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
229*7f2fe78bSCy Schubert     ccs_ccache_list_iterator_t iterator = NULL;
230*7f2fe78bSCy Schubert 
231*7f2fe78bSCy Schubert     if (!in_cache_collection) { err = cci_check_error (ccErrBadParam); }
232*7f2fe78bSCy Schubert     if (!in_name            ) { err = cci_check_error (ccErrBadParam); }
233*7f2fe78bSCy Schubert     if (!out_ccache         ) { err = cci_check_error (ccErrBadParam); }
234*7f2fe78bSCy Schubert 
235*7f2fe78bSCy Schubert     if (!err) {
236*7f2fe78bSCy Schubert         err = ccs_ccache_list_new_iterator (in_cache_collection->ccaches,
237*7f2fe78bSCy Schubert                                             CCS_PIPE_NULL,
238*7f2fe78bSCy Schubert                                             &iterator);
239*7f2fe78bSCy Schubert     }
240*7f2fe78bSCy Schubert 
241*7f2fe78bSCy Schubert     while (!err) {
242*7f2fe78bSCy Schubert         ccs_ccache_t ccache = NULL;
243*7f2fe78bSCy Schubert 
244*7f2fe78bSCy Schubert         err = ccs_ccache_list_iterator_next (iterator, &ccache);
245*7f2fe78bSCy Schubert 
246*7f2fe78bSCy Schubert         if (!err) {
247*7f2fe78bSCy Schubert             cc_uint32 equal = 0;
248*7f2fe78bSCy Schubert 
249*7f2fe78bSCy Schubert             err = ccs_ccache_compare_name (ccache, in_name, &equal);
250*7f2fe78bSCy Schubert 
251*7f2fe78bSCy Schubert             if (!err && equal) {
252*7f2fe78bSCy Schubert                 *out_ccache = ccache;
253*7f2fe78bSCy Schubert                 break;
254*7f2fe78bSCy Schubert             }
255*7f2fe78bSCy Schubert         }
256*7f2fe78bSCy Schubert     }
257*7f2fe78bSCy Schubert     if (err == ccIteratorEnd) { err = ccErrCCacheNotFound; }
258*7f2fe78bSCy Schubert 
259*7f2fe78bSCy Schubert     if (iterator) { ccs_ccache_list_iterator_release (iterator); }
260*7f2fe78bSCy Schubert 
261*7f2fe78bSCy Schubert     return cci_check_error (err);
262*7f2fe78bSCy Schubert }
263*7f2fe78bSCy Schubert 
264*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
265*7f2fe78bSCy Schubert #pragma mark -
266*7f2fe78bSCy Schubert #endif
267*7f2fe78bSCy Schubert 
268*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
269*7f2fe78bSCy Schubert 
ccs_cache_collection_find_ccache(ccs_cache_collection_t in_cache_collection,cci_identifier_t in_identifier,ccs_ccache_t * out_ccache)270*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_find_ccache (ccs_cache_collection_t  in_cache_collection,
271*7f2fe78bSCy Schubert                                            cci_identifier_t        in_identifier,
272*7f2fe78bSCy Schubert                                            ccs_ccache_t           *out_ccache)
273*7f2fe78bSCy Schubert {
274*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
275*7f2fe78bSCy Schubert 
276*7f2fe78bSCy Schubert     if (!in_cache_collection) { err = cci_check_error (ccErrBadParam); }
277*7f2fe78bSCy Schubert     if (!in_identifier      ) { err = cci_check_error (ccErrBadParam); }
278*7f2fe78bSCy Schubert     if (!out_ccache         ) { err = cci_check_error (ccErrBadParam); }
279*7f2fe78bSCy Schubert 
280*7f2fe78bSCy Schubert     if (!err) {
281*7f2fe78bSCy Schubert         err = ccs_ccache_list_find (in_cache_collection->ccaches,
282*7f2fe78bSCy Schubert                                     in_identifier, out_ccache);
283*7f2fe78bSCy Schubert     }
284*7f2fe78bSCy Schubert 
285*7f2fe78bSCy Schubert     return cci_check_error (err);
286*7f2fe78bSCy Schubert }
287*7f2fe78bSCy Schubert 
288*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
289*7f2fe78bSCy Schubert 
ccs_ccache_collection_move_ccache(ccs_cache_collection_t io_cache_collection,cci_identifier_t in_source_identifier,ccs_ccache_t io_destination_ccache)290*7f2fe78bSCy Schubert cc_int32 ccs_ccache_collection_move_ccache (ccs_cache_collection_t io_cache_collection,
291*7f2fe78bSCy Schubert                                             cci_identifier_t       in_source_identifier,
292*7f2fe78bSCy Schubert                                             ccs_ccache_t           io_destination_ccache)
293*7f2fe78bSCy Schubert {
294*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
295*7f2fe78bSCy Schubert     ccs_ccache_t source_ccache = NULL;
296*7f2fe78bSCy Schubert 
297*7f2fe78bSCy Schubert     if (!io_cache_collection  ) { err = cci_check_error (ccErrBadParam); }
298*7f2fe78bSCy Schubert     if (!in_source_identifier ) { err = cci_check_error (ccErrBadParam); }
299*7f2fe78bSCy Schubert     if (!io_destination_ccache) { err = cci_check_error (ccErrBadParam); }
300*7f2fe78bSCy Schubert 
301*7f2fe78bSCy Schubert     if (!err) {
302*7f2fe78bSCy Schubert         err = ccs_cache_collection_find_ccache (io_cache_collection,
303*7f2fe78bSCy Schubert                                                 in_source_identifier,
304*7f2fe78bSCy Schubert                                                 &source_ccache);
305*7f2fe78bSCy Schubert     }
306*7f2fe78bSCy Schubert 
307*7f2fe78bSCy Schubert     if (!err) {
308*7f2fe78bSCy Schubert         err = ccs_ccache_swap_contents (source_ccache,
309*7f2fe78bSCy Schubert 					io_destination_ccache,
310*7f2fe78bSCy Schubert 					io_cache_collection);
311*7f2fe78bSCy Schubert     }
312*7f2fe78bSCy Schubert 
313*7f2fe78bSCy Schubert     if (!err) {
314*7f2fe78bSCy Schubert         err = ccs_cache_collection_destroy_ccache (io_cache_collection,
315*7f2fe78bSCy Schubert                                                    in_source_identifier);
316*7f2fe78bSCy Schubert     }
317*7f2fe78bSCy Schubert 
318*7f2fe78bSCy Schubert     return cci_check_error (err);
319*7f2fe78bSCy Schubert }
320*7f2fe78bSCy Schubert 
321*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
322*7f2fe78bSCy Schubert 
ccs_cache_collection_destroy_ccache(ccs_cache_collection_t io_cache_collection,cci_identifier_t in_identifier)323*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_destroy_ccache (ccs_cache_collection_t  io_cache_collection,
324*7f2fe78bSCy Schubert                                               cci_identifier_t        in_identifier)
325*7f2fe78bSCy Schubert {
326*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
327*7f2fe78bSCy Schubert     ccs_ccache_t ccache = NULL;
328*7f2fe78bSCy Schubert 
329*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
330*7f2fe78bSCy Schubert     if (!in_identifier      ) { err = cci_check_error (ccErrBadParam); }
331*7f2fe78bSCy Schubert 
332*7f2fe78bSCy Schubert     if (!err) {
333*7f2fe78bSCy Schubert         err = ccs_cache_collection_find_ccache (io_cache_collection,
334*7f2fe78bSCy Schubert                                                 in_identifier,
335*7f2fe78bSCy Schubert                                                 &ccache);
336*7f2fe78bSCy Schubert     }
337*7f2fe78bSCy Schubert 
338*7f2fe78bSCy Schubert     if (!err) {
339*7f2fe78bSCy Schubert         /* Notify before deletion because after deletion the ccache
340*7f2fe78bSCy Schubert          * will no longer exist (and won't know about its clients) */
341*7f2fe78bSCy Schubert         err = ccs_ccache_changed (ccache, io_cache_collection);
342*7f2fe78bSCy Schubert     }
343*7f2fe78bSCy Schubert 
344*7f2fe78bSCy Schubert     if (!err) {
345*7f2fe78bSCy Schubert         err = ccs_ccache_list_remove (io_cache_collection->ccaches,
346*7f2fe78bSCy Schubert                                       in_identifier);
347*7f2fe78bSCy Schubert     }
348*7f2fe78bSCy Schubert 
349*7f2fe78bSCy Schubert     return cci_check_error (err);
350*7f2fe78bSCy Schubert }
351*7f2fe78bSCy Schubert 
352*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
353*7f2fe78bSCy Schubert #pragma mark -
354*7f2fe78bSCy Schubert #endif
355*7f2fe78bSCy Schubert 
356*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
357*7f2fe78bSCy Schubert 
ccs_cache_collection_find_ccache_iterator(ccs_cache_collection_t in_cache_collection,cci_identifier_t in_identifier,ccs_ccache_iterator_t * out_ccache_iterator)358*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_find_ccache_iterator (ccs_cache_collection_t  in_cache_collection,
359*7f2fe78bSCy Schubert                                                     cci_identifier_t        in_identifier,
360*7f2fe78bSCy Schubert                                                     ccs_ccache_iterator_t  *out_ccache_iterator)
361*7f2fe78bSCy Schubert {
362*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
363*7f2fe78bSCy Schubert 
364*7f2fe78bSCy Schubert     if (!in_cache_collection) { err = cci_check_error (ccErrBadParam); }
365*7f2fe78bSCy Schubert     if (!in_identifier      ) { err = cci_check_error (ccErrBadParam); }
366*7f2fe78bSCy Schubert     if (!out_ccache_iterator) { err = cci_check_error (ccErrBadParam); }
367*7f2fe78bSCy Schubert 
368*7f2fe78bSCy Schubert     if (!err) {
369*7f2fe78bSCy Schubert         err = ccs_ccache_list_find_iterator (in_cache_collection->ccaches,
370*7f2fe78bSCy Schubert                                              in_identifier,
371*7f2fe78bSCy Schubert                                              out_ccache_iterator);
372*7f2fe78bSCy Schubert     }
373*7f2fe78bSCy Schubert 
374*7f2fe78bSCy Schubert     return cci_check_error (err);
375*7f2fe78bSCy Schubert }
376*7f2fe78bSCy Schubert 
377*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
378*7f2fe78bSCy Schubert #pragma mark -
379*7f2fe78bSCy Schubert #endif
380*7f2fe78bSCy Schubert 
381*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
382*7f2fe78bSCy Schubert 
ccs_cache_collection_find_credentials_iterator(ccs_cache_collection_t in_cache_collection,cci_identifier_t in_identifier,ccs_ccache_t * out_ccache,ccs_credentials_iterator_t * out_credentials_iterator)383*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_find_credentials_iterator (ccs_cache_collection_t      in_cache_collection,
384*7f2fe78bSCy Schubert                                                          cci_identifier_t            in_identifier,
385*7f2fe78bSCy Schubert                                                          ccs_ccache_t               *out_ccache,
386*7f2fe78bSCy Schubert                                                          ccs_credentials_iterator_t *out_credentials_iterator)
387*7f2fe78bSCy Schubert {
388*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
389*7f2fe78bSCy Schubert     ccs_ccache_list_iterator_t iterator = NULL;
390*7f2fe78bSCy Schubert 
391*7f2fe78bSCy Schubert     if (!in_cache_collection     ) { err = cci_check_error (ccErrBadParam); }
392*7f2fe78bSCy Schubert     if (!in_identifier           ) { err = cci_check_error (ccErrBadParam); }
393*7f2fe78bSCy Schubert     if (!out_credentials_iterator) { err = cci_check_error (ccErrBadParam); }
394*7f2fe78bSCy Schubert 
395*7f2fe78bSCy Schubert     if (!err) {
396*7f2fe78bSCy Schubert         err = ccs_ccache_list_new_iterator (in_cache_collection->ccaches,
397*7f2fe78bSCy Schubert                                             CCS_PIPE_NULL,
398*7f2fe78bSCy Schubert                                             &iterator);
399*7f2fe78bSCy Schubert     }
400*7f2fe78bSCy Schubert 
401*7f2fe78bSCy Schubert     while (!err) {
402*7f2fe78bSCy Schubert         ccs_ccache_t ccache = NULL;
403*7f2fe78bSCy Schubert 
404*7f2fe78bSCy Schubert         err = ccs_ccache_list_iterator_next (iterator, &ccache);
405*7f2fe78bSCy Schubert 
406*7f2fe78bSCy Schubert         if (!err) {
407*7f2fe78bSCy Schubert             cc_int32 terr = ccs_ccache_find_credentials_iterator (ccache,
408*7f2fe78bSCy Schubert                                                                   in_identifier,
409*7f2fe78bSCy Schubert                                                                   out_credentials_iterator);
410*7f2fe78bSCy Schubert             if (!terr) {
411*7f2fe78bSCy Schubert                 *out_ccache = ccache;
412*7f2fe78bSCy Schubert                 break;
413*7f2fe78bSCy Schubert             }
414*7f2fe78bSCy Schubert         }
415*7f2fe78bSCy Schubert     }
416*7f2fe78bSCy Schubert     if (err == ccIteratorEnd) { err = cci_check_error (ccErrInvalidCredentialsIterator); }
417*7f2fe78bSCy Schubert 
418*7f2fe78bSCy Schubert     if (iterator) { ccs_ccache_list_iterator_release (iterator); }
419*7f2fe78bSCy Schubert 
420*7f2fe78bSCy Schubert     return cci_check_error (err);
421*7f2fe78bSCy Schubert }
422*7f2fe78bSCy Schubert 
423*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
424*7f2fe78bSCy Schubert #pragma mark -
425*7f2fe78bSCy Schubert #endif
426*7f2fe78bSCy Schubert 
427*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
428*7f2fe78bSCy Schubert 
ccs_cache_collection_get_next_unique_ccache_name(ccs_cache_collection_t io_cache_collection,char ** out_name)429*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_get_next_unique_ccache_name (ccs_cache_collection_t   io_cache_collection,
430*7f2fe78bSCy Schubert                                                                   char                   **out_name)
431*7f2fe78bSCy Schubert {
432*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
433*7f2fe78bSCy Schubert     cc_uint64 count = 0;
434*7f2fe78bSCy Schubert     char *name = NULL;
435*7f2fe78bSCy Schubert 
436*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
437*7f2fe78bSCy Schubert     if (!out_name           ) { err = cci_check_error (ccErrBadParam); }
438*7f2fe78bSCy Schubert 
439*7f2fe78bSCy Schubert     if (!err) {
440*7f2fe78bSCy Schubert         err = ccs_cache_collection_list_count (io_cache_collection->ccaches, &count);
441*7f2fe78bSCy Schubert     }
442*7f2fe78bSCy Schubert 
443*7f2fe78bSCy Schubert     if (!err) {
444*7f2fe78bSCy Schubert         if (count > 0) {
445*7f2fe78bSCy Schubert             while (!err) {
446*7f2fe78bSCy Schubert                 int ret = asprintf (&name, "%lld", io_cache_collection->next_unique_name++);
447*7f2fe78bSCy Schubert                 if (ret < 0 || !name) { err = cci_check_error (ccErrNoMem); }
448*7f2fe78bSCy Schubert 
449*7f2fe78bSCy Schubert                 if (!err) {
450*7f2fe78bSCy Schubert                     ccs_ccache_t ccache = NULL;  /* temporary to hold ccache pointer */
451*7f2fe78bSCy Schubert                     err = ccs_cache_collection_find_ccache_by_name (io_cache_collection,
452*7f2fe78bSCy Schubert                                                                     name, &ccache);
453*7f2fe78bSCy Schubert                 }
454*7f2fe78bSCy Schubert 
455*7f2fe78bSCy Schubert                 if (err == ccErrCCacheNotFound) {
456*7f2fe78bSCy Schubert                     err = ccNoError;
457*7f2fe78bSCy Schubert                     break;   /* found a unique one */
458*7f2fe78bSCy Schubert                 }
459*7f2fe78bSCy Schubert             }
460*7f2fe78bSCy Schubert         } else {
461*7f2fe78bSCy Schubert             name = strdup (k_cci_context_initial_ccache_name);
462*7f2fe78bSCy Schubert             if (!name) { err = cci_check_error (ccErrNoMem); }
463*7f2fe78bSCy Schubert         }
464*7f2fe78bSCy Schubert     }
465*7f2fe78bSCy Schubert 
466*7f2fe78bSCy Schubert     if (!err) {
467*7f2fe78bSCy Schubert         *out_name = name;
468*7f2fe78bSCy Schubert         name = NULL;
469*7f2fe78bSCy Schubert     }
470*7f2fe78bSCy Schubert 
471*7f2fe78bSCy Schubert     free (name);
472*7f2fe78bSCy Schubert 
473*7f2fe78bSCy Schubert     return cci_check_error (err);
474*7f2fe78bSCy Schubert }
475*7f2fe78bSCy Schubert 
476*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
477*7f2fe78bSCy Schubert 
ccs_cache_collection_get_default_ccache(ccs_cache_collection_t in_cache_collection,ccs_ccache_t * out_ccache)478*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_get_default_ccache (ccs_cache_collection_t  in_cache_collection,
479*7f2fe78bSCy Schubert                                                          ccs_ccache_t           *out_ccache)
480*7f2fe78bSCy Schubert {
481*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
482*7f2fe78bSCy Schubert     cc_uint64 count = 0;
483*7f2fe78bSCy Schubert 
484*7f2fe78bSCy Schubert     if (!in_cache_collection) { err = cci_check_error (ccErrBadParam); }
485*7f2fe78bSCy Schubert     if (!out_ccache         ) { err = cci_check_error (ccErrBadParam); }
486*7f2fe78bSCy Schubert 
487*7f2fe78bSCy Schubert     if (!err) {
488*7f2fe78bSCy Schubert         err = ccs_ccache_list_count (in_cache_collection->ccaches, &count);
489*7f2fe78bSCy Schubert     }
490*7f2fe78bSCy Schubert 
491*7f2fe78bSCy Schubert     if (!err) {
492*7f2fe78bSCy Schubert         if (count > 0) {
493*7f2fe78bSCy Schubert             /* First ccache is the default */
494*7f2fe78bSCy Schubert             ccs_ccache_list_iterator_t iterator = NULL;
495*7f2fe78bSCy Schubert 
496*7f2fe78bSCy Schubert             err = ccs_ccache_list_new_iterator (in_cache_collection->ccaches,
497*7f2fe78bSCy Schubert                                                 CCS_PIPE_NULL,
498*7f2fe78bSCy Schubert                                                 &iterator);
499*7f2fe78bSCy Schubert 
500*7f2fe78bSCy Schubert             if (!err) {
501*7f2fe78bSCy Schubert                 err = ccs_ccache_list_iterator_next (iterator, out_ccache);
502*7f2fe78bSCy Schubert             }
503*7f2fe78bSCy Schubert 
504*7f2fe78bSCy Schubert             ccs_ccache_list_iterator_release (iterator);
505*7f2fe78bSCy Schubert 
506*7f2fe78bSCy Schubert         } else {
507*7f2fe78bSCy Schubert             err = cci_check_error (ccErrCCacheNotFound);
508*7f2fe78bSCy Schubert         }
509*7f2fe78bSCy Schubert     }
510*7f2fe78bSCy Schubert 
511*7f2fe78bSCy Schubert     return cci_check_error (err);
512*7f2fe78bSCy Schubert }
513*7f2fe78bSCy Schubert 
514*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
515*7f2fe78bSCy Schubert 
ccs_cache_collection_set_default_ccache(ccs_cache_collection_t io_cache_collection,cci_identifier_t in_identifier)516*7f2fe78bSCy Schubert cc_int32 ccs_cache_collection_set_default_ccache (ccs_cache_collection_t  io_cache_collection,
517*7f2fe78bSCy Schubert                                                   cci_identifier_t        in_identifier)
518*7f2fe78bSCy Schubert {
519*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
520*7f2fe78bSCy Schubert     ccs_ccache_t old_default = NULL;
521*7f2fe78bSCy Schubert     ccs_ccache_t new_default = NULL;
522*7f2fe78bSCy Schubert     cc_uint32 equal = 0;
523*7f2fe78bSCy Schubert 
524*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
525*7f2fe78bSCy Schubert     if (!in_identifier      ) { err = cci_check_error (ccErrBadParam); }
526*7f2fe78bSCy Schubert 
527*7f2fe78bSCy Schubert     if (!err) {
528*7f2fe78bSCy Schubert         err = ccs_cache_collection_get_default_ccache (io_cache_collection,
529*7f2fe78bSCy Schubert                                                        &old_default);
530*7f2fe78bSCy Schubert     }
531*7f2fe78bSCy Schubert 
532*7f2fe78bSCy Schubert     if (!err) {
533*7f2fe78bSCy Schubert 	err = ccs_ccache_compare_identifier (old_default, in_identifier, &equal);
534*7f2fe78bSCy Schubert     }
535*7f2fe78bSCy Schubert 
536*7f2fe78bSCy Schubert 
537*7f2fe78bSCy Schubert     if (!err && !equal) {
538*7f2fe78bSCy Schubert         err = ccs_ccache_list_push_front (io_cache_collection->ccaches,
539*7f2fe78bSCy Schubert                                           in_identifier);
540*7f2fe78bSCy Schubert 
541*7f2fe78bSCy Schubert 	if (!err) {
542*7f2fe78bSCy Schubert 	    err = ccs_ccache_notify_default_state_changed (old_default,
543*7f2fe78bSCy Schubert 							   io_cache_collection,
544*7f2fe78bSCy Schubert 							   0 /* no longer default */);
545*7f2fe78bSCy Schubert 	}
546*7f2fe78bSCy Schubert 
547*7f2fe78bSCy Schubert 	if (!err) {
548*7f2fe78bSCy Schubert 	    err = ccs_cache_collection_get_default_ccache (io_cache_collection,
549*7f2fe78bSCy Schubert 							   &new_default);
550*7f2fe78bSCy Schubert 	}
551*7f2fe78bSCy Schubert 
552*7f2fe78bSCy Schubert 	if (!err) {
553*7f2fe78bSCy Schubert 	    err = ccs_ccache_notify_default_state_changed (new_default,
554*7f2fe78bSCy Schubert 							   io_cache_collection,
555*7f2fe78bSCy Schubert 							   1 /* now default */);
556*7f2fe78bSCy Schubert 	}
557*7f2fe78bSCy Schubert 
558*7f2fe78bSCy Schubert 	if (!err) {
559*7f2fe78bSCy Schubert 	    err = ccs_cache_collection_changed (io_cache_collection);
560*7f2fe78bSCy Schubert 	}
561*7f2fe78bSCy Schubert     }
562*7f2fe78bSCy Schubert 
563*7f2fe78bSCy Schubert     return cci_check_error (err);
564*7f2fe78bSCy Schubert }
565*7f2fe78bSCy Schubert 
566*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
567*7f2fe78bSCy Schubert #pragma mark -
568*7f2fe78bSCy Schubert #pragma mark -- IPC Messages --
569*7f2fe78bSCy Schubert #endif
570*7f2fe78bSCy Schubert 
571*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
572*7f2fe78bSCy Schubert 
ccs_cache_collection_sync(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)573*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_sync (ccs_cache_collection_t io_cache_collection,
574*7f2fe78bSCy Schubert                                             k5_ipc_stream           in_request_data,
575*7f2fe78bSCy Schubert                                             k5_ipc_stream           io_reply_data)
576*7f2fe78bSCy Schubert {
577*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
578*7f2fe78bSCy Schubert 
579*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
580*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
581*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
582*7f2fe78bSCy Schubert 
583*7f2fe78bSCy Schubert     if (!err) {
584*7f2fe78bSCy Schubert         err = cci_identifier_write (io_cache_collection->identifier, io_reply_data);
585*7f2fe78bSCy Schubert     }
586*7f2fe78bSCy Schubert 
587*7f2fe78bSCy Schubert     return cci_check_error (err);
588*7f2fe78bSCy Schubert }
589*7f2fe78bSCy Schubert 
590*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
591*7f2fe78bSCy Schubert 
ccs_cache_collection_get_change_time(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)592*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_get_change_time (ccs_cache_collection_t io_cache_collection,
593*7f2fe78bSCy Schubert                                                        k5_ipc_stream           in_request_data,
594*7f2fe78bSCy Schubert                                                        k5_ipc_stream           io_reply_data)
595*7f2fe78bSCy Schubert {
596*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
597*7f2fe78bSCy Schubert 
598*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
599*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
600*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
601*7f2fe78bSCy Schubert 
602*7f2fe78bSCy Schubert     if (!err) {
603*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_write_time (io_reply_data, io_cache_collection->last_changed_time);
604*7f2fe78bSCy Schubert     }
605*7f2fe78bSCy Schubert 
606*7f2fe78bSCy Schubert     return cci_check_error (err);
607*7f2fe78bSCy Schubert }
608*7f2fe78bSCy Schubert 
609*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
610*7f2fe78bSCy Schubert 
ccs_cache_collection_wait_for_change(ccs_pipe_t in_client_pipe,ccs_pipe_t in_reply_pipe,ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data,cc_uint32 * out_will_block)611*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_wait_for_change (ccs_pipe_t              in_client_pipe,
612*7f2fe78bSCy Schubert 						      ccs_pipe_t              in_reply_pipe,
613*7f2fe78bSCy Schubert 						      ccs_cache_collection_t  io_cache_collection,
614*7f2fe78bSCy Schubert 						      k5_ipc_stream            in_request_data,
615*7f2fe78bSCy Schubert 						      k5_ipc_stream            io_reply_data,
616*7f2fe78bSCy Schubert 						      cc_uint32              *out_will_block)
617*7f2fe78bSCy Schubert {
618*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
619*7f2fe78bSCy Schubert     cc_time_t last_wait_for_change_time = 0;
620*7f2fe78bSCy Schubert     cc_uint32 will_block = 0;
621*7f2fe78bSCy Schubert 
622*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_client_pipe)) { err = cci_check_error (ccErrBadParam); }
623*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_reply_pipe )) { err = cci_check_error (ccErrBadParam); }
624*7f2fe78bSCy Schubert     if (!io_cache_collection            ) { err = cci_check_error (ccErrBadParam); }
625*7f2fe78bSCy Schubert     if (!in_request_data                ) { err = cci_check_error (ccErrBadParam); }
626*7f2fe78bSCy Schubert     if (!out_will_block                 ) { err = cci_check_error (ccErrBadParam); }
627*7f2fe78bSCy Schubert 
628*7f2fe78bSCy Schubert     if (!err) {
629*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_time (in_request_data, &last_wait_for_change_time);
630*7f2fe78bSCy Schubert     }
631*7f2fe78bSCy Schubert 
632*7f2fe78bSCy Schubert     if (!err) {
633*7f2fe78bSCy Schubert 	if (last_wait_for_change_time < io_cache_collection->last_changed_time) {
634*7f2fe78bSCy Schubert 	    err = krb5int_ipc_stream_write_time (io_reply_data, io_cache_collection->last_changed_time);
635*7f2fe78bSCy Schubert 
636*7f2fe78bSCy Schubert 	} else {
637*7f2fe78bSCy Schubert 	    ccs_callback_t callback = NULL;
638*7f2fe78bSCy Schubert 
639*7f2fe78bSCy Schubert 	    err = ccs_callback_new (&callback,
640*7f2fe78bSCy Schubert 				    ccErrInvalidContext,
641*7f2fe78bSCy Schubert 				    in_client_pipe,
642*7f2fe78bSCy Schubert 				    in_reply_pipe,
643*7f2fe78bSCy Schubert 				    (ccs_callback_owner_t) io_cache_collection,
644*7f2fe78bSCy Schubert 				    ccs_cache_collection_invalidate_change_callback);
645*7f2fe78bSCy Schubert 
646*7f2fe78bSCy Schubert 	    if (!err) {
647*7f2fe78bSCy Schubert 		err = ccs_callback_array_insert (io_cache_collection->change_callbacks, callback,
648*7f2fe78bSCy Schubert 						 ccs_callback_array_count (io_cache_collection->change_callbacks));
649*7f2fe78bSCy Schubert 		if (!err) { callback = NULL; /* take ownership */ }
650*7f2fe78bSCy Schubert 
651*7f2fe78bSCy Schubert 		will_block = 1;
652*7f2fe78bSCy Schubert 	    }
653*7f2fe78bSCy Schubert 
654*7f2fe78bSCy Schubert 	    ccs_callback_release (callback);
655*7f2fe78bSCy Schubert 	}
656*7f2fe78bSCy Schubert     }
657*7f2fe78bSCy Schubert 
658*7f2fe78bSCy Schubert     if (!err) {
659*7f2fe78bSCy Schubert 	*out_will_block = will_block;
660*7f2fe78bSCy Schubert     }
661*7f2fe78bSCy Schubert 
662*7f2fe78bSCy Schubert     return cci_check_error (err);
663*7f2fe78bSCy Schubert }
664*7f2fe78bSCy Schubert 
665*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
666*7f2fe78bSCy Schubert 
ccs_cache_collection_get_default_ccache_name(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)667*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_get_default_ccache_name (ccs_cache_collection_t io_cache_collection,
668*7f2fe78bSCy Schubert                                                                k5_ipc_stream           in_request_data,
669*7f2fe78bSCy Schubert                                                                k5_ipc_stream           io_reply_data)
670*7f2fe78bSCy Schubert {
671*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
672*7f2fe78bSCy Schubert     cc_uint64 count = 0;
673*7f2fe78bSCy Schubert 
674*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
675*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
676*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
677*7f2fe78bSCy Schubert 
678*7f2fe78bSCy Schubert     if (!err) {
679*7f2fe78bSCy Schubert         err = ccs_cache_collection_list_count (io_cache_collection->ccaches, &count);
680*7f2fe78bSCy Schubert     }
681*7f2fe78bSCy Schubert 
682*7f2fe78bSCy Schubert     if (!err) {
683*7f2fe78bSCy Schubert         if (count > 0) {
684*7f2fe78bSCy Schubert             ccs_ccache_t ccache = NULL;
685*7f2fe78bSCy Schubert 
686*7f2fe78bSCy Schubert             err = ccs_cache_collection_get_default_ccache (io_cache_collection, &ccache);
687*7f2fe78bSCy Schubert 
688*7f2fe78bSCy Schubert             if (!err) {
689*7f2fe78bSCy Schubert                 err = ccs_ccache_write_name (ccache, io_reply_data);
690*7f2fe78bSCy Schubert             }
691*7f2fe78bSCy Schubert         } else {
692*7f2fe78bSCy Schubert             err = krb5int_ipc_stream_write_string (io_reply_data,
693*7f2fe78bSCy Schubert                                            k_cci_context_initial_ccache_name);
694*7f2fe78bSCy Schubert         }
695*7f2fe78bSCy Schubert     }
696*7f2fe78bSCy Schubert 
697*7f2fe78bSCy Schubert     return cci_check_error (err);
698*7f2fe78bSCy Schubert }
699*7f2fe78bSCy Schubert 
700*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
701*7f2fe78bSCy Schubert 
ccs_cache_collection_open_ccache(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)702*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_open_ccache (ccs_cache_collection_t io_cache_collection,
703*7f2fe78bSCy Schubert                                                    k5_ipc_stream           in_request_data,
704*7f2fe78bSCy Schubert                                                    k5_ipc_stream           io_reply_data)
705*7f2fe78bSCy Schubert {
706*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
707*7f2fe78bSCy Schubert     char *name = NULL;
708*7f2fe78bSCy Schubert     ccs_ccache_t ccache = NULL;
709*7f2fe78bSCy Schubert 
710*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
711*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
712*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
713*7f2fe78bSCy Schubert 
714*7f2fe78bSCy Schubert     if (!err) {
715*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_string (in_request_data, &name);
716*7f2fe78bSCy Schubert     }
717*7f2fe78bSCy Schubert 
718*7f2fe78bSCy Schubert     if (!err) {
719*7f2fe78bSCy Schubert         err = ccs_cache_collection_find_ccache_by_name (io_cache_collection,
720*7f2fe78bSCy Schubert                                                         name, &ccache);
721*7f2fe78bSCy Schubert     }
722*7f2fe78bSCy Schubert 
723*7f2fe78bSCy Schubert     if (!err) {
724*7f2fe78bSCy Schubert         err = ccs_ccache_write (ccache, io_reply_data);
725*7f2fe78bSCy Schubert     }
726*7f2fe78bSCy Schubert 
727*7f2fe78bSCy Schubert     krb5int_ipc_stream_free_string (name);
728*7f2fe78bSCy Schubert 
729*7f2fe78bSCy Schubert     return cci_check_error (err);
730*7f2fe78bSCy Schubert }
731*7f2fe78bSCy Schubert 
732*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
733*7f2fe78bSCy Schubert 
ccs_cache_collection_open_default_ccache(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)734*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_open_default_ccache (ccs_cache_collection_t io_cache_collection,
735*7f2fe78bSCy Schubert                                                            k5_ipc_stream           in_request_data,
736*7f2fe78bSCy Schubert                                                            k5_ipc_stream           io_reply_data)
737*7f2fe78bSCy Schubert {
738*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
739*7f2fe78bSCy Schubert     ccs_ccache_t ccache = NULL;
740*7f2fe78bSCy Schubert 
741*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
742*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
743*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
744*7f2fe78bSCy Schubert 
745*7f2fe78bSCy Schubert     if (!err) {
746*7f2fe78bSCy Schubert          err = ccs_cache_collection_get_default_ccache (io_cache_collection,
747*7f2fe78bSCy Schubert                                                         &ccache);
748*7f2fe78bSCy Schubert     }
749*7f2fe78bSCy Schubert 
750*7f2fe78bSCy Schubert     if (!err) {
751*7f2fe78bSCy Schubert         err = ccs_ccache_write (ccache, io_reply_data);
752*7f2fe78bSCy Schubert     }
753*7f2fe78bSCy Schubert 
754*7f2fe78bSCy Schubert     return cci_check_error (err);
755*7f2fe78bSCy Schubert }
756*7f2fe78bSCy Schubert 
757*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
758*7f2fe78bSCy Schubert 
ccs_cache_collection_create_ccache(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)759*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_create_ccache (ccs_cache_collection_t io_cache_collection,
760*7f2fe78bSCy Schubert                                                     k5_ipc_stream           in_request_data,
761*7f2fe78bSCy Schubert                                                     k5_ipc_stream           io_reply_data)
762*7f2fe78bSCy Schubert {
763*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
764*7f2fe78bSCy Schubert     char *name = NULL;
765*7f2fe78bSCy Schubert     cc_uint32 cred_vers;
766*7f2fe78bSCy Schubert     char *principal = NULL;
767*7f2fe78bSCy Schubert     ccs_ccache_t ccache = NULL;
768*7f2fe78bSCy Schubert 
769*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
770*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
771*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
772*7f2fe78bSCy Schubert 
773*7f2fe78bSCy Schubert     if (!err) {
774*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_string (in_request_data, &name);
775*7f2fe78bSCy Schubert     }
776*7f2fe78bSCy Schubert 
777*7f2fe78bSCy Schubert     if (!err) {
778*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_uint32 (in_request_data, &cred_vers);
779*7f2fe78bSCy Schubert     }
780*7f2fe78bSCy Schubert 
781*7f2fe78bSCy Schubert     if (!err) {
782*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_string (in_request_data, &principal);
783*7f2fe78bSCy Schubert     }
784*7f2fe78bSCy Schubert 
785*7f2fe78bSCy Schubert     if (!err) {
786*7f2fe78bSCy Schubert         cc_int32 terr = ccs_cache_collection_find_ccache_by_name (io_cache_collection,
787*7f2fe78bSCy Schubert                                                                   name,
788*7f2fe78bSCy Schubert                                                                   &ccache);
789*7f2fe78bSCy Schubert 
790*7f2fe78bSCy Schubert         if (!terr) {
791*7f2fe78bSCy Schubert             err = ccs_ccache_reset (ccache, io_cache_collection, cred_vers, principal);
792*7f2fe78bSCy Schubert 
793*7f2fe78bSCy Schubert         } else {
794*7f2fe78bSCy Schubert             err = ccs_ccache_new (&ccache, cred_vers, name, principal,
795*7f2fe78bSCy Schubert                                   io_cache_collection->ccaches);
796*7f2fe78bSCy Schubert         }
797*7f2fe78bSCy Schubert     }
798*7f2fe78bSCy Schubert 
799*7f2fe78bSCy Schubert     if (!err) {
800*7f2fe78bSCy Schubert         err = ccs_ccache_write (ccache, io_reply_data);
801*7f2fe78bSCy Schubert     }
802*7f2fe78bSCy Schubert 
803*7f2fe78bSCy Schubert     if (!err) {
804*7f2fe78bSCy Schubert         err = ccs_cache_collection_changed (io_cache_collection);
805*7f2fe78bSCy Schubert     }
806*7f2fe78bSCy Schubert 
807*7f2fe78bSCy Schubert     krb5int_ipc_stream_free_string (name);
808*7f2fe78bSCy Schubert     krb5int_ipc_stream_free_string (principal);
809*7f2fe78bSCy Schubert 
810*7f2fe78bSCy Schubert     return cci_check_error (err);
811*7f2fe78bSCy Schubert }
812*7f2fe78bSCy Schubert 
813*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
814*7f2fe78bSCy Schubert 
ccs_cache_collection_create_default_ccache(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)815*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_create_default_ccache (ccs_cache_collection_t io_cache_collection,
816*7f2fe78bSCy Schubert                                                             k5_ipc_stream           in_request_data,
817*7f2fe78bSCy Schubert                                                             k5_ipc_stream           io_reply_data)
818*7f2fe78bSCy Schubert {
819*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
820*7f2fe78bSCy Schubert     cc_uint32 cred_vers;
821*7f2fe78bSCy Schubert     char *principal = NULL;
822*7f2fe78bSCy Schubert     ccs_ccache_t ccache = NULL;
823*7f2fe78bSCy Schubert 
824*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
825*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
826*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
827*7f2fe78bSCy Schubert 
828*7f2fe78bSCy Schubert     if (!err) {
829*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_uint32 (in_request_data, &cred_vers);
830*7f2fe78bSCy Schubert     }
831*7f2fe78bSCy Schubert 
832*7f2fe78bSCy Schubert     if (!err) {
833*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_string (in_request_data, &principal);
834*7f2fe78bSCy Schubert     }
835*7f2fe78bSCy Schubert 
836*7f2fe78bSCy Schubert     if (!err) {
837*7f2fe78bSCy Schubert         err = ccs_cache_collection_get_default_ccache (io_cache_collection,
838*7f2fe78bSCy Schubert                                                        &ccache);
839*7f2fe78bSCy Schubert 
840*7f2fe78bSCy Schubert         if (!err) {
841*7f2fe78bSCy Schubert             err = ccs_ccache_reset (ccache, io_cache_collection, cred_vers, principal);
842*7f2fe78bSCy Schubert 
843*7f2fe78bSCy Schubert         } else if (err == ccErrCCacheNotFound) {
844*7f2fe78bSCy Schubert             char *name = NULL;
845*7f2fe78bSCy Schubert 
846*7f2fe78bSCy Schubert             err = ccs_cache_collection_get_next_unique_ccache_name (io_cache_collection,
847*7f2fe78bSCy Schubert                                                                     &name);
848*7f2fe78bSCy Schubert 
849*7f2fe78bSCy Schubert             if (!err) {
850*7f2fe78bSCy Schubert                 err = ccs_ccache_new (&ccache, cred_vers, name, principal,
851*7f2fe78bSCy Schubert                                       io_cache_collection->ccaches);
852*7f2fe78bSCy Schubert             }
853*7f2fe78bSCy Schubert 
854*7f2fe78bSCy Schubert             free (name);
855*7f2fe78bSCy Schubert         }
856*7f2fe78bSCy Schubert     }
857*7f2fe78bSCy Schubert 
858*7f2fe78bSCy Schubert     if (!err) {
859*7f2fe78bSCy Schubert         err = ccs_ccache_write (ccache, io_reply_data);
860*7f2fe78bSCy Schubert     }
861*7f2fe78bSCy Schubert 
862*7f2fe78bSCy Schubert     if (!err) {
863*7f2fe78bSCy Schubert         err = ccs_cache_collection_changed (io_cache_collection);
864*7f2fe78bSCy Schubert     }
865*7f2fe78bSCy Schubert 
866*7f2fe78bSCy Schubert     krb5int_ipc_stream_free_string (principal);
867*7f2fe78bSCy Schubert 
868*7f2fe78bSCy Schubert     return cci_check_error (err);
869*7f2fe78bSCy Schubert }
870*7f2fe78bSCy Schubert 
871*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
872*7f2fe78bSCy Schubert 
ccs_cache_collection_create_new_ccache(ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)873*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_create_new_ccache (ccs_cache_collection_t io_cache_collection,
874*7f2fe78bSCy Schubert                                                         k5_ipc_stream           in_request_data,
875*7f2fe78bSCy Schubert                                                         k5_ipc_stream           io_reply_data)
876*7f2fe78bSCy Schubert {
877*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
878*7f2fe78bSCy Schubert     cc_uint32 cred_vers;
879*7f2fe78bSCy Schubert     char *principal = NULL;
880*7f2fe78bSCy Schubert     char *name = NULL;
881*7f2fe78bSCy Schubert     ccs_ccache_t ccache = NULL;
882*7f2fe78bSCy Schubert 
883*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
884*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
885*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
886*7f2fe78bSCy Schubert 
887*7f2fe78bSCy Schubert     if (!err) {
888*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_uint32 (in_request_data, &cred_vers);
889*7f2fe78bSCy Schubert     }
890*7f2fe78bSCy Schubert 
891*7f2fe78bSCy Schubert     if (!err) {
892*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_string (in_request_data, &principal);
893*7f2fe78bSCy Schubert     }
894*7f2fe78bSCy Schubert 
895*7f2fe78bSCy Schubert     if (!err) {
896*7f2fe78bSCy Schubert         err = ccs_cache_collection_get_next_unique_ccache_name (io_cache_collection,
897*7f2fe78bSCy Schubert                                                                 &name);
898*7f2fe78bSCy Schubert     }
899*7f2fe78bSCy Schubert 
900*7f2fe78bSCy Schubert     if (!err) {
901*7f2fe78bSCy Schubert         err = ccs_ccache_new (&ccache, cred_vers, name, principal,
902*7f2fe78bSCy Schubert                               io_cache_collection->ccaches);
903*7f2fe78bSCy Schubert     }
904*7f2fe78bSCy Schubert 
905*7f2fe78bSCy Schubert     if (!err) {
906*7f2fe78bSCy Schubert         err = ccs_ccache_write (ccache, io_reply_data);
907*7f2fe78bSCy Schubert     }
908*7f2fe78bSCy Schubert 
909*7f2fe78bSCy Schubert     if (!err) {
910*7f2fe78bSCy Schubert         err = ccs_cache_collection_changed (io_cache_collection);
911*7f2fe78bSCy Schubert     }
912*7f2fe78bSCy Schubert 
913*7f2fe78bSCy Schubert     free (name);
914*7f2fe78bSCy Schubert     krb5int_ipc_stream_free_string (principal);
915*7f2fe78bSCy Schubert 
916*7f2fe78bSCy Schubert     return cci_check_error (err);
917*7f2fe78bSCy Schubert }
918*7f2fe78bSCy Schubert 
919*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
920*7f2fe78bSCy Schubert 
ccs_cache_collection_new_ccache_iterator(ccs_cache_collection_t io_cache_collection,ccs_pipe_t in_client_pipe,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)921*7f2fe78bSCy Schubert static  cc_int32 ccs_cache_collection_new_ccache_iterator (ccs_cache_collection_t io_cache_collection,
922*7f2fe78bSCy Schubert                                                            ccs_pipe_t             in_client_pipe,
923*7f2fe78bSCy Schubert                                                            k5_ipc_stream           in_request_data,
924*7f2fe78bSCy Schubert                                                            k5_ipc_stream           io_reply_data)
925*7f2fe78bSCy Schubert {
926*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
927*7f2fe78bSCy Schubert     ccs_ccache_iterator_t ccache_iterator = NULL;
928*7f2fe78bSCy Schubert 
929*7f2fe78bSCy Schubert     if (!io_cache_collection) { err = cci_check_error (ccErrBadParam); }
930*7f2fe78bSCy Schubert     if (!in_request_data    ) { err = cci_check_error (ccErrBadParam); }
931*7f2fe78bSCy Schubert     if (!io_reply_data      ) { err = cci_check_error (ccErrBadParam); }
932*7f2fe78bSCy Schubert 
933*7f2fe78bSCy Schubert     if (!err) {
934*7f2fe78bSCy Schubert         err = ccs_ccache_list_new_iterator (io_cache_collection->ccaches,
935*7f2fe78bSCy Schubert                                             in_client_pipe,
936*7f2fe78bSCy Schubert                                             &ccache_iterator);
937*7f2fe78bSCy Schubert     }
938*7f2fe78bSCy Schubert 
939*7f2fe78bSCy Schubert     if (!err) {
940*7f2fe78bSCy Schubert         err = ccs_ccache_list_iterator_write (ccache_iterator, io_reply_data);
941*7f2fe78bSCy Schubert     }
942*7f2fe78bSCy Schubert 
943*7f2fe78bSCy Schubert     return cci_check_error (err);
944*7f2fe78bSCy Schubert }
945*7f2fe78bSCy Schubert 
946*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
947*7f2fe78bSCy Schubert 
ccs_cache_collection_lock(ccs_pipe_t in_client_pipe,ccs_pipe_t in_reply_pipe,ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,cc_uint32 * out_will_block,k5_ipc_stream io_reply_data)948*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_lock (ccs_pipe_t              in_client_pipe,
949*7f2fe78bSCy Schubert                                            ccs_pipe_t              in_reply_pipe,
950*7f2fe78bSCy Schubert                                            ccs_cache_collection_t  io_cache_collection,
951*7f2fe78bSCy Schubert                                            k5_ipc_stream            in_request_data,
952*7f2fe78bSCy Schubert                                            cc_uint32              *out_will_block,
953*7f2fe78bSCy Schubert                                            k5_ipc_stream            io_reply_data)
954*7f2fe78bSCy Schubert {
955*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
956*7f2fe78bSCy Schubert     cc_uint32 lock_type;
957*7f2fe78bSCy Schubert     cc_uint32 block;
958*7f2fe78bSCy Schubert 
959*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_client_pipe)) { err = cci_check_error (ccErrBadParam); }
960*7f2fe78bSCy Schubert     if (!io_cache_collection            ) { err = cci_check_error (ccErrBadParam); }
961*7f2fe78bSCy Schubert     if (!in_request_data                ) { err = cci_check_error (ccErrBadParam); }
962*7f2fe78bSCy Schubert     if (!out_will_block                 ) { err = cci_check_error (ccErrBadParam); }
963*7f2fe78bSCy Schubert     if (!io_reply_data                  ) { err = cci_check_error (ccErrBadParam); }
964*7f2fe78bSCy Schubert 
965*7f2fe78bSCy Schubert     if (!err) {
966*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_uint32 (in_request_data, &lock_type);
967*7f2fe78bSCy Schubert     }
968*7f2fe78bSCy Schubert 
969*7f2fe78bSCy Schubert     if (!err) {
970*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_read_uint32 (in_request_data, &block);
971*7f2fe78bSCy Schubert     }
972*7f2fe78bSCy Schubert 
973*7f2fe78bSCy Schubert     if (!err) {
974*7f2fe78bSCy Schubert         err = ccs_lock_state_add (io_cache_collection->lock_state,
975*7f2fe78bSCy Schubert                                   in_client_pipe, in_reply_pipe,
976*7f2fe78bSCy Schubert                                   lock_type, block, out_will_block);
977*7f2fe78bSCy Schubert     }
978*7f2fe78bSCy Schubert 
979*7f2fe78bSCy Schubert     return cci_check_error (err);
980*7f2fe78bSCy Schubert }
981*7f2fe78bSCy Schubert 
982*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
983*7f2fe78bSCy Schubert 
ccs_cache_collection_unlock(ccs_pipe_t in_client_pipe,ccs_cache_collection_t io_cache_collection,k5_ipc_stream in_request_data,k5_ipc_stream io_reply_data)984*7f2fe78bSCy Schubert static cc_int32 ccs_cache_collection_unlock (ccs_pipe_t             in_client_pipe,
985*7f2fe78bSCy Schubert                                              ccs_cache_collection_t io_cache_collection,
986*7f2fe78bSCy Schubert                                              k5_ipc_stream           in_request_data,
987*7f2fe78bSCy Schubert                                              k5_ipc_stream           io_reply_data)
988*7f2fe78bSCy Schubert {
989*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
990*7f2fe78bSCy Schubert 
991*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_client_pipe)) { err = cci_check_error (ccErrBadParam); }
992*7f2fe78bSCy Schubert     if (!io_cache_collection            ) { err = cci_check_error (ccErrBadParam); }
993*7f2fe78bSCy Schubert     if (!in_request_data                ) { err = cci_check_error (ccErrBadParam); }
994*7f2fe78bSCy Schubert     if (!io_reply_data                  ) { err = cci_check_error (ccErrBadParam); }
995*7f2fe78bSCy Schubert 
996*7f2fe78bSCy Schubert     if (!err) {
997*7f2fe78bSCy Schubert         err = ccs_lock_state_remove (io_cache_collection->lock_state,
998*7f2fe78bSCy Schubert                                      in_client_pipe);
999*7f2fe78bSCy Schubert     }
1000*7f2fe78bSCy Schubert 
1001*7f2fe78bSCy Schubert     return cci_check_error (err);
1002*7f2fe78bSCy Schubert }
1003*7f2fe78bSCy Schubert 
1004*7f2fe78bSCy Schubert #ifdef TARGET_OS_MAC
1005*7f2fe78bSCy Schubert #pragma mark -
1006*7f2fe78bSCy Schubert #endif
1007*7f2fe78bSCy Schubert 
1008*7f2fe78bSCy Schubert /* ------------------------------------------------------------------------ */
1009*7f2fe78bSCy Schubert 
ccs_cache_collection_handle_message(ccs_pipe_t in_client_pipe,ccs_pipe_t in_reply_pipe,ccs_cache_collection_t io_cache_collection,enum cci_msg_id_t in_request_name,k5_ipc_stream in_request_data,cc_uint32 * out_will_block,k5_ipc_stream * out_reply_data)1010*7f2fe78bSCy Schubert  cc_int32 ccs_cache_collection_handle_message (ccs_pipe_t              in_client_pipe,
1011*7f2fe78bSCy Schubert                                                ccs_pipe_t              in_reply_pipe,
1012*7f2fe78bSCy Schubert                                                ccs_cache_collection_t  io_cache_collection,
1013*7f2fe78bSCy Schubert                                                enum cci_msg_id_t       in_request_name,
1014*7f2fe78bSCy Schubert                                                k5_ipc_stream            in_request_data,
1015*7f2fe78bSCy Schubert                                                cc_uint32              *out_will_block,
1016*7f2fe78bSCy Schubert                                                k5_ipc_stream           *out_reply_data)
1017*7f2fe78bSCy Schubert {
1018*7f2fe78bSCy Schubert     cc_int32 err = ccNoError;
1019*7f2fe78bSCy Schubert     cc_uint32 will_block = 0;
1020*7f2fe78bSCy Schubert     k5_ipc_stream reply_data = NULL;
1021*7f2fe78bSCy Schubert 
1022*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_client_pipe)) { err = cci_check_error (ccErrBadParam); }
1023*7f2fe78bSCy Schubert     if (!ccs_pipe_valid (in_reply_pipe) ) { err = cci_check_error (ccErrBadParam); }
1024*7f2fe78bSCy Schubert     if (!io_cache_collection            ) { err = cci_check_error (ccErrBadParam); }
1025*7f2fe78bSCy Schubert     if (!in_request_data                ) { err = cci_check_error (ccErrBadParam); }
1026*7f2fe78bSCy Schubert     if (!out_will_block                 ) { err = cci_check_error (ccErrBadParam); }
1027*7f2fe78bSCy Schubert     if (!out_reply_data                 ) { err = cci_check_error (ccErrBadParam); }
1028*7f2fe78bSCy Schubert 
1029*7f2fe78bSCy Schubert     if (!err) {
1030*7f2fe78bSCy Schubert         err = krb5int_ipc_stream_new (&reply_data);
1031*7f2fe78bSCy Schubert     }
1032*7f2fe78bSCy Schubert 
1033*7f2fe78bSCy Schubert     if (!err) {
1034*7f2fe78bSCy Schubert         if (in_request_name == cci_context_unused_release_msg_id) {
1035*7f2fe78bSCy Schubert             /* Old release message.  Do nothing. */
1036*7f2fe78bSCy Schubert 
1037*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_sync_msg_id) {
1038*7f2fe78bSCy Schubert             err = ccs_cache_collection_sync (io_cache_collection,
1039*7f2fe78bSCy Schubert                                              in_request_data, reply_data);
1040*7f2fe78bSCy Schubert 
1041*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_get_change_time_msg_id) {
1042*7f2fe78bSCy Schubert             err = ccs_cache_collection_get_change_time (io_cache_collection,
1043*7f2fe78bSCy Schubert                                                         in_request_data, reply_data);
1044*7f2fe78bSCy Schubert 
1045*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_wait_for_change_msg_id) {
1046*7f2fe78bSCy Schubert             err = ccs_cache_collection_wait_for_change (in_client_pipe, in_reply_pipe,
1047*7f2fe78bSCy Schubert 							io_cache_collection,
1048*7f2fe78bSCy Schubert                                                         in_request_data, reply_data,
1049*7f2fe78bSCy Schubert 							&will_block);
1050*7f2fe78bSCy Schubert 
1051*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_get_default_ccache_name_msg_id) {
1052*7f2fe78bSCy Schubert             err = ccs_cache_collection_get_default_ccache_name (io_cache_collection,
1053*7f2fe78bSCy Schubert                                                                 in_request_data, reply_data);
1054*7f2fe78bSCy Schubert 
1055*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_open_ccache_msg_id) {
1056*7f2fe78bSCy Schubert             err = ccs_cache_collection_open_ccache (io_cache_collection,
1057*7f2fe78bSCy Schubert                                                     in_request_data, reply_data);
1058*7f2fe78bSCy Schubert 
1059*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_open_default_ccache_msg_id) {
1060*7f2fe78bSCy Schubert             err = ccs_cache_collection_open_default_ccache (io_cache_collection,
1061*7f2fe78bSCy Schubert                                                             in_request_data, reply_data);
1062*7f2fe78bSCy Schubert 
1063*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_create_ccache_msg_id) {
1064*7f2fe78bSCy Schubert             err = ccs_cache_collection_create_ccache (io_cache_collection,
1065*7f2fe78bSCy Schubert                                                       in_request_data, reply_data);
1066*7f2fe78bSCy Schubert 
1067*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_create_default_ccache_msg_id) {
1068*7f2fe78bSCy Schubert             err = ccs_cache_collection_create_default_ccache (io_cache_collection,
1069*7f2fe78bSCy Schubert                                                               in_request_data, reply_data);
1070*7f2fe78bSCy Schubert 
1071*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_create_new_ccache_msg_id) {
1072*7f2fe78bSCy Schubert             err = ccs_cache_collection_create_new_ccache (io_cache_collection,
1073*7f2fe78bSCy Schubert                                                           in_request_data, reply_data);
1074*7f2fe78bSCy Schubert 
1075*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_new_ccache_iterator_msg_id) {
1076*7f2fe78bSCy Schubert             err = ccs_cache_collection_new_ccache_iterator (io_cache_collection,
1077*7f2fe78bSCy Schubert                                                             in_client_pipe,
1078*7f2fe78bSCy Schubert                                                             in_request_data,
1079*7f2fe78bSCy Schubert                                                             reply_data);
1080*7f2fe78bSCy Schubert 
1081*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_lock_msg_id) {
1082*7f2fe78bSCy Schubert             err = ccs_cache_collection_lock (in_client_pipe, in_reply_pipe,
1083*7f2fe78bSCy Schubert                                              io_cache_collection,
1084*7f2fe78bSCy Schubert                                              in_request_data,
1085*7f2fe78bSCy Schubert                                              &will_block, reply_data);
1086*7f2fe78bSCy Schubert 
1087*7f2fe78bSCy Schubert         } else if (in_request_name == cci_context_unlock_msg_id) {
1088*7f2fe78bSCy Schubert             err = ccs_cache_collection_unlock (in_client_pipe, io_cache_collection,
1089*7f2fe78bSCy Schubert                                                in_request_data, reply_data);
1090*7f2fe78bSCy Schubert 
1091*7f2fe78bSCy Schubert         } else {
1092*7f2fe78bSCy Schubert             err = ccErrBadInternalMessage;
1093*7f2fe78bSCy Schubert         }
1094*7f2fe78bSCy Schubert     }
1095*7f2fe78bSCy Schubert 
1096*7f2fe78bSCy Schubert     if (!err) {
1097*7f2fe78bSCy Schubert         *out_will_block = will_block;
1098*7f2fe78bSCy Schubert         if (!will_block) {
1099*7f2fe78bSCy Schubert             *out_reply_data = reply_data;
1100*7f2fe78bSCy Schubert             reply_data = NULL; /* take ownership */
1101*7f2fe78bSCy Schubert         } else {
1102*7f2fe78bSCy Schubert             *out_reply_data = NULL;
1103*7f2fe78bSCy Schubert         }
1104*7f2fe78bSCy Schubert     }
1105*7f2fe78bSCy Schubert 
1106*7f2fe78bSCy Schubert     krb5int_ipc_stream_release (reply_data);
1107*7f2fe78bSCy Schubert 
1108*7f2fe78bSCy Schubert     return cci_check_error (err);
1109*7f2fe78bSCy Schubert }
1110