Blame SOURCES/cyrus-imapd.cvt_cyrusdb_all

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