zone.c revision 24b22ddce73e79214161730edd2f43704f10d48f
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister/*
d7201de09b85929a86b157f4b2d91667c68c6b52Automatic Updater * Copyright (C) 1999 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
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
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * copyright notice and this permission notice appear in all copies.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister *
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.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
d7201de09b85929a86b157f4b2d91667c68c6b52Automatic Updater /* $Id: zone.c,v 1.44 1999/12/13 07:56:26 marka Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <string.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <../isc/util.h> /* XXX MPA */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/assertions.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <isc/error.h>
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉#include <isc/magic.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <isc/print.h>
b3e77535185043f089b346166440402d092030c3David Lawrence#include <isc/rwlock.h>
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington#include <isc/serial.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <isc/taskpool.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <isc/timer.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/confparser.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <dns/db.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <dns/dbiterator.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <dns/dispatch.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <dns/journal.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <dns/master.h>
72166c5cf59db011cce11e98abe377f5aa6f7052Andreas Gustafsson#include <dns/message.h>
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister#include <dns/rcode.h>
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington#include <dns/rdata.h>
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews#include <dns/rdatalist.h>
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews#include <dns/rdatasetiter.h>
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews#include <dns/rdatastruct.h>
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <dns/resolver.h>
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson#include <dns/xfrin.h>
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson#include <dns/zone.h>
b3e77535185043f089b346166440402d092030c3David Lawrence#include <dns/zt.h>
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson/* XXX remove once config changes are in place */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define dns_zone_uptodate(x) zone_log(x, me, ISC_LOG_INFO, "dns_zone_uptodate")
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define referral(x) ISC_FALSE
b3e77535185043f089b346166440402d092030c3David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#include <stdarg.h>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define ZONE_MAGIC 0x5a4f4e45U
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#define CHECKSERVERS_MAGIC 0x43484346U
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson#define DNS_ZONE_VALID(zone) \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_MAGIC_VALID(zone, ZONE_MAGIC)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#define DNS_CHECKSERVERS_VALID(server) \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_MAGIC_VALID(zone, CHECKSERVERS_MAGIC)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#ifndef DNS_GLOBAL_OPTION /* XXX MPA */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_GLOBAL_OPTION(o) 0
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#endif
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#define DEFAULT_REFRESH 900 /*XXX*/
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DEFAULT_RETRY 300 /*XXX*/
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define MAX_XFER_TIME (2*3600) /* Documented default is 2 hours. */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define RANGE(a, b, c) (((a) < (b)) ? (b) : ((a) < (c) ? (a) : (c)))
b3e77535185043f089b346166440402d092030c3David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_MIN_REFRESH 2
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_MAX_REFRESH 2419200 /* 4 weeks */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_MIN_RETRY 1
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_MAX_RETRY 1209600 /* 2 weeks */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_MAX_EXPIRE 14515200 /* 24 weeks */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#define DNS_DEFAULT_IDLEIN 3600 /* 1 hour */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_DEFAULT_IDLEOUT 3600 /* 1 hour */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristertypedef enum {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister get_a6, get_aaaa, get_a, get_ns, get_soa
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister} dns_zone_state_t;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
b3e77535185043f089b346166440402d092030c3David Lawrencetypedef struct dns_zone_checkservers {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t magic;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_boolean_t name_known;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_name_t server;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_sockaddr_t address;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_zone_state_t state;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_zone_t *zone;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_resolver_t *res;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_t *mctx;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_fetch_t *fetch;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_LINK(struct dns_zone_checkservers) link;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister} dns_zone_checkservers_t;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerstruct dns_zone {
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson /* Unlocked */
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson unsigned int magic;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mutex_t lock;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_t *mctx;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson /* Locked */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_db_t *top;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_zonemgr_t *zmgr;
b3e77535185043f089b346166440402d092030c3David Lawrence ISC_LINK(dns_zone_t) link; /* Used by zmgr. */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_timer_t *timer;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister unsigned int references;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_name_t origin;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister char *database;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister char *journal;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_int32_t journalsize;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_rdataclass_t rdclass;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_zonetype_t type;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister unsigned int flags;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister unsigned int options;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister unsigned int setoptions;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson char * db_type;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister unsigned int db_argc;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister char ** db_argv;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_stdtime_t expiretime;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_stdtime_t refreshtime;
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson isc_stdtime_t dumptime;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_stdtime_t servertime;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_stdtime_t parenttime;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_stdtime_t childtime;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t serial;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t refresh;
b3e77535185043f089b346166440402d092030c3David Lawrence isc_uint32_t retry;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t expire;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t minimum;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_sockaddr_t * masters;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence unsigned int masterscnt;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews in_port_t masterport;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews unsigned int curmaster;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews isc_sockaddr_t * notify;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews unsigned int notifycnt;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews isc_sockaddr_t notifyfrom;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews isc_task_t * task;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews isc_sockaddr_t xfrsource;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews /* Access Control Lists */
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_c_ipmatchlist_t *update_acl;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_c_ipmatchlist_t *query_acl;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_c_ipmatchlist_t *xfr_acl;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_c_severity_t check_names;
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_c_pubkey_t *pubkey;
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews ISC_LIST(dns_zone_checkservers_t) checkservers;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_fetch_t *fetch;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_resolver_t *res;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_socketmgr_t *socketmgr;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t maxxfrin;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t maxxfrout;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t idlein;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_uint32_t idleout;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_boolean_t diff_on_reload;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence};
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_FLAG(z,f) (((z)->flags & (f)) != 0)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence /* XXX MPA these may need to go back into zone.h */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_F_REFRESH 0x00000001U /* refresh check in progress */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_F_NEEDDUMP 0x00000002U /* zone need consolidation */
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews#define DNS_ZONE_F_SERVERS 0x00000004U /* servers check in progress */
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews#define DNS_ZONE_F_PARENTS 0x00000008U /* parents check in progress */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_F_CHILDREN 0x00000010U /* child check in progress */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#define DNS_ZONE_F_LOADED 0x00000020U /* database has loaded */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_F_EXITING 0x00000040U /* zone is being destroyed */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_F_EXPIRED 0x00000080U /* zone has expired */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#define DNS_ZONE_F_NEEDREFRESH 0x00000100U /* refresh check needed */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_F_UPTODATE 0x00000200U /* zone contents are
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * uptodate */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define DNS_ZONE_OPTION(z,o) ((((z)->setoptions & (o)) != 0) ? \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister (((z)->options & (o)) != 0) : \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister DNS_GLOBAL_OPTION(o))
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerstruct dns_zonemgr {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_t * mctx;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_taskmgr_t * taskmgr;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_timermgr_t * timermgr;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_socketmgr_t * socketmgr;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_taskpool_t * zonetasks;
b3e77535185043f089b346166440402d092030c3David Lawrence struct soaquery {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_task_t * task;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister } soaquery;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_rwlock_t rwlock;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister /* Locked by rwlock. */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_LIST(dns_zone_t) zones;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister};
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerstatic void refresh_callback(isc_task_t *, isc_event_t *);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic void zone_shutdown(isc_task_t *, isc_event_t *);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerstatic void soa_query(dns_zone_t *, isc_taskaction_t);
b3e77535185043f089b346166440402d092030c3David Lawrencestatic dns_result_t zone_settimer(dns_zone_t *, isc_stdtime_t);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void cancel_refresh(dns_zone_t *);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic dns_result_t dns_notify(dns_name_t *, isc_sockaddr_t *, dns_rdatatype_t,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_rdataclass_t, isc_sockaddr_t *, isc_mem_t *);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void checkservers_callback(isc_task_t *task, isc_event_t *event);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void zone_log(dns_zone_t *zone, const char *, int, const char *msg, ...);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic int message_count(dns_message_t *msg, dns_section_t section,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_rdatatype_t type);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews#if 0
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void sockaddr_fromaddr(isc_sockaddr_t *sockaddr, dns_c_addr_t *a,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews in_port_t port);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews#endif
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void add_address_tocheck(dns_message_t *msg,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_zone_checkservers_t *checkservers,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_rdatatype_t type);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsextern void dns_zone_transfer_in(dns_zone_t *zone);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void record_serial(void);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic dns_result_t dns_zone_tostr(dns_zone_t *zone, isc_mem_t *mctx, char **s);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void unload(dns_zone_t *zone);
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsstatic void expire(dns_zone_t *zone);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrewsstatic dns_result_t replacedb(dns_zone_t *zone, dns_db_t *db,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_boolean_t dump);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic dns_result_t default_journal(dns_zone_t *zone);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerstatic void releasezone(dns_zonemgr_t *zmgr, dns_zone_t *zone);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic void xfrin_start_temporary_kludge(dns_zone_t *zone);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafssonstatic void xfrdone(dns_zone_t *zone, dns_result_t result);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define PRINT_ZONE_REF(zone) \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister do { \
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews char *s = NULL; \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_result_t r; \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister r = dns_zone_tostr(zone, zone->mctx, &s); \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (r == DNS_R_SUCCESS) { \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister printf("%p: %s: references = %d\n", zone, s, \
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister zone->references); \
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister isc_mem_free(zone->mctx, s); \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister } \
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister } while (0)
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews#define DNS_ENTER zone_log(zone, me, ISC_LOG_DEBUG(10), "enter")
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews#define DNS_LEAVE zone_log(zone, me, ISC_LOG_DEBUG(10), "leave")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson/***
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *** Public functions.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ***/
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerdns_result_t
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencedns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_result_t iresult;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_zone_t *zone;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_sockaddr_t sockaddr_any;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
b3e77535185043f089b346166440402d092030c3David Lawrence REQUIRE(zonep != NULL && *zonep == NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister REQUIRE(mctx != NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_sockaddr_fromin6(&sockaddr_any, &in6addr_any, 0);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone = isc_mem_get(mctx, sizeof *zone);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (zone == NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (DNS_R_NOMEMORY);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister iresult = isc_mutex_init(&zone->lock);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (iresult != ISC_R_SUCCESS) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_put(mctx, zone, sizeof *zone);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
b3e77535185043f089b346166440402d092030c3David Lawrence "isc_mutex_init() failed: %s",
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_result_totext(iresult));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (DNS_R_UNEXPECTED);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister }
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister /* XXX MPA check that all elements are initialised */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->mctx = mctx;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->top = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->zmgr = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ISC_LINK_INIT(zone, link);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->references = 1; /* Implicit attach. */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_name_init(&zone->origin, NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->database = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->journalsize = -1;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson zone->journal = NULL;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson zone->rdclass = dns_rdataclass_none;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->type = dns_zone_none;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->flags = 0;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson zone->options = 0;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson zone->setoptions = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->db_type = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->db_argc = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->db_argv = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->expiretime = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->refreshtime = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->dumptime = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->servertime = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->parenttime = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->childtime = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->serial = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->refresh = DEFAULT_REFRESH;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->retry = DEFAULT_RETRY;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->expire = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->minimum = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->masters = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->masterscnt = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->masterport = 0;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->curmaster = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->notify = NULL;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 zone->notifycnt = 0;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 zone->task = NULL;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 zone->update_acl = NULL;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 zone->query_acl = NULL;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 zone->xfr_acl = NULL;
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson zone->check_names = dns_c_severity_ignore;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->pubkey = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->fetch = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->res = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->socketmgr = NULL;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->timer = NULL;
b3e77535185043f089b346166440402d092030c3David Lawrence zone->idlein = DNS_DEFAULT_IDLEIN;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->idleout = DNS_DEFAULT_IDLEOUT;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ISC_LIST_INIT(zone->checkservers);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->xfrsource = sockaddr_any;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->maxxfrin = MAX_XFER_TIME;
b3e77535185043f089b346166440402d092030c3David Lawrence zone->maxxfrout = MAX_XFER_TIME;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->diff_on_reload = ISC_FALSE;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->magic = ZONE_MAGIC;
b3e77535185043f089b346166440402d092030c3David Lawrence#if 0
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence PRINT_ZONE_REF(zone);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#endif
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister *zonep = zone;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (DNS_R_SUCCESS);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister}
b3e77535185043f089b346166440402d092030c3David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerstatic void
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerzone_free(dns_zone_t *zone) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
b3e77535185043f089b346166440402d092030c3David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister LOCK(&zone->lock);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister REQUIRE(zone->references == 0);
b3e77535185043f089b346166440402d092030c3David Lawrence zone->flags |= DNS_ZONE_F_EXITING;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister UNLOCK(&zone->lock);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister /* managed objects */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister /* order is important */
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_REFRESH))
b3e77535185043f089b346166440402d092030c3David Lawrence cancel_refresh(zone);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->timer != NULL)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_timer_detach(&zone->timer);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->res != NULL)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_resolver_detach(&zone->res);
b3e77535185043f089b346166440402d092030c3David Lawrence if (zone->fetch != NULL)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_resolver_destroyfetch(zone->res, &zone->fetch);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->task != NULL)
b3e77535185043f089b346166440402d092030c3David Lawrence isc_task_detach(&zone->task);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (zone->socketmgr != NULL)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister isc_socketmgr_destroy(&zone->socketmgr);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->zmgr)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister dns_zonemgr_releasezone(zone->zmgr, zone);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister
b3e77535185043f089b346166440402d092030c3David Lawrence /* unmanaged objects */
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->database != NULL)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister isc_mem_free(zone->mctx, zone->database);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->database = NULL;
b3e77535185043f089b346166440402d092030c3David Lawrence zone->journalsize = -1;
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->journal != NULL)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister isc_mem_free(zone->mctx, zone->journal);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister zone->journal = NULL;
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->db_type != NULL)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister isc_mem_free(zone->mctx, zone->db_type);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister zone->db_type = NULL;
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->top != NULL)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister dns_db_detach(&zone->top);
b3e77535185043f089b346166440402d092030c3David Lawrence dns_zone_cleardbargs(zone);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister dns_zone_clearmasters(zone);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->masterport = 0;
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister dns_zone_clearnotify(zone);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->check_names = dns_c_severity_ignore;
b3e77535185043f089b346166440402d092030c3David Lawrence zone->pubkey = NULL; /* XXX detach */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (zone->update_acl != NULL)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister dns_c_ipmatchlist_detach(&zone->update_acl);
b3e77535185043f089b346166440402d092030c3David Lawrence if (zone->query_acl != NULL)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_c_ipmatchlist_detach(&zone->query_acl);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (zone->xfr_acl != NULL)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister dns_c_ipmatchlist_detach(&zone->xfr_acl);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (dns_name_dynamic(&zone->origin))
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister dns_name_free(&zone->origin, zone->mctx);
b3e77535185043f089b346166440402d092030c3David Lawrence
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister /* last stuff */
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister isc_mutex_destroy(&zone->lock);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone->magic = 0;
b3e77535185043f089b346166440402d092030c3David Lawrence isc_mem_put(zone->mctx, zone, sizeof *zone);
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister}
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister
b3e77535185043f089b346166440402d092030c3David Lawrence/*
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister * Single shot.
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister */
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Bristervoid
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtondns_zone_setclass(dns_zone_t *zone, dns_rdataclass_t rdclass) {
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington REQUIRE(rdclass != dns_rdataclass_none);
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington /* test and set */
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington LOCK(&zone->lock);
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington REQUIRE(zone->rdclass == dns_rdataclass_none ||
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington zone->rdclass == rdclass);
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington zone->rdclass = rdclass;
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington UNLOCK(&zone->lock);
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington}
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtondns_rdataclass_t
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtondns_zone_getclass(dns_zone_t *zone){
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington return (zone->rdclass);
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington}
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington/*
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington * Single shot.
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington */
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtonvoid
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtondns_zone_settype(dns_zone_t *zone, dns_zonetype_t type) {
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews REQUIRE(type != dns_zone_none);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews /* test and set */
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews LOCK(&zone->lock);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews REQUIRE(zone->type == dns_zone_none || zone->type == type);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews zone->type = type;
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews UNLOCK(&zone->lock);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews}
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrewsdns_result_t
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrewsdns_zone_setdbtype(dns_zone_t *zone, char *db_type) {
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews dns_result_t result = DNS_R_SUCCESS;
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews LOCK(&zone->lock);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews if (zone->db_type != NULL)
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews isc_mem_free(zone->mctx, zone->db_type);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews zone->db_type = isc_mem_strdup(zone->mctx, db_type);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews if (zone->db_type == NULL)
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews result = DNS_R_NOMEMORY;
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews UNLOCK(&zone->lock);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews return (result);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews}
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerdns_result_t
3e42bdfdc901b6b921b02028bd51ca2af8e84adcMark Andrewsdns_zone_setorigin(dns_zone_t *zone, char *origin) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_t buffer;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_fixedname_t fixed;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_result_t result;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
b3e77535185043f089b346166440402d092030c3David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence REQUIRE(origin != NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
b3e77535185043f089b346166440402d092030c3David Lawrence dns_fixedname_init(&fixed);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_buffer_init(&buffer, origin, strlen(origin), ISC_BUFFERTYPE_TEXT);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_buffer_add(&buffer, strlen(origin));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister result = dns_name_fromtext(dns_fixedname_name(&fixed),
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister &buffer, dns_rootname, ISC_FALSE, NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (result != DNS_R_SUCCESS)
3e42bdfdc901b6b921b02028bd51ca2af8e84adcMark Andrews return (result);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister LOCK(&zone->lock);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (dns_name_dynamic(&zone->origin)) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_name_free(&zone->origin, zone->mctx);
b3e77535185043f089b346166440402d092030c3David Lawrence dns_name_init(&zone->origin, NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister }
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister result = dns_name_dup(dns_fixedname_name(&fixed), zone->mctx,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister &zone->origin);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister UNLOCK(&zone->lock);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (result);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister}
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerdns_result_t
b3e77535185043f089b346166440402d092030c3David Lawrencedns_zone_setdatabase(dns_zone_t *zone, const char *database) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_result_t result = DNS_R_SUCCESS;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister REQUIRE(DNS_ZONE_VALID(zone));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence REQUIRE(database != NULL);
b3e77535185043f089b346166440402d092030c3David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister LOCK(&zone->lock);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (zone->database != NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_free(zone->mctx, zone->database);
b3e77535185043f089b346166440402d092030c3David Lawrence zone->database = isc_mem_strdup(zone->mctx, database);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (zone->database == NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister result = DNS_R_NOMEMORY;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister else
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister result = default_journal(zone);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister UNLOCK(&zone->lock);
b3e77535185043f089b346166440402d092030c3David Lawrence return (result);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister}
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic dns_result_t
b3e77535185043f089b346166440402d092030c3David Lawrencedefault_journal(dns_zone_t *zone) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister int len;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister REQUIRE(DNS_ZONE_VALID(zone));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister REQUIRE(zone->database != NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (zone->journal != NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_free(zone->mctx, zone->journal);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister len = strlen(zone->database) + sizeof ".jnl"; /* includes '\0' */
b3e77535185043f089b346166440402d092030c3David Lawrence zone->journal = isc_mem_allocate(zone->mctx, len);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (zone->journal == NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (DNS_R_NOMEMORY);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister strcpy(zone->journal, zone->database);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister strcat(zone->journal, ".jnl");
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (DNS_R_SUCCESS);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister}
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerdns_result_t
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerdns_zone_setjournal(dns_zone_t *zone, const char *journal) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_result_t result = DNS_R_SUCCESS;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
b3e77535185043f089b346166440402d092030c3David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister REQUIRE(journal != NULL);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister LOCK(&zone->lock);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (zone->journal != NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_free(zone->mctx, zone->journal);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister zone->journal = isc_mem_strdup(zone->mctx, journal);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if (zone->journal == NULL)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister result = DNS_R_NOMEMORY;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister UNLOCK(&zone->lock);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (result);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerchar *
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellingtondns_zone_getjournal(dns_zone_t *zone) {
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington return (zone->journal);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington}
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellingtonvoid
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellingtondns_zone_validate(dns_zone_t *zone) {
15c5b240a6122795212c1aff03634e7156951e91Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
6f7abb89ec22aef5eda40ed60fcf605a42b78d4dMark Andrews REQUIRE(dns_name_countlabels(&zone->origin) != 0);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington REQUIRE(zone->type != dns_zone_none);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 switch (zone->rdclass) {
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington case dns_zone_master:
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington case dns_zone_slave:
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington case dns_zone_stub:
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington case dns_zone_hint:
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington REQUIRE(zone->database != NULL);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington /*FALLTHROUGH*/
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington case dns_zone_forward:
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington REQUIRE(zone->rdclass != dns_rdataclass_none);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington break;
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington case dns_zone_cache:
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington REQUIRE(zone->rdclass == dns_rdataclass_none);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington REQUIRE(zone->database == NULL);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington break;
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington }
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington REQUIRE(zone->db_type != NULL);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington}
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellingtondns_result_t
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellingtondns_zone_load(dns_zone_t *zone) {
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 const char me[] = "dns_zone_load";
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 int soacount = 0;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews int nscount = 0;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews dns_result_t result;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews dns_dbnode_t *node = NULL;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 dns_dbversion_t *version = NULL;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 dns_rdataset_t rdataset;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 isc_boolean_t cache = ISC_FALSE;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 dns_rdata_soa_t soa;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 dns_rdata_t rdata;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 isc_stdtime_t now;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 dns_db_t *db = NULL;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 REQUIRE(DNS_ZONE_VALID(zone));
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 LOCK(&zone->lock);
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 if (isc_stdtime_get(&now) != ISC_R_SUCCESS) {
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 result = DNS_R_UNEXPECTED;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 goto cleanup;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 }
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 switch (zone->type) {
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 case dns_zone_forward:
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 case dns_zone_none:
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 result = DNS_R_SUCCESS;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 goto cleanup;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 case dns_zone_master:
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 case dns_zone_slave:
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 case dns_zone_stub:
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 case dns_zone_hint:
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 cache = ISC_FALSE;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 break;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 case dns_zone_cache:
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews cache = ISC_TRUE;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews break;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews default:
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews INSIST("bad zone type" == NULL);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews }
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews REQUIRE(zone->database != NULL);
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews result = dns_db_create(zone->mctx, zone->db_type,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews &zone->origin,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews cache, zone->rdclass,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews zone->db_argc, zone->db_argv, &db);
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews if (result != DNS_R_SUCCESS)
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews goto cleanup;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews result = dns_db_load(db, zone->database);
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews /*
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * Initiate zone transfer? We may need a error code that
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * indicates that the "permanent" form does not exist.
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * XXX better error feedback to log.
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews */
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews if (result != DNS_R_SUCCESS) {
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews zone_log(zone, me, ISC_LOG_ERROR,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews "database %s: dns_db_load failed: %s",
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews zone->database, dns_result_totext(result));
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews goto cleanup;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews }
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews /*
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * Apply update log, if any.
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews */
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews if (zone->journal != NULL) {
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews result = dns_journal_rollforward(zone->mctx, db, zone->journal);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews if (result != DNS_R_SUCCESS && result != DNS_R_NOTFOUND &&
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews result != DNS_R_UPTODATE && result != DNS_R_NOJOURNAL)
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews goto cleanup;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews if (result == DNS_R_NOTFOUND) {
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews zone_log(zone, me, ISC_LOG_ERROR,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews "journal out of sync with zone");
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews goto cleanup;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews }
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews zone_log(zone, me, ISC_LOG_DEBUG(1),
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews "dns_journal_rollforward: %s",
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews dns_result_totext(result));
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews if (result == DNS_R_SUCCESS)
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews zone->flags |= DNS_ZONE_F_NEEDDUMP;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews }
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews /*
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * Obtain ns and soa counts for top of zone.
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews */
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews nscount = 0;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews soacount = 0;
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews dns_db_currentversion(db, &version);
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews result = dns_db_findnode(db, &zone->origin, ISC_FALSE, &node);
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews if (result == DNS_R_SUCCESS) {
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews dns_rdataset_init(&rdataset);
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews result = dns_db_findrdataset(db, node, version,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews dns_rdatatype_ns,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews dns_rdatatype_none, 0, &rdataset,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews NULL);
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews if (result == DNS_R_SUCCESS) {
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews result = dns_rdataset_first(&rdataset);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews while (result == DNS_R_SUCCESS) {
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews nscount++;
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews result = dns_rdataset_next(&rdataset);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews }
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews dns_rdataset_disassociate(&rdataset);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews }
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews result = dns_db_findrdataset(db, node, version,
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews dns_rdatatype_soa,
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews dns_rdatatype_none, 0, &rdataset,
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews NULL);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews if (result == DNS_R_SUCCESS) {
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews result = dns_rdataset_first(&rdataset);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews while (result == DNS_R_SUCCESS) {
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews dns_rdataset_current(&rdataset, &rdata);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews if (soacount == 0)
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews dns_rdata_tostruct(&rdata, &soa,
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews zone->mctx);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews soacount++;
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews result = dns_rdataset_next(&rdataset);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews }
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews dns_rdataset_disassociate(&rdataset);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews }
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews dns_rdataset_invalidate(&rdataset);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews }
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews dns_db_detachnode(db, &node);
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews dns_db_closeversion(db, &version, ISC_FALSE);
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews /*
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews * Master / Slave / Stub zones require both NS and SOA records at
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews * the top of the zone.
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews * Hint zones only require NS records.
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews * Cache zones have no reqirements.
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews */
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews switch (zone->type) {
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews case dns_zone_master:
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews case dns_zone_slave:
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews case dns_zone_stub:
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews if (soacount != 1 || nscount == 0) {
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews if (soacount != 1)
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews zone_log(zone, me, ISC_LOG_ERROR,
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews "has %d SOA record%s", soacount,
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews (soacount != 0) ? "s" : "");
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews if (nscount == 0)
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews zone_log(zone, me, ISC_LOG_ERROR,
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews "no NS records");
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews result = DNS_R_BADZONE;
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews goto cleanup;
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews }
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews if (zone->top != NULL) {
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews if (!isc_serial_gt(soa.serial, zone->serial)) {
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews zone_log(zone, me, ISC_LOG_ERROR,
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews "zone serial has gone backwards");
}
}
zone->serial = soa.serial;
zone->refresh = RANGE(soa.refresh, DNS_MIN_REFRESH,
DNS_MAX_REFRESH);
zone->retry = RANGE(soa.retry, DNS_MIN_REFRESH,
DNS_MAX_REFRESH);
zone->expire = RANGE(soa.expire, zone->refresh + zone->retry,
DNS_MAX_EXPIRE);
zone->minimum = soa.minimum;
if (zone->type == dns_zone_slave ||
zone->type == dns_zone_stub) {
/* XXX need database modification time */
zone->expiretime = now /*XXX*/ + zone->expire;
zone->refreshtime = now /*XXX*/;
}
break;
case dns_zone_hint:
if (nscount == 0) {
zone_log(zone, me, ISC_LOG_ERROR, "no NS records");
result = DNS_R_BADZONE;
goto cleanup;
}
break;
case dns_zone_cache:
break;
default:
UNEXPECTED_ERROR(__FILE__, __LINE__,
"unexpected zone type %d", zone->type);
result = DNS_R_UNEXPECTED;
goto cleanup;
}
if (zone->top != NULL) {
result = replacedb(zone, db, ISC_FALSE);
if (result != ISC_R_SUCCESS)
goto cleanup;
} else {
dns_db_attach(db, &zone->top);
zone->flags |= DNS_ZONE_F_LOADED;
}
result = ISC_R_SUCCESS;
cleanup:
UNLOCK(&zone->lock);
if (soacount != 0)
dns_rdata_freestruct(&soa);
if (db != NULL)
dns_db_detach(&db);
return (result);
}
#ifdef notyet
void
dns_zone_checkservers(dns_zone_t *zone) {
dns_name_t *zonename;
unsigned int i;
dns_zone_checkservers_t *checkservers;
dns_rdataset_t rdataset;
dns_rdata_t rdata;
dns_dbnode_t *node = NULL;
dns_dbversion_t *version = NULL;
dns_result_t result;
dns_rdata_ns_t ns;
REQUIRE(DNS_ZONE_VALID(zone));
/* XXX MPA */
/*
* get NS list from database, add in notify also list
*/
zonename = &zone->origin;
dns_db_currentversion(zone->top, &version);
result = dns_db_findnode(zone->top, zonename, ISC_FALSE, &node);
if (result == DNS_R_SUCCESS) {
dns_rdataset_init(&rdataset);
result = dns_db_findrdataset(zone->top, node, version,
dns_rdatatype_ns,
dns_rdatatype_none, 0, &rdataset,
NULL);
if (result == DNS_R_SUCCESS) {
result = dns_rdataset_first(&rdataset);
while (result == DNS_R_SUCCESS) {
dns_rdataset_current(&rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &ns, zone->mctx);
if (result != DNS_R_SUCCESS)
continue;
checkservers = isc_mem_get(zone->mctx,
sizeof *checkservers);
if (checkservers == NULL)
break;
dns_name_init(&checkservers->server, NULL);
dns_name_dup(&ns.name, zone->mctx,
&checkservers->server);
checkservers->name_known = ISC_TRUE;
checkservers->state = get_a; /* XXXMPA */
dns_zone_attach(zone, &checkservers->zone);
checkservers->mctx = zone->mctx;
dns_resolver_attach(zone->res, &checkservers->res);
checkservers->fetch = NULL;
ISC_LINK_INIT(checkservers, link);
checkservers->magic = CHECKSERVERS_MAGIC;
/* XXX lookup A/AAAA/A6 records */
result = dns_rdataset_next(&rdataset);
}
}
dns_rdataset_disassociate(&rdataset);
dns_rdataset_invalidate(&rdataset);
}
dns_db_detachnode(zone->top, &node);
dns_db_closeversion(zone->top, &version, ISC_FALSE);
/*
* Foreach NS in NS list perform a non-recursive query to obtain
* NS list for zone (remove self from list).
*
* callback to check:
* If NXDOMAIN -> log error.
* If NODATA -> log error.
* If referral -> log error.
* If non-auth -> log error.
* Compare NS list returned with server list if not identical
* log error if current list is at least 3 x refresh old.
* Compare glue A/AAAA/A6 records.
*/
/*
* Foreach NS in NS list perform a non-recursive query to obtain
* SOA record for zone (remove self from list).
*
* callback to check:
* If NXDOMAIN -> log error.
* If NODATA -> log error.
* If referral -> log error.
* If no-auth -> log error.
* Compare SOA serial with ixfr list and if older that 3x refresh
* log error.
*/
LOCK(&zone->lock);
for (i = 0 ; i < zone->notifycnt; i++) {
checkservers = isc_mem_get(zone->mctx, sizeof *checkservers);
if (checkservers == NULL)
break;
dns_name_init(&checkservers->server, NULL);
checkservers->name_known = ISC_FALSE;
checkservers->state = get_ns;
checkservers->address = zone->notify[i];
dns_zone_attach(zone, &checkservers->zone);
checkservers->mctx = zone->mctx;
dns_resolver_attach(zone->res, &checkservers->res);
checkservers->fetch = NULL;
ISC_LINK_INIT(checkservers, link);
checkservers->magic = CHECKSERVERS_MAGIC;
ISC_LIST_APPEND(zone->checkservers, checkservers, link);
dns_resolver_createfetch(zone->res, zonename, dns_rdatatype_ns,
NULL, NULL, NULL,
DNS_FETCHOPT_UNSHARED,
zone->task, checkservers_callback,
checkservers, &checkservers->fetch);
}
UNLOCK(&zone->lock);
}
#endif
#ifdef notyet
static void
checkservers_callback(isc_task_t *task, isc_event_t *event) {
const char me[] = "checkservers_callback";
dns_fetchdoneevent_t *devent = (dns_fetchdoneevent_t *)event;
dns_zone_checkservers_t *checkservers = event->arg;
dns_zone_state_t state;
dns_zone_t *zone;
dns_name_t *name;
isc_mem_t *mctx;
isc_sockaddr_t *address;
dns_resolver_t *res;
dns_message_t *msg;
REQUIRE(DNS_CHECKSERVERS_VALID(checkservers));
state = checkservers->state;
zone = checkservers->zone;
name = &checkservers->server;
address = &checkservers->address;
mctx = checkservers->mctx;
res = checkservers->res;
task = task; /* unused */
if (devent->result != DNS_R_SUCCESS) {
/* timeout */
switch (state) {
case get_a6:
case get_aaaa:
case get_a:
zone_log(zone, me, ISC_LOG_INFO,
"unable to obtain address for (%s)");
break;
case get_ns:
case get_soa:
zone_log(zone, me, ISC_LOG_INFO,
"unable to obtain %s RRset from %s"
);
}
goto cleanup;
}
msg = NULL;
dns_resolver_getanswer(event, &msg);
switch (state) {
case get_a6:
add_address_tocheck(msg, checkservers, dns_rdatatype_a6);
dns_resolver_createfetch(res, name, dns_rdatatype_aaaa,
NULL, NULL, NULL, 0, zone->task,
checkservers_callback,
checkservers, &checkservers->fetch);
checkservers->state = get_aaaa;
break;
case get_aaaa:
add_address_tocheck(msg, checkservers, dns_rdatatype_a6);
dns_resolver_createfetch(res, name, dns_rdatatype_a,
NULL, NULL, NULL, 0, zone->task,
checkservers_callback,
checkservers, &checkservers->fetch);
checkservers->state = get_a;
break;
case get_a:
add_address_tocheck(msg, checkservers, dns_rdatatype_a);
/* make NS query to address */
dns_resolver_createfetch(res, name, dns_rdatatype_ns,
NULL, NULL, NULL,
DNS_FETCHOPT_UNSHARED,
zone->task, checkservers_callback,
checkservers, &checkservers->fetch);
checkservers->state = get_ns;
break;
case get_ns:
case get_soa:
if (msg->rcode != dns_rcode_noerror) {
char rcode[128];
isc_buffer_t rb;
isc_buffer_init(&rb, rcode, sizeof rcode,
ISC_BUFFERTYPE_TEXT);
dns_rcode_totext(msg->rcode, &rb);
zone_log(zone, me, ISC_LOG_INFO,
"server %s (%s) unexpected rcode = %.*s",
rb.used, rcode);
break;
}
if (msg->counts[DNS_SECTION_ANSWER] == 0) {
if (referral(msg))
zone_log(zone, me, ISC_LOG_INFO,
"server %s (%s) referral response");
else
zone_log(zone, me, ISC_LOG_INFO,
"server %s (%s) type = %s NODATA response");
}
if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) {
zone_log(zone,�me, ISC_LOG_INFO,
"server %s (%s) not authorative");
}
if (state == get_ns) {
/* compare NS RR sets */
/* make soa query to address */
dns_resolver_createfetch(res, name, dns_rdatatype_soa,
NULL, NULL, NULL,
DNS_FETCHOPT_UNSHARED,
zone->task,
checkservers_callback,
checkservers,
&checkservers->fetch);
checkservers->state = get_soa;
break;
} else {
/* compare SOA RR sets */
goto cleanup;
}
break;
default:
UNEXPECTED_ERROR(__FILE__, __LINE__, "unexpected state");
break;
}
isc_event_free(&event);
return;
cleanup:
isc_event_free(&event);
ISC_LIST_UNLINK(zone->checkservers, checkservers, link);
checkservers->magic = 0;
dns_zone_detach(&checkservers->zone);
isc_mem_put(mctx, checkservers, sizeof *checkservers);
}
#endif
#if 0
static void
cmp_soa(dns_message_t *msg, dns_zone_t *zone, char *server) {
dns_rdata_soa_t msgsoa, zonesoa;
dns_result_t result;
dns_rdataset_t *rdataset = NULL;
dns_rdataset_t zonerdataset;
dns_rdata_t rdata;
dns_rdata_init(&rdata);
/*
* extract SOA from message
*/
result = dns_message_findname(msg, DNS_SECTION_ANSWER,
&zone->origin,
dns_rdatatype_soa,
dns_rdatatype_none, NULL, &rdataset);
if (result != DNS_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO,
"Unable to extract SOA from answer: %s", server);
return;
}
result = dns_rdataset_first(rdataset);
if (DNS_R_SUCCESS != result)
return;
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &msgsoa, zone->mctx);
if (DNS_R_SUCCESS != result)
return;
result = dns_rdataset_next(rdataset);
if (DNS_R_NOMORE != result) {
zone_log(zone, me, ISC_LOG_INFO,
"More that one SOA record returned: %s", server);
goto cleanup_msgsoa;
}
/*
* Get SOA record for zone.
*/
dns_rdataset_init(&zonerdataset);
LOCK(&zone->lock);
result = dns_db_find(zone->top, &zone->origin,
NULL, dns_rdatatype_soa, dns_rdatatype_none,
0, 0, NULL, NULL, &zonerdataset);
UNLOCK(&zone->lock);
if (result != DNS_R_SUCCESS) {
/* XXXMPA */
goto cleanup_msgsoa;
}
result = dns_rdataset_first(&zonerdataset);
if (DNS_R_SUCCESS != result)
return;
dns_rdataset_current(&zonerdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &msgsoa, zone->mctx);
if (DNS_R_SUCCESS != result)
return;
result = dns_rdataset_next(&zonerdataset);
if (DNS_R_NOMORE != result) {
zone_log(zone, me, ISC_LOG_INFO, "More that one SOA in zone");
goto cleanup_msgsoa;
}
dns_rdataset_disassociate(&zonerdataset);
/*
* Check SOA contents. If serials do not match check to see
* if the slave is ahead of us (i.e. we have reset the serial
* number).
*
* If the serials do match then check the other values for
* consistancy.
*/
if (msgsoa.serial != zonesoa.serial) {
if (!isc_serial_lt(msgsoa.serial, zonesoa.serial)) {
zone_log(zone, me, ISC_LOG_INFO,
"slave serial not less than or equal to zone serial: %s",
server);
goto cleanup_zonesoa;
}
record_serial();
goto cleanup_zonesoa;
}
if (msgsoa.refresh != zonesoa.refresh ||
msgsoa.retry != zonesoa.retry ||
msgsoa.expire != zonesoa.expire ||
msgsoa.minimum != zonesoa.minimum ||
dns_name_compare(&msgsoa.origin, &zonesoa.origin) != 0 ||
dns_name_compare(&msgsoa.mname, &zonesoa.mname) != 0) {
zone_log(zone, me, ISC_LOG_INFO, "SOA contents differ: %s",
server);
}
cleanup_zonesoa:
dns_rdata_freestruct(&zonesoa);
cleanup_msgsoa:
dns_rdata_freestruct(&msgsoa);
}
#endif
static void
add_address_tocheck(dns_message_t *msg, dns_zone_checkservers_t *checkservers,
dns_rdatatype_t type)
{
dns_rdataset_t *rdataset = NULL;
dns_result_t result;
isc_sockaddr_t sockaddr;
dns_rdata_in_a_t a;
dns_rdata_in_a6_t a6;
dns_rdata_t rdata;
if (msg->rcode != dns_rcode_noerror)
return;
if (msg->counts[DNS_SECTION_QUESTION] != 0 ||
dns_message_findname(msg, DNS_SECTION_QUESTION,
&checkservers->server,
type, dns_rdatatype_none,
NULL, &rdataset) != DNS_R_SUCCESS)
return;
result = dns_rdataset_first(rdataset);
while (DNS_R_SUCCESS == result) {
dns_rdataset_current(rdataset, &rdata);
switch (type) {
case dns_rdatatype_a:
result = dns_rdata_tostruct(&rdata, &a,
checkservers->mctx);
isc_sockaddr_fromin(&sockaddr, &a.in_addr, 0);
dns_rdata_freestruct(&a);
break;
case dns_rdatatype_a6:
result = dns_rdata_tostruct(&rdata, &a6,
checkservers->mctx);
isc_sockaddr_fromin6(&sockaddr, &a6.in6_addr, 0);
dns_rdata_freestruct(&a6);
break;
default:
INSIST(0);
}
result = dns_rdataset_next(rdataset);
}
}
void
dns_zone_checkparents(dns_zone_t *zone) {
/* XXX MPA */
REQUIRE(DNS_ZONE_VALID(zone));
/*
* Obtain a parent NS list.
* Remove LSL from zone name. Check to see if we are serving
* zone otherwise make non-recursive query for NS set of
* of given name. Follow referral until NXDOMAIN, NODATA or
* answer is found. If NXDOMAIN or NODATA remove next LSL
* and repeat.
*/
/*
* If self in NS list check masked NS list in parent against zone
* ns list.
*
* Foreach NS on parent NS list make non recursive query for NS set
* of current zone (removed self from list if required).
*
* Check NS list return for agreement with zone's NS list.
*/
}
void
dns_zone_checkchildren(dns_zone_t *zone) {
/* XXX MPA */
REQUIRE(DNS_ZONE_VALID(zone));
/*
* For each child zone obtain NS list from parent zone.
* For each NS in list send non-recursive query for child zone's
* NS list for zone.
*
* If NXDOMAIN is returned log error.
* If NODATA is return log error.
* If referral is return log error.
* If non-auth is return log error.
* If NS list disagree's with parents NS list log error.
*/
}
void
dns_zone_checkglue(dns_zone_t *zone) {
/* XXX MPA */
REQUIRE(DNS_ZONE_VALID(zone));
/*
* For each glue record in this zone, check with an authorative
* server for the zone to ensure that there have not been any
* changes.
*/
}
void
dns_zone_attach(dns_zone_t *source, dns_zone_t **target) {
REQUIRE(DNS_ZONE_VALID(source));
REQUIRE(target != NULL && *target == NULL);
LOCK(&source->lock);
REQUIRE(source->references > 0);
source->references++;
#if 0
PRINT_ZONE_REF(source);
#endif
INSIST(source->references != 0xffffffffU);
UNLOCK(&source->lock);
*target = source;
}
void
dns_zone_print(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
PRINT_ZONE_REF(zone);
}
static dns_result_t
dns_zone_tostr(dns_zone_t *zone, isc_mem_t *mctx, char **s) {
isc_buffer_t tbuf;
char outbuf[1024];
dns_result_t result;
REQUIRE(s != NULL && *s == NULL);
REQUIRE(DNS_ZONE_VALID(zone));
isc_buffer_init(&tbuf, outbuf, sizeof(outbuf) - 1,
ISC_BUFFERTYPE_TEXT);
if (dns_name_countlabels(&zone->origin) > 0) {
result = dns_name_totext(&zone->origin, ISC_FALSE, &tbuf);
if (result == DNS_R_SUCCESS)
outbuf[tbuf.used] = '\0';
else {
strncpy(outbuf, "<name conversion failed>",
sizeof outbuf - 1);
outbuf[sizeof outbuf - 1] = '\0';
}
} else {
strncpy(outbuf, "<unnamed zone>", sizeof outbuf - 1);
outbuf[sizeof outbuf - 1] = '\0';
}
*s = isc_mem_strdup(mctx, outbuf);
return ((*s == NULL) ? DNS_R_NOMEMORY : DNS_R_SUCCESS);
}
void
dns_zone_detach(dns_zone_t **zonep) {
dns_zone_t *zone;
REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
zone = *zonep;
LOCK(&zone->lock);
REQUIRE(zone->references > 0);
zone->references--;
#if 0
PRINT_ZONE_REF(zone);
#endif
UNLOCK(&zone->lock);
if (zone->references == 0)
zone_free(zone);
*zonep = NULL;
}
void
dns_zone_setflag(dns_zone_t *zone, unsigned int flags, isc_boolean_t value) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (value)
zone->flags |= flags;
else
zone->flags &= ~flags;
UNLOCK(&zone->lock);
}
void
dns_zone_setoption(dns_zone_t *zone, unsigned int option, isc_boolean_t value)
{
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (value)
zone->options |= option;
else
zone->options &= ~option;
zone->setoptions |= option;
UNLOCK(&zone->lock);
}
void
dns_zone_clearoption(dns_zone_t *zone, unsigned int option) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
zone->setoptions &= ~option;
UNLOCK(&zone->lock);
}
void
dns_zone_getoptions(dns_zone_t *zone, unsigned int *options,
unsigned int *optionsmask)
{
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(options != NULL);
REQUIRE(optionsmask != NULL);
LOCK(&zone->lock);
*options = zone->options;
*optionsmask = zone->setoptions;
UNLOCK(&zone->lock);
}
dns_result_t
dns_zone_adddbarg(dns_zone_t *zone, char *arg) {
char **new = NULL;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(arg != NULL);
/*
* Allocate new 'db_argv' and set last to be copy of 'arg'.
*/
LOCK(&zone->lock);
new = isc_mem_get(zone->mctx, (zone->db_argc + 1) * sizeof *new);
if (new == NULL)
goto cleanup;
new[zone->db_argc] = isc_mem_strdup(zone->mctx, arg);
if (new[zone->db_argc] == NULL)
goto cleanup;
/*
* Copy old 'db_argv' if required the free it.
*/
if (zone->db_argc != 0) {
memcpy(new, zone->db_argv, zone->db_argc * sizeof *new);
isc_mem_put(zone->mctx, zone->db_argv,
zone->db_argc * sizeof *new);
}
zone->db_argv = new;
zone->db_argc++;
UNLOCK(&zone->lock);
return (DNS_R_SUCCESS);
cleanup:
if (new != NULL)
isc_mem_put(zone->mctx, new,
(zone->db_argc + 1) * sizeof *new);
UNLOCK(&zone->lock);
return (DNS_R_NOMEMORY);
}
void
dns_zone_cleardbargs(dns_zone_t *zone) {
unsigned int i;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->db_argc) {
for (i = 0 ; i < zone->db_argc; i++)
isc_mem_free(zone->mctx, zone->db_argv[i]);
isc_mem_put(zone->mctx, zone->db_argv,
zone->db_argc * sizeof *zone->db_argv);
zone->db_argc = 0;
zone->db_argv = NULL;
}
UNLOCK(&zone->lock);
}
dns_result_t
dns_zone_setxfrsource(dns_zone_t *zone, isc_sockaddr_t *xfrsource) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
zone->xfrsource = *xfrsource;
UNLOCK(&zone->lock);
return (DNS_R_SUCCESS);
}
isc_sockaddr_t *
dns_zone_getxfrsource(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (&zone->xfrsource);
}
dns_result_t
dns_zone_addnotify(dns_zone_t *zone, isc_sockaddr_t *notify) {
isc_sockaddr_t *new;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
new = isc_mem_get(zone->mctx, (zone->notifycnt + 1) * sizeof *new);
if (new == NULL)
goto cleanup;
new[zone->notifycnt] = *notify;
if (zone->notifycnt > 0) {
memcpy(new, zone->notify, zone->notifycnt * sizeof *new);
isc_mem_put(zone->mctx, zone->notify,
zone->notifycnt * sizeof *new);
}
zone->notify = new;
zone->notifycnt++;
UNLOCK(&zone->lock);
return (DNS_R_SUCCESS);
cleanup:
UNLOCK(&zone->lock);
return (DNS_R_NOMEMORY);
}
void
dns_zone_clearnotify(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->notify != NULL) {
isc_mem_put(zone->mctx, zone->notify,
zone->notifycnt * sizeof *zone->notify);
zone->notify = NULL;
zone->notifycnt = 0;
}
UNLOCK(&zone->lock);
}
dns_result_t
dns_zone_addmaster(dns_zone_t *zone, isc_sockaddr_t *master) {
isc_sockaddr_t *new;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
new = isc_mem_get(zone->mctx, (zone->masterscnt + 1) * sizeof *new);
if (new == NULL) {
UNLOCK(&zone->lock);
return (DNS_R_NOMEMORY);
}
new[zone->masterscnt] = *master;
if (zone->masterscnt > 0) {
memcpy(new, zone->masters, zone->masterscnt * sizeof *new);
isc_mem_put(zone->mctx, zone->masters,
zone->masterscnt * sizeof *new);
}
zone->masters = new;
zone->masterscnt++;
UNLOCK(&zone->lock);
return (DNS_R_SUCCESS);
}
void
dns_zone_clearmasters(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
while (DNS_ZONE_FLAG(zone, DNS_ZONE_F_REFRESH)) {
cancel_refresh(zone);
}
if (zone->masters != NULL) {
isc_mem_put(zone->mctx, zone->masters,
zone->masterscnt * sizeof *zone->masters);
zone->masters = NULL;
zone->masterscnt = 0;
zone->curmaster = 0;
}
UNLOCK(&zone->lock);
}
dns_result_t
dns_zone_getdb(dns_zone_t *zone, dns_db_t **dpb) {
dns_result_t result = DNS_R_SUCCESS;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->top == NULL)
result = DNS_R_NOTLOADED;
else
dns_db_attach(zone->top, dpb);
UNLOCK(&zone->lock);
return (result);
}
/*
* Co-ordinates the starting of routine jobs.
*/
void
dns_zone_maintenance(dns_zone_t *zone) {
const char me[] = "dns_zone_maintenance";
isc_stdtime_t now;
REQUIRE(DNS_ZONE_VALID(zone));
DNS_ENTER;
if (isc_stdtime_get(&now) != ISC_R_SUCCESS)
return;
/*
* Expire check.
*/
switch (zone->type) {
case dns_zone_slave:
case dns_zone_stub:
LOCK(&zone->lock);
if (now >= zone->expiretime &&
DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED)) {
expire(zone);
zone->refreshtime = now;
}
UNLOCK(&zone->lock);
break;
default:
break;
}
/*
* Up to date check.
*/
switch (zone->type) {
case dns_zone_slave:
case dns_zone_stub:
if (now >= zone->refreshtime)
dns_zone_refresh(zone);
break;
default:
break;
}
/*
* Do we need to consolidate the backing store?
*/
switch (zone->type) {
case dns_zone_master:
LOCK(&zone->lock);
if (now >= zone->dumptime &&
DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED) &&
DNS_ZONE_FLAG(zone, DNS_ZONE_F_NEEDDUMP)) {
dns_zone_dump(zone, stdout);
}
UNLOCK(&zone->lock);
break;
default:
break;
}
/*
* Check servers for zone.
*/
switch (zone->type) {
case dns_zone_master:
case dns_zone_slave:
case dns_zone_stub:
#ifdef notyet
if (now >= zone->servertime &&
DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED) &&
DNS_ZONE_OPTION(zone, DNS_ZONE_O_SERVERS) &&
!DNS_ZONE_FLAG(zone, DNS_ZONE_F_SERVERS))
dns_zone_checkservers(zone);
#endif
break;
default:
break;
}
/*
* Check parent servers for zone.
*/
switch (zone->type) {
case dns_zone_master:
case dns_zone_slave:
case dns_zone_stub:
if (now >= zone->parenttime &&
DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED) &&
DNS_ZONE_OPTION(zone, DNS_ZONE_O_PARENTS) &&
!DNS_ZONE_FLAG(zone, DNS_ZONE_F_PARENTS))
dns_zone_checkparents(zone);
break;
default:
break;
}
/*
* Check child servers for zone.
*/
switch (zone->type) {
case dns_zone_master:
case dns_zone_slave:
case dns_zone_stub:
if (now >= zone->childtime &&
DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED) &&
DNS_ZONE_OPTION(zone, DNS_ZONE_O_CHILDREN) &&
!DNS_ZONE_FLAG(zone, DNS_ZONE_F_CHILDREN))
dns_zone_checkchildren(zone);
break;
default:
break;
}
(void) zone_settimer(zone, now); /*XXX*/
}
void
dns_zone_expire(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
expire(zone);
UNLOCK(&zone->lock);
}
static void
expire(dns_zone_t *zone) {
if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_NEEDDUMP))
dns_zone_dump(zone, stdout); /* XXX */
zone->flags |= DNS_ZONE_F_EXPIRED;
dns_zone_setrefresh(zone, DEFAULT_REFRESH, DEFAULT_RETRY);
unload(zone);
}
void
dns_zone_refresh(dns_zone_t *zone) {
isc_stdtime_t now;
isc_uint32_t oldflags;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(zone->masterscnt > 0);
if (isc_stdtime_get(&now) != ISC_R_SUCCESS)
return;
/*
* Set DNS_ZONE_F_REFRESH so that there is only one refresh operation
* in progress at the one time.
*/
LOCK(&zone->lock);
oldflags = zone->flags;
zone->flags |= DNS_ZONE_F_REFRESH;
UNLOCK(&zone->lock);
if ((oldflags & DNS_ZONE_F_REFRESH) != 0)
return;
/*
* Set the next refresh time as if refresh check has failed.
* If we are successful it will be reset using zone->refresh.
*/
zone->refreshtime = now + zone->retry;
zone->curmaster = 0;
#ifdef notyet
/* initiate soa query */
soa_query(zone, refresh_callback);
#else
/* initiate zone transfer */
xfrin_start_temporary_kludge(zone);
#endif
}
dns_result_t
dns_zone_dump(dns_zone_t *zone, FILE *fd) {
dns_dbiterator_t *dbiterator = NULL;
dns_dbversion_t *version = NULL;
dns_result_t result;
dns_fixedname_t fname;
dns_name_t *name;
dns_rdatasetiter_t *rdsiter = NULL;
dns_dbnode_t *node = NULL;
isc_buffer_t text;
isc_region_t region;
char *buf = NULL;
unsigned int buflen = 1024;
dns_rdataset_t rdataset;
dns_db_t *top = NULL;
REQUIRE(DNS_ZONE_VALID(zone));
dns_fixedname_init(&fname);
name = dns_fixedname_name(&fname);
dns_db_attach(zone->top, &top);
dns_db_currentversion(top, &version);
result = dns_db_createiterator(top, ISC_FALSE, &dbiterator);
if (result == DNS_R_SUCCESS)
result = dns_dbiterator_first(dbiterator);
while (result == DNS_R_SUCCESS) {
result = dns_dbiterator_current(dbiterator, &node, name);
if (result != DNS_R_SUCCESS && result != DNS_R_NEWORIGIN)
break;
result = dns_db_allrdatasets(zone->top, node, version, 0,
&rdsiter);
if (result != DNS_R_SUCCESS) {
dns_db_detachnode(top, &node);
break;
}
dns_rdataset_init(&rdataset);
result = dns_rdatasetiter_first(rdsiter);
while (result == DNS_R_SUCCESS) {
dns_rdatasetiter_current(rdsiter, &rdataset);
retry:
if (buf == NULL)
buf = isc_mem_get(zone->mctx, buflen);
if (buf == NULL)
result = DNS_R_NOMEMORY;
isc_buffer_init(&text, buf, buflen,
ISC_BUFFERTYPE_TEXT);
if (result == DNS_R_SUCCESS)
result = dns_rdataset_totext(&rdataset, name,
ISC_FALSE,
ISC_FALSE, &text);
if (result == DNS_R_NOSPACE) {
isc_mem_put(zone->mctx, buf, buflen);
buf = NULL;
buflen += 1024;
goto retry;
}
isc_buffer_used(&text, &region);
if (result == DNS_R_SUCCESS)
fprintf(fd, "%.*s", (int)region.length,
(char *)region.base);
dns_rdataset_disassociate(&rdataset);
if (result == DNS_R_SUCCESS)
result = dns_rdatasetiter_next(rdsiter);
}
dns_rdatasetiter_destroy(&rdsiter);
dns_db_detachnode(top, &node);
if (result == DNS_R_NOMORE)
result = dns_dbiterator_next(dbiterator);
}
if (buf != NULL)
isc_mem_put(zone->mctx, buf, buflen);
dns_dbiterator_destroy(&dbiterator);
dns_db_closeversion(top, &version, ISC_FALSE);
dns_db_detach(&top);
return (result);
}
void
dns_zone_unload(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
unload(zone);
UNLOCK(&zone->lock);
}
static void
unload(dns_zone_t *zone) {
/* caller to lock */
dns_db_detach(&zone->top);
zone->flags &= ~DNS_ZONE_F_LOADED;
}
void
dns_zone_unmount(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
/*XXX MPA*/
}
dns_result_t
dns_zone_manage(dns_zone_t *zone, isc_taskmgr_t *tmgr) {
#if 1
REQUIRE(DNS_ZONE_VALID(zone));
(void)tmgr;
dns_zone_maintenance(zone);
return (DNS_R_SUCCESS);
#else
isc_result_t iresult;
dns_result_t result;
/*
* XXXRTH Zones do not have resolvers!!!!
*/
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(zone->task == NULL);
iresult = isc_task_create(tmgr, zone->mctx, 0, &zone->task);
if (iresult != ISC_R_SUCCESS) {
/* XXX */
return (DNS_R_UNEXPECTED);
}
iresult = isc_task_onshutdown(zone->task, zone_shutdown, zone);
if (iresult != ISC_R_SUCCESS) {
/* XXX */
return (DNS_R_UNEXPECTED);
}
if (zone->res == NULL) {
isc_socket_t *s;
dns_dispatch_t *dispatch;
RUNTIME_CHECK(isc_socketmgr_create(zone->mctx, &zone->socketmgr)
== ISC_R_SUCCESS);
s = NULL;
RUNTIME_CHECK(isc_socket_create(zone->socketmgr, PF_INET,
isc_sockettype_udp, &s) == ISC_R_SUCCESS);
dispatch = NULL;
RUNTIME_CHECK(dns_dispatch_create(zone->mctx, s, zone->task,
4096, 1000, 1000, 4, &dispatch) == DNS_R_SUCCESS);
result = dns_resolver_create(zone->mctx, tmgr, 10, zone->timgr,
zone->rdclass, dispatch,
&zone->res);
if (result != DNS_R_SUCCESS)
return (result);
dns_dispatch_detach(&dispatch);
isc_socket_detach(&s);
}
dns_zone_maintenance(zone);
return (DNS_R_SUCCESS);
#endif
}
void
dns_zone_setrefresh(dns_zone_t *zone, isc_uint32_t refresh,
isc_uint32_t retry)
{
REQUIRE(DNS_ZONE_VALID(zone));
zone->refresh = refresh;
zone->retry = retry;
}
void
dns_zone_notify(dns_zone_t *zone) {
unsigned int i;
dns_name_t *origin = NULL;
isc_sockaddr_t addr;
dns_rdataset_t nsrdset;
dns_rdataset_t ardset;
dns_dbversion_t *version = NULL;
dns_result_t result;
dns_dbnode_t *node = NULL;
dns_rdata_ns_t ns;
dns_rdata_in_a_t a;
dns_rdata_t rdata;
REQUIRE(DNS_ZONE_VALID(zone));
if (!DNS_ZONE_OPTION(zone, DNS_ZONE_O_NOTIFY))
return;
origin = &zone->origin;
/*
* Enqueue notify request.
*/
for (i = 0; i < zone->notifycnt; i++) {
(void)dns_notify(origin, &zone->notify[i], dns_rdatatype_soa,
zone->rdclass, &zone->xfrsource, zone->mctx);
}
dns_db_currentversion(zone->top, &version);
result = dns_db_findnode(zone->top, origin, ISC_FALSE, &node);
if (result != DNS_R_SUCCESS)
goto cleanup1;
dns_rdataset_init(&nsrdset);
result = dns_db_findrdataset(zone->top, node, version,
dns_rdatatype_ns,
dns_rdatatype_none, 0, &nsrdset, NULL);
if (result != DNS_R_SUCCESS)
goto cleanup2;
result = dns_rdataset_first(&nsrdset);
while (result == DNS_R_SUCCESS) {
dns_rdataset_current(&nsrdset, &rdata);
result = dns_rdata_tostruct(&rdata, &ns, zone->mctx);
if (result != DNS_R_SUCCESS)
continue;
/*
* Look up address records.
*/
/* XXX MPA */
if (result == DNS_R_NOTFOUND) {
/*
* Query for address.
* Arrange for notify to be sent when
* we have it.
*/
/* XXX MPA*/
result = dns_rdataset_next(&nsrdset);
continue;
} else if (result != DNS_R_SUCCESS) {
result = dns_rdataset_next(&nsrdset);
continue;
}
result = dns_rdataset_first(&ardset);
while (result == DNS_R_SUCCESS) {
dns_rdataset_current(&ardset, &rdata);
result = dns_rdata_tostruct(&rdata, &a, zone->mctx);
if (result != DNS_R_SUCCESS)
continue;
/*
* Remove duplicates w/ notify list.
*/
isc_sockaddr_fromin(&addr, &a.in_addr, 0);
for (i = 0; i < zone->notifycnt; i++) {
if (isc_sockaddr_equal(&zone->notify[i], &addr))
break;
}
if (i == zone->notifycnt) {
(void)dns_notify(origin, &addr,
dns_rdatatype_soa,
zone->rdclass,
&zone->xfrsource, zone->mctx);
}
result = dns_rdataset_next(&ardset);
}
result = dns_rdataset_next(&nsrdset);
}
dns_rdataset_disassociate(&nsrdset);
cleanup2:
dns_db_detachnode(zone->top, &node);
cleanup1:
dns_db_closeversion(zone->top, &version, ISC_FALSE);
}
/***
*** Private
***/
static void
refresh_callback(isc_task_t *task, isc_event_t *event) {
#if 1
(void)task;
(void)event;
#else
dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
dns_zone_t *zone;
dns_message_t *msg = NULL;
isc_uint32_t soacnt, cnamecnt, soacount, nscount;
isc_stdtime_t now;
char *master;
isc_buffer_t masterbuf;
char mastermem[256];
dns_rdataset_t *rdataset;
dns_rdata_t rdata;
dns_rdata_soa_t soa;
dns_result_t result;
isc_uint32_t serial;
zone = devent->arg;
INSIST(DNS_ZONE_VALID(zone));
/*
* if timeout log and next master;
*/
isc_buffer_init(&masterbuf, mastermem, sizeof(mastermem),
ISC_BUFFERTYPE_TEXT);
result = isc_sockaddr_totext(&zone->masters[zone->curmaster],
&masterbuf);
if (result == ISC_R_SUCCESS)
master = (char *) masterbuf.base;
else
master = "<UNKNOWN>";
if (devent->result != DNS_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO, "failure for %s: %s",
master, dns_result_totext(devent->result));
goto next_master;
}
dns_resolver_getanswer(event, &msg);
/*
* Unexpected rcode.
*/
if (msg->rcode != dns_rcode_noerror) {
char rcode[128];
isc_buffer_t rb;
isc_buffer_init(&rb, rcode, sizeof rcode, ISC_BUFFERTYPE_TEXT);
dns_rcode_totext(msg->rcode, &rb);
zone_log(zone, me, ISC_LOG_INFO,
"unexpected rcode (%.*s) from %s\n",
rb.used, rcode, master);
goto next_master;
}
/*
* if non-auth log and next master;
*/
if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) {
zone_log(zone, me, ISC_LOG_INFO,
"non-authorative answer from %s", master);
goto next_master;
}
/*
* There should not be a CNAME record at top of zone.
*/
cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname);
soacnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_soa);
if (cnamecnt != 0) {
zone_log(zone, me, ISC_LOG_INFO,
"CNAME discovered: master %s", master);
goto next_master;
}
if (soacnt != 1) {
zone_log(zone, me, ISC_LOG_INFO,
"SOA count (%d) != 1: master %s", soacnt, master);
goto next_master;
}
nscount = message_count(msg, DNS_SECTION_AUTHORITY, dns_rdatatype_ns);
soacount = message_count(msg, DNS_SECTION_AUTHORITY, dns_rdatatype_soa);
/*
* if referral log and next master;
*/
if (soacnt == 0 && soacount == 0 && nscount != 0) {
zone_log(zone, me, ISC_LOG_INFO,
"referral from: master %s", master);
goto next_master;
}
/*
* if nodata log and next master;
*/
if (soacnt == 0 && nscount == 0) {
zone_log(zone, me, ISC_LOG_INFO,
"NODATA from master %s", master);
goto next_master;
}
/*
* Extract serial
*/
rdataset = NULL;
result = dns_message_findname(msg, DNS_SECTION_ANSWER, &zone->origin,
dns_rdatatype_soa, dns_rdatatype_none,
NULL, &rdataset);
if (result != DNS_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO,
"unable to get soa record from %s", master);
goto next_master;
}
result = dns_rdataset_first(rdataset);
if (result != DNS_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO, "dns_rdataset_first failed");
goto next_master;
}
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &soa, zone->mctx);
if (result != DNS_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO, "dns_rdata_tostruct failed");
goto next_master;
}
serial = soa.serial;
dns_rdata_freestruct(&soa);
if (!DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED) ||
isc_serial_gt(serial, zone->serial)) {
dns_zone_transfer_in(zone);
isc_event_free(&event);
dns_resolver_destroyfetch(zone->res, &zone->fetch);
} else if (isc_serial_eq(soa.serial, zone->serial)) {
dns_zone_uptodate(zone);
goto next_master;
} else {
goto next_master;
}
return;
next_master:
LOCK(&zone->lock);
isc_event_free(&event);
dns_resolver_destroyfetch(zone->res, &zone->fetch);
zone->curmaster++;
if (zone->curmaster >= zone->masterscnt) {
zone->flags &= ~DNS_ZONE_F_REFRESH;
if (isc_stdtime_get(&now) != ISC_R_SUCCESS)
return;
zone_settimer(zone, now);
UNLOCK(&zone->lock);
return;
}
UNLOCK(&zone->lock);
soa_query(zone, refresh_callback);
return;
#endif
}
#ifdef notyet
static void
soa_query(dns_zone_t *zone, isc_taskaction_t callback) {
dns_name_t *zonename;
dns_result_t result;
zonename = &zone->origin;
LOCK(&zone->lock);
result = dns_resolver_createfetch(zone->res, zonename,
dns_rdatatype_soa,
NULL, NULL, NULL,
DNS_FETCHOPT_UNSHARED,
zone->task, callback, zone,
&zone->fetch);
UNLOCK(&zone->lock);
if (result != DNS_R_SUCCESS)
cancel_refresh(zone);
}
#endif
static void
zone_shutdown(isc_task_t *task, isc_event_t *event) {
dns_zone_t *zone = (dns_zone_t *)event->arg;
isc_event_free(&event);
task = task; /* XXX */
zone = zone; /* XXX */
}
static void
zone_timer(isc_task_t *task, isc_event_t *event) {
const char me[] = "zone_timer";
dns_zone_t *zone = (dns_zone_t *)event->arg;
DNS_ENTER;
dns_zone_maintenance(zone);
isc_event_free(&event);
task = task; /* XXX */
}
static dns_result_t
zone_settimer(dns_zone_t *zone, isc_stdtime_t now) {
const char me[] = "zone_settimer";
isc_stdtime_t next = 0;
isc_time_t expires;
isc_interval_t interval;
isc_result_t iresult;
REQUIRE(DNS_ZONE_VALID(zone));
switch (zone->type) {
case dns_zone_master:
if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_NEEDDUMP))
next = zone->dumptime;
if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED)) {
if (DNS_ZONE_OPTION(zone, DNS_ZONE_O_SERVERS) &&
(zone->servertime < next || next == 0))
next = zone->servertime;
if (DNS_ZONE_OPTION(zone, DNS_ZONE_O_PARENTS) &&
(zone->parenttime < next || next == 0))
next = zone->parenttime;
if (DNS_ZONE_OPTION(zone, DNS_ZONE_O_CHILDREN) &&
(zone->childtime < next || next == 0))
next = zone->childtime;
}
break;
case dns_zone_slave:
case dns_zone_stub:
if (!DNS_ZONE_FLAG(zone, DNS_ZONE_F_REFRESH))
next = zone->refreshtime;
if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED)) {
if (zone->expiretime < next || next == 0)
next = zone->expiretime;
if (DNS_ZONE_OPTION(zone, DNS_ZONE_O_SERVERS) &&
(zone->servertime < next || next == 0))
next = zone->servertime;
if (DNS_ZONE_OPTION(zone, DNS_ZONE_O_PARENTS) &&
(zone->parenttime < next || next == 0))
next = zone->parenttime;
if (DNS_ZONE_OPTION(zone, DNS_ZONE_O_CHILDREN) &&
(zone->childtime < next || next == 0))
next = zone->childtime;
}
break;
default:
break;
}
zone_log(zone, me, ISC_LOG_INFO, "settimer %d %d = %d seconds\n",
next, now, next - now);
if (next == 0) {
iresult = isc_timer_reset(zone->timer, isc_timertype_inactive,
NULL, NULL, ISC_TRUE);
} else {
isc_time_settoepoch(&expires);
if (next <= now)
isc_interval_set(&interval, 0, 1);
else
isc_interval_set(&interval, next - now, 0);
iresult = isc_timer_reset(zone->timer, isc_timertype_once,
&expires, &interval, ISC_TRUE);
}
if (iresult != ISC_R_SUCCESS) {
/* XXX */
return (DNS_R_UNEXPECTED);
}
return (DNS_R_SUCCESS);
}
static void
cancel_refresh(dns_zone_t *zone) {
isc_stdtime_t now;
/*
* caller to lock.
*/
REQUIRE(DNS_ZONE_VALID(zone));
zone->flags &= ~DNS_ZONE_F_REFRESH;
if (isc_stdtime_get(&now) != ISC_R_SUCCESS)
return;
if (!DNS_ZONE_FLAG(zone, DNS_ZONE_F_EXITING))
zone_settimer(zone, now);
}
static dns_result_t
dns_notify(dns_name_t *name, isc_sockaddr_t *addr, dns_rdatatype_t type,
dns_rdataclass_t rdclass, isc_sockaddr_t *source, isc_mem_t *mctx)
{
dns_message_t *msg = NULL;
dns_result_t result;
isc_buffer_t target;
/* dns_rdatalist_t *rdatalist = NULL; */
dns_rdatalist_t rdatalist;
dns_rdataset_t *rdataset = NULL;
char buf[512];
result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &msg);
if (result != DNS_R_SUCCESS)
return (result);
msg->opcode = dns_opcode_notify;
msg->rdclass = rdclass;
msg->id = htons(3456); /* XXX */
/* result = dns_message_gettemprdatalist(msg, &rdatalist); */
ISC_LIST_INIT(rdatalist.rdata);
ISC_LINK_INIT(&rdatalist, link);
rdatalist.type = type;
rdatalist.rdclass = rdclass;
rdatalist.ttl = 0;
result = dns_message_gettemprdataset(msg, &rdataset);
if (result != DNS_R_SUCCESS)
goto cleanup;
dns_rdataset_init(rdataset);
dns_rdatalist_tordataset(&rdatalist, rdataset);
ISC_LIST_APPEND(name->list, rdataset, link);
dns_message_addname(msg, name, DNS_SECTION_QUESTION);
isc_buffer_init(&target, buf, sizeof buf, ISC_BUFFERTYPE_BINARY);
result = dns_message_renderbegin(msg, &target);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_message_rendersection(msg, DNS_SECTION_QUESTION, 0, 0);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_message_rendersection(msg, DNS_SECTION_ANSWER, 0, 0);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_message_rendersection(msg, DNS_SECTION_AUTHORITY, 0, 0);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_message_rendersection(msg, DNS_SECTION_ADDITIONAL, 0, 0);
if (result != DNS_R_SUCCESS)
goto cleanup;
/* XXX TSIG here */
result = dns_message_renderend(msg);
if (result != DNS_R_SUCCESS)
goto cleanup;
/* XXX Queue for sending */
addr = addr; /* XXX */
source = source; /* XXX */
cleanup:
dns_message_destroy(&msg);
return (result);
}
dns_result_t
dns_zone_notifyreceive(dns_zone_t *zone, isc_sockaddr_t *from,
dns_message_t *msg)
{
unsigned int i;
dns_rdata_soa_t soa;
dns_rdataset_t *rdataset = NULL;
dns_rdata_t rdata;
dns_result_t result;
REQUIRE(DNS_ZONE_VALID(zone));
/*
* If type != T_SOA return DNS_R_REFUSED. We don't yet support
* ROLLOVER.
*
* SOA: RFC 1996
* Check that 'from' is a valid notify source, (zone->masters).
* Return DNS_R_REFUSED if not.
*
* If the notify message contains a serial number check it
* against the zones serial and return if <= current serial
*
* If a refresh check is progress, if so just record the
* fact we received a NOTIFY and from where and return.
* We will perform a new refresh check when the current one
* completes. Return DNS_R_SUCCESS.
*
* Otherwise initiate a refresh check using 'from' as the
* first address to check. Return DNS_R_SUCCESS.
*/
/*
* We only handle NOTIFY (SOA) at the present.
*/
LOCK(&zone->lock);
if (msg->counts[DNS_SECTION_QUESTION] != 0 ||
dns_message_findname(msg, DNS_SECTION_QUESTION, &zone->origin,
dns_rdatatype_soa, dns_rdatatype_none,
NULL, NULL) != DNS_R_SUCCESS) {
UNLOCK(&zone->lock);
return (DNS_R_REFUSED);
}
for (i = 0; i < zone->masterscnt; i++)
if (isc_sockaddr_equal(from, &zone->masters[i]))
break;
if (i >= zone->masterscnt) {
UNLOCK(&zone->lock);
return (DNS_R_REFUSED);
}
/*
* If the zone is loaded and there are answers check the serial
* to see if we need to do a refresh. Do not worry about this
* check if we are a dialup zone as we use the notify request
* to trigger a refresh check.
*/
if (msg->counts[DNS_SECTION_ANSWER] > 0 &&
DNS_ZONE_FLAG(zone, DNS_ZONE_F_LOADED) &&
!DNS_ZONE_OPTION(zone, DNS_ZONE_O_DIALUP)) {
result = dns_message_findname(msg, DNS_SECTION_ANSWER,
&zone->origin,
dns_rdatatype_soa,
dns_rdatatype_none, NULL,
&rdataset);
if (result == DNS_R_SUCCESS)
result = dns_rdataset_first(rdataset);
if (result == DNS_R_SUCCESS) {
isc_uint32_t serial = 0;
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &soa, zone->mctx);
if (result == DNS_R_SUCCESS) {
serial = soa.serial;
dns_rdata_freestruct(&soa);
if (isc_serial_le(serial, zone->serial))
return (DNS_R_SUCCESS);
}
}
}
/*
* If we got this far and there was a refresh in progress just
* let it complete. Record where we got the notify from so we
* can perform a refresh check when the current one completes
*/
if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_REFRESH)) {
zone->flags |= DNS_ZONE_F_NEEDREFRESH;
zone->notifyfrom = *from;
UNLOCK(&zone->lock);
return (DNS_R_SUCCESS);
}
UNLOCK(&zone->lock);
dns_zone_refresh(zone);
return (DNS_R_SUCCESS);
}
/*
* Copy / translate zone configuration data to dns_zone_t.
*/
dns_result_t
dns_zone_copy(isc_log_t *lctx, dns_c_ctx_t *ctx, dns_c_zone_t *czone,
dns_zone_t *zone) {
isc_result_t iresult;
dns_result_t result;
isc_boolean_t boolean;
const char *filename = NULL;
dns_c_ipmatchlist_t *acl = 0;
dns_c_severity_t severity;
dns_c_iplist_t *iplist = NULL;
dns_c_pubkey_t *pubkey = NULL;
dns_c_pklist_t *pubkeylist = NULL;
isc_uint32_t i;
isc_sockaddr_t sockaddr;
isc_int32_t maxxfr;
isc_int32_t idle;
in_port_t port;
const char *origin;
char *o;
isc_sockaddr_t sockaddr_any;
ctx = ctx; /* unused */
lctx = lctx; /* XXX unused */
isc_sockaddr_fromin6(&sockaddr_any, &in6addr_any, 0);
dns_zone_setclass(zone, czone->zclass);
origin = NULL;
result = dns_c_zone_getname(czone, &origin);
if (result != DNS_R_SUCCESS)
return (result);
o = isc_mem_strdup(zone->mctx, origin);
if (o == NULL)
return (DNS_R_NOMEMORY);
result = dns_zone_setorigin(zone, o);
isc_mem_free(zone->mctx, o);
if (result != DNS_R_SUCCESS)
return (result);
/* XXX needs to be an zone option */
result = dns_zone_setdbtype(zone, "rbt");
if (result != DNS_R_SUCCESS)
return (result);
switch (czone->ztype) {
case dns_c_zone_master:
dns_zone_settype(zone, dns_zone_master);
iresult = dns_c_zone_getfile(czone, &filename);
if (iresult != ISC_R_SUCCESS)
return (iresult);
result = dns_zone_setdatabase(zone, filename);
if (result != DNS_R_SUCCESS)
return (iresult);
iresult = dns_c_zone_getchecknames(czone, &severity);
if (iresult == ISC_R_SUCCESS)
dns_zone_setchecknames(zone, severity);
else
dns_zone_setchecknames(zone, dns_c_severity_fail);
iresult = dns_c_zone_getallowupd(czone, &acl);
if (iresult == ISC_R_SUCCESS) {
dns_zone_setupdateacl(zone, acl);
dns_c_ipmatchlist_detach(&acl);
} else
dns_zone_clearupdateacl(zone);
iresult = dns_c_zone_getallowquery(czone, &acl);
if (iresult == ISC_R_SUCCESS) {
dns_zone_setqueryacl(zone, acl);
dns_c_ipmatchlist_detach(&acl);
} else
dns_zone_clearqueryacl(zone);
iresult = dns_c_zone_getallowtransfer(czone, &acl);
if (iresult == ISC_R_SUCCESS) {
dns_zone_setxfracl(zone, acl);
dns_c_ipmatchlist_detach(&acl);
} else
dns_zone_clearxfracl(zone);
iresult = dns_c_zone_getdialup(czone, &boolean);
if (iresult == ISC_R_SUCCESS)
dns_zone_setoption(zone, DNS_ZONE_O_DIALUP, boolean);
else
dns_zone_clearoption(zone, DNS_ZONE_O_DIALUP);
iresult = dns_c_zone_getnotify(czone, &boolean);
if (iresult == ISC_R_SUCCESS)
dns_zone_setoption(zone, DNS_ZONE_O_NOTIFY, boolean);
else
dns_zone_clearoption(zone, DNS_ZONE_O_NOTIFY);
iresult = dns_c_zone_getalsonotify(czone, &iplist);
if (iresult == ISC_R_SUCCESS) {
for (i = 0; i < iplist->nextidx; i++) {
result = dns_zone_addnotify(zone,
&iplist->ips[i]);
if (result != DNS_R_SUCCESS)
return (result);
}
} else
dns_zone_clearnotify(zone);
#if 1 /* XXX brister */
iresult = dns_c_zone_getpubkeylist(czone, &pubkeylist);
if (iresult == ISC_R_SUCCESS) {
pubkey = ISC_LIST_HEAD(pubkeylist->keylist);
}
dns_zone_setpubkey(zone, pubkey);
#else
iresult = dns_c_zone_getpubkey(czone, &pubkey);
if (iresult == ISC_R_SUCCESS)
dns_zone_setpubkey(zone, pubkey);
else
dns_zone_setpubkey(zone, NULL);
#endif
iresult = dns_c_zone_getmaxtranstimeout(czone, &maxxfr);
if (result == ISC_R_SUCCESS)
zone->maxxfrout = maxxfr;
else
zone->maxxfrout = MAX_XFER_TIME;
iresult = dns_c_zone_getmaxtransidleout(czone, &idle);
if (iresult == ISC_R_SUCCESS)
dns_zone_setidleout(zone, idle);
else
dns_zone_setidleout(zone, 0);
break;
case dns_c_zone_forward:
#ifdef notyet
/*
* forward zones are still in a state of flux
*/
czone->u.fzone.check_names; /* XXX unused in BIND 8 */
czone->u.fzone.forward; /* XXX*/
czone->u.fzone.forwarders; /* XXX*/
#endif
break;
case dns_c_zone_slave:
dns_zone_settype(zone, dns_zone_slave);
iresult = dns_c_zone_getfile(czone, &filename);
if (iresult != ISC_R_SUCCESS)
return (iresult);
result = dns_zone_setdatabase(zone, filename);
if (result != DNS_R_SUCCESS)
return (iresult);
iresult = dns_c_zone_getchecknames(czone, &severity);
if (iresult == ISC_R_SUCCESS)
dns_zone_setchecknames(zone, severity);
else
dns_zone_setchecknames(zone, dns_c_severity_warn);
iresult = dns_c_zone_getallowquery(czone, &acl);
if (iresult == ISC_R_SUCCESS) {
dns_zone_setqueryacl(zone, acl);
dns_c_ipmatchlist_detach(&acl);
} else
dns_zone_clearqueryacl(zone);
#if 1 /* XXX brister */
iresult = dns_c_zone_getpubkeylist(czone, &pubkeylist);
if (iresult == ISC_R_SUCCESS) {
pubkey = ISC_LIST_HEAD(pubkeylist->keylist);
}
dns_zone_setpubkey(zone, pubkey);
#else
iresult = dns_c_zone_getpubkey(czone, &pubkey);
if (iresult == ISC_R_SUCCESS)
dns_zone_setpubkey(zone, pubkey);
else
dns_zone_setpubkey(zone, NULL);
#endif
iresult = dns_c_zone_getmasterport(czone, &port);
if (iresult != ISC_R_SUCCESS)
port = 53;
dns_zone_setmasterport(zone, port);
iresult = dns_c_zone_getmasterips(czone, &iplist);
if (iresult == ISC_R_SUCCESS) {
for (i = 0; i < iplist->nextidx; i++) {
result = dns_zone_addmaster(zone,
&iplist->ips[i]);
if (result != DNS_R_SUCCESS)
return (result);
}
} else
dns_zone_clearmasters(zone);
iresult = dns_c_zone_getmaxtranstimein(czone, &maxxfr);
if (result == ISC_R_SUCCESS)
zone->maxxfrin = maxxfr;
else
zone->maxxfrin = MAX_XFER_TIME;
iresult = dns_c_zone_gettransfersource(czone, &sockaddr);
if (iresult == ISC_R_SUCCESS)
zone->xfrsource = sockaddr;
else
zone->xfrsource = sockaddr_any;
iresult = dns_c_zone_getmaxtransidlein(czone, &idle);
if (iresult == ISC_R_SUCCESS)
dns_zone_setidlein(zone, idle);
else
dns_zone_setidlein(zone, 0);
iresult = dns_c_zone_getmaxtranstimeout(czone, &maxxfr);
if (result == ISC_R_SUCCESS)
zone->maxxfrout = maxxfr;
else
zone->maxxfrout = MAX_XFER_TIME;
iresult = dns_c_zone_getmaxtransidleout(czone, &idle);
if (iresult == ISC_R_SUCCESS)
dns_zone_setidleout(zone, idle);
else
dns_zone_setidleout(zone, 0);
break;
case dns_c_zone_stub:
dns_zone_settype(zone, dns_zone_stub);
iresult = dns_c_zone_getfile(czone, &filename);
if (iresult != ISC_R_SUCCESS)
return (iresult);
result = dns_zone_setdatabase(zone, filename);
if (result != DNS_R_SUCCESS)
return (iresult);
iresult = dns_c_zone_getchecknames(czone, &severity);
if (iresult == ISC_R_SUCCESS)
dns_zone_setchecknames(zone, severity);
else
dns_zone_setchecknames(zone, dns_c_severity_warn);
iresult = dns_c_zone_getallowquery(czone, &acl);
if (iresult == ISC_R_SUCCESS) {
dns_zone_setqueryacl(zone, acl);
dns_c_ipmatchlist_detach(&acl);
} else
dns_zone_clearqueryacl(zone);
#if 1 /* XXX brister */
iresult = dns_c_zone_getpubkeylist(czone, &pubkeylist);
if (iresult == ISC_R_SUCCESS) {
pubkey = ISC_LIST_HEAD(pubkeylist->keylist);
}
dns_zone_setpubkey(zone, pubkey);
#else
iresult = dns_c_zone_getpubkey(czone, &pubkey);
if (iresult == ISC_R_SUCCESS)
dns_zone_setpubkey(zone, pubkey);
else
dns_zone_setpubkey(zone, NULL);
#endif
iresult = dns_c_zone_getmasterport(czone, &port);
if (iresult != ISC_R_SUCCESS)
port = 53;
dns_zone_setmasterport(zone, port);
iresult = dns_c_zone_getmasterips(czone, &iplist);
if (iresult == ISC_R_SUCCESS) {
for (i = 0; i < iplist->nextidx; i++) {
result = dns_zone_addmaster(zone,
&iplist->ips[i]);
if (result != DNS_R_SUCCESS)
return (result);
}
} else
dns_zone_clearmasters(zone);
iresult = dns_c_zone_getmaxtranstimein(czone, &maxxfr);
if (result == ISC_R_SUCCESS)
zone->maxxfrin = maxxfr;
else
zone->maxxfrin = MAX_XFER_TIME;
iresult = dns_c_zone_gettransfersource(czone, &sockaddr);
if (iresult == ISC_R_SUCCESS)
zone->xfrsource = sockaddr;
else
zone->xfrsource = sockaddr_any;
iresult = dns_c_zone_getmaxtransidlein(czone, &idle);
if (iresult == ISC_R_SUCCESS)
dns_zone_setidlein(zone, idle);
else
dns_zone_setidlein(zone, 0);
break;
case dns_c_zone_hint:
dns_zone_settype(zone, dns_zone_hint);
iresult = dns_c_zone_getfile(czone, &filename);
if (iresult != ISC_R_SUCCESS)
return (iresult);
result = dns_zone_setdatabase(zone, filename);
if (result != DNS_R_SUCCESS)
return (result);
iresult = dns_c_zone_getchecknames(czone, &severity);
if (iresult == ISC_R_SUCCESS)
dns_zone_setchecknames(zone, severity);
else
dns_zone_setchecknames(zone, dns_c_severity_fail);
#if 1 /* XXX brister */
iresult = dns_c_zone_getpubkeylist(czone, &pubkeylist);
if (iresult == ISC_R_SUCCESS) {
pubkey = ISC_LIST_HEAD(pubkeylist->keylist);
}
dns_zone_setpubkey(zone, pubkey);
#else
iresult = dns_c_zone_getpubkey(czone, &pubkey);
if (iresult == ISC_R_SUCCESS)
dns_zone_setpubkey(zone, pubkey);
else
dns_zone_setpubkey(zone, NULL);
#endif
break;
}
return (DNS_R_SUCCESS);
}
void
dns_zone_setqueryacl(dns_zone_t *zone, dns_c_ipmatchlist_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->query_acl != NULL)
dns_c_ipmatchlist_detach(&zone->query_acl);
dns_c_ipmatchlist_attach(acl, &zone->query_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_setupdateacl(dns_zone_t *zone, dns_c_ipmatchlist_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->update_acl != NULL)
dns_c_ipmatchlist_detach(&zone->update_acl);
dns_c_ipmatchlist_attach(acl, &zone->update_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_setxfracl(dns_zone_t *zone, dns_c_ipmatchlist_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->xfr_acl != NULL)
dns_c_ipmatchlist_detach(&zone->xfr_acl);
dns_c_ipmatchlist_attach(acl, &zone->xfr_acl);
UNLOCK(&zone->lock);
}
dns_c_ipmatchlist_t *
dns_zone_getqueryacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->query_acl);
}
dns_c_ipmatchlist_t *
dns_zone_getupdateacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->update_acl);
}
dns_c_ipmatchlist_t *
dns_zone_getxfracl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->xfr_acl);
}
void
dns_zone_clearupdateacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->update_acl != NULL)
dns_c_ipmatchlist_detach(&zone->update_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_clearqueryacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->query_acl != NULL)
dns_c_ipmatchlist_detach(&zone->query_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_clearxfracl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->xfr_acl != NULL)
dns_c_ipmatchlist_detach(&zone->xfr_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_setchecknames(dns_zone_t *zone, dns_c_severity_t severity) {
REQUIRE(DNS_ZONE_VALID(zone));
zone->check_names = severity;
}
dns_c_severity_t
dns_zone_getchecknames(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->check_names);
}
void
dns_zone_setpubkey(dns_zone_t *zone, dns_c_pubkey_t *pubkey) {
REQUIRE(DNS_ZONE_VALID(zone));
zone->pubkey = pubkey; /* XXX should be an attach */
}
dns_c_pubkey_t *
dns_zone_getpubkey(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->pubkey);
}
void
dns_zone_setjournalsize(dns_zone_t *zone, isc_int32_t size) {
REQUIRE(DNS_ZONE_VALID(zone));
zone->journalsize = size;
}
isc_int32_t
dns_zone_getjournalsize(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->journalsize);
}
void
dns_zone_setmasterport(dns_zone_t *zone, in_port_t port) {
REQUIRE(DNS_ZONE_VALID(zone));
zone->masterport = port;
}
in_port_t
dns_zone_getmasterport(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->masterport);
}
static void
zone_log(dns_zone_t *zone, const char *me, int level,
const char *fmt, ...) {
va_list ap;
char message[4096];
char namebuf[1024+32];
isc_buffer_t buffer;
int len;
dns_result_t result;
isc_buffer_init(&buffer, namebuf, sizeof namebuf, ISC_BUFFERTYPE_TEXT);
result = dns_name_totext(&zone->origin, ISC_FALSE, &buffer);
if (result != DNS_R_SUCCESS)
(void)isc_buffer_putstr(&buffer, "<UNKNOWN>");
(void)isc_buffer_putstr(&buffer, "/");
(void)dns_rdataclass_totext(zone->rdclass, &buffer);
len = buffer.used; /* XXX */
va_start(ap, fmt);
vsnprintf(message, sizeof message, fmt, ap);
va_end(ap);
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE,
level, "%s: zone %.*s: %s", me, len, namebuf, message);
}
static int
message_count(dns_message_t *msg, dns_section_t section, dns_rdatatype_t type) {
dns_result_t result;
dns_name_t *name;
dns_rdataset_t *curr;
int res = 0;
result = dns_message_firstname(msg, section);
while (result == DNS_R_SUCCESS) {
name = NULL;
dns_message_currentname(msg, section, &name);
for (curr = ISC_LIST_TAIL(name->list); curr != NULL;
curr = ISC_LIST_PREV(curr, link)) {
if (curr->type == type)
res++;
}
result = dns_message_nextname(msg, section);
}
return (res);
}
void
dns_zone_setresolver(dns_zone_t *zone, dns_resolver_t *resolver) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->res != NULL)
dns_resolver_detach(&zone->res);
dns_resolver_attach(resolver, &zone->res);
UNLOCK(&zone->lock);
}
void
dns_zone_setmaxxfrin(dns_zone_t *zone, isc_uint32_t maxxfrin) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(maxxfrin != 0);
zone->maxxfrin = maxxfrin;
}
isc_uint32_t
dns_zone_getmaxxfrin(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->maxxfrin);
}
void
dns_zone_setmaxxfrout(dns_zone_t *zone, isc_uint32_t maxxfrout) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(maxxfrout != 0);
zone->maxxfrout = maxxfrout;
}
isc_uint32_t
dns_zone_getmaxxfrout(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->maxxfrout);
}
void
dns_zone_transfer_in(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
fprintf(stdout, "dns_zone_transfer_in\n");
}
dns_zonetype_t dns_zone_gettype(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->type);
}
dns_name_t *
dns_zone_getorigin(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (&zone->origin);
}
void
dns_zone_settask(dns_zone_t *zone, isc_task_t *task) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->task != NULL)
isc_task_detach(&zone->task);
isc_task_attach(task, &zone->task);
UNLOCK(&zone->lock);
}
void
dns_zone_gettask(dns_zone_t *zone, isc_task_t **target) {
REQUIRE(DNS_ZONE_VALID(zone));
isc_task_attach(zone->task, target);
}
const char *
dns_zone_getdatabase(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->database);
}
void
dns_zone_setidlein(dns_zone_t *zone, isc_uint32_t idlein) {
REQUIRE(DNS_ZONE_VALID(zone));
if (idlein == 0)
idlein = DNS_DEFAULT_IDLEIN;
zone->idlein = idlein;
}
isc_uint32_t
dns_zone_getidlein(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->idlein);
}
void
dns_zone_setidleout(dns_zone_t *zone, isc_uint32_t idleout) {
REQUIRE(DNS_ZONE_VALID(zone));
if (idleout == 0)
idleout = DNS_DEFAULT_IDLEOUT;
zone->idleout = idleout;
}
isc_uint32_t
dns_zone_getidleout(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->idleout);
}
static void
record_serial() {
}
isc_boolean_t
dns_zone_equal(dns_zone_t *oldzone, dns_zone_t *newzone) {
unsigned int i;
REQUIRE(DNS_ZONE_VALID(oldzone));
REQUIRE(DNS_ZONE_VALID(newzone));
LOCK(&oldzone->lock);
LOCK(&newzone->lock);
if (oldzone->type != newzone->type ||
oldzone->maxxfrin != newzone->maxxfrin ||
oldzone->maxxfrout != newzone->maxxfrout ||
oldzone->idlein != newzone->idlein ||
oldzone->idleout != newzone->idleout ||
oldzone->rdclass != newzone->rdclass ||
oldzone->db_argc != newzone->db_argc ||
oldzone->notifycnt != newzone->notifycnt ||
oldzone->masterscnt != newzone->masterscnt ||
oldzone->masterport != newzone->masterport ||
oldzone->check_names != newzone->check_names ||
oldzone->diff_on_reload != newzone->diff_on_reload ||
oldzone->journalsize != newzone->journalsize)
goto false;
if (!dns_name_equal(&oldzone->origin, &newzone->origin))
goto false;
if ((oldzone->journal == NULL && newzone->journal != NULL) ||
(oldzone->journal != NULL && newzone->journal == NULL) ||
(oldzone->journal != NULL &&
strcmp(oldzone->journal, newzone->journal) != 0))
goto false;
if ((oldzone->options & oldzone->setoptions) !=
(newzone->options & newzone->setoptions))
goto false;
if ((oldzone->db_type == NULL && newzone->db_type != NULL) ||
(oldzone->db_type != NULL && newzone->db_type == NULL) ||
(oldzone->db_type != NULL &&
strcmp(oldzone->db_type, newzone->db_type) != 0))
goto false;
for (i = 0; i < oldzone->db_argc; i++)
if (strcmp(oldzone->db_argv[i], newzone->db_argv[i]) != 0)
goto false;
if (!isc_sockaddr_equal(&oldzone->xfrsource, &newzone->xfrsource))
goto false;
for (i = 0; i < oldzone->notifycnt; i++)
if (!isc_sockaddr_equal(&oldzone->notify[i],
&newzone->notify[i]))
goto false;
for (i = 0; i < oldzone->masterscnt; i++)
if (!isc_sockaddr_equal(&oldzone->masters[i],
&newzone->masters[i]))
goto false;
if (!dns_c_ipmatchlist_equal(oldzone->update_acl, newzone->update_acl))
goto false;
if (!dns_c_ipmatchlist_equal(oldzone->query_acl, newzone->query_acl))
goto false;
if (!dns_c_ipmatchlist_equal(oldzone->xfr_acl, newzone->xfr_acl))
goto false;
if (!dns_c_pubkey_equal(oldzone->pubkey, newzone->pubkey))
goto false;
UNLOCK(&newzone->lock);
UNLOCK(&oldzone->lock);
return(ISC_TRUE); /* XXX should be ISC_TRUE once acl/pubkey
checks are done. */
false:
UNLOCK(&newzone->lock);
UNLOCK(&oldzone->lock);
return (ISC_FALSE);
}
dns_result_t
dns_zone_replacedb(dns_zone_t *zone, dns_db_t *db, isc_boolean_t dump) {
dns_result_t result;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
result = replacedb(zone, db, dump);
UNLOCK(&zone->lock);
return (result);
}
static dns_result_t
replacedb(dns_zone_t *zone, dns_db_t *db, isc_boolean_t dump) {
dns_dbversion_t *ver;
isc_result_t result;
REQUIRE(DNS_ZONE_VALID(zone));
ver = NULL;
dns_db_currentversion(db, &ver);
/*
* The initial version of a slave zone is always dumped;
* subsequent versions may be journalled instead if this
* is enabled in the configuration.
*/
if (zone->top != NULL && zone->journal != NULL &&
zone->diff_on_reload) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
"generating diffs");
result = dns_db_diff(zone->mctx,
db, ver,
zone->top, NULL /* XXX */,
zone->journal);
if (result != DNS_R_SUCCESS)
goto fail;
} else {
if (dump) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
"dumping new zone version");
/* XXX should use temporary file and rename */
result = dns_db_dump(db, ver, zone->database);
if (result != DNS_R_SUCCESS)
goto fail;
}
if (zone->journal != NULL) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
"removing journal file");
(void) remove(zone->journal);
}
}
dns_db_closeversion(db, &ver, ISC_FALSE);
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
"replacing zone database");
if (zone->top != NULL)
dns_db_detach(&zone->top);
dns_db_attach(db, &zone->top);
zone->flags |= DNS_ZONE_F_LOADED;
return (DNS_R_SUCCESS);
fail:
dns_db_closeversion(db, &ver, ISC_FALSE);
return (result);
}
static void
xfrdone(dns_zone_t *zone, dns_result_t result) {
const char me[] = "xfrdone";
isc_stdtime_t now;
REQUIRE(DNS_ZONE_VALID(zone));
zone_log(zone, me, ISC_LOG_DEBUG(1), "%s", dns_result_totext(result));
LOCK(&zone->lock);
INSIST((zone->flags & DNS_ZONE_F_REFRESH) != 0);
zone->flags &= ~DNS_ZONE_F_REFRESH;
switch (result) {
case DNS_R_UPTODATE:
case DNS_R_SUCCESS:
if (isc_stdtime_get(&now) != ISC_R_SUCCESS)
return;
zone->refreshtime = now + zone->refresh;
zone_settimer(zone, now);
break;
default:
zone->curmaster++;
if (zone->curmaster >= zone->masterscnt)
zone->curmaster = 0;
else
xfrin_start_temporary_kludge(zone);
break;
}
UNLOCK(&zone->lock);
}
/***
*** Zone manager.
***/
static void
xfrin_start_temporary_kludge(dns_zone_t *zone) {
isc_sockaddr_t sa;
in_port_t port;
if (zone->masterscnt < 1)
return;
port = zone->masterport;
if (port == 0)
port = 53; /* XXX is this the right place? */
isc_sockaddr_fromin(&sa,
&zone->masters[zone->curmaster].type.sin.sin_addr,
port);
dns_xfrin_start(zone, &sa, zone->mctx,
zone->zmgr->taskmgr, zone->zmgr->timermgr,
zone->zmgr->socketmgr,
xfrdone);
}
isc_result_t
dns_zonemgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
isc_timermgr_t *timermgr, isc_socketmgr_t *socketmgr,
dns_zonemgr_t **zmgrp)
{
dns_zonemgr_t *zmgr;
isc_result_t result;
zmgr = isc_mem_get(mctx, sizeof *zmgr);
if (zmgr == NULL)
return (ISC_R_NOMEMORY);
zmgr->mctx = mctx;
zmgr->taskmgr = taskmgr;
zmgr->timermgr = timermgr;
zmgr->socketmgr = socketmgr;
zmgr->zonetasks = NULL;
zmgr->soaquery.task = NULL;
ISC_LIST_INIT(zmgr->zones);
result = isc_rwlock_init(&zmgr->rwlock, 0, 0);
if (result != ISC_R_SUCCESS) {
isc_mem_put(mctx, zmgr, sizeof *zmgr);
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_rwlock_init() failed: %s",
isc_result_totext(result));
return (DNS_R_UNEXPECTED);
}
/* Create the zone task pool. */
result = isc_taskpool_create(taskmgr, mctx,
8 /* XXX */, 0, &zmgr->zonetasks);
if (result != ISC_R_SUCCESS)
goto failure;
/* Create a single task for queueing of SOA queries. */
result = isc_task_create(taskmgr, mctx, 1, &zmgr->soaquery.task);
if (result != ISC_R_SUCCESS)
goto failure;
*zmgrp = zmgr;
return (ISC_R_SUCCESS);
failure:
dns_zonemgr_destroy(&zmgr);
return (result);
}
isc_result_t
dns_zonemgr_managezone(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
isc_result_t result;
REQUIRE(DNS_ZONE_VALID(zone));
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
LOCK(&zone->lock);
REQUIRE(zone->task == NULL);
REQUIRE(zone->timer == NULL);
REQUIRE(zone->zmgr == NULL);
isc_taskpool_gettask(zmgr->zonetasks,
dns_name_hash(dns_zone_getorigin(zone),
ISC_FALSE),
&zone->task);
result = isc_timer_create(zmgr->timermgr, isc_timertype_inactive,
NULL, NULL,
zmgr->soaquery.task, zone_timer, zone,
&zone->timer);
if (result != ISC_R_SUCCESS)
goto failure;
zone->zmgr = zmgr;
ISC_LIST_APPEND(zmgr->zones, zone, link);
goto cleanup;
failure:
if (zone->task != NULL)
isc_task_detach(&zone->task);
cleanup:
UNLOCK(&zone->lock);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
return (result);
}
static void
releasezone(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
/*
* Caller to lock zone and zmgr
*/
ISC_LIST_UNLINK(zmgr->zones, zone, link);
zone->zmgr = NULL;
}
void
dns_zonemgr_releasezone(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
LOCK(&zone->lock);
releasezone(zmgr, zone);
UNLOCK(&zone->lock);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
}
isc_result_t
dns_zonemgr_forcemaint(dns_zonemgr_t *zmgr) {
dns_zone_t *p;
RWLOCK(&zmgr->rwlock, isc_rwlocktype_read);
for (p = ISC_LIST_HEAD(zmgr->zones);
p != NULL;
p = ISC_LIST_NEXT(p, link))
{
dns_zone_maintenance(p);
}
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_read);
return (ISC_R_SUCCESS);
}
void
dns_zonemgr_destroy(dns_zonemgr_t **zmgrp) {
dns_zonemgr_t *zmgr = *zmgrp;
dns_zone_t *zone;
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
zone = ISC_LIST_HEAD(zmgr->zones);
while (zone != NULL) {
LOCK(&zone->lock);
releasezone(zmgr, zone);
UNLOCK(&zone->lock);
zone = ISC_LIST_HEAD(zmgr->zones);
}
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
if (zmgr->soaquery.task != NULL)
isc_task_destroy(&zmgr->soaquery.task);
if (zmgr->zonetasks != NULL)
isc_taskpool_destroy(&zmgr->zonetasks);
isc_rwlock_destroy(&zmgr->rwlock);
isc_mem_put(zmgr->mctx, zmgr, sizeof *zmgr);
*zmgrp = NULL;
}