Automatsko podesavanje minimalne duzine zaporke (8 znakova)
[squirrelmail-change-pass-cn.git] / debian / postinst
index 3811f55..f515ad7 100644 (file)
@@ -6,50 +6,176 @@ set -e
 [ $DEBIAN_SCRIPT_DEBUG ] && set -vx
 
 
-PKG="squirrelmail-change-pass-cn"
-SQCONF=/etc/squirrelmail/config.php
+# Load debconf
+. /usr/share/debconf/confmodule
 
 # Include CARNet functions
 . /usr/share/carnet-tools/functions.sh
 
+PKG="squirrelmail-change-pass-cn"
+VERSION="3.0-2"
+SQCONFDIR="/etc/squirrelmail"
+SQCONF="$SQCONFDIR/config.php"
+SQCPCONF="$SQCONFDIR/config_change_pass.php"
+CPCONF="/usr/share/squirrelmail/plugins/change_pass/config.php"
+need_restart=0
+min_pass_changed=0
+temp_files=
+
+set_min_pass_length () {
+
+    local sed_re conf_file
+    conf_re="$1"
+    conf_file="$2"
+    temp_files="$temp_files $conf_file.dpkg-tmp"
+
+    sed "$conf_re" "$conf_file" > "$conf_file.dpkg-tmp"
+
+    if ! cmp -s "$conf_file" "$conf_file.dpkg-tmp" 2>&1 >/dev/null; then
+        cp_mv "$conf_file.dpkg-tmp" "$conf_file"
+        min_pass_changed=1
+    else
+        rm -f "$conf_file.dpkg-tmp"
+    fi
+}
+
+cleanup () {
+
+    if [ -n "$temp_files" ]; then
+        for temp_item in $temp_files; do
+            if [ -e "$temp_item" ]; then
+                rm -f $temp_item
+            fi
+        done
+    fi
+}
+
+trap cleanup 0 1 2 15
+
 
 # Configure poppassd to refuse connection from all hosts
 # but localhost (127.0.0.1).
 #
-if [ -e /etc/hosts.deny -a -e /etc/hosts.allow ]; then
+if [ ! -f /etc/hosts.deny ]; then
+    touch /etc/hosts.deny
+fi
+if ! egrep -q '^poppassd:' /etc/hosts.deny; then
+    echo "poppassd: ALL" | cp-update "$PKG" /etc/hosts.deny
+fi
+if [ ! -f /etc/hosts.allow ]; then
+    touch /etc/hosts.allow
+fi
+if ! egrep -q '^poppassd:' /etc/hosts.allow; then
+    echo "poppassd: 127.0.0.1" | cp-update "$PKG" /etc/hosts.allow
+fi
 
-  if ! egrep -q '^poppassd:' /etc/hosts.deny; then
-       echo "poppassd: ALL" | cp-update "$PKG" /etc/hosts.deny
-  fi
 
-  if ! egrep -q '^poppassd:' /etc/hosts.allow; then
-       echo "poppassd: 127.0.0.1" | cp-update "$PKG" /etc/hosts.allow
-  fi
+# Configure SquirrelMail change_pass plugin - using defaults,
+# no configuration files if earlier version is older than 3.0-1.
+#
+if [ "$2" ] && dpkg --compare-versions $2 lt 3.0-1; then
+
+    cp_echo "CN: This version of change_pass plugin contains major changes."
+    cp_echo "CN: For detailed description, see documentation in /usr/share/doc/$PKG/."
+
+    if [ -f "$SQCPCONF" ]; then
+        mv -f "$SQCPCONF" "$SQCPCONF.cn-old"
+        cp_echo "CN: Old $SQCPCONF disabled and renamed to $SQCPCONF.cn-old."
+    fi
+    if [ -f "$CPCONF" ]; then
+        mv -f "$CPCONF" "$CPCONF.cn-old"
+        cp_echo "CN: Old $CPCONF disabled and renamed to $CPCONF.cn-old."
+    fi
 fi
 
 
-# Enable change_pass plugin in SquirrelMail config.php.
+# Check for change_pass configuration file and notify user about
+# minimum password length configuration.
 #
-if ! egrep '^\$plugins\[[0-9]+\][\ ]*=[\ ]*'\''change_pass'\' "$SQCONF"; then
+if [ ! -f "$SQCPCONF" ] && [ ! -f "$CPCONF" ]; then
+
+    # Configuration file does not exist.
+    cp_echo "CN: Generating new file $CPCONF"
+    cat > "$CPCONF" <<EOF
+<?php
+//// Begin - Generated by CARNet package $PKG
+//
+//  REMOVE this whole block if you DON'T WANT $PKG
+//  to edit your configuration file.
+//
+//// End - Generated by CARNet package $PKG
+    \$min_pass_length = 8;
+
+    // The default server is the configured IMAP server
+    #\$poppass_server = 'localhost';
+
+    // The default port is 106
+    #\$poppass_port = 106;
+
+    \$max_pass_length = 9999999;
+EOF
+    min_pass_changed=1
+else
+
+    # Checking for configuration file/s and minimum password length.
+    echo "CN: Checking for minimum password length configuration."
+    cn_tag_re="^\/\/\/\/ Begin - Generated by CARNet package $PKG$"
+
+    for cpconf_file in $SQCPCONF $CPCONF; do
+
+        min_pass_changed=0
+        if [ -f "$cpconf_file" ]; then
+
+            seddef="/^[[:space:]]*\\\$min_pass_length[[:space:]]*=[^;]*;[[:space:]]*$/d;
+                    s/\\\$min_pass_length[[:space:]]*=[^;]*;//g;"
+
+            if egrep -q "$cn_tag_re" "$cpconf_file"; then
+
+                # Configuration file is tagged by CARNet.
+                seddef="$seddef /^\/\/\/\/ End - Generated by CARNet package $PKG$/s/\(.*\)/\1\n"
+                seddef="$seddef    \\\$min_pass_length = 8;/"
+
+                set_min_pass_length "$seddef" "$cpconf_file"
+            else
+
+                if [ "$2" ] && dpkg --compare-versions $2 lt 3.0-2; then
+
+                    # Add CARNet package info lines to config's header and set up $min_pass_length.
+                    seddef="$seddef 0,/?>/s/^[[:space:]]*\(<?php\|<?\)\(.*\)/\1\n"
+                    seddef="$seddef\/\/\/\/ Begin - Generated by CARNet $PKG\n\/\/\n"
+                    seddef="$seddef\/\/  REMOVE this whole block if you DON'T WANT $PKG\n"
+                    seddef="$seddef\/\/  to edit your configuration file.\n\/\/\n"
+                    seddef="$seddef\/\/\/\/ End - Generated by CARNet package $PKG\n"
+                    seddef="$seddef    \\\$min_pass_length = 8;\n\2/"
+
+                    set_min_pass_length "$seddef" "$cpconf_file"
+                fi
+            fi
+        fi
+    done
+fi
 
-  sq_last_plugin=`egrep '^\\$plugins\[[0-9]+\][\ ]*=[\ ]*'\' "$SQCONF" | sed 's/^.*\[//;s/\].*//' | sort -n | tail -n1` || true
-  
-  if [ -n "$sq_last_plugin" ]; then
+if [ $min_pass_changed -eq 1 ]; then
+    db_fset squirrelmail-change-pass-cn/min_pass_length seen false
+    db_title squirrelmail-change-pass-cn - konfiguracija
+    db_input critical squirrelmail-change-pass-cn/min_pass_length || true
+    db_go || true
+    cp_echo "CN: Minimum password length has been set to 8 characters."
+fi
 
-       # Backup SquirrelMail configuration file.
-       cp_check_and_backup "$SQCONF"
+db_stop || true
 
-       change_pass_num=$sq_last_plugin
-       let change_pass_num++
-       
-       SQCONFTMP=`mktemp $SQCONF.tmp.XXXXXX`
 
-       echo -n "CN: Enabling SquirrelMail change_pass plugin in $SQCONF"
-       sed '/^$plugins\[.*'${sq_last_plugin}'\]/a $plugins['${change_pass_num}'] = '\''change_pass'\'';' "$SQCONF" > "$SQCONFTMP"
-       cp_mv "$SQCONFTMP" "$SQCONF"
-       echo "."
-  fi
+# Enable change_pass plugin in SquirrelMail config.php.
+#
+if ! egrep -q '^\$plugins\[[0-9]+\][\ ]*=[\ ]*'\''change_pass'\' "$SQCONF"; then
+
+    if cp_check_and_backup "$SQCONF"; then
+        cp_echo "CN: Old $SQCONF saved as \"/var/backups/${SQCONF##*/}.bak\"."
+    fi
 
+    cp_echo "CN: Enabling SquirrelMail change_pass plugin in $SQCONF"
+    /usr/sbin/squirrelmail-configure --install-plugin change_pass
 fi
 
 
@@ -59,31 +185,36 @@ fi
 if [ -x /etc/init.d/xinetd -a -d /etc/xinetd.d -a -x /usr/sbin/itox ]; then
 
   if egrep -q '^poppassd' /etc/inetd.conf && \
-     [ ! -e /etc/xinetd.d/poppassd ]; then
+     [ ! -f /etc/xinetd.d/poppassd ]; then
+
+       cp_echo "CN: Enabling poppassd in /etc/xinetd.d/poppassd"
 
-       echo -n "CN: Enabling poppassd in /etc/xinetd.d/poppassd"
+       touch /etc/xinetd.d/poppassd.$$
        grep "^poppassd" /etc/inetd.conf  | \
-               itox -daemon_dir /usr/sbin | cp-update "$PKG" /etc/xinetd.d/poppassd
-       changed_config=1
-       echo "."
+               itox -daemon_dir /usr/sbin | cp-update "$PKG" /etc/xinetd.d/poppassd.$$
+       cp_mv /etc/xinetd.d/poppassd.$$ /etc/xinetd.d/poppassd
+
+       need_restart=1
   fi
 fi
 
 
 # Restart xinetd if needed.
 #
-[ "$changed_config" ] && do=reload
-pgrep -u root -f /usr/sbin/xinetd > /dev/null || do=start
-
-if [ "$do" ]; then
+if [ $need_restart -eq 1 ]; then
 
        if [ -x "/etc/init.d/xinetd" ]; then
                if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
-                       invoke-rc.d xinetd "$do" || exit $?
+                       invoke-rc.d xinetd restart || exit $?
                else
-                       /etc/init.d/xinetd "$do" || exit $?
+                       /etc/init.d/xinetd restart || exit $?
                fi
        fi
 fi
 
+
+# Mail root
+#
+cp_mail "$PKG"
+
 exit 0