#!/bin/sh
#
# $NetBSD: smtpfront.sh,v 1.3 2017/07/31 20:38:51 schmonz Exp $
#

# PROVIDE: smtpfront
# REQUIRE: qmailsend cvm

name="smtpfront"

# User-settable rc.conf variables and their default values:
: ${smtpfront_postenv:="CVM_LOOKUP_SECRET='' CVM_LOOKUP='cvm-local:/var/run/cvm-qmail'"}
: ${smtpfront_tcpflags:="-vRl0"}
: ${smtpfront_tcphost:="0"}
: ${smtpfront_tcpport:="25"}
: ${smtpfront_datalimit:="180000000"}
: ${smtpfront_pretcpserver:=""}
: ${smtpfront_tcpserver:="/usr/pkg/bin/tcpserver"}
: ${smtpfront_presmtpd:=""}
: ${smtpfront_smtpdcmd:="/usr/pkg/bin/smtpfront-qmail"}
: ${smtpfront_postsmtpd:=""}
: ${smtpfront_log:="YES"}
: ${smtpfront_logcmd:="logger -t nb${name} -p mail.info"}
: ${smtpfront_nologcmd:="/usr/pkg/bin/multilog -*"}

if [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
fi

rcvar=${name}
required_files="/usr/pkg/etc/qmail/control/concurrencyincoming"
required_files="${required_files} /usr/pkg/etc/qmail/tcp.smtp.cdb"
required_files="${required_files} /usr/pkg/etc/qmail/control/rcpthosts"
command="${smtpfront_tcpserver}"
procname=${name}
start_precmd="smtpfront_precmd"
extra_commands="cdb"
cdb_cmd="smtpfront_cdb"

smtpfront_precmd()
{
	if [ -f /etc/rc.subr ] && ! checkyesno smtpfront_log; then
		smtpfront_logcmd=${smtpfront_nologcmd}
	fi
	# tcpserver(1) is akin to inetd(8), but runs one service per process.
	# We want to signal only the tcpserver process responsible for SMTP
	# service. Use argv0(1) to set procname to "smtpfront".
	command="/usr/pkg/bin/pgrphack /usr/bin/env - ${smtpfront_postenv}
/usr/pkg/bin/softlimit -m ${smtpfront_datalimit} ${smtpfront_pretcpserver}
/usr/pkg/bin/argv0 ${smtpfront_tcpserver} ${name}
${smtpfront_tcpflags} -x /usr/pkg/etc/qmail/tcp.smtp.cdb
-c `/usr/bin/head -1 /usr/pkg/etc/qmail/control/concurrencyincoming`
-u `/usr/bin/id -u qmaild ` -g `/usr/bin/id -g qmaild`
${smtpfront_tcphost} ${smtpfront_tcpport}
${smtpfront_presmtpd} ${smtpfront_smtpdcmd} ${smtpfront_postsmtpd}
2>&1 |
/usr/pkg/bin/pgrphack /usr/pkg/bin/setuidgid qmaill ${smtpfront_logcmd}"
	command_args="&"
	rc_flags=""
}

smtpfront_cdb()
{
	echo "Reloading /usr/pkg/etc/qmail/tcp.smtp."
	cd /usr/pkg/etc/qmail
	/usr/pkg/bin/tcprules tcp.smtp.cdb tcp.smtp.tmp < tcp.smtp
	/bin/chmod 644 tcp.smtp.cdb
}

if [ -f /etc/rc.subr ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	echo -n " ${name}"
	smtpfront_precmd
	eval ${command} ${smtpfront_flags} ${command_args}
fi
