Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/python/python_plugin_audit.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 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
#include "python_plugin_common.h"
20
21
struct AuditPluginContext
22
{
23
struct PluginContext base_ctx;
24
struct audit_plugin *plugin;
25
};
26
27
#define BASE_CTX(audit_ctx) (&(audit_ctx->base_ctx))
28
29
#define PY_AUDIT_PLUGIN_VERSION SUDO_API_MKVERSION(1, 0)
30
31
#define CALLBACK_PLUGINFUNC(func_name) audit_ctx->plugin->func_name
32
33
// This also verifies compile time that the name matches the sudo plugin API.
34
#define CALLBACK_PYNAME(func_name) ((void)CALLBACK_PLUGINFUNC(func_name), #func_name)
35
36
#define MARK_CALLBACK_OPTIONAL(function_name) \
37
do { \
38
python_plugin_mark_callback_optional(plugin_ctx, CALLBACK_PYNAME(function_name), \
39
(void **)&CALLBACK_PLUGINFUNC(function_name)); \
40
} while(0)
41
42
sudo_dso_public struct audit_plugin *python_audit_clone(void);
43
44
static int
45
_call_plugin_open(struct AuditPluginContext *audit_ctx, int submit_optind, char * const submit_argv[])
46
{
47
debug_decl(_call_plugin_open, PYTHON_DEBUG_CALLBACKS);
48
49
struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx);
50
if (!PyObject_HasAttrString(plugin_ctx->py_instance, CALLBACK_PYNAME(open))) {
51
debug_return_int(SUDO_RC_OK);
52
}
53
54
int rc = SUDO_RC_ERROR;
55
PyObject *py_submit_argv = py_str_array_to_tuple(submit_argv);
56
57
if (py_submit_argv != NULL) {
58
rc = python_plugin_api_rc_call(plugin_ctx, CALLBACK_PYNAME(open),
59
Py_BuildValue("(iO)", submit_optind, py_submit_argv));
60
}
61
62
Py_XDECREF(py_submit_argv);
63
debug_return_int(rc);
64
}
65
66
static int
67
python_plugin_audit_open(struct AuditPluginContext *audit_ctx,
68
unsigned int version, sudo_conv_t conversation,
69
sudo_printf_t sudo_printf, char * const settings[],
70
char * const user_info[], int submit_optind,
71
char * const submit_argv[], char * const submit_envp[],
72
char * const plugin_options[], const char **errstr)
73
{
74
debug_decl(python_plugin_audit_open, PYTHON_DEBUG_CALLBACKS);
75
(void) version;
76
77
int rc = python_plugin_register_logging(conversation, sudo_printf, settings);
78
if (rc != SUDO_RC_OK) {
79
debug_return_int(rc);
80
}
81
82
struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx);
83
84
rc = python_plugin_init(plugin_ctx, plugin_options, version);
85
if (rc != SUDO_RC_OK) {
86
debug_return_int(rc);
87
}
88
89
rc = python_plugin_construct(plugin_ctx, PY_AUDIT_PLUGIN_VERSION, settings,
90
user_info, submit_envp, plugin_options);
91
CALLBACK_SET_ERROR(plugin_ctx, errstr);
92
if (rc != SUDO_RC_OK) {
93
debug_return_int(rc);
94
}
95
96
// skip plugin callbacks which are not mandatory
97
MARK_CALLBACK_OPTIONAL(accept);
98
MARK_CALLBACK_OPTIONAL(reject);
99
MARK_CALLBACK_OPTIONAL(error);
100
101
plugin_ctx->call_close = 1;
102
rc = _call_plugin_open(audit_ctx, submit_optind, submit_argv);
103
CALLBACK_SET_ERROR(plugin_ctx, errstr);
104
105
if (PyErr_Occurred()) {
106
py_log_last_error("Error during calling audit open");
107
}
108
109
debug_return_int(rc);
110
}
111
112
static void
113
python_plugin_audit_close(struct AuditPluginContext *audit_ctx, int status_type, int status)
114
{
115
debug_decl(python_plugin_audit_close, PYTHON_DEBUG_CALLBACKS);
116
117
python_plugin_close(BASE_CTX(audit_ctx), CALLBACK_PYNAME(close),
118
Py_BuildValue("(ii)", status_type, status));
119
120
debug_return;
121
}
122
123
static int
124
python_plugin_audit_accept(struct AuditPluginContext *audit_ctx,
125
const char *plugin_name, unsigned int plugin_type,
126
char * const command_info[], char * const run_argv[],
127
char * const run_envp[], const char **errstr)
128
{
129
debug_decl(python_plugin_audit_accept, PYTHON_DEBUG_CALLBACKS);
130
131
struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx);
132
PyThreadState_Swap(plugin_ctx->py_interpreter);
133
134
PyObject *py_command_info = NULL, *py_run_argv = NULL, *py_run_envp = NULL;
135
int rc = SUDO_RC_ERROR;
136
137
py_run_argv = py_str_array_to_tuple(run_argv);
138
if (py_run_argv == NULL)
139
goto cleanup;
140
141
py_command_info = py_str_array_to_tuple(command_info);
142
if (py_command_info == NULL)
143
goto cleanup;
144
145
py_run_envp = py_str_array_to_tuple(run_envp);
146
if (py_run_envp == NULL)
147
goto cleanup;
148
149
PyObject *py_args = Py_BuildValue("(ziOOO)", plugin_name, plugin_type, py_command_info, py_run_argv, py_run_envp);
150
rc = python_plugin_api_rc_call(plugin_ctx, CALLBACK_PYNAME(accept), py_args);
151
CALLBACK_SET_ERROR(plugin_ctx, errstr);
152
153
cleanup:
154
Py_CLEAR(py_command_info);
155
Py_CLEAR(py_run_argv);
156
Py_CLEAR(py_run_envp);
157
158
debug_return_int(rc);
159
}
160
161
static int
162
python_plugin_audit_reject(struct AuditPluginContext *audit_ctx,
163
const char *plugin_name, unsigned int plugin_type,
164
const char *audit_msg, char * const command_info[], const char **errstr)
165
{
166
debug_decl(python_plugin_audit_reject, PYTHON_DEBUG_CALLBACKS);
167
168
struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx);
169
PyThreadState_Swap(plugin_ctx->py_interpreter);
170
171
PyObject *py_command_info = NULL;
172
int rc = SUDO_RC_ERROR;
173
174
py_command_info = py_str_array_to_tuple(command_info);
175
if (PyErr_Occurred())
176
goto cleanup;
177
178
PyObject *py_args = Py_BuildValue("(zizO)", plugin_name, plugin_type, audit_msg, py_command_info);
179
rc = python_plugin_api_rc_call(plugin_ctx, CALLBACK_PYNAME(reject), py_args);
180
181
CALLBACK_SET_ERROR(plugin_ctx, errstr);
182
183
cleanup:
184
Py_CLEAR(py_command_info);
185
if (PyErr_Occurred())
186
py_log_last_error("Error during calling audit reject");
187
188
debug_return_int(rc);
189
}
190
191
static int
192
python_plugin_audit_error(struct AuditPluginContext *audit_ctx,
193
const char *plugin_name, unsigned int plugin_type,
194
const char *audit_msg, char * const command_info[], const char **errstr)
195
{
196
debug_decl(python_plugin_audit_error, PYTHON_DEBUG_CALLBACKS);
197
198
struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx);
199
PyThreadState_Swap(plugin_ctx->py_interpreter);
200
201
PyObject *py_command_info = NULL;
202
int rc = SUDO_RC_ERROR;
203
204
py_command_info = py_str_array_to_tuple(command_info);
205
if (PyErr_Occurred())
206
goto cleanup;
207
208
PyObject *py_args = Py_BuildValue("(zizO)", plugin_name, plugin_type, audit_msg, py_command_info);
209
rc = python_plugin_api_rc_call(plugin_ctx, CALLBACK_PYNAME(error), py_args);
210
CALLBACK_SET_ERROR(plugin_ctx, errstr);
211
212
cleanup:
213
Py_CLEAR(py_command_info);
214
215
debug_return_int(rc);
216
}
217
218
static int
219
python_plugin_audit_show_version(struct AuditPluginContext *audit_ctx, int verbose)
220
{
221
debug_decl(python_plugin_audit_show_version, PYTHON_DEBUG_CALLBACKS);
222
223
struct PluginContext *plugin_ctx = BASE_CTX(audit_ctx);
224
PyThreadState_Swap(plugin_ctx->py_interpreter);
225
226
debug_return_int(python_plugin_show_version(plugin_ctx,
227
CALLBACK_PYNAME(show_version), verbose, PY_AUDIT_PLUGIN_VERSION, "audit"));
228
}
229
230
sudo_dso_public struct audit_plugin python_audit;
231
232
// generate symbols for loading multiple audit plugins:
233
#define AUDIT_SYMBOL_NAME(symbol) symbol
234
#include "python_plugin_audit_multi.inc"
235
#define AUDIT_SYMBOL_NAME(symbol) symbol##1
236
#include "python_plugin_audit_multi.inc"
237
#define AUDIT_SYMBOL_NAME(symbol) symbol##2
238
#include "python_plugin_audit_multi.inc"
239
#define AUDIT_SYMBOL_NAME(symbol) symbol##3
240
#include "python_plugin_audit_multi.inc"
241
#define AUDIT_SYMBOL_NAME(symbol) symbol##4
242
#include "python_plugin_audit_multi.inc"
243
#define AUDIT_SYMBOL_NAME(symbol) symbol##5
244
#include "python_plugin_audit_multi.inc"
245
#define AUDIT_SYMBOL_NAME(symbol) symbol##6
246
#include "python_plugin_audit_multi.inc"
247
#define AUDIT_SYMBOL_NAME(symbol) symbol##7
248
#include "python_plugin_audit_multi.inc"
249
250
static struct audit_plugin *extra_audit_plugins[] = {
251
&python_audit1,
252
&python_audit2,
253
&python_audit3,
254
&python_audit4,
255
&python_audit5,
256
&python_audit6,
257
&python_audit7
258
};
259
260
struct audit_plugin *
261
python_audit_clone(void)
262
{
263
static size_t counter = 0;
264
struct audit_plugin *next_plugin = NULL;
265
266
size_t max = sizeof(extra_audit_plugins) / sizeof(*extra_audit_plugins);
267
if (counter < max) {
268
next_plugin = extra_audit_plugins[counter];
269
++counter;
270
} else if (counter == max) {
271
++counter;
272
py_sudo_log(SUDO_CONV_ERROR_MSG, "sudo: loading more than %d sudo python audit plugins is not supported\n", counter);
273
}
274
275
return next_plugin;
276
}
277
278