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 int
19 main(int argc, char **argv)
20 {
21  char *filename;
22  FILE *fp;
23  ldns_zone *z;
24  int line_nr = 0;
25  int c;
26  bool canonicalize = false;
27  bool sort = false;
28  bool strip = false;
29  bool only_dnssec = false;
30  bool print_soa = true;
31  ldns_status s;
32  size_t i;
33  ldns_rr_list *stripped_list;
34  ldns_rr *cur_rr;
35  ldns_rr_type cur_rr_type;
36  ldns_output_format fmt = {
39  };
40  ldns_soa_serial_increment_func_t soa_serial_increment_func = NULL;
41  int soa_serial_increment_func_data = 0;
42 
43  while ((c = getopt(argc, argv, "0bcdhnpsvzS:")) != -1) {
44  switch(c) {
45  case 'b':
46  fmt.flags |=
49  break;
50  case '0':
52  break;
53  case 'c':
54  canonicalize = true;
55  break;
56  case 'd':
57  only_dnssec = true;
58  if (strip) {
59  fprintf(stderr, "Warning: stripping both DNSSEC and non-DNSSEC records. Output will be sparse.\n");
60  }
61  break;
62  case 'h':
63  printf("Usage: %s [OPTIONS] <zonefile>\n", argv[0]);
64  printf("\tReads the zonefile and prints it.\n");
65  printf("\tThe RR count of the zone is printed to stderr.\n");
66  printf("\t-b include Bubble Babble encoding of DS's.\n");
67  printf("\t-0 zeroize timestamps and signature in RRSIG records.\n");
68  printf("\t-c canonicalize all rrs in the zone.\n");
69  printf("\t-d only show DNSSEC data from the zone\n");
70  printf("\t-h show this text\n");
71  printf("\t-n do not print the SOA record\n");
72  printf("\t-p prepend SOA serial with spaces so"
73  " it takes exactly ten characters.\n");
74  printf("\t-s strip DNSSEC data from the zone\n");
75  printf("\t-S [[+|-]<number> | YYYYMMDDxx | "
76  " unixtime ]\n"
77  "\t\tSet serial number to <number> or,"
78  " when preceded by a sign,\n"
79  "\t\toffset the existing number with "
80  "<number>. With YYYYMMDDxx\n"
81  "\t\tthe serial is formatted as a datecounter"
82  ", and with unixtime as the\n"
83  "\t\tnumber of seconds since 1-1-1970."
84  " However, on serial number"
85  "\n\t\tdecrease, +1 is used in stead"
86  ". (implies -s)\n");
87  printf("\t-v shows the version and exits\n");
88  printf("\t-z sort the zone (implies -c).\n");
89  printf("\nif no file is given standard input is read\n");
90  exit(EXIT_SUCCESS);
91  break;
92  case 'n':
93  print_soa = false;
94  break;
95  case 'p':
97  break;
98  case 's':
99  strip = true;
100  if (only_dnssec) {
101  fprintf(stderr, "Warning: stripping both DNSSEC and non-DNSSEC records. Output will be sparse.\n");
102  }
103  break;
104  case 'v':
105  printf("read zone version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
106  exit(EXIT_SUCCESS);
107  break;
108  case 'z':
109  canonicalize = true;
110  sort = true;
111  break;
112  case 'S':
113  strip = true;
114  if (*optarg == '+' || *optarg == '-') {
115  soa_serial_increment_func_data =
116  atoi(optarg);
117  soa_serial_increment_func =
119  } else if (! strtok(optarg, "0123456789")) {
120  soa_serial_increment_func_data =
121  atoi(optarg);
122  soa_serial_increment_func =
124  } else if (!strcasecmp(optarg, "YYYYMMDDxx")){
125  soa_serial_increment_func =
127  } else if (!strcasecmp(optarg, "unixtime")){
128  soa_serial_increment_func =
130  } else {
131  fprintf(stderr, "-S expects a number "
132  "optionally preceded by a "
133  "+ or - sign to indicate an "
134  "offset, or the text YYYYMM"
135  "DDxx or unixtime\n");
136  exit(EXIT_FAILURE);
137  }
138  break;
139  }
140  }
141 
142  argc -= optind;
143  argv += optind;
144 
145  if (argc == 0) {
146  fp = stdin;
147  } else {
148  filename = argv[0];
149 
150  fp = fopen(filename, "r");
151  if (!fp) {
152  fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
153  exit(EXIT_FAILURE);
154  }
155  }
156 
157  s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);
158 
159  fclose(fp);
160  if (s != LDNS_STATUS_OK) {
161  fprintf(stderr, "%s at %d\n",
163  line_nr);
164  exit(EXIT_FAILURE);
165  }
166 
167 
168  if (strip) {
169  stripped_list = ldns_rr_list_new();
170  while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z)))) {
171  cur_rr_type = ldns_rr_get_type(cur_rr);
172  if (cur_rr_type == LDNS_RR_TYPE_RRSIG ||
173  cur_rr_type == LDNS_RR_TYPE_NSEC ||
174  cur_rr_type == LDNS_RR_TYPE_NSEC3 ||
175  cur_rr_type == LDNS_RR_TYPE_NSEC3PARAM
176  ) {
177  ldns_rr_free(cur_rr);
178  } else {
179  ldns_rr_list_push_rr(stripped_list, cur_rr);
180  }
181  }
183  ldns_zone_set_rrs(z, stripped_list);
184  }
185  if (only_dnssec) {
186  stripped_list = ldns_rr_list_new();
187  while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z)))) {
188  cur_rr_type = ldns_rr_get_type(cur_rr);
189  if (cur_rr_type == LDNS_RR_TYPE_RRSIG ||
190  cur_rr_type == LDNS_RR_TYPE_NSEC ||
191  cur_rr_type == LDNS_RR_TYPE_NSEC3 ||
192  cur_rr_type == LDNS_RR_TYPE_NSEC3PARAM
193  ) {
194  ldns_rr_list_push_rr(stripped_list, cur_rr);
195  } else {
196  ldns_rr_free(cur_rr);
197  }
198  }
200  ldns_zone_set_rrs(z, stripped_list);
201  }
202 
203  if (canonicalize) {
205  for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
207  }
208  }
209  if (sort) {
210  ldns_zone_sort(z);
211  }
212 
213  if (print_soa && ldns_zone_soa(z)) {
214  if (soa_serial_increment_func) {
216  ldns_zone_soa(z)
217  , soa_serial_increment_func
218  , soa_serial_increment_func_data
219  );
220  }
221  ldns_rr_print_fmt(stdout, &fmt, ldns_zone_soa(z));
222  }
223  ldns_rr_list_print_fmt(stdout, &fmt, ldns_zone_rrs(z));
224 
226 
227  exit(EXIT_SUCCESS);
228 }
ldns_rr_list * ldns_rr_list_new()
creates a new rr_list structure.
Definition: rr.c:939
void ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
Set the zone&#39;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
void ldns_rr2canonical(ldns_rr *rr)
converts each dname in a rr to its canonical form.
Definition: rr.c:1737
#define LDNS_COMMENT_BUBBLEBABBLE
Provide bubblebabble representation for DS RR&#39;s as comment.
Definition: host2str.h:58
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:1071
List or Set of Resource Records.
Definition: rr.h:306
Output format specifier.
Definition: host2str.h:80
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:292
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
DNS Zone.
Definition: zone.h:42
#define LDNS_FMT_PAD_SOA_SERIAL
Definition: host2str.h:68
int main(void)
Definition: linktest.c:6
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition: rr.c:75
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition: rr.c:950
const ldns_output_format * ldns_output_format_default
The default output format record.
Definition: host2str.c:125
Resource Record.
Definition: rr.h:278
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:929
const char * ldns_version(void)
Show the internal library version.
Definition: util.c:196
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:2228
#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:1116
void * data
Potential extra data to be used with formatting RR&#39;s in text.
Definition: host2str.h:85
return NULL
Definition: keys.c:738
int flags
Specification of how RR&#39;s should be formatted in text.
Definition: host2str.h:83
the Internet
Definition: rr.h:50
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:426
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition: rr.c:882
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:122
#define LDNS_FMT_ZEROIZE_RRSIGS
Definition: host2str.h:67
#define LDNS_COMMENT_FLAGS
Show when a NSEC3 RR has the optout flag set as comment.
Definition: host2str.h:60
ldns_rr_list * ldns_zone_rrs(const ldns_zone *z)
Get a list of a zone&#39;s content.
Definition: zone.c:35
size_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
returns the number of rr&#39;s in an rr_list.
Definition: rr.c:896
enum ldns_enum_rr_type ldns_rr_type
Definition: rr.h:215
void ldns_zone_sort(ldns_zone *zone)
Sort the rrs in a zone, with the current impl.
Definition: zone.c:393
const char * ldns_get_errorstr_by_id(ldns_status err)
look up a descriptive text by each error.
Definition: error.c:131
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
i
Definition: keys.c:681
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:2266
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