2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00
bind/bin/named/server.c

538 lines
12 KiB
C
Raw Normal View History

/*
1999-03-06 03:55:54 +00:00
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/rwlock.h>
#include <isc/mem.h>
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/result.h>
#include <isc/socket.h>
#include <isc/timer.h>
1999-05-27 01:51:31 +00:00
#include <isc/app.h>
#include <dns/confparser.h>
#include <dns/types.h>
#include <dns/result.h>
#include <dns/master.h>
#include <dns/name.h>
1999-05-03 19:56:23 +00:00
#include <dns/fixedname.h>
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
1999-05-03 19:56:23 +00:00
#include <dns/rdatasetiter.h>
#include <dns/compress.h>
1999-01-31 12:31:31 +00:00
#include <dns/db.h>
1999-05-03 19:56:23 +00:00
#include <dns/dbtable.h>
#include <dns/message.h>
#include <dns/journal.h>
#include <dns/view.h>
#include <dns/zone.h>
1999-07-24 01:16:38 +00:00
#include <named/types.h>
#include <named/globals.h>
1999-10-22 19:35:19 +00:00
#include <named/log.h>
1999-09-24 01:42:22 +00:00
#include <named/rootns.h>
1999-07-24 01:16:38 +00:00
#include <named/server.h>
1999-08-27 18:30:00 +00:00
#include <named/xfrin.h>
#include "../../isc/util.h" /* XXXRTH */
typedef struct {
isc_mem_t * mctx;
dns_viewlist_t viewlist;
} ns_load_t;
1999-07-24 01:16:38 +00:00
static isc_task_t * server_task;
static dns_view_t * version_view;
1999-01-31 12:31:31 +00:00
static isc_result_t
create_default_view(isc_mem_t *mctx, dns_rdataclass_t rdclass,
dns_view_t **viewp)
{
dns_view_t *view;
dns_db_t *db;
isc_result_t result;
REQUIRE(viewp != NULL && *viewp == NULL);
/*
* View.
*/
view = NULL;
1999-10-22 19:35:19 +00:00
result = dns_view_create(mctx, rdclass, "_default", &view);
if (result != ISC_R_SUCCESS)
return (result);
/*
* Cache.
*/
db = NULL;
result = dns_db_create(mctx, "rbt", dns_rootname, ISC_TRUE,
rdclass, 0, NULL, &db);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_view_setcachedb(view, db);
dns_db_detach(&db);
1999-05-03 19:56:23 +00:00
/*
* Resolver.
*
* XXXRTH hardwired number of tasks. Also, we'll need to
* see if we are dealing with a shared dispatcher in this view.
*/
result = dns_view_createresolver(view, ns_g_taskmgr, 16,
ns_g_socketmgr, ns_g_timermgr,
NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
/*
* We have default hints for class IN.
*/
if (rdclass == dns_rdataclass_in)
dns_view_sethints(view, ns_g_rootns);
*viewp = view;
1999-10-14 01:37:00 +00:00
return (ISC_R_SUCCESS);
cleanup:
dns_view_detach(&view);
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;
isc_boolean_t need_load;
/*
* Load (or reload) a zone.
*/
lctx = uap;
tzone = NULL;
zone = NULL;
pview = NULL;
need_load = ISC_TRUE;
/*
* 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_copy(ctx, 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;
}
1999-05-03 19:56:23 +00:00
/*
* 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);
need_load = ISC_FALSE;
} 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 (need_load) {
/*
* XXXRTH What should we do about errors? We don't
* want to fail the whole config file load just
* because we couldn't get a zone.
*/
1999-10-19 19:14:14 +00:00
result = dns_zone_load(zone);
if (result != ISC_R_SUCCESS) {
fprintf(stderr, "dns_zone_load failed: %s\n",
isc_result_totext(result));
}
}
1999-10-14 01:37:00 +00:00
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);
1999-05-03 19:56:23 +00:00
}
1999-07-24 01:16:38 +00:00
static isc_result_t
load_configuration(const char *filename) {
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;
lctx.mctx = ns_g_mctx;
ISC_LIST_INIT(lctx.viewlist);
1999-07-24 01:16:38 +00:00
callbacks.zonecbk = load_zone;
callbacks.zonecbkuap = &lctx;
callbacks.optscbk = NULL;
callbacks.optscbkuap = NULL;
1999-09-24 01:42:22 +00:00
1999-10-22 19:35:19 +00:00
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "Loading '%s'", ns_g_conffile);
1999-07-24 01:16:38 +00:00
configctx = NULL;
1999-10-22 19:35:19 +00:00
result = dns_c_parse_namedconf(ns_g_lctx,
filename, ns_g_mctx, &configctx,
&callbacks);
if (result != ISC_R_SUCCESS) {
return (result);
1999-05-03 19:56:23 +00:00
}
/*
* Create default view, if required.
*/
/*
* Freeze the views.
*/
for (view = ISC_LIST_HEAD(lctx.viewlist);
view != NULL;
view = ISC_LIST_NEXT(view, link))
dns_view_freeze(view);
/*
* Attach the version view.
*/
view = NULL;
dns_view_attach(version_view, &view);
ISC_LIST_APPEND(lctx.viewlist, view, link);
1999-01-31 12:31:31 +00:00
/*
* Load zones. (???)
1999-07-24 01:16:38 +00:00
*/
1999-01-31 12:31:31 +00:00
/*
* Put the configuration into production.
*/
RWLOCK(&ns_g_viewlock, isc_rwlocktype_write);
oviewlist = ns_g_viewlist;
ns_g_viewlist = lctx.viewlist;
oconfigctx = ns_g_confctx;
ns_g_confctx = configctx;
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);
1999-07-24 01:16:38 +00:00
}
if (oconfigctx != NULL)
dns_c_ctx_delete(NULL /* XXX isc_log_t */, &oconfigctx);
return (ISC_R_SUCCESS);
1999-07-24 01:16:38 +00:00
}
1999-07-24 01:16:38 +00:00
static void
run_server(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
1999-07-24 01:16:38 +00:00
(void)task;
1999-10-22 19:35:19 +00:00
isc_event_free(&event);
result = load_configuration(ns_g_conffile);
if (result != ISC_R_SUCCESS) {
1999-10-22 19:35:19 +00:00
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_CRITICAL,
"Load of '%s' failed: %s", ns_g_conffile,
isc_result_totext(result));
isc_app_shutdown();
1999-10-22 19:35:19 +00:00
return;
}
ns_interfacemgr_scan(ns_g_interfacemgr);
1999-10-22 19:35:19 +00:00
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "Running");
1999-07-24 01:16:38 +00:00
}
1999-01-28 05:52:20 +00:00
static isc_result_t
create_version_view(void) {
dns_view_t *view;
dns_zone_t *zone;
dns_db_t *db;
dns_name_t *origin;
dns_result_t result, eresult;
isc_buffer_t source;
size_t len;
int soacount, nscount;
dns_rdatacallbacks_t callbacks;
char version_text[1024];
(void)sprintf(version_text, "version 0 CHAOS TXT \"%s\"\n",
ns_g_version);
view = NULL;
result = dns_view_create(ns_g_mctx, dns_rdataclass_ch, "_version",
&view);
if (result != ISC_R_SUCCESS)
return (result);
zone = NULL;
result = dns_zone_create(&zone, ns_g_mctx);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_zone_setorigin(zone, "bind.");
if (result != ISC_R_SUCCESS)
goto cleanup;
origin = dns_zone_getorigin(zone);
db = NULL;
result = dns_db_create(ns_g_mctx, "rbt", origin, ISC_FALSE,
view->rdclass, 0, NULL, &db);
if (result != ISC_R_SUCCESS)
goto cleanup;
len = strlen(version_text);
isc_buffer_init(&source, version_text, len, ISC_BUFFERTYPE_TEXT);
isc_buffer_add(&source, len);
dns_rdatacallbacks_init(&callbacks);
result = dns_db_beginload(db, &callbacks.add, &callbacks.add_private);
if (result != DNS_R_SUCCESS)
return (result);
result = dns_master_loadbuffer(&source, &db->origin, &db->origin,
db->rdclass, ISC_FALSE,
&soacount, &nscount, &callbacks,
ns_g_mctx);
eresult = dns_db_endload(db, &callbacks.add_private);
if (result != ISC_R_SUCCESS)
result = eresult;
if (result != ISC_R_SUCCESS)
goto cleanup;
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);
version_view = view;
view = NULL;
result = ISC_R_SUCCESS;
cleanup:
if (db != NULL)
dns_db_detach(&db);
if (zone != NULL)
dns_zone_detach(&zone);
if (view != NULL)
dns_view_detach(&view);
return (result);
}
1999-07-24 01:16:38 +00:00
static void
shutdown_server(isc_task_t *task, isc_event_t *event) {
dns_view_t *view, *view_next;
1999-07-24 01:16:38 +00:00
(void)task;
1999-10-22 19:35:19 +00:00
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);
}
/*
* XXXRTH Is this the right place to do this?
*/
dns_c_ctx_delete(NULL /* XXX isc_log_t */, &ns_g_confctx);
RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_write);
1999-07-24 01:16:38 +00:00
isc_task_detach(&server_task);
isc_taskpool_destroy(&ns_g_zonetasks);
dns_view_detach(&version_view);
1999-09-24 01:42:22 +00:00
ns_rootns_destroy();
1999-07-24 01:16:38 +00:00
isc_event_free(&event);
}
1999-07-24 01:16:38 +00:00
isc_result_t
ns_server_init(void) {
isc_result_t result;
/*
* Setup default root server hints.
*/
1999-09-24 01:42:22 +00:00
result = ns_rootns_init();
if (result != ISC_R_SUCCESS)
return (result);
result = create_version_view();
1999-07-24 01:16:38 +00:00
if (result != ISC_R_SUCCESS)
return (result);
result = isc_taskpool_create(ns_g_taskmgr, ns_g_mctx, 8 /* XXX */,
0, &ns_g_zonetasks);
if (result != ISC_R_SUCCESS)
return (result);
/*
* Setup the server task, which is responsible for coordinating
* startup and shutdown of the server.
*/
1999-07-24 01:16:38 +00:00
result = isc_task_create(ns_g_taskmgr, ns_g_mctx, 0, &server_task);
if (result != ISC_R_SUCCESS)
goto cleanup_rootns;
1999-07-24 01:16:38 +00:00
result = isc_task_onshutdown(server_task, shutdown_server, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
result = isc_app_onrun(ns_g_mctx, server_task, run_server, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
1999-07-24 01:16:38 +00:00
return (ISC_R_SUCCESS);
1999-10-22 19:35:19 +00:00
/* XXXRTH Add taskpool, and version view cleanups. */
1999-07-24 01:16:38 +00:00
cleanup_task:
isc_task_detach(&server_task);
cleanup_rootns:
1999-09-24 01:42:22 +00:00
ns_rootns_destroy();
1999-07-24 01:16:38 +00:00
return (result);
}