Zone/ParserX.pm untested

filter: Statementstmt 0.0% 0 / 403 Branchbran 0.0% 0 / 284 Conditioncond 0.0% 0 / 103 MC/DCmcdc 0.0% 0 / 55 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 883 CC 142 SCAR 76.0
CC
142
Cov
0%
_read
3660.0
_parseline
2256.0
read
156.0
CRAP 1988.7
1 package Net::DNS::Zone::ParserX;
2  
3 use strict;
4 use warnings;
5 use vars qw (
6 $VERSION
7 $REVISION
8 @ISA
9 @EXPORT_OK
10 $ZONE_RR_REGEX
11 $NAMED_COMPILEZONE
12 $DUMP
13 );
14  
15 use File::Basename;
16 use Carp;
17 use File::Temp;
18 use IO::File;
19 use IO::Handle;
20 use Net::DNS;
21 use Net::DNS::RR;
22  
23 # ABSTRACT: A Zone Pre-Parser (slightly altered fork in cpanel2autodns)
24  
25  
26 BEGIN {
27 require Exporter;
28  
29 @ISA = qw(Exporter);
30 $VERSION = '0.02';
31 $REVISION = (qw$LastChangedRevision: 788 $)[1];
32  
33 @EXPORT_OK = qw (
34 processGENERATEarg
35 );
36 }
37  
38 # Debugging during code development ... Anything greater than 0 will
39 # cause debugging output.
40 use constant DEBUG => 0; ## no critic
41  
42 my $debug = DEBUG;
43  
44 my $MaxIncludeDepth = 20; # maximum time $INCLUDE recursion.
45  
46 ############
47 #
48 # The ZONE_RR_REGEX all classes and types known by Net::DNS::RR and creates
49 # a regexp to match input against.
50 #
51 # This way we match against all know RRs at least those known to
52 # Net::DNS
53 #
54  
55 # the classes regexp component we need elsewhere in the code as well
56 my $classes = join('|', keys %Net::DNS::classesbyname, 'CLASS\\d+');
57  
58 build_regex() unless $ZONE_RR_REGEX;
59  
60  
61 sub new {
62 my ($class, $argument) = @_;
63  
64 $class = ref($class) || $class;
65 my $self = {};
66 bless($self, $class);
67  
68 if ($argument) {
69 print "new called with an argument\n" if $debug;
70 if ($argument->isa("IO::Handle")) {
71 $self->{"fh"} = $argument;
72 }
73 else {
74 die
75 'Failure: supplied argument is not an instance of IO::File, IO::Handle or related i.o.w. isa( IO::Handle) failed';
76 }
77 }
78 else {
79 $self->{"fh"} = IO::File->new_tmpfile;
80 }
81  
82 return $self;
83 }
84  
85  
86 sub get_io {
87 my $self = shift;
88 return $self->{"fh"};
89 }
90  
91  
92 sub read { ## no critic (ProhibitBuiltinHomonyms)
93 my ($self, $possible_filename, $arghash) = @_;
94  
95 my $origin = $arghash->{"ORIGIN"};
96  
97 if ($arghash->{"CREATE_RR"}) {
98 $self->{create_rr} = [];
99 }
100  
101 if ($arghash->{"STRIP_SEC"}) {
102 $self->{strip_dnskey} = 1;
103 $self->{strip_nsec} = 1;
104 $self->{strip_rrsig} = 1;
105 }
106  
107 if ($arghash->{"STRIP_DNSKEY"}) {
108 $self->{strip_dnskey} = 1;
109 }
110  
111 if ($arghash->{"STRIP_NSEC"}) {
112 $self->{strip_nsec} = 1;
113 }
114  
115 if ($arghash->{"STRIP_RRSIG"}) {
116 $self->{strip_rrsig} = 1;
117 }
118  
119 if ($arghash->{"STRIP_OLD"}) {
120 $self->{strip_old} = 1;
121 }
122  
123 $self->{bump_soa} = 0;
124 if ($arghash->{"BUMP_SOA"}) {
125 $self->{bump_soa} = 1;
126 }
127  
128 my $fh = $self->{"fh"};
129  
130 my @filename = glob($possible_filename);
131 return "READ FAILURE: ambigueous input: " . join " ", @filename . "\n"
132 if (@filename > 1);
133 $self->{'filename'} = $filename[0];
134  
135 if (defined($origin) && $origin =~ /\S+/o) {
136  
137 # Strip spaces from begin and end of string.
138 $origin =~ s/\s//og;
139 $self->{'_origin'} = $origin;
140 }
141 else {
142 $self->{'_origin'} = basename($filename[0]);
143 }
144  
145 $self->{'IncludeRecursionDetector'} = 0;
146 $self->{'DefaultTTLDirectiveFound'} = 0;
147 $self->{'_origin'} .= "." if $self->{'_origin'} !~ /\.$/o;
148 my $returnval = $self->_read($fh, $filename[0], $self->get_origin, '', 0);
149 return $returnval if $returnval =~ /^READ FAILURE:/o;
150  
151 return 0;
152 }
153  
154  
155 sub get_array {
156 my $self = shift;
157  
158 return [] unless $self->{create_rr};
159 return $self->{create_rr};
160 }
161  
162  
163 sub get_origin {
164 my $self = shift;
165 return $self->{'_origin'};
166 }
167  
168  
169 sub build_regex {
170  
171 # Longest ones go first, so the regex engine will match AAAA before A.
172 my $types =
173 join('|', sort { length $b <=> length $a } keys %Net::DNS::typesbyname);
174  
175 $types .= '|TYPE\\d+';
176  
177 $ZONE_RR_REGEX = " ^
178 \\s*
179 (\\S+)+ # name anything non-space will do
180 \\s*
181 (\\d+)?
182 \\s*
183 ($classes)?
184 \\s*
185 ($types) # There must be a type specified.
186 \\s*
187 (.*)
188 \$";
189  
190 print STDERR "Regex: $ZONE_RR_REGEX\n" if DEBUG;
191  
192 return;
193 }
194  
195 sub _read {
196  
197 my $self = shift;
198 my $fh_out = shift; # Filehandle to print parsed output to
199 my $filename = shift; # Filename of file to be read
200 my $lastseenORIGIN = shift; # Relevant to relative domains
201 my $previousdname = shift; # Relevant during $INCLUDE
202 my $lastseenTTL = shift;
203 print ";; _read method called on $filename with origin $lastseenORIGIN\n"
204 if $debug;
205  
206 my $namedcomp_return;
207 $namedcomp_return =
208 $self->_read_namedcomp($fh_out, $filename, $lastseenORIGIN)
209 if ($NAMED_COMPILEZONE);
210 print "namedcomp_return returned: $namedcomp_return\n" if $debug;
211 if (defined($namedcomp_return)) {
212 if ($namedcomp_return eq "success") {
213 return "success";
214 }
215 else {
216 # Only if the command failed to execute we'll continue with the "perl code";
217 return ("READ FAILURE: from named-compilezone: $namedcomp_return")
218 unless $namedcomp_return =~ /Failed to execute/;
219 }
220 }
221  
222 $self->{"IncludeRecursionDetector"}++; # Used for testing INCLUDE LOOPS
223 my $fh_in = IO::File->new;
224 $fh_in->open("< $filename")
225 || return "READ FAILURE: Could not open $filename\n";
226  
227 $lastseenORIGIN .= "." if $lastseenORIGIN !~ /\.$/o;
228  
229 my $TTL = 0;
230 my $defaultTTL = 0;
231  
232 # The following loop parses the zone file. At the end of the
233 # paring logic the $_ contains "name TTL CLASS RTYPE RDATA" whith
234 # all variables set and all names expanded to FQDN.
235 #
236 # During the loop the APEX keyset and it's signatures are removed.
237 # Check RFC1035 section 5 for details on how to handle INCLUDES
238 # and how the lastseenORIGIN propagates
239  
240 my $buffer = '';
241 my $openingbracketfound = 0;
242  
243 READLINE: while (<$fh_in>) {
244 next READLINE if /^\s*$/o; # All spaces
245 print "LINE: >" . $_ if DEBUG > 1;
246 print "BUFF: >" . $buffer . "\n\n" if DEBUG > 9;
247 my $i = 0; # i is a counter to prevent overruns in multiline RRs
248  
249 # Start parsing the line 'token' by 'token'.
250 # As long as there are non whitespace tokens on the
251 # end of the line then (.*)$ matches those.
252 #
253 # $1 either contains a single whitespace or
254 # the longest nonwhitespace collection of characters
255  
256 # Only go through char-by-char lineparsing if there
257 # are parenthesis, quotes or comments or if we are parsing multilines
258 if (
259 $openingbracketfound
260 || /\(/o
261 || # Opening bracket
262 /\;/o || # Comment at end of line
263 /\"/o || # Quote
264 /\)/o # Closing bracket
265 )
266 {
267  
268 LINEPARSE:
269 while (s/^(.)(.*)$/$2/o && $i < 200) { # no more than 200
270 # lines for multi-
271 # line RRs
272 print "LINE: " . $_ if DEBUG > 10;
273 print "BUFF: " . $buffer . "\n\n" if DEBUG > 10;
274  
275 my $char = $1;
276 if ($char eq ';') {
277  
278 # rest of line is a comment...
279 if ($openingbracketfound) {
280 next READLINE;
281 }
282 else {
283 next READLINE
284 if $buffer =~ s/^\s*$//o; # buffer only
285 # contains spaces
286 last LINEPARSE;
287 }
288 }
289 elsif ($char eq '(') {
290  
291 # Maybe we are to strict here and we should just ignore this
292 return
293 "READ FAILURE: Multiple enclosing opening brackets around "
294 . $_
295 if $openingbracketfound == 1;
296 $openingbracketfound = 1;
297 }
298 elsif ($char eq ')') {
299 return
300 "READ FAILURE: Multiple enclosing closing brackets around "
301 . $_
302 if $openingbracketfound == 0;
303 $openingbracketfound = 0;
304 }
305 elsif ($char eq '"') {
306  
307 # We entered a 'character string'
308 # collect everything upto the closing quote
309 $buffer .= '"';
310 my $k = 0;
311 CHRSTR: while (s/(.)(.*)/$2/o) {
312 $buffer .= $1;
313 $k++;
314 if ($k > 256) {
315 print
316 "Character strings should not be longer than 256 chars\n";
317 print "See RFC1035 section 3.3\n";
318 exit;
319 }
320  
321 # Note that end of line will also terminate character
322 # strings.
323 # This may not be RFC complient so we print a warning.
324 last CHRSTR if $1 eq '"';
325 }
326 print "WARNING: Unmatched quotes at end of line\n"
327 if $buffer !~ /\"$/o;
328 }
329 else {
330 # Single spaces between the tokens.. we depend on this later
331 if ($char =~ /^\s+$/o) {
332 $buffer .= " " unless $buffer =~ /\s$/o;
333 }
334 else {
335 $buffer .= $char;
336 }
337 }
338 $i++;
339  
340 # Next line if we are at end of line and there is still a open bracket
341 # not matched.
342 next READLINE if $openingbracketfound && /^\s*$/o;
343 } # END LINEPARSE
344  
345 # LINE HAS NOW BEEN PARSED.. ALL MULTILINES ARE ON ONE LINE AND
346 #
347 $buffer =~ s/\s*$//go; # remove possible trailing spce
348 $_ = $buffer;
349 $buffer = '';
350 }
351 else { # when not parsing the line char by char
352 s/\s+/ /go; # Remove extra spaces
353 s/\s*$//go; # Remove extra spaces
354 }
355  
356 print "READLINE:>>" . $_ . "<<\n" if DEBUG > 2;
357  
358 if (/^\s*\$TTL\s+(\d+)/o) { #FOUND a $TTL directive
359 $lastseenTTL = $1;
360 $defaultTTL = $lastseenTTL if (!$defaultTTL);
361 print ";; DEFAULT TTL found : " . $lastseenTTL . "\n"
362 if DEBUG > 1;
363 $self->{'default_ttl'} = $defaultTTL;
364 next READLINE;
365 }
366  
367 # replace the @ by the ORIGIN.. as given by the argument.
368 s/@/$lastseenORIGIN/;
369  
370 # Set the current originin. This is the one from the $ORIGIN value from
371 # the zone file. It will be used to complete dnames below.
372 if (/^\s*\$ORIGIN\s+(\S+)\s*$/o) {
373 $lastseenORIGIN = $1;
374 print ";; lastseenORIGIN set to : " . $lastseenORIGIN . "\n"
375 if DEBUG > 1;
376 next READLINE;
377 }
378  
379 if (/^\s*\$INCLUDE\s+(\S+)\s*(\S*)?$/io) {
380 my $newfilename = $1;
381 $lastseenORIGIN = $2 if $2;
382 if ($newfilename =~ /\//o) {
383  
384 # absolute pathname
385 }
386 else {
387 #relative pathname
388 $newfilename =
389 dirname($self->{'filename'}) . "/" . $1; # Relative path...
390 }
391  
392 # Deep recursion is still possible....
393 return
394 "READ FAILURE: Including $filename from itself would cause deep recursion\n"
395 if ($filename eq $newfilename);
396  
397 # Other recursion check
398  
399 return "READ FAILURE: Nested INCLUDE more than 20 levels deep... \n"
400 . "check if the files are not including in loops..."
401 if $self->{"IncludeRecursionDetector"} > $MaxIncludeDepth;
402  
403 # RFC 1035 section 5 specifies that the lastseenORIGIN does not traverse
404 # INCLUDES but is unclear on the last seen TTL. We use the lastseen TTL
405 # from the included file
406 $lastseenTTL =
407 $self->_read($fh_out, $newfilename, $lastseenORIGIN,
408 $previousdname, $lastseenTTL);
409  
410 return $lastseenTTL if $lastseenTTL =~ /^READ FAILURE:/o;
411 next READLINE;
412 }
413  
414 # Use the previous dname if no dname was qualified (line starts with blanks)
415 if (/^(\S+)\s+/o) {
416 $previousdname = $1;
417  
418 # below is a uggly bug fix.
419 $previousdname = $lastseenORIGIN
420 if ($previousdname eq '$GENERATE');
421 $previousdname = $lastseenORIGIN
422 if ($previousdname eq '$INCLUDE');
423 }
424 else {
425 $_ = $previousdname . $_;
426 }
427  
428 # $_ now either contains a GENERATE statement or a line with not
429 # fully qualified domain names in both owner name as RDATA and
430 # with possibly unqualified TTL and CLASS.
431  
432 if (
433 m{^\s*\$GENERATE #Generate directive
434 \s+((\d+)-(\d+)(/(\d+))?) #Range start-stop or start-stop/step.
435 \s+(\S+) #The LHS
436 \s+(\S+) #The TYPE
437 \s+(\S+) #The RHS
438 }xo
439 )
440 {
441 print "Range: $2-$3 " if DEBUG;
442 print "/$5 " if DEBUG && $5;
443 print "LHS: $6 " . "TYPE: $7 " . "RHS: $8 \n" if DEBUG;
444 my $RANGE = $1;
445 my $LOW = $2;
446 my $HIGH = $3;
447 my $STEP = $5 ? $5 : 1;
448 my $LHS = $6;
449 my $TYPE = $7;
450 my $RHS = $8;
451  
452 if ($TYPE !~ /PTR|CNAME|DNAME|A|AAAA|NS/o) {
453 print
454 "Generate only supports PTR, CNAME, DNAME, A, AAAA and NS.\n";
455 next READLINE;
456 }
457 if ($LOW > $HIGH) {
458 print "Range should be increasing.\n";
459 print "Skipping the following \$GENERATE directive:\n" . $_;
460 next READLINE;
461 }
462 if ($LOW < 0 || $STEP < 0) {
463 print "Sorry all vallues in the range need to be positive";
464 print "Skipping the following \$GENERATE directive:\n" . $_;
465 next READLINE;
466 }
467 my $i = $LOW;
468 while ($i <= $HIGH) {
469  
470 my $ownername = processGENERATEarg($LHS, $i, $lastseenORIGIN);
471  
472 my $my_generated_record =
473 $ownername . " " . $lastseenTTL . " IN " . $TYPE . " ";
474 if ($TYPE =~ /CNAME|PTR|DNAME|NS/o) {
475  
476 # These types have expansion of the RDATA to FQDN
477 my $rdatastr =
478 processGENERATEarg($RHS, $i, $lastseenORIGIN);
479 $my_generated_record .= $rdatastr;
480 if ( ($TYPE =~ /CNAME|DNAME/)
481 && ($ownername eq $rdatastr))
482 {
483 $i += $STEP;
484 next;
485 }
486  
487 }
488 else {
489 # A and AAAA are left alone
490 $my_generated_record .= processGENERATEarg($RHS, $i, "");
491 }
492 print ";; GENERATE: " . $my_generated_record . "\n"
493 if DEBUG;
494  
495 print $fh_out $my_generated_record . "\n";
496  
497 if (defined $self->{"create_rr"}) {
498 my $rr = Net::DNS::RR->new($my_generated_record);
499  
500 push @{ $self->{"create_rr"} }, $rr;
501 }
502 $i += $STEP;
503 }
504  
505 }
506 else {
507 my $returnval =
508 $self->_parseline($_, $lastseenORIGIN, $lastseenTTL);
509 next READLINE if $returnval =~ /^__SKIPPED__$/o;
510 return $returnval if $returnval =~ /^READ FAILURE:/o;
511 $_ = $returnval;
512 print ";; " . $_ . "\n" if DEBUG > 2;
513  
514 print $fh_out $_ . "\n";
515 if (defined $self->{"create_rr"}) {
516 my $rr = Net::DNS::RR->new($_);
517 push @{ $self->{"create_rr"} }, $rr;
518 }
519 }
520 }
521  
522 # Done parsing this file.
523 $fh_in->close;
524 $self->{"IncludeRecursionDetector"}--;
525 print ";; returning from _read\n" if DEBUG > 2;
526 return $lastseenTTL;
527 }
528  
529 #
530 # Internal functions.
531  
532 #####################################
533 # complete_dname will append the origin to the input string if needed.
534 # Does a sanity check on escaped \.
535  
536 sub _complete_dname {
537 my $self = shift;
538 my $dname = shift;
539 my $origin = shift;
540 if ($dname !~ /\.$/o && $dname !~ /\\\.$/o)
541 { # Hmmmm what if a label ends in an escapped \.
542  
543 $dname .= "." . $origin;
544  
545 # This fixes a bug, If the origin equals the root the above line
546 # caused two trailing dots to be added.
547 chop $dname if $origin eq ".";
548 }
549  
550 return $dname;
551 }
552  
553  
554 ####################################################
555 # processGENERATEarg
556 #
557 # this function is used to expand lhs or rhs variables in
558 # a generate statment.
559 # it takes the lhs or rhs string and and the current vallue of
560 # the itterator as input and returns the beast fully expanded according
561 # to the following rules.
562  
563 #lhs describes the owner name of the resource records to be
564 #created. Any single $ symbols within the lhs side are replaced by the
565 #iterator value. To get a $ in the output you need to escape the $
566 #using a backslash \, e.g. \$. The $ may optionally be followed by
567 #modifiers which change the offset from the interator, field width and
568 #base. Modifiers are introduced by a { immediately following the $ as
569 #${offset[,width[,base]]}. e.g. ${-20,3,d} which subtracts 20 from the
570 #current value, prints the result as a decimal in a zero padded field
571 #of with 3. Available output forms are decimal (d), octal (o) and
572 #hexadecimal (x or X for uppercase). The default modifier is
573 #${0,0,d}. If the lhs is not absolute, the current $ORIGIN is appended
574 #to the name.
575  
576 sub processGENERATEarg {
577 my $lhs = shift;
578 my $i = shift;
579 my $origin = shift;
580  
581 my $expanded = "";
582 while ($lhs) {
583 my $remaining = "";
584 if (
585 $lhs =~ s/^(\S*?)
586 ((?<!\\)\$.*)$ # The first non escaped $ character and anything beyond to end of sring
587 /$2/x
588 )
589 {
590 $expanded .= $1 if $1;
591 $lhs =~ s/^\$(\{(\d+)(,(\d+))?(,(\w+))?\})?(.*)\s?$/$7/;
592  
593 # $lhs=~ s/\$//;
594 my $offset = $2 ? $2 : 0;
595 my $width = $4 ? $4 : 0;
596 my $format = $6 ? $6 : "d";
597 if ($format !~ /d|o|x|X/o) {
598 die
599 "Fatal error in parsing the format in a \$GENERATE statement.\n Should be d,o,x or X\n";
600 }
601 $expanded .= sprintf("%0$width$format", $i + $offset);
602 }
603 else {
604 $expanded .= $lhs;
605 $lhs = "";
606  
607 }
608 }
609 $expanded =~ s/\\\$/\$/og; #finally substitute '$' for the escaped \$
610  
611 # Only expand to FQDN if the last char is a "." and if the
612 # the $origin argument is not empty.
613  
614 $expanded .= "." . $origin if $expanded !~ /\.$/o && $origin ne "";
615  
616 return $expanded;
617 }
618  
619 ###################################
620 #
621 # parseline will complete an inputline of the form <dname> [<TTL>]
622 # [<CLASS>] <type> <RDATA> to a line with fully qualified names in
623 # the dname and the RDATA, it will insert the CLASS and TTL if not
624 # specified. The arguments are the lastseenORIGIN and lastseenTTL
625 # that are used to complete the domain names with, and to add to fill
626 # in the unqualified TTLs.
627 #
628  
629 # returns 0 on success
630 # returns string starting with "READ FAILURE:" on error.
631  
632 # returns the string "__SKIPPED__" if a line was skipped (see the
633 # argumens to the read method such as STRIP_SEC &c).
634  
635 sub _parseline {
636 my $self = shift;
637 $_ = shift;
638 my $lastseenORIGIN = shift; # vallue of the last seen $ORIGIN directive
639 my $lastseenTTL = shift;
640  
641 my $ttl;
642  
643 my $rtype = '';
644 my $rdata = '';
645 my $prefix = '';
646  
647 ($_ =~ m/$ZONE_RR_REGEX/xso)
648 || return "READ FAILURE: \""
649 . $_
650 . "\" did not match RR pattern.\nPlease clean your zonefile!\n";
651  
652 my $dname = $1;
653  
654 s/^\s*(\S+) / /o; # remove the dname to put it back fully qualified
655 # If there is a match it could still be matching the
656 # string 0, so just testing on $1 will now work....
657 if ($1 || $1 eq "0") {
658 $dname = $1;
659  
660 $dname = $self->_complete_dname($dname, $lastseenORIGIN);
661 $_ = $dname . $_;
662 print ";; read DNAME: " . $dname . "\n" if DEBUG > 2;
663 }
664 else {
665 return
666 "READ FAILURE: Couldn't match dname in read method while reading\n"
667 . $_
668 . " \nthis Should not happen\n";
669 }
670  
671 # See if there is a TTL value, if not insert one
672 if (/^\S+ (\d+)/o) {
673 print ";; TTL : " . $1 . "\n" if $debug > 2;
674 $ttl = $1;
675 }
676 else {
677  
678 # RFC 1035
679 # 'Omitted class and TTL values are default to the
680 # last explicitly stated values"
681  
682 # I take that to mean last explicitly stated in a $TTL
683 # statement. (Purerely because of bind9 compatibility)
684  
685 # instert last seen TTL
686  
687 s/^(\S+) (.*)$/$1 $lastseenTTL $2/;
688  
689 }
690  
691 # See if there is the CLASS is defined, if not insert one.
692 if (!/^\S+ \d+ ($classes)/) {
693  
694 #insert IN
695 s/^(\S+ \d+ )(.*)$/$1IN $2/o;
696 }
697  
698 # We have everything specified.. We now get the RTYPE AND RDATA...
699 /^\S+ \d+ ($classes) (\S+) (.*)$/;
700 if ($1) {
701 print ";; rtype: " . $2 . "\n" if DEBUG > 2;
702 $rtype = $2;
703 }
704 else {
705 return "READ FAILURE: We expected to match an RTYPE\n" . $_
706 . " \nthis Should not happen\n";
707 }
708 if ($3) {
709 $rdata = $3;
710 print ";; rdata:-->" . $rdata . "<---\n" if DEBUG > 2;
711  
712 }
713 else {
714 return
715 "READ FAILURE: We expected to find RDATA in the following record\n"
716 . $_
717 . " \ncheck your zonefile\n";
718 }
719  
720 if (defined $ttl) {
721 $prefix = $dname . " " . $ttl . " IN " . $rtype . " ";
722 }
723 else {
724 $prefix = $dname . " " . $lastseenTTL . " IN " . $rtype . " ";
725 }
726  
727 # Expand to FQDN in the RDATA.
728 #
729 # We apply a regular expression to the rdata and expand dnames in there
730 # to fully qualified dnames using the complete_dname function.
731  
732 if (uc $rtype eq "NS") {
733  
734 #"NS" RFC 1035, Section 3.3.11
735 # the pattern below is appropriate if the rdata only contains a dname
736 # or the dname is the last item in the RDATA string
737 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
738  
739 # skipping
740 # "MD" RFC 1035, Section 3.3.4 (obsolete)
741 # "MF" RFC 1035, Section 3.3.5 (obsolete)
742 }
743 elsif (uc $rtype eq "CNAME") {
744  
745 # "CNAME" RFC 1035, Section 3.3.1
746 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
747 }
748 elsif (uc $rtype eq "SOA") {
749  
750 # "SOA" RFC 1035, Section 3.3.13
751 # first two strings in the SOA rdata are dnames
752 $rdata =~ /(\S+)\s+(\S+)\s+(\d+)\s+(.*)$/o;
753 return "READ FAILURE: Soa serial not found in\n $_\n"
754 if (!$3 && $3 ne "0");
755  
756 my $soaserial = $3;
757 $soaserial++ if $self->{"bump_soa"};
758 $_ =
759 $prefix
760 . $self->_complete_dname($1, $lastseenORIGIN) . " "
761 . $self->_complete_dname($2, $lastseenORIGIN) . " "
762 . $soaserial . " "
763 . $4; #
764  
765 # Additional sanity check.
766 if (lc($dname) ne lc($self->{'_origin'})) {
767 print
768 "WARNING: ORIGIN as specified or determined from the file name\n";
769 print
770 " does not match the SOA ownername. I'll be using the ownername!\n";
771 print " origin set from "
772 . $self->{'_origin'} . " to: "
773 . $dname . "\n";
774 $self->{'_origin'} = $dname;
775 }
776  
777 }
778 elsif (uc $rtype eq "MB") {
779  
780 # "MB" RFC 1035, Section 3.3.3
781  
782 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
783 }
784 elsif (uc $rtype eq "PTR") {
785 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
786 }
787 elsif (uc $rtype eq "MG") {
788  
789 # "MG" RFC 1035, Section 3.3.6
790 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
791 }
792 elsif (uc $rtype eq "MR") {
793  
794 # "MR" RFC 1035, Section 3.3.8
795 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
796  
797 # skipping
798 # "NULL" RFC 1035, Section 3.3.10
799 # "WKS" RFC 1035, Section 3.4.2 (deprecated, and no dname)
800 }
801 elsif (uc $rtype eq "PTR") {
802  
803 # "PTR" RFC 1035, Section 3.3.12
804 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
805  
806 #skipping
807 # "HINFO" RFC 1035, Section 3.3.2
808 }
809 elsif (uc $rtype eq "MINFO") {
810  
811 # "MINFO" RFC 1035, Section 3.3.7
812 $rdata =~ /(\S+) (\S+)$/o;
813 $_ =
814 $prefix
815 . $self->_complete_dname($1, $lastseenORIGIN) . " "
816 . $self->_complete_dname($2, $lastseenORIGIN);
817 }
818 elsif (uc $rtype eq "MX") {
819  
820 # "MX" RFC 1035, Section 3.3.9
821 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
822  
823 # skipping
824 # "TXT" RFC 1035, Section 3.3.14
825 }
826 elsif (uc $rtype eq "RP") {
827  
828 # "RP" RFC 1183, Section 2.2
829 $rdata =~ /(\S+) (\S+)$/o;
830 $_ =
831 $prefix
832 . $self->_complete_dname($1, $lastseenORIGIN) . " "
833 . $self->_complete_dname($2, $lastseenORIGIN);
834 }
835 elsif (uc $rtype eq "AFSDB") {
836  
837 # "AFSDB" RFC 1183, Section 1
838 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
839  
840 # skipped
841 # "X25" RFC 1183, Section 3.1
842 # "ISDN" RFC 1183, Section 3.2
843 }
844 elsif (uc $rtype eq "RT") {
845  
846 # "RT" RFC 1183, Section 3.3
847 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
848  
849 # skipped
850 # "NSAP" RFC 1706, Section 5
851 }
852 elsif (uc $rtype eq "SIG") {
853  
854 # "SIG" RFC 2555, Section 4.1
855 return "__SKIPPED__" if $self->{'strip_old'};
856 my (
857 $typecovered, $algoritm, $type,
858 $orgttl, $sigexpiration, $siginception,
859 $keytag, $signame, $sig
860 )
861 = $rdata =~
862 /^\s*(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)/o;
863 $_ =
864 $prefix
865 . " $typecovered $algoritm $type $orgttl $sigexpiration $siginception $keytag "
866 . $self->_complete_dname($signame, $lastseenORIGIN) . " $sig";
867  
868 }
869 elsif (uc $rtype eq "PX") {
870  
871 # "PX" RFC 2163,
872 my ($preference, $map822, $mapx400) = $rdata =~ /(\d+) (\S+) (\S+)$/o;
873 $_ =
874 $prefix . " "
875 . $preference . " "
876 . $self->_complete_dname($map822, $lastseenORIGIN) . " "
877 . $self->_complete_dname($mapx400, $lastseenORIGIN);
878 }
879 elsif (uc $rtype eq "KEY") {
880  
881 # NOTHING
882  
883 # skipped
884 # "GPOS" RFC 1712 (obsolete)
885 # "AAAA" RFC 1886, Section 2.1
886 # "LOC" RFC 1876
887  
888 }
889 elsif (uc $rtype eq "NXT") {
890 return "__SKIPPED__" if $self->{'strip_old'};
891  
892 # "NXT" RFC 2535
893 $rdata =~ /(\S+) (.*)$/o;
894 $_ = $prefix . $self->_complete_dname($1, $lastseenORIGIN) . " " . $2;
895  
896 # "EID" draft-ietf-nimrod-dns-xx.txt
897 # "NIMLOC" draft-ietf-nimrod-dns-xx.txt
898 }
899 elsif (uc $rtype eq "SRV") {
900  
901 # "SRV" RFC 2782
902 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
903  
904 # skipped
905  
906 # "ATMA" [Dobrowski]
907 # skipped... hmmmmm...
908  
909 }
910 elsif (uc $rtype eq "NAPTR") {
911  
912 # "NAPTR" RFC 2168, 2915
913 $rdata =~ /(.*) (\S+)$/o;
914 $_ = $prefix . $1 . " " . $self->_complete_dname($2, $lastseenORIGIN);
915 }
916 elsif (uc $rtype eq "KX") {
917  
918 # "KX" RFC 2230
919 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
920  
921 # skipped
922 # "CERT" RFC 2358
923 # "A6" RFC 2874
924 }
925 elsif (uc $rtype eq "DNAME") {
926  
927 # "DNAME" RFC 2672
928 $_ = $prefix . $self->_complete_dname($rdata, $lastseenORIGIN);
929  
930 #skipped
931 # "SINK" [Eastlake] # I don't know about this RR
932 # "OPT" RFC 2671
933  
934 # "APL" RFC 3123 NO dname in RDATA
935 # "DS" NO dname in RDATA
936 # "SSHFP NO dname in RDATA
937  
938 }
939 elsif (uc $rtype eq "NSEC") {
940  
941 # "NSEC"
942 return "__SKIPPED__" if $self->{'strip_nsec'};
943 $rdata =~ /(\S+) (.*)$/o;
944 $_ = $prefix . $self->_complete_dname($1, $lastseenORIGIN) . " " . $2;
945 }
946 elsif (uc $rtype eq "DNSKEY") {
947  
948 # "DNSKEY"
949 return "__SKIPPED__" if $self->{'strip_dnskey'};
950  
951 }
952 elsif (uc $rtype eq "RRSIG") {
953  
954 # "RRSIG"
955 return "__SKIPPED__" if $self->{'strip_rrsig'};
956 my (
957 $typecovered, $algoritm, $type,
958 $orgttl, $sigexpiration, $siginception,
959 $keytag, $signame, $sig
960 )
961 = $rdata =~
962 /^\s*(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)/o;
963 return "__SKIPPED__"
964 if $self->{'strip_dnskey'} && uc($typecovered) eq "DNSKEY";
965 return "__SKIPPED__"
966 if $self->{'strip_nsec'} && uc($typecovered) eq "NSEC";
967 return "__SKIPPED__"
968 if $self->{'bump_soa'} && uc($typecovered) eq "SOA";
969 $_ =
970 $prefix
971 . " $typecovered $algoritm $type $orgttl $sigexpiration $siginception $keytag "
972 . $self->_complete_dname($signame, $lastseenORIGIN) . " $sig";
973  
974 }
975 elsif (uc $rtype =~ /TYPE\d+/o) {
976  
977 # Unknown RR.
978 }
979  
980 return $_;
981  
982 }
983  
984 # Use named-compilezone -D to do the processing;
985  
986 sub _read_namedcomp {
987 my ($self, $fh_out, $filename, $origin) = @_;
988  
989 my $tmpfh = File::Temp->new();
990 my $tmpfname = $tmpfh->filename;
991  
992 print ";; Tempfilename: $tmpfname\n" if $debug;
993  
994 print ";; _read_namedcomp: $filename $origin\n" if $debug;
995 my $cmd = "named-compilezone -i none -o $tmpfname $origin $filename";
996 print ";; Running: " . join(" ", $cmd) . "\n" if $debug;
997  
998 my @result = `$cmd`;
999 if ($debug) {
1000 foreach my $i (@result) {
1001 print ";;; $i\n";
1002 }
1003 }
1004 my $lastresult = pop(@result);
1005  
1006 if ($? == -1) {
1007 return "command failed: $!\n";
1008 }
1009 elsif ($lastresult =~ /failed/) {
1010 return $lastresult;
1011 }
1012  
1013 $origin =~ s/\.$// unless ($origin eq ".");
1014  
1015 my $loadzone_result = "";
1016 my $ProcessedApex;
1017  
1018 open($DUMP, "<", $tmpfname) ## no critic
1019 || return ("Could not execute " . join(" ", $cmd))
1020 ; # This should cause classic parsing
1021  
1022 CONTENT: while (<$DUMP>) {
1023 if (/^\S+\s+\d+\s+IN\s+(SOA|RRSIG\s+\w+|DNSKEY|NSEC|SOA|NXT|SIG)\s+/o) {
1024 my $type = $1;
1025 $self->{strip_dnskey} && ($type eq "DNSKEY") && next CONTENT;
1026  
1027 # Also strip the sig over DNSKEY if we are stripping DNSKEYS
1028 if ($type =~ /RRSIG\s+(\w+)/) {
1029 $self->{strip_rrsig} && next CONTENT;
1030 $self->{strip_dnskey} && ($1 eq "DNSKEY") && next CONTENT;
1031 $self->{strip_nsec} && ($1 eq "NSEC") && next CONTENT;
1032 $self->{bump_soa} && ($1 eq "SOA") && next CONTENT;
1033 }
1034  
1035 $self->{strip_rrsig} && ($type eq "RRSIG") && next CONTENT;
1036 $self->{strip_nsec} && ($type eq "NSEC") && next CONTENT;
1037 $self->{strip_old}
1038 && (($type eq "NXT") || ($type eq "SIG"))
1039 && next CONTENT;
1040 if ($self->{bump_soa} && ($type eq "SOA")) {
1041 my $soa = Net::DNS::RR->new($_);
1042 $soa->serial($soa->serial() + 1);
1043 $_ =
1044 $soa->string . "\n"; # The newline is FITAL, the next record
1045 # would otherwise disapear behind a
1046 # comment.
1047 }
1048 }
1049  
1050 print $fh_out $_;
1051 if (defined $self->{"create_rr"}) {
1052 my $rr = Net::DNS::RR->new($_);
1053 push @{ $self->{"create_rr"} }, $rr;
1054 }
1055 }
1056  
1057 close($DUMP);
1058  
1059 print $fh_out "\n"; # Make sure file ends with newline
1060  
1061 return ("success");
1062  
1063 }
1064  
1065 sub DESTROY {
1066 close($DUMP);
1067 return;
1068 }
1069  
1070  
1071 1;
1072  
1073 __END__