Path: blob/main/crypto/krb5/src/lib/kadm5/srv/server_init.c
39566 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.3*4* $Id$5* $Source$6*/7/*8* Copyright 2004 Sun Microsystems, Inc. All rights reserved.9* Use is subject to license terms.10*/1112#include "k5-int.h"13#include <com_err.h>14#include <kadm5/admin.h>15#include <krb5.h>16#include <kdb_log.h>17#include "server_internal.h"18#include "osconf.h"19#include "iprop_hdr.h"2021static int dup_db_args(kadm5_server_handle_t handle, char **db_args)22{23size_t count = 0;24int ret = 0;2526for (count=0; db_args && db_args[count]; count++);27if (count == 0) {28handle->db_args = NULL;29goto clean_n_exit;30}3132handle->db_args = calloc(sizeof(char*), count+1);33if (handle->db_args == NULL) {34ret=ENOMEM;35goto clean_n_exit;36}3738for (count=0; db_args[count]; count++) {39handle->db_args[count] = strdup(db_args[count]);40if (handle->db_args[count] == NULL) {41ret = ENOMEM;42goto clean_n_exit;43}44}4546clean_n_exit:47if (ret && handle->db_args) {48for (count=0; handle->db_args[count]; count++)49free(handle->db_args[count]);5051free(handle->db_args), handle->db_args = NULL;52}5354return ret;55}5657static void free_db_args(kadm5_server_handle_t handle)58{59size_t count;6061if (handle->db_args) {62for (count=0; handle->db_args[count]; count++)63free(handle->db_args[count]);6465free(handle->db_args), handle->db_args = NULL;66}67}6869static void70free_handle(kadm5_server_handle_t handle)71{72if (handle == NULL)73return;7475destroy_pwqual(handle);76k5_kadm5_hook_free_handles(handle->context, handle->hook_handles);77ulog_fini(handle->context);78krb5_db_fini(handle->context);79krb5_free_principal(handle->context, handle->current_caller);80kadm5_free_config_params(handle->context, &handle->params);81free(handle->lhandle);82free_db_args(handle);83free(handle);84}8586kadm5_ret_t kadm5_init_with_password(krb5_context context, char *client_name,87char *pass, char *service_name,88kadm5_config_params *params,89krb5_ui_4 struct_version,90krb5_ui_4 api_version,91char **db_args,92void **server_handle)93{94return kadm5_init(context, client_name, pass, service_name, params,95struct_version, api_version, db_args,96server_handle);97}9899kadm5_ret_t kadm5_init_anonymous(krb5_context context, char *client_name,100char *service_name,101kadm5_config_params *params,102krb5_ui_4 struct_version,103krb5_ui_4 api_version,104char **db_args,105void **server_handle)106{107return kadm5_init(context, client_name, NULL, service_name, params,108struct_version, api_version, db_args,109server_handle);110}111112kadm5_ret_t kadm5_init_with_creds(krb5_context context,113char *client_name,114krb5_ccache ccache,115char *service_name,116kadm5_config_params *params,117krb5_ui_4 struct_version,118krb5_ui_4 api_version,119char **db_args,120void **server_handle)121{122/*123* A program calling init_with_creds *never* expects to prompt124* the user. If this is KADM5_API_VERSION_2 and MKEY_FROM_KBD is125* non-zero, return an error.126*/127if (params && (params->mask & KADM5_CONFIG_MKEY_FROM_KBD) &&128params->mkey_from_kbd)129return KADM5_BAD_SERVER_PARAMS;130return kadm5_init(context, client_name, NULL, service_name, params,131struct_version, api_version, db_args,132server_handle);133}134135136kadm5_ret_t kadm5_init_with_skey(krb5_context context, char *client_name,137char *keytab, char *service_name,138kadm5_config_params *params,139krb5_ui_4 struct_version,140krb5_ui_4 api_version,141char **db_args,142void **server_handle)143{144/*145* A program calling init_with_skey *never* expects to prompt the146* user. If this is KADM5_API_VERSION_2 and MKEY_FROM_KBD is147* non-zero, return an error.148*/149if (params && (params->mask & KADM5_CONFIG_MKEY_FROM_KBD) &&150params->mkey_from_kbd)151return KADM5_BAD_SERVER_PARAMS;152return kadm5_init(context, client_name, NULL, service_name, params,153struct_version, api_version, db_args,154server_handle);155}156157kadm5_ret_t kadm5_init(krb5_context context, char *client_name, char *pass,158char *service_name,159kadm5_config_params *params_in,160krb5_ui_4 struct_version,161krb5_ui_4 api_version,162char **db_args,163void **server_handle)164{165krb5_error_code ret;166kadm5_server_handle_t handle = NULL;167kadm5_config_params params_local; /* for v1 compat */168169if (! server_handle)170return EINVAL;171172if (! client_name)173return EINVAL;174175CHECK_VERSIONS(struct_version, api_version, KADM5_OLD_SERVER_API_VERSION,176KADM5_NEW_SERVER_API_VERSION);177178handle = k5alloc(sizeof(*handle), &ret);179if (handle == NULL)180goto cleanup;181handle->context = context;182183ret = dup_db_args(handle, db_args);184if (ret)185goto cleanup;186187initialize_ovk_error_table();188initialize_ovku_error_table();189190handle->magic_number = KADM5_SERVER_HANDLE_MAGIC;191handle->struct_version = struct_version;192handle->api_version = api_version;193194/*195* Acquire relevant profile entries. Merge values196* in params_in with values from profile, based on197* params_in->mask.198*/199memset(¶ms_local, 0, sizeof(params_local));200201ret = kadm5_get_config_params(handle->context, 1, params_in,202&handle->params);203if (ret)204goto cleanup;205206#define REQUIRED_PARAMS (KADM5_CONFIG_REALM | KADM5_CONFIG_DBNAME | \207KADM5_CONFIG_ENCTYPE | \208KADM5_CONFIG_FLAGS | \209KADM5_CONFIG_MAX_LIFE | KADM5_CONFIG_MAX_RLIFE | \210KADM5_CONFIG_EXPIRATION | KADM5_CONFIG_ENCTYPES)211212#define IPROP_REQUIRED_PARAMS \213(KADM5_CONFIG_IPROP_ENABLED | \214KADM5_CONFIG_IPROP_LOGFILE | \215KADM5_CONFIG_IPROP_PORT)216217if ((handle->params.mask & REQUIRED_PARAMS) != REQUIRED_PARAMS) {218ret = KADM5_MISSING_CONF_PARAMS;219goto cleanup;220}221if ((handle->params.mask & KADM5_CONFIG_IPROP_ENABLED) == KADM5_CONFIG_IPROP_ENABLED222&& handle->params.iprop_enabled) {223if ((handle->params.mask & IPROP_REQUIRED_PARAMS) != IPROP_REQUIRED_PARAMS) {224ret = KADM5_MISSING_CONF_PARAMS;225goto cleanup;226}227}228229ret = krb5_set_default_realm(handle->context, handle->params.realm);230if (ret)231goto cleanup;232233ret = krb5_db_open(handle->context, db_args,234KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_ADMIN);235if (ret)236goto cleanup;237238ret = krb5_parse_name(handle->context, client_name,239&handle->current_caller);240if (ret)241goto cleanup;242243handle->lhandle = k5alloc(sizeof(*handle), &ret);244if (handle->lhandle == NULL)245goto cleanup;246*handle->lhandle = *handle;247handle->lhandle->api_version = KADM5_API_VERSION_4;248handle->lhandle->struct_version = KADM5_STRUCT_VERSION;249handle->lhandle->lhandle = handle->lhandle;250251ret = kdb_init_master(handle, handle->params.realm,252(handle->params.mask & KADM5_CONFIG_MKEY_FROM_KBD)253&& handle->params.mkey_from_kbd);254if (ret)255goto cleanup;256257ret = kdb_init_hist(handle, handle->params.realm);258if (ret)259goto cleanup;260261ret = k5_kadm5_hook_load(context,&handle->hook_handles);262if (ret)263goto cleanup;264265ret = init_pwqual(handle);266if (ret)267goto cleanup;268269*server_handle = handle;270handle = NULL;271272cleanup:273free_handle(handle);274return ret;275}276277kadm5_ret_t kadm5_destroy(void *server_handle)278{279CHECK_HANDLE(server_handle);280free_handle(server_handle);281return KADM5_OK;282}283284kadm5_ret_t kadm5_lock(void *server_handle)285{286kadm5_server_handle_t handle = server_handle;287kadm5_ret_t ret;288289CHECK_HANDLE(server_handle);290ret = krb5_db_lock(handle->context, KRB5_DB_LOCKMODE_EXCLUSIVE);291if (ret)292return ret;293294return KADM5_OK;295}296297kadm5_ret_t kadm5_unlock(void *server_handle)298{299kadm5_server_handle_t handle = server_handle;300kadm5_ret_t ret;301302CHECK_HANDLE(server_handle);303ret = krb5_db_unlock(handle->context);304if (ret)305return ret;306307return KADM5_OK;308}309310kadm5_ret_t kadm5_flush(void *server_handle)311{312kadm5_server_handle_t handle = server_handle;313kadm5_ret_t ret;314315CHECK_HANDLE(server_handle);316317if ((ret = krb5_db_fini(handle->context)) ||318(ret = krb5_db_open(handle->context, handle->db_args,319KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_ADMIN))) {320(void) kadm5_destroy(server_handle);321return ret;322}323return KADM5_OK;324}325326int _kadm5_check_handle(void *handle)327{328CHECK_HANDLE(handle);329return 0;330}331332#include "gssapiP_krb5.h"333krb5_error_code kadm5_init_krb5_context (krb5_context *ctx)334{335static int first_time = 1;336if (first_time) {337krb5_error_code err;338err = krb5_gss_use_kdc_context();339if (err)340return err;341first_time = 0;342}343return krb5int_init_context_kdc(ctx);344}345346krb5_error_code347kadm5_init_iprop(void *handle, char **db_args)348{349kadm5_server_handle_t iprop_h;350krb5_error_code retval;351352iprop_h = handle;353if (iprop_h->params.iprop_enabled) {354ulog_set_role(iprop_h->context, IPROP_PRIMARY);355retval = ulog_map(iprop_h->context, iprop_h->params.iprop_logfile,356iprop_h->params.iprop_ulogsize);357if (retval)358return (retval);359}360return (0);361}362363364