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);
1284 * Negative cache entries don't have sigrdatasets.
1286 INSIST(sigrdataset == NULL ||
1287 ! dns_rdataset_isassociated(sigrdataset));
1289 if (result == ISC_R_SUCCESS) {
1291 if (!query_isduplicate(client, fname,
1292 dns_rdatatype_a, &mname)) {
1293 if (mname != NULL) {
1294 query_releasename(client, &fname);
1297 need_addname = ISC_TRUE;
1298 ISC_LIST_APPEND(fname->list, rdataset, link);
1299 added_something = ISC_TRUE;
1300 if (sigrdataset != NULL &&
1301 dns_rdataset_isassociated(sigrdataset))
1303 ISC_LIST_APPEND(fname->list,
1306 query_newrdataset(client);
1308 rdataset = query_newrdataset(client);
1309 if (rdataset == NULL)
1311 if (WANTDNSSEC(client) && sigrdataset == NULL)
1314 dns_rdataset_disassociate(rdataset);
1315 if (sigrdataset != NULL &&
1316 dns_rdataset_isassociated(sigrdataset))
1317 dns_rdataset_disassociate(sigrdataset);
1320 result = dns_db_findrdataset(db, node, version,
1321 dns_rdatatype_aaaa, 0,
1322 client->now, rdataset,
1324 if (result == DNS_R_NCACHENXDOMAIN)
1326 if (result == DNS_R_NCACHENXRRSET) {
1327 dns_rdataset_disassociate(rdataset);
1328 INSIST(sigrdataset == NULL ||
1329 ! dns_rdataset_isassociated(sigrdataset));
1331 if (result == ISC_R_SUCCESS) {
1333 if (!query_isduplicate(client, fname,
1334 dns_rdatatype_aaaa, &mname)) {
1335 if (mname != NULL) {
1336 query_releasename(client, &fname);
1339 need_addname = ISC_TRUE;
1340 ISC_LIST_APPEND(fname->list, rdataset, link);
1341 added_something = ISC_TRUE;
1342 if (sigrdataset != NULL &&
1343 dns_rdataset_isassociated(sigrdataset))
1345 ISC_LIST_APPEND(fname->list,
1355 CTRACE("query_addadditional: addname");
1357 * If we haven't added anything, then we're done.
1359 if (!added_something)
1363 * We may have added our rdatasets to an existing name, if so, then
1364 * need_addname will be ISC_FALSE. Whether we used an existing name
1365 * or a new one, we must set fname to NULL to prevent cleanup.
1368 dns_message_addname(client->message, fname,
1369 DNS_SECTION_ADDITIONAL);
1373 * In a few cases, we want to add additional data for additional
1374 * data. It's simpler to just deal with special cases here than
1375 * to try to create a general purpose mechanism and allow the
1376 * rdata implementations to do it themselves.
1378 * This involves recursion, but the depth is limited. The
1379 * most complex case is adding a SRV rdataset, which involves
1380 * recursing to add address records, which in turn can cause
1381 * recursion to add KEYs.
1383 if (type == dns_rdatatype_srv && trdataset != NULL) {
1385 * If we're adding SRV records to the additional data
1386 * section, it's helpful if we add the SRV additional data
1389 eresult = dns_rdataset_additionaldata(trdataset,
1390 query_addadditional,
1395 CTRACE("query_addadditional: cleanup");
1396 query_putrdataset(client, &rdataset);
1397 if (sigrdataset != NULL)
1398 query_putrdataset(client, &sigrdataset);
1400 query_releasename(client, &fname);
1402 dns_db_detachnode(db, &node);
1406 dns_zone_detach(&zone);
1408 CTRACE("query_addadditional: done");
1413 query_discardcache(ns_client_t *client, dns_rdataset_t *rdataset_base,
1414 dns_rdatasetadditional_t additionaltype,
1415 dns_rdatatype_t type, dns_zone_t **zonep, dns_db_t **dbp,
1416 dns_dbversion_t **versionp, dns_dbnode_t **nodep,
1419 dns_rdataset_t *rdataset;
1421 while ((rdataset = ISC_LIST_HEAD(fname->list)) != NULL) {
1422 ISC_LIST_UNLINK(fname->list, rdataset, link);
1423 query_putrdataset(client, &rdataset);
1425 if (*versionp != NULL)
1426 dns_db_closeversion(*dbp, versionp, ISC_FALSE);
1428 dns_db_detachnode(*dbp, nodep);
1432 dns_zone_detach(zonep);
1433 (void)dns_rdataset_putadditional(client->view->acache, rdataset_base,
1434 additionaltype, type);
1437 static inline isc_result_t
1438 query_iscachevalid(dns_zone_t *zone, dns_db_t *db, dns_db_t *db0,
1439 dns_dbversion_t *version)
1441 isc_result_t result = ISC_R_SUCCESS;
1442 dns_dbversion_t *version_current = NULL;
1443 dns_db_t *db_current = db0;
1445 if (db_current == NULL) {
1446 result = dns_zone_getdb(zone, &db_current);
1447 if (result != ISC_R_SUCCESS)
1450 dns_db_currentversion(db_current, &version_current);
1451 if (db_current != db || version_current != version) {
1452 result = ISC_R_FAILURE;
1457 dns_db_closeversion(db_current, &version_current, ISC_FALSE);
1458 if (db0 == NULL && db_current != NULL)
1459 dns_db_detach(&db_current);
1465 query_addadditional2(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1466 client_additionalctx_t *additionalctx = arg;
1467 dns_rdataset_t *rdataset_base;
1468 ns_client_t *client;
1469 isc_result_t result, eresult;
1470 dns_dbnode_t *node, *cnode;
1472 dns_name_t *fname, *mname0, cfname;
1473 dns_rdataset_t *rdataset, *sigrdataset;
1474 dns_rdataset_t *crdataset, *crdataset_next;
1477 dns_dbversion_t *version, *cversion;
1478 isc_boolean_t added_something, need_addname, needadditionalcache;
1479 isc_boolean_t need_sigrrset;
1481 dns_rdatatype_t type;
1482 dns_rdatasetadditional_t additionaltype;
1484 if (qtype != dns_rdatatype_a) {
1486 * This function is optimized for "address" types. For other
1487 * types, use a generic routine.
1488 * XXX: ideally, this function should be generic enough.
1490 return (query_addadditional(additionalctx->client,
1497 rdataset_base = additionalctx->rdataset;
1498 client = additionalctx->client;
1499 REQUIRE(NS_CLIENT_VALID(client));
1500 eresult = ISC_R_SUCCESS;
1510 added_something = ISC_FALSE;
1511 need_addname = ISC_FALSE;
1513 needadditionalcache = ISC_FALSE;
1514 additionaltype = dns_rdatasetadditional_fromauth;
1515 dns_name_init(&cfname, NULL);
1517 CTRACE("query_addadditional2");
1520 * We treat type A additional section processing as if it
1521 * were "any address type" additional section processing.
1522 * To avoid multiple lookups, we do an 'any' database
1523 * lookup and iterate over the node.
1524 * XXXJT: this approach can cause a suboptimal result when the cache
1525 * DB only has partial address types and the glue DB has remaining
1528 type = dns_rdatatype_any;
1531 * Get some resources.
1533 dbuf = query_getnamebuf(client);
1536 fname = query_newname(client, dbuf, &b);
1539 dns_name_setbuffer(&cfname, &b); /* share the buffer */
1541 /* Check additional cache */
1542 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1543 type, client->view->acache, &zone,
1544 &cdb, &cversion, &cnode, &cfname,
1545 client->message, client->now);
1546 if (result != ISC_R_SUCCESS)
1549 CTRACE("query_addadditional2: auth zone not found");
1553 /* Is the cached DB up-to-date? */
1554 result = query_iscachevalid(zone, cdb, NULL, cversion);
1555 if (result != ISC_R_SUCCESS) {
1556 CTRACE("query_addadditional2: old auth additional cache");
1557 query_discardcache(client, rdataset_base, additionaltype,
1558 type, &zone, &cdb, &cversion, &cnode,
1563 if (cnode == NULL) {
1565 * We have a negative cache. We don't have to check the zone
1566 * ACL, since the result (not using this zone) would be same
1567 * regardless of the result.
1569 CTRACE("query_addadditional2: negative auth additional cache");
1570 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1571 dns_db_detach(&cdb);
1572 dns_zone_detach(&zone);
1576 result = query_validatezonedb(client, name, qtype, DNS_GETDB_NOLOG,
1578 if (result != ISC_R_SUCCESS) {
1579 query_discardcache(client, rdataset_base, additionaltype,
1580 type, &zone, &cdb, &cversion, &cnode,
1585 /* We've got an active cache. */
1586 CTRACE("query_addadditional2: auth additional cache");
1587 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1590 dns_name_clone(&cfname, fname);
1591 query_keepname(client, fname, dbuf);
1595 * Look for a zone database that might contain authoritative
1599 result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1600 &zone, &db, &version);
1601 if (result != ISC_R_SUCCESS) {
1602 /* Cache the negative result */
1603 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1604 type, client->view->acache,
1605 NULL, NULL, NULL, NULL,
1610 CTRACE("query_addadditional2: db_find");
1613 * Since we are looking for authoritative data, we do not set
1614 * the GLUEOK flag. Glue will be looked for later, but not
1615 * necessarily in the same database.
1618 result = dns_db_find(db, name, version, type, client->query.dboptions,
1619 client->now, &node, fname, NULL, NULL);
1620 if (result == ISC_R_SUCCESS)
1623 /* Cache the negative result */
1624 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1625 type, client->view->acache, zone, db,
1626 version, NULL, fname);
1629 dns_db_detachnode(db, &node);
1634 * No authoritative data was found. The cache is our next best bet.
1638 additionaltype = dns_rdatasetadditional_fromcache;
1639 result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1640 if (result != ISC_R_SUCCESS)
1642 * Most likely the client isn't allowed to query the cache.
1646 result = dns_db_find(db, name, version, type,
1647 client->query.dboptions |
1648 DNS_DBFIND_GLUEOK | DNS_DBFIND_ADDITIONALOK,
1649 client->now, &node, fname, NULL, NULL);
1650 if (result == ISC_R_SUCCESS)
1654 dns_db_detachnode(db, &node);
1659 * No cached data was found. Glue is our last chance.
1662 * NS records cause both the usual additional section
1663 * processing to locate a type A record, and, when used
1664 * in a referral, a special search of the zone in which
1665 * they reside for glue information.
1667 * This is the "special search". Note that we must search
1668 * the zone where the NS record resides, not the zone it
1669 * points to, and that we only do the search in the delegation
1670 * case (identified by client->query.gluedb being set).
1672 if (client->query.gluedb == NULL)
1676 * Don't poison caches using the bailiwick protection model.
1678 if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1681 /* Check additional cache */
1682 additionaltype = dns_rdatasetadditional_fromglue;
1683 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1684 type, client->view->acache, NULL,
1685 &cdb, &cversion, &cnode, &cfname,
1686 client->message, client->now);
1687 if (result != ISC_R_SUCCESS)
1690 result = query_iscachevalid(zone, cdb, client->query.gluedb, cversion);
1691 if (result != ISC_R_SUCCESS) {
1692 CTRACE("query_addadditional2: old glue additional cache");
1693 query_discardcache(client, rdataset_base, additionaltype,
1694 type, &zone, &cdb, &cversion, &cnode,
1699 if (cnode == NULL) {
1700 /* We have a negative cache. */
1701 CTRACE("query_addadditional2: negative glue additional cache");
1702 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1703 dns_db_detach(&cdb);
1708 CTRACE("query_addadditional2: glue additional cache");
1709 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1712 dns_name_clone(&cfname, fname);
1713 query_keepname(client, fname, dbuf);
1717 dns_db_attach(client->query.gluedb, &db);
1718 result = dns_db_find(db, name, version, type,
1719 client->query.dboptions | DNS_DBFIND_GLUEOK,
1720 client->now, &node, fname, NULL, NULL);
1721 if (!(result == ISC_R_SUCCESS ||
1722 result == DNS_R_ZONECUT ||
1723 result == DNS_R_GLUE)) {
1724 /* cache the negative result */
1725 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1726 type, client->view->acache,
1727 NULL, db, version, NULL,
1734 * We have found a DB node to iterate over from a DB.
1735 * We are going to look for address RRsets (i.e., A and AAAA) in the DB
1736 * node we've just found. We'll then store the complete information
1737 * in the additional data cache.
1739 dns_name_clone(fname, &cfname);
1740 query_keepname(client, fname, dbuf);
1741 needadditionalcache = ISC_TRUE;
1743 rdataset = query_newrdataset(client);
1744 if (rdataset == NULL)
1747 sigrdataset = query_newrdataset(client);
1748 if (sigrdataset == NULL)
1752 * Find A RRset with sig RRset. Even if we don't find a sig RRset
1753 * for a client using DNSSEC, we'll continue the process to make a
1754 * complete list to be cached. However, we need to cancel the
1755 * caching when something unexpected happens, in order to avoid
1756 * caching incomplete information.
1758 result = dns_db_findrdataset(db, node, version, dns_rdatatype_a, 0,
1759 client->now, rdataset, sigrdataset);
1761 * If we can't promote glue/pending from the cache to secure
1764 if (result == ISC_R_SUCCESS &&
1765 additionaltype == dns_rdatasetadditional_fromcache &&
1766 (DNS_TRUST_PENDING(rdataset->trust) ||
1767 DNS_TRUST_GLUE(rdataset->trust)) &&
1768 !validate(client, db, fname, rdataset, sigrdataset)) {
1769 dns_rdataset_disassociate(rdataset);
1770 if (dns_rdataset_isassociated(sigrdataset))
1771 dns_rdataset_disassociate(sigrdataset);
1772 result = ISC_R_NOTFOUND;
1774 if (result == DNS_R_NCACHENXDOMAIN)
1776 if (result == DNS_R_NCACHENXRRSET) {
1777 dns_rdataset_disassociate(rdataset);
1779 * Negative cache entries don't have sigrdatasets.
1781 INSIST(! dns_rdataset_isassociated(sigrdataset));
1783 if (result == ISC_R_SUCCESS) {
1784 /* Remember the result as a cache */
1785 ISC_LIST_APPEND(cfname.list, rdataset, link);
1786 if (dns_rdataset_isassociated(sigrdataset)) {
1787 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1788 sigrdataset = query_newrdataset(client);
1790 rdataset = query_newrdataset(client);
1791 if (sigrdataset == NULL || rdataset == NULL) {
1792 /* do not cache incomplete information */
1797 /* Find AAAA RRset with sig RRset */
1798 result = dns_db_findrdataset(db, node, version, dns_rdatatype_aaaa,
1799 0, client->now, rdataset, sigrdataset);
1801 * If we can't promote glue/pending from the cache to secure
1804 if (result == ISC_R_SUCCESS &&
1805 additionaltype == dns_rdatasetadditional_fromcache &&
1806 (DNS_TRUST_PENDING(rdataset->trust) ||
1807 DNS_TRUST_GLUE(rdataset->trust)) &&
1808 !validate(client, db, fname, rdataset, sigrdataset)) {
1809 dns_rdataset_disassociate(rdataset);
1810 if (dns_rdataset_isassociated(sigrdataset))
1811 dns_rdataset_disassociate(sigrdataset);
1812 result = ISC_R_NOTFOUND;
1814 if (result == ISC_R_SUCCESS) {
1815 ISC_LIST_APPEND(cfname.list, rdataset, link);
1817 if (dns_rdataset_isassociated(sigrdataset)) {
1818 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1825 * Set the new result in the cache if required. We do not support
1826 * caching additional data from a cache DB.
1828 if (needadditionalcache == ISC_TRUE &&
1829 (additionaltype == dns_rdatasetadditional_fromauth ||
1830 additionaltype == dns_rdatasetadditional_fromglue)) {
1831 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1832 type, client->view->acache,
1833 zone, db, version, node,
1838 need_sigrrset = ISC_FALSE;
1840 for (crdataset = ISC_LIST_HEAD(cfname.list);
1842 crdataset = crdataset_next) {
1845 crdataset_next = ISC_LIST_NEXT(crdataset, link);
1848 if (crdataset->type == dns_rdatatype_a ||
1849 crdataset->type == dns_rdatatype_aaaa) {
1850 if (!query_isduplicate(client, fname, crdataset->type,
1852 if (mname != NULL) {
1854 * A different type of this name is
1855 * already stored in the additional
1856 * section. We'll reuse the name.
1857 * Note that this should happen at most
1858 * once. Otherwise, fname->link could
1861 INSIST(mname0 == NULL);
1863 query_releasename(client, &fname);
1867 need_addname = ISC_TRUE;
1868 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1869 ISC_LIST_APPEND(fname->list, crdataset, link);
1870 added_something = ISC_TRUE;
1871 need_sigrrset = ISC_TRUE;
1873 need_sigrrset = ISC_FALSE;
1874 } else if (crdataset->type == dns_rdatatype_rrsig &&
1875 need_sigrrset && WANTDNSSEC(client)) {
1876 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1877 ISC_LIST_APPEND(fname->list, crdataset, link);
1878 added_something = ISC_TRUE; /* just in case */
1879 need_sigrrset = ISC_FALSE;
1883 CTRACE("query_addadditional2: addname");
1886 * If we haven't added anything, then we're done.
1888 if (!added_something)
1892 * We may have added our rdatasets to an existing name, if so, then
1893 * need_addname will be ISC_FALSE. Whether we used an existing name
1894 * or a new one, we must set fname to NULL to prevent cleanup.
1897 dns_message_addname(client->message, fname,
1898 DNS_SECTION_ADDITIONAL);
1902 CTRACE("query_addadditional2: cleanup");
1904 if (rdataset != NULL)
1905 query_putrdataset(client, &rdataset);
1906 if (sigrdataset != NULL)
1907 query_putrdataset(client, &sigrdataset);
1908 while ((crdataset = ISC_LIST_HEAD(cfname.list)) != NULL) {
1909 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1910 query_putrdataset(client, &crdataset);
1913 query_releasename(client, &fname);
1915 dns_db_detachnode(db, &node);
1919 dns_zone_detach(&zone);
1921 CTRACE("query_addadditional2: done");
1926 query_addrdataset(ns_client_t *client, dns_name_t *fname,
1927 dns_rdataset_t *rdataset)
1929 client_additionalctx_t additionalctx;
1932 * Add 'rdataset' and any pertinent additional data to
1933 * 'fname', a name in the response message for 'client'.
1936 CTRACE("query_addrdataset");
1938 ISC_LIST_APPEND(fname->list, rdataset, link);
1940 if (client->view->order != NULL)
1941 rdataset->attributes |= dns_order_find(client->view->order,
1942 fname, rdataset->type,
1944 rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
1946 if (NOADDITIONAL(client))
1950 * Add additional data.
1952 * We don't care if dns_rdataset_additionaldata() fails.
1954 additionalctx.client = client;
1955 additionalctx.rdataset = rdataset;
1956 (void)dns_rdataset_additionaldata(rdataset, query_addadditional2,
1958 CTRACE("query_addrdataset: done");
1962 query_addrrset(ns_client_t *client, dns_name_t **namep,
1963 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp,
1964 isc_buffer_t *dbuf, dns_section_t section)
1966 dns_name_t *name, *mname;
1967 dns_rdataset_t *rdataset, *mrdataset, *sigrdataset;
1968 isc_result_t result;
1971 * To the current response for 'client', add the answer RRset
1972 * '*rdatasetp' and an optional signature set '*sigrdatasetp', with
1973 * owner name '*namep', to section 'section', unless they are
1974 * already there. Also add any pertinent additional data.
1976 * If 'dbuf' is not NULL, then '*namep' is the name whose data is
1977 * stored in 'dbuf'. In this case, query_addrrset() guarantees that
1978 * when it returns the name will either have been kept or released.
1980 CTRACE("query_addrrset");
1982 rdataset = *rdatasetp;
1983 if (sigrdatasetp != NULL)
1984 sigrdataset = *sigrdatasetp;
1989 result = dns_message_findname(client->message, section,
1990 name, rdataset->type, rdataset->covers,
1991 &mname, &mrdataset);
1992 if (result == ISC_R_SUCCESS) {
1994 * We've already got an RRset of the given name and type.
1995 * There's nothing else to do;
1997 CTRACE("query_addrrset: dns_message_findname succeeded: done");
1999 query_releasename(client, namep);
2001 } else if (result == DNS_R_NXDOMAIN) {
2003 * The name doesn't exist.
2006 query_keepname(client, name, dbuf);
2007 dns_message_addname(client->message, name, section);
2011 RUNTIME_CHECK(result == DNS_R_NXRRSET);
2013 query_releasename(client, namep);
2016 if (rdataset->trust != dns_trust_secure &&
2017 (section == DNS_SECTION_ANSWER ||
2018 section == DNS_SECTION_AUTHORITY))
2019 client->query.attributes &= ~NS_QUERYATTR_SECURE;
2021 * Note: we only add SIGs if we've added the type they cover, so
2022 * we do not need to check if the SIG rdataset is already in the
2025 query_addrdataset(client, mname, rdataset);
2027 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset)) {
2029 * We have a signature. Add it to the response.
2031 ISC_LIST_APPEND(mname->list, sigrdataset, link);
2032 *sigrdatasetp = NULL;
2034 CTRACE("query_addrrset: done");
2037 static inline isc_result_t
2038 query_addsoa(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version,
2039 isc_boolean_t zero_ttl, isc_boolean_t isassociated)
2043 isc_result_t result, eresult;
2044 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2045 dns_rdataset_t **sigrdatasetp = NULL;
2047 CTRACE("query_addsoa");
2051 eresult = ISC_R_SUCCESS;
2057 * Don't add the SOA record for test which set "-T nosoa".
2059 if (ns_g_nosoa && (!WANTDNSSEC(client) || !isassociated))
2060 return (ISC_R_SUCCESS);
2063 * Get resources and make 'name' be the database origin.
2065 result = dns_message_gettempname(client->message, &name);
2066 if (result != ISC_R_SUCCESS)
2068 dns_name_init(name, NULL);
2069 dns_name_clone(dns_db_origin(db), name);
2070 rdataset = query_newrdataset(client);
2071 if (rdataset == NULL) {
2072 eresult = DNS_R_SERVFAIL;
2075 if (WANTDNSSEC(client) && dns_db_issecure(db)) {
2076 sigrdataset = query_newrdataset(client);
2077 if (sigrdataset == NULL) {
2078 eresult = DNS_R_SERVFAIL;
2086 result = dns_db_getoriginnode(db, &node);
2087 if (result == ISC_R_SUCCESS) {
2088 result = dns_db_findrdataset(db, node, version,
2090 0, client->now, rdataset,
2093 dns_fixedname_t foundname;
2096 dns_fixedname_init(&foundname);
2097 fname = dns_fixedname_name(&foundname);
2099 result = dns_db_find(db, name, version, dns_rdatatype_soa,
2100 client->query.dboptions, 0, &node,
2101 fname, rdataset, sigrdataset);
2103 if (result != ISC_R_SUCCESS) {
2105 * This is bad. We tried to get the SOA RR at the zone top
2106 * and it didn't work!
2108 eresult = DNS_R_SERVFAIL;
2111 * Extract the SOA MINIMUM.
2113 dns_rdata_soa_t soa;
2114 dns_rdata_t rdata = DNS_RDATA_INIT;
2115 result = dns_rdataset_first(rdataset);
2116 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2117 dns_rdataset_current(rdataset, &rdata);
2118 result = dns_rdata_tostruct(&rdata, &soa, NULL);
2119 if (result != ISC_R_SUCCESS)
2124 if (sigrdataset != NULL)
2125 sigrdataset->ttl = 0;
2129 * Add the SOA and its SIG to the response, with the
2130 * TTLs adjusted per RFC2308 section 3.
2132 if (rdataset->ttl > soa.minimum)
2133 rdataset->ttl = soa.minimum;
2134 if (sigrdataset != NULL && sigrdataset->ttl > soa.minimum)
2135 sigrdataset->ttl = soa.minimum;
2137 if (sigrdataset != NULL)
2138 sigrdatasetp = &sigrdataset;
2140 sigrdatasetp = NULL;
2141 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2142 DNS_SECTION_AUTHORITY);
2146 query_putrdataset(client, &rdataset);
2147 if (sigrdataset != NULL)
2148 query_putrdataset(client, &sigrdataset);
2150 query_releasename(client, &name);
2152 dns_db_detachnode(db, &node);
2157 static inline isc_result_t
2158 query_addns(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version) {
2159 dns_name_t *name, *fname;
2161 isc_result_t result, eresult;
2162 dns_fixedname_t foundname;
2163 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2164 dns_rdataset_t **sigrdatasetp = NULL;
2166 CTRACE("query_addns");
2170 eresult = ISC_R_SUCCESS;
2174 dns_fixedname_init(&foundname);
2175 fname = dns_fixedname_name(&foundname);
2178 * Get resources and make 'name' be the database origin.
2180 result = dns_message_gettempname(client->message, &name);
2181 if (result != ISC_R_SUCCESS) {
2182 CTRACE("query_addns: dns_message_gettempname failed: done");
2185 dns_name_init(name, NULL);
2186 dns_name_clone(dns_db_origin(db), name);
2187 rdataset = query_newrdataset(client);
2188 if (rdataset == NULL) {
2189 CTRACE("query_addns: query_newrdataset failed");
2190 eresult = DNS_R_SERVFAIL;
2193 if (WANTDNSSEC(client) && dns_db_issecure(db)) {
2194 sigrdataset = query_newrdataset(client);
2195 if (sigrdataset == NULL) {
2196 CTRACE("query_addns: query_newrdataset failed");
2197 eresult = DNS_R_SERVFAIL;
2203 * Find the NS rdataset.
2205 result = dns_db_getoriginnode(db, &node);
2206 if (result == ISC_R_SUCCESS) {
2207 result = dns_db_findrdataset(db, node, version,
2209 0, client->now, rdataset,
2212 CTRACE("query_addns: calling dns_db_find");
2213 result = dns_db_find(db, name, NULL, dns_rdatatype_ns,
2214 client->query.dboptions, 0, &node,
2215 fname, rdataset, sigrdataset);
2216 CTRACE("query_addns: dns_db_find complete");
2218 if (result != ISC_R_SUCCESS) {
2219 CTRACE("query_addns: "
2220 "dns_db_findrdataset or dns_db_find failed");
2222 * This is bad. We tried to get the NS rdataset at the zone
2223 * top and it didn't work!
2225 eresult = DNS_R_SERVFAIL;
2227 if (sigrdataset != NULL)
2228 sigrdatasetp = &sigrdataset;
2230 sigrdatasetp = NULL;
2231 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2232 DNS_SECTION_AUTHORITY);
2236 CTRACE("query_addns: cleanup");
2237 query_putrdataset(client, &rdataset);
2238 if (sigrdataset != NULL)
2239 query_putrdataset(client, &sigrdataset);
2241 query_releasename(client, &name);
2243 dns_db_detachnode(db, &node);
2245 CTRACE("query_addns: done");
2249 static inline isc_result_t
2250 query_addcnamelike(ns_client_t *client, dns_name_t *qname, dns_name_t *tname,
2251 dns_rdataset_t *dname, dns_name_t **anamep,
2252 dns_rdatatype_t type)
2254 dns_rdataset_t *rdataset;
2255 dns_rdatalist_t *rdatalist;
2257 isc_result_t result;
2261 * We assume the name data referred to by tname won't go away.
2264 REQUIRE(anamep != NULL);
2267 result = dns_message_gettemprdatalist(client->message, &rdatalist);
2268 if (result != ISC_R_SUCCESS)
2271 result = dns_message_gettemprdata(client->message, &rdata);
2272 if (result != ISC_R_SUCCESS)
2275 result = dns_message_gettemprdataset(client->message, &rdataset);
2276 if (result != ISC_R_SUCCESS)
2278 dns_rdataset_init(rdataset);
2279 result = dns_name_dup(qname, client->mctx, *anamep);
2280 if (result != ISC_R_SUCCESS) {
2281 dns_message_puttemprdataset(client->message, &rdataset);
2285 rdatalist->type = type;
2286 rdatalist->covers = 0;
2287 rdatalist->rdclass = client->message->rdclass;
2288 rdatalist->ttl = dname->ttl;
2290 dns_name_toregion(tname, &r);
2291 rdata->data = r.base;
2292 rdata->length = r.length;
2293 rdata->rdclass = client->message->rdclass;
2296 ISC_LIST_INIT(rdatalist->rdata);
2297 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
2298 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
2300 rdataset->trust = dname->trust;
2302 query_addrrset(client, anamep, &rdataset, NULL, NULL,
2303 DNS_SECTION_ANSWER);
2305 if (rdataset != NULL) {
2306 if (dns_rdataset_isassociated(rdataset))
2307 dns_rdataset_disassociate(rdataset);
2308 dns_message_puttemprdataset(client->message, &rdataset);
2311 return (ISC_R_SUCCESS);
2315 * Mark the RRsets as secure. Update the cache (db) to reflect the
2316 * change in trust level.
2319 mark_secure(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2320 isc_uint32_t ttl, dns_rdataset_t *rdataset,
2321 dns_rdataset_t *sigrdataset)
2323 isc_result_t result;
2324 dns_dbnode_t *node = NULL;
2326 rdataset->trust = dns_trust_secure;
2327 sigrdataset->trust = dns_trust_secure;
2330 * Save the updated secure state. Ignore failures.
2332 result = dns_db_findnode(db, name, ISC_TRUE, &node);
2333 if (result != ISC_R_SUCCESS)
2336 * Bound the validated ttls then minimise.
2338 if (sigrdataset->ttl > ttl)
2339 sigrdataset->ttl = ttl;
2340 if (rdataset->ttl > ttl)
2341 rdataset->ttl = ttl;
2342 if (rdataset->ttl > sigrdataset->ttl)
2343 rdataset->ttl = sigrdataset->ttl;
2345 sigrdataset->ttl = rdataset->ttl;
2347 (void)dns_db_addrdataset(db, node, NULL, client->now, rdataset,
2349 (void)dns_db_addrdataset(db, node, NULL, client->now, sigrdataset,
2351 dns_db_detachnode(db, &node);
2355 * Find the secure key that corresponds to rrsig.
2356 * Note: 'keyrdataset' maintains state between successive calls,
2357 * there may be multiple keys with the same keyid.
2358 * Return ISC_FALSE if we have exhausted all the possible keys.
2360 static isc_boolean_t
2361 get_key(ns_client_t *client, dns_db_t *db, dns_rdata_rrsig_t *rrsig,
2362 dns_rdataset_t *keyrdataset, dst_key_t **keyp)
2364 isc_result_t result;
2365 dns_dbnode_t *node = NULL;
2366 isc_boolean_t secure = ISC_FALSE;
2368 if (!dns_rdataset_isassociated(keyrdataset)) {
2369 result = dns_db_findnode(db, &rrsig->signer, ISC_FALSE, &node);
2370 if (result != ISC_R_SUCCESS)
2373 result = dns_db_findrdataset(db, node, NULL,
2374 dns_rdatatype_dnskey, 0,
2375 client->now, keyrdataset, NULL);
2376 dns_db_detachnode(db, &node);
2377 if (result != ISC_R_SUCCESS)
2380 if (keyrdataset->trust != dns_trust_secure)
2383 result = dns_rdataset_first(keyrdataset);
2385 result = dns_rdataset_next(keyrdataset);
2387 for ( ; result == ISC_R_SUCCESS;
2388 result = dns_rdataset_next(keyrdataset)) {
2389 dns_rdata_t rdata = DNS_RDATA_INIT;
2392 dns_rdataset_current(keyrdataset, &rdata);
2393 isc_buffer_init(&b, rdata.data, rdata.length);
2394 isc_buffer_add(&b, rdata.length);
2395 result = dst_key_fromdns(&rrsig->signer, rdata.rdclass, &b,
2396 client->mctx, keyp);
2397 if (result != ISC_R_SUCCESS)
2399 if (rrsig->algorithm == (dns_secalg_t)dst_key_alg(*keyp) &&
2400 rrsig->keyid == (dns_keytag_t)dst_key_id(*keyp) &&
2401 dst_key_iszonekey(*keyp)) {
2410 static isc_boolean_t
2411 verify(dst_key_t *key, dns_name_t *name, dns_rdataset_t *rdataset,
2412 dns_rdata_t *rdata, isc_mem_t *mctx, isc_boolean_t acceptexpired)
2414 isc_result_t result;
2415 dns_fixedname_t fixed;
2416 isc_boolean_t ignore = ISC_FALSE;
2418 dns_fixedname_init(&fixed);
2421 result = dns_dnssec_verify2(name, rdataset, key, ignore, mctx,
2423 if (result == DNS_R_SIGEXPIRED && acceptexpired) {
2427 if (result == ISC_R_SUCCESS || result == DNS_R_FROMWILDCARD)
2433 * Validate the rdataset if possible with available records.
2435 static isc_boolean_t
2436 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2437 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2439 isc_result_t result;
2440 dns_rdata_t rdata = DNS_RDATA_INIT;
2441 dns_rdata_rrsig_t rrsig;
2442 dst_key_t *key = NULL;
2443 dns_rdataset_t keyrdataset;
2445 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2448 for (result = dns_rdataset_first(sigrdataset);
2449 result == ISC_R_SUCCESS;
2450 result = dns_rdataset_next(sigrdataset)) {
2452 dns_rdata_reset(&rdata);
2453 dns_rdataset_current(sigrdataset, &rdata);
2454 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
2455 if (result != ISC_R_SUCCESS)
2457 if (!dns_resolver_algorithm_supported(client->view->resolver,
2458 name, rrsig.algorithm))
2460 if (!dns_name_issubdomain(name, &rrsig.signer))
2462 dns_rdataset_init(&keyrdataset);
2464 if (!get_key(client, db, &rrsig, &keyrdataset, &key))
2466 if (verify(key, name, rdataset, &rdata, client->mctx,
2467 client->view->acceptexpired)) {
2469 dns_rdataset_disassociate(&keyrdataset);
2470 mark_secure(client, db, name,
2472 rdataset, sigrdataset);
2477 if (dns_rdataset_isassociated(&keyrdataset))
2478 dns_rdataset_disassociate(&keyrdataset);
2484 query_addbestns(ns_client_t *client) {
2487 dns_name_t *fname, *zfname;
2488 dns_rdataset_t *rdataset, *sigrdataset, *zrdataset, *zsigrdataset;
2489 isc_boolean_t is_zone, use_zone;
2491 isc_result_t result;
2492 dns_dbversion_t *version;
2496 CTRACE("query_addbestns");
2502 zsigrdataset = NULL;
2508 is_zone = ISC_FALSE;
2509 use_zone = ISC_FALSE;
2512 * Find the right database.
2514 result = query_getdb(client, client->query.qname, dns_rdatatype_ns, 0,
2515 &zone, &db, &version, &is_zone);
2516 if (result != ISC_R_SUCCESS)
2521 * We'll need some resources...
2523 dbuf = query_getnamebuf(client);
2526 fname = query_newname(client, dbuf, &b);
2527 rdataset = query_newrdataset(client);
2528 if (fname == NULL || rdataset == NULL)
2531 * Get the RRSIGs if the client requested them or if we may
2532 * need to validate answers from the cache.
2534 if (WANTDNSSEC(client) || !is_zone) {
2535 sigrdataset = query_newrdataset(client);
2536 if (sigrdataset == NULL)
2541 * Now look for the zonecut.
2544 result = dns_db_find(db, client->query.qname, version,
2545 dns_rdatatype_ns, client->query.dboptions,
2546 client->now, &node, fname,
2547 rdataset, sigrdataset);
2548 if (result != DNS_R_DELEGATION)
2550 if (USECACHE(client)) {
2551 query_keepname(client, fname, dbuf);
2555 zrdataset = rdataset;
2557 zsigrdataset = sigrdataset;
2559 dns_db_detachnode(db, &node);
2562 dns_db_attach(client->view->cachedb, &db);
2563 is_zone = ISC_FALSE;
2567 result = dns_db_findzonecut(db, client->query.qname,
2568 client->query.dboptions,
2569 client->now, &node, fname,
2570 rdataset, sigrdataset);
2571 if (result == ISC_R_SUCCESS) {
2572 if (zfname != NULL &&
2573 !dns_name_issubdomain(fname, zfname)) {
2575 * We found a zonecut in the cache, but our
2576 * zone delegation is better.
2578 use_zone = ISC_TRUE;
2580 } else if (result == ISC_R_NOTFOUND && zfname != NULL) {
2582 * We didn't find anything in the cache, but we
2583 * have a zone delegation, so use it.
2585 use_zone = ISC_TRUE;
2591 query_releasename(client, &fname);
2595 * We've already done query_keepname() on
2596 * zfname, so we must set dbuf to NULL to
2597 * prevent query_addrrset() from trying to
2598 * call query_keepname() again.
2601 query_putrdataset(client, &rdataset);
2602 if (sigrdataset != NULL)
2603 query_putrdataset(client, &sigrdataset);
2604 rdataset = zrdataset;
2606 sigrdataset = zsigrdataset;
2607 zsigrdataset = NULL;
2611 * Attempt to validate RRsets that are pending or that are glue.
2613 if ((DNS_TRUST_PENDING(rdataset->trust) ||
2614 (sigrdataset != NULL && DNS_TRUST_PENDING(sigrdataset->trust)))
2615 && !validate(client, db, fname, rdataset, sigrdataset) &&
2616 !PENDINGOK(client->query.dboptions))
2619 if ((DNS_TRUST_GLUE(rdataset->trust) ||
2620 (sigrdataset != NULL && DNS_TRUST_GLUE(sigrdataset->trust))) &&
2621 !validate(client, db, fname, rdataset, sigrdataset) &&
2622 SECURE(client) && WANTDNSSEC(client))
2626 * If the client doesn't want DNSSEC we can discard the sigrdataset
2629 if (!WANTDNSSEC(client))
2630 query_putrdataset(client, &sigrdataset);
2631 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2632 DNS_SECTION_AUTHORITY);
2635 if (rdataset != NULL)
2636 query_putrdataset(client, &rdataset);
2637 if (sigrdataset != NULL)
2638 query_putrdataset(client, &sigrdataset);
2640 query_releasename(client, &fname);
2642 dns_db_detachnode(db, &node);
2646 dns_zone_detach(&zone);
2648 query_putrdataset(client, &zrdataset);
2649 if (zsigrdataset != NULL)
2650 query_putrdataset(client, &zsigrdataset);
2652 query_releasename(client, &zfname);
2653 dns_db_detach(&zdb);
2658 fixrdataset(ns_client_t *client, dns_rdataset_t **rdataset) {
2659 if (*rdataset == NULL)
2660 *rdataset = query_newrdataset(client);
2661 else if (dns_rdataset_isassociated(*rdataset))
2662 dns_rdataset_disassociate(*rdataset);
2666 fixfname(ns_client_t *client, dns_name_t **fname, isc_buffer_t **dbuf,
2669 if (*fname == NULL) {
2670 *dbuf = query_getnamebuf(client);
2673 *fname = query_newname(client, *dbuf, nbuf);
2678 query_addds(ns_client_t *client, dns_db_t *db, dns_dbnode_t *node,
2679 dns_dbversion_t *version, dns_name_t *name)
2681 dns_fixedname_t fixed;
2682 dns_name_t *fname = NULL;
2684 dns_rdataset_t *rdataset, *sigrdataset;
2685 isc_buffer_t *dbuf, b;
2686 isc_result_t result;
2689 CTRACE("query_addds");
2695 * We'll need some resources...
2697 rdataset = query_newrdataset(client);
2698 sigrdataset = query_newrdataset(client);
2699 if (rdataset == NULL || sigrdataset == NULL)
2703 * Look for the DS record, which may or may not be present.
2705 result = dns_db_findrdataset(db, node, version, dns_rdatatype_ds, 0,
2706 client->now, rdataset, sigrdataset);
2708 * If we didn't find it, look for an NSEC.
2710 if (result == ISC_R_NOTFOUND)
2711 result = dns_db_findrdataset(db, node, version,
2712 dns_rdatatype_nsec, 0, client->now,
2713 rdataset, sigrdataset);
2714 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
2716 if (!dns_rdataset_isassociated(rdataset) ||
2717 !dns_rdataset_isassociated(sigrdataset))
2721 * We've already added the NS record, so if the name's not there,
2722 * we have other problems. Use this name rather than calling
2725 result = dns_message_firstname(client->message, DNS_SECTION_AUTHORITY);
2726 if (result != ISC_R_SUCCESS)
2730 dns_message_currentname(client->message, DNS_SECTION_AUTHORITY,
2732 result = dns_message_findtype(rname, dns_rdatatype_ns, 0, NULL);
2733 if (result != ISC_R_SUCCESS)
2736 ISC_LIST_APPEND(rname->list, rdataset, link);
2737 ISC_LIST_APPEND(rname->list, sigrdataset, link);
2743 if (!dns_db_iszone(db))
2746 * Add the NSEC3 which proves the DS does not exist.
2748 dbuf = query_getnamebuf(client);
2751 fname = query_newname(client, dbuf, &b);
2752 dns_fixedname_init(&fixed);
2753 if (dns_rdataset_isassociated(rdataset))
2754 dns_rdataset_disassociate(rdataset);
2755 if (dns_rdataset_isassociated(sigrdataset))
2756 dns_rdataset_disassociate(sigrdataset);
2757 query_findclosestnsec3(name, db, version, client, rdataset,
2758 sigrdataset, fname, ISC_TRUE,
2759 dns_fixedname_name(&fixed));
2760 if (!dns_rdataset_isassociated(rdataset))
2762 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2763 DNS_SECTION_AUTHORITY);
2765 * Did we find the closest provable encloser instead?
2766 * If so add the nearest to the closest provable encloser.
2768 if (!dns_name_equal(name, dns_fixedname_name(&fixed))) {
2769 count = dns_name_countlabels(dns_fixedname_name(&fixed)) + 1;
2770 dns_name_getlabelsequence(name,
2771 dns_name_countlabels(name) - count,
2772 count, dns_fixedname_name(&fixed));
2773 fixfname(client, &fname, &dbuf, &b);
2774 fixrdataset(client, &rdataset);
2775 fixrdataset(client, &sigrdataset);
2776 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2778 query_findclosestnsec3(dns_fixedname_name(&fixed), db, version,
2779 client, rdataset, sigrdataset, fname,
2781 if (!dns_rdataset_isassociated(rdataset))
2783 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2784 DNS_SECTION_AUTHORITY);
2788 if (rdataset != NULL)
2789 query_putrdataset(client, &rdataset);
2790 if (sigrdataset != NULL)
2791 query_putrdataset(client, &sigrdataset);
2793 query_releasename(client, &fname);
2797 query_addwildcardproof(ns_client_t *client, dns_db_t *db,
2798 dns_dbversion_t *version, dns_name_t *name,
2799 isc_boolean_t ispositive)
2801 isc_buffer_t *dbuf, b;
2803 dns_rdataset_t *rdataset, *sigrdataset;
2804 dns_fixedname_t wfixed;
2807 unsigned int options;
2808 unsigned int olabels, nlabels, labels;
2809 isc_result_t result;
2810 dns_rdata_t rdata = DNS_RDATA_INIT;
2811 dns_rdata_nsec_t nsec;
2812 isc_boolean_t have_wname;
2814 dns_fixedname_t cfixed;
2817 CTRACE("query_addwildcardproof");
2824 * Get the NOQNAME proof then if !ispositive
2825 * get the NOWILDCARD proof.
2827 * DNS_DBFIND_NOWILD finds the NSEC records that covers the
2828 * name ignoring any wildcard. From the owner and next names
2829 * of this record you can compute which wildcard (if it exists)
2830 * will match by finding the longest common suffix of the
2831 * owner name and next names with the qname and prefixing that
2832 * with the wildcard label.
2837 * example NSEC b.example
2839 * b.example NSEC a.d.example
2841 * a.d.example NSEC g.f.example
2843 * g.f.example NSEC z.i.example
2845 * z.i.example NSEC example
2848 * a.example -> example NSEC b.example
2849 * owner common example
2850 * next common example
2852 * d.b.example -> b.example NSEC a.d.example
2853 * owner common b.example
2854 * next common example
2856 * a.f.example -> a.d.example NSEC g.f.example
2857 * owner common example
2858 * next common f.example
2860 * j.example -> z.i.example NSEC example
2861 * owner common example
2862 * next common example
2865 options = client->query.dboptions | DNS_DBFIND_NOWILD;
2866 dns_fixedname_init(&wfixed);
2867 wname = dns_fixedname_name(&wfixed);
2869 have_wname = ISC_FALSE;
2871 * We'll need some resources...
2873 dbuf = query_getnamebuf(client);
2876 fname = query_newname(client, dbuf, &b);
2877 rdataset = query_newrdataset(client);
2878 sigrdataset = query_newrdataset(client);
2879 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2882 result = dns_db_find(db, name, version, dns_rdatatype_nsec, options,
2883 0, &node, fname, rdataset, sigrdataset);
2885 dns_db_detachnode(db, &node);
2887 if (!dns_rdataset_isassociated(rdataset)) {
2889 * No NSEC proof available, return NSEC3 proofs instead.
2891 dns_fixedname_init(&cfixed);
2892 cname = dns_fixedname_name(&cfixed);
2894 * Find the closest encloser.
2896 dns_name_copy(name, cname, NULL);
2897 while (result == DNS_R_NXDOMAIN) {
2898 labels = dns_name_countlabels(cname) - 1;
2899 dns_name_split(cname, labels, NULL, cname);
2900 result = dns_db_find(db, cname, version,
2902 options, 0, NULL, fname,
2906 * Add closest (provable) encloser NSEC3.
2908 query_findclosestnsec3(cname, db, NULL, client, rdataset,
2909 sigrdataset, fname, ISC_TRUE, cname);
2910 if (!dns_rdataset_isassociated(rdataset))
2912 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2913 dbuf, DNS_SECTION_AUTHORITY);
2916 * Replace resources which were consumed by query_addrrset.
2918 if (fname == NULL) {
2919 dbuf = query_getnamebuf(client);
2922 fname = query_newname(client, dbuf, &b);
2925 if (rdataset == NULL)
2926 rdataset = query_newrdataset(client);
2927 else if (dns_rdataset_isassociated(rdataset))
2928 dns_rdataset_disassociate(rdataset);
2930 if (sigrdataset == NULL)
2931 sigrdataset = query_newrdataset(client);
2932 else if (dns_rdataset_isassociated(sigrdataset))
2933 dns_rdataset_disassociate(sigrdataset);
2935 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2938 * Add no qname proof.
2940 labels = dns_name_countlabels(cname) + 1;
2941 if (dns_name_countlabels(name) == labels)
2942 dns_name_copy(name, wname, NULL);
2944 dns_name_split(name, labels, NULL, wname);
2946 query_findclosestnsec3(wname, db, NULL, client, rdataset,
2947 sigrdataset, fname, ISC_FALSE, NULL);
2948 if (!dns_rdataset_isassociated(rdataset))
2950 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2951 dbuf, DNS_SECTION_AUTHORITY);
2957 * Replace resources which were consumed by query_addrrset.
2959 if (fname == NULL) {
2960 dbuf = query_getnamebuf(client);
2963 fname = query_newname(client, dbuf, &b);
2966 if (rdataset == NULL)
2967 rdataset = query_newrdataset(client);
2968 else if (dns_rdataset_isassociated(rdataset))
2969 dns_rdataset_disassociate(rdataset);
2971 if (sigrdataset == NULL)
2972 sigrdataset = query_newrdataset(client);
2973 else if (dns_rdataset_isassociated(sigrdataset))
2974 dns_rdataset_disassociate(sigrdataset);
2976 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2979 * Add the no wildcard proof.
2981 result = dns_name_concatenate(dns_wildcardname,
2982 cname, wname, NULL);
2983 if (result != ISC_R_SUCCESS)
2986 query_findclosestnsec3(wname, db, NULL, client, rdataset,
2987 sigrdataset, fname, ISC_FALSE, NULL);
2988 if (!dns_rdataset_isassociated(rdataset))
2990 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2991 dbuf, DNS_SECTION_AUTHORITY);
2994 } else if (result == DNS_R_NXDOMAIN) {
2996 result = dns_rdataset_first(rdataset);
2997 if (result == ISC_R_SUCCESS) {
2998 dns_rdataset_current(rdataset, &rdata);
2999 result = dns_rdata_tostruct(&rdata, &nsec, NULL);
3001 if (result == ISC_R_SUCCESS) {
3002 (void)dns_name_fullcompare(name, fname, &order,
3004 (void)dns_name_fullcompare(name, &nsec.next, &order,
3007 * Check for a pathological condition created when
3008 * serving some malformed signed zones and bail out.
3010 if (dns_name_countlabels(name) == nlabels)
3013 if (olabels > nlabels)
3014 dns_name_split(name, olabels, NULL, wname);
3016 dns_name_split(name, nlabels, NULL, wname);
3017 result = dns_name_concatenate(dns_wildcardname,
3018 wname, wname, NULL);
3019 if (result == ISC_R_SUCCESS)
3020 have_wname = ISC_TRUE;
3021 dns_rdata_freestruct(&nsec);
3023 query_addrrset(client, &fname, &rdataset, &sigrdataset,
3024 dbuf, DNS_SECTION_AUTHORITY);
3026 if (rdataset != NULL)
3027 query_putrdataset(client, &rdataset);
3028 if (sigrdataset != NULL)
3029 query_putrdataset(client, &sigrdataset);
3031 query_releasename(client, &fname);
3033 ispositive = ISC_TRUE; /* prevent loop */
3034 if (!dns_name_equal(name, wname)) {
3040 if (rdataset != NULL)
3041 query_putrdataset(client, &rdataset);
3042 if (sigrdataset != NULL)
3043 query_putrdataset(client, &sigrdataset);
3045 query_releasename(client, &fname);
3049 query_addnxrrsetnsec(ns_client_t *client, dns_db_t *db,
3050 dns_dbversion_t *version, dns_name_t **namep,
3051 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp)
3054 dns_rdataset_t *sigrdataset;
3055 dns_rdata_t sigrdata;
3056 dns_rdata_rrsig_t sig;
3057 unsigned int labels;
3058 isc_buffer_t *dbuf, b;
3060 isc_result_t result;
3063 if ((name->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
3064 query_addrrset(client, namep, rdatasetp, sigrdatasetp,
3065 NULL, DNS_SECTION_AUTHORITY);
3069 if (sigrdatasetp == NULL)
3072 sigrdataset = *sigrdatasetp;
3073 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
3075 result = dns_rdataset_first(sigrdataset);
3076 if (result != ISC_R_SUCCESS)
3078 dns_rdata_init(&sigrdata);
3079 dns_rdataset_current(sigrdataset, &sigrdata);
3080 result = dns_rdata_tostruct(&sigrdata, &sig, NULL);
3081 if (result != ISC_R_SUCCESS)
3084 labels = dns_name_countlabels(name);
3085 if ((unsigned int)sig.labels + 1 >= labels)
3089 query_addwildcardproof(client, db, version, client->query.qname,
3093 * We'll need some resources...
3095 dbuf = query_getnamebuf(client);
3098 fname = query_newname(client, dbuf, &b);
3101 dns_name_split(name, sig.labels + 1, NULL, fname);
3102 /* This will succeed, since we've stripped labels. */
3103 RUNTIME_CHECK(dns_name_concatenate(dns_wildcardname, fname, fname,
3104 NULL) == ISC_R_SUCCESS);
3105 query_addrrset(client, &fname, rdatasetp, sigrdatasetp,
3106 dbuf, DNS_SECTION_AUTHORITY);
3110 query_resume(isc_task_t *task, isc_event_t *event) {
3111 dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
3113 ns_client_t *client;
3114 isc_boolean_t fetch_canceled, client_shuttingdown;
3115 isc_result_t result;
3116 isc_logcategory_t *logcategory = NS_LOGCATEGORY_QUERY_EERRORS;
3120 * Resume a query after recursion.
3125 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
3126 client = devent->ev_arg;
3127 REQUIRE(NS_CLIENT_VALID(client));
3128 REQUIRE(task == client->task);
3129 REQUIRE(RECURSING(client));
3131 LOCK(&client->query.fetchlock);
3132 if (client->query.fetch != NULL) {
3134 * This is the fetch we've been waiting for.
3136 INSIST(devent->fetch == client->query.fetch);
3137 client->query.fetch = NULL;
3138 fetch_canceled = ISC_FALSE;
3140 * Update client->now.
3142 isc_stdtime_get(&client->now);
3145 * This is a fetch completion event for a canceled fetch.
3146 * Clean up and don't resume the find.
3148 fetch_canceled = ISC_TRUE;
3150 UNLOCK(&client->query.fetchlock);
3151 INSIST(client->query.fetch == NULL);
3153 client->query.attributes &= ~NS_QUERYATTR_RECURSING;
3154 fetch = devent->fetch;
3155 devent->fetch = NULL;
3158 * If this client is shutting down, or this transaction
3159 * has timed out, do not resume the find.
3161 client_shuttingdown = ns_client_shuttingdown(client);
3162 if (fetch_canceled || client_shuttingdown) {
3163 if (devent->node != NULL)
3164 dns_db_detachnode(devent->db, &devent->node);
3165 if (devent->db != NULL)
3166 dns_db_detach(&devent->db);
3167 query_putrdataset(client, &devent->rdataset);
3168 if (devent->sigrdataset != NULL)
3169 query_putrdataset(client, &devent->sigrdataset);
3170 isc_event_free(&event);
3172 query_error(client, DNS_R_SERVFAIL, __LINE__);
3174 query_next(client, ISC_R_CANCELED);
3176 * This may destroy the client.
3178 ns_client_detach(&client);
3180 result = query_find(client, devent, 0);
3181 if (result != ISC_R_SUCCESS) {
3182 if (result == DNS_R_SERVFAIL)
3183 errorloglevel = ISC_LOG_DEBUG(2);
3185 errorloglevel = ISC_LOG_DEBUG(4);
3186 if (isc_log_wouldlog(ns_g_lctx, errorloglevel)) {
3187 dns_resolver_logfetch(fetch, ns_g_lctx,
3190 errorloglevel, ISC_FALSE);
3195 dns_resolver_destroyfetch(&fetch);
3199 query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qdomain,
3200 dns_rdataset_t *nameservers, isc_boolean_t resuming)
3202 isc_result_t result;
3203 dns_rdataset_t *rdataset, *sigrdataset;
3204 isc_sockaddr_t *peeraddr;
3207 inc_stats(client, dns_nsstatscounter_recursion);
3210 * We are about to recurse, which means that this client will
3211 * be unavailable for serving new requests for an indeterminate
3212 * amount of time. If this client is currently responsible
3213 * for handling incoming queries, set up a new client
3214 * object to handle them while we are waiting for a
3215 * response. There is no need to replace TCP clients
3216 * because those have already been replaced when the
3217 * connection was accepted (if allowed by the TCP quota).
3219 if (client->recursionquota == NULL) {
3220 result = isc_quota_attach(&ns_g_server->recursionquota,
3221 &client->recursionquota);
3222 if (result == ISC_R_SOFTQUOTA) {
3223 static isc_stdtime_t last = 0;
3225 isc_stdtime_get(&now);
3228 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3231 "recursive-clients soft limit "
3232 "exceeded, aborting oldest query");
3234 ns_client_killoldestquery(client);
3235 result = ISC_R_SUCCESS;
3236 } else if (result == ISC_R_QUOTA) {
3237 static isc_stdtime_t last = 0;
3239 isc_stdtime_get(&now);
3242 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3245 "no more recursive clients: %s",
3246 isc_result_totext(result));
3248 ns_client_killoldestquery(client);
3250 if (result == ISC_R_SUCCESS && !client->mortal &&
3251 (client->attributes & NS_CLIENTATTR_TCP) == 0) {
3252 result = ns_client_replace(client);
3253 if (result != ISC_R_SUCCESS) {
3254 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3257 "ns_client_replace() failed: %s",
3258 isc_result_totext(result));
3259 isc_quota_detach(&client->recursionquota);
3262 if (result != ISC_R_SUCCESS)
3264 ns_client_recursing(client);
3268 * Invoke the resolver.
3270 REQUIRE(nameservers == NULL || nameservers->type == dns_rdatatype_ns);
3271 REQUIRE(client->query.fetch == NULL);
3273 rdataset = query_newrdataset(client);
3274 if (rdataset == NULL)
3275 return (ISC_R_NOMEMORY);
3276 if (WANTDNSSEC(client)) {
3277 sigrdataset = query_newrdataset(client);
3278 if (sigrdataset == NULL) {
3279 query_putrdataset(client, &rdataset);
3280 return (ISC_R_NOMEMORY);
3285 if (client->query.timerset == ISC_FALSE)
3286 ns_client_settimeout(client, 60);
3287 if ((client->attributes & NS_CLIENTATTR_TCP) == 0)
3288 peeraddr = &client->peeraddr;
3291 result = dns_resolver_createfetch2(client->view->resolver,
3292 client->query.qname,
3293 qtype, qdomain, nameservers,
3294 NULL, peeraddr, client->message->id,
3295 client->query.fetchoptions,
3297 query_resume, client,
3298 rdataset, sigrdataset,
3299 &client->query.fetch);
3301 if (result == ISC_R_SUCCESS) {
3303 * Record that we're waiting for an event. A client which
3304 * is shutting down will not be destroyed until all the
3305 * events have been received.
3308 query_putrdataset(client, &rdataset);
3309 if (sigrdataset != NULL)
3310 query_putrdataset(client, &sigrdataset);
3316 #define MAX_RESTARTS 16
3318 #define QUERY_ERROR(r) \
3321 want_restart = ISC_FALSE; \
3325 #define RECURSE_ERROR(r) \
3327 if ((r) == DNS_R_DUPLICATE || (r) == DNS_R_DROP) \
3330 QUERY_ERROR(DNS_R_SERVFAIL); \
3334 * Extract a network address from the RDATA of an A or AAAA
3339 * ISC_R_NOTIMPLEMENTED The rdata is not a known address type.
3342 rdata_tonetaddr(const dns_rdata_t *rdata, isc_netaddr_t *netaddr) {
3344 struct in6_addr in6a;
3346 switch (rdata->type) {
3347 case dns_rdatatype_a:
3348 INSIST(rdata->length == 4);
3349 memcpy(&ina.s_addr, rdata->data, 4);
3350 isc_netaddr_fromin(netaddr, &ina);
3351 return (ISC_R_SUCCESS);
3352 case dns_rdatatype_aaaa:
3353 INSIST(rdata->length == 16);
3354 memcpy(in6a.s6_addr, rdata->data, 16);
3355 isc_netaddr_fromin6(netaddr, &in6a);
3356 return (ISC_R_SUCCESS);
3358 return (ISC_R_NOTIMPLEMENTED);
3363 * Find the sort order of 'rdata' in the topology-like
3364 * ACL forming the second element in a 2-element top-level
3365 * sortlist statement.
3368 query_sortlist_order_2element(const dns_rdata_t *rdata, const void *arg) {
3369 isc_netaddr_t netaddr;
3371 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3373 return (ns_sortlist_addrorder2(&netaddr, arg));
3377 * Find the sort order of 'rdata' in the matching element
3378 * of a 1-element top-level sortlist statement.
3381 query_sortlist_order_1element(const dns_rdata_t *rdata, const void *arg) {
3382 isc_netaddr_t netaddr;
3384 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3386 return (ns_sortlist_addrorder1(&netaddr, arg));
3390 * Find the sortlist statement that applies to 'client' and set up
3391 * the sortlist info in in client->message appropriately.
3394 setup_query_sortlist(ns_client_t *client) {
3395 isc_netaddr_t netaddr;
3396 dns_rdatasetorderfunc_t order = NULL;
3397 const void *order_arg = NULL;
3399 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
3400 switch (ns_sortlist_setup(client->view->sortlist,
3401 &netaddr, &order_arg)) {
3402 case NS_SORTLISTTYPE_1ELEMENT:
3403 order = query_sortlist_order_1element;
3405 case NS_SORTLISTTYPE_2ELEMENT:
3406 order = query_sortlist_order_2element;
3408 case NS_SORTLISTTYPE_NONE:
3415 dns_message_setsortorder(client->message, order, order_arg);
3419 query_addnoqnameproof(ns_client_t *client, dns_rdataset_t *rdataset) {
3420 isc_buffer_t *dbuf, b;
3422 dns_rdataset_t *neg, *negsig;
3423 isc_result_t result = ISC_R_NOMEMORY;
3425 CTRACE("query_addnoqnameproof");
3431 dbuf = query_getnamebuf(client);
3434 fname = query_newname(client, dbuf, &b);
3435 neg = query_newrdataset(client);
3436 negsig = query_newrdataset(client);
3437 if (fname == NULL || neg == NULL || negsig == NULL)
3440 result = dns_rdataset_getnoqname(rdataset, fname, neg, negsig);
3441 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3443 query_addrrset(client, &fname, &neg, &negsig, dbuf,
3444 DNS_SECTION_AUTHORITY);
3446 if ((rdataset->attributes & DNS_RDATASETATTR_CLOSEST) == 0)
3449 if (fname == NULL) {
3450 dbuf = query_getnamebuf(client);
3453 fname = query_newname(client, dbuf, &b);
3456 neg = query_newrdataset(client);
3457 else if (dns_rdataset_isassociated(neg))
3458 dns_rdataset_disassociate(neg);
3460 negsig = query_newrdataset(client);
3461 else if (dns_rdataset_isassociated(negsig))
3462 dns_rdataset_disassociate(negsig);
3463 if (fname == NULL || neg == NULL || negsig == NULL)
3465 result = dns_rdataset_getclosest(rdataset, fname, neg, negsig);
3466 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3468 query_addrrset(client, &fname, &neg, &negsig, dbuf,
3469 DNS_SECTION_AUTHORITY);
3473 query_putrdataset(client, &neg);
3475 query_putrdataset(client, &negsig);
3477 query_releasename(client, &fname);
3481 answer_in_glue(ns_client_t *client, dns_rdatatype_t qtype) {
3484 dns_section_t section = DNS_SECTION_ADDITIONAL;
3485 dns_rdataset_t *rdataset = NULL;
3487 msg = client->message;
3488 for (name = ISC_LIST_HEAD(msg->sections[section]);
3490 name = ISC_LIST_NEXT(name, link))
3491 if (dns_name_equal(name, client->query.qname)) {
3492 for (rdataset = ISC_LIST_HEAD(name->list);
3494 rdataset = ISC_LIST_NEXT(rdataset, link))
3495 if (rdataset->type == qtype)
3499 if (rdataset != NULL) {
3500 ISC_LIST_UNLINK(msg->sections[section], name, link);
3501 ISC_LIST_PREPEND(msg->sections[section], name, link);
3502 ISC_LIST_UNLINK(name->list, rdataset, link);
3503 ISC_LIST_PREPEND(name->list, rdataset, link);
3504 rdataset->attributes |= DNS_RDATASETATTR_REQUIREDGLUE;
3508 #define NS_NAME_INIT(A,B) \
3511 A, sizeof(A), sizeof(B), \
3512 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE, \
3513 B, NULL, { (void *)-1, (void *)-1}, \
3517 static unsigned char inaddr10_offsets[] = { 0, 3, 11, 16 };
3518 static unsigned char inaddr172_offsets[] = { 0, 3, 7, 15, 20 };
3519 static unsigned char inaddr192_offsets[] = { 0, 4, 8, 16, 21 };
3521 static unsigned char inaddr10[] = "\00210\007IN-ADDR\004ARPA";
3523 static unsigned char inaddr16172[] = "\00216\003172\007IN-ADDR\004ARPA";
3524 static unsigned char inaddr17172[] = "\00217\003172\007IN-ADDR\004ARPA";
3525 static unsigned char inaddr18172[] = "\00218\003172\007IN-ADDR\004ARPA";
3526 static unsigned char inaddr19172[] = "\00219\003172\007IN-ADDR\004ARPA";
3527 static unsigned char inaddr20172[] = "\00220\003172\007IN-ADDR\004ARPA";
3528 static unsigned char inaddr21172[] = "\00221\003172\007IN-ADDR\004ARPA";
3529 static unsigned char inaddr22172[] = "\00222\003172\007IN-ADDR\004ARPA";
3530 static unsigned char inaddr23172[] = "\00223\003172\007IN-ADDR\004ARPA";
3531 static unsigned char inaddr24172[] = "\00224\003172\007IN-ADDR\004ARPA";
3532 static unsigned char inaddr25172[] = "\00225\003172\007IN-ADDR\004ARPA";
3533 static unsigned char inaddr26172[] = "\00226\003172\007IN-ADDR\004ARPA";
3534 static unsigned char inaddr27172[] = "\00227\003172\007IN-ADDR\004ARPA";
3535 static unsigned char inaddr28172[] = "\00228\003172\007IN-ADDR\004ARPA";
3536 static unsigned char inaddr29172[] = "\00229\003172\007IN-ADDR\004ARPA";
3537 static unsigned char inaddr30172[] = "\00230\003172\007IN-ADDR\004ARPA";
3538 static unsigned char inaddr31172[] = "\00231\003172\007IN-ADDR\004ARPA";
3540 static unsigned char inaddr168192[] = "\003168\003192\007IN-ADDR\004ARPA";
3542 static dns_name_t rfc1918names[] = {
3543 NS_NAME_INIT(inaddr10, inaddr10_offsets),
3544 NS_NAME_INIT(inaddr16172, inaddr172_offsets),
3545 NS_NAME_INIT(inaddr17172, inaddr172_offsets),
3546 NS_NAME_INIT(inaddr18172, inaddr172_offsets),
3547 NS_NAME_INIT(inaddr19172, inaddr172_offsets),
3548 NS_NAME_INIT(inaddr20172, inaddr172_offsets),
3549 NS_NAME_INIT(inaddr21172, inaddr172_offsets),
3550 NS_NAME_INIT(inaddr22172, inaddr172_offsets),
3551 NS_NAME_INIT(inaddr23172, inaddr172_offsets),
3552 NS_NAME_INIT(inaddr24172, inaddr172_offsets),
3553 NS_NAME_INIT(inaddr25172, inaddr172_offsets),
3554 NS_NAME_INIT(inaddr26172, inaddr172_offsets),
3555 NS_NAME_INIT(inaddr27172, inaddr172_offsets),
3556 NS_NAME_INIT(inaddr28172, inaddr172_offsets),
3557 NS_NAME_INIT(inaddr29172, inaddr172_offsets),
3558 NS_NAME_INIT(inaddr30172, inaddr172_offsets),
3559 NS_NAME_INIT(inaddr31172, inaddr172_offsets),
3560 NS_NAME_INIT(inaddr168192, inaddr192_offsets)
3564 static unsigned char prisoner_data[] = "\010prisoner\004iana\003org";
3565 static unsigned char hostmaster_data[] = "\012hostmaster\014root-servers\003org";
3567 static unsigned char prisoner_offsets[] = { 0, 9, 14, 18 };
3568 static unsigned char hostmaster_offsets[] = { 0, 11, 24, 28 };
3570 static dns_name_t prisoner = NS_NAME_INIT(prisoner_data, prisoner_offsets);
3571 static dns_name_t hostmaster = NS_NAME_INIT(hostmaster_data, hostmaster_offsets);
3574 warn_rfc1918(ns_client_t *client, dns_name_t *fname, dns_rdataset_t *rdataset) {
3576 dns_rdata_t rdata = DNS_RDATA_INIT;
3577 dns_rdata_soa_t soa;
3578 dns_rdataset_t found;
3579 isc_result_t result;
3581 for (i = 0; i < (sizeof(rfc1918names)/sizeof(*rfc1918names)); i++) {
3582 if (dns_name_issubdomain(fname, &rfc1918names[i])) {
3583 dns_rdataset_init(&found);
3584 result = dns_ncache_getrdataset(rdataset,
3588 if (result != ISC_R_SUCCESS)
3591 result = dns_rdataset_first(&found);
3592 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3593 dns_rdataset_current(&found, &rdata);
3594 result = dns_rdata_tostruct(&rdata, &soa, NULL);
3595 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3596 if (dns_name_equal(&soa.origin, &prisoner) &&
3597 dns_name_equal(&soa.contact, &hostmaster)) {
3598 char buf[DNS_NAME_FORMATSIZE];
3599 dns_name_format(fname, buf, sizeof(buf));
3600 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3603 "RFC 1918 response from "
3604 "Internet for %s", buf);
3606 dns_rdataset_disassociate(&found);
3613 query_findclosestnsec3(dns_name_t *qname, dns_db_t *db,
3614 dns_dbversion_t *version, ns_client_t *client,
3615 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3616 dns_name_t *fname, isc_boolean_t exact,
3619 unsigned char salt[256];
3621 isc_uint16_t iterations;
3622 isc_result_t result;
3623 unsigned int dboptions;
3624 dns_fixedname_t fixed;
3629 dns_rdata_nsec3_t nsec3;
3630 dns_rdata_t rdata = DNS_RDATA_INIT;
3631 isc_boolean_t optout;
3633 salt_length = sizeof(salt);
3634 result = dns_db_getnsec3parameters(db, version, &hash, NULL,
3635 &iterations, salt, &salt_length);
3636 if (result != ISC_R_SUCCESS)
3639 dns_name_init(&name, NULL);
3640 dns_name_clone(qname, &name);
3643 * Map unknown algorithm to known value.
3645 if (hash == DNS_NSEC3_UNKNOWNALG)
3649 dns_fixedname_init(&fixed);
3650 result = dns_nsec3_hashname(&fixed, NULL, NULL, &name,
3651 dns_db_origin(db), hash,
3652 iterations, salt, salt_length);
3653 if (result != ISC_R_SUCCESS)
3656 dboptions = client->query.dboptions | DNS_DBFIND_FORCENSEC3;
3657 result = dns_db_find(db, dns_fixedname_name(&fixed), version,
3658 dns_rdatatype_nsec3, dboptions, client->now,
3659 NULL, fname, rdataset, sigrdataset);
3661 if (result == DNS_R_NXDOMAIN) {
3662 if (!dns_rdataset_isassociated(rdataset)) {
3665 result = dns_rdataset_first(rdataset);
3666 INSIST(result == ISC_R_SUCCESS);
3667 dns_rdataset_current(rdataset, &rdata);
3668 dns_rdata_tostruct(&rdata, &nsec3, NULL);
3669 dns_rdata_reset(&rdata);
3670 optout = ISC_TF((nsec3.flags & DNS_NSEC3FLAG_OPTOUT) != 0);
3671 if (found != NULL && optout &&
3672 dns_name_fullcompare(&name, dns_db_origin(db), &order,
3673 &count) == dns_namereln_subdomain) {
3674 dns_rdataset_disassociate(rdataset);
3675 if (dns_rdataset_isassociated(sigrdataset))
3676 dns_rdataset_disassociate(sigrdataset);
3677 count = dns_name_countlabels(&name) - 1;
3678 dns_name_getlabelsequence(&name, 1, count, &name);
3679 ns_client_log(client, DNS_LOGCATEGORY_DNSSEC,
3680 NS_LOGMODULE_QUERY, ISC_LOG_DEBUG(3),
3681 "looking for closest provable encloser");
3685 ns_client_log(client, DNS_LOGCATEGORY_DNSSEC,
3686 NS_LOGMODULE_QUERY, ISC_LOG_WARNING,
3687 "expected a exact match NSEC3, got "
3688 "a covering record");
3690 } else if (result != ISC_R_SUCCESS) {
3693 ns_client_log(client, DNS_LOGCATEGORY_DNSSEC,
3694 NS_LOGMODULE_QUERY, ISC_LOG_WARNING,
3695 "expected covering NSEC3, got an exact match");
3697 dns_name_copy(&name, found, NULL);
3702 * Do the bulk of query processing for the current query of 'client'.
3703 * If 'event' is non-NULL, we are returning from recursion and 'qtype'
3704 * is ignored. Otherwise, 'qtype' is the query type.
3707 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
3711 dns_rdatatype_t type;
3712 dns_name_t *fname, *zfname, *tname, *prefix;
3713 dns_rdataset_t *rdataset, *trdataset;
3714 dns_rdataset_t *sigrdataset, *zrdataset, *zsigrdataset;
3715 dns_rdataset_t **sigrdatasetp;
3716 dns_rdata_t rdata = DNS_RDATA_INIT;
3717 dns_rdatasetiter_t *rdsiter;
3718 isc_boolean_t want_restart, authoritative, is_zone, need_wildcardproof;
3719 unsigned int n, nlabels;
3720 dns_namereln_t namereln;
3724 isc_result_t result, eresult;
3725 dns_fixedname_t fixed;
3726 dns_fixedname_t wildcardname;
3727 dns_dbversion_t *version, *zversion;
3729 dns_rdata_cname_t cname;
3730 dns_rdata_dname_t dname;
3731 unsigned int options;
3732 isc_boolean_t empty_wild;
3733 dns_rdataset_t *noqname;
3734 isc_boolean_t resuming;
3737 CTRACE("query_find");
3740 * One-time initialization.
3742 * It's especially important to initialize anything that the cleanup
3743 * code might cleanup.
3746 eresult = ISC_R_SUCCESS;
3752 zsigrdataset = NULL;
3759 need_wildcardproof = ISC_FALSE;
3760 empty_wild = ISC_FALSE;
3762 resuming = ISC_FALSE;
3763 is_zone = ISC_FALSE;
3765 if (event != NULL) {
3767 * We're returning from recursion. Restore the query context
3771 want_restart = ISC_FALSE;
3772 authoritative = ISC_FALSE;
3774 qtype = event->qtype;
3775 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3776 type = dns_rdatatype_any;
3781 rdataset = event->rdataset;
3782 sigrdataset = event->sigrdataset;
3785 * We'll need some resources...
3787 dbuf = query_getnamebuf(client);
3789 QUERY_ERROR(DNS_R_SERVFAIL);
3792 fname = query_newname(client, dbuf, &b);
3793 if (fname == NULL) {
3794 QUERY_ERROR(DNS_R_SERVFAIL);
3797 tname = dns_fixedname_name(&event->foundname);
3798 result = dns_name_copy(tname, fname, NULL);
3799 if (result != ISC_R_SUCCESS) {
3800 QUERY_ERROR(DNS_R_SERVFAIL);
3804 result = event->result;
3805 resuming = ISC_TRUE;
3811 * Not returning from recursion.
3815 * If it's a SIG query, we'll iterate the node.
3817 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3818 type = dns_rdatatype_any;
3823 CTRACE("query_find: restart");
3824 want_restart = ISC_FALSE;
3825 authoritative = ISC_FALSE;
3827 need_wildcardproof = ISC_FALSE;
3829 if (client->view->checknames &&
3830 !dns_rdata_checkowner(client->query.qname,
3831 client->message->rdclass,
3832 qtype, ISC_FALSE)) {
3833 char namebuf[DNS_NAME_FORMATSIZE];
3834 char typename[DNS_RDATATYPE_FORMATSIZE];
3835 char classname[DNS_RDATACLASS_FORMATSIZE];
3837 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
3838 dns_rdatatype_format(qtype, typename, sizeof(typename));
3839 dns_rdataclass_format(client->message->rdclass, classname,
3841 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3842 NS_LOGMODULE_QUERY, ISC_LOG_ERROR,
3843 "check-names failure %s/%s/%s", namebuf,
3844 typename, classname);
3845 QUERY_ERROR(DNS_R_REFUSED);
3850 * First we must find the right database.
3852 options &= DNS_GETDB_NOLOG; /* Preserve DNS_GETDB_NOLOG. */
3853 if (dns_rdatatype_atparent(qtype) &&
3854 !dns_name_equal(client->query.qname, dns_rootname))
3855 options |= DNS_GETDB_NOEXACT;
3856 result = query_getdb(client, client->query.qname, qtype, options,
3857 &zone, &db, &version, &is_zone);
3858 if ((result != ISC_R_SUCCESS || !is_zone) && !RECURSIONOK(client) &&
3859 (options & DNS_GETDB_NOEXACT) != 0 && qtype == dns_rdatatype_ds) {
3861 * Look to see if we are authoritative for the
3862 * child zone if the query type is DS.
3864 dns_db_t *tdb = NULL;
3865 dns_zone_t *tzone = NULL;
3866 dns_dbversion_t *tversion = NULL;
3867 isc_result_t tresult;
3869 tresult = query_getzonedb(client, client->query.qname, qtype,
3870 DNS_GETDB_PARTIAL, &tzone, &tdb,
3872 if (tresult == ISC_R_SUCCESS) {
3873 options &= ~DNS_GETDB_NOEXACT;
3874 query_putrdataset(client, &rdataset);
3878 dns_zone_detach(&zone);
3883 result = ISC_R_SUCCESS;
3886 dns_db_detach(&tdb);
3888 dns_zone_detach(&tzone);
3891 if (result != ISC_R_SUCCESS) {
3892 if (result == DNS_R_REFUSED) {
3893 if (WANTRECURSION(client)) {
3895 dns_nsstatscounter_recurserej);
3897 inc_stats(client, dns_nsstatscounter_authrej);
3898 if (!PARTIALANSWER(client))
3899 QUERY_ERROR(DNS_R_REFUSED);
3901 QUERY_ERROR(DNS_R_SERVFAIL);
3906 authoritative = ISC_TRUE;
3908 if (event == NULL && client->query.restarts == 0) {
3913 * if is_zone = true, zone = NULL then this is
3914 * a DLZ zone. Don't attempt to attach zone.
3917 dns_zone_attach(zone, &client->query.authzone);
3921 dns_db_attach(db, &client->query.authdb);
3923 client->query.authdbset = ISC_TRUE;
3927 CTRACE("query_find: db_find");
3929 * We'll need some resources...
3931 dbuf = query_getnamebuf(client);
3933 QUERY_ERROR(DNS_R_SERVFAIL);
3936 fname = query_newname(client, dbuf, &b);
3937 rdataset = query_newrdataset(client);
3938 if (fname == NULL || rdataset == NULL) {
3939 QUERY_ERROR(DNS_R_SERVFAIL);
3942 if (WANTDNSSEC(client) && (!is_zone || dns_db_issecure(db))) {
3943 sigrdataset = query_newrdataset(client);
3944 if (sigrdataset == NULL) {
3945 QUERY_ERROR(DNS_R_SERVFAIL);
3951 * Now look for an answer in the database.
3953 result = dns_db_find(db, client->query.qname, version, type,
3954 client->query.dboptions, client->now,
3955 &node, fname, rdataset, sigrdataset);
3958 CTRACE("query_find: resume");
3962 * This case is handled in the main line below.
3968 * These cases are handled in the main line below.
3971 authoritative = ISC_FALSE;
3973 case ISC_R_NOTFOUND:
3975 * The cache doesn't even have the root NS. Get them from
3982 if (client->view->hints == NULL) {
3983 /* We have no hints. */
3984 result = ISC_R_FAILURE;
3986 dns_db_attach(client->view->hints, &db);
3987 result = dns_db_find(db, dns_rootname,
3988 NULL, dns_rdatatype_ns,
3989 0, client->now, &node, fname,
3990 rdataset, sigrdataset);
3992 if (result != ISC_R_SUCCESS) {
3994 * Nonsensical root hints may require cleanup.
3996 if (dns_rdataset_isassociated(rdataset))
3997 dns_rdataset_disassociate(rdataset);
3998 if (sigrdataset != NULL &&
3999 dns_rdataset_isassociated(sigrdataset))
4000 dns_rdataset_disassociate(sigrdataset);
4002 dns_db_detachnode(db, &node);
4005 * We don't have any root server hints, but
4006 * we may have working forwarders, so try to
4009 if (RECURSIONOK(client)) {
4010 result = query_recurse(client, qtype,
4011 NULL, NULL, resuming);
4012 if (result == ISC_R_SUCCESS)
4013 client->query.attributes |=
4014 NS_QUERYATTR_RECURSING;
4016 RECURSE_ERROR(result);
4019 /* Unable to give root server referral. */
4020 QUERY_ERROR(DNS_R_SERVFAIL);
4025 * XXXRTH We should trigger root server priming here.
4028 case DNS_R_DELEGATION:
4029 authoritative = ISC_FALSE;
4032 * Look to see if we are authoritative for the
4033 * child zone if the query type is DS.
4035 if (!RECURSIONOK(client) &&
4036 (options & DNS_GETDB_NOEXACT) != 0 &&
4037 qtype == dns_rdatatype_ds) {
4038 dns_db_t *tdb = NULL;
4039 dns_zone_t *tzone = NULL;
4040 dns_dbversion_t *tversion = NULL;
4041 result = query_getzonedb(client,
4042 client->query.qname,
4047 if (result == ISC_R_SUCCESS) {
4048 options &= ~DNS_GETDB_NOEXACT;
4049 query_putrdataset(client, &rdataset);
4050 if (sigrdataset != NULL)
4051 query_putrdataset(client,
4054 query_releasename(client,
4057 dns_db_detachnode(db, &node);
4061 dns_zone_detach(&zone);
4065 authoritative = ISC_TRUE;
4069 dns_db_detach(&tdb);
4071 dns_zone_detach(&tzone);
4074 * We're authoritative for an ancestor of QNAME.
4076 if (!USECACHE(client) || !RECURSIONOK(client)) {
4077 dns_fixedname_t fixed;
4079 dns_fixedname_init(&fixed);
4080 dns_name_copy(fname,
4081 dns_fixedname_name(&fixed), NULL);
4084 * If we don't have a cache, this is the best
4087 * If the client is making a nonrecursive
4088 * query we always give out the authoritative
4089 * delegation. This way even if we get
4090 * junk in our cache, we won't fail in our
4091 * role as the delegating authority if another
4092 * nameserver asks us about a delegated
4095 * We enable the retrieval of glue for this
4096 * database by setting client->query.gluedb.
4098 client->query.gluedb = db;
4099 client->query.isreferral = ISC_TRUE;
4101 * We must ensure NOADDITIONAL is off,
4102 * because the generation of
4103 * additional data is required in
4106 client->query.attributes &=
4107 ~NS_QUERYATTR_NOADDITIONAL;
4108 if (sigrdataset != NULL)
4109 sigrdatasetp = &sigrdataset;
4111 sigrdatasetp = NULL;
4112 query_addrrset(client, &fname,
4113 &rdataset, sigrdatasetp,
4114 dbuf, DNS_SECTION_AUTHORITY);
4115 client->query.gluedb = NULL;
4116 if (WANTDNSSEC(client))
4117 query_addds(client, db, node, version,
4118 dns_fixedname_name(&fixed));
4121 * We might have a better answer or delegation
4122 * in the cache. We'll remember the current
4123 * values of fname, rdataset, and sigrdataset.
4124 * We'll then go looking for QNAME in the
4125 * cache. If we find something better, we'll
4128 query_keepname(client, fname, dbuf);
4132 zrdataset = rdataset;
4134 zsigrdataset = sigrdataset;
4136 dns_db_detachnode(db, &node);
4140 dns_db_attach(client->view->cachedb, &db);
4141 is_zone = ISC_FALSE;
4145 if (zfname != NULL &&
4146 !dns_name_issubdomain(fname, zfname)) {
4148 * We've already got a delegation from
4149 * authoritative data, and it is better
4150 * than what we found in the cache. Use
4151 * it instead of the cache delegation.
4153 query_releasename(client, &fname);
4157 * We've already done query_keepname() on
4158 * zfname, so we must set dbuf to NULL to
4159 * prevent query_addrrset() from trying to
4160 * call query_keepname() again.
4163 query_putrdataset(client, &rdataset);
4164 if (sigrdataset != NULL)
4165 query_putrdataset(client,
4167 rdataset = zrdataset;
4169 sigrdataset = zsigrdataset;
4170 zsigrdataset = NULL;
4174 * We don't clean up zdb here because we
4175 * may still need it. It will get cleaned
4176 * up by the main cleanup code.
4180 if (RECURSIONOK(client)) {
4184 if (dns_rdatatype_atparent(type))
4185 result = query_recurse(client, qtype,
4189 result = query_recurse(client, qtype,
4192 if (result == ISC_R_SUCCESS)
4193 client->query.attributes |=
4194 NS_QUERYATTR_RECURSING;
4196 RECURSE_ERROR(result);
4198 dns_fixedname_t fixed;
4200 dns_fixedname_init(&fixed);
4201 dns_name_copy(fname,
4202 dns_fixedname_name(&fixed), NULL);
4204 * This is the best answer.
4206 client->query.attributes |=
4207 NS_QUERYATTR_CACHEGLUEOK;
4208 client->query.gluedb = zdb;
4209 client->query.isreferral = ISC_TRUE;
4211 * We must ensure NOADDITIONAL is off,
4212 * because the generation of
4213 * additional data is required in
4216 client->query.attributes &=
4217 ~NS_QUERYATTR_NOADDITIONAL;
4218 if (sigrdataset != NULL)
4219 sigrdatasetp = &sigrdataset;
4221 sigrdatasetp = NULL;
4222 query_addrrset(client, &fname,
4223 &rdataset, sigrdatasetp,
4224 dbuf, DNS_SECTION_AUTHORITY);
4225 client->query.gluedb = NULL;
4226 client->query.attributes &=
4227 ~NS_QUERYATTR_CACHEGLUEOK;
4228 if (WANTDNSSEC(client))
4229 query_addds(client, db, node, version,
4230 dns_fixedname_name(&fixed));
4234 case DNS_R_EMPTYNAME:
4235 result = DNS_R_NXRRSET;
4240 * Look for a NSEC3 record if we don't have a NSEC record.
4242 if (!dns_rdataset_isassociated(rdataset) &&
4243 WANTDNSSEC(client)) {
4244 if ((fname->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
4248 dns_fixedname_init(&fixed);
4249 found = dns_fixedname_name(&fixed);
4250 qname = client->query.qname;
4252 query_findclosestnsec3(qname, db, version,
4257 * Did we find the closest provable encloser
4258 * instead? If so add the nearest to the
4259 * closest provable encloser.
4262 dns_rdataset_isassociated(rdataset) &&
4263 !dns_name_equal(qname, found))
4269 * Add the closest provable encloser.
4271 query_addrrset(client, &fname,
4272 &rdataset, &sigrdataset,
4274 DNS_SECTION_AUTHORITY);
4276 count = dns_name_countlabels(found)
4278 skip = dns_name_countlabels(qname) -
4280 dns_name_getlabelsequence(qname, skip,
4284 fixfname(client, &fname, &dbuf, &b);
4285 fixrdataset(client, &rdataset);
4286 fixrdataset(client, &sigrdataset);
4287 if (fname == NULL ||
4289 sigrdataset == NULL) {
4290 QUERY_ERROR(DNS_R_SERVFAIL);
4294 * 'nearest' doesn't exist so
4295 * 'exist' is set to ISC_FALSE.
4297 query_findclosestnsec3(found, db,
4307 query_releasename(client, &fname);
4308 query_addwildcardproof(client, db, version,
4309 client->query.qname,
4313 if (dns_rdataset_isassociated(rdataset)) {
4315 * If we've got a NSEC record, we need to save the
4316 * name now because we're going call query_addsoa()
4317 * below, and it needs to use the name buffer.
4319 query_keepname(client, fname, dbuf);
4320 } else if (fname != NULL) {
4322 * We're not going to use fname, and need to release
4323 * our hold on the name buffer so query_addsoa()
4326 query_releasename(client, &fname);
4331 result = query_addsoa(client, db, version, ISC_FALSE,
4332 dns_rdataset_isassociated(rdataset));
4333 if (result != ISC_R_SUCCESS) {
4334 QUERY_ERROR(result);
4338 * Add NSEC record if we found one.
4340 if (WANTDNSSEC(client)) {
4341 if (dns_rdataset_isassociated(rdataset))
4342 query_addnxrrsetnsec(client, db, version,
4348 case DNS_R_EMPTYWILD:
4349 empty_wild = ISC_TRUE;
4352 case DNS_R_NXDOMAIN:
4354 if (dns_rdataset_isassociated(rdataset)) {
4356 * If we've got a NSEC record, we need to save the
4357 * name now because we're going call query_addsoa()
4358 * below, and it needs to use the name buffer.
4360 query_keepname(client, fname, dbuf);
4361 } else if (fname != NULL) {
4363 * We're not going to use fname, and need to release
4364 * our hold on the name buffer so query_addsoa()
4367 query_releasename(client, &fname);
4370 * Add SOA. If the query was for a SOA record force the
4371 * ttl to zero so that it is possible for clients to find
4372 * the containing zone of an arbitrary name with a stub
4373 * resolver and not have it cached.
4375 if (qtype == dns_rdatatype_soa &&
4379 dns_zone_getzeronosoattl(zone))
4380 result = query_addsoa(client, db, version, ISC_TRUE,
4381 dns_rdataset_isassociated(rdataset));
4383 result = query_addsoa(client, db, version, ISC_FALSE,
4384 dns_rdataset_isassociated(rdataset));
4385 if (result != ISC_R_SUCCESS) {
4386 QUERY_ERROR(result);
4390 if (WANTDNSSEC(client)) {
4392 * Add NSEC record if we found one.
4394 if (dns_rdataset_isassociated(rdataset))
4395 query_addrrset(client, &fname, &rdataset,
4397 NULL, DNS_SECTION_AUTHORITY);
4398 query_addwildcardproof(client, db, version,
4399 client->query.qname, ISC_FALSE);
4403 * Set message rcode.
4406 client->message->rcode = dns_rcode_noerror;
4408 client->message->rcode = dns_rcode_nxdomain;
4411 case DNS_R_NCACHENXDOMAIN:
4412 case DNS_R_NCACHENXRRSET:
4414 authoritative = ISC_FALSE;
4416 * Set message rcode, if required.
4418 if (result == DNS_R_NCACHENXDOMAIN)
4419 client->message->rcode = dns_rcode_nxdomain;
4421 * Look for RFC 1918 leakage from Internet.
4423 if (result == DNS_R_NCACHENXDOMAIN &&
4424 qtype == dns_rdatatype_ptr &&
4425 client->message->rdclass == dns_rdataclass_in &&
4426 dns_name_countlabels(fname) == 7)
4427 warn_rfc1918(client, fname, rdataset);
4429 * We don't call query_addrrset() because we don't need any
4430 * of its extra features (and things would probably break!).
4432 query_keepname(client, fname, dbuf);
4433 dns_message_addname(client->message, fname,
4434 DNS_SECTION_AUTHORITY);
4435 ISC_LIST_APPEND(fname->list, rdataset, link);
4442 * Keep a copy of the rdataset. We have to do this because
4443 * query_addrrset may clear 'rdataset' (to prevent the
4444 * cleanup code from cleaning it up).
4446 trdataset = rdataset;
4448 * Add the CNAME to the answer section.
4450 if (sigrdataset != NULL)
4451 sigrdatasetp = &sigrdataset;
4453 sigrdatasetp = NULL;
4454 if (WANTDNSSEC(client) &&
4455 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4457 dns_fixedname_init(&wildcardname);
4458 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
4460 need_wildcardproof = ISC_TRUE;
4462 if (NOQNAME(rdataset) && WANTDNSSEC(client))
4466 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4467 DNS_SECTION_ANSWER);
4468 if (noqname != NULL)
4469 query_addnoqnameproof(client, noqname);
4471 * We set the PARTIALANSWER attribute so that if anything goes
4472 * wrong later on, we'll return what we've got so far.
4474 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
4476 * Reset qname to be the target name of the CNAME and restart
4480 result = dns_message_gettempname(client->message, &tname);
4481 if (result != ISC_R_SUCCESS)
4483 result = dns_rdataset_first(trdataset);
4484 if (result != ISC_R_SUCCESS) {
4485 dns_message_puttempname(client->message, &tname);
4488 dns_rdataset_current(trdataset, &rdata);
4489 result = dns_rdata_tostruct(&rdata, &cname, NULL);
4490 dns_rdata_reset(&rdata);
4491 if (result != ISC_R_SUCCESS) {
4492 dns_message_puttempname(client->message, &tname);
4495 dns_name_init(tname, NULL);
4496 result = dns_name_dup(&cname.cname, client->mctx, tname);
4497 if (result != ISC_R_SUCCESS) {
4498 dns_message_puttempname(client->message, &tname);
4499 dns_rdata_freestruct(&cname);
4502 dns_rdata_freestruct(&cname);
4503 ns_client_qnamereplace(client, tname);
4504 want_restart = ISC_TRUE;
4505 if (!WANTRECURSION(client))
4506 options |= DNS_GETDB_NOLOG;
4510 * Compare the current qname to the found name. We need
4511 * to know how many labels and bits are in common because
4512 * we're going to have to split qname later on.
4514 namereln = dns_name_fullcompare(client->query.qname, fname,
4516 INSIST(namereln == dns_namereln_subdomain);
4518 * Keep a copy of the rdataset. We have to do this because
4519 * query_addrrset may clear 'rdataset' (to prevent the
4520 * cleanup code from cleaning it up).
4522 trdataset = rdataset;
4524 * Add the DNAME to the answer section.
4526 if (sigrdataset != NULL)
4527 sigrdatasetp = &sigrdataset;
4529 sigrdatasetp = NULL;
4530 if (WANTDNSSEC(client) &&
4531 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4533 dns_fixedname_init(&wildcardname);
4534 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
4536 need_wildcardproof = ISC_TRUE;
4538 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4539 DNS_SECTION_ANSWER);
4541 * We set the PARTIALANSWER attribute so that if anything goes
4542 * wrong later on, we'll return what we've got so far.
4544 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
4546 * Get the target name of the DNAME.
4549 result = dns_message_gettempname(client->message, &tname);
4550 if (result != ISC_R_SUCCESS)
4552 result = dns_rdataset_first(trdataset);
4553 if (result != ISC_R_SUCCESS) {
4554 dns_message_puttempname(client->message, &tname);
4557 dns_rdataset_current(trdataset, &rdata);
4558 result = dns_rdata_tostruct(&rdata, &dname, NULL);
4559 dns_rdata_reset(&rdata);
4560 if (result != ISC_R_SUCCESS) {
4561 dns_message_puttempname(client->message, &tname);
4564 dns_name_init(tname, NULL);
4565 dns_name_clone(&dname.dname, tname);
4566 dns_rdata_freestruct(&dname);
4568 * Construct the new qname.
4570 dns_fixedname_init(&fixed);
4571 prefix = dns_fixedname_name(&fixed);
4572 dns_name_split(client->query.qname, nlabels, prefix, NULL);
4573 INSIST(fname == NULL);
4574 dbuf = query_getnamebuf(client);
4576 dns_message_puttempname(client->message, &tname);
4579 fname = query_newname(client, dbuf, &b);
4580 if (fname == NULL) {
4581 dns_message_puttempname(client->message, &tname);
4584 result = dns_name_concatenate(prefix, tname, fname, NULL);
4585 if (result != ISC_R_SUCCESS) {
4586 dns_message_puttempname(client->message, &tname);
4587 if (result == ISC_R_NOSPACE) {
4589 * RFC2672, section 4.1, subsection 3c says
4590 * we should return YXDOMAIN if the constructed
4591 * name would be too long.
4593 client->message->rcode = dns_rcode_yxdomain;
4597 query_keepname(client, fname, dbuf);
4599 * Synthesize a CNAME for this DNAME.
4601 * We want to synthesize a CNAME since if we don't
4602 * then older software that doesn't understand DNAME
4603 * will not chain like it should.
4605 * We do not try to synthesize a signature because we hope
4606 * that security aware servers will understand DNAME. Also,
4607 * even if we had an online key, making a signature
4608 * on-the-fly is costly, and not really legitimate anyway
4609 * since the synthesized CNAME is NOT in the zone.
4611 dns_name_init(tname, NULL);
4612 (void)query_addcnamelike(client, client->query.qname, fname,
4614 dns_rdatatype_cname);
4616 dns_message_puttempname(client->message, &tname);
4618 * Switch to the new qname and restart.
4620 ns_client_qnamereplace(client, fname);
4622 want_restart = ISC_TRUE;
4623 if (!WANTRECURSION(client))
4624 options |= DNS_GETDB_NOLOG;
4628 * Something has gone wrong.
4630 QUERY_ERROR(DNS_R_SERVFAIL);
4634 if (WANTDNSSEC(client) &&
4635 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4637 dns_fixedname_init(&wildcardname);
4638 dns_name_copy(fname, dns_fixedname_name(&wildcardname), NULL);
4639 need_wildcardproof = ISC_TRUE;
4642 if (type == dns_rdatatype_any) {
4644 * XXXRTH Need to handle zonecuts with special case
4649 result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
4650 if (result != ISC_R_SUCCESS) {
4651 QUERY_ERROR(DNS_R_SERVFAIL);
4655 * Calling query_addrrset() with a non-NULL dbuf is going
4656 * to either keep or release the name. We don't want it to
4657 * release fname, since we may have to call query_addrrset()
4658 * more than once. That means we have to call query_keepname()
4659 * now, and pass a NULL dbuf to query_addrrset().
4661 * If we do a query_addrrset() below, we must set fname to
4662 * NULL before leaving this block, otherwise we might try to
4663 * cleanup fname even though we're using it!
4665 query_keepname(client, fname, dbuf);
4667 result = dns_rdatasetiter_first(rdsiter);
4668 while (result == ISC_R_SUCCESS) {
4669 dns_rdatasetiter_current(rdsiter, rdataset);
4670 if (is_zone && qtype == dns_rdatatype_any &&
4671 !dns_db_issecure(db) &&
4672 dns_rdatatype_isdnssec(rdataset->type)) {
4674 * The zone is transitioning from insecure
4675 * to secure. Hide the dnssec records from
4678 dns_rdataset_disassociate(rdataset);
4679 } else if ((qtype == dns_rdatatype_any ||
4680 rdataset->type == qtype) && rdataset->type != 0) {
4681 if (NOQNAME(rdataset) && WANTDNSSEC(client))
4685 query_addrrset(client,
4686 fname != NULL ? &fname : &tname,
4688 NULL, DNS_SECTION_ANSWER);
4689 if (noqname != NULL)
4690 query_addnoqnameproof(client, noqname);
4692 INSIST(tname != NULL);
4694 * rdataset is non-NULL only in certain
4695 * pathological cases involving DNAMEs.
4697 if (rdataset != NULL)
4698 query_putrdataset(client, &rdataset);
4699 rdataset = query_newrdataset(client);
4700 if (rdataset == NULL)
4704 * We're not interested in this rdataset.
4706 dns_rdataset_disassociate(rdataset);
4708 result = dns_rdatasetiter_next(rdsiter);
4712 dns_message_puttempname(client->message, &fname);
4714 if (n == 0 && is_zone) {
4716 * We didn't match any rdatasets.
4718 if (qtype == dns_rdatatype_rrsig &&
4719 result == ISC_R_NOMORE) {
4721 * XXXRTH If this is a secure zone and we
4722 * didn't find any SIGs, we should generate
4723 * an error unless we were searching for
4727 authoritative = ISC_FALSE;
4728 dns_rdatasetiter_destroy(&rdsiter);
4729 if (RECURSIONOK(client)) {
4730 result = query_recurse(client,
4735 if (result == ISC_R_SUCCESS)
4736 client->query.attributes |=
4737 NS_QUERYATTR_RECURSING;
4739 RECURSE_ERROR(result);
4744 * We were searching for SIG records in
4745 * a nonsecure zone. Send a "no error,
4746 * no data" response.
4751 result = query_addsoa(client, db, version,
4752 ISC_FALSE, ISC_FALSE);
4753 if (result == ISC_R_SUCCESS)
4754 result = ISC_R_NOMORE;
4757 * Something went wrong.
4759 result = DNS_R_SERVFAIL;
4762 dns_rdatasetiter_destroy(&rdsiter);
4763 if (result != ISC_R_NOMORE) {
4764 QUERY_ERROR(DNS_R_SERVFAIL);
4769 * This is the "normal" case -- an ordinary question to which
4770 * we know the answer.
4772 if (sigrdataset != NULL)
4773 sigrdatasetp = &sigrdataset;
4775 sigrdatasetp = NULL;
4776 if (NOQNAME(rdataset) && WANTDNSSEC(client))
4781 * BIND 8 priming queries need the additional section.
4783 if (is_zone && qtype == dns_rdatatype_ns &&
4784 dns_name_equal(client->query.qname, dns_rootname))
4785 client->query.attributes &= ~NS_QUERYATTR_NOADDITIONAL;
4787 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4788 DNS_SECTION_ANSWER);
4789 if (noqname != NULL)
4790 query_addnoqnameproof(client, noqname);
4792 * We shouldn't ever fail to add 'rdataset'
4793 * because it's already in the answer.
4795 INSIST(rdataset == NULL);
4799 CTRACE("query_find: addauth");
4801 * Add NS records to the authority section (if we haven't already
4802 * added them to the answer section).
4804 if (!want_restart && !NOAUTHORITY(client)) {
4806 if (!((qtype == dns_rdatatype_ns ||
4807 qtype == dns_rdatatype_any) &&
4808 dns_name_equal(client->query.qname,
4809 dns_db_origin(db))))
4810 (void)query_addns(client, db, version);
4811 } else if (qtype != dns_rdatatype_ns) {
4813 query_releasename(client, &fname);
4814 query_addbestns(client);
4819 * Add NSEC records to the authority section if they're needed for
4820 * DNSSEC wildcard proofs.
4822 if (need_wildcardproof && dns_db_issecure(db))
4823 query_addwildcardproof(client, db, version,
4824 dns_fixedname_name(&wildcardname),
4827 CTRACE("query_find: cleanup");
4831 if (rdataset != NULL)
4832 query_putrdataset(client, &rdataset);
4833 if (sigrdataset != NULL)
4834 query_putrdataset(client, &sigrdataset);
4836 query_releasename(client, &fname);
4838 dns_db_detachnode(db, &node);
4842 dns_zone_detach(&zone);
4844 query_putrdataset(client, &zrdataset);
4845 if (zsigrdataset != NULL)
4846 query_putrdataset(client, &zsigrdataset);
4848 query_releasename(client, &zfname);
4849 dns_db_detach(&zdb);
4852 isc_event_free(ISC_EVENT_PTR(&event));
4857 if (client->query.restarts == 0 && !authoritative) {
4859 * We're not authoritative, so we must ensure the AA bit
4862 client->message->flags &= ~DNS_MESSAGEFLAG_AA;
4866 * Restart the query?
4868 if (want_restart && client->query.restarts < MAX_RESTARTS) {
4869 client->query.restarts++;
4873 if (eresult != ISC_R_SUCCESS &&
4874 (!PARTIALANSWER(client) || WANTRECURSION(client))) {
4875 if (eresult == DNS_R_DUPLICATE || eresult == DNS_R_DROP) {
4877 * This was a duplicate query that we are
4878 * recursing on. Don't send a response now.
4879 * The original query will still cause a response.
4881 query_next(client, eresult);
4884 * If we don't have any answer to give the client,
4885 * or if the client requested recursion and thus wanted
4886 * the complete answer, send an error response.
4889 query_error(client, eresult, line);
4891 ns_client_detach(&client);
4892 } else if (!RECURSING(client)) {
4894 * We are done. Set up sortlist data for the message
4895 * rendering code, make a final tweak to the AA bit if the
4896 * auth-nxdomain config option says so, then render and
4897 * send the response.
4899 setup_query_sortlist(client);
4902 * If this is a referral and the answer to the question
4903 * is in the glue sort it to the start of the additional
4906 if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER]) &&
4907 client->message->rcode == dns_rcode_noerror &&
4908 (qtype == dns_rdatatype_a || qtype == dns_rdatatype_aaaa))
4909 answer_in_glue(client, qtype);
4911 if (client->message->rcode == dns_rcode_nxdomain &&
4912 client->view->auth_nxdomain == ISC_TRUE)
4913 client->message->flags |= DNS_MESSAGEFLAG_AA;
4916 * If the response is somehow unexpected for the client and this
4917 * is a result of recursion, return an error to the caller
4918 * to indicate it may need to be logged.
4921 (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER]) ||
4922 client->message->rcode != dns_rcode_noerror))
4923 eresult = ISC_R_FAILURE;
4926 ns_client_detach(&client);
4928 CTRACE("query_find: done");
4934 log_query(ns_client_t *client, unsigned int flags, unsigned int extflags) {
4935 char namebuf[DNS_NAME_FORMATSIZE];
4936 char typename[DNS_RDATATYPE_FORMATSIZE];
4937 char classname[DNS_RDATACLASS_FORMATSIZE];
4938 dns_rdataset_t *rdataset;
4939 int level = ISC_LOG_INFO;
4941 if (! isc_log_wouldlog(ns_g_lctx, level))
4944 rdataset = ISC_LIST_HEAD(client->query.qname->list);
4945 INSIST(rdataset != NULL);
4946 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
4947 dns_rdataclass_format(rdataset->rdclass, classname, sizeof(classname));
4948 dns_rdatatype_format(rdataset->type, typename, sizeof(typename));
4950 ns_client_log(client, NS_LOGCATEGORY_QUERIES, NS_LOGMODULE_QUERY,
4951 level, "query: %s %s %s %s%s%s%s%s", namebuf, classname,
4952 typename, WANTRECURSION(client) ? "+" : "-",
4953 (client->signer != NULL) ? "S": "",
4954 (client->opt != NULL) ? "E" : "",
4955 ((extflags & DNS_MESSAGEEXTFLAG_DO) != 0) ? "D" : "",
4956 ((flags & DNS_MESSAGEFLAG_CD) != 0) ? "C" : "");
4960 log_queryerror(ns_client_t *client, isc_result_t result, int line, int level) {
4961 char namebuf[DNS_NAME_FORMATSIZE];
4962 char typename[DNS_RDATATYPE_FORMATSIZE];
4963 char classname[DNS_RDATACLASS_FORMATSIZE];
4964 const char *namep, *typep, *classp, *sep1, *sep2;
4965 dns_rdataset_t *rdataset;
4967 if (!isc_log_wouldlog(ns_g_lctx, level))
4970 namep = typep = classp = sep1 = sep2 = "";
4973 * Query errors can happen for various reasons. In some cases we cannot
4974 * even assume the query contains a valid question section, so we should
4975 * expect exceptional cases.
4977 if (client->query.origqname != NULL) {
4978 dns_name_format(client->query.origqname, namebuf,
4983 rdataset = ISC_LIST_HEAD(client->query.origqname->list);
4984 if (rdataset != NULL) {
4985 dns_rdataclass_format(rdataset->rdclass, classname,
4988 dns_rdatatype_format(rdataset->type, typename,
4995 ns_client_log(client, NS_LOGCATEGORY_QUERY_EERRORS, NS_LOGMODULE_QUERY,
4996 level, "query failed (%s)%s%s%s%s%s%s at %s:%d",
4997 isc_result_totext(result), sep1, namep, sep2,
4998 classp, sep2, typep, __FILE__, line);
5002 ns_query_start(ns_client_t *client) {
5003 isc_result_t result;
5004 dns_message_t *message = client->message;
5005 dns_rdataset_t *rdataset;
5006 ns_client_t *qclient;
5007 dns_rdatatype_t qtype;
5008 unsigned int saved_extflags = client->extflags;
5009 unsigned int saved_flags = client->message->flags;
5010 isc_boolean_t want_ad;
5012 CTRACE("ns_query_start");
5017 if (ns_g_clienttest && (client->attributes & NS_CLIENTATTR_TCP) == 0)
5018 RUNTIME_CHECK(ns_client_replace(client) == ISC_R_SUCCESS);
5021 * Ensure that appropriate cleanups occur.
5023 client->next = query_next_callback;
5026 * Behave as if we don't support DNSSEC if not enabled.
5028 if (!client->view->enablednssec) {
5029 message->flags &= ~DNS_MESSAGEFLAG_CD;
5030 client->extflags &= ~DNS_MESSAGEEXTFLAG_DO;
5031 if (client->opt != NULL)
5032 client->opt->ttl &= ~DNS_MESSAGEEXTFLAG_DO;
5035 if ((message->flags & DNS_MESSAGEFLAG_RD) != 0)
5036 client->query.attributes |= NS_QUERYATTR_WANTRECURSION;
5038 if ((client->extflags & DNS_MESSAGEEXTFLAG_DO) != 0)
5039 client->attributes |= NS_CLIENTATTR_WANTDNSSEC;
5041 if (client->view->minimalresponses)
5042 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
5043 NS_QUERYATTR_NOADDITIONAL);
5045 if ((client->view->cachedb == NULL)
5046 || (!client->view->additionalfromcache)) {
5048 * We don't have a cache. Turn off cache support and
5051 client->query.attributes &=
5052 ~(NS_QUERYATTR_RECURSIONOK|NS_QUERYATTR_CACHEOK);
5053 } else if ((client->attributes & NS_CLIENTATTR_RA) == 0 ||
5054 (message->flags & DNS_MESSAGEFLAG_RD) == 0) {
5056 * If the client isn't allowed to recurse (due to
5057 * "recursion no", the allow-recursion ACL, or the
5058 * lack of a resolver in this view), or if it
5059 * doesn't want recursion, turn recursion off.
5061 client->query.attributes &= ~NS_QUERYATTR_RECURSIONOK;
5065 * Get the question name.
5067 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
5068 if (result != ISC_R_SUCCESS) {
5069 query_error(client, result, __LINE__);
5072 dns_message_currentname(message, DNS_SECTION_QUESTION,
5073 &client->query.qname);
5074 client->query.origqname = client->query.qname;
5075 result = dns_message_nextname(message, DNS_SECTION_QUESTION);
5076 if (result != ISC_R_NOMORE) {
5077 if (result == ISC_R_SUCCESS) {
5079 * There's more than one QNAME in the question
5082 query_error(client, DNS_R_FORMERR, __LINE__);
5084 query_error(client, result, __LINE__);
5088 if (ns_g_server->log_queries)
5089 log_query(client, saved_flags, saved_extflags);
5092 * Check for multiple question queries, since edns1 is dead.
5094 if (message->counts[DNS_SECTION_QUESTION] > 1) {
5095 query_error(client, DNS_R_FORMERR, __LINE__);
5100 * Check for meta-queries like IXFR and AXFR.
5102 rdataset = ISC_LIST_HEAD(client->query.qname->list);
5103 INSIST(rdataset != NULL);
5104 qtype = rdataset->type;
5105 dns_rdatatypestats_increment(ns_g_server->rcvquerystats, qtype);
5106 if (dns_rdatatype_ismeta(qtype)) {
5108 case dns_rdatatype_any:
5109 break; /* Let query_find handle it. */
5110 case dns_rdatatype_ixfr:
5111 case dns_rdatatype_axfr:
5112 ns_xfr_start(client, rdataset->type);
5114 case dns_rdatatype_maila:
5115 case dns_rdatatype_mailb:
5116 query_error(client, DNS_R_NOTIMP, __LINE__);
5118 case dns_rdatatype_tkey:
5119 result = dns_tkey_processquery(client->message,
5120 ns_g_server->tkeyctx,
5121 client->view->dynamickeys);
5122 if (result == ISC_R_SUCCESS)
5125 query_error(client, result, __LINE__);
5127 default: /* TSIG, etc. */
5128 query_error(client, DNS_R_FORMERR, __LINE__);
5134 * Turn on minimal response for DNSKEY and DS queries.
5136 if (qtype == dns_rdatatype_dnskey || qtype == dns_rdatatype_ds)
5137 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
5138 NS_QUERYATTR_NOADDITIONAL);
5141 * Turn on minimal responses for EDNS/UDP bufsize 512 queries.
5143 if (client->opt != NULL && client->udpsize <= 512U &&
5144 (client->attributes & NS_CLIENTATTR_TCP) == 0)
5145 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
5146 NS_QUERYATTR_NOADDITIONAL);
5149 * If the client has requested that DNSSEC checking be disabled,
5150 * allow lookups to return pending data and instruct the resolver
5151 * to return data before validation has completed.
5153 * We don't need to set DNS_DBFIND_PENDINGOK when validation is
5154 * disabled as there will be no pending data.
5156 if (message->flags & DNS_MESSAGEFLAG_CD ||
5157 qtype == dns_rdatatype_rrsig)
5159 client->query.dboptions |= DNS_DBFIND_PENDINGOK;
5160 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
5161 } else if (!client->view->enablevalidation)
5162 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
5165 * Allow glue NS records to be added to the authority section
5166 * if the answer is secure.
5168 if (message->flags & DNS_MESSAGEFLAG_CD)
5169 client->query.attributes &= ~NS_QUERYATTR_SECURE;
5172 * Set 'want_ad' if the client has set AD in the query.
5173 * This allows AD to be returned on queries without DO set.
5175 if ((message->flags & DNS_MESSAGEFLAG_AD) != 0)
5178 want_ad = ISC_FALSE;
5181 * This is an ordinary query.
5183 result = dns_message_reply(message, ISC_TRUE);
5184 if (result != ISC_R_SUCCESS) {
5185 query_next(client, result);
5190 * Assume authoritative response until it is known to be
5193 message->flags |= DNS_MESSAGEFLAG_AA;
5196 * Set AD. We must clear it if we add non-validated data to a
5199 if (WANTDNSSEC(client) || want_ad)
5200 message->flags |= DNS_MESSAGEFLAG_AD;
5203 ns_client_attach(client, &qclient);
5204 (void)query_find(qclient, NULL, qtype);