Sophie

Sophie

distrib > Mandriva > 2007.0 > x86_64 > by-pkgid > 08b68bca035167118f0784235c7182ec > files > 23

cyrus-imapd-2.2.13-4mdv2007.0.src.rpm

#!/bin/sh

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# This script converts all db files of a cyrus installation from their
# existing format to the format required by the current installation.
# The format of current db files is determined using the 'file' command
# with a magic file added for skiplist db, the new format is read from
# a config file usually in /usr/share/cyrus-imapd/rpm/db.cfg, which is
# created while compiling. After converting, the db.cfg file is
# copied to a cache file usually at /var/lib/imap/rpm/db.cfg.cache to
# allow bypassing this converting script if both files are identical.
# While this is a bit less secure, it may be useful on big server where
# db converting is done automatically.
#
# This script can safely be run as root, it will reexec itself as user
# cyrus if needed.
#
# author: Simon Matter, Invoca Systems <simon.matter@invoca.ch>

# changelog
# v1.0.1, Oct 22 2002 Simon Matter <simon.matter@invoca.ch>
# - added two-step conversion method
#
# v1.0.2, Jan 10 2003 Simon Matter <simon.matter@invoca.ch>
# - fixed a bug where cvt_cyrusdb was called to convert empty or
#   nonexistent files
#
# v1.0.3, Mar 14 2003 Simon Matter <simon.matter@invoca.ch>
# - fixed a problem with new versions of the file command
#
# v1.0.4
# - added GPL license
#
# v1.0.5, May 02 2003 Simon Matter <simon.matter@invoca.ch>
# - modified exec path
#
# Extra modifications for Mandrake RPM by 
# Luca Olivetti  <luca@olivetti.cjb.net>:
#
#  - get spool directory from config file
#  - get system_magic from file --version
#  - Dec 01 2003 added annotations.db for cyrus-2.2.2
#  - Dec 01 2003 changed "db3" to "berkeley"
#  - Jan 15 2003 get db configuration from /etc/imapd.conf

if [ -n "`/sbin/pidof cyrus-master`" ]; then
  echo "ERROR: cyrus-master is running, unable to convert mailboxes!"
  exit 1
fi

if [ ! -f /etc/imapd.conf ]; then
  echo "ERROR: configuration file not found."
  exit 1
fi

# force cyrus user for security reasons
if [ ! $(whoami) = "cyrus" ]; then
  exec su - cyrus -c "cd $PWD ; $0"
fi

# take system magic location from file --version
system_magic=`file --version | awk '/magic file/ {print $4}'`
cyrus_magic=/usr/share/cyrus-imapd/rpm/magic
cvt_cyrusdb=/usr/lib/cyrus-imapd/cvt_cyrusdb

# get_config [config default]
# extracts config option from config file
get_config() {
  if config=`grep "^$1" /etc/imapd.conf` ; then
    echo $config | cut -d: -f2 | sed -e 's/^ *//' -e 's/-nosync//' -e 's/ *$//'
  else
    echo $2
  fi
}

# get imap directory from config file
imap_prefix=`get_config configdirectory /var/lib/imap`

# files get mode 0600
umask 166

# get database backends from config file
CONFIG_DB_DUPLICATE=`get_config duplicate_db berkeley`
CONFIG_DB_MBOX=`get_config mboxlist_db skiplist`
CONFIG_DB_SEEN=`get_config seenstate_db skiplist`
CONFIG_DB_SUBS=`get_config subscription_db flat`
CONFIG_DB_TLS=`get_config tlscache_db berkeley`
CONFIG_DB_ANNOTATION=`get_config annotation_db skiplist`

# file_type [file]
file_type() {
  this_type=$(file -b -m "$system_magic:$cyrus_magic" "$1")
  if echo "$this_type" | grep -qi skip > /dev/null 2>&1; then
    echo skiplist
  elif echo "$this_type" | grep -qi text > /dev/null 2>&1; then
    echo flat
  else
    echo berkeley
  fi
}

# cvt_file [file] [db]
cvt_file() {
  target="$1"
  new_db="$2"
  if [ -s "$target" ]; then
    old_db=$(file_type "$target")
    if [ ! "$old_db" = "$new_db" ]; then
      # The two-step conversion is paranoia against the filenames being encoded
      # inside the database or logfiles (berkeley does this, for example).
      rm -f "${target}.flat"
      if [ "$old_db" = "flat" ]; then
        cp -a "$target" "${target}.flat"
      else
        $cvt_cyrusdb "$target" "$old_db" "${target}.flat" flat
      fi
      RETVAL=$?
      ERRVAL=$[ $ERRVAL + $RETVAL ]
      if [ $RETVAL -eq 0 ]; then
        rm -f "$target"
        if [ -s "${target}.flat" ]; then
          if [ "$new_db" = "flat" ]; then
            cp -a "${target}.flat" "$target"
          else
            $cvt_cyrusdb "${target}.flat" flat "$target" "$new_db"
          fi
        fi
        RETVAL=$?
        ERRVAL=$[ $ERRVAL + $RETVAL ]
        if [ $RETVAL -eq 0 ]; then
          rm -f "${target}.flat"
        else
          echo "ERROR: unable to convert ${target}.flat from flat to $new_db"
        fi
      else
        echo "ERROR: unable to convert $target from $old_db to flat"
      fi
    fi
  fi
}

ERRVAL=0

# convert all db files
cvt_file "$imap_prefix/mailboxes.db"    "$CONFIG_DB_MBOX"
cvt_file "$imap_prefix/deliver.db"      "$CONFIG_DB_DUPLICATE"
cvt_file "$imap_prefix/tls_sessions.db" "$CONFIG_DB_TLS"

find "$imap_prefix/user/" -name '*.seen' -type f | while read db_file; do
  cvt_file "$db_file" "$CONFIG_DB_SEEN"
done

find "$imap_prefix/user/" -name '*.sub' -type f | while read db_file; do
  cvt_file "$db_file" "$CONFIG_DB_SUBS"
done

find "$imap_prefix/" -name 'annotations.db' -type f | while read db_file; do
  cvt_file "$db_file" "$CONFIG_DB_ANNOTATION"
done

exit $ERRVAL