Properly terminate ifdef conditional in krb5-types.h
[heimdal.git] / lib / kadm5 / get_s.c
bloba4e1d82592c916fc67e371a689c71e0be8031da6
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kadm5_locl.h"
35 #include <assert.h>
37 RCSID("$Id$");
39 static kadm5_ret_t
40 add_tl_data(kadm5_principal_ent_t ent, int16_t type,
41 const void *data, size_t size)
43 krb5_tl_data *tl;
45 tl = calloc(1, sizeof(*tl));
46 if (tl == NULL)
47 return _kadm5_error_code(ENOMEM);
49 tl->tl_data_type = type;
50 tl->tl_data_length = size;
51 tl->tl_data_contents = malloc(size);
52 if (tl->tl_data_contents == NULL && size != 0) {
53 free(tl);
54 return _kadm5_error_code(ENOMEM);
56 memcpy(tl->tl_data_contents, data, size);
58 tl->tl_data_next = ent->tl_data;
59 ent->tl_data = tl;
60 ent->n_tl_data++;
62 return 0;
65 KRB5_LIB_FUNCTION krb5_ssize_t KRB5_LIB_CALL
66 _krb5_put_int(void *buffer, unsigned long value, size_t size); /* XXX */
68 static
69 krb5_error_code
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)
74 size_t i;
75 Key *key;
76 krb5_key_data *kd;
77 krb5_data *sp;
78 krb5_error_code ret = 0;
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;
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;
112 memcpy(kd->key_data_contents[1], sp->data, kd->key_data_length[1]);
113 out->n_key_data++;
116 return ret;
119 kadm5_ret_t
120 kadm5_s_get_principal(void *server_handle,
121 krb5_principal princ,
122 kadm5_principal_ent_t out,
123 uint32_t mask)
125 kadm5_server_context *context = server_handle;
126 kadm5_ret_t ret;
127 hdb_entry_ex ent;
129 memset(&ent, 0, sizeof(ent));
131 if (!context->keep_open) {
132 ret = context->db->hdb_open(context->context, context->db, O_RDONLY, 0);
133 if(ret)
134 return ret;
136 ret = context->db->hdb_fetch_kvno(context->context, context->db, princ,
137 HDB_F_DECRYPT|HDB_F_ALL_KVNOS|
138 HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent);
139 if (!context->keep_open)
140 context->db->hdb_close(context->context, context->db);
141 if(ret)
142 return _kadm5_error_code(ret);
144 memset(out, 0, sizeof(*out));
145 if(mask & KADM5_PRINCIPAL)
146 ret = krb5_copy_principal(context->context, ent.entry.principal,
147 &out->principal);
148 if(ret)
149 goto out;
150 if(mask & KADM5_PRINC_EXPIRE_TIME && ent.entry.valid_end)
151 out->princ_expire_time = *ent.entry.valid_end;
152 if(mask & KADM5_PW_EXPIRATION && ent.entry.pw_end)
153 out->pw_expiration = *ent.entry.pw_end;
154 if(mask & KADM5_LAST_PWD_CHANGE)
155 hdb_entry_get_pw_change_time(&ent.entry, &out->last_pwd_change);
156 if(mask & KADM5_ATTRIBUTES){
157 out->attributes |= ent.entry.flags.postdate ? 0 : KRB5_KDB_DISALLOW_POSTDATED;
158 out->attributes |= ent.entry.flags.forwardable ? 0 : KRB5_KDB_DISALLOW_FORWARDABLE;
159 out->attributes |= ent.entry.flags.initial ? KRB5_KDB_DISALLOW_TGT_BASED : 0;
160 out->attributes |= ent.entry.flags.renewable ? 0 : KRB5_KDB_DISALLOW_RENEWABLE;
161 out->attributes |= ent.entry.flags.proxiable ? 0 : KRB5_KDB_DISALLOW_PROXIABLE;
162 out->attributes |= ent.entry.flags.invalid ? KRB5_KDB_DISALLOW_ALL_TIX : 0;
163 out->attributes |= ent.entry.flags.require_preauth ? KRB5_KDB_REQUIRES_PRE_AUTH : 0;
164 out->attributes |= ent.entry.flags.require_pwchange ? KRB5_KDB_REQUIRES_PWCHANGE : 0;
165 out->attributes |= ent.entry.flags.server ? 0 : KRB5_KDB_DISALLOW_SVR;
166 out->attributes |= ent.entry.flags.change_pw ? KRB5_KDB_PWCHANGE_SERVICE : 0;
167 out->attributes |= ent.entry.flags.ok_as_delegate ? KRB5_KDB_OK_AS_DELEGATE : 0;
168 out->attributes |= ent.entry.flags.trusted_for_delegation ? KRB5_KDB_TRUSTED_FOR_DELEGATION : 0;
169 out->attributes |= ent.entry.flags.allow_kerberos4 ? KRB5_KDB_ALLOW_KERBEROS4 : 0;
170 out->attributes |= ent.entry.flags.allow_digest ? KRB5_KDB_ALLOW_DIGEST : 0;
172 if(mask & KADM5_MAX_LIFE) {
173 if(ent.entry.max_life)
174 out->max_life = *ent.entry.max_life;
175 else
176 out->max_life = INT_MAX;
178 if(mask & KADM5_MOD_TIME) {
179 if(ent.entry.modified_by)
180 out->mod_date = ent.entry.modified_by->time;
181 else
182 out->mod_date = ent.entry.created_by.time;
184 if(mask & KADM5_MOD_NAME) {
185 if(ent.entry.modified_by) {
186 if (ent.entry.modified_by->principal != NULL)
187 ret = krb5_copy_principal(context->context,
188 ent.entry.modified_by->principal,
189 &out->mod_name);
190 } else if(ent.entry.created_by.principal != NULL)
191 ret = krb5_copy_principal(context->context,
192 ent.entry.created_by.principal,
193 &out->mod_name);
194 else
195 out->mod_name = NULL;
197 if(ret)
198 goto out;
200 if(mask & KADM5_KVNO)
201 out->kvno = ent.entry.kvno;
202 if(mask & KADM5_MKVNO) {
203 size_t n;
204 out->mkvno = 0; /* XXX */
205 for(n = 0; n < ent.entry.keys.len; n++)
206 if(ent.entry.keys.val[n].mkvno) {
207 out->mkvno = *ent.entry.keys.val[n].mkvno; /* XXX this isn't right */
208 break;
211 #if 0 /* XXX implement */
212 if(mask & KADM5_AUX_ATTRIBUTES)
214 if(mask & KADM5_LAST_SUCCESS)
216 if(mask & KADM5_LAST_FAILED)
218 if(mask & KADM5_FAIL_AUTH_COUNT)
220 #endif
221 if(mask & KADM5_POLICY) {
222 HDB_extension *ext;
224 ext = hdb_find_extension(&ent.entry, choice_HDB_extension_data_policy);
225 if (ext == NULL) {
226 out->policy = strdup("default");
227 /* It's OK if we retun NULL instead of "default" */
228 } else {
229 out->policy = strdup(ext->data.u.policy);
230 if (out->policy == NULL) {
231 ret = ENOMEM;
232 goto out;
236 if(mask & KADM5_MAX_RLIFE) {
237 if(ent.entry.max_renew)
238 out->max_renewable_life = *ent.entry.max_renew;
239 else
240 out->max_renewable_life = INT_MAX;
242 if(mask & KADM5_KEY_DATA){
243 size_t i;
244 size_t n_keys = ent.entry.keys.len;
245 krb5_salt salt;
246 HDB_extension *ext;
247 HDB_Ext_KeySet *hist_keys = NULL;
249 ext = hdb_find_extension(&ent.entry, choice_HDB_extension_data_hist_keys);
250 if (ext != NULL)
251 hist_keys = &ext->data.u.hist_keys;
253 krb5_get_pw_salt(context->context, ent.entry.principal, &salt);
254 for (i = 0; hist_keys != NULL && i < hist_keys->len; i++)
255 n_keys += hist_keys->val[i].keys.len;
256 out->key_data = malloc(n_keys * sizeof(*out->key_data));
257 if (out->key_data == NULL && n_keys != 0) {
258 ret = ENOMEM;
259 goto out;
261 out->n_key_data = 0;
262 ret = copy_keyset_to_kadm5(context, ent.entry.kvno, ent.entry.keys.len,
263 ent.entry.keys.val, &salt, out);
264 if (ret)
265 goto out;
266 for (i = 0; hist_keys != NULL && i < hist_keys->len; i++) {
267 ret = copy_keyset_to_kadm5(context, hist_keys->val[i].kvno,
268 hist_keys->val[i].keys.len,
269 hist_keys->val[i].keys.val,
270 &salt, out);
271 if (ret)
272 goto out;
274 krb5_free_salt(context->context, salt);
275 assert( out->n_key_data == n_keys );
277 if(ret){
278 kadm5_free_principal_ent(context, out);
279 goto out;
281 if(mask & KADM5_TL_DATA) {
282 time_t last_pw_expire;
283 const HDB_Ext_PKINIT_acl *acl;
284 const HDB_Ext_Aliases *aliases;
286 ret = hdb_entry_get_pw_change_time(&ent.entry, &last_pw_expire);
287 if (ret == 0 && last_pw_expire) {
288 unsigned char buf[4];
289 _krb5_put_int(buf, last_pw_expire, sizeof(buf));
290 ret = add_tl_data(out, KRB5_TL_LAST_PWD_CHANGE, buf, sizeof(buf));
292 if(ret){
293 kadm5_free_principal_ent(context, out);
294 goto out;
297 * If the client was allowed to get key data, let it have the
298 * password too.
300 if(mask & KADM5_KEY_DATA) {
301 heim_utf8_string pw;
303 ret = hdb_entry_get_password(context->context,
304 context->db, &ent.entry, &pw);
305 if (ret == 0) {
306 ret = add_tl_data(out, KRB5_TL_PASSWORD, pw, strlen(pw) + 1);
307 free(pw);
309 krb5_clear_error_message(context->context);
312 ret = hdb_entry_get_pkinit_acl(&ent.entry, &acl);
313 if (ret == 0 && acl) {
314 krb5_data buf;
315 size_t len;
317 ASN1_MALLOC_ENCODE(HDB_Ext_PKINIT_acl, buf.data, buf.length,
318 acl, &len, ret);
319 if (ret) {
320 kadm5_free_principal_ent(context, out);
321 goto out;
323 if (len != buf.length)
324 krb5_abortx(context->context,
325 "internal ASN.1 encoder error");
326 ret = add_tl_data(out, KRB5_TL_PKINIT_ACL, buf.data, buf.length);
327 free(buf.data);
328 if (ret) {
329 kadm5_free_principal_ent(context, out);
330 goto out;
333 if(ret){
334 kadm5_free_principal_ent(context, out);
335 goto out;
338 ret = hdb_entry_get_aliases(&ent.entry, &aliases);
339 if (ret == 0 && aliases) {
340 krb5_data buf;
341 size_t len;
343 ASN1_MALLOC_ENCODE(HDB_Ext_Aliases, buf.data, buf.length,
344 aliases, &len, ret);
345 if (ret) {
346 kadm5_free_principal_ent(context, out);
347 goto out;
349 if (len != buf.length)
350 krb5_abortx(context->context,
351 "internal ASN.1 encoder error");
352 ret = add_tl_data(out, KRB5_TL_ALIASES, buf.data, buf.length);
353 free(buf.data);
354 if (ret) {
355 kadm5_free_principal_ent(context, out);
356 goto out;
359 if(ret){
360 kadm5_free_principal_ent(context, out);
361 goto out;
365 out:
366 hdb_free_entry(context->context, &ent);
368 return _kadm5_error_code(ret);