server.c revision 58eb053ed266c0d2fa8d6edc10b39a515afeba6b
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer/*
e20788e1216ed720aefa84f3295f7899d9f28c22Mark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews *
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * copyright notice and this permission notice appear in all copies.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer *
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer */
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrews/* $Id: server.c,v 1.294 2001/03/05 17:20:48 halley Exp $ */
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <config.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <stdlib.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <isc/app.h>
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer#include <isc/base64.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/dir.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/entropy.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/file.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/lex.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/print.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/resource.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/stdio.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <isc/string.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/task.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <isc/timer.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isc/util.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isccfg/cfg.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <isccfg/check.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/cache.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/confparser.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/db.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/dispatch.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/forward.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/journal.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/keytable.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/master.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <dns/peer.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/rdataclass.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <dns/rdatastruct.h>
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews#include <dns/resolver.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <dns/rootns.h>
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrews#include <dns/stats.h>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer#include <dns/tkey.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <dns/view.h>
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrews#include <dns/zone.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <dns/zt.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <dst/dst.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <named/client.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <named/config.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <named/interfacemgr.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <named/log.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <named/logconf.h>
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews#include <named/lwresd.h>
e7c38ca9635e73c9a928bbab9c73c2abbd499f8bEvan Hunt#include <named/omapi.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <named/os.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <named/server.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <named/tkeyconf.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <named/tsigconf.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#include <named/zoneconf.h>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence/*
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * Check an operation for failure. Assumes that the function
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * using it has a 'result' variable and a 'cleanup' label.
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence */
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#define CHECK(op) \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence do { result = (op); \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence if (result != ISC_R_SUCCESS) goto cleanup; \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence } while (0)
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#define CHECKM(op, msg) \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence do { result = (op); \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence if (result != ISC_R_SUCCESS) { \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence isc_log_write(ns_g_lctx, \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence NS_LOGCATEGORY_GENERAL, \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence NS_LOGMODULE_SERVER, \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence ISC_LOG_ERROR, \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence "%s: %s", msg, \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence isc_result_totext(result)); \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence goto cleanup; \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence } \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence } while (0) \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#define CHECKFATAL(op, msg) \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence do { result = (op); \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence if (result != ISC_R_SUCCESS) \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence fatal(msg, result); \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence } while (0) \
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic void
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencefatal(const char *msg, isc_result_t result);
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic void
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencens_server_reload(isc_task_t *task, isc_event_t *event);
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic isc_result_t
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencens_listenelt_fromconfig(cfg_obj_t *listener, cfg_obj_t *config,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence ns_aclconfctx_t *actx,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence isc_mem_t *mctx, ns_listenelt_t **target);
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic isc_result_t
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencens_listenlist_fromconfig(cfg_obj_t *listenlist, cfg_obj_t *config,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence ns_aclconfctx_t *actx,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence isc_mem_t *mctx, ns_listenlist_t **target);
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic isc_result_t
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceconfigure_forward(cfg_obj_t *config, cfg_obj_t *zconfig, cfg_obj_t *vconfig,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence dns_view_t *view, dns_name_t *origin,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence cfg_obj_t *forwarders);
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencestatic isc_result_t
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrenceconfigure_zone(cfg_obj_t *config, cfg_obj_t *zconfig, cfg_obj_t *vconfig,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence isc_mem_t *mctx, dns_view_t *view,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence ns_aclconfctx_t *aclconf);
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer/*
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * Configure a single view ACL at '*aclp'. Get its configuration by
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * calling 'getvcacl' (for per-view configuration) and maybe 'getscacl'
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews * (for a global default).
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews */
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrewsstatic isc_result_t
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrewsconfigure_view_acl(cfg_obj_t *vconfig, cfg_obj_t *config,
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews const char *aclname, ns_aclconfctx_t *actx,
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews isc_mem_t *mctx, dns_acl_t **aclp)
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews{
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews isc_result_t result;
ecffc3aae3e72c5c65b1911ec3f4e1d180dc4bbcMark Andrews cfg_obj_t *maps[3];
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt cfg_obj_t *aclobj = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt int i = 0;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (vconfig != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt maps[i++] = cfg_tuple_get(vconfig, "options");
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt if (config != NULL) {
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt cfg_obj_t *options = NULL;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt cfg_map_get(config, "options", &options);
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt if (options != NULL)
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt maps[i++] = options;
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt }
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt maps[i] = NULL;
07555e64d9102eae058efd58f872b4a3b9ddff61Mark Andrews
578f588228f5e04ccf648b6ae596f396ad6a22c9Mark Andrews result = ns_config_get(maps, aclname, &aclobj);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (aclobj == NULL)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer /*
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * No value available. *aclp == NULL.
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer return (ISC_R_SUCCESS);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer result = ns_acl_fromconfig(aclobj, config, actx, mctx, aclp);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer return (result);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer}
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayerstatic isc_result_t
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayerconfigure_view_dnsseckey(cfg_obj_t *vconfig, cfg_obj_t *key,
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dns_keytable_t *keytable, isc_mem_t *mctx)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer{
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dns_rdataclass_t viewclass;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dns_rdata_key_t keystruct;
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer isc_uint32_t flags, proto, alg;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer char *keystr, *keynamestr;
a687db7ce86c97d884b7ee1a68f59fcddf4d1c2aAutomatic Updater unsigned char keydata[4096];
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_buffer_t keydatabuf;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer unsigned char rrdata[4096];
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_buffer_t rrdatabuf;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_region_t r;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dns_fixedname_t fkeyname;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dns_name_t *keyname;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_buffer_t namebuf;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_result_t result;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dst_key_t *dstkey = NULL;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence flags = cfg_obj_asuint32(cfg_tuple_get(key, "flags"));
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence proto = cfg_obj_asuint32(cfg_tuple_get(key, "protocol"));
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence alg = cfg_obj_asuint32(cfg_tuple_get(key, "algorithm"));
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keyname = dns_fixedname_name(&fkeyname);
4d6329c1b3c80f82880291ae35f68f84c14ad820Francis Dupont keynamestr = cfg_obj_asstring(cfg_tuple_get(key, "name"));
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
4d6329c1b3c80f82880291ae35f68f84c14ad820Francis Dupont if (vconfig == NULL)
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrews viewclass = dns_rdataclass_in;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer else {
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrews cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer CHECK(ns_config_getclass(classobj, &viewclass));
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer }
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.common.rdclass = viewclass;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.common.rdtype = dns_rdatatype_key;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer /*
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * The key data in keystruct is not dynamically allocated.
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.mctx = NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews ISC_LINK_INIT(&keystruct.common, link);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews if (flags > 0xffff)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer CHECKM(ISC_R_RANGE, "key flags");
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (proto > 0xff)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer CHECKM(ISC_R_RANGE, "key protocol");
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (alg > 0xff)
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews CHECKM(ISC_R_RANGE, "key algorithm");
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.flags = (isc_uint16_t)flags;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.protocol = (isc_uint8_t)proto;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.algorithm = (isc_uint8_t)alg;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c213258356638f9af1e1c68425df527b856567a2Francis Dupont isc_buffer_init(&keydatabuf, keydata, sizeof(keydata));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer CHECK(isc_base64_decodestring(mctx, keystr, &keydatabuf));
6fa7a247a1037be08d7a8e0b4c4fd3785b2f268cAndreas Gustafsson isc_buffer_usedregion(&keydatabuf, &r);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.datalen = r.length;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.data = r.base;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer CHECK(dns_rdata_fromstruct(NULL,
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keystruct.common.rdclass,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence keystruct.common.rdtype,
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence &keystruct, &rrdatabuf));
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence dns_fixedname_init(&fkeyname);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_buffer_init(&namebuf, keynamestr, strlen(keynamestr));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews isc_buffer_add(&namebuf, strlen(keynamestr));
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer CHECK(dns_name_fromtext(keyname, &namebuf,
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dns_rootname, ISC_FALSE,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews NULL));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews mctx, &dstkey));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews CHECK(dns_keytable_add(keytable, &dstkey));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews INSIST(dstkey == NULL);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews return (ISC_R_SUCCESS);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer cleanup:
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer "configuring trusted key for '%s': %s",
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keynamestr, isc_result_totext(result));
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer result = ISC_R_FAILURE;
a687db7ce86c97d884b7ee1a68f59fcddf4d1c2aAutomatic Updater
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (dstkey != NULL)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dst_key_free(&dstkey);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer return (result);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer}
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer/*
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * Configure DNSSEC keys for a view. Currently used only for
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * the security roots.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer *
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * The per-view configuration values and the server-global defaults are read
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * from 'vconfig' and 'config'. The variable to be configured is '*target'.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayerstatic isc_result_t
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayerconfigure_view_dnsseckeys(cfg_obj_t *vconfig, cfg_obj_t *config,
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_mem_t *mctx, dns_keytable_t **target)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer{
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer isc_result_t result;
90039392500d15bf056ab4505d048fcba0d485dfMark Andrews cfg_obj_t *keys = NULL;
b39213ce599dc433c6f99144c97ca3d562f40344Mark Andrews cfg_obj_t *voptions = NULL;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer cfg_listelt_t *element, *element2;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer cfg_obj_t *keylist;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer cfg_obj_t *key;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer dns_keytable_t *keytable = NULL;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer CHECK(dns_keytable_create(mctx, &keytable));
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (vconfig != NULL)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer voptions = cfg_tuple_get(vconfig, "options");
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer keys = NULL;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (voptions != NULL)
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews (void)cfg_map_get(voptions, "trusted-keys", &keys);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer if (keys == NULL)
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews (void)cfg_map_get(config, "trusted-keys", &keys);
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer
22d44896ad30af27f7b483a806d1452daf5451c2Mark Andrews for (element = cfg_list_first(keys);
22d44896ad30af27f7b483a806d1452daf5451c2Mark Andrews element != NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews element = cfg_list_next(element))
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews {
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews keylist = cfg_listelt_value(element);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews for (element2 = cfg_list_first(keylist);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews element2 != NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews element2 = cfg_list_next(element2))
22d44896ad30af27f7b483a806d1452daf5451c2Mark Andrews {
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews key = cfg_listelt_value(element2);
aeff7de836558fa8002ab5db35292d2bb6450da8Evan Hunt CHECK(configure_view_dnsseckey(vconfig, key,
aeff7de836558fa8002ab5db35292d2bb6450da8Evan Hunt keytable, mctx));
aeff7de836558fa8002ab5db35292d2bb6450da8Evan Hunt }
aeff7de836558fa8002ab5db35292d2bb6450da8Evan Hunt }
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
e7c38ca9635e73c9a928bbab9c73c2abbd499f8bEvan Hunt dns_keytable_detach(target);
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews *target = keytable; /* Transfer ownership. */
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews keytable = NULL;
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews result = ISC_R_SUCCESS;
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews cleanup:
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews return (result);
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews}
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt/*
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt * Get a dispatch appropriate for the resolver of a given view.
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt */
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Huntstatic isc_result_t
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Huntget_view_querysource_dispatch(cfg_obj_t **maps,
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt int af, dns_dispatch_t **dispatchp)
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt{
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt isc_result_t result;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt dns_dispatch_t *disp;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews isc_sockaddr_t sa;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews unsigned int attrs, attrmask;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews cfg_obj_t *obj = NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews /*
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * Make compiler happy.
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews */
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews result = ISC_R_FAILURE;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews switch (af) {
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews case AF_INET:
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews result = ns_config_get(maps, "query-source", &obj);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews INSIST(result == ISC_R_SUCCESS);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews break;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews case AF_INET6:
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews result = ns_config_get(maps, "query-source-v6", &obj);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews INSIST(result == ISC_R_SUCCESS);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews break;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews default:
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews INSIST(0);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews }
891b61c3909afdae7e5d7ba14cd12d16186f4a68Mark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews sa = *(cfg_obj_assockaddr(obj));
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews INSIST(isc_sockaddr_pf(&sa) == af);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * If we don't support this address family, we're done!
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt switch (af) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt case AF_INET:
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt result = isc_net_probeipv4();
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt break;
12bf5d4796505b4c20680531da96a31e6c2c1144Evan Hunt case AF_INET6:
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt result = isc_net_probeipv6();
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt break;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt default:
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt INSIST(0);
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt }
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt if (result != ISC_R_SUCCESS)
fc63119c8b7aa8827fad9e3e45e50c69bc2630e8Francis Dupont return (ISC_R_SUCCESS);
fc63119c8b7aa8827fad9e3e45e50c69bc2630e8Francis Dupont
fc63119c8b7aa8827fad9e3e45e50c69bc2630e8Francis Dupont /*
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt * Try to find a dispatcher that we can share.
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt */
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt attrs = 0;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt attrs |= DNS_DISPATCHATTR_UDP;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt switch (af) {
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt case AF_INET:
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt attrs |= DNS_DISPATCHATTR_IPV4;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt break;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt case AF_INET6:
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews attrs |= DNS_DISPATCHATTR_IPV6;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews break;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews }
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews attrmask = 0;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews attrmask |= DNS_DISPATCHATTR_UDP;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews attrmask |= DNS_DISPATCHATTR_TCP;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews attrmask |= DNS_DISPATCHATTR_IPV4;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews attrmask |= DNS_DISPATCHATTR_IPV6;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews disp = NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews ns_g_taskmgr, &sa, 4096,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews 1000, 32768, 16411, 16433,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews attrs, attrmask, &disp);
891b61c3909afdae7e5d7ba14cd12d16186f4a68Mark Andrews if (result != ISC_R_SUCCESS) {
891b61c3909afdae7e5d7ba14cd12d16186f4a68Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
891b61c3909afdae7e5d7ba14cd12d16186f4a68Mark Andrews "could not get query source dispatcher");
891b61c3909afdae7e5d7ba14cd12d16186f4a68Mark Andrews return (result);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews }
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews *dispatchp = disp;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews return (ISC_R_SUCCESS);
98091cb21da79b0c7fd329fd64497dcb03402467Evan Hunt}
98091cb21da79b0c7fd329fd64497dcb03402467Evan Hunt
98091cb21da79b0c7fd329fd64497dcb03402467Evan Huntstatic isc_result_t
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Huntconfigure_peer(cfg_obj_t *cpeer, isc_mem_t *mctx, dns_peer_t **peerp) {
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt isc_sockaddr_t *sa;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt isc_netaddr_t na;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt dns_peer_t *peer;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt cfg_obj_t *obj;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt char *str;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt isc_result_t result;
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt sa = cfg_obj_assockaddr(cfg_map_getname(cpeer));
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt isc_netaddr_fromsockaddr(&na, sa);
98922b2b2b024dcca25be7c220cf3b16b1e6c4b5Evan Hunt
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews peer = NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews result = dns_peer_new(mctx, &na, &peer);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews if (result != ISC_R_SUCCESS)
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews return (result);
2dd959aa18d0f661efb0d9646963993a4773d036Evan Hunt
2dd959aa18d0f661efb0d9646963993a4773d036Evan Hunt obj = NULL;
2dd959aa18d0f661efb0d9646963993a4773d036Evan Hunt (void)cfg_map_get(cpeer, "bogus", &obj);
if (obj != NULL)
dns_peer_setbogus(peer, cfg_obj_asboolean(obj));
obj = NULL;
(void)cfg_map_get(cpeer, "provide-ixfr", &obj);
if (obj != NULL)
dns_peer_setprovideixfr(peer, cfg_obj_asboolean(obj));
obj = NULL;
(void)cfg_map_get(cpeer, "request-ixfr", &obj);
if (obj != NULL)
dns_peer_setrequestixfr(peer, cfg_obj_asboolean(obj));
obj = NULL;
(void)cfg_map_get(cpeer, "transfers", &obj);
if (obj != NULL)
dns_peer_settransfers(peer, cfg_obj_asuint32(obj));
obj = NULL;
(void)cfg_map_get(cpeer, "transfer-format", &obj);
if (obj != NULL) {
str = cfg_obj_asstring(obj);
if (strcasecmp(str, "many-answers") == 0)
dns_peer_settransferformat(peer, dns_many_answers);
else if (strcasecmp(str, "one-answer") == 0)
dns_peer_settransferformat(peer, dns_one_answer);
else
INSIST(0);
}
obj = NULL;
(void)cfg_map_get(cpeer, "keys", &obj);
if (obj != NULL) {
result = dns_peer_setkeybycharp(peer, cfg_obj_asstring(obj));
if (result != ISC_R_SUCCESS)
goto cleanup;
}
*peerp = peer;
return (ISC_R_SUCCESS);
cleanup:
dns_peer_detach(&peer);
return (result);
}
/*
* Configure 'view' according to 'vconfig', taking defaults from 'config'
* where values are missing in 'vconfig'.
*
* When configuring the default view, 'vconfig' will be NULL and the
* global defaults in 'config' used exclusively.
*/
static isc_result_t
configure_view(dns_view_t *view, cfg_obj_t *config, cfg_obj_t *vconfig,
isc_mem_t *mctx, ns_aclconfctx_t *actx)
{
cfg_obj_t *maps[4];
cfg_obj_t *cfgmaps[3];
cfg_obj_t *options = NULL;
cfg_obj_t *voptions = NULL;
cfg_obj_t *forwarders;
cfg_obj_t *zonelist;
cfg_obj_t *obj;
cfg_listelt_t *element;
in_port_t port;
dns_cache_t *cache = NULL;
isc_result_t result;
isc_uint32_t max_cache_size;
isc_uint32_t lame_ttl;
dns_tsig_keyring_t *ring;
dns_view_t *pview = NULL; /* Production view */
isc_mem_t *cmctx;
dns_dispatch_t *dispatch4 = NULL;
dns_dispatch_t *dispatch6 = NULL;
isc_boolean_t reused_cache = ISC_FALSE;
int i;
char *str;
REQUIRE(DNS_VIEW_VALID(view));
cmctx = NULL;
if (config != NULL)
cfg_map_get(config, "options", &options);
i = 0;
if (vconfig != NULL) {
voptions = cfg_tuple_get(vconfig, "options");
maps[i++] = voptions;
}
if (options != NULL)
maps[i++] = options;
maps[i++] = ns_g_defaults;
maps[i] = NULL;
i = 0;
if (voptions != NULL)
cfgmaps[i++] = voptions;
if (config != NULL)
cfgmaps[i++] = config;
cfgmaps[i] = NULL;
/*
* Set the view's port number for outgoing queries.
*/
CHECKM(ns_config_getport(config, &port), "port");
dns_view_setdstport(view, port);
/*
* Configure the zones.
*/
zonelist = NULL;
if (voptions != NULL)
(void)cfg_map_get(voptions, "zone", &zonelist);
else
(void)cfg_map_get(config, "zone", &zonelist);
for (element = cfg_list_first(zonelist);
element != NULL;
element = cfg_list_next(element))
{
cfg_obj_t *zconfig = cfg_listelt_value(element);
CHECK(configure_zone(config, zconfig, vconfig, mctx, view,
actx));
}
/*
* Configure the view's cache. Try to reuse an existing
* cache if possible, otherwise create a new cache.
* Note that the ADB is not preserved in either case.
*
* XXX Determining when it is safe to reuse a cache is
* tricky. When the view's configuration changes, the cached
* data may become invalid because it reflects our old
* view of the world. As more view attributes become
* configurable, we will have to add code here to check
* whether they have changed in ways that could
* invalidate the cache.
*/
result = dns_viewlist_find(&ns_g_server->viewlist,
view->name, view->rdclass,
&pview);
if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
goto cleanup;
if (pview != NULL) {
INSIST(pview->cache != NULL);
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(3),
"reusing existing cache");
reused_cache = ISC_TRUE;
dns_cache_attach(pview->cache, &cache);
dns_view_detach(&pview);
} else {
CHECK(isc_mem_create(0, 0, &cmctx));
CHECK(dns_cache_create(cmctx, ns_g_taskmgr, ns_g_timermgr,
view->rdclass, "rbt", 0, NULL, &cache));
}
dns_view_setcache(view, cache);
/*
* cache-file cannot be inherited if views are present, but this
* should be caught by the configuration checking stage.
*/
obj = NULL;
result = ns_config_get(maps, "cache-file", &obj);
if (result == ISC_R_SUCCESS) {
dns_cache_setfilename(cache, cfg_obj_asstring(obj));
if (!reused_cache)
CHECK(dns_cache_load(cache));
}
obj = NULL;
result = ns_config_get(maps, "cleaning-interval", &obj);
INSIST(result == ISC_R_SUCCESS);
dns_cache_setcleaninginterval(cache, cfg_obj_asuint32(obj));
obj = NULL;
result = ns_config_get(maps, "max-cache-size", &obj);
INSIST(result == ISC_R_SUCCESS);
if (cfg_obj_isstring(obj)) {
str = cfg_obj_asstring(obj);
INSIST(strcasecmp(str, "unlimited") == 0);
max_cache_size = ISC_UINT32_MAX;
} else {
isc_resourcevalue_t value;
value = cfg_obj_asuint64(obj);
if (value > ISC_UINT32_MAX) {
cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
"'max-cache-size "
"%" ISC_PRINT_QUADFORMAT "d' is too large",
value);
result = ISC_R_RANGE;
goto cleanup;
}
max_cache_size = (isc_uint32_t)value;
}
/*
* XXX remove once rbt is fixed
*/
max_cache_size = 0;
dns_cache_setcachesize(cache, max_cache_size);
dns_cache_detach(&cache);
/*
* Resolver.
*
* XXXRTH Hardwired number of tasks.
*/
CHECK(get_view_querysource_dispatch(maps, AF_INET, &dispatch4));
CHECK(get_view_querysource_dispatch(maps, AF_INET6, &dispatch6));
CHECK(dns_view_createresolver(view, ns_g_taskmgr, 31,
ns_g_socketmgr, ns_g_timermgr,
0, ns_g_dispatchmgr,
dispatch4, dispatch6));
if (dispatch4 != NULL)
dns_dispatch_detach(&dispatch4);
if (dispatch6 != NULL)
dns_dispatch_detach(&dispatch6);
/*
* Set resolver's lame-ttl.
*/
obj = NULL;
result = ns_config_get(maps, "lame-ttl", &obj);
INSIST(result == ISC_R_SUCCESS);
lame_ttl = cfg_obj_asuint32(obj);
if (lame_ttl > 18000)
lame_ttl = 18000;
dns_resolver_setlamettl(view->resolver, lame_ttl);
/*
* Set resolver forwarding policy.
*/
forwarders = NULL;
(void)ns_config_get(maps, "forwarders", &forwarders);
result = configure_forward(config, NULL, vconfig,
view, dns_rootname, forwarders);
/*
* We have default hints for class IN if we need them.
*/
if (view->rdclass == dns_rdataclass_in && view->hints == NULL)
dns_view_sethints(view, ns_g_server->in_roothints);
/*
* If we still have no hints, this is a non-IN view with no
* "hints zone" configured. Issue a warning, except if this
* is a root server. Root servers never need to consult
* their hints, so it's no point requireing users to configure
* them.
*/
if (view->hints == NULL) {
dns_zone_t *rootzone = NULL;
dns_view_findzone(view, dns_rootname, &rootzone);
if (rootzone != NULL) {
dns_zone_detach(&rootzone);
} else {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
"no root hints for view '%s'",
view->name);
}
}
/*
* Configure the view's TSIG keys.
*/
ring = NULL;
CHECK(ns_tsigkeyring_fromconfig(config, vconfig, view->mctx, &ring));
dns_view_setkeyring(view, ring);
/*
* Configure the view's peer list.
*/
{
cfg_obj_t *peers = NULL;
cfg_listelt_t *element;
dns_peerlist_t *newpeers = NULL;
(void)ns_config_get(cfgmaps, "server", &peers);
CHECK(dns_peerlist_new(mctx, &newpeers));
for (element = cfg_list_first(peers);
element != NULL;
element = cfg_list_next(element))
{
cfg_obj_t *cpeer = cfg_listelt_value(element);
dns_peer_t *peer;
CHECK(configure_peer(cpeer, mctx, &peer));
dns_peerlist_addpeer(newpeers, peer);
dns_peer_detach(&peer);
}
dns_peerlist_detach(&view->peers);
view->peers = newpeers; /* Transfer ownership. */
}
/*
* Configure the "match-clients" ACL.
*/
CHECK(configure_view_acl(vconfig, config, "match-clients", actx,
ns_g_mctx, &view->matchclients));
/*
* Configure other configurable data.
*/
obj = NULL;
result = ns_config_get(maps, "recursion", &obj);
INSIST(result == ISC_R_SUCCESS);
view->recursion = cfg_obj_asboolean(obj);
obj = NULL;
result = ns_config_get(maps, "auth-nxdomain", &obj);
INSIST(result == ISC_R_SUCCESS);
view->auth_nxdomain = cfg_obj_asboolean(obj);
obj = NULL;
result = ns_config_get(maps, "transfer-format", &obj);
INSIST(result == ISC_R_SUCCESS);
str = cfg_obj_asstring(obj);
if (strcasecmp(str, "many-answers") == 0)
view->transfer_format = dns_many_answers;
else if (strcasecmp(str, "one-answer") == 0)
view->transfer_format = dns_one_answer;
else
INSIST(0);
/*
* Set sources where additional data, CNAMEs, and DNAMEs may be found.
*/
obj = NULL;
result = ns_config_get(maps, "additional-from-auth", &obj);
INSIST(result == ISC_R_SUCCESS);
view->additionalfromauth = cfg_obj_asboolean(obj);
obj = NULL;
result = ns_config_get(maps, "additional-from-cache", &obj);
INSIST(result == ISC_R_SUCCESS);
view->additionalfromcache = cfg_obj_asboolean(obj);
CHECK(configure_view_acl(vconfig, config, "allow-query",
actx, ns_g_mctx, &view->queryacl));
CHECK(configure_view_acl(vconfig, config, "allow-recursion",
actx, ns_g_mctx, &view->recursionacl));
CHECK(configure_view_acl(vconfig, config, "allow-v6-synthesis",
actx, ns_g_mctx, &view->v6synthesisacl));
CHECK(configure_view_acl(vconfig, config, "sortlist",
actx, ns_g_mctx, &view->sortlist));
obj = NULL;
result = ns_config_get(maps, "request-ixfr", &obj);
INSIST(result == ISC_R_SUCCESS);
view->requestixfr = cfg_obj_asboolean(obj);
obj = NULL;
result = ns_config_get(maps, "provide-ixfr", &obj);
INSIST(result == ISC_R_SUCCESS);
view->provideixfr = cfg_obj_asboolean(obj);
/*
* For now, there is only one kind of trusted keys, the
* "security roots".
*/
CHECK(configure_view_dnsseckeys(vconfig, config, mctx,
&view->secroots));
obj = NULL;
result = ns_config_get(maps, "max-cache-ttl", &obj);
INSIST(result == ISC_R_SUCCESS);
view->maxcachettl = cfg_obj_asuint32(obj);
obj = NULL;
result = ns_config_get(maps, "max-ncache-ttl", &obj);
INSIST(result == ISC_R_SUCCESS);
view->maxncachettl = cfg_obj_asuint32(obj);
if (view->maxncachettl > 7 * 24 * 3600)
view->maxncachettl = 7 * 24 * 3600;
result = ISC_R_SUCCESS;
cleanup:
if (cmctx != NULL)
isc_mem_detach(&cmctx);
if (cache != NULL)
dns_cache_detach(&cache);
return (result);
}
/*
* Create the special view that handles queries under "bind. CH".
*/
static isc_result_t
create_bind_view(dns_view_t **viewp)
{
isc_result_t result;
dns_view_t *view = NULL;
REQUIRE(viewp != NULL && *viewp == NULL);
CHECK(dns_view_create(ns_g_mctx, dns_rdataclass_ch, "_bind", &view));
/* Transfer ownership. */
*viewp = view;
view = NULL;
result = ISC_R_SUCCESS;
cleanup:
if (view != NULL)
dns_view_detach(&view);
return (result);
}
/*
* Create the zone that handles queries for "version.bind. CH". The
* version string returned either from the "version" configuration
* option or the global defaults.
*/
static isc_result_t
create_version_zone(cfg_obj_t **maps, dns_zonemgr_t *zmgr, dns_view_t *view) {
isc_result_t result;
dns_db_t *db = NULL;
dns_zone_t *zone = NULL;
dns_dbversion_t *dbver = NULL;
dns_difftuple_t *tuple = NULL;
dns_diff_t diff;
char *versiontext;
unsigned char buf[256];
isc_region_t r;
size_t len;
dns_rdata_t rdata = DNS_RDATA_INIT;
static unsigned char origindata[] = "\007version\004bind";
dns_name_t origin;
cfg_obj_t *obj = NULL;
dns_diff_init(ns_g_mctx, &diff);
dns_name_init(&origin, NULL);
r.base = origindata;
r.length = sizeof(origindata);
dns_name_fromregion(&origin, &r);
result = ns_config_get(maps, "version", &obj);
INSIST(result == ISC_R_SUCCESS);
versiontext = cfg_obj_asstring(obj);
len = strlen(versiontext);
if (len > 255)
len = 255; /* Silently truncate. */
buf[0] = len;
memcpy(buf + 1, versiontext, len);
r.base = buf;
r.length = 1 + len;
dns_rdata_fromregion(&rdata, dns_rdataclass_ch, dns_rdatatype_txt, &r);
CHECK(dns_zone_create(&zone, ns_g_mctx));
CHECK(dns_zone_setorigin(zone, &origin));
dns_zone_settype(zone, dns_zone_master);
dns_zone_setclass(zone, dns_rdataclass_ch);
dns_zone_setview(zone, view);
CHECK(dns_zonemgr_managezone(zmgr, zone));
CHECK(dns_db_create(ns_g_mctx, "rbt", &origin, dns_dbtype_zone,
dns_rdataclass_ch, 0, NULL, &db));
CHECK(dns_db_newversion(db, &dbver));
CHECK(dns_difftuple_create(ns_g_mctx, DNS_DIFFOP_ADD, &origin,
0, &rdata, &tuple));
dns_diff_append(&diff, &tuple);
CHECK(dns_diff_apply(&diff, db, dbver));
dns_db_closeversion(db, &dbver, ISC_TRUE);
CHECK(dns_zone_replacedb(zone, db, ISC_FALSE));
CHECK(dns_view_addzone(view, zone));
result = ISC_R_SUCCESS;
cleanup:
if (zone != NULL)
dns_zone_detach(&zone);
if (dbver != NULL)
dns_db_closeversion(db, &dbver, ISC_FALSE);
if (db != NULL)
dns_db_detach(&db);
dns_diff_clear(&diff);
return (result);
}
/*
* Create the special view that handles queries for
* "authors.bind. CH". The strings returned list
* the BIND 9 authors.
*/
static isc_result_t
create_authors_zone(cfg_obj_t *options, dns_zonemgr_t *zmgr, dns_view_t *view)
{
isc_result_t result;
dns_db_t *db = NULL;
dns_zone_t *zone = NULL;
dns_dbversion_t *dbver = NULL;
dns_difftuple_t *tuple;
dns_diff_t diff;
isc_constregion_t r;
isc_constregion_t cr;
dns_rdata_t rdata = DNS_RDATA_INIT;
static const char origindata[] = "\007authors\004bind";
dns_name_t origin;
int i;
static const char *authors[] = {
"\014Mark Andrews",
"\015James Brister",
"\014Ben Cottrell",
"\015Michael Graff",
"\022Andreas Gustafsson",
"\012Bob Halley",
"\016David Lawrence",
"\013Damien Neil",
"\013Matt Nelson",
"\016Michael Sawyer",
"\020Brian Wellington",
NULL,
};
cfg_obj_t *obj = NULL;
/*
* If a version string is specified, disable the authors.bind zone.
*/
if (options != NULL &&
cfg_map_get(options, "version", &obj) == ISC_R_SUCCESS)
return (ISC_R_SUCCESS);
dns_diff_init(ns_g_mctx, &diff);
dns_name_init(&origin, NULL);
r.base = origindata;
r.length = sizeof(origindata);
dns_name_fromregion(&origin, (isc_region_t *)&r);
CHECK(dns_zone_create(&zone, ns_g_mctx));
CHECK(dns_zone_setorigin(zone, &origin));
dns_zone_settype(zone, dns_zone_master);
dns_zone_setclass(zone, dns_rdataclass_ch);
dns_zone_setview(zone, view);
CHECK(dns_zonemgr_managezone(zmgr, zone));
CHECK(dns_db_create(ns_g_mctx, "rbt", &origin, dns_dbtype_zone,
dns_rdataclass_ch, 0, NULL, &db));
CHECK(dns_db_newversion(db, &dbver));
for (i = 0; authors[i] != NULL; i++) {
cr.base = authors[i];
cr.length = strlen(authors[i]);
INSIST(cr.length == ((const unsigned char *)cr.base)[0] + 1U);
dns_rdata_fromregion(&rdata, dns_rdataclass_ch,
dns_rdatatype_txt, (isc_region_t *)&cr);
tuple = NULL;
CHECK(dns_difftuple_create(ns_g_mctx, DNS_DIFFOP_ADD, &origin,
0, &rdata, &tuple));
dns_diff_append(&diff, &tuple);
dns_rdata_reset(&rdata);
}
CHECK(dns_diff_apply(&diff, db, dbver));
dns_db_closeversion(db, &dbver, ISC_TRUE);
CHECK(dns_zone_replacedb(zone, db, ISC_FALSE));
CHECK(dns_view_addzone(view, zone));
result = ISC_R_SUCCESS;
cleanup:
if (zone != NULL)
dns_zone_detach(&zone);
if (dbver != NULL)
dns_db_closeversion(db, &dbver, ISC_FALSE);
if (db != NULL)
dns_db_detach(&db);
dns_diff_clear(&diff);
return (result);
}
static isc_result_t
configure_hints(dns_view_t *view, const char *filename) {
isc_result_t result;
dns_db_t *db;
db = NULL;
result = dns_rootns_create(view->mctx, view->rdclass, filename, &db);
if (result == ISC_R_SUCCESS) {
dns_view_sethints(view, db);
dns_db_detach(&db);
}
return (result);
}
static isc_result_t
configure_forward(cfg_obj_t *config, cfg_obj_t *zconfig, cfg_obj_t *vconfig,
dns_view_t *view, dns_name_t *origin, cfg_obj_t *forwarders)
{
cfg_obj_t *obj;
cfg_obj_t *maps[5];
cfg_obj_t *options;
cfg_obj_t *portobj;
cfg_obj_t *faddresses;
cfg_listelt_t *element;
dns_fwdpolicy_t fwdpolicy;
isc_sockaddrlist_t addresses;
isc_sockaddr_t *sa;
isc_result_t result;
in_port_t port;
unsigned int i;
options = NULL;
if (config != NULL)
(void)cfg_map_get(config, "options", &options);
i = 0;
if (zconfig != NULL)
maps[i++] = cfg_tuple_get(zconfig, "options");
if (vconfig != NULL)
maps[i++] = cfg_tuple_get(vconfig, "options");
if (options != NULL)
maps[i++] = options;
maps[i++] = ns_g_defaults;
maps[i++] = NULL;
/*
* Determine which port to send forwarded requests to.
*/
CHECKM(ns_config_getport(config, &port), "port");
if (forwarders != NULL) {
portobj = cfg_tuple_get(forwarders, "port");
if (cfg_obj_isuint32(portobj)) {
isc_uint32_t val = cfg_obj_asuint32(portobj);
if (val > ISC_UINT16_MAX) {
cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
"port '%u' out of range", val);
return (ISC_R_RANGE);
}
port = (in_port_t) val;
}
}
faddresses = NULL;
if (forwarders != NULL)
faddresses = cfg_tuple_get(forwarders, "addresses");
ISC_LIST_INIT(addresses);
for (element = cfg_list_first(faddresses);
element != NULL;
element = cfg_list_next(element))
{
cfg_obj_t *forwarder = cfg_listelt_value(element);
sa = isc_mem_get(view->mctx, sizeof(isc_sockaddr_t));
if (sa == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
*sa = *cfg_obj_assockaddr(forwarder);
if (isc_sockaddr_getport(sa) == 0)
isc_sockaddr_setport(sa, port);
ISC_LINK_INIT(sa, link);
ISC_LIST_APPEND(addresses, sa, link);
}
if (ISC_LIST_EMPTY(addresses))
fwdpolicy = dns_fwdpolicy_none;
else
fwdpolicy = dns_fwdpolicy_first;
obj = NULL;
result = ns_config_get(maps, "forward", &obj);
if (result == ISC_R_SUCCESS) {
char *forwardstr = cfg_obj_asstring(obj);
if (strcasecmp(forwardstr, "first") == 0)
fwdpolicy = dns_fwdpolicy_first;
else if (strcasecmp(forwardstr, "only") == 0)
fwdpolicy = dns_fwdpolicy_only;
else
INSIST(0);
}
result = dns_fwdtable_add(view->fwdtable, origin, &addresses,
fwdpolicy);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = ISC_R_SUCCESS;
cleanup:
while (!ISC_LIST_EMPTY(addresses)) {
sa = ISC_LIST_HEAD(addresses);
ISC_LIST_UNLINK(addresses, sa, link);
isc_mem_put(view->mctx, sa, sizeof(isc_sockaddr_t));
}
return (result);
}
/*
* Create a new view and add it to the list.
*
* If 'vconfig' is NULL, find or create the default view.
*
* The view found or created is attached to '*viewp'.
*/
static isc_result_t
create_view(cfg_obj_t *vconfig, dns_viewlist_t *viewlist, dns_view_t **viewp) {
isc_result_t result;
const char *viewname;
dns_rdataclass_t viewclass;
dns_view_t *view = NULL;
if (vconfig != NULL) {
cfg_obj_t *classobj = NULL;
viewname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
classobj = cfg_tuple_get(vconfig, "class");
result = ns_config_getclass(classobj, &viewclass);
} else {
viewname = "_default";
viewclass = dns_rdataclass_in;
}
result = dns_viewlist_find(viewlist, viewname, viewclass, &view);
if (result == ISC_R_SUCCESS)
return (ISC_R_EXISTS);
if (result != ISC_R_NOTFOUND)
return (result);
INSIST(view == NULL);
result = dns_view_create(ns_g_mctx, viewclass, viewname, &view);
if (result != ISC_R_SUCCESS)
return (result);
ISC_LIST_APPEND(*viewlist, view, link);
dns_view_attach(view, viewp);
return (ISC_R_SUCCESS);
}
/*
* Configure or reconfigure a zone. This callback function
* is called after parsing each "zone" statement in named.conf.
*/
static isc_result_t
configure_zone(cfg_obj_t *config, cfg_obj_t *zconfig, cfg_obj_t *vconfig,
isc_mem_t *mctx, dns_view_t *view,
ns_aclconfctx_t *aclconf)
{
dns_view_t *pview = NULL; /* Production view */
dns_zone_t *zone = NULL; /* New or reused zone */
dns_zone_t *dupzone = NULL;
cfg_obj_t *options = NULL;
cfg_obj_t *zoptions = NULL;
cfg_obj_t *typeobj = NULL;
cfg_obj_t *forwarders = NULL;
isc_result_t result;
isc_buffer_t buffer;
dns_fixedname_t fixorigin;
dns_name_t *origin;
const char *zname;
dns_rdataclass_t zclass;
const char *ztypestr;
dns_zonetype_t ztype;
options = NULL;
(void)cfg_map_get(config, "options", &options);
zoptions = cfg_tuple_get(zconfig, "options");
/*
* Get the zone origin as a dns_name_t.
*/
zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
isc_buffer_init(&buffer, zname, strlen(zname));
isc_buffer_add(&buffer, strlen(zname));
dns_fixedname_init(&fixorigin);
CHECK(dns_name_fromtext(dns_fixedname_name(&fixorigin),
&buffer, dns_rootname, ISC_FALSE, NULL));
origin = dns_fixedname_name(&fixorigin);
CHECK(ns_config_getclass(cfg_tuple_get(zconfig, "class"), &zclass));
if (zclass != view->rdclass) {
const char *vname = NULL;
if (vconfig != NULL)
vname = cfg_obj_asstring(cfg_tuple_get(vconfig,
"name"));
else
vname = "<default view>";
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
"zone '%s': wrong class for view '%s'",
zname, vname);
result = ISC_R_FAILURE;
goto cleanup;
}
(void)cfg_map_get(zoptions, "type", &typeobj);
if (typeobj == NULL) {
cfg_obj_log(zconfig, ns_g_lctx, ISC_LOG_ERROR,
"zone '%s' 'type' not specified", zname);
return (ISC_R_FAILURE);
}
ztypestr = cfg_obj_asstring(typeobj);
/*
* "hints zones" aren't zones. If we've got one,
* configure it and return.
*/
if (strcasecmp(ztypestr, "hint") == 0) {
cfg_obj_t *fileobj = NULL;
if (cfg_map_get(zoptions, "file", &fileobj) != ISC_R_SUCCESS) {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
"zone '%s': 'file' not specified",
zname);
result = ISC_R_FAILURE;
goto cleanup;
}
if (dns_name_equal(origin, dns_rootname)) {
result = configure_hints(view,
cfg_obj_asstring(fileobj));
} else {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
"ignoring non-root hint zone '%s'",
zname);
result = ISC_R_SUCCESS;
}
goto cleanup;
}
/*
* "forward zones" aren't zones either. Translate this syntax into
* the appropriate selective forwarding configuration and return.
*/
if (strcasecmp(ztypestr, "forward") == 0) {
forwarders = NULL;
cfg_map_get(zoptions, "forwarders", &forwarders);
result = configure_forward(config, zconfig, vconfig,
view, origin, forwarders);
goto cleanup;
}
ztype = ns_config_getzonetype(typeobj);
/*
* Check for duplicates in the new zone table.
*/
result = dns_view_findzone(view, origin, &dupzone);
if (result == ISC_R_SUCCESS) {
/*
* We already have this zone!
*/
dns_zone_detach(&dupzone);
result = ISC_R_EXISTS;
goto cleanup;
}
INSIST(dupzone == NULL);
/*
* See if we can reuse an existing zone. This is
* only possible if all of these are true:
* - The zone's view exists
* - A zone with the right name exists in the view
* - The zone is compatible with the config
* options (e.g., an existing master zone cannot
* be reused if the options specify a slave zone)
*/
result = dns_viewlist_find(&ns_g_server->viewlist,
view->name, view->rdclass,
&pview);
if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
goto cleanup;
if (pview != NULL)
result = dns_view_findzone(pview, origin, &zone);
if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
goto cleanup;
if (zone != NULL) {
if (! ns_zone_reusable(zone, zconfig))
dns_zone_detach(&zone);
}
if (zone != NULL) {
/*
* We found a reusable zone. Make it use the
* new view.
*/
dns_zone_setview(zone, view);
} else {
/*
* We cannot reuse an existing zone, we have
* to create a new one.
*/
CHECK(dns_zone_create(&zone, mctx));
CHECK(dns_zone_setorigin(zone, origin));
dns_zone_setview(zone, view);
CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
}
/*
* If the zone contains 'forward' or 'forwarders' statements,
* configure selective forwarding.
*/
forwarders = NULL;
if (cfg_map_get(zoptions, "forward", &forwarders) == ISC_R_SUCCESS)
CHECK(configure_forward(config, zconfig, vconfig,
view, origin, forwarders));
/*
* Configure the zone.
*/
CHECK(ns_zone_configure(config, vconfig, zconfig, aclconf, zone));
/*
* Add the zone to its view in the new view list.
*/
CHECK(dns_view_addzone(view, zone));
cleanup:
if (zone != NULL)
dns_zone_detach(&zone);
if (pview != NULL)
dns_view_detach(&pview);
return (result);
}
/*
* Configure a single server quota.
*/
static void
configure_server_quota(cfg_obj_t **maps, const char *name, isc_quota_t *quota)
{
cfg_obj_t *obj = NULL;
isc_result_t result;
result = ns_config_get(maps, name, &obj);
INSIST(result == ISC_R_SUCCESS);
quota->max = cfg_obj_asuint32(obj);
}
/*
* This function is called as soon as the 'directory' statement has been
* parsed. This can be extended to support other options if necessary.
*/
static isc_result_t
directory_callback(const char *clausename, cfg_obj_t *obj, void *arg) {
isc_result_t result;
char *directory;
REQUIRE(strcasecmp("directory", clausename) == 0);
UNUSED(arg);
/*
* Change directory.
*/
directory = cfg_obj_asstring(obj);
result = isc_dir_chdir(directory);
if (result != ISC_R_SUCCESS) {
cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
"change directory to '%s' failed: %s",
directory, isc_result_totext(result));
return (result);
}
return (ISC_R_SUCCESS);
}
static void
scan_interfaces(ns_server_t *server, isc_boolean_t verbose) {
ns_interfacemgr_scan(server->interfacemgr, verbose);
/*
* Update the "localhost" and "localnets" ACLs to match the
* current set of network interfaces.
*/
dns_aclenv_copy(&server->aclenv,
ns_interfacemgr_getaclenv(server->interfacemgr));
}
/*
* This event callback is invoked to do periodic network
* interface scanning.
*/
static void
interface_timer_tick(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
ns_server_t *server = (ns_server_t *) event->ev_arg;
INSIST(task == server->task);
UNUSED(task);
isc_event_free(&event);
/*
* XXX should scan interfaces unlocked and get exclusive access
* only to replace ACLs.
*/
result = isc_task_beginexclusive(server->task);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
scan_interfaces(server, ISC_FALSE);
isc_task_endexclusive(server->task);
}
static void
heartbeat_timer_tick(isc_task_t *task, isc_event_t *event) {
ns_server_t *server = (ns_server_t *) event->ev_arg;
dns_view_t *view;
UNUSED(task);
isc_event_free(&event);
view = ISC_LIST_HEAD(server->viewlist);
while (view != NULL) {
dns_view_dialup(view);
view = ISC_LIST_NEXT(view, link);
}
}
static isc_result_t
setstatsfile(ns_server_t *server, const char *name) {
char *p;
REQUIRE(name != NULL);
p = isc_mem_strdup(server->mctx, name);
if (p == NULL)
return (ISC_R_NOMEMORY);
if (server->statsfile != NULL)
isc_mem_free(server->mctx, server->statsfile);
server->statsfile = p;
return (ISC_R_SUCCESS);
}
static isc_result_t
setdumpfile(ns_server_t *server, const char *name) {
char *p;
REQUIRE(name != NULL);
p = isc_mem_strdup(server->mctx, name);
if (p == NULL)
return (ISC_R_NOMEMORY);
if (server->dumpfile != NULL)
isc_mem_free(server->mctx, server->dumpfile);
server->dumpfile = p;
return (ISC_R_SUCCESS);
}
static void
set_limit(cfg_obj_t **maps, const char *configname, const char *description,
isc_resource_t resourceid, isc_resourcevalue_t defaultvalue)
{
cfg_obj_t *obj = NULL;
char *resource;
isc_resourcevalue_t value;
isc_result_t result;
if (ns_config_get(maps, configname, &obj) != ISC_R_SUCCESS)
return;
if (cfg_obj_isstring(obj)) {
resource = cfg_obj_asstring(obj);
if (strcasecmp(resource, "unlimited") == 0)
value = ISC_RESOURCE_UNLIMITED;
else {
INSIST(strcasecmp(resource, "default") == 0);
value = defaultvalue;
}
} else
value = cfg_obj_asuint64(obj);
result = isc_resource_setlimit(resourceid, value);
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
result == ISC_R_SUCCESS ?
ISC_LOG_DEBUG(1) : ISC_LOG_WARNING,
"set maximum %s to %" ISC_PRINT_QUADFORMAT "d: %s",
description, value, isc_result_totext(result));
}
#define SETLIMIT(cfgvar, resource, description) \
set_limit(maps, cfgvar, description, isc_resource_ ## resource, \
ns_g_init ## resource)
static void
set_limits(cfg_obj_t **maps) {
SETLIMIT("stacksize", stacksize, "stack size");
SETLIMIT("datasize", datasize, "data size");
SETLIMIT("coresize", coresize, "core size");
SETLIMIT("files", openfiles, "open files");
}
static isc_result_t
load_configuration(const char *filename, ns_server_t *server,
isc_boolean_t first_time)
{
isc_result_t result;
cfg_parser_t *parser = NULL;
cfg_obj_t *config;
cfg_obj_t *options;
cfg_obj_t *views;
cfg_obj_t *obj;
cfg_obj_t *maps[3];
cfg_listelt_t *element;
dns_view_t *view = NULL;
dns_view_t *view_next;
dns_viewlist_t viewlist;
dns_viewlist_t tmpviewlist;
ns_aclconfctx_t aclconfctx;
dns_dispatch_t *dispatchv4 = NULL;
dns_dispatch_t *dispatchv6 = NULL;
isc_uint32_t interface_interval;
isc_uint32_t heartbeat_interval;
in_port_t listen_port;
int i;
ns_aclconfctx_init(&aclconfctx);
ISC_LIST_INIT(viewlist);
/* Ensure exclusive access to configuration data. */
result = isc_task_beginexclusive(server->task);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
/*
* Parse the global default pseudo-config file.
*/
if (first_time) {
CHECK(ns_config_parsedefaults(ns_g_parser, &ns_g_config));
RUNTIME_CHECK(cfg_map_get(ns_g_config, "options",
&ns_g_defaults) ==
ISC_R_SUCCESS);
}
/*
* Parse the configuration file using the new config code.
*/
CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx, &parser));
cfg_parser_setcallback(parser, directory_callback, NULL);
result = ISC_R_FAILURE;
config = NULL;
/*
* Unless this is lwresd with the -C option, parse the config file.
*/
if (!(ns_g_lwresdonly && lwresd_g_useresolvconf)) {
isc_log_write(ns_g_lctx,
NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "loading configuration from '%s'",
filename);
result = cfg_parse_file(parser, filename, &cfg_type_namedconf,
&config);
}
/*
* If this is lwresd with the -C option, or lwresd with no -C or -c
* option where the above parsing failed, parse resolv.conf.
*/
if (ns_g_lwresdonly &&
(lwresd_g_useresolvconf ||
(!ns_g_conffileset && result != ISC_R_FILENOTFOUND)))
{
isc_log_write(ns_g_lctx,
NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "loading configuration from '%s'",
lwresd_g_resolvconffile);
result = ns_lwresd_parseeresolvconf(ns_g_mctx, parser,
&config);
}
CHECK(result);
/*
* Check the validity of the configuration.
*/
CHECK(cfg_check_namedconf(config, ns_g_lctx));
/*
* Fill in the maps array, used for resolving defaults.
*/
i = 0;
options = NULL;
result = cfg_map_get(config, "options", &options);
if (result == ISC_R_SUCCESS)
maps[i++] = options;
maps[i++] = ns_g_defaults;
maps[i++] = NULL;
/*
* Set process limits, which (usually) needs to be done as root.
*/
set_limits(maps);
/*
* Configure various server options.
*/
configure_server_quota(maps, "transfers-out", &server->xfroutquota);
configure_server_quota(maps, "tcp-clients", &server->tcpquota);
configure_server_quota(maps, "recursive-clients",
&server->recursionquota);
CHECK(configure_view_acl(NULL, config, "blackhole", &aclconfctx,
ns_g_mctx, &server->blackholeacl));
if (server->blackholeacl != NULL)
dns_dispatchmgr_setblackhole(ns_g_dispatchmgr,
server->blackholeacl);
/*
* Configure the zone manager.
*/
obj = NULL;
result = ns_config_get(maps, "transfers-in", &obj);
INSIST(result == ISC_R_SUCCESS);
dns_zonemgr_settransfersin(server->zonemgr, cfg_obj_asuint32(obj));
obj = NULL;
result = ns_config_get(maps, "transfers-per-ns", &obj);
INSIST(result == ISC_R_SUCCESS);
dns_zonemgr_settransfersperns(server->zonemgr, cfg_obj_asuint32(obj));
/*
* Determine which port to use for listening for incoming connections.
*/
CHECKM(ns_config_getport(config, &listen_port), "port");
/*
* Configure the interface manager according to the "listen-on"
* statement.
*/
{
cfg_obj_t *clistenon = NULL;
ns_listenlist_t *listenon = NULL;
clistenon = NULL;
(void)cfg_map_get(options, "listen-on", &clistenon);
if (clistenon != NULL) {
result = ns_listenlist_fromconfig(clistenon,
config,
&aclconfctx,
ns_g_mctx,
&listenon);
} else if (!ns_g_lwresdonly) {
/*
* Not specified, use default.
*/
CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
ISC_TRUE, &listenon));
}
if (listenon != NULL) {
ns_interfacemgr_setlistenon4(server->interfacemgr,
listenon);
ns_listenlist_detach(&listenon);
}
}
/*
* Ditto for IPv6.
*/
{
cfg_obj_t *clistenon = NULL;
ns_listenlist_t *listenon = NULL;
(void)cfg_map_get(options, "listen-on", &clistenon);
if (clistenon != NULL) {
result = ns_listenlist_fromconfig(clistenon,
config,
&aclconfctx,
ns_g_mctx,
&listenon);
} else if (!ns_g_lwresdonly) {
/*
* Not specified, use default.
*/
CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
ISC_FALSE, &listenon));
}
if (listenon != NULL) {
ns_interfacemgr_setlistenon6(server->interfacemgr,
listenon);
ns_listenlist_detach(&listenon);
}
}
/*
* Rescan the interface list to pick up changes in the
* listen-on option. It's important that we do this before we try
* to configure the query source, since the dispatcher we use might
* be shared with an interface.
*/
scan_interfaces(server, ISC_TRUE);
/*
* Arrange for further interface scanning to occur periodically
* as specified by the "interface-interval" option.
*/
obj = NULL;
result = ns_config_get(maps, "interface-interval", &obj);
INSIST(result == ISC_R_SUCCESS);
interface_interval = cfg_obj_asuint32(obj);
if (interface_interval == 0) {
isc_timer_reset(server->interface_timer,
isc_timertype_inactive,
NULL, NULL, ISC_TRUE);
} else if (server->interface_interval != interface_interval) {
isc_interval_t interval;
isc_interval_set(&interval, interface_interval, 0);
isc_timer_reset(server->interface_timer, isc_timertype_ticker,
NULL, &interval, ISC_FALSE);
}
server->interface_interval = interface_interval;
/*
* Configure the dialup heartbeat timer.
*/
obj = NULL;
result = ns_config_get(maps, "heartbeat-interval", &obj);
INSIST(result == ISC_R_SUCCESS);
heartbeat_interval = cfg_obj_asuint32(obj);
if (heartbeat_interval == 0) {
isc_timer_reset(server->heartbeat_timer,
isc_timertype_inactive,
NULL, NULL, ISC_TRUE);
} else if (server->heartbeat_interval != heartbeat_interval) {
isc_interval_t interval;
isc_interval_set(&interval, heartbeat_interval, 0);
isc_timer_reset(server->heartbeat_timer, isc_timertype_ticker,
NULL, &interval, ISC_FALSE);
}
server->heartbeat_interval = heartbeat_interval;
/*
* Configure and freeze all explicit views. Explicit
* views that have zones were already created at parsing
* time, but views with no zones must be created here.
*/
views = NULL;
(void)cfg_map_get(config, "view", &views);
for (element = cfg_list_first(views);
element != NULL;
element = cfg_list_next(element))
{
cfg_obj_t *vconfig;
view = NULL;
vconfig = cfg_listelt_value(element);
CHECK(create_view(vconfig, &viewlist, &view));
INSIST(view != NULL);
CHECK(configure_view(view, config, vconfig,
ns_g_mctx, &aclconfctx));
dns_view_freeze(view);
dns_view_detach(&view);
}
/*
* Make sure we have a default view if and only if there
* were no explicit views.
*/
if (views == NULL) {
/*
* No explicit views; there ought to be a default view.
* There may already be one created as a side effect
* of zone statements, or we may have to create one.
* In either case, we need to configure and freeze it.
*/
CHECK(create_view(NULL, &viewlist, &view));
CHECK(configure_view(view, config, NULL, ns_g_mctx,
&aclconfctx));
dns_view_freeze(view);
dns_view_detach(&view);
}
/*
* Create (or recreate) the internal _bind view.
*/
CHECK(create_bind_view(&view));
ISC_LIST_APPEND(viewlist, view, link);
CHECK(create_version_zone(maps, server->zonemgr, view));
CHECK(create_authors_zone(options, server->zonemgr, view));
dns_view_freeze(view);
view = NULL;
/*
* Swap our new view list with the production one.
*/
tmpviewlist = server->viewlist;
server->viewlist = viewlist;
viewlist = tmpviewlist;
/*
* Load the TKEY information from the configuration.
*/
{
dns_tkeyctx_t *t = NULL;
CHECKM(ns_tkeyctx_fromconfig(options, ns_g_mctx, ns_g_entropy,
&t),
"configuring TKEY");
if (server->tkeyctx != NULL)
dns_tkeyctx_destroy(&server->tkeyctx);
server->tkeyctx = t;
}
/*
* Bind the OMAPI port(s).
*/
CHECKM(ns_omapi_configure(ns_g_mctx, config, &aclconfctx),
"binding control channel(s)");
/*
* Bind the lwresd port(s).
*/
CHECKM(ns_lwresd_configure(ns_g_mctx, config),
"binding lightweight resolver ports");
/*
* Open the source of entropy.
*/
if (first_time) {
obj = NULL;
result = ns_config_get(maps, "random-device", &obj);
if (result != ISC_R_SUCCESS) {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
"no source of entropy found");
} else {
const char *randomdev = cfg_obj_asstring(obj);
result = isc_entropy_createfilesource(ns_g_entropy,
randomdev);
if (result != ISC_R_SUCCESS)
isc_log_write(ns_g_lctx,
NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER,
ISC_LOG_INFO,
"could not open entropy source "
"%s: %s",
randomdev,
isc_result_totext(result));
}
}
/*
* Relinquish root privileges.
*/
if (first_time)
ns_os_changeuser();
/*
* Configure the logging system.
*
* Do this after changing UID to make sure that any log
* files specified in named.conf get created by the
* unprivileged user, not root.
*/
if (ns_g_logstderr) {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
"ignoring config file logging "
"statement due to -g option");
} else {
cfg_obj_t *logobj = NULL;
isc_logconfig_t *logc = NULL;
CHECKM(isc_logconfig_create(ns_g_lctx, &logc),
"creating new logging configuration");
logobj = NULL;
(void)cfg_map_get(config, "logging", &logobj);
if (logobj != NULL) {
CHECKM(ns_log_configure(logc, logobj),
"configuring logging");
} else {
CHECKM(ns_log_setdefaultchannels(logc),
"setting up default logging channels");
CHECKM(ns_log_setdefaultcategory(logc),
"setting up default 'category default'");
}
result = isc_logconfig_use(ns_g_lctx, logc);
if (result != ISC_R_SUCCESS) {
isc_logconfig_destroy(&logc);
CHECKM(result, "installing logging configuration");
}
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(1),
"now using logging configuration from "
"config file");
}
/*
* Set the default value of the query logging flag depending
* whether a "queries" category has been defined. This is
* a disgusting hack, but we need to do this for BIND 8
* compatibility.
*/
if (first_time) {
cfg_obj_t *logobj = NULL;
cfg_obj_t *categories = NULL;
(void)cfg_map_get(config, "logging", &logobj);
if (logobj != NULL)
(void)cfg_map_get(logobj, "category", &categories);
if (categories != NULL) {
cfg_listelt_t *element;
for (element = cfg_list_first(categories);
element != NULL;
element = cfg_list_next(element))
{
cfg_obj_t *catobj;
char *str;
obj = cfg_listelt_value(element);
catobj = cfg_tuple_get(obj, "name");
str = cfg_obj_asstring(catobj);
if (strcasecmp(str, "queries") == 0)
server->log_queries = ISC_TRUE;
}
}
}
obj = NULL;
if (ns_config_get(maps, "pid-file", &obj) == ISC_R_SUCCESS)
ns_os_writepidfile(cfg_obj_asstring(obj));
else if (ns_g_lwresdonly)
ns_os_writepidfile(lwresd_g_defaultpidfile);
else
ns_os_writepidfile(ns_g_defaultpidfile);
obj = NULL;
result = ns_config_get(maps, "statistics-file", &obj);
INSIST(result == ISC_R_SUCCESS);
CHECKM(setstatsfile(server, cfg_obj_asstring(obj)), "strdup");
obj = NULL;
result = ns_config_get(maps, "dump-file", &obj);
INSIST(result == ISC_R_SUCCESS);
CHECKM(setdumpfile(server, cfg_obj_asstring(obj)), "strdup");
cleanup:
ns_aclconfctx_destroy(&aclconfctx);
if (config != NULL)
cfg_obj_destroy(parser, &config);
if (parser != NULL)
cfg_parser_destroy(&parser);
if (view != NULL)
dns_view_detach(&view);
/*
* This cleans up either the old production view list
* or our temporary list depending on whether they
* were swapped above or not.
*/
for (view = ISC_LIST_HEAD(viewlist);
view != NULL;
view = view_next) {
view_next = ISC_LIST_NEXT(view, link);
ISC_LIST_UNLINK(viewlist, view, link);
dns_view_detach(&view);
}
if (dispatchv4 != NULL)
dns_dispatch_detach(&dispatchv4);
if (dispatchv6 != NULL)
dns_dispatch_detach(&dispatchv6);
/* Relinquish exclusive access to configuration data. */
isc_task_endexclusive(server->task);
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_DEBUG(1), "load_configuration: %s",
isc_result_totext(result));
return (result);
}
static isc_result_t
load_zones(ns_server_t *server, isc_boolean_t stop) {
isc_result_t result;
dns_view_t *view;
result = isc_task_beginexclusive(server->task);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
/*
* Load zone data from disk.
*/
for (view = ISC_LIST_HEAD(server->viewlist);
view != NULL;
view = ISC_LIST_NEXT(view, link))
{
CHECK(dns_view_load(view, stop));
}
/*
* Force zone maintenance. Do this after loading
* so that we know when we need to force AXFR of
* slave zones whose master files are missing.
*/
CHECK(dns_zonemgr_forcemaint(server->zonemgr));
cleanup:
isc_task_endexclusive(server->task);
return (result);
}
static void
run_server(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
ns_server_t *server = (ns_server_t *)event->ev_arg;
UNUSED(task);
isc_event_free(&event);
CHECKFATAL(dns_dispatchmgr_create(ns_g_mctx, ns_g_entropy,
&ns_g_dispatchmgr),
"creating dispatch manager");
CHECKFATAL(ns_interfacemgr_create(ns_g_mctx, ns_g_taskmgr,
ns_g_socketmgr, ns_g_dispatchmgr,
&server->interfacemgr),
"creating interface manager");
CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
NULL, NULL, server->task,
interface_timer_tick,
server, &server->interface_timer),
"creating interface timer");
CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
NULL, NULL, server->task,
heartbeat_timer_tick,
server, &server->heartbeat_timer),
"creating heartbeat timer");
CHECKFATAL(cfg_parser_create(ns_g_mctx, NULL, &ns_g_parser),
"creating default configuration parser");
if (ns_g_lwresdonly)
CHECKFATAL(load_configuration(lwresd_g_conffile, server,
ISC_TRUE),
"loading configuration");
else
CHECKFATAL(load_configuration(ns_g_conffile, server, ISC_TRUE),
"loading configuration");
CHECKFATAL(load_zones(server, ISC_FALSE),
"loading zones");
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "running");
}
void
ns_server_flushonshutdown(ns_server_t *server, isc_boolean_t flush) {
REQUIRE(NS_SERVER_VALID(server));
server->flushonshutdown = flush;
}
static void
shutdown_server(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
dns_view_t *view, *view_next;
ns_server_t *server = (ns_server_t *)event->ev_arg;
isc_boolean_t flush = server->flushonshutdown;
UNUSED(task);
INSIST(task == server->task);
result = isc_task_beginexclusive(server->task);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "shutting down%s",
flush ? ": flushing changes" : "");
cfg_obj_destroy(ns_g_parser, &ns_g_config);
cfg_parser_destroy(&ns_g_parser);
for (view = ISC_LIST_HEAD(server->viewlist);
view != NULL;
view = view_next) {
view_next = ISC_LIST_NEXT(view, link);
ISC_LIST_UNLINK(server->viewlist, view, link);
if (flush)
dns_view_flushanddetach(&view);
else
dns_view_detach(&view);
}
isc_timer_detach(&server->interface_timer);
isc_timer_detach(&server->heartbeat_timer);
ns_interfacemgr_shutdown(server->interfacemgr);
ns_interfacemgr_detach(&server->interfacemgr);
dns_dispatchmgr_destroy(&ns_g_dispatchmgr);
dns_zonemgr_shutdown(server->zonemgr);
if (server->blackholeacl != NULL)
dns_acl_detach(&server->blackholeacl);
isc_task_endexclusive(server->task);
isc_task_detach(&server->task);
isc_event_free(&event);
}
void
ns_server_create(isc_mem_t *mctx, ns_server_t **serverp) {
isc_result_t result;
ns_server_t *server = isc_mem_get(mctx, sizeof(*server));
if (server == NULL)
fatal("allocating server object", ISC_R_NOMEMORY);
server->mctx = mctx;
server->task = NULL;
/* Initialize configuration data with default values. */
result = isc_quota_init(&server->xfroutquota, 10);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
result = isc_quota_init(&server->tcpquota, 10);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
result = isc_quota_init(&server->recursionquota, 100);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
result = dns_aclenv_init(mctx, &server->aclenv);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
/* Initialize server data structures. */
server->zonemgr = NULL;
server->interfacemgr = NULL;
ISC_LIST_INIT(server->viewlist);
server->in_roothints = NULL;
server->blackholeacl = NULL;
CHECKFATAL(dns_rootns_create(mctx, dns_rdataclass_in, NULL,
&server->in_roothints),
"setting up root hints");
CHECKFATAL(isc_mutex_init(&server->reload_event_lock),
"initializing reload event lock");
server->reload_event =
isc_event_allocate(ns_g_mctx, server,
NS_EVENT_RELOAD,
ns_server_reload,
server,
sizeof(isc_event_t));
CHECKFATAL(server->reload_event == NULL ?
ISC_R_NOMEMORY : ISC_R_SUCCESS,
"allocating reload event");
CHECKFATAL(dst_lib_init(ns_g_mctx, ns_g_entropy, ISC_ENTROPY_GOODONLY),
"initializing DST");
server->tkeyctx = NULL;
CHECKFATAL(dns_tkeyctx_create(ns_g_mctx, ns_g_entropy,
&server->tkeyctx),
"creating TKEY context");
/*
* Setup the server task, which is responsible for coordinating
* startup and shutdown of the server.
*/
CHECKFATAL(isc_task_create(ns_g_taskmgr, 0, &server->task),
"creating server task");
isc_task_setname(server->task, "server", server);
CHECKFATAL(isc_task_onshutdown(server->task, shutdown_server, server),
"isc_task_onshutdown");
CHECKFATAL(isc_app_onrun(ns_g_mctx, server->task, run_server, server),
"isc_app_onrun");
server->interface_timer = NULL;
server->heartbeat_timer = NULL;
server->interface_interval = 0;
server->heartbeat_interval = 0;
CHECKFATAL(dns_zonemgr_create(ns_g_mctx, ns_g_taskmgr, ns_g_timermgr,
ns_g_socketmgr, &server->zonemgr),
"dns_zonemgr_create");
server->statsfile = isc_mem_strdup(server->mctx, "named.stats");
CHECKFATAL(server->statsfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
"isc_mem_strdup");
server->querystats = NULL;
CHECKFATAL(dns_stats_alloccounters(ns_g_mctx, &server->querystats),
"dns_stats_alloccounters");
server->dumpfile = isc_mem_strdup(server->mctx, "named.dump");
CHECKFATAL(server->dumpfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
"isc_mem_strdup");
server->flushonshutdown = ISC_FALSE;
server->log_queries = ISC_FALSE;
server->magic = NS_SERVER_MAGIC;
*serverp = server;
}
void
ns_server_destroy(ns_server_t **serverp) {
ns_server_t *server = *serverp;
REQUIRE(NS_SERVER_VALID(server));
dns_stats_freecounters(server->mctx, &server->querystats);
isc_mem_free(server->mctx, server->statsfile);
isc_mem_free(server->mctx, server->dumpfile);
dns_zonemgr_detach(&server->zonemgr);
if (server->tkeyctx != NULL)
dns_tkeyctx_destroy(&server->tkeyctx);
dst_lib_destroy();
isc_event_free(&server->reload_event);
INSIST(ISC_LIST_EMPTY(server->viewlist));
dns_db_detach(&server->in_roothints);
dns_aclenv_destroy(&server->aclenv);
isc_quota_destroy(&server->recursionquota);
isc_quota_destroy(&server->tcpquota);
isc_quota_destroy(&server->xfroutquota);
server->magic = 0;
isc_mem_put(server->mctx, server, sizeof(*server));
}
static void
fatal(const char *msg, isc_result_t result) {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_CRITICAL, "%s: %s", msg,
isc_result_totext(result));
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_CRITICAL, "exiting (due to fatal error)");
exit(1);
}
static void
ns_server_reload(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
ns_server_t *server = (ns_server_t *)event->ev_arg;
INSIST(task = server->task);
UNUSED(task);
if (ns_g_lwresdonly)
result = load_configuration(lwresd_g_conffile, server,
ISC_FALSE);
else
result = load_configuration(ns_g_conffile, server, ISC_FALSE);
if (result != ISC_R_SUCCESS) {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
"reloading configuration failed: %s",
isc_result_totext(result));
}
result = load_zones(server, ISC_FALSE);
if (result != ISC_R_SUCCESS) {
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
"reloading zones failed: %s",
isc_result_totext(result));
}
LOCK(&server->reload_event_lock);
INSIST(server->reload_event == NULL);
server->reload_event = event;
UNLOCK(&server->reload_event_lock);
}
void
ns_server_reloadwanted(ns_server_t *server) {
LOCK(&server->reload_event_lock);
if (server->reload_event != NULL)
isc_task_send(server->task, &server->reload_event);
UNLOCK(&server->reload_event_lock);
}
static char *
next_token(char **stringp, const char *delim) {
char *res;
do {
res = strsep(stringp, delim);
if (res == NULL)
break;
} while (*res == '\0');
return (res);
}
/*
* Find the zone specified in the control channel command 'args',
* if any. If a zone is specified, point '*zonep' at it, otherwise
* set '*zonep' to NULL.
*/
static isc_result_t
zone_from_args(ns_server_t *server, char *args, dns_zone_t **zonep) {
char *input, *ptr;
const char *zonetxt;
char *classtxt;
const char *viewtxt = NULL;
dns_fixedname_t name;
isc_result_t result;
isc_buffer_t buf;
dns_view_t *view = NULL;
dns_rdataclass_t rdclass;
REQUIRE(zonep != NULL && *zonep == NULL);
input = args;
/* Skip the command name. */
ptr = next_token(&input, " \t");
if (ptr == NULL)
return (ISC_R_UNEXPECTEDEND);
/* Look for the zone name. */
zonetxt = next_token(&input, " \t");
if (zonetxt == NULL) {
*zonep = NULL;
return (ISC_R_SUCCESS);
}
/* Look for the optional class name. */
classtxt = next_token(&input, " \t");
if (classtxt != NULL) {
/* Look for the optional view name. */
viewtxt = next_token(&input, " \t");
}
isc_buffer_init(&buf, zonetxt, strlen(zonetxt));
isc_buffer_add(&buf, strlen(zonetxt));
dns_fixedname_init(&name);
result = dns_name_fromtext(dns_fixedname_name(&name),
&buf, dns_rootname, ISC_FALSE, NULL);
if (result != ISC_R_SUCCESS)
goto fail1;
if (classtxt != NULL) {
isc_textregion_t r;
r.base = classtxt;
r.length = strlen(classtxt);
result = dns_rdataclass_fromtext(&rdclass, &r);
if (result != ISC_R_SUCCESS)
goto fail1;
} else {
rdclass = dns_rdataclass_in;
}
if (viewtxt == NULL)
viewtxt = "_default";
result = dns_viewlist_find(&server->viewlist, viewtxt,
rdclass, &view);
if (result != ISC_R_SUCCESS)
goto fail1;
result = dns_zt_find(view->zonetable, dns_fixedname_name(&name),
0, NULL, zonep);
if (result != ISC_R_SUCCESS)
goto fail2;
fail2:
dns_view_detach(&view);
fail1:
return (result);
}
/*
* Act on a "reload" command from the command channel.
*/
isc_result_t
ns_server_reloadcommand(ns_server_t *server, char *args) {
isc_result_t result;
dns_zone_t *zone = NULL;
dns_zonetype_t type;
UNUSED(server);
result = zone_from_args(server, args, &zone);
if (result != ISC_R_SUCCESS)
return (result);
if (zone == NULL) {
ns_server_reloadwanted(server);
} else {
type = dns_zone_gettype(zone);
if (type == dns_zone_slave || type == dns_zone_stub)
dns_zone_refresh(zone);
else
dns_zone_load(zone);
dns_zone_detach(&zone);
}
return (ISC_R_SUCCESS);
}
/*
* Act on a "refresh" command from the command channel.
*/
isc_result_t
ns_server_refreshcommand(ns_server_t *server, char *args) {
isc_result_t result;
dns_zone_t *zone = NULL;
result = zone_from_args(server, args, &zone);
if (result != ISC_R_SUCCESS)
return (result);
if (zone == NULL)
return (ISC_R_UNEXPECTEDEND);
dns_zone_refresh(zone);
dns_zone_detach(&zone);
return (ISC_R_SUCCESS);
}
isc_result_t
ns_server_togglequerylog(ns_server_t *server) {
server->log_queries = server->log_queries ? ISC_FALSE : ISC_TRUE;
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
"query logging is now %s",
server->log_queries ? "on" : "off");
return (ISC_R_SUCCESS);
}
static isc_result_t
ns_listenlist_fromconfig(cfg_obj_t *listenlist, cfg_obj_t *config,
ns_aclconfctx_t *actx,
isc_mem_t *mctx, ns_listenlist_t **target)
{
isc_result_t result;
cfg_listelt_t *element;
ns_listenlist_t *dlist = NULL;
REQUIRE(target != NULL && *target == NULL);
result = ns_listenlist_create(mctx, &dlist);
if (result != ISC_R_SUCCESS)
return (result);
for (element = cfg_list_first(listenlist);
element != NULL;
element = cfg_list_next(element))
{
ns_listenelt_t *delt = NULL;
cfg_obj_t *listener = cfg_listelt_value(element);
result = ns_listenelt_fromconfig(listener, config, actx,
mctx, &delt);
if (result != ISC_R_SUCCESS)
goto cleanup;
ISC_LIST_APPEND(dlist->elts, delt, link);
}
*target = dlist;
return (ISC_R_SUCCESS);
cleanup:
ns_listenlist_detach(&dlist);
return (result);
}
/*
* Create a listen list from the corresponding configuration
* data structure.
*/
static isc_result_t
ns_listenelt_fromconfig(cfg_obj_t *listener, cfg_obj_t *config,
ns_aclconfctx_t *actx,
isc_mem_t *mctx, ns_listenelt_t **target)
{
isc_result_t result;
cfg_obj_t *portobj;
in_port_t port;
ns_listenelt_t *delt = NULL;
REQUIRE(target != NULL && *target == NULL);
portobj = cfg_tuple_get(listener, "port");
if (!cfg_obj_isuint32(portobj)) {
result = ns_config_getport(config, &port);
if (result != ISC_R_SUCCESS)
return (result);
} else {
if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
"port value '%u' is out of range",
cfg_obj_asuint32(portobj));
return (ISC_R_RANGE);
}
port = (in_port_t)cfg_obj_asuint32(portobj);
}
result = ns_listenelt_create(mctx, port, NULL, &delt);
if (result != ISC_R_SUCCESS)
return (result);
result = ns_acl_fromconfig(cfg_tuple_get(listener, "acl"),
config, actx, mctx, &delt->acl);
if (result != ISC_R_SUCCESS) {
ns_listenelt_destroy(delt);
return (result);
}
*target = delt;
return (ISC_R_SUCCESS);
}
isc_result_t
ns_server_dumpstats(ns_server_t *server) {
isc_result_t result;
dns_zone_t *zone, *next;
isc_stdtime_t now;
FILE *fp = NULL;
int i;
int ncounters;
isc_stdtime_get(&now);
CHECKM(isc_stdio_open(server->statsfile, "a", &fp),
"could not open statistics dump file");
ncounters = DNS_STATS_NCOUNTERS;
fprintf(fp, "+++ Statistics Dump +++ (%lu)\n", (unsigned long)now);
for (i = 0; i < ncounters; i++)
fprintf(fp, "%s %" ISC_PRINT_QUADFORMAT "d\n",
dns_statscounter_names[i],
server->querystats[i]);
zone = NULL;
for (result = dns_zone_first(server->zonemgr, &zone);
result == ISC_R_SUCCESS;
next = NULL, result = dns_zone_next(zone, &next), zone = next)
{
isc_uint64_t *zonestats = dns_zone_getstatscounters(zone);
if (zonestats != NULL) {
char zonename[DNS_NAME_FORMATSIZE];
dns_view_t *view;
char *viewname;
dns_name_format(dns_zone_getorigin(zone),
zonename, sizeof(zonename));
view = dns_zone_getview(zone);
viewname = view->name;
for (i = 0; i < ncounters; i++) {
fprintf(fp, "%s %" ISC_PRINT_QUADFORMAT
"d %s",
dns_statscounter_names[i],
zonestats[i],
zonename);
if (strcmp(viewname, "_default") != 0)
fprintf(fp, " %s", viewname);
fprintf(fp, "\n");
}
}
}
if (result == ISC_R_NOMORE)
result = ISC_R_SUCCESS;
CHECK(result);
fprintf(fp, "--- Statistics Dump --- (%lu)\n", (unsigned long)now);
cleanup:
if (fp != NULL)
(void)isc_stdio_close(fp);
return (result);
}
isc_result_t
ns_server_dumpdb(ns_server_t *server) {
FILE *fp = NULL;
dns_view_t *view;
isc_result_t result;
CHECKM(isc_stdio_open(server->dumpfile, "w", &fp),
"could not open dump file");
for (view = ISC_LIST_HEAD(server->viewlist);
view != NULL;
view = ISC_LIST_NEXT(view, link))
{
if (view->cachedb != NULL)
CHECKM(dns_view_dumpdbtostream(view, fp),
"could not dump view databases");
}
cleanup:
if (fp != NULL)
(void)isc_stdio_close(fp);
return (result);
}
isc_result_t
ns_server_setdebuglevel(ns_server_t *server, char *args) {
char *ptr;
char *levelstr;
char *endp;
long newlevel;
UNUSED(server);
/* Skip the command name. */
ptr = next_token(&args, " \t");
if (ptr == NULL)
return (ISC_R_UNEXPECTEDEND);
/* Look for the new level name. */
levelstr = next_token(&args, " \t");
if (levelstr == NULL)
ns_g_debuglevel++;
else {
newlevel = strtol(levelstr, &endp, 10);
if (*endp != '\0' || newlevel < 0 || newlevel > 99)
return (ISC_R_RANGE);
ns_g_debuglevel = (unsigned int)newlevel;
}
isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
return (ISC_R_SUCCESS);
}