243 lines
7.8 KiB
Plaintext
243 lines
7.8 KiB
Plaintext
|
#!/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
|
||
|
#
|
||
|
# v1.0.6, Jul 18 2003 Simon Matter <simon.matter@invoca.ch>
|
||
|
# - changed db3 to berkeley
|
||
|
# - added new db backends for 2.2
|
||
|
#
|
||
|
# v1.0.7, Jan 23 2004 Simon Matter <simon.matter@invoca.ch>
|
||
|
# - included some modifications from Luca Olivetti <luca@olivetti.cjb.net>
|
||
|
# - added masssievec functionality
|
||
|
#
|
||
|
# v1.0.8, Jan 28 2004 Simon Matter <simon.matter@invoca.ch>
|
||
|
# - convert sieve scripts to UTF-8 before calling masssievec
|
||
|
#
|
||
|
# v1.0.9, Jan 29 2004 Simon Matter <simon.matter@invoca.ch>
|
||
|
# - convert sieve scripts to UTF-8 only if sievec failed before
|
||
|
#
|
||
|
# v1.0.10, Feb 24 2004 Simon Matter <simon.matter@invoca.ch>
|
||
|
# - change su within init script to get input from
|
||
|
# /dev/null, this prevents hang when running in SELinux
|
||
|
#
|
||
|
# v1.0.11, Mar 02 2004 Simon Matter <simon.matter@invoca.ch>
|
||
|
# - fixed SELinux fix
|
||
|
#
|
||
|
# v1.0.12, Dec 16 2004 Simon Matter <simon.matter@invoca.ch>
|
||
|
# - use runuser instead of su if available
|
||
|
|
||
|
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
|
||
|
|
||
|
# fallback to su if runuser not available
|
||
|
if [ -x /sbin/runuser ]; then
|
||
|
RUNUSER=runuser
|
||
|
else
|
||
|
RUNUSER=su
|
||
|
fi
|
||
|
|
||
|
# force cyrus user for security reasons
|
||
|
if [ ! $(whoami) = "cyrus" ]; then
|
||
|
exec $RUNUSER - cyrus -c "cd $PWD < /dev/null ; $0"
|
||
|
fi
|
||
|
|
||
|
# files get mode 0600
|
||
|
umask 166
|
||
|
|
||
|
# 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
|
||
|
}
|
||
|
|
||
|
# where to find files and directories
|
||
|
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
|
||
|
sievec=/usr/lib/cyrus-imapd/sievec
|
||
|
imap_prefix=$(get_config configdirectory /var/lib/imap)
|
||
|
sieve_dir=$(get_config sievedir /var/lib/imap/sieve)
|
||
|
db_cfg=/usr/share/cyrus-imapd/rpm/db.cfg
|
||
|
db_current=${imap_prefix}/rpm/db.cfg.current
|
||
|
db_cache=${imap_prefix}/rpm/db.cfg.cache
|
||
|
|
||
|
# source default db backend config
|
||
|
. $db_cfg
|
||
|
|
||
|
# get configured db backend config
|
||
|
CONFIG_DB_DUPLICATE=$(get_config duplicate_db $CONFIG_DB_DUPLICATE)
|
||
|
CONFIG_DB_MBOX=$(get_config mboxlist_db $CONFIG_DB_MBOX)
|
||
|
CONFIG_DB_SEEN=$(get_config seenstate_db $CONFIG_DB_SEEN)
|
||
|
CONFIG_DB_SUBS=$(get_config subscription_db $CONFIG_DB_SUBS)
|
||
|
CONFIG_DB_TLS=$(get_config tlscache_db $CONFIG_DB_TLS)
|
||
|
CONFIG_DB_ANNOTATION=$(get_config annotation_db $CONFIG_DB_ANNOTATION)
|
||
|
|
||
|
# remember current db backend config
|
||
|
{
|
||
|
echo "CONFIG_DB_DUPLICATE=$CONFIG_DB_DUPLICATE"
|
||
|
echo "CONFIG_DB_MBOX=$CONFIG_DB_MBOX"
|
||
|
echo "CONFIG_DB_SEEN=$CONFIG_DB_SEEN"
|
||
|
echo "CONFIG_DB_SUBS=$CONFIG_DB_SUBS"
|
||
|
echo "CONFIG_DB_TLS=$CONFIG_DB_TLS"
|
||
|
echo "CONFIG_DB_ANNOTATION=$CONFIG_DB_ANNOTATION"
|
||
|
echo "CONFIG_DB_SIEVE=$CONFIG_DB_SIEVE"
|
||
|
} > $db_current
|
||
|
|
||
|
# file_type [file]
|
||
|
file_type() {
|
||
|
this_type=$(file -b -m "$system_magic:$cyrus_magic" "$1" 2> /dev/null)
|
||
|
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
|
||
|
}
|
||
|
|
||
|
# cvt_to_utf8 [file]
|
||
|
cvt_to_utf8() {
|
||
|
target="$1"
|
||
|
if [ -s "$target" ]; then
|
||
|
if ! $sievec "$target" "${target}.sievec"; then
|
||
|
iconv --from-code=ISO-8859-1 --to-code=UTF-8 --output="${target}.UTF-8" "$target"
|
||
|
if [ -s "${target}.UTF-8" ]; then
|
||
|
# preserve timestamp
|
||
|
touch --reference="$target" "${target}.UTF-8"
|
||
|
mv -f "${target}.UTF-8" "$target"
|
||
|
else
|
||
|
ERRVAL=$[ $ERRVAL + 1 ]
|
||
|
fi
|
||
|
fi
|
||
|
rm -f "${target}.sievec"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
ERRVAL=0
|
||
|
|
||
|
# do we need to convert databases ?
|
||
|
if ! cmp -s $db_current $db_cache; then
|
||
|
# we treat sieve scripts the same way like db files
|
||
|
find ${sieve_dir}/ -name "*.script" -type f | while read db_file trash; do
|
||
|
cvt_to_utf8 "$db_file"
|
||
|
done
|
||
|
/usr/lib/cyrus-imapd/masssievec /usr/lib/cyrus-imapd/sievec
|
||
|
# convert all db files
|
||
|
cvt_file $imap_prefix/deliver.db "$CONFIG_DB_DUPLICATE"
|
||
|
cvt_file $imap_prefix/mailboxes.db "$CONFIG_DB_MBOX"
|
||
|
cvt_file $imap_prefix/tls_sessions.db "$CONFIG_DB_TLS"
|
||
|
cvt_file $imap_prefix/annotations.db "$CONFIG_DB_ANNOTATION"
|
||
|
find ${imap_prefix}/user/ -name "*.seen" -type f | while read db_file trash; do
|
||
|
cvt_file "$db_file" "$CONFIG_DB_SEEN"
|
||
|
done
|
||
|
find ${imap_prefix}/user/ -name "*.sub" -type f | while read db_file trash; do
|
||
|
cvt_file "$db_file" "$CONFIG_DB_SUBS"
|
||
|
done
|
||
|
fi
|
||
|
|
||
|
# copy the current config file so we can check whether something has changed
|
||
|
if [ $ERRVAL -eq 0 ]; then
|
||
|
mv -f $db_current $db_cache
|
||
|
else
|
||
|
rm -f $db_cache
|
||
|
rm -f $db_current
|
||
|
fi
|
||
|
|
||
|
exit $ERRVAL
|