Cleanup php4 remove function.
[carnet-upgrade.git] / src / functions.sh
index ff90033..ef321d6 100644 (file)
@@ -451,8 +451,11 @@ check_my_version () {
 check_php_version () {
 
   if pkg php4-cn && ! pkg php5-cn; then
+    # prepare for php upgrade by installing php5 extensions
+    install_php5_extensions
+
     notice "
-Prije prelaska na Debian Etch potrebno je napraviti nadogradnju sa PHP 4 na PHP 5.
+Prije prelaska na Debian Lenny potrebno je napraviti nadogradnju sa PHP 4 na PHP 5.
 
 Pokrenite:
   # apt-get update
@@ -464,6 +467,33 @@ Nakon sto provjerite da sve web aplikacije rade ispravno, ponovno pozovite ovu s
   fi
 }
 
+# install php5 extensions based on installed php4 extensions
+install_php5_extensions () {
+  local php4_list php5_pkg
+
+  # get a list of installed php4 packages
+  php4_list=$(mktemp /var/lib/carnet-upgrade/php4_list.XXXXXX)
+  dpkg -l php4-\* | awk '/^ii/ {
+    pkg=$2; sub("^php4", "php5", pkg); print pkg }' > $php4_list
+
+  # compare it with the list of available php5 packages
+  php5_pkg=$(apt-cache search -n ^php5- | cut -d' ' -f 1 \
+    | grep -F -x -f $php4_list | grep -F -x -v php5-cn)
+
+  # handle special cases
+  if pkg php4-xslt; then
+     php5_pkg="$php5_pkg php5-xsl"
+  fi
+
+  # install the required php5 modules
+  if [ "$php5_pkg" ]; then
+     pkgadd $php5_pkg
+  fi
+
+  # cleanup
+  rm -f $php4_list
+}
+
 # if grsec is installed make sure it is fresh
 check_grsec_kernel () {
   local ver=`uname -r`
@@ -659,7 +689,7 @@ backup_slapd_db () {
     [ -x /etc/init.d/freeradius ] && /etc/init.d/freeradius start || true
   else
     log "slapd backup already at $ldap_backup, skipping."
-    notice "Backup slapd baze u $ldap_backup vec postoji!"
+    #notice "Backup slapd baze u $ldap_backup vec postoji!"
   fi
 }
 
@@ -683,7 +713,7 @@ backup_etc_dir () {
     umask $old_umask
   else
     log "/etc backup already present in $etc_backup, skipping."
-    notice "Backup /etc direktorija u $etc_backup vec postoji!"
+    #notice "Backup /etc direktorija u $etc_backup vec postoji!"
   fi
 }
 
@@ -703,111 +733,157 @@ fix_etc_default_raid2 () {
   fi
 }
 
-# restore original config file (if the new package version is not
-# installed already)
-restore_config () {
-  local file file_backup file_expect file_restore config_changed
-  local cn_package cn_version package version
+# restore a configuration file if it contains only CN modifications
+restore_file () {
+  local file file_expect file_restore file_backup
 
-  # check if new package version is installed
-  cn_package=$1
-  package=$2
-  version=$3
+  file=$1
+  file_expect=$2
+  file_restore=$3
+  file_backup=$file.$backup_ext
 
-  if [ "$cn_package" ]; then
-     if ! pkg $cn_package; then
-        # do nothing if cn package is not installed
-        return 0
-     fi
-  fi
+  [ "$file_expect" ] || file_expect=$cnup/files/$file.expect
+  [ "$file_restore" ] || file_restore=$cnup/files/$file.restore
 
-  if [ "$package" ]; then
-     if [ "$version" ]; then
-        # do nothing if package is already upgraded to new version
-        pkg $package lt $version || return 0
-     else
-        # do nothing if package is not installed
-        pkg $package || return 0
-     fi
+  # check if all required files are there
+  if ! [ -e $file -a -e $file_expect -a -e $file_restore ]; then
+    log "Required files missing, skipping $file restore."
+    return 1
+  fi 
+
+  # is restore needed
+  if ! cmp $file_expect $file >/dev/null; then
+    log "Config doesn't match the template, skipping $file restore."
+    return 1
   fi
-  shift 3
 
-  # restore package files
-  for file in $*; do
-    file_expect=/usr/share/carnet-upgrade/files/$file.expect
-    file_restore=/usr/share/carnet-upgrade/files/$file.restore
-    file_backup=$file.$backup_ext
+  # backup and restore
+  if [ ! -e $file_backup ]; then
+    log "Restoring config file $file."
 
-    # is config files are there
-    if ! [ -e $file -a -e $file_expect -a -e $file_restore ]; then
-        continue
-    fi 
+    # backup local changes
+    cp -av $file $file_backup
 
-    # is restore needed
-    if ! cmp $file_expect $file >/dev/null; then
-        continue
-    fi
+    # restore original
+    cp -v $file_restore $file
 
-    # backup and restore
-    if [ ! -e $file_backup ]; then
-      log "Restoring config file $file"
+    # restore succedded
+    return 0
+  else 
+    log "Backup file exists, skipping $file restore."
+    return 1
+  fi
+}
 
-      # backup local changes
-      cp -av $file $file_backup
+# force reconfiguration at the end if package is not upgraded automatically
+postupgrade_reconfigure () {
+  local cn_package cn_version
+
+  cn_package=$1
+  cn_version=$( dpkg -s "$cn_package" | awk '/^Version:/ {print $2}' )
+  post_upgrade "pkg $cn_package gt $cn_version || dpkg-reconfigure $cn_package"
+}
 
-      # restore original
-      cp -v $file_restore $file
+# copy template into a temporary file
+copy_template () {
+  local path file template config_new
 
-      config_changed=1
-    fi
-  done
+  path=$1
+  file=$(basename $path)
+  template=$cnup/files/$path.template
+  config_new=$(mktemp /var/lib/carnet-upgrade/$file.XXXXXX)
+  cp $template $config_new
 
-  # force reconfiguration at the end if package is not upgraded automatically
-  if [ "$config_changed" -a "$cn_package" ]; then
-     cn_version=$( dpkg -s "$cn_package" | awk '/^Version:/ {print $2}' )
-     post_upgrade "pkg $cn_package gt $cn_version || dpkg-reconfigure $cn_package"
-  fi
+  echo $config_new
 }
 
 # restore modified config to their package defaults
 # so the upgrade doesn't complain so much
 restore_configs () {
   local hostname domain template config_new
-  local config_backup
 
   # restore simple configs
-  restore_config kernel-2.6-cn procps 1:3.2.7-11 /etc/sysctl.conf
-  restore_config ntp-cn ntp 1:4.2.4p4+dfsg-8 /etc/ntp.conf
-  restore_config apache2-cn apache2.2-common 2.2.9-10+lenny2 /etc/apache2/ports.conf
-  restore_config postfix-cn sasl2-bin 2.1.22.dfsg1-23 /etc/default/saslauthd
-  restore_config bind9-cn bind9 1:9.5.1.dfsg.P1-1 /etc/bind/named.conf.options
-  restore_config amavisd-cn amavisd-new 1:2.6.1.dfsg-1 /etc/cron.daily/amavisd-new
-  rm -f /etc/cron.daily/amavisd-new.$backup_ext
-  restore_config spamassassin-cn razor 1:2.85-1 /etc/razor/razor-agent.conf
-  restore_config kernel-2.6-cn libpam-modules 1.0.1-5 /etc/security/limits.conf
-  restore_config samba-cn samba-common 2:3.2.5-4lenny2 /etc/samba/smb.conf
-  restore_config '' base-files 5lenny2 /etc/issue /etc/issue.net
+  if pkg kernel-2.6-cn && pkg procps lt 1:3.2.7-11; then
+     if restore_file /etc/sysctl.conf; then
+        postupgrade_reconfigure kernel-2.6-cn
+     fi
+  fi
+
+  if pkg ntp-cn && pkg ntp lt 1:4.2.4p4+dfsg-8; then
+     if restore_file /etc/ntp.conf; then
+        postupgrade_reconfigure ntp-cn
+     fi
+  fi
+
+  if pkg apache2-cn && pkg apache2.2-common lt 2.2.9-10+lenny2; then
+     if restore_file /etc/apache2/ports.conf; then
+        postupgrade_reconfigure apache2-cn
+     fi
+  fi
+
+  if pkg postfix-cn && pkg sasl2-bin lt 2.1.22.dfsg1-23; then
+     if restore_file /etc/default/saslauthd; then
+        postupgrade_reconfigure postfix-cn
+     fi
+  fi
+
+  if pkg bind9-cn && pkg bind9 lt 1:9.5.1.dfsg.P1-1; then
+     if restore_file /etc/bind/named.conf.options; then
+        postupgrade_reconfigure bind9-cn
+     fi
+  fi
+
+  if pkg amavisd-cn && pkg amavisd-new lt 1:2.6.1.dfsg-1; then
+     if restore_file /etc/cron.daily/amavisd-new; then
+        rm -f /etc/cron.daily/amavisd-new.$backup_ext
+        postupgrade_reconfigure amavisd-cn
+     fi
+  fi
+
+  if pkg spamassassin-cn && pkg razor lt 1:2.85-1; then
+     if restore_file /etc/razor/razor-agent.conf; then
+        postupgrade_reconfigure spamassassin-cn
+     fi
+  fi
+
+  if pkg kernel-2.6-cn && pkg libpam-modules lt 1.0.1-5; then
+     if restore_file /etc/security/limits.conf; then
+        postupgrade_reconfigure kernel-2.6-cn
+     fi
+  fi
+
+  if pkg samba-cn && pkg samba-common lt 2:3.2.5-4lenny2; then
+     if restore_file /etc/samba/smb.conf; then
+        postupgrade_reconfigure samba-cn
+     fi
+  fi
+
+  if pkg base-files lt 5lenny2; then
+     restore_file /etc/issue
+     restore_file /etc/issue.net
+  fi
+
+  if pkg php5-cn && pkg php5-cli lt 5.2.6.dfsg.1-1+lenny2; then
+     if restore_file /etc/php5/cli/php.ini; then
+        postupgrade_reconfigure php5-cn
+     fi
+  fi
+
+  if pkg php5-cn && pkg libapache2-mod-php5 lt 5.2.6.dfsg.1-1+lenny2; then
+     if restore_file /etc/php5/apache2/php.ini; then
+        postupgrade_reconfigure php5-cn
+     fi
+  fi
 
   # check if monitrc is template based
-  if [ -f /etc/monit/monitrc -a ! -f /etc/monit/monitrc.$backup_ext ]; then
-     template=/usr/share/carnet-upgrade/files/etc/monit/monitrc.template
-     config_new=$(mktemp /var/lib/carnet-upgrade/monitrc.XXXXXX)
+  if [ -f /etc/monit/monitrc ]; then
+     # regenerate config from template
      hostname=$(hostname)
      domain=$(hostname --domain)
-
-     # generate config from template
-     cp $template $config_new
+     config_new=$(copy_template /etc/monit/monitrc)
      check_and_sed "@localhost" \
        "s/@localhost/@$hostname.$domain/g" $config_new || true
-
-     # compare with running config
-     if cmp -s /etc/monit/monitrc $config_new; then
-        # restore if running config is generated template
-        log "Restoring config file /etc/monit/monitrc"
-        cp -av /etc/monit/monitrc /etc/monit/monitrc.$backup_ext
-        cp -v /usr/share/carnet-upgrade/files/etc/monit/monitrc.restore \
-              /etc/monit/monitrc
-     fi
+     restore_file /etc/monit/monitrc $config_new
      rm -f $config_new
   fi
 
@@ -898,6 +974,14 @@ remove_skey () {
   fi
 }
 
+# remove unsupported php version
+# but leave the configuration just in case
+remove_php4 () {
+  if pkg php5-cn && pkg php4-common; then
+     pkgrm_only php4-common
+  fi
+}
+
 # check if package is orphaned (nothing depends on it)
 is_orphaned () {
   local package deps
@@ -943,18 +1027,17 @@ upgrade_libc () {
   DEBIAN_FRONTEND=noninteractive pkgadd libc6 tzdata
 }
 
-# upgrade apache2 to etch
+# upgrade apache2 to lenny
 upgrade_apache2 () {
   local package packages
 
   for package in \
-      libapache2-mod-php4 \
-      php4-cli \
-      php4-cgi \
-      php4-cn \
+      libapache2-mod-php5 \
+      php5-cli \
+      php5-cn \
       apache2-cn \
-      php4-odbc \
-      php4-suhosin \
+      php5-odbc \
+      php5-suhosin \
       php-suhosin-cn
   do
       if pkg $package; then
@@ -962,11 +1045,6 @@ upgrade_apache2 () {
       fi
   done
 
-  # enable suhosin downgrade to etch version
-  if pkg php4-suhosin; then
-      pkgrm php4-suhosin php-suhosin-cn
-  fi
-
   eval pkgadd $packages
 }
 
@@ -1007,42 +1085,37 @@ upgrade_amavis () {
 
 # handle freerdius config files upgrade
 upgrade_freeradius () {
-  local template config_new password realm
+  local template config_new password realm hostname basedn
 
   pkg freeradius-aai lt 2.1.3-0lenny0 || return 0
 
   # handle static configs
-  restore_config freeradius-aai freeradius 2.1.3-0lenny0 \
-    /etc/freeradius/clients.conf \
-    /etc/freeradius/hints \
-    /etc/freeradius/ldap.attrmap \
-    /etc/freeradius/radiusd.conf
+  restore_file /etc/freeradius/clients.conf
+  restore_file /etc/freeradius/hints
+  restore_file /etc/freeradius/ldap.attrmap
 
   # handle template based configs
-  template=/usr/share/carnet-upgrade/files/etc/freeradius/eap.conf.template
-  config_new=$(mktemp /var/lib/carnet-upgrade/eap.conf.XXXXXX)
+  basedn=$(sed -n '/^[[:space:]]*suffix[[:space:]]*/ { 
+    s///; s/"//g; p; q }' /etc/ldap/slapd.conf)
+  hostname=$(hostname -f)
+  config_new=$(copy_template /etc/freeradius/radiusd.conf)
+  sed -i "s/#HOSTNAME#/$hostname/" $config_new
+  sed -i "s/#BASEDN#/$basedn/" $config_new
+  restore_file /etc/freeradius/radiusd.conf $config_new
+  rm -f $config_new
+
   password=$(grep -s '^[[:space:]]*private_key_password[[:space:]]*=' \
     /etc/freeradius/eap.conf)
-  cp $template $config_new
+  config_new=$(copy_template /etc/freeradius/eap.conf)
   sed -i "s/.*#PASSWORD#.*/$password/" $config_new
-  if cmp -s $config_new /etc/freeradius/eap.conf >/dev/null; then
-     log "Restoring config file /etc/freeradius/eap.conf"
-     cp -v /usr/share/carnet-upgrade/files/etc/freeradius/eap.conf.restore \
-           /etc/freeradius/eap.conf
-  fi
+  restore_file /etc/freeradius/eap.conf $config_new
   rm -f $config_new
   
-  template=/usr/share/carnet-upgrade/files/etc/freeradius/proxy.conf.template
-  config_new=$(mktemp /var/lib/carnet-upgrade/proxy.conf.XXXXXX)
   realm=$(sed -n '/^[[:space:]]*suffix[[:space:]]*/ { 
     s///; s/"//g; s/,dc=/./g; s/dc=//; s/.hr$//; p; q }' /etc/ldap/slapd.conf)
-  cp $template $config_new
+  config_new=$(copy_template /etc/freeradius/proxy.conf)
   sed -i "s/#REALM#/$realm/" $config_new
-  if cmp -s $config_new /etc/freeradius/proxy.conf >/dev/null; then
-     log "Restoring config file /etc/freeradius/proxy.conf"
-     cp -v /usr/share/carnet-upgrade/files/etc/freeradius/proxy.conf.restore \
-           /etc/freeradius/proxy.conf
-  fi
+  restore_file /etc/freeradius/proxy.conf $config_new
   rm -f $config_new
 
   # install the new packages
@@ -1110,7 +1183,7 @@ upgrade_postgresql () {
 
 # upgrade the MTA
 upgrade_postfix () {
-  pkgadd postfix-cn
+  pkgadd sasl2-bin postfix-cn
 
   if pkg postgrey; then
      pkgrm postgrey # not used by postfix-cn anymore
@@ -1141,7 +1214,7 @@ upgrade_dovecot () {
   default_mail_env=$(get_variable default_mail_env /etc/dovecot/dovecot.conf)
 
   # silently upgrade package
-  restore_config dovecot-cn dovecot-common 1.0 /etc/dovecot/dovecot.conf
+  restore_file /etc/dovecot/dovecot.conf
   pkgadd dovecot-cn
 
   # insert old mail location