Import Debian changes 4.92-8+deb10u3
[hcoop/debian/exim4.git] / debian / debconf / update-exim4.conf
CommitLineData
de45f55a
AM
1#!/bin/sh
2# update-exim4.conf(8) - Generate /var/lib/exim4/config.autogenerated
3
4set -e
5set -C
6set -f
7
8UPEX4C_confdir="/etc/exim4"
9UPEX4C_sections="main acl router transport retry rewrite auth"
10
11# list of ue4cc options that need to support both colons and
12# semicolons as separators. dc_other_hostnames and dc_smarthost
13# has special handling.
14UPEX4C_semicolon="dc_local_interfaces dc_relay_nets dc_relay_domains"
15EXIM="/usr/sbin/exim4"
16
17UPEX4C_verbose=no
18UPEX4C_autoconfigfile=/var/lib/exim4/config.autogenerated
19UPEX4C_outputfile="${UPEX4C_autoconfigfile}"
20UPEX4C_version=""
21
22usage() {
23cat <<EOF
24$0 - Generate exim4 configuration files
25 Options:
26 -v|--verbose - Enable verbose mode, tell about ignored files
27 -h|--help - Show this message
28 --keepcomments - Do not remove comment lines
29 --removecomments - Remove comment lines
30 -o|--output file - write output to file instead of ${UPEX4C_outputfile}
31 -d|--confdir directory - read input from given directory instead of ${UPEX4C_confdir}
0baa7b9d 32 --check - Test generated file for validity and remove it again.
de45f55a
AM
33EOF
34}
35
36## Parse commandline
37TEMP=$(getopt -n update-exim4.conf \
0baa7b9d 38 -l check,keepcomments,removecomments,output:,confdir:,help,verbose -- \
de45f55a
AM
39 +o:d:vh "$@")
40
41if test "$?" != 0; then
42 echo "Terminating..." >&2
43 exit 1
44fi
45
46eval set -- ${TEMP}
47while test "$1" != "--"; do
48 case $1 in
49 -h|--help)
50 usage
51 exit 0
52 ;;
53 -v|--verbose)
54 UPEX4C_verbose=yes
55 ;;
56 --keepcomments)
57 UPEX4C_comments=yes
58 ;;
59 --removecomments)
60 UPEX4C_comments=no
61 ;;
0baa7b9d
SB
62 --check)
63 UPEX4C_check=yes
64 ;;
de45f55a
AM
65 -o|--output)
66 shift
67 UPEX4C_outputfile="$1"
68 ;;
69 -d|--confdir)
70 shift
71 UPEX4C_confdir="$1"
72 ;;
73 esac
74 shift
75done
76shift
77
78# No non-option arguments allowed.
79if [ "$#" -ne 0 ]; then
80 echo "No non option arguments ($@) allowed" >&2
81 usage >&2
82 exit 1
83fi
84
85# exit immediately if /etc/exim4/exim4.conf exists and -o was not specified
86if [ -e /etc/exim4/exim4.conf ] && \
87 [ "${UPEX4C_outputfile}" = "${UPEX4C_autoconfigfile}" ] ; then
88 exit 0
89fi
90
91UE4CC="$UPEX4C_confdir/update-exim4.conf.conf"
92UPEX4C_confd="$UPEX4C_confdir/conf.d"
93
94[ -d "$(dirname "$UPEX4C_outputfile")" ] || \
95{ printf "$0: Error, missing $(dirname "$UPEX4C_outputfile"), exiting.\n" 1>&2 ; exit 1 ; }
96
97if [ -f "$UE4CC" ]; then
98 . "$UE4CC"
99else
100 echo >&2 "$0: Error, no $UE4CC, exiting."
101 exit 1
102fi
103
0baa7b9d
SB
104
105UPEX4C_autoconfigfile=/var/lib/exim4/config.autogenerated
106if [ "$(dirname ${UPEX4C_outputfile})" = "/var/lib/exim4" ] ; then
107 UPEX4C_tmp="${UPEX4C_outputfile}.tmp"
108else
109 UPEX4C_tmp="$(tempfile -m600 -p ex4)"
110fi
111
de45f55a
AM
112lowerpipe() {
113 tr 'A-Z' 'a-z'
114}
115
116lowercase() {
117 echo "$*" | lowerpipe
118}
119
120check_ascii_pipe() {
121 IN="$(cat)"
122 # Use "abcdef... instead of a a-z or [:alnum:] here since the alternatives
123 # will also match non-ascii characters.
124 OUT="$(echo $IN | sed 's/[^-0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\/\.!*@_~:;< \[\]]/_/g')"
125 if [ "$OUT" != "$IN" ]; then
126 echo >&2 "$0: non-ascii value $IN read from $UE4CC, sanitizing to $OUT"
127 fi
128 echo $OUT
129}
130
131[ "${CFILEMODE}" = "" ] && CFILEMODE=644
132[ "${dc_use_split_config}" = "" ] && dc_use_split_config='false'
133[ "${dc_localdelivery}" = "" ] && dc_localdelivery='mail_spool'
134[ "${UPEX4C_comments:-}" = "" ] && UPEX4C_comments="${ue4c_keepcomments:-no}"
135
136TEMPLATEFILE="${UPEX4C_confdir}/exim4.conf.template"
137
138dc_use_split_config="$(lowercase $dc_use_split_config)"
139UPEX4C_verbose="$(lowercase $UPEX4C_verbose)"
140
141if [ "${dc_use_split_config}" = "true" ]; then
142 [ "${UPEX4C_verbose}" = "yes" ] && \
143 echo "using split configuration scheme from ${UPEX4C_confd}"
144 if ! [ -d "${UPEX4C_confd}" ]; then
145 printf >&2 "$0: Error, no ${UPEX4C_confd}, exiting.\n"
146 exit 1
147 fi
148else
149 [ "${UPEX4C_verbose}" = "yes" ] && \
150 echo "using non-split configuration scheme from ${TEMPLATEFILE}"
151fi
152
153# take only the first word from /etc/mailname
154mailname="$(< /etc/mailname sed -n 's/\([-[:alnum:]@\.]\+\).*/\1/;p;q' | lowerpipe | check_ascii_pipe)"
155
156# barf if lookups are found. They have never been supported here.
157if echo " ${dc_other_hostnames} ${dc_smarthost} ${dc_local_interfaces} ${dc_relay_nets} ${dc_relay_domains}"| grep -q '[[:space:]]\(partial-\)\?\(cdb\|dbm\|dbmnz\|\(d\|ipl\|\(n\?wild\)\?l\)search\|nis\)\([\*@]\)\?[[:space:]]*;'; then
158 echo >&2 "WARNING: using 'lookup;' constructs in $UE4CC has never been supported! See /usr/share/doc/exim4-config/NEWS.Debian.gz for details."
159fi
160
161dc_other_hostnames="$(lowercase $dc_other_hostnames | check_ascii_pipe)"
162# add localhost, get rid of spaces, trailing (semi)colons and make the list
163# colon separated
164local_domains="$(echo @:localhost:"${dc_other_hostnames}" | \
165 sed -e 's/[;: ]*$//' -e 's/ *//' -e 's/;/:/g')"
166
de45f55a
AM
167
168# run-parts emulation, stolen from Branden's /etc/X11/Xsession
169# Addition: Use file.rul instead if file if it exists.
170run_parts () {
171 # reset LC_COLLATE
172 unset LANG LC_COLLATE LC_ALL
173
174 if [ -z "$1" ]; then
175 errormessage "$0: internal run_parts called without an argument"
176 fi
177 if [ ! -d "$1" ]; then
178 errormessage "$0: internal run_parts called, but $1 does not exist or is not a directory."
179 fi
180 for F in $(ls $1); do
181 if expr "$F" : '[[:alnum:]_-]\+$' > /dev/null 2>&1; then
182 if [ -f "$1/$F" ] ; then
183 if [ -f "$1/${F}.rul" ] ; then
184 echo "$1/${F}.rul"
185 else
186 echo "$1/$F"
187 fi
188 fi
189 else
190 if [ "${UPEX4C_verbose}" = "yes" ] && \
191 [ -f "$1/$F" ] && \
192 ! expr "$F" : '[[:alnum:]_-]\+\.rul'> /dev/null 2>&1 ; then
193 echo \
194 "internal run-parts: ignoring file: $1/$F" 1>&2
195 fi
196 fi
197 done;
198}
199# also from Branden
200errormessage () {
201 # pretty-print messages of arbitrary length (no trailing newline)
202 echo "$*" | fold -s -w ${COLUMNS:-80} >&2;
203}
204
205cat_parts() {
206 if [ -z "$1" ]; then
207 errormessage "$0: internal cat_parts called without an argument"
208 fi
209 if [ ! -d "$1" ]; then
210 errormessage "$0: internal cat_parts called, but $1 does not exist or is not a directory."
211 fi
212 for file in $(run_parts $1); do
213 echo "#####################################################"
214 echo "### $file"
215 echo "#####################################################"
216 cat "$file"
217 echo
218 echo "#####################################################"
219 echo "### end $file"
220 echo "#####################################################"
221 done
222}
223
224gentmpconf() {
0baa7b9d
SB
225 rm -f "${UPEX4C_tmp}"
226 touch "${UPEX4C_tmp}"
de45f55a
AM
227 # this can be removed by the end of 2007
228 #chown --reference=${TEMPLATEFILE} \
0baa7b9d 229 # ${UPEX4C_tmp} ${UPEX4C_outputfile}
de45f55a 230 #chmod --reference=${TEMPLATEFILE} \
0baa7b9d 231 # ${UPEX4C_tmp} ${UPEX4C_outputfile}
de45f55a 232 if [ "$(id -u)" = "0" ]; then
0baa7b9d 233 chown root:Debian-exim "${UPEX4C_tmp}"
de45f55a
AM
234 [ -e "${UPEX4C_outputfile}" ] && \
235 chown root:Debian-exim "${UPEX4C_outputfile}"
236 fi
0baa7b9d 237 chmod 640 "${UPEX4C_tmp}"
de45f55a
AM
238 if [ -e "${UPEX4C_outputfile}" ]; then
239 chmod 640 "${UPEX4C_outputfile}"
240 fi
241}
242
243removecomments(){
244 if [ "${UPEX4C_comments}" = "no" ] ; then
245 grep -E -v '^[[:space:]]*#' | sed -e '/^$/N;/\n$/D' ;
246 else
247 cat
248 fi
249}
250
251gentmpconf
252
0baa7b9d 253cat << EOF >> "${UPEX4C_tmp}"
de45f55a
AM
254#########
255# WARNING WARNING WARNING
256# WARNING WARNING WARNING
257# WARNING WARNING WARNING
258# WARNING WARNING WARNING
259# WARNING WARNING WARNING
260# This file was generated dynamically from
261EOF
262
263if [ "${dc_use_split_config}" = "true" ] ; then
0baa7b9d 264cat << EOF >> "${UPEX4C_tmp}"
de45f55a
AM
265# split config files in the $UPEX4C_confd/ directory.
266EOF
267else
0baa7b9d 268cat << EOF >> "${UPEX4C_tmp}"
de45f55a
AM
269# non-split config ($UPEX4C_confdir/exim4.conf.localmacros
270# and $UPEX4C_confdir/exim4.conf.template).
271EOF
272fi
273
0baa7b9d 274cat << EOF >> "${UPEX4C_tmp}"
de45f55a
AM
275# The config files are supplemented with package installation/configuration
276# settings managed by debconf. This data is stored in
277# $UPEX4C_confdir/update-exim4.conf.conf
278# Any changes you make here will be lost.
279# See /usr/share/doc/exim4-base/README.Debian.gz and update-exim4.conf(8)
280# for instructions of customization.
281# WARNING WARNING WARNING
282# WARNING WARNING WARNING
283# WARNING WARNING WARNING
284# WARNING WARNING WARNING
285# WARNING WARNING WARNING
286#########
287EOF
288
289# handle ";" in input values as separator change
290
291for field in $UPEX4C_semicolon; do
292 if eval echo \$$field | grep -q ";"; then
293 eval temp=\$$field
294 if ! echo $temp | grep -q "^<"; then
295 temp="<; $temp"
296 eval "$field='$temp'"
297 fi
298 fi
299done
300
301# fix up smarthost line: change semicolons into single colons
302dc_smarthost="$(lowercase $dc_smarthost | check_ascii_pipe | sed 's/;/:/g')"
303
304dc_relay_nets="$(lowercase $dc_relay_nets | check_ascii_pipe)"
305
306if echo "$dc_relay_nets" | grep -q '^<;'; then
307 dc_relay_nets="$dc_relay_nets ; 127.0.0.1 ; ::1"
308else
309 dc_relay_nets="$dc_relay_nets : 127.0.0.1 : ::::1"
310fi
311
312dc_eximconfig_configtype="$(lowercase $dc_eximconfig_configtype | check_ascii_pipe)"
313dc_hide_mailname="$(lowercase $dc_hide_mailname | check_ascii_pipe)"
314dc_readhost="$(lowercase $dc_readhost | check_ascii_pipe)"
315case "$dc_eximconfig_configtype" in
316 satellite|smarthost)
317 if [ "${dc_hide_mailname}" = "true" ] && [ -n "${dc_readhost}" ] ; then
318 hide_mailname=1
319 fi
320 ;;
321 local)
322 ;;
323 internet)
324 ;;
325 none|*)
326 if [ "${dc_use_split_config}" = "true" ] ; then
327 for i in ${UPEX4C_sections} ; do
328 cat_parts "${UPEX4C_confd}/$i"
329 done | \
330 removecomments \
0baa7b9d 331 >> "${UPEX4C_tmp}"
de45f55a
AM
332 else
333 LOCALMACROS=""
334 if [ -e "/etc/exim4/exim4.conf.localmacros" ]; then
335 LOCALMACROS="/etc/exim4/exim4.conf.localmacros"
336 fi
337 cat "${LOCALMACROS:-/dev/null}" "${TEMPLATEFILE:-/dev/null}" | \
338 removecomments \
0baa7b9d 339 >> "${UPEX4C_tmp}"
de45f55a 340 fi
0baa7b9d 341 mv -f "${UPEX4C_tmp}" "${UPEX4C_outputfile}"
de45f55a
AM
342 chmod "${CFILEMODE}" "${UPEX4C_outputfile}"
343 [ "${UPEX4C_verbose}" = "yes" ] && \
344 echo "Not substituting variables since conftype is none (or other)"
345 exit 0
346 ;;
347esac
348
349UPEX4C_macros="##############################################\n"
350UPEX4C_macros="${UPEX4C_macros}# the following macro definitions were created\n"
351UPEX4C_macros="${UPEX4C_macros}# dynamically by $0\n"
352
353preprocess_macro() {
354 macroname="${1:-}"
355 shift
01e60269 356 contents="$(lowercase ${@} | check_ascii_pipe)"
de45f55a
AM
357 printf "%s" ".ifndef $macroname\n$macroname=$contents\n.endif\n"
358}
359
360seed_macro() {
361 UPEX4C_macros="${UPEX4C_macros}$(preprocess_macro "$1" "$2")"
362}
363
364file2macros() {
365 file="$1"
366 < $1 \
367 sed -n '/^[[:upper:]]/p;' | \
368 grep -v '^CFILEMODE=' | \
369 while read line; do
370 errormessage "undocumented line $line found in $1, generating exim macro"
371 left="$(echo $line | sed 's/\([^=]*\).*/\1/')"
372 right="$(echo $line | sed 's/[^=]*=\(.*\)/\1/')"
373 preprocess_macro "$left" "$right"
374 done
375}
376
377if [ "${dc_local_interfaces}" != "" ] ; then
378 seed_macro "MAIN_LOCAL_INTERFACES" "${dc_local_interfaces}"
379fi
380
381if [ "${dc_minimaldns}" = "true" ] ; then
382 seed_macro "DC_minimaldns" "1"
383 if guessed_name="$(hostname --fqdn | lowerpipe | check_ascii_pipe | grep '\.')" ; then
384 seed_macro "MAIN_HARDCODE_PRIMARY_HOSTNAME" "$guessed_name"
385 else
386 errormessage "hostname --fqdn did not return a fully qualified name, dc_minimaldns will not work. Please fix your /etc/hosts setup."
387 fi
388fi
389
390if [ -n "${hide_mailname:-}" ]; then
391 seed_macro "HIDE_MAILNAME" "${hide_mailname:-}"
392fi
393seed_macro "MAIN_PACKAGE_VERSION" "$UPEX4C_version"
394seed_macro "MAIN_LOCAL_DOMAINS" "${local_domains}"
395seed_macro "MAIN_RELAY_TO_DOMAINS" "${dc_relay_domains}"
396seed_macro "ETC_MAILNAME" "$mailname"
397seed_macro "LOCAL_DELIVERY" "${dc_localdelivery}"
398seed_macro "MAIN_RELAY_NETS" "${dc_relay_nets}"
399seed_macro "DCreadhost" "${dc_readhost}"
400seed_macro "DCsmarthost" "${dc_smarthost}"
401seed_macro "DC_eximconfig_configtype" "${dc_eximconfig_configtype}"
402seed_macro "DCconfig_${dc_eximconfig_configtype}" "1"
403
404# dump everything starting with a capital into macros as well
405# this is going to stay undocumented, but fixes PEBCAK where people write
406# macros into ue4cc.
407
408UPEX4C_macros="${UPEX4C_macros}$(file2macros $UE4CC)"
409
410UPEX4C_macros="${UPEX4C_macros}##############################################\n"
411
412case "${dc_use_split_config}" in
413true)
414 for i in ${UPEX4C_sections} ; do
415 echo "# begin processing $i #####"
416 cat_parts "${UPEX4C_confd}/$i"
417 echo "# end of $i #####"
418 done \
419 | removecomments \
420 | sed "s|^\(UPEX4CmacrosUPEX4C.*\)$|\1\n$UPEX4C_macros|" \
0baa7b9d 421 >> "${UPEX4C_tmp}"
de45f55a
AM
422 RELEVANTTEMPLATE="$UPEX4C_confd"
423;;
424false)
425 if [ ! -r "$TEMPLATEFILE" ] ; then
426 echo "Error: Unsplit config selected and $TEMPLATEFILE missing ... exiting" 1>&2
427 exit 1
428 fi
429 LOCALMACROS=""
430 if [ -e "/etc/exim4/exim4.conf.localmacros" ]; then
431 LOCALMACROS="${UPEX4C_confdir}/exim4.conf.localmacros"
432 fi
433 cat "${LOCALMACROS:-/dev/null}" "${TEMPLATEFILE:-/dev/null}" \
434 | removecomments \
435 | sed "s|^\(UPEX4CmacrosUPEX4C.*\)$|\1\n$UPEX4C_macros|" \
0baa7b9d 436 >> "${UPEX4C_tmp}"
de45f55a
AM
437 RELEVANTTEMPLATE="$TEMPLATEFILE"
438;;
439*)
440 errormessage "Invalid value for dc_use_split_config: \"${dc_use_split_config}\", exiting."
0baa7b9d 441 rm -f "${UPEX4C_tmp}"
de45f55a
AM
442 exit 1
443;;
444esac
445
446# check for left-over DEBCONF strings that may cause installation trouble
447# (fix PEBCAK for people who don't accept conffile changes and don't
448# read docs)
449if grep -qr '^[^#]*DEBCONF[[:lower:]_]\+DEBCONF' $RELEVANTTEMPLATE \
450 && ! grep -qr '^[[:space:]]*DEBCONFstringOK_config_adapted[[:space:]]*=' $RELEVANTTEMPLATE; then
451 errormessage "DEBCONFsomethingDEBCONF found in exim configuration. This is most probably caused by you upgrading to exim4 4.67-3 or later without accepting the suggested conffile changes. Please read /usr/share/doc/exim4-config/NEWS.Debian.gz for 4.67-2 and 4.67-4"
452fi
453
454# check for left-over UPEX4CmacrosUPEX4C comment string that may cause
455# installation trouble (fix PEBCAK for people who don't accept conffile
456# changes and don't read docs)
457if grep -qr '# UPEX4CmacrosUPEX4C' $RELEVANTTEMPLATE \
458 && ! grep -qr '^[[:space:]]*UPEX4CmacrosOK_config_adapted[[:space:]]*=' $RELEVANTTEMPLATE; then
459 errormessage "UPEX4CmacrosUPEX4C found in an exim configuration comment. This is most probably caused by you upgrading to exim4 4.67-5 or later without accepting the suggested conffile changes. Please read /usr/share/doc/exim4-config/NEWS.Debian.gz for 4.67-5"
460fi
461
462
0baa7b9d
SB
463# test validity if called without -o or if --check was supplied
464if [ "${UPEX4C_outputfile}" = "${UPEX4C_autoconfigfile}" ] || \
465 [ "x${UPEX4C_check}" = "xyes" ]; then
466 if [ -x "${EXIM}" ] ; then
467 if ! "${EXIM}" -C "${UPEX4C_tmp}" -bV > /dev/null ; then
468 # we have an error in the configuration file. Do not install
469 # and activate. However, errors in string expansions inside
470 # the configuration file are not detected by this check!
471 errormessage "Invalid new configfile ${UPEX4C_tmp}, not installing ${UPEX4C_tmp} to ${UPEX4C_outputfile}"
472 exit 1
473 fi
de45f55a
AM
474 fi
475fi
0baa7b9d
SB
476if [ "x${UPEX4C_check}" = "xyes" ]; then
477 rm -f "${UPEX4C_tmp}"
478 exit 0
479fi
de45f55a 480
0baa7b9d 481mv -f "${UPEX4C_tmp}" "${UPEX4C_outputfile}"
de45f55a
AM
482chmod "${CFILEMODE}" "${UPEX4C_outputfile}"
483
484# end of file