#!/bin/sh
# $Id: t-mt-lum-0.sh,v 1.3 2006/11/10 06:16:02 ca Exp $
#
# ----------------------------------------
# test mail routing using full addresses and interaction with local user map
# uses smtpcnf.sh
# ----------------------------------------
#
test -s stop && exit 1
#

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

LUM="lu.db"

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

# cd to working directory
cd ../qmgr || exit 1

# remove maps
rm -f ${ALIMAP}
rm -f ${ACCMAP}
rm -f ${MTMAPDB}

# prepare mailertable
${CAT} > ${MT} <<EOF
host123.some.dom	[1.2.3.0]
some.dom	[127.0.0.1]
.some.dom	[1.2.3.4]
.org		[1.2.3.6]
.		[1.2.3.9]
lu1@some.dom	lmtp:
notlu@some.dom	lmtp:
EOF

${MM} -t'	' -F ${MTMAPDB} < ${MT}

# create local user map
rm -f ${LUM}
${MM} -F ${LUM} <<EOF
lu1:	ok
EOF
if test $? -ne 0
then
  echo "$0: ${MM} failed"
  exit 1
fi

# ----------------------------------------
${CAT} > ${SMXCNF} <<EOF
qmgr { Log_Level = 12; wait_for_server=4; wait_for_client=4;
}
smar {
 map mt { type=hash; file=${MTMAPDB}; }
 mailertable { name=mt; flags= {full_address, domain, dotsubdomain, dot} }
 map lum { type=hash; file=${LUM}; }
 local_user_map { name=lum;}
Log_Level = 12;
nameserver = ${NS};
DNS_timeout = 5;
}
smtpc { Log_Level = 12; remote_port=${SNKPORT}; wait_for_server=4; }
smtps {
Log_Level = 14;
daemon_address = localhost:${SRVPORT};
wait_for_server=4;
}
EOF


# ----------------------------------------
# test: send mail with recipient considered local per mt and it is in lum,
# hence mail is accepted by smtps.

TEST=1
if echo " ${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
ERR=false

if ${SHELL} ${SD}/smtpcnf.sh -3 lmtpsock -Ssender@some.dom -Rlu1@some.dom -E 0 -X 0 -A 1 -e 1
then
  if test -s ${B1}
  then
    :
  else
    ERR=true
  fi
else
  ERR=true
fi
if ${ERR}
then
   echo "$0: test ${TEST} failed"
   ERRS=`expr ${ERRS} + 1 `
   test X"${MTA_STOPONERROR}" != X && exit 1
fi
rm -f ${B1}
else
  ${VERBOSE} && echo "$0: test ${TEST} SKIPPED"
fi


# ----------------------------------------
# test: send mail with recipient not considered local per mt

TEST=2
if echo " ${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
ERR=false

if ${SHELL} ${SD}/smtpcnf.sh -3 lmtpsock -Ssender@some.dom -Rrcpt@some.dom -E 1 -X 1 -A 1 -e 0
then
  if test -s ${B1}
  then
    ERR=true
  fi
else
  ERR=true
fi
if ${ERR}
then
   echo "$0: test ${TEST} failed"
   ERRS=`expr ${ERRS} + 1 `
   test X"${MTA_STOPONERROR}" != X && exit 1
fi
else
  ${VERBOSE} && echo "$0: test ${TEST} SKIPPED"
fi


# ----------------------------------------
# test: send mail with recipient considered local per mt and it is not in lum,
# hence mail is rejected by smtps.

TEST=3
if echo " ${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
ERR=false

if ${SHELL} ${SD}/smtpcnf.sh -3 lmtpsock -Ssender@some.dom -Rnotlu@some.dom -E 0 -X 0 -A 0 -e 0
then
  if test -s ${B1}
  then
    ERR=true
  fi
else
  ERR=true
fi
if ${ERR}
then
   echo "$0: test ${TEST} failed"
   ERRS=`expr ${ERRS} + 1 `
   test X"${MTA_STOPONERROR}" != X && exit 1
fi
else
  ${VERBOSE} && echo "$0: test ${TEST} SKIPPED"
fi


rm -f ${MTMAP} ${ALIMAP} ${LUM}
# ----------------------------------------
# end
if test "${ERRS}" = "0"
then
  exit 0
else
  echo "${ERRS} error(s)"
  exit 1
fi


syntax highlighted by Code2HTML, v. 0.9.1