Debian Codegraph @

plugins/rcpt_map

raw
1 #!perl -w
2
3 =head1 NAME
4
5 rcpt_map - check recipients against recipient map
6
7 =head1 DESCRIPTION
8
9 B<rcpt_map> reads a list of adresses, return codes and comments
10 from the supplied config file. Adresses are compared with I<eq lc($rcpt)>.
11 The recipient addresses are checked against this list, and if the first
12 matches, the return code from that line and the comment are returned to
13 qpsmtpd. Return code can be any valid plugin return code from
14 L<Qpsmtpd::Constants>. Matching is always done case insenstive.
15
16 When the given map file changes on disk, it is re-read in the pre-connection
17 hook.
18
19 =head1 ARGUMENTS
20
21 The C<file MAP> and C<domain NAME> arguments are required. The default value of
22 the C<default> argument is C<DENY=No_such_user.> (see below why C<_>).
23
24 =over 4
25
26 =item domain NAME
27
28 If the recipient address does not match this domain name NAME, this plugin will
29 return C<DECLINED>
30
31 =item file MAP
32
33 Use the config file as map file, format as explained below
34
35 =item default CODE[=MSG]
36
37 Use CODE as default return code (and return MSG as message) if a recipient
38 was B<not> found in the map. Since we can't use spaces in MSG, every C<_>
39 is replaced by a space, i.e. use C<DENY=User_not_found> if you want a deny
40 message C<User not found>.
41
42 =back
43
44 =head1 CONFIG FILE
45
46 The config file contains lines with an address, a return code and a comment,
47 which will be returned to the sender, if the code is not OK or DECLINED.
48 Example:
49
50 # example_org_map - config for rcpt_map plugin
51 [email protected] OK
52 [email protected] OK
53 [email protected] DENY User not found.
54
55 =head1 NOTES
56
57 We're currently running this plugin like shown in the following example.
58
59 Excerpt from the C<plugins> config file:
60
61 ## list of valid users, config in /srv/qpsmtpd/config/rcpt_regexp
62 ## ... except for "*@example.org":
63 rcpt_regexp
64 ## only for "@example.org":
65 rcpt_map domain example.org file /srv/qpsmtpd/config/map_example_org
66
67 And the C<rcpt_regexp> config file:
68
69 ### "example.org" addresses are checked later by the rcpt_map
70 ### plugin, return DECLINED here:
71 /^.*\@example\.org$/ DECLINED
72 ### all other domains just check for valid users, the validity
73 ### of the domain is checked by the rcpt_ok plugin => never use
74 ### something else than "DENY" or "DECLINED" here!
75 /^(abuse|postmaster)\@/ DECLINED
76 /^(me|you)\@/ DECLINED
77 /^.*$/ DENY No such user.
78
79 =head1 COPYRIGHT AND LICENSE
80
81 Copyright (c) 2009 Hanno Hecker
82
83 This plugin is licensed under the same terms as the qpsmtpd package itself.
84 Please see the LICENSE file included with qpsmtpd for details.
85
86 =cut
87
88 use Qpsmtpd::Constants;
89
90 our %map;
91
92 sub register {
93 my ($self, $qp, %args) = @_;
94 foreach my $arg (qw(domain file default)) {
95 next unless exists $args{$arg};
96 if ($arg eq "default") {
97 my ($code, $msg) = split /=/, $args{$arg};
98
99 $code = Qpsmtpd::Constants::return_code($code);
100 die "Not a valid constant for 'default' arg"
101 unless defined $code;
102
103 $msg or $msg = "No such user.";
104 $msg =~ s/_/ /g;
105
106 $self->{_default} = [$code, $msg];
107 }
108 else {
109 $self->{"_$arg"} = $args{$arg};
110 }
111 }
112
113 $self->{_default}
114 or $self->{_default} = [DENY, "No such user."];
115
116 $self->{_file}
117 or die "No map file given...";
118
119 $self->{_domain}
120 or die "No domain name given...";
121 $self->{_domain} = lc $self->{_domain};
122
123 $self->log(LOGDEBUG,
124 "Using map " . $self->{_file} . " for domain " . $self->{_domain});
125 %map = $self->read_map(1);
126 die "Empty map file " . $self->{_file}
127 unless keys %map;
128 }
129
130 sub hook_pre_connection {
131 my $self = shift;
132 my ($time) = (stat($self->{_file}))[9] || 0;
133 if ($time > $self->{_time}) {
134 my %temp = $self->read_map();
135 keys %temp
136 or return DECLINED;
137 %map = %temp;
138 }
139 return DECLINED;
140 }
141
142 sub read_map {
143 my $self = shift;
144 my %hash = ();
145 open F, $self->{_file}
146 or do { $_[0] ? die "ERROR opening: $!" : return (); };
147
148 ($self->{_time}) = (stat(F))[9] || 0;
149
150 my $line = 0;
151 while (<F>) {
152 ++$line;
153 s/^\s*//;
154 next if /^#/;
155 next unless $_;
156 my ($addr, $code, $msg) = split / /, $_, 3;
157 next unless $addr;
158
159 unless ($code) {
160 $self->log(LOGERROR,
161 "No constant in line $line in " . $self->{_file});
162 next;
163 }
164 $code = Qpsmtpd::Constants::return_code($code);
165 unless (defined $code) {
166 $self->log(LOGERROR,
167 "Not a valid constant in line $line in " . $self->{_file});
168 next;
169 }
170 $msg or $msg = "No such user.";
171 $hash{$addr} = [$code, $msg];
172 }
173 return %hash;
174 }
175
176 sub hook_rcpt {
177 my ($self, $transaction, $recipient) = @_;
178 return (DECLINED)
179 unless $recipient->host && $recipient->user;
180
181 return (DECLINED)
182 unless lc($recipient->host) eq $self->{_domain};
183
184 my $rcpt = lc $recipient->user . '@' . lc $recipient->host;
185 return (@{$self->{_default}})
186 unless exists $map{$rcpt};
187
188 return @{$map{$rcpt}};
189 }