xref: /illumos-gate/usr/src/cmd/sendmail/lib/smtp-sendmail (revision d4660949aa62dd6a963f4913b7120b383cf473c4)
17c478bd9Sstevel@tonic-gate#!/sbin/sh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
60ea5e3a5Sjjj# Common Development and Distribution License (the "License").
70ea5e3a5Sjjj# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
22*d4660949Sjbeck# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate# Use is subject to license terms.
247c478bd9Sstevel@tonic-gate#
257c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate. /lib/svc/share/smf_include.sh
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gateERRMSG1='WARNING: /var/mail is NFS-mounted without setting actimeo=0,'
307c478bd9Sstevel@tonic-gateERRMSG2='this can cause mailbox locking and access problems.'
317c478bd9Sstevel@tonic-gateSERVER_PID_FILE="/var/run/sendmail.pid"
327c478bd9Sstevel@tonic-gateCLIENT_PID_FILE="/var/spool/clientmqueue/sm-client.pid"
337c478bd9Sstevel@tonic-gateDEFAULT_FILE="/etc/default/sendmail"
347c478bd9Sstevel@tonic-gateALIASES_FILE="/etc/mail/aliases"
35*d4660949SjbeckSENDMAIL_CF="/etc/mail/sendmail.cf"
36*d4660949SjbeckSUBMIT_CF="/etc/mail/submit.cf"
37*d4660949SjbeckSENDMAIL="/usr/lib/sendmail"
38*d4660949SjbeckPATH="/usr/bin:/usr/sbin:/usr/ccs/bin"
39*d4660949Sjbeckexport PATH
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gatecheck_queue_interval_syntax()
427c478bd9Sstevel@tonic-gate{
437c478bd9Sstevel@tonic-gate	default="15m"
447c478bd9Sstevel@tonic-gate	if [ $# -lt 1 ]; then
457c478bd9Sstevel@tonic-gate		answer=$default
467c478bd9Sstevel@tonic-gate		return
477c478bd9Sstevel@tonic-gate	fi
487c478bd9Sstevel@tonic-gate	if echo $1 | egrep '^([0-9]*[1-9][0-9]*[smhdw])+$' >/dev/null 2>&1; then
497c478bd9Sstevel@tonic-gate		answer=$1
507c478bd9Sstevel@tonic-gate	else
517c478bd9Sstevel@tonic-gate		answer=$default
527c478bd9Sstevel@tonic-gate	fi
537c478bd9Sstevel@tonic-gate}
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gatecheck_and_kill()
567c478bd9Sstevel@tonic-gate{
577c478bd9Sstevel@tonic-gate	PID=`head -1 $1`
587c478bd9Sstevel@tonic-gate	kill -0 $PID > /dev/null 2>&1
597c478bd9Sstevel@tonic-gate	[ $? -eq 0 ] && kill $PID
607c478bd9Sstevel@tonic-gate}
617c478bd9Sstevel@tonic-gate
62*d4660949Sjbeckexist_or_exit()
63*d4660949Sjbeck{
64*d4660949Sjbeck	if [ ! -f $1 ]; then
65*d4660949Sjbeck		echo "$1 does not exist"
66*d4660949Sjbeck		exit $SMF_EXIT_ERR_CONFIG
67*d4660949Sjbeck	fi
68*d4660949Sjbeck}
69*d4660949Sjbeck
70*d4660949Sjbeckturn_m4_crank()
71*d4660949Sjbeck{
72*d4660949Sjbeck	# expected to be called with two arguments: .cf path & path to m4 file
73*d4660949Sjbeck	[ $# -lt 2 ] && return
74*d4660949Sjbeck	cf_path=$1
75*d4660949Sjbeck	m4_path=$2
76*d4660949Sjbeck	case "$m4_path" in
77*d4660949Sjbeck	/*)	;;	# absolute path
78*d4660949Sjbeck	*)	return;;
79*d4660949Sjbeck	esac
80*d4660949Sjbeck	if [ "$m4_path" = "_DONT_TOUCH_THIS" ]; then
81*d4660949Sjbeck		if [ -f "${cf_path}.old" ]; then
82*d4660949Sjbeck			mv "$cf_path" "${cf_path}.new"
83*d4660949Sjbeck			[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
84*d4660949Sjbeck			mv "${cf_path}.old" "$cf_path"
85*d4660949Sjbeck			[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
86*d4660949Sjbeck		fi
87*d4660949Sjbeck		#
88*d4660949Sjbeck		# If ${cf_path}.old does not exist, assume it was taken care
89*d4660949Sjbeck		# of on a previous run.
90*d4660949Sjbeck		#
91*d4660949Sjbeck	else
92*d4660949Sjbeck		exist_or_exit "$m4_path"
93*d4660949Sjbeck		cd `dirname "$m4_path"`
94*d4660949Sjbeck		base=`basename "$m4_path"`
95*d4660949Sjbeck		name=`basename "$m4_path" .mc`
96*d4660949Sjbeck		info=`svcprop -p config/include_info $SMF_FMRI 2>/dev/null`
97*d4660949Sjbeck		if [ "$info" = "true" ]; then
98*d4660949Sjbeck			m4flags=""
99*d4660949Sjbeck		else
100*d4660949Sjbeck			m4flags="-DSUN_HIDE_INTERNAL_DETAILS"
101*d4660949Sjbeck		fi
102*d4660949Sjbeck		m4 $m4flags /etc/mail/cf/m4/cf.m4 "$base" > "${name}.cf"
103*d4660949Sjbeck		[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
104*d4660949Sjbeck		cmp -s "${name}.cf" "$cf_path" || (
105*d4660949Sjbeck			cp "${name}.cf" "${cf_path}.tmp" &&
106*d4660949Sjbeck			chown root:bin "${cf_path}.tmp" &&
107*d4660949Sjbeck			chmod 444 "${cf_path}.tmp" &&
108*d4660949Sjbeck			mv "${cf_path}.tmp" "$cf_path"
109*d4660949Sjbeck		)
110*d4660949Sjbeck		[ $? -ne 0 ] && exit $SMF_EXIT_ERR_CONFIG
111*d4660949Sjbeck	fi
112*d4660949Sjbeck}
113*d4660949Sjbeck
1147c478bd9Sstevel@tonic-gatecase "$1" in
1157c478bd9Sstevel@tonic-gate'refresh')
1167c478bd9Sstevel@tonic-gate        [ -f $SERVER_PID_FILE ] && kill -1 `head -1 $SERVER_PID_FILE`
1177c478bd9Sstevel@tonic-gate        [ -f $CLIENT_PID_FILE ] && kill -1 `head -1 $CLIENT_PID_FILE`
1187c478bd9Sstevel@tonic-gate        ;;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate'start')
121*d4660949Sjbeck	exist_or_exit $SENDMAIL
1227c478bd9Sstevel@tonic-gate	if [ ! -d /var/spool/mqueue ]; then
1237c478bd9Sstevel@tonic-gate		/usr/bin/mkdir -m 0750 /var/spool/mqueue
1247c478bd9Sstevel@tonic-gate		/usr/bin/chown root:bin /var/spool/mqueue
1257c478bd9Sstevel@tonic-gate	fi
1267c478bd9Sstevel@tonic-gate	if [ ! -f $ALIASES_FILE.db ] && [ ! -f $ALIASES_FILE.dir ] \
1277c478bd9Sstevel@tonic-gate	    && [ ! -f $ALIASES_FILE.pag ]; then
1287c478bd9Sstevel@tonic-gate		/usr/sbin/newaliases
1297c478bd9Sstevel@tonic-gate	fi
1307c478bd9Sstevel@tonic-gate	MODE="-bd"
1317c478bd9Sstevel@tonic-gate	[ -f $DEFAULT_FILE ] && . $DEFAULT_FILE
1327c478bd9Sstevel@tonic-gate	#
1337c478bd9Sstevel@tonic-gate	# * MODE should be "-bd" or null (MODE= or MODE="") or
1347c478bd9Sstevel@tonic-gate	#   left alone.  Anything else and you're on your own.
1357c478bd9Sstevel@tonic-gate	# * QUEUEOPTION should be "p" or null (as above).
1367c478bd9Sstevel@tonic-gate	# * [CLIENT]QUEUEINTERVAL should be set to some legal value;
1377c478bd9Sstevel@tonic-gate	#   sanity checks are done below.
1387c478bd9Sstevel@tonic-gate	# * [CLIENT]OPTIONS are catch-alls; set with care.
1397c478bd9Sstevel@tonic-gate	#
1407c478bd9Sstevel@tonic-gate	if [ -n "$QUEUEOPTION" -a "$QUEUEOPTION" != "p" ]; then
1417c478bd9Sstevel@tonic-gate		QUEUEOPTION=""
1427c478bd9Sstevel@tonic-gate	fi
1437c478bd9Sstevel@tonic-gate	if [ -z "$QUEUEOPTION" -o -n "$QUEUEINTERVAL" ]; then
1447c478bd9Sstevel@tonic-gate		check_queue_interval_syntax $QUEUEINTERVAL
1457c478bd9Sstevel@tonic-gate		QUEUEINTERVAL=$answer
1467c478bd9Sstevel@tonic-gate	fi
1477c478bd9Sstevel@tonic-gate	check_queue_interval_syntax $CLIENTQUEUEINTERVAL
1487c478bd9Sstevel@tonic-gate	CLIENTQUEUEINTERVAL=$answer
1490ea5e3a5Sjjj
1500ea5e3a5Sjjj	local=`/usr/bin/svcprop -p config/local_only $SMF_FMRI 2>/dev/null`
151*d4660949Sjbeck	if [ $? -eq 0 -a "$local" = "true" ]; then
152*d4660949Sjbeck		MODE="-bl"
1530ea5e3a5Sjjj	fi
154*d4660949Sjbeck	sendmail_path=`svcprop -p config/path_to_sendmail_mc $SMF_FMRI \
155*d4660949Sjbeck	    2>/dev/null`
156*d4660949Sjbeck	if [ $? -eq 0 -a -n "$sendmail_path" ]; then
157*d4660949Sjbeck		turn_m4_crank $SENDMAIL_CF $sendmail_path
158*d4660949Sjbeck	fi
159*d4660949Sjbeck	exist_or_exit $SENDMAIL_CF
160*d4660949Sjbeck	submit_path=`svcprop -p config/path_to_submit_mc $SMF_FMRI 2>/dev/null`
161*d4660949Sjbeck	if [ $? -eq 0 -a -n "$submit_path" ]; then
162*d4660949Sjbeck		turn_m4_crank $SUBMIT_CF $submit_path
163*d4660949Sjbeck	fi
164*d4660949Sjbeck	exist_or_exit $SUBMIT_CF
1650ea5e3a5Sjjj
166*d4660949Sjbeck	$SENDMAIL $MODE -q$QUEUEOPTION$QUEUEINTERVAL $OPTIONS &
167*d4660949Sjbeck	$SENDMAIL -Ac -q$CLIENTQUEUEINTERVAL $CLIENTOPTIONS &
1680ea5e3a5Sjjj
1697c478bd9Sstevel@tonic-gate	#
1707c478bd9Sstevel@tonic-gate	# ETRN_HOSTS should be of the form
1717c478bd9Sstevel@tonic-gate	# "s1:c1.1,c1.2        s2:c2.1 s3:c3.1,c3.2,c3.3"
1727c478bd9Sstevel@tonic-gate	# i.e., white-space separated groups of server:client where
1737c478bd9Sstevel@tonic-gate	# client can be one or more comma-separated names; N.B. that
1747c478bd9Sstevel@tonic-gate	# the :client part is optional; see etrn(1M) for details.
1757c478bd9Sstevel@tonic-gate	# server is the name of the server to prod; a mail queue run
1767c478bd9Sstevel@tonic-gate	# is requested for each client name.  This is comparable to
1777c478bd9Sstevel@tonic-gate	# running "/usr/lib/sendmail -qRclient" on the host server.
1787c478bd9Sstevel@tonic-gate	#
1797c478bd9Sstevel@tonic-gate	# See RFC 1985 for more information.
1807c478bd9Sstevel@tonic-gate	#
1817c478bd9Sstevel@tonic-gate	for i in $ETRN_HOSTS; do
1827c478bd9Sstevel@tonic-gate		SERVER=`echo $i | /usr/bin/sed -e 's/:.*$//'`
1837c478bd9Sstevel@tonic-gate		CLIENTS=`echo $i | /usr/bin/sed -n -e 's/,/ /g' \
1847c478bd9Sstevel@tonic-gate		    -e '/:/s/^.*://p'`
1857c478bd9Sstevel@tonic-gate		/usr/sbin/etrn -b $SERVER $CLIENTS >/dev/null 2>&1 &
1867c478bd9Sstevel@tonic-gate	done
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate	if /usr/bin/nawk 'BEGIN{s = 1}
1897c478bd9Sstevel@tonic-gate	    $2 == "/var/mail" && $3 == "nfs" && $4 !~ /actimeo=0/ &&
1907c478bd9Sstevel@tonic-gate	    $4 !~ /noac/{s = 0} END{exit s}' /etc/mnttab; then
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate		/usr/bin/logger -p mail.crit "$ERRMSG1"
1937c478bd9Sstevel@tonic-gate		/usr/bin/logger -p mail.crit "$ERRMSG2"
1947c478bd9Sstevel@tonic-gate	fi
1957c478bd9Sstevel@tonic-gate	;;
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate'stop')
1987c478bd9Sstevel@tonic-gate	[ -f $SERVER_PID_FILE ] && check_and_kill $SERVER_PID_FILE
1997c478bd9Sstevel@tonic-gate	if [ -f $CLIENT_PID_FILE ]; then
2007c478bd9Sstevel@tonic-gate		check_and_kill $CLIENT_PID_FILE
2017c478bd9Sstevel@tonic-gate		rm -f $CLIENT_PID_FILE
2027c478bd9Sstevel@tonic-gate	fi
2037c478bd9Sstevel@tonic-gate	# Need to kill the entire service contract to kill all sendmail related
2047c478bd9Sstevel@tonic-gate	# processes
2057c478bd9Sstevel@tonic-gate	smf_kill_contract $2 TERM 1 30
2067c478bd9Sstevel@tonic-gate	ret=$?
2077c478bd9Sstevel@tonic-gate	[ $ret -eq 1 ] && exit 1
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate	# Since sendmail spawns user processes out of .forward files, it is
2107c478bd9Sstevel@tonic-gate	# possible that some of these are not responding to TERM.  If the
2117c478bd9Sstevel@tonic-gate	# contract did not empty after TERM, move on to KILL.
2127c478bd9Sstevel@tonic-gate	if [ $ret -eq 2 ] ; then
2137c478bd9Sstevel@tonic-gate		smf_kill_contract $2 KILL 1
2147c478bd9Sstevel@tonic-gate	fi
2157c478bd9Sstevel@tonic-gate	;;
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate*)
2187c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop | refresh }"
2197c478bd9Sstevel@tonic-gate	exit 1
2207c478bd9Sstevel@tonic-gate	;;
2217c478bd9Sstevel@tonic-gateesac
2227c478bd9Sstevel@tonic-gateexit 0
223