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
|
#include "Python.h"
#include "openssl/crypto.h"
#include "stdio.h"
static PyObject *SpamError;
static PyObject *IdCallback;
static PyObject *LockingCallback;
//--------------------------
static void locking_function(int mode, int n, const char * file, int line)
{
PyObject *arglist;
PyObject *result;
printf("Enter locking_function\n");
arglist = Py_BuildValue("(i, i, s, i)", mode, n, file, line);
result = PyObject_CallObject(LockingCallback, arglist);
// if(mode & CRYPTO_LOCK)
//
// result = PyObject_CallObject(IdCallback, arglist);
// else
// a--;
Py_DECREF(arglist);
Py_DECREF(result);
printf("Leave locking_function\n");
}
static unsigned long id_function(void)
{
PyObject *arglist;
PyObject *result;
int value;
arglist = Py_BuildValue(NULL);
result = PyObject_CallObject(IdCallback, arglist);
if (!PyArg_ParseTuple(result, "i", &value))
return 0;
Py_DECREF(arglist);
Py_DECREF(result);
return ((unsigned long)value);
}
//--------------------------
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
if (!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command);
if (sts < 0) {
PyErr_SetString(SpamError, "System command failed");
return NULL;
}
return PyLong_FromLong(sts);
}
static PyObject * enable_mutexes(PyObject *self, PyObject *args) {
PyObject *pIdCallback, *pLockingCallback;
if (!PyArg_UnpackTuple(args, "enable_mutexes", 2, 2, &pIdCallback, &pLockingCallback)) {
return NULL;
}
IdCallback = pIdCallback;
LockingCallback = pLockingCallback;
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
printf("Enabled mutexes\n");
Py_RETURN_NONE;
}
static PyMethodDef SpamMethods[] = {
{"system", spam_system, METH_VARARGS,
"Execute a shell command."},
{"enable_mutexes", enable_mutexes, METH_VARARGS,
"Enable mutexes for openssl"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initfoobar(void)
{
(void) Py_InitModule("foobar", SpamMethods);
}
|