2007-06-17 23:35:42 +02:00
#
2008-05-04 02:18:47 +02:00
# Shorewall-perl 4.2 -- /usr/share/shorewall-perl/Shorewall/Config.pm
2007-03-15 22:55:22 +01: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-03-15 22:55:22 +01: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-03-15 22:55:22 +01:00
#
2007-04-19 01:43:54 +02:00
# This module is responsible for lower level configuration file handling.
# It also exports functions for generating warning and error messages.
2007-07-26 20:36:18 +02:00
# The get_configuration function parses the shorewall.conf, capabilities and
# modules files during compiler startup. The module also provides the basic
2007-07-17 16:11:20 +02:00
# output file services such as creation of temporary 'object' files, writing
2007-07-26 20:36:18 +02:00
# into those files (emitters) and finalizing those files (renaming
2007-07-17 00:07:50 +02:00
# them to their final name and setting their mode appropriately).
2007-03-15 22:55:22 +01:00
#
2007-03-14 03:44:41 +01:00
package Shorewall::Config ;
use strict ;
use warnings ;
2007-04-16 01:41:13 +02:00
use File::Basename ;
2007-07-17 00:07:50 +02:00
use File::Temp qw/ tempfile tempdir / ;
2007-09-10 17:52:57 +02:00
use Cwd qw( abs_path getcwd ) ;
2007-07-08 16:44:01 +02:00
use autouse 'Carp' = > qw( longmess confess ) ;
2007-11-16 00:24:54 +01:00
use Scalar::Util 'reftype' ;
2007-03-14 03:44:41 +01:00
our @ ISA = qw( Exporter ) ;
2007-09-10 17:52:57 +02:00
#
# Imported variables should be treated as read-only by importers
#
2007-03-29 19:36:04 +02:00
our @ EXPORT = qw(
2007-11-16 00:24:54 +01:00
warning_message
fatal_error
2007-07-17 00:07:50 +02:00
progress_message
progress_message2
progress_message3
2007-11-16 00:24:54 +01:00
) ;
our @ EXPORT_OK = qw( $shorewall_dir initialize read_a_line1 set_config_path shorewall ) ;
our % EXPORT_TAGS = ( internal = > [ qw( create_temp_object
finalize_object
2007-11-23 22:33:36 +01:00
numeric_value
2008-04-10 00:56:23 +02:00
numeric_value1
2008-01-01 18:47:58 +01:00
in_hex
2008-03-18 00:54:41 +01:00
in_hex2
2008-03-19 17:02:16 +01:00
in_hex3
2008-03-17 00:57:58 +01:00
in_hex4
2008-03-16 17:51:53 +01:00
in_hex8
2007-11-16 00:24:54 +01:00
emit
emit_unindented
save_progress_message
save_progress_message_short
set_timestamp
set_verbose
2007-11-23 22:33:36 +01:00
set_log
close_log
2007-11-16 00:24:54 +01:00
set_command
push_indent
pop_indent
copy
create_temp_aux_config
finalize_aux_config
set_shorewall_dir
set_debug
find_file
2008-01-26 02:07:57 +01:00
split_list
2007-11-16 00:24:54 +01:00
split_line
split_line1
first_entry
open_file
close_file
push_open
pop_open
read_a_line
validate_level
qt
ensure_config_path
get_configuration
require_capability
report_capabilities
propagateconfig
append_file
run_user_exit
run_user_exit1
run_user_exit2
generate_aux_config
2007-12-01 18:14:08 +01:00
2007-11-16 00:24:54 +01:00
$ command
$ doing
$ done
$ currentline
% config
% globals
% capabilities
2008-04-10 00:56:23 +02:00
MIN_VERBOSITY
MAX_VERBOSITY
2007-11-16 00:24:54 +01:00
) ] ) ;
Exporter:: export_ok_tags ( 'internal' ) ;
2008-02-06 00:50:00 +01:00
our $ VERSION = 4.1 .5 ;
2007-03-14 03:44:41 +01:00
2007-07-17 16:11:20 +02:00
#
# describe the current command, it's present progressive, and it's completion.
#
2007-07-17 00:07:50 +02:00
our ( $ command , $ doing , $ done ) ;
2007-07-17 16:11:20 +02:00
#
# VERBOSITY
#
2007-07-17 00:07:50 +02:00
our $ verbose ;
2007-07-17 16:11:20 +02:00
#
2007-11-23 22:33:36 +01:00
# Logging
#
our ( $ log , $ log_verbose ) ;
#
2007-07-17 16:11:20 +02:00
# Timestamp each progress message, if true.
#
2007-07-17 00:07:50 +02:00
our $ timestamp ;
2007-07-17 16:11:20 +02:00
#
# Object file handle
#
2007-07-17 00:07:50 +02:00
our $ object ;
2007-07-17 16:11:20 +02:00
#
# True, if last line emitted is blank
#
2007-07-17 00:07:50 +02:00
our $ lastlineblank ;
2007-07-17 16:11:20 +02:00
#
# Number of columns to indent the output
#
2007-07-17 00:07:50 +02:00
our $ indent ;
2007-07-17 16:11:20 +02:00
#
# Object's Directory and File
#
our ( $ dir , $ file ) ;
#
# Temporary output file's name
#
our $ tempfile ;
2007-03-24 19:11:32 +01:00
#
# Misc Globals
#
2007-06-14 01:02:39 +02:00
our % globals ;
#
# From shorewall.conf file
#
our % config ;
#
# Config options and global settings that are to be copied to object script
#
2007-06-19 01:04:17 +02:00
our @ propagateconfig = qw/ DISABLE_IPV6 MODULESDIR MODULE_SUFFIX LOGFORMAT SUBSYSLOCK LOCKFILE / ;
our @ propagateenv = qw/ LOGLIMIT LOGTAGONLY LOGRULENUMBERS / ;
2007-06-14 01:02:39 +02:00
#
# From parsing the capabilities file
#
our % capabilities ;
#
# Capabilities
#
2007-09-10 17:52:57 +02:00
our % capdesc = ( NAT_ENABLED = > 'NAT' ,
MANGLE_ENABLED = > 'Packet Mangling' ,
MULTIPORT = > 'Multi-port Match' ,
XMULTIPORT = > 'Extended Multi-port Match' ,
CONNTRACK_MATCH = > 'Connection Tracking Match' ,
USEPKTTYPE = > 'Packet Type Match' ,
POLICY_MATCH = > 'Policy Match' ,
PHYSDEV_MATCH = > 'Physdev Match' ,
2007-11-16 00:24:54 +01:00
PHYSDEV_BRIDGE = > 'Physdev-is-bridged support' ,
2007-09-10 17:52:57 +02:00
LENGTH_MATCH = > 'Packet length Match' ,
IPRANGE_MATCH = > 'IP Range Match' ,
RECENT_MATCH = > 'Recent Match' ,
OWNER_MATCH = > 'Owner Match' ,
IPSET_MATCH = > 'Ipset Match' ,
CONNMARK = > 'CONNMARK Target' ,
XCONNMARK = > 'Extended CONNMARK Target' ,
CONNMARK_MATCH = > 'Connmark Match' ,
XCONNMARK_MATCH = > 'Extended Connmark Match' ,
RAW_TABLE = > 'Raw Table' ,
IPP2P_MATCH = > 'IPP2P Match' ,
CLASSIFY_TARGET = > 'CLASSIFY Target' ,
ENHANCED_REJECT = > 'Extended Reject' ,
KLUDGEFREE = > 'Repeat match' ,
MARK = > 'MARK Target' ,
XMARK = > 'Extended Mark Target' ,
MANGLE_FORWARD = > 'Mangle FORWARD Chain' ,
COMMENTS = > 'Comments' ,
ADDRTYPE = > 'Address Type Match' ,
TCPMSS_MATCH = > 'TCPMSS Match' ,
HASHLIMIT_MATCH = > 'Hashlimit Match' ,
NFQUEUE_TARGET = > 'NFQUEUE Target' ,
2007-11-16 01:14:43 +01:00
REALM_MATCH = > 'Realm Match' ,
2007-09-10 17:52:57 +02:00
CAPVERSION = > 'Capability Version' ,
) ;
2007-06-14 01:02:39 +02:00
#
# Directories to search for configuration files
#
our @ config_path ;
#
# Stash away file references here when we encounter INCLUDE
#
our @ includestack ;
#
# Allow nested opens
#
our @ openstack ;
2007-07-07 18:34:38 +02:00
our $ currentline ; # Current config file line image
2007-06-14 01:02:39 +02:00
our $ currentfile ; # File handle reference
our $ currentfilename ; # File NAME
our $ currentlinenumber ; # Line number
2007-11-16 00:24:54 +01:00
our $ scriptfile ; # File Handle Reference to current temporary file being written by an in-line Perl script
our $ scriptfilename ; # Name of that file.
our @ tempfiles ; # Files that need unlinking at END
our $ first_entry ; # Message to output or function to call on first non-blank line of a file
2007-06-14 01:02:39 +02:00
2007-07-17 16:11:20 +02:00
our $ shorewall_dir ; # Shorewall Directory
2007-06-14 01:02:39 +02:00
2007-07-17 16:11:20 +02:00
our $ debug ; # If true, use Carp to report errors with stack trace.
2007-06-23 23:12:48 +02:00
2008-04-10 00:56:23 +02:00
use constant { MIN_VERBOSITY = > - 1 ,
MAX_VERBOSITY = > 2 } ;
2007-06-15 00:07:45 +02:00
#
# Initialize globals -- we take this novel approach to globals initialization to allow
# the compiler to run multiple times in the same process. The
# initialize() function does globals initialization for this
# module and is called from an INIT block below. The function is
# also called by Shorewall::Compiler::compiler at the beginning of
2007-07-26 20:36:18 +02:00
# the second and subsequent calls to that function.
2007-06-15 00:07:45 +02:00
#
2007-06-14 01:02:39 +02:00
sub initialize () {
2007-07-17 00:07:50 +02:00
( $ command , $ doing , $ done ) = qw/ compile Compiling Compiled/ ; #describe the current command, it's present progressive, and it's completion.
$ verbose = 0 ; # Verbosity setting. 0 = almost silent, 1 = major progress messages only, 2 = all progress messages (very noisy)
2007-11-23 22:33:36 +01:00
$ log = undef ; # File reference for log file
$ log_verbose = - 1 ; # Verbosity of log.
2007-07-17 00:07:50 +02:00
$ timestamp = '' ; # If true, we are to timestamp each progress message
$ object = 0 ; # Object (script) file Handle Reference
$ lastlineblank = 0 ; # Avoid extra blank lines in the output
$ indent = '' ; # Current indentation
( $ dir , $ file ) = ( '' , '' ) ; # Object's Directory and File
$ tempfile = '' ; # Temporary File Name
2007-06-14 01:02:39 +02:00
#
# Misc Globals
#
% globals = ( SHAREDIR = > '/usr/share/shorewall' ,
2007-03-31 19:44:16 +02:00
CONFDIR = > '/etc/shorewall' ,
SHAREDIRPL = > '/usr/share/shorewall-perl/' ,
2007-04-27 17:15:13 +02:00
ORIGINAL_POLICY_MATCH = > '' ,
2007-03-31 19:44:16 +02:00
LOGPARMS = > '' ,
2007-04-23 22:44:24 +02:00
TC_SCRIPT = > '' ,
2008-06-02 21:39:35 +02:00
VERSION = > "4.2.0-Beta2" ,
2007-11-19 22:15:36 +01:00
CAPVERSION = > 40100 ,
2007-03-31 19:44:16 +02:00
) ;
2007-06-14 01:02:39 +02:00
#
# From shorewall.conf file
#
% config =
2007-12-08 01:04:43 +01:00
( STARTUP_ENABLED = > undef ,
2007-03-14 03:44:41 +01:00
VERBOSITY = > undef ,
#
# Logging
#
LOGFILE = > undef ,
LOGFORMAT = > undef ,
LOGTAGONLY = > undef ,
LOGRATE = > undef ,
LOGBURST = > undef ,
LOGALLNEW = > undef ,
BLACKLIST_LOGLEVEL = > undef ,
MACLIST_LOG_LEVEL = > undef ,
TCP_FLAGS_LOG_LEVEL = > undef ,
RFC1918_LOG_LEVEL = > undef ,
SMURF_LOG_LEVEL = > undef ,
LOG_MARTIANS = > undef ,
2007-11-23 22:33:36 +01:00
LOG_VERBOSITY = > undef ,
STARTUP_LOG = > undef ,
2007-03-14 03:44:41 +01:00
#
# Location of Files
#
IPTABLES = > undef ,
2007-04-01 17:20:07 +02:00
#
2007-03-14 03:44:41 +01:00
#PATH is inherited
2007-04-01 17:20:07 +02:00
#
2007-03-14 03:44:41 +01:00
PATH = > undef ,
SHOREWALL_SHELL = > undef ,
SUBSYSLOCK = > undef ,
MODULESDIR = > undef ,
2007-04-01 17:20:07 +02:00
#
2007-03-14 03:44:41 +01:00
#CONFIG_PATH is inherited
2007-04-01 17:20:07 +02:00
#
2007-03-14 03:44:41 +01:00
CONFIG_PATH = > undef ,
RESTOREFILE = > undef ,
IPSECFILE = > undef ,
2007-04-16 01:41:13 +02:00
LOCKFILE = > undef ,
2007-03-14 03:44:41 +01:00
#
# Default Actions/Macros
#
DROP_DEFAULT = > undef ,
REJECT_DEFAULT = > undef ,
ACCEPT_DEFAULT = > undef ,
2007-07-02 23:34:08 +02:00
QUEUE_DEFAULT = > undef ,
2007-08-26 17:12:04 +02:00
NFQUEUE_DEFAULT = > undef ,
2007-03-14 03:44:41 +01:00
#
2007-05-13 19:00:19 +02:00
# RSH/RCP Commands
#
RSH_COMMAND = > undef ,
RCP_COMMAND = > undef ,
#
2007-03-14 03:44:41 +01:00
# Firewall Options
#
BRIDGING = > undef ,
IP_FORWARDING = > undef ,
ADD_IP_ALIASES = > undef ,
ADD_SNAT_ALIASES = > undef ,
RETAIN_ALIASES = > undef ,
TC_ENABLED = > undef ,
TC_EXPERT = > undef ,
CLEAR_TC = > undef ,
MARK_IN_FORWARD_CHAIN = > undef ,
CLAMPMSS = > undef ,
ROUTE_FILTER = > undef ,
DETECT_DNAT_IPADDRS = > undef ,
MUTEX_TIMEOUT = > undef ,
ADMINISABSENTMINDED = > undef ,
BLACKLISTNEWONLY = > undef ,
DELAYBLACKLISTLOAD = > undef ,
MODULE_SUFFIX = > undef ,
DISABLE_IPV6 = > undef ,
DYNAMIC_ZONES = > undef ,
PKTTYPE = > undef ,
RFC1918_STRICT = > undef ,
MACLIST_TABLE = > undef ,
MACLIST_TTL = > undef ,
SAVE_IPSETS = > undef ,
MAPOLDACTIONS = > undef ,
FASTACCEPT = > undef ,
IMPLICIT_CONTINUE = > undef ,
HIGH_ROUTE_MARKS = > undef ,
USE_ACTIONS = > undef ,
OPTIMIZE = > undef ,
EXPORTPARAMS = > undef ,
2007-07-02 23:34:08 +02:00
SHOREWALL_COMPILER = > undef ,
2007-07-19 20:36:04 +02:00
EXPAND_POLICIES = > undef ,
2007-08-10 22:55:54 +02:00
KEEP_RT_TABLES = > undef ,
2007-09-21 18:55:28 +02:00
DELETE_THEN_ADD = > undef ,
2007-09-29 00:26:29 +02:00
MULTICAST = > undef ,
2007-11-16 00:24:54 +01:00
DONT_LOAD = > '' ,
2008-03-24 23:36:48 +01:00
AUTO_COMMENT = > undef ,
MANGLE_ENABLED = > undef ,
2008-03-29 01:35:00 +01:00
NULL_ROUTE_RFC1918 = > undef ,
2007-03-14 03:44:41 +01:00
#
# Packet Disposition
#
MACLIST_DISPOSITION = > undef ,
TCP_FLAGS_DISPOSITION = > undef ,
BLACKLIST_DISPOSITION = > undef ,
) ;
2007-06-14 01:02:39 +02:00
#
# From parsing the capabilities file
#
% capabilities =
2007-04-18 22:53:25 +02:00
( NAT_ENABLED = > undef ,
2007-03-14 03:44:41 +01:00
MANGLE_ENABLED = > undef ,
MULTIPORT = > undef ,
XMULTIPORT = > undef ,
CONNTRACK_MATCH = > undef ,
USEPKTTYPE = > undef ,
POLICY_MATCH = > undef ,
PHYSDEV_MATCH = > undef ,
2007-11-16 00:24:54 +01:00
PHYSDEV_BRIDGE = > undef ,
2007-03-14 03:44:41 +01:00
LENGTH_MATCH = > undef ,
IPRANGE_MATCH = > undef ,
RECENT_MATCH = > undef ,
OWNER_MATCH = > undef ,
IPSET_MATCH = > undef ,
CONNMARK = > undef ,
XCONNMARK = > undef ,
CONNMARK_MATCH = > undef ,
XCONNMARK_MATCH = > undef ,
RAW_TABLE = > undef ,
IPP2P_MATCH = > undef ,
CLASSIFY_TARGET = > undef ,
ENHANCED_REJECT = > undef ,
KLUDGEFREE = > undef ,
MARK = > undef ,
XMARK = > undef ,
MANGLE_FORWARD = > undef ,
COMMENTS = > undef ,
ADDRTYPE = > undef ,
2007-08-26 17:12:04 +02:00
TCPMSS_MATCH = > undef ,
HASHLIMIT_MATCH = > undef ,
NFQUEUE_TARGET = > undef ,
2007-11-16 01:14:43 +01:00
REALM_MATCH = > undef ,
2007-06-17 23:35:42 +02:00
CAPVERSION = > undef ,
2007-03-14 03:44:41 +01:00
) ;
2007-06-14 01:02:39 +02:00
#
# Directories to search for configuration files
#
@ config_path = ( ) ;
#
# Stash away file references here when we encounter INCLUDE
#
@ includestack = ( ) ;
#
# Allow nested opens
#
@ openstack = ( ) ;
2007-03-29 20:57:53 +02:00
2007-07-07 18:34:38 +02:00
$ currentline = '' ; # Line image
2007-06-14 01:02:39 +02:00
$ currentfile = undef ; # File handle reference
$ currentfilename = '' ; # File NAME
$ currentlinenumber = 0 ; # Line number
2007-11-16 00:24:54 +01:00
$ first_entry = 0 ; # Message to output or function to call on first non-blank file entry
2007-06-14 01:02:39 +02:00
$ shorewall_dir = '' ; #Shorewall Directory
2007-06-23 23:12:48 +02:00
$ debug = 0 ;
2007-06-14 01:02:39 +02:00
}
2007-03-31 19:29:17 +02:00
2007-06-14 01:02:39 +02:00
INIT {
initialize ;
2008-03-12 00:04:11 +01:00
#
# These variables appear within single quotes in shorewall.conf -- add them to ENV
# so that read_a_line doesn't have to be smart enough to parse that usage.
#
for ( qw/root system command files destination/ ) {
$ ENV { $ _ } = '' unless exists $ ENV { $ _ } ;
}
2007-06-14 01:02:39 +02:00
}
2007-06-11 20:07:34 +02:00
2007-11-23 23:15:22 +01:00
my @ abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ) ;
2007-03-29 19:36:04 +02:00
#
# Issue a Warning Message
#
2007-04-08 16:42:26 +02:00
sub warning_message
2007-03-29 19:36:04 +02:00
{
2007-09-10 17:52:57 +02:00
my $ linenumber = $ currentlinenumber || 1 ;
my $ currentlineinfo = $ currentfile ? " : $currentfilename (line $linenumber)" : '' ;
2007-12-01 02:53:37 +01:00
our @ localtime ;
2007-03-30 01:07:15 +02:00
2007-11-16 00:24:54 +01:00
$| = 1 ;
2007-11-23 23:15:22 +01:00
if ( $ log ) {
@ localtime = localtime ;
2007-12-01 19:23:00 +01:00
printf $ log '%s %2d %02d:%02d:%02d ' , $ abbr [ $ localtime [ 4 ] ] , @ localtime [ 3 , 2 , 1 , 0 ] ;
2007-11-23 23:15:22 +01:00
}
2007-06-23 23:12:48 +02:00
if ( $ debug ) {
2007-07-08 16:44:01 +02:00
print STDERR longmess ( " WARNING: @_$currentlineinfo" ) ;
2007-12-01 02:53:37 +01:00
print $ log longmess ( " WARNING: @_$currentlineinfo\n" ) if $ log ;
2007-06-23 23:12:48 +02:00
} else {
2007-07-07 18:34:38 +02:00
print STDERR " WARNING: @_$currentlineinfo\n" ;
2007-11-23 22:33:36 +01:00
print $ log " WARNING: @_$currentlineinfo\n" if $ log ;
2007-06-23 23:12:48 +02:00
}
2007-11-16 00:24:54 +01:00
$| = 0 ;
2007-03-29 19:36:04 +02:00
}
2007-04-16 21:33:25 +02:00
#
# Issue fatal error message and die
#
2007-03-29 19:36:04 +02:00
sub fatal_error {
2007-09-10 17:52:57 +02:00
my $ linenumber = $ currentlinenumber || 1 ;
my $ currentlineinfo = $ currentfile ? " : $currentfilename (line $linenumber)" : '' ;
2007-11-23 22:33:36 +01:00
2007-11-16 00:24:54 +01:00
$| = 1 ;
2007-11-23 22:33:36 +01:00
if ( $ log ) {
2007-12-01 02:53:37 +01:00
our @ localtime = localtime ;
2007-12-01 19:23:00 +01:00
printf $ log '%s %2d %02d:%02d:%02d ' , $ abbr [ $ localtime [ 4 ] ] , @ localtime [ 3 , 2 , 1 , 0 ] ;
2007-11-23 23:15:22 +01:00
2007-11-23 22:33:36 +01:00
if ( $ debug ) {
2007-12-01 02:53:37 +01:00
print $ log longmess ( " ERROR: @_$currentlineinfo\n" ) ;
2007-11-23 22:33:36 +01:00
} else {
print $ log " ERROR: @_$currentlineinfo\n" ;
}
close $ log ;
$ log = undef ;
}
2007-07-08 16:44:01 +02:00
confess " ERROR: @_$currentlineinfo" if $ debug ;
2007-07-07 18:34:38 +02:00
die " ERROR: @_$currentlineinfo\n" ;
2007-06-12 01:17:02 +02:00
}
2007-03-29 19:36:04 +02:00
2007-11-16 00:24:54 +01:00
sub fatal_error1 {
$| = 1 ;
2007-11-23 22:33:36 +01:00
if ( $ log ) {
2007-12-01 02:53:37 +01:00
our @ localtime = localtime ;
2007-12-01 19:23:00 +01:00
printf $ log '%s %2d %02d:%02d:%02d ' , $ abbr [ $ localtime [ 4 ] ] , @ localtime [ 3 , 2 , 1 , 0 ] ;
2007-11-23 23:15:22 +01:00
2007-11-23 22:33:36 +01:00
if ( $ debug ) {
print $ log longmess ( " ERROR: @_\n" ) ;
} else {
print $ log " ERROR: @_\n" ;
}
close $ log ;
$ log = undef ;
}
2007-11-16 00:24:54 +01:00
confess " ERROR: @_" if $ debug ;
die " ERROR: @_\n" ;
}
2007-11-23 22:33:36 +01:00
#
# Convert value to decimal number
#
sub numeric_value ( $ ) {
2007-12-05 01:14:30 +01:00
my $ mark = lc $ _ [ 0 ] ;
return undef unless $ mark =~ /^-?(0x[a-f0-9]+|0[0-7]*|[1-9]\d*)$/ ;
$ mark =~ /^0/ ? oct $ mark : $ mark ;
}
2008-04-10 00:56:23 +02:00
sub numeric_value1 ( $ ) {
my $ val = numeric_value $ _ [ 0 ] ;
fatal_error "Invalid Number ($_[0])" unless defined $ val ;
$ val ;
}
2008-01-01 22:53:29 +01:00
#
# Return the argument expressed in Hex
#
2008-01-01 18:47:58 +01:00
sub in_hex ( $ ) {
sprintf '0x%x' , $ _ [ 0 ] ;
}
2008-03-18 00:54:41 +01:00
sub in_hex2 ( $ ) {
sprintf '0x%02x' , $ _ [ 0 ] ;
}
2008-03-19 17:02:16 +01:00
sub in_hex3 ( $ ) {
2008-03-19 17:32:08 +01:00
sprintf '0x%03x' , $ _ [ 0 ] ;
2008-03-19 17:02:16 +01:00
}
2008-03-17 00:57:58 +01:00
sub in_hex4 ( $ ) {
sprintf '0x%04x' , $ _ [ 0 ] ;
}
2008-03-16 17:51:53 +01:00
sub in_hex8 ( $ ) {
sprintf '0x%08x' , $ _ [ 0 ] ;
}
2007-07-17 00:07:50 +02:00
#
2007-07-23 20:14:12 +02:00
# Write the arguments to the object file (if any) with the current indentation.
2007-07-17 00:07:50 +02:00
#
# Replaces leading spaces with tabs as appropriate and suppresses consecutive blank lines.
#
2007-07-23 20:14:12 +02:00
sub emit {
2007-07-17 00:07:50 +02:00
if ( $ object ) {
#
# 'compile' as opposed to 'check'
#
for ( @ _ ) {
unless ( /^\s*$/ ) {
my $ line = $ _ ; # This copy is necessary because the actual arguments are almost always read-only.
$ line =~ s/^\n// if $ lastlineblank ;
$ line =~ s/^/$indent/gm if $ indent ;
$ line =~ s/ /\t/gm ;
print $ object "$line\n" ;
$ lastlineblank = ( substr ( $ line , - 1 , 1 ) eq "\n" ) ;
} else {
print $ object "\n" unless $ lastlineblank ;
$ lastlineblank = 1 ;
}
}
}
}
#
# Write passed message to the object with newline but no indentation.
#
sub emit_unindented ( $ ) {
print $ object "$_[0]\n" if $ object ;
}
#
# Write a progress_message2 command with surrounding blank lines to the output file.
#
sub save_progress_message ( $ ) {
emit "\nprogress_message2 @_\n" if $ object ;
}
#
# Write a progress_message command to the output file.
#
sub save_progress_message_short ( $ ) {
emit "progress_message $_[0]" if $ object ;
}
#
# Set $timestamp
2007-07-26 20:36:18 +02:00
#
2007-07-17 00:07:50 +02:00
sub set_timestamp ( $ ) {
$ timestamp = shift ;
}
#
# Set $verbose
2007-07-26 20:36:18 +02:00
#
2007-07-17 00:07:50 +02:00
sub set_verbose ( $ ) {
$ verbose = shift ;
}
2007-11-23 22:33:36 +01:00
#
# Set $log and $log_verbose
#
sub set_log ( $$ ) {
my ( $ l , $ v ) = @ _ ;
if ( defined $ v ) {
my $ value = numeric_value ( $ v ) ;
2007-12-07 20:06:01 +01:00
fatal_error "Invalid Log Verbosity ( $v )" unless defined ( $ value ) && ( $ value >= - 1 ) && ( $ value <= 2 ) ;
2007-11-23 22:33:36 +01:00
$ log_verbose = $ value ;
}
if ( $ l && $ log_verbose >= 0 ) {
unless ( open $ log , '>>' , $ l ) {
$ log = undef ;
2007-12-07 20:06:01 +01:00
fatal_error "Unable to open STARTUP_LOG ($l) for writing: $!" ;
2007-11-23 22:33:36 +01:00
}
} else {
$ log_verbose = - 1 ;
}
}
sub close_log () {
close $ log , $ log = undef if $ log ;
}
2007-09-10 17:52:57 +02:00
#
# Set $command, $doing and $done
#
sub set_command ( $$$ ) {
( $ command , $ doing , $ done ) = @ _ ;
}
2007-07-17 00:07:50 +02:00
#
# Print the current TOD to STDOUT.
2007-07-26 20:36:18 +02:00
#
2007-07-17 00:07:50 +02:00
sub timestamp () {
2007-12-01 02:53:37 +01:00
our @ localtime = localtime ;
printf '%02d:%02d:%02d ' , @ localtime [ 2 , 1 , 0 ] ;
2007-07-17 00:07:50 +02:00
}
#
# Write a message if $verbose >= 2
#
sub progress_message {
2007-12-01 02:53:37 +01:00
my $ havelocaltime = 0 ;
2007-07-17 00:07:50 +02:00
if ( $ verbose > 1 ) {
2007-12-01 02:53:37 +01:00
timestamp , $ havelocaltime = 1 if $ timestamp ;
2007-07-17 00:07:50 +02:00
#
# We use this function to display messages containing raw config file images which may contains tabs (including multiple tabs in succession).
# The following makes such messages look more readable and uniform
#
my $ line = "@_" ;
$ line =~ s/\s+/ /g ;
print "$line\n" ;
}
2007-11-23 22:33:36 +01:00
if ( $ log_verbose > 1 ) {
2007-12-01 02:53:37 +01:00
our @ localtime ;
@ localtime = localtime unless $ havelocaltime ;
2007-11-23 22:33:36 +01:00
2007-12-01 19:23:00 +01:00
printf $ log '%s %2d %2d:%02d:%02d ' , $ abbr [ $ localtime [ 4 ] ] , @ localtime [ 3 , 2 , 1 , 0 ] ;
2007-11-23 22:33:36 +01:00
my $ line = "@_" ;
$ line =~ s/\s+/ /g ;
print $ log "$line\n" ;
}
2007-07-17 00:07:50 +02:00
}
#
# Write a message if $verbose >= 1
#
sub progress_message2 {
2007-12-01 02:53:37 +01:00
my $ havelocaltime = 0 ;
2007-07-17 00:07:50 +02:00
if ( $ verbose > 0 ) {
2007-12-01 02:53:37 +01:00
timestamp , $ havelocaltime = 1 if $ timestamp ;
2007-07-17 00:07:50 +02:00
print "@_\n" ;
}
2007-11-23 22:33:36 +01:00
if ( $ log_verbose > 0 ) {
2007-12-01 02:53:37 +01:00
our @ localtime ;
@ localtime = localtime unless $ havelocaltime ;
2007-11-23 22:33:36 +01:00
2007-12-01 19:23:00 +01:00
printf $ log '%s %2d %02d:%02d:%02d ' , $ abbr [ $ localtime [ 4 ] ] , @ localtime [ 3 , 2 , 1 , 0 ] ;
2007-11-23 22:33:36 +01:00
print $ log "@_\n" ;
}
2007-07-17 00:07:50 +02:00
}
#
# Write a message if $verbose >= 0
#
sub progress_message3 {
2007-12-01 02:53:37 +01:00
my $ havelocaltime = 0 ;
2007-07-17 00:07:50 +02:00
if ( $ verbose >= 0 ) {
2007-12-01 02:53:37 +01:00
timestamp , $ havelocaltime = 1 if $ timestamp ;
2007-07-17 00:07:50 +02:00
print "@_\n" ;
}
2007-11-23 22:33:36 +01:00
2007-12-01 02:53:37 +01:00
if ( $ log_verbose >= 0 ) {
our @ localtime ;
@ localtime = localtime unless $ havelocaltime ;
2007-11-23 22:33:36 +01:00
2007-12-01 19:23:00 +01:00
printf $ log '%s %2d %02d:%02d:%02d ' , $ abbr [ $ localtime [ 4 ] ] , @ localtime [ 3 , 2 , 1 , 0 ] ;
2007-11-23 22:33:36 +01:00
print $ log "@_\n" ;
}
2007-07-17 00:07:50 +02:00
}
#
# Push/Pop Indent
#
sub push_indent () {
$ indent = "$indent " ;
}
sub pop_indent () {
$ indent = substr ( $ indent , 0 , ( length $ indent ) - 4 ) ;
}
#
# Functions for copying files into the object
#
sub copy ( $ ) {
if ( $ object ) {
my $ file = $ _ [ 0 ] ;
open IF , $ file or fatal_error "Unable to open $file: $!" ;
while ( <IF> ) {
2007-10-19 21:43:14 +02:00
if ( /^\s*$/ ) {
print $ object "\n" unless $ lastlineblank ;
$ lastlineblank = 1 ;
} else {
s/^/$indent/ if $ indent ;
print $ object $ _ ;
$ lastlineblank = 0 ;
}
2007-07-17 00:07:50 +02:00
}
close IF ;
}
}
#
# This one handles line continuation.
sub copy1 ( $ ) {
if ( $ object ) {
my $ file = $ _ [ 0 ] ;
open IF , $ file or fatal_error "Unable to open $file: $!" ;
my $ do_indent = 1 ;
while ( <IF> ) {
if ( /^\s*$/ ) {
print $ object "\n" ;
$ do_indent = 1 ;
next ;
}
s/^/$indent/ if $ indent && $ do_indent ;
print $ object $ _ ;
$ do_indent = ! ( /\\$/ ) ;
}
close IF ;
}
}
#
# Create the temporary object file -- the passed file name is the name of the final file.
# We create a temporary file in the same directory so that we can use rename to finalize it.
#
sub create_temp_object ( $ ) {
my $ objectfile = $ _ [ 0 ] ;
my $ suffix ;
eval {
( $ file , $ dir , $ suffix ) = fileparse ( $ objectfile ) ;
} ;
die if $@ ;
fatal_error "$dir is a Symbolic Link" if - l $ dir ;
2007-08-09 17:16:08 +02:00
fatal_error "Directory $dir does not exist" unless - d _ ;
fatal_error "Directory $dir is not writable" unless - w _ ;
fatal_error "$objectfile is a Symbolic Link" if - l $ objectfile ;
fatal_error "$objectfile is a Directory" if - d _ ;
2007-07-17 00:07:50 +02:00
fatal_error "$objectfile exists and is not a compiled script" if - e _ && ! - x _ ;
eval {
$ dir = abs_path $ dir ;
( $ object , $ tempfile ) = tempfile ( 'tempfileXXXX' , DIR = > $ dir ) ;
} ;
fatal_error "Unable to create temporary file in directory $dir" if $@ ;
$ file = "$file.$suffix" if $ suffix ;
$ dir . = '/' unless substr ( $ dir , - 1 , 1 ) eq '/' ;
$ file = $ dir . $ file ;
}
#
# Finalize the object file
#
sub finalize_object ( $ ) {
my $ export = $ _ [ 0 ] ;
close $ object ;
$ object = 0 ;
rename $ tempfile , $ file or fatal_error "Cannot Rename $tempfile to $file: $!" ;
chmod 0700 , $ file or fatal_error "Cannot secure $file for execute access" ;
progress_message3 "Shorewall configuration compiled to $file" unless $ export ;
}
#
# Create the temporary aux config file.
#
sub create_temp_aux_config () {
eval {
( $ object , $ tempfile ) = tempfile ( 'tempfileXXXX' , DIR = > $ dir ) ;
} ;
die if $@ ;
}
#
# Finalize the aux config file.
#
sub finalize_aux_config () {
close $ object ;
$ object = 0 ;
rename $ tempfile , "$file.conf" or fatal_error "Cannot Rename $tempfile to $file.conf: $!" ;
progress_message3 "Shorewall configuration compiled to $file" ;
}
2007-06-27 00:49:16 +02:00
#
2007-09-10 17:52:57 +02:00
# Set $config{CONFIG_PATH}
2007-06-27 00:49:16 +02:00
#
sub set_config_path ( $ ) {
$ config { CONFIG_PATH } = shift ;
}
2007-06-23 23:12:48 +02:00
#
# Set $debug
#
sub set_debug ( $ ) {
$ debug = shift ;
}
2007-03-14 03:44:41 +01:00
#
# Search the CONFIG_PATH for the passed file
#
2007-04-08 16:42:26 +02:00
sub find_file ($)
2007-03-14 03:44:41 +01:00
{
my $ filename = $ _ [ 0 ] ;
2007-06-30 18:11:56 +02:00
return $ filename if $ filename =~ '/' ;
2007-03-14 03:44:41 +01:00
my $ directory ;
2007-03-31 00:38:09 +02:00
for $ directory ( @ config_path ) {
2007-03-30 01:07:15 +02:00
my $ file = "$directory$filename" ;
return $ file if - f $ file ;
2007-03-14 03:44:41 +01:00
}
2007-03-31 19:44:16 +02:00
"$globals{CONFDIR}/$filename" ;
2007-03-14 03:44:41 +01:00
}
2007-03-14 03:50:28 +01:00
2008-01-26 02:07:57 +01:00
sub split_list ( $$ ) {
my ( $ list , $ type ) = @ _ ;
2008-01-26 23:15:07 +01:00
fatal_error "Invalid $type list ($list)" if $ list =~ /^,|,$|,,|!,|,!$/ ;
2008-01-26 02:07:57 +01:00
split /,/ , $ list ;
}
2007-03-30 01:07:15 +02:00
#
# Pre-process a line from a configuration file.
2007-04-01 01:43:10 +02:00
2007-03-30 01:07:15 +02:00
# ensure that it has an appropriate number of columns.
# supply '-' in omitted trailing columns.
#
2007-04-01 17:38:05 +02:00
sub split_line ( $$$ ) {
my ( $ mincolumns , $ maxcolumns , $ description ) = @ _ ;
2007-03-30 01:07:15 +02:00
2007-07-07 18:34:38 +02:00
fatal_error "Shorewall Configuration file entries may not contain single quotes, double quotes, single back quotes or backslashes" if $ currentline =~ /["'`\\]/ ;
2008-02-06 19:01:24 +01:00
fatal_error "Non-ASCII gunk in file" if $ currentline =~ /[^\s[:print:]]/ ;
2007-05-10 17:29:41 +02:00
2007-07-07 18:34:38 +02:00
my @ line = split ( ' ' , $ currentline ) ;
2007-05-10 17:29:41 +02:00
2008-03-22 18:35:46 +01:00
my $ line = @ line ;
fatal_error "Invalid $description entry (too many columns)" if $ line > $ maxcolumns ;
2008-03-22 18:41:12 +01:00
$ line - - while $ line > 0 && $ line [ $ line - 1 ] eq '-' ;
2008-03-22 18:35:46 +01:00
fatal_error "Invalid $description entry (too few columns)" if $ line < $ mincolumns ;
2007-05-10 17:29:41 +02:00
push @ line , '-' while @ line < $ maxcolumns ;
@ line ;
}
2007-05-10 18:13:16 +02:00
#
2008-03-28 01:05:25 +01:00
# Version of 'split_line' used on files with exceptions
2007-05-10 18:13:16 +02:00
#
2008-03-28 01:05:25 +01:00
sub split_line1 ( $$$;$ ) {
my ( $ mincolumns , $ maxcolumns , $ description , $ nopad ) = @ _ ;
2007-05-10 17:29:41 +02:00
2007-07-07 18:34:38 +02:00
fatal_error "Shorewall Configuration file entries may not contain double quotes, single back quotes or backslashes" if $ currentline =~ /["`\\]/ ;
2007-05-10 17:29:41 +02:00
2007-07-07 18:34:38 +02:00
my @ line = split ( ' ' , $ currentline ) ;
2007-05-10 17:29:41 +02:00
2008-03-28 01:05:25 +01:00
$ nopad = { COMMENT = > 0 } unless $ nopad ;
2007-03-30 01:07:15 +02:00
2007-05-10 17:29:41 +02:00
my $ first = $ line [ 0 ] ;
2008-03-28 01:05:25 +01:00
my $ columns = $ nopad - > { $ first } ;
2007-05-10 17:29:41 +02:00
if ( defined $ columns ) {
2008-03-28 02:47:40 +01:00
fatal_error "Invalid $first entry" if $ columns && @ line != $ columns ;
2007-07-26 20:36:18 +02:00
return @ line
2007-05-10 17:29:41 +02:00
}
2007-03-30 01:07:15 +02:00
2007-07-07 18:34:38 +02:00
fatal_error "Shorewall Configuration file entries may not contain single quotes" if $ currentline =~ /'/ ;
2007-05-09 22:55:54 +02:00
2008-03-22 18:35:46 +01:00
my $ line = @ line ;
fatal_error "Invalid $description entry (too many columns)" if $ line > $ maxcolumns ;
2008-03-22 18:41:12 +01:00
$ line - - while $ line > 0 && $ line [ $ line - 1 ] eq '-' ;
2008-03-22 18:35:46 +01:00
fatal_error "Invalid $description entry (too few columns)" if $ line < $ mincolumns ;
2007-03-30 01:07:15 +02:00
2007-04-01 17:38:05 +02:00
push @ line , '-' while @ line < $ maxcolumns ;
2007-03-30 01:07:15 +02:00
@ line ;
}
2007-03-29 17:47:47 +02:00
#
2007-03-31 00:38:09 +02:00
# Open a file, setting $currentfile. Returns the file's absolute pathname if the file
2007-03-30 17:57:08 +02:00
# exists, is non-empty and was successfully opened. Terminates with a fatal error
# if the file exists, is non-empty, but the open fails.
2007-03-29 17:47:47 +02:00
#
2007-04-01 01:43:10 +02:00
sub do_open_file ( $ ) {
my $ fname = $ _ [ 0 ] ;
open $ currentfile , '<' , $ fname or fatal_error "Unable to open $fname: $!" ;
$ currentlinenumber = 0 ;
$ currentfilename = $ fname ;
}
2007-03-29 17:47:47 +02:00
sub open_file ( $ ) {
my $ fname = find_file $ _ [ 0 ] ;
fatal_error 'Internal Error in open_file()' if defined $ currentfile ;
2007-11-25 18:21:22 +01:00
- f $ fname && - s _ ? do_open_file $ fname : '' ;
2007-03-29 17:47:47 +02:00
}
2007-11-16 00:24:54 +01:00
#
# Pop the include stack
#
sub pop_include () {
my $ arrayref = pop @ includestack ;
if ( $ arrayref ) {
( $ currentfile , $ currentfilename , $ currentlinenumber ) = @$ arrayref ;
} else {
$ currentfile = undef ;
}
}
2007-03-31 20:44:48 +02:00
#
2007-04-08 16:42:26 +02:00
# This function is normally called below in read_a_line() when EOF is reached. Clients of the
2007-03-31 20:44:48 +02:00
# module may also call the function to close the file before EOF
#
sub close_file () {
if ( $ currentfile ) {
2007-11-16 00:24:54 +01:00
my $ result = close $ currentfile ;
2007-04-08 16:42:26 +02:00
2007-11-16 00:24:54 +01:00
pop_include ;
fatal_error "SHELL Script failed" unless $ result ;
$ first_entry = 0 ;
2007-03-31 20:44:48 +02:00
}
}
2007-04-08 16:35:31 +02:00
#
# The following two functions allow module clients to nest opens. This happens frequently
# in the Actions module.
#
2007-03-29 17:47:47 +02:00
sub push_open ( $ ) {
2007-04-03 21:06:23 +02:00
push @ includestack , [ $ currentfile , $ currentfilename , $ currentlinenumber ] ;
my @ a = @ includestack ;
push @ openstack , \ @ a ;
@ includestack = ( ) ;
2007-03-29 17:47:47 +02:00
$ currentfile = undef ;
open_file ( $ _ [ 0 ] ) ;
}
sub pop_open () {
2007-04-03 21:06:23 +02:00
@ includestack = @ { pop @ openstack } ;
2007-11-16 00:24:54 +01:00
pop_include ;
}
2007-03-29 19:36:04 +02:00
2007-11-16 00:24:54 +01:00
sub shorewall {
unless ( $ scriptfile ) {
fatal_error "shorewall() may not be called in this context" unless $ currentfile ;
2007-03-29 19:36:04 +02:00
2007-11-16 00:24:54 +01:00
$ dir || = '/tmp/' ;
eval {
( $ scriptfile , $ scriptfilename ) = tempfile ( 'scriptfileXXXX' , DIR = > $ dir ) ;
} ;
fatal_error "Unable to create temporary file in directory $dir" if $@ ;
}
print $ scriptfile "@_\n" ;
}
#
# We don't announce that we are checking/compiling a file until we determine that the file contains
# at least one non-blank, non-commentary line.
#
# The argument to this function may be either a scalar or a function reference. When the first
# non-blank/non-commentary line is reached:
#
# - if a function reference was passed to first_entry(), that function is called
# - otherwise, the argument to first_entry() is passed to progress_message2().
#
# We do this processing in read_a_line() rather than in the higher-level routines because
# Embedded Shell/Perl scripts are processed out of read_a_line(). If we were to defer announcement
# until we get back to the caller of read_a_line(), we could issue error messages about parsing and
# running scripts in the file before we'd even indicated that we are processing it.
#
sub first_entry ( $ ) {
$ first_entry = $ _ [ 0 ] ;
my $ reftype = reftype $ first_entry ;
if ( $ reftype ) {
fatal_error "Invalid argument to first_entry()" unless $ reftype eq 'CODE' ;
}
}
sub embedded_shell ( $ ) {
my $ multiline = shift ;
fatal_error "INCLUDEs nested too deeply" if @ includestack >= 4 ;
my ( $ command , $ linenumber ) = ( "/bin/sh -c '$currentline" , $ currentlinenumber ) ;
if ( $ multiline ) {
#
# Multi-line script
#
fatal_error "Invalid BEGIN SHELL directive" unless $ currentline =~ /^\s*$/ ;
$ command . = "\n" ;
my $ last = 0 ;
while ( <$currentfile> ) {
$ currentlinenumber + + ;
last if $ last = s/^\s*END(\s+SHELL)?\s*;?// ;
$ command . = $ _ ;
}
fatal_error ( "Missing END SHELL" ) unless $ last ;
fatal_error ( "Invalid END SHELL directive" ) unless /^\s*$/ ;
}
$ command . = q( ' ) ;
push @ includestack , [ $ currentfile , $ currentfilename , $ currentlinenumber ] ;
$ currentfile = undef ;
open $ currentfile , '-|' , $ command or fatal_error qq( Shell Command failed ) ;
$ currentfilename = "SHELL\@$currentfilename:$currentlinenumber" ;
$ currentline = '' ;
$ currentlinenumber = 0 ;
}
sub embedded_perl ( $ ) {
my $ multiline = shift ;
my ( $ command , $ linenumber ) = ( qq( package Shorewall::User;\nno strict;\nuse Shorewall::Config qw/shorewall/;\n# line $currentlinenumber "$currentfilename"\n$currentline ) , $ currentlinenumber ) ;
if ( $ multiline ) {
#
# Multi-line script
#
fatal_error "Invalid BEGIN PERL directive" unless $ currentline =~ /^\s*$/ ;
$ command . = "\n" ;
my $ last = 0 ;
while ( <$currentfile> ) {
$ currentlinenumber + + ;
last if $ last = s/^\s*END(\s+PERL)?\s*;?// ;
$ command . = $ _ ;
}
fatal_error ( "Missing END PERL" ) unless $ last ;
fatal_error ( "Invalid END PERL directive" ) unless /^\s*$/ ;
}
unless ( my $ return = eval $ command ) {
if ( $@ ) {
#
# Perl found the script offensive or the script itself died
#
$@ =~ s/, <\$currentfile> line \d+//g ;
fatal_error1 "$@" ;
}
unless ( defined $ return ) {
fatal_error "Perl Script failed: $!" if $! ;
fatal_error "Perl Script failed" ;
}
fatal_error "Perl Script Returned False" ;
}
if ( $ scriptfile ) {
fatal_error "INCLUDEs nested too deeply" if @ includestack >= 4 ;
close $ scriptfile or fatal_error "Internal Error in embedded_perl()" ;
$ scriptfile = undef ;
push @ includestack , [ $ currentfile , $ currentfilename , $ currentlinenumber ] ;
2007-03-29 19:36:04 +02:00
$ currentfile = undef ;
2007-11-16 00:24:54 +01:00
open $ currentfile , '<' , $ scriptfilename or fatal_error "Unable to open Perl Script $scriptfilename" ;
push @ tempfiles , $ scriptfilename unless unlink $ scriptfilename ; #unlink fails on Cygwin
$ scriptfilename = '' ;
$ currentfilename = "PERL\@$currentfilename:$linenumber" ;
$ currentline = '' ;
$ currentlinenumber = 0 ;
2007-03-29 19:36:04 +02:00
}
2007-04-08 16:42:26 +02:00
}
2007-03-29 17:47:47 +02:00
2007-03-29 19:02:13 +02:00
#
2007-04-08 16:35:31 +02:00
# Read a line from the current include stack.
2007-03-29 19:02:13 +02:00
#
# - Ignore blank or comment-only lines.
# - Remove trailing comments.
2007-04-01 01:43:10 +02:00
# - Handle Line Continuation
2007-11-16 00:24:54 +01:00
# - Handle embedded SHELL and PERL scripts
2007-03-29 19:02:13 +02:00
# - Expand shell variables from $ENV.
# - Handle INCLUDE <filename>
#
2007-07-18 18:45:20 +02:00
sub read_a_line () {
2007-03-29 17:47:47 +02:00
while ( $ currentfile ) {
2007-03-29 16:24:35 +02:00
2007-07-07 18:34:38 +02:00
$ currentline = '' ;
2007-09-10 17:52:57 +02:00
$ currentlinenumber = 0 ;
2007-03-29 19:02:13 +02:00
2007-07-07 18:18:27 +02:00
while ( <$currentfile> ) {
2007-03-30 17:57:08 +02:00
2007-09-10 17:52:57 +02:00
$ currentlinenumber = $. unless $ currentlinenumber ;
2007-07-07 18:18:27 +02:00
chomp ;
2007-04-01 01:43:10 +02:00
#
2007-04-01 02:05:42 +02:00
# Continuation
2007-04-01 01:43:10 +02:00
#
2007-07-07 18:34:38 +02:00
chop $ currentline , next if substr ( ( $ currentline . = $ _ ) , - 1 , 1 ) eq '\\' ;
2007-07-21 22:40:34 +02:00
#
# Remove Trailing Comments -- result might be a blank line
#
2007-07-26 20:36:18 +02:00
$ currentline =~ s/#.*$// ;
2007-04-01 02:05:42 +02:00
#
# Ignore ( concatenated ) Blank Lines
#
2007-09-23 17:21:18 +02:00
$ currentline = '' , $ currentlinenumber = 0 , next if $ currentline =~ /^\s*$/ ;
2007-04-01 01:43:10 +02:00
#
2008-02-06 19:01:24 +01:00
# Line not blank -- Handle any first-entry message/capabilities check
2007-04-01 01:43:10 +02:00
#
2007-11-16 00:24:54 +01:00
if ( $ first_entry ) {
reftype ( $ first_entry ) ? $ first_entry - > ( ) : progress_message2 ( $ first_entry ) ;
$ first_entry = 0 ;
2007-07-05 16:01:50 +02:00
}
2007-11-16 00:24:54 +01:00
#
# Must check for shell/perl before doing variable expansion
#
if ( $ currentline =~ s/^\s*(BEGIN\s+)?SHELL\s*;?// ) {
embedded_shell ( $ 1 ) ;
} elsif ( $ currentline =~ s/^\s*(BEGIN\s+)?PERL\s*\;?// ) {
embedded_perl ( $ 1 ) ;
} else {
my $ count = 0 ;
#
# Expand Shell Variables using %ENV
#
# $1 $2 $3 - $4
while ( $ currentline =~ m ( ^(.*?) \ $( { ) ? ( [ a - zA - Z ] \ w * ) ( ? ( 2 ) } ) ( . * ) $ ) x ) {
my $ val = $ ENV { $ 3 } ;
2008-03-07 01:36:16 +01:00
unless ( defined $ val ) {
fatal_error "Undefined shell variable (\$$3)" unless exists $ ENV { $ 3 } ;
$ val = '' ;
}
2007-11-16 00:24:54 +01:00
$ currentline = join ( '' , $ 1 , $ val , $ 4 ) ;
fatal_error "Variable Expansion Loop" if + + $ count > 100 ;
}
if ( $ currentline =~ /^\s*INCLUDE\s/ ) {
my @ line = split ' ' , $ currentline ;
fatal_error "Invalid INCLUDE command" if @ line != 2 ;
fatal_error "INCLUDEs/Scripts nested too deeply" if @ includestack >= 4 ;
my $ filename = find_file $ line [ 1 ] ;
fatal_error "INCLUDE file $filename not found" unless - f $ filename ;
fatal_error "Directory ($filename) not allowed in INCLUDE" if - d _ ;
if ( - s _ ) {
push @ includestack , [ $ currentfile , $ currentfilename , $ currentlinenumber ] ;
$ currentfile = undef ;
do_open_file $ filename ;
} else {
$ currentlinenumber = 0 ;
}
$ currentline = '' ;
2007-09-10 17:52:57 +02:00
} else {
2007-11-16 00:24:54 +01:00
return 1 ;
2007-04-01 01:43:10 +02:00
}
2007-03-29 06:22:10 +02:00
}
}
2007-04-08 16:42:26 +02:00
2007-03-31 20:44:48 +02:00
close_file ;
2007-03-29 06:22:10 +02:00
}
}
2007-06-20 03:12:35 +02:00
#
# Simple version of the above. Doesn't do line concatenation, shell variable expansion or INCLUDE processing
#
2007-07-18 18:45:20 +02:00
sub read_a_line1 () {
2007-06-20 03:12:35 +02:00
while ( $ currentfile ) {
2007-07-07 18:34:38 +02:00
while ( $ currentline = <$currentfile> ) {
next if $ currentline =~ /^\s*#/ ;
chomp $ currentline ;
next if $ currentline =~ /^\s*$/ ;
$ currentline =~ s/#.*$// ; # Remove Trailing Comments
2008-02-06 18:45:03 +01:00
fatal_error "Non-ASCII gunk in file" if $ currentline =~ /[^\s[:print:]]/ ;
2007-07-26 16:32:25 +02:00
$ currentlinenumber = $. ;
2007-06-20 03:12:35 +02:00
return 1 ;
}
close_file ;
}
}
2007-04-16 21:33:25 +02:00
#
# Provide the passed default value for the passed configuration variable
#
2007-03-30 05:30:28 +02:00
sub default ( $$ ) {
my ( $ var , $ val ) = @ _ ;
$ config { $ var } = $ val unless defined $ config { $ var } && $ config { $ var } ne '' ;
}
2007-04-16 21:33:25 +02:00
#
# Provide a default value for a yes/no configuration variable.
#
2007-03-30 05:30:28 +02:00
sub default_yes_no ( $$ ) {
my ( $ var , $ val ) = @ _ ;
my $ curval = "\L$config{$var}" ;
2007-04-16 21:33:25 +02:00
if ( defined $ curval && $ curval ne '' ) {
2007-03-30 05:30:28 +02:00
if ( $ curval eq 'no' ) {
$ config { $ var } = '' ;
} else {
fatal_error "Invalid value for $var ($val)" unless $ curval eq 'yes' ;
}
} else {
$ config { $ var } = $ val ;
}
}
2007-04-24 16:53:45 +02:00
2007-12-01 18:14:08 +01:00
my % validlevels = ( DEBUG = > 7 ,
INFO = > 6 ,
NOTICE = > 5 ,
WARNING = > 4 ,
WARN = > 4 ,
ERR = > 3 ,
ERROR = > 3 ,
CRIT = > 2 ,
ALERT = > 1 ,
EMERG = > 0 ,
PANIC = > 0 ,
NONE = > '' ,
2007-11-18 19:05:08 +01:00
ULOG = > 'ULOG' ,
NFLOG = > 'NFLOG' ) ;
2007-04-24 16:53:45 +02:00
2007-11-20 17:01:27 +01:00
my @ suffixes = qw( group range threshold nlgroup cprange qthreshold ) ;
2007-11-18 21:56:40 +01:00
2007-04-24 16:53:45 +02:00
#
2007-07-28 16:56:39 +02:00
# Validate a log level -- Drop the trailing '!' and translate to numeric value if appropriate"
2007-04-24 16:53:45 +02:00
#
2007-11-18 21:56:40 +01:00
sub level_error ( $ ) {
fatal_error "Invalid log level ($_[0])" ;
}
2007-04-24 16:53:45 +02:00
sub validate_level ( $ ) {
2007-12-07 06:36:51 +01:00
my $ rawlevel = $ _ [ 0 ] ;
my $ level = uc $ rawlevel ;
2007-04-24 16:53:45 +02:00
if ( defined $ level && $ level ne '' ) {
2007-05-01 02:01:24 +02:00
$ level =~ s/!$// ;
2007-04-24 16:53:45 +02:00
my $ value = $ validlevels { $ level } ;
return $ value if defined $ value ;
return $ level if $ level =~ /^[0-7]$/ ;
2007-11-18 19:05:08 +01:00
2007-11-18 21:56:40 +01:00
if ( $ level =~ /^(NFLOG|ULOG)[(](.*)[)]$/ ) {
my $ olevel = $ 1 ;
my @ options = split /,/ , $ 2 ;
my $ prefix = lc $ olevel ;
2007-11-20 17:01:27 +01:00
my $ index = $ prefix eq 'ulog' ? 3 : 0 ;
2007-11-18 19:05:08 +01:00
2007-11-18 21:56:40 +01:00
level_error ( $ level ) if @ options > 3 ;
2007-11-18 19:05:08 +01:00
2007-11-18 21:56:40 +01:00
for ( @ options ) {
if ( defined $ _ and $ _ ne '' ) {
level_error ( $ level ) unless /^\d+/ ;
$ olevel . = " --${prefix}-$suffixes[$index] $_" ;
}
$ index + + ;
}
2007-11-18 19:05:08 +01:00
2007-11-18 21:56:40 +01:00
return $ olevel ;
2007-11-18 19:05:08 +01:00
}
2007-11-18 21:56:40 +01:00
if ( $ level =~ /^NFLOG --/ or $ level =~ /^ULOG --/ ) {
2007-12-07 06:36:51 +01:00
return $ rawlevel ;
2007-11-18 19:05:08 +01:00
}
2007-12-07 06:36:51 +01:00
level_error ( $ rawlevel ) ;
2007-04-24 16:53:45 +02:00
}
'' ;
}
#
# Validate a log level and supply default
#
sub default_log_level ( $$ ) {
my ( $ level , $ default ) = @ _ ;
my $ value = $ config { $ level } ;
2007-04-24 23:27:40 +02:00
unless ( defined $ value && $ value ne '' ) {
2007-04-24 16:53:45 +02:00
$ config { $ level } = $ default ;
} else {
$ config { $ level } = validate_level $ value ;
}
}
2007-04-22 00:41:47 +02:00
#
# Check a tri-valued variable
#
2007-04-22 21:30:42 +02:00
sub check_trivalue ( $$ ) {
my ( $ var , $ default ) = @ _ ;
2007-04-22 00:41:47 +02:00
my $ val = "\L$config{$var}" ;
2007-03-30 05:30:28 +02:00
2007-04-22 00:41:47 +02:00
if ( defined $ val ) {
2007-04-22 21:30:42 +02:00
if ( $ val eq 'yes' || $ val eq 'on' ) {
2007-04-22 21:45:27 +02:00
$ config { $ var } = 'on' ;
2007-04-22 21:30:42 +02:00
} elsif ( $ val eq 'no' || $ val eq 'off' ) {
2007-04-22 21:45:27 +02:00
$ config { $ var } = 'off' ;
2007-04-22 00:41:47 +02:00
} elsif ( $ val eq 'keep' ) {
$ config { $ var } = '' ;
2007-04-22 21:30:42 +02:00
} elsif ( $ val eq '' ) {
2007-04-23 21:49:02 +02:00
$ config { $ var } = $ default
2007-04-22 21:30:42 +02:00
} else {
2007-06-16 23:08:12 +02:00
fatal_error "Invalid value ($val) for $var" ;
2007-04-22 00:41:47 +02:00
}
2007-04-22 21:30:42 +02:00
} else {
$ config { var } = $ default
2007-04-22 00:41:47 +02:00
}
}
2007-07-26 20:36:18 +02:00
2007-04-16 21:33:25 +02:00
#
# Produce a report of the detected capabilities
#
2007-03-30 05:30:28 +02:00
sub report_capabilities () {
sub report_capability ( $ ) {
my $ cap = $ _ [ 0 ] ;
print " $capdesc{$cap}: " ;
2007-06-22 01:15:00 +02:00
if ( $ cap eq 'CAPVERSION' ) {
my $ version = $ capabilities { CAPVERSION } ;
printf "%d.%d.%d\n" , int ( $ version / 10000 ) , int ( ( $version % 10000 ) / 100 ) , int ( $ version % 100 ) ;
} else {
print $ capabilities { $ cap } ? "Available\n" : "Not Available\n" ;
}
2007-03-30 05:30:28 +02:00
}
2007-09-10 17:52:57 +02:00
if ( $ verbose > 1 ) {
print "Shorewall has detected the following capabilities:\n" ;
2007-03-30 05:30:28 +02:00
2007-09-10 17:52:57 +02:00
for my $ cap ( sort { $ capdesc { $ a } cmp $ capdesc { $ b } } keys % capabilities ) {
report_capability $ cap ;
}
2007-03-30 05:30:28 +02:00
}
}
2007-04-16 21:33:25 +02:00
#
# Search the current PATH for the passed executable
#
2007-09-10 17:52:57 +02:00
sub which ( $ ) {
2007-04-01 18:42:49 +02:00
my $ prog = $ _ [ 0 ] ;
2007-12-01 18:14:08 +01:00
for ( split /:/ , $ config { PATH } ) {
return "$_/$prog" if - x "$_/$prog" ;
2007-04-01 18:42:49 +02:00
}
'' ;
}
2007-04-16 21:33:25 +02:00
#
# Load the kernel modules defined in the 'modules' file.
#
2007-04-01 21:30:09 +02:00
sub load_kernel_modules ( ) {
2007-09-10 17:52:57 +02:00
my $ moduleloader = which ( 'modprobe' ) || ( which 'insmod' ) ;
2007-04-01 21:30:09 +02:00
my $ modulesdir = $ config { MODULESDIR } ;
unless ( $ modulesdir ) {
my $ uname = `uname -r` ;
fatal_error "The command 'uname -r' failed" unless $? == 0 ;
chomp $ uname ;
$ modulesdir = "/lib/modules/$uname/kernel/net/ipv4/netfilter:/lib/modules/$uname/kernel/net/netfilter" ;
}
my @ moduledirectories = split /:/ , $ modulesdir ;
2007-09-10 17:52:57 +02:00
if ( $ moduleloader && open_file 'modules' ) {
2007-04-01 21:30:09 +02:00
my % loadedmodules ;
2007-04-08 16:42:26 +02:00
2008-01-26 02:07:57 +01:00
$ loadedmodules { $ _ } + + for split_list ( $ config { DONT_LOAD } , 'module' ) ;
2007-11-16 00:24:54 +01:00
2007-04-01 21:30:09 +02:00
progress_message "Loading Modules..." ;
open LSMOD , '-|' , 'lsmod' or fatal_error "Can't run lsmod" ;
2007-12-01 18:14:08 +01:00
while ( <LSMOD> ) {
my $ module = ( split ( /\s+/ , $ _ , 2 ) ) [ 0 ] ;
$ loadedmodules { $ module } + + unless $ module eq 'Module'
2007-04-01 21:30:09 +02:00
}
2007-04-08 16:42:26 +02:00
2007-04-01 21:30:09 +02:00
close LSMOD ;
$ config { MODULE_SUFFIX } = 'o gz ko o.gz ko.gz' unless $ config { MODULES_SUFFIX } ;
my @ suffixes = split /\s+/ , $ config { MODULE_SUFFIX } ;
while ( read_a_line ) {
2007-07-07 18:34:38 +02:00
fatal_error "Invalid modules file entry" unless ( $ currentline =~ /^loadmodule\s+([a-zA-Z]\w*)\s*(.*)$/ ) ;
2007-04-01 21:30:09 +02:00
my ( $ module , $ arguments ) = ( $ 1 , $ 2 ) ;
unless ( $ loadedmodules { $ module } ) {
2007-04-02 19:47:05 +02:00
for my $ directory ( @ moduledirectories ) {
for my $ suffix ( @ suffixes ) {
2007-04-01 21:30:09 +02:00
my $ modulefile = "$directory/$module.$suffix" ;
if ( - f $ modulefile ) {
if ( $ moduleloader eq 'insmod' ) {
system ( "insmod $modulefile $arguments" ) ;
} else {
system ( "modprobe $module $arguments" ) ;
}
$ loadedmodules { $ module } = 1 ;
}
}
}
}
}
2007-04-08 16:42:26 +02:00
}
2007-04-01 21:30:09 +02:00
}
2007-04-02 19:47:05 +02:00
#
# Q[uie]t version of system(). Returns true for success
#
sub qt ( $ ) {
system ( "@_ > /dev/null 2>&1" ) == 0 ;
}
2007-11-26 23:34:36 +01:00
sub qt1 ( $ ) {
1 while system ( "@_ > /dev/null 2>&1" ) == 4 ;
$? == 0 ;
}
2007-04-01 18:42:49 +02:00
#
# Determine which optional facilities are supported by iptables/netfilter
#
2007-09-10 17:52:57 +02:00
sub determine_capabilities ( $ ) {
2007-04-08 16:42:26 +02:00
2007-09-10 17:52:57 +02:00
my $ iptables = $ _ [ 0 ] ;
2007-05-02 23:59:11 +02:00
my $ pid = $$ ;
my $ sillyname = "fooX$pid" ;
2007-04-01 18:42:49 +02:00
2007-11-26 23:34:36 +01:00
$ capabilities { NAT_ENABLED } = qt1 ( "$iptables -t nat -L -n" ) ;
$ capabilities { MANGLE_ENABLED } = qt1 ( "$iptables -t mangle -L -n" ) ;
2007-04-01 18:42:49 +02:00
2007-11-26 23:34:36 +01:00
qt1 ( "$iptables -N $sillyname" ) ;
2007-04-08 16:42:26 +02:00
2007-11-26 23:34:36 +01:00
$ capabilities { CONNTRACK_MATCH } = qt1 ( "$iptables -A $sillyname -m conntrack --ctorigdst 192.168.1.1 -j ACCEPT" ) ;
2007-09-21 18:55:28 +02:00
2007-11-26 23:34:36 +01:00
if ( qt1 ( "$iptables -A $sillyname -p tcp -m multiport --dports 21,22 -j ACCEPT" ) ) {
2007-09-21 18:55:28 +02:00
$ capabilities { MULTIPORT } = 1 ;
2007-11-26 23:34:36 +01:00
$ capabilities { KLUDGEFREE } = qt1 ( "$iptables -A $sillyname -p tcp -m multiport --sports 60 -m multiport --dports 99 -j ACCEPT" ) ;
2007-09-21 18:55:28 +02:00
}
2007-11-26 23:34:36 +01:00
$ capabilities { XMULTIPORT } = qt1 ( "$iptables -A $sillyname -p tcp -m multiport --dports 21:22 -j ACCEPT" ) ;
$ capabilities { POLICY_MATCH } = qt1 ( "$iptables -A $sillyname -m policy --pol ipsec --mode tunnel --dir in -j ACCEPT" ) ;
2007-09-21 18:55:28 +02:00
2007-11-26 23:34:36 +01:00
if ( qt1 ( "$iptables -A $sillyname -m physdev --physdev-in eth0 -j ACCEPT" ) ) {
2007-11-16 00:24:54 +01:00
$ capabilities { PHYSDEV_MATCH } = 1 ;
2007-11-26 23:34:36 +01:00
$ capabilities { PHYSDEV_BRIDGE } = qt1 ( "$iptables -A $sillyname -m physdev --physdev-is-bridged --physdev-in eth0 --physdev-out eth1 -j ACCEPT" ) ;
2007-09-21 18:55:28 +02:00
unless ( $ capabilities { KLUDGEFREE } ) {
2007-11-26 23:34:36 +01:00
$ capabilities { KLUDGEFREE } = qt1 ( "$iptables -A $sillyname -m physdev --physdev-in eth0 -m physdev --physdev-out eth0 -j ACCEPT" ) ;
2007-09-21 18:55:28 +02:00
}
}
2007-04-01 18:42:49 +02:00
2007-11-26 23:34:36 +01:00
if ( qt1 ( "$iptables -A $sillyname -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT" ) ) {
2007-04-01 18:42:49 +02:00
$ capabilities { IPRANGE_MATCH } = 1 ;
unless ( $ capabilities { KLUDGEFREE } ) {
2007-11-26 23:34:36 +01:00
$ capabilities { KLUDGEFREE } = qt1 ( "$iptables -A $sillyname -m iprange --src-range 192.168.1.5-192.168.1.124 -m iprange --dst-range 192.168.1.5-192.168.1.124 -j ACCEPT" ) ;
2007-04-01 18:42:49 +02:00
}
}
2007-11-26 23:34:36 +01:00
$ capabilities { RECENT_MATCH } = qt1 ( "$iptables -A $sillyname -m recent --update -j ACCEPT" ) ;
$ capabilities { OWNER_MATCH } = qt1 ( "$iptables -A $sillyname -m owner --uid-owner 0 -j ACCEPT" ) ;
2007-04-01 18:42:49 +02:00
2007-11-26 23:34:36 +01:00
if ( qt1 ( "$iptables -A $sillyname -m connmark --mark 2 -j ACCEPT" ) ) {
2007-04-01 18:42:49 +02:00
$ capabilities { CONNMARK_MATCH } = 1 ;
2007-11-26 23:34:36 +01:00
$ capabilities { XCONNMARK_MATCH } = qt1 ( "$iptables -A $sillyname -m connmark --mark 2/0xFF -j ACCEPT" ) ;
2007-04-01 18:42:49 +02:00
}
2007-04-08 16:42:26 +02:00
2007-11-26 23:34:36 +01:00
$ capabilities { IPP2P_MATCH } = qt1 ( "$iptables -A $sillyname -p tcp -m ipp2p --ipp2p -j ACCEPT" ) ;
$ capabilities { LENGTH_MATCH } = qt1 ( "$iptables -A $sillyname -m length --length 10:20 -j ACCEPT" ) ;
$ capabilities { ENHANCED_REJECT } = qt1 ( "$iptables -A $sillyname -j REJECT --reject-with icmp-host-prohibited" ) ;
$ capabilities { COMMENTS } = qt1 ( qq( $iptables -A $sillyname -j ACCEPT -m comment --comment "This is a comment" ) ) ;
2007-04-01 18:42:49 +02:00
if ( $ capabilities { MANGLE_ENABLED } ) {
2007-11-26 23:34:36 +01:00
qt1 ( "$iptables -t mangle -N $sillyname" ) ;
2007-04-01 18:42:49 +02:00
2007-11-26 23:34:36 +01:00
if ( qt1 ( "$iptables -t mangle -A $sillyname -j MARK --set-mark 1" ) ) {
2007-04-01 18:42:49 +02:00
$ capabilities { MARK } = 1 ;
2007-11-26 23:34:36 +01:00
$ capabilities { XMARK } = qt1 ( "$iptables -t mangle -A $sillyname -j MARK --and-mark 0xFF" ) ;
2007-04-01 18:42:49 +02:00
}
2007-11-26 23:34:36 +01:00
if ( qt1 ( "$iptables -t mangle -A $sillyname -j CONNMARK --save-mark" ) ) {
2007-04-01 18:42:49 +02:00
$ capabilities { CONNMARK } = 1 ;
2007-11-26 23:34:36 +01:00
$ capabilities { XCONNMARK } = qt1 ( "$iptables -t mangle -A $sillyname -j CONNMARK --save-mark --mask 0xFF" ) ;
2007-04-01 18:42:49 +02:00
}
2007-11-26 23:34:36 +01:00
$ capabilities { CLASSIFY_TARGET } = qt1 ( "$iptables -t mangle -A $sillyname -j CLASSIFY --set-class 1:1" ) ;
qt1 ( "$iptables -t mangle -F $sillyname" ) ;
qt1 ( "$iptables -t mangle -X $sillyname" ) ;
2007-04-01 18:42:49 +02:00
2007-11-26 23:34:36 +01:00
$ capabilities { MANGLE_FORWARD } = qt1 ( "$iptables -t mangle -L FORWARD -n" ) ;
2007-04-01 18:42:49 +02:00
}
2007-11-26 23:34:36 +01:00
$ capabilities { RAW_TABLE } = qt1 ( "$iptables -t raw -L -n" ) ;
2007-04-01 18:42:49 +02:00
2007-09-10 17:52:57 +02:00
if ( which 'ipset' ) {
2007-05-02 23:59:11 +02:00
qt ( "ipset -X $sillyname" ) ;
2007-04-01 18:42:49 +02:00
2007-05-02 23:59:11 +02:00
if ( qt ( "ipset -N $sillyname iphash" ) ) {
2007-11-26 23:34:36 +01:00
if ( qt1 ( "$iptables -A $sillyname -m set --set $sillyname src -j ACCEPT" ) ) {
qt1 ( "$iptables -D $sillyname -m set --set $sillyname src -j ACCEPT" ) ;
2007-04-01 18:42:49 +02:00
$ capabilities { IPSET_MATCH } = 1 ;
}
2007-05-02 23:59:11 +02:00
qt ( "ipset -X $sillyname" ) ;
2007-04-01 18:42:49 +02:00
}
}
2007-11-26 23:34:36 +01:00
$ capabilities { USEPKTTYPE } = qt1 ( "$iptables -A $sillyname -m pkttype --pkt-type broadcast -j ACCEPT" ) ;
$ capabilities { ADDRTYPE } = qt1 ( "$iptables -A $sillyname -m addrtype --src-type BROADCAST -j ACCEPT" ) ;
$ capabilities { TCPMSS_MATCH } = qt1 ( "$iptables -A $sillyname -p tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1000:1500 -j ACCEPT" ) ;
$ capabilities { HASHLIMIT_MATCH } = qt1 ( "$iptables -A $sillyname -m hashlimit --hashlimit 4 --hashlimit-burst 5 --hashlimit-name fooX1234 --hashlimit-mode dstip -j ACCEPT" ) ;
$ capabilities { NFQUEUE_TARGET } = qt1 ( "$iptables -A $sillyname -j NFQUEUE --queue-num 4" ) ;
2007-04-01 18:42:49 +02:00
2007-11-26 23:34:36 +01:00
$ capabilities { REALM_MATCH } = qt1 ( "$iptables -A $sillyname -m realm --realm 1" ) ;
2007-11-16 01:14:43 +01:00
2007-11-26 23:34:36 +01:00
qt1 ( "$iptables -F $sillyname" ) ;
qt1 ( "$iptables -X $sillyname" ) ;
2007-06-17 23:35:42 +02:00
$ capabilities { CAPVERSION } = $ globals { CAPVERSION } ;
2007-04-08 16:42:26 +02:00
}
2007-04-01 18:42:49 +02:00
2007-04-16 21:33:25 +02:00
#
# Require the passed capability
#
2007-05-04 17:12:29 +02:00
sub require_capability ( $$$ ) {
my ( $ capability , $ description , $ singular ) = @ _ ;
2007-03-30 05:30:28 +02:00
2007-05-04 17:12:29 +02:00
fatal_error "$description require${singular} $capdesc{$capability} in your kernel and iptables"
2007-03-30 05:30:28 +02:00
unless $ capabilities { $ capability } ;
}
2007-04-03 03:20:56 +02:00
#
# Set default config path
#
2007-05-16 05:37:37 +02:00
sub ensure_config_path () {
2007-04-03 03:20:56 +02:00
my $ f = "$globals{SHAREDIR}/configpath" ;
2007-05-16 05:37:37 +02:00
$ globals { CONFDIR } = '/usr/share/shorewall/configfiles/' if $> != 0 ;
2007-04-08 16:42:26 +02:00
2007-04-03 03:20:56 +02:00
unless ( $ config { CONFIG_PATH } ) {
fatal_error "$f does not exist" unless - f $ f ;
2007-04-08 16:42:26 +02:00
2007-04-07 19:14:55 +02:00
open_file $ f ;
2007-04-03 03:20:56 +02:00
2007-05-16 06:01:08 +02:00
$ ENV { CONFDIR } = $ globals { CONFDIR } ;
2007-04-03 03:20:56 +02:00
while ( read_a_line ) {
2007-07-07 18:34:38 +02:00
if ( $ currentline =~ /^\s*([a-zA-Z]\w*)=(.*?)\s*$/ ) {
2007-04-03 03:20:56 +02:00
my ( $ var , $ val ) = ( $ 1 , $ 2 ) ;
$ config { $ var } = ( $ val =~ /\"([^\"]*)\"$/ ? $ 1 : $ val ) if exists $ config { $ var } ;
} else {
fatal_error "Unrecognized entry" ;
}
}
2007-04-08 16:42:26 +02:00
2007-04-03 03:20:56 +02:00
fatal_error "CONFIG_PATH not found in $f" unless $ config { CONFIG_PATH } ;
}
@ config_path = split /:/ , $ config { CONFIG_PATH } ;
for ( @ config_path ) {
2007-06-19 15:48:16 +02:00
$ _ . = '/' unless m | / $| ;
2007-04-03 03:20:56 +02:00
}
2007-06-11 20:07:34 +02:00
if ( $ shorewall_dir ) {
2007-09-21 18:55:28 +02:00
$ shorewall_dir = getcwd if $ shorewall_dir =~ m | ^ ( \ . / * ) + $| ;
2007-06-19 15:48:16 +02:00
$ shorewall_dir . = '/' unless $ shorewall_dir =~ m | / $| ;
2007-06-11 20:07:34 +02:00
unshift @ config_path , $ shorewall_dir if $ shorewall_dir ne $ config_path [ 0 ] ;
2007-09-10 17:52:57 +02:00
$ config { CONFIG_PATH } = join ':' , @ config_path ;
2007-04-03 03:20:56 +02:00
}
}
2007-06-27 02:15:58 +02:00
#
# Set $shorewall_dir
#
sub set_shorewall_dir ( $ ) {
$ shorewall_dir = shift ;
ensure_config_path ;
}
2007-03-14 03:44:41 +01:00
#
2007-06-20 01:14:15 +02:00
# Small functions called by get_configuration. We separate them so profiling is more useful
2007-03-14 03:44:41 +01:00
#
2007-06-20 01:14:15 +02:00
sub process_shorewall_conf () {
2007-03-14 03:44:41 +01:00
my $ file = find_file 'shorewall.conf' ;
if ( - f $ file ) {
if ( - r _ ) {
2007-03-30 01:07:15 +02:00
open_file $ file ;
2007-03-14 03:44:41 +01:00
2007-03-29 06:22:10 +02:00
while ( read_a_line ) {
2007-07-07 18:34:38 +02:00
if ( $ currentline =~ /^\s*([a-zA-Z]\w*)=(.*?)\s*$/ ) {
2007-03-14 03:44:41 +01:00
my ( $ var , $ val ) = ( $ 1 , $ 2 ) ;
unless ( exists $ config { $ var } ) {
2007-04-03 03:20:56 +02:00
warning_message "Unknown configuration option ($var) ignored" ;
2007-03-14 03:44:41 +01:00
next ;
}
2007-03-31 01:21:08 +02:00
$ config { $ var } = ( $ val =~ /\"([^\"]*)\"$/ ? $ 1 : $ val ) ;
2007-03-14 03:44:41 +01:00
} else {
2007-04-03 03:20:56 +02:00
fatal_error "Unrecognized entry" ;
2007-03-14 03:44:41 +01:00
}
}
} else {
fatal_error "Cannot read $file (Hint: Are you root?)" ;
}
} else {
fatal_error "$file does not exist!" ;
}
2007-06-20 01:14:15 +02:00
}
2007-03-14 03:44:41 +01:00
2007-09-10 17:52:57 +02:00
#
# Process the records in the capabilities file
#
sub read_capabilities () {
2007-06-20 03:12:35 +02:00
while ( read_a_line1 ) {
2007-07-07 18:34:38 +02:00
if ( $ currentline =~ /^([a-zA-Z]\w*)=(.*)$/ ) {
2007-04-01 18:42:49 +02:00
my ( $ var , $ val ) = ( $ 1 , $ 2 ) ;
unless ( exists $ capabilities { $ var } ) {
2007-04-02 00:13:27 +02:00
warning_message "Unknown capability ($var) ignored" ;
2007-04-01 18:42:49 +02:00
next ;
}
2007-03-14 03:44:41 +01:00
2007-04-01 18:42:49 +02:00
$ capabilities { $ var } = $ val =~ /^\"([^\"]*)\"$/ ? $ 1 : $ val ;
} else {
2007-04-01 19:05:27 +02:00
fatal_error "Unrecognized capabilities entry" ;
2007-04-01 18:42:49 +02:00
}
}
2007-03-14 03:44:41 +01:00
2007-06-17 23:35:42 +02:00
if ( $ capabilities { CAPVERSION } ) {
warning_message "Your capabilities file is out of date -- it does not contain all of the capabilities defined by Shorewall version $globals{VERSION}" unless $ capabilities { CAPVERSION } >= $ globals { CAPVERSION } ;
} else {
2007-06-18 00:13:00 +02:00
warning_message "Your capabilities file may not contain all of the capabilities defined by Shorewall version $globals{VERSION}" ;
2007-06-17 23:35:42 +02:00
}
2007-06-20 01:14:15 +02:00
}
2007-09-10 17:52:57 +02:00
#
# Get the system's capabilities, either by probing or by reading a capabilities file
#
sub get_capabilities ( $ ) {
my $ export = $ _ [ 0 ] ;
if ( ! $ export && $> == 0 ) { # $> == $EUID
my $ iptables = $ config { IPTABLES } ;
if ( $ iptables ) {
fatal_error "IPTABLES=$iptables does not exist or is not executable" unless - x $ iptables ;
} else {
fatal_error "Can't find iptables executable" unless $ iptables = which 'iptables' ;
}
my $ iptables_restore = $ iptables . '-restore' ;
fatal_error "$iptables_restore does not exist or is not executable" unless - x $ iptables_restore ;
load_kernel_modules ;
if ( open_file 'capabilities' ) {
read_capabilities ;
} else {
determine_capabilities $ iptables ;
}
} else {
unless ( open_file 'capabilities' ) {
fatal_error "The -e compiler option requires a capabilities file" if $ export ;
fatal_error "Compiling under non-root uid requires a capabilities file" ;
}
read_capabilities ;
}
}
2007-06-20 01:14:15 +02:00
#
# - Read the shorewall.conf file
# - Read the capabilities file, if any
# - establish global hashes %config , %globals and %capabilities
#
sub get_configuration ( $ ) {
my $ export = $ _ [ 0 ] ;
2007-11-16 00:24:54 +01:00
our ( $ once , @ originalinc ) ;
@ originalinc = @ INC unless $ once + + ;
2007-06-20 01:14:15 +02:00
ensure_config_path ;
process_shorewall_conf ;
ensure_config_path ;
2007-11-16 00:24:54 +01:00
@ INC = @ originalinc ;
unshift @ INC , @ config_path ;
2007-06-20 01:14:15 +02:00
default 'PATH' , '/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin' ;
default 'MODULE_PREFIX' , 'o gz ko o.gz ko.gz' ;
get_capabilities ( $ export ) ;
2007-06-17 23:35:42 +02:00
2007-04-27 17:15:13 +02:00
$ globals { ORIGINAL_POLICY_MATCH } = $ capabilities { POLICY_MATCH } ;
2007-03-14 03:44:41 +01:00
if ( $ config { LOGRATE } || $ config { LOGBURST } ) {
2007-05-08 04:34:58 +02:00
$ globals { LOGLIMIT } = '-m limit ' ;
$ globals { LOGLIMIT } . = "--limit $config{LOGRATE} " if $ config { LOGRATE } ;
$ globals { LOGLIMIT } . = "--limit-burst $config{LOGBURST} " if $ config { LOGBURST } ;
2007-03-14 03:44:41 +01:00
} else {
2007-03-31 19:44:16 +02:00
$ globals { LOGLIMIT } = '' ;
2007-03-14 03:44:41 +01:00
}
2007-04-22 21:45:27 +02:00
check_trivalue ( 'IP_FORWARDING' , 'on' ) ;
check_trivalue ( 'ROUTE_FILTER' , '' ) ;
2008-02-13 21:05:18 +01:00
check_trivalue ( 'LOG_MARTIANS' , 'on' ) ;
2007-07-26 20:36:18 +02:00
2007-11-23 22:33:36 +01:00
default 'STARTUP_LOG' , '' ;
2007-12-07 17:32:01 +01:00
if ( $ config { STARTUP_LOG } ne '' ) {
if ( defined $ config { LOG_VERBOSITY } ) {
if ( $ config { LOG_VERBOSITY } eq '' ) {
$ config { LOG_VERBOSITY } = 2 ;
} else {
2007-12-07 20:06:01 +01:00
my $ val = numeric_value ( $ config { LOG_VERBOSITY } ) ;
fatal_error "Invalid LOG_VERBOSITY ($config{LOG_VERBOSITY} )" unless defined ( $ val ) && ( $ val >= - 1 ) && ( $ val <= 2 ) ;
2007-12-07 17:32:01 +01:00
$ config { STARTUP_LOG } = '' if $ config { LOG_VERBOSITY } < 0 ;
}
} else {
$ config { LOG_VERBOSITY } = 2 ;
}
} else {
$ config { LOG_VERBOSITY } = - 1 ;
}
2007-03-14 03:44:41 +01:00
default_yes_no 'ADD_IP_ALIASES' , 'Yes' ;
default_yes_no 'ADD_SNAT_ALIASES' , '' ;
default_yes_no 'DETECT_DNAT_IPADDRS' , '' ;
default_yes_no 'DETECT_DNAT_IPADDRS' , '' ;
default_yes_no 'CLEAR_TC' , 'Yes' ;
2007-06-11 20:07:34 +02:00
2007-04-23 21:49:02 +02:00
if ( defined $ config { CLAMPMSS } ) {
default_yes_no 'CLAMPMSS' , '' unless $ config { CLAMPMSS } =~ /^\d+$/ ;
} else {
$ config { CLAMPMSS } = '' ;
}
2007-03-14 03:44:41 +01:00
2007-03-23 22:24:28 +01:00
unless ( $ config { ADD_IP_ALIASES } || $ config { ADD_SNAT_ALIASES } ) {
2007-03-14 03:44:41 +01:00
$ config { RETAIN_ALIASES } = '' ;
} else {
default_yes_no 'RETAIN_ALIASES' , '' ;
}
default_yes_no 'ADMINISABSENTMINDED' , '' ;
default_yes_no 'BLACKLISTNEWONLY' , '' ;
default_yes_no 'DISABLE_IPV6' , '' ;
default_yes_no 'DYNAMIC_ZONES' , '' ;
2007-03-31 19:44:16 +02:00
fatal_error "DYNAMIC_ZONES=Yes is incompatible with the -e option" if $ config { DYNAMIC_ZONES } && $ export ;
2007-03-24 20:19:29 +01:00
default_yes_no 'BRIDGING' , '' ;
2007-03-31 19:44:16 +02:00
fatal_error 'BRIDGING=Yes is not supported by Shorewall-perl' . $ globals { VERSION } if $ config { BRIDGING } ;
2007-03-27 01:17:46 +02:00
2007-03-14 03:44:41 +01:00
default_yes_no 'STARTUP_ENABLED' , 'Yes' ;
default_yes_no 'DELAYBLACKLISTLOAD' , '' ;
2007-04-01 17:20:07 +02:00
warning_message 'DELAYBLACKLISTLOAD=Yes is not supported by Shorewall-perl ' . $ globals { VERSION } if $ config { DELAYBLACKLISTLOAD } ;
2007-07-02 23:17:47 +02:00
default_yes_no 'LOGTAGONLY' , '' ; $ globals { LOGTAGONLY } = $ config { LOGTAGONLY } ;
2007-03-14 03:44:41 +01:00
default_yes_no 'RFC1918_STRICT' , '' ;
default_yes_no 'SAVE_IPSETS' , '' ;
2007-03-24 18:16:13 +01:00
2007-03-31 19:44:16 +02:00
warning_message 'SAVE_IPSETS=Yes is not supported by Shorewall-perl ' . $ globals { VERSION } if $ config { SAVE_IPSETS } ;
2007-03-24 18:16:13 +01:00
2007-03-14 03:44:41 +01:00
default_yes_no 'MAPOLDACTIONS' , '' ;
2007-03-24 18:16:13 +01:00
2007-03-31 19:44:16 +02:00
warning_message 'MAPOLDACTIONS=Yes is not supported by Shorewall-perl ' . $ globals { VERSION } if $ config { MAPOLDACTIONS } ;
2007-03-24 18:16:13 +01:00
2007-03-14 03:44:41 +01:00
default_yes_no 'FASTACCEPT' , '' ;
2007-04-22 23:42:35 +02:00
fatal_error "BLACKLISTNEWONLY=No may not be specified with FASTACCEPT=Yes" if $ config { FASTACCEPT } && ! $ config { BLACKLISTNEWONLY } ;
2007-03-14 03:44:41 +01:00
default_yes_no 'IMPLICIT_CONTINUE' , '' ;
default_yes_no 'HIGH_ROUTE_MARKS' , '' ;
default_yes_no 'TC_EXPERT' , '' ;
default_yes_no 'USE_ACTIONS' , 'Yes' ;
2007-03-31 20:44:48 +02:00
warning_message 'USE_ACTIONS=No is not supported by Shorewall-perl ' . $ globals { VERSION } unless $ config { USE_ACTIONS } ;
2007-03-14 03:44:41 +01:00
default_yes_no 'EXPORTPARAMS' , '' ;
2007-07-19 20:36:04 +02:00
default_yes_no 'EXPAND_POLICIES' , '' ;
2007-08-26 17:12:04 +02:00
default_yes_no 'KEEP_RT_TABLES' , '' ;
2007-09-21 18:55:28 +02:00
default_yes_no 'DELETE_THEN_ADD' , 'Yes' ;
2008-02-23 02:46:50 +01:00
default_yes_no 'AUTO_COMMENT' , 'Yes' ;
2007-10-19 21:43:14 +02:00
default_yes_no 'MULTICAST' , '' ;
2007-03-25 18:38:00 +02:00
default_yes_no 'MARK_IN_FORWARD_CHAIN' , '' ;
2008-03-24 23:36:48 +01:00
default_yes_no 'MANGLE_ENABLED' , 'Yes' ;
2008-03-29 01:35:00 +01:00
default_yes_no 'NULL_ROUTE_RFC1918' , '' ;
2007-09-21 18:55:28 +02:00
2007-03-14 03:44:41 +01:00
$ capabilities { XCONNMARK } = '' unless $ capabilities { XCONNMARK_MATCH } and $ capabilities { XMARK } ;
default 'BLACKLIST_DISPOSITION' , 'DROP' ;
2007-03-27 01:17:46 +02:00
2007-04-24 16:53:45 +02:00
default_log_level 'BLACKLIST_LOGLEVEL' , '' ;
default_log_level 'MACLIST_LOG_LEVEL' , '' ;
default_log_level 'TCP_FLAGS_LOG_LEVEL' , '' ;
2007-04-24 23:27:40 +02:00
default_log_level 'RFC1918_LOG_LEVEL' , 6 ;
2007-04-24 16:53:45 +02:00
default_log_level 'SMURF_LOG_LEVEL' , '' ;
default_log_level 'LOGALLNEW' , '' ;
2007-03-14 03:44:41 +01:00
my $ val ;
2007-03-31 19:44:16 +02:00
$ globals { MACLIST_TARGET } = 'reject' ;
2007-03-14 03:44:41 +01:00
if ( $ val = $ config { MACLIST_DISPOSITION } ) {
unless ( $ val eq 'REJECT' ) {
if ( $ val eq 'DROP' ) {
2007-03-31 19:44:16 +02:00
$ globals { MACLIST_TARGET } = 'DROP' ;
2007-03-14 03:44:41 +01:00
} elsif ( $ val eq 'ACCEPT' ) {
2007-03-31 19:44:16 +02:00
$ globals { MACLIST_TARGET } = 'RETURN' ;
2007-03-14 03:44:41 +01:00
} else {
2007-06-16 23:08:12 +02:00
fatal_error "Invalid value ($config{MACLIST_DISPOSITION}) for MACLIST_DISPOSITION"
2007-03-14 03:44:41 +01:00
}
}
} else {
$ config { MACLIST_DISPOSITION } = 'REJECT' ;
}
2007-03-27 01:17:46 +02:00
2007-03-14 03:44:41 +01:00
if ( $ val = $ config { MACLIST_TABLE } ) {
if ( $ val eq 'mangle' ) {
fatal_error 'MACLIST_DISPOSITION=REJECT is not allowed with MACLIST_TABLE=mangle' if $ config { MACLIST_DISPOSITION } eq 'REJECT' ;
} else {
fatal_error "Invalid value ($val) for MACLIST_TABLE option" unless $ val eq 'filter' ;
}
2007-04-08 16:42:26 +02:00
} else {
2007-03-14 03:44:41 +01:00
default 'MACLIST_TABLE' , 'filter' ;
}
if ( $ val = $ config { TCP_FLAGS_DISPOSITION } ) {
fatal_error "Invalid value ($config{TCP_FLAGS_DISPOSITION}) for TCP_FLAGS_DISPOSITION" unless $ val =~ /^(REJECT|ACCEPT|DROP)$/ ;
} else {
$ config { TCP_FLAGS_DISPOSITION } = 'DROP' ;
}
2007-03-27 01:17:46 +02:00
2007-04-23 21:49:02 +02:00
default 'TC_ENABLED' , 'Internal' ;
2007-04-24 01:17:30 +02:00
$ val = "\L$config{TC_ENABLED}" ;
if ( $ val eq 'yes' ) {
2007-06-20 01:14:15 +02:00
my $ file = find_file 'tcstart' ;
2007-04-24 01:17:30 +02:00
fatal_error "Unable to find tcstart file" unless - f $ file ;
$ globals { TC_SCRIPT } = $ file ;
2007-09-21 18:55:28 +02:00
} elsif ( $ val eq 'internal' ) {
$ config { TC_ENABLED } = 'Internal' ;
} else {
2007-04-24 01:17:30 +02:00
fatal_error "Invalid value ($config{TC_ENABLED}) for TC_ENABLED" unless $ val eq 'no' ;
$ config { TC_ENABLED } = '' ;
2007-03-14 03:44:41 +01:00
}
2008-03-24 23:36:48 +01:00
fatal_error "TC_ENABLED=$config{TC_ENABLED} is not allowed with MANGLE_ENABLED=No" if $ config { TC_ENABLED } && ! $ config { MANGLE_ENABLED } ;
2007-03-14 03:44:41 +01:00
default 'RESTOREFILE' , 'restore' ;
2007-08-26 17:12:04 +02:00
default 'IPSECFILE' , 'zones' ;
2007-03-14 03:44:41 +01:00
default 'DROP_DEFAULT' , 'Drop' ;
default 'REJECT_DEFAULT' , 'Reject' ;
default 'QUEUE_DEFAULT' , 'none' ;
2007-08-26 17:12:04 +02:00
default 'NFQUEUE_DEFAULT' , 'none' ;
2007-03-14 03:44:41 +01:00
default 'ACCEPT_DEFAULT' , 'none' ;
default 'OPTIMIZE' , 0 ;
2007-03-27 01:17:46 +02:00
2007-04-01 17:38:05 +02:00
fatal_error 'IPSECFILE=ipsec is not supported by Shorewall-perl ' . $ globals { VERSION } unless $ config { IPSECFILE } eq 'zones' ;
2007-04-01 17:20:07 +02:00
2007-08-26 17:12:04 +02:00
for my $ default qw/DROP_DEFAULT REJECT_DEFAULT QUEUE_DEFAULT NFQUEUE_DEFAULT ACCEPT_DEFAULT/ {
2007-03-14 03:44:41 +01:00
$ config { $ default } = 'none' if "\L$config{$default}" eq 'none' ;
}
$ val = $ config { OPTIMIZE } ;
fatal_error "Invalid OPTIMIZE value ($val)" unless ( $ val eq '0' ) || ( $ val eq '1' ) ;
fatal_error "Invalid IPSECFILE value ($config{IPSECFILE}" unless $ config { IPSECFILE } eq 'zones' ;
2007-03-31 19:44:16 +02:00
$ globals { MARKING_CHAIN } = $ config { MARK_IN_FORWARD_CHAIN } ? 'tcfor' : 'tcpre' ;
2007-03-14 03:44:41 +01:00
if ( $ val = $ config { LOGFORMAT } ) {
my $ result ;
eval {
if ( $ val =~ /%d/ ) {
2007-03-31 19:44:16 +02:00
$ globals { LOGRULENUMBERS } = 'Yes' ;
2007-03-14 03:44:41 +01:00
$ result = sprintf "$val" , 'fooxx2barxx' , 1 , 'ACCEPT' ;
} else {
$ result = sprintf "$val" , 'fooxx2barxx' , 'ACCEPT' ;
}
} ;
fatal_error "Invalid LOGFORMAT ($val)" if $@ ;
2007-03-27 01:17:46 +02:00
2007-06-16 23:08:12 +02:00
fatal_error "LOGFORMAT string is longer than 29 characters ($val)" if length $ result > 29 ;
2007-03-14 03:44:41 +01:00
2007-03-31 19:44:16 +02:00
$ globals { MAXZONENAMELENGTH } = int ( 5 + ( ( 29 - ( length $ result ) ) / 2 ) ) ;
2007-03-14 03:44:41 +01:00
} else {
2007-04-23 21:49:02 +02:00
$ config { LOGFORMAT } = 'Shorewall:%s:%s:' ;
2007-03-31 19:44:16 +02:00
$ globals { MAXZONENAMELENGTH } = 5 ;
2007-03-14 03:44:41 +01:00
}
2007-04-16 01:41:13 +02:00
if ( $ config { LOCKFILE } ) {
my ( $ file , $ dir , $ suffix ) ;
eval {
( $ file , $ dir , $ suffix ) = fileparse ( $ config { LOCKFILE } ) ;
} ;
die $@ if $@ ;
fatal_error "LOCKFILE=$config{LOCKFILE}: Directory $dir does not exist" unless - d $ dir ;
} else {
$ config { LOCKFILE } = '' ;
}
2007-03-14 03:44:41 +01:00
}
2007-04-16 21:33:25 +02:00
#
2007-06-05 18:49:13 +02:00
# The values of the options in @propagateconfig are copied to the object file in OPTION=<value> format.
2007-04-16 21:33:25 +02:00
#
2007-03-14 03:44:41 +01:00
sub propagateconfig () {
2007-06-05 18:49:13 +02:00
for my $ option ( @ propagateconfig ) {
2007-03-14 03:44:41 +01:00
my $ value = $ config { $ option } || '' ;
emit "$option=\"$value\"" ;
}
2007-03-27 01:17:46 +02:00
2007-06-05 18:49:13 +02:00
for my $ option ( @ propagateenv ) {
2007-03-31 19:44:16 +02:00
my $ value = $ globals { $ option } || '' ;
2007-03-14 03:44:41 +01:00
emit "$option=\"$value\"" ;
}
}
2007-04-16 21:33:25 +02:00
#
2007-06-03 23:44:17 +02:00
# Add a shell script file to the output script -- Return true if the
# file exists and is not in /usr/share/shorewall/.
2007-04-16 21:33:25 +02:00
#
2007-03-14 05:06:32 +01:00
sub append_file ( $ ) {
my $ user_exit = find_file $ _ [ 0 ] ;
2007-06-03 23:44:17 +02:00
my $ result = 0 ;
2007-03-14 05:06:32 +01:00
2007-06-06 00:13:58 +02:00
unless ( $ user_exit =~ /^($globals{SHAREDIR})/ ) {
2007-03-14 05:06:32 +01:00
if ( - f $ user_exit ) {
2007-06-03 23:44:17 +02:00
$ result = 1 ;
2007-03-14 05:06:32 +01:00
save_progress_message "Processing $user_exit ..." ;
copy1 $ user_exit ;
}
2007-03-27 01:17:46 +02:00
}
2007-06-03 23:44:17 +02:00
$ result ;
2007-03-14 05:06:32 +01:00
}
2007-03-23 00:06:16 +01:00
#
# Run a Perl extension script
#
sub run_user_exit ( $ ) {
my $ chainref = $ _ [ 0 ] ;
my $ file = find_file $ chainref - > { name } ;
if ( - f $ file ) {
progress_message "Processing $file..." ;
2007-11-16 00:24:54 +01:00
my $ command = qq( package Shorewall::User;\nno strict;\n# line 1 "$file"\n ) . `cat $file` ;
unless ( my $ return = eval $ command ) {
2007-03-23 00:06:16 +01:00
fatal_error "Couldn't parse $file: $@" if $@ ;
2007-11-16 00:24:54 +01:00
unless ( defined $ return ) {
fatal_error "Couldn't do $file: $!" if $! ;
fatal_error "Couldn't do $file" ;
}
fatal_error "$file returned a false value" ;
2007-03-23 00:06:16 +01:00
}
}
}
2007-05-20 18:25:53 +02:00
sub run_user_exit1 ( $ ) {
my $ file = find_file $ _ [ 0 ] ;
if ( - f $ file ) {
progress_message "Processing $file..." ;
#
# File may be empty -- in which case eval would fail
#
push_open $ file ;
if ( read_a_line ) {
close_file ;
2007-11-16 00:24:54 +01:00
my $ command = qq( package Shorewall::User;\n# line 1 "$file"\n ) . `cat $file` ;
unless ( my $ return = eval $ command ) {
2007-05-20 18:25:53 +02:00
fatal_error "Couldn't parse $file: $@" if $@ ;
2007-11-16 00:24:54 +01:00
unless ( defined $ return ) {
fatal_error "Couldn't do $file: $!" if $! ;
fatal_error "Couldn't do $file" ;
}
fatal_error "$file returned a false value" ;
2007-05-20 18:25:53 +02:00
}
2007-11-16 00:24:54 +01:00
} else {
pop_open ;
2007-05-20 18:25:53 +02:00
}
2007-05-20 18:37:09 +02:00
}
}
sub run_user_exit2 ( $$ ) {
my ( $ file , $ chainref ) = ( find_file $ _ [ 0 ] , $ _ [ 1 ] ) ;
if ( - f $ file ) {
progress_message "Processing $file..." ;
#
# File may be empty -- in which case eval would fail
#
push_open $ file ;
if ( read_a_line ) {
close_file ;
unless ( my $ return = eval `cat $file` ) {
fatal_error "Couldn't parse $file: $@" if $@ ;
2007-11-16 00:24:54 +01:00
unless ( defined $ return ) {
fatal_error "Couldn't do $file: $!" if $! ;
fatal_error "Couldn't do $file" ;
}
fatal_error "$file returned a false value" ;
2007-05-20 18:37:09 +02:00
}
}
pop_open ;
2007-07-26 20:36:18 +02:00
2007-05-20 18:25:53 +02:00
}
}
2007-04-16 21:33:25 +02:00
#
# Generate the aux config file for Shorewall Lite
#
2007-03-17 19:18:54 +01:00
sub generate_aux_config () {
sub conditionally_add_option ( $ ) {
my $ option = $ _ [ 0 ] ;
my $ value = $ config { $ option } ;
2007-04-14 00:37:23 +02:00
emit "[ -n \"\${$option:=$value}\" ]" if $ value ne '' ;
2007-03-17 19:18:54 +01:00
}
sub conditionally_add_option1 ( $ ) {
my $ option = $ _ [ 0 ] ;
my $ value = $ config { $ option } ;
emit "$option=\"$value\"" if $ value ;
}
create_temp_aux_config ;
2008-01-05 00:20:09 +01:00
my $ date = localtime ;
emit "#\n# Shorewall auxiliary configuration file created by Shorewall-perl version $globals{VERSION} - $date\n#" ;
2007-03-27 01:17:46 +02:00
2007-04-16 01:41:13 +02:00
for my $ option qw( VERBOSITY LOGFILE LOGFORMAT IPTABLES PATH SHOREWALL_SHELL SUBSYSLOCK LOCKFILE RESTOREFILE SAVE_IPSETS ) {
2007-03-17 19:18:54 +01:00
conditionally_add_option $ option ;
}
conditionally_add_option1 'TC_ENABLED' ;
finalize_aux_config ;
}
2007-07-17 00:07:50 +02:00
END {
2007-11-16 00:24:54 +01:00
#
# Close files first in case we're running under Cygwin
#
close $ object if $ object ;
close $ scriptfile if $ scriptfile ;
2007-11-23 22:33:36 +01:00
close $ log if $ log ;
2007-11-16 00:24:54 +01:00
#
# Unlink temporary files
#
unlink $ tempfile if $ tempfile ;
unlink $ scriptfilename if $ scriptfilename ;
unlink $ _ for @ tempfiles ;
2007-07-17 00:07:50 +02:00
}
2007-03-17 19:18:54 +01:00
1 ;