File: check_badmailfrom

package info (click to toggle)
qpsmtpd 0.84-9
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,376 kB
  • sloc: perl: 8,012; sh: 382; makefile: 61
file content (61 lines) | stat: -rw-r--r-- 1,802 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- perl -*-

=head1 NAME

check_badmailfrom - checks the badmailfrom config, with per-line reasons

=head1 DESCRIPTION

Reads the "badmailfrom" configuration like qmail-smtpd does.  From the
qmail-smtpd docs:

"Unacceptable envelope sender addresses.  qmail-smtpd will reject every
recipient address for a message if the envelope sender address is
listed in badmailfrom.  A line in badmailfrom may be of the form
@host, meaning every address at host."

You may optionally include a message after the sender address (leave a space),
which is used when rejecting the sender.

=head1 NOTES

According to the SMTP protocol, we can't reject until after the RCPT
stage, so store it until later.

=cut

# TODO: add the ability to provide a custom default rejection reason

sub hook_mail {
  my ($self, $transaction, $sender, %param) = @_;

  my @badmailfrom = $self->qp->config("badmailfrom")
    or return (DECLINED);

  return (DECLINED) unless ($sender->format ne "<>"
                            and $sender->host && $sender->user);

  my $host = lc $sender->host;
  my $from = lc($sender->user) . '@' . $host;

  for my $config (@badmailfrom) {
    my ($bad, $reason) = $config =~ /^\s*(\S+)(?:\s*(.*))?$/;
    $reason = "sorry, your envelope sender is in my badmailfrom list" unless $reason;
    next unless $bad;
    $bad = lc $bad;
    $self->log(LOGWARN, "Bad badmailfrom config: No \@ sign in $bad") and next unless $bad =~ m/\@/;
    $transaction->notes('badmailfrom', $reason)
      if ($bad eq $from) || (substr($bad,0,1) eq '@' && $bad eq "\@$host");
  }
  return (DECLINED);
}

sub hook_rcpt {
  my ($self, $transaction, $rcpt, %param) = @_;
  my $note = $transaction->notes('badmailfrom');
  if ($note) {
    $self->log(LOGINFO, $note);
    return (DENY, $note);
  }
  return (DECLINED);
}