Files
sudo/plugins/python/pyhelpers.h
Robert Manner d67c64bb37 plugins/python/pyhelpers: have a default sudo_printf function
Adapted the default sudo_printf from sudoers plugin to be able to print
errors before plugin open() gets called. (This is used by the multiple io
plugin loading to display error for too much plugin load.)

Since this makes us always have a sudo_log, I have removed the logic about
whether it is available or not.
2020-01-23 12:46:14 -07:00

89 lines
2.8 KiB
C

/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2019 Robert Manner <robert.manner@oneidentity.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef SUDO_PLUGIN_PYHELPERS_H
#define SUDO_PLUGIN_PYHELPERS_H
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "config.h"
#include "sudo_compat.h"
#include "sudo_plugin.h"
#include "pyhelpers_cpychecker.h"
#include "sudo_python_debug.h"
enum SudoPluginFunctionReturnCode {
SUDO_RC_OK = 1,
SUDO_RC_ACCEPT = 1,
SUDO_RC_REJECT = 0,
SUDO_RC_ERROR = -1,
SUDO_RC_USAGE_ERROR = -2,
};
struct PythonContext
{
sudo_printf_t sudo_log;
sudo_conv_t sudo_conv;
int open_plugin_count;
PyObject *py_traceback_module;
};
extern struct PythonContext py_ctx;
#define Py_TYPENAME(object) (object ? Py_TYPE(object)->tp_name : "NULL")
#define Py_SSIZE2SIZE(value) ((value) < 0 ? 0 : (size_t)(value))
#define py_sudo_log(...) py_ctx.sudo_log(__VA_ARGS__)
int py_sudo_conv(int num_msgs, const struct sudo_conv_message msgs[],
struct sudo_conv_reply replies[], struct sudo_conv_callback *callback);
void py_log_last_error(const char *context_message);
char *py_create_string_rep(PyObject *py_object);
char *py_join_str_list(PyObject *py_str_list, const char *separator);
PyObject *py_from_passwd(const struct passwd *pwd);
PyObject *py_str_array_to_tuple_with_count(Py_ssize_t count, char * const strings[]);
PyObject *py_str_array_to_tuple(char * const strings[]);
char **py_str_array_from_tuple(PyObject *py_tuple);
CPYCHECKER_RETURNS_BORROWED_REF
PyObject *py_tuple_get(PyObject *py_tuple, Py_ssize_t index, PyTypeObject *expected_type);
PyObject *py_create_version(unsigned int version);
void py_debug_python_call(const char *class_name, const char *function_name,
PyObject *py_args, PyObject *py_kwargs, int subsystem_id);
void py_debug_python_result(const char *class_name, const char *function_name,
PyObject *py_args, int subsystem_id);
void str_array_free(char ***array);
int py_get_current_execution_frame(char **file_name, long *line_number, char **function_name);
void py_ctx_reset(void);
#endif // SUDO_PLUGIN_PYHELPERS_H