5 =head1 NAME $RCSfile: maps,v $
7 This script filters mail based on the files nulllist, blacklist and whitelist.
8 Input is an email message. This script extracts the From line and then parses
9 the email address. If the email is from a sender who should be /dev/null'ed
10 (e.g. bounce messages from mail daemons) the message will be discarded. If the
11 sender is on the blacklist then a message is sent back informing the sender that
12 he's been blacklisted. If the sender is on the white list then the email is
13 appended to the mail drop file. Otherwise a message is sent back informing the
14 sender that in order to successfully send email the sender must register for the
15 permission to do so, along with a URL that allows the sender to sign up.
23 Andrew DeFaria <Andrew@DeFaria.com>
31 Fri Nov 29 14:17:21 2002
35 $Date: 2013/06/12 14:05:47 $
41 Usage maps: [-u|ser <username>] [-ve|rbose] [-deb|ug] [-e|xecute]
44 -u|ser <username>: Set context to this username
47 -de|bug: Output debug messages
49 -[no]e|xecute: Set execute mode.
51 # (c) Copyright 2000-2021, Andrew@DeFaria.com, all rights reserved.
61 use File::Temp qw (tempfile);
62 use Net::Domain qw (hostdomain);
64 use lib "$FindBin::Bin/../lib", "$FindBin::Bin/../../lib";
74 my $userid = $ENV{USER};
76 my $logpath = "$FindBin::Bin/../log";
77 my $logfile = "$logpath/debug.log";
78 my $forwardto = $ENV{MAPS_FORWARDTO} || 'adefaria@gmail.com';
80 # For some reason I'm not parsing messages correctly but it only seems to
81 # happen when the message is piped in from the MTA. This routine will
82 # temporarily save the messages in a file.
85 my $msgfile = tempfile ();
87 # Read STDIN and write it out to the tempfile
92 # Seek to the start of the file (Note if we closed it, it would be deleted)
95 # Return the filehandle
99 sub ValidDomainUser($) {
102 my ($username, $domainname);
104 if ($sender =~ /(.*)\@(.*)/) {
111 return 1 if $domainname ne hostdomain;
113 # Let BICE email come through
114 return 1 if $username eq "bice";
116 my $uid = getpwnam $username;
118 return defined $uid ? 1 : 0;
121 sub ProcessMsgs ($$$) {
122 my ($msgfile, $username, $user_email) = @_;
124 return unless $execute;
126 while (!eof *$msgfile) {
127 my ($sender, $sender_long, $reply_to, $subject, $data) = ReadMsg (*$msgfile);
131 #open my $mail, '|', "/usr/lib/sendmail $forwardto"
132 #or die "Unable to open pipe to sendmail - $!";
134 #print $mail "$data\n";
137 #or die "Unable to forward email to $forwardto - $!";
140 my ($onlist, $rule, $sequence, $hit_count);
142 # Algorithm change: We now first check to see if the sender is not found
143 # in the message and skip it if so. Then we handle if we are the sender
144 # and that the from address is formatted properly. Spammers often use
145 # the senders email address (i.e. andrew@defaria.com) as their from address
146 # so we check "Andrew DeFaria <Andrew@DeFaria.com>", which they have never
147 # forged. This catches a lot of spam actually.
149 # Next we check to see if the sender is on our whitelist. If so then we let
150 # them in. This allows us to say whitelist josephrosenberg@hotmail.com while
151 # still nulllisting all of the other hotmail.com spammers.
153 # Next we process blacklisted people as they are also of high priority.
155 # Then we process nulllist people.
157 # Finally, we handle return processing
159 # Discard any email with an invalid email address
160 next unless Email::Valid->address($sender);
162 if ($sender eq $user_email and
163 (lc ($sender_long) !~ lc ("\"$username\" <$user_email>") and
164 lc ($sender_long) !~ lc ("$username <$user_email>"))) {
165 verbose "Nulllisting message from sender ($sender_long) pretending to be $user_email";
171 ($onlist, $rule, $sequence, $hit_count) = OnWhitelist $sender;
174 if (ValidDomainUser $sender) {
175 verbose "Whitelisting $sender";
176 Whitelist $sender, $data, $sequence, $hit_count;
178 verbose "Sender from this domain but user not found";
186 ($onlist, $rule, $sequence, $hit_count) = OnBlacklist $sender;
189 verbose "Blacklisting $sender";
194 sequence => $sequence,
195 hit_count => $hit_count,
203 ($onlist, $rule, $sequence, $hit_count) = OnNulllist $sender;
206 verbose "Nulllisting $sender";
207 Nulllist $sender, $sequence, $hit_count;
212 verbose "Returning message from $sender";
216 reply_to => $reply_to,
225 'user=s' => \$userid,
226 'verbose' => sub { set_verbose },
227 'debug' => sub { set_debug },
228 'execute!' => \$execute,
229 'forwardto=s' => \$forwardto
234 if ($ARGV[0] and $ARGV[0] ne "") {
235 open $msgfile, '<', $ARGV[0];
238 Error "Unable to open file ($ARGV[0]): $!\n";
242 $msgfile = SaveStdin;
245 verbose "Starting MAPS....";
247 my %userOptions = SetContext $userid
248 or die "$userid is not a registered MAPS user\n";
250 ProcessMsgs $msgfile, $userOptions{name}, $userOptions{email};