forked from mbachry/mosquitto_pyauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosquitto_pyplugin_impl.c
388 lines (333 loc) · 12.2 KB
/
mosquitto_pyplugin_impl.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <libgen.h>
#include <assert.h>
#include <Python.h>
#include <mosquitto.h>
#include <mosquitto_broker.h>
#include <mosquitto_plugin.h>
#include <mqtt_protocol.h>
#include <openssl/x509.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <dlfcn.h>
struct pyplugin_data {
mosquitto_plugin_id_t *identifier;
void *user_data;
};
#define _UNUSED_ATR __attribute__((unused))
__attribute__((format(printf, 1, 2)))
static void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}
static void _mosq_log(int loglevel, char* message)
{
mosquitto_log_printf(loglevel, "%s", message);
}
static const char *_mosq_client_address(const struct mosquitto *client)
{
return mosquitto_client_address(client);
}
static const char *_mosq_client_id(const struct mosquitto *client)
{
return mosquitto_client_id(client);
}
static char* _mosq_client_certificate(const struct mosquitto *client)
{
X509* cert = mosquitto_client_certificate(client);
if (NULL == cert)
return NULL;
BIO *mem = BIO_new(BIO_s_mem());
PEM_write_bio_X509(mem, cert);
BUF_MEM *bptr;
BIO_get_mem_ptr(mem, &bptr);
char* result = calloc(bptr->length + 1, sizeof(char));
if (NULL == result)
return NULL;
BIO_read(mem, result, bptr->length);
X509_free(cert);
BIO_free(mem);
return result;
}
static int _mosq_client_protocol(const struct mosquitto *client)
{
return mosquitto_client_protocol(client);
}
static int _mosq_client_protocol_version(const struct mosquitto *client)
{
return mosquitto_client_protocol_version(client);
}
static const char *_mosq_client_username(const struct mosquitto *client)
{
return mosquitto_client_username(client);
}
static int _mosq_set_username(struct mosquitto *client, const char *username)
{
return mosquitto_set_username(client, username);
}
static int _mosq_kick_client_by_clientid(const char *client_id, bool with_will)
{
return mosquitto_kick_client_by_clientid(client_id, with_will);
}
static int _mosq_kick_client_by_username(const char *client_username, bool with_will)
{
return mosquitto_kick_client_by_username(client_username, with_will);
}
static bool _mosq_topic_matches_sub(char* sub, char* topic)
{
bool res = false;
mosquitto_topic_matches_sub(sub, topic, &res);
return res;
}
static char *_mosq_strdup(const char* s)
{
return mosquitto_strdup(s);
}
static void* _mosq_copy(void* src, size_t size)
{
void *dest = mosquitto_malloc(size);
if (NULL != dest)
{
void *ret = memcpy(dest, src, size);
if (NULL == ret)
mosquitto_free(dest);
return ret;
}
return NULL;
}
/* event callback methods */
static int _py_basic_auth(void* user_data,
const struct mosquitto* client,
const char* username,
const char* password);
static int handle_basic_auth(int event _UNUSED_ATR, void *event_data, void *user_data)
{
struct pyplugin_data *data = user_data;
struct mosquitto_evt_basic_auth *basic_auth_event = event_data;
return _py_basic_auth(data->user_data,
basic_auth_event->client,
basic_auth_event->username,
basic_auth_event->password);
}
static int _py_acl_check(void* user_data,
const struct mosquitto* client,
const char *topic,
int access,
const void* payload,
uint32_t payloadlen);
static int handle_acl_check(int event _UNUSED_ATR, void *event_data, void *user_data)
{
struct pyplugin_data *data = user_data;
struct mosquitto_evt_acl_check *acl_check_event = event_data;
return _py_acl_check(data->user_data,
acl_check_event->client,
acl_check_event->topic,
acl_check_event->access,
acl_check_event->payload,
acl_check_event->payloadlen);
}
static int _py_psk_key(void* user_data,
const struct mosquitto* client,
const char *hint,
const char *identity,
char *key,
int max_key_len);
static int handle_psk_key(int event _UNUSED_ATR, void *event_data, void *user_data)
{
struct pyplugin_data *data = user_data;
struct mosquitto_evt_psk_key *psk_key_event = event_data;
return _py_psk_key(data->user_data,
psk_key_event->client,
psk_key_event->hint,
psk_key_event->identity,
psk_key_event->key,
psk_key_event->max_key_len);
}
static int _py_disconnect(void* user_data,
const struct mosquitto* client,
int reason);
static int handle_disconnect(int event _UNUSED_ATR, void *event_data, void *user_data)
{
struct pyplugin_data *data = user_data;
struct mosquitto_evt_disconnect *disconnect_event = event_data;
return _py_disconnect(data->user_data,
disconnect_event->client,
disconnect_event->reason);
}
static int _py_message(void* user_data,
const struct mosquitto* client,
struct mosquitto_evt_message* event_message);
static int handle_message(int event _UNUSED_ATR, void *event_data, void *user_data)
{
struct pyplugin_data *data = user_data;
struct mosquitto_evt_message *event_message = event_data;
return _py_message(data->user_data,
event_message->client,
event_message);
}
static int _py_reload(void* user_data);
static int handle_reload(int event _UNUSED_ATR, void *event_data _UNUSED_ATR, void *user_data)
{
struct pyplugin_data *data = user_data;
return _py_reload(data->user_data);
}
static void _py_tick(void* user_data);
static int handle_tick(int event _UNUSED_ATR, void *event_data _UNUSED_ATR, void *user_data)
{
struct pyplugin_data *data = user_data;
_py_tick(data->user_data);
return MOSQ_ERR_SUCCESS;
}
/* Plugin entry points */
CFFI_DLLEXPORT int mosquitto_plugin_version(int supported_version_count,
const int *supported_versions)
{
for (int i=0; i < supported_version_count; ++i) {
if (supported_versions[i] == MOSQ_PLUGIN_VERSION)
return MOSQ_PLUGIN_VERSION;
}
return -1;
}
static void* _py_plugin_init(struct mosquitto_opt *options, int option_count);
CFFI_DLLEXPORT int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier,
void ** userdata,
struct mosquitto_opt *options,
int option_count)
{
static bool started = false;
struct pyplugin_data *data = calloc(1, sizeof(*data));
assert(NULL != data);
data->identifier = identifier;
if (!Py_IsInitialized()) {
PyConfig config;
PyConfig_InitIsolatedConfig(&config);
config.buffered_stdio = 0;
#ifdef PYHOME
{
wchar_t *pyhome = Py_DecodeLocale(PYHOME, NULL);
if (NULL != pyhome) {
PyStatus status = PyConfig_SetString(&config, &config.home,
pyhome);
PyMem_RawFree(pyhome);
#else
Dl_info info;
if (0 != dladdr(mosquitto_plugin_init, &info) &&
info.dli_fname && *info.dli_fname) {
wchar_t *program = Py_DecodeLocale(info.dli_fname, NULL);
if (NULL != program) {
PyStatus status = PyConfig_SetString(&config, &config.program_name,
program);
PyMem_RawFree(program);
#endif
if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
Py_ExitStatusException(status);
}
}
}
PyStatus status = Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
if (PyStatus_Exception(status)) {
Py_ExitStatusException(status);
}
}
if (!started) {
if (cffi_start_python())
die("failed to start python");
started = true;
}
void *user_data = _py_plugin_init(options, option_count);
if (NULL == user_data) {
die("could not init python plugin");
}
data->user_data = user_data;
// TODO: register further callbacks
/*
* REMINDER: every callback added here must be unregistered in the
* mosquitto_plugin_cleanup below
*/
mosquitto_callback_register(identifier,
MOSQ_EVT_BASIC_AUTH,
handle_basic_auth,
NULL,
data);
mosquitto_callback_register(identifier,
MOSQ_EVT_ACL_CHECK,
handle_acl_check,
NULL,
data);
mosquitto_callback_register(identifier,
MOSQ_EVT_PSK_KEY,
handle_psk_key,
NULL,
data);
mosquitto_callback_register(identifier,
MOSQ_EVT_DISCONNECT,
handle_disconnect,
NULL,
data);
mosquitto_callback_register(identifier,
MOSQ_EVT_MESSAGE,
handle_message,
NULL,
data);
mosquitto_callback_register(identifier,
MOSQ_EVT_TICK,
handle_tick,
NULL,
data);
mosquitto_callback_register(identifier,
MOSQ_EVT_RELOAD,
handle_reload,
NULL,
data);
*userdata = data;
return MOSQ_ERR_SUCCESS;
}
static int _py_plugin_cleanup(void* user_data, struct mosquitto_opt *options, int option_count);
CFFI_DLLEXPORT int mosquitto_plugin_cleanup(void *user_data,
struct mosquitto_opt *options,
int option_count)
{
struct pyplugin_data *data = user_data;
mosquitto_callback_unregister(data->identifier,
MOSQ_EVT_BASIC_AUTH,
handle_basic_auth,
NULL);
mosquitto_callback_unregister(data->identifier,
MOSQ_EVT_ACL_CHECK,
handle_acl_check,
NULL);
mosquitto_callback_unregister(data->identifier,
MOSQ_EVT_PSK_KEY,
handle_psk_key,
NULL);
mosquitto_callback_unregister(data->identifier,
MOSQ_EVT_DISCONNECT,
handle_disconnect,
NULL);
mosquitto_callback_unregister(data->identifier,
MOSQ_EVT_MESSAGE,
handle_message,
NULL);
mosquitto_callback_unregister(data->identifier,
MOSQ_EVT_TICK,
handle_tick,
NULL);
mosquitto_callback_unregister(data->identifier,
MOSQ_EVT_RELOAD,
handle_reload,
NULL);
return _py_plugin_cleanup(data->user_data, options, option_count);
}