xref: /onnv-gate/usr/src/cmd/sendmail/lib/smtp-sendmail (revision 6562:53563fcf544b)
10Sstevel@tonic-gate#!/sbin/sh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
62104Sjjj# Common Development and Distribution License (the "License").
72104Sjjj# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
22*6562Sjbeck# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate# Use is subject to license terms.
240Sstevel@tonic-gate#
250Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
260Sstevel@tonic-gate
270Sstevel@tonic-gate. /lib/svc/share/smf_include.sh
280Sstevel@tonic-gate
290Sstevel@tonic-gateERRMSG1='WARNING: /var/mail is NFS-mounted without setting actimeo=0,'
300Sstevel@tonic-gateERRMSG2='this can cause mailbox locking and access problems.'
310Sstevel@tonic-gateSERVER_PID_FILE="/var/run/sendmail.pid"
320Sstevel@tonic-gateCLIENT_PID_FILE="/var/spool/clientmqueue/sm-client.pid"
330Sstevel@tonic-gateDEFAULT_FILE="/etc/default/sendmail"
340Sstevel@tonic-gateALIASES_FILE="/etc/mail/aliases"
35*6562SjbeckSENDMAIL_CF="/etc/mail/sendmail.cf"
36*6562SjbeckSUBMIT_CF="/etc/mail/submit.cf"
37*6562SjbeckSENDMAIL="/usr/lib/sendmail"
38*6562SjbeckPATH="/usr/bin:/usr/sbin:/usr/ccs/bin"
39*6562Sjbeckexport PATH
400Sstevel@tonic-gate
410Sstevel@tonic-gatecheck_queue_interval_syntax()
420Sstevel@tonic-gate{
430Sstevel@tonic-gate	default="15m"
440Sstevel@tonic-gate	if [ $# -lt 1 ]; then
450Sstevel@tonic-gate		answer=$default
460Sstevel@tonic-gate		return
470Sstevel@tonic-gate	fi
480Sstevel@tonic-gate	if echo $1 | egrep '^([0-9]*[1-9][0-9]*[smhdw])+$' >/dev/null 2>&1; then
490Sstevel@tonic-gate		answer=$1
500Sstevel@tonic-gate	else
510Sstevel@tonic-gate		answer=$default
520Sstevel@tonic-gate	fi
530Sstevel@tonic-gate}
540Sstevel@tonic-gate
550Sstevel@tonic-gatecheck_and_kill()
560Sstevel@tonic-gate{
570Sstevel@tonic-gate	PID=`head -1 $1`
580Sstevel@tonic-gate	kill -0 $PID > /dev/null 2>&1
590Sstevel@tonic-gate	[ $? -eq 0 ] && kill $PID
600Sstevel@tonic-gate}
610Sstevel@tonic-gate
62*6562Sjbeckexist_or_exit()
63*6562Sjbeck{
64*6562Sjbeck	if [ ! -f $1 ]; then
65*6562Sjbeck		echo "$1 does not exist"
66*6562Sjbeck		exit $SMF_EXIT_ERR_CONFIG
67*6562Sjbeck	fi
68*6562Sjbeck}
69*6562Sjbeck
70*6562Sjbeckturn_m4_crank()
71*6562Sjbeck{
72*6562Sjbeck	# expected to be called with two arguments: .cf path & path to m4 file
73*6562Sjbeck	[ $# -lt 2 ] && return
74*6562Sjbeck	cf_path=$1
75*6562Sjbeck	m4_path=$2
76*6562Sjbeck	case "$m4_path" in
77*6562Sjbeck	/*)	;;	# absolute path
78*6562Sjbeck	*)	return;;
79*6562Sjbeck	esac
80*6562Sjbeck	if [ "$m4_path" = "_DONT_TOUCH_THIS" ]; then
81*6562Sjbeck		if [ -f "${cf_path}.old" ]; then
82*6562Sjbeck			mv "$cf_path" "${cf_path}.new"
83*6562Sjbeck			[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
84*6562Sjbeck			mv "${cf_path}.old" "$cf_path"
85*6562Sjbeck			[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
86*6562Sjbeck		fi
87*6562Sjbeck		#
88*6562Sjbeck		# If ${cf_path}.old does not exist, assume it was taken care
89*6562Sjbeck		# of on a previous run.
90*6562Sjbeck		#
91*6562Sjbeck	else
92*6562Sjbeck		exist_or_exit "$m4_path"
93*6562Sjbeck		cd `dirname "$m4_path"`
94*6562Sjbeck		base=`basename "$m4_path"`
95*6562Sjbeck		name=`basename "$m4_path" .mc`
96*6562Sjbeck		info=`svcprop -p config/include_info $SMF_FMRI 2>/dev/null`
97*6562Sjbeck		if [ "$info" = "true" ]; then
98*6562Sjbeck			m4flags=""
99*6562Sjbeck		else
100*6562Sjbeck			m4flags="-DSUN_HIDE_INTERNAL_DETAILS"
101*6562Sjbeck		fi
102*6562Sjbeck		m4 $m4flags /etc/mail/cf/m4/cf.m4 "$base" > "${name}.cf"
103*6562Sjbeck		[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
104*6562Sjbeck		cmp -s "${name}.cf" "$cf_path" || (
105*6562Sjbeck			cp "${name}.cf" "${cf_path}.tmp" &&
106*6562Sjbeck			chown root:bin "${cf_path}.tmp" &&
107*6562Sjbeck			chmod 444 "${cf_path}.tmp" &&
108*6562Sjbeck			mv "${cf_path}.tmp" "$cf_path"
109*6562Sjbeck		)
110*6562Sjbeck		[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
111*6562Sjbeck	fi
112*6562Sjbeck}
113*6562Sjbeck
1140Sstevel@tonic-gatecase "$1" in
1150Sstevel@tonic-gate'refresh')
1160Sstevel@tonic-gate        [ -f $SERVER_PID_FILE ] && kill -1 `head -1 $SERVER_PID_FILE`
1170Sstevel@tonic-gate        [ -f $CLIENT_PID_FILE ] && kill -1 `head -1 $CLIENT_PID_FILE`
1180Sstevel@tonic-gate        ;;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate'start')
121*6562Sjbeck	exist_or_exit $SENDMAIL
1220Sstevel@tonic-gate	if [ ! -d /var/spool/mqueue ]; then
1230Sstevel@tonic-gate		/usr/bin/mkdir -m 0750 /var/spool/mqueue
1240Sstevel@tonic-gate		/usr/bin/chown root:bin /var/spool/mqueue
1250Sstevel@tonic-gate	fi
1260Sstevel@tonic-gate	if [ ! -f $ALIASES_FILE.db ] && [ ! -f $ALIASES_FILE.dir ] \
1270Sstevel@tonic-gate	    && [ ! -f $ALIASES_FILE.pag ]; then
1280Sstevel@tonic-gate		/usr/sbin/newaliases
1290Sstevel@tonic-gate	fi
1300Sstevel@tonic-gate	MODE="-bd"
1310Sstevel@tonic-gate	[ -f $DEFAULT_FILE ] && . $DEFAULT_FILE
1320Sstevel@tonic-gate	#
1330Sstevel@tonic-gate	# * MODE should be "-bd" or null (MODE= or MODE="") or
1340Sstevel@tonic-gate	#   left alone.  Anything else and you're on your own.
1350Sstevel@tonic-gate	# * QUEUEOPTION should be "p" or null (as above).
1360Sstevel@tonic-gate	# * [CLIENT]QUEUEINTERVAL should be set to some legal value;
1370Sstevel@tonic-gate	#   sanity checks are done below.
1380Sstevel@tonic-gate	# * [CLIENT]OPTIONS are catch-alls; set with care.
1390Sstevel@tonic-gate	#
1400Sstevel@tonic-gate	if [ -n "$QUEUEOPTION" -a "$QUEUEOPTION" != "p" ]; then
1410Sstevel@tonic-gate		QUEUEOPTION=""
1420Sstevel@tonic-gate	fi
1430Sstevel@tonic-gate	if [ -z "$QUEUEOPTION" -o -n "$QUEUEINTERVAL" ]; then
1440Sstevel@tonic-gate		check_queue_interval_syntax $QUEUEINTERVAL
1450Sstevel@tonic-gate		QUEUEINTERVAL=$answer
1460Sstevel@tonic-gate	fi
1470Sstevel@tonic-gate	check_queue_interval_syntax $CLIENTQUEUEINTERVAL
1480Sstevel@tonic-gate	CLIENTQUEUEINTERVAL=$answer
1492104Sjjj
1502104Sjjj	local=`/usr/bin/svcprop -p config/local_only $SMF_FMRI 2>/dev/null`
151*6562Sjbeck	if [ $? -eq 0 -a "$local" = "true" ]; then
152*6562Sjbeck		MODE="-bl"
153*6562Sjbeck	fi
154*6562Sjbeck	sendmail_path=`svcprop -p config/path_to_sendmail_mc $SMF_FMRI \
155*6562Sjbeck	    2>/dev/null`
156*6562Sjbeck	if [ $? -eq 0 -a -n "$sendmail_path" ]; then
157*6562Sjbeck		turn_m4_crank $SENDMAIL_CF $sendmail_path
1582104Sjjj	fi
159*6562Sjbeck	exist_or_exit $SENDMAIL_CF
160*6562Sjbeck	submit_path=`svcprop -p config/path_to_submit_mc $SMF_FMRI 2>/dev/null`
161*6562Sjbeck	if [ $? -eq 0 -a -n "$submit_path" ]; then
162*6562Sjbeck		turn_m4_crank $SUBMIT_CF $submit_path
163*6562Sjbeck	fi
164*6562Sjbeck	exist_or_exit $SUBMIT_CF
1652104Sjjj
166*6562Sjbeck	$SENDMAIL $MODE -q$QUEUEOPTION$QUEUEINTERVAL $OPTIONS &
167*6562Sjbeck	$SENDMAIL -Ac -q$CLIENTQUEUEINTERVAL $CLIENTOPTIONS &
1682104Sjjj
1690Sstevel@tonic-gate	#
1700Sstevel@tonic-gate	# ETRN_HOSTS should be of the form
1710Sstevel@tonic-gate	# "s1:c1.1,c1.2        s2:c2.1 s3:c3.1,c3.2,c3.3"
1720Sstevel@tonic-gate	# i.e., white-space separated groups of server:client where
1730Sstevel@tonic-gate	# client can be one or more comma-separated names; N.B. that
1740Sstevel@tonic-gate	# the :client part is optional; see etrn(1M) for details.
1750Sstevel@tonic-gate	# server is the name of the server to prod; a mail queue run
1760Sstevel@tonic-gate	# is requested for each client name.  This is comparable to
1770Sstevel@tonic-gate	# running "/usr/lib/sendmail -qRclient" on the host server.
1780Sstevel@tonic-gate	#
1790Sstevel@tonic-gate	# See RFC 1985 for more information.
1800Sstevel@tonic-gate	#
1810Sstevel@tonic-gate	for i in $ETRN_HOSTS; do
1820Sstevel@tonic-gate		SERVER=`echo $i | /usr/bin/sed -e 's/:.*$//'`
1830Sstevel@tonic-gate		CLIENTS=`echo $i | /usr/bin/sed -n -e 's/,/ /g' \
1840Sstevel@tonic-gate		    -e '/:/s/^.*://p'`
1850Sstevel@tonic-gate		/usr/sbin/etrn -b $SERVER $CLIENTS >/dev/null 2>&1 &
1860Sstevel@tonic-gate	done
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate	if /usr/bin/nawk 'BEGIN{s = 1}
1890Sstevel@tonic-gate	    $2 == "/var/mail" && $3 == "nfs" && $4 !~ /actimeo=0/ &&
1900Sstevel@tonic-gate	    $4 !~ /noac/{s = 0} END{exit s}' /etc/mnttab; then
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate		/usr/bin/logger -p mail.crit "$ERRMSG1"
1930Sstevel@tonic-gate		/usr/bin/logger -p mail.crit "$ERRMSG2"
1940Sstevel@tonic-gate	fi
1950Sstevel@tonic-gate	;;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate'stop')
1980Sstevel@tonic-gate	[ -f $SERVER_PID_FILE ] && check_and_kill $SERVER_PID_FILE
1990Sstevel@tonic-gate	if [ -f $CLIENT_PID_FILE ]; then
2000Sstevel@tonic-gate		check_and_kill $CLIENT_PID_FILE
2010Sstevel@tonic-gate		rm -f $CLIENT_PID_FILE
2020Sstevel@tonic-gate	fi
2030Sstevel@tonic-gate	# Need to kill the entire service contract to kill all sendmail related
2040Sstevel@tonic-gate	# processes
2050Sstevel@tonic-gate	smf_kill_contract $2 TERM 1 30
2060Sstevel@tonic-gate	ret=$?
2070Sstevel@tonic-gate	[ $ret -eq 1 ] && exit 1
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate	# Since sendmail spawns user processes out of .forward files, it is
2100Sstevel@tonic-gate	# possible that some of these are not responding to TERM.  If the
2110Sstevel@tonic-gate	# contract did not empty after TERM, move on to KILL.
2120Sstevel@tonic-gate	if [ $ret -eq 2 ] ; then
2130Sstevel@tonic-gate		smf_kill_contract $2 KILL 1
2140Sstevel@tonic-gate	fi
2150Sstevel@tonic-gate	;;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate*)
2180Sstevel@tonic-gate	echo "Usage: $0 { start | stop | refresh }"
2190Sstevel@tonic-gate	exit 1
2200Sstevel@tonic-gate	;;
2210Sstevel@tonic-gateesac
2220Sstevel@tonic-gateexit 0
223