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