xref: /netbsd-src/etc/rc.d/postfix (revision b151f12f8e12511f967454ca3bf61f995dcb83cc)
1#!/bin/sh
2#
3# $NetBSD: postfix,v 1.20 2023/12/25 08:37:08 kre 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	echo "Starting ${name}."
69}
70
71postfix_op()
72{
73	${postfix_command} ${rc_arg}
74}
75
76check_use_postfix()
77{
78	_mta_path=$(awk '/^sendmail[ \t]/{print$2}' /etc/mailer.conf)
79	_postfix_path="/usr/libexec/postfix/sendmail"
80	_sendmail_path="/usr/libexec/sendmail/sendmail"
81
82	if [ "${postfix}" != "check" ]; then
83	    echo "${postfix}"
84	elif [ "${_mta_path}" = "${_postfix_path}" ]; then
85	    echo YES
86	else
87	    echo "WARNING: default postfix not used as not selected in mailer.conf" >&2
88	    if [ "${_mta_path}" = "${_sendmail_path}" ] &&
89	     ! [ -x "${_mta_path}" ]; then
90		echo "WARNING: mailer.conf points to the removed sendmail" >&2
91		echo "update /etc/mailer.conf to get a working mailer configuration" >&2
92	    fi
93	    echo NO
94	fi
95}
96
97# force re-evaluation of /etc/rc.conf and resetting of $sendmail
98_rc_conf_loaded=false
99_rc_d_postfix=check
100load_rc_config $name
101unset _rc_d_postfix
102postfix=$(check_use_postfix)
103run_rc_command "$1"
104