update.c revision 732e0731dec1922747bb3b3147cf2c3d16b22eaa
/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <isc/assertions.h>
#include <dns/fixedname.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatastruct.h>
/*
* This module implements dynamic update as in RFC2136.
*/
/*
XXX TODO:
- serialization and queueing of update requests
- forwarding
- locking?
- document strict minimality
- use logging library, not printf
*/
} while (0)
struct rr {
/* dns_name_t name; */
};
/**************************************************************************/
/*
* Update a single RR in version 'ver' of 'db' and log the
* update in 'diff'.
* Ensures:
* '*tuple' == NULL. Either the tuple is freed, or its
* ownership has been transferred to the diff.
*/
static dns_result_t
{
/* Create a singleton diff */
/* Apply it to the database. */
if (result != DNS_R_SUCCESS) {
return (result);
}
/* Merge it into the current pending journal entry. */
/* Do not clear temp_diff. */
return (DNS_R_SUCCESS);
}
static dns_result_t
{
if (result != DNS_R_SUCCESS)
return (result);
}
/**************************************************************************/
/*
* Callback-style iteration over rdatasets and rdatas.
*
* foreach_rrset() can be used to iterate over the RRsets
* of a name and call a callback function with each
* one. Similarly, foreach_rr() can be used to iterate
* over the individual RRs at name, optionally restricted
* to RRs of a given type.
*
* The callback functions are called "actions" and take
* two arguments: a void pointer for passing arbitrary
* context information, and a pointer to the current RRset
* or RR. By convention, their names end in "_action".
*/
/*
* XXXRTH We might want to make this public somewhere in libdns.
*/
/* Function type for foreach_rrset() iterator actions. */
/* Function type for foreach_rr() iterator actions. */
/* Internal context struct for foreach_node_rr(). */
typedef struct {
void * rr_action_data;
/* Internal helper function for foreach_node_rr(). */
static dns_result_t
{
result == DNS_R_SUCCESS;
{
if (result != DNS_R_SUCCESS)
return (result);
}
if (result != DNS_R_NOMORE)
return (result);
return (DNS_R_SUCCESS);
}
/*
* For each rdataset of 'name' in 'ver' of 'db', call 'action'
* with the rdataset and 'action_data' as arguments. If the name
* does not exist, do nothing.
*
* If 'action' returns an error, abort iteration and return the error.
*/
static dns_result_t
void *action_data)
{
if (result == DNS_R_NOTFOUND)
return (DNS_R_SUCCESS);
if (result != DNS_R_SUCCESS)
return (result);
(isc_stdtime_t) 0, &iter);
if (result != DNS_R_SUCCESS)
goto cleanup_node;
result == DNS_R_SUCCESS;
{
if (result != DNS_R_SUCCESS)
goto cleanup_iterator;
}
if (result == DNS_R_NOMORE)
return (result);
}
/*
* For each RR of 'name' in 'ver' of 'db', call 'action'
* with the RR and 'action_data' as arguments. If the name
* does not exist, do nothing.
*
* If 'action' returns an error, abort iteration
* and return the error.
*/
static dns_result_t
void *rr_action_data)
{
}
/*
* For each of the RRs specified by 'db', 'ver', 'name', 'type',
* (which can be dns_rdatatype_any to match any type), and 'covers', call
* 'action' with the RR and 'action_data' as arguments. If the name
* does not exist, or if no RRset of the given type exists at the name,
* do nothing.
*
* If 'action' returns an error, abort iteration and return the error.
*/
static dns_result_t
void *rr_action_data)
{
if (type == dns_rdatatype_any)
if (result == DNS_R_NOTFOUND)
return (DNS_R_SUCCESS);
if (result != DNS_R_SUCCESS)
return (result);
(isc_stdtime_t) 0, &rdataset);
if (result == DNS_R_NOTFOUND) {
goto cleanup_node;
}
if (result != DNS_R_SUCCESS)
goto cleanup_node;
result == DNS_R_SUCCESS;
{
if (result != DNS_R_SUCCESS)
goto cleanup_rdataset;
}
if (result != DNS_R_NOMORE)
goto cleanup_rdataset;
return (result);
}
/**************************************************************************/
/*
* Various tests on the database contents (for prerequisites, etc).
*/
/*
* Function type for predicate functions that compare a database RR 'db_rr'
* against an update RR 'update_rr'.
*/
/* Helper function for rrset_exists(). */
static dns_result_t
{
return (DNS_R_EXISTS);
}
#define RETURN_EXISTENCE_FLAG \
do { \
if (result == DNS_R_EXISTS) { \
return (DNS_R_SUCCESS); \
} else if (result == DNS_R_SUCCESS) { \
return (DNS_R_SUCCESS); \
} else { \
return (result); \
} \
} while (0)
/*
* Set '*exists' to true iff an rrset of the given type exists,
* to false otherwise.
*/
static dns_result_t
{
}
/* Helper function for cname_incompatible_rrset_exists(). */
/*
* XXXRTH We should move this to rdata.c.
*/
static isc_boolean_t
return ((type == dns_rdatatype_sig ||
type == dns_rdatatype_key ||
type == dns_rdatatype_nxt) ?
}
/* Helper function for cname_incompatible_rrset_exists */
static dns_result_t
/*ARGSUSED*/
{
return (DNS_R_EXISTS);
return (DNS_R_SUCCESS);
}
/*
* Check whether there is an rrset incompatible with adding a CNAME RR,
* i.e., anything but another CNAME (which can be replaced) or a
* DNSSEC RR (which can coexist).
*
* If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
* Otherwise, set it to ISC_FALSE.
*/
static dns_result_t
}
/* Helper function for rr_count(). */
static dns_result_t
(*countp)++;
return (DNS_R_SUCCESS);
}
/*
* Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
*/
static dns_result_t
{
*countp = 0;
}
/* Context struct for matching_rr_exists(). */
typedef struct {
/* Helper function for matching_rr_exists(). */
static dns_result_t
return (DNS_R_EXISTS);
return (DNS_R_SUCCESS);
}
/*
* Compare the 'update_rr' with all RRs in the RRset specified by 'db',
* 'ver', 'name', and 'type' using 'predicate'. If the predicate returns
* true for at least one of them, set '*exists' to ISC_TRUE. Otherwise,
* set it to ISC_FALSE.
*/
static dns_result_t
{
}
/* Context struct and helper function for name_exists() */
static dns_result_t
{
return (DNS_R_EXISTS);
}
/*
* Set '*exists' to true iff the given name exists, to false otherwise.
*/
static dns_result_t
{
}
/**************************************************************************/
/*
* Checking of "RRset exists (value dependent)" prerequisites.
*
* In the RFC2136 section 3.2.5, this is the pseudocode involving
* a variable called "temp", a mapping of <name, type> tuples to rrsets.
*
* Here, we represent the "temp" data structure as (non-minimial) "dns_diff_t"
* where each typle has op==DNS_DIFFOP_EXISTS.
*/
/*
* Append a tuple asserting the existence of the RR with
* 'name' and 'rdata' to 'diff'.
*/
static dns_result_t
{
if (result != DNS_R_SUCCESS)
return (result);
return (DNS_R_SUCCESS);
}
/*
* Compare two rdatasets represented as sorted lists of tuples.
* All list elements must have the same owner name and type.
* Return DNS_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
* if not.
*/
static dns_result_t
for (;;) {
break;
return (DNS_R_NXRRSET);
a = ISC_LIST_NEXT(a, link);
b = ISC_LIST_NEXT(b, link);
}
return (DNS_R_NXRRSET);
return (DNS_R_SUCCESS);
}
/*
* A comparison function defining the sorting order for the entries
* in the "temp" data structure. The major sort key is the owner name,
* followed by the type and rdata.
*/
static int
{
dns_difftuple_t *a = *ap;
dns_difftuple_t *b = *bp;
int r;
if (r != 0)
return (r);
if (r != 0)
return (r);
return (r);
}
/*
* Check the "RRset exists (value dependent)" prerequisite information
* in 'temp' against the contents of the database 'db'.
*
* Return DNS_R_SUCCESS if the prerequisites are satisfied,
* rcode(dns_rcode_nxrrset) if not.
*/
static dns_result_t
{
dns_difftuple_t *t;
/* Exit early if the list is empty (for efficiency only). */
return (DNS_R_SUCCESS);
/*
* Sort the prerequisite records by owner name,
* type, and rdata.
*/
if (result != DNS_R_SUCCESS)
return (result);
/*
* For each name and type in the prerequisites,
* construct a sorted rdata list of the corresponding
* database contents, and compare the lists.
*/
while (t != NULL) {
/* A new unique name begins here. */
if (result == DNS_R_NOTFOUND)
return (DNS_R_NXRRSET);
if (result != DNS_R_SUCCESS)
return (result);
/* A new unique type begins here. */
this name and type */
this name and type */
if (type == dns_rdatatype_sig)
else
covers = 0;
/*
* Collect all database RRs for this name and type
* onto d_rrs and sort them.
*/
covers, (isc_stdtime_t) 0,
&rdataset);
if (result != DNS_R_SUCCESS) {
return (DNS_R_NXRRSET);
}
result == DNS_R_SUCCESS;
{
if (result != DNS_R_SUCCESS)
goto failure;
}
if (result != DNS_R_NOMORE)
goto failure;
if (result != DNS_R_SUCCESS)
goto failure;
/*
* Collect all update RRs for this name and type
* onto u_rrs. No need to sort them here -
* they are already sorted.
*/
while (t != NULL &&
{
ISC_LIST_NEXT(t, link);
t = next;
}
/* Compare the two sorted lists. */
if (result != DNS_R_SUCCESS)
goto failure;
/*
* We are done with the tuples, but we can't free
* them yet because "name" still points into one
* of them. Move them on a temporary list.
*/
continue;
return (result);
}
}
return (DNS_R_SUCCESS);
}
/**************************************************************************/
/*
* Conditional deletion of RRs.
*/
/* Context structure for delete_if(). */
typedef struct {
/* Predicate functions for delete_if(). */
/* Return true iff 'update_rr' is neither a SOA nor an NS RR. */
static isc_boolean_t
{
}
/* Return true always. */
static isc_boolean_t
{
return (ISC_TRUE);
}
/* Return true iff the two RRs have identical rdata. */
static isc_boolean_t
/*
* XXXRTH This is not a problem, but we should consider creating
* dns_rdata_equal() (that used dns_name_equal()), since it
* would be faster. Not a priority.
*/
}
/*
* Return true iff 'update_rr' should replace 'db_rr' according
* to the special RFC2136 rules for CNAME, SOA, and WKS records.
*/
static isc_boolean_t
return (ISC_FALSE);
return (ISC_TRUE);
return (ISC_TRUE);
/*
* RFC2136 does not mention NXT, but multiple NXTs make little
* sense, so we replace those, too.
*/
return (ISC_TRUE);
/*
* Compare the address and protocol fields only. These
* form the first five bytes of the RR data. Do a
* raw binary comparison; unpacking the WKS RRs using
* dns_rdata_tostruct() might be cleaner in some ways,
* but it would require us to pass around an mctx.
*/
}
return (ISC_FALSE);
}
/* Internal helper function for delete_if(). */
static dns_result_t
return (result);
} else {
return (DNS_R_SUCCESS);
}
}
/*
* Conditionally delete RRs. Apply 'predicate' to the RRs
* specified by 'db', 'ver', 'name', and 'type' (which can
* be dns_rdatatype_any to match any type). Delete those
* RRs for which the predicate returns true, and log the
* deletions in 'diff'.
*/
static dns_result_t
{
delete_if_action, &ctx));
}
/**************************************************************************/
/*
* Miscellaneous subroutines.
*/
/*
* Extract a single update RR from 'section' of dynamic update message
* 'msg', with consistency checking.
*
* Stores the owner name, rdata, and TTL of the update RR at 'name',
* 'rdata', and 'ttl', respectively.
*/
static void
{
}
/*
* Increment the SOA serial number of database 'db', version 'ver'.
* Replace the SOA record in the database, and log the
* change in 'diff'.
*/
/*
* XXXRTH Failures in this routine will be worth logging, when
* we have a logging system. Failure to find the zonename
* or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
*/
static dns_result_t
{
/* RFC1982 */
if (serial == 0)
serial = 1;
return (result);
}
/*
* Check that the new SOA record at 'update_rdata' does not
* illegally cause the SOA serial number to decrease relative to the
* existing SOA in 'db'.
*
* Sets '*changed' to ISC_TRUE if the update changed the serial
* number, to ISC_FALSE if not.
*
* Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
*/
static dns_result_t
{
if (result != DNS_R_SUCCESS)
return (result);
if (db_serial != update_serial) {
} else {
}
} else {
}
return (DNS_R_SUCCESS);
}
/**************************************************************************/
/*
* The actual update code in all its glory. We try to follow
* the RFC2136 pseudocode as closely as possible.
*/
static dns_result_t
{
unsigned int response_rcode = dns_rcode_noerror;
printf("got update request\n");
/*
* Interpret the zone section.
*/
if (result != DNS_R_SUCCESS)
"update zone section empty");
/*
* The zone section must contain exactly one "question", and
* it must be of type SOA.
*/
"update zone section contains non-SOA");
"update zone section contains multiple RRs");
/* The zone section must have exactly one name. */
if (result != DNS_R_NOMORE)
"update zone section contains multiple RRs");
/* XXX check that the zone is a master zone,
forward request if slave */
/* XXX we should get a class-specific dbtable from the view */
if (result != DNS_R_SUCCESS)
"not authoritative for update zone");
/* XXX this should go away when caches are no longer in the dbtable */
if (dns_db_iscache(db))
"not authoritative for update zone");
printf("zone section checked out OK\n");
/* Create a new database version. */
/* XXX should queue an update event here if someone else
has a writable version open */
printf("new database version created\n");
/* Check prerequisites. */
result == DNS_R_SUCCESS;
{
if (ttl != 0)
"prereq name out of zone");
if (update_class == dns_rdataclass_any) {
"prereq data not empty");
if (! flag) {
"'name in use' prereq "
"not satisfied");
}
} else {
if (! flag) {
/* RRset does not exist. */
"'rrset exists (value independent)' "
"prereq not satisfied");
}
}
} else if (update_class == dns_rdataclass_none) {
"prereq data not empty");
if (flag) {
"'name not in use' prereq "
"not satisfied");
}
} else {
if (flag) {
/* RRset exists. */
"'rrset does not exist' "
"prereq not satisfied");
}
}
} else if (update_class == zoneclass) {
/* "temp<rr.name, rr.type> += rr;" */
if (result != DNS_R_SUCCESS) {
"temp entry creation failed: %s",
}
} else {
}
}
if (result != DNS_R_NOMORE)
/*
* Perform the final check of the "rrset exists (value dependent)"
* prerequisites.
*/
if (result != DNS_R_SUCCESS)
"not satisfied");
printf("prereqs ok\n");
/* XXX Check Requestor's Permissions Here */
/* Perform the Update Section Prescan. */
result == DNS_R_SUCCESS;
{
"update RR is outside zone");
if (update_class == zoneclass) {
/*
* Check for meta-RRs. The RFC2136 pseudocode says
* check for ANY|AXFR|MAILA|MAILB, but the text adds
* "or any other QUERY metatype"
*/
"meta-RR in update");
}
} else if (update_class == dns_rdataclass_any) {
"meta-RR in update");
} else if (update_class == dns_rdataclass_none) {
if (ttl != 0 ||
"meta-RR in update");
} else {
printf("update RR has incorrect class %d\n",
}
}
if (result != DNS_R_NOMORE)
printf("prescan ok\n");
/* Process the Update Section. */
result == DNS_R_SUCCESS;
{
if (update_class == zoneclass) {
name,
&flag));
if (flag) {
printf("attempt to add cname "
"alongside non-cname "
"ignored\n");
continue;
}
} else {
&flag));
printf("attempt to add non-cname "
"alongside cname ignored\n");
continue;
}
}
&flag));
if (! flag) {
printf("attempt to create extra SOA "
"ignored\n");
continue;
}
if (! ok) {
printf("attempt to decrement SOA "
"serial ignored\n");
continue;
}
if (changed)
}
/*
* Add an RR. If an identical RR already exists,
* do nothing. If a similar but not identical
* CNAME, SOA, or WKS exists, remove it first.
*/
&flag));
if (! flag) {
printf("add an RR\n");
&diff));
if (result != DNS_R_SUCCESS)
} else {
printf("attempt to add existing RR ignored\n");
}
} else if (update_class == dns_rdataclass_any) {
printf("delete all rrsets from a name\n");
} else {
}
printf("attempt to delete all SOA or NS "
"records ignored\n");
continue;
} else {
printf("delete an rrset\n");
&diff));
}
} else if (update_class == dns_rdataclass_none) {
printf("attempt to delete SOA ignored\n");
continue;
}
int count;
dns_rdatatype_ns, 0, &count));
if (count == 1) {
printf("attempt to delete last "
"NS ignored\n");
continue;
}
}
printf("delete an RR\n");
}
}
if (result != DNS_R_NOMORE)
/*
* If any changes were made, increment the SOA serial number
* and write the update to the journal.
*/
/*
* Increment the SOA serial, but only if it was not changed as
* a result of an update operation.
*/
if (! soa_serial_changed) {
}
printf("write journal\n");
/* XXX use a real file name */
if (result != DNS_R_SUCCESS)
if (result != DNS_R_SUCCESS) {
}
}
/*
* XXXRTH Just a note that this committing code will have to change
* to handle databases that need two-phase commit, but this
* isn't a priority.
*/
printf("commit\n");
goto common;
printf("rollback\n");
}
dns_db_detach(&db);
/*
* Construct the response message.
*/
&response);
if (render_result != DNS_R_SUCCESS)
goto render_failure;
goto render_success;
/*
* If we could send a response, we have succeded, even if it
* was a failure response.
*/
return (render_result);
}
void
}