Commit 5fc7ba3e authored by Andreas Gustafsson's avatar Andreas Gustafsson

added dns_db_dump(), $DATE, and supporting changes

parent f28a94af
......@@ -144,7 +144,7 @@ select_db(char *origintext) {
}
static void
dump(dbinfo *dbi) {
list(dbinfo *dbi) {
dns_fixedname_t fname;
dns_name_t *name;
dns_dbnode_t *node;
......@@ -586,7 +586,13 @@ main(int argc, char *argv[]) {
continue;
} else if (strcmp(s, "!LS") == 0) {
DBI_CHECK(dbi);
dump(dbi);
list(dbi);
continue;
} else if (strstr(s, "!DU ") == s) {
DBI_CHECK(dbi);
result = dns_db_dump(dbi->db, dbi->version, s+4);
if (result != DNS_R_SUCCESS)
printf("\n%s\n", dns_result_totext(result));
continue;
} else if (strstr(s, "!P") == s) {
DBI_CHECK(dbi);
......
......@@ -100,6 +100,7 @@ test_master(char *testfile, char *origin, dns_result_t exp_result) {
&dns_origin,
&dns_origin,
1,
ISC_FALSE,
&soacount,
&nscount,
&callbacks,
......
......@@ -90,8 +90,8 @@ main(int argc, char *argv[]) {
dns_rdatacallbacks_init(&callbacks);
callbacks.commit = print_dataset;
result = dns_master_load(argv[1], &origin, &origin, 1,
&soacount, &nscount,
result = dns_master_load(argv[1], &origin, &origin, 1, ISC_FALSE,
&soacount, &nscount,
&callbacks, mctx);
fprintf(stdout, "dns_master_load: %s\n",
dns_result_totext(result));
......
......@@ -36,12 +36,12 @@ LIBS = @LIBS@
OBJS = callbacks.o compress.o db.o dbiterator.o dbtable.o \
master.o message.o name.o rbt.o rbtdb.o rbtdb64.o \
rdata.o rdatalist.o rdataset.o rdatasetiter.o rdataslab.o \
result.o version.o
result.o version.o masterdump.o time.o ttl.o
OSRC = callbacks.c compress.c db.c dbiterator.c dbtable.c \
master.c name.c rbt.c rbtdb.c rbtdb64.c \
rdata.c rdatalist.c rdataset.c rdatasetiter.c rdataslab.c \
result.c version.c
result.c version.c masterdump.c time.c ttl.c
SUBDIRS = include
TARGETS = include/dns/enumtype.h include/dns/enumclass.h \
......
......@@ -180,6 +180,17 @@ dns_db_load(dns_db_t *db, char *filename) {
return (db->methods->load(db, filename));
}
dns_result_t
dns_db_dump(dns_db_t *db, dns_dbversion_t *version, char *filename) {
/*
* Dump 'db' into master file 'filename'.
*/
REQUIRE(DNS_DB_VALID(db));
return (db->methods->dump(db, version, filename));
}
/***
*** Version Methods
***/
......
......@@ -15,7 +15,7 @@
* SOFTWARE.
*/
/* $Id: gen.c,v 1.19 1999/05/07 03:24:04 marka Exp $ */
/* $Id: gen.c,v 1.20 1999/06/08 10:35:04 gson Exp $ */
#include <sys/types.h>
......@@ -35,8 +35,8 @@
#define FROMTEXTTYPE "type"
#define FROMTEXTDEF "use_default = ISC_TRUE"
#define TOTEXTDECL "dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target"
#define TOTEXTARGS "rdata, origin, target"
#define TOTEXTDECL "dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, isc_buffer_t *target"
#define TOTEXTARGS "rdata, tctx, target"
#define TOTEXTCLASS "rdata->class"
#define TOTEXTTYPE "rdata->type"
#define TOTEXTDEF "use_default = ISC_TRUE"
......
......@@ -74,6 +74,8 @@ typedef struct dns_dbmethods {
void (*attach)(dns_db_t *source, dns_db_t **targetp);
void (*detach)(dns_db_t **dbp);
dns_result_t (*load)(dns_db_t *db, char *filename);
dns_result_t (*dump)(dns_db_t *db, dns_dbversion_t *version,
char *filename);
void (*currentversion)(dns_db_t *db,
dns_dbversion_t **versionp);
dns_result_t (*newversion)(dns_db_t *db,
......@@ -320,6 +322,26 @@ dns_db_load(dns_db_t *db, char *filename);
* implementation used, syntax errors in the master file, etc.
*/
dns_result_t
dns_db_dump(dns_db_t *db, dns_dbversion_t *version, char *filename);
/*
* Dump version 'version' of 'db' to master file 'filename'.
*
* Requires:
*
* 'db' is a valid database.
*
* 'version' is a valid version.
*
* Returns:
*
* DNS_R_SUCCESS
* DNS_R_NOMEMORY
*
* Other results are possible, depending upon the database
* implementation used, OS file errors, etc.
*/
/***
*** Version Methods
***/
......
......@@ -41,7 +41,8 @@ ISC_LANG_BEGINDECLS
dns_result_t dns_master_load(char *master_file,
dns_name_t *top,
dns_name_t *origin,
dns_rdataclass_t class,
dns_rdataclass_t zclass,
isc_boolean_t age_ttl,
int *soacount,
int *nscount,
dns_rdatacallbacks_t *callbacks,
......@@ -54,6 +55,9 @@ dns_result_t dns_master_load(char *master_file,
* completes. dns_load_master will abort if callbacks->commit returns
* any value other than DNS_R_SUCCESS.
*
* If 'age_ttl' is ISC_TRUE and the master file contains one or more
* $DATE directives, the TTLs of the data will be aged accordingly.
*
* 'callbacks->commit' is assumed to call 'callbacks->error' or
* 'callbacks->warn' to generate any error messages required.
*
......
/*
* 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.
*/
#ifndef DNS_MASTERDUMP_H
#define DNS_MASTERDUMP_H 1
/***
*** Imports
***/
#include <stdio.h>
#include <isc/lang.h>
#include <isc/mem.h>
#include <dns/types.h>
#include <dns/result.h>
#include <dns/name.h>
#include <dns/rdataset.h>
/***
*** Types
***/
/*
* Style options for masterfile dumps. This struct is currently
* opaque, so applications cannot define their own style but have
* to choose a predefined style. A more flexible interface may
* be exported in the future.
*/
typedef struct dns_master_style dns_master_style_t;
ISC_LANG_BEGINDECLS
/***
*** Constants
***/
/*
* The default masterfile style.
*/
extern const dns_master_style_t dns_master_style_default;
/***
*** Functions
***/
dns_result_t
dns_master_dump(isc_mem_t *mctx, dns_db_t *db,
dns_dbversion_t *version,
const dns_master_style_t *style, char *filename);
/*
* Dump the database 'db' to the file 'filename' in RFC1035 master
* file format, in the style defined by 'style'
* (e.g., &dns_default_master_style_default)
*
* Temporary dynamic memory may be allocated from 'mctx'.
*
* Returns:
* DNS_R_SUCCESS
* DNS_R_NOMEMORY
* Any database or rrset iterator error.
* Any dns_rdata_totext() error code.
*/
ISC_LANG_ENDDECLS
#endif /* DNS_MASTERDUMP_H */
......@@ -126,6 +126,30 @@ struct dns_rdata {
ISC_LINK(dns_rdata_t) link;
};
/*
* Context structure for dns_rdata_totext(). Defines
* the way the rdata part of a master file lines is
* formatted.
*/
typedef struct dns_rdata_textctx {
dns_name_t *origin; /* Current origin, or NULL. */
unsigned int flags; /* DNS_STYLEFLAG_* */
unsigned int width; /* Width of rdata column. */
char *linebreak; /* Line break string. */
} dns_rdata_textctx_t;
/*
* Flags affecting rdata formatting. Flags 0xFFFF0000
* are used by masterfile-level formatting and defined elsewhere.
*/
/* Split the rdata into multiple lines to try to keep it
within the "width". */
#define DNS_STYLEFLAG_MULTILINE 0x00000001U
/* Output explanatory comments. */
#define DNS_STYLEFLAG_COMMENT 0x00000002U
/***
*** Initialization
***/
......@@ -299,7 +323,7 @@ dns_result_t dns_rdata_fromtext(dns_rdata_t *rdata,
* Resource Limit: Not enough space
*/
dns_result_t dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin,
dns_result_t dns_rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
isc_buffer_t *target);
/*
* Convert 'rdata' into text format, storing the result in 'target'.
......
/*
* 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.
*/
#ifndef DNS_TIME_H
#define DNS_TIME_H 1
/***
*** Imports
***/
#include <isc/lang.h>
#include <isc/int.h>
#include <isc/buffer.h>
ISC_LANG_BEGINDECLS
/***
*** Functions
***/
dns_result_t dns_time64_fromtext(char *source, isc_int64_t *target);
/*
* Convert a date and time in YYYYMMDDHHMMSS text format at 'source'
* into to a 64-bit count of seconds since Jan 1 1970 0:00 GMT.
* Store the count at 'target'.
*/
dns_result_t dns_time32_fromtext(char *source, isc_uint32_t *target);
/*
* Like dns_time64_fromtext, but returns the second count modulo 2^32
* as per RFC2535.
*/
dns_result_t dns_time64_totext(isc_int64_t value, isc_buffer_t *target);
/*
* Convert a 64-bit count of seconds since Jan 1 1970 0:00 GMT into
* a YYYYMMDDHHMMSS text representation and append it to 'target'.
*/
dns_result_t dns_time32_totext(isc_uint32_t value, isc_buffer_t *target);
/*
* Like dns_time64_totext, but for a 32-bit cyclic time value.
* Of those dates whose counts of seconds since Jan 1 1970 0:00 GMT
* are congruent with 'value' modulo 2^32, the one closest to the
* current date is chosen.
*/
ISC_LANG_ENDDECLS
#endif /* DNS_TIME_H */
/*
* 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.
*/
#ifndef DNS_TTL_H
#define DNS_TTL_H 1
/***
*** Imports
***/
#include <isc/types.h>
#include <isc/buffer.h>
ISC_LANG_BEGINDECLS
/***
*** Functions
***/
dns_result_t
dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose,
isc_buffer_t *target);
/*
* Output a TTL or other time interval in a human-readable form.
* The time interval is given as a count of seconds in 'src'.
* The text representation is appended to 'target'.
*
* If 'verbose' is ISC_FALSE, use the terse BIND 8 style, like "1w2d3h4m5s".
*
* If 'verbose' is ISC_TRUE, use a verbose style like the SOA comments
* in "dig", like "1 week 2 days 3 hours 4 minutes 5 seconds".
*
* Returns:
* DNS_R_SUCCESS
* DNS_R_NOSPACE
*/
ISC_LANG_ENDDECLS
#endif /* DNS_MASTER_H */
......@@ -15,7 +15,7 @@
* SOFTWARE.
*/
/* $Id: master.c,v 1.17 1999/04/25 22:18:11 marka Exp $ */
/* $Id: master.c,v 1.18 1999/06/08 10:35:04 gson Exp $ */
#include <config.h>
......@@ -23,12 +23,14 @@
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include <isc/lex.h>
#include <isc/list.h>
#include <isc/mem.h>
#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/stdtime.h>
#include <dns/master.h>
#include <dns/types.h>
......@@ -38,6 +40,7 @@
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
#include <dns/rdata.h>
#include <dns/time.h>
/*
* Grow the number of dns_rdatalist_t (RDLSZ) and dns_rdata_t (RDSZ) structures
......@@ -132,13 +135,15 @@ gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *token,
dns_result_t
dns_master_load(char *master_file, dns_name_t *top, dns_name_t *origin,
dns_rdataclass_t zclass, int *soacount, int *nscount,
dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
dns_rdataclass_t zclass, isc_boolean_t age_ttl,
int *soacount, int *nscount, dns_rdatacallbacks_t *callbacks,
isc_mem_t *mctx)
{
dns_rdataclass_t rdclass;
dns_rdatatype_t type;
isc_uint32_t ttl = 0;
isc_uint32_t default_ttl = 0;
isc_uint32_t ttl_offset = 0;
dns_name_t current_name;
dns_name_t glue_name;
dns_name_t new_name;
......@@ -316,6 +321,14 @@ dns_master_load(char *master_file, dns_name_t *top, dns_name_t *origin,
continue;
} else if (strcasecmp(token.value.as_pointer,
"$INCLUDE") == 0) {
if (ttl_offset != 0) {
(callbacks->error)(callbacks,
"dns_load_master: %s:%d: $INCLUDE "
"may not be used with $DATE",
master_file,
isc_lex_getsourceline(lex));
goto cleanup;
}
GETTOKEN(lex, 0, &token, ISC_FALSE);
if (include_file != NULL)
isc_mem_free(mctx, include_file);
......@@ -332,6 +345,7 @@ dns_master_load(char *master_file, dns_name_t *top, dns_name_t *origin,
top,
&origin_name,
zclass,
age_ttl,
soacount,
nscount,
callbacks,
......@@ -343,6 +357,34 @@ dns_master_load(char *master_file, dns_name_t *top, dns_name_t *origin,
}
read_till_eol = ISC_TRUE;
finish_include = ISC_TRUE;
} else if (strcasecmp(token.value.as_pointer,
"$DATE") == 0) {
isc_uint64_t dump_time64;
isc_stdtime_t dump_time, current_time;
GETTOKEN(lex, 0, &token, ISC_FALSE);
iresult = isc_stdtime_get(&current_time);
result = dns_time64_fromtext(token.value.
as_pointer, &dump_time64);
dump_time = dump_time64;
if (dump_time != dump_time64) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"dns_master_load: %s:%d: "
"$DATE outside epoch",
master_file,
isc_lex_getsourceline(lex));
goto cleanup;
}
if (dump_time > current_time) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"dns_master_load: %s:%d: "
"$DATE in future, using current date",
master_file,
isc_lex_getsourceline(lex));
dump_time = current_time;
}
ttl_offset = current_time - dump_time;
read_till_eol = ISC_TRUE;
continue;
}
/*
......@@ -386,6 +428,7 @@ dns_master_load(char *master_file, dns_name_t *top, dns_name_t *origin,
top,
&new_name,
zclass,
age_ttl,
soacount,
nscount,
callbacks,
......@@ -597,6 +640,20 @@ dns_master_load(char *master_file, dns_name_t *top, dns_name_t *origin,
if (type == dns_rdatatype_ns && !in_glue)
current_has_delegation = ISC_TRUE;
if (age_ttl) {
/*
* Adjust the TTL for $DATE. If the RR has already
* expired, ignore it without even parsing the rdata
* part (good for performance, bad for catching
* syntax errors).
*/
if (ttl < ttl_offset) {
read_till_eol = ISC_TRUE;
continue;
}
ttl -= ttl_offset;
}
/*
* Find type in rdatalist.
* If it does not exit create new one and prepend to list
......
This diff is collapsed.
......@@ -40,6 +40,7 @@
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/masterdump.h>
#ifdef DNS_RBTDB_VERSION64
#include "rbtdb64.h"
......@@ -2550,23 +2551,21 @@ load(dns_db_t *db, char *filename) {
dns_rdatacallbacks_t callbacks;
dns_result_t result;
dns_name_t name;
isc_boolean_t age_ttl;
rbtdb = (dns_rbtdb_t *)db;
REQUIRE(VALID_RBTDB(rbtdb));
loadctx.rbtdb = rbtdb;
if ((rbtdb->common.attributes & DNS_DBATTR_CACHE) != 0) {
/*
* XXXRTH This is not quite right. We should probably
* use the last file modification time or perhaps the value
* of an new master file option (e.g. $TIME) as the 'now'
* time.
*/
if (isc_stdtime_get(&loadctx.now) != DNS_R_SUCCESS)
return (DNS_R_UNEXPECTED);
} else
age_ttl = ISC_TRUE;
} else {
loadctx.now = 0;
age_ttl = ISC_FALSE;
}
LOCK(&rbtdb->lock);
......@@ -2613,10 +2612,23 @@ load(dns_db_t *db, char *filename) {
return (dns_master_load(filename, &rbtdb->common.origin,
&rbtdb->common.origin, rbtdb->common.rdclass,
&soacount, &nscount, &callbacks,
age_ttl, &soacount, &nscount, &callbacks,
rbtdb->common.mctx));
}
static dns_result_t
dump(dns_db_t *db, dns_dbversion_t *version, char *filename) {
dns_rbtdb_t *rbtdb;
rbtdb = (dns_rbtdb_t *)db;
REQUIRE(VALID_RBTDB(rbtdb));
return (dns_master_dump(rbtdb->common.mctx, db, version,
&dns_master_style_default,
filename));
}
static void
delete_callback(void *data, void *arg) {
dns_rbtdb_t *rbtdb = arg;
......@@ -2632,6 +2644,7 @@ static dns_dbmethods_t zone_methods = {
attach,
detach,
load,
dump,
currentversion,
newversion,
attachversion,
......@@ -2653,6 +2666,7 @@ static dns_dbmethods_t cache_methods = {
attach,
detach,
load,
dump,
currentversion,
newversion,
attachversion,
......
......@@ -15,13 +15,12 @@
* SOFTWARE.