| 1 |
# Part of this code was borrowed from Richard Jones's Net::FTPServer |
| 2 |
# http://www.annexia.org/freeware/netftpserver |
| 3 |
|
| 4 |
package FTPServer; |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
|
| 9 |
use Cwd; |
| 10 |
use Socket; |
| 11 |
use IO::Socket::INET; |
| 12 |
use IO::Seekable; |
| 13 |
use POSIX qw(strftime); |
| 14 |
|
| 15 |
my $log = undef; |
| 16 |
my $GOT_SIGURG = 0; |
| 17 |
|
| 18 |
# CONSTANTS |
| 19 |
|
| 20 |
# connection states |
| 21 |
my %_connection_states = ( |
| 22 |
'NEWCONN' => 0x01, |
| 23 |
'WAIT4PWD' => 0x02, |
| 24 |
'LOGGEDIN' => 0x04, |
| 25 |
'TWOSOCKS' => 0x08, |
| 26 |
); |
| 27 |
|
| 28 |
# subset of FTP commands supported by these server and the respective |
| 29 |
# connection states in which they are allowed |
| 30 |
my %_commands = ( |
| 31 |
|
| 32 |
# Standard commands from RFC 959. |
| 33 |
'CWD' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS}, |
| 34 |
|
| 35 |
# 'EPRT' => $_connection_states{LOGGEDIN}, |
| 36 |
# 'EPSV' => $_connection_states{LOGGEDIN}, |
| 37 |
'LIST' => $_connection_states{TWOSOCKS}, |
| 38 |
|
| 39 |
# 'LPRT' => $_connection_states{LOGGEDIN}, |
| 40 |
# 'LPSV' => $_connection_states{LOGGEDIN}, |
| 41 |
'PASS' => $_connection_states{WAIT4PWD}, |
| 42 |
'PASV' => $_connection_states{LOGGEDIN}, |
| 43 |
'PORT' => $_connection_states{LOGGEDIN}, |
| 44 |
'PWD' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS}, |
| 45 |
'QUIT' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS}, |
| 46 |
'REST' => $_connection_states{TWOSOCKS}, |
| 47 |
'RETR' => $_connection_states{TWOSOCKS}, |
| 48 |
'SYST' => $_connection_states{LOGGEDIN}, |
| 49 |
'TYPE' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS}, |
| 50 |
'USER' => $_connection_states{NEWCONN}, |
| 51 |
|
| 52 |
# From ftpexts Internet Draft. |
| 53 |
'SIZE' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS}, |
| 54 |
); |
| 55 |
|
| 56 |
# COMMAND-HANDLING ROUTINES |
| 57 |
|
| 58 |
sub _CWD_command |
| 59 |
{ |
| 60 |
my ($conn, $cmd, $path) = @_; |
| 61 |
my $paths = $conn->{'paths'}; |
| 62 |
|
| 63 |
local $_; |
| 64 |
my $new_path = FTPPaths::path_merge($conn->{'dir'}, $path); |
| 65 |
|
| 66 |
# Split the path into its component parts and process each separately. |
| 67 |
if (!$paths->dir_exists($new_path)) |
| 68 |
{ |
| 69 |
print {$conn->{socket}} "550 Directory not found.\r\n"; |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$conn->{'dir'} = $new_path; |
| 74 |
print {$conn->{socket}} "200 directory changed to $new_path.\r\n"; |
| 75 |
} |
| 76 |
|
| 77 |
sub _LIST_command |
| 78 |
{ |
| 79 |
my ($conn, $cmd, $path) = @_; |
| 80 |
my $paths = $conn->{'paths'}; |
| 81 |
|
| 82 |
my $ReturnEmptyList = |
| 83 |
($paths->GetBehavior('list_empty_if_list_a') && $path eq '-a'); |
| 84 |
my $SkipHiddenFiles = |
| 85 |
($paths->GetBehavior('list_no_hidden_if_list') && (!$path)); |
| 86 |
|
| 87 |
if ($paths->GetBehavior('list_fails_if_list_a') && $path eq '-a') |
| 88 |
{ |
| 89 |
print {$conn->{socket}} "500 Unknown command\r\n"; |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
if (!$paths->GetBehavior('list_dont_clean_path')) |
| 94 |
{ |
| 95 |
# This is something of a hack. Some clients expect a Unix server |
| 96 |
# to respond to flags on the 'ls command line'. Remove these flags |
| 97 |
# and ignore them. This is particularly an issue with ncftp 2.4.3. |
| 98 |
$path =~ s/^-[a-zA-Z0-9]+\s?//; |
| 99 |
} |
| 100 |
|
| 101 |
my $dir = $conn->{'dir'}; |
| 102 |
|
| 103 |
print STDERR "_LIST_command - dir is: $dir\n"; |
| 104 |
|
| 105 |
# Parse the first elements of the path until we find the appropriate |
| 106 |
# working directory. |
| 107 |
local $_; |
| 108 |
|
| 109 |
my $listing; |
| 110 |
if (!$ReturnEmptyList) |
| 111 |
{ |
| 112 |
$dir = FTPPaths::path_merge($dir, $path); |
| 113 |
$listing = $paths->get_list($dir, $SkipHiddenFiles); |
| 114 |
unless ($listing) |
| 115 |
{ |
| 116 |
print {$conn->{socket}} "550 File or directory not found.\r\n"; |
| 117 |
return; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
print STDERR "_LIST_command - dir is: $dir\n" if $log; |
| 122 |
|
| 123 |
print {$conn->{socket}} "150 Opening data connection for file listing.\r\n"; |
| 124 |
|
| 125 |
# Open a path back to the client. |
| 126 |
my $sock = __open_data_connection($conn); |
| 127 |
unless ($sock) |
| 128 |
{ |
| 129 |
print {$conn->{socket}} "425 Can't open data connection.\r\n"; |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if (!$ReturnEmptyList) |
| 134 |
{ |
| 135 |
for my $item (@$listing) |
| 136 |
{ |
| 137 |
print $sock "$item\r\n"; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
unless ($sock->close) |
| 142 |
{ |
| 143 |
print {$conn->{socket}} "550 Error closing data connection: $!\r\n"; |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
print {$conn->{socket}} |
| 148 |
"226 Listing complete. Data connection has been closed.\r\n"; |
| 149 |
} |
| 150 |
|
| 151 |
sub _PASS_command |
| 152 |
{ |
| 153 |
my ($conn, $cmd, $pass) = @_; |
| 154 |
|
| 155 |
# TODO: implement authentication? |
| 156 |
|
| 157 |
print STDERR "switching to LOGGEDIN state\n" if $log; |
| 158 |
$conn->{state} = $_connection_states{LOGGEDIN}; |
| 159 |
|
| 160 |
if ($conn->{username} eq "anonymous") |
| 161 |
{ |
| 162 |
print {$conn->{socket}} |
| 163 |
"202 Anonymous user access is always granted.\r\n"; |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
print {$conn->{socket}} |
| 168 |
"230 Authentication not implemented yet, access is always granted.\r\n"; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
sub _PASV_command |
| 173 |
{ |
| 174 |
my ($conn, $cmd, $rest) = @_; |
| 175 |
|
| 176 |
# Open a listening socket - but don't actually accept on it yet. |
| 177 |
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround. |
| 178 |
my $sock = IO::Socket::INET->new( |
| 179 |
LocalHost => '127.0.0.1', |
| 180 |
LocalPort => '0', |
| 181 |
Listen => 1, |
| 182 |
Reuse => 1, |
| 183 |
Proto => 'tcp', |
| 184 |
Type => SOCK_STREAM |
| 185 |
); |
| 186 |
|
| 187 |
unless ($sock) |
| 188 |
{ |
| 189 |
# Return a code 550 here, even though this is not in the RFC. XXX |
| 190 |
print {$conn->{socket}} "550 Can't open a listening socket.\r\n"; |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
$conn->{passive} = 1; |
| 195 |
$conn->{passive_socket} = $sock; |
| 196 |
|
| 197 |
# Get our port number. |
| 198 |
my $sockport = $sock->sockport; |
| 199 |
|
| 200 |
# Split the port number into high and low components. |
| 201 |
my $p1 = int($sockport / 256); |
| 202 |
my $p2 = $sockport % 256; |
| 203 |
|
| 204 |
$conn->{state} = $_connection_states{TWOSOCKS}; |
| 205 |
|
| 206 |
# We only accept connections from localhost. |
| 207 |
print {$conn->{socket}} "227 Entering Passive Mode (127,0,0,1,$p1,$p2)\r\n"; |
| 208 |
} |
| 209 |
|
| 210 |
sub _PORT_command |
| 211 |
{ |
| 212 |
my ($conn, $cmd, $rest) = @_; |
| 213 |
|
| 214 |
# The arguments to PORT are a1,a2,a3,a4,p1,p2 where a1 is the |
| 215 |
# most significant part of the address (eg. 127,0,0,1) and |
| 216 |
# p1 is the most significant part of the port. |
| 217 |
unless ($rest =~ |
| 218 |
/^\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})/ |
| 219 |
) |
| 220 |
{ |
| 221 |
print {$conn->{socket}} "501 Syntax error in PORT command.\r\n"; |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
# Check host address. |
| 226 |
unless ( $1 > 0 |
| 227 |
&& $1 < 224 |
| 228 |
&& $2 >= 0 |
| 229 |
&& $2 < 256 |
| 230 |
&& $3 >= 0 |
| 231 |
&& $3 < 256 |
| 232 |
&& $4 >= 0 |
| 233 |
&& $4 < 256) |
| 234 |
{ |
| 235 |
print {$conn->{socket}} "501 Invalid host address.\r\n"; |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
# Construct host address and port number. |
| 240 |
my $peeraddrstring = "$1.$2.$3.$4"; |
| 241 |
my $peerport = $5 * 256 + $6; |
| 242 |
|
| 243 |
# Check port number. |
| 244 |
unless ($peerport > 0 && $peerport < 65536) |
| 245 |
{ |
| 246 |
print {$conn->{socket}} "501 Invalid port number.\r\n"; |
| 247 |
} |
| 248 |
|
| 249 |
$conn->{peeraddrstring} = $peeraddrstring; |
| 250 |
$conn->{peeraddr} = inet_aton($peeraddrstring); |
| 251 |
$conn->{peerport} = $peerport; |
| 252 |
$conn->{passive} = 0; |
| 253 |
|
| 254 |
$conn->{state} = $_connection_states{TWOSOCKS}; |
| 255 |
|
| 256 |
print {$conn->{socket}} "200 PORT command OK.\r\n"; |
| 257 |
} |
| 258 |
|
| 259 |
sub _PWD_command |
| 260 |
{ |
| 261 |
my ($conn, $cmd, $rest) = @_; |
| 262 |
|
| 263 |
# See RFC 959 Appendix II and draft-ietf-ftpext-mlst-11.txt section 6.2.1. |
| 264 |
my $pathname = $conn->{dir}; |
| 265 |
$pathname =~ s,/+$,, unless $pathname eq "/"; |
| 266 |
$pathname =~ tr,/,/,s; |
| 267 |
|
| 268 |
print {$conn->{socket}} "257 \"$pathname\"\r\n"; |
| 269 |
} |
| 270 |
|
| 271 |
sub _REST_command |
| 272 |
{ |
| 273 |
my ($conn, $cmd, $restart_from) = @_; |
| 274 |
|
| 275 |
unless ($restart_from =~ /^([1-9][0-9]*|0)$/) |
| 276 |
{ |
| 277 |
print {$conn->{socket}} |
| 278 |
"501 REST command needs a numeric argument.\r\n"; |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
$conn->{restart} = $1; |
| 283 |
|
| 284 |
print {$conn->{socket}} "350 Restarting next transfer at $1.\r\n"; |
| 285 |
} |
| 286 |
|
| 287 |
sub _RETR_command |
| 288 |
{ |
| 289 |
my ($conn, $cmd, $path) = @_; |
| 290 |
|
| 291 |
$path = FTPPaths::path_merge($conn->{dir}, $path); |
| 292 |
my $info = $conn->{'paths'}->get_info($path); |
| 293 |
|
| 294 |
unless ($info->{'_type'} eq 'f') |
| 295 |
{ |
| 296 |
print {$conn->{socket}} "550 File not found.\r\n"; |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
print {$conn->{socket}} "150 Opening " |
| 301 |
. ($conn->{type} eq 'A' ? "ASCII mode" : "BINARY mode") |
| 302 |
. " data connection.\r\n"; |
| 303 |
|
| 304 |
# Open a path back to the client. |
| 305 |
my $sock = __open_data_connection($conn); |
| 306 |
|
| 307 |
unless ($sock) |
| 308 |
{ |
| 309 |
print {$conn->{socket}} "425 Can't open data connection.\r\n"; |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
my $content = $info->{'content'}; |
| 314 |
|
| 315 |
# Restart the connection from previous point? |
| 316 |
if ($conn->{restart}) |
| 317 |
{ |
| 318 |
$content = substr($content, $conn->{restart}); |
| 319 |
$conn->{restart} = 0; |
| 320 |
} |
| 321 |
|
| 322 |
# What mode are we sending this file in? |
| 323 |
unless ($conn->{type} eq 'A') # Binary type. |
| 324 |
{ |
| 325 |
my ($r, $buffer, $n, $w, $sent); |
| 326 |
|
| 327 |
# Copy data. |
| 328 |
$sent = 0; |
| 329 |
while ($sent < length($content)) |
| 330 |
{ |
| 331 |
$buffer = substr($content, $sent, 65536); |
| 332 |
$r = length $buffer; |
| 333 |
|
| 334 |
# Restart alarm clock timer. |
| 335 |
alarm $conn->{idle_timeout}; |
| 336 |
|
| 337 |
for ($n = 0 ; $n < $r ;) |
| 338 |
{ |
| 339 |
$w = syswrite($sock, $buffer, $r - $n, $n); |
| 340 |
|
| 341 |
# Cleanup and exit if there was an error. |
| 342 |
unless (defined $w) |
| 343 |
{ |
| 344 |
close $sock; |
| 345 |
print {$conn->{socket}} |
| 346 |
"426 File retrieval error: $!. Data connection has been closed.\r\n"; |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
$n += $w; |
| 351 |
} |
| 352 |
|
| 353 |
# Transfer aborted by client? |
| 354 |
if ($GOT_SIGURG) |
| 355 |
{ |
| 356 |
$GOT_SIGURG = 0; |
| 357 |
close $sock; |
| 358 |
print {$conn->{socket}} |
| 359 |
"426 Transfer aborted. Data connection closed.\r\n"; |
| 360 |
return; |
| 361 |
} |
| 362 |
$sent += $r; |
| 363 |
} |
| 364 |
|
| 365 |
# Cleanup and exit if there was an error. |
| 366 |
unless (defined $r) |
| 367 |
{ |
| 368 |
close $sock; |
| 369 |
print {$conn->{socket}} |
| 370 |
"426 File retrieval error: $!. Data connection has been closed.\r\n"; |
| 371 |
return; |
| 372 |
} |
| 373 |
} |
| 374 |
else |
| 375 |
{ # ASCII type. |
| 376 |
# Copy data. |
| 377 |
my @lines = split /\r\n?|\n/, $content; |
| 378 |
for (@lines) |
| 379 |
{ |
| 380 |
# Remove any native line endings. |
| 381 |
s/[\n\r]+$//; |
| 382 |
|
| 383 |
# Restart alarm clock timer. |
| 384 |
alarm $conn->{idle_timeout}; |
| 385 |
|
| 386 |
# Write the line with telnet-format line endings. |
| 387 |
print $sock "$_\r\n"; |
| 388 |
|
| 389 |
# Transfer aborted by client? |
| 390 |
if ($GOT_SIGURG) |
| 391 |
{ |
| 392 |
$GOT_SIGURG = 0; |
| 393 |
close $sock; |
| 394 |
print {$conn->{socket}} |
| 395 |
"426 Transfer aborted. Data connection closed.\r\n"; |
| 396 |
return; |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
unless (close($sock)) |
| 402 |
{ |
| 403 |
print {$conn->{socket}} "550 File retrieval error: $!.\r\n"; |
| 404 |
return; |
| 405 |
} |
| 406 |
|
| 407 |
print {$conn->{socket}} |
| 408 |
"226 File retrieval complete. Data connection has been closed.\r\n"; |
| 409 |
} |
| 410 |
|
| 411 |
sub _SIZE_command |
| 412 |
{ |
| 413 |
my ($conn, $cmd, $path) = @_; |
| 414 |
|
| 415 |
$path = FTPPaths::path_merge($conn->{dir}, $path); |
| 416 |
my $info = $conn->{'paths'}->get_info($path); |
| 417 |
unless ($info) |
| 418 |
{ |
| 419 |
print {$conn->{socket}} "550 File or directory not found.\r\n"; |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
if ($info->{'_type'} eq 'd') |
| 424 |
{ |
| 425 |
print {$conn->{socket}} |
| 426 |
"550 SIZE command is not supported on directories.\r\n"; |
| 427 |
return; |
| 428 |
} |
| 429 |
|
| 430 |
my $size = length $info->{'content'}; |
| 431 |
|
| 432 |
print {$conn->{socket}} "213 $size\r\n"; |
| 433 |
} |
| 434 |
|
| 435 |
sub _SYST_command |
| 436 |
{ |
| 437 |
my ($conn, $cmd, $dummy) = @_; |
| 438 |
|
| 439 |
if ($conn->{'paths'}->GetBehavior('syst_response')) |
| 440 |
{ |
| 441 |
print {$conn->{socket}} $conn->{'paths'}->GetBehavior('syst_response') |
| 442 |
. "\r\n"; |
| 443 |
} |
| 444 |
else |
| 445 |
{ |
| 446 |
print {$conn->{socket}} "215 UNIX Type: L8\r\n"; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
sub _TYPE_command |
| 451 |
{ |
| 452 |
my ($conn, $cmd, $type) = @_; |
| 453 |
|
| 454 |
# See RFC 959 section 5.3.2. |
| 455 |
if ($type =~ /^([AI])$/i) |
| 456 |
{ |
| 457 |
$conn->{type} = $1; |
| 458 |
} |
| 459 |
elsif ($type =~ /^([AI])\sN$/i) |
| 460 |
{ |
| 461 |
$conn->{type} = $1; |
| 462 |
} |
| 463 |
elsif ($type =~ /^L\s8$/i) |
| 464 |
{ |
| 465 |
$conn->{type} = 'L8'; |
| 466 |
} |
| 467 |
else |
| 468 |
{ |
| 469 |
print {$conn->{socket}} |
| 470 |
"504 This server does not support TYPE $type.\r\n"; |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
print {$conn->{socket}} "200 TYPE changed to $type.\r\n"; |
| 475 |
} |
| 476 |
|
| 477 |
sub _USER_command |
| 478 |
{ |
| 479 |
my ($conn, $cmd, $username) = @_; |
| 480 |
|
| 481 |
print STDERR "username: $username\n" if $log; |
| 482 |
$conn->{username} = $username; |
| 483 |
|
| 484 |
print STDERR "switching to WAIT4PWD state\n" if $log; |
| 485 |
$conn->{state} = $_connection_states{WAIT4PWD}; |
| 486 |
|
| 487 |
if ($conn->{username} eq "anonymous") |
| 488 |
{ |
| 489 |
print {$conn->{socket}} "230 Anonymous user access granted.\r\n"; |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
print {$conn->{socket}} "331 Password required.\r\n"; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
# HELPER ROUTINES |
| 498 |
|
| 499 |
sub __open_data_connection |
| 500 |
{ |
| 501 |
my $conn = shift; |
| 502 |
|
| 503 |
my $sock; |
| 504 |
|
| 505 |
if ($conn->{passive}) |
| 506 |
{ |
| 507 |
# Passive mode - wait for a connection from the client. |
| 508 |
accept($sock, $conn->{passive_socket}) or return undef; |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
# Active mode - connect back to the client. |
| 513 |
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround. |
| 514 |
$sock = IO::Socket::INET->new( |
| 515 |
LocalAddr => '127.0.0.1', |
| 516 |
PeerAddr => $conn->{peeraddrstring}, |
| 517 |
PeerPort => $conn->{peerport}, |
| 518 |
Proto => 'tcp', |
| 519 |
Type => SOCK_STREAM |
| 520 |
) |
| 521 |
or return undef; |
| 522 |
} |
| 523 |
|
| 524 |
return $sock; |
| 525 |
} |
| 526 |
|
| 527 |
########################################################################### |
| 528 |
# FTPSERVER CLASS |
| 529 |
########################################################################### |
| 530 |
|
| 531 |
{ |
| 532 |
my %_attr_data = ( # DEFAULT |
| 533 |
_input => undef, |
| 534 |
_localAddr => 'localhost', |
| 535 |
_localPort => undef, |
| 536 |
_reuseAddr => 1, |
| 537 |
_rootDir => Cwd::getcwd(), |
| 538 |
_server_behavior => {}, |
| 539 |
); |
| 540 |
|
| 541 |
sub _default_for |
| 542 |
{ |
| 543 |
my ($self, $attr) = @_; |
| 544 |
$_attr_data{$attr}; |
| 545 |
} |
| 546 |
|
| 547 |
sub _standard_keys |
| 548 |
{ |
| 549 |
keys %_attr_data; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
sub new |
| 554 |
{ |
| 555 |
my ($caller, %args) = @_; |
| 556 |
my $caller_is_obj = ref($caller); |
| 557 |
my $class = $caller_is_obj || $caller; |
| 558 |
my $self = bless {}, $class; |
| 559 |
foreach my $attrname ($self->_standard_keys()) |
| 560 |
{ |
| 561 |
my ($argname) = ($attrname =~ /^_(.*)/); |
| 562 |
if (exists $args{$argname}) |
| 563 |
{ |
| 564 |
$self->{$attrname} = $args{$argname}; |
| 565 |
} |
| 566 |
elsif ($caller_is_obj) |
| 567 |
{ |
| 568 |
$self->{$attrname} = $caller->{$attrname}; |
| 569 |
} |
| 570 |
else |
| 571 |
{ |
| 572 |
$self->{$attrname} = $self->_default_for($attrname); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
# create server socket |
| 577 |
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround. |
| 578 |
$self->{_server_sock} = |
| 579 |
IO::Socket::INET->new( |
| 580 |
LocalHost => $self->{_localAddr}, |
| 581 |
LocalPort => $self->{_localPort}, |
| 582 |
Listen => 1, |
| 583 |
Reuse => $self->{_reuseAddr}, |
| 584 |
Proto => 'tcp', |
| 585 |
Type => SOCK_STREAM |
| 586 |
) |
| 587 |
or die "bind: $!"; |
| 588 |
|
| 589 |
foreach my $file (keys %{$self->{_input}}) |
| 590 |
{ |
| 591 |
my $ref = \$self->{_input}{$file}{content}; |
| 592 |
$$ref =~ s/\Q{{port}}/$self->sockport/eg; |
| 593 |
} |
| 594 |
|
| 595 |
return $self; |
| 596 |
} |
| 597 |
|
| 598 |
sub run |
| 599 |
{ |
| 600 |
my ($self, $synch_callback) = @_; |
| 601 |
my $initialized = 0; |
| 602 |
|
| 603 |
# turn buffering off on STDERR |
| 604 |
select((select(STDERR), $| = 1)[0]); |
| 605 |
|
| 606 |
# initialize command table |
| 607 |
my $command_table = {}; |
| 608 |
foreach (keys %_commands) |
| 609 |
{ |
| 610 |
my $subname = "_${_}_command"; |
| 611 |
$command_table->{$_} = \&$subname; |
| 612 |
} |
| 613 |
|
| 614 |
my $old_ils = $/; |
| 615 |
$/ = "\r\n"; |
| 616 |
|
| 617 |
if (!$initialized) |
| 618 |
{ |
| 619 |
$synch_callback->(); |
| 620 |
$initialized = 1; |
| 621 |
} |
| 622 |
|
| 623 |
$SIG{CHLD} = sub { wait }; |
| 624 |
my $server_sock = $self->{_server_sock}; |
| 625 |
|
| 626 |
# the accept loop |
| 627 |
while (my $client_addr = accept(my $socket, $server_sock)) |
| 628 |
{ |
| 629 |
# turn buffering off on $socket |
| 630 |
select((select($socket), $| = 1)[0]); |
| 631 |
|
| 632 |
# find out who connected |
| 633 |
my ($client_port, $client_ip) = sockaddr_in($client_addr); |
| 634 |
my $client_ipnum = inet_ntoa($client_ip); |
| 635 |
|
| 636 |
# print who connected |
| 637 |
print STDERR "got a connection from: $client_ipnum\n" if $log; |
| 638 |
|
| 639 |
# fork off a process to handle this connection. |
| 640 |
# my $pid = fork(); |
| 641 |
# unless (defined $pid) { |
| 642 |
# warn "fork: $!"; |
| 643 |
# sleep 5; # Back off in case system is overloaded. |
| 644 |
# next; |
| 645 |
# } |
| 646 |
|
| 647 |
if (1) |
| 648 |
{ # Child process. |
| 649 |
|
| 650 |
# install signals |
| 651 |
$SIG{URG} = sub { |
| 652 |
$GOT_SIGURG = 1; |
| 653 |
}; |
| 654 |
|
| 655 |
$SIG{PIPE} = sub { |
| 656 |
print STDERR "Client closed connection abruptly.\n"; |
| 657 |
exit; |
| 658 |
}; |
| 659 |
|
| 660 |
$SIG{ALRM} = sub { |
| 661 |
print STDERR |
| 662 |
"Connection idle timeout expired. Closing server.\n"; |
| 663 |
exit; |
| 664 |
}; |
| 665 |
|
| 666 |
#$SIG{CHLD} = 'IGNORE'; |
| 667 |
|
| 668 |
print STDERR "in child\n" if $log; |
| 669 |
|
| 670 |
my $conn = { |
| 671 |
'paths' => |
| 672 |
FTPPaths->new($self->{'_input'}, $self->{'_server_behavior'}), |
| 673 |
'socket' => $socket, |
| 674 |
'state' => $_connection_states{NEWCONN}, |
| 675 |
'dir' => '/', |
| 676 |
'restart' => 0, |
| 677 |
'idle_timeout' => 60, # 1 minute timeout |
| 678 |
'rootdir' => $self->{_rootDir}, |
| 679 |
}; |
| 680 |
|
| 681 |
print {$conn->{socket}} |
| 682 |
"220 GNU Wget Testing FTP Server ready.\r\n"; |
| 683 |
|
| 684 |
# command handling loop |
| 685 |
for (; ;) |
| 686 |
{ |
| 687 |
print STDERR "waiting for request\n" if $log; |
| 688 |
|
| 689 |
last unless defined(my $req = <$socket>); |
| 690 |
|
| 691 |
# Remove trailing CRLF. |
| 692 |
$req =~ s/[\n\r]+$//; |
| 693 |
|
| 694 |
print STDERR "received request $req\n" if $log; |
| 695 |
|
| 696 |
# Get the command. |
| 697 |
# See also RFC 2640 section 3.1. |
| 698 |
unless ($req =~ m/^([A-Z]{3,4})\s?(.*)/i) |
| 699 |
{ |
| 700 |
# badly formed command |
| 701 |
exit 0; |
| 702 |
} |
| 703 |
|
| 704 |
# The following strange 'eval' is necessary to work around a |
| 705 |
# very odd bug in Perl 5.6.0. The following assignment to |
| 706 |
# $cmd will fail in some cases unless you use $1 in some sort |
| 707 |
# of an expression beforehand. |
| 708 |
# - RWMJ 2002-07-05. |
| 709 |
eval '$1 eq $1'; |
| 710 |
|
| 711 |
my ($cmd, $rest) = (uc $1, $2); |
| 712 |
|
| 713 |
# Got a command which matches in the table? |
| 714 |
unless (exists $command_table->{$cmd}) |
| 715 |
{ |
| 716 |
print {$conn->{socket}} "500 Unrecognized command.\r\n"; |
| 717 |
next; |
| 718 |
} |
| 719 |
|
| 720 |
# Command requires user to be authenticated? |
| 721 |
unless ($_commands{$cmd} | $conn->{state}) |
| 722 |
{ |
| 723 |
print {$conn->{socket}} "530 Not logged in.\r\n"; |
| 724 |
next; |
| 725 |
} |
| 726 |
|
| 727 |
# Handle the QUIT command specially. |
| 728 |
if ($cmd eq "QUIT") |
| 729 |
{ |
| 730 |
print {$conn->{socket}} |
| 731 |
"221 Goodbye. Service closing connection.\r\n"; |
| 732 |
last; |
| 733 |
} |
| 734 |
|
| 735 |
if (defined($self->{_server_behavior}{fail_on_pasv}) |
| 736 |
&& $cmd eq 'PASV') |
| 737 |
{ |
| 738 |
undef $self->{_server_behavior}{fail_on_pasv}; |
| 739 |
close $socket; |
| 740 |
last; |
| 741 |
} |
| 742 |
|
| 743 |
if (defined($self->{_server_behavior}{pasv_not_supported}) |
| 744 |
&& $cmd eq 'PASV') |
| 745 |
{ |
| 746 |
print {$conn->{socket}} |
| 747 |
"500 PASV not supported.\r\n"; |
| 748 |
next; |
| 749 |
} |
| 750 |
|
| 751 |
# Run the command. |
| 752 |
&{$command_table->{$cmd}}($conn, $cmd, $rest); |
| 753 |
} |
| 754 |
} |
| 755 |
else |
| 756 |
{ # Father |
| 757 |
close $socket; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
$/ = $old_ils; |
| 762 |
} |
| 763 |
|
| 764 |
sub sockport |
| 765 |
{ |
| 766 |
my $self = shift; |
| 767 |
return $self->{_server_sock}->sockport; |
| 768 |
} |
| 769 |
|
| 770 |
package FTPPaths; |
| 771 |
|
| 772 |
use POSIX qw(strftime); |
| 773 |
|
| 774 |
# not a method |
| 775 |
sub final_component |
| 776 |
{ |
| 777 |
my $path = shift; |
| 778 |
|
| 779 |
$path =~ s|.*/||; |
| 780 |
return $path; |
| 781 |
} |
| 782 |
|
| 783 |
# not a method |
| 784 |
sub path_merge |
| 785 |
{ |
| 786 |
my ($path_a, $path_b) = @_; |
| 787 |
|
| 788 |
if (!$path_b) |
| 789 |
{ |
| 790 |
return $path_a; |
| 791 |
} |
| 792 |
|
| 793 |
if ($path_b =~ m.^/.) |
| 794 |
{ |
| 795 |
$path_a = ''; |
| 796 |
$path_b =~ s.^/..; |
| 797 |
} |
| 798 |
$path_a =~ s./$..; |
| 799 |
|
| 800 |
my @components = split m{/}msx, $path_b; |
| 801 |
|
| 802 |
foreach my $c (@components) |
| 803 |
{ |
| 804 |
if ($c =~ /^\.?$/) |
| 805 |
{ |
| 806 |
next; |
| 807 |
} |
| 808 |
elsif ($c eq '..') |
| 809 |
{ |
| 810 |
if (!$path_a) |
| 811 |
{ |
| 812 |
next; |
| 813 |
} |
| 814 |
$path_a =~ s|/[^/]*$||; |
| 815 |
} |
| 816 |
else |
| 817 |
{ |
| 818 |
$path_a .= "/$c"; |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
return $path_a; |
| 823 |
} |
| 824 |
|
| 825 |
sub new |
| 826 |
{ |
| 827 |
my ($this, @args) = @_; |
| 828 |
my $class = ref($this) || $this; |
| 829 |
my $self = {}; |
| 830 |
bless $self, $class; |
| 831 |
$self->initialize(@args); |
| 832 |
return $self; |
| 833 |
} |
| 834 |
|
| 835 |
sub initialize |
| 836 |
{ |
| 837 |
my ($self, $urls, $behavior) = @_; |
| 838 |
my $paths = {_type => 'd'}; |
| 839 |
|
| 840 |
# From a path like '/foo/bar/baz.txt', construct $paths such that |
| 841 |
# $paths->{'foo'}->{'bar'}->{'baz.txt'} is |
| 842 |
# $urls->{'/foo/bar/baz.txt'}. |
| 843 |
for my $path (keys %$urls) |
| 844 |
{ |
| 845 |
my @components = split m{/}msx, $path; |
| 846 |
shift @components; |
| 847 |
my $x = $paths; |
| 848 |
for my $c (@components) |
| 849 |
{ |
| 850 |
if (!exists $x->{$c}) |
| 851 |
{ |
| 852 |
$x->{$c} = {_type => 'd'}; |
| 853 |
} |
| 854 |
$x = $x->{$c}; |
| 855 |
} |
| 856 |
%$x = %{$urls->{$path}}; |
| 857 |
$x->{_type} = 'f'; |
| 858 |
} |
| 859 |
|
| 860 |
$self->{'_paths'} = $paths; |
| 861 |
$self->{'_behavior'} = $behavior; |
| 862 |
return 1; |
| 863 |
} |
| 864 |
|
| 865 |
sub get_info |
| 866 |
{ |
| 867 |
my ($self, $path, $node) = @_; |
| 868 |
$node = $self->{'_paths'} unless $node; |
| 869 |
my @components = split('/', $path); |
| 870 |
shift @components if @components && $components[0] eq ''; |
| 871 |
|
| 872 |
for my $c (@components) |
| 873 |
{ |
| 874 |
if ($node->{'_type'} eq 'd') |
| 875 |
{ |
| 876 |
$node = $node->{$c}; |
| 877 |
} |
| 878 |
else |
| 879 |
{ |
| 880 |
return; |
| 881 |
} |
| 882 |
} |
| 883 |
return $node; |
| 884 |
} |
| 885 |
|
| 886 |
sub dir_exists |
| 887 |
{ |
| 888 |
my ($self, $path) = @_; |
| 889 |
return $self->path_exists($path, 'd'); |
| 890 |
} |
| 891 |
|
| 892 |
sub path_exists |
| 893 |
{ |
| 894 |
# type is optional, in which case we don't check it. |
| 895 |
my ($self, $path, $type) = @_; |
| 896 |
my $paths = $self->{'_paths'}; |
| 897 |
|
| 898 |
die "Invalid path $path (not absolute).\n" unless $path =~ m.^/.; |
| 899 |
my $info = $self->get_info($path); |
| 900 |
return 0 unless defined($info); |
| 901 |
return $info->{'_type'} eq $type if defined($type); |
| 902 |
return 1; |
| 903 |
} |
| 904 |
|
| 905 |
sub _format_for_list |
| 906 |
{ |
| 907 |
my ($self, $name, $info) = @_; |
| 908 |
|
| 909 |
# XXX: mode should be specifyable as part of the node info. |
| 910 |
my $mode_str; |
| 911 |
if ($info->{'_type'} eq 'd') |
| 912 |
{ |
| 913 |
$mode_str = 'dr-xr-xr-x'; |
| 914 |
} |
| 915 |
else |
| 916 |
{ |
| 917 |
$mode_str = '-r--r--r--'; |
| 918 |
} |
| 919 |
|
| 920 |
my $size = 0; |
| 921 |
if ($info->{'_type'} eq 'f') |
| 922 |
{ |
| 923 |
$size = length $info->{'content'}; |
| 924 |
if ($self->{'_behavior'}{'bad_list'}) |
| 925 |
{ |
| 926 |
$size = 0; |
| 927 |
} |
| 928 |
} |
| 929 |
my $date = strftime("%b %d %H:%M", localtime); |
| 930 |
return "$mode_str 1 0 0 $size $date $name"; |
| 931 |
} |
| 932 |
|
| 933 |
sub get_list |
| 934 |
{ |
| 935 |
my ($self, $path, $no_hidden) = @_; |
| 936 |
my $info = $self->get_info($path); |
| 937 |
if (!defined $info) |
| 938 |
{ |
| 939 |
return; |
| 940 |
} |
| 941 |
my $list = []; |
| 942 |
|
| 943 |
if ($info->{'_type'} eq 'd') |
| 944 |
{ |
| 945 |
for my $item (keys %$info) |
| 946 |
{ |
| 947 |
next if $item =~ /^_/; |
| 948 |
|
| 949 |
# 2013-10-17 Andrea Urbani (matfanjol) |
| 950 |
# I skip the hidden files if requested |
| 951 |
if ( ($no_hidden) |
| 952 |
&& (defined($info->{$item}->{'attr'})) |
| 953 |
&& (index($info->{$item}->{'attr'}, "H") >= 0)) |
| 954 |
{ |
| 955 |
# This is an hidden file and I don't want to see it! |
| 956 |
print STDERR "get_list: Skipped hidden file [$item]\n"; |
| 957 |
} |
| 958 |
else |
| 959 |
{ |
| 960 |
push @$list, $self->_format_for_list($item, $info->{$item}); |
| 961 |
} |
| 962 |
} |
| 963 |
} |
| 964 |
else |
| 965 |
{ |
| 966 |
push @$list, $self->_format_for_list(final_component($path), $info); |
| 967 |
} |
| 968 |
|
| 969 |
return $list; |
| 970 |
} |
| 971 |
|
| 972 |
# 2013-10-17 Andrea Urbani (matfanjol) |
| 973 |
# It returns the behavior of the given name. |
| 974 |
# In this file I handle also the following behaviors: |
| 975 |
# list_dont_clean_path : if defined, the command |
| 976 |
# $path =~ s/^-[a-zA-Z0-9]+\s?//; |
| 977 |
# is not runt and the given path |
| 978 |
# remains the original one |
| 979 |
# list_empty_if_list_a : if defined, "LIST -a" returns an |
| 980 |
# empty content |
| 981 |
# list_fails_if_list_a : if defined, "LIST -a" returns an |
| 982 |
# error |
| 983 |
# list_no_hidden_if_list: if defined, "LIST" doesn't return |
| 984 |
# hidden files. |
| 985 |
# To define an hidden file add |
| 986 |
# attr => "H" |
| 987 |
# to the url files |
| 988 |
# syst_response : if defined, its content is printed |
| 989 |
# out as SYST response |
| 990 |
sub GetBehavior |
| 991 |
{ |
| 992 |
my ($self, $name) = @_; |
| 993 |
return $self->{'_behavior'}{$name}; |
| 994 |
} |
| 995 |
|
| 996 |
1; |
| 997 |
|
| 998 |
# vim: et ts=4 sw=4 |