forked from mbachry/mosquitto_pyauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_plugin_pyauth.c
289 lines (234 loc) · 8.32 KB
/
auth_plugin_pyauth.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <Python.h>
#include <mosquitto.h>
#include <mosquitto_plugin.h>
#if !defined(LIBMOSQUITTO_VERSION_NUMBER) || LIBMOSQUITTO_VERSION_NUMBER < 1002001
#error "mosquitto 1.2.1 or higher is required"
#endif
struct pyauth_data {
char *module_name;
PyObject *module;
PyObject *plugin_cleanup_func;
PyObject *unpwd_check_func;
PyObject *acl_check_func;
PyObject *security_init_func;
PyObject *security_cleanup_func;
PyObject *psk_key_get_func;
};
#define unused __attribute__((unused))
#ifdef PYAUTH_DEBUG
__attribute__((format(printf, 1, 2)))
static void debug(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
#else
static void debug(const char *fmt unused, ...)
{
}
#endif
__attribute__((format(printf, 2, 3)))
static void die(bool print_exception, const char *fmt, ...)
{
if (print_exception)
PyErr_Print();
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}
/* Aux "mosquitto_auth" module */
static PyObject *pyauth_mosquitto_topic_matches_sub(PyObject *self unused, PyObject *args)
{
const char *sub;
const char *topic;
if (!PyArg_ParseTuple(args, "ss", &sub, &topic))
return NULL;
bool res;
mosquitto_topic_matches_sub(sub, topic, &res);
return PyBool_FromLong(res);
}
static PyMethodDef methods[] = {
{"topic_matches_sub", pyauth_mosquitto_topic_matches_sub, METH_VARARGS,
"Check whether a topic matches a subscription"},
{NULL, NULL, 0, NULL}
};
static void init_aux_module(void)
{
PyObject *module = Py_InitModule("mosquitto_auth", methods);
PyModule_AddIntConstant(module, "MOSQ_ACL_NONE", MOSQ_ACL_NONE);
PyModule_AddIntConstant(module, "MOSQ_ACL_READ", MOSQ_ACL_READ);
PyModule_AddIntConstant(module, "MOSQ_ACL_WRITE", MOSQ_ACL_WRITE);
}
/* Plugin entry points */
int mosquitto_auth_plugin_version(void)
{
return MOSQ_AUTH_PLUGIN_VERSION;
}
static PyObject *make_auth_opts_tuple(struct mosquitto_auth_opt *auth_opts, int auth_opt_count)
{
PyObject *optlist = PyTuple_New(auth_opt_count - 1); /* -1 because of skipped "pyauth_module" */
if (optlist == NULL)
return NULL;
int idx = 0;
for (int i = 0; i < auth_opt_count; i++) {
if (!strcmp(auth_opts[i].key, "pyauth_module"))
continue;
PyObject *elt = PyTuple_Pack(2,
PyString_FromString(auth_opts[i].key),
PyString_FromString(auth_opts[i].value));
if (elt == NULL) {
Py_DECREF(optlist);
return NULL;
}
PyTuple_SET_ITEM(optlist, idx++, elt);
}
return optlist;
}
int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count)
{
struct pyauth_data *data = calloc(1, sizeof(*data));
assert(data != NULL);
for (int i = 0; i < auth_opt_count; i++) {
if (!strcmp(auth_opts[i].key, "pyauth_module")) {
data->module_name = strdup(auth_opts[i].value);
debug("pyauth_module = %s", data->module_name);
}
}
if (data->module_name == NULL)
die(false, "pyauth_module config param missing");
Py_Initialize();
init_aux_module();
data->module = PyImport_ImportModule(data->module_name);
if (data->module == NULL)
die(true, "failed to import module: %s", data->module_name);
data->plugin_cleanup_func = PyObject_GetAttrString(data->module, "plugin_cleanup");
data->unpwd_check_func = PyObject_GetAttrString(data->module, "unpwd_check");
data->acl_check_func = PyObject_GetAttrString(data->module, "acl_check");
data->security_init_func = PyObject_GetAttrString(data->module, "security_init");
data->security_cleanup_func = PyObject_GetAttrString(data->module, "security_cleanup");
data->psk_key_get_func = PyObject_GetAttrString(data->module, "psk_key_get");
PyErr_Clear(); /* don't care about AttributeError from above code */
PyObject *init_func = PyObject_GetAttrString(data->module, "plugin_init");
if (init_func != NULL) {
PyObject *optlist = make_auth_opts_tuple(auth_opts, auth_opt_count);
if (optlist == NULL)
die(true, "python module initialization failed");
PyObject *res = PyObject_CallFunctionObjArgs(init_func, optlist, NULL);
if (res == NULL)
die(true, "python module initialization failed");
Py_DECREF(res);
Py_DECREF(optlist);
Py_DECREF(init_func);
}
PyErr_Clear();
*user_data = data;
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts unused, int auth_opt_count unused)
{
struct pyauth_data *data = user_data;
if (data->plugin_cleanup_func != NULL) {
PyObject *res = PyObject_CallFunction(data->plugin_cleanup_func, NULL);
if (res == NULL) {
fprintf(stderr, "pyauth plugin_cleanup failed\n");
PyErr_Print();
}
Py_XDECREF(res);
}
Py_DECREF(data->module);
Py_XDECREF(data->plugin_cleanup_func);
Py_XDECREF(data->unpwd_check_func);
Py_XDECREF(data->acl_check_func);
Py_XDECREF(data->security_init_func);
Py_XDECREF(data->security_cleanup_func);
Py_XDECREF(data->psk_key_get_func);
free(data->module_name);
free(data);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_security_init(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count, bool reload)
{
struct pyauth_data *data = user_data;
if (data->security_init_func == NULL)
return MOSQ_ERR_SUCCESS;
PyObject *optlist = make_auth_opts_tuple(auth_opts, auth_opt_count);
if (optlist == NULL)
goto err_no_optlist;
PyObject *py_reload = PyBool_FromLong(reload);
PyObject *res = PyObject_CallFunctionObjArgs(data->security_init_func, optlist, py_reload, NULL);
if (res == NULL)
goto err_call_failed;
Py_DECREF(res);
Py_DECREF(py_reload);
Py_DECREF(optlist);
return MOSQ_ERR_SUCCESS;
err_call_failed:
Py_XDECREF(py_reload);
Py_XDECREF(optlist);
err_no_optlist:
fprintf(stderr, "pyauth security_init failed\n");
PyErr_Print();
return MOSQ_ERR_UNKNOWN;
}
int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts unused, int auth_opt_count unused, bool reload)
{
struct pyauth_data *data = user_data;
if (data->security_cleanup_func == NULL)
return MOSQ_ERR_SUCCESS;
PyObject *py_reload = PyBool_FromLong(reload);
PyObject *res = PyObject_CallFunctionObjArgs(data->security_cleanup_func, py_reload, NULL);
Py_DECREF(py_reload);
if (res == NULL) {
fprintf(stderr, "pyauth security_cleanup failed\n");
PyErr_Print();
return MOSQ_ERR_UNKNOWN;
}
Py_DECREF(res);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_auth_acl_check(void *user_data, const char *clientid, const char *username, const char *topic, int access)
{
struct pyauth_data *data = user_data;
if (data->acl_check_func == NULL)
return MOSQ_ERR_ACL_DENIED;
PyObject *res = PyObject_CallFunction(data->acl_check_func, "sssi", clientid, username, topic, access);
if (res == NULL) {
PyErr_Print();
return MOSQ_ERR_UNKNOWN;
}
int ok = PyObject_IsTrue(res);
Py_DECREF(res);
return ok ? MOSQ_ERR_SUCCESS : MOSQ_ERR_ACL_DENIED;
}
int mosquitto_auth_unpwd_check(void *user_data, const char *username, const char *password)
{
struct pyauth_data *data = user_data;
if (username == NULL || password == NULL)
return MOSQ_ERR_AUTH;
debug("mosquitto_auth_unpwd_check: username=%s, password=%s", username, password);
if (data->unpwd_check_func == NULL)
return MOSQ_ERR_AUTH;
PyObject *res = PyObject_CallFunction(data->unpwd_check_func, "ss", username, password);
if (res == NULL) {
PyErr_Print();
return MOSQ_ERR_UNKNOWN;
}
int ok = PyObject_IsTrue(res);
Py_DECREF(res);
return ok ? MOSQ_ERR_SUCCESS : MOSQ_ERR_AUTH;
}
int mosquitto_auth_psk_key_get(void *user_data unused, const char *hint unused, const char *identity unused, char *key unused, int max_key_len unused)
{
return MOSQ_ERR_AUTH;
}