1 /*	$NetBSD: marshall.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 1999 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "kadm5_locl.h"
37 
38 __RCSID("NetBSD");
39 
40 kadm5_ret_t
kadm5_store_key_data(krb5_storage * sp,krb5_key_data * key)41 kadm5_store_key_data(krb5_storage *sp,
42 		     krb5_key_data *key)
43 {
44     krb5_data c;
45     krb5_store_int32(sp, key->key_data_ver);
46     krb5_store_int32(sp, key->key_data_kvno);
47     krb5_store_int32(sp, key->key_data_type[0]);
48     c.length = key->key_data_length[0];
49     c.data = key->key_data_contents[0];
50     krb5_store_data(sp, c);
51     krb5_store_int32(sp, key->key_data_type[1]);
52     c.length = key->key_data_length[1];
53     c.data = key->key_data_contents[1];
54     krb5_store_data(sp, c);
55     return 0;
56 }
57 
58 kadm5_ret_t
kadm5_ret_key_data(krb5_storage * sp,krb5_key_data * key)59 kadm5_ret_key_data(krb5_storage *sp,
60 		   krb5_key_data *key)
61 {
62     krb5_data c;
63     int32_t tmp;
64     krb5_ret_int32(sp, &tmp);
65     key->key_data_ver = tmp;
66     krb5_ret_int32(sp, &tmp);
67     key->key_data_kvno = tmp;
68     krb5_ret_int32(sp, &tmp);
69     key->key_data_type[0] = tmp;
70     krb5_ret_data(sp, &c);
71     key->key_data_length[0] = c.length;
72     key->key_data_contents[0] = c.data;
73     krb5_ret_int32(sp, &tmp);
74     key->key_data_type[1] = tmp;
75     krb5_ret_data(sp, &c);
76     key->key_data_length[1] = c.length;
77     key->key_data_contents[1] = c.data;
78     return 0;
79 }
80 
81 kadm5_ret_t
kadm5_store_tl_data(krb5_storage * sp,krb5_tl_data * tl)82 kadm5_store_tl_data(krb5_storage *sp,
83 		    krb5_tl_data *tl)
84 {
85     krb5_data c;
86     krb5_store_int32(sp, tl->tl_data_type);
87     c.length = tl->tl_data_length;
88     c.data = tl->tl_data_contents;
89     krb5_store_data(sp, c);
90     return 0;
91 }
92 
93 kadm5_ret_t
kadm5_ret_tl_data(krb5_storage * sp,krb5_tl_data * tl)94 kadm5_ret_tl_data(krb5_storage *sp,
95 		  krb5_tl_data *tl)
96 {
97     krb5_data c;
98     int32_t tmp;
99     krb5_ret_int32(sp, &tmp);
100     tl->tl_data_type = tmp;
101     krb5_ret_data(sp, &c);
102     tl->tl_data_length = c.length;
103     tl->tl_data_contents = c.data;
104     return 0;
105 }
106 
107 static kadm5_ret_t
store_principal_ent(krb5_storage * sp,kadm5_principal_ent_t princ,uint32_t mask)108 store_principal_ent(krb5_storage *sp,
109 		    kadm5_principal_ent_t princ,
110 		    uint32_t mask)
111 {
112     int i;
113 
114     if (mask & KADM5_PRINCIPAL)
115 	krb5_store_principal(sp, princ->principal);
116     if (mask & KADM5_PRINC_EXPIRE_TIME)
117 	krb5_store_int32(sp, princ->princ_expire_time);
118     if (mask & KADM5_PW_EXPIRATION)
119 	krb5_store_int32(sp, princ->pw_expiration);
120     if (mask & KADM5_LAST_PWD_CHANGE)
121 	krb5_store_int32(sp, princ->last_pwd_change);
122     if (mask & KADM5_MAX_LIFE)
123 	krb5_store_int32(sp, princ->max_life);
124     if (mask & KADM5_MOD_NAME) {
125 	krb5_store_int32(sp, princ->mod_name != NULL);
126 	if(princ->mod_name)
127 	    krb5_store_principal(sp, princ->mod_name);
128     }
129     if (mask & KADM5_MOD_TIME)
130 	krb5_store_int32(sp, princ->mod_date);
131     if (mask & KADM5_ATTRIBUTES)
132 	krb5_store_int32(sp, princ->attributes);
133     if (mask & KADM5_KVNO)
134 	krb5_store_int32(sp, princ->kvno);
135     if (mask & KADM5_MKVNO)
136 	krb5_store_int32(sp, princ->mkvno);
137     if (mask & KADM5_POLICY) {
138 	krb5_store_int32(sp, princ->policy != NULL);
139 	if(princ->policy)
140 	    krb5_store_string(sp, princ->policy);
141     }
142     if (mask & KADM5_AUX_ATTRIBUTES)
143 	krb5_store_int32(sp, princ->aux_attributes);
144     if (mask & KADM5_MAX_RLIFE)
145 	krb5_store_int32(sp, princ->max_renewable_life);
146     if (mask & KADM5_LAST_SUCCESS)
147 	krb5_store_int32(sp, princ->last_success);
148     if (mask & KADM5_LAST_FAILED)
149 	krb5_store_int32(sp, princ->last_failed);
150     if (mask & KADM5_FAIL_AUTH_COUNT)
151 	krb5_store_int32(sp, princ->fail_auth_count);
152     if (mask & KADM5_KEY_DATA) {
153 	krb5_store_int32(sp, princ->n_key_data);
154 	for(i = 0; i < princ->n_key_data; i++)
155 	    kadm5_store_key_data(sp, &princ->key_data[i]);
156     }
157     if (mask & KADM5_TL_DATA) {
158 	krb5_tl_data *tp;
159 
160 	krb5_store_int32(sp, princ->n_tl_data);
161 	for(tp = princ->tl_data; tp; tp = tp->tl_data_next)
162 	    kadm5_store_tl_data(sp, tp);
163     }
164     return 0;
165 }
166 
167 
168 kadm5_ret_t
kadm5_store_principal_ent(krb5_storage * sp,kadm5_principal_ent_t princ)169 kadm5_store_principal_ent(krb5_storage *sp,
170 			  kadm5_principal_ent_t princ)
171 {
172     return store_principal_ent (sp, princ, ~0);
173 }
174 
175 kadm5_ret_t
kadm5_store_principal_ent_mask(krb5_storage * sp,kadm5_principal_ent_t princ,uint32_t mask)176 kadm5_store_principal_ent_mask(krb5_storage *sp,
177 			       kadm5_principal_ent_t princ,
178 			       uint32_t mask)
179 {
180     krb5_store_int32(sp, mask);
181     return store_principal_ent (sp, princ, mask);
182 }
183 
184 static kadm5_ret_t
ret_principal_ent(krb5_storage * sp,kadm5_principal_ent_t princ,uint32_t mask)185 ret_principal_ent(krb5_storage *sp,
186 		  kadm5_principal_ent_t princ,
187 		  uint32_t mask)
188 {
189     int i;
190     int32_t tmp;
191 
192     if (mask & KADM5_PRINCIPAL)
193 	krb5_ret_principal(sp, &princ->principal);
194 
195     if (mask & KADM5_PRINC_EXPIRE_TIME) {
196 	krb5_ret_int32(sp, &tmp);
197 	princ->princ_expire_time = tmp;
198     }
199     if (mask & KADM5_PW_EXPIRATION) {
200 	krb5_ret_int32(sp, &tmp);
201 	princ->pw_expiration = tmp;
202     }
203     if (mask & KADM5_LAST_PWD_CHANGE) {
204 	krb5_ret_int32(sp, &tmp);
205 	princ->last_pwd_change = tmp;
206     }
207     if (mask & KADM5_MAX_LIFE) {
208 	krb5_ret_int32(sp, &tmp);
209 	princ->max_life = tmp;
210     }
211     if (mask & KADM5_MOD_NAME) {
212 	krb5_ret_int32(sp, &tmp);
213 	if(tmp)
214 	    krb5_ret_principal(sp, &princ->mod_name);
215 	else
216 	    princ->mod_name = NULL;
217     }
218     if (mask & KADM5_MOD_TIME) {
219 	krb5_ret_int32(sp, &tmp);
220 	princ->mod_date = tmp;
221     }
222     if (mask & KADM5_ATTRIBUTES) {
223 	krb5_ret_int32(sp, &tmp);
224 	princ->attributes = tmp;
225     }
226     if (mask & KADM5_KVNO) {
227 	krb5_ret_int32(sp, &tmp);
228 	princ->kvno = tmp;
229     }
230     if (mask & KADM5_MKVNO) {
231 	krb5_ret_int32(sp, &tmp);
232 	princ->mkvno = tmp;
233     }
234     if (mask & KADM5_POLICY) {
235 	krb5_ret_int32(sp, &tmp);
236 	if(tmp)
237 	    krb5_ret_string(sp, &princ->policy);
238 	else
239 	    princ->policy = NULL;
240     }
241     if (mask & KADM5_AUX_ATTRIBUTES) {
242 	krb5_ret_int32(sp, &tmp);
243 	princ->aux_attributes = tmp;
244     }
245     if (mask & KADM5_MAX_RLIFE) {
246 	krb5_ret_int32(sp, &tmp);
247 	princ->max_renewable_life = tmp;
248     }
249     if (mask & KADM5_LAST_SUCCESS) {
250 	krb5_ret_int32(sp, &tmp);
251 	princ->last_success = tmp;
252     }
253     if (mask & KADM5_LAST_FAILED) {
254 	krb5_ret_int32(sp, &tmp);
255 	princ->last_failed = tmp;
256     }
257     if (mask & KADM5_FAIL_AUTH_COUNT) {
258 	krb5_ret_int32(sp, &tmp);
259 	princ->fail_auth_count = tmp;
260     }
261     if (mask & KADM5_KEY_DATA) {
262 	krb5_ret_int32(sp, &tmp);
263 	princ->n_key_data = tmp;
264 	princ->key_data = malloc(princ->n_key_data * sizeof(*princ->key_data));
265 	if (princ->key_data == NULL && princ->n_key_data != 0)
266 	    return ENOMEM;
267 	for(i = 0; i < princ->n_key_data; i++)
268 	    kadm5_ret_key_data(sp, &princ->key_data[i]);
269     }
270     if (mask & KADM5_TL_DATA) {
271 	krb5_ret_int32(sp, &tmp);
272 	princ->n_tl_data = tmp;
273 	princ->tl_data = NULL;
274 	for(i = 0; i < princ->n_tl_data; i++){
275 	    krb5_tl_data *tp = malloc(sizeof(*tp));
276 	    if (tp == NULL)
277 		return ENOMEM;
278 	    kadm5_ret_tl_data(sp, tp);
279 	    tp->tl_data_next = princ->tl_data;
280 	    princ->tl_data = tp;
281 	}
282     }
283     return 0;
284 }
285 
286 kadm5_ret_t
kadm5_ret_principal_ent(krb5_storage * sp,kadm5_principal_ent_t princ)287 kadm5_ret_principal_ent(krb5_storage *sp,
288 			kadm5_principal_ent_t princ)
289 {
290     return ret_principal_ent (sp, princ, ~0);
291 }
292 
293 kadm5_ret_t
kadm5_ret_principal_ent_mask(krb5_storage * sp,kadm5_principal_ent_t princ,uint32_t * mask)294 kadm5_ret_principal_ent_mask(krb5_storage *sp,
295 			     kadm5_principal_ent_t princ,
296 			     uint32_t *mask)
297 {
298     int32_t tmp;
299 
300     krb5_ret_int32 (sp, &tmp);
301     *mask = tmp;
302     return ret_principal_ent (sp, princ, *mask);
303 }
304 
305 kadm5_ret_t
_kadm5_marshal_params(krb5_context context,kadm5_config_params * params,krb5_data * out)306 _kadm5_marshal_params(krb5_context context,
307 		      kadm5_config_params *params,
308 		      krb5_data *out)
309 {
310     krb5_storage *sp = krb5_storage_emem();
311 
312     krb5_store_int32(sp, params->mask & (KADM5_CONFIG_REALM));
313 
314     if(params->mask & KADM5_CONFIG_REALM)
315 	krb5_store_string(sp, params->realm);
316     krb5_storage_to_data(sp, out);
317     krb5_storage_free(sp);
318 
319     return 0;
320 }
321 
322 kadm5_ret_t
_kadm5_unmarshal_params(krb5_context context,krb5_data * in,kadm5_config_params * params)323 _kadm5_unmarshal_params(krb5_context context,
324 			krb5_data *in,
325 			kadm5_config_params *params)
326 {
327     krb5_error_code ret;
328     krb5_storage *sp;
329     int32_t mask;
330 
331     sp = krb5_storage_from_data(in);
332     if (sp == NULL)
333 	return ENOMEM;
334 
335     ret = krb5_ret_int32(sp, &mask);
336     if (ret)
337 	goto out;
338     params->mask = mask;
339 
340     if(params->mask & KADM5_CONFIG_REALM)
341 	ret = krb5_ret_string(sp, &params->realm);
342  out:
343     krb5_storage_free(sp);
344 
345     return ret;
346 }
347