summaryrefslogtreecommitdiffstats
path: root/debian/debconf/update-exim4.conf
blob: d345fe6af94dc14240a1569bbd602c6c52007dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/bin/sh
# update-exim4.conf(8) - Generate /var/lib/exim4/config.autogenerated

set -e
set -C
set -f

UPEX4C_confdir="/etc/exim4"
UPEX4C_sections="main acl router transport retry rewrite auth"

# list of ue4cc options that need to support both colons and
# semicolons as separators. dc_other_hostnames and dc_smarthost
# has special handling.
UPEX4C_semicolon="dc_local_interfaces dc_relay_nets dc_relay_domains"
EXIM="/usr/sbin/exim4"

UPEX4C_verbose=no
UPEX4C_autoconfigfile=/var/lib/exim4/config.autogenerated
UPEX4C_outputfile="${UPEX4C_autoconfigfile}"
UPEX4C_version=""

usage() {
cat <<EOF
$0 - Generate exim4 configuration files
       Options:
       -v|--verbose - Enable verbose mode, tell about ignored files
       -h|--help - Show this message
       --keepcomments - Do not remove comment lines
       --removecomments - Remove comment lines
       -o|--output file - write output to file instead of ${UPEX4C_outputfile}
       -d|--confdir directory - read input from given directory instead of ${UPEX4C_confdir}
       --check - Test generated file for validity and remove it again.
EOF
}

## Parse commandline
TEMP=$(getopt -n update-exim4.conf \
	-l check,keepcomments,removecomments,output:,confdir:,help,verbose -- \
	+o:d:vh "$@")

if test "$?" != 0; then
	echo "Terminating..." >&2
	exit 1
fi

eval set -- ${TEMP}
while test "$1" != "--"; do
	case $1 in
		-h|--help)
			usage
			exit 0
		;;
		-v|--verbose)
			UPEX4C_verbose=yes
		;;
		--keepcomments)
			UPEX4C_comments=yes
		;;
		--removecomments)
			UPEX4C_comments=no
		;;
		--check)
			UPEX4C_check=yes
		;;
		-o|--output)
			shift
			UPEX4C_outputfile="$1"
		;;
		-d|--confdir)
			shift
			UPEX4C_confdir="$1"
		;;
	esac
	shift
done
shift

# No non-option arguments allowed.
if [ "$#" -ne 0 ]; then
	echo "No non option arguments ($@) allowed" >&2
	usage >&2
	exit 1
fi

# exit immediately if /etc/exim4/exim4.conf exists and -o was not specified
if [ -e /etc/exim4/exim4.conf ] && \
  [ "${UPEX4C_outputfile}" = "${UPEX4C_autoconfigfile}" ] ; then
  exit 0
fi

UE4CC="$UPEX4C_confdir/update-exim4.conf.conf"
UPEX4C_confd="$UPEX4C_confdir/conf.d"

[ -d "$(dirname "$UPEX4C_outputfile")" ] || \
{ printf "$0: Error, missing $(dirname "$UPEX4C_outputfile"), exiting.\n" 1>&2 ; exit 1 ; }

if [ -f "$UE4CC" ]; then
  . "$UE4CC"
else
  echo >&2 "$0: Error, no $UE4CC, exiting."
  exit 1
fi


UPEX4C_autoconfigfile=/var/lib/exim4/config.autogenerated
if [ "$(dirname ${UPEX4C_outputfile})" = "/var/lib/exim4" ] ; then
	UPEX4C_tmp="${UPEX4C_outputfile}.tmp"
else
	UPEX4C_tmp="$(mktemp)"
fi

lowerpipe() {
  tr 'A-Z' 'a-z'
}

lowercase() {
  echo "$*" | lowerpipe
}

check_ascii_pipe() {
  IN="$(cat)"
  # Use "abcdef... instead of a a-z or [:alnum:] here since the alternatives
  # will also match non-ascii characters.
  OUT="$(echo $IN | sed 's/[^-0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\/\.!*@_~:;< \[\]]/_/g')"
  if [ "$OUT" != "$IN" ]; then
    echo >&2 "$0: non-ascii value $IN read from $UE4CC, sanitizing to $OUT"
  fi
  echo $OUT
}

[ "${CFILEMODE}" = "" ] && CFILEMODE=644
[ "${dc_use_split_config}" = "" ] && dc_use_split_config='false'
[ "${dc_localdelivery}" = "" ] && dc_localdelivery='mail_spool'
[ "${UPEX4C_comments:-}" = "" ] && UPEX4C_comments="${ue4c_keepcomments:-no}"

TEMPLATEFILE="${UPEX4C_confdir}/exim4.conf.template"

dc_use_split_config="$(lowercase $dc_use_split_config)"
UPEX4C_verbose="$(lowercase $UPEX4C_verbose)"

if [ "${dc_use_split_config}" = "true" ]; then
  [ "${UPEX4C_verbose}" = "yes" ] && \
	echo "using split configuration scheme from ${UPEX4C_confd}"
  if ! [ -d "${UPEX4C_confd}" ]; then
    printf >&2 "$0: Error, no ${UPEX4C_confd}, exiting.\n"
    exit 1
  fi
else
  [ "${UPEX4C_verbose}" = "yes" ] && \
	echo "using non-split configuration scheme from ${TEMPLATEFILE}"
fi

# take only the first word from /etc/mailname
mailname="$(< /etc/mailname sed -n 's/\([-[:alnum:]@\.]\+\).*/\1/;p;q' | lowerpipe | check_ascii_pipe)"

# barf if lookups are found. They have never been supported here.
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
  echo >&2 "WARNING: using 'lookup;' constructs in $UE4CC has never been supported! See /usr/share/doc/exim4-config/NEWS.Debian.gz for details."
fi

dc_other_hostnames="$(lowercase $dc_other_hostnames | check_ascii_pipe)"
# add localhost, get rid of spaces, trailing (semi)colons and make the list
# colon separated
local_domains="$(echo @:localhost:"${dc_other_hostnames}" | \
	sed -e 's/[;: ]*$//' -e 's/ *//' -e 's/;/:/g')"


# run-parts emulation, stolen from Branden's /etc/X11/Xsession
# Addition: Use file.rul instead if file if it exists.
run_parts () {
	# reset LC_COLLATE
	unset LANG LC_COLLATE LC_ALL

	if [ -z "$1" ]; then
		errormessage "$0: internal run_parts called without an argument"
	fi
	if [ ! -d "$1" ]; then
		errormessage "$0: internal run_parts called, but $1 does not exist or is not a directory."
	fi
	for F in $(ls $1); do
		if expr "$F" : '[[:alnum:]_-]\+$' > /dev/null 2>&1; then
			if [ -f "$1/$F" ] ; then
				if [ -f "$1/${F}.rul" ] ; then
					echo "$1/${F}.rul"
				else
					echo "$1/$F"
				fi
			fi
		else
			if [ "${UPEX4C_verbose}" = "yes" ] && \
				[ -f "$1/$F" ] && \
				! expr "$F" : '[[:alnum:]_-]\+\.rul'> /dev/null 2>&1 ; then
				echo \
				"internal run-parts: ignoring file: $1/$F" 1>&2
			fi
		fi
	done;
}
# also from Branden
errormessage () {
	# pretty-print messages of arbitrary length (no trailing newline)
	echo "$*" | fold -s -w ${COLUMNS:-80} >&2;
}

cat_parts() {
       if [ -z "$1" ]; then
               errormessage "$0: internal cat_parts called without an argument"
       fi
       if [ ! -d "$1" ]; then
               errormessage "$0: internal cat_parts called, but $1 does not exist or is not a directory."
       fi
       for file in $(run_parts $1); do
               echo "#####################################################"
               echo "### $file"
               echo "#####################################################"
               cat "$file"
	       echo
               echo "#####################################################"
               echo "### end $file"
               echo "#####################################################"
       done
}

gentmpconf() {
	rm -f "${UPEX4C_tmp}"
	touch "${UPEX4C_tmp}"
	# this can be removed by the end of 2007
	#chown --reference=${TEMPLATEFILE} \
	#	${UPEX4C_tmp} ${UPEX4C_outputfile}
	#chmod --reference=${TEMPLATEFILE} \
	#		${UPEX4C_tmp} ${UPEX4C_outputfile}
	if [ "$(id -u)" = "0" ]; then 
		chown root:Debian-exim "${UPEX4C_tmp}"
		[ -e "${UPEX4C_outputfile}" ] && \
		chown root:Debian-exim "${UPEX4C_outputfile}"
	fi
	chmod 640 "${UPEX4C_tmp}"
	if [ -e "${UPEX4C_outputfile}" ]; then
	  chmod 640 "${UPEX4C_outputfile}"
	fi
}

removecomments(){
	if [ "${UPEX4C_comments}" = "no" ] ; then
		grep -E -v '^[[:space:]]*#' | sed -e '/^$/N;/\n$/D' ;
	else
		cat
	fi
}

gentmpconf

cat << EOF >> "${UPEX4C_tmp}"
#########
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# This file was generated dynamically from
EOF

if [ "${dc_use_split_config}" = "true" ] ; then
cat << EOF >> "${UPEX4C_tmp}"
# split config files in the $UPEX4C_confd/ directory.
EOF
else
cat << EOF >> "${UPEX4C_tmp}"
# non-split config ($UPEX4C_confdir/exim4.conf.localmacros
# and $UPEX4C_confdir/exim4.conf.template).
EOF
fi

cat << EOF >> "${UPEX4C_tmp}"
# The config files are supplemented with package installation/configuration
# settings managed by debconf. This data is stored in
# $UPEX4C_confdir/update-exim4.conf.conf
# Any changes you make here will be lost.
# See /usr/share/doc/exim4-base/README.Debian.gz and update-exim4.conf(8)
# for instructions of customization.
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
#########
EOF

# handle ";" in input values as separator change

for field in $UPEX4C_semicolon; do
  if eval echo \$$field | grep -q ";"; then
    eval temp=\$$field
    if ! echo $temp | grep -q "^<"; then
      temp="<; $temp"
      eval "$field='$temp'"
    fi
  fi
done

# fix up smarthost line: change semicolons into single colons
dc_smarthost="$(lowercase $dc_smarthost | check_ascii_pipe | sed 's/;/:/g')"

dc_relay_nets="$(lowercase $dc_relay_nets | check_ascii_pipe)"

if echo "$dc_relay_nets" | grep -q '^<;'; then
  dc_relay_nets="$dc_relay_nets ; 127.0.0.1 ; ::1"
else
  dc_relay_nets="$dc_relay_nets : 127.0.0.1 : ::::1"
fi

dc_eximconfig_configtype="$(lowercase $dc_eximconfig_configtype | check_ascii_pipe)"
dc_hide_mailname="$(lowercase $dc_hide_mailname | check_ascii_pipe)"
dc_readhost="$(lowercase $dc_readhost | check_ascii_pipe)"
case "$dc_eximconfig_configtype" in
	satellite|smarthost)
        if [ "${dc_hide_mailname}" = "true" ] && [ -n "${dc_readhost}" ] ; then
		hide_mailname=1
	fi
	;;
	local)
	;;
	internet)
	;;
	none|*)
                if [ "${dc_use_split_config}" = "true" ] ; then
                        for i in ${UPEX4C_sections} ; do
                                cat_parts "${UPEX4C_confd}/$i"
                        done | \
                        removecomments \
			>> "${UPEX4C_tmp}"
                else
			LOCALMACROS=""
			if [ -e "/etc/exim4/exim4.conf.localmacros" ]; then
			  LOCALMACROS="/etc/exim4/exim4.conf.localmacros"
			fi
                        cat "${LOCALMACROS:-/dev/null}" "${TEMPLATEFILE:-/dev/null}" | \
                        removecomments \
                                >> "${UPEX4C_tmp}"
                fi
		mv -f "${UPEX4C_tmp}" "${UPEX4C_outputfile}"
		chmod "${CFILEMODE}" "${UPEX4C_outputfile}"
		[ "${UPEX4C_verbose}" = "yes" ] && \
			echo "Not substituting variables since conftype is none (or other)"
		exit 0
	;;
esac

UPEX4C_macros="##############################################\n"
UPEX4C_macros="${UPEX4C_macros}# the following macro definitions were created\n"
UPEX4C_macros="${UPEX4C_macros}# dynamically by $0\n"

preprocess_macro() {
  macroname="${1:-}"
  shift
  contents="$(lowercase ${@} | check_ascii_pipe)"
  printf "%s" ".ifndef $macroname\n$macroname=$contents\n.endif\n"
}

seed_macro() {
  UPEX4C_macros="${UPEX4C_macros}$(preprocess_macro "$1" "$2")"
}

file2macros() {
  file="$1"
  < $1 \
    sed -n '/^[[:upper:]]/p;' | \
    grep -v '^CFILEMODE=' | \
    while read line; do
      errormessage "undocumented line $line found in $1, generating exim macro"
      left="$(echo $line | sed 's/\([^=]*\).*/\1/')"
      right="$(echo $line | sed 's/[^=]*=\(.*\)/\1/')"
      preprocess_macro "$left" "$right"
    done
}

if [ "${dc_local_interfaces}" != "" ] ; then
	seed_macro "MAIN_LOCAL_INTERFACES" "${dc_local_interfaces}"
fi

if [ "${dc_minimaldns}" = "true" ] ; then
	seed_macro "DC_minimaldns" "1"
	if guessed_name="$(hostname --fqdn | lowerpipe | check_ascii_pipe | grep '\.')" ; then
		seed_macro "MAIN_HARDCODE_PRIMARY_HOSTNAME" "$guessed_name"
	else
		errormessage "hostname --fqdn did not return a fully qualified name, dc_minimaldns will not work. Please fix your /etc/hosts setup."
	fi
fi

if [ -n "${hide_mailname:-}" ]; then
  seed_macro "HIDE_MAILNAME" "${hide_mailname:-}"
fi
seed_macro "MAIN_PACKAGE_VERSION" "$UPEX4C_version"
seed_macro "MAIN_LOCAL_DOMAINS" "${local_domains}"
seed_macro "MAIN_RELAY_TO_DOMAINS" "${dc_relay_domains}"
seed_macro "ETC_MAILNAME" "$mailname"
seed_macro "LOCAL_DELIVERY" "${dc_localdelivery}"
seed_macro "MAIN_RELAY_NETS" "${dc_relay_nets}"
seed_macro "DCreadhost" "${dc_readhost}"
seed_macro "DCsmarthost" "${dc_smarthost}"
seed_macro "DC_eximconfig_configtype" "${dc_eximconfig_configtype}"
seed_macro "DCconfig_${dc_eximconfig_configtype}" "1"

# dump everything starting with a capital into macros as well
# this is going to stay undocumented, but fixes PEBCAK where people write
# macros into ue4cc.

UPEX4C_macros="${UPEX4C_macros}$(file2macros $UE4CC)"

UPEX4C_macros="${UPEX4C_macros}##############################################\n"

case "${dc_use_split_config}" in
true)
	for i in ${UPEX4C_sections} ; do
		echo "# begin processing $i #####"
		cat_parts "${UPEX4C_confd}/$i"
		echo "# end of $i  #####"
	done \
	| removecomments \
	| sed "s|^\(UPEX4CmacrosUPEX4C.*\)$|\1\n$UPEX4C_macros|" \
	>> "${UPEX4C_tmp}"
	RELEVANTTEMPLATE="$UPEX4C_confd"
;;
false)
	if [ ! -r "$TEMPLATEFILE" ] ; then
		echo "Error: Unsplit config selected and $TEMPLATEFILE missing ... exiting" 1>&2
		exit 1
	fi
	LOCALMACROS=""
	if [ -e "/etc/exim4/exim4.conf.localmacros" ]; then
		LOCALMACROS="${UPEX4C_confdir}/exim4.conf.localmacros"
	fi
	cat "${LOCALMACROS:-/dev/null}" "${TEMPLATEFILE:-/dev/null}" \
	| removecomments \
	| sed "s|^\(UPEX4CmacrosUPEX4C.*\)$|\1\n$UPEX4C_macros|" \
 	>> "${UPEX4C_tmp}"
	RELEVANTTEMPLATE="$TEMPLATEFILE"
;;
*)
	errormessage "Invalid value for dc_use_split_config: \"${dc_use_split_config}\", exiting."
	rm -f "${UPEX4C_tmp}"
	exit 1
;;
esac

# check for left-over DEBCONF strings that may cause installation trouble
# (fix PEBCAK for people who don't accept conffile changes and don't
# read docs)
if grep -qr '^[^#]*DEBCONF[[:lower:]_]\+DEBCONF' $RELEVANTTEMPLATE \
   && ! grep -qr '^[[:space:]]*DEBCONFstringOK_config_adapted[[:space:]]*=' $RELEVANTTEMPLATE; then
  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"
fi

# check for left-over UPEX4CmacrosUPEX4C comment string that may cause
# installation trouble (fix PEBCAK for people who don't accept conffile
# changes and don't read docs)
if grep -qr '# UPEX4CmacrosUPEX4C' $RELEVANTTEMPLATE \
   && ! grep -qr '^[[:space:]]*UPEX4CmacrosOK_config_adapted[[:space:]]*=' $RELEVANTTEMPLATE; then
  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"
fi


# test validity if called without -o or if --check was supplied
if [ "${UPEX4C_outputfile}" = "${UPEX4C_autoconfigfile}" ] || \
	[ "x${UPEX4C_check}" = "xyes" ]; then
	if [ -x "${EXIM}" ] ; then
		if ! "${EXIM}" -C "${UPEX4C_tmp}" -bV > /dev/null ; then
			# we have an error in the configuration file. Do not install
			# and activate. However, errors in string expansions inside
			# the configuration file are not detected by this check!
			errormessage "Invalid new configfile ${UPEX4C_tmp}, not installing ${UPEX4C_tmp} to ${UPEX4C_outputfile}"
			exit 1
		fi
	fi
fi
if [ "x${UPEX4C_check}" = "xyes" ]; then
	rm -f "${UPEX4C_tmp}"
	exit 0
fi

mv -f "${UPEX4C_tmp}" "${UPEX4C_outputfile}"
chmod "${CFILEMODE}" "${UPEX4C_outputfile}"

# end of file