2007-06-09 01:31:48 +02:00
|
|
|
#! /usr/bin/perl -w
|
|
|
|
#
|
2007-06-11 21:39:30 +02:00
|
|
|
# The Shoreline Firewall4 (Shorewall-perl) Packet Filtering Firewall Compiler - V4.0
|
2007-06-09 01:31:48 +02:00
|
|
|
#
|
2007-09-08 18:09:51 +02:00
|
|
|
# This program is under GPL [http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt]
|
2007-06-09 01:31:48 +02:00
|
|
|
#
|
|
|
|
# (c) 2007 - Tom Eastep (teastep@shorewall.net)
|
|
|
|
#
|
|
|
|
# Complete documentation is available at http://shorewall.net
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of Version 2 of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2007-09-08 18:09:51 +02:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-06-09 01:31:48 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
package Shorewall::Compiler;
|
|
|
|
require Exporter;
|
|
|
|
use Shorewall::Config;
|
|
|
|
use Shorewall::Chains;
|
|
|
|
use Shorewall::Zones;
|
|
|
|
use Shorewall::Policy;
|
|
|
|
use Shorewall::Nat;
|
|
|
|
use Shorewall::Providers;
|
|
|
|
use Shorewall::Tc;
|
|
|
|
use Shorewall::Tunnels;
|
|
|
|
use Shorewall::Actions;
|
|
|
|
use Shorewall::Accounting;
|
|
|
|
use Shorewall::Rules;
|
|
|
|
use Shorewall::Proc;
|
|
|
|
use Shorewall::Proxyarp;
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
2007-06-23 23:12:48 +02:00
|
|
|
our @EXPORT = qw( compiler EXPORT TIMESTAMP DEBUG );
|
2007-06-11 20:07:34 +02:00
|
|
|
our @EXPORT_OK = qw( $export );
|
2007-10-19 21:43:14 +02:00
|
|
|
our $VERSION = 4.0.4;
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
our $export;
|
|
|
|
|
|
|
|
our $reused = 0;
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-06-12 21:25:28 +02:00
|
|
|
use constant { EXPORT => 0x01 ,
|
2007-06-23 23:12:48 +02:00
|
|
|
TIMESTAMP => 0x02 ,
|
|
|
|
DEBUG => 0x04 };
|
2007-06-14 01:02:39 +02:00
|
|
|
|
2007-06-14 16:58:48 +02:00
|
|
|
#
|
|
|
|
# Reinitilize the package-globals in the other modules
|
|
|
|
#
|
|
|
|
sub reinitialize() {
|
2007-06-14 01:02:39 +02:00
|
|
|
Shorewall::Config::initialize;
|
|
|
|
Shorewall::Chains::initialize;
|
2007-06-14 16:58:48 +02:00
|
|
|
Shorewall::Zones::initialize;
|
2007-09-10 17:52:57 +02:00
|
|
|
Shorewall::Policy::initialize;
|
2007-06-14 01:02:39 +02:00
|
|
|
Shorewall::Nat::initialize;
|
|
|
|
Shorewall::Providers::initialize;
|
|
|
|
Shorewall::Tc::initialize;
|
2007-06-14 16:58:48 +02:00
|
|
|
Shorewall::Actions::initialize;
|
|
|
|
Shorewall::Accounting::initialize;
|
|
|
|
Shorewall::Rules::initialize;
|
|
|
|
Shorewall::Proxyarp::initialize;
|
2007-06-14 01:02:39 +02:00
|
|
|
}
|
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
#
|
|
|
|
# First stage of script generation.
|
|
|
|
#
|
|
|
|
# Copy the prog.header to the generated script.
|
|
|
|
# Generate the various user-exit jacket functions.
|
|
|
|
# Generate the 'initialize()' function.
|
|
|
|
#
|
2007-07-11 01:09:33 +02:00
|
|
|
# Note: This function is not called when $command eq 'check'. So it must have no side effects other
|
|
|
|
# than those related to writing to the object file.
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
sub generate_script_1() {
|
|
|
|
|
|
|
|
my $date = localtime;
|
|
|
|
|
2007-07-03 20:22:36 +02:00
|
|
|
emit "#!/bin/sh\n#\n# Compiled firewall script generated by Shorewall-perl $globals{VERSION} - $date\n#";
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-07-03 20:22:36 +02:00
|
|
|
copy $globals{SHAREDIRPL} . 'prog.header';
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-08-26 17:12:04 +02:00
|
|
|
for my $exit qw/init isusable start tcclear started stop stopped clear refresh refreshed/ {
|
2007-08-03 20:47:58 +02:00
|
|
|
emit "\nrun_${exit}_exit() {";
|
2007-06-09 01:31:48 +02:00
|
|
|
push_indent;
|
|
|
|
append_file $exit or emit 'true';
|
|
|
|
pop_indent;
|
2007-08-03 20:47:58 +02:00
|
|
|
emit '}';
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2007-08-03 20:47:58 +02:00
|
|
|
emit ( '',
|
|
|
|
'#',
|
2007-07-23 20:14:12 +02:00
|
|
|
'# This function initializes the global variables used by the program',
|
|
|
|
'#',
|
|
|
|
'initialize()',
|
|
|
|
'{',
|
|
|
|
' #',
|
|
|
|
' # These variables are required by the library functions called in this script',
|
|
|
|
' #'
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
push_indent;
|
|
|
|
|
2007-07-03 20:22:36 +02:00
|
|
|
if ( $export ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( 'SHAREDIR=/usr/share/shorewall-lite',
|
|
|
|
'CONFDIR=/etc/shorewall-lite',
|
|
|
|
'PRODUCT="Shorewall Lite"'
|
|
|
|
);
|
2007-07-03 20:22:36 +02:00
|
|
|
} else {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( 'SHAREDIR=/usr/share/shorewall',
|
|
|
|
'CONFDIR=/etc/shorewall',
|
|
|
|
'PRODUCT=\'Shorewall\'',
|
|
|
|
);
|
2007-07-03 20:22:36 +02:00
|
|
|
}
|
|
|
|
|
2007-07-08 19:10:42 +02:00
|
|
|
emit( '[ -f ${CONFDIR}/vardir ] && . ${CONFDIR}/vardir' );
|
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
if ( $export ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( 'CONFIG_PATH="/etc/shorewall-lite:/usr/share/shorewall-lite"' ,
|
|
|
|
'[ -n "${VARDIR:=/var/lib/shorewall-lite}" ]' );
|
2007-06-09 01:31:48 +02:00
|
|
|
} else {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( qq(CONFIG_PATH="$config{CONFIG_PATH}") ,
|
|
|
|
'[ -n "${VARDIR:=/var/lib/shorewall}" ]' );
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2007-07-03 20:22:36 +02:00
|
|
|
emit 'TEMPFILE=';
|
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
propagateconfig;
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( '[ -n "${COMMAND:=restart}" ]',
|
|
|
|
'[ -n "${VERBOSE:=0}" ]',
|
|
|
|
qq([ -n "\${RESTOREFILE:=$config{RESTOREFILE}}" ]),
|
|
|
|
'[ -n "$LOGFORMAT" ] || LOGFORMAT="Shorewall:%s:%s:"',
|
|
|
|
qq(VERSION="$globals{VERSION}") ,
|
|
|
|
qq(PATH="$config{PATH}") ,
|
2007-09-10 17:52:57 +02:00
|
|
|
'TERMINATOR=fatal_error' ,
|
|
|
|
''
|
2007-07-23 20:14:12 +02:00
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
if ( $config{IPTABLES} ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( qq(IPTABLES="$config{IPTABLES}"),
|
|
|
|
'[ -x "$IPTABLES" ] || startup_error "IPTABLES=$IPTABLES does not exist or is not executable"',
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
} else {
|
2007-09-10 17:52:57 +02:00
|
|
|
emit( '[ -z "$IPTABLES" ] && IPTABLES=$(mywhich iptables) # /sbin/shorewall exports IPTABLES',
|
2007-07-23 20:14:12 +02:00
|
|
|
'[ -n "$IPTABLES" -a -x "$IPTABLES" ] || startup_error "Can\'t find iptables executable"'
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( 'IPTABLES_RESTORE=${IPTABLES}-restore',
|
|
|
|
'[ -x "$IPTABLES_RESTORE" ] || startup_error "$IPTABLES_RESTORE does not exist or is not executable"' );
|
2007-06-25 19:03:18 +02:00
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
append_file 'params' if $config{EXPORTPARAMS};
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( '',
|
|
|
|
"STOPPING=",
|
|
|
|
'',
|
|
|
|
'#',
|
|
|
|
'# The library requires that ${VARDIR} exist',
|
|
|
|
'#',
|
|
|
|
'[ -d ${VARDIR} ] || mkdir -p ${VARDIR}'
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-07-29 23:30:22 +02:00
|
|
|
emit ( '',
|
|
|
|
'#',
|
|
|
|
'# Recent kernels are difficult to configure -- we see state match omitted a lot so we check for it here',
|
|
|
|
'#',
|
2007-07-31 23:41:00 +02:00
|
|
|
'qt $IPTABLES -N foox1234',
|
|
|
|
'qt $IPTABLES -A foox1234 -m state --state ESTABLISHED,RELATED -j ACCEPT',
|
2007-07-29 23:30:22 +02:00
|
|
|
'result=$?',
|
2007-07-31 23:41:00 +02:00
|
|
|
'qt $IPTABLES -F foox1234',
|
|
|
|
'qt $IPTABLES -X foox1234',
|
2007-07-29 23:30:22 +02:00
|
|
|
'[ $result = 0 ] || startup_error "Your kernel/iptables do not include state match support. No version of Shorewall will run on this system"',
|
|
|
|
'' );
|
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
pop_indent;
|
|
|
|
|
|
|
|
emit "}\n"; # End of initialize()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub compile_stop_firewall() {
|
|
|
|
|
|
|
|
emit <<'EOF';
|
|
|
|
#
|
|
|
|
# Stop/restore the firewall after an error or because of a 'stop' or 'clear' command
|
|
|
|
#
|
|
|
|
stop_firewall() {
|
|
|
|
|
|
|
|
deletechain() {
|
|
|
|
qt $IPTABLES -L $1 -n && qt $IPTABLES -F $1 && qt $IPTABLES -X $1
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteallchains() {
|
|
|
|
$IPTABLES -F
|
|
|
|
$IPTABLES -X
|
|
|
|
}
|
|
|
|
|
|
|
|
setcontinue() {
|
|
|
|
$IPTABLES -A $1 -m state --state ESTABLISHED,RELATED -j ACCEPT
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_nat() {
|
|
|
|
$IPTABLES -t nat -F
|
|
|
|
$IPTABLES -t nat -X
|
|
|
|
|
|
|
|
if [ -f ${VARDIR}/nat ]; then
|
|
|
|
while read external interface; do
|
|
|
|
del_ip_addr $external $interface
|
|
|
|
done < ${VARDIR}/nat
|
|
|
|
|
|
|
|
rm -f ${VARDIR}/nat
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
case $COMMAND in
|
|
|
|
stop|clear|restore)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
set +x
|
|
|
|
|
|
|
|
case $COMMAND in
|
|
|
|
start)
|
|
|
|
logger -p kern.err "ERROR:$PRODUCT start failed"
|
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
logger -p kern.err "ERROR:$PRODUCT restart failed"
|
|
|
|
;;
|
|
|
|
restore)
|
|
|
|
logger -p kern.err "ERROR:$PRODUCT restore failed"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ "$RESTOREFILE" = NONE ]; then
|
|
|
|
COMMAND=clear
|
|
|
|
clear_firewall
|
|
|
|
echo "$PRODUCT Cleared"
|
|
|
|
|
|
|
|
kill $$
|
|
|
|
exit 2
|
|
|
|
else
|
|
|
|
RESTOREPATH=${VARDIR}/$RESTOREFILE
|
|
|
|
|
|
|
|
if [ -x $RESTOREPATH ]; then
|
|
|
|
|
|
|
|
if [ -x ${RESTOREPATH}-ipsets ]; then
|
|
|
|
progress_message2 Restoring Ipsets...
|
|
|
|
#
|
|
|
|
# We must purge iptables to be sure that there are no
|
|
|
|
# references to ipsets
|
|
|
|
#
|
|
|
|
for table in mangle nat filter; do
|
|
|
|
$IPTABLES -t $table -F
|
|
|
|
$IPTABLES -t $table -X
|
|
|
|
done
|
|
|
|
|
|
|
|
${RESTOREPATH}-ipsets
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo Restoring ${PRODUCT:=Shorewall}...
|
|
|
|
|
|
|
|
if $RESTOREPATH restore; then
|
|
|
|
echo "$PRODUCT restored from $RESTOREPATH"
|
|
|
|
set_state "Started"
|
|
|
|
else
|
|
|
|
set_state "Unknown"
|
|
|
|
fi
|
|
|
|
|
|
|
|
kill $$
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
set_state "Stopping"
|
|
|
|
|
|
|
|
STOPPING="Yes"
|
|
|
|
|
|
|
|
TERMINATOR=
|
|
|
|
|
|
|
|
deletechain shorewall
|
|
|
|
|
2007-09-26 02:01:39 +02:00
|
|
|
run_stop_exit
|
2007-07-03 01:52:49 +02:00
|
|
|
EOF
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-07-03 01:52:49 +02:00
|
|
|
if ( $capabilities{MANGLE_ENABLED} ) {
|
|
|
|
emit <<'EOF';
|
|
|
|
run_iptables -t mangle -F
|
|
|
|
run_iptables -t mangle -X
|
|
|
|
for chain in PREROUTING INPUT FORWARD POSTROUTING; do
|
|
|
|
qt $IPTABLES -t mangle -P $chain ACCEPT
|
|
|
|
done
|
|
|
|
EOF
|
|
|
|
}
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-07-03 01:52:49 +02:00
|
|
|
if ( $capabilities{RAW_TABLE} ) {
|
|
|
|
emit <<'EOF';
|
|
|
|
run_iptables -t raw -F
|
|
|
|
run_iptables -t raw -X
|
|
|
|
for chain in PREROUTING OUTPUT; do
|
|
|
|
qt $IPTABLES -t raw -P $chain ACCEPT
|
|
|
|
done
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $capabilities{NAT_ENABLED} ) {
|
|
|
|
emit <<'EOF';
|
2007-07-31 23:52:59 +02:00
|
|
|
delete_nat
|
|
|
|
for chain in PREROUTING POSTROUTING OUTPUT; do
|
|
|
|
qt $IPTABLES -t nat -P $chain ACCEPT
|
|
|
|
done
|
2007-07-03 01:52:49 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
emit <<'EOF';
|
2007-06-09 01:31:48 +02:00
|
|
|
if [ -f ${VARDIR}/proxyarp ]; then
|
|
|
|
while read address interface external haveroute; do
|
|
|
|
qt arp -i $external -d $address pub
|
|
|
|
[ -z "${haveroute}${NOROUTES}" ] && qt ip route del $address dev $interface
|
2007-09-21 18:55:28 +02:00
|
|
|
f=/proc/sys/net/ipv4/conf/$interface/proxy_arp
|
|
|
|
[ -f $f ] && echo 0 > $f
|
2007-06-09 01:31:48 +02:00
|
|
|
done < ${VARDIR}/proxyarp
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -f ${VARDIR}/proxyarp
|
|
|
|
EOF
|
|
|
|
|
|
|
|
push_indent;
|
|
|
|
|
|
|
|
emit 'delete_tc1' if $config{CLEAR_TC};
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( 'undo_routing',
|
|
|
|
'restore_default_route'
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
my $criticalhosts = process_criticalhosts;
|
|
|
|
|
|
|
|
if ( @$criticalhosts ) {
|
|
|
|
if ( $config{ADMINISABSENTMINDED} ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( 'for chain in INPUT OUTPUT; do',
|
2007-06-09 01:31:48 +02:00
|
|
|
' setpolicy $chain ACCEPT',
|
|
|
|
'done',
|
|
|
|
'',
|
|
|
|
'setpolicy FORWARD DROP',
|
|
|
|
'',
|
|
|
|
'deleteallchains',
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
for my $hosts ( @$criticalhosts ) {
|
|
|
|
my ( $interface, $host ) = ( split /:/, $hosts );
|
|
|
|
my $source = match_source_net $host;
|
|
|
|
my $dest = match_dest_net $host;
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( "\$IPTABLES -A INPUT -i $interface $source -j ACCEPT",
|
|
|
|
"\$IPTABLES -A OUTPUT -o $interface $dest -j ACCEPT"
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( '',
|
|
|
|
'for chain in INPUT OUTPUT; do',
|
|
|
|
' setpolicy $chain DROP',
|
|
|
|
"done\n"
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
} else {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( '',
|
|
|
|
'for chain in INPUT OUTPUT; do',
|
|
|
|
' setpolicy \$chain ACCEPT',
|
|
|
|
'done',
|
|
|
|
'',
|
|
|
|
'setpolicy FORWARD DROP',
|
|
|
|
'',
|
|
|
|
"deleteallchains\n"
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
for my $hosts ( @$criticalhosts ) {
|
|
|
|
my ( $interface, $host ) = ( split /:/, $hosts );
|
|
|
|
my $source = match_source_net $host;
|
|
|
|
my $dest = match_dest_net $host;
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( "\$IPTABLES -A INPUT -i $interface $source -j ACCEPT",
|
2007-06-09 01:31:48 +02:00
|
|
|
"\$IPTABLES -A OUTPUT -o $interface $dest -j ACCEPT"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( "\nsetpolicy INPUT DROP",
|
|
|
|
'',
|
|
|
|
'for chain in INPUT FORWARD; do',
|
|
|
|
' setcontinue $chain',
|
|
|
|
"done\n"
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
} elsif ( $config{ADMINISABSENTMINDED} ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( 'for chain in INPUT FORWARD; do',
|
|
|
|
' setpolicy $chain DROP',
|
|
|
|
'done',
|
|
|
|
'',
|
|
|
|
'setpolicy OUTPUT ACCEPT',
|
|
|
|
'',
|
|
|
|
'deleteallchains',
|
|
|
|
'',
|
|
|
|
'for chain in INPUT FORWARD; do',
|
|
|
|
' setcontinue $chain',
|
|
|
|
"done\n",
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
} else {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( 'for chain in INPUT OUTPUT FORWARD; do',
|
|
|
|
' setpolicy $chain DROP',
|
|
|
|
'done',
|
|
|
|
'',
|
|
|
|
"deleteallchains\n"
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
process_routestopped;
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( '$IPTABLES -A INPUT -i lo -j ACCEPT',
|
|
|
|
'$IPTABLES -A OUTPUT -o lo -j ACCEPT'
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
emit '$IPTABLES -A OUTPUT -o lo -j ACCEPT' unless $config{ADMINISABSENTMINDED};
|
|
|
|
|
|
|
|
my $interfaces = find_interfaces_by_option 'dhcp';
|
|
|
|
|
|
|
|
for my $interface ( @$interfaces ) {
|
|
|
|
emit "\$IPTABLES -A INPUT -p udp -i $interface --dport 67:68 -j ACCEPT";
|
|
|
|
emit "\$IPTABLES -A OUTPUT -p udp -o $interface --dport 67:68 -j ACCEPT" unless $config{ADMINISABSENTMINDED};
|
|
|
|
#
|
|
|
|
# This might be a bridge
|
|
|
|
#
|
|
|
|
emit "\$IPTABLES -A FORWARD -p udp -i $interface -o $interface --dport 67:68 -j ACCEPT";
|
|
|
|
}
|
|
|
|
|
|
|
|
emit '';
|
|
|
|
|
|
|
|
if ( $config{IP_FORWARDING} eq 'on' ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( 'echo 1 > /proc/sys/net/ipv4/ip_forward',
|
|
|
|
'progress_message2 IP Forwarding Enabled' );
|
2007-06-09 01:31:48 +02:00
|
|
|
} elsif ( $config{IP_FORWARDING} eq 'off' ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( 'echo 0 > /proc/sys/net/ipv4/ip_forward',
|
|
|
|
'progress_message2 IP Forwarding Disabled!'
|
|
|
|
);
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emit 'run_stopped_exit';
|
|
|
|
|
|
|
|
pop_indent;
|
|
|
|
|
|
|
|
emit '
|
|
|
|
set_state "Stopped"
|
|
|
|
|
|
|
|
logger -p kern.info "$PRODUCT Stopped"
|
|
|
|
|
|
|
|
case $COMMAND in
|
|
|
|
stop|clear)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
#
|
|
|
|
# The firewall is being stopped when we were trying to do something
|
|
|
|
# else. Kill the shell in case we\'re running in a subshell
|
|
|
|
#
|
|
|
|
kill $$
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Second Phase of Script Generation
|
|
|
|
#
|
|
|
|
# copies the 'prog.functions' file into the script, generates
|
2007-07-26 20:36:18 +02:00
|
|
|
# clear_routing_and_traffic_shaping() and the first part of
|
2007-06-09 01:31:48 +02:00
|
|
|
# 'setup_routing_and_traffic_shaping()'
|
|
|
|
#
|
|
|
|
# The bulk of that function is produced by the various config file
|
|
|
|
# parsing routines that are called directly out of 'compiler()'.
|
|
|
|
#
|
|
|
|
# We create two separate functions rather than one so that the
|
2007-10-19 21:43:14 +02:00
|
|
|
# define_firewall() shell function can set global IP configuration variables
|
2007-06-09 01:31:48 +02:00
|
|
|
# after the old config has been cleared and before we start instantiating
|
|
|
|
# the new config. That way, the variables reflect the way that the
|
|
|
|
# distribution's tools have configured IP without any Shorewall
|
2007-10-19 21:43:14 +02:00
|
|
|
# modifications and the firewall configuration is the same after
|
|
|
|
# 'restart' as it is after 'start'.
|
2007-06-09 01:31:48 +02:00
|
|
|
#
|
2007-07-11 01:09:33 +02:00
|
|
|
# Note: This function is not called when $command eq 'check'. So it must have no side effects other
|
|
|
|
# than those related to writing to the object file.
|
|
|
|
#
|
2007-06-09 01:31:48 +02:00
|
|
|
sub generate_script_2 () {
|
|
|
|
|
|
|
|
copy $globals{SHAREDIRPL} . 'prog.functions';
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( '',
|
2007-06-09 01:31:48 +02:00
|
|
|
'#',
|
|
|
|
'# Clear Routing and Traffic Shaping',
|
|
|
|
'#',
|
|
|
|
'clear_routing_and_traffic_shaping() {'
|
|
|
|
);
|
|
|
|
|
|
|
|
push_indent;
|
|
|
|
|
|
|
|
save_progress_message 'Initializing...';
|
|
|
|
|
|
|
|
if ( $export ) {
|
|
|
|
my $fn = find_file 'modules';
|
|
|
|
|
|
|
|
if ( $fn ne "$globals{SHAREDIR}/modules" && -f $fn ) {
|
|
|
|
emit 'echo MODULESDIR="$MODULESDIR" > ${VARDIR}/.modulesdir';
|
|
|
|
emit 'cat > ${VARDIR}/.modules << EOF';
|
|
|
|
open_file $fn;
|
|
|
|
while ( read_a_line ) {
|
2007-07-07 18:34:38 +02:00
|
|
|
emit_unindented $currentline;
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
emit_unindented 'EOF';
|
|
|
|
emit 'reload_kernel_modules < ${VARDIR}/.modules';
|
|
|
|
} else {
|
|
|
|
emit 'load_kernel_modules Yes';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emit 'load_kernel_modules Yes';
|
|
|
|
}
|
|
|
|
|
|
|
|
emit '';
|
|
|
|
|
|
|
|
for my $interface ( @{find_interfaces_by_option 'norfc1918'} ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( "addr=\$(ip -f inet addr show $interface 2> /dev/null | grep 'inet\ ' | head -n1)",
|
2007-06-09 01:31:48 +02:00
|
|
|
'if [ -n "$addr" ]; then',
|
|
|
|
' addr=$(echo $addr | sed \'s/inet //;s/\/.*//;s/ peer.*//\')',
|
|
|
|
' for network in 10.0.0.0/8 176.16.0.0/12 192.168.0.0/16; do',
|
|
|
|
' if in_network $addr $network; then',
|
|
|
|
" error_message \"WARNING: The 'norfc1918' option has been specified on an interface with an RFC 1918 address. Interface:$interface\"",
|
|
|
|
' fi',
|
|
|
|
' done',
|
|
|
|
"fi\n" );
|
|
|
|
}
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit ( '[ "$COMMAND" = refresh ] && run_refresh_exit || run_init_exit',
|
2007-06-09 01:31:48 +02:00
|
|
|
'',
|
|
|
|
'qt $IPTABLES -L shorewall -n && qt $IPTABLES -F shorewall && qt $IPTABLES -X shorewall',
|
|
|
|
'',
|
|
|
|
'delete_proxyarp',
|
|
|
|
''
|
|
|
|
);
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
if ( $capabilities{NAT_ENABLED} ) {
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( 'if [ -f ${VARDIR}/nat ]; then',
|
2007-06-09 01:31:48 +02:00
|
|
|
' while read external interface; do',
|
|
|
|
' del_ip_addr $external $interface',
|
|
|
|
' done < ${VARDIR}/nat',
|
|
|
|
'',
|
|
|
|
' rm -f ${VARDIR}/nat',
|
|
|
|
"fi\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
emit "delete_tc1\n" if $config{CLEAR_TC};
|
|
|
|
emit "disable_ipv6\n" if $config{DISABLE_IPV6};
|
|
|
|
|
|
|
|
pop_indent;
|
|
|
|
|
|
|
|
emit "}\n";
|
|
|
|
|
2007-07-23 20:14:12 +02:00
|
|
|
emit( '#',
|
2007-06-09 01:31:48 +02:00
|
|
|
'# Setup Routing and Traffic Shaping',
|
|
|
|
'#',
|
|
|
|
'setup_routing_and_traffic_shaping() {'
|
|
|
|
);
|
|
|
|
|
|
|
|
push_indent;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Third (final) stage of script generation.
|
|
|
|
#
|
|
|
|
# Generate the end of 'setup_routing_and_traffic_shaping()':
|
|
|
|
# Generate code for loading the various files in /var/lib/shorewall[-lite]
|
|
|
|
# Generate code to add IP addresses under ADD_IP_ALIASES and ADD_SNAT_ALIASES
|
|
|
|
#
|
|
|
|
# Generate the 'setup_netfilter()' function that runs iptables-restore.
|
|
|
|
# Generate the 'define_firewall()' function.
|
|
|
|
#
|
2007-07-11 01:09:33 +02:00
|
|
|
# Note: This function is not called when $command eq 'check'. So it must have no side effects other
|
|
|
|
# than those related to writing to the object file.
|
|
|
|
#
|
2007-09-10 17:52:57 +02:00
|
|
|
sub generate_script_3($) {
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
emit 'cat > ${VARDIR}/proxyarp << __EOF__';
|
|
|
|
dump_proxy_arp;
|
|
|
|
emit_unindented '__EOF__';
|
|
|
|
|
2007-08-09 17:16:08 +02:00
|
|
|
emit( '',
|
|
|
|
'if [ "$COMMAND" != refresh ]; then' );
|
|
|
|
|
|
|
|
push_indent;
|
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
emit 'cat > ${VARDIR}/chains << __EOF__';
|
|
|
|
dump_rule_chains;
|
|
|
|
emit_unindented '__EOF__';
|
|
|
|
|
|
|
|
emit 'cat > ${VARDIR}/zones << __EOF__';
|
|
|
|
dump_zone_contents;
|
|
|
|
emit_unindented '__EOF__';
|
|
|
|
|
2007-08-09 17:16:08 +02:00
|
|
|
pop_indent;
|
|
|
|
|
|
|
|
emit "fi\n";
|
|
|
|
|
2007-06-09 01:31:48 +02:00
|
|
|
emit '> ${VARDIR}/nat';
|
|
|
|
|
|
|
|
add_addresses;
|
|
|
|
|
|
|
|
pop_indent;
|
|
|
|
|
|
|
|
emit "}\n";
|
|
|
|
|
|
|
|
progress_message2 "Creating iptables-restore input...";
|
|
|
|
create_netfilter_load;
|
2007-09-10 17:52:57 +02:00
|
|
|
create_chainlist_reload( $_[0] );
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
emit "#\n# Start/Restart the Firewall\n#";
|
|
|
|
emit 'define_firewall() {';
|
|
|
|
push_indent;
|
|
|
|
|
|
|
|
emit "\nclear_routing_and_traffic_shaping";
|
|
|
|
|
|
|
|
set_global_variables;
|
|
|
|
|
|
|
|
emit '';
|
|
|
|
|
|
|
|
emit<<'EOF';
|
|
|
|
setup_routing_and_traffic_shaping
|
|
|
|
|
|
|
|
if [ $COMMAND = restore ]; then
|
|
|
|
iptables_save_file=${VARDIR}/$(basename $0)-iptables
|
|
|
|
if [ -f $iptables_save_file ]; then
|
2007-09-10 17:52:57 +02:00
|
|
|
cat $iptables_save_file | $IPTABLES_RESTORE # Use this nonsensical form to appease SELinux
|
2007-06-09 01:31:48 +02:00
|
|
|
else
|
|
|
|
fatal_error "$iptables_save_file does not exist"
|
|
|
|
fi
|
|
|
|
set_state "Started"
|
|
|
|
else
|
2007-07-03 20:22:36 +02:00
|
|
|
if [ $COMMAND = refresh ]; then
|
2007-09-10 17:52:57 +02:00
|
|
|
chainlist_reload
|
2007-07-03 22:03:25 +02:00
|
|
|
run_refreshed_exit
|
2007-07-03 20:59:42 +02:00
|
|
|
$IPTABLES -N shorewall
|
|
|
|
set_state "Started"
|
2007-07-03 20:22:36 +02:00
|
|
|
else
|
|
|
|
setup_netfilter
|
|
|
|
restore_dynamic_rules
|
|
|
|
run_start_exit
|
|
|
|
$IPTABLES -N shorewall
|
|
|
|
set_state "Started"
|
|
|
|
run_started_exit
|
|
|
|
fi
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-03 20:59:42 +02:00
|
|
|
cp -f $(my_pathname) ${VARDIR}/.restore
|
2007-06-09 01:31:48 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
date > ${VARDIR}/restarted
|
|
|
|
|
|
|
|
case $COMMAND in
|
|
|
|
start)
|
|
|
|
logger -p kern.info "$PRODUCT started"
|
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
logger -p kern.info "$PRODUCT restarted"
|
|
|
|
;;
|
|
|
|
refresh)
|
|
|
|
logger -p kern.info "$PRODUCT refreshed"
|
|
|
|
;;
|
|
|
|
restore)
|
|
|
|
logger -p kern.info "$PRODUCT restored"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
EOF
|
|
|
|
|
|
|
|
pop_indent;
|
|
|
|
|
|
|
|
emit "}\n";
|
|
|
|
|
|
|
|
copy $globals{SHAREDIRPL} . 'prog.footer';
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# The Compiler.
|
|
|
|
#
|
2007-06-12 21:25:28 +02:00
|
|
|
# If the first argument is non-null, it names the script file to generate.
|
2007-06-09 01:31:48 +02:00
|
|
|
# Otherwise, this is a 'check' command and no script is produced.
|
|
|
|
#
|
2007-09-10 17:52:57 +02:00
|
|
|
sub compiler( $$$$$ ) {
|
2007-06-12 21:25:28 +02:00
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
my ( $objectfile, $directory, $verbosity, $options , $chains ) = @_;
|
2007-06-12 21:25:28 +02:00
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
$export = 0;
|
|
|
|
|
2007-06-14 16:58:48 +02:00
|
|
|
reinitialize if $reused++;
|
2007-06-14 01:02:39 +02:00
|
|
|
|
2007-06-12 21:25:28 +02:00
|
|
|
if ( $directory ne '' ) {
|
|
|
|
fatal_error "$directory is not an existing directory" unless -d $directory;
|
|
|
|
set_shorewall_dir( $directory );
|
|
|
|
}
|
2007-06-09 01:31:48 +02:00
|
|
|
|
2007-06-12 21:25:28 +02:00
|
|
|
set_verbose( $verbosity ) unless $verbosity eq '';
|
|
|
|
$export = 1 if $options & EXPORT;
|
|
|
|
set_timestamp( 1 ) if $options & TIMESTAMP;
|
2007-06-23 23:12:48 +02:00
|
|
|
set_debug( 1 ) if $options & DEBUG;
|
2007-06-09 01:31:48 +02:00
|
|
|
#
|
|
|
|
# Get shorewall.conf and capabilities.
|
|
|
|
#
|
|
|
|
get_configuration( $export );
|
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
report_capabilities;
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
require_capability( 'MULTIPORT' , "Shorewall-perl $globals{VERSION}" , 's' );
|
|
|
|
require_capability( 'RECENT_MATCH' , 'MACLIST_TTL' , 's' ) if $config{MACLIST_TTL};
|
|
|
|
require_capability( 'XCONNMARK' , 'HIGH_ROUTE_MARKS=Yes' , 's' ) if $config{HIGH_ROUTE_MARKS};
|
|
|
|
require_capability( 'MANGLE_ENABLED' , 'Traffic Shaping' , 's' ) if $config{TC_ENABLED};
|
|
|
|
require_capability( 'CONNTRACK_MATCH' , 'RFC1918_STRICT=Yes' , 's' ) if $config{RFC1918_STRICT};
|
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
set_command( 'check', 'Checking', 'Checked' ) unless $objectfile;
|
2007-06-09 01:31:48 +02:00
|
|
|
|
|
|
|
initialize_chain_table;
|
|
|
|
|
2007-07-11 01:09:33 +02:00
|
|
|
unless ( $command eq 'check' ) {
|
2007-06-09 01:31:48 +02:00
|
|
|
create_temp_object( $objectfile );
|
|
|
|
generate_script_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process the zones file.
|
|
|
|
#
|
|
|
|
determine_zones;
|
|
|
|
#
|
|
|
|
# Process the interfaces file.
|
|
|
|
#
|
|
|
|
validate_interfaces_file ( $export );
|
|
|
|
#
|
|
|
|
# Process the hosts file.
|
|
|
|
#
|
|
|
|
validate_hosts_file;
|
|
|
|
#
|
|
|
|
# Report zone contents
|
|
|
|
#
|
|
|
|
zone_report;
|
|
|
|
#
|
|
|
|
# Do action pre-processing.
|
|
|
|
#
|
|
|
|
process_actions1;
|
|
|
|
#
|
|
|
|
# Process the Policy File.
|
|
|
|
#
|
|
|
|
validate_policy;
|
|
|
|
#
|
|
|
|
# Compile the 'stop_firewall()' function
|
|
|
|
#
|
|
|
|
compile_stop_firewall;
|
|
|
|
#
|
|
|
|
# Start Second Part of script
|
|
|
|
#
|
2007-07-10 21:41:58 +02:00
|
|
|
generate_script_2 unless $command eq 'check';
|
2007-06-09 01:31:48 +02:00
|
|
|
#
|
2007-08-26 17:12:04 +02:00
|
|
|
# Set up MSS rules
|
|
|
|
#
|
|
|
|
setup_mss;
|
|
|
|
#
|
2007-06-09 01:31:48 +02:00
|
|
|
# Do all of the zone-independent stuff
|
|
|
|
#
|
|
|
|
add_common_rules;
|
|
|
|
#
|
|
|
|
# /proc stuff
|
|
|
|
#
|
|
|
|
setup_arp_filtering;
|
|
|
|
setup_route_filtering;
|
|
|
|
setup_martian_logging;
|
|
|
|
setup_source_routing;
|
|
|
|
setup_forwarding;
|
|
|
|
#
|
|
|
|
# Proxy Arp
|
|
|
|
#
|
|
|
|
setup_proxy_arp;
|
|
|
|
#
|
|
|
|
# Handle MSS setings in the zones file
|
|
|
|
#
|
|
|
|
setup_zone_mss;
|
|
|
|
#
|
|
|
|
# [Re-]establish Routing
|
|
|
|
#
|
|
|
|
setup_providers;
|
|
|
|
#
|
|
|
|
# TOS
|
|
|
|
#
|
|
|
|
process_tos;
|
|
|
|
#
|
|
|
|
# ECN
|
|
|
|
#
|
|
|
|
setup_ecn;
|
|
|
|
#
|
|
|
|
# Setup Masquerading/SNAT
|
|
|
|
#
|
|
|
|
setup_masq;
|
|
|
|
#
|
|
|
|
# MACLIST Filtration
|
|
|
|
#
|
|
|
|
setup_mac_lists 1;
|
|
|
|
#
|
|
|
|
# Process the rules file.
|
|
|
|
#
|
|
|
|
process_rules;
|
|
|
|
#
|
|
|
|
# Add Tunnel rules.
|
|
|
|
#
|
|
|
|
setup_tunnels;
|
|
|
|
#
|
|
|
|
# Post-rules action processing.
|
|
|
|
#
|
|
|
|
process_actions2;
|
|
|
|
process_actions3;
|
|
|
|
#
|
|
|
|
# MACLIST Filtration again
|
|
|
|
#
|
|
|
|
setup_mac_lists 2;
|
|
|
|
#
|
|
|
|
# Apply Policies
|
|
|
|
#
|
|
|
|
apply_policy_rules;
|
|
|
|
#
|
|
|
|
# TCRules and Traffic Shaping
|
|
|
|
#
|
|
|
|
setup_tc;
|
|
|
|
#
|
|
|
|
# Setup Nat
|
|
|
|
#
|
|
|
|
setup_nat;
|
|
|
|
#
|
|
|
|
# Setup NETMAP
|
|
|
|
#
|
|
|
|
setup_netmap;
|
|
|
|
#
|
|
|
|
# Accounting.
|
|
|
|
#
|
|
|
|
setup_accounting;
|
2007-07-11 01:09:33 +02:00
|
|
|
#
|
|
|
|
# We generate the matrix even though we don't write out the rules. That way, we insure that
|
|
|
|
# a compile of the script won't blow up during that step.
|
|
|
|
#
|
2007-06-09 01:31:48 +02:00
|
|
|
generate_matrix;
|
|
|
|
|
|
|
|
if ( $command eq 'check' ) {
|
|
|
|
progress_message3 "Shorewall configuration verified";
|
|
|
|
} else {
|
|
|
|
#
|
|
|
|
# Finish the script.
|
|
|
|
#
|
2007-09-10 17:52:57 +02:00
|
|
|
generate_script_3( $chains );
|
2007-06-09 01:31:48 +02:00
|
|
|
finalize_object ( $export );
|
|
|
|
#
|
|
|
|
# And generate the auxilary config file
|
|
|
|
#
|
|
|
|
generate_aux_config if $export;
|
|
|
|
}
|
2007-06-14 22:27:40 +02:00
|
|
|
|
|
|
|
1;
|
2007-06-09 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|