#!/sbin/runscript
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-firewall/iptables/files/iptables-1.2.9-r1.init,v 1.5 2005/05/05 01:27:29 vapier Exp $

opts="save reload"

depend() {
	before net
	need logger
}

checkrules() {
	if [ ! -f ${IPTABLES_SAVE} ]
	then
		eerror "Not starting iptables. First create some rules then run"
		eerror "/etc/init.d/iptables save"
		return 1
	fi
}

start() {
	checkrules || return 1
	ebegin "Loading iptables state and starting firewall"
	einfo "Restoring iptables ruleset"
	/sbin/iptables-restore ${SAVE_RESTORE_OPTIONS} < ${IPTABLES_SAVE}
	eend $?
}

stop() {
	if [ "${SAVE_ON_STOP}" = "yes" ]; then
		save || return 1
	fi
	ebegin "Stopping firewall"
	for a in `cat /proc/net/ip_tables_names`; do
		/sbin/iptables -F -t $a
		/sbin/iptables -X -t $a

		if [ $a == nat ]; then
			/sbin/iptables -t nat -P PREROUTING ACCEPT
			/sbin/iptables -t nat -P POSTROUTING ACCEPT
			/sbin/iptables -t nat -P OUTPUT ACCEPT
		elif [ $a == mangle ]; then
			/sbin/iptables -t mangle -P PREROUTING ACCEPT
			/sbin/iptables -t mangle -P INPUT ACCEPT
			/sbin/iptables -t mangle -P FORWARD ACCEPT
			/sbin/iptables -t mangle -P OUTPUT ACCEPT
			/sbin/iptables -t mangle -P POSTROUTING ACCEPT
		elif [ $a == filter ]; then
			/sbin/iptables -t filter -P INPUT ACCEPT
			/sbin/iptables -t filter -P FORWARD ACCEPT
			/sbin/iptables -t filter -P OUTPUT ACCEPT
		fi
	done
	eend $?
}

reload() {
	ebegin "Flushing firewall"
	for a in `cat /proc/net/ip_tables_names`; do
		/sbin/iptables -F -t $a
		/sbin/iptables -X -t $a
	done;
	eend $?

	start
}

save() {
	local ret
	ebegin "Saving iptables state"
	/sbin/iptables-save ${SAVE_RESTORE_OPTIONS} > ${IPTABLES_SAVE}
	ret=$?
	chmod 0600 ${IPTABLES_SAVE}
	eend ${ret}
}
