-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathmessage-table.c
More file actions
1630 lines (1349 loc) · 52 KB
/
Copy pathmessage-table.c
File metadata and controls
1630 lines (1349 loc) · 52 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
/* Pi-hole: A black hole for Internet advertisements
* (c) 2020 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Message table routines
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
#include "database/message-table.h"
#include "database/common.h"
// logging routines
#include "log.h"
// get_group_names()
#include "gravity-db.h"
// cli_mode
#include "args.h"
// main_pid()
#include "signals.h"
// struct config
#include "config/config.h"
// get_rate_limit_turnaround()
#include "gc.h"
// get_filesystem_details()
#include "files.h"
// get_memdb()
#include "database/query-table.h"
// escape_html()
#include "webserver/http-common.h"
// GIT_HASH, FTL_ARCH
#include "version.h"
// Number of arguments in a variadic macro
// Credit: https://stackoverflow.com/a/35693080/2087442
#define PP_NARG(...) \
PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) \
PP_128TH_ARG(__VA_ARGS__)
#define PP_128TH_ARG( \
_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,N,...) N
#define PP_RSEQ_N() \
127,126,125,124,123,122,121,120, \
119,118,117,116,115,114,113,112,111,110, \
109,108,107,106,105,104,103,102,101,100, \
99,98,97,96,95,94,93,92,91,90, \
89,88,87,86,85,84,83,82,81,80, \
79,78,77,76,75,74,73,72,71,70, \
69,68,67,66,65,64,63,62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
static const char *get_message_type_str(const enum message_type type)
{
switch(type)
{
case REGEX_MESSAGE:
return "REGEX";
case SUBNET_MESSAGE:
return "SUBNET";
case HOSTNAME_MESSAGE:
return "HOSTNAME";
case DNSMASQ_CONFIG_MESSAGE:
return "DNSMASQ_CONFIG";
case RATE_LIMIT_MESSAGE:
return "RATE_LIMIT";
case DNSMASQ_WARN_MESSAGE:
return "DNSMASQ_WARN";
case LOAD_MESSAGE:
return "LOAD";
case SHMEM_MESSAGE:
return "SHMEM";
case DISK_MESSAGE:
return "DISK";
case INACCESSIBLE_ADLIST_MESSAGE:
return "LIST";
case DISK_MESSAGE_EXTENDED:
return "DISK_EXTENDED";
case CERTIFICATE_DOMAIN_MISMATCH_MESSAGE:
return "CERTIFICATE_DOMAIN_MISMATCH";
case CONNECTION_ERROR_MESSAGE:
return "CONNECTION_ERROR";
case NTP_MESSAGE:
return "NTP";
case VERIFY_MESSAGE:
return "VERIFY";
case GRAVITY_RESTORED_MESSAGE:
return "GRAVITY_RESTORED";
case MAX_MESSAGE:
default:
return "UNKNOWN";
}
}
static enum message_type get_message_type_from_string(const char *typestr)
{
if (strcmp(typestr, "REGEX") == 0)
return REGEX_MESSAGE;
else if (strcmp(typestr, "SUBNET") == 0)
return SUBNET_MESSAGE;
else if (strcmp(typestr, "HOSTNAME") == 0)
return HOSTNAME_MESSAGE;
else if (strcmp(typestr, "DNSMASQ_CONFIG") == 0)
return DNSMASQ_CONFIG_MESSAGE;
else if (strcmp(typestr, "RATE_LIMIT") == 0)
return RATE_LIMIT_MESSAGE;
else if (strcmp(typestr, "DNSMASQ_WARN") == 0)
return DNSMASQ_WARN_MESSAGE;
else if (strcmp(typestr, "LOAD") == 0)
return LOAD_MESSAGE;
else if (strcmp(typestr, "SHMEM") == 0)
return SHMEM_MESSAGE;
else if (strcmp(typestr, "DISK") == 0)
return DISK_MESSAGE;
else if (strcmp(typestr, "LIST") == 0)
return INACCESSIBLE_ADLIST_MESSAGE;
else if (strcmp(typestr, "DISK_EXTENDED") == 0)
return DISK_MESSAGE_EXTENDED;
else if (strcmp(typestr, "CERTIFICATE_DOMAIN_MISMATCH") == 0)
return CERTIFICATE_DOMAIN_MISMATCH_MESSAGE;
else if (strcmp(typestr, "CONNECTION_ERROR") == 0)
return CONNECTION_ERROR_MESSAGE;
else if (strcmp(typestr, "NTP") == 0)
return NTP_MESSAGE;
else if (strcmp(typestr, "VERIFY") == 0)
return VERIFY_MESSAGE;
else if (strcmp(typestr, "GRAVITY_RESTORED") == 0)
return GRAVITY_RESTORED_MESSAGE;
else
return MAX_MESSAGE;
}
static unsigned char message_blob_types[MAX_MESSAGE][5] =
{
{ // REGEX_MESSAGE: The message column contains the regex text (the erroring regex filter itself)
SQLITE_TEXT, // regex type ("deny", "allow")
SQLITE_TEXT, // regex warning text
SQLITE_INTEGER, // database index of regex (so the dashboard can show a link)
SQLITE_NULL, // not used
SQLITE_NULL // not used
},
{ // SUBNET_MESSAGE: The message column contains the IP address of the client in question
SQLITE_INTEGER, // number of matching
SQLITE_TEXT, // comma-separated list of matching subnets (text representation)
SQLITE_TEXT, // comma-separated list of matching subnets (database IDs)
SQLITE_TEXT, // chosen subnet (text representation)
SQLITE_INTEGER // chosen subnet (database ID)
},
{ // HOSTNAME_MESSAGE: The message column contains the IP address of the device
SQLITE_TEXT, // Obtained host name
SQLITE_INTEGER, // Position of error in string
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL // not used
},
{ // DNSMASQ_CONFIG_MESSAGE: The message column contains the full message itself
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
{ // RATE_LIMIT_MESSAGE: The message column contains the IP address of the client in question
SQLITE_INTEGER, // Configured maximum number of queries
SQLITE_INTEGER, // Configured rate-limiting interval [seconds]
SQLITE_INTEGER, // Turnaround time [seconds]
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
{ // DNSMASQ_WARN_MESSAGE: The message column contains the full message itself
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
{ // LOAD_MESSAGE: The message column contains a general message
SQLITE_FLOAT, // 15min load average
SQLITE_INTEGER, // Number of cores
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
{ // SHMEM_MESSAGE: The message column contains the corresponding path
SQLITE_INTEGER, // Percentage currently used
SQLITE_TEXT, // Human-readable details about memory/disk usage
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
{ // DISK_MESSAGE: The message column contains the corresponding path
SQLITE_INTEGER, // Percentage currently used
SQLITE_TEXT, // Human-readable details about memory/disk usage
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
{ // INACCESSIBLE_ADLIST_MESSAGE: The message column contains the corresponding adlist URL
SQLITE_INTEGER, // database index of the adlist (so the dashboard can show a link)
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL // not used
},
{
// DISK_MESSAGE_EXTENDED: The message column contains the corresponding path
SQLITE_INTEGER, // Percentage currently used
SQLITE_TEXT, // Human-readable details about memory/disk usage
SQLITE_TEXT, // File system type
SQLITE_TEXT, // Directory mounted on
SQLITE_NULL // not used
},
{
// CERTIFICATE_DOMAIN_MISMATCH_MESSAGE: The message column contains the certificate file
SQLITE_TEXT, // domain
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL // not used
},
{
// CONNECTION_ERROR_MESSAGE: The message column contains the server address
SQLITE_TEXT, // reason
SQLITE_TEXT, // error message
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL // not used
},
{
// NTP: The message column contains the warning/error
SQLITE_TEXT, // level (warning/error)
SQLITE_TEXT, // component (server/client)
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL // not used
},
{
// VERIFY_MESSAGE: The message column contains the error
SQLITE_TEXT, // expected checksum
SQLITE_TEXT, // actual checksum
SQLITE_TEXT, // FTL commit hash
SQLITE_TEXT, // FTL architecture
SQLITE_NULL // not used
},
{
// GRAVITY_RESTORED_MESSAGE: The message column contains the status
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL, // not used
SQLITE_NULL // not used
}
};
// Create message table in the database
bool create_message_table(sqlite3 *db)
{
// Start transaction
SQL_bool(db, "BEGIN");
// The blob fields can hold arbitrary data. Their type is specified through the type.
SQL_bool(db, "CREATE TABLE message ( id INTEGER PRIMARY KEY AUTOINCREMENT, "
"timestamp INTEGER NOT NULL, "
"type TEXT NOT NULL, "
"message TEXT NOT NULL, "
"blob1 BLOB, "
"blob2 BLOB, "
"blob3 BLOB, "
"blob4 BLOB, "
"blob5 BLOB );");
// Update database version to 6
if(!db_set_FTL_property(db, DB_VERSION, 6))
{
log_err("create_message_table(): Failed to update database version!");
dbquery(db, "ROLLBACK");
return false;
}
// End transaction
SQL_bool(db, "END");
return true;
}
// Flush message table
bool flush_message_table(sqlite3 *memdb)
{
// Flush message table
SQL_bool(memdb, "DELETE FROM disk.message;");
return true;
}
static int _add_message(const enum message_type type,
const char *message, const size_t count, ...);
#define add_message(type, message, ...) _add_message(type, message, PP_NARG(__VA_ARGS__), __VA_ARGS__)
#define add_message_no_args(type, message) _add_message(type, message, 0)
static int _add_message(const enum message_type type,
const char *message, const size_t count,...)
{
// Log to database only if not in CLI mode
if(cli_mode)
return -1;
int rowid = -1;
// Return early if database is known to be broken
if(FTLDBerror())
return -1;
// Check if message type is known
if(type >= MAX_MESSAGE)
{
log_err("add_message(type=%u, message=%s) - Invalid message type with %zu arguments",
type, message, count);
return -1;
}
// Check if number of arguments is valid
// Total number of arguments
if(count > 5)
{
log_err("add_message(type=%u, message=%s) - Too many arguments (%zu), expected at most 5",
type, message, count);
return -1;
}
// No arguments check
if(count == 0 && message_blob_types[type][0] != SQLITE_NULL)
{
log_err("add_message(type=%u, message=%s) - Invalid number of arguments: No arguments passed for message type requiring arguments",
type, message);
return -1;
}
// Non-zero arguments check
else if(count > 1 && message_blob_types[type][count - 2] == SQLITE_NULL)
{
log_err("add_message(type=%u, message=%s) - Invalid number of arguments: Too many (%zu) arguments passed for this message type",
type, message, count);
return -1;
}
sqlite3 *db = dbopen(false, false);
// Open database connection
if(db == NULL)
// Reason for failure is logged in dbopen()
return -1;
// Ensure there are no duplicates when adding messages
sqlite3_stmt* stmt = NULL;
const char *querystr = "DELETE FROM message WHERE type = ?1 AND message = ?2";
int rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK ){
log_err("add_message(type=%u, message=%s) - SQL error prepare DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
// Bind type to prepared statement
if((rc = sqlite3_bind_text(stmt, 1, get_message_type_str(type), -1, SQLITE_STATIC)) != SQLITE_OK)
{
log_err("add_message(type=%u, message=%s) - Failed to bind type DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
// Bind message to prepared statement
if((rc = sqlite3_bind_text(stmt, 2, message, -1, SQLITE_STATIC)) != SQLITE_OK)
{
log_err("add_message(type=%u, message=%s) - Failed to bind message DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
// Execute and finalize. sqlite3_step() returns SQLITE_DONE for
// non-SELECT statements (whether or not any rows were deleted).
if((rc = sqlite3_step(stmt)) != SQLITE_DONE)
{
log_err("add_message(type=%u, message=%s) - SQL error step DELETE: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
sqlite3_finalize(stmt);
stmt = NULL;
// Prepare SQLite statement
querystr = "INSERT INTO message (timestamp,type,message,blob1,blob2,blob3,blob4,blob5) "
"VALUES ((cast(strftime('%s', 'now') as int)),?,?,?,?,?,?,?);";
rc = sqlite3_prepare_v2(db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
log_err("add_message(type=%u, message=%s) - SQL error prepare: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
// Bind type to prepared statement
if((rc = sqlite3_bind_text(stmt, 1, get_message_type_str(type), -1, SQLITE_STATIC)) != SQLITE_OK)
{
log_err("add_message(type=%u, message=%s) - Failed to bind type: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
// Bind message to prepared statement
if((rc = sqlite3_bind_text(stmt, 2, message, -1, SQLITE_STATIC)) != SQLITE_OK)
{
log_err("add_message(type=%u, message=%s) - Failed to bind message: %s",
type, message, sqlite3_errstr(rc));
goto end_of_add_message;
}
va_list ap;
va_start(ap, count);
for (size_t j = 0; j < count; j++)
{
const unsigned char datatype = message_blob_types[type][j];
switch (datatype)
{
case SQLITE_INTEGER:
rc = sqlite3_bind_int(stmt, 3 + j, va_arg(ap, int));
break;
case SQLITE_FLOAT:
rc = sqlite3_bind_double(stmt, 3 + j, va_arg(ap, double));
break;
case SQLITE_TEXT:
rc = sqlite3_bind_text(stmt, 3 + j, va_arg(ap, char*), -1, SQLITE_STATIC);
break;
case SQLITE_NULL: /* Fall through */
default:
log_warn("add_message(type=%s, message=%s) - Excess property, binding NULL",
get_message_type_str(type), message);
rc = sqlite3_bind_null(stmt, 3 + j);
break;
}
// Bind message to prepared statement
if(rc != SQLITE_OK)
{
log_err("add_message(type=%u, message=%s) - Failed to bind argument %zu (type %u): %s",
type, message, 3 + j, datatype, sqlite3_errstr(rc));
sqlite3_finalize(stmt);
stmt = NULL;
checkFTLDBrc(rc);
va_end(ap);
goto end_of_add_message;
}
}
va_end(ap);
// Step and check if successful
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE)
{
log_err("Encountered error while trying to store message in long-term database: %s", sqlite3_errstr(rc));
checkFTLDBrc(rc);
goto end_of_add_message;
}
// Get row ID of the newly added message (only on success)
rowid = sqlite3_last_insert_rowid(db);
end_of_add_message: // Close database connection
// Final database handling
if(stmt != NULL)
sqlite3_finalize(stmt);
dbclose(&db);
return rowid;
}
bool delete_message(cJSON *ids, int *deleted)
{
// Return early if database is known to be broken
if(FTLDBerror())
return false;
sqlite3 *db;
// Open database connection
if((db = dbopen(false, false)) == NULL)
{
log_err("delete_message() - Failed to open DB");
return false;
}
sqlite3_stmt *res = NULL;
if(sqlite3_prepare_v2(db, "DELETE FROM message WHERE id = ?;", -1, &res, 0) != SQLITE_OK)
{
log_err("SQL error (%i): %s", sqlite3_errcode(db), sqlite3_errmsg(db));
dbclose(&db);
return false;
}
// Loop over id in ids array
bool success = true;
cJSON *id = NULL;
cJSON_ArrayForEach(id, ids)
{
// Bind id to prepared statement
const int idval = cJSON_GetNumberValue(id);
if(sqlite3_bind_int(res, 1, idval) != SQLITE_OK)
{
log_err("delete_message() - Failed to bind id %d: %s", idval, sqlite3_errmsg(db));
success = false;
break;
}
// Execute and finalize
if(sqlite3_step(res) != SQLITE_DONE)
{
log_err("SQL error (%i): %s", sqlite3_errcode(db), sqlite3_errmsg(db));
success = false;
break;
}
// Add to deleted count
*deleted += sqlite3_changes(db);
sqlite3_reset(res);
}
sqlite3_finalize(res);
// Close database connection
dbclose(&db);
return success;
}
static void format_regex_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const char *type, const char *regex, const char *warning, const int dbindex)
{
if(snprintf(plain, sizeof_plain, "Invalid regex %s filter \"%s\": %s",
type, regex, warning) > sizeof_plain)
log_warn("format_regex_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_regex = escape_html(regex);
char *escaped_warning = escape_html(warning);
// Return early if memory allocation failed
if(escaped_regex == NULL || escaped_warning == NULL)
{
if(escaped_regex != NULL)
free(escaped_regex);
if(escaped_warning != NULL)
free(escaped_warning);
return;
}
if(snprintf(html, sizeof_html, "Encountered an error when processing <a href=\"groups-domains.lp?domainid=%d\">regex %s filter with ID %d</a>: <pre>%s</pre>Error message: <pre>%s</pre>",
dbindex, type, dbindex, escaped_regex, escaped_warning) > sizeof_html)
log_warn("format_regex_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_regex);
free(escaped_warning);
}
static void format_subnet_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const char *ip, const int matching_count, const char *names, const char *matching_ids, const char *chosen_match_text, const int chosen_match_id)
{
if(snprintf(plain, sizeof_plain, "Client %s is managed by %i groups (IDs %s), all describing the same subnet. "
"FTL chose the most recent entry %s (ID %i) to obtain the group configuration for this client.",
ip, matching_count, matching_ids,
chosen_match_text, chosen_match_id) > sizeof_plain)
log_warn("format_subnet_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_ip = escape_html(ip);
char *escaped_ids = escape_html(matching_ids);
char *escaped_names = escape_html(names);
// Return early if memory allocation failed
if(escaped_ip == NULL || escaped_ids == NULL || escaped_names == NULL)
{
if(escaped_ip != NULL)
free(escaped_ip);
if(escaped_ids != NULL)
free(escaped_ids);
if(escaped_names != NULL)
free(escaped_names);
return;
}
if(snprintf(html, sizeof_html, "Client <code>%s</code> is managed by %i groups (IDs [%s]), all describing the same subnet:<pre>%s</pre>"
"FTL chose the most recent entry (ID %i) to obtain the group configuration for this client.",
escaped_ip, matching_count, escaped_ids, escaped_names, chosen_match_id) > sizeof_html)
log_warn("format_subnet_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_ip);
free(escaped_ids);
free(escaped_names);
}
static void format_hostname_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const char *ip, const char *name, const int pos)
{
// Format the plain text message (the JSON string is already escaped and
// contains "" around the string)
if(snprintf(plain, sizeof_plain, "Host name of client \"%s\" => %s contains (at least) one invalid character at position %i",
ip, name, pos) > sizeof_plain)
log_warn("format_hostname_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_ip = escape_html(ip);
char *escaped_name = escape_html(name);
// Return early if memory allocation failed
if(escaped_ip == NULL || escaped_name == NULL)
{
if(escaped_ip != NULL)
free(escaped_ip);
if(escaped_name != NULL)
free(escaped_name);
return;
}
// Only read name[pos] if pos is actually within the string, otherwise
// a stored/caller-supplied pos unrelated to the name length would cause
// an out-of-bounds read.
const unsigned char badchar = (pos >= 0 && (size_t)pos < strlen(name)) ? (unsigned char)name[pos] : 0;
if(snprintf(html, sizeof_html, "Host name of client <code>%s</code> => <code>%s</code> contains (at least) one invalid character (hex %02x) at position %i",
escaped_ip, escaped_name, badchar, pos) > sizeof_html)
log_warn("format_hostname_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_ip);
free(escaped_name);
}
static void format_dnsmasq_config_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const char *message)
{
if(snprintf(plain, sizeof_plain, "Error in dnsmasq configuration: %s", message) > sizeof_plain)
log_warn("format_dnsmasq_config_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_message = escape_html(message);
// Return early if memory allocation failed
if(escaped_message == NULL)
return;
if(snprintf(html, sizeof_html, "FTL failed to start due to %s.", escaped_message) > sizeof_html)
log_warn("format_dnsmasq_config_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_message);
}
static void format_rate_limit_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const char *clientIP, const int count, const int interval, const int turnaround)
{
if(snprintf(plain, sizeof_plain, "Rate-limiting %s for at least %d second%s",
clientIP, turnaround, turnaround == 1 ? "" : "s") > sizeof_plain)
log_warn("format_rate_limit_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_clientIP = escape_html(clientIP);
// Return early if memory allocation failed
if(escaped_clientIP == NULL)
return;
if(snprintf(html, sizeof_html, "Client <code>%s</code> has been rate-limited for at least %d second%s (current limit: %d queries per %d seconds)",
escaped_clientIP, turnaround, turnaround == 1 ? "" : "s", count, interval) > sizeof_html)
log_warn("format_rate_limit_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_clientIP);
}
static void format_dnsmasq_warn_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const char *message)
{
if(snprintf(plain, sizeof_plain, "dnsmasq: %s", message) > sizeof_plain)
log_warn("format_dnsmasq_warn_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
// Escape the warning text before embedding it into HTML, matching the
// other renderers - the dnsmasq message is untrusted and would
// otherwise allow stored HTML/script injection in the admin interface
char *escaped_message = escape_html(message);
if(escaped_message == NULL)
return;
if(snprintf(html, sizeof_html, "<code>dnsmasq</code> warning:<pre>%s</pre>Check out <a href=\"https://docs.pi-hole.net/ftldns/dnsmasq_warn/\" target=\"_blank\">our documentation</a> for further information.", escaped_message) > sizeof_html)
log_warn("format_dnsmasq_warn_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_message);
}
static void format_load_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const double load, const int nprocs)
{
if(snprintf(plain, sizeof_plain, "Long-term load (15min avg) larger than number of processors: %.1f > %d",
load, nprocs) > sizeof_plain)
log_warn("format_load_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
if(snprintf(html, sizeof_html, "Long-term load (15min avg) larger than number of processors: <strong>%.1f > %d</strong><br>This may slow down DNS resolution and can cause bottlenecks.",
load, nprocs) > sizeof_html)
log_warn("format_load_message(): Buffer too small to hold HTML message, warning truncated");
}
static void format_shmem_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html, const char *path, int shmem, const char *msg)
{
if(snprintf(plain, sizeof_plain, "Shared memory shortage (%s) ahead: %d%% is used (%s)",
path, shmem, msg) > sizeof_plain)
log_warn("format_messages(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_path = escape_html(path);
char *escaped_msg = escape_html(msg);
// Return early if memory allocation failed
if(escaped_path == NULL || escaped_msg == NULL)
{
if(escaped_path != NULL)
free(escaped_path);
if(escaped_msg != NULL)
free(escaped_msg);
return;
}
if(snprintf(html, sizeof_html, "Shared memory shortage (<code>%s</code>) ahead: <strong>%d%%</strong> is used<br>%s",
escaped_path, shmem, escaped_msg) > sizeof_html)
log_warn("log_resource_shortage(): Buffer too small to hold HTML message, warning truncated");
free(escaped_path);
free(escaped_msg);
}
static void format_disk_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const char *path, const int disk, const char *msg)
{
if(snprintf(plain, sizeof_plain, "Disk shortage ahead: %d%% is used (%s) on partition containing the file %s",
disk, msg, path) > sizeof_plain)
log_warn("format_disk_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_path = escape_html(path);
char *escaped_msg = escape_html(msg);
// Return early if memory allocation failed
if(escaped_path == NULL || escaped_msg == NULL)
{
if(escaped_path != NULL)
free(escaped_path);
if(escaped_msg != NULL)
free(escaped_msg);
return;
}
if(snprintf(html, sizeof_html, "Disk shortage ahead: <strong>%d%%</strong> is used (%s) on partition containing the file <code>%s</code>",
disk, escaped_msg, escaped_path) > sizeof_html)
log_warn("format_disk_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_path);
free(escaped_msg);
}
static void format_disk_message_extended(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const int disk, const char *msg, const char *mnt_type, const char *mnt_dir)
{
if(snprintf(plain, sizeof_plain, "Disk shortage ahead: %d%% is used (%s) on %s filesystem mounted at %s",
disk, msg, mnt_type, mnt_dir) > sizeof_plain)
log_warn("format_disk_message_extended(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_mnt_type = escape_html(mnt_type);
char *escaped_mnt_dir = escape_html(mnt_dir);
char *escaped_msg = escape_html(msg);
// Return early if memory allocation failed
if(escaped_mnt_type == NULL || escaped_mnt_dir == NULL || escaped_msg == NULL)
{
if(escaped_mnt_type != NULL)
free(escaped_mnt_type);
if(escaped_mnt_dir != NULL)
free(escaped_mnt_dir);
if(escaped_msg != NULL)
free(escaped_msg);
return;
}
if(snprintf(html, sizeof_html, "Disk shortage ahead: <strong>%d%%</strong> is used (%s) on %s filesystem mounted at <code>%s</code>",
disk, escaped_msg, escaped_mnt_type, escaped_mnt_dir) > sizeof_html)
log_warn("format_disk_message_extended(): Buffer too small to hold HTML message, warning truncated");
free(escaped_mnt_type);
free(escaped_mnt_dir);
free(escaped_msg);
}
static void format_inaccessible_adlist_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const char *address, int dbindex)
{
if(snprintf(plain, sizeof_plain, "List with ID %d (%s) was inaccessible during last gravity run",
dbindex, address) > sizeof_plain)
log_warn("format_inaccessible_adlist_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_address = escape_html(address);
// Return early if memory allocation failed
if(escaped_address == NULL)
return;
if(snprintf(html, sizeof_html, "<a href=\"groups/lists?listid=%i\">List with ID <strong>%d</strong> (<code>%s</code>)</a> was inaccessible during last gravity run",
dbindex, dbindex, escaped_address) > sizeof_html)
log_warn("format_inaccessible_adlist_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_address);
}
static void format_certificate_domain_mismatch(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const char *certfile, const char*domain)
{
if(snprintf(plain, sizeof_plain, "SSL/TLS certificate %s does not match domain %s!", certfile, domain) > sizeof_plain)
log_warn("format_certificate_domain_mismatch(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_certfile = escape_html(certfile);
char *escaped_domain = escape_html(domain);
// Return early if memory allocation failed
if(escaped_certfile == NULL || escaped_domain == NULL)
{
if(escaped_certfile != NULL)
free(escaped_certfile);
if(escaped_domain != NULL)
free(escaped_domain);
return;
}
if(snprintf(html, sizeof_html, "SSL/TLS certificate %s does not match domain <strong>%s</strong>!", escaped_certfile, escaped_domain) > sizeof_html)
log_warn("format_certificate_domain_mismatch(): Buffer too small to hold HTML message, warning truncated");
free(escaped_certfile);
free(escaped_domain);
}
static void format_connection_error(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const char *server, const char *reason, const char *error)
{
if(snprintf(plain, sizeof_plain, "Connection error (%s): %s (%s)", server, reason, error) > sizeof_plain)
log_warn("format_connection_error(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_reason = escape_html(reason);
char *escaped_error = escape_html(error);
char *escaped_server = escape_html(server);
// Return early if memory allocation failed
if(escaped_reason == NULL || escaped_error == NULL || escaped_server == NULL)
{
if(escaped_reason != NULL)
free(escaped_reason);
if(escaped_error != NULL)
free(escaped_error);
if(escaped_server != NULL)
free(escaped_server);
return;
}
if(snprintf(html, sizeof_html, "Connection error (<strong>%s</strong>): %s (<strong>%s</strong>)", escaped_server, escaped_reason, escaped_error) > sizeof_html)
log_warn("format_connection_error(): Buffer too small to hold HTML message, warning truncated");
free(escaped_reason);
free(escaped_error);
free(escaped_server);
}
static void format_ntp_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const char *message, const char *level, const char *who)
{
if(snprintf(plain, sizeof_plain, "%s NTP %s: %s", level, who, message) > sizeof_plain)
log_warn("format_ntp_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_level = escape_html(level);
char *escaped_who = escape_html(who);
char *escaped_message = escape_html(message);
// Return early if memory allocation failed
if(escaped_level == NULL || escaped_who == NULL || escaped_message == NULL)
{
if(escaped_level != NULL)
free(escaped_level);
if(escaped_who != NULL)
free(escaped_who);
if(escaped_message != NULL)
free(escaped_message);
return;
}
if(snprintf(html, sizeof_html, "%s in NTP %s:<pre>%s</pre>", escaped_level, escaped_who, escaped_message) > sizeof_html)
log_warn("format_ntp_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_level);
free(escaped_who);
free(escaped_message);
}
static void format_verify_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const char *message, const char *expected, const char *actual,
const char *commit, const char *arch)
{
if(snprintf(plain, sizeof_plain, "%s - expected \"%s\", but got \"%s\" - FTL commit is %s on %s",
message, expected, actual, commit, arch) > sizeof_plain)
log_warn("format_verify_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required
if(sizeof_html < 1 || html == NULL)
return;
char *escaped_message = escape_html(message);
char *escaped_expected = escape_html(expected);
char *escaped_actual = escape_html(actual);
char *escaped_commit = escape_html(commit);
char *escaped_arch = escape_html(arch);
// Return early if memory allocation failed
if(escaped_message == NULL || escaped_expected == NULL || escaped_actual == NULL || escaped_commit == NULL || escaped_arch == NULL)
return;
if(snprintf(html, sizeof_html, "%s<br>Expected: <pre>%s</pre><br>Actual: <pre>%s</pre><br>FTL commit is <code>%s</code> on <code>%s</code>",
escaped_message, escaped_expected, escaped_actual, escaped_commit, escaped_arch) > sizeof_html)
log_warn("format_verify_message(): Buffer too small to hold HTML message, warning truncated");
free(escaped_message);
free(escaped_expected);
free(escaped_actual);
free(escaped_commit);
free(escaped_arch);
}
static void format_gravity_restored_message(char *plain, const int sizeof_plain, char *html, const int sizeof_html,
const char *status)
{
const bool failed = strcmp(status, "failed") == 0;
if(snprintf(plain, sizeof_plain, "Gravity database restore %s", failed ? "failed" : "successful") > sizeof_plain)
log_warn("format_gravity_restored_message(): Buffer too small to hold plain message, warning truncated");
// Return early if HTML text is not required