-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathlookup-domain.pl
executable file
·219 lines (207 loc) · 5.62 KB
/
lookup-domain.pl
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
#!/usr/local/bin/perl
# Returns the domain ID for some user, if the domain has spam enabled and
# if the user is not approaching his quota
$no_acl_check++;
use Socket;
# Parse command line args
$exitcode = 73;
$port = 11000;
while(@ARGV) {
my $a = shift(@ARGV);
if ($a eq "--exitcode") {
$exitcode = shift(@ARGV);
}
elsif ($a eq "--port") {
$port = shift(@ARGV);
}
elsif ($a !~ /^-/) {
$username = $a;
}
else {
die "usage: lookup-domain.pl --exitcode number <username>";
}
}
$username || die "Missing username parameter";
# Get the message size
while($got = read(STDIN, $buf, 32768)) {
$size += $got;
}
$margin = $size*2+5*1024*1024;
$quota_margin = 100*1024; # Bounce if message would get this close to quota
# First, try connecting to the lookup-domain-daemon.pl process. Only wait for
# 30 seconds, in case it has hung.
$connect_timed_out = 0;
local $SIG{ALRM} = sub { $connect_timed_out = 1 };
socket(DAEMON, PF_INET, SOCK_STREAM, getprotobyname("tcp"));
alarm(30);
$rv = connect(DAEMON, pack_sockaddr_in($port, inet_aton("127.0.0.1")));
if ($rv) {
select(DAEMON); $| = 1; select(STDOUT);
print DAEMON $username,"\n";
$fromdaemon = <DAEMON>;
$fromdaemon =~ s/\r|\n//g;
close(DAEMON);
}
alarm(0);
if ($connect_timed_out) {
print STDERR "Timeout connecting to lookup-domain-daemon.pl\n";
}
if ($fromdaemon && !$connect_timed_out) {
# We have an answer from the server process
($did, $dname, $spam, $spamc, $quotaleft) = split(/\t/, $fromdaemon);
if ($did && $quotaleft ne "UNLIMITED" &&
$quotaleft-$size-$quota_margin <= 0) {
# Quota has been reached, bounce mail
print STDERR "Disk quota for $username has been reached.\n";
print $did,"\n";
exit($exitcode);
}
elsif (!$did || !$spam) {
# No such user, or user's domain doesn't have spam enabled -
# don't do spam check
}
elsif ($spamc || $quotaleft eq "UNLIMITED") {
# Domain is using spamc, or user has no quota, or quota disabled
# Do spam check.
print $did,"\n";
}
elsif ($quotaleft < $margin) {
# Too close to quota - don't check for spam, so return
# no domain ID
}
else {
# Do spam check
print $did,"\n";
}
exit(0);
}
# Open the cache DBM
$cachefile = "$ENV{'WEBMIN_VAR'}/lookup-domain-cache";
eval "use SDBM_File";
dbmopen(%usercache, $cachefile, 0700);
eval "\$usercache{'1111111111'} = 1";
if ($@) {
dbmclose(%usercache);
eval "use NDBM_File";
dbmopen(%usercache, $cachefile, 0700);
}
# Check our cache first, in case we have just done this user
$now = time();
if (defined($usercache{$username})) {
($cachespam, $cachequota, $cacheuquota, $cachetime, $cacheclient) =
split(/ /, $usercache{$username});
$cacheclient ||= "spamassassin";
if ($now - $cachetime < 60*60) {
if ($cachequota &&
$cacheuquota+$size+$quota_margin >= $cachequota) {
# At or over quota. Actually re-check for real though,
# to avoid bouncing mail un-necessarily
}
elsif (!$cachespam) {
# Domain doesn't have spam enabled, so don't do check
$cacheuquota += $size;
&update_cache();
exit(0);
}
elsif ($cacheclient eq "spamc") {
# Using spamc, so quotas don't matter
$cacheuquota += $size;
&update_cache();
print $cachespam,"\n";
exit(0);
}
elsif ($cachequota && $cacheuquota+$margin >= $cachequota) {
# User is over quota, so don't do spam check
$cacheuquota += $size;
$cacheuquota = $cachequota
if ($cacheuquota > $cachequota);
&update_cache();
exit(0);
}
else {
# User is under quota, so proceed
$cacheuquota += $size;
&update_cache();
print $cachespam,"\n";
exit(0);
}
}
}
# Lookup the user for real
do './virtual-server-lib.pl';
$d = &get_user_domain($username);
if (!$d || !$d->{'spam'}) {
$cachespam = $cachequota = $cacheuquota = 0;
$cachetime = $now;
&update_cache();
exit(0);
}
# See what kind of quotas are relevant
$qmode = &mail_under_home() && &has_home_quotas() ? "home" :
&has_mail_quotas() ? "mail" : undef;
if (!$qmode) {
# None .. so run spam checks
$cachespam = $d->{'id'};
$cachequota = $cacheuquota = 0;
$cachetime = $now;
&update_cache();
print "$d->{'id'}\n";
exit(0);
}
# Check if the domain is using spamc or spamassassin
$cacheclient = &get_domain_spam_client($d);
# Check if the user is approaching his quota
@users = &list_domain_users($d, 0, 1, 0, 1);
($user) = grep { $_->{'user'} eq $username ||
&replace_atsign($_->{'user'}) eq $username } @users;
if (!$user) {
# Couldn't find him?! So do the spam check
$cachespam = $d->{'id'};
$cachequota = $cacheuquota = 0;
$cachetime = $now;
&update_cache();
print "$d->{'id'}\n";
exit(0);
}
if ($qmode eq "home") {
($quota, $uquota) = ($user->{'quota'}, $user->{'uquota'});
}
else {
($quota, $uquota) = ($user->{'mquota'}, $user->{'umquota'});
}
$bsize = "a_bsize($qmode);
$quota *= $bsize;
$uquota *= $bsize;
if ($quota && $uquota+$size+$quota_margin >= $quota) {
# Over quota - intentionally fail
print STDERR "Disk quota for $user->{'user'} of $quota blocks has been reached.\n";
print "$d->{'id'}\n";
exit($exitcode);
}
elsif ($user->{'nospam'}) {
# Spam filtering disabled for this user
$cachespam = 0;
}
elsif ($cacheclient eq "spamc") {
# Using spamc, so quotas don't matter since spam processing is run
# by a daemon
print "$d->{'id'}\n";
}
elsif ($quota && $uquota+$margin >= $quota) {
# Close to quota - don't return domain ID, to disable spam checking
}
else {
# Under quota ... do the spam check
print "$d->{'id'}\n";
}
$cachespam = $d->{'id'};
$cachequota = $quota;
$cacheuquota = $uquota;
$cachetime = $now;
&update_cache();
exit(0);
sub update_cache
{
$usercache{$username} = join(" ", $cachespam, $cachequota, $cacheuquota, $cachetime, $cacheclient);
dbmclose(%usercache);
}