2 * Copyright (C) 2004-2009 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.298.48.15.2.4 2009/12/31 22:52:47 each Exp $ */
27 #include <isc/stats.h>
31 #include <dns/byaddr.h>
36 #include <dns/dnssec.h>
37 #include <dns/events.h>
38 #include <dns/message.h>
39 #include <dns/ncache.h>
40 #include <dns/order.h>
41 #include <dns/rdata.h>
42 #include <dns/rdataclass.h>
43 #include <dns/rdatalist.h>
44 #include <dns/rdataset.h>
45 #include <dns/rdatasetiter.h>
46 #include <dns/rdatastruct.h>
47 #include <dns/rdatatype.h>
48 #include <dns/resolver.h>
49 #include <dns/result.h>
50 #include <dns/stats.h>
56 #include <named/client.h>
57 #include <named/log.h>
58 #include <named/server.h>
59 #include <named/sortlist.h>
60 #include <named/xfrout.h>
62 /*% Partial answer? */
63 #define PARTIALANSWER(c) (((c)->query.attributes & \
64 NS_QUERYATTR_PARTIALANSWER) != 0)
66 #define USECACHE(c) (((c)->query.attributes & \
67 NS_QUERYATTR_CACHEOK) != 0)
69 #define RECURSIONOK(c) (((c)->query.attributes & \
70 NS_QUERYATTR_RECURSIONOK) != 0)
72 #define RECURSING(c) (((c)->query.attributes & \
73 NS_QUERYATTR_RECURSING) != 0)
75 #define CACHEGLUEOK(c) (((c)->query.attributes & \
76 NS_QUERYATTR_CACHEGLUEOK) != 0)
77 /*% Want Recursion? */
78 #define WANTRECURSION(c) (((c)->query.attributes & \
79 NS_QUERYATTR_WANTRECURSION) != 0)
81 #define WANTDNSSEC(c) (((c)->attributes & \
82 NS_CLIENTATTR_WANTDNSSEC) != 0)
84 #define NOAUTHORITY(c) (((c)->query.attributes & \
85 NS_QUERYATTR_NOAUTHORITY) != 0)
87 #define NOADDITIONAL(c) (((c)->query.attributes & \
88 NS_QUERYATTR_NOADDITIONAL) != 0)
90 #define SECURE(c) (((c)->query.attributes & \
91 NS_QUERYATTR_SECURE) != 0)
94 #define CTRACE(m) isc_log_write(ns_g_lctx, \
95 NS_LOGCATEGORY_CLIENT, \
98 "client %p: %s", client, (m))
99 #define QTRACE(m) isc_log_write(ns_g_lctx, \
100 NS_LOGCATEGORY_GENERAL, \
101 NS_LOGMODULE_QUERY, \
103 "query %p: %s", query, (m))
105 #define CTRACE(m) ((void)m)
106 #define QTRACE(m) ((void)m)
109 #define DNS_GETDB_NOEXACT 0x01U
110 #define DNS_GETDB_NOLOG 0x02U
111 #define DNS_GETDB_PARTIAL 0x04U
113 #define PENDINGOK(x) (((x) & DNS_DBFIND_PENDINGOK) != 0)
115 typedef struct client_additionalctx {
117 dns_rdataset_t *rdataset;
118 } client_additionalctx_t;
121 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype);
124 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
125 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
128 log_queryerror(ns_client_t *client, isc_result_t result, int line, int level);
131 * Increment query statistics counters.
134 inc_stats(ns_client_t *client, isc_statscounter_t counter) {
135 dns_zone_t *zone = client->query.authzone;
137 isc_stats_increment(ns_g_server->nsstats, counter);
140 isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
141 if (zonestats != NULL)
142 isc_stats_increment(zonestats, counter);
147 query_send(ns_client_t *client) {
148 isc_statscounter_t counter;
149 if ((client->message->flags & DNS_MESSAGEFLAG_AA) == 0)
150 inc_stats(client, dns_nsstatscounter_nonauthans);
152 inc_stats(client, dns_nsstatscounter_authans);
153 if (client->message->rcode == dns_rcode_noerror) {
154 if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER])) {
155 if (client->query.isreferral) {
156 counter = dns_nsstatscounter_referral;
158 counter = dns_nsstatscounter_nxrrset;
161 counter = dns_nsstatscounter_success;
163 } else if (client->message->rcode == dns_rcode_nxdomain) {
164 counter = dns_nsstatscounter_nxdomain;
166 /* We end up here in case of YXDOMAIN, and maybe others */
167 counter = dns_nsstatscounter_failure;
169 inc_stats(client, counter);
170 ns_client_send(client);
174 query_error(ns_client_t *client, isc_result_t result, int line) {
175 int loglevel = ISC_LOG_DEBUG(3);
179 loglevel = ISC_LOG_DEBUG(1);
180 inc_stats(client, dns_nsstatscounter_servfail);
183 inc_stats(client, dns_nsstatscounter_formerr);
186 inc_stats(client, dns_nsstatscounter_failure);
190 log_queryerror(client, result, line, loglevel);
192 ns_client_error(client, result);
196 query_next(ns_client_t *client, isc_result_t result) {
197 if (result == DNS_R_DUPLICATE)
198 inc_stats(client, dns_nsstatscounter_duplicate);
199 else if (result == DNS_R_DROP)
200 inc_stats(client, dns_nsstatscounter_dropped);
202 inc_stats(client, dns_nsstatscounter_failure);
203 ns_client_next(client, result);
207 query_freefreeversions(ns_client_t *client, isc_boolean_t everything) {
208 ns_dbversion_t *dbversion, *dbversion_next;
211 for (dbversion = ISC_LIST_HEAD(client->query.freeversions), i = 0;
213 dbversion = dbversion_next, i++)
215 dbversion_next = ISC_LIST_NEXT(dbversion, link);
217 * If we're not freeing everything, we keep the first three
218 * dbversions structures around.
220 if (i > 3 || everything) {
221 ISC_LIST_UNLINK(client->query.freeversions, dbversion,
223 isc_mem_put(client->mctx, dbversion,
230 ns_query_cancel(ns_client_t *client) {
231 LOCK(&client->query.fetchlock);
232 if (client->query.fetch != NULL) {
233 dns_resolver_cancelfetch(client->query.fetch);
235 client->query.fetch = NULL;
237 UNLOCK(&client->query.fetchlock);
241 query_reset(ns_client_t *client, isc_boolean_t everything) {
242 isc_buffer_t *dbuf, *dbuf_next;
243 ns_dbversion_t *dbversion, *dbversion_next;
246 * Reset the query state of a client to its default state.
250 * Cancel the fetch if it's running.
252 ns_query_cancel(client);
255 * Cleanup any active versions.
257 for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
259 dbversion = dbversion_next) {
260 dbversion_next = ISC_LIST_NEXT(dbversion, link);
261 dns_db_closeversion(dbversion->db, &dbversion->version,
263 dns_db_detach(&dbversion->db);
264 ISC_LIST_INITANDAPPEND(client->query.freeversions,
267 ISC_LIST_INIT(client->query.activeversions);
269 if (client->query.authdb != NULL)
270 dns_db_detach(&client->query.authdb);
271 if (client->query.authzone != NULL)
272 dns_zone_detach(&client->query.authzone);
274 query_freefreeversions(client, everything);
276 for (dbuf = ISC_LIST_HEAD(client->query.namebufs);
279 dbuf_next = ISC_LIST_NEXT(dbuf, link);
280 if (dbuf_next != NULL || everything) {
281 ISC_LIST_UNLINK(client->query.namebufs, dbuf, link);
282 isc_buffer_free(&dbuf);
286 if (client->query.restarts > 0) {
288 * client->query.qname was dynamically allocated.
290 dns_message_puttempname(client->message,
291 &client->query.qname);
293 client->query.qname = NULL;
294 client->query.attributes = (NS_QUERYATTR_RECURSIONOK |
295 NS_QUERYATTR_CACHEOK |
296 NS_QUERYATTR_SECURE);
297 client->query.restarts = 0;
298 client->query.timerset = ISC_FALSE;
299 client->query.origqname = NULL;
300 client->query.qname = NULL;
301 client->query.dboptions = 0;
302 client->query.fetchoptions = 0;
303 client->query.gluedb = NULL;
304 client->query.authdbset = ISC_FALSE;
305 client->query.isreferral = ISC_FALSE;
309 query_next_callback(ns_client_t *client) {
310 query_reset(client, ISC_FALSE);
314 ns_query_free(ns_client_t *client) {
315 query_reset(client, ISC_TRUE);
318 static inline isc_result_t
319 query_newnamebuf(ns_client_t *client) {
323 CTRACE("query_newnamebuf");
325 * Allocate a name buffer.
329 result = isc_buffer_allocate(client->mctx, &dbuf, 1024);
330 if (result != ISC_R_SUCCESS) {
331 CTRACE("query_newnamebuf: isc_buffer_allocate failed: done");
334 ISC_LIST_APPEND(client->query.namebufs, dbuf, link);
336 CTRACE("query_newnamebuf: done");
337 return (ISC_R_SUCCESS);
340 static inline isc_buffer_t *
341 query_getnamebuf(ns_client_t *client) {
346 CTRACE("query_getnamebuf");
348 * Return a name buffer with space for a maximal name, allocating
349 * a new one if necessary.
352 if (ISC_LIST_EMPTY(client->query.namebufs)) {
353 result = query_newnamebuf(client);
354 if (result != ISC_R_SUCCESS) {
355 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
360 dbuf = ISC_LIST_TAIL(client->query.namebufs);
361 INSIST(dbuf != NULL);
362 isc_buffer_availableregion(dbuf, &r);
363 if (r.length < 255) {
364 result = query_newnamebuf(client);
365 if (result != ISC_R_SUCCESS) {
366 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
370 dbuf = ISC_LIST_TAIL(client->query.namebufs);
371 isc_buffer_availableregion(dbuf, &r);
372 INSIST(r.length >= 255);
374 CTRACE("query_getnamebuf: done");
379 query_keepname(ns_client_t *client, dns_name_t *name, isc_buffer_t *dbuf) {
382 CTRACE("query_keepname");
384 * 'name' is using space in 'dbuf', but 'dbuf' has not yet been
385 * adjusted to take account of that. We do the adjustment.
388 REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) != 0);
390 dns_name_toregion(name, &r);
391 isc_buffer_add(dbuf, r.length);
392 dns_name_setbuffer(name, NULL);
393 client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
397 query_releasename(ns_client_t *client, dns_name_t **namep) {
398 dns_name_t *name = *namep;
401 * 'name' is no longer needed. Return it to our pool of temporary
402 * names. If it is using a name buffer, relinquish its exclusive
403 * rights on the buffer.
406 CTRACE("query_releasename");
407 if (dns_name_hasbuffer(name)) {
408 INSIST((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED)
410 client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
412 dns_message_puttempname(client->message, namep);
413 CTRACE("query_releasename: done");
416 static inline dns_name_t *
417 query_newname(ns_client_t *client, isc_buffer_t *dbuf,
424 REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) == 0);
426 CTRACE("query_newname");
428 result = dns_message_gettempname(client->message, &name);
429 if (result != ISC_R_SUCCESS) {
430 CTRACE("query_newname: dns_message_gettempname failed: done");
433 isc_buffer_availableregion(dbuf, &r);
434 isc_buffer_init(nbuf, r.base, r.length);
435 dns_name_init(name, NULL);
436 dns_name_setbuffer(name, nbuf);
437 client->query.attributes |= NS_QUERYATTR_NAMEBUFUSED;
439 CTRACE("query_newname: done");
443 static inline dns_rdataset_t *
444 query_newrdataset(ns_client_t *client) {
445 dns_rdataset_t *rdataset;
448 CTRACE("query_newrdataset");
450 result = dns_message_gettemprdataset(client->message, &rdataset);
451 if (result != ISC_R_SUCCESS) {
452 CTRACE("query_newrdataset: "
453 "dns_message_gettemprdataset failed: done");
456 dns_rdataset_init(rdataset);
458 CTRACE("query_newrdataset: done");
463 query_putrdataset(ns_client_t *client, dns_rdataset_t **rdatasetp) {
464 dns_rdataset_t *rdataset = *rdatasetp;
466 CTRACE("query_putrdataset");
467 if (rdataset != NULL) {
468 if (dns_rdataset_isassociated(rdataset))
469 dns_rdataset_disassociate(rdataset);
470 dns_message_puttemprdataset(client->message, rdatasetp);
472 CTRACE("query_putrdataset: done");
476 static inline isc_result_t
477 query_newdbversion(ns_client_t *client, unsigned int n) {
479 ns_dbversion_t *dbversion;
481 for (i = 0; i < n; i++) {
482 dbversion = isc_mem_get(client->mctx, sizeof(*dbversion));
483 if (dbversion != NULL) {
484 dbversion->db = NULL;
485 dbversion->version = NULL;
486 ISC_LIST_INITANDAPPEND(client->query.freeversions,
490 * We only return ISC_R_NOMEMORY if we couldn't
494 return (ISC_R_NOMEMORY);
496 return (ISC_R_SUCCESS);
500 return (ISC_R_SUCCESS);
503 static inline ns_dbversion_t *
504 query_getdbversion(ns_client_t *client) {
506 ns_dbversion_t *dbversion;
508 if (ISC_LIST_EMPTY(client->query.freeversions)) {
509 result = query_newdbversion(client, 1);
510 if (result != ISC_R_SUCCESS)
513 dbversion = ISC_LIST_HEAD(client->query.freeversions);
514 INSIST(dbversion != NULL);
515 ISC_LIST_UNLINK(client->query.freeversions, dbversion, link);
521 ns_query_init(ns_client_t *client) {
524 ISC_LIST_INIT(client->query.namebufs);
525 ISC_LIST_INIT(client->query.activeversions);
526 ISC_LIST_INIT(client->query.freeversions);
527 client->query.restarts = 0;
528 client->query.timerset = ISC_FALSE;
529 client->query.qname = NULL;
530 result = isc_mutex_init(&client->query.fetchlock);
531 if (result != ISC_R_SUCCESS)
533 client->query.fetch = NULL;
534 client->query.authdb = NULL;
535 client->query.authzone = NULL;
536 client->query.authdbset = ISC_FALSE;
537 client->query.isreferral = ISC_FALSE;
538 query_reset(client, ISC_FALSE);
539 result = query_newdbversion(client, 3);
540 if (result != ISC_R_SUCCESS) {
541 DESTROYLOCK(&client->query.fetchlock);
544 result = query_newnamebuf(client);
545 if (result != ISC_R_SUCCESS)
546 query_freefreeversions(client, ISC_TRUE);
551 static inline ns_dbversion_t *
552 query_findversion(ns_client_t *client, dns_db_t *db,
553 isc_boolean_t *newzonep)
555 ns_dbversion_t *dbversion;
558 * We may already have done a query related to this
559 * database. If so, we must be sure to make subsequent
560 * queries from the same version.
562 for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
564 dbversion = ISC_LIST_NEXT(dbversion, link)) {
565 if (dbversion->db == db)
569 if (dbversion == NULL) {
571 * This is a new zone for this query. Add it to
574 dbversion = query_getdbversion(client);
575 if (dbversion == NULL)
577 dns_db_attach(db, &dbversion->db);
578 dns_db_currentversion(db, &dbversion->version);
579 dbversion->queryok = ISC_FALSE;
580 ISC_LIST_APPEND(client->query.activeversions,
582 *newzonep = ISC_TRUE;
584 *newzonep = ISC_FALSE;
589 static inline isc_result_t
590 query_validatezonedb(ns_client_t *client, dns_name_t *name,
591 dns_rdatatype_t qtype, unsigned int options,
592 dns_zone_t *zone, dns_db_t *db,
593 dns_dbversion_t **versionp)
596 isc_boolean_t check_acl, new_zone;
598 ns_dbversion_t *dbversion;
600 REQUIRE(zone != NULL);
604 * This limits our searching to the zone where the first name
605 * (the query target) was looked for. This prevents following
606 * CNAMES or DNAMES into other zones and prevents returning
607 * additional data from other zones.
609 if (!client->view->additionalfromauth &&
610 client->query.authdbset &&
611 db != client->query.authdb)
615 * If the zone has an ACL, we'll check it, otherwise
616 * we use the view's "allow-query" ACL. Each ACL is only checked
619 * Also, get the database version to use.
622 check_acl = ISC_TRUE; /* Keep compiler happy. */
626 * Get the current version of this database.
628 dbversion = query_findversion(client, db, &new_zone);
629 if (dbversion == NULL) {
630 result = DNS_R_SERVFAIL;
634 check_acl = ISC_TRUE;
635 } else if (!dbversion->queryok) {
638 check_acl = ISC_FALSE;
641 queryacl = dns_zone_getqueryacl(zone);
642 if (queryacl == NULL) {
643 queryacl = client->view->queryacl;
644 if ((client->query.attributes &
645 NS_QUERYATTR_QUERYOKVALID) != 0) {
647 * We've evaluated the view's queryacl already. If
648 * NS_QUERYATTR_QUERYOK is set, then the client is
649 * allowed to make queries, otherwise the query should
652 check_acl = ISC_FALSE;
653 if ((client->query.attributes &
654 NS_QUERYATTR_QUERYOK) == 0)
658 * We haven't evaluated the view's queryacl yet.
660 check_acl = ISC_TRUE;
665 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
667 result = ns_client_checkaclsilent(client, NULL, queryacl,
670 char msg[NS_CLIENT_ACLMSGSIZE("query")];
671 if (result == ISC_R_SUCCESS) {
672 if (isc_log_wouldlog(ns_g_lctx,
675 ns_client_aclmsg("query", name, qtype,
676 client->view->rdclass,
678 ns_client_log(client,
679 DNS_LOGCATEGORY_SECURITY,
685 ns_client_aclmsg("query", name, qtype,
686 client->view->rdclass,
688 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
689 NS_LOGMODULE_QUERY, ISC_LOG_INFO,
694 if (queryacl == client->view->queryacl) {
695 if (result == ISC_R_SUCCESS) {
697 * We were allowed by the default
698 * "allow-query" ACL. Remember this so we
699 * don't have to check again.
701 client->query.attributes |=
702 NS_QUERYATTR_QUERYOK;
705 * We've now evaluated the view's query ACL, and
706 * the NS_QUERYATTR_QUERYOK attribute is now valid.
708 client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
711 if (result != ISC_R_SUCCESS)
718 * Remember the result of the ACL check so we
719 * don't have to check again.
721 dbversion->queryok = ISC_TRUE;
723 /* Transfer ownership, if necessary. */
724 if (versionp != NULL)
725 *versionp = dbversion->version;
727 return (ISC_R_SUCCESS);
730 return (DNS_R_REFUSED);
736 static inline isc_result_t
737 query_getzonedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
738 unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
739 dns_dbversion_t **versionp)
742 unsigned int ztoptions;
743 dns_zone_t *zone = NULL;
745 isc_boolean_t partial = ISC_FALSE;
747 REQUIRE(zonep != NULL && *zonep == NULL);
748 REQUIRE(dbp != NULL && *dbp == NULL);
751 * Find a zone database to answer the query.
753 ztoptions = ((options & DNS_GETDB_NOEXACT) != 0) ?
754 DNS_ZTFIND_NOEXACT : 0;
756 result = dns_zt_find(client->view->zonetable, name, ztoptions, NULL,
758 if (result == DNS_R_PARTIALMATCH)
760 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
761 result = dns_zone_getdb(zone, &db);
763 if (result != ISC_R_SUCCESS)
766 result = query_validatezonedb(client, name, qtype, options, zone, db,
769 if (result != ISC_R_SUCCESS)
772 /* Transfer ownership. */
776 if (partial && (options & DNS_GETDB_PARTIAL) != 0)
777 return (DNS_R_PARTIALMATCH);
778 return (ISC_R_SUCCESS);
782 dns_zone_detach(&zone);
789 static inline isc_result_t
790 query_getcachedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
791 dns_db_t **dbp, unsigned int options)
794 isc_boolean_t check_acl;
797 REQUIRE(dbp != NULL && *dbp == NULL);
800 * Find a cache database to answer the query.
801 * This may fail with DNS_R_REFUSED if the client
802 * is not allowed to use the cache.
805 if (!USECACHE(client))
806 return (DNS_R_REFUSED);
807 dns_db_attach(client->view->cachedb, &db);
809 if ((client->query.attributes &
810 NS_QUERYATTR_QUERYOKVALID) != 0) {
812 * We've evaluated the view's queryacl already. If
813 * NS_QUERYATTR_QUERYOK is set, then the client is
814 * allowed to make queries, otherwise the query should
817 check_acl = ISC_FALSE;
818 if ((client->query.attributes &
819 NS_QUERYATTR_QUERYOK) == 0)
823 * We haven't evaluated the view's queryacl yet.
825 check_acl = ISC_TRUE;
829 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
830 char msg[NS_CLIENT_ACLMSGSIZE("query (cache)")];
832 result = ns_client_checkaclsilent(client, NULL,
833 client->view->queryacl,
835 if (result == ISC_R_SUCCESS) {
837 * We were allowed by the default
838 * "allow-query" ACL. Remember this so we
839 * don't have to check again.
841 client->query.attributes |=
842 NS_QUERYATTR_QUERYOK;
843 if (log && isc_log_wouldlog(ns_g_lctx,
846 ns_client_aclmsg("query (cache)", name, qtype,
847 client->view->rdclass,
849 ns_client_log(client,
850 DNS_LOGCATEGORY_SECURITY,
856 ns_client_aclmsg("query (cache)", name, qtype,
857 client->view->rdclass, msg,
859 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
860 NS_LOGMODULE_QUERY, ISC_LOG_INFO,
864 * We've now evaluated the view's query ACL, and
865 * the NS_QUERYATTR_QUERYOK attribute is now valid.
867 client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
869 if (result != ISC_R_SUCCESS)
875 /* Transfer ownership. */
878 return (ISC_R_SUCCESS);
881 result = DNS_R_REFUSED;
890 static inline isc_result_t
891 query_getdb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
892 unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
893 dns_dbversion_t **versionp, isc_boolean_t *is_zonep)
898 isc_result_t tresult;
899 unsigned int namelabels;
900 unsigned int zonelabels;
901 dns_zone_t *zone = NULL;
904 REQUIRE(zonep != NULL && *zonep == NULL);
908 /* Calculate how many labels are in name. */
909 namelabels = dns_name_countlabels(name);
912 /* Try to find name in bind's standard database. */
913 result = query_getzonedb(client, name, qtype, options, &zone,
916 /* See how many labels are in the zone's name. */
917 if (result == ISC_R_SUCCESS && zone != NULL)
918 zonelabels = dns_name_countlabels(dns_zone_getorigin(zone));
920 * If # zone labels < # name labels, try to find an even better match
921 * Only try if a DLZ driver is loaded for this view
923 if (zonelabels < namelabels && client->view->dlzdatabase != NULL) {
924 tresult = dns_dlzfindzone(client->view, name,
926 /* If we successful, we found a better match. */
927 if (tresult == ISC_R_SUCCESS) {
929 * If the previous search returned a zone, detach it.
932 dns_zone_detach(&zone);
935 * If the previous search returned a database,
942 * If the previous search returned a version, clear it.
947 * Get our database version.
949 dns_db_currentversion(tdbp, versionp);
952 * Be sure to return our database.
957 * We return a null zone, No stats for DLZ zones.
964 result = query_getzonedb(client, name, qtype, options,
965 zonep, dbp, versionp);
968 /* If successful, Transfer ownership of zone. */
969 if (result == ISC_R_SUCCESS) {
974 * If neither attempt above succeeded, return the cache instead
976 *is_zonep = ISC_TRUE;
977 } else if (result == ISC_R_NOTFOUND) {
978 result = query_getcachedb(client, name, qtype, dbp, options);
979 *is_zonep = ISC_FALSE;
984 static inline isc_boolean_t
985 query_isduplicate(ns_client_t *client, dns_name_t *name,
986 dns_rdatatype_t type, dns_name_t **mnamep)
988 dns_section_t section;
989 dns_name_t *mname = NULL;
992 CTRACE("query_isduplicate");
994 for (section = DNS_SECTION_ANSWER;
995 section <= DNS_SECTION_ADDITIONAL;
997 result = dns_message_findname(client->message, section,
998 name, type, 0, &mname, NULL);
999 if (result == ISC_R_SUCCESS) {
1001 * We've already got this RRset in the response.
1003 CTRACE("query_isduplicate: true: done");
1005 } else if (result == DNS_R_NXRRSET) {
1007 * The name exists, but the rdataset does not.
1009 if (section == DNS_SECTION_ADDITIONAL)
1012 RUNTIME_CHECK(result == DNS_R_NXDOMAIN);
1017 * If the dns_name_t we're looking up is already in the message,
1018 * we don't want to trigger the caller's name replacement logic.
1025 CTRACE("query_isduplicate: false: done");
1030 query_addadditional(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1031 ns_client_t *client = arg;
1032 isc_result_t result, eresult;
1035 dns_name_t *fname, *mname;
1036 dns_rdataset_t *rdataset, *sigrdataset, *trdataset;
1039 dns_dbversion_t *version;
1040 isc_boolean_t added_something, need_addname;
1042 dns_rdatatype_t type;
1044 REQUIRE(NS_CLIENT_VALID(client));
1045 REQUIRE(qtype != dns_rdatatype_any);
1047 if (!WANTDNSSEC(client) && dns_rdatatype_isdnssec(qtype))
1048 return (ISC_R_SUCCESS);
1050 CTRACE("query_addadditional");
1055 eresult = ISC_R_SUCCESS;
1063 added_something = ISC_FALSE;
1064 need_addname = ISC_FALSE;
1068 * We treat type A additional section processing as if it
1069 * were "any address type" additional section processing.
1070 * To avoid multiple lookups, we do an 'any' database
1071 * lookup and iterate over the node.
1073 if (qtype == dns_rdatatype_a)
1074 type = dns_rdatatype_any;
1079 * Get some resources.
1081 dbuf = query_getnamebuf(client);
1084 fname = query_newname(client, dbuf, &b);
1085 rdataset = query_newrdataset(client);
1086 if (fname == NULL || rdataset == NULL)
1088 if (WANTDNSSEC(client)) {
1089 sigrdataset = query_newrdataset(client);
1090 if (sigrdataset == NULL)
1095 * Look for a zone database that might contain authoritative
1098 result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1099 &zone, &db, &version);
1100 if (result != ISC_R_SUCCESS)
1103 CTRACE("query_addadditional: db_find");
1106 * Since we are looking for authoritative data, we do not set
1107 * the GLUEOK flag. Glue will be looked for later, but not
1108 * necessarily in the same database.
1111 result = dns_db_find(db, name, version, type, client->query.dboptions,
1112 client->now, &node, fname, rdataset,
1114 if (result == ISC_R_SUCCESS)
1117 if (dns_rdataset_isassociated(rdataset))
1118 dns_rdataset_disassociate(rdataset);
1119 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
1120 dns_rdataset_disassociate(sigrdataset);
1122 dns_db_detachnode(db, &node);
1127 * No authoritative data was found. The cache is our next best bet.
1131 result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1132 if (result != ISC_R_SUCCESS)
1134 * Most likely the client isn't allowed to query the cache.
1138 * Attempt to validate glue.
1140 if (sigrdataset == NULL) {
1141 sigrdataset = query_newrdataset(client);
1142 if (sigrdataset == NULL)
1145 result = dns_db_find(db, name, version, type,
1146 client->query.dboptions |
1147 DNS_DBFIND_GLUEOK | DNS_DBFIND_ADDITIONALOK,
1148 client->now, &node, fname, rdataset,
1150 if (result == DNS_R_GLUE &&
1151 validate(client, db, fname, rdataset, sigrdataset))
1152 result = ISC_R_SUCCESS;
1153 if (!WANTDNSSEC(client))
1154 query_putrdataset(client, &sigrdataset);
1155 if (result == ISC_R_SUCCESS)
1158 if (dns_rdataset_isassociated(rdataset))
1159 dns_rdataset_disassociate(rdataset);
1160 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
1161 dns_rdataset_disassociate(sigrdataset);
1163 dns_db_detachnode(db, &node);
1168 * No cached data was found. Glue is our last chance.
1171 * NS records cause both the usual additional section
1172 * processing to locate a type A record, and, when used
1173 * in a referral, a special search of the zone in which
1174 * they reside for glue information.
1176 * This is the "special search". Note that we must search
1177 * the zone where the NS record resides, not the zone it
1178 * points to, and that we only do the search in the delegation
1179 * case (identified by client->query.gluedb being set).
1182 if (client->query.gluedb == NULL)
1186 * Don't poison caches using the bailiwick protection model.
1188 if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1191 dns_db_attach(client->query.gluedb, &db);
1192 result = dns_db_find(db, name, version, type,
1193 client->query.dboptions | DNS_DBFIND_GLUEOK,
1194 client->now, &node, fname, rdataset,
1196 if (!(result == ISC_R_SUCCESS ||
1197 result == DNS_R_ZONECUT ||
1198 result == DNS_R_GLUE))
1203 * We have found a potential additional data rdataset, or
1204 * at least a node to iterate over.
1206 query_keepname(client, fname, dbuf);
1209 * If we have an rdataset, add it to the additional data
1213 if (dns_rdataset_isassociated(rdataset) &&
1214 !query_isduplicate(client, fname, type, &mname)) {
1215 if (mname != NULL) {
1216 query_releasename(client, &fname);
1219 need_addname = ISC_TRUE;
1220 ISC_LIST_APPEND(fname->list, rdataset, link);
1221 trdataset = rdataset;
1223 added_something = ISC_TRUE;
1225 * Note: we only add SIGs if we've added the type they cover,
1226 * so we do not need to check if the SIG rdataset is already
1229 if (sigrdataset != NULL &&
1230 dns_rdataset_isassociated(sigrdataset))
1232 ISC_LIST_APPEND(fname->list, sigrdataset, link);
1237 if (qtype == dns_rdatatype_a) {
1239 * We now go looking for A and AAAA records, along with
1242 * XXXRTH This code could be more efficient.
1244 if (rdataset != NULL) {
1245 if (dns_rdataset_isassociated(rdataset))
1246 dns_rdataset_disassociate(rdataset);
1248 rdataset = query_newrdataset(client);
1249 if (rdataset == NULL)
1252 if (sigrdataset != NULL) {
1253 if (dns_rdataset_isassociated(sigrdataset))
1254 dns_rdataset_disassociate(sigrdataset);
1255 } else if (WANTDNSSEC(client)) {
1256 sigrdataset = query_newrdataset(client);
1257 if (sigrdataset == NULL)
1260 result = dns_db_findrdataset(db, node, version,
1262 client->now, rdataset,
1264 if (result == DNS_R_NCACHENXDOMAIN)
1266 if (result == DNS_R_NCACHENXRRSET) {
1267 dns_rdataset_disassociate(rdataset);
1269 * Negative cache entries don't have sigrdatasets.
1271 INSIST(sigrdataset == NULL ||
1272 ! dns_rdataset_isassociated(sigrdataset));
1274 if (result == ISC_R_SUCCESS) {
1276 if (!query_isduplicate(client, fname,
1277 dns_rdatatype_a, &mname)) {
1278 if (mname != NULL) {
1279 query_releasename(client, &fname);
1282 need_addname = ISC_TRUE;
1283 ISC_LIST_APPEND(fname->list, rdataset, link);
1284 added_something = ISC_TRUE;
1285 if (sigrdataset != NULL &&
1286 dns_rdataset_isassociated(sigrdataset))
1288 ISC_LIST_APPEND(fname->list,
1291 query_newrdataset(client);
1293 rdataset = query_newrdataset(client);
1294 if (rdataset == NULL)
1296 if (WANTDNSSEC(client) && sigrdataset == NULL)
1299 dns_rdataset_disassociate(rdataset);
1300 if (sigrdataset != NULL &&
1301 dns_rdataset_isassociated(sigrdataset))
1302 dns_rdataset_disassociate(sigrdataset);
1305 result = dns_db_findrdataset(db, node, version,
1306 dns_rdatatype_aaaa, 0,
1307 client->now, rdataset,
1309 if (result == DNS_R_NCACHENXDOMAIN)
1311 if (result == DNS_R_NCACHENXRRSET) {
1312 dns_rdataset_disassociate(rdataset);
1313 INSIST(sigrdataset == NULL ||
1314 ! dns_rdataset_isassociated(sigrdataset));
1316 if (result == ISC_R_SUCCESS) {
1318 if (!query_isduplicate(client, fname,
1319 dns_rdatatype_aaaa, &mname)) {
1320 if (mname != NULL) {
1321 query_releasename(client, &fname);
1324 need_addname = ISC_TRUE;
1325 ISC_LIST_APPEND(fname->list, rdataset, link);
1326 added_something = ISC_TRUE;
1327 if (sigrdataset != NULL &&
1328 dns_rdataset_isassociated(sigrdataset))
1330 ISC_LIST_APPEND(fname->list,
1340 CTRACE("query_addadditional: addname");
1342 * If we haven't added anything, then we're done.
1344 if (!added_something)
1348 * We may have added our rdatasets to an existing name, if so, then
1349 * need_addname will be ISC_FALSE. Whether we used an existing name
1350 * or a new one, we must set fname to NULL to prevent cleanup.
1353 dns_message_addname(client->message, fname,
1354 DNS_SECTION_ADDITIONAL);
1358 * In a few cases, we want to add additional data for additional
1359 * data. It's simpler to just deal with special cases here than
1360 * to try to create a general purpose mechanism and allow the
1361 * rdata implementations to do it themselves.
1363 * This involves recursion, but the depth is limited. The
1364 * most complex case is adding a SRV rdataset, which involves
1365 * recursing to add address records, which in turn can cause
1366 * recursion to add KEYs.
1368 if (type == dns_rdatatype_srv && trdataset != NULL) {
1370 * If we're adding SRV records to the additional data
1371 * section, it's helpful if we add the SRV additional data
1374 eresult = dns_rdataset_additionaldata(trdataset,
1375 query_addadditional,
1380 CTRACE("query_addadditional: cleanup");
1381 query_putrdataset(client, &rdataset);
1382 if (sigrdataset != NULL)
1383 query_putrdataset(client, &sigrdataset);
1385 query_releasename(client, &fname);
1387 dns_db_detachnode(db, &node);
1391 dns_zone_detach(&zone);
1393 CTRACE("query_addadditional: done");
1398 query_discardcache(ns_client_t *client, dns_rdataset_t *rdataset_base,
1399 dns_rdatasetadditional_t additionaltype,
1400 dns_rdatatype_t type, dns_zone_t **zonep, dns_db_t **dbp,
1401 dns_dbversion_t **versionp, dns_dbnode_t **nodep,
1404 dns_rdataset_t *rdataset;
1406 while ((rdataset = ISC_LIST_HEAD(fname->list)) != NULL) {
1407 ISC_LIST_UNLINK(fname->list, rdataset, link);
1408 query_putrdataset(client, &rdataset);
1410 if (*versionp != NULL)
1411 dns_db_closeversion(*dbp, versionp, ISC_FALSE);
1413 dns_db_detachnode(*dbp, nodep);
1417 dns_zone_detach(zonep);
1418 (void)dns_rdataset_putadditional(client->view->acache, rdataset_base,
1419 additionaltype, type);
1422 static inline isc_result_t
1423 query_iscachevalid(dns_zone_t *zone, dns_db_t *db, dns_db_t *db0,
1424 dns_dbversion_t *version)
1426 isc_result_t result = ISC_R_SUCCESS;
1427 dns_dbversion_t *version_current = NULL;
1428 dns_db_t *db_current = db0;
1430 if (db_current == NULL) {
1431 result = dns_zone_getdb(zone, &db_current);
1432 if (result != ISC_R_SUCCESS)
1435 dns_db_currentversion(db_current, &version_current);
1436 if (db_current != db || version_current != version) {
1437 result = ISC_R_FAILURE;
1442 dns_db_closeversion(db_current, &version_current, ISC_FALSE);
1443 if (db0 == NULL && db_current != NULL)
1444 dns_db_detach(&db_current);
1450 query_addadditional2(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1451 client_additionalctx_t *additionalctx = arg;
1452 dns_rdataset_t *rdataset_base;
1453 ns_client_t *client;
1454 isc_result_t result, eresult;
1455 dns_dbnode_t *node, *cnode;
1457 dns_name_t *fname, *mname0, cfname;
1458 dns_rdataset_t *rdataset, *sigrdataset;
1459 dns_rdataset_t *crdataset, *crdataset_next;
1462 dns_dbversion_t *version, *cversion;
1463 isc_boolean_t added_something, need_addname, needadditionalcache;
1464 isc_boolean_t need_sigrrset;
1466 dns_rdatatype_t type;
1467 dns_rdatasetadditional_t additionaltype;
1469 if (qtype != dns_rdatatype_a) {
1471 * This function is optimized for "address" types. For other
1472 * types, use a generic routine.
1473 * XXX: ideally, this function should be generic enough.
1475 return (query_addadditional(additionalctx->client,
1482 rdataset_base = additionalctx->rdataset;
1483 client = additionalctx->client;
1484 REQUIRE(NS_CLIENT_VALID(client));
1485 eresult = ISC_R_SUCCESS;
1495 added_something = ISC_FALSE;
1496 need_addname = ISC_FALSE;
1498 needadditionalcache = ISC_FALSE;
1499 additionaltype = dns_rdatasetadditional_fromauth;
1500 dns_name_init(&cfname, NULL);
1502 CTRACE("query_addadditional2");
1505 * We treat type A additional section processing as if it
1506 * were "any address type" additional section processing.
1507 * To avoid multiple lookups, we do an 'any' database
1508 * lookup and iterate over the node.
1509 * XXXJT: this approach can cause a suboptimal result when the cache
1510 * DB only has partial address types and the glue DB has remaining
1513 type = dns_rdatatype_any;
1516 * Get some resources.
1518 dbuf = query_getnamebuf(client);
1521 fname = query_newname(client, dbuf, &b);
1524 dns_name_setbuffer(&cfname, &b); /* share the buffer */
1526 /* Check additional cache */
1527 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1528 type, client->view->acache, &zone,
1529 &cdb, &cversion, &cnode, &cfname,
1530 client->message, client->now);
1531 if (result != ISC_R_SUCCESS)
1534 CTRACE("query_addadditional2: auth zone not found");
1538 /* Is the cached DB up-to-date? */
1539 result = query_iscachevalid(zone, cdb, NULL, cversion);
1540 if (result != ISC_R_SUCCESS) {
1541 CTRACE("query_addadditional2: old auth additional cache");
1542 query_discardcache(client, rdataset_base, additionaltype,
1543 type, &zone, &cdb, &cversion, &cnode,
1548 if (cnode == NULL) {
1550 * We have a negative cache. We don't have to check the zone
1551 * ACL, since the result (not using this zone) would be same
1552 * regardless of the result.
1554 CTRACE("query_addadditional2: negative auth additional cache");
1555 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1556 dns_db_detach(&cdb);
1557 dns_zone_detach(&zone);
1561 result = query_validatezonedb(client, name, qtype, DNS_GETDB_NOLOG,
1563 if (result != ISC_R_SUCCESS) {
1564 query_discardcache(client, rdataset_base, additionaltype,
1565 type, &zone, &cdb, &cversion, &cnode,
1570 /* We've got an active cache. */
1571 CTRACE("query_addadditional2: auth additional cache");
1572 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1575 dns_name_clone(&cfname, fname);
1576 query_keepname(client, fname, dbuf);
1580 * Look for a zone database that might contain authoritative
1584 result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1585 &zone, &db, &version);
1586 if (result != ISC_R_SUCCESS) {
1587 /* Cache the negative result */
1588 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1589 type, client->view->acache,
1590 NULL, NULL, NULL, NULL,
1595 CTRACE("query_addadditional2: db_find");
1598 * Since we are looking for authoritative data, we do not set
1599 * the GLUEOK flag. Glue will be looked for later, but not
1600 * necessarily in the same database.
1603 result = dns_db_find(db, name, version, type, client->query.dboptions,
1604 client->now, &node, fname, NULL, NULL);
1605 if (result == ISC_R_SUCCESS)
1608 /* Cache the negative result */
1609 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1610 type, client->view->acache, zone, db,
1611 version, NULL, fname);
1614 dns_db_detachnode(db, &node);
1619 * No authoritative data was found. The cache is our next best bet.
1623 additionaltype = dns_rdatasetadditional_fromcache;
1624 result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1625 if (result != ISC_R_SUCCESS)
1627 * Most likely the client isn't allowed to query the cache.
1631 result = dns_db_find(db, name, version, type,
1632 client->query.dboptions |
1633 DNS_DBFIND_GLUEOK | DNS_DBFIND_ADDITIONALOK,
1634 client->now, &node, fname, NULL, NULL);
1635 if (result == ISC_R_SUCCESS)
1639 dns_db_detachnode(db, &node);
1644 * No cached data was found. Glue is our last chance.
1647 * NS records cause both the usual additional section
1648 * processing to locate a type A record, and, when used
1649 * in a referral, a special search of the zone in which
1650 * they reside for glue information.
1652 * This is the "special search". Note that we must search
1653 * the zone where the NS record resides, not the zone it
1654 * points to, and that we only do the search in the delegation
1655 * case (identified by client->query.gluedb being set).
1657 if (client->query.gluedb == NULL)
1661 * Don't poison caches using the bailiwick protection model.
1663 if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1666 /* Check additional cache */
1667 additionaltype = dns_rdatasetadditional_fromglue;
1668 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1669 type, client->view->acache, NULL,
1670 &cdb, &cversion, &cnode, &cfname,
1671 client->message, client->now);
1672 if (result != ISC_R_SUCCESS)
1675 result = query_iscachevalid(zone, cdb, client->query.gluedb, cversion);
1676 if (result != ISC_R_SUCCESS) {
1677 CTRACE("query_addadditional2: old glue additional cache");
1678 query_discardcache(client, rdataset_base, additionaltype,
1679 type, &zone, &cdb, &cversion, &cnode,
1684 if (cnode == NULL) {
1685 /* We have a negative cache. */
1686 CTRACE("query_addadditional2: negative glue additional cache");
1687 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1688 dns_db_detach(&cdb);
1693 CTRACE("query_addadditional2: glue additional cache");
1694 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1697 dns_name_clone(&cfname, fname);
1698 query_keepname(client, fname, dbuf);
1702 dns_db_attach(client->query.gluedb, &db);
1703 result = dns_db_find(db, name, version, type,
1704 client->query.dboptions | DNS_DBFIND_GLUEOK,
1705 client->now, &node, fname, NULL, NULL);
1706 if (!(result == ISC_R_SUCCESS ||
1707 result == DNS_R_ZONECUT ||
1708 result == DNS_R_GLUE)) {
1709 /* cache the negative result */
1710 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1711 type, client->view->acache,
1712 NULL, db, version, NULL,
1719 * We have found a DB node to iterate over from a DB.
1720 * We are going to look for address RRsets (i.e., A and AAAA) in the DB
1721 * node we've just found. We'll then store the complete information
1722 * in the additional data cache.
1724 dns_name_clone(fname, &cfname);
1725 query_keepname(client, fname, dbuf);
1726 needadditionalcache = ISC_TRUE;
1728 rdataset = query_newrdataset(client);
1729 if (rdataset == NULL)
1732 sigrdataset = query_newrdataset(client);
1733 if (sigrdataset == NULL)
1737 * Find A RRset with sig RRset. Even if we don't find a sig RRset
1738 * for a client using DNSSEC, we'll continue the process to make a
1739 * complete list to be cached. However, we need to cancel the
1740 * caching when something unexpected happens, in order to avoid
1741 * caching incomplete information.
1743 result = dns_db_findrdataset(db, node, version, dns_rdatatype_a, 0,
1744 client->now, rdataset, sigrdataset);
1746 * If we can't promote glue/pending from the cache to secure
1749 if (result == ISC_R_SUCCESS &&
1750 additionaltype == dns_rdatasetadditional_fromcache &&
1751 (DNS_TRUST_PENDING(rdataset->trust) ||
1752 DNS_TRUST_GLUE(rdataset->trust)) &&
1753 !validate(client, db, fname, rdataset, sigrdataset)) {
1754 dns_rdataset_disassociate(rdataset);
1755 if (dns_rdataset_isassociated(sigrdataset))
1756 dns_rdataset_disassociate(sigrdataset);
1757 result = ISC_R_NOTFOUND;
1759 if (result == DNS_R_NCACHENXDOMAIN)
1761 if (result == DNS_R_NCACHENXRRSET) {
1762 dns_rdataset_disassociate(rdataset);
1764 * Negative cache entries don't have sigrdatasets.
1766 INSIST(! dns_rdataset_isassociated(sigrdataset));
1768 if (result == ISC_R_SUCCESS) {
1769 /* Remember the result as a cache */
1770 ISC_LIST_APPEND(cfname.list, rdataset, link);
1771 if (dns_rdataset_isassociated(sigrdataset)) {
1772 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1773 sigrdataset = query_newrdataset(client);
1775 rdataset = query_newrdataset(client);
1776 if (sigrdataset == NULL || rdataset == NULL) {
1777 /* do not cache incomplete information */
1782 /* Find AAAA RRset with sig RRset */
1783 result = dns_db_findrdataset(db, node, version, dns_rdatatype_aaaa,
1784 0, client->now, rdataset, sigrdataset);
1786 * If we can't promote glue/pending from the cache to secure
1789 if (result == ISC_R_SUCCESS &&
1790 additionaltype == dns_rdatasetadditional_fromcache &&
1791 (DNS_TRUST_PENDING(rdataset->trust) ||
1792 DNS_TRUST_GLUE(rdataset->trust)) &&
1793 !validate(client, db, fname, rdataset, sigrdataset)) {
1794 dns_rdataset_disassociate(rdataset);
1795 if (dns_rdataset_isassociated(sigrdataset))
1796 dns_rdataset_disassociate(sigrdataset);
1797 result = ISC_R_NOTFOUND;
1799 if (result == ISC_R_SUCCESS) {
1800 ISC_LIST_APPEND(cfname.list, rdataset, link);
1802 if (dns_rdataset_isassociated(sigrdataset)) {
1803 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1810 * Set the new result in the cache if required. We do not support
1811 * caching additional data from a cache DB.
1813 if (needadditionalcache == ISC_TRUE &&
1814 (additionaltype == dns_rdatasetadditional_fromauth ||
1815 additionaltype == dns_rdatasetadditional_fromglue)) {
1816 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1817 type, client->view->acache,
1818 zone, db, version, node,
1823 need_sigrrset = ISC_FALSE;
1825 for (crdataset = ISC_LIST_HEAD(cfname.list);
1827 crdataset = crdataset_next) {
1830 crdataset_next = ISC_LIST_NEXT(crdataset, link);
1833 if (crdataset->type == dns_rdatatype_a ||
1834 crdataset->type == dns_rdatatype_aaaa) {
1835 if (!query_isduplicate(client, fname, crdataset->type,
1837 if (mname != NULL) {
1839 * A different type of this name is
1840 * already stored in the additional
1841 * section. We'll reuse the name.
1842 * Note that this should happen at most
1843 * once. Otherwise, fname->link could
1846 INSIST(mname0 == NULL);
1848 query_releasename(client, &fname);
1852 need_addname = ISC_TRUE;
1853 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1854 ISC_LIST_APPEND(fname->list, crdataset, link);
1855 added_something = ISC_TRUE;
1856 need_sigrrset = ISC_TRUE;
1858 need_sigrrset = ISC_FALSE;
1859 } else if (crdataset->type == dns_rdatatype_rrsig &&
1860 need_sigrrset && WANTDNSSEC(client)) {
1861 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1862 ISC_LIST_APPEND(fname->list, crdataset, link);
1863 added_something = ISC_TRUE; /* just in case */
1864 need_sigrrset = ISC_FALSE;
1868 CTRACE("query_addadditional2: addname");
1871 * If we haven't added anything, then we're done.
1873 if (!added_something)
1877 * We may have added our rdatasets to an existing name, if so, then
1878 * need_addname will be ISC_FALSE. Whether we used an existing name
1879 * or a new one, we must set fname to NULL to prevent cleanup.
1882 dns_message_addname(client->message, fname,
1883 DNS_SECTION_ADDITIONAL);
1887 CTRACE("query_addadditional2: cleanup");
1889 if (rdataset != NULL)
1890 query_putrdataset(client, &rdataset);
1891 if (sigrdataset != NULL)
1892 query_putrdataset(client, &sigrdataset);
1893 while ((crdataset = ISC_LIST_HEAD(cfname.list)) != NULL) {
1894 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1895 query_putrdataset(client, &crdataset);
1898 query_releasename(client, &fname);
1900 dns_db_detachnode(db, &node);
1904 dns_zone_detach(&zone);
1906 CTRACE("query_addadditional2: done");
1911 query_addrdataset(ns_client_t *client, dns_name_t *fname,
1912 dns_rdataset_t *rdataset)
1914 client_additionalctx_t additionalctx;
1917 * Add 'rdataset' and any pertinent additional data to
1918 * 'fname', a name in the response message for 'client'.
1921 CTRACE("query_addrdataset");
1923 ISC_LIST_APPEND(fname->list, rdataset, link);
1925 if (client->view->order != NULL)
1926 rdataset->attributes |= dns_order_find(client->view->order,
1927 fname, rdataset->type,
1929 rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
1931 if (NOADDITIONAL(client))
1935 * Add additional data.
1937 * We don't care if dns_rdataset_additionaldata() fails.
1939 additionalctx.client = client;
1940 additionalctx.rdataset = rdataset;
1941 (void)dns_rdataset_additionaldata(rdataset, query_addadditional2,
1943 CTRACE("query_addrdataset: done");
1947 query_addrrset(ns_client_t *client, dns_name_t **namep,
1948 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp,
1949 isc_buffer_t *dbuf, dns_section_t section)
1951 dns_name_t *name, *mname;
1952 dns_rdataset_t *rdataset, *mrdataset, *sigrdataset;
1953 isc_result_t result;
1956 * To the current response for 'client', add the answer RRset
1957 * '*rdatasetp' and an optional signature set '*sigrdatasetp', with
1958 * owner name '*namep', to section 'section', unless they are
1959 * already there. Also add any pertinent additional data.
1961 * If 'dbuf' is not NULL, then '*namep' is the name whose data is
1962 * stored in 'dbuf'. In this case, query_addrrset() guarantees that
1963 * when it returns the name will either have been kept or released.
1965 CTRACE("query_addrrset");
1967 rdataset = *rdatasetp;
1968 if (sigrdatasetp != NULL)
1969 sigrdataset = *sigrdatasetp;
1974 result = dns_message_findname(client->message, section,
1975 name, rdataset->type, rdataset->covers,
1976 &mname, &mrdataset);
1977 if (result == ISC_R_SUCCESS) {
1979 * We've already got an RRset of the given name and type.
1980 * There's nothing else to do;
1982 CTRACE("query_addrrset: dns_message_findname succeeded: done");
1984 query_releasename(client, namep);
1986 } else if (result == DNS_R_NXDOMAIN) {
1988 * The name doesn't exist.
1991 query_keepname(client, name, dbuf);
1992 dns_message_addname(client->message, name, section);
1996 RUNTIME_CHECK(result == DNS_R_NXRRSET);
1998 query_releasename(client, namep);
2001 if (rdataset->trust != dns_trust_secure &&
2002 (section == DNS_SECTION_ANSWER ||
2003 section == DNS_SECTION_AUTHORITY))
2004 client->query.attributes &= ~NS_QUERYATTR_SECURE;
2006 * Note: we only add SIGs if we've added the type they cover, so
2007 * we do not need to check if the SIG rdataset is already in the
2010 query_addrdataset(client, mname, rdataset);
2012 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset)) {
2014 * We have a signature. Add it to the response.
2016 ISC_LIST_APPEND(mname->list, sigrdataset, link);
2017 *sigrdatasetp = NULL;
2019 CTRACE("query_addrrset: done");
2022 static inline isc_result_t
2023 query_addsoa(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version,
2024 isc_boolean_t zero_ttl)
2028 isc_result_t result, eresult;
2029 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2030 dns_rdataset_t **sigrdatasetp = NULL;
2032 CTRACE("query_addsoa");
2036 eresult = ISC_R_SUCCESS;
2042 * Get resources and make 'name' be the database origin.
2044 result = dns_message_gettempname(client->message, &name);
2045 if (result != ISC_R_SUCCESS)
2047 dns_name_init(name, NULL);
2048 dns_name_clone(dns_db_origin(db), name);
2049 rdataset = query_newrdataset(client);
2050 if (rdataset == NULL) {
2051 eresult = DNS_R_SERVFAIL;
2054 if (WANTDNSSEC(client)) {
2055 sigrdataset = query_newrdataset(client);
2056 if (sigrdataset == NULL) {
2057 eresult = DNS_R_SERVFAIL;
2065 result = dns_db_getoriginnode(db, &node);
2066 if (result == ISC_R_SUCCESS) {
2067 result = dns_db_findrdataset(db, node, version,
2069 0, client->now, rdataset,
2072 dns_fixedname_t foundname;
2075 dns_fixedname_init(&foundname);
2076 fname = dns_fixedname_name(&foundname);
2078 result = dns_db_find(db, name, version, dns_rdatatype_soa,
2079 client->query.dboptions, 0, &node,
2080 fname, rdataset, sigrdataset);
2082 if (result != ISC_R_SUCCESS) {
2084 * This is bad. We tried to get the SOA RR at the zone top
2085 * and it didn't work!
2087 eresult = DNS_R_SERVFAIL;
2090 * Extract the SOA MINIMUM.
2092 dns_rdata_soa_t soa;
2093 dns_rdata_t rdata = DNS_RDATA_INIT;
2094 result = dns_rdataset_first(rdataset);
2095 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2096 dns_rdataset_current(rdataset, &rdata);
2097 result = dns_rdata_tostruct(&rdata, &soa, NULL);
2098 if (result != ISC_R_SUCCESS)
2103 if (sigrdataset != NULL)
2104 sigrdataset->ttl = 0;
2108 * Add the SOA and its SIG to the response, with the
2109 * TTLs adjusted per RFC2308 section 3.
2111 if (rdataset->ttl > soa.minimum)
2112 rdataset->ttl = soa.minimum;
2113 if (sigrdataset != NULL && sigrdataset->ttl > soa.minimum)
2114 sigrdataset->ttl = soa.minimum;
2116 if (sigrdataset != NULL)
2117 sigrdatasetp = &sigrdataset;
2119 sigrdatasetp = NULL;
2120 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2121 DNS_SECTION_AUTHORITY);
2125 query_putrdataset(client, &rdataset);
2126 if (sigrdataset != NULL)
2127 query_putrdataset(client, &sigrdataset);
2129 query_releasename(client, &name);
2131 dns_db_detachnode(db, &node);
2136 static inline isc_result_t
2137 query_addns(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version) {
2138 dns_name_t *name, *fname;
2140 isc_result_t result, eresult;
2141 dns_fixedname_t foundname;
2142 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2143 dns_rdataset_t **sigrdatasetp = NULL;
2145 CTRACE("query_addns");
2149 eresult = ISC_R_SUCCESS;
2153 dns_fixedname_init(&foundname);
2154 fname = dns_fixedname_name(&foundname);
2157 * Get resources and make 'name' be the database origin.
2159 result = dns_message_gettempname(client->message, &name);
2160 if (result != ISC_R_SUCCESS) {
2161 CTRACE("query_addns: dns_message_gettempname failed: done");
2164 dns_name_init(name, NULL);
2165 dns_name_clone(dns_db_origin(db), name);
2166 rdataset = query_newrdataset(client);
2167 if (rdataset == NULL) {
2168 CTRACE("query_addns: query_newrdataset failed");
2169 eresult = DNS_R_SERVFAIL;
2172 if (WANTDNSSEC(client)) {
2173 sigrdataset = query_newrdataset(client);
2174 if (sigrdataset == NULL) {
2175 CTRACE("query_addns: query_newrdataset failed");
2176 eresult = DNS_R_SERVFAIL;
2182 * Find the NS rdataset.
2184 result = dns_db_getoriginnode(db, &node);
2185 if (result == ISC_R_SUCCESS) {
2186 result = dns_db_findrdataset(db, node, version,
2188 0, client->now, rdataset,
2191 CTRACE("query_addns: calling dns_db_find");
2192 result = dns_db_find(db, name, NULL, dns_rdatatype_ns,
2193 client->query.dboptions, 0, &node,
2194 fname, rdataset, sigrdataset);
2195 CTRACE("query_addns: dns_db_find complete");
2197 if (result != ISC_R_SUCCESS) {
2198 CTRACE("query_addns: "
2199 "dns_db_findrdataset or dns_db_find failed");
2201 * This is bad. We tried to get the NS rdataset at the zone
2202 * top and it didn't work!
2204 eresult = DNS_R_SERVFAIL;
2206 if (sigrdataset != NULL)
2207 sigrdatasetp = &sigrdataset;
2209 sigrdatasetp = NULL;
2210 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2211 DNS_SECTION_AUTHORITY);
2215 CTRACE("query_addns: cleanup");
2216 query_putrdataset(client, &rdataset);
2217 if (sigrdataset != NULL)
2218 query_putrdataset(client, &sigrdataset);
2220 query_releasename(client, &name);
2222 dns_db_detachnode(db, &node);
2224 CTRACE("query_addns: done");
2228 static inline isc_result_t
2229 query_addcnamelike(ns_client_t *client, dns_name_t *qname, dns_name_t *tname,
2230 dns_trust_t trust, dns_name_t **anamep, dns_rdatatype_t type)
2232 dns_rdataset_t *rdataset;
2233 dns_rdatalist_t *rdatalist;
2235 isc_result_t result;
2239 * We assume the name data referred to by tname won't go away.
2242 REQUIRE(anamep != NULL);
2245 result = dns_message_gettemprdatalist(client->message, &rdatalist);
2246 if (result != ISC_R_SUCCESS)
2249 result = dns_message_gettemprdata(client->message, &rdata);
2250 if (result != ISC_R_SUCCESS)
2253 result = dns_message_gettemprdataset(client->message, &rdataset);
2254 if (result != ISC_R_SUCCESS)
2256 dns_rdataset_init(rdataset);
2257 result = dns_name_dup(qname, client->mctx, *anamep);
2258 if (result != ISC_R_SUCCESS) {
2259 dns_message_puttemprdataset(client->message, &rdataset);
2263 rdatalist->type = type;
2264 rdatalist->covers = 0;
2265 rdatalist->rdclass = client->message->rdclass;
2268 dns_name_toregion(tname, &r);
2269 rdata->data = r.base;
2270 rdata->length = r.length;
2271 rdata->rdclass = client->message->rdclass;
2274 ISC_LIST_INIT(rdatalist->rdata);
2275 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
2276 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
2278 rdataset->trust = trust;
2280 query_addrrset(client, anamep, &rdataset, NULL, NULL,
2281 DNS_SECTION_ANSWER);
2283 if (rdataset != NULL) {
2284 if (dns_rdataset_isassociated(rdataset))
2285 dns_rdataset_disassociate(rdataset);
2286 dns_message_puttemprdataset(client->message, &rdataset);
2289 return (ISC_R_SUCCESS);
2293 * Mark the RRsets as secure. Update the cache (db) to reflect the
2294 * change in trust level.
2297 mark_secure(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2298 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2300 isc_result_t result;
2301 dns_dbnode_t *node = NULL;
2303 rdataset->trust = dns_trust_secure;
2304 sigrdataset->trust = dns_trust_secure;
2307 * Save the updated secure state. Ignore failures.
2309 result = dns_db_findnode(db, name, ISC_TRUE, &node);
2310 if (result != ISC_R_SUCCESS)
2312 (void)dns_db_addrdataset(db, node, NULL, client->now, rdataset,
2314 (void)dns_db_addrdataset(db, node, NULL, client->now, sigrdataset,
2316 dns_db_detachnode(db, &node);
2320 * Find the secure key that corresponds to rrsig.
2321 * Note: 'keyrdataset' maintains state between successive calls,
2322 * there may be multiple keys with the same keyid.
2323 * Return ISC_FALSE if we have exhausted all the possible keys.
2325 static isc_boolean_t
2326 get_key(ns_client_t *client, dns_db_t *db, dns_rdata_rrsig_t *rrsig,
2327 dns_rdataset_t *keyrdataset, dst_key_t **keyp)
2329 isc_result_t result;
2330 dns_dbnode_t *node = NULL;
2331 isc_boolean_t secure = ISC_FALSE;
2333 if (!dns_rdataset_isassociated(keyrdataset)) {
2334 result = dns_db_findnode(db, &rrsig->signer, ISC_FALSE, &node);
2335 if (result != ISC_R_SUCCESS)
2338 result = dns_db_findrdataset(db, node, NULL,
2339 dns_rdatatype_dnskey, 0,
2340 client->now, keyrdataset, NULL);
2341 dns_db_detachnode(db, &node);
2342 if (result != ISC_R_SUCCESS)
2345 if (keyrdataset->trust != dns_trust_secure)
2348 result = dns_rdataset_first(keyrdataset);
2350 result = dns_rdataset_next(keyrdataset);
2352 for ( ; result == ISC_R_SUCCESS;
2353 result = dns_rdataset_next(keyrdataset)) {
2354 dns_rdata_t rdata = DNS_RDATA_INIT;
2357 dns_rdataset_current(keyrdataset, &rdata);
2358 isc_buffer_init(&b, rdata.data, rdata.length);
2359 isc_buffer_add(&b, rdata.length);
2360 result = dst_key_fromdns(&rrsig->signer, rdata.rdclass, &b,
2361 client->mctx, keyp);
2362 if (result != ISC_R_SUCCESS)
2364 if (rrsig->algorithm == (dns_secalg_t)dst_key_alg(*keyp) &&
2365 rrsig->keyid == (dns_keytag_t)dst_key_id(*keyp) &&
2366 dst_key_iszonekey(*keyp)) {
2375 static isc_boolean_t
2376 verify(dst_key_t *key, dns_name_t *name, dns_rdataset_t *rdataset,
2377 dns_rdata_t *rdata, isc_mem_t *mctx, isc_boolean_t acceptexpired)
2379 isc_result_t result;
2380 dns_fixedname_t fixed;
2381 isc_boolean_t ignore = ISC_FALSE;
2383 dns_fixedname_init(&fixed);
2386 result = dns_dnssec_verify2(name, rdataset, key, ignore, mctx,
2388 if (result == DNS_R_SIGEXPIRED && acceptexpired) {
2392 if (result == ISC_R_SUCCESS || result == DNS_R_FROMWILDCARD)
2398 * Validate the rdataset if possible with available records.
2400 static isc_boolean_t
2401 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2402 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2404 isc_result_t result;
2405 dns_rdata_t rdata = DNS_RDATA_INIT;
2406 dns_rdata_rrsig_t rrsig;
2407 dst_key_t *key = NULL;
2408 dns_rdataset_t keyrdataset;
2410 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2413 for (result = dns_rdataset_first(sigrdataset);
2414 result == ISC_R_SUCCESS;
2415 result = dns_rdataset_next(sigrdataset)) {
2417 dns_rdata_reset(&rdata);
2418 dns_rdataset_current(sigrdataset, &rdata);
2419 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
2420 if (result != ISC_R_SUCCESS)
2422 if (!dns_resolver_algorithm_supported(client->view->resolver,
2423 name, rrsig.algorithm))
2425 if (!dns_name_issubdomain(name, &rrsig.signer))
2427 dns_rdataset_init(&keyrdataset);
2429 if (!get_key(client, db, &rrsig, &keyrdataset, &key))
2431 if (verify(key, name, rdataset, &rdata, client->mctx,
2432 client->view->acceptexpired)) {
2434 dns_rdataset_disassociate(&keyrdataset);
2435 mark_secure(client, db, name, rdataset,
2441 if (dns_rdataset_isassociated(&keyrdataset))
2442 dns_rdataset_disassociate(&keyrdataset);
2448 query_addbestns(ns_client_t *client) {
2451 dns_name_t *fname, *zfname;
2452 dns_rdataset_t *rdataset, *sigrdataset, *zrdataset, *zsigrdataset;
2453 isc_boolean_t is_zone, use_zone;
2455 isc_result_t result;
2456 dns_dbversion_t *version;
2460 CTRACE("query_addbestns");
2466 zsigrdataset = NULL;
2472 is_zone = ISC_FALSE;
2473 use_zone = ISC_FALSE;
2476 * Find the right database.
2478 result = query_getdb(client, client->query.qname, dns_rdatatype_ns, 0,
2479 &zone, &db, &version, &is_zone);
2480 if (result != ISC_R_SUCCESS)
2485 * We'll need some resources...
2487 dbuf = query_getnamebuf(client);
2490 fname = query_newname(client, dbuf, &b);
2491 rdataset = query_newrdataset(client);
2492 if (fname == NULL || rdataset == NULL)
2495 * Get the RRSIGs if the client requested them or if we may
2496 * need to validate answers from the cache.
2498 if (WANTDNSSEC(client) || !is_zone) {
2499 sigrdataset = query_newrdataset(client);
2500 if (sigrdataset == NULL)
2505 * Now look for the zonecut.
2508 result = dns_db_find(db, client->query.qname, version,
2509 dns_rdatatype_ns, client->query.dboptions,
2510 client->now, &node, fname,
2511 rdataset, sigrdataset);
2512 if (result != DNS_R_DELEGATION)
2514 if (USECACHE(client)) {
2515 query_keepname(client, fname, dbuf);
2519 zrdataset = rdataset;
2521 zsigrdataset = sigrdataset;
2523 dns_db_detachnode(db, &node);
2526 dns_db_attach(client->view->cachedb, &db);
2527 is_zone = ISC_FALSE;
2531 result = dns_db_findzonecut(db, client->query.qname,
2532 client->query.dboptions,
2533 client->now, &node, fname,
2534 rdataset, sigrdataset);
2535 if (result == ISC_R_SUCCESS) {
2536 if (zfname != NULL &&
2537 !dns_name_issubdomain(fname, zfname)) {
2539 * We found a zonecut in the cache, but our
2540 * zone delegation is better.
2542 use_zone = ISC_TRUE;
2544 } else if (result == ISC_R_NOTFOUND && zfname != NULL) {
2546 * We didn't find anything in the cache, but we
2547 * have a zone delegation, so use it.
2549 use_zone = ISC_TRUE;
2555 query_releasename(client, &fname);
2559 * We've already done query_keepname() on
2560 * zfname, so we must set dbuf to NULL to
2561 * prevent query_addrrset() from trying to
2562 * call query_keepname() again.
2565 query_putrdataset(client, &rdataset);
2566 if (sigrdataset != NULL)
2567 query_putrdataset(client, &sigrdataset);
2568 rdataset = zrdataset;
2570 sigrdataset = zsigrdataset;
2571 zsigrdataset = NULL;
2575 * Attempt to validate RRsets that are pending or that are glue.
2577 if ((DNS_TRUST_PENDING(rdataset->trust) ||
2578 (sigrdataset != NULL && DNS_TRUST_PENDING(sigrdataset->trust)))
2579 && !validate(client, db, fname, rdataset, sigrdataset) &&
2580 !PENDINGOK(client->query.dboptions))
2583 if ((DNS_TRUST_GLUE(rdataset->trust) ||
2584 (sigrdataset != NULL && DNS_TRUST_GLUE(sigrdataset->trust))) &&
2585 !validate(client, db, fname, rdataset, sigrdataset) &&
2586 SECURE(client) && WANTDNSSEC(client))
2590 * If the client doesn't want DNSSEC we can discard the sigrdataset
2593 if (!WANTDNSSEC(client))
2594 query_putrdataset(client, &sigrdataset);
2595 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2596 DNS_SECTION_AUTHORITY);
2599 if (rdataset != NULL)
2600 query_putrdataset(client, &rdataset);
2601 if (sigrdataset != NULL)
2602 query_putrdataset(client, &sigrdataset);
2604 query_releasename(client, &fname);
2606 dns_db_detachnode(db, &node);
2610 dns_zone_detach(&zone);
2612 query_putrdataset(client, &zrdataset);
2613 if (zsigrdataset != NULL)
2614 query_putrdataset(client, &zsigrdataset);
2616 query_releasename(client, &zfname);
2617 dns_db_detach(&zdb);
2622 query_addds(ns_client_t *client, dns_db_t *db, dns_dbnode_t *node,
2623 dns_dbversion_t *version)
2626 dns_rdataset_t *rdataset, *sigrdataset;
2627 isc_result_t result;
2629 CTRACE("query_addds");
2635 * We'll need some resources...
2637 rdataset = query_newrdataset(client);
2638 sigrdataset = query_newrdataset(client);
2639 if (rdataset == NULL || sigrdataset == NULL)
2643 * Look for the DS record, which may or may not be present.
2645 result = dns_db_findrdataset(db, node, version, dns_rdatatype_ds, 0,
2646 client->now, rdataset, sigrdataset);
2648 * If we didn't find it, look for an NSEC. */
2649 if (result == ISC_R_NOTFOUND)
2650 result = dns_db_findrdataset(db, node, version,
2651 dns_rdatatype_nsec, 0, client->now,
2652 rdataset, sigrdataset);
2653 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
2655 if (!dns_rdataset_isassociated(rdataset) ||
2656 !dns_rdataset_isassociated(sigrdataset))
2660 * We've already added the NS record, so if the name's not there,
2661 * we have other problems. Use this name rather than calling
2664 result = dns_message_firstname(client->message, DNS_SECTION_AUTHORITY);
2665 if (result != ISC_R_SUCCESS)
2669 dns_message_currentname(client->message, DNS_SECTION_AUTHORITY,
2671 result = dns_message_findtype(rname, dns_rdatatype_ns, 0, NULL);
2672 if (result != ISC_R_SUCCESS)
2675 ISC_LIST_APPEND(rname->list, rdataset, link);
2676 ISC_LIST_APPEND(rname->list, sigrdataset, link);
2681 if (rdataset != NULL)
2682 query_putrdataset(client, &rdataset);
2683 if (sigrdataset != NULL)
2684 query_putrdataset(client, &sigrdataset);
2688 query_addwildcardproof(ns_client_t *client, dns_db_t *db,
2689 dns_dbversion_t *version, dns_name_t *name,
2690 isc_boolean_t ispositive)
2692 isc_buffer_t *dbuf, b;
2694 dns_rdataset_t *rdataset, *sigrdataset;
2695 dns_fixedname_t wfixed;
2698 unsigned int options;
2699 unsigned int olabels, nlabels;
2700 isc_result_t result;
2701 dns_rdata_t rdata = DNS_RDATA_INIT;
2702 dns_rdata_nsec_t nsec;
2703 isc_boolean_t have_wname;
2706 CTRACE("query_addwildcardproof");
2713 * Get the NOQNAME proof then if !ispositive
2714 * get the NOWILDCARD proof.
2716 * DNS_DBFIND_NOWILD finds the NSEC records that covers the
2717 * name ignoring any wildcard. From the owner and next names
2718 * of this record you can compute which wildcard (if it exists)
2719 * will match by finding the longest common suffix of the
2720 * owner name and next names with the qname and prefixing that
2721 * with the wildcard label.
2726 * example NSEC b.example
2728 * b.example NSEC a.d.example
2730 * a.d.example NSEC g.f.example
2732 * g.f.example NSEC z.i.example
2734 * z.i.example NSEC example
2737 * a.example -> example NSEC b.example
2738 * owner common example
2739 * next common example
2741 * d.b.example -> b.example NSEC a.d.example
2742 * owner common b.example
2743 * next common example
2745 * a.f.example -> a.d.example NSEC g.f.example
2746 * owner common example
2747 * next common f.example
2749 * j.example -> z.i.example NSEC example
2750 * owner common example
2751 * next common example
2754 options = client->query.dboptions | DNS_DBFIND_NOWILD;
2755 dns_fixedname_init(&wfixed);
2756 wname = dns_fixedname_name(&wfixed);
2758 have_wname = ISC_FALSE;
2760 * We'll need some resources...
2762 dbuf = query_getnamebuf(client);
2765 fname = query_newname(client, dbuf, &b);
2766 rdataset = query_newrdataset(client);
2767 sigrdataset = query_newrdataset(client);
2768 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2771 result = dns_db_find(db, name, version, dns_rdatatype_nsec, options,
2772 0, &node, fname, rdataset, sigrdataset);
2774 dns_db_detachnode(db, &node);
2775 if (result == DNS_R_NXDOMAIN) {
2777 result = dns_rdataset_first(rdataset);
2778 if (result == ISC_R_SUCCESS) {
2779 dns_rdataset_current(rdataset, &rdata);
2780 result = dns_rdata_tostruct(&rdata, &nsec, NULL);
2782 if (result == ISC_R_SUCCESS) {
2783 (void)dns_name_fullcompare(name, fname, &order,
2785 (void)dns_name_fullcompare(name, &nsec.next, &order,
2788 * Check for a pathological condition created when
2789 * serving some malformed signed zones and bail out.
2791 if (dns_name_countlabels(name) == nlabels)
2794 if (olabels > nlabels)
2795 dns_name_split(name, olabels, NULL, wname);
2797 dns_name_split(name, nlabels, NULL, wname);
2798 result = dns_name_concatenate(dns_wildcardname,
2799 wname, wname, NULL);
2800 if (result == ISC_R_SUCCESS)
2801 have_wname = ISC_TRUE;
2802 dns_rdata_freestruct(&nsec);
2804 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2805 dbuf, DNS_SECTION_AUTHORITY);
2807 if (rdataset != NULL)
2808 query_putrdataset(client, &rdataset);
2809 if (sigrdataset != NULL)
2810 query_putrdataset(client, &sigrdataset);
2812 query_releasename(client, &fname);
2814 ispositive = ISC_TRUE; /* prevent loop */
2815 if (!dns_name_equal(name, wname)) {
2821 if (rdataset != NULL)
2822 query_putrdataset(client, &rdataset);
2823 if (sigrdataset != NULL)
2824 query_putrdataset(client, &sigrdataset);
2826 query_releasename(client, &fname);
2830 query_addnxrrsetnsec(ns_client_t *client, dns_db_t *db,
2831 dns_dbversion_t *version, dns_name_t **namep,
2832 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp)
2835 dns_rdataset_t *sigrdataset;
2836 dns_rdata_t sigrdata;
2837 dns_rdata_rrsig_t sig;
2838 unsigned int labels;
2839 isc_buffer_t *dbuf, b;
2841 isc_result_t result;
2844 if ((name->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
2845 query_addrrset(client, namep, rdatasetp, sigrdatasetp,
2846 NULL, DNS_SECTION_AUTHORITY);
2850 if (sigrdatasetp == NULL)
2852 sigrdataset = *sigrdatasetp;
2853 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2855 result = dns_rdataset_first(sigrdataset);
2856 if (result != ISC_R_SUCCESS)
2858 dns_rdata_init(&sigrdata);
2859 dns_rdataset_current(sigrdataset, &sigrdata);
2860 result = dns_rdata_tostruct(&sigrdata, &sig, NULL);
2861 if (result != ISC_R_SUCCESS)
2864 labels = dns_name_countlabels(name);
2865 if ((unsigned int)sig.labels + 1 >= labels)
2869 query_addwildcardproof(client, db, version, client->query.qname,
2873 * We'll need some resources...
2875 dbuf = query_getnamebuf(client);
2878 fname = query_newname(client, dbuf, &b);
2881 dns_name_split(name, sig.labels + 1, NULL, fname);
2882 /* This will succeed, since we've stripped labels. */
2883 RUNTIME_CHECK(dns_name_concatenate(dns_wildcardname, fname, fname,
2884 NULL) == ISC_R_SUCCESS);
2885 query_addrrset(client, &fname, rdatasetp, sigrdatasetp,
2886 dbuf, DNS_SECTION_AUTHORITY);
2890 query_resume(isc_task_t *task, isc_event_t *event) {
2891 dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
2893 ns_client_t *client;
2894 isc_boolean_t fetch_canceled, client_shuttingdown;
2895 isc_result_t result;
2896 isc_logcategory_t *logcategory = NS_LOGCATEGORY_QUERY_EERRORS;
2900 * Resume a query after recursion.
2905 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
2906 client = devent->ev_arg;
2907 REQUIRE(NS_CLIENT_VALID(client));
2908 REQUIRE(task == client->task);
2909 REQUIRE(RECURSING(client));
2911 LOCK(&client->query.fetchlock);
2912 if (client->query.fetch != NULL) {
2914 * This is the fetch we've been waiting for.
2916 INSIST(devent->fetch == client->query.fetch);
2917 client->query.fetch = NULL;
2918 fetch_canceled = ISC_FALSE;
2920 * Update client->now.
2922 isc_stdtime_get(&client->now);
2925 * This is a fetch completion event for a canceled fetch.
2926 * Clean up and don't resume the find.
2928 fetch_canceled = ISC_TRUE;
2930 UNLOCK(&client->query.fetchlock);
2931 INSIST(client->query.fetch == NULL);
2933 client->query.attributes &= ~NS_QUERYATTR_RECURSING;
2934 fetch = devent->fetch;
2935 devent->fetch = NULL;
2938 * If this client is shutting down, or this transaction
2939 * has timed out, do not resume the find.
2941 client_shuttingdown = ns_client_shuttingdown(client);
2942 if (fetch_canceled || client_shuttingdown) {
2943 if (devent->node != NULL)
2944 dns_db_detachnode(devent->db, &devent->node);
2945 if (devent->db != NULL)
2946 dns_db_detach(&devent->db);
2947 query_putrdataset(client, &devent->rdataset);
2948 if (devent->sigrdataset != NULL)
2949 query_putrdataset(client, &devent->sigrdataset);
2950 isc_event_free(&event);
2952 query_error(client, DNS_R_SERVFAIL, __LINE__);
2954 query_next(client, ISC_R_CANCELED);
2956 * This may destroy the client.
2958 ns_client_detach(&client);
2960 result = query_find(client, devent, 0);
2961 if (result != ISC_R_SUCCESS) {
2962 if (result == DNS_R_SERVFAIL)
2963 errorloglevel = ISC_LOG_DEBUG(2);
2965 errorloglevel = ISC_LOG_DEBUG(4);
2966 if (isc_log_wouldlog(ns_g_lctx, errorloglevel)) {
2967 dns_resolver_logfetch(fetch, ns_g_lctx,
2970 errorloglevel, ISC_FALSE);
2975 dns_resolver_destroyfetch(&fetch);
2979 query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qdomain,
2980 dns_rdataset_t *nameservers, isc_boolean_t resuming)
2982 isc_result_t result;
2983 dns_rdataset_t *rdataset, *sigrdataset;
2984 isc_sockaddr_t *peeraddr;
2987 inc_stats(client, dns_nsstatscounter_recursion);
2990 * We are about to recurse, which means that this client will
2991 * be unavailable for serving new requests for an indeterminate
2992 * amount of time. If this client is currently responsible
2993 * for handling incoming queries, set up a new client
2994 * object to handle them while we are waiting for a
2995 * response. There is no need to replace TCP clients
2996 * because those have already been replaced when the
2997 * connection was accepted (if allowed by the TCP quota).
2999 if (client->recursionquota == NULL) {
3000 result = isc_quota_attach(&ns_g_server->recursionquota,
3001 &client->recursionquota);
3002 if (result == ISC_R_SOFTQUOTA) {
3003 static isc_stdtime_t last = 0;
3005 isc_stdtime_get(&now);
3008 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3011 "recursive-clients soft limit "
3012 "exceeded, aborting oldest query");
3014 ns_client_killoldestquery(client);
3015 result = ISC_R_SUCCESS;
3016 } else if (result == ISC_R_QUOTA) {
3017 static isc_stdtime_t last = 0;
3019 isc_stdtime_get(&now);
3022 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3025 "no more recursive clients: %s",
3026 isc_result_totext(result));
3028 ns_client_killoldestquery(client);
3030 if (result == ISC_R_SUCCESS && !client->mortal &&
3031 (client->attributes & NS_CLIENTATTR_TCP) == 0) {
3032 result = ns_client_replace(client);
3033 if (result != ISC_R_SUCCESS) {
3034 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3037 "ns_client_replace() failed: %s",
3038 isc_result_totext(result));
3039 isc_quota_detach(&client->recursionquota);
3042 if (result != ISC_R_SUCCESS)
3044 ns_client_recursing(client);
3048 * Invoke the resolver.
3050 REQUIRE(nameservers == NULL || nameservers->type == dns_rdatatype_ns);
3051 REQUIRE(client->query.fetch == NULL);
3053 rdataset = query_newrdataset(client);
3054 if (rdataset == NULL)
3055 return (ISC_R_NOMEMORY);
3056 if (WANTDNSSEC(client)) {
3057 sigrdataset = query_newrdataset(client);
3058 if (sigrdataset == NULL) {
3059 query_putrdataset(client, &rdataset);
3060 return (ISC_R_NOMEMORY);
3065 if (client->query.timerset == ISC_FALSE)
3066 ns_client_settimeout(client, 60);
3067 if ((client->attributes & NS_CLIENTATTR_TCP) == 0)
3068 peeraddr = &client->peeraddr;
3071 result = dns_resolver_createfetch2(client->view->resolver,
3072 client->query.qname,
3073 qtype, qdomain, nameservers,
3074 NULL, peeraddr, client->message->id,
3075 client->query.fetchoptions,
3077 query_resume, client,
3078 rdataset, sigrdataset,
3079 &client->query.fetch);
3081 if (result == ISC_R_SUCCESS) {
3083 * Record that we're waiting for an event. A client which
3084 * is shutting down will not be destroyed until all the
3085 * events have been received.
3088 query_putrdataset(client, &rdataset);
3089 if (sigrdataset != NULL)
3090 query_putrdataset(client, &sigrdataset);
3096 #define MAX_RESTARTS 16
3098 #define QUERY_ERROR(r) \
3101 want_restart = ISC_FALSE; \
3106 * Extract a network address from the RDATA of an A or AAAA
3111 * ISC_R_NOTIMPLEMENTED The rdata is not a known address type.
3114 rdata_tonetaddr(const dns_rdata_t *rdata, isc_netaddr_t *netaddr) {
3116 struct in6_addr in6a;
3118 switch (rdata->type) {
3119 case dns_rdatatype_a:
3120 INSIST(rdata->length == 4);
3121 memcpy(&ina.s_addr, rdata->data, 4);
3122 isc_netaddr_fromin(netaddr, &ina);
3123 return (ISC_R_SUCCESS);
3124 case dns_rdatatype_aaaa:
3125 INSIST(rdata->length == 16);
3126 memcpy(in6a.s6_addr, rdata->data, 16);
3127 isc_netaddr_fromin6(netaddr, &in6a);
3128 return (ISC_R_SUCCESS);
3130 return (ISC_R_NOTIMPLEMENTED);
3135 * Find the sort order of 'rdata' in the topology-like
3136 * ACL forming the second element in a 2-element top-level
3137 * sortlist statement.
3140 query_sortlist_order_2element(const dns_rdata_t *rdata, const void *arg) {
3141 isc_netaddr_t netaddr;
3143 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3145 return (ns_sortlist_addrorder2(&netaddr, arg));
3149 * Find the sort order of 'rdata' in the matching element
3150 * of a 1-element top-level sortlist statement.
3153 query_sortlist_order_1element(const dns_rdata_t *rdata, const void *arg) {
3154 isc_netaddr_t netaddr;
3156 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3158 return (ns_sortlist_addrorder1(&netaddr, arg));
3162 * Find the sortlist statement that applies to 'client' and set up
3163 * the sortlist info in in client->message appropriately.
3166 setup_query_sortlist(ns_client_t *client) {
3167 isc_netaddr_t netaddr;
3168 dns_rdatasetorderfunc_t order = NULL;
3169 const void *order_arg = NULL;
3171 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
3172 switch (ns_sortlist_setup(client->view->sortlist,
3173 &netaddr, &order_arg)) {
3174 case NS_SORTLISTTYPE_1ELEMENT:
3175 order = query_sortlist_order_1element;
3177 case NS_SORTLISTTYPE_2ELEMENT:
3178 order = query_sortlist_order_2element;
3180 case NS_SORTLISTTYPE_NONE:
3187 dns_message_setsortorder(client->message, order, order_arg);
3191 query_addnoqnameproof(ns_client_t *client, dns_rdataset_t *rdataset) {
3192 isc_buffer_t *dbuf, b;
3194 dns_rdataset_t *nsec, *nsecsig;
3195 isc_result_t result = ISC_R_NOMEMORY;
3197 CTRACE("query_addnoqnameproof");
3203 dbuf = query_getnamebuf(client);
3206 fname = query_newname(client, dbuf, &b);
3207 nsec = query_newrdataset(client);
3208 nsecsig = query_newrdataset(client);
3209 if (fname == NULL || nsec == NULL || nsecsig == NULL)
3212 result = dns_rdataset_getnoqname(rdataset, fname, nsec, nsecsig);
3213 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3215 query_addrrset(client, &fname, &nsec, &nsecsig, dbuf,
3216 DNS_SECTION_AUTHORITY);
3220 query_putrdataset(client, &nsec);
3221 if (nsecsig != NULL)
3222 query_putrdataset(client, &nsecsig);
3224 query_releasename(client, &fname);
3228 answer_in_glue(ns_client_t *client, dns_rdatatype_t qtype) {
3231 dns_section_t section = DNS_SECTION_ADDITIONAL;
3232 dns_rdataset_t *rdataset = NULL;
3234 msg = client->message;
3235 for (name = ISC_LIST_HEAD(msg->sections[section]);
3237 name = ISC_LIST_NEXT(name, link))
3238 if (dns_name_equal(name, client->query.qname)) {
3239 for (rdataset = ISC_LIST_HEAD(name->list);
3241 rdataset = ISC_LIST_NEXT(rdataset, link))
3242 if (rdataset->type == qtype)
3246 if (rdataset != NULL) {
3247 ISC_LIST_UNLINK(msg->sections[section], name, link);
3248 ISC_LIST_PREPEND(msg->sections[section], name, link);
3249 ISC_LIST_UNLINK(name->list, rdataset, link);
3250 ISC_LIST_PREPEND(name->list, rdataset, link);
3251 rdataset->attributes |= DNS_RDATASETATTR_REQUIREDGLUE;
3255 #define NS_NAME_INIT(A,B) \
3258 A, sizeof(A), sizeof(B), \
3259 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE, \
3260 B, NULL, { (void *)-1, (void *)-1}, \
3264 static unsigned char inaddr10_offsets[] = { 0, 3, 11, 16 };
3265 static unsigned char inaddr172_offsets[] = { 0, 3, 7, 15, 20 };
3266 static unsigned char inaddr192_offsets[] = { 0, 4, 8, 16, 21 };
3268 static unsigned char inaddr10[] = "\00210\007IN-ADDR\004ARPA";
3270 static unsigned char inaddr16172[] = "\00216\003172\007IN-ADDR\004ARPA";
3271 static unsigned char inaddr17172[] = "\00217\003172\007IN-ADDR\004ARPA";
3272 static unsigned char inaddr18172[] = "\00218\003172\007IN-ADDR\004ARPA";
3273 static unsigned char inaddr19172[] = "\00219\003172\007IN-ADDR\004ARPA";
3274 static unsigned char inaddr20172[] = "\00220\003172\007IN-ADDR\004ARPA";
3275 static unsigned char inaddr21172[] = "\00221\003172\007IN-ADDR\004ARPA";
3276 static unsigned char inaddr22172[] = "\00222\003172\007IN-ADDR\004ARPA";
3277 static unsigned char inaddr23172[] = "\00223\003172\007IN-ADDR\004ARPA";
3278 static unsigned char inaddr24172[] = "\00224\003172\007IN-ADDR\004ARPA";
3279 static unsigned char inaddr25172[] = "\00225\003172\007IN-ADDR\004ARPA";
3280 static unsigned char inaddr26172[] = "\00226\003172\007IN-ADDR\004ARPA";
3281 static unsigned char inaddr27172[] = "\00227\003172\007IN-ADDR\004ARPA";
3282 static unsigned char inaddr28172[] = "\00228\003172\007IN-ADDR\004ARPA";
3283 static unsigned char inaddr29172[] = "\00229\003172\007IN-ADDR\004ARPA";
3284 static unsigned char inaddr30172[] = "\00230\003172\007IN-ADDR\004ARPA";
3285 static unsigned char inaddr31172[] = "\00231\003172\007IN-ADDR\004ARPA";
3287 static unsigned char inaddr168192[] = "\003168\003192\007IN-ADDR\004ARPA";
3289 static dns_name_t rfc1918names[] = {
3290 NS_NAME_INIT(inaddr10, inaddr10_offsets),
3291 NS_NAME_INIT(inaddr16172, inaddr172_offsets),
3292 NS_NAME_INIT(inaddr17172, inaddr172_offsets),
3293 NS_NAME_INIT(inaddr18172, inaddr172_offsets),
3294 NS_NAME_INIT(inaddr19172, inaddr172_offsets),
3295 NS_NAME_INIT(inaddr20172, inaddr172_offsets),
3296 NS_NAME_INIT(inaddr21172, inaddr172_offsets),
3297 NS_NAME_INIT(inaddr22172, inaddr172_offsets),
3298 NS_NAME_INIT(inaddr23172, inaddr172_offsets),
3299 NS_NAME_INIT(inaddr24172, inaddr172_offsets),
3300 NS_NAME_INIT(inaddr25172, inaddr172_offsets),
3301 NS_NAME_INIT(inaddr26172, inaddr172_offsets),
3302 NS_NAME_INIT(inaddr27172, inaddr172_offsets),
3303 NS_NAME_INIT(inaddr28172, inaddr172_offsets),
3304 NS_NAME_INIT(inaddr29172, inaddr172_offsets),
3305 NS_NAME_INIT(inaddr30172, inaddr172_offsets),
3306 NS_NAME_INIT(inaddr31172, inaddr172_offsets),
3307 NS_NAME_INIT(inaddr168192, inaddr192_offsets)
3311 static unsigned char prisoner_data[] = "\010prisoner\004iana\003org";
3312 static unsigned char hostmaster_data[] = "\012hostmaster\014root-servers\003org";
3314 static unsigned char prisoner_offsets[] = { 0, 9, 14, 18 };
3315 static unsigned char hostmaster_offsets[] = { 0, 11, 24, 28 };
3317 static dns_name_t prisoner = NS_NAME_INIT(prisoner_data, prisoner_offsets);
3318 static dns_name_t hostmaster = NS_NAME_INIT(hostmaster_data, hostmaster_offsets);
3321 warn_rfc1918(ns_client_t *client, dns_name_t *fname, dns_rdataset_t *rdataset) {
3323 dns_rdata_t rdata = DNS_RDATA_INIT;
3324 dns_rdata_soa_t soa;
3325 dns_rdataset_t found;
3326 isc_result_t result;
3328 for (i = 0; i < (sizeof(rfc1918names)/sizeof(*rfc1918names)); i++) {
3329 if (dns_name_issubdomain(fname, &rfc1918names[i])) {
3330 dns_rdataset_init(&found);
3331 result = dns_ncache_getrdataset(rdataset,
3335 if (result != ISC_R_SUCCESS)
3338 result = dns_rdataset_first(&found);
3339 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3340 dns_rdataset_current(&found, &rdata);
3341 result = dns_rdata_tostruct(&rdata, &soa, NULL);
3342 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3343 if (dns_name_equal(&soa.origin, &prisoner) &&
3344 dns_name_equal(&soa.contact, &hostmaster)) {
3345 char buf[DNS_NAME_FORMATSIZE];
3346 dns_name_format(fname, buf, sizeof(buf));
3347 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3350 "RFC 1918 response from "
3351 "Internet for %s", buf);
3353 dns_rdataset_disassociate(&found);
3360 * Do the bulk of query processing for the current query of 'client'.
3361 * If 'event' is non-NULL, we are returning from recursion and 'qtype'
3362 * is ignored. Otherwise, 'qtype' is the query type.
3365 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
3369 dns_rdatatype_t type;
3370 dns_name_t *fname, *zfname, *tname, *prefix;
3371 dns_rdataset_t *rdataset, *trdataset;
3372 dns_rdataset_t *sigrdataset, *zrdataset, *zsigrdataset;
3373 dns_rdataset_t **sigrdatasetp;
3374 dns_rdata_t rdata = DNS_RDATA_INIT;
3375 dns_rdatasetiter_t *rdsiter;
3376 isc_boolean_t want_restart, authoritative, is_zone, need_wildcardproof;
3377 unsigned int n, nlabels;
3378 dns_namereln_t namereln;
3382 isc_result_t result, eresult;
3383 dns_fixedname_t fixed;
3384 dns_fixedname_t wildcardname;
3385 dns_dbversion_t *version;
3387 dns_rdata_cname_t cname;
3388 dns_rdata_dname_t dname;
3389 unsigned int options;
3390 isc_boolean_t empty_wild;
3391 dns_rdataset_t *noqname;
3392 isc_boolean_t resuming;
3395 CTRACE("query_find");
3398 * One-time initialization.
3400 * It's especially important to initialize anything that the cleanup
3401 * code might cleanup.
3404 eresult = ISC_R_SUCCESS;
3410 zsigrdataset = NULL;
3416 need_wildcardproof = ISC_FALSE;
3417 empty_wild = ISC_FALSE;
3419 resuming = ISC_FALSE;
3420 is_zone = ISC_FALSE;
3422 if (event != NULL) {
3424 * We're returning from recursion. Restore the query context
3428 want_restart = ISC_FALSE;
3429 authoritative = ISC_FALSE;
3431 qtype = event->qtype;
3432 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3433 type = dns_rdatatype_any;
3438 rdataset = event->rdataset;
3439 sigrdataset = event->sigrdataset;
3442 * We'll need some resources...
3444 dbuf = query_getnamebuf(client);
3446 QUERY_ERROR(DNS_R_SERVFAIL);
3449 fname = query_newname(client, dbuf, &b);
3450 if (fname == NULL) {
3451 QUERY_ERROR(DNS_R_SERVFAIL);
3454 tname = dns_fixedname_name(&event->foundname);
3455 result = dns_name_copy(tname, fname, NULL);
3456 if (result != ISC_R_SUCCESS) {
3457 QUERY_ERROR(DNS_R_SERVFAIL);
3461 result = event->result;
3462 resuming = ISC_TRUE;
3468 * Not returning from recursion.
3472 * If it's a SIG query, we'll iterate the node.
3474 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3475 type = dns_rdatatype_any;
3480 CTRACE("query_find: restart");
3481 want_restart = ISC_FALSE;
3482 authoritative = ISC_FALSE;
3484 need_wildcardproof = ISC_FALSE;
3486 if (client->view->checknames &&
3487 !dns_rdata_checkowner(client->query.qname,
3488 client->message->rdclass,
3489 qtype, ISC_FALSE)) {
3490 char namebuf[DNS_NAME_FORMATSIZE];
3491 char typename[DNS_RDATATYPE_FORMATSIZE];
3492 char classname[DNS_RDATACLASS_FORMATSIZE];
3494 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
3495 dns_rdatatype_format(qtype, typename, sizeof(typename));
3496 dns_rdataclass_format(client->message->rdclass, classname,
3498 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3499 NS_LOGMODULE_QUERY, ISC_LOG_ERROR,
3500 "check-names failure %s/%s/%s", namebuf,
3501 typename, classname);
3502 QUERY_ERROR(DNS_R_REFUSED);
3507 * First we must find the right database.
3509 options &= DNS_GETDB_NOLOG; /* Preserve DNS_GETDB_NOLOG. */
3510 if (dns_rdatatype_atparent(qtype) &&
3511 !dns_name_equal(client->query.qname, dns_rootname))
3512 options |= DNS_GETDB_NOEXACT;
3513 result = query_getdb(client, client->query.qname, qtype, options,
3514 &zone, &db, &version, &is_zone);
3515 if ((result != ISC_R_SUCCESS || !is_zone) && !RECURSIONOK(client) &&
3516 (options & DNS_GETDB_NOEXACT) != 0 && qtype == dns_rdatatype_ds) {
3518 * Look to see if we are authoritative for the
3519 * child zone if the query type is DS.
3521 dns_db_t *tdb = NULL;
3522 dns_zone_t *tzone = NULL;
3523 dns_dbversion_t *tversion = NULL;
3524 isc_result_t tresult;
3526 tresult = query_getzonedb(client, client->query.qname, qtype,
3527 DNS_GETDB_PARTIAL, &tzone, &tdb,
3529 if (tresult == ISC_R_SUCCESS) {
3530 options &= ~DNS_GETDB_NOEXACT;
3531 query_putrdataset(client, &rdataset);
3535 dns_zone_detach(&zone);
3540 result = ISC_R_SUCCESS;
3543 dns_db_detach(&tdb);
3545 dns_zone_detach(&tzone);
3548 if (result != ISC_R_SUCCESS) {
3549 if (result == DNS_R_REFUSED) {
3550 if (WANTRECURSION(client)) {
3552 dns_nsstatscounter_recurserej);
3554 inc_stats(client, dns_nsstatscounter_authrej);
3555 if (!PARTIALANSWER(client))
3556 QUERY_ERROR(DNS_R_REFUSED);
3558 QUERY_ERROR(DNS_R_SERVFAIL);
3563 authoritative = ISC_TRUE;
3565 if (event == NULL && client->query.restarts == 0) {
3570 * if is_zone = true, zone = NULL then this is
3571 * a DLZ zone. Don't attempt to attach zone.
3574 dns_zone_attach(zone, &client->query.authzone);
3578 dns_db_attach(db, &client->query.authdb);
3580 client->query.authdbset = ISC_TRUE;
3584 CTRACE("query_find: db_find");
3586 * We'll need some resources...
3588 dbuf = query_getnamebuf(client);
3590 QUERY_ERROR(DNS_R_SERVFAIL);
3593 fname = query_newname(client, dbuf, &b);
3594 rdataset = query_newrdataset(client);
3595 if (fname == NULL || rdataset == NULL) {
3596 QUERY_ERROR(DNS_R_SERVFAIL);
3599 if (WANTDNSSEC(client)) {
3600 sigrdataset = query_newrdataset(client);
3601 if (sigrdataset == NULL) {
3602 QUERY_ERROR(DNS_R_SERVFAIL);
3608 * Now look for an answer in the database.
3610 result = dns_db_find(db, client->query.qname, version, type,
3611 client->query.dboptions, client->now,
3612 &node, fname, rdataset, sigrdataset);
3615 CTRACE("query_find: resume");
3619 * This case is handled in the main line below.
3625 * These cases are handled in the main line below.
3628 authoritative = ISC_FALSE;
3630 case ISC_R_NOTFOUND:
3632 * The cache doesn't even have the root NS. Get them from
3639 if (client->view->hints == NULL) {
3640 /* We have no hints. */
3641 result = ISC_R_FAILURE;
3643 dns_db_attach(client->view->hints, &db);
3644 result = dns_db_find(db, dns_rootname,
3645 NULL, dns_rdatatype_ns,
3646 0, client->now, &node, fname,
3647 rdataset, sigrdataset);
3649 if (result != ISC_R_SUCCESS) {
3651 * Nonsensical root hints may require cleanup.
3653 if (dns_rdataset_isassociated(rdataset))
3654 dns_rdataset_disassociate(rdataset);
3655 if (sigrdataset != NULL &&
3656 dns_rdataset_isassociated(sigrdataset))
3657 dns_rdataset_disassociate(sigrdataset);
3659 dns_db_detachnode(db, &node);
3662 * We don't have any root server hints, but
3663 * we may have working forwarders, so try to
3666 if (RECURSIONOK(client)) {
3667 result = query_recurse(client, qtype,
3668 NULL, NULL, resuming);
3669 if (result == ISC_R_SUCCESS)
3670 client->query.attributes |=
3671 NS_QUERYATTR_RECURSING;
3672 else if (result == DNS_R_DUPLICATE ||
3673 result == DNS_R_DROP) {
3674 /* Duplicate query. */
3675 QUERY_ERROR(result);
3677 /* Unable to recurse. */
3678 QUERY_ERROR(DNS_R_SERVFAIL);
3682 /* Unable to give root server referral. */
3683 QUERY_ERROR(DNS_R_SERVFAIL);
3688 * XXXRTH We should trigger root server priming here.
3691 case DNS_R_DELEGATION:
3692 authoritative = ISC_FALSE;
3695 * Look to see if we are authoritative for the
3696 * child zone if the query type is DS.
3698 if (!RECURSIONOK(client) &&
3699 (options & DNS_GETDB_NOEXACT) != 0 &&
3700 qtype == dns_rdatatype_ds) {
3701 dns_db_t *tdb = NULL;
3702 dns_zone_t *tzone = NULL;
3703 dns_dbversion_t *tversion = NULL;
3704 result = query_getzonedb(client,
3705 client->query.qname,
3710 if (result == ISC_R_SUCCESS) {
3711 options &= ~DNS_GETDB_NOEXACT;
3712 query_putrdataset(client, &rdataset);
3713 if (sigrdataset != NULL)
3714 query_putrdataset(client,
3717 query_releasename(client,
3720 dns_db_detachnode(db, &node);
3724 dns_zone_detach(&zone);
3728 authoritative = ISC_TRUE;
3732 dns_db_detach(&tdb);
3734 dns_zone_detach(&tzone);
3737 * We're authoritative for an ancestor of QNAME.
3739 if (!USECACHE(client) || !RECURSIONOK(client)) {
3741 * If we don't have a cache, this is the best
3744 * If the client is making a nonrecursive
3745 * query we always give out the authoritative
3746 * delegation. This way even if we get
3747 * junk in our cache, we won't fail in our
3748 * role as the delegating authority if another
3749 * nameserver asks us about a delegated
3752 * We enable the retrieval of glue for this
3753 * database by setting client->query.gluedb.
3755 client->query.gluedb = db;
3756 client->query.isreferral = ISC_TRUE;
3758 * We must ensure NOADDITIONAL is off,
3759 * because the generation of
3760 * additional data is required in
3763 client->query.attributes &=
3764 ~NS_QUERYATTR_NOADDITIONAL;
3765 if (sigrdataset != NULL)
3766 sigrdatasetp = &sigrdataset;
3768 sigrdatasetp = NULL;
3769 query_addrrset(client, &fname,
3770 &rdataset, sigrdatasetp,
3771 dbuf, DNS_SECTION_AUTHORITY);
3772 client->query.gluedb = NULL;
3773 if (WANTDNSSEC(client) && dns_db_issecure(db))
3774 query_addds(client, db, node, version);
3777 * We might have a better answer or delegation
3778 * in the cache. We'll remember the current
3779 * values of fname, rdataset, and sigrdataset.
3780 * We'll then go looking for QNAME in the
3781 * cache. If we find something better, we'll
3784 query_keepname(client, fname, dbuf);
3788 zrdataset = rdataset;
3790 zsigrdataset = sigrdataset;
3792 dns_db_detachnode(db, &node);
3795 dns_db_attach(client->view->cachedb, &db);
3796 is_zone = ISC_FALSE;
3800 if (zfname != NULL &&
3801 !dns_name_issubdomain(fname, zfname)) {
3803 * We've already got a delegation from
3804 * authoritative data, and it is better
3805 * than what we found in the cache. Use
3806 * it instead of the cache delegation.
3808 query_releasename(client, &fname);
3812 * We've already done query_keepname() on
3813 * zfname, so we must set dbuf to NULL to
3814 * prevent query_addrrset() from trying to
3815 * call query_keepname() again.
3818 query_putrdataset(client, &rdataset);
3819 if (sigrdataset != NULL)
3820 query_putrdataset(client,
3822 rdataset = zrdataset;
3824 sigrdataset = zsigrdataset;
3825 zsigrdataset = NULL;
3827 * We don't clean up zdb here because we
3828 * may still need it. It will get cleaned
3829 * up by the main cleanup code.
3833 if (RECURSIONOK(client)) {
3837 if (dns_rdatatype_atparent(type))
3838 result = query_recurse(client, qtype,
3842 result = query_recurse(client, qtype,
3845 if (result == ISC_R_SUCCESS)
3846 client->query.attributes |=
3847 NS_QUERYATTR_RECURSING;
3848 else if (result == DNS_R_DUPLICATE ||
3849 result == DNS_R_DROP)
3850 QUERY_ERROR(result);
3852 QUERY_ERROR(DNS_R_SERVFAIL);
3855 * This is the best answer.
3857 client->query.attributes |=
3858 NS_QUERYATTR_CACHEGLUEOK;
3859 client->query.gluedb = zdb;
3860 client->query.isreferral = ISC_TRUE;
3862 * We must ensure NOADDITIONAL is off,
3863 * because the generation of
3864 * additional data is required in
3867 client->query.attributes &=
3868 ~NS_QUERYATTR_NOADDITIONAL;
3869 if (sigrdataset != NULL)
3870 sigrdatasetp = &sigrdataset;
3872 sigrdatasetp = NULL;
3873 query_addrrset(client, &fname,
3874 &rdataset, sigrdatasetp,
3875 dbuf, DNS_SECTION_AUTHORITY);
3876 client->query.gluedb = NULL;
3877 client->query.attributes &=
3878 ~NS_QUERYATTR_CACHEGLUEOK;
3879 if (WANTDNSSEC(client))
3880 query_addds(client, db, node, version);
3884 case DNS_R_EMPTYNAME:
3885 result = DNS_R_NXRRSET;
3889 if (dns_rdataset_isassociated(rdataset)) {
3891 * If we've got a NSEC record, we need to save the
3892 * name now because we're going call query_addsoa()
3893 * below, and it needs to use the name buffer.
3895 query_keepname(client, fname, dbuf);
3898 * We're not going to use fname, and need to release
3899 * our hold on the name buffer so query_addsoa()
3902 query_releasename(client, &fname);
3907 result = query_addsoa(client, db, version, ISC_FALSE);
3908 if (result != ISC_R_SUCCESS) {
3909 QUERY_ERROR(result);
3913 * Add NSEC record if we found one.
3915 if (WANTDNSSEC(client)) {
3916 if (dns_rdataset_isassociated(rdataset))
3917 query_addnxrrsetnsec(client, db, version,
3922 case DNS_R_EMPTYWILD:
3923 empty_wild = ISC_TRUE;
3925 case DNS_R_NXDOMAIN:
3927 if (dns_rdataset_isassociated(rdataset)) {
3929 * If we've got a NSEC record, we need to save the
3930 * name now because we're going call query_addsoa()
3931 * below, and it needs to use the name buffer.
3933 query_keepname(client, fname, dbuf);
3936 * We're not going to use fname, and need to release
3937 * our hold on the name buffer so query_addsoa()
3940 query_releasename(client, &fname);
3943 * Add SOA. If the query was for a SOA record force the
3944 * ttl to zero so that it is possible for clients to find
3945 * the containing zone of an arbitrary name with a stub
3946 * resolver and not have it cached.
3948 if (qtype == dns_rdatatype_soa &&
3952 dns_zone_getzeronosoattl(zone))
3953 result = query_addsoa(client, db, version, ISC_TRUE);
3955 result = query_addsoa(client, db, version, ISC_FALSE);
3956 if (result != ISC_R_SUCCESS) {
3957 QUERY_ERROR(result);
3961 * Add NSEC record if we found one.
3963 if (dns_rdataset_isassociated(rdataset)) {
3964 if (WANTDNSSEC(client)) {
3965 query_addrrset(client, &fname, &rdataset,
3967 NULL, DNS_SECTION_AUTHORITY);
3968 query_addwildcardproof(client, db, version,
3969 client->query.qname,
3974 * Set message rcode.
3977 client->message->rcode = dns_rcode_noerror;
3979 client->message->rcode = dns_rcode_nxdomain;
3981 case DNS_R_NCACHENXDOMAIN:
3982 case DNS_R_NCACHENXRRSET:
3984 authoritative = ISC_FALSE;
3986 * Set message rcode, if required.
3988 if (result == DNS_R_NCACHENXDOMAIN)
3989 client->message->rcode = dns_rcode_nxdomain;
3991 * Look for RFC 1918 leakage from Internet.
3993 if (result == DNS_R_NCACHENXDOMAIN &&
3994 qtype == dns_rdatatype_ptr &&
3995 client->message->rdclass == dns_rdataclass_in &&
3996 dns_name_countlabels(fname) == 7)
3997 warn_rfc1918(client, fname, rdataset);
3999 * We don't call query_addrrset() because we don't need any
4000 * of its extra features (and things would probably break!).
4002 query_keepname(client, fname, dbuf);
4003 dns_message_addname(client->message, fname,
4004 DNS_SECTION_AUTHORITY);
4005 ISC_LIST_APPEND(fname->list, rdataset, link);
4011 * Keep a copy of the rdataset. We have to do this because
4012 * query_addrrset may clear 'rdataset' (to prevent the
4013 * cleanup code from cleaning it up).
4015 trdataset = rdataset;
4017 * Add the CNAME to the answer section.
4019 if (sigrdataset != NULL)
4020 sigrdatasetp = &sigrdataset;
4022 sigrdatasetp = NULL;
4023 if (WANTDNSSEC(client) &&
4024 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4026 dns_fixedname_init(&wildcardname);
4027 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
4029 need_wildcardproof = ISC_TRUE;
4031 if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0 &&
4036 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4037 DNS_SECTION_ANSWER);
4038 if (noqname != NULL)
4039 query_addnoqnameproof(client, noqname);
4041 * We set the PARTIALANSWER attribute so that if anything goes
4042 * wrong later on, we'll return what we've got so far.
4044 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
4046 * Reset qname to be the target name of the CNAME and restart
4050 result = dns_message_gettempname(client->message, &tname);
4051 if (result != ISC_R_SUCCESS)
4053 result = dns_rdataset_first(trdataset);
4054 if (result != ISC_R_SUCCESS) {
4055 dns_message_puttempname(client->message, &tname);
4058 dns_rdataset_current(trdataset, &rdata);
4059 result = dns_rdata_tostruct(&rdata, &cname, NULL);
4060 dns_rdata_reset(&rdata);
4061 if (result != ISC_R_SUCCESS) {
4062 dns_message_puttempname(client->message, &tname);
4065 dns_name_init(tname, NULL);
4066 result = dns_name_dup(&cname.cname, client->mctx, tname);
4067 if (result != ISC_R_SUCCESS) {
4068 dns_message_puttempname(client->message, &tname);
4069 dns_rdata_freestruct(&cname);
4072 dns_rdata_freestruct(&cname);
4073 ns_client_qnamereplace(client, tname);
4074 want_restart = ISC_TRUE;
4075 if (!WANTRECURSION(client))
4076 options |= DNS_GETDB_NOLOG;
4080 * Compare the current qname to the found name. We need
4081 * to know how many labels and bits are in common because
4082 * we're going to have to split qname later on.
4084 namereln = dns_name_fullcompare(client->query.qname, fname,
4086 INSIST(namereln == dns_namereln_subdomain);
4088 * Keep a copy of the rdataset. We have to do this because
4089 * query_addrrset may clear 'rdataset' (to prevent the
4090 * cleanup code from cleaning it up).
4092 trdataset = rdataset;
4094 * Add the DNAME to the answer section.
4096 if (sigrdataset != NULL)
4097 sigrdatasetp = &sigrdataset;
4099 sigrdatasetp = NULL;
4100 if (WANTDNSSEC(client) &&
4101 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4103 dns_fixedname_init(&wildcardname);
4104 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
4106 need_wildcardproof = ISC_TRUE;
4108 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4109 DNS_SECTION_ANSWER);
4111 * We set the PARTIALANSWER attribute so that if anything goes
4112 * wrong later on, we'll return what we've got so far.
4114 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
4116 * Get the target name of the DNAME.
4119 result = dns_message_gettempname(client->message, &tname);
4120 if (result != ISC_R_SUCCESS)
4122 result = dns_rdataset_first(trdataset);
4123 if (result != ISC_R_SUCCESS) {
4124 dns_message_puttempname(client->message, &tname);
4127 dns_rdataset_current(trdataset, &rdata);
4128 result = dns_rdata_tostruct(&rdata, &dname, NULL);
4129 dns_rdata_reset(&rdata);
4130 if (result != ISC_R_SUCCESS) {
4131 dns_message_puttempname(client->message, &tname);
4134 dns_name_init(tname, NULL);
4135 dns_name_clone(&dname.dname, tname);
4136 dns_rdata_freestruct(&dname);
4138 * Construct the new qname.
4140 dns_fixedname_init(&fixed);
4141 prefix = dns_fixedname_name(&fixed);
4142 dns_name_split(client->query.qname, nlabels, prefix, NULL);
4143 INSIST(fname == NULL);
4144 dbuf = query_getnamebuf(client);
4146 dns_message_puttempname(client->message, &tname);
4149 fname = query_newname(client, dbuf, &b);
4150 if (fname == NULL) {
4151 dns_message_puttempname(client->message, &tname);
4154 result = dns_name_concatenate(prefix, tname, fname, NULL);
4155 if (result != ISC_R_SUCCESS) {
4156 dns_message_puttempname(client->message, &tname);
4157 if (result == ISC_R_NOSPACE) {
4159 * RFC2672, section 4.1, subsection 3c says
4160 * we should return YXDOMAIN if the constructed
4161 * name would be too long.
4163 client->message->rcode = dns_rcode_yxdomain;
4167 query_keepname(client, fname, dbuf);
4169 * Synthesize a CNAME for this DNAME.
4171 * We want to synthesize a CNAME since if we don't
4172 * then older software that doesn't understand DNAME
4173 * will not chain like it should.
4175 * We do not try to synthesize a signature because we hope
4176 * that security aware servers will understand DNAME. Also,
4177 * even if we had an online key, making a signature
4178 * on-the-fly is costly, and not really legitimate anyway
4179 * since the synthesized CNAME is NOT in the zone.
4181 dns_name_init(tname, NULL);
4182 (void)query_addcnamelike(client, client->query.qname, fname,
4183 trdataset->trust, &tname,
4184 dns_rdatatype_cname);
4186 dns_message_puttempname(client->message, &tname);
4188 * Switch to the new qname and restart.
4190 ns_client_qnamereplace(client, fname);
4192 want_restart = ISC_TRUE;
4193 if (!WANTRECURSION(client))
4194 options |= DNS_GETDB_NOLOG;
4198 * Something has gone wrong.
4200 QUERY_ERROR(DNS_R_SERVFAIL);
4204 if (WANTDNSSEC(client) &&
4205 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4207 dns_fixedname_init(&wildcardname);
4208 dns_name_copy(fname, dns_fixedname_name(&wildcardname), NULL);
4209 need_wildcardproof = ISC_TRUE;
4212 if (type == dns_rdatatype_any) {
4214 * XXXRTH Need to handle zonecuts with special case
4219 result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
4220 if (result != ISC_R_SUCCESS) {
4221 QUERY_ERROR(DNS_R_SERVFAIL);
4225 * Calling query_addrrset() with a non-NULL dbuf is going
4226 * to either keep or release the name. We don't want it to
4227 * release fname, since we may have to call query_addrrset()
4228 * more than once. That means we have to call query_keepname()
4229 * now, and pass a NULL dbuf to query_addrrset().
4231 * If we do a query_addrrset() below, we must set fname to
4232 * NULL before leaving this block, otherwise we might try to
4233 * cleanup fname even though we're using it!
4235 query_keepname(client, fname, dbuf);
4237 result = dns_rdatasetiter_first(rdsiter);
4238 while (result == ISC_R_SUCCESS) {
4239 dns_rdatasetiter_current(rdsiter, rdataset);
4240 if ((qtype == dns_rdatatype_any ||
4241 rdataset->type == qtype) && rdataset->type != 0) {
4242 query_addrrset(client,
4243 fname != NULL ? &fname : &tname,
4245 NULL, DNS_SECTION_ANSWER);
4247 INSIST(tname != NULL);
4249 * rdataset is non-NULL only in certain pathological
4250 * cases involving DNAMEs.
4252 if (rdataset != NULL)
4253 query_putrdataset(client, &rdataset);
4254 rdataset = query_newrdataset(client);
4255 if (rdataset == NULL)
4259 * We're not interested in this rdataset.
4261 dns_rdataset_disassociate(rdataset);
4263 result = dns_rdatasetiter_next(rdsiter);
4267 dns_message_puttempname(client->message, &fname);
4271 * We didn't match any rdatasets.
4273 if (qtype == dns_rdatatype_rrsig &&
4274 result == ISC_R_NOMORE) {
4276 * XXXRTH If this is a secure zone and we
4277 * didn't find any SIGs, we should generate
4278 * an error unless we were searching for
4282 authoritative = ISC_FALSE;
4283 dns_rdatasetiter_destroy(&rdsiter);
4284 if (RECURSIONOK(client)) {
4285 result = query_recurse(client,
4290 if (result == ISC_R_SUCCESS)
4291 client->query.attributes |=
4292 NS_QUERYATTR_RECURSING;
4294 QUERY_ERROR(DNS_R_SERVFAIL); }
4298 * We were searching for SIG records in
4299 * a nonsecure zone. Send a "no error,
4300 * no data" response.
4305 result = query_addsoa(client, db, version,
4307 if (result == ISC_R_SUCCESS)
4308 result = ISC_R_NOMORE;
4311 * Something went wrong.
4313 result = DNS_R_SERVFAIL;
4316 dns_rdatasetiter_destroy(&rdsiter);
4317 if (result != ISC_R_NOMORE) {
4318 QUERY_ERROR(DNS_R_SERVFAIL);
4323 * This is the "normal" case -- an ordinary question to which
4324 * we know the answer.
4326 if (sigrdataset != NULL)
4327 sigrdatasetp = &sigrdataset;
4329 sigrdatasetp = NULL;
4330 if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0 &&
4336 * BIND 8 priming queries need the additional section.
4338 if (is_zone && qtype == dns_rdatatype_ns &&
4339 dns_name_equal(client->query.qname, dns_rootname))
4340 client->query.attributes &= ~NS_QUERYATTR_NOADDITIONAL;
4342 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4343 DNS_SECTION_ANSWER);
4344 if (noqname != NULL)
4345 query_addnoqnameproof(client, noqname);
4347 * We shouldn't ever fail to add 'rdataset'
4348 * because it's already in the answer.
4350 INSIST(rdataset == NULL);
4354 CTRACE("query_find: addauth");
4356 * Add NS records to the authority section (if we haven't already
4357 * added them to the answer section).
4359 if (!want_restart && !NOAUTHORITY(client)) {
4361 if (!((qtype == dns_rdatatype_ns ||
4362 qtype == dns_rdatatype_any) &&
4363 dns_name_equal(client->query.qname,
4364 dns_db_origin(db))))
4365 (void)query_addns(client, db, version);
4366 } else if (qtype != dns_rdatatype_ns) {
4368 query_releasename(client, &fname);
4369 query_addbestns(client);
4374 * Add NSEC records to the authority section if they're needed for
4375 * DNSSEC wildcard proofs.
4377 if (need_wildcardproof && dns_db_issecure(db))
4378 query_addwildcardproof(client, db, version,
4379 dns_fixedname_name(&wildcardname),
4382 CTRACE("query_find: cleanup");
4386 if (rdataset != NULL)
4387 query_putrdataset(client, &rdataset);
4388 if (sigrdataset != NULL)
4389 query_putrdataset(client, &sigrdataset);
4391 query_releasename(client, &fname);
4393 dns_db_detachnode(db, &node);
4397 dns_zone_detach(&zone);
4399 query_putrdataset(client, &zrdataset);
4400 if (zsigrdataset != NULL)
4401 query_putrdataset(client, &zsigrdataset);
4403 query_releasename(client, &zfname);
4404 dns_db_detach(&zdb);
4407 isc_event_free(ISC_EVENT_PTR(&event));
4412 if (client->query.restarts == 0 && !authoritative) {
4414 * We're not authoritative, so we must ensure the AA bit
4417 client->message->flags &= ~DNS_MESSAGEFLAG_AA;
4421 * Restart the query?
4423 if (want_restart && client->query.restarts < MAX_RESTARTS) {
4424 client->query.restarts++;
4428 if (eresult != ISC_R_SUCCESS &&
4429 (!PARTIALANSWER(client) || WANTRECURSION(client))) {
4430 if (eresult == DNS_R_DUPLICATE || eresult == DNS_R_DROP) {
4432 * This was a duplicate query that we are
4433 * recursing on. Don't send a response now.
4434 * The original query will still cause a response.
4436 query_next(client, eresult);
4439 * If we don't have any answer to give the client,
4440 * or if the client requested recursion and thus wanted
4441 * the complete answer, send an error response.
4444 query_error(client, eresult, line);
4446 ns_client_detach(&client);
4447 } else if (!RECURSING(client)) {
4449 * We are done. Set up sortlist data for the message
4450 * rendering code, make a final tweak to the AA bit if the
4451 * auth-nxdomain config option says so, then render and
4452 * send the response.
4454 setup_query_sortlist(client);
4457 * If this is a referral and the answer to the question
4458 * is in the glue sort it to the start of the additional
4461 if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER]) &&
4462 client->message->rcode == dns_rcode_noerror &&
4463 (qtype == dns_rdatatype_a || qtype == dns_rdatatype_aaaa))
4464 answer_in_glue(client, qtype);
4466 if (client->message->rcode == dns_rcode_nxdomain &&
4467 client->view->auth_nxdomain == ISC_TRUE)
4468 client->message->flags |= DNS_MESSAGEFLAG_AA;
4471 * If the response is somehow unexpected for the client and this
4472 * is a result of recursion, return an error to the caller
4473 * to indicate it may need to be logged.
4476 (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER]) ||
4477 client->message->rcode != dns_rcode_noerror))
4478 eresult = ISC_R_FAILURE;
4481 ns_client_detach(&client);
4483 CTRACE("query_find: done");
4489 log_query(ns_client_t *client, unsigned int flags, unsigned int extflags) {
4490 char namebuf[DNS_NAME_FORMATSIZE];
4491 char typename[DNS_RDATATYPE_FORMATSIZE];
4492 char classname[DNS_RDATACLASS_FORMATSIZE];
4493 dns_rdataset_t *rdataset;
4494 int level = ISC_LOG_INFO;
4496 if (! isc_log_wouldlog(ns_g_lctx, level))
4499 rdataset = ISC_LIST_HEAD(client->query.qname->list);
4500 INSIST(rdataset != NULL);
4501 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
4502 dns_rdataclass_format(rdataset->rdclass, classname, sizeof(classname));
4503 dns_rdatatype_format(rdataset->type, typename, sizeof(typename));
4505 ns_client_log(client, NS_LOGCATEGORY_QUERIES, NS_LOGMODULE_QUERY,
4506 level, "query: %s %s %s %s%s%s%s%s", namebuf, classname,
4507 typename, WANTRECURSION(client) ? "+" : "-",
4508 (client->signer != NULL) ? "S": "",
4509 (client->opt != NULL) ? "E" : "",
4510 ((extflags & DNS_MESSAGEEXTFLAG_DO) != 0) ? "D" : "",
4511 ((flags & DNS_MESSAGEFLAG_CD) != 0) ? "C" : "");
4515 log_queryerror(ns_client_t *client, isc_result_t result, int line, int level) {
4516 char namebuf[DNS_NAME_FORMATSIZE];
4517 char typename[DNS_RDATATYPE_FORMATSIZE];
4518 char classname[DNS_RDATACLASS_FORMATSIZE];
4519 const char *namep, *typep, *classp, *sep1, *sep2;
4520 dns_rdataset_t *rdataset;
4522 if (!isc_log_wouldlog(ns_g_lctx, level))
4525 namep = typep = classp = sep1 = sep2 = "";
4528 * Query errors can happen for various reasons. In some cases we cannot
4529 * even assume the query contains a valid question section, so we should
4530 * expect exceptional cases.
4532 if (client->query.origqname != NULL) {
4533 dns_name_format(client->query.origqname, namebuf,
4538 rdataset = ISC_LIST_HEAD(client->query.origqname->list);
4539 if (rdataset != NULL) {
4540 dns_rdataclass_format(rdataset->rdclass, classname,
4543 dns_rdatatype_format(rdataset->type, typename,
4550 ns_client_log(client, NS_LOGCATEGORY_QUERY_EERRORS, NS_LOGMODULE_QUERY,
4551 level, "query failed (%s)%s%s%s%s%s%s at %s:%d",
4552 isc_result_totext(result), sep1, namep, sep2,
4553 classp, sep2, typep, __FILE__, line);
4557 ns_query_start(ns_client_t *client) {
4558 isc_result_t result;
4559 dns_message_t *message = client->message;
4560 dns_rdataset_t *rdataset;
4561 ns_client_t *qclient;
4562 dns_rdatatype_t qtype;
4563 unsigned int saved_extflags = client->extflags;
4564 unsigned int saved_flags = client->message->flags;
4565 isc_boolean_t want_ad;
4567 CTRACE("ns_query_start");
4570 * Ensure that appropriate cleanups occur.
4572 client->next = query_next_callback;
4575 * Behave as if we don't support DNSSEC if not enabled.
4577 if (!client->view->enablednssec) {
4578 message->flags &= ~DNS_MESSAGEFLAG_CD;
4579 client->extflags &= ~DNS_MESSAGEEXTFLAG_DO;
4580 if (client->opt != NULL)
4581 client->opt->ttl &= ~DNS_MESSAGEEXTFLAG_DO;
4584 if ((message->flags & DNS_MESSAGEFLAG_RD) != 0)
4585 client->query.attributes |= NS_QUERYATTR_WANTRECURSION;
4587 if ((client->extflags & DNS_MESSAGEEXTFLAG_DO) != 0)
4588 client->attributes |= NS_CLIENTATTR_WANTDNSSEC;
4590 if (client->view->minimalresponses)
4591 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
4592 NS_QUERYATTR_NOADDITIONAL);
4594 if ((client->view->cachedb == NULL)
4595 || (!client->view->additionalfromcache)) {
4597 * We don't have a cache. Turn off cache support and
4600 client->query.attributes &=
4601 ~(NS_QUERYATTR_RECURSIONOK|NS_QUERYATTR_CACHEOK);
4602 } else if ((client->attributes & NS_CLIENTATTR_RA) == 0 ||
4603 (message->flags & DNS_MESSAGEFLAG_RD) == 0) {
4605 * If the client isn't allowed to recurse (due to
4606 * "recursion no", the allow-recursion ACL, or the
4607 * lack of a resolver in this view), or if it
4608 * doesn't want recursion, turn recursion off.
4610 client->query.attributes &= ~NS_QUERYATTR_RECURSIONOK;
4614 * Get the question name.
4616 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
4617 if (result != ISC_R_SUCCESS) {
4618 query_error(client, result, __LINE__);
4621 dns_message_currentname(message, DNS_SECTION_QUESTION,
4622 &client->query.qname);
4623 client->query.origqname = client->query.qname;
4624 result = dns_message_nextname(message, DNS_SECTION_QUESTION);
4625 if (result != ISC_R_NOMORE) {
4626 if (result == ISC_R_SUCCESS) {
4628 * There's more than one QNAME in the question
4631 query_error(client, DNS_R_FORMERR, __LINE__);
4633 query_error(client, result, __LINE__);
4637 if (ns_g_server->log_queries)
4638 log_query(client, saved_flags, saved_extflags);
4641 * Check for multiple question queries, since edns1 is dead.
4643 if (message->counts[DNS_SECTION_QUESTION] > 1) {
4644 query_error(client, DNS_R_FORMERR, __LINE__);
4649 * Check for meta-queries like IXFR and AXFR.
4651 rdataset = ISC_LIST_HEAD(client->query.qname->list);
4652 INSIST(rdataset != NULL);
4653 qtype = rdataset->type;
4654 dns_rdatatypestats_increment(ns_g_server->rcvquerystats, qtype);
4655 if (dns_rdatatype_ismeta(qtype)) {
4657 case dns_rdatatype_any:
4658 break; /* Let query_find handle it. */
4659 case dns_rdatatype_ixfr:
4660 case dns_rdatatype_axfr:
4661 ns_xfr_start(client, rdataset->type);
4663 case dns_rdatatype_maila:
4664 case dns_rdatatype_mailb:
4665 query_error(client, DNS_R_NOTIMP, __LINE__);
4667 case dns_rdatatype_tkey:
4668 result = dns_tkey_processquery(client->message,
4669 ns_g_server->tkeyctx,
4670 client->view->dynamickeys);
4671 if (result == ISC_R_SUCCESS)
4674 query_error(client, result, __LINE__);
4676 default: /* TSIG, etc. */
4677 query_error(client, DNS_R_FORMERR, __LINE__);
4683 * Turn on minimal response for DNSKEY and DS queries.
4685 if (qtype == dns_rdatatype_dnskey || qtype == dns_rdatatype_ds)
4686 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
4687 NS_QUERYATTR_NOADDITIONAL);
4690 * If the client has requested that DNSSEC checking be disabled,
4691 * allow lookups to return pending data and instruct the resolver
4692 * to return data before validation has completed.
4694 * We don't need to set DNS_DBFIND_PENDINGOK when validation is
4695 * disabled as there will be no pending data.
4697 if (message->flags & DNS_MESSAGEFLAG_CD ||
4698 qtype == dns_rdatatype_rrsig)
4700 client->query.dboptions |= DNS_DBFIND_PENDINGOK;
4701 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
4702 } else if (!client->view->enablevalidation)
4703 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
4706 * Allow glue NS records to be added to the authority section
4707 * if the answer is secure.
4709 if (message->flags & DNS_MESSAGEFLAG_CD)
4710 client->query.attributes &= ~NS_QUERYATTR_SECURE;
4713 * Set 'want_ad' if the client has set AD in the query.
4714 * This allows AD to be returned on queries without DO set.
4716 if ((message->flags & DNS_MESSAGEFLAG_AD) != 0)
4719 want_ad = ISC_FALSE;
4722 * This is an ordinary query.
4724 result = dns_message_reply(message, ISC_TRUE);
4725 if (result != ISC_R_SUCCESS) {
4726 query_next(client, result);
4731 * Assume authoritative response until it is known to be
4734 message->flags |= DNS_MESSAGEFLAG_AA;
4737 * Set AD. We must clear it if we add non-validated data to a
4740 if (WANTDNSSEC(client) || want_ad)
4741 message->flags |= DNS_MESSAGEFLAG_AD;
4744 ns_client_attach(client, &qclient);
4745 (void)query_find(qclient, NULL, qtype);