-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
security.c
474 lines (416 loc) · 14.7 KB
/
security.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
469
470
471
472
473
/*
Copyright (c) 2011-2016 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
https://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "mosquitto_broker_internal.h"
#include "mosquitto_plugin.h"
#include "memory_mosq.h"
#include "lib_load.h"
typedef int (*FUNC_auth_plugin_version)(void);
void LIB_ERROR(void)
{
#ifdef WIN32
char *buf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
NULL, GetLastError(), LANG_NEUTRAL, &buf, 0, NULL);
log__printf(NULL, MOSQ_LOG_ERR, "Load error: %s", buf);
LocalFree(buf);
#else
log__printf(NULL, MOSQ_LOG_ERR, "Load error: %s", dlerror());
#endif
}
int security__load_v2(struct mosquitto_db *db, struct mosquitto__auth_plugin *plugin, struct mosquitto_auth_opt *auth_options, int auth_option_count, void *lib)
{
int rc;
if(!(plugin->plugin_init_v2 = (FUNC_auth_plugin_init_v2)LIB_SYM(lib, "mosquitto_auth_plugin_init"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_plugin_init().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->plugin_cleanup_v2 = (FUNC_auth_plugin_cleanup_v2)LIB_SYM(lib, "mosquitto_auth_plugin_cleanup"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_plugin_cleanup().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->security_init_v2 = (FUNC_auth_plugin_security_init_v2)LIB_SYM(lib, "mosquitto_auth_security_init"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_security_init().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->security_cleanup_v2 = (FUNC_auth_plugin_security_cleanup_v2)LIB_SYM(lib, "mosquitto_auth_security_cleanup"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_security_cleanup().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->acl_check_v2 = (FUNC_auth_plugin_acl_check_v2)LIB_SYM(lib, "mosquitto_auth_acl_check"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_acl_check().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->unpwd_check_v2 = (FUNC_auth_plugin_unpwd_check_v2)LIB_SYM(lib, "mosquitto_auth_unpwd_check"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_unpwd_check().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->psk_key_get_v2 = (FUNC_auth_plugin_psk_key_get_v2)LIB_SYM(lib, "mosquitto_auth_psk_key_get"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_psk_key_get().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
plugin->lib = lib;
plugin->user_data = NULL;
if(plugin->plugin_init_v2){
rc = plugin->plugin_init_v2(&plugin->user_data, auth_options, auth_option_count);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Authentication plugin returned %d when initialising.", rc);
return rc;
}
}
return 0;
}
int security__load_v3(struct mosquitto_db *db, struct mosquitto__auth_plugin *plugin, struct mosquitto_opt *auth_options, int auth_option_count, void *lib)
{
int rc;
if(!(plugin->plugin_init_v3 = (FUNC_auth_plugin_init_v3)LIB_SYM(lib, "mosquitto_auth_plugin_init"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_plugin_init().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->plugin_cleanup_v3 = (FUNC_auth_plugin_cleanup_v3)LIB_SYM(lib, "mosquitto_auth_plugin_cleanup"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_plugin_cleanup().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->security_init_v3 = (FUNC_auth_plugin_security_init_v3)LIB_SYM(lib, "mosquitto_auth_security_init"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_security_init().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->security_cleanup_v3 = (FUNC_auth_plugin_security_cleanup_v3)LIB_SYM(lib, "mosquitto_auth_security_cleanup"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_security_cleanup().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->acl_check_v3 = (FUNC_auth_plugin_acl_check_v3)LIB_SYM(lib, "mosquitto_auth_acl_check"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_acl_check().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->unpwd_check_v3 = (FUNC_auth_plugin_unpwd_check_v3)LIB_SYM(lib, "mosquitto_auth_unpwd_check"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_unpwd_check().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
if(!(plugin->psk_key_get_v3 = (FUNC_auth_plugin_psk_key_get_v3)LIB_SYM(lib, "mosquitto_auth_psk_key_get"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_psk_key_get().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
plugin->lib = lib;
plugin->user_data = NULL;
if(plugin->plugin_init_v3){
rc = plugin->plugin_init_v3(&plugin->user_data, auth_options, auth_option_count);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Authentication plugin returned %d when initialising.", rc);
return rc;
}
}
return 0;
}
int mosquitto_security_module_init(struct mosquitto_db *db)
{
void *lib;
int (*plugin_version)(void) = NULL;
int version;
int rc;
int i;
if(db->config->auth_plugin_count == 0){
db->auth_plugins = NULL;
db->auth_plugin_count = 0;
}else{
db->auth_plugin_count = db->config->auth_plugin_count;
db->auth_plugins = mosquitto__calloc(db->auth_plugin_count, sizeof(struct mosquitto__auth_plugin));
if(!db->auth_plugins){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return 1;
}
}
for(i=0; i<db->config->auth_plugin_count; i++){
if(db->config->auth_plugins[i].path){
lib = LIB_LOAD(db->config->auth_plugins[i].path);
if(!lib){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin \"%s\".", db->config->auth_plugins[i].path);
LIB_ERROR();
return 1;
}
db->auth_plugins[i].lib = NULL;
if(!(plugin_version = (FUNC_auth_plugin_version)LIB_SYM(lib, "mosquitto_auth_plugin_version"))){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Unable to load auth plugin function mosquitto_auth_plugin_version().");
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
version = plugin_version();
db->auth_plugins[i].version = version;
if(version == 3){
rc = security__load_v3(db, &db->auth_plugins[i], db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count, lib);
if(rc){
return rc;
}
}else if(version == 2){
rc = security__load_v2(db, &db->auth_plugins[i], (struct mosquitto_auth_opt *)db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count, lib);
if(rc){
return rc;
}
}else{
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Incorrect auth plugin version (got %d, expected %d).",
version, MOSQ_AUTH_PLUGIN_VERSION);
LIB_ERROR();
LIB_CLOSE(lib);
return 1;
}
}
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_security_module_cleanup(struct mosquitto_db *db)
{
int i;
mosquitto_security_cleanup(db, false);
for(i=0; i<db->config->auth_plugin_count; i++){
if(db->auth_plugins[i].version == 3){
if(db->auth_plugins[i].plugin_cleanup_v3){
db->auth_plugins[i].plugin_cleanup_v3(db->auth_plugins[i].user_data, db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count);
}
}else if(db->auth_plugins[i].version == 2){
if(db->auth_plugins[i].plugin_cleanup_v2){
db->auth_plugins[i].plugin_cleanup_v2(db->auth_plugins[i].user_data, (struct mosquitto_auth_opt *)db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count);
}
}
if(db->auth_plugins[i].lib){
LIB_CLOSE(db->auth_plugins[i].lib);
}
db->auth_plugins[i].lib = NULL;
db->auth_plugins[i].plugin_init_v2 = NULL;
db->auth_plugins[i].plugin_cleanup_v2 = NULL;
db->auth_plugins[i].security_init_v2 = NULL;
db->auth_plugins[i].security_cleanup_v2 = NULL;
db->auth_plugins[i].acl_check_v2 = NULL;
db->auth_plugins[i].unpwd_check_v2 = NULL;
db->auth_plugins[i].psk_key_get_v2 = NULL;
db->auth_plugins[i].plugin_init_v3 = NULL;
db->auth_plugins[i].plugin_cleanup_v3 = NULL;
db->auth_plugins[i].security_init_v3 = NULL;
db->auth_plugins[i].security_cleanup_v3 = NULL;
db->auth_plugins[i].acl_check_v3 = NULL;
db->auth_plugins[i].unpwd_check_v3 = NULL;
db->auth_plugins[i].psk_key_get_v3 = NULL;
}
mosquitto__free(db->auth_plugins);
db->auth_plugins = NULL;
return MOSQ_ERR_SUCCESS;
}
int mosquitto_security_init(struct mosquitto_db *db, bool reload)
{
int i;
int rc;
for(i=0; i<db->config->auth_plugin_count; i++){
if(db->auth_plugins[i].version == 3){
rc = db->auth_plugins[i].security_init_v3(db->auth_plugins[i].user_data, db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count, reload);
}else if(db->auth_plugins[i].version == 2){
rc = db->auth_plugins[i].security_init_v2(db->auth_plugins[i].user_data, (struct mosquitto_auth_opt *)db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count, reload);
}else{
rc = MOSQ_ERR_INVAL;
}
if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
}
return mosquitto_security_init_default(db, reload);
}
/* Apply security settings after a reload.
* Includes:
* - Disconnecting anonymous users if appropriate
* - Disconnecting users with invalid passwords
* - Reapplying ACLs
*/
int mosquitto_security_apply(struct mosquitto_db *db)
{
return mosquitto_security_apply_default(db);
}
int mosquitto_security_cleanup(struct mosquitto_db *db, bool reload)
{
int i;
int rc;
for(i=0; i<db->config->auth_plugin_count; i++){
if(db->auth_plugins[i].version == 3){
rc = db->auth_plugins[i].security_cleanup_v3(db->auth_plugins[i].user_data, db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count, reload);
}else if(db->auth_plugins[i].version == 2){
rc = db->auth_plugins[i].security_cleanup_v2(db->auth_plugins[i].user_data, (struct mosquitto_auth_opt *)db->config->auth_plugins[i].options, db->config->auth_plugins[i].option_count, reload);
}else{
rc = MOSQ_ERR_INVAL;
}
if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
}
return mosquitto_security_cleanup_default(db, reload);
}
int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int access)
{
int rc;
int i;
struct mosquitto_acl_msg msg;
char *username;
if(!context->id){
return MOSQ_ERR_ACL_DENIED;
}
rc = mosquitto_acl_check_default(db, context, topic, access);
if(rc != MOSQ_ERR_PLUGIN_DEFER){
return rc;
}
/* Default check has accepted or deferred at this point.
* If no plugins exist we should accept at this point so set rc to success.
*/
rc = MOSQ_ERR_SUCCESS;
for(i=0; i<db->auth_plugin_count; i++){
memset(&msg, 0, sizeof(msg));
msg.topic = topic;
username = mosquito_client_username(context);
/* Check whether the client id or username contains a +, # or / and if
* so deny access.
*
* Do this check for every message regardless, we have to protect the
* plugins against possible pattern based attacks.
*/
if(username && strpbrk(username, "+#/")){
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "ACL denying access to client with dangerous username \"%s\"", username);
return MOSQ_ERR_ACL_DENIED;
}
if(context->id && strpbrk(context->id, "+#/")){
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "ACL denying access to client with dangerous client id \"%s\"", context->id);
return MOSQ_ERR_ACL_DENIED;
}
if(db->auth_plugins[i].version == 3){
rc = db->auth_plugins[i].acl_check_v3(db->auth_plugins[i].user_data, access, context, &msg);
}else if(db->auth_plugins[i].version == 2){
rc = db->auth_plugins[i].acl_check_v2(db->auth_plugins[i].user_data, context->id, username, topic, access);
}else{
rc = MOSQ_ERR_INVAL;
}
if(rc != MOSQ_ERR_PLUGIN_DEFER){
return rc;
}
}
/* If all plugins deferred, this is a denial. If rc == MOSQ_ERR_SUCCESS
* here, then no plugins were configured. */
if(rc == MOSQ_ERR_PLUGIN_DEFER){
rc = MOSQ_ERR_ACL_DENIED;
}
return rc;
}
int mosquitto_unpwd_check(struct mosquitto_db *db, struct mosquitto *context, const char *username, const char *password)
{
int rc;
int i;
rc = mosquitto_unpwd_check_default(db, username, password);
if(rc != MOSQ_ERR_PLUGIN_DEFER){
return rc;
}
/* Default check has accepted or deferred at this point.
* If no plugins exist we should accept at this point so set rc to success.
*/
rc = MOSQ_ERR_SUCCESS;
for(i=0; i<db->auth_plugin_count; i++){
if(db->auth_plugins[i].version == 3){
rc = db->auth_plugins[i].unpwd_check_v3(db->auth_plugins[i].user_data, context, username, password);
}else if(db->auth_plugins[i].version == 2){
rc = db->auth_plugins[i].unpwd_check_v2(db->auth_plugins[i].user_data, username, password);
}else{
rc = MOSQ_ERR_INVAL;
}
if(rc != MOSQ_ERR_PLUGIN_DEFER){
return rc;
}
}
/* If all plugins deferred, this is a denial. If rc == MOSQ_ERR_SUCCESS
* here, then no plugins were configured. */
if(rc == MOSQ_ERR_PLUGIN_DEFER){
rc = MOSQ_ERR_AUTH;
}
return rc;
}
int mosquitto_psk_key_get(struct mosquitto_db *db, struct mosquitto *context, const char *hint, const char *identity, char *key, int max_key_len)
{
int rc;
int i;
rc = mosquitto_psk_key_get_default(db, hint, identity, key, max_key_len);
if(rc != MOSQ_ERR_PLUGIN_DEFER){
return rc;
}
/* Default check has accepted or deferred at this point.
* If no plugins exist we should accept at this point so set rc to success.
*/
for(i=0; i<db->auth_plugin_count; i++){
if(db->auth_plugins[i].version == 3){
rc = db->auth_plugins[i].psk_key_get_v3(db->auth_plugins[i].user_data, context, hint, identity, key, max_key_len);
}else if(db->auth_plugins[i].version == 2){
rc = db->auth_plugins[i].psk_key_get_v2(db->auth_plugins[i].user_data, hint, identity, key, max_key_len);
}else{
rc = MOSQ_ERR_INVAL;
}
if(rc != MOSQ_ERR_PLUGIN_DEFER){
return rc;
}
}
/* If all plugins deferred, this is a denial. If rc == MOSQ_ERR_SUCCESS
* here, then no plugins were configured. */
if(rc == MOSQ_ERR_PLUGIN_DEFER){
rc = MOSQ_ERR_AUTH;
}
return rc;
}