Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_ban.pm

raw
1 package ProFTPD::Tests::Modules::mod_ban;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Carp;
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 ban_on_event_max_login_attempts => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 ban_on_event_max_login_attempts_from_user => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 ban_message => {
30 order => ++$order,
31 test_class => [qw(forking)],
32 },
33
34 ban_ifclass_engine_on => {
35 order => ++$order,
36 test_class => [qw(forking mod_ifsession)],
37 },
38
39 ban_ifclass_engine_off => {
40 order => ++$order,
41 test_class => [qw(forking mod_ifsession)],
42 },
43
44 ban_max_logins_exceeded_bug3281 => {
45 order => ++$order,
46 test_class => [qw(bug forking)],
47 },
48
49 ban_timeout_login_exceeded_bug3281 => {
50 order => ++$order,
51 test_class => [qw(bug forking)],
52 },
53
54 ban_engine_vhost_bug3355 => {
55 order => ++$order,
56 test_class => [qw(bug forking)],
57 },
58
59 ban_unhandled_cmd => {
60 order => ++$order,
61 test_class => [qw(forking)],
62 },
63
64 ban_on_event_client_connect_rate => {
65 order => ++$order,
66 test_class => [qw(forking)],
67 },
68
69 ban_sighup_bug3751 => {
70 order => ++$order,
71 test_class => [qw(bug forking os_linux)],
72 },
73
74 ban_on_event_tlshandshake => {
75 order => ++$order,
76 test_class => [qw(forking mod_tls)],
77 },
78
79 ban_on_event_rootlogin => {
80 order => ++$order,
81 test_class => [qw(forking)],
82 },
83
84 ban_on_event_rootlogin_userdefined => {
85 order => ++$order,
86 test_class => [qw(forking)],
87 },
88
89 ban_opt_any_server_issue1010 => {
90 order => ++$order,
91 test_class => [qw(forking)],
92 },
93
94 };
95
96 sub new {
97 return shift()->SUPER::new(@_);
98 }
99
100 sub list_tests {
101 # return testsuite_get_runnable_tests($TESTS);
102 return qw(
103 ban_on_event_max_login_attempts_from_user
104 );
105 }
106
107 sub get_server_pid {
108 my $pid_file = shift;
109
110 my $pid;
111 if (open(my $fh, "< $pid_file")) {
112 $pid = <$fh>;
113 chomp($pid);
114 close($fh);
115
116 } else {
117 croak("Can't read $pid_file: $!");
118 }
119
120 return $pid;
121 }
122
123 sub server_open_fds {
124 my $pid_file = shift;
125
126 my $pid = get_server_pid($pid_file);
127
128 my $proc_dir = "/proc/$pid/fd";
129 if (opendir(my $dirh, $proc_dir)) {
130 my $count = 0;
131
132 # Only count entries whose names are numbers
133 while (my $dent = readdir($dirh)) {
134 if ($dent =~ /^\d+$/) {
135 $count++;
136 }
137 }
138
139 closedir($dirh);
140 return $count;
141
142 } else {
143 croak("Can't open directory '$proc_dir': $!");
144 }
145 }
146
147 sub ban_on_event_max_login_attempts {
148 my $self = shift;
149 my $tmpdir = $self->{tmpdir};
150 my $setup = test_setup($tmpdir, 'ban');
151
152 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
153
154 my $config = {
155 PidFile => $setup->{pid_file},
156 ScoreboardFile => $setup->{scoreboard_file},
157 SystemLog => $setup->{log_file},
158 TraceLog => $setup->{log_file},
159 Trace => 'event:10',
160
161 AuthUserFile => $setup->{auth_user_file},
162 AuthGroupFile => $setup->{auth_group_file},
163 AuthOrder => 'mod_auth_file.c',
164
165 MaxLoginAttempts => 2,
166
167 IfModules => {
168 'mod_ban.c' => {
169 BanEngine => 'on',
170 BanLog => $setup->{log_file},
171
172 # This says to ban a client which exceeds the MaxLoginAttempts
173 # limit once within the last 1 minute will be banned for 5 secs
174 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05',
175
176 BanTable => $ban_tab,
177 },
178
179 'mod_delay.c' => {
180 DelayEngine => 'off',
181 },
182 },
183 };
184
185 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
186 $config);
187
188 # Open pipes, for use between the parent and child processes. Specifically,
189 # the child will indicate when it's done with its test by writing a message
190 # to the parent.
191 my ($rfh, $wfh);
192 unless (pipe($rfh, $wfh)) {
193 die("Can't open pipe: $!");
194 }
195
196 my $ex;
197
198 # Fork child
199 $self->handle_sigchld();
200 defined(my $pid = fork()) or die("Can't fork: $!");
201 if ($pid) {
202 eval {
203 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
204
205 eval { $client->login($setup->{user}, 'foo') };
206 unless ($@) {
207 die("Login succeeded unexpectedly");
208 }
209
210 my $resp_code = $client->response_code();
211 my $resp_msg = $client->response_msg();
212
213 my $expected;
214
215 $expected = 530;
216 $self->assert($expected == $resp_code,
217 test_msg("Expected response code $expected, got $resp_code"));
218
219 $expected = "Login incorrect.";
220 $self->assert($expected eq $resp_msg,
221 test_msg("Expected response message '$expected', got '$resp_msg'"));
222
223 eval { $client->login($setup->{user}, 'foo') };
224 unless ($@) {
225 die("Login succeeded unexpectedly");
226 }
227
228 $resp_code = $client->response_code();
229 $resp_msg = $client->response_msg();
230
231 $expected = 530;
232 $self->assert($expected == $resp_code,
233 test_msg("Expected response code $expected, got $resp_code"));
234
235 $expected = "Login incorrect.";
236 $self->assert($expected eq $resp_msg,
237 test_msg("Expected response message '$expected', got '$resp_msg'"));
238
239 # Now try again with the correct info; we should be banned. Note
240 # that we have to create a separate connection for this.
241
242 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
243 undef, 0) };
244 unless ($@) {
245 die("Connect succeeded unexpectedly");
246 }
247
248 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
249
250 $expected = "";
251 $self->assert($expected eq $conn_ex,
252 test_msg("Expected '$expected', got '$conn_ex'"));
253 };
254 if ($@) {
255 $ex = $@;
256 }
257
258 $wfh->print("done\n");
259 $wfh->flush();
260
261 } else {
262 eval { server_wait($setup->{config_file}, $rfh) };
263 if ($@) {
264 warn($@);
265 exit 1;
266 }
267
268 exit 0;
269 }
270
271 # Stop server
272 server_stop($setup->{pid_file});
273 $self->assert_child_ok($pid);
274
275 test_cleanup($setup->{log_file}, $ex);
276 }
277
278 sub ban_on_event_max_login_attempts_from_user {
279 my $self = shift;
280 my $tmpdir = $self->{tmpdir};
281 my $setup = test_setup($tmpdir, 'ban');
282
283 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
284
285 my $config = {
286 PidFile => $setup->{pid_file},
287 ScoreboardFile => $setup->{scoreboard_file},
288 SystemLog => $setup->{log_file},
289 TraceLog => $setup->{log_file},
290 Trace => 'event:10',
291
292 AuthUserFile => $setup->{auth_user_file},
293 AuthGroupFile => $setup->{auth_group_file},
294 AuthOrder => 'mod_auth_file.c',
295
296 MaxLoginAttempts => 2,
297
298 IfModules => {
299 'mod_ban.c' => {
300 BanEngine => 'on',
301 BanLog => $setup->{log_file},
302
303 # This says to ban a client which exceeds the MaxLoginAttempts
304 # limit once within the last 1 minute will be banned for 5 secs
305 BanOnEvent => 'MaxLoginAttemptsFromUser 1/00:01:00 00:00:05',
306
307 BanTable => $ban_tab,
308 },
309
310 'mod_delay.c' => {
311 DelayEngine => 'off',
312 },
313 },
314 };
315
316 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
317 $config);
318
319 # Open pipes, for use between the parent and child processes. Specifically,
320 # the child will indicate when it's done with its test by writing a message
321 # to the parent.
322 my ($rfh, $wfh);
323 unless (pipe($rfh, $wfh)) {
324 die("Can't open pipe: $!");
325 }
326
327 my $ex;
328
329 # Fork child
330 $self->handle_sigchld();
331 defined(my $pid = fork()) or die("Can't fork: $!");
332 if ($pid) {
333 eval {
334 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
335
336 eval { $client->login($setup->{user}, 'foo') };
337 unless ($@) {
338 die("Login succeeded unexpectedly");
339 }
340
341 my $resp_code = $client->response_code();
342 my $resp_msg = $client->response_msg();
343
344 my $expected;
345
346 $expected = 530;
347 $self->assert($expected == $resp_code,
348 test_msg("Expected response code $expected, got $resp_code"));
349
350 $expected = "Login incorrect.";
351 $self->assert($expected eq $resp_msg,
352 test_msg("Expected response message '$expected', got '$resp_msg'"));
353
354 eval { $client->login($setup->{user}, 'foo') };
355 unless ($@) {
356 die("Login succeeded unexpectedly");
357 }
358
359 $resp_code = $client->response_code();
360 $resp_msg = $client->response_msg();
361
362 $expected = 530;
363 $self->assert($expected == $resp_code,
364 test_msg("Expected response code $expected, got $resp_code"));
365
366 $expected = "Login incorrect.";
367 $self->assert($expected eq $resp_msg,
368 test_msg("Expected response message '$expected', got '$resp_msg'"));
369
370 # Now try again with the correct info; we should be banned. Note
371 # that we have to create a separate connection for this.
372
373 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
374 eval { $client->login($setup->{user}, $setup->{passwd}) };
375 unless ($@) {
376 die("Login succeeded unexpectedly");
377 }
378
379 $resp_code = $client->response_code();
380 $resp_msg = $client->response_msg();
381
382 $expected = 530;
383 $self->assert($expected == $resp_code,
384 test_msg("Expected response code $expected, got $resp_code"));
385
386 $expected = "Login incorrect.";
387 $self->assert($expected eq $resp_msg,
388 test_msg("Expected response message '$expected', got '$resp_msg'"));
389 $client->quit();
390 };
391 if ($@) {
392 $ex = $@;
393 }
394
395 $wfh->print("done\n");
396 $wfh->flush();
397
398 } else {
399 eval { server_wait($setup->{config_file}, $rfh) };
400 if ($@) {
401 warn($@);
402 exit 1;
403 }
404
405 exit 0;
406 }
407
408 # Stop server
409 server_stop($setup->{pid_file});
410 $self->assert_child_ok($pid);
411
412 eval {
413 if (open(my $fh, "< $setup->{log_file}")) {
414 my $ok = 0;
415
416 while (my $line = <$fh>) {
417 chomp($line);
418
419 if ($ENV{TEST_VERBOSE}) {
420 print STDERR "# $line\n";
421 }
422
423 if ($line =~ /Login denied: user\@host .*? banned/) {
424 $ok = 1;
425 last;
426 }
427 }
428
429 close($fh);
430
431 $self->assert($ok, test_msg("Did not see expected BanLog messages"));
432
433 } else {
434 die("Can't read $setup->{log_file}: $!");
435 }
436 };
437 if ($@) {
438 $ex = $@;
439 }
440
441 test_cleanup($setup->{log_file}, $ex);
442 }
443
444 sub ban_message {
445 my $self = shift;
446 my $tmpdir = $self->{tmpdir};
447
448 my $config_file = "$tmpdir/ban.conf";
449 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
450 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
451
452 my $log_file = test_get_logfile();
453
454 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
455
456 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
457 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
458
459 my $user = 'proftpd';
460 my $passwd = 'test';
461 my $group = 'ftpd';
462 my $home_dir = File::Spec->rel2abs($tmpdir);
463 my $uid = 500;
464 my $gid = 500;
465
466 # Make sure that, if we're running as root, that the home directory has
467 # permissions/privs set for the account we create
468 if ($< == 0) {
469 unless (chmod(0755, $home_dir)) {
470 die("Can't set perms on $home_dir to 0755: $!");
471 }
472
473 unless (chown($uid, $gid, $home_dir)) {
474 die("Can't set owner of $home_dir to $uid/$gid: $!");
475 }
476 }
477
478 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
479 '/bin/bash');
480 auth_group_write($auth_group_file, $group, $gid, $user);
481
482 my $config = {
483 PidFile => $pid_file,
484 ScoreboardFile => $scoreboard_file,
485 SystemLog => $log_file,
486
487 AuthUserFile => $auth_user_file,
488 AuthGroupFile => $auth_group_file,
489 AuthOrder => 'mod_auth_file.c',
490
491 MaxLoginAttempts => 2,
492
493 IfModules => {
494 'mod_ban.c' => {
495 BanEngine => 'on',
496 BanLog => $log_file,
497
498 # This says to ban a client which exceeds the MaxLoginAttempts
499 # limit once within the last 1 minute will be banned for 5 secs
500 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05',
501
502 BanTable => $ban_tab,
503
504 BanMessage => '"Go away %a (%c), or I shall taunt you (%u) a second time!"',
505 },
506
507 'mod_delay.c' => {
508 DelayEngine => 'off',
509 },
510 },
511 };
512
513 my ($port, $config_user, $config_group) = config_write($config_file, $config);
514
515 # Open pipes, for use between the parent and child processes. Specifically,
516 # the child will indicate when it's done with its test by writing a message
517 # to the parent.
518 my ($rfh, $wfh);
519 unless (pipe($rfh, $wfh)) {
520 die("Can't open pipe: $!");
521 }
522
523 my $ex;
524
525 # Fork child
526 $self->handle_sigchld();
527 defined(my $pid = fork()) or die("Can't fork: $!");
528 if ($pid) {
529 eval {
530 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
531
532 eval { $client->login($user, 'foo') };
533 unless ($@) {
534 die("Login succeeded unexpectedly");
535 }
536
537 my $resp_code = $client->response_code();
538 my $resp_msg = $client->response_msg();
539
540 my $expected;
541
542 $expected = 530;
543 $self->assert($expected == $resp_code,
544 test_msg("Expected $expected, got $resp_code"));
545
546 $expected = "Login incorrect.";
547 $self->assert($expected eq $resp_msg,
548 test_msg("Expected '$expected', got '$resp_msg'"));
549
550 eval { $client->login($user, 'foo') };
551 unless ($@) {
552 die("Login succeeded unexpectedly");
553 }
554
555 $resp_code = $client->response_code();
556 $resp_msg = $client->response_msg();
557
558 $expected = 530;
559 $self->assert($expected == $resp_code,
560 test_msg("Expected $expected, got $resp_code"));
561
562 $expected = "Login incorrect.";
563 $self->assert($expected eq $resp_msg,
564 test_msg("Expected '$expected', got '$resp_msg'"));
565
566 # Now try again with the correct info; we should be banned.
567 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
568 undef, 0) };
569 unless ($@) {
570 die("Connect succeeded unexpectedly");
571 }
572
573 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
574
575 $expected = "Go away 127.0.0.1 ((none)), or I shall taunt you ((none)) a second time!";
576 $self->assert($expected eq $conn_ex,
577 test_msg("Expected '$expected', got '$conn_ex'"));
578 };
579
580 if ($@) {
581 $ex = $@;
582 }
583
584 $wfh->print("done\n");
585 $wfh->flush();
586
587 } else {
588 eval { server_wait($config_file, $rfh) };
589 if ($@) {
590 warn($@);
591 exit 1;
592 }
593
594 exit 0;
595 }
596
597 # Stop server
598 server_stop($pid_file);
599
600 $self->assert_child_ok($pid);
601
602 if ($ex) {
603 test_append_logfile($log_file, $ex);
604 unlink($log_file);
605
606 die($ex);
607 }
608
609 unlink($log_file);
610 }
611
612 sub ban_ifclass_engine_on {
613 my $self = shift;
614 my $tmpdir = $self->{tmpdir};
615
616 my $config_file = "$tmpdir/ban.conf";
617 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
618 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
619
620 my $log_file = test_get_logfile();
621
622 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
623
624 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
625 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
626
627 my $user = 'proftpd';
628 my $passwd = 'test';
629 my $group = 'ftpd';
630 my $home_dir = File::Spec->rel2abs($tmpdir);
631 my $uid = 500;
632 my $gid = 500;
633
634 # Make sure that, if we're running as root, that the home directory has
635 # permissions/privs set for the account we create
636 if ($< == 0) {
637 unless (chmod(0755, $home_dir)) {
638 die("Can't set perms on $home_dir to 0755: $!");
639 }
640
641 unless (chown($uid, $gid, $home_dir)) {
642 die("Can't set owner of $home_dir to $uid/$gid: $!");
643 }
644 }
645
646 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
647 '/bin/bash');
648 auth_group_write($auth_group_file, $group, $gid, $user);
649
650 my $config = {
651 PidFile => $pid_file,
652 ScoreboardFile => $scoreboard_file,
653 SystemLog => $log_file,
654
655 AuthUserFile => $auth_user_file,
656 AuthGroupFile => $auth_group_file,
657 AuthOrder => 'mod_auth_file.c',
658
659 MaxLoginAttempts => 2,
660
661 Class => {
662 test => {
663 From => '127.0.0.1',
664 },
665 },
666
667 IfModules => {
668 'mod_ban.c' => {
669 BanLog => $log_file,
670
671 # This says to ban a client which exceeds the MaxLoginAttempts
672 # limit once within the last 1 minute will be banned for 5 secs
673 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05',
674
675 BanTable => $ban_tab,
676 },
677
678 'mod_delay.c' => {
679 DelayEngine => 'off',
680 },
681 },
682 };
683
684 my ($port, $config_user, $config_group) = config_write($config_file, $config);
685
686 if (open(my $fh, ">> $config_file")) {
687 print $fh <<EOI;
688 <IfClass test>
689 BanEngine on
690 </IfClass>
691 EOI
692 unless (close($fh)) {
693 die("Can't write $config_file: $!");
694 }
695
696 } else {
697 die("Can't open $config_file: $!");
698 }
699
700 # Open pipes, for use between the parent and child processes. Specifically,
701 # the child will indicate when it's done with its test by writing a message
702 # to the parent.
703 my ($rfh, $wfh);
704 unless (pipe($rfh, $wfh)) {
705 die("Can't open pipe: $!");
706 }
707
708 my $ex;
709
710 # Fork child
711 $self->handle_sigchld();
712 defined(my $pid = fork()) or die("Can't fork: $!");
713 if ($pid) {
714 eval {
715 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
716
717 my ($resp_code, $resp_msg);
718
719 eval { $client->login($user, 'foo') };
720 unless ($@) {
721 die("Login succeeded unexpectedly");
722
723 } else {
724 $resp_code = $client->response_code();
725 $resp_msg = $client->response_msg();
726 }
727
728 my $expected;
729
730 $expected = 530;
731 $self->assert($expected == $resp_code,
732 test_msg("Expected $expected, got $resp_code"));
733
734 $expected = "Login incorrect.";
735 $self->assert($expected eq $resp_msg,
736 test_msg("Expected '$expected', got '$resp_msg'"));
737
738 eval { $client->login($user, 'foo') };
739 unless ($@) {
740 die("Login succeeded unexpectedly");
741
742 } else {
743 $resp_code = $client->response_code();
744 $resp_msg = $client->response_msg();
745 }
746
747 $expected = 530;
748 $self->assert($expected == $resp_code,
749 test_msg("Expected $expected, got $resp_code"));
750
751 $expected = "Login incorrect.";
752 $self->assert($expected eq $resp_msg,
753 test_msg("Expected '$expected', got '$resp_msg'"));
754
755 # Now try again with the correct info; we should be banned. Note
756 # that we have to create a separate connection for this.
757
758 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
759 undef, 0) };
760 unless ($@) {
761 die("Connect succeeded unexpectedly");
762 }
763
764 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
765
766 $expected = "";
767 $self->assert($expected eq $conn_ex,
768 test_msg("Expected '$expected', got '$conn_ex'"));
769 };
770
771 if ($@) {
772 $ex = $@;
773 }
774
775 $wfh->print("done\n");
776 $wfh->flush();
777
778 } else {
779 eval { server_wait($config_file, $rfh) };
780 if ($@) {
781 warn($@);
782 exit 1;
783 }
784
785 exit 0;
786 }
787
788 # Stop server
789 server_stop($pid_file);
790
791 $self->assert_child_ok($pid);
792
793 if ($ex) {
794 test_append_logfile($log_file, $ex);
795 unlink($log_file);
796
797 die($ex);
798 }
799
800 unlink($log_file);
801 }
802
803 sub ban_ifclass_engine_off {
804 my $self = shift;
805 my $tmpdir = $self->{tmpdir};
806
807 my $config_file = "$tmpdir/ban.conf";
808 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
809 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
810
811 my $log_file = test_get_logfile();
812
813 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
814
815 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
816 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
817
818 my $user = 'proftpd';
819 my $passwd = 'test';
820 my $group = 'ftpd';
821 my $home_dir = File::Spec->rel2abs($tmpdir);
822 my $uid = 500;
823 my $gid = 500;
824
825 # Make sure that, if we're running as root, that the home directory has
826 # permissions/privs set for the account we create
827 if ($< == 0) {
828 unless (chmod(0755, $home_dir)) {
829 die("Can't set perms on $home_dir to 0755: $!");
830 }
831
832 unless (chown($uid, $gid, $home_dir)) {
833 die("Can't set owner of $home_dir to $uid/$gid: $!");
834 }
835 }
836
837 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
838 '/bin/bash');
839 auth_group_write($auth_group_file, $group, $gid, $user);
840
841 my $config = {
842 PidFile => $pid_file,
843 ScoreboardFile => $scoreboard_file,
844 SystemLog => $log_file,
845
846 AuthUserFile => $auth_user_file,
847 AuthGroupFile => $auth_group_file,
848 AuthOrder => 'mod_auth_file.c',
849
850 MaxLoginAttempts => 1,
851
852 Class => {
853 test => {
854 From => '127.0.0.1',
855 },
856 },
857
858 IfModules => {
859 'mod_ban.c' => {
860 BanEngine => 'on',
861 BanLog => $log_file,
862
863 # This says to ban a client which exceeds the MaxLoginAttempts
864 # limit once within the last 1 minute will be banned for 5 secs
865 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05',
866
867 BanTable => $ban_tab,
868 },
869
870 'mod_delay.c' => {
871 DelayEngine => 'off',
872 },
873 },
874 };
875
876 my ($port, $config_user, $config_group) = config_write($config_file, $config);
877
878 if (open(my $fh, ">> $config_file")) {
879 print $fh <<EOI;
880 <IfClass test>
881 BanEngine off
882 </IfClass>
883 EOI
884 unless (close($fh)) {
885 die("Can't write $config_file: $!");
886 }
887
888 } else {
889 die("Can't open $config_file: $!");
890 }
891
892 # Open pipes, for use between the parent and child processes. Specifically,
893 # the child will indicate when it's done with its test by writing a message
894 # to the parent.
895 my ($rfh, $wfh);
896 unless (pipe($rfh, $wfh)) {
897 die("Can't open pipe: $!");
898 }
899
900 my $ex;
901
902 # Fork child
903 $self->handle_sigchld();
904 defined(my $pid = fork()) or die("Can't fork: $!");
905 if ($pid) {
906 eval {
907 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
908
909 my ($resp_code, $resp_msg);
910
911 eval { $client->login($user, 'foo') };
912 unless ($@) {
913 die("Login succeeded unexpectedly");
914
915 } else {
916 $resp_code = $client->response_code();
917 $resp_msg = $client->response_msg();
918 }
919
920 my $expected;
921
922 $expected = 530;
923 $self->assert($expected == $resp_code,
924 test_msg("Expected $expected, got $resp_code"));
925
926 $expected = "Login incorrect.";
927 $self->assert($expected eq $resp_msg,
928 test_msg("Expected '$expected', got '$resp_msg'"));
929
930 # Now try again with the correct info; we should NOT be banned. Note
931 # that we have to create a separate connection for this.
932
933 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
934 undef, 0) };
935 if ($@) {
936 die("Connect failed unexpectedly");
937 }
938 };
939
940 if ($@) {
941 $ex = $@;
942 }
943
944 $wfh->print("done\n");
945 $wfh->flush();
946
947 } else {
948 eval { server_wait($config_file, $rfh) };
949 if ($@) {
950 warn($@);
951 exit 1;
952 }
953
954 exit 0;
955 }
956
957 # Stop server
958 server_stop($pid_file);
959
960 $self->assert_child_ok($pid);
961
962 if ($ex) {
963 test_append_logfile($log_file, $ex);
964 unlink($log_file);
965
966 die($ex);
967 }
968
969 unlink($log_file);
970 }
971
972 sub ban_max_logins_exceeded_bug3281 {
973 my $self = shift;
974 my $tmpdir = $self->{tmpdir};
975
976 my $config_file = "$tmpdir/ban.conf";
977 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
978 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
979
980 my $log_file = test_get_logfile();
981
982 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
983
984 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
985 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
986
987 my $user = 'proftpd';
988 my $passwd = 'test';
989 my $group = 'ftpd';
990 my $home_dir = File::Spec->rel2abs($tmpdir);
991 my $uid = 500;
992 my $gid = 500;
993
994 # Make sure that, if we're running as root, that the home directory has
995 # permissions/privs set for the account we create
996 if ($< == 0) {
997 unless (chmod(0755, $home_dir)) {
998 die("Can't set perms on $home_dir to 0755: $!");
999 }
1000
1001 unless (chown($uid, $gid, $home_dir)) {
1002 die("Can't set owner of $home_dir to $uid/$gid: $!");
1003 }
1004 }
1005
1006 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1007 '/bin/bash');
1008 auth_group_write($auth_group_file, $group, $gid, $user);
1009
1010 my $config = {
1011 PidFile => $pid_file,
1012 ScoreboardFile => $scoreboard_file,
1013 SystemLog => $log_file,
1014 TraceLog => $log_file,
1015 Trace => 'lock:10 scoreboard:10',
1016
1017 AuthUserFile => $auth_user_file,
1018 AuthGroupFile => $auth_group_file,
1019 AuthOrder => 'mod_auth_file.c',
1020
1021 MaxLoginAttempts => 2,
1022
1023 IfModules => {
1024 'mod_ban.c' => {
1025 BanEngine => 'on',
1026 BanLog => $log_file,
1027
1028 # This says to ban a client which exceeds the MaxLoginAttempts
1029 # limit once within the last 1 minute will be banned for 5 secs
1030 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05',
1031
1032 BanTable => $ban_tab,
1033 },
1034
1035 'mod_delay.c' => {
1036 DelayEngine => 'off',
1037 },
1038 },
1039 };
1040
1041 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1042
1043 # Open pipes, for use between the parent and child processes. Specifically,
1044 # the child will indicate when it's done with its test by writing a message
1045 # to the parent.
1046 my ($rfh, $wfh);
1047 unless (pipe($rfh, $wfh)) {
1048 die("Can't open pipe: $!");
1049 }
1050
1051 my $ex;
1052
1053 # Fork child
1054 $self->handle_sigchld();
1055 defined(my $pid = fork()) or die("Can't fork: $!");
1056 if ($pid) {
1057 eval {
1058 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1059
1060 my ($resp_code, $resp_msg);
1061
1062 eval { $client->login($user, 'foo') };
1063 unless ($@) {
1064 die("Login succeeded unexpectedly");
1065
1066 } else {
1067 $resp_code = $client->response_code();
1068 $resp_msg = $client->response_msg();
1069 }
1070
1071 my $expected;
1072
1073 $expected = 530;
1074 $self->assert($expected == $resp_code,
1075 test_msg("Expected $expected, got $resp_code"));
1076
1077 $expected = "Login incorrect.";
1078 $self->assert($expected eq $resp_msg,
1079 test_msg("Expected '$expected', got '$resp_msg'"));
1080
1081 # Try the login again
1082 eval { $client->login($user, 'foo') };
1083 unless ($@) {
1084 die("Login succeeded unexpectedly");
1085
1086 } else {
1087 $resp_code = $client->response_code();
1088 $resp_msg = $client->response_msg();
1089 }
1090
1091 $expected = 530;
1092 $self->assert($expected == $resp_code,
1093 test_msg("Expected $expected, got $resp_code"));
1094
1095 $expected = "Login incorrect.";
1096 $self->assert($expected eq $resp_msg,
1097 test_msg("Expected '$expected', got '$resp_msg'"));
1098
1099 # According to Bug#3281, the session process should be have closed/ended.
1100 # Make sure this is the case. This QUIT command should fail.
1101 eval { $client->quit() };
1102 unless ($@) {
1103 die("QUIT succeeded unexpectedly");
1104 }
1105
1106 # Now try again with the correct info; we should be banned. Note
1107 # that we have to create a separate connection for this.
1108
1109 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
1110 undef, 0) };
1111 unless ($@) {
1112 die("Connect succeeded unexpectedly");
1113 }
1114
1115 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
1116
1117 $expected = "";
1118 $self->assert($expected eq $conn_ex,
1119 test_msg("Expected '$expected', got '$conn_ex'"));
1120 };
1121
1122 if ($@) {
1123 $ex = $@;
1124 }
1125
1126 $wfh->print("done\n");
1127 $wfh->flush();
1128
1129 } else {
1130 eval { server_wait($config_file, $rfh) };
1131 if ($@) {
1132 warn($@);
1133 exit 1;
1134 }
1135
1136 exit 0;
1137 }
1138
1139 # Stop server
1140 server_stop($pid_file);
1141
1142 $self->assert_child_ok($pid);
1143
1144 if ($ex) {
1145 test_append_logfile($log_file, $ex);
1146 unlink($log_file);
1147
1148 die($ex);
1149 }
1150
1151 unlink($log_file);
1152 }
1153
1154 sub ban_timeout_login_exceeded_bug3281 {
1155 my $self = shift;
1156 my $tmpdir = $self->{tmpdir};
1157 my $setup = test_setup($tmpdir, 'ban');
1158
1159 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
1160
1161 my $timeout_login = 1;
1162
1163 my $config = {
1164 PidFile => $setup->{pid_file},
1165 ScoreboardFile => $setup->{scoreboard_file},
1166 SystemLog => $setup->{log_file},
1167 TraceLog => $setup->{log_file},
1168 Trace => 'ban:10 event:10',
1169
1170 AuthUserFile => $setup->{auth_user_file},
1171 AuthGroupFile => $setup->{auth_group_file},
1172 AuthOrder => 'mod_auth_file.c',
1173
1174 TimeoutLogin => $timeout_login,
1175
1176 IfModules => {
1177 'mod_ban.c' => {
1178 BanEngine => 'on',
1179 BanLog => $setup->{log_file},
1180
1181 # This says to ban a client which exceeds the TimeoutLogin
1182 # limit once within the last 1 minute will be banned for 5 secs
1183 BanOnEvent => 'TimeoutLogin 1/00:01:00 00:00:05',
1184
1185 BanTable => $ban_tab,
1186 },
1187
1188 'mod_delay.c' => {
1189 DelayEngine => 'off',
1190 },
1191 },
1192 };
1193
1194 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1195 $config);
1196
1197 # Open pipes, for use between the parent and child processes. Specifically,
1198 # the child will indicate when it's done with its test by writing a message
1199 # to the parent.
1200 my ($rfh, $wfh);
1201 unless (pipe($rfh, $wfh)) {
1202 die("Can't open pipe: $!");
1203 }
1204
1205 my $ex;
1206
1207 # Fork child
1208 $self->handle_sigchld();
1209 defined(my $pid = fork()) or die("Can't fork: $!");
1210 if ($pid) {
1211 eval {
1212 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1213
1214 # Wait more than the TimeoutLogin interval
1215 sleep($timeout_login + 1);
1216
1217 eval { $client->user($setup->{user}) };
1218 unless ($@) {
1219 die("USER succeeded unexpectedly");
1220 }
1221
1222 # According to Bug#3281, the session process should be have closed/ended.
1223 # Make sure this is the case. This QUIT command should fail.
1224 eval { $client->quit() };
1225 unless ($@) {
1226 die("QUIT succeeded unexpectedly");
1227 }
1228
1229 # Now try to connect; we should be banned.
1230 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
1231 undef, 0) };
1232 unless ($@) {
1233 die("Connect succeeded unexpectedly");
1234 }
1235
1236 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
1237
1238 my $expected = "";
1239 $self->assert($expected eq $conn_ex,
1240 test_msg("Expected '$expected', got '$conn_ex'"));
1241 };
1242 if ($@) {
1243 $ex = $@;
1244 }
1245
1246 $wfh->print("done\n");
1247 $wfh->flush();
1248
1249 } else {
1250 eval { server_wait($setup->{config_file}, $rfh, 15) };
1251 if ($@) {
1252 warn($@);
1253 exit 1;
1254 }
1255
1256 exit 0;
1257 }
1258
1259 # Stop server
1260 server_stop($setup->{pid_file});
1261 $self->assert_child_ok($pid);
1262
1263 test_cleanup($setup->{log_file}, $ex);
1264 }
1265
1266 sub ban_engine_vhost_bug3355 {
1267 my $self = shift;
1268 my $tmpdir = $self->{tmpdir};
1269
1270 my $config_file = "$tmpdir/ban.conf";
1271 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
1272 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
1273
1274 my $log_file = test_get_logfile();
1275
1276 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
1277
1278 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
1279 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
1280
1281 my $user = 'proftpd';
1282 my $passwd = 'test';
1283 my $group = 'ftpd';
1284 my $home_dir = File::Spec->rel2abs($tmpdir);
1285 my $uid = 500;
1286 my $gid = 500;
1287
1288 # Make sure that, if we're running as root, that the home directory has
1289 # permissions/privs set for the account we create
1290 if ($< == 0) {
1291 unless (chmod(0755, $home_dir)) {
1292 die("Can't set perms on $home_dir to 0755: $!");
1293 }
1294
1295 unless (chown($uid, $gid, $home_dir)) {
1296 die("Can't set owner of $home_dir to $uid/$gid: $!");
1297 }
1298 }
1299
1300 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1301 '/bin/bash');
1302 auth_group_write($auth_group_file, $group, $gid, $user);
1303
1304 my $max_login_attempts = 2;
1305
1306 my $config = {
1307 PidFile => $pid_file,
1308 ScoreboardFile => $scoreboard_file,
1309 SystemLog => $log_file,
1310 TraceLog => $log_file,
1311 Trace => 'event:10',
1312 DefaultServer => 'off',
1313
1314 AuthUserFile => $auth_user_file,
1315 AuthGroupFile => $auth_group_file,
1316 AuthOrder => 'mod_auth_file.c',
1317
1318 MaxLoginAttempts => $max_login_attempts,
1319
1320 IfModules => {
1321 'mod_ban.c' => {
1322 BanLog => $log_file,
1323
1324 # This says to ban a client which exceeds the MaxLoginAttempts
1325 # limit once within the last 1 minute will be banned for 5 secs
1326 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05',
1327
1328 BanTable => $ban_tab,
1329 },
1330
1331 'mod_delay.c' => {
1332 DelayEngine => 'off',
1333 },
1334 },
1335 };
1336
1337 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1338 my $vhost_port = $port + 21;
1339
1340 if (open(my $fh, ">> $config_file")) {
1341 print $fh <<EOC;
1342 <VirtualHost 127.0.0.1>
1343 Port $vhost_port
1344 AuthUserFile $auth_user_file
1345 AuthGroupFile $auth_group_file
1346 AuthOrder mod_auth_file.c
1347
1348 MaxLoginAttempts $max_login_attempts
1349 <IfModule mod_ban.c>
1350 BanEngine off
1351 </IfModule>
1352 </VirtualHost>
1353 EOC
1354 unless (close($fh)) {
1355 die("Can't write $config_file: $!");
1356 }
1357
1358 } else {
1359 die("Can't open $config_file: $!");
1360 }
1361
1362 # Open pipes, for use between the parent and child processes. Specifically,
1363 # the child will indicate when it's done with its test by writing a message
1364 # to the parent.
1365 my ($rfh, $wfh);
1366 unless (pipe($rfh, $wfh)) {
1367 die("Can't open pipe: $!");
1368 }
1369
1370 my $ex;
1371
1372 # Fork child
1373 $self->handle_sigchld();
1374 defined(my $pid = fork()) or die("Can't fork: $!");
1375 if ($pid) {
1376 eval {
1377 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $vhost_port);
1378
1379 my ($resp_code, $resp_msg);
1380
1381 eval { $client->login($user, 'foo') };
1382 unless ($@) {
1383 die("Login succeeded unexpectedly");
1384
1385 } else {
1386 $resp_code = $client->response_code();
1387 $resp_msg = $client->response_msg();
1388 }
1389
1390 my $expected;
1391
1392 $expected = 530;
1393 $self->assert($expected == $resp_code,
1394 test_msg("Expected $expected, got $resp_code"));
1395
1396 $expected = "Login incorrect.";
1397 $self->assert($expected eq $resp_msg,
1398 test_msg("Expected '$expected', got '$resp_msg'"));
1399
1400 eval { $client->login($user, 'foo') };
1401 unless ($@) {
1402 die("Login succeeded unexpectedly");
1403
1404 } else {
1405 $resp_code = $client->response_code();
1406 $resp_msg = $client->response_msg();
1407 }
1408
1409 $expected = 530;
1410 $self->assert($expected == $resp_code,
1411 test_msg("Expected $expected, got $resp_code"));
1412
1413 $expected = "Login incorrect.";
1414 $self->assert($expected eq $resp_msg,
1415 test_msg("Expected '$expected', got '$resp_msg'"));
1416
1417 # Now try again with the correct info; we should be banned, except
1418 # that we are connecting to the <VirtualHost> which has mod_ban disabled.
1419 # Note that we have to create a separate connection for this.
1420
1421 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $vhost_port);
1422 $client->login($user, $passwd);
1423 $client->quit();
1424 };
1425
1426 if ($@) {
1427 $ex = $@;
1428 }
1429
1430 $wfh->print("done\n");
1431 $wfh->flush();
1432
1433 } else {
1434 eval { server_wait($config_file, $rfh) };
1435 if ($@) {
1436 warn($@);
1437 exit 1;
1438 }
1439
1440 exit 0;
1441 }
1442
1443 # Stop server
1444 server_stop($pid_file);
1445
1446 $self->assert_child_ok($pid);
1447
1448 if ($ex) {
1449 test_append_logfile($log_file, $ex);
1450 unlink($log_file);
1451
1452 die($ex);
1453 }
1454
1455 unlink($log_file);
1456 }
1457
1458 sub ban_unhandled_cmd {
1459 my $self = shift;
1460 my $tmpdir = $self->{tmpdir};
1461
1462 my $config_file = "$tmpdir/ban.conf";
1463 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
1464 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
1465
1466 my $log_file = test_get_logfile();
1467
1468 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
1469
1470 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
1471 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
1472
1473 my $user = 'proftpd';
1474 my $passwd = 'test';
1475 my $group = 'ftpd';
1476 my $home_dir = File::Spec->rel2abs($tmpdir);
1477 my $uid = 500;
1478 my $gid = 500;
1479
1480 # Make sure that, if we're running as root, that the home directory has
1481 # permissions/privs set for the account we create
1482 if ($< == 0) {
1483 unless (chmod(0755, $home_dir)) {
1484 die("Can't set perms on $home_dir to 0755: $!");
1485 }
1486
1487 unless (chown($uid, $gid, $home_dir)) {
1488 die("Can't set owner of $home_dir to $uid/$gid: $!");
1489 }
1490 }
1491
1492 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1493 '/bin/bash');
1494 auth_group_write($auth_group_file, $group, $gid, $user);
1495
1496 my $config = {
1497 PidFile => $pid_file,
1498 ScoreboardFile => $scoreboard_file,
1499 SystemLog => $log_file,
1500 TraceLog => $log_file,
1501 Trace => 'event:10',
1502
1503 AuthUserFile => $auth_user_file,
1504 AuthGroupFile => $auth_group_file,
1505 AuthOrder => 'mod_auth_file.c',
1506
1507 MaxLoginAttempts => 1,
1508
1509 IfModules => {
1510 'mod_ban.c' => {
1511 BanEngine => 'on',
1512 BanLog => $log_file,
1513
1514 # This says to ban a client which sends unhandled commands
1515 # three times within the last 1 minute will be banned for 5 secs
1516 BanOnEvent => 'UnhandledCommand 3/00:01:00 00:00:05',
1517
1518 BanTable => $ban_tab,
1519 },
1520
1521 'mod_delay.c' => {
1522 DelayEngine => 'off',
1523 },
1524 },
1525 };
1526
1527 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1528
1529 # Open pipes, for use between the parent and child processes. Specifically,
1530 # the child will indicate when it's done with its test by writing a message
1531 # to the parent.
1532 my ($rfh, $wfh);
1533 unless (pipe($rfh, $wfh)) {
1534 die("Can't open pipe: $!");
1535 }
1536
1537 my $ex;
1538
1539 # Fork child
1540 $self->handle_sigchld();
1541 defined(my $pid = fork()) or die("Can't fork: $!");
1542 if ($pid) {
1543 eval {
1544 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1545
1546 eval { $client->quote('FOO') };
1547 unless ($@) {
1548 die("FOO command succeeded unexpectedly");
1549 }
1550
1551 my $resp_code = $client->response_code();
1552 my $resp_msg = $client->response_msg();
1553
1554 my $expected;
1555
1556 $expected = 500;
1557 $self->assert($expected == $resp_code,
1558 test_msg("Expected $expected, got $resp_code"));
1559
1560 $expected = "FOO not understood";
1561 $self->assert($expected eq $resp_msg,
1562 test_msg("Expected '$expected', got '$resp_msg'"));
1563
1564 eval { $client->quote('BAR') };
1565 unless ($@) {
1566 die("BAR command succeeded unexpectedly");
1567 }
1568
1569 $resp_code = $client->response_code();
1570 $resp_msg = $client->response_msg();
1571
1572 $expected = 500;
1573 $self->assert($expected == $resp_code,
1574 test_msg("Expected $expected, got $resp_code"));
1575
1576 $expected = "BAR not understood";
1577 $self->assert($expected eq $resp_msg,
1578 test_msg("Expected '$expected', got '$resp_msg'"));
1579
1580 # The mod_ban rule will kick in as part of handling this command, so
1581 # this command shouldn't even get a response (which Net::FTP indicates
1582 # with a response code of "000").
1583
1584 eval { $client->quote('BAZ') };
1585 unless ($@) {
1586 die("BAZ command succeeded unexpectedly");
1587 }
1588
1589 $resp_code = $client->response_code();
1590
1591 $expected = 000;
1592 $self->assert($expected == $resp_code,
1593 test_msg("Expected $expected, got $resp_code"));
1594
1595 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
1596 undef, 0) };
1597 unless ($@) {
1598 die("Connect succeeded unexpectedly");
1599 }
1600
1601 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
1602
1603 $expected = "";
1604 $self->assert($expected eq $conn_ex,
1605 test_msg("Expected '$expected', got '$conn_ex'"));
1606 };
1607
1608 if ($@) {
1609 $ex = $@;
1610 }
1611
1612 $wfh->print("done\n");
1613 $wfh->flush();
1614
1615 } else {
1616 eval { server_wait($config_file, $rfh) };
1617 if ($@) {
1618 warn($@);
1619 exit 1;
1620 }
1621
1622 exit 0;
1623 }
1624
1625 # Stop server
1626 server_stop($pid_file);
1627
1628 $self->assert_child_ok($pid);
1629
1630 if ($ex) {
1631 test_append_logfile($log_file, $ex);
1632 unlink($log_file);
1633
1634 die($ex);
1635 }
1636
1637 unlink($log_file);
1638 }
1639
1640 sub ban_on_event_client_connect_rate {
1641 my $self = shift;
1642 my $tmpdir = $self->{tmpdir};
1643
1644 my $config_file = "$tmpdir/ban.conf";
1645 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
1646 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
1647
1648 my $log_file = test_get_logfile();
1649
1650 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
1651
1652 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
1653 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
1654
1655 my $user = 'proftpd';
1656 my $passwd = 'test';
1657 my $group = 'ftpd';
1658 my $home_dir = File::Spec->rel2abs($tmpdir);
1659 my $uid = 500;
1660 my $gid = 500;
1661
1662 # Make sure that, if we're running as root, that the home directory has
1663 # permissions/privs set for the account we create
1664 if ($< == 0) {
1665 unless (chmod(0755, $home_dir)) {
1666 die("Can't set perms on $home_dir to 0755: $!");
1667 }
1668
1669 unless (chown($uid, $gid, $home_dir)) {
1670 die("Can't set owner of $home_dir to $uid/$gid: $!");
1671 }
1672 }
1673
1674 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1675 '/bin/bash');
1676 auth_group_write($auth_group_file, $group, $gid, $user);
1677
1678 my $config = {
1679 PidFile => $pid_file,
1680 ScoreboardFile => $scoreboard_file,
1681 SystemLog => $log_file,
1682 TraceLog => $log_file,
1683 Trace => 'event:10',
1684
1685 AuthUserFile => $auth_user_file,
1686 AuthGroupFile => $auth_group_file,
1687 AuthOrder => 'mod_auth_file.c',
1688
1689 IfModules => {
1690 'mod_ban.c' => {
1691 BanEngine => 'on',
1692 BanLog => $log_file,
1693
1694 # This says to ban a client which connects more than twice in the
1695 # last 1 minute will be banned for 5 secs
1696 BanOnEvent => 'ClientConnectRate 2/00:01:00 00:00:05',
1697
1698 BanTable => $ban_tab,
1699 },
1700
1701 'mod_delay.c' => {
1702 DelayEngine => 'off',
1703 },
1704 },
1705 };
1706
1707 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1708
1709 # Open pipes, for use between the parent and child processes. Specifically,
1710 # the child will indicate when it's done with its test by writing a message
1711 # to the parent.
1712 my ($rfh, $wfh);
1713 unless (pipe($rfh, $wfh)) {
1714 die("Can't open pipe: $!");
1715 }
1716
1717 my $ex;
1718
1719 # Fork child
1720 $self->handle_sigchld();
1721 defined(my $pid = fork()) or die("Can't fork: $!");
1722 if ($pid) {
1723 eval {
1724 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1725
1726 eval { $client->login($user, 'foo') };
1727 unless ($@) {
1728 die("Login succeeded unexpectedly");
1729 }
1730
1731 my $resp_code = $client->response_code();
1732 my $resp_msg = $client->response_msg();
1733
1734 my $expected;
1735
1736 $expected = 530;
1737 $self->assert($expected == $resp_code,
1738 test_msg("Expected $expected, got $resp_code"));
1739
1740 $expected = "Login incorrect.";
1741 $self->assert($expected eq $resp_msg,
1742 test_msg("Expected '$expected', got '$resp_msg'"));
1743
1744 eval { $client->login($user, 'foo') };
1745 unless ($@) {
1746 die("Login succeeded unexpectedly");
1747 }
1748
1749 $resp_code = $client->response_code();
1750 $resp_msg = $client->response_msg();
1751
1752 $expected = 530;
1753 $self->assert($expected == $resp_code,
1754 test_msg("Expected $expected, got $resp_code"));
1755
1756 $expected = "Login incorrect.";
1757 $self->assert($expected eq $resp_msg,
1758 test_msg("Expected '$expected', got '$resp_msg'"));
1759
1760 # Now try again with the correct info; we should be banned. Note
1761 # that we have to create a separate connection for this.
1762
1763 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
1764 undef, 0) };
1765 unless ($@) {
1766 die("Connect succeeded unexpectedly");
1767 }
1768
1769 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
1770
1771 $expected = "";
1772 $self->assert($expected eq $conn_ex,
1773 test_msg("Expected '$expected', got '$conn_ex'"));
1774 };
1775
1776 if ($@) {
1777 $ex = $@;
1778 }
1779
1780 $wfh->print("done\n");
1781 $wfh->flush();
1782
1783 } else {
1784 eval { server_wait($config_file, $rfh) };
1785 if ($@) {
1786 warn($@);
1787 exit 1;
1788 }
1789
1790 exit 0;
1791 }
1792
1793 # Stop server
1794 server_stop($pid_file);
1795
1796 $self->assert_child_ok($pid);
1797
1798 if ($ex) {
1799 test_append_logfile($log_file, $ex);
1800 unlink($log_file);
1801
1802 die($ex);
1803 }
1804
1805 unlink($log_file);
1806 }
1807
1808 sub ban_sighup_bug3751 {
1809 my $self = shift;
1810 my $tmpdir = $self->{tmpdir};
1811
1812 my $config_file = "$tmpdir/ban.conf";
1813 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
1814 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
1815
1816 my $log_file = test_get_logfile();
1817
1818 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
1819
1820 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
1821 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
1822
1823 my $user = 'proftpd';
1824 my $passwd = 'test';
1825 my $group = 'ftpd';
1826 my $home_dir = File::Spec->rel2abs($tmpdir);
1827 my $uid = 500;
1828 my $gid = 500;
1829
1830 # Make sure that, if we're running as root, that the home directory has
1831 # permissions/privs set for the account we create
1832 if ($< == 0) {
1833 unless (chmod(0755, $home_dir)) {
1834 die("Can't set perms on $home_dir to 0755: $!");
1835 }
1836
1837 unless (chown($uid, $gid, $home_dir)) {
1838 die("Can't set owner of $home_dir to $uid/$gid: $!");
1839 }
1840 }
1841
1842 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1843 '/bin/bash');
1844 auth_group_write($auth_group_file, $group, $gid, $user);
1845
1846 my $config = {
1847 PidFile => $pid_file,
1848 ScoreboardFile => $scoreboard_file,
1849 SystemLog => $log_file,
1850 TraceLog => $log_file,
1851 Trace => 'event:10',
1852
1853 AuthUserFile => $auth_user_file,
1854 AuthGroupFile => $auth_group_file,
1855 AuthOrder => 'mod_auth_file.c',
1856
1857 MaxLoginAttempts => 2,
1858
1859 IfModules => {
1860 'mod_ban.c' => {
1861 BanEngine => 'on',
1862 BanLog => $log_file,
1863
1864 # This says to ban a client which exceeds the MaxLoginAttempts
1865 # limit once within the last 1 minute will be banned for 5 secs
1866 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:05',
1867
1868 BanTable => $ban_tab,
1869 },
1870
1871 'mod_delay.c' => {
1872 DelayEngine => 'off',
1873 },
1874 },
1875 };
1876
1877 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1878
1879 # Start the server
1880 server_start($config_file);
1881 sleep(1);
1882
1883 # Use proc(5) filesystem to count the number of open fds in the daemon
1884 my $orig_nfds = server_open_fds($pid_file);
1885 if ($ENV{TEST_VERBOSE}) {
1886 print STDERR "Found $orig_nfds open fds after server startup\n";
1887 }
1888
1889 # Restart the server
1890 server_restart($pid_file);
1891 sleep(1);
1892
1893 # Count the open fds again, make sure we haven't leaked any
1894 my $restart_nfds = server_open_fds($pid_file);
1895 if ($ENV{TEST_VERBOSE}) {
1896 print STDERR "Found $restart_nfds open fds after server restart #1\n";
1897 }
1898
1899 $self->assert($orig_nfds == $restart_nfds,
1900 test_msg("Expected $orig_nfds open fds, found $restart_nfds"));
1901
1902 # Restart the server
1903 server_restart($pid_file);
1904 sleep(1);
1905
1906 # And count the open fds one more time, to make doubly sure we are not
1907 # leaking fds.
1908 $restart_nfds = server_open_fds($pid_file);
1909 if ($ENV{TEST_VERBOSE}) {
1910 print STDERR "Found $restart_nfds open fds after server restart #2\n";
1911 }
1912
1913 $self->assert($orig_nfds == $restart_nfds,
1914 test_msg("Expected $orig_nfds open fds, found $restart_nfds"));
1915
1916 # Stop server
1917 server_stop($pid_file);
1918 unlink($log_file);
1919 }
1920
1921 sub ban_on_event_tlshandshake {
1922 my $self = shift;
1923 my $tmpdir = $self->{tmpdir};
1924
1925 my $config_file = "$tmpdir/ban.conf";
1926 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
1927 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
1928
1929 my $log_file = test_get_logfile();
1930
1931 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
1932
1933 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
1934 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
1935
1936 my $user = 'proftpd';
1937 my $passwd = 'test';
1938 my $group = 'ftpd';
1939 my $home_dir = File::Spec->rel2abs($tmpdir);
1940 my $uid = 500;
1941 my $gid = 500;
1942
1943 # Make sure that, if we're running as root, that the home directory has
1944 # permissions/privs set for the account we create
1945 if ($< == 0) {
1946 unless (chmod(0755, $home_dir)) {
1947 die("Can't set perms on $home_dir to 0755: $!");
1948 }
1949
1950 unless (chown($uid, $gid, $home_dir)) {
1951 die("Can't set owner of $home_dir to $uid/$gid: $!");
1952 }
1953 }
1954
1955 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1956 '/bin/bash');
1957 auth_group_write($auth_group_file, $group, $gid, $user);
1958
1959 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
1960 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
1961
1962 my $config = {
1963 PidFile => $pid_file,
1964 ScoreboardFile => $scoreboard_file,
1965 SystemLog => $log_file,
1966 TraceLog => $log_file,
1967 Trace => 'event:10',
1968
1969 AuthUserFile => $auth_user_file,
1970 AuthGroupFile => $auth_group_file,
1971 AuthOrder => 'mod_auth_file.c',
1972
1973 IfModules => {
1974 'mod_ban.c' => {
1975 BanEngine => 'on',
1976 BanLog => $log_file,
1977
1978 # This says to ban a client which requests TLS handshakes (control
1979 # connection) more than twice in the last 1 minute will be banned for
1980 # 5 secs
1981 BanOnEvent => 'TLSHandshake 2/00:01:00 00:00:05',
1982
1983 BanTable => $ban_tab,
1984 },
1985
1986 'mod_delay.c' => {
1987 DelayEngine => 'off',
1988 },
1989
1990 'mod_tls.c' => {
1991 TLSEngine => 'on',
1992 TLSLog => $log_file,
1993 TLSRequired => 'on',
1994 TLSRSACertificateFile => $cert_file,
1995 TLSCACertificateFile => $ca_file,
1996 },
1997 },
1998 };
1999
2000 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2001
2002 # Open pipes, for use between the parent and child processes. Specifically,
2003 # the child will indicate when it's done with its test by writing a message
2004 # to the parent.
2005 my ($rfh, $wfh);
2006 unless (pipe($rfh, $wfh)) {
2007 die("Can't open pipe: $!");
2008 }
2009
2010 require Net::FTPSSL;
2011
2012 my $ex;
2013
2014 # Fork child
2015 $self->handle_sigchld();
2016 defined(my $pid = fork()) or die("Can't fork: $!");
2017 if ($pid) {
2018 eval {
2019 # Give the server a chance to start up
2020 sleep(2);
2021
2022 my $client_opts = {
2023 Encryption => 'E',
2024 Port => $port,
2025 };
2026
2027 if ($ENV{TEST_VERBOSE}) {
2028 $client_opts->{Debug} = 1;
2029 }
2030
2031 my $client = Net::FTPSSL->new('127.0.0.1', $client_opts);
2032 unless ($client) {
2033 die("Can't connect to FTPS server: " . IO::Socket::SSL::errstr());
2034 }
2035
2036 unless ($client->login($user, $passwd)) {
2037 die("Can't login: " . $client->last_message());
2038 }
2039
2040 $client->quit();
2041
2042 # Now try again; we should be banned. Note that we have to create a
2043 # separate connection for this.
2044
2045 $client = Net::FTPSSL->new('127.0.0.1', $client_opts);
2046 if ($client) {
2047 die("Client successfully connected to FTPS unexpectedly");
2048 }
2049
2050 my $errstr = IO::Socket::SSL::errstr();
2051 my $expected = 'connect attempt failed';
2052 $self->assert(qr/$expected/, $errstr,
2053 test_msg("Expected error message '$expected', got '$errstr'"));
2054 };
2055
2056 if ($@) {
2057 $ex = $@;
2058 }
2059
2060 $wfh->print("done\n");
2061 $wfh->flush();
2062
2063 } else {
2064 eval { server_wait($config_file, $rfh) };
2065 if ($@) {
2066 warn($@);
2067 exit 1;
2068 }
2069
2070 exit 0;
2071 }
2072
2073 # Stop server
2074 server_stop($pid_file);
2075
2076 $self->assert_child_ok($pid);
2077
2078 if ($ex) {
2079 test_append_logfile($log_file, $ex);
2080 unlink($log_file);
2081
2082 die($ex);
2083 }
2084
2085 unlink($log_file);
2086 }
2087
2088 sub ban_on_event_rootlogin {
2089 my $self = shift;
2090 my $tmpdir = $self->{tmpdir};
2091
2092 my $config_file = "$tmpdir/ban.conf";
2093 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
2094 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
2095
2096 my $log_file = test_get_logfile();
2097
2098 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
2099
2100 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
2101 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
2102
2103 my $user = 'proftpd';
2104 my $passwd = 'test';
2105 my $group = 'ftpd';
2106 my $home_dir = File::Spec->rel2abs($tmpdir);
2107 my $uid = 500;
2108 my $gid = 500;
2109
2110 my $root_user = 'root';
2111 my $root_passwd = 'root';
2112 my $root_group = 'ftpd';
2113 my $root_home_dir = File::Spec->rel2abs($tmpdir);
2114 my $root_uid = 0;
2115 my $root_gid = 0;
2116
2117 # Make sure that, if we're running as root, that the home directory has
2118 # permissions/privs set for the account we create
2119 if ($< == 0) {
2120 unless (chmod(0755, $home_dir)) {
2121 die("Can't set perms on $home_dir to 0755: $!");
2122 }
2123
2124 unless (chown($uid, $gid, $home_dir)) {
2125 die("Can't set owner of $home_dir to $uid/$gid: $!");
2126 }
2127 }
2128
2129 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2130 '/bin/bash');
2131 auth_group_write($auth_group_file, $group, $gid, $user);
2132
2133 auth_user_write($auth_user_file, $root_user, $root_passwd, $root_uid,
2134 $root_gid, $root_home_dir, '/bin/bash');
2135 auth_group_write($auth_group_file, $root_group, $root_gid, $root_user);
2136
2137 my $config = {
2138 PidFile => $pid_file,
2139 ScoreboardFile => $scoreboard_file,
2140 SystemLog => $log_file,
2141 TraceLog => $log_file,
2142 Trace => 'event:10',
2143
2144 AuthUserFile => $auth_user_file,
2145 AuthGroupFile => $auth_group_file,
2146 AuthOrder => 'mod_auth_file.c',
2147
2148 RootLogin => 'off',
2149
2150 IfModules => {
2151 'mod_ban.c' => {
2152 BanEngine => 'on',
2153 BanLog => $log_file,
2154
2155 # This says to ban a client which requests a root login more than twice
2156 # in the last 1 minute will be banned for 5 secs
2157 BanOnEvent => 'RootLogin 2/00:01:00 00:00:05',
2158
2159 BanTable => $ban_tab,
2160 },
2161
2162 'mod_delay.c' => {
2163 DelayEngine => 'off',
2164 },
2165 },
2166 };
2167
2168 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2169
2170 # Open pipes, for use between the parent and child processes. Specifically,
2171 # the child will indicate when it's done with its test by writing a message
2172 # to the parent.
2173 my ($rfh, $wfh);
2174 unless (pipe($rfh, $wfh)) {
2175 die("Can't open pipe: $!");
2176 }
2177
2178 require Net::FTPSSL;
2179
2180 my $ex;
2181
2182 # Fork child
2183 $self->handle_sigchld();
2184 defined(my $pid = fork()) or die("Can't fork: $!");
2185 if ($pid) {
2186 eval {
2187 # Give server time to start up
2188 sleep(2);
2189
2190 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2191 eval { $client->login($root_user, $root_passwd) };
2192 unless ($@) {
2193 die("Login succeeded unexpectedly");
2194 }
2195
2196 my $resp_code = $client->response_code();
2197 my $resp_msg = $client->response_msg();
2198
2199 my $expected;
2200
2201 $expected = 530;
2202 $self->assert($expected == $resp_code,
2203 test_msg("Expected response code $expected, got $resp_code"));
2204
2205 $expected = "Login incorrect.";
2206 $self->assert($expected eq $resp_msg,
2207 test_msg("Expected response message '$expected', got '$resp_msg'"));
2208 $client->quit();
2209
2210 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2211 $client->user($root_user);
2212 eval { $client->pass($root_passwd) };
2213 unless ($@) {
2214 die("PASS succeeded unexpectedly");
2215 }
2216
2217 $resp_code = $client->response_code();
2218 $resp_msg = $client->response_msg();
2219
2220 $expected = 000;
2221 $self->assert($expected == $resp_code,
2222 test_msg("Expected response code $expected, got $resp_code"));
2223
2224 # Now try again with the correct info; we should be banned. Note
2225 # that we have to create a separate connection for this.
2226 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
2227 undef, 0) };
2228 unless ($@) {
2229 die("Connect succeeded unexpectedly");
2230 }
2231
2232 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
2233
2234 $expected = "";
2235 $self->assert($expected eq $conn_ex,
2236 test_msg("Expected '$expected', got '$conn_ex'"));
2237 };
2238
2239 if ($@) {
2240 $ex = $@;
2241 }
2242
2243 $wfh->print("done\n");
2244 $wfh->flush();
2245
2246 } else {
2247 eval { server_wait($config_file, $rfh) };
2248 if ($@) {
2249 warn($@);
2250 exit 1;
2251 }
2252
2253 exit 0;
2254 }
2255
2256 # Stop server
2257 server_stop($pid_file);
2258
2259 $self->assert_child_ok($pid);
2260
2261 if ($ex) {
2262 test_append_logfile($log_file, $ex);
2263 unlink($log_file);
2264
2265 die($ex);
2266 }
2267
2268 unlink($log_file);
2269 }
2270
2271 sub ban_on_event_rootlogin_userdefined {
2272 my $self = shift;
2273 my $tmpdir = $self->{tmpdir};
2274
2275 my $config_file = "$tmpdir/ban.conf";
2276 my $pid_file = File::Spec->rel2abs("$tmpdir/ban.pid");
2277 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ban.scoreboard");
2278
2279 my $log_file = test_get_logfile();
2280
2281 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
2282
2283 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ban.passwd");
2284 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ban.group");
2285
2286 my $user = 'proftpd';
2287 my $passwd = 'test';
2288 my $group = 'ftpd';
2289 my $home_dir = File::Spec->rel2abs($tmpdir);
2290 my $uid = 500;
2291 my $gid = 500;
2292
2293 my $root_user = 'root';
2294 my $root_passwd = 'root';
2295 my $root_group = 'ftpd';
2296 my $root_home_dir = File::Spec->rel2abs($tmpdir);
2297 my $root_uid = 0;
2298 my $root_gid = 0;
2299
2300 # Make sure that, if we're running as root, that the home directory has
2301 # permissions/privs set for the account we create
2302 if ($< == 0) {
2303 unless (chmod(0755, $home_dir)) {
2304 die("Can't set perms on $home_dir to 0755: $!");
2305 }
2306
2307 unless (chown($uid, $gid, $home_dir)) {
2308 die("Can't set owner of $home_dir to $uid/$gid: $!");
2309 }
2310 }
2311
2312 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2313 '/bin/bash');
2314 auth_group_write($auth_group_file, $group, $gid, $user);
2315
2316 auth_user_write($auth_user_file, $root_user, $root_passwd, $root_uid,
2317 $root_gid, $root_home_dir, '/bin/bash');
2318 auth_group_write($auth_group_file, $root_group, $root_gid, $root_user);
2319
2320 my $config = {
2321 PidFile => $pid_file,
2322 ScoreboardFile => $scoreboard_file,
2323 SystemLog => $log_file,
2324 TraceLog => $log_file,
2325 Trace => 'event:10',
2326
2327 AuthUserFile => $auth_user_file,
2328 AuthGroupFile => $auth_group_file,
2329 AuthOrder => 'mod_auth_file.c',
2330
2331 RootLogin => 'off',
2332
2333 IfModules => {
2334 'mod_ban.c' => {
2335 BanEngine => 'on',
2336 BanLog => $log_file,
2337
2338 # This says to ban a client which requests a root login more than twice
2339 # in the last 1 minute will be banned for 5 secs
2340 BanOnEvent => 'mod_auth.root-login 2/00:01:00 00:00:05',
2341
2342 BanTable => $ban_tab,
2343 },
2344
2345 'mod_delay.c' => {
2346 DelayEngine => 'off',
2347 },
2348 },
2349 };
2350
2351 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2352
2353 # Open pipes, for use between the parent and child processes. Specifically,
2354 # the child will indicate when it's done with its test by writing a message
2355 # to the parent.
2356 my ($rfh, $wfh);
2357 unless (pipe($rfh, $wfh)) {
2358 die("Can't open pipe: $!");
2359 }
2360
2361 require Net::FTPSSL;
2362
2363 my $ex;
2364
2365 # Fork child
2366 $self->handle_sigchld();
2367 defined(my $pid = fork()) or die("Can't fork: $!");
2368 if ($pid) {
2369 eval {
2370 # Give server time to start up
2371 sleep(2);
2372
2373 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2374 eval { $client->login($root_user, $root_passwd) };
2375 unless ($@) {
2376 die("Login succeeded unexpectedly");
2377 }
2378
2379 my $resp_code = $client->response_code();
2380 my $resp_msg = $client->response_msg();
2381
2382 my $expected;
2383
2384 $expected = 530;
2385 $self->assert($expected == $resp_code,
2386 test_msg("Expected response code $expected, got $resp_code"));
2387
2388 $expected = "Login incorrect.";
2389 $self->assert($expected eq $resp_msg,
2390 test_msg("Expected response message '$expected', got '$resp_msg'"));
2391 $client->quit();
2392
2393 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2394 $client->user($root_user);
2395 eval { $client->pass($root_passwd) };
2396 unless ($@) {
2397 die("PASS succeeded unexpectedly");
2398 }
2399
2400 $resp_code = $client->response_code();
2401 $resp_msg = $client->response_msg();
2402
2403 $expected = 000;
2404 $self->assert($expected == $resp_code,
2405 test_msg("Expected response code $expected, got $resp_code"));
2406
2407 # Now try again with the correct info; we should be banned. Note
2408 # that we have to create a separate connection for this.
2409 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
2410 undef, 0) };
2411 unless ($@) {
2412 die("Connect succeeded unexpectedly");
2413 }
2414
2415 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
2416
2417 $expected = "";
2418 $self->assert($expected eq $conn_ex,
2419 test_msg("Expected '$expected', got '$conn_ex'"));
2420 };
2421
2422 if ($@) {
2423 $ex = $@;
2424 }
2425
2426 $wfh->print("done\n");
2427 $wfh->flush();
2428
2429 } else {
2430 eval { server_wait($config_file, $rfh) };
2431 if ($@) {
2432 warn($@);
2433 exit 1;
2434 }
2435
2436 exit 0;
2437 }
2438
2439 # Stop server
2440 server_stop($pid_file);
2441
2442 $self->assert_child_ok($pid);
2443
2444 if ($ex) {
2445 test_append_logfile($log_file, $ex);
2446 unlink($log_file);
2447
2448 die($ex);
2449 }
2450
2451 unlink($log_file);
2452 }
2453
2454 sub ban_opt_any_server_issue1010 {
2455 my $self = shift;
2456 my $tmpdir = $self->{tmpdir};
2457 my $setup = test_setup($tmpdir, 'ban');
2458
2459 my $ban_tab = File::Spec->rel2abs("$tmpdir/ban.tab");
2460
2461 my $vhost_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
2462 $vhost_port += 11;
2463
2464 my $config = {
2465 PidFile => $setup->{pid_file},
2466 ScoreboardFile => $setup->{scoreboard_file},
2467 SystemLog => $setup->{log_file},
2468 TraceLog => $setup->{log_file},
2469
2470 AuthUserFile => $setup->{auth_user_file},
2471 AuthGroupFile => $setup->{auth_group_file},
2472 AuthOrder => 'mod_auth_file.c',
2473
2474 MaxLoginAttempts => 2,
2475
2476 IfModules => {
2477 'mod_ban.c' => {
2478 BanEngine => 'on',
2479 BanLog => $setup->{log_file},
2480 BanTable => $ban_tab,
2481
2482 # This says to ban a client which exceeds the MaxLoginAttempts
2483 # limit once within the last 1 minute will be banned for 15 secs
2484 BanOnEvent => 'MaxLoginAttempts 1/00:01:00 00:00:15',
2485
2486 BanOptions => 'MatchAnyServer',
2487 },
2488
2489 'mod_delay.c' => {
2490 DelayEngine => 'off',
2491 },
2492 },
2493 };
2494
2495 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2496 $config);
2497
2498 if (open(my $fh, ">> $setup->{config_file}")) {
2499 print $fh <<EOC;
2500 <VirtualHost 127.0.0.1>
2501 Port $vhost_port
2502 ServerName "Other Server"
2503
2504 WtmpLog off
2505 TransferLog none
2506
2507 <IfModule mod_ban.c>
2508 BanEngine on
2509 </IfModule>
2510 </VirtualHost>
2511 EOC
2512 unless (close($fh)) {
2513 die("Can't write $setup->{config_file}: $!");
2514 }
2515
2516 } else {
2517 die("Can't open $setup->{config_file}: $!");
2518 }
2519
2520 # Open pipes, for use between the parent and child processes. Specifically,
2521 # the child will indicate when it's done with its test by writing a message
2522 # to the parent.
2523 my ($rfh, $wfh);
2524 unless (pipe($rfh, $wfh)) {
2525 die("Can't open pipe: $!");
2526 }
2527
2528 my $ex;
2529
2530 # Fork child
2531 $self->handle_sigchld();
2532 defined(my $pid = fork()) or die("Can't fork: $!");
2533 if ($pid) {
2534 eval {
2535 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2536
2537 eval { $client->login($setup->{user}, 'foo') };
2538 unless ($@) {
2539 die("Login succeeded unexpectedly");
2540 }
2541
2542 my $resp_code = $client->response_code();
2543 my $resp_msg = $client->response_msg();
2544
2545 my $expected = 530;
2546 $self->assert($expected == $resp_code,
2547 test_msg("Expected response code $expected, got $resp_code"));
2548
2549 $expected = 'Login incorrect.';
2550 $self->assert($expected eq $resp_msg,
2551 test_msg("Expected response message '$expected', got '$resp_msg'"));
2552
2553 eval { $client->login($setup->{user}, 'foo') };
2554 unless ($@) {
2555 die("Login succeeded unexpectedly");
2556 }
2557
2558 $resp_code = $client->response_code();
2559 $resp_msg = $client->response_msg();
2560
2561 $expected = 530;
2562 $self->assert($expected == $resp_code,
2563 test_msg("Expected respones code $expected, got $resp_code"));
2564
2565 $expected = 'Login incorrect.';
2566 $self->assert($expected eq $resp_msg,
2567 test_msg("Expected response message '$expected', got '$resp_msg'"));
2568
2569 # Now try again with the correct info; we should be banned. Note
2570 # that we have to create a separate connection for this.
2571
2572 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
2573 undef, 0) };
2574 unless ($@) {
2575 die("Connect succeeded unexpectedly");
2576 }
2577
2578 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
2579
2580 $expected = '';
2581 $self->assert($expected eq $conn_ex,
2582 test_msg("Expected '$expected', got '$conn_ex'"));
2583
2584 # We should also be banned by the OTHER vhost in the config, to which
2585 # we have not connected at all.
2586
2587 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $vhost_port,
2588 undef, 0) };
2589 unless ($@) {
2590 die("Connect succeeded unexpectedly");
2591 }
2592
2593 $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
2594
2595 $expected = '';
2596 $self->assert($expected eq $conn_ex,
2597 test_msg("Expected '$expected', got '$conn_ex'"));
2598 };
2599 if ($@) {
2600 $ex = $@;
2601 }
2602
2603 $wfh->print("done\n");
2604 $wfh->flush();
2605
2606 } else {
2607 eval { server_wait($setup->{config_file}, $rfh) };
2608 if ($@) {
2609 warn($@);
2610 exit 1;
2611 }
2612
2613 exit 0;
2614 }
2615
2616 # Stop server
2617 server_stop($setup->{pid_file});
2618 $self->assert_child_ok($pid);
2619
2620 test_cleanup($setup->{log_file}, $ex);
2621 }
2622
2623 1;