#!/bin/sh
#
# $FreeBSD: ports/mail/dkim-milter/files/milter-dkim.sh.in,v 1.10 2007/05/25 22:11:34 miwi Exp $
#

# PROVIDE: milter-dkim
# REQUIRE: DAEMON
# BEFORE: mail
# KEYWORD: shutdown

# Define these milterdkim_* variables in one of these files:
#	/etc/rc.conf
#	/etc/rc.conf.local
#	/etc/rc.conf.d/milterdkim
#
# milterdkim_enable (bool):   Set to "NO" by default.
#                             Set it to "YES" to enable dkim-milter
# milterdkim_uid (str):       Set username to run milter.
# milterdkim_profiles (list): Set to "" by default.
#                             Define your profiles here.
# milterdkim_socket (str):    Path to the milter socket.
# milterdkim_cfgfile (str):   Configuration file. Also see dkim-filter.conf(5)
# milterdkim_domain (str):    Domainpart of From: in mails to sign.
# milterdkim_key (str):       Path to the private key file to sign with.
# milterdkim_selector (str):  Selector to use when signing
# milterdkim_alg (str):       Algorithm to use when signing
# milterdkim_flags (str):     Flags passed to start command.
#
# milterdkim_${profile}_* :   Variables per profile.
#                             Sockets must be different from each other.
#
# DO NOT CHANGE THESE DEFAULT VALUES HERE
#
milterdkim_enable=${milterdkim_enable:-"NO"}
milterdkim_uid=${milterdkim_uid:-"mailnull"}
milterdkim_profiles=${milterdkim_profiles:-}
milterdkim_socket=${milterdkim_socket:-"local:/var/run/milterdkim/filter"}
milterdkim_cfgfile=${milterdkim_cfgfile:-"%%PREFIX%%/etc/mail/dkim-filter.conf"}
milterdkim_domain=${milterdkim_domain:-"example.com"}
milterdkim_key=${milterdkim_key:-}
milterdkim_selector=${milterdkim_selector:-}
milterdkim_alg=${milterdkim_alg:-}
# Options other than above can be set with $milterdkim_flags.
# see dkim-milter documentation for detail.
milterdkim_flags=${milterdkim_flags:-"-b sv -c simple/simple -m MSA"}

. %%RC_SUBR%%

name="milterdkim"
rcvar=`set_rcvar`

start_precmd="dkim_prepcmd"
stop_postcmd="dkim_postcmd"
command="%%PREFIX%%/libexec/dkim-filter"
_piddir="/var/run/milterdkim"
pidfile="${_piddir}/pid"

load_rc_config $name

if [ -n "$2" ]; then
    profile="$2"
    if [ "x${milterdkim_profiles}" != "x" ]; then
	pidfile="${_piddir}/${profile}.pid"
	eval milterdkim_enable="\${milterdkim_${profile}_enable:-${milterdkim_enable}}"
	eval milterdkim_socket="\${milterdkim_${profile}_socket:-}"
	if [ "x${milterdkim_socket}" = "x" ];then
	    echo "You must define a socket (milterdkim_${profile}_socket)"
	    exit 1
	fi
	eval milterdkim_cfgfile="\${milterdkim_${profile}_cfgfile:-${milterdkim_cfgfile}}"
	eval milterdkim_domain="\${milterdkim_${profile}_domain:-${milterdkim_domain}}"
	eval milterdkim_key="\${milterdkim_${profile}_key:-${milterdkim_key}}"
	eval milterdkim_selector="\${milterdkim_${profile}_selector:-${milterdkim_selector}}"
	eval milterdkim_alg="\${milterdkim_${profile}_alg:-${milterdkim_alg}}"
	eval milterdkim_flags="\${milterdkim_${profile}_flags:-${milterdkim_flags}}"
	if [ -f "${milterdkim_cfgfile}" ];then
	    milterdkim_cfgfile="-x ${milterdkim_cfgfile}"
	else
	    milterdkim_cfgfile=""
	fi
	if [ "x${milterdkim_domain}" != "x" ];then
	    milterdkim_domain="-d ${milterdkim_domain}"
	fi
	if [ "x${milterdkim_key}" != "x" ];then
	    milterdkim_key="-k ${milterdkim_key}"
	fi
	if [ "x${milterdkim_selector}" != "x" ];then
	    milterdkim_selector="-s ${milterdkim_selector}"
	fi
	if [ "x${milterdkim_alg}" != "x" ];then
	    milterdkim_alg="-S ${milterdkim_alg}"
	fi
	command_args="-l -p ${milterdkim_socket} -u ${milterdkim_uid} -P ${pidfile} ${milterdkim_cfgfile} ${milterdkim_domain} ${milterdkim_key} ${milterdkim_selector} ${milterdkim_alg}"
    else
	echo "$0: extra argument ignored"
    fi
else
    if [ "x${milterdkim_profiles}" != "x" -a "x$1" != "x" ]; then
	if [ "x$1" != "xrestart" ]; then
	    for profile in ${milterdkim_profiles}; do
		echo "===> milterdkim profile: ${profile}"
		%%RC_SCRIPT%% $1 ${profile}
		retcode="$?"
		if [ "0${retcode}" -ne 0 ]; then
		    failed="${profile} (${retcode}) ${failed:-}"
		else
		    success="${profile} ${success:-}"
		fi
	    done
	    exit 0
	else
	    restart_precmd=""
	fi
    else
	if [ -f "${milterdkim_cfgfile}" ];then
	    milterdkim_cfgfile="-x ${milterdkim_cfgfile}"
	else
	    milterdkim_cfgfile=""
	fi
	if [ "x${milterdkim_domain}" != "x" ];then
	    milterdkim_domain="-d ${milterdkim_domain}"
	fi
	if [ "x${milterdkim_key}" != "x" ];then
	    milterdkim_key="-k ${milterdkim_key}"
	fi
	if [ "x${milterdkim_selector}" != "x" ];then
	    milterdkim_selector="-s ${milterdkim_selector}"
	fi
	if [ "x${milterdkim_alg}" != "x" ];then
	    milterdkim_alg="-S ${milterdkim_alg}"
	fi
	command_args="-l -p ${milterdkim_socket} -u ${milterdkim_uid} -P ${pidfile} ${milterdkim_cfgfile} ${milterdkim_domain} ${milterdkim_key} ${milterdkim_selector} ${milterdkim_alg}"
    fi
fi

dkim_prepcmd ()
{
    if [ -S ${milterdkim_socket##local:} ] ; then
        rm -f ${milterdkim_socket##local:}
    elif [ -S ${milterdkim_socket##unix:} ] ; then
        rm -f ${milterdkim_socket##unix:}
    fi
    if [ ! -d ${_piddir} ] ; then
	mkdir -p ${_piddir}
    fi
    if [ -n "${milterdkim_uid}" ] ; then
	chown ${milterdkim_uid} ${_piddir}
    fi
}

dkim_postcmd()
{
    if [ -S ${milterdkim_socket##local:} ] ; then
        rm -f ${milterdkim_socket##local:}
    elif [ -S ${milterdkim_socket##unix:} ] ; then
        rm -f ${milterdkim_socket##unix:}
    fi
    # just if the directory is empty
    rmdir ${_piddir} > /dev/null 2>&1
}

run_rc_command "$1"


syntax highlighted by Code2HTML, v. 0.9.1