2 * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: query.c,v 1.257.18.46 2008/10/15 22:33:01 marka Exp $ */
30 #include <dns/byaddr.h>
35 #include <dns/dnssec.h>
36 #include <dns/events.h>
37 #include <dns/message.h>
38 #include <dns/ncache.h>
39 #include <dns/order.h>
40 #include <dns/rdata.h>
41 #include <dns/rdataclass.h>
42 #include <dns/rdatalist.h>
43 #include <dns/rdataset.h>
44 #include <dns/rdatasetiter.h>
45 #include <dns/rdatastruct.h>
46 #include <dns/rdatatype.h>
47 #include <dns/resolver.h>
48 #include <dns/result.h>
49 #include <dns/stats.h>
55 #include <named/client.h>
56 #include <named/log.h>
57 #include <named/server.h>
58 #include <named/sortlist.h>
59 #include <named/xfrout.h>
61 /*% Partial answer? */
62 #define PARTIALANSWER(c) (((c)->query.attributes & \
63 NS_QUERYATTR_PARTIALANSWER) != 0)
65 #define USECACHE(c) (((c)->query.attributes & \
66 NS_QUERYATTR_CACHEOK) != 0)
68 #define RECURSIONOK(c) (((c)->query.attributes & \
69 NS_QUERYATTR_RECURSIONOK) != 0)
71 #define RECURSING(c) (((c)->query.attributes & \
72 NS_QUERYATTR_RECURSING) != 0)
74 #define CACHEGLUEOK(c) (((c)->query.attributes & \
75 NS_QUERYATTR_CACHEGLUEOK) != 0)
76 /*% Want Recursion? */
77 #define WANTRECURSION(c) (((c)->query.attributes & \
78 NS_QUERYATTR_WANTRECURSION) != 0)
80 #define WANTDNSSEC(c) (((c)->attributes & \
81 NS_CLIENTATTR_WANTDNSSEC) != 0)
83 #define NOAUTHORITY(c) (((c)->query.attributes & \
84 NS_QUERYATTR_NOAUTHORITY) != 0)
86 #define NOADDITIONAL(c) (((c)->query.attributes & \
87 NS_QUERYATTR_NOADDITIONAL) != 0)
89 #define SECURE(c) (((c)->query.attributes & \
90 NS_QUERYATTR_SECURE) != 0)
93 #define CTRACE(m) isc_log_write(ns_g_lctx, \
94 NS_LOGCATEGORY_CLIENT, \
97 "client %p: %s", client, (m))
98 #define QTRACE(m) isc_log_write(ns_g_lctx, \
99 NS_LOGCATEGORY_GENERAL, \
100 NS_LOGMODULE_QUERY, \
102 "query %p: %s", query, (m))
104 #define CTRACE(m) ((void)m)
105 #define QTRACE(m) ((void)m)
108 #define DNS_GETDB_NOEXACT 0x01U
109 #define DNS_GETDB_NOLOG 0x02U
110 #define DNS_GETDB_PARTIAL 0x04U
112 typedef struct client_additionalctx {
114 dns_rdataset_t *rdataset;
115 } client_additionalctx_t;
118 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype);
121 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
122 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
125 * Increment query statistics counters.
128 inc_stats(ns_client_t *client, dns_statscounter_t counter) {
129 dns_zone_t *zone = client->query.authzone;
131 REQUIRE(counter < DNS_STATS_NCOUNTERS);
133 ns_g_server->querystats[counter]++;
136 isc_uint64_t *zonestats = dns_zone_getstatscounters(zone);
137 if (zonestats != NULL)
138 zonestats[counter]++;
143 query_send(ns_client_t *client) {
144 dns_statscounter_t counter;
145 if (client->message->rcode == dns_rcode_noerror) {
146 if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER])) {
147 if (client->query.isreferral) {
148 counter = dns_statscounter_referral;
150 counter = dns_statscounter_nxrrset;
153 counter = dns_statscounter_success;
155 } else if (client->message->rcode == dns_rcode_nxdomain) {
156 counter = dns_statscounter_nxdomain;
158 /* We end up here in case of YXDOMAIN, and maybe others */
159 counter = dns_statscounter_failure;
161 inc_stats(client, counter);
162 ns_client_send(client);
166 query_error(ns_client_t *client, isc_result_t result) {
167 inc_stats(client, dns_statscounter_failure);
168 ns_client_error(client, result);
172 query_next(ns_client_t *client, isc_result_t result) {
173 if (result == DNS_R_DUPLICATE)
174 inc_stats(client, dns_statscounter_duplicate);
175 else if (result == DNS_R_DROP)
176 inc_stats(client, dns_statscounter_dropped);
178 inc_stats(client, dns_statscounter_failure);
179 ns_client_next(client, result);
183 query_freefreeversions(ns_client_t *client, isc_boolean_t everything) {
184 ns_dbversion_t *dbversion, *dbversion_next;
187 for (dbversion = ISC_LIST_HEAD(client->query.freeversions), i = 0;
189 dbversion = dbversion_next, i++)
191 dbversion_next = ISC_LIST_NEXT(dbversion, link);
193 * If we're not freeing everything, we keep the first three
194 * dbversions structures around.
196 if (i > 3 || everything) {
197 ISC_LIST_UNLINK(client->query.freeversions, dbversion,
199 isc_mem_put(client->mctx, dbversion,
206 ns_query_cancel(ns_client_t *client) {
207 LOCK(&client->query.fetchlock);
208 if (client->query.fetch != NULL) {
209 dns_resolver_cancelfetch(client->query.fetch);
211 client->query.fetch = NULL;
213 UNLOCK(&client->query.fetchlock);
217 query_reset(ns_client_t *client, isc_boolean_t everything) {
218 isc_buffer_t *dbuf, *dbuf_next;
219 ns_dbversion_t *dbversion, *dbversion_next;
222 * Reset the query state of a client to its default state.
226 * Cancel the fetch if it's running.
228 ns_query_cancel(client);
231 * Cleanup any active versions.
233 for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
235 dbversion = dbversion_next) {
236 dbversion_next = ISC_LIST_NEXT(dbversion, link);
237 dns_db_closeversion(dbversion->db, &dbversion->version,
239 dns_db_detach(&dbversion->db);
240 ISC_LIST_INITANDAPPEND(client->query.freeversions,
243 ISC_LIST_INIT(client->query.activeversions);
245 if (client->query.authdb != NULL)
246 dns_db_detach(&client->query.authdb);
247 if (client->query.authzone != NULL)
248 dns_zone_detach(&client->query.authzone);
250 query_freefreeversions(client, everything);
252 for (dbuf = ISC_LIST_HEAD(client->query.namebufs);
255 dbuf_next = ISC_LIST_NEXT(dbuf, link);
256 if (dbuf_next != NULL || everything) {
257 ISC_LIST_UNLINK(client->query.namebufs, dbuf, link);
258 isc_buffer_free(&dbuf);
262 if (client->query.restarts > 0) {
264 * client->query.qname was dynamically allocated.
266 dns_message_puttempname(client->message,
267 &client->query.qname);
269 client->query.qname = NULL;
270 client->query.attributes = (NS_QUERYATTR_RECURSIONOK |
271 NS_QUERYATTR_CACHEOK |
272 NS_QUERYATTR_SECURE);
273 client->query.restarts = 0;
274 client->query.timerset = ISC_FALSE;
275 client->query.origqname = NULL;
276 client->query.qname = NULL;
277 client->query.dboptions = 0;
278 client->query.fetchoptions = 0;
279 client->query.gluedb = NULL;
280 client->query.authdbset = ISC_FALSE;
281 client->query.isreferral = ISC_FALSE;
285 query_next_callback(ns_client_t *client) {
286 query_reset(client, ISC_FALSE);
290 ns_query_free(ns_client_t *client) {
291 query_reset(client, ISC_TRUE);
294 static inline isc_result_t
295 query_newnamebuf(ns_client_t *client) {
299 CTRACE("query_newnamebuf");
301 * Allocate a name buffer.
305 result = isc_buffer_allocate(client->mctx, &dbuf, 1024);
306 if (result != ISC_R_SUCCESS) {
307 CTRACE("query_newnamebuf: isc_buffer_allocate failed: done");
310 ISC_LIST_APPEND(client->query.namebufs, dbuf, link);
312 CTRACE("query_newnamebuf: done");
313 return (ISC_R_SUCCESS);
316 static inline isc_buffer_t *
317 query_getnamebuf(ns_client_t *client) {
322 CTRACE("query_getnamebuf");
324 * Return a name buffer with space for a maximal name, allocating
325 * a new one if necessary.
328 if (ISC_LIST_EMPTY(client->query.namebufs)) {
329 result = query_newnamebuf(client);
330 if (result != ISC_R_SUCCESS) {
331 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
336 dbuf = ISC_LIST_TAIL(client->query.namebufs);
337 INSIST(dbuf != NULL);
338 isc_buffer_availableregion(dbuf, &r);
339 if (r.length < 255) {
340 result = query_newnamebuf(client);
341 if (result != ISC_R_SUCCESS) {
342 CTRACE("query_getnamebuf: query_newnamebuf failed: done");
346 dbuf = ISC_LIST_TAIL(client->query.namebufs);
347 isc_buffer_availableregion(dbuf, &r);
348 INSIST(r.length >= 255);
350 CTRACE("query_getnamebuf: done");
355 query_keepname(ns_client_t *client, dns_name_t *name, isc_buffer_t *dbuf) {
358 CTRACE("query_keepname");
360 * 'name' is using space in 'dbuf', but 'dbuf' has not yet been
361 * adjusted to take account of that. We do the adjustment.
364 REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) != 0);
366 dns_name_toregion(name, &r);
367 isc_buffer_add(dbuf, r.length);
368 dns_name_setbuffer(name, NULL);
369 client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
373 query_releasename(ns_client_t *client, dns_name_t **namep) {
374 dns_name_t *name = *namep;
377 * 'name' is no longer needed. Return it to our pool of temporary
378 * names. If it is using a name buffer, relinquish its exclusive
379 * rights on the buffer.
382 CTRACE("query_releasename");
383 if (dns_name_hasbuffer(name)) {
384 INSIST((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED)
386 client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
388 dns_message_puttempname(client->message, namep);
389 CTRACE("query_releasename: done");
392 static inline dns_name_t *
393 query_newname(ns_client_t *client, isc_buffer_t *dbuf,
400 REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) == 0);
402 CTRACE("query_newname");
404 result = dns_message_gettempname(client->message, &name);
405 if (result != ISC_R_SUCCESS) {
406 CTRACE("query_newname: dns_message_gettempname failed: done");
409 isc_buffer_availableregion(dbuf, &r);
410 isc_buffer_init(nbuf, r.base, r.length);
411 dns_name_init(name, NULL);
412 dns_name_setbuffer(name, nbuf);
413 client->query.attributes |= NS_QUERYATTR_NAMEBUFUSED;
415 CTRACE("query_newname: done");
419 static inline dns_rdataset_t *
420 query_newrdataset(ns_client_t *client) {
421 dns_rdataset_t *rdataset;
424 CTRACE("query_newrdataset");
426 result = dns_message_gettemprdataset(client->message, &rdataset);
427 if (result != ISC_R_SUCCESS) {
428 CTRACE("query_newrdataset: "
429 "dns_message_gettemprdataset failed: done");
432 dns_rdataset_init(rdataset);
434 CTRACE("query_newrdataset: done");
439 query_putrdataset(ns_client_t *client, dns_rdataset_t **rdatasetp) {
440 dns_rdataset_t *rdataset = *rdatasetp;
442 CTRACE("query_putrdataset");
443 if (rdataset != NULL) {
444 if (dns_rdataset_isassociated(rdataset))
445 dns_rdataset_disassociate(rdataset);
446 dns_message_puttemprdataset(client->message, rdatasetp);
448 CTRACE("query_putrdataset: done");
452 static inline isc_result_t
453 query_newdbversion(ns_client_t *client, unsigned int n) {
455 ns_dbversion_t *dbversion;
457 for (i = 0; i < n; i++) {
458 dbversion = isc_mem_get(client->mctx, sizeof(*dbversion));
459 if (dbversion != NULL) {
460 dbversion->db = NULL;
461 dbversion->version = NULL;
462 ISC_LIST_INITANDAPPEND(client->query.freeversions,
466 * We only return ISC_R_NOMEMORY if we couldn't
470 return (ISC_R_NOMEMORY);
472 return (ISC_R_SUCCESS);
476 return (ISC_R_SUCCESS);
479 static inline ns_dbversion_t *
480 query_getdbversion(ns_client_t *client) {
482 ns_dbversion_t *dbversion;
484 if (ISC_LIST_EMPTY(client->query.freeversions)) {
485 result = query_newdbversion(client, 1);
486 if (result != ISC_R_SUCCESS)
489 dbversion = ISC_LIST_HEAD(client->query.freeversions);
490 INSIST(dbversion != NULL);
491 ISC_LIST_UNLINK(client->query.freeversions, dbversion, link);
497 ns_query_init(ns_client_t *client) {
500 ISC_LIST_INIT(client->query.namebufs);
501 ISC_LIST_INIT(client->query.activeversions);
502 ISC_LIST_INIT(client->query.freeversions);
503 client->query.restarts = 0;
504 client->query.timerset = ISC_FALSE;
505 client->query.qname = NULL;
506 result = isc_mutex_init(&client->query.fetchlock);
507 if (result != ISC_R_SUCCESS)
509 client->query.fetch = NULL;
510 client->query.authdb = NULL;
511 client->query.authzone = NULL;
512 client->query.authdbset = ISC_FALSE;
513 client->query.isreferral = ISC_FALSE;
514 query_reset(client, ISC_FALSE);
515 result = query_newdbversion(client, 3);
516 if (result != ISC_R_SUCCESS) {
517 DESTROYLOCK(&client->query.fetchlock);
520 result = query_newnamebuf(client);
521 if (result != ISC_R_SUCCESS)
522 query_freefreeversions(client, ISC_TRUE);
527 static inline ns_dbversion_t *
528 query_findversion(ns_client_t *client, dns_db_t *db,
529 isc_boolean_t *newzonep)
531 ns_dbversion_t *dbversion;
534 * We may already have done a query related to this
535 * database. If so, we must be sure to make subsequent
536 * queries from the same version.
538 for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
540 dbversion = ISC_LIST_NEXT(dbversion, link)) {
541 if (dbversion->db == db)
545 if (dbversion == NULL) {
547 * This is a new zone for this query. Add it to
550 dbversion = query_getdbversion(client);
551 if (dbversion == NULL)
553 dns_db_attach(db, &dbversion->db);
554 dns_db_currentversion(db, &dbversion->version);
555 dbversion->queryok = ISC_FALSE;
556 ISC_LIST_APPEND(client->query.activeversions,
558 *newzonep = ISC_TRUE;
560 *newzonep = ISC_FALSE;
565 static inline isc_result_t
566 query_validatezonedb(ns_client_t *client, dns_name_t *name,
567 dns_rdatatype_t qtype, unsigned int options,
568 dns_zone_t *zone, dns_db_t *db,
569 dns_dbversion_t **versionp)
572 isc_boolean_t check_acl, new_zone;
574 ns_dbversion_t *dbversion;
576 REQUIRE(zone != NULL);
580 * This limits our searching to the zone where the first name
581 * (the query target) was looked for. This prevents following
582 * CNAMES or DNAMES into other zones and prevents returning
583 * additional data from other zones.
585 if (!client->view->additionalfromauth &&
586 client->query.authdbset &&
587 db != client->query.authdb)
591 * If the zone has an ACL, we'll check it, otherwise
592 * we use the view's "allow-query" ACL. Each ACL is only checked
595 * Also, get the database version to use.
598 check_acl = ISC_TRUE; /* Keep compiler happy. */
602 * Get the current version of this database.
604 dbversion = query_findversion(client, db, &new_zone);
605 if (dbversion == NULL) {
606 result = DNS_R_SERVFAIL;
610 check_acl = ISC_TRUE;
611 } else if (!dbversion->queryok) {
614 check_acl = ISC_FALSE;
617 queryacl = dns_zone_getqueryacl(zone);
618 if (queryacl == NULL) {
619 queryacl = client->view->queryacl;
620 if ((client->query.attributes &
621 NS_QUERYATTR_QUERYOKVALID) != 0) {
623 * We've evaluated the view's queryacl already. If
624 * NS_QUERYATTR_QUERYOK is set, then the client is
625 * allowed to make queries, otherwise the query should
628 check_acl = ISC_FALSE;
629 if ((client->query.attributes &
630 NS_QUERYATTR_QUERYOK) == 0)
634 * We haven't evaluated the view's queryacl yet.
636 check_acl = ISC_TRUE;
641 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
643 result = ns_client_checkaclsilent(client, queryacl, ISC_TRUE);
645 char msg[NS_CLIENT_ACLMSGSIZE("query")];
646 if (result == ISC_R_SUCCESS) {
647 if (isc_log_wouldlog(ns_g_lctx,
650 ns_client_aclmsg("query", name, qtype,
651 client->view->rdclass,
653 ns_client_log(client,
654 DNS_LOGCATEGORY_SECURITY,
660 ns_client_aclmsg("query", name, qtype,
661 client->view->rdclass,
663 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
664 NS_LOGMODULE_QUERY, ISC_LOG_INFO,
669 if (queryacl == client->view->queryacl) {
670 if (result == ISC_R_SUCCESS) {
672 * We were allowed by the default
673 * "allow-query" ACL. Remember this so we
674 * don't have to check again.
676 client->query.attributes |=
677 NS_QUERYATTR_QUERYOK;
680 * We've now evaluated the view's query ACL, and
681 * the NS_QUERYATTR_QUERYOK attribute is now valid.
683 client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
686 if (result != ISC_R_SUCCESS)
693 * Remember the result of the ACL check so we
694 * don't have to check again.
696 dbversion->queryok = ISC_TRUE;
698 /* Transfer ownership, if necessary. */
699 if (versionp != NULL)
700 *versionp = dbversion->version;
702 return (ISC_R_SUCCESS);
705 return (DNS_R_REFUSED);
711 static inline isc_result_t
712 query_getzonedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
713 unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
714 dns_dbversion_t **versionp)
717 unsigned int ztoptions;
718 dns_zone_t *zone = NULL;
720 isc_boolean_t partial = ISC_FALSE;
722 REQUIRE(zonep != NULL && *zonep == NULL);
723 REQUIRE(dbp != NULL && *dbp == NULL);
726 * Find a zone database to answer the query.
728 ztoptions = ((options & DNS_GETDB_NOEXACT) != 0) ?
729 DNS_ZTFIND_NOEXACT : 0;
731 result = dns_zt_find(client->view->zonetable, name, ztoptions, NULL,
733 if (result == DNS_R_PARTIALMATCH)
735 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
736 result = dns_zone_getdb(zone, &db);
738 if (result != ISC_R_SUCCESS)
741 result = query_validatezonedb(client, name, qtype, options, zone, db,
744 if (result != ISC_R_SUCCESS)
747 /* Transfer ownership. */
751 if (partial && (options & DNS_GETDB_PARTIAL) != 0)
752 return (DNS_R_PARTIALMATCH);
753 return (ISC_R_SUCCESS);
757 dns_zone_detach(&zone);
764 static inline isc_result_t
765 query_getcachedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
766 dns_db_t **dbp, unsigned int options)
769 isc_boolean_t check_acl;
772 REQUIRE(dbp != NULL && *dbp == NULL);
775 * Find a cache database to answer the query.
776 * This may fail with DNS_R_REFUSED if the client
777 * is not allowed to use the cache.
780 if (!USECACHE(client))
781 return (DNS_R_REFUSED);
782 dns_db_attach(client->view->cachedb, &db);
784 if ((client->query.attributes &
785 NS_QUERYATTR_QUERYOKVALID) != 0) {
787 * We've evaluated the view's queryacl already. If
788 * NS_QUERYATTR_QUERYOK is set, then the client is
789 * allowed to make queries, otherwise the query should
792 check_acl = ISC_FALSE;
793 if ((client->query.attributes &
794 NS_QUERYATTR_QUERYOK) == 0)
798 * We haven't evaluated the view's queryacl yet.
800 check_acl = ISC_TRUE;
804 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
805 char msg[NS_CLIENT_ACLMSGSIZE("query (cache)")];
807 result = ns_client_checkaclsilent(client,
808 client->view->queryacl,
810 if (result == ISC_R_SUCCESS) {
812 * We were allowed by the default
813 * "allow-query" ACL. Remember this so we
814 * don't have to check again.
816 client->query.attributes |=
817 NS_QUERYATTR_QUERYOK;
818 if (log && isc_log_wouldlog(ns_g_lctx,
821 ns_client_aclmsg("query (cache)", name, qtype,
822 client->view->rdclass,
824 ns_client_log(client,
825 DNS_LOGCATEGORY_SECURITY,
831 ns_client_aclmsg("query (cache)", name, qtype,
832 client->view->rdclass, msg,
834 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
835 NS_LOGMODULE_QUERY, ISC_LOG_INFO,
839 * We've now evaluated the view's query ACL, and
840 * the NS_QUERYATTR_QUERYOK attribute is now valid.
842 client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
844 if (result != ISC_R_SUCCESS)
850 /* Transfer ownership. */
853 return (ISC_R_SUCCESS);
856 result = DNS_R_REFUSED;
865 static inline isc_result_t
866 query_getdb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
867 unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
868 dns_dbversion_t **versionp, isc_boolean_t *is_zonep)
873 isc_result_t tresult;
874 unsigned int namelabels;
875 unsigned int zonelabels;
876 dns_zone_t *zone = NULL;
879 REQUIRE(zonep != NULL && *zonep == NULL);
883 /* Calculate how many labels are in name. */
884 namelabels = dns_name_countlabels(name);
887 /* Try to find name in bind's standard database. */
888 result = query_getzonedb(client, name, qtype, options, &zone,
891 /* See how many labels are in the zone's name. */
892 if (result == ISC_R_SUCCESS && zone != NULL)
893 zonelabels = dns_name_countlabels(dns_zone_getorigin(zone));
895 * If # zone labels < # name labels, try to find an even better match
896 * Only try if a DLZ driver is loaded for this view
898 if (zonelabels < namelabels && client->view->dlzdatabase != NULL) {
899 tresult = dns_dlzfindzone(client->view, name,
901 /* If we successful, we found a better match. */
902 if (tresult == ISC_R_SUCCESS) {
904 * If the previous search returned a zone, detach it.
907 dns_zone_detach(&zone);
910 * If the previous search returned a database,
917 * If the previous search returned a version, clear it.
922 * Get our database version.
924 dns_db_currentversion(tdbp, versionp);
927 * Be sure to return our database.
932 * We return a null zone, No stats for DLZ zones.
939 result = query_getzonedb(client, name, qtype, options,
940 zonep, dbp, versionp);
943 /* If successfull, Transfer ownership of zone. */
944 if (result == ISC_R_SUCCESS) {
949 * If neither attempt above succeeded, return the cache instead
951 *is_zonep = ISC_TRUE;
952 } else if (result == ISC_R_NOTFOUND) {
953 result = query_getcachedb(client, name, qtype, dbp, options);
954 *is_zonep = ISC_FALSE;
959 static inline isc_boolean_t
960 query_isduplicate(ns_client_t *client, dns_name_t *name,
961 dns_rdatatype_t type, dns_name_t **mnamep)
963 dns_section_t section;
964 dns_name_t *mname = NULL;
967 CTRACE("query_isduplicate");
969 for (section = DNS_SECTION_ANSWER;
970 section <= DNS_SECTION_ADDITIONAL;
972 result = dns_message_findname(client->message, section,
973 name, type, 0, &mname, NULL);
974 if (result == ISC_R_SUCCESS) {
976 * We've already got this RRset in the response.
978 CTRACE("query_isduplicate: true: done");
980 } else if (result == DNS_R_NXRRSET) {
982 * The name exists, but the rdataset does not.
984 if (section == DNS_SECTION_ADDITIONAL)
987 RUNTIME_CHECK(result == DNS_R_NXDOMAIN);
992 * If the dns_name_t we're looking up is already in the message,
993 * we don't want to trigger the caller's name replacement logic.
1000 CTRACE("query_isduplicate: false: done");
1005 query_addadditional(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1006 ns_client_t *client = arg;
1007 isc_result_t result, eresult;
1010 dns_name_t *fname, *mname;
1011 dns_rdataset_t *rdataset, *sigrdataset, *trdataset;
1014 dns_dbversion_t *version;
1015 isc_boolean_t added_something, need_addname;
1017 dns_rdatatype_t type;
1019 REQUIRE(NS_CLIENT_VALID(client));
1020 REQUIRE(qtype != dns_rdatatype_any);
1022 if (!WANTDNSSEC(client) && dns_rdatatype_isdnssec(qtype))
1023 return (ISC_R_SUCCESS);
1025 CTRACE("query_addadditional");
1030 eresult = ISC_R_SUCCESS;
1038 added_something = ISC_FALSE;
1039 need_addname = ISC_FALSE;
1043 * We treat type A additional section processing as if it
1044 * were "any address type" additional section processing.
1045 * To avoid multiple lookups, we do an 'any' database
1046 * lookup and iterate over the node.
1048 if (qtype == dns_rdatatype_a)
1049 type = dns_rdatatype_any;
1054 * Get some resources.
1056 dbuf = query_getnamebuf(client);
1059 fname = query_newname(client, dbuf, &b);
1060 rdataset = query_newrdataset(client);
1061 if (fname == NULL || rdataset == NULL)
1063 if (WANTDNSSEC(client)) {
1064 sigrdataset = query_newrdataset(client);
1065 if (sigrdataset == NULL)
1070 * Look for a zone database that might contain authoritative
1073 result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1074 &zone, &db, &version);
1075 if (result != ISC_R_SUCCESS)
1078 CTRACE("query_addadditional: db_find");
1081 * Since we are looking for authoritative data, we do not set
1082 * the GLUEOK flag. Glue will be looked for later, but not
1083 * necessarily in the same database.
1086 result = dns_db_find(db, name, version, type, client->query.dboptions,
1087 client->now, &node, fname, rdataset,
1089 if (result == ISC_R_SUCCESS)
1092 if (dns_rdataset_isassociated(rdataset))
1093 dns_rdataset_disassociate(rdataset);
1094 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
1095 dns_rdataset_disassociate(sigrdataset);
1097 dns_db_detachnode(db, &node);
1102 * No authoritative data was found. The cache is our next best bet.
1106 result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1107 if (result != ISC_R_SUCCESS)
1109 * Most likely the client isn't allowed to query the cache.
1113 * Attempt to validate glue.
1115 if (sigrdataset == NULL) {
1116 sigrdataset = query_newrdataset(client);
1117 if (sigrdataset == NULL)
1120 result = dns_db_find(db, name, version, type,
1121 client->query.dboptions | DNS_DBFIND_GLUEOK,
1122 client->now, &node, fname, rdataset,
1124 if (result == DNS_R_GLUE &&
1125 validate(client, db, fname, rdataset, sigrdataset))
1126 result = ISC_R_SUCCESS;
1127 if (!WANTDNSSEC(client))
1128 query_putrdataset(client, &sigrdataset);
1129 if (result == ISC_R_SUCCESS)
1132 if (dns_rdataset_isassociated(rdataset))
1133 dns_rdataset_disassociate(rdataset);
1134 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
1135 dns_rdataset_disassociate(sigrdataset);
1137 dns_db_detachnode(db, &node);
1142 * No cached data was found. Glue is our last chance.
1145 * NS records cause both the usual additional section
1146 * processing to locate a type A record, and, when used
1147 * in a referral, a special search of the zone in which
1148 * they reside for glue information.
1150 * This is the "special search". Note that we must search
1151 * the zone where the NS record resides, not the zone it
1152 * points to, and that we only do the search in the delegation
1153 * case (identified by client->query.gluedb being set).
1156 if (client->query.gluedb == NULL)
1160 * Don't poision caches using the bailiwick protection model.
1162 if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1165 dns_db_attach(client->query.gluedb, &db);
1166 result = dns_db_find(db, name, version, type,
1167 client->query.dboptions | DNS_DBFIND_GLUEOK,
1168 client->now, &node, fname, rdataset,
1170 if (!(result == ISC_R_SUCCESS ||
1171 result == DNS_R_ZONECUT ||
1172 result == DNS_R_GLUE))
1177 * We have found a potential additional data rdataset, or
1178 * at least a node to iterate over.
1180 query_keepname(client, fname, dbuf);
1183 * If we have an rdataset, add it to the additional data
1187 if (dns_rdataset_isassociated(rdataset) &&
1188 !query_isduplicate(client, fname, type, &mname)) {
1189 if (mname != NULL) {
1190 query_releasename(client, &fname);
1193 need_addname = ISC_TRUE;
1194 ISC_LIST_APPEND(fname->list, rdataset, link);
1195 trdataset = rdataset;
1197 added_something = ISC_TRUE;
1199 * Note: we only add SIGs if we've added the type they cover,
1200 * so we do not need to check if the SIG rdataset is already
1203 if (sigrdataset != NULL &&
1204 dns_rdataset_isassociated(sigrdataset))
1206 ISC_LIST_APPEND(fname->list, sigrdataset, link);
1211 if (qtype == dns_rdatatype_a) {
1213 * We now go looking for A and AAAA records, along with
1216 * XXXRTH This code could be more efficient.
1218 if (rdataset != NULL) {
1219 if (dns_rdataset_isassociated(rdataset))
1220 dns_rdataset_disassociate(rdataset);
1222 rdataset = query_newrdataset(client);
1223 if (rdataset == NULL)
1226 if (sigrdataset != NULL) {
1227 if (dns_rdataset_isassociated(sigrdataset))
1228 dns_rdataset_disassociate(sigrdataset);
1229 } else if (WANTDNSSEC(client)) {
1230 sigrdataset = query_newrdataset(client);
1231 if (sigrdataset == NULL)
1234 result = dns_db_findrdataset(db, node, version,
1236 client->now, rdataset,
1238 if (result == DNS_R_NCACHENXDOMAIN)
1240 if (result == DNS_R_NCACHENXRRSET) {
1241 dns_rdataset_disassociate(rdataset);
1243 * Negative cache entries don't have sigrdatasets.
1245 INSIST(sigrdataset == NULL ||
1246 ! dns_rdataset_isassociated(sigrdataset));
1248 if (result == ISC_R_SUCCESS) {
1250 if (!query_isduplicate(client, fname,
1251 dns_rdatatype_a, &mname)) {
1252 if (mname != NULL) {
1253 query_releasename(client, &fname);
1256 need_addname = ISC_TRUE;
1257 ISC_LIST_APPEND(fname->list, rdataset, link);
1258 added_something = ISC_TRUE;
1259 if (sigrdataset != NULL &&
1260 dns_rdataset_isassociated(sigrdataset))
1262 ISC_LIST_APPEND(fname->list,
1265 query_newrdataset(client);
1267 rdataset = query_newrdataset(client);
1268 if (rdataset == NULL)
1270 if (WANTDNSSEC(client) && sigrdataset == NULL)
1273 dns_rdataset_disassociate(rdataset);
1274 if (sigrdataset != NULL &&
1275 dns_rdataset_isassociated(sigrdataset))
1276 dns_rdataset_disassociate(sigrdataset);
1279 result = dns_db_findrdataset(db, node, version,
1280 dns_rdatatype_aaaa, 0,
1281 client->now, rdataset,
1283 if (result == DNS_R_NCACHENXDOMAIN)
1285 if (result == DNS_R_NCACHENXRRSET) {
1286 dns_rdataset_disassociate(rdataset);
1287 INSIST(sigrdataset == NULL ||
1288 ! dns_rdataset_isassociated(sigrdataset));
1290 if (result == ISC_R_SUCCESS) {
1292 if (!query_isduplicate(client, fname,
1293 dns_rdatatype_aaaa, &mname)) {
1294 if (mname != NULL) {
1295 query_releasename(client, &fname);
1298 need_addname = ISC_TRUE;
1299 ISC_LIST_APPEND(fname->list, rdataset, link);
1300 added_something = ISC_TRUE;
1301 if (sigrdataset != NULL &&
1302 dns_rdataset_isassociated(sigrdataset))
1304 ISC_LIST_APPEND(fname->list,
1314 CTRACE("query_addadditional: addname");
1316 * If we haven't added anything, then we're done.
1318 if (!added_something)
1322 * We may have added our rdatasets to an existing name, if so, then
1323 * need_addname will be ISC_FALSE. Whether we used an existing name
1324 * or a new one, we must set fname to NULL to prevent cleanup.
1327 dns_message_addname(client->message, fname,
1328 DNS_SECTION_ADDITIONAL);
1332 * In a few cases, we want to add additional data for additional
1333 * data. It's simpler to just deal with special cases here than
1334 * to try to create a general purpose mechanism and allow the
1335 * rdata implementations to do it themselves.
1337 * This involves recursion, but the depth is limited. The
1338 * most complex case is adding a SRV rdataset, which involves
1339 * recursing to add address records, which in turn can cause
1340 * recursion to add KEYs.
1342 if (type == dns_rdatatype_srv && trdataset != NULL) {
1344 * If we're adding SRV records to the additional data
1345 * section, it's helpful if we add the SRV additional data
1348 eresult = dns_rdataset_additionaldata(trdataset,
1349 query_addadditional,
1354 CTRACE("query_addadditional: cleanup");
1355 query_putrdataset(client, &rdataset);
1356 if (sigrdataset != NULL)
1357 query_putrdataset(client, &sigrdataset);
1359 query_releasename(client, &fname);
1361 dns_db_detachnode(db, &node);
1365 dns_zone_detach(&zone);
1367 CTRACE("query_addadditional: done");
1372 query_discardcache(ns_client_t *client, dns_rdataset_t *rdataset_base,
1373 dns_rdatasetadditional_t additionaltype,
1374 dns_rdatatype_t type, dns_zone_t **zonep, dns_db_t **dbp,
1375 dns_dbversion_t **versionp, dns_dbnode_t **nodep,
1378 dns_rdataset_t *rdataset;
1380 while ((rdataset = ISC_LIST_HEAD(fname->list)) != NULL) {
1381 ISC_LIST_UNLINK(fname->list, rdataset, link);
1382 query_putrdataset(client, &rdataset);
1384 if (*versionp != NULL)
1385 dns_db_closeversion(*dbp, versionp, ISC_FALSE);
1387 dns_db_detachnode(*dbp, nodep);
1391 dns_zone_detach(zonep);
1392 (void)dns_rdataset_putadditional(client->view->acache, rdataset_base,
1393 additionaltype, type);
1396 static inline isc_result_t
1397 query_iscachevalid(dns_zone_t *zone, dns_db_t *db, dns_db_t *db0,
1398 dns_dbversion_t *version)
1400 isc_result_t result = ISC_R_SUCCESS;
1401 dns_dbversion_t *version_current = NULL;
1402 dns_db_t *db_current = db0;
1404 if (db_current == NULL) {
1405 result = dns_zone_getdb(zone, &db_current);
1406 if (result != ISC_R_SUCCESS)
1409 dns_db_currentversion(db_current, &version_current);
1410 if (db_current != db || version_current != version) {
1411 result = ISC_R_FAILURE;
1416 dns_db_closeversion(db_current, &version_current, ISC_FALSE);
1417 if (db0 == NULL && db_current != NULL)
1418 dns_db_detach(&db_current);
1424 query_addadditional2(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1425 client_additionalctx_t *additionalctx = arg;
1426 dns_rdataset_t *rdataset_base;
1427 ns_client_t *client;
1428 isc_result_t result, eresult;
1429 dns_dbnode_t *node, *cnode;
1431 dns_name_t *fname, *mname0, cfname;
1432 dns_rdataset_t *rdataset, *sigrdataset;
1433 dns_rdataset_t *crdataset, *crdataset_next;
1436 dns_dbversion_t *version, *cversion;
1437 isc_boolean_t added_something, need_addname, needadditionalcache;
1438 isc_boolean_t need_sigrrset;
1440 dns_rdatatype_t type;
1441 dns_rdatasetadditional_t additionaltype;
1443 if (qtype != dns_rdatatype_a) {
1445 * This function is optimized for "address" types. For other
1446 * types, use a generic routine.
1447 * XXX: ideally, this function should be generic enough.
1449 return (query_addadditional(additionalctx->client,
1456 rdataset_base = additionalctx->rdataset;
1457 client = additionalctx->client;
1458 REQUIRE(NS_CLIENT_VALID(client));
1459 eresult = ISC_R_SUCCESS;
1469 added_something = ISC_FALSE;
1470 need_addname = ISC_FALSE;
1472 needadditionalcache = ISC_FALSE;
1473 additionaltype = dns_rdatasetadditional_fromauth;
1474 dns_name_init(&cfname, NULL);
1476 CTRACE("query_addadditional2");
1479 * We treat type A additional section processing as if it
1480 * were "any address type" additional section processing.
1481 * To avoid multiple lookups, we do an 'any' database
1482 * lookup and iterate over the node.
1483 * XXXJT: this approach can cause a suboptimal result when the cache
1484 * DB only has partial address types and the glue DB has remaining
1487 type = dns_rdatatype_any;
1490 * Get some resources.
1492 dbuf = query_getnamebuf(client);
1495 fname = query_newname(client, dbuf, &b);
1498 dns_name_setbuffer(&cfname, &b); /* share the buffer */
1500 /* Check additional cache */
1501 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1502 type, client->view->acache, &zone,
1503 &cdb, &cversion, &cnode, &cfname,
1504 client->message, client->now);
1505 if (result != ISC_R_SUCCESS)
1508 CTRACE("query_addadditional2: auth zone not found");
1512 /* Is the cached DB up-to-date? */
1513 result = query_iscachevalid(zone, cdb, NULL, cversion);
1514 if (result != ISC_R_SUCCESS) {
1515 CTRACE("query_addadditional2: old auth additional cache");
1516 query_discardcache(client, rdataset_base, additionaltype,
1517 type, &zone, &cdb, &cversion, &cnode,
1522 if (cnode == NULL) {
1524 * We have a negative cache. We don't have to check the zone
1525 * ACL, since the result (not using this zone) would be same
1526 * regardless of the result.
1528 CTRACE("query_addadditional2: negative auth additional cache");
1529 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1530 dns_db_detach(&cdb);
1531 dns_zone_detach(&zone);
1535 result = query_validatezonedb(client, name, qtype, DNS_GETDB_NOLOG,
1537 if (result != ISC_R_SUCCESS) {
1538 query_discardcache(client, rdataset_base, additionaltype,
1539 type, &zone, &cdb, &cversion, &cnode,
1544 /* We've got an active cache. */
1545 CTRACE("query_addadditional2: auth additional cache");
1546 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1549 dns_name_clone(&cfname, fname);
1550 query_keepname(client, fname, dbuf);
1554 * Look for a zone database that might contain authoritative
1558 result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1559 &zone, &db, &version);
1560 if (result != ISC_R_SUCCESS) {
1561 /* Cache the negative result */
1562 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1563 type, client->view->acache,
1564 NULL, NULL, NULL, NULL,
1569 CTRACE("query_addadditional2: db_find");
1572 * Since we are looking for authoritative data, we do not set
1573 * the GLUEOK flag. Glue will be looked for later, but not
1574 * necessarily in the same database.
1577 result = dns_db_find(db, name, version, type, client->query.dboptions,
1578 client->now, &node, fname, NULL, NULL);
1579 if (result == ISC_R_SUCCESS)
1582 /* Cache the negative result */
1583 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1584 type, client->view->acache, zone, db,
1585 version, NULL, fname);
1588 dns_db_detachnode(db, &node);
1593 * No authoritative data was found. The cache is our next best bet.
1597 additionaltype = dns_rdatasetadditional_fromcache;
1598 result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1599 if (result != ISC_R_SUCCESS)
1601 * Most likely the client isn't allowed to query the cache.
1605 result = dns_db_find(db, name, version, type,
1606 client->query.dboptions | DNS_DBFIND_GLUEOK,
1607 client->now, &node, fname, NULL, NULL);
1608 if (result == ISC_R_SUCCESS)
1612 dns_db_detachnode(db, &node);
1617 * No cached data was found. Glue is our last chance.
1620 * NS records cause both the usual additional section
1621 * processing to locate a type A record, and, when used
1622 * in a referral, a special search of the zone in which
1623 * they reside for glue information.
1625 * This is the "special search". Note that we must search
1626 * the zone where the NS record resides, not the zone it
1627 * points to, and that we only do the search in the delegation
1628 * case (identified by client->query.gluedb being set).
1630 if (client->query.gluedb == NULL)
1634 * Don't poision caches using the bailiwick protection model.
1636 if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1639 /* Check additional cache */
1640 additionaltype = dns_rdatasetadditional_fromglue;
1641 result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1642 type, client->view->acache, NULL,
1643 &cdb, &cversion, &cnode, &cfname,
1644 client->message, client->now);
1645 if (result != ISC_R_SUCCESS)
1648 result = query_iscachevalid(zone, cdb, client->query.gluedb, cversion);
1649 if (result != ISC_R_SUCCESS) {
1650 CTRACE("query_addadditional2: old glue additional cache");
1651 query_discardcache(client, rdataset_base, additionaltype,
1652 type, &zone, &cdb, &cversion, &cnode,
1657 if (cnode == NULL) {
1658 /* We have a negative cache. */
1659 CTRACE("query_addadditional2: negative glue additional cache");
1660 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1661 dns_db_detach(&cdb);
1666 CTRACE("query_addadditional2: glue additional cache");
1667 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1670 dns_name_clone(&cfname, fname);
1671 query_keepname(client, fname, dbuf);
1675 dns_db_attach(client->query.gluedb, &db);
1676 result = dns_db_find(db, name, version, type,
1677 client->query.dboptions | DNS_DBFIND_GLUEOK,
1678 client->now, &node, fname, NULL, NULL);
1679 if (!(result == ISC_R_SUCCESS ||
1680 result == DNS_R_ZONECUT ||
1681 result == DNS_R_GLUE)) {
1682 /* cache the negative result */
1683 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1684 type, client->view->acache,
1685 NULL, db, version, NULL,
1692 * We have found a DB node to iterate over from a DB.
1693 * We are going to look for address RRsets (i.e., A and AAAA) in the DB
1694 * node we've just found. We'll then store the complete information
1695 * in the additional data cache.
1697 dns_name_clone(fname, &cfname);
1698 query_keepname(client, fname, dbuf);
1699 needadditionalcache = ISC_TRUE;
1701 rdataset = query_newrdataset(client);
1702 if (rdataset == NULL)
1705 sigrdataset = query_newrdataset(client);
1706 if (sigrdataset == NULL)
1710 * Find A RRset with sig RRset. Even if we don't find a sig RRset
1711 * for a client using DNSSEC, we'll continue the process to make a
1712 * complete list to be cached. However, we need to cancel the
1713 * caching when something unexpected happens, in order to avoid
1714 * caching incomplete information.
1716 result = dns_db_findrdataset(db, node, version, dns_rdatatype_a, 0,
1717 client->now, rdataset, sigrdataset);
1719 * If we can't promote glue/pending from the cache to secure
1722 if (result == ISC_R_SUCCESS &&
1723 additionaltype == dns_rdatasetadditional_fromcache &&
1724 (rdataset->trust == dns_trust_pending ||
1725 rdataset->trust == dns_trust_glue) &&
1726 !validate(client, db, fname, rdataset, sigrdataset)) {
1727 dns_rdataset_disassociate(rdataset);
1728 if (dns_rdataset_isassociated(sigrdataset))
1729 dns_rdataset_disassociate(sigrdataset);
1730 result = ISC_R_NOTFOUND;
1732 if (result == DNS_R_NCACHENXDOMAIN)
1734 if (result == DNS_R_NCACHENXRRSET) {
1735 dns_rdataset_disassociate(rdataset);
1737 * Negative cache entries don't have sigrdatasets.
1739 INSIST(! dns_rdataset_isassociated(sigrdataset));
1741 if (result == ISC_R_SUCCESS) {
1742 /* Remember the result as a cache */
1743 ISC_LIST_APPEND(cfname.list, rdataset, link);
1744 if (dns_rdataset_isassociated(sigrdataset)) {
1745 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1746 sigrdataset = query_newrdataset(client);
1748 rdataset = query_newrdataset(client);
1749 if (sigrdataset == NULL || rdataset == NULL) {
1750 /* do not cache incomplete information */
1755 /* Find AAAA RRset with sig RRset */
1756 result = dns_db_findrdataset(db, node, version, dns_rdatatype_aaaa,
1757 0, client->now, rdataset, sigrdataset);
1759 * If we can't promote glue/pending from the cache to secure
1762 if (result == ISC_R_SUCCESS &&
1763 additionaltype == dns_rdatasetadditional_fromcache &&
1764 (rdataset->trust == dns_trust_pending ||
1765 rdataset->trust == dns_trust_glue) &&
1766 !validate(client, db, fname, rdataset, sigrdataset)) {
1767 dns_rdataset_disassociate(rdataset);
1768 if (dns_rdataset_isassociated(sigrdataset))
1769 dns_rdataset_disassociate(sigrdataset);
1770 result = ISC_R_NOTFOUND;
1772 if (result == ISC_R_SUCCESS) {
1773 ISC_LIST_APPEND(cfname.list, rdataset, link);
1775 if (dns_rdataset_isassociated(sigrdataset)) {
1776 ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1783 * Set the new result in the cache if required. We do not support
1784 * caching additional data from a cache DB.
1786 if (needadditionalcache == ISC_TRUE &&
1787 (additionaltype == dns_rdatasetadditional_fromauth ||
1788 additionaltype == dns_rdatasetadditional_fromglue)) {
1789 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1790 type, client->view->acache,
1791 zone, db, version, node,
1796 need_sigrrset = ISC_FALSE;
1798 for (crdataset = ISC_LIST_HEAD(cfname.list);
1800 crdataset = crdataset_next) {
1803 crdataset_next = ISC_LIST_NEXT(crdataset, link);
1806 if (crdataset->type == dns_rdatatype_a ||
1807 crdataset->type == dns_rdatatype_aaaa) {
1808 if (!query_isduplicate(client, fname, crdataset->type,
1810 if (mname != NULL) {
1812 * A different type of this name is
1813 * already stored in the additional
1814 * section. We'll reuse the name.
1815 * Note that this should happen at most
1816 * once. Otherwise, fname->link could
1819 INSIST(mname0 == NULL);
1821 query_releasename(client, &fname);
1825 need_addname = ISC_TRUE;
1826 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1827 ISC_LIST_APPEND(fname->list, crdataset, link);
1828 added_something = ISC_TRUE;
1829 need_sigrrset = ISC_TRUE;
1831 need_sigrrset = ISC_FALSE;
1832 } else if (crdataset->type == dns_rdatatype_rrsig &&
1833 need_sigrrset && WANTDNSSEC(client)) {
1834 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1835 ISC_LIST_APPEND(fname->list, crdataset, link);
1836 added_something = ISC_TRUE; /* just in case */
1837 need_sigrrset = ISC_FALSE;
1841 CTRACE("query_addadditional2: addname");
1844 * If we haven't added anything, then we're done.
1846 if (!added_something)
1850 * We may have added our rdatasets to an existing name, if so, then
1851 * need_addname will be ISC_FALSE. Whether we used an existing name
1852 * or a new one, we must set fname to NULL to prevent cleanup.
1855 dns_message_addname(client->message, fname,
1856 DNS_SECTION_ADDITIONAL);
1860 CTRACE("query_addadditional2: cleanup");
1862 if (rdataset != NULL)
1863 query_putrdataset(client, &rdataset);
1864 if (sigrdataset != NULL)
1865 query_putrdataset(client, &sigrdataset);
1866 while ((crdataset = ISC_LIST_HEAD(cfname.list)) != NULL) {
1867 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1868 query_putrdataset(client, &crdataset);
1871 query_releasename(client, &fname);
1873 dns_db_detachnode(db, &node);
1877 dns_zone_detach(&zone);
1879 CTRACE("query_addadditional2: done");
1884 query_addrdataset(ns_client_t *client, dns_name_t *fname,
1885 dns_rdataset_t *rdataset)
1887 client_additionalctx_t additionalctx;
1890 * Add 'rdataset' and any pertinent additional data to
1891 * 'fname', a name in the response message for 'client'.
1894 CTRACE("query_addrdataset");
1896 ISC_LIST_APPEND(fname->list, rdataset, link);
1898 if (client->view->order != NULL)
1899 rdataset->attributes |= dns_order_find(client->view->order,
1900 fname, rdataset->type,
1902 rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
1904 if (NOADDITIONAL(client))
1908 * Add additional data.
1910 * We don't care if dns_rdataset_additionaldata() fails.
1912 additionalctx.client = client;
1913 additionalctx.rdataset = rdataset;
1914 (void)dns_rdataset_additionaldata(rdataset, query_addadditional2,
1916 CTRACE("query_addrdataset: done");
1920 query_addrrset(ns_client_t *client, dns_name_t **namep,
1921 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp,
1922 isc_buffer_t *dbuf, dns_section_t section)
1924 dns_name_t *name, *mname;
1925 dns_rdataset_t *rdataset, *mrdataset, *sigrdataset;
1926 isc_result_t result;
1929 * To the current response for 'client', add the answer RRset
1930 * '*rdatasetp' and an optional signature set '*sigrdatasetp', with
1931 * owner name '*namep', to section 'section', unless they are
1932 * already there. Also add any pertinent additional data.
1934 * If 'dbuf' is not NULL, then '*namep' is the name whose data is
1935 * stored in 'dbuf'. In this case, query_addrrset() guarantees that
1936 * when it returns the name will either have been kept or released.
1938 CTRACE("query_addrrset");
1940 rdataset = *rdatasetp;
1941 if (sigrdatasetp != NULL)
1942 sigrdataset = *sigrdatasetp;
1947 result = dns_message_findname(client->message, section,
1948 name, rdataset->type, rdataset->covers,
1949 &mname, &mrdataset);
1950 if (result == ISC_R_SUCCESS) {
1952 * We've already got an RRset of the given name and type.
1953 * There's nothing else to do;
1955 CTRACE("query_addrrset: dns_message_findname succeeded: done");
1957 query_releasename(client, namep);
1959 } else if (result == DNS_R_NXDOMAIN) {
1961 * The name doesn't exist.
1964 query_keepname(client, name, dbuf);
1965 dns_message_addname(client->message, name, section);
1969 RUNTIME_CHECK(result == DNS_R_NXRRSET);
1971 query_releasename(client, namep);
1974 if (rdataset->trust != dns_trust_secure &&
1975 (section == DNS_SECTION_ANSWER ||
1976 section == DNS_SECTION_AUTHORITY))
1977 client->query.attributes &= ~NS_QUERYATTR_SECURE;
1979 * Note: we only add SIGs if we've added the type they cover, so
1980 * we do not need to check if the SIG rdataset is already in the
1983 query_addrdataset(client, mname, rdataset);
1985 if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset)) {
1987 * We have a signature. Add it to the response.
1989 ISC_LIST_APPEND(mname->list, sigrdataset, link);
1990 *sigrdatasetp = NULL;
1992 CTRACE("query_addrrset: done");
1995 static inline isc_result_t
1996 query_addsoa(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version,
1997 isc_boolean_t zero_ttl)
2001 isc_result_t result, eresult;
2002 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2003 dns_rdataset_t **sigrdatasetp = NULL;
2005 CTRACE("query_addsoa");
2009 eresult = ISC_R_SUCCESS;
2015 * Get resources and make 'name' be the database origin.
2017 result = dns_message_gettempname(client->message, &name);
2018 if (result != ISC_R_SUCCESS)
2020 dns_name_init(name, NULL);
2021 dns_name_clone(dns_db_origin(db), name);
2022 rdataset = query_newrdataset(client);
2023 if (rdataset == NULL) {
2024 eresult = DNS_R_SERVFAIL;
2027 if (WANTDNSSEC(client)) {
2028 sigrdataset = query_newrdataset(client);
2029 if (sigrdataset == NULL) {
2030 eresult = DNS_R_SERVFAIL;
2038 result = dns_db_getoriginnode(db, &node);
2039 if (result == ISC_R_SUCCESS) {
2040 result = dns_db_findrdataset(db, node, version,
2042 0, client->now, rdataset,
2045 dns_fixedname_t foundname;
2048 dns_fixedname_init(&foundname);
2049 fname = dns_fixedname_name(&foundname);
2051 result = dns_db_find(db, name, version, dns_rdatatype_soa,
2052 client->query.dboptions, 0, &node,
2053 fname, rdataset, sigrdataset);
2055 if (result != ISC_R_SUCCESS) {
2057 * This is bad. We tried to get the SOA RR at the zone top
2058 * and it didn't work!
2060 eresult = DNS_R_SERVFAIL;
2063 * Extract the SOA MINIMUM.
2065 dns_rdata_soa_t soa;
2066 dns_rdata_t rdata = DNS_RDATA_INIT;
2067 result = dns_rdataset_first(rdataset);
2068 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2069 dns_rdataset_current(rdataset, &rdata);
2070 result = dns_rdata_tostruct(&rdata, &soa, NULL);
2071 if (result != ISC_R_SUCCESS)
2076 if (sigrdataset != NULL)
2077 sigrdataset->ttl = 0;
2081 * Add the SOA and its SIG to the response, with the
2082 * TTLs adjusted per RFC2308 section 3.
2084 if (rdataset->ttl > soa.minimum)
2085 rdataset->ttl = soa.minimum;
2086 if (sigrdataset != NULL && sigrdataset->ttl > soa.minimum)
2087 sigrdataset->ttl = soa.minimum;
2089 if (sigrdataset != NULL)
2090 sigrdatasetp = &sigrdataset;
2092 sigrdatasetp = NULL;
2093 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2094 DNS_SECTION_AUTHORITY);
2098 query_putrdataset(client, &rdataset);
2099 if (sigrdataset != NULL)
2100 query_putrdataset(client, &sigrdataset);
2102 query_releasename(client, &name);
2104 dns_db_detachnode(db, &node);
2109 static inline isc_result_t
2110 query_addns(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version) {
2111 dns_name_t *name, *fname;
2113 isc_result_t result, eresult;
2114 dns_fixedname_t foundname;
2115 dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2116 dns_rdataset_t **sigrdatasetp = NULL;
2118 CTRACE("query_addns");
2122 eresult = ISC_R_SUCCESS;
2126 dns_fixedname_init(&foundname);
2127 fname = dns_fixedname_name(&foundname);
2130 * Get resources and make 'name' be the database origin.
2132 result = dns_message_gettempname(client->message, &name);
2133 if (result != ISC_R_SUCCESS) {
2134 CTRACE("query_addns: dns_message_gettempname failed: done");
2137 dns_name_init(name, NULL);
2138 dns_name_clone(dns_db_origin(db), name);
2139 rdataset = query_newrdataset(client);
2140 if (rdataset == NULL) {
2141 CTRACE("query_addns: query_newrdataset failed");
2142 eresult = DNS_R_SERVFAIL;
2145 if (WANTDNSSEC(client)) {
2146 sigrdataset = query_newrdataset(client);
2147 if (sigrdataset == NULL) {
2148 CTRACE("query_addns: query_newrdataset failed");
2149 eresult = DNS_R_SERVFAIL;
2155 * Find the NS rdataset.
2157 result = dns_db_getoriginnode(db, &node);
2158 if (result == ISC_R_SUCCESS) {
2159 result = dns_db_findrdataset(db, node, version,
2161 0, client->now, rdataset,
2164 CTRACE("query_addns: calling dns_db_find");
2165 result = dns_db_find(db, name, NULL, dns_rdatatype_ns,
2166 client->query.dboptions, 0, &node,
2167 fname, rdataset, sigrdataset);
2168 CTRACE("query_addns: dns_db_find complete");
2170 if (result != ISC_R_SUCCESS) {
2171 CTRACE("query_addns: "
2172 "dns_db_findrdataset or dns_db_find failed");
2174 * This is bad. We tried to get the NS rdataset at the zone
2175 * top and it didn't work!
2177 eresult = DNS_R_SERVFAIL;
2179 if (sigrdataset != NULL)
2180 sigrdatasetp = &sigrdataset;
2182 sigrdatasetp = NULL;
2183 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2184 DNS_SECTION_AUTHORITY);
2188 CTRACE("query_addns: cleanup");
2189 query_putrdataset(client, &rdataset);
2190 if (sigrdataset != NULL)
2191 query_putrdataset(client, &sigrdataset);
2193 query_releasename(client, &name);
2195 dns_db_detachnode(db, &node);
2197 CTRACE("query_addns: done");
2201 static inline isc_result_t
2202 query_addcnamelike(ns_client_t *client, dns_name_t *qname, dns_name_t *tname,
2203 dns_trust_t trust, dns_name_t **anamep, dns_rdatatype_t type)
2205 dns_rdataset_t *rdataset;
2206 dns_rdatalist_t *rdatalist;
2208 isc_result_t result;
2212 * We assume the name data referred to by tname won't go away.
2215 REQUIRE(anamep != NULL);
2218 result = dns_message_gettemprdatalist(client->message, &rdatalist);
2219 if (result != ISC_R_SUCCESS)
2222 result = dns_message_gettemprdata(client->message, &rdata);
2223 if (result != ISC_R_SUCCESS)
2226 result = dns_message_gettemprdataset(client->message, &rdataset);
2227 if (result != ISC_R_SUCCESS)
2229 dns_rdataset_init(rdataset);
2230 result = dns_name_dup(qname, client->mctx, *anamep);
2231 if (result != ISC_R_SUCCESS) {
2232 dns_message_puttemprdataset(client->message, &rdataset);
2236 rdatalist->type = type;
2237 rdatalist->covers = 0;
2238 rdatalist->rdclass = client->message->rdclass;
2241 dns_name_toregion(tname, &r);
2242 rdata->data = r.base;
2243 rdata->length = r.length;
2244 rdata->rdclass = client->message->rdclass;
2247 ISC_LIST_INIT(rdatalist->rdata);
2248 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
2249 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
2251 rdataset->trust = trust;
2253 query_addrrset(client, anamep, &rdataset, NULL, NULL,
2254 DNS_SECTION_ANSWER);
2256 if (rdataset != NULL) {
2257 if (dns_rdataset_isassociated(rdataset))
2258 dns_rdataset_disassociate(rdataset);
2259 dns_message_puttemprdataset(client->message, &rdataset);
2262 return (ISC_R_SUCCESS);
2266 * Mark the RRsets as secure. Update the cache (db) to reflect the
2267 * change in trust level.
2270 mark_secure(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2271 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2273 isc_result_t result;
2274 dns_dbnode_t *node = NULL;
2276 rdataset->trust = dns_trust_secure;
2277 sigrdataset->trust = dns_trust_secure;
2280 * Save the updated secure state. Ignore failures.
2282 result = dns_db_findnode(db, name, ISC_TRUE, &node);
2283 if (result != ISC_R_SUCCESS)
2285 (void)dns_db_addrdataset(db, node, NULL, client->now, rdataset,
2287 (void)dns_db_addrdataset(db, node, NULL, client->now, sigrdataset,
2289 dns_db_detachnode(db, &node);
2293 * Find the secure key that corresponds to rrsig.
2294 * Note: 'keyrdataset' maintains state between sucessive calls,
2295 * there may be multiple keys with the same keyid.
2296 * Return ISC_FALSE if we have exhausted all the possible keys.
2298 static isc_boolean_t
2299 get_key(ns_client_t *client, dns_db_t *db, dns_rdata_rrsig_t *rrsig,
2300 dns_rdataset_t *keyrdataset, dst_key_t **keyp)
2302 isc_result_t result;
2303 dns_dbnode_t *node = NULL;
2304 isc_boolean_t secure = ISC_FALSE;
2306 if (!dns_rdataset_isassociated(keyrdataset)) {
2307 result = dns_db_findnode(db, &rrsig->signer, ISC_FALSE, &node);
2308 if (result != ISC_R_SUCCESS)
2311 result = dns_db_findrdataset(db, node, NULL,
2312 dns_rdatatype_dnskey, 0,
2313 client->now, keyrdataset, NULL);
2314 dns_db_detachnode(db, &node);
2315 if (result != ISC_R_SUCCESS)
2318 if (keyrdataset->trust != dns_trust_secure)
2321 result = dns_rdataset_first(keyrdataset);
2323 result = dns_rdataset_next(keyrdataset);
2325 for ( ; result == ISC_R_SUCCESS;
2326 result = dns_rdataset_next(keyrdataset)) {
2327 dns_rdata_t rdata = DNS_RDATA_INIT;
2330 dns_rdataset_current(keyrdataset, &rdata);
2331 isc_buffer_init(&b, rdata.data, rdata.length);
2332 isc_buffer_add(&b, rdata.length);
2333 result = dst_key_fromdns(&rrsig->signer, rdata.rdclass, &b,
2334 client->mctx, keyp);
2335 if (result != ISC_R_SUCCESS)
2337 if (rrsig->algorithm == (dns_secalg_t)dst_key_alg(*keyp) &&
2338 rrsig->keyid == (dns_keytag_t)dst_key_id(*keyp) &&
2339 dst_key_iszonekey(*keyp)) {
2348 static isc_boolean_t
2349 verify(dst_key_t *key, dns_name_t *name, dns_rdataset_t *rdataset,
2350 dns_rdata_t *rdata, isc_mem_t *mctx, isc_boolean_t acceptexpired)
2352 isc_result_t result;
2353 dns_fixedname_t fixed;
2354 isc_boolean_t ignore = ISC_FALSE;
2356 dns_fixedname_init(&fixed);
2359 result = dns_dnssec_verify2(name, rdataset, key, ignore, mctx,
2361 if (result == DNS_R_SIGEXPIRED && acceptexpired) {
2365 if (result == ISC_R_SUCCESS || result == DNS_R_FROMWILDCARD)
2371 * Validate the rdataset if possible with available records.
2373 static isc_boolean_t
2374 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2375 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2377 isc_result_t result;
2378 dns_rdata_t rdata = DNS_RDATA_INIT;
2379 dns_rdata_rrsig_t rrsig;
2380 dst_key_t *key = NULL;
2381 dns_rdataset_t keyrdataset;
2383 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2386 for (result = dns_rdataset_first(sigrdataset);
2387 result == ISC_R_SUCCESS;
2388 result = dns_rdataset_next(sigrdataset)) {
2390 dns_rdata_reset(&rdata);
2391 dns_rdataset_current(sigrdataset, &rdata);
2392 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
2393 if (result != ISC_R_SUCCESS)
2395 if (!dns_resolver_algorithm_supported(client->view->resolver,
2396 name, rrsig.algorithm))
2398 if (!dns_name_issubdomain(name, &rrsig.signer))
2400 dns_rdataset_init(&keyrdataset);
2402 if (!get_key(client, db, &rrsig, &keyrdataset, &key))
2404 if (verify(key, name, rdataset, &rdata, client->mctx,
2405 client->view->acceptexpired)) {
2407 dns_rdataset_disassociate(&keyrdataset);
2408 mark_secure(client, db, name, rdataset,
2414 if (dns_rdataset_isassociated(&keyrdataset))
2415 dns_rdataset_disassociate(&keyrdataset);
2421 query_addbestns(ns_client_t *client) {
2424 dns_name_t *fname, *zfname;
2425 dns_rdataset_t *rdataset, *sigrdataset, *zrdataset, *zsigrdataset;
2426 isc_boolean_t is_zone, use_zone;
2428 isc_result_t result;
2429 dns_dbversion_t *version;
2433 CTRACE("query_addbestns");
2439 zsigrdataset = NULL;
2445 is_zone = ISC_FALSE;
2446 use_zone = ISC_FALSE;
2449 * Find the right database.
2451 result = query_getdb(client, client->query.qname, dns_rdatatype_ns, 0,
2452 &zone, &db, &version, &is_zone);
2453 if (result != ISC_R_SUCCESS)
2458 * We'll need some resources...
2460 dbuf = query_getnamebuf(client);
2463 fname = query_newname(client, dbuf, &b);
2464 rdataset = query_newrdataset(client);
2465 if (fname == NULL || rdataset == NULL)
2468 * Get the RRSIGs if the client requested them or if we may
2469 * need to validate answers from the cache.
2471 if (WANTDNSSEC(client) || !is_zone) {
2472 sigrdataset = query_newrdataset(client);
2473 if (sigrdataset == NULL)
2478 * Now look for the zonecut.
2481 result = dns_db_find(db, client->query.qname, version,
2482 dns_rdatatype_ns, client->query.dboptions,
2483 client->now, &node, fname,
2484 rdataset, sigrdataset);
2485 if (result != DNS_R_DELEGATION)
2487 if (USECACHE(client)) {
2488 query_keepname(client, fname, dbuf);
2492 zrdataset = rdataset;
2494 zsigrdataset = sigrdataset;
2496 dns_db_detachnode(db, &node);
2499 dns_db_attach(client->view->cachedb, &db);
2500 is_zone = ISC_FALSE;
2504 result = dns_db_findzonecut(db, client->query.qname,
2505 client->query.dboptions,
2506 client->now, &node, fname,
2507 rdataset, sigrdataset);
2508 if (result == ISC_R_SUCCESS) {
2509 if (zfname != NULL &&
2510 !dns_name_issubdomain(fname, zfname)) {
2512 * We found a zonecut in the cache, but our
2513 * zone delegation is better.
2515 use_zone = ISC_TRUE;
2517 } else if (result == ISC_R_NOTFOUND && zfname != NULL) {
2519 * We didn't find anything in the cache, but we
2520 * have a zone delegation, so use it.
2522 use_zone = ISC_TRUE;
2528 query_releasename(client, &fname);
2532 * We've already done query_keepname() on
2533 * zfname, so we must set dbuf to NULL to
2534 * prevent query_addrrset() from trying to
2535 * call query_keepname() again.
2538 query_putrdataset(client, &rdataset);
2539 if (sigrdataset != NULL)
2540 query_putrdataset(client, &sigrdataset);
2541 rdataset = zrdataset;
2543 sigrdataset = zsigrdataset;
2544 zsigrdataset = NULL;
2548 * Attempt to validate RRsets that are pending or that are glue.
2550 if ((rdataset->trust == dns_trust_pending ||
2551 (sigrdataset != NULL && sigrdataset->trust == dns_trust_pending))
2552 && !validate(client, db, fname, rdataset, sigrdataset) &&
2553 (client->query.dboptions & DNS_DBFIND_PENDINGOK) == 0)
2556 if ((rdataset->trust == dns_trust_glue ||
2557 (sigrdataset != NULL && sigrdataset->trust == dns_trust_glue)) &&
2558 !validate(client, db, fname, rdataset, sigrdataset) &&
2559 SECURE(client) && WANTDNSSEC(client))
2563 * If the client doesn't want DNSSEC we can discard the sigrdataset
2566 if (!WANTDNSSEC(client))
2567 query_putrdataset(client, &sigrdataset);
2568 query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2569 DNS_SECTION_AUTHORITY);
2572 if (rdataset != NULL)
2573 query_putrdataset(client, &rdataset);
2574 if (sigrdataset != NULL)
2575 query_putrdataset(client, &sigrdataset);
2577 query_releasename(client, &fname);
2579 dns_db_detachnode(db, &node);
2583 dns_zone_detach(&zone);
2585 query_putrdataset(client, &zrdataset);
2586 if (zsigrdataset != NULL)
2587 query_putrdataset(client, &zsigrdataset);
2589 query_releasename(client, &zfname);
2590 dns_db_detach(&zdb);
2595 query_addds(ns_client_t *client, dns_db_t *db, dns_dbnode_t *node,
2596 dns_dbversion_t *version)
2599 dns_rdataset_t *rdataset, *sigrdataset;
2600 isc_result_t result;
2602 CTRACE("query_addds");
2608 * We'll need some resources...
2610 rdataset = query_newrdataset(client);
2611 sigrdataset = query_newrdataset(client);
2612 if (rdataset == NULL || sigrdataset == NULL)
2616 * Look for the DS record, which may or may not be present.
2618 result = dns_db_findrdataset(db, node, version, dns_rdatatype_ds, 0,
2619 client->now, rdataset, sigrdataset);
2621 * If we didn't find it, look for an NSEC. */
2622 if (result == ISC_R_NOTFOUND)
2623 result = dns_db_findrdataset(db, node, version,
2624 dns_rdatatype_nsec, 0, client->now,
2625 rdataset, sigrdataset);
2626 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
2628 if (!dns_rdataset_isassociated(rdataset) ||
2629 !dns_rdataset_isassociated(sigrdataset))
2633 * We've already added the NS record, so if the name's not there,
2634 * we have other problems. Use this name rather than calling
2637 result = dns_message_firstname(client->message, DNS_SECTION_AUTHORITY);
2638 if (result != ISC_R_SUCCESS)
2642 dns_message_currentname(client->message, DNS_SECTION_AUTHORITY,
2644 result = dns_message_findtype(rname, dns_rdatatype_ns, 0, NULL);
2645 if (result != ISC_R_SUCCESS)
2648 ISC_LIST_APPEND(rname->list, rdataset, link);
2649 ISC_LIST_APPEND(rname->list, sigrdataset, link);
2654 if (rdataset != NULL)
2655 query_putrdataset(client, &rdataset);
2656 if (sigrdataset != NULL)
2657 query_putrdataset(client, &sigrdataset);
2661 query_addwildcardproof(ns_client_t *client, dns_db_t *db,
2662 dns_dbversion_t *version, dns_name_t *name,
2663 isc_boolean_t ispositive)
2665 isc_buffer_t *dbuf, b;
2667 dns_rdataset_t *rdataset, *sigrdataset;
2668 dns_fixedname_t wfixed;
2671 unsigned int options;
2672 unsigned int olabels, nlabels;
2673 isc_result_t result;
2674 dns_rdata_t rdata = DNS_RDATA_INIT;
2675 dns_rdata_nsec_t nsec;
2676 isc_boolean_t have_wname;
2679 CTRACE("query_addwildcardproof");
2686 * Get the NOQNAME proof then if !ispositve
2687 * get the NOWILDCARD proof.
2689 * DNS_DBFIND_NOWILD finds the NSEC records that covers the
2690 * name ignoring any wildcard. From the owner and next names
2691 * of this record you can compute which wildcard (if it exists)
2692 * will match by finding the longest common suffix of the
2693 * owner name and next names with the qname and prefixing that
2694 * with the wildcard label.
2699 * example NSEC b.example
2701 * b.example NSEC a.d.example
2703 * a.d.example NSEC g.f.example
2705 * g.f.example NSEC z.i.example
2707 * z.i.example NSEC example
2710 * a.example -> example NSEC b.example
2711 * owner common example
2712 * next common example
2714 * d.b.example -> b.example NSEC a.d.example
2715 * owner common b.example
2716 * next common example
2718 * a.f.example -> a.d.example NSEC g.f.example
2719 * owner common example
2720 * next common f.example
2722 * j.example -> z.i.example NSEC example
2723 * owner common example
2724 * next common example
2727 options = client->query.dboptions | DNS_DBFIND_NOWILD;
2728 dns_fixedname_init(&wfixed);
2729 wname = dns_fixedname_name(&wfixed);
2731 have_wname = ISC_FALSE;
2733 * We'll need some resources...
2735 dbuf = query_getnamebuf(client);
2738 fname = query_newname(client, dbuf, &b);
2739 rdataset = query_newrdataset(client);
2740 sigrdataset = query_newrdataset(client);
2741 if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2744 result = dns_db_find(db, name, version, dns_rdatatype_nsec, options,
2745 0, &node, fname, rdataset, sigrdataset);
2747 dns_db_detachnode(db, &node);
2748 if (result == DNS_R_NXDOMAIN) {
2750 result = dns_rdataset_first(rdataset);
2751 if (result == ISC_R_SUCCESS) {
2752 dns_rdataset_current(rdataset, &rdata);
2753 result = dns_rdata_tostruct(&rdata, &nsec, NULL);
2755 if (result == ISC_R_SUCCESS) {
2756 (void)dns_name_fullcompare(name, fname, &order,
2758 (void)dns_name_fullcompare(name, &nsec.next, &order,
2761 * Check for a pathological condition created when
2762 * serving some malformed signed zones and bail out.
2764 if (dns_name_countlabels(name) == nlabels)
2767 if (olabels > nlabels)
2768 dns_name_split(name, olabels, NULL, wname);
2770 dns_name_split(name, nlabels, NULL, wname);
2771 result = dns_name_concatenate(dns_wildcardname,
2772 wname, wname, NULL);
2773 if (result == ISC_R_SUCCESS)
2774 have_wname = ISC_TRUE;
2775 dns_rdata_freestruct(&nsec);
2777 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2778 dbuf, DNS_SECTION_AUTHORITY);
2780 if (rdataset != NULL)
2781 query_putrdataset(client, &rdataset);
2782 if (sigrdataset != NULL)
2783 query_putrdataset(client, &sigrdataset);
2785 query_releasename(client, &fname);
2787 ispositive = ISC_TRUE; /* prevent loop */
2788 if (!dns_name_equal(name, wname)) {
2794 if (rdataset != NULL)
2795 query_putrdataset(client, &rdataset);
2796 if (sigrdataset != NULL)
2797 query_putrdataset(client, &sigrdataset);
2799 query_releasename(client, &fname);
2803 query_addnxrrsetnsec(ns_client_t *client, dns_db_t *db,
2804 dns_dbversion_t *version, dns_name_t **namep,
2805 dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp)
2808 dns_rdataset_t *sigrdataset;
2809 dns_rdata_t sigrdata;
2810 dns_rdata_rrsig_t sig;
2811 unsigned int labels;
2812 isc_buffer_t *dbuf, b;
2814 isc_result_t result;
2817 if ((name->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
2818 query_addrrset(client, namep, rdatasetp, sigrdatasetp,
2819 NULL, DNS_SECTION_AUTHORITY);
2823 if (sigrdatasetp == NULL)
2825 sigrdataset = *sigrdatasetp;
2826 if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2828 result = dns_rdataset_first(sigrdataset);
2829 if (result != ISC_R_SUCCESS)
2831 dns_rdata_init(&sigrdata);
2832 dns_rdataset_current(sigrdataset, &sigrdata);
2833 result = dns_rdata_tostruct(&sigrdata, &sig, NULL);
2834 if (result != ISC_R_SUCCESS)
2837 labels = dns_name_countlabels(name);
2838 if ((unsigned int)sig.labels + 1 >= labels)
2842 query_addwildcardproof(client, db, version, client->query.qname,
2846 * We'll need some resources...
2848 dbuf = query_getnamebuf(client);
2851 fname = query_newname(client, dbuf, &b);
2854 dns_name_split(name, sig.labels + 1, NULL, fname);
2855 /* This will succeed, since we've stripped labels. */
2856 RUNTIME_CHECK(dns_name_concatenate(dns_wildcardname, fname, fname,
2857 NULL) == ISC_R_SUCCESS);
2858 query_addrrset(client, &fname, rdatasetp, sigrdatasetp,
2859 dbuf, DNS_SECTION_AUTHORITY);
2863 query_resume(isc_task_t *task, isc_event_t *event) {
2864 dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
2865 ns_client_t *client;
2866 isc_boolean_t fetch_cancelled, client_shuttingdown;
2869 * Resume a query after recursion.
2874 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
2875 client = devent->ev_arg;
2876 REQUIRE(NS_CLIENT_VALID(client));
2877 REQUIRE(task == client->task);
2878 REQUIRE(RECURSING(client));
2880 LOCK(&client->query.fetchlock);
2881 if (client->query.fetch != NULL) {
2883 * This is the fetch we've been waiting for.
2885 INSIST(devent->fetch == client->query.fetch);
2886 client->query.fetch = NULL;
2887 fetch_cancelled = ISC_FALSE;
2889 * Update client->now.
2891 isc_stdtime_get(&client->now);
2894 * This is a fetch completion event for a cancelled fetch.
2895 * Clean up and don't resume the find.
2897 fetch_cancelled = ISC_TRUE;
2899 UNLOCK(&client->query.fetchlock);
2900 INSIST(client->query.fetch == NULL);
2902 client->query.attributes &= ~NS_QUERYATTR_RECURSING;
2903 dns_resolver_destroyfetch(&devent->fetch);
2906 * If this client is shutting down, or this transaction
2907 * has timed out, do not resume the find.
2909 client_shuttingdown = ns_client_shuttingdown(client);
2910 if (fetch_cancelled || client_shuttingdown) {
2911 if (devent->node != NULL)
2912 dns_db_detachnode(devent->db, &devent->node);
2913 if (devent->db != NULL)
2914 dns_db_detach(&devent->db);
2915 query_putrdataset(client, &devent->rdataset);
2916 if (devent->sigrdataset != NULL)
2917 query_putrdataset(client, &devent->sigrdataset);
2918 isc_event_free(&event);
2919 if (fetch_cancelled)
2920 query_error(client, DNS_R_SERVFAIL);
2922 query_next(client, ISC_R_CANCELED);
2924 * This may destroy the client.
2926 ns_client_detach(&client);
2928 query_find(client, devent, 0);
2933 query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qdomain,
2934 dns_rdataset_t *nameservers, isc_boolean_t resuming)
2936 isc_result_t result;
2937 dns_rdataset_t *rdataset, *sigrdataset;
2938 isc_sockaddr_t *peeraddr;
2941 inc_stats(client, dns_statscounter_recursion);
2944 * We are about to recurse, which means that this client will
2945 * be unavailable for serving new requests for an indeterminate
2946 * amount of time. If this client is currently responsible
2947 * for handling incoming queries, set up a new client
2948 * object to handle them while we are waiting for a
2949 * response. There is no need to replace TCP clients
2950 * because those have already been replaced when the
2951 * connection was accepted (if allowed by the TCP quota).
2953 if (client->recursionquota == NULL) {
2954 result = isc_quota_attach(&ns_g_server->recursionquota,
2955 &client->recursionquota);
2956 if (result == ISC_R_SOFTQUOTA) {
2957 static isc_stdtime_t last = 0;
2959 isc_stdtime_get(&now);
2962 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2965 "recursive-clients soft limit "
2966 "exceeded, aborting oldest query");
2968 ns_client_killoldestquery(client);
2969 result = ISC_R_SUCCESS;
2970 } else if (result == ISC_R_QUOTA) {
2971 static isc_stdtime_t last = 0;
2973 isc_stdtime_get(&now);
2976 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2979 "no more recursive clients: %s",
2980 isc_result_totext(result));
2982 ns_client_killoldestquery(client);
2984 if (result == ISC_R_SUCCESS && !client->mortal &&
2985 (client->attributes & NS_CLIENTATTR_TCP) == 0) {
2986 result = ns_client_replace(client);
2987 if (result != ISC_R_SUCCESS) {
2988 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2991 "ns_client_replace() failed: %s",
2992 isc_result_totext(result));
2993 isc_quota_detach(&client->recursionquota);
2996 if (result != ISC_R_SUCCESS)
2998 ns_client_recursing(client);
3002 * Invoke the resolver.
3004 REQUIRE(nameservers == NULL || nameservers->type == dns_rdatatype_ns);
3005 REQUIRE(client->query.fetch == NULL);
3007 rdataset = query_newrdataset(client);
3008 if (rdataset == NULL)
3009 return (ISC_R_NOMEMORY);
3010 if (WANTDNSSEC(client)) {
3011 sigrdataset = query_newrdataset(client);
3012 if (sigrdataset == NULL) {
3013 query_putrdataset(client, &rdataset);
3014 return (ISC_R_NOMEMORY);
3019 if (client->query.timerset == ISC_FALSE)
3020 ns_client_settimeout(client, 60);
3021 if ((client->attributes & NS_CLIENTATTR_TCP) == 0)
3022 peeraddr = &client->peeraddr;
3025 result = dns_resolver_createfetch2(client->view->resolver,
3026 client->query.qname,
3027 qtype, qdomain, nameservers,
3028 NULL, peeraddr, client->message->id,
3029 client->query.fetchoptions,
3031 query_resume, client,
3032 rdataset, sigrdataset,
3033 &client->query.fetch);
3035 if (result == ISC_R_SUCCESS) {
3037 * Record that we're waiting for an event. A client which
3038 * is shutting down will not be destroyed until all the
3039 * events have been received.
3042 query_putrdataset(client, &rdataset);
3043 if (sigrdataset != NULL)
3044 query_putrdataset(client, &sigrdataset);
3050 #define MAX_RESTARTS 16
3052 #define QUERY_ERROR(r) \
3055 want_restart = ISC_FALSE; \
3059 * Extract a network address from the RDATA of an A or AAAA
3064 * ISC_R_NOTIMPLEMENTED The rdata is not a known address type.
3067 rdata_tonetaddr(const dns_rdata_t *rdata, isc_netaddr_t *netaddr) {
3069 struct in6_addr in6a;
3071 switch (rdata->type) {
3072 case dns_rdatatype_a:
3073 INSIST(rdata->length == 4);
3074 memcpy(&ina.s_addr, rdata->data, 4);
3075 isc_netaddr_fromin(netaddr, &ina);
3076 return (ISC_R_SUCCESS);
3077 case dns_rdatatype_aaaa:
3078 INSIST(rdata->length == 16);
3079 memcpy(in6a.s6_addr, rdata->data, 16);
3080 isc_netaddr_fromin6(netaddr, &in6a);
3081 return (ISC_R_SUCCESS);
3083 return (ISC_R_NOTIMPLEMENTED);
3088 * Find the sort order of 'rdata' in the topology-like
3089 * ACL forming the second element in a 2-element top-level
3090 * sortlist statement.
3093 query_sortlist_order_2element(const dns_rdata_t *rdata, const void *arg) {
3094 isc_netaddr_t netaddr;
3096 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3098 return (ns_sortlist_addrorder2(&netaddr, arg));
3102 * Find the sort order of 'rdata' in the matching element
3103 * of a 1-element top-level sortlist statement.
3106 query_sortlist_order_1element(const dns_rdata_t *rdata, const void *arg) {
3107 isc_netaddr_t netaddr;
3109 if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3111 return (ns_sortlist_addrorder1(&netaddr, arg));
3115 * Find the sortlist statement that applies to 'client' and set up
3116 * the sortlist info in in client->message appropriately.
3119 setup_query_sortlist(ns_client_t *client) {
3120 isc_netaddr_t netaddr;
3121 dns_rdatasetorderfunc_t order = NULL;
3122 const void *order_arg = NULL;
3124 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
3125 switch (ns_sortlist_setup(client->view->sortlist,
3126 &netaddr, &order_arg)) {
3127 case NS_SORTLISTTYPE_1ELEMENT:
3128 order = query_sortlist_order_1element;
3130 case NS_SORTLISTTYPE_2ELEMENT:
3131 order = query_sortlist_order_2element;
3133 case NS_SORTLISTTYPE_NONE:
3140 dns_message_setsortorder(client->message, order, order_arg);
3144 query_addnoqnameproof(ns_client_t *client, dns_rdataset_t *rdataset) {
3145 isc_buffer_t *dbuf, b;
3147 dns_rdataset_t *nsec, *nsecsig;
3148 isc_result_t result = ISC_R_NOMEMORY;
3150 CTRACE("query_addnoqnameproof");
3156 dbuf = query_getnamebuf(client);
3159 fname = query_newname(client, dbuf, &b);
3160 nsec = query_newrdataset(client);
3161 nsecsig = query_newrdataset(client);
3162 if (fname == NULL || nsec == NULL || nsecsig == NULL)
3165 result = dns_rdataset_getnoqname(rdataset, fname, nsec, nsecsig);
3166 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3168 query_addrrset(client, &fname, &nsec, &nsecsig, dbuf,
3169 DNS_SECTION_AUTHORITY);
3173 query_putrdataset(client, &nsec);
3174 if (nsecsig != NULL)
3175 query_putrdataset(client, &nsecsig);
3177 query_releasename(client, &fname);
3181 answer_in_glue(ns_client_t *client, dns_rdatatype_t qtype) {
3184 dns_section_t section = DNS_SECTION_ADDITIONAL;
3185 dns_rdataset_t *rdataset = NULL;
3187 msg = client->message;
3188 for (name = ISC_LIST_HEAD(msg->sections[section]);
3190 name = ISC_LIST_NEXT(name, link))
3191 if (dns_name_equal(name, client->query.qname)) {
3192 for (rdataset = ISC_LIST_HEAD(name->list);
3194 rdataset = ISC_LIST_NEXT(rdataset, link))
3195 if (rdataset->type == qtype)
3199 if (rdataset != NULL) {
3200 ISC_LIST_UNLINK(msg->sections[section], name, link);
3201 ISC_LIST_PREPEND(msg->sections[section], name, link);
3202 ISC_LIST_UNLINK(name->list, rdataset, link);
3203 ISC_LIST_PREPEND(name->list, rdataset, link);
3204 rdataset->attributes |= DNS_RDATASETATTR_REQUIREDGLUE;
3208 #define NS_NAME_INIT(A,B) \
3211 A, sizeof(A), sizeof(B), \
3212 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE, \
3213 B, NULL, { (void *)-1, (void *)-1}, \
3217 static unsigned char inaddr10_offsets[] = { 0, 3, 11, 16 };
3218 static unsigned char inaddr172_offsets[] = { 0, 3, 7, 15, 20 };
3219 static unsigned char inaddr192_offsets[] = { 0, 4, 8, 16, 21 };
3221 static unsigned char inaddr10[] = "\00210\007IN-ADDR\004ARPA";
3223 static unsigned char inaddr16172[] = "\00216\003172\007IN-ADDR\004ARPA";
3224 static unsigned char inaddr17172[] = "\00217\003172\007IN-ADDR\004ARPA";
3225 static unsigned char inaddr18172[] = "\00218\003172\007IN-ADDR\004ARPA";
3226 static unsigned char inaddr19172[] = "\00219\003172\007IN-ADDR\004ARPA";
3227 static unsigned char inaddr20172[] = "\00220\003172\007IN-ADDR\004ARPA";
3228 static unsigned char inaddr21172[] = "\00221\003172\007IN-ADDR\004ARPA";
3229 static unsigned char inaddr22172[] = "\00222\003172\007IN-ADDR\004ARPA";
3230 static unsigned char inaddr23172[] = "\00223\003172\007IN-ADDR\004ARPA";
3231 static unsigned char inaddr24172[] = "\00224\003172\007IN-ADDR\004ARPA";
3232 static unsigned char inaddr25172[] = "\00225\003172\007IN-ADDR\004ARPA";
3233 static unsigned char inaddr26172[] = "\00226\003172\007IN-ADDR\004ARPA";
3234 static unsigned char inaddr27172[] = "\00227\003172\007IN-ADDR\004ARPA";
3235 static unsigned char inaddr28172[] = "\00228\003172\007IN-ADDR\004ARPA";
3236 static unsigned char inaddr29172[] = "\00229\003172\007IN-ADDR\004ARPA";
3237 static unsigned char inaddr30172[] = "\00230\003172\007IN-ADDR\004ARPA";
3238 static unsigned char inaddr31172[] = "\00231\003172\007IN-ADDR\004ARPA";
3240 static unsigned char inaddr168192[] = "\003168\003192\007IN-ADDR\004ARPA";
3242 static dns_name_t rfc1918names[] = {
3243 NS_NAME_INIT(inaddr10, inaddr10_offsets),
3244 NS_NAME_INIT(inaddr16172, inaddr172_offsets),
3245 NS_NAME_INIT(inaddr17172, inaddr172_offsets),
3246 NS_NAME_INIT(inaddr18172, inaddr172_offsets),
3247 NS_NAME_INIT(inaddr19172, inaddr172_offsets),
3248 NS_NAME_INIT(inaddr20172, inaddr172_offsets),
3249 NS_NAME_INIT(inaddr21172, inaddr172_offsets),
3250 NS_NAME_INIT(inaddr22172, inaddr172_offsets),
3251 NS_NAME_INIT(inaddr23172, inaddr172_offsets),
3252 NS_NAME_INIT(inaddr24172, inaddr172_offsets),
3253 NS_NAME_INIT(inaddr25172, inaddr172_offsets),
3254 NS_NAME_INIT(inaddr26172, inaddr172_offsets),
3255 NS_NAME_INIT(inaddr27172, inaddr172_offsets),
3256 NS_NAME_INIT(inaddr28172, inaddr172_offsets),
3257 NS_NAME_INIT(inaddr29172, inaddr172_offsets),
3258 NS_NAME_INIT(inaddr30172, inaddr172_offsets),
3259 NS_NAME_INIT(inaddr31172, inaddr172_offsets),
3260 NS_NAME_INIT(inaddr168192, inaddr192_offsets)
3264 static unsigned char prisoner_data[] = "\010prisoner\004iana\003org";
3265 static unsigned char hostmaster_data[] = "\012hostmaster\014root-servers\003org";
3267 static unsigned char prisoner_offsets[] = { 0, 9, 14, 18 };
3268 static unsigned char hostmaster_offsets[] = { 0, 11, 24, 28 };
3270 static dns_name_t prisoner = NS_NAME_INIT(prisoner_data, prisoner_offsets);
3271 static dns_name_t hostmaster = NS_NAME_INIT(hostmaster_data, hostmaster_offsets);
3274 warn_rfc1918(ns_client_t *client, dns_name_t *fname, dns_rdataset_t *rdataset) {
3276 dns_rdata_t rdata = DNS_RDATA_INIT;
3277 dns_rdata_soa_t soa;
3278 dns_rdataset_t found;
3279 isc_result_t result;
3281 for (i = 0; i < (sizeof(rfc1918names)/sizeof(*rfc1918names)); i++) {
3282 if (dns_name_issubdomain(fname, &rfc1918names[i])) {
3283 dns_rdataset_init(&found);
3284 result = dns_ncache_getrdataset(rdataset,
3288 if (result != ISC_R_SUCCESS)
3291 result = dns_rdataset_first(&found);
3292 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3293 dns_rdataset_current(&found, &rdata);
3294 result = dns_rdata_tostruct(&rdata, &soa, NULL);
3295 if (result != ISC_R_SUCCESS)
3297 if (dns_name_equal(&soa.origin, &prisoner) &&
3298 dns_name_equal(&soa.contact, &hostmaster)) {
3299 char buf[DNS_NAME_FORMATSIZE];
3300 dns_name_format(fname, buf, sizeof(buf));
3301 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3304 "RFC 1918 response from "
3305 "Internet for %s", buf);
3307 dns_rdataset_disassociate(&found);
3314 * Do the bulk of query processing for the current query of 'client'.
3315 * If 'event' is non-NULL, we are returning from recursion and 'qtype'
3316 * is ignored. Otherwise, 'qtype' is the query type.
3319 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
3323 dns_rdatatype_t type;
3324 dns_name_t *fname, *zfname, *tname, *prefix;
3325 dns_rdataset_t *rdataset, *trdataset;
3326 dns_rdataset_t *sigrdataset, *zrdataset, *zsigrdataset;
3327 dns_rdataset_t **sigrdatasetp;
3328 dns_rdata_t rdata = DNS_RDATA_INIT;
3329 dns_rdatasetiter_t *rdsiter;
3330 isc_boolean_t want_restart, authoritative, is_zone, need_wildcardproof;
3331 unsigned int n, nlabels;
3332 dns_namereln_t namereln;
3336 isc_result_t result, eresult;
3337 dns_fixedname_t fixed;
3338 dns_fixedname_t wildcardname;
3339 dns_dbversion_t *version;
3341 dns_rdata_cname_t cname;
3342 dns_rdata_dname_t dname;
3343 unsigned int options;
3344 isc_boolean_t empty_wild;
3345 dns_rdataset_t *noqname;
3346 isc_boolean_t resuming;
3348 CTRACE("query_find");
3351 * One-time initialization.
3353 * It's especially important to initialize anything that the cleanup
3354 * code might cleanup.
3357 eresult = ISC_R_SUCCESS;
3363 zsigrdataset = NULL;
3369 need_wildcardproof = ISC_FALSE;
3370 empty_wild = ISC_FALSE;
3372 resuming = ISC_FALSE;
3373 is_zone = ISC_FALSE;
3375 if (event != NULL) {
3377 * We're returning from recursion. Restore the query context
3381 want_restart = ISC_FALSE;
3382 authoritative = ISC_FALSE;
3384 qtype = event->qtype;
3385 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3386 type = dns_rdatatype_any;
3391 rdataset = event->rdataset;
3392 sigrdataset = event->sigrdataset;
3395 * We'll need some resources...
3397 dbuf = query_getnamebuf(client);
3399 QUERY_ERROR(DNS_R_SERVFAIL);
3402 fname = query_newname(client, dbuf, &b);
3403 if (fname == NULL) {
3404 QUERY_ERROR(DNS_R_SERVFAIL);
3407 tname = dns_fixedname_name(&event->foundname);
3408 result = dns_name_copy(tname, fname, NULL);
3409 if (result != ISC_R_SUCCESS) {
3410 QUERY_ERROR(DNS_R_SERVFAIL);
3414 result = event->result;
3415 resuming = ISC_TRUE;
3421 * Not returning from recursion.
3425 * If it's a SIG query, we'll iterate the node.
3427 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3428 type = dns_rdatatype_any;
3433 CTRACE("query_find: restart");
3434 want_restart = ISC_FALSE;
3435 authoritative = ISC_FALSE;
3437 need_wildcardproof = ISC_FALSE;
3439 if (client->view->checknames &&
3440 !dns_rdata_checkowner(client->query.qname,
3441 client->message->rdclass,
3442 qtype, ISC_FALSE)) {
3443 char namebuf[DNS_NAME_FORMATSIZE];
3444 char typename[DNS_RDATATYPE_FORMATSIZE];
3445 char classname[DNS_RDATACLASS_FORMATSIZE];
3447 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
3448 dns_rdatatype_format(qtype, typename, sizeof(typename));
3449 dns_rdataclass_format(client->message->rdclass, classname,
3451 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3452 NS_LOGMODULE_QUERY, ISC_LOG_ERROR,
3453 "check-names failure %s/%s/%s", namebuf,
3454 typename, classname);
3455 QUERY_ERROR(DNS_R_REFUSED);
3460 * First we must find the right database.
3462 options &= DNS_GETDB_NOLOG; /* Preserve DNS_GETDB_NOLOG. */
3463 if (dns_rdatatype_atparent(qtype) &&
3464 !dns_name_equal(client->query.qname, dns_rootname))
3465 options |= DNS_GETDB_NOEXACT;
3466 result = query_getdb(client, client->query.qname, qtype, options,
3467 &zone, &db, &version, &is_zone);
3468 if ((result != ISC_R_SUCCESS || !is_zone) && !RECURSIONOK(client) &&
3469 (options & DNS_GETDB_NOEXACT) != 0 && qtype == dns_rdatatype_ds) {
3471 * Look to see if we are authoritative for the
3472 * child zone if the query type is DS.
3474 dns_db_t *tdb = NULL;
3475 dns_zone_t *tzone = NULL;
3476 dns_dbversion_t *tversion = NULL;
3477 isc_result_t tresult;
3479 tresult = query_getzonedb(client, client->query.qname, qtype,
3480 DNS_GETDB_PARTIAL, &tzone, &tdb,
3482 if (tresult == ISC_R_SUCCESS) {
3483 options &= ~DNS_GETDB_NOEXACT;
3484 query_putrdataset(client, &rdataset);
3488 dns_zone_detach(&zone);
3493 result = ISC_R_SUCCESS;
3496 dns_db_detach(&tdb);
3498 dns_zone_detach(&tzone);
3501 if (result != ISC_R_SUCCESS) {
3502 if (result == DNS_R_REFUSED) {
3503 if (!PARTIALANSWER(client))
3504 QUERY_ERROR(DNS_R_REFUSED);
3506 QUERY_ERROR(DNS_R_SERVFAIL);
3511 authoritative = ISC_TRUE;
3513 if (event == NULL && client->query.restarts == 0) {
3518 * if is_zone = true, zone = NULL then this is
3519 * a DLZ zone. Don't attempt to attach zone.
3522 dns_zone_attach(zone, &client->query.authzone);
3526 dns_db_attach(db, &client->query.authdb);
3528 client->query.authdbset = ISC_TRUE;
3532 CTRACE("query_find: db_find");
3534 * We'll need some resources...
3536 dbuf = query_getnamebuf(client);
3538 QUERY_ERROR(DNS_R_SERVFAIL);
3541 fname = query_newname(client, dbuf, &b);
3542 rdataset = query_newrdataset(client);
3543 if (fname == NULL || rdataset == NULL) {
3544 QUERY_ERROR(DNS_R_SERVFAIL);
3547 if (WANTDNSSEC(client)) {
3548 sigrdataset = query_newrdataset(client);
3549 if (sigrdataset == NULL) {
3550 QUERY_ERROR(DNS_R_SERVFAIL);
3556 * Now look for an answer in the database.
3558 result = dns_db_find(db, client->query.qname, version, type,
3559 client->query.dboptions, client->now,
3560 &node, fname, rdataset, sigrdataset);
3563 CTRACE("query_find: resume");
3567 * This case is handled in the main line below.
3573 * These cases are handled in the main line below.
3576 authoritative = ISC_FALSE;
3578 case ISC_R_NOTFOUND:
3580 * The cache doesn't even have the root NS. Get them from
3587 if (client->view->hints == NULL) {
3588 /* We have no hints. */
3589 result = ISC_R_FAILURE;
3591 dns_db_attach(client->view->hints, &db);
3592 result = dns_db_find(db, dns_rootname,
3593 NULL, dns_rdatatype_ns,
3594 0, client->now, &node, fname,
3595 rdataset, sigrdataset);
3597 if (result != ISC_R_SUCCESS) {
3599 * Nonsensical root hints may require cleanup.
3601 if (dns_rdataset_isassociated(rdataset))
3602 dns_rdataset_disassociate(rdataset);
3603 if (sigrdataset != NULL &&
3604 dns_rdataset_isassociated(sigrdataset))
3605 dns_rdataset_disassociate(sigrdataset);
3607 dns_db_detachnode(db, &node);
3610 * We don't have any root server hints, but
3611 * we may have working forwarders, so try to
3614 if (RECURSIONOK(client)) {
3615 result = query_recurse(client, qtype,
3616 NULL, NULL, resuming);
3617 if (result == ISC_R_SUCCESS)
3618 client->query.attributes |=
3619 NS_QUERYATTR_RECURSING;
3620 else if (result == DNS_R_DUPLICATE ||
3621 result == DNS_R_DROP) {
3622 /* Duplicate query. */
3623 QUERY_ERROR(result);
3625 /* Unable to recurse. */
3626 QUERY_ERROR(DNS_R_SERVFAIL);
3630 /* Unable to give root server referral. */
3631 QUERY_ERROR(DNS_R_SERVFAIL);
3636 * XXXRTH We should trigger root server priming here.
3639 case DNS_R_DELEGATION:
3640 authoritative = ISC_FALSE;
3643 * Look to see if we are authoritative for the
3644 * child zone if the query type is DS.
3646 if (!RECURSIONOK(client) &&
3647 (options & DNS_GETDB_NOEXACT) != 0 &&
3648 qtype == dns_rdatatype_ds) {
3649 dns_db_t *tdb = NULL;
3650 dns_zone_t *tzone = NULL;
3651 dns_dbversion_t *tversion = NULL;
3652 result = query_getzonedb(client,
3653 client->query.qname,
3658 if (result == ISC_R_SUCCESS) {
3659 options &= ~DNS_GETDB_NOEXACT;
3660 query_putrdataset(client, &rdataset);
3661 if (sigrdataset != NULL)
3662 query_putrdataset(client,
3665 query_releasename(client,
3668 dns_db_detachnode(db, &node);
3672 dns_zone_detach(&zone);
3676 authoritative = ISC_TRUE;
3680 dns_db_detach(&tdb);
3682 dns_zone_detach(&tzone);
3685 * We're authoritative for an ancestor of QNAME.
3687 if (!USECACHE(client) || !RECURSIONOK(client)) {
3689 * If we don't have a cache, this is the best
3692 * If the client is making a nonrecursive
3693 * query we always give out the authoritative
3694 * delegation. This way even if we get
3695 * junk in our cache, we won't fail in our
3696 * role as the delegating authority if another
3697 * nameserver asks us about a delegated
3700 * We enable the retrieval of glue for this
3701 * database by setting client->query.gluedb.
3703 client->query.gluedb = db;
3704 client->query.isreferral = ISC_TRUE;
3706 * We must ensure NOADDITIONAL is off,
3707 * because the generation of
3708 * additional data is required in
3711 client->query.attributes &=
3712 ~NS_QUERYATTR_NOADDITIONAL;
3713 if (sigrdataset != NULL)
3714 sigrdatasetp = &sigrdataset;
3716 sigrdatasetp = NULL;
3717 query_addrrset(client, &fname,
3718 &rdataset, sigrdatasetp,
3719 dbuf, DNS_SECTION_AUTHORITY);
3720 client->query.gluedb = NULL;
3721 if (WANTDNSSEC(client) && dns_db_issecure(db))
3722 query_addds(client, db, node, version);
3725 * We might have a better answer or delegation
3726 * in the cache. We'll remember the current
3727 * values of fname, rdataset, and sigrdataset.
3728 * We'll then go looking for QNAME in the
3729 * cache. If we find something better, we'll
3732 query_keepname(client, fname, dbuf);
3736 zrdataset = rdataset;
3738 zsigrdataset = sigrdataset;
3740 dns_db_detachnode(db, &node);
3743 dns_db_attach(client->view->cachedb, &db);
3744 is_zone = ISC_FALSE;
3748 if (zfname != NULL &&
3749 !dns_name_issubdomain(fname, zfname)) {
3751 * We've already got a delegation from
3752 * authoritative data, and it is better
3753 * than what we found in the cache. Use
3754 * it instead of the cache delegation.
3756 query_releasename(client, &fname);
3760 * We've already done query_keepname() on
3761 * zfname, so we must set dbuf to NULL to
3762 * prevent query_addrrset() from trying to
3763 * call query_keepname() again.
3766 query_putrdataset(client, &rdataset);
3767 if (sigrdataset != NULL)
3768 query_putrdataset(client,
3770 rdataset = zrdataset;
3772 sigrdataset = zsigrdataset;
3773 zsigrdataset = NULL;
3775 * We don't clean up zdb here because we
3776 * may still need it. It will get cleaned
3777 * up by the main cleanup code.
3781 if (RECURSIONOK(client)) {
3785 if (dns_rdatatype_atparent(type))
3786 result = query_recurse(client, qtype,
3790 result = query_recurse(client, qtype,
3793 if (result == ISC_R_SUCCESS)
3794 client->query.attributes |=
3795 NS_QUERYATTR_RECURSING;
3796 else if (result == DNS_R_DUPLICATE ||
3797 result == DNS_R_DROP)
3798 QUERY_ERROR(result);
3800 QUERY_ERROR(DNS_R_SERVFAIL);
3803 * This is the best answer.
3805 client->query.attributes |=
3806 NS_QUERYATTR_CACHEGLUEOK;
3807 client->query.gluedb = zdb;
3808 client->query.isreferral = ISC_TRUE;
3810 * We must ensure NOADDITIONAL is off,
3811 * because the generation of
3812 * additional data is required in
3815 client->query.attributes &=
3816 ~NS_QUERYATTR_NOADDITIONAL;
3817 if (sigrdataset != NULL)
3818 sigrdatasetp = &sigrdataset;
3820 sigrdatasetp = NULL;
3821 query_addrrset(client, &fname,
3822 &rdataset, sigrdatasetp,
3823 dbuf, DNS_SECTION_AUTHORITY);
3824 client->query.gluedb = NULL;
3825 client->query.attributes &=
3826 ~NS_QUERYATTR_CACHEGLUEOK;
3827 if (WANTDNSSEC(client))
3828 query_addds(client, db, node, version);
3832 case DNS_R_EMPTYNAME:
3833 result = DNS_R_NXRRSET;
3837 if (dns_rdataset_isassociated(rdataset)) {
3839 * If we've got a NSEC record, we need to save the
3840 * name now because we're going call query_addsoa()
3841 * below, and it needs to use the name buffer.
3843 query_keepname(client, fname, dbuf);
3846 * We're not going to use fname, and need to release
3847 * our hold on the name buffer so query_addsoa()
3850 query_releasename(client, &fname);
3855 result = query_addsoa(client, db, version, ISC_FALSE);
3856 if (result != ISC_R_SUCCESS) {
3857 QUERY_ERROR(result);
3861 * Add NSEC record if we found one.
3863 if (WANTDNSSEC(client)) {
3864 if (dns_rdataset_isassociated(rdataset))
3865 query_addnxrrsetnsec(client, db, version,
3870 case DNS_R_EMPTYWILD:
3871 empty_wild = ISC_TRUE;
3873 case DNS_R_NXDOMAIN:
3875 if (dns_rdataset_isassociated(rdataset)) {
3877 * If we've got a NSEC record, we need to save the
3878 * name now because we're going call query_addsoa()
3879 * below, and it needs to use the name buffer.
3881 query_keepname(client, fname, dbuf);
3884 * We're not going to use fname, and need to release
3885 * our hold on the name buffer so query_addsoa()
3888 query_releasename(client, &fname);
3891 * Add SOA. If the query was for a SOA record force the
3892 * ttl to zero so that it is possible for clients to find
3893 * the containing zone of an arbitrary name with a stub
3894 * resolver and not have it cached.
3896 if (qtype == dns_rdatatype_soa &&
3900 dns_zone_getzeronosoattl(zone))
3901 result = query_addsoa(client, db, version, ISC_TRUE);
3903 result = query_addsoa(client, db, version, ISC_FALSE);
3904 if (result != ISC_R_SUCCESS) {
3905 QUERY_ERROR(result);
3909 * Add NSEC record if we found one.
3911 if (dns_rdataset_isassociated(rdataset)) {
3912 if (WANTDNSSEC(client)) {
3913 query_addrrset(client, &fname, &rdataset,
3915 NULL, DNS_SECTION_AUTHORITY);
3916 query_addwildcardproof(client, db, version,
3917 client->query.qname,
3922 * Set message rcode.
3925 client->message->rcode = dns_rcode_noerror;
3927 client->message->rcode = dns_rcode_nxdomain;
3929 case DNS_R_NCACHENXDOMAIN:
3930 case DNS_R_NCACHENXRRSET:
3932 authoritative = ISC_FALSE;
3934 * Set message rcode, if required.
3936 if (result == DNS_R_NCACHENXDOMAIN)
3937 client->message->rcode = dns_rcode_nxdomain;
3939 * Look for RFC 1918 leakage from Internet.
3941 if (result == DNS_R_NCACHENXDOMAIN &&
3942 qtype == dns_rdatatype_ptr &&
3943 client->message->rdclass == dns_rdataclass_in &&
3944 dns_name_countlabels(fname) == 7)
3945 warn_rfc1918(client, fname, rdataset);
3947 * We don't call query_addrrset() because we don't need any
3948 * of its extra features (and things would probably break!).
3950 query_keepname(client, fname, dbuf);
3951 dns_message_addname(client->message, fname,
3952 DNS_SECTION_AUTHORITY);
3953 ISC_LIST_APPEND(fname->list, rdataset, link);
3959 * Keep a copy of the rdataset. We have to do this because
3960 * query_addrrset may clear 'rdataset' (to prevent the
3961 * cleanup code from cleaning it up).
3963 trdataset = rdataset;
3965 * Add the CNAME to the answer section.
3967 if (sigrdataset != NULL)
3968 sigrdatasetp = &sigrdataset;
3970 sigrdatasetp = NULL;
3971 if (WANTDNSSEC(client) &&
3972 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
3974 dns_fixedname_init(&wildcardname);
3975 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
3977 need_wildcardproof = ISC_TRUE;
3979 if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0 &&
3984 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
3985 DNS_SECTION_ANSWER);
3986 if (noqname != NULL)
3987 query_addnoqnameproof(client, noqname);
3989 * We set the PARTIALANSWER attribute so that if anything goes
3990 * wrong later on, we'll return what we've got so far.
3992 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
3994 * Reset qname to be the target name of the CNAME and restart
3998 result = dns_message_gettempname(client->message, &tname);
3999 if (result != ISC_R_SUCCESS)
4001 result = dns_rdataset_first(trdataset);
4002 if (result != ISC_R_SUCCESS) {
4003 dns_message_puttempname(client->message, &tname);
4006 dns_rdataset_current(trdataset, &rdata);
4007 result = dns_rdata_tostruct(&rdata, &cname, NULL);
4008 dns_rdata_reset(&rdata);
4009 if (result != ISC_R_SUCCESS) {
4010 dns_message_puttempname(client->message, &tname);
4013 dns_name_init(tname, NULL);
4014 result = dns_name_dup(&cname.cname, client->mctx, tname);
4015 if (result != ISC_R_SUCCESS) {
4016 dns_message_puttempname(client->message, &tname);
4017 dns_rdata_freestruct(&cname);
4020 dns_rdata_freestruct(&cname);
4021 ns_client_qnamereplace(client, tname);
4022 want_restart = ISC_TRUE;
4023 if (!WANTRECURSION(client))
4024 options |= DNS_GETDB_NOLOG;
4028 * Compare the current qname to the found name. We need
4029 * to know how many labels and bits are in common because
4030 * we're going to have to split qname later on.
4032 namereln = dns_name_fullcompare(client->query.qname, fname,
4034 INSIST(namereln == dns_namereln_subdomain);
4036 * Keep a copy of the rdataset. We have to do this because
4037 * query_addrrset may clear 'rdataset' (to prevent the
4038 * cleanup code from cleaning it up).
4040 trdataset = rdataset;
4042 * Add the DNAME to the answer section.
4044 if (sigrdataset != NULL)
4045 sigrdatasetp = &sigrdataset;
4047 sigrdatasetp = NULL;
4048 if (WANTDNSSEC(client) &&
4049 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4051 dns_fixedname_init(&wildcardname);
4052 dns_name_copy(fname, dns_fixedname_name(&wildcardname),
4054 need_wildcardproof = ISC_TRUE;
4056 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4057 DNS_SECTION_ANSWER);
4059 * We set the PARTIALANSWER attribute so that if anything goes
4060 * wrong later on, we'll return what we've got so far.
4062 client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
4064 * Get the target name of the DNAME.
4067 result = dns_message_gettempname(client->message, &tname);
4068 if (result != ISC_R_SUCCESS)
4070 result = dns_rdataset_first(trdataset);
4071 if (result != ISC_R_SUCCESS) {
4072 dns_message_puttempname(client->message, &tname);
4075 dns_rdataset_current(trdataset, &rdata);
4076 result = dns_rdata_tostruct(&rdata, &dname, NULL);
4077 dns_rdata_reset(&rdata);
4078 if (result != ISC_R_SUCCESS) {
4079 dns_message_puttempname(client->message, &tname);
4082 dns_name_init(tname, NULL);
4083 dns_name_clone(&dname.dname, tname);
4084 dns_rdata_freestruct(&dname);
4086 * Construct the new qname.
4088 dns_fixedname_init(&fixed);
4089 prefix = dns_fixedname_name(&fixed);
4090 dns_name_split(client->query.qname, nlabels, prefix, NULL);
4091 INSIST(fname == NULL);
4092 dbuf = query_getnamebuf(client);
4094 dns_message_puttempname(client->message, &tname);
4097 fname = query_newname(client, dbuf, &b);
4098 if (fname == NULL) {
4099 dns_message_puttempname(client->message, &tname);
4102 result = dns_name_concatenate(prefix, tname, fname, NULL);
4103 if (result != ISC_R_SUCCESS) {
4104 dns_message_puttempname(client->message, &tname);
4105 if (result == ISC_R_NOSPACE) {
4107 * RFC2672, section 4.1, subsection 3c says
4108 * we should return YXDOMAIN if the constructed
4109 * name would be too long.
4111 client->message->rcode = dns_rcode_yxdomain;
4115 query_keepname(client, fname, dbuf);
4117 * Synthesize a CNAME for this DNAME.
4119 * We want to synthesize a CNAME since if we don't
4120 * then older software that doesn't understand DNAME
4121 * will not chain like it should.
4123 * We do not try to synthesize a signature because we hope
4124 * that security aware servers will understand DNAME. Also,
4125 * even if we had an online key, making a signature
4126 * on-the-fly is costly, and not really legitimate anyway
4127 * since the synthesized CNAME is NOT in the zone.
4129 dns_name_init(tname, NULL);
4130 (void)query_addcnamelike(client, client->query.qname, fname,
4131 trdataset->trust, &tname,
4132 dns_rdatatype_cname);
4134 dns_message_puttempname(client->message, &tname);
4136 * Switch to the new qname and restart.
4138 ns_client_qnamereplace(client, fname);
4140 want_restart = ISC_TRUE;
4141 if (!WANTRECURSION(client))
4142 options |= DNS_GETDB_NOLOG;
4146 * Something has gone wrong.
4148 QUERY_ERROR(DNS_R_SERVFAIL);
4152 if (WANTDNSSEC(client) &&
4153 (fname->attributes & DNS_NAMEATTR_WILDCARD) != 0)
4155 dns_fixedname_init(&wildcardname);
4156 dns_name_copy(fname, dns_fixedname_name(&wildcardname), NULL);
4157 need_wildcardproof = ISC_TRUE;
4160 if (type == dns_rdatatype_any) {
4162 * XXXRTH Need to handle zonecuts with special case
4167 result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
4168 if (result != ISC_R_SUCCESS) {
4169 QUERY_ERROR(DNS_R_SERVFAIL);
4173 * Calling query_addrrset() with a non-NULL dbuf is going
4174 * to either keep or release the name. We don't want it to
4175 * release fname, since we may have to call query_addrrset()
4176 * more than once. That means we have to call query_keepname()
4177 * now, and pass a NULL dbuf to query_addrrset().
4179 * If we do a query_addrrset() below, we must set fname to
4180 * NULL before leaving this block, otherwise we might try to
4181 * cleanup fname even though we're using it!
4183 query_keepname(client, fname, dbuf);
4185 result = dns_rdatasetiter_first(rdsiter);
4186 while (result == ISC_R_SUCCESS) {
4187 dns_rdatasetiter_current(rdsiter, rdataset);
4188 if ((qtype == dns_rdatatype_any ||
4189 rdataset->type == qtype) && rdataset->type != 0) {
4190 query_addrrset(client,
4191 fname != NULL ? &fname : &tname,
4193 NULL, DNS_SECTION_ANSWER);
4195 INSIST(tname != NULL);
4197 * rdataset is non-NULL only in certain pathological
4198 * cases involving DNAMEs.
4200 if (rdataset != NULL)
4201 query_putrdataset(client, &rdataset);
4202 rdataset = query_newrdataset(client);
4203 if (rdataset == NULL)
4207 * We're not interested in this rdataset.
4209 dns_rdataset_disassociate(rdataset);
4211 result = dns_rdatasetiter_next(rdsiter);
4215 dns_message_puttempname(client->message, &fname);
4219 * We didn't match any rdatasets.
4221 if (qtype == dns_rdatatype_rrsig &&
4222 result == ISC_R_NOMORE) {
4224 * XXXRTH If this is a secure zone and we
4225 * didn't find any SIGs, we should generate
4226 * an error unless we were searching for
4230 authoritative = ISC_FALSE;
4231 dns_rdatasetiter_destroy(&rdsiter);
4232 if (RECURSIONOK(client)) {
4233 result = query_recurse(client,
4238 if (result == ISC_R_SUCCESS)
4239 client->query.attributes |=
4240 NS_QUERYATTR_RECURSING;
4242 QUERY_ERROR(DNS_R_SERVFAIL); }
4246 * We were searching for SIG records in
4247 * a nonsecure zone. Send a "no error,
4248 * no data" response.
4253 result = query_addsoa(client, db, version,
4255 if (result == ISC_R_SUCCESS)
4256 result = ISC_R_NOMORE;
4259 * Something went wrong.
4261 result = DNS_R_SERVFAIL;
4264 dns_rdatasetiter_destroy(&rdsiter);
4265 if (result != ISC_R_NOMORE) {
4266 QUERY_ERROR(DNS_R_SERVFAIL);
4271 * This is the "normal" case -- an ordinary question to which
4272 * we know the answer.
4274 if (sigrdataset != NULL)
4275 sigrdatasetp = &sigrdataset;
4277 sigrdatasetp = NULL;
4278 if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0 &&
4284 * BIND 8 priming queries need the additional section.
4286 if (is_zone && qtype == dns_rdatatype_ns &&
4287 dns_name_equal(client->query.qname, dns_rootname))
4288 client->query.attributes &= ~NS_QUERYATTR_NOADDITIONAL;
4290 query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
4291 DNS_SECTION_ANSWER);
4292 if (noqname != NULL)
4293 query_addnoqnameproof(client, noqname);
4295 * We shouldn't ever fail to add 'rdataset'
4296 * because it's already in the answer.
4298 INSIST(rdataset == NULL);
4302 CTRACE("query_find: addauth");
4304 * Add NS records to the authority section (if we haven't already
4305 * added them to the answer section).
4307 if (!want_restart && !NOAUTHORITY(client)) {
4309 if (!((qtype == dns_rdatatype_ns ||
4310 qtype == dns_rdatatype_any) &&
4311 dns_name_equal(client->query.qname,
4312 dns_db_origin(db))))
4313 (void)query_addns(client, db, version);
4314 } else if (qtype != dns_rdatatype_ns) {
4316 query_releasename(client, &fname);
4317 query_addbestns(client);
4322 * Add NSEC records to the authority section if they're needed for
4323 * DNSSEC wildcard proofs.
4325 if (need_wildcardproof && dns_db_issecure(db))
4326 query_addwildcardproof(client, db, version,
4327 dns_fixedname_name(&wildcardname),
4330 CTRACE("query_find: cleanup");
4334 if (rdataset != NULL)
4335 query_putrdataset(client, &rdataset);
4336 if (sigrdataset != NULL)
4337 query_putrdataset(client, &sigrdataset);
4339 query_releasename(client, &fname);
4341 dns_db_detachnode(db, &node);
4345 dns_zone_detach(&zone);
4347 query_putrdataset(client, &zrdataset);
4348 if (zsigrdataset != NULL)
4349 query_putrdataset(client, &zsigrdataset);
4351 query_releasename(client, &zfname);
4352 dns_db_detach(&zdb);
4355 isc_event_free(ISC_EVENT_PTR(&event));
4360 if (client->query.restarts == 0 && !authoritative) {
4362 * We're not authoritative, so we must ensure the AA bit
4365 client->message->flags &= ~DNS_MESSAGEFLAG_AA;
4369 * Restart the query?
4371 if (want_restart && client->query.restarts < MAX_RESTARTS) {
4372 client->query.restarts++;
4376 if (eresult != ISC_R_SUCCESS &&
4377 (!PARTIALANSWER(client) || WANTRECURSION(client))) {
4378 if (eresult == DNS_R_DUPLICATE || eresult == DNS_R_DROP) {
4380 * This was a duplicate query that we are
4381 * recursing on. Don't send a response now.
4382 * The original query will still cause a response.
4384 query_next(client, eresult);
4387 * If we don't have any answer to give the client,
4388 * or if the client requested recursion and thus wanted
4389 * the complete answer, send an error response.
4391 query_error(client, eresult);
4393 ns_client_detach(&client);
4394 } else if (!RECURSING(client)) {
4396 * We are done. Set up sortlist data for the message
4397 * rendering code, make a final tweak to the AA bit if the
4398 * auth-nxdomain config option says so, then render and
4399 * send the response.
4401 setup_query_sortlist(client);
4404 * If this is a referral and the answer to the question
4405 * is in the glue sort it to the start of the additional
4408 if (client->message->counts[DNS_SECTION_ANSWER] == 0 &&
4409 client->message->rcode == dns_rcode_noerror &&
4410 (qtype == dns_rdatatype_a || qtype == dns_rdatatype_aaaa))
4411 answer_in_glue(client, qtype);
4413 if (client->message->rcode == dns_rcode_nxdomain &&
4414 client->view->auth_nxdomain == ISC_TRUE)
4415 client->message->flags |= DNS_MESSAGEFLAG_AA;
4418 ns_client_detach(&client);
4420 CTRACE("query_find: done");
4424 log_query(ns_client_t *client) {
4425 char namebuf[DNS_NAME_FORMATSIZE];
4426 char typename[DNS_RDATATYPE_FORMATSIZE];
4427 char classname[DNS_RDATACLASS_FORMATSIZE];
4428 dns_rdataset_t *rdataset;
4429 int level = ISC_LOG_INFO;
4431 if (! isc_log_wouldlog(ns_g_lctx, level))
4434 rdataset = ISC_LIST_HEAD(client->query.qname->list);
4435 INSIST(rdataset != NULL);
4436 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
4437 dns_rdataclass_format(rdataset->rdclass, classname, sizeof(classname));
4438 dns_rdatatype_format(rdataset->type, typename, sizeof(typename));
4440 ns_client_log(client, NS_LOGCATEGORY_QUERIES, NS_LOGMODULE_QUERY,
4441 level, "query: %s %s %s %s%s%s", namebuf, classname,
4442 typename, WANTRECURSION(client) ? "+" : "-",
4443 (client->signer != NULL) ? "S": "",
4444 (client->opt != NULL) ? "E" : "");
4448 ns_query_start(ns_client_t *client) {
4449 isc_result_t result;
4450 dns_message_t *message = client->message;
4451 dns_rdataset_t *rdataset;
4452 ns_client_t *qclient;
4453 dns_rdatatype_t qtype;
4454 isc_boolean_t want_ad;
4456 CTRACE("ns_query_start");
4459 * Ensure that appropriate cleanups occur.
4461 client->next = query_next_callback;
4464 * Behave as if we don't support DNSSEC if not enabled.
4466 if (!client->view->enablednssec) {
4467 message->flags &= ~DNS_MESSAGEFLAG_CD;
4468 client->extflags &= ~DNS_MESSAGEEXTFLAG_DO;
4469 if (client->opt != NULL)
4470 client->opt->ttl &= ~DNS_MESSAGEEXTFLAG_DO;
4473 if ((message->flags & DNS_MESSAGEFLAG_RD) != 0)
4474 client->query.attributes |= NS_QUERYATTR_WANTRECURSION;
4476 if ((client->extflags & DNS_MESSAGEEXTFLAG_DO) != 0)
4477 client->attributes |= NS_CLIENTATTR_WANTDNSSEC;
4479 if (client->view->minimalresponses)
4480 client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
4481 NS_QUERYATTR_NOADDITIONAL);
4483 if ((client->view->cachedb == NULL)
4484 || (!client->view->additionalfromcache)) {
4486 * We don't have a cache. Turn off cache support and
4489 client->query.attributes &=
4490 ~(NS_QUERYATTR_RECURSIONOK|NS_QUERYATTR_CACHEOK);
4491 } else if ((client->attributes & NS_CLIENTATTR_RA) == 0 ||
4492 (message->flags & DNS_MESSAGEFLAG_RD) == 0) {
4494 * If the client isn't allowed to recurse (due to
4495 * "recursion no", the allow-recursion ACL, or the
4496 * lack of a resolver in this view), or if it
4497 * doesn't want recursion, turn recursion off.
4499 client->query.attributes &= ~NS_QUERYATTR_RECURSIONOK;
4503 * Get the question name.
4505 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
4506 if (result != ISC_R_SUCCESS) {
4507 query_error(client, result);
4510 dns_message_currentname(message, DNS_SECTION_QUESTION,
4511 &client->query.qname);
4512 client->query.origqname = client->query.qname;
4513 result = dns_message_nextname(message, DNS_SECTION_QUESTION);
4514 if (result != ISC_R_NOMORE) {
4515 if (result == ISC_R_SUCCESS) {
4517 * There's more than one QNAME in the question
4520 query_error(client, DNS_R_FORMERR);
4522 query_error(client, result);
4526 if (ns_g_server->log_queries)
4530 * Check for multiple question queries, since edns1 is dead.
4532 if (message->counts[DNS_SECTION_QUESTION] > 1) {
4533 query_error(client, DNS_R_FORMERR);
4538 * Check for meta-queries like IXFR and AXFR.
4540 rdataset = ISC_LIST_HEAD(client->query.qname->list);
4541 INSIST(rdataset != NULL);
4542 qtype = rdataset->type;
4543 if (dns_rdatatype_ismeta(qtype)) {
4545 case dns_rdatatype_any:
4546 break; /* Let query_find handle it. */
4547 case dns_rdatatype_ixfr:
4548 case dns_rdatatype_axfr:
4549 ns_xfr_start(client, rdataset->type);
4551 case dns_rdatatype_maila:
4552 case dns_rdatatype_mailb:
4553 query_error(client, DNS_R_NOTIMP);
4555 case dns_rdatatype_tkey:
4556 result = dns_tkey_processquery(client->message,
4557 ns_g_server->tkeyctx,
4558 client->view->dynamickeys);
4559 if (result == ISC_R_SUCCESS)
4562 query_error(client, result);
4564 default: /* TSIG, etc. */
4565 query_error(client, DNS_R_FORMERR);
4571 * If the client has requested that DNSSEC checking be disabled,
4572 * allow lookups to return pending data and instruct the resolver
4573 * to return data before validation has completed.
4575 * We don't need to set DNS_DBFIND_PENDINGOK when validation is
4576 * disabled as there will be no pending data.
4578 if (message->flags & DNS_MESSAGEFLAG_CD ||
4579 qtype == dns_rdatatype_rrsig)
4581 client->query.dboptions |= DNS_DBFIND_PENDINGOK;
4582 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
4583 } else if (!client->view->enablevalidation)
4584 client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
4587 * Allow glue NS records to be added to the authority section
4588 * if the answer is secure.
4590 if (message->flags & DNS_MESSAGEFLAG_CD)
4591 client->query.attributes &= ~NS_QUERYATTR_SECURE;
4594 * Set 'want_ad' if the client has set AD in the query.
4595 * This allows AD to be returned on queries without DO set.
4597 if ((message->flags & DNS_MESSAGEFLAG_AD) != 0)
4600 want_ad = ISC_FALSE;
4603 * This is an ordinary query.
4605 result = dns_message_reply(message, ISC_TRUE);
4606 if (result != ISC_R_SUCCESS) {
4607 query_next(client, result);
4612 * Assume authoritative response until it is known to be
4615 message->flags |= DNS_MESSAGEFLAG_AA;
4618 * Set AD. We must clear it if we add non-validated data to a
4621 if (WANTDNSSEC(client) || want_ad)
4622 message->flags |= DNS_MESSAGEFLAG_AD;
4625 ns_client_attach(client, &qclient);
4626 query_find(qclient, NULL, qtype);