#!/sbin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"%Z%%M%	%I%	%E% SMI"

. /lib/svc/share/smf_include.sh

ERRMSG1='WARNING: /var/mail is NFS-mounted without setting actimeo=0,'
ERRMSG2='this can cause mailbox locking and access problems.'
SERVER_PID_FILE="/var/run/sendmail.pid"
CLIENT_PID_FILE="/var/spool/clientmqueue/sm-client.pid"
DEFAULT_FILE="/etc/default/sendmail"
ALIASES_FILE="/etc/mail/aliases"
SENDMAIL_CF="/etc/mail/sendmail.cf"
SUBMIT_CF="/etc/mail/submit.cf"
SENDMAIL="/usr/lib/sendmail"
PATH="/usr/bin:/usr/sbin:/usr/ccs/bin"
export PATH

check_queue_interval_syntax()
{
	default="15m"
	if [ $# -lt 1 ]; then
		answer=$default
		return
	fi
	if echo $1 | egrep '^([0-9]*[1-9][0-9]*[smhdw])+$' >/dev/null 2>&1; then
		answer=$1
	else
		answer=$default
	fi
}

check_and_kill()
{
	PID=`head -1 $1`
	kill -0 $PID > /dev/null 2>&1
	[ $? -eq 0 ] && kill $PID
}

exist_or_exit()
{
	if [ ! -f $1 ]; then
		echo "$1 does not exist"
		exit $SMF_EXIT_ERR_CONFIG
	fi
}

turn_m4_crank()
{
	# expected to be called with two arguments: .cf path & path to m4 file
	[ $# -lt 2 ] && return
	cf_path=$1
	m4_path=$2
	case "$m4_path" in
	/*)	;;	# absolute path
	*)	return;;
	esac
	if [ "$m4_path" = "_DONT_TOUCH_THIS" ]; then
		if [ -f "${cf_path}.old" ]; then
			mv "$cf_path" "${cf_path}.new"
			[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
			mv "${cf_path}.old" "$cf_path"
			[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
		fi
		#
		# If ${cf_path}.old does not exist, assume it was taken care
		# of on a previous run.
		#
	else
		exist_or_exit "$m4_path"
		cd `dirname "$m4_path"`
		base=`basename "$m4_path"`
		name=`basename "$m4_path" .mc`
		info=`svcprop -p config/include_info $SMF_FMRI 2>/dev/null`
		if [ "$info" = "true" ]; then
			m4flags=""
		else
			m4flags="-DSUN_HIDE_INTERNAL_DETAILS"
		fi
		m4 $m4flags /etc/mail/cf/m4/cf.m4 "$base" > "${name}.cf"
		[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
		cmp -s "${name}.cf" "$cf_path" || (
			cp "${name}.cf" "${cf_path}.tmp" &&
			chown root:bin "${cf_path}.tmp" &&
			chmod 444 "${cf_path}.tmp" &&
			mv "${cf_path}.tmp" "$cf_path"
		)
		[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
	fi
}

case "$1" in 
'refresh')
        [ -f $SERVER_PID_FILE ] && kill -1 `head -1 $SERVER_PID_FILE`
        [ -f $CLIENT_PID_FILE ] && kill -1 `head -1 $CLIENT_PID_FILE`
        ;;

'start')
	exist_or_exit $SENDMAIL
	if [ ! -d /var/spool/mqueue ]; then
		/usr/bin/mkdir -m 0750 /var/spool/mqueue
		/usr/bin/chown root:bin /var/spool/mqueue
	fi
	if [ ! -f $ALIASES_FILE.db ] && [ ! -f $ALIASES_FILE.dir ] \
	    && [ ! -f $ALIASES_FILE.pag ]; then
		/usr/sbin/newaliases
	fi
	MODE="-bd"
	[ -f $DEFAULT_FILE ] && . $DEFAULT_FILE
	#
	# * MODE should be "-bd" or null (MODE= or MODE="") or
	#   left alone.  Anything else and you're on your own.
	# * QUEUEOPTION should be "p" or null (as above).
	# * [CLIENT]QUEUEINTERVAL should be set to some legal value;
	#   sanity checks are done below.
	# * [CLIENT]OPTIONS are catch-alls; set with care.
	#
	if [ -n "$QUEUEOPTION" -a "$QUEUEOPTION" != "p" ]; then
		QUEUEOPTION=""
	fi
	if [ -z "$QUEUEOPTION" -o -n "$QUEUEINTERVAL" ]; then
		check_queue_interval_syntax $QUEUEINTERVAL
		QUEUEINTERVAL=$answer
	fi
	check_queue_interval_syntax $CLIENTQUEUEINTERVAL
	CLIENTQUEUEINTERVAL=$answer

	local=`/usr/bin/svcprop -p config/local_only $SMF_FMRI 2>/dev/null`
	if [ $? -eq 0 -a "$local" = "true" ]; then
		MODE="-bl"
	fi
	sendmail_path=`svcprop -p config/path_to_sendmail_mc $SMF_FMRI \
	    2>/dev/null`
	if [ $? -eq 0 -a -n "$sendmail_path" ]; then
		turn_m4_crank $SENDMAIL_CF $sendmail_path
	fi
	exist_or_exit $SENDMAIL_CF
	submit_path=`svcprop -p config/path_to_submit_mc $SMF_FMRI 2>/dev/null`
	if [ $? -eq 0 -a -n "$submit_path" ]; then
		turn_m4_crank $SUBMIT_CF $submit_path
	fi
	exist_or_exit $SUBMIT_CF

	$SENDMAIL $MODE -q$QUEUEOPTION$QUEUEINTERVAL $OPTIONS &
	$SENDMAIL -Ac -q$CLIENTQUEUEINTERVAL $CLIENTOPTIONS &

	#
	# ETRN_HOSTS should be of the form
	# "s1:c1.1,c1.2        s2:c2.1 s3:c3.1,c3.2,c3.3"
	# i.e., white-space separated groups of server:client where
	# client can be one or more comma-separated names; N.B. that
	# the :client part is optional; see etrn(1M) for details.
	# server is the name of the server to prod; a mail queue run
	# is requested for each client name.  This is comparable to
	# running "/usr/lib/sendmail -qRclient" on the host server.
	#
	# See RFC 1985 for more information.
	#
	for i in $ETRN_HOSTS; do
		SERVER=`echo $i | /usr/bin/sed -e 's/:.*$//'`
		CLIENTS=`echo $i | /usr/bin/sed -n -e 's/,/ /g' \
		    -e '/:/s/^.*://p'`
		/usr/sbin/etrn -b $SERVER $CLIENTS >/dev/null 2>&1 &
	done

	if /usr/bin/nawk 'BEGIN{s = 1}
	    $2 == "/var/mail" && $3 == "nfs" && $4 !~ /actimeo=0/ &&
	    $4 !~ /noac/{s = 0} END{exit s}' /etc/mnttab; then

		/usr/bin/logger -p mail.crit "$ERRMSG1"
		/usr/bin/logger -p mail.crit "$ERRMSG2"
	fi
	;;

'stop')
	[ -f $SERVER_PID_FILE ] && check_and_kill $SERVER_PID_FILE
	if [ -f $CLIENT_PID_FILE ]; then
		check_and_kill $CLIENT_PID_FILE
		rm -f $CLIENT_PID_FILE
	fi
	# Need to kill the entire service contract to kill all sendmail related
	# processes
	smf_kill_contract $2 TERM 1 30
	ret=$?
	[ $ret -eq 1 ] && exit 1

	# Since sendmail spawns user processes out of .forward files, it is
	# possible that some of these are not responding to TERM.  If the
	# contract did not empty after TERM, move on to KILL.
	if [ $ret -eq 2 ] ; then
		smf_kill_contract $2 KILL 1
	fi
	;;

*)
	echo "Usage: $0 { start | stop | refresh }"
	exit 1
	;;
esac
exit 0