forked from eclipse-mosquitto/mosquitto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.c
1948 lines (1870 loc) · 75.3 KB
/
conf.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) 2009-2018 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
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include <config.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef WIN32
#else
# include <dirent.h>
#endif
#ifndef WIN32
# include <netdb.h>
# include <sys/socket.h>
#else
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
#if !defined(WIN32) && !defined(__CYGWIN__)
# include <syslog.h>
#endif
#include <mosquitto_broker.h>
#include <memory_mosq.h>
#include "tls_mosq.h"
#include "util_mosq.h"
#include "mqtt3_protocol.h"
struct config_recurse {
int log_dest;
int log_dest_set;
int log_type;
int log_type_set;
int max_inflight_messages;
int max_queued_messages;
};
#if defined(WIN32) || defined(__CYGWIN__)
#include <windows.h>
extern SERVICE_STATUS_HANDLE service_handle;
#endif
static int _conf_parse_bool(char **token, const char *name, bool *value, char *saveptr);
static int _conf_parse_int(char **token, const char *name, int *value, char *saveptr);
static int _conf_parse_string(char **token, const char *name, char **value, char *saveptr);
static int _config_read_file(struct mqtt3_config *config, bool reload, const char *file, struct config_recurse *config_tmp, int level, int *lineno);
static char *fgets_extending(char **buf, int *buflen, FILE *stream)
{
char *rc;
char endchar;
int offset = 0;
char *newbuf;
do{
rc = fgets(&((*buf)[offset]), *buflen-offset, stream);
if(feof(stream)){
return rc;
}
endchar = (*buf)[strlen(*buf)-1];
if(endchar == '\n'){
return rc;
}
/* No EOL char found, so extend buffer */
offset = *buflen-1;
*buflen += 1000;
newbuf = realloc(*buf, *buflen);
if(!newbuf){
return NULL;
}
*buf = newbuf;
}while(1);
}
static int _conf_attempt_resolve(const char *host, const char *text, int log, const char *msg)
{
struct addrinfo gai_hints;
struct addrinfo *gai_res;
int rc;
memset(&gai_hints, 0, sizeof(struct addrinfo));
gai_hints.ai_family = PF_UNSPEC;
gai_hints.ai_flags = AI_ADDRCONFIG;
gai_hints.ai_socktype = SOCK_STREAM;
gai_res = NULL;
rc = getaddrinfo(host, NULL, &gai_hints, &gai_res);
if(gai_res){
freeaddrinfo(gai_res);
}
if(rc != 0){
#ifndef WIN32
if(rc == EAI_SYSTEM){
if(errno == ENOENT){
_mosquitto_log_printf(NULL, log, "%s: Unable to resolve %s %s.", msg, text, host);
}else{
_mosquitto_log_printf(NULL, log, "%s: Error resolving %s: %s.", msg, text, strerror(errno));
}
}else{
_mosquitto_log_printf(NULL, log, "%s: Error resolving %s: %s.", msg, text, gai_strerror(rc));
}
#else
if(rc == WSAHOST_NOT_FOUND){
_mosquitto_log_printf(NULL, log, "%s: Error resolving %s.", msg, text);
}
#endif
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;
}
static void _config_init_reload(struct mosquitto_db *db, struct mqtt3_config *config)
{
int i;
/* Set defaults */
if(config->acl_file) _mosquitto_free(config->acl_file);
config->acl_file = NULL;
config->allow_anonymous = true;
config->allow_duplicate_messages = false;
config->allow_zero_length_clientid = true;
config->auto_id_prefix = NULL;
config->auto_id_prefix_len = 0;
config->autosave_interval = 1800;
config->autosave_on_changes = false;
if(config->clientid_prefixes) _mosquitto_free(config->clientid_prefixes);
config->connection_messages = true;
config->clientid_prefixes = NULL;
if(config->log_fptr){
fclose(config->log_fptr);
config->log_fptr = NULL;
}
if(config->log_file){
_mosquitto_free(config->log_file);
config->log_file = NULL;
}
#if defined(WIN32) || defined(__CYGWIN__)
if(service_handle){
/* This is running as a Windows service. Default to no logging. Using
* stdout/stderr is forbidden because the first clients to connect will
* get log information sent to them for some reason. */
config->log_dest = MQTT3_LOG_NONE;
}else{
config->log_dest = MQTT3_LOG_STDERR;
}
#else
config->log_facility = LOG_DAEMON;
config->log_dest = MQTT3_LOG_STDERR;
if(db->verbose){
config->log_type = INT_MAX;
}else{
config->log_type = MOSQ_LOG_ERR | MOSQ_LOG_WARNING | MOSQ_LOG_NOTICE | MOSQ_LOG_INFO;
}
#endif
config->log_timestamp = true;
if(config->password_file) _mosquitto_free(config->password_file);
config->password_file = NULL;
config->persistence = false;
if(config->persistence_location) _mosquitto_free(config->persistence_location);
config->persistence_location = NULL;
if(config->persistence_file) _mosquitto_free(config->persistence_file);
config->persistence_file = NULL;
config->persistent_client_expiration = 0;
if(config->psk_file) _mosquitto_free(config->psk_file);
config->psk_file = NULL;
config->queue_qos0_messages = false;
config->retry_interval = 20;
config->sys_interval = 10;
config->upgrade_outgoing_qos = false;
if(config->auth_options){
for(i=0; i<config->auth_option_count; i++){
_mosquitto_free(config->auth_options[i].key);
_mosquitto_free(config->auth_options[i].value);
}
_mosquitto_free(config->auth_options);
config->auth_options = NULL;
config->auth_option_count = 0;
}
}
void mqtt3_config_init(struct mosquitto_db *db, struct mqtt3_config *config)
{
memset(config, 0, sizeof(struct mqtt3_config));
_config_init_reload(db, config);
config->daemon = false;
config->default_listener.host = NULL;
config->default_listener.port = 0;
config->default_listener.max_connections = -1;
config->default_listener.mount_point = NULL;
config->default_listener.socks = NULL;
config->default_listener.sock_count = 0;
config->default_listener.client_count = 0;
config->default_listener.protocol = mp_mqtt;
config->default_listener.use_username_as_clientid = false;
#ifdef WITH_TLS
config->default_listener.tls_version = NULL;
config->default_listener.cafile = NULL;
config->default_listener.capath = NULL;
config->default_listener.certfile = NULL;
config->default_listener.keyfile = NULL;
config->default_listener.ciphers = NULL;
config->default_listener.psk_hint = NULL;
config->default_listener.require_certificate = false;
config->default_listener.crlfile = NULL;
config->default_listener.use_identity_as_username = false;
#endif
config->listeners = NULL;
config->listener_count = 0;
config->pid_file = NULL;
config->user = NULL;
#ifdef WITH_BRIDGE
config->bridges = NULL;
config->bridge_count = 0;
#endif
config->auth_plugin = NULL;
config->auth_plugin_deny_special_chars = true;
config->message_size_limit = 0;
}
void mqtt3_config_cleanup(struct mqtt3_config *config)
{
int i;
#ifdef WITH_BRIDGE
int j;
#endif
if(config->acl_file) _mosquitto_free(config->acl_file);
if(config->auto_id_prefix) _mosquitto_free(config->auto_id_prefix);
if(config->clientid_prefixes) _mosquitto_free(config->clientid_prefixes);
if(config->password_file) _mosquitto_free(config->password_file);
if(config->persistence_location) _mosquitto_free(config->persistence_location);
if(config->persistence_file) _mosquitto_free(config->persistence_file);
if(config->persistence_filepath) _mosquitto_free(config->persistence_filepath);
if(config->psk_file) _mosquitto_free(config->psk_file);
if(config->pid_file) _mosquitto_free(config->pid_file);
if(config->listeners){
for(i=0; i<config->listener_count; i++){
if(config->listeners[i].host) _mosquitto_free(config->listeners[i].host);
if(config->listeners[i].mount_point) _mosquitto_free(config->listeners[i].mount_point);
if(config->listeners[i].socks) _mosquitto_free(config->listeners[i].socks);
#ifdef WITH_TLS
if(config->listeners[i].cafile) _mosquitto_free(config->listeners[i].cafile);
if(config->listeners[i].capath) _mosquitto_free(config->listeners[i].capath);
if(config->listeners[i].certfile) _mosquitto_free(config->listeners[i].certfile);
if(config->listeners[i].keyfile) _mosquitto_free(config->listeners[i].keyfile);
if(config->listeners[i].ciphers) _mosquitto_free(config->listeners[i].ciphers);
if(config->listeners[i].psk_hint) _mosquitto_free(config->listeners[i].psk_hint);
if(config->listeners[i].crlfile) _mosquitto_free(config->listeners[i].crlfile);
if(config->listeners[i].tls_version) _mosquitto_free(config->listeners[i].tls_version);
#ifdef WITH_WEBSOCKETS
if(config->listeners[i].http_dir) _mosquitto_free(config->listeners[i].http_dir);
if(!config->listeners[i].ws_context) /* libwebsockets frees its own SSL_CTX */
#endif
{
SSL_CTX_free(config->listeners[i].ssl_ctx);
}
#endif
}
_mosquitto_free(config->listeners);
}
#ifdef WITH_BRIDGE
if(config->bridges){
for(i=0; i<config->bridge_count; i++){
if(config->bridges[i].name) _mosquitto_free(config->bridges[i].name);
if(config->bridges[i].addresses){
for(j=0; j<config->bridges[i].address_count; j++){
_mosquitto_free(config->bridges[i].addresses[j].address);
}
_mosquitto_free(config->bridges[i].addresses);
}
if(config->bridges[i].remote_clientid) _mosquitto_free(config->bridges[i].remote_clientid);
if(config->bridges[i].remote_username) _mosquitto_free(config->bridges[i].remote_username);
if(config->bridges[i].remote_password) _mosquitto_free(config->bridges[i].remote_password);
if(config->bridges[i].local_clientid) _mosquitto_free(config->bridges[i].local_clientid);
if(config->bridges[i].local_username) _mosquitto_free(config->bridges[i].local_username);
if(config->bridges[i].local_password) _mosquitto_free(config->bridges[i].local_password);
if(config->bridges[i].topics){
for(j=0; j<config->bridges[i].topic_count; j++){
if(config->bridges[i].topics[j].topic) _mosquitto_free(config->bridges[i].topics[j].topic);
if(config->bridges[i].topics[j].local_prefix) _mosquitto_free(config->bridges[i].topics[j].local_prefix);
if(config->bridges[i].topics[j].remote_prefix) _mosquitto_free(config->bridges[i].topics[j].remote_prefix);
if(config->bridges[i].topics[j].local_topic) _mosquitto_free(config->bridges[i].topics[j].local_topic);
if(config->bridges[i].topics[j].remote_topic) _mosquitto_free(config->bridges[i].topics[j].remote_topic);
}
_mosquitto_free(config->bridges[i].topics);
}
if(config->bridges[i].notification_topic) _mosquitto_free(config->bridges[i].notification_topic);
#ifdef WITH_TLS
if(config->bridges[i].tls_version) _mosquitto_free(config->bridges[i].tls_version);
if(config->bridges[i].tls_cafile) _mosquitto_free(config->bridges[i].tls_cafile);
#ifdef REAL_WITH_TLS_PSK
if(config->bridges[i].tls_psk_identity) _mosquitto_free(config->bridges[i].tls_psk_identity);
if(config->bridges[i].tls_psk) _mosquitto_free(config->bridges[i].tls_psk);
#endif
#endif
}
_mosquitto_free(config->bridges);
}
#endif
if(config->auth_plugin) _mosquitto_free(config->auth_plugin);
if(config->auth_options){
for(i=0; i<config->auth_option_count; i++){
_mosquitto_free(config->auth_options[i].key);
_mosquitto_free(config->auth_options[i].value);
}
_mosquitto_free(config->auth_options);
config->auth_options = NULL;
config->auth_option_count = 0;
}
if(config->log_fptr){
fclose(config->log_fptr);
config->log_fptr = NULL;
}
if(config->log_file){
_mosquitto_free(config->log_file);
config->log_file = NULL;
}
}
static void print_usage(void)
{
printf("mosquitto version %s (build date %s)\n\n", VERSION, TIMESTAMP);
printf("mosquitto is an MQTT v3.1.1/v3.1 broker.\n\n");
printf("Usage: mosquitto [-c config_file] [-d] [-h] [-p port]\n\n");
printf(" -c : specify the broker config file.\n");
printf(" -d : put the broker into the background after starting.\n");
printf(" -h : display this help.\n");
printf(" -p : start the broker listening on the specified port.\n");
printf(" Not recommended in conjunction with the -c option.\n");
printf(" -v : verbose mode - enable all logging types. This overrides\n");
printf(" any logging options given in the config file.\n");
printf("\nSee http://mosquitto.org/ for more information.\n\n");
}
int mqtt3_config_parse_args(struct mosquitto_db *db, struct mqtt3_config *config, int argc, char *argv[])
{
int i;
int port_tmp;
for(i=1; i<argc; i++){
if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config-file")){
if(i<argc-1){
db->config_file = argv[i+1];
if(mqtt3_config_read(db, config, false)){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open configuration file.");
return MOSQ_ERR_INVAL;
}
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: -c argument given, but no config file specified.");
return MOSQ_ERR_INVAL;
}
i++;
}else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon")){
config->daemon = true;
}else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")){
print_usage();
return MOSQ_ERR_INVAL;
}else if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){
if(i<argc-1){
port_tmp = atoi(argv[i+1]);
if(port_tmp<1 || port_tmp>65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port specified (%d).", port_tmp);
return MOSQ_ERR_INVAL;
}else{
if(config->default_listener.port){
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Default listener port specified multiple times. Only the latest will be used.");
}
config->default_listener.port = port_tmp;
}
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: -p argument given, but no port specified.");
return MOSQ_ERR_INVAL;
}
i++;
}else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")){
db->verbose = true;
}else{
fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]);
print_usage();
return MOSQ_ERR_INVAL;
}
}
if(config->listener_count == 0
#ifdef WITH_TLS
|| config->default_listener.cafile
|| config->default_listener.capath
|| config->default_listener.certfile
|| config->default_listener.keyfile
|| config->default_listener.ciphers
|| config->default_listener.psk_hint
|| config->default_listener.require_certificate
|| config->default_listener.crlfile
|| config->default_listener.use_identity_as_username
#endif
|| config->default_listener.use_username_as_clientid
|| config->default_listener.host
|| config->default_listener.port
|| config->default_listener.max_connections != -1
|| config->default_listener.mount_point
|| config->default_listener.protocol != mp_mqtt){
config->listener_count++;
config->listeners = _mosquitto_realloc(config->listeners, sizeof(struct _mqtt3_listener)*config->listener_count);
if(!config->listeners){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
memset(&config->listeners[config->listener_count-1], 0, sizeof(struct _mqtt3_listener));
if(config->default_listener.port){
config->listeners[config->listener_count-1].port = config->default_listener.port;
}else{
config->listeners[config->listener_count-1].port = 1883;
}
if(config->default_listener.host){
config->listeners[config->listener_count-1].host = config->default_listener.host;
}else{
config->listeners[config->listener_count-1].host = NULL;
}
if(config->default_listener.mount_point){
config->listeners[config->listener_count-1].mount_point = config->default_listener.mount_point;
}else{
config->listeners[config->listener_count-1].mount_point = NULL;
}
config->listeners[config->listener_count-1].max_connections = config->default_listener.max_connections;
config->listeners[config->listener_count-1].protocol = config->default_listener.protocol;
config->listeners[config->listener_count-1].client_count = 0;
config->listeners[config->listener_count-1].socks = NULL;
config->listeners[config->listener_count-1].sock_count = 0;
config->listeners[config->listener_count-1].client_count = 0;
config->listeners[config->listener_count-1].use_username_as_clientid = config->default_listener.use_username_as_clientid;
#ifdef WITH_TLS
config->listeners[config->listener_count-1].tls_version = config->default_listener.tls_version;
config->listeners[config->listener_count-1].cafile = config->default_listener.cafile;
config->listeners[config->listener_count-1].capath = config->default_listener.capath;
config->listeners[config->listener_count-1].certfile = config->default_listener.certfile;
config->listeners[config->listener_count-1].keyfile = config->default_listener.keyfile;
config->listeners[config->listener_count-1].ciphers = config->default_listener.ciphers;
config->listeners[config->listener_count-1].psk_hint = config->default_listener.psk_hint;
config->listeners[config->listener_count-1].require_certificate = config->default_listener.require_certificate;
config->listeners[config->listener_count-1].ssl_ctx = NULL;
config->listeners[config->listener_count-1].crlfile = config->default_listener.crlfile;
config->listeners[config->listener_count-1].use_identity_as_username = config->default_listener.use_identity_as_username;
#endif
}
/* Default to drop to mosquitto user if we are privileged and no user specified. */
if(!config->user){
config->user = "mosquitto";
}
if(db->verbose){
config->log_type = INT_MAX;
}
return MOSQ_ERR_SUCCESS;
}
/* Copy reloaded config into existing config struct */
void config__copy(struct mqtt3_config *src, struct mqtt3_config *dest)
{
_mosquitto_free(dest->acl_file);
dest->acl_file = src->acl_file;
dest->allow_anonymous = src->allow_anonymous;
dest->allow_duplicate_messages = src->allow_duplicate_messages;
dest->allow_zero_length_clientid = src->allow_zero_length_clientid;
_mosquitto_free(dest->auto_id_prefix);
dest->auto_id_prefix = src->auto_id_prefix;
dest->auto_id_prefix_len = src->auto_id_prefix_len;
dest->autosave_interval = src->autosave_interval;
dest->autosave_on_changes = src->autosave_on_changes;
_mosquitto_free(dest->clientid_prefixes);
dest->clientid_prefixes = src->clientid_prefixes;
dest->connection_messages = src->connection_messages;
dest->log_dest = src->log_dest;
dest->log_facility = src->log_facility;
dest->log_type = src->log_type;
dest->log_timestamp = src->log_timestamp;
_mosquitto_free(dest->log_file);
dest->log_file = src->log_file;
dest->message_size_limit = src->message_size_limit;
_mosquitto_free(dest->password_file);
dest->password_file = src->password_file;
dest->persistence = src->persistence;
_mosquitto_free(dest->persistence_location);
dest->persistence_location = src->persistence_location;
_mosquitto_free(dest->persistence_file);
dest->persistence_file = src->persistence_file;
_mosquitto_free(dest->persistence_filepath);
dest->persistence_filepath = src->persistence_filepath;
dest->persistent_client_expiration = src->persistent_client_expiration;
_mosquitto_free(dest->psk_file);
dest->psk_file = src->psk_file;
dest->queue_qos0_messages = src->queue_qos0_messages;
dest->retry_interval = src->retry_interval;
dest->sys_interval = src->sys_interval;
dest->upgrade_outgoing_qos = src->upgrade_outgoing_qos;
#ifdef WITH_WEBSOCKETS
dest->websockets_log_level = src->websockets_log_level;
#endif
}
int mqtt3_config_read(struct mosquitto_db *db, struct mqtt3_config *config, bool reload)
{
int rc = MOSQ_ERR_SUCCESS;
struct config_recurse cr;
int lineno;
int len;
struct mqtt3_config config_reload;
#ifdef WITH_BRIDGE
int i;
#endif
if(reload){
memset(&config_reload, 0, sizeof(struct mqtt3_config));
}
cr.log_dest = MQTT3_LOG_NONE;
cr.log_dest_set = 0;
cr.log_type = MOSQ_LOG_NONE;
cr.log_type_set = 0;
cr.max_inflight_messages = 20;
cr.max_queued_messages = 100;
if(!db->config_file) return 0;
if(reload){
/* Re-initialise appropriate config vars to default for reload. */
_config_init_reload(db, &config_reload);
rc = _config_read_file(&config_reload, reload, db->config_file, &cr, 0, &lineno);
}else{
rc = _config_read_file(config, reload, db->config_file, &cr, 0, &lineno);
}
if(rc){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error found at %s:%d.", db->config_file, lineno);
return rc;
}
if(reload){
config__copy(&config_reload, config);
}
#ifdef WITH_PERSISTENCE
if(config->persistence){
if(!config->persistence_file){
config->persistence_file = _mosquitto_strdup("mosquitto.db");
if(!config->persistence_file) return MOSQ_ERR_NOMEM;
}
if(config->persistence_filepath){
_mosquitto_free(config->persistence_filepath);
}
if(config->persistence_location && strlen(config->persistence_location)){
len = strlen(config->persistence_location) + strlen(config->persistence_file) + 1;
config->persistence_filepath = _mosquitto_malloc(len);
if(!config->persistence_filepath) return MOSQ_ERR_NOMEM;
snprintf(config->persistence_filepath, len, "%s%s", config->persistence_location, config->persistence_file);
}else{
config->persistence_filepath = _mosquitto_strdup(config->persistence_file);
if(!config->persistence_filepath) return MOSQ_ERR_NOMEM;
}
}
#endif
/* Default to drop to mosquitto user if no other user specified. This must
* remain here even though it is covered in mqtt3_parse_args() because this
* function may be called on its own. */
if(!config->user){
config->user = "mosquitto";
}
mqtt3_db_limits_set(cr.max_inflight_messages, cr.max_queued_messages);
#ifdef WITH_BRIDGE
for(i=0; i<config->bridge_count; i++){
if(!config->bridges[i].name || !config->bridges[i].addresses || !config->bridges[i].topic_count){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
#ifdef REAL_WITH_TLS_PSK
if(config->bridges[i].tls_psk && !config->bridges[i].tls_psk_identity){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration: missing bridge_identity.\n");
return MOSQ_ERR_INVAL;
}
if(config->bridges[i].tls_psk_identity && !config->bridges[i].tls_psk){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration: missing bridge_psk.\n");
return MOSQ_ERR_INVAL;
}
#endif
}
#endif
if(cr.log_dest_set){
config->log_dest = cr.log_dest;
}
if(db->verbose){
config->log_type = INT_MAX;
}else if(cr.log_type_set){
config->log_type = cr.log_type;
}
return MOSQ_ERR_SUCCESS;
}
int _config_read_file_core(struct mqtt3_config *config, bool reload, const char *file, struct config_recurse *cr, int level, int *lineno, FILE *fptr, char **buf, int *buflen)
{
int rc;
char *token;
int tmp_int;
char *saveptr = NULL;
#ifdef WITH_BRIDGE
struct _mqtt3_bridge *cur_bridge = NULL;
struct _mqtt3_bridge_topic *cur_topic;
#endif
time_t expiration_mult;
char *key;
char *conf_file;
#ifdef WIN32
HANDLE fh;
char dirpath[MAX_PATH];
WIN32_FIND_DATA find_data;
#else
DIR *dh;
struct dirent *de;
#endif
int len;
struct _mqtt3_listener *cur_listener = &config->default_listener;
#ifdef WITH_BRIDGE
char *address;
int i;
#endif
int lineno_ext;
*lineno = 0;
while(fgets_extending(buf, buflen, fptr)){
(*lineno)++;
if((*buf)[0] != '#' && (*buf)[0] != 10 && (*buf)[0] != 13){
while((*buf)[strlen((*buf))-1] == 10 || (*buf)[strlen((*buf))-1] == 13){
(*buf)[strlen((*buf))-1] = 0;
}
token = strtok_r((*buf), " ", &saveptr);
if(token){
if(!strcmp(token, "acl_file")){
if(reload){
if(config->acl_file){
_mosquitto_free(config->acl_file);
config->acl_file = NULL;
}
}
if(_conf_parse_string(&token, "acl_file", &config->acl_file, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "address") || !strcmp(token, "addresses")){
#ifdef WITH_BRIDGE
if(reload) continue; // FIXME
if(!cur_bridge || cur_bridge->addresses){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
while((token = strtok_r(NULL, " ", &saveptr))){
cur_bridge->address_count++;
cur_bridge->addresses = _mosquitto_realloc(cur_bridge->addresses, sizeof(struct bridge_address)*cur_bridge->address_count);
if(!cur_bridge->addresses){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
cur_bridge->addresses[cur_bridge->address_count-1].address = token;
}
for(i=0; i<cur_bridge->address_count; i++){
address = strtok_r(cur_bridge->addresses[i].address, ":", &saveptr);
if(address){
token = strtok_r(NULL, ":", &saveptr);
if(token){
tmp_int = atoi(token);
if(tmp_int < 1 || tmp_int > 65535){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid port value (%d).", tmp_int);
return MOSQ_ERR_INVAL;
}
cur_bridge->addresses[i].port = tmp_int;
}else{
cur_bridge->addresses[i].port = 1883;
}
cur_bridge->addresses[i].address = _mosquitto_strdup(address);
_conf_attempt_resolve(address, "bridge address", MOSQ_LOG_WARNING, "Warning");
}
}
if(cur_bridge->address_count == 0){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Empty address value in configuration.");
return MOSQ_ERR_INVAL;
}
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available.");
#endif
}else if(!strcmp(token, "allow_anonymous")){
if(_conf_parse_bool(&token, "allow_anonymous", &config->allow_anonymous, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "allow_duplicate_messages")){
if(_conf_parse_bool(&token, "allow_duplicate_messages", &config->allow_duplicate_messages, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "allow_zero_length_clientid")){
if(_conf_parse_bool(&token, "allow_zero_length_clientid", &config->allow_zero_length_clientid, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strncmp(token, "auth_opt_", 9)){
if(strlen(token) < 12){
/* auth_opt_ == 9, + one digit key == 10, + one space == 11, + one value == 12 */
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid auth_opt_ config option.");
return MOSQ_ERR_INVAL;
}
key = _mosquitto_strdup(&token[9]);
if(!key){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}else if(STREMPTY(key)){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid auth_opt_ config option.");
return MOSQ_ERR_INVAL;
}
token += 9+strlen(key)+1;
while(token[0] == ' ' || token[0] == '\t'){
token++;
}
if(token[0]){
config->auth_option_count++;
config->auth_options = _mosquitto_realloc(config->auth_options, config->auth_option_count*sizeof(struct mosquitto_auth_opt));
if(!config->auth_options){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
config->auth_options[config->auth_option_count-1].key = key;
config->auth_options[config->auth_option_count-1].value = _mosquitto_strdup(token);
if(!config->auth_options[config->auth_option_count-1].value){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Empty %s value in configuration.", key);
return MOSQ_ERR_INVAL;
}
}else if(!strcmp(token, "auth_plugin")){
if(reload) continue; // Auth plugin not currently valid for reloading.
if(_conf_parse_string(&token, "auth_plugin", &config->auth_plugin, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "auth_plugin_deny_special_chars")){
if(reload) continue; // Auth plugin not currently valid for reloading.
if(_conf_parse_bool(&token, "auth_plugin_deny_special_chars", &config->auth_plugin_deny_special_chars, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "auto_id_prefix")){
if(_conf_parse_string(&token, "auto_id_prefix", &config->auto_id_prefix, saveptr)) return MOSQ_ERR_INVAL;
if(config->auto_id_prefix){
config->auto_id_prefix_len = strlen(config->auto_id_prefix);
}else{
config->auto_id_prefix_len = 0;
}
}else if(!strcmp(token, "autosave_interval")){
if(_conf_parse_int(&token, "autosave_interval", &config->autosave_interval, saveptr)) return MOSQ_ERR_INVAL;
if(config->autosave_interval < 0) config->autosave_interval = 0;
}else if(!strcmp(token, "autosave_on_changes")){
if(_conf_parse_bool(&token, "autosave_on_changes", &config->autosave_on_changes, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "bind_address")){
if(reload) continue; // Listener not valid for reloading.
if(_conf_parse_string(&token, "default listener bind_address", &config->default_listener.host, saveptr)) return MOSQ_ERR_INVAL;
if(_conf_attempt_resolve(config->default_listener.host, "bind_address", MOSQ_LOG_ERR, "Error")){
return MOSQ_ERR_INVAL;
}
}else if(!strcmp(token, "bridge_attempt_unsubscribe")){
#ifdef WITH_BRIDGE
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_bool(&token, "bridge_attempt_unsubscribe", &cur_bridge->attempt_unsubscribe, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available.");
#endif
}else if(!strcmp(token, "bridge_cafile")){
#if defined(WITH_BRIDGE) && defined(WITH_TLS)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
#ifdef REAL_WITH_TLS_PSK
if(cur_bridge->tls_psk_identity || cur_bridge->tls_psk){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and psk encryption in a single bridge.");
return MOSQ_ERR_INVAL;
}
#endif
if(_conf_parse_string(&token, "bridge_cafile", &cur_bridge->tls_cafile, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS support not available.");
#endif
}else if(!strcmp(token, "bridge_capath")){
#if defined(WITH_BRIDGE) && defined(WITH_TLS)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
#ifdef REAL_WITH_TLS_PSK
if(cur_bridge->tls_psk_identity || cur_bridge->tls_psk){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and psk encryption in a single bridge.");
return MOSQ_ERR_INVAL;
}
#endif
if(_conf_parse_string(&token, "bridge_capath", &cur_bridge->tls_capath, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS support not available.");
#endif
}else if(!strcmp(token, "bridge_certfile")){
#if defined(WITH_BRIDGE) && defined(WITH_TLS)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
#ifdef REAL_WITH_TLS_PSK
if(cur_bridge->tls_psk_identity || cur_bridge->tls_psk){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and psk encryption in a single bridge.");
return MOSQ_ERR_INVAL;
}
#endif
if(_conf_parse_string(&token, "bridge_certfile", &cur_bridge->tls_certfile, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS support not available.");
#endif
}else if(!strcmp(token, "bridge_identity")){
#if defined(WITH_BRIDGE) && defined(REAL_WITH_TLS_PSK)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
if(cur_bridge->tls_cafile || cur_bridge->tls_capath || cur_bridge->tls_certfile || cur_bridge->tls_keyfile){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and identity encryption in a single bridge.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_string(&token, "bridge_identity", &cur_bridge->tls_psk_identity, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS-PSK support not available.");
#endif
}else if(!strcmp(token, "bridge_insecure")){
#if defined(WITH_BRIDGE) && defined(WITH_TLS)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_bool(&token, "bridge_insecure", &cur_bridge->tls_insecure, saveptr)) return MOSQ_ERR_INVAL;
if(cur_bridge->tls_insecure){
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge %s using insecure mode.", cur_bridge->name);
}
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS-PSK support not available.");
#endif
}else if(!strcmp(token, "bridge_keyfile")){
#if defined(WITH_BRIDGE) && defined(WITH_TLS)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
#ifdef REAL_WITH_TLS_PSK
if(cur_bridge->tls_psk_identity || cur_bridge->tls_psk){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and psk encryption in a single bridge.");
return MOSQ_ERR_INVAL;
}
#endif
if(_conf_parse_string(&token, "bridge_keyfile", &cur_bridge->tls_keyfile, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS support not available.");
#endif
}else if(!strcmp(token, "bridge_protocol_version")){
#ifdef WITH_BRIDGE
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
token = strtok_r(NULL, "", &saveptr);
if(token){
if(!strcmp(token, "mqttv31")){
cur_bridge->protocol_version = mosq_p_mqtt31;
}else if(!strcmp(token, "mqttv311")){
cur_bridge->protocol_version = mosq_p_mqtt311;
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge_protocol_version value (%s).", token);
return MOSQ_ERR_INVAL;
}
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Empty bridge_protocol_version value in configuration.");
return MOSQ_ERR_INVAL;
}
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available.");
#endif
}else if(!strcmp(token, "bridge_psk")){
#if defined(WITH_BRIDGE) && defined(REAL_WITH_TLS_PSK)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
if(cur_bridge->tls_cafile || cur_bridge->tls_capath || cur_bridge->tls_certfile || cur_bridge->tls_keyfile){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and psk encryption in a single bridge.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_string(&token, "bridge_psk", &cur_bridge->tls_psk, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS-PSK support not available.");
#endif
}else if(!strcmp(token, "bridge_tls_version")){
#if defined(WITH_BRIDGE) && defined(WITH_TLS)
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_string(&token, "bridge_tls_version", &cur_bridge->tls_version, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TLS support not available.");
#endif
}else if(!strcmp(token, "cafile")){
#if defined(WITH_TLS)
if(reload) continue; // Listeners not valid for reloading.
if(cur_listener->psk_hint){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and psk encryption in a single listener.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_string(&token, "cafile", &cur_listener->cafile, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: TLS support not available.");
#endif
}else if(!strcmp(token, "capath")){
#ifdef WITH_TLS
if(reload) continue; // Listeners not valid for reloading.
if(_conf_parse_string(&token, "capath", &cur_listener->capath, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: TLS support not available.");
#endif
}else if(!strcmp(token, "certfile")){
#ifdef WITH_TLS
if(reload) continue; // Listeners not valid for reloading.
if(cur_listener->psk_hint){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Cannot use both certificate and psk encryption in a single listener.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_string(&token, "certfile", &cur_listener->certfile, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: TLS support not available.");
#endif
}else if(!strcmp(token, "ciphers")){
#ifdef WITH_TLS
if(reload) continue; // Listeners not valid for reloading.
if(_conf_parse_string(&token, "ciphers", &cur_listener->ciphers, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: TLS support not available.");
#endif
}else if(!strcmp(token, "clientid") || !strcmp(token, "remote_clientid")){
#ifdef WITH_BRIDGE
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}
if(_conf_parse_string(&token, "bridge remote clientid", &cur_bridge->remote_clientid, saveptr)) return MOSQ_ERR_INVAL;
#else
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available.");
#endif
}else if(!strcmp(token, "cleansession")){
#ifdef WITH_BRIDGE
if(reload) continue; // FIXME
if(!cur_bridge){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration.");
return MOSQ_ERR_INVAL;
}