-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_whitelist
343 lines (301 loc) · 8.97 KB
/
auto_whitelist
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
=head1 NAME
auto_whitelist - whitelist override for other qpsmtpd plugins
=head1 DESCRIPTION
The uber_whitelist_soft plugin allows whitelisting of incoming email
across multiple vectors
recipient address
sender host
sender helo
sender envelope address
sender envelope domain
plugin also auto-adds whitelisted sender for rcpts from all outbound email.
storage and lookup is via SQLite database, by default placed in
/var/lib/qpsmtpd/auto_whitelist/whitelist.db. on first run plugin
will initialize the db and populate with existing whitelisted values from
whitelisthosts
whitelisthelo
whitelistsenders (split between senders and domains on import)
whitelistrcpt
loosely based on Gavin Carr's 'whitelist_soft', which itself was based on
Devin Carraway's 'whitelist' plugin.
=head1 DEPENDENCIES
libsqlite3
DBI
DBD::SQLite
Qpsmtpd (obviously)
=head1 AUTHOR
mark warren bracher <bracher@gmail.com>.
loosely based on 'whitelist_soft' by Gavin Carr <gavin@openfusion.com.au>.
=head1 TODO
* fixup import/bootstrap on first run
* don't auto whitelist addresses from domains in rcpthosts
they should be authenticated anyways, so no greylisting
* parametrize whitelist tables by rcpt, and support per-recipient lists
for auto, rcpt should be the sender that originated the outbound email
=cut
use strict;
use warnings;
use DBI;
use DBD::SQLite;
use Qpsmtpd::Constants;
my $VERSION = 0.01;
my $ME = 'auto_whitelist';
our %DEFAULTS = (
dbdir => "/var/lib/qpsmtpd/$ME",
dbfile => 'whitelist.db',
);
our $uws_config;
sub _config {
my $self = shift;
$uws_config ||= +{
%DEFAULTS,
map { split /\s+/, $_, 2 } $self->qp->config($ME)
};
}
sub register {
my($self, $qp) = @_;
# don't really need it _now_, but forces init and import if necessary
my $dbh = $self->_dbh;
$self->register_hook('connect', 'check_host');
$self->register_hook('helo', 'check_helo');
$self->register_hook('ehlo', 'check_helo');
$self->register_hook('mail', 'check_sender');
$self->register_hook('rcpt', 'check_rcpt');
}
our $dbh;
sub _dbh {
return $dbh if $dbh;
my $self = shift;
my $config = $self->_config;
my $file = $config->{dbdir} . '/' . $config->{dbfile};
my $new = !-e $file;
$dbh = DBI->connect(
"dbi:SQLite:dbname=$file",
undef, undef,
{ AutoCommit => 1,
RaiseError => 1,
sqlite_unicode => 1,
}
);
if ($new) {
$self->_initdb($dbh);
}
return $dbh;
}
sub _initdb {
my($self, $dbh) = @_;
$self->_initdb_hosts($dbh);
$self->_initdb_helos($dbh);
$self->_initdb_senders($dbh);
$self->_initdb_domains($dbh);
$self->_initdb_rcpts($dbh);
}
sub _check_sql_count {
my($self, $sql, $bind) = @_;
my $sth = $self->_dbh->prepare_cached($sql);
$sth->execute(@$bind);
my $count;
$sth->bind_columns(\$count);
$sth->fetch;
$sth->finish;
return $count;
}
sub _initdb_hosts {
my($self, $dbh) = @_;
my $create = <<'CREATE';
CREATE TABLE whitelist_hosts (
host text not null primary key,
imported integer not null default 0,
created timestamp not null default CURRENT_TIMESTAMP,
comment text
)
CREATE
$dbh->do($create);
my $insert = <<'INSERT';
insert into whitelist_hosts (host, imported) values (?,?)
INSERT
my $sth = $dbh->prepare($insert);
# bootstrap with data from whitelist(_soft) text configs
foreach my $value ($self->qp->config('whitelisthosts')) {
$value = lc $value;
$sth->execute($value, 1);
}
$sth->finish;
}
our $host_sql = <<'HOST_SQL';
select count(1) from whitelist_hosts where host = ?
HOST_SQL
sub check_host {
my($self, $trans, $rcpt) = @_;
my $ip = $self->qp->connection->remote_ip || return (DECLINED);
# From tcpserver
if (exists $ENV{WHITELISTCLIENT}) {
$self->qp->connection->notes('whitelistclient', 1);
return (OK);
}
if ($self->_check_sql_count($host_sql, [$ip])) {
$self->qp->connection->notes('whitelisthost', 1);
return (OK);
}
# test partial ips
my @pieces = split('.', $ip);
for my $i (3..1) {
my $test = join('.', @pieces[1..$i]) . '.';
if ($self->_check_sql_count($host_sql, [$test])) {
$self->qp->connection->notes('whitelisthost', 1);
return (OK);
}
}
return (DECLINED);
}
sub _initdb_helos {
my($self, $dbh) = @_;
my $create = <<'CREATE';
CREATE TABLE whitelist_helos (
helo text not null primary key,
imported integer not null default 0,
created timestamp not null default CURRENT_TIMESTAMP,
comment text
)
CREATE
$dbh->do($create);
my $insert = <<'INSERT';
insert into whitelist_helos (helo, imported) values (?,?)
INSERT
my $sth = $dbh->prepare($insert);
# bootstrap with data from whitelist(_soft) text configs
foreach my $value ($self->qp->config('whitelisthelo')) {
$value = lc $value;
$sth->execute($value, 1);
}
$sth->finish;
}
our $helo_sql = <<'HOST_SQL';
select count(1) from whitelist_helos where helo = ?
HOST_SQL
sub check_helo {
my($self, $trans, $helo) = @_;
my $check = lc $helo;
if ($self->_check_sql_count($helo_sql, [$check])) {
$self->qp->connection->notes('whitelisthelo', 1);
return (OK);
}
return (DECLINED);
}
sub _initdb_senders {
my($self, $dbh) = @_;
my $create = <<'CREATE';
CREATE TABLE whitelist_senders (
sender text not null primary key,
auto integer not null default 0,
imported integer not null default 0,
created integer not null default CURRENT_TIMESTAMP,
comment text
)
CREATE
$dbh->do($create);
my $insert = <<'INSERT';
insert into whitelist_senders (sender, imported) values (?,?)
INSERT
my $sth = $dbh->prepare($insert);
# bootstrap with data from whitelist(_soft) text configs
foreach my $value ($self->qp->config('whitelistsenders')) {
# skip domain whitelists, we'll handle them separately
next unless $value =~ m/@/;
$value = lc $value;
$sth->execute($value, 1);
}
$sth->finish;
}
sub _initdb_domains {
my($self, $dbh) = @_;
my $create = <<'CREATE';
CREATE TABLE whitelist_sender_domains (
domain text not null primary key,
imported integer not null default 0,
created timestamp not null default CURRENT_TIMESTAMP,
comment text
)
CREATE
$dbh->do($create);
my $insert = <<'INSERT';
insert into whitelist_sender_domains (domain, imported) values (?,?)
INSERT
my $sth = $dbh->prepare($insert);
# bootstrap with data from whitelist(_soft) text configs
foreach my $value ($self->qp->config('whitelistsenders')) {
# skip address whitelists, only want domains here...
next if $value =~ m/@/;
$value = lc $value;
$sth->execute($value, 1);
}
$sth->finish;
}
our $sender_sql = <<'SENDER_SQL';
select count(1) from whitelist_senders where sender = ?
SENDER_SQL
our $domain_sql = <<'DOMAIN_SQL';
select count(1) from whitelist_sender_domains where domain = ?
DOMAIN_SQL
sub check_sender {
my($self, $trans, $sender) = @_;
my $host = lc($sender->host);
if ($self->_check_sql_count($domain_sql, [$host])) {
$trans->notes('whitelistsender', 1);
return (OK);
}
my $addr = lc($sender->user.'@'.$host);
if ($self->_check_sql_count($sender_sql, [$addr])) {
$trans->notes('whitelistsender', 1);
return (OK);
}
return (DECLINED);
}
sub _initdb_rcpts {
my($self, $dbh) = @_;
my $create = <<'CREATE';
CREATE TABLE whitelist_rcpts (
rcpt text not null primary key,
imported integer not null default 0,
created timestamp not null default CURRENT_TIMESTAMP,
comment text
)
CREATE
$dbh->do($create);
my $insert = <<'INSERT';
insert into whitelist_rcpts (rcpt, imported) values (?,?)
INSERT
my $sth = $dbh->prepare($insert);
# bootstrap with data from whitelist(_soft) text configs
foreach my $value ($self->qp->config('whitelistrcpt')) {
$value = lc $value;
$sth->execute($value, 1);
}
$sth->finish;
}
our $auto_sender_sql = <<'AUTO_SENDER';
insert into whitelist_senders (sender, auto) values (?,?)
AUTO_SENDER
our $rcpt_sql = <<'RCPT_SQL';
select count(1) from whitelist_rcpts where rcpt = ?
RCPT_SQL
sub check_rcpt {
my($self, $trans, $rcpt) = @_;
my $addr = lc($rcpt->user.'@'.$rcpt->host);
# outbound relay, add recipient to sender whitelist
if ($self->qp->connection->relay_client) {
# check that addr does not yet exist
unless ($self->_check_sql_count($sender_sql, [$addr])) {
$self->_dbh->do($auto_sender_sql, {}, $addr, 1);
}
return (DECLINED);
}
return (OK) if $self->_check_sql_count($rcpt_sql, [$addr]);
if ($self->_check_sql_count($rcpt_sql, [$addr])) {
my $note = $trans->notes('whitelistrcpt');
$trans->notes('whitelistrcpt', ++$note);
return (OK);
}
return (DECLINED);
}
1;