-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmosquitto_multi_tenant.c
401 lines (317 loc) · 10.9 KB
/
mosquitto_multi_tenant.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
/*
Copyright (c) 2020-2024 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Abilio Marques - initial implementation and documentation.
Ben Hardill - convert to regex based multi-tenant
*/
/*
* A very simple Multi-Tenant broker plugin
*
* A enhanced version of the mosquitto_topic_jail example.
*
* It uses a supplied regex to extract a "team" name from the username.
* The regex must include a single capture group that extracts the team name,
* with a default '[a-z0-9]+@([a-z0-9]+)'
*
* e.g. username 'foo@bar' would give a team of 'bar'
*
* Compile with:
* gcc -I<path to mosquitto-repo/include> -fPIC -shared mosquitto_multi_tenant.c -o mosquitto_multi_tenant.so
*
* Use in config with:
*
* plugin /path/to/mosquitto_multi_tenant.so
*
* Note that this only works on Mosquitto 2.1 or later.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include "mosquitto.h"
#define PLUGIN_NAME "multi-tenant"
#define PLUGIN_VERSION "1.1.0"
#define UNUSED(A) (void)(A)
MOSQUITTO_PLUGIN_DECLARE_VERSION(5);
static mosquitto_plugin_id_t *mosq_pid = NULL;
static regex_t username_match;
static regex_t shared_sub_match;
static char* get_team( const char *str) {
regmatch_t pmatch[2];
char *team;
size_t size;
int result = regexec(&username_match, str, 2, pmatch, 0);
if (result == 0) {
size = (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so);
team = mosquitto_malloc(size+1);
strncpy(team, str+pmatch[1].rm_so, size);
team[size] = 0;
return team;
} else {
return NULL;
}
}
static int connect_callback(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_message *ed = event_data;
const char *id, *username;
char *new_id;
size_t idlen, new_id_len;
UNUSED(event);
UNUSED(userdata);
username = mosquitto_client_username(ed->client);
if (!username) {
return MOSQ_ERR_SUCCESS;
}
id = mosquitto_client_id(ed->client);
idlen = strlen(id);
const char *team = get_team(username);
if(!team){
/* will only modify the client id of team clients */
return MOSQ_ERR_SUCCESS;
}
/* calculate new client id length */
new_id_len = strlen(team) + sizeof('@') + idlen + 1;
new_id = mosquitto_calloc(1, new_id_len);
if(new_id == NULL){
return MOSQ_ERR_NOMEM;
}
/* generate new client id with team name */
snprintf(new_id, new_id_len, "%s@%s", id, team);
mosquitto_free((void *)team);
mosquitto_set_clientid(ed->client, new_id);
return MOSQ_ERR_SUCCESS;
}
static int callback_message_in(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_message *ed = event_data;
char *new_topic;
size_t new_topic_len;
UNUSED(event);
UNUSED(userdata);
const char *username = mosquitto_client_username(ed->client);
if (!username) {
return MOSQ_ERR_SUCCESS;
}
const char *team = get_team(username);
if(!team){
/* will only modify the topic of team clients */
return MOSQ_ERR_SUCCESS;
}
/* put the team on front of the topic */
/* calculate the length of the new payload */
new_topic_len = strlen(team) + sizeof('/') + strlen(ed->topic) + 1;
/* Allocate some memory - use
* mosquitto_calloc/mosquitto_malloc/mosquitto_strdup when allocating, to
* allow the broker to track memory usage */
new_topic = mosquitto_calloc(1, new_topic_len);
if(new_topic == NULL){
return MOSQ_ERR_NOMEM;
}
/* prepend the team to the topic */
snprintf(new_topic, new_topic_len, "%s/%s", team, ed->topic);
mosquitto_free((void *)team);
/* Assign the new topic to the event data structure. You
* must *not* free the original topic, it will be handled by the
* broker. */
ed->topic = new_topic;
return MOSQ_ERR_SUCCESS;
}
static int callback_message_out(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_message *ed = event_data;
size_t team_len;
UNUSED(event);
UNUSED(userdata);
const char *username = mosquitto_client_username(ed->client);
if (!username) {
return MOSQ_ERR_SUCCESS;
}
const char *team = get_team(username);
if(!team){
/* will only modify the topic of team clients */
return MOSQ_ERR_SUCCESS;
}
/* remove the team from the front of the topic */
team_len = strlen(team);
if(strlen(ed->topic) <= team_len + 1){
/* the topic is not long enough to contain the
* team + '/' */
return MOSQ_ERR_SUCCESS;
}
if(!strncmp(team, ed->topic, team_len) && ed->topic[team_len] == '/'){
/* Allocate some memory - use
* mosquitto_calloc/mosquitto_malloc/mosquitto_strdup when allocating, to
* allow the broker to track memory usage */
/* skip the team + '/' */
char *new_topic = mosquitto_strdup(ed->topic + team_len + 1);
if(new_topic == NULL){
return MOSQ_ERR_NOMEM;
}
/* Assign the new topic to the event data structure. You
* must *not* free the original topic, it will be handled by the
* broker. */
ed->topic = new_topic;
}
mosquitto_free((void *)team);
return MOSQ_ERR_SUCCESS;
}
static int callback_subscribe(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_subscribe *ed = event_data;
char *new_sub, *share_group, *topic;
regmatch_t pmatch[3];
size_t new_sub_len, group_size, topic_size;
UNUSED(event);
UNUSED(userdata);
const char *username = mosquitto_client_username(ed->client);
if (!username) {
return MOSQ_ERR_SUCCESS;
}
const char *team = get_team(username);
if(!team){
/* will only modify the topic of team clients */
return MOSQ_ERR_SUCCESS;
}
if (!strncmp(ed->data.topic_filter, "$share/", 7)) {
new_sub_len = strlen(team) + (sizeof('/') * 2) + strlen(ed->data.topic_filter) + 1;
new_sub = mosquitto_calloc(1, new_sub_len);
if(new_sub == NULL){
return MOSQ_ERR_NOMEM;
}
int rc = regexec(&shared_sub_match, ed->data.topic_filter, 3, pmatch, 0);
if (rc == 0) {
group_size = (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so);
topic_size = (size_t)(pmatch[2].rm_eo - pmatch[2].rm_so);
share_group = mosquitto_malloc(group_size+1);
topic = mosquitto_malloc(topic_size+1);
strncpy(share_group, ed->data.topic_filter + pmatch[1].rm_so, group_size);
share_group[group_size] = 0;
strncpy(topic, ed->data.topic_filter + pmatch[2].rm_so, topic_size);
topic[topic_size+1] = 0;
snprintf(new_sub, new_sub_len, "%s/%s/%s", share_group, team, topic);
mosquitto_free((void *)share_group);
mosquitto_free((void *)topic);
} else {
return MOSQ_ERR_NOMEM;
}
} else {
/* put the team on front of the topic */
/* calculate the length of the new payload */
new_sub_len = strlen(team) + sizeof('/') + strlen(ed->data.topic_filter) + 1;
/* Allocate some memory - use
* mosquitto_calloc/mosquitto_malloc/mosquitto_strdup when allocating, to
* allow the broker to track memory usage */
new_sub = mosquitto_calloc(1, new_sub_len);
if(new_sub == NULL){
return MOSQ_ERR_NOMEM;
}
/* prepend the team to the subscription */
snprintf(new_sub, new_sub_len, "%s/%s", team, ed->data.topic_filter);
}
/* Assign the new topic to the event data structure. You
* must *not* free the original topic, it will be handled by the
* broker. */
ed->data.topic_filter = new_sub;
mosquitto_free((void *) team);
return MOSQ_ERR_SUCCESS;
}
static int callback_unsubscribe(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_unsubscribe *ed = event_data;
char *new_sub, *share_group, *topic;
regmatch_t pmatch[3];
size_t new_sub_len, group_size, topic_size;
UNUSED(event);
UNUSED(userdata);
const char *username = mosquitto_client_username(ed->client);
if (!username) {
return MOSQ_ERR_SUCCESS;
}
const char *team = get_team(username);
if(!team){
/* will only modify the topic of team clients */
// mosquitto_free((void *)team);
return MOSQ_ERR_SUCCESS;
}
if (!strncmp(ed->data.topic_filter, "$share/", 7)) {
new_sub_len = strlen(team) + (sizeof('/') * 2) + strlen(ed->data.topic_filter) + 1;
new_sub = mosquitto_calloc(1, new_sub_len);
if(new_sub == NULL){
return MOSQ_ERR_NOMEM;
}
int rc = regexec(&shared_sub_match, ed->data.topic_filter, 3, pmatch, 0);
if (rc == 0) {
group_size = (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so);
topic_size = (size_t)(pmatch[2].rm_eo - pmatch[2].rm_so);
share_group = mosquitto_malloc(group_size+1);
topic = mosquitto_malloc(topic_size+1);
strncpy(share_group, ed->data.topic_filter + pmatch[1].rm_so, group_size);
share_group[group_size] = 0;
strncpy(topic, ed->data.topic_filter + pmatch[2].rm_so, topic_size);
topic[topic_size+1] = 0;
snprintf(new_sub, new_sub_len, "%s/%s/%s", share_group, team, topic);
mosquitto_free((void *)share_group);
mosquitto_free((void *)topic);
} else {
return MOSQ_ERR_NOMEM;
}
} else {
/* put the team on front of the topic */
/* calculate the length of the new payload */
new_sub_len = strlen(team) + sizeof('/') + strlen(ed->data.topic_filter) + 1;
/* Allocate some memory - use
* mosquitto_calloc/mosquitto_malloc/mosquitto_strdup when allocating, to
* allow the broker to track memory usage */
new_sub = mosquitto_calloc(1, new_sub_len);
if(new_sub == NULL){
return MOSQ_ERR_NOMEM;
}
/* prepend the team to the subscription */
snprintf(new_sub, new_sub_len, "%s/%s", team, ed->data.topic_filter);
}
/* Assign the new topic to the event data structure. You
* must *not* free the original topic, it will be handled by the
* broker. */
ed->data.topic_filter = new_sub;
mosquitto_free((void *)team);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count)
{
int i, found = 0;
UNUSED(user_data);
mosq_pid = identifier;
mosquitto_plugin_set_info(identifier, PLUGIN_NAME, PLUGIN_VERSION);
/* Find the configuration regex*/
for(i=0; i<opt_count; i++) {
if (!strcasecmp(opts[i].key, "regex")) {
regcomp(&username_match, opts[i].value, REG_EXTENDED);
found = 1;
}
}
/* If not found use the default */
if (!found) {
regcomp(&username_match, "^[a-z0-9]+@([a-z0-9]+)$", REG_EXTENDED);
}
regcomp(&shared_sub_match, "^(\\$share/[^/]+)/(.+)$", REG_EXTENDED);
int rc;
rc = mosquitto_callback_register(mosq_pid, MOSQ_EVT_CONNECT, connect_callback, NULL, NULL);
if(rc) return rc;
rc = mosquitto_callback_register(mosq_pid, MOSQ_EVT_MESSAGE_IN, callback_message_in, NULL, NULL);
if(rc) return rc;
rc = mosquitto_callback_register(mosq_pid, MOSQ_EVT_MESSAGE_OUT, callback_message_out, NULL, NULL);
if(rc) return rc;
rc = mosquitto_callback_register(mosq_pid, MOSQ_EVT_SUBSCRIBE, callback_subscribe, NULL, NULL);
if(rc) return rc;
rc = mosquitto_callback_register(mosq_pid, MOSQ_EVT_UNSUBSCRIBE, callback_unsubscribe, NULL, NULL);
return rc;
}