#!/bin/sh
#
# ufdbDLstatus
#
# Retrieve the status of previous downloads (today and yestesday)
#
# $Id: ufdbDLstatus.in,v 1.1 2018/12/30 20:06:28 root Exp root $

if [ "${1:-notset}" = "-v" ] 
then
   verbose=yes
else
   verbose=no
fi

# begin of user settings

# This script requires DOWNLOAD_USER and DOWNLOAD_PASSWORD in the same manner as ufdbUpdate
# Set DOWNLOAD_USER and DOWNLOAD_PASSWORD and
# other variables in either /etc/sysconfig/ufdbguard, /etc/conf.d/ufdb,
# /etc/default/ufdbguard, /usr/pkg/etc/ufdbguard, /etc/urlfilterdb/ufdbguard or
# /usr/local/etc/ufdbguard.
DOWNLOAD_USER=""                        # fill in the user and password
DOWNLOAD_PASSWORD=""

PROXY_USER=""                           # if the download must go via an authenticating proxy
PROXY_PASSWORD=""

CUSTOMER_ID="0000-0000-0000-0000"	# not yet required
WGET_COMMAND="/usr/pkg/bin/wget"                     # or a full path name to wget

# end of user settings.
# DO NOT EDIT ANYTHING BELOW THIS LINE.  ########################################

prefix=/usr/pkg
exec_prefix=${prefix}
cfgdir=/usr/pkg/etc
BINDIR="/usr/pkg/bin"
UFDB_VERSION="1.35.5"
UPDATE_HOST="updates.urlfilterdb.com"

WGET_OPTIONS="--tries 4 --waitretry=8 --timeout=60 --user-agent=ufdbDLstatus-$UFDB_VERSION --no-check-certificate"
# --limit-rate is removed since the implementation in wget has a severe bug
# --no-check-certificate is used since on some older systems wget does not have a certificate database


reporterror () {
   MSG="ufdbDLstatus failed: $*"
   if [ "$ADMIN_MAIL" != "" ]
   then
      if [ -x /bin/mailx  -o  -x /usr/bin/mailx ]
      then
         echo "$MSG" | mailx -s "ufdbDLstatus error  *****"  $ADMIN_MAIL
      elif [ -x /usr/sbin/sendmail ]
      then
         ( echo "Subject: ufdbDLstatus error  *****" ; echo ; echo "$MSG" ) | /usr/sbin/sendmail $ADMIN_MAIL
      else
         echo "could not send email to $ADMIN_MAIL to report about the following ufdbDLstatus error  *****"
	 echo "$MSG"
      fi
   fi
}


if [ $verbose = yes ]
then
   echo "ufdbDLstatus $UFDB_VERSION"
fi

# Gentoo uses /etc/conf.d
if [ -f /etc/conf.d/ufdb ]
then
   if [ $verbose = yes ]
   then
      echo "sourcing /etc/conf.d/ufdb ..."
   fi
   . /etc/conf.d/ufdb
fi
# Redhat, CentOS, Fedora use /etc/sysconfig
if [ -f /etc/sysconfig/ufdbguard ]
then
   if [ $verbose = yes ]
   then
      echo "sourcing /etc/sysconfig/ufdbguard ..."
   fi
   . /etc/sysconfig/ufdbguard
fi 
# Ubuntu and Debian uses /etc/default
if [ -f /etc/default/ufdbguard ]
then
   if [ $verbose = yes ]
   then
      echo "sourcing /etc/default/ufdbguard ..."
   fi
   . /etc/default/ufdbguard
fi
# NetBSD
if [ -f /usr/pkg/etc/ufdbguard ]
then
   if [ $verbose = yes ]
   then
      echo "sourcing /usr/pkg/etc/ufdbguard ..."
   fi
   . /usr/pkg/etc/ufdbguard
fi
# FreeBSD
if [ -f /usr/local/etc/ufdbguard ]
then
   if [ $verbose = yes ]
   then
      echo "sourcing /usr/local/etc/ufdbguard ..."
   fi
   . /usr/local/etc/ufdbguard
fi
# all others
if [ -f /etc/urlfilterdb/ufdbguard ]
then
   if [ $verbose = yes ]
   then
      echo "sourcing /etc/urlfilterdb/ufdbguard ..."
   fi
   . /etc/urlfilterdb/ufdbguard
fi

PATH="/usr/xpg4/bin:/bin:/usr/bin:$BINDIR:/usr/sbin:$PATH"
export PATH

if [ "$TMPDIR" = "" ]
then
   TMPDIR="/tmp"
fi

umask 022

if [ "$DOWNLOAD_USER" = "" ]
then
   echo "The download user is not specified."
   echo "On most systems DOWNLOAD_USER should be set in /etc/sysconfig/ufdbguard."
   echo "Please contact support@urlfilterdb.com to get your (trial)"
   echo "username and password to download updates of URLfilterDB."
   echo "For trial licenses:"
   echo "During the evalution period you may use the demo username/password."
   reporterror "download user name not set. "
   logger -p $SYSLOG_FACILITY.error -t ufdbDLstatus \
          "download user name not set. contact support@urlfilterdb.com"
   exit 11
fi


if [ $verbose = no ]
then
   WGET_OPTIONS="-q $WGET_OPTIONS"
else
   WGET_OPTIONS="--progress=dot:mega $WGET_OPTIONS"
fi

if [ ! -x $WGET_COMMAND ]
then
   echo "WGET_COMMAND is $WGET_COMMAND but is not an executable."
   reporterror "WGET_COMMAND is $WGET_COMMAND but is not an executable."
   logger -p $SYSLOG_FACILITY.error -t ufdbDLstatus \
        "WGET_COMMAND is $WGET_COMMAND but is not an executable."
   if [ $verbose = yes ]
   then
      echo "Make sure that \"wget\" is installed and that PATH is set correctly."
      echo "Rerun the configure command and \"make install\""
   fi
   exit 11
fi

if [ "$http_proxy" != ""  -a  "$https_proxy" = "" ]
then
   echo "ERROR: http_proxy is set but https_proxy is not."
   exit 14
fi

if [ $verbose = yes ]
then
   if [ "$https_proxy" = "" ]
   then
      echo "https_proxy is not set: no proxy is used for downloads."
   else
      if [ "$PROXY_USER" != ""  -a  "$PROXY_PASSWORD" != "" ]
      then
	 echo "Warning: wget uses https_proxy=$https_proxy and PROXY_USER/PROXY_PASSWORD are unset."
      else
	 echo "wget uses https_proxy=$https_proxy and PROXY_USER=$PROXY_USER."
      fi
   fi
fi

if [ "$PROXY_USER" != ""  -a  "$PROXY_PASSWORD" != "" ]
then
   WGET_OPTIONS="$WGET_OPTIONS  --proxy-user=$PROXY_USER  --proxy-passwd=$PROXY_PASSWORD "
fi

WGET_OPTIONS="$WGET_OPTIONS  --http-user=$DOWNLOAD_USER  --http-passwd=$DOWNLOAD_PASSWORD "


if [ $verbose = yes ]
then
   echo "The URL database download status will be requested from \"$UPDATE_HOST\"."
   echo "The username $DOWNLOAD_USER will be used for authentication."
   echo "   $WGET_COMMAND -O - https://$DOWNLOAD_USER:$DOWNLOAD_PASSWORD@$URL_DIR/$GUARD_TYPE/$DBFILE"
fi

# there are very old wget's which need the username/password in the URL :-(
$WGET_COMMAND -O - $WGET_OPTIONS  --header="X-Hostname: `hostname 2>&1`"  \
   "https://$DOWNLOAD_USER:$DOWNLOAD_PASSWORD@$UPDATE_HOST/scgi-bin/dlhist.pl"
exitval=$?
if [ $exitval -ne 0  ]
then
   echo "Download of the URL download status failed (wget exit code is $exitval)."
   echo "You might need a new username/password from support@urlfilterdb.com"
   reporterror "URL database download status report failed.  You might need a new username/password.  wget exit code is $exitval."
   logger -p $SYSLOG_FACILITY.error -t ufdbDLstatus \
          "URL database download status report failed.  You might need a new username/password.  wget exit code is $exitval."
   exitval=`expr $exitval + 20`
else
   if [ $verbose = yes ]
   then
      echo "retrieval of URL database download status report was successful"
   fi
fi


if [ $exitval -ne 0 ]
then
   if [ $verbose = yes ]
   then
      echo "exiting with exit status $exitval"
   fi
   exit $exitval
fi


if [ $verbose = yes ]
then
   echo "Done."
fi

if [ $verbose = yes ]
then
   echo "exit value of ufdbDLstatus is $exitval"
fi
exit $exitval

