blib/lib/Parser/IPTables/Save.pm

filter: Statementstmt 47.3% 70 / 148 Branchbran 30.3% 31 / 102 Conditioncond 66.6% 14 / 21 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 50.0% 7 / 14 Podpod 100% 9 / 9 totaltotal 44.6% 137 / 307 CC 63 SCAR 47.0
CC
63
Cov
42%
save
306.0
table
80.7
fetch_rules
20.0
CRAP 109.5
↑ Summary
1 package Parser::IPTables::Save;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Parser::IPTables::Save - A parser for iptables-save output files.
13  
14 =head1 VERSION
15  
16 Version 0.04
17  
18 =cut
19  
20 1 our $VERSION = '0.04';
21  
22  
23 =head1 SYNOPSIS
24  
25 Quick summary of what the module does.
26  
27 Perhaps a little code snippet.
28  
29 use Parser::IPTables::Save;
30  
31 my $iptables_save = Parser::IPTables::Save->new('/tmp/iptables.out');
32 $iptables_save->table('filter');
33  
34 $iptables_save->create({ chain => 'POSTROUTING', source => '192.168.1.0/24', target => 'MASQUERADE', comment => 'Rule to masquerade' });
35  
36 $iptables_save->move(3, -1);
37  
38 $iptables_save->delete(8);
39 $iptables_save->delete(9);
40  
41 my @rules = $iptables_save->fetch_rules();
42  
43 $iptables_save->disable(10);
44 $iptables_save->enable(10);
45  
46 $iptables_save->save();
47  
48  
49 =head1 METHODS
50  
51 =head2 create
52  
53 Create a new rule
54  
55 $iptables_save->create({ chain => 'POSTROUTING', source => '192.168.1.0/24', target => 'MASQUERADE', comment => 'Rule to masquerade' });
56  
57 =cut
58  
59 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
60 0 my($self, $rule, $index) = @_;
61  
62 0 ▶ if($index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index) { } 0 0 0
63 # replace
64 0 splice(@{ $self->{rules}}, $index, 0, ($rule));
65 }
66 else {
67 0 unshift(@{ $self->{rules} }, $rule);
68 }
69 }
70  
71  
72 =head2 delete
73  
74 Delete a rule
75  
76 $iptables_save->delete(8);
77  
78 =cut
79  
80 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
81  
82 0 my($self, $index) = @_;
83  
84 0 delete @{ $self->{rules}}[$index];
85 }
86  
87  
88 =head2 disable
89  
90 disable a rule
91  
92 # disable rule with index 5
93 $iptables_save->disable(5);
94  
95 =cut
96  
97 0 ▶sub disable {
Subroutine disable: not called CC=1 SCAR=6.9
Pod documented
98  
99 0 my($self, $index) = @_;
100  
101 0 @{ $self->{rules}}[$index]->{disabled} = 1;
102  
103 0 return;
104  
105 }
106  
107  
108 =head2 enable
109  
110 enable a rule
111  
112 # enable a rule previously disabled
113 $iptables_save->enable(5);
114  
115 =cut
116  
117 0 ▶sub enable {
Subroutine enable: not called CC=1 SCAR=6.9
Pod documented
118  
119 0 my($self, $index) = @_;
120  
121 0 @{ $self->{rules}}[$index]->{disabled} = 0;
122  
123 0 return;
124  
125 }
126  
127  
128 =head2 new
129  
130 =cut
131  
132 1 ▶sub new {
Subroutine new: called CC=5 SCAR=18.3
Pod documented
133  
134 1 my($proto, $iptables_save_file) = @_;
135  
136 0 ▶ unless( $proto && defined($iptables_save_file) ) { croak "Parser::IPTables::Save->new(): usage error"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($proto and defined $iptables_save_file) 0 1 1
Condition33%1/3
$proto and defined $iptables_save_file
!ll&&!rl&&r
001
MC/DC0%0/2
$proto and defined $iptables_save_file
$proto defined $iptables_save_file
Truth table33%1/3
A and B
A= $protoB= defined $iptables_save_file
ABresult
0X0
100
111
137  
138 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
139  
140 1 my $self = {};
141  
142 # open iptables-save output file
143 1 ▶ tie my @file_array, 'Tie::File', $iptables_save_file or croak("Error opening file $iptables_save_file $!");
Branch: 1 branch
BranchTrueFalseTotal
tie my @file_array, "Tie::File", $iptables_save_file or croak("Error opening file $iptables_save_file $!") 1 0 1
144  
145 1 $self->{file_array} = \@file_array;
146  
147 1 bless($self, $class);
148  
149 1 return $self;
150  
151 }
152  
153  
154 =head2 table
155  
156 Set table name
157  
158 $iptables_save->table('filter');
159  
160 =cut
161  
162 1 ▶sub table {
Subroutine table: called CC=36 SCAR=43.9
Pod documented
163  
164 1 my($self, $table_name) = @_;
165  
166 # set table name
167 1 $self->{table_name} = $table_name;
168  
169 1 my $get_lines = 0;
170 1 my @rules;
171  
172 # set line of
173 1 my $i = 0;
174  
175 1 foreach my $line (@{ $self->{file_array} }) {
176  
177 7 ▶ if($line eq '*'.$self->{table_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq '*' . $self->{"table_name"}) 1 6 7
178 1 $get_lines = 1;
179 1 $self->{initial_line} = ($i + 1);
180 1 next;
181 }
182  
183 # packet counters
184 6 ▶ if($get_lines == 1 && substr($line, 0, 1) eq ':') {
Branch: 2 branches
BranchTrueFalseTotal
if ($get_lines == 1 and substr($line, 0, 1) eq ':') { } 3 3 6
elsif ($get_lines == 1 and substr($line, 0, 2) eq '-A' || substr($line, 0, 3) eq '#-A') { } 1 3 4
Condition100%3/3
$get_lines == 1 and substr($line, 0, 1) eq ":"
!ll&&!rl&&r
123
MC/DC100%2/2
$get_lines == 1 and substr($line, 0, 1) eq ":"
$get_lines == 1 substr($line, 0, 1) eq ":"
Truth table100%3/3
A and B
A= $get_lines == 1B= substr($line, 0, 1) eq ":"
ABresult
0X0
100
111
185 3 $self->{initial_line}++;
186 }
187 ▶ elsif($get_lines == 1 && (substr($line, 0, 2) eq '-A' || substr($line, 0, 3) eq '#-A')) {
Condition83%5/6
$get_lines == 1 and substr($line, 0, 2) eq "-A" || substr($line, 0, 3) eq "#-A"
!ll&&!rl&&r
111
substr($line, 0, 2) eq "-A" or substr($line, 0, 3) eq "#-A"
l!l&&r!l&&!r
101
MC/DC66%2/3
$get_lines == 1 and substr($line, 0, 2) eq "-A" || substr($line, 0, 3) eq "#-A"
$get_lines == 1 substr($line, 0, 2) eq "-A" substr($line, 0, 3) eq "#-A"
Truth table75%3/4
A and B or C
A= $get_lines == 1B= substr($line, 0, 2) eq "-A"C= substr($line, 0, 3) eq "#-A"
ABCresult
0XX0
1000
11X1
1011
188  
189 1 my $rule = {};
190  
191 # if rule is disabled
192 1 ▶ $rule->{disabled} = 1 if(substr($line, 0, 1) eq '#');
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "#" 0 1 1
193  
194 # chain
195 1 ▶ $rule->{chain} = $1 if($line =~ /-A\s+([\w]+)/g);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /-A\s+([\w]+)/g 1 0 1
196  
197 # protocol
198 1 ▶ if($line =~ /-p\s+([\!\w\d]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /-p\s+([\!\w\d]+)/g) 1 0 1
199 1 $rule->{proto} = $1;
200  
201 # when get only ! character
202 1 ▶ if($rule->{proto} =~ /^\!$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"proto"} =~ /^\!$/) 0 1 1
203 0 ▶ $rule->{proto} = '! '.$1 if($line =~ /-p\s+\!\s+([\!\w\d]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /-p\s+\!\s+([\!\w\d]+)/ 0 0 0
204 }
205 }
206  
207 # module
208 1 my @modules;
209 1 ▶ while($line =~ /-m\s+([\w\d]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /-m\s+([\w\d]+)/g) 1 1 2
210 1 push(@modules, $1);
211 }
212 1 $rule->{module} = \@modules;
213  
214 # source
215 1 ▶ if($line =~ /-s\s+([\w\d\!\-\.\/]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[-s\s+([\w\d\!\-\./]+)]g) 1 0 1
216 1 $rule->{source} = $1;
217  
218 # when get only ! character
219 1 ▶ if($rule->{source} =~ /^\!$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"source"} =~ /^\!$/) 0 1 1
220 0 ▶ $rule->{source} = '! '.$1 if($line =~ /-s\s+\!\s+([\w\d\!\-\.\/]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[-s\s+\!\s+([\w\d\!\-\./]+)] 0 0 0
221 }
222 }
223  
224 # destination
225 1 ▶ if($line =~ /-d\s+([\w\d\!\-\.\/]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[-d\s+([\w\d\!\-\./]+)]g) 1 0 1
226 1 $rule->{destination} = $1;
227  
228 # when get only ! character
229 1 ▶ if($rule->{destination} =~ /^\!$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"destination"} =~ /^\!$/) 1 0 1
230 1 ▶ $rule->{destination} = '! '.$1 if($line =~ /-d\s+\!\s+([\w\d\!\-\.\/]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[-d\s+\!\s+([\w\d\!\-\./]+)] 1 0 1
231 }
232 }
233  
234 # state
235 1 ▶ if($line =~ /--state\s+(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /--state\s+(\w+)/g) 0 1 1
236 0 $rule->{state} = $1;
237 }
238  
239 # source port
240 1 ▶ if($line =~ /--sport\s+([\w\:]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /--sport\s+([\w\:]+)/g) 0 1 1
241 0 $rule->{port_source} = $1;
242  
243 # when get only ! character
244 0 ▶ if($rule->{port_source} =~ /^\!$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"port_source"} =~ /^\!$/) 0 0 0
245 0 ▶ $rule->{port_source} = '! '.$1 if($line =~ /-d\s+\!\s+([\w\:]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /-d\s+\!\s+([\w\:]+)/ 0 0 0
246 }
247 }
248  
249 # destination port
250 1 ▶ if($line =~ /--dport\s+([\w\:]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /--dport\s+([\w\:]+)/g) 1 0 1
251 1 $rule->{port_destination} = $1;
252  
253 # when get only ! character
254 1 ▶ if($rule->{port_destination} =~ /^\!$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"port_destination"} =~ /^\!$/) 0 1 1
255 0 ▶ $rule->{port_destination} = '! '.$1 if($line =~ /-d\s+\!\s+([\w\:]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /-d\s+\!\s+([\w\:]+)/ 0 0 0
256 }
257 }
258  
259  
260 # target
261 1 ▶ $rule->{target} = $1 if($line =~ /-j\s+([\w]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /-j\s+([\w]+)/ 1 0 1
262  
263 # target param1
264 1 ▶ $rule->{target_param1} = $2 if($line =~ /-j\s+(.*?)\s+([\w\-]*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /-j\s+(.*?)\s+([\w\-]*)/ 1 0 1
265  
266 # target param2
267 1 ▶ $rule->{target_param2} = $3 if($line =~ /-j\s+(.*?)\s+([\w\-]*)\s+([\w\-\.\:]*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /-j\s+(.*?)\s+([\w\-]*)\s+([\w\-\.\:]*)/ 0 1 1
268  
269  
270 # prevent target_param1 and targer_param2 from get --comment
271 1 ▶ if($rule->{target_param1} && $rule->{target_param1} eq '--comment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"target_param1"} and $rule->{"target_param1"} eq "--comment") 0 1 1
Condition33%1/3
$rule->{"target_param1"} and $rule->{"target_param1"} eq "--comment"
!ll&&!rl&&r
100
MC/DC0%0/2
$rule->{"target_param1"} and $rule->{"target_param1"} eq "--comment"
$rule->{"target_param1"} $rule->{"target_param1"} eq "--comment"
Truth table33%1/3
A and B
A= $rule->{"target_param1"}B= $rule->{"target_param1"} eq "--comment"
ABresult
0X0
100
111
272 0 $rule->{target_param1} = '';
273 0 $rule->{target_param2} = '';
274 }
275  
276  
277 # comment
278 1 ▶ $rule->{comment} = $1 if($line =~ /--comment\s+\"(.*)\"/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /--comment\s+\"(.*)\"/ 0 1 1
279  
280  
281 1 push(@rules, $rule);
282  
283 }
284  
285 6 ▶ last if($get_lines == 1 && $line eq 'COMMIT');
Branch: 1 branch
BranchTrueFalseTotal
if $get_lines == 1 and $line eq "COMMIT" 1 5 6
Condition100%3/3
$get_lines == 1 and $line eq "COMMIT"
!ll&&!rl&&r
141
MC/DC100%2/2
$get_lines == 1 and $line eq "COMMIT"
$get_lines == 1 $line eq "COMMIT"
Truth table100%3/3
A and B
A= $get_lines == 1B= $line eq "COMMIT"
ABresult
0X0
100
111
286  
287 5 $i++;
288  
289 }
290  
291 # number of rules
292 1 $self->{number_of_rules} = @rules;
293  
294 # save rules on object
295 1 $self->{rules} = \@rules;
296  
297 1 ▶ return $self->{table_name} if($self->{table_name});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"table_name"} 1 0 1
298  
299 0 return 0;
300  
301 }
302  
303  
304 =head2 fetch_rules
305  
306 =cut
307  
308 0 ▶sub fetch_rules {
Subroutine fetch_rules: not called CC=4 SCAR=30.0
Pod documented
309  
310 0 my $self = shift;
311  
312 0 ▶ croak("You need set a table name: \$obj->table('tablename');") if(! $self->{table_name});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table_name"} 0 0 0
313  
314 # set index foreach row
315 0 my @trules;
316 0 my $i = 0;
317 0 foreach my $row (@{ $self->{rules} }) {
318  
319 0 $row->{id} = $i;
320 0 $i++;
321  
322 0 push(@trules, $row);
323  
324 }
325  
326 # save trules on object
327 0 $self->{rules} = \@trules;
328  
329 # if wants a array
330 0 ▶ return @{ $self->{rules} } if wantarray();
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
331  
332 # if wants a arrayref
333 0 return $self->{rules};
334  
335 }
336  
337  
338 =head2 DESTROY
339  
340 =cut
341  
342 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
343 1 my $self = shift;
344 1 untie $self->{file_array};
345 }
346  
347  
348 =head2 move
349  
350 Move rules
351  
352 # Move rule of index 1, 3 positions down
353 $iptables_save->move(1, 3);
354  
355 =cut
356  
357 0 ▶sub move {
Subroutine move: not called CC=4 SCAR=30.0
Pod documented
358  
359 0 my($self, $index, $move) = @_;
360  
361 # Get rule
362 0 my $rule = @{ $self->{rules}}[$index];
363  
364 # Prevent eg: index 0, move -1
365 0 ▶ if($move < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move < 0) 0 0 0
366 0 $move *= -1;
367 0 ▶ return if(($index - $move) < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $index - $move < 0 0 0 0
368 }
369  
370 # delete rule on current position
371 0 delete @{ $self->{rules}}[$index];
372  
373 # prevent erro where $move > 0
374 0 ▶ $move++ if($move > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $move > 0 0 0 0
375  
376 # replace
377 0 splice(@{ $self->{rules}}, ($index + $move), 0, ($rule));
378  
379 }
380  
381  
382 =head2 save
383  
384 $iptables_save->save();
385  
386 =cut
387  
388 0 ▶sub save {
Subroutine save: not called CC=17 SCAR=57.2
Pod documented
389 0 my $self = shift;
390  
391 0 my @trules;
392  
393 0 foreach my $rule (@{ $self->{rules} }) {
394  
395 # if deleted rule
396 0 ▶ next if(!$rule);
Branch: 1 branch
BranchTrueFalseTotal
unless $rule 0 0 0
397  
398 # Mount rule
399 0 my $str_rule = '';
400  
401 # if rule is disabled
402 0 ▶ $str_rule .= '#' if($rule->{disabled});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"disabled"} 0 0 0
403  
404 # chain
405 0 $str_rule .= '-A '.$rule->{chain}.' ';
406  
407 # protocol
408 0 ▶ $str_rule .= '-p '.$rule->{proto}.' ' if($rule->{proto});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"proto"} 0 0 0
409  
410 # modules
411 0 foreach my $module (@{ $rule->{module} }) {
412 0 $str_rule .= '-m '.$module.' ';
413 }
414  
415 # interface input
416 0 ▶ $str_rule .= '-i '.$rule->{iface_input}.' ' if($rule->{iface_input});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"iface_input"} 0 0 0
417  
418 # interface output
419 0 ▶ $str_rule .= '-o '.$rule->{iface_output}.' ' if($rule->{iface_output});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"iface_output"} 0 0 0
420  
421 # source
422 0 ▶ $str_rule .= '-s '.$rule->{source}.' ' if($rule->{source});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"source"} 0 0 0
423  
424 # destination
425 0 ▶ $str_rule .= '-d '.$rule->{destination}.' ' if($rule->{destination});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"destination"} 0 0 0
426  
427 # state
428 0 ▶ $str_rule .= '--state '.$rule->{state}.' ' if($rule->{state});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"state"} 0 0 0
429  
430 # source port
431 0 ▶ $str_rule .= '--sport '.$rule->{port_source}.' ' if($rule->{port_source});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"port_source"} 0 0 0
432  
433 # destination port
434 0 ▶ $str_rule .= '--dport '.$rule->{port_destination}.' ' if($rule->{port_destination});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"port_destination"} 0 0 0
435  
436 # target
437 0 ▶ $str_rule .= '-j '.$rule->{target}.' ' if($rule->{target});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"target"} 0 0 0
438  
439 # target param1
440 0 ▶ $str_rule .= $rule->{target_param1}.' ' if($rule->{target_param1});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"target_param1"} 0 0 0
441  
442 # target param2
443 0 ▶ $str_rule .= $rule->{target_param2}.' ' if($rule->{target_param2});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"target_param2"} 0 0 0
444  
445 # comment
446 0 ▶ $str_rule .= '--comment "'.$rule->{comment}.'" ' if($rule->{comment});
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"comment"} 0 0 0
447  
448 0 push(@trules, $str_rule);
449 }
450  
451 0 splice(@{ $self->{file_array} }, $self->{initial_line}, $self->{number_of_rules}, @trules);
452  
453 }
454  
455  
456 =head1 AUTHOR
457  
458 Geovanny Junio, C<< <geovannyjs at gmail.com> >>
459  
460 =head1 BUGS
461  
462 Please report any bugs or feature requests to C<bug-parser-iptables-save at rt.cpan.org>, or through
463 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parser-IPTables-Save>. I will be notified, and then you'll
464 automatically be notified of progress on your bug as I make changes.
465  
466  
467  
468  
469 =head1 SUPPORT
470  
471 You can find documentation for this module with the perldoc command.
472  
473 perldoc Parser::IPTables::Save
474  
475  
476 You can also look for information at:
477  
478 =over 4
479  
480 =item * RT: CPAN's request tracker
481  
482 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parser-IPTables-Save>
483  
484 =item * AnnoCPAN: Annotated CPAN documentation
485  
486 L<http://annocpan.org/dist/Parser-IPTables-Save>
487  
488 =item * CPAN Ratings
489  
490 L<http://cpanratings.perl.org/d/Parser-IPTables-Save>
491  
492 =item * Search CPAN
493  
494 L<http://search.cpan.org/dist/Parser-IPTables-Save/>
495  
496 =back
497  
498  
499 =head1 ACKNOWLEDGEMENTS
500  
501  
502 =head1 LICENSE AND COPYRIGHT
503  
504 Copyright 2010 Geovanny Junio.
505  
506 This program is free software; you can redistribute it and/or modify it
507 under the terms of either: the GNU General Public License as published
508 by the Free Software Foundation; or the Artistic License.
509  
510 See http://dev.perl.org/licenses/ for more information.
511  
512  
513 =cut
514  
515 1 1; # End of Parser::IPTables::Save
↑ Summary