#!/bin/sh
# $Id: t-dnsbl-2.sh,v 1.5 2007/01/28 01:08:36 ca Exp $
#
# ------------------------------------------------------------
# Description:
# Script to test DNS BL and access map restrictions
# Uses smar, t-smar-3, access map, t-mm, startsmar.sh
# ------------------------------------------------------------
#
test -s stop && exit 1
#

if test X"${MTA_NO_DNS_TEST}" != "X"
then
  echo "SKIPPED: $0: MTA_NO_DNS_TEST is set"
  exit 0
fi
# this test requires DNS entries for 10.0.255.*
if test X"${MTA_HAVE_10_INADDRARPA}" != "Xtrue"
then
  echo "SKIPPED: $0: MTA_HAVE_10_INADDRARPA is not set to true"
  exit 0
fi


SD=`dirname $0`
if test "${SD}" = "."
then
   SD=../smar
fi
. ${SD}/common.sh

OUT=dnsbl-2.out
IP=dnsbl-2.ip
OK=dnsbl-2.ok
VERBOSE=false
CONF=smar.conf
TPRG=./t-smar-3
RMTMP=true

if test X"`uname`" != "XOSF1"
then
while getopts kV FLAG
do
  case "${FLAG}" in
    k) RMTMP=false;;
    V) VERBOSE=true;;
  esac
done
shift `expr ${OPTIND} - 1`
fi

rm -f ${ACCMAP} ${SOCK} ${PIDS}

# create access map for tests
${MM} -t'	' -F ${ACCMAP} <<EOF
meta1dnsbl:10.0.255.1	error:550 5.7.1 found 10.0.255.1 in meta1dnsbl
cltname:one.meta1.org.	ok
meta1dnsbl:10.0.255.2	error:550 5.7.2 found 10.0.255.2 in meta1dnsbl
cltaddr:10.0.255.2	ok
meta1dnsbl:127.0.0.3	error:550 5.7.3 found 127.0.0.3 in meta1dnsbl
EOF

cat > ${CONF} <<EOF
smar {
Log_Level = 12;
DNS_timeout = 5;
# should tag include ":"?
dnsbl { domain = dnsbl.meta1.org.; tag = "meta1dnsbl"; }
}
EOF

# start SMAR
${SHELL} ${SD}/startsmar.sh -f ${CONF} -d 6 || exit 1
rm -f ${OUT} ${IP} ${OK}

TEST=1
# ----------------------------------------
# test: dnsbl: override by access map: client IPv4 address
${VALGRIND} ${TPRG} -d '10.0.255.2' > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
cat > ${OK} <<EOF
EOF
if diff ${OK} ${IP}
then
  if fgrep 'func=smar_dnsbl_cb, u=0, dnsbl=dnsbl.meta1.org., pa=10.0.255.2' ${ALOG} >/dev/null
  then
    ERRS=`expr ${ERRS} + 1 `
    echo "$0: test ${TEST} failed: smar_dnsbl_cb was executed"
    ${VERBOSE} && echo ${TEST} failed
  fi
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  ${VERBOSE} && echo ${TEST} failed
fi

TEST=2
# ----------------------------------------
# test: dnsbl: override by access map: client name
${VALGRIND} ${TPRG} -n -D '10.0.255.1' > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
cat > ${OK} <<EOF
react=rvrs_st, ret=0, stat=1
react=rvrs_name, ret=0, name=one.meta1.org.
EOF
if diff ${OK} ${IP}
then
  if fgrep 'func=smar_dnsbl_cb, u=0, dnsbl=dnsbl.meta1.org., pa=10.0.255.2' ${ALOG} >/dev/null
  then
    ERRS=`expr ${ERRS} + 1 `
    echo "$0: test ${TEST} failed: smar_dnsbl_cb was executed"
    ${VERBOSE} && echo ${TEST} failed
  fi
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  ${VERBOSE} && echo ${TEST} failed
fi

TEST=3
# ----------------------------------------
# test: dnsbl: no override by access map
${VALGRIND} ${TPRG} -n -D '127.0.0.3' > ${OUT} 2>&1
grep '^react=' ${OUT} > ${IP}
cat > ${OK} <<EOF
react=statt, ret=0, statt='550 5.7.3 found 127.0.0.3 in meta1dnsbl
react=rvrs_st, ret=0, stat=2
EOF
if diff ${OK} ${IP}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  ${VERBOSE} && echo ${TEST} failed
fi


# stop SMAR
for i in `cat ${PIDS}`
do
  kill ${i}
done
# ----------------
# thanks kids for not following the POSIX standard...
if test X"`uname`" = "XLinux"
then
  killall smar
fi
sleep 3

if test ${ERRS} -eq 0
then
  ${RMTMP} && rm -f ${OUT} ${IP} ${OK}
fi

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1