server.c revision 92583b722bfdcc5714dfd77711c54fee55f51c5d
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews/*
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * Copyright (C) 1999 Internet Software Consortium.
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews *
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * Permission to use, copy, modify, and distribute this software for any
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * purpose with or without fee is hereby granted, provided that the above
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * copyright notice and this permission notice appear in all copies.
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews *
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * SOFTWARE.
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews */
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <config.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <stdio.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <stdlib.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <unistd.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <string.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <isc/assertions.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <isc/error.h>
8b61d2012063306528286680bd9f086fa868d86eMark Andrews#include <isc/rwlock.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <isc/mem.h>
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellington#include <isc/task.h>
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellington#include <isc/thread.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <isc/result.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <isc/socket.h>
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellington#include <isc/timer.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <isc/app.h>
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <dns/types.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <dns/result.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <dns/master.h>
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews#include <dns/name.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <dns/fixedname.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <dns/rdata.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <dns/rdatalist.h>
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews#include <dns/rdataset.h>
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews#include <dns/rdatasetiter.h>
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews#include <dns/compress.h>
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews#include <dns/db.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <dns/dbtable.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <dns/message.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <dns/journal.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <dns/view.h>
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <named/types.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <named/globals.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <named/rootns.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <named/server.h>
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include <named/xfrin.h>
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff#include "../../isc/util.h" /* XXXRTH */
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graffstatic isc_task_t * server_task;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graffstatic dns_db_t * version_db;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graffstatic dns_view_t * version_view;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graffstatic dns_result_t
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graffload(ns_dbinfo_t *dbi, char *view_name) {
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_fixedname_t forigin;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_name_t *origin;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_result_t result;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff isc_buffer_t source;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff size_t len;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff dns_view_t *view;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /*
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff * XXXRTH View list code will move to its own module soon.
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff */
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff RWLOCK(&ns_g_viewlock, isc_rwlocktype_read);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff for (view = ISC_LIST_HEAD(ns_g_viewlist);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff view != NULL;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff view = ISC_LIST_NEXT(view, link)) {
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff if (strcasecmp(view_name, view->name) == 0) {
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington dns_view_attach(view, &dbi->view);
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellington break;
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellington }
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews }
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_read);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews if (view == NULL)
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews return (DNS_R_NOTFOUND);
7ed73313b1c129c7134d7d33beb82c85bd6d1df4Mark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews len = strlen(dbi->origin);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews isc_buffer_init(&source, dbi->origin, len, ISC_BUFFERTYPE_TEXT);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews isc_buffer_add(&source, len);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews dns_fixedname_init(&forigin);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews origin = dns_fixedname_name(&forigin);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews result = dns_name_fromtext(origin, &source, dns_rootname, ISC_FALSE,
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews NULL);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != DNS_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto view_detach;
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews result = dns_db_create(ns_g_mctx, "rbt", origin, dbi->iscache,
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews view->rdclass, 0, NULL, &dbi->db);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != DNS_R_SUCCESS)
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews goto view_detach;
59abb512d344bfa09012cc11b7d814966f035da4Mark Andrews
59abb512d344bfa09012cc11b7d814966f035da4Mark Andrews printf("loading %s (%s)\n", dbi->path, dbi->origin);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews result = dns_db_load(dbi->db, dbi->path);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews if (result != DNS_R_SUCCESS) {
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews if (dbi->isslave) {
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews /* Ignore the error, just leave dbi->db == NULL. */
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews dns_db_detach(&dbi->db);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews return (DNS_R_SUCCESS);
8b61d2012063306528286680bd9f086fa868d86eMark Andrews } else {
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto db_detach;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews }
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews }
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews printf("loaded\n");
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews printf("journal rollforward\n");
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews result = dns_journal_rollforward(ns_g_mctx, dbi->db, "journal");
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews if (result != DNS_R_SUCCESS) {
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff "ns_rollforward(): %s",
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff dns_result_totext(result));
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews /* Continue anyway... */
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews }
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews if (dbi->iscache)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_setcachedb(view, dbi->db);
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews else if (dns_view_addzonedb(view, dbi->db) != DNS_R_SUCCESS)
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews goto db_detach;
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (DNS_R_SUCCESS);
8b61d2012063306528286680bd9f086fa868d86eMark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews db_detach:
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_db_detach(&dbi->db);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews view_detach:
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff dns_view_detach(&dbi->view);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (result);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews}
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrewsstatic isc_result_t
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrewsload_version(void) {
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_fixedname_t forigin;
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews dns_name_t *origin;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_result_t result, eresult;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_buffer_t source;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews size_t len;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews int soacount, nscount;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_rdatacallbacks_t callbacks;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_t *view;
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews char version_text[1024];
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews sprintf(version_text, "version 0 CHAOS TXT \"%s\"\n", ns_g_version);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews /*
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews * XXXRTH View list code will move to its own module soon.
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff RWLOCK(&ns_g_viewlock, isc_rwlocktype_read);
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews for (view = ISC_LIST_HEAD(ns_g_viewlist);
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews view != NULL;
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews view = ISC_LIST_NEXT(view, link)) {
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (strcasecmp(view->name, "default/CHAOS") == 0) {
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews version_view = NULL;
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews dns_view_attach(view, &version_view);
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews break;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews }
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews }
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_read);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff if (view == NULL)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (DNS_R_NOTFOUND);
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews len = strlen("bind.");
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_buffer_init(&source, "bind.", len, ISC_BUFFERTYPE_TEXT);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_buffer_add(&source, len);
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews dns_fixedname_init(&forigin);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews origin = dns_fixedname_name(&forigin);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews result = dns_name_fromtext(origin, &source, dns_rootname, ISC_FALSE,
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews NULL);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (result != DNS_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto view_detach;
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews version_db = NULL;
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews result = dns_db_create(ns_g_mctx, "rbt", origin, ISC_FALSE,
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews view->rdclass, 0, NULL, &version_db);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != DNS_R_SUCCESS)
8b61d2012063306528286680bd9f086fa868d86eMark Andrews goto view_detach;
8b61d2012063306528286680bd9f086fa868d86eMark Andrews
8b61d2012063306528286680bd9f086fa868d86eMark Andrews dns_rdatacallbacks_init(&callbacks);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews len = strlen(version_text);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_buffer_init(&source, version_text, len, ISC_BUFFERTYPE_TEXT);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_buffer_add(&source, len);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews result = dns_db_beginload(version_db, &callbacks.add,
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews &callbacks.add_private);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != DNS_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (result);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews result = dns_master_loadbuffer(&source, &version_db->origin,
8b61d2012063306528286680bd9f086fa868d86eMark Andrews &version_db->origin,
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews version_db->rdclass, ISC_FALSE,
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews &soacount, &nscount, &callbacks,
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews version_db->mctx);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews eresult = dns_db_endload(version_db, &callbacks.add_private);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result == ISC_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews result = eresult;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != ISC_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto db_detach;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (dns_view_addzonedb(version_view, version_db) != DNS_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto db_detach;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (DNS_R_SUCCESS);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews db_detach:
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_db_detach(&version_db);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews view_detach:
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff dns_view_detach(&version_view);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (result);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews}
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrewsstatic isc_result_t
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrewsload_all(void) {
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_result_t result = ISC_R_SUCCESS;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews ns_dbinfo_t *dbi;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_t *view;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
8b61d2012063306528286680bd9f086fa868d86eMark Andrews result = load_version();
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (result != ISC_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (result);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews for (dbi = ISC_LIST_HEAD(ns_g_dbs);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews dbi != NULL;
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews dbi = ISC_LIST_NEXT(dbi, link)) {
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews result = load(dbi, "default/IN");
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews if (result != ISC_R_SUCCESS)
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews break;
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews }
8b61d2012063306528286680bd9f086fa868d86eMark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (result == ISC_R_SUCCESS) {
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews RWLOCK(&ns_g_viewlock, isc_rwlocktype_read);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews for (view = ISC_LIST_HEAD(ns_g_viewlist);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews view != NULL;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews view = ISC_LIST_NEXT(view, link))
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_freeze(view);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_read);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews }
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews return (result);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews}
4be19dcd14cea678511f1d1b269ab89273e987eeMark Andrews
4be19dcd14cea678511f1d1b269ab89273e987eeMark Andrewsstatic void
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrewsunload_all(void) {
4be19dcd14cea678511f1d1b269ab89273e987eeMark Andrews ns_dbinfo_t *dbi, *dbi_next;
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews for (dbi = ISC_LIST_HEAD(ns_g_dbs); dbi != NULL; dbi = dbi_next) {
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews dbi_next = ISC_LIST_NEXT(dbi, link);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (dbi->view != NULL) {
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews INSIST(dbi->db != NULL);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews dns_db_detach(&dbi->db);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews dns_view_detach(&dbi->view);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews }
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews isc_mem_free(ns_g_mctx, dbi->path);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews isc_mem_free(ns_g_mctx, dbi->origin);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (dbi->master != NULL)
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews isc_mem_free(ns_g_mctx, dbi->master);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews ISC_LIST_UNLINK(ns_g_dbs, dbi, link);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews isc_mem_put(ns_g_mctx, dbi, sizeof *dbi);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews }
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (version_view != NULL) {
8b61d2012063306528286680bd9f086fa868d86eMark Andrews INSIST(version_db != NULL);
8b61d2012063306528286680bd9f086fa868d86eMark Andrews dns_db_detach(&version_db);
8b61d2012063306528286680bd9f086fa868d86eMark Andrews dns_view_detach(&version_view);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews }
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews}
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrewsstatic void
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrewsload_configuration(void) {
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews isc_result_t result;
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews /*
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews * XXXRTH loading code below is temporary; it
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews * will be replaced by proper config file processing.
8b61d2012063306528286680bd9f086fa868d86eMark Andrews */
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews result = load_all();
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (result != ISC_R_SUCCESS) {
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews /* XXXRTH */
8b61d2012063306528286680bd9f086fa868d86eMark Andrews printf("load_all(): %s\n", isc_result_totext(result));
8b61d2012063306528286680bd9f086fa868d86eMark Andrews }
8b61d2012063306528286680bd9f086fa868d86eMark Andrews
8b61d2012063306528286680bd9f086fa868d86eMark Andrews ns_interfacemgr_scan(ns_g_interfacemgr);
4be19dcd14cea678511f1d1b269ab89273e987eeMark Andrews}
4be19dcd14cea678511f1d1b269ab89273e987eeMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrewsstatic void
4be19dcd14cea678511f1d1b269ab89273e987eeMark Andrewsrun_server(isc_task_t *task, isc_event_t *event) {
4be19dcd14cea678511f1d1b269ab89273e987eeMark Andrews (void)task;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews printf("server running\n");
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews load_configuration();
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews isc_event_free(&event);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews}
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrewsstatic void
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrewsshutdown_server(isc_task_t *task, isc_event_t *event) {
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_t *view, *view_next;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews (void)task;
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews printf("server shutting down\n");
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews RWLOCK(&ns_g_viewlock, isc_rwlocktype_write);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews unload_all();
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews for (view = ISC_LIST_HEAD(ns_g_viewlist);
8b61d2012063306528286680bd9f086fa868d86eMark Andrews view != NULL;
8b61d2012063306528286680bd9f086fa868d86eMark Andrews view = view_next) {
8b61d2012063306528286680bd9f086fa868d86eMark Andrews view_next = ISC_LIST_NEXT(view, link);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews ISC_LIST_UNLINK(ns_g_viewlist, view, link);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_detach(&view);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews }
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews ISC_LIST_INIT(ns_g_viewlist);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews RWUNLOCK(&ns_g_viewlock, isc_rwlocktype_write);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_task_detach(&server_task);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews ns_rootns_destroy();
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews isc_event_free(&event);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews}
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrewsisc_result_t
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrewsns_server_init(void) {
8b61d2012063306528286680bd9f086fa868d86eMark Andrews isc_result_t result;
8b61d2012063306528286680bd9f086fa868d86eMark Andrews dns_view_t *view, *view_next;
8b61d2012063306528286680bd9f086fa868d86eMark Andrews
8b61d2012063306528286680bd9f086fa868d86eMark Andrews result = ns_rootns_init();
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews if (result != ISC_R_SUCCESS)
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews return (result);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews /*
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * XXXRTH The view management code here will probably move to its
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews * own module when we start using the config file.
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews */
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews view = NULL;
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews result = dns_view_create(ns_g_mctx, dns_rdataclass_in, "default/IN",
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews &view);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != ISC_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto cleanup_views;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews ISC_LIST_APPEND(ns_g_viewlist, view, link);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_sethints(view, ns_g_rootns);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews view = NULL;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews result = dns_view_create(ns_g_mctx, dns_rdataclass_ch, "default/CHAOS",
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews &view);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != ISC_R_SUCCESS)
8b61d2012063306528286680bd9f086fa868d86eMark Andrews goto cleanup_views;
8b61d2012063306528286680bd9f086fa868d86eMark Andrews ISC_LIST_APPEND(ns_g_viewlist, view, link);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews result = isc_task_create(ns_g_taskmgr, ns_g_mctx, 0, &server_task);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != ISC_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto cleanup_views;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews result = isc_task_onshutdown(server_task, shutdown_server, NULL);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff if (result != ISC_R_SUCCESS)
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff goto cleanup_task;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff result = isc_app_onrun(ns_g_mctx, server_task, run_server, NULL);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews if (result != ISC_R_SUCCESS)
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews goto cleanup_task;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (ISC_R_SUCCESS);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews cleanup_task:
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews isc_task_detach(&server_task);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews cleanup_views:
8b61d2012063306528286680bd9f086fa868d86eMark Andrews for (view = ISC_LIST_HEAD(ns_g_viewlist);
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews view != NULL;
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews view = view_next) {
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews view_next = ISC_LIST_NEXT(view, link);
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews ISC_LIST_UNLINK(ns_g_viewlist, view, link);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews dns_view_detach(&view);
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews }
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews ns_rootns_destroy();
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews return (result);
a5c30de2601a1d130a15a78cf3dc7610a02b2d85Mark Andrews}
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews