#!/bin/sh
# $Id: t-alias-conf-1.sh,v 1.4 2007/04/26 04:08:59 ca Exp $
# Copyright (c) 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 alias expansion for RFC2821 addresses using a socket map
# Uses smar, t-smar-1, "aliases" map, startsmar.sh
# and t-hostname
# ------------------------------------------------------------
#
test -s stop && exit 1
#

# path to programs
P=..
SD=`dirname $0`
if test "${SD}" = "."
then
   SD=../smar
fi
. ${SD}/common.sh

OUT=alias-1.out
RES=alias-1.ip
OK=alias-1.ok
TPRG=./t-smar-1
VERBOSE=false
SOCKSRV=${SD}/../libsmmap/t-sockmapsrv.pl
PID=sock.pid
PORT=1289
ADDR=127.0.0.1

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

# 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} -o -s ${PID}
then
  # stop MTA components
  for i in `cat ${PIDS} ${PID}`
  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
}

CONF=alias.conf

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

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

# create configuration file
${CAT} > ${CONF} <<EOF
smar {
map alias_sock { type=socket; address=${ADDR}; port=${PORT}; mapname=aliases; }
aliases { name=alias_sock; flags=implicitly_match_detail; }
}
EOF

# start socket server (before smar!)
perl ${SOCKSRV} tcp:${ADDR}:${PORT} &
echo $! > ${PID}
sleep 3

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

TEST=1
if echo "${SKIP} X" | grep -v " ${TEST} " > /dev/null
then
# ----------------------------------------
${VALGRIND} ${TPRG} -VVV '<user2@local.dom>' > ${OUT} 2>&1
egrep '^(IP|alias)' ${OUT} > ${RES}
cat > ${OK} <<EOF
IP[0]=${MTA_LMTPIPV4D} [${MTA_LMTPIPV4H}]
EOF
if diff -i ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
fi
fi

TEST=2
if echo "${SKIP} X" | grep -v " ${TEST} " > /dev/null
then
# ----------------------------------------
${VALGRIND} ${TPRG} -VVV '<list2@local.dom>' > ${OUT} 2>&1
egrep '^(IP|alias)' ${OUT} | sort > ${RES}
sort > ${OK} <<EOF
aliases=3
alias 0/3
aliased=<user0@${HOSTNAME}>
IP[0]=${MTA_LMTPIPV4D} [${MTA_LMTPIPV4H}]
alias 1/3
aliased=<user2@local.dom>
IP[0]=${MTA_LMTPIPV4D} [${MTA_LMTPIPV4H}]
alias 2/3
aliased=<user1@local.dom>
IP[0]=${MTA_LMTPIPV4D} [${MTA_LMTPIPV4H}]
EOF
if diff -i ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
fi
fi

TEST=3
if echo "${SKIP} X" | grep -v " ${TEST} " > /dev/null
then
# ----------------------------------------
${VALGRIND} ${TPRG} -VVV '<user3@local.dom>' > ${OUT} 2>&1
egrep '^(IP|alias)' ${OUT} | sed -e 's/ \[.*//' > ${RES}
cat > ${OK} <<EOF
aliased=<ext3@other.dom>
IP[0]=10.2.3.4
EOF
if diff -i ${OK} ${RES}
then
  :
else
  ERRS=`expr ${ERRS} + 1 `
  echo "$0: test ${TEST} failed."
  test X"${MTA_STOPONERROR}" != X && killit
fi
fi


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

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1