#!/bin/sh # Copyright 1999-2007 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # # Remove old signatures and unimportant tokens from the DSPAM database. # Purge old log entries in user logs. # # # Parse optional command line parameters # for foo in $@ do case "${foo}" in --logdays=*) LOGROTATE_AGE="${foo#--logdays=}";; --sigdays=*) SIGNATURE_AGE="${foo#--sigdays=}";; esac done # # Parameters # [ -z "${LOGROTATE_AGE}" ] && LOGROTATE_AGE=30 # Delete log entries older than $LOGROTATE_AGE days [ -z "${SIGNATURE_AGE}" ] && SIGNATURE_AGE=30 # Delete signatures older than $SIGNATURE_AGE days # # Function to run dspam_clean # run_dspam_clean() { if [ ! -e "/usr/bin/dspam_clean" ] then echo "Can not run DSPAM clean application:" echo " /usr/bin/dspam_clean does not exist" return 1 else /usr/bin/dspam_clean -s${SIGNATURE_AGE} -p${SIGNATURE_AGE} -u${SIGNATURE_AGE},${SIGNATURE_AGE},${SIGNATURE_AGE},${SIGNATURE_AGE} >/dev/null 2>&1 return $? fi } # # Function to check if we have all needed tools # check_for_tools() { local myrc=0 for foo in awk cut sed getent do if ! which ${foo} >/dev/null 2>&1 then echo "Command ${foo} not found!" myrc=1 fi done return ${myrc} } # # Function to clean DSPAM MySQL data # clean_mysql_drv() { # # MySQL # if [ -f "${DSPAM_CONFIGDIR}/mysql.data" ] then if [ ! -e "/usr/bin/mysql_config" ] then echo "Can not run MySQL purge script:" echo " /usr/bin/mysql_config does not exist" return 1 fi DSPAM_MySQL_PURGE_SQL= DSPAM_MySQL_VER=$(/usr/bin/mysql_config --version | sed "s:[^0-9.]*::g") DSPAM_MySQL_MAJOR=$(echo "${DSPAM_MySQL_VER}" | cut -d. -f1) DSPAM_MySQL_MINOR=$(echo "${DSPAM_MySQL_VER}" | cut -d. -f2) DSPAM_MySQL_MICRO=$(echo "${DSPAM_MySQL_VER}" | cut -d. -f3) DSPAM_MySQL_INT=$(($DSPAM_MySQL_MAJOR * 65536 + $DSPAM_MySQL_MINOR * 256 + $DSPAM_MySQL_MICRO)) # For MySQL >= 4.1 use the new purge script if [ "${DSPAM_MySQL_INT}" -ge "262400" ] then if [ -f "${DSPAM_CONFIGDIR}/config/mysql_purge-4.1-optimized.sql" -o -f "${DSPAM_CONFIGDIR}/mysql_purge-4.1-optimized.sql" ] then # See: http://securitydot.net/txt/id/32/type/articles/ [ -f "${DSPAM_CONFIGDIR}/config/mysql_purge-4.1-optimized.sql" ] && DSPAM_MySQL_PURGE_SQL="${DSPAM_CONFIGDIR}/config/mysql_purge-4.1-optimized.sql" [ -f "${DSPAM_CONFIGDIR}/mysql_purge-4.1-optimized.sql" ] && DSPAM_MySQL_PURGE_SQL="${DSPAM_CONFIGDIR}/mysql_purge-4.1-optimized.sql" else [ -f "${DSPAM_CONFIGDIR}/config/mysql_purge-4.1.sql" ] && DSPAM_MySQL_PURGE_SQL="${DSPAM_CONFIGDIR}/config/mysql_purge-4.1.sql" [ -f "${DSPAM_CONFIGDIR}/mysql_purge-4.1.sql" ] && DSPAM_MySQL_PURGE_SQL="${DSPAM_CONFIGDIR}/mysql_purge-4.1.sql" fi else [ -f "${DSPAM_CONFIGDIR}/config/mysql_purge.sql" ] && DSPAM_MySQL_PURGE_SQL="${DSPAM_CONFIGDIR}/config/mysql_purge.sql" [ -f "${DSPAM_CONFIGDIR}/mysql_purge.sql" ] && DSPAM_MySQL_PURGE_SQL="${DSPAM_CONFIGDIR}/mysql_purge.sql" fi if [ -z "${DSPAM_MySQL_PURGE_SQL}" ] then echo "Can not run MySQL purge script:" echo " No mysql_purge SQL script found" return 1 fi if [ ! -e "/usr/bin/mysql" ] then echo "Can not run MySQL purge script:" echo " /usr/bin/mysql does not exist" return 1 fi # Get DSPAM MySQL username and password DSPAM_MySQL_HOST=$(sed "1q;d" "${DSPAM_CONFIGDIR}/mysql.data") DSPAM_MySQL_PORT=$(sed "2q;d" "${DSPAM_CONFIGDIR}/mysql.data") DSPAM_MySQL_USER=$(sed "3q;d" "${DSPAM_CONFIGDIR}/mysql.data") DSPAM_MySQL_PWD=$(sed "4q;d" "${DSPAM_CONFIGDIR}/mysql.data") DSPAM_MySQL_DB=$(sed "5q;d" "${DSPAM_CONFIGDIR}/mysql.data") # Check if MySQL is remote or using a socket if [ -S "${DSPAM_MySQL_HOST}" ] then DSPAM_MySQL_HOSTCMD="--socket" else DSPAM_MySQL_HOSTCMD="--host" fi # Run the MySQL purge script /usr/bin/mysql --silent --user="${DSPAM_MySQL_USER}" --password="${DSPAM_MySQL_PWD}" ${DSPAM_MySQL_HOSTCMD}="${DSPAM_MySQL_HOST}" ${DSPAM_MySQL_DB} < ${DSPAM_MySQL_PURGE_SQL} _RC=$? if [ ${_RC} != 0 ] then echo "MySQL purge script returned error code ${_RC}" fi # Optimize the MySQL tables for DSPAM for foo in $(/usr/bin/mysql --user="${DSPAM_MySQL_USER}" --password="${DSPAM_MySQL_PWD}" ${DSPAM_MySQL_HOSTCMD}="${DSPAM_MySQL_HOST}" --silent --skip-column-names --batch ${DSPAM_MySQL_DB} -e 'SHOW TABLES;' 2>&1) do /usr/bin/mysql --user="${DSPAM_MySQL_USER}" --password="${DSPAM_MySQL_PWD}" ${DSPAM_MySQL_HOSTCMD}="${DSPAM_MySQL_HOST}" ${DSPAM_MySQL_DB} -e "OPTIMIZE TABLE ${foo};" 1>/dev/null 2>&1 _RC=$? if [ ${_RC} != 0 ] then echo "MySQL optimize script for table \"${foo}\" returned error code ${_RC}" fi done return 0 fi } # # Function to clean DSPAM PostgreSQL data # clean_pgsql_drv() { # # PostgreSQL # if [ -f "${DSPAM_CONFIGDIR}/pgsql.data" ] then DSPAM_PgSQL_PURGE_SQL="" [ -f "${DSPAM_CONFIGDIR}/config/pgsql_purge.sql" ] && DSPAM_PgSQL_PURGE_SQL="${DSPAM_CONFIGDIR}/config/pgsql_purge.sql" [ -f "${DSPAM_CONFIGDIR}/pgsql_purge.sql" ] && DSPAM_PgSQL_PURGE_SQL="${DSPAM_CONFIGDIR}/pgsql_purge.sql" if [ -z "${DSPAM_PgSQL_PURGE_SQL}" ] then echo "Can not run PostgreSQL purge script:" echo " No pgsql_purge SQL script found" return 1 fi if [ ! -e "/usr/bin/psql" ] then echo "Can not run PostgreSQL purge script:" echo " /usr/bin/psql does not exist" return 1 fi # Get DSPAM PostgreSQL username and password DSPAM_PgSQL_HOST=$(sed "1q;d" "${DSPAM_CONFIGDIR}/pgsql.data") DSPAM_PgSQL_PORT=$(sed "2q;d" "${DSPAM_CONFIGDIR}/pgsql.data") DSPAM_PgSQL_USER=$(sed "3q;d" "${DSPAM_CONFIGDIR}/pgsql.data") DSPAM_PgSQL_PWD=$(sed "4q;d" "${DSPAM_CONFIGDIR}/pgsql.data") DSPAM_PgSQL_DB=$(sed "5q;d" "${DSPAM_CONFIGDIR}/pgsql.data") # Run the PostgreSQL purge script PGUSER="${DSPAM_PgSQL_USER}" PGPASSWORD="${DSPAM_PgSQL_PWD}" /usr/bin/psql -q -U "${DSPAM_PgSQL_USER}" -d "${DSPAM_PgSQL_DB}" -p "${DSPAM_PgSQL_PORT}" -h "${DSPAM_PgSQL_HOST}" -f "${DSPAM_PgSQL_PURGE_SQL}" >/dev/null 2>&1 _RC=$? if [ ${_RC} != 0 ] then echo "PostgreSQL purge script returned error code ${_RC}" fi # Optimize the PostgreSQL tables for DSPAM for foo in dspam_preferences dspam_signature_data dspam_stats dspam_token_data dspam_virtual_uids do PGUSER="${DSPAM_PgSQL_USER}" PGPASSWORD="${DSPAM_PgSQL_PWD}" /usr/bin/vacuumdb -q -U "${DSPAM_PgSQL_USER}" -d "${DSPAM_PgSQL_DB}" -p "${DSPAM_PgSQL_PORT}" -h "${DSPAM_PgSQL_HOST}" -f -t "${foo}" >/dev/null 2>&1 _RC=$? if [ ${_RC} != 0 ] then echo "PostgreSQL vacuumdb script for table \"public.${foo}\" returned error code ${_RC}" fi done return 0 fi } # # Function to clean DSPAM Hash data # clean_hash_drv() { # # Hash # if [ -d "${DSPAM_HOMEDIR}/data" ] then find ${DSPAM_HOMEDIR}/data/ -maxdepth 4 -mindepth 1 -type f -name "*.css" | while read name do /usr/bin/csscompress "${name}" 1>/dev/null 2>&1 /usr/bin/cssclean "${name}" 1>/dev/null 2>&1 # chown dspam:dspam "${name}" done find ${DSPAM_HOMEDIR}/data/ -maxdepth 4 -mindepth 1 -type d -name "*.sig" | while read name do find "${name}" -maxdepth 1 -mindepth 1 -type f -mtime +${SIGNATURE_AGE} -name "*.sig" -exec /bin/rm "{}" ";" done return 0 else return 1 fi } # # Function to clean DSPAM SQLite3 data # clean_sqlite3_drv() { # # SQLite3 # # if ( grep -q "^[[:space:]]*StorageDriver[[:space:]]*.*libsqlite3_drv" "${DSPAM_CONFIGDIR}/dspam.conf" ) # then DSPAM_SQLite3_PURGE_SQL="" [ -f "${DSPAM_CONFIGDIR}/config/sqlite3_purge.sql" ] && DSPAM_SQLite3_PURGE_SQL="${DSPAM_CONFIGDIR}/config/sqlite3_purge.sql" [ -f "${DSPAM_CONFIGDIR}/sqlite3_purge.sql" ] && DSPAM_SQLite3_PURGE_SQL="${DSPAM_CONFIGDIR}/sqlite3_purge.sql" if [ -z "${DSPAM_SQLite3_PURGE_SQL}" ] then echo "Can not run SQLite3 purge script:" echo " No sqlite_purge SQL script found" return 1 fi if [ ! -e "/usr/bin/sqlite3" ] then echo "Can not run SQLite3 purge script:" echo " /usr/bin/sqlite3 does not exist" return 1 fi # Run the SQLite3 purge script and optimize database find "${DSPAM_HOMEDIR}" -name "*.sdb" -print | while read name do /usr/bin/sqlite3 "$name" < "${DSPAM_SQLite3_PURGE_SQL}" /usr/bin/sqlite3 "$name" vacuum done 1>/dev/null 2>&1 return 0 # fi } # # Function to clean DSPAM SQLite data # clean_sqlite_drv() { # # SQLite # # if ( grep -q "^[[:space:]]*StorageDriver[[:space:]]*.*libsqlite_drv" "${DSPAM_CONFIGDIR}/dspam.conf" ) # then DSPAM_SQLite_PURGE_SQL="" [ -f "${DSPAM_CONFIGDIR}/config/sqlite_purge.sql" ] && DSPAM_SQLite_PURGE_SQL="${DSPAM_CONFIGDIR}/config/sqlite_purge.sql" [ -f "${DSPAM_CONFIGDIR}/sqlite_purge.sql" ] && DSPAM_SQLite_PURGE_SQL="${DSPAM_CONFIGDIR}/sqlite_purge.sql" if [ -z "${DSPAM_SQLite_PURGE_SQL}" ] then echo "Can not run SQLite purge script:" echo " No sqlite_purge SQL script found" return 1 fi if [ ! -e "/usr/bin/sqlite" ] then echo "Can not run SQLite purge script:" echo " /usr/bin/sqlite does not exist" return 1 fi # Run the SQLite purge script and optimize database find "${DSPAM_HOMEDIR}" -name "*.sdb" -print | while read name do /usr/bin/sqlite "$name" < "${DSPAM_SQLite_PURGE_SQL}" /usr/bin/sqlite "$name" vacuum done 1>/dev/null 2>&1 return 0 # fi } # # Acquire lock file and start processing # DSPAM_CRON_LOCKFILE="/var/run/$(basename $0 .sh).pid" if ( set -o noclobber; echo "$$" > "${DSPAM_CRON_LOCKFILE}") 2> /dev/null; then trap 'rm -f "${DSPAM_CRON_LOCKFILE}"; exit $?' INT TERM EXIT # # Check for needed tools # if ! check_for_tools then # We have not all needed tools installed. Run just the dspam_clean part. run_dspam_clean exit $? fi # # Try to get DSPAM config directory # DSPAM_CONFIGDIR=$(getent passwd dspam | awk -F : '{print $6}') if [ ! -f "${DSPAM_CONFIGDIR}/dspam.conf" ] then # Something is wrong in passwd! Check if /etc/mail/dspam exists instead. if [ -f /etc/mail/dspam/dspam.conf ] then DSPAM_CONFIGDIR="/etc/mail/dspam" fi fi if [ ! -d "${DSPAM_CONFIGDIR}" ] then echo "Configuration directory not found!" exit 2 fi # # Try to get DSPAM data home directory # DSPAM_HOMEDIR=$(awk 'tolower($1) ~ /^home$/ {print $2}' "${DSPAM_CONFIGDIR}/dspam.conf") if [ ! -d "${DSPAM_HOMEDIR}" ] then # Something is wrong in dspam.conf! Check if /var/spool/dspam exists instead. if [ -d /var/spool/dspam ] then DSPAM_HOMEDIR="/var/spool/dspam" fi fi if [ ! -d "${DSPAM_HOMEDIR}" ] then echo "Home directory not found! Please fix your dspam.conf." exit 2 fi # # User log purging # if [ -d "${DSPAM_CONFIGDIR}/data" ] then dspam_logrotate -a ${LOGROTATE_AGE} -d "${DSPAM_CONFIGDIR}/data" >/dev/null # 2>&1 fi if [ ! -e "/usr/bin/dspam" ] then echo "Can not run DSPAM application:" echo " /usr/bin/dspam does not exist" return 1 fi # # Process all available storage drivers # for foo in $(/usr/bin/dspam --version 2>&1 | sed -n "s:,: :g;s:^.*\-\-with\-storage\-driver=\([^\'\]*\).*:\1:gIp") do case "${foo}" in hash_drv) clean_hash_drv ;; mysql_drv) clean_mysql_drv ;; pgsql_drv) clean_pgsql_drv ;; sqlite3_drv) clean_sqlite3_drv ;; sqlite_drv) clean_sqlite_drv ;; esac done # # Run the dspam_clean command # run_dspam_clean # # Release lock # /bin/rm -f "${DSPAM_CRON_LOCKFILE}" trap - INT TERM EXIT fi