#!/bin/sh
#
# Copyright (C) 2005 Messiah College.
# Copyright (C) 2008 Thomas Goirand <thomas@goirand.fr>

### BEGIN INIT INFO
# Provides:		dkimproxy
# Required-Start:	$local_fs $remote_fs
# Required-Stop:	$local_fs $remote_fs
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Domain key filter init script
# Description:		dkimproxy is an SMTP-proxy designed for Postfix. It
#                       implements DKIM message signing and verification.
#                       It comprises two separate filters, an "outbound" filter
#                       for signing outgoing email, and an "inbound" filter for
#                       verifying signatures of incoming email. The filters can
#                       operate as either Before-Queue or After-Queue Postfix
#                       content filters.
### END INIT INFO

. /lib/lsb/init-functions

DKIMPROXYUSER=dkimproxy
DKIMPROXYGROUP=dkimproxy
DKIMPROXY_OUT_PRIVKEY="/var/lib/dkimproxy/private.key"

DKIM_HOSTNAME=`hostname -f`

# Get the host domains dynamically. You can change this to the location where
# you have your virtual table here, or best: ehance this script to support more
# situations with packages others than DTC
HOST_DOMAIN=`hostname -d`
if [ -f /var/lib/dtc/etc/local_domains ] ; then
	DTC_DOMAIN=`cat /var/lib/dtc/etc/local_domains | tr \\\r\\\n ,,`
	DTC_DOMAIN=`echo ${DTC_DOMAIN} | grep -v ^${HOST_DOMAIN}`
else
	DTC_DOMAIN=""
fi
DOMAIN=${DTC_DOMAIN}${HOST_DOMAIN}

DKIMPROXY_IN_ARGS="--hostname=${DKIM_HOSTNAME} --conf_file /etc/dkimproxy/dkimproxy_in.conf --user=${DKIMPROXYUSER} --group=${DKIMPROXYGROUP}"
DKIMPROXY_OUT_ARGS="--domain=${DOMAIN} --method=simple --conf_file /etc/dkimproxy/dkimproxy_out.conf --user=${DKIMPROXYUSER} --group=${DKIMPROXYGROUP}"

DKIMPROXY_IN_BIN="/usr/sbin/dkimproxy.in"
DKIMPROXY_OUT_BIN="/usr/sbin/dkimproxy.out"
PIDDKIMPROXY_IN="/var/run/dkimproxy.in"
PIDDKIMPROXY_OUT="/var/run/dkimproxy.out"

if [ -x /sbin/start-stop-daemon ] ; then
	STRT_STP_DMN=/sbin/start-stop-daemon
else
	STRT_STP_DMN=`which start-stop-daemon`
fi
if [ -z "${STRT_STP_DMN}" ] ; then
	echo "Can't find the start-stop-daemon binary"
fi

case "$1" in
	start)
		log_daemon_msg "Starting inbound DomainKeys-filter" "dkimproxy.in"
		${STRT_STP_DMN} --background --make-pidfile --start -p ${PIDDKIMPROXY_IN} -u ${DKIMPROXYUSER} -g ${DKIMPROXYGROUP} -x ${DKIMPROXY_IN_BIN} -- ${DKIMPROXY_IN_ARGS}
		RETVAL=$?
		START_ERROR=${RETVAL}
		log_end_msg ${RETVAL}
		if ! [ "${RETVAL}" -eq 0 ] ; then
			exit ${RETVAL}
		fi

		log_daemon_msg "Starting outbound DomainKeys-filter" "dkimproxy.out"
		${STRT_STP_DMN} --background --make-pidfile --start -p ${PIDDKIMPROXY_OUT} -u ${DKIMPROXYUSER} -g ${DKIMPROXYGROUP} -x ${DKIMPROXY_OUT_BIN} -- ${DKIMPROXY_OUT_ARGS}
		RETVAL=$?
		log_end_msg ${RETVAL}
		if ! [ "${RETVAL}" -eq 0 -a "${START_ERROR}" -eq 0 ] ; then
			if ! [ ${RETVAL} -eq 0 ] ; then
				echo "Error ${RETVAL} when starting ${DKIMPROXY_IN_BIN}"
			fi
			if ! [ "${START_ERROR}" -eq 0 ] ; then
				echo "Error ${START_ERROR} when starting ${DKIMPROXY_OUT_BIN}"
			fi
		fi
		;;

	stop)
		log_daemon_msg "Shutting down inbound DomainKeys-filter" "dkimproxy.in"
		${STRT_STP_DMN} --stop -p ${PIDDKIMPROXY_IN}
		RETVALIN=$?
		log_end_msg ${RETVALIN}
		log_daemon_msg "Shutting down outbound DomainKeys-filter" "dkimproxy.out"
		${STRT_STP_DMN} --stop -p ${PIDDKIMPROXY_OUT}
		RETVALOUT=$?
		log_end_msg ${RETVALOUT}
		if ! [ ${RETVALIN} -eq 0 -a ${RETVALOUT} -eq 0 ]; then
			if ! [ ${RETVALIN} -eq 0 ] ; then
				echo "Error ${RETVALIN} when shutting down ${PIDDKIMPROXY_IN}"
			fi
			if ! [ "${RETVALOUT}" -eq 0 ] ; then
				echo "Error ${RETVALOUT} when shutting down ${PIDDKIMPROXY_OUT}"
			fi
		fi
		;;
	force-reload)
		$0 stop
		sleep 1
		$0 start
		;;
	reload)
		$0 stop
		sleep 1
		$0 start
		;;
	restart)
		$0 stop
		sleep 1
		$0 start
		;;
	*)
		echo "Usage: $0 {start|stop|restart|reload|force-reload}"
		exit 1
		;;
esac

exit 0
