2014-05-29 22:22:53 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2014-05-29 22:22:53 -07:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10: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.
|
2014-05-29 22:22:53 -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
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
#include <isc/async.h>
|
2014-06-18 16:47:22 -07:00
|
|
|
#include <isc/buffer.h>
|
2022-07-26 13:03:40 +02:00
|
|
|
#include <isc/event.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <isc/log.h>
|
2022-07-26 13:03:40 +02:00
|
|
|
#include <isc/loop.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <isc/mem.h>
|
2014-06-19 10:20:34 +10:00
|
|
|
#include <isc/print.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <isc/rwlock.h>
|
|
|
|
#include <isc/string.h>
|
2014-06-18 16:47:22 -07:00
|
|
|
#include <isc/task.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <isc/time.h>
|
2014-06-18 16:47:22 -07:00
|
|
|
#include <isc/timer.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2014-06-18 16:47:22 -07:00
|
|
|
#include <dns/db.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <dns/fixedname.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <dns/log.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <dns/name.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <dns/nta.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
#include <dns/rbt.h>
|
2014-06-18 16:47:22 -07:00
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/resolver.h>
|
2015-01-12 09:04:16 +05:30
|
|
|
#include <dns/time.h>
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
struct dns_ntatable {
|
|
|
|
/* Unlocked. */
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int magic;
|
2022-09-12 16:36:30 +02:00
|
|
|
dns_view_t *view;
|
|
|
|
isc_rwlock_t rwlock;
|
|
|
|
isc_loopmgr_t *loopmgr;
|
|
|
|
isc_task_t *task;
|
|
|
|
/* Protected by atomics */
|
|
|
|
isc_refcount_t references;
|
|
|
|
/* Locked by rwlock. */
|
|
|
|
dns_rbt_t *table;
|
|
|
|
bool shuttingdown;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dns__nta {
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_refcount_t references;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_ntatable_t *ntatable;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool forced;
|
2022-09-12 16:36:30 +02:00
|
|
|
isc_loop_t *loop;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_timer_t *timer;
|
|
|
|
dns_fetch_t *fetch;
|
|
|
|
dns_rdataset_t rdataset;
|
|
|
|
dns_rdataset_t sigrdataset;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_fixedname_t fn;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *name;
|
|
|
|
isc_stdtime_t expiry;
|
2014-06-18 16:47:22 -07:00
|
|
|
};
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define NTA_MAGIC ISC_MAGIC('N', 'T', 'A', 'n')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_NTA(nn) ISC_MAGIC_VALID(nn, NTA_MAGIC)
|
2014-06-18 16:47:22 -07:00
|
|
|
|
2014-07-10 10:24:47 +10:00
|
|
|
static void
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_shutdown(dns__nta_t *nta);
|
2014-07-10 10:24:47 +10:00
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
static void
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_destroy(dns__nta_t *nta) {
|
|
|
|
isc_refcount_destroy(&nta->references);
|
|
|
|
nta->magic = 0;
|
|
|
|
REQUIRE(nta->timer == NULL);
|
|
|
|
if (dns_rdataset_isassociated(&nta->rdataset)) {
|
|
|
|
dns_rdataset_disassociate(&nta->rdataset);
|
|
|
|
}
|
|
|
|
if (dns_rdataset_isassociated(&nta->sigrdataset)) {
|
|
|
|
dns_rdataset_disassociate(&nta->sigrdataset);
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
2022-09-12 16:36:30 +02:00
|
|
|
if (nta->fetch != NULL) {
|
|
|
|
dns_resolver_cancelfetch(nta->fetch);
|
|
|
|
dns_resolver_destroyfetch(&nta->fetch);
|
|
|
|
}
|
|
|
|
isc_mem_putanddetach(&nta->mctx, nta, sizeof(*nta));
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
ISC_REFCOUNT_IMPL(dns__nta, dns__nta_destroy);
|
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
static void
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_free(void *data, void *arg) {
|
|
|
|
dns__nta_t *nta = (dns__nta_t *)data;
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
UNUSED(arg);
|
|
|
|
|
|
|
|
dns__nta_shutdown(nta);
|
|
|
|
dns__nta_detach(&nta); /* for nta_create() */
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_ntatable_create(dns_view_t *view, isc_taskmgr_t *taskmgr,
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_loopmgr_t *loopmgr, dns_ntatable_t **ntatablep) {
|
2014-05-29 22:22:53 -07:00
|
|
|
dns_ntatable_t *ntatable;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2014-05-29 22:22:53 -07:00
|
|
|
|
|
|
|
REQUIRE(ntatablep != NULL && *ntatablep == NULL);
|
|
|
|
|
2014-06-18 16:47:22 -07:00
|
|
|
ntatable = isc_mem_get(view->mctx, sizeof(*ntatable));
|
2022-09-12 16:36:30 +02:00
|
|
|
*ntatable = (dns_ntatable_t){
|
|
|
|
.loopmgr = loopmgr,
|
|
|
|
.view = view,
|
|
|
|
};
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-07-26 13:03:45 +02:00
|
|
|
result = isc_task_create(taskmgr, &ntatable->task, 0);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2014-05-29 22:22:53 -07:00
|
|
|
goto cleanup_ntatable;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-18 16:47:22 -07:00
|
|
|
isc_task_setname(ntatable->task, "ntatable", ntatable);
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
result = dns_rbt_create(view->mctx, dns__nta_free, NULL,
|
2014-06-18 16:47:22 -07:00
|
|
|
&ntatable->table);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2014-06-18 16:47:22 -07:00
|
|
|
goto cleanup_task;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2021-02-01 15:59:41 +11:00
|
|
|
isc_rwlock_init(&ntatable->rwlock, 0, 0);
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2019-05-20 16:41:24 +02:00
|
|
|
isc_refcount_init(&ntatable->references, 1);
|
2014-06-18 16:47:22 -07:00
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
ntatable->magic = NTATABLE_MAGIC;
|
|
|
|
*ntatablep = ntatable;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup_task:
|
2014-06-18 16:47:22 -07:00
|
|
|
isc_task_detach(&ntatable->task);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup_ntatable:
|
2018-11-09 15:32:33 +11:00
|
|
|
isc_mem_put(view->mctx, ntatable, sizeof(*ntatable));
|
2014-05-29 22:22:53 -07:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
static void
|
|
|
|
dns__ntatable_destroy(dns_ntatable_t *ntatable) {
|
|
|
|
isc_refcount_destroy(&ntatable->references);
|
|
|
|
ntatable->magic = 0;
|
|
|
|
dns_rbt_destroy(&ntatable->table);
|
|
|
|
isc_rwlock_destroy(&ntatable->rwlock);
|
|
|
|
isc_task_detach(&ntatable->task);
|
|
|
|
isc_mem_put(ntatable->view->mctx, ntatable, sizeof(*ntatable));
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
ISC_REFCOUNT_IMPL(dns_ntatable, dns__ntatable_destroy);
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
fetch_done(isc_task_t *task, isc_event_t *event) {
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t *nta = devent->ev_arg;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t eresult = devent->result;
|
|
|
|
dns_ntatable_t *ntatable = nta->ntatable;
|
|
|
|
dns_view_t *view = ntatable->view;
|
|
|
|
isc_stdtime_t now;
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dns_rdataset_isassociated(&nta->rdataset)) {
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_rdataset_disassociate(&nta->rdataset);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (dns_rdataset_isassociated(&nta->sigrdataset)) {
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_rdataset_disassociate(&nta->sigrdataset);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (nta->fetch == devent->fetch) {
|
2014-10-18 10:07:24 +11:00
|
|
|
nta->fetch = NULL;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-10-18 10:07:24 +11:00
|
|
|
dns_resolver_destroyfetch(&devent->fetch);
|
2014-06-18 16:47:22 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (devent->node != NULL) {
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_db_detachnode(devent->db, &devent->node);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (devent->db != NULL) {
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_db_detach(&devent->db);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
isc_event_free(&event);
|
2014-06-25 00:44:11 +10:00
|
|
|
isc_stdtime_get(&now);
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
switch (eresult) {
|
|
|
|
case ISC_R_SUCCESS:
|
|
|
|
case DNS_R_NCACHENXDOMAIN:
|
|
|
|
case DNS_R_NXDOMAIN:
|
|
|
|
case DNS_R_NCACHENXRRSET:
|
|
|
|
case DNS_R_NXRRSET:
|
2022-07-26 13:03:40 +02:00
|
|
|
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (nta->expiry > now) {
|
2017-10-24 09:54:25 +11:00
|
|
|
nta->expiry = now;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-07-26 13:03:40 +02:00
|
|
|
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
2014-06-18 16:47:22 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're expiring before the next recheck, we might
|
|
|
|
* as well stop the timer now.
|
|
|
|
*/
|
2022-07-26 13:03:40 +02:00
|
|
|
RWLOCK(&ntatable->rwlock, isc_rwlocktype_read);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (nta->timer != NULL && nta->expiry - now < view->nta_recheck) {
|
2022-09-12 16:36:30 +02:00
|
|
|
isc_timer_stop(nta->timer);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-07-26 13:03:40 +02:00
|
|
|
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_read);
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_detach(&nta); /* for dns_resolver_createfetch() */
|
2020-08-07 18:00:41 +10:00
|
|
|
dns_view_weakdetach(&view);
|
2014-06-18 16:47:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-07-26 13:03:40 +02:00
|
|
|
checkbogus(void *arg) {
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t *nta = arg;
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_ntatable_t *ntatable = nta->ntatable;
|
2020-08-07 18:00:41 +10:00
|
|
|
dns_view_t *view = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
if (nta->fetch != NULL) {
|
|
|
|
dns_resolver_cancelfetch(nta->fetch);
|
2014-10-18 10:07:24 +11:00
|
|
|
nta->fetch = NULL;
|
2014-06-18 16:47:22 -07:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dns_rdataset_isassociated(&nta->rdataset)) {
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_rdataset_disassociate(&nta->rdataset);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (dns_rdataset_isassociated(&nta->sigrdataset)) {
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_rdataset_disassociate(&nta->sigrdataset);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-18 16:47:22 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_ref(nta); /* for dns_resolver_createfetch */
|
2020-08-07 18:00:41 +10:00
|
|
|
dns_view_weakattach(ntatable->view, &view);
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_resolver_createfetch(
|
|
|
|
view->resolver, nta->name, dns_rdatatype_nsec, NULL, NULL, NULL,
|
2022-07-26 13:03:40 +02:00
|
|
|
NULL, 0, DNS_FETCHOPT_NONTA, 0, NULL, ntatable->task,
|
|
|
|
fetch_done, nta, &nta->rdataset, &nta->sigrdataset,
|
|
|
|
&nta->fetch);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_detach(&nta); /* for dns_resolver_createfetch() */
|
2020-11-03 11:25:55 +11:00
|
|
|
dns_view_weakdetach(&view);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-18 16:47:22 -07:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
static void
|
2022-09-12 16:36:30 +02:00
|
|
|
settimer(dns_ntatable_t *ntatable, dns__nta_t *nta, uint32_t lifetime) {
|
2022-07-26 13:03:40 +02:00
|
|
|
dns_view_t *view = NULL;
|
2022-09-12 16:36:30 +02:00
|
|
|
isc_interval_t interval;
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
|
|
|
REQUIRE(VALID_NTA(nta));
|
|
|
|
|
|
|
|
view = ntatable->view;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (view->nta_recheck == 0 || lifetime <= view->nta_recheck) {
|
2022-07-26 13:03:40 +02:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-18 16:47:22 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
isc_timer_create(nta->loop, checkbogus, nta, &nta->timer);
|
2014-06-18 16:47:22 -07:00
|
|
|
isc_interval_set(&interval, view->nta_recheck, 0);
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_timer_start(nta->timer, isc_timertype_ticker, &interval);
|
2014-06-18 16:47:22 -07:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
nta_create(dns_ntatable_t *ntatable, const dns_name_t *name,
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t **target) {
|
|
|
|
dns__nta_t *nta = NULL;
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_view_t *view;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
|
|
|
REQUIRE(target != NULL && *target == NULL);
|
|
|
|
|
|
|
|
view = ntatable->view;
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
nta = isc_mem_get(view->mctx, sizeof(dns__nta_t));
|
|
|
|
*nta = (dns__nta_t){
|
|
|
|
.ntatable = ntatable,
|
|
|
|
.loop = isc_loop_current(ntatable->loopmgr),
|
|
|
|
.magic = NTA_MAGIC,
|
|
|
|
};
|
|
|
|
|
|
|
|
isc_mem_attach(view->mctx, &nta->mctx);
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
dns_rdataset_init(&nta->rdataset);
|
|
|
|
dns_rdataset_init(&nta->sigrdataset);
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
isc_refcount_init(&nta->references, 1);
|
2014-06-18 16:47:22 -07:00
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
nta->name = dns_fixedname_initname(&nta->fn);
|
2021-05-21 17:20:44 -07:00
|
|
|
dns_name_copy(name, nta->name);
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
*target = nta;
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_ntatable_add(dns_ntatable_t *ntatable, const dns_name_t *name, bool force,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stdtime_t now, uint32_t lifetime) {
|
2020-08-07 18:00:41 +10:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t *nta = NULL;
|
|
|
|
dns_rbtnode_t *node = NULL;
|
2014-05-29 22:22:53 -07:00
|
|
|
|
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
|
|
|
|
2020-08-07 18:00:41 +10:00
|
|
|
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
if (ntatable->shuttingdown) {
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
nta_create(ntatable, name, &nta);
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2014-06-18 16:47:22 -07:00
|
|
|
nta->expiry = now + lifetime;
|
2015-01-12 09:04:16 +05:30
|
|
|
nta->forced = force;
|
2014-05-29 22:22:53 -07:00
|
|
|
|
|
|
|
result = dns_rbt_addnode(ntatable->table, name, &node);
|
2022-09-12 16:36:30 +02:00
|
|
|
switch (result) {
|
|
|
|
case ISC_R_EXISTS:
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
if (node->data != NULL) {
|
|
|
|
/* NTA already exists, just update the timer */
|
|
|
|
dns__nta_t *node_nta = (dns__nta_t *)node->data;
|
|
|
|
node_nta->expiry = nta->expiry;
|
|
|
|
dns__nta_detach(&nta); /* for nta_create */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Node was empty, update as if new */
|
|
|
|
FALLTHROUGH;
|
|
|
|
case ISC_R_SUCCESS:
|
|
|
|
INSIST(node != NULL);
|
|
|
|
INSIST(node->data == NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!force) {
|
2022-07-26 13:03:40 +02:00
|
|
|
settimer(ntatable, nta, lifetime);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
node->data = nta;
|
2022-09-12 16:36:30 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-05-30 23:45:21 +00:00
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2020-08-07 18:00:41 +10:00
|
|
|
unlock:
|
2014-05-29 22:22:53 -07:00
|
|
|
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Caller must hold a write lock on rwlock.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
deletenode(dns_ntatable_t *ntatable, const dns_name_t *name) {
|
|
|
|
isc_result_t result;
|
2014-05-29 22:22:53 -07:00
|
|
|
dns_rbtnode_t *node = NULL;
|
|
|
|
|
|
|
|
result = dns_rbt_findnode(ntatable->table, name, NULL, &node, NULL,
|
|
|
|
DNS_RBTFIND_NOOPTIONS, NULL, NULL);
|
2022-09-12 16:36:30 +02:00
|
|
|
switch (result) {
|
|
|
|
case ISC_R_SUCCESS:
|
|
|
|
if (node->data == NULL) {
|
|
|
|
/* Found empty node */
|
|
|
|
return (ISC_R_NOTFOUND);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
result = dns_rbt_deletenode(ntatable->table, node, false);
|
|
|
|
return (result);
|
|
|
|
case DNS_R_PARTIALMATCH:
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
default:
|
|
|
|
return (result);
|
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_ntatable_delete(dns_ntatable_t *ntatable, const dns_name_t *name) {
|
2014-05-29 22:22:53 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
|
|
|
REQUIRE(name != NULL);
|
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
2017-07-21 11:52:24 +10:00
|
|
|
result = deletenode(ntatable, name);
|
2014-05-29 22:22:53 -07:00
|
|
|
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
2014-05-30 23:45:21 +00:00
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2014-05-29 22:22:53 -07:00
|
|
|
dns_ntatable_covered(dns_ntatable_t *ntatable, isc_stdtime_t now,
|
2020-02-13 14:44:37 -08:00
|
|
|
const dns_name_t *name, const dns_name_t *anchor) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_fixedname_t fn;
|
|
|
|
dns_rbtnode_t *node;
|
|
|
|
dns_name_t *foundname;
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t *nta = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool answer = false;
|
2022-09-12 16:36:30 +02:00
|
|
|
isc_rwlocktype_t locktype;
|
|
|
|
char nb[DNS_NAME_FORMATSIZE];
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
2014-05-29 22:22:53 -07:00
|
|
|
REQUIRE(dns_name_isabsolute(name));
|
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
foundname = dns_fixedname_initname(&fn);
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
locktype = isc_rwlocktype_read;
|
2020-02-12 13:59:18 +01:00
|
|
|
relock:
|
2014-05-29 22:22:53 -07:00
|
|
|
RWLOCK(&ntatable->rwlock, locktype);
|
2020-02-12 13:59:18 +01:00
|
|
|
again:
|
2014-05-29 22:22:53 -07:00
|
|
|
node = NULL;
|
|
|
|
result = dns_rbt_findnode(ntatable->table, name, foundname, &node, NULL,
|
|
|
|
DNS_RBTFIND_NOOPTIONS, NULL, NULL);
|
2022-09-12 16:36:30 +02:00
|
|
|
switch (result) {
|
|
|
|
case ISC_R_SUCCESS:
|
|
|
|
/* Found a node */
|
|
|
|
break;
|
|
|
|
case DNS_R_PARTIALMATCH:
|
|
|
|
if (!dns_name_issubdomain(foundname, anchor)) {
|
|
|
|
goto unlock;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-09-12 16:36:30 +02:00
|
|
|
/* Found a parental node */
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto unlock;
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
2022-09-12 16:36:30 +02:00
|
|
|
|
|
|
|
INSIST(result == ISC_R_SUCCESS);
|
|
|
|
nta = (dns__nta_t *)node->data;
|
|
|
|
if (nta->expiry > now) {
|
|
|
|
/* We got non-expired answer */
|
|
|
|
answer = true;
|
|
|
|
goto unlock;
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with expired NTA */
|
2022-09-12 16:36:30 +02:00
|
|
|
if (locktype == isc_rwlocktype_read) {
|
|
|
|
RWUNLOCK(&ntatable->rwlock, locktype);
|
|
|
|
locktype = isc_rwlocktype_write;
|
|
|
|
goto relock;
|
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
dns_name_format(foundname, nb, sizeof(nb));
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DNSSEC, DNS_LOGMODULE_NTA,
|
|
|
|
ISC_LOG_INFO, "deleting expired NTA at %s", nb);
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
/* We already found the node under the lock, so just delete it */
|
|
|
|
result = dns_rbt_deletenode(ntatable->table, node, false);
|
|
|
|
INSIST(result == ISC_R_SUCCESS);
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
/* Look again */
|
|
|
|
goto again;
|
2014-06-18 16:47:22 -07:00
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
unlock:
|
2014-05-29 22:22:53 -07:00
|
|
|
RWUNLOCK(&ntatable->rwlock, locktype);
|
|
|
|
|
|
|
|
return (answer);
|
|
|
|
}
|
|
|
|
|
2015-02-05 17:18:15 -08:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
putstr(isc_buffer_t **b, const char *str) {
|
2015-02-05 17:18:15 -08:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
result = isc_buffer_reserve(b, strlen(str));
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2015-02-05 17:18:15 -08:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-02-06 23:45:21 +00:00
|
|
|
|
2015-02-05 17:18:15 -08:00
|
|
|
isc_buffer_putstr(*b, str);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
isc_result_t
|
2016-12-06 14:32:47 +00:00
|
|
|
dns_ntatable_totext(dns_ntatable_t *ntatable, const char *view,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t **buf) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_rbtnode_t *node;
|
2014-05-29 22:22:53 -07:00
|
|
|
dns_rbtnodechain_t chain;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool first = true;
|
|
|
|
isc_stdtime_t now;
|
2014-05-29 22:22:53 -07:00
|
|
|
|
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
|
|
|
|
2014-06-18 16:47:22 -07:00
|
|
|
isc_stdtime_get(&now);
|
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
RWLOCK(&ntatable->rwlock, isc_rwlocktype_read);
|
2019-08-27 22:41:18 -07:00
|
|
|
dns_rbtnodechain_init(&chain);
|
2014-05-29 22:22:53 -07:00
|
|
|
result = dns_rbtnodechain_first(&chain, ntatable->table, NULL, NULL);
|
2015-02-05 17:18:15 -08:00
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result == ISC_R_NOTFOUND) {
|
2015-02-05 17:18:15 -08:00
|
|
|
result = ISC_R_SUCCESS;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
goto cleanup;
|
2015-02-05 17:18:15 -08:00
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
for (;;) {
|
|
|
|
dns_rbtnodechain_current(&chain, NULL, NULL, &node);
|
|
|
|
if (node->data != NULL) {
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t *n = (dns__nta_t *)node->data;
|
2020-02-13 14:44:37 -08:00
|
|
|
char nbuf[DNS_NAME_FORMATSIZE];
|
|
|
|
char tbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
|
|
|
|
char obuf[DNS_NAME_FORMATSIZE +
|
|
|
|
ISC_FORMATHTTPTIMESTAMP_SIZE +
|
|
|
|
sizeof("expired: \n")];
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_fixedname_t fn;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *name;
|
|
|
|
isc_time_t t;
|
2014-05-29 22:22:53 -07:00
|
|
|
|
2020-03-03 22:46:58 -08:00
|
|
|
name = dns_fixedname_initname(&fn);
|
|
|
|
dns_rbt_fullnamefromnode(node, name);
|
|
|
|
dns_name_format(name, nbuf, sizeof(nbuf));
|
|
|
|
|
2018-04-30 16:10:17 -07:00
|
|
|
if (n->expiry != 0xffffffffU) {
|
2020-03-03 22:46:58 -08:00
|
|
|
/* Normal NTA entries */
|
2018-04-30 16:10:17 -07:00
|
|
|
isc_time_set(&t, n->expiry, 0);
|
|
|
|
isc_time_formattimestamp(&t, tbuf,
|
|
|
|
sizeof(tbuf));
|
|
|
|
|
2016-12-06 14:32:47 +00:00
|
|
|
snprintf(obuf, sizeof(obuf), "%s%s%s%s: %s %s",
|
2018-04-30 16:10:17 -07:00
|
|
|
first ? "" : "\n", nbuf,
|
2016-12-06 14:32:47 +00:00
|
|
|
view != NULL ? "/" : "",
|
|
|
|
view != NULL ? view : "",
|
2020-02-12 13:59:18 +01:00
|
|
|
n->expiry <= now ? "expired"
|
|
|
|
: "expiry",
|
2018-04-30 16:10:17 -07:00
|
|
|
tbuf);
|
2020-03-03 22:46:58 -08:00
|
|
|
} else {
|
|
|
|
/* "validate-except" entries */
|
|
|
|
snprintf(obuf, sizeof(obuf), "%s%s%s%s: %s",
|
|
|
|
first ? "" : "\n", nbuf,
|
|
|
|
view != NULL ? "/" : "",
|
|
|
|
view != NULL ? view : "", "permanent");
|
|
|
|
}
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
result = putstr(buf, obuf);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto cleanup;
|
2018-04-30 16:10:17 -07:00
|
|
|
}
|
2014-06-18 16:47:22 -07:00
|
|
|
}
|
|
|
|
result = dns_rbtnodechain_next(&chain, NULL, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result == ISC_R_NOMORE) {
|
2014-06-18 16:47:22 -07:00
|
|
|
result = ISC_R_SUCCESS;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-18 16:47:22 -07:00
|
|
|
break;
|
|
|
|
}
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2014-06-18 16:47:22 -07:00
|
|
|
dns_rbtnodechain_invalidate(&chain);
|
|
|
|
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_read);
|
|
|
|
return (result);
|
2014-05-29 22:22:53 -07:00
|
|
|
}
|
2015-02-05 17:18:15 -08:00
|
|
|
|
2015-01-12 09:04:16 +05:30
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_ntatable_save(dns_ntatable_t *ntatable, FILE *fp) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_rbtnode_t *node;
|
2015-01-12 09:04:16 +05:30
|
|
|
dns_rbtnodechain_t chain;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stdtime_t now;
|
|
|
|
bool written = false;
|
2015-01-12 09:04:16 +05:30
|
|
|
|
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
|
|
|
|
|
|
|
isc_stdtime_get(&now);
|
|
|
|
|
|
|
|
RWLOCK(&ntatable->rwlock, isc_rwlocktype_read);
|
2019-08-27 22:41:18 -07:00
|
|
|
dns_rbtnodechain_init(&chain);
|
2015-01-12 09:04:16 +05:30
|
|
|
result = dns_rbtnodechain_first(&chain, ntatable->table, NULL, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2015-01-12 09:04:16 +05:30
|
|
|
goto cleanup;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-01-12 09:04:16 +05:30
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
dns_rbtnodechain_current(&chain, NULL, NULL, &node);
|
|
|
|
if (node->data != NULL) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t b;
|
|
|
|
char nbuf[DNS_NAME_FORMATSIZE + 1], tbuf[80];
|
2018-04-30 16:10:17 -07:00
|
|
|
dns_fixedname_t fn;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *name;
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t *n = (dns__nta_t *)node->data;
|
2015-01-12 09:04:16 +05:30
|
|
|
|
2018-04-30 16:10:17 -07:00
|
|
|
/*
|
|
|
|
* Skip this node if the expiry is already in the
|
|
|
|
* past, or if this is a "validate-except" entry.
|
|
|
|
*/
|
|
|
|
if (n->expiry <= now || n->expiry == 0xffffffffU) {
|
|
|
|
goto skip;
|
|
|
|
}
|
2015-01-12 09:04:16 +05:30
|
|
|
|
2018-04-30 16:10:17 -07:00
|
|
|
name = dns_fixedname_initname(&fn);
|
|
|
|
dns_rbt_fullnamefromnode(node, name);
|
2015-01-12 09:04:16 +05:30
|
|
|
|
2018-04-30 16:10:17 -07:00
|
|
|
isc_buffer_init(&b, nbuf, sizeof(nbuf));
|
|
|
|
result = dns_name_totext(name, false, &b);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2018-04-30 16:10:17 -07:00
|
|
|
goto skip;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-01-12 09:04:16 +05:30
|
|
|
|
2018-04-30 16:10:17 -07:00
|
|
|
/* Zero terminate. */
|
|
|
|
isc_buffer_putuint8(&b, 0);
|
2015-01-12 09:04:16 +05:30
|
|
|
|
2018-04-30 16:10:17 -07:00
|
|
|
isc_buffer_init(&b, tbuf, sizeof(tbuf));
|
|
|
|
dns_time32_totext(n->expiry, &b);
|
2015-02-10 14:01:38 -08:00
|
|
|
|
2018-04-30 16:10:17 -07:00
|
|
|
/* Zero terminate. */
|
|
|
|
isc_buffer_putuint8(&b, 0);
|
|
|
|
|
|
|
|
fprintf(fp, "%s %s %s\n", nbuf,
|
2020-02-12 13:59:18 +01:00
|
|
|
n->forced ? "forced" : "regular", tbuf);
|
2018-04-30 16:10:17 -07:00
|
|
|
written = true;
|
2015-01-12 09:04:16 +05:30
|
|
|
}
|
|
|
|
skip:
|
|
|
|
result = dns_rbtnodechain_next(&chain, NULL, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result == ISC_R_NOMORE) {
|
2015-01-12 09:04:16 +05:30
|
|
|
result = ISC_R_SUCCESS;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-01-12 09:04:16 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2015-01-12 09:04:16 +05:30
|
|
|
dns_rbtnodechain_invalidate(&chain);
|
|
|
|
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_read);
|
|
|
|
|
2022-09-12 16:36:30 +02:00
|
|
|
if (result == ISC_R_SUCCESS && !written) {
|
|
|
|
result = ISC_R_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dns__nta_shutdown_cb(dns__nta_t *nta) {
|
|
|
|
REQUIRE(VALID_NTA(nta));
|
|
|
|
|
|
|
|
if (nta->timer) {
|
|
|
|
isc_timer_stop(nta->timer); /* This is superfluous */
|
|
|
|
isc_timer_destroy(&nta->timer);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-09-12 16:36:30 +02:00
|
|
|
|
|
|
|
dns__nta_detach(&nta);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dns__nta_shutdown(dns__nta_t *nta) {
|
|
|
|
REQUIRE(VALID_NTA(nta));
|
|
|
|
|
|
|
|
if (nta->timer != NULL) {
|
|
|
|
isc_timer_stop(nta->timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
dns__nta_ref(nta);
|
|
|
|
isc_async_run(nta->loop, (isc_job_cb)dns__nta_shutdown_cb, nta);
|
2015-01-12 09:04:16 +05:30
|
|
|
}
|
2020-08-07 18:00:41 +10:00
|
|
|
|
|
|
|
void
|
|
|
|
dns_ntatable_shutdown(dns_ntatable_t *ntatable) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_rbtnode_t *node;
|
|
|
|
dns_rbtnodechain_t chain;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NTATABLE(ntatable));
|
|
|
|
|
|
|
|
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
|
|
|
ntatable->shuttingdown = true;
|
|
|
|
|
|
|
|
dns_rbtnodechain_init(&chain);
|
|
|
|
result = dns_rbtnodechain_first(&chain, ntatable->table, NULL, NULL);
|
|
|
|
while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
|
|
|
|
dns_rbtnodechain_current(&chain, NULL, NULL, &node);
|
|
|
|
if (node->data != NULL) {
|
2022-09-12 16:36:30 +02:00
|
|
|
dns__nta_t *nta = (dns__nta_t *)node->data;
|
|
|
|
dns__nta_shutdown(nta);
|
2020-08-07 18:00:41 +10:00
|
|
|
}
|
|
|
|
result = dns_rbtnodechain_next(&chain, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
dns_rbtnodechain_invalidate(&chain);
|
|
|
|
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
|
|
|
|
}
|