2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: query.c,v 1.257.18.40 2007/09/26 03:08:14 each Exp $ */
30 #include <dns/byaddr.h>
35 #include <dns/dnssec.h>
36 #include <dns/events.h>
37 #include <dns/message.h>
38 #include <dns/ncache.h>
39 #include <dns/order.h>
40 #include <dns/rdata.h>
41 #include <dns/rdataclass.h>
42 #include <dns/rdatalist.h>
43 #include <dns/rdataset.h>
44 #include <dns/rdatasetiter.h>
45 #include <dns/rdatastruct.h>
46 #include <dns/rdatatype.h>
47 #include <dns/resolver.h>
48 #include <dns/result.h>
49 #include <dns/stats.h>
55 #include <named/client.h>
56 #include <named/log.h>
57 #include <named/server.h>
58 #include <named/sortlist.h>
59 #include <named/xfrout.h>
61 /*% Partial answer? */
62 #define PARTIALANSWER(c) (((c)->query.attributes & \
63 NS_QUERYATTR_PARTIALANSWER) != 0)
65 #define USECACHE(c) (((c)->query.attributes & \
66 NS_QUERYATTR_CACHEOK) != 0)
68 #define RECURSIONOK(c) (((c)->query.attributes & \
69 NS_QUERYATTR_RECURSIONOK) != 0)
71 #define RECURSING(c) (((c)->query.attributes & \
72 NS_QUERYATTR_RECURSING) != 0)
74 #define CACHEGLUEOK(c) (((c)->query.attributes & \
75 NS_QUERYATTR_CACHEGLUEOK) != 0)
76 /*% Want Recursion? */
77 #define WANTRECURSION(c) (((c)->query.attributes & \
78 NS_QUERYATTR_WANTRECURSION) != 0)
80 #define WANTDNSSEC(c) (((c)->attributes & \
81 NS_CLIENTATTR_WANTDNSSEC) != 0)
83 #define NOAUTHORITY(c) (((c)->query.attributes & \
84 NS_QUERYATTR_NOAUTHORITY) != 0)
86 #define NOADDITIONAL(c) (((c)->query.attributes & \
87 NS_QUERYATTR_NOADDITIONAL) != 0)
89 #define SECURE(c) (((c)->query.attributes & \
90 NS_QUERYATTR_SECURE) != 0)
93 #define CTRACE(m) isc_log_write(ns_g_lctx, \
94 NS_LOGCATEGORY_CLIENT, \
97 "client %p: %s", client, (m))
98 #define QTRACE(m) isc_log_write(ns_g_lctx, \
99 NS_LOGCATEGORY_GENERAL, \
100 NS_LOGMODULE_QUERY, \
102 "query %p: %s", query, (m))
104 #define CTRACE(m) ((void)m)
105 #define QTRACE(m) ((void)m)
108 #define DNS_GETDB_NOEXACT 0x01U
109 #define DNS_GETDB_NOLOG 0x02U
110 #define DNS_GETDB_PARTIAL 0x04U
112 typedef struct client_additionalctx
{
114 dns_rdataset_t
*rdataset
;
115 } client_additionalctx_t
;
118 query_find(ns_client_t
*client
, dns_fetchevent_t
*event
, dns_rdatatype_t qtype
);
121 validate(ns_client_t
*client
, dns_db_t
*db
, dns_name_t
*name
,
122 dns_rdataset_t
*rdataset
, dns_rdataset_t
*sigrdataset
);
125 * Increment query statistics counters.
128 inc_stats(ns_client_t
*client
, dns_statscounter_t counter
) {
129 dns_zone_t
*zone
= client
->query
.authzone
;
131 REQUIRE(counter
< DNS_STATS_NCOUNTERS
);
133 ns_g_server
->querystats
[counter
]++;
136 isc_uint64_t
*zonestats
= dns_zone_getstatscounters(zone
);
137 if (zonestats
!= NULL
)
138 zonestats
[counter
]++;
143 query_send(ns_client_t
*client
) {
144 dns_statscounter_t counter
;
145 if (client
->message
->rcode
== dns_rcode_noerror
) {
146 if (ISC_LIST_EMPTY(client
->message
->sections
[DNS_SECTION_ANSWER
])) {
147 if (client
->query
.isreferral
) {
148 counter
= dns_statscounter_referral
;
150 counter
= dns_statscounter_nxrrset
;
153 counter
= dns_statscounter_success
;
155 } else if (client
->message
->rcode
== dns_rcode_nxdomain
) {
156 counter
= dns_statscounter_nxdomain
;
158 /* We end up here in case of YXDOMAIN, and maybe others */
159 counter
= dns_statscounter_failure
;
161 inc_stats(client
, counter
);
162 ns_client_send(client
);
166 query_error(ns_client_t
*client
, isc_result_t result
) {
167 inc_stats(client
, dns_statscounter_failure
);
168 ns_client_error(client
, result
);
172 query_next(ns_client_t
*client
, isc_result_t result
) {
173 if (result
== DNS_R_DUPLICATE
)
174 inc_stats(client
, dns_statscounter_duplicate
);
175 else if (result
== DNS_R_DROP
)
176 inc_stats(client
, dns_statscounter_dropped
);
178 inc_stats(client
, dns_statscounter_failure
);
179 ns_client_next(client
, result
);
183 query_freefreeversions(ns_client_t
*client
, isc_boolean_t everything
) {
184 ns_dbversion_t
*dbversion
, *dbversion_next
;
187 for (dbversion
= ISC_LIST_HEAD(client
->query
.freeversions
), i
= 0;
189 dbversion
= dbversion_next
, i
++)
191 dbversion_next
= ISC_LIST_NEXT(dbversion
, link
);
193 * If we're not freeing everything, we keep the first three
194 * dbversions structures around.
196 if (i
> 3 || everything
) {
197 ISC_LIST_UNLINK(client
->query
.freeversions
, dbversion
,
199 isc_mem_put(client
->mctx
, dbversion
,
206 ns_query_cancel(ns_client_t
*client
) {
207 LOCK(&client
->query
.fetchlock
);
208 if (client
->query
.fetch
!= NULL
) {
209 dns_resolver_cancelfetch(client
->query
.fetch
);
211 client
->query
.fetch
= NULL
;
213 UNLOCK(&client
->query
.fetchlock
);
217 query_reset(ns_client_t
*client
, isc_boolean_t everything
) {
218 isc_buffer_t
*dbuf
, *dbuf_next
;
219 ns_dbversion_t
*dbversion
, *dbversion_next
;
222 * Reset the query state of a client to its default state.
226 * Cancel the fetch if it's running.
228 ns_query_cancel(client
);
231 * Cleanup any active versions.
233 for (dbversion
= ISC_LIST_HEAD(client
->query
.activeversions
);
235 dbversion
= dbversion_next
) {
236 dbversion_next
= ISC_LIST_NEXT(dbversion
, link
);
237 dns_db_closeversion(dbversion
->db
, &dbversion
->version
,
239 dns_db_detach(&dbversion
->db
);
240 ISC_LIST_INITANDAPPEND(client
->query
.freeversions
,
243 ISC_LIST_INIT(client
->query
.activeversions
);
245 if (client
->query
.authdb
!= NULL
)
246 dns_db_detach(&client
->query
.authdb
);
247 if (client
->query
.authzone
!= NULL
)
248 dns_zone_detach(&client
->query
.authzone
);
250 query_freefreeversions(client
, everything
);
252 for (dbuf
= ISC_LIST_HEAD(client
->query
.namebufs
);
255 dbuf_next
= ISC_LIST_NEXT(dbuf
, link
);
256 if (dbuf_next
!= NULL
|| everything
) {
257 ISC_LIST_UNLINK(client
->query
.namebufs
, dbuf
, link
);
258 isc_buffer_free(&dbuf
);
262 if (client
->query
.restarts
> 0) {
264 * client->query.qname was dynamically allocated.
266 dns_message_puttempname(client
->message
,
267 &client
->query
.qname
);
269 client
->query
.qname
= NULL
;
270 client
->query
.attributes
= (NS_QUERYATTR_RECURSIONOK
|
271 NS_QUERYATTR_CACHEOK
|
272 NS_QUERYATTR_SECURE
);
273 client
->query
.restarts
= 0;
274 client
->query
.timerset
= ISC_FALSE
;
275 client
->query
.origqname
= NULL
;
276 client
->query
.qname
= NULL
;
277 client
->query
.dboptions
= 0;
278 client
->query
.fetchoptions
= 0;
279 client
->query
.gluedb
= NULL
;
280 client
->query
.authdbset
= ISC_FALSE
;
281 client
->query
.isreferral
= ISC_FALSE
;
285 query_next_callback(ns_client_t
*client
) {
286 query_reset(client
, ISC_FALSE
);
290 ns_query_free(ns_client_t
*client
) {
291 query_reset(client
, ISC_TRUE
);
294 static inline isc_result_t
295 query_newnamebuf(ns_client_t
*client
) {
299 CTRACE("query_newnamebuf");
301 * Allocate a name buffer.
305 result
= isc_buffer_allocate(client
->mctx
, &dbuf
, 1024);
306 if (result
!= ISC_R_SUCCESS
) {
307 CTRACE("query_newnamebuf: isc_buffer_allocate failed: done");
310 ISC_LIST_APPEND(client
->query
.namebufs
, dbuf
, link
);
312 CTRACE("query_newnamebuf: done");
313 return (ISC_R_SUCCESS
);
316 static inline isc_buffer_t
*
317 query_getnamebuf(ns_client_t
*client
) {
322 CTRACE("query_getnamebuf");
324 * Return a name buffer with space for a maximal name, allocating
325 * a new one if necessary.
328 if (ISC_LIST_EMPTY(client
->query
.namebufs
)) {
329 result
= query_newnamebuf(client
);
330 if (result
!= ISC_R_SUCCESS
) {
331 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
336 dbuf
= ISC_LIST_TAIL(client
->query
.namebufs
);
337 INSIST(dbuf
!= NULL
);
338 isc_buffer_availableregion(dbuf
, &r
);
339 if (r
.length
< 255) {
340 result
= query_newnamebuf(client
);
341 if (result
!= ISC_R_SUCCESS
) {
342 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
346 dbuf
= ISC_LIST_TAIL(client
->query
.namebufs
);
347 isc_buffer_availableregion(dbuf
, &r
);
348 INSIST(r
.length
>= 255);
350 CTRACE("query_getnamebuf: done");
355 query_keepname(ns_client_t
*client
, dns_name_t
*name
, isc_buffer_t
*dbuf
) {
358 CTRACE("query_keepname");
360 * 'name' is using space in 'dbuf', but 'dbuf' has not yet been
361 * adjusted to take account of that. We do the adjustment.
364 REQUIRE((client
->query
.attributes
& NS_QUERYATTR_NAMEBUFUSED
) != 0);
366 dns_name_toregion(name
, &r
);
367 isc_buffer_add(dbuf
, r
.length
);
368 dns_name_setbuffer(name
, NULL
);
369 client
->query
.attributes
&= ~NS_QUERYATTR_NAMEBUFUSED
;
373 query_releasename(ns_client_t
*client
, dns_name_t
**namep
) {
374 dns_name_t
*name
= *namep
;
377 * 'name' is no longer needed. Return it to our pool of temporary
378 * names. If it is using a name buffer, relinquish its exclusive
379 * rights on the buffer.
382 CTRACE("query_releasename");
383 if (dns_name_hasbuffer(name
)) {
384 INSIST((client
->query
.attributes
& NS_QUERYATTR_NAMEBUFUSED
)
386 client
->query
.attributes
&= ~NS_QUERYATTR_NAMEBUFUSED
;
388 dns_message_puttempname(client
->message
, namep
);
389 CTRACE("query_releasename: done");
392 static inline dns_name_t
*
393 query_newname(ns_client_t
*client
, isc_buffer_t
*dbuf
,
400 REQUIRE((client
->query
.attributes
& NS_QUERYATTR_NAMEBUFUSED
) == 0);
402 CTRACE("query_newname");
404 result
= dns_message_gettempname(client
->message
, &name
);
405 if (result
!= ISC_R_SUCCESS
) {
406 CTRACE("query_newname: dns_message_gettempname failed: done");
409 isc_buffer_availableregion(dbuf
, &r
);
410 isc_buffer_init(nbuf
, r
.base
, r
.length
);
411 dns_name_init(name
, NULL
);
412 dns_name_setbuffer(name
, nbuf
);
413 client
->query
.attributes
|= NS_QUERYATTR_NAMEBUFUSED
;
415 CTRACE("query_newname: done");
419 static inline dns_rdataset_t
*
420 query_newrdataset(ns_client_t
*client
) {
421 dns_rdataset_t
*rdataset
;
424 CTRACE("query_newrdataset");
426 result
= dns_message_gettemprdataset(client
->message
, &rdataset
);
427 if (result
!= ISC_R_SUCCESS
) {
428 CTRACE("query_newrdataset: "
429 "dns_message_gettemprdataset failed: done");
432 dns_rdataset_init(rdataset
);
434 CTRACE("query_newrdataset: done");
439 query_putrdataset(ns_client_t
*client
, dns_rdataset_t
**rdatasetp
) {
440 dns_rdataset_t
*rdataset
= *rdatasetp
;
442 CTRACE("query_putrdataset");
443 if (rdataset
!= NULL
) {
444 if (dns_rdataset_isassociated(rdataset
))
445 dns_rdataset_disassociate(rdataset
);
446 dns_message_puttemprdataset(client
->message
, rdatasetp
);
448 CTRACE("query_putrdataset: done");
452 static inline isc_result_t
453 query_newdbversion(ns_client_t
*client
, unsigned int n
) {
455 ns_dbversion_t
*dbversion
;
457 for (i
= 0; i
< n
; i
++) {
458 dbversion
= isc_mem_get(client
->mctx
, sizeof(*dbversion
));
459 if (dbversion
!= NULL
) {
460 dbversion
->db
= NULL
;
461 dbversion
->version
= NULL
;
462 ISC_LIST_INITANDAPPEND(client
->query
.freeversions
,
466 * We only return ISC_R_NOMEMORY if we couldn't
470 return (ISC_R_NOMEMORY
);
472 return (ISC_R_SUCCESS
);
476 return (ISC_R_SUCCESS
);
479 static inline ns_dbversion_t
*
480 query_getdbversion(ns_client_t
*client
) {
482 ns_dbversion_t
*dbversion
;
484 if (ISC_LIST_EMPTY(client
->query
.freeversions
)) {
485 result
= query_newdbversion(client
, 1);
486 if (result
!= ISC_R_SUCCESS
)
489 dbversion
= ISC_LIST_HEAD(client
->query
.freeversions
);
490 INSIST(dbversion
!= NULL
);
491 ISC_LIST_UNLINK(client
->query
.freeversions
, dbversion
, link
);
497 ns_query_init(ns_client_t
*client
) {
500 ISC_LIST_INIT(client
->query
.namebufs
);
501 ISC_LIST_INIT(client
->query
.activeversions
);
502 ISC_LIST_INIT(client
->query
.freeversions
);
503 client
->query
.restarts
= 0;
504 client
->query
.timerset
= ISC_FALSE
;
505 client
->query
.qname
= NULL
;
506 result
= isc_mutex_init(&client
->query
.fetchlock
);
507 if (result
!= ISC_R_SUCCESS
)
509 client
->query
.fetch
= NULL
;
510 client
->query
.authdb
= NULL
;
511 client
->query
.authzone
= NULL
;
512 client
->query
.authdbset
= ISC_FALSE
;
513 client
->query
.isreferral
= ISC_FALSE
;
514 query_reset(client
, ISC_FALSE
);
515 result
= query_newdbversion(client
, 3);
516 if (result
!= ISC_R_SUCCESS
) {
517 DESTROYLOCK(&client
->query
.fetchlock
);
520 result
= query_newnamebuf(client
);
521 if (result
!= ISC_R_SUCCESS
)
522 query_freefreeversions(client
, ISC_TRUE
);
527 static inline ns_dbversion_t
*
528 query_findversion(ns_client_t
*client
, dns_db_t
*db
,
529 isc_boolean_t
*newzonep
)
531 ns_dbversion_t
*dbversion
;
534 * We may already have done a query related to this
535 * database. If so, we must be sure to make subsequent
536 * queries from the same version.
538 for (dbversion
= ISC_LIST_HEAD(client
->query
.activeversions
);
540 dbversion
= ISC_LIST_NEXT(dbversion
, link
)) {
541 if (dbversion
->db
== db
)
545 if (dbversion
== NULL
) {
547 * This is a new zone for this query. Add it to
550 dbversion
= query_getdbversion(client
);
551 if (dbversion
== NULL
)
553 dns_db_attach(db
, &dbversion
->db
);
554 dns_db_currentversion(db
, &dbversion
->version
);
555 dbversion
->queryok
= ISC_FALSE
;
556 ISC_LIST_APPEND(client
->query
.activeversions
,
558 *newzonep
= ISC_TRUE
;
560 *newzonep
= ISC_FALSE
;
565 static inline isc_result_t
566 query_validatezonedb(ns_client_t
*client
, dns_name_t
*name
,
567 dns_rdatatype_t qtype
, unsigned int options
,
568 dns_zone_t
*zone
, dns_db_t
*db
,
569 dns_dbversion_t
**versionp
)
572 isc_boolean_t check_acl
, new_zone
;
574 ns_dbversion_t
*dbversion
;
576 REQUIRE(zone
!= NULL
);
580 * This limits our searching to the zone where the first name
581 * (the query target) was looked for. This prevents following
582 * CNAMES or DNAMES into other zones and prevents returning
583 * additional data from other zones.
585 if (!client
->view
->additionalfromauth
&&
586 client
->query
.authdbset
&&
587 db
!= client
->query
.authdb
)
591 * If the zone has an ACL, we'll check it, otherwise
592 * we use the view's "allow-query" ACL. Each ACL is only checked
595 * Also, get the database version to use.
598 check_acl
= ISC_TRUE
; /* Keep compiler happy. */
602 * Get the current version of this database.
604 dbversion
= query_findversion(client
, db
, &new_zone
);
605 if (dbversion
== NULL
) {
606 result
= DNS_R_SERVFAIL
;
610 check_acl
= ISC_TRUE
;
611 } else if (!dbversion
->queryok
) {
614 check_acl
= ISC_FALSE
;
617 queryacl
= dns_zone_getqueryacl(zone
);
618 if (queryacl
== NULL
) {
619 queryacl
= client
->view
->queryacl
;
620 if ((client
->query
.attributes
&
621 NS_QUERYATTR_QUERYOKVALID
) != 0) {
623 * We've evaluated the view's queryacl already. If
624 * NS_QUERYATTR_QUERYOK is set, then the client is
625 * allowed to make queries, otherwise the query should
628 check_acl
= ISC_FALSE
;
629 if ((client
->query
.attributes
&
630 NS_QUERYATTR_QUERYOK
) == 0)
634 * We haven't evaluated the view's queryacl yet.
636 check_acl
= ISC_TRUE
;
641 isc_boolean_t log
= ISC_TF((options
& DNS_GETDB_NOLOG
) == 0);
643 result
= ns_client_checkaclsilent(client
, queryacl
, ISC_TRUE
);
645 char msg
[NS_CLIENT_ACLMSGSIZE("query")];
646 if (result
== ISC_R_SUCCESS
) {
647 if (isc_log_wouldlog(ns_g_lctx
,
650 ns_client_aclmsg("query", name
, qtype
,
651 client
->view
->rdclass
,
653 ns_client_log(client
,
654 DNS_LOGCATEGORY_SECURITY
,
660 ns_client_aclmsg("query", name
, qtype
,
661 client
->view
->rdclass
,
663 ns_client_log(client
, DNS_LOGCATEGORY_SECURITY
,
664 NS_LOGMODULE_QUERY
, ISC_LOG_INFO
,
669 if (queryacl
== client
->view
->queryacl
) {
670 if (result
== ISC_R_SUCCESS
) {
672 * We were allowed by the default
673 * "allow-query" ACL. Remember this so we
674 * don't have to check again.
676 client
->query
.attributes
|=
677 NS_QUERYATTR_QUERYOK
;
680 * We've now evaluated the view's query ACL, and
681 * the NS_QUERYATTR_QUERYOK attribute is now valid.
683 client
->query
.attributes
|= NS_QUERYATTR_QUERYOKVALID
;
686 if (result
!= ISC_R_SUCCESS
)
693 * Remember the result of the ACL check so we
694 * don't have to check again.
696 dbversion
->queryok
= ISC_TRUE
;
698 /* Transfer ownership, if necessary. */
699 if (versionp
!= NULL
)
700 *versionp
= dbversion
->version
;
702 return (ISC_R_SUCCESS
);
705 return (DNS_R_REFUSED
);
711 static inline isc_result_t
712 query_getzonedb(ns_client_t
*client
, dns_name_t
*name
, dns_rdatatype_t qtype
,
713 unsigned int options
, dns_zone_t
**zonep
, dns_db_t
**dbp
,
714 dns_dbversion_t
**versionp
)
717 unsigned int ztoptions
;
718 dns_zone_t
*zone
= NULL
;
720 isc_boolean_t partial
= ISC_FALSE
;
722 REQUIRE(zonep
!= NULL
&& *zonep
== NULL
);
723 REQUIRE(dbp
!= NULL
&& *dbp
== NULL
);
726 * Find a zone database to answer the query.
728 ztoptions
= ((options
& DNS_GETDB_NOEXACT
) != 0) ?
729 DNS_ZTFIND_NOEXACT
: 0;
731 result
= dns_zt_find(client
->view
->zonetable
, name
, ztoptions
, NULL
,
733 if (result
== DNS_R_PARTIALMATCH
)
735 if (result
== ISC_R_SUCCESS
|| result
== DNS_R_PARTIALMATCH
)
736 result
= dns_zone_getdb(zone
, &db
);
738 if (result
!= ISC_R_SUCCESS
)
741 result
= query_validatezonedb(client
, name
, qtype
, options
, zone
, db
,
744 if (result
!= ISC_R_SUCCESS
)
747 /* Transfer ownership. */
751 if (partial
&& (options
& DNS_GETDB_PARTIAL
) != 0)
752 return (DNS_R_PARTIALMATCH
);
753 return (ISC_R_SUCCESS
);
757 dns_zone_detach(&zone
);
764 static inline isc_result_t
765 query_getcachedb(ns_client_t
*client
, dns_name_t
*name
, dns_rdatatype_t qtype
,
766 dns_db_t
**dbp
, unsigned int options
)
769 isc_boolean_t check_acl
;
772 REQUIRE(dbp
!= NULL
&& *dbp
== NULL
);
775 * Find a cache database to answer the query.
776 * This may fail with DNS_R_REFUSED if the client
777 * is not allowed to use the cache.
780 if (!USECACHE(client
))
781 return (DNS_R_REFUSED
);
782 dns_db_attach(client
->view
->cachedb
, &db
);
784 if ((client
->query
.attributes
&
785 NS_QUERYATTR_QUERYOKVALID
) != 0) {
787 * We've evaluated the view's queryacl already. If
788 * NS_QUERYATTR_QUERYOK is set, then the client is
789 * allowed to make queries, otherwise the query should
792 check_acl
= ISC_FALSE
;
793 if ((client
->query
.attributes
&
794 NS_QUERYATTR_QUERYOK
) == 0)
798 * We haven't evaluated the view's queryacl yet.
800 check_acl
= ISC_TRUE
;
804 isc_boolean_t log
= ISC_TF((options
& DNS_GETDB_NOLOG
) == 0);
805 char msg
[NS_CLIENT_ACLMSGSIZE("query (cache)")];
807 result
= ns_client_checkaclsilent(client
,
808 client
->view
->queryacl
,
810 if (result
== ISC_R_SUCCESS
) {
812 * We were allowed by the default
813 * "allow-query" ACL. Remember this so we
814 * don't have to check again.
816 client
->query
.attributes
|=
817 NS_QUERYATTR_QUERYOK
;
818 if (log
&& isc_log_wouldlog(ns_g_lctx
,
821 ns_client_aclmsg("query (cache)", name
, qtype
,
822 client
->view
->rdclass
,
824 ns_client_log(client
,
825 DNS_LOGCATEGORY_SECURITY
,
831 ns_client_aclmsg("query (cache)", name
, qtype
,
832 client
->view
->rdclass
, msg
,
834 ns_client_log(client
, DNS_LOGCATEGORY_SECURITY
,
835 NS_LOGMODULE_QUERY
, ISC_LOG_INFO
,
839 * We've now evaluated the view's query ACL, and
840 * the NS_QUERYATTR_QUERYOK attribute is now valid.
842 client
->query
.attributes
|= NS_QUERYATTR_QUERYOKVALID
;
844 if (result
!= ISC_R_SUCCESS
)
850 /* Transfer ownership. */
853 return (ISC_R_SUCCESS
);
856 result
= DNS_R_REFUSED
;
865 static inline isc_result_t
866 query_getdb(ns_client_t
*client
, dns_name_t
*name
, dns_rdatatype_t qtype
,
867 unsigned int options
, dns_zone_t
**zonep
, dns_db_t
**dbp
,
868 dns_dbversion_t
**versionp
, isc_boolean_t
*is_zonep
)
873 isc_result_t tresult
;
874 unsigned int namelabels
;
875 unsigned int zonelabels
;
876 dns_zone_t
*zone
= NULL
;
879 REQUIRE(zonep
!= NULL
&& *zonep
== NULL
);
883 /* Calculate how many labels are in name. */
884 namelabels
= dns_name_countlabels(name
);
887 /* Try to find name in bind's standard database. */
888 result
= query_getzonedb(client
, name
, qtype
, options
, &zone
,
891 /* See how many labels are in the zone's name. */
892 if (result
== ISC_R_SUCCESS
&& zone
!= NULL
)
893 zonelabels
= dns_name_countlabels(dns_zone_getorigin(zone
));
895 * If # zone labels < # name labels, try to find an even better match
896 * Only try if a DLZ driver is loaded for this view
898 if (zonelabels
< namelabels
&& client
->view
->dlzdatabase
!= NULL
) {
899 tresult
= dns_dlzfindzone(client
->view
, name
,
901 /* If we successful, we found a better match. */
902 if (tresult
== ISC_R_SUCCESS
) {
904 * If the previous search returned a zone, detach it.
907 dns_zone_detach(&zone
);
910 * If the previous search returned a database,
917 * If the previous search returned a version, clear it.
922 * Get our database version.
924 dns_db_currentversion(tdbp
, versionp
);
927 * Be sure to return our database.
932 * We return a null zone, No stats for DLZ zones.
939 result
= query_getzonedb(client
, name
, qtype
, options
,
940 zonep
, dbp
, versionp
);
943 /* If successfull, Transfer ownership of zone. */
944 if (result
== ISC_R_SUCCESS
) {
949 * If neither attempt above succeeded, return the cache instead
951 *is_zonep
= ISC_TRUE
;
952 } else if (result
== ISC_R_NOTFOUND
) {
953 result
= query_getcachedb(client
, name
, qtype
, dbp
, options
);
954 *is_zonep
= ISC_FALSE
;
959 static inline isc_boolean_t
960 query_isduplicate(ns_client_t
*client
, dns_name_t
*name
,
961 dns_rdatatype_t type
, dns_name_t
**mnamep
)
963 dns_section_t section
;
964 dns_name_t
*mname
= NULL
;
967 CTRACE("query_isduplicate");
969 for (section
= DNS_SECTION_ANSWER
;
970 section
<= DNS_SECTION_ADDITIONAL
;
972 result
= dns_message_findname(client
->message
, section
,
973 name
, type
, 0, &mname
, NULL
);
974 if (result
== ISC_R_SUCCESS
) {
976 * We've already got this RRset in the response.
978 CTRACE("query_isduplicate: true: done");
980 } else if (result
== DNS_R_NXRRSET
) {
982 * The name exists, but the rdataset does not.
984 if (section
== DNS_SECTION_ADDITIONAL
)
987 RUNTIME_CHECK(result
== DNS_R_NXDOMAIN
);
992 * If the dns_name_t we're looking up is already in the message,
993 * we don't want to trigger the caller's name replacement logic.
1000 CTRACE("query_isduplicate: false: done");
1005 query_addadditional(void *arg
, dns_name_t
*name
, dns_rdatatype_t qtype
) {
1006 ns_client_t
*client
= arg
;
1007 isc_result_t result
, eresult
;
1010 dns_name_t
*fname
, *mname
;
1011 dns_rdataset_t
*rdataset
, *sigrdataset
, *trdataset
;
1014 dns_dbversion_t
*version
;
1015 isc_boolean_t added_something
, need_addname
;
1017 dns_rdatatype_t type
;
1019 REQUIRE(NS_CLIENT_VALID(client
));
1020 REQUIRE(qtype
!= dns_rdatatype_any
);
1022 if (!WANTDNSSEC(client
) && dns_rdatatype_isdnssec(qtype
))
1023 return (ISC_R_SUCCESS
);
1025 CTRACE("query_addadditional");
1030 eresult
= ISC_R_SUCCESS
;
1038 added_something
= ISC_FALSE
;
1039 need_addname
= ISC_FALSE
;
1043 * We treat type A additional section processing as if it
1044 * were "any address type" additional section processing.
1045 * To avoid multiple lookups, we do an 'any' database
1046 * lookup and iterate over the node.
1048 if (qtype
== dns_rdatatype_a
)
1049 type
= dns_rdatatype_any
;
1054 * Get some resources.
1056 dbuf
= query_getnamebuf(client
);
1059 fname
= query_newname(client
, dbuf
, &b
);
1060 rdataset
= query_newrdataset(client
);
1061 if (fname
== NULL
|| rdataset
== NULL
)
1063 if (WANTDNSSEC(client
)) {
1064 sigrdataset
= query_newrdataset(client
);
1065 if (sigrdataset
== NULL
)
1070 * Look for a zone database that might contain authoritative
1073 result
= query_getzonedb(client
, name
, qtype
, DNS_GETDB_NOLOG
,
1074 &zone
, &db
, &version
);
1075 if (result
!= ISC_R_SUCCESS
)
1078 CTRACE("query_addadditional: db_find");
1081 * Since we are looking for authoritative data, we do not set
1082 * the GLUEOK flag. Glue will be looked for later, but not
1083 * necessarily in the same database.
1086 result
= dns_db_find(db
, name
, version
, type
, client
->query
.dboptions
,
1087 client
->now
, &node
, fname
, rdataset
,
1089 if (result
== ISC_R_SUCCESS
)
1092 if (dns_rdataset_isassociated(rdataset
))
1093 dns_rdataset_disassociate(rdataset
);
1094 if (sigrdataset
!= NULL
&& dns_rdataset_isassociated(sigrdataset
))
1095 dns_rdataset_disassociate(sigrdataset
);
1097 dns_db_detachnode(db
, &node
);
1102 * No authoritative data was found. The cache is our next best bet.
1106 result
= query_getcachedb(client
, name
, qtype
, &db
, DNS_GETDB_NOLOG
);
1107 if (result
!= ISC_R_SUCCESS
)
1109 * Most likely the client isn't allowed to query the cache.
1113 * Attempt to validate glue.
1115 if (sigrdataset
== NULL
) {
1116 sigrdataset
= query_newrdataset(client
);
1117 if (sigrdataset
== NULL
)
1120 result
= dns_db_find(db
, name
, version
, type
,
1121 client
->query
.dboptions
| DNS_DBFIND_GLUEOK
,
1122 client
->now
, &node
, fname
, rdataset
,
1124 if (result
== DNS_R_GLUE
&&
1125 validate(client
, db
, fname
, rdataset
, sigrdataset
))
1126 result
= ISC_R_SUCCESS
;
1127 if (!WANTDNSSEC(client
))
1128 query_putrdataset(client
, &sigrdataset
);
1129 if (result
== ISC_R_SUCCESS
)
1132 if (dns_rdataset_isassociated(rdataset
))
1133 dns_rdataset_disassociate(rdataset
);
1134 if (sigrdataset
!= NULL
&& dns_rdataset_isassociated(sigrdataset
))
1135 dns_rdataset_disassociate(sigrdataset
);
1137 dns_db_detachnode(db
, &node
);
1142 * No cached data was found. Glue is our last chance.
1145 * NS records cause both the usual additional section
1146 * processing to locate a type A record, and, when used
1147 * in a referral, a special search of the zone in which
1148 * they reside for glue information.
1150 * This is the "special search". Note that we must search
1151 * the zone where the NS record resides, not the zone it
1152 * points to, and that we only do the search in the delegation
1153 * case (identified by client->query.gluedb being set).
1156 if (client
->query
.gluedb
== NULL
)
1160 * Don't poision caches using the bailiwick protection model.
1162 if (!dns_name_issubdomain(name
, dns_db_origin(client
->query
.gluedb
)))
1165 dns_db_attach(client
->query
.gluedb
, &db
);
1166 result
= dns_db_find(db
, name
, version
, type
,
1167 client
->query
.dboptions
| DNS_DBFIND_GLUEOK
,
1168 client
->now
, &node
, fname
, rdataset
,
1170 if (!(result
== ISC_R_SUCCESS
||
1171 result
== DNS_R_ZONECUT
||
1172 result
== DNS_R_GLUE
))
1177 * We have found a potential additional data rdataset, or
1178 * at least a node to iterate over.
1180 query_keepname(client
, fname
, dbuf
);
1183 * If we have an rdataset, add it to the additional data
1187 if (dns_rdataset_isassociated(rdataset
) &&
1188 !query_isduplicate(client
, fname
, type
, &mname
)) {
1189 if (mname
!= NULL
) {
1190 query_releasename(client
, &fname
);
1193 need_addname
= ISC_TRUE
;
1194 ISC_LIST_APPEND(fname
->list
, rdataset
, link
);
1195 trdataset
= rdataset
;
1197 added_something
= ISC_TRUE
;
1199 * Note: we only add SIGs if we've added the type they cover,
1200 * so we do not need to check if the SIG rdataset is already
1203 if (sigrdataset
!= NULL
&&
1204 dns_rdataset_isassociated(sigrdataset
))
1206 ISC_LIST_APPEND(fname
->list
, sigrdataset
, link
);
1211 if (qtype
== dns_rdatatype_a
) {
1213 * We now go looking for A and AAAA records, along with
1216 * XXXRTH This code could be more efficient.
1218 if (rdataset
!= NULL
) {
1219 if (dns_rdataset_isassociated(rdataset
))
1220 dns_rdataset_disassociate(rdataset
);
1222 rdataset
= query_newrdataset(client
);
1223 if (rdataset
== NULL
)
1226 if (sigrdataset
!= NULL
) {
1227 if (dns_rdataset_isassociated(sigrdataset
))
1228 dns_rdataset_disassociate(sigrdataset
);
1229 } else if (WANTDNSSEC(client
)) {
1230 sigrdataset
= query_newrdataset(client
);
1231 if (sigrdataset
== NULL
)
1234 result
= dns_db_findrdataset(db
, node
, version
,
1236 client
->now
, rdataset
,
1238 if (result
== DNS_R_NCACHENXDOMAIN
)
1240 if (result
== DNS_R_NCACHENXRRSET
) {
1241 dns_rdataset_disassociate(rdataset
);
1243 * Negative cache entries don't have sigrdatasets.
1245 INSIST(sigrdataset
== NULL
||
1246 ! dns_rdataset_isassociated(sigrdataset
));
1248 if (result
== ISC_R_SUCCESS
) {
1250 if (!query_isduplicate(client
, fname
,
1251 dns_rdatatype_a
, &mname
)) {
1252 if (mname
!= NULL
) {
1253 query_releasename(client
, &fname
);
1256 need_addname
= ISC_TRUE
;
1257 ISC_LIST_APPEND(fname
->list
, rdataset
, link
);
1258 added_something
= ISC_TRUE
;
1259 if (sigrdataset
!= NULL
&&
1260 dns_rdataset_isassociated(sigrdataset
))
1262 ISC_LIST_APPEND(fname
->list
,
1265 query_newrdataset(client
);
1267 rdataset
= query_newrdataset(client
);
1268 if (rdataset
== NULL
)
1270 if (WANTDNSSEC(client
) && sigrdataset
== NULL
)
1273 dns_rdataset_disassociate(rdataset
);
1274 if (sigrdataset
!= NULL
&&
1275 dns_rdataset_isassociated(sigrdataset
))
1276 dns_rdataset_disassociate(sigrdataset
);
1279 result
= dns_db_findrdataset(db
, node
, version
,
1280 dns_rdatatype_aaaa
, 0,
1281 client
->now
, rdataset
,
1283 if (result
== DNS_R_NCACHENXDOMAIN
)
1285 if (result
== DNS_R_NCACHENXRRSET
) {
1286 dns_rdataset_disassociate(rdataset
);
1287 INSIST(sigrdataset
== NULL
||
1288 ! dns_rdataset_isassociated(sigrdataset
));
1290 if (result
== ISC_R_SUCCESS
) {
1292 if (!query_isduplicate(client
, fname
,
1293 dns_rdatatype_aaaa
, &mname
)) {
1294 if (mname
!= NULL
) {
1295 query_releasename(client
, &fname
);
1298 need_addname
= ISC_TRUE
;
1299 ISC_LIST_APPEND(fname
->list
, rdataset
, link
);
1300 added_something
= ISC_TRUE
;
1301 if (sigrdataset
!= NULL
&&
1302 dns_rdataset_isassociated(sigrdataset
))
1304 ISC_LIST_APPEND(fname
->list
,
1314 CTRACE("query_addadditional: addname");
1316 * If we haven't added anything, then we're done.
1318 if (!added_something
)
1322 * We may have added our rdatasets to an existing name, if so, then
1323 * need_addname will be ISC_FALSE. Whether we used an existing name
1324 * or a new one, we must set fname to NULL to prevent cleanup.
1327 dns_message_addname(client
->message
, fname
,
1328 DNS_SECTION_ADDITIONAL
);
1332 * In a few cases, we want to add additional data for additional
1333 * data. It's simpler to just deal with special cases here than
1334 * to try to create a general purpose mechanism and allow the
1335 * rdata implementations to do it themselves.
1337 * This involves recursion, but the depth is limited. The
1338 * most complex case is adding a SRV rdataset, which involves
1339 * recursing to add address records, which in turn can cause
1340 * recursion to add KEYs.
1342 if (type
== dns_rdatatype_srv
&& trdataset
!= NULL
) {
1344 * If we're adding SRV records to the additional data
1345 * section, it's helpful if we add the SRV additional data
1348 eresult
= dns_rdataset_additionaldata(trdataset
,
1349 query_addadditional
,
1354 CTRACE("query_addadditional: cleanup");
1355 query_putrdataset(client
, &rdataset
);
1356 if (sigrdataset
!= NULL
)
1357 query_putrdataset(client
, &sigrdataset
);
1359 query_releasename(client
, &fname
);
1361 dns_db_detachnode(db
, &node
);
1365 dns_zone_detach(&zone
);
1367 CTRACE("query_addadditional: done");
1372 query_discardcache(ns_client_t
*client
, dns_rdataset_t
*rdataset_base
,
1373 dns_rdatasetadditional_t additionaltype
,
1374 dns_rdatatype_t type
, dns_zone_t
**zonep
, dns_db_t
**dbp
,
1375 dns_dbversion_t
**versionp
, dns_dbnode_t
**nodep
,
1378 dns_rdataset_t
*rdataset
;
1380 while ((rdataset
= ISC_LIST_HEAD(fname
->list
)) != NULL
) {
1381 ISC_LIST_UNLINK(fname
->list
, rdataset
, link
);
1382 query_putrdataset(client
, &rdataset
);
1384 if (*versionp
!= NULL
)
1385 dns_db_closeversion(*dbp
, versionp
, ISC_FALSE
);
1387 dns_db_detachnode(*dbp
, nodep
);
1391 dns_zone_detach(zonep
);
1392 (void)dns_rdataset_putadditional(client
->view
->acache
, rdataset_base
,
1393 additionaltype
, type
);
1396 static inline isc_result_t
1397 query_iscachevalid(dns_zone_t
*zone
, dns_db_t
*db
, dns_db_t
*db0
,
1398 dns_dbversion_t
*version
)
1400 isc_result_t result
= ISC_R_SUCCESS
;
1401 dns_dbversion_t
*version_current
= NULL
;
1402 dns_db_t
*db_current
= db0
;
1404 if (db_current
== NULL
) {
1405 result
= dns_zone_getdb(zone
, &db_current
);
1406 if (result
!= ISC_R_SUCCESS
)
1409 dns_db_currentversion(db_current
, &version_current
);
1410 if (db_current
!= db
|| version_current
!= version
) {
1411 result
= ISC_R_FAILURE
;
1416 dns_db_closeversion(db_current
, &version_current
, ISC_FALSE
);
1417 if (db0
== NULL
&& db_current
!= NULL
)
1418 dns_db_detach(&db_current
);
1424 query_addadditional2(void *arg
, dns_name_t
*name
, dns_rdatatype_t qtype
) {
1425 client_additionalctx_t
*additionalctx
= arg
;
1426 dns_rdataset_t
*rdataset_base
;
1427 ns_client_t
*client
;
1428 isc_result_t result
, eresult
;
1429 dns_dbnode_t
*node
, *cnode
;
1431 dns_name_t
*fname
, *mname0
, cfname
;
1432 dns_rdataset_t
*rdataset
, *sigrdataset
;
1433 dns_rdataset_t
*crdataset
, *crdataset_next
;
1436 dns_dbversion_t
*version
, *cversion
;
1437 isc_boolean_t added_something
, need_addname
, needadditionalcache
;
1438 isc_boolean_t need_sigrrset
;
1440 dns_rdatatype_t type
;
1441 dns_rdatasetadditional_t additionaltype
;
1443 if (qtype
!= dns_rdatatype_a
) {
1445 * This function is optimized for "address" types. For other
1446 * types, use a generic routine.
1447 * XXX: ideally, this function should be generic enough.
1449 return (query_addadditional(additionalctx
->client
,
1456 rdataset_base
= additionalctx
->rdataset
;
1457 client
= additionalctx
->client
;
1458 REQUIRE(NS_CLIENT_VALID(client
));
1459 eresult
= ISC_R_SUCCESS
;
1469 added_something
= ISC_FALSE
;
1470 need_addname
= ISC_FALSE
;
1472 needadditionalcache
= ISC_FALSE
;
1473 additionaltype
= dns_rdatasetadditional_fromauth
;
1474 dns_name_init(&cfname
, NULL
);
1476 CTRACE("query_addadditional2");
1479 * We treat type A additional section processing as if it
1480 * were "any address type" additional section processing.
1481 * To avoid multiple lookups, we do an 'any' database
1482 * lookup and iterate over the node.
1483 * XXXJT: this approach can cause a suboptimal result when the cache
1484 * DB only has partial address types and the glue DB has remaining
1487 type
= dns_rdatatype_any
;
1490 * Get some resources.
1492 dbuf
= query_getnamebuf(client
);
1495 fname
= query_newname(client
, dbuf
, &b
);
1498 dns_name_setbuffer(&cfname
, &b
); /* share the buffer */
1500 /* Check additional cache */
1501 result
= dns_rdataset_getadditional(rdataset_base
, additionaltype
,
1502 type
, client
->view
->acache
, &zone
,
1503 &cdb
, &cversion
, &cnode
, &cfname
,
1504 client
->message
, client
->now
);
1505 if (result
!= ISC_R_SUCCESS
)
1508 CTRACE("query_addadditional2: auth zone not found");
1512 /* Is the cached DB up-to-date? */
1513 result
= query_iscachevalid(zone
, cdb
, NULL
, cversion
);
1514 if (result
!= ISC_R_SUCCESS
) {
1515 CTRACE("query_addadditional2: old auth additional cache");
1516 query_discardcache(client
, rdataset_base
, additionaltype
,
1517 type
, &zone
, &cdb
, &cversion
, &cnode
,
1522 if (cnode
== NULL
) {
1524 * We have a negative cache. We don't have to check the zone
1525 * ACL, since the result (not using this zone) would be same
1526 * regardless of the result.
1528 CTRACE("query_addadditional2: negative auth additional cache");
1529 dns_db_closeversion(cdb
, &cversion
, ISC_FALSE
);
1530 dns_db_detach(&cdb
);
1531 dns_zone_detach(&zone
);
1535 result
= query_validatezonedb(client
, name
, qtype
, DNS_GETDB_NOLOG
,
1537 if (result
!= ISC_R_SUCCESS
) {
1538 query_discardcache(client
, rdataset_base
, additionaltype
,
1539 type
, &zone
, &cdb
, &cversion
, &cnode
,
1544 /* We've got an active cache. */
1545 CTRACE("query_addadditional2: auth additional cache");
1546 dns_db_closeversion(cdb
, &cversion
, ISC_FALSE
);
1549 dns_name_clone(&cfname
, fname
);
1550 query_keepname(client
, fname
, dbuf
);
1554 * Look for a zone database that might contain authoritative
1558 result
= query_getzonedb(client
, name
, qtype
, DNS_GETDB_NOLOG
,
1559 &zone
, &db
, &version
);
1560 if (result
!= ISC_R_SUCCESS
) {
1561 /* Cache the negative result */
1562 (void)dns_rdataset_setadditional(rdataset_base
, additionaltype
,
1563 type
, client
->view
->acache
,
1564 NULL
, NULL
, NULL
, NULL
,
1569 CTRACE("query_addadditional2: db_find");
1572 * Since we are looking for authoritative data, we do not set
1573 * the GLUEOK flag. Glue will be looked for later, but not
1574 * necessarily in the same database.
1577 result
= dns_db_find(db
, name
, version
, type
, client
->query
.dboptions
,
1578 client
->now
, &node
, fname
, NULL
, NULL
);
1579 if (result
== ISC_R_SUCCESS
)
1582 /* Cache the negative result */
1583 (void)dns_rdataset_setadditional(rdataset_base
, additionaltype
,
1584 type
, client
->view
->acache
, zone
, db
,
1585 version
, NULL
, fname
);
1588 dns_db_detachnode(db
, &node
);
1593 * No authoritative data was found. The cache is our next best bet.
1597 additionaltype
= dns_rdatasetadditional_fromcache
;
1598 result
= query_getcachedb(client
, name
, qtype
, &db
, DNS_GETDB_NOLOG
);
1599 if (result
!= ISC_R_SUCCESS
)
1601 * Most likely the client isn't allowed to query the cache.
1605 result
= dns_db_find(db
, name
, version
, type
,
1606 client
->query
.dboptions
| DNS_DBFIND_GLUEOK
,
1607 client
->now
, &node
, fname
, NULL
, NULL
);
1608 if (result
== ISC_R_SUCCESS
)
1612 dns_db_detachnode(db
, &node
);
1617 * No cached data was found. Glue is our last chance.
1620 * NS records cause both the usual additional section
1621 * processing to locate a type A record, and, when used
1622 * in a referral, a special search of the zone in which
1623 * they reside for glue information.
1625 * This is the "special search". Note that we must search
1626 * the zone where the NS record resides, not the zone it
1627 * points to, and that we only do the search in the delegation
1628 * case (identified by client->query.gluedb being set).
1630 if (client
->query
.gluedb
== NULL
)
1634 * Don't poision caches using the bailiwick protection model.
1636 if (!dns_name_issubdomain(name
, dns_db_origin(client
->query
.gluedb
)))
1639 /* Check additional cache */
1640 additionaltype
= dns_rdatasetadditional_fromglue
;
1641 result
= dns_rdataset_getadditional(rdataset_base
, additionaltype
,
1642 type
, client
->view
->acache
, NULL
,
1643 &cdb
, &cversion
, &cnode
, &cfname
,
1644 client
->message
, client
->now
);
1645 if (result
!= ISC_R_SUCCESS
)
1648 result
= query_iscachevalid(zone
, cdb
, client
->query
.gluedb
, cversion
);
1649 if (result
!= ISC_R_SUCCESS
) {
1650 CTRACE("query_addadditional2: old glue additional cache");
1651 query_discardcache(client
, rdataset_base
, additionaltype
,
1652 type
, &zone
, &cdb
, &cversion
, &cnode
,
1657 if (cnode
== NULL
) {
1658 /* We have a negative cache. */
1659 CTRACE("query_addadditional2: negative glue additional cache");
1660 dns_db_closeversion(cdb
, &cversion
, ISC_FALSE
);
1661 dns_db_detach(&cdb
);
1666 CTRACE("query_addadditional2: glue additional cache");
1667 dns_db_closeversion(cdb
, &cversion
, ISC_FALSE
);
1670 dns_name_clone(&cfname
, fname
);
1671 query_keepname(client
, fname
, dbuf
);
1675 dns_db_attach(client
->query
.gluedb
, &db
);
1676 result
= dns_db_find(db
, name
, version
, type
,
1677 client
->query
.dboptions
| DNS_DBFIND_GLUEOK
,
1678 client
->now
, &node
, fname
, NULL
, NULL
);
1679 if (!(result
== ISC_R_SUCCESS
||
1680 result
== DNS_R_ZONECUT
||
1681 result
== DNS_R_GLUE
)) {
1682 /* cache the negative result */
1683 (void)dns_rdataset_setadditional(rdataset_base
, additionaltype
,
1684 type
, client
->view
->acache
,
1685 NULL
, db
, version
, NULL
,
1692 * We have found a DB node to iterate over from a DB.
1693 * We are going to look for address RRsets (i.e., A and AAAA) in the DB
1694 * node we've just found. We'll then store the complete information
1695 * in the additional data cache.
1697 dns_name_clone(fname
, &cfname
);
1698 query_keepname(client
, fname
, dbuf
);
1699 needadditionalcache
= ISC_TRUE
;
1701 rdataset
= query_newrdataset(client
);
1702 if (rdataset
== NULL
)
1705 sigrdataset
= query_newrdataset(client
);
1706 if (sigrdataset
== NULL
)
1710 * Find A RRset with sig RRset. Even if we don't find a sig RRset
1711 * for a client using DNSSEC, we'll continue the process to make a
1712 * complete list to be cached. However, we need to cancel the
1713 * caching when something unexpected happens, in order to avoid
1714 * caching incomplete information.
1716 result
= dns_db_findrdataset(db
, node
, version
, dns_rdatatype_a
, 0,
1717 client
->now
, rdataset
, sigrdataset
);
1719 * If we can't promote glue/pending from the cache to secure
1722 if (result
== ISC_R_SUCCESS
&&
1723 additionaltype
== dns_rdatasetadditional_fromcache
&&
1724 (rdataset
->trust
== dns_trust_pending
||
1725 rdataset
->trust
== dns_trust_glue
) &&
1726 !validate(client
, db
, fname
, rdataset
, sigrdataset
)) {
1727 dns_rdataset_disassociate(rdataset
);
1728 if (dns_rdataset_isassociated(sigrdataset
))
1729 dns_rdataset_disassociate(sigrdataset
);
1730 result
= ISC_R_NOTFOUND
;
1732 if (result
== DNS_R_NCACHENXDOMAIN
)
1734 if (result
== DNS_R_NCACHENXRRSET
) {
1735 dns_rdataset_disassociate(rdataset
);
1737 * Negative cache entries don't have sigrdatasets.
1739 INSIST(! dns_rdataset_isassociated(sigrdataset
));
1741 if (result
== ISC_R_SUCCESS
) {
1742 /* Remember the result as a cache */
1743 ISC_LIST_APPEND(cfname
.list
, rdataset
, link
);
1744 if (dns_rdataset_isassociated(sigrdataset
)) {
1745 ISC_LIST_APPEND(cfname
.list
, sigrdataset
, link
);
1746 sigrdataset
= query_newrdataset(client
);
1748 rdataset
= query_newrdataset(client
);
1749 if (sigrdataset
== NULL
|| rdataset
== NULL
) {
1750 /* do not cache incomplete information */
1755 /* Find AAAA RRset with sig RRset */
1756 result
= dns_db_findrdataset(db
, node
, version
, dns_rdatatype_aaaa
,
1757 0, client
->now
, rdataset
, sigrdataset
);
1759 * If we can't promote glue/pending from the cache to secure
1762 if (result
== ISC_R_SUCCESS
&&
1763 additionaltype
== dns_rdatasetadditional_fromcache
&&
1764 (rdataset
->trust
== dns_trust_pending
||
1765 rdataset
->trust
== dns_trust_glue
) &&
1766 !validate(client
, db
, fname
, rdataset
, sigrdataset
)) {
1767 dns_rdataset_disassociate(rdataset
);
1768 if (dns_rdataset_isassociated(sigrdataset
))
1769 dns_rdataset_disassociate(sigrdataset
);
1770 result
= ISC_R_NOTFOUND
;
1772 if (result
== ISC_R_SUCCESS
) {
1773 ISC_LIST_APPEND(cfname
.list
, rdataset
, link
);
1775 if (dns_rdataset_isassociated(sigrdataset
)) {
1776 ISC_LIST_APPEND(cfname
.list
, sigrdataset
, link
);
1783 * Set the new result in the cache if required. We do not support
1784 * caching additional data from a cache DB.
1786 if (needadditionalcache
== ISC_TRUE
&&
1787 (additionaltype
== dns_rdatasetadditional_fromauth
||
1788 additionaltype
== dns_rdatasetadditional_fromglue
)) {
1789 (void)dns_rdataset_setadditional(rdataset_base
, additionaltype
,
1790 type
, client
->view
->acache
,
1791 zone
, db
, version
, node
,
1796 need_sigrrset
= ISC_FALSE
;
1798 for (crdataset
= ISC_LIST_HEAD(cfname
.list
);
1800 crdataset
= crdataset_next
) {
1803 crdataset_next
= ISC_LIST_NEXT(crdataset
, link
);
1806 if (crdataset
->type
== dns_rdatatype_a
||
1807 crdataset
->type
== dns_rdatatype_aaaa
) {
1808 if (!query_isduplicate(client
, fname
, crdataset
->type
,
1810 if (mname
!= NULL
) {
1812 * A different type of this name is
1813 * already stored in the additional
1814 * section. We'll reuse the name.
1815 * Note that this should happen at most
1816 * once. Otherwise, fname->link could
1819 INSIST(mname0
== NULL
);
1821 query_releasename(client
, &fname
);
1825 need_addname
= ISC_TRUE
;
1826 ISC_LIST_UNLINK(cfname
.list
, crdataset
, link
);
1827 ISC_LIST_APPEND(fname
->list
, crdataset
, link
);
1828 added_something
= ISC_TRUE
;
1829 need_sigrrset
= ISC_TRUE
;
1831 need_sigrrset
= ISC_FALSE
;
1832 } else if (crdataset
->type
== dns_rdatatype_rrsig
&&
1833 need_sigrrset
&& WANTDNSSEC(client
)) {
1834 ISC_LIST_UNLINK(cfname
.list
, crdataset
, link
);
1835 ISC_LIST_APPEND(fname
->list
, crdataset
, link
);
1836 added_something
= ISC_TRUE
; /* just in case */
1837 need_sigrrset
= ISC_FALSE
;
1841 CTRACE("query_addadditional2: addname");
1844 * If we haven't added anything, then we're done.
1846 if (!added_something
)
1850 * We may have added our rdatasets to an existing name, if so, then
1851 * need_addname will be ISC_FALSE. Whether we used an existing name
1852 * or a new one, we must set fname to NULL to prevent cleanup.
1855 dns_message_addname(client
->message
, fname
,
1856 DNS_SECTION_ADDITIONAL
);
1860 CTRACE("query_addadditional2: cleanup");
1862 if (rdataset
!= NULL
)
1863 query_putrdataset(client
, &rdataset
);
1864 if (sigrdataset
!= NULL
)
1865 query_putrdataset(client
, &sigrdataset
);
1866 while ((crdataset
= ISC_LIST_HEAD(cfname
.list
)) != NULL
) {
1867 ISC_LIST_UNLINK(cfname
.list
, crdataset
, link
);
1868 query_putrdataset(client
, &crdataset
);
1871 query_releasename(client
, &fname
);
1873 dns_db_detachnode(db
, &node
);
1877 dns_zone_detach(&zone
);
1879 CTRACE("query_addadditional2: done");
1884 query_addrdataset(ns_client_t
*client
, dns_name_t
*fname
,
1885 dns_rdataset_t
*rdataset
)
1887 client_additionalctx_t additionalctx
;
1890 * Add 'rdataset' and any pertinent additional data to
1891 * 'fname', a name in the response message for 'client'.
1894 CTRACE("query_addrdataset");
1896 ISC_LIST_APPEND(fname
->list
, rdataset
, link
);
1898 if (client
->view
->order
!= NULL
)
1899 rdataset
->attributes
|= dns_order_find(client
->view
->order
,
1900 fname
, rdataset
->type
,
1902 rdataset
->attributes
|= DNS_RDATASETATTR_LOADORDER
;
1904 if (NOADDITIONAL(client
))
1908 * Add additional data.
1910 * We don't care if dns_rdataset_additionaldata() fails.
1912 additionalctx
.client
= client
;
1913 additionalctx
.rdataset
= rdataset
;
1914 (void)dns_rdataset_additionaldata(rdataset
, query_addadditional2
,
1916 CTRACE("query_addrdataset: done");
1920 query_addrrset(ns_client_t
*client
, dns_name_t
**namep
,
1921 dns_rdataset_t
**rdatasetp
, dns_rdataset_t
**sigrdatasetp
,
1922 isc_buffer_t
*dbuf
, dns_section_t section
)
1924 dns_name_t
*name
, *mname
;
1925 dns_rdataset_t
*rdataset
, *mrdataset
, *sigrdataset
;
1926 isc_result_t result
;
1929 * To the current response for 'client', add the answer RRset
1930 * '*rdatasetp' and an optional signature set '*sigrdatasetp', with
1931 * owner name '*namep', to section 'section', unless they are
1932 * already there. Also add any pertinent additional data.
1934 * If 'dbuf' is not NULL, then '*namep' is the name whose data is
1935 * stored in 'dbuf'. In this case, query_addrrset() guarantees that
1936 * when it returns the name will either have been kept or released.
1938 CTRACE("query_addrrset");
1940 rdataset
= *rdatasetp
;
1941 if (sigrdatasetp
!= NULL
)
1942 sigrdataset
= *sigrdatasetp
;
1947 result
= dns_message_findname(client
->message
, section
,
1948 name
, rdataset
->type
, rdataset
->covers
,
1949 &mname
, &mrdataset
);
1950 if (result
== ISC_R_SUCCESS
) {
1952 * We've already got an RRset of the given name and type.
1953 * There's nothing else to do;
1955 CTRACE("query_addrrset: dns_message_findname succeeded: done");
1957 query_releasename(client
, namep
);
1959 } else if (result
== DNS_R_NXDOMAIN
) {
1961 * The name doesn't exist.
1964 query_keepname(client
, name
, dbuf
);
1965 dns_message_addname(client
->message
, name
, section
);
1969 RUNTIME_CHECK(result
== DNS_R_NXRRSET
);
1971 query_releasename(client
, namep
);
1974 if (rdataset
->trust
!= dns_trust_secure
&&
1975 (section
== DNS_SECTION_ANSWER
||
1976 section
== DNS_SECTION_AUTHORITY
))
1977 client
->query
.attributes
&= ~NS_QUERYATTR_SECURE
;
1979 * Note: we only add SIGs if we've added the type they cover, so
1980 * we do not need to check if the SIG rdataset is already in the
1983 query_addrdataset(client
, mname
, rdataset
);
1985 if (sigrdataset
!= NULL
&& dns_rdataset_isassociated(sigrdataset
)) {
1987 * We have a signature. Add it to the response.
1989 ISC_LIST_APPEND(mname
->list
, sigrdataset
, link
);
1990 *sigrdatasetp
= NULL
;
1992 CTRACE("query_addrrset: done");
1995 static inline isc_result_t
1996 query_addsoa(ns_client_t
*client
, dns_db_t
*db
, dns_dbversion_t
*version
,
1997 isc_boolean_t zero_ttl
)
2001 isc_result_t result
, eresult
;
2002 dns_rdataset_t
*rdataset
= NULL
, *sigrdataset
= NULL
;
2003 dns_rdataset_t
**sigrdatasetp
= NULL
;
2005 CTRACE("query_addsoa");
2009 eresult
= ISC_R_SUCCESS
;
2015 * Get resources and make 'name' be the database origin.
2017 result
= dns_message_gettempname(client
->message
, &name
);
2018 if (result
!= ISC_R_SUCCESS
)
2020 dns_name_init(name
, NULL
);
2021 dns_name_clone(dns_db_origin(db
), name
);
2022 rdataset
= query_newrdataset(client
);
2023 if (rdataset
== NULL
) {
2024 eresult
= DNS_R_SERVFAIL
;
2027 if (WANTDNSSEC(client
)) {
2028 sigrdataset
= query_newrdataset(client
);
2029 if (sigrdataset
== NULL
) {
2030 eresult
= DNS_R_SERVFAIL
;
2038 result
= dns_db_getoriginnode(db
, &node
);
2039 if (result
== ISC_R_SUCCESS
) {
2040 result
= dns_db_findrdataset(db
, node
, version
,
2042 0, client
->now
, rdataset
,
2045 dns_fixedname_t foundname
;
2048 dns_fixedname_init(&foundname
);
2049 fname
= dns_fixedname_name(&foundname
);
2051 result
= dns_db_find(db
, name
, version
, dns_rdatatype_soa
,
2052 client
->query
.dboptions
, 0, &node
,
2053 fname
, rdataset
, sigrdataset
);
2055 if (result
!= ISC_R_SUCCESS
) {
2057 * This is bad. We tried to get the SOA RR at the zone top
2058 * and it didn't work!
2060 eresult
= DNS_R_SERVFAIL
;
2063 * Extract the SOA MINIMUM.
2065 dns_rdata_soa_t soa
;
2066 dns_rdata_t rdata
= DNS_RDATA_INIT
;
2067 result
= dns_rdataset_first(rdataset
);
2068 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
2069 dns_rdataset_current(rdataset
, &rdata
);
2070 result
= dns_rdata_tostruct(&rdata
, &soa
, NULL
);
2071 if (result
!= ISC_R_SUCCESS
)
2076 if (sigrdataset
!= NULL
)
2077 sigrdataset
->ttl
= 0;
2081 * Add the SOA and its SIG to the response, with the
2082 * TTLs adjusted per RFC2308 section 3.
2084 if (rdataset
->ttl
> soa
.minimum
)
2085 rdataset
->ttl
= soa
.minimum
;
2086 if (sigrdataset
!= NULL
&& sigrdataset
->ttl
> soa
.minimum
)
2087 sigrdataset
->ttl
= soa
.minimum
;
2089 if (sigrdataset
!= NULL
)
2090 sigrdatasetp
= &sigrdataset
;
2092 sigrdatasetp
= NULL
;
2093 query_addrrset(client
, &name
, &rdataset
, sigrdatasetp
, NULL
,
2094 DNS_SECTION_AUTHORITY
);
2098 query_putrdataset(client
, &rdataset
);
2099 if (sigrdataset
!= NULL
)
2100 query_putrdataset(client
, &sigrdataset
);
2102 query_releasename(client
, &name
);
2104 dns_db_detachnode(db
, &node
);
2109 static inline isc_result_t
2110 query_addns(ns_client_t
*client
, dns_db_t
*db
, dns_dbversion_t
*version
) {
2111 dns_name_t
*name
, *fname
;
2113 isc_result_t result
, eresult
;
2114 dns_fixedname_t foundname
;
2115 dns_rdataset_t
*rdataset
= NULL
, *sigrdataset
= NULL
;
2116 dns_rdataset_t
**sigrdatasetp
= NULL
;
2118 CTRACE("query_addns");
2122 eresult
= ISC_R_SUCCESS
;
2126 dns_fixedname_init(&foundname
);
2127 fname
= dns_fixedname_name(&foundname
);
2130 * Get resources and make 'name' be the database origin.
2132 result
= dns_message_gettempname(client
->message
, &name
);
2133 if (result
!= ISC_R_SUCCESS
) {
2134 CTRACE("query_addns: dns_message_gettempname failed: done");
2137 dns_name_init(name
, NULL
);
2138 dns_name_clone(dns_db_origin(db
), name
);
2139 rdataset
= query_newrdataset(client
);
2140 if (rdataset
== NULL
) {
2141 CTRACE("query_addns: query_newrdataset failed");
2142 eresult
= DNS_R_SERVFAIL
;
2145 if (WANTDNSSEC(client
)) {
2146 sigrdataset
= query_newrdataset(client
);
2147 if (sigrdataset
== NULL
) {
2148 CTRACE("query_addns: query_newrdataset failed");
2149 eresult
= DNS_R_SERVFAIL
;
2155 * Find the NS rdataset.
2157 result
= dns_db_getoriginnode(db
, &node
);
2158 if (result
== ISC_R_SUCCESS
) {
2159 result
= dns_db_findrdataset(db
, node
, version
,
2161 0, client
->now
, rdataset
,
2164 CTRACE("query_addns: calling dns_db_find");
2165 result
= dns_db_find(db
, name
, NULL
, dns_rdatatype_ns
,
2166 client
->query
.dboptions
, 0, &node
,
2167 fname
, rdataset
, sigrdataset
);
2168 CTRACE("query_addns: dns_db_find complete");
2170 if (result
!= ISC_R_SUCCESS
) {
2171 CTRACE("query_addns: "
2172 "dns_db_findrdataset or dns_db_find failed");
2174 * This is bad. We tried to get the NS rdataset at the zone
2175 * top and it didn't work!
2177 eresult
= DNS_R_SERVFAIL
;
2179 if (sigrdataset
!= NULL
)
2180 sigrdatasetp
= &sigrdataset
;
2182 sigrdatasetp
= NULL
;
2183 query_addrrset(client
, &name
, &rdataset
, sigrdatasetp
, NULL
,
2184 DNS_SECTION_AUTHORITY
);
2188 CTRACE("query_addns: cleanup");
2189 query_putrdataset(client
, &rdataset
);
2190 if (sigrdataset
!= NULL
)
2191 query_putrdataset(client
, &sigrdataset
);
2193 query_releasename(client
, &name
);
2195 dns_db_detachnode(db
, &node
);
2197 CTRACE("query_addns: done");
2201 static inline isc_result_t
2202 query_addcnamelike(ns_client_t
*client
, dns_name_t
*qname
, dns_name_t
*tname
,
2203 dns_trust_t trust
, dns_name_t
**anamep
, dns_rdatatype_t type
)
2205 dns_rdataset_t
*rdataset
;
2206 dns_rdatalist_t
*rdatalist
;
2208 isc_result_t result
;
2212 * We assume the name data referred to by tname won't go away.
2215 REQUIRE(anamep
!= NULL
);
2218 result
= dns_message_gettemprdatalist(client
->message
, &rdatalist
);
2219 if (result
!= ISC_R_SUCCESS
)
2222 result
= dns_message_gettemprdata(client
->message
, &rdata
);
2223 if (result
!= ISC_R_SUCCESS
)
2226 result
= dns_message_gettemprdataset(client
->message
, &rdataset
);
2227 if (result
!= ISC_R_SUCCESS
)
2229 dns_rdataset_init(rdataset
);
2230 result
= dns_name_dup(qname
, client
->mctx
, *anamep
);
2231 if (result
!= ISC_R_SUCCESS
) {
2232 dns_message_puttemprdataset(client
->message
, &rdataset
);
2236 rdatalist
->type
= type
;
2237 rdatalist
->covers
= 0;
2238 rdatalist
->rdclass
= client
->message
->rdclass
;
2241 dns_name_toregion(tname
, &r
);
2242 rdata
->data
= r
.base
;
2243 rdata
->length
= r
.length
;
2244 rdata
->rdclass
= client
->message
->rdclass
;
2247 ISC_LIST_INIT(rdatalist
->rdata
);
2248 ISC_LIST_APPEND(rdatalist
->rdata
, rdata
, link
);
2249 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist
, rdataset
)
2251 rdataset
->trust
= trust
;
2253 query_addrrset(client
, anamep
, &rdataset
, NULL
, NULL
,
2254 DNS_SECTION_ANSWER
);
2256 if (rdataset
!= NULL
) {
2257 if (dns_rdataset_isassociated(rdataset
))
2258 dns_rdataset_disassociate(rdataset
);
2259 dns_message_puttemprdataset(client
->message
, &rdataset
);
2262 return (ISC_R_SUCCESS
);
2266 * Mark the RRsets as secure. Update the cache (db) to reflect the
2267 * change in trust level.
2270 mark_secure(ns_client_t
*client
, dns_db_t
*db
, dns_name_t
*name
,
2271 dns_rdataset_t
*rdataset
, dns_rdataset_t
*sigrdataset
)
2273 isc_result_t result
;
2274 dns_dbnode_t
*node
= NULL
;
2276 rdataset
->trust
= dns_trust_secure
;
2277 sigrdataset
->trust
= dns_trust_secure
;
2280 * Save the updated secure state. Ignore failures.
2282 result
= dns_db_findnode(db
, name
, ISC_TRUE
, &node
);
2283 if (result
!= ISC_R_SUCCESS
)
2285 (void)dns_db_addrdataset(db
, node
, NULL
, client
->now
, rdataset
,
2287 (void)dns_db_addrdataset(db
, node
, NULL
, client
->now
, sigrdataset
,
2289 dns_db_detachnode(db
, &node
);
2293 * Find the secure key that corresponds to rrsig.
2294 * Note: 'keyrdataset' maintains state between sucessive calls,
2295 * there may be multiple keys with the same keyid.
2296 * Return ISC_FALSE if we have exhausted all the possible keys.
2298 static isc_boolean_t
2299 get_key(ns_client_t
*client
, dns_db_t
*db
, dns_rdata_rrsig_t
*rrsig
,
2300 dns_rdataset_t
*keyrdataset
, dst_key_t
**keyp
)
2302 isc_result_t result
;
2303 dns_dbnode_t
*node
= NULL
;
2304 isc_boolean_t secure
= ISC_FALSE
;
2306 if (!dns_rdataset_isassociated(keyrdataset
)) {
2307 result
= dns_db_findnode(db
, &rrsig
->signer
, ISC_FALSE
, &node
);
2308 if (result
!= ISC_R_SUCCESS
)
2311 result
= dns_db_findrdataset(db
, node
, NULL
,
2312 dns_rdatatype_dnskey
, 0,
2313 client
->now
, keyrdataset
, NULL
);
2314 dns_db_detachnode(db
, &node
);
2315 if (result
!= ISC_R_SUCCESS
)
2318 if (keyrdataset
->trust
!= dns_trust_secure
)
2321 result
= dns_rdataset_first(keyrdataset
);
2323 result
= dns_rdataset_next(keyrdataset
);
2325 for ( ; result
== ISC_R_SUCCESS
;
2326 result
= dns_rdataset_next(keyrdataset
)) {
2327 dns_rdata_t rdata
= DNS_RDATA_INIT
;
2330 dns_rdataset_current(keyrdataset
, &rdata
);
2331 isc_buffer_init(&b
, rdata
.data
, rdata
.length
);
2332 isc_buffer_add(&b
, rdata
.length
);
2333 result
= dst_key_fromdns(&rrsig
->signer
, rdata
.rdclass
, &b
,
2334 client
->mctx
, keyp
);
2335 if (result
!= ISC_R_SUCCESS
)
2337 if (rrsig
->algorithm
== (dns_secalg_t
)dst_key_alg(*keyp
) &&
2338 rrsig
->keyid
== (dns_keytag_t
)dst_key_id(*keyp
) &&
2339 dst_key_iszonekey(*keyp
)) {
2348 static isc_boolean_t
2349 verify(dst_key_t
*key
, dns_name_t
*name
, dns_rdataset_t
*rdataset
,
2350 dns_rdata_t
*rdata
, isc_mem_t
*mctx
, isc_boolean_t acceptexpired
)
2352 isc_result_t result
;
2353 dns_fixedname_t fixed
;
2354 isc_boolean_t ignore
= ISC_FALSE
;
2356 dns_fixedname_init(&fixed
);
2359 result
= dns_dnssec_verify2(name
, rdataset
, key
, ignore
, mctx
,
2361 if (result
== DNS_R_SIGEXPIRED
&& acceptexpired
) {
2365 if (result
== ISC_R_SUCCESS
|| result
== DNS_R_FROMWILDCARD
)
2371 * Validate the rdataset if possible with available records.
2373 static isc_boolean_t
2374 validate(ns_client_t
*client
, dns_db_t
*db
, dns_name_t
*name
,
2375 dns_rdataset_t
*rdataset
, dns_rdataset_t
*sigrdataset
)
2377 isc_result_t result
;
2378 dns_rdata_t rdata
= DNS_RDATA_INIT
;
2379 dns_rdata_rrsig_t rrsig
;
2380 dst_key_t
*key
= NULL
;
2381 dns_rdataset_t keyrdataset
;
2383 if (sigrdataset
== NULL
|| !dns_rdataset_isassociated(sigrdataset
))
2386 for (result
= dns_rdataset_first(sigrdataset
);
2387 result
== ISC_R_SUCCESS
;
2388 result
= dns_rdataset_next(sigrdataset
)) {
2390 dns_rdata_reset(&rdata
);
2391 dns_rdataset_current(sigrdataset
, &rdata
);
2392 result
= dns_rdata_tostruct(&rdata
, &rrsig
, NULL
);
2393 if (result
!= ISC_R_SUCCESS
)
2395 if (!dns_resolver_algorithm_supported(client
->view
->resolver
,
2396 name
, rrsig
.algorithm
))
2398 if (!dns_name_issubdomain(name
, &rrsig
.signer
))
2400 dns_rdataset_init(&keyrdataset
);
2402 if (!get_key(client
, db
, &rrsig
, &keyrdataset
, &key
))
2404 if (verify(key
, name
, rdataset
, &rdata
, client
->mctx
,
2405 client
->view
->acceptexpired
)) {
2407 dns_rdataset_disassociate(&keyrdataset
);
2408 mark_secure(client
, db
, name
, rdataset
,
2414 if (dns_rdataset_isassociated(&keyrdataset
))
2415 dns_rdataset_disassociate(&keyrdataset
);
2421 query_addbestns(ns_client_t
*client
) {
2424 dns_name_t
*fname
, *zfname
;
2425 dns_rdataset_t
*rdataset
, *sigrdataset
, *zrdataset
, *zsigrdataset
;
2426 isc_boolean_t is_zone
, use_zone
;
2428 isc_result_t result
;
2429 dns_dbversion_t
*version
;
2433 CTRACE("query_addbestns");
2439 zsigrdataset
= NULL
;
2445 is_zone
= ISC_FALSE
;
2446 use_zone
= ISC_FALSE
;
2449 * Find the right database.
2451 result
= query_getdb(client
, client
->query
.qname
, dns_rdatatype_ns
, 0,
2452 &zone
, &db
, &version
, &is_zone
);
2453 if (result
!= ISC_R_SUCCESS
)
2458 * We'll need some resources...
2460 dbuf
= query_getnamebuf(client
);
2463 fname
= query_newname(client
, dbuf
, &b
);
2464 rdataset
= query_newrdataset(client
);
2465 if (fname
== NULL
|| rdataset
== NULL
)
2468 * Get the RRSIGs if the client requested them or if we may
2469 * need to validate answers from the cache.
2471 if (WANTDNSSEC(client
) || !is_zone
) {
2472 sigrdataset
= query_newrdataset(client
);
2473 if (sigrdataset
== NULL
)
2478 * Now look for the zonecut.
2481 result
= dns_db_find(db
, client
->query
.qname
, version
,
2482 dns_rdatatype_ns
, client
->query
.dboptions
,
2483 client
->now
, &node
, fname
,
2484 rdataset
, sigrdataset
);
2485 if (result
!= DNS_R_DELEGATION
)
2487 if (USECACHE(client
)) {
2488 query_keepname(client
, fname
, dbuf
);
2492 zrdataset
= rdataset
;
2494 zsigrdataset
= sigrdataset
;
2496 dns_db_detachnode(db
, &node
);
2499 dns_db_attach(client
->view
->cachedb
, &db
);
2500 is_zone
= ISC_FALSE
;
2504 result
= dns_db_findzonecut(db
, client
->query
.qname
,
2505 client
->query
.dboptions
,
2506 client
->now
, &node
, fname
,
2507 rdataset
, sigrdataset
);
2508 if (result
== ISC_R_SUCCESS
) {
2509 if (zfname
!= NULL
&&
2510 !dns_name_issubdomain(fname
, zfname
)) {
2512 * We found a zonecut in the cache, but our
2513 * zone delegation is better.
2515 use_zone
= ISC_TRUE
;
2517 } else if (result
== ISC_R_NOTFOUND
&& zfname
!= NULL
) {
2519 * We didn't find anything in the cache, but we
2520 * have a zone delegation, so use it.
2522 use_zone
= ISC_TRUE
;
2528 query_releasename(client
, &fname
);
2532 * We've already done query_keepname() on
2533 * zfname, so we must set dbuf to NULL to
2534 * prevent query_addrrset() from trying to
2535 * call query_keepname() again.
2538 query_putrdataset(client
, &rdataset
);
2539 if (sigrdataset
!= NULL
)
2540 query_putrdataset(client
, &sigrdataset
);
2541 rdataset
= zrdataset
;
2543 sigrdataset
= zsigrdataset
;
2544 zsigrdataset
= NULL
;
2548 * Attempt to validate RRsets that are pending or that are glue.
2550 if ((rdataset
->trust
== dns_trust_pending
||
2551 (sigrdataset
!= NULL
&& sigrdataset
->trust
== dns_trust_pending
))
2552 && !validate(client
, db
, fname
, rdataset
, sigrdataset
) &&
2553 (client
->query
.dboptions
& DNS_DBFIND_PENDINGOK
) == 0)
2556 if ((rdataset
->trust
== dns_trust_glue
||
2557 (sigrdataset
!= NULL
&& sigrdataset
->trust
== dns_trust_glue
)) &&
2558 !validate(client
, db
, fname
, rdataset
, sigrdataset
) &&
2559 SECURE(client
) && WANTDNSSEC(client
))
2563 * If the client doesn't want DNSSEC we can discard the sigrdataset
2566 if (!WANTDNSSEC(client
))
2567 query_putrdataset(client
, &sigrdataset
);
2568 query_addrrset(client
, &fname
, &rdataset
, &sigrdataset
, dbuf
,
2569 DNS_SECTION_AUTHORITY
);
2572 if (rdataset
!= NULL
)
2573 query_putrdataset(client
, &rdataset
);
2574 if (sigrdataset
!= NULL
)
2575 query_putrdataset(client
, &sigrdataset
);
2577 query_releasename(client
, &fname
);
2579 dns_db_detachnode(db
, &node
);
2583 dns_zone_detach(&zone
);
2585 query_putrdataset(client
, &zrdataset
);
2586 if (zsigrdataset
!= NULL
)
2587 query_putrdataset(client
, &zsigrdataset
);
2589 query_releasename(client
, &zfname
);
2590 dns_db_detach(&zdb
);
2595 query_addds(ns_client_t
*client
, dns_db_t
*db
, dns_dbnode_t
*node
,
2596 dns_dbversion_t
*version
)
2599 dns_rdataset_t
*rdataset
, *sigrdataset
;
2600 isc_result_t result
;
2602 CTRACE("query_addds");
2608 * We'll need some resources...
2610 rdataset
= query_newrdataset(client
);
2611 sigrdataset
= query_newrdataset(client
);
2612 if (rdataset
== NULL
|| sigrdataset
== NULL
)
2616 * Look for the DS record, which may or may not be present.
2618 result
= dns_db_findrdataset(db
, node
, version
, dns_rdatatype_ds
, 0,
2619 client
->now
, rdataset
, sigrdataset
);
2621 * If we didn't find it, look for an NSEC. */
2622 if (result
== ISC_R_NOTFOUND
)
2623 result
= dns_db_findrdataset(db
, node
, version
,
2624 dns_rdatatype_nsec
, 0, client
->now
,
2625 rdataset
, sigrdataset
);
2626 if (result
!= ISC_R_SUCCESS
&& result
!= ISC_R_NOTFOUND
)
2628 if (!dns_rdataset_isassociated(rdataset
) ||
2629 !dns_rdataset_isassociated(sigrdataset
))
2633 * We've already added the NS record, so if the name's not there,
2634 * we have other problems. Use this name rather than calling
2637 result
= dns_message_firstname(client
->message
, DNS_SECTION_AUTHORITY
);
2638 if (result
!= ISC_R_SUCCESS
)
2642 dns_message_currentname(client
->message
, DNS_SECTION_AUTHORITY
,
2644 result
= dns_message_findtype(rname
, dns_rdatatype_ns
, 0, NULL
);
2645 if (result
!= ISC_R_SUCCESS
)
2648 ISC_LIST_APPEND(rname
->list
, rdataset
, link
);
2649 ISC_LIST_APPEND(rname
->list
, sigrdataset
, link
);
2654 if (rdataset
!= NULL
)
2655 query_putrdataset(client
, &rdataset
);
2656 if (sigrdataset
!= NULL
)
2657 query_putrdataset(client
, &sigrdataset
);
2661 query_addwildcardproof(ns_client_t
*client
, dns_db_t
*db
,
2662 dns_dbversion_t
*version
, dns_name_t
*name
,
2663 isc_boolean_t ispositive
)
2665 isc_buffer_t
*dbuf
, b
;
2667 dns_rdataset_t
*rdataset
, *sigrdataset
;
2668 dns_fixedname_t wfixed
;
2671 unsigned int options
;
2672 unsigned int olabels
, nlabels
;
2673 isc_result_t result
;
2674 dns_rdata_t rdata
= DNS_RDATA_INIT
;
2675 dns_rdata_nsec_t nsec
;
2676 isc_boolean_t have_wname
;
2679 CTRACE("query_addwildcardproof");
2686 * Get the NOQNAME proof then if !ispositve
2687 * get the NOWILDCARD proof.
2689 * DNS_DBFIND_NOWILD finds the NSEC records that covers the
2690 * name ignoring any wildcard. From the owner and next names
2691 * of this record you can compute which wildcard (if it exists)
2692 * will match by finding the longest common suffix of the
2693 * owner name and next names with the qname and prefixing that
2694 * with the wildcard label.
2699 * example NSEC b.example
2701 * b.example NSEC a.d.example
2703 * a.d.example NSEC g.f.example
2705 * g.f.example NSEC z.i.example
2707 * z.i.example NSEC example
2710 * a.example -> example NSEC b.example
2711 * owner common example
2712 * next common example
2714 * d.b.example -> b.example NSEC a.d.example
2715 * owner common b.example
2716 * next common example
2718 * a.f.example -> a.d.example NSEC g.f.example
2719 * owner common example
2720 * next common f.example
2722 * j.example -> z.i.example NSEC example
2723 * owner common example
2724 * next common example
2727 options
= client
->query
.dboptions
| DNS_DBFIND_NOWILD
;
2728 dns_fixedname_init(&wfixed
);
2729 wname
= dns_fixedname_name(&wfixed
);
2731 have_wname
= ISC_FALSE
;
2733 * We'll need some resources...
2735 dbuf
= query_getnamebuf(client
);
2738 fname
= query_newname(client
, dbuf
, &b
);
2739 rdataset
= query_newrdataset(client
);
2740 sigrdataset
= query_newrdataset(client
);
2741 if (fname
== NULL
|| rdataset
== NULL
|| sigrdataset
== NULL
)
2744 result
= dns_db_find(db
, name
, version
, dns_rdatatype_nsec
, options
,
2745 0, &node
, fname
, rdataset
, sigrdataset
);
2747 dns_db_detachnode(db
, &node
);
2748 if (result
== DNS_R_NXDOMAIN
) {
2750 result
= dns_rdataset_first(rdataset
);
2751 if (result
== ISC_R_SUCCESS
) {
2752 dns_rdataset_current(rdataset
, &rdata
);
2753 result
= dns_rdata_tostruct(&rdata
, &nsec
, NULL
);
2755 if (result
== ISC_R_SUCCESS
) {
2756 (void)dns_name_fullcompare(name
, fname
, &order
,
2758 (void)dns_name_fullcompare(name
, &nsec
.next
, &order
,
2760 if (olabels
> nlabels
)
2761 dns_name_split(name
, olabels
, NULL
, wname
);
2763 dns_name_split(name
, nlabels
, NULL
, wname
);
2764 result
= dns_name_concatenate(dns_wildcardname
,
2765 wname
, wname
, NULL
);
2766 if (result
== ISC_R_SUCCESS
)
2767 have_wname
= ISC_TRUE
;
2768 dns_rdata_freestruct(&nsec
);
2770 query_addrrset(client
, &fname
, &rdataset
, &sigrdataset
,
2771 dbuf
, DNS_SECTION_AUTHORITY
);
2773 if (rdataset
!= NULL
)
2774 query_putrdataset(client
, &rdataset
);
2775 if (sigrdataset
!= NULL
)
2776 query_putrdataset(client
, &sigrdataset
);
2778 query_releasename(client
, &fname
);
2780 ispositive
= ISC_TRUE
; /* prevent loop */
2781 if (!dns_name_equal(name
, wname
)) {
2787 if (rdataset
!= NULL
)
2788 query_putrdataset(client
, &rdataset
);
2789 if (sigrdataset
!= NULL
)
2790 query_putrdataset(client
, &sigrdataset
);
2792 query_releasename(client
, &fname
);
2796 query_addnxrrsetnsec(ns_client_t
*client
, dns_db_t
*db
,
2797 dns_dbversion_t
*version
, dns_name_t
**namep
,
2798 dns_rdataset_t
**rdatasetp
, dns_rdataset_t
**sigrdatasetp
)
2801 dns_rdataset_t
*sigrdataset
;
2802 dns_rdata_t sigrdata
;
2803 dns_rdata_rrsig_t sig
;
2804 unsigned int labels
;
2805 isc_buffer_t
*dbuf
, b
;
2807 isc_result_t result
;
2810 if ((name
->attributes
& DNS_NAMEATTR_WILDCARD
) == 0) {
2811 query_addrrset(client
, namep
, rdatasetp
, sigrdatasetp
,
2812 NULL
, DNS_SECTION_AUTHORITY
);
2816 if (sigrdatasetp
== NULL
)
2818 sigrdataset
= *sigrdatasetp
;
2819 if (sigrdataset
== NULL
|| !dns_rdataset_isassociated(sigrdataset
))
2821 result
= dns_rdataset_first(sigrdataset
);
2822 if (result
!= ISC_R_SUCCESS
)
2824 dns_rdata_init(&sigrdata
);
2825 dns_rdataset_current(sigrdataset
, &sigrdata
);
2826 result
= dns_rdata_tostruct(&sigrdata
, &sig
, NULL
);
2827 if (result
!= ISC_R_SUCCESS
)
2830 labels
= dns_name_countlabels(name
);
2831 if ((unsigned int)sig
.labels
+ 1 >= labels
)
2835 query_addwildcardproof(client
, db
, version
, client
->query
.qname
,
2839 * We'll need some resources...
2841 dbuf
= query_getnamebuf(client
);
2844 fname
= query_newname(client
, dbuf
, &b
);
2847 dns_name_split(name
, sig
.labels
+ 1, NULL
, fname
);
2848 /* This will succeed, since we've stripped labels. */
2849 RUNTIME_CHECK(dns_name_concatenate(dns_wildcardname
, fname
, fname
,
2850 NULL
) == ISC_R_SUCCESS
);
2851 query_addrrset(client
, &fname
, rdatasetp
, sigrdatasetp
,
2852 dbuf
, DNS_SECTION_AUTHORITY
);
2856 query_resume(isc_task_t
*task
, isc_event_t
*event
) {
2857 dns_fetchevent_t
*devent
= (dns_fetchevent_t
*)event
;
2858 ns_client_t
*client
;
2859 isc_boolean_t fetch_cancelled
, client_shuttingdown
;
2862 * Resume a query after recursion.
2867 REQUIRE(event
->ev_type
== DNS_EVENT_FETCHDONE
);
2868 client
= devent
->ev_arg
;
2869 REQUIRE(NS_CLIENT_VALID(client
));
2870 REQUIRE(task
== client
->task
);
2871 REQUIRE(RECURSING(client
));
2873 LOCK(&client
->query
.fetchlock
);
2874 if (client
->query
.fetch
!= NULL
) {
2876 * This is the fetch we've been waiting for.
2878 INSIST(devent
->fetch
== client
->query
.fetch
);
2879 client
->query
.fetch
= NULL
;
2880 fetch_cancelled
= ISC_FALSE
;
2882 * Update client->now.
2884 isc_stdtime_get(&client
->now
);
2887 * This is a fetch completion event for a cancelled fetch.
2888 * Clean up and don't resume the find.
2890 fetch_cancelled
= ISC_TRUE
;
2892 UNLOCK(&client
->query
.fetchlock
);
2893 INSIST(client
->query
.fetch
== NULL
);
2895 client
->query
.attributes
&= ~NS_QUERYATTR_RECURSING
;
2896 dns_resolver_destroyfetch(&devent
->fetch
);
2899 * If this client is shutting down, or this transaction
2900 * has timed out, do not resume the find.
2902 client_shuttingdown
= ns_client_shuttingdown(client
);
2903 if (fetch_cancelled
|| client_shuttingdown
) {
2904 if (devent
->node
!= NULL
)
2905 dns_db_detachnode(devent
->db
, &devent
->node
);
2906 if (devent
->db
!= NULL
)
2907 dns_db_detach(&devent
->db
);
2908 query_putrdataset(client
, &devent
->rdataset
);
2909 if (devent
->sigrdataset
!= NULL
)
2910 query_putrdataset(client
, &devent
->sigrdataset
);
2911 isc_event_free(&event
);
2912 if (fetch_cancelled
)
2913 query_error(client
, DNS_R_SERVFAIL
);
2915 query_next(client
, ISC_R_CANCELED
);
2917 * This may destroy the client.
2919 ns_client_detach(&client
);
2921 query_find(client
, devent
, 0);
2926 query_recurse(ns_client_t
*client
, dns_rdatatype_t qtype
, dns_name_t
*qdomain
,
2927 dns_rdataset_t
*nameservers
)
2929 isc_result_t result
;
2930 dns_rdataset_t
*rdataset
, *sigrdataset
;
2931 isc_sockaddr_t
*peeraddr
;
2933 inc_stats(client
, dns_statscounter_recursion
);
2936 * We are about to recurse, which means that this client will
2937 * be unavailable for serving new requests for an indeterminate
2938 * amount of time. If this client is currently responsible
2939 * for handling incoming queries, set up a new client
2940 * object to handle them while we are waiting for a
2941 * response. There is no need to replace TCP clients
2942 * because those have already been replaced when the
2943 * connection was accepted (if allowed by the TCP quota).
2945 if (client
->recursionquota
== NULL
) {
2946 result
= isc_quota_attach(&ns_g_server
->recursionquota
,
2947 &client
->recursionquota
);
2948 if (result
== ISC_R_SOFTQUOTA
) {
2949 static isc_stdtime_t last
= 0;
2951 isc_stdtime_get(&now
);
2954 ns_client_log(client
, NS_LOGCATEGORY_CLIENT
,
2957 "recursive-clients soft limit "
2958 "exceeded, aborting oldest query");
2960 ns_client_killoldestquery(client
);
2961 result
= ISC_R_SUCCESS
;
2962 } else if (result
== ISC_R_QUOTA
) {
2963 static isc_stdtime_t last
= 0;
2965 isc_stdtime_get(&now
);
2968 ns_client_log(client
, NS_LOGCATEGORY_CLIENT
,
2971 "no more recursive clients: %s",
2972 isc_result_totext(result
));
2974 ns_client_killoldestquery(client
);
2976 if (result
== ISC_R_SUCCESS
&& !client
->mortal
&&
2977 (client
->attributes
& NS_CLIENTATTR_TCP
) == 0) {
2978 result
= ns_client_replace(client
);
2979 if (result
!= ISC_R_SUCCESS
) {
2980 ns_client_log(client
, NS_LOGCATEGORY_CLIENT
,
2983 "ns_client_replace() failed: %s",
2984 isc_result_totext(result
));
2985 isc_quota_detach(&client
->recursionquota
);
2988 if (result
!= ISC_R_SUCCESS
)
2990 ns_client_recursing(client
);
2994 * Invoke the resolver.
2996 REQUIRE(nameservers
== NULL
|| nameservers
->type
== dns_rdatatype_ns
);
2997 REQUIRE(client
->query
.fetch
== NULL
);
2999 rdataset
= query_newrdataset(client
);
3000 if (rdataset
== NULL
)
3001 return (ISC_R_NOMEMORY
);
3002 if (WANTDNSSEC(client
)) {
3003 sigrdataset
= query_newrdataset(client
);
3004 if (sigrdataset
== NULL
) {
3005 query_putrdataset(client
, &rdataset
);
3006 return (ISC_R_NOMEMORY
);
3011 if (client
->query
.timerset
== ISC_FALSE
)
3012 ns_client_settimeout(client
, 60);
3013 if ((client
->attributes
& NS_CLIENTATTR_TCP
) == 0)
3014 peeraddr
= &client
->peeraddr
;
3017 result
= dns_resolver_createfetch2(client
->view
->resolver
,
3018 client
->query
.qname
,
3019 qtype
, qdomain
, nameservers
,
3020 NULL
, peeraddr
, client
->message
->id
,
3021 client
->query
.fetchoptions
,
3023 query_resume
, client
,
3024 rdataset
, sigrdataset
,
3025 &client
->query
.fetch
);
3027 if (result
== ISC_R_SUCCESS
) {
3029 * Record that we're waiting for an event. A client which
3030 * is shutting down will not be destroyed until all the
3031 * events have been received.
3034 query_putrdataset(client
, &rdataset
);
3035 if (sigrdataset
!= NULL
)
3036 query_putrdataset(client
, &sigrdataset
);
3042 #define MAX_RESTARTS 16
3044 #define QUERY_ERROR(r) \
3047 want_restart = ISC_FALSE; \
3051 * Extract a network address from the RDATA of an A or AAAA
3056 * ISC_R_NOTIMPLEMENTED The rdata is not a known address type.
3059 rdata_tonetaddr(const dns_rdata_t
*rdata
, isc_netaddr_t
*netaddr
) {
3061 struct in6_addr in6a
;
3063 switch (rdata
->type
) {
3064 case dns_rdatatype_a
:
3065 INSIST(rdata
->length
== 4);
3066 memcpy(&ina
.s_addr
, rdata
->data
, 4);
3067 isc_netaddr_fromin(netaddr
, &ina
);
3068 return (ISC_R_SUCCESS
);
3069 case dns_rdatatype_aaaa
:
3070 INSIST(rdata
->length
== 16);
3071 memcpy(in6a
.s6_addr
, rdata
->data
, 16);
3072 isc_netaddr_fromin6(netaddr
, &in6a
);
3073 return (ISC_R_SUCCESS
);
3075 return (ISC_R_NOTIMPLEMENTED
);
3080 * Find the sort order of 'rdata' in the topology-like
3081 * ACL forming the second element in a 2-element top-level
3082 * sortlist statement.
3085 query_sortlist_order_2element(const dns_rdata_t
*rdata
, const void *arg
) {
3086 isc_netaddr_t netaddr
;
3088 if (rdata_tonetaddr(rdata
, &netaddr
) != ISC_R_SUCCESS
)
3090 return (ns_sortlist_addrorder2(&netaddr
, arg
));
3094 * Find the sort order of 'rdata' in the matching element
3095 * of a 1-element top-level sortlist statement.
3098 query_sortlist_order_1element(const dns_rdata_t
*rdata
, const void *arg
) {
3099 isc_netaddr_t netaddr
;
3101 if (rdata_tonetaddr(rdata
, &netaddr
) != ISC_R_SUCCESS
)
3103 return (ns_sortlist_addrorder1(&netaddr
, arg
));
3107 * Find the sortlist statement that applies to 'client' and set up
3108 * the sortlist info in in client->message appropriately.
3111 setup_query_sortlist(ns_client_t
*client
) {
3112 isc_netaddr_t netaddr
;
3113 dns_rdatasetorderfunc_t order
= NULL
;
3114 const void *order_arg
= NULL
;
3116 isc_netaddr_fromsockaddr(&netaddr
, &client
->peeraddr
);
3117 switch (ns_sortlist_setup(client
->view
->sortlist
,
3118 &netaddr
, &order_arg
)) {
3119 case NS_SORTLISTTYPE_1ELEMENT
:
3120 order
= query_sortlist_order_1element
;
3122 case NS_SORTLISTTYPE_2ELEMENT
:
3123 order
= query_sortlist_order_2element
;
3125 case NS_SORTLISTTYPE_NONE
:
3132 dns_message_setsortorder(client
->message
, order
, order_arg
);
3136 query_addnoqnameproof(ns_client_t
*client
, dns_rdataset_t
*rdataset
) {
3137 isc_buffer_t
*dbuf
, b
;
3139 dns_rdataset_t
*nsec
, *nsecsig
;
3140 isc_result_t result
= ISC_R_NOMEMORY
;
3142 CTRACE("query_addnoqnameproof");
3148 dbuf
= query_getnamebuf(client
);
3151 fname
= query_newname(client
, dbuf
, &b
);
3152 nsec
= query_newrdataset(client
);
3153 nsecsig
= query_newrdataset(client
);
3154 if (fname
== NULL
|| nsec
== NULL
|| nsecsig
== NULL
)
3157 result
= dns_rdataset_getnoqname(rdataset
, fname
, nsec
, nsecsig
);
3158 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3160 query_addrrset(client
, &fname
, &nsec
, &nsecsig
, dbuf
,
3161 DNS_SECTION_AUTHORITY
);
3165 query_putrdataset(client
, &nsec
);
3166 if (nsecsig
!= NULL
)
3167 query_putrdataset(client
, &nsecsig
);
3169 query_releasename(client
, &fname
);
3173 answer_in_glue(ns_client_t
*client
, dns_rdatatype_t qtype
) {
3176 dns_section_t section
= DNS_SECTION_ADDITIONAL
;
3177 dns_rdataset_t
*rdataset
= NULL
;
3179 msg
= client
->message
;
3180 for (name
= ISC_LIST_HEAD(msg
->sections
[section
]);
3182 name
= ISC_LIST_NEXT(name
, link
))
3183 if (dns_name_equal(name
, client
->query
.qname
)) {
3184 for (rdataset
= ISC_LIST_HEAD(name
->list
);
3186 rdataset
= ISC_LIST_NEXT(rdataset
, link
))
3187 if (rdataset
->type
== qtype
)
3191 if (rdataset
!= NULL
) {
3192 ISC_LIST_UNLINK(msg
->sections
[section
], name
, link
);
3193 ISC_LIST_PREPEND(msg
->sections
[section
], name
, link
);
3194 ISC_LIST_UNLINK(name
->list
, rdataset
, link
);
3195 ISC_LIST_PREPEND(name
->list
, rdataset
, link
);
3196 rdataset
->attributes
|= DNS_RDATASETATTR_REQUIREDGLUE
;
3200 #define NS_NAME_INIT(A,B) \
3203 A, sizeof(A), sizeof(B), \
3204 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE, \
3205 B, NULL, { (void *)-1, (void *)-1}, \
3209 static unsigned char inaddr10_offsets
[] = { 0, 3, 11, 16 };
3210 static unsigned char inaddr172_offsets
[] = { 0, 3, 7, 15, 20 };
3211 static unsigned char inaddr192_offsets
[] = { 0, 4, 8, 16, 21 };
3213 static unsigned char inaddr10
[] = "\00210\007IN-ADDR\004ARPA";
3215 static unsigned char inaddr16172
[] = "\00216\003172\007IN-ADDR\004ARPA";
3216 static unsigned char inaddr17172
[] = "\00217\003172\007IN-ADDR\004ARPA";
3217 static unsigned char inaddr18172
[] = "\00218\003172\007IN-ADDR\004ARPA";
3218 static unsigned char inaddr19172
[] = "\00219\003172\007IN-ADDR\004ARPA";
3219 static unsigned char inaddr20172
[] = "\00220\003172\007IN-ADDR\004ARPA";
3220 static unsigned char inaddr21172
[] = "\00221\003172\007IN-ADDR\004ARPA";
3221 static unsigned char inaddr22172
[] = "\00222\003172\007IN-ADDR\004ARPA";
3222 static unsigned char inaddr23172
[] = "\00223\003172\007IN-ADDR\004ARPA";
3223 static unsigned char inaddr24172
[] = "\00224\003172\007IN-ADDR\004ARPA";
3224 static unsigned char inaddr25172
[] = "\00225\003172\007IN-ADDR\004ARPA";
3225 static unsigned char inaddr26172
[] = "\00226\003172\007IN-ADDR\004ARPA";
3226 static unsigned char inaddr27172
[] = "\00227\003172\007IN-ADDR\004ARPA";
3227 static unsigned char inaddr28172
[] = "\00228\003172\007IN-ADDR\004ARPA";
3228 static unsigned char inaddr29172
[] = "\00229\003172\007IN-ADDR\004ARPA";
3229 static unsigned char inaddr30172
[] = "\00230\003172\007IN-ADDR\004ARPA";
3230 static unsigned char inaddr31172
[] = "\00231\003172\007IN-ADDR\004ARPA";
3232 static unsigned char inaddr168192
[] = "\003168\003192\007IN-ADDR\004ARPA";
3234 static dns_name_t rfc1918names
[] = {
3235 NS_NAME_INIT(inaddr10
, inaddr10_offsets
),
3236 NS_NAME_INIT(inaddr16172
, inaddr172_offsets
),
3237 NS_NAME_INIT(inaddr17172
, inaddr172_offsets
),
3238 NS_NAME_INIT(inaddr18172
, inaddr172_offsets
),
3239 NS_NAME_INIT(inaddr19172
, inaddr172_offsets
),
3240 NS_NAME_INIT(inaddr20172
, inaddr172_offsets
),
3241 NS_NAME_INIT(inaddr21172
, inaddr172_offsets
),
3242 NS_NAME_INIT(inaddr22172
, inaddr172_offsets
),
3243 NS_NAME_INIT(inaddr23172
, inaddr172_offsets
),
3244 NS_NAME_INIT(inaddr24172
, inaddr172_offsets
),
3245 NS_NAME_INIT(inaddr25172
, inaddr172_offsets
),
3246 NS_NAME_INIT(inaddr26172
, inaddr172_offsets
),
3247 NS_NAME_INIT(inaddr27172
, inaddr172_offsets
),
3248 NS_NAME_INIT(inaddr28172
, inaddr172_offsets
),
3249 NS_NAME_INIT(inaddr29172
, inaddr172_offsets
),
3250 NS_NAME_INIT(inaddr30172
, inaddr172_offsets
),
3251 NS_NAME_INIT(inaddr31172
, inaddr172_offsets
),
3252 NS_NAME_INIT(inaddr168192
, inaddr192_offsets
)
3256 static unsigned char prisoner_data
[] = "\010prisoner\004iana\003org";
3257 static unsigned char hostmaster_data
[] = "\012hostmaster\014root-servers\003org";
3259 static unsigned char prisoner_offsets
[] = { 0, 9, 14, 18 };
3260 static unsigned char hostmaster_offsets
[] = { 0, 11, 24, 28 };
3262 static dns_name_t prisoner
= NS_NAME_INIT(prisoner_data
, prisoner_offsets
);
3263 static dns_name_t hostmaster
= NS_NAME_INIT(hostmaster_data
, hostmaster_offsets
);
3266 warn_rfc1918(ns_client_t
*client
, dns_name_t
*fname
, dns_rdataset_t
*rdataset
) {
3268 dns_rdata_t rdata
= DNS_RDATA_INIT
;
3269 dns_rdata_soa_t soa
;
3270 dns_rdataset_t found
;
3271 isc_result_t result
;
3273 for (i
= 0; i
< (sizeof(rfc1918names
)/sizeof(*rfc1918names
)); i
++) {
3274 if (dns_name_issubdomain(fname
, &rfc1918names
[i
])) {
3275 dns_rdataset_init(&found
);
3276 result
= dns_ncache_getrdataset(rdataset
,
3280 if (result
!= ISC_R_SUCCESS
)
3283 result
= dns_rdataset_first(&found
);
3284 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3285 dns_rdataset_current(&found
, &rdata
);
3286 result
= dns_rdata_tostruct(&rdata
, &soa
, NULL
);
3287 if (result
!= ISC_R_SUCCESS
)
3289 if (dns_name_equal(&soa
.origin
, &prisoner
) &&
3290 dns_name_equal(&soa
.contact
, &hostmaster
)) {
3291 char buf
[DNS_NAME_FORMATSIZE
];
3292 dns_name_format(fname
, buf
, sizeof(buf
));
3293 ns_client_log(client
, DNS_LOGCATEGORY_SECURITY
,
3296 "RFC 1918 response from "
3297 "Internet for %s", buf
);
3299 dns_rdataset_disassociate(&found
);
3306 * Do the bulk of query processing for the current query of 'client'.
3307 * If 'event' is non-NULL, we are returning from recursion and 'qtype'
3308 * is ignored. Otherwise, 'qtype' is the query type.
3311 query_find(ns_client_t
*client
, dns_fetchevent_t
*event
, dns_rdatatype_t qtype
)
3315 dns_rdatatype_t type
;
3316 dns_name_t
*fname
, *zfname
, *tname
, *prefix
;
3317 dns_rdataset_t
*rdataset
, *trdataset
;
3318 dns_rdataset_t
*sigrdataset
, *zrdataset
, *zsigrdataset
;
3319 dns_rdataset_t
**sigrdatasetp
;
3320 dns_rdata_t rdata
= DNS_RDATA_INIT
;
3321 dns_rdatasetiter_t
*rdsiter
;
3322 isc_boolean_t want_restart
, authoritative
, is_zone
, need_wildcardproof
;
3323 unsigned int n
, nlabels
;
3324 dns_namereln_t namereln
;
3328 isc_result_t result
, eresult
;
3329 dns_fixedname_t fixed
;
3330 dns_fixedname_t wildcardname
;
3331 dns_dbversion_t
*version
;
3333 dns_rdata_cname_t cname
;
3334 dns_rdata_dname_t dname
;
3335 unsigned int options
;
3336 isc_boolean_t empty_wild
;
3337 dns_rdataset_t
*noqname
;
3339 CTRACE("query_find");
3342 * One-time initialization.
3344 * It's especially important to initialize anything that the cleanup
3345 * code might cleanup.
3348 eresult
= ISC_R_SUCCESS
;
3354 zsigrdataset
= NULL
;
3360 need_wildcardproof
= ISC_FALSE
;
3361 empty_wild
= ISC_FALSE
;
3364 if (event
!= NULL
) {
3366 * We're returning from recursion. Restore the query context
3370 want_restart
= ISC_FALSE
;
3371 authoritative
= ISC_FALSE
;
3372 is_zone
= ISC_FALSE
;
3374 qtype
= event
->qtype
;
3375 if (qtype
== dns_rdatatype_rrsig
|| qtype
== dns_rdatatype_sig
)
3376 type
= dns_rdatatype_any
;
3381 rdataset
= event
->rdataset
;
3382 sigrdataset
= event
->sigrdataset
;
3385 * We'll need some resources...
3387 dbuf
= query_getnamebuf(client
);
3389 QUERY_ERROR(DNS_R_SERVFAIL
);
3392 fname
= query_newname(client
, dbuf
, &b
);
3393 if (fname
== NULL
) {
3394 QUERY_ERROR(DNS_R_SERVFAIL
);
3397 tname
= dns_fixedname_name(&event
->foundname
);
3398 result
= dns_name_copy(tname
, fname
, NULL
);
3399 if (result
!= ISC_R_SUCCESS
) {
3400 QUERY_ERROR(DNS_R_SERVFAIL
);
3404 result
= event
->result
;
3410 * Not returning from recursion.
3414 * If it's a SIG query, we'll iterate the node.
3416 if (qtype
== dns_rdatatype_rrsig
|| qtype
== dns_rdatatype_sig
)
3417 type
= dns_rdatatype_any
;
3422 CTRACE("query_find: restart");
3423 want_restart
= ISC_FALSE
;
3424 authoritative
= ISC_FALSE
;
3426 need_wildcardproof
= ISC_FALSE
;
3428 if (client
->view
->checknames
&&
3429 !dns_rdata_checkowner(client
->query
.qname
,
3430 client
->message
->rdclass
,
3431 qtype
, ISC_FALSE
)) {
3432 char namebuf
[DNS_NAME_FORMATSIZE
];
3433 char typename
[DNS_RDATATYPE_FORMATSIZE
];
3434 char classname
[DNS_RDATACLASS_FORMATSIZE
];
3436 dns_name_format(client
->query
.qname
, namebuf
, sizeof(namebuf
));
3437 dns_rdatatype_format(qtype
, typename
, sizeof(typename
));
3438 dns_rdataclass_format(client
->message
->rdclass
, classname
,
3440 ns_client_log(client
, DNS_LOGCATEGORY_SECURITY
,
3441 NS_LOGMODULE_QUERY
, ISC_LOG_ERROR
,
3442 "check-names failure %s/%s/%s", namebuf
,
3443 typename
, classname
);
3444 QUERY_ERROR(DNS_R_REFUSED
);
3449 * First we must find the right database.
3451 options
&= DNS_GETDB_NOLOG
; /* Preserve DNS_GETDB_NOLOG. */
3452 if (dns_rdatatype_atparent(qtype
) &&
3453 !dns_name_equal(client
->query
.qname
, dns_rootname
))
3454 options
|= DNS_GETDB_NOEXACT
;
3455 result
= query_getdb(client
, client
->query
.qname
, qtype
, options
,
3456 &zone
, &db
, &version
, &is_zone
);
3457 if ((result
!= ISC_R_SUCCESS
|| !is_zone
) && !RECURSIONOK(client
) &&
3458 (options
& DNS_GETDB_NOEXACT
) != 0 && qtype
== dns_rdatatype_ds
) {
3460 * Look to see if we are authoritative for the
3461 * child zone if the query type is DS.
3463 dns_db_t
*tdb
= NULL
;
3464 dns_zone_t
*tzone
= NULL
;
3465 dns_dbversion_t
*tversion
= NULL
;
3466 isc_result_t tresult
;
3468 tresult
= query_getzonedb(client
, client
->query
.qname
, qtype
,
3469 DNS_GETDB_PARTIAL
, &tzone
, &tdb
,
3471 if (tresult
== ISC_R_SUCCESS
) {
3472 options
&= ~DNS_GETDB_NOEXACT
;
3473 query_putrdataset(client
, &rdataset
);
3477 dns_zone_detach(&zone
);
3482 result
= ISC_R_SUCCESS
;
3485 dns_db_detach(&tdb
);
3487 dns_zone_detach(&tzone
);
3490 if (result
!= ISC_R_SUCCESS
) {
3491 if (result
== DNS_R_REFUSED
) {
3492 if (!PARTIALANSWER(client
))
3493 QUERY_ERROR(DNS_R_REFUSED
);
3495 QUERY_ERROR(DNS_R_SERVFAIL
);
3500 authoritative
= ISC_TRUE
;
3502 if (event
== NULL
&& client
->query
.restarts
== 0) {
3507 * if is_zone = true, zone = NULL then this is
3508 * a DLZ zone. Don't attempt to attach zone.
3511 dns_zone_attach(zone
, &client
->query
.authzone
);
3515 dns_db_attach(db
, &client
->query
.authdb
);
3517 client
->query
.authdbset
= ISC_TRUE
;
3521 CTRACE("query_find: db_find");
3523 * We'll need some resources...
3525 dbuf
= query_getnamebuf(client
);
3527 QUERY_ERROR(DNS_R_SERVFAIL
);
3530 fname
= query_newname(client
, dbuf
, &b
);
3531 rdataset
= query_newrdataset(client
);
3532 if (fname
== NULL
|| rdataset
== NULL
) {
3533 QUERY_ERROR(DNS_R_SERVFAIL
);
3536 if (WANTDNSSEC(client
)) {
3537 sigrdataset
= query_newrdataset(client
);
3538 if (sigrdataset
== NULL
) {
3539 QUERY_ERROR(DNS_R_SERVFAIL
);
3545 * Now look for an answer in the database.
3547 result
= dns_db_find(db
, client
->query
.qname
, version
, type
,
3548 client
->query
.dboptions
, client
->now
,
3549 &node
, fname
, rdataset
, sigrdataset
);
3552 CTRACE("query_find: resume");
3556 * This case is handled in the main line below.
3562 * These cases are handled in the main line below.
3565 authoritative
= ISC_FALSE
;
3567 case ISC_R_NOTFOUND
:
3569 * The cache doesn't even have the root NS. Get them from
3576 if (client
->view
->hints
== NULL
) {
3577 /* We have no hints. */
3578 result
= ISC_R_FAILURE
;
3580 dns_db_attach(client
->view
->hints
, &db
);
3581 result
= dns_db_find(db
, dns_rootname
,
3582 NULL
, dns_rdatatype_ns
,
3583 0, client
->now
, &node
, fname
,
3584 rdataset
, sigrdataset
);
3586 if (result
!= ISC_R_SUCCESS
) {
3588 * Nonsensical root hints may require cleanup.
3590 if (dns_rdataset_isassociated(rdataset
))
3591 dns_rdataset_disassociate(rdataset
);
3592 if (sigrdataset
!= NULL
&&
3593 dns_rdataset_isassociated(sigrdataset
))
3594 dns_rdataset_disassociate(sigrdataset
);
3596 dns_db_detachnode(db
, &node
);
3599 * We don't have any root server hints, but
3600 * we may have working forwarders, so try to
3603 if (RECURSIONOK(client
)) {
3604 result
= query_recurse(client
, qtype
,
3606 if (result
== ISC_R_SUCCESS
)
3607 client
->query
.attributes
|=
3608 NS_QUERYATTR_RECURSING
;
3609 else if (result
== DNS_R_DUPLICATE
||
3610 result
== DNS_R_DROP
) {
3611 /* Duplicate query. */
3612 QUERY_ERROR(result
);
3614 /* Unable to recurse. */
3615 QUERY_ERROR(DNS_R_SERVFAIL
);
3619 /* Unable to give root server referral. */
3620 QUERY_ERROR(DNS_R_SERVFAIL
);
3625 * XXXRTH We should trigger root server priming here.
3628 case DNS_R_DELEGATION
:
3629 authoritative
= ISC_FALSE
;
3632 * Look to see if we are authoritative for the
3633 * child zone if the query type is DS.
3635 if (!RECURSIONOK(client
) &&
3636 (options
& DNS_GETDB_NOEXACT
) != 0 &&
3637 qtype
== dns_rdatatype_ds
) {
3638 dns_db_t
*tdb
= NULL
;
3639 dns_zone_t
*tzone
= NULL
;
3640 dns_dbversion_t
*tversion
= NULL
;
3641 result
= query_getzonedb(client
,
3642 client
->query
.qname
,
3647 if (result
== ISC_R_SUCCESS
) {
3648 options
&= ~DNS_GETDB_NOEXACT
;
3649 query_putrdataset(client
, &rdataset
);
3650 if (sigrdataset
!= NULL
)
3651 query_putrdataset(client
,
3654 query_releasename(client
,
3657 dns_db_detachnode(db
, &node
);
3661 dns_zone_detach(&zone
);
3665 authoritative
= ISC_TRUE
;
3669 dns_db_detach(&tdb
);
3671 dns_zone_detach(&tzone
);
3674 * We're authoritative for an ancestor of QNAME.
3676 if (!USECACHE(client
) || !RECURSIONOK(client
)) {
3678 * If we don't have a cache, this is the best
3681 * If the client is making a nonrecursive
3682 * query we always give out the authoritative
3683 * delegation. This way even if we get
3684 * junk in our cache, we won't fail in our
3685 * role as the delegating authority if another
3686 * nameserver asks us about a delegated
3689 * We enable the retrieval of glue for this
3690 * database by setting client->query.gluedb.
3692 client
->query
.gluedb
= db
;
3693 client
->query
.isreferral
= ISC_TRUE
;
3695 * We must ensure NOADDITIONAL is off,
3696 * because the generation of
3697 * additional data is required in
3700 client
->query
.attributes
&=
3701 ~NS_QUERYATTR_NOADDITIONAL
;
3702 if (sigrdataset
!= NULL
)
3703 sigrdatasetp
= &sigrdataset
;
3705 sigrdatasetp
= NULL
;
3706 query_addrrset(client
, &fname
,
3707 &rdataset
, sigrdatasetp
,
3708 dbuf
, DNS_SECTION_AUTHORITY
);
3709 client
->query
.gluedb
= NULL
;
3710 if (WANTDNSSEC(client
) && dns_db_issecure(db
))
3711 query_addds(client
, db
, node
, version
);
3714 * We might have a better answer or delegation
3715 * in the cache. We'll remember the current
3716 * values of fname, rdataset, and sigrdataset.
3717 * We'll then go looking for QNAME in the
3718 * cache. If we find something better, we'll
3721 query_keepname(client
, fname
, dbuf
);
3725 zrdataset
= rdataset
;
3727 zsigrdataset
= sigrdataset
;
3729 dns_db_detachnode(db
, &node
);
3732 dns_db_attach(client
->view
->cachedb
, &db
);
3733 is_zone
= ISC_FALSE
;
3737 if (zfname
!= NULL
&&
3738 !dns_name_issubdomain(fname
, zfname
)) {
3740 * We've already got a delegation from
3741 * authoritative data, and it is better
3742 * than what we found in the cache. Use
3743 * it instead of the cache delegation.
3745 query_releasename(client
, &fname
);
3749 * We've already done query_keepname() on
3750 * zfname, so we must set dbuf to NULL to
3751 * prevent query_addrrset() from trying to
3752 * call query_keepname() again.
3755 query_putrdataset(client
, &rdataset
);
3756 if (sigrdataset
!= NULL
)
3757 query_putrdataset(client
,
3759 rdataset
= zrdataset
;
3761 sigrdataset
= zsigrdataset
;
3762 zsigrdataset
= NULL
;
3764 * We don't clean up zdb here because we
3765 * may still need it. It will get cleaned
3766 * up by the main cleanup code.
3770 if (RECURSIONOK(client
)) {
3774 if (dns_rdatatype_atparent(type
))
3775 result
= query_recurse(client
, qtype
,
3778 result
= query_recurse(client
, qtype
,
3780 if (result
== ISC_R_SUCCESS
)
3781 client
->query
.attributes
|=
3782 NS_QUERYATTR_RECURSING
;
3783 else if (result
== DNS_R_DUPLICATE
||
3784 result
== DNS_R_DROP
)
3785 QUERY_ERROR(result
);
3787 QUERY_ERROR(DNS_R_SERVFAIL
);
3790 * This is the best answer.
3792 client
->query
.attributes
|=
3793 NS_QUERYATTR_CACHEGLUEOK
;
3794 client
->query
.gluedb
= zdb
;
3795 client
->query
.isreferral
= ISC_TRUE
;
3797 * We must ensure NOADDITIONAL is off,
3798 * because the generation of
3799 * additional data is required in
3802 client
->query
.attributes
&=
3803 ~NS_QUERYATTR_NOADDITIONAL
;
3804 if (sigrdataset
!= NULL
)
3805 sigrdatasetp
= &sigrdataset
;
3807 sigrdatasetp
= NULL
;
3808 query_addrrset(client
, &fname
,
3809 &rdataset
, sigrdatasetp
,
3810 dbuf
, DNS_SECTION_AUTHORITY
);
3811 client
->query
.gluedb
= NULL
;
3812 client
->query
.attributes
&=
3813 ~NS_QUERYATTR_CACHEGLUEOK
;
3814 if (WANTDNSSEC(client
))
3815 query_addds(client
, db
, node
, version
);
3819 case DNS_R_EMPTYNAME
:
3820 result
= DNS_R_NXRRSET
;
3824 if (dns_rdataset_isassociated(rdataset
)) {
3826 * If we've got a NSEC record, we need to save the
3827 * name now because we're going call query_addsoa()
3828 * below, and it needs to use the name buffer.
3830 query_keepname(client
, fname
, dbuf
);
3833 * We're not going to use fname, and need to release
3834 * our hold on the name buffer so query_addsoa()
3837 query_releasename(client
, &fname
);
3842 result
= query_addsoa(client
, db
, version
, ISC_FALSE
);
3843 if (result
!= ISC_R_SUCCESS
) {
3844 QUERY_ERROR(result
);
3848 * Add NSEC record if we found one.
3850 if (WANTDNSSEC(client
)) {
3851 if (dns_rdataset_isassociated(rdataset
))
3852 query_addnxrrsetnsec(client
, db
, version
,
3857 case DNS_R_EMPTYWILD
:
3858 empty_wild
= ISC_TRUE
;
3860 case DNS_R_NXDOMAIN
:
3862 if (dns_rdataset_isassociated(rdataset
)) {
3864 * If we've got a NSEC record, we need to save the
3865 * name now because we're going call query_addsoa()
3866 * below, and it needs to use the name buffer.
3868 query_keepname(client
, fname
, dbuf
);
3871 * We're not going to use fname, and need to release
3872 * our hold on the name buffer so query_addsoa()
3875 query_releasename(client
, &fname
);
3878 * Add SOA. If the query was for a SOA record force the
3879 * ttl to zero so that it is possible for clients to find
3880 * the containing zone of an arbitrary name with a stub
3881 * resolver and not have it cached.
3883 if (qtype
== dns_rdatatype_soa
&&
3887 dns_zone_getzeronosoattl(zone
))
3888 result
= query_addsoa(client
, db
, version
, ISC_TRUE
);
3890 result
= query_addsoa(client
, db
, version
, ISC_FALSE
);
3891 if (result
!= ISC_R_SUCCESS
) {
3892 QUERY_ERROR(result
);
3896 * Add NSEC record if we found one.
3898 if (dns_rdataset_isassociated(rdataset
)) {
3899 if (WANTDNSSEC(client
)) {
3900 query_addrrset(client
, &fname
, &rdataset
,
3902 NULL
, DNS_SECTION_AUTHORITY
);
3903 query_addwildcardproof(client
, db
, version
,
3904 client
->query
.qname
,
3909 * Set message rcode.
3912 client
->message
->rcode
= dns_rcode_noerror
;
3914 client
->message
->rcode
= dns_rcode_nxdomain
;
3916 case DNS_R_NCACHENXDOMAIN
:
3917 case DNS_R_NCACHENXRRSET
:
3919 authoritative
= ISC_FALSE
;
3921 * Set message rcode, if required.
3923 if (result
== DNS_R_NCACHENXDOMAIN
)
3924 client
->message
->rcode
= dns_rcode_nxdomain
;
3926 * Look for RFC 1918 leakage from Internet.
3928 if (result
== DNS_R_NCACHENXDOMAIN
&&
3929 qtype
== dns_rdatatype_ptr
&&
3930 client
->message
->rdclass
== dns_rdataclass_in
&&
3931 dns_name_countlabels(fname
) == 7)
3932 warn_rfc1918(client
, fname
, rdataset
);
3934 * We don't call query_addrrset() because we don't need any
3935 * of its extra features (and things would probably break!).
3937 query_keepname(client
, fname
, dbuf
);
3938 dns_message_addname(client
->message
, fname
,
3939 DNS_SECTION_AUTHORITY
);
3940 ISC_LIST_APPEND(fname
->list
, rdataset
, link
);
3946 * Keep a copy of the rdataset. We have to do this because
3947 * query_addrrset may clear 'rdataset' (to prevent the
3948 * cleanup code from cleaning it up).
3950 trdataset
= rdataset
;
3952 * Add the CNAME to the answer section.
3954 if (sigrdataset
!= NULL
)
3955 sigrdatasetp
= &sigrdataset
;
3957 sigrdatasetp
= NULL
;
3958 if (WANTDNSSEC(client
) &&
3959 (fname
->attributes
& DNS_NAMEATTR_WILDCARD
) != 0)
3961 dns_fixedname_init(&wildcardname
);
3962 dns_name_copy(fname
, dns_fixedname_name(&wildcardname
),
3964 need_wildcardproof
= ISC_TRUE
;
3966 if ((rdataset
->attributes
& DNS_RDATASETATTR_NOQNAME
) != 0 &&
3971 query_addrrset(client
, &fname
, &rdataset
, sigrdatasetp
, dbuf
,
3972 DNS_SECTION_ANSWER
);
3973 if (noqname
!= NULL
)
3974 query_addnoqnameproof(client
, noqname
);
3976 * We set the PARTIALANSWER attribute so that if anything goes
3977 * wrong later on, we'll return what we've got so far.
3979 client
->query
.attributes
|= NS_QUERYATTR_PARTIALANSWER
;
3981 * Reset qname to be the target name of the CNAME and restart
3985 result
= dns_message_gettempname(client
->message
, &tname
);
3986 if (result
!= ISC_R_SUCCESS
)
3988 result
= dns_rdataset_first(trdataset
);
3989 if (result
!= ISC_R_SUCCESS
) {
3990 dns_message_puttempname(client
->message
, &tname
);
3993 dns_rdataset_current(trdataset
, &rdata
);
3994 result
= dns_rdata_tostruct(&rdata
, &cname
, NULL
);
3995 dns_rdata_reset(&rdata
);
3996 if (result
!= ISC_R_SUCCESS
) {
3997 dns_message_puttempname(client
->message
, &tname
);
4000 dns_name_init(tname
, NULL
);
4001 result
= dns_name_dup(&cname
.cname
, client
->mctx
, tname
);
4002 if (result
!= ISC_R_SUCCESS
) {
4003 dns_message_puttempname(client
->message
, &tname
);
4004 dns_rdata_freestruct(&cname
);
4007 dns_rdata_freestruct(&cname
);
4008 ns_client_qnamereplace(client
, tname
);
4009 want_restart
= ISC_TRUE
;
4010 if (!WANTRECURSION(client
))
4011 options
|= DNS_GETDB_NOLOG
;
4015 * Compare the current qname to the found name. We need
4016 * to know how many labels and bits are in common because
4017 * we're going to have to split qname later on.
4019 namereln
= dns_name_fullcompare(client
->query
.qname
, fname
,
4021 INSIST(namereln
== dns_namereln_subdomain
);
4023 * Keep a copy of the rdataset. We have to do this because
4024 * query_addrrset may clear 'rdataset' (to prevent the
4025 * cleanup code from cleaning it up).
4027 trdataset
= rdataset
;
4029 * Add the DNAME to the answer section.
4031 if (sigrdataset
!= NULL
)
4032 sigrdatasetp
= &sigrdataset
;
4034 sigrdatasetp
= NULL
;
4035 if (WANTDNSSEC(client
) &&
4036 (fname
->attributes
& DNS_NAMEATTR_WILDCARD
) != 0)
4038 dns_fixedname_init(&wildcardname
);
4039 dns_name_copy(fname
, dns_fixedname_name(&wildcardname
),
4041 need_wildcardproof
= ISC_TRUE
;
4043 query_addrrset(client
, &fname
, &rdataset
, sigrdatasetp
, dbuf
,
4044 DNS_SECTION_ANSWER
);
4046 * We set the PARTIALANSWER attribute so that if anything goes
4047 * wrong later on, we'll return what we've got so far.
4049 client
->query
.attributes
|= NS_QUERYATTR_PARTIALANSWER
;
4051 * Get the target name of the DNAME.
4054 result
= dns_message_gettempname(client
->message
, &tname
);
4055 if (result
!= ISC_R_SUCCESS
)
4057 result
= dns_rdataset_first(trdataset
);
4058 if (result
!= ISC_R_SUCCESS
) {
4059 dns_message_puttempname(client
->message
, &tname
);
4062 dns_rdataset_current(trdataset
, &rdata
);
4063 result
= dns_rdata_tostruct(&rdata
, &dname
, NULL
);
4064 dns_rdata_reset(&rdata
);
4065 if (result
!= ISC_R_SUCCESS
) {
4066 dns_message_puttempname(client
->message
, &tname
);
4069 dns_name_init(tname
, NULL
);
4070 dns_name_clone(&dname
.dname
, tname
);
4071 dns_rdata_freestruct(&dname
);
4073 * Construct the new qname.
4075 dns_fixedname_init(&fixed
);
4076 prefix
= dns_fixedname_name(&fixed
);
4077 dns_name_split(client
->query
.qname
, nlabels
, prefix
, NULL
);
4078 INSIST(fname
== NULL
);
4079 dbuf
= query_getnamebuf(client
);
4081 dns_message_puttempname(client
->message
, &tname
);
4084 fname
= query_newname(client
, dbuf
, &b
);
4085 if (fname
== NULL
) {
4086 dns_message_puttempname(client
->message
, &tname
);
4089 result
= dns_name_concatenate(prefix
, tname
, fname
, NULL
);
4090 if (result
!= ISC_R_SUCCESS
) {
4091 dns_message_puttempname(client
->message
, &tname
);
4092 if (result
== ISC_R_NOSPACE
) {
4094 * RFC2672, section 4.1, subsection 3c says
4095 * we should return YXDOMAIN if the constructed
4096 * name would be too long.
4098 client
->message
->rcode
= dns_rcode_yxdomain
;
4102 query_keepname(client
, fname
, dbuf
);
4104 * Synthesize a CNAME for this DNAME.
4106 * We want to synthesize a CNAME since if we don't
4107 * then older software that doesn't understand DNAME
4108 * will not chain like it should.
4110 * We do not try to synthesize a signature because we hope
4111 * that security aware servers will understand DNAME. Also,
4112 * even if we had an online key, making a signature
4113 * on-the-fly is costly, and not really legitimate anyway
4114 * since the synthesized CNAME is NOT in the zone.
4116 dns_name_init(tname
, NULL
);
4117 (void)query_addcnamelike(client
, client
->query
.qname
, fname
,
4118 trdataset
->trust
, &tname
,
4119 dns_rdatatype_cname
);
4121 dns_message_puttempname(client
->message
, &tname
);
4123 * Switch to the new qname and restart.
4125 ns_client_qnamereplace(client
, fname
);
4127 want_restart
= ISC_TRUE
;
4128 if (!WANTRECURSION(client
))
4129 options
|= DNS_GETDB_NOLOG
;
4133 * Something has gone wrong.
4135 QUERY_ERROR(DNS_R_SERVFAIL
);
4139 if (WANTDNSSEC(client
) &&
4140 (fname
->attributes
& DNS_NAMEATTR_WILDCARD
) != 0)
4142 dns_fixedname_init(&wildcardname
);
4143 dns_name_copy(fname
, dns_fixedname_name(&wildcardname
), NULL
);
4144 need_wildcardproof
= ISC_TRUE
;
4147 if (type
== dns_rdatatype_any
) {
4149 * XXXRTH Need to handle zonecuts with special case
4154 result
= dns_db_allrdatasets(db
, node
, version
, 0, &rdsiter
);
4155 if (result
!= ISC_R_SUCCESS
) {
4156 QUERY_ERROR(DNS_R_SERVFAIL
);
4160 * Calling query_addrrset() with a non-NULL dbuf is going
4161 * to either keep or release the name. We don't want it to
4162 * release fname, since we may have to call query_addrrset()
4163 * more than once. That means we have to call query_keepname()
4164 * now, and pass a NULL dbuf to query_addrrset().
4166 * If we do a query_addrrset() below, we must set fname to
4167 * NULL before leaving this block, otherwise we might try to
4168 * cleanup fname even though we're using it!
4170 query_keepname(client
, fname
, dbuf
);
4172 result
= dns_rdatasetiter_first(rdsiter
);
4173 while (result
== ISC_R_SUCCESS
) {
4174 dns_rdatasetiter_current(rdsiter
, rdataset
);
4175 if ((qtype
== dns_rdatatype_any
||
4176 rdataset
->type
== qtype
) && rdataset
->type
!= 0) {
4177 query_addrrset(client
,
4178 fname
!= NULL
? &fname
: &tname
,
4180 NULL
, DNS_SECTION_ANSWER
);
4182 INSIST(tname
!= NULL
);
4184 * rdataset is non-NULL only in certain pathological
4185 * cases involving DNAMEs.
4187 if (rdataset
!= NULL
)
4188 query_putrdataset(client
, &rdataset
);
4189 rdataset
= query_newrdataset(client
);
4190 if (rdataset
== NULL
)
4194 * We're not interested in this rdataset.
4196 dns_rdataset_disassociate(rdataset
);
4198 result
= dns_rdatasetiter_next(rdsiter
);
4202 dns_message_puttempname(client
->message
, &fname
);
4206 * We didn't match any rdatasets.
4208 if (qtype
== dns_rdatatype_rrsig
&&
4209 result
== ISC_R_NOMORE
) {
4211 * XXXRTH If this is a secure zone and we
4212 * didn't find any SIGs, we should generate
4213 * an error unless we were searching for
4217 authoritative
= ISC_FALSE
;
4218 dns_rdatasetiter_destroy(&rdsiter
);
4219 if (RECURSIONOK(client
)) {
4220 result
= query_recurse(client
,
4224 if (result
== ISC_R_SUCCESS
)
4225 client
->query
.attributes
|=
4226 NS_QUERYATTR_RECURSING
;
4228 QUERY_ERROR(DNS_R_SERVFAIL
); }
4232 * We were searching for SIG records in
4233 * a nonsecure zone. Send a "no error,
4234 * no data" response.
4239 result
= query_addsoa(client
, db
, version
,
4241 if (result
== ISC_R_SUCCESS
)
4242 result
= ISC_R_NOMORE
;
4245 * Something went wrong.
4247 result
= DNS_R_SERVFAIL
;
4250 dns_rdatasetiter_destroy(&rdsiter
);
4251 if (result
!= ISC_R_NOMORE
) {
4252 QUERY_ERROR(DNS_R_SERVFAIL
);
4257 * This is the "normal" case -- an ordinary question to which
4258 * we know the answer.
4260 if (sigrdataset
!= NULL
)
4261 sigrdatasetp
= &sigrdataset
;
4263 sigrdatasetp
= NULL
;
4264 if ((rdataset
->attributes
& DNS_RDATASETATTR_NOQNAME
) != 0 &&
4270 * BIND 8 priming queries need the additional section.
4272 if (is_zone
&& qtype
== dns_rdatatype_ns
&&
4273 dns_name_equal(client
->query
.qname
, dns_rootname
))
4274 client
->query
.attributes
&= ~NS_QUERYATTR_NOADDITIONAL
;
4276 query_addrrset(client
, &fname
, &rdataset
, sigrdatasetp
, dbuf
,
4277 DNS_SECTION_ANSWER
);
4278 if (noqname
!= NULL
)
4279 query_addnoqnameproof(client
, noqname
);
4281 * We shouldn't ever fail to add 'rdataset'
4282 * because it's already in the answer.
4284 INSIST(rdataset
== NULL
);
4288 CTRACE("query_find: addauth");
4290 * Add NS records to the authority section (if we haven't already
4291 * added them to the answer section).
4293 if (!want_restart
&& !NOAUTHORITY(client
)) {
4295 if (!((qtype
== dns_rdatatype_ns
||
4296 qtype
== dns_rdatatype_any
) &&
4297 dns_name_equal(client
->query
.qname
,
4298 dns_db_origin(db
))))
4299 (void)query_addns(client
, db
, version
);
4300 } else if (qtype
!= dns_rdatatype_ns
) {
4302 query_releasename(client
, &fname
);
4303 query_addbestns(client
);
4308 * Add NSEC records to the authority section if they're needed for
4309 * DNSSEC wildcard proofs.
4311 if (need_wildcardproof
&& dns_db_issecure(db
))
4312 query_addwildcardproof(client
, db
, version
,
4313 dns_fixedname_name(&wildcardname
),
4316 CTRACE("query_find: cleanup");
4320 if (rdataset
!= NULL
)
4321 query_putrdataset(client
, &rdataset
);
4322 if (sigrdataset
!= NULL
)
4323 query_putrdataset(client
, &sigrdataset
);
4325 query_releasename(client
, &fname
);
4327 dns_db_detachnode(db
, &node
);
4331 dns_zone_detach(&zone
);
4333 query_putrdataset(client
, &zrdataset
);
4334 if (zsigrdataset
!= NULL
)
4335 query_putrdataset(client
, &zsigrdataset
);
4337 query_releasename(client
, &zfname
);
4338 dns_db_detach(&zdb
);
4341 isc_event_free(ISC_EVENT_PTR(&event
));
4346 if (client
->query
.restarts
== 0 && !authoritative
) {
4348 * We're not authoritative, so we must ensure the AA bit
4351 client
->message
->flags
&= ~DNS_MESSAGEFLAG_AA
;
4355 * Restart the query?
4357 if (want_restart
&& client
->query
.restarts
< MAX_RESTARTS
) {
4358 client
->query
.restarts
++;
4362 if (eresult
!= ISC_R_SUCCESS
&&
4363 (!PARTIALANSWER(client
) || WANTRECURSION(client
))) {
4364 if (eresult
== DNS_R_DUPLICATE
|| eresult
== DNS_R_DROP
) {
4366 * This was a duplicate query that we are
4367 * recursing on. Don't send a response now.
4368 * The original query will still cause a response.
4370 query_next(client
, eresult
);
4373 * If we don't have any answer to give the client,
4374 * or if the client requested recursion and thus wanted
4375 * the complete answer, send an error response.
4377 query_error(client
, eresult
);
4379 ns_client_detach(&client
);
4380 } else if (!RECURSING(client
)) {
4382 * We are done. Set up sortlist data for the message
4383 * rendering code, make a final tweak to the AA bit if the
4384 * auth-nxdomain config option says so, then render and
4385 * send the response.
4387 setup_query_sortlist(client
);
4390 * If this is a referral and the answer to the question
4391 * is in the glue sort it to the start of the additional
4394 if (client
->message
->counts
[DNS_SECTION_ANSWER
] == 0 &&
4395 client
->message
->rcode
== dns_rcode_noerror
&&
4396 (qtype
== dns_rdatatype_a
|| qtype
== dns_rdatatype_aaaa
))
4397 answer_in_glue(client
, qtype
);
4399 if (client
->message
->rcode
== dns_rcode_nxdomain
&&
4400 client
->view
->auth_nxdomain
== ISC_TRUE
)
4401 client
->message
->flags
|= DNS_MESSAGEFLAG_AA
;
4404 ns_client_detach(&client
);
4406 CTRACE("query_find: done");
4410 log_query(ns_client_t
*client
) {
4411 char namebuf
[DNS_NAME_FORMATSIZE
];
4412 char typename
[DNS_RDATATYPE_FORMATSIZE
];
4413 char classname
[DNS_RDATACLASS_FORMATSIZE
];
4414 dns_rdataset_t
*rdataset
;
4415 int level
= ISC_LOG_INFO
;
4417 if (! isc_log_wouldlog(ns_g_lctx
, level
))
4420 rdataset
= ISC_LIST_HEAD(client
->query
.qname
->list
);
4421 INSIST(rdataset
!= NULL
);
4422 dns_name_format(client
->query
.qname
, namebuf
, sizeof(namebuf
));
4423 dns_rdataclass_format(rdataset
->rdclass
, classname
, sizeof(classname
));
4424 dns_rdatatype_format(rdataset
->type
, typename
, sizeof(typename
));
4426 ns_client_log(client
, NS_LOGCATEGORY_QUERIES
, NS_LOGMODULE_QUERY
,
4427 level
, "query: %s %s %s %s%s%s", namebuf
, classname
,
4428 typename
, WANTRECURSION(client
) ? "+" : "-",
4429 (client
->signer
!= NULL
) ? "S": "",
4430 (client
->opt
!= NULL
) ? "E" : "");
4434 ns_query_start(ns_client_t
*client
) {
4435 isc_result_t result
;
4436 dns_message_t
*message
= client
->message
;
4437 dns_rdataset_t
*rdataset
;
4438 ns_client_t
*qclient
;
4439 dns_rdatatype_t qtype
;
4441 CTRACE("ns_query_start");
4444 * Ensure that appropriate cleanups occur.
4446 client
->next
= query_next_callback
;
4449 * Behave as if we don't support DNSSEC if not enabled.
4451 if (!client
->view
->enablednssec
) {
4452 message
->flags
&= ~DNS_MESSAGEFLAG_CD
;
4453 client
->extflags
&= ~DNS_MESSAGEEXTFLAG_DO
;
4454 if (client
->opt
!= NULL
)
4455 client
->opt
->ttl
&= ~DNS_MESSAGEEXTFLAG_DO
;
4458 if ((message
->flags
& DNS_MESSAGEFLAG_RD
) != 0)
4459 client
->query
.attributes
|= NS_QUERYATTR_WANTRECURSION
;
4461 if ((client
->extflags
& DNS_MESSAGEEXTFLAG_DO
) != 0)
4462 client
->attributes
|= NS_CLIENTATTR_WANTDNSSEC
;
4464 if (client
->view
->minimalresponses
)
4465 client
->query
.attributes
|= (NS_QUERYATTR_NOAUTHORITY
|
4466 NS_QUERYATTR_NOADDITIONAL
);
4468 if ((client
->view
->cachedb
== NULL
)
4469 || (!client
->view
->additionalfromcache
)) {
4471 * We don't have a cache. Turn off cache support and
4474 client
->query
.attributes
&=
4475 ~(NS_QUERYATTR_RECURSIONOK
|NS_QUERYATTR_CACHEOK
);
4476 } else if ((client
->attributes
& NS_CLIENTATTR_RA
) == 0 ||
4477 (message
->flags
& DNS_MESSAGEFLAG_RD
) == 0) {
4479 * If the client isn't allowed to recurse (due to
4480 * "recursion no", the allow-recursion ACL, or the
4481 * lack of a resolver in this view), or if it
4482 * doesn't want recursion, turn recursion off.
4484 client
->query
.attributes
&= ~NS_QUERYATTR_RECURSIONOK
;
4488 * Get the question name.
4490 result
= dns_message_firstname(message
, DNS_SECTION_QUESTION
);
4491 if (result
!= ISC_R_SUCCESS
) {
4492 query_error(client
, result
);
4495 dns_message_currentname(message
, DNS_SECTION_QUESTION
,
4496 &client
->query
.qname
);
4497 client
->query
.origqname
= client
->query
.qname
;
4498 result
= dns_message_nextname(message
, DNS_SECTION_QUESTION
);
4499 if (result
!= ISC_R_NOMORE
) {
4500 if (result
== ISC_R_SUCCESS
) {
4502 * There's more than one QNAME in the question
4505 query_error(client
, DNS_R_FORMERR
);
4507 query_error(client
, result
);
4511 if (ns_g_server
->log_queries
)
4515 * Check for multiple question queries, since edns1 is dead.
4517 if (message
->counts
[DNS_SECTION_QUESTION
] > 1) {
4518 query_error(client
, DNS_R_FORMERR
);
4523 * Check for meta-queries like IXFR and AXFR.
4525 rdataset
= ISC_LIST_HEAD(client
->query
.qname
->list
);
4526 INSIST(rdataset
!= NULL
);
4527 qtype
= rdataset
->type
;
4528 if (dns_rdatatype_ismeta(qtype
)) {
4530 case dns_rdatatype_any
:
4531 break; /* Let query_find handle it. */
4532 case dns_rdatatype_ixfr
:
4533 case dns_rdatatype_axfr
:
4534 ns_xfr_start(client
, rdataset
->type
);
4536 case dns_rdatatype_maila
:
4537 case dns_rdatatype_mailb
:
4538 query_error(client
, DNS_R_NOTIMP
);
4540 case dns_rdatatype_tkey
:
4541 result
= dns_tkey_processquery(client
->message
,
4542 ns_g_server
->tkeyctx
,
4543 client
->view
->dynamickeys
);
4544 if (result
== ISC_R_SUCCESS
)
4547 query_error(client
, result
);
4549 default: /* TSIG, etc. */
4550 query_error(client
, DNS_R_FORMERR
);
4556 * If the client has requested that DNSSEC checking be disabled,
4557 * allow lookups to return pending data and instruct the resolver
4558 * to return data before validation has completed.
4560 * We don't need to set DNS_DBFIND_PENDINGOK when validation is
4561 * disabled as there will be no pending data.
4563 if (message
->flags
& DNS_MESSAGEFLAG_CD
||
4564 qtype
== dns_rdatatype_rrsig
)
4566 client
->query
.dboptions
|= DNS_DBFIND_PENDINGOK
;
4567 client
->query
.fetchoptions
|= DNS_FETCHOPT_NOVALIDATE
;
4568 } else if (!client
->view
->enablevalidation
)
4569 client
->query
.fetchoptions
|= DNS_FETCHOPT_NOVALIDATE
;
4572 * Allow glue NS records to be added to the authority section
4573 * if the answer is secure.
4575 if (message
->flags
& DNS_MESSAGEFLAG_CD
)
4576 client
->query
.attributes
&= ~NS_QUERYATTR_SECURE
;
4579 * This is an ordinary query.
4581 result
= dns_message_reply(message
, ISC_TRUE
);
4582 if (result
!= ISC_R_SUCCESS
) {
4583 query_next(client
, result
);
4588 * Assume authoritative response until it is known to be
4591 message
->flags
|= DNS_MESSAGEFLAG_AA
;
4594 * Set AD. We must clear it if we add non-validated data to a
4597 if (WANTDNSSEC(client
))
4598 message
->flags
|= DNS_MESSAGEFLAG_AD
;
4601 ns_client_attach(client
, &qclient
);
4602 query_find(qclient
, NULL
, qtype
);