2 * Copyright (C) 2004-2010 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.313.20.16.10.3 2010/09/29 00:03:32 marka Exp $ */
28 #include <isc/stats.h>
32 #include <dns/byaddr.h>
37 #include <dns/dnssec.h>
38 #include <dns/events.h>
39 #include <dns/message.h>
40 #include <dns/ncache.h>
41 #include <dns/nsec3.h>
42 #include <dns/order.h>
43 #include <dns/rdata.h>
44 #include <dns/rdataclass.h>
45 #include <dns/rdatalist.h>
46 #include <dns/rdataset.h>
47 #include <dns/rdatasetiter.h>
48 #include <dns/rdatastruct.h>
49 #include <dns/rdatatype.h>
50 #include <dns/resolver.h>
51 #include <dns/result.h>
52 #include <dns/stats.h>
58 #include <named/client.h>
59 #include <named/globals.h>
60 #include <named/log.h>
61 #include <named/server.h>
62 #include <named/sortlist.h>
63 #include <named/xfrout.h>
65 /*% Partial answer? */
66 #define PARTIALANSWER(c) (((c)->query.attributes & \
67 NS_QUERYATTR_PARTIALANSWER) != 0)
69 #define USECACHE(c) (((c)->query.attributes & \
70 NS_QUERYATTR_CACHEOK) != 0)
72 #define RECURSIONOK(c) (((c)->query.attributes & \
73 NS_QUERYATTR_RECURSIONOK) != 0)
75 #define RECURSING(c) (((c)->query.attributes & \
76 NS_QUERYATTR_RECURSING) != 0)
78 #define CACHEGLUEOK(c) (((c)->query.attributes & \
79 NS_QUERYATTR_CACHEGLUEOK) != 0)
80 /*% Want Recursion? */
81 #define WANTRECURSION(c) (((c)->query.attributes & \
82 NS_QUERYATTR_WANTRECURSION) != 0)
84 #define WANTDNSSEC(c) (((c)->attributes & \
85 NS_CLIENTATTR_WANTDNSSEC) != 0)
87 #define NOAUTHORITY(c) (((c)->query.attributes & \
88 NS_QUERYATTR_NOAUTHORITY) != 0)
90 #define NOADDITIONAL(c) (((c)->query.attributes & \
91 NS_QUERYATTR_NOADDITIONAL) != 0)
93 #define SECURE(c) (((c)->query.attributes & \
94 NS_QUERYATTR_SECURE) != 0)
96 /*% No QNAME Proof? */
97 #define NOQNAME(r) (((r)->attributes & \
98 DNS_RDATASETATTR_NOQNAME) != 0)
101 #define CTRACE(m) isc_log_write(ns_g_lctx, \
102 NS_LOGCATEGORY_CLIENT, \
103 NS_LOGMODULE_QUERY, \
105 "client %p: %s", client, (m))
106 #define QTRACE(m) isc_log_write(ns_g_lctx, \
107 NS_LOGCATEGORY_GENERAL, \
108 NS_LOGMODULE_QUERY, \
110 "query %p: %s", query, (m))
112 #define CTRACE(m) ((void)m)
113 #define QTRACE(m) ((void)m)
116 #define DNS_GETDB_NOEXACT 0x01U
117 #define DNS_GETDB_NOLOG 0x02U
118 #define DNS_GETDB_PARTIAL 0x04U
120 #define PENDINGOK(x) (((x) & DNS_DBFIND_PENDINGOK) != 0)
122 typedef struct client_additionalctx {
124 dns_rdataset_t *rdataset;
125 } client_additionalctx_t;
128 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype);
131 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
132 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
135 query_findclosestnsec3(dns_name_t *qname, dns_db_t *db,
136 dns_dbversion_t *version, ns_client_t *client,
137 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
138 dns_name_t *fname, isc_boolean_t exact,
142 log_queryerror(ns_client_t *client, isc_result_t result, int line, int level);
145 * Increment query statistics counters.
148 inc_stats(ns_client_t *client, isc_statscounter_t counter) {
149 dns_zone_t *zone = client->query.authzone;
151 isc_stats_increment(ns_g_server->nsstats, counter);
154 isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
155 if (zonestats != NULL)
156 isc_stats_increment(zonestats, counter);
161 query_send(ns_client_t *client) {
162 isc_statscounter_t counter;
163 if ((client->message->flags & DNS_MESSAGEFLAG_AA) == 0)
164 inc_stats(client, dns_nsstatscounter_nonauthans);
166 inc_stats(client, dns_nsstatscounter_authans);
167 if (client->message->rcode == dns_rcode_noerror) {
168 if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER])) {
169 if (client->query.isreferral) {
170 counter = dns_nsstatscounter_referral;
172 counter = dns_nsstatscounter_nxrrset;
175 counter = dns_nsstatscounter_success;
177 } else if (client->message->rcode == dns_rcode_nxdomain) {
178 counter = dns_nsstatscounter_nxdomain;
180 /* We end up here in case of YXDOMAIN, and maybe others */
181 counter = dns_nsstatscounter_failure;
183 inc_stats(client, counter);
184 ns_client_send(client);
188 query_error(ns_client_t *client, isc_result_t result, int line) {
189 int loglevel = ISC_LOG_DEBUG(3);
193 loglevel = ISC_LOG_DEBUG(1);
194 inc_stats(client, dns_nsstatscounter_servfail);
197 inc_stats(client, dns_nsstatscounter_formerr);
200 inc_stats(client, dns_nsstatscounter_failure);
204 log_queryerror(client, result, line, loglevel);
206 ns_client_error(client, result);
210 query_next(ns_client_t *client, isc_result_t result) {
211 if (result == DNS_R_DUPLICATE)
212 inc_stats(client, dns_nsstatscounter_duplicate);
213 else if (result == DNS_R_DROP)
214 inc_stats(client, dns_nsstatscounter_dropped);
216 inc_stats(client, dns_nsstatscounter_failure);
217 ns_client_next(client, result);
221 query_freefreeversions(ns_client_t *client, isc_boolean_t everything) {
222 ns_dbversion_t *dbversion, *dbversion_next;
225 for (dbversion = ISC_LIST_HEAD(client->query.freeversions), i = 0;
227 dbversion = dbversion_next, i++)
229 dbversion_next = ISC_LIST_NEXT(dbversion, link);
231 * If we're not freeing everything, we keep the first three
232 * dbversions structures around.
234 if (i > 3 || everything) {
235 ISC_LIST_UNLINK(client->query.freeversions, dbversion,
237 isc_mem_put(client->mctx, dbversion,
244 ns_query_cancel(ns_client_t *client) {
245 LOCK(&client->query.fetchlock);
246 if (client->query.fetch != NULL) {
247 dns_resolver_cancelfetch(client->query.fetch);
249 client->query.fetch = NULL;
251 UNLOCK(&client->query.fetchlock);
255 query_reset(ns_client_t *client, isc_boolean_t everything) {
256 isc_buffer_t *dbuf, *dbuf_next;
257 ns_dbversion_t *dbversion, *dbversion_next;
260 * Reset the query state of a client to its default state.
264 * Cancel the fetch if it's running.
266 ns_query_cancel(client);
269 * Cleanup any active versions.
271 for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
273 dbversion = dbversion_next) {
274 dbversion_next = ISC_LIST_NEXT(dbversion, link);
275 dns_db_closeversion(dbversion->db, &dbversion->version,
277 dns_db_detach(&dbversion->db);
278 ISC_LIST_INITANDAPPEND(client->query.freeversions,
281 ISC_LIST_INIT(client->query.activeversions);
283 if (client->query.authdb != NULL)
284 dns_db_detach(&client->query.authdb);
285 if (client->query.authzone != NULL)
286 dns_zone_detach(&client->query.authzone);
288 query_freefreeversions(client, everything);
290 for (dbuf = ISC_LIST_HEAD(client->query.namebufs);
293 dbuf_next = ISC_LIST_NEXT(dbuf, link);
294 if (dbuf_next != NULL || everything) {
295 ISC_LIST_UNLINK(client->query.namebufs, dbuf, link);
296 isc_buffer_free(&dbuf);
300 if (client->query.restarts > 0) {
302 * client->query.qname was dynamically allocated.
304 dns_message_puttempname(client->message,
305 &client->query.qname);
307 client->query.qname = NULL;
308 client->query.attributes = (NS_QUERYATTR_RECURSIONOK |
309 NS_QUERYATTR_CACHEOK |
310 NS_QUERYATTR_SECURE);
311 client->query.restarts = 0;
312 client->query.timerset = ISC_FALSE;
313 client->query.origqname = NULL;
314 client->query.qname = NULL;
315 client->query.dboptions = 0;
316 client->query.fetchoptions = 0;
317 client->query.gluedb = NULL;
318 client->query.authdbset = ISC_FALSE;
319 client->query.isreferral = ISC_FALSE;
323 query_next_callback(ns_client_t *client) {
324 query_reset(client, ISC_FALSE);
328 ns_query_free(ns_client_t *client) {
329 query_reset(client, ISC_TRUE);
332 static inline isc_result_t
333 query_newnamebuf(ns_client_t *client) {
337 CTRACE("query_newnamebuf");
339 * Allocate a name buffer.
343 result = isc_buffer_allocate(client->mctx, &dbuf, 1024);
344 if (result != ISC_R_SUCCESS) {
345 CTRACE("query_newnamebuf: isc_buffer_allocate failed: done");
348 ISC_LIST_APPEND(client->query.namebufs, dbuf, link);
350 CTRACE("query_newnamebuf: done");
351 return (ISC_R_SUCCESS);
354 static inline isc_buffer_t *
355 query_getnamebuf(ns_client_t *client) {
360 CTRACE("query_getnamebuf");
362 * Return a name buffer with space for a maximal name, allocating
363 * a new one if necessary.
366 if (ISC_LIST_EMPTY(client->query.namebufs)) {
367 result = query_newnamebuf(client);
368 if (result != ISC_R_SUCCESS) {
369 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
374 dbuf = ISC_LIST_TAIL(client->query.namebufs);
375 INSIST(dbuf != NULL);
376 isc_buffer_availableregion(dbuf, &r);
377 if (r.length < 255) {
378 result = query_newnamebuf(client);
379 if (result != ISC_R_SUCCESS) {
380 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
384 dbuf = ISC_LIST_TAIL(client->query.namebufs);
385 isc_buffer_availableregion(dbuf, &r);
386 INSIST(r.length >= 255);
388 CTRACE("query_getnamebuf: done");
393 query_keepname(ns_client_t *client, dns_name_t *name, isc_buffer_t *dbuf) {
396 CTRACE("query_keepname");
398 * 'name' is using space in 'dbuf', but 'dbuf' has not yet been
399 * adjusted to take account of that. We do the adjustment.
402 REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) != 0);
404 dns_name_toregion(name, &r);
405 isc_buffer_add(dbuf, r.length);
406 dns_name_setbuffer(name, NULL);
407 client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
411 query_releasename(ns_client_t *client, dns_name_t **namep) {
412 dns_name_t *name = *namep;
415 * 'name' is no longer needed. Return it to our pool of temporary
416 * names. If it is using a name buffer, relinquish its exclusive
417 * rights on the buffer.
420 CTRACE("query_releasename");
421 if (dns_name_hasbuffer(name)) {
422 INSIST((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED)
424 client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
426 dns_message_puttempname(client->message, namep);
427 CTRACE("query_releasename: done");
430 static inline dns_name_t *
431 query_newname(ns_client_t *client, isc_buffer_t *dbuf,
438 REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) == 0);
440 CTRACE("query_newname");
442 result = dns_message_gettempname(client->message, &name);
443 if (result != ISC_R_SUCCESS) {
444 CTRACE("query_newname: dns_message_gettempname failed: done");
447 isc_buffer_availableregion(dbuf, &r);
448 isc_buffer_init(nbuf, r.base, r.length);
449 dns_name_init(name, NULL);
450 dns_name_setbuffer(name, nbuf);
451 client->query.attributes |= NS_QUERYATTR_NAMEBUFUSED;
453 CTRACE("query_newname: done");
457 static inline dns_rdataset_t *
458 query_newrdataset(ns_client_t *client) {
459 dns_rdataset_t *rdataset;
462 CTRACE("query_newrdataset");
464 result = dns_message_gettemprdataset(client->message, &rdataset);
465 if (result != ISC_R_SUCCESS) {
466 CTRACE("query_newrdataset: "
467 "dns_message_gettemprdataset failed: done");
470 dns_rdataset_init(rdataset);
472 CTRACE("query_newrdataset: done");
477 query_putrdataset(ns_client_t *client, dns_rdataset_t **rdatasetp) {
478 dns_rdataset_t *rdataset = *rdatasetp;
480 CTRACE("query_putrdataset");
481 if (rdataset != NULL) {
482 if (dns_rdataset_isassociated(rdataset))
483 dns_rdataset_disassociate(rdataset);
484 dns_message_puttemprdataset(client->message, rdatasetp);
486 CTRACE("query_putrdataset: done");
490 static inline isc_result_t
491 query_newdbversion(ns_client_t *client, unsigned int n) {
493 ns_dbversion_t *dbversion;
495 for (i = 0; i < n; i++) {
496 dbversion = isc_mem_get(client->mctx, sizeof(*dbversion));
497 if (dbversion != NULL) {
498 dbversion->db = NULL;
499 dbversion->version = NULL;
500 ISC_LIST_INITANDAPPEND(client->query.freeversions,
504 * We only return ISC_R_NOMEMORY if we couldn't
508 return (ISC_R_NOMEMORY);
510 return (ISC_R_SUCCESS);
514 return (ISC_R_SUCCESS);
517 static inline ns_dbversion_t *
518 query_getdbversion(ns_client_t *client) {
520 ns_dbversion_t *dbversion;
522 if (ISC_LIST_EMPTY(client->query.freeversions)) {
523 result = query_newdbversion(client, 1);
524 if (result != ISC_R_SUCCESS)
527 dbversion = ISC_LIST_HEAD(client->query.freeversions);
528 INSIST(dbversion != NULL);
529 ISC_LIST_UNLINK(client->query.freeversions, dbversion, link);
535 ns_query_init(ns_client_t *client) {
538 ISC_LIST_INIT(client->query.namebufs);
539 ISC_LIST_INIT(client->query.activeversions);
540 ISC_LIST_INIT(client->query.freeversions);
541 client->query.restarts = 0;
542 client->query.timerset = ISC_FALSE;
543 client->query.qname = NULL;
544 result = isc_mutex_init(&client->query.fetchlock);
545 if (result != ISC_R_SUCCESS)
547 client->query.fetch = NULL;
548 client->query.authdb = NULL;
549 client->query.authzone = NULL;
550 client->query.authdbset = ISC_FALSE;
551 client->query.isreferral = ISC_FALSE;
552 query_reset(client, ISC_FALSE);
553 result = query_newdbversion(client, 3);
554 if (result != ISC_R_SUCCESS) {
555 DESTROYLOCK(&client->query.fetchlock);
558 result = query_newnamebuf(client);
559 if (result != ISC_R_SUCCESS)
560 query_freefreeversions(client, ISC_TRUE);
565 static inline ns_dbversion_t *
566 query_findversion(ns_client_t *client, dns_db_t *db,
567 isc_boolean_t *newzonep)
569 ns_dbversion_t *dbversion;
572 * We may already have done a query related to this
573 * database. If so, we must be sure to make subsequent
574 * queries from the same version.
576 for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
578 dbversion = ISC_LIST_NEXT(dbversion, link)) {
579 if (dbversion->db == db)
583 if (dbversion == NULL) {
585 * This is a new zone for this query. Add it to
588 dbversion = query_getdbversion(client);
589 if (dbversion == NULL)
591 dns_db_attach(db, &dbversion->db);
592 dns_db_currentversion(db, &dbversion->version);
593 dbversion->queryok = ISC_FALSE;
594 ISC_LIST_APPEND(client->query.activeversions,
596 *newzonep = ISC_TRUE;
598 *newzonep = ISC_FALSE;
603 static inline isc_result_t
604 query_validatezonedb(ns_client_t *client, dns_name_t *name,
605 dns_rdatatype_t qtype, unsigned int options,
606 dns_zone_t *zone, dns_db_t *db,
607 dns_dbversion_t **versionp)
610 isc_boolean_t check_acl, new_zone;
612 ns_dbversion_t *dbversion;
614 REQUIRE(zone != NULL);
618 * This limits our searching to the zone where the first name
619 * (the query target) was looked for. This prevents following
620 * CNAMES or DNAMES into other zones and prevents returning
621 * additional data from other zones.
623 if (!client->view->additionalfromauth &&
624 client->query.authdbset &&
625 db != client->query.authdb)
629 * If the zone has an ACL, we'll check it, otherwise
630 * we use the view's "allow-query" ACL. Each ACL is only checked
633 * Also, get the database version to use.
636 check_acl = ISC_TRUE; /* Keep compiler happy. */
640 * Get the current version of this database.
642 dbversion = query_findversion(client, db, &new_zone);
643 if (dbversion == NULL) {
644 result = DNS_R_SERVFAIL;
648 check_acl = ISC_TRUE;
649 } else if (!dbversion->queryok) {
652 check_acl = ISC_FALSE;
655 queryacl = dns_zone_getqueryacl(zone);
656 if (queryacl == NULL) {
657 queryacl = client->view->queryacl;
658 if ((client->query.attributes &
659 NS_QUERYATTR_QUERYOKVALID) != 0) {
661 * We've evaluated the view's queryacl already. If
662 * NS_QUERYATTR_QUERYOK is set, then the client is
663 * allowed to make queries, otherwise the query should
666 check_acl = ISC_FALSE;
667 if ((client->query.attributes &
668 NS_QUERYATTR_QUERYOK) == 0)
672 * We haven't evaluated the view's queryacl yet.
674 check_acl = ISC_TRUE;
679 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
681 result = ns_client_checkaclsilent(client, NULL, queryacl,
684 char msg[NS_CLIENT_ACLMSGSIZE("query")];
685 if (result == ISC_R_SUCCESS) {
686 if (isc_log_wouldlog(ns_g_lctx,
689 ns_client_aclmsg("query", name, qtype,
690 client->view->rdclass,
692 ns_client_log(client,
693 DNS_LOGCATEGORY_SECURITY,
699 ns_client_aclmsg("query", name, qtype,
700 client->view->rdclass,
702 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
703 NS_LOGMODULE_QUERY, ISC_LOG_INFO,
708 if (queryacl == client->view->queryacl) {
709 if (result == ISC_R_SUCCESS) {
711 * We were allowed by the default
712 * "allow-query" ACL. Remember this so we
713 * don't have to check again.
715 client->query.attributes |=
716 NS_QUERYATTR_QUERYOK;
719 * We've now evaluated the view's query ACL, and
720 * the NS_QUERYATTR_QUERYOK attribute is now valid.
722 client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
725 if (result != ISC_R_SUCCESS)
732 * Remember the result of the ACL check so we
733 * don't have to check again.
735 dbversion->queryok = ISC_TRUE;
737 /* Transfer ownership, if necessary. */
738 if (versionp != NULL)
739 *versionp = dbversion->version;
741 return (ISC_R_SUCCESS);
744 return (DNS_R_REFUSED);
750 static inline isc_result_t
751 query_getzonedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
752 unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
753 dns_dbversion_t **versionp)
756 unsigned int ztoptions;
757 dns_zone_t *zone = NULL;
759 isc_boolean_t partial = ISC_FALSE;
761 REQUIRE(zonep != NULL && *zonep == NULL);
762 REQUIRE(dbp != NULL && *dbp == NULL);
765 * Find a zone database to answer the query.
767 ztoptions = ((options & DNS_GETDB_NOEXACT) != 0) ?
768 DNS_ZTFIND_NOEXACT : 0;
770 result = dns_zt_find(client->view->zonetable, name, ztoptions, NULL,
772 if (result == DNS_R_PARTIALMATCH)
774 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
775 result = dns_zone_getdb(zone, &db);
777 if (result != ISC_R_SUCCESS)
780 result = query_validatezonedb(client, name, qtype, options, zone, db,
783 if (result != ISC_R_SUCCESS)
786 /* Transfer ownership. */
790 if (partial && (options & DNS_GETDB_PARTIAL) != 0)
791 return (DNS_R_PARTIALMATCH);
792 return (ISC_R_SUCCESS);
796 dns_zone_detach(&zone);
803 static inline isc_result_t
804 query_getcachedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
805 dns_db_t **dbp, unsigned int options)
808 isc_boolean_t check_acl;
811 REQUIRE(dbp != NULL && *dbp == NULL);
814 * Find a cache database to answer the query.
815 * This may fail with DNS_R_REFUSED if the client
816 * is not allowed to use the cache.
819 if (!USECACHE(client))
820 return (DNS_R_REFUSED);
821 dns_db_attach(client->view->cachedb, &db);
823 if ((client->query.attributes & NS_QUERYATTR_CACHEACLOKVALID) != 0) {
825 * We've evaluated the view's cacheacl already. If
826 * NS_QUERYATTR_CACHEACLOK is set, then the client is
827 * allowed to make queries, otherwise the query should
830 check_acl = ISC_FALSE;
831 if ((client->query.attributes & NS_QUERYATTR_CACHEACLOK) == 0)
835 * We haven't evaluated the view's queryacl yet.
837 check_acl = ISC_TRUE;
841 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
842 char msg[NS_CLIENT_ACLMSGSIZE("query (cache)")];
844 result = ns_client_checkaclsilent(client, NULL,
845 client->view->cacheacl,
847 if (result == ISC_R_SUCCESS) {
849 * We were allowed by the "allow-query-cache" ACL.
850 * Remember this so we don't have to check again.
852 client->query.attributes |=
853 NS_QUERYATTR_CACHEACLOK;
854 if (log && isc_log_wouldlog(ns_g_lctx,
857 ns_client_aclmsg("query (cache)", name, qtype,
858 client->view->rdclass,
860 ns_client_log(client,
861 DNS_LOGCATEGORY_SECURITY,
867 ns_client_aclmsg("query (cache)", name, qtype,
868 client->view->rdclass, msg,
870 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
871 NS_LOGMODULE_QUERY, ISC_LOG_INFO,
875 * We've now evaluated the view's query ACL, and
876 * the NS_QUERYATTR_CACHEACLOKVALID attribute is now valid.
878 client->query.attributes |= NS_QUERYATTR_CACHEACLOKVALID;
880 if (result != ISC_R_SUCCESS)
886 /* Transfer ownership. */
889 return (ISC_R_SUCCESS);
892 result = DNS_R_REFUSED;
901 static inline isc_result_t
902 query_getdb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
903 unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
904 dns_dbversion_t **versionp, isc_boolean_t *is_zonep)
909 isc_result_t tresult;
910 unsigned int namelabels;
911 unsigned int zonelabels;
912 dns_zone_t *zone = NULL;
915 REQUIRE(zonep != NULL && *zonep == NULL);
919 /* Calculate how many labels are in name. */
920 namelabels = dns_name_countlabels(name);
923 /* Try to find name in bind's standard database. */
924 result = query_getzonedb(client, name, qtype, options, &zone,
927 /* See how many labels are in the zone's name. */
928 if (result == ISC_R_SUCCESS && zone != NULL)
929 zonelabels = dns_name_countlabels(dns_zone_getorigin(zone));
931 * If # zone labels < # name labels, try to find an even better match
932 * Only try if a DLZ driver is loaded for this view
934 if (zonelabels < namelabels && client->view->dlzdatabase != NULL) {
935 tresult = dns_dlzfindzone(client->view, name,
937 /* If we successful, we found a better match. */
938 if (tresult == ISC_R_SUCCESS) {
940 * If the previous search returned a zone, detach it.
943 dns_zone_detach(&zone);
946 * If the previous search returned a database,
953 * If the previous search returned a version, clear it.
958 * Get our database version.
960 dns_db_currentversion(tdbp, versionp);
963 * Be sure to return our database.
968 * We return a null zone, No stats for DLZ zones.
975 result = query_getzonedb(client, name, qtype, options,
976 zonep, dbp, versionp);
979 /* If successful, Transfer ownership of zone. */
980 if (result == ISC_R_SUCCESS) {
985 * If neither attempt above succeeded, return the cache instead
987 *is_zonep = ISC_TRUE;
988 } else if (result == ISC_R_NOTFOUND) {
989 result = query_getcachedb(client, name, qtype, dbp, options);
990 *is_zonep = ISC_FALSE;
995 static inline isc_boolean_t
996 query_isduplicate(ns_client_t *client, dns_name_t *name,
997 dns_rdatatype_t type, dns_name_t **mnamep)
999 dns_section_t section;
1000 dns_name_t *mname = NULL;
1001 isc_result_t result;
1003 CTRACE("query_isduplicate");
1005 for (section = DNS_SECTION_ANSWER;
1006 section <= DNS_SECTION_ADDITIONAL;
1008 result = dns_message_findname(client->message, section,
1009 name, type, 0, &mname, NULL);
1010 if (result == ISC_R_SUCCESS) {
1012 * We've already got this RRset in the response.
1014 CTRACE("query_isduplicate: true: done");
1016 } else if (result == DNS_R_NXRRSET) {
1018 * The name exists, but the rdataset does not.
1020 if (section == DNS_SECTION_ADDITIONAL)
1023 RUNTIME_CHECK(result == DNS_R_NXDOMAIN);
1028 * If the dns_name_t we're looking up is already in the message,
1029 * we don't want to trigger the caller's name replacement logic.
1036 CTRACE("query_isduplicate: false: done");
1041 query_addadditional(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1042 ns_client_t *client = arg;
1043 isc_result_t result, eresult;
1046 dns_name_t *fname, *mname;
1047 dns_rdataset_t *rdataset, *sigrdataset, *trdataset;
1050 dns_dbversion_t *version;
1051 isc_boolean_t added_something, need_addname;
1053 dns_rdatatype_t type;
1055 REQUIRE(NS_CLIENT_VALID(client));
1056 REQUIRE(qtype != dns_rdatatype_any);
1058 if (!WANTDNSSEC(client) && dns_rdatatype_isdnssec(qtype))
1059 return (ISC_R_SUCCESS);
1061 CTRACE("query_addadditional");
1066 eresult = ISC_R_SUCCESS;
1074 added_something = ISC_FALSE;
1075 need_addname = ISC_FALSE;
1079 * We treat type A additional section processing as if it
1080 * were "any address type" additional section processing.
1081 * To avoid multiple lookups, we do an 'any' database
1082 * lookup and iterate over the node.
1084 if (qtype == dns_rdatatype_a)
1085 type = dns_rdatatype_any;
1090 * Get some resources.
1092 dbuf = query_getnamebuf(client);
1095 fname = query_newname(client, dbuf, &b);
1096 rdataset = query_newrdataset(client);
1097 if (fname == NULL || rdataset == NULL)
1099 if (WANTDNSSEC(client)) {
1100 sigrdataset = query_newrdataset(client);
1101 if (sigrdataset == NULL)
1106 * Look for a zone database that might contain authoritative
1109 result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1110 &zone, &db, &version);
1111 if (result != ISC_R_SUCCESS)
1114 CTRACE("query_addadditional: db_find");
1117 * Since we are looking for authoritative data, we do not set
1118 * the GLUEOK flag. Glue will be looked for later, but not
1119 * necessarily in the same database.
1122 result = dns_db_find(db, name, version, type, client->query.dboptions,
1123 client->now, &node, fname, rdataset,
1125 if (result == ISC_R_SUCCESS) {
1126 if (sigrdataset != NULL && !dns_db_issecure(db) &&
1127 dns_rdataset_isassociated(sigrdataset))
1128 dns_rdataset_disassociate(sigrdataset);
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 authoritative data was found. The cache is our next best bet.
1146 result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1147 if (result != ISC_R_SUCCESS)
1149 * Most likely the client isn't allowed to query the cache.
1153 * Attempt to validate glue.
1155 if (sigrdataset == NULL) {
1156 sigrdataset = query_newrdataset(client);
1157 if (sigrdataset == NULL)
1160 result = dns_db_find(db, name, version, type,
1161 client->query.dboptions |
1162 DNS_DBFIND_GLUEOK | DNS_DBFIND_ADDITIONALOK,
1163 client->now, &node, fname, rdataset,
1165 if (result == DNS_R_GLUE &&
1166 validate(client, db, fname, rdataset, sigrdataset))
1167 result = ISC_R_SUCCESS;
1168 if (!WANTDNSSEC(client))
1169 query_putrdataset(client, &sigrdataset);
1170 if (result == ISC_R_SUCCESS)
1173 if (dns_rdataset_isassociated(rdataset))
1174 dns_rdataset_disassociate(rdataset);
1175 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
1176 dns_rdataset_disassociate(sigrdataset);
1178 dns_db_detachnode(db, &node);
1183 * No cached data was found. Glue is our last chance.
1186 * NS records cause both the usual additional section
1187 * processing to locate a type A record, and, when used
1188 * in a referral, a special search of the zone in which
1189 * they reside for glue information.
1191 * This is the "special search". Note that we must search
1192 * the zone where the NS record resides, not the zone it
1193 * points to, and that we only do the search in the delegation
1194 * case (identified by client->query.gluedb being set).
1197 if (client->query.gluedb == NULL)
1201 * Don't poison caches using the bailiwick protection model.
1203 if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1206 dns_db_attach(client->query.gluedb, &db);
1207 result = dns_db_find(db, name, version, type,
1208 client->query.dboptions | DNS_DBFIND_GLUEOK,
1209 client->now, &node, fname, rdataset,
1211 if (!(result == ISC_R_SUCCESS ||
1212 result == DNS_R_ZONECUT ||
1213 result == DNS_R_GLUE))
1218 * We have found a potential additional data rdataset, or
1219 * at least a node to iterate over.
1221 query_keepname(client, fname, dbuf);
1224 * If we have an rdataset, add it to the additional data
1228 if (dns_rdataset_isassociated(rdataset) &&
1229 !query_isduplicate(client, fname, type, &mname)) {
1230 if (mname != NULL) {
1231 query_releasename(client, &fname);
1234 need_addname = ISC_TRUE;
1235 ISC_LIST_APPEND(fname->list, rdataset, link);
1236 trdataset = rdataset;
1238 added_something = ISC_TRUE;
1240 * Note: we only add SIGs if we've added the type they cover,
1241 * so we do not need to check if the SIG rdataset is already
1244 if (sigrdataset != NULL &&
1245 dns_rdataset_isassociated(sigrdataset))
1247 ISC_LIST_APPEND(fname->list, sigrdataset, link);
1252 if (qtype == dns_rdatatype_a) {
1254 * We now go looking for A and AAAA records, along with
1257 * XXXRTH This code could be more efficient.
1259 if (rdataset != NULL) {
1260 if (dns_rdataset_isassociated(rdataset))
1261 dns_rdataset_disassociate(rdataset);
1263 rdataset = query_newrdataset(client);
1264 if (rdataset == NULL)
1267 if (sigrdataset != NULL) {
1268 if (dns_rdataset_isassociated(sigrdataset))
1269 dns_rdataset_disassociate(sigrdataset);
1270 } else if (WANTDNSSEC(client)) {
1271 sigrdataset = query_newrdataset(client);
1272 if (sigrdataset == NULL)
1275 result = dns_db_findrdataset(db, node, version,
1277 client->now, rdataset,
1279 if (result == DNS_R_NCACHENXDOMAIN)
1281 if (result == DNS_R_NCACHENXRRSET) {
1282 dns_rdataset_disassociate(rdataset);
1283 if (sigrdataset != NULL &&
1284 dns_rdataset_isassociated(sigrdataset))
1285 dns_rdataset_disassociate(sigrdataset);
1287 if (result == ISC_R_SUCCESS) {
1289 if (!query_isduplicate(client, fname,
1290 dns_rdatatype_a, &mname)) {
1291 if (mname != NULL) {
1292 query_releasename(client, &fname);
1295 need_addname = ISC_TRUE;
1296 ISC_LIST_APPEND(fname->list, rdataset, link);
1297 added_something = ISC_TRUE;
1298 if (sigrdataset != NULL &&
1299 dns_rdataset_isassociated(sigrdataset))
1301 ISC_LIST_APPEND(fname->list,
1304 query_newrdataset(client);
1306 rdataset = query_newrdataset(client);
1307 if (rdataset == NULL)
1309 if (WANTDNSSEC(client) && sigrdataset == NULL)
1312 dns_rdataset_disassociate(rdataset);
1313 if (sigrdataset != NULL &&
1314 dns_rdataset_isassociated(sigrdataset))
1315 dns_rdataset_disassociate(sigrdataset);
1318 result = dns_db_findrdataset(db, node, version,
1319 dns_rdatatype_aaaa, 0,
1320 client->now, rdataset,
1322 if (result == DNS_R_NCACHENXDOMAIN)
1324 if (result == DNS_R_NCACHENXRRSET) {
1325 dns_rdataset_disassociate(rdataset);
1326 if (sigrdataset != NULL &&
1327 dns_rdataset_isassociated(sigrdataset))
1328 dns_rdataset_disassociate(sigrdataset);
1330 if (result == ISC_R_SUCCESS) {
1332 if (!query_isduplicate(client, fname,
1333 dns_rdatatype_aaaa, &mname)) {
1334 if (mname != NULL) {
1335 query_releasename(client, &fname);
1338 need_addname = ISC_TRUE;
1339 ISC_LIST_APPEND(fname->list, rdataset, link);
1340 added_something = ISC_TRUE;
1341 if (sigrdataset != NULL &&
1342 dns_rdataset_isassociated(sigrdataset))
1344 ISC_LIST_APPEND(fname->list,
1354 CTRACE("query_addadditional: addname");
1356 * If we haven't added anything, then we're done.
1358 if (!added_something)
1362 * We may have added our rdatasets to an existing name, if so, then
1363 * need_addname will be ISC_FALSE. Whether we used an existing name
1364 * or a new one, we must set fname to NULL to prevent cleanup.
1367 dns_message_addname(client->message, fname,
1368 DNS_SECTION_ADDITIONAL);
1372 * In a few cases, we want to add additional data for additional
1373 * data. It's simpler to just deal with special cases here than
1374 * to try to create a general purpose mechanism and allow the
1375 * rdata implementations to do it themselves.
1377 * This involves recursion, but the depth is limited. The
1378 * most complex case is adding a SRV rdataset, which involves
1379 * recursing to add address records, which in turn can cause
1380 * recursion to add KEYs.
1382 if (type == dns_rdatatype_srv && trdataset != NULL) {
1384 * If we're adding SRV records to the additional data
1385 * section, it's helpful if we add the SRV additional data
1388 eresult = dns_rdataset_additionaldata(trdataset,
1389 query_addadditional,
1394 CTRACE("query_addadditional: cleanup");
1395 query_putrdataset(client, &rdataset);
1396 if (sigrdataset != NULL)
1397 query_putrdataset(client, &sigrdataset);
1399 query_releasename(client, &fname);
1401 dns_db_detachnode(db, &node);
1405 dns_zone_detach(&zone);
1407 CTRACE("query_addadditional: done");
1412 query_discardcache(ns_client_t *client, dns_rdataset_t *rdataset_base,
1413 dns_rdatasetadditional_t additionaltype,
1414 dns_rdatatype_t type, dns_zone_t **zonep, dns_db_t **dbp,
1415 dns_dbversion_t **versionp, dns_dbnode_t **nodep,
1418 dns_rdataset_t *rdataset;
1420 while ((rdataset = ISC_LIST_HEAD(fname->list)) != NULL) {
1421 ISC_LIST_UNLINK(fname->list, rdataset, link);
1422 query_putrdataset(client, &rdataset);
1424 if (*versionp != NULL)
1425 dns_db_closeversion(*dbp, versionp, ISC_FALSE);
1427 dns_db_detachnode(*dbp, nodep);
1431 dns_zone_detach(zonep);
1432 (void)dns_rdataset_putadditional(client->view->acache, rdataset_base,
1433 additionaltype, type);
1436 static inline isc_result_t
1437 query_iscachevalid(dns_zone_t *zone, dns_db_t *db, dns_db_t *db0,
1438 dns_dbversion_t *version)
1440 isc_result_t result = ISC_R_SUCCESS;
1441 dns_dbversion_t *version_current = NULL;
1442 dns_db_t *db_current = db0;
1444 if (db_current == NULL) {
1445 result = dns_zone_getdb(zone, &db_current);
1446 if (result != ISC_R_SUCCESS)
1449 dns_db_currentversion(db_current, &version_current);
1450 if (db_current != db || version_current != version) {
1451 result = ISC_R_FAILURE;
1456 dns_db_closeversion(db_current, &version_current, ISC_FALSE);
1457 if (db0 == NULL && db_current != NULL)
1458 dns_db_detach(&db_current);
1464 query_addadditional2(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1465 client_additionalctx_t *additionalctx = arg;
1466 dns_rdataset_t *rdataset_base;
1467 ns_client_t *client;
1468 isc_result_t result, eresult;
1469 dns_dbnode_t *node, *cnode;
1471 dns_name_t *fname, *mname0, cfname;
1472 dns_rdataset_t *rdataset, *sigrdataset;
1473 dns_rdataset_t *crdataset, *crdataset_next;
1476 dns_dbversion_t *version, *cversion;
1477 isc_boolean_t added_something, need_addname, needadditionalcache;
1478 isc_boolean_t need_sigrrset;
1480 dns_rdatatype_t type;
1481 dns_rdatasetadditional_t additionaltype;
1483 if (qtype != dns_rdatatype_a) {
1485 * This function is optimized for "address" types. For other
1486 * types, use a generic routine.
1487 * XXX: ideally, this function should be generic enough.
1489 return (query_addadditional(additionalctx->client,
1496 rdataset_base = additionalctx->rdataset;
1497 client = additionalctx->client;
1498 REQUIRE(NS_CLIENT_VALID(client));
1499 eresult = ISC_R_SUCCESS;
1509 added_something = ISC_FALSE;
1510 need_addname = ISC_FALSE;
1512 needadditionalcache = ISC_FALSE;
1513 additionaltype = dns_rdatasetadditional_fromauth;
1514 dns_name_init(&cfname, NULL);
1516 CTRACE("query_addadditional2");
1519 * We treat type A additional section processing as if it
1520 * were "any address type" additional section processing.
1521 * To avoid multiple lookups, we do an 'any' database
1522 * lookup and iterate over the node.
1523 * XXXJT: this approach can cause a suboptimal result when the cache
1524 * DB only has partial address types and the glue DB has remaining
1527 type = dns_rdatatype_any;
1530 * Get some resources.
1532 dbuf = query_getnamebuf(client);
1535 fname = query_newname(client, dbuf, &b);
1538 dns_name_setbuffer(&cfname, &b); /* share the buffer */
1540 /* Check additional cache */
1541 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1542 type, client->view->acache, &zone,
1543 &cdb, &cversion, &cnode, &cfname,
1544 client->message, client->now);
1545 if (result != ISC_R_SUCCESS)
1548 CTRACE("query_addadditional2: auth zone not found");
1552 /* Is the cached DB up-to-date? */
1553 result = query_iscachevalid(zone, cdb, NULL, cversion);
1554 if (result != ISC_R_SUCCESS) {
1555 CTRACE("query_addadditional2: old auth additional cache");
1556 query_discardcache(client, rdataset_base, additionaltype,
1557 type, &zone, &cdb, &cversion, &cnode,
1562 if (cnode == NULL) {
1564 * We have a negative cache. We don't have to check the zone
1565 * ACL, since the result (not using this zone) would be same
1566 * regardless of the result.
1568 CTRACE("query_addadditional2: negative auth additional cache");
1569 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1570 dns_db_detach(&cdb);
1571 dns_zone_detach(&zone);
1575 result = query_validatezonedb(client, name, qtype, DNS_GETDB_NOLOG,
1577 if (result != ISC_R_SUCCESS) {
1578 query_discardcache(client, rdataset_base, additionaltype,
1579 type, &zone, &cdb, &cversion, &cnode,
1584 /* We've got an active cache. */
1585 CTRACE("query_addadditional2: auth additional cache");
1586 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1589 dns_name_clone(&cfname, fname);
1590 query_keepname(client, fname, dbuf);
1594 * Look for a zone database that might contain authoritative
1598 result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1599 &zone, &db, &version);
1600 if (result != ISC_R_SUCCESS) {
1601 /* Cache the negative result */
1602 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1603 type, client->view->acache,
1604 NULL, NULL, NULL, NULL,
1609 CTRACE("query_addadditional2: db_find");
1612 * Since we are looking for authoritative data, we do not set
1613 * the GLUEOK flag. Glue will be looked for later, but not
1614 * necessarily in the same database.
1617 result = dns_db_find(db, name, version, type, client->query.dboptions,
1618 client->now, &node, fname, NULL, NULL);
1619 if (result == ISC_R_SUCCESS)
1622 /* Cache the negative result */
1623 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1624 type, client->view->acache, zone, db,
1625 version, NULL, fname);
1628 dns_db_detachnode(db, &node);
1633 * No authoritative data was found. The cache is our next best bet.
1637 additionaltype = dns_rdatasetadditional_fromcache;
1638 result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1639 if (result != ISC_R_SUCCESS)
1641 * Most likely the client isn't allowed to query the cache.
1645 result = dns_db_find(db, name, version, type,
1646 client->query.dboptions |
1647 DNS_DBFIND_GLUEOK | DNS_DBFIND_ADDITIONALOK,
1648 client->now, &node, fname, NULL, NULL);
1649 if (result == ISC_R_SUCCESS)
1653 dns_db_detachnode(db, &node);
1658 * No cached data was found. Glue is our last chance.
1661 * NS records cause both the usual additional section
1662 * processing to locate a type A record, and, when used
1663 * in a referral, a special search of the zone in which
1664 * they reside for glue information.
1666 * This is the "special search". Note that we must search
1667 * the zone where the NS record resides, not the zone it
1668 * points to, and that we only do the search in the delegation
1669 * case (identified by client->query.gluedb being set).
1671 if (client->query.gluedb == NULL)
1675 * Don't poison caches using the bailiwick protection model.
1677 if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1680 /* Check additional cache */
1681 additionaltype = dns_rdatasetadditional_fromglue;
1682 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1683 type, client->view->acache, NULL,
1684 &cdb, &cversion, &cnode, &cfname,
1685 client->message, client->now);
1686 if (result != ISC_R_SUCCESS)
1689 result = query_iscachevalid(zone, cdb, client->query.gluedb, cversion);
1690 if (result != ISC_R_SUCCESS) {
1691 CTRACE("query_addadditional2: old glue additional cache");
1692 query_discardcache(client, rdataset_base, additionaltype,
1693 type, &zone, &cdb, &cversion, &cnode,
1698 if (cnode == NULL) {
1699 /* We have a negative cache. */
1700 CTRACE("query_addadditional2: negative glue additional cache");
1701 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1702 dns_db_detach(&cdb);
1707 CTRACE("query_addadditional2: glue additional cache");
1708 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1711 dns_name_clone(&cfname, fname);
1712 query_keepname(client, fname, dbuf);
1716 dns_db_attach(client->query.gluedb, &db);
1717 result = dns_db_find(db, name, version, type,
1718 client->query.dboptions | DNS_DBFIND_GLUEOK,
1719 client->now, &node, fname, NULL, NULL);
1720 if (!(result == ISC_R_SUCCESS ||
1721 result == DNS_R_ZONECUT ||
1722 result == DNS_R_GLUE)) {
1723 /* cache the negative result */
1724 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1725 type, client->view->acache,
1726 NULL, db, version, NULL,
1733 * We have found a DB node to iterate over from a DB.
1734 * We are going to look for address RRsets (i.e., A and AAAA) in the DB
1735 * node we've just found. We'll then store the complete information
1736 * in the additional data cache.
1738 dns_name_clone(fname, &cfname);
1739 query_keepname(client, fname, dbuf);
1740 needadditionalcache = ISC_TRUE;
1742 rdataset = query_newrdataset(client);
1743 if (rdataset == NULL)
1746 sigrdataset = query_newrdataset(client);
1747 if (sigrdataset == NULL)
1751 * Find A RRset with sig RRset. Even if we don't find a sig RRset
1752 * for a client using DNSSEC, we'll continue the process to make a
1753 * complete list to be cached. However, we need to cancel the
1754 * caching when something unexpected happens, in order to avoid
1755 * caching incomplete information.
1757 result = dns_db_findrdataset(db, node, version, dns_rdatatype_a, 0,
1758 client->now, rdataset, sigrdataset);
1760 * If we can't promote glue/pending from the cache to secure
1763 if (result == ISC_R_SUCCESS &&
1764 additionaltype == dns_rdatasetadditional_fromcache &&
1765 (DNS_TRUST_PENDING(rdataset->trust) ||
1766 DNS_TRUST_GLUE(rdataset->trust)) &&
1767 !validate(client, db, fname, rdataset, sigrdataset)) {
1768 dns_rdataset_disassociate(rdataset);
1769 if (dns_rdataset_isassociated(sigrdataset))
1770 dns_rdataset_disassociate(sigrdataset);
1771 result = ISC_R_NOTFOUND;
1773 if (result == DNS_R_NCACHENXDOMAIN)
1775 if (result == DNS_R_NCACHENXRRSET) {
1776 dns_rdataset_disassociate(rdataset);
1777 if (dns_rdataset_isassociated(sigrdataset))
1778 dns_rdataset_disassociate(sigrdataset);
1780 if (result == ISC_R_SUCCESS) {
1781 /* Remember the result as a cache */
1782 ISC_LIST_APPEND(cfname.list, rdataset, link);
1783 if (dns_rdataset_isassociated(sigrdataset)) {
1784 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1785 sigrdataset = query_newrdataset(client);
1787 rdataset = query_newrdataset(client);
1788 if (sigrdataset == NULL || rdataset == NULL) {
1789 /* do not cache incomplete information */
1794 /* Find AAAA RRset with sig RRset */
1795 result = dns_db_findrdataset(db, node, version, dns_rdatatype_aaaa,
1796 0, client->now, rdataset, sigrdataset);
1798 * If we can't promote glue/pending from the cache to secure
1801 if (result == ISC_R_SUCCESS &&
1802 additionaltype == dns_rdatasetadditional_fromcache &&
1803 (DNS_TRUST_PENDING(rdataset->trust) ||
1804 DNS_TRUST_GLUE(rdataset->trust)) &&
1805 !validate(client, db, fname, rdataset, sigrdataset)) {
1806 dns_rdataset_disassociate(rdataset);
1807 if (dns_rdataset_isassociated(sigrdataset))
1808 dns_rdataset_disassociate(sigrdataset);
1809 result = ISC_R_NOTFOUND;
1811 if (result == ISC_R_SUCCESS) {
1812 ISC_LIST_APPEND(cfname.list, rdataset, link);
1814 if (dns_rdataset_isassociated(sigrdataset)) {
1815 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1822 * Set the new result in the cache if required. We do not support
1823 * caching additional data from a cache DB.
1825 if (needadditionalcache == ISC_TRUE &&
1826 (additionaltype == dns_rdatasetadditional_fromauth ||
1827 additionaltype == dns_rdatasetadditional_fromglue)) {
1828 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1829 type, client->view->acache,
1830 zone, db, version, node,
1835 need_sigrrset = ISC_FALSE;
1837 for (crdataset = ISC_LIST_HEAD(cfname.list);
1839 crdataset = crdataset_next) {
1842 crdataset_next = ISC_LIST_NEXT(crdataset, link);
1845 if (crdataset->type == dns_rdatatype_a ||
1846 crdataset->type == dns_rdatatype_aaaa) {
1847 if (!query_isduplicate(client, fname, crdataset->type,
1849 if (mname != NULL) {
1851 * A different type of this name is
1852 * already stored in the additional
1853 * section. We'll reuse the name.
1854 * Note that this should happen at most
1855 * once. Otherwise, fname->link could
1858 INSIST(mname0 == NULL);
1860 query_releasename(client, &fname);
1864 need_addname = ISC_TRUE;
1865 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1866 ISC_LIST_APPEND(fname->list, crdataset, link);
1867 added_something = ISC_TRUE;
1868 need_sigrrset = ISC_TRUE;
1870 need_sigrrset = ISC_FALSE;
1871 } else if (crdataset->type == dns_rdatatype_rrsig &&
1872 need_sigrrset && WANTDNSSEC(client)) {
1873 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1874 ISC_LIST_APPEND(fname->list, crdataset, link);
1875 added_something = ISC_TRUE; /* just in case */
1876 need_sigrrset = ISC_FALSE;
1880 CTRACE("query_addadditional2: addname");
1883 * If we haven't added anything, then we're done.
1885 if (!added_something)
1889 * We may have added our rdatasets to an existing name, if so, then
1890 * need_addname will be ISC_FALSE. Whether we used an existing name
1891 * or a new one, we must set fname to NULL to prevent cleanup.
1894 dns_message_addname(client->message, fname,
1895 DNS_SECTION_ADDITIONAL);
1899 CTRACE("query_addadditional2: cleanup");
1901 if (rdataset != NULL)
1902 query_putrdataset(client, &rdataset);
1903 if (sigrdataset != NULL)
1904 query_putrdataset(client, &sigrdataset);
1905 while ((crdataset = ISC_LIST_HEAD(cfname.list)) != NULL) {
1906 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1907 query_putrdataset(client, &crdataset);
1910 query_releasename(client, &fname);
1912 dns_db_detachnode(db, &node);
1916 dns_zone_detach(&zone);
1918 CTRACE("query_addadditional2: done");
1923 query_addrdataset(ns_client_t *client, dns_name_t *fname,
1924 dns_rdataset_t *rdataset)
1926 client_additionalctx_t additionalctx;
1929 * Add 'rdataset' and any pertinent additional data to
1930 * 'fname', a name in the response message for 'client'.
1933 CTRACE("query_addrdataset");
1935 ISC_LIST_APPEND(fname->list, rdataset, link);
1937 if (client->view->order != NULL)
1938 rdataset->attributes |= dns_order_find(client->view->order,
1939 fname, rdataset->type,
1941 rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
1943 if (NOADDITIONAL(client))
1947 * Add additional data.
1949 * We don't care if dns_rdataset_additionaldata() fails.
1951 additionalctx.client = client;
1952 additionalctx.rdataset = rdataset;
1953 (void)dns_rdataset_additionaldata(rdataset, query_addadditional2,
1955 CTRACE("query_addrdataset: done");
1959 query_addrrset(ns_client_t *client, dns_name_t **namep,
1960 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp,
1961 isc_buffer_t *dbuf, dns_section_t section)
1963 dns_name_t *name, *mname;
1964 dns_rdataset_t *rdataset, *mrdataset, *sigrdataset;
1965 isc_result_t result;
1968 * To the current response for 'client', add the answer RRset
1969 * '*rdatasetp' and an optional signature set '*sigrdatasetp', with
1970 * owner name '*namep', to section 'section', unless they are
1971 * already there. Also add any pertinent additional data.
1973 * If 'dbuf' is not NULL, then '*namep' is the name whose data is
1974 * stored in 'dbuf'. In this case, query_addrrset() guarantees that
1975 * when it returns the name will either have been kept or released.
1977 CTRACE("query_addrrset");
1979 rdataset = *rdatasetp;
1980 if (sigrdatasetp != NULL)
1981 sigrdataset = *sigrdatasetp;
1986 result = dns_message_findname(client->message, section,
1987 name, rdataset->type, rdataset->covers,
1988 &mname, &mrdataset);
1989 if (result == ISC_R_SUCCESS) {
1991 * We've already got an RRset of the given name and type.
1992 * There's nothing else to do;
1994 CTRACE("query_addrrset: dns_message_findname succeeded: done");
1996 query_releasename(client, namep);
1998 } else if (result == DNS_R_NXDOMAIN) {
2000 * The name doesn't exist.
2003 query_keepname(client, name, dbuf);
2004 dns_message_addname(client->message, name, section);
2008 RUNTIME_CHECK(result == DNS_R_NXRRSET);
2010 query_releasename(client, namep);
2013 if (rdataset->trust != dns_trust_secure &&
2014 (section == DNS_SECTION_ANSWER ||
2015 section == DNS_SECTION_AUTHORITY))
2016 client->query.attributes &= ~NS_QUERYATTR_SECURE;
2018 * Note: we only add SIGs if we've added the type they cover, so
2019 * we do not need to check if the SIG rdataset is already in the
2022 query_addrdataset(client, mname, rdataset);
2024 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset)) {
2026 * We have a signature. Add it to the response.
2028 ISC_LIST_APPEND(mname->list, sigrdataset, link);
2029 *sigrdatasetp = NULL;
2031 CTRACE("query_addrrset: done");
2034 static inline isc_result_t
2035 query_addsoa(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version,
2036 isc_boolean_t zero_ttl, isc_boolean_t isassociated)
2040 isc_result_t result, eresult;
2041 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2042 dns_rdataset_t **sigrdatasetp = NULL;
2044 CTRACE("query_addsoa");
2048 eresult = ISC_R_SUCCESS;
2054 * Don't add the SOA record for test which set "-T nosoa".
2056 if (ns_g_nosoa && (!WANTDNSSEC(client) || !isassociated))
2057 return (ISC_R_SUCCESS);
2060 * Get resources and make 'name' be the database origin.
2062 result = dns_message_gettempname(client->message, &name);
2063 if (result != ISC_R_SUCCESS)
2065 dns_name_init(name, NULL);
2066 dns_name_clone(dns_db_origin(db), name);
2067 rdataset = query_newrdataset(client);
2068 if (rdataset == NULL) {
2069 eresult = DNS_R_SERVFAIL;
2072 if (WANTDNSSEC(client) && dns_db_issecure(db)) {
2073 sigrdataset = query_newrdataset(client);
2074 if (sigrdataset == NULL) {
2075 eresult = DNS_R_SERVFAIL;
2083 result = dns_db_getoriginnode(db, &node);
2084 if (result == ISC_R_SUCCESS) {
2085 result = dns_db_findrdataset(db, node, version,
2087 0, client->now, rdataset,
2090 dns_fixedname_t foundname;
2093 dns_fixedname_init(&foundname);
2094 fname = dns_fixedname_name(&foundname);
2096 result = dns_db_find(db, name, version, dns_rdatatype_soa,
2097 client->query.dboptions, 0, &node,
2098 fname, rdataset, sigrdataset);
2100 if (result != ISC_R_SUCCESS) {
2102 * This is bad. We tried to get the SOA RR at the zone top
2103 * and it didn't work!
2105 eresult = DNS_R_SERVFAIL;
2108 * Extract the SOA MINIMUM.
2110 dns_rdata_soa_t soa;
2111 dns_rdata_t rdata = DNS_RDATA_INIT;
2112 result = dns_rdataset_first(rdataset);
2113 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2114 dns_rdataset_current(rdataset, &rdata);
2115 result = dns_rdata_tostruct(&rdata, &soa, NULL);
2116 if (result != ISC_R_SUCCESS)
2121 if (sigrdataset != NULL)
2122 sigrdataset->ttl = 0;
2126 * Add the SOA and its SIG to the response, with the
2127 * TTLs adjusted per RFC2308 section 3.
2129 if (rdataset->ttl > soa.minimum)
2130 rdataset->ttl = soa.minimum;
2131 if (sigrdataset != NULL && sigrdataset->ttl > soa.minimum)
2132 sigrdataset->ttl = soa.minimum;
2134 if (sigrdataset != NULL)
2135 sigrdatasetp = &sigrdataset;
2137 sigrdatasetp = NULL;
2138 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2139 DNS_SECTION_AUTHORITY);
2143 query_putrdataset(client, &rdataset);
2144 if (sigrdataset != NULL)
2145 query_putrdataset(client, &sigrdataset);
2147 query_releasename(client, &name);
2149 dns_db_detachnode(db, &node);
2154 static inline isc_result_t
2155 query_addns(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version) {
2156 dns_name_t *name, *fname;
2158 isc_result_t result, eresult;
2159 dns_fixedname_t foundname;
2160 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2161 dns_rdataset_t **sigrdatasetp = NULL;
2163 CTRACE("query_addns");
2167 eresult = ISC_R_SUCCESS;
2171 dns_fixedname_init(&foundname);
2172 fname = dns_fixedname_name(&foundname);
2175 * Get resources and make 'name' be the database origin.
2177 result = dns_message_gettempname(client->message, &name);
2178 if (result != ISC_R_SUCCESS) {
2179 CTRACE("query_addns: dns_message_gettempname failed: done");
2182 dns_name_init(name, NULL);
2183 dns_name_clone(dns_db_origin(db), name);
2184 rdataset = query_newrdataset(client);
2185 if (rdataset == NULL) {
2186 CTRACE("query_addns: query_newrdataset failed");
2187 eresult = DNS_R_SERVFAIL;
2190 if (WANTDNSSEC(client) && dns_db_issecure(db)) {
2191 sigrdataset = query_newrdataset(client);
2192 if (sigrdataset == NULL) {
2193 CTRACE("query_addns: query_newrdataset failed");
2194 eresult = DNS_R_SERVFAIL;
2200 * Find the NS rdataset.
2202 result = dns_db_getoriginnode(db, &node);
2203 if (result == ISC_R_SUCCESS) {
2204 result = dns_db_findrdataset(db, node, version,
2206 0, client->now, rdataset,
2209 CTRACE("query_addns: calling dns_db_find");
2210 result = dns_db_find(db, name, NULL, dns_rdatatype_ns,
2211 client->query.dboptions, 0, &node,
2212 fname, rdataset, sigrdataset);
2213 CTRACE("query_addns: dns_db_find complete");
2215 if (result != ISC_R_SUCCESS) {
2216 CTRACE("query_addns: "
2217 "dns_db_findrdataset or dns_db_find failed");
2219 * This is bad. We tried to get the NS rdataset at the zone
2220 * top and it didn't work!
2222 eresult = DNS_R_SERVFAIL;
2224 if (sigrdataset != NULL)
2225 sigrdatasetp = &sigrdataset;
2227 sigrdatasetp = NULL;
2228 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2229 DNS_SECTION_AUTHORITY);
2233 CTRACE("query_addns: cleanup");
2234 query_putrdataset(client, &rdataset);
2235 if (sigrdataset != NULL)
2236 query_putrdataset(client, &sigrdataset);
2238 query_releasename(client, &name);
2240 dns_db_detachnode(db, &node);
2242 CTRACE("query_addns: done");
2246 static inline isc_result_t
2247 query_addcnamelike(ns_client_t *client, dns_name_t *qname, dns_name_t *tname,
2248 dns_rdataset_t *dname, dns_name_t **anamep,
2249 dns_rdatatype_t type)
2251 dns_rdataset_t *rdataset;
2252 dns_rdatalist_t *rdatalist;
2254 isc_result_t result;
2258 * We assume the name data referred to by tname won't go away.
2261 REQUIRE(anamep != NULL);
2264 result = dns_message_gettemprdatalist(client->message, &rdatalist);
2265 if (result != ISC_R_SUCCESS)
2268 result = dns_message_gettemprdata(client->message, &rdata);
2269 if (result != ISC_R_SUCCESS)
2272 result = dns_message_gettemprdataset(client->message, &rdataset);
2273 if (result != ISC_R_SUCCESS)
2275 dns_rdataset_init(rdataset);
2276 result = dns_name_dup(qname, client->mctx, *anamep);
2277 if (result != ISC_R_SUCCESS) {
2278 dns_message_puttemprdataset(client->message, &rdataset);
2282 rdatalist->type = type;
2283 rdatalist->covers = 0;
2284 rdatalist->rdclass = client->message->rdclass;
2285 rdatalist->ttl = dname->ttl;
2287 dns_name_toregion(tname, &r);
2288 rdata->data = r.base;
2289 rdata->length = r.length;
2290 rdata->rdclass = client->message->rdclass;
2293 ISC_LIST_INIT(rdatalist->rdata);
2294 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
2295 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
2297 rdataset->trust = dname->trust;
2299 query_addrrset(client, anamep, &rdataset, NULL, NULL,
2300 DNS_SECTION_ANSWER);
2302 if (rdataset != NULL) {
2303 if (dns_rdataset_isassociated(rdataset))
2304 dns_rdataset_disassociate(rdataset);
2305 dns_message_puttemprdataset(client->message, &rdataset);
2308 return (ISC_R_SUCCESS);
2312 * Mark the RRsets as secure. Update the cache (db) to reflect the
2313 * change in trust level.
2316 mark_secure(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2317 isc_uint32_t ttl, dns_rdataset_t *rdataset,
2318 dns_rdataset_t *sigrdataset)
2320 isc_result_t result;
2321 dns_dbnode_t *node = NULL;
2323 rdataset->trust = dns_trust_secure;
2324 sigrdataset->trust = dns_trust_secure;
2327 * Save the updated secure state. Ignore failures.
2329 result = dns_db_findnode(db, name, ISC_TRUE, &node);
2330 if (result != ISC_R_SUCCESS)
2333 * Bound the validated ttls then minimise.
2335 if (sigrdataset->ttl > ttl)
2336 sigrdataset->ttl = ttl;
2337 if (rdataset->ttl > ttl)
2338 rdataset->ttl = ttl;
2339 if (rdataset->ttl > sigrdataset->ttl)
2340 rdataset->ttl = sigrdataset->ttl;
2342 sigrdataset->ttl = rdataset->ttl;
2344 (void)dns_db_addrdataset(db, node, NULL, client->now, rdataset,
2346 (void)dns_db_addrdataset(db, node, NULL, client->now, sigrdataset,
2348 dns_db_detachnode(db, &node);
2352 * Find the secure key that corresponds to rrsig.
2353 * Note: 'keyrdataset' maintains state between successive calls,
2354 * there may be multiple keys with the same keyid.
2355 * Return ISC_FALSE if we have exhausted all the possible keys.
2357 static isc_boolean_t
2358 get_key(ns_client_t *client, dns_db_t *db, dns_rdata_rrsig_t *rrsig,
2359 dns_rdataset_t *keyrdataset, dst_key_t **keyp)
2361 isc_result_t result;
2362 dns_dbnode_t *node = NULL;
2363 isc_boolean_t secure = ISC_FALSE;
2365 if (!dns_rdataset_isassociated(keyrdataset)) {
2366 result = dns_db_findnode(db, &rrsig->signer, ISC_FALSE, &node);
2367 if (result != ISC_R_SUCCESS)
2370 result = dns_db_findrdataset(db, node, NULL,
2371 dns_rdatatype_dnskey, 0,
2372 client->now, keyrdataset, NULL);
2373 dns_db_detachnode(db, &node);
2374 if (result != ISC_R_SUCCESS)
2377 if (keyrdataset->trust != dns_trust_secure)
2380 result = dns_rdataset_first(keyrdataset);
2382 result = dns_rdataset_next(keyrdataset);
2384 for ( ; result == ISC_R_SUCCESS;
2385 result = dns_rdataset_next(keyrdataset)) {
2386 dns_rdata_t rdata = DNS_RDATA_INIT;
2389 dns_rdataset_current(keyrdataset, &rdata);
2390 isc_buffer_init(&b, rdata.data, rdata.length);
2391 isc_buffer_add(&b, rdata.length);
2392 result = dst_key_fromdns(&rrsig->signer, rdata.rdclass, &b,
2393 client->mctx, keyp);
2394 if (result != ISC_R_SUCCESS)
2396 if (rrsig->algorithm == (dns_secalg_t)dst_key_alg(*keyp) &&
2397 rrsig->keyid == (dns_keytag_t)dst_key_id(*keyp) &&
2398 dst_key_iszonekey(*keyp)) {
2407 static isc_boolean_t
2408 verify(dst_key_t *key, dns_name_t *name, dns_rdataset_t *rdataset,
2409 dns_rdata_t *rdata, isc_mem_t *mctx, isc_boolean_t acceptexpired)
2411 isc_result_t result;
2412 dns_fixedname_t fixed;
2413 isc_boolean_t ignore = ISC_FALSE;
2415 dns_fixedname_init(&fixed);
2418 result = dns_dnssec_verify2(name, rdataset, key, ignore, mctx,
2420 if (result == DNS_R_SIGEXPIRED && acceptexpired) {
2424 if (result == ISC_R_SUCCESS || result == DNS_R_FROMWILDCARD)
2430 * Validate the rdataset if possible with available records.
2432 static isc_boolean_t
2433 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2434 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2436 isc_result_t result;
2437 dns_rdata_t rdata = DNS_RDATA_INIT;
2438 dns_rdata_rrsig_t rrsig;
2439 dst_key_t *key = NULL;
2440 dns_rdataset_t keyrdataset;
2442 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2445 for (result = dns_rdataset_first(sigrdataset);
2446 result == ISC_R_SUCCESS;
2447 result = dns_rdataset_next(sigrdataset)) {
2449 dns_rdata_reset(&rdata);
2450 dns_rdataset_current(sigrdataset, &rdata);
2451 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
2452 if (result != ISC_R_SUCCESS)
2454 if (!dns_resolver_algorithm_supported(client->view->resolver,
2455 name, rrsig.algorithm))
2457 if (!dns_name_issubdomain(name, &rrsig.signer))
2459 dns_rdataset_init(&keyrdataset);
2461 if (!get_key(client, db, &rrsig, &keyrdataset, &key))
2463 if (verify(key, name, rdataset, &rdata, client->mctx,
2464 client->view->acceptexpired)) {
2466 dns_rdataset_disassociate(&keyrdataset);
2467 mark_secure(client, db, name,
2469 rdataset, sigrdataset);
2474 if (dns_rdataset_isassociated(&keyrdataset))
2475 dns_rdataset_disassociate(&keyrdataset);
2481 query_addbestns(ns_client_t *client) {
2484 dns_name_t *fname, *zfname;
2485 dns_rdataset_t *rdataset, *sigrdataset, *zrdataset, *zsigrdataset;
2486 isc_boolean_t is_zone, use_zone;
2488 isc_result_t result;
2489 dns_dbversion_t *version;
2493 CTRACE("query_addbestns");
2499 zsigrdataset = NULL;
2505 is_zone = ISC_FALSE;
2506 use_zone = ISC_FALSE;
2509 * Find the right database.
2511 result = query_getdb(client, client->query.qname, dns_rdatatype_ns, 0,
2512 &zone, &db, &version, &is_zone);
2513 if (result != ISC_R_SUCCESS)
2518 * We'll need some resources...
2520 dbuf = query_getnamebuf(client);
2523 fname = query_newname(client, dbuf, &b);
2524 rdataset = query_newrdataset(client);
2525 if (fname == NULL || rdataset == NULL)
2528 * Get the RRSIGs if the client requested them or if we may
2529 * need to validate answers from the cache.
2531 if (WANTDNSSEC(client) || !is_zone) {
2532 sigrdataset = query_newrdataset(client);
2533 if (sigrdataset == NULL)
2538 * Now look for the zonecut.
2541 result = dns_db_find(db, client->query.qname, version,
2542 dns_rdatatype_ns, client->query.dboptions,
2543 client->now, &node, fname,
2544 rdataset, sigrdataset);
2545 if (result != DNS_R_DELEGATION)
2547 if (USECACHE(client)) {
2548 query_keepname(client, fname, dbuf);
2552 zrdataset = rdataset;
2554 zsigrdataset = sigrdataset;
2556 dns_db_detachnode(db, &node);
2559 dns_db_attach(client->view->cachedb, &db);
2560 is_zone = ISC_FALSE;
2564 result = dns_db_findzonecut(db, client->query.qname,
2565 client->query.dboptions,
2566 client->now, &node, fname,
2567 rdataset, sigrdataset);
2568 if (result == ISC_R_SUCCESS) {
2569 if (zfname != NULL &&
2570 !dns_name_issubdomain(fname, zfname)) {
2572 * We found a zonecut in the cache, but our
2573 * zone delegation is better.
2575 use_zone = ISC_TRUE;
2577 } else if (result == ISC_R_NOTFOUND && zfname != NULL) {
2579 * We didn't find anything in the cache, but we
2580 * have a zone delegation, so use it.
2582 use_zone = ISC_TRUE;
2588 query_releasename(client, &fname);
2592 * We've already done query_keepname() on
2593 * zfname, so we must set dbuf to NULL to
2594 * prevent query_addrrset() from trying to
2595 * call query_keepname() again.
2598 query_putrdataset(client, &rdataset);
2599 if (sigrdataset != NULL)
2600 query_putrdataset(client, &sigrdataset);
2601 rdataset = zrdataset;
2603 sigrdataset = zsigrdataset;
2604 zsigrdataset = NULL;
2608 * Attempt to validate RRsets that are pending or that are glue.
2610 if ((DNS_TRUST_PENDING(rdataset->trust) ||
2611 (sigrdataset != NULL && DNS_TRUST_PENDING(sigrdataset->trust)))
2612 && !validate(client, db, fname, rdataset, sigrdataset) &&
2613 !PENDINGOK(client->query.dboptions))
2616 if ((DNS_TRUST_GLUE(rdataset->trust) ||
2617 (sigrdataset != NULL && DNS_TRUST_GLUE(sigrdataset->trust))) &&
2618 !validate(client, db, fname, rdataset, sigrdataset) &&
2619 SECURE(client) && WANTDNSSEC(client))
2623 * If the client doesn't want DNSSEC we can discard the sigrdataset
2626 if (!WANTDNSSEC(client))
2627 query_putrdataset(client, &sigrdataset);
2628 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2629 DNS_SECTION_AUTHORITY);
2632 if (rdataset != NULL)
2633 query_putrdataset(client, &rdataset);
2634 if (sigrdataset != NULL)
2635 query_putrdataset(client, &sigrdataset);
2637 query_releasename(client, &fname);
2639 dns_db_detachnode(db, &node);
2643 dns_zone_detach(&zone);
2645 query_putrdataset(client, &zrdataset);
2646 if (zsigrdataset != NULL)
2647 query_putrdataset(client, &zsigrdataset);
2649 query_releasename(client, &zfname);
2650 dns_db_detach(&zdb);
2655 fixrdataset(ns_client_t *client, dns_rdataset_t **rdataset) {
2656 if (*rdataset == NULL)
2657 *rdataset = query_newrdataset(client);
2658 else if (dns_rdataset_isassociated(*rdataset))
2659 dns_rdataset_disassociate(*rdataset);
2663 fixfname(ns_client_t *client, dns_name_t **fname, isc_buffer_t **dbuf,
2666 if (*fname == NULL) {
2667 *dbuf = query_getnamebuf(client);
2670 *fname = query_newname(client, *dbuf, nbuf);
2675 query_addds(ns_client_t *client, dns_db_t *db, dns_dbnode_t *node,
2676 dns_dbversion_t *version, dns_name_t *name)
2678 dns_fixedname_t fixed;
2679 dns_name_t *fname = NULL;
2681 dns_rdataset_t *rdataset, *sigrdataset;
2682 isc_buffer_t *dbuf, b;
2683 isc_result_t result;
2686 CTRACE("query_addds");
2692 * We'll need some resources...
2694 rdataset = query_newrdataset(client);
2695 sigrdataset = query_newrdataset(client);
2696 if (rdataset == NULL || sigrdataset == NULL)
2700 * Look for the DS record, which may or may not be present.
2702 result = dns_db_findrdataset(db, node, version, dns_rdatatype_ds, 0,
2703 client->now, rdataset, sigrdataset);
2705 * If we didn't find it, look for an NSEC.
2707 if (result == ISC_R_NOTFOUND)
2708 result = dns_db_findrdataset(db, node, version,
2709 dns_rdatatype_nsec, 0, client->now,
2710 rdataset, sigrdataset);
2711 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
2713 if (!dns_rdataset_isassociated(rdataset) ||
2714 !dns_rdataset_isassociated(sigrdataset))
2718 * We've already added the NS record, so if the name's not there,
2719 * we have other problems. Use this name rather than calling
2722 result = dns_message_firstname(client->message, DNS_SECTION_AUTHORITY);
2723 if (result != ISC_R_SUCCESS)
2727 dns_message_currentname(client->message, DNS_SECTION_AUTHORITY,
2729 result = dns_message_findtype(rname, dns_rdatatype_ns, 0, NULL);
2730 if (result != ISC_R_SUCCESS)
2733 ISC_LIST_APPEND(rname->list, rdataset, link);
2734 ISC_LIST_APPEND(rname->list, sigrdataset, link);
2740 if (!dns_db_iszone(db))
2743 * Add the NSEC3 which proves the DS does not exist.
2745 dbuf = query_getnamebuf(client);
2748 fname = query_newname(client, dbuf, &b);
2749 dns_fixedname_init(&fixed);
2750 if (dns_rdataset_isassociated(rdataset))
2751 dns_rdataset_disassociate(rdataset);
2752 if (dns_rdataset_isassociated(sigrdataset))
2753 dns_rdataset_disassociate(sigrdataset);
2754 query_findclosestnsec3(name, db, version, client, rdataset,
2755 sigrdataset, fname, ISC_TRUE,
2756 dns_fixedname_name(&fixed));
2757 if (!dns_rdataset_isassociated(rdataset))
2759 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2760 DNS_SECTION_AUTHORITY);
2762 * Did we find the closest provable encloser instead?
2763 * If so add the nearest to the closest provable encloser.
2765 if (!dns_name_equal(name, dns_fixedname_name(&fixed))) {
2766 count = dns_name_countlabels(dns_fixedname_name(&fixed)) + 1;
2767 dns_name_getlabelsequence(name,
2768 dns_name_countlabels(name) - count,
2769 count, dns_fixedname_name(&fixed));
2770 fixfname(client, &fname, &dbuf, &b);
2771 fixrdataset(client, &rdataset);
2772 fixrdataset(client, &sigrdataset);
2773 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2775 query_findclosestnsec3(dns_fixedname_name(&fixed), db, version,
2776 client, rdataset, sigrdataset, fname,
2778 if (!dns_rdataset_isassociated(rdataset))
2780 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2781 DNS_SECTION_AUTHORITY);
2785 if (rdataset != NULL)
2786 query_putrdataset(client, &rdataset);
2787 if (sigrdataset != NULL)
2788 query_putrdataset(client, &sigrdataset);
2790 query_releasename(client, &fname);
2794 query_addwildcardproof(ns_client_t *client, dns_db_t *db,
2795 dns_dbversion_t *version, dns_name_t *name,
2796 isc_boolean_t ispositive)
2798 isc_buffer_t *dbuf, b;
2800 dns_rdataset_t *rdataset, *sigrdataset;
2801 dns_fixedname_t wfixed;
2804 unsigned int options;
2805 unsigned int olabels, nlabels, labels;
2806 isc_result_t result;
2807 dns_rdata_t rdata = DNS_RDATA_INIT;
2808 dns_rdata_nsec_t nsec;
2809 isc_boolean_t have_wname;
2811 dns_fixedname_t cfixed;
2814 CTRACE("query_addwildcardproof");
2821 * Get the NOQNAME proof then if !ispositive
2822 * get the NOWILDCARD proof.
2824 * DNS_DBFIND_NOWILD finds the NSEC records that covers the
2825 * name ignoring any wildcard. From the owner and next names
2826 * of this record you can compute which wildcard (if it exists)
2827 * will match by finding the longest common suffix of the
2828 * owner name and next names with the qname and prefixing that
2829 * with the wildcard label.
2834 * example NSEC b.example
2836 * b.example NSEC a.d.example
2838 * a.d.example NSEC g.f.example
2840 * g.f.example NSEC z.i.example
2842 * z.i.example NSEC example
2845 * a.example -> example NSEC b.example
2846 * owner common example
2847 * next common example
2849 * d.b.example -> b.example NSEC a.d.example
2850 * owner common b.example
2851 * next common example
2853 * a.f.example -> a.d.example NSEC g.f.example
2854 * owner common example
2855 * next common f.example
2857 * j.example -> z.i.example NSEC example
2858 * owner common example
2859 * next common example
2862 options = client->query.dboptions | DNS_DBFIND_NOWILD;
2863 dns_fixedname_init(&wfixed);
2864 wname = dns_fixedname_name(&wfixed);
2866 have_wname = ISC_FALSE;
2868 * We'll need some resources...
2870 dbuf = query_getnamebuf(client);
2873 fname = query_newname(client, dbuf, &b);
2874 rdataset = query_newrdataset(client);
2875 sigrdataset = query_newrdataset(client);
2876 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2879 result = dns_db_find(db, name, version, dns_rdatatype_nsec, options,
2880 0, &node, fname, rdataset, sigrdataset);
2882 dns_db_detachnode(db, &node);
2884 if (!dns_rdataset_isassociated(rdataset)) {
2886 * No NSEC proof available, return NSEC3 proofs instead.
2888 dns_fixedname_init(&cfixed);
2889 cname = dns_fixedname_name(&cfixed);
2891 * Find the closest encloser.
2893 dns_name_copy(name, cname, NULL);
2894 while (result == DNS_R_NXDOMAIN) {
2895 labels = dns_name_countlabels(cname) - 1;
2896 dns_name_split(cname, labels, NULL, cname);
2897 result = dns_db_find(db, cname, version,
2899 options, 0, NULL, fname,
2903 * Add closest (provable) encloser NSEC3.
2905 query_findclosestnsec3(cname, db, NULL, client, rdataset,
2906 sigrdataset, fname, ISC_TRUE, cname);
2907 if (!dns_rdataset_isassociated(rdataset))
2909 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2910 dbuf, DNS_SECTION_AUTHORITY);
2913 * Replace resources which were consumed by query_addrrset.
2915 if (fname == NULL) {
2916 dbuf = query_getnamebuf(client);
2919 fname = query_newname(client, dbuf, &b);
2922 if (rdataset == NULL)
2923 rdataset = query_newrdataset(client);
2924 else if (dns_rdataset_isassociated(rdataset))
2925 dns_rdataset_disassociate(rdataset);
2927 if (sigrdataset == NULL)
2928 sigrdataset = query_newrdataset(client);
2929 else if (dns_rdataset_isassociated(sigrdataset))
2930 dns_rdataset_disassociate(sigrdataset);
2932 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2935 * Add no qname proof.
2937 labels = dns_name_countlabels(cname) + 1;
2938 if (dns_name_countlabels(name) == labels)
2939 dns_name_copy(name, wname, NULL);
2941 dns_name_split(name, labels, NULL, wname);
2943 query_findclosestnsec3(wname, db, NULL, client, rdataset,
2944 sigrdataset, fname, ISC_FALSE, NULL);
2945 if (!dns_rdataset_isassociated(rdataset))
2947 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2948 dbuf, DNS_SECTION_AUTHORITY);
2954 * Replace resources which were consumed by query_addrrset.
2956 if (fname == NULL) {
2957 dbuf = query_getnamebuf(client);
2960 fname = query_newname(client, dbuf, &b);
2963 if (rdataset == NULL)
2964 rdataset = query_newrdataset(client);
2965 else if (dns_rdataset_isassociated(rdataset))
2966 dns_rdataset_disassociate(rdataset);
2968 if (sigrdataset == NULL)
2969 sigrdataset = query_newrdataset(client);
2970 else if (dns_rdataset_isassociated(sigrdataset))
2971 dns_rdataset_disassociate(sigrdataset);
2973 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2976 * Add the no wildcard proof.
2978 result = dns_name_concatenate(dns_wildcardname,
2979 cname, wname, NULL);
2980 if (result != ISC_R_SUCCESS)
2983 query_findclosestnsec3(wname, db, NULL, client, rdataset,
2984 sigrdataset, fname, ISC_FALSE, NULL);
2985 if (!dns_rdataset_isassociated(rdataset))
2987 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2988 dbuf, DNS_SECTION_AUTHORITY);
2991 } else if (result == DNS_R_NXDOMAIN) {
2993 result = dns_rdataset_first(rdataset);
2994 if (result == ISC_R_SUCCESS) {
2995 dns_rdataset_current(rdataset, &rdata);
2996 result = dns_rdata_tostruct(&rdata, &nsec, NULL);
2998 if (result == ISC_R_SUCCESS) {
2999 (void)dns_name_fullcompare(name, fname, &order,
3001 (void)dns_name_fullcompare(name, &nsec.next, &order,
3004 * Check for a pathological condition created when
3005 * serving some malformed signed zones and bail out.
3007 if (dns_name_countlabels(name) == nlabels)
3010 if (olabels > nlabels)
3011 dns_name_split(name, olabels, NULL, wname);
3013 dns_name_split(name, nlabels, NULL, wname);
3014 result = dns_name_concatenate(dns_wildcardname,
3015 wname, wname, NULL);
3016 if (result == ISC_R_SUCCESS)
3017 have_wname = ISC_TRUE;
3018 dns_rdata_freestruct(&nsec);
3020 query_addrrset(client, &fname, &rdataset, &sigrdataset,
3021 dbuf, DNS_SECTION_AUTHORITY);
3023 if (rdataset != NULL)
3024 query_putrdataset(client, &rdataset);
3025 if (sigrdataset != NULL)
3026 query_putrdataset(client, &sigrdataset);
3028 query_releasename(client, &fname);
3030 ispositive = ISC_TRUE; /* prevent loop */
3031 if (!dns_name_equal(name, wname)) {
3037 if (rdataset != NULL)
3038 query_putrdataset(client, &rdataset);
3039 if (sigrdataset != NULL)
3040 query_putrdataset(client, &sigrdataset);
3042 query_releasename(client, &fname);
3046 query_addnxrrsetnsec(ns_client_t *client, dns_db_t *db,
3047 dns_dbversion_t *version, dns_name_t **namep,
3048 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp)
3051 dns_rdataset_t *sigrdataset;
3052 dns_rdata_t sigrdata;
3053 dns_rdata_rrsig_t sig;
3054 unsigned int labels;
3055 isc_buffer_t *dbuf, b;
3057 isc_result_t result;
3060 if ((name->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
3061 query_addrrset(client, namep, rdatasetp, sigrdatasetp,
3062 NULL, DNS_SECTION_AUTHORITY);
3066 if (sigrdatasetp == NULL)
3069 sigrdataset = *sigrdatasetp;
3070 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
3072 result = dns_rdataset_first(sigrdataset);
3073 if (result != ISC_R_SUCCESS)
3075 dns_rdata_init(&sigrdata);
3076 dns_rdataset_current(sigrdataset, &sigrdata);
3077 result = dns_rdata_tostruct(&sigrdata, &sig, NULL);
3078 if (result != ISC_R_SUCCESS)
3081 labels = dns_name_countlabels(name);
3082 if ((unsigned int)sig.labels + 1 >= labels)
3086 query_addwildcardproof(client, db, version, client->query.qname,
3090 * We'll need some resources...
3092 dbuf = query_getnamebuf(client);
3095 fname = query_newname(client, dbuf, &b);
3098 dns_name_split(name, sig.labels + 1, NULL, fname);
3099 /* This will succeed, since we've stripped labels. */
3100 RUNTIME_CHECK(dns_name_concatenate(dns_wildcardname, fname, fname,
3101 NULL) == ISC_R_SUCCESS);
3102 query_addrrset(client, &fname, rdatasetp, sigrdatasetp,
3103 dbuf, DNS_SECTION_AUTHORITY);
3107 query_resume(isc_task_t *task, isc_event_t *event) {
3108 dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
3110 ns_client_t *client;
3111 isc_boolean_t fetch_canceled, client_shuttingdown;
3112 isc_result_t result;
3113 isc_logcategory_t *logcategory = NS_LOGCATEGORY_QUERY_EERRORS;
3117 * Resume a query after recursion.
3122 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
3123 client = devent->ev_arg;
3124 REQUIRE(NS_CLIENT_VALID(client));
3125 REQUIRE(task == client->task);
3126 REQUIRE(RECURSING(client));
3128 LOCK(&client->query.fetchlock);
3129 if (client->query.fetch != NULL) {
3131 * This is the fetch we've been waiting for.
3133 INSIST(devent->fetch == client->query.fetch);
3134 client->query.fetch = NULL;
3135 fetch_canceled = ISC_FALSE;
3137 * Update client->now.
3139 isc_stdtime_get(&client->now);
3142 * This is a fetch completion event for a canceled fetch.
3143 * Clean up and don't resume the find.
3145 fetch_canceled = ISC_TRUE;
3147 UNLOCK(&client->query.fetchlock);
3148 INSIST(client->query.fetch == NULL);
3150 client->query.attributes &= ~NS_QUERYATTR_RECURSING;
3151 fetch = devent->fetch;
3152 devent->fetch = NULL;
3155 * If this client is shutting down, or this transaction
3156 * has timed out, do not resume the find.
3158 client_shuttingdown = ns_client_shuttingdown(client);
3159 if (fetch_canceled || client_shuttingdown) {
3160 if (devent->node != NULL)
3161 dns_db_detachnode(devent->db, &devent->node);
3162 if (devent->db != NULL)
3163 dns_db_detach(&devent->db);
3164 query_putrdataset(client, &devent->rdataset);
3165 if (devent->sigrdataset != NULL)
3166 query_putrdataset(client, &devent->sigrdataset);
3167 isc_event_free(&event);
3169 query_error(client, DNS_R_SERVFAIL, __LINE__);
3171 query_next(client, ISC_R_CANCELED);
3173 * This may destroy the client.
3175 ns_client_detach(&client);
3177 result = query_find(client, devent, 0);
3178 if (result != ISC_R_SUCCESS) {
3179 if (result == DNS_R_SERVFAIL)
3180 errorloglevel = ISC_LOG_DEBUG(2);
3182 errorloglevel = ISC_LOG_DEBUG(4);
3183 if (isc_log_wouldlog(ns_g_lctx, errorloglevel)) {
3184 dns_resolver_logfetch(fetch, ns_g_lctx,
3187 errorloglevel, ISC_FALSE);
3192 dns_resolver_destroyfetch(&fetch);
3196 query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qdomain,
3197 dns_rdataset_t *nameservers, isc_boolean_t resuming)
3199 isc_result_t result;
3200 dns_rdataset_t *rdataset, *sigrdataset;
3201 isc_sockaddr_t *peeraddr;
3204 inc_stats(client, dns_nsstatscounter_recursion);
3207 * We are about to recurse, which means that this client will
3208 * be unavailable for serving new requests for an indeterminate
3209 * amount of time. If this client is currently responsible
3210 * for handling incoming queries, set up a new client
3211 * object to handle them while we are waiting for a
3212 * response. There is no need to replace TCP clients
3213 * because those have already been replaced when the
3214 * connection was accepted (if allowed by the TCP quota).
3216 if (client->recursionquota == NULL) {
3217 result = isc_quota_attach(&ns_g_server->recursionquota,
3218 &client->recursionquota);
3219 if (result == ISC_R_SOFTQUOTA) {
3220 static isc_stdtime_t last = 0;
3222 isc_stdtime_get(&now);
3225 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3228 "recursive-clients soft limit "
3229 "exceeded, aborting oldest query");
3231 ns_client_killoldestquery(client);
3232 result = ISC_R_SUCCESS;
3233 } else if (result == ISC_R_QUOTA) {
3234 static isc_stdtime_t last = 0;
3236 isc_stdtime_get(&now);
3239 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3242 "no more recursive clients: %s",
3243 isc_result_totext(result));
3245 ns_client_killoldestquery(client);
3247 if (result == ISC_R_SUCCESS && !client->mortal &&
3248 (client->attributes & NS_CLIENTATTR_TCP) == 0) {
3249 result = ns_client_replace(client);
3250 if (result != ISC_R_SUCCESS) {
3251 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3254 "ns_client_replace() failed: %s",
3255 isc_result_totext(result));
3256 isc_quota_detach(&client->recursionquota);
3259 if (result != ISC_R_SUCCESS)
3261 ns_client_recursing(client);
3265 * Invoke the resolver.
3267 REQUIRE(nameservers == NULL || nameservers->type == dns_rdatatype_ns);
3268 REQUIRE(client->query.fetch == NULL);
3270 rdataset = query_newrdataset(client);
3271 if (rdataset == NULL)
3272 return (ISC_R_NOMEMORY);
3273 if (WANTDNSSEC(client)) {
3274 sigrdataset = query_newrdataset(client);
3275 if (sigrdataset == NULL) {
3276 query_putrdataset(client, &rdataset);
3277 return (ISC_R_NOMEMORY);
3282 if (client->query.timerset == ISC_FALSE)
3283 ns_client_settimeout(client, 60);
3284 if ((client->attributes & NS_CLIENTATTR_TCP) == 0)
3285 peeraddr = &client->peeraddr;
3288 result = dns_resolver_createfetch2(client->view->resolver,
3289 client->query.qname,
3290 qtype, qdomain, nameservers,
3291 NULL, peeraddr, client->message->id,
3292 client->query.fetchoptions,
3294 query_resume, client,
3295 rdataset, sigrdataset,
3296 &client->query.fetch);
3298 if (result == ISC_R_SUCCESS) {
3300 * Record that we're waiting for an event. A client which
3301 * is shutting down will not be destroyed until all the
3302 * events have been received.
3305 query_putrdataset(client, &rdataset);
3306 if (sigrdataset != NULL)
3307 query_putrdataset(client, &sigrdataset);
3313 #define MAX_RESTARTS 16
3315 #define QUERY_ERROR(r) \
3318 want_restart = ISC_FALSE; \
3322 #define RECURSE_ERROR(r) \
3324 if ((r) == DNS_R_DUPLICATE || (r) == DNS_R_DROP) \
3327 QUERY_ERROR(DNS_R_SERVFAIL); \
3331 * Extract a network address from the RDATA of an A or AAAA
3336 * ISC_R_NOTIMPLEMENTED The rdata is not a known address type.
3339 rdata_tonetaddr(const dns_rdata_t *rdata, isc_netaddr_t *netaddr) {
3341 struct in6_addr in6a;
3343 switch (rdata->type) {
3344 case dns_rdatatype_a:
3345 INSIST(rdata->length == 4);
3346 memcpy(&ina.s_addr, rdata->data, 4);
3347 isc_netaddr_fromin(netaddr, &ina);
3348 return (ISC_R_SUCCESS);
3349 case dns_rdatatype_aaaa:
3350 INSIST(rdata->length == 16);
3351 memcpy(in6a.s6_addr, rdata->data, 16);
3352 isc_netaddr_fromin6(netaddr, &in6a);
3353 return (ISC_R_SUCCESS);
3355 return (ISC_R_NOTIMPLEMENTED);
3360 * Find the sort order of 'rdata' in the topology-like
3361 * ACL forming the second element in a 2-element top-level
3362 * sortlist statement.
3365 query_sortlist_order_2element(const dns_rdata_t *rdata, const void *arg) {
3366 isc_netaddr_t netaddr;
3368 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3370 return (ns_sortlist_addrorder2(&netaddr, arg));
3374 * Find the sort order of 'rdata' in the matching element
3375 * of a 1-element top-level sortlist statement.
3378 query_sortlist_order_1element(const dns_rdata_t *rdata, const void *arg) {
3379 isc_netaddr_t netaddr;
3381 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3383 return (ns_sortlist_addrorder1(&netaddr, arg));
3387 * Find the sortlist statement that applies to 'client' and set up
3388 * the sortlist info in in client->message appropriately.
3391 setup_query_sortlist(ns_client_t *client) {
3392 isc_netaddr_t netaddr;
3393 dns_rdatasetorderfunc_t order = NULL;
3394 const void *order_arg = NULL;
3396 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
3397 switch (ns_sortlist_setup(client->view->sortlist,
3398 &netaddr, &order_arg)) {
3399 case NS_SORTLISTTYPE_1ELEMENT:
3400 order = query_sortlist_order_1element;
3402 case NS_SORTLISTTYPE_2ELEMENT:
3403 order = query_sortlist_order_2element;
3405 case NS_SORTLISTTYPE_NONE:
3412 dns_message_setsortorder(client->message, order, order_arg);
3416 query_addnoqnameproof(ns_client_t *client, dns_rdataset_t *rdataset) {
3417 isc_buffer_t *dbuf, b;
3419 dns_rdataset_t *neg, *negsig;
3420 isc_result_t result = ISC_R_NOMEMORY;
3422 CTRACE("query_addnoqnameproof");
3428 dbuf = query_getnamebuf(client);
3431 fname = query_newname(client, dbuf, &b);
3432 neg = query_newrdataset(client);
3433 negsig = query_newrdataset(client);
3434 if (fname == NULL || neg == NULL || negsig == NULL)
3437 result = dns_rdataset_getnoqname(rdataset, fname, neg, negsig);
3438 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3440 query_addrrset(client, &fname, &neg, &negsig, dbuf,
3441 DNS_SECTION_AUTHORITY);
3443 if ((rdataset->attributes & DNS_RDATASETATTR_CLOSEST) == 0)
3446 if (fname == NULL) {
3447 dbuf = query_getnamebuf(client);
3450 fname = query_newname(client, dbuf, &b);
3453 neg = query_newrdataset(client);
3454 else if (dns_rdataset_isassociated(neg))
3455 dns_rdataset_disassociate(neg);
3457 negsig = query_newrdataset(client);
3458 else if (dns_rdataset_isassociated(negsig))
3459 dns_rdataset_disassociate(negsig);
3460 if (fname == NULL || neg == NULL || negsig == NULL)
3462 result = dns_rdataset_getclosest(rdataset, fname, neg, negsig);
3463 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3465 query_addrrset(client, &fname, &neg, &negsig, dbuf,
3466 DNS_SECTION_AUTHORITY);
3470 query_putrdataset(client, &neg);
3472 query_putrdataset(client, &negsig);
3474 query_releasename(client, &fname);
3478 answer_in_glue(ns_client_t *client, dns_rdatatype_t qtype) {
3481 dns_section_t section = DNS_SECTION_ADDITIONAL;
3482 dns_rdataset_t *rdataset = NULL;
3484 msg = client->message;
3485 for (name = ISC_LIST_HEAD(msg->sections[section]);
3487 name = ISC_LIST_NEXT(name, link))
3488 if (dns_name_equal(name, client->query.qname)) {
3489 for (rdataset = ISC_LIST_HEAD(name->list);
3491 rdataset = ISC_LIST_NEXT(rdataset, link))
3492 if (rdataset->type == qtype)
3496 if (rdataset != NULL) {
3497 ISC_LIST_UNLINK(msg->sections[section], name, link);
3498 ISC_LIST_PREPEND(msg->sections[section], name, link);
3499 ISC_LIST_UNLINK(name->list, rdataset, link);
3500 ISC_LIST_PREPEND(name->list, rdataset, link);
3501 rdataset->attributes |= DNS_RDATASETATTR_REQUIREDGLUE;
3505 #define NS_NAME_INIT(A,B) \
3508 A, sizeof(A), sizeof(B), \
3509 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE, \
3510 B, NULL, { (void *)-1, (void *)-1}, \
3514 static unsigned char inaddr10_offsets[] = { 0, 3, 11, 16 };
3515 static unsigned char inaddr172_offsets[] = { 0, 3, 7, 15, 20 };
3516 static unsigned char inaddr192_offsets[] = { 0, 4, 8, 16, 21 };
3518 static unsigned char inaddr10[] = "\00210\007IN-ADDR\004ARPA";
3520 static unsigned char inaddr16172[] = "\00216\003172\007IN-ADDR\004ARPA";
3521 static unsigned char inaddr17172[] = "\00217\003172\007IN-ADDR\004ARPA";
3522 static unsigned char inaddr18172[] = "\00218\003172\007IN-ADDR\004ARPA";
3523 static unsigned char inaddr19172[] = "\00219\003172\007IN-ADDR\004ARPA";
3524 static unsigned char inaddr20172[] = "\00220\003172\007IN-ADDR\004ARPA";
3525 static unsigned char inaddr21172[] = "\00221\003172\007IN-ADDR\004ARPA";
3526 static unsigned char inaddr22172[] = "\00222\003172\007IN-ADDR\004ARPA";
3527 static unsigned char inaddr23172[] = "\00223\003172\007IN-ADDR\004ARPA";
3528 static unsigned char inaddr24172[] = "\00224\003172\007IN-ADDR\004ARPA";
3529 static unsigned char inaddr25172[] = "\00225\003172\007IN-ADDR\004ARPA";
3530 static unsigned char inaddr26172[] = "\00226\003172\007IN-ADDR\004ARPA";
3531 static unsigned char inaddr27172[] = "\00227\003172\007IN-ADDR\004ARPA";
3532 static unsigned char inaddr28172[] = "\00228\003172\007IN-ADDR\004ARPA";
3533 static unsigned char inaddr29172[] = "\00229\003172\007IN-ADDR\004ARPA";
3534 static unsigned char inaddr30172[] = "\00230\003172\007IN-ADDR\004ARPA";
3535 static unsigned char inaddr31172[] = "\00231\003172\007IN-ADDR\004ARPA";
3537 static unsigned char inaddr168192[] = "\003168\003192\007IN-ADDR\004ARPA";
3539 static dns_name_t rfc1918names[] = {
3540 NS_NAME_INIT(inaddr10, inaddr10_offsets),
3541 NS_NAME_INIT(inaddr16172, inaddr172_offsets),
3542 NS_NAME_INIT(inaddr17172, inaddr172_offsets),
3543 NS_NAME_INIT(inaddr18172, inaddr172_offsets),
3544 NS_NAME_INIT(inaddr19172, inaddr172_offsets),
3545 NS_NAME_INIT(inaddr20172, inaddr172_offsets),
3546 NS_NAME_INIT(inaddr21172, inaddr172_offsets),
3547 NS_NAME_INIT(inaddr22172, inaddr172_offsets),
3548 NS_NAME_INIT(inaddr23172, inaddr172_offsets),
3549 NS_NAME_INIT(inaddr24172, inaddr172_offsets),
3550 NS_NAME_INIT(inaddr25172, inaddr172_offsets),
3551 NS_NAME_INIT(inaddr26172, inaddr172_offsets),
3552 NS_NAME_INIT(inaddr27172, inaddr172_offsets),
3553 NS_NAME_INIT(inaddr28172, inaddr172_offsets),
3554 NS_NAME_INIT(inaddr29172, inaddr172_offsets),
3555 NS_NAME_INIT(inaddr30172, inaddr172_offsets),
3556 NS_NAME_INIT(inaddr31172, inaddr172_offsets),
3557 NS_NAME_INIT(inaddr168192, inaddr192_offsets)
3561 static unsigned char prisoner_data[] = "\010prisoner\004iana\003org";
3562 static unsigned char hostmaster_data[] = "\012hostmaster\014root-servers\003org";
3564 static unsigned char prisoner_offsets[] = { 0, 9, 14, 18 };
3565 static unsigned char hostmaster_offsets[] = { 0, 11, 24, 28 };
3567 static dns_name_t prisoner = NS_NAME_INIT(prisoner_data, prisoner_offsets);
3568 static dns_name_t hostmaster = NS_NAME_INIT(hostmaster_data, hostmaster_offsets);
3571 warn_rfc1918(ns_client_t *client, dns_name_t *fname, dns_rdataset_t *rdataset) {
3573 dns_rdata_t rdata = DNS_RDATA_INIT;
3574 dns_rdata_soa_t soa;
3575 dns_rdataset_t found;
3576 isc_result_t result;
3578 for (i = 0; i < (sizeof(rfc1918names)/sizeof(*rfc1918names)); i++) {
3579 if (dns_name_issubdomain(fname, &rfc1918names[i])) {
3580 dns_rdataset_init(&found);
3581 result = dns_ncache_getrdataset(rdataset,
3585 if (result != ISC_R_SUCCESS)
3588 result = dns_rdataset_first(&found);
3589 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3590 dns_rdataset_current(&found, &rdata);
3591 result = dns_rdata_tostruct(&rdata, &soa, NULL);
3592 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3593 if (dns_name_equal(&soa.origin, &prisoner) &&
3594 dns_name_equal(&soa.contact, &hostmaster)) {
3595 char buf[DNS_NAME_FORMATSIZE];
3596 dns_name_format(fname, buf, sizeof(buf));
3597 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3600 "RFC 1918 response from "
3601 "Internet for %s", buf);
3603 dns_rdataset_disassociate(&found);
3610 query_findclosestnsec3(dns_name_t *qname, dns_db_t *db,
3611 dns_dbversion_t *version, ns_client_t *client,
3612 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3613 dns_name_t *fname, isc_boolean_t exact,
3616 unsigned char salt[256];
3618 isc_uint16_t iterations;
3619 isc_result_t result;
3620 unsigned int dboptions;
3621 dns_fixedname_t fixed;
3626 dns_rdata_nsec3_t nsec3;
3627 dns_rdata_t rdata = DNS_RDATA_INIT;
3628 isc_boolean_t optout;
3630 salt_length = sizeof(salt);
3631 result = dns_db_getnsec3parameters(db, version, &hash, NULL,
3632 &iterations, salt, &salt_length);
3633 if (result != ISC_R_SUCCESS)
3636 dns_name_init(&name, NULL);
3637 dns_name_clone(qname, &name);
3640 * Map unknown algorithm to known value.
3642 if (hash == DNS_NSEC3_UNKNOWNALG)
3646 dns_fixedname_init(&fixed);
3647 result = dns_nsec3_hashname(&fixed, NULL, NULL, &name,
3648 dns_db_origin(db), hash,
3649 iterations, salt, salt_length);
3650 if (result != ISC_R_SUCCESS)
3653 dboptions = client->query.dboptions | DNS_DBFIND_FORCENSEC3;
3654 result = dns_db_find(db, dns_fixedname_name(&fixed), version,
3655 dns_rdatatype_nsec3, dboptions, client->now,
3656 NULL, fname, rdataset, sigrdataset);
3658 if (result == DNS_R_NXDOMAIN) {
3659 if (!dns_rdataset_isassociated(rdataset)) {
3662 result = dns_rdataset_first(rdataset);
3663 INSIST(result == ISC_R_SUCCESS);
3664 dns_rdataset_current(rdataset, &rdata);
3665 dns_rdata_tostruct(&rdata, &nsec3, NULL);
3666 dns_rdata_reset(&rdata);
3667 optout = ISC_TF((nsec3.flags & DNS_NSEC3FLAG_OPTOUT) != 0);
3668 if (found != NULL && optout &&
3669 dns_name_fullcompare(&name, dns_db_origin(db), &order,
3670 &count) == dns_namereln_subdomain) {
3671 dns_rdataset_disassociate(rdataset);
3672 if (dns_rdataset_isassociated(sigrdataset))
3673 dns_rdataset_disassociate(sigrdataset);
3674 count = dns_name_countlabels(&name) - 1;
3675 dns_name_getlabelsequence(&name, 1, count, &name);
3676 ns_client_log(client, DNS_LOGCATEGORY_DNSSEC,
3677 NS_LOGMODULE_QUERY, ISC_LOG_DEBUG(3),
3678 "looking for closest provable encloser");
3682 ns_client_log(client, DNS_LOGCATEGORY_DNSSEC,
3683 NS_LOGMODULE_QUERY, ISC_LOG_WARNING,
3684 "expected a exact match NSEC3, got "
3685 "a covering record");
3687 } else if (result != ISC_R_SUCCESS) {
3690 ns_client_log(client, DNS_LOGCATEGORY_DNSSEC,
3691 NS_LOGMODULE_QUERY, ISC_LOG_WARNING,
3692 "expected covering NSEC3, got an exact match");
3694 dns_name_copy(&name, found, NULL);
3699 * Do the bulk of query processing for the current query of 'client'.
3700 * If 'event' is non-NULL, we are returning from recursion and 'qtype'
3701 * is ignored. Otherwise, 'qtype' is the query type.
3704 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
3708 dns_rdatatype_t type;
3709 dns_name_t *fname, *zfname, *tname, *prefix;
3710 dns_rdataset_t *rdataset, *trdataset;
3711 dns_rdataset_t *sigrdataset, *zrdataset, *zsigrdataset;
3712 dns_rdataset_t **sigrdatasetp;
3713 dns_rdata_t rdata = DNS_RDATA_INIT;
3714 dns_rdatasetiter_t *rdsiter;
3715 isc_boolean_t want_restart, authoritative, is_zone, need_wildcardproof;
3716 unsigned int n, nlabels;
3717 dns_namereln_t namereln;
3721 isc_result_t result, eresult;
3722 dns_fixedname_t fixed;
3723 dns_fixedname_t wildcardname;
3724 dns_dbversion_t *version, *zversion;
3726 dns_rdata_cname_t cname;
3727 dns_rdata_dname_t dname;
3728 unsigned int options;
3729 isc_boolean_t empty_wild;
3730 dns_rdataset_t *noqname;
3731 isc_boolean_t resuming;
3734 CTRACE("query_find");
3737 * One-time initialization.
3739 * It's especially important to initialize anything that the cleanup
3740 * code might cleanup.
3743 eresult = ISC_R_SUCCESS;
3749 zsigrdataset = NULL;
3756 need_wildcardproof = ISC_FALSE;
3757 empty_wild = ISC_FALSE;
3759 resuming = ISC_FALSE;
3760 is_zone = ISC_FALSE;
3762 if (event != NULL) {
3764 * We're returning from recursion. Restore the query context
3768 want_restart = ISC_FALSE;
3769 authoritative = ISC_FALSE;
3771 qtype = event->qtype;
3772 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3773 type = dns_rdatatype_any;
3778 rdataset = event->rdataset;
3779 sigrdataset = event->sigrdataset;
3782 * We'll need some resources...
3784 dbuf = query_getnamebuf(client);
3786 QUERY_ERROR(DNS_R_SERVFAIL);
3789 fname = query_newname(client, dbuf, &b);
3790 if (fname == NULL) {
3791 QUERY_ERROR(DNS_R_SERVFAIL);
3794 tname = dns_fixedname_name(&event->foundname);
3795 result = dns_name_copy(tname, fname, NULL);
3796 if (result != ISC_R_SUCCESS) {
3797 QUERY_ERROR(DNS_R_SERVFAIL);
3801 result = event->result;
3802 resuming = ISC_TRUE;
3808 * Not returning from recursion.
3812 * If it's a SIG query, we'll iterate the node.
3814 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3815 type = dns_rdatatype_any;
3820 CTRACE("query_find: restart");
3821 want_restart = ISC_FALSE;
3822 authoritative = ISC_FALSE;
3824 need_wildcardproof = ISC_FALSE;
3826 if (client->view->checknames &&
3827 !dns_rdata_checkowner(client->query.qname,
3828 client->message->rdclass,
3829 qtype, ISC_FALSE)) {
3830 char namebuf[DNS_NAME_FORMATSIZE];
3831 char typename[DNS_RDATATYPE_FORMATSIZE];
3832 char classname[DNS_RDATACLASS_FORMATSIZE];
3834 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
3835 dns_rdatatype_format(qtype, typename, sizeof(typename));
3836 dns_rdataclass_format(client->message->rdclass, classname,
3838 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3839 NS_LOGMODULE_QUERY, ISC_LOG_ERROR,
3840 "check-names failure %s/%s/%s", namebuf,
3841 typename, classname);
3842 QUERY_ERROR(DNS_R_REFUSED);
3847 * First we must find the right database.
3849 options &= DNS_GETDB_NOLOG; /* Preserve DNS_GETDB_NOLOG. */
3850 if (dns_rdatatype_atparent(qtype) &&
3851 !dns_name_equal(client->query.qname, dns_rootname))
3852 options |= DNS_GETDB_NOEXACT;
3853 result = query_getdb(client, client->query.qname, qtype, options,
3854 &zone, &db, &version, &is_zone);
3855 if ((result != ISC_R_SUCCESS || !is_zone) && !RECURSIONOK(client) &&
3856 (options & DNS_GETDB_NOEXACT) != 0 && qtype == dns_rdatatype_ds) {
3858 * Look to see if we are authoritative for the
3859 * child zone if the query type is DS.
3861 dns_db_t *tdb = NULL;
3862 dns_zone_t *tzone = NULL;
3863 dns_dbversion_t *tversion = NULL;
3864 isc_result_t tresult;
3866 tresult = query_getzonedb(client, client->query.qname, qtype,
3867 DNS_GETDB_PARTIAL, &tzone, &tdb,
3869 if (tresult == ISC_R_SUCCESS) {
3870 options &= ~DNS_GETDB_NOEXACT;
3871 query_putrdataset(client, &rdataset);
3875 dns_zone_detach(&zone);
3880 result = ISC_R_SUCCESS;
3883 dns_db_detach(&tdb);
3885 dns_zone_detach(&tzone);
3888 if (result != ISC_R_SUCCESS) {
3889 if (result == DNS_R_REFUSED) {
3890 if (WANTRECURSION(client)) {
3892 dns_nsstatscounter_recurserej);
3894 inc_stats(client, dns_nsstatscounter_authrej);
3895 if (!PARTIALANSWER(client))
3896 QUERY_ERROR(DNS_R_REFUSED);
3898 QUERY_ERROR(DNS_R_SERVFAIL);
3903 authoritative = ISC_TRUE;
3905 if (event == NULL && client->query.restarts == 0) {
3910 * if is_zone = true, zone = NULL then this is
3911 * a DLZ zone. Don't attempt to attach zone.
3914 dns_zone_attach(zone, &client->query.authzone);
3918 dns_db_attach(db, &client->query.authdb);
3920 client->query.authdbset = ISC_TRUE;
3924 CTRACE("query_find: db_find");
3926 * We'll need some resources...
3928 dbuf = query_getnamebuf(client);
3930 QUERY_ERROR(DNS_R_SERVFAIL);
3933 fname = query_newname(client, dbuf, &b);
3934 rdataset = query_newrdataset(client);
3935 if (fname == NULL || rdataset == NULL) {
3936 QUERY_ERROR(DNS_R_SERVFAIL);
3939 if (WANTDNSSEC(client) && (!is_zone || dns_db_issecure(db))) {
3940 sigrdataset = query_newrdataset(client);
3941 if (sigrdataset == NULL) {
3942 QUERY_ERROR(DNS_R_SERVFAIL);
3948 * Now look for an answer in the database.
3950 result = dns_db_find(db, client->query.qname, version, type,
3951 client->query.dboptions, client->now,
3952 &node, fname, rdataset, sigrdataset);
3955 CTRACE("query_find: resume");
3959 * This case is handled in the main line below.
3965 * These cases are handled in the main line below.
3968 authoritative = ISC_FALSE;
3970 case ISC_R_NOTFOUND:
3972 * The cache doesn't even have the root NS. Get them from
3979 if (client->view->hints == NULL) {
3980 /* We have no hints. */
3981 result = ISC_R_FAILURE;
3983 dns_db_attach(client->view->hints, &db);
3984 result = dns_db_find(db, dns_rootname,
3985 NULL, dns_rdatatype_ns,
3986 0, client->now, &node, fname,
3987 rdataset, sigrdataset);
3989 if (result != ISC_R_SUCCESS) {
3991 * Nonsensical root hints may require cleanup.
3993 if (dns_rdataset_isassociated(rdataset))
3994 dns_rdataset_disassociate(rdataset);
3995 if (sigrdataset != NULL &&
3996 dns_rdataset_isassociated(sigrdataset))
3997 dns_rdataset_disassociate(sigrdataset);
3999 dns_db_detachnode(db, &node);
4002 * We don't have any root server hints, but
4003 * we may have working forwarders, so try to
4006 if (RECURSIONOK(client)) {
4007 result = query_recurse(client, qtype,
4008 NULL, NULL, resuming);
4009 if (result == ISC_R_SUCCESS)
4010 client->query.attributes |=
4011 NS_QUERYATTR_RECURSING;
4013 RECURSE_ERROR(result);
4016 /* Unable to give root server referral. */
4017 QUERY_ERROR(DNS_R_SERVFAIL);
4022 * XXXRTH We should trigger root server priming here.
4025 case DNS_R_DELEGATION:
4026 authoritative = ISC_FALSE;
4029 * Look to see if we are authoritative for the
4030 * child zone if the query type is DS.
4032 if (!RECURSIONOK(client) &&
4033 (options & DNS_GETDB_NOEXACT) != 0 &&
4034 qtype == dns_rdatatype_ds) {
4035 dns_db_t *tdb = NULL;
4036 dns_zone_t *tzone = NULL;
4037 dns_dbversion_t *tversion = NULL;
4038 result = query_getzonedb(client,
4039 client->query.qname,
4044 if (result == ISC_R_SUCCESS) {
4045 options &= ~DNS_GETDB_NOEXACT;
4046 query_putrdataset(client, &rdataset);
4047 if (sigrdataset != NULL)
4048 query_putrdataset(client,
4051 query_releasename(client,
4054 dns_db_detachnode(db, &node);
4058 dns_zone_detach(&zone);
4062 authoritative = ISC_TRUE;
4066 dns_db_detach(&tdb);
4068 dns_zone_detach(&tzone);
4071 * We're authoritative for an ancestor of QNAME.
4073 if (!USECACHE(client) || !RECURSIONOK(client)) {
4074 dns_fixedname_t fixed;
4076 dns_fixedname_init(&fixed);
4077 dns_name_copy(fname,
4078 dns_fixedname_name(&fixed), NULL);
4081 * If we don't have a cache, this is the best
4084 * If the client is making a nonrecursive
4085 * query we always give out the authoritative
4086 * delegation. This way even if we get
4087 * junk in our cache, we won't fail in our
4088 * role as the delegating authority if another
4089 * nameserver asks us about a delegated
4092 * We enable the retrieval of glue for this
4093 * database by setting client->query.gluedb.
4095 client->query.gluedb = db;
4096 client->query.isreferral = ISC_TRUE;
4098 * We must ensure NOADDITIONAL is off,
4099 * because the generation of
4100 * additional data is required in
4103 client->query.attributes &=
4104 ~NS_QUERYATTR_NOADDITIONAL;
4105 if (sigrdataset != NULL)
4106 sigrdatasetp = &sigrdataset;
4108 sigrdatasetp = NULL;
4109 query_addrrset(client, &fname,
4110 &rdataset, sigrdatasetp,
4111 dbuf, DNS_SECTION_AUTHORITY);
4112 client->query.gluedb = NULL;
4113 if (WANTDNSSEC(client))
4114 query_addds(client, db, node, version,
4115 dns_fixedname_name(&fixed));
4118 * We might have a better answer or delegation
4119 * in the cache. We'll remember the current
4120 * values of fname, rdataset, and sigrdataset.
4121 * We'll then go looking for QNAME in the
4122 * cache. If we find something better, we'll
4125 query_keepname(client, fname, dbuf);
4129 zrdataset = rdataset;
4131 zsigrdataset = sigrdataset;
4133 dns_db_detachnode(db, &node);
4137 dns_db_attach(client->view->cachedb, &db);
4138 is_zone = ISC_FALSE;
4142 if (zfname != NULL &&
4143 !dns_name_issubdomain(fname, zfname)) {
4145 * We've already got a delegation from
4146 * authoritative data, and it is better
4147 * than what we found in the cache. Use
4148 * it instead of the cache delegation.
4150 query_releasename(client, &fname);
4154 * We've already done query_keepname() on
4155 * zfname, so we must set dbuf to NULL to
4156 * prevent query_addrrset() from trying to
4157 * call query_keepname() again.
4160 query_putrdataset(client, &rdataset);
4161 if (sigrdataset != NULL)
4162 query_putrdataset(client,
4164 rdataset = zrdataset;
4166 sigrdataset = zsigrdataset;
4167 zsigrdataset = NULL;
4171 * We don't clean up zdb here because we
4172 * may still need it. It will get cleaned
4173 * up by the main cleanup code.
4177 if (RECURSIONOK(client)) {
4181 if (dns_rdatatype_atparent(type))
4182 result = query_recurse(client, qtype,
4186 result = query_recurse(client, qtype,
4189 if (result == ISC_R_SUCCESS)
4190 client->query.attributes |=
4191 NS_QUERYATTR_RECURSING;
4193 RECURSE_ERROR(result);
4195 dns_fixedname_t fixed;
4197 dns_fixedname_init(&fixed);
4198 dns_name_copy(fname,
4199 dns_fixedname_name(&fixed), NULL);
4201 * This is the best answer.
4203 client->query.attributes |=
4204 NS_QUERYATTR_CACHEGLUEOK;
4205 client->query.gluedb = zdb;
4206 client->query.isreferral = ISC_TRUE;
4208 * We must ensure NOADDITIONAL is off,
4209 * because the generation of
4210 * additional data is required in
4213 client->query.attributes &=
4214 ~NS_QUERYATTR_NOADDITIONAL;
4215 if (sigrdataset != NULL)
4216 sigrdatasetp = &sigrdataset;
4218 sigrdatasetp = NULL;
4219 query_addrrset(client, &fname,
4220 &rdataset, sigrdatasetp,
4221 dbuf, DNS_SECTION_AUTHORITY);
4222 client->query.gluedb = NULL;
4223 client->query.attributes &=
4224 ~NS_QUERYATTR_CACHEGLUEOK;
4225 if (WANTDNSSEC(client))
4226 query_addds(client, db, node, version,
4227 dns_fixedname_name(&fixed));
4231 case DNS_R_EMPTYNAME:
4232 result = DNS_R_NXRRSET;
4237 * Look for a NSEC3 record if we don't have a NSEC record.
4239 if (!dns_rdataset_isassociated(rdataset) &&
4240 WANTDNSSEC(client)) {
4241 if ((fname->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
4245 dns_fixedname_init(&fixed);
4246 found = dns_fixedname_name(&fixed);
4247 qname = client->query.qname;
4249 query_findclosestnsec3(qname, db, version,
4254 * Did we find the closest provable encloser
4255 * instead? If so add the nearest to the
4256 * closest provable encloser.
4259 dns_rdataset_isassociated(rdataset) &&
4260 !dns_name_equal(qname, found))
4266 * Add the closest provable encloser.
4268 query_addrrset(client, &fname,
4269 &rdataset, &sigrdataset,
4271 DNS_SECTION_AUTHORITY);
4273 count = dns_name_countlabels(found)
4275 skip = dns_name_countlabels(qname) -
4277 dns_name_getlabelsequence(qname, skip,
4281 fixfname(client, &fname, &dbuf, &b);
4282 fixrdataset(client, &rdataset);
4283 fixrdataset(client, &sigrdataset);
4284 if (fname == NULL ||
4286 sigrdataset == NULL) {
4287 QUERY_ERROR(DNS_R_SERVFAIL);
4291 * 'nearest' doesn't exist so
4292 * 'exist' is set to ISC_FALSE.
4294 query_findclosestnsec3(found, db,
4304 query_releasename(client, &fname);
4305 query_addwildcardproof(client, db, version,
4306 client->query.qname,
4310 if (dns_rdataset_isassociated(rdataset)) {
4312 * If we've got a NSEC record, we need to save the
4313 * name now because we're going call query_addsoa()
4314 * below, and it needs to use the name buffer.
4316 query_keepname(client, fname, dbuf);
4317 } else if (fname != NULL) {
4319 * We're not going to use fname, and need to release
4320 * our hold on the name buffer so query_addsoa()
4323 query_releasename(client, &fname);
4328 result = query_addsoa(client, db, version, ISC_FALSE,
4329 dns_rdataset_isassociated(rdataset));
4330 if (result != ISC_R_SUCCESS) {
4331 QUERY_ERROR(result);
4335 * Add NSEC record if we found one.
4337 if (WANTDNSSEC(client)) {
4338 if (dns_rdataset_isassociated(rdataset))
4339 query_addnxrrsetnsec(client, db, version,
4345 case DNS_R_EMPTYWILD:
4346 empty_wild = ISC_TRUE;
4349 case DNS_R_NXDOMAIN:
4351 if (dns_rdataset_isassociated(rdataset)) {
4353 * If we've got a NSEC record, we need to save the
4354 * name now because we're going call query_addsoa()
4355 * below, and it needs to use the name buffer.
4357 query_keepname(client, fname, dbuf);
4358 } else if (fname != NULL) {
4360 * We're not going to use fname, and need to release
4361 * our hold on the name buffer so query_addsoa()
4364 query_releasename(client, &fname);
4367 * Add SOA. If the query was for a SOA record force the
4368 * ttl to zero so that it is possible for clients to find
4369 * the containing zone of an arbitrary name with a stub
4370 * resolver and not have it cached.
4372 if (qtype == dns_rdatatype_soa &&
4376 dns_zone_getzeronosoattl(zone))
4377 result = query_addsoa(client, db, version, ISC_TRUE,
4378 dns_rdataset_isassociated(rdataset));
4380 result = query_addsoa(client, db, version, ISC_FALSE,
4381 dns_rdataset_isassociated(rdataset));
4382 if (result != ISC_R_SUCCESS) {
4383 QUERY_ERROR(result);
4387 if (WANTDNSSEC(client)) {
4389 * Add NSEC record if we found one.
4391 if (dns_rdataset_isassociated(rdataset))
4392 query_addrrset(client, &fname, &rdataset,
4394 NULL, DNS_SECTION_AUTHORITY);
4395 query_addwildcardproof(client, db, version,
4396 client->query.qname, ISC_FALSE);
4400 * Set message rcode.
4403 client->message->rcode = dns_rcode_noerror;
4405 client->message->rcode = dns_rcode_nxdomain;
4408 case DNS_R_NCACHENXDOMAIN:
4409 case DNS_R_NCACHENXRRSET:
4411 authoritative = ISC_FALSE;
4413 * Set message rcode, if required.
4415 if (result == DNS_R_NCACHENXDOMAIN)
4416 client->message->rcode = dns_rcode_nxdomain;
4418 * Look for RFC 1918 leakage from Internet.
4420 if (result == DNS_R_NCACHENXDOMAIN &&
4421 qtype == dns_rdatatype_ptr &&
4422 client->message->rdclass == dns_rdataclass_in &&
4423 dns_name_countlabels(fname) == 7)
4424 warn_rfc1918(client, fname, rdataset);
4426 * We don't call query_addrrset() because we don't need any
4427 * of its extra features (and things would probably break!).
4429 query_keepname(client, fname, dbuf);
4430 dns_message_addname(client->message, fname,
4431 DNS_SECTION_AUTHORITY);
4432 ISC_LIST_APPEND(fname->list, rdataset, link);
4439 * Keep a copy of the rdataset. We have to do this because
4440 * query_addrrset may clear 'rdataset' (to prevent the
4441 * cleanup code from cleaning it up).
4443 trdataset = rdataset;
4445 * Add the CNAME to the answer section.
4447 if (sigrdataset != NULL)
4448 sigrdatasetp = &sigrdataset;
4450 sigrdatasetp = NULL;
4451 if (WANTDNSSEC(client) &&
4452 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4454 dns_fixedname_init(&wildcardname);
4455 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
4457 need_wildcardproof = ISC_TRUE;
4459 if (NOQNAME(rdataset) && WANTDNSSEC(client))
4463 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4464 DNS_SECTION_ANSWER);
4465 if (noqname != NULL)
4466 query_addnoqnameproof(client, noqname);
4468 * We set the PARTIALANSWER attribute so that if anything goes
4469 * wrong later on, we'll return what we've got so far.
4471 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
4473 * Reset qname to be the target name of the CNAME and restart
4477 result = dns_message_gettempname(client->message, &tname);
4478 if (result != ISC_R_SUCCESS)
4480 result = dns_rdataset_first(trdataset);
4481 if (result != ISC_R_SUCCESS) {
4482 dns_message_puttempname(client->message, &tname);
4485 dns_rdataset_current(trdataset, &rdata);
4486 result = dns_rdata_tostruct(&rdata, &cname, NULL);
4487 dns_rdata_reset(&rdata);
4488 if (result != ISC_R_SUCCESS) {
4489 dns_message_puttempname(client->message, &tname);
4492 dns_name_init(tname, NULL);
4493 result = dns_name_dup(&cname.cname, client->mctx, tname);
4494 if (result != ISC_R_SUCCESS) {
4495 dns_message_puttempname(client->message, &tname);
4496 dns_rdata_freestruct(&cname);
4499 dns_rdata_freestruct(&cname);
4500 ns_client_qnamereplace(client, tname);
4501 want_restart = ISC_TRUE;
4502 if (!WANTRECURSION(client))
4503 options |= DNS_GETDB_NOLOG;
4507 * Compare the current qname to the found name. We need
4508 * to know how many labels and bits are in common because
4509 * we're going to have to split qname later on.
4511 namereln = dns_name_fullcompare(client->query.qname, fname,
4513 INSIST(namereln == dns_namereln_subdomain);
4515 * Keep a copy of the rdataset. We have to do this because
4516 * query_addrrset may clear 'rdataset' (to prevent the
4517 * cleanup code from cleaning it up).
4519 trdataset = rdataset;
4521 * Add the DNAME to the answer section.
4523 if (sigrdataset != NULL)
4524 sigrdatasetp = &sigrdataset;
4526 sigrdatasetp = NULL;
4527 if (WANTDNSSEC(client) &&
4528 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4530 dns_fixedname_init(&wildcardname);
4531 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
4533 need_wildcardproof = ISC_TRUE;
4535 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4536 DNS_SECTION_ANSWER);
4538 * We set the PARTIALANSWER attribute so that if anything goes
4539 * wrong later on, we'll return what we've got so far.
4541 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
4543 * Get the target name of the DNAME.
4546 result = dns_message_gettempname(client->message, &tname);
4547 if (result != ISC_R_SUCCESS)
4549 result = dns_rdataset_first(trdataset);
4550 if (result != ISC_R_SUCCESS) {
4551 dns_message_puttempname(client->message, &tname);
4554 dns_rdataset_current(trdataset, &rdata);
4555 result = dns_rdata_tostruct(&rdata, &dname, NULL);
4556 dns_rdata_reset(&rdata);
4557 if (result != ISC_R_SUCCESS) {
4558 dns_message_puttempname(client->message, &tname);
4561 dns_name_init(tname, NULL);
4562 dns_name_clone(&dname.dname, tname);
4563 dns_rdata_freestruct(&dname);
4565 * Construct the new qname.
4567 dns_fixedname_init(&fixed);
4568 prefix = dns_fixedname_name(&fixed);
4569 dns_name_split(client->query.qname, nlabels, prefix, NULL);
4570 INSIST(fname == NULL);
4571 dbuf = query_getnamebuf(client);
4573 dns_message_puttempname(client->message, &tname);
4576 fname = query_newname(client, dbuf, &b);
4577 if (fname == NULL) {
4578 dns_message_puttempname(client->message, &tname);
4581 result = dns_name_concatenate(prefix, tname, fname, NULL);
4582 if (result != ISC_R_SUCCESS) {
4583 dns_message_puttempname(client->message, &tname);
4584 if (result == ISC_R_NOSPACE) {
4586 * RFC2672, section 4.1, subsection 3c says
4587 * we should return YXDOMAIN if the constructed
4588 * name would be too long.
4590 client->message->rcode = dns_rcode_yxdomain;
4594 query_keepname(client, fname, dbuf);
4596 * Synthesize a CNAME for this DNAME.
4598 * We want to synthesize a CNAME since if we don't
4599 * then older software that doesn't understand DNAME
4600 * will not chain like it should.
4602 * We do not try to synthesize a signature because we hope
4603 * that security aware servers will understand DNAME. Also,
4604 * even if we had an online key, making a signature
4605 * on-the-fly is costly, and not really legitimate anyway
4606 * since the synthesized CNAME is NOT in the zone.
4608 dns_name_init(tname, NULL);
4609 (void)query_addcnamelike(client, client->query.qname, fname,
4611 dns_rdatatype_cname);
4613 dns_message_puttempname(client->message, &tname);
4615 * Switch to the new qname and restart.
4617 ns_client_qnamereplace(client, fname);
4619 want_restart = ISC_TRUE;
4620 if (!WANTRECURSION(client))
4621 options |= DNS_GETDB_NOLOG;
4625 * Something has gone wrong.
4627 QUERY_ERROR(DNS_R_SERVFAIL);
4631 if (WANTDNSSEC(client) &&
4632 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4634 dns_fixedname_init(&wildcardname);
4635 dns_name_copy(fname, dns_fixedname_name(&wildcardname), NULL);
4636 need_wildcardproof = ISC_TRUE;
4639 if (type == dns_rdatatype_any) {
4641 * XXXRTH Need to handle zonecuts with special case
4646 result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
4647 if (result != ISC_R_SUCCESS) {
4648 QUERY_ERROR(DNS_R_SERVFAIL);
4652 * Calling query_addrrset() with a non-NULL dbuf is going
4653 * to either keep or release the name. We don't want it to
4654 * release fname, since we may have to call query_addrrset()
4655 * more than once. That means we have to call query_keepname()
4656 * now, and pass a NULL dbuf to query_addrrset().
4658 * If we do a query_addrrset() below, we must set fname to
4659 * NULL before leaving this block, otherwise we might try to
4660 * cleanup fname even though we're using it!
4662 query_keepname(client, fname, dbuf);
4664 result = dns_rdatasetiter_first(rdsiter);
4665 while (result == ISC_R_SUCCESS) {
4666 dns_rdatasetiter_current(rdsiter, rdataset);
4667 if (is_zone && qtype == dns_rdatatype_any &&
4668 !dns_db_issecure(db) &&
4669 dns_rdatatype_isdnssec(rdataset->type)) {
4671 * The zone is transitioning from insecure
4672 * to secure. Hide the dnssec records from
4675 dns_rdataset_disassociate(rdataset);
4676 } else if ((qtype == dns_rdatatype_any ||
4677 rdataset->type == qtype) && rdataset->type != 0) {
4678 if (NOQNAME(rdataset) && WANTDNSSEC(client))
4682 query_addrrset(client,
4683 fname != NULL ? &fname : &tname,
4685 NULL, DNS_SECTION_ANSWER);
4686 if (noqname != NULL)
4687 query_addnoqnameproof(client, noqname);
4689 INSIST(tname != NULL);
4691 * rdataset is non-NULL only in certain
4692 * pathological cases involving DNAMEs.
4694 if (rdataset != NULL)
4695 query_putrdataset(client, &rdataset);
4696 rdataset = query_newrdataset(client);
4697 if (rdataset == NULL)
4701 * We're not interested in this rdataset.
4703 dns_rdataset_disassociate(rdataset);
4705 result = dns_rdatasetiter_next(rdsiter);
4709 dns_message_puttempname(client->message, &fname);
4711 if (n == 0 && is_zone) {
4713 * We didn't match any rdatasets.
4715 if (qtype == dns_rdatatype_rrsig &&
4716 result == ISC_R_NOMORE) {
4718 * XXXRTH If this is a secure zone and we
4719 * didn't find any SIGs, we should generate
4720 * an error unless we were searching for
4724 authoritative = ISC_FALSE;
4725 dns_rdatasetiter_destroy(&rdsiter);
4726 if (RECURSIONOK(client)) {
4727 result = query_recurse(client,
4732 if (result == ISC_R_SUCCESS)
4733 client->query.attributes |=
4734 NS_QUERYATTR_RECURSING;
4736 RECURSE_ERROR(result);
4741 * We were searching for SIG records in
4742 * a nonsecure zone. Send a "no error,
4743 * no data" response.
4748 result = query_addsoa(client, db, version,
4749 ISC_FALSE, ISC_FALSE);
4750 if (result == ISC_R_SUCCESS)
4751 result = ISC_R_NOMORE;
4754 * Something went wrong.
4756 result = DNS_R_SERVFAIL;
4759 dns_rdatasetiter_destroy(&rdsiter);
4760 if (result != ISC_R_NOMORE) {
4761 QUERY_ERROR(DNS_R_SERVFAIL);
4766 * This is the "normal" case -- an ordinary question to which
4767 * we know the answer.
4769 if (sigrdataset != NULL)
4770 sigrdatasetp = &sigrdataset;
4772 sigrdatasetp = NULL;
4773 if (NOQNAME(rdataset) && WANTDNSSEC(client))
4778 * BIND 8 priming queries need the additional section.
4780 if (is_zone && qtype == dns_rdatatype_ns &&
4781 dns_name_equal(client->query.qname, dns_rootname))
4782 client->query.attributes &= ~NS_QUERYATTR_NOADDITIONAL;
4784 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4785 DNS_SECTION_ANSWER);
4786 if (noqname != NULL)
4787 query_addnoqnameproof(client, noqname);
4789 * We shouldn't ever fail to add 'rdataset'
4790 * because it's already in the answer.
4792 INSIST(rdataset == NULL);
4796 CTRACE("query_find: addauth");
4798 * Add NS records to the authority section (if we haven't already
4799 * added them to the answer section).
4801 if (!want_restart && !NOAUTHORITY(client)) {
4803 if (!((qtype == dns_rdatatype_ns ||
4804 qtype == dns_rdatatype_any) &&
4805 dns_name_equal(client->query.qname,
4806 dns_db_origin(db))))
4807 (void)query_addns(client, db, version);
4808 } else if (qtype != dns_rdatatype_ns) {
4810 query_releasename(client, &fname);
4811 query_addbestns(client);
4816 * Add NSEC records to the authority section if they're needed for
4817 * DNSSEC wildcard proofs.
4819 if (need_wildcardproof && dns_db_issecure(db))
4820 query_addwildcardproof(client, db, version,
4821 dns_fixedname_name(&wildcardname),
4824 CTRACE("query_find: cleanup");
4828 if (rdataset != NULL)
4829 query_putrdataset(client, &rdataset);
4830 if (sigrdataset != NULL)
4831 query_putrdataset(client, &sigrdataset);
4833 query_releasename(client, &fname);
4835 dns_db_detachnode(db, &node);
4839 dns_zone_detach(&zone);
4841 query_putrdataset(client, &zrdataset);
4842 if (zsigrdataset != NULL)
4843 query_putrdataset(client, &zsigrdataset);
4845 query_releasename(client, &zfname);
4846 dns_db_detach(&zdb);
4849 isc_event_free(ISC_EVENT_PTR(&event));
4854 if (client->query.restarts == 0 && !authoritative) {
4856 * We're not authoritative, so we must ensure the AA bit
4859 client->message->flags &= ~DNS_MESSAGEFLAG_AA;
4863 * Restart the query?
4865 if (want_restart && client->query.restarts < MAX_RESTARTS) {
4866 client->query.restarts++;
4870 if (eresult != ISC_R_SUCCESS &&
4871 (!PARTIALANSWER(client) || WANTRECURSION(client))) {
4872 if (eresult == DNS_R_DUPLICATE || eresult == DNS_R_DROP) {
4874 * This was a duplicate query that we are
4875 * recursing on. Don't send a response now.
4876 * The original query will still cause a response.
4878 query_next(client, eresult);
4881 * If we don't have any answer to give the client,
4882 * or if the client requested recursion and thus wanted
4883 * the complete answer, send an error response.
4886 query_error(client, eresult, line);
4888 ns_client_detach(&client);
4889 } else if (!RECURSING(client)) {
4891 * We are done. Set up sortlist data for the message
4892 * rendering code, make a final tweak to the AA bit if the
4893 * auth-nxdomain config option says so, then render and
4894 * send the response.
4896 setup_query_sortlist(client);
4899 * If this is a referral and the answer to the question
4900 * is in the glue sort it to the start of the additional
4903 if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER]) &&
4904 client->message->rcode == dns_rcode_noerror &&
4905 (qtype == dns_rdatatype_a || qtype == dns_rdatatype_aaaa))
4906 answer_in_glue(client, qtype);
4908 if (client->message->rcode == dns_rcode_nxdomain &&
4909 client->view->auth_nxdomain == ISC_TRUE)
4910 client->message->flags |= DNS_MESSAGEFLAG_AA;
4913 * If the response is somehow unexpected for the client and this
4914 * is a result of recursion, return an error to the caller
4915 * to indicate it may need to be logged.
4918 (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER]) ||
4919 client->message->rcode != dns_rcode_noerror))
4920 eresult = ISC_R_FAILURE;
4923 ns_client_detach(&client);
4925 CTRACE("query_find: done");
4931 log_query(ns_client_t *client, unsigned int flags, unsigned int extflags) {
4932 char namebuf[DNS_NAME_FORMATSIZE];
4933 char typename[DNS_RDATATYPE_FORMATSIZE];
4934 char classname[DNS_RDATACLASS_FORMATSIZE];
4935 dns_rdataset_t *rdataset;
4936 int level = ISC_LOG_INFO;
4938 if (! isc_log_wouldlog(ns_g_lctx, level))
4941 rdataset = ISC_LIST_HEAD(client->query.qname->list);
4942 INSIST(rdataset != NULL);
4943 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
4944 dns_rdataclass_format(rdataset->rdclass, classname, sizeof(classname));
4945 dns_rdatatype_format(rdataset->type, typename, sizeof(typename));
4947 ns_client_log(client, NS_LOGCATEGORY_QUERIES, NS_LOGMODULE_QUERY,
4948 level, "query: %s %s %s %s%s%s%s%s", namebuf, classname,
4949 typename, WANTRECURSION(client) ? "+" : "-",
4950 (client->signer != NULL) ? "S": "",
4951 (client->opt != NULL) ? "E" : "",
4952 ((extflags & DNS_MESSAGEEXTFLAG_DO) != 0) ? "D" : "",
4953 ((flags & DNS_MESSAGEFLAG_CD) != 0) ? "C" : "");
4957 log_queryerror(ns_client_t *client, isc_result_t result, int line, int level) {
4958 char namebuf[DNS_NAME_FORMATSIZE];
4959 char typename[DNS_RDATATYPE_FORMATSIZE];
4960 char classname[DNS_RDATACLASS_FORMATSIZE];
4961 const char *namep, *typep, *classp, *sep1, *sep2;
4962 dns_rdataset_t *rdataset;
4964 if (!isc_log_wouldlog(ns_g_lctx, level))
4967 namep = typep = classp = sep1 = sep2 = "";
4970 * Query errors can happen for various reasons. In some cases we cannot
4971 * even assume the query contains a valid question section, so we should
4972 * expect exceptional cases.
4974 if (client->query.origqname != NULL) {
4975 dns_name_format(client->query.origqname, namebuf,
4980 rdataset = ISC_LIST_HEAD(client->query.origqname->list);
4981 if (rdataset != NULL) {
4982 dns_rdataclass_format(rdataset->rdclass, classname,
4985 dns_rdatatype_format(rdataset->type, typename,
4992 ns_client_log(client, NS_LOGCATEGORY_QUERY_EERRORS, NS_LOGMODULE_QUERY,
4993 level, "query failed (%s)%s%s%s%s%s%s at %s:%d",
4994 isc_result_totext(result), sep1, namep, sep2,
4995 classp, sep2, typep, __FILE__, line);
4999 ns_query_start(ns_client_t *client) {
5000 isc_result_t result;
5001 dns_message_t *message = client->message;
5002 dns_rdataset_t *rdataset;
5003 ns_client_t *qclient;
5004 dns_rdatatype_t qtype;
5005 unsigned int saved_extflags = client->extflags;
5006 unsigned int saved_flags = client->message->flags;
5007 isc_boolean_t want_ad;
5009 CTRACE("ns_query_start");
5014 if (ns_g_clienttest && (client->attributes & NS_CLIENTATTR_TCP) == 0)
5015 RUNTIME_CHECK(ns_client_replace(client) == ISC_R_SUCCESS);
5018 * Ensure that appropriate cleanups occur.
5020 client->next = query_next_callback;
5023 * Behave as if we don't support DNSSEC if not enabled.
5025 if (!client->view->enablednssec) {
5026 message->flags &= ~DNS_MESSAGEFLAG_CD;
5027 client->extflags &= ~DNS_MESSAGEEXTFLAG_DO;
5028 if (client->opt != NULL)
5029 client->opt->ttl &= ~DNS_MESSAGEEXTFLAG_DO;
5032 if ((message->flags & DNS_MESSAGEFLAG_RD) != 0)
5033 client->query.attributes |= NS_QUERYATTR_WANTRECURSION;
5035 if ((client->extflags & DNS_MESSAGEEXTFLAG_DO) != 0)
5036 client->attributes |= NS_CLIENTATTR_WANTDNSSEC;
5038 if (client->view->minimalresponses)
5039 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
5040 NS_QUERYATTR_NOADDITIONAL);
5042 if ((client->view->cachedb == NULL)
5043 || (!client->view->additionalfromcache)) {
5045 * We don't have a cache. Turn off cache support and
5048 client->query.attributes &=
5049 ~(NS_QUERYATTR_RECURSIONOK|NS_QUERYATTR_CACHEOK);
5050 } else if ((client->attributes & NS_CLIENTATTR_RA) == 0 ||
5051 (message->flags & DNS_MESSAGEFLAG_RD) == 0) {
5053 * If the client isn't allowed to recurse (due to
5054 * "recursion no", the allow-recursion ACL, or the
5055 * lack of a resolver in this view), or if it
5056 * doesn't want recursion, turn recursion off.
5058 client->query.attributes &= ~NS_QUERYATTR_RECURSIONOK;
5062 * Get the question name.
5064 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
5065 if (result != ISC_R_SUCCESS) {
5066 query_error(client, result, __LINE__);
5069 dns_message_currentname(message, DNS_SECTION_QUESTION,
5070 &client->query.qname);
5071 client->query.origqname = client->query.qname;
5072 result = dns_message_nextname(message, DNS_SECTION_QUESTION);
5073 if (result != ISC_R_NOMORE) {
5074 if (result == ISC_R_SUCCESS) {
5076 * There's more than one QNAME in the question
5079 query_error(client, DNS_R_FORMERR, __LINE__);
5081 query_error(client, result, __LINE__);
5085 if (ns_g_server->log_queries)
5086 log_query(client, saved_flags, saved_extflags);
5089 * Check for multiple question queries, since edns1 is dead.
5091 if (message->counts[DNS_SECTION_QUESTION] > 1) {
5092 query_error(client, DNS_R_FORMERR, __LINE__);
5097 * Check for meta-queries like IXFR and AXFR.
5099 rdataset = ISC_LIST_HEAD(client->query.qname->list);
5100 INSIST(rdataset != NULL);
5101 qtype = rdataset->type;
5102 dns_rdatatypestats_increment(ns_g_server->rcvquerystats, qtype);
5103 if (dns_rdatatype_ismeta(qtype)) {
5105 case dns_rdatatype_any:
5106 break; /* Let query_find handle it. */
5107 case dns_rdatatype_ixfr:
5108 case dns_rdatatype_axfr:
5109 ns_xfr_start(client, rdataset->type);
5111 case dns_rdatatype_maila:
5112 case dns_rdatatype_mailb:
5113 query_error(client, DNS_R_NOTIMP, __LINE__);
5115 case dns_rdatatype_tkey:
5116 result = dns_tkey_processquery(client->message,
5117 ns_g_server->tkeyctx,
5118 client->view->dynamickeys);
5119 if (result == ISC_R_SUCCESS)
5122 query_error(client, result, __LINE__);
5124 default: /* TSIG, etc. */
5125 query_error(client, DNS_R_FORMERR, __LINE__);
5131 * Turn on minimal response for DNSKEY and DS queries.
5133 if (qtype == dns_rdatatype_dnskey || qtype == dns_rdatatype_ds)
5134 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
5135 NS_QUERYATTR_NOADDITIONAL);
5138 * Turn on minimal responses for EDNS/UDP bufsize 512 queries.
5140 if (client->opt != NULL && client->udpsize <= 512U &&
5141 (client->attributes & NS_CLIENTATTR_TCP) == 0)
5142 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
5143 NS_QUERYATTR_NOADDITIONAL);
5146 * If the client has requested that DNSSEC checking be disabled,
5147 * allow lookups to return pending data and instruct the resolver
5148 * to return data before validation has completed.
5150 * We don't need to set DNS_DBFIND_PENDINGOK when validation is
5151 * disabled as there will be no pending data.
5153 if (message->flags & DNS_MESSAGEFLAG_CD ||
5154 qtype == dns_rdatatype_rrsig)
5156 client->query.dboptions |= DNS_DBFIND_PENDINGOK;
5157 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
5158 } else if (!client->view->enablevalidation)
5159 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
5162 * Allow glue NS records to be added to the authority section
5163 * if the answer is secure.
5165 if (message->flags & DNS_MESSAGEFLAG_CD)
5166 client->query.attributes &= ~NS_QUERYATTR_SECURE;
5169 * Set 'want_ad' if the client has set AD in the query.
5170 * This allows AD to be returned on queries without DO set.
5172 if ((message->flags & DNS_MESSAGEFLAG_AD) != 0)
5175 want_ad = ISC_FALSE;
5178 * This is an ordinary query.
5180 result = dns_message_reply(message, ISC_TRUE);
5181 if (result != ISC_R_SUCCESS) {
5182 query_next(client, result);
5187 * Assume authoritative response until it is known to be
5190 message->flags |= DNS_MESSAGEFLAG_AA;
5193 * Set AD. We must clear it if we add non-validated data to a
5196 if (WANTDNSSEC(client) || want_ad)
5197 message->flags |= DNS_MESSAGEFLAG_AD;
5200 ns_client_attach(client, &qclient);
5201 (void)query_find(qclient, NULL, qtype);