ldns-read-zone.c

Go to the documentation of this file.
00001 /*
00002  * read a zone file from disk and prints it, one RR per line
00003  *
00004  * (c) NLnetLabs 2005-2008
00005  *
00006  * See the file LICENSE for the license
00007  */
00008 
00009 #include "config.h"
00010 #include <unistd.h>
00011 #include <stdlib.h>
00012 
00013 #include <ldns/ldns.h>
00014 #include <ldns/host2str.h>
00015 
00016 #include <errno.h>
00017 
00018 int
00019 main(int argc, char **argv)
00020 {
00021         char *filename;
00022         FILE *fp;
00023         ldns_zone *z;
00024         int line_nr = 0;
00025         int c;
00026         bool canonicalize = false;
00027         bool sort = false;
00028         bool strip = false;
00029         bool only_dnssec = false;
00030         bool print_soa = true;
00031         ldns_status s;
00032         size_t i;
00033         ldns_rr_list *stripped_list;
00034         ldns_rr *cur_rr;
00035         ldns_rr_type cur_rr_type;
00036         const ldns_output_format *fmt = NULL;
00037         ldns_soa_serial_increment_func_t soa_serial_increment_func = NULL;
00038         int soa_serial_increment_func_data = 0;
00039 
00040         while ((c = getopt(argc, argv, "bcdhnsvzS:")) != -1) {
00041                 switch(c) {
00042                         case 'b':
00043                                 fmt = ldns_output_format_bubblebabble;
00044                         case 'c':
00045                                 canonicalize = true;
00046                                 break;
00047                         case 'd':
00048                                 only_dnssec = true;
00049                                 if (strip) {
00050                                         fprintf(stderr, "Warning: stripping both DNSSEC and non-DNSSEC records. Output will be sparse.\n");
00051                                 }
00052                                 break;
00053                         case 'h':
00054                                 printf("Usage: %s [-c] [-v] [-z] <zonefile>\n", argv[0]);
00055                                 printf("\tReads the zonefile and prints it.\n");
00056                                 printf("\tThe RR count of the zone is printed to stderr.\n");
00057                                 printf("\t-b include bubblebabble of DS's.\n");
00058                                 printf("\t-c canonicalize all rrs in the zone.\n");
00059                                 printf("\t-d only show DNSSEC data from the zone\n");
00060                                 printf("\t-h show this text\n");
00061                                 printf("\t-n do not print the SOA record\n");
00062                                 printf("\t-s strip DNSSEC data from the zone\n");
00063                                 printf("\t-S [[+|-]<number> | YYYYMMDDxx | "
00064                                                 " unixtime ]\n"
00065                                        "\t\tSet serial number to <number> or,"
00066                                                 " when preceded by a sign,\n"
00067                                        "\t\toffset the existing number with "
00068                                                 "<number>.  With YYYYMMDDxx\n"
00069                                        "\t\tthe serial is formatted as a datecounter"
00070                                                 ", and with unixtime as the\n"
00071                                        "\t\tnumber of seconds since 1-1-1970."
00072                                                 "  However, on serial number"
00073                                        "\n\t\tdecrease, +1 is used in stead"
00074                                                 ".  (implies -s)\n");
00075                                 printf("\t-v shows the version and exits\n");
00076                                 printf("\t-z sort the zone (implies -c).\n");
00077                                 printf("\nif no file is given standard input is read\n");
00078                                 exit(EXIT_SUCCESS);
00079                                 break;
00080                         case 'n':
00081                                 print_soa = false;
00082                                 break;
00083                         case 's':
00084                                 strip = true;
00085                                 if (only_dnssec) {
00086                                         fprintf(stderr, "Warning: stripping both DNSSEC and non-DNSSEC records. Output will be sparse.\n");
00087                                 }
00088                                 break;
00089                         case 'v':
00090                                 printf("read zone version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
00091                                 exit(EXIT_SUCCESS);
00092                                 break;
00093                         case 'z':
00094                                 canonicalize = true;
00095                                 sort = true;
00096                                 break;
00097                         case 'S':
00098                                 strip = true;
00099                                 if (*optarg == '+' || *optarg == '-') {
00100                                         soa_serial_increment_func_data =
00101                                                 atoi(optarg);
00102                                         soa_serial_increment_func =
00103                                                 ldns_soa_serial_increment_by;
00104                                 } else if (! strtok(optarg, "0123456789")) {
00105                                         soa_serial_increment_func_data =
00106                                                 atoi(optarg);
00107                                         soa_serial_increment_func =
00108                                                 ldns_soa_serial_identity;
00109                                 } else if (!strcasecmp(optarg, "YYYYMMDDxx")){
00110                                         soa_serial_increment_func =
00111                                                 ldns_soa_serial_datecounter;
00112                                 } else if (!strcasecmp(optarg, "unixtime")){
00113                                         soa_serial_increment_func =
00114                                                 ldns_soa_serial_unixtime;
00115                                 } else {
00116                                         fprintf(stderr, "-S expects a number "
00117                                                 "optionally preceded by a "
00118                                                 "+ or - sign to indicate an "
00119                                                 "offset, or the text YYYYMM"
00120                                                 "DDxx or unixtime\n");
00121                                         exit(EXIT_FAILURE);
00122                                 }
00123                                 break;
00124                 }
00125         }
00126 
00127         argc -= optind;
00128         argv += optind;
00129 
00130         if (argc == 0) {
00131                 fp = stdin;
00132         } else {
00133                 filename = argv[0];
00134 
00135                 fp = fopen(filename, "r");
00136                 if (!fp) {
00137                         fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
00138                         exit(EXIT_FAILURE);
00139                 }
00140         }
00141         
00142         s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);
00143 
00144         if (strip) {
00145                 stripped_list = ldns_rr_list_new();
00146                 while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z)))) {
00147                         cur_rr_type = ldns_rr_get_type(cur_rr);
00148                         if (cur_rr_type == LDNS_RR_TYPE_RRSIG ||
00149                             cur_rr_type == LDNS_RR_TYPE_NSEC ||
00150                             cur_rr_type == LDNS_RR_TYPE_NSEC3 ||
00151                             cur_rr_type == LDNS_RR_TYPE_NSEC3PARAM
00152                            ) {
00153                                 ldns_rr_free(cur_rr);
00154                         } else {
00155                                 ldns_rr_list_push_rr(stripped_list, cur_rr);
00156                         }
00157                 }
00158                 ldns_rr_list_free(ldns_zone_rrs(z));
00159                 ldns_zone_set_rrs(z, stripped_list);
00160         }
00161         if (only_dnssec) {
00162                 stripped_list = ldns_rr_list_new();
00163                 while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z)))) {
00164                         cur_rr_type = ldns_rr_get_type(cur_rr);
00165                         if (cur_rr_type == LDNS_RR_TYPE_RRSIG ||
00166                             cur_rr_type == LDNS_RR_TYPE_NSEC ||
00167                             cur_rr_type == LDNS_RR_TYPE_NSEC3 ||
00168                             cur_rr_type == LDNS_RR_TYPE_NSEC3PARAM
00169                            ) {
00170                                 ldns_rr_list_push_rr(stripped_list, cur_rr);
00171                         } else {
00172                                 ldns_rr_free(cur_rr);
00173                         }
00174                 }
00175                 ldns_rr_list_free(ldns_zone_rrs(z));
00176                 ldns_zone_set_rrs(z, stripped_list);
00177         }
00178 
00179         if (s == LDNS_STATUS_OK) {
00180                 if (canonicalize) {
00181                         ldns_rr2canonical(ldns_zone_soa(z));
00182                         for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
00183                                 ldns_rr2canonical(ldns_rr_list_rr(ldns_zone_rrs(z), i));
00184                         }
00185                 }
00186                 if (sort) {
00187                         ldns_zone_sort(z);
00188                 }
00189 
00190                 if (print_soa && ldns_zone_soa(z)) {
00191                         if (soa_serial_increment_func) {
00192                                 ldns_rr_soa_increment_func_int(
00193                                           ldns_zone_soa(z)
00194                                         , soa_serial_increment_func
00195                                         , soa_serial_increment_func_data
00196                                         );
00197                         }
00198                         ldns_rr_print_fmt(stdout, fmt, ldns_zone_soa(z));
00199                 }
00200                 ldns_rr_list_print_fmt(stdout, fmt, ldns_zone_rrs(z));
00201 
00202                 ldns_zone_deep_free(z);
00203         } else {
00204                 fprintf(stderr, "%s at %d\n", 
00205                                 ldns_get_errorstr_by_id(s),
00206                                 line_nr);
00207                 exit(EXIT_FAILURE);
00208         }
00209         fclose(fp);
00210 
00211         exit(EXIT_SUCCESS);
00212 }

Generated on Thu Apr 5 23:03:54 2012 for ldns by  doxygen 1.4.7