9d65fa6
#!/bin/bash
38f2261
38f2261
# This program is free software; you can redistribute it and/or modify
38f2261
# it under the terms of the GNU General Public License as published by
38f2261
# the Free Software Foundation; either version 2 of the License, or
38f2261
# (at your option) any later version.
38f2261
#
38f2261
# This program is distributed in the hope that it will be useful,
38f2261
# but WITHOUT ANY WARRANTY; without even the implied warranty of
38f2261
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38f2261
# GNU General Public License for more details.
38f2261
#
38f2261
# You should have received a copy of the GNU General Public License
38f2261
# along with this program; if not, write to the Free Software
38f2261
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38f2261
38f2261
# This script converts all db files of a cyrus installation from their
38f2261
# existing format to the format required by the current installation.
38f2261
# The format of current db files is determined using the 'file' command
38f2261
# with a magic file added for skiplist db, the new format is read from
38f2261
# a config file usually in /usr/share/cyrus-imapd/rpm/db.cfg, which is
38f2261
# created while compiling. After converting, the db.cfg file is
38f2261
# copied to a cache file usually at /var/lib/imap/rpm/db.cfg.cache to
38f2261
# allow bypassing this converting script if both files are identical.
38f2261
# While this is a bit less secure, it may be useful on big server where
38f2261
# db converting is done automatically.
38f2261
#
38f2261
# This script can safely be run as root, it will reexec itself as user
38f2261
# cyrus if needed.
38f2261
#
38f2261
# author: Simon Matter, Invoca Systems <simon.matter@invoca.ch>
38f2261
38f2261
# changelog
38f2261
# v1.0.1, Oct 22 2002 Simon Matter <simon.matter@invoca.ch>
38f2261
# - added two-step conversion method
38f2261
#
38f2261
# v1.0.2, Jan 10 2003 Simon Matter <simon.matter@invoca.ch>
38f2261
# - fixed a bug where cvt_cyrusdb was called to convert empty or
38f2261
#   nonexistent files
38f2261
#
38f2261
# v1.0.3, Mar 14 2003 Simon Matter <simon.matter@invoca.ch>
38f2261
# - fixed a problem with new versions of the file command
38f2261
#
38f2261
# v1.0.4
38f2261
# - added GPL license
38f2261
#
38f2261
# v1.0.5, May 02 2003 Simon Matter <simon.matter@invoca.ch>
38f2261
# - modified exec path
38f2261
#
38f2261
# v1.0.6, Jul 18 2003 Simon Matter <simon.matter@invoca.ch>
38f2261
# - changed db3 to berkeley
38f2261
# - added new db backends for 2.2
38f2261
#
38f2261
# v1.0.7, Jan 23 2004 Simon Matter <simon.matter@invoca.ch>
38f2261
# - included some modifications from Luca Olivetti <luca@olivetti.cjb.net>
38f2261
# - added masssievec functionality
38f2261
#
38f2261
# v1.0.8, Jan 28 2004 Simon Matter <simon.matter@invoca.ch>
38f2261
# - convert sieve scripts to UTF-8 before calling masssievec
38f2261
#
38f2261
# v1.0.9, Jan 29 2004 Simon Matter <simon.matter@invoca.ch>
38f2261
# - convert sieve scripts to UTF-8 only if sievec failed before
38f2261
#
38f2261
# v1.0.10, Feb 24 2004 Simon Matter <simon.matter@invoca.ch>
38f2261
# - change su within init script to get input from
38f2261
#   /dev/null, this prevents hang when running in SELinux
38f2261
#
38f2261
# v1.0.11, Mar 02 2004 Simon Matter <simon.matter@invoca.ch>
38f2261
# - fixed SELinux fix
38f2261
#
38f2261
# v1.0.12, Dec 16 2004 Simon Matter <simon.matter@invoca.ch>
38f2261
# - use runuser instead of su if available
9e566a4
#
9e566a4
# v1.0.13, Jul 15 2005 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - don't use flat in the two step conversion, use skiplist instead
9e566a4
#
9e566a4
# v1.0.14, Jul 18 2005 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - replace the order of the magic files in the file call to make
9e566a4
#   sure skiplist is detected correctly.
9e566a4
#
9e566a4
# v1.0.15, Aug 17 2005 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - add functionality to export all berkeley db files to skiplist
9e566a4
#
9e566a4
# v1.1.0, Aug 18 2005 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - fix export functionality, try to recover Berkeley databases
9e566a4
#   as much as possible before any conversion.
9e566a4
#
9e566a4
# v1.1.1, Dec 05 2005 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - run db_checkpoint in background with a timeout to prevent
9e566a4
#   that cyrus-imapd doesn't start at all if it hangs.
9e566a4
#
9e566a4
# v1.1.2, Dec 06 2005 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - make handling of db_checkpoint more robust
9e566a4
#
9e566a4
# v1.2.0, Jan 12 2006 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - adopt for cyrus-imapd-2.3
9e566a4
#
9e566a4
# v1.2.1, Jan 13 2006 Simon Matter <simon.matter@invoca.ch>
9e566a4
# - code cleanup
9d65fa6
#
9d65fa6
# v1.2.2, Nov 29 2007 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - add ability to handle "@include" options in imapd.conf, patch
9d65fa6
#   provided by Tim Bannister
9d65fa6
#
9d65fa6
# v1.2.3, Feb 07 2008 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - add ability to handle tabs in imapd.conf, patch provided
9d65fa6
#   by Franz Knipp
9d65fa6
# - disable default values for some config options like sievedir
9d65fa6
#
9d65fa6
# v1.2.4, Apr 23 2008 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - add support for statuscache.db
9d65fa6
#
9d65fa6
# v1.3.0, Sep 29 2008 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - add multi-instance support
9d65fa6
#
9d65fa6
# v1.3.1, Oct 09 2008 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - improve variable handling
9d65fa6
#
9d65fa6
# v1.3.2, May 26 2009 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - add some sanity checks to multi-instance support
9d65fa6
#
9d65fa6
# v1.3.3, May 27 2009 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - make some cosmetic changes
9d65fa6
#
9d65fa6
# v1.3.4, Dec 22 2009 Simon Matter <simon.matter@invoca.ch>
9d65fa6
# - add support for user_deny.db
9e566a4
9d65fa6
VERSION=1.3.4
38f2261
9d65fa6
PIDFILE=/var/run/cyrus-master${INSTANCE}.pid
9d65fa6
9d65fa6
# instance config
9d65fa6
CYRUSCONF=/etc/cyrus${INSTANCE}.conf
9d65fa6
IMAPDCONF=/etc/imapd${INSTANCE}.conf
9d65fa6
9d65fa6
# make sure what we have is a valid instance
9d65fa6
# and that config files are present
9d65fa6
if [ -n "$INSTANCE" ]; then
9d65fa6
  [ -L /etc/rc.d/init.d/${BASENAME} ] || exit 0
9d65fa6
fi
9d65fa6
[ -f $CYRUSCONF ] || exit 0
9d65fa6
[ -f $IMAPDCONF ] || exit 0
9d65fa6
9d65fa6
if [ -f $PIDFILE ]; then
9d65fa6
  read CYRUS_PID < $PIDFILE
9d65fa6
  if [ -n "$CYRUS_PID" ]; then
9d65fa6
    if ps -p $CYRUS_PID > /dev/null 2>&1; then
9d65fa6
      echo "ERROR: cyrus-master is running, unable to convert mailboxes!"
9d65fa6
      exit 1
9d65fa6
    fi
9d65fa6
  fi
38f2261
fi
38f2261
9d65fa6
if [ ! -f $IMAPDCONF ]; then
9d65fa6
  echo "ERROR: configuration file '${IMAPDCONF}' not found, exiting!"
38f2261
  exit 1
38f2261
fi
38f2261
38f2261
# fallback to su if runuser not available
38f2261
if [ -x /sbin/runuser ]; then
38f2261
  RUNUSER=runuser
38f2261
else
38f2261
  RUNUSER=su
38f2261
fi
38f2261
38f2261
# force cyrus user for security reasons
38f2261
if [ ! $(whoami) = "cyrus" ]; then
9d65fa6
  exec $RUNUSER - cyrus -c "cd $PWD < /dev/null ; INSTANCE=$INSTANCE $0 $*"
38f2261
fi
38f2261
9e566a4
# special function for migration
9e566a4
EXPORT=$1
9e566a4
38f2261
# files get mode 0600
38f2261
umask 166
38f2261
9e566a4
# show version info in log files
9e566a4
echo "cvt_cyrusdb_all version: $VERSION"
9e566a4
9d65fa6
# expand_config <path>
9d65fa6
# handle "@include" sections from imapd style config file
9d65fa6
expand_config() {
9d65fa6
  while read line; do
9d65fa6
    if printf "%s\n" "${line}" | grep -q '^@include:'; then
9d65fa6
      expand_config "$( printf "%s\n" "${line}" | cut -d : -f 2- | sed -e 's/^[\t ]*//' )"
9d65fa6
    else
9d65fa6
      printf "%s\n" "${line}"
9d65fa6
    fi
9d65fa6
  done < $1
9d65fa6
}
9d65fa6
9d65fa6
# get_config <config> [<default>]
38f2261
# extracts config option from config file
38f2261
get_config() {
9d65fa6
  searchstr=$1
9d65fa6
  if config="$(expand_config $IMAPDCONF | egrep "^${searchstr}:")"; then
9d65fa6
    CFGVAL="$(printf "%s\n" "$config" | cut -d : -f 2- | sed -e 's/^[\t ]*//')"
38f2261
  else
9d65fa6
    if [ -z "$2" ]; then
9d65fa6
      echo "ERROR: config option '$1' not found in ${IMAPDCONF}, exiting!" 1>&2
9d65fa6
      return 1
9d65fa6
    fi
9d65fa6
    CFGVAL="$2"
38f2261
  fi
9d65fa6
  echo "get_config ${1}: $CFGVAL" 1>&2
9d65fa6
  echo "$CFGVAL"
38f2261
}
38f2261
38f2261
# where to find files and directories
9e566a4
data_dir=/usr/share/cyrus-imapd/rpm
9e566a4
lib_dir=/usr/lib/cyrus-imapd
38f2261
system_magic=$(file --version | awk '/magic file/ {print $4}')
9e566a4
cyrus_magic=${data_dir}/magic
9e566a4
cvt_cyrusdb=${lib_dir}/cvt_cyrusdb
9e566a4
sievec=${lib_dir}/sievec
9e566a4
masssievec=${lib_dir}/masssievec
9d65fa6
imap_prefix=$(get_config configdirectory) || exit 1
9d65fa6
sieve_dir=$(get_config sievedir) || exit 1
9e566a4
db_cfg=${data_dir}/db.cfg
38f2261
db_current=${imap_prefix}/rpm/db.cfg.current
38f2261
db_cache=${imap_prefix}/rpm/db.cfg.cache
38f2261
38f2261
# source default db backend config
38f2261
. $db_cfg
38f2261
38f2261
# get configured db backend config
9d65fa6
duplicate_db=$(get_config duplicate_db $duplicate_db) || exit 1
9d65fa6
mboxlist_db=$(get_config mboxlist_db $mboxlist_db) || exit 1
9d65fa6
seenstate_db=$(get_config seenstate_db $seenstate_db) || exit 1
9d65fa6
subscription_db=$(get_config subscription_db $subscription_db) || exit 1
9d65fa6
tlscache_db=$(get_config tlscache_db $tlscache_db) || exit 1
9d65fa6
annotation_db=$(get_config annotation_db $annotation_db) || exit 1
9d65fa6
mboxkey_db=$(get_config mboxkey_db $mboxkey_db) || exit 1
9d65fa6
ptscache_db=$(get_config ptscache_db $ptscache_db) || exit 1
9d65fa6
quota_db=$(get_config quota_db $quota_db) || exit 1
9d65fa6
statuscache_db=$(get_config statuscache_db $statuscache_db) || exit 1
9d65fa6
userdeny_db=$(get_config userdeny_db $userdeny_db) || exit 1
38f2261
38f2261
# remember current db backend config
38f2261
{
9d65fa6
echo "duplicate_db=${duplicate_db}"
9d65fa6
echo "mboxlist_db=${mboxlist_db}"
9d65fa6
echo "seenstate_db=${seenstate_db}"
9d65fa6
echo "subscription_db=${subscription_db}"
9d65fa6
echo "tlscache_db=${tlscache_db}"
9d65fa6
echo "annotation_db=${annotation_db}"
9d65fa6
echo "mboxkey_db=${mboxkey_db}"
9d65fa6
echo "ptscache_db=${ptscache_db}"
9d65fa6
echo "quota_db=${quota_db}"
9d65fa6
echo "statuscache_db=${statuscache_db}"
9d65fa6
echo "userdeny_db=${userdeny_db}"
9d65fa6
echo "sieve_version=${sieve_version}"
9e566a4
} | sort > $db_current
38f2261
9d65fa6
# file_type <file>
38f2261
file_type() {
9d65fa6
  this_type=$(file -b -m "${cyrus_magic}:${system_magic}" "$1" 2> /dev/null)
38f2261
  if echo "$this_type" | grep -qi skip > /dev/null 2>&1; then
38f2261
    echo skiplist
38f2261
  elif echo "$this_type" | grep -qi text > /dev/null 2>&1; then
38f2261
    echo flat
38f2261
  else
38f2261
    echo berkeley
38f2261
  fi
38f2261
}
38f2261
9d65fa6
# cvt_file <file> <db>
38f2261
cvt_file() {
38f2261
  target="$1"
38f2261
  new_db="$2"
38f2261
  if [ -s "$target" ]; then
38f2261
    old_db=$(file_type "$target")
38f2261
    if [ ! "$old_db" = "$new_db" ]; then
38f2261
      # The two-step conversion is paranoia against the filenames being encoded
38f2261
      # inside the database or logfiles (berkeley does this, for example).
9e566a4
      rm -f "${target}.skiplist"
9e566a4
      if [ "$old_db" = "skiplist" ]; then
9e566a4
        cp -a "$target" "${target}.skiplist"
38f2261
      else
9d65fa6
        $cvt_cyrusdb -C $IMAPDCONF "$target" "$old_db" "${target}.skiplist" skiplist
38f2261
      fi
38f2261
      RETVAL=$?
9d65fa6
      ERRVAL=$(( $ERRVAL + $RETVAL ))
38f2261
      if [ $RETVAL -eq 0 ]; then
38f2261
        rm -f "$target"
9e566a4
        if [ -s "${target}.skiplist" ]; then
9e566a4
          if [ "$new_db" = "skiplist" ]; then
9e566a4
            cp -a "${target}.skiplist" "$target"
38f2261
          else
9d65fa6
            $cvt_cyrusdb -C $IMAPDCONF "${target}.skiplist" skiplist "$target" "$new_db"
38f2261
          fi
38f2261
        fi
38f2261
        RETVAL=$?
9d65fa6
        ERRVAL=$(( $ERRVAL + $RETVAL ))
38f2261
        if [ $RETVAL -eq 0 ]; then
9e566a4
          rm -f "${target}.skiplist"
38f2261
        else
9e566a4
          echo "ERROR: unable to convert ${target}.skiplist from skiplist to $new_db"
38f2261
        fi
38f2261
      else
9e566a4
        echo "ERROR: unable to convert $target from $old_db to skiplist"
38f2261
      fi
38f2261
    fi
38f2261
  fi
38f2261
}
38f2261
9d65fa6
# cvt_to_utf8 <file>
38f2261
cvt_to_utf8() {
38f2261
  target="$1"
38f2261
  if [ -s "$target" ]; then
9d65fa6
    if ! $sievec -C $IMAPDCONF "$target" "${target}.sievec"; then
38f2261
      iconv --from-code=ISO-8859-1 --to-code=UTF-8 --output="${target}.UTF-8" "$target"
38f2261
      if [ -s "${target}.UTF-8" ]; then
38f2261
        # preserve timestamp
9d65fa6
        touch --reference="${target}" "${target}.UTF-8"
38f2261
        mv -f "${target}.UTF-8" "$target"
38f2261
      else
9d65fa6
        ERRVAL=$(( $ERRVAL + 1 ))
38f2261
      fi
38f2261
    fi
38f2261
    rm -f "${target}.sievec"
38f2261
  fi
38f2261
}
38f2261
38f2261
ERRVAL=0
38f2261
9e566a4
# make sure our Berkeley databases are in a sane state
9e566a4
# wait for db_checkpoint to end successfully or kill it after a timeout
9d65fa6
db_checkpoint -v -1 -h ${imap_prefix}/db &
9e566a4
DB_CHECK_PID=$!
9e566a4
CNT=0
9e566a4
while [ $CNT -lt 60 ]; do
9e566a4
  if ! kill -0 $DB_CHECK_PID > /dev/null 2>&1; then
9e566a4
    break
9e566a4
  fi
9e566a4
  sleep 1
9e566a4
  let CNT+=1
9e566a4
done
9e566a4
if kill -0 $DB_CHECK_PID > /dev/null 2>&1; then
9e566a4
  kill -USR1 $DB_CHECK_PID > /dev/null 2>&1
9e566a4
  sleep 1
9e566a4
  kill -KILL $DB_CHECK_PID > /dev/null 2>&1
9e566a4
  wait $DB_CHECK_PID > /dev/null 2>&1
9e566a4
fi
9e566a4
9e566a4
# do a normal recovery
9d65fa6
db_recover -v -h ${imap_prefix}/db
9e566a4
RETVAL=$?
9e566a4
if [ $RETVAL -ne 0 ]; then
9e566a4
  # try a catastrophic recovery instead of normal recovery
9d65fa6
  db_recover -v -c -h ${imap_prefix}/db
9e566a4
  RETVAL=$?
9d65fa6
  ERRVAL=$(( $ERRVAL + $RETVAL ))
9e566a4
  if [ $RETVAL -ne 0 ]; then
9e566a4
    echo "ERROR: catastrophic recovery of Berkeley databases failed"
9e566a4
  fi
9e566a4
fi
9e566a4
9e566a4
if [ "$EXPORT" = "export" ]; then
9d65fa6
  # convert all db files to portable format for migration
9e566a4
  # TODO: quota_db, we don't touch it for now
9d65fa6
  cvt_file ${imap_prefix}/deliver.db           "skiplist"
9d65fa6
  cvt_file ${imap_prefix}/mailboxes.db         "skiplist"
9d65fa6
  cvt_file ${imap_prefix}/tls_sessions.db      "skiplist"
9d65fa6
  cvt_file ${imap_prefix}/annotations.db       "skiplist"
9d65fa6
  cvt_file ${imap_prefix}/ptclient/ptscache.db "skiplist"
9d65fa6
  cvt_file ${imap_prefix}/statuscache.db       "skiplist"
9d65fa6
  cvt_file ${imap_prefix}/user_deny.db         "flat"
9d65fa6
  rm -vf ${imap_prefix}/db/log.*
9d65fa6
  rm -vf ${imap_prefix}/db/__db.*
9e566a4
else
9e566a4
  # always convert db files which have been converted to skiplist
9e566a4
  # TODO: quota_db, we don't touch it for now
9d65fa6
  cvt_file ${imap_prefix}/deliver.db           "$duplicate_db"
9d65fa6
  cvt_file ${imap_prefix}/mailboxes.db         "$mboxlist_db"
9d65fa6
  cvt_file ${imap_prefix}/tls_sessions.db      "$tlscache_db"
9d65fa6
  cvt_file ${imap_prefix}/annotations.db       "$annotation_db"
9d65fa6
  cvt_file ${imap_prefix}/ptclient/ptscache.db "$ptscache_db"
9d65fa6
  cvt_file ${imap_prefix}/statuscache.db       "$statuscache_db"
9d65fa6
  cvt_file ${imap_prefix}/user_deny.db         "$userdeny_db"
9e566a4
  # do we have to convert all databases?
9e566a4
  if ! cmp -s $db_current $db_cache; then
9e566a4
    # we treat sieve scripts the same way like db files
9e566a4
    find ${sieve_dir}/ -name "*.script" -type f | while read db_file trash; do
9e566a4
      cvt_to_utf8 "$db_file"
9e566a4
    done
9d65fa6
    $masssievec $sievec $IMAPDCONF
9e566a4
    # convert all db files left
9e566a4
    find ${imap_prefix}/user/ -name "*.seen" -type f | while read db_file trash; do
9e566a4
      cvt_file "$db_file" "$seenstate_db"
9e566a4
    done
9e566a4
    find ${imap_prefix}/user/ -name "*.sub" -type f | while read db_file trash; do
9e566a4
      cvt_file "$db_file" "$subscription_db"
9e566a4
    done
9e566a4
    find ${imap_prefix}/user/ -name "*.mboxkey" -type f | while read db_file trash; do
9e566a4
      cvt_file "$db_file" "$mboxkey_db"
9e566a4
    done
9e566a4
  fi
38f2261
fi
38f2261
9e566a4
# update the config cache file so we can check whether something has changed
38f2261
if [ $ERRVAL -eq 0 ]; then
38f2261
  mv -f $db_current $db_cache
38f2261
else
38f2261
  rm -f $db_cache
38f2261
  rm -f $db_current
38f2261
fi
38f2261
38f2261
exit $ERRVAL