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 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-b include Bubble Babble encoding of DS's.\n");
24  printf("\t-0 zeroize timestamps and signature in RRSIG records.\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-h show this text\n");
28  printf("\t-n do not print the SOA record\n");
29  printf("\t-p prepend SOA serial with spaces so"
30  " it takes exactly ten characters.\n");
31  printf("\t-s strip DNSSEC data from the zone\n");
32  printf("\t-S [[+|-]<number> | YYYYMMDDxx | "
33  " unixtime ]\n"
34  "\t\tSet serial number to <number> or,"
35  " when preceded by a sign,\n"
36  "\t\toffset the existing number with "
37  "<number>. With YYYYMMDDxx\n"
38  "\t\tthe serial is formatted as a datecounter"
39  ", and with unixtime as\n"
40  "\t\tthe number of seconds since 1-1-1970."
41  " However, on serial\n"
42  "\t\tnumber decrease, +1 is used in stead"
43  ". (implies -s)\n");
44  printf("\t-u <rr type>\n");
45  printf("\t\tMark <rr type> for printing in unknown type format.\n");
46  printf("\t\tThis option may be given multiple times.\n");
47  printf("\t\t-u is not meant to be used together with -U.\n");
48  printf("\t-U <rr type>\n");
49  printf("\t\tMark <rr type> for not printing in unknown type format.\n");
50  printf("\t\tThis option may be given multiple times.\n");
51  printf(
52  "\t\tThe first occurrence of the -U option marks all RR types for"
53  "\n\t\tprinting in unknown type format except for the given <rr type>."
54  "\n\t\tSubsequent -U options will clear the mark for those <rr type>s"
55  "\n\t\ttoo, so that only the given <rr type>s will be printed in the"
56  "\n\t\tpresentation format specific for those <rr type>s.\n");
57  printf("\t\t-U is not meant to be used together with -u.\n");
58  printf("\t-v shows the version and exits\n");
59  printf("\t-z sort the zone (implies -c).\n");
60  printf("\nif no file is given standard input is read\n");
61  exit(EXIT_SUCCESS);
62 }
63 
64 int
65 main(int argc, char **argv)
66 {
67  char *filename;
68  FILE *fp;
69  ldns_zone *z;
70  int line_nr = 0;
71  int c;
72  bool canonicalize = false;
73  bool sort = false;
74  bool strip = false;
75  bool only_dnssec = false;
76  bool print_soa = true;
77  ldns_status s;
78  size_t i;
79  ldns_rr_list *stripped_list;
80  ldns_rr *cur_rr;
81  ldns_rr_type cur_rr_type;
82  ldns_output_format_storage fmt_storage;
83  ldns_output_format* fmt = ldns_output_format_init(&fmt_storage);
84 
85  ldns_soa_serial_increment_func_t soa_serial_increment_func = NULL;
86  int soa_serial_increment_func_data = 0;
87 
88  while ((c = getopt(argc, argv, "0bcdhnpsu:U:vzS:")) != -1) {
89  switch(c) {
90  case 'b':
91  fmt->flags |=
94  break;
95  case '0':
97  break;
98  case 'c':
99  canonicalize = true;
100  break;
101  case 'd':
102  only_dnssec = true;
103  if (strip) {
104  fprintf(stderr, "Warning: stripping both DNSSEC and non-DNSSEC records. Output will be sparse.\n");
105  }
106  break;
107  case 'h':
108  print_usage("ldns-read-zone");
109  break;
110  case 'n':
111  print_soa = false;
112  break;
113  case 'p':
115  break;
116  case 's':
117  strip = true;
118  if (only_dnssec) {
119  fprintf(stderr, "Warning: stripping both DNSSEC and non-DNSSEC records. Output will be sparse.\n");
120  }
121  break;
122  case 'u':
124  ldns_get_rr_type_by_name(optarg));
125  if (s != LDNS_STATUS_OK) {
126  fprintf( stderr
127  , "Cannot set rr type %s "
128  "in output format to "
129  "print as unknown type: %s\n"
132  )->_name
134  );
135  exit(EXIT_FAILURE);
136  }
137  break;
138  case 'U':
140  ldns_get_rr_type_by_name(optarg));
141  if (s != LDNS_STATUS_OK) {
142  fprintf( stderr
143  , "Cannot set rr type %s "
144  "in output format to not "
145  "print as unknown type: %s\n"
148  )->_name
150  );
151  exit(EXIT_FAILURE);
152  }
153  break;
154  case 'v':
155  printf("read zone version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
156  exit(EXIT_SUCCESS);
157  break;
158  case 'z':
159  canonicalize = true;
160  sort = true;
161  break;
162  case 'S':
163  strip = true;
164  if (*optarg == '+' || *optarg == '-') {
165  soa_serial_increment_func_data =
166  atoi(optarg);
167  soa_serial_increment_func =
169  } else if (! strtok(optarg, "0123456789")) {
170  soa_serial_increment_func_data =
171  atoi(optarg);
172  soa_serial_increment_func =
174  } else if (!strcasecmp(optarg, "YYYYMMDDxx")){
175  soa_serial_increment_func =
177  } else if (!strcasecmp(optarg, "unixtime")){
178  soa_serial_increment_func =
180  } else {
181  fprintf(stderr, "-S expects a number "
182  "optionally preceded by a "
183  "+ or - sign to indicate an "
184  "offset, or the text YYYYMM"
185  "DDxx or unixtime\n");
186  exit(EXIT_FAILURE);
187  }
188  break;
189  }
190  }
191 
192  argc -= optind;
193  argv += optind;
194 
195  if (argc == 0) {
196  fp = stdin;
197  } else {
198  filename = argv[0];
199 
200  fp = fopen(filename, "r");
201  if (!fp) {
202  fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
203  exit(EXIT_FAILURE);
204  }
205  }
206 
207  s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);
208 
209  fclose(fp);
210  if (s != LDNS_STATUS_OK) {
211  fprintf(stderr, "%s at %d\n",
213  line_nr);
214  exit(EXIT_FAILURE);
215  }
216 
217 
218  if (strip) {
219  stripped_list = ldns_rr_list_new();
220  while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z)))) {
221  cur_rr_type = ldns_rr_get_type(cur_rr);
222  if (cur_rr_type == LDNS_RR_TYPE_RRSIG ||
223  cur_rr_type == LDNS_RR_TYPE_NSEC ||
224  cur_rr_type == LDNS_RR_TYPE_NSEC3 ||
225  cur_rr_type == LDNS_RR_TYPE_NSEC3PARAM
226  ) {
227  ldns_rr_free(cur_rr);
228  } else {
229  ldns_rr_list_push_rr(stripped_list, cur_rr);
230  }
231  }
233  ldns_zone_set_rrs(z, stripped_list);
234  }
235  if (only_dnssec) {
236  stripped_list = ldns_rr_list_new();
237  while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z)))) {
238  cur_rr_type = ldns_rr_get_type(cur_rr);
239  if (cur_rr_type == LDNS_RR_TYPE_RRSIG ||
240  cur_rr_type == LDNS_RR_TYPE_NSEC ||
241  cur_rr_type == LDNS_RR_TYPE_NSEC3 ||
242  cur_rr_type == LDNS_RR_TYPE_NSEC3PARAM
243  ) {
244  ldns_rr_list_push_rr(stripped_list, cur_rr);
245  } else {
246  ldns_rr_free(cur_rr);
247  }
248  }
250  ldns_zone_set_rrs(z, stripped_list);
251  }
252 
253  if (canonicalize) {
255  for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
257  }
258  }
259  if (sort) {
260  ldns_zone_sort(z);
261  }
262 
263  if (print_soa && ldns_zone_soa(z)) {
264  if (soa_serial_increment_func) {
266  ldns_zone_soa(z)
267  , soa_serial_increment_func
268  , soa_serial_increment_func_data
269  );
270  }
271  ldns_rr_print_fmt(stdout, fmt, ldns_zone_soa(z));
272  }
273  ldns_rr_list_print_fmt(stdout, fmt, ldns_zone_rrs(z));
274 
276 
277  exit(EXIT_SUCCESS);
278 }
void ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
Set the zone's contents.
Definition: zone.c:41
ldns_rr * ldns_zone_soa(const ldns_zone *z)
Return the soa record of a zone.
Definition: zone.c:17
Output format struct with additional data for flags that use them.
Definition: host2str.h:100
void ldns_rr2canonical(ldns_rr *rr)
converts each dname in a rr to its canonical form.
Definition: rr.c:1744
#define LDNS_COMMENT_BUBBLEBABBLE
Provide bubblebabble representation for DS RR's as comment.
Definition: host2str.h:56
DNSSEC.
Definition: rr.h:173
bool ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
pushes an rr to an rrlist.
Definition: rr.c:1096
List or Set of Resource Records.
Definition: rr.h:327
Output format specifier.
Definition: host2str.h:86
ldns_status ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, ldns_rdf *origin, uint32_t ttl, ldns_rr_class c __attribute__((unused)), int *line_nr)
Definition: zone.c:195
DNS Zone.
Definition: zone.h:42
#define LDNS_FMT_PAD_SOA_SERIAL
Definition: host2str.h:66
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition: rr.c:75
ldns_rr_type ldns_get_rr_type_by_name(const char *name)
retrieves a rrtype by looking up its name.
Definition: rr.c:2593
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition: rr.c:975
const ldns_rr_descriptor * ldns_rr_descript(uint16_t type)
returns the resource record descriptor for the given rr type.
Definition: rr.c:2535
void print_usage(const char *progname)
Resource Record.
Definition: rr.h:299
Including this file will include all ldns files, and define some lookup tables.
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:414
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:954
const char * ldns_version(void)
Show the internal library version.
Definition: util.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:2501
#define LDNS_VERSION
Definition: util.h:30
ldns_rr * ldns_rr_list_pop_rr(ldns_rr_list *rr_list)
pops the last rr from an rrlist.
Definition: rr.c:1141
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
Definition: rr.c:964
int flags
Specification of how RR's should be formatted in text.
Definition: host2str.h:89
the Internet
Definition: rr.h:50
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
host2str.h - txt presentation of RRs
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:313
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition: rr.c:907
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:354
enum ldns_enum_status ldns_status
Definition: error.h:131
ldns_status ldns_output_format_set_type(ldns_output_format *fmt, ldns_rr_type t)
Makes sure the LDNS_FMT_RFC3597 is set in the output format.
Definition: host2str.c:143
#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
ldns_status ldns_output_format_clear_type(ldns_output_format *fmt, ldns_rr_type t)
Makes sure the LDNS_FMT_RFC3597 is set in the output format.
Definition: host2str.c:163
ldns_rr_list * ldns_zone_rrs(const ldns_zone *z)
Get a list of a zone's content.
Definition: zone.c:35
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:921
enum ldns_enum_rr_type ldns_rr_type
Definition: rr.h:236
void ldns_zone_sort(ldns_zone *zone)
Sort the rrs in a zone, with the current impl.
Definition: zone.c:296
const char * ldns_get_errorstr_by_id(ldns_status err)
look up a descriptive text by each error.
Definition: error.c:150
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:359
uint32_t ldns_soa_serial_identity(uint32_t unused __attribute__((unused)), void *data)
Definition: rr_functions.c:344
int main(int argc, char **argv)
void ldns_rr_list_print_fmt(FILE *output, const ldns_output_format *fmt, const ldns_rr_list *lst)
print a rr_list to output
Definition: host2str.c:2539
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:371