#!/bin/sh
#
# $NetBSD: smmsp,v 1.4 2004/10/12 14:51:03 lukem Exp $
#

# REQUIRE: mail

#	The only function of the smmsp process is to push locally
#	collected mail (which got stuck in the local client queue)
#	to a more fully functional smtp relay.  It is not a fully
#	functional smtp daemon.
#
#	It can, however, be used as a completely unprivileged
#	replacement for hosts that were previously using a "null
#	client" configuration and a smart relay.

$_rc_subr_loaded . /etc/rc.subr

name="smmsp"
rcvar=$name
command="/usr/sbin/sendmail"
pidfile="/var/spool/clientmqueue/sm-client.pid"
required_files="/etc/mail/submit.cf"
start_precmd="smmsp_precmd"

smmsp_precmd()
{
	_suidroot="${sendmail_suidroot:-NO}"
	if checkyesno _suidroot; then
		_owner_fmt="%p %Su"
		_def_owner="104555 root"
		_def_cqueuemode=""
	else
		_owner_fmt="%p %Su %Sg"
		_def_owner="102555 root smmsp"
		_def_cqueuemode="40770 smmsp smmsp"
	fi
	_def_version="10"
	_cqueuemode="${_def_cqueuemode}"	# changed later if needed

	# check owner and mode of real sendmail binary
	_owner=$(stat -qf"${_owner_fmt}" /usr/libexec/sendmail/sendmail)
	if [ "${_owner}" != "${_def_owner}" ]; then
		warn "${name} /usr/libexec/sendmail/sendmail has " \
		    "wrong owner/mode"
	fi

	# check submit.cf existence
	if checkyesno _suidroot; then
		if [ -f /etc/mail/submit.cf ]; then
			warn "${name} starting; " \
			    "sendmail is suid root, but submit.cf exists"
		else
			warn "${name} not started; no need for ${name} " \
			    "service when sendmail is suid root"
			return 1
		fi
	else
		if [ ! -f /etc/mail/submit.cf ]; then
			warn "${name} not started; /etc/mail/submit.cf missing"
			return 1
		else
			_cqueue=$(sed -n 's/^O *QueueDirectory=\(.*\)/\1/p' \
				/etc/mail/submit.cf)
			_cqueuemode=$(stat -qf"%p %Su %Sg" "${_cqueue}")
		fi
	fi

	# check .cf file version
	_cfversion=$(sed -n 's/^V *\([0-9]*\).*/\1/p' /etc/mail/submit.cf)
	if [ "${_cfversion}" != "${_def_version}" ]; then
		warn "${name} not started; " \
		    "/etc/mail/submit.cf has wrong version " \
		    "(have ${_cfversion}, should be ${_def_version})"
		return 1
	fi

	# look at clientmqueue's owner, group, and mode
	if [ "${_cqueuemode}" != "${_def_cqueuemode}" ]; then
		warn "${name} not started;" \
		    "queue ${_cqueue} has wrong owner/mode"
		return 1
	fi
}

load_rc_config $name
load_rc_config_var sendmail sendmail_suidroot
run_rc_command "$1"
