Bind8/DB/Parser.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 167 CC 13 SCAR 34.1
CC
13
Cov
0%
__parse_loop
72.0
__getline
20.0
__parse_db
6.0
CRAP 30.2
1 # Parse a Bind8 DB
2 #
3 # Copyright Karthik Krishnamurthy <karthik.k@extremix.net>
4  
5 package Unix::Conf::Bind8::DB;
6  
7 use strict;
8 use warnings;
9 use Unix::Conf;
10 use Unix::Conf::Bind8::DB::Lib;
11  
12 #
13 my $ttl_pat = qr/(?:\d+[wdhms])+|\d+/oi;
14 my $type_pat = qr/SOA|A|NS|MX|CNAME|PTR/oi;
15 my $class_pat = qr/IN/oi;
16  
17 my ($FH, @Parse_Stack, $DB_Origin, $DB, %Args, $Include_Encountered);
18  
19 sub __pushfile () { push (@Parse_Stack, [ $FH ]); }
20 sub __popfile () { ($FH) = @{pop (@Parse_Stack)}; }
21  
22 # forward decl to suppress warning
23 sub __parse_loop ($$);
24  
25 sub __parse_db ($)
26 {
27 $DB = $_[0];
28  
29 # make the origin absolute.
30 $DB_Origin = $DB->origin ();
31 __parse_loop ($DB->fh (), $DB->origin ());
32 # clean so we don't sync to db just because we read in the data
33 # but don't do it if we encountered an $INCLUDE, as we delete
34 # the include file and print all the data in this one.
35 $DB->dirty (0) unless ($Include_Encountered);
36 undef ($DB);
37 return (1);
38 }
39  
40 sub __die ($;$)
41 {
42 die ($_[0]) if (@_ == 1);
43 die (Unix::Conf->_err (sprintf ("$_[0]($FH:%s)",$FH->lineno ()), "$_[1]\n"))
44 }
45  
46 sub __parse_loop ($$)
47 {
48 __pushfile ();
49 $FH = $_[0];
50 my $corigin = $_[1];
51 my ($def_ttl, $line, @tokens);
52 my ($token, $rtype, $ret);
53  
54 while (defined (($line = __getline ()))) {
55 # this is for the method call at the end of this loop.
56 no strict 'refs';
57 ($line =~ /^\$TTL(?:\s+)($ttl_pat+)(?:\s*)$/i) && do {
58 $def_ttl = $1;
59 next;
60 };
61 #
62 ($line =~ /^\$ORIGIN\s+([\w.-]+)\s*$/i) && do {
63 # if the argument to $ORIGIN is not absolute make it into one
64 $corigin = __make_absolute ($corigin, $1);
65 next;
66 };
67 # read in data and delete the file.
68 ($line =~ /^\$INCLUDE\s+(\S+)\s*(\S*)\s*$/) && do {
69 # $DB->new_include ();;
70 my ($fh, $origin);
71 $fh = Unix::Conf->_open_conf (NAME => $1, SECURE_OPEN => 0) or
72 return ($fh);
73 $origin = __make_absolute ($origin, $2);
74 __parse_loop ($fh, $origin);
75 $Include_Encountered = 1;
76 next;
77 };
78  
79 @tokens = split (/\s+/, $line);
80 # get label for the record
81 $token = shift (@tokens);
82 if ($token eq '@') {
83 $Args{LABEL} = __make_relative ($DB_Origin, $corigin);
84 }
85 # __make_absolute only if label exists. otherwise
86 # we will do it to the last label, which could
87 # result in an error if it was empty.
88 elsif ($token) {
89 $Args{LABEL} = __make_relative ($DB_Origin, __make_absolute ($corigin, $token));
90 }
91  
92 # if there was no label specifed, the last one used
93 # remains.
94  
95 # check to see if next token is TTL or CLASS.
96 (($token = shift (@tokens)) =~ /^$ttl_pat$/) && do {
97 $Args{TTL} = $token; $token = shift (@tokens);
98 };
99 ($token =~ /^$class_pat$/) && do {
100 $Args{CLASS} = $token; $token = shift (@tokens);
101 };
102 # at this point what we have must be the record type
103 __die ('__parse_db', "illegal record type `$token'")
104 unless ($token =~ /^$type_pat$/);
105 $rtype = $token;
106 if ($rtype =~ /soa/i) {
107 __die ('__parse_loop', "SOA owner `$Args{LABEL}' not same as DB origin `$DB_Origin'")
108 if (__make_absolute ($DB_Origin, $Args{LABEL}) ne $DB_Origin);
109 ($Args{AUTH_NS}, $Args{MAIL_ADDR}, $Args{SERIAL}, $Args{REFRESH},
110 $Args{RETRY}, $Args{EXPIRE}, $Args{MIN_TTL}) = @tokens;
111 $Args{AUTH_NS} = __make_relative ($corigin, $Args{AUTH_NS});
112 $Args{MAIL_ADDR} = __make_relative ($corigin, $Args{MAIL_ADDR});
113 }
114 elsif ($rtype =~ /mx/i) {
115 $Args{MXPREF} = shift (@tokens);
116 $Args{RDATA} = __make_relative ($DB_Origin, __make_absolute ($corigin, shift (@tokens)));
117 }
118 # RDATA for A are not labels but IP addresses so don't try append with corigin
119 elsif ($rtype =~ /a/i) {
120 $Args{RDATA} = shift (@tokens);
121 }
122 else {
123 $Args{RDATA} = __make_relative ($DB_Origin, __make_absolute ($corigin, shift (@tokens)));
124 }
125 # invoke as a subroutine but pass the object as the first
126 # argument, method like.
127 $ret = &{"new_\L$rtype\E"} ($DB, %Args) or __die ($ret);
128 }
129 __popfile ();
130 }
131  
132 sub __getline
133 {
134 while (<$FH>) {
135 chomp;
136 s/^(.*?);.*$/$1/;
137 # get another line if no non white space chars after stripping comment
138 next if ($_ !~ /\S/);
139 if (s/^(.+)\(\s*$/$1/) {
140 my $tmp;
141 while (defined (($tmp = <$FH>))) {
142 $tmp =~ s/^(.*?);.*$/$1/;
143 $_ .= $tmp;
144 last if (s/^([^)]+)\).*$/$1/);
145 }
146 # remove any newline added above in the while loop
147 chomp;
148 }
149 return ($_);
150 }
151 return;
152 }
153  
154 1;