Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::PassivePorts;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9
10 use ProFTPD::TestSuite::FTP;
11 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13 $| = 1;
14
15 my $order = 0;
16
17 my $TESTS = {
18 pasv_ports_server_config => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 pasv_ports_global => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 pasv_ports_vhost => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 pasv_ports_random_issue1396 => {
34 order => ++$order,
35 test_class => [qw(bug forking)],
36 },
37 };
38
39 sub new {
40 return shift()->SUPER::new(@_);
41 }
42
43 sub list_tests {
44 return testsuite_get_runnable_tests($TESTS);
45 }
46
47 sub pasv_ports_server_config {
48 my $self = shift;
49 my $tmpdir = $self->{tmpdir};
50
51 my $config_file = "$tmpdir/config.conf";
52 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
53 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
54
55 my $log_file = test_get_logfile();
56
57 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
58 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
59
60 my $user = 'proftpd';
61 my $passwd = 'test';
62 my $group = 'ftpd';
63 my $home_dir = File::Spec->rel2abs($tmpdir);
64 my $uid = 500;
65 my $gid = 500;
66
67 # Make sure that, if we're running as root, that the home directory has
68 # permissions/privs set for the account we create
69 if ($< == 0) {
70 unless (chmod(0755, $home_dir)) {
71 die("Can't set perms on $home_dir to 0755: $!");
72 }
73
74 unless (chown($uid, $gid, $home_dir)) {
75 die("Can't set owner of $home_dir to $uid/$gid: $!");
76 }
77 }
78
79 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
80 '/bin/bash');
81 auth_group_write($auth_group_file, $group, $gid, $user);
82
83 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
84 if (open(my $fh, "> $test_file")) {
85 print $fh "ABCD" x 8192;
86 unless (close($fh)) {
87 die("Can't write $test_file: $!");
88 }
89
90 } else {
91 die("Can't open $test_file: $!");
92 }
93
94 my $min_port = 40100;
95 my $max_port = 40200;
96
97 my $config = {
98 PidFile => $pid_file,
99 ScoreboardFile => $scoreboard_file,
100 SystemLog => $log_file,
101 TraceLog => $log_file,
102 Trace => 'DEFAULT:0 data:10',
103
104 AuthUserFile => $auth_user_file,
105 AuthGroupFile => $auth_group_file,
106 AuthOrder => 'mod_auth_file.c',
107
108 PassivePorts => "$min_port $max_port",
109
110 IfModules => {
111 'mod_delay.c' => {
112 DelayEngine => 'off',
113 },
114 },
115 };
116
117 my ($port, $config_user, $config_group) = config_write($config_file, $config);
118
119 # Open pipes, for use between the parent and child processes. Specifically,
120 # the child will indicate when it's done with its test by writing a message
121 # to the parent.
122 my ($rfh, $wfh);
123 unless (pipe($rfh, $wfh)) {
124 die("Can't open pipe: $!");
125 }
126
127 my $ex;
128
129 # Fork child
130 $self->handle_sigchld();
131 defined(my $pid = fork()) or die("Can't fork: $!");
132 if ($pid) {
133 eval {
134 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
135 $client->login($user, $passwd);
136
137 my ($resp_code, $resp_msg) = $client->pasv();
138 $client->quit();
139
140 my $expected;
141
142 $expected = 227;
143 $self->assert($expected == $resp_code,
144 test_msg("Expected response code $expected, got $resp_code"));
145
146 $expected = '\(\d+,\d+,\d+,\d+,\d+,\d+\)';
147 $self->assert(qr/$expected/, $resp_msg,
148 test_msg("Expected response message '$expected', got '$resp_msg'"));
149
150 unless ($resp_msg =~ /\(\d+,\d+,\d+,\d+,(\d+),(\d+)\)/) {
151 die("Response '$resp_msg' does not match expected pattern");
152 }
153
154 my $pasv_port = ($1 * 256) + $2;
155
156 $self->assert($min_port <= $pasv_port && $max_port >= $pasv_port,
157 test_msg("Expected port from $min_port to $max_port, got $pasv_port"));
158 };
159
160 if ($@) {
161 $ex = $@;
162 }
163
164 $wfh->print("done\n");
165 $wfh->flush();
166
167 } else {
168 eval { server_wait($config_file, $rfh) };
169 if ($@) {
170 warn($@);
171 exit 1;
172 }
173
174 exit 0;
175 }
176
177 # Stop server
178 server_stop($pid_file);
179
180 $self->assert_child_ok($pid);
181
182 if ($ex) {
183 test_append_logfile($log_file, $ex);
184 unlink($log_file);
185
186 die($ex);
187 }
188
189 unlink($log_file);
190 }
191
192 sub pasv_ports_global {
193 my $self = shift;
194 my $tmpdir = $self->{tmpdir};
195
196 my $config_file = "$tmpdir/config.conf";
197 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
198 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
199
200 my $log_file = test_get_logfile();
201
202 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
203 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
204
205 my $user = 'proftpd';
206 my $passwd = 'test';
207 my $group = 'ftpd';
208 my $home_dir = File::Spec->rel2abs($tmpdir);
209 my $uid = 500;
210 my $gid = 500;
211
212 # Make sure that, if we're running as root, that the home directory has
213 # permissions/privs set for the account we create
214 if ($< == 0) {
215 unless (chmod(0755, $home_dir)) {
216 die("Can't set perms on $home_dir to 0755: $!");
217 }
218
219 unless (chown($uid, $gid, $home_dir)) {
220 die("Can't set owner of $home_dir to $uid/$gid: $!");
221 }
222 }
223
224 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
225 '/bin/bash');
226 auth_group_write($auth_group_file, $group, $gid, $user);
227
228 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
229 if (open(my $fh, "> $test_file")) {
230 print $fh "ABCD" x 8192;
231 unless (close($fh)) {
232 die("Can't write $test_file: $!");
233 }
234
235 } else {
236 die("Can't open $test_file: $!");
237 }
238
239 my $min_port = 40100;
240 my $max_port = 40200;
241
242 my $config = {
243 PidFile => $pid_file,
244 ScoreboardFile => $scoreboard_file,
245 SystemLog => $log_file,
246 TraceLog => $log_file,
247 Trace => 'DEFAULT:0 data:10',
248
249 AuthUserFile => $auth_user_file,
250 AuthGroupFile => $auth_group_file,
251 AuthOrder => 'mod_auth_file.c',
252
253 Global => {
254 PassivePorts => "$min_port $max_port",
255 },
256
257 IfModules => {
258 'mod_delay.c' => {
259 DelayEngine => 'off',
260 },
261 },
262 };
263
264 my ($port, $config_user, $config_group) = config_write($config_file, $config);
265
266 # Open pipes, for use between the parent and child processes. Specifically,
267 # the child will indicate when it's done with its test by writing a message
268 # to the parent.
269 my ($rfh, $wfh);
270 unless (pipe($rfh, $wfh)) {
271 die("Can't open pipe: $!");
272 }
273
274 my $ex;
275
276 # Fork child
277 $self->handle_sigchld();
278 defined(my $pid = fork()) or die("Can't fork: $!");
279 if ($pid) {
280 eval {
281 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
282 $client->login($user, $passwd);
283
284 my ($resp_code, $resp_msg) = $client->pasv();
285 $client->quit();
286
287 my $expected;
288
289 $expected = 227;
290 $self->assert($expected == $resp_code,
291 test_msg("Expected response code $expected, got $resp_code"));
292
293 $expected = '\(\d+,\d+,\d+,\d+,\d+,\d+\)';
294 $self->assert(qr/$expected/, $resp_msg,
295 test_msg("Expected response message '$expected', got '$resp_msg'"));
296
297 unless ($resp_msg =~ /\(\d+,\d+,\d+,\d+,(\d+),(\d+)\)/) {
298 die("Response '$resp_msg' does not match expected pattern");
299 }
300
301 my $pasv_port = ($1 * 256) + $2;
302
303 $self->assert($min_port <= $pasv_port && $max_port >= $pasv_port,
304 test_msg("Expected port from $min_port to $max_port, got $pasv_port"));
305 };
306
307 if ($@) {
308 $ex = $@;
309 }
310
311 $wfh->print("done\n");
312 $wfh->flush();
313
314 } else {
315 eval { server_wait($config_file, $rfh) };
316 if ($@) {
317 warn($@);
318 exit 1;
319 }
320
321 exit 0;
322 }
323
324 # Stop server
325 server_stop($pid_file);
326
327 $self->assert_child_ok($pid);
328
329 if ($ex) {
330 test_append_logfile($log_file, $ex);
331 unlink($log_file);
332
333 die($ex);
334 }
335
336 unlink($log_file);
337 }
338
339 sub pasv_ports_vhost {
340 my $self = shift;
341 my $tmpdir = $self->{tmpdir};
342 my $setup = test_setup($tmpdir, 'config');
343
344 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
345 if (open(my $fh, "> $test_file")) {
346 print $fh "ABCD" x 8192;
347 unless (close($fh)) {
348 die("Can't write $test_file: $!");
349 }
350
351 # Make sure that, if we're running as root, that the test file has
352 # permissions/privs set for the account we create
353 if ($< == 0) {
354 unless (chown($setup->{uid}, $setup->{gid}, $test_file)) {
355 die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!");
356 }
357 }
358
359 } else {
360 die("Can't open $test_file: $!");
361 }
362
363 my $min_port = 40100;
364 my $max_port = 40200;
365
366 my $config = {
367 PidFile => $setup->{pid_file},
368 ScoreboardFile => $setup->{scoreboard_file},
369 SystemLog => $setup->{log_file},
370 TraceLog => $setup->{log_file},
371 Trace => 'DEFAULT:0 binding:20 data:10',
372
373 AuthUserFile => $setup->{auth_user_file},
374 AuthGroupFile => $setup->{auth_group_file},
375 AuthOrder => 'mod_auth_file.c',
376
377 Port => '0',
378 SocketBindTight => 'on',
379
380 IfModules => {
381 'mod_delay.c' => {
382 DelayEngine => 'off',
383 },
384 },
385 };
386
387 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
388 $config);
389
390 my $vhost_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
391
392 if (open(my $fh, ">> $setup->{config_file}")) {
393 print $fh <<EOC;
394 <VirtualHost 127.0.0.1>
395 ServerName "Vhost"
396 Port $vhost_port
397 AuthUserFile $setup->{auth_user_file}
398 AuthGroupFile $setup->{auth_group_file}
399 AuthOrder mod_auth_file.c
400 PassivePorts $min_port $max_port
401 </VirtualHost>
402 EOC
403 unless (close($fh)) {
404 die("Can't write $setup->{config_file}: $!");
405 }
406
407 } else {
408 die("Can't open $setup->{config_file}: $!");
409 }
410
411 # Open pipes, for use between the parent and child processes. Specifically,
412 # the child will indicate when it's done with its test by writing a message
413 # to the parent.
414 my ($rfh, $wfh);
415 unless (pipe($rfh, $wfh)) {
416 die("Can't open pipe: $!");
417 }
418
419 my $ex;
420
421 # Fork child
422 $self->handle_sigchld();
423 defined(my $pid = fork()) or die("Can't fork: $!");
424 if ($pid) {
425 eval {
426 # Allow server to start up
427 sleep(2);
428
429 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $vhost_port);
430 $client->login($setup->{user}, $setup->{passwd});
431
432 my ($resp_code, $resp_msg) = $client->pasv();
433 $client->quit();
434
435 my $expected = 227;
436 $self->assert($expected == $resp_code,
437 test_msg("Expected response code $expected, got $resp_code"));
438
439 $expected = '\(\d+,\d+,\d+,\d+,\d+,\d+\)';
440 $self->assert(qr/$expected/, $resp_msg,
441 test_msg("Expected response message '$expected', got '$resp_msg'"));
442
443 unless ($resp_msg =~ /\(\d+,\d+,\d+,\d+,(\d+),(\d+)\)/) {
444 die("Response '$resp_msg' does not match expected pattern");
445 }
446
447 my $pasv_port = ($1 * 256) + $2;
448
449 $self->assert($min_port <= $pasv_port && $max_port >= $pasv_port,
450 test_msg("Expected port from $min_port to $max_port, got $pasv_port"));
451 };
452 if ($@) {
453 $ex = $@;
454 }
455
456 $wfh->print("done\n");
457 $wfh->flush();
458
459 } else {
460 eval { server_wait($setup->{config_file}, $rfh) };
461 if ($@) {
462 warn($@);
463 exit 1;
464 }
465
466 exit 0;
467 }
468
469 # Stop server
470 server_stop($setup->{pid_file});
471 $self->assert_child_ok($pid);
472
473 test_cleanup($setup->{log_file}, $ex);
474 }
475
476 sub pasv_ports_random_issue1396 {
477 my $self = shift;
478 my $tmpdir = $self->{tmpdir};
479 my $setup = test_setup($tmpdir, 'config');
480
481 my $min_port = 30000;
482 my $max_port = 31000;
483
484 my $config = {
485 PidFile => $setup->{pid_file},
486 ScoreboardFile => $setup->{scoreboard_file},
487 SystemLog => $setup->{log_file},
488 TraceLog => $setup->{log_file},
489 Trace => 'DEFAULT:0 data:10',
490
491 AuthUserFile => $setup->{auth_user_file},
492 AuthGroupFile => $setup->{auth_group_file},
493 AuthOrder => 'mod_auth_file.c',
494
495 PassivePorts => "$min_port $max_port",
496
497 IfModules => {
498 'mod_delay.c' => {
499 DelayEngine => 'off',
500 },
501 },
502 };
503
504 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
505 $config);
506
507 # Open pipes, for use between the parent and child processes. Specifically,
508 # the child will indicate when it's done with its test by writing a message
509 # to the parent.
510 my ($rfh, $wfh);
511 unless (pipe($rfh, $wfh)) {
512 die("Can't open pipe: $!");
513 }
514
515 my $ex;
516
517 # Fork child
518 $self->handle_sigchld();
519 defined(my $pid = fork()) or die("Can't fork: $!");
520 if ($pid) {
521 eval {
522 my $first_pasv_port;
523
524 for (my $i = 0; $i < 3; $i++) {
525 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
526 $client->login($setup->{user}, $setup->{passwd});
527
528 my ($resp_code, $resp_msg) = $client->pasv();
529 $client->quit();
530
531 if ($ENV{TEST_VERBOSE}) {
532 print STDERR "# $resp_code $resp_msg\n";
533 }
534
535 my $expected = 227;
536 $self->assert($expected == $resp_code,
537 test_msg("Expected response code $expected, got $resp_code"));
538
539 $expected = '\(\d+,\d+,\d+,\d+,\d+,\d+\)';
540 $self->assert(qr/$expected/, $resp_msg,
541 test_msg("Expected response message '$expected', got '$resp_msg'"));
542
543 unless ($resp_msg =~ /\(\d+,\d+,\d+,\d+,(\d+),(\d+)\)/) {
544 die("Response '$resp_msg' does not match expected pattern");
545 }
546
547 my $pasv_port = ($1 * 256) + $2;
548 $self->assert($min_port <= $pasv_port && $max_port >= $pasv_port,
549 test_msg("Expected port from $min_port to $max_port, got $pasv_port"));
550
551 if (defined($first_pasv_port)) {
552 $self->assert($pasv_port != $first_pasv_port,
553 test_msg("Expected different port than $first_pasv_port for subsequent sessions ($pasv_port)"));
554
555 } else {
556 $first_pasv_port = $pasv_port;
557 }
558 }
559 };
560 if ($@) {
561 $ex = $@;
562 }
563
564 $wfh->print("done\n");
565 $wfh->flush();
566
567 } else {
568 eval { server_wait($setup->{config_file}, $rfh) };
569 if ($@) {
570 warn($@);
571 exit 1;
572 }
573
574 exit 0;
575 }
576
577 # Stop server
578 server_stop($setup->{pid_file});
579 $self->assert_child_ok($pid);
580
581 test_cleanup($setup->{log_file}, $ex);
582 }
583
584 1;