Skip to content

Commit

Permalink
Don't require root to simulate commands.
Browse files Browse the repository at this point in the history
If running as a simulation, the script will use `/usr/bin/env iptables` as the IPTables command.

Closes #7.
  • Loading branch information
abenson committed Nov 23, 2016
1 parent 3c609dc commit f778a42
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions firewall
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ ALLOWALL="0"
DENYALL="0"
SHOWRULES="0"
PRINTCMD="echo "
SCRIPTGEN="0"
DEFTRUST="/etc/trusted.hosts"
DEFTARGS="/etc/target.hosts"
DEFEXCLD="/etc/exclude.hosts"

IPTABLES=`which iptables 2>/dev/null`

version()
{
echo "`basename $0` v$VERSION; Copyright (c) 2016, Andrew C. Benson"
Expand Down Expand Up @@ -126,23 +129,6 @@ help_and_quit()
exit
}

# You must be root (uid=0) to set iptables rules.
if [ `id -u` != "0" ]; then
echo "You must be root to run this command."
echo
help_and_quit
fi

IPTABLES=`which iptables 2>/dev/null`

# We want to make sure iptables is available before we attempt to create
# the rules.

if [ -z $IPTABLES ]; then
echo "Unable to find \`iptables\` in path."
exit
fi

while [ ! -z "$1" ]; do
case "$1" in
"-h" )
Expand Down Expand Up @@ -189,7 +175,8 @@ while [ ! -z "$1" ]; do
"-tt")
AUTOTRUST="1" ;;
"-s" )
IPTABLES="echo $IPTABLES"
SCRIPTGEN="1"
IPTABLES="echo /usr/bin/env iptables"
PRINTCMD="echo # " ;;
"-q" )
PRINTCMD="" ;;
Expand All @@ -200,6 +187,22 @@ while [ ! -z "$1" ]; do
shift
done

# You must be root (uid=0) to set iptables rules.

if [ `id -u` != "0" ] && [ "$SCRIPTGEN" -eq 0 ] ; then
echo "You must be root to run this command."
echo
help_and_quit
fi

# We want to make sure iptables is available before we attempt to create
# the rules.

if [ -z "$IPTABLES" ] && [ "$SCRIPTGEN" -eq 0 ] ; then
echo "Unable to find \`iptables\` in path."
exit
fi

# Handy wrapper to clear the rules.
flush_rules()
{
Expand Down

0 comments on commit f778a42

Please sign in to comment.