-
Notifications
You must be signed in to change notification settings - Fork 36
/
daemon.pl
367 lines (347 loc) · 9.88 KB
/
daemon.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/perl
use Socket;
use POSIX ;
use DBI;
use Switch;
use threads;
use threads::shared;
use Thread::Semaphore;
use File::Basename;
my $semaphore = new Thread::Semaphore;
### CONFIGURATION SECTION
my @allowedhosts = ('127.0.0.1');
my $LOGFILE = "/var/log/send_rate_policyd.log";
chomp( my $vhost_dir = `pwd`);
my $port = 10381;
my $listen_address = '127.0.0.1';
my $s_key_type = 'domain'; #domain or email
my $dsn = "DBI:mysql:DBNAME:127.0.0.1";
my $db_user = '*********';
my $db_passwd = '***************';
my $db_table = 'email_sender_rate';
my $db_quotacol = 'message_quota';
my $db_tallycol = 'message_tally';
my $db_timecol = 'timestamp';
my $db_wherecol = 'name';
my $deltaconf = 'daily'; #daily, weekly, monthly
my $default_quota = 100; #used, when domain not yet found
### QUERY CONFIGURATION
my $sql_getquota = "SELECT $db_quotacol, $db_tallycol, $db_timecol FROM $db_table WHERE $db_wherecol = ? AND $db_quotacol > 0";
my $sql_insertquota = "INSERT INTO $db_table SET $db_wherecol = ?, $db_quotacol=?, $db_tallycol = 1, $db_timecol=0";
my $sql_updatequota = "UPDATE $db_table SET $db_tallycol = $db_tallycol + ? WHERE $db_wherecol = ?";
my $sql_resetquota = "UPDATE $db_table SET $db_tallycol = 0 , $db_timecol = ? WHERE $db_wherecol = ?";
### END OF CONFIGURATION SECTION
$0=join(' ',($0,@ARGV));
if($ARGV[0] eq "printshm"){
my $out = `echo "printshm"|nc $listen_address $port`;
print $out;
exit(0);
}
my %quotahash :shared;
my %scoreboard :shared;
my $lock:shared;
my $cnt=0;
my $proto = getprotobyname('tcp');
my $thread_count = 3;
my $min_threads = 2;
# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
my $paddr = sockaddr_in($port, inet_aton($listen_address)); #Server sockaddr_in
bind(SERVER, $paddr) or die "bind: $!";# bind to a port, then listen
listen(SERVER, SOMAXCONN) or die "listen: $!";
&daemonize;
$SIG{TERM} = \&sigterm_handler;
$SIG{HUP} = \&print_cache;
while (1) {
my $i = 0;
my @threads;
while($i < $thread_count){
#$threads[$i] = threads->new(\&start_thr)->detach();
threads->new(\&start_thr);
logger("Started thead num $i.");
$i++;
}
while(1){
sleep 5;
$cnt++;
my $r = 0;
my $w = 0;
if($cnt % 6 == 0){
lock($lock);
&commit_cache;
&flush_cache;
logger("Master: cache committed and flushed");
}
while (my ($k, $v) = each(%scoreboard)){
if($v eq 'running'){
$r++;
}else{
$w++;
}
}
if($r/($r + $w) > 0.9){
threads->new(\&start_thr);
logger("New thread started");
}
if($cnt % 150 == 0){
logger("STATS: threads running: $r, threads waiting $w.");
}
}
}
exit;
sub start_thr {
my $threadid = threads->tid();
my $client_addr;
my $client_ipnum;
my $client_ip;
my $client;
while(1){
$scoreboard{$threadid} = 'waiting';
$semaphore->down();#TODO move to non-block
$client_addr = accept($client, SERVER);
$semaphore->up();
$scoreboard{$threadid} = 'running';
if(!$client_addr){
logger("TID: $threadid accept() failed with: $!");
next;
}
my ($client_port, $client_ip) = unpack_sockaddr_in($client_addr);
$client_ipnum = inet_ntoa($client_ip);
logger("TID: $threadid accepted from $client_ipnum ...");
select($client);
$|=1;
if(grep $_ eq $client_ipnum, @allowedhosts){
my $message;
my @buf;
while(!eof($client)) {
$message = <$client>;
if($message =~ m/printshm/){
my $r=0;
my $w =0;
print $client "Printing shm:\r\n";
print $client "Domain\t\t:\tQuota\t:\tUsed\t:\t Sum \t:\tExpire\r\n";
while(($k,$v) = each(%quotahash)){
chomp(my $exp = ctime($quotahash{$k}{'expire'}));
print $client "$k\t:\t".$quotahash{$k}{'quota'}."\t:\t $quotahash{$k}{'tally'}\t:\t$quotahash{$k}{'sum'}\t:\t $exp\r\n";
}
while (my ($k, $v) = each(%scoreboard)){
if($v eq 'running'){
$r++;
}else{
$w++;
}
}
print $client "Threads running: $r, Threads waiting: $w\r\n";
last;
}elsif($message =~ m/=/){
push(@buf, $message);
next;
}elsif($message == "\r\n"){
#logger("Handle new request");
my $ret = &handle_req(@buf);
if($ret =~ m/unknown/){
last;
#New thread model - old code
# shutdown($client,2);
#?? threads->exit(0);
}else{
print $client "action=$ret\n\n";
}
@buf = ();
}else{
print $client "message not understood\r\n";
}
}
}else{
logger("Client $client_ipnum connection not allowed.");
}
shutdown($client,2);
undef $client;
logger("TID: $threadid Client $client_ipnum disconnected.");
}
undef $scoreboard{$threadid};
threads->exit(0);
}
sub handle_req {
my @buf = @_;
my $protocol_state;
my $sasl_username;
my $recipient_count;
local $/ = "\n";
foreach $aline(@buf){
my @line = split("=", $aline);
chomp(@line);
#logger("Line ". $line[0] ."=". $line[1]);
switch($line[0]){
case "protocol_state" {
chomp($protocol_state = $line[1]);
}
case "sasl_username"{
chomp($sasl_username = $line[1]);
}
case "recipient_count"{
chomp($recipient_count = $line[1]);
}
}
}
#if($recipient_count <= 5){
# $recipient_count = 1;
#}
if($protocol_state !~ m/DATA/ || $sasl_username eq "" ){
return "ok";
}
my $skey = '';
if($s_key_type eq 'domain'){
$skey = (split("@", $sasl_username))[1];
}else{
$skey = $sasl_username;
}
#TODO: Maybe i should move to semaphore!!!
lock($lock);
if(!exists($quotahash{$skey})){
logger("Looking for $skey");
if(my $dbh = DBI->connect($dsn, $db_user, $db_passwd)){
my $sql_query = $dbh->prepare($sql_getquota);
$sql_query->execute($skey);
if($sql_query->rows < 1){
$sql_query->finish();
#$dbh->disconnect;
#return "dunno";
#Add new default quota for this domain
$sql_query = $dbh->prepare($sql_insertquota);
$sql_query->execute($skey, $default_quota)
or logger("Query error while inserting new default quota: ". $sql_query->errstr);
$sql_query->finish();
#Check new quota, maybe ommit?
$sql_query = $dbh->prepare($sql_getquota);
$sql_query->execute($skey);
if($sql_query->rows < 1){
# could not find new quota, error in db?
$sql_query->finish();
$dbh->disconnect;
logger("Error could not find new default quota for $skey");
return "dunno";
}
}
while(@row = $sql_query->fetchrow_array()){
$quotahash{$skey} = &share({});
$quotahash{$skey}{'quota'} = $row[0];
if($row[1]){
$quotahash{$skey}{'tally'} = $row[1];
} else {
$quotahash{$skey}{'tally'} = 0;
}
$quotahash{$skey}{'sum'} = 0;
if($row[2]){
$quotahash{$skey}{'expire'} = $row[2];
}else{
#$quotahash{$skey}{'expire'} = calcexpire($deltaconf);
$quotahash{$skey}{'expire'} = 0;
}
undef @row;
}
$sql_query->finish();
$dbh->disconnect;
} else {
logger("Error connection to database: " . $DBI::errstr);
return "dunno";
}
}
if($quotahash{$skey}{'expire'} < time() ){
lock($lock);
$quotahash{$skey}{'sum'} = 0;
$quotahash{$skey}{'tally'} = 0;
$quotahash{$skey}{'expire'} = calcexpire($deltaconf);
my $dbh = DBI->connect($dsn, $db_user, $db_passwd);
my $sql_query = $dbh->prepare($sql_resetquota);
$sql_query->execute($quotahash{$skey}{'expire'}, $skey)
or logger("Query error: ". $sql_query->errstr);
}
if($quotahash{$skey}{'tally'} + $recipient_count > $quotahash{$skey}{'quota'}){
return "471 Message quota exceeded";
}
$quotahash{$skey}{'tally'} += $recipient_count;
$quotahash{$skey}{'sum'} += $recipient_count;
return "dunno";
}
sub sigterm_handler {
shutdown(SERVER,2);
lock($lock);
logger("SIGTERM received.\nFlushing cache...\nExiting.");
&commit_cache;
exit(0);
}
sub commit_cache {
if(my $dbh = DBI->connect($dsn, $db_user, $db_passwd)){
my $sql_query = $dbh->prepare($sql_updatequota);
while(($k,$v) = each(%quotahash)){
$sql_query->execute($quotahash{$k}{'sum'}, $k)
or logger("Query error:".$sql_query->errstr);
$quotahash{$k}{'sum'} = 0;
}
$dbh->disconnect;
} else {
logger("Error connection to database: " . $DBI::errstr);
}
}
sub flush_cache {
foreach $k(keys %quotahash){
delete $quotahash{$k};
}
}
sub print_cache {
foreach $k(keys %quotahash){
logger("$k: $quotahash{$k}{'quota'}, $quotahash{$k}{'tally'}, $quotahash{$k}{'sum'}");
}
}
sub daemonize {
my ($i,$pid);
my $mask = umask 0027;
print "Keedra SMTP Policy Daemon. Logging to $LOGFILE\n";
#Should i delete this??
#$ENV{PATH}="/bin:/usr/bin";
#chdir("/");
close STDIN;
if(!defined(my $pid=fork())){
die "Impossible to fork\n";
}elsif($pid >0){
exit 0;
}
setsid();
close STDOUT;
open STDIN, "/dev/null";
open LOG, ">>$LOGFILE" or die "Unable to open $LOGFILE: $!\n";
select((select(LOG), $|=1)[0]);
open STDERR, ">>$LOGFILE" or die "Unable to redirect STDERR to STDOUT: $!\n";
open PID, ">/var/run/".basename($0) or die $!;
print PID $$;
close PID;
umask $mask;
}
sub calcexpire{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my ($arg) = @_;
if($arg eq 'monthly'){
$exp = mktime (0, 0, 0, 1, ++$mon, $year);
}elsif($arg eq 'weekly'){
$exp = mktime (0, 0, 0, 1, $mon, $year);
}elsif($arg eq 'daily'){
$exp = mktime (0, 0, 0, ++$mday, $mon, $year);
}elsif($arg eq 'hourly'){
#mktime($sec,$min,$hour,$mday,$mon,$year);
$exp = mktime (0, 0, ++$hour, $mday, $mon, $year);
}elsif($arg eq 'minutely'){
#mktime($sec,$min,$hour,$mday,$mon,$year);
$exp = mktime (0, ++$min, $hour, $mday, $mon, $year);
}else{
$exp = mktime (0, 0, 0, 1, ++$mon, $year); #default = monthly
}
return $exp;
}
sub logger {
my ($arg) = @_;
my $time = localtime();
chomp($time);
print LOG "$time $arg\n";
}