plugins/python/sudo_module: let a reject also supply error message

Same as sudo.PluginError exception, have a sudo.PluginReject exception
as well. Added common base exception as well.
This commit is contained in:
Robert Manner
2020-02-11 14:46:23 +01:00
committed by Todd C. Miller
parent d1f94c857c
commit b165242035
3 changed files with 27 additions and 7 deletions

View File

@@ -111,14 +111,26 @@ _import_module(const char *path)
}
void
python_plugin_handle_plugin_error_exception(struct PluginContext *plugin_ctx)
python_plugin_handle_plugin_error_exception(PyObject **py_result, struct PluginContext *plugin_ctx)
{
debug_decl(python_plugin_handle_plugin_error_exception, PYTHON_DEBUG_INTERNAL);
free(plugin_ctx->callback_error);
plugin_ctx->callback_error = NULL;
if (PyErr_Occurred() && PyErr_ExceptionMatches(sudo_exc_PluginError)) {
if (PyErr_Occurred()) {
int rc = SUDO_RC_ERROR;
if (PyErr_ExceptionMatches(sudo_exc_PluginReject)) {
rc = SUDO_RC_REJECT;
} else if (!PyErr_ExceptionMatches(sudo_exc_PluginError)) {
debug_return;
}
if (py_result != NULL) {
Py_CLEAR(*py_result);
*py_result = PyLong_FromLong(rc);
}
PyObject *py_type = NULL, *py_message = NULL, *py_traceback = NULL;
PyErr_Fetch(&py_type, &py_message, &py_traceback);
@@ -155,7 +167,7 @@ python_plugin_construct_custom(struct PluginContext *plugin_ctx, PyObject *py_kw
py_args, py_kwargs, PYTHON_DEBUG_PY_CALLS);
plugin_ctx->py_instance = PyObject_Call(plugin_ctx->py_class, py_args, py_kwargs);
python_plugin_handle_plugin_error_exception(plugin_ctx);
python_plugin_handle_plugin_error_exception(NULL, plugin_ctx);
py_debug_python_result(python_plugin_name(plugin_ctx), "__init__",
plugin_ctx->py_instance, PYTHON_DEBUG_PY_CALLS);
@@ -429,7 +441,7 @@ python_plugin_api_call(struct PluginContext *plugin_ctx, const char *func_name,
py_debug_python_result(python_plugin_name(plugin_ctx), func_name,
py_result, PYTHON_DEBUG_PY_CALLS);
python_plugin_handle_plugin_error_exception(plugin_ctx);
python_plugin_handle_plugin_error_exception(&py_result, plugin_ctx);
if (PyErr_Occurred()) {
py_log_last_error(NULL);

View File

@@ -31,7 +31,9 @@ PyAPI_FUNC(PyObject *) PyStructSequence_GetItem(PyObject *, Py_ssize_t);
// exceptions:
PyObject *sudo_exc_SudoException;
PyObject *sudo_exc_PluginException;
PyObject *sudo_exc_PluginError;
PyObject *sudo_exc_PluginReject;
static PyObject *sudo_exc_ConversationInterrupted;
// the methods exposed in the "sudo" python module
@@ -563,7 +565,11 @@ sudo_module_init(void)
} while(0);
MODULE_ADD_EXCEPTION(SudoException, NULL);
MODULE_ADD_EXCEPTION(PluginError, NULL);
MODULE_ADD_EXCEPTION(PluginException, NULL);
MODULE_ADD_EXCEPTION(PluginError, EXC_VAR(PluginException));
MODULE_ADD_EXCEPTION(PluginReject, EXC_VAR(PluginException));
MODULE_ADD_EXCEPTION(ConversationInterrupted, EXC_VAR(SudoException));
#define MODULE_REGISTER_ENUM(name, key_values) \

View File

@@ -23,8 +23,10 @@
extern PyObject *sudo_exc_SudoException; // Base exception for the sudo module problems
// This is for the python plugins to report errors for us
extern PyObject *sudo_exc_PluginError;
// This is for the python plugins to report error messages for us
extern PyObject *sudo_exc_PluginException; // base exception of the following:
extern PyObject *sudo_exc_PluginReject; // a reject with message
extern PyObject *sudo_exc_PluginError; // an error with message
extern PyTypeObject *sudo_type_Plugin;
extern PyTypeObject *sudo_type_ConvMessage;