1*0a6a1f1dSLionel Sambuc /* $NetBSD: ent_setup.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc *
10ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc * are met:
13ebfedea0SLionel Sambuc *
14ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc *
17ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc * without specific prior written permission.
24ebfedea0SLionel Sambuc *
25ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc * SUCH DAMAGE.
36ebfedea0SLionel Sambuc */
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc #include "kadm5_locl.h"
39ebfedea0SLionel Sambuc
40*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
41ebfedea0SLionel Sambuc
42ebfedea0SLionel Sambuc #define set_value(X, V) do { if((X) == NULL) (X) = malloc(sizeof(*(X))); *(X) = V; } while(0)
43ebfedea0SLionel Sambuc #define set_null(X) do { if((X) != NULL) free((X)); (X) = NULL; } while (0)
44ebfedea0SLionel Sambuc
45ebfedea0SLionel Sambuc static void
attr_to_flags(unsigned attr,HDBFlags * flags)46ebfedea0SLionel Sambuc attr_to_flags(unsigned attr, HDBFlags *flags)
47ebfedea0SLionel Sambuc {
48ebfedea0SLionel Sambuc flags->postdate = !(attr & KRB5_KDB_DISALLOW_POSTDATED);
49ebfedea0SLionel Sambuc flags->forwardable = !(attr & KRB5_KDB_DISALLOW_FORWARDABLE);
50ebfedea0SLionel Sambuc flags->initial = !!(attr & KRB5_KDB_DISALLOW_TGT_BASED);
51ebfedea0SLionel Sambuc flags->renewable = !(attr & KRB5_KDB_DISALLOW_RENEWABLE);
52ebfedea0SLionel Sambuc flags->proxiable = !(attr & KRB5_KDB_DISALLOW_PROXIABLE);
53ebfedea0SLionel Sambuc /* DUP_SKEY */
54ebfedea0SLionel Sambuc flags->invalid = !!(attr & KRB5_KDB_DISALLOW_ALL_TIX);
55ebfedea0SLionel Sambuc flags->require_preauth = !!(attr & KRB5_KDB_REQUIRES_PRE_AUTH);
56ebfedea0SLionel Sambuc /* HW_AUTH */
57ebfedea0SLionel Sambuc flags->server = !(attr & KRB5_KDB_DISALLOW_SVR);
58ebfedea0SLionel Sambuc flags->change_pw = !!(attr & KRB5_KDB_PWCHANGE_SERVICE);
59ebfedea0SLionel Sambuc flags->client = 1; /* XXX */
60ebfedea0SLionel Sambuc flags->ok_as_delegate = !!(attr & KRB5_KDB_OK_AS_DELEGATE);
61ebfedea0SLionel Sambuc flags->trusted_for_delegation = !!(attr & KRB5_KDB_TRUSTED_FOR_DELEGATION);
62ebfedea0SLionel Sambuc flags->allow_kerberos4 = !!(attr & KRB5_KDB_ALLOW_KERBEROS4);
63ebfedea0SLionel Sambuc flags->allow_digest = !!(attr & KRB5_KDB_ALLOW_DIGEST);
64ebfedea0SLionel Sambuc }
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc /*
67ebfedea0SLionel Sambuc * Modify the `ent' according to `tl_data'.
68ebfedea0SLionel Sambuc */
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc static kadm5_ret_t
perform_tl_data(krb5_context context,HDB * db,hdb_entry_ex * ent,const krb5_tl_data * tl_data)71ebfedea0SLionel Sambuc perform_tl_data(krb5_context context,
72ebfedea0SLionel Sambuc HDB *db,
73ebfedea0SLionel Sambuc hdb_entry_ex *ent,
74ebfedea0SLionel Sambuc const krb5_tl_data *tl_data)
75ebfedea0SLionel Sambuc {
76ebfedea0SLionel Sambuc kadm5_ret_t ret = 0;
77ebfedea0SLionel Sambuc
78ebfedea0SLionel Sambuc if (tl_data->tl_data_type == KRB5_TL_PASSWORD) {
79ebfedea0SLionel Sambuc heim_utf8_string pw = tl_data->tl_data_contents;
80ebfedea0SLionel Sambuc
81ebfedea0SLionel Sambuc if (pw[tl_data->tl_data_length] != '\0')
82ebfedea0SLionel Sambuc return KADM5_BAD_TL_TYPE;
83ebfedea0SLionel Sambuc
84ebfedea0SLionel Sambuc ret = hdb_entry_set_password(context, db, &ent->entry, pw);
85ebfedea0SLionel Sambuc
86ebfedea0SLionel Sambuc } else if (tl_data->tl_data_type == KRB5_TL_LAST_PWD_CHANGE) {
87ebfedea0SLionel Sambuc unsigned char *s;
88ebfedea0SLionel Sambuc time_t t;
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc if (tl_data->tl_data_length != 4)
91ebfedea0SLionel Sambuc return KADM5_BAD_TL_TYPE;
92ebfedea0SLionel Sambuc
93ebfedea0SLionel Sambuc s = tl_data->tl_data_contents;
94ebfedea0SLionel Sambuc
95ebfedea0SLionel Sambuc t = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc ret = hdb_entry_set_pw_change_time(context, &ent->entry, t);
98ebfedea0SLionel Sambuc
99ebfedea0SLionel Sambuc } else if (tl_data->tl_data_type == KRB5_TL_EXTENSION) {
100ebfedea0SLionel Sambuc HDB_extension ext;
101ebfedea0SLionel Sambuc
102ebfedea0SLionel Sambuc ret = decode_HDB_extension(tl_data->tl_data_contents,
103ebfedea0SLionel Sambuc tl_data->tl_data_length,
104ebfedea0SLionel Sambuc &ext,
105ebfedea0SLionel Sambuc NULL);
106ebfedea0SLionel Sambuc if (ret)
107ebfedea0SLionel Sambuc return KADM5_BAD_TL_TYPE;
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc ret = hdb_replace_extension(context, &ent->entry, &ext);
110ebfedea0SLionel Sambuc free_HDB_extension(&ext);
111ebfedea0SLionel Sambuc } else {
112ebfedea0SLionel Sambuc return KADM5_BAD_TL_TYPE;
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc return ret;
115ebfedea0SLionel Sambuc }
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc static void
default_flags(hdb_entry_ex * ent,int server)118ebfedea0SLionel Sambuc default_flags(hdb_entry_ex *ent, int server)
119ebfedea0SLionel Sambuc {
120ebfedea0SLionel Sambuc ent->entry.flags.client = 1;
121ebfedea0SLionel Sambuc ent->entry.flags.server = !!server;
122ebfedea0SLionel Sambuc ent->entry.flags.forwardable = 1;
123ebfedea0SLionel Sambuc ent->entry.flags.proxiable = 1;
124ebfedea0SLionel Sambuc ent->entry.flags.renewable = 1;
125ebfedea0SLionel Sambuc ent->entry.flags.postdate = 1;
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc
128ebfedea0SLionel Sambuc
129ebfedea0SLionel Sambuc /*
130ebfedea0SLionel Sambuc * Create the hdb entry `ent' based on data from `princ' with
131ebfedea0SLionel Sambuc * `princ_mask' specifying what fields to be gotten from there and
132ebfedea0SLionel Sambuc * `mask' specifying what fields we want filled in.
133ebfedea0SLionel Sambuc */
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_setup_entry(kadm5_server_context * context,hdb_entry_ex * ent,uint32_t mask,kadm5_principal_ent_t princ,uint32_t princ_mask,kadm5_principal_ent_t def,uint32_t def_mask)136ebfedea0SLionel Sambuc _kadm5_setup_entry(kadm5_server_context *context,
137ebfedea0SLionel Sambuc hdb_entry_ex *ent,
138ebfedea0SLionel Sambuc uint32_t mask,
139ebfedea0SLionel Sambuc kadm5_principal_ent_t princ,
140ebfedea0SLionel Sambuc uint32_t princ_mask,
141ebfedea0SLionel Sambuc kadm5_principal_ent_t def,
142ebfedea0SLionel Sambuc uint32_t def_mask)
143ebfedea0SLionel Sambuc {
144ebfedea0SLionel Sambuc if(mask & KADM5_PRINC_EXPIRE_TIME
145ebfedea0SLionel Sambuc && princ_mask & KADM5_PRINC_EXPIRE_TIME) {
146ebfedea0SLionel Sambuc if (princ->princ_expire_time)
147ebfedea0SLionel Sambuc set_value(ent->entry.valid_end, princ->princ_expire_time);
148ebfedea0SLionel Sambuc else
149ebfedea0SLionel Sambuc set_null(ent->entry.valid_end);
150ebfedea0SLionel Sambuc }
151ebfedea0SLionel Sambuc if(mask & KADM5_PW_EXPIRATION
152ebfedea0SLionel Sambuc && princ_mask & KADM5_PW_EXPIRATION) {
153ebfedea0SLionel Sambuc if (princ->pw_expiration)
154ebfedea0SLionel Sambuc set_value(ent->entry.pw_end, princ->pw_expiration);
155ebfedea0SLionel Sambuc else
156ebfedea0SLionel Sambuc set_null(ent->entry.pw_end);
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc if(mask & KADM5_ATTRIBUTES) {
159ebfedea0SLionel Sambuc if (princ_mask & KADM5_ATTRIBUTES) {
160ebfedea0SLionel Sambuc attr_to_flags(princ->attributes, &ent->entry.flags);
161ebfedea0SLionel Sambuc } else if(def_mask & KADM5_ATTRIBUTES) {
162ebfedea0SLionel Sambuc attr_to_flags(def->attributes, &ent->entry.flags);
163ebfedea0SLionel Sambuc ent->entry.flags.invalid = 0;
164ebfedea0SLionel Sambuc } else {
165ebfedea0SLionel Sambuc default_flags(ent, 1);
166ebfedea0SLionel Sambuc }
167ebfedea0SLionel Sambuc }
168ebfedea0SLionel Sambuc
169ebfedea0SLionel Sambuc if(mask & KADM5_MAX_LIFE) {
170ebfedea0SLionel Sambuc if(princ_mask & KADM5_MAX_LIFE) {
171ebfedea0SLionel Sambuc if(princ->max_life)
172ebfedea0SLionel Sambuc set_value(ent->entry.max_life, princ->max_life);
173ebfedea0SLionel Sambuc else
174ebfedea0SLionel Sambuc set_null(ent->entry.max_life);
175ebfedea0SLionel Sambuc } else if(def_mask & KADM5_MAX_LIFE) {
176ebfedea0SLionel Sambuc if(def->max_life)
177ebfedea0SLionel Sambuc set_value(ent->entry.max_life, def->max_life);
178ebfedea0SLionel Sambuc else
179ebfedea0SLionel Sambuc set_null(ent->entry.max_life);
180ebfedea0SLionel Sambuc }
181ebfedea0SLionel Sambuc }
182ebfedea0SLionel Sambuc if(mask & KADM5_KVNO
183ebfedea0SLionel Sambuc && princ_mask & KADM5_KVNO)
184ebfedea0SLionel Sambuc ent->entry.kvno = princ->kvno;
185ebfedea0SLionel Sambuc if(mask & KADM5_MAX_RLIFE) {
186ebfedea0SLionel Sambuc if(princ_mask & KADM5_MAX_RLIFE) {
187ebfedea0SLionel Sambuc if(princ->max_renewable_life)
188ebfedea0SLionel Sambuc set_value(ent->entry.max_renew, princ->max_renewable_life);
189ebfedea0SLionel Sambuc else
190ebfedea0SLionel Sambuc set_null(ent->entry.max_renew);
191ebfedea0SLionel Sambuc } else if(def_mask & KADM5_MAX_RLIFE) {
192ebfedea0SLionel Sambuc if(def->max_renewable_life)
193ebfedea0SLionel Sambuc set_value(ent->entry.max_renew, def->max_renewable_life);
194ebfedea0SLionel Sambuc else
195ebfedea0SLionel Sambuc set_null(ent->entry.max_renew);
196ebfedea0SLionel Sambuc }
197ebfedea0SLionel Sambuc }
198ebfedea0SLionel Sambuc if(mask & KADM5_KEY_DATA
199ebfedea0SLionel Sambuc && princ_mask & KADM5_KEY_DATA) {
200ebfedea0SLionel Sambuc _kadm5_set_keys2(context, &ent->entry,
201ebfedea0SLionel Sambuc princ->n_key_data, princ->key_data);
202ebfedea0SLionel Sambuc }
203ebfedea0SLionel Sambuc if(mask & KADM5_TL_DATA) {
204ebfedea0SLionel Sambuc krb5_tl_data *tl;
205ebfedea0SLionel Sambuc
206ebfedea0SLionel Sambuc for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next) {
207ebfedea0SLionel Sambuc kadm5_ret_t ret;
208ebfedea0SLionel Sambuc ret = perform_tl_data(context->context, context->db, ent, tl);
209ebfedea0SLionel Sambuc if (ret)
210ebfedea0SLionel Sambuc return ret;
211ebfedea0SLionel Sambuc }
212ebfedea0SLionel Sambuc }
213ebfedea0SLionel Sambuc if(mask & KADM5_FAIL_AUTH_COUNT) {
214ebfedea0SLionel Sambuc /* XXX */
215ebfedea0SLionel Sambuc }
216ebfedea0SLionel Sambuc return 0;
217ebfedea0SLionel Sambuc }
218