Path: blob/main/crypto/krb5/src/lib/kadm5/srv/kadm5_hook.c
39566 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/kadm5/srv/kadm5_hook.c */2/*3* Copyright (C) 2010 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/25/* Consumer interface for kadm5_hook plugins. */2627#include "k5-int.h"28#include "server_internal.h"29#include <krb5/kadm5_hook_plugin.h>30#include <adm_proto.h>31#include <syslog.h>3233struct kadm5_hook_handle_st {34kadm5_hook_vftable_1 vt;35kadm5_hook_modinfo *data;36};3738krb5_error_code39k5_kadm5_hook_load(krb5_context context,40kadm5_hook_handle **handles_out)41{42krb5_error_code ret;43krb5_plugin_initvt_fn *modules = NULL, *mod;44size_t count;45kadm5_hook_handle *list = NULL, handle = NULL;4647*handles_out = NULL;4849ret = k5_plugin_load_all(context, PLUGIN_INTERFACE_KADM5_HOOK, &modules);50if (ret != 0)51goto cleanup;5253/* Allocate a large enough list of handles. */54for (count = 0; modules[count] != NULL; count++);55list = k5calloc(count + 1, sizeof(*list), &ret);56if (list == NULL)57goto cleanup;5859/* For each module, allocate a handle, initialize its vtable, and60* initialize the module. */61count = 0;62for (mod = modules; *mod != NULL; mod++) {63handle = k5alloc(sizeof(*handle), &ret);64if (handle == NULL)65goto cleanup;66ret = (*mod)(context, 1, 3, (krb5_plugin_vtable)&handle->vt);67if (ret != 0) { /* Failed vtable init is non-fatal. */68free(handle);69handle = NULL;70continue;71}72handle->data = NULL;73if (handle->vt.init != NULL) {74ret = handle->vt.init(context, &handle->data);75if (ret != 0) /* Failed initialization is fatal. */76goto cleanup;77}78list[count++] = handle;79list[count] = NULL;80handle = NULL;81}82list[count] = NULL;8384ret = 0;85*handles_out = list;86list = NULL;8788cleanup:89free(handle);90k5_plugin_free_modules(context, modules);91k5_kadm5_hook_free_handles(context, list);92return ret;93}9495void96k5_kadm5_hook_free_handles(krb5_context context, kadm5_hook_handle *handles)97{98kadm5_hook_handle *hp, handle;99100if (handles == NULL)101return;102for (hp = handles; *hp != NULL; hp++) {103handle = *hp;104if (handle->vt.fini != NULL)105handle->vt.fini(context, handle->data);106free(handle);107}108free(handles);109}110111static void112log_failure(krb5_context context,113const char *name,114const char *function,115krb5_error_code ret)116{117const char *e = krb5_get_error_message(context, ret);118119krb5_klog_syslog(LOG_ERR, _("kadm5_hook %s failed postcommit %s: %s"),120name, function, e);121krb5_free_error_message(context, e);122}123124#define ITERATE(operation, params) \125for (; *handles; handles++) { \126kadm5_hook_handle h = *handles; \127krb5_error_code ret = 0; \128if (h->vt.operation) { \129ret = h->vt.operation params; \130} \131if (ret) { \132if (stage == KADM5_HOOK_STAGE_PRECOMMIT) \133return ret; \134else \135log_failure(context, h->vt.name, #operation, ret); \136} \137}138139140kadm5_ret_t141k5_kadm5_hook_chpass(krb5_context context, kadm5_hook_handle *handles,142int stage, krb5_principal princ, krb5_boolean keepold,143int n_ks_tuple, krb5_key_salt_tuple *ks_tuple,144const char *newpass)145{146ITERATE(chpass, (context, h->data,147stage, princ, keepold,148n_ks_tuple, ks_tuple, newpass));149return 0;150}151152kadm5_ret_t153k5_kadm5_hook_create(krb5_context context, kadm5_hook_handle *handles,154int stage, kadm5_principal_ent_t princ, long mask,155int n_ks_tuple, krb5_key_salt_tuple *ks_tuple,156const char *newpass)157{158ITERATE(create, (context, h->data,159stage, princ, mask, n_ks_tuple, ks_tuple, newpass));160return 0;161}162163kadm5_ret_t164k5_kadm5_hook_modify(krb5_context context, kadm5_hook_handle *handles,165int stage, kadm5_principal_ent_t princ, long mask)166{167ITERATE(modify, (context, h->data, stage, princ, mask));168return 0;169}170171kadm5_ret_t172k5_kadm5_hook_rename(krb5_context context, kadm5_hook_handle *handles,173int stage, krb5_principal oprinc, krb5_principal nprinc)174{175ITERATE(rename, (context, h->data, stage, oprinc, nprinc));176return 0;177}178179kadm5_ret_t180k5_kadm5_hook_remove(krb5_context context, kadm5_hook_handle *handles,181int stage, krb5_principal princ)182{183ITERATE(remove, (context, h->data, stage, princ));184return 0;185}186187kadm5_ret_t188k5_kadm5_hook_alias(krb5_context context, kadm5_hook_handle *handles,189int stage, krb5_principal alias, krb5_principal target)190{191ITERATE(alias, (context, h->data, stage, alias, target));192return 0;193}194195196