-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
security_default.c
1369 lines (1205 loc) · 36.5 KB
/
security_default.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2011-2020 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:https://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "send_mosq.h"
#include "misc_mosq.h"
#include "util_mosq.h"
static int aclfile__parse(struct mosquitto__security_options *security_opts);
static int unpwd__file_parse(struct mosquitto__unpwd **unpwd, const char *password_file);
static int acl__cleanup(bool reload);
static int unpwd__cleanup(struct mosquitto__unpwd **unpwd, bool reload);
static int psk__file_parse(struct mosquitto__unpwd **psk_id, const char *psk_file);
#ifdef WITH_TLS
static int pw__digest(const char *password, const unsigned char *salt, unsigned int salt_len, unsigned char *hash, unsigned int *hash_len, enum mosquitto_pwhash_type hashtype, int iterations);
#endif
static int mosquitto_unpwd_check_default(int event, void *event_data, void *userdata);
static int mosquitto_acl_check_default(int event, void *event_data, void *userdata);
int mosquitto_security_init_default(bool reload)
{
int rc;
int i;
char *pwf;
char *pskf = NULL;
UNUSED(reload);
/* Configure plugin identifier */
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
db.config->listeners[i].security_options.pid = mosquitto__calloc(1, sizeof(mosquitto_plugin_id_t));
if(db.config->listeners[i].security_options.pid == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
db.config->listeners[i].security_options.pid->listener = &db.config->listeners[i];
}
}else{
db.config->security_options.pid = mosquitto__calloc(1, sizeof(mosquitto_plugin_id_t));
if(db.config->security_options.pid == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}
/* Load username/password data if required. */
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
pwf = db.config->listeners[i].security_options.password_file;
if(pwf){
rc = unpwd__file_parse(&db.config->listeners[i].security_options.unpwd, pwf);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR, "Error opening password file \"%s\".", pwf);
return rc;
}
mosquitto_callback_register(db.config->listeners[i].security_options.pid,
MOSQ_EVT_BASIC_AUTH, mosquitto_unpwd_check_default, NULL, NULL);
}
}
}else{
if(db.config->security_options.password_file){
pwf = db.config->security_options.password_file;
if(pwf){
rc = unpwd__file_parse(&db.config->security_options.unpwd, pwf);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR, "Error opening password file \"%s\".", pwf);
return rc;
}
}
mosquitto_callback_register(db.config->security_options.pid,
MOSQ_EVT_BASIC_AUTH, mosquitto_unpwd_check_default, NULL, NULL);
}
}
/* Load acl data if required. */
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
if(db.config->listeners[i].security_options.acl_file){
rc = aclfile__parse(&db.config->listeners[i].security_options);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR, "Error opening acl file \"%s\".", db.config->listeners[i].security_options.acl_file);
return rc;
}
mosquitto_callback_register(db.config->listeners[i].security_options.pid,
MOSQ_EVT_ACL_CHECK, mosquitto_acl_check_default, NULL, NULL);
}
}
}else{
if(db.config->security_options.acl_file){
rc = aclfile__parse(&db.config->security_options);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR, "Error opening acl file \"%s\".", db.config->security_options.acl_file);
return rc;
}
mosquitto_callback_register(db.config->security_options.pid,
MOSQ_EVT_ACL_CHECK, mosquitto_acl_check_default, NULL, NULL);
}
}
/* Load psk data if required. */
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
pskf = db.config->listeners[i].security_options.psk_file;
if(pskf){
rc = psk__file_parse(&db.config->listeners[i].security_options.psk_id, pskf);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR, "Error opening psk file \"%s\".", pskf);
return rc;
}
}
}
}else{
pskf = db.config->security_options.psk_file;
if(pskf){
rc = psk__file_parse(&db.config->security_options.psk_id, pskf);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR, "Error opening psk file \"%s\".", pskf);
return rc;
}
}
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_security_cleanup_default(bool reload)
{
int rc;
int i;
rc = acl__cleanup(reload);
if(rc != MOSQ_ERR_SUCCESS) return rc;
rc = unpwd__cleanup(&db.config->security_options.unpwd, reload);
if(rc != MOSQ_ERR_SUCCESS) return rc;
for(i=0; i<db.config->listener_count; i++){
if(db.config->listeners[i].security_options.unpwd){
rc = unpwd__cleanup(&db.config->listeners[i].security_options.unpwd, reload);
if(rc != MOSQ_ERR_SUCCESS) return rc;
}
}
rc = unpwd__cleanup(&db.config->security_options.psk_id, reload);
if(rc != MOSQ_ERR_SUCCESS) return rc;
for(i=0; i<db.config->listener_count; i++){
if(db.config->listeners[i].security_options.psk_id){
rc = unpwd__cleanup(&db.config->listeners[i].security_options.psk_id, reload);
if(rc != MOSQ_ERR_SUCCESS) return rc;
}
}
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
if(db.config->listeners[i].security_options.pid){
mosquitto_callback_unregister(db.config->listeners[i].security_options.pid,
MOSQ_EVT_BASIC_AUTH, mosquitto_unpwd_check_default, NULL);
mosquitto_callback_unregister(db.config->listeners[i].security_options.pid,
MOSQ_EVT_ACL_CHECK, mosquitto_acl_check_default, NULL);
mosquitto__free(db.config->listeners[i].security_options.pid);
}
}
}else{
if(db.config->security_options.pid){
mosquitto_callback_unregister(db.config->security_options.pid,
MOSQ_EVT_BASIC_AUTH, mosquitto_unpwd_check_default, NULL);
mosquitto_callback_unregister(db.config->security_options.pid,
MOSQ_EVT_ACL_CHECK, mosquitto_acl_check_default, NULL);
mosquitto__free(db.config->security_options.pid);
}
}
return MOSQ_ERR_SUCCESS;
}
static int add__acl(struct mosquitto__security_options *security_opts, const char *user, const char *topic, int access)
{
struct mosquitto__acl_user *acl_user=NULL, *user_tail;
struct mosquitto__acl *acl, *acl_tail;
char *local_topic;
bool new_user = false;
if(!security_opts || !topic) return MOSQ_ERR_INVAL;
local_topic = mosquitto__strdup(topic);
if(!local_topic){
return MOSQ_ERR_NOMEM;
}
if(security_opts->acl_list){
user_tail = security_opts->acl_list;
while(user_tail){
if(user == NULL){
if(user_tail->username == NULL){
acl_user = user_tail;
break;
}
}else if(user_tail->username && !strcmp(user_tail->username, user)){
acl_user = user_tail;
break;
}
user_tail = user_tail->next;
}
}
if(!acl_user){
acl_user = mosquitto__malloc(sizeof(struct mosquitto__acl_user));
if(!acl_user){
mosquitto__free(local_topic);
return MOSQ_ERR_NOMEM;
}
new_user = true;
if(user){
acl_user->username = mosquitto__strdup(user);
if(!acl_user->username){
mosquitto__free(local_topic);
mosquitto__free(acl_user);
return MOSQ_ERR_NOMEM;
}
}else{
acl_user->username = NULL;
}
acl_user->next = NULL;
acl_user->acl = NULL;
}
acl = mosquitto__malloc(sizeof(struct mosquitto__acl));
if(!acl){
mosquitto__free(local_topic);
mosquitto__free(acl_user->username);
mosquitto__free(acl_user);
return MOSQ_ERR_NOMEM;
}
acl->access = access;
acl->topic = local_topic;
acl->next = NULL;
acl->ccount = 0;
acl->ucount = 0;
/* Add acl to user acl list */
if(acl_user->acl){
acl_tail = acl_user->acl;
if(access == MOSQ_ACL_NONE){
/* Put "deny" acls at front of the list */
acl->next = acl_tail;
acl_user->acl = acl;
}else{
while(acl_tail->next){
acl_tail = acl_tail->next;
}
acl_tail->next = acl;
}
}else{
acl_user->acl = acl;
}
if(new_user){
/* Add to end of list */
if(security_opts->acl_list){
user_tail = security_opts->acl_list;
while(user_tail->next){
user_tail = user_tail->next;
}
user_tail->next = acl_user;
}else{
security_opts->acl_list = acl_user;
}
}
return MOSQ_ERR_SUCCESS;
}
static int add__acl_pattern(struct mosquitto__security_options *security_opts, const char *topic, int access)
{
struct mosquitto__acl *acl, *acl_tail;
char *local_topic;
char *s;
if(!security_opts| !topic) return MOSQ_ERR_INVAL;
local_topic = mosquitto__strdup(topic);
if(!local_topic){
return MOSQ_ERR_NOMEM;
}
acl = mosquitto__malloc(sizeof(struct mosquitto__acl));
if(!acl){
mosquitto__free(local_topic);
return MOSQ_ERR_NOMEM;
}
acl->access = access;
acl->topic = local_topic;
acl->next = NULL;
acl->ccount = 0;
s = local_topic;
while(s){
s = strstr(s, "%c");
if(s){
acl->ccount++;
s+=2;
}
}
acl->ucount = 0;
s = local_topic;
while(s){
s = strstr(s, "%u");
if(s){
acl->ucount++;
s+=2;
}
}
if(acl->ccount == 0 && acl->ucount == 0){
log__printf(NULL, MOSQ_LOG_WARNING,
"Warning: ACL pattern '%s' does not contain '%%c' or '%%u'.",
topic);
}
if(security_opts->acl_patterns){
acl_tail = security_opts->acl_patterns;
if(access == MOSQ_ACL_NONE){
/* Put "deny" acls at front of the list */
acl->next = acl_tail;
security_opts->acl_patterns = acl;
}else{
while(acl_tail->next){
acl_tail = acl_tail->next;
}
acl_tail->next = acl;
}
}else{
security_opts->acl_patterns = acl;
}
return MOSQ_ERR_SUCCESS;
}
static int mosquitto_acl_check_default(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_acl_check *ed = event_data;
char *local_acl;
struct mosquitto__acl *acl_root;
bool result;
size_t i;
size_t len, tlen, clen, ulen;
char *s;
struct mosquitto__security_options *security_opts = NULL;
UNUSED(event);
UNUSED(userdata);
if(ed->client->bridge) return MOSQ_ERR_SUCCESS;
if(ed->access == MOSQ_ACL_SUBSCRIBE || ed->access == MOSQ_ACL_UNSUBSCRIBE) return MOSQ_ERR_SUCCESS; /* FIXME - implement ACL subscription strings. */
if(db.config->per_listener_settings){
if(!ed->client->listener) return MOSQ_ERR_ACL_DENIED;
security_opts = &ed->client->listener->security_options;
}else{
security_opts = &db.config->security_options;
}
if(!security_opts->acl_file && !security_opts->acl_list && !security_opts->acl_patterns){
return MOSQ_ERR_PLUGIN_DEFER;
}
if(!ed->client->acl_list && !security_opts->acl_patterns) return MOSQ_ERR_ACL_DENIED;
if(ed->client->acl_list){
acl_root = ed->client->acl_list->acl;
}else{
acl_root = NULL;
}
/* Loop through all ACLs for this client. ACL denials are iterated over first. */
while(acl_root){
/* Loop through the topic looking for matches to this ACL. */
/* If subscription starts with $, acl_root->topic must also start with $. */
if(ed->topic[0] == '$' && acl_root->topic[0] != '$'){
acl_root = acl_root->next;
continue;
}
mosquitto_topic_matches_sub(acl_root->topic, ed->topic, &result);
if(result){
if(acl_root->access == MOSQ_ACL_NONE){
/* Access was explicitly denied for this topic. */
return MOSQ_ERR_ACL_DENIED;
}
if(ed->access & acl_root->access){
/* And access is allowed. */
return MOSQ_ERR_SUCCESS;
}
}
acl_root = acl_root->next;
}
acl_root = security_opts->acl_patterns;
if(acl_root){
/* We are using pattern based acls. Check whether the username or
* client id contains a + or # and if so deny access.
*
* Without this, a malicious client may configure its username/client
* id to bypass ACL checks (or have a username/client id that cannot
* publish or receive messages to its own place in the hierarchy).
*/
if(ed->client->username && strpbrk(ed->client->username, "+#")){
log__printf(NULL, MOSQ_LOG_NOTICE, "ACL denying access to client with dangerous username \"%s\"", ed->client->username);
return MOSQ_ERR_ACL_DENIED;
}
if(ed->client->id && strpbrk(ed->client->id, "+#")){
log__printf(NULL, MOSQ_LOG_NOTICE, "ACL denying access to client with dangerous client id \"%s\"", ed->client->id);
return MOSQ_ERR_ACL_DENIED;
}
}
/* Loop through all pattern ACLs. ACL denial patterns are iterated over first. */
if(!ed->client->id) return MOSQ_ERR_ACL_DENIED;
clen = strlen(ed->client->id);
while(acl_root){
tlen = strlen(acl_root->topic);
if(acl_root->ucount && !ed->client->username){
acl_root = acl_root->next;
continue;
}
if(ed->client->username){
ulen = strlen(ed->client->username);
len = tlen + (size_t)acl_root->ccount*(clen-2) + (size_t)acl_root->ucount*(ulen-2);
}else{
ulen = 0;
len = tlen + (size_t)acl_root->ccount*(clen-2);
}
local_acl = mosquitto__malloc(len+1);
if(!local_acl) return MOSQ_ERR_NOMEM;
s = local_acl;
for(i=0; i<tlen; i++){
if(i<tlen-1 && acl_root->topic[i] == '%'){
if(acl_root->topic[i+1] == 'c'){
i++;
strncpy(s, ed->client->id, clen);
s+=clen;
continue;
}else if(ed->client->username && acl_root->topic[i+1] == 'u'){
i++;
strncpy(s, ed->client->username, ulen);
s+=ulen;
continue;
}
}
s[0] = acl_root->topic[i];
s++;
}
local_acl[len] = '\0';
mosquitto_topic_matches_sub(local_acl, ed->topic, &result);
mosquitto__free(local_acl);
if(result){
if(acl_root->access == MOSQ_ACL_NONE){
/* Access was explicitly denied for this topic pattern. */
return MOSQ_ERR_ACL_DENIED;
}
if(ed->access & acl_root->access){
/* And access is allowed. */
return MOSQ_ERR_SUCCESS;
}
}
acl_root = acl_root->next;
}
return MOSQ_ERR_ACL_DENIED;
}
static int aclfile__parse(struct mosquitto__security_options *security_opts)
{
FILE *aclfptr = NULL;
char *token;
char *user = NULL;
char *topic;
char *access_s;
int access;
int rc = MOSQ_ERR_SUCCESS;
size_t slen;
int topic_pattern;
char *saveptr = NULL;
char *buf = NULL;
int buflen = 256;
if(!db.config) return MOSQ_ERR_INVAL;
if(!security_opts) return MOSQ_ERR_INVAL;
if(!security_opts->acl_file) return MOSQ_ERR_SUCCESS;
buf = mosquitto__malloc((size_t)buflen);
if(buf == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
aclfptr = mosquitto__fopen(security_opts->acl_file, "rt", false);
if(!aclfptr){
mosquitto__free(buf);
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open acl_file \"%s\".", security_opts->acl_file);
return MOSQ_ERR_UNKNOWN;
}
/* topic [read|write] <topic>
* user <user>
*/
while(fgets_extending(&buf, &buflen, aclfptr)){
slen = strlen(buf);
while(slen > 0 && isspace(buf[slen-1])){
buf[slen-1] = '\0';
slen = strlen(buf);
}
if(buf[0] == '#'){
continue;
}
token = strtok_r(buf, " ", &saveptr);
if(token){
if(!strcmp(token, "topic") || !strcmp(token, "pattern")){
if(!strcmp(token, "topic")){
topic_pattern = 0;
}else{
topic_pattern = 1;
}
access_s = strtok_r(NULL, " ", &saveptr);
if(!access_s){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Empty topic in acl_file \"%s\".", security_opts->acl_file);
rc = MOSQ_ERR_INVAL;
break;
}
token = strtok_r(NULL, "", &saveptr);
if(token){
topic = misc__trimblanks(token);
}else{
topic = access_s;
access_s = NULL;
}
if(access_s){
if(!strcmp(access_s, "read")){
access = MOSQ_ACL_READ;
}else if(!strcmp(access_s, "write")){
access = MOSQ_ACL_WRITE;
}else if(!strcmp(access_s, "readwrite")){
access = MOSQ_ACL_READ | MOSQ_ACL_WRITE;
}else if(!strcmp(access_s, "deny")){
access = MOSQ_ACL_NONE;
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid topic access type \"%s\" in acl_file \"%s\".", access_s, security_opts->acl_file);
rc = MOSQ_ERR_INVAL;
break;
}
}else{
access = MOSQ_ACL_READ | MOSQ_ACL_WRITE;
}
rc = mosquitto_sub_topic_check(topic);
if(rc != MOSQ_ERR_SUCCESS){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid ACL topic \"%s\" in acl_file \"%s\".", topic, security_opts->acl_file);
rc = MOSQ_ERR_INVAL;
break;
}
if(topic_pattern == 0){
rc = add__acl(security_opts, user, topic, access);
}else{
rc = add__acl_pattern(security_opts, topic, access);
}
if(rc){
break;
}
}else if(!strcmp(token, "user")){
token = strtok_r(NULL, "", &saveptr);
if(token){
token = misc__trimblanks(token);
if(slen == 0){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Missing username in acl_file \"%s\".", security_opts->acl_file);
rc = MOSQ_ERR_INVAL;
break;
}
mosquitto__free(user);
user = mosquitto__strdup(token);
if(!user){
rc = MOSQ_ERR_NOMEM;
break;
}
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Missing username in acl_file \"%s\".", security_opts->acl_file);
rc = MOSQ_ERR_INVAL;
break;
}
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid line in acl_file \"%s\": %s.", security_opts->acl_file, buf);
rc = MOSQ_ERR_INVAL;
break;
}
}
}
mosquitto__free(buf);
mosquitto__free(user);
fclose(aclfptr);
return rc;
}
static void free__acl(struct mosquitto__acl *acl)
{
if(!acl) return;
if(acl->next){
free__acl(acl->next);
}
mosquitto__free(acl->topic);
mosquitto__free(acl);
}
static void acl__cleanup_single(struct mosquitto__security_options *security_opts)
{
struct mosquitto__acl_user *user_tail;
while(security_opts->acl_list){
user_tail = security_opts->acl_list->next;
free__acl(security_opts->acl_list->acl);
mosquitto__free(security_opts->acl_list->username);
mosquitto__free(security_opts->acl_list);
security_opts->acl_list = user_tail;
}
if(security_opts->acl_patterns){
free__acl(security_opts->acl_patterns);
security_opts->acl_patterns = NULL;
}
}
static int acl__cleanup(bool reload)
{
struct mosquitto *context, *ctxt_tmp = NULL;
int i;
UNUSED(reload);
/* As we're freeing ACLs, we must clear context->acl_list to ensure no
* invalid memory accesses take place later.
* This *requires* the ACLs to be reapplied after acl__cleanup()
* is called if we are reloading the config. If this is not done, all
* access will be denied to currently connected clients.
*/
HASH_ITER(hh_id, db.contexts_by_id, context, ctxt_tmp){
context->acl_list = NULL;
}
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
acl__cleanup_single(&db.config->listeners[i].security_options);
}
}else{
acl__cleanup_single(&db.config->security_options);
}
return MOSQ_ERR_SUCCESS;
}
int acl__find_acls(struct mosquitto *context)
{
struct mosquitto__acl_user *acl_tail;
struct mosquitto__security_options *security_opts;
/* Associate user with its ACL, assuming we have ACLs loaded. */
if(db.config->per_listener_settings){
if(!context->listener){
return MOSQ_ERR_INVAL;
}
security_opts = &context->listener->security_options;
}else{
security_opts = &db.config->security_options;
}
if(security_opts->acl_list){
acl_tail = security_opts->acl_list;
while(acl_tail){
if(context->username){
if(acl_tail->username && !strcmp(context->username, acl_tail->username)){
context->acl_list = acl_tail;
break;
}
}else{
if(acl_tail->username == NULL){
context->acl_list = acl_tail;
break;
}
}
acl_tail = acl_tail->next;
}
}else{
context->acl_list = NULL;
}
return MOSQ_ERR_SUCCESS;
}
static int pwfile__parse(const char *file, struct mosquitto__unpwd **root)
{
FILE *pwfile;
struct mosquitto__unpwd *unpwd;
char *username, *password;
char *saveptr = NULL;
char *buf;
int buflen = 256;
buf = mosquitto__malloc((size_t)buflen);
if(buf == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
pwfile = mosquitto__fopen(file, "rt", false);
if(!pwfile){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open pwfile \"%s\".", file);
mosquitto__free(buf);
return MOSQ_ERR_UNKNOWN;
}
while(!feof(pwfile)){
if(fgets_extending(&buf, &buflen, pwfile)){
if(buf[0] == '#') continue;
if(!strchr(buf, ':')) continue;
username = strtok_r(buf, ":", &saveptr);
if(username){
unpwd = mosquitto__calloc(1, sizeof(struct mosquitto__unpwd));
if(!unpwd){
fclose(pwfile);
mosquitto__free(buf);
return MOSQ_ERR_NOMEM;
}
username = misc__trimblanks(username);
if(strlen(username) > 65535){
log__printf(NULL, MOSQ_LOG_NOTICE, "Warning: Invalid line in password file '%s', username too long.", file);
mosquitto__free(unpwd);
continue;
}
unpwd->username = mosquitto__strdup(username);
if(!unpwd->username){
mosquitto__free(unpwd);
mosquitto__free(buf);
fclose(pwfile);
return MOSQ_ERR_NOMEM;
}
password = strtok_r(NULL, ":", &saveptr);
if(password){
password = misc__trimblanks(password);
if(strlen(password) > 65535){
log__printf(NULL, MOSQ_LOG_NOTICE, "Warning: Invalid line in password file '%s', password too long.", file);
mosquitto__free(unpwd->username);
mosquitto__free(unpwd);
continue;
}
unpwd->password = mosquitto__strdup(password);
if(!unpwd->password){
fclose(pwfile);
mosquitto__free(unpwd->username);
mosquitto__free(unpwd);
mosquitto__free(buf);
return MOSQ_ERR_NOMEM;
}
HASH_ADD_KEYPTR(hh, *root, unpwd->username, strlen(unpwd->username), unpwd);
}else{
log__printf(NULL, MOSQ_LOG_NOTICE, "Warning: Invalid line in password file '%s': %s", file, buf);
mosquitto__free(unpwd->username);
mosquitto__free(unpwd);
}
}
}
}
fclose(pwfile);
mosquitto__free(buf);
return MOSQ_ERR_SUCCESS;
}
void unpwd__free_item(struct mosquitto__unpwd **unpwd, struct mosquitto__unpwd *item)
{
mosquitto__free(item->username);
mosquitto__free(item->password);
#ifdef WITH_TLS
mosquitto__free(item->salt);
#endif
HASH_DEL(*unpwd, item);
mosquitto__free(item);
}
#ifdef WITH_TLS
static int unpwd__decode_passwords(struct mosquitto__unpwd **unpwd)
{
struct mosquitto__unpwd *u, *tmp = NULL;
char *token;
unsigned char *salt;
unsigned int salt_len;
unsigned char *password;
unsigned int password_len;
int rc;
enum mosquitto_pwhash_type hashtype;
HASH_ITER(hh, *unpwd, u, tmp){
/* Need to decode password into hashed data + salt. */
if(u->password == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Missing password hash for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
continue;
}
token = strtok(u->password, "$");
if(token == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
continue;
}
if(!strcmp(token, "6")){
hashtype = pw_sha512;
}else if(!strcmp(token, "7")){
hashtype = pw_sha512_pbkdf2;
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash type for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
continue;
}
if(hashtype == pw_sha512_pbkdf2){
token = strtok(NULL, "$");
if(token == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
continue;
}
u->iterations = atoi(token);
if(u->iterations < 1){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid hash iterations for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
continue;
}
}
token = strtok(NULL, "$");
if(token == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
continue;
}
rc = base64__decode(token, &salt, &salt_len);
if(rc == MOSQ_ERR_SUCCESS && salt_len == 12){
u->salt = salt;
u->salt_len = salt_len;
token = strtok(NULL, "$");
if(token){
rc = base64__decode(token, &password, &password_len);
if(rc == MOSQ_ERR_SUCCESS && password_len == 64){
mosquitto__free(u->password);
u->password = (char *)password;
u->password_len = password_len;
u->hashtype = hashtype;
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to decode password for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
}
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
}
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to decode password salt for user %s, removing entry.", u->username);
unpwd__free_item(unpwd, u);
}
}
return MOSQ_ERR_SUCCESS;
}
#endif
static int unpwd__file_parse(struct mosquitto__unpwd **unpwd, const char *password_file)
{
int rc;
if(!unpwd) return MOSQ_ERR_INVAL;
if(!password_file) return MOSQ_ERR_SUCCESS;
rc = pwfile__parse(password_file, unpwd);
#ifdef WITH_TLS
if(rc) return rc;
rc = unpwd__decode_passwords(unpwd);
#endif
return rc;
}
static int psk__file_parse(struct mosquitto__unpwd **psk_id, const char *psk_file)
{
int rc;
struct mosquitto__unpwd *u, *tmp = NULL;
if(!db.config || !psk_id) return MOSQ_ERR_INVAL;
/* We haven't been asked to parse a psk file. */
if(!psk_file) return MOSQ_ERR_SUCCESS;
rc = pwfile__parse(psk_file, psk_id);
if(rc) return rc;
HASH_ITER(hh, (*psk_id), u, tmp){
/* Check for hex only digits */
if(!u->password){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Empty psk for identity \"%s\".", u->username);
return MOSQ_ERR_INVAL;
}
if(strspn(u->password, "0123456789abcdefABCDEF") < strlen(u->password)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: psk for identity \"%s\" contains non-hexadecimal characters.", u->username);
return MOSQ_ERR_INVAL;
}
}
return MOSQ_ERR_SUCCESS;
}
#ifdef WITH_TLS
static int mosquitto__memcmp_const(const void *a, const void *b, size_t len)
{
size_t i;
int rc = 0;
if(!a || !b) return 1;
for(i=0; i<len; i++){
if( ((char *)a)[i] != ((char *)b)[i] ){
rc = 1;
}
}
return rc;
}
#endif
static int mosquitto_unpwd_check_default(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_basic_auth *ed = event_data;
struct mosquitto__unpwd *u;
struct mosquitto__unpwd *unpwd_ref;
#ifdef WITH_TLS
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hash_len;
int rc;