Commit 6017f424 authored by Andreas Gustafsson's avatar Andreas Gustafsson
Browse files

introducing dns_acl_t; other restructuring of server

configuration process aiming to reduce the degree of mutual dependency
between lib/dns/config and the rest of libdns
parent b24e559d
/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <config.h>
#include <isc/assertions.h>
#include <isc/mem.h>
#include <isc/result.h>
#include <dns/aclconf.h>
#include <dns/fixedname.h>
#include <dns/types.h>
#include <dns/zone.h>
void dns_aclconfctx_init(dns_aclconfctx_t *ctx)
{
ISC_LIST_INIT(ctx->named_acl_cache);
}
void dns_aclconfctx_destroy(dns_aclconfctx_t *ctx)
{
dns_acl_t *dacl, *next;
for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
dacl != NULL;
dacl = next)
{
next = ISC_LIST_NEXT(dacl, nextincache);
dacl->name = NULL;
dns_acl_detach(&dacl);
}
}
static isc_result_t
convert_named_acl(char *aclname, dns_c_ctx_t *cctx,
dns_aclconfctx_t *ctx, isc_mem_t *mctx,
dns_acl_t **target)
{
isc_result_t result;
dns_c_acl_t *cacl;
dns_acl_t *dacl;
/* Look for an already-converted version. */
for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
dacl != NULL;
dacl = ISC_LIST_NEXT(dacl, nextincache))
{
if (strcmp(aclname, dacl->name) == 0) {
dns_acl_attach(dacl, target);
return ISC_R_SUCCESS;
}
}
/* Not yet converted. Convert now. */
result = dns_c_acltable_getacl(cctx->acls, aclname, &cacl);
if (result != DNS_R_SUCCESS) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
DNS_LOGMODULE_ACL, ISC_LOG_WARNING,
"undefined ACL \"%s\"", aclname);
return (result);
}
result = dns_acl_fromconfig(cacl->ipml, cctx, ctx, mctx, &dacl);
if (result != DNS_R_SUCCESS)
return (result);
dacl->name = aclname;
ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
dns_acl_attach(dacl, target);
return (ISC_R_SUCCESS);
}
static isc_result_t
convert_keyname(char *txtname, isc_mem_t *mctx, dns_name_t *dnsname) {
isc_result_t result;
isc_buffer_t buf;
dns_fixedname_t fixname;
unsigned int keylen;
keylen = strlen(txtname);
isc_buffer_init(&buf, txtname, keylen, ISC_BUFFERTYPE_TEXT);
isc_buffer_add(&buf, keylen);
dns_fixedname_init(&fixname);
result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
dns_rootname, ISC_FALSE, NULL);
if (result != ISC_R_SUCCESS) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
DNS_LOGMODULE_ACL, ISC_LOG_WARNING,
"key name \"%s\" is not a valid domain name",
txtname);
return (result);
}
return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
}
isc_result_t
dns_acl_fromconfig(dns_c_ipmatchlist_t *caml,
dns_c_ctx_t *cctx,
dns_aclconfctx_t *ctx,
isc_mem_t *mctx,
dns_acl_t **target)
{
isc_result_t result;
unsigned int count;
dns_acl_t *dacl = NULL;
dns_aclelement_t *de;
dns_c_ipmatchelement_t *ce;
REQUIRE(target != NULL && *target == NULL);
count = 0;
for (ce = ISC_LIST_HEAD(caml->elements);
ce != NULL;
ce = ISC_LIST_NEXT(ce, next))
count++;
dacl = isc_mem_get(mctx, sizeof(*dacl));
if (dacl == NULL)
return (ISC_R_NOMEMORY);
dacl->mctx = mctx;
dacl->name = NULL;
dacl->refcount = 1;
dacl->elements = NULL;
dacl->alloc = 0;
dacl->length = 0;
ISC_LINK_INIT(dacl, nextincache);
/* Must set magic early because we use dns_acl_detach() to clean up. */
dacl->magic = DNS_ACL_MAGIC;
dacl->elements = isc_mem_get(mctx, count * sizeof(dns_aclelement_t));
if (dacl->elements == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
dacl->alloc = count;
memset(dacl->elements, 0, count * sizeof(dns_aclelement_t));
de = dacl->elements;
for (ce = ISC_LIST_HEAD(caml->elements);
ce != NULL;
ce = ISC_LIST_NEXT(ce, next))
{
de->negative = dns_c_ipmatchelement_isneg(ce);
switch (ce->type) {
case dns_c_ipmatch_pattern:
de->type = dns_aclelementtype_ipprefix;
de->u.ip_prefix.address = ce->u.direct.address;
/* XXX "mask" is a misnomer */
de->u.ip_prefix.prefixlen = ce->u.direct.mask;
break;
case dns_c_ipmatch_key:
de->type = dns_aclelementtype_keyname;
dns_name_init(&de->u.keyname, NULL);
result = convert_keyname(ce->u.key, mctx, &de->u.keyname);
if (result != ISC_R_SUCCESS)
goto cleanup;
break;
case dns_c_ipmatch_indirect:
de->type = dns_aclelementtype_nestedacl;
result = dns_acl_fromconfig(ce->u.indirect.list, cctx,
ctx, mctx, &de->u.nestedacl);
if (result != ISC_R_SUCCESS)
goto cleanup;
break;
case dns_c_ipmatch_localhost:
de->type = dns_aclelementtype_localhost;
break;
case dns_c_ipmatch_localnets:
de->type = dns_aclelementtype_localnets;
break;
case dns_c_ipmatch_acl:
de->type = dns_aclelementtype_nestedacl;
result = convert_named_acl(ce->u.aclname, cctx,
ctx, mctx, &de->u.nestedacl);
if (result != ISC_R_SUCCESS)
goto cleanup;
break;
default:
isc_log_write(dns_lctx, DNS_LOGCATEGORY_SECURITY,
DNS_LOGMODULE_ACL, ISC_LOG_WARNING,
"address match list contains "
"unsupported element type");
result = ISC_R_FAILURE;
goto cleanup;
}
de++;
dacl->length++;
}
*target = dacl;
return (ISC_R_SUCCESS);
cleanup:
dns_acl_detach(&dacl);
return (result);
}
......@@ -25,7 +25,7 @@
#include <isc/timer.h>
#include <isc/util.h>
#include <dns/aml.h>
#include <dns/acl.h>
#include <dns/dispatch.h>
#include <dns/events.h>
#include <dns/message.h>
......@@ -39,6 +39,7 @@
#include <named/client.h>
#include <named/log.h>
#include <named/query.h>
#include <named/server.h>
#include <named/update.h>
#include <named/notify.h>
......@@ -613,16 +614,14 @@ client_request(isc_task_t *task, isc_event_t *event) {
ra = ISC_FALSE;
} else {
ra = ISC_TRUE;
(void) dns_c_ctx_getrecursion(ns_g_confctx, &ra);
if (ra == ISC_TRUE) {
dns_c_ipmatchlist_t *acl = NULL;
if (ns_g_server->recursion == ISC_TRUE) {
/* XXX ACL should be view specific. */
dns_c_ctx_getrecursionacl(ns_g_confctx, &acl);
/* XXX this will log too much too early */
result = dns_aml_checkrequest(client->signer,
result = dns_acl_checkrequest(client->signer,
ns_client_getsockaddr(client),
ns_g_confctx->acls, "recursion",
acl, NULL, ISC_TRUE);
"recursion",
ns_g_server->recursionacl,
NULL, ISC_TRUE);
if (result != DNS_R_SUCCESS)
ra = ISC_FALSE;
}
......
/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#ifndef DNS_ACLCONF_H
#define DNS_ACLCONF_H 1
#include <dns/acl.h>
#include <dns/aclconf.h>
#include <dns/confacl.h>
#include <dns/confctx.h>
#include <dns/confip.h>
typedef struct {
ISC_LIST(dns_acl_t) named_acl_cache;
} dns_aclconfctx_t;
/***
*** Functions
***/
ISC_LANG_BEGINDECLS
void dns_aclconfctx_init(dns_aclconfctx_t *ctx);
void dns_aclconfctx_destroy(dns_aclconfctx_t *ctx);
isc_result_t
dns_acl_fromconfig(dns_c_ipmatchlist_t *caml,
dns_c_ctx_t *cctx,
dns_aclconfctx_t *ctx,
isc_mem_t *mctx,
dns_acl_t **target);
ISC_LANG_ENDDECLS
#endif /* DNS_ACLCONF_H */
......@@ -25,8 +25,8 @@
#include <dns/types.h>
#include <dns/confctx.h>
#include <named/types.h>
#include <named/interfacemgr.h>
#include <named/types.h>
#undef EXTERN
#undef INIT
......@@ -57,6 +57,8 @@ EXTERN dns_zonemgr_t * ns_g_zonemgr INIT(NULL);
EXTERN dns_viewlist_t ns_g_viewlist;
EXTERN isc_rwlock_t ns_g_viewlock;
EXTERN ns_server_t * ns_g_server INIT(NULL);
/*
* Default root nameserver hints.
*/
......
......@@ -21,8 +21,45 @@
#include <isc/types.h>
#include <isc/log.h>
#include <dns/types.h>
/*
* Name server state. Better here than in lots of separate global variables.
*/
struct ns_server {
isc_uint32_t magic;
isc_mem_t * mctx;
/* Configurable data. */
isc_boolean_t recursion;
isc_boolean_t auth_nxdomain;
dns_transfer_format_t transfer_format;
dns_acl_t * queryacl;
dns_acl_t * recursionacl;
dns_acl_t * transferacl;
};
#define NS_SERVER_MAGIC 0x53564552 /* SVER */
#define NS_SERVER_VALID(s) ((s) != NULL && \
(s)->magic == NS_SERVER_MAGIC)
isc_result_t
ns_server_init(void);
ns_server_create(isc_mem_t *mctx, ns_server_t **serverp);
/*
* Create a server object with default settings.
*/
void
ns_server_destroy(ns_server_t **serverp);
/*
* Destroy a server object, freeing its memory.
*/
isc_result_t ns_server_init(void);
/*
* Create the singleton names server object of BIND 9.
*/
void
ns_server_fatal(isc_logmodule_t *module, isc_boolean_t want_core,
......
......@@ -25,5 +25,6 @@
typedef struct ns_client ns_client_t;
typedef struct ns_clientmgr ns_clientmgr_t;
typedef struct ns_query ns_query_t;
typedef struct ns_server ns_server_t;
#endif /* NS_TYPES_H */
/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#ifndef NS_ZONECONF_H
#define NS_ZONECONF_H 1
#include <isc/types.h>
#include <dns/acl.h>
#include <dns/confacl.h>
#include <dns/confip.h>
/*
* Create a dns_acl_t from the corresponding configuration data structure,
* 'caml'. References to named ACLs in caml are resolved against the ACL
* table in 'cctx'.
*/
dns_result_t dns_zone_configure(isc_log_t *lctx, dns_c_ctx_t *ctx,
dns_aclconfctx_t *ac,
dns_c_zone_t *czone, dns_zone_t *zone);
/*
* Configure or reconfigure a zone according to the named.conf
* data in 'ctx' and 'czone'.
*
* Require:
* 'lctx' to be initalised or NULL.
* 'ctx' to be initalised or NULL.
* 'ac' to point to an initialized ns_aclconfctx_t.
* 'czone' to be initalised.
* 'zone' to be initalised.
*/
ISC_LANG_ENDDECLS
#endif /* NS_ZONECONF_H */
......@@ -28,7 +28,6 @@
#include <isc/result.h>
#include <isc/taskpool.h>
#include <dns/aml.h>
#include <dns/confip.h>
#include <dns/db.h>
#include <dns/dbiterator.h>
......
......@@ -29,7 +29,7 @@
#include <isc/util.h>
#include <dns/a6.h>
#include <dns/aml.h>
#include <dns/acl.h>
#include <dns/db.h>
#include <dns/dbtable.h>
#include <dns/dispatch.h>
......@@ -49,9 +49,12 @@
#include <named/client.h>
#include <named/globals.h>
#include <named/log.h>
#include <named/query.h>
#include <named/server.h>
#include <named/xfrout.h>
#include <named/log.h>
#include "../../isc/util.h" /* XXX */
#define PARTIALANSWER(c) (((c)->query.attributes & \
NS_QUERYATTR_PARTIALANSWER) != 0)
......@@ -1693,7 +1696,6 @@ query_find(ns_client_t *client, dns_fetchevent_t *event) {
dns_fixedname_t fixed;
dns_dbversion_t *version;
dns_zone_t *zone;
dns_c_ipmatchlist_t *queryacl;
/*
* One-time initialization.
......@@ -1817,18 +1819,17 @@ query_find(ns_client_t *client, dns_fetchevent_t *event) {
version = NULL;
/*
* Check the query against the "allow-query" AML.
* Check the query against the "allow-query" AMLs.
* XXX there should also be a per-view one.
*/
if (is_zone) {
queryacl = dns_zone_getqueryacl(zone);
} else {
queryacl = NULL;
(void) dns_c_ctx_getqueryacl(ns_g_confctx, &queryacl);
}
result = dns_aml_checkrequest(client->signer,
result = dns_acl_checkrequest(client->signer,
ns_client_getsockaddr(client),
ns_g_confctx->acls, "query",
queryacl, NULL, ISC_TRUE);
"query",
(is_zone ?
dns_zone_getqueryacl(zone) :
ns_g_server->queryacl),
ns_g_server->queryacl,
ISC_TRUE);
if (result != DNS_R_SUCCESS) {
QUERY_ERROR(result);
goto cleanup;
......@@ -2485,14 +2486,10 @@ query_find(ns_client_t *client, dns_fetchevent_t *event) {
* auth-nxdomain config option says so, then send the
* response.
*/
if (client->message->rcode == dns_rcode_nxdomain) {
/* Note: default was "true" in BIND 8. */
isc_boolean_t auth_nxdomain = ISC_FALSE;
(void) dns_c_ctx_getauth_nx_domain(ns_g_confctx,
&auth_nxdomain);
if (auth_nxdomain == ISC_TRUE)
client->message->flags |= DNS_MESSAGEFLAG_AA;
}
if (client->message->rcode == dns_rcode_nxdomain &&
ns_g_server->auth_nxdomain == ISC_TRUE)
client->message->flags |= DNS_MESSAGEFLAG_AA;
ns_client_send(client);
}
}
......
......@@ -36,6 +36,7 @@
#include <isc/dir.h>
#include <isc/util.h>
#include <dns/aclconf.h>
#include <dns/cache.h>
#include <dns/confparser.h>
#include <dns/types.h>
......@@ -54,6 +55,7 @@
#include <dns/journal.h>
#include <dns/view.h>
#include <dns/zone.h>
#include <dns/zoneconf.h>
#include <dns/tsig.h>
#include <dns/tkey.h>
......@@ -66,6 +68,7 @@
typedef struct {
isc_mem_t * mctx;
dns_viewlist_t viewlist;
dns_aclconfctx_t *aclconf;
} ns_load_t;
static isc_task_t * server_task;
......@@ -305,7 +308,7 @@ load_zone(dns_c_ctx_t *ctx, dns_c_zone_t *czone, dns_c_view_t *cview,
result = dns_zone_create(&zone, lctx->mctx);
if (result != ISC_R_SUCCESS)
return (result);
result = dns_zone_copy(ns_g_lctx, ctx, czone, zone);
result = dns_zone_configure(ns_g_lctx, ctx, lctx->aclconf, czone, zone);
if (result != ISC_R_SUCCESS)
return (result);
......@@ -371,16 +374,39 @@ load_zone(dns_c_ctx_t *ctx, dns_c_zone_t *czone, dns_c_view_t *cview,
return (result);
}
/* XXX will need error recovery for reconfig */
static void
load_configuration(const char *filename) {
configure_server_acl(dns_c_ctx_t *cctx, dns_aclconfctx_t *actx, isc_mem_t *mctx,
isc_result_t (*getcacl)(dns_c_ctx_t *, dns_c_ipmatchlist_t **),
dns_acl_t **aclp)
{
isc_result_t result;
dns_c_ipmatchlist_t *cacl = NULL;
if (*aclp != NULL)
dns_acl_detach(aclp);
(void) (*getcacl)(cctx, &cacl);
if (cacl != NULL) {
result = dns_acl_fromconfig(cacl, cctx, actx, mctx, aclp);
if (result != DNS_R_SUCCESS)
ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
"server ACL setup failed");
}
}
static void
load_configuration(const char *filename, ns_server_t *server) {
isc_result_t result;
ns_load_t lctx;
dns_c_cbks_t callbacks;
dns_c_ctx_t *configctx, *oconfigctx;
dns_view_t *view, *view_next;
dns_viewlist_t oviewlist;
dns_aclconfctx_t aclconfctx;
dns_aclconfctx_init(&aclconfctx);
lctx.mctx = ns_g_mctx;
lctx.aclconf = &aclconfctx;
ISC_LIST_INIT(lctx.viewlist);
callbacks.zonecbk = load_zone;
......@@ -407,7 +433,23 @@ load_configuration(const char *filename) {
ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
"load of '%s' failed", filename);
}
/*
* Configure various server options.
*/
(void) dns_c_ctx_getrecursion(configctx, &server->recursion);
(void) dns_c_ctx_getauth_nx_domain(configctx, &server->auth_nxdomain);
(void) dns_c_ctx_gettransferformat(configctx, &server->transfer_format);
configure_server_acl(configctx, &aclconfctx, ns_g_mctx,
dns_c_ctx_getqueryacl, &server->queryacl);
configure_server_acl(configctx, &aclconfctx, ns_g_mctx,
dns_c_ctx_getrecursionacl, &server->recursionacl);
configure_server_acl(configctx, &aclconfctx, ns_g_mctx,
dns_c_ctx_gettransferacl, &server->transferacl);
/*
* If we haven't created any views, create a default view for class
* IN. (We're a caching-only server.)
......@@ -518,16 +560,18 @@ load_configuration(const char *filename) {
"dns_tkey_init() failed: %s",
isc_result_totext(result));
}
dns_aclconfctx_destroy(&aclconfctx);
}
static void
run_server(isc_task_t *task, isc_event_t *event) {
ns_server_t *server = (ns_server_t *) event->arg;
(void)task;
isc_event_free(&event);
load_configuration(ns_g_conffile);
load_configuration(ns_g_conffile, server);
ns_interfacemgr_scan(ns_g_interfacemgr);
</