DBus/Authn.pm

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