Blame SOURCES/iptables.init

26b15f
#!/bin/bash
26b15f
#
26b15f
# iptables	Start iptables firewall
26b15f
#
26b15f
# chkconfig: 2345 08 92
26b15f
# description:	Starts, stops and saves iptables firewall
26b15f
#
26b15f
# config: /etc/sysconfig/iptables
26b15f
# config: /etc/sysconfig/iptables-config
26b15f
#
26b15f
### BEGIN INIT INFO
26b15f
# Provides: iptables
26b15f
# Required-Start:
26b15f
# Required-Stop:
26b15f
# Default-Start: 2 3 4 5
26b15f
# Default-Stop: 0 1 6
26b15f
# Short-Description: start and stop iptables firewall
26b15f
# Description: Start, stop and save iptables firewall
26b15f
### END INIT INFO
26b15f
26b15f
# Source function library.
26b15f
. /etc/init.d/functions
26b15f
26b15f
IPTABLES=iptables
26b15f
IPTABLES_DATA=/etc/sysconfig/$IPTABLES
26b15f
IPTABLES_FALLBACK_DATA=${IPTABLES_DATA}.fallback
26b15f
IPTABLES_CONFIG=/etc/sysconfig/${IPTABLES}-config
26b15f
IPV=${IPTABLES%tables} # ip for ipv4 | ip6 for ipv6
26b15f
[ "$IPV" = "ip" ] && _IPV="ipv4" || _IPV="ipv6"
26b15f
PROC_IPTABLES_NAMES=/proc/net/${IPV}_tables_names
26b15f
VAR_SUBSYS_IPTABLES=/var/lock/subsys/$IPTABLES
7115fc
RESTORECON=$(which restorecon 2>/dev/null)
26b15f
[ ! -x "$RESTORECON" ] && RESTORECON=/bin/true
26b15f
26b15f
# only usable for root
26b15f
if [ $EUID != 0 ]; then
26b15f
    echo -n $"${IPTABLES}: Only usable by root."; warning; echo
26b15f
    exit 4
26b15f
fi
26b15f
26b15f
if [ ! -x /sbin/$IPTABLES ]; then
26b15f
    echo -n $"${IPTABLES}: /sbin/$IPTABLES does not exist."; warning; echo
26b15f
    exit 5
26b15f
fi
26b15f
26b15f
# Old or new modutils
26b15f
/sbin/modprobe --version 2>&1 | grep -q 'kmod version' \
26b15f
    && NEW_MODUTILS=1 \
26b15f
    || NEW_MODUTILS=0
26b15f
26b15f
# Default firewall configuration:
26b15f
IPTABLES_MODULES=""
26b15f
IPTABLES_MODULES_UNLOAD="yes"
26b15f
IPTABLES_SAVE_ON_STOP="no"
26b15f
IPTABLES_SAVE_ON_RESTART="no"
26b15f
IPTABLES_SAVE_COUNTER="no"
26b15f
IPTABLES_STATUS_NUMERIC="yes"
26b15f
IPTABLES_STATUS_VERBOSE="no"
26b15f
IPTABLES_STATUS_LINENUMBERS="yes"
26b15f
IPTABLES_SYSCTL_LOAD_LIST=""
26b15f
IPTABLES_RESTORE_WAIT=600
26b15f
IPTABLES_RESTORE_WAIT_INTERVAL=1000000
26b15f
26b15f
# Load firewall configuration.
26b15f
[ -f "$IPTABLES_CONFIG" ] && . "$IPTABLES_CONFIG"
26b15f
26b15f
# Netfilter modules
26b15f
NF_MODULES=($(lsmod | awk "/^${IPV}table_/ {print \$1}") ${IPV}_tables)
26b15f
NF_MODULES_COMMON=(x_tables nf_nat nf_conntrack) # Used by netfilter v4 and v6
26b15f
26b15f
# Get active tables
26b15f
NF_TABLES=$(cat "$PROC_IPTABLES_NAMES" 2>/dev/null)
26b15f
86bcb8
# Prepare commands for wait options
86bcb8
IPTABLES_CMD="$IPTABLES"
86bcb8
IPTABLES_RESTORE_CMD="$IPTABLES-restore"
86bcb8
if [ $IPTABLES_RESTORE_WAIT -ne 0 ]; then
86bcb8
	OPT="--wait ${IPTABLES_RESTORE_WAIT}"
86bcb8
	if [ $IPTABLES_RESTORE_WAIT_INTERVAL -lt 1000000 ]; then
86bcb8
	    OPT+=" --wait-interval ${IPTABLES_RESTORE_WAIT_INTERVAL}"
86bcb8
	fi
86bcb8
	IPTABLES_CMD+=" $OPT"
86bcb8
	IPTABLES_RESTORE_CMD+=" $OPT"
86bcb8
fi
26b15f
26b15f
rmmod_r() {
26b15f
    # Unload module with all referring modules.
26b15f
    # At first all referring modules will be unloaded, then the module itself.
26b15f
    local mod=$1
26b15f
    local ret=0
26b15f
    local ref=
26b15f
26b15f
    # Get referring modules.
26b15f
    # New modutils have another output format.
26b15f
    [ $NEW_MODUTILS = 1 ] \
26b15f
	&& ref=$(lsmod | awk "/^${mod}/ { print \$4; }" | tr ',' ' ') \
26b15f
	|| ref=$(lsmod | grep ^${mod} | cut -d "[" -s -f 2 | cut -d "]" -s -f 1)
26b15f
26b15f
    # recursive call for all referring modules
26b15f
    for i in $ref; do
26b15f
	rmmod_r $i
26b15f
	let ret+=$?;
26b15f
    done
26b15f
26b15f
    # Unload module.
26b15f
    # The extra test is for 2.6: The module might have autocleaned,
26b15f
    # after all referring modules are unloaded.
26b15f
    if grep -q "^${mod}" /proc/modules ; then
26b15f
	modprobe -r $mod > /dev/null 2>&1
26b15f
	res=$?
26b15f
	[ $res -eq 0 ] || echo -n " $mod"
26b15f
	let ret+=$res;
26b15f
    fi
26b15f
26b15f
    return $ret
26b15f
}
26b15f
26b15f
flush_n_delete() {
86bcb8
    local ret=0
86bcb8
26b15f
    # Flush firewall rules and delete chains.
26b15f
    [ ! -e "$PROC_IPTABLES_NAMES" ] && return 0
26b15f
26b15f
    # Check if firewall is configured (has tables)
26b15f
    [ -z "$NF_TABLES" ] && return 1
26b15f
26b15f
    echo -n $"${IPTABLES}: Flushing firewall rules: "
26b15f
    # For all tables
26b15f
    for i in $NF_TABLES; do
26b15f
        # Flush firewall rules.
86bcb8
	$IPTABLES_CMD -t $i -F;
26b15f
	let ret+=$?;
26b15f
26b15f
        # Delete firewall chains.
86bcb8
	$IPTABLES_CMD -t $i -X;
26b15f
	let ret+=$?;
26b15f
26b15f
	# Set counter to zero.
86bcb8
	$IPTABLES_CMD -t $i -Z;
26b15f
	let ret+=$?;
26b15f
    done
26b15f
26b15f
    [ $ret -eq 0 ] && success || failure
26b15f
    echo
26b15f
    return $ret
26b15f
}
26b15f
26b15f
set_policy() {
86bcb8
    local ret=0
86bcb8
26b15f
    # Set policy for configured tables.
26b15f
    policy=$1
26b15f
26b15f
    # Check if iptable module is loaded
26b15f
    [ ! -e "$PROC_IPTABLES_NAMES" ] && return 0
26b15f
26b15f
    # Check if firewall is configured (has tables)
26b15f
    tables=$(cat "$PROC_IPTABLES_NAMES" 2>/dev/null)
26b15f
    [ -z "$tables" ] && return 1
26b15f
26b15f
    echo -n $"${IPTABLES}: Setting chains to policy $policy: "
26b15f
    for i in $tables; do
26b15f
	echo -n "$i "
26b15f
	case "$i" in
26b15f
	    raw)
86bcb8
		$IPTABLES_CMD -t raw -P PREROUTING $policy \
86bcb8
		    && $IPTABLES_CMD -t raw -P OUTPUT $policy \
26b15f
		    || let ret+=1
26b15f
		;;
26b15f
	    filter)
86bcb8
                $IPTABLES_CMD -t filter -P INPUT $policy \
86bcb8
		    && $IPTABLES_CMD -t filter -P OUTPUT $policy \
86bcb8
		    && $IPTABLES_CMD -t filter -P FORWARD $policy \
26b15f
		    || let ret+=1
26b15f
		;;
26b15f
	    nat)
86bcb8
		$IPTABLES_CMD -t nat -P PREROUTING $policy \
86bcb8
		    && $IPTABLES_CMD -t nat -P POSTROUTING $policy \
86bcb8
		    && $IPTABLES_CMD -t nat -P OUTPUT $policy \
26b15f
		    || let ret+=1
26b15f
		;;
26b15f
	    mangle)
86bcb8
	        $IPTABLES_CMD -t mangle -P PREROUTING $policy \
86bcb8
		    && $IPTABLES_CMD -t mangle -P POSTROUTING $policy \
86bcb8
		    && $IPTABLES_CMD -t mangle -P INPUT $policy \
86bcb8
		    && $IPTABLES_CMD -t mangle -P OUTPUT $policy \
86bcb8
		    && $IPTABLES_CMD -t mangle -P FORWARD $policy \
26b15f
		    || let ret+=1
26b15f
		;;
388998
	    security)
388998
	        # Ignore the security table
388998
	        ;;
26b15f
	    *)
26b15f
	        let ret+=1
26b15f
		;;
26b15f
        esac
26b15f
    done
26b15f
26b15f
    [ $ret -eq 0 ] && success || failure
26b15f
    echo
26b15f
    return $ret
26b15f
}
26b15f
26b15f
load_sysctl() {
86bcb8
    local ret=0
86bcb8
26b15f
    # load matched sysctl values
26b15f
    if [ -n "$IPTABLES_SYSCTL_LOAD_LIST" ]; then
26b15f
        echo -n $"Loading sysctl settings: "
26b15f
        for item in $IPTABLES_SYSCTL_LOAD_LIST; do
7115fc
            fgrep -hs $item /etc/sysctl.d/* | sysctl -p - >/dev/null
26b15f
            let ret+=$?;
26b15f
        done
26b15f
        [ $ret -eq 0 ] && success || failure
26b15f
        echo
26b15f
    fi
26b15f
    return $ret
26b15f
}
26b15f
26b15f
start() {
86bcb8
    local ret=0
86bcb8
26b15f
    # Do not start if there is no config file.
26b15f
    if [ ! -f "$IPTABLES_DATA" ]; then
26b15f
	echo -n $"${IPTABLES}: No config file."; warning; echo
26b15f
	return 6
26b15f
    fi
26b15f
26b15f
    # check if ipv6 module load is deactivated
26b15f
    if [ "${_IPV}" = "ipv6" ] \
26b15f
	&& grep -qIsE "^install[[:space:]]+${_IPV}[[:space:]]+/bin/(true|false)" /etc/modprobe.conf /etc/modprobe.d/* ; then
26b15f
	echo $"${IPTABLES}: ${_IPV} is disabled."
26b15f
	return 150
26b15f
    fi
26b15f
26b15f
    echo -n $"${IPTABLES}: Applying firewall rules: "
26b15f
26b15f
    OPT=
26b15f
    [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
26b15f
86bcb8
    $IPTABLES_RESTORE_CMD $OPT $IPTABLES_DATA
26b15f
    if [ $? -eq 0 ]; then
26b15f
	success; echo
26b15f
    else
26b15f
	failure; echo;
26b15f
	if [ -f "$IPTABLES_FALLBACK_DATA" ]; then
26b15f
	    echo -n $"${IPTABLES}: Applying firewall fallback rules: "
86bcb8
	    $IPTABLES_RESTORE_CMD $OPT $IPTABLES_FALLBACK_DATA
26b15f
	    if [ $? -eq 0 ]; then
26b15f
		success; echo
26b15f
	    else
26b15f
		failure; echo; return 1
26b15f
	    fi
26b15f
	else
26b15f
	    return 1
26b15f
	fi
26b15f
    fi
26b15f
    
26b15f
    # Load additional modules (helpers)
26b15f
    if [ -n "$IPTABLES_MODULES" ]; then
26b15f
	echo -n $"${IPTABLES}: Loading additional modules: "
26b15f
	for mod in $IPTABLES_MODULES; do
26b15f
	    echo -n "$mod "
26b15f
	    modprobe $mod > /dev/null 2>&1
26b15f
	    let ret+=$?;
26b15f
	done
26b15f
	[ $ret -eq 0 ] && success || failure
26b15f
	echo
26b15f
    fi
26b15f
    
26b15f
    # Load sysctl settings
26b15f
    load_sysctl
26b15f
26b15f
    touch $VAR_SUBSYS_IPTABLES
26b15f
    return $ret
26b15f
}
26b15f
26b15f
stop() {
86bcb8
    local ret=0
86bcb8
26b15f
    # Do not stop if iptables module is not loaded.
26b15f
    [ ! -e "$PROC_IPTABLES_NAMES" ] && return 0
26b15f
26b15f
    # Set default chain policy to ACCEPT, in order to not break shutdown
26b15f
    # on systems where the default policy is DROP and root device is
26b15f
    # network-based (i.e.: iSCSI, NFS)
26b15f
    set_policy ACCEPT
86bcb8
    let ret+=$?
26b15f
    # And then, flush the rules and delete chains
26b15f
    flush_n_delete
86bcb8
    let ret+=$?
26b15f
    
26b15f
    if [ "x$IPTABLES_MODULES_UNLOAD" = "xyes" ]; then
26b15f
	echo -n $"${IPTABLES}: Unloading modules: "
86bcb8
	ret2=0
26b15f
	for mod in ${NF_MODULES[*]}; do
26b15f
	    rmmod_r $mod
86bcb8
	    let ret2+=$?;
26b15f
	done
26b15f
	# try to unload remaining netfilter modules used by ipv4 and ipv6 
26b15f
	# netfilter
26b15f
	for mod in ${NF_MODULES_COMMON[*]}; do
26b15f
	    rmmod_r $mod >/dev/null
26b15f
	done
86bcb8
	[ $ret2 -eq 0 ] && success || failure
26b15f
	echo
86bcb8
        let ret+=$ret2
26b15f
    fi
26b15f
    
26b15f
    rm -f $VAR_SUBSYS_IPTABLES
26b15f
    return $ret
26b15f
}
26b15f
26b15f
save() {
86bcb8
    local ret=0
86bcb8
26b15f
    # Check if iptable module is loaded
26b15f
    if [ ! -e "$PROC_IPTABLES_NAMES" ]; then
26b15f
	echo -n $"${IPTABLES}: Nothing to save."; warning; echo
26b15f
	return 0
26b15f
    fi
26b15f
26b15f
    # Check if firewall is configured (has tables)
26b15f
    if [ -z "$NF_TABLES" ]; then
26b15f
	echo -n $"${IPTABLES}: Nothing to save."; warning; echo
26b15f
	return 6
26b15f
    fi
26b15f
26b15f
    echo -n $"${IPTABLES}: Saving firewall rules to $IPTABLES_DATA: "
26b15f
26b15f
    OPT=
26b15f
    [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
26b15f
26b15f
    TMP_FILE=$(/bin/mktemp -q $IPTABLES_DATA.XXXXXX) \
26b15f
	&& chmod 600 "$TMP_FILE" \
26b15f
	&& $IPTABLES-save $OPT > $TMP_FILE 2>/dev/null \
26b15f
	&& size=$(stat -c '%s' $TMP_FILE) && [ $size -gt 0 ] \
26b15f
	|| ret=1
26b15f
    if [ $ret -eq 0 ]; then
26b15f
	if [ -e $IPTABLES_DATA ]; then
26b15f
	    cp -f $IPTABLES_DATA $IPTABLES_DATA.save \
26b15f
		&& chmod 600 $IPTABLES_DATA.save \
26b15f
		&& $RESTORECON $IPTABLES_DATA.save \
26b15f
		|| ret=1
26b15f
	fi
26b15f
	if [ $ret -eq 0 ]; then
26b15f
	    mv -f $TMP_FILE $IPTABLES_DATA \
26b15f
		&& chmod 600 $IPTABLES_DATA \
26b15f
		&& $RESTORECON $IPTABLES_DATA \
26b15f
	        || ret=1
26b15f
	fi
26b15f
    fi
26b15f
    rm -f $TMP_FILE
26b15f
    [ $ret -eq 0 ] && success || failure
26b15f
    echo
26b15f
    return $ret
26b15f
}
26b15f
26b15f
status() {
26b15f
    if [ ! -f "$VAR_SUBSYS_IPTABLES" -a -z "$NF_TABLES" ]; then
26b15f
	echo $"${IPTABLES}: Firewall is not running."
26b15f
	return 3
26b15f
    fi
26b15f
26b15f
    # Do not print status if lockfile is missing and iptables modules are not 
26b15f
    # loaded.
26b15f
    # Check if iptable modules are loaded
26b15f
    if [ ! -e "$PROC_IPTABLES_NAMES" ]; then
26b15f
	echo $"${IPTABLES}: Firewall modules are not loaded."
26b15f
	return 3
26b15f
    fi
26b15f
26b15f
    # Check if firewall is configured (has tables)
26b15f
    if [ -z "$NF_TABLES" ]; then
26b15f
	echo $"${IPTABLES}: Firewall is not configured. "
26b15f
	return 3
26b15f
    fi
26b15f
26b15f
    NUM=
26b15f
    [ "x$IPTABLES_STATUS_NUMERIC" = "xyes" ] && NUM="-n"
26b15f
    VERBOSE= 
26b15f
    [ "x$IPTABLES_STATUS_VERBOSE" = "xyes" ] && VERBOSE="--verbose"
26b15f
    COUNT=
26b15f
    [ "x$IPTABLES_STATUS_LINENUMBERS" = "xyes" ] && COUNT="--line-numbers"
26b15f
26b15f
    for table in $NF_TABLES; do
26b15f
	echo $"Table: $table"
26b15f
	$IPTABLES -t $table --list $NUM $VERBOSE $COUNT && echo
26b15f
    done
26b15f
26b15f
    return 0
26b15f
}
26b15f
26b15f
reload() {
86bcb8
    local ret=0
86bcb8
26b15f
    # Do not reload if there is no config file.
26b15f
    if [ ! -f "$IPTABLES_DATA" ]; then
26b15f
	echo -n $"${IPTABLES}: No config file."; warning; echo
26b15f
	return 6
26b15f
    fi
26b15f
26b15f
    # check if ipv6 module load is deactivated
26b15f
    if [ "${_IPV}" = "ipv6" ] \
26b15f
	&& grep -qIsE "^install[[:space:]]+${_IPV}[[:space:]]+/bin/(true|false)" /etc/modprobe.conf /etc/modprobe.d/* ; then
26b15f
	echo $"${IPTABLES}: ${_IPV} is disabled."
26b15f
	return 150
26b15f
    fi
26b15f
26b15f
    echo -n $"${IPTABLES}: Trying to reload firewall rules: "
26b15f
26b15f
    OPT=
26b15f
    [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
26b15f
86bcb8
    $IPTABLES_RESTORE_CMD $OPT $IPTABLES_DATA
26b15f
    if [ $? -eq 0 ]; then
26b15f
	success; echo
26b15f
    else
26b15f
	failure; echo; echo "Firewall rules are not changed."; return 1
26b15f
    fi
26b15f
26b15f
    # Load additional modules (helpers)
26b15f
    if [ -n "$IPTABLES_MODULES" ]; then
26b15f
	echo -n $"${IPTABLES}: Loading additional modules: "
26b15f
	for mod in $IPTABLES_MODULES; do
26b15f
	    echo -n "$mod "
26b15f
	    modprobe $mod > /dev/null 2>&1
26b15f
	    let ret+=$?;
26b15f
	done
26b15f
	[ $ret -eq 0 ] && success || failure
26b15f
	echo
26b15f
    fi
26b15f
26b15f
    # Load sysctl settings
26b15f
    load_sysctl
26b15f
26b15f
    return $ret
26b15f
}
26b15f
26b15f
restart() {
26b15f
    [ "x$IPTABLES_SAVE_ON_RESTART" = "xyes" ] && save
26b15f
    stop
26b15f
    start
26b15f
}
26b15f
26b15f
26b15f
case "$1" in
26b15f
    start)
26b15f
	[ -f "$VAR_SUBSYS_IPTABLES" ] && exit 0
26b15f
	start
26b15f
	RETVAL=$?
26b15f
	;;
26b15f
    stop)
26b15f
	[ "x$IPTABLES_SAVE_ON_STOP" = "xyes" ] && save
26b15f
	stop
26b15f
	RETVAL=$?
26b15f
	;;
26b15f
    restart|force-reload)
26b15f
	restart
26b15f
	RETVAL=$?
26b15f
	;;
26b15f
    reload)
26b15f
	[ -e "$VAR_SUBSYS_IPTABLES" ] && reload
26b15f
	RETVAL=$?
26b15f
	;;      
26b15f
    condrestart|try-restart)
26b15f
	[ ! -e "$VAR_SUBSYS_IPTABLES" ] && exit 0
26b15f
	restart
26b15f
	RETVAL=$?
26b15f
	;;
26b15f
    status)
26b15f
	status
26b15f
	RETVAL=$?
26b15f
	;;
26b15f
    panic)
26b15f
	set_policy DROP
26b15f
	RETVAL=$?
26b15f
        ;;
26b15f
    save)
26b15f
	save
26b15f
	RETVAL=$?
26b15f
	;;
26b15f
    *)
26b15f
	echo $"Usage: ${IPTABLES} {start|stop|reload|restart|condrestart|status|panic|save}"
26b15f
	RETVAL=2
26b15f
	;;
26b15f
esac
26b15f
26b15f
exit $RETVAL