Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/NLST.pm

raw
1 package ProFTPD::Tests::Commands::NLST;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Cwd;
8 use File::Path qw(mkpath);
9 use File::Spec;
10 use IO::Handle;
11
12 use ProFTPD::TestSuite::FTP;
13 use ProFTPD::TestSuite::Utils qw(:auth :config :features :running :test :testsuite);
14
15 $| = 1;
16
17 my $order = 0;
18
19 my $TESTS = {
20 nlst_ok_raw_active => {
21 order => ++$order,
22 test_class => [qw(forking)],
23 },
24
25 nlst_ok_raw_passive => {
26 order => ++$order,
27 test_class => [qw(forking)],
28 },
29
30 nlst_ok_file => {
31 order => ++$order,
32 test_class => [qw(forking)],
33 },
34
35 nlst_ok_dir => {
36 order => ++$order,
37 test_class => [qw(forking)],
38 },
39
40 nlst_ok_symlink => {
41 order => ++$order,
42 test_class => [qw(forking)],
43 },
44
45 nlst_ok_symlink_chrooted_bug4219 => {
46 order => ++$order,
47 test_class => [qw(bug forking rootprivs)],
48 },
49
50 nlst_ok_no_path => {
51 order => ++$order,
52 test_class => [qw(forking)],
53 },
54
55 nlst_ok_glob_file => {
56 order => ++$order,
57 test_class => [qw(forking)],
58 },
59
60 nlst_ok_glob_dir_bug4084 => {
61 order => ++$order,
62 test_class => [qw(bug forking)],
63 },
64
65 nlst_ok_glob_dir_with_recursion_bug4084 => {
66 order => ++$order,
67 test_class => [qw(bug forking)],
68 },
69
70 nlst_ok_glob_limited_dir => {
71 order => ++$order,
72 test_class => [qw(bug forking)],
73 },
74
75 nlst_fails_login_required => {
76 order => ++$order,
77 test_class => [qw(forking)],
78 },
79
80 nlst_fails_enoent => {
81 order => ++$order,
82 test_class => [qw(forking)],
83 },
84
85 nlst_fails_enoent_glob => {
86 order => ++$order,
87 test_class => [qw(forking)],
88 },
89
90 nlst_fails_eperm => {
91 order => ++$order,
92 test_class => [qw(forking)],
93 },
94
95 nlst_bug2821 => {
96 order => ++$order,
97 test_class => [qw(bug forking slow)],
98 },
99
100 nlst_nonascii_chars_bug3032 => {
101 order => ++$order,
102 test_class => [qw(bug forking)],
103 },
104
105 nlst_leading_whitespace_bug3268 => {
106 order => ++$order,
107 test_class => [qw(bug forking)],
108 },
109
110 nlst_leading_whitespace_with_opts_bug3268 => {
111 order => ++$order,
112 test_class => [qw(bug forking)],
113 },
114
115 nlst_leading_whitespace_with_strict_opts_bug3268 => {
116 order => ++$order,
117 test_class => [qw(bug forking)],
118 },
119
120 nlst_symlink_bug3254 => {
121 order => ++$order,
122 test_class => [qw(bug forking rootprivs)],
123 },
124
125 nlst_dash_filename_bug3476 => {
126 order => ++$order,
127 test_class => [qw(bug forking)],
128 },
129
130 nlst_opt_noerrorifabsent_bug3506 => {
131 order => ++$order,
132 test_class => [qw(bug forking)],
133 },
134
135 nlst_trailing_slashes_bug2457 => {
136 order => ++$order,
137 test_class => [qw(bug forking)],
138 },
139
140 nlst_rel_path_bug2496 => {
141 order => ++$order,
142 test_class => [qw(bug forking)],
143 },
144
145 nlst_rel_path_chrooted_bug2496 => {
146 order => ++$order,
147 test_class => [qw(bug forking rootprivs)],
148 },
149
150 nlst_parent_dir_bug4011 => {
151 order => ++$order,
152 test_class => [qw(bug forking)],
153 },
154
155 nlst_opt_a_root_dir_bug4069 => {
156 order => ++$order,
157 test_class => [qw(bug forking)],
158 },
159
160 nlst_opt_1_with_chroot => {
161 order => ++$order,
162 test_class => [qw(forking rootprivs)],
163 },
164
165 nlst_glob_with_rel_path_issue1325 => {
166 order => ++$order,
167 test_class => [qw(bug forking)],
168 },
169
170 nlst_glob_with_rel_path_dotdir_issue1325 => {
171 order => ++$order,
172 test_class => [qw(bug forking)],
173 },
174
175 };
176
177 sub new {
178 return shift()->SUPER::new(@_);
179 }
180
181 sub list_tests {
182 return testsuite_get_runnable_tests($TESTS);
183 }
184
185 sub nlst_ok_raw_active {
186 my $self = shift;
187 my $tmpdir = $self->{tmpdir};
188 my $setup = test_setup($tmpdir, 'cmds');
189
190 my $config = {
191 PidFile => $setup->{pid_file},
192 ScoreboardFile => $setup->{scoreboard_file},
193 SystemLog => $setup->{log_file},
194
195 AuthUserFile => $setup->{auth_user_file},
196 AuthGroupFile => $setup->{auth_group_file},
197 AuthOrder => 'mod_auth_file.c',
198
199 IfModules => {
200 'mod_delay.c' => {
201 DelayEngine => 'off',
202 },
203 },
204 };
205
206 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
207 $config);
208
209 # Open pipes, for use between the parent and child processes. Specifically,
210 # the child will indicate when it's done with its test by writing a message
211 # to the parent.
212 my ($rfh, $wfh);
213 unless (pipe($rfh, $wfh)) {
214 die("Can't open pipe: $!");
215 }
216
217 my $ex;
218
219 # Fork child
220 $self->handle_sigchld();
221 defined(my $pid = fork()) or die("Can't fork: $!");
222 if ($pid) {
223 eval {
224 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
225 $client->login($setup->{user}, $setup->{passwd});
226
227 my $conn = $client->nlst_raw();
228 unless ($conn) {
229 die("Failed to NLST: " . $client->response_code() . " " .
230 $client->response_msg());
231 }
232
233 my $buf;
234 $conn->read($buf, 8192, 25);
235 eval { $conn->close() };
236
237 my $resp_code = $client->response_code();
238 my $resp_msg = $client->response_msg();
239 $client->quit();
240
241 $self->assert_transfer_ok($resp_code, $resp_msg);
242
243 # We have to be careful of the fact that readdir returns directory
244 # entries in an unordered fashion.
245 my $res = {};
246 my $names = [split(/\n/, $buf)];
247 foreach my $name (@$names) {
248 $res->{$name} = 1;
249 }
250
251 my $expected = {
252 'cmds.conf' => 1,
253 'cmds.group' => 1,
254 'cmds.passwd' => 1,
255 'cmds.pid' => 1,
256 'cmds.scoreboard' => 1,
257 'cmds.scoreboard.lck' => 1,
258 };
259
260 my $ok = 1;
261 my $mismatch;
262 foreach my $name (keys(%$res)) {
263 unless (defined($expected->{$name})) {
264 $mismatch = $name;
265 $ok = 0;
266 last;
267 }
268 }
269
270 unless ($ok) {
271 die("Unexpected name '$mismatch' appeared in NLST data")
272 }
273 };
274
275 if ($@) {
276 $ex = $@;
277 }
278
279 $wfh->print("done\n");
280 $wfh->flush();
281
282 } else {
283 eval { server_wait($setup->{config_file}, $rfh) };
284 if ($@) {
285 warn($@);
286 exit 1;
287 }
288
289 exit 0;
290 }
291
292 # Stop server
293 server_stop($setup->{pid_file});
294 $self->assert_child_ok($pid);
295
296 test_cleanup($setup->{log_file}, $ex);
297 }
298
299 sub nlst_ok_raw_passive {
300 my $self = shift;
301 my $tmpdir = $self->{tmpdir};
302 my $setup = test_setup($tmpdir, 'cmds');
303
304 my $config = {
305 PidFile => $setup->{pid_file},
306 ScoreboardFile => $setup->{scoreboard_file},
307 SystemLog => $setup->{log_file},
308
309 AuthUserFile => $setup->{auth_user_file},
310 AuthGroupFile => $setup->{auth_group_file},
311 AuthOrder => 'mod_auth_file.c',
312
313 IfModules => {
314 'mod_delay.c' => {
315 DelayEngine => 'off',
316 },
317 },
318 };
319
320 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
321 $config);
322
323 # Open pipes, for use between the parent and child processes. Specifically,
324 # the child will indicate when it's done with its test by writing a message
325 # to the parent.
326 my ($rfh, $wfh);
327 unless (pipe($rfh, $wfh)) {
328 die("Can't open pipe: $!");
329 }
330
331 my $ex;
332
333 # Fork child
334 $self->handle_sigchld();
335 defined(my $pid = fork()) or die("Can't fork: $!");
336 if ($pid) {
337 eval {
338 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
339 $client->login($setup->{user}, $setup->{passwd});
340
341 my $conn = $client->nlst_raw();
342 unless ($conn) {
343 die("Failed to NLST: " . $client->response_code() . " " .
344 $client->response_msg());
345 }
346
347 my $buf = '';
348
349 my $info;
350 while ($conn->read($info, 8192, 25)) {
351 $buf .= $info;
352 }
353 eval { $conn->close() };
354
355 my $resp_code = $client->response_code();
356 my $resp_msg = $client->response_msg();
357 $client->quit();
358
359 $self->assert_transfer_ok($resp_code, $resp_msg);
360
361 # We have to be careful of the fact that readdir returns directory
362 # entries in an unordered fashion.
363 my $res = {};
364 my $names = [split(/\n/, $buf)];
365 foreach my $name (@$names) {
366 $res->{$name} = 1;
367 }
368
369 my $expected = {
370 'cmds.conf' => 1,
371 'cmds.group' => 1,
372 'cmds.passwd' => 1,
373 'cmds.pid' => 1,
374 'cmds.scoreboard' => 1,
375 'cmds.scoreboard.lck' => 1,
376 };
377
378 my $ok = 1;
379 my $mismatch;
380 foreach my $name (keys(%$res)) {
381 unless (defined($expected->{$name})) {
382 $mismatch = $name;
383 $ok = 0;
384 last;
385 }
386 }
387
388 unless ($ok) {
389 die("Unexpected name '$mismatch' appeared in NLST data")
390 }
391 };
392 if ($@) {
393 $ex = $@;
394 }
395
396 $wfh->print("done\n");
397 $wfh->flush();
398
399 } else {
400 eval { server_wait($setup->{config_file}, $rfh) };
401 if ($@) {
402 warn($@);
403 exit 1;
404 }
405
406 exit 0;
407 }
408
409 # Stop server
410 server_stop($setup->{pid_file});
411 $self->assert_child_ok($pid);
412
413 test_cleanup($setup->{log_file}, $ex);
414 }
415
416 sub nlst_ok_file {
417 my $self = shift;
418 my $tmpdir = $self->{tmpdir};
419 my $setup = test_setup($tmpdir, 'cmds');
420
421 my $test_file = File::Spec->rel2abs($setup->{config_file});
422
423 my $config = {
424 PidFile => $setup->{pid_file},
425 ScoreboardFile => $setup->{scoreboard_file},
426 SystemLog => $setup->{log_file},
427
428 AuthUserFile => $setup->{auth_user_file},
429 AuthGroupFile => $setup->{auth_group_file},
430 AuthOrder => 'mod_auth_file.c',
431
432 IfModules => {
433 'mod_delay.c' => {
434 DelayEngine => 'off',
435 },
436 },
437 };
438
439 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
440 $config);
441
442 # Open pipes, for use between the parent and child processes. Specifically,
443 # the child will indicate when it's done with its test by writing a message
444 # to the parent.
445 my ($rfh, $wfh);
446 unless (pipe($rfh, $wfh)) {
447 die("Can't open pipe: $!");
448 }
449
450 my $ex;
451
452 # Fork child
453 $self->handle_sigchld();
454 defined(my $pid = fork()) or die("Can't fork: $!");
455 if ($pid) {
456 eval {
457 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
458 $client->login($setup->{user}, $setup->{passwd});
459
460 my $conn = $client->nlst_raw($test_file);
461 unless ($conn) {
462 die("NLST failed: " . $client->response_code() . " " .
463 $client->response_msg());
464 }
465
466 my $buf;
467 $conn->read($buf, 8192, 25);
468 eval { $conn->close() };
469
470 my $resp_code = $client->response_code();
471 my $resp_msg = $client->response_msg();
472 $client->quit();
473
474 $self->assert_transfer_ok($resp_code, $resp_msg);
475
476 my $res = {};
477 my $names = [split(/\n/, $buf)];
478 foreach my $name (@$names) {
479 $res->{$name} = 1;
480 }
481
482 my $count = scalar(keys(%$res));
483 unless ($count == 1) {
484 die("NLST returned wrong number of entries (expected 1, got $count)");
485 }
486
487 unless (defined($res->{$test_file})) {
488 die("NLST failed to return $test_file");
489 }
490 };
491 if ($@) {
492 $ex = $@;
493 }
494
495 $wfh->print("done\n");
496 $wfh->flush();
497
498 } else {
499 eval { server_wait($setup->{config_file}, $rfh) };
500 if ($@) {
501 warn($@);
502 exit 1;
503 }
504
505 exit 0;
506 }
507
508 # Stop server
509 server_stop($setup->{pid_file});
510 $self->assert_child_ok($pid);
511
512 test_cleanup($setup->{log_file}, $ex);
513 }
514
515 sub nlst_ok_dir {
516 my $self = shift;
517 my $tmpdir = $self->{tmpdir};
518 my $setup = test_setup($tmpdir, 'cmds');
519
520 my $config = {
521 PidFile => $setup->{pid_file},
522 ScoreboardFile => $setup->{scoreboard_file},
523 SystemLog => $setup->{log_file},
524
525 AuthUserFile => $setup->{auth_user_file},
526 AuthGroupFile => $setup->{auth_group_file},
527 AuthOrder => 'mod_auth_file.c',
528
529 IfModules => {
530 'mod_delay.c' => {
531 DelayEngine => 'off',
532 },
533 },
534 };
535
536 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
537 $config);
538
539 # Open pipes, for use between the parent and child processes. Specifically,
540 # the child will indicate when it's done with its test by writing a message
541 # to the parent.
542 my ($rfh, $wfh);
543 unless (pipe($rfh, $wfh)) {
544 die("Can't open pipe: $!");
545 }
546
547 my $ex;
548
549 # Fork child
550 $self->handle_sigchld();
551 defined(my $pid = fork()) or die("Can't fork: $!");
552 if ($pid) {
553 eval {
554 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
555 $client->login($setup->{user}, $setup->{passwd});
556
557 my $conn = $client->nlst_raw($setup->{home_dir});
558 unless ($conn) {
559 die("NLST failed: " . $client->response_code() . " " .
560 $client->response_msg());
561 }
562
563 my $buf;
564 $conn->read($buf, 8192, 25);
565 eval { $conn->close() };
566
567 my $resp_code = $client->response_code();
568 my $resp_msg = $client->response_msg();
569 $client->quit();
570
571 $self->assert_transfer_ok($resp_code, $resp_msg);
572
573 my $res = {};
574 my $names = [split(/\n/, $buf)];
575 foreach my $name (@$names) {
576 $res->{$name} = 1;
577 }
578
579 my $count = scalar(keys(%$res));
580 my $expected = 6;
581 unless ($count == $expected) {
582 die("NLST returned wrong number of entries (expected $expected, got $count)");
583 }
584 };
585 if ($@) {
586 $ex = $@;
587 }
588
589 $wfh->print("done\n");
590 $wfh->flush();
591
592 } else {
593 eval { server_wait($setup->{config_file}, $rfh) };
594 if ($@) {
595 warn($@);
596 exit 1;
597 }
598
599 exit 0;
600 }
601
602 # Stop server
603 server_stop($setup->{pid_file});
604 $self->assert_child_ok($pid);
605
606 test_cleanup($setup->{log_file}, $ex);
607 }
608
609 sub nlst_ok_symlink {
610 my $self = shift;
611 my $tmpdir = $self->{tmpdir};
612 my $setup = test_setup($tmpdir, 'cmds');
613
614 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
615 mkpath($test_dir);
616
617 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
618 if (open(my $fh, "> $test_file")) {
619 unless (close($fh)) {
620 die("Can't write $test_file: $!");
621 }
622
623 } else {
624 die("Can't open $test_file: $!");
625 }
626
627 # Change to the test directory in order to create a relative path in the
628 # symlink we need
629
630 my $cwd = getcwd();
631 unless (chdir($test_dir)) {
632 die("Can't chdir to $test_dir: $!");
633 }
634
635 unless (symlink('./test.txt', './test.lnk')) {
636 die("Can't symlink 'test.lnk' to './test.txt': $!");
637 }
638
639 unless (chdir($cwd)) {
640 die("Can't chdir to $cwd: $!");
641 }
642
643 if ($< == 0) {
644 unless (chmod(0755, $test_dir)) {
645 die("Can't set perms on $test_dir to 0755: $!");
646 }
647
648 unless (chown($setup->{uid}, $setup->{gid}, $test_dir)) {
649 die("Can't set owner of $test_dir to $setup->{uid}/$setup->{gid}: $!");
650 }
651 }
652
653 my $config = {
654 PidFile => $setup->{pid_file},
655 ScoreboardFile => $setup->{scoreboard_file},
656 SystemLog => $setup->{log_file},
657
658 AuthUserFile => $setup->{auth_user_file},
659 AuthGroupFile => $setup->{auth_group_file},
660 AuthOrder => 'mod_auth_file.c',
661
662 IfModules => {
663 'mod_delay.c' => {
664 DelayEngine => 'off',
665 },
666 },
667 };
668
669 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
670 $config);
671
672 # Open pipes, for use between the parent and child processes. Specifically,
673 # the child will indicate when it's done with its test by writing a message
674 # to the parent.
675 my ($rfh, $wfh);
676 unless (pipe($rfh, $wfh)) {
677 die("Can't open pipe: $!");
678 }
679
680 my $ex;
681
682 # Fork child
683 $self->handle_sigchld();
684 defined(my $pid = fork()) or die("Can't fork: $!");
685 if ($pid) {
686 eval {
687 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
688 $client->login($setup->{user}, $setup->{passwd});
689
690 my $path = 'test.d/test.lnk';
691 my $conn = $client->nlst_raw($path);
692 unless ($conn) {
693 die("NLST failed: " . $client->response_code() . " " .
694 $client->response_msg());
695 }
696
697 my $buf;
698 $conn->read($buf, 8192, 25);
699 eval { $conn->close() };
700
701 if ($ENV{TEST_VERBOSE}) {
702 print STDERR "# Response:\n$buf\n";
703 }
704
705 my $resp_code = $client->response_code();
706 my $resp_msg = $client->response_msg();
707 $client->quit();
708
709 $self->assert_transfer_ok($resp_code, $resp_msg);
710
711 my $res = {};
712 my $names = [split(/(\r)?\n/, $buf)];
713 foreach my $name (@$names) {
714 $res->{$name} = 1;
715 }
716
717 my $count = scalar(keys(%$res));
718 unless ($count == 1) {
719 die("NLST returned wrong number of entries (expected 1, got $count)");
720 }
721
722 unless (defined($res->{$path})) {
723 die("NLST failed to return $path");
724 }
725 };
726 if ($@) {
727 $ex = $@;
728 }
729
730 $wfh->print("done\n");
731 $wfh->flush();
732
733 } else {
734 eval { server_wait($setup->{config_file}, $rfh) };
735 if ($@) {
736 warn($@);
737 exit 1;
738 }
739
740 exit 0;
741 }
742
743 # Stop server
744 server_stop($setup->{pid_file});
745 $self->assert_child_ok($pid);
746
747 test_cleanup($setup->{log_file}, $ex);
748 }
749
750 sub nlst_ok_symlink_chrooted_bug4219 {
751 my $self = shift;
752 my $tmpdir = $self->{tmpdir};
753 my $setup = test_setup($tmpdir, 'cmds');
754
755 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
756 mkpath($test_dir);
757
758 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
759 if (open(my $fh, "> $test_file")) {
760 unless (close($fh)) {
761 die("Can't write $test_file: $!");
762 }
763
764 } else {
765 die("Can't open $test_file: $!");
766 }
767
768 # Change to the test directory in order to create a relative path in the
769 # symlink we need
770
771 my $cwd = getcwd();
772 unless (chdir($test_dir)) {
773 die("Can't chdir to $test_dir: $!");
774 }
775
776 unless (symlink('./test.txt', './test.lnk')) {
777 die("Can't symlink 'test.lnk' to './test.txt': $!");
778 }
779
780 unless (chdir($cwd)) {
781 die("Can't chdir to $cwd: $!");
782 }
783
784 if ($< == 0) {
785 unless (chmod(0755, $test_dir)) {
786 die("Can't set perms on $test_dir to 0755: $!");
787 }
788
789 unless (chown($setup->{uid}, $setup->{gid}, $test_dir)) {
790 die("Can't set owner of $test_dir to $setup->{uid}/$setup->{gid}: $!");
791 }
792 }
793
794 my $config = {
795 PidFile => $setup->{pid_file},
796 ScoreboardFile => $setup->{scoreboard_file},
797 SystemLog => $setup->{log_file},
798
799 AuthUserFile => $setup->{auth_user_file},
800 AuthGroupFile => $setup->{auth_group_file},
801 AuthOrder => 'mod_auth_file.c',
802
803 DefaultRoot => '~',
804
805 IfModules => {
806 'mod_delay.c' => {
807 DelayEngine => 'off',
808 },
809 },
810 };
811
812 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
813 $config);
814
815 # Open pipes, for use between the parent and child processes. Specifically,
816 # the child will indicate when it's done with its test by writing a message
817 # to the parent.
818 my ($rfh, $wfh);
819 unless (pipe($rfh, $wfh)) {
820 die("Can't open pipe: $!");
821 }
822
823 my $ex;
824
825 # Fork child
826 $self->handle_sigchld();
827 defined(my $pid = fork()) or die("Can't fork: $!");
828 if ($pid) {
829 eval {
830 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
831 $client->login($setup->{user}, $setup->{passwd});
832
833 my $path = 'test.d/test.lnk';
834 my $conn = $client->nlst_raw($path);
835 unless ($conn) {
836 die("NLST failed: " . $client->response_code() . " " .
837 $client->response_msg());
838 }
839
840 my $buf;
841 $conn->read($buf, 8192, 25);
842 eval { $conn->close() };
843
844 if ($ENV{TEST_VERBOSE}) {
845 print STDERR "# Response:\n$buf\n";
846 }
847
848 my $resp_code = $client->response_code();
849 my $resp_msg = $client->response_msg();
850 $client->quit();
851
852 $self->assert_transfer_ok($resp_code, $resp_msg);
853
854 my $res = {};
855 my $names = [split(/(\r)?\n/, $buf)];
856 foreach my $name (@$names) {
857 $res->{$name} = 1;
858 }
859
860 my $count = scalar(keys(%$res));
861 unless ($count == 1) {
862 die("NLST returned wrong number of entries (expected 1, got $count)");
863 }
864
865 unless (defined($res->{$path})) {
866 die("NLST failed to return $path");
867 }
868 };
869 if ($@) {
870 $ex = $@;
871 }
872
873 $wfh->print("done\n");
874 $wfh->flush();
875
876 } else {
877 eval { server_wait($setup->{config_file}, $rfh) };
878 if ($@) {
879 warn($@);
880 exit 1;
881 }
882
883 exit 0;
884 }
885
886 # Stop server
887 server_stop($setup->{pid_file});
888 $self->assert_child_ok($pid);
889
890 test_cleanup($setup->{log_file}, $ex);
891 }
892
893 sub nlst_ok_no_path {
894 my $self = shift;
895 my $tmpdir = $self->{tmpdir};
896
897 my $config_file = "$tmpdir/cmds.conf";
898 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
899 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
900
901 my $log_file = test_get_logfile();
902
903 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
904 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
905
906 my $user = 'proftpd';
907 my $passwd = 'test';
908 my $group = 'ftpd';
909 my $home_dir = File::Spec->rel2abs($tmpdir);
910 my $uid = 500;
911 my $gid = 500;
912
913 # Make sure that, if we're running as root, that the home directory has
914 # permissions/privs set for the account we create
915 if ($< == 0) {
916 unless (chmod(0755, $home_dir)) {
917 die("Can't set perms on $home_dir to 0755: $!");
918 }
919
920 unless (chown($uid, $gid, $home_dir)) {
921 die("Can't set owner of $home_dir to $uid/$gid: $!");
922 }
923 }
924
925 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
926 '/bin/bash');
927 auth_group_write($auth_group_file, $group, $gid, $user);
928
929 my $config = {
930 PidFile => $pid_file,
931 ScoreboardFile => $scoreboard_file,
932 SystemLog => $log_file,
933
934 AuthUserFile => $auth_user_file,
935 AuthGroupFile => $auth_group_file,
936 AuthOrder => 'mod_auth_file.c',
937
938 IfModules => {
939 'mod_delay.c' => {
940 DelayEngine => 'off',
941 },
942 },
943 };
944
945 my ($port, $config_user, $config_group) = config_write($config_file, $config);
946
947 # Open pipes, for use between the parent and child processes. Specifically,
948 # the child will indicate when it's done with its test by writing a message
949 # to the parent.
950 my ($rfh, $wfh);
951 unless (pipe($rfh, $wfh)) {
952 die("Can't open pipe: $!");
953 }
954
955 my $ex;
956
957 # Fork child
958 $self->handle_sigchld();
959 defined(my $pid = fork()) or die("Can't fork: $!");
960 if ($pid) {
961 eval {
962 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
963
964 $client->login($user, $passwd);
965
966 my $conn = $client->nlst_raw();
967 unless ($conn) {
968 die("NLST failed: " . $client->response_code() . " " .
969 $client->response_msg());
970 }
971
972 my $buf;
973 $conn->read($buf, 8192, 25);
974 eval { $conn->close() };
975
976 my $res = {};
977 my $names = [split(/\n/, $buf)];
978 foreach my $name (@$names) {
979 $res->{$name} = 1;
980 }
981
982 my $count = scalar(keys(%$res));
983 my $expected = 6;
984 unless ($count == $expected) {
985 die("NLST returned wrong number of entries (expected $expected, got $count)");
986 }
987 };
988
989 if ($@) {
990 $ex = $@;
991 }
992
993 print $wfh "done\n";
994
995 } else {
996 eval { server_wait($config_file, $rfh) };
997 if ($@) {
998 warn($@);
999 exit 1;
1000 }
1001
1002 exit 0;
1003 }
1004
1005 # Stop server
1006 server_stop($pid_file);
1007
1008 $self->assert_child_ok($pid);
1009
1010 if ($ex) {
1011 test_append_logfile($log_file, $ex);
1012 unlink($log_file);
1013
1014 die($ex);
1015 }
1016
1017 unlink($log_file);
1018 }
1019
1020 sub nlst_ok_glob_file {
1021 my $self = shift;
1022 my $tmpdir = $self->{tmpdir};
1023 my $setup = test_setup($tmpdir, 'cmds');
1024
1025 my $config = {
1026 PidFile => $setup->{pid_file},
1027 ScoreboardFile => $setup->{scoreboard_file},
1028 SystemLog => $setup->{log_file},
1029
1030 AuthUserFile => $setup->{auth_user_file},
1031 AuthGroupFile => $setup->{auth_group_file},
1032 AuthOrder => 'mod_auth_file.c',
1033
1034 IfModules => {
1035 'mod_delay.c' => {
1036 DelayEngine => 'off',
1037 },
1038 },
1039 };
1040
1041 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1042 $config);
1043
1044 # Open pipes, for use between the parent and child processes. Specifically,
1045 # the child will indicate when it's done with its test by writing a message
1046 # to the parent.
1047 my ($rfh, $wfh);
1048 unless (pipe($rfh, $wfh)) {
1049 die("Can't open pipe: $!");
1050 }
1051
1052 my $ex;
1053
1054 # Fork child
1055 $self->handle_sigchld();
1056 defined(my $pid = fork()) or die("Can't fork: $!");
1057 if ($pid) {
1058 eval {
1059 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1060 $client->login($setup->{user}, $setup->{passwd});
1061
1062 my $conn = $client->nlst_raw("*.conf");
1063 unless ($conn) {
1064 die("NLST failed: " . $client->response_code() . " " .
1065 $client->response_msg());
1066 }
1067
1068 my $buf;
1069 $conn->read($buf, 8192, 25);
1070 eval { $conn->close() };
1071
1072 my $res = {};
1073 my $names = [split(/\n/, $buf)];
1074 foreach my $name (@$names) {
1075 $res->{$name} = 1;
1076 }
1077
1078 my $count = scalar(keys(%$res));
1079 unless ($count == 1) {
1080 die("NLST returned wrong number of entries (expected 1, got $count)");
1081 }
1082
1083 my $test_file = 'cmds.conf';
1084 unless (defined($res->{$test_file})) {
1085 die("NLST failed to return $test_file");
1086 }
1087 };
1088
1089 if ($@) {
1090 $ex = $@;
1091 }
1092
1093 $wfh->print("done\n");
1094 $wfh->flush();
1095
1096 } else {
1097 eval { server_wait($setup->{config_file}, $rfh) };
1098 if ($@) {
1099 warn($@);
1100 exit 1;
1101 }
1102
1103 exit 0;
1104 }
1105
1106 # Stop server
1107 server_stop($setup->{pid_file});
1108
1109 $self->assert_child_ok($pid);
1110 test_cleanup($setup->{log_file}, $ex);
1111 }
1112
1113 sub nlst_ok_glob_dir_bug4084 {
1114 my $self = shift;
1115 my $tmpdir = $self->{tmpdir};
1116 my $setup = test_setup($tmpdir, 'cmds');
1117
1118 my $sub_dir = File::Spec->rel2abs("$tmpdir/test.d");
1119 mkpath($sub_dir);
1120
1121 my $sub_file = File::Spec->rel2abs("$sub_dir/test.dat");
1122 if (open(my $fh, "> $sub_file")) {
1123 close($fh);
1124
1125 } else {
1126 die("Can't open $sub_file: $!");
1127 }
1128
1129 my $config = {
1130 PidFile => $setup->{pid_file},
1131 ScoreboardFile => $setup->{scoreboard_file},
1132 SystemLog => $setup->{log_file},
1133
1134 AuthUserFile => $setup->{auth_user_file},
1135 AuthGroupFile => $setup->{auth_group_file},
1136 AuthOrder => 'mod_auth_file.c',
1137
1138 IfModules => {
1139 'mod_delay.c' => {
1140 DelayEngine => 'off',
1141 },
1142 },
1143 };
1144
1145 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1146 $config);
1147
1148 # Open pipes, for use between the parent and child processes. Specifically,
1149 # the child will indicate when it's done with its test by writing a message
1150 # to the parent.
1151 my ($rfh, $wfh);
1152 unless (pipe($rfh, $wfh)) {
1153 die("Can't open pipe: $!");
1154 }
1155
1156 my $ex;
1157
1158 # Fork child
1159 $self->handle_sigchld();
1160 defined(my $pid = fork()) or die("Can't fork: $!");
1161 if ($pid) {
1162 eval {
1163 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1164 $client->login($setup->{user}, $setup->{passwd});
1165
1166 my $conn = $client->nlst_raw("*");
1167 unless ($conn) {
1168 die("NLST failed: " . $client->response_code() . " " .
1169 $client->response_msg());
1170 }
1171
1172 my $buf;
1173 $conn->read($buf, 8192, 25);
1174 eval { $conn->close() };
1175
1176 $client->quit();
1177
1178 my $res = {};
1179 my $names = [split(/\n/, $buf)];
1180 foreach my $name (@$names) {
1181 $res->{$name} = 1;
1182 }
1183
1184 my $expected = {
1185 'cmds.pid' => 1,
1186 'cmds.scoreboard' => 1,
1187 'cmds.scoreboard.lck' => 1,
1188 'cmds.group' => 1,
1189 'cmds.passwd' => 1,
1190 'cmds.conf' => 1,
1191 'test.d' => 1,
1192 };
1193
1194 my $ok = 1;
1195 my $mismatch;
1196 foreach my $name (keys(%$res)) {
1197 unless (defined($expected->{$name})) {
1198 $mismatch = $name;
1199 $ok = 0;
1200 last;
1201 }
1202 }
1203
1204 unless ($ok) {
1205 die("Unexpected name '$mismatch' appeared in NLST data")
1206 }
1207 };
1208
1209 if ($@) {
1210 $ex = $@;
1211 }
1212
1213 $wfh->print("done\n");
1214 $wfh->flush();
1215
1216 } else {
1217 eval { server_wait($setup->{config_file}, $rfh) };
1218 if ($@) {
1219 warn($@);
1220 exit 1;
1221 }
1222
1223 exit 0;
1224 }
1225
1226 # Stop server
1227 server_stop($setup->{pid_file});
1228
1229 $self->assert_child_ok($pid);
1230 test_cleanup($setup->{log_file}, $ex);
1231 }
1232
1233 sub nlst_ok_glob_dir_with_recursion_bug4084 {
1234 my $self = shift;
1235 my $tmpdir = $self->{tmpdir};
1236 my $setup = test_setup($tmpdir, 'cmds');
1237
1238 my $sub_dir = File::Spec->rel2abs("$tmpdir/test.d");
1239 mkpath($sub_dir);
1240
1241 my $sub_file = File::Spec->rel2abs("$sub_dir/test.dat");
1242 if (open(my $fh, "> $sub_file")) {
1243 close($fh);
1244
1245 } else {
1246 die("Can't open $sub_file: $!");
1247 }
1248
1249 my $config = {
1250 PidFile => $setup->{pid_file},
1251 ScoreboardFile => $setup->{scoreboard_file},
1252 SystemLog => $setup->{log_file},
1253
1254 AuthUserFile => $setup->{auth_user_file},
1255 AuthGroupFile => $setup->{auth_group_file},
1256 AuthOrder => 'mod_auth_file.c',
1257
1258 IfModules => {
1259 'mod_delay.c' => {
1260 DelayEngine => 'off',
1261 },
1262 },
1263 };
1264
1265 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1266 $config);
1267
1268 # Open pipes, for use between the parent and child processes. Specifically,
1269 # the child will indicate when it's done with its test by writing a message
1270 # to the parent.
1271 my ($rfh, $wfh);
1272 unless (pipe($rfh, $wfh)) {
1273 die("Can't open pipe: $!");
1274 }
1275
1276 my $ex;
1277
1278 # Fork child
1279 $self->handle_sigchld();
1280 defined(my $pid = fork()) or die("Can't fork: $!");
1281 if ($pid) {
1282 eval {
1283 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1284 $client->login($setup->{user}, $setup->{passwd});
1285
1286 my $conn = $client->nlst_raw("-R *");
1287 unless ($conn) {
1288 die("NLST failed: " . $client->response_code() . " " .
1289 $client->response_msg());
1290 }
1291
1292 my $buf;
1293 $conn->read($buf, 8192, 25);
1294 eval { $conn->close() };
1295
1296 $client->quit();
1297
1298 my $res = {};
1299 my $names = [split(/\n/, $buf)];
1300 foreach my $name (@$names) {
1301 $res->{$name} = 1;
1302 }
1303
1304 my $expected = {
1305 'cmds.pid' => 1,
1306 'cmds.scoreboard' => 1,
1307 'cmds.scoreboard.lck' => 1,
1308 'cmds.group' => 1,
1309 'cmds.passwd' => 1,
1310 'cmds.conf' => 1,
1311 'test.d/test.dat' => 1,
1312 };
1313
1314 my $ok = 1;
1315 my $mismatch;
1316 foreach my $name (keys(%$res)) {
1317 unless (defined($expected->{$name})) {
1318 $mismatch = $name;
1319 $ok = 0;
1320 last;
1321 }
1322 }
1323
1324 unless ($ok) {
1325 die("Unexpected name '$mismatch' appeared in NLST data")
1326 }
1327 };
1328
1329 if ($@) {
1330 $ex = $@;
1331 }
1332
1333 $wfh->print("done\n");
1334 $wfh->flush();
1335
1336 } else {
1337 eval { server_wait($setup->{config_file}, $rfh) };
1338 if ($@) {
1339 warn($@);
1340 exit 1;
1341 }
1342
1343 exit 0;
1344 }
1345
1346 # Stop server
1347 server_stop($setup->{pid_file});
1348
1349 $self->assert_child_ok($pid);
1350 test_cleanup($setup->{log_file}, $ex);
1351 }
1352
1353 sub nlst_ok_glob_limited_dir {
1354 my $self = shift;
1355 my $tmpdir = $self->{tmpdir};
1356 my $setup = test_setup($tmpdir, 'cmds');
1357
1358 my $sub_dir = File::Spec->rel2abs("$tmpdir/test.d");
1359 mkpath($sub_dir);
1360
1361 my $sub_file = File::Spec->rel2abs("$sub_dir/test.dat");
1362 if (open(my $fh, "> $sub_file")) {
1363 close($fh);
1364
1365 } else {
1366 die("Can't open $sub_file: $!");
1367 }
1368
1369 my $config = {
1370 PidFile => $setup->{pid_file},
1371 ScoreboardFile => $setup->{scoreboard_file},
1372 SystemLog => $setup->{log_file},
1373
1374 AuthUserFile => $setup->{auth_user_file},
1375 AuthGroupFile => $setup->{auth_group_file},
1376 AuthOrder => 'mod_auth_file.c',
1377
1378 IfModules => {
1379 'mod_delay.c' => {
1380 DelayEngine => 'off',
1381 },
1382 },
1383 };
1384
1385 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1386 $config);
1387 if (open(my $fh, ">> $setup->{config_file}")) {
1388 my $config_dir = $sub_dir;
1389
1390 if ($^O eq 'darwin') {
1391 # MacOSX-specific hack
1392 $config_dir = '/private' . $config_dir;
1393 }
1394
1395 print $fh <<EOC;
1396 <Directory $config_dir>
1397 <Limit NLST>
1398 DenyAll
1399 </Limit>
1400 </Directory>
1401 EOC
1402 unless (close($fh)) {
1403 die("Can't write $setup->{config_file}: $!");
1404 }
1405
1406 } else {
1407 die("Can't open $setup->{config_file}: $!");
1408 }
1409
1410 # Open pipes, for use between the parent and child processes. Specifically,
1411 # the child will indicate when it's done with its test by writing a message
1412 # to the parent.
1413 my ($rfh, $wfh);
1414 unless (pipe($rfh, $wfh)) {
1415 die("Can't open pipe: $!");
1416 }
1417
1418 my $ex;
1419
1420 # Fork child
1421 $self->handle_sigchld();
1422 defined(my $pid = fork()) or die("Can't fork: $!");
1423 if ($pid) {
1424 eval {
1425 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1426 $client->login($setup->{user}, $setup->{passwd});
1427
1428 my $conn = $client->nlst_raw("*");
1429 unless ($conn) {
1430 die("NLST failed: " . $client->response_code() . " " .
1431 $client->response_msg());
1432 }
1433
1434 my $buf;
1435 $conn->read($buf, 8192, 25);
1436 eval { $conn->close() };
1437
1438 $client->quit();
1439
1440 if ($ENV{TEST_VERBOSE}) {
1441 print STDERR "# data:\n$buf";
1442 }
1443
1444 my $res = {};
1445 my $names = [split(/\n/, $buf)];
1446 foreach my $name (@$names) {
1447 $res->{$name} = 1;
1448 }
1449
1450 my $expected = {
1451 'cmds.pid' => 1,
1452 'cmds.scoreboard' => 1,
1453 'cmds.scoreboard.lck' => 1,
1454 'cmds.group' => 1,
1455 'cmds.passwd' => 1,
1456 'cmds.conf' => 1,
1457 };
1458
1459 my $ok = 1;
1460 my $mismatch;
1461 foreach my $name (keys(%$res)) {
1462 unless (defined($expected->{$name})) {
1463 $mismatch = $name;
1464 $ok = 0;
1465 last;
1466 }
1467 }
1468
1469 unless ($ok) {
1470 die("Unexpected name '$mismatch' appeared in NLST data")
1471 }
1472 };
1473
1474 if ($@) {
1475 $ex = $@;
1476 }
1477
1478 $wfh->print("done\n");
1479 $wfh->flush();
1480
1481 } else {
1482 eval { server_wait($setup->{config_file}, $rfh) };
1483 if ($@) {
1484 warn($@);
1485 exit 1;
1486 }
1487
1488 exit 0;
1489 }
1490
1491 # Stop server
1492 server_stop($setup->{pid_file});
1493
1494 $self->assert_child_ok($pid);
1495 test_cleanup($setup->{log_file}, $ex);
1496 }
1497
1498 sub nlst_fails_login_required {
1499 my $self = shift;
1500 my $tmpdir = $self->{tmpdir};
1501
1502 my $config_file = "$tmpdir/cmds.conf";
1503 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
1504 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
1505
1506 my $log_file = test_get_logfile();
1507
1508 my $config = {
1509 PidFile => $pid_file,
1510 ScoreboardFile => $scoreboard_file,
1511 SystemLog => $log_file,
1512
1513 IfModules => {
1514 'mod_delay.c' => {
1515 DelayEngine => 'off',
1516 },
1517 },
1518 };
1519
1520 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1521
1522 # Open pipes, for use between the parent and child processes. Specifically,
1523 # the child will indicate when it's done with its test by writing a message
1524 # to the parent.
1525 my ($rfh, $wfh);
1526 unless (pipe($rfh, $wfh)) {
1527 die("Can't open pipe: $!");
1528 }
1529
1530 my $ex;
1531
1532 # Fork child
1533 $self->handle_sigchld();
1534 defined(my $pid = fork()) or die("Can't fork: $!");
1535 if ($pid) {
1536 eval {
1537 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1538
1539 my ($resp_code, $resp_msg);
1540 eval { ($resp_code, $resp_msg) = $client->nlst() };
1541 unless ($@) {
1542 die("NLST succeeded unexpectedly");
1543
1544 } else {
1545 $resp_code = $client->response_code();
1546 $resp_msg = $client->response_msg();
1547 }
1548
1549 my $expected;
1550
1551 $expected = 530;
1552 $self->assert($expected == $resp_code,
1553 test_msg("Expected $expected, got $resp_code"));
1554
1555 $expected = "Please login with USER and PASS";
1556 $self->assert($expected eq $resp_msg,
1557 test_msg("Expected '$expected', got '$resp_msg'"));
1558 };
1559
1560 if ($@) {
1561 $ex = $@;
1562 }
1563
1564 $wfh->print("done\n");
1565 $wfh->flush();
1566
1567 } else {
1568 eval { server_wait($config_file, $rfh) };
1569 if ($@) {
1570 warn($@);
1571 exit 1;
1572 }
1573
1574 exit 0;
1575 }
1576
1577 # Stop server
1578 server_stop($pid_file);
1579
1580 $self->assert_child_ok($pid);
1581
1582 if ($ex) {
1583 test_append_logfile($log_file, $ex);
1584 unlink($log_file);
1585
1586 die($ex);
1587 }
1588
1589 unlink($log_file);
1590 }
1591
1592 sub nlst_fails_enoent {
1593 my $self = shift;
1594 my $tmpdir = $self->{tmpdir};
1595
1596 my $config_file = "$tmpdir/cmds.conf";
1597 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
1598 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
1599
1600 my $log_file = test_get_logfile();
1601
1602 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
1603 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
1604
1605 my $user = 'proftpd';
1606 my $passwd = 'test';
1607 my $group = 'ftpd';
1608 my $home_dir = File::Spec->rel2abs($tmpdir);
1609 my $uid = 500;
1610 my $gid = 500;
1611
1612 # Make sure that, if we're running as root, that the home directory has
1613 # permissions/privs set for the account we create
1614 if ($< == 0) {
1615 unless (chmod(0755, $home_dir)) {
1616 die("Can't set perms on $home_dir to 0755: $!");
1617 }
1618
1619 unless (chown($uid, $gid, $home_dir)) {
1620 die("Can't set owner of $home_dir to $uid/$gid: $!");
1621 }
1622 }
1623
1624 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1625 '/bin/bash');
1626 auth_group_write($auth_group_file, $group, $gid, $user);
1627
1628 my $config = {
1629 PidFile => $pid_file,
1630 ScoreboardFile => $scoreboard_file,
1631 SystemLog => $log_file,
1632
1633 AuthUserFile => $auth_user_file,
1634 AuthGroupFile => $auth_group_file,
1635 AuthOrder => 'mod_auth_file.c',
1636
1637 IfModules => {
1638 'mod_delay.c' => {
1639 DelayEngine => 'off',
1640 },
1641 },
1642 };
1643
1644 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1645
1646 # Open pipes, for use between the parent and child processes. Specifically,
1647 # the child will indicate when it's done with its test by writing a message
1648 # to the parent.
1649 my ($rfh, $wfh);
1650 unless (pipe($rfh, $wfh)) {
1651 die("Can't open pipe: $!");
1652 }
1653
1654 my $ex;
1655
1656 # Fork child
1657 $self->handle_sigchld();
1658 defined(my $pid = fork()) or die("Can't fork: $!");
1659 if ($pid) {
1660 eval {
1661 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1662
1663 $client->login($user, $passwd);
1664
1665 my ($resp_code, $resp_msg);
1666 $client->port();
1667
1668 my $test_file = 'foo/bar/baz';
1669
1670 eval { ($resp_code, $resp_msg) = $client->nlst($test_file) };
1671 unless ($@) {
1672 die("NLST succeeded unexpectedly");
1673
1674 } else {
1675 $resp_code = $client->response_code();
1676 $resp_msg = $client->response_msg();
1677 }
1678
1679 my $expected;
1680
1681 $expected = 450;
1682 $self->assert($expected == $resp_code,
1683 test_msg("Expected $expected, got $resp_code"));
1684
1685 $expected = "$test_file: No such file or directory";
1686 $self->assert($expected eq $resp_msg,
1687 test_msg("Expected '$expected', got '$resp_msg'"));
1688 };
1689
1690 if ($@) {
1691 $ex = $@;
1692 }
1693
1694 $wfh->print("done\n");
1695 $wfh->flush();
1696
1697 } else {
1698 eval { server_wait($config_file, $rfh) };
1699 if ($@) {
1700 warn($@);
1701 exit 1;
1702 }
1703
1704 exit 0;
1705 }
1706
1707 # Stop server
1708 server_stop($pid_file);
1709
1710 $self->assert_child_ok($pid);
1711
1712 if ($ex) {
1713 test_append_logfile($log_file, $ex);
1714 unlink($log_file);
1715
1716 die($ex);
1717 }
1718
1719 unlink($log_file);
1720 }
1721
1722 sub nlst_fails_enoent_glob {
1723 my $self = shift;
1724 my $tmpdir = $self->{tmpdir};
1725
1726 my $config_file = "$tmpdir/cmds.conf";
1727 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
1728 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
1729
1730 my $log_file = test_get_logfile();
1731
1732 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
1733 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
1734
1735 my $user = 'proftpd';
1736 my $passwd = 'test';
1737 my $group = 'ftpd';
1738 my $home_dir = File::Spec->rel2abs($tmpdir);
1739 my $uid = 500;
1740 my $gid = 500;
1741
1742 # Make sure that, if we're running as root, that the home directory has
1743 # permissions/privs set for the account we create
1744 if ($< == 0) {
1745 unless (chmod(0755, $home_dir)) {
1746 die("Can't set perms on $home_dir to 0755: $!");
1747 }
1748
1749 unless (chown($uid, $gid, $home_dir)) {
1750 die("Can't set owner of $home_dir to $uid/$gid: $!");
1751 }
1752 }
1753
1754 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1755 '/bin/bash');
1756 auth_group_write($auth_group_file, $group, $gid, $user);
1757
1758 my $config = {
1759 PidFile => $pid_file,
1760 ScoreboardFile => $scoreboard_file,
1761 SystemLog => $log_file,
1762
1763 AuthUserFile => $auth_user_file,
1764 AuthGroupFile => $auth_group_file,
1765 AuthOrder => 'mod_auth_file.c',
1766
1767 IfModules => {
1768 'mod_delay.c' => {
1769 DelayEngine => 'off',
1770 },
1771 },
1772 };
1773
1774 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1775
1776 # Open pipes, for use between the parent and child processes. Specifically,
1777 # the child will indicate when it's done with its test by writing a message
1778 # to the parent.
1779 my ($rfh, $wfh);
1780 unless (pipe($rfh, $wfh)) {
1781 die("Can't open pipe: $!");
1782 }
1783
1784 my $ex;
1785
1786 # Fork child
1787 $self->handle_sigchld();
1788 defined(my $pid = fork()) or die("Can't fork: $!");
1789 if ($pid) {
1790 eval {
1791 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1792
1793 $client->login($user, $passwd);
1794 $client->port();
1795
1796 my $test_glob = '*foo';
1797
1798 my ($resp_code, $resp_msg);
1799 eval { ($resp_code, $resp_msg) = $client->nlst($test_glob) };
1800 unless ($@) {
1801 die("NLST succeeded unexpectedly");
1802
1803 } else {
1804 $resp_code = $client->response_code();
1805 $resp_msg = $client->response_msg();
1806 }
1807
1808 my $expected;
1809
1810 $expected = 450;
1811 $self->assert($expected == $resp_code,
1812 test_msg("Expected $expected, got $resp_code"));
1813
1814 $expected = "No files found";
1815 $self->assert($expected eq $resp_msg,
1816 test_msg("Expected '$expected', got '$resp_msg'"));
1817 };
1818
1819 if ($@) {
1820 $ex = $@;
1821 }
1822
1823 $wfh->print("done\n");
1824 $wfh->flush();
1825
1826 } else {
1827 eval { server_wait($config_file, $rfh) };
1828 if ($@) {
1829 warn($@);
1830 exit 1;
1831 }
1832
1833 exit 0;
1834 }
1835
1836 # Stop server
1837 server_stop($pid_file);
1838
1839 $self->assert_child_ok($pid);
1840
1841 if ($ex) {
1842 test_append_logfile($log_file, $ex);
1843 unlink($log_file);
1844
1845 die($ex);
1846 }
1847
1848 unlink($log_file);
1849 }
1850
1851 sub nlst_fails_eperm {
1852 my $self = shift;
1853 my $tmpdir = $self->{tmpdir};
1854
1855 my $config_file = "$tmpdir/cmds.conf";
1856 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
1857 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
1858
1859 my $log_file = test_get_logfile();
1860
1861 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
1862 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
1863
1864 my $user = 'proftpd';
1865 my $passwd = 'test';
1866 my $group = 'ftpd';
1867 my $home_dir = File::Spec->rel2abs($tmpdir);
1868 my $uid = 500;
1869 my $gid = 500;
1870
1871 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
1872 mkpath($sub_dir);
1873
1874 my $test_file = File::Spec->rel2abs("$tmpdir/foo/bar");
1875 if (open(my $fh, "> $test_file")) {
1876 close($fh);
1877
1878 } else {
1879 die("Can't open $test_file: $!");
1880 }
1881
1882 # Make sure that, if we're running as root, that the home directory has
1883 # permissions/privs set for the account we create
1884 if ($< == 0) {
1885 unless (chmod(0755, $home_dir)) {
1886 die("Can't set perms on $home_dir to 0755: $!");
1887 }
1888
1889 unless (chown($uid, $gid, $home_dir)) {
1890 die("Can't set owner of $home_dir to $uid/$gid: $!");
1891 }
1892 }
1893
1894 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1895 '/bin/bash');
1896 auth_group_write($auth_group_file, $group, $gid, $user);
1897
1898 my $config = {
1899 PidFile => $pid_file,
1900 ScoreboardFile => $scoreboard_file,
1901 SystemLog => $log_file,
1902
1903 AuthUserFile => $auth_user_file,
1904 AuthGroupFile => $auth_group_file,
1905 AuthOrder => 'mod_auth_file.c',
1906
1907 IfModules => {
1908 'mod_delay.c' => {
1909 DelayEngine => 'off',
1910 },
1911 },
1912 };
1913
1914 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1915
1916 # Open pipes, for use between the parent and child processes. Specifically,
1917 # the child will indicate when it's done with its test by writing a message
1918 # to the parent.
1919 my ($rfh, $wfh);
1920 unless (pipe($rfh, $wfh)) {
1921 die("Can't open pipe: $!");
1922 }
1923
1924 my $ex;
1925
1926 # Fork child
1927 $self->handle_sigchld();
1928 defined(my $pid = fork()) or die("Can't fork: $!");
1929 if ($pid) {
1930 eval {
1931 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1932
1933 $client->login($user, $passwd);
1934 $client->port();
1935
1936 chmod(0660, $sub_dir);
1937
1938 my ($resp_code, $resp_msg);
1939 eval { ($resp_code, $resp_msg) = $client->nlst($test_file) };
1940 unless ($@) {
1941 die("NLST succeeded unexpectedly");
1942
1943 } else {
1944 $resp_code = $client->response_code();
1945 $resp_msg = $client->response_msg();
1946 }
1947
1948 my $expected;
1949
1950 $expected = 450;
1951 $self->assert($expected == $resp_code,
1952 test_msg("Expected $expected, got $resp_code"));
1953
1954 $expected = "$test_file: Permission denied";
1955 $self->assert($expected eq $resp_msg,
1956 test_msg("Expected '$expected', got '$resp_msg'"));
1957 };
1958
1959 if ($@) {
1960 $ex = $@;
1961 }
1962
1963 $wfh->print("done\n");
1964 $wfh->flush();
1965
1966 } else {
1967 eval { server_wait($config_file, $rfh) };
1968 if ($@) {
1969 warn($@);
1970 exit 1;
1971 }
1972
1973 exit 0;
1974 }
1975
1976 # Stop server
1977 server_stop($pid_file);
1978
1979 $self->assert_child_ok($pid);
1980
1981 if ($ex) {
1982 test_append_logfile($log_file, $ex);
1983 unlink($log_file);
1984
1985 die($ex);
1986 }
1987
1988 unlink($log_file);
1989 }
1990
1991 sub nlst_bug2821 {
1992 my $self = shift;
1993 my $tmpdir = $self->{tmpdir};
1994
1995 my $config_file = "$tmpdir/cmds.conf";
1996 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
1997 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
1998
1999 my $log_file = test_get_logfile();
2000
2001 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
2002 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
2003
2004 my $user = 'proftpd';
2005 my $passwd = 'test';
2006 my $group = 'ftpd';
2007 my $home_dir = File::Spec->rel2abs($tmpdir);
2008 my $uid = 500;
2009 my $gid = 500;
2010
2011 # Make sure that, if we're running as root, that the home directory has
2012 # permissions/privs set for the account we create
2013 if ($< == 0) {
2014 unless (chmod(0755, $home_dir)) {
2015 die("Can't set perms on $home_dir to 0755: $!");
2016 }
2017
2018 unless (chown($uid, $gid, $home_dir)) {
2019 die("Can't set owner of $home_dir to $uid/$gid: $!");
2020 }
2021 }
2022
2023 # For this test, we need to create MANY (i.e. 150K) files in the
2024 # home directory.
2025 my $test_file_prefix = File::Spec->rel2abs("$tmpdir/test_");
2026
2027 my $count = 150000;
2028 print STDOUT "# Creating $count files in $tmpdir\n";
2029 for (my $i = 1; $i <= $count; $i++) {
2030 my $test_file = 'test_' . sprintf("%07s", $i);
2031 my $test_path = "$home_dir/$test_file";
2032
2033 if (open(my $fh, "> $test_path")) {
2034 close($fh);
2035
2036 } else {
2037 die("Can't open $test_path: $!");
2038 }
2039
2040 if ($i % 10000 == 0) {
2041 print STDOUT "# Created file $test_file\n";
2042 }
2043 }
2044
2045 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2046 '/bin/bash');
2047 auth_group_write($auth_group_file, $group, $gid, $user);
2048
2049 my $timeout = 600;
2050
2051 my $config = {
2052 PidFile => $pid_file,
2053 ScoreboardFile => $scoreboard_file,
2054 SystemLog => $log_file,
2055 TraceLog => $log_file,
2056 Trace => 'DEFAULT:10 fsio:0 lock:0',
2057
2058 AuthUserFile => $auth_user_file,
2059 AuthGroupFile => $auth_group_file,
2060 AuthOrder => 'mod_auth_file.c',
2061
2062 TimeoutIdle => $timeout + 15,
2063 TimeoutNoTransfer => $timeout + 15,
2064
2065 IfModules => {
2066 'mod_delay.c' => {
2067 DelayEngine => 'off',
2068 },
2069 },
2070 };
2071
2072 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2073
2074 # Open pipes, for use between the parent and child processes. Specifically,
2075 # the child will indicate when it's done with its test by writing a message
2076 # to the parent.
2077 my ($rfh, $wfh);
2078 unless (pipe($rfh, $wfh)) {
2079 die("Can't open pipe: $!");
2080 }
2081
2082 my $ex;
2083
2084 # Fork child
2085 $self->handle_sigchld();
2086 defined(my $pid = fork()) or die("Can't fork: $!");
2087 if ($pid) {
2088 eval {
2089 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2090
2091 $client->login($user, $passwd);
2092
2093 # One of the symptoms of Bug#2821 is that a LIST (or NLST) glob
2094 # which matches more than 99999 files will not receive the full
2095 # list.
2096 #
2097 # The issue turned out to be the MAX_RESULTS (now
2098 # PR_TUNABLE_GLOBBING_MAX_MATCHES) limit in lib/glibc-glob.c.
2099 # So now, we deliberate make sure that the number of files returned
2100 # is less than the number of potential matches.
2101
2102 my $conn = $client->nlst_raw("test_*");
2103 unless ($conn) {
2104 die("NLST failed: " . $client->response_code() . " " .
2105 $client->response_msg());
2106 }
2107
2108 my $buf;
2109 my $tmp;
2110 while ($conn->read($tmp, 32768, 25)) {
2111 $buf .= $tmp;
2112 }
2113 eval { $conn->close() };
2114 $client->quit();
2115
2116 my $res = {};
2117
2118 # Preallocate memory for the expected hash size, to help speed up the
2119 # evaluation of the results.
2120 keys(%$res) = $count;
2121
2122 my $lines = [split(/\n/, $buf)];
2123 foreach my $line (@$lines) {
2124 $res->{$line} = 1;
2125 }
2126
2127 my $list_count = scalar(keys(%$res));
2128 if ($list_count >= $count) {
2129 die("NLST returned wrong number of entries (expected less than $count, got $list_count); check the PR_TUNABLE_GLOBBING_MAX_MATCHES value");
2130 }
2131
2132 };
2133
2134 if ($@) {
2135 $ex = $@;
2136 }
2137
2138 $wfh->print("done\n");
2139 $wfh->flush();
2140
2141 } else {
2142 eval { server_wait($config_file, $rfh, $timeout) };
2143 if ($@) {
2144 warn($@);
2145 exit 1;
2146 }
2147
2148 exit 0;
2149 }
2150
2151 # Stop server
2152 server_stop($pid_file);
2153
2154 $self->assert_child_ok($pid);
2155
2156 if ($ex) {
2157 test_append_logfile($log_file, $ex);
2158 unlink($log_file);
2159
2160 die($ex);
2161 }
2162
2163 unlink($log_file);
2164 }
2165
2166 sub nlst_nonascii_chars_bug3032 {
2167 my $self = shift;
2168 my $tmpdir = $self->{tmpdir};
2169
2170 if (feature_have_feature_enabled('nls')) {
2171 # This test is only valid if NLS support is not enabled.
2172 return;
2173 }
2174
2175 my $config_file = "$tmpdir/cmds.conf";
2176 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
2177 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
2178
2179 my $log_file = test_get_logfile();
2180
2181 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
2182 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
2183
2184 my $user = 'proftpd';
2185 my $passwd = 'test';
2186 my $group = 'ftpd';
2187 my $home_dir = File::Spec->rel2abs($tmpdir);
2188 my $uid = 500;
2189 my $gid = 500;
2190
2191 # Make sure that, if we're running as root, that the home directory has
2192 # permissions/privs set for the account we create
2193 if ($< == 0) {
2194 unless (chmod(0755, $home_dir)) {
2195 die("Can't set perms on $home_dir to 0755: $!");
2196 }
2197
2198 unless (chown($uid, $gid, $home_dir)) {
2199 die("Can't set owner of $home_dir to $uid/$gid: $!");
2200 }
2201 }
2202
2203 my $test_file = File::Spec->rel2abs("$tmpdir/test\b");
2204 if (open(my $fh, "> $test_file")) {
2205 close($fh);
2206
2207 } else {
2208 die("Can't open $test_file: $!");
2209 }
2210
2211 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2212 '/bin/bash');
2213 auth_group_write($auth_group_file, $group, $gid, $user);
2214
2215 my $config = {
2216 PidFile => $pid_file,
2217 ScoreboardFile => $scoreboard_file,
2218 SystemLog => $log_file,
2219 TraceLog => $log_file,
2220 Trace => 'DEFAULT:10 fsio:0 lock:0',
2221
2222 AuthUserFile => $auth_user_file,
2223 AuthGroupFile => $auth_group_file,
2224 AuthOrder => 'mod_auth_file.c',
2225
2226 IfModules => {
2227 'mod_delay.c' => {
2228 DelayEngine => 'off',
2229 },
2230 },
2231 };
2232
2233 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2234
2235 # Open pipes, for use between the parent and child processes. Specifically,
2236 # the child will indicate when it's done with its test by writing a message
2237 # to the parent.
2238 my ($rfh, $wfh);
2239 unless (pipe($rfh, $wfh)) {
2240 die("Can't open pipe: $!");
2241 }
2242
2243 my $ex;
2244
2245 # Fork child
2246 $self->handle_sigchld();
2247 defined(my $pid = fork()) or die("Can't fork: $!");
2248 if ($pid) {
2249 eval {
2250 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2251
2252 $client->login($user, $passwd);
2253
2254 my $conn = $client->nlst_raw("-B test*");
2255 unless ($conn) {
2256 die("NLST failed: " . $client->response_code() . " " .
2257 $client->response_msg());
2258 }
2259
2260 my $buf;
2261 my $tmp;
2262 while ($conn->read($tmp, 32768, 25)) {
2263 $buf .= $tmp;
2264 }
2265 eval { $conn->close() };
2266 $client->quit();
2267
2268 my $res = {};
2269 my $lines = [split(/\n/, $buf)];
2270 foreach my $line (@$lines) {
2271 $res->{$line} = 1;
2272 }
2273
2274 my $expected = {
2275 'test\010' => 1,
2276 };
2277
2278 my $ok = 1;
2279 my $mismatch;
2280 foreach my $name (keys(%$res)) {
2281 unless (defined($expected->{$name})) {
2282 $mismatch = $name;
2283 $ok = 0;
2284 last;
2285 }
2286 }
2287
2288 unless ($ok) {
2289 die("Unexpected name '$mismatch' appeared in NLST data")
2290 }
2291 };
2292
2293 if ($@) {
2294 $ex = $@;
2295 }
2296
2297 $wfh->print("done\n");
2298 $wfh->flush();
2299
2300 } else {
2301 eval { server_wait($config_file, $rfh) };
2302 if ($@) {
2303 warn($@);
2304 exit 1;
2305 }
2306
2307 exit 0;
2308 }
2309
2310 # Stop server
2311 server_stop($pid_file);
2312
2313 $self->assert_child_ok($pid);
2314
2315 if ($ex) {
2316 test_append_logfile($log_file, $ex);
2317 unlink($log_file);
2318
2319 die($ex);
2320 }
2321
2322 unlink($log_file);
2323 }
2324
2325 sub nlst_leading_whitespace_bug3268 {
2326 my $self = shift;
2327 my $tmpdir = $self->{tmpdir};
2328
2329 my $config_file = "$tmpdir/cmds.conf";
2330 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
2331 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
2332
2333 my $log_file = test_get_logfile();
2334
2335 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
2336 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
2337
2338 my $user = 'proftpd';
2339 my $passwd = 'test';
2340 my $group = 'ftpd';
2341 my $home_dir = File::Spec->rel2abs($tmpdir);
2342 my $uid = 500;
2343 my $gid = 500;
2344
2345 # Make sure that, if we're running as root, that the home directory has
2346 # permissions/privs set for the account we create
2347 if ($< == 0) {
2348 unless (chmod(0755, $home_dir)) {
2349 die("Can't set perms on $home_dir to 0755: $!");
2350 }
2351
2352 unless (chown($uid, $gid, $home_dir)) {
2353 die("Can't set owner of $home_dir to $uid/$gid: $!");
2354 }
2355 }
2356
2357 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2358 '/bin/bash');
2359 auth_group_write($auth_group_file, $group, $gid, $user);
2360
2361 my $test_file = File::Spec->rel2abs("$tmpdir/ test.txt");
2362 if (open(my $fh, "> $test_file")) {
2363 close($fh);
2364
2365 } else {
2366 die("Can't open '$test_file': $!");
2367 }
2368
2369 my $config = {
2370 PidFile => $pid_file,
2371 ScoreboardFile => $scoreboard_file,
2372 SystemLog => $log_file,
2373
2374 AuthUserFile => $auth_user_file,
2375 AuthGroupFile => $auth_group_file,
2376 AuthOrder => 'mod_auth_file.c',
2377
2378 IfModules => {
2379 'mod_delay.c' => {
2380 DelayEngine => 'off',
2381 },
2382 },
2383 };
2384
2385 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2386
2387 # Open pipes, for use between the parent and child processes. Specifically,
2388 # the child will indicate when it's done with its test by writing a message
2389 # to the parent.
2390 my ($rfh, $wfh);
2391 unless (pipe($rfh, $wfh)) {
2392 die("Can't open pipe: $!");
2393 }
2394
2395 my $ex;
2396
2397 # Fork child
2398 $self->handle_sigchld();
2399 defined(my $pid = fork()) or die("Can't fork: $!");
2400 if ($pid) {
2401 eval {
2402 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2403 $client->login($user, $passwd);
2404
2405 my $conn = $client->nlst_raw(' test.txt');
2406 unless ($conn) {
2407 die("Failed to NLST: " . $client->response_code() . " " .
2408 $client->response_msg());
2409 }
2410
2411 my $buf;
2412 $conn->read($buf, 8192, 25);
2413 eval { $conn->close() };
2414
2415 my $resp_code = $client->response_code();
2416 my $resp_msg = $client->response_msg();
2417
2418 my $expected;
2419
2420 $expected = 226;
2421 $self->assert($expected == $resp_code,
2422 test_msg("Expected $expected, got $resp_code"));
2423
2424 $expected = 'Transfer complete';
2425 $self->assert($expected eq $resp_msg,
2426 test_msg("Expected '$expected', got '$resp_msg'"));
2427
2428 # We have to be careful of the fact that readdir returns directory
2429 # entries in an unordered fashion.
2430 my $res = {};
2431 my $names = [split(/\n/, $buf)];
2432 foreach my $name (@$names) {
2433 $res->{$name} = 1;
2434 }
2435
2436 $expected = {
2437 ' test.txt' => 1,
2438 };
2439
2440 my $ok = 1;
2441 my $mismatch;
2442 foreach my $name (keys(%$res)) {
2443 unless (defined($expected->{$name})) {
2444 $mismatch = $name;
2445 $ok = 0;
2446 last;
2447 }
2448 }
2449
2450 unless ($ok) {
2451 die("Unexpected name '$mismatch' appeared in NLST data")
2452 }
2453 };
2454
2455 if ($@) {
2456 $ex = $@;
2457 }
2458
2459 $wfh->print("done\n");
2460 $wfh->flush();
2461
2462 } else {
2463 eval { server_wait($config_file, $rfh) };
2464 if ($@) {
2465 warn($@);
2466 exit 1;
2467 }
2468
2469 exit 0;
2470 }
2471
2472 # Stop server
2473 server_stop($pid_file);
2474
2475 $self->assert_child_ok($pid);
2476
2477 if ($ex) {
2478 test_append_logfile($log_file, $ex);
2479 unlink($log_file);
2480
2481 die($ex);
2482 }
2483
2484 unlink($log_file);
2485 }
2486
2487 sub nlst_leading_whitespace_with_opts_bug3268 {
2488 my $self = shift;
2489 my $tmpdir = $self->{tmpdir};
2490
2491 my $config_file = "$tmpdir/cmds.conf";
2492 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
2493 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
2494
2495 my $log_file = test_get_logfile();
2496
2497 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
2498 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
2499
2500 my $user = 'proftpd';
2501 my $passwd = 'test';
2502 my $group = 'ftpd';
2503 my $home_dir = File::Spec->rel2abs($tmpdir);
2504 my $uid = 500;
2505 my $gid = 500;
2506
2507 # Make sure that, if we're running as root, that the home directory has
2508 # permissions/privs set for the account we create
2509 if ($< == 0) {
2510 unless (chmod(0755, $home_dir)) {
2511 die("Can't set perms on $home_dir to 0755: $!");
2512 }
2513
2514 unless (chown($uid, $gid, $home_dir)) {
2515 die("Can't set owner of $home_dir to $uid/$gid: $!");
2516 }
2517 }
2518
2519 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2520 '/bin/bash');
2521 auth_group_write($auth_group_file, $group, $gid, $user);
2522
2523 my $test_file = File::Spec->rel2abs("$tmpdir/ test.txt");
2524 if (open(my $fh, "> $test_file")) {
2525 close($fh);
2526
2527 } else {
2528 die("Can't open '$test_file': $!");
2529 }
2530
2531 my $config = {
2532 PidFile => $pid_file,
2533 ScoreboardFile => $scoreboard_file,
2534 SystemLog => $log_file,
2535
2536 AuthUserFile => $auth_user_file,
2537 AuthGroupFile => $auth_group_file,
2538 AuthOrder => 'mod_auth_file.c',
2539
2540 IfModules => {
2541 'mod_delay.c' => {
2542 DelayEngine => 'off',
2543 },
2544 },
2545 };
2546
2547 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2548
2549 # Open pipes, for use between the parent and child processes. Specifically,
2550 # the child will indicate when it's done with its test by writing a message
2551 # to the parent.
2552 my ($rfh, $wfh);
2553 unless (pipe($rfh, $wfh)) {
2554 die("Can't open pipe: $!");
2555 }
2556
2557 my $ex;
2558
2559 # Fork child
2560 $self->handle_sigchld();
2561 defined(my $pid = fork()) or die("Can't fork: $!");
2562 if ($pid) {
2563 eval {
2564 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2565 $client->login($user, $passwd);
2566
2567 my $conn = $client->nlst_raw('-a test.txt');
2568 unless ($conn) {
2569 die("Failed to NLST: " . $client->response_code() . " " .
2570 $client->response_msg());
2571 }
2572
2573 my $buf;
2574 $conn->read($buf, 8192, 25);
2575 eval { $conn->close() };
2576
2577 my $resp_code = $client->response_code();
2578 my $resp_msg = $client->response_msg();
2579
2580 my $expected;
2581
2582 $expected = 226;
2583 $self->assert($expected == $resp_code,
2584 test_msg("Expected $expected, got $resp_code"));
2585
2586 $expected = 'Transfer complete';
2587 $self->assert($expected eq $resp_msg,
2588 test_msg("Expected '$expected', got '$resp_msg'"));
2589
2590 # We have to be careful of the fact that readdir returns directory
2591 # entries in an unordered fashion.
2592 my $res = {};
2593 my $names = [split(/\n/, $buf)];
2594 foreach my $name (@$names) {
2595 $res->{$name} = 1;
2596 }
2597
2598 $expected = {
2599 ' test.txt' => 1,
2600 };
2601
2602 my $ok = 1;
2603 my $mismatch;
2604 foreach my $name (keys(%$res)) {
2605 unless (defined($expected->{$name})) {
2606 $mismatch = $name;
2607 $ok = 0;
2608 last;
2609 }
2610 }
2611
2612 unless ($ok) {
2613 die("Unexpected name '$mismatch' appeared in NLST data")
2614 }
2615 };
2616
2617 if ($@) {
2618 $ex = $@;
2619 }
2620
2621 $wfh->print("done\n");
2622 $wfh->flush();
2623
2624 } else {
2625 eval { server_wait($config_file, $rfh) };
2626 if ($@) {
2627 warn($@);
2628 exit 1;
2629 }
2630
2631 exit 0;
2632 }
2633
2634 # Stop server
2635 server_stop($pid_file);
2636
2637 $self->assert_child_ok($pid);
2638
2639 if ($ex) {
2640 test_append_logfile($log_file, $ex);
2641 unlink($log_file);
2642
2643 die($ex);
2644 }
2645
2646 unlink($log_file);
2647 }
2648
2649 sub nlst_leading_whitespace_with_strict_opts_bug3268 {
2650 my $self = shift;
2651 my $tmpdir = $self->{tmpdir};
2652
2653 my $config_file = "$tmpdir/cmds.conf";
2654 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
2655 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
2656
2657 my $log_file = test_get_logfile();
2658
2659 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
2660 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
2661
2662 my $user = 'proftpd';
2663 my $passwd = 'test';
2664 my $group = 'ftpd';
2665 my $home_dir = File::Spec->rel2abs($tmpdir);
2666 my $uid = 500;
2667 my $gid = 500;
2668
2669 # Make sure that, if we're running as root, that the home directory has
2670 # permissions/privs set for the account we create
2671 if ($< == 0) {
2672 unless (chmod(0755, $home_dir)) {
2673 die("Can't set perms on $home_dir to 0755: $!");
2674 }
2675
2676 unless (chown($uid, $gid, $home_dir)) {
2677 die("Can't set owner of $home_dir to $uid/$gid: $!");
2678 }
2679 }
2680
2681 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2682 '/bin/bash');
2683 auth_group_write($auth_group_file, $group, $gid, $user);
2684
2685 my $test_file = File::Spec->rel2abs("$tmpdir/ test.txt");
2686 if (open(my $fh, "> $test_file")) {
2687 close($fh);
2688
2689 } else {
2690 die("Can't open '$test_file': $!");
2691 }
2692
2693 my $config = {
2694 PidFile => $pid_file,
2695 ScoreboardFile => $scoreboard_file,
2696 SystemLog => $log_file,
2697
2698 AuthUserFile => $auth_user_file,
2699 AuthGroupFile => $auth_group_file,
2700 AuthOrder => 'mod_auth_file.c',
2701
2702 ListOptions => '"-a +R" strict',
2703
2704 IfModules => {
2705 'mod_delay.c' => {
2706 DelayEngine => 'off',
2707 },
2708 },
2709 };
2710
2711 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2712
2713 # Open pipes, for use between the parent and child processes. Specifically,
2714 # the child will indicate when it's done with its test by writing a message
2715 # to the parent.
2716 my ($rfh, $wfh);
2717 unless (pipe($rfh, $wfh)) {
2718 die("Can't open pipe: $!");
2719 }
2720
2721 my $ex;
2722
2723 # Fork child
2724 $self->handle_sigchld();
2725 defined(my $pid = fork()) or die("Can't fork: $!");
2726 if ($pid) {
2727 eval {
2728 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2729 $client->login($user, $passwd);
2730
2731 my $conn = $client->nlst_raw(' -l test.txt');
2732 unless ($conn) {
2733 die("Failed to NLST: " . $client->response_code() . " " .
2734 $client->response_msg());
2735 }
2736
2737 my $buf;
2738 $conn->read($buf, 8192, 25);
2739 eval { $conn->close() };
2740
2741 my $resp_code = $client->response_code();
2742 my $resp_msg = $client->response_msg();
2743
2744 my $expected;
2745
2746 $expected = 226;
2747 $self->assert($expected == $resp_code,
2748 test_msg("Expected $expected, got $resp_code"));
2749
2750 $expected = 'Transfer complete';
2751 $self->assert($expected eq $resp_msg,
2752 test_msg("Expected '$expected', got '$resp_msg'"));
2753
2754 # We have to be careful of the fact that readdir returns directory
2755 # entries in an unordered fashion.
2756 my $res = {};
2757 my $names = [split(/\n/, $buf)];
2758 foreach my $name (@$names) {
2759 $res->{$name} = 1;
2760 }
2761
2762 $expected = {
2763 ' test.txt' => 1,
2764 };
2765
2766 my $ok = 1;
2767 my $mismatch;
2768 foreach my $name (keys(%$res)) {
2769 unless (defined($expected->{$name})) {
2770 $mismatch = $name;
2771 $ok = 0;
2772 last;
2773 }
2774 }
2775
2776 unless ($ok) {
2777 die("Unexpected name '$mismatch' appeared in NLST data")
2778 }
2779 };
2780
2781 if ($@) {
2782 $ex = $@;
2783 }
2784
2785 $wfh->print("done\n");
2786 $wfh->flush();
2787
2788 } else {
2789 eval { server_wait($config_file, $rfh) };
2790 if ($@) {
2791 warn($@);
2792 exit 1;
2793 }
2794
2795 exit 0;
2796 }
2797
2798 # Stop server
2799 server_stop($pid_file);
2800
2801 $self->assert_child_ok($pid);
2802
2803 if ($ex) {
2804 test_append_logfile($log_file, $ex);
2805 unlink($log_file);
2806
2807 die($ex);
2808 }
2809
2810 unlink($log_file);
2811 }
2812
2813 sub nlst_symlink_bug3254 {
2814 my $self = shift;
2815 my $tmpdir = $self->{tmpdir};
2816
2817 my $config_file = "$tmpdir/cmds.conf";
2818 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
2819 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
2820
2821 my $log_file = test_get_logfile();
2822
2823 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
2824 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
2825
2826 my $user = 'proftpd';
2827 my $passwd = 'test';
2828 my $group = 'ftpd';
2829 my $home_dir = File::Spec->rel2abs($tmpdir);
2830 my $uid = 500;
2831 my $gid = 500;
2832
2833 my $foo_dir = File::Spec->rel2abs("$tmpdir/foo");
2834 my $bar_dir = File::Spec->rel2abs("$tmpdir/bar");
2835 my $baz_dir = File::Spec->rel2abs("$tmpdir/bar/baz");
2836 my $quxx_dir = File::Spec->rel2abs("$tmpdir/bar/quxx");
2837 mkpath([$foo_dir, $bar_dir, $baz_dir, $quxx_dir]);
2838
2839 # Change to the 'foo' directory in order to create a relative path in the
2840 # symlink we need
2841
2842 my $cwd = getcwd();
2843 unless (chdir("$foo_dir")) {
2844 die("Can't chdir to $foo_dir: $!");
2845 }
2846
2847 unless (symlink("../bar/baz", "baz")) {
2848 die("Can't symlink '../bar/baz' to 'baz': $!");
2849 }
2850
2851 unless (chdir($cwd)) {
2852 die("Can't chdir to $cwd: $!");
2853 }
2854
2855 my $ftpaccess_file = File::Spec->rel2abs("$foo_dir/.ftpaccess");
2856 if (open(my $fh, "> $ftpaccess_file")) {
2857 print $fh <<EOL;
2858 <Limit ALL>
2859 AllowUser $user
2860 DenyAll
2861 </Limit>
2862 EOL
2863 unless (close($fh)) {
2864 die("Can't write $ftpaccess_file: $!");
2865 }
2866
2867 } else {
2868 die("Can't open $ftpaccess_file: $!");
2869 }
2870
2871 $ftpaccess_file = File::Spec->rel2abs("$bar_dir/.ftpaccess");
2872 if (open(my $fh, "> $ftpaccess_file")) {
2873 print $fh <<EOL;
2874 <Limit ALL>
2875 AllowUser bar
2876 DenyAll
2877 </Limit>
2878 EOL
2879 unless (close($fh)) {
2880 die("Can't write $ftpaccess_file: $!");
2881 }
2882
2883 } else {
2884 die("Can't open $ftpaccess_file: $!");
2885 }
2886
2887 $ftpaccess_file = File::Spec->rel2abs("$baz_dir/.ftpaccess");
2888 if (open(my $fh, "> $ftpaccess_file")) {
2889 print $fh <<EOL;
2890 <Limit ALL>
2891 AllowUser bar,$user
2892 DenyAll
2893 </Limit>
2894 EOL
2895 unless (close($fh)) {
2896 die("Can't write $ftpaccess_file: $!");
2897 }
2898
2899 } else {
2900 die("Can't open $ftpaccess_file: $!");
2901 }
2902
2903 $ftpaccess_file = File::Spec->rel2abs("$quxx_dir/.ftpaccess");
2904 if (open(my $fh, "> $ftpaccess_file")) {
2905 print $fh <<EOL;
2906 <Limit ALL>
2907 AllowUser bar,$user,
2908 DenyAll
2909 </Limit>
2910 EOL
2911 unless (close($fh)) {
2912 die("Can't write $ftpaccess_file: $!");
2913 }
2914
2915 } else {
2916 die("Can't open $ftpaccess_file: $!");
2917 }
2918
2919 # Make sure that, if we're running as root, that the home directory has
2920 # permissions/privs set for the account we create
2921 if ($< == 0) {
2922 unless (chmod(0755, $home_dir, $foo_dir, $bar_dir, $baz_dir, $quxx_dir)) {
2923 die("Can't set perms on dirs to 0755: $!");
2924 }
2925
2926 unless (chown($uid, $gid, $home_dir, $foo_dir, $bar_dir, $baz_dir, $quxx_dir)) {
2927 die("Can't set owner of dirs to $uid/$gid: $!");
2928 }
2929 }
2930
2931 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2932 '/bin/bash');
2933 auth_group_write($auth_group_file, $group, $gid, $user);
2934
2935 my $config = {
2936 PidFile => $pid_file,
2937 ScoreboardFile => $scoreboard_file,
2938 SystemLog => $log_file,
2939
2940 AuthUserFile => $auth_user_file,
2941 AuthGroupFile => $auth_group_file,
2942 AuthOrder => 'mod_auth_file.c',
2943
2944 AllowOverride => 'on',
2945 DefaultRoot => '~',
2946 ShowSymlinks => 'off',
2947
2948 IfModules => {
2949 'mod_delay.c' => {
2950 DelayEngine => 'off',
2951 },
2952 },
2953 };
2954
2955 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2956
2957 # Open pipes, for use between the parent and child processes. Specifically,
2958 # the child will indicate when it's done with its test by writing a message
2959 # to the parent.
2960 my ($rfh, $wfh);
2961 unless (pipe($rfh, $wfh)) {
2962 die("Can't open pipe: $!");
2963 }
2964
2965 my $ex;
2966
2967 # Fork child
2968 $self->handle_sigchld();
2969 defined(my $pid = fork()) or die("Can't fork: $!");
2970 if ($pid) {
2971 eval {
2972 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2973
2974 $client->login($user, $passwd);
2975
2976 $client->cwd("foo/baz");
2977 my $conn = $client->nlst_raw();
2978 unless ($conn) {
2979 die("Failed to NLST: " . $client->response_code() . " " .
2980 $client->response_msg());
2981 }
2982
2983 my $buf;
2984 $conn->read($buf, 8192, 25);
2985 eval { $conn->close() };
2986
2987 my $resp_code = $client->response_code();
2988 my $resp_msg = $client->response_msg();
2989
2990 my $expected;
2991
2992 $expected = 226;
2993 $self->assert($expected == $resp_code,
2994 test_msg("Expected $expected, got $resp_code"));
2995
2996 $expected = 'Transfer complete';
2997 $self->assert($expected eq $resp_msg,
2998 test_msg("Expected '$expected', got '$resp_msg'"));
2999
3000 $client->quote('CWD', '..');
3001
3002 $client->quit();
3003 };
3004
3005 if ($@) {
3006 $ex = $@;
3007 }
3008
3009 $wfh->print("done\n");
3010 $wfh->flush();
3011
3012 } else {
3013 eval { server_wait($config_file, $rfh) };
3014 if ($@) {
3015 warn($@);
3016 exit 1;
3017 }
3018
3019 exit 0;
3020 }
3021
3022 # Stop server
3023 server_stop($pid_file);
3024
3025 $self->assert_child_ok($pid);
3026
3027 if ($ex) {
3028 test_append_logfile($log_file, $ex);
3029 unlink($log_file);
3030
3031 die($ex);
3032 }
3033
3034 unlink($log_file);
3035 }
3036
3037 sub nlst_dash_filename_bug3476 {
3038 my $self = shift;
3039 my $tmpdir = $self->{tmpdir};
3040
3041 my $config_file = "$tmpdir/cmds.conf";
3042 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
3043 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
3044
3045 my $log_file = test_get_logfile();
3046
3047 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
3048 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
3049
3050 my $user = 'proftpd';
3051 my $passwd = 'test';
3052 my $group = 'ftpd';
3053 my $home_dir = File::Spec->rel2abs($tmpdir);
3054 my $uid = 500;
3055 my $gid = 500;
3056
3057 # Make sure that, if we're running as root, that the home directory has
3058 # permissions/privs set for the account we create
3059 if ($< == 0) {
3060 unless (chmod(0755, $home_dir)) {
3061 die("Can't set perms on $home_dir to 0755: $!");
3062 }
3063
3064 unless (chown($uid, $gid, $home_dir)) {
3065 die("Can't set owner of $home_dir to $uid/$gid: $!");
3066 }
3067 }
3068
3069 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3070 '/bin/bash');
3071 auth_group_write($auth_group_file, $group, $gid, $user);
3072
3073 my $test_file = File::Spec->rel2abs("$tmpdir/-test.txt");
3074 if (open(my $fh, "> $test_file")) {
3075 close($fh);
3076
3077 } else {
3078 die("Can't open $test_file: $!");
3079 }
3080
3081 my $config = {
3082 PidFile => $pid_file,
3083 ScoreboardFile => $scoreboard_file,
3084 SystemLog => $log_file,
3085
3086 AuthUserFile => $auth_user_file,
3087 AuthGroupFile => $auth_group_file,
3088 AuthOrder => 'mod_auth_file.c',
3089
3090 IfModules => {
3091 'mod_delay.c' => {
3092 DelayEngine => 'off',
3093 },
3094 },
3095 };
3096
3097 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3098
3099 # Open pipes, for use between the parent and child processes. Specifically,
3100 # the child will indicate when it's done with its test by writing a message
3101 # to the parent.
3102 my ($rfh, $wfh);
3103 unless (pipe($rfh, $wfh)) {
3104 die("Can't open pipe: $!");
3105 }
3106
3107 my $ex;
3108
3109 # Fork child
3110 $self->handle_sigchld();
3111 defined(my $pid = fork()) or die("Can't fork: $!");
3112 if ($pid) {
3113 eval {
3114 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3115
3116 $client->login($user, $passwd);
3117
3118 my $conn = $client->nlst_raw('-test.txt');
3119 unless ($conn) {
3120 die("NLST failed: " . $client->response_code() . " " .
3121 $client->response_msg());
3122 }
3123
3124 my $buf;
3125 $conn->read($buf, 8192, 25);
3126 eval { $conn->close() };
3127
3128 my $res = {};
3129 my $names = [split(/\n/, $buf)];
3130 foreach my $name (@$names) {
3131 $res->{$name} = 1;
3132 }
3133
3134 my $count = scalar(keys(%$res));
3135 unless ($count == 1) {
3136 die("NLST returned wrong number of entries (expected 1, got $count)");
3137 }
3138
3139 my $expected = '-test.txt';
3140 unless (defined($res->{$expected})) {
3141 die("NLST failed to return $expected");
3142 }
3143 };
3144
3145 if ($@) {
3146 $ex = $@;
3147 }
3148
3149 $wfh->print("done\n");
3150 $wfh->flush();
3151
3152 } else {
3153 eval { server_wait($config_file, $rfh) };
3154 if ($@) {
3155 warn($@);
3156 exit 1;
3157 }
3158
3159 exit 0;
3160 }
3161
3162 # Stop server
3163 server_stop($pid_file);
3164
3165 $self->assert_child_ok($pid);
3166
3167 if ($ex) {
3168 test_append_logfile($log_file, $ex);
3169 unlink($log_file);
3170
3171 die($ex);
3172 }
3173
3174 unlink($log_file);
3175 }
3176
3177 sub nlst_opt_noerrorifabsent_bug3506 {
3178 my $self = shift;
3179 my $tmpdir = $self->{tmpdir};
3180
3181 my $config_file = "$tmpdir/cmds.conf";
3182 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
3183 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
3184
3185 my $log_file = test_get_logfile();
3186
3187 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
3188 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
3189
3190 my $user = 'proftpd';
3191 my $passwd = 'test';
3192 my $group = 'ftpd';
3193 my $home_dir = File::Spec->rel2abs($tmpdir);
3194 my $uid = 500;
3195 my $gid = 500;
3196
3197 # Make sure that, if we're running as root, that the home directory has
3198 # permissions/privs set for the account we create
3199 if ($< == 0) {
3200 unless (chmod(0755, $home_dir)) {
3201 die("Can't set perms on $home_dir to 0755: $!");
3202 }
3203
3204 unless (chown($uid, $gid, $home_dir)) {
3205 die("Can't set owner of $home_dir to $uid/$gid: $!");
3206 }
3207 }
3208
3209 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3210 '/bin/bash');
3211 auth_group_write($auth_group_file, $group, $gid, $user);
3212
3213 my $test_file = File::Spec->rel2abs('foo-bar.txt');
3214
3215 my $config = {
3216 PidFile => $pid_file,
3217 ScoreboardFile => $scoreboard_file,
3218 SystemLog => $log_file,
3219
3220 AuthUserFile => $auth_user_file,
3221 AuthGroupFile => $auth_group_file,
3222 AuthOrder => 'mod_auth_file.c',
3223
3224 ListOptions => '"" NoErrorIfAbsent',
3225
3226 IfModules => {
3227 'mod_delay.c' => {
3228 DelayEngine => 'off',
3229 },
3230 },
3231 };
3232
3233 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3234
3235 # Open pipes, for use between the parent and child processes. Specifically,
3236 # the child will indicate when it's done with its test by writing a message
3237 # to the parent.
3238 my ($rfh, $wfh);
3239 unless (pipe($rfh, $wfh)) {
3240 die("Can't open pipe: $!");
3241 }
3242
3243 my $ex;
3244
3245 # Fork child
3246 $self->handle_sigchld();
3247 defined(my $pid = fork()) or die("Can't fork: $!");
3248 if ($pid) {
3249 eval {
3250 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3251
3252 $client->login($user, $passwd);
3253
3254 my $conn = $client->nlst_raw($test_file);
3255 unless ($conn) {
3256 die("NLST failed: " . $client->response_code() . " " .
3257 $client->response_msg());
3258 }
3259
3260 my $buf;
3261 $conn->read($buf, 8192, 25);
3262 eval { $conn->close() };
3263
3264 my $resp_code = $client->response_code();
3265 my $resp_msg = $client->response_msg();
3266
3267 my $expected;
3268
3269 $expected = 226;
3270 $self->assert($expected == $resp_code,
3271 test_msg("Expected $expected, got $resp_code"));
3272
3273 $expected = 'Transfer complete';
3274 $self->assert($expected eq $resp_msg,
3275 test_msg("Expected '$expected', got '$resp_msg'"));
3276
3277 my $res = {};
3278 my $names = [split(/\n/, $buf)];
3279 foreach my $name (@$names) {
3280 $res->{$name} = 1;
3281 }
3282
3283 my $count = scalar(keys(%$res));
3284 unless ($count == 0) {
3285 die("NLST returned wrong number of entries (expected 0, got $count)");
3286 }
3287 };
3288
3289 if ($@) {
3290 $ex = $@;
3291 }
3292
3293 $wfh->print("done\n");
3294 $wfh->flush();
3295
3296 } else {
3297 eval { server_wait($config_file, $rfh) };
3298 if ($@) {
3299 warn($@);
3300 exit 1;
3301 }
3302
3303 exit 0;
3304 }
3305
3306 # Stop server
3307 server_stop($pid_file);
3308
3309 $self->assert_child_ok($pid);
3310
3311 if ($ex) {
3312 test_append_logfile($log_file, $ex);
3313 unlink($log_file);
3314
3315 die($ex);
3316 }
3317
3318 unlink($log_file);
3319 }
3320
3321 sub nlst_trailing_slashes_bug2457 {
3322 my $self = shift;
3323 my $tmpdir = $self->{tmpdir};
3324
3325 my $config_file = "$tmpdir/cmds.conf";
3326 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
3327 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
3328
3329 my $log_file = test_get_logfile();
3330
3331 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
3332 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
3333
3334 my $user = 'proftpd';
3335 my $passwd = 'test';
3336 my $group = 'ftpd';
3337 my $home_dir = File::Spec->rel2abs($tmpdir);
3338 my $uid = 500;
3339 my $gid = 500;
3340
3341 my $sub_dir = File::Spec->rel2abs("$tmpdir/test.d");
3342 mkpath($sub_dir);
3343
3344 my $test_file = File::Spec->rel2abs("$sub_dir/.test.dat");
3345 if (open(my $fh, "> $test_file")) {
3346 print $fh "File\n";
3347 unless (close($fh)) {
3348 die("Can't write $test_file: $!");
3349 }
3350
3351 } else {
3352 die("Can't open $test_file: $!");
3353 }
3354
3355 # Make sure that, if we're running as root, that the home directory has
3356 # permissions/privs set for the account we create
3357 if ($< == 0) {
3358 unless (chmod(0755, $home_dir, $sub_dir)) {
3359 die("Can't set perms on $home_dir to 0755: $!");
3360 }
3361
3362 unless (chown($uid, $gid, $home_dir, $sub_dir)) {
3363 die("Can't set owner of $home_dir to $uid/$gid: $!");
3364 }
3365 }
3366
3367 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3368 '/bin/bash');
3369 auth_group_write($auth_group_file, $group, $gid, $user);
3370
3371 my $config = {
3372 PidFile => $pid_file,
3373 ScoreboardFile => $scoreboard_file,
3374 SystemLog => $log_file,
3375
3376 AuthUserFile => $auth_user_file,
3377 AuthGroupFile => $auth_group_file,
3378 AuthOrder => 'mod_auth_file.c',
3379
3380 IfModules => {
3381 'mod_delay.c' => {
3382 DelayEngine => 'off',
3383 },
3384 },
3385 };
3386
3387 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3388
3389 # Open pipes, for use between the parent and child processes. Specifically,
3390 # the child will indicate when it's done with its test by writing a message
3391 # to the parent.
3392 my ($rfh, $wfh);
3393 unless (pipe($rfh, $wfh)) {
3394 die("Can't open pipe: $!");
3395 }
3396
3397 my $ex;
3398
3399 # Fork child
3400 $self->handle_sigchld();
3401 defined(my $pid = fork()) or die("Can't fork: $!");
3402 if ($pid) {
3403 eval {
3404 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3405 $client->login($user, $passwd);
3406
3407 my $conn = $client->nlst_raw('test.d///.test.dat');
3408 unless ($conn) {
3409 die("Failed to NLST: " . $client->response_code() . " " .
3410 $client->response_msg());
3411 }
3412
3413 my $buf;
3414 $conn->read($buf, 8192, 25);
3415 eval { $conn->close() };
3416
3417 my $resp_code = $client->response_code();
3418 my $resp_msg = $client->response_msg();
3419 $self->assert_transfer_ok($resp_code, $resp_msg);
3420
3421 # We have to be careful of the fact that readdir returns directory
3422 # entries in an unordered fashion.
3423 my $res = {};
3424 my $names = [split(/\n/, $buf)];
3425 foreach my $name (@$names) {
3426 $res->{$name} = 1;
3427 }
3428
3429 my $expected = {
3430 'test.d/.test.dat' => 1,
3431 };
3432
3433 my $ok = 1;
3434 my $mismatch;
3435 foreach my $name (keys(%$res)) {
3436 unless (defined($expected->{$name})) {
3437 $mismatch = $name;
3438 $ok = 0;
3439 last;
3440 }
3441 }
3442
3443 unless ($ok) {
3444 die("Unexpected name '$mismatch' appeared in NLST data")
3445 }
3446 };
3447
3448 if ($@) {
3449 $ex = $@;
3450 }
3451
3452 $wfh->print("done\n");
3453 $wfh->flush();
3454
3455 } else {
3456 eval { server_wait($config_file, $rfh) };
3457 if ($@) {
3458 warn($@);
3459 exit 1;
3460 }
3461
3462 exit 0;
3463 }
3464
3465 # Stop server
3466 server_stop($pid_file);
3467
3468 $self->assert_child_ok($pid);
3469
3470 if ($ex) {
3471 test_append_logfile($log_file, $ex);
3472 unlink($log_file);
3473
3474 die($ex);
3475 }
3476
3477 unlink($log_file);
3478 }
3479
3480 sub nlst_rel_path_bug2496 {
3481 my $self = shift;
3482 my $tmpdir = $self->{tmpdir};
3483
3484 my $config_file = "$tmpdir/cmds.conf";
3485 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
3486 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
3487
3488 my $log_file = test_get_logfile();
3489
3490 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
3491 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
3492
3493 my $user = 'proftpd';
3494 my $passwd = 'test';
3495 my $group = 'ftpd';
3496 my $home_dir = File::Spec->rel2abs($tmpdir);
3497 my $uid = 500;
3498 my $gid = 500;
3499
3500 my $sub_dir = File::Spec->rel2abs("$tmpdir/test.d");
3501 mkpath($sub_dir);
3502
3503 my $test_file = File::Spec->rel2abs("$sub_dir/.test.dat");
3504 if (open(my $fh, "> $test_file")) {
3505 print $fh "File\n";
3506 unless (close($fh)) {
3507 die("Can't write $test_file: $!");
3508 }
3509
3510 } else {
3511 die("Can't open $test_file: $!");
3512 }
3513
3514 # Make sure that, if we're running as root, that the home directory has
3515 # permissions/privs set for the account we create
3516 if ($< == 0) {
3517 unless (chmod(0755, $home_dir, $sub_dir)) {
3518 die("Can't set perms on $home_dir to 0755: $!");
3519 }
3520
3521 unless (chown($uid, $gid, $home_dir, $sub_dir)) {
3522 die("Can't set owner of $home_dir to $uid/$gid: $!");
3523 }
3524 }
3525
3526 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3527 '/bin/bash');
3528 auth_group_write($auth_group_file, $group, $gid, $user);
3529
3530 my $config = {
3531 PidFile => $pid_file,
3532 ScoreboardFile => $scoreboard_file,
3533 SystemLog => $log_file,
3534
3535 AuthUserFile => $auth_user_file,
3536 AuthGroupFile => $auth_group_file,
3537 AuthOrder => 'mod_auth_file.c',
3538
3539 IfModules => {
3540 'mod_delay.c' => {
3541 DelayEngine => 'off',
3542 },
3543 },
3544 };
3545
3546 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3547
3548 # Open pipes, for use between the parent and child processes. Specifically,
3549 # the child will indicate when it's done with its test by writing a message
3550 # to the parent.
3551 my ($rfh, $wfh);
3552 unless (pipe($rfh, $wfh)) {
3553 die("Can't open pipe: $!");
3554 }
3555
3556 my $ex;
3557
3558 # Fork child
3559 $self->handle_sigchld();
3560 defined(my $pid = fork()) or die("Can't fork: $!");
3561 if ($pid) {
3562 eval {
3563 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3564 $client->login($user, $passwd);
3565 $client->cwd("test.d");
3566
3567 my $conn = $client->nlst_raw('.test.dat');
3568 unless ($conn) {
3569 die("Failed to NLST: " . $client->response_code() . " " .
3570 $client->response_msg());
3571 }
3572
3573 my $buf;
3574 $conn->read($buf, 8192, 25);
3575 eval { $conn->close() };
3576
3577 my $resp_code = $client->response_code();
3578 my $resp_msg = $client->response_msg();
3579 $self->assert_transfer_ok($resp_code, $resp_msg);
3580
3581 # We have to be careful of the fact that readdir returns directory
3582 # entries in an unordered fashion.
3583 my $res = {};
3584 my $names = [split(/\n/, $buf)];
3585 foreach my $name (@$names) {
3586 $res->{$name} = 1;
3587 }
3588
3589 my $expected = {
3590 '.test.dat' => 1,
3591 };
3592
3593 my $ok = 1;
3594 my $mismatch;
3595 foreach my $name (keys(%$res)) {
3596 unless (defined($expected->{$name})) {
3597 $mismatch = $name;
3598 $ok = 0;
3599 last;
3600 }
3601 }
3602
3603 unless ($ok) {
3604 die("Unexpected name '$mismatch' appeared in NLST data")
3605 }
3606 };
3607
3608 if ($@) {
3609 $ex = $@;
3610 }
3611
3612 $wfh->print("done\n");
3613 $wfh->flush();
3614
3615 } else {
3616 eval { server_wait($config_file, $rfh) };
3617 if ($@) {
3618 warn($@);
3619 exit 1;
3620 }
3621
3622 exit 0;
3623 }
3624
3625 # Stop server
3626 server_stop($pid_file);
3627
3628 $self->assert_child_ok($pid);
3629
3630 if ($ex) {
3631 test_append_logfile($log_file, $ex);
3632 unlink($log_file);
3633
3634 die($ex);
3635 }
3636
3637 unlink($log_file);
3638 }
3639
3640 sub nlst_rel_path_chrooted_bug2496 {
3641 my $self = shift;
3642 my $tmpdir = $self->{tmpdir};
3643
3644 my $config_file = "$tmpdir/cmds.conf";
3645 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
3646 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
3647
3648 my $log_file = test_get_logfile();
3649
3650 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
3651 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
3652
3653 my $user = 'proftpd';
3654 my $passwd = 'test';
3655 my $group = 'ftpd';
3656 my $home_dir = File::Spec->rel2abs($tmpdir);
3657 my $uid = 500;
3658 my $gid = 500;
3659
3660 my $sub_dir = File::Spec->rel2abs("$tmpdir/test.d");
3661 mkpath($sub_dir);
3662
3663 my $test_file = File::Spec->rel2abs("$sub_dir/.test.dat");
3664 if (open(my $fh, "> $test_file")) {
3665 print $fh "File\n";
3666 unless (close($fh)) {
3667 die("Can't write $test_file: $!");
3668 }
3669
3670 } else {
3671 die("Can't open $test_file: $!");
3672 }
3673
3674 # Make sure that, if we're running as root, that the home directory has
3675 # permissions/privs set for the account we create
3676 if ($< == 0) {
3677 unless (chmod(0755, $home_dir, $sub_dir)) {
3678 die("Can't set perms on $home_dir to 0755: $!");
3679 }
3680
3681 unless (chown($uid, $gid, $home_dir, $sub_dir)) {
3682 die("Can't set owner of $home_dir to $uid/$gid: $!");
3683 }
3684 }
3685
3686 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3687 '/bin/bash');
3688 auth_group_write($auth_group_file, $group, $gid, $user);
3689
3690 my $config = {
3691 PidFile => $pid_file,
3692 ScoreboardFile => $scoreboard_file,
3693 SystemLog => $log_file,
3694
3695 AuthUserFile => $auth_user_file,
3696 AuthGroupFile => $auth_group_file,
3697 AuthOrder => 'mod_auth_file.c',
3698
3699 DefaultRoot => '~',
3700
3701 IfModules => {
3702 'mod_delay.c' => {
3703 DelayEngine => 'off',
3704 },
3705 },
3706 };
3707
3708 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3709
3710 # Open pipes, for use between the parent and child processes. Specifically,
3711 # the child will indicate when it's done with its test by writing a message
3712 # to the parent.
3713 my ($rfh, $wfh);
3714 unless (pipe($rfh, $wfh)) {
3715 die("Can't open pipe: $!");
3716 }
3717
3718 my $ex;
3719
3720 # Fork child
3721 $self->handle_sigchld();
3722 defined(my $pid = fork()) or die("Can't fork: $!");
3723 if ($pid) {
3724 eval {
3725 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3726 $client->login($user, $passwd);
3727 $client->cwd("test.d");
3728
3729 my $conn = $client->nlst_raw('.test.dat');
3730 unless ($conn) {
3731 die("Failed to NLST: " . $client->response_code() . " " .
3732 $client->response_msg());
3733 }
3734
3735 my $buf;
3736 $conn->read($buf, 8192, 25);
3737 eval { $conn->close() };
3738
3739 my $resp_code = $client->response_code();
3740 my $resp_msg = $client->response_msg();
3741 $self->assert_transfer_ok($resp_code, $resp_msg);
3742
3743 # We have to be careful of the fact that readdir returns directory
3744 # entries in an unordered fashion.
3745 my $res = {};
3746 my $names = [split(/\n/, $buf)];
3747 foreach my $name (@$names) {
3748 $res->{$name} = 1;
3749 }
3750
3751 my $expected = {
3752 '.test.dat' => 1,
3753 };
3754
3755 my $ok = 1;
3756 my $mismatch;
3757 foreach my $name (keys(%$res)) {
3758 unless (defined($expected->{$name})) {
3759 $mismatch = $name;
3760 $ok = 0;
3761 last;
3762 }
3763 }
3764
3765 unless ($ok) {
3766 die("Unexpected name '$mismatch' appeared in NLST data")
3767 }
3768 };
3769
3770 if ($@) {
3771 $ex = $@;
3772 }
3773
3774 $wfh->print("done\n");
3775 $wfh->flush();
3776
3777 } else {
3778 eval { server_wait($config_file, $rfh) };
3779 if ($@) {
3780 warn($@);
3781 exit 1;
3782 }
3783
3784 exit 0;
3785 }
3786
3787 # Stop server
3788 server_stop($pid_file);
3789
3790 $self->assert_child_ok($pid);
3791
3792 if ($ex) {
3793 test_append_logfile($log_file, $ex);
3794 unlink($log_file);
3795
3796 die($ex);
3797 }
3798
3799 unlink($log_file);
3800 }
3801
3802 sub nlst_parent_dir_bug4011 {
3803 my $self = shift;
3804 my $tmpdir = $self->{tmpdir};
3805 my $setup = test_setup($tmpdir, 'cmds');
3806
3807 my $sub_dir1 = File::Spec->rel2abs("$tmpdir/dir1");
3808 my $sub_dir2 = File::Spec->rel2abs("$tmpdir/dir1/dir2");
3809 mkpath($sub_dir2);
3810
3811 my $test_file1 = File::Spec->rel2abs("$tmpdir/dir1/file1");
3812 if (open(my $fh, "> $test_file1")) {
3813 print $fh "File1\n";
3814 unless (close($fh)) {
3815 die("Can't write $test_file1: $!");
3816 }
3817
3818 } else {
3819 die("Can't open $test_file1: $!");
3820 }
3821
3822 my $test_file2 = File::Spec->rel2abs("$tmpdir/dir1/dir2/file2");
3823 if (open(my $fh, "> $test_file2")) {
3824 print $fh "File2\n";
3825 unless (close($fh)) {
3826 die("Can't write $test_file2: $!");
3827 }
3828
3829 } else {
3830 die("Can't open $test_file2: $!");
3831 }
3832
3833 # Make sure that, if we're running as root, that the home directory has
3834 # permissions/privs set for the account we create
3835 if ($< == 0) {
3836 unless (chmod(0755, $sub_dir1)) {
3837 die("Can't set perms on $sub_dir1 to 0755: $!");
3838 }
3839
3840 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir1)) {
3841 die("Can't set owner of $sub_dir1 to $setup->{uid}/$setup->{gid}: $!");
3842 }
3843 }
3844
3845 my $config = {
3846 PidFile => $setup->{pid_file},
3847 ScoreboardFile => $setup->{scoreboard_file},
3848 SystemLog => $setup->{log_file},
3849
3850 AuthUserFile => $setup->{auth_user_file},
3851 AuthGroupFile => $setup->{auth_group_file},
3852 AuthOrder => 'mod_auth_file.c',
3853
3854 IfModules => {
3855 'mod_delay.c' => {
3856 DelayEngine => 'off',
3857 },
3858 },
3859 };
3860
3861 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
3862 $config);
3863
3864 # Open pipes, for use between the parent and child processes. Specifically,
3865 # the child will indicate when it's done with its test by writing a message
3866 # to the parent.
3867 my ($rfh, $wfh);
3868 unless (pipe($rfh, $wfh)) {
3869 die("Can't open pipe: $!");
3870 }
3871
3872 my $ex;
3873
3874 # Fork child
3875 $self->handle_sigchld();
3876 defined(my $pid = fork()) or die("Can't fork: $!");
3877 if ($pid) {
3878 eval {
3879 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3880 $client->login($setup->{user}, $setup->{passwd});
3881 $client->cwd("dir1");
3882 $client->cwd("dir2");
3883
3884 my $conn = $client->nlst_raw('..////');
3885 unless ($conn) {
3886 die("Failed to NLST: " . $client->response_code() . " " .
3887 $client->response_msg());
3888 }
3889
3890 my $buf;
3891 $conn->read($buf, 8192, 25);
3892 eval { $conn->close() };
3893
3894 $client->quit();
3895
3896 if ($ENV{TEST_VERBOSE}) {
3897 print STDERR "# response:\n$buf\n";
3898 }
3899
3900 # We have to be careful of the fact that readdir returns directory
3901 # entries in an unordered fashion.
3902 my $res = {};
3903 my $names = [split(/\n/, $buf)];
3904 foreach my $name (@$names) {
3905 $res->{$name} = 1;
3906 }
3907
3908 my $expected = {
3909 '../file1' => 1,
3910 '../dir2' => 1,
3911 };
3912
3913 my $ok = 1;
3914 my $mismatch;
3915 foreach my $name (keys(%$res)) {
3916 unless (defined($expected->{$name})) {
3917 $mismatch = $name;
3918 $ok = 0;
3919 last;
3920 }
3921 }
3922
3923 unless ($ok) {
3924 die("Unexpected name '$mismatch' appeared in NLST data")
3925 }
3926 };
3927 if ($@) {
3928 $ex = $@;
3929 }
3930
3931 $wfh->print("done\n");
3932 $wfh->flush();
3933
3934 } else {
3935 eval { server_wait($setup->{config_file}, $rfh) };
3936 if ($@) {
3937 warn($@);
3938 exit 1;
3939 }
3940
3941 exit 0;
3942 }
3943
3944 # Stop server
3945 server_stop($setup->{pid_file});
3946 $self->assert_child_ok($pid);
3947
3948 test_cleanup($setup->{log_file}, $ex);
3949 }
3950
3951 sub nlst_opt_a_root_dir_bug4069 {
3952 my $self = shift;
3953 my $tmpdir = $self->{tmpdir};
3954
3955 my $config_file = "$tmpdir/cmds.conf";
3956 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
3957 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
3958
3959 my $log_file = test_get_logfile();
3960
3961 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
3962 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
3963
3964 my $user = 'proftpd';
3965 my $passwd = 'test';
3966 my $group = 'ftpd';
3967 my $home_dir = File::Spec->rel2abs($tmpdir);
3968 my $uid = 500;
3969 my $gid = 500;
3970
3971 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
3972 mkpath($test_dir);
3973
3974 my $test_file1 = File::Spec->rel2abs("$test_dir/.file1");
3975 if (open(my $fh, "> $test_file1")) {
3976 print $fh "File1\n";
3977 unless (close($fh)) {
3978 die("Can't write $test_file1: $!");
3979 }
3980
3981 } else {
3982 die("Can't open $test_file1: $!");
3983 }
3984
3985 my $test_file2 = File::Spec->rel2abs("$test_dir/.file2");
3986 if (open(my $fh, "> $test_file2")) {
3987 print $fh "File2\n";
3988 unless (close($fh)) {
3989 die("Can't write $test_file2: $!");
3990 }
3991
3992 } else {
3993 die("Can't open $test_file2: $!");
3994 }
3995
3996 # Make sure that, if we're running as root, that the home directory has
3997 # permissions/privs set for the account we create
3998 if ($< == 0) {
3999 unless (chmod(0755, $home_dir, $test_dir)) {
4000 die("Can't set perms on $home_dir to 0755: $!");
4001 }
4002
4003 unless (chown($uid, $gid, $home_dir, $test_dir)) {
4004 die("Can't set owner of $home_dir to $uid/$gid: $!");
4005 }
4006 }
4007
4008 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4009 '/bin/bash');
4010 auth_group_write($auth_group_file, $group, $gid, $user);
4011
4012 my $config = {
4013 PidFile => $pid_file,
4014 ScoreboardFile => $scoreboard_file,
4015 SystemLog => $log_file,
4016
4017 AuthUserFile => $auth_user_file,
4018 AuthGroupFile => $auth_group_file,
4019 AuthOrder => 'mod_auth_file.c',
4020
4021 IfModules => {
4022 'mod_delay.c' => {
4023 DelayEngine => 'off',
4024 },
4025 },
4026 };
4027
4028 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4029
4030 # Open pipes, for use between the parent and child processes. Specifically,
4031 # the child will indicate when it's done with its test by writing a message
4032 # to the parent.
4033 my ($rfh, $wfh);
4034 unless (pipe($rfh, $wfh)) {
4035 die("Can't open pipe: $!");
4036 }
4037
4038 my $ex;
4039
4040 # Fork child
4041 $self->handle_sigchld();
4042 defined(my $pid = fork()) or die("Can't fork: $!");
4043 if ($pid) {
4044 eval {
4045 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4046 $client->login($user, $passwd);
4047 $client->cwd('test.d');
4048
4049 my $conn = $client->nlst_raw('-a');
4050 unless ($conn) {
4051 die("Failed to NLST: " . $client->response_code() . " " .
4052 $client->response_msg());
4053 }
4054
4055 my $buf;
4056 $conn->read($buf, 8192, 25);
4057 eval { $conn->close() };
4058
4059 # We have to be careful of the fact that readdir returns directory
4060 # entries in an unordered fashion.
4061 my $res = {};
4062 my $names = [split(/\n/, $buf)];
4063 foreach my $name (@$names) {
4064 $res->{$name} = 1;
4065 }
4066
4067 my $expected = {
4068 '.' => 1,
4069 '..' => 1,
4070 '.file1' => 1,
4071 '.file2' => 1,
4072 };
4073
4074 my $ok = 1;
4075 my $mismatch;
4076 foreach my $name (keys(%$res)) {
4077 unless (defined($expected->{$name})) {
4078 $mismatch = $name;
4079 $ok = 0;
4080 last;
4081 }
4082 }
4083
4084 unless ($ok) {
4085 die("Unexpected name '$mismatch' appeared in NLST data")
4086 }
4087 };
4088
4089 if ($@) {
4090 $ex = $@;
4091 }
4092
4093 $wfh->print("done\n");
4094 $wfh->flush();
4095
4096 } else {
4097 eval { server_wait($config_file, $rfh) };
4098 if ($@) {
4099 warn($@);
4100 exit 1;
4101 }
4102
4103 exit 0;
4104 }
4105
4106 # Stop server
4107 server_stop($pid_file);
4108
4109 $self->assert_child_ok($pid);
4110
4111 if ($ex) {
4112 test_append_logfile($log_file, $ex);
4113 unlink($log_file);
4114
4115 die($ex);
4116 }
4117
4118 unlink($log_file);
4119 }
4120
4121 sub nlst_opt_1_with_chroot {
4122 my $self = shift;
4123 my $tmpdir = $self->{tmpdir};
4124
4125 my $config_file = "$tmpdir/cmds.conf";
4126 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
4127 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
4128
4129 my $log_file = test_get_logfile();
4130
4131 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
4132 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
4133
4134 my $user = 'proftpd';
4135 my $passwd = 'test';
4136 my $group = 'ftpd';
4137 my $home_dir = File::Spec->rel2abs($tmpdir);
4138 my $uid = 500;
4139 my $gid = 500;
4140
4141 # Make sure that, if we're running as root, that the home directory has
4142 # permissions/privs set for the account we create
4143 if ($< == 0) {
4144 unless (chmod(0755, $home_dir)) {
4145 die("Can't set perms on $home_dir to 0755: $!");
4146 }
4147
4148 unless (chown($uid, $gid, $home_dir)) {
4149 die("Can't set owner of $home_dir to $uid/$gid: $!");
4150 }
4151 }
4152
4153 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4154 '/bin/bash');
4155 auth_group_write($auth_group_file, $group, $gid, $user);
4156
4157 my $config = {
4158 PidFile => $pid_file,
4159 ScoreboardFile => $scoreboard_file,
4160 SystemLog => $log_file,
4161
4162 AuthUserFile => $auth_user_file,
4163 AuthGroupFile => $auth_group_file,
4164 AuthOrder => 'mod_auth_file.c',
4165
4166 DefaultRoot => '~',
4167
4168 IfModules => {
4169 'mod_delay.c' => {
4170 DelayEngine => 'off',
4171 },
4172 },
4173 };
4174
4175 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4176
4177 # Open pipes, for use between the parent and child processes. Specifically,
4178 # the child will indicate when it's done with its test by writing a message
4179 # to the parent.
4180 my ($rfh, $wfh);
4181 unless (pipe($rfh, $wfh)) {
4182 die("Can't open pipe: $!");
4183 }
4184
4185 my $ex;
4186
4187 # Fork child
4188 $self->handle_sigchld();
4189 defined(my $pid = fork()) or die("Can't fork: $!");
4190 if ($pid) {
4191 eval {
4192 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4193 $client->login($user, $passwd);
4194
4195 my $conn = $client->nlst_raw("-a1 /");
4196 unless ($conn) {
4197 die("NLST failed: " . $client->response_code() . " " .
4198 $client->response_msg());
4199 }
4200
4201 my $buf;
4202 $conn->read($buf, 8192, 25);
4203 eval { $conn->close() };
4204
4205 my $res = {};
4206 my $names = [split(/\n/, $buf)];
4207 foreach my $name (@$names) {
4208 if ($name !~ /^\//) {
4209 $res->{$name} = 1;
4210 }
4211 }
4212
4213 my $count = scalar(keys(%$res));
4214 my $expected = 8;
4215 unless ($count == $expected) {
4216 die("NLST returned wrong number of entries (expected $expected, got $count)");
4217 }
4218 };
4219
4220 if ($@) {
4221 $ex = $@;
4222 }
4223
4224 $wfh->print("done\n");
4225 $wfh->flush();
4226
4227 } else {
4228 eval { server_wait($config_file, $rfh) };
4229 if ($@) {
4230 warn($@);
4231 exit 1;
4232 }
4233
4234 exit 0;
4235 }
4236
4237 # Stop server
4238 server_stop($pid_file);
4239
4240 $self->assert_child_ok($pid);
4241
4242 if ($ex) {
4243 test_append_logfile($log_file, $ex);
4244 unlink($log_file);
4245
4246 die($ex);
4247 }
4248
4249 unlink($log_file);
4250 }
4251
4252 sub nlst_glob_with_rel_path_issue1325 {
4253 my $self = shift;
4254 my $tmpdir = $self->{tmpdir};
4255 my $setup = test_setup($tmpdir, 'cmds');
4256
4257 my $test_path = File::Spec->rel2abs("$tmpdir/test.d");
4258 mkpath($test_path);
4259
4260 for (my $i = 0; $i < 10; $i++) {
4261 my $test_file = File::Spec->rel2abs("$test_path/TEST000$i.dat");
4262 if (open(my $fh, "> $test_file")) {
4263 print $fh "Hello, World!\n";
4264 unless (close($fh)) {
4265 die("Can't write $test_file: $!");
4266 }
4267
4268 } else {
4269 die("Can't open $test_file: $!");
4270 }
4271 }
4272
4273 my $config = {
4274 PidFile => $setup->{pid_file},
4275 ScoreboardFile => $setup->{scoreboard_file},
4276 SystemLog => $setup->{log_file},
4277
4278 AuthUserFile => $setup->{auth_user_file},
4279 AuthGroupFile => $setup->{auth_group_file},
4280 AuthOrder => 'mod_auth_file.c',
4281
4282 IfModules => {
4283 'mod_delay.c' => {
4284 DelayEngine => 'off',
4285 },
4286 },
4287 };
4288
4289 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
4290 $config);
4291
4292 # Open pipes, for use between the parent and child processes. Specifically,
4293 # the child will indicate when it's done with its test by writing a message
4294 # to the parent.
4295 my ($rfh, $wfh);
4296 unless (pipe($rfh, $wfh)) {
4297 die("Can't open pipe: $!");
4298 }
4299
4300 my $ex;
4301
4302 # Fork child
4303 $self->handle_sigchld();
4304 defined(my $pid = fork()) or die("Can't fork: $!");
4305 if ($pid) {
4306 eval {
4307 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4308 $client->login($setup->{user}, $setup->{passwd});
4309
4310 my $conn = $client->nlst_raw('test.d/TEST????.dat');
4311 unless ($conn) {
4312 die("Failed to NLST: " . $client->response_code() . " " .
4313 $client->response_msg());
4314 }
4315
4316 my $buf;
4317 $conn->read($buf, 8192, 25);
4318 eval { $conn->close() };
4319
4320 if ($ENV{TEST_VERBOSE}) {
4321 print STDERR "# response:\n$buf\n";
4322 }
4323
4324 # We have to be careful of the fact that readdir returns directory
4325 # entries in an unordered fashion.
4326 my $res = {};
4327 my $names = [split(/\n/, $buf)];
4328 foreach my $name (@$names) {
4329 $res->{$name} = 1;
4330 }
4331
4332 $self->assert(scalar(@$names) > 0,
4333 test_msg("Expected multiple names, got 0"));
4334
4335 my $expected = {
4336 'test.d/TEST0000.dat' => 1,
4337 'test.d/TEST0001.dat' => 1,
4338 'test.d/TEST0002.dat' => 1,
4339 'test.d/TEST0003.dat' => 1,
4340 'test.d/TEST0004.dat' => 1,
4341 'test.d/TEST0005.dat' => 1,
4342 'test.d/TEST0006.dat' => 1,
4343 'test.d/TEST0007.dat' => 1,
4344 'test.d/TEST0008.dat' => 1,
4345 'test.d/TEST0009.dat' => 1,
4346 };
4347
4348 my $ok = 1;
4349 my $mismatch;
4350 foreach my $name (keys(%$res)) {
4351 unless (defined($expected->{$name})) {
4352 $mismatch = $name;
4353 $ok = 0;
4354 last;
4355 }
4356 }
4357
4358 unless ($ok) {
4359 die("Unexpected name '$mismatch' appeared in NLST data")
4360 }
4361
4362 # Now do it again, this time using an explicit relative path.
4363
4364 $conn = $client->nlst_raw('./test.d/TEST????.dat');
4365 unless ($conn) {
4366 die("Failed to NLST: " . $client->response_code() . " " .
4367 $client->response_msg());
4368 }
4369
4370 $buf = '';
4371 $conn->read($buf, 8192, 25);
4372 eval { $conn->close() };
4373
4374 if ($ENV{TEST_VERBOSE}) {
4375 print STDERR "# response:\n$buf\n";
4376 }
4377
4378 # We have to be careful of the fact that readdir returns directory
4379 # entries in an unordered fashion.
4380 $res = {};
4381 $names = [split(/\n/, $buf)];
4382 foreach my $name (@$names) {
4383 $res->{$name} = 1;
4384 }
4385
4386 $self->assert(scalar(@$names) > 0,
4387 test_msg("Expected multiple names, got 0"));
4388
4389 $expected = {
4390 './test.d/TEST0000.dat' => 1,
4391 './test.d/TEST0001.dat' => 1,
4392 './test.d/TEST0002.dat' => 1,
4393 './test.d/TEST0003.dat' => 1,
4394 './test.d/TEST0004.dat' => 1,
4395 './test.d/TEST0005.dat' => 1,
4396 './test.d/TEST0006.dat' => 1,
4397 './test.d/TEST0007.dat' => 1,
4398 './test.d/TEST0008.dat' => 1,
4399 './test.d/TEST0009.dat' => 1,
4400 };
4401
4402 $ok = 1;
4403 $mismatch = '';
4404 foreach my $name (keys(%$res)) {
4405 unless (defined($expected->{$name})) {
4406 $mismatch = $name;
4407 $ok = 0;
4408 last;
4409 }
4410 }
4411
4412 unless ($ok) {
4413 die("Unexpected name '$mismatch' appeared in NLST data")
4414 }
4415
4416 $client->quit();
4417 };
4418 if ($@) {
4419 $ex = $@;
4420 }
4421
4422 $wfh->print("done\n");
4423 $wfh->flush();
4424
4425 } else {
4426 eval { server_wait($setup->{config_file}, $rfh) };
4427 if ($@) {
4428 warn($@);
4429 exit 1;
4430 }
4431
4432 exit 0;
4433 }
4434
4435 # Stop server
4436 server_stop($setup->{pid_file});
4437 $self->assert_child_ok($pid);
4438
4439 test_cleanup($setup->{log_file}, $ex);
4440 }
4441
4442 sub nlst_glob_with_rel_path_dotdir_issue1325 {
4443 my $self = shift;
4444 my $tmpdir = $self->{tmpdir};
4445 my $setup = test_setup($tmpdir, 'cmds');
4446
4447 my $test_path = File::Spec->rel2abs("$tmpdir/.test.d");
4448 mkpath($test_path);
4449
4450 for (my $i = 0; $i < 10; $i++) {
4451 my $test_file = File::Spec->rel2abs("$test_path/TEST000$i.dat");
4452 if (open(my $fh, "> $test_file")) {
4453 print $fh "Hello, World!\n";
4454 unless (close($fh)) {
4455 die("Can't write $test_file: $!");
4456 }
4457
4458 } else {
4459 die("Can't open $test_file: $!");
4460 }
4461 }
4462
4463 my $config = {
4464 PidFile => $setup->{pid_file},
4465 ScoreboardFile => $setup->{scoreboard_file},
4466 SystemLog => $setup->{log_file},
4467
4468 AuthUserFile => $setup->{auth_user_file},
4469 AuthGroupFile => $setup->{auth_group_file},
4470 AuthOrder => 'mod_auth_file.c',
4471
4472 IfModules => {
4473 'mod_delay.c' => {
4474 DelayEngine => 'off',
4475 },
4476 },
4477 };
4478
4479 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
4480 $config);
4481
4482 # Open pipes, for use between the parent and child processes. Specifically,
4483 # the child will indicate when it's done with its test by writing a message
4484 # to the parent.
4485 my ($rfh, $wfh);
4486 unless (pipe($rfh, $wfh)) {
4487 die("Can't open pipe: $!");
4488 }
4489
4490 my $ex;
4491
4492 # Fork child
4493 $self->handle_sigchld();
4494 defined(my $pid = fork()) or die("Can't fork: $!");
4495 if ($pid) {
4496 eval {
4497 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4498 $client->login($setup->{user}, $setup->{passwd});
4499
4500 my $conn = $client->nlst_raw('.test.d/TEST????.dat');
4501 unless ($conn) {
4502 die("Failed to NLST: " . $client->response_code() . " " .
4503 $client->response_msg());
4504 }
4505
4506 my $buf;
4507 $conn->read($buf, 8192, 25);
4508 eval { $conn->close() };
4509
4510 if ($ENV{TEST_VERBOSE}) {
4511 print STDERR "# response:\n$buf\n";
4512 }
4513
4514 # We have to be careful of the fact that readdir returns directory
4515 # entries in an unordered fashion.
4516 my $res = {};
4517 my $names = [split(/\n/, $buf)];
4518 foreach my $name (@$names) {
4519 $res->{$name} = 1;
4520 }
4521
4522 $self->assert(scalar(@$names) > 0,
4523 test_msg("Expected multiple names, got 0"));
4524
4525 my $expected = {
4526 '.test.d/TEST0000.dat' => 1,
4527 '.test.d/TEST0001.dat' => 1,
4528 '.test.d/TEST0002.dat' => 1,
4529 '.test.d/TEST0003.dat' => 1,
4530 '.test.d/TEST0004.dat' => 1,
4531 '.test.d/TEST0005.dat' => 1,
4532 '.test.d/TEST0006.dat' => 1,
4533 '.test.d/TEST0007.dat' => 1,
4534 '.test.d/TEST0008.dat' => 1,
4535 '.test.d/TEST0009.dat' => 1,
4536 };
4537
4538 my $ok = 1;
4539 my $mismatch;
4540 foreach my $name (keys(%$res)) {
4541 unless (defined($expected->{$name})) {
4542 $mismatch = $name;
4543 $ok = 0;
4544 last;
4545 }
4546 }
4547
4548 unless ($ok) {
4549 die("Unexpected name '$mismatch' appeared in NLST data")
4550 }
4551
4552 # Now do it again, this time using an explicit relative path.
4553
4554 $conn = $client->nlst_raw('./.test.d/TEST????.dat');
4555 unless ($conn) {
4556 die("Failed to NLST: " . $client->response_code() . " " .
4557 $client->response_msg());
4558 }
4559
4560 $buf = '';
4561 $conn->read($buf, 8192, 25);
4562 eval { $conn->close() };
4563
4564 if ($ENV{TEST_VERBOSE}) {
4565 print STDERR "# response:\n$buf\n";
4566 }
4567
4568 # We have to be careful of the fact that readdir returns directory
4569 # entries in an unordered fashion.
4570 $res = {};
4571 $names = [split(/\n/, $buf)];
4572 foreach my $name (@$names) {
4573 $res->{$name} = 1;
4574 }
4575
4576 $self->assert(scalar(@$names) > 0,
4577 test_msg("Expected multiple names, got 0"));
4578
4579 $expected = {
4580 './.test.d/TEST0000.dat' => 1,
4581 './.test.d/TEST0001.dat' => 1,
4582 './.test.d/TEST0002.dat' => 1,
4583 './.test.d/TEST0003.dat' => 1,
4584 './.test.d/TEST0004.dat' => 1,
4585 './.test.d/TEST0005.dat' => 1,
4586 './.test.d/TEST0006.dat' => 1,
4587 './.test.d/TEST0007.dat' => 1,
4588 './.test.d/TEST0008.dat' => 1,
4589 './.test.d/TEST0009.dat' => 1,
4590 };
4591
4592 $ok = 1;
4593 $mismatch = '';
4594 foreach my $name (keys(%$res)) {
4595 unless (defined($expected->{$name})) {
4596 $mismatch = $name;
4597 $ok = 0;
4598 last;
4599 }
4600 }
4601
4602 unless ($ok) {
4603 die("Unexpected name '$mismatch' appeared in NLST data")
4604 }
4605
4606 $client->quit();
4607 };
4608 if ($@) {
4609 $ex = $@;
4610 }
4611
4612 $wfh->print("done\n");
4613 $wfh->flush();
4614
4615 } else {
4616 eval { server_wait($setup->{config_file}, $rfh) };
4617 if ($@) {
4618 warn($@);
4619 exit 1;
4620 }
4621
4622 exit 0;
4623 }
4624
4625 # Stop server
4626 server_stop($setup->{pid_file});
4627 $self->assert_child_ok($pid);
4628
4629 test_cleanup($setup->{log_file}, $ex);
4630 }
4631
4632 1;