ldns-read-zone.c
Go to the documentation of this file.
/*
* read a zone file from disk and prints it, one RR per line
*
* (c) NLnetLabs 2005-2008
*
* See the file LICENSE for the license
*/
#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <ldns/ldns.h>
#include <ldns/host2str.h>
#include <errno.h>
static void print_usage(const char* progname)
{
printf("Usage: %s [OPTIONS] <zonefile>\n", progname);
printf("\tReads the zonefile and prints it.\n");
printf("\tThe RR count of the zone is printed to stderr.\n");
printf("\t-0 zeroize timestamps and signature in RRSIG records.\n");
printf("\t-b include Bubble Babble encoding of DS's.\n");
printf("\t-c canonicalize all rrs in the zone.\n");
printf("\t-d only show DNSSEC data from the zone\n");
printf("\t-e <rr type>\n");
printf("\t\tDo not print RRs of the given <rr type>.\n");
printf("\t\tThis option may be given multiple times.\n");
printf("\t\t-e is not meant to be used together with -E.\n");
printf("\t-E <rr type>\n");
printf("\t\tPrint only RRs of the given <rr type>.\n");
printf("\t\tThis option may be given multiple times.\n");
printf("\t\t-E is not meant to be used together with -e.\n");
printf("\t-h show this text\n");
printf("\t-n do not print the SOA record\n");
printf("\t-p prepend SOA serial with spaces so"
" it takes exactly ten characters.\n");
printf("\t-s strip DNSSEC data from the zone\n");
printf("\t-S [[+|-]<number> | YYYYMMDDxx | "
" unixtime ]\n"
"\t\tSet serial number to <number> or,"
" when preceded by a sign,\n"
"\t\toffset the existing number with "
"<number>. With YYYYMMDDxx\n"
"\t\tthe serial is formatted as a datecounter"
", and with unixtime as\n"
"\t\tthe number of seconds since 1-1-1970."
" However, on serial\n"
"\t\tnumber decrease, +1 is used in stead"
". (implies -s)\n");
printf("\t-u <rr type>\n");
printf("\t\tMark <rr type> for printing in unknown type format.\n");
printf("\t\tThis option may be given multiple times.\n");
printf("\t\t-u is not meant to be used together with -U.\n");
printf("\t-U <rr type>\n");
printf("\t\tMark <rr type> for not printing in unknown type format.\n");
printf("\t\tThis option may be given multiple times.\n");
printf(
"\t\tThe first occurrence of the -U option marks all RR types for"
"\n\t\tprinting in unknown type format except for the given <rr type>."
"\n\t\tSubsequent -U options will clear the mark for those <rr type>s"
"\n\t\ttoo, so that only the given <rr type>s will be printed in the"
"\n\t\tpresentation format specific for those <rr type>s.\n");
printf("\t\t-U is not meant to be used together with -u.\n");
printf("\t-v shows the version and exits\n");
printf("\t-z sort the zone (implies -c).\n");
printf("\nif no file is given standard input is read\n");
exit(EXIT_SUCCESS);
}
static void exclude_type(ldns_rdf **show_types, ldns_rr_type t)
{
ldns_status s;
assert(show_types != NULL);
if (! *show_types && LDNS_STATUS_OK !=
(s = ldns_rdf_bitmap_known_rr_types(show_types)))
goto fail;
s = ldns_nsec_bitmap_clear_type(*show_types, t);
if (s == LDNS_STATUS_OK)
return;
fail:
fprintf(stderr, "Cannot exclude rr type %s: %s\n"
, ldns_rr_descript(t)->_name
, ldns_get_errorstr_by_id(s));
exit(EXIT_FAILURE);
}
static void include_type(ldns_rdf **show_types, ldns_rr_type t)
{
ldns_status s;
assert(show_types != NULL);
if (! *show_types && LDNS_STATUS_OK !=
(s = ldns_rdf_bitmap_known_rr_types_space(show_types)))
goto fail;
s = ldns_nsec_bitmap_set_type(*show_types, t);
if (s == LDNS_STATUS_OK)
return;
fail:
fprintf(stderr, "Cannot exclude all rr types except %s: %s\n"
, ldns_rr_descript(t)->_name
, ldns_get_errorstr_by_id(s));
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
char *filename;
FILE *fp;
ldns_zone *z;
int line_nr = 0;
int c;
bool canonicalize = false;
bool sort = false;
bool print_soa = true;
ldns_status s;
size_t i;
ldns_rr_list *stripped_list;
ldns_rr *cur_rr;
ldns_output_format_storage fmt_storage;
ldns_output_format* fmt = ldns_output_format_init(&fmt_storage);
ldns_rdf *show_types = NULL;
ldns_soa_serial_increment_func_t soa_serial_increment_func = NULL;
int soa_serial_increment_func_data = 0;
while ((c = getopt(argc, argv, "0bcde:E:hnpsS:u:U:vz")) != -1) {
switch(c) {
case '0':
fmt->flags |= LDNS_FMT_ZEROIZE_RRSIGS;
break;
case 'b':
fmt->flags |=
( LDNS_COMMENT_BUBBLEBABBLE |
LDNS_COMMENT_FLAGS );
break;
case 'c':
canonicalize = true;
break;
case 'd':
include_type(&show_types, LDNS_RR_TYPE_RRSIG);
include_type(&show_types, LDNS_RR_TYPE_NSEC);
include_type(&show_types, LDNS_RR_TYPE_NSEC3);
break;
case 'e':
exclude_type(&show_types,
ldns_get_rr_type_by_name(optarg));
break;
case 'E':
include_type(&show_types,
ldns_get_rr_type_by_name(optarg));
break;
case 'h':
print_usage("ldns-read-zone");
break;
case 'n':
print_soa = false;
break;
case 'p':
fmt->flags |= LDNS_FMT_PAD_SOA_SERIAL;
break;
case 's':
case 'S':
exclude_type(&show_types, LDNS_RR_TYPE_RRSIG);
exclude_type(&show_types, LDNS_RR_TYPE_NSEC);
exclude_type(&show_types, LDNS_RR_TYPE_NSEC3);
if (c == 's') break;
if (*optarg == '+' || *optarg == '-') {
soa_serial_increment_func_data =
atoi(optarg);
soa_serial_increment_func =
ldns_soa_serial_increment_by;
} else if (! strtok(optarg, "0123456789")) {
soa_serial_increment_func_data =
atoi(optarg);
soa_serial_increment_func =
ldns_soa_serial_identity;
} else if (!strcasecmp(optarg, "YYYYMMDDxx")){
soa_serial_increment_func =
ldns_soa_serial_datecounter;
} else if (!strcasecmp(optarg, "unixtime")){
soa_serial_increment_func =
ldns_soa_serial_unixtime;
} else {
fprintf(stderr, "-S expects a number "
"optionally preceded by a "
"+ or - sign to indicate an "
"offset, or the text YYYYMM"
"DDxx or unixtime\n");
exit(EXIT_FAILURE);
}
break;
case 'u':
s = ldns_output_format_set_type(fmt,
ldns_get_rr_type_by_name(optarg));
if (s != LDNS_STATUS_OK) {
fprintf( stderr
, "Cannot set rr type %s "
"in output format to "
"print as unknown type: %s\n"
, ldns_rr_descript(
ldns_get_rr_type_by_name(optarg)
)->_name
, ldns_get_errorstr_by_id(s)
);
exit(EXIT_FAILURE);
}
break;
case 'U':
s = ldns_output_format_clear_type(fmt,
ldns_get_rr_type_by_name(optarg));
if (s != LDNS_STATUS_OK) {
fprintf( stderr
, "Cannot set rr type %s "
"in output format to not "
"print as unknown type: %s\n"
, ldns_rr_descript(
ldns_get_rr_type_by_name(optarg)
)->_name
, ldns_get_errorstr_by_id(s)
);
exit(EXIT_FAILURE);
}
break;
case 'v':
printf("read zone version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
exit(EXIT_SUCCESS);
break;
case 'z':
canonicalize = true;
sort = true;
break;
}
}
argc -= optind;
argv += optind;
if (argc == 0) {
fp = stdin;
} else {
filename = argv[0];
fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
}
s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);
fclose(fp);
if (s != LDNS_STATUS_OK) {
fprintf(stderr, "%s at line %d\n",
ldns_get_errorstr_by_id(s),
line_nr);
exit(EXIT_FAILURE);
}
if (show_types) {
if (print_soa)
print_soa = ldns_nsec_bitmap_covers_type(show_types,
LDNS_RR_TYPE_SOA);
stripped_list = ldns_rr_list_new();
while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z))))
if (ldns_nsec_bitmap_covers_type(show_types,
ldns_rr_get_type(cur_rr)))
ldns_rr_list_push_rr(stripped_list, cur_rr);
else
ldns_rr_free(cur_rr);
ldns_rr_list_free(ldns_zone_rrs(z));
ldns_zone_set_rrs(z, stripped_list);
}
if (canonicalize) {
ldns_rr2canonical(ldns_zone_soa(z));
for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
ldns_rr2canonical(ldns_rr_list_rr(ldns_zone_rrs(z), i));
}
}
if (sort) {
ldns_zone_sort(z);
}
if (print_soa && ldns_zone_soa(z)) {
if (soa_serial_increment_func) {
ldns_rr_soa_increment_func_int(
ldns_zone_soa(z)
, soa_serial_increment_func
, soa_serial_increment_func_data
);
}
ldns_rr_print_fmt(stdout, fmt, ldns_zone_soa(z));
}
ldns_rr_list_print_fmt(stdout, fmt, ldns_zone_rrs(z));
ldns_zone_deep_free(z);
exit(EXIT_SUCCESS);
}
ldns_nsec_bitmap_covers_type
signed char ldns_nsec_bitmap_covers_type(const ldns_rdf *bitmap, ldns_rr_type type)
Check if RR type t is enumerated and set in the RR type bitmap rdf.
Definition dnssec.c:1395
ldns_nsec_bitmap_set_type
ldns_status ldns_nsec_bitmap_set_type(ldns_rdf *bitmap, ldns_rr_type type)
Checks if RR type t is enumerated in the type bitmap rdf and sets the bit.
Definition dnssec.c:1432
ldns_nsec_bitmap_clear_type
ldns_status ldns_nsec_bitmap_clear_type(ldns_rdf *bitmap, ldns_rr_type type)
Checks if RR type t is enumerated in the type bitmap rdf and clears the bit.
Definition dnssec.c:1470
LDNS_STATUS_OK
@ LDNS_STATUS_OK
Definition error.h:26
ldns_status
enum ldns_enum_status ldns_status
Definition error.h:152
ldns_get_errorstr_by_id
const char * ldns_get_errorstr_by_id(ldns_status err)
look up a descriptive text by each error.
Definition error.c:204
host2str.h
host2str.h - txt presentation of RRs
LDNS_FMT_PAD_SOA_SERIAL
#define LDNS_FMT_PAD_SOA_SERIAL
Definition host2str.h:66
LDNS_COMMENT_BUBBLEBABBLE
#define LDNS_COMMENT_BUBBLEBABBLE
Provide bubblebabble representation for DS RR's as comment.
Definition host2str.h:56
ldns_output_format_clear_type
ldns_status ldns_output_format_clear_type(ldns_output_format *fmt, ldns_rr_type type)
Makes sure the LDNS_FMT_RFC3597 is set in the output format.
Definition host2str.c:180
LDNS_FMT_ZEROIZE_RRSIGS
#define LDNS_FMT_ZEROIZE_RRSIGS
Definition host2str.h:65
LDNS_COMMENT_FLAGS
#define LDNS_COMMENT_FLAGS
Show when a NSEC3 RR has the optout flag set as comment.
Definition host2str.h:58
ldns_output_format_storage
struct ldns_struct_output_format_storage ldns_output_format_storage
Definition host2str.h:107
ldns_rr_list_print_fmt
void ldns_rr_list_print_fmt(FILE *output, const ldns_output_format *fmt, const ldns_rr_list *list)
print a rr_list to output
Definition host2str.c:3504
ldns_output_format_set_type
ldns_status ldns_output_format_set_type(ldns_output_format *fmt, ldns_rr_type type)
Makes sure the LDNS_FMT_RFC3597 is set in the output format.
Definition host2str.c:160
ldns_output_format
struct ldns_struct_output_format ldns_output_format
Definition host2str.h:95
ldns_rr_print_fmt
void ldns_rr_print_fmt(FILE *output, const ldns_output_format *fmt, const ldns_rr *rr)
Prints the data in the resource record to the given file stream (in presentation format).
Definition host2str.c:3466
ldns.h
Including this file will include all ldns files, and define some lookup tables.
main
int main(void)
Definition linktest.c:6
ldns_rdf
struct ldns_struct_rdf ldns_rdf
Definition rdata.h:211
ldns_rr_list_free
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition rr.c:1009
ldns_rr_free
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition rr.c:81
ldns_rr
struct ldns_struct_rr ldns_rr
Definition rr.h:346
ldns_rr_list_new
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
Definition rr.c:998
ldns_rr_list_pop_rr
ldns_rr * ldns_rr_list_pop_rr(ldns_rr_list *rr_list)
pops the last rr from an rrlist.
Definition rr.c:1175
ldns_rr_type
enum ldns_enum_rr_type ldns_rr_type
Definition rr.h:260
LDNS_RR_TYPE_RRSIG
@ LDNS_RR_TYPE_RRSIG
DNSSEC.
Definition rr.h:170
LDNS_RR_TYPE_SOA
@ LDNS_RR_TYPE_SOA
marks the start of a zone of authority
Definition rr.h:90
LDNS_RR_TYPE_NSEC
@ LDNS_RR_TYPE_NSEC
Definition rr.h:171
LDNS_RR_TYPE_NSEC3
@ LDNS_RR_TYPE_NSEC3
Definition rr.h:176
ldns_rr_list_push_rr
signed char ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
pushes an rr to an rrlist.
Definition rr.c:1130
ldns_rr2canonical
void ldns_rr2canonical(ldns_rr *rr)
converts each dname in a rr to its canonical form.
Definition rr.c:1827
ldns_rr_list_rr_count
size_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
returns the number of rr's in an rr_list.
Definition rr.c:955
ldns_rr_get_type
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition rr.c:941
ldns_rr_list
struct ldns_struct_rr_list ldns_rr_list
Definition rr.h:360
ldns_rr_list_rr
ldns_rr * ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
returns a specific rr of an rrlist.
Definition rr.c:988
ldns_rdf_bitmap_known_rr_types_space
ldns_status ldns_rdf_bitmap_known_rr_types_space(ldns_rdf **rdf)
Create a rr type bitmap rdf providing enough space to set all known (to ldns) rr types.
Definition rr.c:2722
LDNS_RR_CLASS_IN
@ LDNS_RR_CLASS_IN
the Internet
Definition rr.h:47
ldns_rr_descript
const ldns_rr_descriptor * ldns_rr_descript(uint16_t type)
returns the resource record descriptor for the given rr type.
Definition rr.c:2737
ldns_rdf_bitmap_known_rr_types
ldns_status ldns_rdf_bitmap_known_rr_types(ldns_rdf **rdf)
Create a rr type bitmap rdf with at least all known (to ldns) rr types set.
Definition rr.c:2728
ldns_get_rr_type_by_name
ldns_rr_type ldns_get_rr_type_by_name(const char *name)
retrieves a rrtype by looking up its name.
Definition rr.c:2795
ldns_soa_serial_unixtime
uint32_t ldns_soa_serial_unixtime(uint32_t s, void *data)
Function to be used with ldns_rr_soa_increment_func or ldns_rr_soa_increment_func_int to set the soa ...
Definition rr_functions.c:383
ldns_rr_soa_increment_func_int
void ldns_rr_soa_increment_func_int(ldns_rr *soa, ldns_soa_serial_increment_func_t f, int data)
Increment the serial number of the given SOA with the given function using data as an argument for th...
Definition rr_functions.c:426
ldns_soa_serial_identity
uint32_t ldns_soa_serial_identity(uint32_t unused, void *data)
Function to be used with dns_rr_soa_increment_func_int, to set the soa serial number.
ldns_soa_serial_increment_by
uint32_t ldns_soa_serial_increment_by(uint32_t s, void *data)
Function to be used with dns_rr_soa_increment_func_int, to increment the soa serial number with a cer...
Definition rr_functions.c:366
ldns_soa_serial_increment_func_t
uint32_t(* ldns_soa_serial_increment_func_t)(uint32_t, void *)
The type of function to be passed to ldns_rr_soa_increment_func, ldns_rr_soa_increment_func_data or l...
Definition rr_functions.h:266
ldns_soa_serial_datecounter
uint32_t ldns_soa_serial_datecounter(uint32_t s, void *data)
Function to be used with ldns_rr_soa_increment_func or ldns_rr_soa_increment_func_int to set the soa ...
Definition rr_functions.c:371
ldns_struct_output_format::flags
int flags
Specification of how RR's should be formatted in text.
Definition host2str.h:91
ldns_version
const char * ldns_version(void)
Show the internal library version.
Definition util.c:160
LDNS_VERSION
#define LDNS_VERSION
Definition util.h:30
ldns_zone_soa
ldns_rr * ldns_zone_soa(const ldns_zone *z)
Return the soa record of a zone.
Definition zone.c:17
ldns_zone_set_rrs
void ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
Set the zone's contents.
Definition zone.c:41
ldns_zone_rrs
ldns_rr_list * ldns_zone_rrs(const ldns_zone *z)
Get a list of a zone's content.
Definition zone.c:35
ldns_zone_deep_free
void ldns_zone_deep_free(ldns_zone *zone)
Frees the allocated memory for the zone, the soa rr in it, and the rr_list structure in it,...
Definition zone.c:376
ldns_zone_sort
void ldns_zone_sort(ldns_zone *zone)
Sort the rrs in a zone, with the current impl.
Definition zone.c:359
ldns_zone_new_frm_fp_l
ldns_status ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t ttl, ldns_rr_class c, int *line_nr)
Create a new zone from a file, keep track of the line numbering.
ldns_zone
struct ldns_struct_zone ldns_zone
Definition zone.h:50