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 $
42 Usage maps: [-u|ser <username>] [-ve|rbose] [-deb|ug] [-e|xecute]
45 -u|ser <username>: Set context to this username
48 -de|bug: Output debug messages
50 -[no]e|xecute: Set execute mode.
52 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
61 use File::Temp qw (tempfile);
62 use Net::Domain qw (hostdomain);
64 use lib $FindBin::Bin, '/opt/clearscm/lib';
74 my $userid = $ENV{USER};
76 my $logpath = "$FindBin::Bin/../log";
77 my $logfile = "$logpath/debug.log";
79 # For some reason I'm not parsing messages correctly but it only seems to
80 # happen when the message is piped in from the MTA. This routine will
81 # temporarily save the messages in a file.
84 my $msgfile = tempfile ();
86 # Read STDIN and write it out to the tempfile
91 # Seek to the start of the file (Note if we closed it, it would be deleted)
94 # Return the filehandle
99 my ($sender, $sender_long, $reply_to, $subject, $data) = @_;
101 open SAVED_MSG, ">>$logpath/$sender"
102 or die "Unable to open $logpath/$sender - $!\n";
104 print SAVED_MSG "Sender = $sender\n";
105 print SAVED_MSG "Sender long = $sender\n";
106 print SAVED_MSG "reply_to = $reply_to\n";
107 print SAVED_MSG "subject = $subject\n";
108 print SAVED_MSG "data:\n\n";
109 print SAVED_MSG $data;
110 print SAVED_MSG "*** END OF DATA***\n";
113 sub ValidDomainUser ($) {
116 my ($username, $domainname);
118 if ($sender =~ /(.*)\@(.*)/) {
125 return 1 if $domainname ne hostdomain;
127 # Let BICE email come through
128 return 1 if $username eq "bice";
130 my $uid = getpwnam $username;
132 return defined $uid ? 1 : 0;
135 sub ProcessMsgs ($$$) {
136 my ($msgfile, $username, $user_email) = @_;
141 while (!eof *$msgfile) {
142 my ($sender, $sender_long, $reply_to, $subject, $data) = ReadMsg (*$msgfile);
144 my ($onlist, $rule, $sequence, $hit_count);
146 if ($sender eq "" or $sender eq "@" or $sender =~ /.*\@$/) {
147 verbose "Sender not found in message";
149 } elsif ($sender eq $user_email and
150 (lc ($sender_long) !~ lc ("\"$username\" <$user_email>") and
151 lc ($sender_long) !~ lc ("$username <$user_email>"))) {
152 verbose "Nulllisting message from sender ($sender_long) pretending to be $user_email";
157 ($onlist, $rule, $sequence, $hit_count) = OnNulllist $sender;
160 verbose "Nulllisting $sender";
161 Nulllist $sender, $sequence, $hit_count;
165 ($onlist, $rule, $sequence, $hit_count) = OnBlacklist $sender;
168 verbose "Blacklisting $sender";
169 my @msg = split /\n/, $data;
171 Blacklist $sender, $sequence, $hit_count, @msg;
175 ($onlist, $rule, $sequence, $hit_count) = OnWhitelist $sender;
178 if (ValidDomainUser $sender) {
179 verbose "Whitelisting $sender";
180 Whitelist $sender, $data, $sequence, $hit_count;
182 verbose "Sender from this domain but user not found";
186 if ($sender !~ /\@/) {
187 verbose "Sender ($sender) does not appear to be a valid email address";
189 verbose "Returning message from $sender";
190 ReturnMsg $sender, $reply_to, $subject, $data;
198 'user=s' => \$userid,
199 'verbose' => sub { set_verbose },
200 'debug' => sub { set_debug },
201 'execute!' => \$execute,
206 if ($ARGV[0] and $ARGV[0] ne "") {
207 open $msgfile, $ARGV[0];
210 Error "Unable to open file ($ARGV[0]): $!\n";
214 $msgfile = SaveStdin;
217 verbose "Starting MAPS....";
219 my ($username, $user_email) = SetContext $userid
220 or die "$userid is not a registered MAPS user\n";
222 ProcessMsgs $msgfile, $username, $user_email;