#!/bin/sh
# $Id: t-lum-0.sh,v 1.18 2006/10/05 01:32:28 ca Exp $
# Copyright (c) 2004-2006 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#
# ------------------------------------------------------------
# Description:
# Script to test local user map for RFC2821 addresses.
# Uses smar, t-smar-2, aliases and "local user" map, t-mm, startsmar.sh
# and t-hostname
# ------------------------------------------------------------
#
test -s stop && exit 1
#

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

OUT=lum-0.out
RES=lum-0.ip
OK=lum-0.ok
TPRG=./t-smar-2
LU=users
LUM=${LU}.db
CONF=lum.conf
VERBOSE=false
MAPTYPES=""

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

# Note: reference to t-hostname! XREF
HOSTNAME=`../libmta/t-hostname -l -V 2>/dev/null`
if test $? -ne 0
then
  echo "t-hostname failed"
  exit 1
fi
if test X"${HOSTNAME}" = X
then
  echo "t-hostname returned empty name"
  exit 1
fi
if echo X"${HOSTNAME}" | grep '\.' >/dev/null
then
  :
else
  echo "t-hostname returned bogus name ${HOSTNAME}"
  exit 1
fi

killit()
{
if test -s ${PIDS}
then
  # stop MTA components
  for i in `${CAT} ${PIDS}`
  do
    kill ${i}
  done
fi
# ----------------
# thanks kids for not following the POSIX standard...
if test X"`uname`" = "XLinux"
then
  killall smar
fi
#rm -f ${MTMAP}
exit 1
}

# create alias map for smar
rm -f ${ALIMAP}
${MM} <<EOF
postmaster:	user1
toolong: <user0@some.long.domain> <user1@some.long.domain> <user2@some.long.domain> <user3@some.long.domain> <user4@some.long.domain> <user5@some.long.domain> <user6@some.long.domain> <user7@some.long.domain> <user8@some.long.domain> <user9@some.long.domain> <user10@some.long.domain> <user11@some.long.domain> <user12@some.long.domain> <user13@some.long.domain> <user14@some.long.domain> <user15@some.long.domain> <user16@some.long.domain> <user17@some.long.domain> <user18@some.long.domain> <user19@some.long.domain> <user20@some.long.domain> <user21@some.long.domain> <user22@some.long.domain> <user23@some.long.domain> <user24@some.long.domain> <user25@some.long.domain> <user26@some.long.domain> <user27@some.long.domain> <user28@some.long.domain> <user29@some.long.domain> <user30@some.long.domain> <user31@some.long.domain> <user32@some.long.domain> <user33@some.long.domain> <user34@some.long.domain> <user35@some.long.domain> <user36@some.long.domain> <user37@some.long.domain> <user38@some.long.domain> <user39@some.long.domain>
EOF
if test $? -ne 0
then
  echo t-mm failed
  exit 1
fi

if test X"${MAPTYPES}" = "X"
then
./smar -l 1 | grep ':A:L:.:F' >${MAPC}
MAPTYPES="`sed -e 's/^\([a-zA-Z][a-zA-Z0-9_]*\):A:L:.:F/\1/' < ${MAPC}`"
fi
# list map classes available for t-mm
${MM} -l 0 > ${MMMAPC}

for maptype in ${MAPTYPES}
do
if grep ${maptype} ${MMMAPC} >/dev/null
then

# create map for smar
rm -f ${LUM}
${MM} -T ${maptype} -F ${LUM} <<EOF
user1:	ok
user2:	rhs
user3:	something
EOF
if test $? -ne 0
then
  echo t-mm failed
  exit 1
fi

${CAT} > ${CONF} <<EOF
smar {
 map lum { type=${maptype}; file=${LUM}; }
 local_user_map { name= lum; }
}
EOF

rm -f ${SOCK} ${PIDS}
trap killit 1 2 3

# create mailertable (save old version?)
${CAT} > ${MT} <<EOF
local.dom	lmtp:
${HOSTNAME}	lmtp:
EOF

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

TEST=1
# ----------------------------------------
# check output somehow...
${VALGRIND} ${TPRG} -VVV '<no@local.dom>' > ${OUT} 2>&1
grep '^status=' ${OUT} > ${RES}
${CAT} > ${OK} <<EOF
status=550
EOF
if diff ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
fi

TEST=2
# ----------------------------------------
${VALGRIND} ${TPRG} -VVV '<user1@local.dom>' > ${OUT} 2>&1
grep '^status=' ${OUT} > ${RES}
${CAT} > ${OK} <<EOF
status=${SMAR_DFLT_RES}
EOF
if diff ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
fi

TEST=3
# ----------------------------------------
${VALGRIND} ${TPRG} -VVV '<postmaster@local.dom>' > ${OUT} 2>&1
grep '^status=' ${OUT} > ${RES}
${CAT} > ${OK} <<EOF
status=${SMAR_DFLT_RES}
EOF
if diff ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
fi

TEST=4
# ----------------------------------------
${VALGRIND} ${TPRG} -VVV '<user2@local.dom>' '<user3@local.dom>' > ${OUT} 2>&1
grep '^status=' ${OUT} > ${RES}
${CAT} > ${OK} <<EOF
status=${SMAR_DFLT_RES}
status=${SMAR_DFLT_RES}
EOF
if diff ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
fi

TEST=5
# ----------------------------------------
${VALGRIND} ${TPRG} -VVV '<toolong@local.dom>' > ${OUT} 2>&1
grep '^status=' ${OUT} > ${RES}
${CAT} > ${OK} <<EOF
status=${SMAR_DFLT_RES}
EOF
if diff ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
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

else
  echo "$0: ${maptype} not available for ${MM}"
fi
done
rm -f ${MTMAP}
rm -f ${MTMAP}

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1