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