2002-07-24 17:03:06 +02:00
|
|
|
#!/bin/sh
|
2002-05-01 01:13:15 +02:00
|
|
|
#
|
2003-02-15 01:09:44 +01:00
|
|
|
# Shorewall 1.4 -- /usr/lib/shorewall/functions
|
2002-05-01 01:13:15 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Suppress all output for a command
|
2003-02-23 15:10:37 +01:00
|
|
|
#
|
|
|
|
qt()
|
|
|
|
{
|
2002-05-01 01:13:15 +02:00
|
|
|
"$@" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find a File -- Look first in $SHOREWALL_DIR then in /etc/shorewall
|
|
|
|
#
|
|
|
|
find_file()
|
|
|
|
{
|
|
|
|
if [ -n "$SHOREWALL_DIR" -a -f $SHOREWALL_DIR/$1 ]; then
|
|
|
|
echo $SHOREWALL_DIR/$1
|
|
|
|
else
|
|
|
|
echo /etc/shorewall/$1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Replace commas with spaces and echo the result
|
|
|
|
#
|
2003-02-23 15:10:37 +01:00
|
|
|
separate_list() {
|
2002-12-06 01:31:49 +01:00
|
|
|
local list
|
|
|
|
local part
|
|
|
|
local newlist
|
2003-02-23 15:10:37 +01:00
|
|
|
#
|
|
|
|
# There's been whining about us not catching embedded white space in
|
|
|
|
# comma-separated lists. This is an attempt to snag some of the cases.
|
|
|
|
#
|
|
|
|
# The 'terminator' function will be set by the 'firewall' script to
|
|
|
|
# either 'startup_error' or 'fatal_error' depending on the command and
|
|
|
|
# command phase
|
|
|
|
#
|
|
|
|
case "$@" in
|
2003-02-23 16:56:45 +01:00
|
|
|
*,|,*|*,,*|*[[:space:]]*)
|
2003-02-23 15:10:37 +01:00
|
|
|
[ -n "$terminator" ] && \
|
|
|
|
$terminator "Invalid comma-separated list \"$@\""
|
|
|
|
echo "Warning -- invalid comma-separated list \"$@\"" >&2
|
|
|
|
;;
|
|
|
|
esac
|
2002-12-06 01:31:49 +01:00
|
|
|
|
|
|
|
list="$@"
|
|
|
|
part="${list%%,*}"
|
|
|
|
newlist="$part"
|
2003-02-23 15:10:37 +01:00
|
|
|
|
2002-12-06 01:31:49 +01:00
|
|
|
while [ "x$part" != "x$list" ]; do
|
|
|
|
list="${list#*,}";
|
|
|
|
part="${list%%,*}";
|
|
|
|
newlist="$newlist $part";
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "$newlist"
|
2002-05-01 01:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Find the zones
|
|
|
|
#
|
|
|
|
find_zones() # $1 = name of the zone file
|
|
|
|
{
|
|
|
|
while read zone display comments; do
|
|
|
|
[ -n "$zone" ] && case "$zone" in
|
|
|
|
\#*)
|
|
|
|
;;
|
|
|
|
$FW|multi)
|
|
|
|
echo "Reserved zone name \"$zone\" in zones file ignored" >&2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo $zone
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done < $1
|
|
|
|
}
|
|
|
|
|
|
|
|
find_display() # $1 = zone, $2 = name of the zone file
|
|
|
|
{
|
|
|
|
grep ^$1 $2 | while read z display comments; do
|
|
|
|
[ "x$1" = "x$z" ] && echo $display
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2003-02-23 15:10:37 +01:00
|
|
|
determine_zones()
|
2002-05-01 01:13:15 +02:00
|
|
|
{
|
|
|
|
local zonefile=`find_file zones`
|
|
|
|
|
|
|
|
multi_display=Multi-zone
|
|
|
|
|
2003-06-14 03:22:46 +02:00
|
|
|
[ -z "$TMP_DIR" ] && TMP_DIR=/tmp
|
|
|
|
strip_file zones $zonefile
|
|
|
|
zones=`find_zones $TMP_DIR/zones`
|
|
|
|
zones=`echo $zones` # Remove extra trash
|
|
|
|
|
|
|
|
for zone in $zones; do
|
|
|
|
dsply=`find_display $zone $TMP_DIR/zones`
|
|
|
|
eval ${zone}_display=\$dsply
|
|
|
|
done
|
2002-05-01 01:13:15 +02:00
|
|
|
|
2003-06-14 03:22:46 +02:00
|
|
|
rm -f $TMP_DIR/zones
|
2002-05-01 01:13:15 +02:00
|
|
|
}
|
|
|
|
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
# The following functions may be used by apps that wish to ensure that
|
|
|
|
# the state of Shorewall isn't changing
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
# This function loads the STATEDIR variable (directory where Shorewall is to
|
|
|
|
# store state files). If your application supports alternate Shorewall
|
|
|
|
# configurations then the name of the alternate configuration directory should
|
|
|
|
# be in $SHOREWALL_DIR at the time of the call.
|
|
|
|
#
|
|
|
|
# If the shorewall.conf file does not exist, this function does not return
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
get_statedir()
|
|
|
|
{
|
2002-07-24 05:47:34 +02:00
|
|
|
MUTEX_TIMEOUT=
|
|
|
|
|
2002-05-01 01:13:15 +02:00
|
|
|
local config=`find_file shorewall.conf`
|
|
|
|
|
|
|
|
if [ -f $config ]; then
|
|
|
|
. $config
|
|
|
|
else
|
|
|
|
echo "/etc/shorewall/shorewall.conf does not exist!" >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ -z "${STATEDIR}" ] && STATEDIR=/var/state/shorewall
|
|
|
|
}
|
|
|
|
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
# Call this function to assert MUTEX with Shorewall. If you invoke the
|
|
|
|
# /sbin/shorewall program while holding MUTEX, you should pass "nolock" as
|
|
|
|
# the first argument. Example "shorewall nolock refresh"
|
|
|
|
#
|
|
|
|
# This function uses the lockfile utility from procmail if it exists.
|
|
|
|
# Otherwise, it uses a somewhat race-prone algorithm to attempt to simulate the
|
|
|
|
# behavior of lockfile.
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
mutex_on()
|
|
|
|
{
|
|
|
|
local try=0
|
|
|
|
local lockf=$STATEDIR/lock
|
|
|
|
|
2002-07-24 05:47:34 +02:00
|
|
|
MUTEX_TIMEOUT=${MUTEX_TIMEOUT:-60}
|
|
|
|
|
2002-07-24 17:03:06 +02:00
|
|
|
if [ $MUTEX_TIMEOUT -gt 0 ]; then
|
2002-07-24 05:47:34 +02:00
|
|
|
|
2002-07-24 17:03:06 +02:00
|
|
|
[ -d $STATEDIR ] || mkdir -p $STATEDIR
|
2002-05-01 01:13:15 +02:00
|
|
|
|
2002-07-24 17:03:06 +02:00
|
|
|
if qt which lockfile; then
|
|
|
|
lockfile -${MUTEX_TIMEOUT} -r1 ${lockf}
|
|
|
|
else
|
|
|
|
while [ -f ${lockf} -a ${try} -lt ${MUTEX_TIMEOUT} ] ; do
|
|
|
|
sleep 1
|
|
|
|
try=$((${try} + 1))
|
2002-05-01 01:13:15 +02:00
|
|
|
done
|
|
|
|
|
2002-07-24 17:03:06 +02:00
|
|
|
if [ ${try} -lt ${MUTEX_TIMEOUT} ] ; then
|
|
|
|
# Create the lockfile
|
|
|
|
echo $$ > ${lockf}
|
|
|
|
else
|
|
|
|
echo "Giving up on lock file ${lockf}" >&2
|
|
|
|
fi
|
2002-05-01 01:13:15 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
# Call this function to release MUTEX
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
mutex_off()
|
|
|
|
{
|
|
|
|
rm -f $STATEDIR/lock
|
|
|
|
}
|
|
|
|
|
2003-03-29 15:37:50 +01:00
|
|
|
#
|
|
|
|
# Read a file and handle "INCLUDE" directives
|
|
|
|
#
|
|
|
|
|
2003-03-29 16:15:05 +01:00
|
|
|
read_file() # $1 = file name, $2 = nest count
|
2003-03-29 15:37:50 +01:00
|
|
|
{
|
|
|
|
local first rest
|
|
|
|
|
|
|
|
while read first rest; do
|
|
|
|
if [ "x$first" = "xINCLUDE" ]; then
|
2003-03-29 16:15:05 +01:00
|
|
|
if [ $2 -lt 4 ]; then
|
2003-04-07 23:20:46 +02:00
|
|
|
read_file `find_file ${rest%#*}` $(($2 + 1))
|
2003-03-29 16:15:05 +01:00
|
|
|
else
|
|
|
|
echo " WARNING: INCLUDE in $1 ignored (nested too deeply)" >&2
|
|
|
|
fi
|
2003-03-29 15:37:50 +01:00
|
|
|
else
|
|
|
|
echo "$first $rest"
|
|
|
|
fi
|
|
|
|
done < $1
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Function for including one file into another
|
|
|
|
#
|
|
|
|
INCLUDE() {
|
|
|
|
. `find_file $@`
|
|
|
|
}
|
|
|
|
|
2002-10-23 18:48:40 +02:00
|
|
|
#
|
|
|
|
# Strip comments and blank lines from a file and place the result in the
|
|
|
|
# temporary directory
|
|
|
|
#
|
2002-05-01 01:13:15 +02:00
|
|
|
strip_file() # $1 = Base Name of the file, $2 = Full Name of File (optional)
|
|
|
|
{
|
|
|
|
local fname
|
|
|
|
|
|
|
|
[ $# = 1 ] && fname=`find_file $1` || fname=$2
|
|
|
|
|
|
|
|
if [ -f $fname ]; then
|
2003-03-29 16:15:05 +01:00
|
|
|
read_file $fname 0 | cut -d'#' -f1 | grep -v '^[[:space:]]*$' > $TMP_DIR/$1
|
2002-05-01 01:13:15 +02:00
|
|
|
else
|
|
|
|
> $TMP_DIR/$1
|
|
|
|
fi
|
|
|
|
}
|