Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/python/python_convmessage.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_ConvMessage;
22
23
static PyObject *
24
_sudo_ConvMessage__Init(PyObject *py_self, PyObject *py_args, PyObject *py_kwargs)
25
{
26
debug_decl(_sudo_ConvMessage__Init, PYTHON_DEBUG_C_CALLS);
27
28
py_debug_python_call("ConvMessage", "__init__", py_args, py_kwargs, PYTHON_DEBUG_C_CALLS);
29
30
PyObject *py_empty = PyTuple_New(0);
31
32
struct sudo_conv_message conv_message = { 0, 0, NULL };
33
34
static const char *keywords[] = { "self", "msg_type", "msg", "timeout", NULL };
35
if (!PyArg_ParseTupleAndKeywords(py_args ? py_args : py_empty, py_kwargs, "Ois|i:sudo.ConvMessage", (char **)keywords,
36
&py_self, &(conv_message.msg_type), &(conv_message.msg),
37
&(conv_message.timeout)))
38
goto cleanup;
39
40
sudo_debug_printf(SUDO_DEBUG_TRACE, "Parsed arguments: self='%p' msg_type='%d' timeout='%d' msg='%s'",
41
(void *)py_self, conv_message.msg_type, conv_message.timeout, conv_message.msg);
42
43
py_object_set_attr_number(py_self, "msg_type", conv_message.msg_type);
44
if (PyErr_Occurred())
45
goto cleanup;
46
47
py_object_set_attr_number(py_self, "timeout", conv_message.timeout);
48
if (PyErr_Occurred()) // -V547
49
goto cleanup;
50
51
py_object_set_attr_string(py_self, "msg", conv_message.msg);
52
if (PyErr_Occurred()) // -V547
53
goto cleanup;
54
55
cleanup:
56
Py_CLEAR(py_empty);
57
58
if (PyErr_Occurred())
59
debug_return_ptr(NULL);
60
61
debug_return_ptr_pynone;
62
}
63
64
65
static PyMethodDef _sudo_ConvMessage_class_methods[] =
66
{
67
{"__init__", (PyCFunction)_sudo_ConvMessage__Init,
68
METH_VARARGS | METH_KEYWORDS,
69
"Conversation message (same as C type sudo_conv_message)"},
70
{NULL, NULL, 0, NULL}
71
};
72
73
74
int
75
sudo_module_register_conv_message(PyObject *py_module)
76
{
77
debug_decl(sudo_module_register_conv_message, PYTHON_DEBUG_INTERNAL);
78
int rc = SUDO_RC_ERROR;
79
PyObject *py_class = NULL;
80
81
py_class = sudo_module_create_class("sudo.ConvMessage", _sudo_ConvMessage_class_methods, NULL);
82
if (py_class == NULL)
83
goto cleanup;
84
85
if (PyModule_AddObject(py_module, "ConvMessage", py_class) < 0) {
86
goto cleanup;
87
}
88
89
// PyModule_AddObject steals the reference to py_class on success
90
Py_INCREF(py_class);
91
rc = SUDO_RC_OK;
92
93
Py_CLEAR(sudo_type_ConvMessage);
94
sudo_type_ConvMessage = (PyTypeObject *)py_class;
95
Py_INCREF(sudo_type_ConvMessage);
96
97
cleanup:
98
Py_CLEAR(py_class);
99
debug_return_int(rc);
100
}
101
102
int
103
sudo_module_ConvMessage_to_c(PyObject *py_conv_message, struct sudo_conv_message *conv_message)
104
{
105
debug_decl(sudo_module_ConvMessage_to_c, PYTHON_DEBUG_C_CALLS);
106
107
conv_message->msg_type = (int)py_object_get_optional_attr_number(py_conv_message, "msg_type");
108
if (PyErr_Occurred())
109
debug_return_int(SUDO_RC_ERROR);
110
111
conv_message->timeout = (int)py_object_get_optional_attr_number(py_conv_message, "timeout");
112
if (PyErr_Occurred()) // -V547
113
debug_return_int(SUDO_RC_ERROR);
114
115
conv_message->msg = py_object_get_optional_attr_string(py_conv_message, "msg");
116
if (PyErr_Occurred()) // -V547
117
debug_return_int(SUDO_RC_ERROR);
118
119
debug_return_int(SUDO_RC_OK);
120
}
121
122
int
123
sudo_module_ConvMessages_to_c(PyObject *py_tuple, Py_ssize_t *num_msgs, struct sudo_conv_message **msgs)
124
{
125
debug_decl(sudo_module_ConvMessages_to_c, PYTHON_DEBUG_C_CALLS);
126
127
*num_msgs = PyTuple_Size(py_tuple);
128
*msgs = NULL;
129
130
if (*num_msgs <= 0) {
131
*num_msgs = 0;
132
PyErr_Format(sudo_exc_SudoException, "Expected at least one ConvMessage");
133
debug_return_int(SUDO_RC_ERROR);
134
}
135
136
*msgs = calloc((size_t)*num_msgs, sizeof(struct sudo_conv_message));
137
if (*msgs == NULL) {
138
debug_return_int(SUDO_RC_ERROR);
139
}
140
141
for (Py_ssize_t i = 0; i < *num_msgs; ++i) {
142
PyObject *py_msg = py_tuple_get(py_tuple, i, sudo_type_ConvMessage);
143
if (py_msg == NULL || sudo_module_ConvMessage_to_c(py_msg, &(*msgs)[i]) < 0) {
144
debug_return_int(SUDO_RC_ERROR);
145
}
146
}
147
148
debug_return_int(SUDO_RC_OK);
149
}
150
151
152