SASL/Perl.pm

filter: Statementstmt 47.1% 82 / 174 Branchbran 29.6% 19 / 64 Conditioncond 46.3% 19 / 41 MC/DCmcdc 22.7% 5 / 22 Subroutinesub 56.7% 21 / 37 Podpod 0.0% 0 / 19 totaltotal 40.9% 146 / 357 CC 50 SCAR 32.3
CC
50
Cov
43%
_getbuf
110.0
READ
72.0
WRITE
30.0
CRAP 25.3
« SASL/Perl/GSSAPI.pm ↑ Summary SASL/Perl/DIGEST_MD5.pm »
1 # Copyright (c) 2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
2 # This program is free software; you can redistribute it and/or
3 # modify it under the same terms as Perl itself.
4  
5 package Authen::SASL::Perl 2.2100;
6  
7 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 16 my %secflags = (
13 noplaintext => 1,
14 noanonymous => 1,
15 nodictionary => 1,
16 );
17 16 my %have;
18  
19 20 ▶sub server_new {
Subroutine server_new: called CC=6 SCAR=18.6
Pod undocumented
20 20 my ($pkg, $parent, $service, $host, $options) = @_;
21  
22 my $self = {
23 20 ▶ callback => { %{$parent->callback} },
Condition50%3/6
$service || ''
l!l
200
$host || ''
l!l
200
$parent->{'debug'} || 0
l!l
020
MC/DC0%0/1
$service || ''
$service
MC/DC0%0/1
$host || ''
$host
MC/DC0%0/1
$parent->{'debug'} || 0
$parent->{'debug'}
Truth table50%1/2
A
A= $service
Aresult
11
00
Truth table50%1/2
A
A= $host
Aresult
11
00
Truth table50%1/2
A
A= $parent->{'debug'}
Aresult
11
00
24 service => $service || '',
25 host => $host || '',
26 20 debug => $parent->{debug} || 0,
27 need_step => 1,
28 };
29  
30 20 ▶ my $mechanism = $parent->mechanism
Branch: 1 branch
BranchTrueFalseTotal
my $mechanism = $parent->mechanism or croak("No server mechanism specified") 20 0 20
31 or croak "No server mechanism specified";
32 20 $mechanism =~ s/^\s*\b(.*)\b\s*$/$1/g;
33 20 $mechanism =~ s/-/_/g;
34 20 $mechanism = uc $mechanism;
35 20 my $mpkg = __PACKAGE__ . "::$mechanism";
36 20 ▶ eval "require $mpkg;"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $mpkg;" or croak("Cannot use $mpkg for " . $parent->mechanism) 20 0 20
37 or croak "Cannot use $mpkg for " . $parent->mechanism;
38 20 my $server = $mpkg->_init($self);
39 20 $server->_init_server($options);
40 20 return $server;
41 }
42  
43 36 ▶sub client_new {
Subroutine client_new: called CC=11 SCAR=26.5
Pod undocumented
44 36 my ($pkg, $parent, $service, $host, $secflags) = @_;
45  
46 36 ▶ my @sec = grep { $secflags{$_} } split /\W+/, lc($secflags || '');
Condition100%2/2
$secflags || ''
l!l
1026
MC/DC100%1/1
$secflags || ''
$secflags
Truth table100%2/2
A
A= $secflags
Aresult
11
00
47  
48 my $self = {
49 36 ▶ callback => { %{$parent->callback} },
Condition66%4/6
$service || ''
l!l
360
$host || ''
l!l
351
$parent->{'debug'} || 0
l!l
036
MC/DC0%0/1
$service || ''
$service
MC/DC100%1/1
$host || ''
$host
MC/DC0%0/1
$parent->{'debug'} || 0
$parent->{'debug'}
Truth table50%1/2
A
A= $service
Aresult
11
00
Truth table100%2/2
A
A= $host
Aresult
11
00
Truth table50%1/2
A
A= $parent->{'debug'}
Aresult
11
00
50 service => $service || '',
51 host => $host || '',
52 36 debug => $parent->{debug} || 0,
53 need_step => 1,
54 };
55  
56 my @mpkg = sort {
57 105 $b->_order <=> $a->_order
58 } grep {
59 92 ▶ my $have = $have{$_} ||= (eval "require $_;" and $_->can('_secflags')) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $_;" && $_->can('_secflags') ? : 18 0 18
Condition50%3/6
$have{$_} ||= eval "require $_;" && $_->can("_secflags") ? 1 : -1
l!l&&r!l&&!r
74180
eval "require $_;" and $_->can("_secflags")
!ll&&!rl&&r
0018
MC/DC0%0/3
$have{$_} ||= eval "require $_;" && $_->can("_secflags") ? 1 : -1
$have{$_} eval "require $_;" $_->can("_secflags")
Truth table0%0/4
A ||= B and C
A= $have{$_}B= eval "require $_;"C= $_->can("_secflags")
ABCresult
1XX1
0111
00X0
0100
60 $have > 0 and $_->_secflags(@sec) == @sec
61 92 ▶ and $_->_acceptable( %{$parent->callback} )
Condition33%2/6
$have > 0 && $_->_secflags(@sec) == @sec && $_->_acceptable(%{$parent->callback;})
!ll&&!rl&&r
0092
$have > 0 and $_->_secflags(@sec) == @sec
!ll&&!rl&&r
0092
MC/DC0%0/3
$have > 0 && $_->_secflags(@sec) == @sec && $_->_acceptable(%{$parent->callback;})
$have > 0 $_->_secflags(@sec) == @sec $_->_acceptable(%{$parent->callback;})
Truth table25%1/4
A and B && C
A= $have > 0B= $_->_secflags(@sec) == @secC= $_->_acceptable(%{$parent->callback;})
ABCresult
0XX0
10X0
1100
1111
62 } map {
63 36 ▶ (my $mpkg = __PACKAGE__ . "::$_") =~ s/-/_/g;
Branch: 1 branch
BranchTrueFalseTotal
my(@mpkg) = (sort {$b->_order <=> $a->_order;} grep({my $have = ($have{$_} ||= eval "require $_;" && $_->can("_secflags") ? 1 : -1); $have > 0 and $_->_secflags(@sec) == @sec and $_->_acceptable(%{$parent->callback;});} map({(my $mpkg = 'Authen::SASL::Perl' . "::$_") =~ s/-/_/g; $mpkg;} split(/[^-\w]+/, $parent->mechanism, 0)))) or croak("No SASL mechanism found: ", $parent->mechanism, "\n") 36 0 36
64 92 $mpkg;
65 } split /[^-\w]+/, $parent->mechanism
66 or croak "No SASL mechanism found: ", $parent->mechanism, "\n";
67  
68 36 $mpkg[0]->_init($self);
69 }
70  
71 10 ▶sub _init_server {}
Subroutine _init_server: called CC=1 SCAR=0.0
72  
73 92 ▶sub _acceptable { 1 }
Subroutine _acceptable: called CC=1 SCAR=0.0
74 0 ▶sub _order { 0 }
Subroutine _order: not called CC=1 SCAR=6.9
75 60 ▶sub code { defined(shift->{error}) || 0 }
Subroutine code: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined +(shift())->{"error"} or 0 30 30 60
76 71 ▶sub error { shift->{error} }
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
77 14 ▶sub service { shift->{service} }
Subroutine service: called CC=1 SCAR=0.0
Pod undocumented
78 24 ▶sub host { shift->{host} }
Subroutine host: called CC=1 SCAR=0.0
Pod undocumented
79  
80 88 ▶sub need_step {
Subroutine need_step: called CC=2 SCAR=6.9
Pod undocumented
81 88 my $self = shift;
82 88 ▶ return 0 if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 4 84 88
83 84 return $self->{need_step};
84 }
85  
86 ## I think I need to rename that to end()?
87 ## It doesn't mean that SASL is successful, but that
88 ## that the negotiation is over, no more step necessary
89 ## at least for the client
90 16 ▶sub set_success {
Subroutine set_success: called CC=1 SCAR=0.0
Pod undocumented
91 16 my $self = shift;
92 16 $self->{need_step} = 0;
93 }
94  
95 56 ▶sub is_success {
Subroutine is_success: called CC=2 SCAR=6.9
Pod undocumented
96 56 my $self = shift;
97 56 ▶ return !$self->code && !$self->need_step;
Condition100%3/3
$self->code || $self->need_step
l!l&&r!l&&!r
30818
MC/DC100%2/2
$self->code || $self->need_step
$self->code $self->need_step
Truth table100%3/3
A || B
A= $self->codeB= $self->need_step
ABresult
1X1
011
000
98 }
99  
100 31 ▶sub set_error {
Subroutine set_error: called CC=1 SCAR=0.0
Pod undocumented
101 31 my $self = shift;
102 31 $self->{error} = shift;
103 31 return;
104 }
105  
106 # set/get property
107 209 ▶sub property {
Subroutine property: called CC=3 SCAR=11.0
Pod undocumented
108 209 my $self = shift;
109 209 ▶ my $prop = $self->{property} ||= {};
Condition100%2/2
$self->{"property"} ||= {}
l!l
18722
MC/DC100%1/1
$self->{"property"} ||= {}
$self->{"property"}
Truth table100%2/2
A
A= $self->{"property"}
Aresult
11
00
110 209 ▶ return $prop->{ $_[0] } if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 65 144 209
111 144 my %new = @_;
112 144 @{$prop}{keys %new} = values %new;
113 144 1;
114 }
115  
116 38 ▶sub callback {
Subroutine callback: called CC=2 SCAR=10.9
Pod undocumented
117 38 my $self = shift;
118  
119 38 ▶ return $self->{callback}{$_[0]} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 38 0 38
120  
121 0 my %new = @_;
122 0 @{$self->{callback}}{keys %new} = values %new;
123  
124 0 $self->{callback};
125 }
126  
127 # Should be defined in the mechanism sub-class
128 0 ▶sub mechanism { undef }
Subroutine mechanism: not called CC=1 SCAR=6.9
Pod undocumented
129 3 ▶sub client_step { undef }
Subroutine client_step: called CC=1 SCAR=0.0
Pod undocumented
130 0 ▶sub client_start { undef }
Subroutine client_start: not called CC=1 SCAR=6.9
Pod undocumented
131 0 ▶sub server_step { undef }
Subroutine server_step: not called CC=1 SCAR=6.9
Pod undocumented
132 0 ▶sub server_start { undef }
Subroutine server_start: not called CC=1 SCAR=6.9
Pod undocumented
133  
134 # Private methods used by Authen::SASL::Perl that
135 # may be overridden in mechanism sub-classes
136  
137 34 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
138 34 my ($pkg, $href) = @_;
139  
140 34 bless $href, $pkg;
141 }
142  
143 79 ▶sub _call {
Subroutine _call: called CC=5 SCAR=16.1
144 79 my ($self, $name) = splice(@_,0,2);
145  
146 79 my $cb = $self->{callback}{$name};
147  
148 79 ▶ return undef unless defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb 33 46 79
149  
150 46 my $value;
151  
152 46 ▶ if (ref($cb) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cb eq 'ARRAY') { } 1 45 46
elsif (ref $cb eq 'CODE') { } 4 41 45
153 1 my @args = @$cb;
154 1 $cb = shift @args;
155 1 $value = $cb->($self, @args);
156 }
157 elsif (ref($cb) eq 'CODE') {
158 4 $value = $cb->($self, @_);
159 }
160 else {
161 41 $value = $cb;
162 }
163  
164 46 ▶ $self->{answer}{$name} = $value
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "pass" 27 19 46
165 unless $name eq 'pass'; # Do not store password
166  
167 46 return $value;
168 }
169  
170 # TODO: Need a better name than this
171 0 ▶sub answer {
Subroutine answer: not called CC=1 SCAR=6.9
Pod undocumented
172 0 my ($self, $name) = @_;
173 0 $self->{answer}{$name};
174 }
175  
176 0 ▶sub _secflags { 0 }
Subroutine _secflags: not called CC=1 SCAR=6.9
177  
178 0 ▶sub securesocket {
Subroutine securesocket: not called CC=3 SCAR=24.8
Pod undocumented
179 0 my $self = shift;
180 0 ▶ return $_[0] unless (defined($self->property('ssf')) && $self->property('ssf') > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->property("ssf") and $self->property("ssf") > 0 0 0 0
Condition0%0/3
defined $self->property("ssf") and $self->property("ssf") > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->property("ssf") and $self->property("ssf") > 0
defined $self->property("ssf") $self->property("ssf") > 0
Truth table0%0/3
A and B
A= defined $self->property("ssf")B= $self->property("ssf") > 0
ABresult
0X0
100
111
181  
182 0 local *GLOB; # avoid used only once warning
183 0 my $glob = \do { local *GLOB; };
184 0 tie(*$glob, 'Authen::SASL::Perl::Layer', $_[0], $self);
185 0 $glob;
186 }
187  
188 {
189  
190 #
191 # Add SASL encoding/decoding to a filehandle
192 #
193  
194 package # private package; prevent detection by MetaCPAN
195 Authen::SASL::Perl::Layer;
196  
197 16 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
198  
199 16 require Tie::Handle;
200 16 our @ISA = qw(Tie::Handle);
201  
202 0 ▶ sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=3 SCAR=24.8
203 0 my ($class, $fh, $conn) = @_;
204 0 my $self;
205  
206 0 ▶ warn __PACKAGE__ . ': non-blocking handle may not work'
Branch: 1 branch
BranchTrueFalseTotal
if $fh->can("blocking") and not $fh->blocking 0 0 0
Condition0%0/3
$fh->can("blocking") and not $fh->blocking
!ll&&!rl&&r
000
MC/DC0%0/2
$fh->can("blocking") and not $fh->blocking
$fh->can("blocking") not $fh->blocking
Truth table0%0/3
A and not(B)
A= $fh->can("blocking")B= not $fh->blocking
ABresult
0X0
100
111
207 if ($fh->can('blocking') and not $fh->blocking());
208  
209 0 $self->{fh} = $fh;
210 0 $self->{conn} = $conn;
211 0 $self->{readbuflen} = 0;
212 0 $self->{sndbufsz} = $conn->property('maxout');
213 0 $self->{rcvbufsz} = $conn->property('maxbuf');
214  
215 0 return bless($self, $class);
216 }
217  
218 0 ▶ sub CLOSE {
Subroutine CLOSE: not called CC=1 SCAR=6.9
219 0 my ($self) = @_;
220  
221 # forward close to the inner handle
222 0 close($self->{fh});
223 0 delete $self->{fh};
224 }
225  
226 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
227 0 my ($self) = @_;
228 0 delete $self->{fh};
229 0 undef $self;
230 }
231  
232 0 ▶ sub FETCH {
Subroutine FETCH: not called CC=1 SCAR=6.9
233 0 my ($self) = @_;
234 0 return $self->{fh};
235 }
236  
237 0 ▶ sub FILENO {
Subroutine FILENO: not called CC=1 SCAR=6.9
238 0 my ($self) = @_;
239 0 return fileno($self->{fh});
240 }
241  
242  
243 0 ▶ sub READ {
Subroutine READ: not called CC=8 SCAR=42.8
244 0 my ($self, $buf, $len, $offset) = @_;
245 0 my $debug = $self->{conn}->{debug};
246  
247 0 $buf = \$_[1];
248  
249 0 my $avail = $self->{readbuflen};
250  
251 0 ▶ print STDERR " [READ(len=$len,offset=$offset)] avail=$avail;\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 4 0 0 0
252 if ($debug & 4);
253  
254 # Check if there's leftovers from a previous READ
255 0 ▶ if ($avail <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avail <= 0) 0 0 0
256 0 $avail = $self->_getbuf();
257 0 ▶ return undef unless ($avail > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $avail > 0 0 0 0
258 }
259  
260 # if there's more than we need right now, leave the rest for later
261 0 ▶ if ($avail >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avail >= $len) 0 0 0
262 0 ▶ print STDERR " GOT ALL: avail=$avail; need=$len\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 4 0 0 0
263 if ($debug & 4);
264 0 substr($$buf, $offset, $len) = substr($self->{readbuf}, 0, $len, '');
265 0 $self->{readbuflen} -= $len;
266 0 return ($len);
267 }
268  
269 # there's not enough; take all we have, read more on next call
270 0 ▶ print STDERR " GOT PARTIAL: avail=$avail; need=$len\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 4 0 0 0
271 if ($debug & 4);
272 0 ▶ substr($$buf, $offset || 0, $avail) = $self->{readbuf};
Condition0%0/2
$offset || 0
l!l
00
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
273 0 $self->{readbuf} = '';
274 0 $self->{readbuflen} = 0;
275  
276 0 return ($avail);
277 }
278  
279 # retrieve and decode a buffer of cipher text in SASL format
280 0 ▶ sub _getbuf {
Subroutine _getbuf: not called CC=10 SCAR=47.0
281 0 my ($self) = @_;
282 0 my $debug = $self->{conn}->{debug};
283 0 my $fh = $self->{fh};
284 0 my $buf = '';
285  
286 # first, read 4-octet buffer size
287 0 my $n = 0;
288 0 ▶ while ($n < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 4) 0 0 0
289 0 my $rv = sysread($fh, $buf, 4 - $n, $n);
290 0 ▶ print STDERR " [getbuf: sysread($fh,$buf,4-$n,$n)=$rv: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 4 0 0 0
291 if ($debug & 4);
292 0 ▶ return $rv unless $rv > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv > 0 0 0 0
293 0 $n += $rv;
294 }
295  
296 # size is encoded in network byte order
297 0 my ($bsz) = unpack('N', $buf);
298 0 ▶ print STDERR " [getbuf: cipher buffer sz=$bsz]\n" if ($debug & 4);
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 4 0 0 0
299 0 ▶ return undef unless ($bsz <= $self->{rcvbufsz});
Branch: 1 branch
BranchTrueFalseTotal
unless $bsz <= $self->{"rcvbufsz"} 0 0 0
300  
301 # next, read actual cipher text
302 0 $buf = '';
303 0 $n = 0;
304 0 ▶ while ($n < $bsz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $bsz) 0 0 0
305 0 my $rv = sysread($fh, $buf, $bsz - $n, $n);
306 0 ▶ print STDERR " [getbuf: got o=$n,n=", $bsz - $n, ",rv=$rv,bl=" . length($buf) . "]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 4 0 0 0
307 if ($debug & 4);
308 0 ▶ return $rv unless $rv > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv > 0 0 0 0
309 0 $n += $rv;
310 }
311  
312 # call mechanism specific decoding routine
313 0 $self->{readbuf} = $self->{conn}->decode($buf, $bsz);
314 0 $n = length($self->{readbuf});
315 0 ▶ print STDERR " [getbuf: clear text buffer sz=$n]\n" if ($debug & 4);
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 4 0 0 0
316 0 $self->{readbuflen} = $n;
317 }
318  
319  
320 # Encrypting a write() to a filehandle is much easier than reading, because
321 # all the data to be encrypted is immediately available
322 0 ▶ sub WRITE {
Subroutine WRITE: not called CC=5 SCAR=34.0
323 0 my ($self, $data, $len, $offset) = @_;
324 0 my $debug = $self->{conn}->{debug};
325  
326 0 my $fh = $self->{fh};
327 0 ▶ $len = length($data) if $len > length($data); # RT 85294
Branch: 1 branch
BranchTrueFalseTotal
if $len > length $data 0 0 0
328  
329 # put on wire in peer-sized chunks
330 0 my $bsz = $self->{sndbufsz};
331 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
332 0 ▶ print STDERR " [WRITE: chunk $bsz/$len]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug & 8 0 0 0
333 if ($debug & 8);
334  
335 # call mechanism specific encoding routine
336 0 ▶ my $x = $self->{conn}->encode(substr($data, $offset || 0, $bsz));
Condition0%0/2
$offset || 0
l!l
00
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
337 0 print $fh pack('N', length($x)), $x;
338 0 $len -= $bsz;
339 0 $offset += $bsz;
340 }
341  
342 0 return $_[2];
343 }
344  
345 }
346  
347 16 1;
« SASL/Perl/GSSAPI.pm ↑ Summary SASL/Perl/DIGEST_MD5.pm »