xref: /freebsd/crypto/krb5/src/ccapi/lib/ccapi_credentials_iterator.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* ccapi/lib/ccapi_credentials_iterator.c */
2 /*
3  * Copyright 2006 Massachusetts Institute of Technology.
4  * All Rights Reserved.
5  *
6  * Export of this software from the United States of America may
7  * require a specific license from the United States Government.
8  * It is the responsibility of any person or organization contemplating
9  * export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  */
25 
26 #include "ccapi_credentials_iterator.h"
27 #include "ccapi_credentials.h"
28 #include "ccapi_ipc.h"
29 
30 /* ------------------------------------------------------------------------ */
31 
32 typedef struct cci_credentials_iterator_d {
33     cc_credentials_iterator_f *functions;
34 #if TARGET_OS_MAC
35     cc_credentials_iterator_f *vector_functions;
36 #endif
37     cci_identifier_t identifier;
38     cc_uint32 compat_version;
39 } *cci_credentials_iterator_t;
40 
41 /* ------------------------------------------------------------------------ */
42 
43 struct cci_credentials_iterator_d cci_credentials_iterator_initializer = {
44     NULL
45     VECTOR_FUNCTIONS_INITIALIZER,
46     NULL,
47     0
48 };
49 
50 cc_credentials_iterator_f cci_credentials_iterator_f_initializer = {
51     ccapi_credentials_iterator_release,
52     ccapi_credentials_iterator_next,
53     ccapi_credentials_iterator_clone
54 };
55 
56 /* ------------------------------------------------------------------------ */
57 
58 cc_int32 cci_credentials_iterator_new (cc_credentials_iterator_t *out_credentials_iterator,
59                                        cci_identifier_t           in_identifier)
60 {
61     cc_int32 err = ccNoError;
62     cci_credentials_iterator_t credentials_iterator = NULL;
63 
64     if (!out_credentials_iterator) { err = cci_check_error (ccErrBadParam); }
65     if (!in_identifier           ) { err = cci_check_error (ccErrBadParam); }
66 
67     if (!err) {
68         credentials_iterator = malloc (sizeof (*credentials_iterator));
69         if (credentials_iterator) {
70             *credentials_iterator = cci_credentials_iterator_initializer;
71         } else {
72             err = cci_check_error (ccErrNoMem);
73         }
74     }
75 
76     if (!err) {
77         credentials_iterator->functions = malloc (sizeof (*credentials_iterator->functions));
78         if (credentials_iterator->functions) {
79             *credentials_iterator->functions = cci_credentials_iterator_f_initializer;
80         } else {
81             err = cci_check_error (ccErrNoMem);
82         }
83     }
84 
85     if (!err) {
86         err = cci_identifier_copy (&credentials_iterator->identifier, in_identifier);
87     }
88 
89     if (!err) {
90         *out_credentials_iterator = (cc_credentials_iterator_t) credentials_iterator;
91         credentials_iterator = NULL; /* take ownership */
92     }
93 
94     if (credentials_iterator) { ccapi_credentials_iterator_release ((cc_credentials_iterator_t) credentials_iterator); }
95 
96     return cci_check_error (err);
97 }
98 
99 /* ------------------------------------------------------------------------ */
100 
101 cc_int32 cci_credentials_iterator_write (cc_credentials_iterator_t in_credentials_iterator,
102                                          k5_ipc_stream              in_stream)
103 {
104     cc_int32 err = ccNoError;
105     cci_credentials_iterator_t credentials_iterator = (cci_credentials_iterator_t) in_credentials_iterator;
106 
107     if (!in_credentials_iterator) { err = cci_check_error (ccErrBadParam); }
108     if (!in_stream         ) { err = cci_check_error (ccErrBadParam); }
109 
110     if (!err) {
111         err = cci_identifier_write (credentials_iterator->identifier, in_stream);
112     }
113 
114     return cci_check_error (err);
115 }
116 
117 /* ------------------------------------------------------------------------ */
118 
119 cc_int32 ccapi_credentials_iterator_release (cc_credentials_iterator_t io_credentials_iterator)
120 {
121     cc_int32 err = ccNoError;
122     cci_credentials_iterator_t credentials_iterator = (cci_credentials_iterator_t) io_credentials_iterator;
123 
124     if (!io_credentials_iterator) { err = ccErrBadParam; }
125 
126     if (!err) {
127         err =  cci_ipc_send (cci_credentials_iterator_release_msg_id,
128                              credentials_iterator->identifier,
129                              NULL,
130                              NULL);
131         if (err) {
132             cci_debug_printf ("%s: cci_ipc_send failed with error %d",
133                              __FUNCTION__, err);
134             err = ccNoError;
135         }
136     }
137 
138     if (!err) {
139         free ((char *) credentials_iterator->functions);
140         cci_identifier_release (credentials_iterator->identifier);
141         free (credentials_iterator);
142     }
143 
144     return err;
145 }
146 
147 /* ------------------------------------------------------------------------ */
148 
149 cc_int32 ccapi_credentials_iterator_next (cc_credentials_iterator_t  in_credentials_iterator,
150                                           cc_credentials_t          *out_credentials)
151 {
152     cc_int32 err = ccNoError;
153     cci_credentials_iterator_t credentials_iterator = (cci_credentials_iterator_t) in_credentials_iterator;
154     k5_ipc_stream reply = NULL;
155 
156     if (!in_credentials_iterator) { err = cci_check_error (ccErrBadParam); }
157     if (!out_credentials        ) { err = cci_check_error (ccErrBadParam); }
158 
159     if (!err) {
160         err =  cci_ipc_send (cci_credentials_iterator_next_msg_id,
161                              credentials_iterator->identifier,
162                              NULL,
163                              &reply);
164     }
165 
166     if (!err) {
167         err = cci_credentials_read (out_credentials, reply);
168     }
169 
170     krb5int_ipc_stream_release (reply);
171 
172     return cci_check_error (err);
173 }
174 
175 /* ------------------------------------------------------------------------ */
176 
177 cc_int32 ccapi_credentials_iterator_clone (cc_credentials_iterator_t  in_credentials_iterator,
178                                            cc_credentials_iterator_t *out_credentials_iterator)
179 {
180     cc_int32 err = ccNoError;
181     cci_credentials_iterator_t credentials_iterator = (cci_credentials_iterator_t) in_credentials_iterator;
182     k5_ipc_stream reply = NULL;
183     cci_identifier_t identifier = NULL;
184 
185     if (!in_credentials_iterator ) { err = cci_check_error (ccErrBadParam); }
186     if (!out_credentials_iterator) { err = cci_check_error (ccErrBadParam); }
187 
188     if (!err) {
189         err =  cci_ipc_send (cci_credentials_iterator_next_msg_id,
190                              credentials_iterator->identifier,
191                              NULL,
192                              &reply);
193     }
194 
195     if (!err) {
196         err =  cci_identifier_read (&identifier, reply);
197     }
198 
199     if (!err) {
200         err = cci_credentials_iterator_new (out_credentials_iterator, identifier);
201     }
202 
203     krb5int_ipc_stream_release (reply);
204     cci_identifier_release (identifier);
205 
206     return cci_check_error (err);
207 }
208 
209 #ifdef TARGET_OS_MAC
210 #pragma mark -
211 #endif
212 
213 /* ------------------------------------------------------------------------ */
214 
215 cc_int32 cci_credentials_iterator_get_compat_version (cc_credentials_iterator_t  in_credentials_iterator,
216                                                       cc_uint32                 *out_compat_version)
217 {
218     cc_int32 err = ccNoError;
219     cci_credentials_iterator_t credentials_iterator = (cci_credentials_iterator_t) in_credentials_iterator;
220 
221     if (!in_credentials_iterator) { err = cci_check_error (ccErrBadParam); }
222     if (!out_compat_version     ) { err = cci_check_error (ccErrBadParam); }
223 
224     if (!err) {
225         *out_compat_version = credentials_iterator->compat_version;
226     }
227 
228     return cci_check_error (err);
229 }
230 
231 /* ------------------------------------------------------------------------ */
232 
233 cc_int32 cci_credentials_iterator_set_compat_version (cc_credentials_iterator_t io_credentials_iterator,
234                                                       cc_uint32                 in_compat_version)
235 {
236     cc_int32 err = ccNoError;
237     cci_credentials_iterator_t credentials_iterator = (cci_credentials_iterator_t) io_credentials_iterator;
238 
239     if (!io_credentials_iterator) { err = cci_check_error (ccErrBadParam); }
240 
241     if (!err) {
242         credentials_iterator->compat_version = in_compat_version;
243     }
244 
245     return cci_check_error (err);
246 }
247