lib/SMB/Client.pm

filter: Statementstmt 16.4% 40 / 243 Branchbran 0.0% 0 / 130 Conditioncond 0.0% 0 / 95 MC/DCmcdc 0.0% 0 / 54 Subroutinesub 48.1% 13 / 27 Podpod 8.3% 1 / 12 totaltotal 9.6% 54 / 561 SCAR 90.3
CC
104
Cov
9%
perform_tree_command
2550.0
process_sessionsetup_if_needed
156.0
_normalize_path
72.0
CRAP 8377.0
↑ Summary blib/lib/SMB/Auth.pm »
1 # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 package SMB::Client;
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use parent 'SMB::Agent';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use SMB::v2::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use SMB::v2::Command::Negotiate;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use SMB::v2::Command::SessionSetup;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use SMB::v2::Command::TreeConnect;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use SMB::v2::Command::Create;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use SMB::v2::Command::Close;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use SMB::v2::Command::QueryDirectory;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use SMB::v2::Command::Read;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use SMB::v2::Command::Write;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use SMB::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 sub new ($$%) {
35 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
36 0 my $share_uri = shift;
37 0 my %options = @_;
38  
39 0 ▶ my $verbose = delete $options{verbose} || 0;
Condition0%0/2
delete $options{'verbose'} || 0
l!l
00
MC/DC0%0/1
delete $options{'verbose'} || 0
delete $options{'verbose'}
Truth table0%0/2
A
A= delete $options{'verbose'}
Aresult
11
00
40 0 ▶ $options{quiet} = 1 unless $verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose 0 0 0
41  
42 0 my $self = $class->SUPER::new(
43 %options,
44 server_id => 0, # running index
45 curr_conn_key => undef, # key in connections hash
46 unique_conn_addr => 1,
47 );
48  
49 0 ▶ $self->connect($share_uri, %options)
Branch: 1 branch
BranchTrueFalseTotal
if $share_uri 0 0 0
50 if $share_uri;
51  
52 0 return $self;
53 }
54  
55 sub connect ($$%) {
56 0 ▶ my $self = shift;
Subroutine connect: not called CC=6 SCAR=37.4
Pod undocumented
57 0 my $share_uri = shift;
58 0 my %options = @_;
59  
60 0 ▶ my ($addr, $share) = $share_uri =~ m![/\\]!
Branch: 1 branch
BranchTrueFalseTotal
$share_uri =~ m([/\\]) ? : 0 0 0
61 ? $self->parse_share_uri($share_uri)
62 : ($share_uri);
63 0 ▶ die "Please specify share uri //server.name.or.ip[:port]/share or server.name.or.ip[:port]\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 0 0 0
64 unless $addr;
65 0 ▶ $addr .= ':445' unless $addr =~ /:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr =~ /:/ 0 0 0
66  
67 0 ▶ my $socket = IO::Socket::INET->new(PeerAddr => $addr, Proto => 'tcp')
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("PeerAddr", $addr, "Proto", "tcp") or die "Can't open ${addr}: $!\n" 0 0 0
68 or die "Can't open $addr: $!\n";
69  
70 0 ▶ if ($options{just_socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'just_socket'}) { } 0 0 0
71 0 $self->{socket} = $socket;
72 } else {
73 my ($conn_key) = $self->add_connection(
74 $socket, --$self->{server_id},
75 addr => $addr,
76 share => $share,
77 username => $options{username},
78 password => $options{password},
79 0 tree => undef,
80 cwd => '',
81 openfiles => {},
82 dialect => undef,
83 session_id => 0,
84 message_id => 0,
85 sent_request => undef,
86 );
87  
88 0 $self->curr_conn_key($conn_key);
89 }
90  
91 0 return $self;
92 }
93  
94 sub get_curr_connection ($) {
95 0 ▶ my $self = shift;
Subroutine get_curr_connection: not called CC=3 SCAR=24.8
Pod undocumented
96  
97 0 my $connections = $self->connections;
98 0 ▶ unless (%$connections) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$connections) 0 0 0
99 0 $self->err("Called get_curr_connection when no connections established");
100 0 return;
101 }
102  
103 0 my $connection = $connections->{$self->curr_conn_key};
104 0 ▶ unless ($connection) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connection) 0 0 0
105 0 $self->err("Called get_curr_connection when curr_conn_key is invalid");
106 0 return;
107 }
108  
109 0 return $connection;
110 }
111  
112 sub find_connection_by_tree ($$) {
113 0 ▶ my $self = shift;
Subroutine find_connection_by_tree: not called CC=4 SCAR=30.0
Pod undocumented
114 0 ▶ my $tree = shift // die;
Condition0%0/2
shift() // die()
l!l
00
MC/DC0%0/1
shift() // die()
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
115  
116 0 for (values %{$self->connections}) {
117 0 ▶ return $_ if $_->tree == $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $_->tree == $tree 0 0 0
118 }
119  
120 0 $self->err("Can't find connection for $tree");
121 0 return;
122 }
123  
124 sub process_request ($$$%) {
125 0 ▶ my $self = shift;
Subroutine process_request: not called CC=6 SCAR=37.4
Pod undocumented
126 0 my $connection = shift;
127 0 my $command_name = shift;
128 0 my %command_options = @_;
129  
130 0 my $command_class = "SMB::v2::Command::$command_name";
131 0 my $command_code = $SMB::v2::Commands::command_codes{$command_name};
132 0 my $no_warn = delete $command_options{_no_warn};
133  
134 my $request = $command_class->new(
135 SMB::v2::Header->new(
136 0 ▶ mid => $connection->{message_id}++,
Branch: 1 branch
BranchTrueFalseTotal
$connection->tree ? : 0 0 0
137 uid => $connection->session_id,
138 tid => $connection->tree ? $connection->tree->id : 0,
139 code => $command_code,
140 ),
141 );
142 0 $request->set(%command_options);
143  
144 0 $connection->send_command($request);
145 0 $connection->sent_request($request);
146  
147 0 my $response = $self->wait_for_response($connection);
148  
149 0 ▶ warn "SMB Error on $command_name response: " . ($response ? sprintf "%x", $response->status : "internal") . "\n"
Branch: 2 branches
BranchTrueFalseTotal
if not $no_warn and !$response || $response->is_error 0 0 0
$response ? : 0 0 0
Condition0%0/6
not $no_warn and !$response || $response->is_error
!ll&&!rl&&r
000
not $response or $response->is_error
l!l&&r!l&&!r
000
MC/DC0%0/3
not $no_warn and !$response || $response->is_error
not $no_warn not $response $response->is_error
Truth table0%0/4
not(A) and not(B) or C
A= not $no_warnB= not $responseC= $response->is_error
ABCresult
0XX0
1000
11X1
1011
150 if !$no_warn && (!$response || $response->is_error);
151  
152 0 return $response;
153 }
154  
155 sub process_negotiate_if_needed ($$) {
156 0 ▶ my $self = shift;
Subroutine process_negotiate_if_needed: not called CC=5 SCAR=34.0
Pod undocumented
157 0 my $connection = shift;
158  
159 0 ▶ return 1 if $connection->dialect;
Branch: 1 branch
BranchTrueFalseTotal
if $connection->dialect 0 0 0
160  
161 0 my $response = $self->process_request($connection, 'Negotiate');
162 0 ▶ if ($response && $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and $response->is_success) 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
163 0 ▶ unless ($connection->auth->process_spnego($response->security_buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connection->auth->process_spnego($response->security_buffer)) 0 0 0
164 0 $self->err("Server does not support our negotiation mechanism, expect problems on SessionSetup");
165 }
166 0 $connection->dialect($response->dialect);
167 0 return 1;
168 }
169  
170 0 return 0;
171 }
172  
173 sub process_sessionsetup_if_needed ($$) {
174 0 ▶ my $self = shift;
Subroutine process_sessionsetup_if_needed: not called CC=12 SCAR=50.5
Pod undocumented
175 0 my $connection = shift;
176  
177 0 ▶ return 1 if $connection->session_id;
Branch: 1 branch
BranchTrueFalseTotal
if $connection->session_id 0 0 0
178  
179 0 my $response = $self->process_request($connection, 'SessionSetup',
180 security_buffer => $connection->auth->generate_spnego,
181 _no_warn => 1,
182 );
183 0 my $more_processing = $response->status == SMB::STATUS_MORE_PROCESSING_REQUIRED;
184 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success || $more_processing and $connection->auth->process_spnego($response->security_buffer) 0 0 0
Condition0%0/9
$response and $response->is_success || $more_processing and $connection->auth->process_spnego($response->security_buffer)
!ll&&!rl&&r
000
$response and $response->is_success || $more_processing
!ll&&!rl&&r
000
$response->is_success or $more_processing
l!l&&r!l&&!r
000
MC/DC0%0/4
$response and $response->is_success || $more_processing and $connection->auth->process_spnego($response->security_buffer)
$response $response->is_success $more_processing $connection->auth->process_spnego($response->security_buffer)
Truth table0%0/6
A and B or C and D
A= $responseB= $response->is_successC= $more_processingD= $connection->auth->process_spnego($response->security_buffer)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
185 unless $response && ($response->is_success || $more_processing)
186 && $connection->auth->process_spnego($response->security_buffer);
187 0 $connection->session_id($response->header->uid);
188 0 ▶ return 1 if $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_success 0 0 0
189 0 ▶ return 0 unless $more_processing;
Branch: 1 branch
BranchTrueFalseTotal
unless $more_processing 0 0 0
190  
191 0 $response = $self->process_request($connection, 'SessionSetup',
192 security_buffer => $connection->auth->generate_spnego(
193 username => $connection->username,
194 password => $connection->password,
195 ),
196 );
197 0 ▶ if ($response && $response->is_success && $connection->auth->process_spnego($response->security_buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and $response->is_success and $connection->auth->process_spnego($response->security_buffer)) 0 0 0
Condition0%0/6
$response and $response->is_success and $connection->auth->process_spnego($response->security_buffer)
!ll&&!rl&&r
000
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/3
$response and $response->is_success and $connection->auth->process_spnego($response->security_buffer)
$response $response->is_success $connection->auth->process_spnego($response->security_buffer)
Truth table0%0/4
A and B and C
A= $responseB= $response->is_successC= $connection->auth->process_spnego($response->security_buffer)
ABCresult
0XX0
10X0
1100
1111
198 0 ▶ die "Got different session id on second SessionSetup"
Branch: 1 branch
BranchTrueFalseTotal
unless $connection->session_id == $response->header->uid 0 0 0
199 unless $connection->session_id == $response->header->uid;
200 0 return 1;
201 }
202  
203 0 return 0;
204 }
205  
206 sub check_session ($$) {
207 0 ▶ my $self = shift;
Subroutine check_session: not called CC=2 SCAR=17.9
Pod undocumented
208 0 my $connection = shift;
209  
210 return
211 0 ▶ $self->process_negotiate_if_needed($connection) &&
Condition0%0/3
$self->process_negotiate_if_needed($connection) && $self->process_sessionsetup_if_needed($connection)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->process_negotiate_if_needed($connection) && $self->process_sessionsetup_if_needed($connection)
$self->process_negotiate_if_needed($connection) $self->process_sessionsetup_if_needed($connection)
Truth table0%0/3
A && B
A= $self->process_negotiate_if_needed($connection)B= $self->process_sessionsetup_if_needed($connection)
ABresult
0X0
100
111
212 $self->process_sessionsetup_if_needed($connection);
213 }
214  
215 sub connect_tree ($%) {
216 0 ▶ my $self = shift;
Subroutine connect_tree: not called CC=7 SCAR=40.3
Pod undocumented
217 0 my %options = @_;
218  
219 0 ▶ my $connection = $self->get_curr_connection || return;
Condition0%0/2
$self->get_curr_connection || (return)
l!l
00
MC/DC0%0/1
$self->get_curr_connection || (return)
$self->get_curr_connection
Truth table0%0/2
A
A= $self->get_curr_connection
Aresult
11
00
220  
221 my ($addr, $share, $username, $password) =
222 0 ▶ map { $connection->{$_} || $options{$_} || die "No $_ to connect_tree\n" }
Branch: 1 branch
BranchTrueFalseTotal
$connection->{$_} or $options{$_} or die "No $_ to connect_tree\n" 0 0 0
Condition0%0/3
$connection->{$_} or $options{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$connection->{$_} or $options{$_}
$connection->{$_} $options{$_}
Truth table0%0/3
A or B
A= $connection->{$_}B= $options{$_}
ABresult
1X1
011
000
223 qw(addr share username password);
224  
225 0 ▶ return unless $self->check_session($connection);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_session($connection) 0 0 0
226  
227 0 $addr =~ s/:\d+//;
228 0 my $response = $self->process_request($connection, 'TreeConnect', uri => "\\\\$addr\\$share");
229 0 ▶ if ($response && $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and $response->is_success) 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
230 0 my $tree = SMB::Tree->new(
231 $share, $response->header->tid,
232 addr => $addr,
233 client => $self,
234 cwd => '',
235 );
236 0 $connection->tree($tree);
237 0 return $tree;
238 }
239  
240 0 return;
241 }
242  
243 sub _normalize_path ($$;$) {
244 0 ▶ my $path = shift // '';
Subroutine _normalize_path: not called CC=8 SCAR=42.8
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
245 0 ▶ my $base = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
246 0 ▶ my $to_dos = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
247  
248 0 ▶ $path = "$base/$path" if $path =~ m!^[^/]!;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^[^/]] 0 0 0
249  
250 0 $path =~ s![/\\]+$!/!g; # to unix
251 # remove "./", "any/../", "../.." at the end
252 0 ▶ while ($path =~ s=(^|/)\.(?:/|$)=$1=g) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[(^|/)\.(?:/|$)][$1]g) 0 0 0
253 0 ▶ while ($path =~ s=(^|/)(?!\.\./)[^/]+/\.\.(?:/|$)=$1=g) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[(^|/)(?!\.\./)[^/]+/\.\.(?:/|$)][$1]g) 0 0 0
254 0 $path =~ s!(?:(?:^|/)\.\.)+/?$!!;
255 0 $path =~ s!/$!!;
256  
257 0 ▶ if ($to_dos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_dos) 0 0 0
258 0 $path =~ s=^/==;
259 0 $path =~ s=/=\\=g;
260 }
261  
262 0 return $path;
263 }
264  
265 sub _basename ($;$) {
266 0 ▶ my $path = shift // '';
Subroutine _basename: not called CC=5 SCAR=34.0
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
267 0 ▶ my $is_dos = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
268  
269 0 ▶ my $delim = $is_dos ? '\\' : '/';
Branch: 1 branch
BranchTrueFalseTotal
$is_dos ? : 0 0 0
270  
271 0 ▶ return $path =~ /.*\Q$delim\E(.*)/ ? $1 : $path;
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /.*\Q$delim\E(.*)/ ? : 0 0 0
272 }
273  
274 sub perform_tree_command ($$$@) {
275 0 ▶ my $self = shift;
Subroutine perform_tree_command: not called CC=50 SCAR=78.4
Pod undocumented
276 0 my $tree = shift;
277 0 my $command = shift;
278  
279 0 ▶ my $connection = $self->find_connection_by_tree($tree) || return;
Condition0%0/2
$self->find_connection_by_tree($tree) || (return)
l!l
00
MC/DC0%0/1
$self->find_connection_by_tree($tree) || (return)
$self->find_connection_by_tree($tree)
Truth table0%0/2
A
A= $self->find_connection_by_tree($tree)
Aresult
11
00
280  
281 0 ▶ if ($command eq 'chdir') {
Branch: 5 branches
BranchTrueFalseTotal
if ($command eq 'chdir') { } 0 0 0
elsif ($command eq 'find') { } 0 0 0
elsif ($command eq 'dnload') { } 0 0 0
elsif ($command eq 'upload') { } 0 0 0
elsif ($command eq 'remove') { } 0 0 0
282 0 ▶ my $dir = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
283  
284 0 $tree->cwd(_normalize_path($dir, $tree->cwd));
285 } elsif ($command eq 'find') {
286 0 ▶ my $pattern = _normalize_path(shift || "*", $tree->cwd, 1);
Condition0%0/2
shift() || '*'
l!l
00
MC/DC0%0/1
shift() || '*'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
287 0 ▶ my $dirname = $pattern =~ /^(.*)\\(.*)/ ? $1 : "";
Branch: 1 branch
BranchTrueFalseTotal
$pattern =~ /^(.*)\\(.*)/ ? : 0 0 0
288 0 ▶ $pattern = $2 if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 0 0 0
289  
290 0 my $response = $self->process_request($connection, 'Create',
291 file_name => $dirname,
292 file_attributes => SMB::File::ATTR_DIRECTORY,
293 );
294 0 ▶ return unless $response && $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
295 0 my $fid = $response->fid;
296 0 $response = $self->process_request($connection, 'QueryDirectory',
297 file_pattern => $pattern,
298 fid => $fid,
299 );
300 0 ▶ my $files = $response && $response->is_success ? $response->files : undef;
Branch: 1 branch
BranchTrueFalseTotal
$response && $response->is_success ? : 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
301 0 $self->process_request($connection, 'Close',
302 fid => $fid,
303 );
304  
305 0 ▶ return wantarray ? @$files : $files;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
306 } elsif ($command eq 'dnload') {
307 0 ▶ my $filename = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
308 0 ▶ return $self->err("No filename") if $filename eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 0 0 0
309 0 $filename = _normalize_path($filename, $tree->cwd, 1);
310 0 ▶ my $dst_filename = _normalize_path(shift || _basename($filename, 1), '.');
Condition0%0/3
shift() || &_basename($filename, 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || &_basename($filename, 1)
shift() &_basename($filename, 1)
Truth table0%0/3
A || B
A= shift()B= &_basename($filename, 1)
ABresult
1X1
011
000
311  
312 0 my $response = $self->process_request($connection, 'Create',
313 file_name => $filename,
314 );
315 0 ▶ return unless $response && $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
316 0 my $file = $response->openfile->file;
317 0 my $fid = $response->fid;
318 0 my $remaining = $file->end_of_file;
319 0 my $time = $file->mtime;
320 0 my $content = '';
321 0 my $offset = 0;
322 0 ▶ while ($remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining) 0 0 0
323 0 ▶ my $length = $remaining >= 65536 ? 65536 : $remaining;
Branch: 1 branch
BranchTrueFalseTotal
$remaining >= 65536 ? : 0 0 0
324 0 $remaining -= $length;
325 0 $response = $self->process_request($connection, 'Read',
326 fid => $fid,
327 offset => $offset,
328 length => $length,
329 remaining_bytes => $remaining,
330 );
331 0 ▶ return unless $response && $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
332 0 my $read = $response->length;
333 0 ▶ return $self->err("Unexpected $read bytes read instead of $length at offset $offset")
Branch: 1 branch
BranchTrueFalseTotal
if $read != $length 0 0 0
334 if $read != $length;
335 0 $content .= $response->buffer;
336 0 $offset += $length;
337 }
338 0 $self->process_request($connection, 'Close',
339 fid => $fid,
340 );
341  
342 0 ▶ open DST, '>', $dst_filename
Branch: 1 branch
BranchTrueFalseTotal
open DST, ">", $dst_filename or return $self->err("Can't open $dst_filename for write: $!") 0 0 0
343 or return $self->err("Can't open $dst_filename for write: $!");
344 0 ▶ print DST $content
Branch: 1 branch
BranchTrueFalseTotal
print DST $content or return $self->err("Can't write content to ${dst_filename}: $!") 0 0 0
345 or return $self->err("Can't write content to $dst_filename: $!");
346 0 ▶ close DST
Branch: 1 branch
BranchTrueFalseTotal
close DST or return $self->err("Can't close $dst_filename after write: $!") 0 0 0
347 or return $self->err("Can't close $dst_filename after write: $!");
348  
349 # consider to set $time on file
350 0 return 1;
351 } elsif ($command eq 'upload') {
352 0 ▶ my $filename = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
353 0 ▶ return $self->err("No filename") if $filename eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 0 0 0
354 0 $filename = _normalize_path($filename, '.');
355 0 ▶ my $dst_filename = _normalize_path(shift || _basename($filename), $tree->cwd, 1);
Condition0%0/3
shift() || &_basename($filename)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || &_basename($filename)
shift() &_basename($filename)
Truth table0%0/3
A || B
A= shift()B= &_basename($filename)
ABresult
1X1
011
000
356  
357 0 local $/ = undef;
358 0 ▶ open SRC, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open SRC, "<", $filename or return $self->err("Can't open $filename for read: $!") 0 0 0
359 or return $self->err("Can't open $filename for read: $!");
360 0 ▶ my $content = <SRC>
Condition0%0/2
readline SRC // (return $self->err("Can't read content from ${filename}: $!"))
l!l
00
MC/DC0%0/1
readline SRC // (return $self->err("Can't read content from ${filename}: $!"))
readline SRC
Truth table0%0/2
A
A= readline SRC
Aresult
11
00
361 // return $self->err("Can't read content from $filename: $!");
362 0 ▶ close SRC
Branch: 1 branch
BranchTrueFalseTotal
close SRC or return $self->err("Can't close $filename after read: $!") 0 0 0
363 or return $self->err("Can't close $filename after read: $!");
364  
365 0 my $response = $self->process_request($connection, 'Create',
366 file_name => $dst_filename,
367 options => SMB::v2::Command::Create::OPTIONS_NON_DIRECTORY_FILE,
368 access_mask => 0x12019f,
369 disposition => SMB::File::DISPOSITION_OVERWRITE_IF,
370 );
371 0 ▶ return unless $response && $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
372 0 my $fid = $response->fid;
373 0 my $remaining = length($content);
374 0 my $offset = 0;
375 0 ▶ while ($remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining) 0 0 0
376 0 ▶ my $length = $remaining >= 65536 ? 65536 : $remaining;
Branch: 1 branch
BranchTrueFalseTotal
$remaining >= 65536 ? : 0 0 0
377 0 $remaining -= $length;
378 0 $response = $self->process_request($connection, 'Write',
379 fid => $fid,
380 offset => $offset,
381 remaining_bytes => $remaining,
382 buffer => substr($content, $offset, $length),
383 );
384 0 ▶ return unless $response && $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
385 0 my $written = $response->length;
386 0 ▶ return $self->err("Unexpected $written bytes written instead of $length at offset $offset")
Branch: 1 branch
BranchTrueFalseTotal
if $written != $length 0 0 0
387 if $written != $length;
388 0 $offset += $length;
389 }
390 0 $self->process_request($connection, 'Close',
391 fid => $fid,
392 );
393  
394 0 return 1;
395 } elsif ($command eq 'remove') {
396 0 ▶ my $filename = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
397 0 ▶ return $self->err("No filename") if $filename eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 0 0 0
398 0 $filename = _normalize_path($filename, $tree->cwd, 1);
399  
400 0 ▶ my $is_dir = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
401 0 ▶ my $options = ($is_dir
Branch: 1 branch
BranchTrueFalseTotal
$is_dir ? : 0 0 0
402 ? SMB::v2::Command::Create::OPTIONS_DIRECTORY_FILE
403 : SMB::v2::Command::Create::OPTIONS_NON_DIRECTORY_FILE
404 ) | SMB::v2::Command::Create::OPTIONS_DELETE_ON_CLOSE;
405 0 my $response = $self->process_request($connection, 'Create',
406 file_name => $filename,
407 options => $options,
408 access_mask => 0x10081,
409 );
410 0 ▶ return unless $response && $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
411 0 my $fid = $response->fid;
412 0 $self->process_request($connection, 'Close',
413 fid => $fid,
414 );
415 0 ▶ return unless $response && $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response and $response->is_success 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
416  
417 0 return 1;
418 }
419  
420 0 return;
421 }
422  
423 sub on_response ($$$$) {
424 0 ▶ my $self = shift;
Subroutine on_response: not called CC=1 SCAR=6.9
Pod undocumented
425 0 my $connection = shift;
426 0 my $response = shift;
427 0 my $request = shift;
428  
429 0 return 0;
430 }
431  
432 sub wait_for_response ($$) {
433 0 ▶ my $self = shift;
Subroutine wait_for_response: not called CC=4 SCAR=30.0
Pod undocumented
434 0 my $connection = shift;
435 0 my $request = $connection->sent_request;
436  
437 0 ▶ return unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 0 0
438  
439 0 my $response = $connection->recv_command;
440 0 ▶ if (!$response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 0 0
441 0 $self->delete_connection($connection);
442 0 return;
443 }
444  
445 0 ▶ unless ($response->is_response_to($request)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_response_to($request)) 0 0 0
446 0 $self->err("Unexpected: " . $response->dump);
447 0 return;
448 }
449  
450 0 return $response;
451 }
452  
453 1 1;
↑ Summary blib/lib/SMB/Auth.pm »