lib/SMB/Server.pm

filter: Statementstmt 17.8% 25 / 140 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 57.1% 8 / 14 Podpod 16.6% 1 / 6 totaltotal 10.7% 34 / 317 SCAR 79.5
CC
61
Cov
9%
on_command
1406.0
new
306.0
run
72.0
CRAP 2844.3
« blib/lib/SMB/Client.pm ↑ Summary blib/lib/SMB.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::Server;
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 File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use SMB::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use SMB::v2::Command::Negotiate;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use SMB::v2::Command::Create;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use SMB::v2::Command::QueryDirectory;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub new ($%) {
30 0 ▶ my $class = shift;
Subroutine new: not called CC=17 SCAR=57.2
Pod documented
31 0 my %options = @_;
32  
33 0 my $share_roots = delete $options{share_roots};
34 0 my $port = delete $options{port};
35 0 my $fifo_filename = delete $options{fifo_filename};
36  
37 0 ▶ die "Neither port nor fifo-filename is specified for $class\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $port or $fifo_filename 0 0 0
Condition0%0/3
$port or $fifo_filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$port or $fifo_filename
$port $fifo_filename
Truth table0%0/3
A or B
A= $portB= $fifo_filename
ABresult
1X1
011
000
38 unless $port || $fifo_filename;
39  
40 0 ▶ my $main_socket = $fifo_filename
Branch: 1 branch
BranchTrueFalseTotal
$fifo_filename ? : 0 0 0
41 ? IO::Socket::UNIX->new(Listen => 1, Local => $fifo_filename)
42 : IO::Socket::INET->new(Listen => 1, LocalPort => $port, Reuse => 1);
43  
44 0 ▶ my $listen_label = $fifo_filename ? "fifo $fifo_filename" : "port $port";
Branch: 1 branch
BranchTrueFalseTotal
$fifo_filename ? : 0 0 0
45 0 ▶ die "Can't open $listen_label: $!\n" unless $main_socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $main_socket 0 0 0
46  
47 0 ▶ $options{passwd_filename} //= "$FindBin::Bin/../conf/passwd.txt";
Condition0%0/2
$options{"passwd_filename"} //= "$FindBin::Bin/../conf/passwd.txt"
l!l
00
MC/DC0%0/1
$options{"passwd_filename"} //= "$FindBin::Bin/../conf/passwd.txt"
$options{"passwd_filename"}
Truth table0%0/2
A
A= $options{"passwd_filename"}
Aresult
11
00
48  
49 0 my $self = $class->SUPER::new(
50 %options,
51 main_socket => $main_socket,
52 client_id => 0, # running index
53 );
54  
55 0 $self->socket_pool->add($main_socket);
56  
57 0 ▶ if (!$share_roots && $FindBin::Bin) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $share_roots and $FindBin::Bin) 0 0 0
Condition0%0/3
not $share_roots and $FindBin::Bin
!ll&&!rl&&r
000
MC/DC0%0/2
not $share_roots and $FindBin::Bin
not $share_roots $FindBin::Bin
Truth table0%0/3
not(A) and B
A= not $share_rootsB= $FindBin::Bin
ABresult
0X0
100
111
58 0 my $shares_dir = "$FindBin::Bin/../shares";
59 0 ▶ $share_roots = { map { basename($_) => $_ } grep { -d $_ && -x _ && -r _ } glob("$shares_dir/*") }
Branch: 2 branches
BranchTrueFalseTotal
if -d $shares_dir 0 0 0
-d $_ and -x _ and -r _ 0 0 0
Condition0%0/3
-d $_ and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-d $_ and -x _
-d $_ -x _
Truth table0%0/3
A and B
A= -d $_B= -x _
ABresult
0X0
100
111
60 if -d $shares_dir;
61 }
62 0 ▶ unless ($share_roots) {
Branch: 4 branches
BranchTrueFalseTotal
if ($share_roots) { } 0 0 0
if (do { ref $share_roots eq '' and $share_roots eq '-' }) { } 0 0 0
elsif (ref $share_roots ne 'HASH') { } 0 0 0
elsif (not %$share_roots) { } 0 0 0
63 0 $self->err("No share_roots specified and no shares/ autodetected");
64 0 $share_roots = {};
65 ▶ } elsif (ref($share_roots) eq '' && $share_roots eq '-') {
Condition0%0/3
ref $share_roots eq "" and $share_roots eq "-"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $share_roots eq "" and $share_roots eq "-"
ref $share_roots eq "" $share_roots eq "-"
Truth table0%0/3
A and B
A= ref $share_roots eq ""B= $share_roots eq "-"
ABresult
0X0
100
111
66 # special syntax to request a share-less server, don't complain
67 0 $share_roots = {};
68 } elsif (ref($share_roots) ne 'HASH') {
69 0 $self->err("Invalid share_roots ($share_roots) specified");
70 0 $share_roots = {};
71 } elsif (!%$share_roots) {
72 0 $self->err("No shares to manage, specify non-empty share_roots hash");
73 }
74 0 $self->{share_roots} = $share_roots;
75  
76 0 $self->msg("$class started, listening on $listen_label");
77  
78 0 return $self;
79 }
80  
81 sub on_connect ($$) {
82 0 ▶ my $self = shift;
Subroutine on_connect: not called CC=2 SCAR=17.9
Pod undocumented
83 0 my $connection = shift;
84  
85 # intended to be overriden in sub-classes
86  
87 0 my $auth = $connection->auth;
88 0 ▶ $auth->load_user_passwords($self->passwd_filename)
Branch: 1 branch
BranchTrueFalseTotal
$auth->load_user_passwords($self->passwd_filename) or $auth->user_passwords({"test", 12345}) 0 0 0
89 or $auth->user_passwords({ test => '12345' });
90 }
91  
92 sub on_disconnect ($$) {
93 0 ▶ my $self = shift;
Subroutine on_disconnect: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $connection = shift;
95  
96 # intended to be overriden in sub-classes
97 }
98  
99 sub recv_command ($$) {
100 0 ▶ my $self = shift;
Subroutine recv_command: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my $connection = shift;
102  
103 0 return $connection->recv_command;
104 }
105  
106 sub on_command ($$$) {
107 0 ▶ my $self = shift;
Subroutine on_command: not called CC=37 SCAR=72.5
Pod undocumented
108 0 my $connection = shift;
109 0 my $command = shift;
110  
111 0 my $tid = $command->header->tid;
112 0 ▶ my $tree = $tid ? (grep { $_->id == $tid } @{$connection->{trees}})[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
113 0 ▶ $command->{tree} = $tree if $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $tree 0 0 0
114  
115 0 ▶ if ($command->is_smb1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->is_smb1) 0 0 0
116 0 ▶ if ($command->is('Negotiate') && $command->supports_protocol(2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->is("Negotiate") and $command->supports_protocol(2)) 0 0 0
Condition0%0/3
$command->is("Negotiate") and $command->supports_protocol(2)
!ll&&!rl&&r
000
MC/DC0%0/2
$command->is("Negotiate") and $command->supports_protocol(2)
$command->is("Negotiate") $command->supports_protocol(2)
Truth table0%0/3
A and B
A= $command->is("Negotiate")B= $command->supports_protocol(2)
ABresult
0X0
100
111
117 0 $command = SMB::v2::Command::Negotiate->new_from_v1($command);
118 }
119 }
120  
121 0 ▶ if ($command->is_smb2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->is_smb2) 0 0 0
122 0 my $error = 0;
123 0 my $fid = $command->{fid};
124 0 my $openfile = undef;
125  
126 0 ▶ if (($tid || exists $command->{fid}) && !$tree) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tid || exists $command->{'fid'} and not $tree) { } 0 0 0
elsif ($fid) { } 0 0 0
Condition0%0/6
$tid || exists $command->{"fid"} and not $tree
!ll&&!rl&&r
000
$tid or exists $command->{"fid"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$tid || exists $command->{"fid"} and not $tree
$tid exists $command->{"fid"} not $tree
Truth table0%0/5
A or B and not(C)
A= $tidB= exists $command->{"fid"}C= not $tree
ABCresult
00X0
1X00
0100
1X11
0111
127 0 $error = SMB::STATUS_SMB_BAD_TID;
128 }
129 elsif ($fid) {
130 0 ▶ $openfile = $connection->{openfiles}{@$fid}
Branch: 1 branch
BranchTrueFalseTotal
$openfile = $connection->{"openfiles"}{@$fid} or $error = 3221225768 0 0 0
131 or $error = SMB::STATUS_FILE_CLOSED;
132 0 $command->openfile($openfile);
133 }
134  
135 0 ▶ if ($error) {
Branch: 8 branches
BranchTrueFalseTotal
if ($error) { } 0 0 0
elsif ($command->is('Negotiate')) { } 0 0 0
elsif ($command->is('SessionSetup')) { } 0 0 0
elsif ($command->is('TreeConnect')) { } 0 0 0
elsif ($command->is('Create')) { } 0 0 0
elsif ($command->is('Close')) { } 0 0 0
elsif ($command->is('Read')) { } 0 0 0
elsif ($command->is('QueryDirectory')) { } 0 0 0
136 # skip command processing
137 }
138 elsif ($command->is('Negotiate')) {
139 0 $command->security_buffer($connection->auth->generate_spnego(is_initial => 1));
140 }
141 elsif ($command->is('SessionSetup')) {
142 0 $connection->auth->process_spnego($command->security_buffer);
143 0 $command->security_buffer($connection->auth->generate_spnego);
144 0 $command->header->uid($connection->id);
145 0 my $auth_completed = $connection->auth->auth_completed;
146 0 ▶ $error = SMB::STATUS_LOGON_FAILURE
Branch: 1 branch
BranchTrueFalseTotal
if not $command->security_buffer or defined $auth_completed and not $auth_completed 0 0 0
Condition0%0/6
not $command->security_buffer or defined $auth_completed and not $auth_completed
l!l&&r!l&&!r
000
defined $auth_completed and not $auth_completed
!ll&&!rl&&r
000
MC/DC0%0/3
not $command->security_buffer or defined $auth_completed and not $auth_completed
not $command->security_buffer defined $auth_completed not $auth_completed
Truth table0%0/4
not(A) or B and not(C)
A= not $command->security_bufferB= defined $auth_completedC= not $auth_completed
ABCresult
1XX1
0111
00X0
0100
147 if !$command->security_buffer
148 || defined $auth_completed && !$auth_completed;
149 }
150 elsif ($command->is('TreeConnect')) {
151 0 my ($addr, $share) = $self->parse_share_uri($command->verify_uri);
152 0 my $tree_root = $self->share_roots->{$share};
153 0 ▶ if ($tree_root || $share eq 'IPC$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree_root or $share eq 'IPC$') { } 0 0 0
Condition0%0/3
$tree_root or $share eq "IPC\$"
l!l&&r!l&&!r
000
MC/DC0%0/2
$tree_root or $share eq "IPC\$"
$tree_root $share eq "IPC\$"
Truth table0%0/3
A or B
A= $tree_rootB= $share eq "IPC\$"
ABresult
1X1
011
000
154 0 my $tid = $command->header->tid(@{$connection->{trees}} + 1);
155 0 push @{$connection->{trees}}, SMB::Tree->new($share, $tid, root => $tree_root);
156 } else {
157 0 $error = SMB::STATUS_BAD_NETWORK_NAME;
158 }
159 }
160 elsif ($command->is('Create')) {
161 0 my $file = SMB::File->new(
162 name => $command->file_name,
163 share_root => $tree->root,
164 is_ipc => $tree->is_ipc,
165 );
166 0 my $disposition = $command->disposition;
167 0 ▶ if ($file->exists && $disposition == SMB::File::DISPOSITION_OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists and $disposition == 1) 0 0 0
Condition0%0/3
$file->exists and $disposition == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$file->exists and $disposition == 1
$file->exists $disposition == 1
Truth table0%0/3
A and B
A= $file->existsB= $disposition == 1
ABresult
0X0
100
111
168 0 ▶ if ($command->requested_directory && !$file->is_directory) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command->requested_directory and not $file->is_directory) { } 0 0 0
elsif ($command->requested_non_directory and $file->is_directory) { } 0 0 0
Condition0%0/3
$command->requested_directory and not $file->is_directory
!ll&&!rl&&r
000
MC/DC0%0/2
$command->requested_directory and not $file->is_directory
$command->requested_directory not $file->is_directory
Truth table0%0/3
A and not(B)
A= $command->requested_directoryB= not $file->is_directory
ABresult
0X0
100
111
169 0 $error = SMB::STATUS_NOT_A_DIRECTORY;
170 ▶ } elsif ($command->requested_non_directory && $file->is_directory) {
Condition0%0/3
$command->requested_non_directory and $file->is_directory
!ll&&!rl&&r
000
MC/DC0%0/2
$command->requested_non_directory and $file->is_directory
$command->requested_non_directory $file->is_directory
Truth table0%0/3
A and B
A= $command->requested_non_directoryB= $file->is_directory
ABresult
0X0
100
111
171 0 $error = SMB::STATUS_FILE_IS_A_DIRECTORY;
172 }
173 }
174 0 ▶ unless ($error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
175 0 $openfile = $file->open_by_disposition($disposition);
176 0 ▶ if ($openfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($openfile) { } 0 0 0
177 0 $fid = [ ++$connection->{last_fid}, 0 ];
178 0 $connection->{openfiles}{@$fid} = $openfile;
179 0 $command->fid($fid);
180 0 $command->openfile($openfile);
181 } else {
182 0 $error = SMB::STATUS_NO_SUCH_FILE;
183 }
184 }
185 }
186 elsif ($command->is('Close')) {
187 0 $openfile->close;
188 0 delete $connection->{openfiles}{@$fid};
189 }
190 elsif ($command->is('Read')) {
191 $command->{buffer} = $openfile->file->read(
192 length => $command->{length},
193 offset => $command->{offset},
194 minlen => $command->{minimum_count},
195 remain => $command->{remaining_bytes},
196 0 );
197 0 ▶ $error = SMB::STATUS_END_OF_FILE unless defined $command->{buffer};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command->{"buffer"} 0 0 0
198 }
199 elsif ($command->is('QueryDirectory')) {
200 0 ▶ $command->file_index($openfile->last_index)
Branch: 1 branch
BranchTrueFalseTotal
unless $command->flags & 4 0 0 0
201 unless $command->flags & SMB::v2::Command::QueryDirectory::FLAGS_INDEX;
202 0 $command->{files} = $openfile->file->find_files(
203 pattern => $command->file_pattern,
204 start_idx => $command->file_index,
205 );
206 0 ▶ $error = SMB::STATUS_INVALID_PARAMETER unless defined $command->{files};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command->{"files"} 0 0 0
207 }
208 0 $command->prepare_response;
209 0 ▶ $command->set_status($error) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
210 0 $connection->send_command($command);
211 0 return;
212 }
213  
214 0 $self->msg("Command $command ignored; missing functionality");
215 }
216  
217 sub run ($) {
218 0 ▶ my $self = shift;
Subroutine run: not called CC=8 SCAR=42.8
Pod undocumented
219  
220 0 my $socket_pool = $self->socket_pool;
221 0 my $connections = $self->connections;
222  
223 0 ▶ while (my @ready_sockets = $socket_pool->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready_sockets) = $socket_pool->can_read) 0 0 0
224 0 foreach my $socket (@ready_sockets) {
225 0 ▶ if ($socket == $self->main_socket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket == $self->main_socket) { } 0 0 0
226 0 ▶ my $client_socket = $socket->accept || next;
Condition0%0/2
$socket->accept || (next)
l!l
00
MC/DC0%0/1
$socket->accept || (next)
$socket->accept
Truth table0%0/2
A
A= $socket->accept
Aresult
11
00
227 my $connection = $self->add_connection(
228 $client_socket, ++$self->{client_id},
229 0 trees => [],
230 last_fid => 0,
231 openfiles => {},
232 );
233 0 ▶ unless ($connection) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connection) 0 0 0
234 0 $socket->close;
235 0 next;
236 }
237 0 $self->on_connect($connection);
238 }
239 else {
240 0 ▶ my $connection = $self->get_connection($socket)
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $self->get_connection($socket) or die "Unexpected data on unmanaged $socket" 0 0 0
241 or die "Unexpected data on unmanaged $socket";
242 0 my $command = $self->recv_command($connection);
243 0 ▶ if (!$command) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 0 0 0
244 0 $self->on_disconnect($connection);
245 0 $self->delete_connection($connection);
246 0 next;
247 }
248 0 $self->on_command($connection, $command);
249 }
250 }
251 }
252 }
253  
254 1 1;
« blib/lib/SMB/Client.pm ↑ Summary blib/lib/SMB.pm »