#! /bin/sh

# daily DCC cron job

# This script should be run daily or more often when there is a shortage
#   of disk space to run dbclean to discard and compress old checksums.
#   It also discards old DCC client log files.

#.  By default it is installed in /usr/libexec/dcc.  Instead of being copied
#   to a directory such as /etc/cron/daily on some systems, a symbolic link
#   should be used.

# Copyright (c) 2006 by Rhyolite Software, LLC
#
# This agreement is not applicable to any entity which sells anti-spam
# solutions to others or provides an anti-spam solution as part of a
# security solution sold to other entities, or to a private network
# which employs the DCC or uses data provided by operation of the DCC
# but does not provide corresponding data to other users.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# Parties not eligible to receive a license under this agreement can
# obtain a commercial license to use DCC and permission to use
# U.S. Patent 6,330,590 by contacting Commtouch at http://www.commtouch.com/
# or by email to nospam@commtouch.com.
#
# A commercial license would be for Distributed Checksum and Reputation
# Clearinghouse software.  That software includes additional features.  This
# free license for Distributed ChecksumClearinghouse Software does not in any
# way grant permision to use Distributed Checksum and Reputation Clearinghouse
# software
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#	Rhyolite Software DCC 1.3.45-1.60 $Revision$
#	Generated automatically from cron-dccd.in by configure.

exec 1>&2 </dev/null

LOGGER_TAG=cron-dccd
DCC_LOGGER="logger -s -p ${DCC_ERROR_LOG_FACILITY-mail.err} -t  ${LOGGER_TAG-DCC}"
DCC_HOMEDIR=/etc/dcc
DEBUG=
QUIET=-q
# check the args once to get the home directory
while getopts ":xh:a:" c; do
    case $c in
	x) set -x; DEBUG=-x; QUIET=;;
	h) DCC_HOMEDIR="$OPTARG";;
	*) ;;
    esac
done
. $DCC_HOMEDIR/dcc_conf
# deal with bash reserved $UID and old versions of dcc_conf
if test 0"$DCC_CONF_VERSION" -lt 2 -a -z "$DCCUID" -a -n "$UID"; then
    DCCUID="$UID"
fi

USAGE="`basename $0`: [-x] [-h homedir] [-a args]"
OPTIND=1
while getopts "xh:a:" c; do
    case $c in
	x) ;;
	h) ;;
	a) DBCLEAN_ARGS="$DBCLEAN_ARGS $OPTARG";;
	*) eval $DCC_LOGGER "$USAGE"; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 0; then
    eval $DCC_LOGGER "$USAGE"
    exit 1
fi

DCCM_GETOPTS="VdbxANQWG:h:p:m:w:U:a:t:g:S:l:R:r:s:o:j:B:X:L:"
DCCD_GETOPTS="64dVbfFQi:n:h:a:I:q:G:t:K:T:u:C:L:R:"
DBCLEAN_GETOPTS="64dDfFNRPSVqi:a:h:G:s:e:E:t:L:"


DBCLEAN_ARGS="$QUIET -h $DCC_HOMEDIR $DCC_LOG_ARGS $DBCLEAN_ARGS"

# remove -e, -E, and -t from args for `dbclean -Gon`
set -f
GREY_DBCLEAN_ARGS=
OPTIND=1
while getopts "$DBCLEAN_GETOPTS" c $DBCLEAN_ARGS; do
    case $c in
	[eEt:?]) ;;
	*) GREY_DBCLEAN_ARGS="$GREY_DBCLEAN_ARGS -$c $OPTARG";;
    esac
done

# find addresses dccd is using
ADDR=
OPTIND=1
while getopts "$DCCD_GETOPTS" c $DCCD_ARGS; do
    case $c in
	[46a]) ADDR="$ADDR -$c $OPTARG";;
	*) ;;
    esac
done
DBCLEAN_ARGS="$ADDR $DBCLEAN_ARGS"

ADDR=
OPTIND=1
while getopts "$DCCD_GETOPTS" c $GREY_DCCD_ARGS; do
    case $c in
	[46a]) ADDR="$ADDR -$c $OPTARG";;
	*) ;;
    esac
done
GREY_DBCLEAN_ARGS="$ADDR $GREY_DBCLEAN_ARGS"
set +f


# make the paths absolute and trim the per day/hour/minute business
DCCM_LOGDIR=`echo $DCCM_LOGDIR						\
	| sed -e "s@^[DHM]?@@" -e "s@^[^/]@$DCC_HOMEDIR/&@"`
DCCM_USERDIRS=`echo $DCCM_USERDIRS					\
	| sed -e "s@^[^/]@$DCC_HOMEDIR/&@"`
DCCIFD_LOGDIR=`echo $DCCIFD_LOGDIR					\
	| sed -e "s@^[DHM]?@@" -e "s@^[^/]@$DCC_HOMEDIR/&@"`
DCCIFD_USERDIRS=`echo $DCCIFD_USERDIRS					\
	| sed -e "s@^[^/]@$DCC_HOMEDIR/&@"`
LOGDIRS=
if test -n "$DCCM_LOGDIR" -a -d "$DCCM_LOGDIR"; then
    LOGDIRS="$DCCM_LOGDIR"
fi
if test  "$LOGDIRS" != "$DCCIFD_LOGDIR" -a -n "$DCCIFD_LOGDIR"		\
	-a -d "$DCCIFD_LOGDIR"; then
    LOGDIRS="$LOGDIRS $DCCIFD_LOGDIR"
fi
USERDIRS=
if test -n "$DCCM_USERDIRS" -a -d "$DCCM_USERDIRS"; then
    USERDIRS="$DCCM_USERDIRS"
fi
if test "$USERDIRS" != "$DCCIFD_USERDIRS" -a -n "$DCCIFD_USERDIRS"	\
	-a -d "$DCCIFD_USERDIRS"; then
    USERDIRS="$USERDIRS $DCCIFD_USERDIRS"
fi

# Removed old dccm and dccifd log files.
if test -n "$DBCLEAN_LOGDAYS"; then
    if test -n "$LOGDIRS"; then
	find $LOGDIRS -follow						\
		\( \( -name 'msg.*' -mtime +$DBCLEAN_LOGDAYS \)		\
		    -o \( -name 'tmp.*' -mtime +2 \) \) -print		\
	    | /usr/bin/xargs -r /bin/rm -f
	find $LOGDIRS -follow -depth -name '[0-9]*' -type d -mtime +1 -print \
	    | /usr/bin/xargs -r /bin/rmdir 2>/dev/null
    fi
    if test -n "$USERDIRS"; then
	find $USERDIRS -follow						\
		\( \( -name 'msg.*' -mtime +$DBCLEAN_LOGDAYS \)		\
		    -o \( -name 'tmp.*' -mtime +2 \) \) -print		\
	    | /usr/bin/xargs -r /bin/rm -f
    fi
fi

# Notify users about new log files.
#   The file $DCC_LIBEXEC/webuser-notify must be a script that will send
#   a suitable message.  See the example in the cgi-bin directory.
if test -n "$USERDIRS" -a -x $DCC_LIBEXEC/webuser-notify; then
    for DIR in $USERDIRS; do
	MARKER=$DIR/notify.marker
	touch $MARKER.new
	if test -r $MARKER; then
	    NEWER="-newer $MARKER"
	else
	    NEWER=
	fi
	# Find usernames with a pending message or with a new
	#	    but not newer than new log files
	#	Avoid newer than new files to ensure we generate at most
	#	    one notification per log file.
	find $DIR -follow \( -name notify.pending			\
			    -o \( -name 'msg.*' $NEWER \) \)		\
		! -newer $MARKER.new -print				\
	    | sed -n -e 's@.*/\([^/]*/[^/]*\)/log/msg\.[^/]*$@\1@p'	\
		-e 's@.*/\([^/]*/[^/]*\)/notify.pending$@\1@p'		\
	    | LC_ALL=C sort -u						\
	    | $DCC_LIBEXEC/webuser-notify $DEBUG -d "$DIR"

	mv -f $MARKER.new $MARKER
    done
fi


# trim the greylist database
case X"$GREY_ENABLE" in
    [oO][nN])
	GREY_ENABLE=on
	;;
    X)
	if test -n "$GREY_CLIENT_ARGS"; then
	    GREY_ENABLE=on
	fi
	;;
esac
if test -n "$GREY_SRVR_ID" -a "$GREY_ENABLE" = on; then
    $DCC_LIBEXEC/dbclean -Gon -i $GREY_SRVR_ID $GREY_DBCLEAN_ARGS
fi


# Delete old checksums from the dccd database if it seems dccd can run.
#	For historical reasons, SRVR_ID set and DCCD_ENABLE nul
#	turns on dccd
case "$DCCD_ENABLE" in
    [oO][fF][fF]) DCCD_ENABLE=off;;
    *) DCCD_ENABLE=on;;
esac
if test -n "$SRVR_ID" -a "$DCCD_ENABLE" = on; then
    $DCC_LIBEXEC/dbclean -i $SRVR_ID $SADDR $DBCLEAN_ARGS
fi


# encourage local DCC clients to switch back
/usr/bin/cdcc rtt >/dev/null 2>&1
