
rts script 

#!/bin/sh
#
# rts - return to sender
#
# These remarks assume rts is installed in /usr/local/etc
# and that the directory /usr/local/lib/rts contains
# text files, one for each alias name for which we wish to be able 
to
# return messages.  To change the location of this directory, modify
# the definition for RTSDIR, below.  You may install the rts script
# anywhere you wish as long as you put the proper directory specification
# in the alias file.
#
# Then you can make rts work for a username by defining an alias for
# the user that will run the script.  For example, to tell someone 
that
# the user 'joe' is no longer a valid address, define the alias:
#
#   joe:"|/usr/local/etc/rts joe"
#
# When someone sends a message to joe, rts will look up the file
# $RTSDIR/joe and send that text back to the sender, appending their
# original message at the end.
#
# You may also define the alias as:
#
#   joe:"|/usr/local/etc/rts joe joe@abc.com"
#
# which will return the same message, but also send a note to joe@abc.com
# containing the header of the message received here to let him know 
that
# someone was trying to contact him.  The actual text of the message 
is
# not sent.
#
####################################################################
#
# The following may be modified to suit the local installation:
#

NOTIFY=postmaster               # who to notify when there is a problem
RTSDIR=/usr/local/lib/rts       # where the return message files reside
MAILER=/usr/ucb/mail            # mailer to use

####################################################################

PATH="/bin:/usr/bin:/etc:/usr/etc:/usr/ucb"     # secure path for 
root
TMP="/tmp/rts.$$"                               # temp file for message

#
# save stdin (the incoming message)
#

cat - >$TMP

if [ "$1" = "" ]; then
   $MAILER -s "rts failed: no arguments" $NOTIFY <$TMP
   rm -f $TMP
   exit
fi


oldaddr="$1"                    # address we've trapped
newaddr="$2"                    # new address of user if supplied

if [ ! -f "$RTSDIR/$oldaddr" ]; then
   $MAILER -s "rts failed: file not found $RTSDIR/$oldaddr" $NOTIFY 
<$TMP
   rm -f $TMP
   exit
fi

#
# The From: line is in one of two forms:
#
#   From: Full User Name <useraddress>
#   From: useraddress (Full User Name)
#

fromline=`grep "^From:" < $TMP | head -1`

#
# find which format
#

x=`echo "$fromline" | grep "<"`
if [ "$x" = "" ]; then
   from=`echo "$fromline" | awk '{print $2}'`                 # 
addr (name)
else
   from=`echo "$fromline" | sed -e 's/.*<//' -e 's/>.*$//'`   # 
name <addr>
fi

if [ "$from" = "" ]; then
   $MAILER -s "rts failed: cannot determine sender" $NOTIFY < $TMP
   rm -f $TMP
   exit
fi

#
# send the bounce
#

cat $RTSDIR/$oldaddr $TMP | $MAILER -s "undeliverable mail" $from

#
# if there was a 2nd argument, send notification and header to the 
user
#

if [ "$newaddr" != "" ]; then
   ( cat <<EOF 
A message was sent to you here but due to security concerns, we cannot
forward it to you.  This system automatically returned the message 
to the
sender and gave him or her your new address.

However, so that you are aware of the attempt to contact you, we are
enclosing the header of the mail message here:


EOF

sed -e '/^$/,$d' <$TMP ) | $MAILER -s "attempted contact" $newaddr

fi


#
# clean up
#

rm -f $TMP



