summaryrefslogtreecommitdiff
path: root/debian/debconf/update-exim4.conf
blob: ebe530350f28dd802421d02c79f5a0167567f2dd (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
#!/bin/sh
# update-exim4.conf(8) - Generate /var/lib/exim4/config.autogenerated


# exit immediately if /etc/exim4/exim4.conf exists
[ -e /etc/exim4/exim4.conf ] && exit 0

UPEX4C_confd="/etc/exim4/conf.d"
UPEX4C_sections="main acl router transport retry rewrite auth"
EXIM="/usr/sbin/exim4"

[ -d /var/lib/exim4/ ] && \
[ -d ${UPEX4C_confd} ] || \
{ printf "$0: Error, no ${UPEX4C_confd} or missing\n/var/lib/exim4/, exiting.\n" 1>&2 ; exit 1 ; }

. /etc/exim4/update-exim4.conf.conf || \
{ printf "$0: Error, no /etc/exim4/update-exim4.conf.conf, exiting.\n" 1>&2 ; exit 1 ; }

[ -r /etc/default/exim4 ] && \
	. /etc/default/exim4
[ "x${CFILEMODE}" = "x" ] && CFILEMODE=644

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

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}
EOF
}

## Parse commandline
TEMP=$(getopt -n update-exim4.conf \
	-l keepcomments,removecomments,output:,help,verbose -- \
	+o: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
		;;
		-o|--output)
			shift
			UPEX4C_outputfile="$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

mailname=`cat /etc/mailname | head`

# add localhost and mailname, get rid of spaces and trailing colons
local_domains="`echo ${mailname}:localhost:${dc_other_hostnames} | \
	sed -e 'sÄ[: ]*$ÄÄ' -e 'sÄ *ÄÄ'`"
	
TEMPLATEFILE=/etc/exim4/exim4.conf.template

UPEX4C_internal_tmp=`tempfile -m600 -p ex4`

trap "rm -f ${UPEX4C_internal_tmp}" 0 2 15

# test if $1 is user modified, print message
unmodifandmessage() {
	[ "$#" -eq 1 ] || return 1
	if unmodified "$1" ; then
		return 0
	else
		[ "${UPEX4C_verbose}" = "yes" ] && \
			echo "ignoring user modified file $1"
		return 1
	fi
}	

# 0123456789abcdef0123456789abcdef
generatemasquerade() {
	UPEX4C_internal_currfile="${UPEX4C_confd}/rewrite/35_exim4-base_masquerade"
	unmodifandmessage "${UPEX4C_internal_currfile}" || return
cat << EOF > "${UPEX4C_internal_tmp}"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This is an md5sum.
#######################################
# WARNING WARNING WARNING WARNING
# This file is dynamically generated by update-exim4.conf(8) using the data
# in /etc/exim4/update-exim4.conf.conf.

# If you make _any_ changes to it update-exim4.conf(8) won't update it any
# more! - Especially it will be used _even_ if you change to a different
# conftype.

# If you just want to ADD your additional rewriting rules put them in a
# different file, for example CONFDIR/conf.d/rewrite/50_local_mywrite
##################
EOF
	if [ "$1" = "yes" ] ; then
		for domain in `echo "${local_domains}" | sed -e 's/:/ /g'` ; do
			# mailfuerpostmaster=fuser
			#fuser=sysmail
			#echo '^(?i)(root|postmaster|mailer-daemon)@'"$domain" '${local_part}@in.limbo Ffr'
			echo '*@'"$domain" '${local_part}@'"${dc_readhost} Ffr"
		done >> "${UPEX4C_internal_tmp}"
		#echo '*@in.limbo' "$fuser"'@'"${dc_readhost} Ffr" >> "${UPEX4C_internal_tmp}"
	fi
	UPEX4C_internal_md5=`cat "${UPEX4C_internal_tmp}" | md5sum | cut -d\  -f1`
	{ echo "# ${UPEX4C_internal_md5}" ; cat "${UPEX4C_internal_tmp}" ; } > \
		"${UPEX4C_internal_currfile}"
}

# generate code for evaluation of email_addresses
generateemailaddresses() {
	UPEX4C_internal_currfile="${UPEX4C_confd}/rewrite/30_exim4-base_email-addresses"
	unmodifandmessage "${UPEX4C_internal_currfile}" || return
cat << EOF > "${UPEX4C_internal_tmp}"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This is an md5sum.
#######################################
# WARNING WARNING WARNING WARNING
# This file is dynamically generated by update-exim4.conf(8) using the data
# in /etc/exim4/update-exim4.conf.conf.

# If you make _any_ changes to it update-exim4.conf(8) won't update it any
# more! - Especially it will be used _even_ if you change to a different
# conftype.

# If you just want to ADD your additional rewriting rules put them in a
# different file, for example CONFDIR/conf.d/rewrite/50_local_mywrite
##################

# This rewriting rule is particularly useful for dialup users who
# don't have their own domain, but could be useful for anyone.
# It looks up the real address of all local users in a file

# it needs to be generated dynamically for i in local_domains
#
EOF
	for domain in `echo ${local_domains} | sed -e 's/:/ /g'` ; do
		echo '*@'"$domain" '${lookup{${local_part}}lsearch{CONFDIR/email-addresses}{$value}fail} Ffrs'
	done >> "${UPEX4C_internal_tmp}"
	UPEX4C_internal_md5=`cat "${UPEX4C_internal_tmp}" | md5sum | cut -d\  -f1`
	{ echo "# ${UPEX4C_internal_md5}" ; cat "${UPEX4C_internal_tmp}" ; } > \
		"${UPEX4C_internal_currfile}"
}

# only for satellite
gen_hubuserrouter() {
	UPEX4C_internal_currfile="${UPEX4C_confd}/router/28_exim4-base_hubuser"
	unmodifandmessage "${UPEX4C_internal_currfile}" || return
cat << EOF > "${UPEX4C_internal_tmp}"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This is an md5sum.
#######################################
# WARNING WARNING WARNING WARNING
# This file is dynamically generated by update-exim4.conf(8) using the data
# in /etc/exim4/update-exim4.conf.conf.

# If you make _any_ changes to it update-exim4.conf(8) won't update it any
# more! - Especially it will be used _even_ if you change to a different
# conftype.
##################
EOF
	if [ "$1" = "yes" ] ; then

cat << EOF >> "${UPEX4C_internal_tmp}"
# router-hub_user
#
hub_user:
    debug_print = "R: hub_user for $local_part@$domain"
    driver = redirect
    data = \${local_part}@DCreadhost
    check_local_user
##### end of router-hub_user   ########
EOF
	fi
	UPEX4C_internal_md5=`cat "${UPEX4C_internal_tmp}" | md5sum | cut -d\  -f1`
	{ echo "# ${UPEX4C_internal_md5}" ; cat "${UPEX4C_internal_tmp}" ; } > \
		"${UPEX4C_internal_currfile}"
}

# insert primary router definition
gen_primaryrouter() {
	UPEX4C_internal_currfile="${UPEX4C_confd}/router/22_exim4-base_primary"
	unmodifandmessage "${UPEX4C_internal_currfile}" || return
cat << EOF > "${UPEX4C_internal_tmp}"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This is an md5sum.
#######################################
# WARNING WARNING WARNING WARNING
# This file is dynamically generated by update-exim4.conf(8) using the data
# in /etc/exim4/update-exim4.conf.conf.

# If you make _any_ changes to it update-exim4.conf(8) won't update it any
# more! - Especially it will be used _even_ if you change to a different
# conftype.
##################
EOF

	cat /var/lib/exim4/primrout-${dc_eximconfig_configtype} >> \
		"${UPEX4C_internal_tmp}"
	printf '# The "no_more" above means that all routers below here are for\n# domains in the local_domains list, i.e. just like Exim 3 directors.\n\n' >> \
		"${UPEX4C_internal_tmp}"

	UPEX4C_internal_md5=`cat "${UPEX4C_internal_tmp}" | md5sum | cut -d\  -f1`
	{ echo "# ${UPEX4C_internal_md5}" ; cat "${UPEX4C_internal_tmp}" ; } > \
		"${UPEX4C_internal_currfile}"
}

# generate "never_users=root" option if root is aliased somewhere.
gen_neverusers(){
	UPEX4C_internal_currfile="${UPEX4C_confd}/main/03_exim4-base_neverusers"
	unmodifandmessage "${UPEX4C_internal_currfile}" || return
	cat << EOF > "${UPEX4C_internal_tmp}"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This is an md5sum.
#######################################
# WARNING WARNING WARNING WARNING
# This file is dynamically generated by update-exim4.conf(8) depending on
# the data in /etc/aliases. - If it finds an alias for root it'll
# set 'never_users=root' otherwise not.

# If you make _any_ changes to this file update-exim4.conf(8) won't
# update it any more!
##################
# No deliveries will ever be run under the uids of these users (a colon-
# separated list). An attempt to do so causes a panic error to be logged, and
# the delivery to be deferred. This is a paranoic safety catch. Note that the
# default setting means you cannot deliver mail addressed to root as if it
# were a normal user. This isn't usually a problem, as most sites have an alias
# for root that redirects such mail to a human administrator.
#
EOF
	if [ -r /etc/aliases ] &&\
		grep -q '^root:[[:space:]]*[^[:space:]]' /etc/aliases; then
		echo 'never_users = root' >> "${UPEX4C_internal_tmp}"
	else
		echo '#never_users = root' >> "${UPEX4C_internal_tmp}"
	fi
	UPEX4C_internal_md5=`cat "${UPEX4C_internal_tmp}" | md5sum | cut -d\  -f1`
	{ echo "# ${UPEX4C_internal_md5}" ; cat "${UPEX4C_internal_tmp}" ; } > \
		"${UPEX4C_internal_currfile}"
}

# use this as template for new gen_something functions.
UPEX4C_skeleton() {
	UPEX4C_internal_currfile="${UPEX4C_confd}/foo/bar"
	unmodifandmessage "${UPEX4C_internal_currfile}" || return
	cat << EOF > "${UPEX4C_internal_tmp}"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This is an md5sum.
#######################################
# WARNING WARNING WARNING WARNING
# This file is dynamically generated by update-exim4.conf(8) using the data
# in /etc/exim4/update-exim4.conf.conf.
EOF
# insert more code that appends to ${UPEX4C_internal_tmp}
	UPEX4C_internal_md5=`cat "${UPEX4C_internal_tmp}" | md5sum | cut -d\  -f1`
	{ echo "# ${UPEX4C_internal_md5}" ; cat "${UPEX4C_internal_tmp}" ; } > \
		"${UPEX4C_internal_currfile}"
}

# run-parts emulation, stolen from Branden's /etc/X11/Xsession
# Addition: ignore file if file.disabled 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" ] && [ ! -f "$1/${F}.disabled" ] ; then
				echo "$1/$F"
			fi
		fi
	done;
}
# also from Branden
errormessage () {
	# pretty-print messages of arbitrary length (no trailing newline)
	echo "$*" | fold -s -w ${COLUMNS:-80} >&2;
}

# check whether the file given as argument was modified by the user
# by comparing the md5sum in the first line with the real one.
unmodified() {
	[ "$#" -eq 1 ] || return 1
	[ -f "$1" ] || return 1
	# first line, without the leading '# '.
	checksum_current=`sed -n -e '1s/^# //' -e '1p;1q' "$1"`

	# md5sum over the rest of the file.
	# some versions of md5sum produce
	# '68b329da9893e34099c7d8ad5cb9c940  -' others don't add the dash.
	# '68b329da9893e34099c7d8ad5cb9c940'
	checksum_new=`sed -n '2,$p' "$1" | md5sum | cut -d\  -f1`

	if [ "${checksum_current}" = "${checksum_new}" ] ; then
		return 0
	else
		return 1
	fi
}

# update the md5sum given in the first line, return an error if the file does
# not have an md5sum header in the first line.
updatechecksumheader() {
	[ "$#" -eq 1 ] || return 1
	[ -f "$1" ] || return 1
	# check for correct format "# 76a51391da4a0687697224a124b71a17"
	sed -n -e '1p;1q' "$1" | grep -q -E '^# [[:xdigit:]]{32}$' || \
		{ echo "incorrect format" ; return 1 ;}

	NEWFILE=`tempfile -m600 -p ex4`
	sed -n '2,$p' "$1" | md5sum | cut -d\  -f1 | sed '1s/^/# /' > $NEWFILE
	sed -n '2,$p' "$1" >> $NEWFILE
	mv -f $NEWFILE "$1"
}

gentmpconf() {
	touch ${UPEX4C_outputfile}.tmp
	#chown --reference=${TEMPLATEFILE} \
	#	${UPEX4C_outputfile}.tmp ${UPEX4C_outputfile}
	#chmod --reference=${TEMPLATEFILE} \
	#		${UPEX4C_outputfile}.tmp ${UPEX4C_outputfile}
	chown root:mail ${UPEX4C_outputfile}.tmp
	[ -e ${UPEX4C_outputfile} ] && chown root:mail ${UPEX4C_outputfile}
	chmod 640 ${UPEX4C_outputfile}.tmp
	[ -e ${UPEX4C_outputfile} ] && chmod 640 ${UPEX4C_outputfile}
}

removecomments(){
	if [ "x${UPEX4C_comments}" = "xno" ] ; then
		egrep -v '^[[:space:]]*#' ;
	else
		cat
	fi
}

case "$dc_eximconfig_configtype" in
	satellite)
		# include hub-user-router
		gen_hubuserrouter yes
		# hide mailname with rewriting rules
		generatemasquerade yes
	;;
	local)
		gen_hubuserrouter no
		generatemasquerade no
	;;
	smarthost|internet)
		gen_hubuserrouter no
		generatemasquerade no
	;;
	none|*)
		gen_hubuserrouter no
		generatemasquerade no
		gentmpconf
		for i in ${UPEX4C_sections} ; do
			cat `run_parts ${UPEX4C_confd}/$i`
		done | \
		sed -e "s/DEBCONF[^D][^E][^B].*DEBCONF//g" \
			> ${UPEX4C_outputfile}.tmp
		mv -f ${UPEX4C_outputfile}.tmp ${UPEX4C_outputfile}
		exit 0
	;;
esac

case "${dc_listenonpublic}" in
	false)
		listenonpublic='local_interfaces = 127.0.0.1'
	;;
	true|*)
		listenonpublic='# if local_interfaces is unset, we listen on all interfaces'
	;;
esac

gen_neverusers
generateemailaddresses
gen_primaryrouter
gentmpconf

cat << EOF > ${UPEX4C_outputfile}.tmp
#########
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# this file is generated dynamically from the files in
# CONFDIR/conf.d/ and /etc/exim4/update-exim4.conf.conf
# Any changes you make here will be lost.
# See /usr/share/doc/exim4-base/README.Debian 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

for i in ${UPEX4C_sections} ; do
	echo "# begin processing $i #####"
	cat `run_parts ${UPEX4C_confd}/$i`
	echo "# end of $i  #####"
done | \
removecomments | \
sed -e "sÄDEBCONFlocal_domainsDEBCONFÄ@:${local_domains}Äg" \
	-e "sÄDEBCONFrelay_domainsDEBCONFÄ${dc_relay_domains}Äg" \
	-e "sÄDEBCONFrelay_netsDEBCONFÄ${dc_relay_nets}Äg" \
	-e "sÄDEBCONFvisiblenameDEBCONFÄ${mailname}Äg" \
	-e "sÄDEBCONFreadhostDEBCONFÄ${dc_readhost}Äg" \
	-e "sÄDEBCONFsmarthostDEBCONFÄ${dc_smarthost}Äg" \
	-e "sÄDEBCONFconfigtypeDEBCONFÄ${dc_eximconfig_configtype}Äg" \
	-e "sÄDEBCONFlistenonpublicDEBCONFÄ${listenonpublic}Äg" \
	>> ${UPEX4C_outputfile}.tmp

# test validity if called without -o
if [ "x${UPEX4C_outputfile}" = "x${UPEX4C_autoconfigfile}" ] ; then
	if ! ${EXIM} -C "${UPEX4C_outputfile}.tmp" -bV > /dev/null ; then
		errormessage "Invalid new configfile ${UPEX4C_outputfile}.tmp"
		errormessage "not installing ${UPEX4C_outputfile}.tmp to ${UPEX4C_outputfile}"
		exit 1
	fi
fi

mv -f ${UPEX4C_outputfile}.tmp ${UPEX4C_outputfile}
chmod ${CFILEMODE} ${UPEX4C_outputfile}