plugins/python: make show_version display the plugin in verbose mode

Before it only displayed the plugin version, now it also displays
which python plugin is loaded to be more useful.
This commit is contained in:
Robert Manner
2020-02-26 11:33:07 +01:00
committed by Todd C. Miller
parent 38fc37b214
commit f387cdf53f
6 changed files with 25 additions and 33 deletions

View File

@@ -141,14 +141,8 @@ python_plugin_approval_show_version(struct ApprovalPluginContext *approval_ctx,
struct PluginContext *plugin_ctx = BASE_CTX(approval_ctx); struct PluginContext *plugin_ctx = BASE_CTX(approval_ctx);
PyThreadState_Swap(plugin_ctx->py_interpreter); PyThreadState_Swap(plugin_ctx->py_interpreter);
if (verbose) {
py_sudo_log(SUDO_CONV_INFO_MSG, "Python approval plugin API version %d.%d\n",
SUDO_API_VERSION_GET_MAJOR(PY_APPROVAL_PLUGIN_VERSION),
SUDO_API_VERSION_GET_MINOR(PY_APPROVAL_PLUGIN_VERSION));
}
debug_return_int(python_plugin_show_version(plugin_ctx, debug_return_int(python_plugin_show_version(plugin_ctx,
CALLBACK_PYNAME(show_version), verbose)); CALLBACK_PYNAME(show_version), verbose, PY_APPROVAL_PLUGIN_VERSION, "approval"));
} }
__dso_public struct approval_plugin python_approval; __dso_public struct approval_plugin python_approval;

View File

@@ -103,7 +103,6 @@ python_plugin_audit_open(struct AuditPluginContext *audit_ctx,
MARK_CALLBACK_OPTIONAL(accept); MARK_CALLBACK_OPTIONAL(accept);
MARK_CALLBACK_OPTIONAL(reject); MARK_CALLBACK_OPTIONAL(reject);
MARK_CALLBACK_OPTIONAL(error); MARK_CALLBACK_OPTIONAL(error);
MARK_CALLBACK_OPTIONAL(show_version);
plugin_ctx->call_close = 1; plugin_ctx->call_close = 1;
rc = _call_plugin_open(audit_ctx, submit_optind, submit_argv); rc = _call_plugin_open(audit_ctx, submit_optind, submit_argv);
@@ -230,14 +229,8 @@ python_plugin_audit_show_version(struct AuditPluginContext *audit_ctx, int verbo
struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx); struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx);
PyThreadState_Swap(plugin_ctx->py_interpreter); PyThreadState_Swap(plugin_ctx->py_interpreter);
if (verbose) {
py_sudo_log(SUDO_CONV_INFO_MSG, "Python audit plugin API version %d.%d\n",
SUDO_API_VERSION_GET_MAJOR(PY_AUDIT_PLUGIN_VERSION),
SUDO_API_VERSION_GET_MINOR(PY_AUDIT_PLUGIN_VERSION));
}
debug_return_int(python_plugin_show_version(plugin_ctx, debug_return_int(python_plugin_show_version(plugin_ctx,
CALLBACK_PYNAME(show_version), verbose)); CALLBACK_PYNAME(show_version), verbose, PY_AUDIT_PLUGIN_VERSION, "audit"));
} }
__dso_public struct audit_plugin python_audit; __dso_public struct audit_plugin python_audit;

View File

@@ -401,6 +401,11 @@ python_plugin_init(struct PluginContext *plugin_ctx, char * const plugin_options
"Use 'ModulePath' plugin config option in 'sudo.conf'\n", module_path); "Use 'ModulePath' plugin config option in 'sudo.conf'\n", module_path);
goto cleanup; goto cleanup;
} }
plugin_ctx->plugin_path = strdup(module_path);
if (plugin_ctx->plugin_path == NULL) {
py_sudo_log(SUDO_CONV_ERROR_MSG, "Failed to allocate memory");
goto cleanup;
}
sudo_debug_printf(SUDO_DEBUG_DEBUG, "Loading python module from path '%s'", module_path); sudo_debug_printf(SUDO_DEBUG_DEBUG, "Loading python module from path '%s'", module_path);
plugin_ctx->py_module = _import_module(module_path); plugin_ctx->py_module = _import_module(module_path);
@@ -456,6 +461,7 @@ python_plugin_deinit(struct PluginContext *plugin_ctx)
// it is a rare tested scenario. // it is a rare tested scenario.
free(plugin_ctx->callback_error); free(plugin_ctx->callback_error);
free(plugin_ctx->plugin_path);
memset(plugin_ctx, 0, sizeof(*plugin_ctx)); memset(plugin_ctx, 0, sizeof(*plugin_ctx));
python_debug_deregister(); python_debug_deregister();
@@ -527,10 +533,20 @@ python_plugin_api_rc_call(struct PluginContext *plugin_ctx, const char *func_nam
} }
int int
python_plugin_show_version(struct PluginContext *plugin_ctx, const char *python_callback_name, int is_verbose) python_plugin_show_version(struct PluginContext *plugin_ctx, const char *python_callback_name,
int is_verbose, unsigned int plugin_api_version, const char *plugin_api_name)
{ {
debug_decl(python_plugin_show_version, PYTHON_DEBUG_CALLBACKS); debug_decl(python_plugin_show_version, PYTHON_DEBUG_CALLBACKS);
if (is_verbose) {
py_sudo_log(SUDO_CONV_INFO_MSG, "Python %s plugin (API %d.%d): %s (loaded from '%s')\n",
plugin_api_name,
SUDO_API_VERSION_GET_MAJOR(plugin_api_version),
SUDO_API_VERSION_GET_MINOR(plugin_api_version),
python_plugin_name(plugin_ctx),
plugin_ctx->plugin_path);
}
int rc = SUDO_RC_OK; int rc = SUDO_RC_OK;
if (PyObject_HasAttrString(plugin_ctx->py_instance, python_callback_name)) { if (PyObject_HasAttrString(plugin_ctx->py_instance, python_callback_name)) {
rc = python_plugin_api_rc_call(plugin_ctx, python_callback_name, rc = python_plugin_api_rc_call(plugin_ctx, python_callback_name,

View File

@@ -28,6 +28,7 @@ struct PluginContext {
PyObject *py_instance; PyObject *py_instance;
int call_close; int call_close;
unsigned int sudo_api_version; unsigned int sudo_api_version;
char *plugin_path;
// We use this to let the error string live until sudo and the audit plugins // We use this to let the error string live until sudo and the audit plugins
// are using it. // are using it.
@@ -50,7 +51,7 @@ int python_plugin_construct(struct PluginContext *plugin_ctx, unsigned int versi
void python_plugin_deinit(struct PluginContext *plugin_ctx); void python_plugin_deinit(struct PluginContext *plugin_ctx);
int python_plugin_show_version(struct PluginContext *plugin_ctx, int python_plugin_show_version(struct PluginContext *plugin_ctx,
const char *python_callback_name, int isVerbose); const char *python_callback_name, int isVerbose, unsigned int plugin_api_version, const char *plugin_api_name);
CPYCHECKER_STEALS_REFERENCE_TO_ARG(3) CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)
void python_plugin_close(struct PluginContext *plugin_ctx, const char *callback_name, void python_plugin_close(struct PluginContext *plugin_ctx, const char *callback_name,

View File

@@ -109,7 +109,6 @@ python_plugin_io_open(struct IOPluginContext *io_ctx,
} }
// skip plugin callbacks which are not mandatory // skip plugin callbacks which are not mandatory
MARK_CALLBACK_OPTIONAL(show_version);
MARK_CALLBACK_OPTIONAL(log_ttyin); MARK_CALLBACK_OPTIONAL(log_ttyin);
MARK_CALLBACK_OPTIONAL(log_ttyout); MARK_CALLBACK_OPTIONAL(log_ttyout);
MARK_CALLBACK_OPTIONAL(log_stdin); MARK_CALLBACK_OPTIONAL(log_stdin);
@@ -142,13 +141,8 @@ python_plugin_io_show_version(struct IOPluginContext *io_ctx, int verbose)
PyThreadState_Swap(BASE_CTX(io_ctx)->py_interpreter); PyThreadState_Swap(BASE_CTX(io_ctx)->py_interpreter);
if (verbose) { debug_return_int(python_plugin_show_version(BASE_CTX(io_ctx), CALLBACK_PYNAME(show_version),
py_sudo_log(SUDO_CONV_INFO_MSG, "Python io plugin API version %d.%d\n", verbose, PY_IO_PLUGIN_VERSION, "io"));
SUDO_API_VERSION_GET_MAJOR(PY_IO_PLUGIN_VERSION),
SUDO_API_VERSION_GET_MINOR(PY_IO_PLUGIN_VERSION));
}
debug_return_int(python_plugin_show_version(BASE_CTX(io_ctx), CALLBACK_PYNAME(show_version), verbose));
} }
int int

View File

@@ -73,7 +73,6 @@ python_plugin_policy_open(unsigned int version, sudo_conv_t conversation,
} }
// skip plugin callbacks which are not mandatory // skip plugin callbacks which are not mandatory
MARK_CALLBACK_OPTIONAL(show_version);
MARK_CALLBACK_OPTIONAL(list); MARK_CALLBACK_OPTIONAL(list);
MARK_CALLBACK_OPTIONAL(validate); MARK_CALLBACK_OPTIONAL(validate);
MARK_CALLBACK_OPTIONAL(invalidate); MARK_CALLBACK_OPTIONAL(invalidate);
@@ -200,13 +199,8 @@ python_plugin_policy_version(int verbose)
PyThreadState_Swap(plugin_ctx.py_interpreter); PyThreadState_Swap(plugin_ctx.py_interpreter);
if (verbose) { debug_return_int(python_plugin_show_version(&plugin_ctx, CALLBACK_PYNAME(show_version),
py_sudo_log(SUDO_CONV_INFO_MSG, "Python policy plugin API version %d.%d\n", verbose, PY_POLICY_PLUGIN_VERSION, "policy"));
SUDO_API_VERSION_GET_MAJOR(PY_POLICY_PLUGIN_VERSION),
SUDO_API_VERSION_GET_MINOR(PY_POLICY_PLUGIN_VERSION));
}
debug_return_int(python_plugin_show_version(&plugin_ctx, CALLBACK_PYNAME(show_version), verbose));
} }
int int