plugins/python/python_plugin_common: close can get custom arguments

For the audit plugin.
Ensure we do not fail if plugin_ctx->py_instance is NULL (because
plugin init has failed).
This commit is contained in:
Robert Manner
2020-02-06 15:15:12 +01:00
committed by Todd C. Miller
parent a7eb9d3412
commit 36694fce86
4 changed files with 23 additions and 13 deletions

View File

@@ -431,22 +431,30 @@ python_plugin_show_version(struct PluginContext *plugin_ctx, const char *python_
}
void
python_plugin_close(struct PluginContext *plugin_ctx, const char *python_callback_name, int exit_status, int error)
python_plugin_close(struct PluginContext *plugin_ctx, const char *callback_name,
PyObject *py_args)
{
debug_decl(python_plugin_close, PYTHON_DEBUG_CALLBACKS);
PyThreadState_Swap(plugin_ctx->py_interpreter);
if (!plugin_ctx->call_close) {
sudo_debug_printf(SUDO_DEBUG_INFO, "Skipping close call, because there was no command run\n");
// Note, this should handle the case when init has failed
if (plugin_ctx->py_instance != NULL) {
if (!plugin_ctx->call_close) {
sudo_debug_printf(SUDO_DEBUG_INFO, "Skipping close call, because there was no command run\n");
} else if (!PyObject_HasAttrString(plugin_ctx->py_instance, python_callback_name)) {
sudo_debug_printf(SUDO_DEBUG_INFO, "Python plugin function 'close' is skipped (not present)\n");
} else {
PyObject *py_result = python_plugin_api_call(plugin_ctx, python_callback_name,
Py_BuildValue("(ii)", error == 0 ? exit_status : -1, error));
Py_XDECREF(py_result);
} else if (!PyObject_HasAttrString(plugin_ctx->py_instance, callback_name)) {
sudo_debug_printf(SUDO_DEBUG_INFO, "Python plugin function 'close' is skipped (not present)\n");
} else {
PyObject *py_result = python_plugin_api_call(plugin_ctx, callback_name, py_args);
Py_XDECREF(py_result);
}
}
if (PyErr_Occurred()) {
py_log_last_error(NULL);
}
python_plugin_deinit(plugin_ctx);
debug_return;

View File

@@ -44,8 +44,8 @@ void python_plugin_deinit(struct PluginContext *plugin_ctx);
int python_plugin_show_version(struct PluginContext *plugin_ctx,
const char *python_callback_name, int isVerbose);
void python_plugin_close(struct PluginContext *plugin_ctx, const char *python_callback_name,
int exit_status, int error);
void python_plugin_close(struct PluginContext *plugin_ctx, const char *callback_name,
PyObject *py_args);
CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)
PyObject *python_plugin_api_call(struct PluginContext *plugin_ctx,

View File

@@ -125,7 +125,8 @@ void
python_plugin_io_close(struct IOPluginContext *io_ctx, int exit_status, int error)
{
debug_decl(python_plugin_io_close, PYTHON_DEBUG_CALLBACKS);
python_plugin_close(BASE_CTX(io_ctx), CALLBACK_PYNAME(close), exit_status, error);
python_plugin_close(BASE_CTX(io_ctx), CALLBACK_PYNAME(close),
Py_BuildValue("(ii)", error == 0 ? exit_status : -1, error));
debug_return;
}

View File

@@ -85,7 +85,8 @@ static void
python_plugin_policy_close(int exit_status, int error)
{
debug_decl(python_plugin_policy_close, PYTHON_DEBUG_CALLBACKS);
python_plugin_close(&plugin_ctx, CALLBACK_PYNAME(close), exit_status, error);
python_plugin_close(&plugin_ctx, CALLBACK_PYNAME(close),
Py_BuildValue("(ii)", error == 0 ? exit_status : -1, error));
debug_return;
}