Rewrote ReadMsg - Andrew@ClearSCM.com check
authorAndrew DeFaria <Andrew@DeFaria.com>
Mon, 13 Sep 2021 17:16:15 +0000 (10:16 -0700)
committerAndrew DeFaria <Andrew@DeFaria.com>
Mon, 13 Sep 2021 17:16:15 +0000 (10:16 -0700)
As I've gotten Andrew@ClearSCM.com email working again and it just send
it to Andrew@DeFaria.com I noticed that I was now getting additional
spam messages with a To of Andrew@ClearSCM.com. I rarely, if ever send
email to Andrew@ClearSCM.com and if I start I'll use the old trick of a
checking for full email address of "Andrew DeFaria
<Andrew@ClearSCM.com>" on To (not implemented yet). Meantime I will just
assume all email sent to Andrew@ClearSCM.com is spam.

In order to implement this I needed to change ReadMsg to also return the
To: address. Since the return arguments were getting large I chqanged
this to return a hash.

maps/bin/maps
maps/bin/mapsutil.pl
maps/lib/MAPS.pm
maps/lib/MAPSLog.pm

index e716a83..4a9b64e 100755 (executable)
@@ -134,7 +134,7 @@ sub ProcessMsgs ($$$) {
   return unless $opts{execute};
 
   while (!eof $msgfile) {
-    my ($sender, $sender_long, $reply_to, $subject, $data) = ReadMsg ($msgfile);
+    my %msgInfo = ReadMsg $msgfile;
 
     my ($onlist, $rec, $sequence, $hit_count);
 
@@ -156,72 +156,78 @@ sub ProcessMsgs ($$$) {
     # Finally, we handle return processing
 
     # Discard any email with an invalid email address
-    next unless Email::Valid->address($sender);
+    next unless Email::Valid->address($msgInfo{sender});
 
-    if ($sender eq $user_email and
-            (lc ($sender_long) !~ lc ("\"$username\" <$user_email>") and
-             lc ($sender_long) !~ lc ("$username <$user_email>"))) {
-      $log->msg("Nulllisting message from sender ($sender_long) pretending to be $user_email");
+    if ($msgInfo{sender} eq $user_email and
+            (lc ($msgInfo{sender_long}) !~ lc ("\"$username\" <$user_email>") and
+             lc ($msgInfo{sender_long}) !~ lc ("$username <$user_email>"))) {
+      $log->msg("Nulllisting message from sender ($msgInfo{sender_long}) pretending to be $user_email");
 
-      Nulllist $sender;
+      next;
+    } # if
+
+    # Discard messges coming from andrew@clearscm.com because I don't send from
+    # that email address
+    if (lc $msgInfo{to} eq 'andrew@clearscm.com') {
+      $log->msg("Nullisting message from Andrew\@ClearSCM.com since I don't send from that email address");
 
       next;
     } # if
 
     # Check whitelist:
-    ($onlist, $rec) = OnWhitelist $sender;
+    ($onlist, $rec) = OnWhitelist $msgInfo{sender};
 
     if ($onlist) {
-      if (ValidDomainUser $sender) {
-        $log->msg("Whitelisting $sender - Rule: " . formatRule($rec));
+      if (ValidDomainUser $msgInfo{sender}) {
+        $log->msg("Whitelisting $msgInfo{sender} - Rule: " . formatRule($rec));
 
-        Whitelist $sender, $data, $rec->{sequence}, $rec->{hit_count};
+        Whitelist $msgInfo{sender}, $msgInfo{data}, $rec->{sequence}, $rec->{hit_count};
       } else {
-        $log->msg("Sender ($sender) from this domain but user not found");
+        $log->msg("Sender ($msgInfo{sender}) from this domain but user not found");
 
-        Nulllist $sender;
+        Nulllist $msgInfo{sender};
       } # if
 
       next;
     } # if
 
     # Check blacklist:
-    ($onlist, $rec) = OnBlacklist $sender;
+    ($onlist, $rec) = OnBlacklist $msgInfo{sender};
 
     if ($onlist) {
-      $log->msg("Blacklisting $sender - Rule: " . formatRule($rec));
+      $log->msg("Blacklisting $msgInfo{sender} - Rule: " . formatRule($rec));
 
       Blacklist(
         userid    => $userid,
-        sender    => $sender,
+        sender    => $msgInfo{sender},
         sequence  => $rec->{sequence},
         hit_count => $rec->{hit_count},
-        data      => $data,
+        data      => $msgInfo{data},
       );
 
       next;
     } # if 
 
     # Check nulllist:
-    ($onlist, $rec) = OnNulllist $sender;
+    ($onlist, $rec) = OnNulllist $msgInfo{sender};
 
     if ($onlist) {
-      $log->msg("Nulllisting $sender - Rule: " . formatRule($rec));
+      $log->msg("Nulllisting $msgInfo{sender} - Rule: " . formatRule($rec));
 
-      Nulllist $sender, $rec->{sequence}, $rec->{hit_count};
+      Nulllist $msgInfo{sender}, $rec->{sequence}, $rec->{hit_count};
 
       next;
     } # if
 
     # Return processing:
-    $log->msg("Returning message from $sender");
+    $log->msg("Returning message from $msgInfo{sender}");
 
     ReturnMsg(
       userid   => $userid,
-      sender   => $sender,
-      reply_to => $reply_to,
-      subject  => $subject,
-      data     => $data,
+      sender   => $msgInfo{sender},
+      reply_to => $msgInfo{reply_to},
+      subject  => $msgInfo{subject},
+      data     => $msgInfo{data},
     );
   } # while
 } # ProcessMsgs
@@ -243,7 +249,7 @@ $log = Logger->new(
 
 my $msgfile;
 
-if ($ARGV[0] and $ARGV[0] ne "") {
+if ($ARGV[0] and $ARGV[0] ne '') {
   open $msgfile, '<', $ARGV[0];
 
   unless ($msgfile) {
index 76ff209..be9da97 100755 (executable)
@@ -242,18 +242,18 @@ sub LoadEmail($) {
   my $nbr_msgs;
 
   while (!eof $file) {
-    my ($sender, $reply_to, $subject, $data) = ReadMsg (*$file);
+    my %msgInfo = ReadMsg *$file;
 
     $nbr_msgs++;
 
     AddEmail(
       userid  => $userid,
-      sender  => $sender,
-      subject => $subject,
-      data    => $data,
+      sender  => $msgInfo{sender},
+      subject => $msgInfo{subject},
+      data    => $msgInfo{data},
     );
 
-    Info("Added message from $sender to email");
+    Info("Added message from $msgInfo{sender} to email");
   } # while
 
   if ($nbr_msgs == 0) {
index c61a4da..793f015 100644 (file)
@@ -1,5 +1,4 @@
-#!/usr/bin/perl
-#################################################################################
+################################################################################
 #
 # File:         $RCSfile: MAPS.pm,v $
 # Revision:     $Revision: 1.1 $
@@ -39,6 +38,7 @@ our $VERSION = '2.0';
 # Globals
 my $userid = $ENV{MAPS_USERNAME} ? $ENV{MAPS_USERNAME} : $ENV{USER};
 my %useropts;
+my $mailLoopMax = 5;
 
 our @EXPORT = qw(
   Add2Blacklist
@@ -363,7 +363,7 @@ sub Blacklist(%) {
   # Check to see if this sender has already emailed us.
   my $msg_count = $db->count('email', "userid='$rec{userid}' and sender like '%$rec{sender}%'");
 
-  if ($msg_count < 5) {
+  if ($msg_count < $mailLoopMax) {
     # Bounce email
     my @spammsg = split "\n", $rec{data};
 
@@ -966,67 +966,67 @@ sub OptimizeDB() {
 } # OptimizeDB
 
 sub ReadMsg($) {
-  # Reads an email message file from $input. Returns sender, subject,
-  # date and data, which is a copy of the entire message.
   my ($input) = @_;
 
-  my $sender          = '';
-  my $sender_long     = '';
-  my $envelope_sender = '';
-  my $reply_to        = '';
-  my $subject         = '';
-  my $data            = '';
-  my @data;
+  my (%msgInfo, @data, $envelope_sender);
 
-  # Find first message's "From " line indicating start of message
+  # Reads an email message file from $input. Returns sender, subject, date and
+  # data, which is a copy of the entire message. Find first message's "From "
+  # line indicating start of message.
   while (<$input>) {
     chomp;
     last if /^From /;
   } # while
 
   # If we hit eof here then the message was garbled. Return indication of this
-  if (eof($input)) {
-    $data = "Garbled message - unable to find From line";
-    return $sender, $sender_long, $reply_to, $subject, $data;
-  } # if
+  return if eof($input);
 
   if (/From (\S*)/) {
-    $envelope_sender = $1;
-    $sender_long     = $envelope_sender;
+    $msgInfo{sender_long} = $envelope_sender = $1;
   } # if
 
   push @data, $_ if /^From /;
 
   while (<$input>) {
-    chomp;
+    chomp; chop if /\r$/;
+
     push @data, $_;
 
     # Blank line indicates start of message body
-    last if ($_ eq "" || $_ eq "\r");
+    last if ($_ eq '' || $_ eq "\r");
 
     # Extract sender's address
-    if (/^from: .*/i) {
-      $_ = substr ($_, 6);
-
-      $sender_long = $_;
-
-      if (/<(\S*)@(\S*)>/) {
-        $sender = lc ("$1\@$2");
-      } elsif (/(\S*)@(\S*)\ /) {
-        $sender = lc ("$1\@$2");
-      } elsif (/(\S*)@(\S*)/) {
-        $sender = lc ("$1\@$2");
+    if (/^from: (.*)/i) {
+      $msgInfo{sender_long} = $msgInfo{sender} = $1;
+
+      if ($msgInfo{sender} =~ /<(\S*)@(\S*)>/) {
+        $msgInfo{sender} = lc ("$1\@$2");
+      } elsif ($msgInfo{sender} =~ /(\S*)@(\S*)\ /) {
+        $msgInfo{sender} = lc ("$1\@$2");
+      } elsif ($msgInfo{sender} =~ /(\S*)@(\S*)/) {
+        $msgInfo{sender} = lc ("$1\@$2");
       } # if
-    } elsif (/^subject: .*/i) {
-      $subject = substr ($_, 9);
-    } elsif (/^reply-to: .*/i) {
-      $_ = substr ($_, 10);
-      if (/<(\S*)@(\S*)>/) {
-        $reply_to = lc ("$1\@$2");
-      } elsif (/(\S*)@(\S*)\ /) {
-        $reply_to = lc ("$1\@$2");
-      } elsif (/(\S*)@(\S*)/) {
-        $reply_to = lc ("$1\@$2");
+    } elsif (/^subject: (.*)/i) {
+      $msgInfo{subject} = $1;
+    } elsif (/^reply-to: (.*)/i) {
+      $msgInfo{reply_to} = $1;
+
+      if ($msgInfo{reply_to} =~ /<(\S*)@(\S*)>/) {
+        $msgInfo{reply_to} = lc ("$1\@$2");
+      } elsif ($msgInfo{reply_to} =~ /(\S*)@(\S*)\ /) {
+        $msgInfo{reply_to} = lc ("$1\@$2");
+      } elsif ($msgInfo{reply_to} =~ /(\S*)@(\S*)/) {
+        $msgInfo{reply_to} = lc ("$1\@$2");
+      } # if
+    } elsif (/^to: (.*)/i) {
+      $msgInfo{to} = $1;
+
+      if ($msgInfo{to} =~ /<(\S*)@(\S*)>/) {
+        $msgInfo{to} = lc ("$1\@$2");
+      } elsif ($msgInfo{to} =~ /(\S*)@(\S*)\ /) {
+        $msgInfo{to} = lc ("$1\@$2");
+      } elsif ($msgInfo{to} =~ /(\S*)@(\S*)/) {
+        $msgInfo{to} = lc ("$1\@$2");
       } # if
     } # if
   } # while
@@ -1036,31 +1036,38 @@ sub ReadMsg($) {
     chomp;
 
     last if (/^From /);
+
     push @data, $_;
   } # while
 
   # Set file pointer back by length of the line just read
-  seek ($input, -length () - 1, 1) if !eof $input;
+  seek ($input, -length() - 1, 1) if !eof $input;
 
   # Sanitize email addresses
-  $envelope_sender =~ s/\<//g;
-  $envelope_sender =~ s/\>//g;
-  $envelope_sender =~ s/\"//g;
-  $envelope_sender =~ s/\'//g;
-  $sender          =~ s/\<//g;
-  $sender          =~ s/\>//g;
-  $sender          =~ s/\"//g;
-  $sender          =~ s/\'//g;
-  $reply_to        =~ s/\<//g;
-  $reply_to        =~ s/\>//g;
-  $reply_to        =~ s/\"//g;
-  $reply_to        =~ s/\'//g;
+  $envelope_sender   =~ s/\<//g;
+  $envelope_sender   =~ s/\>//g;
+  $envelope_sender   =~ s/\"//g;
+  $envelope_sender   =~ s/\'//g;
+
+  $msgInfo{sender}   =~ s/\<//g;
+  $msgInfo{sender}   =~ s/\>//g;
+  $msgInfo{sender}   =~ s/\"//g;
+  $msgInfo{sender}   =~ s/\'//g;
+
+  if ($msgInfo{reply_to}) {
+    $msgInfo{reply_to} =~ s/\<//g;
+    $msgInfo{reply_to} =~ s/\>//g;
+    $msgInfo{reply_to} =~ s/\"//g;
+    $msgInfo{reply_to} =~ s/\'//g;
+  } # if
 
   # Determine best addresses
-  $sender    = $envelope_sender if $sender eq "";
-  $reply_to  = $sender          if $reply_to eq "";
+  $msgInfo{sender}   = $envelope_sender unless $msgInfo{sender};
+  $msgInfo{reply_to} = $msgInfo{sender} unless $msgInfo{reply_to};
 
-  return $sender, $sender_long, $reply_to, $subject, join "\n", @data;
+  $msgInfo{data} = join "\n", @data;
+
+  return %msgInfo;
 } # ReadMsg
 
 sub RecordHit(%) {
@@ -1152,7 +1159,7 @@ sub ReturnMsg(%) {
   # Check to see if this sender has already emailed us.
   my $msg_count = $db->count('email', "userid='$userid' and sender like '%$params{sender}%'");
 
-  if ($msg_count < 5) {
+  if ($msg_count < $mailLoopMax) {
     # Return register message
     SendMsg(
       userid   => $params{userid},
@@ -1467,7 +1474,7 @@ sub Whitelist ($$;$$) {
 
   # Dump message into a file
   open my $message, '>', "/tmp/MAPSMessage.$$"
-    or Error("Unable to open message file (/tmp/MAPSMessage.$$): $!\n"), return -1;
+    or error("Unable to open message file (/tmp/MAPSMessage.$$): $!\n"), return -1;
 
   print $message $data;
 
@@ -1485,8 +1492,8 @@ sub Whitelist ($$;$$) {
       sender  => $sender, 
       message => 'Delivered message',
     );
-  } else { 
-    Error("Unable to deliver message - is MAPSDeliver setgid? - $!");
+  } else {
+    error("Unable to deliver message - is MAPSDeliver setgid? - $!", $status);
   } # if
 
   $hit_count++ if $sequence;
index 9d88db4..b0b2c85 100644 (file)
@@ -106,7 +106,7 @@ sub Debug(%) {
 sub Error(%) {
   my (%params) = @_;
 
-CheckParms(['userid', 'message'], \%params);
+  CheckParms(['userid', 'message'], \%params);
 
   return Logmsg(
     userid  => $params{userid},