Mail/Abuse/Processor/Radius.pm

filter: Statementstmt 78.4% 109 / 139 Branchbran 58.0% 36 / 62 Conditioncond 53.8% 7 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 71.0% 169 / 238 CC 42 SCAR 36.9
CC
42
Cov
71%
_livingston_parser
69.4
process
18.2
_dispatch
4.2
CRAP 40.1
« Mail/Abuse/Reader/GoogleGroups.pm ↑ Summary Mail/Abuse/Processor/Store.pm »
1 package Mail::Abuse::Processor::Radius;
2  
3 2 require 5.005_62;
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use PerlIO::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 my $Debug = 0; # Global debug flag
14  
15 2 ▶use base 'Mail::Abuse::Processor';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # The code below should be in a single line
18  
19 2 our $VERSION = do { my @r = (q$Revision: 1.7 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
20  
21 2 our @Ignore = (qw/NAS-IP-Address/);
22  
23 =pod
24  
25 =head1 NAME
26  
27 Mail::Abuse::Processor::Radius - Match incidents to users using RADIUS detail files
28  
29 =head1 SYNOPSIS
30  
31 use Mail::Abuse::Processor::Radius;
32  
33 use Mail::Abuse::Report;
34 my $p = new Mail::Abuse::Processor::Radius;
35 my $report = new Mail::Abuse::Report (processors => [ $p ]);
36  
37 # ... other pieces of code that configure the report ...
38  
39 =head1 DESCRIPTION
40  
41 This class attempts to find users associated with incidents by
42 analyzing Radius detail files according to the specifications of the
43 configuration file. Compressed detail files (ending in .gz) will be
44 uncompressed and processed on the fly.
45  
46 The following configuration keys control the behavior of this module.
47  
48 =over
49  
50 =item B<debug radius>
51  
52 If set to a true value, causes this module to emit debugging info
53 using C<warn()>.
54  
55 =cut
56  
57 2 ▶use constant DEBUG => 'debug radius';
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 =pod
60  
61 =item B<radius detail type>
62  
63 The type or format of the RADIUS detail file to be expected. The
64 values can be any of the following.
65  
66 =over
67  
68 =item B<livingston>
69  
70 A standard format derived from the venerable Livingston Radius server,
71 one of the earliest RADIUS server. Most Radius servers can produce
72 output in this format, which is why this is the default.
73  
74 =back
75  
76 Hopefully, other format will be added when needed.
77  
78 =cut
79  
80 2 ▶use constant TYPE => 'radius detail type';
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 2 my %Dispatch = (
83 'livingston' => \&_livingston_parser,
84 );
85  
86 =pod
87  
88 =item B<radius detail location>
89  
90 The path of a file or directory where the accounting details are
91 kept. If pointed to a directory, a recursive lookup will occur and all
92 files found will be analyzed.
93  
94 If pointed to a single file, only that particular file will be
95 analyzed.
96  
97 By default, the directory C</var/raddb/details> will be used if none
98 is specified. This seems to be a quite common default.
99  
100 Since the specified path will be traversed completely, it is a good
101 idea to remove old detail files to keep the response times short. It
102 is never a good idea to let this code loose in a hierarchy containing
103 5 years of detail records.
104  
105 =cut
106  
107 2 ▶use constant LOCATION => 'radius detail location';
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 =pod
110  
111 =back
112  
113 The following functions are implemented.
114  
115 =over
116  
117 =item C<process($report)>
118  
119 Takes a C<Mail::Abuse::Report> object as an argument and, for each
120 C<Mail::Abuse::Incident> collected, perform a lookup in the given
121 detail files.
122  
123 If the user is found, all the Radius detail entries are placed in the
124 incident, so that other modules can use this information. New entries
125 are to be accessed like in the following example:
126  
127 $incident->radius->{'User-Name'};
128 $incident->radius->{'Caller-Id'};
129  
130 Where each key is the actual entry in the detail record.
131  
132 =cut
133  
134 sub _livingston_parser
135 4 ▶{
Subroutine _livingston_parser: called CC=29 SCAR=42.4
136 4 my $self = shift;
137 4 my $rep = shift;
138 4 my $file = shift;
139 4 my $fh = shift;
140  
141 0 ▶ warn "# _livingston_parser $file with ", scalar @{$rep->incidents}, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 4 4
142 if $Debug;
143  
144 4 my $record;
145
146 4 ▶ while (<$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 156 4 160
147 {
148 # warn "# $_";
149 # The following code collects each record
150 # into $record for subsequent parsing
151 156 ▶ $record .= $_ if /^\w+/ .. /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\w+/ .. /^\s*$/ 156 0 156
152 156 ▶ if (/^\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$/) 8 148 156
153 {
154 8 ▶ warn "# RECORD\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 8 8
155  
156 # We only want to deal with Stop
157 # records...
158  
159 8 ▶ unless ($record =~ m/^\s*Acct-Status-Type = Stop/im)
Branch: 1 branch
BranchTrueFalseTotal
unless ($record =~ /^\s*Acct-Status-Type = Stop/im) 0 8 8
160 {
161 0 ▶ warn "# Not a stop record\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 0 0
162 0 $record = '';
163 0 next;
164 }
165  
166 # Find (and cache) which incidents
167 # can match this CDR
168  
169 8 my @match = (); # Incidents that match the IP on this CDR
170 8 my @addrs = (); # Addresses gathered from the CDR
171  
172 8 my $netmask = 32; # Default
173  
174 8 ▶ $netmask = $2
Branch: 1 branch
BranchTrueFalseTotal
if $record =~ /^\s*Framed-(IP-)?Netmask = ([^\n]+)/im 0 8 8
175 if $record =~ m/^\s*Framed-(IP-)?Netmask = ([^\n]+)/mi;
176  
177 8 ▶ while ($record =~ m/^\s*([-\w]+) = (\d+\.\d+\.\d+\.\d+)/mg)
Branch: 1 branch
BranchTrueFalseTotal
if ($record =~ /^\s*([-\w]+) = (\d+\.\d+\.\d+\.\d+)/gm) 16 8 24
178 {
179 16 my $key = $1;
180 16 my $val = $2;
181  
182 16 ▶ warn "# P: $key = $val\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 16 16
183  
184 16 ▶ next if grep { $key eq $_ } @Ignore;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$key eq $_;} @Mail::Abuse::Processor::Radius::Ignore 8 8 16
185
186 8 ▶ if ($key =~ /Framed/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /Framed/) { } 4 4 8
187 {
188 4 push @addrs, NetAddr::IP->new($val, $netmask);
189 }
190 else
191 {
192 4 push @addrs, NetAddr::IP->new($val);
193 }
194 }
195  
196 8 ▶ if ($Debug)
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug) 0 8 8
197 {
198 0 warn "# addr $_\n" for @addrs;
199 0 warn "# iaddr $_\n" for map { $_->ip } @{$rep->incidents};
200 0 warn "# itime $_\n" for map { $_->time } @{$rep->incidents};
201 }
202  
203 8 for my $i (@addrs)
204 {
205 12 push @match, grep { $_->time }
206 8 grep { $i->contains($_->ip) } @{$rep->incidents};
207 }
208  
209 8 ▶ unless (@match)
Branch: 1 branch
BranchTrueFalseTotal
unless (@match) 0 8 8
210 {
211 0 ▶ warn "# No matching IP\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 0 0
212 0 $record = '';
213 0 next;
214 }
215 # Get the CDR timestamp
216  
217 8 ▶ unless ($record =~ m/^([^\n]+)/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($record =~ /^([^\n]+)/) 0 8 8
218 {
219 0 ▶ warn "# No timestamp\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 0 0
220 0 $record = '';
221 0 next;
222 }
223 8 my $stamp = str2time($1);
224  
225 # Adjust stamp according to the accounting
226 # delay, if present
227  
228 8 ▶ $stamp -= $1 if $record =~ m/^\s*Acct-Delay-Time = (\d+)/mi;
Branch: 1 branch
BranchTrueFalseTotal
if $record =~ /^\s*Acct-Delay-Time = (\d+)/im 8 0 8
229  
230 8 ▶ unless ($record =~ m/^\s*Acct-Session-Time = (\d+)/mi)
Branch: 1 branch
BranchTrueFalseTotal
unless ($record =~ /^\s*Acct-Session-Time = (\d+)/im) 0 8 8
231 {
232 0 ▶ warn "# No Acct-Session-Time\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 0 0
233 0 $record = '';
234 0 next;
235 }
236  
237 8 my $length = $1;
238  
239 8 for my $i (@match)
240 {
241 12 ▶ if ($i->time >= $stamp
Branch: 1 branch
BranchTrueFalseTotal
if ($i->time >= $stamp and $i->time <= $stamp + $length) 8 4 12
Condition66%2/3
$i->time >= $stamp and $i->time <= $stamp + $length
!ll&&!rl&&r
408
MC/DC50%1/2
$i->time >= $stamp and $i->time <= $stamp + $length
$i->time >= $stamp $i->time <= $stamp + $length
Truth table66%2/3
A and B
A= $i->time >= $stampB= $i->time <= $stamp + $length
ABresult
0X0
100
111
242 and $i->time <= $stamp + $length)
243 {
244 8 $i->radius({});
245 8 ▶ while ($record =~ m/^\s*([-\w]+) = (.+)$/mg)
Branch: 1 branch
BranchTrueFalseTotal
if ($record =~ /^\s*([-\w]+) = (.+)$/gm) 140 8 148
246 {
247 140 $i->radius->{$1} = $2;
248 }
249 }
250 }
251  
252 8 $record = '';
253 }
254 }
255  
256 4 return;
257 }
258  
259 sub _dispatch
260 5 ▶{
Subroutine _dispatch: called CC=4 SCAR=14.3
261 5 my $self = shift;
262 5 my $rep = shift;
263 5 my $type = shift;
264 5 my $file = shift;
265  
266 5 ▶ return unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 4 5
267 4 ▶ warn "M::A::P::Radius: Processing $file\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 4 4
268 if $Debug;
269 4 my $fh = new IO::File;
270 4 ▶ unless ($fh->open($file, "<:gzip(autopop)"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($file, "<:gzip(autopop)")) 0 4 4
271 {
272 0 warn "M::A::P::Radius: Open of $file failed: $!\n";
273 0 return;
274 }
275 4 $Dispatch{$type}->($self, $rep, $file, $fh);
276 4 $fh->close;
277 }
278  
279 sub process
280 3 ▶{
Subroutine process: called CC=11 SCAR=29.0
Pod documented
281 3 my $self = shift;
282 3 my $rep = shift;
283  
284 3 ▶ unless ($rep->config or ref $rep->config ne 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
unless ($rep->config or ref $rep->config ne "HASH") 0 3 3
Condition33%1/3
$rep->config or ref $rep->config ne "HASH"
l!l&&r!l&&!r
300
MC/DC0%0/2
$rep->config or ref $rep->config ne "HASH"
$rep->config ref $rep->config ne "HASH"
Truth table33%1/3
A or B
A= $rep->configB= ref $rep->config ne "HASH"
ABresult
1X1
011
000
285 {
286 0 warn "Invalid or no config";
287 0 return;
288 }
289  
290 3 ▶ my $type = lc $rep->config->{&TYPE} || 'livingston';
Condition50%1/2
lc $rep->config->{&TYPE} || 'livingston'
l!l
03
MC/DC0%0/1
lc $rep->config->{&TYPE} || 'livingston'
lc $rep->config->{&TYPE}
Truth table50%1/2
A
A= lc $rep->config->{&TYPE}
Aresult
11
00
291 3 ▶ my $loc = $rep->config->{&LOCATION} || '/var/raddb/details';
Condition50%1/2
$rep->config->{&LOCATION} || '/var/raddb/details'
l!l
30
MC/DC0%0/1
$rep->config->{&LOCATION} || '/var/raddb/details'
$rep->config->{&LOCATION}
Truth table50%1/2
A
A= $rep->config->{&LOCATION}
Aresult
11
00
292 3 $Debug = $rep->config->{&DEBUG};
293  
294 3 ▶ unless (-d $loc or -f _) # Bail out if given garbage detail path
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $loc or -f _) 0 3 3
Condition66%2/3
-d $loc or -f _
l!l&&r!l&&!r
120
MC/DC0%0/2
-d $loc or -f _
-d $loc -f _
Truth table66%2/3
A or B
A= -d $locB= -f _
ABresult
1X1
011
000
295 {
296 0 warn "M::A::P::Radius: ", &LOCATION,
297 " does not point to a valid directory or file\n";
298 0 return;
299 }
300  
301 3 ▶ unless (grep { $type eq $_ } keys %Dispatch)
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$type eq $_;} keys %Dispatch) 0 3 3
302 {
303 0 warn "M::A::P::Radius: '$type' is not a valid '", &TYPE, "'\n";
304 0 return;
305 }
306  
307 # Empty reports need no further action
308
309 3 ▶ return if @{$rep->incidents} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$rep->incidents;} == 0 0 3 3
310  
311 3 ▶ if (-f $loc)
Branch: 2 branches
BranchTrueFalseTotal
if (-f $loc) { } 2 1 3
elsif (-d $loc) { } 1 0 1
312 {
313 2 $self->_dispatch($rep, $type, $loc);
314 }
315 elsif (-d $loc)
316 {
317 find
318 ({
319 wanted => sub
320 3 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 3 $self->_dispatch($rep, $type, $File::Find::name);
322 },
323 1 follow => 1,
324 no_chdir => 1,
325 untaint => 1,
326 untaint_skip => 1,
327 }, $loc);
328 }
329 else
330 {
331 0 warn "M::A::P::Radius: Don't know how to deal with ", &LOCATION, "\n";
332 0 return;
333 }
334 3 return 1;
335 }
336  
337 __END__
« Mail/Abuse/Reader/GoogleGroups.pm ↑ Summary Mail/Abuse/Processor/Store.pm »