Path: blob/main/plugins/python/python_plugin_approval.c
1532 views
/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2020 Robert Manner <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include "python_plugin_common.h"1920struct ApprovalPluginContext21{22struct PluginContext base_ctx;23struct approval_plugin *plugin;24};2526#define BASE_CTX(approval_ctx) (&(approval_ctx->base_ctx))2728#define PY_APPROVAL_PLUGIN_VERSION SUDO_API_MKVERSION(1, 0)2930#define CALLBACK_PLUGINFUNC(func_name) approval_ctx->plugin->func_name3132// This also verifies compile time that the name matches the sudo plugin API.33#define CALLBACK_PYNAME(func_name) ((void)CALLBACK_PLUGINFUNC(func_name), #func_name)3435sudo_dso_public struct approval_plugin *python_approval_clone(void);3637static int38python_plugin_approval_open(struct ApprovalPluginContext *approval_ctx,39unsigned int version, sudo_conv_t conversation, sudo_printf_t sudo_printf,40char * const settings[], char * const user_info[], int submit_optind,41char * const submit_argv[], char * const submit_envp[],42char * const plugin_options[], const char **errstr)43{44debug_decl(python_plugin_approval_open, PYTHON_DEBUG_CALLBACKS);45(void) version;4647int rc = python_plugin_register_logging(conversation, sudo_printf, settings);48if (rc != SUDO_RC_OK) {49debug_return_int(rc);50}5152struct PluginContext *plugin_ctx = BASE_CTX(approval_ctx);5354rc = python_plugin_init(plugin_ctx, plugin_options, version);55if (rc != SUDO_RC_OK) {56debug_return_int(rc);57}5859PyObject *py_kwargs = NULL, *py_submit_optind = NULL,60*py_submit_argv = NULL;6162if ((py_kwargs = python_plugin_construct_args(version, settings, user_info,63submit_envp, plugin_options)) == NULL ||64(py_submit_optind = PyLong_FromLong(submit_optind)) == NULL ||65(py_submit_argv = py_str_array_to_tuple(submit_argv)) == NULL)66{67py_log_last_error("Failed to construct plugin instance");68rc = SUDO_RC_ERROR;69} else {70PyDict_SetItemString(py_kwargs, "submit_optind", py_submit_optind);71PyDict_SetItemString(py_kwargs, "submit_argv", py_submit_argv);7273rc = python_plugin_construct_custom(plugin_ctx, py_kwargs);74CALLBACK_SET_ERROR(plugin_ctx, errstr);75}7677Py_CLEAR(py_kwargs);78Py_CLEAR(py_submit_argv);79Py_CLEAR(py_submit_optind);8081if (rc != SUDO_RC_OK) {82debug_return_int(rc);83}8485debug_return_int(rc);86}8788static void89python_plugin_approval_close(struct ApprovalPluginContext *approval_ctx)90{91debug_decl(python_plugin_approval_close, PYTHON_DEBUG_CALLBACKS);9293struct PluginContext *plugin_ctx = BASE_CTX(approval_ctx);94PyThreadState_Swap(plugin_ctx->py_interpreter);95python_plugin_deinit(plugin_ctx);9697debug_return;98}99100static int101python_plugin_approval_check(struct ApprovalPluginContext *approval_ctx,102char * const command_info[], char * const run_argv[],103char * const run_envp[], const char **errstr)104{105debug_decl(python_plugin_approval_check, PYTHON_DEBUG_CALLBACKS);106107struct PluginContext *plugin_ctx = BASE_CTX(approval_ctx);108109PyObject *py_command_info = NULL, *py_run_argv = NULL, *py_run_envp = NULL,110*py_args = NULL;111112int rc = SUDO_RC_ERROR;113if ((py_command_info = py_str_array_to_tuple(command_info)) != NULL &&114(py_run_argv = py_str_array_to_tuple(run_argv)) != NULL &&115(py_run_envp = py_str_array_to_tuple(run_envp)) != NULL)116{117py_args = Py_BuildValue("(OOO)", py_command_info, py_run_argv, py_run_envp);118}119120// Note, py_args gets cleared by api_rc_call121rc = python_plugin_api_rc_call(plugin_ctx, CALLBACK_PYNAME(check), py_args);122CALLBACK_SET_ERROR(plugin_ctx, errstr);123124Py_CLEAR(py_command_info);125Py_CLEAR(py_run_argv);126Py_CLEAR(py_run_envp);127128debug_return_int(rc);129}130131static int132python_plugin_approval_show_version(struct ApprovalPluginContext *approval_ctx, int verbose)133{134debug_decl(python_plugin_approval_show_version, PYTHON_DEBUG_CALLBACKS);135136struct PluginContext *plugin_ctx = BASE_CTX(approval_ctx);137PyThreadState_Swap(plugin_ctx->py_interpreter);138139debug_return_int(python_plugin_show_version(plugin_ctx,140CALLBACK_PYNAME(show_version), verbose, PY_APPROVAL_PLUGIN_VERSION, "approval"));141}142143sudo_dso_public struct approval_plugin python_approval;144145// generate symbols for loading multiple approval plugins:146#define APPROVAL_SYMBOL_NAME(symbol) symbol147#include "python_plugin_approval_multi.inc"148#define APPROVAL_SYMBOL_NAME(symbol) symbol##1149#include "python_plugin_approval_multi.inc"150#define APPROVAL_SYMBOL_NAME(symbol) symbol##2151#include "python_plugin_approval_multi.inc"152#define APPROVAL_SYMBOL_NAME(symbol) symbol##3153#include "python_plugin_approval_multi.inc"154#define APPROVAL_SYMBOL_NAME(symbol) symbol##4155#include "python_plugin_approval_multi.inc"156#define APPROVAL_SYMBOL_NAME(symbol) symbol##5157#include "python_plugin_approval_multi.inc"158#define APPROVAL_SYMBOL_NAME(symbol) symbol##6159#include "python_plugin_approval_multi.inc"160#define APPROVAL_SYMBOL_NAME(symbol) symbol##7161#include "python_plugin_approval_multi.inc"162163static struct approval_plugin *extra_approval_plugins[] = {164&python_approval1,165&python_approval2,166&python_approval3,167&python_approval4,168&python_approval5,169&python_approval6,170&python_approval7171};172173struct approval_plugin *174python_approval_clone(void)175{176static size_t counter = 0;177struct approval_plugin *next_plugin = NULL;178179size_t max = sizeof(extra_approval_plugins) / sizeof(*extra_approval_plugins);180if (counter < max) {181next_plugin = extra_approval_plugins[counter];182++counter;183} else if (counter == max) {184++counter;185py_sudo_log(SUDO_CONV_ERROR_MSG,186"sudo: loading more than %d sudo python approval plugins is not supported\n", counter);187}188189return next_plugin;190}191192193