2017-09-11 11:53:42 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2017-09-11 11:53:42 -07:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2017-09-11 17:47:12 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2017-09-11 11:53:42 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2017-09-11 11:53:42 -07:00
|
|
|
#ifdef USE_DNSRPS
|
|
|
|
|
2019-06-26 08:37:31 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-09-11 11:53:42 -07:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/string.h>
|
2017-11-18 11:22:44 +11:00
|
|
|
#include <isc/util.h>
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
#include <dns/db.h>
|
|
|
|
#define LIBRPZ_LIB_OPEN DNSRPS_LIB_OPEN
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
|
|
|
|
2017-09-11 11:53:42 -07:00
|
|
|
#include <dns/dnsrps.h>
|
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/rdatasetiter.h>
|
|
|
|
#include <dns/rpz.h>
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
librpz_t *librpz;
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t librpz_lib_open_emsg;
|
2020-02-13 14:44:37 -08:00
|
|
|
static void *librpz_handle;
|
2017-09-11 11:53:42 -07:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define RPSDB_MAGIC ISC_MAGIC('R', 'P', 'Z', 'F')
|
2017-09-11 11:53:42 -07:00
|
|
|
#define VALID_RPSDB(rpsdb) ((rpsdb)->common.impmagic == RPSDB_MAGIC)
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define RD_DB(r) ((r)->private1)
|
|
|
|
#define RD_CUR_RR(r) ((r)->private2)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define RD_NEXT_RR(r) ((r)->resign)
|
2020-02-13 14:44:37 -08:00
|
|
|
#define RD_COUNT(r) ((r)->privateuint4)
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
typedef struct {
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_rdatasetiter_t common;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdatatype_t type;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_rdataclass_t class;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t ttl;
|
|
|
|
uint count;
|
2020-02-12 13:59:18 +01:00
|
|
|
librpz_idx_t next_rr;
|
2017-09-11 11:53:42 -07:00
|
|
|
} rpsdb_rdatasetiter_t;
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
static dns_dbmethods_t rpsdb_db_methods;
|
|
|
|
static dns_rdatasetmethods_t rpsdb_rdataset_methods;
|
2017-09-11 11:53:42 -07:00
|
|
|
static dns_rdatasetitermethods_t rpsdb_rdatasetiter_methods;
|
|
|
|
|
|
|
|
static librpz_clist_t *clist;
|
|
|
|
|
|
|
|
static isc_mutex_t dnsrps_mutex;
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dnsrps_lock(void *mutex0) {
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_mutex_t *mutex = mutex0;
|
|
|
|
|
|
|
|
LOCK(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dnsrps_unlock(void *mutex0) {
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_mutex_t *mutex = mutex0;
|
|
|
|
|
|
|
|
UNLOCK(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dnsrps_mutex_destroy(void *mutex0) {
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_mutex_t *mutex = mutex0;
|
|
|
|
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(mutex);
|
2017-09-11 11:53:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dnsrps_log_fnc(librpz_log_level_t level, void *ctxt, const char *buf) {
|
2017-09-11 11:53:42 -07:00
|
|
|
int isc_level;
|
|
|
|
|
|
|
|
UNUSED(ctxt);
|
|
|
|
|
|
|
|
/* Setting librpz_log_level in the configuration overrides the
|
|
|
|
* BIND9 logging levels. */
|
|
|
|
if (level > LIBRPZ_LOG_TRACE1 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
level <= librpz->log_level_val(LIBRPZ_LOG_INVALID))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
level = LIBRPZ_LOG_TRACE1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
switch (level) {
|
2017-09-11 11:53:42 -07:00
|
|
|
case LIBRPZ_LOG_FATAL:
|
2020-02-12 13:59:18 +01:00
|
|
|
case LIBRPZ_LOG_ERROR: /* errors */
|
2017-09-11 11:53:42 -07:00
|
|
|
default:
|
|
|
|
isc_level = DNS_RPZ_ERROR_LEVEL;
|
|
|
|
break;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
case LIBRPZ_LOG_TRACE1: /* big events such as dnsrpzd starts */
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_level = DNS_RPZ_INFO_LEVEL;
|
|
|
|
break;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
case LIBRPZ_LOG_TRACE2: /* smaller dnsrpzd zone transfers */
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_level = DNS_RPZ_DEBUG_LEVEL1;
|
|
|
|
break;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
case LIBRPZ_LOG_TRACE3: /* librpz hits */
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_level = DNS_RPZ_DEBUG_LEVEL2;
|
|
|
|
break;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
case LIBRPZ_LOG_TRACE4: /* librpz lookups */
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_level = DNS_RPZ_DEBUG_LEVEL3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ, DNS_LOGMODULE_RBTDB,
|
|
|
|
isc_level, "dnsrps: %s", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start dnsrps for the entire server.
|
|
|
|
* This is not thread safe, but it is called by a single thread.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dnsrps_server_create(void) {
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t emsg;
|
|
|
|
|
|
|
|
INSIST(clist == NULL);
|
|
|
|
INSIST(librpz == NULL);
|
|
|
|
INSIST(librpz_handle == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notice if librpz is available.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
librpz = librpz_lib_open(&librpz_lib_open_emsg, &librpz_handle,
|
|
|
|
DNSRPS_LIBRPZ_PATH);
|
2017-09-11 11:53:42 -07:00
|
|
|
/*
|
|
|
|
* Stop now without complaining if librpz is not available.
|
|
|
|
* Complain later if and when librpz is needed for a view with
|
2020-02-20 14:49:36 -08:00
|
|
|
* "dnsrps-enable yes" (including the default view).
|
2017-09-11 11:53:42 -07:00
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (librpz == NULL) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&dnsrps_mutex);
|
2017-09-11 11:53:42 -07:00
|
|
|
|
2019-09-09 14:05:31 +02:00
|
|
|
librpz->set_log(dnsrps_log_fnc, NULL);
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
clist = librpz->clist_create(&emsg, dnsrps_lock, dnsrps_unlock,
|
|
|
|
dnsrps_mutex_destroy, &dnsrps_mutex,
|
|
|
|
dns_lctx);
|
|
|
|
if (clist == NULL) {
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
|
|
|
|
DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
|
|
|
|
"dnsrps: %s", emsg.c);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop dnsrps for the entire server.
|
|
|
|
* This is not thread safe.
|
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dnsrps_server_destroy(void) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (clist != NULL) {
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz->clist_detach(&clist);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
#ifdef LIBRPZ_USE_DLOPEN
|
|
|
|
if (librpz != NULL) {
|
|
|
|
INSIST(librpz_handle != NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dlclose(librpz_handle) != 0) {
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
|
|
|
|
DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
|
|
|
|
"dnsrps: dlclose(): %s", dlerror());
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_handle = NULL;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef LIBRPZ_USE_DLOPEN */
|
2017-09-11 11:53:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ready dnsrps for a view.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dnsrps_view_init(dns_rpz_zones_t *new, char *rps_cstr) {
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t emsg;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ, DNS_LOGMODULE_RBTDB,
|
|
|
|
DNS_RPZ_DEBUG_LEVEL3, "dnsrps configuration \"%s\"",
|
|
|
|
rps_cstr);
|
2017-09-11 11:53:42 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
new->rps_client = librpz->client_create(&emsg, clist, rps_cstr, false);
|
2017-09-11 11:53:42 -07:00
|
|
|
if (new->rps_client == NULL) {
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
|
|
|
|
DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
|
|
|
|
"librpz->client_create(): %s", emsg.c);
|
2018-04-17 08:29:14 -07:00
|
|
|
new->p.dnsrps_enabled = false;
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
new->p.dnsrps_enabled = true;
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to and start the dnsrps daemon, dnsrpzd.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dnsrps_connect(dns_rpz_zones_t *rpzs) {
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t emsg;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (rpzs == NULL || !rpzs->p.dnsrps_enabled) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fail only if we failed to link to librpz.
|
|
|
|
*/
|
|
|
|
if (librpz == NULL) {
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
|
|
|
|
DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
|
|
|
|
"librpz->connect(): %s", librpz_lib_open_emsg.c);
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!librpz->connect(&emsg, rpzs->rps_client, true)) {
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
|
|
|
|
DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
|
|
|
|
"librpz->connect(): %s", emsg.c);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ, DNS_LOGMODULE_RBTDB,
|
|
|
|
DNS_RPZ_INFO_LEVEL, "dnsrps: librpz version %s",
|
|
|
|
librpz->version);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get ready to try RPZ rewriting.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
|
|
|
dns_dnsrps_rewrite_init(librpz_emsg_t *emsg, dns_rpz_st_t *st,
|
|
|
|
dns_rpz_zones_t *rpzs, const dns_name_t *qname,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx, bool have_rd) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb;
|
|
|
|
|
|
|
|
rpsdb = isc_mem_get(mctx, sizeof(*rpsdb));
|
|
|
|
memset(rpsdb, 0, sizeof(*rpsdb));
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (!librpz->rsp_create(emsg, &rpsdb->rsp, NULL, rpzs->rps_client,
|
2020-02-13 14:44:37 -08:00
|
|
|
have_rd, false))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
isc_mem_put(mctx, rpsdb, sizeof(*rpsdb));
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
if (rpsdb->rsp == NULL) {
|
|
|
|
isc_mem_put(mctx, rpsdb, sizeof(*rpsdb));
|
|
|
|
return (DNS_R_DISALLOWED);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpsdb->common.magic = DNS_DB_MAGIC;
|
|
|
|
rpsdb->common.impmagic = RPSDB_MAGIC;
|
|
|
|
rpsdb->common.methods = &rpsdb_db_methods;
|
|
|
|
rpsdb->common.rdclass = dns_rdataclass_in;
|
|
|
|
dns_name_init(&rpsdb->common.origin, NULL);
|
|
|
|
isc_mem_attach(mctx, &rpsdb->common.mctx);
|
|
|
|
|
|
|
|
rpsdb->ref_cnt = 1;
|
|
|
|
rpsdb->qname = qname;
|
|
|
|
|
|
|
|
st->rpsdb = &rpsdb->common;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a dnsrps policy to a classic BIND9 RPZ policy.
|
|
|
|
*/
|
|
|
|
dns_rpz_policy_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dnsrps_2policy(librpz_policy_t rps_policy) {
|
2017-09-11 11:53:42 -07:00
|
|
|
switch (rps_policy) {
|
|
|
|
case LIBRPZ_POLICY_UNDEFINED:
|
|
|
|
return (DNS_RPZ_POLICY_MISS);
|
|
|
|
case LIBRPZ_POLICY_PASSTHRU:
|
|
|
|
return (DNS_RPZ_POLICY_PASSTHRU);
|
|
|
|
case LIBRPZ_POLICY_DROP:
|
|
|
|
return (DNS_RPZ_POLICY_DROP);
|
|
|
|
case LIBRPZ_POLICY_TCP_ONLY:
|
|
|
|
return (DNS_RPZ_POLICY_TCP_ONLY);
|
|
|
|
case LIBRPZ_POLICY_NXDOMAIN:
|
|
|
|
return (DNS_RPZ_POLICY_NXDOMAIN);
|
|
|
|
case LIBRPZ_POLICY_NODATA:
|
|
|
|
return (DNS_RPZ_POLICY_NODATA);
|
|
|
|
case LIBRPZ_POLICY_RECORD:
|
|
|
|
case LIBRPZ_POLICY_CNAME:
|
|
|
|
return (DNS_RPZ_POLICY_RECORD);
|
|
|
|
|
|
|
|
case LIBRPZ_POLICY_DELETED:
|
|
|
|
case LIBRPZ_POLICY_GIVEN:
|
|
|
|
case LIBRPZ_POLICY_DISABLED:
|
|
|
|
default:
|
2021-10-11 12:50:17 +02:00
|
|
|
UNREACHABLE();
|
2017-09-11 11:53:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a dnsrps trigger to a classic BIND9 RPZ rewrite or trigger type.
|
|
|
|
*/
|
|
|
|
dns_rpz_type_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dnsrps_trig2type(librpz_trig_t trig) {
|
2017-09-11 11:53:42 -07:00
|
|
|
switch (trig) {
|
|
|
|
case LIBRPZ_TRIG_BAD:
|
|
|
|
default:
|
|
|
|
return (DNS_RPZ_TYPE_BAD);
|
|
|
|
case LIBRPZ_TRIG_CLIENT_IP:
|
|
|
|
return (DNS_RPZ_TYPE_CLIENT_IP);
|
|
|
|
case LIBRPZ_TRIG_QNAME:
|
|
|
|
return (DNS_RPZ_TYPE_QNAME);
|
|
|
|
case LIBRPZ_TRIG_IP:
|
|
|
|
return (DNS_RPZ_TYPE_IP);
|
|
|
|
case LIBRPZ_TRIG_NSDNAME:
|
|
|
|
return (DNS_RPZ_TYPE_NSDNAME);
|
|
|
|
case LIBRPZ_TRIG_NSIP:
|
|
|
|
return (DNS_RPZ_TYPE_NSIP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a classic BIND9 RPZ rewrite or trigger type to a librpz trigger type.
|
|
|
|
*/
|
|
|
|
librpz_trig_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dnsrps_type2trig(dns_rpz_type_t type) {
|
2017-09-11 11:53:42 -07:00
|
|
|
switch (type) {
|
|
|
|
case DNS_RPZ_TYPE_BAD:
|
|
|
|
default:
|
|
|
|
return (LIBRPZ_TRIG_BAD);
|
|
|
|
case DNS_RPZ_TYPE_CLIENT_IP:
|
|
|
|
return (LIBRPZ_TRIG_CLIENT_IP);
|
|
|
|
case DNS_RPZ_TYPE_QNAME:
|
|
|
|
return (LIBRPZ_TRIG_QNAME);
|
|
|
|
case DNS_RPZ_TYPE_IP:
|
|
|
|
return (LIBRPZ_TRIG_IP);
|
|
|
|
case DNS_RPZ_TYPE_NSDNAME:
|
|
|
|
return (LIBRPZ_TRIG_NSDNAME);
|
|
|
|
case DNS_RPZ_TYPE_NSIP:
|
|
|
|
return (LIBRPZ_TRIG_NSIP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_attach(dns_db_t *source, dns_db_t **targetp) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)source;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use a simple count because only one thread uses any single rpsdb_t
|
|
|
|
*/
|
|
|
|
++rpsdb->ref_cnt;
|
|
|
|
*targetp = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_detach(dns_db_t **dbp) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)*dbp;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
REQUIRE(rpsdb->ref_cnt > 0);
|
|
|
|
|
|
|
|
*dbp = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple count because only one thread uses a rpsdb_t.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (--rpsdb->ref_cnt != 0) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
librpz->rsp_detach(&rpsdb->rsp);
|
|
|
|
rpsdb->common.impmagic = 0;
|
|
|
|
isc_mem_putanddetach(&rpsdb->common.mctx, rpsdb, sizeof(*rpsdb));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)db;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
2020-02-12 13:59:18 +01:00
|
|
|
REQUIRE(source == &rpsdb->origin_node || source == &rpsdb->data_node);
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple count because only one thread uses a rpsdb_t.
|
|
|
|
*/
|
|
|
|
++rpsdb->ref_cnt;
|
|
|
|
*targetp = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_detachnode(dns_db_t *db, dns_dbnode_t **targetp) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)db;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
REQUIRE(*targetp == &rpsdb->origin_node ||
|
|
|
|
*targetp == &rpsdb->data_node);
|
|
|
|
|
|
|
|
*targetp = NULL;
|
|
|
|
rpsdb_detach(&db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2018-04-17 08:29:14 -07:00
|
|
|
rpsdb_findnode(dns_db_t *db, const dns_name_t *name, bool create,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_dbnode_t **nodep) {
|
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)db;
|
2017-09-11 11:53:42 -07:00
|
|
|
dns_db_t *dbp;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
REQUIRE(nodep != NULL && *nodep == NULL);
|
|
|
|
REQUIRE(!create);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A fake/shim rpsdb has two nodes.
|
|
|
|
* One is the origin to support query_addsoa() in bin/named/query.c.
|
|
|
|
* The other contains rewritten RRs.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dns_name_equal(name, &db->origin)) {
|
2017-09-11 11:53:42 -07:00
|
|
|
*nodep = &rpsdb->origin_node;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2017-09-11 11:53:42 -07:00
|
|
|
*nodep = &rpsdb->data_node;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
dbp = NULL;
|
|
|
|
rpsdb_attach(db, &dbp);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpsdb_bind_rdataset(dns_rdataset_t *rdataset, uint count, librpz_idx_t next_rr,
|
|
|
|
dns_rdatatype_t type, uint16_t class, uint32_t ttl,
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_t *rpsdb) {
|
2017-09-11 11:53:42 -07:00
|
|
|
dns_db_t *dbp;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
INSIST(rdataset->methods == NULL); /* We must be disassociated. */
|
2017-09-11 11:53:42 -07:00
|
|
|
REQUIRE(type != dns_rdatatype_none);
|
|
|
|
|
|
|
|
rdataset->methods = &rpsdb_rdataset_methods;
|
|
|
|
rdataset->rdclass = class;
|
|
|
|
rdataset->type = type;
|
|
|
|
rdataset->ttl = ttl;
|
|
|
|
dbp = NULL;
|
|
|
|
dns_db_attach(&rpsdb->common, &dbp);
|
|
|
|
RD_DB(rdataset) = dbp;
|
|
|
|
RD_COUNT(rdataset) = count;
|
|
|
|
RD_NEXT_RR(rdataset) = next_rr;
|
|
|
|
RD_CUR_RR(rdataset) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_bind_soa(dns_rdataset_t *rdataset, rpsdb_t *rpsdb) {
|
|
|
|
uint32_t ttl;
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t emsg;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (!librpz->rsp_soa(&emsg, &ttl, NULL, NULL, &rpsdb->result,
|
|
|
|
rpsdb->rsp)) {
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
rpsdb_bind_rdataset(rdataset, 1, LIBRPZ_IDX_BAD, dns_rdatatype_soa,
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_rdataclass_in, ttl, rpsdb);
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forge an rdataset of the desired type from a librpz result.
|
|
|
|
* This is written for simplicity instead of speed, because RPZ rewriting
|
|
|
|
* should be rare compared to normal BIND operations.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
rpsdb_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
|
|
|
|
dns_rdatatype_t type, dns_rdatatype_t covers,
|
|
|
|
isc_stdtime_t now, dns_rdataset_t *rdataset,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdataset_t *sigrdataset) {
|
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)db;
|
2017-09-11 11:53:42 -07:00
|
|
|
dns_rdatatype_t foundtype;
|
|
|
|
dns_rdataclass_t class;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t ttl;
|
|
|
|
uint count;
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t emsg;
|
|
|
|
|
|
|
|
UNUSED(version);
|
|
|
|
UNUSED(covers);
|
|
|
|
UNUSED(now);
|
|
|
|
UNUSED(sigrdataset);
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
|
|
|
|
if (node == &rpsdb->origin_node) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (type == dns_rdatatype_any) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (type == dns_rdatatype_soa) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (rpsdb_bind_soa(rdataset, rpsdb));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
return (DNS_R_NXRRSET);
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE(node == &rpsdb->data_node);
|
|
|
|
|
|
|
|
switch (rpsdb->result.policy) {
|
|
|
|
case LIBRPZ_POLICY_UNDEFINED:
|
|
|
|
case LIBRPZ_POLICY_DELETED:
|
|
|
|
case LIBRPZ_POLICY_PASSTHRU:
|
|
|
|
case LIBRPZ_POLICY_DROP:
|
|
|
|
case LIBRPZ_POLICY_TCP_ONLY:
|
|
|
|
case LIBRPZ_POLICY_GIVEN:
|
|
|
|
case LIBRPZ_POLICY_DISABLED:
|
|
|
|
default:
|
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL,
|
|
|
|
"impossible dnsrps policy %d at %s:%d",
|
|
|
|
rpsdb->result.policy, __FILE__, __LINE__);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
|
|
|
|
case LIBRPZ_POLICY_NXDOMAIN:
|
|
|
|
return (DNS_R_NXDOMAIN);
|
|
|
|
|
|
|
|
case LIBRPZ_POLICY_NODATA:
|
|
|
|
return (DNS_R_NXRRSET);
|
|
|
|
|
|
|
|
case LIBRPZ_POLICY_RECORD:
|
|
|
|
case LIBRPZ_POLICY_CNAME:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (type == dns_rdatatype_soa) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (rpsdb_bind_soa(rdataset, rpsdb));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There is little to do for an ANY query.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (type == dns_rdatatype_any) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset to the start of the RRs.
|
|
|
|
* This function is only used after a policy has been chosen,
|
|
|
|
* and so without caring whether it is after recursion.
|
|
|
|
*/
|
|
|
|
if (!librpz->rsp_result(&emsg, &rpsdb->result, true, rpsdb->rsp)) {
|
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
if (!librpz->rsp_rr(&emsg, &foundtype, &class, &ttl, NULL,
|
|
|
|
&rpsdb->result, rpsdb->qname->ndata,
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb->qname->length, rpsdb->rsp))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
REQUIRE(foundtype != dns_rdatatype_none);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ho many of the target RR type are available?
|
|
|
|
*/
|
|
|
|
count = 0;
|
|
|
|
do {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (type == foundtype || type == dns_rdatatype_any) {
|
2017-09-11 11:53:42 -07:00
|
|
|
++count;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
if (!librpz->rsp_rr(&emsg, &foundtype, NULL, NULL, NULL,
|
|
|
|
&rpsdb->result, rpsdb->qname->ndata,
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb->qname->length, rpsdb->rsp))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
} while (foundtype != dns_rdatatype_none);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (count == 0) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (DNS_R_NXRRSET);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
rpsdb_bind_rdataset(rdataset, count, rpsdb->result.next_rr, type, class,
|
|
|
|
ttl, rpsdb);
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
rpsdb_finddb(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
|
|
|
|
dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
|
|
|
|
dns_dbnode_t **nodep, dns_name_t *foundname,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset) {
|
2017-09-11 11:53:42 -07:00
|
|
|
dns_dbnode_t *node;
|
|
|
|
|
|
|
|
UNUSED(version);
|
|
|
|
UNUSED(options);
|
|
|
|
UNUSED(now);
|
|
|
|
UNUSED(sigrdataset);
|
|
|
|
|
|
|
|
if (nodep == NULL) {
|
|
|
|
node = NULL;
|
|
|
|
nodep = &node;
|
|
|
|
}
|
|
|
|
rpsdb_findnode(db, name, false, nodep);
|
2021-05-21 17:20:44 -07:00
|
|
|
dns_name_copy(name, foundname);
|
2020-02-12 13:59:18 +01:00
|
|
|
return (rpsdb_findrdataset(db, *nodep, NULL, type, 0, 0, rdataset,
|
|
|
|
sigrdataset));
|
2017-09-11 11:53:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
rpsdb_allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stdtime_t now, dns_rdatasetiter_t **iteratorp) {
|
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)db;
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_rdatasetiter_t *rpsdb_iter;
|
|
|
|
|
|
|
|
UNUSED(version);
|
|
|
|
UNUSED(now);
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
REQUIRE(node == &rpsdb->origin_node || node == &rpsdb->data_node);
|
|
|
|
|
|
|
|
rpsdb_iter = isc_mem_get(rpsdb->common.mctx, sizeof(*rpsdb_iter));
|
|
|
|
|
|
|
|
memset(rpsdb_iter, 0, sizeof(*rpsdb_iter));
|
|
|
|
rpsdb_iter->common.magic = DNS_RDATASETITER_MAGIC;
|
|
|
|
rpsdb_iter->common.methods = &rpsdb_rdatasetiter_methods;
|
|
|
|
rpsdb_iter->common.db = db;
|
|
|
|
rpsdb_attachnode(db, node, &rpsdb_iter->common.node);
|
|
|
|
|
|
|
|
*iteratorp = &rpsdb_iter->common;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_issecure(dns_db_t *db) {
|
2017-09-11 11:53:42 -07:00
|
|
|
UNUSED(db);
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2017-09-11 11:53:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_getoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb = (rpsdb_t *)db;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
REQUIRE(nodep != NULL && *nodep == NULL);
|
|
|
|
|
|
|
|
rpsdb_attachnode(db, &rpsdb->origin_node, nodep);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdataset_disassociate(dns_rdataset_t *rdataset) {
|
2017-09-11 11:53:42 -07:00
|
|
|
dns_db_t *db;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach the last RR delivered.
|
|
|
|
*/
|
|
|
|
if (RD_CUR_RR(rdataset) != NULL) {
|
|
|
|
free(RD_CUR_RR(rdataset));
|
|
|
|
RD_CUR_RR(rdataset) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
db = RD_DB(rdataset);
|
|
|
|
RD_DB(rdataset) = NULL;
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdataset_next(dns_rdataset_t *rdataset) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb;
|
|
|
|
uint16_t type;
|
|
|
|
dns_rdataclass_t class;
|
2020-02-13 14:44:37 -08:00
|
|
|
librpz_rr_t *rr;
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t emsg;
|
|
|
|
|
|
|
|
rpsdb = RD_DB(rdataset);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach the previous RR.
|
|
|
|
*/
|
|
|
|
if (RD_CUR_RR(rdataset) != NULL) {
|
|
|
|
free(RD_CUR_RR(rdataset));
|
|
|
|
RD_CUR_RR(rdataset) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next RR of the specified type.
|
|
|
|
* SOAs differ.
|
|
|
|
*/
|
|
|
|
if (rdataset->type == dns_rdatatype_soa) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (RD_NEXT_RR(rdataset) == LIBRPZ_IDX_NULL) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_NOMORE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
RD_NEXT_RR(rdataset) = LIBRPZ_IDX_NULL;
|
2020-02-12 13:59:18 +01:00
|
|
|
if (!librpz->rsp_soa(&emsg, NULL, &rr, NULL, &rpsdb->result,
|
|
|
|
rpsdb->rsp)) {
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
RD_CUR_RR(rdataset) = rr;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpsdb->result.next_rr = RD_NEXT_RR(rdataset);
|
|
|
|
for (;;) {
|
|
|
|
if (!librpz->rsp_rr(&emsg, &type, &class, NULL, &rr,
|
|
|
|
&rpsdb->result, rpsdb->qname->ndata,
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb->qname->length, rpsdb->rsp))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (rdataset->type == type && rdataset->rdclass == class) {
|
2017-09-11 11:53:42 -07:00
|
|
|
RD_CUR_RR(rdataset) = rr;
|
|
|
|
RD_NEXT_RR(rdataset) = rpsdb->result.next_rr;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (type == dns_rdatatype_none) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_NOMORE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
free(rr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdataset_first(dns_rdataset_t *rdataset) {
|
|
|
|
rpsdb_t *rpsdb;
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_emsg_t emsg;
|
|
|
|
|
|
|
|
rpsdb = RD_DB(rdataset);
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
|
|
|
|
if (RD_CUR_RR(rdataset) != NULL) {
|
|
|
|
free(RD_CUR_RR(rdataset));
|
|
|
|
RD_CUR_RR(rdataset) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!librpz->rsp_result(&emsg, &rpsdb->result, true, rpsdb->rsp)) {
|
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (rdataset->type == dns_rdatatype_soa) {
|
2017-09-11 11:53:42 -07:00
|
|
|
RD_NEXT_RR(rdataset) = LIBRPZ_IDX_BAD;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2017-09-11 11:53:42 -07:00
|
|
|
RD_NEXT_RR(rdataset) = rpsdb->result.next_rr;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
return (rpsdb_rdataset_next(rdataset));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
|
|
|
|
rpsdb_t *rpsdb;
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz_rr_t *rr;
|
|
|
|
isc_region_t r;
|
|
|
|
|
|
|
|
rpsdb = RD_DB(rdataset);
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
rr = RD_CUR_RR(rdataset);
|
|
|
|
REQUIRE(rr != NULL);
|
|
|
|
|
|
|
|
r.length = ntohs(rr->rdlength);
|
|
|
|
r.base = rr->rdata;
|
|
|
|
dns_rdata_fromregion(rdata, ntohs(rr->class), ntohs(rr->type), &r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
|
|
|
|
rpsdb_t *rpsdb;
|
2017-09-11 11:53:42 -07:00
|
|
|
dns_db_t *dbp;
|
|
|
|
|
|
|
|
INSIST(!ISC_LINK_LINKED(target, link));
|
|
|
|
*target = *source;
|
|
|
|
ISC_LINK_INIT(target, link);
|
|
|
|
rpsdb = RD_DB(source);
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
dbp = NULL;
|
|
|
|
dns_db_attach(&rpsdb->common, &dbp);
|
|
|
|
RD_DB(target) = dbp;
|
|
|
|
RD_CUR_RR(target) = NULL;
|
|
|
|
RD_NEXT_RR(target) = LIBRPZ_IDX_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdataset_count(dns_rdataset_t *rdataset) {
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_t *rpsdb;
|
|
|
|
|
|
|
|
rpsdb = RD_DB(rdataset);
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
|
|
|
|
return (RD_COUNT(rdataset));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp) {
|
|
|
|
rpsdb_t *rpsdb;
|
2017-09-11 11:53:42 -07:00
|
|
|
dns_rdatasetiter_t *iterator;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx;
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
iterator = *iteratorp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*iteratorp = NULL;
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb = (rpsdb_t *)iterator->db;
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
|
|
|
|
mctx = iterator->db->mctx;
|
|
|
|
dns_db_detachnode(iterator->db, &iterator->node);
|
|
|
|
isc_mem_put(mctx, iterator, sizeof(rpsdb_rdatasetiter_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdatasetiter_next(dns_rdatasetiter_t *iter) {
|
|
|
|
rpsdb_t *rpsdb;
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_rdatasetiter_t *rpsdb_iter;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdatatype_t next_type, type;
|
|
|
|
dns_rdataclass_t next_class, class;
|
|
|
|
uint32_t ttl;
|
|
|
|
librpz_emsg_t emsg;
|
2017-09-11 11:53:42 -07:00
|
|
|
|
|
|
|
rpsdb = (rpsdb_t *)iter->db;
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
rpsdb_iter = (rpsdb_rdatasetiter_t *)iter;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is only used after a policy has been chosen,
|
|
|
|
* and so without caring whether it is after recursion.
|
|
|
|
*/
|
|
|
|
if (!librpz->rsp_result(&emsg, &rpsdb->result, true, rpsdb->rsp)) {
|
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Find the next class and type after the current class and type
|
|
|
|
* among the RRs in current result.
|
|
|
|
* As a side effect, count the number of those RRs.
|
|
|
|
*/
|
|
|
|
rpsdb_iter->count = 0;
|
|
|
|
next_class = dns_rdataclass_reserved0;
|
|
|
|
next_type = dns_rdatatype_none;
|
|
|
|
for (;;) {
|
2020-02-12 13:59:18 +01:00
|
|
|
if (!librpz->rsp_rr(&emsg, &type, &class, &ttl, NULL,
|
|
|
|
&rpsdb->result, rpsdb->qname->ndata,
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb->qname->length, rpsdb->rsp))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
librpz->log(LIBRPZ_LOG_ERROR, NULL, "%s", emsg.c);
|
|
|
|
return (DNS_R_SERVFAIL);
|
|
|
|
}
|
|
|
|
if (type == dns_rdatatype_none) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (next_type == dns_rdatatype_none) {
|
2017-09-11 11:53:42 -07:00
|
|
|
return (ISC_R_NOMORE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_iter->type = next_type;
|
|
|
|
rpsdb_iter->class = next_class;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Skip RRs with the current class and type or before.
|
|
|
|
*/
|
|
|
|
if (rpsdb_iter->class > class ||
|
2020-02-13 14:44:37 -08:00
|
|
|
(rpsdb_iter->class = class && rpsdb_iter->type >= type))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (next_type == dns_rdatatype_none || next_class > class ||
|
2020-02-13 14:44:37 -08:00
|
|
|
(next_class == class && next_type > type))
|
|
|
|
{
|
2017-09-11 11:53:42 -07:00
|
|
|
/*
|
|
|
|
* This is the first of a subsequent class and type.
|
|
|
|
*/
|
|
|
|
next_type = type;
|
|
|
|
next_class = class;
|
|
|
|
rpsdb_iter->ttl = ttl;
|
|
|
|
rpsdb_iter->count = 1;
|
|
|
|
rpsdb_iter->next_rr = rpsdb->result.next_rr;
|
|
|
|
} else if (next_type == type && next_class == class) {
|
|
|
|
++rpsdb_iter->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
rpsdb_rdatasetiter_first(dns_rdatasetiter_t *iterator) {
|
|
|
|
rpsdb_t *rpsdb;
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_rdatasetiter_t *rpsdb_iter;
|
|
|
|
|
|
|
|
rpsdb = (rpsdb_t *)iterator->db;
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
rpsdb_iter = (rpsdb_rdatasetiter_t *)iterator;
|
|
|
|
|
|
|
|
rpsdb_iter->type = dns_rdatatype_none;
|
|
|
|
rpsdb_iter->class = dns_rdataclass_reserved0;
|
|
|
|
return (rpsdb_rdatasetiter_next(iterator));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpsdb_rdatasetiter_current(dns_rdatasetiter_t *iterator,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdataset_t *rdataset) {
|
|
|
|
rpsdb_t *rpsdb;
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_rdatasetiter_t *rpsdb_iter;
|
|
|
|
|
|
|
|
rpsdb = (rpsdb_t *)iterator->db;
|
|
|
|
REQUIRE(VALID_RPSDB(rpsdb));
|
|
|
|
rpsdb_iter = (rpsdb_rdatasetiter_t *)iterator;
|
|
|
|
REQUIRE(rpsdb_iter->type != dns_rdatatype_none);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
rpsdb_bind_rdataset(rdataset, rpsdb_iter->count, rpsdb_iter->next_rr,
|
|
|
|
rpsdb_iter->type, rpsdb_iter->class,
|
|
|
|
rpsdb_iter->ttl, rpsdb);
|
2017-09-11 11:53:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static dns_dbmethods_t rpsdb_db_methods = {
|
|
|
|
rpsdb_attach,
|
|
|
|
rpsdb_detach,
|
2020-02-12 13:59:18 +01:00
|
|
|
NULL, /* beginload */
|
|
|
|
NULL, /* endload */
|
|
|
|
NULL, /* dump */
|
|
|
|
NULL, /* currentversion */
|
|
|
|
NULL, /* newversion */
|
|
|
|
NULL, /* attachversion */
|
|
|
|
NULL, /* closeversion */
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_findnode,
|
|
|
|
rpsdb_finddb,
|
2020-02-12 13:59:18 +01:00
|
|
|
NULL, /* findzonecut*/
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_attachnode,
|
|
|
|
rpsdb_detachnode,
|
2020-02-12 13:59:18 +01:00
|
|
|
NULL, /* expirenode */
|
|
|
|
NULL, /* printnode */
|
|
|
|
NULL, /* createiterator */
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_findrdataset,
|
|
|
|
rpsdb_allrdatasets,
|
2020-02-12 13:59:18 +01:00
|
|
|
NULL, /* addrdataset */
|
|
|
|
NULL, /* subtractrdataset */
|
|
|
|
NULL, /* deleterdataset */
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_issecure,
|
2020-02-12 13:59:18 +01:00
|
|
|
NULL, /* nodecount */
|
|
|
|
NULL, /* ispersistent */
|
|
|
|
NULL, /* overmem */
|
|
|
|
NULL, /* settask */
|
2017-09-11 11:53:42 -07:00
|
|
|
rpsdb_getoriginnode,
|
2020-02-12 13:59:18 +01:00
|
|
|
NULL, /* transfernode */
|
|
|
|
NULL, /* getnsec3parameters */
|
|
|
|
NULL, /* findnsec3node */
|
|
|
|
NULL, /* setsigningtime */
|
|
|
|
NULL, /* getsigningtime */
|
|
|
|
NULL, /* resigned */
|
|
|
|
NULL, /* isdnssec */
|
|
|
|
NULL, /* getrrsetstats */
|
|
|
|
NULL, /* rpz_attach */
|
|
|
|
NULL, /* rpz_ready */
|
|
|
|
NULL, /* findnodeext */
|
|
|
|
NULL, /* findext */
|
|
|
|
NULL, /* setcachestats */
|
|
|
|
NULL, /* hashsize */
|
|
|
|
NULL, /* nodefullname */
|
|
|
|
NULL, /* getsize */
|
|
|
|
NULL, /* setservestalettl */
|
|
|
|
NULL, /* getservestalettl */
|
Add stale-refresh-time option
Before this update, BIND would attempt to do a full recursive resolution
process for each query received if the requested rrset had its ttl
expired. If the resolution fails for any reason, only then BIND would
check for stale rrset in cache (if 'stale-cache-enable' and
'stale-answer-enable' is on).
The problem with this approach is that if an authoritative server is
unreachable or is failing to respond, it is very unlikely that the
problem will be fixed in the next seconds.
A better approach to improve performance in those cases, is to mark the
moment in which a resolution failed, and if new queries arrive for that
same rrset, try to respond directly from the stale cache, and do that
for a window of time configured via 'stale-refresh-time'.
Only when this interval expires we then try to do a normal refresh of
the rrset.
The logic behind this commit is as following:
- In query.c / query_gotanswer(), if the test of 'result' variable falls
to the default case, an error is assumed to have happened, and a call
to 'query_usestale()' is made to check if serving of stale rrset is
enabled in configuration.
- If serving of stale answers is enabled, a flag will be turned on in
the query context to look for stale records:
query.c:6839
qctx->client->query.dboptions |= DNS_DBFIND_STALEOK;
- A call to query_lookup() will be made again, inside it a call to
'dns_db_findext()' is made, which in turn will invoke rbdb.c /
cache_find().
- In rbtdb.c / cache_find() the important bits of this change is the
call to 'check_stale_header()', which is a function that yields true
if we should skip the stale entry, or false if we should consider it.
- In check_stale_header() we now check if the DNS_DBFIND_STALEOK option
is set, if that is the case we know that this new search for stale
records was made due to a failure in a normal resolution, so we keep
track of the time in which the failured occured in rbtdb.c:4559:
header->last_refresh_fail_ts = search->now;
- In check_stale_header(), if DNS_DBFIND_STALEOK is not set, then we
know this is a normal lookup, if the record is stale and the query
time is between last failure time + stale-refresh-time window, then
we return false so cache_find() knows it can consider this stale
rrset entry to return as a response.
The last additions are two new methods to the database interface:
- setservestale_refresh
- getservestale_refresh
Those were added so rbtdb can be aware of the value set in configuration
option, since in that level we have no access to the view object.
2020-10-19 17:02:03 -03:00
|
|
|
NULL, /* setservestalerefresh */
|
|
|
|
NULL, /* getservestalerefresh */
|
Fix the rbt hashtable and grow it when setting max-cache-size
There were several problems with rbt hashtable implementation:
1. Our internal hashing function returns uint64_t value, but it was
silently truncated to unsigned int in dns_name_hash() and
dns_name_fullhash() functions. As the SipHash 2-4 higher bits are
more random, we need to use the upper half of the return value.
2. The hashtable implementation in rbt.c was using modulo to pick the
slot number for the hash table. This has several problems because
modulo is: a) slow, b) oblivious to patterns in the input data. This
could lead to very uneven distribution of the hashed data in the
hashtable. Combined with the single-linked lists we use, it could
really hog-down the lookup and removal of the nodes from the rbt
tree[a]. The Fibonacci Hashing is much better fit for the hashtable
function here. For longer description, read "Fibonacci Hashing: The
Optimization that the World Forgot"[b] or just look at the Linux
kernel. Also this will make Diego very happy :).
3. The hashtable would rehash every time the number of nodes in the rbt
tree would exceed 3 * (hashtable size). The overcommit will make the
uneven distribution in the hashtable even worse, but the main problem
lies in the rehashing - every time the database grows beyond the
limit, each subsequent rehashing will be much slower. The mitigation
here is letting the rbt know how big the cache can grown and
pre-allocate the hashtable to be big enough to actually never need to
rehash. This will consume more memory at the start, but since the
size of the hashtable is capped to `1 << 32` (e.g. 4 mio entries), it
will only consume maximum of 32GB of memory for hashtable in the
worst case (and max-cache-size would need to be set to more than
4TB). Calling the dns_db_adjusthashsize() will also cap the maximum
size of the hashtable to the pre-computed number of bits, so it won't
try to consume more gigabytes of memory than available for the
database.
FIXME: What is the average size of the rbt node that gets hashed? I
chose the pagesize (4k) as initial value to precompute the size of
the hashtable, but the value is based on feeling and not any real
data.
For future work, there are more places where we use result of the hash
value modulo some small number and that would benefit from Fibonacci
Hashing to get better distribution.
Notes:
a. A doubly linked list should be used here to speedup the removal of
the entries from the hashtable.
b. https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/
2020-07-16 10:29:54 +02:00
|
|
|
NULL, /* setgluecachestats */
|
2017-09-11 11:53:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static dns_rdatasetmethods_t rpsdb_rdataset_methods = {
|
|
|
|
rpsdb_rdataset_disassociate,
|
|
|
|
rpsdb_rdataset_first,
|
|
|
|
rpsdb_rdataset_next,
|
|
|
|
rpsdb_rdataset_current,
|
|
|
|
rpsdb_rdataset_clone,
|
|
|
|
rpsdb_rdataset_count,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static dns_rdatasetitermethods_t rpsdb_rdatasetiter_methods = {
|
2020-02-12 13:59:18 +01:00
|
|
|
rpsdb_rdatasetiter_destroy, rpsdb_rdatasetiter_first,
|
|
|
|
rpsdb_rdatasetiter_next, rpsdb_rdatasetiter_current
|
2017-09-11 11:53:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* USE_DNSRPS */
|