2 * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 #include "kadm5_locl.h"
40 #define set_value(X, V) do { if((X) == NULL) (X) = malloc(sizeof(*(X))); *(X) = V; } while(0)
41 #define set_null(X) do { if((X) != NULL) free((X)); (X) = NULL; } while (0)
44 attr_to_flags(unsigned attr, HDBFlags *flags)
46 flags->postdate = !(attr & KRB5_KDB_DISALLOW_POSTDATED);
47 flags->forwardable = !(attr & KRB5_KDB_DISALLOW_FORWARDABLE);
48 flags->initial = !!(attr & KRB5_KDB_DISALLOW_TGT_BASED);
49 flags->renewable = !(attr & KRB5_KDB_DISALLOW_RENEWABLE);
50 flags->proxiable = !(attr & KRB5_KDB_DISALLOW_PROXIABLE);
52 flags->invalid = !!(attr & KRB5_KDB_DISALLOW_ALL_TIX);
53 flags->require_preauth = !!(attr & KRB5_KDB_REQUIRES_PRE_AUTH);
54 flags->require_pwchange = !!(attr & KRB5_KDB_REQUIRES_PWCHANGE);
56 flags->server = !(attr & KRB5_KDB_DISALLOW_SVR);
57 flags->change_pw = !!(attr & KRB5_KDB_PWCHANGE_SERVICE);
58 flags->client = !(attr & KRB5_KDB_DISALLOW_CLIENT);
59 flags->ok_as_delegate = !!(attr & KRB5_KDB_OK_AS_DELEGATE);
60 flags->trusted_for_delegation = !!(attr & KRB5_KDB_TRUSTED_FOR_DELEGATION);
61 flags->allow_kerberos4 = !!(attr & KRB5_KDB_ALLOW_KERBEROS4);
62 flags->allow_digest = !!(attr & KRB5_KDB_ALLOW_DIGEST);
63 flags->materialize = !!(attr & KRB5_KDB_MATERIALIZE);
64 flags->virtual_keys = !!(attr & KRB5_KDB_VIRTUAL_KEYS);
65 flags->virtual = !!(attr & KRB5_KDB_VIRTUAL);
66 flags->no_auth_data_reqd = !!(attr & KRB5_KDB_NO_AUTH_DATA_REQUIRED);
70 * Modify the `ent' according to `tl_data'.
74 perform_tl_data(krb5_context context,
77 const krb5_tl_data *tl_data)
81 if (tl_data->tl_data_type == KRB5_TL_PASSWORD) {
82 heim_utf8_string pw = tl_data->tl_data_contents;
84 if (pw[tl_data->tl_data_length] != '\0')
85 return KADM5_BAD_TL_TYPE;
87 ret = hdb_entry_set_password(context, db, &ent->entry, pw);
89 } else if (tl_data->tl_data_type == KRB5_TL_LAST_PWD_CHANGE) {
93 if (tl_data->tl_data_length != 4)
94 return KADM5_BAD_TL_TYPE;
96 s = tl_data->tl_data_contents;
98 (void) _krb5_get_int(s, &t, tl_data->tl_data_length);
99 ret = hdb_entry_set_pw_change_time(context, &ent->entry, t);
101 } else if (tl_data->tl_data_type == KRB5_TL_KEY_ROTATION) {
102 HDB_Ext_KeyRotation *prev_kr = 0;
103 HDB_extension *prev_ext;
107 ext.data.element = choice_HDB_extension_data_key_rotation;
108 prev_ext = hdb_find_extension(&ent->entry, ext.data.element);
110 prev_kr = &prev_ext->data.u.key_rotation;
111 ret = decode_HDB_Ext_KeyRotation(tl_data->tl_data_contents,
112 tl_data->tl_data_length,
113 &ext.data.u.key_rotation, NULL);
115 ret = hdb_validate_key_rotations(context, prev_kr,
116 &ext.data.u.key_rotation);
118 ret = hdb_replace_extension(context, &ent->entry, &ext);
119 free_HDB_extension(&ext);
120 } else if (tl_data->tl_data_type == KRB5_TL_EXTENSION) {
123 ret = decode_HDB_extension(tl_data->tl_data_contents,
124 tl_data->tl_data_length,
128 return KADM5_BAD_TL_TYPE;
130 if (ext.data.element == choice_HDB_extension_data_key_rotation) {
131 HDB_extension *prev_ext = hdb_find_extension(&ent->entry,
133 HDB_Ext_KeyRotation *prev_kr = 0;
136 prev_kr = &prev_ext->data.u.key_rotation;
137 ret = hdb_validate_key_rotations(context, prev_kr,
138 &ext.data.u.key_rotation);
141 ret = KADM5_BAD_TL_TYPE; /* XXX Need new error code */
143 ret = hdb_replace_extension(context, &ent->entry, &ext);
144 free_HDB_extension(&ext);
145 } else if (tl_data->tl_data_type == KRB5_TL_ETYPES) {
146 if (!ent->entry.etypes &&
147 (ent->entry.etypes = calloc(1,
148 sizeof(ent->entry.etypes[0]))) == NULL)
149 ret = krb5_enomem(context);
150 if (ent->entry.etypes)
151 free_HDB_EncTypeList(ent->entry.etypes);
153 ret = decode_HDB_EncTypeList(tl_data->tl_data_contents,
154 tl_data->tl_data_length,
155 ent->entry.etypes, NULL);
157 return KADM5_BAD_TL_TYPE;
158 } else if (tl_data->tl_data_type == KRB5_TL_ALIASES) {
161 return KADM5_BAD_TL_TYPE;
167 default_flags(hdb_entry_ex *ent)
169 ent->entry.flags.client = 1;
170 ent->entry.flags.server = 1;
171 ent->entry.flags.forwardable = 1;
172 ent->entry.flags.proxiable = 1;
173 ent->entry.flags.renewable = 1;
174 ent->entry.flags.postdate = 1;
179 * Create the hdb entry `ent' based on data from `princ' with
180 * `princ_mask' specifying what fields to be gotten from there and
181 * `mask' specifying what fields we want filled in.
185 _kadm5_setup_entry(kadm5_server_context *context,
188 kadm5_principal_ent_t princ,
190 kadm5_principal_ent_t def,
193 if(mask & KADM5_PRINC_EXPIRE_TIME
194 && princ_mask & KADM5_PRINC_EXPIRE_TIME) {
195 if (princ->princ_expire_time)
196 set_value(ent->entry.valid_end, princ->princ_expire_time);
198 set_null(ent->entry.valid_end);
200 if(mask & KADM5_PW_EXPIRATION
201 && princ_mask & KADM5_PW_EXPIRATION) {
202 if (princ->pw_expiration)
203 set_value(ent->entry.pw_end, princ->pw_expiration);
205 set_null(ent->entry.pw_end);
207 if(mask & KADM5_ATTRIBUTES) {
208 if (princ_mask & KADM5_ATTRIBUTES) {
209 attr_to_flags(princ->attributes, &ent->entry.flags);
210 } else if(def_mask & KADM5_ATTRIBUTES) {
211 attr_to_flags(def->attributes, &ent->entry.flags);
212 ent->entry.flags.invalid = 0;
218 if(mask & KADM5_MAX_LIFE) {
219 if(princ_mask & KADM5_MAX_LIFE) {
221 set_value(ent->entry.max_life, princ->max_life);
223 set_null(ent->entry.max_life);
224 } else if(def_mask & KADM5_MAX_LIFE) {
226 set_value(ent->entry.max_life, def->max_life);
228 set_null(ent->entry.max_life);
232 && (princ_mask & KADM5_KVNO)) {
235 ret = hdb_change_kvno(context->context, princ->kvno, &ent->entry);
236 if (ret && ret != HDB_ERR_KVNO_NOT_FOUND)
238 ent->entry.kvno = princ->kvno; /* force it */
240 if(mask & KADM5_MAX_RLIFE) {
241 if(princ_mask & KADM5_MAX_RLIFE) {
242 if(princ->max_renewable_life)
243 set_value(ent->entry.max_renew, princ->max_renewable_life);
245 set_null(ent->entry.max_renew);
246 } else if(def_mask & KADM5_MAX_RLIFE) {
247 if(def->max_renewable_life)
248 set_value(ent->entry.max_renew, def->max_renewable_life);
250 set_null(ent->entry.max_renew);
253 if(mask & KADM5_KEY_DATA
254 && princ_mask & KADM5_KEY_DATA) {
255 _kadm5_set_keys2(context, &ent->entry,
256 princ->n_key_data, princ->key_data);
258 if(mask & KADM5_TL_DATA) {
261 for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next) {
263 ret = perform_tl_data(context->context, context->db, ent, tl);
268 if(mask & KADM5_FAIL_AUTH_COUNT) {