Merge branch 'vendor/BIND' into bind_vendor2
[dragonfly.git] / contrib / bind-9.5.2 / bin / named / query.c
1 /*
2  * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: query.c,v 1.298.48.15.2.1 2009/11/18 23:41:17 marka Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <isc/mem.h>
27 #include <isc/stats.h>
28 #include <isc/util.h>
29
30 #include <dns/adb.h>
31 #include <dns/byaddr.h>
32 #include <dns/db.h>
33 #ifdef DLZ
34 #include <dns/dlz.h>
35 #endif
36 #include <dns/dnssec.h>
37 #include <dns/events.h>
38 #include <dns/message.h>
39 #include <dns/ncache.h>
40 #include <dns/order.h>
41 #include <dns/rdata.h>
42 #include <dns/rdataclass.h>
43 #include <dns/rdatalist.h>
44 #include <dns/rdataset.h>
45 #include <dns/rdatasetiter.h>
46 #include <dns/rdatastruct.h>
47 #include <dns/rdatatype.h>
48 #include <dns/resolver.h>
49 #include <dns/result.h>
50 #include <dns/stats.h>
51 #include <dns/tkey.h>
52 #include <dns/view.h>
53 #include <dns/zone.h>
54 #include <dns/zt.h>
55
56 #include <named/client.h>
57 #include <named/log.h>
58 #include <named/server.h>
59 #include <named/sortlist.h>
60 #include <named/xfrout.h>
61
62 /*% Partial answer? */
63 #define PARTIALANSWER(c)        (((c)->query.attributes & \
64                                   NS_QUERYATTR_PARTIALANSWER) != 0)
65 /*% Use Cache? */
66 #define USECACHE(c)             (((c)->query.attributes & \
67                                   NS_QUERYATTR_CACHEOK) != 0)
68 /*% Recursion OK? */
69 #define RECURSIONOK(c)          (((c)->query.attributes & \
70                                   NS_QUERYATTR_RECURSIONOK) != 0)
71 /*% Recursing? */
72 #define RECURSING(c)            (((c)->query.attributes & \
73                                   NS_QUERYATTR_RECURSING) != 0)
74 /*% Cache glue ok? */
75 #define CACHEGLUEOK(c)          (((c)->query.attributes & \
76                                   NS_QUERYATTR_CACHEGLUEOK) != 0)
77 /*% Want Recursion? */
78 #define WANTRECURSION(c)        (((c)->query.attributes & \
79                                   NS_QUERYATTR_WANTRECURSION) != 0)
80 /*% Want DNSSEC? */
81 #define WANTDNSSEC(c)           (((c)->attributes & \
82                                   NS_CLIENTATTR_WANTDNSSEC) != 0)
83 /*% No authority? */
84 #define NOAUTHORITY(c)          (((c)->query.attributes & \
85                                   NS_QUERYATTR_NOAUTHORITY) != 0)
86 /*% No additional? */
87 #define NOADDITIONAL(c)         (((c)->query.attributes & \
88                                   NS_QUERYATTR_NOADDITIONAL) != 0)
89 /*% Secure? */
90 #define SECURE(c)               (((c)->query.attributes & \
91                                   NS_QUERYATTR_SECURE) != 0)
92
93 #if 0
94 #define CTRACE(m)       isc_log_write(ns_g_lctx, \
95                                       NS_LOGCATEGORY_CLIENT, \
96                                       NS_LOGMODULE_QUERY, \
97                                       ISC_LOG_DEBUG(3), \
98                                       "client %p: %s", client, (m))
99 #define QTRACE(m)       isc_log_write(ns_g_lctx, \
100                                       NS_LOGCATEGORY_GENERAL, \
101                                       NS_LOGMODULE_QUERY, \
102                                       ISC_LOG_DEBUG(3), \
103                                       "query %p: %s", query, (m))
104 #else
105 #define CTRACE(m) ((void)m)
106 #define QTRACE(m) ((void)m)
107 #endif
108
109 #define DNS_GETDB_NOEXACT 0x01U
110 #define DNS_GETDB_NOLOG 0x02U
111 #define DNS_GETDB_PARTIAL 0x04U
112
113 #define PENDINGOK(x)    (((x) & DNS_DBFIND_PENDINGOK) != 0)
114
115 typedef struct client_additionalctx {
116         ns_client_t *client;
117         dns_rdataset_t *rdataset;
118 } client_additionalctx_t;
119
120 static isc_result_t
121 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype);
122
123 static isc_boolean_t
124 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
125          dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
126
127 static inline void
128 log_queryerror(ns_client_t *client, isc_result_t result, int line, int level);
129
130 /*%
131  * Increment query statistics counters.
132  */
133 static inline void
134 inc_stats(ns_client_t *client, isc_statscounter_t counter) {
135         dns_zone_t *zone = client->query.authzone;
136
137         isc_stats_increment(ns_g_server->nsstats, counter);
138
139         if (zone != NULL) {
140                 isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
141                 if (zonestats != NULL)
142                         isc_stats_increment(zonestats, counter);
143         }
144 }
145
146 static void
147 query_send(ns_client_t *client) {
148         isc_statscounter_t counter;
149         if ((client->message->flags & DNS_MESSAGEFLAG_AA) == 0)
150                 inc_stats(client, dns_nsstatscounter_nonauthans);
151         else
152                 inc_stats(client, dns_nsstatscounter_authans);
153         if (client->message->rcode == dns_rcode_noerror) {
154                 if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER])) {
155                         if (client->query.isreferral) {
156                                 counter = dns_nsstatscounter_referral;
157                         } else {
158                                 counter = dns_nsstatscounter_nxrrset;
159                         }
160                 } else {
161                         counter = dns_nsstatscounter_success;
162                 }
163         } else if (client->message->rcode == dns_rcode_nxdomain) {
164                 counter = dns_nsstatscounter_nxdomain;
165         } else {
166                 /* We end up here in case of YXDOMAIN, and maybe others */
167                 counter = dns_nsstatscounter_failure;
168         }
169         inc_stats(client, counter);
170         ns_client_send(client);
171 }
172
173 static void
174 query_error(ns_client_t *client, isc_result_t result, int line) {
175         int loglevel = ISC_LOG_DEBUG(3);
176
177         switch (result) {
178         case DNS_R_SERVFAIL:
179                 loglevel = ISC_LOG_DEBUG(1);
180                 inc_stats(client, dns_nsstatscounter_servfail);
181                 break;
182         case DNS_R_FORMERR:
183                 inc_stats(client, dns_nsstatscounter_formerr);
184                 break;
185         default:
186                 inc_stats(client, dns_nsstatscounter_failure);
187                 break;
188         }
189
190         log_queryerror(client, result, line, loglevel);
191
192         ns_client_error(client, result);
193 }
194
195 static void
196 query_next(ns_client_t *client, isc_result_t result) {
197         if (result == DNS_R_DUPLICATE)
198                 inc_stats(client, dns_nsstatscounter_duplicate);
199         else if (result == DNS_R_DROP)
200                 inc_stats(client, dns_nsstatscounter_dropped);
201         else
202                 inc_stats(client, dns_nsstatscounter_failure);
203         ns_client_next(client, result);
204 }
205
206 static inline void
207 query_freefreeversions(ns_client_t *client, isc_boolean_t everything) {
208         ns_dbversion_t *dbversion, *dbversion_next;
209         unsigned int i;
210
211         for (dbversion = ISC_LIST_HEAD(client->query.freeversions), i = 0;
212              dbversion != NULL;
213              dbversion = dbversion_next, i++)
214         {
215                 dbversion_next = ISC_LIST_NEXT(dbversion, link);
216                 /*
217                  * If we're not freeing everything, we keep the first three
218                  * dbversions structures around.
219                  */
220                 if (i > 3 || everything) {
221                         ISC_LIST_UNLINK(client->query.freeversions, dbversion,
222                                         link);
223                         isc_mem_put(client->mctx, dbversion,
224                                     sizeof(*dbversion));
225                 }
226         }
227 }
228
229 void
230 ns_query_cancel(ns_client_t *client) {
231         LOCK(&client->query.fetchlock);
232         if (client->query.fetch != NULL) {
233                 dns_resolver_cancelfetch(client->query.fetch);
234
235                 client->query.fetch = NULL;
236         }
237         UNLOCK(&client->query.fetchlock);
238 }
239
240 static inline void
241 query_reset(ns_client_t *client, isc_boolean_t everything) {
242         isc_buffer_t *dbuf, *dbuf_next;
243         ns_dbversion_t *dbversion, *dbversion_next;
244
245         /*%
246          * Reset the query state of a client to its default state.
247          */
248
249         /*
250          * Cancel the fetch if it's running.
251          */
252         ns_query_cancel(client);
253
254         /*
255          * Cleanup any active versions.
256          */
257         for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
258              dbversion != NULL;
259              dbversion = dbversion_next) {
260                 dbversion_next = ISC_LIST_NEXT(dbversion, link);
261                 dns_db_closeversion(dbversion->db, &dbversion->version,
262                                     ISC_FALSE);
263                 dns_db_detach(&dbversion->db);
264                 ISC_LIST_INITANDAPPEND(client->query.freeversions,
265                                       dbversion, link);
266         }
267         ISC_LIST_INIT(client->query.activeversions);
268
269         if (client->query.authdb != NULL)
270                 dns_db_detach(&client->query.authdb);
271         if (client->query.authzone != NULL)
272                 dns_zone_detach(&client->query.authzone);
273
274         query_freefreeversions(client, everything);
275
276         for (dbuf = ISC_LIST_HEAD(client->query.namebufs);
277              dbuf != NULL;
278              dbuf = dbuf_next) {
279                 dbuf_next = ISC_LIST_NEXT(dbuf, link);
280                 if (dbuf_next != NULL || everything) {
281                         ISC_LIST_UNLINK(client->query.namebufs, dbuf, link);
282                         isc_buffer_free(&dbuf);
283                 }
284         }
285
286         if (client->query.restarts > 0) {
287                 /*
288                  * client->query.qname was dynamically allocated.
289                  */
290                 dns_message_puttempname(client->message,
291                                         &client->query.qname);
292         }
293         client->query.qname = NULL;
294         client->query.attributes = (NS_QUERYATTR_RECURSIONOK |
295                                     NS_QUERYATTR_CACHEOK |
296                                     NS_QUERYATTR_SECURE);
297         client->query.restarts = 0;
298         client->query.timerset = ISC_FALSE;
299         client->query.origqname = NULL;
300         client->query.qname = NULL;
301         client->query.dboptions = 0;
302         client->query.fetchoptions = 0;
303         client->query.gluedb = NULL;
304         client->query.authdbset = ISC_FALSE;
305         client->query.isreferral = ISC_FALSE;
306 }
307
308 static void
309 query_next_callback(ns_client_t *client) {
310         query_reset(client, ISC_FALSE);
311 }
312
313 void
314 ns_query_free(ns_client_t *client) {
315         query_reset(client, ISC_TRUE);
316 }
317
318 static inline isc_result_t
319 query_newnamebuf(ns_client_t *client) {
320         isc_buffer_t *dbuf;
321         isc_result_t result;
322
323         CTRACE("query_newnamebuf");
324         /*%
325          * Allocate a name buffer.
326          */
327
328         dbuf = NULL;
329         result = isc_buffer_allocate(client->mctx, &dbuf, 1024);
330         if (result != ISC_R_SUCCESS) {
331                 CTRACE("query_newnamebuf: isc_buffer_allocate failed: done");
332                 return (result);
333         }
334         ISC_LIST_APPEND(client->query.namebufs, dbuf, link);
335
336         CTRACE("query_newnamebuf: done");
337         return (ISC_R_SUCCESS);
338 }
339
340 static inline isc_buffer_t *
341 query_getnamebuf(ns_client_t *client) {
342         isc_buffer_t *dbuf;
343         isc_result_t result;
344         isc_region_t r;
345
346         CTRACE("query_getnamebuf");
347         /*%
348          * Return a name buffer with space for a maximal name, allocating
349          * a new one if necessary.
350          */
351
352         if (ISC_LIST_EMPTY(client->query.namebufs)) {
353                 result = query_newnamebuf(client);
354                 if (result != ISC_R_SUCCESS) {
355                     CTRACE("query_getnamebuf: query_newnamebuf failed: done");
356                         return (NULL);
357                 }
358         }
359
360         dbuf = ISC_LIST_TAIL(client->query.namebufs);
361         INSIST(dbuf != NULL);
362         isc_buffer_availableregion(dbuf, &r);
363         if (r.length < 255) {
364                 result = query_newnamebuf(client);
365                 if (result != ISC_R_SUCCESS) {
366                     CTRACE("query_getnamebuf: query_newnamebuf failed: done");
367                         return (NULL);
368
369                 }
370                 dbuf = ISC_LIST_TAIL(client->query.namebufs);
371                 isc_buffer_availableregion(dbuf, &r);
372                 INSIST(r.length >= 255);
373         }
374         CTRACE("query_getnamebuf: done");
375         return (dbuf);
376 }
377
378 static inline void
379 query_keepname(ns_client_t *client, dns_name_t *name, isc_buffer_t *dbuf) {
380         isc_region_t r;
381
382         CTRACE("query_keepname");
383         /*%
384          * 'name' is using space in 'dbuf', but 'dbuf' has not yet been
385          * adjusted to take account of that.  We do the adjustment.
386          */
387
388         REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) != 0);
389
390         dns_name_toregion(name, &r);
391         isc_buffer_add(dbuf, r.length);
392         dns_name_setbuffer(name, NULL);
393         client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
394 }
395
396 static inline void
397 query_releasename(ns_client_t *client, dns_name_t **namep) {
398         dns_name_t *name = *namep;
399
400         /*%
401          * 'name' is no longer needed.  Return it to our pool of temporary
402          * names.  If it is using a name buffer, relinquish its exclusive
403          * rights on the buffer.
404          */
405
406         CTRACE("query_releasename");
407         if (dns_name_hasbuffer(name)) {
408                 INSIST((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED)
409                        != 0);
410                 client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
411         }
412         dns_message_puttempname(client->message, namep);
413         CTRACE("query_releasename: done");
414 }
415
416 static inline dns_name_t *
417 query_newname(ns_client_t *client, isc_buffer_t *dbuf,
418               isc_buffer_t *nbuf)
419 {
420         dns_name_t *name;
421         isc_region_t r;
422         isc_result_t result;
423
424         REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) == 0);
425
426         CTRACE("query_newname");
427         name = NULL;
428         result = dns_message_gettempname(client->message, &name);
429         if (result != ISC_R_SUCCESS) {
430                 CTRACE("query_newname: dns_message_gettempname failed: done");
431                 return (NULL);
432         }
433         isc_buffer_availableregion(dbuf, &r);
434         isc_buffer_init(nbuf, r.base, r.length);
435         dns_name_init(name, NULL);
436         dns_name_setbuffer(name, nbuf);
437         client->query.attributes |= NS_QUERYATTR_NAMEBUFUSED;
438
439         CTRACE("query_newname: done");
440         return (name);
441 }
442
443 static inline dns_rdataset_t *
444 query_newrdataset(ns_client_t *client) {
445         dns_rdataset_t *rdataset;
446         isc_result_t result;
447
448         CTRACE("query_newrdataset");
449         rdataset = NULL;
450         result = dns_message_gettemprdataset(client->message, &rdataset);
451         if (result != ISC_R_SUCCESS) {
452           CTRACE("query_newrdataset: "
453                  "dns_message_gettemprdataset failed: done");
454                 return (NULL);
455         }
456         dns_rdataset_init(rdataset);
457
458         CTRACE("query_newrdataset: done");
459         return (rdataset);
460 }
461
462 static inline void
463 query_putrdataset(ns_client_t *client, dns_rdataset_t **rdatasetp) {
464         dns_rdataset_t *rdataset = *rdatasetp;
465
466         CTRACE("query_putrdataset");
467         if (rdataset != NULL) {
468                 if (dns_rdataset_isassociated(rdataset))
469                         dns_rdataset_disassociate(rdataset);
470                 dns_message_puttemprdataset(client->message, rdatasetp);
471         }
472         CTRACE("query_putrdataset: done");
473 }
474
475
476 static inline isc_result_t
477 query_newdbversion(ns_client_t *client, unsigned int n) {
478         unsigned int i;
479         ns_dbversion_t *dbversion;
480
481         for (i = 0; i < n; i++) {
482                 dbversion = isc_mem_get(client->mctx, sizeof(*dbversion));
483                 if (dbversion != NULL) {
484                         dbversion->db = NULL;
485                         dbversion->version = NULL;
486                         ISC_LIST_INITANDAPPEND(client->query.freeversions,
487                                               dbversion, link);
488                 } else {
489                         /*
490                          * We only return ISC_R_NOMEMORY if we couldn't
491                          * allocate anything.
492                          */
493                         if (i == 0)
494                                 return (ISC_R_NOMEMORY);
495                         else
496                                 return (ISC_R_SUCCESS);
497                 }
498         }
499
500         return (ISC_R_SUCCESS);
501 }
502
503 static inline ns_dbversion_t *
504 query_getdbversion(ns_client_t *client) {
505         isc_result_t result;
506         ns_dbversion_t *dbversion;
507
508         if (ISC_LIST_EMPTY(client->query.freeversions)) {
509                 result = query_newdbversion(client, 1);
510                 if (result != ISC_R_SUCCESS)
511                         return (NULL);
512         }
513         dbversion = ISC_LIST_HEAD(client->query.freeversions);
514         INSIST(dbversion != NULL);
515         ISC_LIST_UNLINK(client->query.freeversions, dbversion, link);
516
517         return (dbversion);
518 }
519
520 isc_result_t
521 ns_query_init(ns_client_t *client) {
522         isc_result_t result;
523
524         ISC_LIST_INIT(client->query.namebufs);
525         ISC_LIST_INIT(client->query.activeversions);
526         ISC_LIST_INIT(client->query.freeversions);
527         client->query.restarts = 0;
528         client->query.timerset = ISC_FALSE;
529         client->query.qname = NULL;
530         result = isc_mutex_init(&client->query.fetchlock);
531         if (result != ISC_R_SUCCESS)
532                 return (result);
533         client->query.fetch = NULL;
534         client->query.authdb = NULL;
535         client->query.authzone = NULL;
536         client->query.authdbset = ISC_FALSE;
537         client->query.isreferral = ISC_FALSE;
538         query_reset(client, ISC_FALSE);
539         result = query_newdbversion(client, 3);
540         if (result != ISC_R_SUCCESS) {
541                 DESTROYLOCK(&client->query.fetchlock);
542                 return (result);
543         }
544         result = query_newnamebuf(client);
545         if (result != ISC_R_SUCCESS)
546                 query_freefreeversions(client, ISC_TRUE);
547
548         return (result);
549 }
550
551 static inline ns_dbversion_t *
552 query_findversion(ns_client_t *client, dns_db_t *db,
553                   isc_boolean_t *newzonep)
554 {
555         ns_dbversion_t *dbversion;
556
557         /*%
558          * We may already have done a query related to this
559          * database.  If so, we must be sure to make subsequent
560          * queries from the same version.
561          */
562         for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
563              dbversion != NULL;
564              dbversion = ISC_LIST_NEXT(dbversion, link)) {
565                 if (dbversion->db == db)
566                         break;
567         }
568
569         if (dbversion == NULL) {
570                 /*
571                  * This is a new zone for this query.  Add it to
572                  * the active list.
573                  */
574                 dbversion = query_getdbversion(client);
575                 if (dbversion == NULL)
576                         return (NULL);
577                 dns_db_attach(db, &dbversion->db);
578                 dns_db_currentversion(db, &dbversion->version);
579                 dbversion->queryok = ISC_FALSE;
580                 ISC_LIST_APPEND(client->query.activeversions,
581                                 dbversion, link);
582                 *newzonep = ISC_TRUE;
583         } else
584                 *newzonep = ISC_FALSE;
585
586         return (dbversion);
587 }
588
589 static inline isc_result_t
590 query_validatezonedb(ns_client_t *client, dns_name_t *name,
591                      dns_rdatatype_t qtype, unsigned int options,
592                      dns_zone_t *zone, dns_db_t *db,
593                      dns_dbversion_t **versionp)
594 {
595         isc_result_t result;
596         isc_boolean_t check_acl, new_zone;
597         dns_acl_t *queryacl;
598         ns_dbversion_t *dbversion;
599
600         REQUIRE(zone != NULL);
601         REQUIRE(db != NULL);
602
603         /*
604          * This limits our searching to the zone where the first name
605          * (the query target) was looked for.  This prevents following
606          * CNAMES or DNAMES into other zones and prevents returning
607          * additional data from other zones.
608          */
609         if (!client->view->additionalfromauth &&
610             client->query.authdbset &&
611             db != client->query.authdb)
612                 goto refuse;
613
614         /*
615          * If the zone has an ACL, we'll check it, otherwise
616          * we use the view's "allow-query" ACL.  Each ACL is only checked
617          * once per query.
618          *
619          * Also, get the database version to use.
620          */
621
622         check_acl = ISC_TRUE;   /* Keep compiler happy. */
623         queryacl = NULL;
624
625         /*
626          * Get the current version of this database.
627          */
628         dbversion = query_findversion(client, db, &new_zone);
629         if (dbversion == NULL) {
630                 result = DNS_R_SERVFAIL;
631                 goto fail;
632         }
633         if (new_zone) {
634                 check_acl = ISC_TRUE;
635         } else if (!dbversion->queryok) {
636                 goto refuse;
637         } else {
638                 check_acl = ISC_FALSE;
639         }
640
641         queryacl = dns_zone_getqueryacl(zone);
642         if (queryacl == NULL) {
643                 queryacl = client->view->queryacl;
644                 if ((client->query.attributes &
645                      NS_QUERYATTR_QUERYOKVALID) != 0) {
646                         /*
647                          * We've evaluated the view's queryacl already.  If
648                          * NS_QUERYATTR_QUERYOK is set, then the client is
649                          * allowed to make queries, otherwise the query should
650                          * be refused.
651                          */
652                         check_acl = ISC_FALSE;
653                         if ((client->query.attributes &
654                              NS_QUERYATTR_QUERYOK) == 0)
655                                 goto refuse;
656                 } else {
657                         /*
658                          * We haven't evaluated the view's queryacl yet.
659                          */
660                         check_acl = ISC_TRUE;
661                 }
662         }
663
664         if (check_acl) {
665                 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
666
667                 result = ns_client_checkaclsilent(client, NULL, queryacl,
668                                                   ISC_TRUE);
669                 if (log) {
670                         char msg[NS_CLIENT_ACLMSGSIZE("query")];
671                         if (result == ISC_R_SUCCESS) {
672                                 if (isc_log_wouldlog(ns_g_lctx,
673                                                      ISC_LOG_DEBUG(3)))
674                                 {
675                                         ns_client_aclmsg("query", name, qtype,
676                                                          client->view->rdclass,
677                                                          msg, sizeof(msg));
678                                         ns_client_log(client,
679                                                       DNS_LOGCATEGORY_SECURITY,
680                                                       NS_LOGMODULE_QUERY,
681                                                       ISC_LOG_DEBUG(3),
682                                                       "%s approved", msg);
683                                 }
684                         } else {
685                                 ns_client_aclmsg("query", name, qtype,
686                                                  client->view->rdclass,
687                                                  msg, sizeof(msg));
688                                 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
689                                               NS_LOGMODULE_QUERY, ISC_LOG_INFO,
690                                               "%s denied", msg);
691                         }
692                 }
693
694                 if (queryacl == client->view->queryacl) {
695                         if (result == ISC_R_SUCCESS) {
696                                 /*
697                                  * We were allowed by the default
698                                  * "allow-query" ACL.  Remember this so we
699                                  * don't have to check again.
700                                  */
701                                 client->query.attributes |=
702                                         NS_QUERYATTR_QUERYOK;
703                         }
704                         /*
705                          * We've now evaluated the view's query ACL, and
706                          * the NS_QUERYATTR_QUERYOK attribute is now valid.
707                          */
708                         client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
709                 }
710
711                 if (result != ISC_R_SUCCESS)
712                         goto refuse;
713         }
714
715         /* Approved. */
716
717         /*
718          * Remember the result of the ACL check so we
719          * don't have to check again.
720          */
721         dbversion->queryok = ISC_TRUE;
722
723         /* Transfer ownership, if necessary. */
724         if (versionp != NULL)
725                 *versionp = dbversion->version;
726
727         return (ISC_R_SUCCESS);
728
729  refuse:
730         return (DNS_R_REFUSED);
731
732  fail:
733         return (result);
734 }
735
736 static inline isc_result_t
737 query_getzonedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
738                 unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
739                 dns_dbversion_t **versionp)
740 {
741         isc_result_t result;
742         unsigned int ztoptions;
743         dns_zone_t *zone = NULL;
744         dns_db_t *db = NULL;
745         isc_boolean_t partial = ISC_FALSE;
746
747         REQUIRE(zonep != NULL && *zonep == NULL);
748         REQUIRE(dbp != NULL && *dbp == NULL);
749
750         /*%
751          * Find a zone database to answer the query.
752          */
753         ztoptions = ((options & DNS_GETDB_NOEXACT) != 0) ?
754                 DNS_ZTFIND_NOEXACT : 0;
755
756         result = dns_zt_find(client->view->zonetable, name, ztoptions, NULL,
757                              &zone);
758         if (result == DNS_R_PARTIALMATCH)
759                 partial = ISC_TRUE;
760         if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
761                 result = dns_zone_getdb(zone, &db);
762
763         if (result != ISC_R_SUCCESS)
764                 goto fail;
765
766         result = query_validatezonedb(client, name, qtype, options, zone, db,
767                                       versionp);
768
769         if (result != ISC_R_SUCCESS)
770                 goto fail;
771
772         /* Transfer ownership. */
773         *zonep = zone;
774         *dbp = db;
775
776         if (partial && (options & DNS_GETDB_PARTIAL) != 0)
777                 return (DNS_R_PARTIALMATCH);
778         return (ISC_R_SUCCESS);
779
780  fail:
781         if (zone != NULL)
782                 dns_zone_detach(&zone);
783         if (db != NULL)
784                 dns_db_detach(&db);
785
786         return (result);
787 }
788
789 static inline isc_result_t
790 query_getcachedb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
791                  dns_db_t **dbp, unsigned int options)
792 {
793         isc_result_t result;
794         isc_boolean_t check_acl;
795         dns_db_t *db = NULL;
796
797         REQUIRE(dbp != NULL && *dbp == NULL);
798
799         /*%
800          * Find a cache database to answer the query.
801          * This may fail with DNS_R_REFUSED if the client
802          * is not allowed to use the cache.
803          */
804
805         if (!USECACHE(client))
806                 return (DNS_R_REFUSED);
807         dns_db_attach(client->view->cachedb, &db);
808
809         if ((client->query.attributes &
810              NS_QUERYATTR_QUERYOKVALID) != 0) {
811                 /*
812                  * We've evaluated the view's queryacl already.  If
813                  * NS_QUERYATTR_QUERYOK is set, then the client is
814                  * allowed to make queries, otherwise the query should
815                  * be refused.
816                  */
817                 check_acl = ISC_FALSE;
818                 if ((client->query.attributes &
819                      NS_QUERYATTR_QUERYOK) == 0)
820                         goto refuse;
821         } else {
822                 /*
823                  * We haven't evaluated the view's queryacl yet.
824                  */
825                 check_acl = ISC_TRUE;
826         }
827
828         if (check_acl) {
829                 isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
830                 char msg[NS_CLIENT_ACLMSGSIZE("query (cache)")];
831
832                 result = ns_client_checkaclsilent(client, NULL,
833                                                   client->view->queryacl,
834                                                   ISC_TRUE);
835                 if (result == ISC_R_SUCCESS) {
836                         /*
837                          * We were allowed by the default
838                          * "allow-query" ACL.  Remember this so we
839                          * don't have to check again.
840                          */
841                         client->query.attributes |=
842                                 NS_QUERYATTR_QUERYOK;
843                         if (log && isc_log_wouldlog(ns_g_lctx,
844                                                      ISC_LOG_DEBUG(3)))
845                         {
846                                 ns_client_aclmsg("query (cache)", name, qtype,
847                                                  client->view->rdclass,
848                                                  msg, sizeof(msg));
849                                 ns_client_log(client,
850                                               DNS_LOGCATEGORY_SECURITY,
851                                               NS_LOGMODULE_QUERY,
852                                               ISC_LOG_DEBUG(3),
853                                               "%s approved", msg);
854                         }
855                 } else if (log) {
856                         ns_client_aclmsg("query (cache)", name, qtype,
857                                          client->view->rdclass, msg,
858                                          sizeof(msg));
859                         ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
860                                       NS_LOGMODULE_QUERY, ISC_LOG_INFO,
861                                       "%s denied", msg);
862                 }
863                 /*
864                  * We've now evaluated the view's query ACL, and
865                  * the NS_QUERYATTR_QUERYOK attribute is now valid.
866                  */
867                 client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
868
869                 if (result != ISC_R_SUCCESS)
870                         goto refuse;
871         }
872
873         /* Approved. */
874
875         /* Transfer ownership. */
876         *dbp = db;
877
878         return (ISC_R_SUCCESS);
879
880  refuse:
881         result = DNS_R_REFUSED;
882
883         if (db != NULL)
884                 dns_db_detach(&db);
885
886         return (result);
887 }
888
889
890 static inline isc_result_t
891 query_getdb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
892             unsigned int options, dns_zone_t **zonep, dns_db_t **dbp,
893             dns_dbversion_t **versionp, isc_boolean_t *is_zonep)
894 {
895         isc_result_t result;
896
897 #ifdef DLZ
898         isc_result_t tresult;
899         unsigned int namelabels;
900         unsigned int zonelabels;
901         dns_zone_t *zone = NULL;
902         dns_db_t *tdbp;
903
904         REQUIRE(zonep != NULL && *zonep == NULL);
905
906         tdbp = NULL;
907
908         /* Calculate how many labels are in name. */
909         namelabels = dns_name_countlabels(name);
910         zonelabels = 0;
911
912         /* Try to find name in bind's standard database. */
913         result = query_getzonedb(client, name, qtype, options, &zone,
914                                  dbp, versionp);
915
916         /* See how many labels are in the zone's name.    */
917         if (result == ISC_R_SUCCESS && zone != NULL)
918                 zonelabels = dns_name_countlabels(dns_zone_getorigin(zone));
919         /*
920          * If # zone labels < # name labels, try to find an even better match
921          * Only try if a DLZ driver is loaded for this view
922          */
923         if (zonelabels < namelabels && client->view->dlzdatabase != NULL) {
924                 tresult = dns_dlzfindzone(client->view, name,
925                                           zonelabels, &tdbp);
926                  /* If we successful, we found a better match. */
927                 if (tresult == ISC_R_SUCCESS) {
928                         /*
929                          * If the previous search returned a zone, detach it.
930                          */
931                         if (zone != NULL)
932                                 dns_zone_detach(&zone);
933
934                         /*
935                          * If the previous search returned a database,
936                          * detach it.
937                          */
938                         if (*dbp != NULL)
939                                 dns_db_detach(dbp);
940
941                         /*
942                          * If the previous search returned a version, clear it.
943                          */
944                         *versionp = NULL;
945
946                         /*
947                          * Get our database version.
948                          */
949                         dns_db_currentversion(tdbp, versionp);
950
951                         /*
952                          * Be sure to return our database.
953                          */
954                         *dbp = tdbp;
955
956                         /*
957                          * We return a null zone, No stats for DLZ zones.
958                          */
959                         zone = NULL;
960                         result = tresult;
961                 }
962         }
963 #else
964         result = query_getzonedb(client, name, qtype, options,
965                                  zonep, dbp, versionp);
966 #endif
967
968         /* If successful, Transfer ownership of zone. */
969         if (result == ISC_R_SUCCESS) {
970 #ifdef DLZ
971                 *zonep = zone;
972 #endif
973                 /*
974                  * If neither attempt above succeeded, return the cache instead
975                  */
976                 *is_zonep = ISC_TRUE;
977         } else if (result == ISC_R_NOTFOUND) {
978                 result = query_getcachedb(client, name, qtype, dbp, options);
979                 *is_zonep = ISC_FALSE;
980         }
981         return (result);
982 }
983
984 static inline isc_boolean_t
985 query_isduplicate(ns_client_t *client, dns_name_t *name,
986                   dns_rdatatype_t type, dns_name_t **mnamep)
987 {
988         dns_section_t section;
989         dns_name_t *mname = NULL;
990         isc_result_t result;
991
992         CTRACE("query_isduplicate");
993
994         for (section = DNS_SECTION_ANSWER;
995              section <= DNS_SECTION_ADDITIONAL;
996              section++) {
997                 result = dns_message_findname(client->message, section,
998                                               name, type, 0, &mname, NULL);
999                 if (result == ISC_R_SUCCESS) {
1000                         /*
1001                          * We've already got this RRset in the response.
1002                          */
1003                         CTRACE("query_isduplicate: true: done");
1004                         return (ISC_TRUE);
1005                 } else if (result == DNS_R_NXRRSET) {
1006                         /*
1007                          * The name exists, but the rdataset does not.
1008                          */
1009                         if (section == DNS_SECTION_ADDITIONAL)
1010                                 break;
1011                 } else
1012                         RUNTIME_CHECK(result == DNS_R_NXDOMAIN);
1013                 mname = NULL;
1014         }
1015
1016         /*
1017          * If the dns_name_t we're looking up is already in the message,
1018          * we don't want to trigger the caller's name replacement logic.
1019          */
1020         if (name == mname)
1021                 mname = NULL;
1022
1023         *mnamep = mname;
1024
1025         CTRACE("query_isduplicate: false: done");
1026         return (ISC_FALSE);
1027 }
1028
1029 static isc_result_t
1030 query_addadditional(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1031         ns_client_t *client = arg;
1032         isc_result_t result, eresult;
1033         dns_dbnode_t *node;
1034         dns_db_t *db;
1035         dns_name_t *fname, *mname;
1036         dns_rdataset_t *rdataset, *sigrdataset, *trdataset;
1037         isc_buffer_t *dbuf;
1038         isc_buffer_t b;
1039         dns_dbversion_t *version;
1040         isc_boolean_t added_something, need_addname;
1041         dns_zone_t *zone;
1042         dns_rdatatype_t type;
1043
1044         REQUIRE(NS_CLIENT_VALID(client));
1045         REQUIRE(qtype != dns_rdatatype_any);
1046
1047         if (!WANTDNSSEC(client) && dns_rdatatype_isdnssec(qtype))
1048                 return (ISC_R_SUCCESS);
1049
1050         CTRACE("query_addadditional");
1051
1052         /*
1053          * Initialization.
1054          */
1055         eresult = ISC_R_SUCCESS;
1056         fname = NULL;
1057         rdataset = NULL;
1058         sigrdataset = NULL;
1059         trdataset = NULL;
1060         db = NULL;
1061         version = NULL;
1062         node = NULL;
1063         added_something = ISC_FALSE;
1064         need_addname = ISC_FALSE;
1065         zone = NULL;
1066
1067         /*
1068          * We treat type A additional section processing as if it
1069          * were "any address type" additional section processing.
1070          * To avoid multiple lookups, we do an 'any' database
1071          * lookup and iterate over the node.
1072          */
1073         if (qtype == dns_rdatatype_a)
1074                 type = dns_rdatatype_any;
1075         else
1076                 type = qtype;
1077
1078         /*
1079          * Get some resources.
1080          */
1081         dbuf = query_getnamebuf(client);
1082         if (dbuf == NULL)
1083                 goto cleanup;
1084         fname = query_newname(client, dbuf, &b);
1085         rdataset = query_newrdataset(client);
1086         if (fname == NULL || rdataset == NULL)
1087                 goto cleanup;
1088         if (WANTDNSSEC(client)) {
1089                 sigrdataset = query_newrdataset(client);
1090                 if (sigrdataset == NULL)
1091                         goto cleanup;
1092         }
1093
1094         /*
1095          * Look for a zone database that might contain authoritative
1096          * additional data.
1097          */
1098         result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1099                                  &zone, &db, &version);
1100         if (result != ISC_R_SUCCESS)
1101                 goto try_cache;
1102
1103         CTRACE("query_addadditional: db_find");
1104
1105         /*
1106          * Since we are looking for authoritative data, we do not set
1107          * the GLUEOK flag.  Glue will be looked for later, but not
1108          * necessarily in the same database.
1109          */
1110         node = NULL;
1111         result = dns_db_find(db, name, version, type, client->query.dboptions,
1112                              client->now, &node, fname, rdataset,
1113                              sigrdataset);
1114         if (result == ISC_R_SUCCESS)
1115                 goto found;
1116
1117         if (dns_rdataset_isassociated(rdataset))
1118                 dns_rdataset_disassociate(rdataset);
1119         if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
1120                 dns_rdataset_disassociate(sigrdataset);
1121         if (node != NULL)
1122                 dns_db_detachnode(db, &node);
1123         version = NULL;
1124         dns_db_detach(&db);
1125
1126         /*
1127          * No authoritative data was found.  The cache is our next best bet.
1128          */
1129
1130  try_cache:
1131         result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1132         if (result != ISC_R_SUCCESS)
1133                 /*
1134                  * Most likely the client isn't allowed to query the cache.
1135                  */
1136                 goto try_glue;
1137         /*
1138          * Attempt to validate glue.
1139          */
1140         if (sigrdataset == NULL) {
1141                 sigrdataset = query_newrdataset(client);
1142                 if (sigrdataset == NULL)
1143                         goto cleanup;
1144         }
1145         result = dns_db_find(db, name, version, type,
1146                              client->query.dboptions | DNS_DBFIND_GLUEOK,
1147                              client->now, &node, fname, rdataset,
1148                              sigrdataset);
1149         if (result == DNS_R_GLUE &&
1150             validate(client, db, fname, rdataset, sigrdataset))
1151                 result = ISC_R_SUCCESS;
1152         if (!WANTDNSSEC(client))
1153                 query_putrdataset(client, &sigrdataset);
1154         if (result == ISC_R_SUCCESS)
1155                 goto found;
1156
1157         if (dns_rdataset_isassociated(rdataset))
1158                 dns_rdataset_disassociate(rdataset);
1159         if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
1160                 dns_rdataset_disassociate(sigrdataset);
1161         if (node != NULL)
1162                 dns_db_detachnode(db, &node);
1163         dns_db_detach(&db);
1164
1165  try_glue:
1166         /*
1167          * No cached data was found.  Glue is our last chance.
1168          * RFC1035 sayeth:
1169          *
1170          *      NS records cause both the usual additional section
1171          *      processing to locate a type A record, and, when used
1172          *      in a referral, a special search of the zone in which
1173          *      they reside for glue information.
1174          *
1175          * This is the "special search".  Note that we must search
1176          * the zone where the NS record resides, not the zone it
1177          * points to, and that we only do the search in the delegation
1178          * case (identified by client->query.gluedb being set).
1179          */
1180
1181         if (client->query.gluedb == NULL)
1182                 goto cleanup;
1183
1184         /*
1185          * Don't poison caches using the bailiwick protection model.
1186          */
1187         if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1188                 goto cleanup;
1189
1190         dns_db_attach(client->query.gluedb, &db);
1191         result = dns_db_find(db, name, version, type,
1192                              client->query.dboptions | DNS_DBFIND_GLUEOK,
1193                              client->now, &node, fname, rdataset,
1194                              sigrdataset);
1195         if (!(result == ISC_R_SUCCESS ||
1196               result == DNS_R_ZONECUT ||
1197               result == DNS_R_GLUE))
1198                 goto cleanup;
1199
1200  found:
1201         /*
1202          * We have found a potential additional data rdataset, or
1203          * at least a node to iterate over.
1204          */
1205         query_keepname(client, fname, dbuf);
1206
1207         /*
1208          * If we have an rdataset, add it to the additional data
1209          * section.
1210          */
1211         mname = NULL;
1212         if (dns_rdataset_isassociated(rdataset) &&
1213             !query_isduplicate(client, fname, type, &mname)) {
1214                 if (mname != NULL) {
1215                         query_releasename(client, &fname);
1216                         fname = mname;
1217                 } else
1218                         need_addname = ISC_TRUE;
1219                 ISC_LIST_APPEND(fname->list, rdataset, link);
1220                 trdataset = rdataset;
1221                 rdataset = NULL;
1222                 added_something = ISC_TRUE;
1223                 /*
1224                  * Note: we only add SIGs if we've added the type they cover,
1225                  * so we do not need to check if the SIG rdataset is already
1226                  * in the response.
1227                  */
1228                 if (sigrdataset != NULL &&
1229                     dns_rdataset_isassociated(sigrdataset))
1230                 {
1231                         ISC_LIST_APPEND(fname->list, sigrdataset, link);
1232                         sigrdataset = NULL;
1233                 }
1234         }
1235
1236         if (qtype == dns_rdatatype_a) {
1237                 /*
1238                  * We now go looking for A and AAAA records, along with
1239                  * their signatures.
1240                  *
1241                  * XXXRTH  This code could be more efficient.
1242                  */
1243                 if (rdataset != NULL) {
1244                         if (dns_rdataset_isassociated(rdataset))
1245                                 dns_rdataset_disassociate(rdataset);
1246                 } else {
1247                         rdataset = query_newrdataset(client);
1248                         if (rdataset == NULL)
1249                                 goto addname;
1250                 }
1251                 if (sigrdataset != NULL) {
1252                         if (dns_rdataset_isassociated(sigrdataset))
1253                                 dns_rdataset_disassociate(sigrdataset);
1254                 } else if (WANTDNSSEC(client)) {
1255                         sigrdataset = query_newrdataset(client);
1256                         if (sigrdataset == NULL)
1257                                 goto addname;
1258                 }
1259                 result = dns_db_findrdataset(db, node, version,
1260                                              dns_rdatatype_a, 0,
1261                                              client->now, rdataset,
1262                                              sigrdataset);
1263                 if (result == DNS_R_NCACHENXDOMAIN)
1264                         goto addname;
1265                 if (result == DNS_R_NCACHENXRRSET) {
1266                         dns_rdataset_disassociate(rdataset);
1267                         /*
1268                          * Negative cache entries don't have sigrdatasets.
1269                          */
1270                         INSIST(sigrdataset == NULL ||
1271                                ! dns_rdataset_isassociated(sigrdataset));
1272                 }
1273                 if (result == ISC_R_SUCCESS) {
1274                         mname = NULL;
1275                         if (!query_isduplicate(client, fname,
1276                                                dns_rdatatype_a, &mname)) {
1277                                 if (mname != NULL) {
1278                                         query_releasename(client, &fname);
1279                                         fname = mname;
1280                                 } else
1281                                         need_addname = ISC_TRUE;
1282                                 ISC_LIST_APPEND(fname->list, rdataset, link);
1283                                 added_something = ISC_TRUE;
1284                                 if (sigrdataset != NULL &&
1285                                     dns_rdataset_isassociated(sigrdataset))
1286                                 {
1287                                         ISC_LIST_APPEND(fname->list,
1288                                                         sigrdataset, link);
1289                                         sigrdataset =
1290                                                 query_newrdataset(client);
1291                                 }
1292                                 rdataset = query_newrdataset(client);
1293                                 if (rdataset == NULL)
1294                                         goto addname;
1295                                 if (WANTDNSSEC(client) && sigrdataset == NULL)
1296                                         goto addname;
1297                         } else {
1298                                 dns_rdataset_disassociate(rdataset);
1299                                 if (sigrdataset != NULL &&
1300                                     dns_rdataset_isassociated(sigrdataset))
1301                                         dns_rdataset_disassociate(sigrdataset);
1302                         }
1303                 }
1304                 result = dns_db_findrdataset(db, node, version,
1305                                              dns_rdatatype_aaaa, 0,
1306                                              client->now, rdataset,
1307                                              sigrdataset);
1308                 if (result == DNS_R_NCACHENXDOMAIN)
1309                         goto addname;
1310                 if (result == DNS_R_NCACHENXRRSET) {
1311                         dns_rdataset_disassociate(rdataset);
1312                         INSIST(sigrdataset == NULL ||
1313                                ! dns_rdataset_isassociated(sigrdataset));
1314                 }
1315                 if (result == ISC_R_SUCCESS) {
1316                         mname = NULL;
1317                         if (!query_isduplicate(client, fname,
1318                                                dns_rdatatype_aaaa, &mname)) {
1319                                 if (mname != NULL) {
1320                                         query_releasename(client, &fname);
1321                                         fname = mname;
1322                                 } else
1323                                         need_addname = ISC_TRUE;
1324                                 ISC_LIST_APPEND(fname->list, rdataset, link);
1325                                 added_something = ISC_TRUE;
1326                                 if (sigrdataset != NULL &&
1327                                     dns_rdataset_isassociated(sigrdataset))
1328                                 {
1329                                         ISC_LIST_APPEND(fname->list,
1330                                                         sigrdataset, link);
1331                                         sigrdataset = NULL;
1332                                 }
1333                                 rdataset = NULL;
1334                         }
1335                 }
1336         }
1337
1338  addname:
1339         CTRACE("query_addadditional: addname");
1340         /*
1341          * If we haven't added anything, then we're done.
1342          */
1343         if (!added_something)
1344                 goto cleanup;
1345
1346         /*
1347          * We may have added our rdatasets to an existing name, if so, then
1348          * need_addname will be ISC_FALSE.  Whether we used an existing name
1349          * or a new one, we must set fname to NULL to prevent cleanup.
1350          */
1351         if (need_addname)
1352                 dns_message_addname(client->message, fname,
1353                                     DNS_SECTION_ADDITIONAL);
1354         fname = NULL;
1355
1356         /*
1357          * In a few cases, we want to add additional data for additional
1358          * data.  It's simpler to just deal with special cases here than
1359          * to try to create a general purpose mechanism and allow the
1360          * rdata implementations to do it themselves.
1361          *
1362          * This involves recursion, but the depth is limited.  The
1363          * most complex case is adding a SRV rdataset, which involves
1364          * recursing to add address records, which in turn can cause
1365          * recursion to add KEYs.
1366          */
1367         if (type == dns_rdatatype_srv && trdataset != NULL) {
1368                 /*
1369                  * If we're adding SRV records to the additional data
1370                  * section, it's helpful if we add the SRV additional data
1371                  * as well.
1372                  */
1373                 eresult = dns_rdataset_additionaldata(trdataset,
1374                                                       query_addadditional,
1375                                                       client);
1376         }
1377
1378  cleanup:
1379         CTRACE("query_addadditional: cleanup");
1380         query_putrdataset(client, &rdataset);
1381         if (sigrdataset != NULL)
1382                 query_putrdataset(client, &sigrdataset);
1383         if (fname != NULL)
1384                 query_releasename(client, &fname);
1385         if (node != NULL)
1386                 dns_db_detachnode(db, &node);
1387         if (db != NULL)
1388                 dns_db_detach(&db);
1389         if (zone != NULL)
1390                 dns_zone_detach(&zone);
1391
1392         CTRACE("query_addadditional: done");
1393         return (eresult);
1394 }
1395
1396 static inline void
1397 query_discardcache(ns_client_t *client, dns_rdataset_t *rdataset_base,
1398                    dns_rdatasetadditional_t additionaltype,
1399                    dns_rdatatype_t type, dns_zone_t **zonep, dns_db_t **dbp,
1400                    dns_dbversion_t **versionp, dns_dbnode_t **nodep,
1401                    dns_name_t *fname)
1402 {
1403         dns_rdataset_t *rdataset;
1404
1405         while  ((rdataset = ISC_LIST_HEAD(fname->list)) != NULL) {
1406                 ISC_LIST_UNLINK(fname->list, rdataset, link);
1407                 query_putrdataset(client, &rdataset);
1408         }
1409         if (*versionp != NULL)
1410                 dns_db_closeversion(*dbp, versionp, ISC_FALSE);
1411         if (*nodep != NULL)
1412                 dns_db_detachnode(*dbp, nodep);
1413         if (*dbp != NULL)
1414                 dns_db_detach(dbp);
1415         if (*zonep != NULL)
1416                 dns_zone_detach(zonep);
1417         (void)dns_rdataset_putadditional(client->view->acache, rdataset_base,
1418                                          additionaltype, type);
1419 }
1420
1421 static inline isc_result_t
1422 query_iscachevalid(dns_zone_t *zone, dns_db_t *db, dns_db_t *db0,
1423                    dns_dbversion_t *version)
1424 {
1425         isc_result_t result = ISC_R_SUCCESS;
1426         dns_dbversion_t *version_current = NULL;
1427         dns_db_t *db_current = db0;
1428
1429         if (db_current == NULL) {
1430                 result = dns_zone_getdb(zone, &db_current);
1431                 if (result != ISC_R_SUCCESS)
1432                         return (result);
1433         }
1434         dns_db_currentversion(db_current, &version_current);
1435         if (db_current != db || version_current != version) {
1436                 result = ISC_R_FAILURE;
1437                 goto cleanup;
1438         }
1439
1440  cleanup:
1441         dns_db_closeversion(db_current, &version_current, ISC_FALSE);
1442         if (db0 == NULL && db_current != NULL)
1443                 dns_db_detach(&db_current);
1444
1445         return (result);
1446 }
1447
1448 static isc_result_t
1449 query_addadditional2(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
1450         client_additionalctx_t *additionalctx = arg;
1451         dns_rdataset_t *rdataset_base;
1452         ns_client_t *client;
1453         isc_result_t result, eresult;
1454         dns_dbnode_t *node, *cnode;
1455         dns_db_t *db, *cdb;
1456         dns_name_t *fname, *mname0, cfname;
1457         dns_rdataset_t *rdataset, *sigrdataset;
1458         dns_rdataset_t *crdataset, *crdataset_next;
1459         isc_buffer_t *dbuf;
1460         isc_buffer_t b;
1461         dns_dbversion_t *version, *cversion;
1462         isc_boolean_t added_something, need_addname, needadditionalcache;
1463         isc_boolean_t need_sigrrset;
1464         dns_zone_t *zone;
1465         dns_rdatatype_t type;
1466         dns_rdatasetadditional_t additionaltype;
1467
1468         if (qtype != dns_rdatatype_a) {
1469                 /*
1470                  * This function is optimized for "address" types.  For other
1471                  * types, use a generic routine.
1472                  * XXX: ideally, this function should be generic enough.
1473                  */
1474                 return (query_addadditional(additionalctx->client,
1475                                             name, qtype));
1476         }
1477
1478         /*
1479          * Initialization.
1480          */
1481         rdataset_base = additionalctx->rdataset;
1482         client = additionalctx->client;
1483         REQUIRE(NS_CLIENT_VALID(client));
1484         eresult = ISC_R_SUCCESS;
1485         fname = NULL;
1486         rdataset = NULL;
1487         sigrdataset = NULL;
1488         db = NULL;
1489         cdb = NULL;
1490         version = NULL;
1491         cversion = NULL;
1492         node = NULL;
1493         cnode = NULL;
1494         added_something = ISC_FALSE;
1495         need_addname = ISC_FALSE;
1496         zone = NULL;
1497         needadditionalcache = ISC_FALSE;
1498         additionaltype = dns_rdatasetadditional_fromauth;
1499         dns_name_init(&cfname, NULL);
1500
1501         CTRACE("query_addadditional2");
1502
1503         /*
1504          * We treat type A additional section processing as if it
1505          * were "any address type" additional section processing.
1506          * To avoid multiple lookups, we do an 'any' database
1507          * lookup and iterate over the node.
1508          * XXXJT: this approach can cause a suboptimal result when the cache
1509          * DB only has partial address types and the glue DB has remaining
1510          * ones.
1511          */
1512         type = dns_rdatatype_any;
1513
1514         /*
1515          * Get some resources.
1516          */
1517         dbuf = query_getnamebuf(client);
1518         if (dbuf == NULL)
1519                 goto cleanup;
1520         fname = query_newname(client, dbuf, &b);
1521         if (fname == NULL)
1522                 goto cleanup;
1523         dns_name_setbuffer(&cfname, &b); /* share the buffer */
1524
1525         /* Check additional cache */
1526         result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1527                                             type, client->view->acache, &zone,
1528                                             &cdb, &cversion, &cnode, &cfname,
1529                                             client->message, client->now);
1530         if (result != ISC_R_SUCCESS)
1531                 goto findauthdb;
1532         if (zone == NULL) {
1533                 CTRACE("query_addadditional2: auth zone not found");
1534                 goto try_cache;
1535         }
1536
1537         /* Is the cached DB up-to-date? */
1538         result = query_iscachevalid(zone, cdb, NULL, cversion);
1539         if (result != ISC_R_SUCCESS) {
1540                 CTRACE("query_addadditional2: old auth additional cache");
1541                 query_discardcache(client, rdataset_base, additionaltype,
1542                                    type, &zone, &cdb, &cversion, &cnode,
1543                                    &cfname);
1544                 goto findauthdb;
1545         }
1546
1547         if (cnode == NULL) {
1548                 /*
1549                  * We have a negative cache.  We don't have to check the zone
1550                  * ACL, since the result (not using this zone) would be same
1551                  * regardless of the result.
1552                  */
1553                 CTRACE("query_addadditional2: negative auth additional cache");
1554                 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1555                 dns_db_detach(&cdb);
1556                 dns_zone_detach(&zone);
1557                 goto try_cache;
1558         }
1559
1560         result = query_validatezonedb(client, name, qtype, DNS_GETDB_NOLOG,
1561                                       zone, cdb, NULL);
1562         if (result != ISC_R_SUCCESS) {
1563                 query_discardcache(client, rdataset_base, additionaltype,
1564                                    type, &zone, &cdb, &cversion, &cnode,
1565                                    &cfname);
1566                 goto try_cache;
1567         }
1568
1569         /* We've got an active cache. */
1570         CTRACE("query_addadditional2: auth additional cache");
1571         dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1572         db = cdb;
1573         node = cnode;
1574         dns_name_clone(&cfname, fname);
1575         query_keepname(client, fname, dbuf);
1576         goto foundcache;
1577
1578         /*
1579          * Look for a zone database that might contain authoritative
1580          * additional data.
1581          */
1582  findauthdb:
1583         result = query_getzonedb(client, name, qtype, DNS_GETDB_NOLOG,
1584                                  &zone, &db, &version);
1585         if (result != ISC_R_SUCCESS) {
1586                 /* Cache the negative result */
1587                 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1588                                                  type, client->view->acache,
1589                                                  NULL, NULL, NULL, NULL,
1590                                                  NULL);
1591                 goto try_cache;
1592         }
1593
1594         CTRACE("query_addadditional2: db_find");
1595
1596         /*
1597          * Since we are looking for authoritative data, we do not set
1598          * the GLUEOK flag.  Glue will be looked for later, but not
1599          * necessarily in the same database.
1600          */
1601         node = NULL;
1602         result = dns_db_find(db, name, version, type, client->query.dboptions,
1603                              client->now, &node, fname, NULL, NULL);
1604         if (result == ISC_R_SUCCESS)
1605                 goto found;
1606
1607         /* Cache the negative result */
1608         (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1609                                          type, client->view->acache, zone, db,
1610                                          version, NULL, fname);
1611
1612         if (node != NULL)
1613                 dns_db_detachnode(db, &node);
1614         version = NULL;
1615         dns_db_detach(&db);
1616
1617         /*
1618          * No authoritative data was found.  The cache is our next best bet.
1619          */
1620
1621  try_cache:
1622         additionaltype = dns_rdatasetadditional_fromcache;
1623         result = query_getcachedb(client, name, qtype, &db, DNS_GETDB_NOLOG);
1624         if (result != ISC_R_SUCCESS)
1625                 /*
1626                  * Most likely the client isn't allowed to query the cache.
1627                  */
1628                 goto try_glue;
1629
1630         result = dns_db_find(db, name, version, type,
1631                              client->query.dboptions | DNS_DBFIND_GLUEOK,
1632                              client->now, &node, fname, NULL, NULL);
1633         if (result == ISC_R_SUCCESS)
1634                 goto found;
1635
1636         if (node != NULL)
1637                 dns_db_detachnode(db, &node);
1638         dns_db_detach(&db);
1639
1640  try_glue:
1641         /*
1642          * No cached data was found.  Glue is our last chance.
1643          * RFC1035 sayeth:
1644          *
1645          *      NS records cause both the usual additional section
1646          *      processing to locate a type A record, and, when used
1647          *      in a referral, a special search of the zone in which
1648          *      they reside for glue information.
1649          *
1650          * This is the "special search".  Note that we must search
1651          * the zone where the NS record resides, not the zone it
1652          * points to, and that we only do the search in the delegation
1653          * case (identified by client->query.gluedb being set).
1654          */
1655         if (client->query.gluedb == NULL)
1656                 goto cleanup;
1657
1658         /*
1659          * Don't poison caches using the bailiwick protection model.
1660          */
1661         if (!dns_name_issubdomain(name, dns_db_origin(client->query.gluedb)))
1662                 goto cleanup;
1663
1664         /* Check additional cache */
1665         additionaltype = dns_rdatasetadditional_fromglue;
1666         result = dns_rdataset_getadditional(rdataset_base, additionaltype,
1667                                             type, client->view->acache, NULL,
1668                                             &cdb, &cversion, &cnode, &cfname,
1669                                             client->message, client->now);
1670         if (result != ISC_R_SUCCESS)
1671                 goto findglue;
1672
1673         result = query_iscachevalid(zone, cdb, client->query.gluedb, cversion);
1674         if (result != ISC_R_SUCCESS) {
1675                 CTRACE("query_addadditional2: old glue additional cache");
1676                 query_discardcache(client, rdataset_base, additionaltype,
1677                                    type, &zone, &cdb, &cversion, &cnode,
1678                                    &cfname);
1679                 goto findglue;
1680         }
1681
1682         if (cnode == NULL) {
1683                 /* We have a negative cache. */
1684                 CTRACE("query_addadditional2: negative glue additional cache");
1685                 dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1686                 dns_db_detach(&cdb);
1687                 goto cleanup;
1688         }
1689
1690         /* Cache hit. */
1691         CTRACE("query_addadditional2: glue additional cache");
1692         dns_db_closeversion(cdb, &cversion, ISC_FALSE);
1693         db = cdb;
1694         node = cnode;
1695         dns_name_clone(&cfname, fname);
1696         query_keepname(client, fname, dbuf);
1697         goto foundcache;
1698
1699  findglue:
1700         dns_db_attach(client->query.gluedb, &db);
1701         result = dns_db_find(db, name, version, type,
1702                              client->query.dboptions | DNS_DBFIND_GLUEOK,
1703                              client->now, &node, fname, NULL, NULL);
1704         if (!(result == ISC_R_SUCCESS ||
1705               result == DNS_R_ZONECUT ||
1706               result == DNS_R_GLUE)) {
1707                 /* cache the negative result */
1708                 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1709                                                  type, client->view->acache,
1710                                                  NULL, db, version, NULL,
1711                                                  fname);
1712                 goto cleanup;
1713         }
1714
1715  found:
1716         /*
1717          * We have found a DB node to iterate over from a DB.
1718          * We are going to look for address RRsets (i.e., A and AAAA) in the DB
1719          * node we've just found.  We'll then store the complete information
1720          * in the additional data cache.
1721          */
1722         dns_name_clone(fname, &cfname);
1723         query_keepname(client, fname, dbuf);
1724         needadditionalcache = ISC_TRUE;
1725
1726         rdataset = query_newrdataset(client);
1727         if (rdataset == NULL)
1728                 goto cleanup;
1729
1730         sigrdataset = query_newrdataset(client);
1731         if (sigrdataset == NULL)
1732                 goto cleanup;
1733
1734         /*
1735          * Find A RRset with sig RRset.  Even if we don't find a sig RRset
1736          * for a client using DNSSEC, we'll continue the process to make a
1737          * complete list to be cached.  However, we need to cancel the
1738          * caching when something unexpected happens, in order to avoid
1739          * caching incomplete information.
1740          */
1741         result = dns_db_findrdataset(db, node, version, dns_rdatatype_a, 0,
1742                                      client->now, rdataset, sigrdataset);
1743         /*
1744          * If we can't promote glue/pending from the cache to secure
1745          * then drop it.
1746          */
1747         if (result == ISC_R_SUCCESS &&
1748             additionaltype == dns_rdatasetadditional_fromcache &&
1749             (DNS_TRUST_PENDING(rdataset->trust) ||
1750              DNS_TRUST_GLUE(rdataset->trust)) &&
1751             !validate(client, db, fname, rdataset, sigrdataset)) {
1752                 dns_rdataset_disassociate(rdataset);
1753                 if (dns_rdataset_isassociated(sigrdataset))
1754                         dns_rdataset_disassociate(sigrdataset);
1755                 result = ISC_R_NOTFOUND;
1756         }
1757         if (result == DNS_R_NCACHENXDOMAIN)
1758                 goto setcache;
1759         if (result == DNS_R_NCACHENXRRSET) {
1760                 dns_rdataset_disassociate(rdataset);
1761                 /*
1762                  * Negative cache entries don't have sigrdatasets.
1763                  */
1764                 INSIST(! dns_rdataset_isassociated(sigrdataset));
1765         }
1766         if (result == ISC_R_SUCCESS) {
1767                 /* Remember the result as a cache */
1768                 ISC_LIST_APPEND(cfname.list, rdataset, link);
1769                 if (dns_rdataset_isassociated(sigrdataset)) {
1770                         ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1771                         sigrdataset = query_newrdataset(client);
1772                 }
1773                 rdataset = query_newrdataset(client);
1774                 if (sigrdataset == NULL || rdataset == NULL) {
1775                         /* do not cache incomplete information */
1776                         goto foundcache;
1777                 }
1778         }
1779
1780         /* Find AAAA RRset with sig RRset */
1781         result = dns_db_findrdataset(db, node, version, dns_rdatatype_aaaa,
1782                                      0, client->now, rdataset, sigrdataset);
1783         /*
1784          * If we can't promote glue/pending from the cache to secure
1785          * then drop it.
1786          */
1787         if (result == ISC_R_SUCCESS &&
1788             additionaltype == dns_rdatasetadditional_fromcache &&
1789             (DNS_TRUST_PENDING(rdataset->trust) ||
1790              DNS_TRUST_GLUE(rdataset->trust)) &&
1791             !validate(client, db, fname, rdataset, sigrdataset)) {
1792                 dns_rdataset_disassociate(rdataset);
1793                 if (dns_rdataset_isassociated(sigrdataset))
1794                         dns_rdataset_disassociate(sigrdataset);
1795                 result = ISC_R_NOTFOUND;
1796         }
1797         if (result == ISC_R_SUCCESS) {
1798                 ISC_LIST_APPEND(cfname.list, rdataset, link);
1799                 rdataset = NULL;
1800                 if (dns_rdataset_isassociated(sigrdataset)) {
1801                         ISC_LIST_APPEND(cfname.list, sigrdataset, link);
1802                         sigrdataset = NULL;
1803                 }
1804         }
1805
1806  setcache:
1807         /*
1808          * Set the new result in the cache if required.  We do not support
1809          * caching additional data from a cache DB.
1810          */
1811         if (needadditionalcache == ISC_TRUE &&
1812             (additionaltype == dns_rdatasetadditional_fromauth ||
1813              additionaltype == dns_rdatasetadditional_fromglue)) {
1814                 (void)dns_rdataset_setadditional(rdataset_base, additionaltype,
1815                                                  type, client->view->acache,
1816                                                  zone, db, version, node,
1817                                                  &cfname);
1818         }
1819
1820  foundcache:
1821         need_sigrrset = ISC_FALSE;
1822         mname0 = NULL;
1823         for (crdataset = ISC_LIST_HEAD(cfname.list);
1824              crdataset != NULL;
1825              crdataset = crdataset_next) {
1826                 dns_name_t *mname;
1827
1828                 crdataset_next = ISC_LIST_NEXT(crdataset, link);
1829
1830                 mname = NULL;
1831                 if (crdataset->type == dns_rdatatype_a ||
1832                     crdataset->type == dns_rdatatype_aaaa) {
1833                         if (!query_isduplicate(client, fname, crdataset->type,
1834                                                &mname)) {
1835                                 if (mname != NULL) {
1836                                         /*
1837                                          * A different type of this name is
1838                                          * already stored in the additional
1839                                          * section.  We'll reuse the name.
1840                                          * Note that this should happen at most
1841                                          * once.  Otherwise, fname->link could
1842                                          * leak below.
1843                                          */
1844                                         INSIST(mname0 == NULL);
1845
1846                                         query_releasename(client, &fname);
1847                                         fname = mname;
1848                                         mname0 = mname;
1849                                 } else
1850                                         need_addname = ISC_TRUE;
1851                                 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1852                                 ISC_LIST_APPEND(fname->list, crdataset, link);
1853                                 added_something = ISC_TRUE;
1854                                 need_sigrrset = ISC_TRUE;
1855                         } else
1856                                 need_sigrrset = ISC_FALSE;
1857                 } else if (crdataset->type == dns_rdatatype_rrsig &&
1858                            need_sigrrset && WANTDNSSEC(client)) {
1859                         ISC_LIST_UNLINK(cfname.list, crdataset, link);
1860                         ISC_LIST_APPEND(fname->list, crdataset, link);
1861                         added_something = ISC_TRUE; /* just in case */
1862                         need_sigrrset = ISC_FALSE;
1863                 }
1864         }
1865
1866         CTRACE("query_addadditional2: addname");
1867
1868         /*
1869          * If we haven't added anything, then we're done.
1870          */
1871         if (!added_something)
1872                 goto cleanup;
1873
1874         /*
1875          * We may have added our rdatasets to an existing name, if so, then
1876          * need_addname will be ISC_FALSE.  Whether we used an existing name
1877          * or a new one, we must set fname to NULL to prevent cleanup.
1878          */
1879         if (need_addname)
1880                 dns_message_addname(client->message, fname,
1881                                     DNS_SECTION_ADDITIONAL);
1882         fname = NULL;
1883
1884  cleanup:
1885         CTRACE("query_addadditional2: cleanup");
1886
1887         if (rdataset != NULL)
1888                 query_putrdataset(client, &rdataset);
1889         if (sigrdataset != NULL)
1890                 query_putrdataset(client, &sigrdataset);
1891         while  ((crdataset = ISC_LIST_HEAD(cfname.list)) != NULL) {
1892                 ISC_LIST_UNLINK(cfname.list, crdataset, link);
1893                 query_putrdataset(client, &crdataset);
1894         }
1895         if (fname != NULL)
1896                 query_releasename(client, &fname);
1897         if (node != NULL)
1898                 dns_db_detachnode(db, &node);
1899         if (db != NULL)
1900                 dns_db_detach(&db);
1901         if (zone != NULL)
1902                 dns_zone_detach(&zone);
1903
1904         CTRACE("query_addadditional2: done");
1905         return (eresult);
1906 }
1907
1908 static inline void
1909 query_addrdataset(ns_client_t *client, dns_name_t *fname,
1910                   dns_rdataset_t *rdataset)
1911 {
1912         client_additionalctx_t additionalctx;
1913
1914         /*
1915          * Add 'rdataset' and any pertinent additional data to
1916          * 'fname', a name in the response message for 'client'.
1917          */
1918
1919         CTRACE("query_addrdataset");
1920
1921         ISC_LIST_APPEND(fname->list, rdataset, link);
1922
1923         if (client->view->order != NULL)
1924                 rdataset->attributes |= dns_order_find(client->view->order,
1925                                                        fname, rdataset->type,
1926                                                        rdataset->rdclass);
1927         rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
1928
1929         if (NOADDITIONAL(client))
1930                 return;
1931
1932         /*
1933          * Add additional data.
1934          *
1935          * We don't care if dns_rdataset_additionaldata() fails.
1936          */
1937         additionalctx.client = client;
1938         additionalctx.rdataset = rdataset;
1939         (void)dns_rdataset_additionaldata(rdataset, query_addadditional2,
1940                                           &additionalctx);
1941         CTRACE("query_addrdataset: done");
1942 }
1943
1944 static void
1945 query_addrrset(ns_client_t *client, dns_name_t **namep,
1946                dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp,
1947                isc_buffer_t *dbuf, dns_section_t section)
1948 {
1949         dns_name_t *name, *mname;
1950         dns_rdataset_t *rdataset, *mrdataset, *sigrdataset;
1951         isc_result_t result;
1952
1953         /*%
1954          * To the current response for 'client', add the answer RRset
1955          * '*rdatasetp' and an optional signature set '*sigrdatasetp', with
1956          * owner name '*namep', to section 'section', unless they are
1957          * already there.  Also add any pertinent additional data.
1958          *
1959          * If 'dbuf' is not NULL, then '*namep' is the name whose data is
1960          * stored in 'dbuf'.  In this case, query_addrrset() guarantees that
1961          * when it returns the name will either have been kept or released.
1962          */
1963         CTRACE("query_addrrset");
1964         name = *namep;
1965         rdataset = *rdatasetp;
1966         if (sigrdatasetp != NULL)
1967                 sigrdataset = *sigrdatasetp;
1968         else
1969                 sigrdataset = NULL;
1970         mname = NULL;
1971         mrdataset = NULL;
1972         result = dns_message_findname(client->message, section,
1973                                       name, rdataset->type, rdataset->covers,
1974                                       &mname, &mrdataset);
1975         if (result == ISC_R_SUCCESS) {
1976                 /*
1977                  * We've already got an RRset of the given name and type.
1978                  * There's nothing else to do;
1979                  */
1980                 CTRACE("query_addrrset: dns_message_findname succeeded: done");
1981                 if (dbuf != NULL)
1982                         query_releasename(client, namep);
1983                 return;
1984         } else if (result == DNS_R_NXDOMAIN) {
1985                 /*
1986                  * The name doesn't exist.
1987                  */
1988                 if (dbuf != NULL)
1989                         query_keepname(client, name, dbuf);
1990                 dns_message_addname(client->message, name, section);
1991                 *namep = NULL;
1992                 mname = name;
1993         } else {
1994                 RUNTIME_CHECK(result == DNS_R_NXRRSET);
1995                 if (dbuf != NULL)
1996                         query_releasename(client, namep);
1997         }
1998
1999         if (rdataset->trust != dns_trust_secure &&
2000             (section == DNS_SECTION_ANSWER ||
2001              section == DNS_SECTION_AUTHORITY))
2002                 client->query.attributes &= ~NS_QUERYATTR_SECURE;
2003         /*
2004          * Note: we only add SIGs if we've added the type they cover, so
2005          * we do not need to check if the SIG rdataset is already in the
2006          * response.
2007          */
2008         query_addrdataset(client, mname, rdataset);
2009         *rdatasetp = NULL;
2010         if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset)) {
2011                 /*
2012                  * We have a signature.  Add it to the response.
2013                  */
2014                 ISC_LIST_APPEND(mname->list, sigrdataset, link);
2015                 *sigrdatasetp = NULL;
2016         }
2017         CTRACE("query_addrrset: done");
2018 }
2019
2020 static inline isc_result_t
2021 query_addsoa(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version,
2022              isc_boolean_t zero_ttl)
2023 {
2024         dns_name_t *name;
2025         dns_dbnode_t *node;
2026         isc_result_t result, eresult;
2027         dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2028         dns_rdataset_t **sigrdatasetp = NULL;
2029
2030         CTRACE("query_addsoa");
2031         /*
2032          * Initialization.
2033          */
2034         eresult = ISC_R_SUCCESS;
2035         name = NULL;
2036         rdataset = NULL;
2037         node = NULL;
2038
2039         /*
2040          * Get resources and make 'name' be the database origin.
2041          */
2042         result = dns_message_gettempname(client->message, &name);
2043         if (result != ISC_R_SUCCESS)
2044                 return (result);
2045         dns_name_init(name, NULL);
2046         dns_name_clone(dns_db_origin(db), name);
2047         rdataset = query_newrdataset(client);
2048         if (rdataset == NULL) {
2049                 eresult = DNS_R_SERVFAIL;
2050                 goto cleanup;
2051         }
2052         if (WANTDNSSEC(client)) {
2053                 sigrdataset = query_newrdataset(client);
2054                 if (sigrdataset == NULL) {
2055                         eresult = DNS_R_SERVFAIL;
2056                         goto cleanup;
2057                 }
2058         }
2059
2060         /*
2061          * Find the SOA.
2062          */
2063         result = dns_db_getoriginnode(db, &node);
2064         if (result == ISC_R_SUCCESS) {
2065                 result = dns_db_findrdataset(db, node, version,
2066                                              dns_rdatatype_soa,
2067                                              0, client->now, rdataset,
2068                                              sigrdataset);
2069         } else {
2070                 dns_fixedname_t foundname;
2071                 dns_name_t *fname;
2072
2073                 dns_fixedname_init(&foundname);
2074                 fname = dns_fixedname_name(&foundname);
2075
2076                 result = dns_db_find(db, name, version, dns_rdatatype_soa,
2077                                      client->query.dboptions, 0, &node,
2078                                      fname, rdataset, sigrdataset);
2079         }
2080         if (result != ISC_R_SUCCESS) {
2081                 /*
2082                  * This is bad.  We tried to get the SOA RR at the zone top
2083                  * and it didn't work!
2084                  */
2085                 eresult = DNS_R_SERVFAIL;
2086         } else {
2087                 /*
2088                  * Extract the SOA MINIMUM.
2089                  */
2090                 dns_rdata_soa_t soa;
2091                 dns_rdata_t rdata = DNS_RDATA_INIT;
2092                 result = dns_rdataset_first(rdataset);
2093                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2094                 dns_rdataset_current(rdataset, &rdata);
2095                 result = dns_rdata_tostruct(&rdata, &soa, NULL);
2096                 if (result != ISC_R_SUCCESS)
2097                         goto cleanup;
2098
2099                 if (zero_ttl) {
2100                         rdataset->ttl = 0;
2101                         if (sigrdataset != NULL)
2102                                 sigrdataset->ttl = 0;
2103                 }
2104
2105                 /*
2106                  * Add the SOA and its SIG to the response, with the
2107                  * TTLs adjusted per RFC2308 section 3.
2108                  */
2109                 if (rdataset->ttl > soa.minimum)
2110                         rdataset->ttl = soa.minimum;
2111                 if (sigrdataset != NULL && sigrdataset->ttl > soa.minimum)
2112                         sigrdataset->ttl = soa.minimum;
2113
2114                 if (sigrdataset != NULL)
2115                         sigrdatasetp = &sigrdataset;
2116                 else
2117                         sigrdatasetp = NULL;
2118                 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2119                                DNS_SECTION_AUTHORITY);
2120         }
2121
2122  cleanup:
2123         query_putrdataset(client, &rdataset);
2124         if (sigrdataset != NULL)
2125                 query_putrdataset(client, &sigrdataset);
2126         if (name != NULL)
2127                 query_releasename(client, &name);
2128         if (node != NULL)
2129                 dns_db_detachnode(db, &node);
2130
2131         return (eresult);
2132 }
2133
2134 static inline isc_result_t
2135 query_addns(ns_client_t *client, dns_db_t *db, dns_dbversion_t *version) {
2136         dns_name_t *name, *fname;
2137         dns_dbnode_t *node;
2138         isc_result_t result, eresult;
2139         dns_fixedname_t foundname;
2140         dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
2141         dns_rdataset_t **sigrdatasetp = NULL;
2142
2143         CTRACE("query_addns");
2144         /*
2145          * Initialization.
2146          */
2147         eresult = ISC_R_SUCCESS;
2148         name = NULL;
2149         rdataset = NULL;
2150         node = NULL;
2151         dns_fixedname_init(&foundname);
2152         fname = dns_fixedname_name(&foundname);
2153
2154         /*
2155          * Get resources and make 'name' be the database origin.
2156          */
2157         result = dns_message_gettempname(client->message, &name);
2158         if (result != ISC_R_SUCCESS) {
2159                 CTRACE("query_addns: dns_message_gettempname failed: done");
2160                 return (result);
2161         }
2162         dns_name_init(name, NULL);
2163         dns_name_clone(dns_db_origin(db), name);
2164         rdataset = query_newrdataset(client);
2165         if (rdataset == NULL) {
2166                 CTRACE("query_addns: query_newrdataset failed");
2167                 eresult = DNS_R_SERVFAIL;
2168                 goto cleanup;
2169         }
2170         if (WANTDNSSEC(client)) {
2171                 sigrdataset = query_newrdataset(client);
2172                 if (sigrdataset == NULL) {
2173                         CTRACE("query_addns: query_newrdataset failed");
2174                         eresult = DNS_R_SERVFAIL;
2175                         goto cleanup;
2176                 }
2177         }
2178
2179         /*
2180          * Find the NS rdataset.
2181          */
2182         result = dns_db_getoriginnode(db, &node);
2183         if (result == ISC_R_SUCCESS) {
2184                 result = dns_db_findrdataset(db, node, version,
2185                                              dns_rdatatype_ns,
2186                                              0, client->now, rdataset,
2187                                              sigrdataset);
2188         } else {
2189                 CTRACE("query_addns: calling dns_db_find");
2190                 result = dns_db_find(db, name, NULL, dns_rdatatype_ns,
2191                                      client->query.dboptions, 0, &node,
2192                                      fname, rdataset, sigrdataset);
2193                 CTRACE("query_addns: dns_db_find complete");
2194         }
2195         if (result != ISC_R_SUCCESS) {
2196                 CTRACE("query_addns: "
2197                        "dns_db_findrdataset or dns_db_find failed");
2198                 /*
2199                  * This is bad.  We tried to get the NS rdataset at the zone
2200                  * top and it didn't work!
2201                  */
2202                 eresult = DNS_R_SERVFAIL;
2203         } else {
2204                 if (sigrdataset != NULL)
2205                         sigrdatasetp = &sigrdataset;
2206                 else
2207                         sigrdatasetp = NULL;
2208                 query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
2209                                DNS_SECTION_AUTHORITY);
2210         }
2211
2212  cleanup:
2213         CTRACE("query_addns: cleanup");
2214         query_putrdataset(client, &rdataset);
2215         if (sigrdataset != NULL)
2216                 query_putrdataset(client, &sigrdataset);
2217         if (name != NULL)
2218                 query_releasename(client, &name);
2219         if (node != NULL)
2220                 dns_db_detachnode(db, &node);
2221
2222         CTRACE("query_addns: done");
2223         return (eresult);
2224 }
2225
2226 static inline isc_result_t
2227 query_addcnamelike(ns_client_t *client, dns_name_t *qname, dns_name_t *tname,
2228                    dns_trust_t trust, dns_name_t **anamep, dns_rdatatype_t type)
2229 {
2230         dns_rdataset_t *rdataset;
2231         dns_rdatalist_t *rdatalist;
2232         dns_rdata_t *rdata;
2233         isc_result_t result;
2234         isc_region_t r;
2235
2236         /*
2237          * We assume the name data referred to by tname won't go away.
2238          */
2239
2240         REQUIRE(anamep != NULL);
2241
2242         rdatalist = NULL;
2243         result = dns_message_gettemprdatalist(client->message, &rdatalist);
2244         if (result != ISC_R_SUCCESS)
2245                 return (result);
2246         rdata = NULL;
2247         result = dns_message_gettemprdata(client->message, &rdata);
2248         if (result != ISC_R_SUCCESS)
2249                 return (result);
2250         rdataset = NULL;
2251         result = dns_message_gettemprdataset(client->message, &rdataset);
2252         if (result != ISC_R_SUCCESS)
2253                 return (result);
2254         dns_rdataset_init(rdataset);
2255         result = dns_name_dup(qname, client->mctx, *anamep);
2256         if (result != ISC_R_SUCCESS) {
2257                 dns_message_puttemprdataset(client->message, &rdataset);
2258                 return (result);
2259         }
2260
2261         rdatalist->type = type;
2262         rdatalist->covers = 0;
2263         rdatalist->rdclass = client->message->rdclass;
2264         rdatalist->ttl = 0;
2265
2266         dns_name_toregion(tname, &r);
2267         rdata->data = r.base;
2268         rdata->length = r.length;
2269         rdata->rdclass = client->message->rdclass;
2270         rdata->type = type;
2271
2272         ISC_LIST_INIT(rdatalist->rdata);
2273         ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
2274         RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
2275                       == ISC_R_SUCCESS);
2276         rdataset->trust = trust;
2277
2278         query_addrrset(client, anamep, &rdataset, NULL, NULL,
2279                        DNS_SECTION_ANSWER);
2280
2281         if (rdataset != NULL) {
2282                 if (dns_rdataset_isassociated(rdataset))
2283                         dns_rdataset_disassociate(rdataset);
2284                 dns_message_puttemprdataset(client->message, &rdataset);
2285         }
2286
2287         return (ISC_R_SUCCESS);
2288 }
2289
2290 /*
2291  * Mark the RRsets as secure.  Update the cache (db) to reflect the
2292  * change in trust level.
2293  */
2294 static void
2295 mark_secure(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2296             dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2297 {
2298         isc_result_t result;
2299         dns_dbnode_t *node = NULL;
2300
2301         rdataset->trust = dns_trust_secure;
2302         sigrdataset->trust = dns_trust_secure;
2303
2304         /*
2305          * Save the updated secure state.  Ignore failures.
2306          */
2307         result = dns_db_findnode(db, name, ISC_TRUE, &node);
2308         if (result != ISC_R_SUCCESS)
2309                 return;
2310         (void)dns_db_addrdataset(db, node, NULL, client->now, rdataset,
2311                                  0, NULL);
2312         (void)dns_db_addrdataset(db, node, NULL, client->now, sigrdataset,
2313                                  0, NULL);
2314         dns_db_detachnode(db, &node);
2315 }
2316
2317 /*
2318  * Find the secure key that corresponds to rrsig.
2319  * Note: 'keyrdataset' maintains state between successive calls,
2320  * there may be multiple keys with the same keyid.
2321  * Return ISC_FALSE if we have exhausted all the possible keys.
2322  */
2323 static isc_boolean_t
2324 get_key(ns_client_t *client, dns_db_t *db, dns_rdata_rrsig_t *rrsig,
2325         dns_rdataset_t *keyrdataset, dst_key_t **keyp)
2326 {
2327         isc_result_t result;
2328         dns_dbnode_t *node = NULL;
2329         isc_boolean_t secure = ISC_FALSE;
2330
2331         if (!dns_rdataset_isassociated(keyrdataset)) {
2332                 result = dns_db_findnode(db, &rrsig->signer, ISC_FALSE, &node);
2333                 if (result != ISC_R_SUCCESS)
2334                         return (ISC_FALSE);
2335
2336                 result = dns_db_findrdataset(db, node, NULL,
2337                                              dns_rdatatype_dnskey, 0,
2338                                              client->now, keyrdataset, NULL);
2339                 dns_db_detachnode(db, &node);
2340                 if (result != ISC_R_SUCCESS)
2341                         return (ISC_FALSE);
2342
2343                 if (keyrdataset->trust != dns_trust_secure)
2344                         return (ISC_FALSE);
2345
2346                 result = dns_rdataset_first(keyrdataset);
2347         } else
2348                 result = dns_rdataset_next(keyrdataset);
2349
2350         for ( ; result == ISC_R_SUCCESS;
2351              result = dns_rdataset_next(keyrdataset)) {
2352                 dns_rdata_t rdata = DNS_RDATA_INIT;
2353                 isc_buffer_t b;
2354
2355                 dns_rdataset_current(keyrdataset, &rdata);
2356                 isc_buffer_init(&b, rdata.data, rdata.length);
2357                 isc_buffer_add(&b, rdata.length);
2358                 result = dst_key_fromdns(&rrsig->signer, rdata.rdclass, &b,
2359                                          client->mctx, keyp);
2360                 if (result != ISC_R_SUCCESS)
2361                         continue;
2362                 if (rrsig->algorithm == (dns_secalg_t)dst_key_alg(*keyp) &&
2363                     rrsig->keyid == (dns_keytag_t)dst_key_id(*keyp) &&
2364                     dst_key_iszonekey(*keyp)) {
2365                         secure = ISC_TRUE;
2366                         break;
2367                 }
2368                 dst_key_free(keyp);
2369         }
2370         return (secure);
2371 }
2372
2373 static isc_boolean_t
2374 verify(dst_key_t *key, dns_name_t *name, dns_rdataset_t *rdataset,
2375        dns_rdata_t *rdata, isc_mem_t *mctx, isc_boolean_t acceptexpired)
2376 {
2377         isc_result_t result;
2378         dns_fixedname_t fixed;
2379         isc_boolean_t ignore = ISC_FALSE;
2380
2381         dns_fixedname_init(&fixed);
2382
2383 again:
2384         result = dns_dnssec_verify2(name, rdataset, key, ignore, mctx,
2385                                     rdata, NULL);
2386         if (result == DNS_R_SIGEXPIRED && acceptexpired) {
2387                 ignore = ISC_TRUE;
2388                 goto again;
2389         }
2390         if (result == ISC_R_SUCCESS || result == DNS_R_FROMWILDCARD)
2391                 return (ISC_TRUE);
2392         return (ISC_FALSE);
2393 }
2394
2395 /*
2396  * Validate the rdataset if possible with available records.
2397  */
2398 static isc_boolean_t
2399 validate(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2400          dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
2401 {
2402         isc_result_t result;
2403         dns_rdata_t rdata = DNS_RDATA_INIT;
2404         dns_rdata_rrsig_t rrsig;
2405         dst_key_t *key = NULL;
2406         dns_rdataset_t keyrdataset;
2407
2408         if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2409                 return (ISC_FALSE);
2410
2411         for (result = dns_rdataset_first(sigrdataset);
2412              result == ISC_R_SUCCESS;
2413              result = dns_rdataset_next(sigrdataset)) {
2414
2415                 dns_rdata_reset(&rdata);
2416                 dns_rdataset_current(sigrdataset, &rdata);
2417                 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
2418                 if (result != ISC_R_SUCCESS)
2419                         return (ISC_FALSE);
2420                 if (!dns_resolver_algorithm_supported(client->view->resolver,
2421                                                       name, rrsig.algorithm))
2422                         continue;
2423                 if (!dns_name_issubdomain(name, &rrsig.signer))
2424                         continue;
2425                 dns_rdataset_init(&keyrdataset);
2426                 do {
2427                         if (!get_key(client, db, &rrsig, &keyrdataset, &key))
2428                                 break;
2429                         if (verify(key, name, rdataset, &rdata, client->mctx,
2430                                    client->view->acceptexpired)) {
2431                                 dst_key_free(&key);
2432                                 dns_rdataset_disassociate(&keyrdataset);
2433                                 mark_secure(client, db, name, rdataset,
2434                                             sigrdataset);
2435                                 return (ISC_TRUE);
2436                         }
2437                         dst_key_free(&key);
2438                 } while (1);
2439                 if (dns_rdataset_isassociated(&keyrdataset))
2440                         dns_rdataset_disassociate(&keyrdataset);
2441         }
2442         return (ISC_FALSE);
2443 }
2444
2445 static void
2446 query_addbestns(ns_client_t *client) {
2447         dns_db_t *db, *zdb;
2448         dns_dbnode_t *node;
2449         dns_name_t *fname, *zfname;
2450         dns_rdataset_t *rdataset, *sigrdataset, *zrdataset, *zsigrdataset;
2451         isc_boolean_t is_zone, use_zone;
2452         isc_buffer_t *dbuf;
2453         isc_result_t result;
2454         dns_dbversion_t *version;
2455         dns_zone_t *zone;
2456         isc_buffer_t b;
2457
2458         CTRACE("query_addbestns");
2459         fname = NULL;
2460         zfname = NULL;
2461         rdataset = NULL;
2462         zrdataset = NULL;
2463         sigrdataset = NULL;
2464         zsigrdataset = NULL;
2465         node = NULL;
2466         db = NULL;
2467         zdb = NULL;
2468         version = NULL;
2469         zone = NULL;
2470         is_zone = ISC_FALSE;
2471         use_zone = ISC_FALSE;
2472
2473         /*
2474          * Find the right database.
2475          */
2476         result = query_getdb(client, client->query.qname, dns_rdatatype_ns, 0,
2477                              &zone, &db, &version, &is_zone);
2478         if (result != ISC_R_SUCCESS)
2479                 goto cleanup;
2480
2481  db_find:
2482         /*
2483          * We'll need some resources...
2484          */
2485         dbuf = query_getnamebuf(client);
2486         if (dbuf == NULL)
2487                 goto cleanup;
2488         fname = query_newname(client, dbuf, &b);
2489         rdataset = query_newrdataset(client);
2490         if (fname == NULL || rdataset == NULL)
2491                 goto cleanup;
2492         /*
2493          * Get the RRSIGs if the client requested them or if we may
2494          * need to validate answers from the cache.
2495          */
2496         if (WANTDNSSEC(client) || !is_zone) {
2497                 sigrdataset = query_newrdataset(client);
2498                 if (sigrdataset == NULL)
2499                         goto cleanup;
2500         }
2501
2502         /*
2503          * Now look for the zonecut.
2504          */
2505         if (is_zone) {
2506                 result = dns_db_find(db, client->query.qname, version,
2507                                      dns_rdatatype_ns, client->query.dboptions,
2508                                      client->now, &node, fname,
2509                                      rdataset, sigrdataset);
2510                 if (result != DNS_R_DELEGATION)
2511                         goto cleanup;
2512                 if (USECACHE(client)) {
2513                         query_keepname(client, fname, dbuf);
2514                         zdb = db;
2515                         zfname = fname;
2516                         fname = NULL;
2517                         zrdataset = rdataset;
2518                         rdataset = NULL;
2519                         zsigrdataset = sigrdataset;
2520                         sigrdataset = NULL;
2521                         dns_db_detachnode(db, &node);
2522                         version = NULL;
2523                         db = NULL;
2524                         dns_db_attach(client->view->cachedb, &db);
2525                         is_zone = ISC_FALSE;
2526                         goto db_find;
2527                 }
2528         } else {
2529                 result = dns_db_findzonecut(db, client->query.qname,
2530                                             client->query.dboptions,
2531                                             client->now, &node, fname,
2532                                             rdataset, sigrdataset);
2533                 if (result == ISC_R_SUCCESS) {
2534                         if (zfname != NULL &&
2535                             !dns_name_issubdomain(fname, zfname)) {
2536                                 /*
2537                                  * We found a zonecut in the cache, but our
2538                                  * zone delegation is better.
2539                                  */
2540                                 use_zone = ISC_TRUE;
2541                         }
2542                 } else if (result == ISC_R_NOTFOUND && zfname != NULL) {
2543                         /*
2544                          * We didn't find anything in the cache, but we
2545                          * have a zone delegation, so use it.
2546                          */
2547                         use_zone = ISC_TRUE;
2548                 } else
2549                         goto cleanup;
2550         }
2551
2552         if (use_zone) {
2553                 query_releasename(client, &fname);
2554                 fname = zfname;
2555                 zfname = NULL;
2556                 /*
2557                  * We've already done query_keepname() on
2558                  * zfname, so we must set dbuf to NULL to
2559                  * prevent query_addrrset() from trying to
2560                  * call query_keepname() again.
2561                  */
2562                 dbuf = NULL;
2563                 query_putrdataset(client, &rdataset);
2564                 if (sigrdataset != NULL)
2565                         query_putrdataset(client, &sigrdataset);
2566                 rdataset = zrdataset;
2567                 zrdataset = NULL;
2568                 sigrdataset = zsigrdataset;
2569                 zsigrdataset = NULL;
2570         }
2571
2572         /*
2573          * Attempt to validate RRsets that are pending or that are glue.
2574          */
2575         if ((DNS_TRUST_PENDING(rdataset->trust) ||
2576              (sigrdataset != NULL && DNS_TRUST_PENDING(sigrdataset->trust)))
2577             && !validate(client, db, fname, rdataset, sigrdataset) &&
2578             !PENDINGOK(client->query.dboptions))
2579                 goto cleanup;
2580
2581         if ((DNS_TRUST_GLUE(rdataset->trust) ||
2582              (sigrdataset != NULL && DNS_TRUST_GLUE(sigrdataset->trust))) &&
2583             !validate(client, db, fname, rdataset, sigrdataset) &&
2584             SECURE(client) && WANTDNSSEC(client))
2585                 goto cleanup;
2586
2587         /*
2588          * If the client doesn't want DNSSEC we can discard the sigrdataset
2589          * now.
2590          */
2591         if (!WANTDNSSEC(client))
2592                 query_putrdataset(client, &sigrdataset);
2593         query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
2594                        DNS_SECTION_AUTHORITY);
2595
2596  cleanup:
2597         if (rdataset != NULL)
2598                 query_putrdataset(client, &rdataset);
2599         if (sigrdataset != NULL)
2600                 query_putrdataset(client, &sigrdataset);
2601         if (fname != NULL)
2602                 query_releasename(client, &fname);
2603         if (node != NULL)
2604                 dns_db_detachnode(db, &node);
2605         if (db != NULL)
2606                 dns_db_detach(&db);
2607         if (zone != NULL)
2608                 dns_zone_detach(&zone);
2609         if (zdb != NULL) {
2610                 query_putrdataset(client, &zrdataset);
2611                 if (zsigrdataset != NULL)
2612                         query_putrdataset(client, &zsigrdataset);
2613                 if (zfname != NULL)
2614                         query_releasename(client, &zfname);
2615                 dns_db_detach(&zdb);
2616         }
2617 }
2618
2619 static void
2620 query_addds(ns_client_t *client, dns_db_t *db, dns_dbnode_t *node,
2621             dns_dbversion_t *version)
2622 {
2623         dns_name_t *rname;
2624         dns_rdataset_t *rdataset, *sigrdataset;
2625         isc_result_t result;
2626
2627         CTRACE("query_addds");
2628         rname = NULL;
2629         rdataset = NULL;
2630         sigrdataset = NULL;
2631
2632         /*
2633          * We'll need some resources...
2634          */
2635         rdataset = query_newrdataset(client);
2636         sigrdataset = query_newrdataset(client);
2637         if (rdataset == NULL || sigrdataset == NULL)
2638                 goto cleanup;
2639
2640         /*
2641          * Look for the DS record, which may or may not be present.
2642          */
2643         result = dns_db_findrdataset(db, node, version, dns_rdatatype_ds, 0,
2644                                      client->now, rdataset, sigrdataset);
2645         /*
2646          * If we didn't find it, look for an NSEC. */
2647         if (result == ISC_R_NOTFOUND)
2648                 result = dns_db_findrdataset(db, node, version,
2649                                              dns_rdatatype_nsec, 0, client->now,
2650                                              rdataset, sigrdataset);
2651         if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
2652                 goto cleanup;
2653         if (!dns_rdataset_isassociated(rdataset) ||
2654             !dns_rdataset_isassociated(sigrdataset))
2655                 goto cleanup;
2656
2657         /*
2658          * We've already added the NS record, so if the name's not there,
2659          * we have other problems.  Use this name rather than calling
2660          * query_addrrset().
2661          */
2662         result = dns_message_firstname(client->message, DNS_SECTION_AUTHORITY);
2663         if (result != ISC_R_SUCCESS)
2664                 goto cleanup;
2665
2666         rname = NULL;
2667         dns_message_currentname(client->message, DNS_SECTION_AUTHORITY,
2668                                 &rname);
2669         result = dns_message_findtype(rname, dns_rdatatype_ns, 0, NULL);
2670         if (result != ISC_R_SUCCESS)
2671                 goto cleanup;
2672
2673         ISC_LIST_APPEND(rname->list, rdataset, link);
2674         ISC_LIST_APPEND(rname->list, sigrdataset, link);
2675         rdataset = NULL;
2676         sigrdataset = NULL;
2677
2678  cleanup:
2679         if (rdataset != NULL)
2680                 query_putrdataset(client, &rdataset);
2681         if (sigrdataset != NULL)
2682                 query_putrdataset(client, &sigrdataset);
2683 }
2684
2685 static void
2686 query_addwildcardproof(ns_client_t *client, dns_db_t *db,
2687                        dns_dbversion_t *version, dns_name_t *name,
2688                        isc_boolean_t ispositive)
2689 {
2690         isc_buffer_t *dbuf, b;
2691         dns_name_t *fname;
2692         dns_rdataset_t *rdataset, *sigrdataset;
2693         dns_fixedname_t wfixed;
2694         dns_name_t *wname;
2695         dns_dbnode_t *node;
2696         unsigned int options;
2697         unsigned int olabels, nlabels;
2698         isc_result_t result;
2699         dns_rdata_t rdata = DNS_RDATA_INIT;
2700         dns_rdata_nsec_t nsec;
2701         isc_boolean_t have_wname;
2702         int order;
2703
2704         CTRACE("query_addwildcardproof");
2705         fname = NULL;
2706         rdataset = NULL;
2707         sigrdataset = NULL;
2708         node = NULL;
2709
2710         /*
2711          * Get the NOQNAME proof then if !ispositive
2712          * get the NOWILDCARD proof.
2713          *
2714          * DNS_DBFIND_NOWILD finds the NSEC records that covers the
2715          * name ignoring any wildcard.  From the owner and next names
2716          * of this record you can compute which wildcard (if it exists)
2717          * will match by finding the longest common suffix of the
2718          * owner name and next names with the qname and prefixing that
2719          * with the wildcard label.
2720          *
2721          * e.g.
2722          *   Given:
2723          *      example SOA
2724          *      example NSEC b.example
2725          *      b.example A
2726          *      b.example NSEC a.d.example
2727          *      a.d.example A
2728          *      a.d.example NSEC g.f.example
2729          *      g.f.example A
2730          *      g.f.example NSEC z.i.example
2731          *      z.i.example A
2732          *      z.i.example NSEC example
2733          *
2734          *   QNAME:
2735          *   a.example -> example NSEC b.example
2736          *      owner common example
2737          *      next common example
2738          *      wild *.example
2739          *   d.b.example -> b.example NSEC a.d.example
2740          *      owner common b.example
2741          *      next common example
2742          *      wild *.b.example
2743          *   a.f.example -> a.d.example NSEC g.f.example
2744          *      owner common example
2745          *      next common f.example
2746          *      wild *.f.example
2747          *  j.example -> z.i.example NSEC example
2748          *      owner common example
2749          *      next common example
2750          *      wild *.f.example
2751          */
2752         options = client->query.dboptions | DNS_DBFIND_NOWILD;
2753         dns_fixedname_init(&wfixed);
2754         wname = dns_fixedname_name(&wfixed);
2755  again:
2756         have_wname = ISC_FALSE;
2757         /*
2758          * We'll need some resources...
2759          */
2760         dbuf = query_getnamebuf(client);
2761         if (dbuf == NULL)
2762                 goto cleanup;
2763         fname = query_newname(client, dbuf, &b);
2764         rdataset = query_newrdataset(client);
2765         sigrdataset = query_newrdataset(client);
2766         if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
2767                 goto cleanup;
2768
2769         result = dns_db_find(db, name, version, dns_rdatatype_nsec, options,
2770                              0, &node, fname, rdataset, sigrdataset);
2771         if (node != NULL)
2772                 dns_db_detachnode(db, &node);
2773         if (result == DNS_R_NXDOMAIN) {
2774                 if (!ispositive)
2775                         result = dns_rdataset_first(rdataset);
2776                 if (result == ISC_R_SUCCESS) {
2777                         dns_rdataset_current(rdataset, &rdata);
2778                         result = dns_rdata_tostruct(&rdata, &nsec, NULL);
2779                 }
2780                 if (result == ISC_R_SUCCESS) {
2781                         (void)dns_name_fullcompare(name, fname, &order,
2782                                                    &olabels);
2783                         (void)dns_name_fullcompare(name, &nsec.next, &order,
2784                                                    &nlabels);
2785                         /*
2786                          * Check for a pathological condition created when
2787                          * serving some malformed signed zones and bail out.
2788                          */
2789                         if (dns_name_countlabels(name) == nlabels)
2790                                 goto cleanup;
2791
2792                         if (olabels > nlabels)
2793                                 dns_name_split(name, olabels, NULL, wname);
2794                         else
2795                                 dns_name_split(name, nlabels, NULL, wname);
2796                         result = dns_name_concatenate(dns_wildcardname,
2797                                                       wname, wname, NULL);
2798                         if (result == ISC_R_SUCCESS)
2799                                 have_wname = ISC_TRUE;
2800                         dns_rdata_freestruct(&nsec);
2801                 }
2802                 query_addrrset(client, &fname, &rdataset, &sigrdataset,
2803                                dbuf, DNS_SECTION_AUTHORITY);
2804         }
2805         if (rdataset != NULL)
2806                 query_putrdataset(client, &rdataset);
2807         if (sigrdataset != NULL)
2808                 query_putrdataset(client, &sigrdataset);
2809         if (fname != NULL)
2810                 query_releasename(client, &fname);
2811         if (have_wname) {
2812                 ispositive = ISC_TRUE;  /* prevent loop */
2813                 if (!dns_name_equal(name, wname)) {
2814                         name = wname;
2815                         goto again;
2816                 }
2817         }
2818  cleanup:
2819         if (rdataset != NULL)
2820                 query_putrdataset(client, &rdataset);
2821         if (sigrdataset != NULL)
2822                 query_putrdataset(client, &sigrdataset);
2823         if (fname != NULL)
2824                 query_releasename(client, &fname);
2825 }
2826
2827 static void
2828 query_addnxrrsetnsec(ns_client_t *client, dns_db_t *db,
2829                      dns_dbversion_t *version, dns_name_t **namep,
2830                      dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp)
2831 {
2832         dns_name_t *name;
2833         dns_rdataset_t *sigrdataset;
2834         dns_rdata_t sigrdata;
2835         dns_rdata_rrsig_t sig;
2836         unsigned int labels;
2837         isc_buffer_t *dbuf, b;
2838         dns_name_t *fname;
2839         isc_result_t result;
2840
2841         name = *namep;
2842         if ((name->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
2843                 query_addrrset(client, namep, rdatasetp, sigrdatasetp,
2844                                NULL, DNS_SECTION_AUTHORITY);
2845                 return;
2846         }
2847
2848         if (sigrdatasetp == NULL)
2849                 return;
2850         sigrdataset = *sigrdatasetp;
2851         if (sigrdataset == NULL || !dns_rdataset_isassociated(sigrdataset))
2852                 return;
2853         result = dns_rdataset_first(sigrdataset);
2854         if (result != ISC_R_SUCCESS)
2855                 return;
2856         dns_rdata_init(&sigrdata);
2857         dns_rdataset_current(sigrdataset, &sigrdata);
2858         result = dns_rdata_tostruct(&sigrdata, &sig, NULL);
2859         if (result != ISC_R_SUCCESS)
2860                 return;
2861
2862         labels = dns_name_countlabels(name);
2863         if ((unsigned int)sig.labels + 1 >= labels)
2864                 return;
2865
2866         /* XXX */
2867         query_addwildcardproof(client, db, version, client->query.qname,
2868                                ISC_TRUE);
2869
2870         /*
2871          * We'll need some resources...
2872          */
2873         dbuf = query_getnamebuf(client);
2874         if (dbuf == NULL)
2875                 return;
2876         fname = query_newname(client, dbuf, &b);
2877         if (fname == NULL)
2878                 return;
2879         dns_name_split(name, sig.labels + 1, NULL, fname);
2880         /* This will succeed, since we've stripped labels. */
2881         RUNTIME_CHECK(dns_name_concatenate(dns_wildcardname, fname, fname,
2882                                            NULL) == ISC_R_SUCCESS);
2883         query_addrrset(client, &fname, rdatasetp, sigrdatasetp,
2884                        dbuf, DNS_SECTION_AUTHORITY);
2885 }
2886
2887 static void
2888 query_resume(isc_task_t *task, isc_event_t *event) {
2889         dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
2890         dns_fetch_t *fetch;
2891         ns_client_t *client;
2892         isc_boolean_t fetch_canceled, client_shuttingdown;
2893         isc_result_t result;
2894         isc_logcategory_t *logcategory = NS_LOGCATEGORY_QUERY_EERRORS;
2895         int errorloglevel;
2896
2897         /*
2898          * Resume a query after recursion.
2899          */
2900
2901         UNUSED(task);
2902
2903         REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
2904         client = devent->ev_arg;
2905         REQUIRE(NS_CLIENT_VALID(client));
2906         REQUIRE(task == client->task);
2907         REQUIRE(RECURSING(client));
2908
2909         LOCK(&client->query.fetchlock);
2910         if (client->query.fetch != NULL) {
2911                 /*
2912                  * This is the fetch we've been waiting for.
2913                  */
2914                 INSIST(devent->fetch == client->query.fetch);
2915                 client->query.fetch = NULL;
2916                 fetch_canceled = ISC_FALSE;
2917                 /*
2918                  * Update client->now.
2919                  */
2920                 isc_stdtime_get(&client->now);
2921         } else {
2922                 /*
2923                  * This is a fetch completion event for a canceled fetch.
2924                  * Clean up and don't resume the find.
2925                  */
2926                 fetch_canceled = ISC_TRUE;
2927         }
2928         UNLOCK(&client->query.fetchlock);
2929         INSIST(client->query.fetch == NULL);
2930
2931         client->query.attributes &= ~NS_QUERYATTR_RECURSING;
2932         fetch = devent->fetch;
2933         devent->fetch = NULL;
2934
2935         /*
2936          * If this client is shutting down, or this transaction
2937          * has timed out, do not resume the find.
2938          */
2939         client_shuttingdown = ns_client_shuttingdown(client);
2940         if (fetch_canceled || client_shuttingdown) {
2941                 if (devent->node != NULL)
2942                         dns_db_detachnode(devent->db, &devent->node);
2943                 if (devent->db != NULL)
2944                         dns_db_detach(&devent->db);
2945                 query_putrdataset(client, &devent->rdataset);
2946                 if (devent->sigrdataset != NULL)
2947                         query_putrdataset(client, &devent->sigrdataset);
2948                 isc_event_free(&event);
2949                 if (fetch_canceled)
2950                         query_error(client, DNS_R_SERVFAIL, __LINE__);
2951                 else
2952                         query_next(client, ISC_R_CANCELED);
2953                 /*
2954                  * This may destroy the client.
2955                  */
2956                 ns_client_detach(&client);
2957         } else {
2958                 result = query_find(client, devent, 0);
2959                 if (result != ISC_R_SUCCESS) {
2960                         if (result == DNS_R_SERVFAIL)
2961                                 errorloglevel = ISC_LOG_DEBUG(2);
2962                         else
2963                                 errorloglevel = ISC_LOG_DEBUG(4);
2964                         if (isc_log_wouldlog(ns_g_lctx, errorloglevel)) {
2965                                 dns_resolver_logfetch(fetch, ns_g_lctx,
2966                                                       logcategory,
2967                                                       NS_LOGMODULE_QUERY,
2968                                                       errorloglevel, ISC_FALSE);
2969                         }
2970                 }
2971         }
2972
2973         dns_resolver_destroyfetch(&fetch);
2974 }
2975
2976 static isc_result_t
2977 query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qdomain,
2978               dns_rdataset_t *nameservers, isc_boolean_t resuming)
2979 {
2980         isc_result_t result;
2981         dns_rdataset_t *rdataset, *sigrdataset;
2982         isc_sockaddr_t *peeraddr;
2983
2984         if (!resuming)
2985                 inc_stats(client, dns_nsstatscounter_recursion);
2986
2987         /*
2988          * We are about to recurse, which means that this client will
2989          * be unavailable for serving new requests for an indeterminate
2990          * amount of time.  If this client is currently responsible
2991          * for handling incoming queries, set up a new client
2992          * object to handle them while we are waiting for a
2993          * response.  There is no need to replace TCP clients
2994          * because those have already been replaced when the
2995          * connection was accepted (if allowed by the TCP quota).
2996          */
2997         if (client->recursionquota == NULL) {
2998                 result = isc_quota_attach(&ns_g_server->recursionquota,
2999                                           &client->recursionquota);
3000                 if  (result == ISC_R_SOFTQUOTA) {
3001                         static isc_stdtime_t last = 0;
3002                         isc_stdtime_t now;
3003                         isc_stdtime_get(&now);
3004                         if (now != last) {
3005                                 last = now;
3006                                 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3007                                               NS_LOGMODULE_QUERY,
3008                                               ISC_LOG_WARNING,
3009                                               "recursive-clients soft limit "
3010                                               "exceeded, aborting oldest query");
3011                         }
3012                         ns_client_killoldestquery(client);
3013                         result = ISC_R_SUCCESS;
3014                 } else if (result == ISC_R_QUOTA) {
3015                         static isc_stdtime_t last = 0;
3016                         isc_stdtime_t now;
3017                         isc_stdtime_get(&now);
3018                         if (now != last) {
3019                                 last = now;
3020                                 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3021                                               NS_LOGMODULE_QUERY,
3022                                               ISC_LOG_WARNING,
3023                                               "no more recursive clients: %s",
3024                                               isc_result_totext(result));
3025                         }
3026                         ns_client_killoldestquery(client);
3027                 }
3028                 if (result == ISC_R_SUCCESS && !client->mortal &&
3029                     (client->attributes & NS_CLIENTATTR_TCP) == 0) {
3030                         result = ns_client_replace(client);
3031                         if (result != ISC_R_SUCCESS) {
3032                                 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
3033                                               NS_LOGMODULE_QUERY,
3034                                               ISC_LOG_WARNING,
3035                                               "ns_client_replace() failed: %s",
3036                                               isc_result_totext(result));
3037                                 isc_quota_detach(&client->recursionquota);
3038                         }
3039                 }
3040                 if (result != ISC_R_SUCCESS)
3041                         return (result);
3042                 ns_client_recursing(client);
3043         }
3044
3045         /*
3046          * Invoke the resolver.
3047          */
3048         REQUIRE(nameservers == NULL || nameservers->type == dns_rdatatype_ns);
3049         REQUIRE(client->query.fetch == NULL);
3050
3051         rdataset = query_newrdataset(client);
3052         if (rdataset == NULL)
3053                 return (ISC_R_NOMEMORY);
3054         if (WANTDNSSEC(client)) {
3055                 sigrdataset = query_newrdataset(client);
3056                 if (sigrdataset == NULL) {
3057                         query_putrdataset(client, &rdataset);
3058                         return (ISC_R_NOMEMORY);
3059                 }
3060         } else
3061                 sigrdataset = NULL;
3062
3063         if (client->query.timerset == ISC_FALSE)
3064                 ns_client_settimeout(client, 60);
3065         if ((client->attributes & NS_CLIENTATTR_TCP) == 0)
3066                 peeraddr = &client->peeraddr;
3067         else
3068                 peeraddr = NULL;
3069         result = dns_resolver_createfetch2(client->view->resolver,
3070                                            client->query.qname,
3071                                            qtype, qdomain, nameservers,
3072                                            NULL, peeraddr, client->message->id,
3073                                            client->query.fetchoptions,
3074                                            client->task,
3075                                            query_resume, client,
3076                                            rdataset, sigrdataset,
3077                                            &client->query.fetch);
3078
3079         if (result == ISC_R_SUCCESS) {
3080                 /*
3081                  * Record that we're waiting for an event.  A client which
3082                  * is shutting down will not be destroyed until all the
3083                  * events have been received.
3084                  */
3085         } else {
3086                 query_putrdataset(client, &rdataset);
3087                 if (sigrdataset != NULL)
3088                         query_putrdataset(client, &sigrdataset);
3089         }
3090
3091         return (result);
3092 }
3093
3094 #define MAX_RESTARTS 16
3095
3096 #define QUERY_ERROR(r) \
3097 do { \
3098         eresult = r; \
3099         want_restart = ISC_FALSE; \
3100         line = __LINE__; \
3101 } while (0)
3102
3103 /*
3104  * Extract a network address from the RDATA of an A or AAAA
3105  * record.
3106  *
3107  * Returns:
3108  *      ISC_R_SUCCESS
3109  *      ISC_R_NOTIMPLEMENTED    The rdata is not a known address type.
3110  */
3111 static isc_result_t
3112 rdata_tonetaddr(const dns_rdata_t *rdata, isc_netaddr_t *netaddr) {
3113         struct in_addr ina;
3114         struct in6_addr in6a;
3115
3116         switch (rdata->type) {
3117         case dns_rdatatype_a:
3118                 INSIST(rdata->length == 4);
3119                 memcpy(&ina.s_addr, rdata->data, 4);
3120                 isc_netaddr_fromin(netaddr, &ina);
3121                 return (ISC_R_SUCCESS);
3122         case dns_rdatatype_aaaa:
3123                 INSIST(rdata->length == 16);
3124                 memcpy(in6a.s6_addr, rdata->data, 16);
3125                 isc_netaddr_fromin6(netaddr, &in6a);
3126                 return (ISC_R_SUCCESS);
3127         default:
3128                 return (ISC_R_NOTIMPLEMENTED);
3129         }
3130 }
3131
3132 /*
3133  * Find the sort order of 'rdata' in the topology-like
3134  * ACL forming the second element in a 2-element top-level
3135  * sortlist statement.
3136  */
3137 static int
3138 query_sortlist_order_2element(const dns_rdata_t *rdata, const void *arg) {
3139         isc_netaddr_t netaddr;
3140
3141         if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3142                 return (INT_MAX);
3143         return (ns_sortlist_addrorder2(&netaddr, arg));
3144 }
3145
3146 /*
3147  * Find the sort order of 'rdata' in the matching element
3148  * of a 1-element top-level sortlist statement.
3149  */
3150 static int
3151 query_sortlist_order_1element(const dns_rdata_t *rdata, const void *arg) {
3152         isc_netaddr_t netaddr;
3153
3154         if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
3155                 return (INT_MAX);
3156         return (ns_sortlist_addrorder1(&netaddr, arg));
3157 }
3158
3159 /*
3160  * Find the sortlist statement that applies to 'client' and set up
3161  * the sortlist info in in client->message appropriately.
3162  */
3163 static void
3164 setup_query_sortlist(ns_client_t *client) {
3165         isc_netaddr_t netaddr;
3166         dns_rdatasetorderfunc_t order = NULL;
3167         const void *order_arg = NULL;
3168
3169         isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
3170         switch (ns_sortlist_setup(client->view->sortlist,
3171                                &netaddr, &order_arg)) {
3172         case NS_SORTLISTTYPE_1ELEMENT:
3173                 order = query_sortlist_order_1element;
3174                 break;
3175         case NS_SORTLISTTYPE_2ELEMENT:
3176                 order = query_sortlist_order_2element;
3177                 break;
3178         case NS_SORTLISTTYPE_NONE:
3179                 order = NULL;
3180                 break;
3181         default:
3182                 INSIST(0);
3183                 break;
3184         }
3185         dns_message_setsortorder(client->message, order, order_arg);
3186 }
3187
3188 static void
3189 query_addnoqnameproof(ns_client_t *client, dns_rdataset_t *rdataset) {
3190         isc_buffer_t *dbuf, b;
3191         dns_name_t *fname;
3192         dns_rdataset_t *nsec, *nsecsig;
3193         isc_result_t result = ISC_R_NOMEMORY;
3194
3195         CTRACE("query_addnoqnameproof");
3196
3197         fname = NULL;
3198         nsec = NULL;
3199         nsecsig = NULL;
3200
3201         dbuf = query_getnamebuf(client);
3202         if (dbuf == NULL)
3203                 goto cleanup;
3204         fname = query_newname(client, dbuf, &b);
3205         nsec = query_newrdataset(client);
3206         nsecsig = query_newrdataset(client);
3207         if (fname == NULL || nsec == NULL || nsecsig == NULL)
3208                 goto cleanup;
3209
3210         result = dns_rdataset_getnoqname(rdataset, fname, nsec, nsecsig);
3211         RUNTIME_CHECK(result == ISC_R_SUCCESS);
3212
3213         query_addrrset(client, &fname, &nsec, &nsecsig, dbuf,
3214                        DNS_SECTION_AUTHORITY);
3215
3216  cleanup:
3217         if (nsec != NULL)
3218                 query_putrdataset(client, &nsec);
3219         if (nsecsig != NULL)
3220                 query_putrdataset(client, &nsecsig);
3221         if (fname != NULL)
3222                 query_releasename(client, &fname);
3223 }
3224
3225 static inline void
3226 answer_in_glue(ns_client_t *client, dns_rdatatype_t qtype) {
3227         dns_name_t *name;
3228         dns_message_t *msg;
3229         dns_section_t section = DNS_SECTION_ADDITIONAL;
3230         dns_rdataset_t *rdataset = NULL;
3231
3232         msg = client->message;
3233         for (name = ISC_LIST_HEAD(msg->sections[section]);
3234              name != NULL;
3235              name = ISC_LIST_NEXT(name, link))
3236                 if (dns_name_equal(name, client->query.qname)) {
3237                         for (rdataset = ISC_LIST_HEAD(name->list);
3238                              rdataset != NULL;
3239                              rdataset = ISC_LIST_NEXT(rdataset, link))
3240                                 if (rdataset->type == qtype)
3241                                         break;
3242                         break;
3243                 }
3244         if (rdataset != NULL) {
3245                 ISC_LIST_UNLINK(msg->sections[section], name, link);
3246                 ISC_LIST_PREPEND(msg->sections[section], name, link);
3247                 ISC_LIST_UNLINK(name->list, rdataset, link);
3248                 ISC_LIST_PREPEND(name->list, rdataset, link);
3249                 rdataset->attributes |= DNS_RDATASETATTR_REQUIREDGLUE;
3250         }
3251 }
3252
3253 #define NS_NAME_INIT(A,B) \
3254          { \
3255                 DNS_NAME_MAGIC, \
3256                 A, sizeof(A), sizeof(B), \
3257                 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE, \
3258                 B, NULL, { (void *)-1, (void *)-1}, \
3259                 {NULL, NULL} \
3260         }
3261
3262 static unsigned char inaddr10_offsets[] = { 0, 3, 11, 16 };
3263 static unsigned char inaddr172_offsets[] = { 0, 3, 7, 15, 20 };
3264 static unsigned char inaddr192_offsets[] = { 0, 4, 8, 16, 21 };
3265
3266 static unsigned char inaddr10[] = "\00210\007IN-ADDR\004ARPA";
3267
3268 static unsigned char inaddr16172[] = "\00216\003172\007IN-ADDR\004ARPA";
3269 static unsigned char inaddr17172[] = "\00217\003172\007IN-ADDR\004ARPA";
3270 static unsigned char inaddr18172[] = "\00218\003172\007IN-ADDR\004ARPA";
3271 static unsigned char inaddr19172[] = "\00219\003172\007IN-ADDR\004ARPA";
3272 static unsigned char inaddr20172[] = "\00220\003172\007IN-ADDR\004ARPA";
3273 static unsigned char inaddr21172[] = "\00221\003172\007IN-ADDR\004ARPA";
3274 static unsigned char inaddr22172[] = "\00222\003172\007IN-ADDR\004ARPA";
3275 static unsigned char inaddr23172[] = "\00223\003172\007IN-ADDR\004ARPA";
3276 static unsigned char inaddr24172[] = "\00224\003172\007IN-ADDR\004ARPA";
3277 static unsigned char inaddr25172[] = "\00225\003172\007IN-ADDR\004ARPA";
3278 static unsigned char inaddr26172[] = "\00226\003172\007IN-ADDR\004ARPA";
3279 static unsigned char inaddr27172[] = "\00227\003172\007IN-ADDR\004ARPA";
3280 static unsigned char inaddr28172[] = "\00228\003172\007IN-ADDR\004ARPA";
3281 static unsigned char inaddr29172[] = "\00229\003172\007IN-ADDR\004ARPA";
3282 static unsigned char inaddr30172[] = "\00230\003172\007IN-ADDR\004ARPA";
3283 static unsigned char inaddr31172[] = "\00231\003172\007IN-ADDR\004ARPA";
3284
3285 static unsigned char inaddr168192[] = "\003168\003192\007IN-ADDR\004ARPA";
3286
3287 static dns_name_t rfc1918names[] = {
3288         NS_NAME_INIT(inaddr10, inaddr10_offsets),
3289         NS_NAME_INIT(inaddr16172, inaddr172_offsets),
3290         NS_NAME_INIT(inaddr17172, inaddr172_offsets),
3291         NS_NAME_INIT(inaddr18172, inaddr172_offsets),
3292         NS_NAME_INIT(inaddr19172, inaddr172_offsets),
3293         NS_NAME_INIT(inaddr20172, inaddr172_offsets),
3294         NS_NAME_INIT(inaddr21172, inaddr172_offsets),
3295         NS_NAME_INIT(inaddr22172, inaddr172_offsets),
3296         NS_NAME_INIT(inaddr23172, inaddr172_offsets),
3297         NS_NAME_INIT(inaddr24172, inaddr172_offsets),
3298         NS_NAME_INIT(inaddr25172, inaddr172_offsets),
3299         NS_NAME_INIT(inaddr26172, inaddr172_offsets),
3300         NS_NAME_INIT(inaddr27172, inaddr172_offsets),
3301         NS_NAME_INIT(inaddr28172, inaddr172_offsets),
3302         NS_NAME_INIT(inaddr29172, inaddr172_offsets),
3303         NS_NAME_INIT(inaddr30172, inaddr172_offsets),
3304         NS_NAME_INIT(inaddr31172, inaddr172_offsets),
3305         NS_NAME_INIT(inaddr168192, inaddr192_offsets)
3306 };
3307
3308
3309 static unsigned char prisoner_data[] = "\010prisoner\004iana\003org";
3310 static unsigned char hostmaster_data[] = "\012hostmaster\014root-servers\003org";
3311
3312 static unsigned char prisoner_offsets[] = { 0, 9, 14, 18 };
3313 static unsigned char hostmaster_offsets[] = { 0, 11, 24, 28 };
3314
3315 static dns_name_t prisoner = NS_NAME_INIT(prisoner_data, prisoner_offsets);
3316 static dns_name_t hostmaster = NS_NAME_INIT(hostmaster_data, hostmaster_offsets);
3317
3318 static void
3319 warn_rfc1918(ns_client_t *client, dns_name_t *fname, dns_rdataset_t *rdataset) {
3320         unsigned int i;
3321         dns_rdata_t rdata = DNS_RDATA_INIT;
3322         dns_rdata_soa_t soa;
3323         dns_rdataset_t found;
3324         isc_result_t result;
3325
3326         for (i = 0; i < (sizeof(rfc1918names)/sizeof(*rfc1918names)); i++) {
3327                 if (dns_name_issubdomain(fname, &rfc1918names[i])) {
3328                         dns_rdataset_init(&found);
3329                         result = dns_ncache_getrdataset(rdataset,
3330                                                         &rfc1918names[i],
3331                                                         dns_rdatatype_soa,
3332                                                         &found);
3333                         if (result != ISC_R_SUCCESS)
3334                                 return;
3335
3336                         result = dns_rdataset_first(&found);
3337                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
3338                         dns_rdataset_current(&found, &rdata);
3339                         result = dns_rdata_tostruct(&rdata, &soa, NULL);
3340                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
3341                         if (dns_name_equal(&soa.origin, &prisoner) &&
3342                             dns_name_equal(&soa.contact, &hostmaster)) {
3343                                 char buf[DNS_NAME_FORMATSIZE];
3344                                 dns_name_format(fname, buf, sizeof(buf));
3345                                 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3346                                               NS_LOGMODULE_QUERY,
3347                                               ISC_LOG_WARNING,
3348                                               "RFC 1918 response from "
3349                                               "Internet for %s", buf);
3350                         }
3351                         dns_rdataset_disassociate(&found);
3352                         return;
3353                 }
3354         }
3355 }
3356
3357 /*
3358  * Do the bulk of query processing for the current query of 'client'.
3359  * If 'event' is non-NULL, we are returning from recursion and 'qtype'
3360  * is ignored.  Otherwise, 'qtype' is the query type.
3361  */
3362 static isc_result_t
3363 query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
3364 {
3365         dns_db_t *db, *zdb;
3366         dns_dbnode_t *node;
3367         dns_rdatatype_t type;
3368         dns_name_t *fname, *zfname, *tname, *prefix;
3369         dns_rdataset_t *rdataset, *trdataset;
3370         dns_rdataset_t *sigrdataset, *zrdataset, *zsigrdataset;
3371         dns_rdataset_t **sigrdatasetp;
3372         dns_rdata_t rdata = DNS_RDATA_INIT;
3373         dns_rdatasetiter_t *rdsiter;
3374         isc_boolean_t want_restart, authoritative, is_zone, need_wildcardproof;
3375         unsigned int n, nlabels;
3376         dns_namereln_t namereln;
3377         int order;
3378         isc_buffer_t *dbuf;
3379         isc_buffer_t b;
3380         isc_result_t result, eresult;
3381         dns_fixedname_t fixed;
3382         dns_fixedname_t wildcardname;
3383         dns_dbversion_t *version;
3384         dns_zone_t *zone;
3385         dns_rdata_cname_t cname;
3386         dns_rdata_dname_t dname;
3387         unsigned int options;
3388         isc_boolean_t empty_wild;
3389         dns_rdataset_t *noqname;
3390         isc_boolean_t resuming;
3391         int line = -1;
3392         dns_rdataset_t tmprdataset;
3393         unsigned int dboptions;
3394
3395         CTRACE("query_find");
3396
3397         /*
3398          * One-time initialization.
3399          *
3400          * It's especially important to initialize anything that the cleanup
3401          * code might cleanup.
3402          */
3403
3404         eresult = ISC_R_SUCCESS;
3405         fname = NULL;
3406         zfname = NULL;
3407         rdataset = NULL;
3408         zrdataset = NULL;
3409         sigrdataset = NULL;
3410         zsigrdataset = NULL;
3411         node = NULL;
3412         db = NULL;
3413         zdb = NULL;
3414         version = NULL;
3415         zone = NULL;
3416         need_wildcardproof = ISC_FALSE;
3417         empty_wild = ISC_FALSE;
3418         options = 0;
3419         resuming = ISC_FALSE;
3420         is_zone = ISC_FALSE;
3421
3422         if (event != NULL) {
3423                 /*
3424                  * We're returning from recursion.  Restore the query context
3425                  * and resume.
3426                  */
3427
3428                 want_restart = ISC_FALSE;
3429                 authoritative = ISC_FALSE;
3430
3431                 qtype = event->qtype;
3432                 if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3433                         type = dns_rdatatype_any;
3434                 else
3435                         type = qtype;
3436                 db = event->db;
3437                 node = event->node;
3438                 rdataset = event->rdataset;
3439                 sigrdataset = event->sigrdataset;
3440
3441                 /*
3442                  * We'll need some resources...
3443                  */
3444                 dbuf = query_getnamebuf(client);
3445                 if (dbuf == NULL) {
3446                         QUERY_ERROR(DNS_R_SERVFAIL);
3447                         goto cleanup;
3448                 }
3449                 fname = query_newname(client, dbuf, &b);
3450                 if (fname == NULL) {
3451                         QUERY_ERROR(DNS_R_SERVFAIL);
3452                         goto cleanup;
3453                 }
3454                 tname = dns_fixedname_name(&event->foundname);
3455                 result = dns_name_copy(tname, fname, NULL);
3456                 if (result != ISC_R_SUCCESS) {
3457                         QUERY_ERROR(DNS_R_SERVFAIL);
3458                         goto cleanup;
3459                 }
3460
3461                 result = event->result;
3462                 resuming = ISC_TRUE;
3463
3464                 goto resume;
3465         }
3466
3467         /*
3468          * Not returning from recursion.
3469          */
3470
3471         /*
3472          * If it's a SIG query, we'll iterate the node.
3473          */
3474         if (qtype == dns_rdatatype_rrsig || qtype == dns_rdatatype_sig)
3475                 type = dns_rdatatype_any;
3476         else
3477                 type = qtype;
3478
3479  restart:
3480         CTRACE("query_find: restart");
3481         want_restart = ISC_FALSE;
3482         authoritative = ISC_FALSE;
3483         version = NULL;
3484         need_wildcardproof = ISC_FALSE;
3485
3486         if (client->view->checknames &&
3487             !dns_rdata_checkowner(client->query.qname,
3488                                   client->message->rdclass,
3489                                   qtype, ISC_FALSE)) {
3490                 char namebuf[DNS_NAME_FORMATSIZE];
3491                 char typename[DNS_RDATATYPE_FORMATSIZE];
3492                 char classname[DNS_RDATACLASS_FORMATSIZE];
3493
3494                 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
3495                 dns_rdatatype_format(qtype, typename, sizeof(typename));
3496                 dns_rdataclass_format(client->message->rdclass, classname,
3497                                       sizeof(classname));
3498                 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
3499                               NS_LOGMODULE_QUERY, ISC_LOG_ERROR,
3500                               "check-names failure %s/%s/%s", namebuf,
3501                               typename, classname);
3502                 QUERY_ERROR(DNS_R_REFUSED);
3503                 goto cleanup;
3504         }
3505
3506         /*
3507          * First we must find the right database.
3508          */
3509         options &= DNS_GETDB_NOLOG; /* Preserve DNS_GETDB_NOLOG. */
3510         if (dns_rdatatype_atparent(qtype) &&
3511             !dns_name_equal(client->query.qname, dns_rootname))
3512                 options |= DNS_GETDB_NOEXACT;
3513         result = query_getdb(client, client->query.qname, qtype, options,
3514                              &zone, &db, &version, &is_zone);
3515         if ((result != ISC_R_SUCCESS || !is_zone) && !RECURSIONOK(client) &&
3516             (options & DNS_GETDB_NOEXACT) != 0 && qtype == dns_rdatatype_ds) {
3517                 /*
3518                  * Look to see if we are authoritative for the
3519                  * child zone if the query type is DS.
3520                  */
3521                 dns_db_t *tdb = NULL;
3522                 dns_zone_t *tzone = NULL;
3523                 dns_dbversion_t *tversion = NULL;
3524                 isc_result_t tresult;
3525
3526                 tresult = query_getzonedb(client, client->query.qname, qtype,
3527                                          DNS_GETDB_PARTIAL, &tzone, &tdb,
3528                                          &tversion);
3529                 if (tresult == ISC_R_SUCCESS) {
3530                         options &= ~DNS_GETDB_NOEXACT;
3531                         query_putrdataset(client, &rdataset);
3532                         if (db != NULL)
3533                                 dns_db_detach(&db);
3534                         if (zone != NULL)
3535                                 dns_zone_detach(&zone);
3536                         version = tversion;
3537                         db = tdb;
3538                         zone = tzone;
3539                         is_zone = ISC_TRUE;
3540                         result = ISC_R_SUCCESS;
3541                 } else {
3542                         if (tdb != NULL)
3543                                 dns_db_detach(&tdb);
3544                         if (tzone != NULL)
3545                                 dns_zone_detach(&tzone);
3546                 }
3547         }
3548         if (result != ISC_R_SUCCESS) {
3549                 if (result == DNS_R_REFUSED) {
3550                         if (WANTRECURSION(client)) {
3551                                 inc_stats(client,
3552                                           dns_nsstatscounter_recurserej);
3553                         } else
3554                                 inc_stats(client, dns_nsstatscounter_authrej);
3555                         if (!PARTIALANSWER(client))
3556                                 QUERY_ERROR(DNS_R_REFUSED);
3557                 } else
3558                         QUERY_ERROR(DNS_R_SERVFAIL);
3559                 goto cleanup;
3560         }
3561
3562         if (is_zone)
3563                 authoritative = ISC_TRUE;
3564
3565         if (event == NULL && client->query.restarts == 0) {
3566                 if (is_zone) {
3567 #ifdef DLZ
3568                         if (zone != NULL) {
3569                                 /*
3570                                  * if is_zone = true, zone = NULL then this is
3571                                  * a DLZ zone.  Don't attempt to attach zone.
3572                                  */
3573 #endif
3574                                 dns_zone_attach(zone, &client->query.authzone);
3575 #ifdef DLZ
3576                         }
3577 #endif
3578                         dns_db_attach(db, &client->query.authdb);
3579                 }
3580                 client->query.authdbset = ISC_TRUE;
3581         }
3582
3583  db_find:
3584         CTRACE("query_find: db_find");
3585         /*
3586          * We'll need some resources...
3587          */
3588         dbuf = query_getnamebuf(client);
3589         if (dbuf == NULL) {
3590                 QUERY_ERROR(DNS_R_SERVFAIL);
3591                 goto cleanup;
3592         }
3593         fname = query_newname(client, dbuf, &b);
3594         rdataset = query_newrdataset(client);
3595         if (fname == NULL || rdataset == NULL) {
3596                 QUERY_ERROR(DNS_R_SERVFAIL);
3597                 goto cleanup;
3598         }
3599         if (WANTDNSSEC(client)) {
3600                 sigrdataset = query_newrdataset(client);
3601                 if (sigrdataset == NULL) {
3602                         QUERY_ERROR(DNS_R_SERVFAIL);
3603                         goto cleanup;
3604                 }
3605         }
3606
3607         /*
3608          * Now look for an answer in the database.
3609          */
3610         dboptions = client->query.dboptions;
3611         if (sigrdataset == NULL && client->view->enablednssec) {
3612                 /*
3613                  * If the client doesn't want DNSSEC we still want to
3614                  * look for any data pending validation to save a remote
3615                  * lookup if possible.
3616                  */
3617                 dns_rdataset_init(&tmprdataset);
3618                 sigrdataset = &tmprdataset;
3619                 dboptions |= DNS_DBFIND_PENDINGOK;
3620         }
3621  refind:
3622         result = dns_db_find(db, client->query.qname, version, type,
3623                              dboptions, client->now, &node, fname,
3624                              rdataset, sigrdataset);
3625         /*
3626          * If we have found pending data try to validate it.
3627          * If the data does not validate as secure and we can't
3628          * use the unvalidated data requery the database with
3629          * pending disabled to prevent infinite looping.
3630          */
3631         if (result != ISC_R_SUCCESS || !DNS_TRUST_PENDING(rdataset->trust))
3632                 goto validation_done;
3633         if (validate(client, db, fname, rdataset, sigrdataset))
3634                 goto validation_done;
3635         if (rdataset->trust != dns_trust_pending_answer ||
3636             !PENDINGOK(client->query.dboptions)) {
3637                 dns_rdataset_disassociate(rdataset);
3638                 if (sigrdataset != NULL &&
3639                     dns_rdataset_isassociated(sigrdataset))
3640                         dns_rdataset_disassociate(sigrdataset);
3641                 if (sigrdataset == &tmprdataset)
3642                         sigrdataset = NULL;
3643                 dns_db_detachnode(db, &node);
3644                 dboptions &= ~DNS_DBFIND_PENDINGOK;
3645                 goto refind;
3646         }
3647  validation_done:
3648         if (sigrdataset == &tmprdataset) {
3649                 if (dns_rdataset_isassociated(sigrdataset))
3650                         dns_rdataset_disassociate(sigrdataset);
3651                 sigrdataset = NULL;
3652         }
3653
3654  resume:
3655         CTRACE("query_find: resume");
3656         switch (result) {
3657         case ISC_R_SUCCESS:
3658                 /*
3659                  * This case is handled in the main line below.
3660                  */
3661                 break;
3662         case DNS_R_GLUE:
3663         case DNS_R_ZONECUT:
3664                 /*
3665                  * These cases are handled in the main line below.
3666                  */
3667                 INSIST(is_zone);
3668                 authoritative = ISC_FALSE;
3669                 break;
3670         case ISC_R_NOTFOUND:
3671                 /*
3672                  * The cache doesn't even have the root NS.  Get them from
3673