xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/kadm5/get_s.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: get_s.c,v 1.3 2023/06/19 21:41:44 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2006 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 <krb5_locl.h>
37 #include "kadm5_locl.h"
38 #include <assert.h>
39 
40 __RCSID("$NetBSD: get_s.c,v 1.3 2023/06/19 21:41:44 christos Exp $");
41 
42 static kadm5_ret_t
add_tl_data(kadm5_principal_ent_t ent,int16_t type,const void * data,size_t size)43 add_tl_data(kadm5_principal_ent_t ent, int16_t type,
44 	    const void *data, size_t size)
45 {
46     krb5_tl_data *tl;
47 
48     tl = calloc(1, sizeof(*tl));
49     if (tl == NULL)
50 	return _kadm5_error_code(ENOMEM);
51 
52     tl->tl_data_type = type;
53     tl->tl_data_length = size;
54     tl->tl_data_contents = malloc(size);
55     if (tl->tl_data_contents == NULL && size != 0) {
56 	free(tl);
57 	return _kadm5_error_code(ENOMEM);
58     }
59     memcpy(tl->tl_data_contents, data, size);
60 
61     tl->tl_data_next = ent->tl_data;
62     ent->tl_data = tl;
63     ent->n_tl_data++;
64 
65     return 0;
66 }
67 
68 static
69 krb5_error_code
copy_keyset_to_kadm5(kadm5_server_context * context,krb5_kvno kvno,size_t n_keys,Key * keys,krb5_salt * salt,kadm5_principal_ent_t out)70 copy_keyset_to_kadm5(kadm5_server_context *context, krb5_kvno kvno,
71 		     size_t n_keys, Key *keys, krb5_salt *salt,
72 		     kadm5_principal_ent_t out)
73 {
74     size_t i;
75     Key *key;
76     krb5_key_data *kd;
77     krb5_data *sp;
78     krb5_error_code ret = 0;
79 
80     for (i = 0; i < n_keys; i++) {
81 	key = &keys[i];
82 	kd = &out->key_data[out->n_key_data];
83 	kd->key_data_ver = 2;
84 	kd->key_data_kvno = kvno;
85 	kd->key_data_type[0] = key->key.keytype;
86 	if(key->salt)
87 	    kd->key_data_type[1] = key->salt->type;
88 	else
89 	    kd->key_data_type[1] = KRB5_PADATA_PW_SALT;
90 	/* setup key */
91 	kd->key_data_length[0] = key->key.keyvalue.length;
92 	kd->key_data_contents[0] = malloc(kd->key_data_length[0]);
93 	if(kd->key_data_contents[0] == NULL && kd->key_data_length[0] != 0){
94 	    ret = ENOMEM;
95 	    break;
96 	}
97 	memcpy(kd->key_data_contents[0], key->key.keyvalue.data,
98 	       kd->key_data_length[0]);
99 	/* setup salt */
100 	if(key->salt)
101 	    sp = &key->salt->salt;
102 	else
103 	    sp = &salt->saltvalue;
104 	kd->key_data_length[1] = sp->length;
105 	kd->key_data_contents[1] = malloc(kd->key_data_length[1]);
106 	if(kd->key_data_length[1] != 0
107 	   && kd->key_data_contents[1] == NULL) {
108 	    memset(kd->key_data_contents[0], 0, kd->key_data_length[0]);
109 	    ret = ENOMEM;
110 	    break;
111 	}
112 	memcpy(kd->key_data_contents[1], sp->data, kd->key_data_length[1]);
113 	out->n_key_data++;
114     }
115 
116     return ret;
117 }
118 
119 kadm5_ret_t
kadm5_s_get_principal(void * server_handle,krb5_principal princ,kadm5_principal_ent_t out,uint32_t mask)120 kadm5_s_get_principal(void *server_handle,
121 		      krb5_principal princ,
122 		      kadm5_principal_ent_t out,
123 		      uint32_t mask)
124 {
125     kadm5_server_context *context = server_handle;
126     kadm5_ret_t ret;
127     hdb_entry_ex ent;
128 
129     memset(&ent, 0, sizeof(ent));
130     memset(out, 0, sizeof(*out));
131 
132     if (!context->keep_open) {
133         ret = context->db->hdb_open(context->context, context->db, O_RDONLY, 0);
134 	if (ret)
135 	    return ret;
136     }
137 
138     /*
139      * We may want to attempt to recover the log on read operations, but we
140      * because the HDB/log lock order is reversed on slaves, in order to avoid
141      * lock contention from kadm5srv apps we need to make sure that the the HDB
142      * open for read-write is optimistic and attempts only a non-blocking lock,
143      * and if it doesn't get it then it should fallback to read-only.  But we
144      * don't have that option in the hdb_open() interface at this time.
145      *
146      * For now we won't attempt to recover the log.
147      */
148 
149     ret = context->db->hdb_fetch_kvno(context->context, context->db, princ,
150 				      HDB_F_DECRYPT|HDB_F_ALL_KVNOS|
151 				      HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent);
152 
153     if (!context->keep_open)
154 	context->db->hdb_close(context->context, context->db);
155     if(ret)
156 	return _kadm5_error_code(ret);
157 
158     if(mask & KADM5_PRINCIPAL)
159 	ret  = krb5_copy_principal(context->context, ent.entry.principal,
160 				   &out->principal);
161     if(ret)
162 	goto out;
163     if(mask & KADM5_PRINC_EXPIRE_TIME && ent.entry.valid_end)
164 	out->princ_expire_time = *ent.entry.valid_end;
165     if(mask & KADM5_PW_EXPIRATION && ent.entry.pw_end)
166 	out->pw_expiration = *ent.entry.pw_end;
167     if(mask & KADM5_LAST_PWD_CHANGE)
168 	hdb_entry_get_pw_change_time(&ent.entry, &out->last_pwd_change);
169     if(mask & KADM5_ATTRIBUTES){
170 	out->attributes |= ent.entry.flags.postdate ? 0 : KRB5_KDB_DISALLOW_POSTDATED;
171 	out->attributes |= ent.entry.flags.forwardable ? 0 : KRB5_KDB_DISALLOW_FORWARDABLE;
172 	out->attributes |= ent.entry.flags.initial ? KRB5_KDB_DISALLOW_TGT_BASED : 0;
173 	out->attributes |= ent.entry.flags.renewable ? 0 : KRB5_KDB_DISALLOW_RENEWABLE;
174 	out->attributes |= ent.entry.flags.proxiable ? 0 : KRB5_KDB_DISALLOW_PROXIABLE;
175 	out->attributes |= ent.entry.flags.invalid ? KRB5_KDB_DISALLOW_ALL_TIX : 0;
176 	out->attributes |= ent.entry.flags.require_preauth ? KRB5_KDB_REQUIRES_PRE_AUTH : 0;
177 	out->attributes |= ent.entry.flags.require_pwchange ? KRB5_KDB_REQUIRES_PWCHANGE : 0;
178 	out->attributes |= ent.entry.flags.server ? 0 : KRB5_KDB_DISALLOW_SVR;
179 	out->attributes |= ent.entry.flags.change_pw ? KRB5_KDB_PWCHANGE_SERVICE : 0;
180 	out->attributes |= ent.entry.flags.ok_as_delegate ? KRB5_KDB_OK_AS_DELEGATE : 0;
181 	out->attributes |= ent.entry.flags.trusted_for_delegation ? KRB5_KDB_TRUSTED_FOR_DELEGATION : 0;
182 	out->attributes |= ent.entry.flags.allow_kerberos4 ? KRB5_KDB_ALLOW_KERBEROS4 : 0;
183 	out->attributes |= ent.entry.flags.allow_digest ? KRB5_KDB_ALLOW_DIGEST : 0;
184     }
185     if(mask & KADM5_MAX_LIFE) {
186 	if(ent.entry.max_life)
187 	    out->max_life = *ent.entry.max_life;
188 	else
189 	    out->max_life = INT_MAX;
190     }
191     if(mask & KADM5_MOD_TIME) {
192 	if(ent.entry.modified_by)
193 	    out->mod_date = ent.entry.modified_by->time;
194 	else
195 	    out->mod_date = ent.entry.created_by.time;
196     }
197     if(mask & KADM5_MOD_NAME) {
198 	if(ent.entry.modified_by) {
199 	    if (ent.entry.modified_by->principal != NULL)
200 		ret = krb5_copy_principal(context->context,
201 					  ent.entry.modified_by->principal,
202 					  &out->mod_name);
203 	} else if(ent.entry.created_by.principal != NULL)
204 	    ret = krb5_copy_principal(context->context,
205 				      ent.entry.created_by.principal,
206 				      &out->mod_name);
207 	else
208 	    out->mod_name = NULL;
209     }
210     if(ret)
211 	goto out;
212 
213     if(mask & KADM5_KVNO)
214 	out->kvno = ent.entry.kvno;
215     if(mask & KADM5_MKVNO) {
216 	size_t n;
217 	out->mkvno = 0; /* XXX */
218 	for(n = 0; n < ent.entry.keys.len; n++)
219 	    if(ent.entry.keys.val[n].mkvno) {
220 		out->mkvno = *ent.entry.keys.val[n].mkvno; /* XXX this isn't right */
221 		break;
222 	    }
223     }
224 #if 0 /* XXX implement */
225     if(mask & KADM5_AUX_ATTRIBUTES)
226 	;
227     if(mask & KADM5_LAST_SUCCESS)
228 	;
229     if(mask & KADM5_LAST_FAILED)
230 	;
231     if(mask & KADM5_FAIL_AUTH_COUNT)
232 	;
233 #endif
234     if(mask & KADM5_POLICY) {
235 	HDB_extension *ext;
236 
237 	ext = hdb_find_extension(&ent.entry, choice_HDB_extension_data_policy);
238 	if (ext == NULL) {
239 	    out->policy = strdup("default");
240 	    /* It's OK if we retun NULL instead of "default" */
241 	} else {
242 	    out->policy = strdup(ext->data.u.policy);
243 	    if (out->policy == NULL) {
244 		ret = ENOMEM;
245 		goto out;
246 	    }
247 	}
248     }
249     if(mask & KADM5_MAX_RLIFE) {
250 	if(ent.entry.max_renew)
251 	    out->max_renewable_life = *ent.entry.max_renew;
252 	else
253 	    out->max_renewable_life = INT_MAX;
254     }
255     if(mask & KADM5_KEY_DATA){
256 	size_t i;
257 	size_t n_keys = ent.entry.keys.len;
258 	krb5_salt salt;
259 	HDB_extension *ext;
260 	HDB_Ext_KeySet *hist_keys = NULL;
261 
262 	/* Don't return stale keys to kadm5 clients */
263 	ret = hdb_prune_keys(context->context, &ent.entry);
264 	if (ret)
265 	    goto out;
266 	ext = hdb_find_extension(&ent.entry, choice_HDB_extension_data_hist_keys);
267 	if (ext != NULL)
268 	    hist_keys = &ext->data.u.hist_keys;
269 
270 	krb5_get_pw_salt(context->context, ent.entry.principal, &salt);
271 	for (i = 0; hist_keys != NULL && i < hist_keys->len; i++)
272 	    n_keys += hist_keys->val[i].keys.len;
273 	out->key_data = malloc(n_keys * sizeof(*out->key_data));
274 	if (out->key_data == NULL && n_keys != 0) {
275 	    ret = ENOMEM;
276 	    goto out;
277 	}
278 	out->n_key_data = 0;
279 	ret = copy_keyset_to_kadm5(context, ent.entry.kvno, ent.entry.keys.len,
280 				   ent.entry.keys.val, &salt, out);
281 	if (ret)
282 	    goto out;
283 	for (i = 0; hist_keys != NULL && i < hist_keys->len; i++) {
284 	    ret = copy_keyset_to_kadm5(context, hist_keys->val[i].kvno,
285 				       hist_keys->val[i].keys.len,
286 				       hist_keys->val[i].keys.val,
287 				       &salt, out);
288 	    if (ret)
289 		goto out;
290 	}
291 	krb5_free_salt(context->context, salt);
292 	assert( out->n_key_data == n_keys );
293     }
294     if (ret)
295 	goto out;
296     if(mask & KADM5_TL_DATA) {
297 	time_t last_pw_expire;
298 	const HDB_Ext_PKINIT_acl *acl;
299 	const HDB_Ext_Aliases *aliases;
300 
301 	ret = hdb_entry_get_pw_change_time(&ent.entry, &last_pw_expire);
302 	if (ret == 0 && last_pw_expire) {
303 	    unsigned char buf[4];
304 	    _krb5_put_int(buf, last_pw_expire, sizeof(buf));
305 	    ret = add_tl_data(out, KRB5_TL_LAST_PWD_CHANGE, buf, sizeof(buf));
306 	}
307 	if (ret)
308 	    goto out;
309 	/*
310 	 * If the client was allowed to get key data, let it have the
311 	 * password too.
312 	 */
313 	if (mask & KADM5_KEY_DATA) {
314 	    heim_utf8_string pw;
315 
316 	    ret = hdb_entry_get_password(context->context,
317 					 context->db, &ent.entry, &pw);
318 	    if (ret == 0) {
319 		(void) add_tl_data(out, KRB5_TL_PASSWORD, pw, strlen(pw) + 1);
320 		free(pw);
321 	    }
322 	    krb5_clear_error_message(context->context);
323 	}
324 
325 	ret = hdb_entry_get_pkinit_acl(&ent.entry, &acl);
326 	if (ret == 0 && acl) {
327 	    krb5_data buf;
328 	    size_t len;
329 
330 	    ASN1_MALLOC_ENCODE(HDB_Ext_PKINIT_acl, buf.data, buf.length,
331 				acl, &len, ret);
332 	    if (ret)
333 		goto out;
334 	    if (len != buf.length)
335 		krb5_abortx(context->context,
336 			    "internal ASN.1 encoder error");
337 	    ret = add_tl_data(out, KRB5_TL_PKINIT_ACL, buf.data, buf.length);
338 	    free(buf.data);
339 	    if (ret)
340 		goto out;
341 	}
342 	if (ret)
343 	    goto out;
344 
345 	ret = hdb_entry_get_aliases(&ent.entry, &aliases);
346 	if (ret == 0 && aliases) {
347 	    krb5_data buf;
348 	    size_t len;
349 
350 	    ASN1_MALLOC_ENCODE(HDB_Ext_Aliases, buf.data, buf.length,
351 			       aliases, &len, ret);
352 	    if (ret)
353 		goto out;
354 	    if (len != buf.length)
355 		krb5_abortx(context->context,
356 			    "internal ASN.1 encoder error");
357 	    ret = add_tl_data(out, KRB5_TL_ALIASES, buf.data, buf.length);
358 	    free(buf.data);
359 	    if (ret)
360 		goto out;
361 	}
362 	if (ret)
363 	    goto out;
364     }
365 
366  out:
367     if (ret)
368         kadm5_free_principal_ent(context, out);
369     hdb_free_entry(context->context, &ent);
370 
371     return _kadm5_error_code(ret);
372 }
373