-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathnet.c
More file actions
1149 lines (993 loc) · 31.7 KB
/
net.c
File metadata and controls
1149 lines (993 loc) · 31.7 KB
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-2021 Roger Light <roger@atchoo.org>
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:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#ifndef WIN32
# include <arpa/inet.h>
# ifndef _AIX
# include <ifaddrs.h>
# endif
# include <netdb.h>
# include <netinet/tcp.h>
# include <strings.h>
# include <sys/socket.h>
# include <unistd.h>
#else
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#ifdef WITH_WRAP
# include <tcpd.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if defined(WITH_UNIX_SOCKETS) || defined(WITH_TLS)
# include "sys/stat.h"
#endif
#ifdef WITH_UNIX_SOCKETS
# include "sys/un.h"
#endif
#ifdef __QNX__
# include <net/netbyte.h>
#endif
#include "mosquitto_broker_internal.h"
#include "mosquitto/mqtt_protocol.h"
#include "net_mosq.h"
#include "util_mosq.h"
#ifdef WITH_TLS
# include "tls_mosq.h"
# include <openssl/err.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
# include <openssl/store.h>
#endif
static int tls_ex_index_context = -1;
static int tls_ex_index_listener = -1;
#endif
#include "sys_tree.h"
/* For EMFILE handling */
static mosq_sock_t spare_sock = INVALID_SOCKET;
void net__broker_init(void)
{
spare_sock = socket(AF_INET, SOCK_STREAM, 0);
net__init();
#ifdef WITH_TLS
net__init_tls();
#endif
}
void net__broker_cleanup(void)
{
if(spare_sock != INVALID_SOCKET){
COMPAT_CLOSE(spare_sock);
spare_sock = INVALID_SOCKET;
}
net__cleanup();
}
static void net__print_error(unsigned int log, const char *format_str)
{
char *buf;
#ifdef WIN32
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, WSAGetLastError(), LANG_NEUTRAL, (LPTSTR)&buf, 0, NULL);
log__printf(NULL, log, format_str, buf);
LocalFree(buf);
#else
buf = strerror(errno);
log__printf(NULL, log, format_str, buf);
#endif
}
struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock)
{
mosq_sock_t new_sock = INVALID_SOCKET;
struct mosquitto *new_context;
#ifdef WITH_TLS
BIO *bio;
#endif
#ifdef WITH_WRAP
struct request_info wrap_req;
char address[1024];
#endif
new_sock = accept(listensock->sock, NULL, 0);
if(new_sock == INVALID_SOCKET){
#ifdef WIN32
WINDOWS_SET_ERRNO();
if(errno == WSAEMFILE){
#else
if(errno == EMFILE || errno == ENFILE){
#endif
/* Close the spare socket, which means we should be able to accept
* this connection. Accept it, then close it immediately and create
* a new spare_sock. This prevents the situation of ever properly
* running out of sockets.
* It would be nice to send a "server not available" connack here,
* but there are lots of reasons why this would be tricky (TLS
* being the big one). */
COMPAT_CLOSE(spare_sock);
new_sock = accept(listensock->sock, NULL, 0);
if(new_sock != INVALID_SOCKET){
COMPAT_CLOSE(new_sock);
}
spare_sock = socket(AF_INET, SOCK_STREAM, 0);
log__printf(NULL, MOSQ_LOG_WARNING,
"Unable to accept new connection, system socket count has been exceeded. Try increasing \"ulimit -n\" or equivalent.");
}
return NULL;
}
metrics__int_inc(mosq_counter_socket_connections, 1);
if(net__socket_nonblock(&new_sock)){
return NULL;
}
#ifdef WITH_WRAP
/* Use tcpd / libwrap to determine whether a connection is allowed. */
request_init(&wrap_req, RQ_FILE, new_sock, RQ_DAEMON, "mosquitto", 0);
fromhost(&wrap_req);
if(!hosts_access(&wrap_req)){
/* Access is denied */
if(db.config->connection_messages == true){
if(!net__socket_get_address(new_sock, address, 1024, NULL)){
log__printf(NULL, MOSQ_LOG_NOTICE, "Client connection from %s denied access by tcpd.", address);
}
}
COMPAT_CLOSE(new_sock);
return NULL;
}
#endif
if(db.config->set_tcp_nodelay && listensock->listener->port){
int flag = 1;
#ifdef WIN32
if(setsockopt(new_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) != 0){
#else
if(setsockopt(new_sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) != 0){
#endif
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Unable to set TCP_NODELAY.");
}
}
new_context = context__init();
if(!new_context){
COMPAT_CLOSE(new_sock);
return NULL;
}
new_context->listener = listensock->listener;
if(!new_context->listener){
context__cleanup(new_context, true);
return NULL;
}
new_context->listener->client_count++;
if(new_context->listener->enable_proxy_protocol){
if(context__init_sock(new_context, new_sock, false) != MOSQ_ERR_SUCCESS){
context__cleanup(new_context, true);
COMPAT_CLOSE(new_sock);
return NULL;
}
if(new_context->listener->enable_proxy_protocol == 2){
new_context->transport = mosq_t_proxy_v2;
}else{
new_context->transport = mosq_t_proxy_v1;
}
new_context->proxy.cmd = -1;
}else{
if(context__init_sock(new_context, new_sock, true) != MOSQ_ERR_SUCCESS){
context__cleanup(new_context, true);
COMPAT_CLOSE(new_sock);
return NULL;
}
switch(new_context->listener->protocol){
case mp_mqtt:
new_context->transport = mosq_t_tcp;
break;
#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN
case mp_websockets:
if(http__context_init(new_context)){
context__cleanup(new_context, true);
return NULL;
}
break;
#endif
default:
context__cleanup(new_context, true);
return NULL;
}
}
if((new_context->listener->max_connections > 0 && new_context->listener->client_count > new_context->listener->max_connections)
|| (db.config->global_max_connections > 0 && HASH_CNT(hh_sock, db.contexts_by_sock) > (unsigned int)db.config->global_max_connections)){
if(db.config->connection_messages == true){
log__printf(NULL, MOSQ_LOG_NOTICE, "Client connection from %s denied: max_connections exceeded.", new_context->address);
}
context__cleanup(new_context, true);
return NULL;
}
#ifdef WITH_TLS
/* TLS init */
if(new_context->listener->ssl_ctx){
new_context->ssl = SSL_new(new_context->listener->ssl_ctx);
if(!new_context->ssl){
context__cleanup(new_context, true);
return NULL;
}
if(!SSL_set_ex_data(new_context->ssl, tls_ex_index_context, new_context)
|| !SSL_set_ex_data(new_context->ssl, tls_ex_index_listener, new_context->listener)){
context__cleanup(new_context, true);
return NULL;
}
new_context->want_write = true;
bio = BIO_new_socket(new_sock, BIO_NOCLOSE);
SSL_set_bio(new_context->ssl, bio, bio);
ERR_clear_error();
SSL_set_accept_state(new_context->ssl);
}
#endif
if(db.config->connection_messages == true
&& !new_context->listener->enable_proxy_protocol){
log__printf(NULL, MOSQ_LOG_NOTICE, "New connection from %s:%d on port %d.",
new_context->address, new_context->remote_port, new_context->listener->port);
}
mux__new(new_context);
keepalive__add(new_context);
return new_context;
}
#ifdef WITH_TLS
static int client_certificate_verify(int preverify_ok, X509_STORE_CTX *ctx)
{
/* Preverify should check expiry, revocation. */
if(preverify_ok == 0){
SSL *ssl;
ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
if(ssl){
struct mosquitto__listener *listener;
listener = SSL_get_ex_data(ssl, tls_ex_index_listener);
if(listener && listener->disable_client_cert_date_checks){
int err = X509_STORE_CTX_get_error(ctx);
if(err == X509_V_ERR_CERT_NOT_YET_VALID || err == X509_V_ERR_CERT_HAS_EXPIRED){
preverify_ok = 1;
}
}
}
}
return preverify_ok;
}
#endif
#ifdef FINAL_WITH_TLS_PSK
static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len)
{
struct mosquitto *context;
struct mosquitto__listener *listener;
char *psk_key = NULL;
int len;
const char *psk_hint;
if(!identity){
return 0;
}
context = SSL_get_ex_data(ssl, tls_ex_index_context);
if(!context){
return 0;
}
listener = SSL_get_ex_data(ssl, tls_ex_index_listener);
if(!listener){
return 0;
}
psk_hint = listener->psk_hint;
/* The hex to BN conversion results in the length halving, so we can pass
* max_psk_len*2 as the max hex key here. */
psk_key = mosquitto_calloc(1, (size_t)max_psk_len*2 + 1);
if(!psk_key){
return 0;
}
if(mosquitto_psk_key_get(context, psk_hint, identity, psk_key, (int)max_psk_len*2) != MOSQ_ERR_SUCCESS){
mosquitto_FREE(psk_key);
return 0;
}
len = mosquitto__hex2bin(psk_key, psk, (int)max_psk_len);
if(len < 0){
mosquitto_FREE(psk_key);
return 0;
}
if(listener->use_identity_as_username){
if(mosquitto_validate_utf8(identity, (int)strlen(identity))){
mosquitto_free(psk_key);
return 0;
}
context->username = mosquitto_strdup(identity);
if(!context->username){
mosquitto_FREE(psk_key);
return 0;
}
}
mosquitto_FREE(psk_key);
return (unsigned int)len;
}
#endif
#ifdef WITH_TLS
static void tls_keylog_callback(const SSL *ssl, const char *line)
{
UNUSED(ssl);
if(db.tls_keylog){
FILE *fptr;
fptr = mosquitto_fopen(db.tls_keylog, "at", true);
if(fptr){
#ifndef WIN32
/* Until mosquitto_fopen enforces file permissions on all files,
* enforce it here. We can enforce it here, because it isn't a
* change of behaviour. */
struct stat statbuf;
if(fstat(fileno(fptr), &statbuf) < 0
|| statbuf.st_mode & (S_IRWXG | S_IRWXO)){
fclose(fptr);
return;
}
#endif
fprintf(fptr, "%s\n", line);
fclose(fptr);
}else{
#ifndef WIN32
if(errno == ELOOP){
log__printf(NULL, MOSQ_LOG_INFO, "Error: keylog file must not be a symbolic link");
}else
#endif
{
log__printf(NULL, MOSQ_LOG_INFO, "Error: Unable to open keylog file: %s", strerror(errno));
}
}
}
}
int net__tls_server_ctx(struct mosquitto__listener *listener)
{
char buf[256];
int rc;
if(tls_ex_index_context == -1){
tls_ex_index_context = SSL_get_ex_new_index(0, "client context", NULL, NULL, NULL);
}
if(tls_ex_index_listener == -1){
tls_ex_index_listener = SSL_get_ex_new_index(0, "listener", NULL, NULL, NULL);
}
if(listener->ssl_ctx){
SSL_CTX_free(listener->ssl_ctx);
listener->ssl_ctx = NULL;
}
listener->ssl_ctx = SSL_CTX_new(TLS_server_method());
if(!listener->ssl_ctx){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to create TLS context.");
return MOSQ_ERR_TLS;
}
#ifdef SSL_OP_NO_TLSv1_3
if(db.config->per_listener_settings){
if(listener->security_options->psk_file){
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_TLSv1_3);
}
}else{
if(db.config->security_options.psk_file){
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_TLSv1_3);
}
}
#endif
if(listener->tls_version == NULL){
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
#ifdef SSL_OP_NO_TLSv1_3
}else if(!strcmp(listener->tls_version, "tlsv1.3")){
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2);
#endif
}else if(!strcmp(listener->tls_version, "tlsv1.2")){
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
}else if(!strcmp(listener->tls_version, "tlsv1.1")){
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1);
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: TLS v1.1 is insecure. It must only be used in the case "
"where you have devices that cannot be upgraded to use a secure version of TLS. It is "
"recommended to use as secure a set of ciphers as possible and that will restrict it to "
"TLS v1.1 only, and to use a separate listener using TLS v1.2 and secure ciphers for your "
"other devices.");
log__printf(NULL, MOSQ_LOG_WARNING, "Please be aware that support for TLS v1.1 will go away eventually "
"and that you should plan now to migrate away from it.");
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unsupported tls_version \"%s\".", listener->tls_version);
return MOSQ_ERR_TLS;
}
#ifdef SSL_OP_NO_COMPRESSION
/* Disable compression */
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_COMPRESSION);
#endif
#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
/* Server chooses cipher */
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
#endif
#ifdef SSL_MODE_RELEASE_BUFFERS
/* Use even less memory per SSL connection. */
SSL_CTX_set_mode(listener->ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
#endif
SSL_CTX_set_dh_auto(listener->ssl_ctx, 1);
#ifdef SSL_OP_NO_RENEGOTIATION
SSL_CTX_set_options(listener->ssl_ctx, SSL_OP_NO_RENEGOTIATION);
#endif
if(db.tls_keylog){
log__printf(NULL, MOSQ_LOG_NOTICE, "TLS key logging to '%s' enabled for all listeners.",
db.tls_keylog);
log__printf(NULL, MOSQ_LOG_NOTICE, "TLS key logging is for DEBUGGING only.");
SSL_CTX_set_keylog_callback(listener->ssl_ctx, tls_keylog_callback);
}
snprintf(buf, 256, "mosquitto-%d", listener->port);
SSL_CTX_set_session_id_context(listener->ssl_ctx, (unsigned char *)buf, (unsigned int)strlen(buf));
if(listener->ciphers){
rc = SSL_CTX_set_cipher_list(listener->ssl_ctx, listener->ciphers);
if(rc == 0){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to set TLS ciphers. Check cipher list \"%s\".", listener->ciphers);
return MOSQ_ERR_TLS;
}
}else{
rc = SSL_CTX_set_cipher_list(listener->ssl_ctx, "DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:@STRENGTH");
if(rc == 0){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to set TLS ciphers. Check cipher list \"%s\".", listener->ciphers);
return MOSQ_ERR_TLS;
}
}
if(listener->ciphers_tls13){
rc = SSL_CTX_set_ciphersuites(listener->ssl_ctx, listener->ciphers_tls13);
if(rc == 0){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to set TLS 1.3 ciphersuites. Check cipher_tls13 list \"%s\".", listener->ciphers_tls13);
return MOSQ_ERR_TLS;
}
}
return MOSQ_ERR_SUCCESS;
}
#endif
#ifdef WITH_TLS
static int net__load_crl_file(struct mosquitto__listener *listener)
{
X509_STORE *store;
X509_LOOKUP *lookup;
int rc;
store = SSL_CTX_get_cert_store(listener->ssl_ctx);
if(!store){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to obtain TLS store.");
net__print_error(MOSQ_LOG_ERR, "Error: %s");
return MOSQ_ERR_TLS;
}
lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
rc = X509_load_crl_file(lookup, listener->crlfile, X509_FILETYPE_PEM);
if(rc < 1){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load certificate revocation file \"%s\". Check crlfile.", listener->crlfile);
net__print_error(MOSQ_LOG_ERR, "Error: %s");
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK);
return MOSQ_ERR_SUCCESS;
}
#endif
int net__load_certificates(struct mosquitto__listener *listener)
{
#ifdef WITH_TLS
int rc;
if(listener->require_certificate){
SSL_CTX_set_verify(listener->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, client_certificate_verify);
}else{
SSL_CTX_set_verify(listener->ssl_ctx, SSL_VERIFY_NONE, client_certificate_verify);
}
rc = SSL_CTX_use_certificate_chain_file(listener->ssl_ctx, listener->certfile);
if(rc != 1){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load server certificate \"%s\". Check certfile.", listener->certfile);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
if(listener->tls_keyform == mosq_k_pem){
rc = SSL_CTX_use_PrivateKey_file(listener->ssl_ctx, listener->keyfile, SSL_FILETYPE_PEM);
if(rc != 1){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load server key file \"%s\". Check keyfile.", listener->keyfile);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
}else if(listener->tls_keyform == mosq_k_uri){
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
OSSL_STORE_CTX *store = NULL;
OSSL_STORE_INFO *info = NULL;
EVP_PKEY *pkey = NULL;
store = OSSL_STORE_open(listener->keyfile, NULL, NULL, NULL, NULL);
if(!store){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open key URI \"%s\".", listener->keyfile);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
while(!OSSL_STORE_eof(store)){
info = OSSL_STORE_load(store);
if(info == NULL){
if(!OSSL_STORE_eof(store)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load key from URI \"%s\".", listener->keyfile);
net__print_ssl_error(NULL, NULL);
}
continue;
}
if(OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY){
pkey = OSSL_STORE_INFO_get1_PKEY(info);
OSSL_STORE_INFO_free(info);
if(pkey){
break;
}
}
OSSL_STORE_INFO_free(info);
}
OSSL_STORE_close(store);
if(!pkey){
log__printf(NULL, MOSQ_LOG_ERR, "Error: No private key found in URI \"%s\".", listener->keyfile);
return MOSQ_ERR_TLS;
}
rc = SSL_CTX_use_PrivateKey(listener->ssl_ctx, pkey);
EVP_PKEY_free(pkey);
if(rc != 1){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to use private key from URI \"%s\".", listener->keyfile);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
#else
log__printf(NULL, MOSQ_LOG_ERR, "Error: 'uri' keyform requires OpenSSL 3.0 or later.");
return MOSQ_ERR_NOT_SUPPORTED;
#endif
}
rc = SSL_CTX_check_private_key(listener->ssl_ctx);
if(rc != 1){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Server certificate/key are inconsistent.");
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
/* Load CRLs if they exist. */
if(listener->crlfile){
rc = net__load_crl_file(listener);
if(rc){
return rc;
}
}
#else
UNUSED(listener);
#endif
return MOSQ_ERR_SUCCESS;
}
#if defined(WITH_TLS) && !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000
static int net__load_engine(struct mosquitto__listener *listener)
{
ENGINE *engine = NULL;
UI_METHOD *ui_method;
EVP_PKEY *pkey;
if(!listener->tls_engine){
return MOSQ_ERR_SUCCESS;
}
engine = ENGINE_by_id(listener->tls_engine);
if(!engine){
log__printf(NULL, MOSQ_LOG_ERR, "Error loading %s engine\n", listener->tls_engine);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
if(!ENGINE_init(engine)){
log__printf(NULL, MOSQ_LOG_ERR, "Failed engine initialisation\n");
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
ENGINE_set_default(engine, ENGINE_METHOD_ALL);
if(listener->tls_keyform == mosq_k_engine){
ui_method = net__get_ui_method();
if(listener->tls_engine_kpass_sha1){
if(!ENGINE_ctrl_cmd(engine, ENGINE_SECRET_MODE, ENGINE_SECRET_MODE_SHA, NULL, NULL, 0)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to set engine secret mode sha");
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
if(!ENGINE_ctrl_cmd(engine, ENGINE_PIN, 0, listener->tls_engine_kpass_sha1, NULL, 0)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to set engine pin");
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
ui_method = NULL;
}
pkey = ENGINE_load_private_key(engine, listener->keyfile, ui_method, NULL);
if(!pkey){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load engine private key file \"%s\".", listener->keyfile);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
if(SSL_CTX_use_PrivateKey(listener->ssl_ctx, pkey) <= 0){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to use engine private key file \"%s\".", listener->keyfile);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
}
ENGINE_free(engine); /* release the structural reference from ENGINE_by_id() */
return MOSQ_ERR_SUCCESS;
}
#endif
int net__tls_load_verify(struct mosquitto__listener *listener)
{
#ifdef WITH_TLS
int rc;
# if OPENSSL_VERSION_NUMBER < 0x30000000L
if(listener->cafile || listener->capath){
rc = SSL_CTX_load_verify_locations(listener->ssl_ctx, listener->cafile, listener->capath);
if(rc == 0){
if(listener->cafile && listener->capath){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load CA certificates. Check cafile \"%s\" and capath \"%s\".", listener->cafile, listener->capath);
}else if(listener->cafile){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load CA certificates. Check cafile \"%s\".", listener->cafile);
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load CA certificates. Check capath \"%s\".", listener->capath);
}
}
}
# else
STACK_OF(X509_NAME) *ca_names = NULL;
if(listener->cafile || listener->capath){
ca_names = sk_X509_NAME_new_null();
}
if(listener->cafile){
rc = SSL_CTX_load_verify_file(listener->ssl_ctx, listener->cafile);
if(rc == 0){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load CA certificates. Check cafile \"%s\".", listener->cafile);
net__print_ssl_error(NULL, NULL);
return MOSQ_ERR_TLS;
}
STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(listener->cafile);
if(cert_names){
for(int i=0; i<sk_X509_NAME_num(cert_names); i++){
sk_X509_NAME_push(ca_names, X509_NAME_dup(sk_X509_NAME_value(cert_names, i)));
}
sk_X509_NAME_pop_free(cert_names, X509_NAME_free);
}
}
if(listener->capath){
rc = SSL_CTX_load_verify_dir(listener->ssl_ctx, listener->capath);
if(rc == 0){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to load CA certificates. Check capath \"%s\".", listener->capath);
net__print_ssl_error(NULL, NULL);
sk_X509_NAME_pop_free(ca_names, X509_NAME_free);
return MOSQ_ERR_TLS;
}
SSL_add_dir_cert_subjects_to_stack(ca_names, listener->capath);
}
if(ca_names){
SSL_CTX_set_client_CA_list(listener->ssl_ctx, ca_names);
}
# endif
# if !defined(OPENSSL_NO_ENGINE) && OPENSSL_API_LEVEL < 30000
if(net__load_engine(listener)){
return MOSQ_ERR_TLS;
}
# endif
#endif
return net__load_certificates(listener);
}
#if !defined(WIN32) && !defined(_AIX)
static int net__bind_interface(struct mosquitto__listener *listener, struct addrinfo *rp)
{
/*
* This binds the listener sock to a network interface.
* The use of SO_BINDTODEVICE requires root access, which we don't have, so instead
* use getifaddrs to find the interface addresses, and use IP of the
* matching interface in the later bind().
*/
struct ifaddrs *ifaddr;
bool have_interface = false;
if(getifaddrs(&ifaddr) < 0){
net__print_error(MOSQ_LOG_ERR, "Error: %s");
return MOSQ_ERR_ERRNO;
}
for(struct ifaddrs *ifa=ifaddr; ifa!=NULL; ifa=ifa->ifa_next){
if(ifa->ifa_addr == NULL){
continue;
}
if(!strcasecmp(listener->bind_interface, ifa->ifa_name)){
have_interface = true;
if(ifa->ifa_addr->sa_family == rp->ai_addr->sa_family){
if(rp->ai_addr->sa_family == AF_INET){
if(listener->host &&
memcmp(&((struct sockaddr_in *)rp->ai_addr)->sin_addr,
&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
sizeof(struct in_addr))){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Interface address for %s does not match specified listener address (%s).",
listener->bind_interface, listener->host);
return MOSQ_ERR_INVAL;
}else{
memcpy(&((struct sockaddr_in *)rp->ai_addr)->sin_addr,
&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
sizeof(struct in_addr));
freeifaddrs(ifaddr);
return MOSQ_ERR_SUCCESS;
}
}else if(rp->ai_addr->sa_family == AF_INET6){
if(listener->host &&
memcmp(&((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr,
&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
sizeof(struct in6_addr))){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Interface address for %s does not match specified listener address (%s).",
listener->bind_interface, listener->host);
return MOSQ_ERR_INVAL;
}else{
memcpy(&((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr,
&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
sizeof(struct in6_addr));
((struct sockaddr_in6 *)rp->ai_addr)->sin6_scope_id = ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id;
freeifaddrs(ifaddr);
return MOSQ_ERR_SUCCESS;
}
}
}
}
}
freeifaddrs(ifaddr);
if(have_interface){
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Interface %s does not support %s configuration.",
listener->bind_interface, rp->ai_addr->sa_family == AF_INET ? "IPv4" : "IPv6");
return MOSQ_ERR_NOT_SUPPORTED;
}else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Interface %s does not exist.", listener->bind_interface);
return MOSQ_ERR_NOT_FOUND;
}
}
#endif
static int net__socket_listen_tcp(struct mosquitto__listener *listener)
{
mosq_sock_t sock = INVALID_SOCKET;
struct addrinfo hints;
struct addrinfo *ainfo, *rp;
char service[10];
int rc;
int ss_opt = 1;
#ifndef WIN32
bool interface_bound = false;
#endif
if(!listener){
return MOSQ_ERR_INVAL;
}
snprintf(service, 10, "%d", listener->port);
memset(&hints, 0, sizeof(struct addrinfo));
if(listener->socket_domain){
hints.ai_family = listener->socket_domain;
}else{
hints.ai_family = AF_UNSPEC;
}
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
rc = getaddrinfo(listener->host, service, &hints, &ainfo);
if(rc){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating listener: %s.", gai_strerror(rc));
return INVALID_SOCKET;
}
listener->sock_count = 0;
listener->socks = NULL;
for(rp = ainfo; rp; rp = rp->ai_next){
if(rp->ai_family == AF_INET){
log__printf(NULL, MOSQ_LOG_INFO, "Opening ipv4 listen socket on port %d.", ntohs(((struct sockaddr_in *)rp->ai_addr)->sin_port));
}else if(rp->ai_family == AF_INET6){
log__printf(NULL, MOSQ_LOG_INFO, "Opening ipv6 listen socket on port %d.", ntohs(((struct sockaddr_in6 *)rp->ai_addr)->sin6_port));
}else{
continue;
}
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock == INVALID_SOCKET){
net__print_error(MOSQ_LOG_WARNING, "Warning: %s");
continue;
}
listener->sock_count++;
listener->socks = mosquitto_realloc(listener->socks, sizeof(mosq_sock_t)*(size_t)listener->sock_count);
if(!listener->socks){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
freeaddrinfo(ainfo);
COMPAT_CLOSE(sock);
return MOSQ_ERR_NOMEM;
}
listener->socks[listener->sock_count-1] = sock;
#ifndef WIN32
ss_opt = 1;
/* Unimportant if this fails */
(void)setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &ss_opt, sizeof(ss_opt));
#endif
#ifdef IPV6_V6ONLY
ss_opt = 1;
(void)setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ss_opt, sizeof(ss_opt));
#endif
if(net__socket_nonblock(&sock)){
freeaddrinfo(ainfo);
mosquitto_FREE(listener->socks);
return 1;
}
#if !defined(WIN32) && !defined(_AIX)
if(listener->bind_interface){
/* It might be possible that an interface does not support all relevant sa_families.
* We should successfully find at least one. */
rc = net__bind_interface(listener, rp);
if(rc){
COMPAT_CLOSE(sock);
listener->sock_count--;
if(rc == MOSQ_ERR_NOT_FOUND || rc == MOSQ_ERR_INVAL){
freeaddrinfo(ainfo);
return rc;
}else{
continue;
}
}
interface_bound = true;
}
#endif
if(bind(sock, rp->ai_addr, rp->ai_addrlen) == -1){
#if defined(__linux__)
if(errno == EACCES){
log__printf(NULL, MOSQ_LOG_ERR, "If you are trying to bind to a privileged port (<1024), try using setcap and do not start the broker as root:");
log__printf(NULL, MOSQ_LOG_ERR, " sudo setcap 'CAP_NET_BIND_SERVICE=+ep /usr/sbin/mosquitto'");
}
#endif
net__print_error(MOSQ_LOG_ERR, "Error: %s");
COMPAT_CLOSE(sock);
freeaddrinfo(ainfo);
mosquitto_FREE(listener->socks);
return 1;
}
if(listen(sock, 100) == -1){
net__print_error(MOSQ_LOG_ERR, "Error: %s");
freeaddrinfo(ainfo);
COMPAT_CLOSE(sock);
mosquitto_FREE(listener->socks);
return 1;
}
}
freeaddrinfo(ainfo);
#ifndef WIN32
if(listener->bind_interface && !interface_bound){
mosquitto_FREE(listener->socks);
return 1;
}
#endif
return 0;
}
#ifdef WITH_UNIX_SOCKETS
static int net__socket_listen_unix(struct mosquitto__listener *listener)
{
struct sockaddr_un addr;
int sock;
int rc;
mode_t old_mask;
if(listener->unix_socket_path == NULL){
return MOSQ_ERR_INVAL;
}
if(strlen(listener->unix_socket_path) > sizeof(addr.sun_path)-1){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Path to unix socket is too long \"%s\".", listener->unix_socket_path);
return MOSQ_ERR_INVAL;
}