check netfilter_canonical_protocol() is not called with arrayref
[ferm.git] / src / ferm
blob5a760d292934209a22dc2a8785a6c0eaf766591e
1 #!/usr/bin/perl
4 # ferm, a firewall setup program that makes firewall rules easy!
6 # Copyright (C) 2001-2007 Auke Kok, Max Kellermann
8 # Comments, questions, greetings and additions to this program
9 # may be sent to <ferm@foo-projects.org>
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 # $Id$
30 BEGIN {
31 eval { require strict; import strict; };
32 $has_strict = not $@;
33 if ($@) {
34 # we need no vars.pm if there is not even strict.pm
35 $INC{'vars.pm'} = 1;
36 *vars::import = sub {};
37 } else {
38 require IO::Handle;
41 eval { require Getopt::Long; import Getopt::Long; };
42 $has_getopt = not $@;
45 use vars qw($has_strict $has_getopt);
47 use vars qw($DATE $VERSION);
49 # subversion keyword magic
50 $DATE = '$Date$' =~ m,(\d{4})-(\d\d)-(\d\d), ? $1.$2.$3 : '';
52 $VERSION = '2.0';
53 $VERSION .= '~svn' . $DATE;
55 ## interface variables
56 # %option = command line and other options
57 use vars qw(%option);
59 ## hooks
60 use vars qw(@pre_hooks @post_hooks);
62 ## parser variables
63 # $script: current script file
64 # @stack = ferm's parser stack containing local variables
65 # $auto_chain = index for the next auto-generated chain
66 use vars qw($script @stack $auto_chain);
68 ## netfilter variables
69 # %domains = state information about all domains ("ip" and "ip6")
70 # - initialized: domain initialization is done
71 # - tools: hash providing the paths of the domain's tools
72 # - previous: save file of the previous ruleset, for rollback
73 # - reset: has this domain already been reset?
74 # - tables{$name}: ferm state information about tables
75 # - chains{$chain}: ferm state information about the chains
76 # - builtin: whether this is a built-in chain
77 # - was_created: custom chain has been created
78 # - non_empty: are there rules for this chain?
79 use vars qw(%domains);
81 ## constants
82 use vars qw(%deprecated_keywords);
84 # keywords from ferm 1.1 which are deprecated, and the new one; these
85 # are automatically replaced, and a warning is printed
86 %deprecated_keywords = ( goto => 'jump',
89 # these hashes provide the Netfilter module definitions
90 use vars qw(%proto_defs %match_defs %target_defs);
93 # This subsubsystem allows you to support (most) new netfilter modules
94 # in ferm. Add a call to one of the "add_XY_def()" functions below.
96 # Ok, now about the cryptic syntax: the function "add_XY_def()"
97 # registers a new module. There are three kinds of modules: protocol
98 # module (e.g. TCP, ICMP), match modules (e.g. state, physdev) and
99 # target modules (e.g. DNAT, MARK).
101 # The first parameter is always the module name which is passed to
102 # iptables with "-p", "-m" or "-j" (depending on which kind of module
103 # this is).
105 # After that, you add an encoded string for each option the module
106 # supports. This is where it becomes tricky.
108 # foo defaults to an option with one argument (which may be a ferm
109 # array)
111 # foo*0 option without any arguments
113 # foo=s one argument which must not be a ferm array ('s' stands for
114 # 'scalar')
116 # u32=m an array which renders into multiple iptables options in one
117 # rule
119 # ctstate=c one argument, if it's an array, pass it to iptables as a
120 # single comma separated value; example:
121 # ctstate (ESTABLISHED RELATED) translates to:
122 # --ctstate ESTABLISHED,RELATED
124 # foo=sac three arguments: scalar, array, comma separated; you may
125 # concatenate more than one letter code after the '='
127 # foo&bar one argument; call the perl function '&bar()' which parses
128 # the argument
130 # !foo negation is allowed and the '!' is written before the keyword
132 # foo! same as above, but '!' is after the keyword and before the
133 # parameters
135 # to:=to-destination makes "to" an alias for "to-destination"; you have
136 # to add a declaration for option "to-destination"
139 # add a module definition
140 sub add_def_x {
141 my $defs = shift;
142 my $domain_family = shift;
143 my $name = shift;
144 die if exists $defs->{$domain_family}{$name};
145 my $def = $defs->{$domain_family}{$name} = {};
146 foreach (@_) {
147 my $keyword = $_;
148 my $k = {};
150 my $params = 1;
151 $params = $1 if $keyword =~ s,\*(\d+)$,,;
152 $params = $1 if $keyword =~ s,=([acs]+|m)$,,;
153 if ($keyword =~ s,&(\S+)$,,) {
154 $params = eval "\\&$1";
155 die $@ if $@;
157 $k->{params} = $params if $params;
159 $k->{negation} = $k->{pre_negation} = 1 if $keyword =~ s,^!,,;
160 $k->{negation} = 1 if $keyword =~ s,!$,,;
162 $k->{alias} = $1 if $keyword =~ s,:=(\S+)$,,;
164 $def->{keywords}{$keyword} = $k;
167 return $def;
170 # add a protocol module definition
171 sub add_proto_def_x(@) {
172 add_def_x(\%proto_defs, @_);
175 # add a match module definition
176 sub add_match_def_x(@) {
177 add_def_x(\%match_defs, @_);
180 # add a target module definition
181 sub add_target_def_x(@) {
182 add_def_x(\%target_defs, @_);
185 sub add_def {
186 my $defs = shift;
187 add_def_x($defs, 'ip', @_);
190 # add a protocol module definition
191 sub add_proto_def(@) {
192 add_def(\%proto_defs, @_);
195 # add a match module definition
196 sub add_match_def(@) {
197 add_def(\%match_defs, @_);
200 # add a target module definition
201 sub add_target_def(@) {
202 add_def(\%target_defs, @_);
205 add_proto_def 'dccp', qw(dccp-types!=c dccp-option!);
206 add_proto_def 'mh', qw(mh-type!);
207 add_proto_def 'icmp', qw(icmp-type!);
208 add_proto_def 'icmpv6', qw(icmpv6-type! icmp-type:=icmpv6-type);
209 add_proto_def 'sctp', qw(chunk-types!=sc);
210 add_proto_def 'tcp', qw(tcp-flags!=cc !syn*0 tcp-option! mss);
211 add_proto_def 'udp', qw();
213 add_match_def '',
214 # --protocol
215 qw(protocol! proto:=protocol),
216 # --source, --destination
217 qw(source! saddr:=source destination! daddr:=destination),
218 # --in-interface
219 qw(in-interface! interface:=in-interface if:=in-interface),
220 # --out-interface
221 qw(out-interface! outerface:=out-interface of:=out-interface),
222 # --fragment
223 qw(!fragment*0);
224 add_match_def 'account', qw(aaddr=s aname=s ashort*0);
225 add_match_def 'addrtype', qw(src-type dst-type);
226 add_match_def 'ah', qw(ahspi! ahlen! ahres*0);
227 add_match_def 'comment', qw(comment=s);
228 add_match_def 'condition', qw(condition!);
229 add_match_def 'connbytes', qw(!connbytes connbytes-dir connbytes-mode);
230 add_match_def 'connlimit', qw(!connlimit-above connlimit-mask);
231 add_match_def 'connmark', qw(mark);
232 add_match_def 'conntrack', qw(ctstate=c ctproto ctorigsrc! ctorigdst!),
233 qw(ctreplsrc! ctrepldst! ctstatus ctexpire=s);
234 add_match_def 'dscp', qw(dscp dscp-class);
235 add_match_def 'ecn', qw(ecn-tcp-cwr*0 ecn-tcp-ece*0 ecn-ip-ect);
236 add_match_def 'esp', qw(espspi!);
237 add_match_def 'eui64';
238 add_match_def 'fuzzy', qw(lower-limit=s upper-limit=s);
239 add_match_def 'hbh', qw(hbh-len! hbh-opts=c);
240 add_match_def 'helper', qw(helper);
241 add_match_def 'hl', qw(hl-eq! hl-lt=s hl-gt=s);
242 add_match_def 'length', qw(length!);
243 add_match_def 'hashlimit', qw(hashlimit=s hashlimit-burst=s hashlimit-mode=s hashlimit-name=s),
244 qw(hashlimit-htable-size=s hashlimit-htable-max=s),
245 qw(hashlimit-htable-expire=s hashlimit-htable-gcinterval=s);
246 add_match_def 'iprange', qw(!src-range !dst-range);
247 add_match_def 'ipv6header', qw(header!=c soft*0);
248 add_match_def 'limit', qw(limit=s limit-burst=s);
249 add_match_def 'mac', qw(mac-source!);
250 add_match_def 'mark', qw(mark);
251 add_match_def 'multiport', qw(source-ports!&multiport_params),
252 qw(destination-ports!&multiport_params ports!&multiport_params);
253 add_match_def 'nth', qw(every counter start packet);
254 add_match_def 'owner', qw(uid-owner gid-owner pid-owner sid-owner cmd-owner);
255 add_match_def 'physdev', qw(physdev-in! physdev-out!),
256 qw(!physdev-is-in*0 !physdev-is-out*0 !physdev-is-bridged*0);
257 add_match_def 'pkttype', qw(pkt-type),
258 add_match_def 'policy',
259 qw(dir pol strict*0 reqid spi proto mode tunnel-src tunnel-dst next*0);
260 add_match_def 'psd', qw(psd-weight-threshold psd-delay-threshold),
261 qw(psd-lo-ports-weight psd-hi-ports-weight);
262 add_match_def 'quota', qw(quota=s);
263 add_match_def 'random', qw(average);
264 add_match_def 'realm', qw(realm!);
265 add_match_def 'recent', qw(name=s !set*0 !rcheck*0 !update*0 !seconds !hitcount rttl*0);
266 add_match_def 'rt', qw(rt-type! rt-segsleft! rt-len! rt-0-res*0 rt-0-addrs=c rt-0-not-strict*0);
267 add_match_def 'set', qw(set=sc);
268 add_match_def 'state', qw(state=c);
269 add_match_def 'statistic', qw(mode=s probability=s every=s packet=s);
270 add_match_def 'tcpmss', qw(!mss);
271 add_match_def 'time', qw(timestart=s timestop=s days=c datestart=s datestop=s);
272 add_match_def 'tos', qw(!tos);
273 add_match_def 'ttl', qw(ttl-eq ttl-lt=s ttl-gt=s);
274 add_match_def 'u32', qw(u32=m);
276 add_target_def 'BALANCE', qw(to-destination to:=to-destination);
277 add_target_def 'CLASSIFY', qw(set-class);
278 add_target_def 'CONNMARK', qw(set-mark save-mark*0 restore-mark*0 mask);
279 add_target_def 'CONNSECMARK', qw(save*0 restore*0);
280 add_target_def 'DNAT', qw(to-destination to:=to-destination);
281 add_target_def 'DSCP', qw(set-dscp set-dscp-class);
282 add_target_def 'ECN', qw(ecn-tcp-remove*0);
283 add_target_def 'HL', qw(hl-set hl-dec hl-inc);
284 add_target_def 'LOG', qw(log-level log-prefix),
285 qw(log-tcp-sequence*0 log-tcp-options*0 log-ip-options*0 log-uid*0);
286 add_target_def 'MARK', qw(set-mark);
287 add_target_def 'MASQUERADE', qw(to-ports);
288 add_target_def 'MIRROR';
289 add_target_def 'NETMAP', qw(to);
290 add_target_def 'NFLOG', qw(nflog-group nflog-prefix nflog-range nflog-threshold);
291 add_target_def 'NFQUEUE', qw(queue-num);
292 add_target_def 'NOTRACK';
293 add_target_def 'REDIRECT', qw(to-ports);
294 add_target_def 'REJECT', qw(reject-with);
295 add_target_def 'ROUTE', qw(oif iif gw continue*0 tee*0);
296 add_target_def 'SAME', qw(to nodst*0);
297 add_target_def 'SECMARK', qw(selctx);
298 add_target_def 'SET', qw(add-set=sc del-set=sc);
299 add_target_def 'SNAT', qw(to-source=m to:=to-source);
300 add_target_def 'TARPIT';
301 add_target_def 'TCPMSS', qw(set-mss clamp-mss-to-pmtu*0);
302 add_target_def 'TOS', qw(set-tos);
303 add_target_def 'TRACE';
304 add_target_def 'TTL', qw(ttl-set ttl-dec ttl-inc);
305 add_target_def 'ULOG', qw(ulog-nlgroup ulog-prefix ulog-cprange ulog-qthreshold);
307 add_match_def_x 'arp', '',
308 # ip
309 qw(source-ip! destination-ip! saddr:=source-ip daddr:=destination-ip),
310 # mac
311 qw(source-mac! destination-mac!),
312 # --in-interface
313 qw(in-interface! interface:=in-interface if:=in-interface),
314 # --out-interface
315 qw(out-interface! outerface:=out-interface of:=out-interface),
316 # misc
317 qw(h-length=s opcode=s h-type=s proto-type=s),
318 qw(mangle-ip-s=s mangle-ip-d=s mangle-mac-s=s mangle-mac-d=s mangle-target=s);
320 add_match_def_x 'eb', '',
321 # protocol
322 qw(protocol! proto:=protocol),
323 # --in-interface
324 qw(in-interface! interface:=in-interface if:=in-interface),
325 # --out-interface
326 qw(out-interface! outerface:=out-interface of:=out-interface),
327 # logical interface
328 qw(logical-in! logical-out!),
329 # --source, --destination
330 qw(source! saddr:=source destination! daddr:=destination),
331 # 802.3
332 qw(802_3-sap! 802_3-type!),
333 # arp
334 qw(arp-opcode! arp-htype!=ss arp-ptype!=ss),
335 qw(arp-ip-src! arp-ip-dst! arp-mac-src! arp-mac-dst!),
336 # ip
337 qw(ip-source! ip-destination! ip-tos! ip-protocol! ip-sport! ip-dport!),
338 # mark_m
339 qw(mark!),
340 # pkttype
341 qw(pkttype-type!),
342 # stp
343 qw(stp-type! stp-flags! stp-root-prio! stp-root-addr! stp-root-cost!),
344 qw(stp-sender-prio! stp-sender-addr! stp-port! stp-msg-age! stp-max-age!),
345 qw(stp-hello-time! stp-forward-delay!),
346 # vlan
347 qw(vlan-id! vlan-prio! vlan-encap!),
348 # log
349 qw(log*0 log-level=s log-prefix=s log-ip*0 log-arp*0);
351 add_target_def_x 'eb', 'arpreply', qw(arpreply-mac arpreply-target);
352 add_target_def_x 'eb', 'dnat', qw(to-destination dnat-target);
353 add_target_def_x 'eb', 'mark', qw(set-mark mark-target);
354 add_target_def_x 'eb', 'redirect', qw(redirect-target);
355 add_target_def_x 'eb', 'snat', qw(to-source snat-target);
357 # parameter parser for ipt_multiport
358 sub multiport_params {
359 my $fw = shift;
361 # multiport only allows 15 ports at a time. For this
362 # reason, we do a little magic here: split the ports
363 # into portions of 15, and handle these portions as
364 # array elements
366 my $proto = $fw->{builtin}{protocol};
367 error('To use multiport, you have to specify "proto tcp" or "proto udp" first')
368 unless defined $proto and grep { /^(?:tcp|udp|udplite)$/ } to_array($proto);
370 my $value = getvalues(undef, undef,
371 allow_negation => 1,
372 allow_array_negation => 1);
373 if (ref $value and ref $value eq 'ARRAY') {
374 my @value = @$value;
375 my @params;
377 while (@value) {
378 push @params, join(',', splice(@value, 0, 15));
381 return @params == 1
382 ? $params[0]
383 : \@params;
384 } else {
385 return join_value(',', $value);
389 # initialize stack: command line definitions
390 unshift @stack, {};
392 # Get command line stuff
393 if ($has_getopt) {
394 my ($opt_noexec, $opt_flush, $opt_lines, $opt_interactive,
395 $opt_verbose, $opt_debug,
396 $opt_help,
397 $opt_version, $opt_test, $opt_fast, $opt_shell,
398 $opt_domain);
400 Getopt::Long::Configure('bundling', 'auto_help', 'no_ignore_case',
401 'no_auto_abbrev');
403 sub opt_def {
404 my ($opt, $value) = @_;
405 die 'Invalid --def specification'
406 unless $value =~ /^\$?(\w+)=(.*)$/s;
407 my ($name, $unparsed_value) = ($1, $2);
408 my @tokens = tokenize_string($unparsed_value);
409 my $value = getvalues(\&next_array_token, \@tokens);
410 die 'Extra tokens after --def'
411 if @tokens;
412 $stack[0]{vars}{$name} = $value;
415 local $SIG{__WARN__} = sub { die $_[0]; };
416 GetOptions('noexec|n' => \$opt_noexec,
417 'flush|F' => \$opt_flush,
418 'lines|l' => \$opt_lines,
419 'interactive|i' => \$opt_interactive,
420 'verbose|v' => \$opt_verbose,
421 'debug|d' => \$opt_debug,
422 'help|h' => \$opt_help,
423 'version|V' => \$opt_version,
424 test => \$opt_test,
425 remote => \$opt_test,
426 fast => \$opt_fast,
427 shell => \$opt_shell,
428 'domain=s' => \$opt_domain,
429 'def=s' => \&opt_def,
432 if (defined $opt_help) {
433 require Pod::Usage;
434 Pod::Usage::pod2usage(-exitstatus => 0);
437 if (defined $opt_version) {
438 printversion();
439 exit 0;
442 $option{'noexec'} = (defined $opt_noexec);
443 $option{flush} = defined $opt_flush;
444 $option{'lines'} = (defined $opt_lines);
445 $option{interactive} = (defined $opt_interactive);
446 $option{test} = (defined $opt_test);
448 if ($option{test}) {
449 $option{noexec} = 1;
450 $option{lines} = 1;
453 delete $option{interactive} if $option{noexec};
455 mydie('ferm interactive mode not possible: /dev/stdin is not a tty')
456 if $option{interactive} and not -t STDIN;
457 mydie('ferm interactive mode not possible: /dev/stderr is not a tty')
458 if $option{interactive} and not -t STDERR;
460 $option{fast} = 1 if defined $opt_fast;
462 if (defined $opt_shell) {
463 $option{$_} = 1 foreach qw(shell fast lines);
466 $option{domain} = $opt_domain if defined $opt_domain;
468 print STDERR "Warning: ignoring the obsolete --debug option\n"
469 if defined $opt_debug;
470 print STDERR "Warning: ignoring the obsolete --verbose option\n"
471 if defined $opt_verbose;
472 } else {
473 # tiny getopt emulation for microperl
474 my $filename;
475 foreach (@ARGV) {
476 if ($_ eq '--noexec' or $_ eq '-n') {
477 $option{noexec} = 1;
478 } elsif ($_ eq '--lines' or $_ eq '-l') {
479 $option{lines} = 1;
480 } elsif ($_ eq '--fast') {
481 $option{fast} = 1;
482 } elsif ($_ eq '--test') {
483 $option{test} = 1;
484 $option{noexec} = 1;
485 $option{lines} = 1;
486 } elsif ($_ eq '--shell') {
487 $option{$_} = 1 foreach qw(shell fast lines);
488 } elsif (/^-/) {
489 printf STDERR "Usage: ferm [--noexec] [--lines] [--fast] [--shell] FILENAME\n";
490 exit 1;
491 } else {
492 $filename = $_;
495 undef @ARGV;
496 push @ARGV, $filename;
499 unless (@ARGV == 1) {
500 require Pod::Usage;
501 Pod::Usage::pod2usage(-exitstatus => 1);
504 if ($has_strict) {
505 open LINES, ">&STDOUT" if $option{lines};
506 open STDOUT, ">&STDERR" if $option{shell};
507 } else {
508 # microperl can't redirect file handles
509 *LINES = *STDOUT;
511 if ($option{fast} and not $option{noexec}) {
512 print STDERR "Sorry, ferm on microperl does not allow --fast without --noexec\n";
513 exit 1
517 unshift @stack, {};
518 open_script($ARGV[0]);
520 # parse all input recursively
521 enter(0);
522 die unless @stack == 2;
524 # check consistency
525 check();
527 # execute all generated rules
528 my $status;
530 foreach my $cmd (@pre_hooks) {
531 print LINES "$cmd\n" if $option{lines};
532 system($cmd) unless $option{noexec};
535 while (my ($domain, $domain_info) = each %domains) {
536 next unless $domain_info->{enabled};
537 my $s = $option{fast} &&
538 defined $domain_info->{tools}{'tables-restore'}
539 ? execute_fast($domain, $domain_info)
540 : execute_slow($domain, $domain_info);
541 $status = $s if defined $s;
544 foreach my $cmd (@post_hooks) {
545 print "$cmd\n" if $option{lines};
546 system($cmd) unless $option{noexec};
549 if (defined $status) {
550 rollback();
551 exit $status;
554 # ask user, and rollback if there is no confirmation
556 confirm_rules() or rollback() if $option{interactive};
558 exit 0;
560 # end of program execution!
563 # funcs
565 sub printversion {
566 print "ferm $VERSION\n";
567 print "Copyright (C) 2001-2007 Auke Kok, Max Kellermann\n";
568 print "This program is free software released under GPLv2.\n";
569 print "See the included COPYING file for license details.\n";
573 sub mydie {
574 print STDERR @_;
575 print STDERR "\n";
576 exit 1;
580 sub error {
581 # returns a nice formatted error message, showing the
582 # location of the error.
583 my $tabs = 0;
584 my @lines;
585 my $l = 0;
586 my @words = map { @$_ } @{$script->{past_tokens}};
588 for my $w ( 0 .. $#words ) {
589 if ($words[$w] eq "\x29")
590 { $l++ ; $lines[$l] = " " x ($tabs-- -1) ;};
591 if ($words[$w] eq "\x28")
592 { $l++ ; $lines[$l] = " " x $tabs++ ;};
593 if ($words[$w] eq "\x7d")
594 { $l++ ; $lines[$l] = " " x ($tabs-- -1) ;};
595 if ($words[$w] eq "\x7b")
596 { $l++ ; $lines[$l] = " " x $tabs++ ;};
597 if ( $l > $#lines ) { $lines[$l] = "" };
598 $lines[$l] .= $words[$w] . " ";
599 if ($words[$w] eq "\x28")
600 { $l++ ; $lines[$l] = " " x $tabs ;};
601 if (($words[$w] eq "\x29") && ($words[$w+1] ne "\x7b"))
602 { $l++ ; $lines[$l] = " " x $tabs ;};
603 if ($words[$w] eq "\x7b")
604 { $l++ ; $lines[$l] = " " x $tabs ;};
605 if (($words[$w] eq "\x7d") && ($words[$w+1] ne "\x7d"))
606 { $l++ ; $lines[$l] = " " x $tabs ;};
607 if (($words[$w] eq "\x3b") && ($words[$w+1] ne "\x7d"))
608 { $l++ ; $lines[$l] = " " x $tabs ;}
609 if ($words[$w-1] eq "option")
610 { $l++ ; $lines[$l] = " " x $tabs ;}
612 my $start = $#lines - 4;
613 if ($start < 0) { $start = 0 } ;
614 print STDERR "Error in $script->{filename} line $script->{line}:\n";
615 for $l ( $start .. $#lines)
616 { print STDERR $lines[$l]; if ($l != $#lines ) {print STDERR "\n"} ; };
617 print STDERR "<--\n";
618 mydie(@_);
621 # print a warning message about code from an input file
622 sub warning {
623 print STDERR "Warning in $script->{filename} line $script->{line}: "
624 . (shift) . "\n";
627 sub find_tool($) {
628 my $name = shift;
629 return $name if $option{test};
630 for my $path ('/sbin', split ':', $ENV{PATH}) {
631 my $ret = "$path/$name";
632 return $ret if -x $ret;
634 die "$name not found in PATH\n";
637 sub initialize_domain {
638 my $domain = shift;
640 return if exists $domains{$domain}{initialized};
642 die "Invalid domain '$domain'\n" unless $domain =~ /^(?:ip6?|arp|eb)$/;
644 my @tools = qw(tables);
645 push @tools, qw(tables-save tables-restore)
646 if $domain =~ /^ip6?$/;
648 # determine the location of this domain's tools
649 foreach my $tool (@tools) {
650 $domains{$domain}{tools}{$tool} = find_tool("${domain}${tool}");
653 # make tables-save tell us about the state of this domain
654 # (which tables and chains do exist?), also remember the old
655 # save data which may be used later by the rollback function
656 local *SAVE;
657 if (!$option{test} &&
658 exists $domains{$domain}{tools}{'tables-save'} &&
659 open(SAVE, "$domains{$domain}{tools}{'tables-save'}|")) {
660 my $save = '';
662 my $table_info;
663 while (<SAVE>) {
664 $save .= $_;
666 if (/^\*(\w+)/) {
667 my $table = $1;
668 $table_info = $domains{$domain}{tables}{$table} ||= {};
669 } elsif (defined $table_info and /^:(\w+)\s+(\S+)/
670 and $2 ne '-') {
671 $table_info->{chains}{$1}{builtin} = 1;
672 $table_info->{has_builtin} = 1;
676 # for rollback
677 $domains{$domain}{previous} = $save;
680 $domains{$domain}{initialized} = 1;
683 # split the an input string into words and delete comments
684 sub tokenize_string($) {
685 my $string = shift;
687 my @ret;
689 foreach my $word ($string =~ m/(".*?"|'.*?'|`.*?`|[!,=&\$\%\(\){};]|[-+\w\/\.:]+|@\w+|#)/g) {
690 last if $word eq '#';
691 push @ret, $word;
694 return @ret;
697 # shift an array; helper function to be passed to &getvar / &getvalues
698 sub next_array_token {
699 my $array = shift;
700 shift @$array;
703 # read some more tokens from the input file into a buffer
704 sub prepare_tokens() {
705 my $tokens = $script->{tokens};
706 while (@$tokens == 0) {
707 my $handle = $script->{handle};
708 my $line = <$handle>;
709 return unless defined $line;
711 $script->{line} ++;
713 my @line = tokenize_string($line);
715 # the next parser stage eats this
716 push @$tokens, @line;
719 return 1;
722 # open a ferm sub script
723 sub open_script($) {
724 my $filename = shift;
726 for (my $s = $script; defined $s; $s = $s->{parent}) {
727 mydie("Circular reference in $script->{filename} line $script->{line}: $filename")
728 if $s->{filename} eq $filename;
731 local *FILE;
732 open FILE, "<$filename"
733 or mydie("Failed to open $filename: $!");
734 my $handle = *FILE;
736 $script = { filename => $filename,
737 handle => $handle,
738 line => 0,
739 past_tokens => [],
740 tokens => [],
741 parent => $script,
744 return $script;
747 # collect script filenames which are being included
748 sub collect_filenames(@) {
749 my @ret;
751 # determine the current script's parent directory for relative
752 # file names
753 die unless defined $script;
754 my $parent_dir = $script->{filename} =~ m,^(.*/),
755 ? $1 : './';
757 foreach my $pathname (@_) {
758 # non-absolute file names are relative to the parent script's
759 # file name
760 $pathname = $parent_dir . $pathname
761 unless $pathname =~ m,^/,;
763 if ($pathname =~ m,/$,) {
764 # include all regular files in a directory
766 error("'$pathname' is not a directory")
767 unless -d $pathname;
769 local *DIR;
770 opendir DIR, $pathname
771 or error("Failed to open directory '$pathname': $!");
772 my @names = readdir DIR;
773 closedir DIR;
775 # sort those names for a well-defined order
776 foreach my $name (sort { $a cmp $b } @names) {
777 # don't include hidden and backup files
778 next if /^\.|~$/;
780 my $filename = $pathname . $name;
781 push @ret, $filename
782 if -f $filename;
784 } elsif ($pathname =~ m,\|$,) {
785 # run a program and use its output
786 push @ret, $pathname;
787 } elsif ($pathname =~ m,^\|,) {
788 error('This kind of pipe is not allowed');
789 } else {
790 # include a regular file
792 error("'$pathname' is a directory; maybe use trailing '/' to include a directory?")
793 if -d $pathname;
794 error("'$pathname' is not a file")
795 unless -f $pathname;
797 push @ret, $pathname;
801 return @ret;
804 # peek a token from the queue, but don't remove it
805 sub peek_token() {
806 return unless prepare_tokens();
807 return $script->{tokens}[0];
810 # get a token from the queue
811 sub next_token() {
812 return unless prepare_tokens();
813 my $token = shift @{$script->{tokens}};
815 # update $script->{past_tokens}
816 my $past_tokens = $script->{past_tokens};
818 if (@$past_tokens > 0) {
819 my $prev_token = $past_tokens->[-1][-1];
820 $past_tokens->[-1] = @$past_tokens > 1 ? ['{'] : []
821 if $prev_token eq ';';
822 pop @$past_tokens
823 if $prev_token eq '}';
826 push @$past_tokens, [] if $token eq '{' or @$past_tokens == 0;
827 push @{$past_tokens->[-1]}, $token;
829 # return
830 return $token;
833 # require that another token exists, and that it's not a "special"
834 # token, e.g. ";" and "{"
835 sub require_next_token {
836 my $code = shift || \&next_token;
838 my $token = &$code(@_);
840 error('unexpected end of file')
841 unless defined $token;
843 error("'$token' not allowed here")
844 if $token =~ /^[;{}]$/;
846 return $token;
849 # return the value of a variable
850 sub variable_value($) {
851 my $name = shift;
853 foreach (@stack) {
854 return $_->{vars}{$name}
855 if exists $_->{vars}{$name};
858 return $stack[0]{auto}{$name}
859 if exists $stack[0]{auto}{$name};
861 return;
864 # determine the value of a variable, die if the value is an array
865 sub string_variable_value($) {
866 my $name = shift;
867 my $value = variable_value($name);
869 error("variable '$name' must be a string, is an array")
870 if ref $value;
872 return $value;
875 # similar to the built-in "join" function, but also handle negated
876 # values in a special way
877 sub join_value($$) {
878 my ($expr, $value) = @_;
880 unless (ref $value) {
881 return $value;
882 } elsif (ref $value eq 'ARRAY') {
883 return join($expr, @$value);
884 } elsif (ref $value eq 'negated') {
885 # bless'negated' is a special marker for negated values
886 $value = join_value($expr, $value->[0]);
887 return bless [ $value ], 'negated';
888 } else {
889 die;
893 # returns the next parameter, which may either be a scalar or an array
894 sub getvalues {
895 my ($code, $param) = (shift, shift);
896 my %options = @_;
898 my $token = require_next_token($code, $param);
900 if ($token eq '(') {
901 # read an array until ")"
902 my @wordlist;
904 for (;;) {
905 $token = getvalues($code, $param,
906 parenthesis_allowed => 1,
907 comma_allowed => 1);
909 unless (ref $token) {
910 last if $token eq ')';
912 if ($token eq ',') {
913 error('Comma is not allowed within arrays, please use only a space');
914 next;
917 push @wordlist, $token;
918 } elsif (ref $token eq 'ARRAY') {
919 push @wordlist, @$token;
920 } else {
921 error('unknown toke type');
925 error('empty array not allowed here')
926 unless @wordlist or not $options{non_empty};
928 return @wordlist == 1
929 ? $wordlist[0]
930 : \@wordlist;
931 } elsif ($token =~ /^\`(.*)\`$/s) {
932 # execute a shell command, insert output
933 my $command = $1;
934 my $output = `$command`;
935 unless ($? == 0) {
936 if ($? == -1) {
937 error("failed to execute: $!");
938 } elsif ($? & 0x7f) {
939 error("child died with signal " . ($? & 0x7f));
940 } elsif ($? >> 8) {
941 error("child exited with status " . ($? >> 8));
945 # remove comments
946 $output =~ s/#.*//mg;
948 # tokenize
949 my @tokens = grep { length } split /\s+/s, $output;
951 my @values;
952 while (@tokens) {
953 my $value = getvalues(\&next_array_token, \@tokens);
954 push @values, to_array($value);
957 # and recurse
958 return @values == 1
959 ? $values[0]
960 : \@values;
961 } elsif ($token =~ /^\'(.*)\'$/s) {
962 # single quotes: a string
963 return $1;
964 } elsif ($token =~ /^\"(.*)\"$/s) {
965 # double quotes: a string with escapes
966 $token = $1;
967 $token =~ s,\$(\w+),string_variable_value($1),eg;
968 return $token;
969 } elsif ($token eq '!') {
970 error('negation is not allowed here')
971 unless $options{allow_negation};
973 $token = getvalues($code, $param);
975 error('it is not possible to negate an array')
976 if ref $token and not $options{allow_array_negation};
978 return bless [ $token ], 'negated';
979 } elsif ($token eq ',') {
980 return $token
981 if $options{comma_allowed};
983 error('comma is not allowed here');
984 } elsif ($token eq '=') {
985 error('equals operator ("=") is not allowed here');
986 } elsif ($token eq '$') {
987 my $name = require_next_token($code, $param);
988 error('variable name expected - if you want to concatenate strings, try using double quotes')
989 unless $name =~ /^\w+$/;
991 my $value = variable_value($name);
993 error("no such variable: \$$name")
994 unless defined $value;
996 return $value;
997 } elsif ($token eq '&') {
998 error("function calls are not allowed as keyword parameter");
999 } elsif ($token eq ')' and not $options{parenthesis_allowed}) {
1000 error('Syntax error');
1001 } elsif ($token =~ /^@/) {
1002 if ($token eq '@resolve') {
1003 my @params = get_function_params();
1004 error('Usage: @resolve((hostname ...))')
1005 unless @params == 1;
1006 eval { require Net::DNS; };
1007 error('For the @resolve() function, you need the Perl library Net::DNS')
1008 if $@;
1009 my $type = 'A';
1010 my $resolver = new Net::DNS::Resolver;
1011 my @result;
1012 foreach my $hostname (to_array($params[0])) {
1013 my $query = $resolver->search($hostname, $type);
1014 error("DNS query for '$hostname' failed: " . $resolver->errorstring)
1015 unless $query;
1016 foreach my $rr ($query->answer) {
1017 next unless $rr->type eq $type;
1018 push @result, $rr->address;
1021 return \@result;
1022 } else {
1023 error("unknown ferm built-in function");
1025 } else {
1026 return $token;
1030 # returns the next parameter, but only allow a scalar
1031 sub getvar {
1032 my $token = getvalues(@_);
1034 error('array not allowed here')
1035 if ref $token and ref $token eq 'ARRAY';
1037 return $token;
1040 sub get_function_params(%) {
1041 my $token = next_token();
1042 error('function name must be followed by "()"')
1043 unless defined $token and $token eq '(';
1045 $token = peek_token();
1046 if ($token eq ')') {
1047 require_next_token;
1048 return;
1051 my @params;
1053 while (1) {
1054 if (@params > 0) {
1055 $token = require_next_token();
1056 last
1057 if $token eq ')';
1059 error('"," expected')
1060 unless $token eq ',';
1063 push @params, getvalues(undef, undef, @_);
1066 return @params;
1069 # collect all tokens in a flat array reference until the end of the
1070 # command is reached
1071 sub collect_tokens() {
1072 my @level;
1073 my @tokens;
1075 while (1) {
1076 my $keyword = next_token();
1077 error('unexpected end of file within function/variable declaration')
1078 unless defined $keyword;
1080 if ($keyword =~ /^[\{\(]$/) {
1081 push @level, $keyword;
1082 } elsif ($keyword =~ /^[\}\)]$/) {
1083 my $expected = $keyword;
1084 $expected =~ tr/\}\)/\{\(/;
1085 my $opener = pop @level;
1086 error("unmatched '$keyword'")
1087 unless defined $opener and $opener eq $expected;
1088 } elsif ($keyword eq ';' and @level == 0) {
1089 last;
1092 push @tokens, $keyword;
1094 last
1095 if $keyword eq '}' and @level == 0;
1098 return \@tokens;
1102 # returns the specified value as an array. dereference arrayrefs
1103 sub to_array($) {
1104 my $value = shift;
1105 die unless wantarray;
1106 die if @_;
1107 unless (ref $value) {
1108 return $value;
1109 } elsif (ref $value eq 'ARRAY') {
1110 return @$value;
1111 } else {
1112 die;
1116 # evaluate the specified value as bool
1117 sub eval_bool($) {
1118 my $value = shift;
1119 die if wantarray;
1120 die if @_;
1121 unless (ref $value) {
1122 return $value;
1123 } elsif (ref $value eq 'ARRAY') {
1124 return @$value > 0;
1125 } else {
1126 die;
1130 sub is_netfilter_core_target($) {
1131 my $target = shift;
1132 die unless defined $target and length $target;
1134 return $target =~ /^(?:ACCEPT|DROP|RETURN|QUEUE)$/;
1137 sub is_netfilter_module_target($$) {
1138 my ($domain_family, $target) = @_;
1139 die unless defined $target and length $target;
1141 return defined $domain_family &&
1142 exists $target_defs{$domain_family} &&
1143 exists $target_defs{$domain_family}{$target};
1146 sub is_netfilter_builtin_chain($$) {
1147 my ($table, $chain) = @_;
1149 return grep { $_ eq $chain }
1150 qw(PREROUTING INPUT FORWARD OUTPUT POSTROUTING);
1153 sub netfilter_canonical_protocol($) {
1154 my $proto = shift;
1155 return unless defined $proto;
1156 return 'icmpv6'
1157 if $proto eq 'ipv6-icmp';
1158 return 'mh'
1159 if $proto eq 'ipv6-mh';
1160 return $proto;
1163 sub netfilter_protocol_module($) {
1164 my $proto = shift;
1165 return unless defined $proto;
1166 return 'icmp6'
1167 if $proto eq 'icmpv6';
1168 return $proto;
1171 # escape the string in a way safe for the shell
1172 sub shell_escape($) {
1173 my $token = shift;
1175 return $token if $token =~ /^[-_a-zA-Z0-9]+$/s;
1177 if ($option{fast}) {
1178 # iptables-save/iptables-restore are quite buggy concerning
1179 # escaping and special characters... we're trying our best
1180 # here
1182 $token =~ s,",',g;
1183 $token = '"' . $token . '"'
1184 if $token =~ /[\s\'\\;&]/s;
1185 } else {
1186 return $token
1187 if $token =~ /^\`.*\`$/;
1188 $token =~ s/'/\\'/g;
1189 $token = '\'' . $token . '\''
1190 if $token =~ /[\s\"\\;<>&|]/s;
1193 return $token;
1196 # append an option to the shell command line, using information from
1197 # the module definition (see %match_defs etc.)
1198 sub shell_append_option($$$$) {
1199 my ($ref, $def, $keyword, $value) = @_;
1201 my $negated = '';
1202 if (ref $value and ref $value eq 'negated') {
1203 $value = $value->[0];
1205 if (exists $def->{pre_negation}) {
1206 $$ref .= ' !';
1207 } else {
1208 $negated = ' !';
1212 unless (defined $value) {
1213 $$ref .= " --$keyword";
1214 } elsif (ref $value and ref $value eq 'params') {
1215 $$ref .= " --$keyword$negated ";
1216 $$ref .= join(' ', map { shell_escape($_) } @$value);
1217 } elsif (ref $value and ref $value eq 'multi') {
1218 foreach (@$value) {
1219 $$ref .= " --$keyword " . shell_escape($_);
1221 } else {
1222 $$ref .= " --$keyword$negated " . shell_escape($value);
1226 # dereference a bless'negated'
1227 sub extract_negation($) {
1228 local $_ = shift;
1229 ref && ref eq 'negated'
1230 ? ( '!', $_->[0] )
1231 : $_;
1234 # convert an internal rule structure into an iptables call
1235 sub tables($) {
1236 my $rule = shift;
1238 my $domain = $rule->{domain};
1239 my $domain_info = $domains{$domain};
1240 $domain_info->{enabled} = 1;
1241 my $domain_family = $rule->{domain_family};
1243 my $table = $rule->{table};
1244 my $table_info = $domain_info->{tables}{$table} ||= {};
1246 my $chain = $rule->{chain};
1247 my $chain_info = $table_info->{chains}{$chain} ||= {};
1248 my $chain_rules = $chain_info->{rules} ||= [];
1250 return if $option{flush};
1252 my $action = $rule->{action};
1254 # mark this chain as "non-empty" because we will add stuff to
1255 # it now; this flag is later used to check if a custom chain
1256 # referenced by "jump" was actually defined
1257 $chain_info->{non_empty} = 1;
1259 # check if the chain is already defined
1260 unless (exists $chain_info->{create} or
1261 is_netfilter_builtin_chain($table, $chain)) {
1262 $chain_info->{create} = 1;
1265 # check for unknown jump target
1266 if (defined $action and
1267 ($action->{type} eq 'jump' or
1268 $action->{type} eq 'goto') and
1269 not exists $table_info->{chains}{$action->{chain}}{create}) {
1270 my $chain = $action->{chain};
1271 $table_info->{chains}{$chain}{create} = 1;
1274 # target=policy is a special case
1275 if ($action->{type} eq 'policy') {
1276 $chain_info->{policy} = $action->{policy};
1277 return;
1280 # return if this is a declaration-only rule
1281 return
1282 unless $rule->{has_rule};
1284 my $rr = '';
1286 # general iptables options
1288 while (my ($keyword, $value) = each %{$rule->{builtin}}) {
1289 my $def = $match_defs{$domain_family}{''}{keywords}{$keyword};
1290 die unless defined $def;
1292 shell_append_option(\$rr, $def, $keyword, $value);
1296 # match module options
1299 my %modules;
1301 if (defined $rule->{builtin}{protocol}) {
1302 my $proto = $rule->{builtin}{protocol};
1304 # special case: --dport and --sport for TCP/UDP
1305 if ($domain_family eq 'ip' and
1306 (exists $rule->{dport} or exists $rule->{sport}) and
1307 $proto =~ /^(?:tcp|udp|udplite|dccp|sctp)$/) {
1308 unless (exists $modules{$proto}) {
1309 $rr .= " -m $proto";
1310 $modules{$proto} = 1;
1313 shell_append_option(\$rr, { params => 1,
1314 negation => 1,
1315 }, 'dport', $rule->{dport})
1316 if exists $rule->{dport};
1317 shell_append_option(\$rr, { params => 1,
1318 negation => 1,
1319 }, 'sport', $rule->{sport})
1320 if exists $rule->{sport};
1324 # modules stored in %match_defs
1326 foreach my $match (@{$rule->{match}}) {
1327 my $module_name = $match->{name};
1328 unless (exists $modules{$module_name}) {
1329 $rr .= " -m $module_name";
1330 $modules{$module_name} = 1;
1333 my $defs = $match->{defs};
1335 while (my ($keyword, $value) = each %{$match->{options}}) {
1336 my $def = $defs->{keywords}{$keyword};
1337 die unless defined $def;
1339 shell_append_option(\$rr, $def, $keyword, $value);
1344 # target options
1347 if ($action->{type} eq 'jump') {
1348 $rr .= " -j " . shell_escape($action->{chain});
1349 } elsif ($action->{type} eq 'goto') {
1350 $rr .= " -g " . shell_escape($action->{chain});
1351 } elsif ($action->{type} eq 'target') {
1352 $rr .= " -j " . shell_escape($action->{target});
1354 # targets stored in %target_defs
1356 while (my ($keyword, $value) = each %{$rule->{target_options}}) {
1357 my $def = $action->{defs}{keywords}{$keyword};
1358 die unless defined $def;
1360 shell_append_option(\$rr, $def, $keyword, $value);
1362 } elsif ($action->{type} ne 'nop') {
1363 die;
1366 # this line is done
1367 push @$chain_rules, { rule => $rr,
1368 script => $rule->{script},
1372 sub transform_rule($) {
1373 my $rule = shift;
1375 $rule->{builtin}{protocol} = 'icmpv6'
1376 if $rule->{domain} eq 'ip6' and $rule->{builtin}{protocol} eq 'icmp';
1379 sub printrule($) {
1380 my $rule = shift;
1382 transform_rule($rule);
1384 # prints all rules in a hash
1385 tables($rule);
1389 sub check_unfold(\@$$) {
1390 my ($unfold, $parent, $key) = @_;
1392 return unless ref $parent->{$key} and
1393 ref $parent->{$key} eq 'ARRAY';
1395 push @$unfold, $parent, $key, $parent->{$key};
1398 # convert a bunch of internal rule structures in iptables calls,
1399 # unfold arrays during that
1400 sub mkrules($) {
1401 # compile the list hashes into rules
1402 my $fw = shift;
1404 my @unfold;
1406 foreach my $key (qw(domain table chain)) {
1407 check_unfold(@unfold, $fw, $key);
1410 foreach my $key (keys %{$fw->{builtin}}) {
1411 check_unfold(@unfold, $fw->{builtin}, $key);
1414 foreach my $match (@{$fw->{match}}) {
1415 while (my ($key, $value) = each %{$match->{options}}) {
1416 check_unfold(@unfold, $match->{options}, $key);
1420 check_unfold(@unfold, $fw, 'sport');
1421 check_unfold(@unfold, $fw, 'dport');
1423 if (@unfold == 0) {
1424 printrule($fw);
1425 return;
1428 sub dofr {
1429 my $fw = shift;
1430 my ($parent, $key, $values) = (shift, shift, shift);
1432 foreach my $value (@$values) {
1433 $parent->{$key} = $value;
1435 if (@_) {
1436 dofr($fw, @_);
1437 } else {
1438 printrule($fw);
1443 dofr($fw, @unfold);
1446 sub filter_domains($) {
1447 my $domains = shift;
1448 my $result = [];
1450 foreach my $domain (to_array $domains) {
1451 next if exists $option{domain}
1452 and $domain ne $option{domain};
1454 eval {
1455 initialize_domain($domain);
1457 error($@) if $@;
1459 push @$result, $domain;
1462 return @$result == 1 ? $result->[0] : $result;
1465 # parse tokens from builtin match modules
1466 sub parse_builtin_matches($$$) {
1467 my ($current, $keyword, $negated_ref) = @_;
1469 my $domain_family = $current->{domain_family};
1471 if (exists $match_defs{$domain_family}) {
1472 parse_option('', $match_defs{$domain_family}{''},
1473 $current, $current->{builtin},
1474 $keyword, $negated_ref)
1475 and return 1;
1478 return;
1481 # parse a keyword from a module definition
1482 sub parse_keyword($$$$) {
1483 my ($current, $def, $keyword, $negated_ref) = @_;
1485 my $params = $def->{params};
1487 my $value;
1489 my $negated;
1490 if ($$negated_ref && exists $def->{pre_negation}) {
1491 $negated = 1;
1492 undef $$negated_ref;
1495 unless (defined $params) {
1496 undef $value;
1497 } elsif (ref $params && ref $params eq 'CODE') {
1498 $value = &$params($current);
1499 } elsif ($params eq 'm') {
1500 $value = bless [ to_array getvalues() ], 'multi';
1501 } elsif ($params =~ /^[a-z]/) {
1502 if (exists $def->{negation} and not $negated) {
1503 my $token = peek_token();
1504 if ($token eq '!') {
1505 require_next_token;
1506 $negated = 1;
1510 my @params;
1511 foreach my $p (split(//, $params)) {
1512 if ($p eq 's') {
1513 push @params, getvar();
1514 } elsif ($p eq 'c') {
1515 my @v = to_array getvalues(undef, undef,
1516 non_empty => 1);
1517 push @params, join(',', @v);
1518 } else {
1519 die;
1523 $value = @params == 1
1524 ? $params[0]
1525 : bless \@params, 'params';
1526 } elsif ($params == 1) {
1527 if (exists $def->{negation} and not $negated) {
1528 my $token = peek_token();
1529 if ($token eq '!') {
1530 require_next_token;
1531 $negated = 1;
1535 $value = getvalues();
1537 warning("log-prefix is too long; truncating to 29 characters: '$1'")
1538 if $keyword eq 'log-prefix' && $value =~ s,^(.{29}).+$,$1,;
1539 } else {
1540 if (exists $def->{negation} and not $negated) {
1541 my $token = peek_token();
1542 if ($token eq '!') {
1543 require_next_token;
1544 $negated = 1;
1548 $value = bless [ map {
1549 getvar()
1550 } (1..$params) ], 'params';
1553 $value = bless [ $value ], 'negated'
1554 if $negated;
1556 return $value;
1559 # parse options of a module
1560 sub parse_option($$$$$$) {
1561 my ($name, $def, $current, $store, $keyword, $negated_ref) = @_;
1563 my $k = $def->{keywords}{$keyword};
1564 return unless defined $k;
1566 while (exists $k->{alias}) {
1567 die if $k->{alias} eq $keyword;
1568 $keyword = $k->{alias};
1569 $k = $def->{keywords}{$keyword};
1570 die unless defined $k;
1573 $store->{$keyword}
1574 = parse_keyword($current, $k,
1575 $keyword, $negated_ref);
1576 $current->{has_rule} = 1;
1577 return 1;
1580 # parse options for a protocol module definition
1581 sub parse_protocol_options($$$$) {
1582 my ($current, $proto, $keyword, $negated_ref) = @_;
1584 my $domain_family = $current->{'domain_family'};
1585 my $proto_defs = $proto_defs{$domain_family};
1586 return unless defined $proto_defs;
1588 my $proto_def = $proto_defs->{$proto};
1589 return unless defined $proto_def and
1590 exists $proto_def->{keywords}{$keyword};
1592 my $module_name = $proto eq 'icmpv6' ? 'icmp6' : $proto;
1593 my $module = { name => $module_name,
1594 options => {},
1595 defs => $proto_def,
1597 push @{$current->{match}}, $module;
1599 return parse_option($proto, $proto_def,
1600 $current, $module->{options},
1601 $keyword, $negated_ref);
1604 # parse options for a match module definition
1605 sub parse_module_options($$$) {
1606 my ($current, $keyword, $negated_ref) = @_;
1608 # modules stored in %match_defs
1609 foreach my $module (@{$current->{match}}) {
1610 my $def = $module->{defs};
1611 next unless defined $def;
1613 parse_option($module->{name}, $def,
1614 $current, $module->{options},
1615 $keyword, $negated_ref)
1616 and do {
1617 # reset hash
1618 keys %{$module->{defs}};
1619 return 1;
1623 return;
1626 # parse options for a target module definition
1627 sub parse_target_options($$$) {
1628 my ($current, $target, $keyword) = @_;
1630 my $target_defs = $current->{action}{defs};
1631 return unless defined $target_defs &&
1632 exists $target_defs->{keywords}{$keyword};
1634 my $k = $target_defs->{keywords}{$keyword};
1636 while (exists $k->{alias}) {
1637 die if $k->{alias} eq $keyword;
1638 $keyword = $k->{alias};
1639 $k = $target_defs->{keywords}{$keyword};
1640 die unless defined $k;
1643 my $negated_dummy;
1644 $current->{target_options}{$keyword}
1645 = parse_keyword($current, $k,
1646 $keyword, \$negated_dummy);
1648 return 1;
1651 sub clone_match($) {
1652 my $match = shift;
1653 return { name => $match->{name},
1654 options => { %{$match->{options}} },
1655 defs => $match->{defs},
1659 sub new_level(\%$) {
1660 my ($current, $prev) = @_;
1662 %$current = ();
1663 if (defined $prev) {
1664 # copy data from previous level
1665 $current->{builtin} = { %{$prev->{builtin}} };
1666 $current->{match} = [ map { clone_match($_) } @{$prev->{match}} ];
1667 $current->{action} = { %{$prev->{action}} };
1668 foreach my $key (qw(domain domain_family table chain proto sport dport)) {
1669 $current->{$key} = $prev->{$key}
1670 if exists $prev->{$key};
1672 } else {
1673 $current->{builtin} = {};
1674 $current->{match} = [];
1675 $current->{action} = {};
1679 sub rule_defined(\%) {
1680 my $rule = shift;
1681 return defined($rule->{domain}) or
1682 keys(%{$rule->{builtin}}) > 0 or
1683 keys(%{$rule->{match}}) > 0 or
1684 keys(%{$rule->{action}}) > 0;
1687 # the main parser loop: read tokens, convert them into internal rule
1688 # structures
1689 sub enter($$) {
1690 my $lev = shift; # current recursion depth
1691 my $prev = shift; # previous rule hash
1693 # enter is the core of the firewall setup, it is a
1694 # simple parser program that recognizes keywords and
1695 # retreives parameters to set up the kernel routing
1696 # chains
1698 my $base_level = $script->{base_level} || 0;
1699 die if $base_level > $lev;
1701 my %current;
1702 new_level(%current, $prev);
1704 # read keywords 1 by 1 and dump into parser
1705 while (defined (my $keyword = next_token())) {
1706 # check if the current rule should be negated
1707 my $negated = $keyword eq '!';
1708 if ($negated) {
1709 # negation. get the next word which contains the 'real'
1710 # rule
1711 $keyword = getvar();
1713 error('unexpected end of file after negation')
1714 unless defined $keyword;
1717 # the core: parse all data
1718 SWITCH: for ($keyword)
1720 # deprecated keyword?
1721 if (exists $deprecated_keywords{$keyword}) {
1722 my $new_keyword = $deprecated_keywords{$keyword};
1723 warning("'$keyword' is deprecated, please use '$new_keyword' instead");
1724 $keyword = $new_keyword;
1727 # effectuation operator
1728 if ($keyword eq ';') {
1729 if ($current{has_rule} and not $current{action}{type}) {
1730 # something is wrong when a rule was specifiedd,
1731 # but no action
1732 error('No action defined; did you mean "NOP"?');
1735 error('No chain defined') unless defined $current{chain};
1737 $current{script} = { filename => $script->{filename},
1738 line => $script->{line},
1741 mkrules(\%current);
1743 # and clean up variables set in this level
1744 new_level(%current, $prev);
1746 next;
1749 # conditional expression
1750 if ($keyword eq '@if') {
1751 unless (eval_bool(getvalues)) {
1752 collect_tokens;
1753 my $token = peek_token();
1754 require_next_token() if $token and $token eq '@else';
1757 next;
1760 if ($keyword eq '@else') {
1761 # hack: if this "else" has not been eaten by the "if"
1762 # handler above, we believe it came from an if clause
1763 # which evaluated "true" - remove the "else" part now.
1764 collect_tokens;
1765 next;
1768 # hooks for custom shell commands
1769 if ($keyword eq 'hook') {
1770 error('"hook" must be the first token in a command')
1771 if rule_defined(%current);
1773 my $position = getvar();
1774 my $hooks;
1775 if ($position eq 'pre') {
1776 $hooks = \@pre_hooks;
1777 } elsif ($position eq 'post') {
1778 $hooks = \@post_hooks;
1779 } else {
1780 error("Invalid hook position: '$position'");
1783 push @$hooks, getvar();
1785 $keyword = next_token();
1786 error('";" expected after hook declaration')
1787 unless defined $keyword and $keyword eq ';';
1789 next;
1792 # recursing operators
1793 if ($keyword eq '{') {
1794 # push stack
1795 my $old_stack_depth = @stack;
1797 unshift @stack, { auto => { %{$stack[0]{auto} || {}} } };
1799 # recurse
1800 enter($lev + 1, \%current);
1802 # pop stack
1803 shift @stack;
1804 die unless @stack == $old_stack_depth;
1806 # after a block, the command is finished, clear this
1807 # level
1808 new_level(%current, $prev);
1810 next;
1813 if ($keyword eq '}') {
1814 error('Unmatched "}"')
1815 if $lev <= $base_level;
1817 # consistency check: check if they havn't forgotten
1818 # the ';' before the last statement
1819 error('Missing semicolon before "}"')
1820 if $current{has_rule};
1822 # and exit
1823 return;
1826 # include another file
1827 if ($keyword eq '@include' or $keyword eq 'include') {
1828 my @files = collect_filenames to_array getvalues;
1829 $keyword = next_token;
1830 error('Missing ";" - "include FILENAME" must be the last command in a rule')
1831 unless defined $keyword and $keyword eq ';';
1833 foreach my $filename (@files) {
1834 # save old script, open new script
1835 my $old_script = $script;
1836 open_script($filename);
1837 $script->{base_level} = $lev + 1;
1839 # push stack
1840 my $old_stack_depth = @stack;
1842 my $stack = {};
1844 if (@stack > 0) {
1845 # include files may set variables for their parent
1846 $stack->{vars} = ($stack[0]{vars} ||= {});
1847 $stack->{functions} = ($stack[0]{functions} ||= {});
1848 $stack->{auto} = { %{ $stack[0]{auto} || {} } };
1851 unshift @stack, $stack;
1853 # parse the script
1854 enter($lev + 1, \%current);
1856 # pop stack
1857 shift @stack;
1858 die unless @stack == $old_stack_depth;
1860 # restore old script
1861 $script = $old_script;
1864 next;
1867 # definition of a variable or function
1868 if ($keyword eq '@def' or $keyword eq 'def') {
1869 error('"def" must be the first token in a command')
1870 if $current{has_rule};
1872 my $type = require_next_token();
1873 if ($type eq '$') {
1874 my $name = require_next_token();
1875 error('invalid variable name')
1876 unless $name =~ /^\w+$/;
1878 $keyword = require_next_token();
1879 error('"=" expected after variable name')
1880 unless $keyword eq '=';
1882 my $value = getvalues(undef, undef, allow_negation => 1);
1884 $keyword = next_token();
1885 error('";" expected after variable declaration')
1886 unless defined $keyword and $keyword eq ';';
1888 $stack[0]{vars}{$name} = $value
1889 unless exists $stack[-1]{vars}{$name};
1890 } elsif ($type eq '&') {
1891 my $name = require_next_token();
1892 error('invalid function name')
1893 unless $name =~ /^\w+$/;
1895 my @params;
1896 my $token = next_token();
1897 error('function parameter list or "()" expected')
1898 unless defined $token and $token eq '(';
1899 while (1) {
1900 $token = require_next_token();
1901 last if $token eq ')';
1903 if (@params > 0) {
1904 error('"," expected')
1905 unless $token eq ',';
1907 $token = require_next_token();
1910 error('"$" and parameter name expected')
1911 unless $token eq '$';
1913 $token = require_next_token();
1914 error('invalid function parameter name')
1915 unless $token =~ /^\w+$/;
1917 push @params, $token;
1920 my %function;
1922 $function{params} = \@params;
1924 $keyword = require_next_token;
1925 error('"=" expected')
1926 unless $keyword eq '=';
1928 my $tokens = collect_tokens();
1929 $function{block} = 1 if grep { $_ eq '{' } @$tokens;
1930 $function{tokens} = $tokens;
1932 $stack[0]{functions}{$name} = \%function
1933 unless exists $stack[-1]{functions}{$name};
1934 } else {
1935 error('"$" (variable) or "&" (function) expected');
1938 next;
1941 # def references
1942 if ($keyword eq '$') {
1943 error('variable references are only allowed as keyword parameter');
1946 if ($keyword eq '&') {
1947 my $name = require_next_token;
1948 error('function name expected')
1949 unless $name =~ /^\w+$/;
1951 my $function;
1952 foreach (@stack) {
1953 $function = $_->{functions}{$name};
1954 last if defined $function;
1956 error("no such function: \&$name")
1957 unless defined $function;
1959 my $paramdef = $function->{params};
1960 die unless defined $paramdef;
1962 my @params = get_function_params(allow_negation => 1);
1964 error("Wrong number of parameters for function '\&$name': "
1965 . @$paramdef . " expected, " . @params . " given")
1966 unless @params == @$paramdef;
1968 my %vars;
1969 for (my $i = 0; $i < @params; $i++) {
1970 $vars{$paramdef->[$i]} = $params[$i];
1973 if ($function->{block}) {
1974 # block {} always ends the current rule, so if the
1975 # function contains a block, we have to require
1976 # the calling rule also ends here
1977 my $token = next_token();
1978 error("';' expected after block function call '\&$name'")
1979 unless defined $token and $token eq ';';
1982 my @tokens = @{$function->{tokens}};
1983 for (my $i = 0; $i < @tokens; $i++) {
1984 if ($tokens[$i] eq '$' and $i + 1 < @tokens and
1985 exists $vars{$tokens[$i + 1]}) {
1986 my @value = to_array($vars{$tokens[$i + 1]});
1987 @value = ('(', @value, ')')
1988 unless @tokens == 1;
1989 splice(@tokens, $i, 2, @value);
1990 $i += @value - 2;
1991 } elsif ($tokens[$i] =~ m,^"(.*)"$,) {
1992 $tokens[$i] =~ s,\$(\w+),exists $vars{$1} ? $vars{$1} : "\$$1",eg;
1996 unshift @{$script->{tokens}}, @tokens;
1998 next;
2001 # where to put the rule?
2002 if ($keyword eq 'domain') {
2003 error('Domain is already specified')
2004 if exists $current{domain};
2006 my $domain = getvalues();
2007 my $filtered_domain = filter_domains($domain);
2008 my $domain_family;
2009 unless (ref $domain) {
2010 $domain_family = $domain eq 'ip6' ? 'ip' : $domain;
2011 } elsif (@$domain == 0) {
2012 $domain_family = 'none';
2013 } elsif (grep { not /^ip6?$/s } @$domain) {
2014 error('Cannot combine non-IP domains');
2015 } else {
2016 $domain_family = 'ip';
2018 $current{domain_family} = $domain_family;
2020 $current{domain} = $stack[0]{auto}{DOMAIN} = $filtered_domain;
2022 next;
2025 if ($keyword eq 'table') {
2026 error('Table is already specified')
2027 if exists $current{table};
2028 $current{table} = $stack[0]{auto}{TABLE} = getvalues();
2030 unless (exists $current{domain}) {
2031 $current{domain} = filter_domains('ip');
2032 $current{domain_family} = 'ip';
2035 next;
2038 if ($keyword eq 'chain') {
2039 error('Chain is already specified')
2040 if exists $current{chain};
2041 $current{chain} = $stack[0]{auto}{CHAIN} = getvalues();
2043 # ferm 1.1 allowed lower case built-in chain names
2044 foreach (ref $current{chain} ? @{$current{chain}} : $current{chain}) {
2045 error('Please write built-in chain names in upper case')
2046 if /^(?:input|forward|output|prerouting|postrouting)$/;
2049 unless (exists $current{domain}) {
2050 $current{domain} = filter_domains('ip');
2051 $current{domain_family} = 'ip';
2054 $current{table} = 'filter'
2055 unless exists $current{table};
2057 next;
2060 error('Chain must be specified')
2061 unless exists $current{chain};
2063 # policy for built-in chain
2064 if ($keyword eq 'policy') {
2065 error('Cannot specify matches for policy')
2066 if $current{has_rule};
2068 my $policy = uc getvar();
2069 error("Invalid policy target: $policy")
2070 unless $policy =~ /^(?:ACCEPT|DROP)$/;
2072 $keyword = peek_token();
2073 error('";" expected after policy declaration')
2074 unless defined $keyword and $keyword eq ';';
2076 $current{action} = { type => 'policy',
2077 policy => $policy,
2079 next;
2082 # create a subchain
2083 if ($keyword eq '@subchain' or $keyword eq 'subchain') {
2084 error('No rule specified before "@subchain"')
2085 unless $current{has_rule};
2087 my $subchain;
2088 $keyword = next_token();
2090 if ($keyword =~ /^(["'])(.*)\1$/s) {
2091 $subchain = $2;
2092 $keyword = next_token();
2093 } else {
2094 $subchain = 'ferm_auto_' . ++$auto_chain;
2097 error('"{" or chain name expected after "sub"')
2098 unless $keyword eq '{';
2100 # create a deep copy of %current, only containing values
2101 # which must be in the subchain
2102 my %inner = ( builtin => {},
2103 action => {},
2105 $inner{domain} = $current{domain};
2106 $inner{domain_family} = $current{domain_family};
2107 $inner{table} = $current{table};
2108 $inner{chain} = $inner{auto}{CHAIN} = $subchain;
2109 $inner{builtin}{protocol} = $current{builtin}{protocol}
2110 if exists $current{builtin}{protocol};
2112 # enter the block
2113 enter(1, \%inner);
2115 # now handle the parent - it's a jump to the sub chain
2116 $current{action} = { type => 'jump',
2117 chain => $subchain,
2120 $current{script} = { filename => $script->{filename},
2121 line => $script->{line},
2124 mkrules(\%current);
2126 # and clean up variables set in this level
2127 new_level(%current, $prev);
2129 next;
2132 # everything else must be part of a "real" rule, not just
2133 # "policy only"
2134 $current{has_rule}++;
2136 # extended parameters:
2137 if ($keyword =~ /^mod(?:ule)?$/) {
2138 foreach my $module (to_array getvalues) {
2139 next if grep { $_->{name} eq $module } @{$current{match}};
2141 my $domain_family = $current{domain_family};
2142 my $defs = $match_defs{$domain_family}{$module};
2143 if (not defined $defs and exists $current{builtin}{protocol}) {
2144 my $proto = $current{builtin}{protocol};
2145 unless (ref $proto) {
2146 $proto = netfilter_canonical_protocol($current{builtin}{protocol});
2147 $defs = $proto_defs{$domain_family}{$proto};
2151 push @{$current{match}}, { name => $module,
2152 options => {},
2153 defs => $defs,
2157 next;
2160 parse_builtin_matches(\%current, $keyword, \$negated)
2161 and next;
2164 # actions
2167 # jump action
2168 if ($keyword eq 'jump') {
2169 error('There can only one action per rule')
2170 if defined $current{action}{type};
2171 my $chain = getvar();
2172 if (is_netfilter_core_target($chain) or
2173 is_netfilter_module_target($current{domain_family}, $chain)) {
2174 my $defs = $target_defs{$current{domain_family}} &&
2175 $target_defs{$current{domain_family}}{$chain};
2176 $current{action} = { type => 'target',
2177 target => $chain,
2178 defs => $defs,
2180 } else {
2181 $current{action} = { type => 'jump',
2182 chain => $chain,
2185 next;
2188 # goto action
2189 if ($keyword eq 'realgoto') {
2190 error('There can only one action per rule')
2191 if defined $current{action}{type};
2192 $current{action} = { type => 'goto',
2193 chain => getvar(),
2195 next;
2198 # action keywords
2199 if (is_netfilter_core_target($keyword)) {
2200 error('There can only one action per rule')
2201 if defined $current{action}{type};
2202 $current{action} = { type => 'target',
2203 target => $keyword,
2205 next;
2208 if ($keyword eq 'NOP') {
2209 error('There can only one action per rule')
2210 if defined $current{action}{type};
2211 $current{action} = { type => 'nop',
2213 next;
2216 if (is_netfilter_module_target($current{domain_family}, $keyword)) {
2217 error('There can only one action per rule')
2218 if defined $current{action}{type};
2220 if ($keyword eq 'TCPMSS') {
2221 my $protos = $current{builtin}{protocol};
2222 error('No protocol specified before TCPMSS')
2223 unless defined $protos;
2224 foreach my $proto (to_array $protos) {
2225 error('TCPMSS not available for protocol "$proto"')
2226 unless $proto eq 'tcp';
2230 $current{action} = { type => 'target',
2231 target => $keyword,
2232 defs => $target_defs{$current{domain_family}}{$keyword},
2234 next;
2237 my $proto = $current{builtin}{protocol};
2240 # module specific options
2243 parse_module_options(\%current, $keyword, \$negated)
2244 and next;
2247 # protocol specific options
2250 if (defined $proto and not ref $proto) {
2251 $proto = netfilter_canonical_protocol($proto);
2253 if ($proto eq 'icmp') {
2254 my $domains = $current{domain};
2255 $proto = 'icmpv6' if not ref $domains and $domains eq 'ip6';
2258 parse_protocol_options(\%current, $proto, $keyword, \$negated)
2259 and next;
2262 # port switches
2263 if ($keyword =~ /^[sd]port$/) {
2264 error('To use sport or dport, you have to specify "proto tcp" or "proto udp" first')
2265 unless defined $proto and grep { /^(?:tcp|udp|udplite|dccp|sctp)$/ } to_array $proto;
2267 $current{$keyword} = getvalues(undef, undef,
2268 allow_negation => 1);
2269 next;
2273 # target specific options
2276 if (exists $current{action}{type} and
2277 $current{action}{type} eq 'target') {
2278 parse_target_options(\%current, $current{action}{target},
2279 $keyword);
2280 next;
2283 # default
2284 error("Unrecognized keyword: $keyword");
2287 # if the rule didn't reset the negated flag, it's not
2288 # supported
2289 error("Doesn't support negation: $keyword")
2290 if $negated;
2293 error('Missing "}" at end of file')
2294 if $lev > $base_level;
2296 # consistency check: check if they havn't forgotten
2297 # the ';' before the last statement
2298 error("Missing semicolon before end of file")
2299 if $current{has_rule}; # XXX
2302 sub check() {
2303 while (my ($domain_name, $domain) = each %domains) {
2304 while (my ($table_name, $table_info) = each %{$domain->{tables}}) {
2305 while (my ($chain_name, $chain) = each %{$table_info->{chains}}) {
2306 warning("chain $chain_name (domain $domain_name, table $table_name) was referenced, but not declared")
2307 if $chain->{was_created} and not $chain->{non_empty};
2313 sub execute_command {
2314 my ($command, $script) = @_;
2316 print LINES "$command\n"
2317 if $option{lines};
2318 return if $option{noexec};
2320 my $ret = system($_);
2321 unless ($ret == 0) {
2322 if ($? == -1) {
2323 print STDERR "failed to execute: $!\n";
2324 exit 1;
2325 } elsif ($? & 0x7f) {
2326 printf STDERR "child died with signal %d\n", $? & 0x7f;
2327 return 1;
2328 } else {
2329 print STDERR "(rule declared in $script->{filename}:$script->{line})\n"
2330 if defined $script;
2331 return $? >> 8;
2335 return;
2338 sub execute_slow($$) {
2339 my ($domain, $domain_info) = @_;
2341 my $domain_cmd = $domain_info->{tools}{tables};
2343 while (my ($table, $table_info) = each %{$domain_info->{tables}}) {
2344 my $table_cmd = "$domain_cmd -t $table";
2346 # reset chain policies
2347 while (my ($chain, $chain_info) = each %{$table_info->{chains}}) {
2348 next unless $chain_info->{builtin} or
2349 (not $table_info->{has_builtin} and
2350 is_netfilter_builtin_chain($table, $chain));
2351 $status ||= execute_command("$table_cmd -P $chain ACCEPT");
2354 # clear
2355 $status ||= execute_command("$table_cmd -F");
2356 $status ||= execute_command("$table_cmd -X");
2358 # create chains / set policy
2359 while (my ($chain, $chain_info) = each %{$table_info->{chains}}) {
2360 if ($chain_info->{create}) {
2361 $status ||= execute_command("$table_cmd -N $chain");
2362 } elsif (exists $chain_info->{policy}) {
2363 $status ||= execute_command("$table_cmd -P $chain $chain_info->{policy}");
2367 # dump rules
2368 while (my ($chain, $chain_info) = each %{$table_info->{chains}}) {
2369 my $chain_cmd = "$table_cmd -A $chain";
2370 foreach my $rule (@{$chain_info->{rules}}) {
2371 $status ||= execute_command($chain_cmd . $rule->{rule});
2376 return $status;
2379 sub rules_to_save($$) {
2380 my ($domain, $domain_info) = @_;
2382 # convert this into an iptables-save text
2383 my $result = "# Generated by ferm $VERSION on " . localtime() . "\n";
2385 while (my ($table, $table_info) = each %{$domain_info->{tables}}) {
2386 # select table
2387 $result .= '*' . $table . "\n";
2389 # create chains / set policy
2390 foreach my $chain (sort keys %{$table_info->{chains}}) {
2391 my $chain_info = $table_info->{chains}{$chain};
2392 my $policy = $chain_info->{create}
2393 ? '-' : ($chain_info->{policy} || 'ACCEPT');
2394 $result .= ":$chain $policy\ [0:0]\n";
2397 # dump rules
2398 foreach my $chain (sort keys %{$table_info->{chains}}) {
2399 my $chain_info = $table_info->{chains}{$chain};
2400 foreach my $rule (@{$chain_info->{rules}}) {
2401 $result .= "-A $chain$rule->{rule}\n";
2405 # do it
2406 $result .= "COMMIT\n";
2409 return $result;
2412 sub restore_domain($$) {
2413 my ($domain, $save) = @_;
2415 my $path = $domains{$domain}{tools}{'tables-restore'};
2417 local *RESTORE;
2418 open RESTORE, "|$path"
2419 or die "Failed to run $path: $!\n";
2421 print RESTORE $save;
2423 close RESTORE
2424 or die "Failed to run $path\n";
2427 sub execute_fast($$) {
2428 my ($domain, $domain_info) = @_;
2430 my $save = rules_to_save($domain, $domain_info);
2432 if ($option{lines}) {
2433 print LINES "$domain_info->{tools}{'tables-restore'} <<EOT\n"
2434 if $option{shell};
2435 print LINES $save;
2436 print LINES "EOT\n"
2437 if $option{shell};
2440 return if $option{noexec};
2442 eval {
2443 restore_domain($domain, $save);
2445 if ($@) {
2446 print STDERR $@;
2447 return 1;
2450 return;
2453 sub rollback() {
2454 my $error;
2455 while (my ($domain, $domain_info) = each %domains) {
2456 next unless $domain_info->{enabled};
2457 unless (defined $domain_info->{tools}{'tables-restore'}) {
2458 print STDERR "Cannot rollback domain '$domain' because there is no ${domain}tables-restore\n";
2459 next;
2462 my $reset = '';
2463 while (my ($table, $table_info) = each %{$domain_info->{tables}}) {
2464 my $reset_chain = '';
2465 foreach my $chain (keys %{$table_info->{chains}{$table}}) {
2466 next unless is_netfilter_builtin_chain($table, $chain);
2467 $reset_chain .= ":${chain} ACCEPT [0:0]\n";
2469 $reset .= "*${table}\n${reset_chain}COMMIT\n"
2470 if length $reset_chain;
2473 $reset .= $domain_info->{previous}
2474 if defined $domain_info->{previous};
2476 restore_domain($domain, $reset);
2479 print STDERR "\nFirewall rules rolled back.\n" unless $error;
2480 exit 1;
2483 sub alrm_handler {
2484 # do nothing, just interrupt a system call
2487 sub confirm_rules() {
2488 $SIG{ALRM} = \&alrm_handler;
2490 alarm(5);
2492 print STDERR "\n"
2493 . "ferm has applied the new firewall rules.\n"
2494 . "Please type 'yes' to confirm:\n";
2495 STDERR->flush();
2497 alarm(30);
2499 my $line = '';
2500 STDIN->sysread($line, 3);
2502 eval {
2503 require POSIX;
2504 POSIX::tcflush(*STDIN, 2);
2506 print STDERR "$@" if $@;
2508 $SIG{ALRM} = 'DEFAULT';
2510 return $line eq 'yes';
2513 # end of ferm
2515 __END__
2517 =head1 NAME
2519 ferm - a firewall rule parser for linux
2521 =head1 SYNOPSIS
2523 B<ferm> I<options> I<inputfiles>
2525 =head1 OPTIONS
2527 -n, --noexec Do not execute the rules, just simulate
2528 -F, --flush Flush all netfilter tables managed by ferm
2529 -l, --lines Show all rules that were created
2530 -i, --interactive Interactive mode: revert if user does not confirm
2531 --remote Remote mode; ignore host specific configuration.
2532 This implies --noexec and --lines.
2533 -V, --version Show current version number
2534 -h, --help Look at this text
2535 --fast Generate an iptables-save file, used by iptables-restore
2536 --shell Generate a shell script which calls iptables-restore
2537 --domain {ip|ip6} Handle only the specified domain
2538 --def '$name=v' Override a variable
2540 =cut