blib/lib/IPTables/Mangle.pm

filter: Statementstmt 95.4% 42 / 44 Branchbran 66.6% 4 / 6 Conditioncond 53.3% 8 / 15 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 80.2% 61 / 76 CC 17 SCAR 24.8
CC
17
Cov
83%
_process_table
16.6
_process_rule
4.1
BEGIN
1.0
CRAP 12.0
↑ Summary
1 package IPTables::Mangle;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.04';
6  
7 =head1 NAME
8  
9 IPTables::Mangle - Manage iptables rules with Perl / YAML
10  
11 =head1 SYNOPSIS
12  
13 Given a config file, produces rules for iptables-restore.
14  
15 Example YAML file, for ease of viewing:
16  
17 filter:
18 forward: { default: drop }
19 foo:
20 rules:
21 - src: 9.9.9.9
22 - src: 10.10.10.10
23 action: drop
24 input:
25 # by default, do not allow any connections unless authorized
26 # in the rules below
27 default: drop
28  
29 # by default, if no "action" is given to a rule below, accept it
30 default_rule_action: accept
31  
32 rules:
33 # Accept all traffic on loopback interface
34 - in-interface: lo
35  
36 # Don't disconnect existing connections during a rule change.
37 - { match: state, state: 'ESTABLISHED,RELATED' }
38  
39 # Allow for pings (no more than 10 a second)
40 - { protocol: icmp, icmp-type: 8, match: limit, limit: 10/sec }
41  
42 # Allow these IPs, no matter what
43 - src: 123.123.123.123
44  
45 # example of blocking an IP
46 - { action: drop, src: 8.8.8.8 }
47  
48 # example of allowing ip to connect to port 25 (smtp) (one-line)
49 - { protocol: tcp, dport: 25, src: 4.2.2.2 }
50  
51 # jump to rules defined in "foo" above
52 - action: foo
53  
54 # if there are no more rules, reject the connection with icmp, don't just let it hang
55 - action: reject
56 action_options:
57 reject-with: icmp-admin-prohibited
58  
59 =head1 DESCRIPTION
60  
61 This module allows for the management of iptables rules with Perl / YAML.
62  
63 =head1 TABLES
64  
65 The top hashref is the table for iptables, this can be either mangle, nat, or filter.
66  
67 =head1 CHAINS
68  
69 The hashref under the top hashref is the chain name. For system chains the default chainrule can
70 be set by setting a default hashref in the chain.
71  
72 $VAR1->{filter}{input} would be the input chain for the filter table.
73  
74 =head1 CHAIN RULES
75  
76 Chainrules live in a 'rules' arrayref under the chain, $VAR1->{filter}{input}{rules}, for example.
77  
78 Every rule in the chain is a hashref which builds a rule. By default, the jump in the rules, referenced
79 as 'action' in a rule, is set to accept. The default action can be modified by changing
80 'default_rule_action' in the chain. Every key in the rule's hashref represents a parameter prefixed by two dashes, '--',
81 in an iptables rule. Two things to note here are that 'action' in a rule really maps to 'jump' in iptables, and
82 a special action_options key exists, which references a hashref, which appends options after the iptables jump. This is
83 useful for things like setting '--reject-with' after a jump to reject.
84  
85  
86 Examples of a chain rule:
87  
88  
89 # by default, allow this ip
90  
91 $VAR1->{filter}{input}{rules}[0] = { src => '10.10.10.10' } ;
92  
93  
94 # allow this ip on port 25 tcp, using accept default
95  
96 $VAR1->{filter}{input}{rules}[1] = { protocol: 'tcp', dport: 25, src => '10.10.10.10' } ;
97  
98  
99 # make it explicit
100  
101 $VAR1->{filter}{input}{rules}[2] = { protocol: 'tcp', dport: 25, src => '10.10.10.10', action => 'accept' } ;
102  
103  
104 # blacklist an ip
105  
106 $VAR1->{filter}{input}{rules}[3] = { src => '10.10.10.10', action => 'drop' } ;
107  
108 # reject with icmp message
109  
110 $VAR1->{filter}{input}{rules}[-1] = {
111 action => 'reject',
112 action_options => {
113 reject-with: 'icmp-admin-prohibited',
114 },
115 };
116  
117 =cut
118  
119 =head1 METHODS
120  
121 =head2 process_config
122  
123 Given a hashref, produces rules usable by iptables-restore.
124  
125 Returns one string.
126  
127 =cut
128  
129 sub process_config
130 1 ▶{
Subroutine process_config: called CC=1 SCAR=0.0
Pod documented
131 1 my $config = shift;
132  
133 1 my $config_out = '';
134  
135 1 $config_out .= _process_table({
136 table => 'mangle',
137 chains => [ qw(prerouting input forward output postrouting) ],
138 config => $config,
139 });
140  
141 1 $config_out .= _process_table({
142 table => 'nat',
143 chains => [ qw(prerouting postrouting output) ],
144 config => $config,
145 });
146  
147 1 $config_out .= _process_table({
148 table => 'filter',
149 chains => [ qw(input forward output) ],
150 config => $config,
151 });
152  
153 1 return $config_out;
154 }
155  
156 sub _process_table
157 3 ▶{
Subroutine _process_table: called CC=14 SCAR=28.1
158 3 my $args = shift;
159  
160 3 my $table = $args->{table};
161 3 my $config = $args->{config};
162  
163 3 my $table_out = '';
164 3 $table_out .= "*$table\n";
165  
166 # configure built-in chains
167 $table_out .= ":" . uc($_) . ' ' .
168 (exists $config->{$table}{$_} ?
169 uc($config->{$table}{$_}{default}) : 'ACCEPT') . "\n"
170 3 ▶ for @{$args->{chains}};
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{$table}{$_} ? : 2 9 11
171  
172 # configure custom chains
173 3 ▶ for my $chain (keys %{$config->{$table} || {} })
Condition50%1/2
$config->{$table} || {}
l!l
30
MC/DC0%0/1
$config->{$table} || {}
$config->{$table}
Truth table50%1/2
A
A= $config->{$table}
Aresult
11
00
174 {
175 # skip built-in chains
176 2 ▶ next if grep { $chain eq $_ } @{$args->{chains}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$chain eq $_;} @{$$args{"chains"};} 2 0 2
177  
178 0 $table_out .= ":" . uc($chain) . " -\n";
179 }
180  
181 3 ▶ for my $chain (keys %{$config->{$table} || {} })
Condition50%1/2
$config->{$table} || {}
l!l
30
MC/DC0%0/1
$config->{$table} || {}
$config->{$table}
Truth table50%1/2
A
A= $config->{$table}
Aresult
11
00
182 {
183 $table_out .= &_process_rule({
184 target => (
185 uc($_->{action} || '')
186 || uc($config->{$table}{$chain}{default_rule_action} || '')
187 || 'ACCEPT'),
188 chain => uc($chain),
189 rule => $_
190 2 ▶ }) . "\n" for @{$config->{filter}{$chain}{rules} || []};
Condition54%6/11
$config->{'filter'}{$chain}{'rules'} || []
l!l
11
uc($_->{'action'} || '') || uc($config->{$table}{$chain}{'default_rule_action'} || '') || 'ACCEPT'
l!l
80
uc($_->{"action"} || "") or uc($config->{$table}{$chain}{"default_rule_action"} || "")
l!l&&r!l&&!r
080
$_->{'action'} || ''
l!l
08
$config->{$table}{$chain}{'default_rule_action'} || ''
l!l
80
MC/DC100%1/1
$config->{'filter'}{$chain}{'rules'} || []
$config->{'filter'}{$chain}{'rules'}
MC/DC0%0/2
uc($_->{'action'} || '') || uc($config->{$table}{$chain}{'default_rule_action'} || '') || 'ACCEPT'
$_->{'action'} $config->{$table}{$chain}{'default_rule_action'}
Truth table100%2/2
A
A= $config->{'filter'}{$chain}{'rules'}
Aresult
11
00
Truth table33%1/3
A or B
A= $_->{'action'}B= $config->{$table}{$chain}{'default_rule_action'}
ABresult
1X1
011
000
191 }
192  
193 3 $table_out .= "COMMIT\n";
194  
195 3 return $table_out;
196 }
197  
198 sub _process_rule
199 8 ▶{
Subroutine _process_rule: called CC=4 SCAR=14.0
200 8 my $args = shift;
201 8 my $output = "-A $args->{chain} ";
202  
203 # remove action from rule
204 8 delete $args->{rule}{action};
205  
206 # grab action options
207 8 my $action_opts = delete $args->{rule}{action_options};
208 8 my $action_opts_str = '';
209  
210 8 ▶ if ($action_opts)
Branch: 1 branch
BranchTrueFalseTotal
if ($action_opts) 0 8 8
211 {
212 $action_opts_str .= "--$_ $action_opts->{$_} "
213 0 for keys %$action_opts;
214 }
215  
216 $output .= "--$_ $args->{rule}{$_} "
217 8 for keys %{$args->{rule}};
218  
219 8 $output .= "-j $args->{target} $action_opts_str";
220  
221 8 return $output;
222 }
223  
224 1 1;
225  
226 =head1 AUTHORS
227  
228 Bizowie <http://bizowie.com>
229  
230 =head1 COPYRIGHT AND LICENSE
231  
232 Copyright (C) 2013 Bizowie
233  
234 This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
235  
236 =cut
↑ Summary