-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspamd.pl
155 lines (136 loc) · 4.07 KB
/
spamd.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
#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (c) 2015, Giovanni Bechis
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#------------------------------------------------------------------------------
# ex:ts=8 sw=4:
package spamd;
use strict;
use warnings;
use Data::Dumper;
use Getopt::Std;
use base qw(Net::Server::PreFork);
use Net::Server::Daemonize qw(daemonize);
use Unix::Syslog qw(:macros :subs);
my %opts = ();
my $daemonize = 0;
my $pid;
my $port=2525;
my $listenip='*';
my $user=undef;
my $group=undef;
my $timeout=300;
# Print slowly
sub slowprint {
my $text = shift(@_);
my $i = 0;
for ( $i .. length($text) ) {
print substr($text, $i, 1);
sleep 2;
$i++;
}
print "\015\012";
}
# Process smtp requests
sub process_request {
my $self = shift;
my $start_time = time;
my $elapsed_time;
# print Dumper $self;
local $SIG{'ALRM'} = sub { do_log_and_die($self->{'server'}->{'peeraddr'}, undef, "Timed out") };
my $previous_alarm = alarm($timeout);
do_log($self->{'server'}->{'peeraddr'}, undef);
slowprint "220 spamd IP-based SPAM blocker";
while (<STDIN>) {
s/[\r\n]+$//;
slowprint "250 spamd Hello, pleased to meet you" if /HELO.*/i;
slowprint "250 2.0.0: Ok" if /(MAIL FROM:.*)|(RCPT TO:.*)/i;
alarm($timeout);
last if /(QUIT)|(^\.$)/i;
}
slowprint "221 2.0.0: Bye";
$elapsed_time = time - $start_time;
do_log($self->{'server'}->{'peeraddr'}, $elapsed_time);
alarm($previous_alarm);
}
sub do_log {
my $ip_addr = shift;
my $elapsed_time = shift;
my $msg = shift;
if ( not defined $elapsed_time ) {
$elapsed_time = 0;
}
my $id = 'spamd.pl';
my $fac = 'mail';
$fac =~ /^[A-Za-z0-9_]+\z/
or die "Suspicious syslog facility name: $fac";
my $syslog_facility_num = eval("LOG_\U$fac"); ## no critic
$syslog_facility_num =~ /^\d+\z/
or die "Unknown syslog facility name: $fac";
openlog($id, LOG_PID | LOG_NDELAY, $syslog_facility_num);
if ( $elapsed_time ne 0 ) {
syslog LOG_INFO, "Spammer %s stuck for %d seconds", $ip_addr, $elapsed_time;
} elsif ( defined $msg ) {
syslog LOG_INFO, "Spammer %s %s", $ip_addr, $msg;
} else {
syslog LOG_INFO, "Spammer %s connected", $ip_addr;
}
closelog();
}
sub do_log_and_die {
my $ip = shift;
my $time = shift;
my $msg = shift;
do_log($ip, $time, $msg);
die;
}
# option parsing
getopts('dg:p:t:u:', \%opts);
if ( defined $opts{'d'} ) {
$daemonize = 1;
if (defined $opts{'g'} and defined $opts{'u'} ) {
$user=$opts{'u'};
$group=$opts{'g'};
} else {
die("Daemon mode needs user [-u] and group [-g] parameters");
}
}
# Set a port different from default
if ( defined $opts{'p'} ) {
$port = $opts{'p'};
}
# Set a timeout for alarm(3)
if ( defined $opts{'t'} ) {
$timeout = $opts{'t'};
}
# Daemonize if requested
if ($daemonize eq 1) {
daemonize(
$user,
$group,
$pid
);
}
spamd->run(port => $port, ipv => $listenip);
1;