zone.c revision 6c90ca893c7f0f3088999333eb9826f5afc46d81
9e6de65c57eddc3790badaad3b9481aaaed18e03Brian Wellington/*
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * Copyright (C) 1999-2001 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
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence * copyright notice and this permission notice appear in all copies.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence */
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
5497de6931b5ac26f65c2343b0318614f73933baMark Andrews/* $Id: zone.c,v 1.297 2001/01/17 00:16:20 gson Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
d09197467bbb156dccf0cbe72bb5c63480d5cfdcDavid Lawrence
d09197467bbb156dccf0cbe72bb5c63480d5cfdcDavid Lawrence#include <isc/file.h>
d09197467bbb156dccf0cbe72bb5c63480d5cfdcDavid Lawrence#include <isc/print.h>
64ba6e4cc3a0ccf8c8c6349fa75b937ca9bad9a6Michael Graff#include <isc/random.h>
64ba6e4cc3a0ccf8c8c6349fa75b937ca9bad9a6Michael Graff#include <isc/ratelimiter.h>
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#include <isc/serial.h>
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews#include <isc/string.h>
dd57718608494835363244429432599aa15124a3Andreas Gustafsson#include <isc/taskpool.h>
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#include <isc/timer.h>
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence#include <isc/util.h>
82a1986c04057804edf670bf5d59f716785af789Bob Halley
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#include <dns/acl.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/adb.h>
891a1bead8d02d29eb7b4993d7c0975047b0963dDavid Lawrence#include <dns/callbacks.h>
891a1bead8d02d29eb7b4993d7c0975047b0963dDavid Lawrence#include <dns/db.h>
891a1bead8d02d29eb7b4993d7c0975047b0963dDavid Lawrence#include <dns/events.h>
891a1bead8d02d29eb7b4993d7c0975047b0963dDavid Lawrence#include <dns/journal.h>
891a1bead8d02d29eb7b4993d7c0975047b0963dDavid Lawrence#include <dns/log.h>
891a1bead8d02d29eb7b4993d7c0975047b0963dDavid Lawrence#include <dns/master.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <dns/masterdump.h>
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence#include <dns/message.h>
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence#include <dns/name.h>
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#include <dns/peer.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <dns/rcode.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <dns/rdataclass.h>
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#include <dns/rdatalist.h>
c80dde0676a7f36f65e0ad8d646bf505705fe64bDavid Lawrence#include <dns/rdataset.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdatastruct.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/request.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/resolver.h>
c80dde0676a7f36f65e0ad8d646bf505705fe64bDavid Lawrence#include <dns/result.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <dns/stats.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <dns/ssu.h>
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence#include <dns/tsig.h>
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#include <dns/xfrin.h>
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence#include <dns/zone.h>
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence#define ZONE_MAGIC 0x5a4f4e45U /* ZONE */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define NOTIFY_MAGIC 0x4e746679U /* Ntfy */
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence#define STUB_MAGIC 0x53747562U /* Stub */
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence#define ZONEMGR_MAGIC 0x5a6d6772U /* Zmgr */
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#define LOAD_MAGIC ISC_MAGIC('L','o','a','d')
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define FORWARD_MAGIC ISC_MAGIC('F','o','r','w')
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define IO_MAGIC ISC_MAGIC('Z','m','I','O')
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONE_VALID(zone) \
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_MAGIC_VALID(zone, ZONE_MAGIC)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_NOTIFY_VALID(notify) \
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_MAGIC_VALID(notify, NOTIFY_MAGIC)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_STUB_VALID(stub) \
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence ISC_MAGIC_VALID(stub, STUB_MAGIC)
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence#define DNS_ZONEMGR_VALID(stub) \
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence ISC_MAGIC_VALID(stub, ZONEMGR_MAGIC)
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence#define DNS_LOAD_VALID(load) \
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence ISC_MAGIC_VALID(load, LOAD_MAGIC)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define DNS_FORWARD_VALID(load) \
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence ISC_MAGIC_VALID(load, FORWARD_MAGIC)
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#define DNS_IO_VALID(load) \
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_MAGIC_VALID(load, IO_MAGIC)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define RANGE(a, b, c) (((a) < (b)) ? (b) : ((a) < (c) ? (a) : (c)))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Default values.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_DEFAULT_IDLEIN 3600 /* 1 hour */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_DEFAULT_IDLEOUT 3600 /* 1 hour */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define MAX_XFER_TIME (2*3600) /* Documented default is 2 hours */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#ifndef DNS_MAX_EXPIRE
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_MAX_EXPIRE 14515200 /* 24 weeks */
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#ifndef DNS_DUMP_DELAY
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#define DNS_DUMP_DELAY 900 /* 15 minutes */
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#endif
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Grafftypedef struct dns_notify dns_notify_t;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Grafftypedef struct dns_stub dns_stub_t;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrencetypedef struct dns_load dns_load_t;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeintypedef struct dns_forward dns_forward_t;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrencetypedef struct dns_io dns_io_t;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrencetypedef ISC_LIST(dns_io_t) dns_iolist_t;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONE_CHECKLOCK
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#ifdef DNS_ZONE_CHECKLOCK
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define LOCK_ZONE(z) \
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff do { LOCK(&(z)->lock); \
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence INSIST((z)->locked == ISC_FALSE); \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein (z)->locked = ISC_TRUE; \
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence } while (0)
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#define UNLOCK_ZONE(z) \
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff do { (z)->locked = ISC_FALSE; UNLOCK(&(z)->lock); } while (0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define LOCKED_ZONE(z) ((z)->locked)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define LOCK_ZONE(z) LOCK(&(z)->lock)
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence#define UNLOCK_ZONE(z) UNLOCK(&(z)->lock)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define LOCKED_ZONE(z) ISC_TRUE
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence#endif
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrencestruct dns_zone {
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence /* Unlocked */
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence unsigned int magic;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence isc_mutex_t lock;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence#ifdef DNS_ZONE_CHECKLOCK
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_boolean_t locked;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mem_t *mctx;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley /* Locked */
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley dns_db_t *db;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley dns_zonemgr_t *zmgr;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley ISC_LINK(dns_zone_t) link; /* Used by zmgr. */
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley isc_timer_t *timer;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley unsigned int erefs;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int irefs;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_t origin;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff char *masterfile;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff char *journal;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_int32_t journalsize;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataclass_t rdclass;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley dns_zonetype_t type;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley unsigned int flags;
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews unsigned int options;
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews unsigned int db_argc;
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews char **db_argv;
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews isc_stdtime_t expiretime;
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews isc_stdtime_t refreshtime;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_stdtime_t dumptime;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_time_t loadtime;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_uint32_t serial;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_uint32_t refresh;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_uint32_t retry;
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence isc_uint32_t expire;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_uint32_t minimum;
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t maxrefresh;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_uint32_t minrefresh;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t maxretry;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t minretry;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_sockaddr_t *masters;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_t **masterkeynames;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence unsigned int masterscnt;
401fc772b1bf058a981e3c474fa6502f6ee0e2bfDavid Lawrence unsigned int curmaster;
401fc772b1bf058a981e3c474fa6502f6ee0e2bfDavid Lawrence unsigned int refreshcnt;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_sockaddr_t masteraddr;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence dns_notifytype_t notifytype;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence isc_sockaddr_t *notify;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence unsigned int notifycnt;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t notifyfrom;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence isc_task_t *task;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t notifysrc4;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t notifysrc6;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t xfrsource4;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t xfrsource6;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_xfrin_ctx_t *xfr;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Access Control Lists */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_acl_t *update_acl;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence dns_acl_t *forward_acl;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_acl_t *notify_acl;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence dns_acl_t *query_acl;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence dns_acl_t *xfr_acl;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence dns_severity_t check_names;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence ISC_LIST(dns_notify_t) notifies;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence dns_request_t *request;
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence dns_io_t *readio;
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence isc_uint32_t maxxfrin;
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence isc_uint32_t maxxfrout;
08e57545c2b1068080f5bf317224160426801406Brian Wellington isc_uint32_t idlein;
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews isc_uint32_t idleout;
40e7c805a8f38ad9b20dd6c688496fc09fc971c2Mark Andrews isc_boolean_t diff_on_reload;
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence isc_event_t ctlevent;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence dns_ssutable_t *ssutable;
08e57545c2b1068080f5bf317224160426801406Brian Wellington isc_uint32_t sigvalidityinterval;
40e7c805a8f38ad9b20dd6c688496fc09fc971c2Mark Andrews dns_view_t *view;
08e57545c2b1068080f5bf317224160426801406Brian Wellington /*
08e57545c2b1068080f5bf317224160426801406Brian Wellington * Zones in certain states such as "waiting for zone transfer"
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence * or "zone transfer in progress" are kept on per-state linked lists
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence * in the zone manager using the 'statelink' field. The 'statelist'
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence * field points at the list the zone is currently on. It the zone
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence * is not on any such list, statelist is NULL.
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence */
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence ISC_LINK(dns_zone_t) statelink;
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence dns_zonelist_t *statelist;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Optional per-zone statistics counters (NULL if not present).
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence */
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence isc_uint64_t *counters;
03052a178cb33b7d62ada0e34f1974e22bb98023David Lawrence};
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews#define DNS_ZONE_FLAG(z,f) (ISC_TF(((z)->flags & (f)) != 0))
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence#define DNS_ZONE_SETFLAG(z,f) do { \
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence INSIST(LOCKED_ZONE(z)); \
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews (z)->flags |= (f); \
40e7c805a8f38ad9b20dd6c688496fc09fc971c2Mark Andrews } while (0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONE_CLRFLAG(z,f) do { \
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews INSIST(LOCKED_ZONE(z)); \
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence (z)->flags &= ~(f); \
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence } while (0)
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence /* XXX MPA these may need to go back into zone.h */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff#define DNS_ZONEFLG_REFRESH 0x00000001U /* refresh check in progress */
037b732f88edc943f3141e0342dc993156e3abf3Mark Andrews#define DNS_ZONEFLG_NEEDDUMP 0x00000002U /* zone need consolidation */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_USEVC 0x00000004U /* use tcp for refresh query */
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley#define DNS_ZONEFLG_DUMPING 0x00000008U /* a dump is in progress */
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews#define DNS_ZONEFLG_HASINCLUDE 0x00000010U /* $INCLUDE in zone file */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_LOADED 0x00000020U /* database has loaded */
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews#define DNS_ZONEFLG_EXITING 0x00000040U /* zone is being destroyed */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_EXPIRED 0x00000080U /* zone has expired */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_NEEDREFRESH 0x00000100U /* refresh check needed */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_UPTODATE 0x00000200U /* zone contents are
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * uptodate */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_NEEDNOTIFY 0x00000400U /* need to send out notify
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * messages */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_DIFFONRELOAD 0x00000800U /* generate a journal diff on
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * reload */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_NOMASTERS 0x00001000U /* an attempt to refresh a
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * zone with no masters
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * occured */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_LOADING 0x00002000U /* load from disk in progress*/
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_HAVETIMERS 0x00004000U /* timer values have been set
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * from SOA (if not set, we
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * are still using
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * default timer values) */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_FORCEXFER 0x00008000U /* Force a zone xfer */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_NOREFRESH 0x00010000U
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_DIALNOTIFY 0x00020000U
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews#define DNS_ZONEFLG_DIALREFRESH 0x00040000U
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONEFLG_SHUTDOWN 0x00080000U
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ZONE_OPTION(z,o) (((z)->options & (o)) != 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstruct dns_zonemgr {
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews unsigned int magic;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_mem_t * mctx;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff int refs; /* Locked by rwlock */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_taskmgr_t * taskmgr;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_timermgr_t * timermgr;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_socketmgr_t * socketmgr;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_taskpool_t * zonetasks;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_task_t * task;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_ratelimiter_t * rl;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_rwlock_t rwlock;
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence isc_rwlock_t conflock;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_mutex_t iolock;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence /* Locked by rwlock. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zonelist_t zones;
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews dns_zonelist_t waiting_for_xfrin;
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews dns_zonelist_t xfrin_in_progress;
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews /* Locked by conflock. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff int transfersin;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence int transfersperns;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence /* Locked by iolock */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t iolimit;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_uint32_t ioactive;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_iolist_t high;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_iolist_t low;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff};
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
83e6eb0dfe7d3525dcf093f440e04e8971c4c5d2Brian Wellington * Hold notify state.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstruct dns_notify {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_int32_t magic;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence unsigned int flags;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_t *mctx;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_adbfind_t *find;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_request_t *request;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_t ns;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_sockaddr_t dst;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence unsigned int attempt;
83e6eb0dfe7d3525dcf093f440e04e8971c4c5d2Brian Wellington ISC_LINK(dns_notify_t) link;
83e6eb0dfe7d3525dcf093f440e04e8971c4c5d2Brian Wellington};
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_NOTIFY_NOSOA 0x0001U
83e6eb0dfe7d3525dcf093f440e04e8971c4c5d2Brian Wellington
83e6eb0dfe7d3525dcf093f440e04e8971c4c5d2Brian Wellington/*
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * dns_stub holds state while performing a 'stub' transfer.
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence * 'db' is the zone's 'db' or a new one if this is the initial
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * transfer.
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstruct dns_stub {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_int32_t magic;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_t *mctx;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_t *db;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_dbversion_t *version;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff};
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Hold load state.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstruct dns_load {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_int32_t magic;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_t *mctx;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_t *db;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_time_t loadtime;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdatacallbacks_t callbacks;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff};
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence * Hold forward state.
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstruct dns_forward {
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence isc_int32_t magic;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_t *mctx;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_buffer_t *msgbuf;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_request_t *request;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t which;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t addr;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_updatecallback_t callback;
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence void *callback_arg;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff};
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Hold IO request state.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstruct dns_io {
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence isc_int32_t magic;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zonemgr_t *zmgr;
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence isc_boolean_t high;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_task_t *task;
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence ISC_LINK(dns_io_t) link;
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence isc_event_t *event;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence};
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrencestatic void zone_settimer(dns_zone_t *, isc_stdtime_t);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrencestatic void cancel_refresh(dns_zone_t *);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrencestatic void zone_log(dns_zone_t *zone, const char *, int, const char *msg,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ...);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void notify_log(dns_zone_t *zone, int level, const char *fmt, ...);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void queue_xfrin(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_unload(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_expire(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_iattach(dns_zone_t *source, dns_zone_t **target);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_idetach(dns_zone_t **zonep);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t zone_replacedb(dns_zone_t *zone, dns_db_t *db,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_boolean_t dump);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t default_journal(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_xfrdone(dns_zone_t *zone, isc_result_t result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t zone_postload(dns_zone_t *zone, dns_db_t *db,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_time_t loadtime, isc_result_t result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_needdump(dns_zone_t *zone, unsigned int delay);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_shutdown(isc_task_t *, isc_event_t *);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_loaddone(void *arg, isc_result_t result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t zone_startload(dns_db_t *db, dns_zone_t *zone,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_time_t loadtime);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#if 0
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/* ondestroy example */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void dns_zonemgr_dbdestroyed(isc_task_t *task, isc_event_t *event);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#endif
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void refresh_callback(isc_task_t *, isc_event_t *);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void stub_callback(isc_task_t *, isc_event_t *);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void queue_soa_query(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void soa_query(isc_task_t *, isc_event_t *);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_stub_t *stub);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic int message_count(dns_message_t *msg, dns_section_t section,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdatatype_t type);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void notify_cancel(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void notify_find_address(dns_notify_t *notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void notify_send(dns_notify_t *notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t notify_createmessage(dns_zone_t *zone,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int flags,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_message_t **messagep);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#ifndef NOMINUM_PUBLIC
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_notifyforward(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#endif /* NOMINUM_PUBLIC */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void notify_done(isc_task_t *task, isc_event_t *event);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void notify_send_toaddr(isc_task_t *task, isc_event_t *event);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t zone_dump(dns_zone_t *);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void got_transfer_quota(isc_task_t *task, isc_event_t *event);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t zmgr_start_xfrin_ifquota(dns_zonemgr_t *zmgr,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zmgr_resume_xfrs(dns_zonemgr_t *zmgr);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zonemgr_free(dns_zonemgr_t *zmgr);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t zonemgr_getio(dns_zonemgr_t *zmgr, isc_boolean_t high,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_task_t *task, isc_taskaction_t action,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff void *arg, dns_io_t **iop);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zonemgr_putio(dns_io_t **iop);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zonemgr_cancelio(dns_io_t *io);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_get_from_db(dns_db_t *db, dns_name_t *origin, unsigned int *nscount,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int *soacount, isc_uint32_t *serial,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t *refresh, isc_uint32_t *retry,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t *expire, isc_uint32_t *minimum);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_freedbargs(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void forward_callback(isc_task_t *task, isc_event_t *event);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_saveunique(dns_zone_t *zone, const char *path,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff const char *templat);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_maintenance(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void zone_notify(dns_zone_t *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define ZONE_LOG(x,y) zone_log(zone, me, ISC_LOG_DEBUG(x), y)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_ENTER zone_log(zone, me, ISC_LOG_DEBUG(1), "enter")
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#define DNS_LEAVE zone_log(zone, me, ISC_LOG_DEBUG(1), "leave")
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffconst unsigned int dbargc_default = 1;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffconst char *dbargv_default[] = { "rbt" };
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/***
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *** Public functions.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ***/
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zonep != NULL && *zonep == NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(mctx != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone = isc_mem_get(mctx, sizeof *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = isc_mutex_init(&zone->lock);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(mctx, zone, sizeof *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "isc_mutex_init() failed: %s",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_totext(result));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_UNEXPECTED);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* XXX MPA check that all elements are initialised */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->mctx = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#ifdef DNS_ZONE_CHECKLOCK
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->locked = ISC_FALSE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#endif
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_attach(mctx, &zone->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->zmgr = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_LINK_INIT(zone, link);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->erefs = 1; /* Implicit attach. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->irefs = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_init(&zone->origin, NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterfile = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->journalsize = -1;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->journal = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->rdclass = dns_rdataclass_none;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->type = dns_zone_none;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->flags = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->options = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argc = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argv = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->expiretime = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshtime = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->dumptime = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_time_settoepoch(&zone->loadtime);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->serial = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refresh = DNS_ZONE_DEFAULTREFRESH;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->retry = DNS_ZONE_DEFAULTRETRY;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->expire = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->minimum = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->maxrefresh = DNS_ZONE_MAXREFRESH;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->minrefresh = DNS_ZONE_MINREFRESH;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->maxretry = DNS_ZONE_MAXRETRY;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->minretry = DNS_ZONE_MINRETRY;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masters = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterkeynames = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterscnt = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->curmaster = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshcnt = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notify = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notifytype = dns_notifytype_yes;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notifycnt = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->task = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->update_acl = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->forward_acl = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notify_acl = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->query_acl = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->xfr_acl = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->check_names = dns_severity_ignore;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->request = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->readio = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->timer = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->idlein = DNS_DEFAULT_IDLEIN;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->idleout = DNS_DEFAULT_IDLEOUT;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_LIST_INIT(zone->notifies);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_any(&zone->notifysrc4);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_any6(&zone->notifysrc6);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_any(&zone->xfrsource4);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_any6(&zone->xfrsource6);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->xfr = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->maxxfrin = MAX_XFER_TIME;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->maxxfrout = MAX_XFER_TIME;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->diff_on_reload = ISC_FALSE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->ssutable = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->sigvalidityinterval = 30 * 24 * 3600;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->view = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_LINK_INIT(zone, statelink);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->statelist = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->counters = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->magic = ZONE_MAGIC;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Must be after magic is set. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_zone_setdbtype(zone, dbargc_default, dbargv_default);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto free_mutex;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_EVENT_INIT(&zone->ctlevent, sizeof(zone->ctlevent), 0, NULL,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_EVENT_ZONECONTROL, zone_shutdown, zone, zone,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff NULL, NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *zonep = zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff free_mutex:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DESTROYLOCK(&zone->lock);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Free a zone. Because we require that there be no more
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * outstanding events or references, no locking is necessary.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_free(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_t *mctx = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zone->erefs == 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zone->irefs == 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(!LOCKED_ZONE(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Managed objects. Order is important.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->timer != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_timer_detach(&zone->timer);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->request != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_request_destroy(&zone->request); /* XXXMPA */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(zone->readio == NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(zone->statelist == NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->task != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_task_detach(&zone->task);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->zmgr)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zonemgr_releasezone(zone->zmgr, zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Unmanaged objects */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterfile != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_free(zone->mctx, zone->masterfile);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterfile = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->journalsize = -1;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->journal != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_free(zone->mctx, zone->journal);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->journal = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->counters != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_stats_freecounters(zone->mctx, &zone->counters);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->db != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_detach(&zone->db);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_freedbargs(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_setmasterswithkeys(zone, NULL, NULL, 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_setalsonotify(zone, NULL, 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->check_names = dns_severity_ignore;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->update_acl != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_acl_detach(&zone->update_acl);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->forward_acl != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_acl_detach(&zone->forward_acl);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->notify_acl != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_acl_detach(&zone->notify_acl);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->query_acl != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_acl_detach(&zone->query_acl);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->xfr_acl != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_acl_detach(&zone->xfr_acl);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (dns_name_dynamic(&zone->origin))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_free(&zone->origin, zone->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->ssutable != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_ssutable_detach(&zone->ssutable);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* last stuff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DESTROYLOCK(&zone->lock);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->magic = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff mctx = zone->mctx;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(mctx, zone, sizeof *zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_detach(&mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Single shot.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setclass(dns_zone_t *zone, dns_rdataclass_t rdclass) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(rdclass != dns_rdataclass_none);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Test and set.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zone->rdclass == dns_rdataclass_none ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->rdclass == rdclass);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->rdclass = rdclass;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrencedns_rdataclass_t
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrencedns_zone_getclass(dns_zone_t *zone){
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence return (zone->rdclass);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff}
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setnotifytype(dns_zone_t *zone, dns_notifytype_t notifytype) {
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence zone->notifytype = notifytype;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Single shot.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_settype(dns_zone_t *zone, dns_zonetype_t type) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(type != dns_zone_none);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Test and set.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zone->type == dns_zone_none || zone->type == type);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence zone->type = type;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence UNLOCK_ZONE(zone);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence}
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrencestatic void
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffzone_freedbargs(dns_zone_t *zone) {
194f54f4da604a6601edefa2175d48412e01bf2fDavid Lawrence unsigned int i;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Free the old database argument list. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->db_argv != NULL) {
587fc7b519b0fed4e3dd234527edba1ca9334757David Lawrence for (i = 0; i < zone->db_argc; i++)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_free(zone->mctx, zone->db_argv[i]);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, zone->db_argv,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argc * sizeof *zone->db_argv);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argc = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argv = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setdbtype(dns_zone_t *zone,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int dbargc, const char * const *dbargv) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff char **new = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int i;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(dbargc >= 1);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(dbargv != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Set up a new database argument list. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff new = isc_mem_get(zone->mctx, dbargc * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (new == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto nomem;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < dbargc; i++)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff new[i] = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < dbargc; i++) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff new[i] = isc_mem_strdup(zone->mctx, dbargv[i]);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (new[i] == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto nomem;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Free the old list. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_freedbargs(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argc = dbargc;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argv = new;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff nomem:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (new != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < dbargc; i++) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->db_argv[i] != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_free(zone->mctx, new[i]);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, new,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dbargc * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_NOMEMORY;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unlock:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setview(dns_zone_t *zone, dns_view_t *view) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->view != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_view_weakdetach(&zone->view);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_view_weakattach(view, &zone->view);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
08e57545c2b1068080f5bf317224160426801406Brian Wellingtondns_view_t *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getview(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (zone->view);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setorigin(dns_zone_t *zone, dns_name_t *origin) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(origin != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (dns_name_dynamic(&zone->origin)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_free(&zone->origin, zone->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_init(&zone->origin, NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_name_dup(origin, zone->mctx, &zone->origin);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setstring(dns_zone_t *zone, char **field, const char *value) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff char *copy;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (value != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff copy = isc_mem_strdup(zone->mctx, value);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (copy == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff copy = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (*field != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_free(zone->mctx, *field);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *field = copy;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setfile(dns_zone_t *zone, const char *file) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_zone_setstring(zone, &zone->masterfile, file);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = default_journal(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffconst char *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getfile(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (zone->masterfile);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdefault_journal(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff char *journal;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterfile != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Calculate string length including '\0'. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff int len = strlen(zone->masterfile) + sizeof ".jnl";
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff journal = isc_mem_allocate(zone->mctx, len);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (journal == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff strcpy(journal, zone->masterfile);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff strcat(journal, ".jnl");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff journal = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_zone_setstring(zone, &zone->journal, journal);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (journal != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_free(zone->mctx, journal);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setjournal(dns_zone_t *zone, const char *journal) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
08e57545c2b1068080f5bf317224160426801406Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_zone_setstring(zone, &zone->journal, journal);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffchar *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getjournal(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (zone->journal);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Return true iff the zone is "dynamic", in the sense that the zone's
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * master file (if any) is written by the server, rather than being
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * updated manually and read by the server.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * This is true for slave zones, stub zones, and zones that allow
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * dynamic updates either by having an update policy ("ssutable")
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * or an "allow-update" ACL with a value other than exactly "{ none; }".
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_boolean_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_isdynamic(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_TF(zone->type == dns_zone_slave ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->type == dns_zone_stub ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->ssutable != NULL ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff (zone->update_acl != NULL &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ! (zone->update_acl->length == 0 &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->update_acl->elements[0].negative == ISC_TRUE
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->update_acl->elements[0].type ==
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_aclelementtype_any))));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_load(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff const char me[] = "dns_zone_load";
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_t now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_time_t loadtime, filetime;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_t *db = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_get(&now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(zone->type != dns_zone_none);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->db != NULL && zone->masterfile == NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * The zone has no master file configured, but it already
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * has a database. It could be the built-in
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * version.bind. CH zone, a zone with a persistent
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * database being reloaded, or maybe a zone that
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * used to have a master file but whose configuration
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * was changed so that it no longer has one. Do nothing.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->db != NULL && zone_isdynamic(zone)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * This is a slave, stub, or dynamically updated
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * zone being reloaded. Do nothing - the database
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * we already have is guaranteed to be up-to-date.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_DEBUG(1), "start");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Don't do the load if the file that stores the zone is older
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * than the last time the zone was loaded. If the zone has not
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * been loaded yet, zone->loadtime will be the epoch.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterfile != NULL &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ! DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HASINCLUDE)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = isc_file_getmodtime(zone->masterfile, &filetime);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_SUCCESS &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff !isc_time_isepoch(&zone->loadtime) &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_time_compare(&filetime, &zone->loadtime) < 0) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_DEBUG(1),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "skipping: master file older"
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff " than last load");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Store the current time before the zone is loaded, so that if the
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * file changes between the time of the load and the time that
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * zone->loadtime is set, then the file will still be reloaded
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * the next time dns_zone_load is called.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = isc_time_now(&loadtime);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(zone->db_argc >= 1);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_db_create(zone->mctx, zone->db_argv[0],
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &zone->origin, (zone->type == dns_zone_stub) ?
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_dbtype_stub : dns_dbtype_zone,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->rdclass,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->db_argc - 1, zone->db_argv + 1,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &db);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "creating database: %s", isc_result_totext(result));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (! dns_db_ispersistent(db)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterfile != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_startload(db, zone, loadtime);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->type == dns_zone_master) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "no master file configured");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == DNS_R_CONTINUE) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADING);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_postload(zone, db, loadtime, result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff cleanup:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (db != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_detach(&db);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_gotreadhandle(isc_task_t *task, isc_event_t *event) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_load_t *load = event->ev_arg;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_LOAD_VALID(load));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_CANCELED;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_event_free(&event);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_CANCELED)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto fail;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_master_loadfileinc(load->zone->masterfile,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_origin(load->db),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_origin(load->db),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load->zone->rdclass, 0,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &load->callbacks, task,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_loaddone, load, load->zone->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS && result != DNS_R_CONTINUE &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result != DNS_R_SEENINCLUDE)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto fail;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff fail:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_loaddone(load, result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_startload(dns_db_t *db, dns_zone_t *zone, isc_time_t loadtime) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_load_t *load;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t tresult;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->zmgr != NULL && zone->db != NULL && zone->task != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load = isc_mem_get(zone->mctx, sizeof(*load));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (load == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load->mctx = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load->zone = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load->db = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load->loadtime = loadtime;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load->magic = LOAD_MAGIC;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_attach(zone->mctx, &load->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_iattach(zone, &load->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_attach(db, &load->db);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdatacallbacks_init(&load->callbacks);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_db_beginload(db, &load->callbacks.add,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &load->callbacks.add_private);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zonemgr_getio(zone->zmgr, ISC_TRUE, zone->task,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_gotreadhandle, load,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &zone->readio);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff tresult = dns_db_endload(load->db,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &load->callbacks.add_private);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = tresult;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = DNS_R_CONTINUE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_MANYERRORS)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdatacallbacks_t callbacks;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdatacallbacks_init(&callbacks);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_db_beginload(db, &callbacks.add,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &callbacks.add_private);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_master_loadfile(zone->masterfile, &zone->origin,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &zone->origin, zone->rdclass,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_MASTER_MANYERRORS,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &callbacks, zone->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff tresult = dns_db_endload(db, &callbacks.add_private);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = tresult;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_db_load(db, zone->masterfile);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff cleanup:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff load->magic = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_detach(&load->db);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_idetach(&load->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_detach(&load->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, load, sizeof(*load));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_postload(dns_zone_t *zone, dns_db_t *db, isc_time_t loadtime,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff const char me[] = "dns_zone_load";
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int soacount = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int nscount = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t serial, refresh, retry, expire, minimum;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_t now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_boolean_t needdump = ISC_FALSE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_get(&now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Initiate zone transfer? We may need a error code that
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * indicates that the "permanent" form does not exist.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * XXX better error feedback to log.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->type == dns_zone_slave ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->type == dns_zone_stub) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_FILENOTFOUND)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_DEBUG(1),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "no master file");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "loading master file %s: %s",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterfile,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_result_totext(result));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "loading master file %s: %s",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterfile, dns_result_totext(result));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_DEBUG(2),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "number of nodes in database: %u",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_nodecount(db));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->loadtime = loadtime;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_DEBUG(1), "loaded");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == DNS_R_SEENINCLUDE)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HASINCLUDE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HASINCLUDE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Apply update log, if any.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->journal != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_journal_rollforward(zone->mctx, db,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->journal);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result != DNS_R_UPTODATE && result != DNS_R_NOJOURNAL &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result != ISC_R_RANGE) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "dns_journal_rollforward returned: %s",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_result_totext(result));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_NOTFOUND || result == ISC_R_RANGE) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "journal out of sync with zone");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_DEBUG(1),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "dns_journal_rollforward: %s",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_result_totext(result));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff needdump = ISC_TRUE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Obtain ns and soa counts for top of zone.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff nscount = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff soacount = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(db != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_get_from_db(db, &zone->origin, &nscount,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &soacount, &serial, &refresh, &retry,
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence &expire, &minimum);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence if (result != ISC_R_SUCCESS) {
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence zone_log(zone, me, ISC_LOG_ERROR,
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence "could not find NS and/or SOA records");
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence }
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
0a09237aa0b12f9e478a9617d9d54c8b3512f94bBob Halley /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Master / Slave / Stub zones require both NS and SOA records at
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * the top of the zone.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff switch (zone->type) {
c4c843edb3f4c609e3552ee77a43400852400467David Lawrence case dns_zone_master:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_slave:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_stub:
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence if (soacount != 1) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, me, ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "has %d SOA records", soacount);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = DNS_R_BADZONE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (nscount == 0) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone_log(zone, me, ISC_LOG_ERROR, "no NS records");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = DNS_R_BADZONE;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence if (result != ISC_R_SUCCESS)
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence goto cleanup;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence if (zone->db != NULL) {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence if (!isc_serial_ge(serial, zone->serial)) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff zone_log(zone, me, ISC_LOG_ERROR,
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence "zone serial has gone backwards");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->serial = serial;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refresh = RANGE(refresh,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->minrefresh, zone->maxrefresh);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->retry = RANGE(retry,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->minretry, zone->maxretry);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->expire = RANGE(expire, zone->refresh + zone->retry,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_MAX_EXPIRE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->minimum = minimum;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->type == dns_zone_slave ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->type == dns_zone_stub) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_time_t t;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = isc_file_getmodtime(zone->journal, &t);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = isc_file_getmodtime(zone->masterfile,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &t);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->expiretime = isc_time_seconds(&t) +
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->expire;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->expiretime = now + zone->retry;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshtime = now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff default:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "unexpected zone type %d", zone->type);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_UNEXPECTED;
94231e22882a98a3805060b0a1322e96d8f57b41David Lawrence goto cleanup;
94231e22882a98a3805060b0a1322e96d8f57b41David Lawrence }
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence#if 0
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence /* destroy notification example. */
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence {
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence isc_event_t *e = isc_event_allocate(zone->mctx, NULL,
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence DNS_EVENT_DBDESTROYED,
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence dns_zonemgr_dbdestroyed,
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence zone,
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence sizeof(isc_event_t));
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence dns_db_ondestroy(db, zone->task, &e);
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence }
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence#endif
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence if (zone->db != NULL) {
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence result = zone_replacedb(zone, db, ISC_FALSE);
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence if (result != ISC_R_SUCCESS)
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence goto cleanup;
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence } else {
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence dns_db_attach(db, &zone->db);
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence DNS_ZONE_SETFLAG(zone,
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence DNS_ZONEFLG_LOADED|DNS_ZONEFLG_NEEDNOTIFY);
b79e66b06d058ef3b9a26b71806ce67971cd3152David Lawrence }
b79e66b06d058ef3b9a26b71806ce67971cd3152David Lawrence result = ISC_R_SUCCESS;
b79e66b06d058ef3b9a26b71806ce67971cd3152David Lawrence if (needdump)
b79e66b06d058ef3b9a26b71806ce67971cd3152David Lawrence zone_needdump(zone, DNS_DUMP_DELAY);
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence if (zone->task != NULL)
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence zone_settimer(zone, now);
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence return (result);
b79e66b06d058ef3b9a26b71806ce67971cd3152David Lawrence
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence cleanup:
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence if (zone->type == dns_zone_slave ||
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence zone->type == dns_zone_stub) {
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence if (zone->journal != NULL)
a571ebca8b8c472e775d518e82b7335f93ea25c4Andreas Gustafsson zone_saveunique(zone, zone->journal, "jn-XXXXXXXX");
a571ebca8b8c472e775d518e82b7335f93ea25c4Andreas Gustafsson if (zone->masterfile != NULL)
510f4bdcb66acbec3f276efaacecbebea891c868David Lawrence zone_saveunique(zone, zone->masterfile, "db-XXXXXXXX");
510f4bdcb66acbec3f276efaacecbebea891c868David Lawrence
510f4bdcb66acbec3f276efaacecbebea891c868David Lawrence /* Mark the zone for immediate refresh. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshtime = now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_boolean_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffexit_check(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN) &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->irefs == 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * DNS_ZONEFLG_SHUTDOWN can only be set if erefs == 0.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(zone->erefs == 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_TRUE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_FALSE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_count_ns_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int *nscount)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int count;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_t rdataset;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(nscount != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_init(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_db_findrdataset(db, node, version, dns_rdatatype_ns,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdatatype_none, 0, &rdataset, NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == ISC_R_NOTFOUND) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *nscount = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto invalidate_rdataset;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff else if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto invalidate_rdataset;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff count = 0;
c0bf2b179a199c50d2b50d43b18302ee5c3af6baAndreas Gustafsson result = dns_rdataset_first(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff while (result == ISC_R_SUCCESS) {
c0bf2b179a199c50d2b50d43b18302ee5c3af6baAndreas Gustafsson count++;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_rdataset_next(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_disassociate(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *nscount = count;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff invalidate_rdataset:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_invalidate(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_load_soa_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int *soacount,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t *serial, isc_uint32_t *refresh,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t *retry, isc_uint32_t *expire,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t *minimum)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int count;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_t rdataset;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdata_t rdata = DNS_RDATA_INIT;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdata_soa_t soa;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_init(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_db_findrdataset(db, node, version, dns_rdatatype_soa,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdatatype_none, 0, &rdataset, NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto invalidate_rdataset;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence count = 0;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence result = dns_rdataset_first(&rdataset);
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence while (result == ISC_R_SUCCESS) {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence dns_rdata_init(&rdata);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_current(&rdataset, &rdata);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff count++;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (count == 1)
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence dns_rdata_tostruct(&rdata, &soa, NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence result = dns_rdataset_next(&rdataset);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence dns_rdata_reset(&rdata);
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence }
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence dns_rdataset_disassociate(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (soacount != NULL)
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence *soacount = count;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (count > 0) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (serial != NULL)
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence *serial = soa.serial;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (refresh != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *refresh = soa.refresh;
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence if (retry != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *retry = soa.retry;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (expire != NULL)
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence *expire = soa.expire;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (minimum != NULL)
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence *minimum = soa.minimum;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_SUCCESS;
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff invalidate_rdataset:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdataset_invalidate(&rdataset);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence}
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence/*
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence * zone must be locked.
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_get_from_db(dns_db_t *db, dns_name_t *origin, unsigned int *nscount,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int *soacount, isc_uint32_t *serial,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t *refresh, isc_uint32_t *retry,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t *expire, isc_uint32_t *minimum)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_dbversion_t *version;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t answer = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_dbnode_t *node;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(db != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(origin != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff version = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_currentversion(db, &version);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence node = NULL;
a09c545af1ceb8eb6f3aa2bb6fae286208a72141David Lawrence result = dns_db_findnode(db, origin, ISC_FALSE, &node);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (result != ISC_R_SUCCESS) {
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence answer = result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto closeversion;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (nscount != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_count_ns_rr(db, node, version, nscount);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff answer = result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (soacount != NULL || serial != NULL || refresh != NULL
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff || retry != NULL || expire != NULL || minimum != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_load_soa_rr(db, node, version, soacount,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff serial, refresh, retry, expire,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff minimum);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff answer = result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_detachnode(db, &node);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff closeversion:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_closeversion(db, &version, ISC_FALSE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (answer);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
08e57545c2b1068080f5bf317224160426801406Brian Wellingtonvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_attach(dns_zone_t *source, dns_zone_t **target) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(source));
08e57545c2b1068080f5bf317224160426801406Brian Wellington REQUIRE(target != NULL && *target == NULL);
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence LOCK_ZONE(source);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(source->erefs > 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff source->erefs++;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(source->erefs != 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(source, "dns_zone_attach", ISC_LOG_DEBUG(10),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "eref = %d, irefs = %d", source->erefs, source->irefs);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(source);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *target = source;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_detach(dns_zone_t **zonep) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_boolean_t free_now = ISC_FALSE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone = *zonep;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zone->erefs > 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->erefs--;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->erefs == 0) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * We just detached the last external reference.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->task != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * This zone is being managed. Post
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence * its control event and let it clean
c0bf2b179a199c50d2b50d43b18302ee5c3af6baAndreas Gustafsson * up synchronously in the context of
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * its task.
c0bf2b179a199c50d2b50d43b18302ee5c3af6baAndreas Gustafsson */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_event_t *ev = &zone->ctlevent;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_task_send(zone->task, &ev);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff } else {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence /*
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence * This zone is not being managed; it has
08e57545c2b1068080f5bf317224160426801406Brian Wellington * no task and can have no outstanding
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence * events. Free it immediately.
40e7c805a8f38ad9b20dd6c688496fc09fc971c2Mark Andrews */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence * Unmanaged zones should not have non-null views;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * we have no way of detaching from the view here
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence * without causing deadlock because this code is called
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * with the view already locked.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence INSIST(zone->view == NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff free_now = ISC_TRUE;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence }
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence }
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence zone_log(zone, "dns_zone_detach", ISC_LOG_DEBUG(10),
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence "eref = %d, irefs = %d", zone->erefs, zone->irefs);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence *zonep = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (free_now)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_free(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_iattach(dns_zone_t *source, dns_zone_t **target) {
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence REQUIRE(DNS_ZONE_VALID(source));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(target != NULL && *target == NULL);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence LOCK_ZONE(source);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_iattach(source, target);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence UNLOCK_ZONE(source);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence}
7554feaef6057f5ea2926076900ac7634b911456Mark Andrews
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrencestatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_iattach(dns_zone_t *source, dns_zone_t **target) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * 'source' locked by caller.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(source));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(source));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(target != NULL && *target == NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff source->irefs++;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(source->irefs != 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *target = source;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(source, "zone_iattach", ISC_LOG_DEBUG(10),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "eref = %d, irefs = %d", source->erefs, source->irefs);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_idetach(dns_zone_t **zonep) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * 'zone' locked by caller.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone = *zonep;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(*zonep));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *zonep = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(zone->irefs > 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->irefs--;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, "zone_idetach", ISC_LOG_DEBUG(10),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "eref = %d, irefs = %d", zone->erefs, zone->irefs);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(zone->irefs + zone->erefs > 0);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence}
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence
7554feaef6057f5ea2926076900ac7634b911456Mark Andrewsvoid
40e7c805a8f38ad9b20dd6c688496fc09fc971c2Mark Andrewsdns_zone_idetach(dns_zone_t **zonep) {
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence dns_zone_t *zone;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_boolean_t free_needed;
c0bf2b179a199c50d2b50d43b18302ee5c3af6baAndreas Gustafsson
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone = *zonep;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence *zonep = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence LOCK_ZONE(zone);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence INSIST(zone->irefs > 0);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence zone->irefs--;
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence zone_log(zone, "dns_zone_idetach", ISC_LOG_DEBUG(10),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "eref = %d, irefs = %d", zone->erefs, zone->irefs);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff free_needed = exit_check(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (free_needed)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_free(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_mem_t *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getmctx(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (zone->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zonemgr_t *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getmgr(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (zone->zmgr);
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrence}
08e57545c2b1068080f5bf317224160426801406Brian Wellington
b65f2ab14abb4b6ef906d7d02064fba158f07b1eDavid Lawrencevoid
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrencedns_zone_setflag(dns_zone_t *zone, unsigned int flags, isc_boolean_t value) {
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (value)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, flags);
5d4f11b265c396d71ec2162a632e620425481a9eDavid Lawrence else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_CLRFLAG(zone, flags);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setoption(dns_zone_t *zone, unsigned int option, isc_boolean_t value)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
5d4f11b265c396d71ec2162a632e620425481a9eDavid Lawrence if (value)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->options |= option;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->options &= ~option;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
510f4bdcb66acbec3f276efaacecbebea891c868David Lawrence}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffunsigned int
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getoptions(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
5d4f11b265c396d71ec2162a632e620425481a9eDavid Lawrence return (zone->options);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrenceisc_result_t
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrencedns_zone_setxfrsource4(dns_zone_t *zone, isc_sockaddr_t *xfrsource) {
092b4e5359c5982a438e36ced3dbefc313f7fbfcDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->xfrsource4 = *xfrsource;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
5d4f11b265c396d71ec2162a632e620425481a9eDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_sockaddr_t *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getxfrsource4(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (&zone->xfrsource4);
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
5d4f11b265c396d71ec2162a632e620425481a9eDavid Lawrencedns_zone_setxfrsource6(dns_zone_t *zone, isc_sockaddr_t *xfrsource) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->xfrsource6 = *xfrsource;
510f4bdcb66acbec3f276efaacecbebea891c868David Lawrence UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_sockaddr_t *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getxfrsource6(dns_zone_t *zone) {
5d4f11b265c396d71ec2162a632e620425481a9eDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (&zone->xfrsource6);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence}
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
c4c843edb3f4c609e3552ee77a43400852400467David Lawrenceisc_result_t
c4c843edb3f4c609e3552ee77a43400852400467David Lawrencedns_zone_setnotifysrc4(dns_zone_t *zone, isc_sockaddr_t *notifysrc) {
c4c843edb3f4c609e3552ee77a43400852400467David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
c4c843edb3f4c609e3552ee77a43400852400467David Lawrence
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notifysrc4 = *notifysrc;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
005c7ad70f55a6e4333808cdf63811777254c339David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_sockaddr_t *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getnotifysrc4(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (&zone->notifysrc4);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setnotifysrc6(dns_zone_t *zone, isc_sockaddr_t *notifysrc) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notifysrc6 = *notifysrc;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_sockaddr_t *
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getnotifysrc6(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (&zone->notifysrc6);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setalsonotify(dns_zone_t *zone, isc_sockaddr_t *notify,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t count)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t *new;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE((notify == NULL && count == 0) ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff (notify != NULL && count != 0));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->notify != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, zone->notify,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notifycnt * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notify = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notifycnt = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff new = isc_mem_get(zone->mctx, count * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (new == NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff memcpy(new, notify, count * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notify = new;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->notifycnt = count;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unlock:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setmasters(dns_zone_t *zone, isc_sockaddr_t *masters,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_uint32_t count)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_zone_setmasterswithkeys(zone, masters, NULL, count);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setmasterswithkeys(dns_zone_t *zone, isc_sockaddr_t *masters,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_t **keynames, isc_uint32_t count)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff{
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t *new;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_t **newname;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int i;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(count == 0 || masters != NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (keynames != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(count != 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masters != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, zone->masters,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterscnt * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masters = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterkeynames != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < zone->masterscnt; i++) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterkeynames[i] != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_free(zone->masterkeynames[i],
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->mctx);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_mem_put(zone->mctx,
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence zone->masterkeynames[i],
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence sizeof(dns_name_t));
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence zone->masterkeynames[i] = NULL;
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence }
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence }
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence isc_mem_put(zone->mctx, zone->masterkeynames,
03052a178cb33b7d62ada0e34f1974e22bb98023David Lawrence zone->masterscnt * sizeof(dns_name_t *));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterkeynames = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterscnt = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * If count == 0, don't allocate any space for masters or keynames
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * so internally, those pointers are NULL if count == 0
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (count == 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * masters must countain count elements!
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff new = isc_mem_get(zone->mctx,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff count * sizeof(isc_sockaddr_t));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (new == NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_NOMEMORY;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff memcpy(new, masters, count * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masters = new;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterscnt = count;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOMASTERS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * if keynames is non-NULL, it must contain count elements!
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (keynames != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff newname = isc_mem_get(zone->mctx,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff count * sizeof(dns_name_t *));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (newname == NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_NOMEMORY;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, zone->masters,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff count * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < count; i++)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff newname[i] = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < count; i++) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (keynames[i] != NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff newname[i] = isc_mem_get(zone->mctx,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff sizeof(dns_name_t));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (newname[i] == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto allocfail;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_init(newname[i], NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_name_dup(keynames[i], zone->mctx,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff newname[i]);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff allocfail:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < count; i++)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (newname[i] != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_free(
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff newname[i],
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, zone->masters,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff count * sizeof *new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(zone->mctx, newname,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff count * sizeof *newname);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->masterkeynames = newname;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unlock:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_getdb(dns_zone_t *zone, dns_db_t **dpb) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->db == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = DNS_R_NOTLOADED;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff else
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_attach(zone->db, dpb);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Co-ordinates the starting of routine jobs.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_maintenance(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff const char me[] = "dns_zone_maintenance";
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_t now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ENTER;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_get(&now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_settimer(zone, now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_maintenance(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff const char me[] = "zone_maintenance";
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_t now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ENTER;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Configuring the view of this zone may have
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * failed, for example because the config file
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * had a syntax error. In that case, the view
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * adb or resolver, and we had better not try
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * to do maintenance on it.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->view == NULL || zone->view->adb == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_get(&now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Expire check.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff switch (zone->type) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_slave:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_stub:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (now >= zone->expiretime &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_expire(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshtime = now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff default:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Up to date check.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff switch (zone->type) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_slave:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_stub:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH) &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff now >= zone->refreshtime)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_refresh(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff default:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Do we need to consolidate the backing store?
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff switch (zone->type) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_master:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_slave:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterfile != NULL &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff now >= zone->dumptime &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_dump(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, "zone_dump", ISC_LOG_WARNING,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "failed: %s",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_result_totext(result));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff default:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Do we need to send out notify messages?
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff switch (zone->type) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_master:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case dns_zone_slave:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_notify(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff default:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_settimer(zone, now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_markdirty(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_needdump(zone, DNS_DUMP_DELAY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_expire(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_expire(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_expire(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * 'zone' locked by caller.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, "zone_expire", ISC_LOG_WARNING, "expired");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXPIRED);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refresh = DNS_ZONE_DEFAULTREFRESH;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->retry = DNS_ZONE_DEFAULTRETRY;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
72d4e9f06f548af14696a0771626d288ba41522fDavid Lawrence zone_unload(zone);
72d4e9f06f548af14696a0771626d288ba41522fDavid Lawrence}
72d4e9f06f548af14696a0771626d288ba41522fDavid Lawrence
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrencevoid
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrencedns_zone_refresh(dns_zone_t *zone) {
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence isc_stdtime_t now;
84f8cc7e22cccde283627f6df6b8b74ec473cdc9David Lawrence isc_uint32_t oldflags;
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence return;
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley isc_stdtime_get(&now);
72d4e9f06f548af14696a0771626d288ba41522fDavid Lawrence
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews /*
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence * Set DNS_ZONEFLG_REFRESH so that there is only one refresh operation
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * in progress at a time.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff oldflags = zone->flags;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterscnt == 0) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOMASTERS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if ((oldflags & DNS_ZONEFLG_NOMASTERS) == 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, "dns_zone_refresh", ISC_LOG_ERROR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff "no masters");
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if ((oldflags & (DNS_ZONEFLG_REFRESH|DNS_ZONEFLG_LOADING)) != 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto unlock;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Set the next refresh time as if refresh check has failed.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Setting this to the retry time will do that. XXXMLG
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * If we are successful it will be reset using zone->refresh.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshtime = now +
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_random_jitter(zone->retry, zone->retry / 4);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_log(zone, "dns_zone_refresh", ISC_LOG_DEBUG(20),
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews "refresh time (%u/%u), now %u",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshtime, zone->refresh, now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * When lacking user-specified timer values from the SOA,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * do exponential backoff of the retry time up to a
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews * maximum of six hours.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews if (! DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->retry = ISC_MIN(zone->retry * 2, 6 * 3600);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->curmaster = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->refreshcnt = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* initiate soa query */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff queue_soa_query(zone);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews unlock:
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews UNLOCK_ZONE(zone);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews}
40e7c805a8f38ad9b20dd6c688496fc09fc971c2Mark Andrews
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
40e7c805a8f38ad9b20dd6c688496fc09fc971c2Mark Andrewsdns_zone_flush(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result = ISC_R_SUCCESS;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_dump(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
72d4e9f06f548af14696a0771626d288ba41522fDavid Lawrence return (result);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_dump(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = zone_dump(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_needdump(dns_zone_t *zone, unsigned int delay) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_t now;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
83a56f1e4f2d11fa895ceff4342fff7157910036Mark Andrews * 'zone' locked by caller
c0bf2b179a199c50d2b50d43b18302ee5c3af6baAndreas Gustafsson */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
c0bf2b179a199c50d2b50d43b18302ee5c3af6baAndreas Gustafsson REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Do we have a place to dump to and are we loaded?
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->masterfile == NULL ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) == 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_get(&now);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* add some noise */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff delay = isc_random_jitter(delay, delay/4);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (zone->dumptime == 0 ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->dumptime > now + delay)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->dumptime = now + delay;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence zone_settimer(zone, now);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence}
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrencestatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_dump(dns_zone_t *zone) {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_dbversion_t *version = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence /*
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence * 'zone' locked by caller.
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence */
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_currentversion(zone->db, &version);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_master_dump(zone->mctx, zone->db, version,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &dns_master_style_default,
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence zone->masterfile);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_closeversion(zone->db, &version, ISC_FALSE);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->dumptime = 0;
9658892dbcf34d7afa8d1ca0400551a8b6ce8394David Lawrence if (result != ISC_R_SUCCESS) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence * Try again in a short while.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence */
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence zone_needdump(zone, DNS_DUMP_DELAY);
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffisc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_dumptostream(dns_zone_t *zone, FILE *fd) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_dbversion_t *version = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_t *db = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_attach(zone->db, &db);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_currentversion(db, &version);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_master_dumptostream(zone->mctx, db, version,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &dns_master_style_default, fd);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_closeversion(db, &version, ISC_FALSE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_db_detach(&db);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_unload(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_unload(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffnotify_cancel(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_notify_t *notify;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * 'zone' locked by caller.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (notify = ISC_LIST_HEAD(zone->notifies);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify != NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify = ISC_LIST_NEXT(notify, link)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify->find != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_adb_cancelfind(notify->find);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify->request != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_request_cancel(notify->request);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence}
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrencestatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_unload(dns_zone_t *zone) {
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence * 'zone' locked by caller.
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence */
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence REQUIRE(LOCKED_ZONE(zone));
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence dns_db_detach(&zone->db);
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADED);
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence}
5497de6931b5ac26f65c2343b0318614f73933baMark Andrews
5497de6931b5ac26f65c2343b0318614f73933baMark Andrewsvoid
5497de6931b5ac26f65c2343b0318614f73933baMark Andrewsdns_zone_setminrefreshtime(dns_zone_t *zone, isc_uint32_t val) {
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
5497de6931b5ac26f65c2343b0318614f73933baMark Andrews REQUIRE(val > 0);
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence
5497de6931b5ac26f65c2343b0318614f73933baMark Andrews zone->minrefresh = val;
5497de6931b5ac26f65c2343b0318614f73933baMark Andrews}
5497de6931b5ac26f65c2343b0318614f73933baMark Andrews
5497de6931b5ac26f65c2343b0318614f73933baMark Andrewsvoid
5497de6931b5ac26f65c2343b0318614f73933baMark Andrewsdns_zone_setmaxrefreshtime(dns_zone_t *zone, isc_uint32_t val) {
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
5497de6931b5ac26f65c2343b0318614f73933baMark Andrews REQUIRE(val > 0);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence zone->maxrefresh = val;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff}
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
c5944292e9ebee4a39fe939b9a16fe5596808556David Lawrencedns_zone_setminretrytime(dns_zone_t *zone, isc_uint32_t val) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(val > 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->minretry = val;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_setmaxretrytime(dns_zone_t *zone, isc_uint32_t val) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(val > 0);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone->maxretry = val;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_boolean_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffnotify_isqueued(dns_zone_t *zone, dns_name_t *name, isc_sockaddr_t *addr) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_notify_t *notify;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (notify = ISC_LIST_HEAD(zone->notifies);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify != NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify = ISC_LIST_NEXT(notify, link)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify->request != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff continue;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (name != NULL && dns_name_dynamic(&notify->ns) &&
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_equal(name, &notify->ns))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_TRUE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (addr != NULL && isc_sockaddr_equal(addr, &notify->dst))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_TRUE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_FALSE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffnotify_destroy(dns_notify_t *notify, isc_boolean_t locked) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_t *mctx;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence * Caller holds zone lock.
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff REQUIRE(DNS_NOTIFY_VALID(notify));
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify->zone != NULL) {
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence if (!locked)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(notify->zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (ISC_LINK_LINKED(notify, link))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_LIST_UNLINK(notify->zone->notifies, notify, link);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (!locked)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_zone_idetach(&notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify->find != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_adb_destroyfind(&notify->find);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify->request != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_request_destroy(&notify->request);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (dns_name_dynamic(&notify->ns))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_free(&notify->ns, notify->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff mctx = notify->mctx;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_put(notify->mctx, notify, sizeof *notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_detach(&mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffnotify_create(isc_mem_t *mctx, unsigned int flags, dns_notify_t **notifyp) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_notify_t *notify;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(notifyp != NULL && *notifyp == NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify = isc_mem_get(mctx, sizeof *notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->mctx = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_mem_attach(mctx, &notify->mctx);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->flags = flags;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->zone = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->find = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->request = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_any(&notify->dst);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_init(&notify->ns, NULL);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->attempt = 0;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_LINK_INIT(notify, link);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->magic = NOTIFY_MAGIC;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff *notifyp = notify;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_SUCCESS);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * XXXAG should check for DNS_ZONEFLG_EXITING
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffprocess_adb_event(isc_task_t *task, isc_event_t *ev) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_notify_t *notify;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_eventtype_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNUSED(task);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify = ev->ev_arg;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_NOTIFY_VALID(notify));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff INSIST(task == notify->zone->task);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ev->ev_type;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_event_free(&ev);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == DNS_EVENT_ADBMOREADDRESSES) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_adb_destroyfind(&notify->find);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_find_address(notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result == DNS_EVENT_ADBNOMOREADDRESSES) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_send(notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_destroy(notify, ISC_FALSE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffnotify_find_address(dns_notify_t *notify) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int options;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_NOTIFY_VALID(notify));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff options = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_INET |
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ADBFIND_INET6 | DNS_ADBFIND_RETURNLAME;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify->zone->view->adb == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto destroy;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_adb_createfind(notify->zone->view->adb,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->zone->task,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff process_adb_event, notify,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &notify->ns, dns_rootname,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff options, 0, NULL,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->zone->view->dstport,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &notify->find);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* Something failed? */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto destroy;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* More addresses pending? */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if ((notify->find->options & DNS_ADBFIND_WANTEVENT) != 0)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /* We have as many addresses as we can get. */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_send(notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff destroy:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_destroy(notify, ISC_FALSE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic isc_result_t
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrencenotify_send_queue(dns_notify_t *notify) {
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence isc_event_t *e;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff e = isc_event_allocate(notify->mctx, NULL,
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence DNS_EVENT_NOTIFYSENDTOADDR,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_send_toaddr,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify, sizeof(isc_event_t));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (e == NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (ISC_R_NOMEMORY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff e->ev_arg = notify;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff e->ev_sender = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = isc_ratelimiter_enqueue(notify->zone->zmgr->rl,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->zone->task, &e);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_event_free(&e);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffnotify_send_toaddr(isc_task_t *task, isc_event_t *event) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_notify_t *notify;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_message_t *message = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_netaddr_t dstip;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_tsigkey_t *key = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff char addrbuf[ISC_SOCKADDR_FORMATSIZE];
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t src;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify = event->ev_arg;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_NOTIFY_VALID(notify));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNUSED(task);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_LOADED) == 0) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_CANCELED;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0 ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_EXITING) ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->zone->view->requestmgr == NULL ||
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->zone->db == NULL) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_CANCELED;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = notify_createmessage(notify->zone, notify->flags, &message);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_netaddr_fromsockaddr(&dstip, &notify->dst);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff (void)dns_view_getpeertsig(notify->zone->view, &dstip, &key);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_format(&notify->dst, addrbuf, sizeof(addrbuf));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_log(notify->zone, ISC_LOG_DEBUG(3), "sending notify to %s",
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff addrbuf);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff switch (isc_sockaddr_pf(&notify->dst)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case PF_INET:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff src = notify->zone->notifysrc4;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff case PF_INET6:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff src = notify->zone->notifysrc6;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff break;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff default:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = ISC_R_NOTIMPLEMENTED;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup_key;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = dns_request_createvia(notify->zone->view->requestmgr, message,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &src, &notify->dst, 0, key, 15,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->zone->task,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_done, notify,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &notify->request);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff cleanup_key:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (key != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_tsigkey_detach(&key);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_message_destroy(&message);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff cleanup:
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff UNLOCK_ZONE(notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_destroy(notify, ISC_FALSE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_event_free(&event);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffnotify_send(dns_notify_t *notify) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_adbaddrinfo_t *ai;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_sockaddr_t dst;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_notify_t *new = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Zone lock held by caller.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_NOTIFY_VALID(notify));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(LOCKED_ZONE(notify->zone));
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (ai = ISC_LIST_HEAD(notify->find->list);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ai != NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ai = ISC_LIST_NEXT(ai, publink)) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dst = ai->sockaddr;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (notify_isqueued(notify->zone, NULL, &dst))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff continue;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff new = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = notify_create(notify->mctx,
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff (notify->flags & DNS_NOTIFY_NOSOA),
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff &new);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff goto cleanup;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff zone_iattach(notify->zone, &new->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff ISC_LIST_APPEND(new->zone->notifies, new, link);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff new->dst = dst;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = notify_send_queue(new);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence if (result != ISC_R_SUCCESS)
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence goto cleanup;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff new = NULL;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence cleanup:
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence if (new != NULL)
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify_destroy(new, ISC_TRUE);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff#ifndef NOMINUM_PUBLIC
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrencestatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_notifyforward(dns_zone_t *zone) {
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff const char me[] = "zone_notifyforward";
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence dns_notify_t *notify = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff unsigned int i;
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence isc_sockaddr_t dst;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff /*
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence * Locked by caller.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff */
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff REQUIRE(LOCKED_ZONE(zone));
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ENTER;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence /*
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * Enqueue notify requests for 'also-notify' servers.
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff for (i = 0; i < zone->masterscnt; i++) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dst = zone->masters[i];
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence if (notify_isqueued(zone, NULL, &dst))
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff continue;
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence result = notify_create(zone->mctx, DNS_NOTIFY_NOSOA, &notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff }
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence zone_iattach(zone, &notify->zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff notify->dst = dst;
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence ISC_LIST_APPEND(zone->notifies, notify, link);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff result = notify_send_queue(notify);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff if (result != ISC_R_SUCCESS) {
1adfd0a9119434de35e1a6b6ddc65b28dd608308David Lawrence notify_destroy(notify, ISC_TRUE);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff return;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence }
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence notify = NULL;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence }
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence}
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence#endif /* NOMINUM_PUBLIC */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffvoid
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffdns_zone_notify(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff isc_stdtime_t now;
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff REQUIRE(DNS_ZONE_VALID(zone));
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff LOCK_ZONE(zone);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence isc_stdtime_get(&now);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence zone_settimer(zone, now);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence UNLOCK_ZONE(zone);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence}
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffstatic void
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graffzone_notify(dns_zone_t *zone) {
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_dbnode_t *node = NULL;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence dns_dbversion_t *version = NULL;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_name_t *origin = NULL;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence dns_name_t master;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff dns_rdata_ns_t ns;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence dns_rdata_soa_t soa;
dns_rdata_t rdata = DNS_RDATA_INIT;
dns_rdataset_t nsrdset;
dns_rdataset_t soardset;
isc_result_t result;
dns_notify_t *notify = NULL;
unsigned int i;
isc_sockaddr_t dst;
isc_boolean_t isqueued;
dns_notifytype_t notifytype;
unsigned int flags = 0;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
notifytype = zone->notifytype;
UNLOCK_ZONE(zone);
if (! DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
return;
if (notifytype == dns_notifytype_no)
return;
notify_log(zone, ISC_LOG_DEBUG(3), "queueing notifies");
origin = &zone->origin;
/*
* If the zone is dialup we are done as we don't want to send
* the current soa so as to force a refresh query.
*/
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALNOTIFY))
flags |= DNS_NOTIFY_NOSOA;
/*
* Enqueue notify requests for 'also-notify' servers.
*/
LOCK_ZONE(zone);
for (i = 0; i < zone->notifycnt; i++) {
dst = zone->notify[i];
if (notify_isqueued(zone, NULL, &dst))
continue;
result = notify_create(zone->mctx, flags, &notify);
if (result != ISC_R_SUCCESS) {
UNLOCK_ZONE(zone);
return;
}
zone_iattach(zone, &notify->zone);
notify->dst = dst;
ISC_LIST_APPEND(zone->notifies, notify, link);
result = notify_send_queue(notify);
if (result != ISC_R_SUCCESS) {
notify_destroy(notify, ISC_TRUE);
UNLOCK_ZONE(zone);
return;
}
notify = NULL;
}
UNLOCK_ZONE(zone);
if (notifytype == dns_notifytype_explicit)
return;
/*
* Process NS RRset to generate notifies.
*/
dns_db_currentversion(zone->db, &version);
result = dns_db_findnode(zone->db, origin, ISC_FALSE, &node);
if (result != ISC_R_SUCCESS)
goto cleanup1;
dns_rdataset_init(&soardset);
result = dns_db_findrdataset(zone->db, node, version,
dns_rdatatype_soa,
dns_rdatatype_none, 0, &soardset, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup2;
/*
* Find master server's name.
*/
dns_name_init(&master, NULL);
result = dns_rdataset_first(&soardset);
while (result == ISC_R_SUCCESS) {
dns_rdataset_current(&soardset, &rdata);
result = dns_rdata_tostruct(&rdata, &soa, NULL);
dns_rdata_reset(&rdata);
if (result != ISC_R_SUCCESS)
continue;
result = dns_name_dup(&soa.origin, zone->mctx, &master);
if (result != ISC_R_SUCCESS)
continue;
result = dns_rdataset_next(&soardset);
if (result != ISC_R_NOMORE)
break;
}
dns_rdataset_disassociate(&soardset);
if (result != ISC_R_NOMORE)
goto cleanup3;
dns_rdataset_init(&nsrdset);
result = dns_db_findrdataset(zone->db, node, version,
dns_rdatatype_ns,
dns_rdatatype_none, 0, &nsrdset, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup3;
result = dns_rdataset_first(&nsrdset);
while (result == ISC_R_SUCCESS) {
dns_rdataset_current(&nsrdset, &rdata);
result = dns_rdata_tostruct(&rdata, &ns, NULL);
dns_rdata_reset(&rdata);
if (result != ISC_R_SUCCESS)
continue;
/*
* don't notify the master server.
*/
if (dns_name_compare(&master, &ns.name) == 0) {
result = dns_rdataset_next(&nsrdset);
continue;
}
LOCK_ZONE(zone);
isqueued = notify_isqueued(zone, &ns.name, NULL);
UNLOCK_ZONE(zone);
if (isqueued) {
result = dns_rdataset_next(&nsrdset);
continue;
}
result = notify_create(zone->mctx, flags, &notify);
if (result != ISC_R_SUCCESS)
continue;
dns_zone_iattach(zone, &notify->zone);
result = dns_name_dup(&ns.name, zone->mctx, &notify->ns);
if (result != ISC_R_SUCCESS) {
LOCK_ZONE(zone);
notify_destroy(notify, ISC_TRUE);
UNLOCK_ZONE(zone);
continue;
}
LOCK_ZONE(zone);
ISC_LIST_APPEND(zone->notifies, notify, link);
UNLOCK_ZONE(zone);
notify_find_address(notify);
notify = NULL;
result = dns_rdataset_next(&nsrdset);
}
dns_rdataset_disassociate(&nsrdset);
cleanup3:
if (dns_name_dynamic(&master))
dns_name_free(&master, zone->mctx);
cleanup2:
dns_db_detachnode(zone->db, &node);
cleanup1:
dns_db_closeversion(zone->db, &version, ISC_FALSE);
}
/***
*** Private
***/
static inline isc_result_t
save_nsrrset(dns_message_t *message, dns_name_t *name,
dns_db_t *db, dns_dbversion_t *version)
{
dns_rdataset_t *nsrdataset = NULL;
dns_rdataset_t *rdataset = NULL;
dns_dbnode_t *node = NULL;
dns_rdata_ns_t ns;
isc_result_t result;
dns_rdata_t rdata = DNS_RDATA_INIT;
/*
* Extract NS RRset from message.
*/
result = dns_message_findname(message, DNS_SECTION_ANSWER, name,
dns_rdatatype_ns, dns_rdatatype_none,
NULL, &nsrdataset);
if (result != ISC_R_SUCCESS)
goto fail;
/*
* Add NS rdataset.
*/
result = dns_db_findnode(db, name, ISC_TRUE, &node);
if (result != ISC_R_SUCCESS)
goto fail;
result = dns_db_addrdataset(db, node, version, 0,
nsrdataset, 0, NULL);
dns_db_detachnode(db, &node);
if (result != ISC_R_SUCCESS)
goto fail;
/*
* Add glue rdatasets.
*/
for (result = dns_rdataset_first(nsrdataset);
result == ISC_R_SUCCESS;
result = dns_rdataset_next(nsrdataset)) {
dns_rdataset_current(nsrdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &ns, NULL);
dns_rdata_reset(&rdata);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
if (!dns_name_issubdomain(&ns.name, name)) {
result = dns_rdataset_next(nsrdataset);
continue;
}
rdataset = NULL;
result = dns_message_findname(message, DNS_SECTION_ADDITIONAL,
&ns.name, dns_rdatatype_a6,
dns_rdatatype_none, NULL,
&rdataset);
if (result == ISC_R_SUCCESS) {
result = dns_db_findnode(db, &ns.name,
ISC_TRUE, &node);
if (result != ISC_R_SUCCESS)
goto fail;
result = dns_db_addrdataset(db, node, version, 0,
rdataset, 0, NULL);
dns_db_detachnode(db, &node);
if (result != ISC_R_SUCCESS)
goto fail;
}
rdataset = NULL;
result = dns_message_findname(message, DNS_SECTION_ADDITIONAL,
&ns.name, dns_rdatatype_aaaa,
dns_rdatatype_none, NULL,
&rdataset);
if (result == ISC_R_SUCCESS) {
result = dns_db_findnode(db, &ns.name,
ISC_TRUE, &node);
if (result != ISC_R_SUCCESS)
goto fail;
result = dns_db_addrdataset(db, node, version, 0,
rdataset, 0, NULL);
dns_db_detachnode(db, &node);
if (result != ISC_R_SUCCESS)
goto fail;
}
rdataset = NULL;
result = dns_message_findname(message, DNS_SECTION_ADDITIONAL,
&ns.name, dns_rdatatype_a,
dns_rdatatype_none, NULL,
&rdataset);
if (result == ISC_R_SUCCESS) {
result = dns_db_findnode(db, &ns.name,
ISC_TRUE, &node);
if (result != ISC_R_SUCCESS)
goto fail;
result = dns_db_addrdataset(db, node, version, 0,
rdataset, 0, NULL);
dns_db_detachnode(db, &node);
if (result != ISC_R_SUCCESS)
goto fail;
}
}
if (result != ISC_R_NOMORE)
goto fail;
return (ISC_R_SUCCESS);
fail:
return (result);
}
static void
stub_callback(isc_task_t *task, isc_event_t *event) {
const char me[] = "stub_callback";
dns_requestevent_t *revent = (dns_requestevent_t *)event;
dns_stub_t *stub = NULL;
dns_message_t *msg = NULL;
dns_zone_t *zone = NULL;
char master[ISC_SOCKADDR_FORMATSIZE];
isc_uint32_t nscnt, cnamecnt;
isc_result_t result;
isc_stdtime_t now;
isc_boolean_t exiting = ISC_FALSE;
stub = revent->ev_arg;
INSIST(DNS_STUB_VALID(stub));
UNUSED(task);
zone = stub->zone;
DNS_ENTER;
isc_stdtime_get(&now);
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
zone_log(zone, me, ISC_LOG_DEBUG(1), "exiting");
exiting = ISC_TRUE;
goto next_master;
}
isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
if (revent->result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO, "failure for %s: %s",
master, dns_result_totext(revent->result));
goto next_master;
}
result = dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
if (result != ISC_R_SUCCESS)
goto next_master;
result = dns_request_getresponse(revent->request, msg, 0);
if (result != ISC_R_SUCCESS)
goto next_master;
/*
* Unexpected rcode.
*/
if (msg->rcode != dns_rcode_noerror) {
char rcode[128];
isc_buffer_t rb;
isc_buffer_init(&rb, rcode, sizeof(rcode));
dns_rcode_totext(msg->rcode, &rb);
zone_log(zone, me, ISC_LOG_INFO,
"unexpected rcode (%.*s) from %s",
rb.used, rcode, master);
goto next_master;
}
/*
* We need complete messages.
*/
if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
if (dns_request_usedtcp(revent->request)) {
zone_log(zone, me, ISC_LOG_INFO,
"truncated TCP response from %s", master);
goto next_master;
}
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEVC);
UNLOCK_ZONE(zone);
goto same_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;
}
/*
* Sanity checks.
*/
cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname);
nscnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_ns);
if (cnamecnt != 0) {
zone_log(zone, me, ISC_LOG_INFO,
"found unexpected CNAME %s", master);
goto next_master;
}
if (nscnt == 0) {
zone_log(zone, me, ISC_LOG_INFO,
"no NS records from %s", master);
goto next_master;
}
/*
* Save answer.
*/
result = save_nsrrset(msg, &zone->origin, stub->db, stub->version);
if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO,
"unable to save ns records from %s", master);
goto next_master;
}
/*
* Tidy up.
*/
dns_db_closeversion(stub->db, &stub->version, ISC_TRUE);
LOCK_ZONE(zone);
if (zone->db == NULL)
dns_db_attach(stub->db, &zone->db);
UNLOCK_ZONE(zone);
dns_db_detach(&stub->db);
if (zone->masterfile != NULL) {
dns_zone_dump(zone);
(void)isc_time_now(&zone->loadtime);
}
dns_message_destroy(&msg);
isc_event_free(&event);
LOCK_ZONE(zone);
dns_request_destroy(&zone->request);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
zone->refreshtime = now +
isc_random_jitter(zone->refresh, zone->refresh / 4);
zone->expiretime = now + zone->expire;
zone_log(zone, me, ISC_LOG_DEBUG(20),
"refresh time (%u/%u), now %u",
zone->refreshtime, zone->refresh, now);
zone_settimer(zone, now);
UNLOCK_ZONE(zone);
goto free_stub;
next_master:
if (stub->version != NULL)
dns_db_closeversion(stub->db, &stub->version, ISC_FALSE);
if (stub->db != NULL)
dns_db_detach(&stub->db);
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
LOCK_ZONE(zone);
dns_request_destroy(&zone->request);
zone->curmaster++;
zone->refreshcnt = 0;
if (exiting || zone->curmaster >= zone->masterscnt) {
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
zone_settimer(zone, now);
UNLOCK_ZONE(zone);
goto free_stub;
}
queue_soa_query(zone);
UNLOCK_ZONE(zone);
goto free_stub;
same_master:
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
LOCK_ZONE(zone);
dns_request_destroy(&zone->request);
UNLOCK_ZONE(zone);
ns_query(zone, NULL, stub);
goto done;
free_stub:
stub->magic = 0;
dns_zone_idetach(&stub->zone);
INSIST(stub->db == NULL);
INSIST(stub->version == NULL);
isc_mem_put(stub->mctx, stub, sizeof(*stub));
done:
INSIST(event == NULL);
return;
}
/*
* An SOA query has finished (successfully or not).
*/
static void
refresh_callback(isc_task_t *task, isc_event_t *event) {
const char me[] = "refresh_callback";
dns_requestevent_t *revent = (dns_requestevent_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_SOCKADDR_FORMATSIZE];
dns_rdataset_t *rdataset;
dns_rdata_t rdata = DNS_RDATA_INIT;
dns_rdata_soa_t soa;
isc_result_t result;
isc_uint32_t serial;
zone = revent->ev_arg;
INSIST(DNS_ZONE_VALID(zone));
UNUSED(task);
DNS_ENTER;
/*
* if timeout log and next master;
*/
isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
isc_stdtime_get(&now);
if (revent->result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO, "failure for %s: %s",
master, dns_result_totext(revent->result));
if (revent->result == ISC_R_TIMEDOUT &&
!dns_request_usedtcp(revent->request)) {
if (zone->refreshcnt < 3)
goto same_master;
zone_log(zone, me, ISC_LOG_INFO, "%s: retries exceeded",
master);
}
goto next_master;
}
result = dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
if (result != ISC_R_SUCCESS)
goto next_master;
result = dns_request_getresponse(revent->request, msg, 0);
if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO,
"failure in request to %s: %s",
master, dns_result_totext(result));
goto next_master;
}
/*
* Unexpected rcode.
*/
if (msg->rcode != dns_rcode_noerror) {
char rcode[128];
isc_buffer_t rb;
isc_buffer_init(&rb, rcode, sizeof(rcode));
dns_rcode_totext(msg->rcode, &rb);
zone_log(zone, me, ISC_LOG_INFO,
"unexpected rcode (%.*s) from %s",
rb.used, rcode, master);
goto next_master;
}
/*
* If truncated punt to zone transfer which will query again.
*/
if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
if (zone->type == dns_zone_slave) {
zone_log(zone, me, ISC_LOG_INFO,
"truncated UDP answer, initiating "
"TCP zone xfer %s",
master);
goto tcp_transfer;
} else {
INSIST(zone->type == dns_zone_stub);
if (dns_request_usedtcp(revent->request)) {
zone_log(zone, me, ISC_LOG_INFO,
"truncated TCP response from %s",
master);
goto next_master;
}
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEVC);
UNLOCK_ZONE(zone);
goto same_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;
}
cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname);
soacnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_soa);
nscount = message_count(msg, DNS_SECTION_AUTHORITY, dns_rdatatype_ns);
soacount = message_count(msg, DNS_SECTION_AUTHORITY,
dns_rdatatype_soa);
/*
* There should not be a CNAME record at top of zone.
*/
if (cnamecnt != 0) {
zone_log(zone, me, ISC_LOG_INFO,
"CNAME at top of zone discovered: master %s", master);
goto next_master;
}
/*
* 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 || soacount != 0)) {
zone_log(zone, me, ISC_LOG_INFO,
"NODATA from master %s", master);
goto next_master;
}
/*
* Only one soa at top of zone.
*/
if (soacnt != 1) {
zone_log(zone, me, ISC_LOG_INFO,
"answer SOA count (%d) != 1: master %s",
soacnt, 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 != ISC_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 != ISC_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, NULL);
if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO, "dns_rdata_tostruct failed");
goto next_master;
}
serial = soa.serial;
zone_log(zone, me, ISC_LOG_DEBUG(1), "Serial: new %u, old %u",
serial, zone->serial);
if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) ||
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER) ||
isc_serial_gt(serial, zone->serial)) {
tcp_transfer:
isc_event_free(&event);
LOCK_ZONE(zone);
dns_request_destroy(&zone->request);
UNLOCK_ZONE(zone);
if (zone->type == dns_zone_slave) {
queue_xfrin(zone);
} else {
INSIST(zone->type == dns_zone_stub);
ns_query(zone, rdataset, NULL);
}
if (msg != NULL)
dns_message_destroy(&msg);
} else if (isc_serial_eq(soa.serial, zone->serial)) {
if (zone->masterfile != NULL) {
isc_time_t t;
isc_time_set(&t, now, 0);
result = isc_file_settime(zone->masterfile, &t);
if (result != ISC_R_SUCCESS)
zone_log(zone, me, ISC_LOG_ERROR,
"isc_file_settime(%s): %s",
zone->masterfile,
dns_result_totext(result));
}
zone->refreshtime = now +
isc_random_jitter(zone->refresh, zone->refresh / 4);
zone->expiretime = now + zone->expire;
zone_log(zone, me, ISC_LOG_DEBUG(20),
"refresh time (%u/%u), now %u",
zone->refreshtime, zone->refresh, now);
goto next_master;
} else {
ZONE_LOG(1, "ahead");
goto next_master;
}
if (msg != NULL)
dns_message_destroy(&msg);
goto detach;
next_master:
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
LOCK_ZONE(zone);
dns_request_destroy(&zone->request);
zone->curmaster++;
zone->refreshcnt = 0;
if (zone->curmaster >= zone->masterscnt) {
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDREFRESH)) {
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDREFRESH);
zone->refreshtime = now;
}
zone_settimer(zone, now);
UNLOCK_ZONE(zone);
goto detach;
}
queue_soa_query(zone);
UNLOCK_ZONE(zone);
goto detach;
same_master:
zone->refreshcnt++;
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
LOCK_ZONE(zone);
dns_request_destroy(&zone->request);
queue_soa_query(zone);
UNLOCK_ZONE(zone);
detach:
dns_zone_idetach(&zone);
return;
}
static void
queue_soa_query(dns_zone_t *zone) {
const char me[] = "queue_soa_query";
isc_event_t *e;
dns_zone_t *dummy = NULL;
isc_result_t result;
DNS_ENTER;
/*
* Locked by caller
*/
REQUIRE(LOCKED_ZONE(zone));
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
cancel_refresh(zone);
return;
}
e = isc_event_allocate(zone->mctx, NULL, DNS_EVENT_ZONE,
soa_query, zone, sizeof(isc_event_t));
if (e == NULL) {
cancel_refresh(zone);
return;
}
/*
* Attach so that we won't clean up
* until the event is delivered.
*/
zone_iattach(zone, &dummy);
e->ev_arg = zone;
e->ev_sender = NULL;
result = isc_ratelimiter_enqueue(zone->zmgr->rl, zone->task, &e);
if (result != ISC_R_SUCCESS) {
zone_idetach(&dummy);
isc_event_free(&e);
cancel_refresh(zone);
}
}
static inline isc_result_t
create_query(dns_zone_t *zone, dns_rdatatype_t rdtype,
dns_message_t **messagep)
{
dns_message_t *message = NULL;
dns_name_t *qname = NULL;
dns_rdataset_t *qrdataset = NULL;
isc_result_t result;
result = dns_message_create(zone->mctx, DNS_MESSAGE_INTENTRENDER,
&message);
if (result != ISC_R_SUCCESS)
goto cleanup;
message->opcode = dns_opcode_query;
message->rdclass = zone->rdclass;
result = dns_message_gettempname(message, &qname);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_message_gettemprdataset(message, &qrdataset);
if (result != ISC_R_SUCCESS)
goto cleanup;
/*
* Make question.
*/
dns_name_init(qname, NULL);
dns_name_clone(&zone->origin, qname);
dns_rdataset_init(qrdataset);
dns_rdataset_makequestion(qrdataset, zone->rdclass, rdtype);
ISC_LIST_APPEND(qname->list, qrdataset, link);
dns_message_addname(message, qname, DNS_SECTION_QUESTION);
*messagep = message;
return (ISC_R_SUCCESS);
cleanup:
if (qname != NULL)
dns_message_puttempname(message, &qname);
if (qrdataset != NULL)
dns_message_puttemprdataset(message, &qrdataset);
if (message != NULL)
dns_message_destroy(&message);
return (result);
}
static void
soa_query(isc_task_t *task, isc_event_t *event) {
const char me[] = "soa_query";
isc_result_t result;
dns_message_t *message = NULL;
dns_zone_t *zone = event->ev_arg;
dns_zone_t *dummy = NULL;
isc_netaddr_t masterip;
dns_tsigkey_t *key = NULL;
isc_uint32_t options;
isc_sockaddr_t src;
isc_boolean_t cancel = ISC_TRUE;
REQUIRE(DNS_ZONE_VALID(zone));
UNUSED(task);
DNS_ENTER;
LOCK_ZONE(zone);
if (((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0) ||
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) ||
zone->view->requestmgr == NULL) {
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
cancel = ISC_FALSE;
goto cleanup;
}
/*
* XXX Optimisation: Create message when zone is setup and reuse.
*/
result = create_query(zone, dns_rdatatype_soa, &message);
if (result != ISC_R_SUCCESS)
goto cleanup;
INSIST(zone->masterscnt > 0);
INSIST(zone->curmaster < zone->masterscnt);
zone->masteraddr = zone->masters[zone->curmaster];
isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
(void)dns_view_getpeertsig(zone->view, &masterip, &key);
options = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEVC) ?
DNS_REQUESTOPT_TCP : 0;
switch (isc_sockaddr_pf(&zone->masteraddr)) {
case PF_INET:
src = zone->xfrsource4;
break;
case PF_INET6:
src = zone->xfrsource6;
break;
default:
result = ISC_R_NOTIMPLEMENTED;
goto cleanup;
}
zone_iattach(zone, &dummy);
result = dns_request_createvia(zone->view->requestmgr, message,
&src, &zone->masteraddr, options, key,
15 /* XXX */, zone->task,
refresh_callback, zone, &zone->request);
if (result != ISC_R_SUCCESS) {
zone_idetach(&dummy);
zone_log(zone, me, ISC_LOG_DEBUG(1),
"dns_request_createvia failed: %s",
dns_result_totext(result));
goto cleanup;
}
if (key != NULL)
dns_tsigkey_detach(&key);
cancel = ISC_FALSE;
cleanup:
if (message != NULL)
dns_message_destroy(&message);
if (cancel)
cancel_refresh(zone);
isc_event_free(&event);
UNLOCK_ZONE(zone);
dns_zone_idetach(&zone);
return;
}
static void
ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset, dns_stub_t *stub) {
const char me[] = "ns_query";
isc_result_t result;
dns_message_t *message = NULL;
isc_netaddr_t masterip;
dns_tsigkey_t *key = NULL;
dns_dbnode_t *node = NULL;
isc_sockaddr_t src;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE((soardataset != NULL && stub == NULL) ||
(soardataset == NULL && stub != NULL));
REQUIRE(stub == NULL || DNS_STUB_VALID(stub));
DNS_ENTER;
LOCK_ZONE(zone);
if (stub == NULL) {
stub = isc_mem_get(zone->mctx, sizeof *stub);
if (stub == NULL)
goto cleanup;
stub->magic = STUB_MAGIC;
stub->mctx = zone->mctx;
stub->zone = NULL;
stub->db = NULL;
stub->version = NULL;
/*
* Attach so that the zone won't disappear from under us.
*/
zone_iattach(zone, &stub->zone);
/*
* If a db exists we will update it, otherwise we create a
* new one and attach it to the zone once we have the NS
* RRset and glue.
*/
if (zone->db != NULL)
dns_db_attach(zone->db, &stub->db);
else {
INSIST(zone->db_argc >= 1);
result = dns_db_create(zone->mctx, zone->db_argv[0],
&zone->origin, dns_dbtype_stub,
zone->rdclass,
zone->db_argc - 1,
zone->db_argv + 1,
&stub->db);
if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO,
"dns_db_create failed: %s",
dns_result_totext(result));
goto cleanup;
}
}
dns_db_newversion(stub->db, &stub->version);
/*
* Update SOA record.
*/
result = dns_db_findnode(stub->db, &zone->origin, ISC_TRUE,
&node);
if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO,
"dns_db_findnode failed: %s",
dns_result_totext(result));
goto cleanup;
}
result = dns_db_addrdataset(stub->db, node, stub->version, 0,
soardataset, 0, NULL);
dns_db_detachnode(stub->db, &node);
if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO,
"dns_db_addrdataset failed: %s",
dns_result_totext(result));
goto cleanup;
}
}
/*
* XXX Optimisation: Create message when zone is setup and reuse.
*/
result = create_query(zone, dns_rdatatype_ns, &message);
INSIST(zone->masterscnt > 0);
INSIST(zone->curmaster < zone->masterscnt);
zone->masteraddr = zone->masters[zone->curmaster];
isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
(void)dns_view_getpeertsig(zone->view, &masterip, &key);
/*
* Always use TCP so that we shouldn't truncate in additional section.
*/
switch (isc_sockaddr_pf(&zone->masteraddr)) {
case PF_INET:
src = zone->xfrsource4;
break;
case PF_INET6:
src = zone->xfrsource6;
break;
default:
result = ISC_R_NOTIMPLEMENTED;
goto cleanup;
}
result = dns_request_createvia(zone->view->requestmgr, message,
&src, &zone->masteraddr,
DNS_REQUESTOPT_TCP, key, 15 /* XXX */,
zone->task, stub_callback, stub,
&zone->request);
if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_DEBUG(1),
"dns_request_createvia failed: %s",
dns_result_totext(result));
goto cleanup;
}
dns_message_destroy(&message);
goto unlock;
cleanup:
cancel_refresh(zone);
if (stub != NULL) {
stub->magic = 0;
if (stub->version != NULL)
dns_db_closeversion(stub->db, &stub->version,
ISC_FALSE);
if (stub->db != NULL)
dns_db_detach(&stub->db);
isc_mem_put(stub->mctx, stub, sizeof(*stub));
zone_idetach(&zone);
}
if (message != NULL)
dns_message_destroy(&message);
unlock:
UNLOCK_ZONE(zone);
return;
}
/*
* 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->ev_arg;
isc_result_t result;
isc_boolean_t free_needed;
UNUSED(task);
REQUIRE(DNS_ZONE_VALID(zone));
INSIST(event->ev_type == DNS_EVENT_ZONECONTROL);
INSIST(zone->erefs == 0);
zone_log(zone, "zone_shutdown", ISC_LOG_DEBUG(3), "shutting down");
/*
* Stop things being restarted after we cancel them below.
*/
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXITING);
UNLOCK_ZONE(zone);
/*
* If we were waiting for xfrin quota, step out of
* the queue.
* If there's no zone manager, we can't be waiting for the
* xfrin quota
*/
if (zone->zmgr != NULL) {
RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
if (zone->statelist == &zone->zmgr->waiting_for_xfrin) {
ISC_LIST_UNLINK(zone->zmgr->waiting_for_xfrin, zone,
statelink);
zone->statelist = NULL;
}
RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
}
LOCK_ZONE(zone);
if (zone->xfr != NULL)
dns_xfrin_shutdown(zone->xfr);
if (zone->request != NULL) {
dns_request_cancel(zone->request);
}
if (zone->readio != NULL)
zonemgr_cancelio(zone->readio);
notify_cancel(zone);
if (zone->timer != NULL) {
result = isc_timer_reset(zone->timer, isc_timertype_inactive,
NULL, NULL, ISC_TRUE);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
}
if (zone->view != NULL)
dns_view_weakdetach(&zone->view);
/*
* We have now canceled everything set the flag to allow exit_check()
* to succeed. We must not unlock between setting this flag and
* calling exit_check().
*/
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_SHUTDOWN);
free_needed = exit_check(zone);
UNLOCK_ZONE(zone);
if (free_needed)
zone_free(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->ev_arg;
UNUSED(task);
REQUIRE(DNS_ZONE_VALID(zone));
DNS_ENTER;
dns_zonemgr_lockconf(zone->zmgr, isc_rwlocktype_read);
/* XXX if we use a view, we need to lock its configuration, too. */
zone_maintenance(zone);
dns_zonemgr_unlockconf(zone->zmgr, isc_rwlocktype_read);
isc_event_free(&event);
}
static void
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));
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
return;
switch (zone->type) {
case dns_zone_master:
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY))
next = now;
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP)) {
INSIST(zone->dumptime != 0);
if (zone->dumptime < next || next == 0)
next = zone->dumptime;
}
break;
case dns_zone_slave:
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY))
next = now;
/*FALLTHROUGH*/
case dns_zone_stub:
if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH) &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOMASTERS) &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOREFRESH)) {
INSIST(zone->refreshtime != 0);
if (zone->refreshtime < next || next == 0)
next = zone->refreshtime;
}
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) {
INSIST(zone->expiretime != 0);
if (zone->expiretime < next || next == 0)
next = zone->expiretime;
}
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);
if (result != ISC_R_SUCCESS)
zone_log(zone, me, ISC_LOG_ERROR,
"isc_timer_reset failed: %s",
isc_result_totext(result));
} else {
if (next <= now) {
next = now;
isc_time_now(&expires);
isc_interval_set(&interval, 0, 0);
} else {
isc_time_settoepoch(&expires);
isc_interval_set(&interval, next - now, 0);
}
zone_log(zone, me, ISC_LOG_DEBUG(10),
"settimer %d %d = %d seconds",
next, now, next - now);
result = isc_timer_reset(zone->timer, isc_timertype_once,
&expires, &interval, ISC_TRUE);
if (result != ISC_R_SUCCESS)
zone_log(zone, me, ISC_LOG_ERROR,
"isc_timer_reset failed: %s",
isc_result_totext(result));
}
}
static void
cancel_refresh(dns_zone_t *zone) {
const char me[] = "cancel_refresh";
isc_stdtime_t now;
/*
* 'zone' locked by caller.
*/
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(LOCKED_ZONE(zone));
DNS_ENTER;
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
isc_stdtime_get(&now);
zone_settimer(zone, now);
}
static isc_result_t
notify_createmessage(dns_zone_t *zone, unsigned int flags,
dns_message_t **messagep)
{
dns_dbnode_t *node = NULL;
dns_dbversion_t *version = NULL;
dns_message_t *message = NULL;
dns_rdataset_t rdataset;
dns_rdata_t rdata = DNS_RDATA_INIT;
dns_name_t *tempname = NULL;
dns_rdata_t *temprdata = NULL;
dns_rdatalist_t *temprdatalist = NULL;
dns_rdataset_t *temprdataset = NULL;
isc_result_t result;
isc_region_t r;
isc_buffer_t *b = NULL;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(messagep != NULL && *messagep == NULL);
message = NULL;
result = dns_message_create(zone->mctx, DNS_MESSAGE_INTENTRENDER,
&message);
if (result != ISC_R_SUCCESS)
return (result);
message->opcode = dns_opcode_notify;
message->flags |= DNS_MESSAGEFLAG_AA;
message->rdclass = zone->rdclass;
result = dns_message_gettempname(message, &tempname);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_message_gettemprdataset(message, &temprdataset);
if (result != ISC_R_SUCCESS)
goto cleanup;
/*
* Make question.
*/
dns_name_init(tempname, NULL);
dns_name_clone(&zone->origin, tempname);
dns_rdataset_init(temprdataset);
dns_rdataset_makequestion(temprdataset, zone->rdclass,
dns_rdatatype_soa);
ISC_LIST_APPEND(tempname->list, temprdataset, link);
dns_message_addname(message, tempname, DNS_SECTION_QUESTION);
tempname = NULL;
temprdataset = NULL;
if ((flags & DNS_NOTIFY_NOSOA) != 0)
goto done;
result = dns_message_gettempname(message, &tempname);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
result = dns_message_gettemprdata(message, &temprdata);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
result = dns_message_gettemprdataset(message, &temprdataset);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
result = dns_message_gettemprdatalist(message, &temprdatalist);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
dns_name_init(tempname, NULL);
dns_name_clone(&zone->origin, tempname);
dns_db_currentversion(zone->db, &version);
result = dns_db_findnode(zone->db, tempname, ISC_FALSE, &node);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
dns_rdataset_init(&rdataset);
result = dns_db_findrdataset(zone->db, node, version,
dns_rdatatype_soa,
dns_rdatatype_none, 0, &rdataset,
NULL);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
result = dns_rdataset_first(&rdataset);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
dns_rdataset_current(&rdataset, &rdata);
dns_rdata_toregion(&rdata, &r);
result = isc_buffer_allocate(zone->mctx, &b, r.length);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
isc_buffer_putmem(b, r.base, r.length);
isc_buffer_usedregion(b, &r);
dns_rdata_init(temprdata);
dns_rdata_fromregion(temprdata, rdata.rdclass, rdata.type, &r);
dns_message_takebuffer(message, &b);
result = dns_rdataset_next(&rdataset);
dns_rdataset_disassociate(&rdataset);
if (result != ISC_R_NOMORE)
goto soa_cleanup;
temprdatalist->rdclass = rdata.rdclass;
temprdatalist->type = rdata.type;
temprdatalist->covers = 0;
temprdatalist->ttl = rdataset.ttl;
ISC_LIST_INIT(temprdatalist->rdata);
ISC_LIST_APPEND(temprdatalist->rdata, temprdata, link);
dns_rdataset_init(temprdataset);
result = dns_rdatalist_tordataset(temprdatalist, temprdataset);
if (result != ISC_R_SUCCESS)
goto soa_cleanup;
ISC_LIST_APPEND(tempname->list, temprdataset, link);
dns_message_addname(message, tempname, DNS_SECTION_ANSWER);
temprdatalist = NULL;
temprdataset = NULL;
temprdata = NULL;
tempname = NULL;
soa_cleanup:
if (node != NULL)
dns_db_detachnode(zone->db, &node);
if (version != NULL)
dns_db_closeversion(zone->db, &version, ISC_FALSE);
if (tempname != NULL)
dns_message_puttempname(message, &tempname);
if (temprdata != NULL)
dns_message_puttemprdata(message, &temprdata);
if (temprdataset != NULL)
dns_message_puttemprdataset(message, &temprdataset);
if (temprdatalist != NULL)
dns_message_puttemprdatalist(message, &temprdatalist);
done:
*messagep = message;
return (ISC_R_SUCCESS);
cleanup:
if (tempname != NULL)
dns_message_puttempname(message, &tempname);
if (temprdataset != NULL)
dns_message_puttemprdataset(message, &temprdataset);
if (message != NULL)
dns_message_destroy(&message);
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 = DNS_RDATA_INIT;
isc_result_t result;
isc_stdtime_t now;
char fromtext[ISC_SOCKADDR_FORMATSIZE];
int match = 0;
isc_netaddr_t netaddr;
#ifndef NOMINUM_PUBLIC
isc_boolean_t forward = ISC_FALSE;
#endif /* NOMINUM_PUBLIC */
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 ISC_R_SUCCESS.
*
* Otherwise initiate a refresh check using 'from' as the
* first address to check. Return ISC_R_SUCCESS.
*/
isc_sockaddr_format(from, fromtext, sizeof(fromtext));
/*
* We only handle NOTIFY (SOA) at the present.
*/
LOCK_ZONE(zone);
if (msg->counts[DNS_SECTION_QUESTION] == 0 ||
dns_message_findname(msg, DNS_SECTION_QUESTION, &zone->origin,
dns_rdatatype_soa, dns_rdatatype_none,
NULL, NULL) != ISC_R_SUCCESS) {
UNLOCK_ZONE(zone);
if (msg->counts[DNS_SECTION_QUESTION] == 0) {
zone_log(zone, me, ISC_LOG_NOTICE,
"FORMERR no question: %s", fromtext);
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) {
UNLOCK_ZONE(zone);
return (ISC_R_SUCCESS);
}
for (i = 0; i < zone->masterscnt; i++)
if (isc_sockaddr_eqaddr(from, &zone->masters[i]))
break;
/*
* Accept notify requests from non masters if they are on
* 'zone->notify_acl'. If DNS_ZONEOPT_NOTIFYFORWARD is set
* then forward the notify.
*/
isc_netaddr_fromsockaddr(&netaddr, from);
if (i >= zone->masterscnt && zone->notify_acl != NULL &&
dns_acl_match(&netaddr, NULL, zone->notify_acl, NULL, &match,
NULL) == ISC_R_SUCCESS && match > 0) {
#ifndef NOMINUM_PUBLIC
if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOTIFYFORWARD))
forward = ISC_TRUE;
#endif /* NOMINUM_PUBLIC */
/* Accept notify. */
} else if (i >= zone->masterscnt) {
UNLOCK_ZONE(zone);
zone_log(zone, me, ISC_LOG_DEBUG(3),
"REFUSED notify from non master: %s", fromtext);
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_ZONEFLG_LOADED) &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOREFRESH)) {
result = dns_message_findname(msg, DNS_SECTION_ANSWER,
&zone->origin,
dns_rdatatype_soa,
dns_rdatatype_none, NULL,
&rdataset);
if (result == ISC_R_SUCCESS)
result = dns_rdataset_first(rdataset);
if (result == ISC_R_SUCCESS) {
isc_uint32_t serial = 0;
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &soa, NULL);
if (result == ISC_R_SUCCESS) {
serial = soa.serial;
if (isc_serial_le(serial, zone->serial)) {
zone_log(zone, me, ISC_LOG_DEBUG(3),
"zone up to date: %s",
fromtext);
UNLOCK_ZONE(zone);
return (ISC_R_SUCCESS);
}
}
}
}
#ifndef NOMINUM_PUBLIC
if (forward) {
zone_notifyforward(zone);
UNLOCK_ZONE(zone);
return (ISC_R_SUCCESS);
}
#endif /* NOMINUM_PUBLIC */
/*
* 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_ZONEFLG_REFRESH)) {
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDREFRESH);
zone->notifyfrom = *from;
UNLOCK_ZONE(zone);
zone_log(zone, me, ISC_LOG_DEBUG(3),
"refresh in progress, refresh check queued: %s",
fromtext);
return (ISC_R_SUCCESS);
}
isc_stdtime_get(&now);
zone->notifyfrom = *from;
UNLOCK_ZONE(zone);
dns_zone_refresh(zone);
return (ISC_R_SUCCESS);
}
void
dns_zone_setnotifyacl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->notify_acl != NULL)
dns_acl_detach(&zone->notify_acl);
dns_acl_attach(acl, &zone->notify_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_setqueryacl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->query_acl != NULL)
dns_acl_detach(&zone->query_acl);
dns_acl_attach(acl, &zone->query_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_setupdateacl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->update_acl != NULL)
dns_acl_detach(&zone->update_acl);
dns_acl_attach(acl, &zone->update_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_setforwardacl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->forward_acl != NULL)
dns_acl_detach(&zone->forward_acl);
dns_acl_attach(acl, &zone->forward_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_setxfracl(dns_zone_t *zone, dns_acl_t *acl) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->xfr_acl != NULL)
dns_acl_detach(&zone->xfr_acl);
dns_acl_attach(acl, &zone->xfr_acl);
UNLOCK_ZONE(zone);
}
dns_acl_t *
dns_zone_getnotifyacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->notify_acl);
}
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_getforwardacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->forward_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(zone);
if (zone->update_acl != NULL)
dns_acl_detach(&zone->update_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_clearforwardacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->forward_acl != NULL)
dns_acl_detach(&zone->forward_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_clearnotifyacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->notify_acl != NULL)
dns_acl_detach(&zone->notify_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_clearqueryacl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->query_acl != NULL)
dns_acl_detach(&zone->query_acl);
UNLOCK_ZONE(zone);
}
void
dns_zone_clearxfracl(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->xfr_acl != NULL)
dns_acl_detach(&zone->xfr_acl);
UNLOCK_ZONE(zone);
}
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);
}
static void
zone_tostr(dns_zone_t *zone, char *buf, size_t length) {
isc_result_t result = ISC_R_FAILURE;
isc_buffer_t buffer;
REQUIRE(buf != NULL);
REQUIRE(length > 1);
/*
* Leave space for terminating '\0'.
*/
isc_buffer_init(&buffer, buf, length - 1);
if (dns_name_dynamic(&zone->origin))
result = dns_name_totext(&zone->origin, ISC_TRUE, &buffer);
if (result != ISC_R_SUCCESS)
isc_buffer_putstr(&buffer, "<UNKNOWN>");
isc_buffer_putstr(&buffer, "/");
(void)dns_rdataclass_totext(zone->rdclass, &buffer);
buf[isc_buffer_usedlength(&buffer)] = '\0';
}
static void
notify_log(dns_zone_t *zone, int level, const char *fmt, ...) {
va_list ap;
char message[4096];
char namebuf[1024+32];
if (isc_log_wouldlog(dns_lctx, level) == ISC_FALSE)
return;
zone_tostr(zone, namebuf, sizeof(namebuf));
va_start(ap, fmt);
vsnprintf(message, sizeof message, fmt, ap);
va_end(ap);
isc_log_write(dns_lctx, DNS_LOGCATEGORY_NOTIFY, DNS_LOGMODULE_ZONE,
level, "zone %s: %s", namebuf, message);
}
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];
if (isc_log_wouldlog(dns_lctx, level) == ISC_FALSE)
return;
zone_tostr(zone, namebuf, sizeof(namebuf));
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, namebuf, message);
}
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 count = 0;
result = dns_message_firstname(msg, section);
while (result == ISC_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)
count++;
}
result = dns_message_nextname(msg, section);
}
return (count);
}
void
dns_zone_setmaxxfrin(dns_zone_t *zone, isc_uint32_t maxxfrin) {
REQUIRE(DNS_ZONE_VALID(zone));
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));
zone->maxxfrout = maxxfrout;
}
isc_uint32_t
dns_zone_getmaxxfrout(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->maxxfrout);
}
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(zone);
if (zone->task != NULL)
isc_task_detach(&zone->task);
isc_task_attach(task, &zone->task);
UNLOCK_ZONE(zone);
}
void
dns_zone_gettask(dns_zone_t *zone, isc_task_t **target) {
REQUIRE(DNS_ZONE_VALID(zone));
isc_task_attach(zone->task, target);
}
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));
zone->idleout = idleout;
}
isc_uint32_t
dns_zone_getidleout(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->idleout);
}
static void
notify_done(isc_task_t *task, isc_event_t *event) {
dns_requestevent_t *revent = (dns_requestevent_t *)event;
dns_notify_t *notify;
isc_result_t result;
dns_message_t *message = NULL;
isc_buffer_t buf;
char rcode[128];
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
UNUSED(task);
notify = event->ev_arg;
REQUIRE(DNS_NOTIFY_VALID(notify));
INSIST(task == notify->zone->task);
isc_buffer_init(&buf, rcode, sizeof(rcode));
isc_sockaddr_format(&notify->dst, addrbuf, sizeof(addrbuf));
result = revent->result;
if (result == ISC_R_SUCCESS)
result = dns_message_create(notify->zone->mctx,
DNS_MESSAGE_INTENTPARSE, &message);
if (result == ISC_R_SUCCESS)
result = dns_request_getresponse(revent->request, message,
DNS_MESSAGEPARSE_PRESERVEORDER);
if (result == ISC_R_SUCCESS)
result = dns_rcode_totext(message->rcode, &buf);
if (result == ISC_R_SUCCESS)
notify_log(notify->zone, ISC_LOG_DEBUG(3),
"notify response from %s: %.*s",
addrbuf, buf.used, rcode);
else
notify_log(notify->zone, ISC_LOG_DEBUG(1),
"notify to %s failed: %s", addrbuf,
dns_result_totext(result));
/*
* Old bind's return formerr if they see a soa record. Retry w/o
* the soa if we see a formerr and had sent a SOA.
*/
isc_event_free(&event);
if ((result == ISC_R_TIMEDOUT ||
(message != NULL && message->rcode == dns_rcode_formerr &&
(notify->flags & DNS_NOTIFY_NOSOA) == 0)) &&
notify->attempt < 3) {
notify->flags |= DNS_NOTIFY_NOSOA;
notify->attempt++;
dns_request_destroy(&notify->request);
notify_send_queue(notify);
} else {
if (result == ISC_R_TIMEDOUT)
notify_log(notify->zone, ISC_LOG_DEBUG(1),
"notify to %s: retries exceeded", addrbuf);
notify_destroy(notify, ISC_FALSE);
}
if (message != NULL)
dns_message_destroy(&message);
}
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(zone);
result = zone_replacedb(zone, db, dump);
UNLOCK_ZONE(zone);
return (result);
}
static isc_result_t
zone_replacedb(dns_zone_t *zone, dns_db_t *db, isc_boolean_t dump) {
dns_dbversion_t *ver;
isc_result_t result;
/*
* 'zone' locked by caller.
*/
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(LOCKED_ZONE(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->db != 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->db, NULL /* XXX */,
zone->journal);
if (result != ISC_R_SUCCESS)
goto fail;
} else {
if (dump && zone->masterfile != NULL) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
"dumping new zone version");
result = dns_db_dump(db, ver, zone->masterfile);
if (result != ISC_R_SUCCESS)
goto fail;
/*
* Update the time the zone was updated, so
* dns_zone_load can avoid loading it when
* the server is reloaded. If isc_time_now
* fails for some reason, all that happens is
* the timestamp is not updated.
*/
(void)isc_time_now(&zone->loadtime);
}
if (dump && zone->journal != NULL) {
/*
* The in-memory database just changed, and
* because 'dump' is set, it didn't change by
* being loaded from disk. Also, we have not
* journalled diffs for this change.
* Therefore, the on-disk journal is missing
* the deltas for this change. Since it can
* no longer be used to bring the zone
* up-to-date, it is useless and should be
* removed.
*/
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->db != NULL)
dns_db_detach(&zone->db);
dns_db_attach(db, &zone->db);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED|DNS_ZONEFLG_NEEDNOTIFY);
return (ISC_R_SUCCESS);
fail:
dns_db_closeversion(db, &ver, ISC_FALSE);
return (result);
}
static void
zone_xfrdone(dns_zone_t *zone, isc_result_t result) {
const char me[] = "zone_xfrdone";
isc_stdtime_t now;
isc_boolean_t again = ISC_FALSE;
unsigned int soacount;
unsigned int nscount;
isc_uint32_t serial, refresh, retry, expire, minimum;
REQUIRE(DNS_ZONE_VALID(zone));
zone_log(zone, me, ISC_LOG_DEBUG(1), "%s", dns_result_totext(result));
LOCK_ZONE(zone);
INSIST((zone->flags & DNS_ZONEFLG_REFRESH) != 0);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
isc_stdtime_get(&now);
switch (result) {
case ISC_R_SUCCESS:
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
/*FALLTHROUGH*/
case DNS_R_UPTODATE:
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FORCEXFER);
/*
* Has the zone expired underneath us?
*/
if (zone->db == NULL)
goto same_master;
/*
* This is not neccessary if we just performed a AXFR
* however it is necessary for an IXFR / UPTODATE and
* won't hurt with an AXFR.
*/
if (zone->masterfile != NULL || zone->journal != NULL) {
isc_time_t t;
isc_time_set(&t, now, 0);
result = ISC_R_FAILURE;
if (zone->journal != NULL)
result = isc_file_settime(zone->journal, &t);
if (result != ISC_R_SUCCESS &&
zone->masterfile != NULL)
result = isc_file_settime(zone->masterfile, &t);
if (result != ISC_R_SUCCESS)
zone_log(zone, me, ISC_LOG_ERROR,
"isc_file_settime(%s): %s",
zone->masterfile,
dns_result_totext(result));
}
/*
* Update the zone structure's data from the actual
* SOA received.
*/
nscount = 0;
soacount = 0;
INSIST(zone->db != NULL);
result = zone_get_from_db(zone->db, &zone->origin, &nscount,
&soacount, &serial, &refresh,
&retry, &expire, &minimum);
if (result == ISC_R_SUCCESS) {
if (soacount != 1)
zone_log(zone, me, ISC_LOG_ERROR,
"has %d SOA record%s", soacount,
(soacount != 0) ? "s" : "");
if (nscount == 0)
zone_log(zone, me, ISC_LOG_ERROR,
"no NS records");
zone->serial = serial;
zone->refresh = RANGE(refresh, zone->minrefresh,
zone->maxrefresh);
zone->retry = RANGE(retry, zone->minretry,
zone->maxretry);
zone->expire = RANGE(expire,
zone->refresh + zone->retry,
DNS_MAX_EXPIRE);
zone->minimum = minimum;
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
}
/*
* Set our next update/expire times.
*/
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDREFRESH)) {
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDREFRESH);
zone->refreshtime = now;
zone->expiretime = now + zone->expire;
} else {
zone->refreshtime = now +
isc_random_jitter(zone->refresh,
zone->refresh / 4);
zone_log(zone, me, ISC_LOG_DEBUG(20),
"refresh time (%u/%u), now %u",
zone->refreshtime, zone->refresh, now);
zone->expiretime = now + zone->expire;
}
break;
default:
zone->curmaster++;
same_master:
if (zone->curmaster >= zone->masterscnt)
zone->curmaster = 0;
else {
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH);
again = ISC_TRUE;
}
break;
}
zone_settimer(zone, now);
/*
* 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);
/*
* This transfer finishing freed up a transfer quota slot.
* Let any other zones waiting for quota have it.
*/
RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
ISC_LIST_UNLINK(zone->zmgr->xfrin_in_progress, zone, statelink);
zone->statelist = NULL;
if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
zmgr_resume_xfrs(zone->zmgr);
RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
/*
* Retry with a different server if necessary.
*/
if (again && !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
queue_soa_query(zone);
UNLOCK_ZONE(zone);
}
static void
zone_loaddone(void *arg, isc_result_t result) {
static char me[] = "zone_loaddone";
dns_load_t *load = arg;
dns_zone_t *zone;
isc_result_t tresult;
REQUIRE(DNS_LOAD_VALID(load));
zone = load->zone;
DNS_ENTER;
tresult = dns_db_endload(load->db, &load->callbacks.add_private);
if (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE)
result = tresult;
LOCK_ZONE(load->zone);
(void)zone_postload(load->zone, load->db, load->loadtime, result);
zonemgr_putio(&load->zone->readio);
DNS_ZONE_CLRFLAG(load->zone, DNS_ZONEFLG_LOADING);
UNLOCK_ZONE(load->zone);
load->magic = 0;
dns_db_detach(&load->db);
dns_zone_idetach(&load->zone);
isc_mem_putanddetach(&load->mctx, load, sizeof (*load));
}
void
dns_zone_getssutable(dns_zone_t *zone, dns_ssutable_t **table) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(table != NULL);
REQUIRE(*table == NULL);
LOCK_ZONE(zone);
if (zone->ssutable != NULL)
dns_ssutable_attach(zone->ssutable, table);
UNLOCK_ZONE(zone);
}
void
dns_zone_setssutable(dns_zone_t *zone, dns_ssutable_t *table) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->ssutable != NULL)
dns_ssutable_detach(&zone->ssutable);
if (table != NULL)
dns_ssutable_attach(table, &zone->ssutable);
UNLOCK_ZONE(zone);
}
void
dns_zone_setsigvalidityinterval(dns_zone_t *zone, isc_uint32_t interval) {
REQUIRE(DNS_ZONE_VALID(zone));
zone->sigvalidityinterval = interval;
}
isc_uint32_t
dns_zone_getsigvalidityinterval(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->sigvalidityinterval);
}
static void
queue_xfrin(dns_zone_t *zone) {
const char me[] = "queue_xfrin";
isc_result_t result;
dns_zonemgr_t *zmgr = zone->zmgr;
DNS_ENTER;
INSIST(zone->statelist == NULL);
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
ISC_LIST_APPEND(zmgr->waiting_for_xfrin, zone, statelink);
zone->statelist = &zmgr->waiting_for_xfrin;
result = zmgr_start_xfrin_ifquota(zmgr, zone);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
if (result == ISC_R_QUOTA) {
zone_log(zone, me, ISC_LOG_DEBUG(1),
"zone transfer deferred due to quota");
} else if (result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_ERROR,
"starting zone transfer: %s",
isc_result_totext(result));
}
}
/*
* This event callback is called when a zone has received
* any necessary zone transfer quota. This is the time
* to go ahead and start the transfer.
*/
static void
got_transfer_quota(isc_task_t *task, isc_event_t *event) {
const char me[] = "got_transfer_quota";
isc_result_t result;
dns_peer_t *peer = NULL;
dns_tsigkey_t *tsigkey = NULL;
char mastertext[256];
dns_rdatatype_t xfrtype;
dns_zone_t *zone = event->ev_arg;
isc_netaddr_t masterip;
UNUSED(task);
INSIST(task == zone->task);
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
result = ISC_R_CANCELED;
goto cleanup;
}
isc_sockaddr_format(&zone->masteraddr, mastertext, sizeof(mastertext));
isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
(void)dns_peerlist_peerbyaddr(zone->view->peers,
&masterip, &peer);
/*
* Decide whether we should request IXFR or AXFR.
*/
if (zone->db == NULL) {
zone_log(zone, me, ISC_LOG_DEBUG(3),
"no database exists yet, requesting AXFR of "
"initial version from %s", mastertext);
xfrtype = dns_rdatatype_axfr;
} else if (dns_zone_isforced(zone)) {
zone_log(zone, me, ISC_LOG_DEBUG(3),
"forced reload, requesting AXFR of "
"initial version from %s", mastertext);
xfrtype = dns_rdatatype_axfr;
} else {
isc_boolean_t use_ixfr = ISC_TRUE;
if (peer != NULL &&
dns_peer_getrequestixfr(peer, &use_ixfr) ==
ISC_R_SUCCESS) {
; /* Using peer setting */
} else {
use_ixfr = zone->view->requestixfr;
}
if (use_ixfr == ISC_FALSE) {
zone_log(zone, me, ISC_LOG_DEBUG(3),
"IXFR disabled, requesting AXFR from %s",
mastertext);
xfrtype = dns_rdatatype_axfr;
} else {
zone_log(zone, me, ISC_LOG_DEBUG(3),
"requesting IXFR from %s",
mastertext);
xfrtype = dns_rdatatype_ixfr;
}
}
/*
* Determine if we should attempt to sign the request with TSIG.
*/
result = ISC_R_NOTFOUND;
/*
* First, look for a tsig key in the master statement, then
* try for a server key.
*/
if ((zone->masterkeynames != NULL) &&
(zone->masterkeynames[zone->curmaster] != NULL)) {
dns_view_t *view = dns_zone_getview(zone);
dns_name_t *keyname = zone->masterkeynames[zone->curmaster];
result = dns_view_gettsig(view, keyname, &tsigkey);
}
if (tsigkey == NULL)
result = dns_view_getpeertsig(zone->view, &masterip, &tsigkey);
if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
zone_log(zone, me, ISC_LOG_ERROR,
"error getting tsig key "
"for zone transfer: %s",
isc_result_totext(result));
}
result = dns_xfrin_create(zone, xfrtype, &zone->masteraddr,
tsigkey, zone->mctx,
zone->zmgr->timermgr, zone->zmgr->socketmgr,
zone->task, zone_xfrdone, &zone->xfr);
cleanup:
/*
* Any failure in this function is handled like a failed
* zone transfer. This ensures that we get removed from
* zmgr->xfrin_in_progress.
*/
if (result != ISC_R_SUCCESS)
zone_xfrdone(zone, result);
if (tsigkey != NULL)
dns_tsigkey_detach(&tsigkey);
isc_event_free(&event);
dns_zone_detach(&zone); /* XXXAG */
return;
}
/*
* Update forwarding support.
*/
static void
forward_destroy(dns_forward_t *forward) {
forward->magic = 0;
if (forward->request != NULL)
dns_request_destroy(&forward->request);
if (forward->msgbuf != NULL)
isc_buffer_free(&forward->msgbuf);
if (forward->zone != NULL)
dns_zone_idetach(&forward->zone);
isc_mem_putanddetach(&forward->mctx, forward, sizeof (*forward));
}
static isc_result_t
sendtomaster(dns_forward_t *forward) {
isc_result_t result;
isc_sockaddr_t src;
LOCK_ZONE(forward->zone);
if (forward->which >= forward->zone->masterscnt) {
UNLOCK_ZONE(forward->zone);
return (ISC_R_NOMORE);
}
forward->addr = forward->zone->masters[forward->which];
/*
* Always use TCP regardless of whether the original update
* used TCP.
* XXX The timeout may but a bit small if we are far down a
* transfer graph and the master has to try several masters.
*/
switch (isc_sockaddr_pf(&forward->addr)) {
case PF_INET:
src = forward->zone->xfrsource4;
break;
case PF_INET6:
src = forward->zone->xfrsource6;
break;
default:
result = ISC_R_NOTIMPLEMENTED;
goto unlock;
}
result = dns_request_createraw(forward->zone->view->requestmgr,
forward->msgbuf,
&src, &forward->addr,
DNS_REQUESTOPT_TCP, 15 /* XXX */,
forward->zone->task,
forward_callback, forward,
&forward->request);
unlock:
UNLOCK_ZONE(forward->zone);
return (result);
}
static void
forward_callback(isc_task_t *task, isc_event_t *event) {
const char me[] = "forward_callback";
dns_requestevent_t *revent = (dns_requestevent_t *)event;
dns_message_t *msg = NULL;
char master[ISC_SOCKADDR_FORMATSIZE];
isc_result_t result;
dns_forward_t *forward;
dns_zone_t *zone;
UNUSED(task);
forward = revent->ev_arg;
INSIST(DNS_FORWARD_VALID(forward));
zone = forward->zone;
INSIST(DNS_ZONE_VALID(zone));
DNS_ENTER;
isc_sockaddr_format(&forward->addr, master, sizeof(master));
if (revent->result != ISC_R_SUCCESS) {
zone_log(zone, me, ISC_LOG_INFO, "failure for %s: %s",
master, dns_result_totext(revent->result));
goto next_master;
}
result = dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
if (result != ISC_R_SUCCESS)
goto next_master;
result = dns_request_getresponse(revent->request, msg,
DNS_MESSAGEPARSE_PRESERVEORDER |
DNS_MESSAGEPARSE_CLONEBUFFER);
if (result != ISC_R_SUCCESS)
goto next_master;
switch (msg->rcode) {
/*
* Pass these rcodes back to client.
*/
case dns_rcode_noerror:
case dns_rcode_yxdomain:
case dns_rcode_yxrrset:
case dns_rcode_nxrrset:
case dns_rcode_refused:
case dns_rcode_nxdomain:
break;
/* These should not occur if the masters/zone are valid. */
case dns_rcode_notzone:
case dns_rcode_notauth: {
char rcode[128];
isc_buffer_t rb;
isc_buffer_init(&rb, rcode, sizeof(rcode));
dns_rcode_totext(msg->rcode, &rb);
zone_log(zone, me, ISC_LOG_WARNING,
"unexpected response: master %s returned: %.*s",
master, rb.used, rcode);
goto next_master;
}
/* Try another server for these rcodes. */
case dns_rcode_formerr:
case dns_rcode_servfail:
case dns_rcode_notimp:
case dns_rcode_badvers:
default:
goto next_master;
}
/* call callback */
(forward->callback)(forward->callback_arg, ISC_R_SUCCESS, msg);
msg = NULL;
dns_request_destroy(&forward->request);
forward_destroy(forward);
isc_event_free(&event);
return;
next_master:
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
forward->which++;
dns_request_destroy(&forward->request);
result = sendtomaster(forward);
if (result != ISC_R_SUCCESS) {
/* call callback */
zone_log(zone, me, ISC_LOG_DEBUG(3), "exhausted forwarder");
(forward->callback)(forward->callback_arg, result, NULL);
forward_destroy(forward);
}
}
isc_result_t
dns_zone_forwardupdate(dns_zone_t *zone, dns_message_t *msg,
dns_updatecallback_t callback, void *callback_arg)
{
dns_forward_t *forward;
isc_result_t result;
isc_region_t *mr;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(msg != NULL);
REQUIRE(callback != NULL);
forward = isc_mem_get(zone->mctx, sizeof(*forward));
if (forward == NULL)
return (ISC_R_NOMEMORY);
forward->request = NULL;
forward->zone = NULL;
forward->msgbuf = NULL;
forward->which = 0;
forward->mctx = 0;
forward->callback = callback;
forward->callback_arg = callback_arg;
forward->magic = FORWARD_MAGIC;
mr = dns_message_getrawmessage(msg);
if (mr == NULL) {
result = ISC_R_UNEXPECTEDEND;
goto cleanup;
}
result = isc_buffer_allocate(zone->mctx, &forward->msgbuf, mr->length);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = isc_buffer_copyregion(forward->msgbuf, mr);
if (result != ISC_R_SUCCESS)
goto cleanup;
isc_mem_attach(zone->mctx, &forward->mctx);
dns_zone_iattach(zone, &forward->zone);
result = sendtomaster(forward);
cleanup:
if (result != ISC_R_SUCCESS) {
forward_destroy(forward);
}
return (result);
}
isc_result_t
dns_zone_next(dns_zone_t *zone, dns_zone_t **next) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(next != NULL && *next == NULL);
*next = ISC_LIST_NEXT(zone, link);
if (*next == NULL)
return (ISC_R_NOMORE);
else
return (ISC_R_SUCCESS);
}
isc_result_t
dns_zone_first(dns_zonemgr_t *zmgr, dns_zone_t **first) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
REQUIRE(first != NULL && *first == NULL);
*first = ISC_LIST_HEAD(zmgr->zones);
if (*first == NULL)
return (ISC_R_NOMORE);
else
return (ISC_R_SUCCESS);
}
/***
*** Zone manager.
***/
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;
isc_interval_t interval;
zmgr = isc_mem_get(mctx, sizeof *zmgr);
if (zmgr == NULL)
return (ISC_R_NOMEMORY);
zmgr->mctx = NULL;
zmgr->refs = 1;
isc_mem_attach(mctx, &zmgr->mctx);
zmgr->taskmgr = taskmgr;
zmgr->timermgr = timermgr;
zmgr->socketmgr = socketmgr;
zmgr->zonetasks = NULL;
zmgr->task = NULL;
zmgr->rl = NULL;
ISC_LIST_INIT(zmgr->zones);
ISC_LIST_INIT(zmgr->waiting_for_xfrin);
ISC_LIST_INIT(zmgr->xfrin_in_progress);
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 = ISC_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 = ISC_R_UNEXPECTED;
goto free_rwlock;
}
zmgr->transfersin = 10;
zmgr->transfersperns = 2;
/* Create the zone task pool. */
result = isc_taskpool_create(taskmgr, mctx,
8 /* XXX */, 0, &zmgr->zonetasks);
if (result != ISC_R_SUCCESS)
goto free_conflock;
/* Create a single task for queueing of SOA queries. */
result = isc_task_create(taskmgr, 1, &zmgr->task);
if (result != ISC_R_SUCCESS)
goto free_taskpool;
isc_task_setname(zmgr->task, "zmgr", zmgr);
result = isc_ratelimiter_create(mctx, timermgr, zmgr->task,
&zmgr->rl);
if (result != ISC_R_SUCCESS)
goto free_task;
/* 20 refresh queries / notifies per second. */
isc_interval_set(&interval, 0, 1000000000/2);
result = isc_ratelimiter_setinterval(zmgr->rl, &interval);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
isc_ratelimiter_setpertic(zmgr->rl, 10);
zmgr->iolimit = 1;
zmgr->ioactive = 0;
ISC_LIST_INIT(zmgr->high);
ISC_LIST_INIT(zmgr->low);
result = isc_mutex_init(&zmgr->iolock);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_mutex_init() failed: %s",
isc_result_totext(result));
goto free_rl;
}
zmgr->magic = ZONEMGR_MAGIC;
*zmgrp = zmgr;
return (ISC_R_SUCCESS);
#if 0
free_iolock:
DESTROYLOCK(&zmgr->iolock);
#endif
free_rl:
isc_ratelimiter_detach(&zmgr->rl);
free_task:
isc_task_detach(&zmgr->task);
free_taskpool:
isc_taskpool_destroy(&zmgr->zonetasks);
free_conflock:
isc_rwlock_destroy(&zmgr->conflock);
free_rwlock:
isc_rwlock_destroy(&zmgr->rwlock);
free_mem:
isc_mem_put(zmgr->mctx, zmgr, sizeof *zmgr);
isc_mem_detach(&mctx);
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));
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
LOCK_ZONE(zone);
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);
/*
* Set the task name. The tag will arbitrarily point to one
* of the zones sharing the task (in practice, the one
* to be managed last).
*/
isc_task_setname(zone->task, "zone", zone);
result = isc_timer_create(zmgr->timermgr, isc_timertype_inactive,
NULL, NULL,
zone->task, zone_timer, zone,
&zone->timer);
if (result != ISC_R_SUCCESS)
goto cleanup_task;
ISC_LIST_APPEND(zmgr->zones, zone, link);
zone->zmgr = zmgr;
zmgr->refs++;
goto unlock;
cleanup_task:
isc_task_detach(&zone->task);
unlock:
UNLOCK_ZONE(zone);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
return (result);
}
void
dns_zonemgr_releasezone(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
isc_boolean_t free_now = ISC_FALSE;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
REQUIRE(zone->zmgr == zmgr);
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
LOCK_ZONE(zone);
ISC_LIST_UNLINK(zmgr->zones, zone, link);
zone->zmgr = NULL;
zmgr->refs--;
if (zmgr->refs == 0)
free_now = ISC_TRUE;
UNLOCK_ZONE(zone);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
if (free_now)
zonemgr_free(zmgr);
ENSURE(zone->zmgr == NULL);
}
void
dns_zonemgr_attach(dns_zonemgr_t *source, dns_zonemgr_t **target) {
REQUIRE(DNS_ZONEMGR_VALID(source));
REQUIRE(target != NULL && *target == NULL);
RWLOCK(&source->rwlock, isc_rwlocktype_write);
REQUIRE(source->refs > 0);
source->refs++;
INSIST(source->refs > 0);
RWUNLOCK(&source->rwlock, isc_rwlocktype_write);
*target = source;
}
void
dns_zonemgr_detach(dns_zonemgr_t **zmgrp) {
dns_zonemgr_t *zmgr;
isc_boolean_t free_now = ISC_FALSE;
REQUIRE(zmgrp != NULL);
zmgr = *zmgrp;
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
zmgr->refs--;
if (zmgr->refs == 0)
free_now = ISC_TRUE;
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
if (free_now)
zonemgr_free(zmgr);
}
isc_result_t
dns_zonemgr_forcemaint(dns_zonemgr_t *zmgr) {
dns_zone_t *p;
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
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);
}
/*
* Recent configuration changes may have increased the
* amount of available transfers quota. Make sure any
* transfers currently blocked on quota get started if
* possible.
*/
zmgr_resume_xfrs(zmgr);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_read);
return (ISC_R_SUCCESS);
}
void
dns_zonemgr_shutdown(dns_zonemgr_t *zmgr) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
isc_ratelimiter_shutdown(zmgr->rl);
if (zmgr->task != NULL)
isc_task_destroy(&zmgr->task);
if (zmgr->zonetasks != NULL)
isc_taskpool_destroy(&zmgr->zonetasks);
}
static void
zonemgr_free(dns_zonemgr_t *zmgr) {
isc_mem_t *mctx;
INSIST(zmgr->refs == 0);
INSIST(ISC_LIST_EMPTY(zmgr->zones));
zmgr->magic = 0;
DESTROYLOCK(&zmgr->iolock);
isc_ratelimiter_detach(&zmgr->rl);
isc_rwlock_destroy(&zmgr->conflock);
isc_rwlock_destroy(&zmgr->rwlock);
mctx = zmgr->mctx;
isc_mem_put(zmgr->mctx, zmgr, sizeof *zmgr);
isc_mem_detach(&mctx);
}
void
dns_zonemgr_lockconf(dns_zonemgr_t *zmgr, isc_rwlocktype_t type) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
RWLOCK(&zmgr->conflock, type);
}
void
dns_zonemgr_unlockconf(dns_zonemgr_t *zmgr, isc_rwlocktype_t type) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
RWUNLOCK(&zmgr->conflock, type);
}
void
dns_zonemgr_settransfersin(dns_zonemgr_t *zmgr, int value) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
zmgr->transfersin = value;
}
int
dns_zonemgr_getttransfersin(dns_zonemgr_t *zmgr) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
return (zmgr->transfersin);
}
void
dns_zonemgr_settransfersperns(dns_zonemgr_t *zmgr, int value) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
zmgr->transfersperns = value;
}
int
dns_zonemgr_getttransfersperns(dns_zonemgr_t *zmgr) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
return (zmgr->transfersperns);
}
/*
* Try to start a new incoming zone transfer to fill a quota
* slot that was just vacated.
*
* Requires:
* The zone manager is locked by the caller.
*/
static void
zmgr_resume_xfrs(dns_zonemgr_t *zmgr) {
static char me[] = "zmgr_resume_xfrs";
dns_zone_t *zone;
for (zone = ISC_LIST_HEAD(zmgr->waiting_for_xfrin);
zone != NULL;
zone = ISC_LIST_NEXT(zone, statelink))
{
isc_result_t result;
result = zmgr_start_xfrin_ifquota(zmgr, zone);
if (result == ISC_R_SUCCESS) {
/*
* We successfully filled the slot. We're done.
*/
break;
} else if (result == ISC_R_QUOTA) {
/*
* Not enough quota. This is probably the per-server
* quota, because we only get called when a unit of
* global quota has just been freed. Try the next
* zone, it may succeed if it uses another master.
*/
continue;
} else {
zone_log(zone, me, ISC_LOG_DEBUG(3),
"starting zone transfer: %s",
isc_result_totext(result));
break;
}
}
}
/*
* Try to start an incoming zone transfer for 'zone', quota permitting.
*
* Requires:
* The zone manager is locked by the caller.
*
* Returns:
* ISC_R_SUCCESS There was enough quota and we attempted to
* start a transfer. zone_xfrdone() has been or will
* be called.
* ISC_R_QUOTA Not enough quota.
* Others Failure.
*/
static isc_result_t
zmgr_start_xfrin_ifquota(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
dns_peer_t *peer = NULL;
isc_netaddr_t masterip;
int nxfrsin, nxfrsperns;
dns_zone_t *x;
int maxtransfersin, maxtransfersperns;
isc_event_t *e;
/*
* Find any configured information about the server we'd
* like to transfer this zone from.
*/
isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
(void)dns_peerlist_peerbyaddr(zone->view->peers,
&masterip, &peer);
/*
* Determine the total maximum number of simultaneous
* transfers allowed, and the maximum for this specific
* master.
*/
maxtransfersin = zmgr->transfersin;
maxtransfersperns = zmgr->transfersperns;
if (peer != NULL)
(void)dns_peer_gettransfers(peer, &maxtransfersperns);
/*
* Count the total number of transfers that are in progress,
* and the number of transfers in progress from this master.
* We linearly scan a list of all transfers; if this turns
* out to be too slow, we could hash on the master address.
*/
nxfrsin = nxfrsperns = 0;
for (x = ISC_LIST_HEAD(zmgr->xfrin_in_progress);
x != NULL;
x = ISC_LIST_NEXT(x, statelink))
{
isc_netaddr_t xip;
isc_netaddr_fromsockaddr(&xip, &x->masteraddr);
nxfrsin++;
if (isc_netaddr_equal(&xip, &masterip))
nxfrsperns++;
}
/* Enforce quota. */
if (nxfrsin >= maxtransfersin)
return (ISC_R_QUOTA);
if (nxfrsperns >= maxtransfersperns)
return (ISC_R_QUOTA);
/*
* We have sufficient quota. Move the zone to the "xfrin_in_progress"
* list and send it an event to let it start the actual transfer in the
* context of its own task.
*/
e = isc_event_allocate(zmgr->mctx, zmgr,
DNS_EVENT_ZONESTARTXFRIN,
got_transfer_quota, zone,
sizeof(isc_event_t));
if (e == NULL)
return (ISC_R_NOMEMORY);
LOCK_ZONE(zone);
INSIST(zone->statelist == &zmgr->waiting_for_xfrin);
ISC_LIST_UNLINK(zmgr->waiting_for_xfrin, zone, statelink);
ISC_LIST_APPEND(zmgr->xfrin_in_progress, zone, statelink);
zone->statelist = &zmgr->xfrin_in_progress;
/*
* Make sure the zone does not go away before it has processed
* the event; in effect, the event is attached to the zone.
*
* XXXAG This should be done as soon as the zone goes on the
* queue, using irefs.
*/
zone->erefs++;
isc_task_send(zone->task, &e);
UNLOCK_ZONE(zone);
return (ISC_R_SUCCESS);
}
void
dns_zonemgr_setiolimit(dns_zonemgr_t *zmgr, isc_uint32_t iolimit) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
REQUIRE(iolimit > 0);
zmgr->iolimit = iolimit;
}
isc_uint32_t
dns_zonemgr_getiolimit(dns_zonemgr_t *zmgr) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
return (zmgr->iolimit);
}
/*
* Get permission to request a file handle from the OS.
* An event will be sent to action when one is available.
* There are two queues available (high and low), the high
* queue will be serviced before the low one.
*
* zonemgr_putio() must be called after the event is delivered to
* 'action'.
*/
static isc_result_t
zonemgr_getio(dns_zonemgr_t *zmgr, isc_boolean_t high,
isc_task_t *task, isc_taskaction_t action, void *arg,
dns_io_t **iop)
{
dns_io_t *io;
isc_boolean_t queue;
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
REQUIRE(iop != NULL && *iop == NULL);
io = isc_mem_get(zmgr->mctx, sizeof(*io));
if (io == NULL)
return (ISC_R_NOMEMORY);
io->event = isc_event_allocate(zmgr->mctx, task, DNS_EVENT_IOREADY,
action, arg, sizeof(*io->event));
if (io->event == NULL) {
isc_mem_put(zmgr->mctx, io, sizeof(*io));
return (ISC_R_NOMEMORY);
}
io->zmgr = zmgr;
io->high = high;
io->task = NULL;
isc_task_attach(task, &io->task);
ISC_LINK_INIT(io, link);
io->magic = IO_MAGIC;
LOCK(&zmgr->iolock);
zmgr->ioactive++;
queue = ISC_TF(zmgr->ioactive > zmgr->iolimit);
if (queue) {
if (io->high)
ISC_LIST_APPEND(zmgr->high, io, link);
else
ISC_LIST_APPEND(zmgr->low, io, link);
}
UNLOCK(&zmgr->iolock);
*iop = io;
if (!queue) {
isc_task_send(io->task, &io->event);
}
return (ISC_R_SUCCESS);
}
static void
zonemgr_putio(dns_io_t **iop) {
dns_io_t *io;
dns_io_t *next;
dns_zonemgr_t *zmgr;
REQUIRE(iop != NULL);
io = *iop;
REQUIRE(DNS_IO_VALID(io));
*iop = NULL;
INSIST(!ISC_LINK_LINKED(io, link));
INSIST(io->event == NULL);
zmgr = io->zmgr;
isc_task_detach(&io->task);
io->magic = 0;
isc_mem_put(zmgr->mctx, io, sizeof(*io));
LOCK(&zmgr->iolock);
INSIST(zmgr->ioactive > 0);
zmgr->ioactive--;
next = HEAD(zmgr->high);
if (next == NULL)
next = HEAD(zmgr->low);
if (next != NULL) {
if (next->high)
ISC_LIST_UNLINK(zmgr->high, next, link);
else
ISC_LIST_UNLINK(zmgr->low, next, link);
INSIST(next->event != NULL);
}
UNLOCK(&zmgr->iolock);
if (next != NULL)
isc_task_send(next->task, &next->event);
}
static void
zonemgr_cancelio(dns_io_t *io) {
isc_boolean_t send_event = ISC_FALSE;
REQUIRE(DNS_IO_VALID(io));
/*
* If we are queued to be run then dequeue.
*/
LOCK(&io->zmgr->iolock);
if (ISC_LINK_LINKED(io, link)) {
if (io->high)
ISC_LIST_UNLINK(io->zmgr->high, io, link);
else
ISC_LIST_UNLINK(io->zmgr->low, io, link);
send_event = ISC_TRUE;
INSIST(io->event != NULL);
}
UNLOCK(&io->zmgr->iolock);
if (send_event) {
io->event->ev_attributes |= ISC_EVENTATTR_CANCELED;
isc_task_send(io->task, &io->event);
}
}
static void
zone_saveunique(dns_zone_t *zone, const char *path, const char *templat) {
char *buf;
int buflen;
isc_result_t result;
const char me[] = "zone_saveunique";
buflen = strlen(path) + strlen(templat) + 2;
buf = isc_mem_get(zone->mctx, buflen);
if (buf == NULL)
return;
result = isc_file_template(path, templat, buf, buflen);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = isc_file_renameunique(path, buf);
if (result != ISC_R_SUCCESS)
goto cleanup;
zone_log(zone, me, ISC_LOG_INFO, "saved '%s' as '%s'",
path, buf);
cleanup:
isc_mem_put(zone->mctx, buf, buflen);
}
#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_DEBUG(3),
"database (%p) destroyed", (void*) db);
}
#endif
void
dns_zone_forcereload(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FORCEXFER);
UNLOCK_ZONE(zone);
dns_zone_refresh(zone);
}
isc_boolean_t
dns_zone_isforced(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER));
}
isc_result_t
dns_zone_setstatistics(dns_zone_t *zone, isc_boolean_t on) {
isc_result_t result = ISC_R_SUCCESS;
LOCK_ZONE(zone);
if (on) {
if (zone->counters != NULL)
goto done;
result = dns_stats_alloccounters(zone->mctx, &zone->counters);
} else {
if (zone->counters == NULL)
goto done;
dns_stats_freecounters(zone->mctx, &zone->counters);
}
done:
UNLOCK_ZONE(zone);
return (result);
}
isc_uint64_t *
dns_zone_getstatscounters(dns_zone_t *zone) {
return (zone->counters);
}
void
dns_zone_dialup(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
zone_log(zone, "dns_zone_dialup", ISC_LOG_DEBUG(3),
"notify = %d, refresh = %d",
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALNOTIFY),
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH));
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALNOTIFY))
dns_zone_notify(zone);
if (zone->type != dns_zone_master &&
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH))
dns_zone_refresh(zone);
}
void
dns_zone_setdialup(dns_zone_t *zone, dns_dialuptype_t dialup) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DIALNOTIFY |
DNS_ZONEFLG_DIALREFRESH |
DNS_ZONEFLG_NOREFRESH);
switch (dialup) {
case dns_dialuptype_no:
break;
case dns_dialuptype_yes:
DNS_ZONE_SETFLAG(zone, (DNS_ZONEFLG_DIALNOTIFY |
DNS_ZONEFLG_DIALREFRESH |
DNS_ZONEFLG_NOREFRESH));
break;
case dns_dialuptype_notify:
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DIALNOTIFY);
break;
case dns_dialuptype_notifypassive:
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DIALNOTIFY);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOREFRESH);
break;
case dns_dialuptype_refresh:
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DIALREFRESH);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOREFRESH);
break;
case dns_dialuptype_passive:
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOREFRESH);
break;
default:
INSIST(0);
}
UNLOCK_ZONE(zone);
}