Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/python/pyhelpers.h
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2019-2020 Robert Manner <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#ifndef SUDO_PLUGIN_PYHELPERS_H
20
#define SUDO_PLUGIN_PYHELPERS_H
21
22
#define PY_SSIZE_T_CLEAN
23
#include <Python.h>
24
25
/* Python may be built with 32-bit time_t support on some platforms. */
26
#undef SIZEOF_TIME_T
27
28
#include <config.h>
29
#include <sudo_compat.h>
30
#include <sudo_plugin.h>
31
32
#include "pyhelpers_cpychecker.h"
33
34
#include "sudo_python_debug.h"
35
36
enum SudoPluginFunctionReturnCode {
37
SUDO_RC_OK = 1,
38
SUDO_RC_ACCEPT = 1,
39
SUDO_RC_REJECT = 0,
40
SUDO_RC_ERROR = -1,
41
SUDO_RC_USAGE_ERROR = -2,
42
};
43
44
#define INTERPRETER_MAX 32
45
46
struct PythonContext
47
{
48
sudo_printf_t sudo_log;
49
sudo_conv_t sudo_conv;
50
PyThreadState *py_main_interpreter;
51
size_t interpreter_count;
52
PyThreadState *py_subinterpreters[INTERPRETER_MAX];
53
};
54
55
extern struct PythonContext py_ctx;
56
57
#define Py_TYPENAME(object) (object ? Py_TYPE(object)->tp_name : "NULL")
58
59
#define py_sudo_log(...) py_ctx.sudo_log(__VA_ARGS__)
60
61
int py_sudo_conv(int num_msgs, const struct sudo_conv_message msgs[],
62
struct sudo_conv_reply replies[], struct sudo_conv_callback *callback);
63
64
void py_log_last_error(const char *context_message);
65
66
char *py_create_string_rep(PyObject *py_object);
67
68
char *py_join_str_list(PyObject *py_str_list, const char *separator);
69
70
struct key_value_str_int
71
{
72
const char *key;
73
int value;
74
};
75
76
PyObject *py_dict_create_string_int(size_t count, struct key_value_str_int *key_values);
77
78
PyObject *py_from_passwd(const struct passwd *pwd);
79
80
PyObject *py_str_array_to_tuple_with_count(Py_ssize_t count, char * const strings[]);
81
PyObject *py_str_array_to_tuple(char * const strings[]);
82
char **py_str_array_from_tuple(PyObject *py_tuple);
83
84
CPYCHECKER_RETURNS_BORROWED_REF
85
PyObject *py_tuple_get(PyObject *py_tuple, Py_ssize_t index, PyTypeObject *expected_type);
86
87
PyObject *py_object_get_optional_attr(PyObject *py_object, const char *attr, PyObject *py_default);
88
long py_object_get_optional_attr_number(PyObject *py_object, const char *attr_name);
89
const char *py_object_get_optional_attr_string(PyObject *py_object, const char *attr_name);
90
91
void py_object_set_attr_number(PyObject *py_object, const char *attr_name, long number);
92
void py_object_set_attr_string(PyObject *py_object, const char *attr_name, const char *value);
93
94
PyObject *py_create_version(unsigned int version);
95
96
void py_debug_python_call(const char *class_name, const char *function_name,
97
PyObject *py_args, PyObject *py_kwargs,
98
unsigned int subsystem_id);
99
void py_debug_python_result(const char *class_name, const char *function_name,
100
PyObject *py_args, unsigned int subsystem_id);
101
102
void str_array_free(char ***array);
103
104
int py_get_current_execution_frame(char **file_name, long *line_number, char **function_name);
105
106
void py_ctx_reset(void);
107
108
#endif // SUDO_PLUGIN_PYHELPERS_H
109
110