Zone/File/Default.pm

filter: Statementstmt 87.5% 120 / 137 Branchbran 67.8% 38 / 56 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 4 totaltotal 77.3% 178 / 230 CC 42 SCAR 32.5
CC
42
Cov
80%
parse
35.6
dump
30.0
new
5.8
CRAP 25.9
« Zone/Record.pm ↑ Summary Zone/File.pm »
1 #!/usr/local/bin/perl -w
2 ######################################################################
3 #
4 # DNS/Zone/File/Default.pm
5 #
6 # $Id: Default.pm,v 1.4 2003/02/04 15:38:01 awolf Exp $
7 # $Revision: 1.4 $
8 # $Author: awolf $
9 # $Date: 2003/02/04 15:38:01 $
10 #
11 # Copyright (C)2001-2003 Andy Wolf. All rights reserved.
12 #
13 # This library is free software; you can redistribute it and/or
14 # modify it under the same terms as Perl itself.
15 #
16 ######################################################################
17  
18 package DNS::Zone::File::Default;
19  
20 2 ▶no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use DNS::Zone;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use DNS::Zone::Label;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use DNS::Zone::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use DNS::Zone::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 @ISA = qw(DNS::Zone::File);
33  
34 2 my $VERSION = '0.85';
35 2 my $REVISION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
36  
37 2 my @known_classes = ( 'IN' );
38  
39 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod undocumented
40 1 my($pkg, $zone, $file) = @_;
41 1 ▶ my $class = ref($pkg) || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
42  
43 1 ▶ return undef unless($zone);
Branch: 1 branch
BranchTrueFalseTotal
unless $zone 0 1 1
44  
45 1 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
ref $zone ? : 0 1 1
46 'ZONE' => (ref($zone)) ? $zone : new DNS::Zone($zone)
47 };
48
49 1 ▶ $self->{'FILE'} = $file if($file);
Branch: 1 branch
BranchTrueFalseTotal
if $file 1 0 1
50
51 1 bless $self, $class;
52
53 1 return $self;
54 }
55  
56 1 ▶sub zone {
Subroutine zone: called CC=1 SCAR=0.0
Pod undocumented
57 1 my($self) = @_;
58
59 1 return($self->{'ZONE'});
60 }
61  
62 1 ▶sub parse {
Subroutine parse: called CC=34 SCAR=35.7
Pod undocumented
63 1 my($self, $file) = @_;
64  
65 1 ▶ return undef unless($self->{'ZONE'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ZONE"} 0 1 1
66  
67 1 my @lines = $self->read($file);
68  
69 # substitute include statements completely
70 # $INCLUDE <filename> [<origin>] [<comment>]
71 1 ▶ for(my $i=0 ; defined $lines[$i] ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lines[$i]) 28 1 29
72 28 ▶ if($lines[$i] =~ /^\s*\$INCLUDE\s+(.+)\s*(.*)\s*.*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$i] =~ /^\s*\$INCLUDE\s+(.+)\s*(.*)\s*.*$/i) 0 28 28
73 0 my @included = $self->read($1);
74 0 ▶ my $origin = "\$ORIGIN " . ($2) ? $self->zone()->name() : $2; ###FIXME
Branch: 1 branch
BranchTrueFalseTotal
'$ORIGIN ' . $2 ? : 0 0 0
75 0 splice @lines, $i, 1, ($origin, @included, $origin);
76 }
77 }
78  
79 1 ▶ return undef unless(scalar @lines);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @lines 0 1 1
80
81 1 my $zone = $self->{'ZONE'};
82 1 my $origin = $zone->{'NAME'} . '.';
83
84 1 my $fullline;
85 my @result;
86  
87 1 for(@lines) {
88 28 ▶ if(!m/^[\s\;]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^[\s\;]*$/) 23 5 28
89 23 s/\s+/ /g;
90 23 s/\s+$//;
91
92 23 ▶ if(m/\(/ || $fullline) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\(/ or $fullline) { } 7 16 23
Condition100%3/3
/\(/ or $fullline
l!l&&r!l&&!r
1616
MC/DC100%2/2
/\(/ or $fullline
/\(/ $fullline
Truth table100%3/3
A or B
A= /\(/B= $fullline
ABresult
1X1
011
000
93 7 s/\s*\;.*$//g;
94 7 $fullline .= $_;
95  
96 7 $_ = $fullline;
97  
98 7 my $open = tr/\(/\(/;
99 7 my $close = tr/\)/\)/;
100 7 my $count = ($open - $close);
101  
102 7 ▶ if (!$count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 1 6 7
103 1 push @result, ($fullline);
104 1 $fullline = '';
105 }
106 }
107 else {
108 16 push @result, ($_);
109 }
110 }
111 }
112  
113 1 @lines = @result;
114  
115 1 my $label = $origin;
116  
117 1 foreach my $line(@lines) {
118 17 ▶ if($line =~ /^\$ORIGIN\s+(.+)\s*\;*\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\$ORIGIN\s+(.+)\s*\;*\s*$/) { } 1 16 17
elsif (not $line =~ s/^\@/$origin/) { } 15 1 16
119 1 my $new = $1;
120 1 my $old = $origin;
121
122 1 ▶ if($new =~ /\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new =~ /\.$/) { } 0 1 1
123 0 $origin = $new;
124 }
125 else {
126 1 $origin = $new . '.' . $old;
127 }
128 }
129 elsif($line !~ s/^\@/$origin/) {
130 15 $line =~ s/^\s+/$label /;
131
132 15 ▶ if($line =~ s/^([-\*\w]+(\.[-\*\w]+)*)\s+/$1\.$origin /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^([-\*\w]+(\.[-\*\w]+)*)\s+/$1.$origin /) 3 12 15
133 3 $label = "$1\.$origin";
134 }
135 }
136 1 else { $label = $origin; }
137 }
138  
139 1 $label = "\;";
140 1 ▶ for (my $i = -1 ; defined $lines[$i] ; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lines[$i]) 17 1 18
141 17 $lines[$i] =~ s/^\;/$label \; \; /;
142 17 $label = $lines[$i];
143 17 $label =~ s/\s+.*//g;
144 }
145  
146 1 my $ttl_default = 0;
147
148 1 for (@lines) {
149 17 my @parts = split " ", $_;
150  
151 17 ▶ next unless defined $parts[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts[0] 0 17 17
152
153 17 ▶ if($parts[0] eq '$TTL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parts[0] eq '$TTL') { } 1 16 17
elsif ($parts[0] ne '$ORIGIN') { } 15 1 16
154 1 $ttl_default = $parts[1];
155 }
156 elsif($parts[0] ne '$ORIGIN') {
157 15 my $label = lc shift @parts;
158 15 $label =~ s/\.$origin$//;
159 15 ▶ $label = '@' if($label eq $origin);
Branch: 1 branch
BranchTrueFalseTotal
if $label eq $origin 6 9 15
160  
161 15 my $ttl;
162 15 ▶ if($parts[0] =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[0] =~ /^\d+$/) { } 0 15 15
163 0 $ttl = shift @parts;
164 }
165 else {
166 15 $ttl = $ttl_default;
167 }
168
169 15 my $class = 'IN';
170 15 my $type = uc shift @parts;
171 15 foreach (@known_classes) {
172 15 ▶ if(uc $_ eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $_ eq $type) 13 2 15
173 13 $class = $type;
174 13 $type = uc shift @parts;
175 13 last;
176 }
177 }
178 15 my $classtype = $class . " " . $type;
179  
180 15 my ($data, @comments) = split /\s*\;\s*/, join ' ', @parts;
181  
182 15 ▶ my $label_ref = $zone->label({'NAME'=>$label}) || $zone->add(DNS::Zone::Label->new($label));
Condition66%2/3
$zone->label({'NAME', $label}) || $zone->add('DNS::Zone::Label'->new($label))
l!l&&r!l&&!r
1140
MC/DC0%0/2
$zone->label({'NAME', $label}) || $zone->add('DNS::Zone::Label'->new($label))
$zone->label({'NAME', $label}) $zone->add('DNS::Zone::Label'->new($label))
Truth table66%2/3
A || B
A= $zone->label({'NAME', $label})B= $zone->add('DNS::Zone::Label'->new($label))
ABresult
1X1
011
000
183 15 my $record_ref = DNS::Zone::Record->new($ttl, $classtype, $data);
184 15 $label_ref->add($record_ref);
185  
186 15 for (@comments) {
187 2 my $record_ref = DNS::Zone::Record->new(0, '', $_);
188 2 $label_ref->add($record_ref);
189 }
190 }
191 }
192  
193 1 ▶ warn "\$TTL not set using SOA minimum instead !" if(!$ttl_default);
Branch: 1 branch
BranchTrueFalseTotal
unless $ttl_default 0 1 1
194  
195 1 my $minimum;
196 1 my @labels = $zone->labels();
197
198 1 for (@labels) {
199 4 my @records = $_->records();
200
201 4 for (@records) {
202 17 ▶ $minimum = $_->minimum() if($_->type() eq 'IN SOA');
Branch: 1 branch
BranchTrueFalseTotal
if $_->type eq "IN SOA" 1 16 17
203 }
204 }
205
206 1 for (@labels) {
207 4 my @records = $_->records();
208
209 4 for (@records) {
210 17 ▶ $_->ttl($minimum) if($_->ttl() == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $_->ttl == 0 2 15 17
211 }
212 }
213  
214 1 return $self;
215 }
216  
217 0 ▶sub dump {
Subroutine dump: not called CC=5 SCAR=34.0
Pod undocumented
218 0 my($self, $file) = @_;
219  
220 0 ▶ $file = $file || $self->{'FILE'};
Condition0%0/3
$file || $self->{'FILE'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$file || $self->{'FILE'}
$file $self->{'FILE'}
Truth table0%0/3
A || B
A= $fileB= $self->{'FILE'}
ABresult
1X1
011
000
221  
222 0 ▶ return undef unless($self->{'ZONE'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ZONE"} 0 0 0
223  
224 0 ▶ if($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
225 0 ▶ if (open(FILE, ">$file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open FILE, ">$file") { } 0 0 0
226 0 my $old_fh = select(FILE);
227
228 0 $self->{'ZONE'}->dump();
229  
230 0 select($old_fh);
231 0 close FILE;
232 }
233 0 else { return undef; }
234 }
235 else {
236 0 $self->{'ZONE'}->dump();
237 }
238  
239 0 return $self;
240 }
241  
242 2 1;
243  
244 __END__
« Zone/Record.pm ↑ Summary Zone/File.pm »