Import Debian changes 4.92-8+deb10u3
[hcoop/debian/exim4.git] / debian / debconf / update-exim4.conf
1 #!/bin/sh
2 # update-exim4.conf(8) - Generate /var/lib/exim4/config.autogenerated
3
4 set -e
5 set -C
6 set -f
7
8 UPEX4C_confdir="/etc/exim4"
9 UPEX4C_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.
14 UPEX4C_semicolon="dc_local_interfaces dc_relay_nets dc_relay_domains"
15 EXIM="/usr/sbin/exim4"
16
17 UPEX4C_verbose=no
18 UPEX4C_autoconfigfile=/var/lib/exim4/config.autogenerated
19 UPEX4C_outputfile="${UPEX4C_autoconfigfile}"
20 UPEX4C_version=""
21
22 usage() {
23 cat <<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}
32 --check - Test generated file for validity and remove it again.
33 EOF
34 }
35
36 ## Parse commandline
37 TEMP=$(getopt -n update-exim4.conf \
38 -l check,keepcomments,removecomments,output:,confdir:,help,verbose -- \
39 +o:d:vh "$@")
40
41 if test "$?" != 0; then
42 echo "Terminating..." >&2
43 exit 1
44 fi
45
46 eval set -- ${TEMP}
47 while 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 ;;
62 --check)
63 UPEX4C_check=yes
64 ;;
65 -o|--output)
66 shift
67 UPEX4C_outputfile="$1"
68 ;;
69 -d|--confdir)
70 shift
71 UPEX4C_confdir="$1"
72 ;;
73 esac
74 shift
75 done
76 shift
77
78 # No non-option arguments allowed.
79 if [ "$#" -ne 0 ]; then
80 echo "No non option arguments ($@) allowed" >&2
81 usage >&2
82 exit 1
83 fi
84
85 # exit immediately if /etc/exim4/exim4.conf exists and -o was not specified
86 if [ -e /etc/exim4/exim4.conf ] && \
87 [ "${UPEX4C_outputfile}" = "${UPEX4C_autoconfigfile}" ] ; then
88 exit 0
89 fi
90
91 UE4CC="$UPEX4C_confdir/update-exim4.conf.conf"
92 UPEX4C_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
97 if [ -f "$UE4CC" ]; then
98 . "$UE4CC"
99 else
100 echo >&2 "$0: Error, no $UE4CC, exiting."
101 exit 1
102 fi
103
104
105 UPEX4C_autoconfigfile=/var/lib/exim4/config.autogenerated
106 if [ "$(dirname ${UPEX4C_outputfile})" = "/var/lib/exim4" ] ; then
107 UPEX4C_tmp="${UPEX4C_outputfile}.tmp"
108 else
109 UPEX4C_tmp="$(tempfile -m600 -p ex4)"
110 fi
111
112 lowerpipe() {
113 tr 'A-Z' 'a-z'
114 }
115
116 lowercase() {
117 echo "$*" | lowerpipe
118 }
119
120 check_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
136 TEMPLATEFILE="${UPEX4C_confdir}/exim4.conf.template"
137
138 dc_use_split_config="$(lowercase $dc_use_split_config)"
139 UPEX4C_verbose="$(lowercase $UPEX4C_verbose)"
140
141 if [ "${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
148 else
149 [ "${UPEX4C_verbose}" = "yes" ] && \
150 echo "using non-split configuration scheme from ${TEMPLATEFILE}"
151 fi
152
153 # take only the first word from /etc/mailname
154 mailname="$(< /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.
157 if 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."
159 fi
160
161 dc_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
164 local_domains="$(echo @:localhost:"${dc_other_hostnames}" | \
165 sed -e 's/[;: ]*$//' -e 's/ *//' -e 's/;/:/g')"
166
167
168 # run-parts emulation, stolen from Branden's /etc/X11/Xsession
169 # Addition: Use file.rul instead if file if it exists.
170 run_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
200 errormessage () {
201 # pretty-print messages of arbitrary length (no trailing newline)
202 echo "$*" | fold -s -w ${COLUMNS:-80} >&2;
203 }
204
205 cat_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
224 gentmpconf() {
225 rm -f "${UPEX4C_tmp}"
226 touch "${UPEX4C_tmp}"
227 # this can be removed by the end of 2007
228 #chown --reference=${TEMPLATEFILE} \
229 # ${UPEX4C_tmp} ${UPEX4C_outputfile}
230 #chmod --reference=${TEMPLATEFILE} \
231 # ${UPEX4C_tmp} ${UPEX4C_outputfile}
232 if [ "$(id -u)" = "0" ]; then
233 chown root:Debian-exim "${UPEX4C_tmp}"
234 [ -e "${UPEX4C_outputfile}" ] && \
235 chown root:Debian-exim "${UPEX4C_outputfile}"
236 fi
237 chmod 640 "${UPEX4C_tmp}"
238 if [ -e "${UPEX4C_outputfile}" ]; then
239 chmod 640 "${UPEX4C_outputfile}"
240 fi
241 }
242
243 removecomments(){
244 if [ "${UPEX4C_comments}" = "no" ] ; then
245 grep -E -v '^[[:space:]]*#' | sed -e '/^$/N;/\n$/D' ;
246 else
247 cat
248 fi
249 }
250
251 gentmpconf
252
253 cat << EOF >> "${UPEX4C_tmp}"
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
261 EOF
262
263 if [ "${dc_use_split_config}" = "true" ] ; then
264 cat << EOF >> "${UPEX4C_tmp}"
265 # split config files in the $UPEX4C_confd/ directory.
266 EOF
267 else
268 cat << EOF >> "${UPEX4C_tmp}"
269 # non-split config ($UPEX4C_confdir/exim4.conf.localmacros
270 # and $UPEX4C_confdir/exim4.conf.template).
271 EOF
272 fi
273
274 cat << EOF >> "${UPEX4C_tmp}"
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 #########
287 EOF
288
289 # handle ";" in input values as separator change
290
291 for 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
299 done
300
301 # fix up smarthost line: change semicolons into single colons
302 dc_smarthost="$(lowercase $dc_smarthost | check_ascii_pipe | sed 's/;/:/g')"
303
304 dc_relay_nets="$(lowercase $dc_relay_nets | check_ascii_pipe)"
305
306 if echo "$dc_relay_nets" | grep -q '^<;'; then
307 dc_relay_nets="$dc_relay_nets ; 127.0.0.1 ; ::1"
308 else
309 dc_relay_nets="$dc_relay_nets : 127.0.0.1 : ::::1"
310 fi
311
312 dc_eximconfig_configtype="$(lowercase $dc_eximconfig_configtype | check_ascii_pipe)"
313 dc_hide_mailname="$(lowercase $dc_hide_mailname | check_ascii_pipe)"
314 dc_readhost="$(lowercase $dc_readhost | check_ascii_pipe)"
315 case "$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 \
331 >> "${UPEX4C_tmp}"
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 \
339 >> "${UPEX4C_tmp}"
340 fi
341 mv -f "${UPEX4C_tmp}" "${UPEX4C_outputfile}"
342 chmod "${CFILEMODE}" "${UPEX4C_outputfile}"
343 [ "${UPEX4C_verbose}" = "yes" ] && \
344 echo "Not substituting variables since conftype is none (or other)"
345 exit 0
346 ;;
347 esac
348
349 UPEX4C_macros="##############################################\n"
350 UPEX4C_macros="${UPEX4C_macros}# the following macro definitions were created\n"
351 UPEX4C_macros="${UPEX4C_macros}# dynamically by $0\n"
352
353 preprocess_macro() {
354 macroname="${1:-}"
355 shift
356 contents="$(lowercase ${@} | check_ascii_pipe)"
357 printf "%s" ".ifndef $macroname\n$macroname=$contents\n.endif\n"
358 }
359
360 seed_macro() {
361 UPEX4C_macros="${UPEX4C_macros}$(preprocess_macro "$1" "$2")"
362 }
363
364 file2macros() {
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
377 if [ "${dc_local_interfaces}" != "" ] ; then
378 seed_macro "MAIN_LOCAL_INTERFACES" "${dc_local_interfaces}"
379 fi
380
381 if [ "${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
388 fi
389
390 if [ -n "${hide_mailname:-}" ]; then
391 seed_macro "HIDE_MAILNAME" "${hide_mailname:-}"
392 fi
393 seed_macro "MAIN_PACKAGE_VERSION" "$UPEX4C_version"
394 seed_macro "MAIN_LOCAL_DOMAINS" "${local_domains}"
395 seed_macro "MAIN_RELAY_TO_DOMAINS" "${dc_relay_domains}"
396 seed_macro "ETC_MAILNAME" "$mailname"
397 seed_macro "LOCAL_DELIVERY" "${dc_localdelivery}"
398 seed_macro "MAIN_RELAY_NETS" "${dc_relay_nets}"
399 seed_macro "DCreadhost" "${dc_readhost}"
400 seed_macro "DCsmarthost" "${dc_smarthost}"
401 seed_macro "DC_eximconfig_configtype" "${dc_eximconfig_configtype}"
402 seed_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
408 UPEX4C_macros="${UPEX4C_macros}$(file2macros $UE4CC)"
409
410 UPEX4C_macros="${UPEX4C_macros}##############################################\n"
411
412 case "${dc_use_split_config}" in
413 true)
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|" \
421 >> "${UPEX4C_tmp}"
422 RELEVANTTEMPLATE="$UPEX4C_confd"
423 ;;
424 false)
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|" \
436 >> "${UPEX4C_tmp}"
437 RELEVANTTEMPLATE="$TEMPLATEFILE"
438 ;;
439 *)
440 errormessage "Invalid value for dc_use_split_config: \"${dc_use_split_config}\", exiting."
441 rm -f "${UPEX4C_tmp}"
442 exit 1
443 ;;
444 esac
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)
449 if 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"
452 fi
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)
457 if 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"
460 fi
461
462
463 # test validity if called without -o or if --check was supplied
464 if [ "${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
474 fi
475 fi
476 if [ "x${UPEX4C_check}" = "xyes" ]; then
477 rm -f "${UPEX4C_tmp}"
478 exit 0
479 fi
480
481 mv -f "${UPEX4C_tmp}" "${UPEX4C_outputfile}"
482 chmod "${CFILEMODE}" "${UPEX4C_outputfile}"
483
484 # end of file