summaryrefslogtreecommitdiffstats
path: root/debian/update-exim4defaults
blob: 06915e7139c21d69377ec3cd8e45b5687f5baf66 (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
#!/bin/sh
# update-exim4defaults(8): manage entries in /etc/default/exim4
# per script

if [ -n "$EX4DEBUG" ]; then
  echo "now debugging $0 $@"
  set -x
fi

unset LC_ALL
export LC_CTYPE=C

defaultfile=/etc/default/exim4

EX4DEF_INIT=false
EX4DEF_FORCE=false


if [ -r ${defaultfile} ]; then
	. ${defaultfile}
fi


# initialize variables
EX4DEF_QUEUERUNNER="${QUEUERUNNER}"
EX4DEF_QUEUEINTERVAL="${QUEUEINTERVAL}"
EX4DEF_COMMONOPTIONS="${COMMONOPTIONS}"
EX4DEF_QUEUERUNNEROPTIONS="${QUEUERUNNEROPTIONS}"
EX4DEF_QFLAGS="${QFLAGS}"
EX4DEF_SMTPLISTENEROPTIONS="${SMTPLISTENEROPTIONS}"

EX4DEF_FLAGOPTIONS=false
EX4DEF_FLAGREMOVE=false

ex4def_usage ()
{
	echo "update-exim4defaults: manage entries in /etc/default/exim4"
	echo "   usage: update-exim4defaults [[--queuerunner combined|separate|queueonly|ppp|no|nodaemon]"
	echo "          [--qflags flags ] [--queuetime time] [--commonoptions options]"
	echo "          [--queuerunneroptions options] [--smtplisteneroptions options]]"
	echo "          [--remove-common options] [--remove-queue options]"
	echo "          [--remove-smtp options]"
	echo "          [--force|-f] [--help|-h]"
	echo "          [--init]"
}


# used for initialzing and with --force.
ex4def_write(){
	EX4DEF_TMP="$(tempfile -m 600 -p ex4)"
	cat << EOF > "${EX4DEF_TMP}"
# /etc/default/exim4
EX4DEF_VERSION='${EX4DEF_VERSION}'

# 'combined' -	 one daemon running queue and listening on SMTP port
# 'no'       -	 no daemon running the queue
# 'separate' -	 two separate daemons
# 'ppp'      -   only run queue with /etc/ppp/ip-up.d/exim4.
# 'nodaemon' - no daemon is started at all.
# 'queueonly' - only a queue running daemon is started, no SMTP listener.
# setting this to 'no' will also disable queueruns from /etc/ppp/ip-up.d/exim4
QUEUERUNNER='${EX4DEF_QUEUERUNNER}'
# how often should we run the queue
QUEUEINTERVAL='${EX4DEF_QUEUEINTERVAL}'
# options common to quez-runner and listening daemon
COMMONOPTIONS='${EX4DEF_COMMONOPTIONS}'
# more options for the daemon/process running the queue (applies to the one
# started in /etc/ppp/ip-up.d/exim4, too.
QUEUERUNNEROPTIONS='${EX4DEF_QUEUERUNNEROPTIONS}'
# special flags given to exim directly after the -q. See exim(8)
QFLAGS='${EX4DEF_QFLAGS}'
# Options for the SMTP listener daemon. By default, it is listening on
# port 25 only. To listen on more ports, it is recommended to use
# -oX 25:587:10025 -oP /run/exim4/exim.pid
SMTPLISTENEROPTIONS='${EX4DEF_SMTPLISTENEROPTIONS}'
EOF
	cat "${EX4DEF_TMP}" > "${defaultfile}"
	rm -f "${EX4DEF_TMP}"
}

## Parse commandline
TEMP=$(getopt -n update-exim4defaults \
	-l qflags:,queuerunner:,queuetime:,commonoptions:,queuerunneroptions:,smtplisteneroptions:,remove-common:,remove-queue:,remove-smtp:,force,help,init -- \
	+fh "$@")

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

eval set -- ${TEMP}
while test "$1" != "--"; do
	case $1 in
		-f|--force)
			EX4DEF_FORCE=true
		;;
		-h|--help)
			ex4def_usage
			exit 0
		;;
		--qflags)
			shift
			EX4DEF_QFLAGS="$1"
			;;
		--queuerunner)
			shift
			EX4DEF_QUEUERUNNER="$1"
			if ! expr match "${EX4DEF_QUEUERUNNER}" '\(ppp\|no\|combined\|nodaemon\|queueonly\|separate\)$' >/dev/null ; then
				echo "invalid argument ${EX4DEF_QUEUERUNNER} for --queuerunner" 1>&2
				exit 1
			fi
		;;
		--queuetime)
			shift
			EX4DEF_QUEUEINTERVAL="$1"
		;;
		--commonoptions)
			shift
			EX4DEF_COMMONOPTIONS="$1"
			EX4DEF_FLAGOPTIONS=true
		;;
		--queuerunneroptions)
			shift
			EX4DEF_QUEUERUNNEROPTIONS="$1"
			EX4DEF_FLAGOPTIONS=true
		;;
		--smtplisteneroptions)
			shift
			EX4DEF_SMTPLISTENEROPTIONS="$1"
			EX4DEF_FLAGOPTIONS=true
		;;
		--remove-common)
			shift
			EX4DEF_REMOVECOMMON="$1"
			EX4DEF_FLAGREMOVE=true
		;;
		--remove-queue)
			shift
			EX4DEF_REMOVEQUEUE="$1"
			EX4DEF_FLAGREMOVE=true
		;;
		--remove-smtp)
			shift
			EX4DEF_REMOVESMTP="$1"
			EX4DEF_FLAGREMOVE=true
		;;
		--init)
			EX4DEF_INIT=true
		;;
	esac
	shift
done
shift

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

if [ "${EX4DEF_FLAGREMOVE}" = "true" ] && [ "${EX4DEF_FLAGOPTIONS}" = "true" ] ; then
	echo "Cannot use --remove-something together with --somethingoptions" >&2
	ex4def_usage >&2
	exit 1
fi

#if [ ! -r ${defaultfile} ]; then
#	echo "Cannot read ${defaultfile}, terminating" >&2
#	exit 1
#fi

if "${EX4DEF_INIT}" = "true" ] ; then
	[ -e "${defaultfile}" ] && [ "${EX4DEF_FORCE}" != "true" ] && exit 0
	# Reset to default values
	EX4DEF_QUEUERUNNER='combined'
	EX4DEF_QUEUEINTERVAL='30m'
	EX4DEF_COMMONOPTIONS=''
	EX4DEF_QUEUERUNNEROPTIONS=''
	EX4DEF_QFLAGS=''
	EX4DEF_SMTPLISTENEROPTIONS=''
	
	ex4def_write
	exit 0
fi

#Try removing
if [ "${EX4DEF_FLAGREMOVE}" = "true" ] ; then
	EX4DEF_REMOVEERROR="false"
	if [ ! -z "${EX4DEF_REMOVECOMMON}" ] ; then
		EX4DEF_COMMONOPTIONS=$(echo "${COMMONOPTIONS}" | \
			sed -e "s�${EX4DEF_REMOVECOMMON}��" -e "s/  / /g" -e 's/^ //' -e 's/ $//')
		[ "${EX4DEF_COMMONOPTIONS}" = "${COMMONOPTIONS}" ] && \
			EX4DEF_REMOVEERROR="true"
	fi
	if [ ! -z "${EX4DEF_REMOVEQUEUE}" ] ; then
		EX4DEF_QUEUERUNNEROPTIONS=$(echo "${QUEUERUNNEROPTIONS}" | \
			sed -e "s�${EX4DEF_REMOVEQUEUE}��" -e "s/  / /g" -e 's/^ //' -e 's/ $//')
		[ "${EX4DEF_QUEUERUNNEROPTIONS}" = "${QUEUERUNNEROPTIONS}" ] && \
			EX4DEF_REMOVEERROR="true"
	fi
	if [ ! -z "${EX4DEF_REMOVESMTP}" ] ; then
		EX4DEF_SMTPLISTENEROPTIONS=$(echo "${SMTPLISTENEROPTIONS}" | \
			sed -e "s�${EX4DEF_REMOVESMTP}��" -e "s/  / /g" -e 's/^ //' -e 's/ $//')
		[ "${EX4DEF_SMTPLISTENEROPTIONS}" = "${SMTPLISTENEROPTIONS}"] && \
			EX4DEF_REMOVEERROR="true"
	fi
	if [ "${EX4DEF_REMOVEERROR}" = "true" ] ; then
		echo "$0: removing failed, no changes" >&2
		exit 64
	fi

	EX4DEF_TMP="$(tempfile -m 600 -p ex4)"
	sed -e "s�^QFLAGS=.*�QFLAGS='${EX4DEF_QFLAGS}'�" \
		-e "s�^QUEUERUNNER=.*�QUEUERUNNER='${EX4DEF_QUEUERUNNER}'�" \
		-e "s�^QUEUEINTERVAL=.*�QUEUEINTERVAL='${EX4DEF_QUEUEINTERVAL}'�" \
		-e "s�^COMMONOPTIONS=.*�COMMONOPTIONS='${EX4DEF_COMMONOPTIONS}'�" \
		-e "s�^QUEUERUNNEROPTIONS=.*�QUEUERUNNEROPTIONS='${EX4DEF_QUEUERUNNEROPTIONS}'�" \
		-e "s�^SMTPLISTENEROPTIONS=.*�SMTPLISTENEROPTIONS='${EX4DEF_SMTPLISTENEROPTIONS}'�" \
		< $defaultfile > "${EX4DEF_TMP}"
	mv "${EX4DEF_TMP}" $defaultfile
	rm -f "${EX4DEF_TMP}"
	exit 0
fi

if [ "${EX4DEF_FORCE}" = "true" ] ; then
	ex4def_write
	exit 0

else
	EX4DEF_DOANYTHING=0
	EX4DEF_NOTALLOWED=0
	if [ "${QUEUERUNNER}" != "${EX4DEF_QUEUERUNNER}" ]; then
		EX4DEF_DOANYTHING=$((${EX4DEF_DOANYTHING}+1))
		# switching the QUEUERUNNER modus is always allowed
		#[ -z "${QUEUERUNNER}" ] || EX4DEF_NOTALLOWED=$((${EX4DEF_NOTALLOWED}+1))
	fi
	if [ "${QUEUEINTERVAL}" != "${EX4DEF_QUEUEINTERVAL}" ] ; then
		EX4DEF_DOANYTHING=$((${EX4DEF_DOANYTHING}+2))
		[ -z "${QUEUEINTERVAL}" ] || \
			EX4DEF_NOTALLOWED=$((${EX4DEF_NOTALLOWED}+2))
	fi
	if [ "${COMMONOPTIONS}" != "${EX4DEF_COMMONOPTIONS}" ] ; then
		EX4DEF_DOANYTHING=$((${EX4DEF_DOANYTHING}+4))
		[ -z "${COMMONOPTIONS}" ] || \
			EX4DEF_NOTALLOWED=$((${EX4DEF_NOTALLOWED}+4))
	fi
	if [ "${QUEUERUNNEROPTIONS}" != "${EX4DEF_QUEUERUNNEROPTIONS}" ] ; then
		EX4DEF_DOANYTHING=$((${EX4DEF_DOANYTHING}+8))
		[ -z "${QUEUERUNNEROPTIONS}" ] || \
			EX4DEF_NOTALLOWED=$((${EX4DEF_NOTALLOWED}+8))
	fi
	if [ "${SMTPLISTENEROPTIONS}" != "${EX4DEF_SMTPLISTENEROPTIONS}" ] ; then
		EX4DEF_DOANYTHING=$((${EX4DEF_DOANYTHING}+16))
		[ -z "${SMTPLISTENEROPTIONS}" ] || \
			EX4DEF_NOTALLOWED=$((${EX4DEF_NOTALLOWED}+16))
	fi
	if [ "${QFLAGS}" != "${EX4DEF_QFLAGS}" ] ; then
		EX4DEF_DOANYTHING=$((${EX4DEF_DOANYTHING}+32))
		[ -z "${QFLAGS}" ] || \
			EX4DEF_NOTALLOWED=$((${EX4DEF_NOTALLOWED}+32))
	fi

	[ ${EX4DEF_DOANYTHING} -eq 0 ] && exit 0
	if [ ${EX4DEF_NOTALLOWED} -ne 0 ] ; then
		echo "setting(s) conflict with current one, terminating" >&2
		exit ${EX4DEF_NOTALLOWED}
	fi
	
	EX4DEF_TMP="$(tempfile -m 600 -p ex4)"
	sed -e "s�^QFLAGS=.*�QFLAGS='${EX4DEF_QFLAGS}'�" \
		-e "s�^QUEUERUNNER=.*�QUEUERUNNER='${EX4DEF_QUEUERUNNER}'�" \
		-e "s�^QUEUEINTERVAL=.*�QUEUEINTERVAL='${EX4DEF_QUEUEINTERVAL}'�" \
		-e "s�^COMMONOPTIONS=.*�COMMONOPTIONS='${EX4DEF_COMMONOPTIONS}'�" \
		-e "s�^QUEUERUNNEROPTIONS=.*�QUEUERUNNEROPTIONS='${EX4DEF_QUEUERUNNEROPTIONS}'�" \
		-e "s�^SMTPLISTENEROPTIONS=.*�SMTPLISTENEROPTIONS='${EX4DEF_SMTPLISTENEROPTIONS}'�" \
		< $defaultfile > "${EX4DEF_TMP}"
	mv "${EX4DEF_TMP}" $defaultfile
	rm -f "${EX4DEF_TMP}"
	exit 0
fi