Debian Codegraph @

plugins/rcpt_regexp

raw
1 #!perl -w
2
3 =head1 NAME
4
5 rcpt_regexp - check recipients against a list of regular expressions
6
7 =head1 DESCRIPTION
8
9 B<rcpt_regexp> reads a list of regular expressions, return codes and comments
10 from the I<rcpt_regexp> config file. If the regular expression does NOT match
11 I<m#^(/.*/)$#>, it is used as a string which is compared with I<eq lc($rcpt)>.
12 The recipient addresses are checked against this list, and if the first
13 matches, the return code from that line and the comment are returned to
14 qpsmtpd. Return code can be any valid plugin return code from
15 L<Qpsmtpd::Constants>. Matching is always done case insenstive.
16
17 =head1 CONFIG FILE
18
19 The config file I<rcpt_regexp> contains lines with a perl RE, including the
20 "/"s, a return code and a comment, which will be returned to the sender, if
21 the code is not OK or DECLINED. Example:
22
23 # rcpt_regexp - config for rcpt_regexp plugin
24 [email protected] OK Accepting mail
25 /^user\d+\@doma\.in$/ OK Accepting mail
26 [email protected] DENY User not found.
27 /^unused\@.*/ DENY User not found.
28 /^.*$/ DECLINED Fall through to next rcpt plugin
29
30 =head1 NOTE
31
32 The C<rcpt_regexp> config file should be writeable by trusted users only: the
33 regexes are compiled with I<eval()>.
34
35 =head1 COPYRIGHT AND LICENSE
36
37 Copyright (c) 2005 Hanno Hecker
38
39 This plugin is licensed under the same terms as the qpsmtpd package itself.
40 Please see the LICENSE file included with qpsmtpd for details.
41
42 =cut
43
44 use Qpsmtpd::Constants;
45
46 sub hook_rcpt {
47 my ($self, $transaction, $recipient) = @_;
48 return (DECLINED)
49 unless $recipient->host && $recipient->user;
50
51 my $rcpt = lc $recipient->user . '@' . $recipient->host;
52 my ($re, $const, $comment, $str, $ok, $err);
53
54 foreach ($self->qp->config("rcpt_regexp")) {
55 s/^\s*//;
56 ($re, $const, $comment) = split /\s+/, $_, 3;
57 $str = undef;
58 if ($re =~ m#^/(.*)/$#) {
59 $re = $1;
60 $ok = eval { $re = qr/$re/i; };
61 if ($@) {
62 ($err = $@) =~ s/\s*at \S+ line \d+\.\s*$//;
63 $self->log(LOGWARN, "REGEXP '$re' not valid: $err");
64 next;
65 }
66 $re = $ok;
67 }
68 else {
69 $str = lc $re;
70 }
71
72 unless (defined $const) {
73 $self->(LOGWARN, "rcpt_regexp - no return code");
74 next;
75 }
76
77 $ok = $const;
78 $const = Qpsmtpd::Constants::return_code($const);
79 unless (defined $const) {
80 $self->log(LOGWARN,
81 "rcpt_regexp - '$ok' is not a valid "
82 . "constant, ignoring this line"
83 );
84 next;
85 }
86
87 if (defined $str) {
88 next unless $str eq $rcpt;
89 $self->log(LOGDEBUG, "String $str matched $rcpt, returning $ok");
90 }
91 else {
92 next unless $rcpt =~ $re;
93 $self->log(LOGDEBUG, "RE $re matched $rcpt, returning $ok");
94 }
95
96 return ($const, $comment);
97 }
98 return (DECLINED);
99 }