Path: blob/main/crypto/heimdal/lib/kadm5/ent_setup.c
34889 views
/*1* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "kadm5_locl.h"3637RCSID("$Id$");3839#define set_value(X, V) do { if((X) == NULL) (X) = malloc(sizeof(*(X))); *(X) = V; } while(0)40#define set_null(X) do { if((X) != NULL) free((X)); (X) = NULL; } while (0)4142static void43attr_to_flags(unsigned attr, HDBFlags *flags)44{45flags->postdate = !(attr & KRB5_KDB_DISALLOW_POSTDATED);46flags->forwardable = !(attr & KRB5_KDB_DISALLOW_FORWARDABLE);47flags->initial = !!(attr & KRB5_KDB_DISALLOW_TGT_BASED);48flags->renewable = !(attr & KRB5_KDB_DISALLOW_RENEWABLE);49flags->proxiable = !(attr & KRB5_KDB_DISALLOW_PROXIABLE);50/* DUP_SKEY */51flags->invalid = !!(attr & KRB5_KDB_DISALLOW_ALL_TIX);52flags->require_preauth = !!(attr & KRB5_KDB_REQUIRES_PRE_AUTH);53/* HW_AUTH */54flags->server = !(attr & KRB5_KDB_DISALLOW_SVR);55flags->change_pw = !!(attr & KRB5_KDB_PWCHANGE_SERVICE);56flags->client = 1; /* XXX */57flags->ok_as_delegate = !!(attr & KRB5_KDB_OK_AS_DELEGATE);58flags->trusted_for_delegation = !!(attr & KRB5_KDB_TRUSTED_FOR_DELEGATION);59flags->allow_kerberos4 = !!(attr & KRB5_KDB_ALLOW_KERBEROS4);60flags->allow_digest = !!(attr & KRB5_KDB_ALLOW_DIGEST);61}6263/*64* Modify the `ent' according to `tl_data'.65*/6667static kadm5_ret_t68perform_tl_data(krb5_context context,69HDB *db,70hdb_entry_ex *ent,71const krb5_tl_data *tl_data)72{73kadm5_ret_t ret = 0;7475if (tl_data->tl_data_type == KRB5_TL_PASSWORD) {76heim_utf8_string pw = tl_data->tl_data_contents;7778if (pw[tl_data->tl_data_length] != '\0')79return KADM5_BAD_TL_TYPE;8081ret = hdb_entry_set_password(context, db, &ent->entry, pw);8283} else if (tl_data->tl_data_type == KRB5_TL_LAST_PWD_CHANGE) {84unsigned char *s;85time_t t;8687if (tl_data->tl_data_length != 4)88return KADM5_BAD_TL_TYPE;8990s = tl_data->tl_data_contents;9192t = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);9394ret = hdb_entry_set_pw_change_time(context, &ent->entry, t);9596} else if (tl_data->tl_data_type == KRB5_TL_EXTENSION) {97HDB_extension ext;9899ret = decode_HDB_extension(tl_data->tl_data_contents,100tl_data->tl_data_length,101&ext,102NULL);103if (ret)104return KADM5_BAD_TL_TYPE;105106ret = hdb_replace_extension(context, &ent->entry, &ext);107free_HDB_extension(&ext);108} else {109return KADM5_BAD_TL_TYPE;110}111return ret;112}113114static void115default_flags(hdb_entry_ex *ent, int server)116{117ent->entry.flags.client = 1;118ent->entry.flags.server = !!server;119ent->entry.flags.forwardable = 1;120ent->entry.flags.proxiable = 1;121ent->entry.flags.renewable = 1;122ent->entry.flags.postdate = 1;123}124125126/*127* Create the hdb entry `ent' based on data from `princ' with128* `princ_mask' specifying what fields to be gotten from there and129* `mask' specifying what fields we want filled in.130*/131132kadm5_ret_t133_kadm5_setup_entry(kadm5_server_context *context,134hdb_entry_ex *ent,135uint32_t mask,136kadm5_principal_ent_t princ,137uint32_t princ_mask,138kadm5_principal_ent_t def,139uint32_t def_mask)140{141if(mask & KADM5_PRINC_EXPIRE_TIME142&& princ_mask & KADM5_PRINC_EXPIRE_TIME) {143if (princ->princ_expire_time)144set_value(ent->entry.valid_end, princ->princ_expire_time);145else146set_null(ent->entry.valid_end);147}148if(mask & KADM5_PW_EXPIRATION149&& princ_mask & KADM5_PW_EXPIRATION) {150if (princ->pw_expiration)151set_value(ent->entry.pw_end, princ->pw_expiration);152else153set_null(ent->entry.pw_end);154}155if(mask & KADM5_ATTRIBUTES) {156if (princ_mask & KADM5_ATTRIBUTES) {157attr_to_flags(princ->attributes, &ent->entry.flags);158} else if(def_mask & KADM5_ATTRIBUTES) {159attr_to_flags(def->attributes, &ent->entry.flags);160ent->entry.flags.invalid = 0;161} else {162default_flags(ent, 1);163}164}165166if(mask & KADM5_MAX_LIFE) {167if(princ_mask & KADM5_MAX_LIFE) {168if(princ->max_life)169set_value(ent->entry.max_life, princ->max_life);170else171set_null(ent->entry.max_life);172} else if(def_mask & KADM5_MAX_LIFE) {173if(def->max_life)174set_value(ent->entry.max_life, def->max_life);175else176set_null(ent->entry.max_life);177}178}179if(mask & KADM5_KVNO180&& princ_mask & KADM5_KVNO)181ent->entry.kvno = princ->kvno;182if(mask & KADM5_MAX_RLIFE) {183if(princ_mask & KADM5_MAX_RLIFE) {184if(princ->max_renewable_life)185set_value(ent->entry.max_renew, princ->max_renewable_life);186else187set_null(ent->entry.max_renew);188} else if(def_mask & KADM5_MAX_RLIFE) {189if(def->max_renewable_life)190set_value(ent->entry.max_renew, def->max_renewable_life);191else192set_null(ent->entry.max_renew);193}194}195if(mask & KADM5_KEY_DATA196&& princ_mask & KADM5_KEY_DATA) {197_kadm5_set_keys2(context, &ent->entry,198princ->n_key_data, princ->key_data);199}200if(mask & KADM5_TL_DATA) {201krb5_tl_data *tl;202203for (tl = princ->tl_data; tl != NULL; tl = tl->tl_data_next) {204kadm5_ret_t ret;205ret = perform_tl_data(context->context, context->db, ent, tl);206if (ret)207return ret;208}209}210if(mask & KADM5_FAIL_AUTH_COUNT) {211/* XXX */212}213return 0;214}215216217