Path: blob/main/crypto/heimdal/lib/kadm5/chpass_s.c
34890 views
/*1* Copyright (c) 1997-2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "kadm5_locl.h"3435RCSID("$Id$");3637static kadm5_ret_t38change(void *server_handle,39krb5_principal princ,40const char *password,41int cond)42{43kadm5_server_context *context = server_handle;44hdb_entry_ex ent;45kadm5_ret_t ret;46Key *keys;47size_t num_keys;48int existsp = 0;4950memset(&ent, 0, sizeof(ent));51ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);52if(ret)53return ret;5455ret = context->db->hdb_fetch_kvno(context->context, context->db, princ,56HDB_F_DECRYPT|HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent);57if(ret)58goto out;5960if (context->db->hdb_capability_flags & HDB_CAP_F_HANDLE_PASSWORDS) {61ret = context->db->hdb_password(context->context, context->db,62&ent, password, cond);63if (ret)64goto out2;65} else {6667num_keys = ent.entry.keys.len;68keys = ent.entry.keys.val;6970ent.entry.keys.len = 0;71ent.entry.keys.val = NULL;7273ret = _kadm5_set_keys(context, &ent.entry, password);74if(ret) {75_kadm5_free_keys (context->context, num_keys, keys);76goto out2;77}7879if (cond)80existsp = _kadm5_exists_keys (ent.entry.keys.val,81ent.entry.keys.len,82keys, num_keys);83_kadm5_free_keys (context->context, num_keys, keys);8485if (existsp) {86ret = KADM5_PASS_REUSE;87krb5_set_error_message(context->context, ret,88"Password reuse forbidden");89goto out2;90}9192ret = hdb_seal_keys(context->context, context->db, &ent.entry);93if (ret)94goto out2;95}96ent.entry.kvno++;9798ret = _kadm5_set_modifier(context, &ent.entry);99if(ret)100goto out2;101102ret = _kadm5_bump_pw_expire(context, &ent.entry);103if (ret)104goto out2;105106ret = context->db->hdb_store(context->context, context->db,107HDB_F_REPLACE, &ent);108if (ret)109goto out2;110111kadm5_log_modify (context,112&ent.entry,113KADM5_PRINCIPAL | KADM5_MOD_NAME | KADM5_MOD_TIME |114KADM5_KEY_DATA | KADM5_KVNO | KADM5_PW_EXPIRATION |115KADM5_TL_DATA);116117out2:118hdb_free_entry(context->context, &ent);119out:120context->db->hdb_close(context->context, context->db);121return _kadm5_error_code(ret);122}123124125126/*127* change the password of `princ' to `password' if it's not already that.128*/129130kadm5_ret_t131kadm5_s_chpass_principal_cond(void *server_handle,132krb5_principal princ,133const char *password)134{135return change (server_handle, princ, password, 1);136}137138/*139* change the password of `princ' to `password'140*/141142kadm5_ret_t143kadm5_s_chpass_principal(void *server_handle,144krb5_principal princ,145const char *password)146{147return change (server_handle, princ, password, 0);148}149150/*151* change keys for `princ' to `keys'152*/153154kadm5_ret_t155kadm5_s_chpass_principal_with_key(void *server_handle,156krb5_principal princ,157int n_key_data,158krb5_key_data *key_data)159{160kadm5_server_context *context = server_handle;161hdb_entry_ex ent;162kadm5_ret_t ret;163164memset(&ent, 0, sizeof(ent));165ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);166if(ret)167return ret;168ret = context->db->hdb_fetch_kvno(context->context, context->db, princ, 0,169HDB_F_GET_ANY|HDB_F_ADMIN_DATA, &ent);170if(ret)171goto out;172ret = _kadm5_set_keys2(context, &ent.entry, n_key_data, key_data);173if(ret)174goto out2;175ent.entry.kvno++;176ret = _kadm5_set_modifier(context, &ent.entry);177if(ret)178goto out2;179ret = _kadm5_bump_pw_expire(context, &ent.entry);180if (ret)181goto out2;182183ret = hdb_seal_keys(context->context, context->db, &ent.entry);184if (ret)185goto out2;186187ret = context->db->hdb_store(context->context, context->db,188HDB_F_REPLACE, &ent);189if (ret)190goto out2;191192kadm5_log_modify (context,193&ent.entry,194KADM5_PRINCIPAL | KADM5_MOD_NAME | KADM5_MOD_TIME |195KADM5_KEY_DATA | KADM5_KVNO | KADM5_PW_EXPIRATION |196KADM5_TL_DATA);197198out2:199hdb_free_entry(context->context, &ent);200out:201context->db->hdb_close(context->context, context->db);202return _kadm5_error_code(ret);203}204205206