Files
sudo/plugins/python/pyhelpers.h
Robert Manner 21c02e1732 plugins/python/sudo_python_module.c: use IntEnums instead of constants
It is a bit more code, but it is more "pythonic" and easier to debug
as the enum values also know their names.

It is also an API break, eg. sudo.RC_OK becomes sudo.RC.OK as sudo.RC will
be the "type" of the enum, but I guess that is acceptable before the
initial release.
2020-02-10 05:24:16 -07:00

103 lines
3.4 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;
PyThreadState *py_main_interpreter;
};
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);
struct key_value_str_int
{
const char *key;
int value;
};
PyObject *py_dict_create_string_int(size_t count, struct key_value_str_int *key_values);
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_object_get_optional_attr(PyObject *py_object, const char *attr, PyObject *py_default);
long long py_object_get_optional_attr_number(PyObject *py_object, const char *attr_name);
const char *py_object_get_optional_attr_string(PyObject *py_object, const char *attr_name);
void py_object_set_attr_number(PyObject *py_object, const char *attr_name, long long number);
void py_object_set_attr_string(PyObject *py_object, const char *attr_name, const char *value);
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