Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/AllowForeignAddress.pm

raw
1 package ProFTPD::Tests::Config::AllowForeignAddress;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
8 use File::Spec;
9 use IO::Handle;
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 fxp_denied => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 fxp_denied_eprt => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 fxp_port_denied_by_class => {
30 order => ++$order,
31 test_class => [qw(forking)],
32 },
33
34 fxp_pasv_denied_by_class_issue1346 => {
35 order => ++$order,
36 test_class => [qw(bug forking)],
37 },
38
39 fxp_allowed => {
40 order => ++$order,
41 test_class => [qw(forking)],
42 },
43
44 fxp_allowed_eprt => {
45 order => ++$order,
46 test_class => [qw(forking)],
47 },
48
49 fxp_port_allowed_by_class => {
50 order => ++$order,
51 test_class => [qw(forking)],
52 },
53
54 fxp_pasv_allowed_by_class_issue1346 => {
55 order => ++$order,
56 test_class => [qw(bug forking)],
57 },
58
59 fxp_allowed_2gb => {
60 order => ++$order,
61 test_class => [qw(forking)],
62 },
63
64 # tls_fxp_allowed
65 };
66
67 sub new {
68 return shift()->SUPER::new(@_);
69 }
70
71 sub list_tests {
72 return testsuite_get_runnable_tests($TESTS);
73 }
74
75 sub fxp_denied {
76 my $self = shift;
77 my $tmpdir = $self->{tmpdir};
78 my $setup = test_setup($tmpdir, 'config');
79
80 my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
81 if (open(my $fh, "> $src_file")) {
82 print $fh "Hello, FXP World!\n";
83
84 unless (close($fh)) {
85 die("Can't write $src_file: $!");
86 }
87
88 } else {
89 die("Can't open $src_file: $!");
90 }
91
92 my $dst_file = File::Spec->rel2abs("$tmpdir/dst.txt");
93
94 my $config = {
95 PidFile => $setup->{pid_file},
96 ScoreboardFile => $setup->{scoreboard_file},
97 SystemLog => $setup->{log_file},
98
99 AuthUserFile => $setup->{auth_user_file},
100 AuthGroupFile => $setup->{auth_group_file},
101 AuthOrder => 'mod_auth_file.c',
102
103 AllowForeignAddress => 'off',
104
105 IfModules => {
106 'mod_delay.c' => {
107 DelayEngine => 'off',
108 },
109 },
110 };
111
112 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
113 $config);
114
115 # Open pipes, for use between the parent and child processes. Specifically,
116 # the child will indicate when it's done with its test by writing a message
117 # to the parent.
118 my ($rfh, $wfh);
119 unless (pipe($rfh, $wfh)) {
120 die("Can't open pipe: $!");
121 }
122
123 my $ex;
124
125 # Fork child
126 $self->handle_sigchld();
127 defined(my $pid = fork()) or die("Can't fork: $!");
128 if ($pid) {
129 eval {
130 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
131 $client1->login($setup->{user}, $setup->{passwd});
132
133 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
134 $client2->login($setup->{user}, $setup->{passwd});
135
136 # Get the PASV address from the first connection, and give it
137 # to the second connection as a PORT command.
138 my ($resp_code, $resp_msg) = $client1->pasv();
139
140 my $expected = 227;
141 $self->assert($expected == $resp_code,
142 test_msg("Expected response code $expected, got $resp_code"));
143
144 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
145 $self->assert(qr/$expected/, $resp_msg,
146 test_msg("Expected response message '$expected', got '$resp_msg'"));
147
148 # Deliberately provide an IP address that does not match our source
149 # IP address, to trigger the AllowForeignAddress restriction.
150 my $port_addr = '1,2,3,4,192,6';
151 eval { $client2->port($port_addr) };
152 unless ($@) {
153 die("PORT $port_addr succeeded unexpectedly");
154 }
155
156 $resp_code = $client2->response_code();
157 $resp_msg = $client2->response_msg();
158
159 $expected = 500;
160 $self->assert($expected == $resp_code,
161 test_msg("Expected response code $expected, got $resp_code"));
162
163 $expected = 'Illegal PORT command';
164 $self->assert($expected eq $resp_msg,
165 test_msg("Expected response message '$expected', got '$resp_msg'"));
166
167 $client1->quit();
168 $client2->quit();
169
170 $self->assert(!-f $dst_file,
171 test_msg("File $dst_file exists unexpectedly"));
172 };
173 if ($@) {
174 $ex = $@;
175 }
176
177 $wfh->print("done\n");
178 $wfh->flush();
179
180 } else {
181 eval { server_wait($setup->{config_file}, $rfh) };
182 if ($@) {
183 warn($@);
184 exit 1;
185 }
186
187 exit 0;
188 }
189
190 # Stop server
191 server_stop($setup->{pid_file});
192 $self->assert_child_ok($pid);
193
194 eval {
195 if (open(my $fh, "< $setup->{log_file}")) {
196 my $ok = 0;
197
198 while (my $line = <$fh>) {
199 chomp($line);
200
201 if ($line =~ /Refused PORT .*? \(address mismatch\)/) {
202 $ok = 1;
203 last;
204 }
205 }
206
207 close($fh);
208
209 $self->assert($ok, test_msg("Did not see expected log messages"));
210
211 } else {
212 die("Can't read $setup->{log_file}: $!");
213 }
214 };
215 if ($@) {
216 $ex = $@;
217 }
218
219 test_cleanup($setup->{log_file}, $ex);
220 }
221
222 sub fxp_denied_eprt {
223 my $self = shift;
224 my $tmpdir = $self->{tmpdir};
225 my $setup = test_setup($tmpdir, 'config');
226
227 my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
228 if (open(my $fh, "> $src_file")) {
229 print $fh "Hello, FXP World!\n";
230
231 unless (close($fh)) {
232 die("Can't write $src_file: $!");
233 }
234
235 } else {
236 die("Can't open $src_file: $!");
237 }
238
239 my $dst_file = File::Spec->rel2abs("$tmpdir/dst.txt");
240
241 my $config = {
242 PidFile => $setup->{pid_file},
243 ScoreboardFile => $setup->{scoreboard_file},
244 SystemLog => $setup->{log_file},
245
246 AuthUserFile => $setup->{auth_user_file},
247 AuthGroupFile => $setup->{auth_group_file},
248 AuthOrder => 'mod_auth_file.c',
249
250 AllowForeignAddress => 'off',
251
252 IfModules => {
253 'mod_delay.c' => {
254 DelayEngine => 'off',
255 },
256 },
257 };
258
259 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
260 $config);
261
262 # Open pipes, for use between the parent and child processes. Specifically,
263 # the child will indicate when it's done with its test by writing a message
264 # to the parent.
265 my ($rfh, $wfh);
266 unless (pipe($rfh, $wfh)) {
267 die("Can't open pipe: $!");
268 }
269
270 my $ex;
271
272 # Fork child
273 $self->handle_sigchld();
274 defined(my $pid = fork()) or die("Can't fork: $!");
275 if ($pid) {
276 eval {
277 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
278 $client1->login($setup->{user}, $setup->{passwd});
279
280 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
281 $client2->login($setup->{user}, $setup->{passwd});
282
283 # Get the PASV address from the first connection, and give it
284 # to the second connection as an EPRT command.
285 my ($resp_code, $resp_msg) = $client1->pasv();
286
287 my $expected = 227;
288 $self->assert($expected == $resp_code,
289 "Expected response code $expected, got $resp_code");
290
291 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
292 $self->assert(qr/$expected/, $resp_msg,
293 "Expected response message '$expected', got '$resp_msg'");
294
295 # Deliberately provide an IP address that does not match our source
296 # IP address, to trigger the AllowForeignAddress restriction.
297 my $eprt_addr = '|1|1.2.3.4|49158|';
298 eval { $client2->eprt($eprt_addr) };
299 unless ($@) {
300 die("EPRT $eprt_addr succeeded unexpectedly");
301 }
302
303 $resp_code = $client2->response_code();
304 $resp_msg = $client2->response_msg();
305
306 $expected = 500;
307 $self->assert($expected == $resp_code,
308 "Expected response code $expected, got $resp_code");
309
310 $expected = 'Illegal EPRT command';
311 $self->assert($expected eq $resp_msg,
312 "Expected response message '$expected', got '$resp_msg'");
313
314 $client1->quit();
315 $client2->quit();
316
317 $self->assert(!-f $dst_file, "File $dst_file exists unexpectedly");
318 };
319 if ($@) {
320 $ex = $@;
321 }
322
323 $wfh->print("done\n");
324 $wfh->flush();
325
326 } else {
327 eval { server_wait($setup->{config_file}, $rfh) };
328 if ($@) {
329 warn($@);
330 exit 1;
331 }
332
333 exit 0;
334 }
335
336 # Stop server
337 server_stop($setup->{pid_file});
338 $self->assert_child_ok($pid);
339
340 eval {
341 if (open(my $fh, "< $setup->{log_file}")) {
342 my $ok = 0;
343
344 while (my $line = <$fh>) {
345 chomp($line);
346
347 if ($line =~ /Refused EPRT .*? \(address mismatch\)/) {
348 $ok = 1;
349 last;
350 }
351 }
352
353 close($fh);
354
355 $self->assert($ok, test_msg("Did not see expected log messages"));
356
357 } else {
358 die("Can't read $setup->{log_file}: $!");
359 }
360 };
361 if ($@) {
362 $ex = $@;
363 }
364
365 test_cleanup($setup->{log_file}, $ex);
366 }
367
368 sub fxp_port_denied_by_class {
369 my $self = shift;
370 my $tmpdir = $self->{tmpdir};
371 my $setup = test_setup($tmpdir, 'config');
372
373 my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
374 if (open(my $fh, "> $src_file")) {
375 print $fh "Hello, FXP World!\n";
376
377 unless (close($fh)) {
378 die("Can't write $src_file: $!");
379 }
380
381 } else {
382 die("Can't open $src_file: $!");
383 }
384
385 my $dst_file = File::Spec->rel2abs("$tmpdir/dst.txt");
386
387 my $class_name = 'allowed_fxp';
388
389 my $config = {
390 PidFile => $setup->{pid_file},
391 ScoreboardFile => $setup->{scoreboard_file},
392 SystemLog => $setup->{log_file},
393
394 AuthUserFile => $setup->{auth_user_file},
395 AuthGroupFile => $setup->{auth_group_file},
396 AuthOrder => 'mod_auth_file.c',
397
398 AllowForeignAddress => $class_name,
399
400 IfModules => {
401 'mod_delay.c' => {
402 DelayEngine => 'off',
403 },
404 },
405 };
406
407 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
408 $config);
409
410 if (open(my $fh, ">> $setup->{config_file}")) {
411 print $fh <<EOC;
412 <Class $class_name>
413 From none
414 </Class>
415 EOC
416 unless (close($fh)) {
417 die("Can't write $setup->{config_file}: $!");
418 }
419
420 } else {
421 die("Can't open $setup->{config_file}: $!");
422 }
423
424 # Open pipes, for use between the parent and child processes. Specifically,
425 # the child will indicate when it's done with its test by writing a message
426 # to the parent.
427 my ($rfh, $wfh);
428 unless (pipe($rfh, $wfh)) {
429 die("Can't open pipe: $!");
430 }
431
432 my $ex;
433
434 # Fork child
435 $self->handle_sigchld();
436 defined(my $pid = fork()) or die("Can't fork: $!");
437 if ($pid) {
438 eval {
439 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
440 $client1->login($setup->{user}, $setup->{passwd});
441
442 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
443 $client2->login($setup->{user}, $setup->{passwd});
444
445 # Get the PASV address from the first connection, and give it
446 # to the second connection as a PORT command.
447 my ($resp_code, $resp_msg) = $client1->pasv();
448
449 my $expected = 227;
450 $self->assert($expected == $resp_code,
451 "Expected response code $expected, got $resp_code");
452
453 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
454 $self->assert(qr/$expected/, $resp_msg,
455 "Expected response message '$expected', got '$resp_msg'");
456
457 # Deliberately provide an IP address that does not match our source
458 # IP address, to trigger the AllowForeignAddress restriction.
459 my $port_addr = '1,2,3,4,192,6';
460 eval { $client2->port($port_addr) };
461 unless ($@) {
462 die("PORT $port_addr succeeded unexpectedly");
463 }
464
465 $resp_code = $client2->response_code();
466 $resp_msg = $client2->response_msg();
467
468 $expected = 500;
469 $self->assert($expected == $resp_code,
470 "Expected response code $expected, got $resp_code");
471
472 $expected = 'Illegal PORT command';
473 $self->assert($expected eq $resp_msg,
474 "Expected response message '$expected', got '$resp_msg'");
475
476 $client1->quit();
477 $client2->quit();
478
479 $self->assert(!-f $dst_file,
480 test_msg("File $dst_file exists unexpectedly"));
481 };
482 if ($@) {
483 $ex = $@;
484 }
485
486 $wfh->print("done\n");
487 $wfh->flush();
488
489 } else {
490 eval { server_wait($setup->{config_file}, $rfh) };
491 if ($@) {
492 warn($@);
493 exit 1;
494 }
495
496 exit 0;
497 }
498
499 # Stop server
500 server_stop($setup->{pid_file});
501 $self->assert_child_ok($pid);
502
503 eval {
504 if (open(my $fh, "< $setup->{log_file}")) {
505 my $ok = 0;
506
507 while (my $line = <$fh>) {
508 chomp($line);
509
510 if ($ENV{TEST_VERBOSE}) {
511 print STDERR "$line\n";
512 }
513
514 if ($line =~ /Refused PORT .*? \(address mismatch\)/) {
515 $ok = 1;
516 last;
517 }
518 }
519
520 close($fh);
521
522 $self->assert($ok, "Did not see expected log messages");
523
524 } else {
525 die("Can't read $setup->{log_file}: $!");
526 }
527 };
528 if ($@) {
529 $ex = $@;
530 }
531
532 test_cleanup($setup->{log_file}, $ex);
533 }
534
535 sub fxp_pasv_denied_by_class_issue1346 {
536 my $self = shift;
537 my $tmpdir = $self->{tmpdir};
538 my $setup = test_setup($tmpdir, 'config');
539
540 my $class_name = 'allowed_fxp';
541
542 my $config = {
543 PidFile => $setup->{pid_file},
544 ScoreboardFile => $setup->{scoreboard_file},
545 SystemLog => $setup->{log_file},
546
547 AuthUserFile => $setup->{auth_user_file},
548 AuthGroupFile => $setup->{auth_group_file},
549 AuthOrder => 'mod_auth_file.c',
550
551 AllowForeignAddress => $class_name,
552
553 IfModules => {
554 'mod_delay.c' => {
555 DelayEngine => 'off',
556 },
557 },
558 };
559
560 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
561 $config);
562
563 if (open(my $fh, ">> $setup->{config_file}")) {
564 print $fh <<EOC;
565 <Class $class_name>
566 From none
567 </Class>
568 EOC
569 unless (close($fh)) {
570 die("Can't write $setup->{config_file}: $!");
571 }
572
573 } else {
574 die("Can't open $setup->{config_file}: $!");
575 }
576
577 # Open pipes, for use between the parent and child processes. Specifically,
578 # the child will indicate when it's done with its test by writing a message
579 # to the parent.
580 my ($rfh, $wfh);
581 unless (pipe($rfh, $wfh)) {
582 die("Can't open pipe: $!");
583 }
584
585 my $ex;
586
587 # Fork child
588 $self->handle_sigchld();
589 defined(my $pid = fork()) or die("Can't fork: $!");
590 if ($pid) {
591 eval {
592 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 3);
593 $client->login($setup->{user}, $setup->{passwd});
594
595 # Attemping a data transfer should fail, due to the AllowForeignAddress
596 # class restriction.
597
598 my $conn = $client->list_raw();
599 unless ($conn) {
600 die("LIST failed: " . $client->response_code() . ' ' .
601 $client->response_msg());
602 }
603
604 my $buf;
605 $conn->read($buf, 8192, 30);
606 eval { $conn->close() };
607
608 my ($resp_code, $resp_msg);
609 $resp_code = $client->response_code();
610 $resp_msg = $client->response_msg();
611
612 $self->assert_transfer_ok($resp_code, $resp_msg);
613 $client->quit();
614 };
615 if ($@) {
616 $ex = $@;
617 }
618
619 $wfh->print("done\n");
620 $wfh->flush();
621
622 } else {
623 eval { server_wait($setup->{config_file}, $rfh) };
624 if ($@) {
625 warn($@);
626 exit 1;
627 }
628
629 exit 0;
630 }
631
632 # Stop server
633 server_stop($setup->{pid_file});
634 $self->assert_child_ok($pid);
635
636 eval {
637 if (open(my $fh, "< $setup->{log_file}")) {
638 my $ok = 1;
639
640 while (my $line = <$fh>) {
641 chomp($line);
642
643 if ($ENV{TEST_VERBOSE}) {
644 print STDERR "$line\n";
645 }
646
647 if ($line =~ /SECURITY VIOLATION/) {
648 $ok = 0;
649 last;
650 }
651
652 if ($line =~ /Passive connection from IP address \S+ matches control connection address; skipping <Class> '\S+'/) {
653 last;
654 }
655 }
656
657 close($fh);
658
659 $self->assert($ok, "Did not see expected log messages");
660
661 } else {
662 die("Can't read $setup->{log_file}: $!");
663 }
664 };
665 if ($@) {
666 $ex = $@;
667 }
668
669 test_cleanup($setup->{log_file}, $ex);
670 }
671
672 sub fxp_allowed {
673 my $self = shift;
674 my $tmpdir = $self->{tmpdir};
675 my $setup = test_setup($tmpdir, 'config');
676
677 my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
678 if (open(my $fh, "> $src_file")) {
679 print $fh "Hello, FXP World!\n";
680
681 unless (close($fh)) {
682 die("Can't write $src_file: $!");
683 }
684
685 } else {
686 die("Can't open $src_file: $!");
687 }
688
689 my $dst_file = File::Spec->rel2abs("$tmpdir/dst.txt");
690
691 my $config = {
692 PidFile => $setup->{pid_file},
693 ScoreboardFile => $setup->{scoreboard_file},
694 SystemLog => $setup->{log_file},
695
696 AuthUserFile => $setup->{auth_user_file},
697 AuthGroupFile => $setup->{auth_group_file},
698 AuthOrder => 'mod_auth_file.c',
699
700 AllowForeignAddress => 'on',
701
702 IfModules => {
703 'mod_delay.c' => {
704 DelayEngine => 'off',
705 },
706 },
707 };
708
709 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
710 $config);
711
712 # Open pipes, for use between the parent and child processes. Specifically,
713 # the child will indicate when it's done with its test by writing a message
714 # to the parent.
715 my ($rfh, $wfh);
716 unless (pipe($rfh, $wfh)) {
717 die("Can't open pipe: $!");
718 }
719
720 my $ex;
721
722 # Fork child
723 $self->handle_sigchld();
724 defined(my $pid = fork()) or die("Can't fork: $!");
725 if ($pid) {
726 eval {
727 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
728 $client1->login($setup->{user}, $setup->{passwd});
729
730 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
731 $client2->login($setup->{user}, $setup->{passwd});
732
733 # Get the PASV address from the first connection, and give it
734 # to the second connection as a PORT command.
735 my ($resp_code, $resp_msg) = $client1->pasv();
736
737 my $expected = 227;
738 $self->assert($expected == $resp_code,
739 test_msg("Expected response code $expected, got $resp_code"));
740
741 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
742 $self->assert(qr/$expected/, $resp_msg,
743 test_msg("Expected response message '$expected', got '$resp_msg'"));
744
745 # This will actually work, since both our connections are
746 # from 127.0.0.1, which means we shouldn't run afoul of the
747 # AllowForeignAddress limit.
748 $resp_msg =~ /'^Entering Passive Mode \((\d+,\d+,\d+,\d+,\d+,\d+\))/;
749 my $port_addr = $1;
750
751 ($resp_code, $resp_msg) = $client2->port($port_addr);
752
753 $expected = 200;
754 $self->assert($expected == $resp_code,
755 test_msg("Expected response code $expected, got $resp_code"));
756
757 $expected = 'PORT command successful';
758 $self->assert($expected eq $resp_msg,
759 test_msg("Expected response message '$expected', got '$resp_msg'"));
760
761 my $tmpfile = 'tmpfile.bin';
762 ($resp_code, $resp_msg) = $client1->stor($src_file, $tmpfile);
763 $self->assert_transfer_ok($resp_code, $resp_msg);
764
765 ($resp_code, $resp_msg) = $client2->retr($tmpfile, $dst_file);
766 $self->assert_transfer_ok($resp_code, $resp_msg);
767
768 $client1->quit();
769 $client2->quit();
770
771 $self->assert(-f $dst_file,
772 test_msg("File $dst_file does not exist as expected"));
773
774 my $dst_size = -s $dst_file;
775 $expected = -s $src_file;
776 $self->assert($expected == $dst_size,
777 test_msg("Expected file size $expected, got $dst_size"));
778 };
779 if ($@) {
780 $ex = $@;
781 }
782
783 $wfh->print("done\n");
784 $wfh->flush();
785
786 } else {
787 eval { server_wait($setup->{config_file}, $rfh) };
788 if ($@) {
789 warn($@);
790 exit 1;
791 }
792
793 exit 0;
794 }
795
796 # Stop server
797 server_stop($setup->{pid_file});
798 $self->assert_child_ok($pid);
799
800 test_cleanup($setup->{log_file}, $ex);
801 }
802
803 sub fxp_allowed_eprt {
804 my $self = shift;
805 my $tmpdir = $self->{tmpdir};
806 my $setup = test_setup($tmpdir, 'config');
807
808 my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
809 if (open(my $fh, "> $src_file")) {
810 print $fh "Hello, FXP World!\n";
811
812 unless (close($fh)) {
813 die("Can't write $src_file: $!");
814 }
815
816 } else {
817 die("Can't open $src_file: $!");
818 }
819
820 my $dst_file = File::Spec->rel2abs("$tmpdir/dst.txt");
821
822 my $config = {
823 PidFile => $setup->{pid_file},
824 ScoreboardFile => $setup->{scoreboard_file},
825 SystemLog => $setup->{log_file},
826
827 AuthUserFile => $setup->{auth_user_file},
828 AuthGroupFile => $setup->{auth_group_file},
829 AuthOrder => 'mod_auth_file.c',
830
831 AllowForeignAddress => 'on',
832
833 IfModules => {
834 'mod_delay.c' => {
835 DelayEngine => 'off',
836 },
837 },
838 };
839
840 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
841 $config);
842
843 # Open pipes, for use between the parent and child processes. Specifically,
844 # the child will indicate when it's done with its test by writing a message
845 # to the parent.
846 my ($rfh, $wfh);
847 unless (pipe($rfh, $wfh)) {
848 die("Can't open pipe: $!");
849 }
850
851 my $ex;
852
853 # Fork child
854 $self->handle_sigchld();
855 defined(my $pid = fork()) or die("Can't fork: $!");
856 if ($pid) {
857 eval {
858 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
859 $client1->login($setup->{user}, $setup->{passwd});
860
861 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
862 $client2->login($setup->{user}, $setup->{passwd});
863
864 # Get the PASV address from the first connection, and give it
865 # to the second connection as an EPRT command.
866 my ($resp_code, $resp_msg) = $client1->pasv();
867
868 my $expected = 227;
869 $self->assert($expected == $resp_code,
870 "Expected response code $expected, got $resp_code");
871
872 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
873 $self->assert(qr/$expected/, $resp_msg,
874 "Expected response message '$expected', got '$resp_msg'");
875
876 # This will actually work, since both our connections are
877 # from 127.0.0.1, which means we shouldn't run afoul of the
878 # AllowForeignAddress limit.
879 $resp_msg =~ /^Entering Passive Mode \(\d+,\d+,\d+,\d+,(\d+),(\d+)\)/;
880
881 my $eprt_port = (($1 << 8) | $2);
882 my $eprt_addr = "|1|127.0.0.1|$eprt_port|";
883
884 ($resp_code, $resp_msg) = $client2->eprt($eprt_addr);
885
886 $expected = 200;
887 $self->assert($expected == $resp_code,
888 "Expected response code $expected, got $resp_code");
889
890 $expected = 'EPRT command successful';
891 $self->assert($expected eq $resp_msg,
892 "Expected response message '$expected', got '$resp_msg'");
893
894 my $tmpfile = 'tmpfile.bin';
895 ($resp_code, $resp_msg) = $client1->stor($src_file, $tmpfile);
896 $self->assert_transfer_ok($resp_code, $resp_msg);
897
898 ($resp_code, $resp_msg) = $client2->retr($tmpfile, $dst_file);
899 $self->assert_transfer_ok($resp_code, $resp_msg);
900
901 $client1->quit();
902 $client2->quit();
903
904 $self->assert(-f $dst_file,
905 "File $dst_file does not exist as expected");
906
907 my $dst_size = -s $dst_file;
908 $expected = -s $src_file;
909 $self->assert($expected == $dst_size,
910 "Expected file size $expected, got $dst_size");
911 };
912 if ($@) {
913 $ex = $@;
914 }
915
916 $wfh->print("done\n");
917 $wfh->flush();
918
919 } else {
920 eval { server_wait($setup->{config_file}, $rfh) };
921 if ($@) {
922 warn($@);
923 exit 1;
924 }
925
926 exit 0;
927 }
928
929 # Stop server
930 server_stop($setup->{pid_file});
931 $self->assert_child_ok($pid);
932
933 test_cleanup($setup->{log_file}, $ex);
934 }
935
936 sub fxp_port_allowed_by_class {
937 my $self = shift;
938 my $tmpdir = $self->{tmpdir};
939 my $setup = test_setup($tmpdir, 'config');
940
941 my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
942 if (open(my $fh, "> $src_file")) {
943 print $fh "Hello, FXP World!\n";
944
945 unless (close($fh)) {
946 die("Can't write $src_file: $!");
947 }
948
949 } else {
950 die("Can't open $src_file: $!");
951 }
952
953 my $dst_file = File::Spec->rel2abs("$tmpdir/dst.txt");
954
955 my $class_name = 'allowed_fxp';
956
957 my $config = {
958 PidFile => $setup->{pid_file},
959 ScoreboardFile => $setup->{scoreboard_file},
960 SystemLog => $setup->{log_file},
961
962 AuthUserFile => $setup->{auth_user_file},
963 AuthGroupFile => $setup->{auth_group_file},
964 AuthOrder => 'mod_auth_file.c',
965
966 AllowForeignAddress => $class_name,
967
968 IfModules => {
969 'mod_delay.c' => {
970 DelayEngine => 'off',
971 },
972 },
973 };
974
975 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
976 $config);
977
978 if (open(my $fh, ">> $setup->{config_file}")) {
979 print $fh <<EOC;
980 <Class $class_name>
981 From 127.0.0.0/8
982 </Class>
983 EOC
984 unless (close($fh)) {
985 die("Can't write $setup->{config_file}: $!");
986 }
987
988 } else {
989 die("Can't open $setup->{config_file}: $!");
990 }
991
992 # Open pipes, for use between the parent and child processes. Specifically,
993 # the child will indicate when it's done with its test by writing a message
994 # to the parent.
995 my ($rfh, $wfh);
996 unless (pipe($rfh, $wfh)) {
997 die("Can't open pipe: $!");
998 }
999
1000 my $ex;
1001
1002 # Fork child
1003 $self->handle_sigchld();
1004 defined(my $pid = fork()) or die("Can't fork: $!");
1005 if ($pid) {
1006 eval {
1007 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
1008 $client1->login($setup->{user}, $setup->{passwd});
1009
1010 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1011 $client2->login($setup->{user}, $setup->{passwd});
1012
1013 # Get the PASV address from the first connection, and give it
1014 # to the second connection as a PORT command.
1015 my ($resp_code, $resp_msg) = $client1->pasv();
1016
1017 my $expected = 227;
1018 $self->assert($expected == $resp_code,
1019 "Expected response code $expected, got $resp_code");
1020
1021 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
1022 $self->assert(qr/$expected/, $resp_msg,
1023 "Expected response message '$expected', got '$resp_msg'");
1024
1025 # This will actually work, since both our connections are
1026 # from 127.0.0.1, which means we shouldn't run afoul of the
1027 # AllowForeignAddress limit.
1028 $resp_msg =~ /'^Entering Passive Mode \((\d+,\d+,\d+,\d+,\d+,\d+\))/;
1029 my $port_addr = $1;
1030
1031 ($resp_code, $resp_msg) = $client2->port($port_addr);
1032
1033 $expected = 200;
1034 $self->assert($expected == $resp_code,
1035 "Expected response code $expected, got $resp_code");
1036
1037 $expected = 'PORT command successful';
1038 $self->assert($expected eq $resp_msg,
1039 "Expected response message '$expected', got '$resp_msg'");
1040
1041 my $tmpfile = 'tmpfile.bin';
1042 ($resp_code, $resp_msg) = $client1->stor($src_file, $tmpfile);
1043 $self->assert_transfer_ok($resp_code, $resp_msg);
1044
1045 ($resp_code, $resp_msg) = $client2->retr($tmpfile, $dst_file);
1046 $self->assert_transfer_ok($resp_code, $resp_msg);
1047
1048 $client1->quit();
1049 $client2->quit();
1050
1051 $self->assert(-f $dst_file, "File $dst_file does not exist as expected");
1052
1053 my $dst_size = -s $dst_file;
1054 $expected = -s $src_file;
1055 $self->assert($expected == $dst_size,
1056 "Expected file size $expected, got $dst_size");
1057 };
1058 if ($@) {
1059 $ex = $@;
1060 }
1061
1062 $wfh->print("done\n");
1063 $wfh->flush();
1064
1065 } else {
1066 eval { server_wait($setup->{config_file}, $rfh) };
1067 if ($@) {
1068 warn($@);
1069 exit 1;
1070 }
1071
1072 exit 0;
1073 }
1074
1075 # Stop server
1076 server_stop($setup->{pid_file});
1077 $self->assert_child_ok($pid);
1078
1079 test_cleanup($setup->{log_file}, $ex);
1080 }
1081
1082 sub fxp_pasv_allowed_by_class_issue1346 {
1083 my $self = shift;
1084 my $tmpdir = $self->{tmpdir};
1085 my $setup = test_setup($tmpdir, 'config');
1086
1087 my $class_name = 'allowed_fxp';
1088
1089 my $config = {
1090 PidFile => $setup->{pid_file},
1091 ScoreboardFile => $setup->{scoreboard_file},
1092 SystemLog => $setup->{log_file},
1093 TraceLog => $setup->{log_file},
1094 Trace => 'class:20 inet:20',
1095
1096 AuthUserFile => $setup->{auth_user_file},
1097 AuthGroupFile => $setup->{auth_group_file},
1098 AuthOrder => 'mod_auth_file.c',
1099
1100 AllowForeignAddress => $class_name,
1101
1102 IfModules => {
1103 'mod_delay.c' => {
1104 DelayEngine => 'off',
1105 },
1106 },
1107 };
1108
1109 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1110 $config);
1111
1112 if (open(my $fh, ">> $setup->{config_file}")) {
1113 print $fh <<EOC;
1114 <Class $class_name>
1115 From 127.0.0.0/8
1116 </Class>
1117 EOC
1118 unless (close($fh)) {
1119 die("Can't write $setup->{config_file}: $!");
1120 }
1121
1122 } else {
1123 die("Can't open $setup->{config_file}: $!");
1124 }
1125
1126 # Open pipes, for use between the parent and child processes. Specifically,
1127 # the child will indicate when it's done with its test by writing a message
1128 # to the parent.
1129 my ($rfh, $wfh);
1130 unless (pipe($rfh, $wfh)) {
1131 die("Can't open pipe: $!");
1132 }
1133
1134 my $ex;
1135
1136 # Fork child
1137 $self->handle_sigchld();
1138 defined(my $pid = fork()) or die("Can't fork: $!");
1139 if ($pid) {
1140 eval {
1141 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 3);
1142 $client->login($setup->{user}, $setup->{passwd});
1143
1144 # Attemping a data transfer should succeed, due to the AllowForeignAddress
1145 # class restriction.
1146 my $conn = $client->list_raw();
1147 unless ($conn) {
1148 die("Failed to LIST: " . $client->response_code() . " " .
1149 $client->response_msg());
1150 }
1151
1152 my $buf;
1153 $conn->read($buf, 8192, 30);
1154 eval { $conn->close() };
1155
1156 my ($resp_code, $resp_msg);
1157 $resp_code = $client->response_code();
1158 $resp_msg = $client->response_msg();
1159
1160 $self->assert_transfer_ok($resp_code, $resp_msg);
1161 $client->quit();
1162 };
1163 if ($@) {
1164 $ex = $@;
1165 }
1166
1167 $wfh->print("done\n");
1168 $wfh->flush();
1169
1170 } else {
1171 eval { server_wait($setup->{config_file}, $rfh) };
1172 if ($@) {
1173 warn($@);
1174 exit 1;
1175 }
1176
1177 exit 0;
1178 }
1179
1180 # Stop server
1181 server_stop($setup->{pid_file});
1182 $self->assert_child_ok($pid);
1183
1184 test_cleanup($setup->{log_file}, $ex);
1185 }
1186
1187 sub fxp_allowed_2gb {
1188 my $self = shift;
1189 my $tmpdir = $self->{tmpdir};
1190 my $setup = test_setup($tmpdir, 'config');
1191
1192 # See:
1193 # http://serverfault.com/questions/660372/proftpd-1-34-and-files-size-2-gb
1194
1195 my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
1196 if (open(my $fh, "> $src_file")) {
1197 # Seek to the 2GB limit, then fill the rest with 'A'
1198 unless (seek($fh, (2 ** 31), 0)) {
1199 die("Can't seek to 2GB length: $!");
1200 }
1201
1202 print $fh "A" x 24;
1203
1204 unless (close($fh)) {
1205 die("Can't write $src_file: $!");
1206 }
1207
1208 } else {
1209 die("Can't open $src_file: $!");
1210 }
1211
1212 my $dst_file = File::Spec->rel2abs("$tmpdir/dst.txt");
1213
1214 my $config = {
1215 PidFile => $setup->{pid_file},
1216 ScoreboardFile => $setup->{scoreboard_file},
1217 SystemLog => $setup->{log_file},
1218
1219 AuthUserFile => $setup->{auth_user_file},
1220 AuthGroupFile => $setup->{auth_group_file},
1221 AuthOrder => 'mod_auth_file.c',
1222
1223 AllowForeignAddress => 'on',
1224
1225 IfModules => {
1226 'mod_delay.c' => {
1227 DelayEngine => 'off',
1228 },
1229 },
1230 };
1231
1232 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1233 $config);
1234
1235 # Open pipes, for use between the parent and child processes. Specifically,
1236 # the child will indicate when it's done with its test by writing a message
1237 # to the parent.
1238 my ($rfh, $wfh);
1239 unless (pipe($rfh, $wfh)) {
1240 die("Can't open pipe: $!");
1241 }
1242
1243 my $ex;
1244
1245 # Fork child
1246 $self->handle_sigchld();
1247 defined(my $pid = fork()) or die("Can't fork: $!");
1248 if ($pid) {
1249 eval {
1250 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
1251 $client1->login($setup->{user}, $setup->{passwd});
1252
1253 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1254 $client2->login($setup->{user}, $setup->{passwd});
1255
1256 # Get the PASV address from the first connection, and give it
1257 # to the second connection as a PORT command.
1258 my ($resp_code, $resp_msg) = $client1->pasv();
1259
1260 my $expected = 227;
1261 $self->assert($expected == $resp_code,
1262 test_msg("Expected response code $expected, got $resp_code"));
1263
1264 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
1265 $self->assert(qr/$expected/, $resp_msg,
1266 test_msg("Expected response message '$expected', got '$resp_msg'"));
1267
1268 # This will actually work, since both our connections are
1269 # from 127.0.0.1, which means we shouldn't run afoul of the
1270 # AllowForeignAddress limit.
1271 $resp_msg =~ /'^Entering Passive Mode \((\d+,\d+,\d+,\d+,\d+,\d+\))/;
1272 my $port_addr = $1;
1273
1274 ($resp_code, $resp_msg) = $client2->port($port_addr);
1275
1276 $expected = 200;
1277 $self->assert($expected == $resp_code,
1278 test_msg("Expected response code $expected, got $resp_code"));
1279
1280 $expected = 'PORT command successful';
1281 $self->assert($expected eq $resp_msg,
1282 test_msg("Expected response message '$expected', got '$resp_msg'"));
1283
1284 my $tmpfile = 'tmpfile.bin';
1285 ($resp_code, $resp_msg) = $client1->stor($src_file, $tmpfile);
1286 $self->assert_transfer_ok($resp_code, $resp_msg);
1287
1288 ($resp_code, $resp_msg) = $client2->retr($tmpfile, $dst_file);
1289 $self->assert_transfer_ok($resp_code, $resp_msg);
1290
1291 $client1->quit();
1292 $client2->quit();
1293
1294 $self->assert(-f $dst_file,
1295 test_msg("File $dst_file does not exist as expected"));
1296
1297 my $dst_size = -s $dst_file;
1298 $expected = -s $src_file;
1299 $self->assert($expected == $dst_size,
1300 test_msg("Expected file size $expected, got $dst_size"));
1301 };
1302 if ($@) {
1303 $ex = $@;
1304 }
1305
1306 $wfh->print("done\n");
1307 $wfh->flush();
1308
1309 } else {
1310 eval { server_wait($setup->{config_file}, $rfh, 600) };
1311 if ($@) {
1312 warn($@);
1313 exit 1;
1314 }
1315
1316 exit 0;
1317 }
1318
1319 # Stop server
1320 server_stop($setup->{pid_file});
1321 $self->assert_child_ok($pid);
1322
1323 test_cleanup($setup->{log_file}, $ex);
1324 }
1325
1326 1;