087bff
#!/bin/bash
087bff
#
087bff
# iptables	Start iptables firewall
087bff
#
087bff
# chkconfig: 2345 08 92
087bff
# description:	Starts, stops and saves iptables firewall
087bff
#
087bff
# config: /etc/sysconfig/iptables
087bff
# config: /etc/sysconfig/iptables-config
087bff
#
087bff
### BEGIN INIT INFO
087bff
# Provides: iptables
087bff
# Required-Start:
087bff
# Required-Stop:
087bff
# Default-Start: 2 3 4 5
087bff
# Default-Stop: 0 1 6
087bff
# Short-Description: start and stop iptables firewall
087bff
# Description: Start, stop and save iptables firewall
087bff
### END INIT INFO
087bff
087bff
# compat for removed initscripts dependency
087bff
087bff
success() {
087bff
	echo -n "[  OK  ]"
087bff
	return 0
087bff
}
087bff
087bff
warning() {
087bff
	echo -n "[WARNING]"
087bff
	return 1
087bff
}
087bff
087bff
failure() {
087bff
	echo -n "[FAILED]"
087bff
	return 1
087bff
}
087bff
087bff
IPTABLES=iptables
087bff
IPTABLES_DATA=/etc/sysconfig/$IPTABLES
087bff
IPTABLES_FALLBACK_DATA=${IPTABLES_DATA}.fallback
087bff
IPTABLES_CONFIG=/etc/sysconfig/${IPTABLES}-config
087bff
IPV=${IPTABLES%tables} # ip for ipv4 | ip6 for ipv6
087bff
[ "$IPV" = "ip" ] && _IPV="ipv4" || _IPV="ipv6"
087bff
PROC_IPTABLES_NAMES=/proc/net/${IPV}_tables_names
087bff
VAR_SUBSYS_IPTABLES=/var/lock/subsys/$IPTABLES
087bff
087bff
# only usable for root
087bff
if [ $EUID != 0 ]; then
087bff
    echo -n $"${IPTABLES}: Only usable by root."; warning; echo
087bff
    exit 4
087bff
fi
087bff
087bff
if [ ! -x /sbin/$IPTABLES ]; then
087bff
    echo -n $"${IPTABLES}: /sbin/$IPTABLES does not exist."; warning; echo
087bff
    exit 5
087bff
fi
087bff
087bff
# Default firewall configuration:
087bff
IPTABLES_MODULES=""
087bff
IPTABLES_SAVE_ON_STOP="no"
087bff
IPTABLES_SAVE_ON_RESTART="no"
087bff
IPTABLES_SAVE_COUNTER="no"
087bff
IPTABLES_STATUS_NUMERIC="yes"
087bff
IPTABLES_STATUS_VERBOSE="no"
087bff
IPTABLES_STATUS_LINENUMBERS="yes"
087bff
IPTABLES_SYSCTL_LOAD_LIST=""
087bff
IPTABLES_RESTORE_WAIT=600
087bff
IPTABLES_RESTORE_WAIT_INTERVAL=1000000
087bff
087bff
# Load firewall configuration.
087bff
[ -f "$IPTABLES_CONFIG" ] && . "$IPTABLES_CONFIG"
087bff
087bff
is_iptables_nft() {
087bff
	iptables --version | grep -q '(nf_tables)'
087bff
}
087bff
087bff
netfilter_active() {
087bff
	is_iptables_nft && return 0
087bff
	[ -e "$PROC_IPTABLES_NAMES" ]
087bff
}
087bff
087bff
netfilter_tables() {
087bff
	netfilter_active || return 1
087bff
	is_iptables_nft && {
087bff
		# explicitly omit security table from this list as
087bff
		# it should be reserved for SELinux use
087bff
		echo "raw mangle filter nat"
087bff
		return 0
087bff
	}
087bff
	cat "$PROC_IPTABLES_NAMES" 2>/dev/null
087bff
}
087bff
087bff
# Get active tables
087bff
NF_TABLES=$(netfilter_tables)
087bff
087bff
087bff
flush_n_delete() {
087bff
    # Flush firewall rules and delete chains.
087bff
    netfilter_active || return 0
087bff
087bff
    # Check if firewall is configured (has tables)
087bff
    [ -z "$NF_TABLES" ] && return 1
087bff
087bff
    echo -n $"${IPTABLES}: Flushing firewall rules: "
087bff
    ret=0
087bff
    # For all tables
087bff
    for i in $NF_TABLES; do
087bff
        # Flush firewall rules.
087bff
	$IPTABLES -t $i -F;
087bff
	let ret+=$?;
087bff
087bff
        # Delete firewall chains.
087bff
	$IPTABLES -t $i -X;
087bff
	let ret+=$?;
087bff
087bff
	# Set counter to zero.
087bff
	$IPTABLES -t $i -Z;
087bff
	let ret+=$?;
087bff
    done
087bff
087bff
    [ $ret -eq 0 ] && success || failure
087bff
    echo
087bff
    return $ret
087bff
}
087bff
087bff
set_policy() {
087bff
    # Set policy for configured tables.
087bff
    policy=$1
087bff
087bff
    # Check if iptable module is loaded
087bff
    netfilter_active || return 0
087bff
087bff
    # Check if firewall is configured (has tables)
087bff
    tables=$(netfilter_tables)
087bff
    [ -z "$tables" ] && return 1
087bff
087bff
    echo -n $"${IPTABLES}: Setting chains to policy $policy: "
087bff
    ret=0
087bff
    for i in $tables; do
087bff
	echo -n "$i "
087bff
	case "$i" in
087bff
	    raw)
087bff
		$IPTABLES -t raw -P PREROUTING $policy \
087bff
		    && $IPTABLES -t raw -P OUTPUT $policy \
087bff
		    || let ret+=1
087bff
		;;
087bff
	    filter)
087bff
                $IPTABLES -t filter -P INPUT $policy \
087bff
		    && $IPTABLES -t filter -P OUTPUT $policy \
087bff
		    && $IPTABLES -t filter -P FORWARD $policy \
087bff
		    || let ret+=1
087bff
		;;
087bff
	    nat)
087bff
		$IPTABLES -t nat -P PREROUTING $policy \
087bff
		    && $IPTABLES -t nat -P POSTROUTING $policy \
087bff
		    && $IPTABLES -t nat -P OUTPUT $policy \
087bff
		    || let ret+=1
087bff
		;;
087bff
	    mangle)
087bff
	        $IPTABLES -t mangle -P PREROUTING $policy \
087bff
		    && $IPTABLES -t mangle -P POSTROUTING $policy \
087bff
		    && $IPTABLES -t mangle -P INPUT $policy \
087bff
		    && $IPTABLES -t mangle -P OUTPUT $policy \
087bff
		    && $IPTABLES -t mangle -P FORWARD $policy \
087bff
		    || let ret+=1
087bff
		;;
087bff
	    *)
087bff
	        let ret+=1
087bff
		;;
087bff
        esac
087bff
    done
087bff
087bff
    [ $ret -eq 0 ] && success || failure
087bff
    echo
087bff
    return $ret
087bff
}
087bff
087bff
load_sysctl() {
087bff
    # load matched sysctl values
087bff
    if [ -n "$IPTABLES_SYSCTL_LOAD_LIST" ]; then
087bff
        echo -n $"Loading sysctl settings: "
087bff
        ret=0
087bff
        for item in $IPTABLES_SYSCTL_LOAD_LIST; do
087bff
            fgrep -hs $item /etc/sysctl.d/*.conf | sysctl -p - >/dev/null
087bff
            let ret+=$?;
087bff
        done
087bff
        [ $ret -eq 0 ] && success || failure
087bff
        echo
087bff
    fi
087bff
    return $ret
087bff
}
087bff
087bff
start() {
087bff
    # Do not start if there is no config file.
087bff
    if [ ! -f "$IPTABLES_DATA" ]; then
087bff
	echo -n $"${IPTABLES}: No config file."; warning; echo
087bff
	return 6
087bff
    fi
087bff
087bff
    # check if ipv6 module load is deactivated
087bff
    if [ "${_IPV}" = "ipv6" ] \
087bff
	&& grep -qIsE "^install[[:space:]]+${_IPV}[[:space:]]+/bin/(true|false)" /etc/modprobe.conf /etc/modprobe.d/* ; then
087bff
	echo $"${IPTABLES}: ${_IPV} is disabled."
087bff
	return 150
087bff
    fi
087bff
087bff
    echo -n $"${IPTABLES}: Applying firewall rules: "
087bff
087bff
    OPT=
087bff
    [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
087bff
    if [ $IPTABLES_RESTORE_WAIT -ne 0 ]; then
087bff
       OPT="${OPT} --wait ${IPTABLES_RESTORE_WAIT}"
087bff
       if [ $IPTABLES_RESTORE_WAIT_INTERVAL -lt 1000000 ]; then
087bff
           OPT="${OPT} --wait-interval ${IPTABLES_RESTORE_WAIT_INTERVAL}"
087bff
       fi
087bff
    fi
087bff
087bff
    $IPTABLES-restore $OPT $IPTABLES_DATA
087bff
    if [ $? -eq 0 ]; then
087bff
	success; echo
087bff
    else
087bff
	failure; echo;
087bff
	if [ -f "$IPTABLES_FALLBACK_DATA" ]; then
087bff
	    echo -n $"${IPTABLES}: Applying firewall fallback rules: "
087bff
	    $IPTABLES-restore $OPT $IPTABLES_FALLBACK_DATA
087bff
	    if [ $? -eq 0 ]; then
087bff
		success; echo
087bff
	    else
087bff
		failure; echo; return 1
087bff
	    fi
087bff
	else
087bff
	    return 1
087bff
	fi
087bff
    fi
087bff
 
087bff
    # Load additional modules (helpers)
087bff
    if [ -n "$IPTABLES_MODULES" ]; then
087bff
	echo -n $"${IPTABLES}: Loading additional modules: "
087bff
	ret=0
087bff
	for mod in $IPTABLES_MODULES; do
087bff
	    echo -n "$mod "
087bff
	    modprobe $mod > /dev/null 2>&1
087bff
	    let ret+=$?;
087bff
	done
087bff
	[ $ret -eq 0 ] && success || failure
087bff
	echo
087bff
    fi
087bff
 
087bff
    # Load sysctl settings
087bff
    load_sysctl
087bff
087bff
    touch $VAR_SUBSYS_IPTABLES
087bff
    return $ret
087bff
}
087bff
087bff
stop() {
087bff
    # Do not stop if iptables module is not loaded.
087bff
    netfilter_active || return 0
087bff
087bff
    # Set default chain policy to ACCEPT, in order to not break shutdown
087bff
    # on systems where the default policy is DROP and root device is
087bff
    # network-based (i.e.: iSCSI, NFS)
087bff
    set_policy ACCEPT
087bff
    # And then, flush the rules and delete chains
087bff
    flush_n_delete
087bff
087bff
    rm -f $VAR_SUBSYS_IPTABLES
087bff
    return $ret
087bff
}
087bff
087bff
save() {
087bff
    # Check if iptable module is loaded
087bff
    if ! netfilter_active; then
087bff
	echo -n $"${IPTABLES}: Nothing to save."; warning; echo
087bff
	return 0
087bff
    fi
087bff
087bff
    # Check if firewall is configured (has tables)
087bff
    if [ -z "$NF_TABLES" ]; then
087bff
	echo -n $"${IPTABLES}: Nothing to save."; warning; echo
087bff
	return 6
087bff
    fi
087bff
087bff
    echo -n $"${IPTABLES}: Saving firewall rules to $IPTABLES_DATA: "
087bff
087bff
    OPT=
087bff
    [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
087bff
087bff
    ret=0
087bff
    TMP_FILE=$(/bin/mktemp -q $IPTABLES_DATA.XXXXXX) \
087bff
	&& chmod 600 "$TMP_FILE" \
087bff
	&& $IPTABLES-save $OPT > $TMP_FILE 2>/dev/null \
087bff
	&& size=$(stat -c '%s' $TMP_FILE) && [ $size -gt 0 ] \
087bff
	|| ret=1
087bff
    if [ $ret -eq 0 ]; then
087bff
	if [ -e $IPTABLES_DATA ]; then
087bff
	    cp -f $IPTABLES_DATA $IPTABLES_DATA.save \
087bff
		&& chmod 600 $IPTABLES_DATA.save \
087bff
		&& restorecon $IPTABLES_DATA.save \
087bff
		|| ret=1
087bff
	fi
087bff
	if [ $ret -eq 0 ]; then
087bff
	    mv -f $TMP_FILE $IPTABLES_DATA \
087bff
		&& chmod 600 $IPTABLES_DATA \
087bff
		&& restorecon $IPTABLES_DATA \
087bff
	        || ret=1
087bff
	fi
087bff
    fi
087bff
    rm -f $TMP_FILE
087bff
    [ $ret -eq 0 ] && success || failure
087bff
    echo
087bff
    return $ret
087bff
}
087bff
087bff
status() {
087bff
    if [ ! -f "$VAR_SUBSYS_IPTABLES" ]; then
087bff
	echo $"${IPTABLES}: Firewall is not running."
087bff
	return 3
087bff
    fi
087bff
087bff
    # Do not print status if lockfile is missing and iptables modules are not 
087bff
    # loaded.
087bff
    # Check if iptable modules are loaded
087bff
    if ! netfilter_active; then
087bff
	echo $"${IPTABLES}: Firewall modules are not loaded."
087bff
	return 3
087bff
    fi
087bff
087bff
    # Check if firewall is configured (has tables)
087bff
    if [ -z "$NF_TABLES" ]; then
087bff
	echo $"${IPTABLES}: Firewall is not configured. "
087bff
	return 3
087bff
    fi
087bff
087bff
    NUM=
087bff
    [ "x$IPTABLES_STATUS_NUMERIC" = "xyes" ] && NUM="-n"
087bff
    VERBOSE=
087bff
    [ "x$IPTABLES_STATUS_VERBOSE" = "xyes" ] && VERBOSE="--verbose"
087bff
    COUNT=
087bff
    [ "x$IPTABLES_STATUS_LINENUMBERS" = "xyes" ] && COUNT="--line-numbers"
087bff
087bff
    for table in $NF_TABLES; do
087bff
	echo $"Table: $table"
087bff
	$IPTABLES -t $table --list $NUM $VERBOSE $COUNT && echo
087bff
    done
087bff
087bff
    return 0
087bff
}
087bff
087bff
reload() {
087bff
    # Do not reload if there is no config file.
087bff
    if [ ! -f "$IPTABLES_DATA" ]; then
087bff
	echo -n $"${IPTABLES}: No config file."; warning; echo
087bff
	return 6
087bff
    fi
087bff
087bff
    # check if ipv6 module load is deactivated
087bff
    if [ "${_IPV}" = "ipv6" ] \
087bff
	&& grep -qIsE "^install[[:space:]]+${_IPV}[[:space:]]+/bin/(true|false)" /etc/modprobe.conf /etc/modprobe.d/* ; then
087bff
	echo $"${IPTABLES}: ${_IPV} is disabled."
087bff
	return 150
087bff
    fi
087bff
087bff
    echo -n $"${IPTABLES}: Trying to reload firewall rules: "
087bff
087bff
    OPT=
087bff
    [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
087bff
    if [ $IPTABLES_RESTORE_WAIT -ne 0 ]; then
087bff
       OPT="${OPT} --wait ${IPTABLES_RESTORE_WAIT}"
087bff
       if [ $IPTABLES_RESTORE_WAIT_INTERVAL -lt 1000000 ]; then
087bff
           OPT="${OPT} --wait-interval ${IPTABLES_RESTORE_WAIT_INTERVAL}"
087bff
       fi
087bff
    fi
087bff
087bff
    $IPTABLES-restore $OPT $IPTABLES_DATA
087bff
    if [ $? -eq 0 ]; then
087bff
	success; echo
087bff
    else
087bff
	failure; echo; echo "Firewall rules are not changed."; return 1
087bff
    fi
087bff
087bff
    # Load additional modules (helpers)
087bff
    if [ -n "$IPTABLES_MODULES" ]; then
087bff
	echo -n $"${IPTABLES}: Loading additional modules: "
087bff
	ret=0
087bff
	for mod in $IPTABLES_MODULES; do
087bff
	    echo -n "$mod "
087bff
	    modprobe $mod > /dev/null 2>&1
087bff
	    let ret+=$?;
087bff
	done
087bff
	[ $ret -eq 0 ] && success || failure
087bff
	echo
087bff
    fi
087bff
087bff
    # Load sysctl settings
087bff
    load_sysctl
087bff
087bff
    return $ret
087bff
}
087bff
087bff
restart() {
087bff
    [ "x$IPTABLES_SAVE_ON_RESTART" = "xyes" ] && save
087bff
    stop
087bff
    start
087bff
}
087bff
087bff
087bff
case "$1" in
087bff
    start)
087bff
	[ -f "$VAR_SUBSYS_IPTABLES" ] && exit 0
087bff
	start
087bff
	RETVAL=$?
087bff
	;;
087bff
    stop)
087bff
	[ "x$IPTABLES_SAVE_ON_STOP" = "xyes" ] && save
087bff
	stop
087bff
	RETVAL=$?
087bff
	;;
087bff
    restart|force-reload)
087bff
	restart
087bff
	RETVAL=$?
087bff
	;;
087bff
    reload)
087bff
	[ -e "$VAR_SUBSYS_IPTABLES" ] && reload
087bff
	RETVAL=$?
087bff
	;;      
087bff
    condrestart|try-restart)
087bff
	[ ! -e "$VAR_SUBSYS_IPTABLES" ] && exit 0
087bff
	restart
087bff
	RETVAL=$?
087bff
	;;
087bff
    status)
087bff
	status
087bff
	RETVAL=$?
087bff
	;;
087bff
    panic)
087bff
	set_policy DROP
087bff
	RETVAL=$?
087bff
        ;;
087bff
    save)
087bff
	save
087bff
	RETVAL=$?
087bff
	;;
087bff
    *)
087bff
	echo $"Usage: ${IPTABLES} {start|stop|reload|restart|condrestart|status|panic|save}"
087bff
	RETVAL=2
087bff
	;;
087bff
esac
087bff
087bff
exit $RETVAL