#! /bin/sh

# start dccifd

# This script assumes that it is being run by root or the DCC user, probably
# while the system is starting.  See the misc/rcDCC script.

# 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.25 $Revision$
#	Generated automatically from start-dccifd.in by configure.

exec 1>&2 </dev/null

LOGGER_TAG=start-dccifd
DCC_LOGGER="logger -s -p ${DCC_ERROR_LOG_FACILITY-mail.err} -t  ${LOGGER_TAG-DCC}"
DCC_HOMEDIR=/etc/dcc
AUTOSTART=
# check the args once to get the home directory
while getopts ":xAh:r:a:" c; do
    case $c in
	x) set -x;;
	A) AUTOSTART=y;;
	h) DCC_HOMEDIR="$OPTARG";;
	*) ;;
    esac
done
if test ! -s $DCC_HOMEDIR/dcc_conf; then
    eval $DCC_LOGGER "cannot find $DCC_HOMEDIR/dcc_conf"
fi
. $DCC_HOMEDIR/dcc_conf

if test "$DCC_HOMEDIR" != /etc/dcc; then
    DCCIFD_ARGS="-h$DCC_HOMEDIR $DCCIFD_ARGS"
fi

# convert old `dccifd -G` to `dccifd -Gon`
DCCIFD_ARGS=`echo "$DCCIFD_ARGS"					\
	| sed -e 's/-\([64VdbxANQW]\{1,\}\)G/-\1 -G/g'			\
	    -e 's/-G\([^- ]\)/-G -\1/g'					\
	    -e 's/-G/-Gon/g'  -e 's/-G[	 ]*on[	 ]*on/-Gon/g'`
if test -n "$GREY_CLIENT_ARGS"; then
    if test X`expr "X$GREY_CLIENT_ARGS" : '.*\(-G\)'` = X; then
	GREY_CLIENT_ARGS="-G$GREY_CLIENT_ARGS"
    fi
    DCCIFD_ARGS="$GREY_CLIENT_ARGS $DCCIFD_ARGS"
fi

if test -n "$DCCIFD_USERDIRS"; then
    DCCIFD_ARGS="-U$DCCIFD_USERDIRS $DCCIFD_ARGS"
fi
if test -n "$DCCIFD_WHITECLNT"; then
    DCCIFD_ARGS="-w$DCCIFD_WHITECLNT $DCCIFD_ARGS"
fi
if test -n "$DCCIFD_LOGDIR"; then
    DCCIFD_ARGS="-l$DCCIFD_LOGDIR $DCCIFD_ARGS"
fi

CKS="$REP_ARGS"
if test -n "$DCCIFD_LOG_AT$DCCIFD_REJECT_AT"; then
    for CK in ${DCCIFD_CKSUMS:=CMN} $DCCIFD_XTRA_CKSUMS; do
	CKS="$CKS -t$CK,$DCCIFD_LOG_AT,$DCCIFD_REJECT_AT"
    done
fi

DCCIFD_ARGS="$CKS $DNSBL_ARGS $XFLTR_ARGS $DCC_LOG_ARGS $DCCIFD_ARGS"



USAGE="`basename $0`: [-x] [-h homedir] [-r rundir] [-a args]"
OPTIND=1
while getopts "xAh:r:a:" c; do
    case $c in
	x) ;;
	A) ;;
	h) ;;
	r) DCC_RUNDIR="$OPTARG";;
	a) DCCIFD_ARGS="$DCCIFD_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

if test -n "$DCC_RUNDIR" -a "$DCC_RUNDIR" != /var/run/dcc; then
    DCCIFD_ARGS="-R$DCC_RUNDIR $DCCIFD_ARGS"
fi


# stop an existing dccifd
#   signal the process group because on Linux systems it does not work
#   to signal the main process.  See `man sigwait` on a Linux system.
PID=`cat $DCC_RUNDIR/dccifd.pid 2>/dev/null`
if test -n "$PID"; then
    if test -n "$AUTOSTART"; then
	if kill -0 "$PID" 2>/dev/null; then
	    eval $DCC_LOGGER "dccifd already running"
	    exit 0
	fi
    else
	if `kill -15 $PID 2>/dev/null`; then
	    CNT=0
	    while test $CNT -lt 6; do
		if `/bin/kill -15 $PID 2>/dev/null`; then
		    if test $CNT -eq 0; then
			echo "    stopping dccifd PID $PID " | tr -d '\012'
		    else
			echo "." | tr -d '\012'
		    fi
		    sleep 1
		    CNT=`expr $CNT + 1`
		else
		    break
		fi
	    done
	    /bin/kill -9 $PID 2>/dev/null
	    if test $CNT -ne 0; then
		echo
	    fi
	fi
    fi
fi


if test -z "$AUTOSTART"; then
    case "$DCCIFD_ENABLE" in
	[oO][nN]);;
	*) exit 0 ;;
    esac
fi

if test ! -x $DCC_LIBEXEC/dccifd; then
    if test -z "$AUTOSTART"; then
	eval $DCC_LOGGER "cannot start dccifd because it has not been installed"
    fi
    exit 0
fi

if test ! -d $DCC_RUNDIR; then
    mkdir $DCC_RUNDIR
fi

# first try to use `su -` to set limits
#   fall back to -I for systems where su requires a valid shell in
#	/etc/passwd but where the dcc user does not have one
if test -n "$DCCUID" -a X"$DCCUID" != X"$USER"; then
    if test -d "$DCC_HOMEDIR"; then
	chown $DCCUID "$DCC_HOMEDIR"
    fi
    if test -d "$DCC_RUNDIR"; then
	chown $DCCUID "$DCC_RUNDIR"
    fi
    if su - $DCCUID -c date >/dev/null 2>&1; then
	su - $DCCUID -c "$DCC_LIBEXEC/dccifd $DCCIFD_ARGS"
	exit
    fi
    DCCIFD_ARGS="-I$DCCUID $DCCIFD_ARGS"
fi
eval $DCC_LIBEXEC/dccifd $DCCIFD_ARGS
