forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsmasq_interface.c
1167 lines (1016 loc) · 35.6 KB
/
dnsmasq_interface.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) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* dnsmasq interfacing routines
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "dnsmasq/dnsmasq.h"
#undef __USE_XOPEN
#include "FTL.h"
#include "dnsmasq_interface.h"
void print_flags(unsigned int flags);
void save_reply_type(unsigned int flags, int queryID, struct timeval response);
unsigned long converttimeval(struct timeval time);
static void block_single_domain(char *domain);
static void detect_blocked_IP(unsigned short flags, char* answer, int queryID);
static void query_externally_blocked(int i);
static int findQueryID(int id);
unsigned char* pihole_privacylevel = &config.privacylevel;
char flagnames[28][12] = {"F_IMMORTAL ", "F_NAMEP ", "F_REVERSE ", "F_FORWARD ", "F_DHCP ", "F_NEG ", "F_HOSTS ", "F_IPV4 ", "F_IPV6 ", "F_BIGNAME ", "F_NXDOMAIN ", "F_CNAME ", "F_DNSKEY ", "F_CONFIG ", "F_DS ", "F_DNSSECOK ", "F_UPSTREAM ", "F_RRNAME ", "F_SERVER ", "F_QUERY ", "F_NOERR ", "F_AUTH ", "F_DNSSEC ", "F_KEYTAG ", "F_SECSTAT ", "F_NO_RR ", "F_IPSET ", "F_NOEXTRA "};
void FTL_new_query(unsigned int flags, char *name, struct all_addr *addr, char *types, int id, char type)
{
// Don't analyze anything if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Create new query in data structure
enable_thread_lock();
// Get timestamp
int querytimestamp, overTimetimestamp;
gettimestamp(&querytimestamp, &overTimetimestamp);
// Save request time
struct timeval request;
gettimeofday(&request, 0);
// Determine query type
unsigned char querytype = 0;
if(strcmp(types,"query[A]") == 0)
querytype = TYPE_A;
else if(strcmp(types,"query[AAAA]") == 0)
querytype = TYPE_AAAA;
else if(strcmp(types,"query[ANY]") == 0)
querytype = TYPE_ANY;
else if(strcmp(types,"query[SRV]") == 0)
querytype = TYPE_SRV;
else if(strcmp(types,"query[SOA]") == 0)
querytype = TYPE_SOA;
else if(strcmp(types,"query[PTR]") == 0)
querytype = TYPE_PTR;
else if(strcmp(types,"query[TXT]") == 0)
querytype = TYPE_TXT;
else
{
// Return early to avoid accessing querytypedata out of bounds
if(debug) logg("Notice: Skipping unknown query type: %s (%i)", types, id);
disable_thread_lock();
return;
}
// Skip AAAA queries if user doesn't want to have them analyzed
if(!config.analyze_AAAA && querytype == TYPE_AAAA)
{
if(debug) logg("Not analyzing AAAA query");
disable_thread_lock();
return;
}
// Ensure we have enough space in the queries struct
memory_check(QUERIES);
int queryID = counters.queries;
// Convert domain to lower case
char *domain = strdup(name);
strtolower(domain);
// If domain is "pi.hole" we skip this query
if(strcmp(domain, "pi.hole") == 0)
{
// free memory already allocated here
free(domain);
disable_thread_lock();
return;
}
// Store plain text domain in buffer for regex validation
char *domainbuffer = strdup(domain);
// Get client IP address
char dest[ADDRSTRLEN];
inet_ntop((flags & F_IPV4) ? AF_INET : AF_INET6, addr, dest, ADDRSTRLEN);
char *client = strdup(dest);
strtolower(client);
// Check if user wants to skip queries coming from localhost
if(config.ignore_localhost &&
(strcmp(client, "127.0.0.1") == 0 || strcmp(client, "::1") == 0))
{
free(domain);
free(client);
disable_thread_lock();
return;
}
// Log new query if in debug mode
char *proto = (type == UDP) ? "UDP" : "TCP";
if(debug) logg("**** new %s %s \"%s\" from %s (ID %i)", proto, types, domain, client, id);
// Update counters
int timeidx = findOverTimeID(overTimetimestamp);
validate_access("overTime", timeidx, true, __LINE__, __FUNCTION__, __FILE__);
overTime[timeidx].querytypedata[querytype-1]++;
counters.querytype[querytype-1]++;
// Skip rest of the analysis if this query is not of type A or AAAA
// but user wants to see only A and AAAA queries (pre-v4.1 behavior)
if(config.analyze_only_A_AAAA && querytype != TYPE_A && querytype != TYPE_AAAA)
{
// Don't process this query further here, we already counted it
if(debug) logg("Notice: Skipping new query: %s (%i)", types, id);
free(domain);
free(domainbuffer);
free(client);
disable_thread_lock();
return;
}
// Go through already knows domains and see if it is one of them
int domainID = findDomainID(domain);
// Go through already knows clients and see if it is one of them
int clientID = findClientID(client);
// Save everything
validate_access("queries", queryID, false, __LINE__, __FUNCTION__, __FILE__);
queries[queryID].magic = MAGICBYTE;
queries[queryID].timestamp = querytimestamp;
queries[queryID].type = querytype;
queries[queryID].status = QUERY_UNKNOWN;
queries[queryID].domainID = domainID;
queries[queryID].clientID = clientID;
queries[queryID].timeidx = timeidx;
queries[queryID].db = false;
queries[queryID].id = id;
queries[queryID].complete = false;
queries[queryID].response = converttimeval(request);
// Initialize reply type
queries[queryID].reply = REPLY_UNKNOWN;
// Store DNSSEC result for this domain
queries[queryID].dnssec = DNSSEC_UNSPECIFIED;
// AD has not yet been received for this query
queries[queryID].AD = false;
// Check and apply possible privacy level rules
// The currently set privacy level (at the time the query is
// generated) is stored in the queries structure
get_privacy_level(NULL);
queries[queryID].privacylevel = config.privacylevel;
// Increase DNS queries counter
counters.queries++;
// Count this query as unknown as long as no reply has
// been found and analyzed
counters.unknown++;
// Update overTime data
validate_access("overTime", timeidx, true, __LINE__, __FUNCTION__, __FILE__);
overTime[timeidx].total++;
// Update overTime data structure with the new client
validate_access_oTcl(timeidx, clientID, __LINE__, __FUNCTION__, __FILE__);
overTime[timeidx].clientdata[clientID]++;
// Try blocking regex if configured
validate_access("domains", domainID, false, __LINE__, __FUNCTION__, __FILE__);
if(domains[domainID].regexmatch == REGEX_UNKNOWN && blockingstatus != BLOCKING_DISABLED)
{
// For minimal performance impact, we test the regex only when
// - regex checking is enabled, and
// - this domain has not already been validated against the regex.
// This effectively prevents multiple evaluations of the same domain
//
// If a regex filter matched, we additionally compare the domain
// against all known whitelisted domains to possibly prevent blocking
// of a specific domain. The logic herein is:
// If matched, then compare against whitelist
// If in whitelist, negate matched so this function returns: not-to-be-blocked
if(match_regex(domainbuffer) && !in_whitelist(domainbuffer))
{
// We have to block this domain
block_single_domain(domainbuffer);
domains[domainID].regexmatch = REGEX_BLOCKED;
}
else
{
// Explicitly mark as not blocked to skip regex test
// next time we see this domain
domains[domainID].regexmatch = REGEX_NOTBLOCKED;
}
}
// Free allocated memory
free(client);
free(domain);
free(domainbuffer);
// Release thread lock
disable_thread_lock();
}
static int findQueryID(int id)
{
// Loop over all queries - we loop in reverse order (start from the most recent query and
// continuously walk older queries while trying to find a match. Ideally, we should always
// find the correct query with zero iterations, but it may happen that queries are processed
// asynchronously, e.g. for slow upstream relies to a huge amount of requests.
// We iterate from the most recent query down to at most MAXITER queries in the past to avoid
// iterating through the entire array of queries
// MAX(0, a) is used to return 0 in case a is negative (negative array indices are harmful)
// Validate access only once for the maximum index (all lower will work)
validate_access("queries", counters.queries-1, false, __LINE__, __FUNCTION__, __FILE__);
int until = MAX(0, counters.queries-MAXITER);
int i;
// Check UUIDs of queries
for(i = counters.queries-1; i >= until; i--)
if(queries[i].id == id)
return i;
// If not found
return -1;
}
void FTL_forwarded(unsigned int flags, char *name, struct all_addr *addr, int id)
{
// Don't analyze anything if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Save that this query got forwarded to an upstream server
enable_thread_lock();
// Get forward destination IP address
char dest[ADDRSTRLEN];
inet_ntop((flags & F_IPV4) ? AF_INET : AF_INET6, addr, dest, ADDRSTRLEN);
// Convert forward to lower case
char *forward = strdup(dest);
strtolower(forward);
// Debug logging
if(debug) logg("**** forwarded %s to %s (ID %i)", name, forward, id);
// Save status and forwardID in corresponding query identified by dnsmasq's ID
int i = findQueryID(id);
if(i < 0)
{
// This may happen e.g. if the original query was a PTR query or "pi.hole"
// as we ignore them altogether
free(forward);
disable_thread_lock();
return;
}
// Set query status
queries[i].status = QUERY_FORWARDED;
// Proceed only if
// - current query has not been marked as replied to so far
// (it could be that answers from multiple forward
// destionations are coimg in for the same query)
// - the query was formally known as cached but had to be forwarded
// (this is a special case further described below)
if(queries[i].complete && queries[i].status != QUERY_CACHE)
{
free(forward);
disable_thread_lock();
return;
}
// Get ID of forward destination, create new forward destination record
// if not found in current data structure
int forwardID = findForwardID(forward, true);
queries[i].forwardID = forwardID;
if(!queries[i].complete)
{
int j = queries[i].timeidx;
validate_access("overTime", j, true, __LINE__, __FUNCTION__, __FILE__);
if(queries[i].status == QUERY_CACHE)
{
// Detect if we cached the <CNAME> but need to ask the upstream
// servers for the actual IPs now, we remove this query from the
// counters for cache replied queries as we had to forward a
// request for it. Example:
// Assume a domain a.com is a CNAME which is cached and has a very
// long TTL. It point to another domain server.a.com which has an
// A record but this has a much lower TTL.
// If you now query a.com and then again after some time, you end
// up in a situation where dnsmasq can answer the first level of
// the DNS result (the CNAME) from cache, hence the status of this
// query is marked as "answered from cache" in FTLDNS. However, for
// server.a.com wit the much shorter TTL, we still have to forward
// something and ask the upstream server for the final IP address.
// This code section acknowledges this by removing one entry from
// the cached counters as we will re-brand this query as having been
// forwarded in the following.
counters.cached--;
// Also correct overTime data
overTime[j].cached--;
// Correct reply timer
struct timeval response;
gettimeofday(&response, 0);
// Reset timer, shift slightly into the past to acknowledge the time
// FTLDNS needed to look up the CNAME in its cache
queries[i].response = converttimeval(response) - queries[i].response;
}
else
{
// Normal cache reply
// Query is no longer unknown
counters.unknown--;
// Hereby, this query is now fully determined
queries[i].complete = true;
}
// Update overTime data
overTime[j].forwarded++;
// Update couter for forwarded queries
counters.forwardedqueries++;
}
// Release allocated memory
free(forward);
disable_thread_lock();
}
void FTL_dnsmasq_reload(void)
{
// This function is called by the dnsmasq code on receive of SIGHUP
// *before* clearing the cache and rereading the lists
// This is the only hook that is not skipped in PRIVACY_NOSTATS mode
// Called when dnsmasq re-reads its config and hosts files
// Reset number of blocked domains
counters.gravity = 0;
// Inspect 01-pihole.conf to see if Pi-hole blocking is enabled,
// i.e. if /etc/pihole/gravity.list is sourced as addn-hosts file
check_blocking_status();
// Reread pihole-FTL.conf to see which blocking mode the user wants to use
// It is possible to change the blocking mode here as we anyhow clear the
// cache and reread all blocking lists
// Passing NULL to this function means it has to open the config file on
// its own behalf (on initial reading, the config file is already opened)
get_blocking_mode(NULL);
// Reread regex.list
free_regex();
read_regex_from_file();
}
void FTL_reply(unsigned short flags, char *name, struct all_addr *addr, int id)
{
// Don't analyze anything if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Interpret hosts files that have been read by dnsmasq
enable_thread_lock();
// Determine returned result if available
char dest[ADDRSTRLEN]; dest[0] = '\0';
if(addr)
{
inet_ntop((flags & F_IPV4) ? AF_INET : AF_INET6, addr, dest, ADDRSTRLEN);
}
// Extract answer (used e.g. for detecting if a local config is a user-defined
// wildcard blocking entry in form "server=/tobeblocked.com/")
char *answer = dest;
if(flags & F_CNAME)
answer = "(CNAME)";
else if((flags & F_NEG) && (flags & F_NXDOMAIN))
answer = "(NXDOMAIN)";
else if(flags & F_NEG)
answer = "(NODATA)";
if(debug)
{
logg("**** got reply %s is %s (ID %i)", name, answer, id);
print_flags(flags);
}
// Get response time
struct timeval response;
gettimeofday(&response, 0);
// Save status in corresponding query identified by dnsmasq's ID
int i = findQueryID(id);
if(i < 0)
{
// This may happen e.g. if the original query was "pi.hole"
if(debug) logg("FTL_reply(): Query %i has not been found", id);
disable_thread_lock();
return;
}
if(queries[i].reply != REPLY_UNKNOWN)
{
// Nothing to be done here
disable_thread_lock();
return;
}
if(flags & F_CONFIG)
{
// Answered from local configuration, might be a wildcard or user-provided
// This query is no longer unknown
counters.unknown--;
// Get time index
int querytimestamp, overTimetimestamp;
gettimestamp(&querytimestamp, &overTimetimestamp);
int timeidx = findOverTimeID(overTimetimestamp);
validate_access("overTime", timeidx, true, __LINE__, __FUNCTION__, __FILE__);
if(strcmp(answer, "(NXDOMAIN)") == 0 ||
strcmp(answer, "0.0.0.0") == 0 ||
strcmp(answer, "::") == 0)
{
// Answered from user-defined blocking rules (dnsmasq config files)
counters.blocked++;
overTime[timeidx].blocked++;
validate_access("domains", queries[i].domainID, true, __LINE__, __FUNCTION__, __FILE__);
domains[queries[i].domainID].blockedcount++;
validate_access("clients", queries[i].clientID, true, __LINE__, __FUNCTION__, __FILE__);
clients[queries[i].clientID].blockedcount++;
queries[i].status = QUERY_WILDCARD;
}
else
{
// Answered from a custom (user provided) cache file
counters.cached++;
overTime[timeidx].cached++;
queries[i].status = QUERY_CACHE;
}
// Save reply type and update individual reply counters
save_reply_type(flags, i, response);
// Hereby, this query is now fully determined
queries[i].complete = true;
}
else if(flags & F_FORWARD)
{
int domainID = queries[i].domainID;
validate_access("domains", domainID, true, __LINE__, __FUNCTION__, __FILE__);
if(strcmp(domains[domainID].domain, name) == 0)
{
// Save reply type and update individual reply counters
save_reply_type(flags, i, response);
// If received NXDOMAIN and AD bit is set, Quad9 may have blocked this query
if(flags & F_NXDOMAIN && queries[i].AD)
{
query_externally_blocked(i);
}
// Detect if returned IP indicates that this query was blocked
detect_blocked_IP(flags, answer, i);
}
}
else if(flags & F_REVERSE)
{
// Save reply type and update individual reply counters
save_reply_type(flags, i, response);
}
else
{
logg("*************************** unknown REPLY ***************************");
print_flags(flags);
}
disable_thread_lock();
}
static void detect_blocked_IP(unsigned short flags, char* answer, int queryID)
{
// Skip replies which originated locally. Otherwise, we would count
// gravity.list blocked queries as externally blocked.
if(flags & F_HOSTS)
{
return;
}
// If received one of the following IPs as reply, OpenDNS
// (Cisco Umbrella) blocked this query
// See https://support.opendns.com/hc/en-us/articles/227986927-What-are-the-Cisco-Umbrella-Block-Page-IP-Addresses-
// for a full list of these IP addresses
if(flags & F_IPV4 && answer != NULL &&
(strcmp("146.112.61.104", answer) == 0 ||
strcmp("146.112.61.105", answer) == 0 ||
strcmp("146.112.61.106", answer) == 0 ||
strcmp("146.112.61.107", answer) == 0 ||
strcmp("146.112.61.108", answer) == 0 ||
strcmp("146.112.61.109", answer) == 0 ||
strcmp("146.112.61.110", answer) == 0 ))
{
query_externally_blocked(queryID);
}
else if(flags & F_IPV6 && answer != NULL &&
(strcmp("::ffff:146.112.61.104", answer) == 0 ||
strcmp("::ffff:146.112.61.105", answer) == 0 ||
strcmp("::ffff:146.112.61.106", answer) == 0 ||
strcmp("::ffff:146.112.61.107", answer) == 0 ||
strcmp("::ffff:146.112.61.108", answer) == 0 ||
strcmp("::ffff:146.112.61.109", answer) == 0 ||
strcmp("::ffff:146.112.61.110", answer) == 0 ))
{
query_externally_blocked(queryID);
}
// If upstream replied with 0.0.0.0 or ::,
// we assume that it filtered the reply as
// nothing is reachable under these addresses
else if(flags & F_IPV4 && answer != NULL &&
strcmp("0.0.0.0", answer) == 0)
{
query_externally_blocked(queryID);
}
else if(flags & F_IPV6 && answer != NULL &&
strcmp("::", answer) == 0)
{
query_externally_blocked(queryID);
}
}
static void query_externally_blocked(int i)
{
// Correct counters if necessary ...
if(queries[i].status == QUERY_FORWARDED)
{
counters.forwardedqueries--;
overTime[queries[i].timeidx].forwarded--;
validate_access("forwarded", queries[i].forwardID, true, __LINE__, __FUNCTION__, __FILE__);
forwarded[queries[i].forwardID].count--;
}
// ... but as blocked
counters.blocked++;
overTime[queries[i].timeidx].blocked++;
validate_access("domains", queries[i].domainID, true, __LINE__, __FUNCTION__, __FILE__);
domains[queries[i].domainID].blockedcount++;
validate_access("clients", queries[i].clientID, true, __LINE__, __FUNCTION__, __FILE__);
clients[queries[i].clientID].blockedcount++;
queries[i].status = QUERY_EXTERNAL_BLOCKED;
}
void FTL_cache(unsigned int flags, char *name, struct all_addr *addr, char *arg, int id)
{
// Don't analyze anything if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Save that this query got answered from cache
enable_thread_lock();
char dest[ADDRSTRLEN]; dest[0] = '\0';
if(addr)
{
inet_ntop((flags & F_IPV4) ? AF_INET : AF_INET6, addr, dest, ADDRSTRLEN);
}
// Convert domain to lower case
char *domain = strdup(name);
strtolower(domain);
// If domain is "pi.hole", we skip this query
if(strcmp(domain, "pi.hole") == 0)
{
// free memory already allocated here
free(domain);
disable_thread_lock();
return;
}
free(domain);
// Debug logging
if(debug) logg("**** got cache answer for %s / %s / %s (ID %i)", name, dest, arg, id);
if(debug) print_flags(flags);
// Get response time
struct timeval response;
gettimeofday(&response, 0);
if(((flags & F_HOSTS) && (flags & F_IMMORTAL)) ||
((flags & F_NAMEP) && (flags & F_DHCP)) ||
(flags & F_FORWARD) ||
(flags & F_REVERSE) ||
(flags & F_RRNAME))
{
// List data: /etc/pihole/gravity.list, /etc/pihole/black.list, /etc/pihole/local.list, etc.
// or
// DHCP server reply
// or
// regex blocked query
// or
// cached answer to previously forwarded request
// Determine requesttype
unsigned char requesttype = 0;
if(flags & F_HOSTS)
{
if(arg != NULL && strstr(arg, "/gravity.list") != NULL)
requesttype = QUERY_GRAVITY;
else if(arg != NULL && strstr(arg, "/black.list") != NULL)
requesttype = QUERY_BLACKLIST;
else // local.list, hostname.list, /etc/hosts and others
requesttype = QUERY_CACHE;
}
else if((flags & F_NAMEP) && (flags & F_DHCP)) // DHCP server reply
requesttype = QUERY_CACHE;
else if(flags & F_FORWARD) // cached answer to previously forwarded request
requesttype = QUERY_CACHE;
else if(flags & F_REVERSE) // cached answer to reverse request (PTR)
requesttype = QUERY_CACHE;
else if(flags & F_RRNAME) // cached answer to TXT query
requesttype = QUERY_CACHE;
else
{
logg("*************************** unknown CACHE reply (1) ***************************");
print_flags(flags);
}
int i = findQueryID(id);
if(i < 0)
{
// This may happen e.g. if the original query was a PTR query or "pi.hole"
// as we ignore them altogether
disable_thread_lock();
return;
}
if(!queries[i].complete)
{
// This query is no longer unknown
counters.unknown--;
// Get time index
int querytimestamp, overTimetimestamp;
gettimestamp(&querytimestamp, &overTimetimestamp);
int timeidx = findOverTimeID(overTimetimestamp);
validate_access("overTime", timeidx, true, __LINE__, __FUNCTION__, __FILE__);
int domainID = queries[i].domainID;
validate_access("domains", domainID, true, __LINE__, __FUNCTION__, __FILE__);
int clientID = queries[i].clientID;
validate_access("clients", clientID, true, __LINE__, __FUNCTION__, __FILE__);
// Mark this query as blocked if domain was matched by a regex
if(domains[domainID].regexmatch == REGEX_BLOCKED)
requesttype = QUERY_WILDCARD;
queries[i].status = requesttype;
// Detect if returned IP indicates that this query was blocked
detect_blocked_IP(flags, dest, i);
// Re-read requesttype as detect_blocked_IP() might have changed it
requesttype = queries[i].status;
// Handle counters accordingly
switch(requesttype)
{
case QUERY_GRAVITY: // gravity.list
case QUERY_BLACKLIST: // black.list
case QUERY_WILDCARD: // regex blocked
counters.blocked++;
overTime[timeidx].blocked++;
domains[domainID].blockedcount++;
clients[clientID].blockedcount++;
break;
case QUERY_CACHE: // cached from one of the lists
counters.cached++;
overTime[timeidx].cached++;
break;
case QUERY_EXTERNAL_BLOCKED:
// everything has already done
// in query_externally_blocked()
break;
}
// Save reply type and update individual reply counters
save_reply_type(flags, i, response);
// Hereby, this query is now fully determined
queries[i].complete = true;
}
}
else
{
logg("*************************** unknown CACHE reply (2) ***************************");
print_flags(flags);
}
disable_thread_lock();
}
void FTL_dnssec(int status, int id)
{
// Don't analyze anything if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Process DNSSEC result for a domain
enable_thread_lock();
// Search for corresponding query identified by ID
int i = findQueryID(id);
if(i < 0)
{
// This may happen e.g. if the original query was an unhandled query type
disable_thread_lock();
return;
}
// Debug logging
if(debug)
{
int domainID = queries[i].domainID;
validate_access("domains", domainID, true, __LINE__, __FUNCTION__, __FILE__);
logg("**** got DNSSEC details for %s: %i (ID %i)", domains[domainID].domain, status, id);
}
// Iterate through possible values
if(status == STAT_SECURE)
queries[i].dnssec = DNSSEC_SECURE;
else if(status == STAT_INSECURE)
queries[i].dnssec = DNSSEC_INSECURE;
else
queries[i].dnssec = DNSSEC_BOGUS;
disable_thread_lock();
}
void FTL_header_ADbit(unsigned char header4, int id)
{
// Don't analyze anything if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
enable_thread_lock();
// Check if AD bit is set in DNS header
if(!(header4 & 0x20))
{
// AD bit not set
disable_thread_lock();
return;
}
// Search for corresponding query identified by ID
int i = findQueryID(id);
if(i < 0)
{
// This may happen e.g. if the original query was an unhandled query type
disable_thread_lock();
return;
}
// Store AD bit in query data
queries[i].AD = true;
disable_thread_lock();
}
void print_flags(unsigned int flags)
{
// Debug function, listing resolver flags in clear text
// e.g. "Flags: F_FORWARD F_NEG F_IPV6"
unsigned int i;
char *flagstr = calloc(256,sizeof(char));
for(i = 0; i < sizeof(flags)*8; i++)
if(flags & (1u << i))
strcat(flagstr, flagnames[i]);
logg(" Flags: %s", flagstr);
free(flagstr);
}
void save_reply_type(unsigned int flags, int queryID, struct timeval response)
{
// Iterate through possible values
validate_access("queries", queryID, false, __LINE__, __FUNCTION__, __FILE__);
if(flags & F_NEG)
{
if(flags & F_NXDOMAIN)
{
// NXDOMAIN
queries[queryID].reply = REPLY_NXDOMAIN;
counters.reply_NXDOMAIN++;
}
else
{
// NODATA(-IPv6)
queries[queryID].reply = REPLY_NODATA;
counters.reply_NODATA++;
}
}
else if(flags & F_CNAME)
{
// <CNAME>
queries[queryID].reply = REPLY_CNAME;
counters.reply_CNAME++;
}
else if(flags & F_REVERSE)
{
// reserve lookup
queries[queryID].reply = REPLY_DOMAIN;
counters.reply_domain++;
}
else if(flags & F_RRNAME)
{
// TXT query
queries[queryID].reply = REPLY_RRNAME;
}
else
{
// Valid IP
queries[queryID].reply = REPLY_IP;
counters.reply_IP++;
}
// Save response time (relative time)
queries[queryID].response = converttimeval(response) -
queries[queryID].response;
}
pthread_t telnet_listenthreadv4;
pthread_t telnet_listenthreadv6;
pthread_t socket_listenthread;
pthread_t DBthread;
pthread_t GCthread;
pthread_t DNSclientthread;
void FTL_fork_and_bind_sockets(struct passwd *ent_pw)
{
if(!debug && daemonmode)
go_daemon();
else
savepid();
// We will use the attributes object later to start all threads in detached mode
pthread_attr_t attr;
// Initialize thread attributes object with default attribute values
pthread_attr_init(&attr);
// When a detached thread terminates, its resources are automatically released back to
// the system without the need for another thread to join with the terminated thread
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// Bind to sockets
bind_sockets();
// Start TELNET IPv4 thread
if(ipv4telnet && pthread_create( &telnet_listenthreadv4, &attr, telnet_listening_thread_IPv4, NULL ) != 0)
{
logg("Unable to open IPv4 telnet listening thread. Exiting...");
exit(EXIT_FAILURE);
}
// Start TELNET IPv6 thread
if(ipv6telnet && pthread_create( &telnet_listenthreadv6, &attr, telnet_listening_thread_IPv6, NULL ) != 0)
{
logg("Unable to open IPv6 telnet listening thread. Exiting...");
exit(EXIT_FAILURE);
}
// Start SOCKET thread
if(pthread_create( &socket_listenthread, &attr, socket_listening_thread, NULL ) != 0)
{
logg("Unable to open Unix socket listening thread. Exiting...");
exit(EXIT_FAILURE);
}
// Start database thread if database is used
if(database && pthread_create( &DBthread, &attr, DB_thread, NULL ) != 0)
{
logg("Unable to open database thread. Exiting...");
exit(EXIT_FAILURE);
}
// Start thread that will stay in the background until garbage collection needs to be done
if(pthread_create( &GCthread, &attr, GC_thread, NULL ) != 0)
{
logg("Unable to open GC thread. Exiting...");
exit(EXIT_FAILURE);
}
// Start thread that will stay in the background until host names needs to be resolved
if(pthread_create( &DNSclientthread, &attr, DNSclient_thread, NULL ) != 0)
{
logg("Unable to open DNS client thread. Exiting...");
exit(EXIT_FAILURE);
}
// Chown files if FTL started as user root but a dnsmasq config option
// states to run as a different user/group (e.g. "nobody")
if(ent_pw != NULL && getuid() == 0)
{
if(chown(FTLfiles.log, ent_pw->pw_uid, ent_pw->pw_gid) == -1)
logg("Setting ownership (%i:%i) of %s failed: %s (%i)", ent_pw->pw_uid, ent_pw->pw_gid, FTLfiles.log, strerror(errno), errno);
if(database && chown(FTLfiles.db, ent_pw->pw_uid, ent_pw->pw_gid) == -1)
logg("Setting ownership (%i:%i) of %s failed: %s (%i)", ent_pw->pw_uid, ent_pw->pw_gid, FTLfiles.db, strerror(errno), errno);
}
}
// int cache_inserted, cache_live_freed are defined in dnsmasq/cache.c
extern int cache_inserted, cache_live_freed;
void getCacheInformation(int *sock)
{
ssend(*sock,"cache-size: %i\ncache-live-freed: %i\ncache-inserted: %i\n",
daemon->cachesize, cache_live_freed, cache_inserted);
// cache-size is obvious
// It means the resolver handled <cache-inserted> names lookups that needed to be sent to
// upstream severes and that <cache-live-freed> was thrown out of the cache
// before reaching the end of its time-to-live, to make room for a newer name.
// For <cache-live-freed>, smaller is better.
// New queries are always cached. If the cache is full with entries
// which haven't reached the end of their time-to-live, then the entry
// which hasn't been looked up for the longest time is evicted.
}
void FTL_forwarding_failed(struct server *server)
{
// Don't analyze anything if in PRIVACY_NOSTATS mode
if(config.privacylevel >= PRIVACY_NOSTATS)
return;
// Save that this query got forwarded to an upstream server
enable_thread_lock();
char dest[ADDRSTRLEN];
if(server->addr.sa.sa_family == AF_INET)
inet_ntop(AF_INET, &server->addr.in.sin_addr, dest, ADDRSTRLEN);
else
inet_ntop(AF_INET6, &server->addr.in6.sin6_addr, dest, ADDRSTRLEN);
// Convert forward to lower case
char *forward = strdup(dest);
strtolower(forward);
int forwardID = findForwardID(forward, false);
if(debug) logg("**** forwarding to %s (ID %i) failed", dest, forwardID);
forwarded[forwardID].failed++;
free(forward);
disable_thread_lock();
return;
}
unsigned long converttimeval(struct timeval time)
{
// Convert time from struct timeval into units
// of 10*milliseconds
return time.tv_sec*10000 + time.tv_usec/100;
}
// This subroutine prepares IPv4 and IPv6 addresses for blocking queries depending on the configured blocking mode
static void prepare_blocking_mode(struct all_addr *addr4, struct all_addr *addr6, bool *has_IPv4, bool *has_IPv6)
{
char *a=NULL;
// Prepare IPv4 entry
char *IPv4addr;
if(config.blockingmode == MODE_IP || config.blockingmode == MODE_IP_NODATA_AAAA)
{
// Read IPv4 address for host entries from setupVars.conf
IPv4addr = read_setupVarsconf("IPV4_ADDRESS");
}
else
{
IPv4addr = "0.0.0.0";
}
if(IPv4addr != NULL)
{
// Strip off everything at the end of the IP (CIDR might be there)
a=IPv4addr; for(;*a;a++) if(*a == '/') *a = 0;
// Prepare IPv4 address for records