Path: blob/main/plugins/python/python_plugin_common.h
1532 views
/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2019-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#ifndef SUDO_PYTHON_PLUGIN_COMMON_H19#define SUDO_PYTHON_PLUGIN_COMMON_H2021#include "pyhelpers.h"2223struct PluginContext {24PyThreadState *py_interpreter;25PyObject *py_module;26PyObject *py_class;27PyObject *py_instance;28int call_close;29unsigned int sudo_api_version;30char *plugin_path;3132// We use this to let the error string live until sudo and the audit plugins33// are using it.34char *callback_error;35};3637int python_plugin_register_logging(sudo_conv_t conversation, sudo_printf_t sudo_printf, char * const settings[]);3839int python_plugin_init(struct PluginContext *plugin_ctx, char * const plugin_options[], unsigned int version);4041int python_plugin_construct_custom(struct PluginContext *plugin_ctx, PyObject *py_kwargs);4243PyObject *python_plugin_construct_args(unsigned int version, char *const settings[],44char *const user_info[], char *const user_env[], char *const plugin_options[]);4546int python_plugin_construct(struct PluginContext *plugin_ctx, unsigned int version,47char *const settings[], char *const user_info[],48char *const user_env[], char *const plugin_options[]);4950void python_plugin_deinit(struct PluginContext *plugin_ctx);5152int python_plugin_show_version(struct PluginContext *plugin_ctx,53const char *python_callback_name, int isVerbose, unsigned int plugin_api_version, const char *plugin_api_name);5455CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)56void python_plugin_close(struct PluginContext *plugin_ctx, const char *callback_name,57PyObject *py_args);5859CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)60PyObject *python_plugin_api_call(struct PluginContext *plugin_ctx,61const char *func_name, PyObject *py_args);6263CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)64int python_plugin_api_rc_call(struct PluginContext *plugin_ctx,65const char *func_name, PyObject *py_args);6667int python_plugin_rc_to_int(PyObject *py_result);6869void python_plugin_mark_callback_optional(struct PluginContext *plugin_ctx,70const char *function_name, void **function);7172const char *python_plugin_name(struct PluginContext *plugin_ctx);7374// sets the callback error stored in plugin_ctx into "errstr" but only if API75// version is enough and "errstr" is valid76#define CALLBACK_SET_ERROR(plugin_ctx, errstr) \77do { \78if ((plugin_ctx)->sudo_api_version >= SUDO_API_MKVERSION(1, 15)) { \79if (errstr != NULL) \80*errstr = (plugin_ctx)->callback_error; \81} \82} while(0)8384#endif // SUDO_PYTHON_PLUGIN_COMMON_H858687