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