b3f9b688d8bad3ebb311b5557497f5416e83b6d4
[kernel-cn.git] / debian / postinst
1 #!/bin/sh
2 # postinst script for spamassassin-cn
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
8 # summary of how this script can be called:
9 #        * <postinst> `configure' <most-recently-configured-version>
10 #        * <old-postinst> `abort-upgrade' <new version>
11 #        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
12 #          <new-version>
13 #        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
14 #          <failed-install-package> <version> `removing'
15 #          <conflicting-package> <version>
16 # for details, see http://www.debian.org/doc/debian-policy/ or
17 # the debian-policy package
18 #
19
20 case "$1" in
21     configure|reconfigure)
22       # continue below
23     ;;
24
25     *)
26         exit 0
27     ;;
28 esac
29
30 # import CN-functions
31 . /usr/share/carnet-tools/functions.sh
32
33 ################################################################################
34
35 # starting up backup
36 echo -n "CN: Backed up to /var/backups:"
37
38 # backup lilo.conf
39 if [ -e /etc/lilo.conf ]; then
40   cp_backup_conffile /etc/lilo.conf
41   echo -n " lilo.conf"
42 fi
43
44 # backup old kernel params
45 if [ -e /etc/sysctl.conf ]; then
46   cp_backup_conffile /etc/sysctl.conf
47   echo -n " sysctl.conf"
48 fi
49
50 # backup old kernel params
51 if [ -e /etc/kernel-img.conf ]; then
52   cp_backup_conffile /etc/kernel-img.conf
53   echo -n " kernel-img.conf"
54 fi
55
56 # finished
57 echo "."
58
59 ################################################################################
60
61 # remove obsolete symlinks and kernels
62 rm -f /boot/vmlinuz /boot/vmlinuz.old /boot/vmlinuz.old2 \
63   /boot/vmlinuz.plain /vmlinuz /vmlinuz.old /boot/vmlinuz.plain \
64   /boot/vmlinuz.generic /boot/vmlinuz-generic /boot/vmlinuz-old
65 echo "CN: Removed old symlinks in / and /boot."
66
67 ################################################################################
68
69 # intro msg
70 echo -n "CN: Configuring system (this will take a while):"
71
72 # generate kernel-img.conf
73 if [ ! -e /etc/kernel-img.conf ]; then
74     touch /etc/kernel-img.conf
75 fi
76
77 # update postinst_hook for grub
78 if grep -q postinst_hook /etc/kernel-img.conf; then
79     cp_check_and_sed '^postinst_hook' \
80       's;^postinst_hook[[:blank:]]*=.*;postinst_hook = /usr/sbin/update-grub;g' \
81           /etc/kernel-img.conf || true
82 else
83     echo "postinst_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
84 fi
85
86 # update postrm_hook for grub
87 if grep -q postrm_hook /etc/kernel-img.conf; then
88     cp_check_and_sed '^postrm_hook' \
89       's;^postrm_hook[[:blank:]]*=.*;postrm_hook = /usr/sbin/update-grub;g' \
90           /etc/kernel-img.conf || true
91 else
92     echo "postrm_hook = /usr/sbin/update-grub" >> /etc/kernel-img.conf
93 fi
94
95 # enable initrd
96 if grep -q do_initrd /etc/kernel-img.conf; then
97     cp_check_and_sed '^do_initrd' \
98       's/^do_initrd[[:blank:]]*=.*/do_initrd = yes/g' \
99           /etc/kernel-img.conf || true
100 else
101     echo "do_initrd = yes" >> /etc/kernel-img.conf
102 fi
103
104 echo -n " kernel-img.conf"
105
106 # generate initial grub loaders
107 if [ ! -d /boot/grub ]; then
108     mkdir -p /boot/grub
109     if [ -d /usr/lib/grub/i386-pc ]; then
110         cp -a /usr/lib/grub/i386-pc/* /boot/grub
111     fi
112 fi
113 echo -n " grub1"
114
115 # create/update grub configuration
116 if [ -e /boot/grub/menu.lst ]; then
117     # is there uncompatibile grub conf present?
118     if ! grep -q 'AUTOMAGIC KERNELS LIST' /boot/grub/menu.lst; then
119         mv -f /boot/grub/menu.lst /boot/grub/menu.lst.old
120     fi
121 fi
122 if [ ! -e /boot/grub/menu.lst ]; then
123     yes | /usr/sbin/update-grub >/dev/null 2>&1 || true
124 else
125     /usr/sbin/update-grub >/dev/null 2>&1 || true
126 fi
127 echo -n " grub2"
128
129 # workaround grsec
130 if [ -x /sbin/chpax ]; then
131     if [ -x /usr/sbin/grub ]; then
132         chpax -spmrx /usr/sbin/grub || true
133     fi
134     if [ -x /usr/sbin/grub-probe ]; then
135         chpax -spmrx /usr/sbin/grub-probe || true
136     fi
137 fi
138 echo -n " grub3"
139
140 # workaround execshield
141 SHIELD=$(sysctl -e -n kernel.exec-shield)
142 if [ ! -z "$SHIELD" ]; then
143     sysctl -w -e kernel.exec-shield=0 >/dev/null 2>&1
144 fi
145
146 # install grub loader
147 if ! grub-install --no-floppy '(hd0)' >/dev/null 2>&1; then
148     echo "."
149     echo "CN: FATAL ERROR running grub-install!"
150     echo "CN: Do not reboot your server and report this to OTRS immediately!"
151     exit 1
152 fi
153 echo -n " grub4"
154
155 # restore execshield state
156 if [ ! -z "$SHIELD" ]; then
157     sysctl -w -e "kernel.exec-shield=$SHIELD" >/dev/null 2>&1
158 fi
159
160 # disable lilo
161 if [ -e /etc/lilo.conf ]; then
162     mv -f /etc/lilo.conf /etc/lilo.conf.old
163 fi
164 echo -n " lilo"
165
166 # mdadm
167 if [ -x /usr/share/mdadm/mkconf ]; then
168     if [ ! -e /etc/mdadm/mdadm.conf ]; then
169         touch /etc/mdadm/mdadm.conf
170     fi
171
172     /usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf.$$
173     if ! cmp -s /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf; then
174         mv /etc/mdadm/mdadm.conf.$$ /etc/mdadm/mdadm.conf
175     fi
176     rm -f /etc/mdadm/mdadm.conf.$$ /etc/initramfs-tools/hooks/md \
177         /var/lib/mdadm/CONF-UNCHECKED
178     echo -n " mdadm"
179 fi
180
181 # update initramfs accordingly
182 update-initramfs -u -k all >/dev/null 2>&1 || true
183 echo -n " initramfs"
184
185 # finished
186 echo "."
187
188 ################################################################################
189
190 # rest of configuration...
191 echo -n "CN: Modifying the neccessary system files:"
192
193 # remove group 99
194 if getent group proc >/dev/null 2>&1; then
195     groupdel proc >/dev/null 2>&1
196 fi
197 echo -n " proc"
198
199 # remove oidentd from oident group
200 if getent group oident >/dev/null 2>&1; then
201     # sarge default
202     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup' \
203       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*nogroup/OIDENT_GROUP=oident/g' \
204           /etc/default/oidentd || true
205
206     # old kernel-2.6-cn default
207     cp_check_and_sed '^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc' \
208       's/^OIDENT_GROUP[[:blank:]]*=[[:blank:]]*proc/OIDENT_GROUP=oident/g' \
209           /etc/default/oidentd || true
210
211     echo -n " oidentd"
212 fi
213
214 # default kernel parameters
215 rm -f /etc/sysctl.conf.$$
216 cat > /etc/sysctl.conf.$$ <<EOF
217 kernel.exec-shield=3
218 kernel.maps_protect=1
219 net.core.rmem_default=1048576
220 net.core.wmem_default=1048576
221 net.ipv4.conf.all.accept_redirects=0
222 net.ipv4.conf.all.accept_source_route=0
223 net.ipv4.conf.all.log_martians=1
224 net.ipv4.conf.all.rp_filter=1
225 net.ipv4.conf.all.secure_redirects=1
226 net.ipv4.conf.all.send_redirects=0
227 net.ipv4.icmp_echo_ignore_broadcasts=1
228 net.ipv4.icmp_ignore_bogus_error_responses=1
229 net.ipv4.ip_forward=0
230 net.ipv4.ip_local_port_range=10000 65000
231 net.ipv4.tcp_congestion_control=cubic
232 net.ipv4.tcp_ecn=0
233 net.ipv4.tcp_max_syn_backlog=8192
234 net.ipv4.tcp_retries1=2
235 net.ipv4.tcp_rfc1337=1
236 net.ipv4.tcp_syncookies=1
237 vm.mmap_min_addr=65536
238 EOF
239
240 # old kernel params
241 if [ -e /etc/sysctl.conf ]; then
242   #cat /etc/sysctl.conf >> /etc/sysctl.conf.$$
243   #ignore some of 2.4 stuff
244   egrep -v 'net\.core\.(r|w)mem_max|net\.ipv4\.tcp_(r|w)mem|vm\.bdflush|net\.ipv4\.ip_local_port_range|kernel\.rtsig-max|net\.ipv4\.tcp_syncookies' \
245     /etc/sysctl.conf >> /etc/sysctl.conf.$$
246 fi
247
248 # add sysctl.conf Debian headers
249 rm -f /etc/sysctl.conf-head
250 cat > /etc/sysctl.conf-head <<EOF
251 #
252 # /etc/sysctl.conf - Configuration file for setting system variables
253 # See sysctl.conf (5) for information.
254 #
255 EOF
256
257 # merge old and new in one conf, primarily respecting old
258 script='
259 my %confhash = ();
260 my ($key, $value);
261 while (<>)
262 {
263   chomp();
264   if (/^\s*(\S+)\s*=\s*(.+)\s*$/o)
265   {
266     my ($key, $value) = ($1, $2);
267     $key =~ s/\//./go;
268     $confhash{$key} = $value;
269   }
270 }
271 for (sort { $a cmp $b } keys %confhash)
272 {
273   print $_, "=", $confhash{$_}, "\n";
274 }
275 '
276 rm -f /etc/sysctl.conf-new
277 perl -e "$script" < /etc/sysctl.conf.$$ > /etc/sysctl.conf-new
278 cat /etc/sysctl.conf-head /etc/sysctl.conf-new > /etc/sysctl.conf.$$
279 rm -f /etc/sysctl.conf-head /etc/sysctl.conf-new
280
281 # finished with merging, move into sysctl.conf
282 cp_mv /etc/sysctl.conf.$$ /etc/sysctl.conf
283 echo -n " sysctl.conf"
284
285 # finished with basic kernel-2.6-cn stuff
286 echo "."
287
288 ################################################################################
289
290 # intro
291 echo -n "CN: Setting up PAM configurations:"
292
293 # update pam_limits accordingly
294 if [ -e /etc/security/limits.conf ]; then
295   rm -f /etc/security/limits.conf.$$
296   cp /etc/security/limits.conf /etc/security/limits.conf.$$
297   cp-update kernel-2.6-cn /etc/security/limits.conf.$$ <<EOF
298 *               soft    core            0
299 *               hard    nofile          4096
300 *               soft    nofile          4096
301 EOF
302   cp_mv /etc/security/limits.conf.$$ /etc/security/limits.conf
303   echo -n " limits"
304 fi
305
306 # check pam.d/login
307 if [ -e /etc/pam.d/login ]; then
308   cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
309     's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
310     /etc/pam.d/login || true
311   echo -n " login"
312 fi
313
314 # check pam.d/ssh
315 if [ -e /etc/pam.d/ssh ]; then
316   cp_check_and_sed '^#.*session.+required.+pam_limits.so' \
317     's/^#.*session.+required.+pam_limits.so/session required pam_limits.so/' \
318     /etc/pam.d/ssh || true
319   echo -n " ssh"
320 fi
321
322 # finished with PAM
323 echo "."
324
325 ################################################################################
326
327 # fix old kernel-2.4-cn postrm
328 if [ -e /var/lib/dpkg/info/kernel-2.4-cn.postrm ]; then
329     echo "CN: Fixed old kernel-2.4-cn postrm."
330     cat > /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ <<EOF
331 #!/bin/sh
332
333 set -e
334
335 # be sure, be safe
336 if [ "$1" != "remove" ]; then
337   exit 0
338 fi
339
340 # import CN-functions
341 . /usr/share/carnet-tools/functions.sh
342
343 # remove us from limits.conf
344 cp-update -r kernel-2.4-cn /etc/security/limits.conf
345
346 # remove us from modules
347 cp-update -r kernel-2.4-cn /etc/modules
348 EOF
349     if ! cmp -s /var/lib/dpkg/info/kernel-2.4-cn.postrm \
350         /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$; then
351         mv /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$ \
352             /var/lib/dpkg/info/kernel-2.4-cn.postrm
353         chmod +x /var/lib/dpkg/info/kernel-2.4-cn.postrm
354     fi
355     rm -f /var/lib/dpkg/info/kernel-2.4-cn.postrm.$$
356 fi
357
358 ################################################################################
359
360 # fix old kernel-cn postrm
361 if [ -e /var/lib/dpkg/info/kernel-cn.postrm ]; then
362     echo "CN: Fixed old kernel-cn postrm."
363     cat > /var/lib/dpkg/info/kernel-cn.postrm.$$ <<EOF
364 #!/bin/sh
365
366 set -e
367
368 # be sure, be safe
369 if [ "$1" != "remove" ]; then
370   exit 0
371 fi
372
373 # import CN-functions
374 . /usr/share/carnet-tools/functions.sh
375
376 # remove us from limits.conf
377 cp-update -r kernel-cn /etc/security/limits.conf
378
379 # remove us from modules
380 cp-update -r kernel-cn /etc/modules
381 EOF
382     if ! cmp -s /var/lib/dpkg/info/kernel-cn.postrm \
383         /var/lib/dpkg/info/kernel-cn.postrm.$$; then
384         mv /var/lib/dpkg/info/kernel-cn.postrm.$$ \
385             /var/lib/dpkg/info/kernel-cn.postrm
386         chmod +x /var/lib/dpkg/info/kernel-cn.postrm
387     fi
388     rm -f /var/lib/dpkg/info/kernel-cn.postrm.$$
389 fi
390
391 ################################################################################
392
393 # dh_installdeb will replace this with shell code automatically
394 # generated by other debhelper scripts.
395
396 #DEBHELPER#
397
398 exit 0