summaryrefslogtreecommitdiff
path: root/debian/apache2.postinst
blob: 177a94936f117de621a8eaf37b6de208d6b8e057 (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
#! /bin/bash
# postinst script
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#       * <postinst> `configure' <most-recently-configured-version>
#       * <old-postinst> `abort-upgrade' <new version>
#       * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#         <new-version>
#       * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#         <failed-install-package> <version> `removing'
#         <conflicting-package> <version>
#
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

# XXX remove
# echo apache2.postinst $@ running

OBSOLETE_CONFFILES="/etc/apache2/mods-available/authz_default.load
/etc/apache2/mods-available/authn_default.load
/etc/apache2/mods-available/mem_cache.load
/etc/apache2/mods-available/mem_cache.conf
/etc/apache2/mods-available/authn_alias.load
/etc/apache2/mods-available/cern_meta.load
/etc/apache2/mods-available/disk_cache.load
/etc/apache2/mods-available/disk_cache.conf
/etc/apache2/mods-available/ident.load
/etc/apache2/mods-available/imagemap.load
/etc/apache2/mods-available/version.load"


MOVED_CONFFILES="/etc/bash_completion.d/apache2.2-common:/etc/bash_completion.d/apache2
/etc/apache2/sites-available/default:/etc/apache2/sites-available/000-default.conf
/etc/apache2/sites-available/default-ssl:/etc/apache2/sites-available/default-ssl.conf
/etc/apache2/conf.d/charset:/etc/apache2/conf-available/charset.conf
/etc/apache2/conf.d/localized-error-pages:/etc/apache2/conf-available/localized-error-pages.conf
/etc/apache2/conf.d/other-vhosts-access-log:/etc/apache2/conf-available/other-vhosts-access-log.conf
/etc/apache2/conf.d/security:/etc/apache2/conf-available/security.conf"


# the functions below need to be idempotent, however we guess the upgrade based
# on obsolete conffiles which might be removed anytime. Thus, remember we were
# already in there once.
WHEEZY_UPGRADE=0

#      n.b you can't rely on $2 (the last installed version)  for upgrades, as
#      the user might have been using apache2.2-common only. Let's pretend we're
#      upgrading if there is either a /etc/apache2/.apache2_mpm_selected file
#      around or an /etc/apache2/.apache2_upgrade file exists.
we_are_upgrading_from_wheezy()
{
	if [ -n "$2" ] && dpkg --compare-versions "$2" le-nl "2.3"; then
		WHEEZY_UPGRADE=1
		return 0
	fi

	# this check is a probably tautology but makes it easier to understand
	# the logic
	if [ -n "$2" ] && dpkg --compare-versions "$2" ge-nl "2.3"; then
		return 1
	fi

	if [ -e /etc/apache2/.apache2_mpm_selected ] ; then
		WHEEZY_UPGRADE=1
		return 0
	fi

	if [ "$WHEEZY_UPGRADE" -eq 1 ] ; then
		return 0
	fi

	for CONFFILE in $OBSOLETE_CONFFILES ; do
		if [ -e "$CONFFILE.dpkg-backup" ] || [ -e "$CONFFILE.dpkg-remove" ] ; then
			WHEEZY_UPGRADE=1
			return 0
		fi
	done

	for CONFFILE in $MOVED_CONFFILES ; do
		OLDCONFFILE=$( echo "$CONFFILE" | cut -d: -f1 )
		if [ -e "$OLDCONFFILE.dpkg-remove" ] ; then
			WHEEZY_UPGRADE=1
			return 0
		fi
	done

	return 1
}

is_fresh_install()
{
	if [ -z "$2" ] ; then
		return 0
	fi
	return 1
}


# The two functions below are licensed GPL-2+ and was written by dpkg maintainers
# See the dpkg-maintscript-helper script for details

remove_conffiles()
{
	# we can't use dpkg-maintscript-helper as we shifted conffiles from the
	# apache2.2-common package to apache2, too. The tool can cope with
	# that, but additionally we didn't require apache2 to be installed. This
	# yields the wrong result when upgrading such an installation
	if we_are_upgrading_from_wheezy $@ ; then
		for CONFFILE in $OBSOLETE_CONFFILES ; do
			if [ -e "$CONFFILE.dpkg-backup" ]; then
				mv -f "$CONFFILE.dpkg-backup" "$CONFFILE.dpkg-bak"
			fi
			if [ -e "$CONFFILE.dpkg-remove" ]; then
				echo "Removing obsolete conffile $CONFFILE ..."
				rm -f "$CONFFILE.dpkg-remove"
			fi
		done
	fi
}

mv_conffiles()
{
	# same rationale as above
	if we_are_upgrading_from_wheezy $@ ; then
		for CONFFILE in $MOVED_CONFFILES ; do
			OLDCONFFILE=$( echo "$CONFFILE" | cut -d: -f1 )
			NEWCONFFILE=$( echo "$CONFFILE" | cut -d: -f2 )

			rm -f $OLDCONFFILE.dpkg-remove
			[ -e "$OLDCONFFILE" ] || continue

			echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
			mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
			mv -f "$OLDCONFFILE" "$NEWCONFFILE"
		done

		if [ -d /etc/apache2/conf.d ] && [ ! "$(ls -A /etc/apache2/conf.d)" ] ; then
			echo "Removing obsolete directory /etc/apache2/conf.d"
			rmdir /etc/apache2/conf.d
		fi

		if [ -d /etc/apache2/conf.d ] && [ "$(ls -A /etc/apache2/conf.d)" ]	; then
			echo "Directory /etc/apache2/conf.d is not empty - leaving as is"
			echo "Please note, that directory is considered obsolete and not read anymore by default"
			# XXX order of processing??? this may become empty later on (after upgrade of apache2-doc)
			ls -A /etc/apache2/conf.d
		fi
	fi
}


enable_default_mpm()
{
	mpm="mpm_event"
	if we_are_upgrading_from_wheezy $@ && [ -e /etc/apache2/.apache2_mpm_selected  ]; then
		tmpmpm=$(grep -v "^#" /etc/apache2/.apache2_mpm_selected | head -n1)
		case "$tmpmpm" in
			apache2-mpm-worker)
				mpm="mpm_worker"
			;;

			apache2-mpm-event)
				mpm="mpm_event"
			;;

			apache2-mpm-prefork)
				mpm="mpm_prefork"
			;;

			apache2-mpm-itk)
				mpm="mpm_itk"
			;;

			*)
				# default MPM for upgrading in case we got an unrecognized
				# hint file
				mpm="mpm_event"
			;;
		esac

		# No -m here, we pretend the user picked the MPM as this choice comes
		# from a 2.2 package relation
		a2enmod -q $mpm
		return 0
	fi

	if is_fresh_install $@ ; then
		a2enmod -m -q $mpm
	fi

}

enable_default_modules()
{
	if is_fresh_install $@; then
		for module in authz_host auth_basic access_compat authn_file authz_user \
				alias dir autoindex \
				env mime negotiation setenvif \
				filter deflate \
				status ; do
			a2enmod -m -q $module
		done
	elif we_are_upgrading_from_wheezy $@; then
		for module in authn_core authz_core filter access_compat ; do
			a2enmod -m -q $module
		done
	fi
}

enable_default_conf()
{
	if is_fresh_install $@ || we_are_upgrading_from_wheezy $@ ; then
		for conf in charset localized-error-pages other-vhosts-access-log security ; do
			a2enconf -m -q $conf
		done
	fi
	# This line must catch upgrades, upgrades from Wheezy und fresh
	# installs
	if dpkg --compare-versions "$2" "le" "2.4.1-4" ; then
		a2enconf -m -q serve-cgi-bin
	fi
}

install_default_site()
{
	if we_are_upgrading_from_wheezy $@ ; then
		# by here, the old default sites were already renamed. Thus, the links
		# are dangling
		for SITE in /etc/apache2/sites-enabled/000-default /etc/apache2/sites-enabled/default-ssl ; do
			if [ -L $SITE ] ; then
				target=$(readlink -e "$SITE") || true
				sitename=$(basename "$SITE")
				if [ -z "$target" ] ; then
					rm -f $SITE
					a2ensite -q "$sitename"
				fi
			fi
		done
	elif is_fresh_install $@ ; then
		if [ ! -L /etc/apache2/sites-enabled/000-default.conf -a \
			! -f /etc/apache2/sites-enabled/000-default.conf ]; then
				a2ensite -q 000-default
		fi

		touch /var/log/apache2/error.log /var/log/apache2/access.log
		chown root:adm /var/log/apache2/error.log /var/log/apache2/access.log
		chmod 0640 /var/log/apache2/error.log /var/log/apache2/access.log

		touch /var/log/apache2/other_vhosts_access.log
		chown root:adm /var/log/apache2/other_vhosts_access.log
		chmod 0640 /var/log/apache2/other_vhosts_access.log
	fi
}

# XXX: This site is installed in the apache2-data package. Should the postinst
# scriptlet move there too?
install_default_files()
{
	if is_fresh_install $@ ; then
		if [ ! -e /var/www/index.html  -a \
			! -h /var/www/index.html  -a \
			! -e /var/www/index.cgi   -a \
			! -e /var/www/index.pl    -a \
			! -e /var/www/index.php   -a \
			! -e /var/www/index.xhtml -a \
			! -e /var/www/index.htm ] ; then
				cp /usr/share/apache2/default-site/index.html /var/www/index.html
		fi
	fi
}

# XXX: Find out whether I am on crack removing stale modules that way
refresh_modules()
{
	if we_are_upgrading_from_wheezy $@ && [ -d /etc/apache2/mods-enabled/ ] ; then
		shopt -s nullglob
		for link in /etc/apache2/mods-enabled/*.load ; do
			target=$(readlink "$link") || true
			if [ -z "$target" ] ; then
				continue
			fi

			module=$(basename "$link" | sed 's/\.load//') || true

			if [ ! -e "/etc/apache2/mods-enabled/$target" ] ; then
				echo "disable obsolete module $module"
				a2dismod -m -q "$module"

				if [ "$module" = "disk_cache" ] ; then
					echo "Enable cache_disk as disk_cache was enabled in Apache 2.2"
					# ditto, we pretend it was the user's
					# choice not to use -m here
					a2enmod -q cache_disk
				fi
			fi
			# the module is already enabled, however
			# dependencies could have changed hence re-call
			# a2enmod again.
			# Example: the deflate module when upgraded from
			# Wheezy
			if [ -e "/etc/apache2/mods-enabled/$target" ] ; then
				a2enmod -m -q "$module"
			fi
		done
	fi
}

move_httpd_conf()
{
	if we_are_upgrading_from_wheezy $@ ; then
		if [ -e /etc/apache2/httpd.conf ] && [ -f /etc/apache2/httpd.conf ] ; then
			local md5sum="$(md5sum /etc/apache2/httpd.conf | sed -e 's/ .*//')"
			if [ $md5sum = "d41d8cd98f00b204e9800998ecf8427e" ] ||
			   [ $md5sum = "a20c3e53dd07836481a5e64bc71e1a33" ]
			then
				echo "Remove obsolete configuration file /etc/apache2/httpd.conf"
				rm -f /etc/apache2/httpd.conf
			else
				if [ -d /etc/apache2/conf-available/ ] && [ ! -f /etc/apache2/conf-available/httpd.conf ] ; then
					echo "Detected legacy httpd.conf - moving file to /etc/apache2/conf-available/httpd.conf"
					mv /etc/apache2/httpd.conf /etc/apache2/conf-available/httpd.conf
					a2enconf -q httpd
				fi
			fi
		fi
	fi
}

migrate_data()
{
	#XXX: jimjag recommends purging the cache albeit it is probably not
	#     technically required.
	#if we_are_upgrading_from_wheezy $@ ; then
	#	# /var/cache/apache2/mod_disk_cache -> /var/cache/apache2/mod_cache_disk
	#	if [ -d /var/cache/apache2/mod_disk_cache ] && [ "$(ls -A /var/cache/apache2/mod_disk_cache)" ] ; then
	#		echo "Migrate mod_disk_cache cache data to /var/cache/apache2/mod_cache_disk/"
	#		mv /var/cache/apache2/mod_disk_cache/* /var/cache/apache2/mod_cache_disk/
	#		rmdir /var/cache/apache2/mod_disk_cache
	#	fi
	#fi
	if we_are_upgrading_from_wheezy $@ ; then
		if [ -d /var/cache/apache2/mod_disk_cache ] ; then
			echo "Purge obsolete mod_disk_cache cache data in /var/cache/apache2/mod_cache_disk/"
			rm -rf /var/cache/apache2/mod_disk_cache
		fi
	fi
}

#XXX: Deal with the sites-available/sites-enabled *.conf transition, e.g. rename
#     all files which look like site configuration?

case "$1" in
	configure)

		remove_conffiles $@
		mv_conffiles $@
		enable_default_mpm $@
		refresh_modules $@
		install_default_files $@
		enable_default_modules $@
		enable_default_conf $@
		install_default_site $@
		move_httpd_conf $@
		migrate_data $@

		# post installation cleanup
		if [ -e /etc/apache2/.apache2_mpm_selected ] ; then
			rm -f /etc/apache2/.apache2_mpm_selected
		fi

	;;

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

	;;

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

#DEBHELPER#

# And now the traditional insanity of apache2 upgrades (see #390823):
# If everything went well, we need to purge apache2.2-common's postrm, or
# purging that package will remove our logs, caches, ...
if [ "$1" = configure ] && we_are_upgrading_from_wheezy $@ ; then
	oldpostrm=$(dpkg-query -c apache2.2-common postrm  2>/dev/null || true)
	if [ -n "$oldpostrm" ] ; then
		rm -f "$oldpostrm"
	fi
fi

exit 0