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
|
#!/usr/bin/perl -w
# Simple script that read message from STDIN and test it on rspamd server
# using specified command.
#
# Usage: rspamc.pl [-c conf_file] [command] [-s statfile]
#
# By default rspamc.pl would read ./rspamd.conf and default command is SYMBOLS
use Socket qw(:DEFAULT :crlf);
use Term::Cap;
use Mail::Rspamd::Client;
use Mail::Rspamd::Config;
use Data::Dumper;
my %cfg = (
'conf_file' => '@CMAKE_INSTALL_PREFIX@/etc/rspamd.conf',
'command' => 'SYMBOLS',
'hosts' => ['localhost:11333', ],
'require_input' => 0,
'password' => '',
'control' => 0,
'statfile' => '',
'deliver_to'=> '',
'weight' => 1,
'imap_search' => 'ALL',
'ip' => '127.0.0.1',
);
my $terminal;
my $is_tty = 1;
$main::VERSION = '@RSPAMD_VERSION@';
sub HELP_MESSAGE {
print <<EOD;
Usage: rspamc.pl [-h host] [-H hosts_list] [-P password] [-c conf_file] [-s statfile] [-d user\@domain] [command] [path]
-h host to connect (in format host:port) or unix socket path
-H path to file that contains list of hosts
-P define control password
-c config file to parse
-s statfile to use for learn commands
Additional options:
-d define deliver-to header
-w define weight for fuzzy operations
-S define search string for IMAP operations
-i emulate that message was send from specified IP
-p pass message throught all filters
Notes:
imap format: imap:user:<username>:password:[<password>]:host:<hostname>:mbox:<mboxname>
Password may be omitted and then it would be asked in terminal
imaps requires IO::Socket::SSL
IMAP search strings samples:
ALL - All messages in the mailbox;
FROM <string> - Messages that contain the specified string in the envelope structure's FROM field;
HEADER <field-name> <string> - Messages that have a header with the specified field-name and that
contains the specified string in the text of the header (what comes after the colon);
NEW - Messages that have the \\Recent flag set but not the \\Seen flag.
This is functionally equivalent to "(RECENT UNSEEN)".
OLD - Messages that do not have the \\Recent flag set.
SEEN - Messages that have the \\Seen flag set.
SENTBEFORE <date> - Messages whose [RFC-2822] Date: header (disregarding time and timezone)
is earlier than the specified date.
TO <string> - Messages that contain the specified string in the envelope structure's TO field.
TEXT <string> - Messages that contain the specified string in the header or body of the message.
OR <search-key1> <search-key2> - Messages that match either search key (same for AND and NOT operations).
Version: @RSPAMD_VERSION@
EOD
exit;
};
sub load_hosts_file {
my $file = shift;
open (HOSTS, "< $file") or die "cannot open file $file";
$cfg{'hosts'} = [ ];
while (<HOSTS>) {
chomp;
next if $_ =~ /^\s*#/;
if ($_ =~ /^\s*(([^:]+):(\d+))\s*$/) {
push (@{ $cfg{'hosts'} }, $1);
}
elsif ($_ =~ /^\s*([^:]+)\s*$/) {
if ($cfg{'control'}) {
push (@{ $cfg{'hosts'} }, "$1:11334");
}
else {
push (@{ $cfg{'hosts'} }, "$1:11333");
}
}
elsif ($_ =~ /^\s*(\/\S*)\s*$/) {
push (@{ $cfg{'hosts'} }, "$1");
}
}
close HOSTS;
}
# Load rspamd config params
sub parse_config {
my ($is_ctrl) = @_;
my $parser = Mail::Rspamd::Config->new();
$parser->load($cfg{'conf_file'});
if (defined ($parser->{workers})) {
foreach my $worker (@{ $parser->{workers} }) {
if ($is_ctrl && $worker->{'type'} eq 'controller') {
$cfg{'hosts'} = [ $worker->{'bind_socket'} ];
$cfg{'password'} = $worker->{options}->{password};
}
elsif (!$is_ctrl && $worker->{'type'} eq 'normal') {
$cfg{'hosts'} = [ $worker->{'bind_socket'} ];
}
}
}
}
sub print_control_result {
my ($host, $res) = @_;
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "Results for host $host:\n\n";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
if ($res->{error_code} == 0) {
print "$res->{error}\n";
}
else {
print "Error occured: $res->{error_code}:\n$res->{error}\n";
}
}
sub print_rspamc_result {
my ($host, $res) = @_;
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "Results for host $host:\n\n";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
if (defined($res->{error})) {
print "Error occured: $res->{error}\n\n";
}
else {
while (my ($metric, $result) = each (%{ $res })) {
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "$metric: ";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
print "$result->{isspam}, [ $result->{score} / $result->{threshold} ]\n";
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "Symbols: ";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
print join("; ", @{ $result->{symbols} }) . "\n";
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "Action: ";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
print "$result->{action}\n";
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "Urls: ";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
print join(", ", @{ $result->{urls} }) . "\n";
foreach my $msg (@{ $result->{messages} }) {
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "Message: ";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
print "$msg\n";
}
print "\n\n";
}
}
}
sub print_item_result {
my ($file, $res) = @_;
$terminal->Tputs( 'md', 1, *STDOUT ) if $is_tty;
print "Results for item $file:\n\n";
$terminal->Tputs( 'me', 1, *STDOUT ) if $is_tty;
while (my ($host, $result) = each (%{ $res })) {
if ($cfg{control}) {
print_control_result ($host, $result);
}
else {
print_rspamc_result ($host, $result);
}
}
}
############################# Main part ###########################################
my %args;
HELP_MESSAGE() unless scalar @ARGV >= 1;
while (my $opt = shift @ARGV) {
if ($opt eq '-c') {
$args{c} = shift @ARGV;
}
elsif ($opt eq '-h') {
$args{h} = shift @ARGV;
}
elsif ($opt eq '-P') {
$args{P} = shift @ARGV;
}
elsif ($opt eq '-s') {
$args{s} = shift @ARGV;
}
elsif ($opt eq '-w') {
$args{w} = shift @ARGV;
}
elsif ($opt eq '-d') {
$args{d} = shift @ARGV;
}
elsif ($opt eq '-S') {
$args{S} = shift @ARGV;
}
elsif ($opt eq '-H') {
$args{H} = shift @ARGV;
}
elsif ($opt eq '-i') {
$args{i} = shift @ARGV;
}
elsif ($opt eq '-p') {
$args{p} = 1;
}
elsif ($opt eq '-?' || $opt eq '--help') {
HELP_MESSAGE();
}
elsif ($opt eq '--') {
last;
}
else {
unshift @ARGV,$opt;
last;
}
}
my $cmd = shift @ARGV;
my @path = shift @ARGV;
if (!defined ($cmd) || $cmd eq '') {
$cmd = 'SYMBOLS';
}
if (defined ($args{c})) {
if (-r $args{c}) {
$cfg{'conf_file'} = $args{c};
}
else {
die "config file $args{c} is not readable";
}
}
if ($cmd =~ /(SYMBOLS|PROCESS|CHECK|URLS|EMAILS)/i) {
$cfg{'command'} = $1;
$cfg{'control'} = 0;
}
elsif ($cmd =~ /(STAT|LEARN|SHUTDOWN|RELOAD|UPTIME|COUNTERS|FUZZY_ADD|FUZZY_DEL|WEIGHTS)/i) {
$cfg{'command'} = $1;
$cfg{'control'} = 1;
$cfg{'hosts'} = ['localhost:11334'];
}
else {
die "unknown command $cmd";
}
if (-r $cfg{'conf_file'}) {
# Try to parse config
parse_config ($cfg{'control'});
}
if (defined ($args{S})) {
$cfg{'imap_search'} = $args{S};
}
if (defined ($args{s})) {
if ($args{s}) {
$cfg{'statfile'} = $args{s};
}
else {
main::HELP_MESSAGE();
}
}
if (defined ($args{h})) {
$cfg{'hosts'} = [ $args{h} ];
}
if (defined ($args{P})) {
$cfg{'password'} = $args{P};
}
if (defined ($args{d})) {
$cfg{'deliver_to'} = $args{d};
}
if (defined ($args{w})) {
$cfg{'weight'} = $args{w};
}
if (defined ($args{i})) {
$cfg{'ip'} = $args{i};
}
if (exists ($args{p})) {
$cfg{'pass_all'} = 1;
}
if ($cmd =~ /SYMBOLS|SCAN|PROCESS|CHECK|REPORT_IFSPAM|REPORT|URLS|EMAILS|LEARN|FUZZY_ADD|FUZZY_DEL|WEIGHTS/i) {
$cfg{'require_input'} = 1;
}
if (defined ($args{H})) {
load_hosts_file ($args{H});
}
my $rspamd = Mail::Rspamd::Client->new(\%cfg);
if (-t STDOUT) {
$terminal = Tgetent Term::Cap { TERM => undef, OSPEED => 9600 };
}
else {
$is_tty = 0;
}
if (!defined ($path[0]) || ! $cfg{'require_input'}) {
my $input;
if ($cfg{'require_input'}) {
while (defined (my $line = <>)) {
$input .= $line;
}
}
my $res = $rspamd->do_all_cmd ($input);
while (my ($host, $result) = each (%{ $res })) {
if ($cfg{control}) {
print_control_result ($host, $result);
}
else {
print_rspamc_result ($host, $result);
}
}
}
else {
$rspamd->process_path (\&print_item_result, @path);
}
|