xref: /netbsd-src/etc/rc.d/postfix (revision c9496f6b604074a9451a67df576a5b423068e71e)
1#!/bin/sh
2#
3# $NetBSD: postfix,v 1.18 2016/10/30 15:47:06 riastradh Exp $
4#
5
6# PROVIDE: mail
7# REQUIRE: LOGIN
8#	we make mail start late, so that things like .forward's are not
9#	processed until the system is fully operational
10
11$_rc_subr_loaded . /etc/rc.subr
12
13name="postfix"
14rcvar=$name
15postfix_command="/usr/sbin/${name}"
16required_files="/etc/${name}/main.cf"
17start_precmd="postfix_precmd"
18start_cmd="postfix_op"
19stop_cmd="postfix_op"
20reload_cmd="postfix_op"
21status_cmd="postfix_op"
22extra_commands="reload status"
23spooletcdir="/var/spool/${name}/etc"
24postconf="/usr/sbin/postconf"
25required_dirs=$spooletcdir
26
27_rebuild() {
28	echo "${name}: rebuilding ${1} ($2 ${1}.db)"
29	$($postconf -h newaliases_path)
30}
31
32postfix_precmd()
33{
34	# As this is called after the is_running and required_dir checks
35	# are made in run_rc_command(), we can safely assume ${spooletcdir}
36	# exists and postfix isn't running at this point (unless forcestart
37	# is used).
38	#
39
40	for f in localtime resolv.conf services; do
41		if [ -f /etc/$f ]; then
42			cmp -s /etc/$f ${spooletcdir}/$f || \
43			    cp -p /etc/$f ${spooletcdir}/$f
44		fi
45	done
46
47	OIFS="${IFS}"
48	IFS="${IFS},"
49	for f in $($postconf -hx alias_database); do
50		IFS="${OIFS}:"
51		set -- $f
52		IFS="${OIFS}"
53		case "$1" in
54		hash)
55			if [ -f "$2.db" ]; then
56				if [ ! "$2" -ot "$2.db" ]; then
57					_rebuild "$2" "out of date"
58				fi
59			else
60				_rebuild "$2" "missing"
61			fi
62			;;
63		*)
64			;;
65		esac
66	done
67}
68
69postfix_op()
70{
71	${postfix_command} ${rc_arg}
72}
73
74check_use_postfix()
75{
76	_mta_path=$(awk '/^sendmail[ \t]/{print$2}' /etc/mailer.conf)
77	_postfix_path="/usr/libexec/postfix/sendmail"
78	_sendmail_path="/usr/libexec/sendmail/sendmail"
79
80	if [ "${postfix}" != "check" ]; then
81	    echo "${postfix}"
82	elif [ "${_mta_path}" = "${_postfix_path}" ]; then
83	    echo YES
84	else
85	    echo "WARNING: default postfix not used as not selected in mailer.conf" >&2
86	    if [ "${_mta_path}" = "${_sendmail_path}" -a \
87		! -x "${_mta_path}" ]; then
88		echo "WARNING: mailer.conf points to the removed sendmail" >&2
89		echo "update /etc/mailer.conf to get a working mailer configuration" >&2
90	    fi
91	    echo NO
92	fi
93}
94
95# force re-evaluation of /etc/rc.conf and resetting of $sendmail
96_rc_conf_loaded=false
97_rc_d_postfix=check
98load_rc_config $name
99unset _rc_d_postfix
100postfix=$(check_use_postfix)
101run_rc_command "$1"
102