zone.c revision 6e70b15670957879f537a120a1fa3fa8e4a8307b
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence/*
c110d61b173a68420d19858abb80285be0dc1120Tinderbox User * Copyright (C) 1999, 2000 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
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * copyright notice and this permission notice appear in all copies.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *
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.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /* $Id: zone.c,v 1.82 2000/02/24 21:40:51 gson Exp $ */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <config.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <string.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <isc/assertions.h>
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <isc/error.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/magic.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/mktemplate.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/print.h>
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence#include <isc/quota.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/serial.h>
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#include <isc/taskpool.h>
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington#include <isc/timer.h>
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#include <isc/ufile.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/util.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#include <dns/acl.h>
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence#include <dns/db.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/dbiterator.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/dispatch.h>
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence#include <dns/events.h>
e19501436a92cd48eba2ff47d90fa49c661ec8d8Brian Wellington#include <dns/journal.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <dns/log.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <dns/master.h>
669e9657c731176df235832367f61435f7b83ddfAndreas Gustafsson#include <dns/masterdump.h>
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence#include <dns/message.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/rcode.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/rdata.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/rdatalist.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/rdatasetiter.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/rdatastruct.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/resolver.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/ssu.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/xfrin.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/zone.h>
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews#include <dns/zt.h>
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington/* XXX remove once config changes are in place */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define dns_zone_uptodate(x) zone_log(x, me, ISC_LOG_INFO, "dns_zone_uptodate")
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#define referral(x) ISC_FALSE
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
3f96cf3e4f96b36cc1ad2ec7edc5b8e285fced8fBrian Wellington#include <stdarg.h>
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
7318a964ece83f748bc7e9814d8c3a61c2b4d946Mark Andrews#define ZONE_MAGIC 0x5a4f4e45U
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence#define CHECKSERVERS_MAGIC 0x43484346U
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence
80b67b3a4f2d9fc7fdd32a50edc67ff189894da2Danny Mayer#define DNS_ZONE_VALID(zone) \
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews ISC_MAGIC_VALID(zone, ZONE_MAGIC)
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence#define DNS_CHECKSERVERS_VALID(server) \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ISC_MAGIC_VALID(zone, CHECKSERVERS_MAGIC)
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#ifndef DNS_GLOBAL_OPTION /* XXX MPA */
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#define DNS_GLOBAL_OPTION(o) 0
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington#endif
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews#define DEFAULT_REFRESH 900 /*XXX*/
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define DEFAULT_RETRY 300 /*XXX*/
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define MAX_XFER_TIME (2*3600) /* Documented default is 2 hours. */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt#define RANGE(a, b, c) (((a) < (b)) ? (b) : ((a) < (c) ? (a) : (c)))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define DNS_MIN_REFRESH 2
4144efb39046963989ad002cf88a0c195401100aJeremy Reed#define DNS_MAX_REFRESH 2419200 /* 4 weeks */
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt#define DNS_MIN_RETRY 1
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington#define DNS_MAX_RETRY 1209600 /* 2 weeks */
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#define DNS_MAX_EXPIRE 14515200 /* 24 weeks */
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#define DNS_DEFAULT_IDLEIN 3600 /* 1 hour */
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington#define DNS_DEFAULT_IDLEOUT 3600 /* 1 hour */
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewstypedef enum {
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt get_a6, get_aaaa, get_a, get_ns, get_soa
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt} dns_zone_state_t;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtontypedef struct dns_zone_checkservers {
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington isc_uint32_t magic;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont isc_boolean_t name_known;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont dns_name_t server;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont isc_sockaddr_t address;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_zone_state_t state;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence dns_zone_t *zone;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_resolver_t *res;
5b79d154014f87b6c54b1ec2d3912c35b02042a1Mark Andrews isc_mem_t *mctx;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt dns_fetch_t *fetch;
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence ISC_LINK(struct dns_zone_checkservers) link;
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson} dns_zone_checkservers_t;
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrewsstruct dns_zone {
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman /* Unlocked */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews unsigned int magic;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman isc_mutex_t lock;
1479200aa05414b2acf33607dbd1682c16f58c51Evan Hunt isc_mem_t *mctx;
b326d7e3a3a50eb65dd06db007d2fddc62606bbfMark Andrews
5455f30a7532738d750252c00e649890c694ee30Brian Wellington /* Locked */
5455f30a7532738d750252c00e649890c694ee30Brian Wellington dns_db_t *top;
60213f2815a7e6584a2285546d05633fa7b6f5b4Mark Andrews dns_zonemgr_t *zmgr;
c30d291128e099a284fa6272b91b2bd64519a209Mark Andrews ISC_LINK(dns_zone_t) link; /* Used by zmgr. */
6150d3cb666a58d5e3a15275562c9fc5c5b6b2d8Evan Hunt isc_timer_t *timer;
6150d3cb666a58d5e3a15275562c9fc5c5b6b2d8Evan Hunt unsigned int erefs;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews unsigned int irefs;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_boolean_t shuttingdown;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_name_t origin;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews char *database;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews char *journal;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_int32_t journalsize;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_rdataclass_t rdclass;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_zonetype_t type;
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews unsigned int flags;
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews unsigned int options;
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews unsigned int setoptions;
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews char * db_type;
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews unsigned int db_argc;
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews char ** db_argv;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman isc_stdtime_t expiretime;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman isc_stdtime_t refreshtime;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman isc_stdtime_t dumptime;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_stdtime_t servertime;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_stdtime_t parenttime;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_stdtime_t childtime;
323bb31d7c54078aa62146b3aa946b755cbfd52bMark Andrews isc_uint32_t serial;
323bb31d7c54078aa62146b3aa946b755cbfd52bMark Andrews isc_uint32_t refresh;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt isc_uint32_t retry;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt isc_uint32_t expire;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt isc_uint32_t minimum;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt isc_sockaddr_t * masters;
629a0159401a6c0d991a78a6d0b90ee84e83668cEvan Hunt unsigned int masterscnt;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt in_port_t masterport;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt unsigned int curmaster;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt isc_sockaddr_t * notify;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt unsigned int notifycnt;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt isc_sockaddr_t notifyfrom;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_task_t * task;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_sockaddr_t xfrsource4;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_sockaddr_t xfrsource6;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_xfrin_ctx_t * xfr;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews /* Access Control Lists */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_acl_t *update_acl;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_acl_t *query_acl;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_acl_t *xfr_acl;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_severity_t check_names;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews ISC_LIST(dns_zone_checkservers_t) checkservers;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews dns_fetch_t *fetch;
62ec9fd1681ffae7d6b0d54618599ecf650e3100Mark Andrews dns_resolver_t *res;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_socketmgr_t *socketmgr;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_uint32_t maxxfrin;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman isc_uint32_t maxxfrout;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman isc_uint32_t idlein;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_uint32_t idleout;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_boolean_t diff_on_reload;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt isc_event_t ctlevent;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt dns_ssutable_t *ssutable;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt dns_view_t *view;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews};
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_FLAG(z,f) (((z)->flags & (f)) != 0)
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews /* XXX MPA these may need to go back into zone.h */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_F_REFRESH 0x00000001U /* refresh check in progress */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_F_NEEDDUMP 0x00000002U /* zone need consolidation */
9bd876a683709be588f6fac6781a76fdd57b2f08Mark Andrews#define DNS_ZONE_F_SERVERS 0x00000004U /* servers check in progress */
9bd876a683709be588f6fac6781a76fdd57b2f08Mark Andrews#define DNS_ZONE_F_PARENTS 0x00000008U /* parents check in progress */
9bd876a683709be588f6fac6781a76fdd57b2f08Mark Andrews#define DNS_ZONE_F_CHILDREN 0x00000010U /* child check in progress */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_F_LOADED 0x00000020U /* database has loaded */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_F_EXITING 0x00000040U /* zone is being destroyed */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_F_EXPIRED 0x00000080U /* zone has expired */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_F_NEEDREFRESH 0x00000100U /* refresh check needed */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_F_UPTODATE 0x00000200U /* zone contents are
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews * uptodate */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews#define DNS_ZONE_OPTION(z,o) ((((z)->setoptions & (o)) != 0) ? \
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews (((z)->options & (o)) != 0) : \
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews DNS_GLOBAL_OPTION(o))
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrewsstruct dns_zonemgr {
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_mem_t * mctx;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_taskmgr_t * taskmgr;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_timermgr_t * timermgr;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_socketmgr_t * socketmgr;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_taskpool_t * zonetasks;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_task_t * task;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_rwlock_t rwlock;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_rwlock_t conflock;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews /* Locked by rwlock. */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews ISC_LIST(dns_zone_t) zones;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews /* Maximum locked by conflock. */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_quota_t transfersin;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews};
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence
87983da955bf63128de85d180359bdc418516c3cDavid Lawrencestatic isc_result_t zone_settimer(dns_zone_t *, isc_stdtime_t);
87983da955bf63128de85d180359bdc418516c3cDavid Lawrencestatic void cancel_refresh(dns_zone_t *);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencestatic isc_result_t dns_notify(dns_name_t *, isc_sockaddr_t *, dns_rdatatype_t,
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence dns_rdataclass_t, isc_sockaddr_t *, isc_mem_t *);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrencestatic void zone_log(dns_zone_t *zone, const char *, int, const char *msg,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ...);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonextern void dns_zone_transfer_in(dns_zone_t *zone);
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellingtonstatic isc_result_t dns_zone_tostr(dns_zone_t *zone, isc_mem_t *mctx,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews char **s);
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellingtonstatic void unload(dns_zone_t *zone);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void expire(dns_zone_t *zone);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic isc_result_t replacedb(dns_zone_t *zone, dns_db_t *db,
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews isc_boolean_t dump);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic isc_result_t default_journal(dns_zone_t *zone);
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updaterstatic void releasezone(dns_zonemgr_t *zmgr, dns_zone_t *zone);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void xfrin_start_temporary_kludge(dns_zone_t *zone);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void xfrdone(dns_zone_t *zone, isc_result_t result);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void zone_shutdown(isc_task_t *, isc_event_t *);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews#if 0
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews/* ondestroy example */
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsstatic void dns_zonemgr_dbdestroyed(isc_task_t *task, isc_event_t *event);
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#endif
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#ifdef notyet
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonstatic void refresh_callback(isc_task_t *, isc_event_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void soa_query(dns_zone_t *, isc_taskaction_t);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void checkservers_callback(isc_task_t *task, isc_event_t *event);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic int message_count(dns_message_t *msg, dns_section_t section,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_rdatatype_t type);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void add_address_tocheck(dns_message_t *msg,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_zone_checkservers_t *checkservers,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_rdatatype_t type);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void record_serial(void);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington#endif
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews#define PRINT_ZONE_REF(zone) \
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews do { \
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews char *s = NULL; \
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews isc_result_t r; \
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews r = dns_zone_tostr(zone, zone->mctx, &s); \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (r == DNS_R_SUCCESS) { \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington printf("%p: %s: erefs = %d\n", zone, s, \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->erefs); \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_free(zone->mctx, s); \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } while (0)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define DNS_ENTER zone_log(zone, me, ISC_LOG_DEBUG(10), "enter")
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington#define DNS_LEAVE zone_log(zone, me, ISC_LOG_DEBUG(10), "leave")
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington/***
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington *** Public functions.
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington ***/
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
e1d05d323526e7e65df13a6d3dfbec30f6ddb500Brian Wellingtonisc_result_t
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsdns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_result_t result;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_zone_t *zone;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_sockaddr_t sockaddr_any4;
287910778c57d4836a52b03b697c2ef342d0eaa9Francis Dupont isc_sockaddr_t sockaddr_any6;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews struct in_addr in4addr_any;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(zonep != NULL && *zonep == NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(mctx != NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington in4addr_any.s_addr = htonl(INADDR_ANY);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_sockaddr_fromin(&sockaddr_any4, &in4addr_any, 0);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_sockaddr_fromin6(&sockaddr_any6, &in6addr_any, 0);
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt zone = isc_mem_get(mctx, sizeof *zone);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence if (zone == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (DNS_R_NOMEMORY);
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews result = isc_mutex_init(&zone->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (result != ISC_R_SUCCESS) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, zone, sizeof *zone);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington "isc_mutex_init() failed: %s",
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_result_totext(result));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (DNS_R_UNEXPECTED);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /* XXX MPA check that all elements are initialised */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->mctx = mctx;
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington zone->top = NULL;
927e4c9fecf448bf3894c68fcaf9dc2f89557f3aEvan Hunt zone->zmgr = NULL;
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt ISC_LINK_INIT(zone, link);
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt zone->erefs = 1; /* Implicit attach. */
927e4c9fecf448bf3894c68fcaf9dc2f89557f3aEvan Hunt zone->irefs = 0;
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington zone->shuttingdown = ISC_FALSE;
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington dns_name_init(&zone->origin, NULL);
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington zone->database = NULL;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->journalsize = -1;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->journal = NULL;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->rdclass = dns_rdataclass_none;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->type = dns_zone_none;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->flags = 0;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->options = 0;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->setoptions = 0;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->db_type = NULL;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->db_argc = 0;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt zone->db_argv = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->expiretime = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->refreshtime = 0;
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews zone->dumptime = 0;
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews zone->servertime = 0;
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews zone->parenttime = 0;
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews zone->childtime = 0;
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews zone->serial = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->refresh = DEFAULT_REFRESH;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->retry = DEFAULT_RETRY;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->expire = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->minimum = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->masters = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->masterscnt = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->masterport = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->curmaster = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->notify = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->notifycnt = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->task = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->update_acl = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->query_acl = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->xfr_acl = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->check_names = dns_severity_ignore;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->fetch = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->res = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->socketmgr = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->timer = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->idlein = DNS_DEFAULT_IDLEIN;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->idleout = DNS_DEFAULT_IDLEOUT;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews ISC_LIST_INIT(zone->checkservers);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->xfrsource4 = sockaddr_any4;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt zone->xfrsource6 = sockaddr_any6;
bcae9a15c1e9c50a6e6433168d5225b1de89d6b9Evan Hunt zone->xfr = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->maxxfrin = MAX_XFER_TIME;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->maxxfrout = MAX_XFER_TIME;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->diff_on_reload = ISC_FALSE;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->ssutable = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->view = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->magic = ZONE_MAGIC;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews ISC_EVENT_INIT(&zone->ctlevent, sizeof(zone->ctlevent), 0, NULL,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews DNS_EVENT_ZONECONTROL, zone_shutdown, zone, zone,
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt NULL, NULL);
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt *zonep = zone;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews return (DNS_R_SUCCESS);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews}
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrewsstatic void
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewszone_free(dns_zone_t *zone) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews LOCK(&zone->lock);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews REQUIRE(zone->erefs == 0);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->flags |= DNS_ZONE_F_EXITING;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews UNLOCK(&zone->lock);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews /* managed objects */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews /* order is important */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_REFRESH))
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews cancel_refresh(zone);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->timer != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_timer_detach(&zone->timer);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->res != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_resolver_detach(&zone->res);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->fetch != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_resolver_destroyfetch(&zone->fetch);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->task != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_task_detach(&zone->task);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (zone->socketmgr != NULL)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_socketmgr_destroy(&zone->socketmgr);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (zone->zmgr)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt dns_zonemgr_releasezone(zone->zmgr, zone);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt /* unmanaged objects */
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (zone->database != NULL)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_mem_free(zone->mctx, zone->database);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt zone->database = NULL;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt zone->journalsize = -1;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (zone->journal != NULL)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_mem_free(zone->mctx, zone->journal);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt zone->journal = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->db_type != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_mem_free(zone->mctx, zone->db_type);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->db_type = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->top != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_db_detach(&zone->top);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_zone_cleardbargs(zone);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_zone_clearmasters(zone);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->masterport = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_zone_clearnotify(zone);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews zone->check_names = dns_severity_ignore;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->update_acl != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_acl_detach(&zone->update_acl);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (zone->query_acl != NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews dns_acl_detach(&zone->query_acl);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (zone->xfr_acl != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_acl_detach(&zone->xfr_acl);
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews if (dns_name_dynamic(&zone->origin))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_free(&zone->origin, zone->mctx);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (zone->ssutable != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_ssutable_destroy(&zone->ssutable);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /* last stuff */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mutex_destroy(&zone->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->magic = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(zone->mctx, zone, sizeof *zone);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington}
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington/*
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews * Single shot.
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews */
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellingtonvoid
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrewsdns_zone_setclass(dns_zone_t *zone, dns_rdataclass_t rdclass) {
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington REQUIRE(rdclass != dns_rdataclass_none);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington /* test and set */
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews LOCK(&zone->lock);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington REQUIRE(zone->rdclass == dns_rdataclass_none ||
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington zone->rdclass == rdclass);
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews zone->rdclass = rdclass;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington UNLOCK(&zone->lock);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondns_rdataclass_t
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsdns_zone_getclass(dns_zone_t *zone){
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (zone->rdclass);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington/*
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt * Single shot.
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt */
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Huntvoid
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Huntdns_zone_settype(dns_zone_t *zone, dns_zonetype_t type) {
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt REQUIRE(DNS_ZONE_VALID(zone));
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt REQUIRE(type != dns_zone_none);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt /* test and set */
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt LOCK(&zone->lock);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt REQUIRE(zone->type == dns_zone_none || zone->type == type);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt zone->type = type;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt UNLOCK(&zone->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrewsisc_result_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondns_zone_setdbtype(dns_zone_t *zone, char *db_type) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_result_t result = DNS_R_SUCCESS;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington LOCK(&zone->lock);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington if (zone->db_type != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_free(zone->mctx, zone->db_type);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone->db_type = isc_mem_strdup(zone->mctx, db_type);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (zone->db_type == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = DNS_R_NOMEMORY;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington UNLOCK(&zone->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (result);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews}
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid dns_zone_setview(dns_zone_t *zone, dns_view_t *view) {
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington zone->view = view;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence}
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencedns_view_t *dns_zone_getview(dns_zone_t *zone) {
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence return (zone->view);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews}
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsisc_result_t
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrewsdns_zone_setorigin(dns_zone_t *zone, dns_name_t *origin) {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews isc_result_t result;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
240e53b13217af266abb3dae8ba103614daf2bf7Mark Andrews REQUIRE(origin != NULL);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews LOCK(&zone->lock);
240e53b13217af266abb3dae8ba103614daf2bf7Mark Andrews if (dns_name_dynamic(&zone->origin)) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_name_free(&zone->origin, zone->mctx);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_name_init(&zone->origin, NULL);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = dns_name_dup(origin, zone->mctx, &zone->origin);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington UNLOCK(&zone->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (result);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_result_t
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtondns_zone_setdatabase(dns_zone_t *zone, const char *database) {
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington isc_result_t result = DNS_R_SUCCESS;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington REQUIRE(database != NULL);
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington LOCK(&zone->lock);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington if (zone->database != NULL)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_mem_free(zone->mctx, zone->database);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews zone->database = isc_mem_strdup(zone->mctx, database);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (zone->database == NULL)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence result = DNS_R_NOMEMORY;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence else
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence result = default_journal(zone);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews UNLOCK(&zone->lock);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews return (result);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews}
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic isc_result_t
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsdefault_journal(dns_zone_t *zone) {
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews int len;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews REQUIRE(zone->database != NULL);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews if (zone->journal != NULL)
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews isc_mem_free(zone->mctx, zone->journal);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews len = strlen(zone->database) + sizeof ".jnl"; /* includes '\0' */
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt zone->journal = isc_mem_allocate(zone->mctx, len);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (zone->journal == NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews return (DNS_R_NOMEMORY);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews strcpy(zone->journal, zone->database);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews strcat(zone->journal, ".jnl");
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence return (DNS_R_SUCCESS);
504f7802d4c9b43db4820f496c4d00e078effa18David Lawrence}
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsisc_result_t
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencedns_zone_setjournal(dns_zone_t *zone, const char *journal) {
17a00ff54c51f6bdfddd7d9ceaef6f2dcf387cc1Mark Andrews isc_result_t result = DNS_R_SUCCESS;
17a00ff54c51f6bdfddd7d9ceaef6f2dcf387cc1Mark Andrews
17a00ff54c51f6bdfddd7d9ceaef6f2dcf387cc1Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
504f7802d4c9b43db4820f496c4d00e078effa18David Lawrence REQUIRE(journal != NULL);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews LOCK(&zone->lock);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (zone->journal != NULL)
4144efb39046963989ad002cf88a0c195401100aJeremy Reed isc_mem_free(zone->mctx, zone->journal);
4144efb39046963989ad002cf88a0c195401100aJeremy Reed zone->journal = isc_mem_strdup(zone->mctx, journal);
4144efb39046963989ad002cf88a0c195401100aJeremy Reed if (zone->journal == NULL)
4144efb39046963989ad002cf88a0c195401100aJeremy Reed result = DNS_R_NOMEMORY;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence UNLOCK(&zone->lock);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence return (result);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence}
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencechar *
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencedns_zone_getjournal(dns_zone_t *zone) {
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson return (zone->journal);
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson}
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsvoid
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsdns_zone_validate(dns_zone_t *zone) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(dns_name_countlabels(&zone->origin) != 0);
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson REQUIRE(zone->type != dns_zone_none);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews switch (zone->rdclass) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_master:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_slave:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_stub:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_hint:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(zone->database != NULL);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*FALLTHROUGH*/
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_forward:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(zone->rdclass != dns_rdataclass_none);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews break;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson case dns_zone_cache:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(zone->rdclass == dns_rdataclass_none);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(zone->database == NULL);
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater break;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(zone->db_type != NULL);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews}
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsisc_result_t
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsdns_zone_load(dns_zone_t *zone) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews const char me[] = "dns_zone_load";
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews int soacount = 0;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews int nscount = 0;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_result_t result;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_dbnode_t *node = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_dbversion_t *version = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_rdataset_t rdataset;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_boolean_t cache = ISC_FALSE;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_rdata_soa_t soa;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_rdata_t rdata;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_stdtime_t now;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_db_t *db = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews LOCK(&zone->lock);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_stdtime_get(&now);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews switch (zone->type) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_forward:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_none:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews result = DNS_R_SUCCESS;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews goto cleanup;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_master:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_slave:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_stub:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_hint:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews cache = ISC_FALSE;
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater break;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case dns_zone_cache:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews cache = ISC_TRUE;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews break;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews default:
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews INSIST("bad zone type" == NULL);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(zone->database != NULL);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews result = dns_db_create(zone->mctx, zone->db_type,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews &zone->origin,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews cache, zone->rdclass,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews zone->db_argc, zone->db_argv, &db);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (result != DNS_R_SUCCESS)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews goto cleanup;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt result = dns_db_load(db, zone->database);
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt /*
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * Initiate zone transfer? We may need a error code that
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * indicates that the "permanent" form does not exist.
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * XXX better error feedback to log.
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt */
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt if (result != DNS_R_SUCCESS) {
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt if (zone->type == dns_zone_slave) {
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt zone_log(zone, me, ISC_LOG_INFO,
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt "no database file");
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt result = ISC_R_SUCCESS;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt } else {
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt zone_log(zone, me, ISC_LOG_ERROR,
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt "database %s: dns_db_load failed: %s",
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews zone->database, dns_result_totext(result));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews goto cleanup;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Apply update log, if any.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (zone->journal != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews result = dns_journal_rollforward(zone->mctx, db, zone->journal);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (result != DNS_R_SUCCESS && result != DNS_R_NOTFOUND &&
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews result != DNS_R_UPTODATE && result != DNS_R_NOJOURNAL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews goto cleanup;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (result == DNS_R_NOTFOUND) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews zone_log(zone, me, ISC_LOG_ERROR,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews "journal out of sync with zone");
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson goto cleanup;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews zone_log(zone, me, ISC_LOG_DEBUG(1),
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews "dns_journal_rollforward: %s",
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_result_totext(result));
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (result == DNS_R_SUCCESS)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews zone->flags |= DNS_ZONE_F_NEEDDUMP;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews /*
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Obtain ns and soa counts for top of zone.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews */
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews nscount = 0;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews soacount = 0;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_db_currentversion(db, &version);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = dns_db_findnode(db, &zone->origin, ISC_FALSE, &node);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (result == DNS_R_SUCCESS) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdataset_init(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = dns_db_findrdataset(db, node, version,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_rdatatype_ns,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdatatype_none, 0, &rdataset,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews NULL);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (result == DNS_R_SUCCESS) {
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews result = dns_rdataset_first(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews while (result == DNS_R_SUCCESS) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews nscount++;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = dns_rdataset_next(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdataset_disassociate(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = dns_db_findrdataset(db, node, version,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdatatype_soa,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdatatype_none, 0, &rdataset,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews NULL);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (result == DNS_R_SUCCESS) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = dns_rdataset_first(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews while (result == DNS_R_SUCCESS) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdataset_current(&rdataset, &rdata);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (soacount == 0)
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater dns_rdata_tostruct(&rdata, &soa,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews zone->mctx);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews soacount++;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = dns_rdataset_next(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdataset_disassociate(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_rdataset_invalidate(&rdataset);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_db_detachnode(db, &node);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews dns_db_closeversion(db, &version, ISC_FALSE);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews /*
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Master / Slave / Stub zones require both NS and SOA records at
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * the top of the zone.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Hint zones only require NS records.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Cache zones have no reqirements.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews */
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews switch (zone->type) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews case dns_zone_master:
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews case dns_zone_slave:
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews case dns_zone_stub:
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (soacount != 1 || nscount == 0) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (soacount != 1)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone_log(zone, me, ISC_LOG_ERROR,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "has %d SOA record%s", soacount,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews (soacount != 0) ? "s" : "");
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (nscount == 0)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone_log(zone, me, ISC_LOG_ERROR,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "no NS records");
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = DNS_R_BADZONE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews goto cleanup;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (zone->top != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (!isc_serial_ge(soa.serial, zone->serial)) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone_log(zone, me, ISC_LOG_ERROR,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "zone serial has gone backwards");
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->serial = soa.serial;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->refresh = RANGE(soa.refresh, DNS_MIN_REFRESH,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews DNS_MAX_REFRESH);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->retry = RANGE(soa.retry, DNS_MIN_REFRESH,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews DNS_MAX_REFRESH);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->expire = RANGE(soa.expire, zone->refresh + zone->retry,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews DNS_MAX_EXPIRE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->minimum = soa.minimum;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (zone->type == dns_zone_slave ||
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->type == dns_zone_stub) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews /* XXX need database modification time */
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->expiretime = now /*XXX*/ + zone->expire;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews zone->refreshtime = now /*XXX*/;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews break;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence case dns_zone_hint:
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence if (nscount == 0) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence zone_log(zone, me, ISC_LOG_ERROR, "no NS records");
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = DNS_R_BADZONE;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence goto cleanup;
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt }
4716e94840921878b26e493576f84afe4fe08752Mark Andrews break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case dns_zone_cache:
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington break;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington default:
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington "unexpected zone type %d", zone->type);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington result = DNS_R_UNEXPECTED;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence goto cleanup;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson#if 0
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /* destroy notification example. */
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence isc_event_t *e = isc_event_allocate(zone->mctx, NULL,
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington DNS_EVENT_DBDESTROYED,
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence dns_zonemgr_dbdestroyed,
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt zone,
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington sizeof(isc_event_t));
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_db_ondestroy(db, zone->task, &e);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews#endif
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (zone->top != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews result = replacedb(zone, db, ISC_FALSE);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (result != ISC_R_SUCCESS)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson goto cleanup;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson } else {
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson dns_db_attach(db, &zone->top);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews zone->flags |= DNS_ZONE_F_LOADED;
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews }
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews result = ISC_R_SUCCESS;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer cleanup:
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence UNLOCK(&zone->lock);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (soacount != 0)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_rdata_freestruct(&soa);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (db != NULL)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_db_detach(&db);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews return (result);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews}
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#ifdef notyet
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsvoid
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsdns_zone_checkservers(dns_zone_t *zone) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_name_t *zonename;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews unsigned int i;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence dns_zone_checkservers_t *checkservers;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence dns_rdataset_t rdataset;
4144efb39046963989ad002cf88a0c195401100aJeremy Reed dns_rdata_t rdata;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence dns_dbnode_t *node = NULL;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence dns_dbversion_t *version = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_result_t result;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_rdata_ns_t ns;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
5096958739769958dd7a6b69356bf41260033873David Lawrence /* XXX MPA */
b39ad8a69bc2859b99c9f5a63d916789b566e470Andreas Gustafsson
5096958739769958dd7a6b69356bf41260033873David Lawrence /*
5096958739769958dd7a6b69356bf41260033873David Lawrence * get NS list from database, add in notify also list
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence zonename = &zone->origin;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_db_currentversion(zone->top, &version);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = dns_db_findnode(zone->top, zonename, ISC_FALSE, &node);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (result == DNS_R_SUCCESS) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews dns_rdataset_init(&rdataset);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence result = dns_db_findrdataset(zone->top, node, version,
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence dns_rdatatype_ns,
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_rdatatype_none, 0, &rdataset,
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence NULL);
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt if (result == DNS_R_SUCCESS) {
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt result = dns_rdataset_first(&rdataset);
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt while (result == DNS_R_SUCCESS) {
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt dns_rdataset_current(&rdataset, &rdata);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt result = dns_rdata_tostruct(&rdata, &ns, zone->mctx);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (result != DNS_R_SUCCESS)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt continue;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt checkservers = isc_mem_get(zone->mctx,
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence sizeof *checkservers);
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence if (checkservers == NULL)
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence break;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews dns_name_init(&checkservers->server, NULL);
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer dns_name_dup(&ns.name, zone->mctx,
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence &checkservers->server);
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence checkservers->name_known = ISC_TRUE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews checkservers->state = get_a; /* XXXMPA */
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence dns_zone_attach(zone, &checkservers->zone);
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence checkservers->mctx = zone->mctx;
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence dns_resolver_attach(zone->res, &checkservers->res);
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater checkservers->fetch = NULL;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ISC_LINK_INIT(checkservers, link);
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews checkservers->magic = CHECKSERVERS_MAGIC;
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews /* XXX lookup A/AAAA/A6 records */
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews result = dns_rdataset_next(&rdataset);
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews }
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt }
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews dns_rdataset_disassociate(&rdataset);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence dns_rdataset_invalidate(&rdataset);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_db_detachnode(zone->top, &node);
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews dns_db_closeversion(zone->top, &version, ISC_FALSE);
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Foreach NS in NS list perform a non-recursive query to obtain
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * NS list for zone (remove self from list).
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * callback to check:
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * If NXDOMAIN -> log error.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * If NODATA -> log error.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * If referral -> log error.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * If non-auth -> log error.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Compare NS list returned with server list if not identical
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * log error if current list is at least 3 x refresh old.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Compare glue A/AAAA/A6 records.
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt */
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Foreach NS in NS list perform a non-recursive query to obtain
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * SOA record for zone (remove self from list).
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt *
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * callback to check:
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * If NXDOMAIN -> log error.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * If NODATA -> log error.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * If referral -> log error.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * If no-auth -> log error.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * Compare SOA serial with ixfr list and if older that 3x refresh
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * log error.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington */
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater LOCK(&zone->lock);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington for (i = 0 ; i < zone->notifycnt; i++) {
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington checkservers = isc_mem_get(zone->mctx, sizeof *checkservers);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington if (checkservers == NULL)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence break;
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt dns_name_init(&checkservers->server, NULL);
d7ba3622ffa20c653ef6c8cfae42d8cd26465b7fBrian Wellington checkservers->name_known = ISC_FALSE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington checkservers->state = get_ns;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence checkservers->address = zone->notify[i];
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_zone_attach(zone, &checkservers->zone);
6dc32b43da2c7af5bf460a45a17d03226c162c91Michael Sawyer checkservers->mctx = zone->mctx;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt dns_resolver_attach(zone->res, &checkservers->res);
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt checkservers->fetch = NULL;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt ISC_LINK_INIT(checkservers, link);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson checkservers->magic = CHECKSERVERS_MAGIC;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson ISC_LIST_APPEND(zone->checkservers, checkservers, link);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_resolver_createfetch(zone->res, zonename, dns_rdatatype_ns,
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson NULL, NULL, NULL,
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson DNS_FETCHOPT_UNSHARED,
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson zone->task, checkservers_callback,
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson checkservers, &checkservers->fetch);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson }
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson UNLOCK(&zone->lock);
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt}
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson#endif
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson#ifdef notyet
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafssonstatic void
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafssoncheckservers_callback(isc_task_t *task, isc_event_t *event) {
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson const char me[] = "checkservers_callback";
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt dns_fetchdoneevent_t *devent = (dns_fetchdoneevent_t *)event;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_zone_checkservers_t *checkservers = event->arg;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_zone_state_t state;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_zone_t *zone;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_name_t *name;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson isc_mem_t *mctx;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson isc_sockaddr_t *address;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson dns_resolver_t *res;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence dns_message_t *msg;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(DNS_CHECKSERVERS_VALID(checkservers));
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington state = checkservers->state;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence zone = checkservers->zone;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence name = &checkservers->server;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews address = &checkservers->address;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews mctx = checkservers->mctx;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews res = checkservers->res;
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson task = task; /* unused */
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson if (devent->result != DNS_R_SUCCESS) {
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson /* timeout */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence switch (state) {
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington case get_a6:
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrews case get_aaaa:
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence case get_a:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington zone_log(zone, me, ISC_LOG_INFO,
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence "unable to obtain address for (%s)");
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington break;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington case get_ns:
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington case get_soa:
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence zone_log(zone, me, ISC_LOG_INFO,
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington "unable to obtain %s RRset from %s"
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington );
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington }
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt goto cleanup;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington }
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews msg = NULL;
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews dns_resolver_getanswer(event, &msg);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington switch (state) {
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt case get_a6:
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence add_address_tocheck(msg, checkservers, dns_rdatatype_a6);
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt dns_resolver_createfetch(res, name, dns_rdatatype_aaaa,
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence NULL, NULL, NULL, 0, zone->task,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington checkservers_callback,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington checkservers, &checkservers->fetch);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence checkservers->state = get_aaaa;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence break;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence 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;
isc_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 than 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
#ifdef notyet
static void
add_address_tocheck(dns_message_t *msg, dns_zone_checkservers_t *checkservers,
dns_rdatatype_t type)
{
dns_rdataset_t *rdataset = NULL;
isc_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);
}
}
#endif
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.
*/
}
static void
exit_check(dns_zone_t *zone)
{
if (zone->irefs == 0 && zone->shuttingdown == ISC_TRUE)
zone_free(zone);
}
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->erefs > 0);
source->erefs++;
INSIST(source->erefs != 0xffffffffU);
UNLOCK(&source->lock);
*target = source;
}
void
dns_zone_detach(dns_zone_t **zonep) {
dns_zone_t *zone;
isc_boolean_t free_now = ISC_FALSE;
REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
zone = *zonep;
LOCK(&zone->lock);
REQUIRE(zone->erefs > 0);
zone->erefs--;
if (zone->erefs == 0) {
if (zone->task != NULL) {
/*
* This zone is being managed. Post
* its control event and let it clean
* up synchronously in the context of
* its task.
*/
isc_event_t *ev = &zone->ctlevent;
isc_task_send(zone->task, &ev);
} else {
/*
* This zone is not being managed; it has
* no task and can have no outstanding
* events. Free it immediately.
*/
free_now = ISC_TRUE;
}
}
UNLOCK(&zone->lock);
if (free_now)
zone_free(zone);
*zonep = NULL;
}
void
dns_zone_iattach(dns_zone_t *source, dns_zone_t **target) {
REQUIRE(DNS_ZONE_VALID(source));
REQUIRE(target != NULL && *target == NULL);
source->irefs++;
INSIST(source->irefs != 0xffffffffU);
*target = source;
}
void
dns_zone_idetach(dns_zone_t **zonep) {
dns_zone_t *zone;
REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
zone = *zonep;
REQUIRE(zone->irefs > 0);
zone->irefs--;
*zonep = NULL;
exit_check(zone);
}
void
dns_zone_print(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
PRINT_ZONE_REF(zone);
}
isc_mem_t *
dns_zone_getmctx(dns_zone_t *zone) {
return zone->mctx;
}
static isc_result_t
dns_zone_tostr(dns_zone_t *zone, isc_mem_t *mctx, char **s) {
isc_buffer_t tbuf;
char outbuf[1024];
isc_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_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);
}
isc_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);
}
isc_result_t
dns_zone_setxfrsource4(dns_zone_t *zone, isc_sockaddr_t *xfrsource) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
zone->xfrsource4 = *xfrsource;
UNLOCK(&zone->lock);
return (DNS_R_SUCCESS);
}
isc_sockaddr_t *
dns_zone_getxfrsource4(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (&zone->xfrsource4);
}
isc_result_t
dns_zone_setxfrsource6(dns_zone_t *zone, isc_sockaddr_t *xfrsource) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
zone->xfrsource6 = *xfrsource;
UNLOCK(&zone->lock);
return (DNS_R_SUCCESS);
}
isc_sockaddr_t *
dns_zone_getxfrsource6(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (&zone->xfrsource6);
}
isc_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);
}
isc_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);
}
isc_result_t
dns_zone_getdb(dns_zone_t *zone, dns_db_t **dpb) {
isc_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;
isc_stdtime_get(&now);
/*
* 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);
}
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);
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);
isc_stdtime_get(&now);
/*
* 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
}
isc_result_t
dns_zone_dump(dns_zone_t *zone) {
isc_result_t result;
dns_dbversion_t *version = NULL;
dns_db_t *top = NULL;
char *buf;
int buflen;
FILE *f;
int n;
REQUIRE(DNS_ZONE_VALID(zone));
buflen = strlen(zone->database) + 20;
buf = isc_mem_get(zone->mctx, buflen);
result = isc_mktemplate(zone->database, buf, buflen);
if (result != ISC_R_SUCCESS)
return (result);
f = isc_ufile(buf);
if (f == NULL) {
result = DNS_R_UNEXPECTED;
goto cleanup;
}
dns_db_attach(zone->top, &top);
dns_db_currentversion(top, &version);
result = dns_master_dumptostream(zone->mctx, top, version,
&dns_master_style_default, f);
dns_db_closeversion(top, &version, ISC_FALSE);
dns_db_detach(&top);
n = fflush(f);
if (n != 0)
result = DNS_R_UNEXPECTED;
n = ferror(f);
if (n != 0)
result = DNS_R_UNEXPECTED;
n = fclose(f);
if (n != 0)
result = DNS_R_UNEXPECTED;
if (result == ISC_R_SUCCESS) {
n = rename(buf, zone->database);
if (n == -1) {
(void)remove(buf);
result = DNS_R_UNEXPECTED;
}
} else
(void)remove(buf);
cleanup:
isc_mem_put(zone->mctx, buf, buflen);
return (result);
}
isc_result_t
dns_zone_dumptostream(dns_zone_t *zone, FILE *fd) {
isc_result_t result;
dns_dbversion_t *version = NULL;
dns_db_t *top = NULL;
REQUIRE(DNS_ZONE_VALID(zone));
dns_db_attach(zone->top, &top);
dns_db_currentversion(top, &version);
result = dns_master_dumptostream(zone->mctx, top, version,
&dns_master_style_default, fd);
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*/
}
#ifdef notyet
/*
* For reference only. Use dns_zonemanager_managezone() instead.
*/
static isc_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 result;
/*
* XXXRTH Zones do not have resolvers!!!!
*/
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(zone->task == NULL);
result = isc_task_create(tmgr, zone->mctx, 0, &zone->task);
if (result != ISC_R_SUCCESS) {
/* XXX */
return (DNS_R_UNEXPECTED);
}
result = isc_task_onshutdown(zone->task, zone_shutdown, zone);
if (result != 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, 17, 19,
&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
}
#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;
isc_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++) {
/* XXX IPv6 */
(void)dns_notify(origin, &zone->notify[i], dns_rdatatype_soa,
zone->rdclass, &zone->xfrsource4, 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) {
/* XXX IPv6 */
(void)dns_notify(origin, &addr,
dns_rdatatype_soa,
zone->rdclass,
&zone->xfrsource4, 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
***/
#ifdef notyet
static void
refresh_callback(isc_task_t *task, isc_event_t *event) {
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;
isc_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;
isc_stdtime_get(&now);
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;
isc_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
/*
* Handle the control event. Note that although this event causes the zone
* to shut down, it is not a shutdown event in the sense of the task library.
*/
static void
zone_shutdown(isc_task_t *task, isc_event_t *event) {
dns_zone_t *zone = (dns_zone_t *) event->arg;
UNUSED(task);
REQUIRE(DNS_ZONE_VALID(zone));
INSIST(event->type == DNS_EVENT_ZONECONTROL);
INSIST(zone->erefs == 0);
zone_log(zone, "zone_shutdown", ISC_LOG_DEBUG(3), "shutting down");
zone->shuttingdown = ISC_TRUE;
if (zone->xfr != NULL)
dns_xfrin_shutdown(zone->xfr);
exit_check(zone);
}
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;
UNUSED(task);
DNS_ENTER;
dns_zonemgr_lockconf(zone->zmgr, isc_rwlocktype_read);
/* XXX if we use a view, we need to lock its configuration, too. */
dns_zone_maintenance(zone);
dns_zonemgr_unlockconf(zone->zmgr, isc_rwlocktype_read);
isc_event_free(&event);
}
static isc_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 result;
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;
}
if (next == 0) {
zone_log(zone, me, ISC_LOG_DEBUG(10),
"settimer inactive");
result = isc_timer_reset(zone->timer, isc_timertype_inactive,
NULL, NULL, ISC_TRUE);
} else {
if (next <= now)
next = now + 1;
zone_log(zone, me, ISC_LOG_DEBUG(10),
"settimer %d %d = %d seconds",
next, now, next - now);
isc_time_settoepoch(&expires);
isc_interval_set(&interval, next - now, 0);
result = isc_timer_reset(zone->timer, isc_timertype_once,
&expires, &interval, ISC_TRUE);
}
if (result != 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;
isc_stdtime_get(&now);
if (!DNS_ZONE_FLAG(zone, DNS_ZONE_F_EXITING))
zone_settimer(zone, now);
}
static isc_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;
isc_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);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_message_rendersection(msg, DNS_SECTION_ANSWER, 0);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_message_rendersection(msg, DNS_SECTION_AUTHORITY, 0);
if (result != DNS_R_SUCCESS)
goto cleanup;
result = dns_message_rendersection(msg, DNS_SECTION_ADDITIONAL, 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);
}
isc_result_t
dns_zone_notifyreceive(dns_zone_t *zone, isc_sockaddr_t *from,
dns_message_t *msg)
{
const char me[] = "dns_zone_notifyreceive";
unsigned int i;
dns_rdata_soa_t soa;
dns_rdataset_t *rdataset = NULL;
dns_rdata_t rdata;
isc_result_t result;
isc_stdtime_t now;
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);
if (msg->counts[DNS_SECTION_QUESTION] == 0) {
zone_log(zone, me, ISC_LOG_NOTICE,
"FORMERR no question");
return (DNS_R_FORMERR);
}
zone_log(zone, me, ISC_LOG_NOTICE,
"REFUSED zone does not match");
return (DNS_R_NOTIMP);
}
/*
* If we are a master zone just succeed.
*/
if (zone->type == dns_zone_master)
return (DNS_R_SUCCESS);
for (i = 0; i < zone->masterscnt; i++)
if (isc_sockaddr_eqaddr(from, &zone->masters[i]))
break;
if (i >= zone->masterscnt) {
UNLOCK(&zone->lock);
zone_log(zone, me, ISC_LOG_NOTICE,
"REFUSED notify from non master");
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)) {
zone_log(zone, me, ISC_LOG_DEBUG(3),
"zone up to date");
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);
zone_log(zone, me, ISC_LOG_DEBUG(3),
"refresh in progress, refresh check queued");
return (DNS_R_SUCCESS);
}
isc_stdtime_get(&now);
zone->refreshtime = now;
zone->notifyfrom = *from;
zone_settimer(zone, now);
UNLOCK(&zone->lock);
zone_log(zone, me, ISC_LOG_DEBUG(3), "immediate refresh check queued");
return (DNS_R_SUCCESS);
}
void
dns_zone_setqueryacl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->query_acl != NULL)
dns_acl_detach(&zone->query_acl);
dns_acl_attach(acl, &zone->query_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_setupdateacl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->update_acl != NULL)
dns_acl_detach(&zone->update_acl);
dns_acl_attach(acl, &zone->update_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_setxfracl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
if (zone->xfr_acl != NULL)
dns_acl_detach(&zone->xfr_acl);
dns_acl_attach(acl, &zone->xfr_acl);
UNLOCK(&zone->lock);
}
dns_acl_t *
dns_zone_getqueryacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->query_acl);
}
dns_acl_t *
dns_zone_getupdateacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->update_acl);
}
dns_acl_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_acl_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_acl_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_acl_detach(&zone->xfr_acl);
UNLOCK(&zone->lock);
}
void
dns_zone_setchecknames(dns_zone_t *zone, dns_severity_t severity) {
REQUIRE(DNS_ZONE_VALID(zone));
zone->check_names = severity;
}
dns_severity_t
dns_zone_getchecknames(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->check_names);
}
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;
isc_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);
}
#ifdef notyet
static int
message_count(dns_message_t *msg, dns_section_t section, dns_rdatatype_t type) {
isc_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);
}
#endif
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);
}
#ifdef notyet
static void
record_serial() {
}
#endif
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->xfrsource4, &newzone->xfrsource4))
goto false;
if (!isc_sockaddr_equal(&oldzone->xfrsource6, &newzone->xfrsource6))
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;
#define COMPARE_POINTERS(equalp, member) \
if ((oldzone->member == NULL && newzone->member != NULL) || \
(oldzone->member != NULL && newzone->member == NULL) || \
(oldzone->member != NULL && \
!(equalp)(oldzone->member, newzone->member))) \
goto false
COMPARE_POINTERS(dns_acl_equal, update_acl);
COMPARE_POINTERS(dns_acl_equal, query_acl);
COMPARE_POINTERS(dns_acl_equal, xfr_acl);
#undef COMPARE_POINTERS
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);
}
isc_result_t
dns_zone_replacedb(dns_zone_t *zone, dns_db_t *db, isc_boolean_t dump) {
isc_result_t result;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK(&zone->lock);
result = replacedb(zone, db, dump);
UNLOCK(&zone->lock);
return (result);
}
static isc_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, isc_result_t result) {
const char me[] = "xfrdone";
isc_stdtime_t now;
isc_boolean_t again = ISC_FALSE;
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:
isc_stdtime_get(&now);
if (DNS_ZONE_FLAG(zone, DNS_ZONE_F_NEEDREFRESH)) {
zone->flags &= ~DNS_ZONE_F_NEEDREFRESH;
zone->refreshtime = now;
} else
zone->refreshtime = now + zone->refresh;
zone_settimer(zone, now);
break;
default:
zone->curmaster++;
if (zone->curmaster >= zone->masterscnt)
zone->curmaster = 0;
else {
zone->flags |= DNS_ZONE_F_REFRESH;
again = ISC_TRUE;
}
break;
}
UNLOCK(&zone->lock);
/*
* If creating the transfer object failed, zone->xfr is NULL.
* Otherwise, we are called as the done callback of a zone
* transfer object that just entered its shutting-down
* state. Since we are no longer responsible for shutting
* it down, we can detach our reference.
*/
if (zone->xfr != NULL)
dns_xfrin_detach(&zone->xfr);
/*
* Retry with a different server if necessary.
*/
if (again)
xfrin_start_temporary_kludge(zone);
}
void
dns_zone_getssutable(dns_zone_t *zone, dns_ssutable_t **table) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(table != NULL);
REQUIRE(*table == NULL);
*table = zone->ssutable;
}
void
dns_zone_setssutable(dns_zone_t *zone, dns_ssutable_t *table) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(table != NULL);
zone->ssutable = table;
}
/***
*** Zone manager.
***/
static void
xfrin_start_temporary_kludge(dns_zone_t *zone) {
isc_result_t result;
isc_sockaddr_t master;
in_port_t port;
if (zone->masterscnt < 1)
return;
master = zone->masters[zone->curmaster];
port = zone->masterport;
if (port == 0)
port = 53; /* XXX is this the right place? */
isc_sockaddr_setport(&master, port);
result = dns_xfrin_create(zone, &master, zone->mctx,
zone->zmgr->timermgr, zone->zmgr->socketmgr,
zone->task,
xfrdone, &zone->xfr);
if (result != DNS_R_SUCCESS)
xfrdone(zone, result);
}
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->task = NULL;
ISC_LIST_INIT(zmgr->zones);
result = isc_rwlock_init(&zmgr->rwlock, 0, 0);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_rwlock_init() failed: %s",
isc_result_totext(result));
result = DNS_R_UNEXPECTED;
goto free_mem;
}
result = isc_rwlock_init(&zmgr->conflock, 1, 1);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_rwlock_init() failed: %s",
isc_result_totext(result));
result = DNS_R_UNEXPECTED;
goto free_rwlock;
}
result = isc_quota_init(&zmgr->transfersin, 10);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_quota_init() failed: %s",
isc_result_totext(result));
result = DNS_R_UNEXPECTED;
goto free_conflock;
}
/* Create the zone task pool. */
result = isc_taskpool_create(taskmgr, mctx,
8 /* XXX */, 0, &zmgr->zonetasks);
if (result != ISC_R_SUCCESS)
goto free_transfersin;
/* Create a single task for queueing of SOA queries. */
result = isc_task_create(taskmgr, mctx, 1, &zmgr->task);
if (result != ISC_R_SUCCESS)
goto free_taskpool;
isc_task_setname(zmgr->task, "zmgr", zmgr);
*zmgrp = zmgr;
return (ISC_R_SUCCESS);
free_taskpool:
isc_taskpool_destroy(&zmgr->zonetasks);
free_transfersin:
isc_quota_destroy(&zmgr->transfersin);
free_conflock:
isc_rwlock_destroy(&zmgr->conflock);
free_rwlock:
isc_rwlock_destroy(&zmgr->rwlock);
free_mem:
isc_mem_put(zmgr->mctx, zmgr, sizeof *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->task, zone_timer, zone,
&zone->timer);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
zone->zmgr = zmgr;
ISC_LIST_APPEND(zmgr->zones, zone, link);
goto unlock;
cleanup_task:
if (zone->task != NULL)
isc_task_detach(&zone->task);
unlock:
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_shutdown(dns_zonemgr_t *zmgr) {
if (zmgr->task != NULL)
isc_task_destroy(&zmgr->task);
if (zmgr->zonetasks != NULL)
isc_taskpool_destroy(&zmgr->zonetasks);
}
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);
/* Probably done already, but does not hurt to repeat. */
dns_zonemgr_shutdown(zmgr);
isc_rwlock_destroy(&zmgr->conflock);
isc_rwlock_destroy(&zmgr->rwlock);
isc_mem_put(zmgr->mctx, zmgr, sizeof *zmgr);
*zmgrp = NULL;
}
void
dns_zonemgr_lockconf(dns_zonemgr_t *zmgr, isc_rwlocktype_t type) {
RWLOCK(&zmgr->conflock, type);
}
void
dns_zonemgr_unlockconf(dns_zonemgr_t *zmgr, isc_rwlocktype_t type) {
RWUNLOCK(&zmgr->conflock, type);
}
void
dns_zonemgr_settransfersin(dns_zonemgr_t *zmgr, int value) {
zmgr->transfersin.max = value;
}
int
dns_zonemgr_getttransfersin(dns_zonemgr_t *zmgr) {
return (zmgr->transfersin.max);
}
#if 0
/* hook for ondestroy notifcation from a database. */
static void
dns_zonemgr_dbdestroyed(isc_task_t *task, isc_event_t *event) {
dns_db_t *db = event->sender;
UNUSED(task);
isc_event_free(&event);
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_ZONE, ISC_LOG_INFO,
"database (%p) destroyed", (void*) db);
}
#endif