server.c revision 6b99e640f34b3e9b3f2b4dcb10986f78e85fe1a4
5fec28507abad910acf4afa3efa1e634acab6d9eAutomatic Updater * Copyright (C) 1999 Internet Software Consortium.
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
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * copyright notice and this permission notice appear in all copies.
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
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrewstypedef struct {
d2ef84e07b67e72a4bd9c729c6b8228067d17584Mark Andrews/* XXX temporary kludge until TSIG/TKEY are objectified */
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrewsstatic isc_boolean_t tsig_initialized = ISC_FALSE;
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrewscreate_default_view(dns_c_ctx_t *cctx, isc_mem_t *mctx,
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews result = dns_view_create(mctx, rdclass, "_default", &view);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_cache_create(mctx, ns_g_taskmgr, ns_g_timermgr, rdclass,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews cleaning_interval = 3600; /* Default is 1 hour. */
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews (void) dns_c_ctx_getcleaninterval(cctx, &cleaning_interval);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews dns_cache_setcleaninginterval(cache, cleaning_interval);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * XXXRTH Temporary support for loading cache contents.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews result = dns_db_load(view->cachedb, ns_g_cachefile);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * XXXRTH hardwired number of tasks. Also, we'll need to
e2c3f8059e77a8e11c4378d22e5d8e78b423a28fMark Andrews * see if we are dealing with a shared dispatcher in this view.
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt result = dns_view_createresolver(view, ns_g_taskmgr, 31,
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence * We have default hints for class IN.
23e4260821eefa5019808e18e14e2b366461aad7Brian Wellington * Create the special view that handles queries for
23e4260821eefa5019808e18e14e2b366461aad7Brian Wellington * "version.bind. CH". The version string returned is that
23e4260821eefa5019808e18e14e2b366461aad7Brian Wellington * configured in 'configctx', or a compiled-in default if
23e4260821eefa5019808e18e14e2b366461aad7Brian Wellington * there is no "version" configuration option.
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssoncreate_version_view(dns_c_ctx_t *configctx, dns_view_t **viewp) {
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews (void) dns_c_ctx_getversion(configctx, &versiontext);
0cae66577c69c89086cd065bb297690072b471b4Mark Andrews dns_rdata_fromregion(&rdata, dns_rdataclass_ch, dns_rdatatype_txt, &r);
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews result = dns_zone_setorigin(zone, "version.bind.");
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = dns_db_create(ns_g_mctx, "rbt", origin, ISC_FALSE,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_difftuple_create(ns_g_mctx, DNS_DIFFOP_ADD, origin,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = dns_view_create(ns_g_mctx, dns_rdataclass_ch, "_version",
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = dns_zone_replacedb(zone, db, ISC_FALSE);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews /* Transfer ownership. */
72dbc7216aae3626a66e6154443be219f5edcaf0Mark Andrewsload_zone(dns_c_ctx_t *ctx, dns_c_zone_t *czone, dns_c_view_t *cview,
72dbc7216aae3626a66e6154443be219f5edcaf0Mark Andrews * Load (or reload) a zone.
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews * Find the view.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_viewlist_find(&lctx->viewlist, cview->name,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_viewlist_find(&lctx->viewlist, "_default",
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * Create a default view.
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews * Do we already have a production version of this view?
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews RWLOCK(&ns_g_server->viewlock, isc_rwlocktype_read);
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews result = dns_viewlist_find(&ns_g_server->viewlist,
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews RWUNLOCK(&ns_g_server->viewlock, isc_rwlocktype_read);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews * Create a new zone structure and configure it.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_zone_configure(ns_g_lctx, ctx, lctx->aclconf,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * Check for duplicates in the new zone table.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_view_findzone(view, origin, &tzone);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * We already have this zone!
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * Do we have the zone in the production view?
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_view_findzone(pview, origin, &tzone);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * If the production zone's configuration is
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * the same as the new zone's, we can use the
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * production zone.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * This is a new zone.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_zonemgr_managezone(ns_g_zonemgr, zone);
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews/* XXX will need error recovery for reconfig */
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrewsconfigure_server_acl(dns_c_ctx_t *cctx, dns_aclconfctx_t *actx, isc_mem_t *mctx,
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews isc_result_t (*getcacl)(dns_c_ctx_t *, dns_c_ipmatchlist_t **),
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington result = dns_acl_fromconfig(cacl, cctx, actx, mctx, aclp);
d8fc410cf830606a82c7ca169714d2f489b19d77Mark Andrews ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington "server ACL setup failed");
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonload_configuration(const char *filename, ns_server_t *server) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ea8278253210df030a24f0c89342b43fe279a127Mark Andrews ISC_LOG_INFO, "loading configuration from '%s'",
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington result = dns_c_parse_namedconf(filename, ns_g_mctx, &configctx,
a27b3757fdd8976ce05e37f391ad9e7ac4638e5dMark Andrews ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
a27b3757fdd8976ce05e37f391ad9e7ac4638e5dMark Andrews * Configure various server options.
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington (void) dns_c_ctx_getrecursion(configctx, &server->recursion);
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington (void) dns_c_ctx_getauthnxdomain(configctx, &server->auth_nxdomain);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson (void) dns_c_ctx_gettransferformat(configctx, &server->transfer_format);
18b7133679efa8f60fd4e396c628576f3f416b3eBrian Wellington configure_server_acl(configctx, &aclconfctx, ns_g_mctx,
8839b6acbf816fedc15b8e9e1c71fd606a9cd8eaBrian Wellington configure_server_acl(configctx, &aclconfctx, ns_g_mctx,
0cae66577c69c89086cd065bb297690072b471b4Mark Andrews dns_c_ctx_getrecursionacl, &server->recursionacl);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews configure_server_acl(configctx, &aclconfctx, ns_g_mctx,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_c_ctx_gettransferacl, &server->transferacl);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * If we haven't created any views, create a default view for class
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * IN. (We're a caching-only server.)
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews result = create_default_view(configctx, ns_g_mctx,
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington "could not create default view");
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Freeze the views.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * Create the version view.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington result = create_version_view(configctx, &view);
7d116211ec7b063891130f191e3ed437b45dba70Mark Andrews ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
7d116211ec7b063891130f191e3ed437b45dba70Mark Andrews "could not create version view");
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Change directory.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = isc_dir_chdir(configctx->options->directory);
d8fc410cf830606a82c7ca169714d2f489b19d77Mark Andrews ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
d8fc410cf830606a82c7ca169714d2f489b19d77Mark Andrews "change directory to '%s' failed: %s",
ea8278253210df030a24f0c89342b43fe279a127Mark Andrews * Load zones.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Force zone maintenance. Do this after loading
a05f23d07e1b60a1d88119678111a47014480611Mark Andrews * so that we know when we need to force AXFR of
a05f23d07e1b60a1d88119678111a47014480611Mark Andrews * slave zones whose master files are missing.
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington * Put the configuration into production.
18b7133679efa8f60fd4e396c628576f3f416b3eBrian Wellington RWLOCK(&server->viewlock, isc_rwlocktype_write);
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington RWUNLOCK(&server->viewlock, isc_rwlocktype_write);
d8fc410cf830606a82c7ca169714d2f489b19d77Mark Andrews * Cleanup old configuration.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Load the TSIG information from the configuration
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = dns_tsig_init(ns_g_lctx, configctx, ns_g_mctx);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews "dns_tsig_init() failed: %s",
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Load the TKEY information from the configuration
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = dns_tkey_init(ns_g_lctx, configctx, ns_g_mctx);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington "dns_tkey_init() failed: %s",
7d116211ec7b063891130f191e3ed437b45dba70Mark Andrews * Rescan the interface list to pick up changes in the
7d116211ec7b063891130f191e3ed437b45dba70Mark Andrews * listen-on option.
e2c3f8059e77a8e11c4378d22e5d8e78b423a28fMark Andrewsrun_server(isc_task_t *task, isc_event_t *event) {
e2c3f8059e77a8e11c4378d22e5d8e78b423a28fMark Andrews ns_server_t *server = (ns_server_t *) event->arg;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
8d414d155953f89a4eff40f16878438a8c9228f3Mark Andrewsshutdown_server(isc_task_t *task, isc_event_t *event) {
8d414d155953f89a4eff40f16878438a8c9228f3Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews RWLOCK(&server->viewlock, isc_rwlocktype_write);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews RWUNLOCK(&server->viewlock, isc_rwlocktype_write);
60ab03125c137c48a6b2ed6df1d2c8657757e09dMark Andrews ns_interfacemgr_shutdown(server->interfacemgr);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsns_server_create(isc_mem_t *mctx, ns_server_t **serverp) {
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ns_server_t *server = isc_mem_get(mctx, sizeof(*server));
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews /* Initialize configuration data with default values. */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington server->auth_nxdomain = ISC_FALSE; /* Was true in BIND 8 */
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington /* XXX these values are for debugging only */
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington result = isc_quota_init(&server->xfroutquota, 1);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington result = isc_quota_init(&server->recursionquota, 1);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington /* Initialize server data structures. */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington result = isc_rwlock_init(&server->viewlock, 0, 0);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington result = ns_interfacemgr_create(ns_g_mctx, ns_g_taskmgr,
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington "ns_interfacemgr_create() failed: %s",
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /* XXX cleanup */
0cae66577c69c89086cd065bb297690072b471b4Mark Andrews isc_mem_put(server->mctx, server, sizeof(*server));
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington * Create the server object.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = ns_server_create(ns_g_mctx, &ns_g_server);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Setup default root server hints.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = dns_zonemgr_create(ns_g_mctx, ns_g_taskmgr, ns_g_timermgr,
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Setup the server task, which is responsible for coordinating
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * startup and shutdown of the server.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = isc_task_create(ns_g_taskmgr, ns_g_mctx, 0, &server_task);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = isc_task_onshutdown(server_task, shutdown_server, ns_g_server);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews result = isc_app_onrun(ns_g_mctx, server_task, run_server, ns_g_server);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews /* XXXRTH Add zonemgr, and version view cleanups. */
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrewsns_server_fatal(isc_logmodule_t *module, isc_boolean_t want_core,
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews const char *format, ...)
515c7f3c43f76d7b439905b18009105364b36100Automatic Updater isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL, module,
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews ISC_LOG_CRITICAL, "exiting (due to fatal error)");