#!/bin/sh -
PATH=/bin
IFS=" 	
"

# Saves a map posting from comp.mail.maps in directory MapDir (specified
# below.
#
# It rejects all input that does not have the format
#
#	(leading junk)
#	cat << 'SOME-MARKER' > MAP-FILE-NAME
#       (the map)
# 	SOME-MARKER
#	(trailing junk)
#
# The marker can be any string as long as it is enclosed in single quotes
# and contains no white space.  Map file name may not contain back-quotes
# or slashes.  If any of these restrictions are not met, mail is sent to
# the administrator (see "MailErrorsTo" variable) asking to investigate.
#
# If there is another error (a truncated article, for example), it just
# gives up quietly, without overwriting the old destination map file.
#
# Jacob Gore <gore@eecs.nwu.edu> 88/12/06

# Customization:
MapDir=/usr/uumap
MailErrorsTo=notes
Awk=/usr/local/bin/nawk
Mail=/usr/local/bin/v6mail
Cd=cd
Mv=/bin/mv
Rm=/bin/rm

myname=$0

TMP=tmp$$; export TMP
ORIG=orig$$

$Cd $MapDir

destination=`tee $ORIG | $Awk '

BEGIN {
	while ($1 != "cat" && $2 != "<<" && $4 != ">") {
		getline;
	}
	if ($5 ~ "[/\`]" || $3 !~ "'\''[^'\'']+'\''") {
		print "'\''" $0 "'\''"
		exit_status=9;
		exit(exit_status);
	}
	split($3, tmp_array, "'\''");
	end_marker = tmp_array[2];
	destination = $5;
	"echo $TMP" | getline temp_file
	exit_status = 1;
}

$1 == end_marker {
	print destination
	exit_status = 0;
	exit(exit_status);
}

{
	print > temp_file
}

END {
	exit(exit_status);
}
'`
status=$?

case $status in
   0) 
	$Mv -f $TMP $destination
	$Rm -f $ORIG
	exit 0
   ;;
   9)
	$Mail -s "Possible attack through $0" $MailErrorsTo << @EOF
File $MapDir/$ORIG was passed off as a USENET map.
It was rejected because the command line
  $destination
looked suspicious.  Please investigate.
@EOF
   ;;
   *)
	$Rm -f $TMP
	exit $status
   ;;
esac
