Path: blob/main/crypto/heimdal/lib/kadm5/create_s.c
34878 views
/*1* Copyright (c) 1997-2001 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_t38get_default(kadm5_server_context *context, krb5_principal princ,39kadm5_principal_ent_t def)40{41kadm5_ret_t ret;42krb5_principal def_principal;43krb5_const_realm realm = krb5_principal_get_realm(context->context, princ);4445ret = krb5_make_principal(context->context, &def_principal,46realm, "default", NULL);47if (ret)48return ret;49ret = kadm5_s_get_principal(context, def_principal, def,50KADM5_PRINCIPAL_NORMAL_MASK);51krb5_free_principal (context->context, def_principal);52return ret;53}5455static kadm5_ret_t56create_principal(kadm5_server_context *context,57kadm5_principal_ent_t princ,58uint32_t mask,59hdb_entry_ex *ent,60uint32_t required_mask,61uint32_t forbidden_mask)62{63kadm5_ret_t ret;64kadm5_principal_ent_rec defrec, *defent;65uint32_t def_mask;6667memset(ent, 0, sizeof(*ent));68if((mask & required_mask) != required_mask)69return KADM5_BAD_MASK;70if((mask & forbidden_mask))71return KADM5_BAD_MASK;72if((mask & KADM5_POLICY) && strcmp(princ->policy, "default"))73/* XXX no real policies for now */74return KADM5_UNK_POLICY;75ret = krb5_copy_principal(context->context, princ->principal,76&ent->entry.principal);77if(ret)78return ret;7980defent = &defrec;81ret = get_default(context, princ->principal, defent);82if(ret) {83defent = NULL;84def_mask = 0;85} else {86def_mask = KADM5_ATTRIBUTES | KADM5_MAX_LIFE | KADM5_MAX_RLIFE;87}8889ret = _kadm5_setup_entry(context,90ent, mask | def_mask,91princ, mask,92defent, def_mask);93if(defent)94kadm5_free_principal_ent(context, defent);95if (ret)96return ret;9798ent->entry.created_by.time = time(NULL);99100return krb5_copy_principal(context->context, context->caller,101&ent->entry.created_by.principal);102}103104kadm5_ret_t105kadm5_s_create_principal_with_key(void *server_handle,106kadm5_principal_ent_t princ,107uint32_t mask)108{109kadm5_ret_t ret;110hdb_entry_ex ent;111kadm5_server_context *context = server_handle;112113ret = create_principal(context, princ, mask, &ent,114KADM5_PRINCIPAL | KADM5_KEY_DATA,115KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME116| KADM5_MOD_NAME | KADM5_MKVNO117| KADM5_AUX_ATTRIBUTES118| KADM5_POLICY_CLR | KADM5_LAST_SUCCESS119| KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);120if(ret)121goto out;122123if ((mask & KADM5_KVNO) == 0)124ent.entry.kvno = 1;125126ret = hdb_seal_keys(context->context, context->db, &ent.entry);127if (ret)128goto out;129130ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);131if(ret)132goto out;133ret = context->db->hdb_store(context->context, context->db, 0, &ent);134context->db->hdb_close(context->context, context->db);135if (ret)136goto out;137kadm5_log_create (context, &ent.entry);138139out:140hdb_free_entry(context->context, &ent);141return _kadm5_error_code(ret);142}143144145kadm5_ret_t146kadm5_s_create_principal(void *server_handle,147kadm5_principal_ent_t princ,148uint32_t mask,149const char *password)150{151kadm5_ret_t ret;152hdb_entry_ex ent;153kadm5_server_context *context = server_handle;154155ret = create_principal(context, princ, mask, &ent,156KADM5_PRINCIPAL,157KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME158| KADM5_MOD_NAME | KADM5_MKVNO159| KADM5_AUX_ATTRIBUTES | KADM5_KEY_DATA160| KADM5_POLICY_CLR | KADM5_LAST_SUCCESS161| KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT);162if(ret)163goto out;164165if ((mask & KADM5_KVNO) == 0)166ent.entry.kvno = 1;167168ent.entry.keys.len = 0;169ent.entry.keys.val = NULL;170171ret = fbsd_ossl_provider_load();172if (ret)173goto out;174175ret = _kadm5_set_keys(context, &ent.entry, password);176if (ret)177goto out;178179ret = hdb_seal_keys(context->context, context->db, &ent.entry);180if (ret)181goto out;182183ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);184if(ret)185goto out;186ret = context->db->hdb_store(context->context, context->db, 0, &ent);187context->db->hdb_close(context->context, context->db);188if (ret)189goto out;190191kadm5_log_create (context, &ent.entry);192193out:194hdb_free_entry(context->context, &ent);195return _kadm5_error_code(ret);196}197198199200