Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::DisplayChdir;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
8 use File::Spec;
9 use IO::Handle;
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 displaychdir_singleline => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 displaychdir_multiline => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 displaychdir_first_chdir => {
30 order => ++$order,
31 test_class => [qw(bug forking)],
32 },
33
34 displaychdir_abs_path_issue1688 => {
35 order => ++$order,
36 test_class => [qw(bug forking)],
37 },
38
39 displaychdir_abs_path_chrooted_issue1688 => {
40 order => ++$order,
41 test_class => [qw(bug forking rootprivs)],
42 },
43
44 displaychdir_first_chdir_abs_path_chrooted_issue1688 => {
45 order => ++$order,
46 test_class => [qw(bug forking rootprivs)],
47 },
48
49 };
50
51 sub new {
52 return shift()->SUPER::new(@_);
53 }
54
55 sub list_tests {
56 return testsuite_get_runnable_tests($TESTS);
57 }
58
59 sub displaychdir_singleline {
60 my $self = shift;
61 my $tmpdir = $self->{tmpdir};
62 my $setup = test_setup($tmpdir, 'config');
63
64 my $sub_dir = File::Spec->rel2abs("$setup->{home_dir}/subdir");
65 mkpath($sub_dir);
66
67 # Make sure that, if we're running as root, that the test directory has
68 # permissions/privs set for the account we create
69 if ($< == 0) {
70 unless (chmod(0755, $sub_dir)) {
71 die("Can't set perms on $sub_dir to 0755: $!");
72 }
73
74 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
75 die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
76 }
77 }
78
79 my $display_file = File::Spec->rel2abs("$sub_dir/display.txt");
80 if (open(my $fh, "> $display_file")) {
81 print $fh "Hello user!\n";
82
83 unless (close($fh)) {
84 die("Unable to write $display_file: $!");
85 }
86
87 } else {
88 die("Unable to open $display_file: $!");
89 }
90
91 my $config = {
92 PidFile => $setup->{pid_file},
93 ScoreboardFile => $setup->{scoreboard_file},
94 SystemLog => $setup->{log_file},
95
96 AuthUserFile => $setup->{auth_user_file},
97 AuthGroupFile => $setup->{auth_group_file},
98 AuthOrder => 'mod_auth_file.c',
99
100 DisplayChdir => 'display.txt',
101
102 IfModules => {
103 'mod_delay.c' => {
104 DelayEngine => 'off',
105 },
106 },
107 };
108
109 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
110 $config);
111
112 # Open pipes, for use between the parent and child processes. Specifically,
113 # the child will indicate when it's done with its test by writing a message
114 # to the parent.
115 my ($rfh, $wfh);
116 unless (pipe($rfh, $wfh)) {
117 die("Can't open pipe: $!");
118 }
119
120 my $ex;
121
122 # Fork child
123 $self->handle_sigchld();
124 defined(my $pid = fork()) or die("Can't fork: $!");
125 if ($pid) {
126 eval {
127 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
128 $client->login($setup->{user}, $setup->{passwd});
129
130 $client->cwd('subdir');
131
132 my $resp_code = $client->response_code();
133 my $resp_msg = $client->response_msg(0);
134
135 my $expected = 250;
136 $self->assert($expected == $resp_code,
137 test_msg("Expected response code $expected, got $resp_code"));
138
139 $expected = "Hello user!";
140 $self->assert($expected eq $resp_msg,
141 test_msg("Expected response message '$expected', got '$resp_msg'"));
142
143 $client->quit();
144
145 $resp_code = $client->response_code();
146 $resp_msg = $client->response_msg();
147
148 $expected = 221;
149 $self->assert($expected == $resp_code,
150 test_msg("Expected response code $expected, got $resp_code"));
151
152 $expected = "Goodbye.";
153 $self->assert($expected eq $resp_msg,
154 test_msg("Expected response message '$expected', got '$resp_msg'"));
155 };
156 if ($@) {
157 $ex = $@;
158 }
159
160 $wfh->print("done\n");
161 $wfh->flush();
162
163 } else {
164 eval { server_wait($setup->{config_file}, $rfh) };
165 if ($@) {
166 warn($@);
167 exit 1;
168 }
169
170 exit 0;
171 }
172
173 # Stop server
174 server_stop($setup->{pid_file});
175 $self->assert_child_ok($pid);
176
177 test_cleanup($setup->{log_file}, $ex);
178 }
179
180 sub displaychdir_multiline {
181 my $self = shift;
182 my $tmpdir = $self->{tmpdir};
183 my $setup = test_setup($tmpdir, 'config');
184
185 my $sub_dir = File::Spec->rel2abs("$setup->{home_dir}/subdir");
186 mkpath($sub_dir);
187
188 # Make sure that, if we're running as root, that the test directory has
189 # permissions/privs set for the account we create
190 if ($< == 0) {
191 unless (chmod(0755, $sub_dir)) {
192 die("Can't set perms on $sub_dir to 0755: $!");
193 }
194
195 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
196 die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
197 }
198 }
199
200 my $display_file = File::Spec->rel2abs("$sub_dir/display.txt");
201 if (open(my $fh, "> $display_file")) {
202 print $fh "Velkommen, Bienvenue\nDoor's open, come on in\nHello user!\n";
203
204 unless (close($fh)) {
205 die("Unable to write $display_file: $!");
206 }
207
208 } else {
209 die("Unable to open $display_file: $!");
210 }
211
212 my $config = {
213 PidFile => $setup->{pid_file},
214 ScoreboardFile => $setup->{scoreboard_file},
215 SystemLog => $setup->{log_file},
216
217 AuthUserFile => $setup->{auth_user_file},
218 AuthGroupFile => $setup->{auth_group_file},
219 AuthOrder => 'mod_auth_file.c',
220
221 DisplayChdir => 'display.txt',
222
223 IfModules => {
224 'mod_delay.c' => {
225 DelayEngine => 'off',
226 },
227 },
228 };
229
230 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
231 $config);
232
233 # Open pipes, for use between the parent and child processes. Specifically,
234 # the child will indicate when it's done with its test by writing a message
235 # to the parent.
236 my ($rfh, $wfh);
237 unless (pipe($rfh, $wfh)) {
238 die("Can't open pipe: $!");
239 }
240
241 my $ex;
242
243 # Fork child
244 $self->handle_sigchld();
245 defined(my $pid = fork()) or die("Can't fork: $!");
246 if ($pid) {
247 eval {
248 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
249 $client->login($setup->{user}, $setup->{passwd});
250
251 $client->cwd('subdir');
252
253 my $resp_code = $client->response_code();
254 my $resp_msg = $client->response_msg(2);
255
256 my $expected = 250;
257 $self->assert($expected == $resp_code,
258 test_msg("Expected response code $expected, got $resp_code"));
259
260 $expected = " Hello user!";
261 $self->assert($expected eq $resp_msg,
262 test_msg("Expected response message '$expected', got '$resp_msg'"));
263
264 $client->quit();
265
266 $resp_code = $client->response_code();
267 $resp_msg = $client->response_msg();
268
269 $expected = 221;
270 $self->assert($expected == $resp_code,
271 test_msg("Expected response code $expected, got $resp_code"));
272
273 $expected = "Goodbye.";
274 $self->assert($expected eq $resp_msg,
275 test_msg("Expected response message '$expected', got '$resp_msg'"));
276 };
277 if ($@) {
278 $ex = $@;
279 }
280
281 $wfh->print("done\n");
282 $wfh->flush();
283
284 } else {
285 eval { server_wait($setup->{config_file}, $rfh) };
286 if ($@) {
287 warn($@);
288 exit 1;
289 }
290
291 exit 0;
292 }
293
294 # Stop server
295 server_stop($setup->{pid_file});
296 $self->assert_child_ok($pid);
297
298 test_cleanup($setup->{log_file}, $ex);
299 }
300
301 sub displaychdir_first_chdir {
302 my $self = shift;
303 my $tmpdir = $self->{tmpdir};
304 my $setup = test_setup($tmpdir, 'config');
305
306 my $sub_dir = File::Spec->rel2abs("$setup->{home_dir}/subdir");
307 mkpath($sub_dir);
308
309 # Make sure that, if we're running as root, that the test directory has
310 # permissions/privs set for the account we create
311 if ($< == 0) {
312 unless (chmod(0755, $sub_dir)) {
313 die("Can't set perms on $sub_dir to 0755: $!");
314 }
315
316 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
317 die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
318 }
319 }
320
321 my $display_file = File::Spec->rel2abs("$sub_dir/display.txt");
322 if (open(my $fh, "> $display_file")) {
323 print $fh "Hello user!\n";
324
325 unless (close($fh)) {
326 die("Unable to write $display_file: $!");
327 }
328
329 } else {
330 die("Unable to open $display_file: $!");
331 }
332
333 my $config = {
334 PidFile => $setup->{pid_file},
335 ScoreboardFile => $setup->{scoreboard_file},
336 SystemLog => $setup->{log_file},
337
338 AuthUserFile => $setup->{auth_user_file},
339 AuthGroupFile => $setup->{auth_group_file},
340 AuthOrder => 'mod_auth_file.c',
341
342 DisplayChdir => "display.txt true",
343
344 IfModules => {
345 'mod_delay.c' => {
346 DelayEngine => 'off',
347 },
348 },
349 };
350
351 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
352 $config);
353
354 # Open pipes, for use between the parent and child processes. Specifically,
355 # the child will indicate when it's done with its test by writing a message
356 # to the parent.
357 my ($rfh, $wfh);
358 unless (pipe($rfh, $wfh)) {
359 die("Can't open pipe: $!");
360 }
361
362 my $ex;
363
364 # Fork child
365 $self->handle_sigchld();
366 defined(my $pid = fork()) or die("Can't fork: $!");
367 if ($pid) {
368 eval {
369 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
370 $client->login($setup->{user}, $setup->{passwd});
371
372 $client->cwd('subdir');
373
374 my $resp_code = $client->response_code();
375 my $resp_msg = $client->response_msg(0);
376
377 my $expected = 250;
378 $self->assert($expected == $resp_code,
379 test_msg("Expected response code $expected, got $resp_code"));
380
381 $expected = "Hello user!";
382 $self->assert($expected eq $resp_msg,
383 test_msg("Expected response message '$expected', got '$resp_msg'"));
384
385 $client->cwd('..');
386 $client->cwd('subdir');
387
388 $resp_code = $client->response_code();
389 $resp_msg = $client->response_msg(0);
390
391 $expected = 250;
392 $self->assert($expected == $resp_code,
393 test_msg("Expected response code $expected, got $resp_code"));
394
395 $expected = "CWD command successful";
396 $self->assert($expected eq $resp_msg,
397 test_msg("Expected response message '$expected', got '$resp_msg'"));
398
399 $client->quit();
400 };
401 if ($@) {
402 $ex = $@;
403 }
404
405 $wfh->print("done\n");
406 $wfh->flush();
407
408 } else {
409 eval { server_wait($setup->{config_file}, $rfh) };
410 if ($@) {
411 warn($@);
412 exit 1;
413 }
414
415 exit 0;
416 }
417
418 # Stop server
419 server_stop($setup->{pid_file});
420 $self->assert_child_ok($pid);
421
422 test_cleanup($setup->{log_file}, $ex);
423 }
424
425 sub displaychdir_abs_path_issue1688 {
426 my $self = shift;
427 my $tmpdir = $self->{tmpdir};
428 my $setup = test_setup($tmpdir, 'config');
429
430 my $sub_dir = File::Spec->rel2abs("$setup->{home_dir}/subdir");
431 mkpath($sub_dir);
432
433 # Make sure that, if we're running as root, that the test directory has
434 # permissions/privs set for the account we create
435 if ($< == 0) {
436 unless (chmod(0755, $sub_dir)) {
437 die("Can't set perms on $sub_dir to 0755: $!");
438 }
439
440 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
441 die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
442 }
443 }
444
445 my $display_file = File::Spec->rel2abs("$sub_dir/display.txt");
446 if (open(my $fh, "> $display_file")) {
447 print $fh "Hello user!\n";
448
449 unless (close($fh)) {
450 die("Unable to write $display_file: $!");
451 }
452
453 } else {
454 die("Unable to open $display_file: $!");
455 }
456
457 my $config = {
458 PidFile => $setup->{pid_file},
459 ScoreboardFile => $setup->{scoreboard_file},
460 SystemLog => $setup->{log_file},
461
462 AuthUserFile => $setup->{auth_user_file},
463 AuthGroupFile => $setup->{auth_group_file},
464 AuthOrder => 'mod_auth_file.c',
465
466 DisplayChdir => $display_file,
467
468 IfModules => {
469 'mod_delay.c' => {
470 DelayEngine => 'off',
471 },
472 },
473 };
474
475 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
476 $config);
477
478 # Open pipes, for use between the parent and child processes. Specifically,
479 # the child will indicate when it's done with its test by writing a message
480 # to the parent.
481 my ($rfh, $wfh);
482 unless (pipe($rfh, $wfh)) {
483 die("Can't open pipe: $!");
484 }
485
486 my $ex;
487
488 # Fork child
489 $self->handle_sigchld();
490 defined(my $pid = fork()) or die("Can't fork: $!");
491 if ($pid) {
492 eval {
493 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
494 $client->login($setup->{user}, $setup->{passwd});
495
496 $client->cwd('subdir');
497
498 my $resp_code = $client->response_code();
499 my $resp_msg = $client->response_msg(0);
500
501 my $expected = 250;
502 $self->assert($expected == $resp_code,
503 test_msg("Expected response code $expected, got $resp_code"));
504
505 $expected = "Hello user!";
506 $self->assert($expected eq $resp_msg,
507 test_msg("Expected response message '$expected', got '$resp_msg'"));
508
509 $client->quit();
510
511 $resp_code = $client->response_code();
512 $resp_msg = $client->response_msg();
513
514 $expected = 221;
515 $self->assert($expected == $resp_code,
516 test_msg("Expected response code $expected, got $resp_code"));
517
518 $expected = "Goodbye.";
519 $self->assert($expected eq $resp_msg,
520 test_msg("Expected response message '$expected', got '$resp_msg'"));
521 };
522 if ($@) {
523 $ex = $@;
524 }
525
526 $wfh->print("done\n");
527 $wfh->flush();
528
529 } else {
530 eval { server_wait($setup->{config_file}, $rfh) };
531 if ($@) {
532 warn($@);
533 exit 1;
534 }
535
536 exit 0;
537 }
538
539 # Stop server
540 server_stop($setup->{pid_file});
541 $self->assert_child_ok($pid);
542
543 test_cleanup($setup->{log_file}, $ex);
544 }
545
546 sub displaychdir_abs_path_chrooted_issue1688 {
547 my $self = shift;
548 my $tmpdir = $self->{tmpdir};
549 my $setup = test_setup($tmpdir, 'config');
550
551 my $sub_dir = File::Spec->rel2abs("$setup->{home_dir}/subdir");
552 mkpath($sub_dir);
553
554 # Make sure that, if we're running as root, that the test directory has
555 # permissions/privs set for the account we create
556 if ($< == 0) {
557 unless (chmod(0755, $sub_dir)) {
558 die("Can't set perms on $sub_dir to 0755: $!");
559 }
560
561 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
562 die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
563 }
564 }
565
566 my $display_file = File::Spec->rel2abs("$setup->{home_dir}/display.txt");
567 if (open(my $fh, "> $display_file")) {
568 print $fh "Hello user!\n";
569
570 unless (close($fh)) {
571 die("Unable to write $display_file: $!");
572 }
573
574 } else {
575 die("Unable to open $display_file: $!");
576 }
577
578 my $config = {
579 PidFile => $setup->{pid_file},
580 ScoreboardFile => $setup->{scoreboard_file},
581 SystemLog => $setup->{log_file},
582
583 AuthUserFile => $setup->{auth_user_file},
584 AuthGroupFile => $setup->{auth_group_file},
585 AuthOrder => 'mod_auth_file.c',
586
587 DefaultRoot => '~',
588 DisplayChdir => $display_file,
589
590 IfModules => {
591 'mod_delay.c' => {
592 DelayEngine => 'off',
593 },
594 },
595 };
596
597 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
598 $config);
599
600 # Open pipes, for use between the parent and child processes. Specifically,
601 # the child will indicate when it's done with its test by writing a message
602 # to the parent.
603 my ($rfh, $wfh);
604 unless (pipe($rfh, $wfh)) {
605 die("Can't open pipe: $!");
606 }
607
608 my $ex;
609
610 # Fork child
611 $self->handle_sigchld();
612 defined(my $pid = fork()) or die("Can't fork: $!");
613 if ($pid) {
614 eval {
615 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
616 $client->login($setup->{user}, $setup->{passwd});
617
618 $client->cwd('subdir');
619
620 my $resp_code = $client->response_code();
621 my $resp_msg = $client->response_msg(0);
622
623 my $expected = 250;
624 $self->assert($expected == $resp_code,
625 test_msg("Expected response code $expected, got $resp_code"));
626
627 $expected = "Hello user!";
628 $self->assert($expected eq $resp_msg,
629 test_msg("Expected response message '$expected', got '$resp_msg'"));
630
631 $client->quit();
632
633 $resp_code = $client->response_code();
634 $resp_msg = $client->response_msg();
635
636 $expected = 221;
637 $self->assert($expected == $resp_code,
638 test_msg("Expected response code $expected, got $resp_code"));
639
640 $expected = "Goodbye.";
641 $self->assert($expected eq $resp_msg,
642 test_msg("Expected response message '$expected', got '$resp_msg'"));
643 };
644 if ($@) {
645 $ex = $@;
646 }
647
648 $wfh->print("done\n");
649 $wfh->flush();
650
651 } else {
652 eval { server_wait($setup->{config_file}, $rfh) };
653 if ($@) {
654 warn($@);
655 exit 1;
656 }
657
658 exit 0;
659 }
660
661 # Stop server
662 server_stop($setup->{pid_file});
663 $self->assert_child_ok($pid);
664
665 test_cleanup($setup->{log_file}, $ex);
666 }
667
668 sub displaychdir_first_chdir_abs_path_chrooted_issue1688 {
669 my $self = shift;
670 my $tmpdir = $self->{tmpdir};
671 my $setup = test_setup($tmpdir, 'config');
672
673 my $sub_dir = File::Spec->rel2abs("$setup->{home_dir}/subdir");
674 mkpath($sub_dir);
675
676 # Make sure that, if we're running as root, that the test directory has
677 # permissions/privs set for the account we create
678 if ($< == 0) {
679 unless (chmod(0755, $sub_dir)) {
680 die("Can't set perms on $sub_dir to 0755: $!");
681 }
682
683 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
684 die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
685 }
686 }
687
688 my $display_file = File::Spec->rel2abs("$setup->{home_dir}/display.txt");
689 if (open(my $fh, "> $display_file")) {
690 print $fh "Hello user!\n";
691
692 unless (close($fh)) {
693 die("Unable to write $display_file: $!");
694 }
695
696 } else {
697 die("Unable to open $display_file: $!");
698 }
699
700 my $config = {
701 PidFile => $setup->{pid_file},
702 ScoreboardFile => $setup->{scoreboard_file},
703 SystemLog => $setup->{log_file},
704
705 AuthUserFile => $setup->{auth_user_file},
706 AuthGroupFile => $setup->{auth_group_file},
707 AuthOrder => 'mod_auth_file.c',
708
709 DefaultRoot => '~',
710 DisplayChdir => "$display_file true",
711
712 IfModules => {
713 'mod_delay.c' => {
714 DelayEngine => 'off',
715 },
716 },
717 };
718
719 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
720 $config);
721
722 # Open pipes, for use between the parent and child processes. Specifically,
723 # the child will indicate when it's done with its test by writing a message
724 # to the parent.
725 my ($rfh, $wfh);
726 unless (pipe($rfh, $wfh)) {
727 die("Can't open pipe: $!");
728 }
729
730 my $ex;
731
732 # Fork child
733 $self->handle_sigchld();
734 defined(my $pid = fork()) or die("Can't fork: $!");
735 if ($pid) {
736 eval {
737 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
738 $client->login($setup->{user}, $setup->{passwd});
739
740 $client->cwd('subdir');
741
742 my $resp_code = $client->response_code();
743 my $resp_msg = $client->response_msg(0);
744
745 my $expected = 250;
746 $self->assert($expected == $resp_code,
747 test_msg("Expected response code $expected, got $resp_code"));
748
749 $expected = "Hello user!";
750 $self->assert($expected eq $resp_msg,
751 test_msg("Expected response message '$expected', got '$resp_msg'"));
752
753 $client->cwd('..');
754 $client->cwd('subdir');
755
756 $resp_code = $client->response_code();
757 $resp_msg = $client->response_msg(0);
758
759 $expected = 250;
760 $self->assert($expected == $resp_code,
761 test_msg("Expected response code $expected, got $resp_code"));
762
763 $expected = "CWD command successful";
764 $self->assert($expected eq $resp_msg,
765 test_msg("Expected response message '$expected', got '$resp_msg'"));
766
767 $client->quit();
768
769 $resp_code = $client->response_code();
770 $resp_msg = $client->response_msg();
771
772 $expected = 221;
773 $self->assert($expected == $resp_code,
774 test_msg("Expected response code $expected, got $resp_code"));
775
776 $expected = "Goodbye.";
777 $self->assert($expected eq $resp_msg,
778 test_msg("Expected response message '$expected', got '$resp_msg'"));
779 };
780 if ($@) {
781 $ex = $@;
782 }
783
784 $wfh->print("done\n");
785 $wfh->flush();
786
787 } else {
788 eval { server_wait($setup->{config_file}, $rfh) };
789 if ($@) {
790 warn($@);
791 exit 1;
792 }
793
794 exit 0;
795 }
796
797 # Stop server
798 server_stop($setup->{pid_file});
799 $self->assert_child_ok($pid);
800
801 test_cleanup($setup->{log_file}, $ex);
802 }
803
804 1;