-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCore.pm
More file actions
2173 lines (1774 loc) · 67.2 KB
/
Copy pathCore.pm
File metadata and controls
2173 lines (1774 loc) · 67.2 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -sw
##
## Razor2::Client::Core - Vipul's Razor Client API
##
## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved.
## This code is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself.
##
## $Id: Core.pm,v 1.92 2006/05/27 00:00:53 rsoderberg Exp $
package Razor2::Client::Core;
use 5.010;
use strict;
use warnings;
use IO::Socket::IP;
use IO::Select;
use Errno qw(:POSIX);
use Razor2::Client::Version;
use parent qw(Razor2::String);
use parent qw(Razor2::Logger);
use parent qw(Razor2::Client::Engine);
use parent qw(Razor2::Errorhandler);
use Razor2::String qw(hextobase64 makesis parsesis hmac_sha1 xor_key
prep_mail debugobj to_batched_query
from_batched_query hexbits2hash
fisher_yates_shuffle);
our $VERSION = '2.87';
our $PROTOCOL = $Razor2::Client::Version::PROTOCOL;
sub new {
my ( $class, $conf, %params ) = @_;
my $self = {};
bless $self, $class;
$self->debug("Razor Agents $VERSION, protocol version $PROTOCOL.");
return $self;
}
#
# We store server-specific config info for each server we know about.
# All info about razor servers is stored in $self->{s}.
#
# Basically we get the server name/ip from {list},
# load that server's specific info from {allconfs} into {conf},
# and do stuff. If server is no good, we get nextserver from {list}
#
# $self->{s}->{list} ptr to {nomination} if report,revoke; or {catalogue} if check
# or the cmd-line server (-rs server)
# $self->{s}->{new_list} set to 1 when discover gets new lists
# $self->{s}->{catalogue} array ref containing catalogue servers
# $self->{s}->{nomination}array ref containing nomination servers
# $self->{s}->{discovery} array ref containing discovery servers
#
# $self->{s}->{modified} array ref containing servers whose .conf needs updating
# $self->{s}->{modified_lst} array ref containing which .lst files need updating
#
# $self->{s}->{ip} string containing ip (or dns name) of current server from {list})
# $self->{s}->{port} string containing port, taken from server:port from {list}
# $self->{s}->{engines} engines supported, derived from {conf}->{se}
# $self->{s}->{conf} hash ref containing current server's config params
# read from $razorhome/server.$ip.conf
#
# $self->{s}->{allconfs} hash ref of all servers' configs. key={ip}, val={conf}
# as read from server.*.conf file
#
# $self->{s}->{listfile} string containing path/file of server.lst, either
# nomination or catalogue depending $self->{breed}
# $self->{conf}->{listfile_discovery} string containing path/file of discovery server
#
# NOTE: if we are razor-check, server is Catalogue Server
# otherwise server is Nomination server.
#
# everytime we update our server list, $self->{s}->{list};
# we want to write that to disk - $self->{s}->{listfile}
#
sub nextserver {
my ($self) = @_;
$self->log( 16, "entered nextserver" );
# see if we need to discover (.lst files might be too old)
$self->discover() or return $self->errprefix("nextserver");
# first time we don't remove from list
# or if we've rediscovered.
shift @{ $self->{s}->{list} } unless ( $self->{s}->{new_list} || !$self->{s}->{ip} );
$self->{s}->{new_list} = 0;
my $next = ${ $self->{s}->{list} }[0];
# do we ever want to put current back on the end of list?
# push @{$self->{s}->{list}}, $self->{s}->{ip};
if ($next) {
( $self->{s}->{port} ) = $next =~ /:(.*)$/;
$next =~ s/:.*$//; # optional
$self->{s}->{ip} = $next; # ip can be IP or DNS name
$self->{s}->{port} ||= $self->{conf}->{port} || 2703;
$self->{s}->{conf} = $self->{s}->{allconfs}->{$next};
my $svrport = "$self->{s}->{ip}:$self->{s}->{port}";
# get rid of server specific stuff
delete $self->{s}->{greeting};
unless ( ref( $self->{s}->{conf} ) ) {
# never used this server before, no cached info. go get it!
$self->{s}->{conf} = {};
$self->connect; # calls parse_greeting which calls compute_server_conf
}
else {
$self->compute_server_conf(1); # computes supported engines, logs info
}
$self->writeservers();
my $srl = defined( $self->{s}->{conf}->{srl} ) ? $self->{s}->{conf}->{srl} : "<unknown>";
$self->log( 8, "Using next closest server $svrport, cached info srl $srl" );
#$self->logobj(11, "Using next closest server $svrport, cached info", $self->{s}->{conf});
return 1;
}
else {
return $self->error("Razor server $self->{opt}->{server} not available at this time")
if $self->{opt}->{server};
$self->{force_discovery} = 1;
if ( $self->{done_discovery} && !( $self->discover ) ) {
return $self->errprefix("No Razor servers available at this time");
}
return $self->nextserver;
}
}
#
# uses DNS to find Discovery servers
# puts discovery servers in $self->{s}->{discovery}
#
sub bootstrap_discovery {
my ($self) = @_;
$self->log( 16, "entered bootstrap_discovery" );
if ( $self->{conf}->{server} ) {
$self->log( 8, "no bootstap_discovery when cmd-line server specified" );
return 1;
}
unless ( $self->{force_bootstrap_discovery} ) {
if ( ref( $self->{s}->{discovery} ) && scalar( @{ $self->{s}->{discovery} } ) ) {
$self->log( 8, "already have " . scalar( @{ $self->{s}->{discovery} } ) . " discovery servers" );
return 1;
}
elsif ( $self->{done_bootstrap} ) {
# if we've done it before {s}->{discovery} should be set
$self->log( 8, "already have done bootstrap_discovery" );
return 1;
}
}
unless ( defined $self->{conf}->{listfile_discovery} ) {
$self->log( 6, "discovery listfile not defined!" );
}
elsif ( -s $self->{conf}->{listfile_discovery} ) {
my $wait = $self->{conf}->{rediscovery_wait_dns} || 604800; # 604800 secs == 7 days
my $randomize = int( rand( $wait / 7 ) );
my $timeleft = ( ( stat( $self->{conf}->{listfile_discovery} ) )[9] + $wait - $randomize ) - time;
if ( $timeleft > 0 ) {
$self->log( 7, "$timeleft seconds before soonest DNS discovery" );
return 1 unless $self->{force_bootstrap_discovery};
$self->log( 5, "forcing DNS discovery" );
}
else {
$self->log( 5, "DNS discovery overdue by " . ( 0 - $timeleft ) . " seconds" );
}
}
else {
if ( -e $self->{conf}->{listfile_discovery} ) {
$self->log( 6, "empty discovery listfile: $self->{conf}->{listfile_discovery}" );
}
else {
$self->log( 6, "no discovery listfile: $self->{conf}->{listfile_discovery}" );
}
}
$self->{s}->{discovery} = [ $self->{conf}->{razordiscovery} ];
push @{ $self->{s}->{modified_lst} }, "discovery";
return 1;
}
#
# uses Discovery Servers to find closest Nomination/Catalogue Servers.
# called every day or so of if .lst file is empty
#
# puts servers in $self->{s}->{list}
#
sub discover {
my ($self) = @_;
$self->log( 16, "entered discover" );
#
# do we need to discover?
#
# no discover if cmd-line server
return 1 if $self->{opt}->{server};
#
# don't discover if conf says turn_off_discovery (unless force_discovery)
#
return 1 if $self->{conf}->{turn_off_discovery} && ( !( $self->{force_discovery} ) );
return $self->error("No Razor servers available at this time")
if $self->{done_discovery};
# so if user has their own servers, and they are temporarily down, force_discovery.
# good: shit will work
# bad: it will erase their custom server*.lst file
#
unless ( defined $self->{s}->{listfile} ) {
$self->debug("listfile not defined!");
}
elsif ( -s $self->{s}->{listfile} ) {
my $randomize = int( rand( $self->{conf}->{rediscovery_wait} / 7 ) );
my $timeleft = ( ( stat( $self->{s}->{listfile} ) )[9] + $self->{conf}->{rediscovery_wait} - $randomize ) - time;
if ( $timeleft > 0 ) {
$self->debug("$timeleft seconds before closest server discovery");
return 1 unless $self->{force_discovery};
$self->debug("forcing discovery");
}
else {
$self->debug( "server discovery overdue by " . ( 0 - $timeleft ) . " seconds" );
}
}
else {
if ( -e $self->{s}->{listfile} ) {
$self->debug("empty listfile: $self->{s}->{listfile}");
}
else {
$self->debug("no listfile: $self->{s}->{listfile}");
}
}
#
# we need to discover.
#
return $self->errprefix("discover0") unless $self->bootstrap_discovery();
#
# Go ahead and do discovery for both csl and nsl.
#
my %stype = ( csl => 'catalogue', nsl => 'nomination' );
my $srvs = { csl => {}, nsl => {} };
my $list_orig = $self->{s}->{list};
$self->{s}->{list} = $self->{s}->{discovery};
foreach ( @{ $self->{s}->{discovery} } ) {
unless ( defined $_ ) {
$self->log( 5, "Razor Discovery Server not defined!" );
next;
}
$self->log( 8, "Checking with Razor Discovery Server $_" );
unless ( $self->connect( server => $_, discovery_server => 1 ) ) {
$self->log( 5, "Razor Discovery Server $_ is unreachable" );
next;
}
foreach my $querytype (qw(csl nsl)) {
my $query = "a=g&pm=$querytype\r\n";
my $resp = $self->_send( [$query] );
unless ($resp) {
$self->{s}->{list} = $list_orig;
return $self->errprefix("discover1");
}
# from_batched_query wants "-" in beginning, but not ".\r\n" at end
$resp->[0] =~ s/\.\r\n$//sg;
my $h = from_batched_query( $resp->[0], {} );
foreach my $href (@$h) {
next unless $href->{$querytype};
$self->log( 8, "Discovery Server $_ replying with $querytype=$href->{$querytype}" );
$srvs->{$querytype}->{ $href->{$querytype} } = 1;
}
unless ( keys %{ $srvs->{$querytype} } ) {
$self->log( 5, "Razor Discovery Server $_ had no valid $querytype servers" );
next;
}
}
}
$self->{s}->{list} = $list_orig;
foreach my $querytype (qw(csl nsl)) {
my @list = keys %{ $srvs->{$querytype} };
#return $self->error("Could not get valid info from Discovery Servers")
# unless @list;
unless (@list) {
if ( $self->{force_bootstrap_discovery} ) {
return $self->error("Bootstrap discovery failed. Giving up.");
}
$self->log( 5, "Couldn't talk to discovery servers. Will force a bootstrap..." );
$self->{force_bootstrap_discovery} = 1;
return $self->error("Bootstrap discovery failed. Giving up.") unless $self->bootstrap_discovery();
return $self->discover();
}
fisher_yates_shuffle( \@list ) if @list > 1;
$self->{s}->{ $stype{$querytype} } = \@list;
push @{ $self->{s}->{modified_lst} }, $stype{$querytype};
}
$self->disconnect();
unless ( $self->{opt}->{server} ) {
if ( $self->{breed} =~ /^check/ ) {
$self->{s}->{list} = $self->{s}->{catalogue};
$self->{s}->{listfile} = $self->{conf}->{listfile_catalogue}; # for discovery()
}
else {
$self->{s}->{list} = $self->{s}->{nomination};
$self->{s}->{listfile} = $self->{conf}->{listfile_nomination}; # for discovery()
}
}
$self->{s}->{new_list} = 1;
$self->{done_discovery} = 1;
$self->writeservers();
return $self;
}
# only for debugging and errorchecking
#
sub logobj {
my ( $self, $loglevel, $prefix, @objs ) = @_;
return unless $self->logll($loglevel);
foreach my $obj (@objs) {
my $line = debugobj($obj);
$self->log( $loglevel, "$prefix:\n $line" );
}
}
#
# Mail Object
#
# Main data type used by check and report is the Mail Object.
# an array of hash ref's, where array order matches mails in mbox (or stdin).
#
# key = value (not all defined)
#
# id = integer NOTE: only key guaranteed to exist
# orig_mail = ref to string containing orig email (headers+body)
# headers = headers of orig_email
# spam = 0, not spam, >1 spam
# skipme = 0|1 (not checked against server, usually whitelisted mail)
# p = array ref to mimeparts. see below
# e1 = similar to p, but special for engine 1
#
# e1: each mail obj contains a special part for engine 1
#
# skipme = 0|1 (ex: 1 if cleaned body goes to 0 len)
# spam = 0, not spam, >1 spam
# body = body of orig_mail
# cleaned = body sent thru razor 1 preproc
# e1 = hash using engine 1
# sent = hash ref sent to server
# resp = hash ref of server response
#
# p: each mail obj contains 1 or more mimeparts, which can contain:
#
# id = string - mailid.part
# skipme = 0|1 (ex: 1 if cleaned body goes to 0 len)
# spam = 0, not spam, >1 spam
# body = bodyparts (mimeparts) of orig_email, has X-Razor & Content-* headers
# cleaned = body sent through preprocessors (deHtml, deQP, etc..), debugging use only
# e2 = hash using engine 2
# e3 = hash using engine 2
# e4 = hash using engine 2
# sent = array ref of hash ref's sent to server
# resp = array ref of hash ref's, where hash is parsed sis of server response
#
#
sub prepare_objects {
my ( $self, $objs ) = @_;
my @objects;
unless ( $self->{s}->{engines}
|| ( $self->{s}->{engines} = $self->compute_supported_engines() ) ) {
$self->log( 1, "ALLBAD. supported engines not defined" );
}
my $i = 1;
if ( ref( $objs->[0] ) eq 'HASH' ) { # checking cmd-line signatures
foreach my $o (@$objs) {
my $obj = { id => $i++ };
$obj->{p}->[0]->{id} = "$obj->{id}.0";
$obj->{p}->[0]->{"e$o->{eng}"} = $o->{sig};
$obj->{ep4} = $o->{ep4} if $o->{ep4};
push @objects, $obj;
}
}
elsif ( ref( $objs->[0] ) eq 'SCALAR' ) { # checking/reporting mail
foreach my $o (@$objs) {
my $obj = { id => $i++ };
$obj->{orig_mail} = $o;
$self->log2file( 16, $o, "$obj->{id}.orig_mail" ); # includes headers and all
push @objects, $obj;
}
$self->prepare_parts( \@objects );
}
$self->logobj( 14, "prepared objs", \@objects );
return \@objects;
}
sub prepare_parts {
my ( $self, $objs ) = @_;
my $prep_mail_debug = 0; # debug print, 0=none, 1=split_mime stuff, 2=more verbose
$prep_mail_debug++ if $self->{conf}->{debuglevel} > 15;
$prep_mail_debug++ if $self->{conf}->{debuglevel} > 16;
foreach my $obj (@$objs) {
next if ( $obj->{skipme} || !$obj->{orig_mail} );
#
# now split up mime parts from orig mail
#
my ( $headers, @bodyparts ) = prep_mail(
$obj->{orig_mail},
$self->{conf}->{report_headers},
4 * 1024,
60 * 1024,
15 * 1024,
$self->{name_version},
$prep_mail_debug, # $debug,
);
my $lines = " prep_mail done: mail $obj->{id} headers=" . length($$headers);
foreach ( 0 .. $#bodyparts ) { $lines .= ", mime$_=" . length( ${ $bodyparts[$_] } ); }
$self->log( 8, $lines );
unless (@bodyparts) {
$self->log( 2, "empty body in mail $obj->{id}, skipping" );
next;
}
$$headers =~ s/\r\n/\n/gs;
$obj->{headers} = $headers;
# $obj->{e1} = {
# id => "$obj->{id}.e1",
# body => $obj->{orig_mail},
# };
$obj->{p} = [];
foreach ( 0 .. $#bodyparts ) {
$bodyparts[$_] =~ s/\r\n/\n/gs;
$obj->{p}->[$_] = {
id => "$obj->{id}.$_",
body => $bodyparts[$_],
};
}
}
return 1;
}
# given mail objects, fills out
#
# - e1
#
# and for each body part of mail object, fills out
#
# - cleaned
# - e2
# - e3
# - e4
#
# also returns array ref of sigs suitable for printing
#
sub compute_sigs {
my ( $self, $objects ) = @_;
my @printable_sigs;
foreach my $obj (@$objects) {
next if ( $obj->{skipme} || !$obj->{orig_mail} );
if ( ${ $obj->{orig_mail} } =~ /\n(Subject: [^\n]+)\n/ ) {
my $subj = substr $1, 0, 70;
$self->log( 8, "mail " . $obj->{id} . " $subj" );
}
else {
$self->log( 8, "mail " . $obj->{id} . " has no subject" );
}
#
# clean each bodypart, removing if new length is 0
#
next unless $obj->{p};
foreach my $objp ( @{ $obj->{p} } ) {
next if $objp->{skipme};
my $olen = length( ${ $objp->{body} } );
my $clnpart = ${ $objp->{body} };
# We'll do a VR8 preproc to determine emptiness
# of email, and store it so VR8 can use it.
my $clnpart_vr8 = $clnpart;
$self->{preproc_vr8}->preproc( \$clnpart_vr8 ); # in da future: $self->{s}->{conf}->{dre}
$objp->{cleaned_vr8} = \$clnpart_vr8;
# This for VR4 (the only other signature scheme
# supported at this time.
$self->{preproc}->preproc( \$clnpart );
$objp->{cleaned} = \$clnpart;
my $clen = length($clnpart_vr8);
$self->log2file( 15, $objp->{body}, "$objp->{id}.before_preproc.as_reported" );
$self->log2file( 15, $objp->{cleaned}, "$objp->{id}.after_preproc" );
if ( $clen eq 0 ) {
$self->log( 6, "preproc: mail $objp->{id} went from $olen bytes to 0, erasing" );
$objp->{skipme} = 1;
next;
}
elsif ( ( $clen < 128 ) and ( $clnpart =~ /^(Content\S*:[^\n]*\n\r?)+(Content\S*:[^\n]*)?\s*$/s ) ) {
$self->log( 6, "preproc: mail $objp->{id} seems empty, erasing" );
$objp->{skipme} = 1;
next;
}
elsif ( $clnpart_vr8 !~ /\S/ ) {
$self->log( 6, "preproc: mail $objp->{id} went to all whitespace, erasing" );
$objp->{skipme} = 1;
next;
}
elsif ( $clen eq $olen ) {
$self->log( 6, "preproc: mail $objp->{id} unchanged, bytes=$olen" );
}
else {
$self->log( 6, "preproc: mail $objp->{id} went from $olen bytes to $clen " );
}
}
#
# compute sig for bodyparts that are cleaned.
#
if ( $self->{s}->{conf}->{ep4} ) {
$obj->{ep4} = $self->{s}->{conf}->{ep4};
}
else {
$obj->{ep4} = '7542-10';
$self->log( 8, "warning: no ep4 for server $self->{s}->{ip}, using $obj->{ep4}" );
}
foreach my $objp ( @{ $obj->{p} } ) {
next if $objp->{skipme};
$self->log( 15, "mail part is [${$objp->{cleaned}}]" );
if ( ${ $objp->{cleaned} } =~ /^\s+$/ ) {
$self->log( 6, "mail $objp->{id} is whitespace only; skipping!" );
}
$self->log( 6, "computing sigs for mail $objp->{id}, len " . length( ${ $objp->{cleaned} } ) );
foreach ( sort keys %{ $self->{s}->{engines} } ) {
my $engine_no = $_;
my $sig;
if ( $engine_no == 4 ) {
$sig = $self->compute_engine(
$engine_no,
$objp->{cleaned},
$obj->{ep4}
);
}
elsif ( $engine_no == 8 ) {
$sig = $self->compute_engine(
$engine_no,
$objp->{cleaned_vr8}
);
}
else {
# Unsupported signature type, don't calculate.
next; # handled above
}
if ($sig) {
$objp->{"e$engine_no"} = $sig;
my @sigs;
if ( ref $sig eq 'ARRAY' ) {
@sigs = @$sig;
}
else {
push @sigs, $sig;
}
for (@sigs) {
my $line = "$objp->{id} e$engine_no: $_";
$line .= ", ep4: $obj->{ep4}" if ( $engine_no eq '4' );
push @printable_sigs, $line;
}
}
else {
$self->log( 6, "Engine ($engine_no) didn't produce a signature for mail $objp->{id}" );
}
}
}
$self->logobj( 14, "computed sigs for obj", $obj );
}
return \@printable_sigs;
}
#
# this function is the only one that has to be aware
# of razor protocol syntax. (not including random logging)
# the hashes generated here are eventually sent to to_batched_query.
#
sub make_query {
my ( $self, $params ) = @_;
if ( $params->{action} =~ /^check/ ) {
if ( ref $params->{sig} eq 'ARRAY' ) { # Multiple signature per part, VR8
my $sigs = $params->{sig};
my @queries;
for (@$sigs) {
my %query = ( a => 'c', e => $params->{eng}, s => $_ );
push @queries, \%query;
}
return \@queries;
}
else {
my %query = ( a => 'c', e => $params->{eng}, s => $params->{sig} );
$query{ep4} = $params->{ep4} if $query{e} eq '4';
return \%query;
}
}
elsif ( $params->{action} =~ /^rcheck/ ) {
my %query = (
a => 'r',
e => $params->{eng},
s => $params->{sig},
);
$query{ep4} = $params->{ep4} if $query{e} eq '4';
return \%query;
}
elsif ( $params->{action} =~ /(report)/ ) {
# prep_mail already truncated headers and body parts > 64K
my @dudes;
my $n = 0;
while ( $params->{obj}->{p}->[$n] ) {
my $line = ${ $params->{obj}->{headers} };
while (1) {
my $body = $params->{obj}->{p}->[$n]->{body};
last unless ( ( length($$body) + length($line) < $self->{s}->{conf}->{bqs} * 1024 ) );
$self->log( 11, "bqs=" . ( $self->{s}->{conf}->{bqs} * 1024 ) . " adding to line [len=" . length($line) . "] mail $params->{obj}->{p}->[$n]->{id}" . " [len=" . length($$body) . "], total len=" . ( length($$body) + length($line) ) );
$line .= "\r\n" . $$body;
$n++;
last unless $params->{obj}->{p}->[$n];
}
push @dudes, $line;
}
my @queries;
foreach (@dudes) {
push @queries, {
a => $params->{action} eq 'report' ? 'r' : 'revoke',
message => $_,
};
}
return @queries;
}
elsif ( $params->{action} =~ /revoke/ ) {
# Never send messages on revoke. Revoke all signature
# that we were able to compute.
my $n = 0;
my @queries;
while ( $params->{obj}->{p}->[$n] ) {
for my $engine ( keys %{ $self->{s}->{engines} } ) {
my $sigs;
if ( $sigs = $params->{obj}->{p}->[$n]->{"e$engine"} ) {
if ( ref $sigs eq 'ARRAY' ) {
for my $sig (@$sigs) {
push @queries, { a => 'revoke', e => $engine, s => $sig };
}
}
else {
push @queries, { a => 'revoke', e => $engine, s => $sigs };
}
}
}
$n++;
}
return @queries;
}
}
#
# prepare queries in correct syntax for sending over network
#
sub obj2queries {
my ( $self, $objects, $action ) = @_;
my @queries = ();
foreach my $obj (@$objects) {
next if $obj->{skipme};
push @queries, $obj->{e1}->{sent} if $obj->{e1}->{sent};
foreach my $objp ( @{ $obj->{p} } ) {
next if $objp->{skipme};
#$self->log(8,"not skipping mail part $objp->{id}, sent: ". scalar(@{$objp->{sent}}));
push @queries, @{ $objp->{sent} } if $objp->{sent};
}
}
if ( scalar(@queries) ) {
$self->log( 8, "preparing " . scalar(@queries) . " queries" );
}
else {
$self->log( 8, "objects yielded no valid queries" );
return [];
}
my $qbatched = to_batched_query(
\@queries,
$self->{s}->{conf}->{bql},
$self->{s}->{conf}->{bqs},
1
);
$self->log( 8, "sending " . scalar(@$qbatched) . " batches" );
return $qbatched;
}
#
# Parse response syntax, add info to appropriate object
#
sub queries2obj {
my ( $self, $objs, $responses, $action ) = @_;
my @resp;
foreach (@$responses) {
# from_batched_query wants "-" in beginning, but not ".\r\n" at end
s/\.\r\n$//sg;
my $arrayref = from_batched_query($_);
push @resp, @$arrayref;
}
$self->log( 12, "processing " . scalar(@resp) . " responses" );
$self->logobj( 14, "from_batched_query", \@resp );
my $j = 0;
while (@resp) {
my $obj = $objs->[ $j++ ];
return $self->error("more responses than mail objs!") unless $obj;
next if $obj->{skipme};
if ( $obj->{e1}->{sent} && !$obj->{e1}->{skipme} ) {
$obj->{e1}->{resp} = shift @resp;
$self->log( 12, "adding a resp to mail $obj->{e1}->{id}" );
}
foreach my $objp ( @{ $obj->{p} } ) {
next unless $objp->{sent};
# for each part, shift out as many responses as there were queries
foreach ( @{ $objp->{sent} } ) {
push @{ $objp->{resp} }, shift @resp;
$self->log( 12, "adding a resp to mail $objp->{id}" );
}
}
#$self->logobj(13,"end of queries2obj",$obj);
}
return 1;
}
sub check_resp {
my ( $self, $me, $sent, $resp, $objp ) = @_;
# default is no contention
$objp->{ct} = 0;
$objp->{ct} = $resp->{ct} if exists $resp->{ct};
if ( exists $resp->{err} ) {
$self->logobj( 4, "$me: got err $resp->{err} for query", $sent );
return 0;
}
if ( $resp->{p} eq '1' ) {
if ( exists $resp->{cf} ) {
if ( $resp->{cf} < $self->{s}->{min_cf} ) {
$self->log( 6, "$me: Not spam: cf $resp->{cf} < min_cf $self->{s}->{min_cf}" );
return 0;
}
else {
$self->log( 6, "$me: Is spam: cf $resp->{cf} >= min_cf $self->{s}->{min_cf}" );
return 1;
}
}
$self->log( 6, "$me: sig found, no cf, ok." );
return 1;
}
if ( $resp->{p} eq '0' ) {
$self->log( 6, "$me: sig not found." );
return 0;
}
# should never get here
$self->logobj(
2, "$me: got bad response from server - sent obj, resp obj",
[ $sent, $resp ]
);
return 0;
}
sub rcheck_resp {
my ( $self, $me, $sent, $resp ) = @_;
$self->log( 8, "$me: invalid $sent" ) unless ref($sent);
$self->log( 8, "$me: invalid $resp" ) unless ref($resp);
if ( exists $resp->{err} ) {
if ( $resp->{err} eq '230' ) {
$self->log( 8, "$me: err 230 - server wants mail" );
return 1;
}
$self->logobj( 4, "$me: got err $resp->{err} for query", $sent );
return 0;
}
if ( $resp->{res} eq '1' ) {
$self->log( 5, "$me: Server accepted report." );
return 0;
}
if ( $resp->{res} eq '0' ) {
$self->log( 1, "$me: Server did not accept report. Shame on the server." );
return 0;
}
# should never get here
$self->logobj(
2, "$me: got bad response from server - sent obj, resp obj",
[ $sent, $resp ]
);
return 0;
}
sub check {
my ( $self, $objects ) = @_;
my $valid = 0;
foreach my $obj (@$objects) {
next if $obj->{skipme};
#
# Logic used in ordering of check queries
#
# queries should go like this: (e=engine, p=part)
# e1, p0e2, p0e3, p0e4, p1e2, p1e3, p1e4, etc..
# unless cmd-line sigs are passed.
#
# engine 1 is for entire mail, not parts
if (
$obj->{e1} # cmd-line sig checks don't have this
&& $self->{s}->{engines}->{1}
) {
$obj->{e1}->{sent} = $self->make_query(
{
action => 'check',
sig => $obj->{e1}->{e1},
eng => 1
}
);
}
# rest of engines and mime parts
foreach my $objp ( @{ $obj->{p} } ) {
if ( $objp->{skipme} ) {
$self->log( 8, "mail $objp->{id} skipped in check" );
next;
}
$objp->{sent} = [];
foreach ( sort keys %{ $self->{s}->{engines} } ) {
my $engine_save = $_;
next if $_ eq 1; # engine 1 done above
my $sig = $objp->{"e$_"};
unless ($sig) {
$self->log( 5, "mail $objp->{id} e$_ got no sig" );
next;
}
unless ( $self->{s}->{engines}->{$_} ) {
# warn if cmd-lig sig check is not supported
$self->log( 5, "mail $objp->{id} engine $_ is not supported, sig check skipped" )
if ( $sig && !$obj->{orig_mail} );
next;
}
if ( ref $sig ) {
for (@$sig) {
$self->log( 8, "mail $objp->{id} e$engine_save sig: $_" );
}
}
else {
$self->log( 8, "mail $objp->{id} e$engine_save sig: $sig" );
}
my $query = $self->make_query(
{
action => 'check',
sig => $sig,
ep4 => $obj->{ep4},
eng => $_
}
);
$valid++ if $query;
if ( ref $query eq 'ARRAY' ) {
push @{ $objp->{sent} }, @$query;
}
else {
push @{ $objp->{sent} }, $query;
}
}
}
}
unless ($valid) {
$self->log( 5, "No queries, no spam" );
return 1;
}
$self->{s}->{list} = $self->{s}->{catalogue};
$self->connect;
# Build query text strings
#
my $queries = $self->obj2queries( $objects, 'check' ) or return $self->errprefix("check 1");
# send to server and store answers in mail obj
#
my $response = $self->_send($queries) or return $self->errprefix("check 2");
$self->queries2obj( $objects, $response, 'check' ) or return $self->errprefix("check 3");
foreach my $obj (@$objects) {