| 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 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use parent 'SMB::Agent'; | ||
| 22 | |||
| 23 | use File::Basename qw(basename); | ||
| 24 | use SMB::Tree; | ||
| 25 | use SMB::v2::Command::Negotiate; | ||
| 26 | use SMB::v2::Command::Create; | ||
| 27 | use SMB::v2::Command::QueryDirectory; | ||
| 28 | |||
| 29 | sub new ($%) { | ||
| 30 | my $class = shift; | ||
| 31 | my %options = @_; | ||
| 32 | |||
| 33 | my $share_roots = delete $options{share_roots}; | ||
| 34 | my $port = delete $options{port}; | ||
| 35 | my $fifo_filename = delete $options{fifo_filename}; | ||
| 36 | |||
| 37 | die "Neither port nor fifo-filename is specified for $class\n" | ||
| 38 | unless $port || $fifo_filename; | ||
| 39 | |||
| 40 | my $main_socket = $fifo_filename | ||
| 41 | ? IO::Socket::UNIX->new(Listen => 1, Local => $fifo_filename) | ||
| 42 | : IO::Socket::INET->new(Listen => 1, LocalPort => $port, Reuse => 1); | ||
| 43 | |||
| 44 | my $listen_label = $fifo_filename ? "fifo $fifo_filename" : "port $port"; | ||
| 45 | die "Can't open $listen_label: $!\n" unless $main_socket; | ||
| 46 | |||
| 47 | $options{passwd_filename} //= "$FindBin::Bin/../conf/passwd.txt"; | ||
| 48 | |||
| 49 | my $self = $class->SUPER::new( | ||
| 50 | %options, | ||
| 51 | main_socket => $main_socket, | ||
| 52 | client_id => 0, # running index | ||
| 53 | ); | ||
| 54 | |||
| 55 | $self->socket_pool->add($main_socket); | ||
| 56 | |||
| 57 | if (!$share_roots && $FindBin::Bin) { | ||
| 58 | my $shares_dir = "$FindBin::Bin/../shares"; | ||
| 59 | $share_roots = { map { basename($_) => $_ } grep { -d $_ && -x _ && -r _ } glob("$shares_dir/*") } | ||
| 60 | if -d $shares_dir; | ||
| 61 | } | ||
| 62 | unless ($share_roots) { | ||
| 63 | $self->err("No share_roots specified and no shares/ autodetected"); | ||
| 64 | $share_roots = {}; | ||
| 65 | } elsif (ref($share_roots) eq '' && $share_roots eq '-') { | ||
| 66 | # special syntax to request a share-less server, don't complain | ||
| 67 | $share_roots = {}; | ||
| 68 | } elsif (ref($share_roots) ne 'HASH') { | ||
| 69 | $self->err("Invalid share_roots ($share_roots) specified"); | ||
| 70 | $share_roots = {}; | ||
| 71 | } elsif (!%$share_roots) { | ||
| 72 | $self->err("No shares to manage, specify non-empty share_roots hash"); | ||
| 73 | } | ||
| 74 | $self->{share_roots} = $share_roots; | ||
| 75 | |||
| 76 | $self->msg("$class started, listening on $listen_label"); | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub on_connect ($$) { | ||
| 82 | my $self = shift; | ||
| 83 | my $connection = shift; | ||
| 84 | |||
| 85 | # intended to be overriden in sub-classes | ||
| 86 | |||
| 87 | my $auth = $connection->auth; | ||
| 88 | $auth->load_user_passwords($self->passwd_filename) | ||
| 89 | or $auth->user_passwords({ test => '12345' }); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub on_disconnect ($$) { | ||
| 93 | my $self = shift; | ||
| 94 | my $connection = shift; | ||
| 95 | |||
| 96 | # intended to be overriden in sub-classes | ||
| 97 | } | ||
| 98 | |||
| 99 | sub recv_command ($$) { | ||
| 100 | my $self = shift; | ||
| 101 | my $connection = shift; | ||
| 102 | |||
| 103 | return $connection->recv_command; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub on_command ($$$) { | ||
| 107 | my $self = shift; | ||
| 108 | my $connection = shift; | ||
| 109 | my $command = shift; | ||
| 110 | |||
| 111 | my $tid = $command->header->tid; | ||
| 112 | my $tree = $tid ? (grep { $_->id == $tid } @{$connection->{trees}})[0] : undef; | ||
| 113 | $command->{tree} = $tree if $tree; | ||
| 114 | |||
| 115 | if ($command->is_smb1) { | ||
| 116 | if ($command->is('Negotiate') && $command->supports_protocol(2)) { | ||
| 117 | $command = SMB::v2::Command::Negotiate->new_from_v1($command); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | if ($command->is_smb2) { | ||
| 122 | my $error = 0; | ||
| 123 | my $fid = $command->{fid}; | ||
| 124 | my $openfile = undef; | ||
| 125 | |||
| 126 | if (($tid || exists $command->{fid}) && !$tree) { | ||
| 127 | $error = SMB::STATUS_SMB_BAD_TID; | ||
| 128 | } | ||
| 129 | elsif ($fid) { | ||
| 130 | $openfile = $connection->{openfiles}{@$fid} | ||
| 131 | or $error = SMB::STATUS_FILE_CLOSED; | ||
| 132 | $command->openfile($openfile); | ||
| 133 | } | ||
| 134 | |||
| 135 | if ($error) { | ||
| 136 | # skip command processing | ||
| 137 | } | ||
| 138 | elsif ($command->is('Negotiate')) { | ||
| 139 | $command->security_buffer($connection->auth->generate_spnego(is_initial => 1)); | ||
| 140 | } | ||
| 141 | elsif ($command->is('SessionSetup')) { | ||
| 142 | $connection->auth->process_spnego($command->security_buffer); | ||
| 143 | $command->security_buffer($connection->auth->generate_spnego); | ||
| 144 | $command->header->uid($connection->id); | ||
| 145 | my $auth_completed = $connection->auth->auth_completed; | ||
| 146 | $error = SMB::STATUS_LOGON_FAILURE | ||
| 147 | if !$command->security_buffer | ||
| 148 | || defined $auth_completed && !$auth_completed; | ||
| 149 | } | ||
| 150 | elsif ($command->is('TreeConnect')) { | ||
| 151 | my ($addr, $share) = $self->parse_share_uri($command->verify_uri); | ||
| 152 | my $tree_root = $self->share_roots->{$share}; | ||
| 153 | if ($tree_root || $share eq 'IPC$') { | ||
| 154 | my $tid = $command->header->tid(@{$connection->{trees}} + 1); | ||
| 155 | push @{$connection->{trees}}, SMB::Tree->new($share, $tid, root => $tree_root); | ||
| 156 | } else { | ||
| 157 | $error = SMB::STATUS_BAD_NETWORK_NAME; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | elsif ($command->is('Create')) { | ||
| 161 | my $file = SMB::File->new( | ||
| 162 | name => $command->file_name, | ||
| 163 | share_root => $tree->root, | ||
| 164 | is_ipc => $tree->is_ipc, | ||
| 165 | ); | ||
| 166 | my $disposition = $command->disposition; | ||
| 167 | if ($file->exists && $disposition == SMB::File::DISPOSITION_OPEN) { | ||
| 168 | if ($command->requested_directory && !$file->is_directory) { | ||
| 169 | $error = SMB::STATUS_NOT_A_DIRECTORY; | ||
| 170 | } elsif ($command->requested_non_directory && $file->is_directory) { | ||
| 171 | $error = SMB::STATUS_FILE_IS_A_DIRECTORY; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | unless ($error) { | ||
| 175 | $openfile = $file->open_by_disposition($disposition); | ||
| 176 | if ($openfile) { | ||
| 177 | $fid = [ ++$connection->{last_fid}, 0 ]; | ||
| 178 | $connection->{openfiles}{@$fid} = $openfile; | ||
| 179 | $command->fid($fid); | ||
| 180 | $command->openfile($openfile); | ||
| 181 | } else { | ||
| 182 | $error = SMB::STATUS_NO_SUCH_FILE; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | elsif ($command->is('Close')) { | ||
| 187 | $openfile->close; | ||
| 188 | 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 | ); | ||
| 197 | $error = SMB::STATUS_END_OF_FILE unless defined $command->{buffer}; | ||
| 198 | } | ||
| 199 | elsif ($command->is('QueryDirectory')) { | ||
| 200 | $command->file_index($openfile->last_index) | ||
| 201 | unless $command->flags & SMB::v2::Command::QueryDirectory::FLAGS_INDEX; | ||
| 202 | $command->{files} = $openfile->file->find_files( | ||
| 203 | pattern => $command->file_pattern, | ||
| 204 | start_idx => $command->file_index, | ||
| 205 | ); | ||
| 206 | $error = SMB::STATUS_INVALID_PARAMETER unless defined $command->{files}; | ||
| 207 | } | ||
| 208 | $command->prepare_response; | ||
| 209 | $command->set_status($error) if $error; | ||
| 210 | $connection->send_command($command); | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | |||
| 214 | $self->msg("Command $command ignored; missing functionality"); | ||
| 215 | } | ||
| 216 | |||
| 217 | sub run ($) { | ||
| 218 | my $self = shift; | ||
| 219 | |||
| 220 | my $socket_pool = $self->socket_pool; | ||
| 221 | my $connections = $self->connections; | ||
| 222 | |||
| 223 | while (my @ready_sockets = $socket_pool->can_read) { | ||
| 224 | foreach my $socket (@ready_sockets) { | ||
| 225 | if ($socket == $self->main_socket) { | ||
| 226 | my $client_socket = $socket->accept || next; | ||
| 227 | my $connection = $self->add_connection( | ||
| 228 | $client_socket, ++$self->{client_id}, | ||
| 229 | trees => [], | ||
| 230 | last_fid => 0, | ||
| 231 | openfiles => {}, | ||
| 232 | ); | ||
| 233 | unless ($connection) { | ||
| 234 | $socket->close; | ||
| 235 | next; | ||
| 236 | } | ||
| 237 | $self->on_connect($connection); | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | my $connection = $self->get_connection($socket) | ||
| 241 | or die "Unexpected data on unmanaged $socket"; | ||
| 242 | my $command = $self->recv_command($connection); | ||
| 243 | if (!$command) { | ||
| 244 | $self->on_disconnect($connection); | ||
| 245 | $self->delete_connection($connection); | ||
| 246 | next; | ||
| 247 | } | ||
| 248 | $self->on_command($connection, $command); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | 1; |