Rename context handle lifetime to endtime
[heimdal.git] / lib / kadm5 / ent_setup.c
blob1266f204b7eb479bcae7f1af07115b3733cc7221
1 /*
2 * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "kadm5_locl.h"
38 RCSID("$Id$");
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)
43 static void
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);
51 /* DUP_SKEY */
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);
55 /* HW_AUTH */
56 flags->server = !(attr & KRB5_KDB_DISALLOW_SVR);
57 flags->change_pw = !!(attr & KRB5_KDB_PWCHANGE_SERVICE);
58 flags->client = 1; /* XXX */
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);
66 * Modify the `ent' according to `tl_data'.
69 static kadm5_ret_t
70 perform_tl_data(krb5_context context,
71 HDB *db,
72 hdb_entry_ex *ent,
73 const krb5_tl_data *tl_data)
75 kadm5_ret_t ret = 0;
77 if (tl_data->tl_data_type == KRB5_TL_PASSWORD) {
78 heim_utf8_string pw = tl_data->tl_data_contents;
80 if (pw[tl_data->tl_data_length] != '\0')
81 return KADM5_BAD_TL_TYPE;
83 ret = hdb_entry_set_password(context, db, &ent->entry, pw);
85 } else if (tl_data->tl_data_type == KRB5_TL_LAST_PWD_CHANGE) {
86 unsigned char *s;
87 time_t t;
89 if (tl_data->tl_data_length != 4)
90 return KADM5_BAD_TL_TYPE;
92 s = tl_data->tl_data_contents;
94 t = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
96 ret = hdb_entry_set_pw_change_time(context, &ent->entry, t);
98 } else if (tl_data->tl_data_type == KRB5_TL_EXTENSION) {
99 HDB_extension ext;
101 ret = decode_HDB_extension(tl_data->tl_data_contents,
102 tl_data->tl_data_length,
103 &ext,
104 NULL);
105 if (ret)
106 return KADM5_BAD_TL_TYPE;
108 ret = hdb_replace_extension(context, &ent->entry, &ext);
109 free_HDB_extension(&ext);
110 } else {
111 return KADM5_BAD_TL_TYPE;
113 return ret;
116 static void
117 default_flags(hdb_entry_ex *ent, int server)
119 ent->entry.flags.client = 1;
120 ent->entry.flags.server = !!server;
121 ent->entry.flags.forwardable = 1;
122 ent->entry.flags.proxiable = 1;
123 ent->entry.flags.renewable = 1;
124 ent->entry.flags.postdate = 1;
129 * Create the hdb entry `ent' based on data from `princ' with
130 * `princ_mask' specifying what fields to be gotten from there and
131 * `mask' specifying what fields we want filled in.
134 kadm5_ret_t
135 _kadm5_setup_entry(kadm5_server_context *context,
136 hdb_entry_ex *ent,
137 uint32_t mask,
138 kadm5_principal_ent_t princ,
139 uint32_t princ_mask,
140 kadm5_principal_ent_t def,
141 uint32_t def_mask)
143 if(mask & KADM5_PRINC_EXPIRE_TIME
144 && princ_mask & KADM5_PRINC_EXPIRE_TIME) {
145 if (princ->princ_expire_time)
146 set_value(ent->entry.valid_end, princ->princ_expire_time);
147 else
148 set_null(ent->entry.valid_end);
150 if(mask & KADM5_PW_EXPIRATION
151 && princ_mask & KADM5_PW_EXPIRATION) {
152 if (princ->pw_expiration)
153 set_value(ent->entry.pw_end, princ->pw_expiration);
154 else
155 set_null(ent->entry.pw_end);
157 if(mask & KADM5_ATTRIBUTES) {
158 if (princ_mask & KADM5_ATTRIBUTES) {
159 attr_to_flags(princ->attributes, &ent->entry.flags);
160 } else if(def_mask & KADM5_ATTRIBUTES) {
161 attr_to_flags(def->attributes, &ent->entry.flags);
162 ent->entry.flags.invalid = 0;
163 } else {
164 default_flags(ent, 1);
168 if(mask & KADM5_MAX_LIFE) {
169 if(princ_mask & KADM5_MAX_LIFE) {
170 if(princ->max_life)
171 set_value(ent->entry.max_life, princ->max_life);
172 else
173 set_null(ent->entry.max_life);
174 } else if(def_mask & KADM5_MAX_LIFE) {
175 if(def->max_life)
176 set_value(ent->entry.max_life, def->max_life);
177 else
178 set_null(ent->entry.max_life);
181 if(mask & KADM5_KVNO
182 && (princ_mask & KADM5_KVNO)) {
183 krb5_error_code ret;
185 ret = hdb_change_kvno(context->context, princ->kvno, &ent->entry);
186 if (ret && ret != HDB_ERR_KVNO_NOT_FOUND)
187 return ret;
188 ent->entry.kvno = princ->kvno; /* force it */
190 if(mask & KADM5_MAX_RLIFE) {
191 if(princ_mask & KADM5_MAX_RLIFE) {
192 if(princ->max_renewable_life)
193 set_value(ent->entry.max_renew, princ->max_renewable_life);
194 else
195 set_null(ent->entry.max_renew);
196 } else if(def_mask & KADM5_MAX_RLIFE) {
197 if(def->max_renewable_life)
198 set_value(ent->entry.max_renew, def->max_renewable_life);
199 else
200 set_null(ent->entry.max_renew);
203 if(mask & KADM5_KEY_DATA
204 && princ_mask & KADM5_KEY_DATA) {
205 _kadm5_set_keys2(context, &ent->entry,
206 princ->n_key_data, princ->key_data);
208 if(mask & KADM5_TL_DATA) {
209 krb5_tl_data *tl;
211 for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next) {
212 kadm5_ret_t ret;
213 ret = perform_tl_data(context->context, context->db, ent, tl);
214 if (ret)
215 return ret;
218 if(mask & KADM5_FAIL_AUTH_COUNT) {
219 /* XXX */
221 return 0;