DBus/Authn.pm

filter: Statementstmt 74.6% 109 / 146 Branchbran 42.8% 24 / 56 Conditioncond 38.8% 7 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 85.7% 18 / 21 Podpod 0.0% 0 / 5 totaltotal 62.2% 160 / 257 CC 37 SCAR 30.7
CC
37
Cov
64%
go
60.0
pending_send
12.0
new
6.4
CRAP 21.6
« DBus/Parser.pm ↑ Summary DBus/Client/EventBase.pm »
1 package Protocol::DBus::Authn;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use IO::Framed ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use Protocol::DBus::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use constant _CRLF => "\x0d\x0a";
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶sub new {
Subroutine new: called CC=6 SCAR=18.5
Pod undocumented
17 5 my ($class, %opts) = @_;
18  
19 5 my @missing = grep { !$opts{$_} } qw( socket mechanism );
20 5 ▶ die "Need: @missing" if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 5 5
21  
22 5 $opts{"_$_"} = delete $opts{$_} for keys %opts;
23  
24 5 $opts{'_can_pass_unix_fd'} = Socket::MsgHdr->can('new');
25 5 ▶ $opts{'_can_pass_unix_fd'} &&= Socket->can('SCM_RIGHTS');
Condition66%2/3
$opts{"_can_pass_unix_fd"} &&= "Socket"->can("SCM_RIGHTS")
!ll&&!rl&&r
302
MC/DC50%1/2
$opts{"_can_pass_unix_fd"} &&= "Socket"->can("SCM_RIGHTS")
$opts{"_can_pass_unix_fd"} "Socket"->can("SCM_RIGHTS")
Truth table66%2/3
A &&= B
A= $opts{"_can_pass_unix_fd"}B= "Socket"->can("SCM_RIGHTS")
ABresult
0X0
100
111
26 5 ▶ $opts{'_can_pass_unix_fd'} &&= _is_unix_socket($opts{'_socket'});
Condition66%2/3
$opts{"_can_pass_unix_fd"} &&= _is_unix_socket($opts{"_socket"})
!ll&&!rl&&r
302
MC/DC50%1/2
$opts{"_can_pass_unix_fd"} &&= _is_unix_socket($opts{"_socket"})
$opts{"_can_pass_unix_fd"} _is_unix_socket($opts{"_socket"})
Truth table66%2/3
A &&= B
A= $opts{"_can_pass_unix_fd"}B= _is_unix_socket($opts{"_socket"})
ABresult
0X0
100
111
27  
28 5 $opts{'_io'} = IO::Framed->new( $opts{'_socket'} )->enable_write_queue();
29  
30 5 my $self = bless \%opts, $class;
31  
32 5 ▶ $self->_set_mechanism( $opts{'_mechanism'} ) or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->_set_mechanism($opts{"_mechanism"}) or do { die qq[\342\200\234$opts{"_mechanism"}\342\200\235 is not a valid authn mechanism.] } 5 0 5
33 0 die "“$opts{'_mechanism'}” is not a valid authn mechanism.";
34 };
35  
36 5 return $self;
37 }
38  
39 5 ▶sub _set_mechanism {
Subroutine _set_mechanism: called CC=3 SCAR=11.4
40 5 my ($self, $mechanism) = @_;
41  
42 5 ▶ if (!ref $mechanism) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $mechanism) 5 0 5
43 5 my $module = __PACKAGE__ . "::Mechanism::$mechanism";
44  
45 5 my $err = $@;
46 5 ▶ if (!eval { Module::Runtime::require_module($module); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &Module::Runtime::require_module($module); 1 } }) 0 5 5
47 0 DEBUG && print STDERR "Failed to load $mechanism authn module: $@";
48 0 return 0;
49 }
50 5 $@ = $err;
51  
52 5 $self->{'_mechanism'} = $module->new();
53 }
54  
55 5 $self->{'_xaction'} = $self->_create_xaction();
56  
57 5 return 1;
58 }
59  
60 0 ▶sub negotiated_unix_fd {
Subroutine negotiated_unix_fd: not called CC=2 SCAR=17.9
Pod undocumented
61 0 ▶ return $_[0]->{'_negotiated_unix_fd'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_negotiated_unix_fd'} ? : 0 0 0
62 }
63  
64 # Whether a send is pending (1) or a receive (0).
65 0 ▶sub pending_send {
Subroutine pending_send: not called CC=3 SCAR=24.8
Pod undocumented
66 0 my ($self) = @_;
67  
68 0 my $next_is_receive = $self->{'_xaction'}[0];
69 0 ▶ $next_is_receive &&= $next_is_receive->[0];
Condition0%0/3
$next_is_receive &&= $next_is_receive->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$next_is_receive &&= $next_is_receive->[0]
$next_is_receive $next_is_receive->[0]
Truth table0%0/3
A &&= B
A= $next_is_receiveB= $next_is_receive->[0]
ABresult
0X0
100
111
70  
71 0 ▶ if (!defined $next_is_receive) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next_is_receive) 0 0 0
72 0 die "Authn transaction is done!";
73 }
74  
75 0 return !$next_is_receive;
76 }
77  
78 4 ▶sub go {
Subroutine go: called CC=18 SCAR=40.9
Pod undocumented
79 4 my ($self) = @_;
80  
81 4 my $s = $self->{'_socket'};
82  
83 # Don’t send_initial() if !must_send_initial().
84 4 ▶ $self->{'_sent_initial'} ||= !$self->{'_mechanism'}->must_send_initial() || $self->{'_mechanism'}->send_initial($s);
Condition33%2/6
$self->{"_sent_initial"} ||= !$self->{"_mechanism"}->must_send_initial || $self->{"_mechanism"}->send_initial($s)
l!l&&r!l&&!r
040
!$self->{'_mechanism'}->must_send_initial || $self->{'_mechanism'}->send_initial($s)
l!l&&r!l&&!r
400
MC/DC0%0/3
$self->{"_sent_initial"} ||= !$self->{"_mechanism"}->must_send_initial || $self->{"_mechanism"}->send_initial($s)
$self->{"_sent_initial"} !$self->{'_mechanism'}->must_send_initial $self->{'_mechanism'}->send_initial($s)
Truth table0%0/4
A ||= not(B) || C
A= $self->{"_sent_initial"}B= !$self->{'_mechanism'}->must_send_initialC= $self->{'_mechanism'}->send_initial($s)
ABCresult
1XX1
01X1
0011
0000
85  
86 4 ▶ if ($self->{'_sent_initial'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_sent_initial"}) 4 0 4
87 LINES:
88 {
89 4 ▶ if ( $self->{'_io'}->get_write_queue_count() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_io"}->get_write_queue_count) 0 4 4
90 0 ▶ $self->flush_write_queue() or last LINES;
Branch: 1 branch
BranchTrueFalseTotal
$self->flush_write_queue or last LINES 0 0 0
91 }
92  
93 4 my $last_lines;
94  
95 4 my $dollar_at = $@;
96 4 my $ok = eval {
97 4 ▶ while ( my $cur = $self->{'_xaction'}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cur = $self->{"_xaction"}[0]) 20 4 24
98 20 ▶ if ($cur->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur->[0]) { } 8 12 20
99 8 ▶ my $line = $self->_read_line() or do {
Branch: 1 branch
BranchTrueFalseTotal
my $line = $self->_read_line or do { $last_lines = 1; last } 8 0 8
100 0 $last_lines = 1;
101 0 last;
102 };
103  
104 8 $cur->[1]->($self, $line);
105 }
106 else {
107 12 my @line_parts;
108  
109 12 ▶ if ('CODE' eq ref $cur->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $cur->[1]) { } 2 10 12
110 2 @line_parts = $cur->[1]->($self);
111 }
112 else {
113 10 @line_parts = @{$cur}[ 1 .. $#$cur ];
114 }
115  
116 12 ▶ $self->_send_line("@line_parts") or last LINES;
Branch: 1 branch
BranchTrueFalseTotal
$self->_send_line(join($", @line_parts)) or last LINES 12 0 12
117  
118 12 push @{ $self->{'_tried_mechanism'} }, $self->{'_mechanism'}->label();
119 }
120  
121 20 shift @{ $self->{'_xaction'} };
122 }
123  
124 4 1;
125 };
126  
127 4 ▶ last LINES if $last_lines;
Branch: 1 branch
BranchTrueFalseTotal
if $last_lines 0 4 4
128  
129 4 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 4 4
130 0 my $err = $@;
131 0 ▶ if (eval { $err->isa('Protocol::DBus::X::Rejected') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $err->isa('Protocol::DBus::X::Rejected') } }) { } 0 0 0
132  
133 0 $self->{'_mechanism'}->on_rejected();
134  
135 0 my @to_try;
136  
137 0 for my $mech ( @{ $err->get('mechanisms') } ) {
138 0 ▶ if (!grep { $_ eq $mech } @{ $self->{'_tried_mechanism'} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $mech;} @{$$self{"_tried_mechanism"};}) 0 0 0
139 0 push @to_try, $mech;
140 }
141 }
142  
143 0 ▶ while (my $mech = shift @to_try) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mech = shift @to_try) 0 0 0
144 0 ▶ if ($self->_set_mechanism($mech)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_set_mechanism($mech)) 0 0 0
145 0 redo LINES;
146 }
147 }
148  
149 0 die "Exhausted all authentication mechanisms! (@{ $self->{'_tried_mechanism'} })";
150 }
151 else {
152 0 local $@ = $err;
153 0 die;
154 }
155 }
156  
157 4 return 1;
158 }
159 }
160  
161 0 return undef;
162 }
163  
164 0 ▶sub cancel {
Subroutine cancel: not called CC=1 SCAR=6.9
Pod undocumented
165 0 my ($self) = @_;
166  
167 0 die 'unimplemented';
168 }
169  
170 5 ▶sub _create_xaction {
Subroutine _create_xaction: called CC=4 SCAR=14.1
171 5 my ($self) = @_;
172  
173 5 my $auth_label = 'AUTH';
174  
175 # Unless the mechanism sends its own initial NUL, might as well use the
176 # same system call to send the initial NUL as we use to send the AUTH.
177 5 ▶ if (!$self->{'_sent_initial'} && !$self->{'_mechanism'}->must_send_initial()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_sent_initial"} or $self->{"_mechanism"}->must_send_initial) 5 0 5
Condition33%1/3
$self->{"_sent_initial"} or $self->{"_mechanism"}->must_send_initial
l!l&&r!l&&!r
005
MC/DC0%0/2
$self->{"_sent_initial"} or $self->{"_mechanism"}->must_send_initial
$self->{"_sent_initial"} $self->{"_mechanism"}->must_send_initial
Truth table33%1/3
A or B
A= $self->{"_sent_initial"}B= $self->{"_mechanism"}->must_send_initial
ABresult
1X1
011
000
178 5 substr( $auth_label, 0, 0 ) = "\0";
179 }
180  
181 # 0 = send; 1 = receive
182 my @xaction = (
183 [ 0 => $auth_label, $self->{'_mechanism'}->label(), $self->{'_mechanism'}->INITIAL_RESPONSE() ],
184  
185 # e.g., for exchange of DATA
186 5 $self->{'_mechanism'}->AFTER_AUTH(),
187  
188 [ 1 => \&_consume_ok ],
189 );
190  
191 5 ▶ if ($self->{'_can_pass_unix_fd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_can_pass_unix_fd"}) 2 3 5
192 2 push @xaction, (
193 [ 0 => 'NEGOTIATE_UNIX_FD' ],
194 [ 1 => \&_consume_agree_unix_fd ],
195 );
196 }
197  
198 5 push @xaction, [ 0 => 'BEGIN' ];
199  
200 5 return \@xaction;
201 }
202  
203 2 ▶sub _consume_agree_unix_fd {
Subroutine _consume_agree_unix_fd: called CC=3 SCAR=13.3
204 2 my ($self, $line) = @_;
205  
206 2 ▶ if ($line eq 'AGREE_UNIX_FD') {
Branch: 2 branches
BranchTrueFalseTotal
if ($line eq 'AGREE_UNIX_FD') { } 2 0 2
elsif (index($line, 'ERROR ') == 0) { } 0 0 0
207 2 $self->{'_negotiated_unix_fd'} = 1;
208 }
209 elsif (index($line, 'ERROR ') == 0) {
210 0 warn "Server rejected unix fd passing: " . substr($line, 6) . $/;
211 }
212  
213 2 return;
214 }
215  
216 4 ▶sub _consume_ok {
Subroutine _consume_ok: called CC=2 SCAR=7.4
217 4 my ($self, $line) = @_;
218  
219 4 ▶ if (index($line, 'OK ') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($line, 'OK ') == 0) { } 4 0 4
220 4 $self->{'_server_guid'} = substr($line, 3);
221 }
222 else {
223 0 die "Unrecognized response: $line";
224 }
225  
226 4 return;
227 }
228  
229 12 ▶sub _send_line {
Subroutine _send_line: called CC=1 SCAR=0.0
230 12 my ($self) = @_;
231  
232 12 DEBUG() && print STDERR "AUTHN SENDING: [$_[1]]$/";
233  
234 12 $self->{'_io'}->write( $_[1] . _CRLF() );
235  
236 12 return $self->_flush_write_queue();
237 }
238  
239 12 ▶sub _flush_write_queue {
Subroutine _flush_write_queue: called CC=1 SCAR=0.0
240 12 my ($self) = @_;
241  
242 12 local $SIG{'PIPE'} = 'IGNORE';
243  
244 12 return $self->{'_io'}->flush_write_queue();
245 }
246  
247 8 ▶sub _read_line {
Subroutine _read_line: called CC=3 SCAR=11.4
248 8 my $line;
249  
250 8 DEBUG() && print STDERR "AUTHN RECEIVING …$/";
251  
252 8 ▶ if ($line = $_[0]->{'_io'}->read_until("\x0d\x0a")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $_[0]{"_io"}->read_until("\r\n")) 8 0 8
253 8 substr( $line, -2 ) = q<>;
254  
255 8 DEBUG() && print STDERR "AUTHN RECEIVED: [$line]$/";
256  
257 8 ▶ if (0 == index( $line, 'REJECTED ')) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == index($line, "REJECTED ")) 0 8 8
258 0 die Protocol::DBus::X->create(
259 'Rejected',
260 split( m< >, substr( $line, 9 ) ),
261 );
262 }
263 }
264  
265 8 return $line;
266 }
267  
268 2 ▶sub _is_unix_socket {
Subroutine _is_unix_socket: called CC=2 SCAR=7.1
269 2 my ($sk) = @_;
270  
271 2 ▶ my $sname = getsockname($sk) or die "getsockname(): $!";
Branch: 1 branch
BranchTrueFalseTotal
my $sname = getsockname $sk or die "getsockname(): $!" 2 0 2
272  
273 2 return Socket::sockaddr_family($sname) == Socket::AF_UNIX();
274 }
275  
276 #sub DESTROY {
277 # print "DESTROYED: [$_[0]]\n";
278 #}
279  
280 11 1;
« DBus/Parser.pm ↑ Summary DBus/Client/EventBase.pm »