Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/python/python_plugin_common.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_PYTHON_PLUGIN_COMMON_H
20
#define SUDO_PYTHON_PLUGIN_COMMON_H
21
22
#include "pyhelpers.h"
23
24
struct PluginContext {
25
PyThreadState *py_interpreter;
26
PyObject *py_module;
27
PyObject *py_class;
28
PyObject *py_instance;
29
int call_close;
30
unsigned int sudo_api_version;
31
char *plugin_path;
32
33
// We use this to let the error string live until sudo and the audit plugins
34
// are using it.
35
char *callback_error;
36
};
37
38
int python_plugin_register_logging(sudo_conv_t conversation, sudo_printf_t sudo_printf, char * const settings[]);
39
40
int python_plugin_init(struct PluginContext *plugin_ctx, char * const plugin_options[], unsigned int version);
41
42
int python_plugin_construct_custom(struct PluginContext *plugin_ctx, PyObject *py_kwargs);
43
44
PyObject *python_plugin_construct_args(unsigned int version, char *const settings[],
45
char *const user_info[], char *const user_env[], char *const plugin_options[]);
46
47
int python_plugin_construct(struct PluginContext *plugin_ctx, unsigned int version,
48
char *const settings[], char *const user_info[],
49
char *const user_env[], char *const plugin_options[]);
50
51
void python_plugin_deinit(struct PluginContext *plugin_ctx);
52
53
int python_plugin_show_version(struct PluginContext *plugin_ctx,
54
const char *python_callback_name, int isVerbose, unsigned int plugin_api_version, const char *plugin_api_name);
55
56
CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)
57
void python_plugin_close(struct PluginContext *plugin_ctx, const char *callback_name,
58
PyObject *py_args);
59
60
CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)
61
PyObject *python_plugin_api_call(struct PluginContext *plugin_ctx,
62
const char *func_name, PyObject *py_args);
63
64
CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)
65
int python_plugin_api_rc_call(struct PluginContext *plugin_ctx,
66
const char *func_name, PyObject *py_args);
67
68
int python_plugin_rc_to_int(PyObject *py_result);
69
70
void python_plugin_mark_callback_optional(struct PluginContext *plugin_ctx,
71
const char *function_name, void **function);
72
73
const char *python_plugin_name(struct PluginContext *plugin_ctx);
74
75
// sets the callback error stored in plugin_ctx into "errstr" but only if API
76
// version is enough and "errstr" is valid
77
#define CALLBACK_SET_ERROR(plugin_ctx, errstr) \
78
do { \
79
if ((plugin_ctx)->sudo_api_version >= SUDO_API_MKVERSION(1, 15)) { \
80
if (errstr != NULL) \
81
*errstr = (plugin_ctx)->callback_error; \
82
} \
83
} while(0)
84
85
#endif // SUDO_PYTHON_PLUGIN_COMMON_H
86
87