This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
plugin-python.c
468 lines (361 loc) · 11.2 KB
/
plugin-python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _PYTHON_PLUGIN
/*******************************************************************************
*
* Python plugin implementation
*
* tested on
* - CPython2.6
* - CPython2.7
* - CPython3.3
*
*/
#include <Python.h>
#include <string.h>
#include <pthread.h>
#include "core.h"
#include "main.h"
#include "plugin.h"
#include "utils.h"
#include "plugin-python.h"
#if _PYTHON_MAJOR_ == 3
#define PYTHON_FROMSTRING PyUnicode_FromString
#define PYTHON_VALUE_FORMAT "iy#y"
# else /* _PYTHON2_ */
#define PYTHON_FROMSTRING PyString_FromString
#define PYTHON_VALUE_FORMAT "is#s"
#endif
#define xlog_python(t, ...) xlog(t, "["_PYTHON_VERSION_"] " __VA_ARGS__)
/**
*
*/
static int proxenet_python_append_path()
{
PyObject *pPath, *pAddPath;
int retcode = 0;
pPath = PySys_GetObject("path");
if (!pPath) {
xlog_python(LOG_ERROR, "%s\n", "Failed to find `sys.path'");
return -1;
}
if (!PyList_Check(pPath)) {
return -1;
}
pAddPath = PYTHON_FROMSTRING(cfg->plugins_path);
if (!pAddPath) {
return -1;
}
if (PyList_Insert(pPath, 0, pAddPath) < 0) {
retcode = -1;
}
Py_DECREF(pAddPath);
return retcode;
}
/**
*
*/
int proxenet_python_initialize_vm(plugin_t* plugin)
{
interpreter_t *interpreter = plugin->interpreter;
PyThreadState* oldctx;
int retcode;
/* is vm initialized ? */
if (interpreter->ready && plugin->internal )
return 0;
if (!interpreter->ready){
#ifdef DEBUG
xlog_python(LOG_DEBUG, "Initializing VM version %s\n", _PYTHON_VERSION_);
#endif
Py_Initialize();
PyEval_InitThreads();
}
if (Py_IsInitialized()==false || PyEval_ThreadsInitialized()==false) {
xlog_python(LOG_CRITICAL, "%s\n", "An error occured during initialization");
plugin->internal = NULL;
interpreter->vm = NULL;
interpreter->ready = false;
return -1;
}
#ifdef DEBUG
xlog_python(LOG_DEBUG, "Initializing sub-interpreter for '%s'\n", plugin->name);
#endif
oldctx = PyThreadState_Get();
if (!oldctx){
xlog_python(LOG_CRITICAL, "%s\n", "Failed to get current PythonThread context.");
plugin->internal = NULL;
interpreter->vm = NULL;
interpreter->ready = false;
return -1;
}
plugin->internal = Py_NewInterpreter();
if(!plugin->internal){
xlog_python(LOG_CRITICAL, "%s\n", "Failed to create sub-interpreter.");
plugin->internal = NULL;
interpreter->vm = NULL;
interpreter->ready = false;
return -1;
}
PyThreadState_Swap( plugin->internal );
if (proxenet_python_append_path() < 0) {
xlog_python(LOG_CRITICAL, "%s\n", "Failed to append plugins directory to sys.path");
Py_Finalize();
interpreter->vm = NULL;
interpreter->ready = false;
retcode = -1;
} else {
interpreter->ready = true;
retcode = 0;
}
PyThreadState_Swap( oldctx );
return retcode;
}
/**
*
*/
int proxenet_python_destroy_plugin(plugin_t* plugin)
{
PyThreadState* oldctx;
PyObject *pResult;
proxenet_plugin_set_state(plugin, INACTIVE);
if (plugin->onleave_function){
pResult = PyObject_CallObject(plugin->onleave_function, NULL);
if (!pResult || PyErr_Occurred()){
xlog_python(LOG_WARNING, "%s\n", "PyObject_CallObject() failed.");
PyErr_Print();
}
}
Py_DECREF(plugin->pre_function);
Py_DECREF(plugin->post_function);
oldctx = PyThreadState_Get();
PyThreadState_Swap( plugin->internal );
Py_EndInterpreter( plugin->internal );
PyThreadState_Swap( oldctx );
return 0;
}
/**
*
*/
int proxenet_python_destroy_vm(interpreter_t* interpreter)
{
if (!Py_IsInitialized()) {
xlog_python(LOG_CRITICAL, "%s\n", "Python VM should not be uninitialized here");
return -1;
}
Py_Finalize();
if (!Py_IsInitialized()){
interpreter->ready = false;
} else {
return -1;
}
return 0;
}
/**
*
*/
static int proxenet_python_initialize_function(plugin_t* plugin, char* function_name, PyObject** pFunc)
{
char* module_name;
PyObject *pModStr, *pMod;
module_name = plugin->name;
pModStr = PYTHON_FROMSTRING(module_name);
if (!pModStr) {
PyErr_Print();
return -1;
}
pMod = PyImport_Import(pModStr);
if(!pMod) {
xlog_python(LOG_ERROR, "Failed to import '%s'\n", module_name);
Py_DECREF(pModStr);
return -1;
}
Py_DECREF(pModStr);
#ifdef DEBUG
xlog_python(LOG_DEBUG, "Importing '%s.%s'\n", module_name, function_name);
#endif
/* find reference to function in module */
*pFunc = PyObject_GetAttrString(pMod, function_name);
if (!*pFunc) {
PyErr_Print();
return -1;
}
if (!PyCallable_Check(*pFunc)) {
xlog_python(LOG_ERROR, "Object in %s is not callable\n", module_name);
return -1;
}
return 0;
}
/**
*
*/
static int proxenet_python_init_request_function(plugin_t* plugin)
{
PyObject* pFunc;
if (plugin->pre_function)
return 0;
if (proxenet_python_initialize_function(plugin, CFG_REQUEST_PLUGIN_FUNCTION, &pFunc) < 0)
return -1;
plugin->pre_function = pFunc;
return 0;
}
/**
*
*/
static int proxenet_python_init_response_function(plugin_t* plugin)
{
PyObject* pFunc;
if (plugin->post_function)
return 0;
if (proxenet_python_initialize_function(plugin, CFG_RESPONSE_PLUGIN_FUNCTION, &pFunc) < 0)
return -1;
plugin->post_function = pFunc;
return 0;
}
/**
*
*/
static int proxenet_python_init_onload_function(plugin_t* plugin)
{
PyObject *pFunc, *pResult;
if (proxenet_python_initialize_function(plugin, CFG_ONLOAD_PLUGIN_FUNCTION, &pFunc) < 0){
xlog_python(LOG_WARNING, "Failed to load '%s()' function\n", CFG_ONLOAD_PLUGIN_FUNCTION);
return -1;
}
plugin->onload_function = pFunc;
pResult = PyObject_CallObject(plugin->onload_function, NULL);
if (!pResult || PyErr_Occurred()){
xlog_python(LOG_ERROR, "%s\n", "PyObject_CallObject() failed.");
PyErr_Print();
return -1;
}
return 0;
}
/**
*
*/
static int proxenet_python_init_onleave_function(plugin_t* plugin)
{
PyObject *pFunc;
if (proxenet_python_initialize_function(plugin, CFG_ONLEAVE_PLUGIN_FUNCTION, &pFunc) < 0){
xlog_python(LOG_WARNING, "Failed to load '%s()' function\n", CFG_ONLEAVE_PLUGIN_FUNCTION);
return -1;
}
plugin->onleave_function = pFunc;
return 0;
}
/**
*
*/
int proxenet_python_load_file(plugin_t* plugin)
{
PyThreadState* oldctx;
int retcode = 0;
oldctx = PyThreadState_Get();
if (!oldctx){
xlog_python(LOG_CRITICAL, "%s\n", "Failed to get current PythonThread context.");
return -1;
}
PyThreadState_Swap( plugin->internal );
proxenet_python_init_onload_function(plugin);
proxenet_python_init_onleave_function(plugin);
if (proxenet_python_init_request_function(plugin) < 0){
xlog_python(LOG_ERROR, "Failed to initialize '%s.%s'\n",
plugin->name, CFG_REQUEST_PLUGIN_FUNCTION);
retcode = -1;
}
if (proxenet_python_init_response_function(plugin) < 0) {
xlog_python(LOG_ERROR, "Failed to initialize '%s.%s'\n",
plugin->name, CFG_RESPONSE_PLUGIN_FUNCTION);
retcode = -1;
}
PyThreadState_Swap( oldctx );
return retcode;
}
/**
*
*/
static char* proxenet_python_execute_function(PyObject* pFuncRef, request_t *request)
{
PyObject *pArgs, *pResult;
char *buffer, *result;
int ret;
Py_ssize_t len;
char *uri = request->http_infos.uri;
buffer = NULL;
len = -1;
pArgs = Py_BuildValue(PYTHON_VALUE_FORMAT, request->id, request->data, request->size, uri);
if (!pArgs) {
xlog_python(LOG_ERROR, "%s\n", "Py_BuildValue() failed.");
PyErr_Print();
return NULL;
}
pResult = PyObject_CallObject(pFuncRef, pArgs);
if (!pResult || PyErr_Occurred()){
xlog_python(LOG_ERROR, "%s\n", "PyObject_CallObject() failed.");
PyErr_Print();
return NULL;
}
if (PyBytes_Check(pResult)) {
ret = PyBytes_AsStringAndSize(pResult, &buffer, &len);
if (ret<0) {
PyErr_Print();
result = NULL;
} else {
result = proxenet_xstrdup(buffer, len);
request->size = len;
request->data = result;
}
} else {
#if _PYTHON_MAJOR_ == 3
xlog_python(LOG_ERROR, "Incorrect return type (expected: %s)\n", "ByteArray");
#else
xlog_python(LOG_ERROR, "Incorrect return type (expected: %s)\n", "String");
#endif
result = NULL;
}
Py_DECREF(pResult);
Py_DECREF(pArgs);
return result;
}
/**
*
*/
static void proxenet_python_lock_vm(interpreter_t *interpreter)
{
pthread_mutex_lock(&interpreter->mutex);
}
/**
*
*/
static void proxenet_python_unlock_vm(interpreter_t *interpreter)
{
pthread_mutex_unlock(&interpreter->mutex);
}
/**
*
*/
char* proxenet_python_plugin(plugin_t* plugin, request_t* request)
{
char *buf = NULL;
PyObject *pFunc = NULL;
bool is_request = (request->type==REQUEST) ? true : false;
interpreter_t *interpreter = plugin->interpreter;
PyThreadState* oldctx;
proxenet_python_lock_vm(interpreter);
oldctx = PyThreadState_Get();
PyThreadState_Swap( plugin->internal );
if (is_request)
pFunc = (PyObject*) plugin->pre_function;
else
pFunc = (PyObject*) plugin->post_function;
buf = proxenet_python_execute_function(pFunc, request);
if (!buf) {
xlog_python(LOG_ERROR, "%s: Error while executing plugin on %s\n",
plugin->name, is_request ? "request" : "response");
}
PyThreadState_Swap( oldctx );
proxenet_python_unlock_vm(interpreter);
return buf;
}
#endif /* _PYTHON_PLUGIN */