postfix filter incoming mails based on 'mail from' and 'rcpt to'

Add in a restriction class. For example:

/etc/postfix/main.cf:
smtpd_recipient_restrictions =
    check_recipient_access hash:/etc/postfix/recipient_access

smtpd_restriction_classes = no_russians
no_russians = check_sender_access pcre:/etc/postfix/no_russians


/etc/postfix/recipient_access:
[email protected]     no_russians
[email protected]     no_russians

/etc/postfix/no_russians:
/\.ru$/ REJECT

Basically copy/pasting from http://code.metager.de/source/xref/postfix-debian/examples/smtpd-policy/greylist.pl#257 and adapting as we go, here is a simple Perl script to implement no_ru.pl as a simple check_policy_service script. See http://www.postfix.org/SMTPD_POLICY_README.html for how to hook it in.

Untested, YMMV, etc. You probably need to use and perhaps initialize a few facilities like syslog -- try this from the command line first.

# Unbuffer standard output.
#
select((select(STDOUT), $| = 1)[0]);

#
# Receive a bunch of attributes, evaluate the policy, send the result.
#
%attr = ();
$ru_sender = $ru_rcpt = 0;
while (<STDIN>) {
  if (/^\s*sender=.*\.ru\n/i) {
     $ru_sender = 1;
  } elsif (/^\s*recipient=.*\.ru$/i) {
     $ru_rcpt = 1;
  } elsif ($_ eq "\n") {
    if ($verbose) {
      syslog $syslog_priority, "ru_sender %i, ru_rcpt %i", $ru_sender, $ru_rcpt;
    }
    $action = ($ru_sender && $ru_rcpt) ? "reject" : "dunno";
    syslog $syslog_priority, "Action: %s", $action if $verbose;
    print STDOUT "action=$action\n\n";
    %attr = ();
  } else {
    chop;
    syslog $syslog_priority, "warning: ignoring garbage: %.100s", $_;
  }
}

Tags:

Smtp

Postfix