~kernevil/ubuntu/trusty/bind9/sdlz-hmac-keys

« back to all changes in this revision

Viewing changes to bin/named/query.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones, Matthew Grant, LaMont Jones
  • Date: 2012-10-29 08:37:49 UTC
  • mfrom: (1.9.2)
  • Revision ID: package-import@ubuntu.com-20121029083749-r9inpzl0yuj9xdlu
Tags: 1:9.8.4.dfsg-1
[Matthew Grant]

* Turn off dlopen as it was causing test compile failures.
* Add missing library .postrm files for debhelper

[LaMont Jones]

* New upstream version
* soname fixup
* Ack NMUs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004-2011  Internet Systems Consortium, Inc. ("ISC")
 
2
 * Copyright (C) 2004-2012  Internet Systems Consortium, Inc. ("ISC")
3
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
4
 *
5
5
 * Permission to use, copy, modify, and/or distribute this software for any
15
15
 * PERFORMANCE OF THIS SOFTWARE.
16
16
 */
17
17
 
18
 
/* $Id: query.c,v 1.353.8.11.4.1 2011-11-16 09:32:08 marka Exp $ */
 
18
/* $Id: query.c,v 1.353.8.24 2012/02/07 01:14:39 marka Exp $ */
19
19
 
20
20
/*! \file */
21
21
 
25
25
 
26
26
#include <isc/hex.h>
27
27
#include <isc/mem.h>
 
28
#include <isc/serial.h>
28
29
#include <isc/stats.h>
29
30
#include <isc/util.h>
30
31
 
830
831
}
831
832
 
832
833
static void
833
 
rpz_log(ns_client_t *client) {
834
 
        char namebuf1[DNS_NAME_FORMATSIZE];
835
 
        char namebuf2[DNS_NAME_FORMATSIZE];
836
 
        dns_rpz_st_t *st;
837
 
        const char *pat;
 
834
rpz_log_rewrite(ns_client_t *client, const char *disabled,
 
835
                dns_rpz_policy_t policy, dns_rpz_type_t type,
 
836
                dns_name_t *rpz_qname) {
 
837
        char qname_buf[DNS_NAME_FORMATSIZE];
 
838
        char rpz_qname_buf[DNS_NAME_FORMATSIZE];
838
839
 
839
 
        if (!ns_g_server->log_queries ||
840
 
            !isc_log_wouldlog(ns_g_lctx, DNS_RPZ_INFO_LEVEL))
 
840
        if (!isc_log_wouldlog(ns_g_lctx, DNS_RPZ_INFO_LEVEL))
841
841
                return;
842
842
 
843
 
        st = client->query.rpz_st;
844
 
        dns_name_format(client->query.qname, namebuf1, sizeof(namebuf1));
845
 
        dns_name_format(st->qname, namebuf2, sizeof(namebuf2));
 
843
        dns_name_format(client->query.qname, qname_buf, sizeof(qname_buf));
 
844
        dns_name_format(rpz_qname, rpz_qname_buf, sizeof(rpz_qname_buf));
846
845
 
847
 
        switch (st->m.policy) {
848
 
        case DNS_RPZ_POLICY_NO_OP:
849
 
                pat ="response policy %s rewrite %s NO-OP using %s";
850
 
                break;
851
 
        case DNS_RPZ_POLICY_NXDOMAIN:
852
 
                pat = "response policy %s rewrite %s to NXDOMAIN using %s";
853
 
                break;
854
 
        case DNS_RPZ_POLICY_NODATA:
855
 
                pat = "response policy %s rewrite %s to NODATA using %s";
856
 
                break;
857
 
        case DNS_RPZ_POLICY_RECORD:
858
 
        case DNS_RPZ_POLICY_CNAME:
859
 
                pat = "response policy %s rewrite %s using %s";
860
 
                break;
861
 
        default:
862
 
                INSIST(0);
863
 
        }
864
 
        ns_client_log(client, NS_LOGCATEGORY_QUERIES, NS_LOGMODULE_QUERY,
865
 
                      DNS_RPZ_INFO_LEVEL, pat, dns_rpz_type2str(st->m.type),
866
 
                      namebuf1, namebuf2);
 
846
        ns_client_log(client, DNS_LOGCATEGORY_RPZ, NS_LOGMODULE_QUERY,
 
847
                      DNS_RPZ_INFO_LEVEL, "%srpz %s %s rewrite %s via %s",
 
848
                      disabled,
 
849
                      dns_rpz_type2str(type), dns_rpz_policy2str(policy),
 
850
                      qname_buf, rpz_qname_buf);
867
851
}
868
852
 
869
853
static void
870
 
rpz_fail_log(ns_client_t *client, int level, dns_rpz_type_t rpz_type,
871
 
             dns_name_t *name, const char *str, isc_result_t result)
 
854
rpz_log_fail(ns_client_t *client, int level,
 
855
             dns_rpz_type_t rpz_type, dns_name_t *name,
 
856
             const char *str, isc_result_t result)
872
857
{
873
858
        char namebuf1[DNS_NAME_FORMATSIZE];
874
859
        char namebuf2[DNS_NAME_FORMATSIZE];
875
860
 
876
 
        if (!ns_g_server->log_queries || !isc_log_wouldlog(ns_g_lctx, level))
 
861
        if (!isc_log_wouldlog(ns_g_lctx, level))
877
862
                return;
878
863
 
879
864
        dns_name_format(client->query.qname, namebuf1, sizeof(namebuf1));
880
865
        dns_name_format(name, namebuf2, sizeof(namebuf2));
881
866
        ns_client_log(client, NS_LOGCATEGORY_QUERY_EERRORS,
882
867
                      NS_LOGMODULE_QUERY, level,
883
 
                      "response policy %s rewrite %s via %s %sfailed: %s",
 
868
                      "rpz %s rewrite %s via %s %sfailed: %s",
884
869
                      dns_rpz_type2str(rpz_type),
885
870
                      namebuf1, namebuf2, str, isc_result_totext(result));
886
871
}
889
874
 * Get a policy rewrite zone database.
890
875
 */
891
876
static isc_result_t
892
 
rpz_getdb(ns_client_t *client, dns_rpz_type_t rpz_type,
893
 
          dns_name_t *rpz_qname, dns_zone_t **zonep,
894
 
          dns_db_t **dbp, dns_dbversion_t **versionp)
 
877
rpz_getdb(ns_client_t *client, dns_rpz_type_t rpz_type, dns_name_t *rpz_qname,
 
878
          dns_zone_t **zonep, dns_db_t **dbp, dns_dbversion_t **versionp)
895
879
{
896
880
        char namebuf1[DNS_NAME_FORMATSIZE];
897
881
        char namebuf2[DNS_NAME_FORMATSIZE];
901
885
        result = query_getzonedb(client, rpz_qname, dns_rdatatype_any,
902
886
                                 DNS_GETDB_IGNOREACL, zonep, dbp, &rpz_version);
903
887
        if (result == ISC_R_SUCCESS) {
904
 
                if (ns_g_server->log_queries &&
905
 
                    isc_log_wouldlog(ns_g_lctx, DNS_RPZ_DEBUG_LEVEL2)) {
 
888
                if (isc_log_wouldlog(ns_g_lctx, DNS_RPZ_DEBUG_LEVEL2)) {
906
889
                        dns_name_format(client->query.qname, namebuf1,
907
890
                                        sizeof(namebuf1));
908
891
                        dns_name_format(rpz_qname, namebuf2, sizeof(namebuf2));
909
 
                        ns_client_log(client, NS_LOGCATEGORY_QUERIES,
 
892
                        ns_client_log(client, DNS_LOGCATEGORY_RPZ,
910
893
                                      NS_LOGMODULE_QUERY, DNS_RPZ_DEBUG_LEVEL2,
911
894
                                      "try rpz %s rewrite %s via %s",
912
895
                                      dns_rpz_type2str(rpz_type),
915
898
                *versionp = rpz_version;
916
899
                return (ISC_R_SUCCESS);
917
900
        }
918
 
        rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL, rpz_type, rpz_qname,
 
901
        rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL, rpz_type, rpz_qname,
919
902
                     "query_getzonedb() ", result);
920
903
        return (result);
921
904
}
1137
1120
                mname = NULL;
1138
1121
        }
1139
1122
 
1140
 
        *mnamep = mname;
 
1123
        if (mnamep != NULL)
 
1124
                *mnamep = mname;
1141
1125
 
1142
1126
        CTRACE("query_isduplicate: false: done");
1143
1127
        return (ISC_FALSE);
1357
1341
        }
1358
1342
 
1359
1343
        if (qtype == dns_rdatatype_a) {
 
1344
#ifdef ALLOW_FILTER_AAAA_ON_V4
 
1345
                isc_boolean_t have_a = ISC_FALSE;
 
1346
#endif
 
1347
 
1360
1348
                /*
1361
1349
                 * We now go looking for A and AAAA records, along with
1362
1350
                 * their signatures.
1379
1367
                        if (sigrdataset == NULL)
1380
1368
                                goto addname;
1381
1369
                }
 
1370
                if (query_isduplicate(client, fname, dns_rdatatype_a, NULL))
 
1371
                        goto aaaa_lookup;
1382
1372
                result = dns_db_findrdataset(db, node, version,
1383
1373
                                             dns_rdatatype_a, 0,
1384
1374
                                             client->now, rdataset,
1393
1383
                }
1394
1384
                if (result == ISC_R_SUCCESS) {
1395
1385
                        mname = NULL;
 
1386
#ifdef ALLOW_FILTER_AAAA_ON_V4
 
1387
                        have_a = ISC_TRUE;
 
1388
#endif
1396
1389
                        if (!query_isduplicate(client, fname,
1397
1390
                                               dns_rdatatype_a, &mname)) {
1398
1391
                                if (mname != fname) {
1424
1417
                                        dns_rdataset_disassociate(sigrdataset);
1425
1418
                        }
1426
1419
                }
 
1420
  aaaa_lookup:
 
1421
                if (query_isduplicate(client, fname, dns_rdatatype_aaaa, NULL))
 
1422
                        goto addname;
1427
1423
                result = dns_db_findrdataset(db, node, version,
1428
1424
                                             dns_rdatatype_aaaa, 0,
1429
1425
                                             client->now, rdataset,
1438
1434
                }
1439
1435
                if (result == ISC_R_SUCCESS) {
1440
1436
                        mname = NULL;
 
1437
                        /*
 
1438
                         * There's an A; check whether we're filtering AAAA
 
1439
                         */
 
1440
#ifdef ALLOW_FILTER_AAAA_ON_V4
 
1441
                        if (have_a &&
 
1442
                            (client->filter_aaaa == dns_v4_aaaa_break_dnssec ||
 
1443
                            (client->filter_aaaa == dns_v4_aaaa_filter &&
 
1444
                             (!WANTDNSSEC(client) || sigrdataset == NULL ||
 
1445
                              !dns_rdataset_isassociated(sigrdataset)))))
 
1446
                                goto addname;
 
1447
#endif
1441
1448
                        if (!query_isduplicate(client, fname,
1442
1449
                                               dns_rdatatype_aaaa, &mname)) {
1443
1450
                                if (mname != fname) {
1591
1598
        dns_rdatatype_t type;
1592
1599
        dns_rdatasetadditional_t additionaltype;
1593
1600
 
1594
 
        if (qtype != dns_rdatatype_a) {
 
1601
        /*
 
1602
         * If we don't have an additional cache call query_addadditional.
 
1603
         */
 
1604
        client = additionalctx->client;
 
1605
        REQUIRE(NS_CLIENT_VALID(client));
 
1606
 
 
1607
        if (qtype != dns_rdatatype_a || client->view->acache == NULL) {
1595
1608
                /*
1596
1609
                 * This function is optimized for "address" types.  For other
1597
1610
                 * types, use a generic routine.
1605
1618
         * Initialization.
1606
1619
         */
1607
1620
        rdataset_base = additionalctx->rdataset;
1608
 
        client = additionalctx->client;
1609
 
        REQUIRE(NS_CLIENT_VALID(client));
1610
1621
        eresult = ISC_R_SUCCESS;
1611
1622
        fname = NULL;
1612
1623
        rdataset = NULL;
1859
1870
        if (sigrdataset == NULL)
1860
1871
                goto cleanup;
1861
1872
 
 
1873
        if (additionaltype == dns_rdatasetadditional_fromcache &&
 
1874
            query_isduplicate(client, fname, dns_rdatatype_a, NULL))
 
1875
                goto aaaa_lookup;
1862
1876
        /*
1863
1877
         * Find A RRset with sig RRset.  Even if we don't find a sig RRset
1864
1878
         * for a client using DNSSEC, we'll continue the process to make a
1903
1917
                }
1904
1918
        }
1905
1919
 
 
1920
 aaaa_lookup:
 
1921
        if (additionaltype == dns_rdatasetadditional_fromcache &&
 
1922
            query_isduplicate(client, fname, dns_rdatatype_aaaa, NULL))
 
1923
                goto foundcache;
1906
1924
        /* Find AAAA RRset with sig RRset */
1907
1925
        result = dns_db_findrdataset(db, node, version, dns_rdatatype_aaaa,
1908
1926
                                     0, client->now, rdataset, sigrdataset);
2758
2776
 */
2759
2777
static void
2760
2778
mark_secure(ns_client_t *client, dns_db_t *db, dns_name_t *name,
2761
 
            isc_uint32_t ttl, dns_rdataset_t *rdataset,
 
2779
            dns_rdata_rrsig_t *rrsig, dns_rdataset_t *rdataset,
2762
2780
            dns_rdataset_t *sigrdataset)
2763
2781
{
2764
2782
        isc_result_t result;
2765
2783
        dns_dbnode_t *node = NULL;
 
2784
        isc_stdtime_t now;
2766
2785
 
2767
2786
        rdataset->trust = dns_trust_secure;
2768
2787
        sigrdataset->trust = dns_trust_secure;
2773
2792
        result = dns_db_findnode(db, name, ISC_TRUE, &node);
2774
2793
        if (result != ISC_R_SUCCESS)
2775
2794
                return;
2776
 
        /*
2777
 
         * Bound the validated ttls then minimise.
2778
 
         */
2779
 
        if (sigrdataset->ttl > ttl)
2780
 
                sigrdataset->ttl = ttl;
2781
 
        if (rdataset->ttl > ttl)
2782
 
                rdataset->ttl = ttl;
2783
 
        if (rdataset->ttl > sigrdataset->ttl)
2784
 
                rdataset->ttl = sigrdataset->ttl;
2785
 
        else
2786
 
                sigrdataset->ttl = rdataset->ttl;
 
2795
 
 
2796
        isc_stdtime_get(&now);
 
2797
        dns_rdataset_trimttl(rdataset, sigrdataset, rrsig, now,
 
2798
                             client->view->acceptexpired);
2787
2799
 
2788
2800
        (void)dns_db_addrdataset(db, node, NULL, client->now, rdataset,
2789
2801
                                 0, NULL);
2908
2920
                                   client->view->acceptexpired)) {
2909
2921
                                dst_key_free(&key);
2910
2922
                                dns_rdataset_disassociate(&keyrdataset);
2911
 
                                mark_secure(client, db, name,
2912
 
                                            rrsig.originalttl,
 
2923
                                mark_secure(client, db, name, &rrsig,
2913
2924
                                            rdataset, sigrdataset);
2914
2925
                                return (ISC_TRUE);
2915
2926
                        }
3337
3348
                dns_name_copy(name, cname, NULL);
3338
3349
                while (result == DNS_R_NXDOMAIN) {
3339
3350
                        labels = dns_name_countlabels(cname) - 1;
 
3351
                        /*
 
3352
                         * Sanity check.
 
3353
                         */
 
3354
                        if (labels == 0U)
 
3355
                                goto cleanup;
3340
3356
                        dns_name_split(cname, labels, NULL, cname);
3341
3357
                        result = dns_db_find(db, cname, version,
3342
3358
                                             dns_rdatatype_nsec,
3350
3366
                                       sigrdataset, fname, ISC_TRUE, cname);
3351
3367
                if (!dns_rdataset_isassociated(rdataset))
3352
3368
                        goto cleanup;
3353
 
                query_addrrset(client, &fname, &rdataset, &sigrdataset,
3354
 
                               dbuf, DNS_SECTION_AUTHORITY);
 
3369
                if (!ispositive)
 
3370
                        query_addrrset(client, &fname, &rdataset, &sigrdataset,
 
3371
                                       dbuf, DNS_SECTION_AUTHORITY);
3355
3372
 
3356
3373
                /*
3357
3374
                 * Replace resources which were consumed by query_addrrset.
3779
3796
                dns_rdataset_disassociate(*rdatasetp);
3780
3797
}
3781
3798
 
 
3799
static void
 
3800
rpz_match_clear(dns_rpz_st_t *st)
 
3801
{
 
3802
        rpz_clean(&st->m.zone, &st->m.db, &st->m.node, &st->m.rdataset);
 
3803
        st->m.version = NULL;
 
3804
}
 
3805
 
3782
3806
static inline isc_result_t
3783
3807
rpz_ready(ns_client_t *client, dns_zone_t **zonep, dns_db_t **dbp,
3784
3808
          dns_dbnode_t **nodep, dns_rdataset_t **rdatasetp)
3798
3822
rpz_st_clear(ns_client_t *client) {
3799
3823
        dns_rpz_st_t *st = client->query.rpz_st;
3800
3824
 
3801
 
        rpz_clean(&st->m.zone, &st->m.db, &st->m.node, NULL);
3802
3825
        if (st->m.rdataset != NULL)
3803
3826
                query_putrdataset(client, &st->m.rdataset);
 
3827
        rpz_match_clear(st);
3804
3828
 
3805
 
        rpz_clean(NULL, &st->ns.db, NULL, NULL);
3806
 
        if (st->ns.ns_rdataset != NULL)
3807
 
                query_putrdataset(client, &st->ns.ns_rdataset);
3808
 
        if (st->ns.r_rdataset != NULL)
3809
 
                query_putrdataset(client, &st->ns.r_rdataset);
 
3829
        rpz_clean(NULL, &st->r.db, NULL, NULL);
 
3830
        if (st->r.ns_rdataset != NULL)
 
3831
                query_putrdataset(client, &st->r.ns_rdataset);
 
3832
        if (st->r.r_rdataset != NULL)
 
3833
                query_putrdataset(client, &st->r.r_rdataset);
3810
3834
 
3811
3835
        rpz_clean(&st->q.zone, &st->q.db, &st->q.node, NULL);
3812
3836
        if (st->q.rdataset != NULL)
3814
3838
        if (st->q.sigrdataset != NULL)
3815
3839
                query_putrdataset(client, &st->q.sigrdataset);
3816
3840
        st->state = 0;
 
3841
        st->m.type = DNS_RPZ_TYPE_BAD;
 
3842
        st->m.policy = DNS_RPZ_POLICY_MISS;
3817
3843
}
3818
3844
 
3819
3845
/*
3820
 
 * Get NS, A, or AAAA rrset for rpz nsdname or nsip checking.
 
3846
 * Get NS, A, or AAAA rrset for response policy zone checks.
3821
3847
 */
3822
3848
static isc_result_t
3823
 
rpz_ns_find(ns_client_t *client, dns_name_t *name, dns_rdatatype_t type,
3824
 
            dns_db_t **dbp, dns_dbversion_t *version,
3825
 
            dns_rdataset_t **rdatasetp, isc_boolean_t resuming)
 
3849
rpz_rrset_find(ns_client_t *client, dns_rpz_type_t rpz_type,
 
3850
               dns_name_t *name, dns_rdatatype_t type,
 
3851
               dns_db_t **dbp, dns_dbversion_t *version,
 
3852
               dns_rdataset_t **rdatasetp, isc_boolean_t resuming)
3826
3853
{
3827
3854
        dns_rpz_st_t *st;
3828
3855
        isc_boolean_t is_zone;
3833
3860
 
3834
3861
        st = client->query.rpz_st;
3835
3862
        if ((st->state & DNS_RPZ_RECURSING) != 0) {
3836
 
                INSIST(st->ns.r_type == type);
 
3863
                INSIST(st->r.r_type == type);
3837
3864
                INSIST(dns_name_equal(name, st->r_name));
3838
3865
                INSIST(*rdatasetp == NULL ||
3839
3866
                       !dns_rdataset_isassociated(*rdatasetp));
3840
3867
                st->state &= ~DNS_RPZ_RECURSING;
3841
 
                *dbp = st->ns.db;
3842
 
                st->ns.db = NULL;
 
3868
                *dbp = st->r.db;
 
3869
                st->r.db = NULL;
3843
3870
                if (*rdatasetp != NULL)
3844
3871
                        query_putrdataset(client, rdatasetp);
3845
 
                *rdatasetp = st->ns.r_rdataset;
3846
 
                st->ns.r_rdataset = NULL;
3847
 
                result = st->ns.r_result;
 
3872
                *rdatasetp = st->r.r_rdataset;
 
3873
                st->r.r_rdataset = NULL;
 
3874
                result = st->r.r_result;
3848
3875
                if (result == DNS_R_DELEGATION) {
3849
 
                        rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL,
3850
 
                                     DNS_RPZ_TYPE_NSIP, name,
3851
 
                                     "rpz_ns_find() ", result);
 
3876
                        rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL,
 
3877
                                     rpz_type, name,
 
3878
                                     "rpz_rrset_find(1) ", result);
3852
3879
                        st->m.policy = DNS_RPZ_POLICY_ERROR;
3853
3880
                        result = DNS_R_SERVFAIL;
3854
3881
                }
3870
3897
                result = query_getdb(client, name, type, 0, &zone, dbp,
3871
3898
                                     &version, &is_zone);
3872
3899
                if (result != ISC_R_SUCCESS) {
3873
 
                        rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL,
3874
 
                                     DNS_RPZ_TYPE_NSIP, name, "NS getdb() ",
3875
 
                                     result);
 
3900
                        rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL,
 
3901
                                     rpz_type, name,
 
3902
                                     "rpz_rrset_find(2) ", result);
3876
3903
                        st->m.policy = DNS_RPZ_POLICY_ERROR;
3877
3904
                        if (zone != NULL)
3878
3905
                                dns_zone_detach(&zone);
3885
3912
        node = NULL;
3886
3913
        dns_fixedname_init(&fixed);
3887
3914
        found = dns_fixedname_name(&fixed);
3888
 
        result = dns_db_find(*dbp, name, version, type, 0, client->now, &node,
3889
 
                             found, *rdatasetp, NULL);
 
3915
        result = dns_db_find(*dbp, name, version, type, DNS_DBFIND_GLUEOK,
 
3916
                             client->now, &node, found, *rdatasetp, NULL);
3890
3917
        if (result == DNS_R_DELEGATION && is_zone && USECACHE(client)) {
3891
3918
                /*
3892
3919
                 * Try the cache if we're authoritative for an
3901
3928
        }
3902
3929
        rpz_clean(NULL, dbp, &node, NULL);
3903
3930
        if (result == DNS_R_DELEGATION) {
3904
 
                /*
3905
 
                 * Recurse to get NS rrset or A or AAAA rrset for an NS name.
3906
 
                 */
3907
3931
                rpz_clean(NULL, NULL, NULL, rdatasetp);
3908
 
                dns_name_copy(name, st->r_name, NULL);
3909
 
                result = query_recurse(client, type, st->r_name, NULL, NULL,
3910
 
                                       resuming);
3911
 
                if (result == ISC_R_SUCCESS) {
3912
 
                        st->state |= DNS_RPZ_RECURSING;
3913
 
                        result = DNS_R_DELEGATION;
 
3932
                /*
 
3933
                 * Recurse for NS rrset or A or AAAA rrset for an NS.
 
3934
                 * Do not recurse for addresses for the query name.
 
3935
                 */
 
3936
                if (rpz_type == DNS_RPZ_TYPE_IP) {
 
3937
                        result = DNS_R_NXRRSET;
 
3938
                } else {
 
3939
                        dns_name_copy(name, st->r_name, NULL);
 
3940
                        result = query_recurse(client, type, st->r_name,
 
3941
                                               NULL, NULL, resuming);
 
3942
                        if (result == ISC_R_SUCCESS) {
 
3943
                                st->state |= DNS_RPZ_RECURSING;
 
3944
                                result = DNS_R_DELEGATION;
 
3945
                        }
3914
3946
                }
3915
3947
        }
3916
3948
        return (result);
3928
3960
        dns_dbversion_t *version;
3929
3961
        dns_zone_t *zone;
3930
3962
        dns_db_t *db;
3931
 
        dns_rpz_zone_t *new_rpz;
 
3963
        dns_rpz_zone_t *rpz;
3932
3964
        isc_result_t result;
3933
3965
 
3934
3966
        st = client->query.rpz_st;
3939
3971
        }
3940
3972
        zone = NULL;
3941
3973
        db = NULL;
3942
 
        for (new_rpz = ISC_LIST_HEAD(client->view->rpz_zones);
3943
 
             new_rpz != NULL;
3944
 
             new_rpz = ISC_LIST_NEXT(new_rpz, link)) {
 
3974
        for (rpz = ISC_LIST_HEAD(client->view->rpz_zones);
 
3975
             rpz != NULL;
 
3976
             rpz = ISC_LIST_NEXT(rpz, link)) {
 
3977
                if (!RECURSIONOK(client) && rpz->recursive_only)
 
3978
                        continue;
 
3979
 
 
3980
                /*
 
3981
                 * Do not check policy zones that cannot replace a policy
 
3982
                 * already known to match.
 
3983
                 */
 
3984
                if (st->m.policy != DNS_RPZ_POLICY_MISS) {
 
3985
                        if (st->m.rpz->num < rpz->num)
 
3986
                                break;
 
3987
                        if (st->m.rpz->num == rpz->num &&
 
3988
                            st->m.type < rpz_type)
 
3989
                                continue;
 
3990
                }
 
3991
 
 
3992
                /*
 
3993
                 * Find the database for this policy zone to get its radix tree.
 
3994
                 */
3945
3995
                version = NULL;
3946
 
 
3947
 
                /*
3948
 
                 * Find the database for this policy zone to get its
3949
 
                 * radix tree.
3950
 
                 */
3951
 
                result = rpz_getdb(client, rpz_type, &new_rpz->origin,
 
3996
                result = rpz_getdb(client, rpz_type, &rpz->origin,
3952
3997
                                   &zone, &db, &version);
3953
3998
                if (result != ISC_R_SUCCESS) {
3954
3999
                        rpz_clean(&zone, &db, NULL, NULL);
3960
4005
                 * hit, if any.  Note the domain name and quality of the
3961
4006
                 * best hit.
3962
4007
                 */
3963
 
                result = dns_db_rpz_findips(new_rpz, rpz_type, zone, db,
3964
 
                                            version, rdataset, st);
3965
 
                RUNTIME_CHECK(result == ISC_R_SUCCESS);
 
4008
                dns_db_rpz_findips(rpz, rpz_type, zone, db, version,
 
4009
                                   rdataset, st, client->query.rpz_st->qname);
3966
4010
                rpz_clean(&zone, &db, NULL, NULL);
3967
4011
        }
3968
4012
        return (ISC_R_SUCCESS);
3969
4013
}
3970
4014
 
 
4015
/*
 
4016
 * Look for an A or AAAA rdataset
 
4017
 * and check for IP or NSIP rewrite policy rules.
 
4018
 */
3971
4019
static isc_result_t
3972
 
rpz_rewrite_nsip(ns_client_t *client, dns_rdatatype_t type, dns_name_t *name,
3973
 
                 dns_db_t **dbp, dns_dbversion_t *version,
3974
 
                 dns_rdataset_t **rdatasetp, isc_boolean_t resuming)
 
4020
rpz_rewrite_rrset(ns_client_t *client, dns_rpz_type_t rpz_type,
 
4021
                  dns_rdatatype_t type, dns_name_t *name,
 
4022
                  dns_db_t **dbp, dns_dbversion_t *version,
 
4023
                  dns_rdataset_t **rdatasetp, isc_boolean_t resuming)
3975
4024
{
3976
4025
        isc_result_t result;
3977
4026
 
3978
 
        result = rpz_ns_find(client, name, type, dbp, version, rdatasetp,
3979
 
                             resuming);
 
4027
        result = rpz_rrset_find(client, rpz_type, name, type, dbp, version,
 
4028
                                rdatasetp, resuming);
3980
4029
        switch (result) {
3981
4030
        case ISC_R_SUCCESS:
3982
 
                result = rpz_rewrite_ip(client, *rdatasetp, DNS_RPZ_TYPE_NSIP);
 
4031
                result = rpz_rewrite_ip(client, *rdatasetp, rpz_type);
3983
4032
                break;
3984
4033
        case DNS_R_EMPTYNAME:
3985
4034
        case DNS_R_EMPTYWILD:
3987
4036
        case DNS_R_NCACHENXDOMAIN:
3988
4037
        case DNS_R_NXRRSET:
3989
4038
        case DNS_R_NCACHENXRRSET:
 
4039
        case ISC_R_NOTFOUND:
3990
4040
                result = ISC_R_SUCCESS;
3991
4041
                break;
3992
4042
        case DNS_R_DELEGATION:
3993
4043
        case DNS_R_DUPLICATE:
3994
4044
        case DNS_R_DROP:
3995
4045
                break;
 
4046
        case DNS_R_CNAME:
 
4047
        case DNS_R_DNAME:
 
4048
                rpz_log_fail(client, DNS_RPZ_DEBUG_LEVEL1, rpz_type,
 
4049
                             name, "NS address rewrite rrset ", result);
 
4050
                result = ISC_R_SUCCESS;
 
4051
                break;
3996
4052
        default:
3997
4053
                if (client->query.rpz_st->m.policy != DNS_RPZ_POLICY_ERROR) {
3998
4054
                        client->query.rpz_st->m.policy = DNS_RPZ_POLICY_ERROR;
3999
 
                        rpz_fail_log(client, ISC_LOG_WARNING, DNS_RPZ_TYPE_NSIP,
4000
 
                                     name, "NS address rewrite nsip ", result);
 
4055
                        rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL, rpz_type,
 
4056
                                     name, "NS address rewrite rrset ", result);
4001
4057
                }
4002
4058
                break;
4003
4059
        }
4005
4061
}
4006
4062
 
4007
4063
/*
 
4064
 * Look for both A and AAAA rdatasets
 
4065
 * and check for IP or NSIP rewrite policy rules.
 
4066
 * Look only for addresses that will be in the ANSWER section
 
4067
 * when checking for IP rules.
 
4068
 */
 
4069
static isc_result_t
 
4070
rpz_rewrite_rrsets(ns_client_t *client, dns_rpz_type_t rpz_type,
 
4071
                   dns_name_t *name, dns_rdatatype_t type,
 
4072
                   dns_rdataset_t **rdatasetp, isc_boolean_t resuming)
 
4073
{
 
4074
        dns_rpz_st_t *st;
 
4075
        dns_dbversion_t *version;
 
4076
        dns_db_t *ipdb;
 
4077
        isc_result_t result;
 
4078
 
 
4079
        st = client->query.rpz_st;
 
4080
        version = NULL;
 
4081
        ipdb = NULL;
 
4082
        if ((st->state & DNS_RPZ_DONE_IPv4) == 0 &&
 
4083
            ((rpz_type == DNS_RPZ_TYPE_NSIP) ?
 
4084
             (st->state & DNS_RPZ_HAVE_NSIPv4) :
 
4085
             (st->state & DNS_RPZ_HAVE_IP)) != 0 &&
 
4086
            (type == dns_rdatatype_any || type == dns_rdatatype_a)) {
 
4087
                result = rpz_rewrite_rrset(client, rpz_type, dns_rdatatype_a,
 
4088
                                           name, &ipdb, version, rdatasetp,
 
4089
                                           resuming);
 
4090
                if (result == ISC_R_SUCCESS)
 
4091
                        st->state |= DNS_RPZ_DONE_IPv4;
 
4092
        } else {
 
4093
                result = ISC_R_SUCCESS;
 
4094
        }
 
4095
        if (result == ISC_R_SUCCESS &&
 
4096
            ((rpz_type == DNS_RPZ_TYPE_NSIP) ?
 
4097
             (st->state & DNS_RPZ_HAVE_NSIPv6) :
 
4098
             (st->state & DNS_RPZ_HAVE_IP)) != 0 &&
 
4099
            (type == dns_rdatatype_any || type == dns_rdatatype_aaaa)) {
 
4100
                result = rpz_rewrite_rrset(client, rpz_type, dns_rdatatype_aaaa,
 
4101
                                           name, &ipdb, version, rdatasetp,
 
4102
                                           resuming);
 
4103
        }
 
4104
        if (ipdb != NULL)
 
4105
                dns_db_detach(&ipdb);
 
4106
        return (result);
 
4107
}
 
4108
 
 
4109
/*
4008
4110
 * Get the rrset from a response policy zone.
4009
4111
 */
4010
4112
static isc_result_t
4011
4113
rpz_find(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qnamef,
4012
 
         dns_name_t *sname, dns_rpz_type_t rpz_type, dns_zone_t **zonep,
4013
 
         dns_db_t **dbp, dns_dbnode_t **nodep, dns_rdataset_t **rdatasetp,
 
4114
         dns_name_t *sname, dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
 
4115
         dns_zone_t **zonep, dns_db_t **dbp, dns_dbversion_t **versionp,
 
4116
         dns_dbnode_t **nodep, dns_rdataset_t **rdatasetp,
4014
4117
         dns_rpz_policy_t *policyp)
4015
4118
{
4016
 
        dns_dbversion_t *version;
4017
4119
        dns_rpz_policy_t policy;
4018
4120
        dns_fixedname_t fixed;
4019
4121
        dns_name_t *found;
4029
4131
         * Try to get either a CNAME or the type of record demanded by the
4030
4132
         * request from the policy zone.
4031
4133
         */
4032
 
        version = NULL;
4033
 
        result = rpz_getdb(client, rpz_type, qnamef, zonep, dbp, &version);
 
4134
        *versionp = NULL;
 
4135
        result = rpz_getdb(client, rpz_type, qnamef, zonep, dbp, versionp);
4034
4136
        if (result != ISC_R_SUCCESS) {
4035
4137
                *policyp = DNS_RPZ_POLICY_MISS;
4036
4138
                return (DNS_R_NXDOMAIN);
4038
4140
 
4039
4141
        dns_fixedname_init(&fixed);
4040
4142
        found = dns_fixedname_name(&fixed);
4041
 
        result = dns_db_find(*dbp, qnamef, version, dns_rdatatype_any, 0,
 
4143
        result = dns_db_find(*dbp, qnamef, *versionp, dns_rdatatype_any, 0,
4042
4144
                             client->now, nodep, found, *rdatasetp, NULL);
4043
4145
        if (result == ISC_R_SUCCESS) {
4044
4146
                dns_rdatasetiter_t *rdsiter;
4045
4147
 
4046
4148
                rdsiter = NULL;
4047
 
                result = dns_db_allrdatasets(*dbp, *nodep, version, 0,
 
4149
                result = dns_db_allrdatasets(*dbp, *nodep, *versionp, 0,
4048
4150
                                             &rdsiter);
4049
4151
                if (result != ISC_R_SUCCESS) {
4050
4152
                        dns_db_detachnode(*dbp, nodep);
4051
 
                        rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL, rpz_type,
4052
 
                                     qnamef, "allrdatasets()", result);
 
4153
                        rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL, rpz_type,
 
4154
                                     qnamef, "allrdatasets() ", result);
4053
4155
                        *policyp = DNS_RPZ_POLICY_ERROR;
4054
4156
                        return (DNS_R_SERVFAIL);
4055
4157
                }
4065
4167
                dns_rdatasetiter_destroy(&rdsiter);
4066
4168
                if (result != ISC_R_SUCCESS) {
4067
4169
                        if (result != ISC_R_NOMORE) {
4068
 
                                rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL,
4069
 
                                             rpz_type, qnamef, "rdatasetiter",
 
4170
                                rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL,
 
4171
                                             rpz_type, qnamef, "rdatasetiter ",
4070
4172
                                             result);
4071
4173
                                *policyp = DNS_RPZ_POLICY_ERROR;
4072
4174
                                return (DNS_R_SERVFAIL);
4083
4185
                            qtype == dns_rdatatype_sig)
4084
4186
                                result = DNS_R_NXRRSET;
4085
4187
                        else
4086
 
                                result = dns_db_find(*dbp, qnamef, version,
 
4188
                                result = dns_db_find(*dbp, qnamef, *versionp,
4087
4189
                                                     qtype, 0, client->now,
4088
4190
                                                     nodep, found, *rdatasetp,
4089
4191
                                                     NULL);
4094
4196
                if ((*rdatasetp)->type != dns_rdatatype_cname) {
4095
4197
                        policy = DNS_RPZ_POLICY_RECORD;
4096
4198
                } else {
4097
 
                        policy = dns_rpz_decode_cname(*rdatasetp, sname);
4098
 
                        if (policy == DNS_RPZ_POLICY_RECORD &&
 
4199
                        policy = dns_rpz_decode_cname(rpz, *rdatasetp, sname);
 
4200
                        if ((policy == DNS_RPZ_POLICY_RECORD ||
 
4201
                             policy == DNS_RPZ_POLICY_WILDCNAME) &&
4099
4202
                            qtype != dns_rdatatype_cname &&
4100
4203
                            qtype != dns_rdatatype_any)
4101
4204
                                result = DNS_R_CNAME;
4106
4209
                 * DNAME policy RRs have very few if any uses that are not
4107
4210
                 * better served with simple wildcards.  Making the work would
4108
4211
                 * require complications to get the number of labels matched
4109
 
                 * in the name or the found name itself to the main DNS_R_DNAME
4110
 
                 * case in query_find(). So fall through to treat them as NODATA.
 
4212
                 * in the name or the found name to the main DNS_R_DNAME case
 
4213
                 * in query_find(). So fall through to treat them as NODATA.
4111
4214
                 */
4112
4215
        case DNS_R_NXRRSET:
4113
4216
                policy = DNS_RPZ_POLICY_NODATA;
4126
4229
        default:
4127
4230
                dns_db_detach(dbp);
4128
4231
                dns_zone_detach(zonep);
4129
 
                rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL, rpz_type, qnamef,
 
4232
                rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL, rpz_type, qnamef,
4130
4233
                             "", result);
4131
4234
                policy = DNS_RPZ_POLICY_ERROR;
4132
4235
                result = DNS_R_SERVFAIL;
4150
4253
        dns_name_t *prefix, *suffix, *rpz_qname;
4151
4254
        dns_zone_t *zone;
4152
4255
        dns_db_t *db;
 
4256
        dns_dbversion_t *version;
4153
4257
        dns_dbnode_t *node;
4154
4258
        dns_rpz_policy_t policy;
4155
4259
        unsigned int labels;
4163
4267
        for (rpz = ISC_LIST_HEAD(client->view->rpz_zones);
4164
4268
             rpz != NULL;
4165
4269
             rpz = ISC_LIST_NEXT(rpz, link)) {
4166
 
                /*
4167
 
                 * Construct the rule's owner name.
 
4270
                if (!RECURSIONOK(client) && rpz->recursive_only)
 
4271
                        continue;
 
4272
 
 
4273
                /*
 
4274
                 * Do not check policy zones that cannot replace a policy
 
4275
                 * already known to match.
 
4276
                 */
 
4277
                if (st->m.policy != DNS_RPZ_POLICY_MISS) {
 
4278
                        if (st->m.rpz->num < rpz->num)
 
4279
                                break;
 
4280
                        if (st->m.rpz->num == rpz->num &&
 
4281
                            st->m.type < rpz_type)
 
4282
                                continue;
 
4283
                }
 
4284
                /*
 
4285
                 * Construct the policy's owner name.
4168
4286
                 */
4169
4287
                dns_fixedname_init(&prefixf);
4170
4288
                prefix = dns_fixedname_name(&prefixf);
4183
4301
                        INSIST(result == DNS_R_NAMETOOLONG);
4184
4302
                        labels = dns_name_countlabels(prefix);
4185
4303
                        if (labels < 2) {
4186
 
                                rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL,
 
4304
                                rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL,
4187
4305
                                             rpz_type, suffix,
4188
4306
                                             "concatentate() ", result);
4189
4307
                                return (ISC_R_SUCCESS);
4190
4308
                        }
4191
4309
                        if (labels+1 == dns_name_countlabels(qname)) {
4192
 
                                rpz_fail_log(client, DNS_RPZ_DEBUG_LEVEL1,
 
4310
                                rpz_log_fail(client, DNS_RPZ_DEBUG_LEVEL1,
4193
4311
                                             rpz_type, suffix,
4194
4312
                                             "concatentate() ", result);
4195
4313
                        }
4197
4315
                }
4198
4316
 
4199
4317
                /*
4200
 
                 * See if the qname rule (or RR) exists.
 
4318
                 * See if the policy record exists and get its policy.
4201
4319
                 */
4202
 
                result = rpz_find(client, qtype, rpz_qname, qname, rpz_type,
4203
 
                                  &zone, &db, &node, rdatasetp, &policy);
 
4320
                result = rpz_find(client, qtype, rpz_qname, qname, rpz,
 
4321
                                  rpz_type, &zone, &db, &version, &node,
 
4322
                                  rdatasetp, &policy);
4204
4323
                switch (result) {
4205
4324
                case DNS_R_NXDOMAIN:
4206
4325
                case DNS_R_EMPTYNAME:
4211
4330
                        return (DNS_R_SERVFAIL);
4212
4331
                default:
4213
4332
                        /*
4214
 
                         * when more than one name or address hits a rule,
4215
 
                         * prefer the first set of names (qname or NS),
4216
 
                         * the first policy zone, and the smallest name
4217
 
                         */
4218
 
                        if (st->m.type == rpz_type &&
4219
 
                            rpz->num > st->m.rpz->num &&
4220
 
                            0 <= dns_name_compare(rpz_qname, st->qname))
4221
 
                                continue;
4222
 
                        rpz_clean(&st->m.zone, &st->m.db, &st->m.node,
4223
 
                                  &st->m.rdataset);
 
4333
                         * We are dealing with names here.
 
4334
                         * With more than one applicable policy, prefer
 
4335
                         * the earliest configured policy,
 
4336
                         * QNAME over IP over NSDNAME over NSIP,
 
4337
                         * and the smallest name.
 
4338
                         * Because of the testing above,
 
4339
                         * we known st->m.rpz->num >= rpz->num  and either
 
4340
                         * st->m.rpz->num > rpz->num or st->m.type >= rpz_type
 
4341
                         */
 
4342
                        if (st->m.policy != DNS_RPZ_POLICY_MISS &&
 
4343
                            rpz->num == st->m.rpz->num &&
 
4344
                            (st->m.type < rpz_type ||
 
4345
                             (st->m.type == rpz_type &&
 
4346
                              0 >= dns_name_compare(rpz_qname, st->qname))))
 
4347
                                continue;
 
4348
 
 
4349
                        /*
 
4350
                         * Merely log DNS_RPZ_POLICY_DISABLED hits.
 
4351
                         */
 
4352
                        if (rpz->policy == DNS_RPZ_POLICY_DISABLED) {
 
4353
                                rpz_log_rewrite(client, "disabled ",
 
4354
                                                policy, rpz_type, rpz_qname);
 
4355
                                continue;
 
4356
                        }
 
4357
 
 
4358
                        rpz_match_clear(st);
4224
4359
                        st->m.rpz = rpz;
4225
4360
                        st->m.type = rpz_type;
4226
4361
                        st->m.prefix = 0;
4227
4362
                        st->m.policy = policy;
4228
4363
                        st->m.result = result;
4229
4364
                        dns_name_copy(rpz_qname, st->qname, NULL);
4230
 
                        if (dns_rdataset_isassociated(*rdatasetp)) {
 
4365
                        if (*rdatasetp != NULL &&
 
4366
                            dns_rdataset_isassociated(*rdatasetp)) {
4231
4367
                                dns_rdataset_t *trdataset;
4232
4368
 
4233
4369
                                trdataset = st->m.rdataset;
4234
4370
                                st->m.rdataset = *rdatasetp;
4235
4371
                                *rdatasetp = trdataset;
4236
 
                                st->m.ttl = st->m.rdataset->ttl;
 
4372
                                st->m.ttl = ISC_MIN(st->m.rdataset->ttl,
 
4373
                                                    rpz->max_policy_ttl);
4237
4374
                        } else {
4238
 
                                st->m.ttl = DNS_RPZ_TTL_DEFAULT;
 
4375
                                st->m.ttl = ISC_MIN(DNS_RPZ_TTL_DEFAULT,
 
4376
                                                    rpz->max_policy_ttl);
4239
4377
                        }
4240
4378
                        st->m.node = node;
4241
4379
                        node = NULL;
4242
4380
                        st->m.db = db;
4243
4381
                        db = NULL;
 
4382
                        st->m.version = version;
4244
4383
                        st->m.zone = zone;
4245
4384
                        zone = NULL;
4246
4385
                }
4250
4389
        return (ISC_R_SUCCESS);
4251
4390
}
4252
4391
 
 
4392
static void
 
4393
rpz_rewrite_ns_skip(ns_client_t *client, dns_name_t *nsname,
 
4394
                    isc_result_t result, int level, const char *str)
 
4395
{
 
4396
        dns_rpz_st_t *st;
 
4397
 
 
4398
        st = client->query.rpz_st;
 
4399
 
 
4400
        if (str != NULL)
 
4401
                rpz_log_fail(client, level, DNS_RPZ_TYPE_NSIP, nsname,
 
4402
                             str, result);
 
4403
        if (st->r.ns_rdataset != NULL &&
 
4404
            dns_rdataset_isassociated(st->r.ns_rdataset))
 
4405
                dns_rdataset_disassociate(st->r.ns_rdataset);
 
4406
 
 
4407
        st->r.label--;
 
4408
}
 
4409
 
4253
4410
/*
4254
 
 * Look for response policy zone NSIP and NSDNAME rewriting.
 
4411
 * Look for response policy zone QNAME, NSIP, and NSDNAME rewriting.
4255
4412
 */
4256
4413
static isc_result_t
4257
 
rpz_rewrite(ns_client_t *client, dns_rdatatype_t qtype,
 
4414
rpz_rewrite(ns_client_t *client, dns_rdatatype_t qtype, isc_result_t qresult,
4258
4415
            isc_boolean_t resuming)
4259
4416
{
4260
4417
        dns_rpz_st_t *st;
4261
 
        dns_db_t *ipdb;
4262
4418
        dns_rdataset_t *rdataset;
4263
4419
        dns_fixedname_t nsnamef;
4264
4420
        dns_name_t *nsname;
4265
 
        dns_dbversion_t *version;
 
4421
        isc_boolean_t ck_ip;
4266
4422
        isc_result_t result;
4267
4423
 
4268
 
        ipdb = NULL;
4269
 
        rdataset = NULL;
4270
 
 
4271
4424
        st = client->query.rpz_st;
4272
4425
        if (st == NULL) {
4273
4426
                st = isc_mem_get(client->mctx, sizeof(*st));
4275
4428
                        return (ISC_R_NOMEMORY);
4276
4429
                st->state = 0;
4277
4430
                memset(&st->m, 0, sizeof(st->m));
4278
 
                memset(&st->ns, 0, sizeof(st->ns));
 
4431
                st->m.type = DNS_RPZ_TYPE_BAD;
 
4432
                st->m.policy = DNS_RPZ_POLICY_MISS;
 
4433
                memset(&st->r, 0, sizeof(st->r));
4279
4434
                memset(&st->q, 0, sizeof(st->q));
4280
4435
                dns_fixedname_init(&st->_qnamef);
4281
4436
                dns_fixedname_init(&st->_r_namef);
4285
4440
                st->fname = dns_fixedname_name(&st->_fnamef);
4286
4441
                client->query.rpz_st = st;
4287
4442
        }
 
4443
 
 
4444
        /*
 
4445
         * There is nothing to rewrite if the main query failed.
 
4446
         */
 
4447
        switch (qresult) {
 
4448
        case ISC_R_SUCCESS:
 
4449
        case DNS_R_GLUE:
 
4450
        case DNS_R_ZONECUT:
 
4451
                ck_ip = ISC_TRUE;
 
4452
                break;
 
4453
        case DNS_R_EMPTYNAME:
 
4454
        case DNS_R_NXRRSET:
 
4455
        case DNS_R_NXDOMAIN:
 
4456
        case DNS_R_EMPTYWILD:
 
4457
        case DNS_R_NCACHENXDOMAIN:
 
4458
        case DNS_R_NCACHENXRRSET:
 
4459
        case DNS_R_CNAME:
 
4460
        case DNS_R_DNAME:
 
4461
                ck_ip = ISC_FALSE;
 
4462
                break;
 
4463
        case DNS_R_DELEGATION:
 
4464
        case ISC_R_NOTFOUND:
 
4465
                return (ISC_R_SUCCESS);
 
4466
        case ISC_R_FAILURE:
 
4467
        case ISC_R_TIMEDOUT:
 
4468
        case DNS_R_BROKENCHAIN:
 
4469
                rpz_log_fail(client, DNS_RPZ_DEBUG_LEVEL3, DNS_RPZ_TYPE_QNAME,
 
4470
                             client->query.qname,
 
4471
                             "stop on qresult in rpz_rewrite() ",
 
4472
                             qresult);
 
4473
                return (ISC_R_SUCCESS);
 
4474
        default:
 
4475
                rpz_log_fail(client, DNS_RPZ_DEBUG_LEVEL1, DNS_RPZ_TYPE_QNAME,
 
4476
                             client->query.qname,
 
4477
                             "stop on unrecognized qresult in rpz_rewrite() ",
 
4478
                             qresult);
 
4479
                return (ISC_R_SUCCESS);
 
4480
        }
 
4481
 
 
4482
        rdataset = NULL;
4288
4483
        if ((st->state & DNS_RPZ_DONE_QNAME) == 0) {
4289
 
                st->state = DNS_RPZ_DONE_QNAME;
4290
 
                st->m.type = DNS_RPZ_TYPE_BAD;
4291
 
                st->m.policy = DNS_RPZ_POLICY_MISS;
4292
 
 
4293
4484
                /*
4294
 
                 * Check rules for the name if this it the first time,
4295
 
                 * i.e. we've not been recursing.
 
4485
                 * Check rules for the query name if this it the first time
 
4486
                 * for the current qname, i.e. we've not been recursing.
 
4487
                 * There is a first time for each name in a CNAME chain.
4296
4488
                 */
4297
 
                st->state &= ~(DNS_RPZ_HAVE_IP | DNS_RPZ_HAVE_NSIPv4 |
4298
 
                               DNS_RPZ_HAVE_NSIPv6 | DNS_RPZ_HAD_NSDNAME);
4299
4489
                result = rpz_rewrite_name(client, qtype, client->query.qname,
4300
4490
                                          DNS_RPZ_TYPE_QNAME, &rdataset);
4301
4491
                if (result != ISC_R_SUCCESS)
4302
4492
                        goto cleanup;
4303
 
                if (st->m.policy != DNS_RPZ_POLICY_MISS)
4304
 
                        goto cleanup;
4305
 
                if ((st->state & (DNS_RPZ_HAVE_NSIPv4 | DNS_RPZ_HAVE_NSIPv6 |
4306
 
                                  DNS_RPZ_HAD_NSDNAME)) == 0)
4307
 
                        goto cleanup;
4308
 
                st->ns.label = dns_name_countlabels(client->query.qname);
 
4493
 
 
4494
                st->r.label = dns_name_countlabels(client->query.qname);
 
4495
 
 
4496
                st->state &= ~(DNS_RPZ_DONE_QNAME_IP | DNS_RPZ_DONE_IPv4);
 
4497
                st->state |= DNS_RPZ_DONE_QNAME;
 
4498
        }
 
4499
 
 
4500
        /*
 
4501
         * Check known IP addresses for the query name.
 
4502
         * Any recursion required for the query has already happened.
 
4503
         * Do not check addresses that will not be in the ANSWER section.
 
4504
         */
 
4505
        if ((st->state & DNS_RPZ_DONE_QNAME_IP) == 0 &&
 
4506
            (st->state & DNS_RPZ_HAVE_IP) != 0 && ck_ip) {
 
4507
                result = rpz_rewrite_rrsets(client, DNS_RPZ_TYPE_IP,
 
4508
                                            client->query.qname, qtype,
 
4509
                                            &rdataset, resuming);
 
4510
                if (result != ISC_R_SUCCESS)
 
4511
                        goto cleanup;
 
4512
                st->state &= ~DNS_RPZ_DONE_IPv4;
 
4513
                st->state |= DNS_RPZ_DONE_QNAME_IP;
 
4514
        }
 
4515
 
 
4516
        /*
 
4517
         * Stop looking for rules if there are none of the other kinds.
 
4518
         */
 
4519
        if ((st->state & (DNS_RPZ_HAVE_NSIPv4 | DNS_RPZ_HAVE_NSIPv6 |
 
4520
                          DNS_RPZ_HAVE_NSDNAME)) == 0) {
 
4521
                result = ISC_R_SUCCESS;
 
4522
                goto cleanup;
4309
4523
        }
4310
4524
 
4311
4525
        dns_fixedname_init(&nsnamef);
4312
4526
        dns_name_clone(client->query.qname, dns_fixedname_name(&nsnamef));
4313
 
        while (st->ns.label > 1 && st->m.policy == DNS_RPZ_POLICY_MISS) {
4314
 
                if (st->ns.label == dns_name_countlabels(client->query.qname)) {
 
4527
        while (st->r.label > 1) {
 
4528
                /*
 
4529
                 * Get NS rrset for each domain in the current qname.
 
4530
                 */
 
4531
                if (st->r.label == dns_name_countlabels(client->query.qname)) {
4315
4532
                        nsname = client->query.qname;
4316
4533
                } else {
4317
4534
                        nsname = dns_fixedname_name(&nsnamef);
4318
 
                        dns_name_split(client->query.qname, st->ns.label,
 
4535
                        dns_name_split(client->query.qname, st->r.label,
4319
4536
                                       NULL, nsname);
4320
4537
                }
4321
 
                if (st->ns.ns_rdataset == NULL ||
4322
 
                    !dns_rdataset_isassociated(st->ns.ns_rdataset)) {
 
4538
                if (st->r.ns_rdataset == NULL ||
 
4539
                    !dns_rdataset_isassociated(st->r.ns_rdataset)) {
4323
4540
                        dns_db_t *db = NULL;
4324
 
                        result = rpz_ns_find(client, nsname, dns_rdatatype_ns,
4325
 
                                             &db, NULL, &st->ns.ns_rdataset,
4326
 
                                             resuming);
 
4541
                        result = rpz_rrset_find(client, DNS_RPZ_TYPE_NSDNAME,
 
4542
                                                nsname, dns_rdatatype_ns,
 
4543
                                                &db, NULL, &st->r.ns_rdataset,
 
4544
                                                resuming);
4327
4545
                        if (db != NULL)
4328
4546
                                dns_db_detach(&db);
4329
 
                        if (result != ISC_R_SUCCESS) {
4330
 
                                if (result == DNS_R_DELEGATION)
 
4547
                        if (st->m.policy == DNS_RPZ_POLICY_ERROR)
 
4548
                                goto cleanup;
 
4549
                        switch (result) {
 
4550
                        case ISC_R_SUCCESS:
 
4551
                                result = dns_rdataset_first(st->r.ns_rdataset);
 
4552
                                if (result != ISC_R_SUCCESS)
4331
4553
                                        goto cleanup;
4332
 
                                if (result == DNS_R_EMPTYNAME ||
4333
 
                                    result == DNS_R_NXRRSET ||
4334
 
                                    result == DNS_R_EMPTYWILD ||
4335
 
                                    result == DNS_R_NXDOMAIN ||
4336
 
                                    result == DNS_R_NCACHENXDOMAIN ||
4337
 
                                    result == DNS_R_NCACHENXRRSET ||
4338
 
                                    result == DNS_R_CNAME ||
4339
 
                                    result == DNS_R_DNAME) {
4340
 
                                        rpz_fail_log(client,
4341
 
                                                     DNS_RPZ_DEBUG_LEVEL2,
4342
 
                                                     DNS_RPZ_TYPE_NSIP, nsname,
4343
 
                                                     "NS db_find() ", result);
4344
 
                                        dns_rdataset_disassociate(st->ns.
4345
 
                                                        ns_rdataset);
4346
 
                                        st->ns.label--;
4347
 
                                        continue;
4348
 
                                }
4349
 
                                if (st->m.policy != DNS_RPZ_POLICY_ERROR) {
4350
 
                                        rpz_fail_log(client, DNS_RPZ_INFO_LEVEL,
4351
 
                                                     DNS_RPZ_TYPE_NSIP, nsname,
4352
 
                                                     "NS db_find() ", result);
4353
 
                                        st->m.policy = DNS_RPZ_POLICY_ERROR;
4354
 
                                }
 
4554
                                st->state &= ~(DNS_RPZ_DONE_NSDNAME |
 
4555
                                               DNS_RPZ_DONE_IPv4);
 
4556
                                break;
 
4557
                        case DNS_R_DELEGATION:
4355
4558
                                goto cleanup;
 
4559
                        case DNS_R_EMPTYNAME:
 
4560
                        case DNS_R_NXRRSET:
 
4561
                        case DNS_R_EMPTYWILD:
 
4562
                        case DNS_R_NXDOMAIN:
 
4563
                        case DNS_R_NCACHENXDOMAIN:
 
4564
                        case DNS_R_NCACHENXRRSET:
 
4565
                        case ISC_R_NOTFOUND:
 
4566
                        case DNS_R_CNAME:
 
4567
                        case DNS_R_DNAME:
 
4568
                                rpz_rewrite_ns_skip(client, nsname, result,
 
4569
                                                    0, NULL);
 
4570
                                continue;
 
4571
                        case ISC_R_TIMEDOUT:
 
4572
                        case DNS_R_BROKENCHAIN:
 
4573
                        case ISC_R_FAILURE:
 
4574
                                rpz_rewrite_ns_skip(client, nsname, result,
 
4575
                                                DNS_RPZ_DEBUG_LEVEL3,
 
4576
                                                "NS db_find() ");
 
4577
                                continue;
 
4578
                        default:
 
4579
                                rpz_rewrite_ns_skip(client, nsname, result,
 
4580
                                                DNS_RPZ_INFO_LEVEL,
 
4581
                                                "unrecognized NS db_find() ");
 
4582
                                continue;
4356
4583
                        }
4357
 
                        result = dns_rdataset_first(st->ns.ns_rdataset);
4358
 
                        if (result != ISC_R_SUCCESS)
4359
 
                                goto cleanup;
4360
4584
                }
4361
4585
                /*
4362
4586
                 * Check all NS names.
4365
4589
                        dns_rdata_ns_t ns;
4366
4590
                        dns_rdata_t nsrdata = DNS_RDATA_INIT;
4367
4591
 
4368
 
                        dns_rdataset_current(st->ns.ns_rdataset, &nsrdata);
 
4592
                        dns_rdataset_current(st->r.ns_rdataset, &nsrdata);
4369
4593
                        result = dns_rdata_tostruct(&nsrdata, &ns, NULL);
4370
4594
                        dns_rdata_reset(&nsrdata);
4371
4595
                        if (result != ISC_R_SUCCESS) {
4372
 
                                rpz_fail_log(client, DNS_RPZ_ERROR_LEVEL,
 
4596
                                rpz_log_fail(client, DNS_RPZ_ERROR_LEVEL,
4373
4597
                                             DNS_RPZ_TYPE_NSIP, nsname,
4374
4598
                                             "rdata_tostruct() ", result);
4375
4599
                                st->m.policy = DNS_RPZ_POLICY_ERROR;
4376
4600
                                goto cleanup;
4377
4601
                        }
4378
 
                        if ((st->state & DNS_RPZ_HAD_NSDNAME) != 0) {
 
4602
                        /*
 
4603
                         * Do nothing about "NS ."
 
4604
                         */
 
4605
                        if (dns_name_equal(&ns.name, dns_rootname)) {
 
4606
                                dns_rdata_freestruct(&ns);
 
4607
                                result = dns_rdataset_next(st->r.ns_rdataset);
 
4608
                                continue;
 
4609
                        }
 
4610
                        /*
 
4611
                         * Check this NS name if we did not handle it
 
4612
                         * during a previous recursion.
 
4613
                         */
 
4614
                        if ((st->state & DNS_RPZ_DONE_NSDNAME) == 0 &&
 
4615
                            (st->state & DNS_RPZ_HAVE_NSDNAME) != 0) {
4379
4616
                                result = rpz_rewrite_name(client, qtype,
4380
4617
                                                        &ns.name,
4381
4618
                                                        DNS_RPZ_TYPE_NSDNAME,
4384
4621
                                        dns_rdata_freestruct(&ns);
4385
4622
                                        goto cleanup;
4386
4623
                                }
 
4624
                                st->state |= DNS_RPZ_DONE_NSDNAME;
4387
4625
                        }
4388
4626
                        /*
4389
 
                         * Check all IP addresses for this NS name, but don't
4390
 
                         * bother without NSIP rules or with a NSDNAME hit.
 
4627
                         * Check all IP addresses for this NS name.
4391
4628
                         */
4392
 
                        version = NULL;
4393
 
                        if ((st->state & DNS_RPZ_HAVE_NSIPv4) != 0 &&
4394
 
                            st->m.type != DNS_RPZ_TYPE_NSDNAME &&
4395
 
                            (st->state & DNS_RPZ_DONE_A) == 0) {
4396
 
                                result = rpz_rewrite_nsip(client,
4397
 
                                                          dns_rdatatype_a,
4398
 
                                                          &ns.name, &ipdb,
4399
 
                                                          version, &rdataset,
4400
 
                                                          resuming);
4401
 
                                if (result == ISC_R_SUCCESS)
4402
 
                                        st->state |= DNS_RPZ_DONE_A;
4403
 
                        }
4404
 
                        if (result == ISC_R_SUCCESS &&
4405
 
                            (st->state & DNS_RPZ_HAVE_NSIPv6) != 0 &&
4406
 
                            st->m.type != DNS_RPZ_TYPE_NSDNAME) {
4407
 
                                result = rpz_rewrite_nsip(client,
4408
 
                                                          dns_rdatatype_aaaa,
4409
 
                                                          &ns.name, &ipdb,
4410
 
                                                          version, &rdataset,
4411
 
                                                          resuming);
4412
 
                        }
 
4629
                        result = rpz_rewrite_rrsets(client, DNS_RPZ_TYPE_NSIP,
 
4630
                                                    &ns.name, dns_rdatatype_any,
 
4631
                                                    &rdataset, resuming);
4413
4632
                        dns_rdata_freestruct(&ns);
4414
 
                        if (ipdb != NULL)
4415
 
                                dns_db_detach(&ipdb);
4416
4633
                        if (result != ISC_R_SUCCESS)
4417
4634
                                goto cleanup;
4418
 
                        st->state &= ~DNS_RPZ_DONE_A;
4419
 
                        result = dns_rdataset_next(st->ns.ns_rdataset);
 
4635
                        st->state &= ~(DNS_RPZ_DONE_NSDNAME |
 
4636
                                       DNS_RPZ_DONE_IPv4);
 
4637
                        result = dns_rdataset_next(st->r.ns_rdataset);
4420
4638
                } while (result == ISC_R_SUCCESS);
4421
 
                dns_rdataset_disassociate(st->ns.ns_rdataset);
4422
 
                st->ns.label--;
 
4639
                dns_rdataset_disassociate(st->r.ns_rdataset);
 
4640
                st->r.label--;
4423
4641
        }
4424
4642
 
4425
4643
        /*
4429
4647
 
4430
4648
cleanup:
4431
4649
        if (st->m.policy != DNS_RPZ_POLICY_MISS &&
4432
 
            st->m.policy != DNS_RPZ_POLICY_NO_OP &&
4433
4650
            st->m.policy != DNS_RPZ_POLICY_ERROR &&
4434
4651
            st->m.rpz->policy != DNS_RPZ_POLICY_GIVEN)
4435
4652
                st->m.policy = st->m.rpz->policy;
4436
 
        if (st->m.policy == DNS_RPZ_POLICY_NO_OP)
4437
 
                rpz_log(client);
4438
4653
        if (st->m.policy == DNS_RPZ_POLICY_MISS ||
4439
 
            st->m.policy == DNS_RPZ_POLICY_NO_OP ||
4440
 
            st->m.policy == DNS_RPZ_POLICY_ERROR)
4441
 
                rpz_clean(&st->m.zone, &st->m.db, &st->m.node, &st->m.rdataset);
4442
 
        if (st->m.policy != DNS_RPZ_POLICY_MISS)
4443
 
                st->state |= DNS_RPZ_REWRITTEN;
 
4654
            st->m.policy == DNS_RPZ_POLICY_PASSTHRU ||
 
4655
            st->m.policy == DNS_RPZ_POLICY_ERROR) {
 
4656
                if (st->m.policy == DNS_RPZ_POLICY_PASSTHRU &&
 
4657
                    result != DNS_R_DELEGATION)
 
4658
                        rpz_log_rewrite(client, "", st->m.policy, st->m.type,
 
4659
                                        st->qname);
 
4660
                rpz_match_clear(st);
 
4661
        }
4444
4662
        if (st->m.policy == DNS_RPZ_POLICY_ERROR) {
4445
4663
                st->m.type = DNS_RPZ_TYPE_BAD;
4446
4664
                result = DNS_R_SERVFAIL;
4447
4665
        }
4448
 
        if (rdataset != NULL)
4449
 
                query_putrdataset(client, &rdataset);
4450
 
        if ((st->state & DNS_RPZ_RECURSING) == 0) {
4451
 
                rpz_clean(NULL, &st->ns.db, NULL, &st->ns.ns_rdataset);
4452
 
        }
 
4666
        query_putrdataset(client, &rdataset);
 
4667
        if ((st->state & DNS_RPZ_RECURSING) == 0)
 
4668
                rpz_clean(NULL, &st->r.db, NULL, &st->r.ns_rdataset);
4453
4669
 
4454
4670
        return (result);
4455
4671
}
4456
4672
 
 
4673
/*
 
4674
 * See if response policy zone rewriting is allowed a lack of interest
 
4675
 * by the client in DNSSEC or a lack of signatures.
 
4676
 */
 
4677
static isc_boolean_t
 
4678
rpz_ck_dnssec(ns_client_t *client, isc_result_t result,
 
4679
              dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
 
4680
{
 
4681
        dns_fixedname_t fixed;
 
4682
        dns_name_t *found;
 
4683
        dns_rdataset_t trdataset;
 
4684
        dns_rdatatype_t type;
 
4685
 
 
4686
        if (client->view->rpz_break_dnssec)
 
4687
                return (ISC_TRUE);
 
4688
        /*
 
4689
         * sigrdataset == NULL if and only !WANTDNSSEC(client)
 
4690
         */
 
4691
        if (sigrdataset == NULL)
 
4692
                return (ISC_TRUE);
 
4693
        if (dns_rdataset_isassociated(sigrdataset))
 
4694
                return (ISC_FALSE);
 
4695
 
 
4696
        /*
 
4697
         * We are happy to rewrite nothing.
 
4698
         */
 
4699
        if (rdataset == NULL || !dns_rdataset_isassociated(rdataset))
 
4700
                return (ISC_TRUE);
 
4701
        /*
 
4702
         * Do not rewrite if there is any sign of signatures.
 
4703
         */
 
4704
        if (rdataset->type == dns_rdatatype_nsec ||
 
4705
            rdataset->type == dns_rdatatype_nsec3 ||
 
4706
            rdataset->type == dns_rdatatype_rrsig)
 
4707
                return (ISC_FALSE);
 
4708
 
 
4709
        /*
 
4710
         * Look for a signature in a negative cache rdataset.
 
4711
         */
 
4712
        if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) == 0)
 
4713
                return (ISC_TRUE);
 
4714
        dns_fixedname_init(&fixed);
 
4715
        found = dns_fixedname_name(&fixed);
 
4716
        dns_rdataset_init(&trdataset);
 
4717
        for (result = dns_rdataset_first(rdataset);
 
4718
             result == ISC_R_SUCCESS;
 
4719
             result = dns_rdataset_next(rdataset)) {
 
4720
                dns_ncache_current(rdataset, found, &trdataset);
 
4721
                type = trdataset.type;
 
4722
                dns_rdataset_disassociate(&trdataset);
 
4723
                if (type == dns_rdatatype_nsec ||
 
4724
                    type == dns_rdatatype_nsec3 ||
 
4725
                    type == dns_rdatatype_rrsig)
 
4726
                        return (ISC_FALSE);
 
4727
        }
 
4728
        return (ISC_TRUE);
 
4729
}
 
4730
 
 
4731
/*
 
4732
 * Add a CNAME to the query response, including translating foo.evil.com and
 
4733
 *      *.evil.com CNAME *.example.com
 
4734
 * to
 
4735
 *      foo.evil.com CNAME foo.evil.com.example.com
 
4736
 */
 
4737
static isc_result_t
 
4738
rpz_add_cname(ns_client_t *client, dns_rpz_st_t *st,
 
4739
              dns_name_t *cname, dns_name_t *fname, isc_buffer_t *dbuf)
 
4740
{
 
4741
        dns_fixedname_t prefix, suffix;
 
4742
        unsigned int labels;
 
4743
        isc_result_t result;
 
4744
 
 
4745
        labels = dns_name_countlabels(cname);
 
4746
        if (labels > 2 && dns_name_iswildcard(cname)) {
 
4747
                dns_fixedname_init(&prefix);
 
4748
                dns_name_split(client->query.qname, 1,
 
4749
                               dns_fixedname_name(&prefix), NULL);
 
4750
                dns_fixedname_init(&suffix);
 
4751
                dns_name_split(cname, labels-1,
 
4752
                               NULL, dns_fixedname_name(&suffix));
 
4753
                result = dns_name_concatenate(dns_fixedname_name(&prefix),
 
4754
                                              dns_fixedname_name(&suffix),
 
4755
                                              fname, NULL);
 
4756
                if (result == DNS_R_NAMETOOLONG)
 
4757
                        client->message->rcode = dns_rcode_yxdomain;
 
4758
        } else {
 
4759
                result = dns_name_copy(cname, fname, NULL);
 
4760
                RUNTIME_CHECK(result == ISC_R_SUCCESS);
 
4761
        }
 
4762
        if (result != ISC_R_SUCCESS)
 
4763
                return (result);
 
4764
        query_keepname(client, fname, dbuf);
 
4765
        result = query_add_cname(client, client->query.qname,
 
4766
                                 fname, dns_trust_authanswer, st->m.ttl);
 
4767
        if (result != ISC_R_SUCCESS)
 
4768
                return (result);
 
4769
        rpz_log_rewrite(client, "", st->m.policy, st->m.type, st->qname);
 
4770
        ns_client_qnamereplace(client, fname);
 
4771
        /*
 
4772
         * Turn off DNSSEC because the results of a
 
4773
         * response policy zone cannot verify.
 
4774
         */
 
4775
        client->attributes &= ~(NS_CLIENTATTR_WANTDNSSEC |
 
4776
                                DNS_MESSAGEFLAG_AD);
 
4777
        return (ISC_R_SUCCESS);
 
4778
}
 
4779
 
4457
4780
#define MAX_RESTARTS 16
4458
4781
 
4459
4782
#define QUERY_ERROR(r) \
5027
5350
                        rpz_st->q.sigrdataset = NULL;
5028
5351
                        qtype = rpz_st->q.qtype;
5029
5352
 
 
5353
                        rpz_st->r.db = event->db;
5030
5354
                        if (event->node != NULL)
5031
 
                                dns_db_detachnode(db, &event->node);
5032
 
                        rpz_st->ns.db = event->db;
5033
 
                        rpz_st->ns.r_type = event->qtype;
5034
 
                        rpz_st->ns.r_rdataset = event->rdataset;
5035
 
                        if (event->sigrdataset != NULL &&
5036
 
                            dns_rdataset_isassociated(event->sigrdataset))
5037
 
                                dns_rdataset_disassociate(event->sigrdataset);
 
5355
                                dns_db_detachnode(event->db, &event->node);
 
5356
                        rpz_st->r.r_type = event->qtype;
 
5357
                        rpz_st->r.r_rdataset = event->rdataset;
 
5358
                        query_putrdataset(client, &event->sigrdataset);
5038
5359
                } else {
5039
5360
                        authoritative = ISC_FALSE;
5040
5361
 
5085
5406
                }
5086
5407
                if (rpz_st != NULL &&
5087
5408
                    (rpz_st->state & DNS_RPZ_RECURSING) != 0) {
5088
 
                        rpz_st->ns.r_result = event->result;
 
5409
                        rpz_st->r.r_result = event->result;
5089
5410
                        result = rpz_st->q.result;
5090
5411
                        isc_event_free(ISC_EVENT_PTR(&event));
5091
5412
                } else {
5247
5568
        CTRACE("query_find: resume");
5248
5569
 
5249
5570
        if (!ISC_LIST_EMPTY(client->view->rpz_zones) &&
5250
 
            RECURSIONOK(client) && !RECURSING(client) &&
5251
 
            result != DNS_R_DELEGATION && result != ISC_R_NOTFOUND &&
 
5571
            (RECURSIONOK(client) || !client->view->rpz_recursive_only) &&
 
5572
            rpz_ck_dnssec(client, result, rdataset, sigrdataset) &&
 
5573
            !RECURSING(client) &&
5252
5574
            (client->query.rpz_st == NULL ||
5253
5575
             (client->query.rpz_st->state & DNS_RPZ_REWRITTEN) == 0) &&
5254
5576
            !dns_name_equal(client->query.qname, dns_rootname)) {
5255
5577
                isc_result_t rresult;
5256
5578
 
5257
 
                rresult = rpz_rewrite(client, qtype, resuming);
 
5579
                rresult = rpz_rewrite(client, qtype, result, resuming);
5258
5580
                rpz_st = client->query.rpz_st;
5259
5581
                switch (rresult) {
5260
5582
                case ISC_R_SUCCESS:
5285
5607
                        RECURSE_ERROR(rresult);
5286
5608
                        goto cleanup;
5287
5609
                }
 
5610
                if (rpz_st->m.policy != DNS_RPZ_POLICY_MISS)
 
5611
                        rpz_st->state |= DNS_RPZ_REWRITTEN;
5288
5612
                if (rpz_st->m.policy != DNS_RPZ_POLICY_MISS &&
5289
 
                    rpz_st->m.policy != DNS_RPZ_POLICY_NO_OP) {
5290
 
                        result = dns_name_copy(client->query.qname, fname,
5291
 
                                               NULL);
5292
 
                        RUNTIME_CHECK(result == ISC_R_SUCCESS);
5293
 
 finish_rewrite:
 
5613
                    rpz_st->m.policy != DNS_RPZ_POLICY_PASSTHRU &&
 
5614
                    rpz_st->m.policy != DNS_RPZ_POLICY_ERROR) {
 
5615
                        if (rpz_st->m.type == DNS_RPZ_TYPE_QNAME) {
 
5616
                                result = dns_name_copy(client->query.qname,
 
5617
                                                       fname, NULL);
 
5618
                                RUNTIME_CHECK(result == ISC_R_SUCCESS);
 
5619
                        }
5294
5620
                        rpz_clean(&zone, &db, &node, NULL);
5295
5621
                        if (rpz_st->m.rdataset != NULL) {
5296
 
                                if (rdataset != NULL)
5297
 
                                        query_putrdataset(client, &rdataset);
 
5622
                                query_putrdataset(client, &rdataset);
5298
5623
                                rdataset = rpz_st->m.rdataset;
5299
5624
                                rpz_st->m.rdataset = NULL;
5300
5625
                        } else if (rdataset != NULL &&
5305
5630
                        rpz_st->m.node = NULL;
5306
5631
                        db = rpz_st->m.db;
5307
5632
                        rpz_st->m.db = NULL;
 
5633
                        version = rpz_st->m.version;
 
5634
                        rpz_st->m.version = NULL;
5308
5635
                        zone = rpz_st->m.zone;
5309
5636
                        rpz_st->m.zone = NULL;
5310
5637
 
5311
 
                        result = rpz_st->m.result;
5312
5638
                        switch (rpz_st->m.policy) {
5313
5639
                        case DNS_RPZ_POLICY_NXDOMAIN:
5314
5640
                                result = DNS_R_NXDOMAIN;
5317
5643
                                result = DNS_R_NXRRSET;
5318
5644
                                break;
5319
5645
                        case DNS_RPZ_POLICY_RECORD:
5320
 
                                if (type == dns_rdatatype_any &&
5321
 
                                    result != DNS_R_CNAME &&
5322
 
                                    dns_rdataset_isassociated(rdataset))
5323
 
                                        dns_rdataset_disassociate(rdataset);
 
5646
                                result = rpz_st->m.result;
 
5647
                                if (qtype == dns_rdatatype_any &&
 
5648
                                    result != DNS_R_CNAME) {
 
5649
                                        /*
 
5650
                                         * We will add all of the rdatasets of
 
5651
                                         * the node by iterating, setting the
 
5652
                                         * TTL then.
 
5653
                                         */
 
5654
                                        if (dns_rdataset_isassociated(rdataset))
 
5655
                                            dns_rdataset_disassociate(rdataset);
 
5656
                                } else {
 
5657
                                        /*
 
5658
                                         * We will add this rdataset.
 
5659
                                         */
 
5660
                                        rdataset->ttl = ISC_MIN(rdataset->ttl,
 
5661
                                                            rpz_st->m.ttl);
 
5662
                                }
5324
5663
                                break;
 
5664
                        case DNS_RPZ_POLICY_WILDCNAME:
 
5665
                                result = dns_rdataset_first(rdataset);
 
5666
                                RUNTIME_CHECK(result == ISC_R_SUCCESS);
 
5667
                                dns_rdataset_current(rdataset, &rdata);
 
5668
                                result = dns_rdata_tostruct(&rdata, &cname,
 
5669
                                                            NULL);
 
5670
                                RUNTIME_CHECK(result == ISC_R_SUCCESS);
 
5671
                                dns_rdata_reset(&rdata);
 
5672
                                result = rpz_add_cname(client, rpz_st,
 
5673
                                                       &cname.cname,
 
5674
                                                       fname, dbuf);
 
5675
                                if (result != ISC_R_SUCCESS)
 
5676
                                        goto cleanup;
 
5677
                                fname = NULL;
 
5678
                                want_restart = ISC_TRUE;
 
5679
                                goto cleanup;
5325
5680
                        case DNS_RPZ_POLICY_CNAME:
5326
 
                                result = dns_name_copy(&rpz_st->m.rpz->cname,
5327
 
                                                       fname, NULL);
5328
 
                                RUNTIME_CHECK(result == ISC_R_SUCCESS);
5329
 
                                query_keepname(client, fname, dbuf);
5330
 
                                result = query_add_cname(client,
5331
 
                                                        client->query.qname,
5332
 
                                                        fname,
5333
 
                                                        dns_trust_authanswer,
5334
 
                                                        rpz_st->m.ttl);
 
5681
                                /*
 
5682
                                 * Add overridding CNAME from a named.conf
 
5683
                                 * response-policy statement
 
5684
                                 */
 
5685
                                result = rpz_add_cname(client, rpz_st,
 
5686
                                                       &rpz_st->m.rpz->cname,
 
5687
                                                       fname, dbuf);
5335
5688
                                if (result != ISC_R_SUCCESS)
5336
5689
                                        goto cleanup;
5337
 
                                ns_client_qnamereplace(client, fname);
5338
5690
                                fname = NULL;
5339
 
                                client->attributes &= ~NS_CLIENTATTR_WANTDNSSEC;
5340
 
                                rpz_log(client);
5341
5691
                                want_restart = ISC_TRUE;
5342
5692
                                goto cleanup;
5343
5693
                        default:
5348
5698
                         * Turn off DNSSEC because the results of a
5349
5699
                         * response policy zone cannot verify.
5350
5700
                         */
5351
 
                        client->attributes &= ~NS_CLIENTATTR_WANTDNSSEC;
5352
 
                        if (sigrdataset != NULL &&
5353
 
                            dns_rdataset_isassociated(sigrdataset))
5354
 
                                dns_rdataset_disassociate(sigrdataset);
 
5701
                        client->attributes &= ~(NS_CLIENTATTR_WANTDNSSEC |
 
5702
                                                DNS_MESSAGEFLAG_AD);
 
5703
                        query_putrdataset(client, &sigrdataset);
5355
5704
                        is_zone = ISC_TRUE;
5356
 
                        rpz_log(client);
 
5705
                        rpz_log_rewrite(client, "", rpz_st->m.policy,
 
5706
                                        rpz_st->m.type, rpz_st->qname);
5357
5707
                }
5358
5708
        }
5359
5709
 
5668
6018
 
5669
6019
        case DNS_R_EMPTYNAME:
5670
6020
        case DNS_R_NXRRSET:
5671
 
        nxrrset:
 
6021
        iszone_nxrrset:
5672
6022
                INSIST(is_zone);
5673
6023
 
5674
6024
#ifdef dns64_bis_return_excluded_addresses
5686
6036
                                query_putrdataset(client, &sigrdataset);
5687
6037
                        rdataset = client->query.dns64_aaaa;
5688
6038
                        sigrdataset = client->query.dns64_sigaaaa;
 
6039
                        client->query.dns64_aaaa = NULL;
 
6040
                        client->query.dns64_sigaaaa = NULL;
5689
6041
                        if (fname == NULL) {
5690
6042
                                dbuf = query_getnamebuf(client);
5691
6043
                                if (dbuf == NULL) {
5699
6051
                                }
5700
6052
                        }
5701
6053
                        dns_name_copy(client->query.qname, fname, NULL);
5702
 
                        client->query.dns64_aaaa = NULL;
5703
 
                        client->query.dns64_sigaaaa = NULL;
5704
6054
                        dns64 = ISC_FALSE;
5705
6055
#ifdef dns64_bis_return_excluded_addresses
5706
6056
                        /*
5735
6085
                /*
5736
6086
                 * Look for a NSEC3 record if we don't have a NSEC record.
5737
6087
                 */
 
6088
 nxrrset_rrsig:
5738
6089
                if (!dns_rdataset_isassociated(rdataset) &&
5739
6090
                     WANTDNSSEC(client)) {
5740
6091
                        if ((fname->attributes & DNS_NAMEATTR_WILDCARD) == 0) {
5860
6211
                         */
5861
6212
                        query_releasename(client, &fname);
5862
6213
                }
 
6214
 
5863
6215
                /*
5864
6216
                 * Add SOA.  If the query was for a SOA record force the
5865
6217
                 * ttl to zero so that it is possible for clients to find
5936
6288
                                query_putrdataset(client, &sigrdataset);
5937
6289
                        rdataset = client->query.dns64_aaaa;
5938
6290
                        sigrdataset = client->query.dns64_sigaaaa;
 
6291
                        client->query.dns64_aaaa = NULL;
 
6292
                        client->query.dns64_sigaaaa = NULL;
5939
6293
                        if (fname == NULL) {
5940
6294
                                dbuf = query_getnamebuf(client);
5941
6295
                                if (dbuf == NULL) {
5949
6303
                                }
5950
6304
                        }
5951
6305
                        dns_name_copy(client->query.qname, fname, NULL);
5952
 
                        client->query.dns64_aaaa = NULL;
5953
 
                        client->query.dns64_sigaaaa = NULL;
5954
6306
                        dns64 = ISC_FALSE;
5955
6307
#ifdef dns64_bis_return_excluded_addresses
5956
6308
                        if (dns64_excluded)
6201
6553
                need_wildcardproof = ISC_TRUE;
6202
6554
        }
6203
6555
 
 
6556
#ifdef ALLOW_FILTER_AAAA_ON_V4
 
6557
        if (client->view->v4_aaaa != dns_v4_aaaa_ok &&
 
6558
            is_v4_client(client) &&
 
6559
            ns_client_checkaclsilent(client, NULL,
 
6560
                                     client->view->v4_aaaa_acl,
 
6561
                                     ISC_TRUE) == ISC_R_SUCCESS)
 
6562
                client->filter_aaaa = client->view->v4_aaaa;
 
6563
        else
 
6564
                client->filter_aaaa = dns_v4_aaaa_ok;
 
6565
 
 
6566
#endif
 
6567
 
6204
6568
        if (type == dns_rdatatype_any) {
6205
6569
#ifdef ALLOW_FILTER_AAAA_ON_V4
6206
 
                isc_boolean_t have_aaaa, have_a, have_sig, filter_aaaa;
 
6570
                isc_boolean_t have_aaaa, have_a, have_sig;
6207
6571
 
6208
6572
                /*
6209
6573
                 * The filter-aaaa-on-v4 option should
6215
6579
                have_aaaa = ISC_FALSE;
6216
6580
                have_a = !authoritative;
6217
6581
                have_sig = ISC_FALSE;
6218
 
                if (client->view->v4_aaaa != dns_v4_aaaa_ok &&
6219
 
                    is_v4_client(client) &&
6220
 
                    ns_client_checkaclsilent(client, NULL,
6221
 
                                             client->view->v4_aaaa_acl,
6222
 
                                             ISC_TRUE) == ISC_R_SUCCESS)
6223
 
                        filter_aaaa = ISC_TRUE;
6224
 
                else
6225
 
                        filter_aaaa = ISC_FALSE;
6226
6582
#endif
6227
6583
                /*
6228
6584
                 * XXXRTH  Need to handle zonecuts with special case
6237
6593
                }
6238
6594
 
6239
6595
                /*
6240
 
                 * Check all A and AAAA records in all response policy
6241
 
                 * IP address zones
6242
 
                 */
6243
 
                rpz_st = client->query.rpz_st;
6244
 
                if (rpz_st != NULL &&
6245
 
                    (rpz_st->state & DNS_RPZ_DONE_QNAME) != 0 &&
6246
 
                    (rpz_st->state & DNS_RPZ_REWRITTEN) == 0 &&
6247
 
                    RECURSIONOK(client) && !RECURSING(client) &&
6248
 
                    (rpz_st->state & DNS_RPZ_HAVE_IP) != 0) {
6249
 
                        for (result = dns_rdatasetiter_first(rdsiter);
6250
 
                             result == ISC_R_SUCCESS;
6251
 
                             result = dns_rdatasetiter_next(rdsiter)) {
6252
 
                                dns_rdatasetiter_current(rdsiter, rdataset);
6253
 
                                if (rdataset->type == dns_rdatatype_a ||
6254
 
                                    rdataset->type == dns_rdatatype_aaaa)
6255
 
                                        result = rpz_rewrite_ip(client,
6256
 
                                                              rdataset,
6257
 
                                                              DNS_RPZ_TYPE_IP);
6258
 
                                dns_rdataset_disassociate(rdataset);
6259
 
                                if (result != ISC_R_SUCCESS)
6260
 
                                        break;
6261
 
                        }
6262
 
                        if (result != ISC_R_NOMORE) {
6263
 
                                dns_rdatasetiter_destroy(&rdsiter);
6264
 
                                QUERY_ERROR(DNS_R_SERVFAIL);
6265
 
                                goto cleanup;
6266
 
                        }
6267
 
                        switch (rpz_st->m.policy) {
6268
 
                        case DNS_RPZ_POLICY_MISS:
6269
 
                                break;
6270
 
                        case DNS_RPZ_POLICY_NO_OP:
6271
 
                                rpz_log(client);
6272
 
                                rpz_st->state |= DNS_RPZ_REWRITTEN;
6273
 
                                break;
6274
 
                        case DNS_RPZ_POLICY_NXDOMAIN:
6275
 
                        case DNS_RPZ_POLICY_NODATA:
6276
 
                        case DNS_RPZ_POLICY_RECORD:
6277
 
                        case DNS_RPZ_POLICY_CNAME:
6278
 
                                dns_rdatasetiter_destroy(&rdsiter);
6279
 
                                rpz_st->state |= DNS_RPZ_REWRITTEN;
6280
 
                                goto finish_rewrite;
6281
 
                        default:
6282
 
                                INSIST(0);
6283
 
                        }
6284
 
                }
6285
 
 
6286
 
                /*
6287
6596
                 * Calling query_addrrset() with a non-NULL dbuf is going
6288
6597
                 * to either keep or release the name.  We don't want it to
6289
6598
                 * release fname, since we may have to call query_addrrset()
6304
6613
                         * Notice the presence of A and AAAAs so
6305
6614
                         * that AAAAs can be hidden from IPv4 clients.
6306
6615
                         */
6307
 
                        if (filter_aaaa) {
 
6616
                        if (client->filter_aaaa != dns_v4_aaaa_ok) {
6308
6617
                                if (rdataset->type == dns_rdatatype_aaaa)
6309
6618
                                        have_aaaa = ISC_TRUE;
6310
6619
                                else if (rdataset->type == dns_rdatatype_a)
6330
6639
                                        noqname = rdataset;
6331
6640
                                else
6332
6641
                                        noqname = NULL;
 
6642
                                rpz_st = client->query.rpz_st;
 
6643
                                if (rpz_st != NULL)
 
6644
                                        rdataset->ttl = ISC_MIN(rdataset->ttl,
 
6645
                                                            rpz_st->m.ttl);
6333
6646
                                query_addrrset(client,
6334
6647
                                               fname != NULL ? &fname : &tname,
6335
6648
                                               &rdataset, NULL,
6361
6674
                 * Filter AAAAs if there is an A and there is no signature
6362
6675
                 * or we are supposed to break DNSSEC.
6363
6676
                 */
6364
 
                if (filter_aaaa && have_aaaa && have_a &&
6365
 
                    (!have_sig || !WANTDNSSEC(client) ||
6366
 
                     client->view->v4_aaaa == dns_v4_aaaa_break_dnssec))
 
6677
                if (client->filter_aaaa == dns_v4_aaaa_break_dnssec)
6367
6678
                        client->attributes |= NS_CLIENTATTR_FILTER_AAAA;
 
6679
                else if (client->filter_aaaa != dns_v4_aaaa_ok &&
 
6680
                         have_aaaa && have_a &&
 
6681
                         (!have_sig || !WANTDNSSEC(client)))
 
6682
                          client->attributes |= NS_CLIENTATTR_FILTER_AAAA;
6368
6683
#endif
6369
6684
                if (fname != NULL)
6370
6685
                        dns_message_puttempname(client->message, &fname);
6371
6686
 
6372
 
                if (n == 0 && is_zone) {
 
6687
                if (n == 0) {
6373
6688
                        /*
6374
 
                         * We didn't match any rdatasets.
 
6689
                         * No matching rdatasets found in cache. If we were
 
6690
                         * searching for RRSIG/SIG, that's probably okay;
 
6691
                         * otherwise this is an error condition.
6375
6692
                         */
6376
6693
                        if ((qtype == dns_rdatatype_rrsig ||
6377
6694
                             qtype == dns_rdatatype_sig) &&
6378
6695
                            result == ISC_R_NOMORE) {
6379
 
                                /*
6380
 
                                 * XXXRTH  If this is a secure zone and we
6381
 
                                 * didn't find any SIGs, we should generate
6382
 
                                 * an error unless we were searching for
6383
 
                                 * glue.  Ugh.
6384
 
                                 */
6385
6696
                                if (!is_zone) {
6386
 
                                        /*
6387
 
                                         * Note: this is dead code because
6388
 
                                         * is_zone is always true due to the
6389
 
                                         * condition above.  But naive
6390
 
                                         * recursion would cause infinite
6391
 
                                         * attempts of recursion because
6392
 
                                         * the answer to (RR)SIG queries
6393
 
                                         * won't be cached.  Until we figure
6394
 
                                         * out what we should do and implement
6395
 
                                         * it we intentionally keep this code
6396
 
                                         * dead.
6397
 
                                         */
6398
6697
                                        authoritative = ISC_FALSE;
6399
6698
                                        dns_rdatasetiter_destroy(&rdsiter);
6400
 
                                        if (RECURSIONOK(client)) {
6401
 
                                                result = query_recurse(client,
6402
 
                                                            qtype,
6403
 
                                                            client->query.qname,
6404
 
                                                            NULL, NULL,
6405
 
                                                            resuming);
6406
 
                                                if (result == ISC_R_SUCCESS)
6407
 
                                                    client->query.attributes |=
6408
 
                                                        NS_QUERYATTR_RECURSING;
6409
 
                                                else
6410
 
                                                    RECURSE_ERROR(result);
6411
 
                                        }
 
6699
                                        client->attributes &= ~NS_CLIENTATTR_RA;
6412
6700
                                        goto addauth;
6413
6701
                                }
6414
 
                                /*
6415
 
                                 * We were searching for SIG records in
6416
 
                                 * a nonsecure zone.  Send a "no error,
6417
 
                                 * no data" response.
6418
 
                                 */
6419
 
                                /*
6420
 
                                 * Add SOA.
6421
 
                                 */
6422
 
                                result = query_addsoa(client, db, version,
6423
 
                                                      ISC_UINT32_MAX,
6424
 
                                                      ISC_FALSE);
6425
 
                                if (result == ISC_R_SUCCESS)
6426
 
                                        result = ISC_R_NOMORE;
6427
 
                        } else {
6428
 
                                /*
6429
 
                                 * Something went wrong.
6430
 
                                 */
 
6702
 
 
6703
                                if (dns_db_issecure(db)) {
 
6704
                                        char namebuf[DNS_NAME_FORMATSIZE];
 
6705
                                        dns_name_format(client->query.qname,
 
6706
                                                        namebuf,
 
6707
                                                        sizeof(namebuf));
 
6708
                                        ns_client_log(client,
 
6709
                                                      DNS_LOGCATEGORY_DNSSEC,
 
6710
                                                      NS_LOGMODULE_QUERY,
 
6711
                                                      ISC_LOG_WARNING,
 
6712
                                                      "missing signature "
 
6713
                                                      "for %s", namebuf);
 
6714
                                }
 
6715
 
 
6716
                                dns_rdatasetiter_destroy(&rdsiter);
 
6717
                                fname = query_newname(client, dbuf, &b);
 
6718
                                goto nxrrset_rrsig;
 
6719
                        } else
6431
6720
                                result = DNS_R_SERVFAIL;
6432
 
                        }
6433
6721
                }
 
6722
 
6434
6723
                dns_rdatasetiter_destroy(&rdsiter);
6435
6724
                if (result != ISC_R_NOMORE) {
6436
6725
                        QUERY_ERROR(DNS_R_SERVFAIL);
6442
6731
                 * we know the answer.
6443
6732
                 */
6444
6733
 
6445
 
                /*
6446
 
                 * Check all A and AAAA records in all response policy
6447
 
                 * IP address zones
6448
 
                 */
6449
 
                rpz_st = client->query.rpz_st;
6450
 
                if (rpz_st != NULL &&
6451
 
                    (rpz_st->state & DNS_RPZ_DONE_QNAME) != 0 &&
6452
 
                    (rpz_st->state & DNS_RPZ_REWRITTEN) == 0 &&
6453
 
                    RECURSIONOK(client) && !RECURSING(client) &&
6454
 
                    (rpz_st->state & DNS_RPZ_HAVE_IP) != 0 &&
6455
 
                    (qtype == dns_rdatatype_aaaa || qtype == dns_rdatatype_a)) {
6456
 
                        result = rpz_rewrite_ip(client, rdataset,
6457
 
                                                DNS_RPZ_TYPE_IP);
6458
 
                        if (result != ISC_R_SUCCESS) {
6459
 
                                QUERY_ERROR(DNS_R_SERVFAIL);
6460
 
                                goto cleanup;
6461
 
                        }
6462
 
                        /*
6463
 
                         * After a hit in the radix tree for the policy domain,
6464
 
                         * either stop trying to rewrite (DNS_RPZ_POLICY_NO_OP)
6465
 
                         * or restart to ask the ordinary database of the
6466
 
                         * policy zone for the DNS record corresponding to the
6467
 
                         * record in the radix tree.
6468
 
                         */
6469
 
                        switch (rpz_st->m.policy) {
6470
 
                        case DNS_RPZ_POLICY_MISS:
6471
 
                                break;
6472
 
                        case DNS_RPZ_POLICY_NO_OP:
6473
 
                                rpz_log(client);
6474
 
                                rpz_st->state |= DNS_RPZ_REWRITTEN;
6475
 
                                break;
6476
 
                        case DNS_RPZ_POLICY_NXDOMAIN:
6477
 
                        case DNS_RPZ_POLICY_NODATA:
6478
 
                        case DNS_RPZ_POLICY_RECORD:
6479
 
                        case DNS_RPZ_POLICY_CNAME:
6480
 
                                rpz_st->state |= DNS_RPZ_REWRITTEN;
6481
 
                                goto finish_rewrite;
6482
 
                        default:
6483
 
                                INSIST(0);
6484
 
                        }
6485
 
                }
6486
 
 
6487
6734
#ifdef ALLOW_FILTER_AAAA_ON_V4
6488
6735
                /*
6489
6736
                 * Optionally hide AAAAs from IPv4 clients if there is an A.
6493
6740
                 * so fundamentally wrong, unavoidably inaccurate, and
6494
6741
                 * unneeded that it is best to keep it as short as possible.
6495
6742
                 */
6496
 
                if (client->view->v4_aaaa != dns_v4_aaaa_ok &&
6497
 
                    is_v4_client(client) &&
6498
 
                    ns_client_checkaclsilent(client, NULL,
6499
 
                                             client->view->v4_aaaa_acl,
6500
 
                                             ISC_TRUE) == ISC_R_SUCCESS &&
6501
 
                    (!WANTDNSSEC(client) ||
6502
 
                     sigrdataset == NULL ||
6503
 
                     !dns_rdataset_isassociated(sigrdataset) ||
6504
 
                     client->view->v4_aaaa == dns_v4_aaaa_break_dnssec)) {
 
6743
                if (client->filter_aaaa == dns_v4_aaaa_break_dnssec ||
 
6744
                    (client->filter_aaaa == dns_v4_aaaa_filter &&
 
6745
                     (!WANTDNSSEC(client) || sigrdataset == NULL ||
 
6746
                     !dns_rdataset_isassociated(sigrdataset))))
 
6747
                {
6505
6748
                        if (qtype == dns_rdatatype_aaaa) {
6506
6749
                                trdataset = query_newrdataset(client);
6507
6750
                                result = dns_db_findrdataset(db, node, version,
6633
6876
                                }
6634
6877
#endif
6635
6878
                                if (is_zone)
6636
 
                                        goto nxrrset;
 
6879
                                        goto iszone_nxrrset;
6637
6880
                                else
6638
6881
                                        goto ncache_nxrrset;
6639
6882
                        } else if (result != ISC_R_SUCCESS) {
6691
6934
         * General cleanup.
6692
6935
         */
6693
6936
        rpz_st = client->query.rpz_st;
6694
 
        if (rpz_st != NULL && (rpz_st->state & DNS_RPZ_RECURSING) == 0)
6695
 
                rpz_clean(&rpz_st->m.zone, &rpz_st->m.db, &rpz_st->m.node,
6696
 
                          &rpz_st->m.rdataset);
 
6937
        if (rpz_st != NULL && (rpz_st->state & DNS_RPZ_RECURSING) == 0) {
 
6938
                rpz_match_clear(rpz_st);
 
6939
                rpz_st->state &= ~DNS_RPZ_DONE_QNAME;
 
6940
        }
6697
6941
        if (rdataset != NULL)
6698
6942
                query_putrdataset(client, &rdataset);
6699
6943
        if (sigrdataset != NULL)