#!/usr/bin/perl use strict; my $MAILBOXN = "/var/mail/mackys"; my $WHITELISTN = "/home/mackys/whitelist/whitelist.txt"; my @HEADER; my $FROM = ""; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); local (*WHITELISTH, *MAILBOXH); # Filehandles can't be "my" vars. # Note the date/time we were run at. printf "%d/%02d/%02d %02d:%02d:%02d ", $year+1900, $mon+1, $mday, $hour, $min, $sec; # Read in the header of the incoming message on stdin. while(<>) { push(@HEADER, $_); if($FROM eq "" && /^From:/){ $FROM = $_; } if($_ eq "\n") { last; } # break out of loop } # Extract just the email address out of the From: line. if($FROM =~ /(\w\S*\@\S+\.\w{2,4})/) { $FROM = $1; } # Check the email address against each line in the whitelist. open(WHITELISTH, "<$WHITELISTN") or die "Can't open whitelist $WHITELISTN"; while() { chomp; # Strip off newline, leading and trailing spaces. s/^\s+//; s/\s+$//; if($_ eq $FROM) { # They're on the whitelist. Deliver the mail to the mailbox. open(MAILBOXH, ">>$MAILBOXN") or die "Can't open mailbox $MAILBOXN"; foreach(@HEADER){ print MAILBOXH; } # Header while(<>){ print MAILBOXH; } # Body print MAILBOXH "\n\n\n"; # Dividing whitespace between msgs print "$FROM OK!\n"; exit 0; } } # If we got here, the From: address is not on the whitelist. Bounce it. print "$FROM denied.\n"; exit 77; # "Permission denied"