server.c revision 573b8b48d888e3f203ddd3fc78eafa986b9af44b
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson/*
754cb8a2b33fa6cfaa15d6470f66e5fb0eab4764Automatic Updater * Copyright (C) 1999 Internet Software Consortium.
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * 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
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson */
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdio.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <stdlib.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <unistd.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <string.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <stdarg.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/assertions.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/error.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <isc/rwlock.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <isc/mem.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <isc/task.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/thread.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <isc/result.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <isc/socket.h>
7dde2780aeae0da4e965d823578a604d8ce1215fAndreas Gustafsson#include <isc/timer.h>
7dde2780aeae0da4e965d823578a604d8ce1215fAndreas Gustafsson#include <isc/app.h>
f5862b6f22761d83c230e17f7d443211df19e52cBrian Wellington#include <isc/dir.h>
9259fed3d8ac5d1efa9b5a647969e40c9c934484Andreas Gustafsson#include <isc/util.h>
9259fed3d8ac5d1efa9b5a647969e40c9c934484Andreas Gustafsson
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <dns/aclconf.h>
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#include <dns/cache.h>
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#include <dns/confparser.h>
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#include <dns/types.h>
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews#include <dns/result.h>
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#include <dns/master.h>
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#include <dns/name.h>
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#include <dns/fixedname.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <dns/rdata.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <dns/rdatalist.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <dns/rdataset.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <dns/rdatasetiter.h>
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington#include <dns/compress.h>
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews#include <dns/db.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <dns/dbtable.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <dns/message.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <dns/journal.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <dns/view.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <dns/zone.h>
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#include <dns/zoneconf.h>
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#include <dns/tsig.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson#include <dns/tkey.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#include <named/types.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <named/globals.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <named/log.h>
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#include <named/rootns.h>
9df7d74e421cf715c6e3cbbad2aba6d33a5d1c9bBrian Wellington#include <named/server.h>
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef struct {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mem_t * mctx;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_viewlist_t viewlist;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_aclconfctx_t *aclconf;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington} ns_load_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic isc_task_t * server_task;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic isc_result_t
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencecreate_default_view(isc_mem_t *mctx, dns_rdataclass_t rdclass,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_view_t **viewp)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington{
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_view_t *view;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_cache_t *cache;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson isc_result_t result;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson REQUIRE(viewp != NULL && *viewp == NULL);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * View.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence view = NULL;
fb64c9cf757422c5473764521ffc7c6111b8c821Brian Wellington result = dns_view_create(mctx, rdclass, "_default", &view);
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews if (result != ISC_R_SUCCESS)
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews return (result);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews /*
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews * Cache.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington cache = NULL;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson result = dns_cache_create(mctx, ns_g_taskmgr, ns_g_timermgr, rdclass,
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson "rbt", 0, NULL, &cache);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (result != ISC_R_SUCCESS)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington goto cleanup;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson dns_view_setcache(view, cache);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson dns_cache_detach(&cache);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson /*
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson * XXXRTH Temporary support for loading cache contents.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence */
734ae1f7c6abafe1f1ca164aad7a3dd01ee82cbdBrian Wellington if (ns_g_cachefile != NULL) {
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson NS_LOGMODULE_SERVER,
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ISC_LOG_DEBUG(1), "loading cache '%s'",
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson ns_g_cachefile);
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington result = dns_db_load(view->cachedb, ns_g_cachefile);
7a184cd4e5a54a4e530f9bff8a4e46be392d0b52Brian Wellington if (result != ISC_R_SUCCESS)
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington goto cleanup;
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews }
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson /*
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson * Resolver.
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson *
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews * XXXRTH hardwired number of tasks. Also, we'll need to
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews * see if we are dealing with a shared dispatcher in this view.
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews */
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews result = dns_view_createresolver(view, ns_g_taskmgr, 31,
c6d4f781529d2f28693546b25b2967d44ec89e60Mark Andrews ns_g_socketmgr, ns_g_timermgr,
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson NULL);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (result != ISC_R_SUCCESS)
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson goto cleanup;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington /*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * We have default hints for class IN.
26a5f97dd8770ced729025488091b77d8beb0ab6Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (rdclass == dns_rdataclass_in)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_view_sethints(view, ns_g_rootns);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
9df7d74e421cf715c6e3cbbad2aba6d33a5d1c9bBrian Wellington *viewp = view;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson return (ISC_R_SUCCESS);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson cleanup:
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews dns_view_detach(&view);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson return (result);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews}
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson/*
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson * Create the special view that handles queries for
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * "version.bind. CH". The version string returned is that
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * configured in 'configctx', or a compiled-in default if
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * there is no "version" configuration option.
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic isc_result_t
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtoncreate_version_view(dns_c_ctx_t *configctx, dns_view_t **viewp) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_result_t result;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_db_t *db = NULL;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_zone_t *zone = NULL;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_dbversion_t *dbver = NULL;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson dns_difftuple_t *tuple = NULL;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson dns_diff_t diff;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson dns_view_t *view = NULL;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson dns_name_t *origin;
c885fad9b8bf204ae9e62c9acb0321e2bcca30a4Andreas Gustafsson char *versiontext;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington char buf[256];
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_region_t r;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington size_t len;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_rdata_t rdata;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington REQUIRE(viewp != NULL && *viewp == NULL);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_diff_init(ns_g_mctx, &diff);
8c7eaac6bbcc9746afe8f57b60bb964745c01eafAndreas Gustafsson
8c7eaac6bbcc9746afe8f57b60bb964745c01eafAndreas Gustafsson (void) dns_c_ctx_getversion(configctx, &versiontext);
8c7eaac6bbcc9746afe8f57b60bb964745c01eafAndreas Gustafsson if (versiontext == NULL)
c885fad9b8bf204ae9e62c9acb0321e2bcca30a4Andreas Gustafsson versiontext = ns_g_version;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson len = strlen(versiontext);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson if (len > 255)
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson len = 255; /* Silently truncate. */
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson buf[0] = len;
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson memcpy(buf + 1, versiontext, len);
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson
f93d33e24fdf76eb2558168f018b8992bcfc5681Andreas Gustafsson r.base = buf;
r.length = 1 + len;
dns_rdata_fromregion(&rdata, dns_rdataclass_ch, dns_rdatatype_txt, &r);
result = dns_zone_create(&zone, ns_g_mctx);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_zone_setorigin(zone, "version.bind.");
if (result != ISC_R_SUCCESS)
goto cleanup;
origin = dns_zone_getorigin(zone);
result = dns_db_create(ns_g_mctx, "rbt", origin, ISC_FALSE,
dns_rdataclass_ch, 0, NULL, &db);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_db_newversion(db, &dbver);
if (result != DNS_R_SUCCESS)
goto cleanup;
dns_difftuple_create(ns_g_mctx, DNS_DIFFOP_ADD, origin,
0, &rdata, &tuple);
dns_diff_append(&diff, &tuple);
result = dns_diff_apply(&diff, db, dbver);
if (result != DNS_R_SUCCESS)
goto cleanup;
dns_db_closeversion(db, &dbver, ISC_TRUE);
result = dns_view_create(ns_g_mctx, dns_rdataclass_ch, "_version",
&view);
if (result != ISC_R_SUCCESS)
return (result);
result = dns_zone_replacedb(zone, db, ISC_FALSE);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_view_addzone(view, zone);
if (result != DNS_R_SUCCESS)
goto cleanup;
dns_view_freeze(view);
/* Transfer ownership. */
*viewp = view;
view = NULL;
result = ISC_R_SUCCESS;
cleanup:
if (view != NULL)
dns_view_detach(&view);
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
load_zone(dns_c_ctx_t *ctx, dns_c_zone_t *czone, dns_c_view_t *cview,
void *uap)
{
ns_load_t *lctx;
dns_view_t *view, *tview, *pview;
dns_zone_t *zone, *tzone;
dns_name_t *origin;
isc_result_t result;
/*
* Load (or reload) a zone.
*/
lctx = uap;
tzone = NULL;
zone = NULL;
pview = NULL;
/*
* Find the view.
*/
view = NULL;
if (cview != NULL) {
result = dns_viewlist_find(&lctx->viewlist, cview->name,
czone->zclass, &view);
if (result != ISC_R_SUCCESS)
return (result);
} else {
result = dns_viewlist_find(&lctx->viewlist, "_default",
czone->zclass, &view);
if (result == ISC_R_NOTFOUND) {
/*
* Create a default view.
*/
tview = NULL;
result = create_default_view(ctx->mem, czone->zclass,
&tview);
if (result != ISC_R_SUCCESS)
return (result);
dns_view_attach(tview, &view);
ISC_LIST_APPEND(lctx->viewlist, view, link);
} else if (result != ISC_R_SUCCESS)
return (result);
}
/*
* Do we already have a production version of this view?
*/
RWLOCK(&ns_g_viewlock, isc_rwlocktype_read);
result = dns_viewlist_find(&ns_g_viewlist, view->name, view->rdclass,
&pview);
RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_read);
if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
goto cleanup;
/*
* Create a new zone structure and configure it.
*/
result = dns_zone_create(&zone, lctx->mctx);
if (result != ISC_R_SUCCESS)
return (result);
result = dns_zone_configure(ns_g_lctx, ctx, lctx->aclconf, czone, zone);
if (result != ISC_R_SUCCESS)
return (result);
if (dns_zone_gettype(zone) == dns_zone_hint) {
INSIST(0);
} else {
/*
* Check for duplicates in the new zone table.
*/
origin = dns_zone_getorigin(zone);
result = dns_view_findzone(view, origin, &tzone);
if (result == ISC_R_SUCCESS) {
/*
* We already have this zone!
*/
result = ISC_R_EXISTS;
goto cleanup;
}
/*
* Do we have the zone in the production view?
*/
if (pview != NULL)
result = dns_view_findzone(pview, origin, &tzone);
else
result = ISC_R_NOTFOUND;
if (result == ISC_R_SUCCESS) {
/*
* Yes.
*
* If the production zone's configuration is
* the same as the new zone's, we can use the
* production zone.
*/
if (dns_zone_equal(zone, tzone))
result = dns_view_addzone(view, tzone);
else
result = dns_view_addzone(view, zone);
} else if (result == ISC_R_NOTFOUND) {
/*
* This is a new zone.
*/
result = dns_view_addzone(view, zone);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_zonemgr_managezone(ns_g_zonemgr, zone);
if (result != DNS_R_SUCCESS)
goto cleanup;
}
}
cleanup:
if (tzone != NULL)
dns_zone_detach(&tzone);
if (zone != NULL)
dns_zone_detach(&zone);
if (pview != NULL)
dns_view_detach(&pview);
if (view != NULL)
dns_view_detach(&view);
return (result);
}
/* XXX will need error recovery for reconfig */
static void
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;
callbacks.zonecbkuap = &lctx;
callbacks.optscbk = NULL;
callbacks.optscbkuap = NULL;
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "loading '%s'", filename);
configctx = NULL;
result = dns_c_parse_namedconf(filename, ns_g_mctx, &configctx,
&callbacks);
if (result != ISC_R_SUCCESS) {
#ifdef notyet
for (view = ISC_LIST_HEAD(lctx.viewlist);
view != NULL;
view = view_next) {
view_next = ISC_LIST_NEXT(view, link);
ISC_LIST_UNLINK(lctx.viewlist, view, link);
dns_view_detach(&view);
}
#endif
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.)
*/
if (ISC_LIST_EMPTY(lctx.viewlist)) {
view = NULL;
result = create_default_view(ns_g_mctx, dns_rdataclass_in,
&view);
if (result != ISC_R_SUCCESS)
ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
"could not create default view");
ISC_LIST_APPEND(lctx.viewlist, view, link);
}
/*
* Freeze the views.
*/
for (view = ISC_LIST_HEAD(lctx.viewlist);
view != NULL;
view = ISC_LIST_NEXT(view, link))
dns_view_freeze(view);
/*
* Create the version view.
*/
view = NULL;
result = create_version_view(configctx, &view);
if (result != ISC_R_SUCCESS)
ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
"could not create version view");
ISC_LIST_APPEND(lctx.viewlist, view, link);
view = NULL;
/*
* Change directory.
*/
if (configctx->options != NULL &&
configctx->options->directory != NULL) {
result = isc_dir_chdir(configctx->options->directory);
if (result != ISC_R_SUCCESS)
ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
"change directory to '%s' failed: %s",
configctx->options->directory,
isc_result_totext(result));
}
/*
* Load zones.
*/
for (view = ISC_LIST_HEAD(lctx.viewlist);
view != NULL;
view = view_next) {
view_next = ISC_LIST_NEXT(view, link);
dns_view_load(view);
}
/*
* 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.
*/
dns_zonemgr_forcemaint(ns_g_zonemgr);
/*
* Put the configuration into production.
*/
RWLOCK(&ns_g_viewlock, isc_rwlocktype_write);
oviewlist = ns_g_viewlist;
ns_g_viewlist = lctx.viewlist;
RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_write);
/*
* Cleanup old configuration.
*/
for (view = ISC_LIST_HEAD(oviewlist);
view != NULL;
view = view_next) {
view_next = ISC_LIST_NEXT(view, link);
ISC_LIST_UNLINK(oviewlist, view, link);
dns_view_detach(&view);
}
/*
* Load the TSIG information from the configuration
*/
result = dns_tsig_init(ns_g_lctx, configctx, ns_g_mctx);
if (result != ISC_R_SUCCESS)
ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
"dns_tsig_init() failed: %s",
isc_result_totext(result));
/*
* Load the TKEY information from the configuration
*/
result = dns_tkey_init(ns_g_lctx, configctx, ns_g_mctx);
if (result != ISC_R_SUCCESS) {
ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
"dns_tkey_init() failed: %s",
isc_result_totext(result));
}
dns_aclconfctx_destroy(&aclconfctx);
dns_c_ctx_delete(&configctx);
}
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, server);
ns_interfacemgr_scan(ns_g_interfacemgr);
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "running");
}
static void
shutdown_server(isc_task_t *task, isc_event_t *event) {
dns_view_t *view, *view_next;
ns_server_t *server = (ns_server_t *) event->arg;
(void)task;
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "shutting down");
RWLOCK(&ns_g_viewlock, isc_rwlocktype_write);
for (view = ISC_LIST_HEAD(ns_g_viewlist);
view != NULL;
view = view_next) {
view_next = ISC_LIST_NEXT(view, link);
ISC_LIST_UNLINK(ns_g_viewlist, view, link);
dns_view_detach(&view);
}
dns_tkey_destroy();
dns_tsig_destroy();
RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_write);
isc_task_detach(&server_task);
dns_zonemgr_destroy(&ns_g_zonemgr);
ns_rootns_destroy();
ns_server_destroy(&server);
isc_event_free(&event);
}
isc_result_t
ns_server_create(isc_mem_t *mctx, ns_server_t **serverp) {
ns_server_t *server = isc_mem_get(mctx, sizeof(*server));
if (server == NULL)
return (ISC_R_NOMEMORY);
server->mctx = mctx;
/* Initialize. */
server->recursion = ISC_TRUE;
server->auth_nxdomain = ISC_FALSE; /* Was true in BIND 8 */
server->transfer_format = dns_one_answer;
server->queryacl = NULL;
server->recursionacl = NULL;
server->transferacl = NULL;
server->magic = NS_SERVER_MAGIC;
*serverp = server;
return (ISC_R_SUCCESS);
}
void
ns_server_destroy(ns_server_t **serverp) {
ns_server_t *server = *serverp;
REQUIRE(NS_SERVER_VALID(server));
if (server->queryacl != NULL)
dns_acl_detach(&server->queryacl);
if (server->recursionacl != NULL)
dns_acl_detach(&server->recursionacl);
if (server->transferacl != NULL)
dns_acl_detach(&server->transferacl);
server->magic = 0;
isc_mem_put(server->mctx, server, sizeof(*server));
}
isc_result_t
ns_server_init() {
isc_result_t result;
/*
* Create the server object.
*/
result = ns_server_create(ns_g_mctx, &ns_g_server);
if (result != ISC_R_SUCCESS)
return (result);
/*
* Setup default root server hints.
*/
result = ns_rootns_init();
if (result != ISC_R_SUCCESS)
return (result);
result = dns_zonemgr_create(ns_g_mctx, ns_g_taskmgr, ns_g_timermgr,
ns_g_socketmgr, &ns_g_zonemgr);
if (result != ISC_R_SUCCESS)
return (result);
/*
* Setup the server task, which is responsible for coordinating
* startup and shutdown of the server.
*/
result = isc_task_create(ns_g_taskmgr, ns_g_mctx, 0, &server_task);
if (result != ISC_R_SUCCESS)
goto cleanup_rootns;
result = isc_task_onshutdown(server_task, shutdown_server, ns_g_server);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
result = isc_app_onrun(ns_g_mctx, server_task, run_server, ns_g_server);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
return (ISC_R_SUCCESS);
/* XXXRTH Add zonemgr, and version view cleanups. */
cleanup_task:
isc_task_detach(&server_task);
cleanup_rootns:
ns_rootns_destroy();
return (result);
}
void
ns_server_fatal(isc_logmodule_t *module, isc_boolean_t want_core,
const char *format, ...)
{
va_list args;
va_start(args, format);
isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL, module,
ISC_LOG_CRITICAL, format, args);
va_end(args);
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_CRITICAL, "exiting (due to fatal error)");
if (want_core && ns_g_coreok)
abort();
exit(1);
}