Blame SOURCES/cyrus-imapd.cvt_cyrusdb_all

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