#!/bin/csh -fb
# (The "-fb" might need to be changed to "-f" on some systems)
#
# Mailserver -- a simple MIME mailserver script.
# Makes all files under a tree available for MIME-based retrieval.
# By default, it sends them as the MIME type "application/octet-stream"
# However, for a file named "x/y/foo.bar", you can specify a "right"
# MIME content-type by putting it in the file "x/y/foo.bar.ct".

# In a distributed sendmail environment, this script can be installed with lines
#	somewhat like the following two in /usr/lib/aliases:
# mail-server: local-mail-server@some-single-machine
# local-mail-server: "|/full/path/to/mailserver"

# By default the program uses "mail-server" as its local return address.
#   and makes available all files under /usr/spool/ftp.
# You might need or want to change the following parameters:
set LOCALADDR=mail-server
set ROOTDIR=/usr/spool/ftp
set MAINTAINER=postmaster
set METAMAILDIR=/usr/local/bin
set LOGADDR=andrew@thumper.bellcore.com
# If LOGADDR is the empty string, no logging is done.
#
# The real program begins here.

setenv PATH ${METAMAILDIR}:${PATH}
rehash
set FromName=""
set Subject=""
#  Generate temporary file name:
if ( -x /bin/mktemp ) then
    set TmpFile=`/bin/mktemp /tmp/ms.XXXXXXX` || exit 1
else if ( -x /usr/bin/mktemp ) then
    set TmpFile=`/usr/bin/mktemp /tmp/ms.XXXXXXX` || exit 1
else
    set TmpFile=/tmp/ms.$$
    rm -rf $TmpFile
endif
set FOORAW=$<
while ("$FOORAW" != "") 
set FOO=(` echo "$FOORAW" | tr "[" "x"`)
set BAR=($FOO)
set BARLC=(`echo "$FOO" | tr A-Z a-z`)
if ("$BARLC[1]" == "from:") then
	if ("$FromName" == "") then
		set FromName = ("$BAR[2-]")
	endif
else if ($BARLC[1] == "reply-to:") then
	set FromName = ("$BAR[2-]")
else if ($BARLC[1] == "subject:") then
	set Subject = ("$BAR[2-]")
endif
set FOORAW=$<
end
# Now, stdin just has the body left, to do with as we please.
# We choose to interpret the first line as the request, nothing more
if ("$Subject" == "") then
    set Subject=$<
endif

if ("$FromName" == "") then
	echo From: "$LOCALADDR"@`hostname` > $TmpFile
	echo To: "$MAINTAINER" >> $TmpFile
	echo Subject: "$Subject" >> $TmpFile
	cat >> $TmpFile <<!

The metamail mailserver script, installed locally as $LOCALADDR, 
has received a request without any reply address.

It is possible that this is the result of a user running the "mailserver" 
program by hand.  It is intended to be run as an automated recipient of 
mail requests, rather than an interactive program.

No reply is being generated, but the contents of the request are 
reproduced below.  If no message appears below, then this program was 
probably run in some circumstance other than mail delivery.
--------------------
!
	cat $TmpFile - | /usr/sbin/sendmail "$MAINTAINER"
	# Takes the rest of the message from standard input
	rm $TmpFile
	exit 0
endif

set danger=`echo "$Subject" | fgrep ..`
if ("$danger" != "") then
	echo From: "$LOCALADDR"@`hostname` > $TmpFile
	echo To: "$FromName" >> $TmpFile
	echo Subject: Re: "$Subject" >> $TmpFile
	cat >> $TmpFile <<!

For security reasons, this mailserver automatically rejects all requests 
that contain ".." in the path name.

The file you requested, if it exists, will not be sent to you.
!
	/usr/sbin/sendmail -t < $TmpFile
	rm $TmpFile
	exit 0
endif

cd $ROOTDIR
if (! -e "$Subject") then
	#  We use a bunch of echo statements rather than a here-document
	#  so that we can be sure to quote all of our variables properly.
	echo From: "$LOCALADDR"@`hostname` > $TmpFile
	echo To: "$FromName" >> $TmpFile
	echo Subject: Re: "$Subject" >> $TmpFile
	echo "" >> $TmpFile
	echo "You recently sent mail to this mail-server requesting the file:" >> $TmpFile
	echo "	$Subject" >> $TmpFile
	echo "" >> $TmpFile
	echo "That file does not exist, so your request could not be met." >> $TmpFile
	echo "" >> $TmpFile
	echo "Here is a list of the currently available files:" >> $TmpFile
	echo "--------------------------------" >> $TmpFile
	ls -R >> $TmpFile
	echo "" >> $TmpFile
	/usr/sbin/sendmail -t < $TmpFile
	rm $TmpFile
	exit 0
endif

if (-e "${Subject}.ct") then
	set ct=`cat "${Subject}.ct"`
else 
	set ct="application/octet-stream"
endif

metasend -b -t "$FromName" -f "$Subject" -m "$ct" -s "Re: $Subject"
if ($status != 0) then
	#  We use a bunch of echo statements rather than a here-document
	#  so that we can be sure to quote all of our variables properly.
	echo From: "$LOCALADDR"@`hostname` > $TmpFile
	echo To: "$FromName" >> $TmpFile
	echo Subject: Re: "$Subject" >> $TmpFile
	echo "" >> $TmpFile
	echo "You recently sent mail to this mail-server requestion the file:" >> $TmpFile
	echo "	$Subject" >> $TmpFile
	echo "" >> $TmpFile
	echo "An unanticipated error apparently precluded delivery of the file." >> $TmpFile
	echo "Please accept our apologies." >> $TmpFile
	echo "" >> $TmpFile
	echo "Command failed:" >> $TmpFile
	echo "  metasend -b -t $FromName -f $Subject -m $ct -s Re: $Subject" >> $TmpFile
	echo "" >> $TmpFile
	/usr/sbin/sendmail -t < $TmpFile
	rm $TmpFile
	exit 0
endif

if ("$LOGADDR" != "") then
	echo From: "${LOCALADDR}"@`hostname` > $TmpFile
	echo To: "$LOGADDR" >> $TmpFile
	echo Subject: Autosend delivery report >> $TmpFile
	echo "" >> $TmpFile
	echo The file: "$Subject" >> $TmpFile
	echo was sent to: "$FromName" >> $TmpFile
	echo "" >> $TmpFile
	/usr/sbin/sendmail -t < $TmpFile
endif

rm -f $TmpFile
exit 0
