server.c revision 24e49b2133ef3dd52839c56eab1c6b5c0d559bb0
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff *
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * SOFTWARE.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdio.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdlib.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <unistd.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <string.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdarg.h>
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/assertions.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/error.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/rwlock.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/mem.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/task.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/thread.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/result.h>
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User#include <isc/socket.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/timer.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/app.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/confparser.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/types.h>
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User#include <dns/result.h>
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User#include <dns/master.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/name.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/fixedname.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdata.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdatalist.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdataset.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdatasetiter.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/compress.h>
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User#include <dns/db.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/dbtable.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/message.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/journal.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/view.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/zone.h>
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User#include <dns/tsig.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/tkey.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <named/types.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <named/globals.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <named/log.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <named/rootns.h>
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User#include <named/server.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <named/xfrin.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User#include "../../isc/util.h" /* XXXRTH */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeintypedef struct {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mem_t * mctx;
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User dns_viewlist_t viewlist;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein} ns_load_t;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic isc_task_t * server_task;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic dns_view_t * version_view;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic isc_result_t
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeincreate_default_view(isc_mem_t *mctx, dns_rdataclass_t rdclass,
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User dns_view_t **viewp)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein{
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dns_view_t *view;
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User dns_db_t *db;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_t result;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein REQUIRE(viewp != NULL && *viewp == NULL);
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * View.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User view = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein result = dns_view_create(mctx, rdclass, "_default", &view);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (result != ISC_R_SUCCESS)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return (result);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Cache.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein db = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein result = dns_db_create(mctx, "rbt", dns_rootname, ISC_TRUE,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein rdclass, 0, NULL, &db);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (result != ISC_R_SUCCESS)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein goto cleanup;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dns_view_setcachedb(view, db);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_db_detach(&db);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * XXXRTH Temporary support for loading cache contents.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (ns_g_cachefile != NULL) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
c1ecf4ace2a59be40c68f3f9f58370e124f68763Brian Wellington NS_LOGMODULE_SERVER,
c1ecf4ace2a59be40c68f3f9f58370e124f68763Brian Wellington ISC_LOG_DEBUG(1), "loading cache '%s'",
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ns_g_cachefile);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_db_load(view->cachedb, ns_g_cachefile);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff goto cleanup;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff }
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Resolver.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff *
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * XXXRTH hardwired number of tasks. Also, we'll need to
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * see if we are dealing with a shared dispatcher in this view.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_view_createresolver(view, ns_g_taskmgr, 16,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ns_g_socketmgr, ns_g_timermgr,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff NULL);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff goto cleanup;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * We have default hints for class IN.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (rdclass == dns_rdataclass_in)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_sethints(view, ns_g_rootns);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff *viewp = view;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (ISC_R_SUCCESS);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cleanup:
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_detach(&view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffstatic isc_result_t
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffload_zone(dns_c_ctx_t *ctx, dns_c_zone_t *czone, dns_c_view_t *cview,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff void *uap)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ns_load_t *lctx;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_t *view, *tview, *pview;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_zone_t *zone, *tzone;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_name_t *origin;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff isc_result_t result;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Load (or reload) a zone.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff lctx = uap;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff tzone = NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff zone = NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff pview = NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Find the view.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (cview != NULL) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_viewlist_find(&lctx->viewlist, cview->name,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff czone->zclass, &view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff } else {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_viewlist_find(&lctx->viewlist, "_default",
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff czone->zclass, &view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result == ISC_R_NOTFOUND) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Create a default view.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein tview = NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = create_default_view(ctx->mem, czone->zclass,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff &tview);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_attach(tview, &view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ISC_LIST_APPEND(lctx->viewlist, view, link);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff } else if (result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Do we already have a production version of this view?
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff RWLOCK(&ns_g_viewlock, isc_rwlocktype_read);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_viewlist_find(&ns_g_viewlist, view->name, view->rdclass,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff &pview);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_read);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff goto cleanup;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Create a new zone structure and configure it.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_zone_create(&zone, lctx->mctx);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_zone_copy(ns_g_lctx, ctx, czone, zone);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_SUCCESS)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (dns_zone_gettype(zone) == dns_zone_hint) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein INSIST(0);
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff } else {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Check for duplicates in the new zone table.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff origin = dns_zone_getorigin(zone);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_view_findzone(view, origin, &tzone);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result == ISC_R_SUCCESS) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * We already have this zone!
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = ISC_R_EXISTS;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff goto cleanup;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff }
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Do we have the zone in the production view?
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (pview != NULL)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein result = dns_view_findzone(pview, origin, &tzone);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff else
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff result = ISC_R_NOTFOUND;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result == ISC_R_SUCCESS) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Yes.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff *
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * If the production zone's configuration is
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * the same as the new zone's, we can use the
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * production zone.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (dns_zone_equal(zone, tzone))
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_view_addzone(view, tzone);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff else
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_view_addzone(view, zone);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein } else if (result == ISC_R_NOTFOUND) {
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * This is a new zone.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_view_addzone(view, zone);
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff }
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff }
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cleanup:
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (tzone != NULL)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_zone_detach(&tzone);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (zone != NULL)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_zone_detach(&zone);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (pview != NULL)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_detach(&pview);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (view != NULL)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_detach(&view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffstatic void
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graffload_configuration(const char *filename) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff isc_result_t result;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ns_load_t lctx;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_c_cbks_t callbacks;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_c_ctx_t *configctx, *oconfigctx;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_t *view, *view_next;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_viewlist_t oviewlist;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff lctx.mctx = ns_g_mctx;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ISC_LIST_INIT(lctx.viewlist);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff callbacks.zonecbk = load_zone;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff callbacks.zonecbkuap = &lctx;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff callbacks.optscbk = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein callbacks.optscbkuap = NULL;
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ISC_LOG_INFO, "loading '%s'", filename);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff configctx = NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = dns_c_parse_namedconf(ns_g_lctx,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff filename, ns_g_mctx, &configctx,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff &callbacks);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (result != ISC_R_SUCCESS) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff#ifdef notyet
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff for (view = ISC_LIST_HEAD(lctx.viewlist);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view != NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view = view_next) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view_next = ISC_LIST_NEXT(view, link);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ISC_LIST_UNLINK(lctx.viewlist, view, link);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_detach(&view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff }
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff#endif
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ns_server_fatal(NS_LOGMODULE_SERVER, ISC_FALSE,
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff "load of '%s' failed", filename);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * XXXRTH Create default view, if required.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Freeze the views.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff for (view = ISC_LIST_HEAD(lctx.viewlist);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view != NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view = ISC_LIST_NEXT(view, link))
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_freeze(view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Attach the version view.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dns_view_attach(version_view, &view);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff ISC_LIST_APPEND(lctx.viewlist, view, link);
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Load zones.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff for (view = ISC_LIST_HEAD(lctx.viewlist);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view != NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view = view_next) {
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff view_next = ISC_LIST_NEXT(view, link);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff dns_view_load(view);
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt }
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence /*
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff * Put the configuration into production.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff RWLOCK(&ns_g_viewlock, isc_rwlocktype_write);
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff oviewlist = ns_g_viewlist;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff ns_g_viewlist = lctx.viewlist;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff oconfigctx = ns_g_confctx;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff ns_g_confctx = configctx;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_write);
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff /*
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff * Cleanup old configuration.
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
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);
}
if (oconfigctx != NULL)
dns_c_ctx_delete(ns_g_lctx, &oconfigctx);
/*
* Load the TSIG information from the configuration
*/
result = dns_tsig_init(ns_g_confctx, 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, ns_g_confctx, 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));
}
}
static void
run_server(isc_task_t *task, isc_event_t *event) {
(void)task;
isc_event_free(&event);
load_configuration(ns_g_conffile);
ns_interfacemgr_scan(ns_g_interfacemgr);
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
ISC_LOG_INFO, "running");
}
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);
}
static void
shutdown_server(isc_task_t *task, isc_event_t *event) {
dns_view_t *view, *view_next;
(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);
}
/*
* XXXRTH Is this the right place to do this?
*/
dns_c_ctx_delete(ns_g_lctx, &ns_g_confctx);
RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_write);
isc_task_detach(&server_task);
isc_taskpool_destroy(&ns_g_zonetasks);
dns_view_detach(&version_view);
ns_rootns_destroy();
isc_event_free(&event);
}
isc_result_t
ns_server_init(void) {
isc_result_t result;
/*
* Setup default root server hints.
*/
result = ns_rootns_init();
if (result != ISC_R_SUCCESS)
return (result);
result = create_version_view();
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.
*/
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, 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;
return (ISC_R_SUCCESS);
/* XXXRTH Add taskpool, 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);
}