-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPFXCheck.pm
188 lines (155 loc) · 5.64 KB
/
PFXCheck.pm
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
package PFXCheck;
use strict;
use warnings;
use Redis::Client;
our $VERSION = '0.1';
my $REDIS_HOST = "localhost";
my $REDIS_PORT = 6379;
my $DAY_LIMIT = 500; # this is the default daily limit, if no value "dayLimit" in redis is set
my $REJECT_MESSAGE = "REJECT 590 daily mail limit reached";
sub new(){
my $class = shift;
my $self = {};
&main::dolog("debug", "$class instantiated.");
$self->{'dbh'} = undef;
bless $self;
}
sub do_check($){
&main::dolog("debug", "Entering do_check routine.");
my $self = shift @_;
my $querydata = shift @_;
my $dayLimit = $DAY_LIMIT;
# Check if all data was sent
unless ($querydata->{'sasl_username'}){
&main::dolog("debug", "Ignoring no SASL authenticated mail request.");
return "DUNNO";
}
my $sasl_username = "$querydata->{'sasl_username'}";
# number of recipient
my $recipient = "";
my $nbrOfRecipients = 1;
if ($querydata->{'recipient'}){
$recipient = "$querydata->{'recipient'}";
$nbrOfRecipients = $recipient =~ tr/,//;
$nbrOfRecipients = $nbrOfRecipients + 1;
}
&main::dolog("debug", "Checking for: sasl_username=$sasl_username");
# Connect to DB
$self->{'dbh'} = Redis::Client->new(
server => "$REDIS_HOST:$REDIS_PORT",
reconnect => 2,
every => 100000
) or die "Cant connect to redis server : $!";
&main::dolog("debug", "Redis DB connection established.");
if ($self->{'dbh'}->hexists($sasl_username, "dayLimit")) {
# user is in redis and has a day limit value
$dayLimit = $self->{'dbh'}->hget($sasl_username, 'dayLimit');
}
unless ($self->{'dbh'}->hexists($sasl_username, "dayCounter")) {
# if no day counter is set yet (first mail of the day), set the counter to 0
$self->{'dbh'}->hset($sasl_username, 'dayCounter' => 0);
}
unless ($self->{'dbh'}->hexists($sasl_username, "totalCounter")) {
# if no total counter is set yet (first mail since resetting the total counter), set the counter to 0
$self->{'dbh'}->hset($sasl_username, 'totalCounter' => 0);
}
my $dayCounter = $self->{'dbh'}->hget($sasl_username, 'dayCounter');
my $totalCounter = $self->{'dbh'}->hget($sasl_username, 'totalCounter');
if ($dayCounter >= 0 and $dayCounter <= $dayLimit and $dayCounter + $nbrOfRecipients <= $dayLimit){
# update limited User who hasnt reached MAXMAIL
my $newDayCounter = $dayCounter + $nbrOfRecipients;
my $newTotalCounter = $totalCounter + $nbrOfRecipients;
$self->{'dbh'}->hset($sasl_username, 'dayCounter' => $newDayCounter);
$self->{'dbh'}->hset($sasl_username, 'totalCounter' => $newTotalCounter);
&main::dolog("info", "PERMIT: sasl_username=$sasl_username --> day counter: $newDayCounter; day limit: $dayLimit; total-counter (since last reset): $newTotalCounter");
undef $self->{'dbh'};
return "DUNNO"
} else {
# limited User has reached MAXMAIL
&main::dolog("warning", "DENY: sasl_username=$sasl_username --> day counter: $dayCounter; day limit: $dayLimit; Nbr of mails not sent because of day limit: $nbrOfRecipients; total-counter (since last reset): $totalCounter");
undef $self->{'dbh'};
return "$REJECT_MESSAGE";
}
}
sub getStats() {
my $self = shift @_;
# Connect to DB
$self->{'dbh'} = Redis::Client->new(
server => "$REDIS_HOST:$REDIS_PORT",
reconnect => 2,
every => 100000
) or die "Cant connect to redis server : $!";
my $stats = "sender-name\t\tday-limit\tday-counter\ttotal-counter\n----------------------------------------------------------------------\n";
# get all keys
my @keys = $self->{'dbh'}->keys( '*' );
# loop all keys
foreach my $senderName (sort @keys) {
my $dayLimit = $DAY_LIMIT;
my $dayCounter = 0;
my $totalCounter = 0;
if($self->{'dbh'}->hexists($senderName, 'dayLimit')){
$dayLimit = $self->{'dbh'}->hget($senderName, 'dayLimit');
}
if($self->{'dbh'}->hexists($senderName, 'dayCounter')){
$dayCounter = $self->{'dbh'}->hget($senderName, 'dayCounter');
}
if($self->{'dbh'}->hexists($senderName, 'totalCounter')){
$totalCounter = $self->{'dbh'}->hget($senderName, 'totalCounter');
}
$stats = $stats . "$senderName\t$dayLimit\t\t$dayCounter\t\t$totalCounter\n";
}
undef $self->{'dbh'};
return $stats;
}
sub resetDayCounter() {
my $self = shift @_;
# Connect to DB
$self->{'dbh'} = Redis::Client->new(
server => "$REDIS_HOST:$REDIS_PORT",
reconnect => 2,
every => 100000
) or die "Cant connect to redis server : $!";
# get all keys
my @keys = $self->{'dbh'}->keys( '*' );
# loop all keys
foreach my $senderName (@keys) {
$self->{'dbh'}->hdel($senderName,'dayCounter');
}
undef $self->{'dbh'};
return 0;
}
sub resetTotalCounter() {
my $self = shift @_;
# Connect to DB
$self->{'dbh'} = Redis::Client->new(
server => "$REDIS_HOST:$REDIS_PORT",
reconnect => 2,
every => 100000
) or die "Cant connect to redis server : $!";
# get all keys
my @keys = $self->{'dbh'}->keys( '*' );
# loop all keys
foreach my $senderName (@keys) {
$self->{'dbh'}->hdel($senderName,'totalCounter');
}
undef $self->{'dbh'};
return 0;
}
sub deleteAllKeys() {
my $self = shift @_;
# Connect to DB
$self->{'dbh'} = Redis::Client->new(
server => "$REDIS_HOST:$REDIS_PORT",
reconnect => 2,
every => 100000
) or die "Cant connect to redis server : $!";
# get all keys
my @keys = $self->{'dbh'}->keys( '*' );
# loop all keys
foreach my $senderName (@keys) {
$self->{'dbh'}->del($senderName);
}
undef $self->{'dbh'};
return 0;
}
1;