ldns-read-zone.c
Go to the documentation of this file.
1 /*
2  * read a zone file from disk and prints it, one RR per line
3  *
4  * (c) NLnetLabs 2005-2008
5  *
6  * See the file LICENSE for the license
7  */
8 
9 #include "config.h"
10 #include <unistd.h>
11 #include <stdlib.h>
12 
13 #include <ldns/ldns.h>
14 #include <ldns/host2str.h>
15 
16 #include <errno.h>
17 
18 static void print_usage(const char* progname)
19 {
20  printf("Usage: %s [OPTIONS] <zonefile>\n", progname);
21  printf("\tReads the zonefile and prints it.\n");
22  printf("\tThe RR count of the zone is printed to stderr.\n");
23  printf("\t-0 zeroize timestamps and signature in RRSIG records.\n");
24  printf("\t-b include Bubble Babble encoding of DS's.\n");
25  printf("\t-c canonicalize all rrs in the zone.\n");
26  printf("\t-d only show DNSSEC data from the zone\n");
27  printf("\t-e <rr type>\n");
28  printf("\t\tDo not print RRs of the given <rr type>.\n");
29  printf("\t\tThis option may be given multiple times.\n");
30  printf("\t\t-e is not meant to be used together with -E.\n");
31  printf("\t-E <rr type>\n");
32  printf("\t\tPrint only RRs of the given <rr type>.\n");
33  printf("\t\tThis option may be given multiple times.\n");
34  printf("\t\t-E is not meant to be used together with -e.\n");
35  printf("\t-h show this text\n");
36  printf("\t-n do not print the SOA record\n");
37  printf("\t-p prepend SOA serial with spaces so"
38  " it takes exactly ten characters.\n");
39  printf("\t-s strip DNSSEC data from the zone\n");
40  printf("\t-S [[+|-]<number> | YYYYMMDDxx | "
41  " unixtime ]\n"
42  "\t\tSet serial number to <number> or,"
43  " when preceded by a sign,\n"
44  "\t\toffset the existing number with "
45  "<number>. With YYYYMMDDxx\n"
46  "\t\tthe serial is formatted as a datecounter"
47  ", and with unixtime as\n"
48  "\t\tthe number of seconds since 1-1-1970."
49  " However, on serial\n"
50  "\t\tnumber decrease, +1 is used in stead"
51  ". (implies -s)\n");
52  printf("\t-u <rr type>\n");
53  printf("\t\tMark <rr type> for printing in unknown type format.\n");
54  printf("\t\tThis option may be given multiple times.\n");
55  printf("\t\t-u is not meant to be used together with -U.\n");
56  printf("\t-U <rr type>\n");
57  printf("\t\tMark <rr type> for not printing in unknown type format.\n");
58  printf("\t\tThis option may be given multiple times.\n");
59  printf(
60  "\t\tThe first occurrence of the -U option marks all RR types for"
61  "\n\t\tprinting in unknown type format except for the given <rr type>."
62  "\n\t\tSubsequent -U options will clear the mark for those <rr type>s"
63  "\n\t\ttoo, so that only the given <rr type>s will be printed in the"
64  "\n\t\tpresentation format specific for those <rr type>s.\n");
65  printf("\t\t-U is not meant to be used together with -u.\n");
66  printf("\t-v shows the version and exits\n");
67  printf("\t-z sort the zone (implies -c).\n");
68  printf("\nif no file is given standard input is read\n");
69  exit(EXIT_SUCCESS);
70 }
71 
72 static void exclude_type(ldns_rdf **show_types, ldns_rr_type t)
73 {
74  ldns_status s;
75 
76  assert(show_types != NULL);
77 
78  if (! *show_types && LDNS_STATUS_OK !=
79  (s = ldns_rdf_bitmap_known_rr_types(show_types)))
80  goto fail;
81 
82  s = ldns_nsec_bitmap_clear_type(*show_types, t);
83  if (s == LDNS_STATUS_OK)
84  return;
85 fail:
86  fprintf(stderr, "Cannot exclude rr type %s: %s\n"
87  , ldns_rr_descript(t)->_name
89  exit(EXIT_FAILURE);
90 }
91 
92 static void include_type(ldns_rdf **show_types, ldns_rr_type t)
93 {
94  ldns_status s;
95 
96  assert(show_types != NULL);
97 
98  if (! *show_types && LDNS_STATUS_OK !=
99  (s = ldns_rdf_bitmap_known_rr_types_space(show_types)))
100  goto fail;
101 
102  s = ldns_nsec_bitmap_set_type(*show_types, t);
103  if (s == LDNS_STATUS_OK)
104  return;
105 fail:
106  fprintf(stderr, "Cannot exclude all rr types except %s: %s\n"
107  , ldns_rr_descript(t)->_name
109  exit(EXIT_FAILURE);
110 }
111 
112 int
113 main(int argc, char **argv)
114 {
115  char *filename;
116  FILE *fp;
117  ldns_zone *z;
118  int line_nr = 0;
119  int c;
120  bool canonicalize = false;
121  bool sort = false;
122  bool print_soa = true;
123  ldns_status s;
124  size_t i;
125  ldns_rr_list *stripped_list;
126  ldns_rr *cur_rr;
127  ldns_output_format_storage fmt_storage;
128  ldns_output_format* fmt = ldns_output_format_init(&fmt_storage);
129  ldns_rdf *show_types = NULL;
130 
131  ldns_soa_serial_increment_func_t soa_serial_increment_func = NULL;
132  int soa_serial_increment_func_data = 0;
133 
134  while ((c = getopt(argc, argv, "0bcde:E:hnpsS:u:U:vz")) != -1) {
135  switch(c) {
136  case '0':
138  break;
139  case 'b':
140  fmt->flags |=
143  break;
144  case 'c':
145  canonicalize = true;
146  break;
147  case 'd':
148  include_type(&show_types, LDNS_RR_TYPE_RRSIG);
149  include_type(&show_types, LDNS_RR_TYPE_NSEC);
150  include_type(&show_types, LDNS_RR_TYPE_NSEC3);
151  break;
152  case 'e':
153  exclude_type(&show_types,
154  ldns_get_rr_type_by_name(optarg));
155  break;
156  case 'E':
157  include_type(&show_types,
158  ldns_get_rr_type_by_name(optarg));
159  break;
160  case 'h':
161  print_usage("ldns-read-zone");
162  break;
163  case 'n':
164  print_soa = false;
165  break;
166  case 'p':
168  break;
169  case 's':
170  case 'S':
171  exclude_type(&show_types, LDNS_RR_TYPE_RRSIG);
172  exclude_type(&show_types, LDNS_RR_TYPE_NSEC);
173  exclude_type(&show_types, LDNS_RR_TYPE_NSEC3);
174  if (c == 's') break;
175  if (*optarg == '+' || *optarg == '-') {
176  soa_serial_increment_func_data =
177  atoi(optarg);
178  soa_serial_increment_func =
180  } else if (! strtok(optarg, "0123456789")) {
181  soa_serial_increment_func_data =
182  atoi(optarg);
183  soa_serial_increment_func =
185  } else if (!strcasecmp(optarg, "YYYYMMDDxx")){
186  soa_serial_increment_func =
188  } else if (!strcasecmp(optarg, "unixtime")){
189  soa_serial_increment_func =
191  } else {
192  fprintf(stderr, "-S expects a number "
193  "optionally preceded by a "
194  "+ or - sign to indicate an "
195  "offset, or the text YYYYMM"
196  "DDxx or unixtime\n");
197  exit(EXIT_FAILURE);
198  }
199  break;
200  case 'u':
202  ldns_get_rr_type_by_name(optarg));
203  if (s != LDNS_STATUS_OK) {
204  fprintf( stderr
205  , "Cannot set rr type %s "
206  "in output format to "
207  "print as unknown type: %s\n"
210  )->_name
212  );
213  exit(EXIT_FAILURE);
214  }
215  break;
216  case 'U':
218  ldns_get_rr_type_by_name(optarg));
219  if (s != LDNS_STATUS_OK) {
220  fprintf( stderr
221  , "Cannot set rr type %s "
222  "in output format to not "
223  "print as unknown type: %s\n"
226  )->_name
228  );
229  exit(EXIT_FAILURE);
230  }
231  break;
232  case 'v':
233  printf("read zone version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
234  exit(EXIT_SUCCESS);
235  break;
236  case 'z':
237  canonicalize = true;
238  sort = true;
239  break;
240  }
241  }
242  argc -= optind;
243  argv += optind;
244 
245  if (argc == 0) {
246  fp = stdin;
247  } else {
248  filename = argv[0];
249 
250  fp = fopen(filename, "r");
251  if (!fp) {
252  fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
253  exit(EXIT_FAILURE);
254  }
255  }
256 
257  s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);
258 
259  fclose(fp);
260  if (s != LDNS_STATUS_OK) {
261  fprintf(stderr, "%s at line %d\n",
263  line_nr);
264  exit(EXIT_FAILURE);
265  }
266 
267  if (show_types) {
268  if (print_soa)
269  print_soa = ldns_nsec_bitmap_covers_type(show_types,
271  stripped_list = ldns_rr_list_new();
272  while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z))))
273  if (ldns_nsec_bitmap_covers_type(show_types,
274  ldns_rr_get_type(cur_rr)))
275  ldns_rr_list_push_rr(stripped_list, cur_rr);
276  else
277  ldns_rr_free(cur_rr);
279  ldns_zone_set_rrs(z, stripped_list);
280  }
281 
282  if (canonicalize) {
284  for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
286  }
287  }
288  if (sort) {
289  ldns_zone_sort(z);
290  }
291 
292  if (print_soa && ldns_zone_soa(z)) {
293  if (soa_serial_increment_func) {
295  ldns_zone_soa(z)
296  , soa_serial_increment_func
297  , soa_serial_increment_func_data
298  );
299  }
300  ldns_rr_print_fmt(stdout, fmt, ldns_zone_soa(z));
301  }
302  ldns_rr_list_print_fmt(stdout, fmt, ldns_zone_rrs(z));
303 
305 
306  exit(EXIT_SUCCESS);
307 }
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:1393
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:1430
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:1468
@ LDNS_STATUS_OK
Definition: error.h:26
const char * ldns_get_errorstr_by_id(ldns_status err)
look up a descriptive text by each error.
Definition: error.c:196
enum ldns_enum_status ldns_status
Definition: error.h:148
host2str.h - txt presentation of RRs
#define LDNS_FMT_PAD_SOA_SERIAL
Definition: host2str.h:66
#define LDNS_COMMENT_BUBBLEBABBLE
Provide bubblebabble representation for DS RR's as comment.
Definition: host2str.h:56
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
#define LDNS_FMT_ZEROIZE_RRSIGS
Definition: host2str.h:65
#define LDNS_COMMENT_FLAGS
Show when a NSEC3 RR has the optout flag set as comment.
Definition: host2str.h:58
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:3444
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
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:3406
int main(int argc, char *argv[])
Definition: ldns-mx.c:20
Including this file will include all ldns files, and define some lookup tables.
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition: rr.c:1015
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:994
ldns_rr * ldns_rr_list_pop_rr(ldns_rr_list *rr_list)
pops the last rr from an rrlist.
Definition: rr.c:1181
const ldns_rr_descriptor * ldns_rr_descript(uint16_t type)
returns the resource record descriptor for the given rr type.
Definition: rr.c:2644
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition: rr.c:81
enum ldns_enum_rr_type ldns_rr_type
Definition: rr.h:251
@ LDNS_RR_TYPE_RRSIG
DNSSEC.
Definition: rr.h:170
@ LDNS_RR_TYPE_SOA
marks the start of a zone of authority
Definition: rr.h:90
@ LDNS_RR_TYPE_NSEC
Definition: rr.h:171
@ LDNS_RR_TYPE_NSEC3
Definition: rr.h:176
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:1136
void ldns_rr2canonical(ldns_rr *rr)
converts each dname in a rr to its canonical form.
Definition: rr.c:1785
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:961
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition: rr.c:947
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:2629
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
Definition: rr.c:1004
@ LDNS_RR_CLASS_IN
the Internet
Definition: rr.h:47
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:2635
ldns_rr_type ldns_get_rr_type_by_name(const char *name)
retrieves a rrtype by looking up its name.
Definition: rr.c:2702
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
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
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.
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
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
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
Output format struct with additional data for flags that use them.
Definition: host2str.h:103
Output format specifier.
Definition: host2str.h:89
int flags
Specification of how RR's should be formatted in text.
Definition: host2str.h:91
Resource record data field.
Definition: rdata.h:197
List or Set of Resource Records.
Definition: rr.h:346
Resource Record.
Definition: rr.h:318
DNS Zone.
Definition: zone.h:43
#define LDNS_VERSION
Definition: util.h:30
const char * ldns_version(void)
Show the internal library version.
Definition: util.c:160
void ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
Set the zone's contents.
Definition: zone.c:41
ldns_rr_list * ldns_zone_rrs(const ldns_zone *z)
Get a list of a zone's content.
Definition: zone.c:35
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_rr * ldns_zone_soa(const ldns_zone *z)
Return the soa record of a zone.
Definition: zone.c:17
void ldns_zone_sort(ldns_zone *zone)
Sort the rrs in a zone, with the current impl.
Definition: zone.c:359
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.