Path: blob/main/crypto/heimdal/lib/kadm5/chpass_s.c
107833 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;5960ret = hdb_add_current_keys_to_history(context->context, &ent.entry);61if (ret)62goto out;6364if (context->db->hdb_capability_flags & HDB_CAP_F_HANDLE_PASSWORDS) {65ret = context->db->hdb_password(context->context, context->db,66&ent, password, cond);67if (ret)68goto out2;69} else {7071num_keys = ent.entry.keys.len;72keys = ent.entry.keys.val;7374ent.entry.keys.len = 0;75ent.entry.keys.val = NULL;7677ret = _kadm5_set_keys(context, &ent.entry, password);78if(ret) {79_kadm5_free_keys (context->context, num_keys, keys);80goto out2;81}8283if (cond)84existsp = _kadm5_exists_keys (ent.entry.keys.val,85ent.entry.keys.len,86keys, num_keys);87_kadm5_free_keys (context->context, num_keys, keys);8889if (existsp) {90ret = KADM5_PASS_REUSE;91krb5_set_error_message(context->context, ret,92"Password reuse forbidden");93goto out2;94}9596ret = hdb_seal_keys(context->context, context->db, &ent.entry);97if (ret)98goto out2;99}100ent.entry.kvno++;101102ret = _kadm5_set_modifier(context, &ent.entry);103if(ret)104goto out2;105106ret = _kadm5_bump_pw_expire(context, &ent.entry);107if (ret)108goto out2;109110ret = context->db->hdb_store(context->context, context->db,111HDB_F_REPLACE, &ent);112if (ret)113goto out2;114115kadm5_log_modify (context,116&ent.entry,117KADM5_PRINCIPAL | KADM5_MOD_NAME | KADM5_MOD_TIME |118KADM5_KEY_DATA | KADM5_KVNO | KADM5_PW_EXPIRATION |119KADM5_TL_DATA);120121out2:122hdb_free_entry(context->context, &ent);123out:124context->db->hdb_close(context->context, context->db);125return _kadm5_error_code(ret);126}127128129130/*131* change the password of `princ' to `password' if it's not already that.132*/133134kadm5_ret_t135kadm5_s_chpass_principal_cond(void *server_handle,136krb5_principal princ,137const char *password)138{139return change (server_handle, princ, password, 1);140}141142/*143* change the password of `princ' to `password'144*/145146kadm5_ret_t147kadm5_s_chpass_principal(void *server_handle,148krb5_principal princ,149const char *password)150{151return change (server_handle, princ, password, 0);152}153154/*155* change keys for `princ' to `keys'156*/157158kadm5_ret_t159kadm5_s_chpass_principal_with_key(void *server_handle,160krb5_principal princ,161int n_key_data,162krb5_key_data *key_data)163{164kadm5_server_context *context = server_handle;165hdb_entry_ex ent;166kadm5_ret_t ret;167168memset(&ent, 0, sizeof(ent));169ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);170if(ret)171return ret;172ret = context->db->hdb_fetch_kvno(context->context, context->db, princ, 0,173HDB_F_GET_ANY|HDB_F_ADMIN_DATA, &ent);174if(ret)175goto out;176ret = hdb_add_current_keys_to_history(context->context, &ent.entry);177if (ret)178goto out2;179ret = _kadm5_set_keys2(context, &ent.entry, n_key_data, key_data);180if(ret)181goto out2;182ent.entry.kvno++;183ret = _kadm5_set_modifier(context, &ent.entry);184if(ret)185goto out2;186ret = _kadm5_bump_pw_expire(context, &ent.entry);187if (ret)188goto out2;189190ret = hdb_seal_keys(context->context, context->db, &ent.entry);191if (ret)192goto out2;193194ret = context->db->hdb_store(context->context, context->db,195HDB_F_REPLACE, &ent);196if (ret)197goto out2;198199kadm5_log_modify (context,200&ent.entry,201KADM5_PRINCIPAL | KADM5_MOD_NAME | KADM5_MOD_TIME |202KADM5_KEY_DATA | KADM5_KVNO | KADM5_PW_EXPIRATION |203KADM5_TL_DATA);204205out2:206hdb_free_entry(context->context, &ent);207out:208context->db->hdb_close(context->context, context->db);209return _kadm5_error_code(ret);210}211212213