Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/FTPAccess/RETR.pm

raw
1 package ProFTPD::Tests::Config::FTPAccess::RETR;
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 ftpaccess_retr_bug2038 => {
20 order => ++$order,
21 test_class => [qw(bug forking)],
22 },
23
24 ftpaccess_anon_retr_bug2038 => {
25 order => ++$order,
26 test_class => [qw(bug forking rootprivs)],
27 },
28
29 ftpaccess_anon_retr_bug2461 => {
30 order => ++$order,
31 test_class => [qw(bug forking rootprivs)],
32 },
33
34 };
35
36 sub new {
37 return shift()->SUPER::new(@_);
38 }
39
40 sub list_tests {
41 return testsuite_get_runnable_tests($TESTS);
42 }
43
44 sub ftpaccess_retr_bug2038 {
45 my $self = shift;
46 my $tmpdir = $self->{tmpdir};
47
48 my $config_file = "$tmpdir/ftpaccess.conf";
49 my $pid_file = File::Spec->rel2abs("$tmpdir/ftpaccess.pid");
50 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ftpaccess.scoreboard");
51
52 my $log_file = File::Spec->rel2abs('tests.log');
53
54 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ftpaccess.passwd");
55 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ftpaccess.group");
56
57 my $user = 'proftpd';
58 my $passwd = 'test';
59 my $home_dir = File::Spec->rel2abs($tmpdir);
60 my $uid = 500;
61 my $gid = 500;
62
63 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
64 mkpath($sub_dir);
65
66 my $fh;
67
68 # Write a .ftpaccess file in the sub dir which denies all access
69 my $ftpaccess_file = File::Spec->rel2abs("$tmpdir/foo/.ftpaccess");
70 if (open($fh, "> $ftpaccess_file")) {
71 print $fh <<EOF;
72 <Limit RETR>
73 DenyAll
74 </Limit>
75 EOF
76
77 unless (close($fh)) {
78 die("Can't write $ftpaccess_file: $!");
79 }
80
81 } else {
82 die("Can't open $ftpaccess_file: $!");
83 }
84
85 # Next, write a test file in the sub dir.
86 my $test_file = File::Spec->rel2abs("$tmpdir/foo/test.txt");
87 if (open($fh, "> $test_file")) {
88 print $fh "Hello, World!\n";
89
90 unless (close($fh)) {
91 die("Can't write $test_file: $!");
92 }
93
94 } else {
95 die("Can't open $test_file: $!");
96 }
97
98 # Make sure that, if we're running as root, that the home directory has
99 # permissions/privs set for the account we create
100 if ($< == 0) {
101 unless (chmod(0755, $home_dir, $sub_dir)) {
102 die("Can't set perms on $home_dir, $sub_dir to 0755: $!");
103 }
104
105 unless (chown($uid, $gid, $home_dir, $sub_dir)) {
106 die("Can't set owner of $home_dir, $sub_dir to $uid/$gid: $!");
107 }
108 }
109
110 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
111 '/bin/bash');
112 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
113
114 my $config = {
115 PidFile => $pid_file,
116 ScoreboardFile => $scoreboard_file,
117 SystemLog => $log_file,
118
119 AuthUserFile => $auth_user_file,
120 AuthGroupFile => $auth_group_file,
121
122 AllowOverride => 'on',
123
124 IfModules => {
125 'mod_delay.c' => {
126 DelayEngine => 'off',
127 },
128 },
129 };
130
131 my ($port, $config_user, $config_group) = config_write($config_file, $config);
132
133 # Open pipes, for use between the parent and child processes. Specifically,
134 # the child will indicate when it's done with its test by writing a message
135 # to the parent.
136 my ($rfh, $wfh);
137 unless (pipe($rfh, $wfh)) {
138 die("Can't open pipe: $!");
139 }
140
141 my $ex;
142
143 # Fork child
144 $self->handle_sigchld();
145 defined(my $pid = fork()) or die("Can't fork: $!");
146 if ($pid) {
147 eval {
148 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
149 $client->login($user, $passwd);
150 $client->cwd($sub_dir);
151
152 my ($resp_code, $resp_msg);
153
154 my $conn = $client->retr_raw("test.txt");
155 if ($conn) {
156 die("RETR test.txt succeeded unexpectedly");
157 }
158
159 $resp_code = $client->response_code();
160 $resp_msg = $client->response_msg();
161
162 my $expected;
163
164 $expected = 550;
165 $self->assert($expected == $resp_code,
166 test_msg("Expected $expected, got $resp_code"));
167
168 $expected = "test.txt: Operation not permitted";
169 $self->assert($expected eq $resp_msg,
170 test_msg("Expected '$expected', got '$resp_msg'"));
171
172 # Now download the file again, only this time using the full path
173 $conn = $client->retr_raw($test_file);
174 if ($conn) {
175 die("RETR $test_file succeeded unexpectedly");
176 }
177
178 $resp_code = $client->response_code();
179 $resp_msg = $client->response_msg();
180
181 $expected = 550;
182 $self->assert($expected == $resp_code,
183 test_msg("Expected $expected, got $resp_code"));
184
185 $expected = "$test_file: Operation not permitted";
186 $self->assert($expected eq $resp_msg,
187 test_msg("Expected '$expected', got '$resp_msg'"));
188
189 # Now prove that the failures are caused by the .ftpaccess file
190 # by deleting that file.
191 unlink($ftpaccess_file);
192
193 $conn = $client->retr_raw("test.txt");
194 unless ($conn) {
195 die("Failed to RETR test.txt: " . $client->response_code() . " " .
196 $client->response_msg());
197 }
198
199 my $buf;
200 $conn->read($buf, 8192, 30);
201 eval { $conn->close() };
202
203 $resp_code = $client->response_code();
204 $resp_msg = $client->response_msg();
205
206 $expected = 226;
207 $self->assert($expected == $resp_code,
208 test_msg("Expected $expected, got $resp_code"));
209
210 $expected = "Transfer complete";
211 $self->assert($expected eq $resp_msg,
212 test_msg("Expected '$expected', got '$resp_msg'"));
213
214 $conn = $client->retr_raw($test_file);
215 unless ($conn) {
216 die("Failed to RETR $test_file: " . $client->response_code() . " " .
217 $client->response_msg());
218 }
219
220 $conn->read($buf, 8192, 30);
221 eval { $conn->close() };
222
223 $resp_code = $client->response_code();
224 $resp_msg = $client->response_msg();
225
226 $expected = 226;
227 $self->assert($expected == $resp_code,
228 test_msg("Expected $expected, got $resp_code"));
229
230 $expected = "Transfer complete";
231 $self->assert($expected eq $resp_msg,
232 test_msg("Expected '$expected', got '$resp_msg'"));
233 };
234
235 if ($@) {
236 $ex = $@;
237 }
238
239 $wfh->print("done\n");
240 $wfh->flush();
241
242 } else {
243 eval { server_wait($config_file, $rfh) };
244 if ($@) {
245 warn($@);
246 exit 1;
247 }
248
249 exit 0;
250 }
251
252 # Stop server
253 server_stop($pid_file);
254
255 $self->assert_child_ok($pid);
256
257 if ($ex) {
258 die($ex);
259 }
260
261 unlink($log_file);
262 }
263
264 sub ftpaccess_anon_retr_bug2038 {
265 my $self = shift;
266 my $tmpdir = $self->{tmpdir};
267
268 my $config_file = "$tmpdir/ftpaccess.conf";
269 my $pid_file = File::Spec->rel2abs("$tmpdir/ftpaccess.pid");
270 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ftpaccess.scoreboard");
271
272 my $log_file = File::Spec->rel2abs('tests.log');
273
274 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ftpaccess.passwd");
275 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ftpaccess.group");
276
277 my ($config_user, $config_group) = config_get_identity();
278
279 my $anon_dir = File::Spec->rel2abs($tmpdir);
280
281 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
282 mkpath($sub_dir);
283
284 my $fh;
285
286 # Write a .ftpaccess file in the sub dir which denies all access
287 my $ftpaccess_file = File::Spec->rel2abs("$tmpdir/foo/.ftpaccess");
288 if (open($fh, "> $ftpaccess_file")) {
289 print $fh <<EOF;
290 <Limit RETR>
291 DenyAll
292 </Limit>
293 EOF
294
295 unless (close($fh)) {
296 die("Can't write $ftpaccess_file: $!");
297 }
298
299 } else {
300 die("Can't open $ftpaccess_file: $!");
301 }
302
303 # Next, write a test file in the sub dir.
304 my $test_file = File::Spec->rel2abs("$tmpdir/foo/test.txt");
305 if (open($fh, "> $test_file")) {
306 print $fh "Hello, World!\n";
307
308 unless (close($fh)) {
309 die("Can't write $test_file: $!");
310 }
311
312 } else {
313 die("Can't open $test_file: $!");
314 }
315
316 # Make a version of the path that the client would see.
317 my $anon_test_file = "/foo/test.txt";
318
319 auth_user_write($auth_user_file, $config_user, 'foo', 500, 500, '/tmp',
320 '/bin/bash');
321 auth_group_write($auth_group_file, $config_group, 500, $config_user);
322
323 my $config = {
324 PidFile => $pid_file,
325 ScoreboardFile => $scoreboard_file,
326 SystemLog => $log_file,
327
328 AuthUserFile => $auth_user_file,
329 AuthGroupFile => $auth_group_file,
330 AllowOverride => 'on',
331
332 Anonymous => {
333 $anon_dir => {
334 User => $config_user,
335 Group => $config_group,
336 UserAlias => "anonymous $config_user",
337 RequireValidShell => 'off',
338 },
339 },
340
341 IfModules => {
342 'mod_delay.c' => {
343 DelayEngine => 'off',
344 },
345 },
346 };
347
348 my $port;
349 ($port, $config_user, $config_group) = config_write($config_file, $config);
350
351 # Open pipes, for use between the parent and child processes. Specifically,
352 # the child will indicate when it's done with its test by writing a message
353 # to the parent.
354 my ($rfh, $wfh);
355 unless (pipe($rfh, $wfh)) {
356 die("Can't open pipe: $!");
357 }
358
359 my $ex;
360
361 # Fork child
362 $self->handle_sigchld();
363 defined(my $pid = fork()) or die("Can't fork: $!");
364 if ($pid) {
365 eval {
366 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
367 $client->login('anonymous', '[email protected]');
368 $client->cwd("foo");
369
370 my ($resp_code, $resp_msg);
371
372 my $conn = $client->retr_raw("test.txt");
373 if ($conn) {
374 die("RETR test.txt succeeded unexpectedly");
375 }
376
377 $resp_code = $client->response_code();
378 $resp_msg = $client->response_msg();
379
380 my $expected;
381
382 $expected = 550;
383 $self->assert($expected == $resp_code,
384 test_msg("Expected $expected, got $resp_code"));
385
386 $expected = "test.txt: Operation not permitted";
387 $self->assert($expected eq $resp_msg,
388 test_msg("Expected '$expected', got '$resp_msg'"));
389
390 # Now download the file again, only this time using a full path
391 $conn = $client->retr_raw($anon_test_file);
392 if ($conn) {
393 die("RETR $anon_test_file succeeded unexpectedly");
394 }
395
396 $resp_code = $client->response_code();
397 $resp_msg = $client->response_msg();
398
399 $expected = 550;
400 $self->assert($expected == $resp_code,
401 test_msg("Expected $expected, got $resp_code"));
402
403 $expected = "$anon_test_file: Operation not permitted";
404 $self->assert($expected eq $resp_msg,
405 test_msg("Expected '$expected', got '$resp_msg'"));
406
407 # Now prove that the failures are caused by the .ftpaccess file
408 # by deleting that file.
409 unlink($ftpaccess_file);
410
411 $conn = $client->retr_raw("test.txt");
412 unless ($conn) {
413 die("Failed to RETR test.txt: " . $client->response_code() . " " .
414 $client->response_msg());
415 }
416
417 my $buf;
418 $conn->read($buf, 8192, 30);
419 eval { $conn->close() };
420
421 $resp_code = $client->response_code();
422 $resp_msg = $client->response_msg();
423
424 $expected = 226;
425 $self->assert($expected == $resp_code,
426 test_msg("Expected $expected, got $resp_code"));
427
428 $expected = "Transfer complete";
429 $self->assert($expected eq $resp_msg,
430 test_msg("Expected '$expected', got '$resp_msg'"));
431
432 $conn = $client->retr_raw($anon_test_file);
433 unless ($conn) {
434 die("Failed to RETR $anon_test_file: " . $client->response_code() .
435 " " . $client->response_msg());
436 }
437
438 $conn->read($buf, 8192, 30);
439 eval { $conn->close() };
440
441 $resp_code = $client->response_code();
442 $resp_msg = $client->response_msg();
443
444 $expected = 226;
445 $self->assert($expected == $resp_code,
446 test_msg("Expected $expected, got $resp_code"));
447
448 $expected = "Transfer complete";
449 $self->assert($expected eq $resp_msg,
450 test_msg("Expected '$expected', got '$resp_msg'"));
451 };
452
453 if ($@) {
454 $ex = $@;
455 }
456
457 $wfh->print("done\n");
458 $wfh->flush();
459
460 } else {
461 eval { server_wait($config_file, $rfh) };
462 if ($@) {
463 warn($@);
464 exit 1;
465 }
466
467 exit 0;
468 }
469
470 # Stop server
471 server_stop($pid_file);
472
473 $self->assert_child_ok($pid);
474
475 if ($ex) {
476 die($ex);
477 }
478
479 unlink($log_file);
480 }
481
482 sub ftpaccess_anon_retr_bug2461 {
483 my $self = shift;
484 my $tmpdir = $self->{tmpdir};
485
486 my $config_file = "$tmpdir/ftpaccess.conf";
487 my $pid_file = File::Spec->rel2abs("$tmpdir/ftpaccess.pid");
488 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ftpaccess.scoreboard");
489
490 my $log_file = File::Spec->rel2abs('tests.log');
491
492 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ftpaccess.passwd");
493 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ftpaccess.group");
494
495 my ($config_user, $config_group) = config_get_identity();
496
497 my $anon_dir = File::Spec->rel2abs($tmpdir);
498
499 my $fh;
500
501 # Write a .ftpaccess file in the sub dir which denies all access
502 my $ftpaccess_file = File::Spec->rel2abs("$tmpdir/.ftpaccess");
503 if (open($fh, "> $ftpaccess_file")) {
504 print $fh <<EOF;
505 <Limit READ>
506 Allow from 127.0.0.2
507 DenyAll
508 </Limit>
509 EOF
510
511 unless (close($fh)) {
512 die("Can't write $ftpaccess_file: $!");
513 }
514
515 } else {
516 die("Can't open $ftpaccess_file: $!");
517 }
518
519 # Next, write a test file in the anon dir.
520 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
521 if (open($fh, "> $test_file")) {
522 print $fh "Hello, World!\n";
523
524 unless (close($fh)) {
525 die("Can't write $test_file: $!");
526 }
527
528 } else {
529 die("Can't open $test_file: $!");
530 }
531
532 # Make a version of the path that the client would see.
533 my $anon_test_file = "/test.txt";
534
535 auth_user_write($auth_user_file, $config_user, 'foo', 500, 500, '/tmp',
536 '/bin/bash');
537 auth_group_write($auth_group_file, $config_group, 500, $config_user);
538
539 my $config = {
540 PidFile => $pid_file,
541 ScoreboardFile => $scoreboard_file,
542 SystemLog => $log_file,
543
544 AuthUserFile => $auth_user_file,
545 AuthGroupFile => $auth_group_file,
546 AllowOverride => 'on',
547
548 Anonymous => {
549 $anon_dir => {
550 User => $config_user,
551 Group => $config_group,
552 UserAlias => "anonymous $config_user",
553 RequireValidShell => 'off',
554 },
555 },
556
557 IfModules => {
558 'mod_delay.c' => {
559 DelayEngine => 'off',
560 },
561 },
562 };
563
564 my $port;
565 ($port, $config_user, $config_group) = config_write($config_file, $config);
566
567 # Open pipes, for use between the parent and child processes. Specifically,
568 # the child will indicate when it's done with its test by writing a message
569 # to the parent.
570 my ($rfh, $wfh);
571 unless (pipe($rfh, $wfh)) {
572 die("Can't open pipe: $!");
573 }
574
575 my $ex;
576
577 # Fork child
578 $self->handle_sigchld();
579 defined(my $pid = fork()) or die("Can't fork: $!");
580 if ($pid) {
581 eval {
582 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
583 $client->login('anonymous', '[email protected]');
584
585 my ($resp_code, $resp_msg);
586
587 my $conn = $client->retr_raw("test.txt");
588 if ($conn) {
589 die("RETR test.txt succeeded unexpectedly");
590 }
591
592 $resp_code = $client->response_code();
593 $resp_msg = $client->response_msg();
594
595 my $expected;
596
597 $expected = 550;
598 $self->assert($expected == $resp_code,
599 test_msg("Expected $expected, got $resp_code"));
600
601 $expected = "test.txt: Operation not permitted";
602 $self->assert($expected eq $resp_msg,
603 test_msg("Expected '$expected', got '$resp_msg'"));
604
605 # Now do a directory listing...
606 $client->list();
607
608 # ...then try the download again
609 $conn = $client->retr_raw("test.txt");
610 if ($conn) {
611 die("RETR test.txt succeeded unexpectedly");
612 }
613
614 $resp_code = $client->response_code();
615 $resp_msg = $client->response_msg();
616
617 $expected = 550;
618 $self->assert($expected == $resp_code,
619 test_msg("Expected $expected, got $resp_code"));
620
621 $expected = "test.txt: Operation not permitted";
622 $self->assert($expected eq $resp_msg,
623 test_msg("Expected '$expected', got '$resp_msg'"));
624
625 # Now prove that the failures are caused by the .ftpaccess file
626 # by deleting that file.
627 unlink($ftpaccess_file);
628
629 $conn = $client->retr_raw("test.txt");
630 unless ($conn) {
631 die("Failed to RETR test.txt: " . $client->response_code() . " " .
632 $client->response_msg());
633 }
634
635 my $buf;
636 $conn->read($buf, 8192, 30);
637 eval { $conn->close() };
638
639 $resp_code = $client->response_code();
640 $resp_msg = $client->response_msg();
641
642 $expected = 226;
643 $self->assert($expected == $resp_code,
644 test_msg("Expected $expected, got $resp_code"));
645
646 $expected = "Transfer complete";
647 $self->assert($expected eq $resp_msg,
648 test_msg("Expected '$expected', got '$resp_msg'"));
649 };
650
651 if ($@) {
652 $ex = $@;
653 }
654
655 $wfh->print("done\n");
656 $wfh->flush();
657
658 } else {
659 eval { server_wait($config_file, $rfh) };
660 if ($@) {
661 warn($@);
662 exit 1;
663 }
664
665 exit 0;
666 }
667
668 # Stop server
669 server_stop($pid_file);
670
671 $self->assert_child_ok($pid);
672
673 if ($ex) {
674 die($ex);
675 }
676
677 unlink($log_file);
678 }
679
680 1;