064cf1746ea4aacd8e3bbc668c2e8c7d3486136f
[squirrelmail-change-pass-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$1" = "configure" ] || exit 0
6 [ $DEBIAN_SCRIPT_DEBUG ] && set -vx
7
8
9 # Load debconf
10 . /usr/share/debconf/confmodule
11
12 # Include CARNet functions
13 . /usr/share/carnet-tools/functions.sh
14
15 PKG="squirrelmail-change-pass-cn"
16 VERSION="3.0-2"
17 SQCONFDIR="/etc/squirrelmail"
18 SQCONF="$SQCONFDIR/config.php"
19 SQCPCONF="$SQCONFDIR/config_change_pass.php"
20 CPCONF="/usr/share/squirrelmail/plugins/change_pass/config.php"
21 need_restart=0
22 min_pass_changed=0
23 temp_files=
24
25 set_min_pass_length () {
26
27     local sed_re conf_file
28     conf_re="$1"
29     conf_file="$2"
30     temp_files="$temp_files $conf_file.dpkg-tmp"
31
32     sed "$conf_re" "$conf_file" > "$conf_file.dpkg-tmp"
33
34     if ! cmp -s "$conf_file" "$conf_file.dpkg-tmp" 2>&1 >/dev/null; then
35         cp_mv "$conf_file.dpkg-tmp" "$conf_file"
36         min_pass_changed=1
37     else
38         rm -f "$conf_file.dpkg-tmp"
39     fi
40 }
41
42 cleanup () {
43
44     if [ -n "$temp_files" ]; then
45         for temp_item in $temp_files; do
46             if [ -e "$temp_item" ]; then
47                 rm -f $temp_item
48             fi
49         done
50     fi
51 }
52
53 trap cleanup 0 1 2 15
54
55
56 # Configure poppassd to refuse connection from all hosts
57 # but localhost (127.0.0.1).
58 #
59 if [ ! -f /etc/hosts.deny ]; then
60     touch /etc/hosts.deny
61 fi
62 if ! egrep -q '^poppassd:' /etc/hosts.deny; then
63     echo "poppassd: ALL" | cp-update "$PKG" /etc/hosts.deny
64 fi
65 if [ ! -f /etc/hosts.allow ]; then
66     touch /etc/hosts.allow
67 fi
68 if ! egrep -q '^poppassd:' /etc/hosts.allow; then
69     echo "poppassd: 127.0.0.1" | cp-update "$PKG" /etc/hosts.allow
70 fi
71
72
73 # First of all, do the backup.
74 #
75 done_backup=0
76 for cpconf_file in $SQCPCONF $CPCONF; do
77     if [ -f "$cpconf_file" ] && [ ! -L "$cpconf_file" ]; then
78         cp_echo "CN: Doing backup for $cpconf_file"
79         cp_backup_conffile -d "/var/backups/$PKG" -p $cpconf_file
80         done_backup=1
81     fi
82 done
83 if [ $done_backup -eq 1 ]; then
84     cp_echo "CN: Backup is located in directory: /var/backups/$PKG/"
85 fi
86
87
88 # Remove change_pass configuration files if earlier version is
89 # older than 3.0-1.
90 #
91 if [ "$2" ] && dpkg --compare-versions $2 lt 3.0-1; then
92
93     cp_echo "CN: This version of change_pass plugin contains major changes."
94     for cpconf_file in $SQCPCONF $CPCONF; do
95         if [ -f "$cpconf_file" ] || [ -L "$cpconf_file" ]; then
96             rm -f $cpconf_file
97             cp_echo "CN: Removed old $cpconf_file"
98         fi
99     done
100 fi
101
102
103 # Check for change_pass configuration files and notify user about
104 # minimum password length configuration.
105 #
106 if egrep -q '^\$plugins\[[0-9]+\][\ ]*=[\ ]*'\''change_pass'\' "$SQCONF"; then
107     /usr/sbin/squirrelmail-configure --remove-plugin change_pass
108 fi
109
110 if [ ! -f "$SQCPCONF" ]; then
111
112     if [ -f "$CPCONF" ]; then
113         cp -f "$CPCONF" "$SQCPCONF"
114         cp_echo "CN: $CPCONF copied to $SQCPCONF"
115     else
116
117         # Configuration file does not exist.
118         cp_echo "CN: Generating new file $SQCPCONF"
119         cat > "$SQCPCONF" <<EOF
120 <?php
121 //// Begin - Generated by CARNet package $PKG
122 //
123 //  REMOVE this whole block if you DON'T WANT $PKG
124 //  to edit your configuration file.
125 //
126 //// End - Generated by CARNet package $PKG
127     \$min_pass_length = 8;
128
129     // The default server is the configured IMAP server
130     #\$poppass_server = 'localhost';
131
132     // The default port is 106
133     #\$poppass_port = 106;
134
135     \$max_pass_length = 9999999;
136 EOF
137         min_pass_changed=1
138     fi
139 fi
140
141 if [ $min_pass_changed -eq 0 ]; then
142
143     echo "CN: Checking for minimum password length configuration."
144
145     cn_tag_re="^\/\/\/\/ Begin - Generated by CARNet package $PKG$"
146     seddef="/^[[:space:]]*\\\$min_pass_length[[:space:]]*=[^;]*;[[:space:]]*$/d;
147             s/\\\$min_pass_length[[:space:]]*=[^;]*;//g;"
148
149     if egrep -q "$cn_tag_re" "$SQCPCONF"; then
150
151         # Configuration file is tagged by CARNet.
152         seddef="$seddef /^\/\/\/\/ End - Generated by CARNet package $PKG$/s/\(.*\)/\1\n"
153         seddef="$seddef    \\\$min_pass_length = 8;/"
154         set_min_pass_length "$seddef" "$SQCPCONF"
155     else
156
157         if dpkg --compare-versions "$2" lt 3.0-2; then
158
159             # Add CARNet package info lines to config's header and set up $min_pass_length.
160             seddef="$seddef 0,/?>/s/^[[:space:]]*\(<?php\|<?\)\(.*\)/\1\n"
161             seddef="$seddef\/\/\/\/ Begin - Generated by CARNet $PKG\n\/\/\n"
162             seddef="$seddef\/\/  REMOVE this whole block if you DON'T WANT $PKG\n"
163             seddef="$seddef\/\/  to edit your configuration file.\n\/\/\n"
164             seddef="$seddef\/\/\/\/ End - Generated by CARNet package $PKG\n"
165             seddef="$seddef    \\\$min_pass_length = 8;\n\2/"
166             set_min_pass_length "$seddef" "$SQCPCONF"
167         fi
168     fi
169 fi
170
171 # Just to be sure.
172 ln -sf "$SQCPCONF" "$CPCONF"
173
174 if [ $min_pass_changed -eq 1 ]; then
175     db_fset squirrelmail-change-pass-cn/min_pass_length seen false
176     db_title squirrelmail-change-pass-cn - konfiguracija
177     db_input critical squirrelmail-change-pass-cn/min_pass_length || true
178     db_go || true
179     cp_echo "CN: Minimum password length has been set to 8 characters."
180 fi
181
182 db_stop || true
183
184
185 # Enable change_pass plugin in SquirrelMail config.php.
186 #
187 if ! egrep -q '^\$plugins\[[0-9]+\][\ ]*=[\ ]*'\''change_pass'\' "$SQCONF"; then
188
189     if cp_check_and_backup "$SQCONF"; then
190         cp_echo "CN: Old $SQCONF saved as \"/var/backups/${SQCONF##*/}.bak\"."
191     fi
192
193     /usr/sbin/squirrelmail-configure --install-plugin change_pass
194 fi
195
196
197 # Convert poppassd service from inetd.conf to xinetd.conf type.
198 # Service configuration is saved in /etc/xinetd.d/ directory.
199 #
200 if [ -x /etc/init.d/xinetd -a -d /etc/xinetd.d -a -x /usr/sbin/itox ]; then
201
202     if egrep -q '^poppassd' /etc/inetd.conf && \
203         [ ! -f /etc/xinetd.d/poppassd ]; then
204
205         cp_echo "CN: Enabling poppassd in /etc/xinetd.d/poppassd"
206
207         touch /etc/xinetd.d/poppassd.$$
208         grep "^poppassd" /etc/inetd.conf  | \
209             itox -daemon_dir /usr/sbin | cp-update "$PKG" /etc/xinetd.d/poppassd.$$
210         cp_mv /etc/xinetd.d/poppassd.$$ /etc/xinetd.d/poppassd
211
212         need_restart=1
213     fi
214 fi
215
216
217 # Restart xinetd if needed.
218 #
219 if [ $need_restart -eq 1 ]; then
220
221     if [ -x "/etc/init.d/xinetd" ]; then
222         if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
223             invoke-rc.d xinetd restart || exit $?
224         else
225             /etc/init.d/xinetd restart || exit $?
226         fi
227     fi
228 fi
229
230
231 # Mail root
232 #
233 cp_mail "$PKG"
234
235 exit 0