source: npl/mailserver/dspam/dspam-3.10.2/contrib/dspam_maintenance/dspam_maintenance.sh

Last change on this file was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100755
File size: 39.3 KB
Line 
1#!/bin/bash
2#
3# $Id: dspam_maintenance.sh,v 1.22 2011/07/02 12:54:17 sbajic Exp $
4#
5# Copyright 2007-2010 Stevan Bajic <stevan@bajic.ch>
6# Distributed under the terms of the GNU Affero General Public License v3
7#
8# Purpose: Remove old signatures and unimportant tokens from the DSPAM
9#          database. Purge old log entries in user and system logs.
10#
11# Note   : I originally wrote that script for Gentoo Linux in 2007 and it
12#          is since then distributed with the Gentoo DSPAM ebuild. This
13#          edition here is slightly changed to be more flexible. I have
14#          not tested that script on other distros except on Gentoo Linux
15#          and on a older CentOS distro. Other members from the DSPAM
16#          mailing list have tested the script on Debian. Should you find
17#          any issues with this script then please post a message on the
18#          DSPAM user mailing list.
19#
20# Note   : This script can be called from the command line or run from
21#          within cron. Either add a line in your crontab or add this
22#          script in your cron.{hourly,daily,weekly,monthly}. Running
23#          this script every hour might not be the best idea but it's
24#          your decision if you want to do so.
25###
26
27DSPAM_CONFIGDIR=""
28INCLUDE_DIRS=""
29DSPAM_HOMEDIR=""
30DSPAM_PURGE_SCRIPT_DIR=""
31DSPAM_BIN_DIR=""
32MYSQL_BIN_DIR="/usr/bin"
33PGSQL_BIN_DIR="/usr/bin"
34SQLITE_BIN_DIR="/usr/bin"
35SQLITE3_BIN_DIR="/usr/bin"
36
37
38#
39# Parse optional command line parameters
40#
41for foo in $@
42do
43        case "${foo}" in
44                --profile=*) PROFILE="${foo#--profile=}";;
45                --logdays=*) LOGROTATE_AGE="${foo#--logdays=}";;
46                --signatures=*) SIGNATURE_AGE="${foo#--signatures=}";;
47                --neutral=*) NEUTRAL_AGE="${foo#--neutral=}";;
48                --unused=*) UNUSED_AGE="${foo#--unused=}";;
49                --hapaxes=*) HAPAXES_AGE="${foo#--hapaxes=}";;
50                --hits1s=*) HITS1S_AGE="${foo#--hits1s=}";;
51                --hits1i=*) HITS1I_AGE="${foo#--hits1i=}";;
52                --purgescriptdir=*) DSPAM_PURGE_SCRIPT_DIR="${foo#--purgescriptdir=}";;
53                --without-sql-purge) USE_SQL_PURGE="false";;
54                --with-sql-optimization) USE_SQL_OPTIMIZATION="true";;
55                --with-sql-autoupdate) USE_SQL_AUTOUPDATE="true";;
56                --with-all-drivers) PURGE_ALL_DRIVERS="true";;
57                --verbose) VERBOSE="true";;
58                --help | -help | --h | -h)
59                        echo "Usage: $0"
60                        echo
61                        echo "  [--profile=[PROFILE]]"
62                        echo "    Specify a storage profile from dspam.conf. The storage profile"
63                        echo "    selected will be used for all database connectivity. See"
64                        echo "    dspam.conf for more information."
65                        echo
66                        echo "  [--logdays=no_of_days]"
67                        echo "    All log entries older than 'no_of_days' days will be removed."
68                        echo "    Note: Default is 31 days. (for more info: man dspam_logrotate)"
69                        echo
70                        echo "  [--signatures=no_of_days]"
71                        echo "    All signatures older than 'no_of_days' days will be removed."
72                        echo "    Note: Default value is set by the 'PurgeSignatures' option in"
73                        echo "    dspam.conf, if this option is not set, the default value is 14"
74                        echo "    days. This option is only used for the Hash driver."
75                        echo "    For more info: man dspam_clean"
76                        echo
77                        echo "  [--neutral=no_of_days]"
78                        echo "    Remove tokens whose probability is between 0.35 and 0.65."
79                        echo "    Note: Default value is set by the 'PurgeNeutral' option in dspam.conf,"
80                        echo "    if this option is not set, the default value is 90 days."
81                        echo "    For more info: man dspam_clean"
82                        echo
83                        echo "  [--unused=no_of_days]"
84                        echo "    Remove tokens which have not been used for a long period of time."
85                        echo "    Note: Default value is set by the 'PurgeUnused' option in dspam.conf,"
86                        echo "    if this option is not set, the default value is 90 days."
87                        echo "    For more info: man dspam_clean"
88                        echo
89                        echo "  [--hapaxes=no_of_days]"
90                        echo "    Remove tokens with a total hit count below 5."
91                        echo "    Note: Default value is set by the 'PurgeHapaxes' option in dspam.conf,"
92                        echo "    if this option is not set, the default value is 30 days."
93                        echo "    For more info: man dspam_clean"
94                        echo
95                        echo "  [--hits1s=no_of_days]"
96                        echo "    Remove tokens with a single SPAM hit."
97                        echo "    Note: Default value is set by the 'PurgeHits1S' option in dspam.conf,"
98                        echo "    if this option is not set, the default value is 15 days."
99                        echo "    For more info: man dspam_clean"
100                        echo
101                        echo "  [--hits1i=no_of_days]"
102                        echo "    Remove tokens with a single INNOCENT hit."
103                        echo "    Note: Default value is set by the 'PurgeHits1I' option in dspam.conf,"
104                        echo "    if this option is not set, the default value is 15 days."
105                        echo "    For more info: man dspam_clean"
106                        echo
107                        echo "  [--without-sql-purge]"
108                        echo "    Do not use SQL based purging. Only run dspam_clean."
109                        echo "    Note: Default is off (aka: use SQL based purging)."
110                        echo
111                        echo "  [--with-sql-optimization]"
112                        echo "    Run VACUUM (for PostgreSQL/SQLite) and/or OPTIMIZE (for MySQL)."
113                        echo "    Note: Default is off (aka: do not use optimizations)."
114                        echo
115                        echo "  [--with-sql-autoupdate]"
116                        echo "    Run SQL based purging with purge day values passed to this script."
117                        echo "    Note: Default is off (aka: do not attempt to modify SQL instructions)."
118                        echo
119                        echo "  [--purgescriptdir=[DIRECTORY]"
120                        echo "    Space separated list of directories where to search for SQL files"
121                        echo "    used for the SQL based purging."
122                        echo
123                        echo "  [--with-all-drivers]"
124                        echo "    Process all installed storage drivers (not just the active driver)."
125                        echo "    Note: Default is true (aka: process all installed drivers)."
126                        echo
127                        echo "  [--verbose]"
128                        echo "    Verbose output while running maintenance script."
129                        echo
130                        exit 1
131                        ;;
132                *)
133                        echo "Unrecognized parameter '${foo}'. Use parameter --help for more info."
134                        exit 1
135                        ;;
136
137        esac
138done
139
140
141#
142# Function to run dspam_clean
143#
144run_dspam_clean() {
145        [ "${VERBOSE}" = "true" ] && echo "Running dspam_clean ..."
146        local PURGE_ALL="${1}"
147        local ADD_PARAMETER=""
148        read_dspam_params DefaultProfile
149        if [ -n "${PROFILE}" -a -n "${DefaultProfile}" -a "${PROFILE/*.}" != "${DefaultProfile}" ]
150        then
151                ADD_PARAMETER="--profile=${PROFILE/*.}"
152        fi
153        if [ -z "${SIGNATURE_AGE}" -o -z "${NEUTRAL_AGE}" -o -z "${UNUSED_AGE}" -o -z "${HAPAXES_AGE}" -o -z "${HITS1S_AGE}" -o -z "${HITS1I_AGE}" ]
154        then
155                [ "${VERBOSE}" = "true" ] && echo " * with default parameters"
156                ${DSPAM_BIN_DIR}/dspam_clean ${ADD_PARAMETER}
157        else
158                if [ "${PURGE_ALL}" = "YES" ]
159                then
160                        [ "${VERBOSE}" = "true" ] && echo "  * with full purging"
161                        ${DSPAM_BIN_DIR}/dspam_clean ${ADD_PARAMETER} -s${SIGNATURE_AGE} -p${NEUTRAL_AGE} -u${UNUSED_AGE},${HAPAXES_AGE},${HITS1S_AGE},${HITS1I_AGE} >/dev/null 2>&1
162                else
163                        [ "${VERBOSE}" = "true" ] && echo "  * with neutral token purging only"
164                        ${DSPAM_BIN_DIR}/dspam_clean ${ADD_PARAMETER} -p${NEUTRAL_AGE} >/dev/null 2>&1
165                fi
166        fi
167        return ${?}
168}
169
170
171#
172# Function to check if we have all needed tools
173#
174check_for_tool() {
175        local myrc=0
176        [ -z "${1}" ] && return 2
177        for foo in ${@}
178        do
179                if ! which ${foo} >/dev/null 2>&1
180                then
181                        echo "Command ${foo} not found!"
182                        myrc=1
183                        break
184                fi
185        done
186        return ${myrc}
187}
188
189
190#
191# Function to read dspam.conf parameters
192#
193read_dspam_params() {
194        local PARAMETER VALUE
195        for PARAMETER in $@ ; do
196                VALUE=$(awk "BEGIN { IGNORECASE=1; } \$1==\"${PARAMETER}\" { print \$2; exit; }" "${DSPAM_CONFIGDIR}/dspam.conf" ${INCLUDE_DIRS[@]} 2>/dev/null)
197                [ ${?} = 0 ] || return 1
198                eval ${PARAMETER/.*}=\'${VALUE}\'
199        done
200        return 0
201}
202
203
204#
205# Function to clean DSPAM MySQL data
206#
207clean_mysql_drv() {
208        #
209        # MySQL
210        #
211        [ "${VERBOSE}" = "true" ] && echo "Running MySQL storage driver data cleanup"
212        if [ "${USE_SQL_PURGE}" = "true" ] && \
213                read_dspam_params MySQLServer${PROFILE} MySQLPort${PROFILE} MySQLUser${PROFILE} MySQLPass${PROFILE} MySQLDb${PROFILE} MySQLCompress${PROFILE} && \
214                [ -n "${MySQLServer}" -a -n "${MySQLUser}" -a -n "${MySQLDb}" ]
215        then
216                for foo in ${MYSQL_BIN_DIR} /usr/bin /usr/local/bin /usr/sbin /usr/local/sbin
217                do
218                        if [ -e "${foo}/mysql_config" -o -e "${foo}/mysql" ]
219                        then
220                                MYSQL_BIN_DIR=${foo}
221                                break
222                        fi
223                done
224                if [ -e "${MYSQL_BIN_DIR}/mysql_config" ]
225                then
226                        DSPAM_MySQL_VER=$(${MYSQL_BIN_DIR}/mysql_config --version | sed -n 's:^[^0-9]*\([0-9.]*\).*:\1:p')
227                elif [ -e "${MYSQL_BIN_DIR}/mysql" ]
228                then
229                        DSPAM_MySQL_VER=$(${MYSQL_BIN_DIR}/mysql --version | sed -n 's:^.*[\t ]Distrib[^0-9]*\([0-9.]*\).*:\1:p')
230                fi
231
232                if [ -z "${DSPAM_MySQL_VER}" ]
233                then
234                        [ "${VERBOSE}" = "true" ] && echo "  Can not run MySQL purge script:"
235                        [ "${VERBOSE}" = "true" ] && echo "    ${MYSQL_BIN_DIR}/mysql_config or ${MYSQL_BIN_DIR}/mysql does not exist"
236                        return 1
237                fi
238
239                DSPAM_MySQL_PURGE_SQL=
240                DSPAM_MySQL_PURGE_SQL_FILES=
241                DSPAM_MySQL_MAJOR=$(echo "${DSPAM_MySQL_VER}" | cut -d. -f1)
242                DSPAM_MySQL_MINOR=$(echo "${DSPAM_MySQL_VER}" | cut -d. -f2)
243                DSPAM_MySQL_MICRO=$(echo "${DSPAM_MySQL_VER}" | cut -d. -f3)
244                DSPAM_MySQL_INT=$(($DSPAM_MySQL_MAJOR * 65536 + $DSPAM_MySQL_MINOR * 256 + $DSPAM_MySQL_MICRO))
245
246                if [ "${DSPAM_MySQL_INT}" -ge "262400" ]
247                then
248                        # For MySQL >= 4.1 use the new purge script
249                        # For the 4.1-optimized version see:
250                        # http://securitydot.net/txt/id/32/type/articles/
251                        # Version >= 3.9.0 of DSPAM do already include a better purge script.
252                        DSPAM_MySQL_PURGE_SQL_FILES="mysql_purge-4.1 mysql_purge-4.1-optimized"
253                else
254                        DSPAM_MySQL_PURGE_SQL_FILES="mysql_purge"
255
256                fi
257
258                #
259                # We first search for the purge scripts in the directory the user has
260                # told us to look for (command line option: --purgescriptdir
261                # Then we look in DSPAM configuration directory under ./config/ and then
262                # in the DSPAM configuration directory it self.
263                #
264                for foo in ${DSPAM_PURGE_SCRIPT_DIR} ${DSPAM_CONFIGDIR}/config ${DSPAM_CONFIGDIR} /usr/share/doc/libdspam7-drv-mysql/sql /usr/share/dspam/sql /usr/local/share/examples/dspam/mysql /usr/share/examples/dspam/mysql
265                do
266                        for bar in ${DSPAM_MySQL_PURGE_SQL_FILES}
267                        do
268                                if [ -z "${DSPAM_MySQL_PURGE_SQL}" -a -f "${foo}/${bar}.sql" ]
269                                then
270                                        DSPAM_MySQL_PURGE_SQL="${foo}/${bar}.sql"
271                                        if (grep -iq "to_days" "${DSPAM_MySQL_PURGE_SQL}")
272                                        then
273                                                break
274                                        else
275                                                DSPAM_MySQL_PURGE_SQL=""
276                                        fi
277                                fi
278                                if [ -z "${DSPAM_MySQL_PURGE_SQL}" -a -f "${foo}/${bar/_//}.sql" ]
279                                then
280                                        DSPAM_MySQL_PURGE_SQL="${foo}/${bar/_//}.sql"
281                                        if (grep -iq "to_days" "${DSPAM_MySQL_PURGE_SQL}")
282                                        then
283                                                break
284                                        else
285                                                DSPAM_MySQL_PURGE_SQL=""
286                                        fi
287                                fi
288                                if [ -z "${DSPAM_MySQL_PURGE_SQL}" -a -f "${foo}/${bar/*_/}.sql" ]
289                                then
290                                        DSPAM_MySQL_PURGE_SQL="${foo}/${bar/*_/}.sql"
291                                        if (grep -iq "to_days" "${DSPAM_MySQL_PURGE_SQL}")
292                                        then
293                                                break
294                                        else
295                                                DSPAM_MySQL_PURGE_SQL=""
296                                        fi
297                                fi
298                        done
299                        if [ -n "${DSPAM_MySQL_PURGE_SQL}" ] && (grep -iq "to_days" "${DSPAM_MySQL_PURGE_SQL}")
300                        then
301                                break
302                        else
303                                DSPAM_MySQL_PURGE_SQL=""
304                        fi
305                done
306
307                if [ -z "${DSPAM_MySQL_PURGE_SQL}" ]
308                then
309                        [ "${VERBOSE}" = "true" ] && echo "  Can not run MySQL purge script:"
310                        [ "${VERBOSE}" = "true" ] && echo "    None of the ${DSPAM_MySQL_PURGE_SQL_FILES} SQL script(s) found"
311                        return 1
312                fi
313
314                if [ ! -r "${DSPAM_MySQL_PURGE_SQL}" ]
315                then
316                        [ "${VERBOSE}" = "true" ] && echo "  Can not read MySQL purge script:"
317                        [ "${VERBOSE}" = "true" ] && echo "    ${DSPAM_MySQL_PURGE_SQL}"
318                        return 1
319                fi
320
321                if [ ! -e "${MYSQL_BIN_DIR}/mysql" ]
322                then
323                        [ "${VERBOSE}" = "true" ] && echo "  Can not run MySQL purge script:"
324                        [ "${VERBOSE}" = "true" ] && echo "    ${MYSQL_BIN_DIR}/mysql does not exist"
325                        return 1
326                fi
327
328                # Construct mysql command line
329                echo "[client]">"${DSPAM_CRON_TMPFILE}"
330                MySQLQuotePass=""
331                if [ "${MySQLPass}" != "${MySQLPass/[#\\\"\$]/}" ]
332                then
333                        if [ "${DSPAM_MySQL_INT}" -lt "262160" ]
334                        then
335                                if [ "${VERBOSE}" = "true" ]
336                                then
337                                        echo "  You will most likely have an authentication issue/failure with the"
338                                        echo "  currently used MySQL DSPAM password and your current MySQL version."
339                                fi
340                        else
341                                MySQLQuotePass="'"
342                        fi
343                fi
344                echo -n "password=${MySQLQuotePass}">>"${DSPAM_CRON_TMPFILE}"
345                awk "BEGIN { IGNORECASE=1; ORS=\"\"; } \$1==\"MySQLPass${PROFILE}\" { gsub(\"^\\\"|\\\"$\", \"\", \$2); print \$2 >>\"${DSPAM_CRON_TMPFILE}\"; exit; }" "${DSPAM_CONFIGDIR}/dspam.conf" ${INCLUDE_DIRS[@]} 2>/dev/null
346                echo "${MySQLQuotePass}">>"${DSPAM_CRON_TMPFILE}"
347                DSPAM_MySQL_CMD="${MYSQL_BIN_DIR}/mysql"
348                DSPAM_MySQL_CMD="${DSPAM_MySQL_CMD} --defaults-file=${DSPAM_CRON_TMPFILE}"
349                DSPAM_MySQL_CMD="${DSPAM_MySQL_CMD} --silent"
350                DSPAM_MySQL_CMD="${DSPAM_MySQL_CMD} --user=${MySQLUser}"
351                [ -S "${MySQLServer}" ] &&
352                        DSPAM_MySQL_CMD="${DSPAM_MySQL_CMD} --socket=${MySQLServer}" ||
353                        DSPAM_MySQL_CMD="${DSPAM_MySQL_CMD} --host=${MySQLServer}"
354                [ -n "${MySQLPort}" ] &&
355                        DSPAM_MySQL_CMD="${DSPAM_MySQL_CMD} --port=${MySQLPort}"
356                [ "${MySQLCompress}" = "true" ] &&
357                        DSPAM_MySQL_CMD="${DSPAM_MySQL_CMD} --compress"
358
359                # Use updated purge script to reflect purge days passed to this script
360                if [ "${USE_SQL_AUTOUPDATE}" = "true" ]
361                then
362                        sed \
363                                -e "s:^\(SET[\t ]\{1,\}\@PurgeSignatures[\t ]*=[\t ]*\)[0-9]\{1,\}\(.*\)$:\1${SIGNATURE_AGE}\2:g" \
364                                -e "s:^\(SET[\t ]\{1,\}\@PurgeUnused[\t ]*=[\t ]*\)[0-9]\{1,\}\(.*\)$:\1${UNUSED_AGE}\2:g" \
365                                -e "s:^\(SET[\t ]\{1,\}\@PurgeHapaxes[\t ]*=[\t ]*\)[0-9]\{1,\}\(.*\)$:\1${HAPAXES_AGE}\2:g" \
366                                -e "s:^\(SET[\t ]\{1,\}\@PurgeHits1S[\t ]*=[\t ]*\)[0-9]\{1,\}\(.*\)$:\1${HITS1S_AGE}\2:g" \
367                                -e "s:^\(SET[\t ]\{1,\}\@PurgeHits1I[\t ]*=[\t ]*\)[0-9]\{1,\}\(.*\)$:\1${HITS1I_AGE}\2:g" \
368                                "${DSPAM_MySQL_PURGE_SQL}">"${DSPAM_SQL_TMPFILE}"
369                        [ -r "${DSPAM_SQL_TMPFILE}" ] && DSPAM_MySQL_PURGE_SQL="${DSPAM_SQL_TMPFILE}"
370                fi
371
372                # Run the MySQL purge script
373                ${DSPAM_MySQL_CMD} ${MySQLDb} < ${DSPAM_MySQL_PURGE_SQL} >/dev/null
374                _RC=${?}
375                if [ ${_RC} != 0 ]
376                then
377                        echo "MySQL purge script returned error code ${_RC}"
378                elif [ "${USE_SQL_OPTIMIZATION}" = "true" ]
379                then
380                        for foo in dspam_preferences dspam_signature_data dspam_stats dspam_token_data dspam_virtual_uids
381                        do
382                                ${DSPAM_MySQL_CMD} --batch -e "OPTIMIZE TABLE ${foo};" ${MySQLDb} >/dev/null 2>&1
383                                # ${DSPAM_MySQL_CMD} --batch -e "ANALYZE TABLE ${foo};" ${MySQLDb} >/dev/null 2>&1
384                        done
385                fi
386                echo "">"${DSPAM_CRON_TMPFILE}"
387                echo "">"${DSPAM_SQL_TMPFILE}"
388
389                return 0
390        fi
391}
392
393
394#
395# Function to clean DSPAM PostgreSQL data
396#
397clean_pgsql_drv() {
398        #
399        # PostgreSQL
400        #
401        [ "${VERBOSE}" = "true" ] && echo "Running PostgreSQL storage driver data cleanup"
402        if [ "${USE_SQL_PURGE}" = "true" ] && \
403                read_dspam_params PgSQLServer${PROFILE} PgSQLPort${PROFILE} PgSQLUser${PROFILE} PgSQLDb${PROFILE} && \
404                [ -n "${PgSQLServer}" -a -n "${PgSQLUser}" -a -n "${PgSQLDb}" ]
405        then
406                for foo in ${PGSQL_BIN_DIR} /usr/bin /usr/local/bin /usr/sbin /usr/local/sbin
407                do
408                        if [ -e "${foo}/psql" ]
409                        then
410                                PGSQL_BIN_DIR=${foo}
411                                break
412                        fi
413                done
414
415                DSPAM_PgSQL_PURGE_SQL=
416                DSPAM_PgSQL_PURGE_SQL_FILES="pgsql_pe-purge pgsql_purge-pe pgsql_purge"
417
418                #
419                # We first search for the purge scripts in the directory the user has
420                # told us to look for (command line option: --purgescriptdir
421                # Then we look in DSPAM configuration directory under ./config/ and then
422                # in the DSPAM configuration directory it self.
423                #
424                for foo in ${DSPAM_PURGE_SCRIPT_DIR} ${DSPAM_CONFIGDIR}/config ${DSPAM_CONFIGDIR} /usr/share/doc/libdspam7-drv-pgsql/sql /usr/share/dspam/sql /usr/local/share/examples/dspam/pgsql /usr/share/examples/dspam/pgsql
425                do
426                        for bar in ${DSPAM_PgSQL_PURGE_SQL_FILES}
427                        do
428                                if [ -z "${DSPAM_PgSQL_PURGE_SQL}" -a -f "${foo}/${bar}.sql" ]
429                                then
430                                        DSPAM_PgSQL_PURGE_SQL="${foo}/${bar}.sql"
431                                        if (grep -iq "VACUUM ANALYSE" "${DSPAM_PgSQL_PURGE_SQL}")
432                                        then
433                                                break
434                                        else
435                                                DSPAM_PgSQL_PURGE_SQL=""
436                                        fi
437                                fi
438                                if [ -z "${DSPAM_PgSQL_PURGE_SQL}" -a -f "${foo}/${bar/_//}.sql" ]
439                                then
440                                        DSPAM_PgSQL_PURGE_SQL="${foo}/${bar/_//}.sql"
441                                        if (grep -iq "VACUUM ANALYSE" "${DSPAM_PgSQL_PURGE_SQL}")
442                                        then
443                                                break
444                                        else
445                                                DSPAM_PgSQL_PURGE_SQL=""
446                                        fi
447                                fi
448                                if [ -z "${DSPAM_PgSQL_PURGE_SQL}" -a -f "${foo}/${bar/*_/}.sql" ]
449                                then
450                                        DSPAM_PgSQL_PURGE_SQL="${foo}/${bar/*_/}.sql"
451                                        if (grep -iq "VACUUM ANALYSE" "${DSPAM_PgSQL_PURGE_SQL}")
452                                        then
453                                                break
454                                        else
455                                                DSPAM_PgSQL_PURGE_SQL=""
456                                        fi
457                                fi
458                        done
459                        if [ -n "${DSPAM_PgSQL_PURGE_SQL}" ] && (grep -q "VACUUM ANALYSE" "${DSPAM_PgSQL_PURGE_SQL}")
460                        then
461                                break
462                        else
463                                DSPAM_PgSQL_PURGE_SQL=""
464                        fi
465                done
466
467                if [ -z "${DSPAM_PgSQL_PURGE_SQL}" ]
468                then
469                        [ "${VERBOSE}" = "true" ] && echo "  Can not run PostgreSQL purge script:"
470                        [ "${VERBOSE}" = "true" ] && echo "    None of the ${DSPAM_PgSQL_PURGE_SQL_FILES} SQL script(s) found"
471                        return 1
472                fi
473
474                if [ ! -r "${DSPAM_PgSQL_PURGE_SQL}" ]
475                then
476                        [ "${VERBOSE}" = "true" ] && echo "  Can not read PostgreSQL purge script:"
477                        [ "${VERBOSE}" = "true" ] && echo "    ${DSPAM_PgSQL_PURGE_SQL}"
478                        return 1
479                fi
480
481                if [ ! -e "${PGSQL_BIN_DIR}/psql" ]
482                then
483                        [ "${VERBOSE}" = "true" ] && echo "  Can not run PostgreSQL purge script:"
484                        [ "${VERBOSE}" = "true" ] && echo "    ${PGSQL_BIN_DIR}/psql does not exist"
485                        return 1
486                fi
487
488                # Construct psql command line
489                echo -n "*:*:${PgSQLDb}:${PgSQLUser}:">"${DSPAM_CRON_TMPFILE}"
490                awk "BEGIN { IGNORECASE=1; } \$1==\"PgSQLPass${PROFILE}\" { gsub(\"^\\\"|\\\"$\", \"\", \$2); print \$2 >>\"${DSPAM_CRON_TMPFILE}\"; exit; }" "${DSPAM_CONFIGDIR}/dspam.conf" ${INCLUDE_DIRS[@]} 2>/dev/null
491                DSPAM_PgSQL_CMD="${PGSQL_BIN_DIR}/psql -q -U ${PgSQLUser} -h ${PgSQLServer} -d ${PgSQLDb}"
492                [ -n "${PgSQLPort}" ] &&
493                        DSPAM_PgSQL_CMD="${DSPAM_PgSQL_CMD} -p ${PgSQLPort}"
494
495                # Use updated purge script to reflect purge days passed to this script
496                if [ "${USE_SQL_AUTOUPDATE}" = "true" ]
497                then
498                        if [ "${HITS1S_AGE}" -gt "${HITS1I_AGE}" ]
499                        then
500                                HITS1MAX=${HITS1S_AGE}
501                        else
502                                HITS1MAX=${HITS1I_AGE}
503                        fi
504                        sed \
505                                -e "/^DELETE FROM dspam_signature_data/,/COMMIT/{s:^\([\t ]*WHERE[\t ]\{1,\}created_on[\t ]\{1,\}<[\t ]\{1,\}CURRENT_DATE[\t ]\{1,\}\-[\t ]\{1,\}\)14\(.*\)$:\1${SIGNATURE_AGE}\2:g}" \
506                                -e "/^DELETE FROM dspam_token_data/,/COMMIT/{s:^\([\t ]*AND[\t ]\{1,\}last_hit[\t ]\{1,\}<[\t ]\{1,\}CURRENT_DATE[\t ]\{1,\}\-[\t ]\{1,\}\)[36]0\(.*\)$:\1${HAPAXES_AGE}\2:g}" \
507                                -e "/^DELETE FROM dspam_token_data/,/COMMIT/{s:^\([\t ]*AND[\t ]\{1,\}last_hit[\t ]\{1,\}<[\t ]\{1,\}CURRENT_DATE[\t ]\{1,\}\-[\t ]\{1,\}\)15\(.*\)$:\1${HITS1MAX}\2:g}" \
508                                -e "/^DELETE FROM dspam_token_data/,/COMMIT/{s:^\([\t ]*\(AND\|WHERE\)[\t ]\{1,\}last_hit[\t ]\{1,\}<[\t ]\{1,\}CURRENT_DATE[\t ]\{1,\}\-[\t ]\{1,\}\)90\(.*\)$:\1${UNUSED_AGE}\3:g}" \
509                                "${DSPAM_PgSQL_PURGE_SQL}">"${DSPAM_SQL_TMPFILE}"
510                        [ -r "${DSPAM_SQL_TMPFILE}" ] && DSPAM_PgSQL_PURGE_SQL="${DSPAM_SQL_TMPFILE}"
511                fi
512
513                # Run the PostgreSQL purge script
514                PGPASSFILE="${DSPAM_CRON_TMPFILE}" ${DSPAM_PgSQL_CMD} -f "${DSPAM_PgSQL_PURGE_SQL}" >/dev/null
515                _RC=${?}
516                if [ ${_RC} != 0 ]
517                then
518                        [ "${VERBOSE}" = "true" ] && echo "PostgreSQL purge script returned error code ${_RC}"
519                fi
520                echo "">"${DSPAM_CRON_TMPFILE}"
521                echo "">"${DSPAM_SQL_TMPFILE}"
522
523                return 0
524        fi
525}
526
527
528#
529# Function to clean DSPAM Hash data
530#
531clean_hash_drv() {
532        #
533        # Hash
534        #
535        local myrc=0
536        [ "${VERBOSE}" = "true" ] && echo "Running Hash storage driver data cleanup"
537        if [ -d "${DSPAM_HOMEDIR}/data" ]
538        then
539                if [ -e ${DSPAM_BIN_DIR}/cssclean ]
540                then
541                        if ! check_for_tool find
542                        then
543                                [ "${VERBOSE}" = "true" ] && echo "  Can not run cleanup without 'find' binary"
544                                myrc=1
545                        else
546                                find ${DSPAM_HOMEDIR}/data/ -maxdepth 4 -mindepth 1 -type f -name "*.css" | while read name
547                                do
548                                        ${DSPAM_BIN_DIR}/cssclean "${name}" 1>/dev/null 2>&1
549                                done
550                        fi
551                else
552                        [ "${VERBOSE}" = "true" ] && echo "  DSPAM cssclean binary not found!"
553                fi
554                if ! check_for_tool find
555                then
556                        [ "${VERBOSE}" = "true" ] && echo "  Can not purge old signatures without 'find' binary"
557                        myrc=1
558                else
559                        find ${DSPAM_HOMEDIR}/data/ -maxdepth 4 -mindepth 1 -type d -name "*.sig" | while read name
560                        do
561                                find "${name}" -maxdepth 1 -mindepth 1 -type f -mtime +${SIGNATURE_AGE} -name "*.sig" -exec rm "{}" ";"
562                        done
563                fi
564        else
565                myrc=1
566        fi
567        return ${myrc}
568}
569
570
571#
572# Function to clean DSPAM SQLite3 data
573#
574clean_sqlite3_drv() {
575        #
576        # SQLite3
577        #
578        [ "${VERBOSE}" = "true" ] && echo "Running SQLite v3 storage driver data cleanup"
579        if [ "${USE_SQL_PURGE}" = "true" ]
580        then
581                for foo in ${SQLITE3_BIN_DIR} /usr/bin /usr/local/bin /usr/sbin /usr/local/sbin
582                do
583                        if [ -e "${foo}/sqlite3" ]
584                        then
585                                SQLITE3_BIN_DIR=${foo}
586                                break
587                        fi
588                done
589
590                DSPAM_SQLite3_PURGE_SQL=""
591                DSPAM_SQLite3_PURGE_SQL_FILES="sqlite3_purge sqlite3_purge-3"
592
593                #
594                # We first search for the purge scripts in the directory the user has
595                # told us to look for (command line option: --purgescriptdir
596                # Then we look in DSPAM configuration directory under ./config/ and then
597                # in the DSPAM configuration directory it self.
598                #
599                for foo in ${DSPAM_PURGE_SCRIPT_DIR} ${DSPAM_CONFIGDIR}/config ${DSPAM_CONFIGDIR} /usr/share/doc/libdspam7-drv-sqlite3/sql /usr/share/dspam/sql /usr/share/dspam/clean /usr/local/share/examples/dspam/sqlite /usr/share/examples/dspam/sqlite
600                do
601                        for bar in ${DSPAM_SQLite3_PURGE_SQL_FILES}
602                        do
603                                if [ -z "${DSPAM_SQLite3_PURGE_SQL}" -a -f "${foo}/${bar}.sql" ]
604                                then
605                                        DSPAM_SQLite3_PURGE_SQL="${foo}/${bar}.sql"
606                                        if (grep -iq "julianday.*now" "${DSPAM_SQLite3_PURGE_SQL}")
607                                        then
608                                                break
609                                        else
610                                                DSPAM_SQLite3_PURGE_SQL=""
611                                        fi
612                                fi
613                                if [ -z "${DSPAM_SQLite3_PURGE_SQL}" -a  -f "${foo}/${bar/_//}.sql" ]
614                                then
615                                        DSPAM_SQLite3_PURGE_SQL="${foo}/${bar/_//}.sql"
616                                        if (grep -iq "julianday.*now" "${DSPAM_SQLite3_PURGE_SQL}")
617                                        then
618                                                break
619                                        else
620                                                DSPAM_SQLite3_PURGE_SQL=""
621                                        fi
622                                fi
623                                if [ -z "${DSPAM_SQLite3_PURGE_SQL}" -a -f "${foo}/${bar/*_/}.sql" ]
624                                then
625                                        DSPAM_SQLite3_PURGE_SQL="${foo}/${bar/*_/}.sql"
626                                        if (grep -iq "julianday.*now" "${DSPAM_SQLite3_PURGE_SQL}")
627                                        then
628                                                break
629                                        else
630                                                DSPAM_SQLite3_PURGE_SQL=""
631                                        fi
632                                fi
633                        done
634                        if [ -n "${DSPAM_SQLite3_PURGE_SQL}" ] && (grep -iq "julianday.*now" "${DSPAM_SQLite3_PURGE_SQL}")
635                        then
636                                break
637                        else
638                                DSPAM_SQLite3_PURGE_SQL=""
639                        fi
640                done
641
642                if [ -z "${DSPAM_SQLite3_PURGE_SQL}" ]
643                then
644                        [ "${VERBOSE}" = "true" ] && echo "  Can not run SQLite3 purge script:"
645                        [ "${VERBOSE}" = "true" ] && echo "    None of the ${DSPAM_SQLite3_PURGE_SQL_FILES} SQL script(s) found"
646                        return 1
647                fi
648
649                if [ ! -r "${DSPAM_SQLite3_PURGE_SQL}" ]
650                then
651                        [ "${VERBOSE}" = "true" ] && echo "  Can not read SQLite3 purge script:"
652                        [ "${VERBOSE}" = "true" ] && echo "    ${DSPAM_SQLite3_PURGE_SQL}"
653                        return 1
654                fi
655
656                if [ ! -e "${SQLITE3_BIN_DIR}/sqlite3" ]
657                then
658                        [ "${VERBOSE}" = "true" ] && echo "  Can not run SQLite3 purge script:"
659                        [ "${VERBOSE}" = "true" ] && echo "    ${SQLITE3_BIN_DIR}/sqlite3 does not exist"
660                        return 1
661                fi
662
663                # Use updated purge script to reflect purge days passed to this script
664                if [ "${USE_SQL_AUTOUPDATE}" = "true" ]
665                then
666                        if [ "${HITS1S_AGE}" -gt "${HITS1I_AGE}" ]
667                        then
668                                HITS1MAX=${HITS1S_AGE}
669                        else
670                                HITS1MAX=${HITS1I_AGE}
671                        fi
672                        sed \
673                                -e "s:^\([\t ]*and[\t ]\{1,\}(julianday('now')\-\)[36]0\()[\t ]\{1,\}>[\t ]\{1,\}julianday(last_hit).*\)$:\1${HAPAXES_AGE}\2:g" \
674                                -e "s:^\([\t ]*and[\t ]\{1,\}(julianday('now')\-\)15\()[\t ]\{1,\}>[\t ]\{1,\}julianday(last_hit).*\)$:\1${HITS1MAX}\2:g" \
675                                -e "s:^\([\t ]*and[\t ]\{1,\}(julianday('now')\-\)90\()[\t ]\{1,\}>[\t ]\{1,\}julianday(last_hit).*\)$:\1${UNUSED_AGE}\2:g" \
676                                -e "s:^\([\t ]*and[\t ]\{1,\}(julianday('now')\-\)14\()[\t ]\{1,\}>[\t ]\{1,\}julianday(created_on).*\)$:\1${SIGNATURE_AGE}\2:g" \
677                                "${DSPAM_SQLite3_PURGE_SQL}">"${DSPAM_SQL_TMPFILE}"
678                        [ -r "${DSPAM_SQL_TMPFILE}" ] && DSPAM_SQLite3_PURGE_SQL="${DSPAM_SQL_TMPFILE}"
679                fi
680
681                # Run the SQLite3 purge script and vacuum
682                find "${DSPAM_HOMEDIR}" -name "*.sdb" -print | while read name
683                do
684                        ${SQLITE3_BIN_DIR}/sqlite3 "${name}" < "${DSPAM_SQLite3_PURGE_SQL}" >/dev/null
685                        if [ "${USE_SQL_OPTIMIZATION}" = "true" ]
686                        then
687                                # Enable the next line if you don't vacuum in the purge script
688                                echo "vacuum;" | ${SQLITE3_BIN_DIR}/sqlite3 "${name}" >/dev/null
689                        fi
690                done 1>/dev/null 2>&1
691
692                echo "">"${DSPAM_SQL_TMPFILE}"
693
694                return 0
695        fi
696
697}
698
699
700#
701# Function to clean DSPAM SQLite < 3.0 data
702#
703clean_sqlite_drv() {
704        #
705        # SQLite
706        #
707        [ "${VERBOSE}" = "true" ] && echo "Running SQLite v2 storage driver data cleanup"
708        if [ "${USE_SQL_PURGE}" = "true" ]
709        then
710                for foo in ${SQLITE_BIN_DIR} /usr/bin /usr/local/bin /usr/sbin /usr/local/sbin
711                do
712                        if [ -e "${foo}/sqlite" ]
713                        then
714                                SQLITE_BIN_DIR=${foo}
715                                break
716                        fi
717                done
718
719                DSPAM_SQLite_PURGE_SQL=""
720                DSPAM_SQLite_PURGE_SQL_FILES="sqlite2_purge sqlite2_purge-2"
721
722                #
723                # We first search for the purge scripts in the directory the user has
724                # told us to look for (command line option: --purgescriptdir
725                # Then we look in DSPAM configuration directory under ./config/ and then
726                # in the DSPAM configuration directory it self.
727                #
728                for foo in ${DSPAM_PURGE_SCRIPT_DIR} ${DSPAM_CONFIGDIR}/config ${DSPAM_CONFIGDIR} /usr/share/doc/libdspam7-drv-sqlite/sql /usr/share/dspam/sql /usr/share/dspam/clean /usr/local/share/examples/dspam/sqlite /usr/share/examples/dspam/sqlite
729                do
730                        for bar in ${DSPAM_SQLite_PURGE_SQL_FILES}
731                        do
732                                if [ -z "${DSPAM_SQLite_PURGE_SQL}" -a -f "${foo}/${bar}.sql" ]
733                                then
734                                        DSPAM_SQLite_PURGE_SQL="${foo}/${bar}.sql"
735                                        if (grep -iq "date.*now.*date" "${DSPAM_SQLite_PURGE_SQL}")
736                                        then
737                                                break
738                                        else
739                                                DSPAM_SQLite_PURGE_SQL=""
740                                        fi
741                                fi
742                                if [ -z "${DSPAM_SQLite_PURGE_SQL}" -a -f "${foo}/${bar/_//}.sql" ]
743                                then
744                                        DSPAM_SQLite_PURGE_SQL="${foo}/${bar/_//}.sql"
745                                        if (grep -iq "date.*now.*date" "${DSPAM_SQLite_PURGE_SQL}")
746                                        then
747                                                break
748                                        else
749                                                DSPAM_SQLite_PURGE_SQL=""
750                                        fi
751                                fi
752                                if [ -z "${DSPAM_SQLite_PURGE_SQL}" -a -f "${foo}/${bar/*_/}.sql" ]
753                                then
754                                        DSPAM_SQLite_PURGE_SQL="${foo}/${bar/*_/}.sql"
755                                        if (grep -iq "date.*now.*date" "${DSPAM_SQLite_PURGE_SQL}")
756                                        then
757                                                break
758                                        else
759                                                DSPAM_SQLite_PURGE_SQL=""
760                                        fi
761                                fi
762                        done
763                        if [ -n "${DSPAM_SQLite_PURGE_SQL}" ] && (grep -iq "date.*now.*date" "${DSPAM_SQLite_PURGE_SQL}")
764                        then
765                                break
766                        else
767                                DSPAM_SQLite_PURGE_SQL=""
768                        fi
769                done
770
771                if [ -z "${DSPAM_SQLite_PURGE_SQL}" ]
772                then
773                        [ "${VERBOSE}" = "true" ] && echo "  Can not run SQLite purge script:"
774                        [ "${VERBOSE}" = "true" ] && echo "    None of the ${DSPAM_SQLite_PURGE_SQL_FILES} SQL script(s) found"
775                        return 1
776                fi
777
778                if [ ! -r "${DSPAM_SQLite_PURGE_SQL}" ]
779                then
780                        [ "${VERBOSE}" = "true" ] && echo "  Can not read SQLite purge script:"
781                        [ "${VERBOSE}" = "true" ] && echo "    ${DSPAM_SQLite_PURGE_SQL}"
782                        return 1
783                fi
784
785                if [ ! -e "${SQLITE_BIN_DIR}/sqlite" ]
786                then
787                        [ "${VERBOSE}" = "true" ] && echo "  Can not run SQLite purge script:"
788                        [ "${VERBOSE}" = "true" ] && echo "    ${SQLITE_BIN_DIR}/sqlite does not exist"
789                        return 1
790                fi
791
792                # Use updated purge script to reflect purge days passed to this script
793                if [ "${USE_SQL_AUTOUPDATE}" = "true" ]
794                then
795                        if [ "${HITS1S_AGE}" -gt "${HITS1I_AGE}" ]
796                        then
797                                HITS1MAX=${HITS1S_AGE}
798                        else
799                                HITS1MAX=${HITS1I_AGE}
800                        fi
801                        sed \
802                                -e "s:^\([\t ]*and[\t ]\{1,\}date('now')\-date(last_hit)[\t ]\{1,\}>[\t ]\{1,\}\)[36]0\(.*\)$:\1${HAPAXES_AGE}\2:g" \
803                                -e "s:^\([\t ]*and[\t ]\{1,\}date('now')\-date(last_hit)[\t ]\{1,\}>[\t ]\{1,\}\)15\(.*\)$:\1${HITS1MAX}\2:g" \
804                                -e "s:^\([\t ]*and[\t ]\{1,\}date('now')\-date(last_hit)[\t ]\{1,\}>[\t ]\{1,\}\)90\(.*\)$:\1${UNUSED_AGE}\2:g" \
805                                -e "s:^\([\t ]*and[\t ]\{1,\}date('now')\-date(created_on)[\t ]\{1,\}>[\t ]\{1,\}\)14\(.*\)$:\1${SIGNATURE_AGE}\2:g" \
806                                "${DSPAM_SQLite_PURGE_SQL}">"${DSPAM_SQL_TMPFILE}"
807                        [ -r "${DSPAM_SQL_TMPFILE}" ] && DSPAM_SQLite_PURGE_SQL="${DSPAM_SQL_TMPFILE}"
808                fi
809
810                # Run the SQLite purge script and vacuum
811                find "${DSPAM_HOMEDIR}" -name "*.sdb" -print | while read name
812                do
813                        ${SQLITE_BIN_DIR}/sqlite "${name}" < "${DSPAM_SQLite_PURGE_SQL}" >/dev/null
814                        if [ "${USE_SQL_OPTIMIZATION}" = "true" ]
815                        then
816                                # Enable the next line if you don't vacuum in the purge script
817                                echo "vacuum;" | ${SQLITE_BIN_DIR}/sqlite "${name}" >/dev/null
818                        fi
819                done 1>/dev/null 2>&1
820
821                echo "">"${DSPAM_SQL_TMPFILE}"
822
823                return 0
824        fi
825
826}
827
828
829#
830# Use a lock file to prevent multiple runs at the same time
831#
832DSPAM_CRON_LOCKFILE="/var/run/$(basename $0 .sh).pid"
833
834
835#
836# File used to save temporary data
837#
838DSPAM_CRON_TMPFILE="/tmp/.ds_$$_$RANDOM"
839DSPAM_SQL_TMPFILE="/tmp/.ds_$$_$RANDOM"
840
841
842#
843# Kill process if lockfile is older or equal 12 hours
844#
845if [ -f ${DSPAM_CRON_LOCKFILE} ]; then
846        DSPAM_REMOVE_CRON_LOCKFILE="YES"
847        for foo in $(cat ${DSPAM_CRON_LOCKFILE} 2>/dev/null); do
848                if [ -L "/proc/${foo}/exe" -a "$(readlink -f /proc/${foo}/exe)" = "$(readlink -f /proc/$$/exe)" ]; then
849                        DSPAM_REMOVE_CRON_LOCKFILE="NO"
850                fi
851        done
852        if [ "${DSPAM_REMOVE_CRON_LOCKFILE}" = "YES" ]; then
853                rm -f ${DSPAM_CRON_LOCKFILE} >/dev/null 2>&1
854        elif [ $(($(date +%s)-$(stat --printf="%X" ${DSPAM_CRON_LOCKFILE}))) -ge $((12*60*60)) ]; then
855                for foo in $(cat ${DSPAM_CRON_LOCKFILE} 2>/dev/null); do
856                        if [ -L "/proc/${foo}/exe" -a "$(readlink -f /proc/${foo}/exe)" = "$(readlink -f /proc/$$/exe)" ]; then
857                                kill -s KILL ${foo} >/dev/null 2>&1
858                        fi
859                done
860                DSPAM_REMOVE_CRON_LOCKFILE="YES"
861                for foo in $(cat ${DSPAM_CRON_LOCKFILE} 2>/dev/null); do
862                        if [ -L "/proc/${foo}/exe" -a "$(readlink -f /proc/${foo}/exe)" = "$(readlink -f /proc/$$/exe)" ]; then
863                                DSPAM_REMOVE_CRON_LOCKFILE="NO"
864                        fi
865                done
866                if [ "${DSPAM_REMOVE_CRON_LOCKFILE}" = "YES" ]; then
867                        rm -f ${DSPAM_CRON_LOCKFILE} >/dev/null 2>&1
868                fi
869        fi
870fi
871
872
873#
874# Acquire lock file and start processing
875#
876if ( set -o noclobber; echo "$$" > "${DSPAM_CRON_LOCKFILE}") 2> /dev/null; then
877
878        trap 'rm -f "${DSPAM_CRON_LOCKFILE}" "${DSPAM_CRON_TMPFILE}"; exit ${?}' INT TERM EXIT
879
880
881        #
882        # Create the temporary file
883        #
884        UMASK_OLD="$(umask)"
885        umask 077
886        [ -e "${DSPAM_CRON_TMPFILE}" ] && rm -f "${DSPAM_CRON_TMPFILE}" >/dev/null 2>&1
887        [ -e "${DSPAM_SQL_TMPFILE}" ] && rm -f "${DSPAM_SQL_TMPFILE}" >/dev/null 2>&1
888        touch "${DSPAM_CRON_TMPFILE}"
889        touch "${DSPAM_SQL_TMPFILE}"
890        umask "${UMASK_OLD}"
891
892
893        #
894        # Check for needed tools
895        #
896        if ! check_for_tool awk cut sed sort tr grep
897        then
898                # We don't have the tools needed to continue.
899                [ "${VERBOSE}" = "true" ] && echo "This script needs awk, cut, sed, sort, tr and grep to work."
900                exit 2
901        fi
902
903
904        #
905        # Try to read most of the configuration options from DSPAM
906        #
907        DSPAM_CONFIG_PARAMETERS=($(dspam --version 2>&1 | sed -n "s:^Configuration parameters\:[\t ]*\(.*\)$:\1:g;s:' '\-\-:\n--:g;s:^'::g;s:' '[a-zA-Z].*::gp"))
908        if [ -z "${DSPAM_CONFIG_PARAMETERS}" -o "${#DSPAM_CONFIG_PARAMETERS[@]}" -lt 3 ]
909        then
910                DSPAM_CONFIG_PARAMETERS=($(dspam --version 2>&1 | sed -n "s:^Configuration parameters\:[\t ]*\(.*\)$:\1:gp" | tr -s "' '" "'\n'" | sed -n "s:^'\(\-\-.*\)'[\t ]*$:\1:gp"))
911        fi
912        if [ -z "${DSPAM_CONFIG_PARAMETERS}" -o "${#DSPAM_CONFIG_PARAMETERS[@]}" -lt 3 ]
913        then
914                # Not good! dspam --version does not print out configuration parameters.
915                # Try getting the information by parsing the strings in the DSPAM binary.
916                if check_for_tool strings
917                then
918                        DSPAM_CONFIG_PARAMETERS=($(strings $(whereis dspam | awk '{print $2}') 2>&1 | sed -n "/'\-\-[^']*'[\t ]*'\-\-[^']*'/p" 2>/dev/null | sed -n "s:' '\-\-:\n--:g;s:^[\t ]*'::g;s:' '[a-zA-Z].*::gp"))
919                        if [ -z "${DSPAM_CONFIG_PARAMETERS}" -o "${#DSPAM_CONFIG_PARAMETERS[@]}" -lt 3 ]
920                        then
921                                DSPAM_CONFIG_PARAMETERS=($(strings $(whereis dspam | awk '{print $2}') 2>&1 | sed -n "/'\-\-[^']*'[\t ]*'\-\-[^']*'/p" 2>/dev/null | tr -s "' '" "'\n'" | sed -n "s:^'\(\-\-.*\)'[\t ]*$:\1:gp"))
922                        fi
923                fi
924        fi
925        if [ -n "${DSPAM_CONFIG_PARAMETERS}" ]
926        then
927                for foo in ${DSPAM_CONFIG_PARAMETERS[@]}
928                do
929                        case "${foo}" in
930                                --sysconfdir=*)
931                                        if [ -z "${DSPAM_CONFIGDIR}" -o ! -d "${DSPAM_CONFIGDIR}" ]
932                                        then
933                                                if [ -f "${foo#--sysconfdir=}/dspam.conf" ]
934                                                then
935                                                        DSPAM_CONFIGDIR="${foo#--sysconfdir=}"
936                                                fi
937                                        fi
938                                        ;;
939                                --with-dspam-home=*)
940                                        if [ -z "${DSPAM_HOMEDIR}" -o ! -d "${DSPAM_HOMEDIR}" ]
941                                        then
942                                                if [ -d "${foo#--with-dspam-home=}/data" ]
943                                                then
944                                                        DSPAM_HOMEDIR="${foo#--with-dspam-home=}"
945                                                fi
946                                        fi
947                                        ;;
948                                --prefix=*)
949                                        if [ -z "${DSPAM_BIN_DIR}" -o ! -d "${DSPAM_BIN_DIR}" -o ! -e "${DSPAM_BIN_DIR}/dspam" ]
950                                        then
951                                                if [ -e "${foo#--prefix=}/bin/dspam" ]
952                                                then
953                                                        DSPAM_BIN_DIR="${foo#--prefix=}"/bin
954                                                fi
955                                        fi
956                                        ;;
957                                --with-storage-driver=*)
958                                        STORAGE_DRIVERS="${foo#--with-storage-driver=}"
959                                        #STORAGE_DRIVERS=($(echo ${STORAGE_DRIVERS} | sed "s:,:\n:g" | sort -u))
960                                        STORAGE_DRIVERS=($(echo ${STORAGE_DRIVERS} | tr -s "," "\n" | sort -u))
961                                        ;;
962                        esac
963                done
964                [ "${VERBOSE}" = "true" -a -n "${STORAGE_DRIVERS}" ] && echo "Enabled drivers are: ${STORAGE_DRIVERS[@]}"
965        else
966                [ "${VERBOSE}" = "true" ] && echo "Warning: dspam --version does not print configuration parameters!"
967        fi
968
969
970        #
971        # Try to get DSPAM bin directory
972        #
973        if [ -z "${DSPAM_BIN_DIR}" ]
974        then
975                for foo in /usr/bin /usr/local/bin /usr/sbin /usr/local/sbin /opt/dspam /opt/dspam/bin
976                do
977                        if [ -e "${foo}/dspam" -a -e "${foo}/dspam_clean" ]
978                        then
979                                DSPAM_BIN_DIR=${foo}
980                                break
981                        fi
982                done
983                if [ -n "${DSPAM_BIN_DIR}" -a ! -e "${DSPAM_BIN_DIR}/dspam" ]
984                then
985                        [ "${VERBOSE}" = "true" ] && echo "DSPAM binary in directory ${DSPAM_BIN_DIR} not found!"
986                        exit 2
987                elif [ -n "${DSPAM_BIN_DIR}" -a ! -e "${DSPAM_BIN_DIR}/dspam_clean" ]
988                then
989                        [ "${VERBOSE}" = "true" ] && echo "DSPAM clean binary in directory ${DSPAM_BIN_DIR} not found!"
990                        exit 2
991                else
992                        [ "${VERBOSE}" = "true" ] && echo "DSPAM binary directory not found!"
993                        exit 2
994                fi
995        else
996                for foo in ${DSPAM_BIN_DIR}
997                do
998                        if [ -d "${foo}" -a -e "${foo}/dspam" ]
999                        then
1000                                DSPAM_BIN_DIR=${foo}
1001                                break
1002                        fi
1003                done
1004        fi
1005        if [ -z "${DSPAM_BIN_DIR}" -o ! -e "${DSPAM_BIN_DIR}/dspam" -o ! -e "${DSPAM_BIN_DIR}/dspam_clean" ]
1006        then
1007                [ "${VERBOSE}" = "true" ] && echo "Binary for dspam and/or dspam_clean not found! Can not continue without it."
1008                exit 2
1009        fi
1010
1011
1012        #
1013        # Try to get DSPAM config directory
1014        #
1015        if [ -z "${DSPAM_CONFIGDIR}" ]
1016        then
1017                for foo in /etc/mail/dspam /etc/dspam /etc /usr/local/etc/mail/dspam /usr/local/etc/dspam /usr/local/etc
1018                do
1019                        if [ -f "${foo}/dspam.conf" ]
1020                        then
1021                                DSPAM_CONFIGDIR=${foo}
1022                                break
1023                        fi
1024                done
1025        fi
1026        if [ -z "${DSPAM_CONFIGDIR}" -o ! -f "${DSPAM_CONFIGDIR}/dspam.conf" ]
1027        then
1028                [ "${VERBOSE}" = "true" ] && echo "dspam.conf not found! Can not continue without it."
1029                exit 2
1030        fi
1031        INCLUDE_DIRS=$(awk "BEGIN { IGNORECASE=1; } \$1==\"Include\" { print \$2 \"/*.conf\"; }" "${DSPAM_CONFIGDIR}/dspam.conf" 2>/dev/null)
1032
1033        #
1034        # Parameters
1035        #
1036        if [ -z "${PROFILE}" ]
1037        then
1038                if read_dspam_params DefaultProfile && [ -n "${DefaultProfile}" ]
1039                then
1040                        PROFILE=.${DefaultProfile}
1041                fi
1042        else
1043                PROFILE=.${PROFILE}
1044        fi
1045        [ -z "${LOGROTATE_AGE}" ] && LOGROTATE_AGE=31                           # System and user log
1046        [ -z "${USE_SQL_PURGE}" ] && USE_SQL_PURGE="true"                       # Run SQL purge scripts
1047        [ -z "${USE_SQL_OPTIMIZATION}" ] && USE_SQL_OPTIMIZATION="false"        # Do not run vacuum or optimize
1048        [ -z "${USE_SQL_AUTOUPDATE}" ] && USE_SQL_AUTOUPDATE="false"            # Do not automatically modify purge days in SQL clause
1049        [ -z "${PURGE_ALL_DRIVERS}" ] && PURGE_ALL_DRIVERS="false"              # Only purge active driver
1050        [ -z "${VERBOSE}" ] && VERBOSE="false"                                  # No additional output
1051        if [ -z "${SIGNATURE_AGE}" ]
1052        then
1053                if read_dspam_params PurgeSignatures && [ -n "${PurgeSignatures}" -a "${PurgeSignatures}" != "off" ]
1054                then
1055                        SIGNATURE_AGE=${PurgeSignatures}
1056                else
1057                        SIGNATURE_AGE=14                # Stale signatures
1058                fi
1059        fi
1060        if [ -z "${NEUTRAL_AGE}" ]
1061        then
1062                if read_dspam_params PurgeNeutral && [ -n "${PurgeNeutral}" -a "${PurgeNeutral}" != "off" ]
1063                then
1064                        NEUTRAL_AGE=${PurgeNeutral}
1065                else
1066                        NEUTRAL_AGE=90                  # Tokens with neutralish probabilities
1067                fi
1068        fi
1069        if [ -z "${UNUSED_AGE}" ]
1070        then
1071                if read_dspam_params PurgeUnused && [ -n "${PurgeUnused}" -a "${PurgeUnused}" != "off" ]
1072                then
1073                        UNUSED_AGE=${PurgeUnused}
1074                else
1075                        UNUSED_AGE=90                   # Unused tokens
1076                fi
1077        fi
1078        if [ -z "${HAPAXES_AGE}" ]
1079        then
1080                if read_dspam_params PurgeHapaxes && [ -n "${PurgeHapaxes}" -a "${PurgeHapaxes}" != "off" ]
1081                then
1082                        HAPAXES_AGE=${PurgeHapaxes}
1083                else
1084                        HAPAXES_AGE=30                  # Tokens with less than 5 hits (hapaxes)
1085                fi
1086        fi
1087        if [ -z "${HITS1S_AGE}" ]
1088        then
1089                if read_dspam_params PurgeHits1S && [ -n "${PurgeHits1S}" -a "${PurgeHits1S}" != "off" ]
1090                then
1091                        HITS1S_AGE=${PurgeHits1S}
1092                else
1093                        HITS1S_AGE=15                   # Tokens with only 1 spam hit
1094                fi
1095        fi
1096        if [ -z "${HITS1I_AGE}" ]
1097        then
1098                if read_dspam_params PurgeHits1I && [ -n "${PurgeHits1I}" -a "${PurgeHits1I}" != "off" ]
1099                then
1100                        HITS1I_AGE=${PurgeHits1I}
1101                else
1102                        HITS1I_AGE=15                   # Tokens with only 1 innocent hit
1103                fi
1104        fi
1105
1106
1107        #
1108        # Try to get DSPAM data home directory
1109        #
1110        if read_dspam_params Home && [ -d "${Home}" ]
1111        then
1112                DSPAM_HOMEDIR=${Home}
1113        else
1114                # Something is wrong in dspam.conf! Check if /var/spool/dspam exists instead.
1115                if [ -z "${DSPAM_HOMEDIR}" -a -d /var/spool/dspam ]
1116                then
1117                        DSPAM_HOMEDIR="/var/spool/dspam"
1118                fi
1119        fi
1120        if [ ! -d "${DSPAM_HOMEDIR}" -o -z "${DSPAM_HOMEDIR}" ]
1121        then
1122                [ "${VERBOSE}" = "true" ] && echo "Home directory not found! Please fix your dspam.conf."
1123                exit 2
1124        fi
1125
1126
1127        #
1128        # System and user log purging
1129        #
1130        if [ ! -e "${DSPAM_BIN_DIR}/dspam_logrotate" ]
1131        then
1132                [ "${VERBOSE}" = "true" ] && echo "dspam_logrotate not found! Not purging system and user logs."
1133        else
1134                [ "${VERBOSE}" = "true" ] && echo "Running dspam_logrotate in the background"
1135                ${DSPAM_BIN_DIR}/dspam_logrotate -a ${LOGROTATE_AGE} -d "${DSPAM_HOMEDIR}" >/dev/null &
1136        fi
1137
1138
1139        #
1140        # Don't purge signatures with dspam_clean if we purged them with SQL
1141        #
1142        RUN_FULL_DSPAM_CLEAN="NO"
1143
1144
1145        #
1146        # Currently active storage driver
1147        #
1148        ACTIVE_DRIVER=""
1149        if [ ${#STORAGE_DRIVERS[@]} -eq 1 ]
1150        then
1151                ACTIVE_DRIVER="${STORAGE_DRIVERS[0]}"
1152        else
1153                read_dspam_params StorageDriver
1154                if [ -n "${StorageDriver}" ]
1155                then
1156                        for foo in hash_drv mysql_drv pgsql_drv sqlite3_drv sqlite_drv
1157                        do
1158                                if ( echo "${StorageDriver}" | grep -q "${foo}" )
1159                                then
1160                                        ACTIVE_DRIVER="${foo}"
1161                                        break
1162                                fi
1163                        done
1164                fi
1165        fi
1166        [ "${VERBOSE}" = "true" -a -n "${ACTIVE_DRIVER}" ] && echo "Active driver is: ${ACTIVE_DRIVER}"
1167
1168
1169        #
1170        # Which drivers to process/purge
1171        #
1172        if [ "${PURGE_ALL_DRIVERS}" = "false" -a -n "${ACTIVE_DRIVER}" ]
1173        then
1174                DRIVERS_TO_PROCESS=${ACTIVE_DRIVER}
1175        elif [ ${#STORAGE_DRIVERS[@]} -gt 0 ]
1176        then
1177                DRIVERS_TO_PROCESS=${STORAGE_DRIVERS[@]}
1178        else
1179                [ "${VERBOSE}" = "true" ] && echo "Warning: Could not get a list of supported storage drivers!"
1180                [ "${VERBOSE}" = "true" ] && echo "Warning: Could not determine the currently active storage driver!"
1181                DRIVERS_TO_PROCESS=""
1182                RUN_FULL_DSPAM_CLEAN="YES"
1183        fi
1184
1185
1186        #
1187        # Process selected storage drivers
1188        #
1189        for foo in ${DRIVERS_TO_PROCESS}
1190        do
1191                case "${foo}" in
1192                        hash_drv)
1193                                clean_hash_drv
1194                                ;;
1195                        mysql_drv)
1196                                clean_mysql_drv
1197                                ;;
1198                        pgsql_drv)
1199                                clean_pgsql_drv
1200                                ;;
1201                        sqlite3_drv)
1202                                clean_sqlite3_drv
1203                                ;;
1204                        sqlite_drv)
1205                                clean_sqlite_drv
1206                                ;;
1207                        *)
1208                                RUN_FULL_DSPAM_CLEAN="YES"
1209                                echo "Unknown storage ${foo} driver"
1210                                ;;
1211                esac
1212        done
1213
1214
1215        #
1216        # Run the dspam_clean command
1217        #
1218        # NOTE: The hash driver does not work with dspam_clean. So under no
1219        #       circumstances do run dspam_clean if the hash driver is the
1220        #       ONLY or the currently used driver!
1221        if [ -n "${ACTIVE_DRIVER}" ]
1222        then
1223                if [ "${ACTIVE_DRIVER}" = "hash_drv" ]
1224                then
1225                        [ "${VERBOSE}" = "true" ] && echo "Hash storage driver detected (not running dspam_clean)"
1226                else
1227                        [ "${USE_SQL_PURGE}" = "false" ] && RUN_FULL_DSPAM_CLEAN="YES"
1228                        run_dspam_clean ${RUN_FULL_DSPAM_CLEAN}
1229                fi
1230        else
1231                # Storage driver probably statically linked. Not running dspam_clean
1232                # because of potential risk that the storage driver used is the Hash
1233                # driver.
1234                [ "${VERBOSE}" = "true" ] && echo "Could not detect current storage driver (not running dspam_clean)"
1235        fi
1236
1237
1238        #
1239        # Release lock and delete temp file
1240        #
1241        rm -f "${DSPAM_CRON_LOCKFILE}"
1242        rm -f "${DSPAM_CRON_TMPFILE}"
1243        rm -f "${DSPAM_SQL_TMPFILE}"
1244        trap - INT TERM EXIT
1245fi
Note: See TracBrowser for help on using the repository browser.