/
query.c
12184 lines (10885 loc) · 329 KB
/
query.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/*! \file */
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <isc/hex.h>
#include <isc/mem.h>
#include <isc/once.h>
#include <isc/print.h>
#include <isc/random.h>
#include <isc/rwlock.h>
#include <isc/serial.h>
#include <isc/stats.h>
#include <isc/string.h>
#include <isc/thread.h>
#include <isc/util.h>
#include <dns/adb.h>
#include <dns/badcache.h>
#include <dns/byaddr.h>
#include <dns/cache.h>
#include <dns/db.h>
#include <dns/dlz.h>
#include <dns/dns64.h>
#include <dns/dnsrps.h>
#include <dns/dnssec.h>
#include <dns/events.h>
#include <dns/keytable.h>
#include <dns/message.h>
#include <dns/ncache.h>
#include <dns/nsec.h>
#include <dns/nsec3.h>
#include <dns/order.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatastruct.h>
#include <dns/rdatatype.h>
#include <dns/resolver.h>
#include <dns/result.h>
#include <dns/stats.h>
#include <dns/tkey.h>
#include <dns/types.h>
#include <dns/view.h>
#include <dns/zone.h>
#include <dns/zt.h>
#include <ns/client.h>
#include <ns/events.h>
#include <ns/hooks.h>
#include <ns/interfacemgr.h>
#include <ns/log.h>
#include <ns/server.h>
#include <ns/sortlist.h>
#include <ns/stats.h>
#include <ns/xfrout.h>
#if 0
/*
* It has been recommended that DNS64 be changed to return excluded
* AAAA addresses if DNS64 synthesis does not occur. This minimises
* the impact on the lookup results. While most DNS AAAA lookups are
* done to send IP packets to a host, not all of them are and filtering
* excluded addresses has a negative impact on those uses.
*/
#define dns64_bis_return_excluded_addresses 1
#endif /* if 0 */
/*%
* Maximum number of chained queries before we give up
* to prevent CNAME loops.
*/
#define MAX_RESTARTS 16
#define QUERY_ERROR(qctx, r) \
do { \
(qctx)->result = r; \
(qctx)->want_restart = false; \
(qctx)->line = __LINE__; \
} while (0)
/*% Partial answer? */
#define PARTIALANSWER(c) \
(((c)->query.attributes & NS_QUERYATTR_PARTIALANSWER) != 0)
/*% Use Cache? */
#define USECACHE(c) (((c)->query.attributes & NS_QUERYATTR_CACHEOK) != 0)
/*% Recursion OK? */
#define RECURSIONOK(c) (((c)->query.attributes & NS_QUERYATTR_RECURSIONOK) != 0)
/*% Recursing? */
#define RECURSING(c) (((c)->query.attributes & NS_QUERYATTR_RECURSING) != 0)
/*% Want Recursion? */
#define WANTRECURSION(c) \
(((c)->query.attributes & NS_QUERYATTR_WANTRECURSION) != 0)
/*% Is TCP? */
#define TCP(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
/*% Want DNSSEC? */
#define WANTDNSSEC(c) (((c)->attributes & NS_CLIENTATTR_WANTDNSSEC) != 0)
/*% Want WANTAD? */
#define WANTAD(c) (((c)->attributes & NS_CLIENTATTR_WANTAD) != 0)
/*% Client presented a valid COOKIE. */
#define HAVECOOKIE(c) (((c)->attributes & NS_CLIENTATTR_HAVECOOKIE) != 0)
/*% Client presented a COOKIE. */
#define WANTCOOKIE(c) (((c)->attributes & NS_CLIENTATTR_WANTCOOKIE) != 0)
/*% Client presented a CLIENT-SUBNET option. */
#define HAVEECS(c) (((c)->attributes & NS_CLIENTATTR_HAVEECS) != 0)
/*% No authority? */
#define NOAUTHORITY(c) (((c)->query.attributes & NS_QUERYATTR_NOAUTHORITY) != 0)
/*% No additional? */
#define NOADDITIONAL(c) \
(((c)->query.attributes & NS_QUERYATTR_NOADDITIONAL) != 0)
/*% Secure? */
#define SECURE(c) (((c)->query.attributes & NS_QUERYATTR_SECURE) != 0)
/*% DNS64 A lookup? */
#define DNS64(c) (((c)->query.attributes & NS_QUERYATTR_DNS64) != 0)
#define DNS64EXCLUDE(c) \
(((c)->query.attributes & NS_QUERYATTR_DNS64EXCLUDE) != 0)
#define REDIRECT(c) (((c)->query.attributes & NS_QUERYATTR_REDIRECT) != 0)
/*% Was the client already sent a response? */
#define QUERY_ANSWERED(q) (((q)->attributes & NS_QUERYATTR_ANSWERED) != 0)
/*% Have we already processed an answer via stale-answer-client-timeout? */
#define QUERY_STALEPENDING(q) \
(((q)->attributes & NS_QUERYATTR_STALEPENDING) != 0)
/*% Does the query allow stale data in the response? */
#define QUERY_STALEOK(q) (((q)->attributes & NS_QUERYATTR_STALEOK) != 0)
/*% Does the query wants to check for stale RRset due to a timeout? */
#define QUERY_STALETIMEOUT(q) (((q)->dboptions & DNS_DBFIND_STALETIMEOUT) != 0)
/*% Does the rdataset 'r' have an attached 'No QNAME Proof'? */
#define NOQNAME(r) (((r)->attributes & DNS_RDATASETATTR_NOQNAME) != 0)
/*% Does the rdataset 'r' contain a stale answer? */
#define STALE(r) (((r)->attributes & DNS_RDATASETATTR_STALE) != 0)
/*% Does the rdataset 'r' is stale and within stale-refresh-time? */
#define STALE_WINDOW(r) (((r)->attributes & DNS_RDATASETATTR_STALE_WINDOW) != 0)
#ifdef WANT_QUERYTRACE
static inline void
client_trace(ns_client_t *client, int level, const char *message) {
if (client != NULL && client->query.qname != NULL) {
if (isc_log_wouldlog(ns_lctx, level)) {
char qbuf[DNS_NAME_FORMATSIZE];
char tbuf[DNS_RDATATYPE_FORMATSIZE];
dns_name_format(client->query.qname, qbuf,
sizeof(qbuf));
dns_rdatatype_format(client->query.qtype, tbuf,
sizeof(tbuf));
isc_log_write(ns_lctx, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_QUERY, level,
"query client=%p thread=0x%" PRIxPTR
"(%s/%s): %s",
client, isc_thread_self(), qbuf, tbuf,
message);
}
} else {
isc_log_write(ns_lctx, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_QUERY, level,
"query client=%p thread=0x%" PRIxPTR
"(<unknown-query>): %s",
client, isc_thread_self(), message);
}
}
#define CTRACE(l, m) client_trace(client, l, m)
#define CCTRACE(l, m) client_trace(qctx->client, l, m)
#else /* ifdef WANT_QUERYTRACE */
#define CTRACE(l, m) ((void)m)
#define CCTRACE(l, m) ((void)m)
#endif /* WANT_QUERYTRACE */
#define DNS_GETDB_NOEXACT 0x01U
#define DNS_GETDB_NOLOG 0x02U
#define DNS_GETDB_PARTIAL 0x04U
#define DNS_GETDB_IGNOREACL 0x08U
#define DNS_GETDB_STALEFIRST 0X0CU
#define PENDINGOK(x) (((x)&DNS_DBFIND_PENDINGOK) != 0)
#define SFCACHE_CDFLAG 0x1
/*
* SAVE and RESTORE have the same semantics as:
*
* foo_attach(b, &a);
* foo_detach(&b);
*
* without the locking and magic testing.
*
* We use the names SAVE and RESTORE to show the operation being performed,
* even though the two macros are identical.
*/
#define SAVE(a, b) \
do { \
INSIST(a == NULL); \
a = b; \
b = NULL; \
} while (0)
#define RESTORE(a, b) SAVE(a, b)
static bool
validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
static void
query_findclosestnsec3(dns_name_t *qname, dns_db_t *db,
dns_dbversion_t *version, ns_client_t *client,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
dns_name_t *fname, bool exact, dns_name_t *found);
static inline void
log_queryerror(ns_client_t *client, isc_result_t result, int line, int level);
static void
rpz_st_clear(ns_client_t *client);
static bool
rpz_ck_dnssec(ns_client_t *client, isc_result_t qresult,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
static void
log_noexistnodata(void *val, int level, const char *fmt, ...)
ISC_FORMAT_PRINTF(3, 4);
static isc_result_t
query_addanswer(query_ctx_t *qctx);
static isc_result_t
query_prepare_delegation_response(query_ctx_t *qctx);
/*
* Return the hooktable in use with 'qctx', or if there isn't one
* set, return the default hooktable.
*/
static inline ns_hooktable_t *
get_hooktab(query_ctx_t *qctx) {
if (qctx == NULL || qctx->view == NULL || qctx->view->hooktable == NULL)
{
return (ns__hook_table);
}
return (qctx->view->hooktable);
}
/*
* Call the specified hook function in every configured module that implements
* that function. If any hook function returns NS_HOOK_RETURN, we
* set 'result' and terminate processing by jumping to the 'cleanup' tag.
*
* (Note that a hook function may set the 'result' to ISC_R_SUCCESS but
* still terminate processing within the calling function. That's why this
* is a macro instead of an inline function; it needs to be able to use
* 'goto cleanup' regardless of the return value.)
*/
#define CALL_HOOK(_id, _qctx) \
do { \
isc_result_t _res = result; \
ns_hooktable_t *_tab = get_hooktab(_qctx); \
ns_hook_t *_hook; \
_hook = ISC_LIST_HEAD((*_tab)[_id]); \
while (_hook != NULL) { \
ns_hook_action_t _func = _hook->action; \
void *_data = _hook->action_data; \
INSIST(_func != NULL); \
switch (_func(_qctx, _data, &_res)) { \
case NS_HOOK_CONTINUE: \
_hook = ISC_LIST_NEXT(_hook, link); \
break; \
case NS_HOOK_RETURN: \
result = _res; \
goto cleanup; \
default: \
INSIST(0); \
} \
} \
} while (false)
/*
* Call the specified hook function in every configured module that
* implements that function. All modules are called; hook function return
* codes are ignored. This is intended for use with initialization and
* destruction calls which *must* run in every configured module.
*
* (This could be implemented as an inline void function, but is left as a
* macro for symmetry with CALL_HOOK above.)
*/
#define CALL_HOOK_NORETURN(_id, _qctx) \
do { \
isc_result_t _res; \
ns_hooktable_t *_tab = get_hooktab(_qctx); \
ns_hook_t *_hook; \
_hook = ISC_LIST_HEAD((*_tab)[_id]); \
while (_hook != NULL) { \
ns_hook_action_t _func = _hook->action; \
void *_data = _hook->action_data; \
INSIST(_func != NULL); \
_func(_qctx, _data, &_res); \
_hook = ISC_LIST_NEXT(_hook, link); \
} \
} while (false)
/*
* The functions defined below implement the query logic that previously lived
* in the single very complex function query_find(). The query_ctx_t structure
* defined in <ns/query.h> maintains state from function to function. The call
* flow for the general query processing algorithm is described below:
*
* 1. Set up query context and other resources for a client
* query (query_setup())
*
* 2. Start the search (ns__query_start())
*
* 3. Identify authoritative data sources which may have an answer;
* search them (query_lookup()). If an answer is found, go to 7.
*
* 4. If recursion or cache access are allowed, search the cache
* (query_lookup() again, using the cache database) to find a better
* answer. If an answer is found, go to 7.
*
* 5. If recursion is allowed, begin recursion (ns_query_recurse()).
* Go to 15 to clean up this phase of the query. When recursion
* is complete, processing will resume at 6.
*
* 6. Resume from recursion; set up query context for resumed processing.
*
* 7. Determine what sort of answer we've found (query_gotanswer())
* and call other functions accordingly:
* - not found (auth or cache), go to 8
* - delegation, go to 9
* - no such domain (auth), go to 10
* - empty answer (auth), go to 11
* - negative response (cache), go to 12
* - answer found, go to 13
*
* 8. The answer was not found in the database (query_notfound().
* Set up a referral and go to 9.
*
* 9. Handle a delegation response (query_delegation()). If we need
* to and are allowed to recurse (query_delegation_recurse()), go to 5,
* otherwise go to 15 to clean up and return the delegation to the client.
*
* 10. No such domain (query_nxdomain()). Attempt redirection; if
* unsuccessful, add authority section records (query_addsoa(),
* query_addauth()), then go to 15 to return NXDOMAIN to client.
*
* 11. Empty answer (query_nodata()). Add authority section records
* (query_addsoa(), query_addauth()) and signatures if authoritative
* (query_sign_nodata()) then go to 15 and return
* NOERROR/ANCOUNT=0 to client.
*
* 12. No such domain or empty answer returned from cache (query_ncache()).
* Set response code appropriately, go to 11.
*
* 13. Prepare a response (query_prepresponse()) and then fill it
* appropriately (query_respond(), or for type ANY,
* query_respond_any()).
*
* 14. If a restart is needed due to CNAME/DNAME chaining, go to 2.
*
* 15. Clean up resources. If recursing, stop and wait for the event
* handler to be called back (step 6). If an answer is ready,
* return it to the client.
*
* (XXX: This description omits several special cases including
* DNS64, RPZ, RRL, and the SERVFAIL cache. It also doesn't discuss
* plugins.)
*/
static void
query_trace(query_ctx_t *qctx);
static void
qctx_init(ns_client_t *client, dns_fetchevent_t **eventp, dns_rdatatype_t qtype,
query_ctx_t *qctx);
static isc_result_t
query_setup(ns_client_t *client, dns_rdatatype_t qtype);
static isc_result_t
query_lookup(query_ctx_t *qctx);
static void
fetch_callback(isc_task_t *task, isc_event_t *event);
static void
recparam_update(ns_query_recparam_t *param, dns_rdatatype_t qtype,
const dns_name_t *qname, const dns_name_t *qdomain);
static isc_result_t
query_resume(query_ctx_t *qctx);
static isc_result_t
query_checkrrl(query_ctx_t *qctx, isc_result_t result);
static isc_result_t
query_checkrpz(query_ctx_t *qctx, isc_result_t result);
static isc_result_t
query_rpzcname(query_ctx_t *qctx, dns_name_t *cname);
static isc_result_t
query_gotanswer(query_ctx_t *qctx, isc_result_t result);
static void
query_addnoqnameproof(query_ctx_t *qctx);
static isc_result_t
query_respond_any(query_ctx_t *qctx);
static isc_result_t
query_respond(query_ctx_t *qctx);
static isc_result_t
query_dns64(query_ctx_t *qctx);
static void
query_filter64(query_ctx_t *qctx);
static isc_result_t
query_notfound(query_ctx_t *qctx);
static isc_result_t
query_zone_delegation(query_ctx_t *qctx);
static isc_result_t
query_delegation(query_ctx_t *qctx);
static isc_result_t
query_delegation_recurse(query_ctx_t *qctx);
static void
query_addds(query_ctx_t *qctx);
static isc_result_t
query_nodata(query_ctx_t *qctx, isc_result_t result);
static isc_result_t
query_sign_nodata(query_ctx_t *qctx);
static void
query_addnxrrsetnsec(query_ctx_t *qctx);
static isc_result_t
query_nxdomain(query_ctx_t *qctx, isc_result_t res);
static isc_result_t
query_redirect(query_ctx_t *qctx);
static isc_result_t
query_ncache(query_ctx_t *qctx, isc_result_t result);
static isc_result_t
query_coveringnsec(query_ctx_t *qctx);
static isc_result_t
query_zerottl_refetch(query_ctx_t *qctx);
static isc_result_t
query_cname(query_ctx_t *qctx);
static isc_result_t
query_dname(query_ctx_t *qctx);
static isc_result_t
query_addcname(query_ctx_t *qctx, dns_trust_t trust, dns_ttl_t ttl);
static isc_result_t
query_prepresponse(query_ctx_t *qctx);
static isc_result_t
query_addsoa(query_ctx_t *qctx, unsigned int override_ttl,
dns_section_t section);
static isc_result_t
query_addns(query_ctx_t *qctx);
static void
query_addbestns(query_ctx_t *qctx);
static void
query_addwildcardproof(query_ctx_t *qctx, bool ispositive, bool nodata);
static void
query_addauth(query_ctx_t *qctx);
static void
query_clear_stale(ns_client_t *client);
/*
* Increment query statistics counters.
*/
static inline void
inc_stats(ns_client_t *client, isc_statscounter_t counter) {
dns_zone_t *zone = client->query.authzone;
dns_rdatatype_t qtype;
dns_rdataset_t *rdataset;
isc_stats_t *zonestats;
dns_stats_t *querystats = NULL;
ns_stats_increment(client->sctx->nsstats, counter);
if (zone == NULL) {
return;
}
/* Do regular response type stats */
zonestats = dns_zone_getrequeststats(zone);
if (zonestats != NULL) {
isc_stats_increment(zonestats, counter);
}
/* Do query type statistics
*
* We only increment per-type if we're using the authoritative
* answer counter, preventing double-counting.
*/
if (counter == ns_statscounter_authans) {
querystats = dns_zone_getrcvquerystats(zone);
if (querystats != NULL) {
rdataset = ISC_LIST_HEAD(client->query.qname->list);
if (rdataset != NULL) {
qtype = rdataset->type;
dns_rdatatypestats_increment(querystats, qtype);
}
}
}
}
static void
query_send(ns_client_t *client) {
isc_statscounter_t counter;
if ((client->message->flags & DNS_MESSAGEFLAG_AA) == 0) {
inc_stats(client, ns_statscounter_nonauthans);
} else {
inc_stats(client, ns_statscounter_authans);
}
if (client->message->rcode == dns_rcode_noerror) {
dns_section_t answer = DNS_SECTION_ANSWER;
if (ISC_LIST_EMPTY(client->message->sections[answer])) {
if (client->query.isreferral) {
counter = ns_statscounter_referral;
} else {
counter = ns_statscounter_nxrrset;
}
} else {
counter = ns_statscounter_success;
}
} else if (client->message->rcode == dns_rcode_nxdomain) {
counter = ns_statscounter_nxdomain;
} else if (client->message->rcode == dns_rcode_badcookie) {
counter = ns_statscounter_badcookie;
} else { /* We end up here in case of YXDOMAIN, and maybe others */
counter = ns_statscounter_failure;
}
inc_stats(client, counter);
ns_client_send(client);
if (!client->nodetach) {
isc_nmhandle_detach(&client->reqhandle);
}
}
static void
query_error(ns_client_t *client, isc_result_t result, int line) {
int loglevel = ISC_LOG_DEBUG(3);
switch (dns_result_torcode(result)) {
case dns_rcode_servfail:
loglevel = ISC_LOG_DEBUG(1);
inc_stats(client, ns_statscounter_servfail);
break;
case dns_rcode_formerr:
inc_stats(client, ns_statscounter_formerr);
break;
default:
inc_stats(client, ns_statscounter_failure);
break;
}
if ((client->sctx->options & NS_SERVER_LOGQUERIES) != 0) {
loglevel = ISC_LOG_INFO;
}
log_queryerror(client, result, line, loglevel);
ns_client_error(client, result);
if (!client->nodetach) {
isc_nmhandle_detach(&client->reqhandle);
}
}
static void
query_next(ns_client_t *client, isc_result_t result) {
if (result == DNS_R_DUPLICATE) {
inc_stats(client, ns_statscounter_duplicate);
} else if (result == DNS_R_DROP) {
inc_stats(client, ns_statscounter_dropped);
} else {
inc_stats(client, ns_statscounter_failure);
}
ns_client_drop(client, result);
if (!client->nodetach) {
isc_nmhandle_detach(&client->reqhandle);
}
}
static inline void
query_freefreeversions(ns_client_t *client, bool everything) {
ns_dbversion_t *dbversion, *dbversion_next;
unsigned int i;
for (dbversion = ISC_LIST_HEAD(client->query.freeversions), i = 0;
dbversion != NULL; dbversion = dbversion_next, i++)
{
dbversion_next = ISC_LIST_NEXT(dbversion, link);
/*
* If we're not freeing everything, we keep the first three
* dbversions structures around.
*/
if (i > 3 || everything) {
ISC_LIST_UNLINK(client->query.freeversions, dbversion,
link);
isc_mem_put(client->mctx, dbversion,
sizeof(*dbversion));
}
}
}
void
ns_query_cancel(ns_client_t *client) {
REQUIRE(NS_CLIENT_VALID(client));
LOCK(&client->query.fetchlock);
if (client->query.fetch != NULL) {
dns_resolver_cancelfetch(client->query.fetch);
client->query.fetch = NULL;
}
if (client->query.hookactx != NULL) {
client->query.hookactx->cancel(client->query.hookactx);
client->query.hookactx = NULL;
}
UNLOCK(&client->query.fetchlock);
}
static inline void
query_reset(ns_client_t *client, bool everything) {
isc_buffer_t *dbuf, *dbuf_next;
ns_dbversion_t *dbversion, *dbversion_next;
CTRACE(ISC_LOG_DEBUG(3), "query_reset");
/*%
* Reset the query state of a client to its default state.
*/
/*
* Cancel the fetch if it's running.
*/
ns_query_cancel(client);
/*
* Cleanup any active versions.
*/
for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
dbversion != NULL; dbversion = dbversion_next)
{
dbversion_next = ISC_LIST_NEXT(dbversion, link);
dns_db_closeversion(dbversion->db, &dbversion->version, false);
dns_db_detach(&dbversion->db);
ISC_LIST_INITANDAPPEND(client->query.freeversions, dbversion,
link);
}
ISC_LIST_INIT(client->query.activeversions);
if (client->query.authdb != NULL) {
dns_db_detach(&client->query.authdb);
}
if (client->query.authzone != NULL) {
dns_zone_detach(&client->query.authzone);
}
if (client->query.dns64_aaaa != NULL) {
ns_client_putrdataset(client, &client->query.dns64_aaaa);
}
if (client->query.dns64_sigaaaa != NULL) {
ns_client_putrdataset(client, &client->query.dns64_sigaaaa);
}
if (client->query.dns64_aaaaok != NULL) {
isc_mem_put(client->mctx, client->query.dns64_aaaaok,
client->query.dns64_aaaaoklen * sizeof(bool));
client->query.dns64_aaaaok = NULL;
client->query.dns64_aaaaoklen = 0;
}
ns_client_putrdataset(client, &client->query.redirect.rdataset);
ns_client_putrdataset(client, &client->query.redirect.sigrdataset);
if (client->query.redirect.db != NULL) {
if (client->query.redirect.node != NULL) {
dns_db_detachnode(client->query.redirect.db,
&client->query.redirect.node);
}
dns_db_detach(&client->query.redirect.db);
}
if (client->query.redirect.zone != NULL) {
dns_zone_detach(&client->query.redirect.zone);
}
query_freefreeversions(client, everything);
for (dbuf = ISC_LIST_HEAD(client->query.namebufs); dbuf != NULL;
dbuf = dbuf_next)
{
dbuf_next = ISC_LIST_NEXT(dbuf, link);
if (dbuf_next != NULL || everything) {
ISC_LIST_UNLINK(client->query.namebufs, dbuf, link);
isc_buffer_free(&dbuf);
}
}
if (client->query.restarts > 0) {
/*
* client->query.qname was dynamically allocated.
*/
dns_message_puttempname(client->message, &client->query.qname);
}
client->query.qname = NULL;
client->query.attributes = (NS_QUERYATTR_RECURSIONOK |
NS_QUERYATTR_CACHEOK | NS_QUERYATTR_SECURE);
client->query.restarts = 0;
client->query.timerset = false;
if (client->query.rpz_st != NULL) {
rpz_st_clear(client);
if (everything) {
INSIST(client->query.rpz_st->rpsdb == NULL);
isc_mem_put(client->mctx, client->query.rpz_st,
sizeof(*client->query.rpz_st));
client->query.rpz_st = NULL;
}
}
client->query.origqname = NULL;
client->query.dboptions = 0;
client->query.fetchoptions = 0;
client->query.gluedb = NULL;
client->query.authdbset = false;
client->query.isreferral = false;
client->query.dns64_options = 0;
client->query.dns64_ttl = UINT32_MAX;
recparam_update(&client->query.recparam, 0, NULL, NULL);
client->query.root_key_sentinel_keyid = 0;
client->query.root_key_sentinel_is_ta = false;
client->query.root_key_sentinel_not_ta = false;
}
static void
query_cleanup(ns_client_t *client) {
query_reset(client, false);
}
void
ns_query_free(ns_client_t *client) {
REQUIRE(NS_CLIENT_VALID(client));
query_reset(client, true);
}
isc_result_t
ns_query_init(ns_client_t *client) {
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(NS_CLIENT_VALID(client));
ISC_LIST_INIT(client->query.namebufs);
ISC_LIST_INIT(client->query.activeversions);
ISC_LIST_INIT(client->query.freeversions);
client->query.restarts = 0;
client->query.timerset = false;
client->query.rpz_st = NULL;
client->query.qname = NULL;
/*
* This mutex is destroyed when the client is destroyed in
* exit_check().
*/
isc_mutex_init(&client->query.fetchlock);
client->query.fetch = NULL;
client->query.prefetch = NULL;
client->query.authdb = NULL;
client->query.authzone = NULL;
client->query.authdbset = false;
client->query.isreferral = false;
client->query.dns64_aaaa = NULL;
client->query.dns64_sigaaaa = NULL;
client->query.dns64_aaaaok = NULL;
client->query.dns64_aaaaoklen = 0;
client->query.redirect.db = NULL;
client->query.redirect.node = NULL;
client->query.redirect.zone = NULL;
client->query.redirect.qtype = dns_rdatatype_none;
client->query.redirect.result = ISC_R_SUCCESS;
client->query.redirect.rdataset = NULL;
client->query.redirect.sigrdataset = NULL;
client->query.redirect.authoritative = false;
client->query.redirect.is_zone = false;
client->query.redirect.fname =
dns_fixedname_initname(&client->query.redirect.fixed);
query_reset(client, false);
ns_client_newdbversion(client, 3);
ns_client_newnamebuf(client);
return (result);
}
/*%
* Check if 'client' is allowed to query the cache of its associated view.
* Unless 'options' has DNS_GETDB_NOLOG set, log the result of cache ACL
* evaluation using the appropriate level, along with 'name' and 'qtype'.
*
* The cache ACL is only evaluated once for each client and then the result is
* cached: if NS_QUERYATTR_CACHEACLOKVALID is set in client->query.attributes,
* cache ACL evaluation has already been performed. The evaluation result is
* also stored in client->query.attributes: if NS_QUERYATTR_CACHEACLOK is set,
* the client is allowed cache access.
*
* Returns:
*
*\li #ISC_R_SUCCESS 'client' is allowed to access cache
*\li #DNS_R_REFUSED 'client' is not allowed to access cache
*/
static isc_result_t
query_checkcacheaccess(ns_client_t *client, const dns_name_t *name,
dns_rdatatype_t qtype, unsigned int options) {
isc_result_t result;
if ((client->query.attributes & NS_QUERYATTR_CACHEACLOKVALID) == 0) {
/*
* The view's cache ACLs have not yet been evaluated.
* Do it now. Both allow-query-cache and
* allow-query-cache-on must be satsified.
*/
bool log = ((options & DNS_GETDB_NOLOG) == 0);
char msg[NS_CLIENT_ACLMSGSIZE("query (cache)")];
result = ns_client_checkaclsilent(client, NULL,
client->view->cacheacl, true);
if (result == ISC_R_SUCCESS) {
result = ns_client_checkaclsilent(
client, &client->destaddr,
client->view->cacheonacl, true);
}
if (result == ISC_R_SUCCESS) {
/*
* We were allowed by the "allow-query-cache" ACL.
*/
client->query.attributes |= NS_QUERYATTR_CACHEACLOK;
if (log && isc_log_wouldlog(ns_lctx, ISC_LOG_DEBUG(3)))
{
ns_client_aclmsg("query (cache)", name, qtype,
client->view->rdclass, msg,
sizeof(msg));
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_QUERY,
ISC_LOG_DEBUG(3), "%s approved",
msg);
}
} else if (log) {
/*
* We were denied by the "allow-query-cache" ACL.
* There is no need to clear NS_QUERYATTR_CACHEACLOK
* since it is cleared by query_reset(), before query
* processing starts.
*/
ns_client_aclmsg("query (cache)", name, qtype,
client->view->rdclass, msg,
sizeof(msg));
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_QUERY, ISC_LOG_INFO,
"%s denied", msg);
}
/*
* Evaluation has been finished; make sure we will just consult
* NS_QUERYATTR_CACHEACLOK for this client from now on.
*/
client->query.attributes |= NS_QUERYATTR_CACHEACLOKVALID;
}
return ((client->query.attributes & NS_QUERYATTR_CACHEACLOK) != 0
? ISC_R_SUCCESS
: DNS_R_REFUSED);
}
static inline isc_result_t
query_validatezonedb(ns_client_t *client, const dns_name_t *name,
dns_rdatatype_t qtype, unsigned int options,
dns_zone_t *zone, dns_db_t *db,
dns_dbversion_t **versionp) {
isc_result_t result;
dns_acl_t *queryacl, *queryonacl;
ns_dbversion_t *dbversion;
REQUIRE(zone != NULL);
REQUIRE(db != NULL);
/*
* Mirror zone data is treated as cache data.
*/
if (dns_zone_gettype(zone) == dns_zone_mirror) {
return (query_checkcacheaccess(client, name, qtype, options));
}
/*
* This limits our searching to the zone where the first name
* (the query target) was looked for. This prevents following
* CNAMES or DNAMES into other zones and prevents returning
* additional data from other zones. This does not apply if we're
* answering a query where recursion is requested and allowed.
*/
if (client->query.rpz_st == NULL &&
!(WANTRECURSION(client) && RECURSIONOK(client)) &&
client->query.authdbset && db != client->query.authdb)
{
return (DNS_R_REFUSED);
}
/*
* Non recursive query to a static-stub zone is prohibited; its
* zone content is not public data, but a part of local configuration
* and should not be disclosed.
*/
if (dns_zone_gettype(zone) == dns_zone_staticstub &&
!RECURSIONOK(client)) {
return (DNS_R_REFUSED);
}
/*
* If the zone has an ACL, we'll check it, otherwise
* we use the view's "allow-query" ACL. Each ACL is only checked
* once per query.
*
* Also, get the database version to use.
*/
/*
* Get the current version of this database.
*/
dbversion = ns_client_findversion(client, db);
if (dbversion == NULL) {
CTRACE(ISC_LOG_ERROR, "unable to get db version");
return (DNS_R_SERVFAIL);
}
if ((options & DNS_GETDB_IGNOREACL) != 0) {
goto approved;
}
if (dbversion->acl_checked) {
if (!dbversion->queryok) {
return (DNS_R_REFUSED);
}
goto approved;
}
queryacl = dns_zone_getqueryacl(zone);
if (queryacl == NULL) {
queryacl = client->view->queryacl;
if ((client->query.attributes & NS_QUERYATTR_QUERYOKVALID) != 0)
{
/*
* We've evaluated the view's queryacl already. If
* NS_QUERYATTR_QUERYOK is set, then the client is
* allowed to make queries, otherwise the query should
* be refused.
*/
dbversion->acl_checked = true;