Firewall/iptables.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 307 CC 38 SCAR 56.5
CC
38
Cov
0%
fw_unblockallow
506.0
fw_blockallow
182.0
init
30.0
CRAP 284.7
1 package VUser::Firewall::iptables;
2 use warnings;
3 use strict;
4  
5 # Copyright 2005 Randy Smith <perlstalker@vuser.org>
6 # $Id: iptables.pm,v 1.1 2006/01/04 18:45:40 perlstalker Exp $
7  
8 use File::Temp;
9 use File::Copy;
10 use VUser::ExtLib qw(:config :ssh :files);
11 use VUser::Log qw(:levels);
12  
13 my $log;
14 my %meta;
15  
16 my $VERSION = '0.1.0';
17 my $c_sec = 'Extension Firewall::iptables';
18  
19 my $def_chain; # Default chain to operate on
20 my %hosts; # List of hosts
21  
22 sub depends { return qw(Firewall); }
23  
24 sub init
25 {
26 my $eh = shift;
27 my %cfg = @_;
28  
29 %meta = %VUser::Firewall::meta;
30 $log = $main::log;
31  
32 $def_chain = strip_ws($cfg{$c_sec}{'default chain'}) || 'FIREWALL';
33 $log->log(LOG_DEBUG, "Setting default chain to %s", $def_chain);
34  
35 $meta{'chain'} = VUser::Meta->new('name' => 'chain',
36 'type' => 'string',
37 'description' => sprintf('Chain to add rules to. Default: %s', $def_chain)
38 );
39  
40 foreach my $section (grep { /^$c_sec-(.+)$/o } keys %cfg) {
41 my $host = $section;
42 $host =~ s/^$c_sec-(.+)$/$1/;
43 $log->log(LOG_DEBUG, "Checking section: %s. Host=%s", $section, $host);
44 next if check_bool($cfg{$section}{skip});
45 $hosts{$host} = {};
46 foreach my $key ('file', 'host', 'user', 'ssh key',
47 'restart') {
48 my $tmp = strip_ws($cfg{$section}{$key});
49 $hosts{$host}{$key} = $tmp if $tmp;
50 }
51  
52 if (not $hosts{$host}{file}) {
53 $log->log(LOG_NOTICE, "No file specified for $host. Skipping.");
54 delete $hosts{$host};
55 }
56 }
57  
58 foreach my $action ('block', 'unblock', 'allow', 'unallow') {
59 $eh->register_option('firewall', $action, $meta{'chain'});
60 }
61  
62 $eh->register_task('firewall', 'block', \&fw_blockallow);
63 $eh->register_task('firewall', 'allow', \&fw_blockallow);
64 $eh->register_task('firewall', 'unblock', \&fw_unblockallow);
65 $eh->register_task('firewall', 'unallow', \&fw_unblockallow);
66 $eh->register_task('firewall', 'restart', \&fw_restart);
67  
68 if (check_bool($cfg{$VUser::Firewall::c_sec}{'auto restart'})) {
69 foreach my $action ('block', 'unblock', 'allow', 'unallow') {
70 $eh->register_task('firewall', $action, \&fw_restart);
71 }
72 }
73 }
74  
75 sub fw_blockallow
76 {
77 my ($cfg, $opts, $action, $eh) = @_;
78  
79 if (not $opts->{source}
80 and not $opts->{sport}
81 and not $opts->{destination}
82 and not $opts->{dport}
83 ) {
84 die "One of source, destination, sport or dport must be sepcified.\n";
85 }
86  
87 my $cmd = 'iptables';
88  
89 $cmd .= sprintf(' -A %s', $opts->{chain}? $opts->{chain} : $def_chain);
90 $cmd .= sprintf(' -s %s', $opts->{source}) if ($opts->{source});
91 $cmd .= sprintf(' --sport %s', $opts->{sport}) if ($opts->{sport});
92 $cmd .= sprintf(' -d %s', $opts->{destination}) if ($opts->{destination});
93 $cmd .= sprintf(' --dport %s', $opts->{dport}) if ($opts->{dport});
94 $cmd .= sprintf(' -p %s', $opts->{protocol}) if $opts->{protocol};
95 if ($action eq 'allow') {
96 $cmd .= ' -j ACCEPT';
97 } elsif ($action eq 'block') {
98 $cmd .= ' -j REJECT';
99 }
100  
101 $log->log(LOG_NOTICE, "Sending %s", $cmd);
102  
103 foreach my $host (keys %hosts) {
104 $log->log(LOG_NOTICE, "Updating firewall on %s", $host);
105
106 my $file = $hosts{$host}->{file};
107 $file =~ s!/?([^/]+)$!$1!; # Rip the path off the file name
108  
109 my $local_file = File::Temp::tempnam('/tmp', 'VUser-Firewall-iptables-');
110 $log->log(LOG_DEBUG, "Tmp file is %s", $local_file);
111 if ($hosts{$host}->{host}) {
112 get_file_scp($hosts{$host}->{user},
113 $hosts{$host}->{host},
114 $hosts{$host}->{'ssh key'},
115 $hosts{$host}->{file},
116 $local_file);
117 } else {
118 copy($hosts{$host}->{file}, $local_file);
119 touch($local_file) unless (-e $local_file);
120 }
121  
122 add_line_to_file($local_file, $cmd);
123  
124 if ($hosts{$host}->{user}) {
125 send_file_scp($hosts{$host}->{user},
126 $hosts{$host}->{host},
127 $hosts{$host}->{'ssh key'},
128 $local_file,
129 $hosts{$host}->{file}
130 );
131 } else {
132 copy($local_file, $hosts{$host}->{file});
133 }
134  
135 unlink $local_file;
136 }
137 }
138  
139 sub fw_unblockallow
140 {
141 my ($cfg, $opts, $action, $eh) = @_;
142  
143 if (not $opts->{source}
144 and not $opts->{sport}
145 and not $opts->{destination}
146 and not $opts->{dport}
147 ) {
148 die "One of source, destination, sport or dport must be sepcified.\n";
149 }
150  
151 foreach my $host (keys %hosts) {
152 $log->log(LOG_NOTICE, "Updating firewall on %s", $host);
153
154 my $file = $hosts{$host}->{file};
155 $file =~ s!/?([^/]+)$!$1!; # Rip the path off the file name
156  
157 my $local_file = File::Temp::tempnam('/tmp', 'VUser-Firewall-iptables-');
158  
159 if ($hosts{$host}->{host}) {
160 get_file_scp($hosts{$host}->{user},
161 $hosts{$host}->{host},
162 $hosts{$host}->{'ssh key'},
163 $hosts{$host}->{file},
164 $local_file);
165 } else {
166 copy($hosts{$host}{file}, $local_file);
167 }
168  
169 # Pull the rule(s) out of the file
170 open ORIG, $local_file or die "Can't open $local_file: $!\n";
171 open NEW, ">$local_file.new" or die "Can't open $local_file.new: $!\n";
172  
173 while (my $line = <ORIG>) {
174 chomp $line;
175  
176 my $match = 0;
177 my $test = 0;
178 if ($opts->{source}) {
179 $test++;
180 $log->log(LOG_DEBUG, "Testing source ($test)");
181 if ($line =~ m{\s(?:-s|--source) # source flag
182 [ =]?\s* # spaces or =
183 \Q$opts->{source}\E # The addr to match
184 (?:\s|$ ) # end of pattern
185 }x
186 ) {
187 $match++;
188 $log->log(LOG_DEBUG, "Rule matches source %d/%d",
189 $match, $test);
190 }
191 }
192  
193 if ($opts->{destination}) {
194 $test++;
195 $log->log(LOG_DEBUG, "Testing dest");
196 if ($line =~ m{\s(?:-d|-destination) # dst flag
197 [ =]?\s* # spaces or =
198 \Q$opts->{source}\E # The addr to match
199 (?:\s|$ ) # end of pattern
200 }x
201 ) {
202 $match++;
203 $log->log(LOG_DEBUG, "Rule matches dest %d/%d",
204 $match, $test);
205 }
206 }
207  
208 if ($opts->{sport}) {
209 $test++;
210 $log->log(LOG_DEBUG, "Testing sport");
211 if ($line =~ m{\s(?:--sport|--source-port)
212 [ =]?\s* # spaces or =
213 \Q$opts->{sport}\E # The addr to match
214 (?:\s|$ ) # end of pattern
215 }x
216 ) {
217 $match++;
218 $log->log(LOG_DEBUG, "Rule matches sport %d/%d",
219 $match, $test);
220 }
221 }
222  
223 if ($opts->{dport}) {
224 $test++;
225 $log->log(LOG_DEBUG, "Testing dport");
226 if ($line =~ m{\s(?:--dport|--destination-port)
227 [ =]?\s* # spaces or =
228 \Q$opts->{dport}\E # The addr to match
229 (?:\s|$ ) # end of pattern
230 }x
231 ) {
232 $match++;
233 $log->log(LOG_DEBUG, "Rule matches dport %d/%d",
234 $match, $test);
235 }
236 }
237  
238 if ($opts->{protocol}) {
239 $test++;
240 $log->log(LOG_DEBUG, "Testing protocol");
241 if ($line =~ m{\s(?:-p|--protocol) # source flag
242 [ =]?\s* # spaces or =
243 \Q$opts->{protocol}\E# The proto to match
244 (?:\s|$ ) # end of pattern
245 }x
246 ) {
247 $match++;
248 $log->log(LOG_DEBUG, "Rule matches protocol %d/%d",
249 $match, $test);
250 }
251 }
252  
253 if ($action eq 'block'
254 or $action eq 'unblock'
255 ) {
256 $test++;
257 $log->log(LOG_DEBUG, "Testing REJECT");
258 if ($line =~ m{\s(?:-j|--jump)
259 [ =]?\s* # spaces or =
260 REJECT # The rule to match
261 (?:\s|$ ) # end of pattern
262 }x
263 ) {
264 $match++;
265 $log->log(LOG_DEBUG, "Rule matches REJECT %d/%d",
266 $match, $test);
267 }
268 }
269  
270 if ($action eq 'accept'
271 or $action eq 'unaccept'
272 ) {
273 $test++;
274 $log->log(LOG_DEBUG, "Testing ACCEPT");
275 if ($line =~ m{\s(?:-j|--jump)
276 [ =]?\s* # spaces or =
277 ACCEPT # The rule to match
278 (?:\s|$ ) # end of pattern
279 }x
280 ) {
281 $match++;
282 $log->log(LOG_DEBUG, "Rule matches ACCEPT %d/%d",
283 $match, $test);
284 }
285 }
286  
287 my $chain = $opts->{chain} || $def_chain;
288  
289 if ($chain) {
290 $test++;
291 $log->log(LOG_DEBUG, "Testing chain %s", $chain);
292 if ($line =~ m{\s(?:-A|--append) # source flag
293 [ =]?\s* # spaces or =
294 \Q$chain\E # The chain to match
295 (?:\s|$ ) # end of pattern
296 }x
297 ) {
298 $match++;
299 $log->log(LOG_DEBUG, "Rule matches chain %d/%d",
300 $match, $test);
301 }
302 }
303  
304 $log->log(LOG_DEBUG, "Line (%d/%d): %s", $match, $test, $line);
305  
306 # The line didn't match in all the tests, let it through.
307 print NEW "$line\n" if $match != $test;
308 #print STDERR "Printing matches\n";
309 #print STDERR "$line\n" if $match != $test;
310 }
311  
312 close NEW;
313 close ORIG;
314  
315 rename("$local_file.new", $local_file);
316  
317 if ($hosts{$host}{user}) {
318 send_file_scp($hosts{$host}->{user},
319 $hosts{$host}->{host},
320 $hosts{$host}->{'ssh key'},
321 $local_file,
322 $hosts{$host}->{file}
323 );
324 } else {
325 copy($local_file, $hosts{$host}{file});
326 }
327  
328 if ($main::DEBUG) {
329 $log->log(LOG_DEBUG, "Debug mode: preserving $local_file.new\n");
330 } else {
331 unlink $local_file, "$local_file.new";
332 }
333 }
334 }
335  
336 sub fw_restart
337 {
338 my ($cfg, $opts, $action, $eh) = @_;
339  
340 foreach my $host (keys %hosts) {
341 $log->log(LOG_NOTICE, "Restarting firewall on %s", $host);
342 run_cmd_ssh($hosts{$host}->{user}, $hosts{$host}->{host}, $hosts{$host}->{'ssh key'},
343 $hosts{$host}->{restart});
344 }
345 }
346  
347 1;
348  
349 __END__