summaryrefslogtreecommitdiffstats
path: root/debian/open-infrastructure-dehydrated-tools.postinst
blob: 3eb3a04228ebee1832742d6a06920d4fb6fc9bc6 (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
#!/bin/sh

set -e

. /usr/share/debconf/confmodule

CONFFILE="/etc/dehydrated/conf.d/config.sh"

Install ()
{
	DEFAULT="${1}"
	TARGET="${2}"

	mkdir -p "${DEFAULT}" > /dev/null 2>&1 || true
	mkdir -p "${TARGET}" > /dev/null 2>&1 || true

	if [ "${TARGET}" != "${DEFAULT}" ]
	then
		if [ -h "${DEFAULT}" ]
		then
			rm -f "${DEFAULT}"
			ln -s "${TARGET}" "${DEFAULT}"
		else
			if [ -e "${DEFAULT}" ] && [ -z "$(ls -A ${DEFAULT})" ]
			then
				rmdir "${DEFAULT}"
				ln -s "${TARGET}" "${DEFAULT}"
			elif [ -n "$(ls -A ${DEFAULT})" ] && [ -z "$(ls -A ${TARGET})" ]
			then
				rmdir "${TARGET}"
				mv "${DEFAULT}" "${TARGET}"
				ln -s "${TARGET}" "${DEFAULT}"
			fi
		fi
	fi

	if ! dpkg-statoverride --list "${DEFAULT}" > /dev/null 2>&1 &&
	   ! dpkg-statoverride --list "${TARGET}" > /dev/null 2>&1
	then
		if getent group ssl-cert > /dev/null 2>&1
		then
			GROUP="ssl-cert"
		else
			GROUP="root"
		fi

		chmod 0770 "${TARGET}"
		chown root:"${GROUP}" "${TARGET}"

		chmod 0770 "${DEFAULT}"
		chown root:"${GROUP}" "${DEFAULT}"
	fi
}

case "${1}" in
	configure)
		db_get open-infrastructure-dehydrated-tools/ca
		CA="${RET}" # select

		db_get open-infrastructure-dehydrated-tools/auto-cleanup
		AUTO_CLEANUP="${RET}" # boolean

		db_get open-infrastructure-dehydrated-tools/challengetype
		CHALLENGETYPE="${RET}" # select

		db_get open-infrastructure-dehydrated-tools/contact-email
		CONTACT_EMAIL="${RET}" # string (w/ empty)

		db_get open-infrastructure-dehydrated-tools/key-algo
		KEY_ALGO="${RET}" # select

		db_get open-infrastructure-dehydrated-tools/ocsp-fetch
		OCSP_FETCH="${RET}" # boolean

		db_get open-infrastructure-dehydrated-tools/ocsp-must-staple
		OCSP_MUST_STAPLE="${RET}" # boolean

		db_get open-infrastructure-dehydrated-tools/preferred-chain
		PREFERRED_CHAIN="${RET}" # string w/ empty

		db_get open-infrastructure-dehydrated-tools/hooks
		HOOKS="${RET}" # multi-select (w/ empty)

		db_get open-infrastructure-dehydrated-tools/basedir
		NEW_BASEDIR="${RET}" # string (w/o empty)

		db_get open-infrastructure-dehydrated-tools/domains
		DOMAINS="${RET}" # string (w/ empty)

		db_get open-infrastructure-dehydrated-tools/tsig
		TSIG="${RET}" # string (w/ empty)

		db_get open-infrastructure-dehydrated-tools/register
		REGISTER="${RET}" # boolean

		db_get open-infrastructure-dehydrated-tools/run
		RUN="${RET}" # boolean

		db_stop

		case "${AUTO_CLEANUP}" in
			true)
				AUTO_CLEANUP="yes"
				;;

			false)
				AUTO_CLEANUP="no"
				;;
		esac

		case "${OCSP_FETCH}" in
			true)
				OCSP_FETCH="yes"
				;;

			false)
				OCSP_FETCH="no"
				;;
		esac

		case "${OCSP_MUST_STAPLE}" in
			true)
				OCSP_MUST_STAPLE="yes"
				;;

			false)
				OCSP_MUST_STAPLE="no"
				;;
		esac

		HOOK="/usr/bin/dehydrated-hook"

		if [ ! -e "${CONFFILE}" ]
		then

cat > "${CONFFILE}" << EOF
# /etc/dehydrated/conf.d/config.sh

AUTO_CLEANUP="${AUTO_CLEANUP}"
CA="${CA}"
CHALLENGETYPE="${CHALLENGETYPE}"
CONTACT_EMAIL="${CONTACT_EMAIL}"
HOOK="${HOOK}"
KEY_ALGO="${KEY_ALGO}"
OCSP_FETCH="${OCSP_FETCH}"
OCSP_MUST_STAPLE="${OCSP_MUST_STAPLE}"
PREFERRED_CHAIN="${PREFERRED_CHAIN}"
EOF

		fi

		cp -a -f "${CONFFILE}" "${CONFFILE}.tmp"

		# If the admin deleted or commented some variables but then set
		# them via debconf, (re-)add them to the config file.

		test -z "${AUTO_CLEANUP}" || \
		grep -Eq '^ *AUTO_CLEANUP=' "${CONFFILE}" || \
		echo "AUTO_CLEANUP=" >> "${CONFFILE}"

		test -z "${CA}" || \
		grep -Eq '^ *CA=' "${CONFFILE}" || \
		echo "CA=" >> "${CONFFILE}"

		test -z "${CHALLENGETYPE}" || \
		grep -Eq '^ *CHALLENGETYPE=' "${CONFFILE}" || \
		echo "CHALLENGETYPE=" >> "${CONFFILE}"

		test -z "${CONTACT_EMAIL}" || \
		grep -Eq '^ *CONTACT_EMAIL=' "${CONFFILE}" || \
		echo "CONTACT_EMAIL=" >> "${CONFFILE}"

		test -z "${HOOK}" || \
		grep -Eq '^ *HOOK=' "${CONFFILE}" || \
		echo "HOOK=" >> "${CONFFILE}"

		test -z "${KEY_ALGO}" || \
		grep -Eq '^ *KEY_ALGO=' "${CONFFILE}" || \
		echo "KEY_ALGO=" >> "${CONFFILE}"

		test -z "${OCSP_FETCH}" || \
		grep -Eq '^ *OCSP_FETCH=' "${CONFFILE}" || \
		echo "OCSP_FETCH=" >> "${CONFFILE}"

		test -z "${OCSP_MUST_STAPLE}" || \
		grep -Eq '^ *OCSP_MUST_STAPLE=' "${CONFFILE}" || \
		echo "OCSP_MUST_STAPLE=" >> "${CONFFILE}"

		test -z "${PREFERRED_CHAIN}" || \
		grep -Eq '^ *PREFERRED_CHAIN=' "${CONFFILE}" || \
		echo "PREFERRED_CHAIN=" >> "${CONFFILE}"

		sed -e "s|^ *AUTO_CLEANUP=.*|AUTO_CLEANUP=\"${AUTO_CLEANUP}\"|" \
		    -e "s|^ *CA=.*|CA=\"${CA}\"|" \
		    -e "s|^ *CHALLENGETYPE=.*|CHALLENGETYPE=\"${CHALLENGETYPE}\"|" \
		    -e "s|^ *CONTACT_EMAIL=.*|CONTACT_EMAIL=\"${CONTACT_EMAIL}\"|" \
		    -e "s|^ *HOOK=.*|HOOK=\"${HOOK}\"|" \
		    -e "s|^ *KEY_ALGO=.*|KEY_ALGO=\"${KEY_ALGO}\"|" \
		    -e "s|^ *OCSP_FETCH=.*|OCSP_FETCH=\"${OCSP_FETCH}\"|" \
		    -e "s|^ *OCSP_MUST_STAPLE=.*|OCSP_MUST_STAPLE=\"${OCSP_MUST_STAPLE}\"|" \
		    -e "s|^ *PREFERRED_CHAIN=.*|PREFERRED_CHAIN=\"${PREFERRED_CHAIN}\"|" \
		< "${CONFFILE}" > "${CONFFILE}.tmp"

		mv -f "${CONFFILE}.tmp" "${CONFFILE}"

		for HOOK in $(cd /usr/share/dehydrated/hooks && find -maxdepth 1 -not -type d -printf '%P\n' | sort)
		do
			if [ -L "/etc/dehydrated/hook.d/${HOOK}" ]
			then
				rm -f "/etc/dehydrated/hook.d/${HOOK}"
			fi
		done

		if [ -n "${HOOKS}" ]
		then
			HOOKS="$(echo ${HOOKS} | sed -e 's|,| |g')"

			if echo "${HOOKS}" | grep -qs "ALL"
			then
				HOOKS="$(cd /usr/share/dehydrated/hooks && find -maxdepth 1 -not -type d -printf '%P\n' | sort)"
			fi

			for HOOK in ${HOOKS}
			do
				if [ ! -e "/etc/dehydrated/hook.d/${HOOK}" ] && [ -e "/usr/share/dehydrated/hooks/${HOOK}" ]
				then
					ln -sf "/usr/share/dehydrated/hooks/${HOOK}" "/etc/dehydrated/hook.d/${HOOK}"
				fi
			done
		fi

		for FILE in /etc/dehydrated/config /etc/dehydrated/conf.d/*.sh
		do
			if [ -e "${FILE}" ]
			then
				. ${FILE} || true
			fi
		done

		if [ -n "${NEW_BASEDIR}" ] && [ "${BASEDIR}" != "${NEW_BASEDIR}" ]
		then
			rmdir "${BASEDIR}/acme-challenges" > /dev/null 2>&1 || true
			Install "${BASEDIR}" "${NEW_BASEDIR}"
			mkdir -p "${BASEDIR}/acme-challenges"
		fi

		if [ -n "${DOMAINS}" ] && [ "${DOMAINS}" != "none" ]
		then
			rm -f /etc/dehydrated/domains.txt

			GROUPS="$(echo ${DOMAINS} | sed -e 's/ /#/g' -e 's/|/ /g')"

			for GROUP in ${GROUPS}
			do
				DOMAINS="$(echo ${GROUP} | sed -e 's/#/ /g' -e 's/^ //g')"

				echo "${DOMAINS}" >> /etc/dehydrated/domains.txt
			done
		fi

		if [ -n "${TSIG}" ]
		then
			case "${TSIG}" in
				http*)
					# tsig is a URL
					echo -n "Downloading tsig.key from '${TSIG}'..."

					if command -v wget -q "${TSIG}" -O /dev/null > /dev/null 2>&1
					then
						rm -f /etc/dehydrated/tsig.key

						wget -q "${TSIG}" -O /etc/dehydrated/tsig.key
						chmod 0600 /etc/dehydrated/tsig.key

						TSIG_FILE="/etc/dehydrated/tsig.key"
					elif command -v curl -s "${TSIG}" -o /dev/null > /dev/null 2>&1
					then
						rm -f /etc/dehydrated/tsig.key

						curl -s "${TSIG}" -o /etc/dehydrated/tsig.key
						chmod 0600 /etc/dehydrated/tsig.key

						TSIG_FILE="/etc/dehydrated/tsig.key"
					else
						echo
						echo "W: need wget or curl" >&2
					fi

					if [ -n "${TSIG_FILE}" ]
					then
						echo " done."
					fi
					;;

				*:*)
					# tsig is a string
					echo "${TSIG}" > /etc/dehydrated/tsig.key
					chmod 0600 /etc/dehydrated/tsig.key

					TSIG_FILE="/etc/dehydrated/tsig.key"
					;;

				*/*)
					# tsig is a path
					TSIG_FILE="${TSIG}"
					;;

				*)
					echo "'${TSIG}' is neither a valid tsig nor a path to an existing file - ignoring"
					;;
			esac
		fi

		if [ -n "${TSIG_FILE}" ]
		then
			DEFAULT_FILE="/etc/default/dehydrated-nsupdate"

			if [ ! -e "${DEFAULT_FILE}" ]
			then

cat > "${DEFAULT_FILE}" << EOF
# ${DEFAULT_FILE}

TSIG_KEYFILE="${TSIG_FILE}"
EOF

			fi

			cp -a -f "${DEFAULT_FILE}" "${DEFAULT_FILE}.tmp"

			# If the admin deleted or commented some variables but then set
			# them via debconf, (re-)add them to the config file.

			test -z "${TSIG_FILE}" || \
			grep -Eq '^ *TSIG_KEYFILE=' "${DEFAULT_FILE}" || \
			echo "TSIG_KEYFILE=" >> "${DEFAULT_FILE}"

			sed -e "s|^ *TSIG_KEYFILE=.*|TSIG_KEYFILE=\"${TSIG_FILE}\"|" \
			< "${DEFAULT_FILE}" > "${DEFAULT_FILE}.tmp"

			mv -f "${DEFAULT_FILE}.tmp" "${DEFAULT_FILE}"
		fi

		case "${REGISTER}" in
			true)
				dehydrated --register --accept-terms
				;;
		esac

		case "${RUN}" in
			true)
				dehydrated --cron --keep-going
				;;
		esac

		;;

	abort-upgrade|abort-remove|abort-deconfigure)

		;;

	*)
		echo "postinst called with unknown argument \`${1}'" >&2
		exit 1
		;;
esac

#DEBHELPER#

exit 0