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
= 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'.
70 perform_tl_data(krb5_context context
,
73 const krb5_tl_data
*tl_data
)
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
) {
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
) {
101 ret
= decode_HDB_extension(tl_data
->tl_data_contents
,
102 tl_data
->tl_data_length
,
106 return KADM5_BAD_TL_TYPE
;
108 ret
= hdb_replace_extension(context
, &ent
->entry
, &ext
);
109 free_HDB_extension(&ext
);
111 return KADM5_BAD_TL_TYPE
;
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.
135 _kadm5_setup_entry(kadm5_server_context
*context
,
138 kadm5_principal_ent_t princ
,
140 kadm5_principal_ent_t def
,
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
);
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
);
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;
164 default_flags(ent
, 1);
168 if(mask
& KADM5_MAX_LIFE
) {
169 if(princ_mask
& KADM5_MAX_LIFE
) {
171 set_value(ent
->entry
.max_life
, princ
->max_life
);
173 set_null(ent
->entry
.max_life
);
174 } else if(def_mask
& KADM5_MAX_LIFE
) {
176 set_value(ent
->entry
.max_life
, def
->max_life
);
178 set_null(ent
->entry
.max_life
);
182 && (princ_mask
& KADM5_KVNO
)) {
185 ret
= hdb_change_kvno(context
->context
, princ
->kvno
, &ent
->entry
);
186 if (ret
&& ret
!= HDB_ERR_KVNO_NOT_FOUND
)
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
);
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
);
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
) {
211 for (tl
= princ
->tl_data
; tl
!= NULL
; tl
= tl
->tl_data_next
) {
213 ret
= perform_tl_data(context
->context
, context
->db
, ent
, tl
);
218 if(mask
& KADM5_FAIL_AUTH_COUNT
) {