zone.c revision 2eeaed2812c732430b0a4a57d5d3f58f01cfeab7
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
1413616670fcb95b9ef236351502e4884ddca8bfTinderbox User * Copyright (C) 1999-2002 Internet Software Consortium.
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark 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
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * copyright notice and this permission notice appear in all copies.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *
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.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: zone.c,v 1.367 2002/04/02 08:04:42 marka Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff#include <isc/file.h>
de153390f5a1f6d4fa86af91d4cae772d9846ca0Mark Andrews#include <isc/mutex.h>
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff#include <isc/print.h>
822f6cdabb1edd44472c7a758b5cae71376fa9beBrian Wellington#include <isc/random.h>
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington#include <isc/ratelimiter.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <isc/refcount.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/serial.h>
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson#include <isc/string.h>
25a66b4e41e2b0a2af4840749bac80ae78c678bfMark Andrews#include <isc/taskpool.h>
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence#include <isc/timer.h>
21f1794606dce19928cf455029e173321f166380Mark Andrews#include <isc/util.h>
973a19342597823f111fce6a8cd5adfd0e2e7c0dMark Andrews
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff#include <dns/acl.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/adb.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/callbacks.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/db.h>
058e44186b74531402c1f99088eb9dbe4926f8daMark Andrews#include <dns/events.h>
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews#include <dns/journal.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/log.h>
21825a8d005ccc2dfaf12889bf9eef3413555277Brian Wellington#include <dns/master.h>
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#include <dns/masterdump.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/message.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/name.h>
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews#include <dns/peer.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rcode.h>
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews#include <dns/rdataclass.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdatalist.h>
54c26ab21c61c6d6b1e484bb88dc3ac263845d17Mark Andrews#include <dns/rdataset.h>
3ddd92da6651bc72aa79a04195ad389d86fd1a66Andreas Gustafsson#include <dns/rdatastruct.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/request.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/resolver.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/result.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <dns/stats.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <dns/ssu.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <dns/tsig.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <dns/xfrin.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <dns/zone.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews#define ZONE_MAGIC ISC_MAGIC('Z', 'O', 'N', 'E')
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ZONE_VALID(zone) ISC_MAGIC_VALID(zone, ZONE_MAGIC)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define NOTIFY_MAGIC ISC_MAGIC('N', 't', 'f', 'y')
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_NOTIFY_VALID(notify) ISC_MAGIC_VALID(notify, NOTIFY_MAGIC)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define STUB_MAGIC ISC_MAGIC('S', 't', 'u', 'b')
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_STUB_VALID(stub) ISC_MAGIC_VALID(stub, STUB_MAGIC)
eb6bd543c7d072efdca509eb17f8f301c1467b53Mark Andrews
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews#define ZONEMGR_MAGIC ISC_MAGIC('Z', 'm', 'g', 'r')
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews#define DNS_ZONEMGR_VALID(stub) ISC_MAGIC_VALID(stub, ZONEMGR_MAGIC)
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence#define LOAD_MAGIC ISC_MAGIC('L', 'o', 'a', 'd')
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define DNS_LOAD_VALID(load) ISC_MAGIC_VALID(load, LOAD_MAGIC)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews#define FORWARD_MAGIC ISC_MAGIC('F', 'o', 'r', 'w')
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define DNS_FORWARD_VALID(load) ISC_MAGIC_VALID(load, FORWARD_MAGIC)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define IO_MAGIC ISC_MAGIC('Z', 'm', 'I', 'O')
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define DNS_IO_VALID(load) ISC_MAGIC_VALID(load, IO_MAGIC)
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Ensure 'a' is at least 'min' but not more than 'max'.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define RANGE(a, min, max) \
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence (((a) < (min)) ? (min) : ((a) < (max) ? (a) : (max)))
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence/*
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Default values.
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define DNS_DEFAULT_IDLEIN 3600 /* 1 hour */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define DNS_DEFAULT_IDLEOUT 3600 /* 1 hour */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define MAX_XFER_TIME (2*3600) /* Documented default is 2 hours */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#ifndef DNS_MAX_EXPIRE
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define DNS_MAX_EXPIRE 14515200 /* 24 weeks */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#endif
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#ifndef DNS_DUMP_DELAY
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define DNS_DUMP_DELAY 900 /* 15 minutes */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#endif
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewstypedef struct dns_notify dns_notify_t;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewstypedef struct dns_stub dns_stub_t;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewstypedef struct dns_load dns_load_t;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewstypedef struct dns_forward dns_forward_t;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewstypedef struct dns_io dns_io_t;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeintypedef ISC_LIST(dns_io_t) dns_iolist_t;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson#define DNS_ZONE_CHECKLOCK
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson#ifdef DNS_ZONE_CHECKLOCK
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson#define LOCK_ZONE(z) \
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson do { LOCK(&(z)->lock); \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein INSIST((z)->locked == ISC_FALSE); \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein (z)->locked = ISC_TRUE; \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein } while (0)
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater#define UNLOCK_ZONE(z) \
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson do { (z)->locked = ISC_FALSE; UNLOCK(&(z)->lock); } while (0)
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson#define LOCKED_ZONE(z) ((z)->locked)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence#else
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt#define LOCK_ZONE(z) LOCK(&(z)->lock)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence#define UNLOCK_ZONE(z) UNLOCK(&(z)->lock)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence#define LOCKED_ZONE(z) ISC_TRUE
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence#endif
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestruct dns_zone {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence /* Unlocked */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int magic;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mutex_t lock;
7712d1660a308ec3de17f1ddbbf801eb0d663f3eEvan Hunt#ifdef DNS_ZONE_CHECKLOCK
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_boolean_t locked;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont#endif
7712d1660a308ec3de17f1ddbbf801eb0d663f3eEvan Hunt isc_mem_t *mctx;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_refcount_t erefs;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence /* Locked */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_db_t *db;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_zonemgr_t *zmgr;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ISC_LINK(dns_zone_t) link; /* Used by zmgr. */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_timer_t *timer;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int irefs;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_name_t origin;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence char *masterfile;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington char *journal;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington isc_int32_t journalsize;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington dns_rdataclass_t rdclass;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_zonetype_t type;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int flags;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int options;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int db_argc;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence char **db_argv;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_time_t expiretime;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_time_t refreshtime;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_time_t dumptime;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_time_t loadtime;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t serial;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t refresh;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t retry;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t expire;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t minimum;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence char *keydirectory;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t maxrefresh;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t minrefresh;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t maxretry;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t minretry;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_sockaddr_t *masters;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_name_t **masterkeynames;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int masterscnt;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int curmaster;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_sockaddr_t masteraddr;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_notifytype_t notifytype;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_sockaddr_t *notify;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews unsigned int notifycnt;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_sockaddr_t notifyfrom;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_task_t *task;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_sockaddr_t notifysrc4;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_sockaddr_t notifysrc6;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_sockaddr_t xfrsource4;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_sockaddr_t xfrsource6;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_xfrin_ctx_t *xfr; /* task locked */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence /* Access Control Lists */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_acl_t *update_acl;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_acl_t *forward_acl;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_acl_t *notify_acl;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_acl_t *query_acl;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_acl_t *xfr_acl;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_boolean_t update_disabled;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_severity_t check_names;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ISC_LIST(dns_notify_t) notifies;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_request_t *request;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson dns_loadctx_t *lctx;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson dns_io_t *readio;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_dumpctx_t *dctx;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_io_t *writeio;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence isc_uint32_t maxxfrin;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence isc_uint32_t maxxfrout;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence isc_uint32_t idlein;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t idleout;
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews isc_event_t ctlevent;
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews dns_ssutable_t *ssutable;
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews isc_uint32_t sigvalidityinterval;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_view_t *view;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence /*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Zones in certain states such as "waiting for zone transfer"
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson * or "zone transfer in progress" are kept on per-state linked lists
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * in the zone manager using the 'statelink' field. The 'statelist'
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * field points at the list the zone is currently on. It the zone
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * is not on any such list, statelist is NULL.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews ISC_LINK(dns_zone_t) statelink;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews dns_zonelist_t *statelist;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews /*
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews * Optional per-zone statistics counters (NULL if not present).
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews */
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_uint64_t *counters;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews};
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews#define DNS_ZONE_FLAG(z,f) (ISC_TF(((z)->flags & (f)) != 0))
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews#define DNS_ZONE_SETFLAG(z,f) do { \
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews INSIST(LOCKED_ZONE(z)); \
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews (z)->flags |= (f); \
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews } while (0)
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONE_CLRFLAG(z,f) do { \
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews INSIST(LOCKED_ZONE(z)); \
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews (z)->flags &= ~(f); \
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews } while (0)
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews /* XXX MPA these may need to go back into zone.h */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_REFRESH 0x00000001U /* refresh check in progress */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_NEEDDUMP 0x00000002U /* zone need consolidation */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_USEVC 0x00000004U /* use tcp for refresh query */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_DUMPING 0x00000008U /* a dump is in progress */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_HASINCLUDE 0x00000010U /* $INCLUDE in zone file */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_LOADED 0x00000020U /* database has loaded */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_EXITING 0x00000040U /* zone is being destroyed */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_EXPIRED 0x00000080U /* zone has expired */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_NEEDREFRESH 0x00000100U /* refresh check needed */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_UPTODATE 0x00000200U /* zone contents are
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * uptodate */
6a528eaa09b6eed63144250dc61062f3a84880abMark Andrews#define DNS_ZONEFLG_NEEDNOTIFY 0x00000400U /* need to send out notify
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * messages */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_DIFFONRELOAD 0x00000800U /* generate a journal diff on
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * reload */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_NOMASTERS 0x00001000U /* an attempt to refresh a
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * zone with no masters
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * occured */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_LOADING 0x00002000U /* load from disk in progress*/
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_HAVETIMERS 0x00004000U /* timer values have been set
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * from SOA (if not set, we
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * are still using
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * default timer values) */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_FORCEXFER 0x00008000U /* Force a zone xfer */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_NOREFRESH 0x00010000U
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_DIALNOTIFY 0x00020000U
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_DIALREFRESH 0x00040000U
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_SHUTDOWN 0x00080000U
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLAG_NOIXFR 0x00100000U /* IXFR failed, force AXFR */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_FLUSH 0x00200000U
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONEFLG_NOEDNS 0x00400000U
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONE_OPTION(z,o) (((z)->options & (o)) != 0)
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews/* Flags for zone_load() */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#define DNS_ZONELOADFLAG_NOSTAT 0x00000001U /* Do not stat() master files */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsstruct dns_zonemgr {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews unsigned int magic;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_mem_t * mctx;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews int refs; /* Locked by rwlock */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_taskmgr_t * taskmgr;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_timermgr_t * timermgr;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_socketmgr_t * socketmgr;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_taskpool_t * zonetasks;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_task_t * task;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_ratelimiter_t * rl;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_rwlock_t rwlock;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_mutex_t iolock;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews /* Locked by rwlock. */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dns_zonelist_t zones;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dns_zonelist_t waiting_for_xfrin;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt dns_zonelist_t xfrin_in_progress;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews /* Configuration data. */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_uint32_t transfersin;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews isc_uint32_t transfersperns;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews unsigned int serialqueryrate;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews /* Locked by iolock */
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews isc_uint32_t iolimit;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews isc_uint32_t ioactive;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews dns_iolist_t high;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews dns_iolist_t low;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews};
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews/*
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews * Hold notify state.
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews */
aa05bbdef7f7827dde158dcc913f4dade84c8511Brian Wellingtonstruct dns_notify {
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews unsigned int magic;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews unsigned int flags;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews isc_mem_t *mctx;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews dns_zone_t *zone;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews dns_adbfind_t *find;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews dns_request_t *request;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews dns_name_t ns;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews isc_sockaddr_t dst;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews ISC_LINK(dns_notify_t) link;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews};
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews#define DNS_NOTIFY_NOSOA 0x0001U
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews/*
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews * dns_stub holds state while performing a 'stub' transfer.
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews * 'db' is the zone's 'db' or a new one if this is the initial
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews * transfer.
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews */
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrewsstruct dns_stub {
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews unsigned int magic;
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews isc_mem_t *mctx;
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews dns_zone_t *zone;
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews dns_db_t *db;
f6407f9a0b890bebbfd5f738d9c4aef3d3315fe9Michael Graff dns_dbversion_t *version;
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews};
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews/*
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews * Hold load state.
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstruct dns_load {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int magic;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_t *mctx;
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews dns_zone_t *zone;
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews dns_db_t *db;
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews isc_time_t loadtime;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdatacallbacks_t callbacks;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews};
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Hold forward state.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstruct dns_forward {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int magic;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_t *mctx;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_zone_t *zone;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_t *msgbuf;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley dns_request_t *request;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_uint32_t which;
e6bd97dded968f82e26b270842b789bff7bca422Mark Andrews isc_sockaddr_t addr;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_updatecallback_t callback;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews void *callback_arg;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews};
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews/*
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews * Hold IO request state.
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews */
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstruct dns_io {
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellington unsigned int magic;
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews dns_zonemgr_t *zmgr;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews isc_boolean_t high;
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews isc_task_t *task;
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews ISC_LINK(dns_io_t) link;
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews isc_event_t *event;
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews};
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews#define SEND_BUFFER_SIZE 2048
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic void zone_settimer(dns_zone_t *, isc_time_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void cancel_refresh(dns_zone_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void zone_debuglog(dns_zone_t *zone, const char *, int debuglevel,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews const char *msg, ...) ISC_FORMAT_PRINTF(4, 5);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void notify_log(dns_zone_t *zone, int level, const char *fmt, ...)
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews ISC_FORMAT_PRINTF(3, 4);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void queue_xfrin(dns_zone_t *zone);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void zone_unload(dns_zone_t *zone);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic void zone_expire(dns_zone_t *zone);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic void zone_iattach(dns_zone_t *source, dns_zone_t **target);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic void zone_idetach(dns_zone_t **zonep);
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellingtonstatic isc_result_t zone_replacedb(dns_zone_t *zone, dns_db_t *db,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews isc_boolean_t dump);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic isc_result_t default_journal(dns_zone_t *zone);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void zone_xfrdone(dns_zone_t *zone, isc_result_t result);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic isc_result_t zone_postload(dns_zone_t *zone, dns_db_t *db,
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews isc_time_t loadtime, isc_result_t result);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic void zone_needdump(dns_zone_t *zone, unsigned int delay);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic void zone_shutdown(isc_task_t *, isc_event_t *);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic void zone_loaddone(void *arg, isc_result_t result);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic isc_result_t zone_startload(dns_db_t *db, dns_zone_t *zone,
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews isc_time_t loadtime);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews#if 0
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/* ondestroy example */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void dns_zonemgr_dbdestroyed(isc_task_t *task, isc_event_t *event);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void refresh_callback(isc_task_t *, isc_event_t *);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic void stub_callback(isc_task_t *, isc_event_t *);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrewsstatic void queue_soa_query(dns_zone_t *zone);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrewsstatic void soa_query(isc_task_t *, isc_event_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_stub_t *stub);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic int message_count(dns_message_t *msg, dns_section_t section,
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews dns_rdatatype_t type);
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrewsstatic void notify_cancel(dns_zone_t *zone);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void notify_find_address(dns_notify_t *notify);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void notify_send(dns_notify_t *notify);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t notify_createmessage(dns_zone_t *zone,
d981ca645597116d227a48bf37cc5edc061c854dBob Halley unsigned int flags,
d981ca645597116d227a48bf37cc5edc061c854dBob Halley dns_message_t **messagep);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void notify_done(isc_task_t *task, isc_event_t *event);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void notify_send_toaddr(isc_task_t *task, isc_event_t *event);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t zone_dump(dns_zone_t *, isc_boolean_t);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void got_transfer_quota(isc_task_t *task, isc_event_t *event);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t zmgr_start_xfrin_ifquota(dns_zonemgr_t *zmgr,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_zone_t *zone);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrewsstatic void zmgr_resume_xfrs(dns_zonemgr_t *zmgr);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic void zonemgr_free(dns_zonemgr_t *zmgr);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_result_t zonemgr_getio(dns_zonemgr_t *zmgr, isc_boolean_t high,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews isc_task_t *task, isc_taskaction_t action,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews void *arg, dns_io_t **iop);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic void zonemgr_putio(dns_io_t **iop);
90e303b114e56db5809fdd19805243457fa43cd9Olafur Gudmundssonstatic void zonemgr_cancelio(dns_io_t *io);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewszone_get_from_db(dns_db_t *db, dns_name_t *origin, unsigned int *nscount,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int *soacount, isc_uint32_t *serial,
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews isc_uint32_t *refresh, isc_uint32_t *retry,
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews isc_uint32_t *expire, isc_uint32_t *minimum);
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic void zone_freedbargs(dns_zone_t *zone);
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic void forward_callback(isc_task_t *task, isc_event_t *event);
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic void zone_saveunique(dns_zone_t *zone, const char *path,
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews const char *templat);
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrewsstatic void zone_maintenance(dns_zone_t *zone);
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrewsstatic void zone_notify(dns_zone_t *zone);
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic void dump_done(void *arg, isc_result_t result);
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define ENTER zone_debuglog(zone, me, 1, "enter")
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic const unsigned int dbargc_default = 1;
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic const char *dbargv_default[] = { "rbt" };
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define DNS_ZONE_JITTER_ADD(a, b, c) \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews do { \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews isc_interval_t _i; \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews isc_uint32_t _j; \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews _j = isc_random_jitter((b), (b)/4); \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews isc_interval_set(&_i, _j, 0); \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) { \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews dns_zone_log(zone, ISC_LOG_WARNING, \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews "epoch approaching: upgrade required: " \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews "now + %s failed", #b); \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews isc_interval_set(&_i, _j/2, 0); \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews (void)isc_time_add((a), &_i, (c)); \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews } \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews } while (0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define DNS_ZONE_TIME_ADD(a, b, c) \
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews do { \
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_interval_t _i; \
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_interval_set(&_i, (b), 0); \
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) { \
d981ca645597116d227a48bf37cc5edc061c854dBob Halley dns_zone_log(zone, ISC_LOG_WARNING, \
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews "epoch approaching: upgrade required: " \
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews "now + %s failed", #b); \
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_interval_set(&_i, (b)/2, 0); \
19d365e4448f1782611280b020987988b7ac3210Mark Andrews (void)isc_time_add((a), &_i, (c)); \
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington } \
19d365e4448f1782611280b020987988b7ac3210Mark Andrews } while (0)
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews/***
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews *** Public functions.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ***/
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
d981ca645597116d227a48bf37cc5edc061c854dBob Halleyisc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsdns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews isc_result_t result;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_zone_t *zone;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(zonep != NULL && *zonep == NULL);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence REQUIRE(mctx != NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
19d365e4448f1782611280b020987988b7ac3210Mark Andrews zone = isc_mem_get(mctx, sizeof(*zone));
19d365e4448f1782611280b020987988b7ac3210Mark Andrews if (zone == NULL)
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (ISC_R_NOMEMORY);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = isc_mutex_init(&zone->lock);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result != ISC_R_SUCCESS) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_put(mctx, zone, sizeof(*zone));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff "isc_mutex_init() failed: %s",
d981ca645597116d227a48bf37cc5edc061c854dBob Halley isc_result_totext(result));
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews return (ISC_R_UNEXPECTED);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews /* XXX MPA check that all elements are initialised */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff zone->mctx = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#ifdef DNS_ZONE_CHECKLOCK
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->locked = ISC_FALSE;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews isc_mem_attach(mctx, &zone->mctx);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->db = NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews zone->zmgr = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LINK_INIT(zone, link);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews isc_refcount_init(&zone->erefs, 1); /* Implicit attach. */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews zone->irefs = 0;
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington dns_name_init(&zone->origin, NULL);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews zone->masterfile = NULL;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews zone->keydirectory = NULL;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews zone->journalsize = -1;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews zone->journal = NULL;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews zone->rdclass = dns_rdataclass_none;
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson zone->type = dns_zone_none;
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson zone->flags = 0;
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson zone->options = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->db_argc = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->db_argv = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_time_settoepoch(&zone->expiretime);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews isc_time_settoepoch(&zone->refreshtime);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews isc_time_settoepoch(&zone->dumptime);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews isc_time_settoepoch(&zone->loadtime);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->serial = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->refresh = DNS_ZONE_DEFAULTREFRESH;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->retry = DNS_ZONE_DEFAULTRETRY;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->expire = 0;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->minimum = 0;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->maxrefresh = DNS_ZONE_MAXREFRESH;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->minrefresh = DNS_ZONE_MINREFRESH;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->maxretry = DNS_ZONE_MAXRETRY;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->minretry = DNS_ZONE_MINRETRY;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->masters = NULL;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->masterkeynames = NULL;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->masterscnt = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->curmaster = 0;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->notify = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->notifytype = dns_notifytype_yes;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->notifycnt = 0;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->task = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->update_acl = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->forward_acl = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->notify_acl = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->query_acl = NULL;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence zone->xfr_acl = NULL;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence zone->update_disabled = ISC_FALSE;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence zone->check_names = dns_severity_ignore;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff zone->request = NULL;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews zone->lctx = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->readio = NULL;
ad7209ea70d346527ffdcda335153831341d9dcfAndreas Gustafsson zone->dctx = NULL;
7d8cdd869ed2162a5befda7cc1600136110f54d6Mark Andrews zone->writeio = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews zone->timer = NULL;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley zone->idlein = DNS_DEFAULT_IDLEIN;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->idleout = DNS_DEFAULT_IDLEOUT;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LIST_INIT(zone->notifies);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_sockaddr_any(&zone->notifysrc4);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_sockaddr_any6(&zone->notifysrc6);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_sockaddr_any(&zone->xfrsource4);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_sockaddr_any6(&zone->xfrsource6);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->xfr = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->maxxfrin = MAX_XFER_TIME;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->maxxfrout = MAX_XFER_TIME;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff zone->ssutable = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->sigvalidityinterval = 30 * 24 * 3600;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington zone->view = NULL;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews ISC_LINK_INIT(zone, statelink);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff zone->statelist = NULL;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews zone->counters = NULL;
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halley
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews zone->magic = ZONE_MAGIC;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
19d365e4448f1782611280b020987988b7ac3210Mark Andrews /* Must be after magic is set. */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews result = dns_zone_setdbtype(zone, dbargc_default, dbargv_default);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (result != ISC_R_SUCCESS)
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson goto free_mutex;
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson ISC_EVENT_INIT(&zone->ctlevent, sizeof(zone->ctlevent), 0, NULL,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews DNS_EVENT_ZONECONTROL, zone_shutdown, zone, zone,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews NULL, NULL);
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson *zonep = zone;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (ISC_R_SUCCESS);
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews free_mutex:
19d365e4448f1782611280b020987988b7ac3210Mark Andrews DESTROYLOCK(&zone->lock);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_NOMEMORY);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence/*
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Free a zone. Because we require that there be no more
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * outstanding events or references, no locking is necessary.
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt */
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halleystatic void
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffzone_free(dns_zone_t *zone) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_t *mctx = NULL;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence REQUIRE(isc_refcount_current(&zone->erefs) == 0);
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence REQUIRE(zone->irefs == 0);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews REQUIRE(!LOCKED_ZONE(zone));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(zone->timer == NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson * Managed objects. Order is important.
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson */
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson if (zone->request != NULL)
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson dns_request_destroy(&zone->request); /* XXXMPA */
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson INSIST(zone->readio == NULL);
3a16668468060842e847ea6556e80e1405d35cd6Brian Wellington INSIST(zone->statelist == NULL);
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson INSIST(zone->writeio == NULL);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews if (zone->task != NULL)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington isc_task_detach(&zone->task);
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington if (zone->zmgr)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington dns_zonemgr_releasezone(zone->zmgr, zone);
942d1a339b1fe617f7d17d66cb5fccce798d15aeBrian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington /* Unmanaged objects */
7e9d637131516486630290d36c4c0db544cb700eMark Andrews if (zone->masterfile != NULL)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington isc_mem_free(zone->mctx, zone->masterfile);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews zone->masterfile = NULL;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews if (zone->keydirectory != NULL)
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews isc_mem_free(zone->mctx, zone->keydirectory);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->keydirectory = NULL;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington zone->journalsize = -1;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (zone->journal != NULL)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington isc_mem_free(zone->mctx, zone->journal);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington zone->journal = NULL;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (zone->counters != NULL)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_stats_freecounters(zone->mctx, &zone->counters);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (zone->db != NULL)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_db_detach(&zone->db);
de5247ae1683ce145662180ee50272d2214a0232Andreas Gustafsson zone_freedbargs(zone);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington RUNTIME_CHECK(dns_zone_setmasterswithkeys(zone, NULL, NULL, 0)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington == ISC_R_SUCCESS);
1cc4695f0da63f0190e3514adccf6a96f3dc1519Mark Andrews RUNTIME_CHECK(dns_zone_setalsonotify(zone, NULL, 0)
1cc4695f0da63f0190e3514adccf6a96f3dc1519Mark Andrews == ISC_R_SUCCESS);
1cc4695f0da63f0190e3514adccf6a96f3dc1519Mark Andrews zone->check_names = dns_severity_ignore;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (zone->update_acl != NULL)
f16495732753175e4a9fc144323a12fdcc29b561Brian Wellington dns_acl_detach(&zone->update_acl);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (zone->forward_acl != NULL)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_acl_detach(&zone->forward_acl);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater if (zone->notify_acl != NULL)
e086935752b6e2f51ef2985fee21ccfff617b115David Lawrence dns_acl_detach(&zone->notify_acl);
e086935752b6e2f51ef2985fee21ccfff617b115David Lawrence if (zone->query_acl != NULL)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_acl_detach(&zone->query_acl);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (zone->xfr_acl != NULL)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_acl_detach(&zone->xfr_acl);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (dns_name_dynamic(&zone->origin))
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_name_free(&zone->origin, zone->mctx);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (zone->ssutable != NULL)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_ssutable_detach(&zone->ssutable);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson /* last stuff */
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington DESTROYLOCK(&zone->lock);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington isc_refcount_destroy(&zone->erefs);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington zone->magic = 0;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington mctx = zone->mctx;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington isc_mem_put(mctx, zone, sizeof(*zone));
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington isc_mem_detach(&mctx);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington}
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington/*
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington * Single shot.
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington */
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellingtonvoid
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellingtondns_zone_setclass(dns_zone_t *zone, dns_rdataclass_t rdclass) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(DNS_ZONE_VALID(zone));
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(rdclass != dns_rdataclass_none);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
d981ca645597116d227a48bf37cc5edc061c854dBob Halley /*
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews * Test and set.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews LOCK_ZONE(zone);
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews REQUIRE(zone->rdclass == dns_rdataclass_none ||
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff zone->rdclass == rdclass);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->rdclass = rdclass;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNLOCK_ZONE(zone);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews}
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsdns_rdataclass_t
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewsdns_zone_getclass(dns_zone_t *zone){
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews return (zone->rdclass);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews}
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsvoid
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencedns_zone_setnotifytype(dns_zone_t *zone, dns_notifytype_t notifytype) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews LOCK_ZONE(zone);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews zone->notifytype = notifytype;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews UNLOCK_ZONE(zone);
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews}
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews/*
19d365e4448f1782611280b020987988b7ac3210Mark Andrews * Single shot.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsvoid
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrewsdns_zone_settype(dns_zone_t *zone, dns_zonetype_t type) {
228c679d7a269423019f7c528db92e855f08240bMark Andrews
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
228c679d7a269423019f7c528db92e855f08240bMark Andrews REQUIRE(type != dns_zone_none);
228c679d7a269423019f7c528db92e855f08240bMark Andrews
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington /*
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington * Test and set.
228c679d7a269423019f7c528db92e855f08240bMark Andrews */
228c679d7a269423019f7c528db92e855f08240bMark Andrews LOCK_ZONE(zone);
228c679d7a269423019f7c528db92e855f08240bMark Andrews REQUIRE(zone->type == dns_zone_none || zone->type == type);
c51f2c9a529b2130a8058cb7a32bd284fa6cfdb9Mark Andrews zone->type = type;
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington UNLOCK_ZONE(zone);
228c679d7a269423019f7c528db92e855f08240bMark Andrews}
228c679d7a269423019f7c528db92e855f08240bMark Andrews
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrewsstatic void
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrewszone_freedbargs(dns_zone_t *zone) {
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews unsigned int i;
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews /* Free the old database argument list. */
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews if (zone->db_argv != NULL) {
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews for (i = 0; i < zone->db_argc; i++)
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews isc_mem_free(zone->mctx, zone->db_argv[i]);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews isc_mem_put(zone->mctx, zone->db_argv,
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews zone->db_argc * sizeof(*zone->db_argv));
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews }
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews zone->db_argc = 0;
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews zone->db_argv = NULL;
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews}
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrewsisc_result_t
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrewsdns_zone_setdbtype(dns_zone_t *zone,
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews unsigned int dbargc, const char * const *dbargv) {
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews isc_result_t result = ISC_R_SUCCESS;
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews char **new = NULL;
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews unsigned int i;
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews
b8e2e5dd861bdc72a5496a745a85f8761bef7dc4Tinderbox User REQUIRE(DNS_ZONE_VALID(zone));
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington REQUIRE(dbargc >= 1);
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington REQUIRE(dbargv != NULL);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington LOCK_ZONE(zone);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews /* Set up a new database argument list. */
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews new = isc_mem_get(zone->mctx, dbargc * sizeof(*new));
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (new == NULL)
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews goto nomem;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews for (i = 0; i < dbargc; i++)
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews new[i] = NULL;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews for (i = 0; i < dbargc; i++) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews new[i] = isc_mem_strdup(zone->mctx, dbargv[i]);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (new[i] == NULL)
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews goto nomem;
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews }
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews /* Free the old list. */
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews zone_freedbargs(zone);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews zone->db_argc = dbargc;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews zone->db_argv = new;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews result = ISC_R_SUCCESS;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto unlock;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews nomem:
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (new != NULL) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews for (i = 0; i < dbargc; i++) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (zone->db_argv[i] != NULL)
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews isc_mem_free(zone->mctx, new[i]);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mem_put(zone->mctx, new,
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews dbargc * sizeof(*new));
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews }
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews }
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews result = ISC_R_NOMEMORY;
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews unlock:
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews UNLOCK_ZONE(zone);
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews return (result);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrewsvoid
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrewsdns_zone_setview(dns_zone_t *zone, dns_view_t *view) {
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff LOCK_ZONE(zone);
7d8cdd869ed2162a5befda7cc1600136110f54d6Mark Andrews if (zone->view != NULL)
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews dns_view_weakdetach(&zone->view);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley dns_view_weakattach(view, &zone->view);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNLOCK_ZONE(zone);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsdns_view_t *
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsdns_zone_getview(dns_zone_t *zone) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews return (zone->view);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews}
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsisc_result_t
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsdns_zone_setorigin(dns_zone_t *zone, dns_name_t *origin) {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews isc_result_t result;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews REQUIRE(origin != NULL);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews LOCK_ZONE(zone);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews if (dns_name_dynamic(&zone->origin)) {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews dns_name_free(&zone->origin, zone->mctx);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews dns_name_init(&zone->origin, NULL);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews }
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews result = dns_name_dup(origin, zone->mctx, &zone->origin);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews UNLOCK_ZONE(zone);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews return (result);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews}
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsstatic isc_result_t
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsdns_zone_setstring(dns_zone_t *zone, char **field, const char *value) {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews char *copy;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews if (value != NULL) {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews copy = isc_mem_strdup(zone->mctx, value);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews if (copy == NULL)
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews return (ISC_R_NOMEMORY);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews } else {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews copy = NULL;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews }
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews if (*field != NULL)
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews isc_mem_free(zone->mctx, *field);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews *field = copy;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews return (ISC_R_SUCCESS);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews}
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffisc_result_t
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halleydns_zone_setfile(dns_zone_t *zone, const char *file) {
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halley isc_result_t result = ISC_R_SUCCESS;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(DNS_ZONE_VALID(zone));
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence LOCK_ZONE(zone);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews result = dns_zone_setstring(zone, &zone->masterfile, file);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (result == ISC_R_SUCCESS)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence result = default_journal(zone);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews UNLOCK_ZONE(zone);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (result);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrewsconst char *
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffdns_zone_getfile(dns_zone_t *zone) {
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
f8aae502686e2448c48f56697c212a50e2a1cbaeAndreas Gustafsson
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (zone->masterfile);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsstatic isc_result_t
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrewsdefault_journal(dns_zone_t *zone) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t result;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char *journal;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff REQUIRE(DNS_ZONE_VALID(zone));
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(LOCKED_ZONE(zone));
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews if (zone->masterfile != NULL) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews /* Calculate string length including '\0'. */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews int len = strlen(zone->masterfile) + sizeof(".jnl");
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews journal = isc_mem_allocate(zone->mctx, len);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (journal == NULL)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (ISC_R_NOMEMORY);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence strcpy(journal, zone->masterfile);
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson strcat(journal, ".jnl");
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson } else {
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson journal = NULL;
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson }
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halley result = dns_zone_setstring(zone, &zone->journal, journal);
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson if (journal != NULL)
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson isc_mem_free(zone->mctx, journal);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff return (result);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson}
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Huntisc_result_t
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Huntdns_zone_setjournal(dns_zone_t *zone, const char *journal) {
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson isc_result_t result = ISC_R_SUCCESS;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews LOCK_ZONE(zone);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence result = dns_zone_setstring(zone, &zone->journal, journal);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence UNLOCK_ZONE(zone);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson return (result);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson}
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafssonchar *
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Huntdns_zone_getjournal(dns_zone_t *zone) {
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt REQUIRE(DNS_ZONE_VALID(zone));
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt return (zone->journal);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson}
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt/*
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt * Return true iff the zone is "dynamic", in the sense that the zone's
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * master file (if any) is written by the server, rather than being
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * updated manually and read by the server.
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halley *
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * This is true for slave zones, stub zones, and zones that allow
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * dynamic updates either by having an update policy ("ssutable")
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * or an "allow-update" ACL with a value other than exactly "{ none; }".
d981ca645597116d227a48bf37cc5edc061c854dBob Halley */
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrewsstatic isc_boolean_t
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrewszone_isdynamic(dns_zone_t *zone) {
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_TF(zone->type == dns_zone_slave ||
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->type == dns_zone_stub ||
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews (!zone->update_disabled && zone->ssutable != NULL) ||
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews (!zone->update_disabled && zone->update_acl != NULL &&
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ! (zone->update_acl->length == 1 &&
19d365e4448f1782611280b020987988b7ac3210Mark Andrews zone->update_acl->elements[0].negative == ISC_TRUE
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews &&
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington zone->update_acl->elements[0].type ==
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_aclelementtype_any))));
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews}
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewszone_load(dns_zone_t *zone, unsigned int flags) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t result;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews isc_time_t now;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews isc_time_t loadtime, filetime;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_db_t *db = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews LOCK_ZONE(zone);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff TIME_NOW(&now);
7d8cdd869ed2162a5befda7cc1600136110f54d6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews INSIST(zone->type != dns_zone_none);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADING)) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = ISC_R_SUCCESS;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto cleanup;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (zone->db != NULL && zone->masterfile == NULL) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff /*
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews * The zone has no master file configured, but it already
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * has a database. It could be the built-in
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews * version.bind. CH zone, a zone with a persistent
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * database being reloaded, or maybe a zone that
19d365e4448f1782611280b020987988b7ac3210Mark Andrews * used to have a master file but whose configuration
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews * was changed so that it no longer has one. Do nothing.
19d365e4448f1782611280b020987988b7ac3210Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = ISC_R_SUCCESS;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto cleanup;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews }
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (zone->db != NULL && zone_isdynamic(zone)) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * This is a slave, stub, or dynamically updated
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * zone being reloaded. Do nothing - the database
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews * we already have is guaranteed to be up-to-date.
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews */
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews result = ISC_R_SUCCESS;
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews goto cleanup;
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews }
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews /*
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews * Don't do the load if the file that stores the zone is older
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * than the last time the zone was loaded. If the zone has not
d981ca645597116d227a48bf37cc5edc061c854dBob Halley * been loaded yet, zone->loadtime will be the epoch.
d981ca645597116d227a48bf37cc5edc061c854dBob Halley */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (zone->masterfile != NULL && ! isc_time_isepoch(&zone->loadtime)) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff /*
d981ca645597116d227a48bf37cc5edc061c854dBob Halley * The file is already loaded. If we are just doing a
d981ca645597116d227a48bf37cc5edc061c854dBob Halley * "rndc reconfig", we are done.
d981ca645597116d227a48bf37cc5edc061c854dBob Halley */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if ((flags & DNS_ZONELOADFLAG_NOSTAT) != 0) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley result = ISC_R_SUCCESS;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley goto cleanup;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (! DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HASINCLUDE)) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley result = isc_file_getmodtime(zone->masterfile,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews &filetime);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (result == ISC_R_SUCCESS &&
d981ca645597116d227a48bf37cc5edc061c854dBob Halley isc_time_compare(&filetime, &zone->loadtime) < 0) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley dns_zone_log(zone, ISC_LOG_DEBUG(1),
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews "skipping load: master file older "
d981ca645597116d227a48bf37cc5edc061c854dBob Halley "than last load");
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews result = ISC_R_SUCCESS;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley goto cleanup;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley dns_zone_log(zone, ISC_LOG_DEBUG(1), "starting load");
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley /*
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews * Store the current time before the zone is loaded, so that if the
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley * file changes between the time of the load and the time that
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley * zone->loadtime is set, then the file will still be reloaded
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley * the next time dns_zone_load is called.
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley */
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley TIME_NOW(&loadtime);
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley INSIST(zone->db_argc >= 1);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews result = dns_db_create(zone->mctx, zone->db_argv[0],
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley &zone->origin, (zone->type == dns_zone_stub) ?
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley dns_dbtype_stub : dns_dbtype_zone,
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley zone->rdclass,
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews zone->db_argc - 1, zone->db_argv + 1,
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews &db);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews if (result != ISC_R_SUCCESS) {
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley dns_zone_log(zone, ISC_LOG_ERROR,
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley "loading zone: creating database: %s",
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley isc_result_totext(result));
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley goto cleanup;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (! dns_db_ispersistent(db)) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (zone->masterfile != NULL) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews result = zone_startload(db, zone, loadtime);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews } else {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews result = DNS_R_NOMASTERFILE;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (zone->type == dns_zone_master) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_zone_log(zone, ISC_LOG_ERROR,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews "loading zone: "
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews "no master file configured");
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews goto cleanup;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_zone_log(zone, ISC_LOG_INFO, "loading zone: "
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews "no master file configured: continuing");
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (result == DNS_R_CONTINUE) {
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADING);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff result = ISC_R_SUCCESS;
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff goto cleanup;
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews }
471e0563c7965c556c759775882cd3448dae78eaMark Andrews
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff result = zone_postload(zone, db, loadtime, result);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff cleanup:
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence UNLOCK_ZONE(zone);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff if (db != NULL)
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews dns_db_detach(&db);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff return (result);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff}
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graffisc_result_t
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graffdns_zone_load(dns_zone_t *zone) {
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff return (zone_load(zone, 0));
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff}
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrenceisc_result_t
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graffdns_zone_loadnew(dns_zone_t *zone) {
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff return (zone_load(zone, DNS_ZONELOADFLAG_NOSTAT));
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff}
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff
29747dfe5e073a299b3681e01f5c55540f8bfed7Mark Andrewsstatic void
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graffzone_gotreadhandle(isc_task_t *task, isc_event_t *event) {
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff dns_load_t *load = event->ev_arg;
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff isc_result_t result = ISC_R_SUCCESS;
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews unsigned int options;
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews REQUIRE(DNS_LOAD_VALID(load));
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington result = ISC_R_CANCELED;
9be230cfca126bea6b666f506a230ffcdfba60dbMark Andrews isc_event_free(&event);
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews if (result == ISC_R_CANCELED)
beed6a0e226fbd0c18c19a3e341a2003cba020a5Mark Andrews goto fail;
beed6a0e226fbd0c18c19a3e341a2003cba020a5Mark Andrews
beed6a0e226fbd0c18c19a3e341a2003cba020a5Mark Andrews options = DNS_MASTER_ZONE;
9be230cfca126bea6b666f506a230ffcdfba60dbMark Andrews if (load->zone->type == dns_zone_slave)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington options |= DNS_MASTER_SLAVE;
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington result = dns_master_loadfileinc(load->zone->masterfile,
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington dns_db_origin(load->db),
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington dns_db_origin(load->db),
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington load->zone->rdclass,
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews options,
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews &load->callbacks, task,
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews zone_loaddone, load,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff &load->zone->lctx, load->zone->mctx);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (result != ISC_R_SUCCESS && result != DNS_R_CONTINUE &&
471e0563c7965c556c759775882cd3448dae78eaMark Andrews result != DNS_R_SEENINCLUDE)
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff goto fail;
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews return;
cc083bb7031c04d57cbad41b2f5a796a4fd1865cMark Andrews
471e0563c7965c556c759775882cd3448dae78eaMark Andrews fail:
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews zone_loaddone(load, result);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews}
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson
8c962eba3d6faebc008806ebb6bb9d08089118e9Andreas Gustafssonstatic void
8c962eba3d6faebc008806ebb6bb9d08089118e9Andreas Gustafssonzone_gotwritehandle(isc_task_t *task, isc_event_t *event) {
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson const char me[] = "zone_gotwritehandle";
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson dns_zone_t *zone = event->ev_arg;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson isc_result_t result = ISC_R_SUCCESS;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson dns_dbversion_t *version = NULL;
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews INSIST(task == zone->task);
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson ENTER;
8c962eba3d6faebc008806ebb6bb9d08089118e9Andreas Gustafsson
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0)
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson result = ISC_R_CANCELED;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson isc_event_free(&event);
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson if (result == ISC_R_CANCELED)
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson goto fail;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson LOCK(&zone->lock);
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson dns_db_currentversion(zone->db, &version);
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson result = dns_master_dumpinc(zone->mctx, zone->db, version,
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews &dns_master_style_default,
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews zone->masterfile, zone->task,
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson dump_done, zone, &zone->dctx);
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson dns_db_closeversion(zone->db, &version, ISC_FALSE);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence UNLOCK(&zone->lock);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (result != DNS_R_CONTINUE)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence goto fail;
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews return;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fail:
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dump_done(zone, result);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_result_t
2383eb527269d333df4222da20e4b422c3662daaEvan Huntzone_startload(dns_db_t *db, dns_zone_t *zone, isc_time_t loadtime) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_load_t *load;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t result;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t tresult;
fe47f41b13620bfafc4f8cf65d5df24f1e568764Bob Halley
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (zone->zmgr != NULL && zone->db != NULL && zone->task != NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews load = isc_mem_get(zone->mctx, sizeof(*load));
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (load == NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_NOMEMORY);
fe47f41b13620bfafc4f8cf65d5df24f1e568764Bob Halley
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews load->mctx = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews load->zone = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews load->db = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews load->loadtime = loadtime;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews load->magic = LOAD_MAGIC;
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt isc_mem_attach(zone->mctx, &load->mctx);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone_iattach(zone, &load->zone);
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt dns_db_attach(db, &load->db);
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt dns_rdatacallbacks_init(&load->callbacks);
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt result = dns_db_beginload(db, &load->callbacks.add,
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt &load->callbacks.add_private);
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt if (result != ISC_R_SUCCESS)
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt goto cleanup;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = zonemgr_getio(zone->zmgr, ISC_TRUE, zone->task,
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt zone_gotreadhandle, load,
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt &zone->readio);
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt if (result != ISC_R_SUCCESS) {
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt tresult = dns_db_endload(load->db,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews &load->callbacks.add_private);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result == ISC_R_SUCCESS)
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews result = tresult;
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews goto cleanup;
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews } else
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews result = DNS_R_CONTINUE;
c2b217ea1135f070da3856d3276895b3026e932aMark Andrews } else if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_MANYERRORS)) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdatacallbacks_t callbacks;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int options;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt dns_rdatacallbacks_init(&callbacks);
9a36fb86f5019f25705d25ea729d03fcf8ecaa95Mark Andrews result = dns_db_beginload(db, &callbacks.add,
9a36fb86f5019f25705d25ea729d03fcf8ecaa95Mark Andrews &callbacks.add_private);
9a36fb86f5019f25705d25ea729d03fcf8ecaa95Mark Andrews if (result != ISC_R_SUCCESS)
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt return (result);
9a36fb86f5019f25705d25ea729d03fcf8ecaa95Mark Andrews options = DNS_MASTER_MANYERRORS|DNS_MASTER_ZONE;
9a36fb86f5019f25705d25ea729d03fcf8ecaa95Mark Andrews if (zone->type == dns_zone_slave)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews options |= DNS_MASTER_SLAVE;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = dns_master_loadfile(zone->masterfile, &zone->origin,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews &zone->origin, zone->rdclass,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews options, &callbacks, zone->mctx);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews tresult = dns_db_endload(db, &callbacks.add_private);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result == ISC_R_SUCCESS)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = tresult;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dns_db_load(db, zone->masterfile);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt return (result);
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt cleanup:
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt load->magic = 0;
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt dns_db_detach(&load->db);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews zone_idetach(&load->zone);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_detach(&load->mctx);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mem_put(zone->mctx, load, sizeof(*load));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (result);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewszone_postload(dns_zone_t *zone, dns_db_t *db, isc_time_t loadtime,
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews isc_result_t result)
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews{
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews unsigned int soacount = 0;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews unsigned int nscount = 0;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews isc_uint32_t serial, refresh, retry, expire, minimum;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews isc_time_t now;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_boolean_t needdump = ISC_FALSE;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews TIME_NOW(&now);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
f0ff273b530afa730025e1c5ad311950f7ff4328Mark Andrews /*
f0ff273b530afa730025e1c5ad311950f7ff4328Mark Andrews * Initiate zone transfer? We may need a error code that
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews * indicates that the "permanent" form does not exist.
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews * XXX better error feedback to log.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (zone->type == dns_zone_slave ||
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence zone->type == dns_zone_stub) {
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (result == ISC_R_FILENOTFOUND)
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_zone_log(zone, ISC_LOG_DEBUG(1),
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence "no master file");
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence else if (result != DNS_R_NOMASTERFILE)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_zone_log(zone, ISC_LOG_ERROR,
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews "loading master file %s: %s",
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews zone->masterfile,
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_result_totext(result));
ad493ef9ddb5a3e78e9d99f57abe75552f36a8f4Andreas Gustafsson } else
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence dns_zone_log(zone, ISC_LOG_ERROR,
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews "loading master file %s: %s",
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews zone->masterfile,
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_result_totext(result));
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews goto cleanup;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews }
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_zone_log(zone, ISC_LOG_DEBUG(2),
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews "number of nodes in database: %u",
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_db_nodecount(db));
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews zone->loadtime = loadtime;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_zone_log(zone, ISC_LOG_DEBUG(1), "loaded");
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (result == DNS_R_SEENINCLUDE)
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HASINCLUDE);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews else
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HASINCLUDE);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews /*
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews * Apply update log, if any, on initial load.
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews */
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (zone->journal != NULL &&
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews ! DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOMERGE) &&
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ! DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
a5a30ae7c0636a57d8575acb98fdf906c0ea818fMark Andrews {
a5a30ae7c0636a57d8575acb98fdf906c0ea818fMark Andrews result = dns_journal_rollforward(zone->mctx, db,
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews zone->journal);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND &&
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews result != DNS_R_UPTODATE && result != DNS_R_NOJOURNAL &&
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews result != ISC_R_RANGE) {
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_zone_log(zone, ISC_LOG_ERROR,
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews "journal rollforward failed: %s",
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews dns_result_totext(result));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto cleanup;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result == ISC_R_NOTFOUND || result == ISC_R_RANGE) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff dns_zone_log(zone, ISC_LOG_ERROR,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "journal rollforward failed: "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "journal out of sync with zone");
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto cleanup;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_zone_log(zone, ISC_LOG_DEBUG(1),
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence "journal rollforward completed "
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews "successfully: %s",
c707e2b9861dfa3f86b3520b9c3630db70cb020cEvan Hunt dns_result_totext(result));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result == ISC_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews needdump = ISC_TRUE;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence /*
bfb2a81b65579882a80855c279cedc45aebd62e8Mark Andrews * Obtain ns and soa counts for top of zone.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews nscount = 0;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews soacount = 0;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt INSIST(db != NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = zone_get_from_db(db, &zone->origin, &nscount,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews &soacount, &serial, &refresh, &retry,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff &expire, &minimum);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result != ISC_R_SUCCESS) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_zone_log(zone, ISC_LOG_ERROR,
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt "could not find NS and/or SOA records");
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt }
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont /*
7712d1660a308ec3de17f1ddbbf801eb0d663f3eEvan Hunt * Master / Slave / Stub zones require both NS and SOA records at
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont * the top of the zone.
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont */
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont switch (zone->type) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont case dns_zone_master:
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont case dns_zone_slave:
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont case dns_zone_stub:
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (soacount != 1) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_zone_log(zone, ISC_LOG_ERROR,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont "has %d SOA records", soacount);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont result = DNS_R_BADZONE;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (nscount == 0) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_zone_log(zone, ISC_LOG_ERROR,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont "has no NS records");
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont result = DNS_R_BADZONE;
7712d1660a308ec3de17f1ddbbf801eb0d663f3eEvan Hunt }
7712d1660a308ec3de17f1ddbbf801eb0d663f3eEvan Hunt if (result != ISC_R_SUCCESS)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont goto cleanup;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zone->db != NULL) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (!isc_serial_ge(serial, zone->serial)) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_zone_log(zone, ISC_LOG_ERROR,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont "zone serial has gone backwards");
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->serial = serial;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->refresh = RANGE(refresh,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->minrefresh, zone->maxrefresh);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->retry = RANGE(retry,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->minretry, zone->maxretry);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->expire = RANGE(expire, zone->refresh + zone->retry,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont DNS_MAX_EXPIRE);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->minimum = minimum;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zone->type == dns_zone_slave ||
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->type == dns_zone_stub) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_time_t t;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont result = isc_file_getmodtime(zone->journal, &t);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (result != ISC_R_SUCCESS)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont result = isc_file_getmodtime(zone->masterfile,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont &t);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (result == ISC_R_SUCCESS)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont DNS_ZONE_TIME_ADD(&t, zone->expire,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont &zone->expiretime);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont else
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont DNS_ZONE_TIME_ADD(&now, zone->retry,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont &zone->expiretime);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews zone->refreshtime = now;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont break;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont default:
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont UNEXPECTED_ERROR(__FILE__, __LINE__,
7712d1660a308ec3de17f1ddbbf801eb0d663f3eEvan Hunt "unexpected zone type %d", zone->type);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont result = ISC_R_UNEXPECTED;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont goto cleanup;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont#if 0
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont /* destroy notification example. */
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_event_t *e = isc_event_allocate(zone->mctx, NULL,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont DNS_EVENT_DBDESTROYED,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_zonemgr_dbdestroyed,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont sizeof(isc_event_t));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_db_ondestroy(db, zone->task, &e);
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont#endif
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zone->db != NULL) {
2383eb527269d333df4222da20e4b422c3662daaEvan Hunt result = zone_replacedb(zone, db, ISC_FALSE);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (result != ISC_R_SUCCESS)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont goto cleanup;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont } else {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_db_attach(db, &zone->db);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont DNS_ZONE_SETFLAG(zone,
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont DNS_ZONEFLG_LOADED|DNS_ZONEFLG_NEEDNOTIFY);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont result = ISC_R_SUCCESS;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (needdump)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone_needdump(zone, DNS_DUMP_DELAY);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zone->task != NULL)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone_settimer(zone, &now);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (! dns_db_ispersistent(db))
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_zone_log(zone, ISC_LOG_INFO, "loaded serial %u", zone->serial);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (result);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont cleanup:
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zone->type == dns_zone_slave ||
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->type == dns_zone_stub) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zone->journal != NULL)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone_saveunique(zone, zone->journal, "jn-XXXXXXXX");
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zone->masterfile != NULL)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone_saveunique(zone, zone->masterfile, "db-XXXXXXXX");
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont /* Mark the zone for immediate refresh. */
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zone->refreshtime = now;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont result = ISC_R_SUCCESS;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (result);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
2383eb527269d333df4222da20e4b422c3662daaEvan Huntstatic isc_boolean_t
ab973ec40cc7a79bbf930085b1293824caf73e7fFrancis Dupontexit_check(dns_zone_t *zone) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont REQUIRE(LOCKED_ZONE(zone));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN) &&
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zone->irefs == 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * DNS_ZONEFLG_SHUTDOWN can only be set if erefs == 0.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(isc_refcount_current(&zone->erefs) == 0);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_TRUE);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_FALSE);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewszone_count_ns_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int *nscount)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews{
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_result_t result;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int count;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdataset_t rdataset;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews REQUIRE(nscount != NULL);
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews dns_rdataset_init(&rdataset);
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews result = dns_db_findrdataset(db, node, version, dns_rdatatype_ns,
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews dns_rdatatype_none, 0, &rdataset, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result == ISC_R_NOTFOUND) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *nscount = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = ISC_R_SUCCESS;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto invalidate_rdataset;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews else if (result != ISC_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto invalidate_rdataset;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff count = 0;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence result = dns_rdataset_first(&rdataset);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews while (result == ISC_R_SUCCESS) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews count++;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dns_rdataset_next(&rdataset);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdataset_disassociate(&rdataset);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *nscount = count;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = ISC_R_SUCCESS;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt invalidate_rdataset:
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdataset_invalidate(&rdataset);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (result);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellingtonstatic isc_result_t
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellingtonzone_load_soa_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington unsigned int *soacount,
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington isc_uint32_t *serial, isc_uint32_t *refresh,
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington isc_uint32_t *retry, isc_uint32_t *expire,
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington isc_uint32_t *minimum)
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington{
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington isc_result_t result;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington unsigned int count;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington dns_rdataset_t rdataset;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington dns_rdata_t rdata = DNS_RDATA_INIT;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington dns_rdata_soa_t soa;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdataset_init(&rdataset);
26b0f58b6c4d65bc8b131debf40b8c376c2978bfBob Halley result = dns_db_findrdataset(db, node, version, dns_rdatatype_soa,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdatatype_none, 0, &rdataset, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result != ISC_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto invalidate_rdataset;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence count = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dns_rdataset_first(&rdataset);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews while (result == ISC_R_SUCCESS) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdata_init(&rdata);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdataset_current(&rdataset, &rdata);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff count++;
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews if (count == 1) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dns_rdata_tostruct(&rdata, &soa, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews RUNTIME_CHECK(result == ISC_R_SUCCESS);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = dns_rdataset_next(&rdataset);
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews dns_rdata_reset(&rdata);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdataset_disassociate(&rdataset);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (soacount != NULL)
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews *soacount = count;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (count > 0) {
1c3191528684f3dd93ebb122298c2f8ebfc6d397Mark Andrews if (serial != NULL)
34b394b43e2207e8f8f3703f0402422121455638David Lawrence *serial = soa.serial;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (refresh != NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *refresh = soa.refresh;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (retry != NULL)
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence *retry = soa.retry;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (expire != NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *expire = soa.expire;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (minimum != NULL)
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff *minimum = soa.minimum;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson }
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson result = ISC_R_SUCCESS;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
34b394b43e2207e8f8f3703f0402422121455638David Lawrence invalidate_rdataset:
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_rdataset_invalidate(&rdataset);
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (result);
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson/*
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson * zone must be locked.
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson */
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafssonstatic isc_result_t
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafssonzone_get_from_db(dns_db_t *db, dns_name_t *origin, unsigned int *nscount,
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson unsigned int *soacount, isc_uint32_t *serial,
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson isc_uint32_t *refresh, isc_uint32_t *retry,
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson isc_uint32_t *expire, isc_uint32_t *minimum)
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson{
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews dns_dbversion_t *version;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t result;
2da53322f30495e9bcf30f3776def2da329d3343Mark Andrews isc_result_t answer = ISC_R_SUCCESS;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_dbnode_t *node;
035504dbd8ca5949e8380b860873b3385a4e61e5Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(db != NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(origin != NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews version = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_db_currentversion(db, &version);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews node = NULL;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_db_findnode(db, origin, ISC_FALSE, &node);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews if (result != ISC_R_SUCCESS) {
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews answer = result;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews goto closeversion;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews }
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews if (nscount != NULL) {
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = zone_count_ns_rr(db, node, version, nscount);
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews if (result != ISC_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews answer = result;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
035504dbd8ca5949e8380b860873b3385a4e61e5Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (soacount != NULL || serial != NULL || refresh != NULL
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews || retry != NULL || expire != NULL || minimum != NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = zone_load_soa_rr(db, node, version, soacount,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews serial, refresh, retry, expire,
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson minimum);
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson if (result != ISC_R_SUCCESS)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence answer = result;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson }
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson dns_db_detachnode(db, &node);
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson closeversion:
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson dns_db_closeversion(db, &version, ISC_FALSE);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews return (answer);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews}
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrewsvoid
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrewsdns_zone_attach(dns_zone_t *source, dns_zone_t **target) {
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews REQUIRE(DNS_ZONE_VALID(source));
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews REQUIRE(target != NULL && *target == NULL);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_refcount_increment(&source->erefs, NULL);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews *target = source;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews}
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencevoid
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellingtondns_zone_detach(dns_zone_t **zonep) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_zone_t *zone;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews unsigned int refs;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt isc_boolean_t free_now = ISC_FALSE;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews zone = *zonep;
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews isc_refcount_decrement(&zone->erefs, &refs);
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence if (refs == 0) {
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence LOCK_ZONE(zone);
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence /*
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence * We just detached the last external reference.
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence */
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews if (zone->task != NULL) {
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence /*
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence * This zone is being managed. Post
cbef026164ceabccb2e85403434b722d77f7b5eeMark Andrews * its control event and let it clean
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews * up synchronously in the context of
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * its task.
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews */
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews isc_event_t *ev = &zone->ctlevent;
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews isc_task_send(zone->task, &ev);
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews } else {
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews /*
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence * This zone is not being managed; it has
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence * no task and can have no outstanding
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence * events. Free it immediately.
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence */
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence /*
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence * Unmanaged zones should not have non-null views;
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews * we have no way of detaching from the view here
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews * without causing deadlock because this code is called
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews * with the view already locked.
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews */
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews INSIST(zone->view == NULL);
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews free_now = ISC_TRUE;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence UNLOCK_ZONE(zone);
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *zonep = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (free_now)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone_free(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsvoid
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_iattach(dns_zone_t *source, dns_zone_t **target) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(source));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(target != NULL && *target == NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK_ZONE(source);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone_iattach(source, target);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews UNLOCK_ZONE(source);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic void
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewszone_iattach(dns_zone_t *source, dns_zone_t **target) {
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews /*
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews * 'source' locked by caller.
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews */
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews REQUIRE(LOCKED_ZONE(source));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(source));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(target != NULL && *target == NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(source->irefs + isc_refcount_current(&source->erefs) > 0);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews source->irefs++;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(source->irefs != 0);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *target = source;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic void
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewszone_idetach(dns_zone_t **zonep) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff dns_zone_t *zone;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff /*
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * 'zone' locked by caller.
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zone = *zonep;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(LOCKED_ZONE(*zonep));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *zonep = NULL;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(zone->irefs > 0);
d4422ec231e58b60693620cf298276e47f04f0ecMark Andrews zone->irefs--;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(zone->irefs + isc_refcount_current(&zone->erefs) > 0);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsvoid
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_idetach(dns_zone_t **zonep) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_zone_t *zone;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_boolean_t free_needed;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone = *zonep;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *zonep = NULL;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(zone->irefs > 0);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->irefs--;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews free_needed = exit_check(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews UNLOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (free_needed)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone_free(zone);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsisc_mem_t *
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_getmctx(dns_zone_t *zone) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (zone->mctx);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zonemgr_t *
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffdns_zone_getmgr(dns_zone_t *zone) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (zone->zmgr);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffvoid
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_setflag(dns_zone_t *zone, unsigned int flags, isc_boolean_t value) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (value)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews DNS_ZONE_SETFLAG(zone, flags);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews else
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews DNS_ZONE_CLRFLAG(zone, flags);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews UNLOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsvoid
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_setoption(dns_zone_t *zone, unsigned int option, isc_boolean_t value)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence{
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (value)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff zone->options |= option;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews else
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->options &= ~option;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence UNLOCK_ZONE(zone);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceunsigned int
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencedns_zone_getoptions(dns_zone_t *zone) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (zone->options);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceisc_result_t
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_setxfrsource4(dns_zone_t *zone, isc_sockaddr_t *xfrsource) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
df8c9ee4819c97089664ccc035eb2aa7569034fdDavid Lawrence LOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->xfrsource4 = *xfrsource;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews UNLOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (ISC_R_SUCCESS);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsisc_sockaddr_t *
add4043305ca411202ed9cf1929a4179016515ceBrian Wellingtondns_zone_getxfrsource4(dns_zone_t *zone) {
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (&zone->xfrsource4);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsisc_result_t
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_setxfrsource6(dns_zone_t *zone, isc_sockaddr_t *xfrsource) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK_ZONE(zone);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence zone->xfrsource6 = *xfrsource;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence UNLOCK_ZONE(zone);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington return (ISC_R_SUCCESS);
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington}
984d2bb9e5aaca7c946bc78354c12b1dd75ed00aMukund Sivaraman
984d2bb9e5aaca7c946bc78354c12b1dd75ed00aMukund Sivaramanisc_sockaddr_t *
984d2bb9e5aaca7c946bc78354c12b1dd75ed00aMukund Sivaramandns_zone_getxfrsource6(dns_zone_t *zone) {
984d2bb9e5aaca7c946bc78354c12b1dd75ed00aMukund Sivaraman REQUIRE(DNS_ZONE_VALID(zone));
984d2bb9e5aaca7c946bc78354c12b1dd75ed00aMukund Sivaraman return (&zone->xfrsource6);
984d2bb9e5aaca7c946bc78354c12b1dd75ed00aMukund Sivaraman}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceisc_result_t
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencedns_zone_setnotifysrc4(dns_zone_t *zone, isc_sockaddr_t *notifysrc) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington LOCK_ZONE(zone);
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews zone->notifysrc4 = *notifysrc;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews UNLOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (ISC_R_SUCCESS);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceisc_sockaddr_t *
add4043305ca411202ed9cf1929a4179016515ceBrian Wellingtondns_zone_getnotifysrc4(dns_zone_t *zone) {
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington REQUIRE(DNS_ZONE_VALID(zone));
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews return (&zone->notifysrc4);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsisc_result_t
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencedns_zone_setnotifysrc6(dns_zone_t *zone, isc_sockaddr_t *notifysrc) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(DNS_ZONE_VALID(zone));
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington LOCK_ZONE(zone);
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington zone->notifysrc6 = *notifysrc;
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews UNLOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (ISC_R_SUCCESS);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsisc_sockaddr_t *
add4043305ca411202ed9cf1929a4179016515ceBrian Wellingtondns_zone_getnotifysrc6(dns_zone_t *zone) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (&zone->notifysrc6);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceisc_result_t
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencedns_zone_setalsonotify(dns_zone_t *zone, isc_sockaddr_t *notify,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_uint32_t count)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews{
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_sockaddr_t *new;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(count == 0 || notify != NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (zone->notify != NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_mem_put(zone->mctx, zone->notify,
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->notifycnt * sizeof(*new));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->notify = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->notifycnt = 0;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (count != 0) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews new = isc_mem_get(zone->mctx, count * sizeof *new);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (new == NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews UNLOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (ISC_R_NOMEMORY);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews memcpy(new, notify, count * sizeof *new);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff zone->notify = new;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->notifycnt = count;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews UNLOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (ISC_R_SUCCESS);
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews}
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceisc_result_t
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencedns_zone_setmasters(dns_zone_t *zone, isc_sockaddr_t *masters,
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_uint32_t count)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence{
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater isc_result_t result;
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews result = dns_zone_setmasterswithkeys(zone, masters, NULL, count);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (result);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater}
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsisc_result_t
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsdns_zone_setmasterswithkeys(dns_zone_t *zone, isc_sockaddr_t *masters,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_name_t **keynames, isc_uint32_t count)
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater{
83ac7ce833930a5c6cb92ad9c04a58e775579e73Bob Halley isc_sockaddr_t *new;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_result_t result = ISC_R_SUCCESS;
83ac7ce833930a5c6cb92ad9c04a58e775579e73Bob Halley dns_name_t **newname;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews unsigned int i;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(DNS_ZONE_VALID(zone));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(count == 0 || masters != NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (keynames != NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews REQUIRE(count != 0);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK_ZONE(zone);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (zone->masters != NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_mem_put(zone->mctx, zone->masters,
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->masterscnt * sizeof(*new));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->masters = NULL;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (zone->masterkeynames != NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews for (i = 0; i < zone->masterscnt; i++) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (zone->masterkeynames[i] != NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_name_free(zone->masterkeynames[i],
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->mctx);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_mem_put(zone->mctx,
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->masterkeynames[i],
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff sizeof(dns_name_t));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->masterkeynames[i] = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson isc_mem_put(zone->mctx, zone->masterkeynames,
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->masterscnt * sizeof(dns_name_t *));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->masterkeynames = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zone->masterscnt = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence /*
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * If count == 0, don't allocate any space for masters or keynames
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * so internally, those pointers are NULL if count == 0
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (count == 0)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews goto unlock;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews /*
806c235ecf533b98d068b3f8df9d7abbe1e30cf9Mark Andrews * masters must countain count elements!
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews */
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews new = isc_mem_get(zone->mctx,
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews count * sizeof(isc_sockaddr_t));
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (new == NULL) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews result = ISC_R_NOMEMORY;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence goto unlock;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence }
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence memcpy(new, masters, count * sizeof(*new));
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews zone->masters = new;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews zone->masterscnt = count;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOMASTERS);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews /*
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * if keynames is non-NULL, it must contain count elements!
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
861cef13977886aca57ba95c2c93039b87bbbc43Brian Wellington if (keynames != NULL) {
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews newname = isc_mem_get(zone->mctx,
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews count * sizeof(dns_name_t *));
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews if (newname == NULL) {
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews result = ISC_R_NOMEMORY;
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews isc_mem_put(zone->mctx, zone->masters,
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews count * sizeof(*new));
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews goto unlock;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews }
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews for (i = 0; i < count; i++)
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews newname[i] = NULL;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews for (i = 0; i < count; i++) {
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews if (keynames[i] != NULL) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews newname[i] = isc_mem_get(zone->mctx,
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews sizeof(dns_name_t));
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (newname[i] == NULL)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews goto allocfail;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews dns_name_init(newname[i], NULL);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews result = dns_name_dup(keynames[i], zone->mctx,
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews newname[i]);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (result != ISC_R_SUCCESS) {
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews allocfail:
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews for (i = 0; i < count; i++)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (newname[i] != NULL)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews dns_name_free(
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater newname[i],
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews zone->mctx);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews isc_mem_put(zone->mctx, zone->masters,
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews count * sizeof(*new));
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews isc_mem_put(zone->mctx, newname,
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews count * sizeof(*newname));
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews goto unlock;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews zone->masterkeynames = newname;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews unlock:
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater UNLOCK_ZONE(zone);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (result);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews}
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsisc_result_t
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updaterdns_zone_getdb(dns_zone_t *zone, dns_db_t **dpb) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_result_t result = ISC_R_SUCCESS;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews LOCK_ZONE(zone);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (zone->db == NULL)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence result = DNS_R_NOTLOADED;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence else
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_db_attach(zone->db, dpb);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews UNLOCK_ZONE(zone);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews return (result);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews}
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews/*
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * Co-ordinates the starting of routine jobs.
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence */
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewsvoid
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewsdns_zone_maintenance(dns_zone_t *zone) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews const char me[] = "dns_zone_maintenance";
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence isc_time_t now;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews REQUIRE(DNS_ZONE_VALID(zone));
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews ENTER;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence LOCK_ZONE(zone);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews TIME_NOW(&now);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews zone_settimer(zone, &now);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews UNLOCK_ZONE(zone);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews}
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewsstatic inline isc_boolean_t
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrencewas_dumping(dns_zone_t *zone) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews isc_boolean_t dumping;
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews REQUIRE(LOCKED_ZONE(zone));
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews dumping = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING);
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (!dumping) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews isc_time_settoepoch(&zone->dumptime);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews }
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews return (dumping);
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence}
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewsstatic void
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewszone_maintenance(dns_zone_t *zone) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson const char me[] = "zone_maintenance";
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley isc_time_t now;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_result_t result;
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews isc_boolean_t dumping;
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley REQUIRE(DNS_ZONE_VALID(zone));
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley ENTER;
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley
ebd68da027cfa8da0fb536c3db11bb88292f41c7Andreas Gustafsson /*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Configuring the view of this zone may have
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff * failed, for example because the config file
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff * had a syntax error. In that case, the view
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff * adb or resolver, and we had better not try
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff * to do maintenance on it.
ebd68da027cfa8da0fb536c3db11bb88292f41c7Andreas Gustafsson */
ebd68da027cfa8da0fb536c3db11bb88292f41c7Andreas Gustafsson if (zone->view == NULL || zone->view->adb == NULL)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return;
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff TIME_NOW(&now);
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff /*
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff * Expire check.
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff */
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff switch (zone->type) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence case dns_zone_slave:
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff case dns_zone_stub:
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff LOCK_ZONE(zone);
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff if (isc_time_compare(&now, &zone->expiretime) >= 0 &&
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) {
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff zone_expire(zone);
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff zone->refreshtime = now;
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff }
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence UNLOCK_ZONE(zone);
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff break;
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff default:
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff break;
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff }
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff /*
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Up to date check.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews */
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews switch (zone->type) {
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews case dns_zone_slave:
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews case dns_zone_stub:
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH) &&
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_time_compare(&now, &zone->refreshtime) >= 0)
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff dns_zone_refresh(zone);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence break;
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff default:
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff break;
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff }
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff /*
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff * Do we need to consolidate the backing store?
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
f8aae502686e2448c48f56697c212a50e2a1cbaeAndreas Gustafsson switch (zone->type) {
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson case dns_zone_master:
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson case dns_zone_slave:
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence LOCK_ZONE(zone);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff if (zone->masterfile != NULL &&
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff isc_time_compare(&now, &zone->dumptime) >= 0 &&
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) &&
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP)) {
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson dumping = was_dumping(zone);
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson } else
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dumping = ISC_TRUE;
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff UNLOCK_ZONE(zone);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff if (!dumping) {
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff result = zone_dump(zone, ISC_TRUE); /* task locked */
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff if (result != ISC_R_SUCCESS)
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff dns_zone_log(zone, ISC_LOG_WARNING,
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson "dump failed: %s",
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson dns_result_totext(result));
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson }
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence break;
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson default:
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson break;
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson }
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Do we need to send out notify messages?
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 switch (zone->type) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 case dns_zone_master:
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 case dns_zone_slave:
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY))
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 zone_notify(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 break;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 default:
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 break;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 }
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 zone_settimer(zone, &now);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉}
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉void
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉dns_zone_markdirty(dns_zone_t *zone) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 LOCK_ZONE(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 zone_needdump(zone, DNS_DUMP_DELAY);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 UNLOCK_ZONE(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉}
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉void
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉dns_zone_expire(dns_zone_t *zone) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 REQUIRE(DNS_ZONE_VALID(zone));
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 LOCK_ZONE(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 zone_expire(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 UNLOCK_ZONE(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉}
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉static void
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉zone_expire(dns_zone_t *zone) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * 'zone' locked by caller.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 REQUIRE(LOCKED_ZONE(zone));
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 dns_zone_log(zone, ISC_LOG_WARNING, "expired");
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXPIRED);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 zone->refresh = DNS_ZONE_DEFAULTREFRESH;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 zone->retry = DNS_ZONE_DEFAULTRETRY;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 zone_unload(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉}
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉void
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉dns_zone_refresh(dns_zone_t *zone) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_interval_t i;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_uint32_t oldflags;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 REQUIRE(DNS_ZONE_VALID(zone));
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 return;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Set DNS_ZONEFLG_REFRESH so that there is only one refresh operation
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * in progress at a time.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 LOCK_ZONE(zone);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 oldflags = zone->flags;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if (zone->masterscnt == 0) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOMASTERS);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if ((oldflags & DNS_ZONEFLG_NOMASTERS) == 0)
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 dns_zone_log(zone, ISC_LOG_ERROR,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 "cannot refresh: no masters");
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 goto unlock;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 }
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if ((oldflags & (DNS_ZONEFLG_REFRESH|DNS_ZONEFLG_LOADING)) != 0)
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 goto unlock;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Set the next refresh time as if refresh check has failed.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Setting this to the retry time will do that. XXXMLG
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * If we are successful it will be reset using zone->refresh.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_interval_set(&i, isc_random_jitter(zone->retry, zone->retry / 4),
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 0);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_time_nowplusinterval(&zone->refreshtime, &i);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * When lacking user-specified timer values from the SOA,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * do exponential backoff of the retry time up to a
* maximum of six hours.
*/
if (! DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS))
zone->retry = ISC_MIN(zone->retry * 2, 6 * 3600);
zone->curmaster = 0;
/* initiate soa query */
queue_soa_query(zone);
unlock:
UNLOCK_ZONE(zone);
}
isc_result_t
dns_zone_flush(dns_zone_t *zone) {
isc_result_t result = ISC_R_SUCCESS;
isc_boolean_t dumping;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FLUSH);
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
zone->masterfile != NULL) {
result = ISC_R_ALREADYRUNNING;
dumping = was_dumping(zone);
} else
dumping = ISC_TRUE;
UNLOCK_ZONE(zone);
if (!dumping)
result = zone_dump(zone, ISC_FALSE); /* Unknown task. */
return (result);
}
isc_result_t
dns_zone_dump(dns_zone_t *zone) {
isc_result_t result = ISC_R_ALREADYRUNNING;
isc_boolean_t dumping;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
dumping = was_dumping(zone);
UNLOCK_ZONE(zone);
if (!dumping)
result = zone_dump(zone, ISC_FALSE); /* Unknown task. */
return (result);
}
static void
zone_needdump(dns_zone_t *zone, unsigned int delay) {
isc_time_t dumptime;
isc_time_t now;
/*
* 'zone' locked by caller
*/
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(LOCKED_ZONE(zone));
/*
* Do we have a place to dump to and are we loaded?
*/
if (zone->masterfile == NULL ||
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) == 0)
return;
TIME_NOW(&now);
/* add some noise */
DNS_ZONE_JITTER_ADD(&now, delay, &dumptime);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
if (isc_time_isepoch(&zone->dumptime) ||
isc_time_compare(&zone->dumptime, &dumptime) > 0)
zone->dumptime = dumptime;
if (zone->task != NULL)
zone_settimer(zone, &now);
}
static void
dump_done(void *arg, isc_result_t result) {
const char me[] = "dump_done";
dns_zone_t *zone = arg;
dns_db_t *db;
dns_dbversion_t *version;
isc_boolean_t again = ISC_FALSE;
REQUIRE(DNS_ZONE_VALID(zone));
ENTER;
/*
* We don't own these, zone->dctx must stay valid.
*/
db = dns_dumpctx_db(zone->dctx);
version = dns_dumpctx_version(zone->dctx);
if (result == ISC_R_SUCCESS && zone->journal != NULL &&
zone->journalsize != -1) {
isc_uint32_t serial;
isc_result_t tresult;
tresult = dns_db_getsoaserial(db, version, &serial);
if (tresult == ISC_R_SUCCESS) {
tresult = dns_journal_compact(zone->mctx, zone->journal,
serial, zone->journalsize);
switch (tresult) {
case ISC_R_SUCCESS:
case ISC_R_NOSPACE:
case ISC_R_NOTFOUND:
dns_zone_log(zone, ISC_LOG_DEBUG(3),
"dns_journal_compact: %s",
dns_result_totext(tresult));
break;
default:
dns_zone_log(zone, ISC_LOG_ERROR,
"dns_journal_compact failed: %s",
dns_result_totext(tresult));
break;
}
}
}
LOCK_ZONE(zone);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DUMPING);
if (result != ISC_R_SUCCESS && result != ISC_R_CANCELED) {
/*
* Try again in a short while.
*/
zone_needdump(zone, DNS_DUMP_DELAY);
} else if (result == ISC_R_SUCCESS &&
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) &&
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) {
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING);
isc_time_settoepoch(&zone->dumptime);
again = ISC_TRUE;
} else if (result == ISC_R_SUCCESS)
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH);
dns_dumpctx_detach(&zone->dctx);
UNLOCK_ZONE(zone);
if (again)
(void)zone_dump(zone, ISC_FALSE);
zonemgr_putio(&zone->writeio);
dns_zone_idetach(&zone);
}
static isc_result_t
zone_dump(dns_zone_t *zone, isc_boolean_t compact) {
const char me[] = "zone_dump";
isc_result_t result;
dns_dbversion_t *version = NULL;
isc_boolean_t again;
dns_db_t *db = NULL;
char *masterfile = NULL;
/*
* 'compact' MUST only be set if we are task locked.
*/
REQUIRE(DNS_ZONE_VALID(zone));
ENTER;
redo:
LOCK_ZONE(zone);
if (zone->db != NULL)
dns_db_attach(zone->db, &db);
if (zone->masterfile != NULL)
masterfile = isc_mem_strdup(zone->mctx, zone->masterfile);
UNLOCK_ZONE(zone);
if (db == NULL) {
result = DNS_R_NOTLOADED;
goto fail;
}
if (masterfile == NULL) {
result = DNS_R_NOMASTERFILE;
goto fail;
}
if (compact) {
dns_zone_t *dummy = NULL;
LOCK_ZONE(zone);
zone_iattach(zone, &dummy);
result = zonemgr_getio(zone->zmgr, ISC_FALSE, zone->task,
zone_gotwritehandle, zone,
&zone->writeio);
if (result != ISC_R_SUCCESS)
zone_idetach(&dummy);
else
result = DNS_R_CONTINUE;
UNLOCK_ZONE(zone);
} else {
dns_db_currentversion(db, &version);
result = dns_master_dump(zone->mctx, db, version,
&dns_master_style_default,
masterfile);
dns_db_closeversion(db, &version, ISC_FALSE);
}
fail:
if (db != NULL)
dns_db_detach(&db);
if (masterfile != NULL)
isc_mem_free(zone->mctx, masterfile);
masterfile = NULL;
if (result == DNS_R_CONTINUE)
return (ISC_R_SUCCESS); /* XXXMPA */
again = ISC_FALSE;
LOCK_ZONE(zone);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DUMPING);
if (result != ISC_R_SUCCESS) {
/*
* Try again in a short while.
*/
zone_needdump(zone, DNS_DUMP_DELAY);
} else if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) &&
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) {
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING);
isc_time_settoepoch(&zone->dumptime);
again = ISC_TRUE;
} else
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH);
UNLOCK_ZONE(zone);
if (again)
goto redo;
return (result);
}
isc_result_t
dns_zone_dumptostream(dns_zone_t *zone, FILE *fd) {
isc_result_t result;
dns_dbversion_t *version = NULL;
dns_db_t *db = NULL;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
if (zone->db != NULL)
dns_db_attach(zone->db, &db);
UNLOCK_ZONE(zone);
if (db == NULL)
return (DNS_R_NOTLOADED);
dns_db_currentversion(db, &version);
result = dns_master_dumptostream(zone->mctx, db, version,
&dns_master_style_default, fd);
dns_db_closeversion(db, &version, ISC_FALSE);
dns_db_detach(&db);
return (result);
}
void
dns_zone_unload(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
zone_unload(zone);
UNLOCK_ZONE(zone);
}
static void
notify_cancel(dns_zone_t *zone) {
dns_notify_t *notify;
/*
* 'zone' locked by caller.
*/
REQUIRE(LOCKED_ZONE(zone));
for (notify = ISC_LIST_HEAD(zone->notifies);
notify != NULL;
notify = ISC_LIST_NEXT(notify, link)) {
if (notify->find != NULL)
dns_adb_cancelfind(notify->find);
if (notify->request != NULL)
dns_request_cancel(notify->request);
}
}
static void
zone_unload(dns_zone_t *zone) {
/*
* 'zone' locked by caller.
*/
REQUIRE(LOCKED_ZONE(zone));
dns_db_detach(&zone->db);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADED);
}
void
dns_zone_setminrefreshtime(dns_zone_t *zone, isc_uint32_t val) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(val > 0);
zone->minrefresh = val;
}
void
dns_zone_setmaxrefreshtime(dns_zone_t *zone, isc_uint32_t val) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(val > 0);
zone->maxrefresh = val;
}
void
dns_zone_setminretrytime(dns_zone_t *zone, isc_uint32_t val) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(val > 0);
zone->minretry = val;
}
void
dns_zone_setmaxretrytime(dns_zone_t *zone, isc_uint32_t val) {
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(val > 0);
zone->maxretry = val;
}
static isc_boolean_t
notify_isqueued(dns_zone_t *zone, dns_name_t *name, isc_sockaddr_t *addr) {
dns_notify_t *notify;
for (notify = ISC_LIST_HEAD(zone->notifies);
notify != NULL;
notify = ISC_LIST_NEXT(notify, link)) {
if (notify->request != NULL)
continue;
if (name != NULL && dns_name_dynamic(&notify->ns) &&
dns_name_equal(name, &notify->ns))
return (ISC_TRUE);
if (addr != NULL && isc_sockaddr_equal(addr, &notify->dst))
return (ISC_TRUE);
}
return (ISC_FALSE);
}
static void
notify_destroy(dns_notify_t *notify, isc_boolean_t locked) {
isc_mem_t *mctx;
/*
* Caller holds zone lock.
*/
REQUIRE(DNS_NOTIFY_VALID(notify));
if (notify->zone != NULL) {
if (!locked)
LOCK_ZONE(notify->zone);
REQUIRE(LOCKED_ZONE(notify->zone));
if (ISC_LINK_LINKED(notify, link))
ISC_LIST_UNLINK(notify->zone->notifies, notify, link);
if (!locked)
UNLOCK_ZONE(notify->zone);
if (locked)
zone_idetach(&notify->zone);
else
dns_zone_idetach(&notify->zone);
}
if (notify->find != NULL)
dns_adb_destroyfind(&notify->find);
if (notify->request != NULL)
dns_request_destroy(&notify->request);
if (dns_name_dynamic(&notify->ns))
dns_name_free(&notify->ns, notify->mctx);
mctx = notify->mctx;
isc_mem_put(notify->mctx, notify, sizeof(*notify));
isc_mem_detach(&mctx);
}
static isc_result_t
notify_create(isc_mem_t *mctx, unsigned int flags, dns_notify_t **notifyp) {
dns_notify_t *notify;
REQUIRE(notifyp != NULL && *notifyp == NULL);
notify = isc_mem_get(mctx, sizeof(*notify));
if (notify == NULL)
return (ISC_R_NOMEMORY);
notify->mctx = NULL;
isc_mem_attach(mctx, &notify->mctx);
notify->flags = flags;
notify->zone = NULL;
notify->find = NULL;
notify->request = NULL;
isc_sockaddr_any(&notify->dst);
dns_name_init(&notify->ns, NULL);
ISC_LINK_INIT(notify, link);
notify->magic = NOTIFY_MAGIC;
*notifyp = notify;
return (ISC_R_SUCCESS);
}
/*
* XXXAG should check for DNS_ZONEFLG_EXITING
*/
static void
process_adb_event(isc_task_t *task, isc_event_t *ev) {
dns_notify_t *notify;
isc_eventtype_t result;
UNUSED(task);
notify = ev->ev_arg;
REQUIRE(DNS_NOTIFY_VALID(notify));
INSIST(task == notify->zone->task);
result = ev->ev_type;
isc_event_free(&ev);
if (result == DNS_EVENT_ADBMOREADDRESSES) {
dns_adb_destroyfind(&notify->find);
notify_find_address(notify);
return;
}
if (result == DNS_EVENT_ADBNOMOREADDRESSES) {
LOCK_ZONE(notify->zone);
notify_send(notify);
UNLOCK_ZONE(notify->zone);
}
notify_destroy(notify, ISC_FALSE);
}
static void
notify_find_address(dns_notify_t *notify) {
isc_result_t result;
unsigned int options;
REQUIRE(DNS_NOTIFY_VALID(notify));
options = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_INET |
DNS_ADBFIND_INET6 | DNS_ADBFIND_RETURNLAME;
if (notify->zone->view->adb == NULL)
goto destroy;
result = dns_adb_createfind(notify->zone->view->adb,
notify->zone->task,
process_adb_event, notify,
&notify->ns, dns_rootname,
options, 0, NULL,
notify->zone->view->dstport,
&notify->find);
/* Something failed? */
if (result != ISC_R_SUCCESS)
goto destroy;
/* More addresses pending? */
if ((notify->find->options & DNS_ADBFIND_WANTEVENT) != 0)
return;
/* We have as many addresses as we can get. */
LOCK_ZONE(notify->zone);
notify_send(notify);
UNLOCK_ZONE(notify->zone);
destroy:
notify_destroy(notify, ISC_FALSE);
}
static isc_result_t
notify_send_queue(dns_notify_t *notify) {
isc_event_t *e;
isc_result_t result;
e = isc_event_allocate(notify->mctx, NULL,
DNS_EVENT_NOTIFYSENDTOADDR,
notify_send_toaddr,
notify, sizeof(isc_event_t));
if (e == NULL)
return (ISC_R_NOMEMORY);
e->ev_arg = notify;
e->ev_sender = NULL;
result = isc_ratelimiter_enqueue(notify->zone->zmgr->rl,
notify->zone->task, &e);
if (result != ISC_R_SUCCESS)
isc_event_free(&e);
return (result);
}
static void
notify_send_toaddr(isc_task_t *task, isc_event_t *event) {
dns_notify_t *notify;
isc_result_t result;
dns_message_t *message = NULL;
isc_netaddr_t dstip;
dns_tsigkey_t *key = NULL;
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
isc_sockaddr_t src;
int timeout;
notify = event->ev_arg;
REQUIRE(DNS_NOTIFY_VALID(notify));
UNUSED(task);
LOCK_ZONE(notify->zone);
if (DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_LOADED) == 0) {
result = ISC_R_CANCELED;
goto cleanup;
}
if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0 ||
DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_EXITING) ||
notify->zone->view->requestmgr == NULL ||
notify->zone->db == NULL) {
result = ISC_R_CANCELED;
goto cleanup;
}
/*
* The raw IPv4 address should also exist. Don't send to the
* mapped form.
*/
if (isc_sockaddr_pf(&notify->dst) == PF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&notify->dst.type.sin6.sin6_addr)) {
isc_sockaddr_format(&notify->dst, addrbuf, sizeof(addrbuf));
notify_log(notify->zone, ISC_LOG_DEBUG(3),
"notify: ignoring IPv6 mapped IPV4 address: %s",
addrbuf);
result = ISC_R_CANCELED;
goto cleanup;
}
result = notify_createmessage(notify->zone, notify->flags, &message);
if (result != ISC_R_SUCCESS)
goto cleanup;
isc_netaddr_fromsockaddr(&dstip, &notify->dst);
(void)dns_view_getpeertsig(notify->zone->view, &dstip, &key);
isc_sockaddr_format(&notify->dst, addrbuf, sizeof(addrbuf));
notify_log(notify->zone, ISC_LOG_DEBUG(3), "sending notify to %s",
addrbuf);
switch (isc_sockaddr_pf(&notify->dst)) {
case PF_INET:
src = notify->zone->notifysrc4;
break;
case PF_INET6:
src = notify->zone->notifysrc6;
break;
default:
result = ISC_R_NOTIMPLEMENTED;
goto cleanup_key;
}
timeout = 15;
if (DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_DIALNOTIFY))
timeout = 30;
result = dns_request_createvia2(notify->zone->view->requestmgr,
message, &src, &notify->dst, 0, key,
timeout * 3, timeout,
notify->zone->task, notify_done,
notify, &notify->request);
cleanup_key:
if (key != NULL)
dns_tsigkey_detach(&key);
dns_message_destroy(&message);
cleanup:
UNLOCK_ZONE(notify->zone);
if (result != ISC_R_SUCCESS)
notify_destroy(notify, ISC_FALSE);
isc_event_free(&event);
}
static void
notify_send(dns_notify_t *notify) {
dns_adbaddrinfo_t *ai;
isc_sockaddr_t dst;
isc_result_t result;
dns_notify_t *new = NULL;
/*
* Zone lock held by caller.
*/
REQUIRE(DNS_NOTIFY_VALID(notify));
REQUIRE(LOCKED_ZONE(notify->zone));
for (ai = ISC_LIST_HEAD(notify->find->list);
ai != NULL;
ai = ISC_LIST_NEXT(ai, publink)) {
dst = ai->sockaddr;
if (notify_isqueued(notify->zone, NULL, &dst))
continue;
new = NULL;
result = notify_create(notify->mctx,
(notify->flags & DNS_NOTIFY_NOSOA),
&new);
if (result != ISC_R_SUCCESS)
goto cleanup;
zone_iattach(notify->zone, &new->zone);
ISC_LIST_APPEND(new->zone->notifies, new, link);
new->dst = dst;
result = notify_send_queue(new);
if (result != ISC_R_SUCCESS)
goto cleanup;
new = NULL;
}
cleanup:
if (new != NULL)
notify_destroy(new, ISC_TRUE);
}
void
dns_zone_notify(dns_zone_t *zone) {
isc_time_t now;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
TIME_NOW(&now);
zone_settimer(zone, &now);
UNLOCK_ZONE(zone);
}
static void
zone_notify(dns_zone_t *zone) {
dns_dbnode_t *node = NULL;
dns_dbversion_t *version = NULL;
dns_name_t *origin = NULL;
dns_name_t master;
dns_rdata_ns_t ns;
dns_rdata_soa_t soa;
isc_uint32_t serial = 0;
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;
isc_boolean_t loggednotify = ISC_FALSE;
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;
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);
if (result == ISC_R_SUCCESS) {
dns_rdataset_current(&soardset, &rdata);
result = dns_rdata_tostruct(&rdata, &soa, NULL);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
dns_rdata_reset(&rdata);
result = dns_name_dup(&soa.origin, zone->mctx, &master);
serial = soa.serial;
dns_rdataset_disassociate(&soardset);
}
if (result != ISC_R_SUCCESS)
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);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
dns_rdata_reset(&rdata);
/*
* don't notify the master server.
*/
if (dns_name_compare(&master, &ns.name) == 0) {
result = dns_rdataset_next(&nsrdset);
continue;
}
if (!loggednotify) {
notify_log(zone, ISC_LOG_INFO,
"sending notifies (serial %u)",
serial);
loggednotify = ISC_TRUE;
}
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);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
dns_rdata_reset(&rdata);
if (!dns_name_issubdomain(&ns.name, name))
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_time_t now;
isc_boolean_t exiting = ISC_FALSE;
isc_interval_t i;
stub = revent->ev_arg;
INSIST(DNS_STUB_VALID(stub));
UNUSED(task);
zone = stub->zone;
ENTER;
TIME_NOW(&now);
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
zone_debuglog(zone, me, 1, "exiting");
exiting = ISC_TRUE;
goto next_master;
}
isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
if (revent->result != ISC_R_SUCCESS) {
if (revent->result == ISC_R_TIMEDOUT &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) {
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
UNLOCK_ZONE(zone);
dns_zone_log(zone, ISC_LOG_DEBUG(1),
"refreshing stub: timeout retrying "
" without EDNS master %s", master);
goto same_master;
}
dns_zone_log(zone, ISC_LOG_INFO,
"could not refresh stub from master %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));
(void)dns_rcode_totext(msg->rcode, &rb);
if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) &&
(msg->rcode == dns_rcode_servfail ||
msg->rcode == dns_rcode_notimp ||
msg->rcode == dns_rcode_formerr)) {
dns_zone_log(zone, ISC_LOG_DEBUG(1),
"refreshing stub: rcode (%.*s) retrying "
"without EDNS master %s",
(int)rb.used, rcode, master);
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
UNLOCK_ZONE(zone);
goto same_master;
}
dns_zone_log(zone, ISC_LOG_INFO,
"refreshing stub: "
"unexpected rcode (%.*s) from %s",
(int)rb.used, rcode, master);
goto next_master;
}
/*
* We need complete messages.
*/
if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
if (dns_request_usedtcp(revent->request)) {
dns_zone_log(zone, ISC_LOG_INFO,
"refreshing stub: "
"truncated TCP response from master %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) {
dns_zone_log(zone, ISC_LOG_INFO, "refreshing stub: "
"non-authoritative answer from master %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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refreshing stub: unexpected CNAME response "
"from master %s", master);
goto next_master;
}
if (nscnt == 0) {
dns_zone_log(zone, ISC_LOG_INFO,
"refreshing stub: no NS records in response "
"from master %s", master);
goto next_master;
}
/*
* Save answer.
*/
result = save_nsrrset(msg, &zone->origin, stub->db, stub->version);
if (result != ISC_R_SUCCESS) {
dns_zone_log(zone, ISC_LOG_INFO,
"refreshing stub: unable to save NS records "
"from master %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);
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);
DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime);
isc_interval_set(&i, zone->expire, 0);
DNS_ZONE_TIME_ADD(&now, zone->expire, &zone->expiretime);
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++;
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS);
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_time_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);
ENTER;
/*
* if timeout log and next master;
*/
isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
TIME_NOW(&now);
if (revent->result != ISC_R_SUCCESS) {
if (revent->result == ISC_R_TIMEDOUT &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) {
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
UNLOCK_ZONE(zone);
dns_zone_log(zone, ISC_LOG_DEBUG(1),
"refresh: timeout retrying without EDNS "
"master %s", master);
goto same_master;
}
if (revent->result == ISC_R_TIMEDOUT &&
!dns_request_usedtcp(revent->request)) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: retry limit for "
"master %s exceeded",
master);
} else
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: failure trying master %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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: failure trying master %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));
(void)dns_rcode_totext(msg->rcode, &rb);
if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) &&
(msg->rcode == dns_rcode_servfail ||
msg->rcode == dns_rcode_notimp ||
msg->rcode == dns_rcode_formerr)) {
dns_zone_log(zone, ISC_LOG_DEBUG(1),
"refresh: rcode (%.*s) retrying without "
"EDNS master %s", (int)rb.used, rcode,
master);
LOCK_ZONE(zone);
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
UNLOCK_ZONE(zone);
goto same_master;
}
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: unexpected rcode (%.*s) from master %s",
(int)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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: truncated UDP answer, "
"initiating TCP zone xfer "
"for master %s",
master);
goto tcp_transfer;
} else {
INSIST(zone->type == dns_zone_stub);
if (dns_request_usedtcp(revent->request)) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: truncated TCP response "
"from master %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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: non-authoritative answer from "
"master %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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: CNAME at top of zone "
"in master %s", master);
goto next_master;
}
/*
* if referral log and next master;
*/
if (soacnt == 0 && soacount == 0 && nscount != 0) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: referral response "
"from master %s", master);
goto next_master;
}
/*
* if nodata log and next master;
*/
if (soacnt == 0 && (nscount == 0 || soacount != 0)) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: NODATA response "
"from master %s", master);
goto next_master;
}
/*
* Only one soa at top of zone.
*/
if (soacnt != 1) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: answer SOA count (%d) != 1 "
"from 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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: unable to get SOA record "
"from master %s", master);
goto next_master;
}
result = dns_rdataset_first(rdataset);
if (result != ISC_R_SUCCESS) {
dns_zone_log(zone, ISC_LOG_INFO,
"refresh: dns_rdataset_first() failed");
goto next_master;
}
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &soa, NULL);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
serial = soa.serial;
zone_debuglog(zone, me, 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) {
result = isc_file_settime(zone->masterfile, &now);
/* Someone removed the file from underneath us! */
if (result == ISC_R_FILENOTFOUND) {
LOCK_ZONE(zone);
zone_needdump(zone, DNS_DUMP_DELAY);
UNLOCK_ZONE(zone);
} else if (result != ISC_R_SUCCESS)
dns_zone_log(zone, ISC_LOG_ERROR,
"refresh: could not set file "
"modification time of '%s': %s",
zone->masterfile,
dns_result_totext(result));
}
DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime);
DNS_ZONE_TIME_ADD(&now, zone->expire, &zone->expiretime);
goto next_master;
} else {
zone_debuglog(zone, me, 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++;
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS);
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:
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;
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 isc_result_t
add_opt(dns_message_t *message) {
dns_rdataset_t *rdataset = NULL;
dns_rdatalist_t *rdatalist = NULL;
dns_rdata_t *rdata = NULL;
isc_result_t result;
result = dns_message_gettemprdatalist(message, &rdatalist);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_message_gettemprdata(message, &rdata);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_message_gettemprdataset(message, &rdataset);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_rdataset_init(rdataset);
rdatalist->type = dns_rdatatype_opt;
rdatalist->covers = 0;
/*
* Set Maximum UDP buffer size.
*/
rdatalist->rdclass = SEND_BUFFER_SIZE;
/*
* Set EXTENDED-RCODE, VERSION, DO and Z to 0.
*/
rdatalist->ttl = 0;
/*
* No EDNS options.
*/
rdata->data = NULL;
rdata->length = 0;
rdata->rdclass = rdatalist->rdclass;
rdata->type = rdatalist->type;
rdata->flags = 0;
ISC_LIST_INIT(rdatalist->rdata);
ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
== ISC_R_SUCCESS);
return (dns_message_setopt(message, rdataset));
cleanup:
if (rdatalist != NULL)
dns_message_puttemprdatalist(message, &rdatalist);
if (rdataset != NULL)
dns_message_puttemprdataset(message, &rdataset);
if (rdata != NULL)
dns_message_puttemprdata(message, &rdata);
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;
int timeout;
REQUIRE(DNS_ZONE_VALID(zone));
UNUSED(task);
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);
if (zone->view->peers != NULL) {
dns_peer_t *peer = NULL;
isc_boolean_t edns;
result = dns_peerlist_peerbyaddr(zone->view->peers,
&masterip, &peer);
if (result == ISC_R_SUCCESS)
result = dns_peer_getsupportedns(peer, &edns);
if (result == ISC_R_SUCCESS && !edns)
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
}
if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) {
result = add_opt(message);
if (result != ISC_R_SUCCESS)
zone_debuglog(zone, me, 1,
"unable to add opt record: %s",
dns_result_totext(result));
}
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);
timeout = 15;
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH))
timeout = 30;
result = dns_request_createvia2(zone->view->requestmgr, message,
&src, &zone->masteraddr, options, key,
timeout * 3, timeout, zone->task,
refresh_callback, zone, &zone->request);
if (result != ISC_R_SUCCESS) {
zone_idetach(&dummy);
zone_debuglog(zone, me, 1,
"dns_request_createvia2() 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;
int timeout;
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE((soardataset != NULL && stub == NULL) ||
(soardataset == NULL && stub != NULL));
REQUIRE(stub == NULL || DNS_STUB_VALID(stub));
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) {
dns_zone_log(zone, ISC_LOG_ERROR,
"refreshing stub: "
"could not create "
"database: %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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refreshing stub: "
"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) {
dns_zone_log(zone, ISC_LOG_INFO,
"refreshing stub: "
"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);
if (zone->view->peers != NULL) {
dns_peer_t *peer = NULL;
isc_boolean_t edns;
result = dns_peerlist_peerbyaddr(zone->view->peers,
&masterip, &peer);
if (result == ISC_R_SUCCESS)
result = dns_peer_getsupportedns(peer, &edns);
if (result == ISC_R_SUCCESS && !edns)
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
}
if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) {
result = add_opt(message);
if (result != ISC_R_SUCCESS)
zone_debuglog(zone, me, 1,
"unable to add opt record: %s",
dns_result_totext(result));
}
/*
* 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;
}
timeout = 15;
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH))
timeout = 30;
result = dns_request_createvia2(zone->view->requestmgr, message,
&src, &zone->masteraddr,
DNS_REQUESTOPT_TCP, key, timeout * 3,
timeout, zone->task, stub_callback,
stub, &zone->request);
if (result != ISC_R_SUCCESS) {
zone_debuglog(zone, me, 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);
if (stub->zone != NULL)
zone_idetach(&stub->zone);
isc_mem_put(stub->mctx, stub, sizeof(*stub));
}
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_boolean_t free_needed;
UNUSED(task);
REQUIRE(DNS_ZONE_VALID(zone));
INSIST(event->ev_type == DNS_EVENT_ZONECONTROL);
INSIST(isc_refcount_current(&zone->erefs) == 0);
zone_debuglog(zone, "zone_shutdown", 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);
}
/*
* In task context, no locking required. See zone_xfrdone().
*/
if (zone->xfr != NULL)
dns_xfrin_shutdown(zone->xfr);
LOCK_ZONE(zone);
if (zone->request != NULL) {
dns_request_cancel(zone->request);
}
if (zone->readio != NULL)
zonemgr_cancelio(zone->readio);
if (zone->writeio != NULL)
zonemgr_cancelio(zone->writeio);
if (zone->lctx != NULL)
dns_loadctx_cancel(zone->lctx);
if (zone->dctx != NULL)
dns_dumpctx_cancel(zone->dctx);
notify_cancel(zone);
if (zone->timer != NULL) {
isc_timer_detach(&zone->timer);
INSIST(zone->irefs > 0);
zone->irefs--;
}
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));
ENTER;
zone_maintenance(zone);
isc_event_free(&event);
}
static void
zone_settimer(dns_zone_t *zone, isc_time_t *now) {
const char me[] = "zone_settimer";
isc_time_t next;
isc_result_t result;
REQUIRE(DNS_ZONE_VALID(zone));
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
return;
isc_time_settoepoch(&next);
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) &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) {
INSIST(!isc_time_isepoch(&zone->dumptime));
if (isc_time_isepoch(&next) ||
isc_time_compare(&zone->dumptime, &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) &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADING)) {
INSIST(!isc_time_isepoch(&zone->refreshtime));
if (isc_time_isepoch(&next) ||
isc_time_compare(&zone->refreshtime, &next) < 0)
next = zone->refreshtime;
}
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) {
INSIST(!isc_time_isepoch(&zone->expiretime));
if (isc_time_isepoch(&next) ||
isc_time_compare(&zone->expiretime, &next) < 0)
next = zone->expiretime;
}
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) {
INSIST(!isc_time_isepoch(&zone->dumptime));
if (isc_time_isepoch(&next) ||
isc_time_compare(&zone->dumptime, &next) < 0)
next = zone->dumptime;
}
break;
default:
break;
}
if (isc_time_isepoch(&next)) {
zone_debuglog(zone, me, 10, "settimer inactive");
result = isc_timer_reset(zone->timer, isc_timertype_inactive,
NULL, NULL, ISC_TRUE);
if (result != ISC_R_SUCCESS)
dns_zone_log(zone, ISC_LOG_ERROR,
"could not deactivate zone timer: %s",
isc_result_totext(result));
} else {
if (isc_time_compare(&next, now) <= 0)
next = *now;
result = isc_timer_reset(zone->timer, isc_timertype_once,
&next, NULL, ISC_TRUE);
if (result != ISC_R_SUCCESS)
dns_zone_log(zone, ISC_LOG_ERROR,
"could not reset zone timer: %s",
isc_result_totext(result));
}
}
static void
cancel_refresh(dns_zone_t *zone) {
const char me[] = "cancel_refresh";
isc_time_t now;
/*
* 'zone' locked by caller.
*/
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(LOCKED_ZONE(zone));
ENTER;
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
TIME_NOW(&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)
{
unsigned int i;
dns_rdata_soa_t soa;
dns_rdataset_t *rdataset = NULL;
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_result_t result;
char fromtext[ISC_SOCKADDR_FORMATSIZE];
int match = 0;
isc_netaddr_t netaddr;
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) {
dns_zone_log(zone, ISC_LOG_NOTICE,
"NOTIFY with no "
"question section from: %s", fromtext);
return (DNS_R_FORMERR);
}
dns_zone_log(zone, ISC_LOG_NOTICE,
"NOTIFY 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'.
*/
isc_netaddr_fromsockaddr(&netaddr, from);
if (i >= zone->masterscnt && zone->notify_acl != NULL &&
dns_acl_match(&netaddr, NULL, zone->notify_acl,
&zone->view->aclenv,
&match, NULL) == ISC_R_SUCCESS &&
match > 0)
{
/* Accept notify. */
} else if (i >= zone->masterscnt) {
UNLOCK_ZONE(zone);
dns_zone_log(zone, 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);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
serial = soa.serial;
if (isc_serial_le(serial, zone->serial)) {
dns_zone_log(zone, ISC_LOG_DEBUG(3),
"notify from %s: "
"zone is up to date",
fromtext);
UNLOCK_ZONE(zone);
return (ISC_R_SUCCESS);
}
}
}
/*
* If we got this far and there was a refresh in progress just
* let it complete. Record where we got the notify from so we
* can perform a refresh check when the current one completes
*/
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH)) {
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDREFRESH);
zone->notifyfrom = *from;
UNLOCK_ZONE(zone);
dns_zone_log(zone, ISC_LOG_DEBUG(3),
"notify from %s: refresh in progress, "
"refresh check queued",
fromtext);
return (ISC_R_SUCCESS);
}
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);
}
isc_boolean_t
dns_zone_getupdatedisabled(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->update_disabled);
}
void
dns_zone_setupdatedisabled(dns_zone_t *zone, isc_boolean_t state) {
REQUIRE(DNS_ZONE_VALID(zone));
zone->update_disabled = state;
}
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_availablelength(&buffer) >= (sizeof("<UNKNOWN>") - 1))
isc_buffer_putstr(&buffer, "<UNKNOWN>");
if (isc_buffer_availablelength(&buffer) > 0)
isc_buffer_putstr(&buffer, "/");
(void)dns_rdataclass_totext(zone->rdclass, &buffer);
if (zone->view != NULL && strcmp(zone->view->name, "_bind") != 0 &&
strcmp(zone->view->name, "_default") != 0 &&
strlen(zone->view->name) < isc_buffer_availablelength(&buffer)) {
isc_buffer_putstr(&buffer, "/");
isc_buffer_putstr(&buffer, zone->view->name);
}
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);
}
void
dns_zone_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_GENERAL, DNS_LOGMODULE_ZONE,
level, "zone %s: %s", namebuf, message);
}
static void
zone_debuglog(dns_zone_t *zone, const char *me, int debuglevel,
const char *fmt, ...)
{
va_list ap;
char message[4096];
char namebuf[1024+32];
int level = ISC_LOG_DEBUG(debuglevel);
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, (int)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->flags |= DNS_NOTIFY_NOSOA;
dns_request_destroy(&notify->request);
result = notify_send_queue(notify);
if (result != ISC_R_SUCCESS)
notify_destroy(notify, ISC_FALSE);
} 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 &&
DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IXFRFROMDIFFS)) {
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;
if (dump)
zone_needdump(zone, DNS_DUMP_DELAY);
} 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.
*/
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) {
isc_time_t now;
isc_boolean_t again = ISC_FALSE;
unsigned int soacount;
unsigned int nscount;
isc_uint32_t serial, refresh, retry, expire, minimum;
isc_result_t xfrresult = result;
REQUIRE(DNS_ZONE_VALID(zone));
dns_zone_log(zone, ISC_LOG_DEBUG(1),
"zone transfer finished: %s", dns_result_totext(result));
LOCK_ZONE(zone);
INSIST((zone->flags & DNS_ZONEFLG_REFRESH) != 0);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
TIME_NOW(&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) {
result = ISC_R_FAILURE;
if (zone->journal != NULL)
result = isc_file_settime(zone->journal, &now);
if (result != ISC_R_SUCCESS &&
zone->masterfile != NULL)
result = isc_file_settime(zone->masterfile,
&now);
/* Someone removed the file from underneath us! */
if (result == ISC_R_FILENOTFOUND &&
zone->masterfile != NULL)
zone_needdump(zone, DNS_DUMP_DELAY);
else if (result != ISC_R_SUCCESS)
dns_zone_log(zone, ISC_LOG_ERROR,
"transfer: could not set file "
"modification time of '%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)
dns_zone_log(zone, ISC_LOG_ERROR,
"transferred zone "
"has %d SOA record%s", soacount,
(soacount != 0) ? "s" : "");
if (nscount == 0)
dns_zone_log(zone, ISC_LOG_ERROR,
"transferred zone "
"has 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;
DNS_ZONE_TIME_ADD(&now, zone->expire,
&zone->expiretime);
} else {
DNS_ZONE_JITTER_ADD(&now, zone->refresh,
&zone->refreshtime);
DNS_ZONE_TIME_ADD(&now, zone->expire,
&zone->expiretime);
}
if (result == ISC_R_SUCCESS && xfrresult == ISC_R_SUCCESS)
dns_zone_log(zone, ISC_LOG_INFO,
"transfered serial %u", zone->serial);
break;
case DNS_R_BADIXFR:
/* Force retry with AXFR. */
DNS_ZONE_SETFLAG(zone, DNS_ZONEFLAG_NOIXFR);
goto same_master;
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;
ENTER;
tresult = dns_db_endload(load->db, &load->callbacks.add_private);
if (tresult != ISC_R_SUCCESS &&
(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);
if (load->zone->lctx != NULL)
dns_loadctx_detach(&load->zone->lctx);
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;
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) {
dns_zone_log(zone, ISC_LOG_DEBUG(1),
"zone transfer deferred due to quota");
} else if (result != ISC_R_SUCCESS) {
dns_zone_log(zone, 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) {
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) {
dns_zone_log(zone, 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)) {
dns_zone_log(zone, ISC_LOG_DEBUG(3),
"forced reload, requesting AXFR of "
"initial version from %s", mastertext);
xfrtype = dns_rdatatype_axfr;
} else if (DNS_ZONE_FLAG(zone, DNS_ZONEFLAG_NOIXFR)) {
dns_zone_log(zone, ISC_LOG_DEBUG(3),
"retrying with AXFR from %s due to "
"previous IXFR failure", mastertext);
xfrtype = dns_rdatatype_axfr;
LOCK_ZONE(zone);
DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLAG_NOIXFR);
UNLOCK_ZONE(zone);
} 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) {
dns_zone_log(zone, ISC_LOG_DEBUG(3),
"IXFR disabled, "
"requesting AXFR from %s",
mastertext);
xfrtype = dns_rdatatype_axfr;
} else {
dns_zone_log(zone, 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) {
dns_zone_log(zone, ISC_LOG_ERROR,
"could not get 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));
ENTER;
isc_sockaddr_format(&forward->addr, master, sizeof(master));
if (revent->result != ISC_R_SUCCESS) {
dns_zone_log(zone, ISC_LOG_INFO,
"could not forward dynamic update to %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));
(void)dns_rcode_totext(msg->rcode, &rb);
dns_zone_log(zone, ISC_LOG_WARNING,
"forwarding dynamic update: "
"unexpected response: master %s returned: %.*s",
master, (int)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 */
dns_zone_log(zone, ISC_LOG_DEBUG(3),
"exhausted dynamic update forwarder list");
(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;
}
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_rwlock;
/* 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;
/* default to 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_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;
/*
* The timer "holds" a iref.
*/
zone->irefs++;
INSIST(zone->irefs != 0);
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->rwlock);
mctx = zmgr->mctx;
isc_mem_put(zmgr->mctx, zmgr, sizeof(*zmgr));
isc_mem_detach(&mctx);
}
void
dns_zonemgr_settransfersin(dns_zonemgr_t *zmgr, isc_uint32_t value) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
zmgr->transfersin = value;
}
isc_uint32_t
dns_zonemgr_getttransfersin(dns_zonemgr_t *zmgr) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
return (zmgr->transfersin);
}
void
dns_zonemgr_settransfersperns(dns_zonemgr_t *zmgr, isc_uint32_t value) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
zmgr->transfersperns = value;
}
isc_uint32_t
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) {
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 {
dns_zone_log(zone, 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;
isc_uint32_t nxfrsin, nxfrsperns;
dns_zone_t *x;
isc_uint32_t 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.
*/
isc_refcount_increment(&zone->erefs, NULL);
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;
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;
dns_zone_log(zone, 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_zonemgr_setserialqueryrate(dns_zonemgr_t *zmgr, unsigned int value) {
isc_interval_t interval;
isc_uint32_t s, ns;
isc_uint32_t pertic;
isc_result_t result;
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
if (value == 0)
value = 1;
if (value == 1) {
s = 1;
ns = 0;
pertic = 1;
} else if (value < 10) {
s = 0;
ns = 1000000000 / value;
pertic = 1;
} else {
s = 0;
ns = (1000000000 / value) * 10;
pertic = 10;
}
isc_interval_set(&interval, s, ns);
result = isc_ratelimiter_setinterval(zmgr->rl, &interval);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
isc_ratelimiter_setpertic(zmgr->rl, pertic);
zmgr->serialqueryrate = value;
}
unsigned int
dns_zonemgr_getserialqueryrate(dns_zonemgr_t *zmgr) {
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
return (zmgr->serialqueryrate);
}
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_debuglog(zone, "dns_zone_dialup", 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);
}
isc_result_t
dns_zone_setkeydirectory(dns_zone_t *zone, const char *directory) {
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(DNS_ZONE_VALID(zone));
LOCK_ZONE(zone);
result = dns_zone_setstring(zone, &zone->keydirectory, directory);
UNLOCK_ZONE(zone);
return (result);
}
const char *
dns_zone_getkeydirectory(dns_zone_t *zone) {
REQUIRE(DNS_ZONE_VALID(zone));
return (zone->keydirectory);
}
unsigned int
dns_zonemgr_getcount(dns_zonemgr_t *zmgr, int state) {
dns_zone_t *zone;
unsigned int count = 0;
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
RWLOCK(&zmgr->rwlock, isc_rwlocktype_read);
switch (state) {
case DNS_ZONESTATE_XFERRUNNING:
for (zone = ISC_LIST_HEAD(zmgr->xfrin_in_progress);
zone != NULL;
zone = ISC_LIST_NEXT(zone, statelink))
count++;
break;
case DNS_ZONESTATE_XFERDEFERRED:
for (zone = ISC_LIST_HEAD(zmgr->waiting_for_xfrin);
zone != NULL;
zone = ISC_LIST_NEXT(zone, statelink))
count++;
break;
case DNS_ZONESTATE_SOAQUERY:
for (zone = ISC_LIST_HEAD(zmgr->zones);
zone != NULL;
zone = ISC_LIST_NEXT(zone, link))
if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH))
count++;
break;
case DNS_ZONESTATE_ANY:
for (zone = ISC_LIST_HEAD(zmgr->zones);
zone != NULL;
zone = ISC_LIST_NEXT(zone, link))
count++;
break;
default:
INSIST(0);
}
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_read);
return (count);
}