Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/python/python_baseplugin.c
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
#include "sudo_python_module.h"
20
21
PyTypeObject *sudo_type_Plugin = NULL;
22
23
static PyObject *
24
_sudo_Plugin__Init(PyObject *py_self, PyObject *py_args, PyObject *py_kwargs)
25
{
26
debug_decl(_sudo_Plugin__Init, PYTHON_DEBUG_C_CALLS);
27
28
py_debug_python_call("Plugin", "__init__", py_args, NULL, PYTHON_DEBUG_C_CALLS);
29
30
if (!PyArg_UnpackTuple(py_args, "sudo.Plugin.__init__", 1, 1, &py_self))
31
goto cleanup;
32
33
Py_ssize_t pos = 0;
34
PyObject *py_key = NULL, *py_value = NULL; // -> borrowed references
35
36
while (PyDict_Next(py_kwargs, &pos, &py_key, &py_value)) {
37
if (PyObject_SetAttr(py_self, py_key, py_value) != 0)
38
goto cleanup;
39
}
40
41
cleanup:
42
if (PyErr_Occurred())
43
debug_return_ptr(NULL);
44
45
debug_return_ptr_pynone;
46
}
47
48
49
static PyMethodDef _sudo_Plugin_class_methods[] = {
50
{"__init__", (PyCFunction)_sudo_Plugin__Init,
51
METH_VARARGS | METH_KEYWORDS,
52
"Base sudo plugin constructor"},
53
{NULL, NULL, 0, NULL}
54
};
55
56
57
int
58
sudo_module_register_baseplugin(PyObject *py_module)
59
{
60
debug_decl(sudo_module_register_baseplugin, PYTHON_DEBUG_INTERNAL);
61
int rc = SUDO_RC_ERROR;
62
PyObject *py_class = NULL;
63
64
py_class = sudo_module_create_class("sudo.Plugin", _sudo_Plugin_class_methods, NULL);
65
if (py_class == NULL)
66
goto cleanup;
67
68
if (PyModule_AddObject(py_module, "Plugin", py_class) < 0) {
69
goto cleanup;
70
}
71
72
// PyModule_AddObject steals a reference to py_class on success
73
Py_INCREF(py_class);
74
rc = SUDO_RC_OK;
75
76
Py_CLEAR(sudo_type_Plugin);
77
sudo_type_Plugin = (PyTypeObject *)py_class;
78
Py_INCREF(sudo_type_Plugin);
79
80
cleanup:
81
Py_CLEAR(py_class);
82
debug_return_int(rc);
83
}
84
85