#!/bin/sh
# $Id: t-alias-conf-0.sh,v 1.5 2007/04/26 04:08:59 ca Exp $
# Copyright (c) 2005, 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 sequence map
# Uses smar, t-smar-1, "aliases" map, t-mm, 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-0.out
RES=alias-0.ip
OK=alias-0.ok
TPRG=./t-smar-1
VERBOSE=false
MAPTYPES=""

if test X"`uname`" != "XOSF1"
then
while getopts s:T:V FLAG
do
  case "${FLAG}" in
    T) MAPTYPES="${OPTARG}";;
    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}
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
}

CONF=alias.conf
AM1=alias1
AM2=alias2
AM1MAP=alias1.db
AM2MAP=alias2.db

trap killit 1 2 3

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

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

rm -f ${SOCK} ${PIDS}
rm -f ${AM1} ${AM2} ${AM1MAP} ${AM2MAP}
# create maps for smar
${MM} -T ${maptype} -F ${AM1MAP} <<EOF
alias1:	local:
user1:	local:
user0:	local:
EOF
if test $? -ne 0
then
  echo t-mm failed
  exit 1
fi
# create maps for smar
${MM} -T ${maptype} -F ${AM2MAP} <<EOF
alias2:	user1
alias0:	user0
user2:	local:
list2:	<user1@local.dom> <user2@local.dom> <alias0@local.dom>
EOF
if test $? -ne 0
then
  echo t-mm failed
  exit 1
fi

# create configuration file
${CAT} > ${CONF} <<EOF
smar {
map aliases1 { type=${maptype}; file=${AM1MAP}; }
map aliases2 { type=${maptype}; file=${AM2MAP}; }
map seqaliases { type=sequence; maps = { aliases1, aliases2}; }
aliases { name=seqaliases; flags=implicitly_match_detail; }
}
EOF

# 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]=127.0.0.255 [7f0000ff]
alias 1/3
aliased=<user2@local.dom>
IP[0]=127.0.0.255 [7f0000ff]
alias 2/3
aliased=<user1@local.dom>
IP[0]=127.0.0.255 [7f0000ff]
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}`
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}

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1