-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
queries.c
1039 lines (953 loc) · 32.7 KB
/
queries.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
/* Pi-hole: A black hole for Internet advertisements
* (c) 2021 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* API Implementation
*
* 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 "webserver/http-common.h"
#include "webserver/json_macros.h"
#include "api/api.h"
#include "datastructure.h"
// config struct
#include "config/config.h"
// get_aliasclient_list()
#include "database/aliasclients.h"
// get_memdb()
#include "database/query-table.h"
// dbopen(false, ), dbclose()
#include "database/common.h"
static int add_strings_to_array(struct ftl_conn *api, cJSON *array, const char *querystr, const int max_count)
{
sqlite3 *memdb = get_memdb();
if(memdb == NULL)
{
return send_json_error(api, 500, // 500 Internal error
"database_error",
"Could not read from in-memory database",
NULL);
}
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(memdb, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
return send_json_error(api, 500, // 500 Internal error
"database_error",
"Could not prepare in-memory database",
sqlite3_errstr(rc));
}
// Loop through returned rows
int counter = 0;
while((rc = sqlite3_step(stmt)) == SQLITE_ROW &&
(max_count < 0 || ++counter < max_count))
JSON_COPY_STR_TO_ARRAY(array, (const char*)sqlite3_column_text(stmt, 0));
// Acceptable return codes are either
// - SQLITE_DONE: We read all lines, or
// - SQLITE_ROW: We ended reading early because of set limit
if( rc != SQLITE_DONE && rc != SQLITE_ROW )
{
sqlite3_finalize(stmt);
return send_json_error(api, 500, // 500 Internal error
"database_error",
"Could not step in-memory database",
sqlite3_errstr(rc));
}
// Finalize SQLite3 statement
sqlite3_finalize(stmt);
return 0;
}
int api_queries_suggestions(struct ftl_conn *api)
{
int rc;
// Does the user request a custom number of records to be included?
int count = 30;
get_int_var(api->request->query_string, "count", &count);
// Get domains
cJSON *domain = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, domain, "SELECT domain FROM domain_by_id", count);
if(rc != 0)
{
log_err("Cannot read domains from database");
cJSON_Delete(domain);
return rc;
}
// Get clients, both by IP and names
// We have to call DISTINCT() here as multiple IPs can map to and name and
// vice versa
cJSON *client_ip = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, client_ip, "SELECT DISTINCT(ip) FROM client_by_id", count);
if(rc != 0)
{
log_err("Cannot read client IPs from database");
cJSON_Delete(domain);
cJSON_Delete(client_ip);
return rc;
}
cJSON *client_name = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, client_name, "SELECT DISTINCT(name) FROM client_by_id", count);
if(rc != 0)
{
log_err("Cannot read client names from database");
cJSON_Delete(domain);
cJSON_Delete(client_ip);
cJSON_Delete(client_name);
return rc;
}
// Get upstreams
cJSON *upstream = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, upstream, "SELECT forward FROM forward_by_id", count);
if(rc != 0)
{
log_err("Cannot read forward from database");
cJSON_Delete(domain);
cJSON_Delete(client_ip);
cJSON_Delete(client_name);
cJSON_Delete(upstream);
return rc;
}
// Get types
cJSON *type = JSON_NEW_ARRAY();
queriesData query = { 0 };
for(enum query_type t = TYPE_A; t < TYPE_MAX; t++)
{
query.type = t;
const char *string = get_query_type_str(t, &query, NULL);
JSON_REF_STR_IN_ARRAY(type, string);
}
// Get status
cJSON *status = JSON_NEW_ARRAY();
for(enum query_status s = QUERY_UNKNOWN; s <QUERY_STATUS_MAX; s++)
{
query.status = s;
const char *string = get_query_status_str(query.status);
JSON_REF_STR_IN_ARRAY(status, string);
}
// Get reply types
cJSON *reply = JSON_NEW_ARRAY();
for(enum reply_type r = REPLY_UNKNOWN; r <QUERY_REPLY_MAX; r++)
{
query.reply = r;
const char *string = get_query_reply_str(query.reply);
JSON_REF_STR_IN_ARRAY(reply, string);
}
// Get dnssec status
cJSON *dnssec = JSON_NEW_ARRAY();
for(enum dnssec_status d = DNSSEC_UNKNOWN; d < DNSSEC_MAX; d++)
{
query.dnssec = d;
const char *string = get_query_dnssec_str(query.dnssec);
JSON_REF_STR_IN_ARRAY(dnssec, string);
}
cJSON *suggestions = JSON_NEW_OBJECT();
JSON_ADD_ITEM_TO_OBJECT(suggestions, "domain", domain);
JSON_ADD_ITEM_TO_OBJECT(suggestions, "client_ip", client_ip);
JSON_ADD_ITEM_TO_OBJECT(suggestions, "client_name", client_name);
JSON_ADD_ITEM_TO_OBJECT(suggestions, "upstream", upstream);
JSON_ADD_ITEM_TO_OBJECT(suggestions, "type", type);
JSON_ADD_ITEM_TO_OBJECT(suggestions, "status", status);
JSON_ADD_ITEM_TO_OBJECT(suggestions, "reply", reply);
JSON_ADD_ITEM_TO_OBJECT(suggestions, "dnssec", dnssec);
cJSON *json = JSON_NEW_OBJECT();
JSON_ADD_ITEM_TO_OBJECT(json, "suggestions", suggestions);
JSON_SEND_OBJECT(json);
}
#define QUERYSTR "SELECT q.id,timestamp,q.type,status,d.domain,f.forward,additional_info,reply_type,reply_time,dnssec,c.ip,c.name,a.content,regex_id"
// JOIN: Only return rows where there is a match in BOTH tables
// LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
#define JOINSTR "JOIN client_by_id c ON q.client = c.id JOIN domain_by_id d ON q.domain = d.id LEFT JOIN forward_by_id f ON q.forward = f.id LEFT JOIN addinfo_by_id a ON a.id = q.additional_info"
#define QUERYSTRBUFFERLEN 4096
static void add_querystr_string(struct ftl_conn *api, char *querystr, const char *sql, const char *val, bool *where)
{
const size_t strpos = strlen(querystr);
const char *glue = *where ? "AND" : "WHERE";
*where = true;
snprintf(querystr + strpos, QUERYSTRBUFFERLEN - strpos, " %s (%s%s)", glue, sql, val);
}
static void querystr_finish(char *querystr, const char *sort_col, const char *sort_dir)
{
const char *sort_col_sql = NULL;
const char *sort_dir_sql = NULL;
if(sort_col[0] != '\0' && sort_dir[0] != '\0')
{
// Try to parse the sort column ...
if(strcasecmp(sort_col, "time") == 0)
sort_col_sql = "timestamp";
else if(strcasecmp(sort_col, "domain") == 0)
sort_col_sql = "d.domain";
else if(strcasecmp(sort_col, "client.ip") == 0)
sort_col_sql = "c.ip";
else if(strcasecmp(sort_col, "client.name") == 0)
sort_col_sql = "c.name";
else if(strcasecmp(sort_col, "upstream") == 0)
sort_col_sql = "f.forward";
else if(strcasecmp(sort_col, "type") == 0)
sort_col_sql = "q.type";
else if(strcasecmp(sort_col, "status") == 0)
sort_col_sql = "q.status";
else if(strcasecmp(sort_col, "reply") == 0)
sort_col_sql = "q.reply_type";
else if(strcasecmp(sort_col, "reply.time") == 0)
sort_col_sql = "q.reply_time";
else if(strcasecmp(sort_col, "dnssec") == 0)
sort_col_sql = "q.dnssec";
else if(strcasecmp(sort_col, "regex_id") == 0)
sort_col_sql = "regex_id";
// ... and the sort direction
if(strcasecmp(sort_dir, "asc") == 0 || strcasecmp(sort_dir, "ascending") == 0)
sort_dir_sql = "ASC";
else if(strcasecmp(sort_dir, "desc") == 0 || strcasecmp(sort_dir, "descending") == 0)
sort_dir_sql = "DESC";
}
const size_t strpos = strlen(querystr);
if(sort_col_sql == NULL || sort_dir_sql == NULL)
{
// Default sorting: Most recent query first, sorting by ID (which is
// the same as timestamp but faster)
sort_col_sql = "q.id";
sort_dir_sql = "DESC";
}
snprintf(querystr + strpos, QUERYSTRBUFFERLEN - strpos, " ORDER BY %s %s",
sort_col_sql, sort_dir_sql);
}
// This function modifies the passed string inline
static bool is_wildcard(char *string)
{
// Check if wildcards are requested
bool wildcard = false;
const size_t stringlen = strlen(string);
for(unsigned int i = 0u; i < stringlen; i++)
{
if(string[i] == '*')
{
// Replace "*" by SQLite3 wildcard character "%" and
// memorize we actually want to do wildcard matching
string[i] = '%';
wildcard = true;
}
}
return wildcard;
}
int api_queries(struct ftl_conn *api)
{
// Exit before processing any data if requested via config setting
if(config.misc.privacylevel.v.privacy_level >= PRIVACY_MAXIMUM)
{
// Minimum structure is
// {"queries":[], "cursor": null}
cJSON *json = JSON_NEW_OBJECT();
cJSON *queries = JSON_NEW_ARRAY();
JSON_ADD_ITEM_TO_OBJECT(json, "queries", queries);
// There are no more queries available, send NULL cursor
JSON_ADD_NULL_TO_OBJECT(json, "cursor");
JSON_SEND_OBJECT(json);
}
// On-disk database lookup requested?
bool disk = false;
if(api->request->query_string != NULL)
get_bool_var(api->request->query_string, "disk", &disk);
// Start building database query string
char querystr[QUERYSTRBUFFERLEN] = { 0 };
snprintf(querystr, QUERYSTRBUFFERLEN, "%s FROM %s q %s", QUERYSTR, disk ? "disk.query_storage" : "query_storage", JOINSTR);
int draw = 0;
char domainname[512] = { 0 };
char clientip[512] = { 0 };
char clientname[512] = { 0 };
char upstreamname[256] = { 0 };
char typename[32] = { 0 };
char statusname[32] = { 0 };
char replyname[32] = { 0 };
char dnssecname[32] = { 0 };
char sort_dir[16] = { 0 };
char sort_col[16] = { 0 };
// We start with the most recent query at the beginning (until the cursor is changed)
unsigned long cursor, largest_db_index, mem_dbnum, disk_dbnum;
db_counts(&largest_db_index, &mem_dbnum, &disk_dbnum);
cursor = largest_db_index;
// We send 100 queries (unless the API is asked for a different limit)
int length = 100;
unsigned int start = 0;
bool cursor_set = false, where = false;
double timestamp_from = 0.0, timestamp_until = 0.0;
// Filter-/sorting based on GET parameters?
if(api->request->query_string != NULL)
{
// Time filtering FROM (inclusive)
if(get_double_var(api->request->query_string, "from", ×tamp_from))
add_querystr_string(api, querystr, "timestamp>=", ":tsfrom", &where);
// Time filtering UNTIL (exclusive)
if(get_double_var(api->request->query_string, "until", ×tamp_until))
add_querystr_string(api, querystr, "timestamp<", ":tsuntil", &where);
// Domain filtering?
if(GET_STR("domain", domainname, api->request->query_string) > 0)
{
if(is_wildcard(domainname))
add_querystr_string(api, querystr, "d.domain LIKE", ":domain", &where);
else
add_querystr_string(api, querystr, "d.domain=", ":domain", &where);
}
// Upstream filtering?
if(GET_STR("upstream", upstreamname, api->request->query_string) > 0)
{
if(strcmp(upstreamname, "blocklist") == 0)
// Pseudo-upstream for blocked queries
add_querystr_string(api, querystr, "q.status IN ", get_blocked_statuslist(), &where);
else if(strcmp(upstreamname, "cache") == 0)
// Pseudo-upstream for cached queries
add_querystr_string(api, querystr, "q.status IN ", get_cached_statuslist(), &where);
else
{
if(is_wildcard(upstreamname))
add_querystr_string(api, querystr, "f.forward LIKE", ":upstream", &where);
else
add_querystr_string(api, querystr, "f.forward=", ":upstream", &where);
}
}
// Client IP filtering?
if(GET_STR("client_ip", clientip, api->request->query_string) > 0)
{
if(is_wildcard(clientip))
add_querystr_string(api, querystr, "c.ip LIKE", ":cip", &where);
else
add_querystr_string(api, querystr, "c.ip=", ":cip", &where);
}
// Client filtering?
if(GET_STR("client_name", clientname, api->request->query_string) > 0)
{
if(is_wildcard(clientname))
add_querystr_string(api, querystr, "c.name LIKE", ":cname", &where);
else
add_querystr_string(api, querystr, "c.name=", ":cname", &where);
}
// DataTables server-side processing protocol
// Draw counter: This is used by DataTables to ensure that the
// Ajax returns from server-side processing
// requests are drawn in sequence by DataTables
// (Ajax requests are asynchronous and thus can
// return out of sequence).
get_int_var(api->request->query_string, "draw", &draw);
// Does the user request a non-default number of replies?
// Note: We do not accept zero query requests here
get_int_var(api->request->query_string, "length", &length);
// Does the user request an offset from the cursor?
get_uint_var(api->request->query_string, "start", &start);
uint64_t unum = 0u;
const char *msg = NULL;
if(get_uint64_var_msg(api->request->query_string, "cursor", &unum, &msg) ||
msg != NULL)
{
// Do not start at the most recent, but at an older
// query (so new queries do not show up suddenly in the
// log and shift pages)
if(unum <= largest_db_index && msg == NULL)
{
cursor = unum;
cursor_set = true;
add_querystr_string(api, querystr, "q.id<=", ":cursor", &where);
}
else
{
if(msg == NULL)
msg = "Cursor larger than largest database index";
// Cursors larger than the current known number
// of queries are invalid
return send_json_error(api, 400,
"bad_request",
"Requested cursor is invalid",
msg);
}
}
// Query type filtering?
if(GET_STR("type", typename, api->request->query_string) > 0)
add_querystr_string(api, querystr, "q.type=", ":type", &where);
// Query status filtering?
if(GET_STR("status", statusname, api->request->query_string) > 0)
add_querystr_string(api, querystr, "q.status=", ":status", &where);
// Reply type filtering?
if(GET_STR("reply", replyname, api->request->query_string) > 0)
add_querystr_string(api, querystr, "q.reply_type=", ":reply_type", &where);
// DNSSEC status filtering?
if(GET_STR("dnssec", dnssecname, api->request->query_string) > 0)
add_querystr_string(api, querystr, "q.dnssec=", ":dnssec", &where);
// Sorting?
int sort_column = -1;
// Encoded URI string: %5B = [ and %5D = ]
if(get_int_var(api->request->query_string, "order%5B0%5D%5Bcolumn%5D", &sort_column) &&
GET_STR("order%5B0%5D%5Bdir%5D", sort_dir, api->request->query_string) > 0)
{
char sort_col_id[32] = { 0 };
snprintf(sort_col_id, sizeof(sort_col_id), "columns%%5B%d%%5D%%5Bdata%%5D",
sort_column);
// Encoded URI string: %5B = [ and %5D = ]
if(GET_VAR(sort_col_id, sort_col, api->request->query_string) > 0)
log_debug(DEBUG_API, "Sorting by column %s (%s)", sort_col, sort_dir);
else
log_warn("Sorting by column %d (%s) requested, but column name not found",
sort_column, sort_dir);
}
}
// We use this boolean to memorize if we are filtering at all. It is used
// later to decide if we can short-circuit the query counting for
// performance reasons.
bool filtering = false;
// Regex filtering?
regex_t *regex_domains = NULL;
unsigned int N_regex_domains = 0;
if(compile_filter_regex(api, "webserver.api.excludeDomains",
config.webserver.api.excludeDomains.v.json,
®ex_domains, &N_regex_domains))
filtering = true;
regex_t *regex_clients = NULL;
unsigned int N_regex_clients = 0;
if(compile_filter_regex(api, "webserver.api.excludeClients",
config.webserver.api.excludeClients.v.json,
®ex_clients, &N_regex_clients))
filtering = true;
// Finish preparing query string
querystr_finish(querystr, sort_col, sort_dir);
// Get connection to in-memory database
sqlite3 *memdb = get_memdb();
if(memdb == NULL)
{
return send_json_error(api, 500, // 500 Internal error
"database_error",
"Could not read from in-memory database",
NULL);
}
// Prepare SQLite3 statement
sqlite3_stmt *read_stmt = NULL;
int rc = sqlite3_prepare_v2(memdb, querystr, -1, &read_stmt, NULL);
if( rc != SQLITE_OK )
{
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to prepare read SQL query",
sqlite3_errstr(rc));
}
// Bind items to prepared statement
if(api->request->query_string != NULL)
{
int idx;
idx = sqlite3_bind_parameter_index(read_stmt, ":tsfrom");
if(idx > 0)
{
log_debug(DEBUG_API, "adding :tsfrom = %lf to query", timestamp_from);
filtering = true;
if((rc = sqlite3_bind_double(read_stmt, idx, timestamp_from)) != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind timestamp:from to SQL query",
sqlite3_errstr(rc));
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":tsuntil");
if(idx > 0)
{
log_debug(DEBUG_API, "adding :tsuntil = %lf to query", timestamp_until);
filtering = true;
if((rc = sqlite3_bind_double(read_stmt, idx, timestamp_until)) != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind timestamp:until to SQL query",
sqlite3_errstr(rc));
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":domain");
if(idx > 0)
{
log_debug(DEBUG_API, "adding :domain = \"%s\" to query", domainname);
filtering = true;
if((rc = sqlite3_bind_text(read_stmt, idx, domainname, -1, SQLITE_STATIC)) != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind domain to SQL query",
sqlite3_errstr(rc));
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":cip");
if(idx > 0)
{
log_debug(DEBUG_API, "adding :cip = \"%s\" to query", clientip);
filtering = true;
if((rc = sqlite3_bind_text(read_stmt, idx, clientip, -1, SQLITE_STATIC)) != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind cip to SQL query",
sqlite3_errstr(rc));
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":cname");
if(idx > 0)
{
log_debug(DEBUG_API, "adding :cname = \"%s\" to query", clientname);
filtering = true;
if((rc = sqlite3_bind_text(read_stmt, idx, clientname, -1, SQLITE_STATIC)) != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind client to SQL query",
sqlite3_errstr(rc));
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":upstream");
if(idx > 0)
{
log_debug(DEBUG_API, "adding :upstream = \"%s\" to query", upstreamname);
filtering = true;
if((rc = sqlite3_bind_text(read_stmt, idx, upstreamname, -1, SQLITE_STATIC)) != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind upstream to SQL query",
sqlite3_errstr(rc));
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":type");
if(idx > 0)
{
enum query_type type;
for(type = TYPE_A; type < TYPE_MAX; type++)
{
if(strcasecmp(typename, get_query_type_str(type, NULL, NULL)) == 0)
break;
}
if(type < TYPE_MAX)
{
log_debug(DEBUG_API, "adding :type = %d to query", type);
filtering = true;
rc = sqlite3_bind_int(read_stmt, idx, type);
if(rc != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind type to SQL query",
sqlite3_errstr(rc));
}
}
else
{
return send_json_error(api, 400,
"bad_request",
"Requested type is invalid",
typename);
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":status");
if(idx > 0)
{
enum query_status status;
for(status = QUERY_UNKNOWN; status < QUERY_STATUS_MAX; status++)
{
if(strcasecmp(statusname, get_query_status_str(status)) == 0)
break;
}
if(status < QUERY_STATUS_MAX)
{
log_debug(DEBUG_API, "adding :status = %d to query", status);
filtering = true;
rc = sqlite3_bind_int(read_stmt, idx, status);
if(rc != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind status to SQL query",
sqlite3_errstr(rc));
}
}
else
{
return send_json_error(api, 400,
"bad_request",
"Requested status is invalid",
statusname);
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":reply_type");
if(idx > 0)
{
enum reply_type reply;
for(reply = REPLY_UNKNOWN; reply < QUERY_REPLY_MAX; reply++)
{
if(strcasecmp(replyname, get_query_reply_str(reply)) == 0)
break;
}
if(reply < QUERY_REPLY_MAX)
{
log_debug(DEBUG_API, "adding :reply_type = %d to query", reply);
filtering = true;
rc = sqlite3_bind_int(read_stmt, idx, reply);
if(rc != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind reply to SQL query",
sqlite3_errstr(rc));
}
}
else
{
return send_json_error(api, 400,
"bad_request",
"Requested reply is invalid",
replyname);
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":dnssec");
if(idx > 0)
{
enum dnssec_status dnssec;
for(dnssec = DNSSEC_UNKNOWN; dnssec < DNSSEC_MAX; dnssec++)
{
if(strcasecmp(dnssecname, get_query_dnssec_str(dnssec)) == 0)
break;
}
if(dnssec < DNSSEC_MAX)
{
log_debug(DEBUG_API, "adding :dnssec = %d to query", dnssec);
filtering = true;
rc = sqlite3_bind_int(read_stmt, idx, dnssec);
if(rc != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind dnssec to SQL query",
sqlite3_errstr(rc));
}
}
else
{
return send_json_error(api, 400,
"bad_request",
"Requested dnssec is invalid",
dnssecname);
}
}
idx = sqlite3_bind_parameter_index(read_stmt, ":cursor");
if(idx > 0)
{
log_debug(DEBUG_API, "adding :cursor = %lu to query", cursor);
// Do not set filtering as the cursor is not a filter
rc = sqlite3_bind_int64(read_stmt, idx, cursor);
if(rc != SQLITE_OK)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind count to SQL query",
sqlite3_errstr(rc));
}
}
}
// Debug logging
log_debug(DEBUG_API, "SQL: %s", querystr);
log_debug(DEBUG_API, " with cursor: %lu, start: %u, length: %d", cursor, start, length);
cJSON *queries = JSON_NEW_ARRAY();
unsigned int added = 0, recordsCounted = 0, regex_skipped = 0;
bool skipTheRest = false;
while((rc = sqlite3_step(read_stmt)) == SQLITE_ROW)
{
// Increase number of records from the database
recordsCounted++;
// Apply possible domain regex filters to Query Log
const char *domain = (const char*)sqlite3_column_text(read_stmt, 4); // d.domain
if(N_regex_domains > 0)
{
bool match = false;
// Iterate over all regex filters
for(unsigned int i = 0; i < N_regex_domains; i++)
{
// Check if the domain matches the regex
if(regexec(®ex_domains[i], domain, 0, NULL, 0) == 0)
{
// Domain matches
match = true;
break;
}
}
if(match)
{
// Domain matches, we skip it and adjust the
// counter
recordsCounted--;
regex_skipped++;
continue;
}
}
// Apply possible client regex filters to Query Log
const char *client_ip = (const char*)sqlite3_column_text(read_stmt, 10); // c.ip
const char *client_name = NULL;
if(sqlite3_column_type(read_stmt, 11) == SQLITE_TEXT && sqlite3_column_bytes(read_stmt, 11) > 0)
client_name = (const char*)sqlite3_column_text(read_stmt, 11); // c.name
if(N_regex_clients > 0)
{
bool match = false;
// Iterate over all regex filters
for(unsigned int i = 0; i < N_regex_clients; i++)
{
// Check if the domain matches the regex
if(regexec(®ex_clients[i], client_ip, 0, NULL, 0) == 0)
{
// Client IP matches
match = true;
break;
}
else if(client_name != NULL && regexec(®ex_clients[i], client_name, 0, NULL, 0) == 0)
{
// Client name matches
match = true;
break;
}
}
if(match)
{
// Domain matches, we skip it and adjust the
// counter
recordsCounted--;
regex_skipped++;
continue;
}
}
// Skip all records once we have enough (but still count them)
if(skipTheRest)
continue;
// Check if we have reached the limit
// Length may be set to -1 to indicate we want everything.
if(length > 0 && added >= (unsigned int)length)
{
if(filtering)
{
// We are filtering, so we have to continue to
// step over the remaining rows to get the
// correct number of total records
skipTheRest = true;
continue;
}
else
{
// We are not filtering, so we can stop here
// The total number of records is the number
// of records in the database
break;
}
}
// Server-side pagination
if(start > 0 && start >= recordsCounted)
{
// Skip all results BEFORE start (server-side pagination)
continue;
}
else if(length > 0 && added >= (unsigned int)length)
{
// Skip everything AFTER we added the requested number
// of queries if length is > 0.
continue;
}
// Check if we have reached the limit
if(added >= (unsigned int)length)
{
if(filtering)
{
// We are filtering, so we have to continue to
// step over the remaining rows to get the
// correct number of total records
skipTheRest = true;
continue;
}
else
{
// We are not filtering, so we can stop here
// The total number of records is the number
// of records in the database
break;
}
}
// Build item object
cJSON *item = JSON_NEW_OBJECT();
queriesData query = { 0 };
char buffer[20] = { 0 };
JSON_ADD_NUMBER_TO_OBJECT(item, "id", sqlite3_column_int64(read_stmt, 0)); // q.id);
JSON_ADD_NUMBER_TO_OBJECT(item, "time", sqlite3_column_double(read_stmt, 1)); // timestamp
query.type = sqlite3_column_int(read_stmt, 2); // type
query.status = sqlite3_column_int(read_stmt, 3); // status
query.reply = sqlite3_column_int(read_stmt, 7); // reply_type
query.dnssec = sqlite3_column_int(read_stmt, 9); // dnssec
// We have to copy the string as TYPExxx string won't be static
JSON_COPY_STR_TO_OBJECT(item, "type", get_query_type_str(query.type, &query, buffer));
JSON_REF_STR_IN_OBJECT(item, "status", get_query_status_str(query.status));
JSON_REF_STR_IN_OBJECT(item, "dnssec", get_query_dnssec_str(query.dnssec));
JSON_COPY_STR_TO_OBJECT(item, "domain", domain);
if(sqlite3_column_type(read_stmt, 5) == SQLITE_TEXT &&
sqlite3_column_bytes(read_stmt, 5) > 0)
JSON_COPY_STR_TO_OBJECT(item, "upstream", sqlite3_column_text(read_stmt, 5)); // f.forward
else
JSON_ADD_NULL_TO_OBJECT(item, "upstream");
cJSON *reply = JSON_NEW_OBJECT();
JSON_REF_STR_IN_OBJECT(reply, "type", get_query_reply_str(query.reply));
JSON_ADD_NUMBER_TO_OBJECT(reply, "time", sqlite3_column_double(read_stmt, 8)); // reply_time
JSON_ADD_ITEM_TO_OBJECT(item, "reply", reply);
cJSON *client = JSON_NEW_OBJECT();
JSON_COPY_STR_TO_OBJECT(client, "ip", client_ip);
if(client_name != NULL)
JSON_COPY_STR_TO_OBJECT(client, "name", client_name);
else
JSON_ADD_NULL_TO_OBJECT(client, "name");
JSON_ADD_ITEM_TO_OBJECT(item, "client", client);
// Add regex_id if it exists
if(sqlite3_column_type(read_stmt, 13) == SQLITE_INTEGER)
JSON_ADD_NUMBER_TO_OBJECT(item, "regex_id", sqlite3_column_int(read_stmt, 13)); // regex_id
else
JSON_ADD_NULL_TO_OBJECT(item, "regex_id");
const unsigned char *cname = NULL;
switch(query.status)
{
case QUERY_GRAVITY_CNAME:
case QUERY_REGEX_CNAME:
case QUERY_DENYLIST_CNAME:
{
cname = sqlite3_column_text(read_stmt, 12); // a.content
break;
}
case QUERY_UNKNOWN:
case QUERY_GRAVITY:
case QUERY_FORWARDED:
case QUERY_CACHE:
case QUERY_REGEX:
case QUERY_DENYLIST:
case QUERY_EXTERNAL_BLOCKED_IP:
case QUERY_EXTERNAL_BLOCKED_NULL:
case QUERY_EXTERNAL_BLOCKED_NXRA:
case QUERY_RETRIED:
case QUERY_RETRIED_DNSSEC:
case QUERY_IN_PROGRESS:
case QUERY_DBBUSY:
case QUERY_SPECIAL_DOMAIN:
case QUERY_CACHE_STALE:
case QUERY_STATUS_MAX:
break;
}
if(cname != NULL)
JSON_COPY_STR_TO_OBJECT(item, "cname", (const char*)cname);
else
JSON_ADD_NULL_TO_OBJECT(item, "cname");
JSON_ADD_ITEM_TO_ARRAY(queries, item);
added++;
}
log_debug(DEBUG_API, "Sending %u of %lu in memory and %lu on disk queries (counted %u, skipped %u)",
added, mem_dbnum, disk_dbnum, recordsCounted, regex_skipped);
cJSON *json = JSON_NEW_OBJECT();
JSON_ADD_ITEM_TO_OBJECT(json, "queries", queries);
if(cursor_set)
{
// Repeat cursor received in the request. This ensures we get a
// static result by skipping any newer queries.
log_debug(DEBUG_API, "Sending cursor %lu", cursor);
JSON_ADD_NUMBER_TO_OBJECT(json, "cursor", cursor);
}
else
{
// Send cursor pointing to the firstID of the data obtained in
// this query. This ensures we get a static result by skipping
// any newer queries.
log_debug(DEBUG_API, "Sending cursor %lu (firstID)", get_max_db_idx());
JSON_ADD_NUMBER_TO_OBJECT(json, "cursor", get_max_db_idx());
}
// DataTables specific properties
const unsigned long recordsTotal = disk ? disk_dbnum : mem_dbnum;
JSON_ADD_NUMBER_TO_OBJECT(json, "recordsTotal", recordsTotal);
JSON_ADD_NUMBER_TO_OBJECT(json, "recordsFiltered", filtering ? recordsCounted : recordsTotal);
JSON_ADD_NUMBER_TO_OBJECT(json, "draw", draw);
// Finalize statements
sqlite3_finalize(read_stmt);
// Free regex memory if allocated
if(N_regex_domains > 0)
{
// Free individual regexes
for(unsigned int i = 0; i < N_regex_domains; i++)
regfree(®ex_domains[i]);
// Free array of regex pointers
free(regex_domains);
}
if(N_regex_clients > 0)
{
// Free individual regexes
for(unsigned int i = 0; i < N_regex_clients; i++)
regfree(®ex_clients[i]);
// Free array of regex po^inters
free(regex_clients);
}
JSON_SEND_OBJECT(json);
}
bool compile_filter_regex(struct ftl_conn *api, const char *path, cJSON *json, regex_t **regex, unsigned int *N_regex)
{
const int N = cJSON_GetArraySize(json);
if(N < 1)
return false;
// Set number of regexes (positive = unsigned integer)
*N_regex = N;
// Allocate memory for regex array
*regex = calloc(N, sizeof(regex_t));
if(*regex == NULL)
{
return send_json_error(api, 500,