#!/sbin/runscript

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp_network 3471 2010-10-16 11:52:34Z scitech $
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "ispCP - ISP Control Panel".
#
# The Initial Developer of the Original Code is ispCP Team.
# Portions created by the ispCP Team are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#
### BEGIN INIT INFO
# Provides:             ispcp_network
# Required-Start:       $network $local_fs $remote_fs
# Required-Stop:
# Should-Stop:          $local_fs
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    ispCP Network Traffic Logger
#
### END INIT INFO
# Note: do not modify any of these vars here, use /etc/default/$NAME instead

IPTABLES=${IPTABLES:-$(which iptables)}
LOGDIR=${LOGDIR:-"/var/log/ispcp"}
LOGFILE=${LOGFILE:-"${LOGDIR}/${SVCNAME}.log"}
ENGINEPATH="/var/www/ispcp/engine"
ENGINETOOLSPATH=${ENGINEPATH}"/tools"
NETWORKCARDMANAGER="ispcp-net-interfaces-mngr"

# To monitor more ports, edit SERVICES variable add your own ports
# (ftp, proxy, http, etc.)
#
# HTTP(S): 80 443
# POP3(S): 110 995
# IMAP4(S)): 143 993
# MAIL(S): 25 465 587
SERVICES="80 443 110 143 25 465 587 995 993"
# To monitor more outgoing ports, edit SERVICES_OUT variable add your own ports
# (mail, etc.)
#
# MAIL(S): 25 465 587
SERVICES_OUT="25 465 587"

umask 027

depend() {
	need net
	use iptables
}

add_rules() {
	${IPTABLES} -N ISPCP_INPUT 2>> "$LOGFILE"
	${IPTABLES} -N ISPCP_OUTPUT 2>> "$LOGFILE"

	# All traffic should jump through ISPCP tables before anything else
	${IPTABLES} -I INPUT -j ISPCP_INPUT 2>> "$LOGFILE"
	${IPTABLES} -I OUTPUT -j ISPCP_OUTPUT 2>> "$LOGFILE"

	# Services from matrix basically receiving data
	for PORT in $SERVICES; do
		${IPTABLES} -I ISPCP_INPUT  -p tcp --dport "$PORT" 2>> "$LOGFILE"
		${IPTABLES} -I ISPCP_OUTPUT -p tcp --sport "$PORT" 2>> "$LOGFILE"
	done

	# Services from matrix basically sending data
	for PORT in $SERVICES_OUT; do
		${IPTABLES} -I ISPCP_INPUT  -p tcp --sport "$PORT" 2>> "$LOGFILE"
		${IPTABLES} -I ISPCP_OUTPUT -p tcp --dport "$PORT" 2>> "$LOGFILE"
	done

	# Explicit return once done
	${IPTABLES} -A ISPCP_INPUT -j RETURN
	${IPTABLES} -A ISPCP_OUTPUT -j RETURN
}

remove_rules() {
	${IPTABLES} -D INPUT -j ISPCP_INPUT 2>> "$LOGFILE"
	${IPTABLES} -D OUTPUT -j ISPCP_OUTPUT 2>> "$LOGFILE"
	${IPTABLES} -F ISPCP_INPUT 2>> "$LOGFILE"
	${IPTABLES} -F ISPCP_OUTPUT 2>> "$LOGFILE"
	${IPTABLES} -X ISPCP_INPUT 2>> "$LOGFILE"
	${IPTABLES} -X ISPCP_OUTPUT 2>> "$LOGFILE"
}

add_interfaces(){
	${ENGINETOOLSPATH}/${NETWORKCARDMANAGER} start >${LOGDIR}/${NETWORKCARDMANAGER}.log 2>&1
}

remove_interfaces(){
	${ENGINETOOLSPATH}/${NETWORKCARDMANAGER} stop >${LOGDIR}/${NETWORKCARDMANAGER}.log 2>&1
}

start() {
	ebegin "Starting $SVCNAME"
	add_interfaces
	add_rules
	eend $?
}

stop() {
	ebegin "Stopping $SVCNAME"
	remove_rules
	remove_interfaces
	eend $?
}

