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 bubblebabble 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 }