Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_site;
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 :running :test :testsuite);
14
15 $| = 1;
16
17 my $order = 0;
18
19 my $TESTS = {
20 site_help_ok => {
21 order => ++$order,
22 test_class => [qw(forking)],
23 },
24
25 site_help_chgrp_ok => {
26 order => ++$order,
27 test_class => [qw(forking)],
28 },
29
30 site_chrgrp_ok => {
31 order => ++$order,
32 test_class => [qw(forking rootprivs)],
33 },
34
35 site_chrgrp_abs_symlink => {
36 order => ++$order,
37 test_class => [qw(forking rootprivs)],
38 },
39
40 site_chrgrp_abs_symlink_chrooted_bug4219 => {
41 order => ++$order,
42 test_class => [qw(bug forking rootprivs)],
43 },
44
45 site_chrgrp_rel_symlink => {
46 order => ++$order,
47 test_class => [qw(forking rootprivs)],
48 },
49
50 site_chrgrp_rel_symlink_chrooted_bug4219 => {
51 order => ++$order,
52 test_class => [qw(bug forking rootprivs)],
53 },
54
55 # XXX Need more SITE CHGRP tests: invalid group names, paths with spaces,
56 # paths that don't exist, PathAllow/DenyFilter, etc.
57
58 site_chmod_no_login => {
59 order => ++$order,
60 test_class => [qw(forking)],
61 },
62
63 site_chmod_numeric_ok => {
64 order => ++$order,
65 test_class => [qw(forking)],
66 },
67
68 site_chmod_symbolic_ok => {
69 order => ++$order,
70 test_class => [qw(forking)],
71 },
72
73 site_chmod_abs_symlink => {
74 order => ++$order,
75 test_class => [qw(forking)],
76 },
77
78 site_chmod_abs_symlink_chrooted_bug4219 => {
79 order => ++$order,
80 test_class => [qw(bug forking rootprivs)],
81 },
82
83 site_chmod_rel_symlink => {
84 order => ++$order,
85 test_class => [qw(forking)],
86 },
87
88 site_chmod_rel_symlink_chrooted_bug4219 => {
89 order => ++$order,
90 test_class => [qw(bug forking rootprivs)],
91 },
92
93 # XXX Need more SITE CHMOD tests: invalid modes (string and octal),
94 # paths with spaces, paths that don't exist, PathAllow/DenyFilter, etc.
95 };
96
97 sub new {
98 return shift()->SUPER::new(@_);
99 }
100
101 sub list_tests {
102 return testsuite_get_runnable_tests($TESTS);
103 }
104
105 sub site_help_ok {
106 my $self = shift;
107 my $tmpdir = $self->{tmpdir};
108
109 my $config_file = "$tmpdir/site.conf";
110 my $pid_file = File::Spec->rel2abs("$tmpdir/site.pid");
111 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/site.scoreboard");
112
113 my $log_file = File::Spec->rel2abs('tests.log');
114
115 my $auth_user_file = File::Spec->rel2abs("$tmpdir/site.passwd");
116 my $auth_group_file = File::Spec->rel2abs("$tmpdir/site.group");
117
118 my $user = 'proftpd';
119 my $passwd = 'test';
120 my $home_dir = File::Spec->rel2abs($tmpdir);
121 my $uid = 500;
122 my $gid = 500;
123
124 # Make sure that, if we're running as root, that the home directory has
125 # permissions/privs set for the account we create
126 if ($< == 0) {
127 unless (chmod(0755, $home_dir)) {
128 die("Can't set perms on $home_dir to 0755: $!");
129 }
130
131 unless (chown($uid, $gid, $home_dir)) {
132 die("Can't set owner of $home_dir to $uid/$gid: $!");
133 }
134 }
135
136 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
137 '/bin/bash');
138 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
139
140 my $config = {
141 PidFile => $pid_file,
142 ScoreboardFile => $scoreboard_file,
143 SystemLog => $log_file,
144
145 AuthUserFile => $auth_user_file,
146 AuthGroupFile => $auth_group_file,
147 AuthOrder => 'mod_auth_file.c',
148
149 IfModules => {
150 'mod_delay.c' => {
151 DelayEngine => 'off',
152 },
153 },
154 };
155
156 my ($port, $config_user, $config_group) = config_write($config_file, $config);
157
158 # Open pipes, for use between the parent and child processes. Specifically,
159 # the child will indicate when it's done with its test by writing a message
160 # to the parent.
161 my ($rfh, $wfh);
162 unless (pipe($rfh, $wfh)) {
163 die("Can't open pipe: $!");
164 }
165
166 my $ex;
167
168 # Fork child
169 $self->handle_sigchld();
170 defined(my $pid = fork()) or die("Can't fork: $!");
171 if ($pid) {
172 eval {
173 sleep(1);
174 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
175 $client->login($user, $passwd);
176
177 $client->site('HELP');
178
179 my $resp_code = $client->response_code();
180 my $resp_msgs = $client->response_msgs();
181
182 my $expected;
183
184 $expected = 214;
185 $self->assert($expected == $resp_code,
186 test_msg("Expected $expected, got $resp_code"));
187
188 my $help_ok = 0;
189 my $chgrp_ok = 0;
190 my $chmod_ok = 0;
191
192 for (my $i = 0; $i < scalar(@$resp_msgs); $i++) {
193 if ($resp_msgs->[$i] =~ / HELP/) {
194 $help_ok = 1;
195 }
196
197 if ($resp_msgs->[$i] =~ / CHGRP/) {
198 $chgrp_ok = 1;
199 }
200
201 if ($resp_msgs->[$i] =~ / CHMOD/) {
202 $chmod_ok = 1;
203 }
204
205 if ($help_ok &&
206 $chgrp_ok &&
207 $chmod_ok) {
208 last;
209 }
210 }
211
212 $self->assert($help_ok,
213 test_msg("Expected ' HELP' did not appear in HELP response"));
214
215 $self->assert($chgrp_ok,
216 test_msg("Expected ' CHGRP' did not appear in HELP response"));
217
218 $self->assert($chmod_ok,
219 test_msg("Expected ' CHMOD' did not appear in HELP response"));
220 };
221
222 if ($@) {
223 $ex = $@;
224 }
225
226 $wfh->print("done\n");
227 $wfh->flush();
228
229 } else {
230 eval { server_wait($config_file, $rfh) };
231 if ($@) {
232 warn($@);
233 exit 1;
234 }
235
236 exit 0;
237 }
238
239 # Stop server
240 server_stop($pid_file);
241
242 $self->assert_child_ok($pid);
243
244 if ($ex) {
245 die($ex);
246 }
247
248 unlink($log_file);
249 }
250
251 sub site_help_chgrp_ok {
252 my $self = shift;
253 my $tmpdir = $self->{tmpdir};
254
255 my $config_file = "$tmpdir/site.conf";
256 my $pid_file = File::Spec->rel2abs("$tmpdir/site.pid");
257 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/site.scoreboard");
258
259 my $log_file = File::Spec->rel2abs('tests.log');
260
261 my $auth_user_file = File::Spec->rel2abs("$tmpdir/site.passwd");
262 my $auth_group_file = File::Spec->rel2abs("$tmpdir/site.group");
263
264 my $user = 'proftpd';
265 my $passwd = 'test';
266 my $home_dir = File::Spec->rel2abs($tmpdir);
267 my $uid = 500;
268 my $gid = 500;
269
270 # Make sure that, if we're running as root, that the home directory has
271 # permissions/privs set for the account we create
272 if ($< == 0) {
273 unless (chmod(0755, $home_dir)) {
274 die("Can't set perms on $home_dir to 0755: $!");
275 }
276
277 unless (chown($uid, $gid, $home_dir)) {
278 die("Can't set owner of $home_dir to $uid/$gid: $!");
279 }
280 }
281
282 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
283 '/bin/bash');
284 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
285
286 my $config = {
287 PidFile => $pid_file,
288 ScoreboardFile => $scoreboard_file,
289 SystemLog => $log_file,
290
291 AuthUserFile => $auth_user_file,
292 AuthGroupFile => $auth_group_file,
293 AuthOrder => 'mod_auth_file.c',
294
295 IfModules => {
296 'mod_delay.c' => {
297 DelayEngine => 'off',
298 },
299 },
300 };
301
302 my ($port, $config_user, $config_group) = config_write($config_file, $config);
303
304 # Open pipes, for use between the parent and child processes. Specifically,
305 # the child will indicate when it's done with its test by writing a message
306 # to the parent.
307 my ($rfh, $wfh);
308 unless (pipe($rfh, $wfh)) {
309 die("Can't open pipe: $!");
310 }
311
312 my $ex;
313
314 # Fork child
315 $self->handle_sigchld();
316 defined(my $pid = fork()) or die("Can't fork: $!");
317 if ($pid) {
318 eval {
319 sleep(1);
320 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
321 $client->login($user, $passwd);
322
323 $client->site('HELP', 'CHGRP');
324
325 my $resp_code = $client->response_code();
326 my $resp_msgs = $client->response_msgs();
327
328 my $expected;
329
330 $expected = 214;
331 $self->assert($expected == $resp_code,
332 test_msg("Expected $expected, got $resp_code"));
333
334 my $chgrp_ok = 0;
335
336 for (my $i = 0; $i < scalar(@$resp_msgs); $i++) {
337
338 if ($resp_msgs->[$i] =~ /Syntax: SITE CHGRP/) {
339 $chgrp_ok = 1;
340 last;
341 }
342 }
343
344 $self->assert($chgrp_ok,
345 test_msg("Expected 'Syntax: SITE CHGRP' did not appear in HELP response"));
346 };
347
348 if ($@) {
349 $ex = $@;
350 }
351
352 $wfh->print("done\n");
353 $wfh->flush();
354
355 } else {
356 eval { server_wait($config_file, $rfh) };
357 if ($@) {
358 warn($@);
359 exit 1;
360 }
361
362 exit 0;
363 }
364
365 # Stop server
366 server_stop($pid_file);
367
368 $self->assert_child_ok($pid);
369
370 if ($ex) {
371 die($ex);
372 }
373
374 unlink($log_file);
375 }
376
377 sub site_chgrp_ok {
378 my $self = shift;
379 my $tmpdir = $self->{tmpdir};
380 my $setup = test_setup($tmpdir, 'site');
381
382 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
383 if (open(my $fh, "> $test_file")) {
384 close($fh);
385
386 } else {
387 die("Can't open $test_file: $!");
388 }
389
390 if ($< == 0) {
391 unless (chown($setup->{uid}, $setup->{gid}, $test_file)) {
392 die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!");
393 }
394 }
395
396 my $config = {
397 PidFile => $setup->{pid_file},
398 ScoreboardFile => $setup->{scoreboard_file},
399 SystemLog => $setup->{log_file},
400 TraceLog => $setup->{log_file},
401 Trace => 'fileperms:10',
402
403 AuthUserFile => $setup->{auth_user_file},
404 AuthGroupFile => $setup->{auth_group_file},
405 AuthOrder => 'mod_auth_file.c',
406
407 IfModules => {
408 'mod_delay.c' => {
409 DelayEngine => 'off',
410 },
411 },
412 };
413
414 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
415 $config);
416
417 # Open pipes, for use between the parent and child processes. Specifically,
418 # the child will indicate when it's done with its test by writing a message
419 # to the parent.
420 my ($rfh, $wfh);
421 unless (pipe($rfh, $wfh)) {
422 die("Can't open pipe: $!");
423 }
424
425 my $ex;
426
427 # Fork child
428 $self->handle_sigchld();
429 defined(my $pid = fork()) or die("Can't fork: $!");
430 if ($pid) {
431 eval {
432 sleep(1);
433 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
434 $client->login($setup->{user}, $setup->{passwd});
435
436 my $path = 'test.txt';
437 $client->site('CHGRP', "$setup->{group} $path");
438
439 my $resp_code = $client->response_code();
440 my $resp_msg = $client->response_msg();
441 $client->quit();
442
443 my $expected = 200;
444 $self->assert($expected == $resp_code,
445 test_msg("Expected response code $expected, got $resp_code"));
446
447 $expected = 'SITE CHGRP command successful';
448 $self->assert($expected eq $resp_msg,
449 test_msg("Expected response message '$expected', got '$resp_msg'"));
450 };
451 if ($@) {
452 $ex = $@;
453 }
454
455 $wfh->print("done\n");
456 $wfh->flush();
457
458 } else {
459 eval { server_wait($setup->{config_file}, $rfh) };
460 if ($@) {
461 warn($@);
462 exit 1;
463 }
464
465 exit 0;
466 }
467
468 # Stop server
469 server_stop($setup->{pid_file});
470 $self->assert_child_ok($pid);
471
472 test_cleanup($setup->{log_file}, $ex);
473 }
474
475 sub site_chgrp_abs_symlink {
476 my $self = shift;
477 my $tmpdir = $self->{tmpdir};
478 my $setup = test_setup($tmpdir, 'site');
479
480 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
481 mkpath($test_dir);
482
483 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
484 if (open(my $fh, "> $test_file")) {
485 close($fh);
486
487 } else {
488 die("Can't open $test_file: $!");
489 }
490
491 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
492
493 my $dst_path = $test_file;
494 if ($^O eq 'darwin') {
495 # MacOSX-specific hack
496 $dst_path = '/private' . $dst_path;
497 }
498
499 unless (symlink($dst_path, $test_symlink)) {
500 die("Can't symlink $test_symlink to $dst_path: $!");
501 }
502
503 if ($< == 0) {
504 unless (chown($setup->{uid}, 0, $test_file)) {
505 die("Can't set owner of $test_file to $setup->{uid}/0: $!");
506 }
507 }
508
509 my $config = {
510 PidFile => $setup->{pid_file},
511 ScoreboardFile => $setup->{scoreboard_file},
512 SystemLog => $setup->{log_file},
513 TraceLog => $setup->{log_file},
514 Trace => 'fileperms:10',
515
516 AuthUserFile => $setup->{auth_user_file},
517 AuthGroupFile => $setup->{auth_group_file},
518 AuthOrder => 'mod_auth_file.c',
519
520 IfModules => {
521 'mod_delay.c' => {
522 DelayEngine => 'off',
523 },
524 },
525 };
526
527 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
528 $config);
529
530 # Open pipes, for use between the parent and child processes. Specifically,
531 # the child will indicate when it's done with its test by writing a message
532 # to the parent.
533 my ($rfh, $wfh);
534 unless (pipe($rfh, $wfh)) {
535 die("Can't open pipe: $!");
536 }
537
538 my $ex;
539
540 # Fork child
541 $self->handle_sigchld();
542 defined(my $pid = fork()) or die("Can't fork: $!");
543 if ($pid) {
544 eval {
545 sleep(1);
546 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
547 $client->login($setup->{user}, $setup->{passwd});
548
549 my $path = 'test.d/test.lnk';
550 $client->site('CHGRP', "$setup->{group} $path");
551
552 my $resp_code = $client->response_code();
553 my $resp_msg = $client->response_msg();
554 $client->quit();
555
556 my $expected = 200;
557 $self->assert($expected == $resp_code,
558 test_msg("Expected response code $expected, got $resp_code"));
559
560 $expected = 'SITE CHGRP command successful';
561 $self->assert($expected eq $resp_msg,
562 test_msg("Expected response message '$expected', got '$resp_msg'"));
563
564 my $file_gid = (stat($test_file))[5];
565 $expected = $setup->{gid};
566 $self->assert($expected == $file_gid,
567 test_msg("Expected file GID $expected, got $file_gid"));
568 };
569 if ($@) {
570 $ex = $@;
571 }
572
573 $wfh->print("done\n");
574 $wfh->flush();
575
576 } else {
577 eval { server_wait($setup->{config_file}, $rfh) };
578 if ($@) {
579 warn($@);
580 exit 1;
581 }
582
583 exit 0;
584 }
585
586 # Stop server
587 server_stop($setup->{pid_file});
588 $self->assert_child_ok($pid);
589
590 test_cleanup($setup->{log_file}, $ex);
591 }
592
593 sub site_chgrp_abs_symlink_chrooted_bug4219 {
594 my $self = shift;
595 my $tmpdir = $self->{tmpdir};
596 my $setup = test_setup($tmpdir, 'site');
597
598 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
599 mkpath($test_dir);
600
601 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
602 if (open(my $fh, "> $test_file")) {
603 close($fh);
604
605 } else {
606 die("Can't open $test_file: $!");
607 }
608
609 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
610
611 my $dst_path = $test_file;
612 if ($^O eq 'darwin') {
613 # MacOSX-specific hack
614 $dst_path = '/private' . $dst_path;
615 }
616
617 unless (symlink($dst_path, $test_symlink)) {
618 die("Can't symlink $test_symlink to $dst_path: $!");
619 }
620
621 if ($< == 0) {
622 unless (chown($setup->{uid}, 0, $test_file)) {
623 die("Can't set owner of $test_file to $setup->{uid}/0: $!");
624 }
625 }
626
627 my $config = {
628 PidFile => $setup->{pid_file},
629 ScoreboardFile => $setup->{scoreboard_file},
630 SystemLog => $setup->{log_file},
631 TraceLog => $setup->{log_file},
632 Trace => 'fileperms:10',
633
634 AuthUserFile => $setup->{auth_user_file},
635 AuthGroupFile => $setup->{auth_group_file},
636 AuthOrder => 'mod_auth_file.c',
637
638 DefaultRoot => '~',
639
640 IfModules => {
641 'mod_delay.c' => {
642 DelayEngine => 'off',
643 },
644 },
645 };
646
647 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
648 $config);
649
650 # Open pipes, for use between the parent and child processes. Specifically,
651 # the child will indicate when it's done with its test by writing a message
652 # to the parent.
653 my ($rfh, $wfh);
654 unless (pipe($rfh, $wfh)) {
655 die("Can't open pipe: $!");
656 }
657
658 my $ex;
659
660 # Fork child
661 $self->handle_sigchld();
662 defined(my $pid = fork()) or die("Can't fork: $!");
663 if ($pid) {
664 eval {
665 sleep(1);
666 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
667 $client->login($setup->{user}, $setup->{passwd});
668
669 my $path = 'test.d/test.lnk';
670 $client->site('CHGRP', "$setup->{group} $path");
671
672 my $resp_code = $client->response_code();
673 my $resp_msg = $client->response_msg();
674 $client->quit();
675
676 my $expected = 200;
677 $self->assert($expected == $resp_code,
678 test_msg("Expected response code $expected, got $resp_code"));
679
680 $expected = 'SITE CHGRP command successful';
681 $self->assert($expected eq $resp_msg,
682 test_msg("Expected response message '$expected', got '$resp_msg'"));
683
684 my $file_gid = (stat($test_file))[5];
685 $expected = $setup->{gid};
686 $self->assert($expected == $file_gid,
687 test_msg("Expected file GID $expected, got $file_gid"));
688 };
689 if ($@) {
690 $ex = $@;
691 }
692
693 $wfh->print("done\n");
694 $wfh->flush();
695
696 } else {
697 eval { server_wait($setup->{config_file}, $rfh) };
698 if ($@) {
699 warn($@);
700 exit 1;
701 }
702
703 exit 0;
704 }
705
706 # Stop server
707 server_stop($setup->{pid_file});
708 $self->assert_child_ok($pid);
709
710 test_cleanup($setup->{log_file}, $ex);
711 }
712
713 sub site_chgrp_rel_symlink {
714 my $self = shift;
715 my $tmpdir = $self->{tmpdir};
716 my $setup = test_setup($tmpdir, 'site');
717
718 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
719 mkpath($test_dir);
720
721 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
722 if (open(my $fh, "> $test_file")) {
723 close($fh);
724
725 } else {
726 die("Can't open $test_file: $!");
727 }
728
729 # Change to the test directory in order to create a relative path in the
730 # symlink we need
731
732 my $cwd = getcwd();
733 unless (chdir($test_dir)) {
734 die("Can't chdir to $test_dir: $!");
735 }
736
737 unless (symlink('./test.txt', './test.lnk')) {
738 die("Can't symlink 'test.lnk' to './test.txt': $!");
739 }
740
741 unless (chdir($cwd)) {
742 die("Can't chdir to $cwd: $!");
743 }
744
745 if ($< == 0) {
746 unless (chown($setup->{uid}, 0, $test_file)) {
747 die("Can't set owner of $test_file to $setup->{uid}/0: $!");
748 }
749 }
750
751 my $config = {
752 PidFile => $setup->{pid_file},
753 ScoreboardFile => $setup->{scoreboard_file},
754 SystemLog => $setup->{log_file},
755 TraceLog => $setup->{log_file},
756 Trace => 'fileperms:10',
757
758 AuthUserFile => $setup->{auth_user_file},
759 AuthGroupFile => $setup->{auth_group_file},
760 AuthOrder => 'mod_auth_file.c',
761
762 IfModules => {
763 'mod_delay.c' => {
764 DelayEngine => 'off',
765 },
766 },
767 };
768
769 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
770 $config);
771
772 # Open pipes, for use between the parent and child processes. Specifically,
773 # the child will indicate when it's done with its test by writing a message
774 # to the parent.
775 my ($rfh, $wfh);
776 unless (pipe($rfh, $wfh)) {
777 die("Can't open pipe: $!");
778 }
779
780 my $ex;
781
782 # Fork child
783 $self->handle_sigchld();
784 defined(my $pid = fork()) or die("Can't fork: $!");
785 if ($pid) {
786 eval {
787 sleep(1);
788 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
789 $client->login($setup->{user}, $setup->{passwd});
790
791 my $path = 'test.d/test.lnk';
792 $client->site('CHGRP', "$setup->{group} $path");
793
794 my $resp_code = $client->response_code();
795 my $resp_msg = $client->response_msg();
796 $client->quit();
797
798 my $expected = 200;
799 $self->assert($expected == $resp_code,
800 test_msg("Expected response code $expected, got $resp_code"));
801
802 $expected = 'SITE CHGRP command successful';
803 $self->assert($expected eq $resp_msg,
804 test_msg("Expected response message '$expected', got '$resp_msg'"));
805
806 my $file_gid = (stat($test_file))[5];
807 $expected = $setup->{gid};
808 $self->assert($expected == $file_gid,
809 test_msg("Expected file GID $expected, got $file_gid"));
810 };
811 if ($@) {
812 $ex = $@;
813 }
814
815 $wfh->print("done\n");
816 $wfh->flush();
817
818 } else {
819 eval { server_wait($setup->{config_file}, $rfh) };
820 if ($@) {
821 warn($@);
822 exit 1;
823 }
824
825 exit 0;
826 }
827
828 # Stop server
829 server_stop($setup->{pid_file});
830 $self->assert_child_ok($pid);
831
832 test_cleanup($setup->{log_file}, $ex);
833 }
834
835 sub site_chgrp_rel_symlink_chrooted_bug4219 {
836 my $self = shift;
837 my $tmpdir = $self->{tmpdir};
838 my $setup = test_setup($tmpdir, 'site');
839
840 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
841 mkpath($test_dir);
842
843 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
844 if (open(my $fh, "> $test_file")) {
845 close($fh);
846
847 } else {
848 die("Can't open $test_file: $!");
849 }
850
851 # Change to the test directory in order to create a relative path in the
852 # symlink we need
853
854 my $cwd = getcwd();
855 unless (chdir($test_dir)) {
856 die("Can't chdir to $test_dir: $!");
857 }
858
859 unless (symlink('./test.txt', './test.lnk')) {
860 die("Can't symlink 'test.lnk' to './test.txt': $!");
861 }
862
863 unless (chdir($cwd)) {
864 die("Can't chdir to $cwd: $!");
865 }
866
867 if ($< == 0) {
868 unless (chown($setup->{uid}, 0, $test_file)) {
869 die("Can't set owner of $test_file to $setup->{uid}/0: $!");
870 }
871 }
872
873 my $config = {
874 PidFile => $setup->{pid_file},
875 ScoreboardFile => $setup->{scoreboard_file},
876 SystemLog => $setup->{log_file},
877 TraceLog => $setup->{log_file},
878 Trace => 'fileperms:10',
879
880 AuthUserFile => $setup->{auth_user_file},
881 AuthGroupFile => $setup->{auth_group_file},
882 AuthOrder => 'mod_auth_file.c',
883
884 DefaultRoot => '~',
885
886 IfModules => {
887 'mod_delay.c' => {
888 DelayEngine => 'off',
889 },
890 },
891 };
892
893 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
894 $config);
895
896 # Open pipes, for use between the parent and child processes. Specifically,
897 # the child will indicate when it's done with its test by writing a message
898 # to the parent.
899 my ($rfh, $wfh);
900 unless (pipe($rfh, $wfh)) {
901 die("Can't open pipe: $!");
902 }
903
904 my $ex;
905
906 # Fork child
907 $self->handle_sigchld();
908 defined(my $pid = fork()) or die("Can't fork: $!");
909 if ($pid) {
910 eval {
911 sleep(1);
912 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
913 $client->login($setup->{user}, $setup->{passwd});
914
915 my $path = 'test.d/test.lnk';
916 $client->site('CHGRP', "$setup->{group} $path");
917
918 my $resp_code = $client->response_code();
919 my $resp_msg = $client->response_msg();
920 $client->quit();
921
922 my $expected = 200;
923 $self->assert($expected == $resp_code,
924 test_msg("Expected response code $expected, got $resp_code"));
925
926 $expected = 'SITE CHGRP command successful';
927 $self->assert($expected eq $resp_msg,
928 test_msg("Expected response message '$expected', got '$resp_msg'"));
929
930 my $file_gid = (stat($test_file))[5];
931 $expected = $setup->{gid};
932 $self->assert($expected == $file_gid,
933 test_msg("Expected file GID $expected, got $file_gid"));
934 };
935 if ($@) {
936 $ex = $@;
937 }
938
939 $wfh->print("done\n");
940 $wfh->flush();
941
942 } else {
943 eval { server_wait($setup->{config_file}, $rfh) };
944 if ($@) {
945 warn($@);
946 exit 1;
947 }
948
949 exit 0;
950 }
951
952 # Stop server
953 server_stop($setup->{pid_file});
954 $self->assert_child_ok($pid);
955
956 test_cleanup($setup->{log_file}, $ex);
957 }
958
959 sub site_chmod_no_login {
960 my $self = shift;
961 my $tmpdir = $self->{tmpdir};
962
963 my $config_file = "$tmpdir/site.conf";
964 my $pid_file = File::Spec->rel2abs("$tmpdir/site.pid");
965 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/site.scoreboard");
966
967 my $log_file = File::Spec->rel2abs('tests.log');
968
969 my $auth_user_file = File::Spec->rel2abs("$tmpdir/site.passwd");
970 my $auth_group_file = File::Spec->rel2abs("$tmpdir/site.group");
971
972 my $user = 'proftpd';
973 my $passwd = 'test';
974 my $group = 'ftpd';
975 my $home_dir = File::Spec->rel2abs($tmpdir);
976 my $uid = 500;
977 my $gid = 500;
978
979 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
980 if (open(my $fh, "> $test_file")) {
981 close($fh);
982
983 } else {
984 die("Can't open $test_file: $!");
985 }
986
987 # Make sure that, if we're running as root, that the home directory has
988 # permissions/privs set for the account we create
989 if ($< == 0) {
990 unless (chmod(0755, $home_dir)) {
991 die("Can't set perms on $home_dir to 0755: $!");
992 }
993
994 unless (chown($uid, $gid, $home_dir, $test_file)) {
995 die("Can't set owner of $home_dir to $uid/$gid: $!");
996 }
997 }
998
999 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1000 '/bin/bash');
1001 auth_group_write($auth_group_file, $group, $gid, $user);
1002
1003 my $config = {
1004 PidFile => $pid_file,
1005 ScoreboardFile => $scoreboard_file,
1006 SystemLog => $log_file,
1007 TraceLog => $log_file,
1008 Trace => 'fileperms:10',
1009
1010 AuthUserFile => $auth_user_file,
1011 AuthGroupFile => $auth_group_file,
1012 AuthOrder => 'mod_auth_file.c',
1013
1014 IfModules => {
1015 'mod_delay.c' => {
1016 DelayEngine => 'off',
1017 },
1018 },
1019 };
1020
1021 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1022
1023 # Open pipes, for use between the parent and child processes. Specifically,
1024 # the child will indicate when it's done with its test by writing a message
1025 # to the parent.
1026 my ($rfh, $wfh);
1027 unless (pipe($rfh, $wfh)) {
1028 die("Can't open pipe: $!");
1029 }
1030
1031 my $ex;
1032
1033 # Fork child
1034 $self->handle_sigchld();
1035 defined(my $pid = fork()) or die("Can't fork: $!");
1036 if ($pid) {
1037 eval {
1038 sleep(1);
1039 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1040
1041 eval { $client->site('CHMOD', "777 test.txt") };
1042 unless ($@) {
1043 die("SITE CHMOD succeeded unexpectedly");
1044 }
1045
1046 my $resp_code = $client->response_code();
1047 my $resp_msg = $client->response_msg();
1048
1049 my $expected;
1050
1051 $expected = 530;
1052 $self->assert($expected == $resp_code,
1053 test_msg("Expected response code $expected, got $resp_code"));
1054
1055 $expected = 'Please login with USER and PASS';
1056 $self->assert($expected eq $resp_msg,
1057 test_msg("Expected response message '$expected', got '$resp_msg'"));
1058
1059 $client->quit();
1060 };
1061
1062 if ($@) {
1063 $ex = $@;
1064 }
1065
1066 $wfh->print("done\n");
1067 $wfh->flush();
1068
1069 } else {
1070 eval { server_wait($config_file, $rfh) };
1071 if ($@) {
1072 warn($@);
1073 exit 1;
1074 }
1075
1076 exit 0;
1077 }
1078
1079 # Stop server
1080 server_stop($pid_file);
1081
1082 $self->assert_child_ok($pid);
1083
1084 if ($ex) {
1085 die($ex);
1086 }
1087
1088 unlink($log_file);
1089 }
1090
1091 sub site_chmod_numeric_ok {
1092 my $self = shift;
1093 my $tmpdir = $self->{tmpdir};
1094
1095 my $config_file = "$tmpdir/site.conf";
1096 my $pid_file = File::Spec->rel2abs("$tmpdir/site.pid");
1097 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/site.scoreboard");
1098
1099 my $log_file = File::Spec->rel2abs('tests.log');
1100
1101 my $auth_user_file = File::Spec->rel2abs("$tmpdir/site.passwd");
1102 my $auth_group_file = File::Spec->rel2abs("$tmpdir/site.group");
1103
1104 my $user = 'proftpd';
1105 my $passwd = 'test';
1106 my $group = 'ftpd';
1107 my $home_dir = File::Spec->rel2abs($tmpdir);
1108 my $uid = 500;
1109 my $gid = 500;
1110
1111 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
1112 if (open(my $fh, "> $test_file")) {
1113 close($fh);
1114
1115 } else {
1116 die("Can't open $test_file: $!");
1117 }
1118
1119 # Make sure that, if we're running as root, that the home directory has
1120 # permissions/privs set for the account we create
1121 if ($< == 0) {
1122 unless (chmod(0755, $home_dir)) {
1123 die("Can't set perms on $home_dir to 0755: $!");
1124 }
1125
1126 unless (chown($uid, $gid, $home_dir, $test_file)) {
1127 die("Can't set owner of $home_dir to $uid/$gid: $!");
1128 }
1129 }
1130
1131 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1132 '/bin/bash');
1133 auth_group_write($auth_group_file, $group, $gid, $user);
1134
1135 my $config = {
1136 PidFile => $pid_file,
1137 ScoreboardFile => $scoreboard_file,
1138 SystemLog => $log_file,
1139 TraceLog => $log_file,
1140 Trace => 'fileperms:10',
1141
1142 AuthUserFile => $auth_user_file,
1143 AuthGroupFile => $auth_group_file,
1144 AuthOrder => 'mod_auth_file.c',
1145
1146 IfModules => {
1147 'mod_delay.c' => {
1148 DelayEngine => 'off',
1149 },
1150 },
1151 };
1152
1153 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1154
1155 # Open pipes, for use between the parent and child processes. Specifically,
1156 # the child will indicate when it's done with its test by writing a message
1157 # to the parent.
1158 my ($rfh, $wfh);
1159 unless (pipe($rfh, $wfh)) {
1160 die("Can't open pipe: $!");
1161 }
1162
1163 my $ex;
1164
1165 # Fork child
1166 $self->handle_sigchld();
1167 defined(my $pid = fork()) or die("Can't fork: $!");
1168 if ($pid) {
1169 eval {
1170 sleep(1);
1171 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1172 $client->login($user, $passwd);
1173
1174 $client->site('CHMOD', "777 test.txt");
1175
1176 my $resp_code = $client->response_code();
1177 my $resp_msg = $client->response_msg();
1178
1179 my $expected;
1180
1181 $expected = 200;
1182 $self->assert($expected == $resp_code,
1183 test_msg("Expected $expected, got $resp_code"));
1184
1185 $expected = 'SITE CHMOD command successful';
1186 $self->assert($expected eq $resp_msg,
1187 test_msg("Expected '$expected', got '$resp_msg'"));
1188
1189 $client->quit();
1190
1191 my $perms = ((stat($test_file))[2] &07777);
1192
1193 $expected = 0777;
1194 $self->assert($expected == $perms,
1195 test_msg("Expected '$expected', got '$perms'"));
1196 };
1197
1198 if ($@) {
1199 $ex = $@;
1200 }
1201
1202 $wfh->print("done\n");
1203 $wfh->flush();
1204
1205 } else {
1206 eval { server_wait($config_file, $rfh) };
1207 if ($@) {
1208 warn($@);
1209 exit 1;
1210 }
1211
1212 exit 0;
1213 }
1214
1215 # Stop server
1216 server_stop($pid_file);
1217
1218 $self->assert_child_ok($pid);
1219
1220 if ($ex) {
1221 die($ex);
1222 }
1223
1224 unlink($log_file);
1225 }
1226
1227 sub site_chmod_symbolic_ok {
1228 my $self = shift;
1229 my $tmpdir = $self->{tmpdir};
1230 my $setup = test_setup($tmpdir, 'site');
1231
1232 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
1233 if (open(my $fh, "> $test_file")) {
1234 close($fh);
1235
1236 } else {
1237 die("Can't open $test_file: $!");
1238 }
1239
1240 if ($< == 0) {
1241 unless (chown($setup->{uid}, $setup->{gid}, $test_file)) {
1242 die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!");
1243 }
1244 }
1245
1246 my $config = {
1247 PidFile => $setup->{pid_file},
1248 ScoreboardFile => $setup->{scoreboard_file},
1249 SystemLog => $setup->{log_file},
1250 TraceLog => $setup->{log_file},
1251 Trace => 'fileperms:10',
1252
1253 AuthUserFile => $setup->{auth_user_file},
1254 AuthGroupFile => $setup->{auth_group_file},
1255 AuthOrder => 'mod_auth_file.c',
1256
1257 IfModules => {
1258 'mod_delay.c' => {
1259 DelayEngine => 'off',
1260 },
1261 },
1262 };
1263
1264 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1265 $config);
1266
1267 # Open pipes, for use between the parent and child processes. Specifically,
1268 # the child will indicate when it's done with its test by writing a message
1269 # to the parent.
1270 my ($rfh, $wfh);
1271 unless (pipe($rfh, $wfh)) {
1272 die("Can't open pipe: $!");
1273 }
1274
1275 my $ex;
1276
1277 # Fork child
1278 $self->handle_sigchld();
1279 defined(my $pid = fork()) or die("Can't fork: $!");
1280 if ($pid) {
1281 eval {
1282 sleep(1);
1283 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1284 $client->login($setup->{user}, $setup->{passwd});
1285
1286 my $path = 'test.txt';
1287 my ($resp_code, $resp_msg) = $client->site('CHMOD', "ugo+rwx $path");
1288 $client->quit();
1289
1290 my $expected = 200;
1291 $self->assert($expected == $resp_code,
1292 test_msg("Expected response code $expected, got $resp_code"));
1293
1294 $expected = 'SITE CHMOD command successful';
1295 $self->assert($expected eq $resp_msg,
1296 test_msg("Expected response message '$expected', got '$resp_msg'"));
1297
1298 my $perms = ((stat($test_file))[2] &07777);
1299 $expected = 0777;
1300 $self->assert($expected == $perms,
1301 test_msg("Expected perms '$expected', got '$perms'"));
1302 };
1303 if ($@) {
1304 $ex = $@;
1305 }
1306
1307 $wfh->print("done\n");
1308 $wfh->flush();
1309
1310 } else {
1311 eval { server_wait($setup->{config_file}, $rfh) };
1312 if ($@) {
1313 warn($@);
1314 exit 1;
1315 }
1316
1317 exit 0;
1318 }
1319
1320 # Stop server
1321 server_stop($setup->{pid_file});
1322 $self->assert_child_ok($pid);
1323
1324 test_cleanup($setup->{log_file}, $ex);
1325 }
1326
1327 sub site_chmod_abs_symlink {
1328 my $self = shift;
1329 my $tmpdir = $self->{tmpdir};
1330 my $setup = test_setup($tmpdir, 'site');
1331
1332 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1333 mkpath($test_dir);
1334
1335 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
1336 if (open(my $fh, "> $test_file")) {
1337 close($fh);
1338
1339 } else {
1340 die("Can't open $test_file: $!");
1341 }
1342
1343 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
1344
1345 my $dst_path = $test_file;
1346 if ($^O eq 'darwin') {
1347 # MacOSX-specific hack
1348 $dst_path = '/private' . $dst_path;
1349 }
1350
1351 unless (symlink($dst_path, $test_symlink)) {
1352 die("Can't symlink $test_symlink to $dst_path: $!");
1353 }
1354
1355 if ($< == 0) {
1356 unless (chmod(0755, $test_dir)) {
1357 die("Can't set perms on $test_dir to 0755: $!");
1358 }
1359
1360 unless (chown($setup->{uid}, $setup->{gid}, $test_dir, $test_file)) {
1361 die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!");
1362 }
1363 }
1364
1365 my $config = {
1366 PidFile => $setup->{pid_file},
1367 ScoreboardFile => $setup->{scoreboard_file},
1368 SystemLog => $setup->{log_file},
1369 TraceLog => $setup->{log_file},
1370 Trace => 'fileperms:10',
1371
1372 AuthUserFile => $setup->{auth_user_file},
1373 AuthGroupFile => $setup->{auth_group_file},
1374 AuthOrder => 'mod_auth_file.c',
1375
1376 IfModules => {
1377 'mod_delay.c' => {
1378 DelayEngine => 'off',
1379 },
1380 },
1381 };
1382
1383 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1384 $config);
1385
1386 # Open pipes, for use between the parent and child processes. Specifically,
1387 # the child will indicate when it's done with its test by writing a message
1388 # to the parent.
1389 my ($rfh, $wfh);
1390 unless (pipe($rfh, $wfh)) {
1391 die("Can't open pipe: $!");
1392 }
1393
1394 my $ex;
1395
1396 # Fork child
1397 $self->handle_sigchld();
1398 defined(my $pid = fork()) or die("Can't fork: $!");
1399 if ($pid) {
1400 eval {
1401 sleep(1);
1402 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1403 $client->login($setup->{user}, $setup->{passwd});
1404
1405 my $path = 'test.d/test.lnk';
1406 my ($resp_code, $resp_msg) = $client->site('CHMOD', "ugo+rwx $path");
1407 $client->quit();
1408
1409 my $expected = 200;
1410 $self->assert($expected == $resp_code,
1411 test_msg("Expected response code $expected, got $resp_code"));
1412
1413 $expected = 'SITE CHMOD command successful';
1414 $self->assert($expected eq $resp_msg,
1415 test_msg("Expected response message '$expected', got '$resp_msg'"));
1416
1417 my $perms = ((stat($test_file))[2] &07777);
1418 $expected = 0777;
1419 $self->assert($expected == $perms,
1420 test_msg("Expected perms '$expected', got '$perms'"));
1421 };
1422 if ($@) {
1423 $ex = $@;
1424 }
1425
1426 $wfh->print("done\n");
1427 $wfh->flush();
1428
1429 } else {
1430 eval { server_wait($setup->{config_file}, $rfh) };
1431 if ($@) {
1432 warn($@);
1433 exit 1;
1434 }
1435
1436 exit 0;
1437 }
1438
1439 # Stop server
1440 server_stop($setup->{pid_file});
1441 $self->assert_child_ok($pid);
1442
1443 test_cleanup($setup->{log_file}, $ex);
1444 }
1445
1446 sub site_chmod_abs_symlink_chrooted_bug4219 {
1447 my $self = shift;
1448 my $tmpdir = $self->{tmpdir};
1449 my $setup = test_setup($tmpdir, 'site');
1450
1451 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1452 mkpath($test_dir);
1453
1454 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
1455 if (open(my $fh, "> $test_file")) {
1456 close($fh);
1457
1458 } else {
1459 die("Can't open $test_file: $!");
1460 }
1461
1462 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
1463
1464 my $dst_path = $test_file;
1465 if ($^O eq 'darwin') {
1466 # MacOSX-specific hack
1467 $dst_path = '/private' . $dst_path;
1468 }
1469
1470 unless (symlink($dst_path, $test_symlink)) {
1471 die("Can't symlink $test_symlink to $dst_path: $!");
1472 }
1473
1474 if ($< == 0) {
1475 unless (chmod(0755, $test_dir)) {
1476 die("Can't set perms on $test_dir to 0755: $!");
1477 }
1478
1479 unless (chown($setup->{uid}, $setup->{gid}, $test_dir, $test_file)) {
1480 die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!");
1481 }
1482 }
1483
1484 my $config = {
1485 PidFile => $setup->{pid_file},
1486 ScoreboardFile => $setup->{scoreboard_file},
1487 SystemLog => $setup->{log_file},
1488 TraceLog => $setup->{log_file},
1489 Trace => 'fileperms:10',
1490
1491 AuthUserFile => $setup->{auth_user_file},
1492 AuthGroupFile => $setup->{auth_group_file},
1493 AuthOrder => 'mod_auth_file.c',
1494
1495 DefaultRoot => '~',
1496
1497 IfModules => {
1498 'mod_delay.c' => {
1499 DelayEngine => 'off',
1500 },
1501 },
1502 };
1503
1504 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1505 $config);
1506
1507 # Open pipes, for use between the parent and child processes. Specifically,
1508 # the child will indicate when it's done with its test by writing a message
1509 # to the parent.
1510 my ($rfh, $wfh);
1511 unless (pipe($rfh, $wfh)) {
1512 die("Can't open pipe: $!");
1513 }
1514
1515 my $ex;
1516
1517 # Fork child
1518 $self->handle_sigchld();
1519 defined(my $pid = fork()) or die("Can't fork: $!");
1520 if ($pid) {
1521 eval {
1522 sleep(1);
1523 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1524 $client->login($setup->{user}, $setup->{passwd});
1525
1526 my $path = 'test.d/test.lnk';
1527 my ($resp_code, $resp_msg) = $client->site('CHMOD', "ugo+rwx $path");
1528 $client->quit();
1529
1530 my $expected = 200;
1531 $self->assert($expected == $resp_code,
1532 test_msg("Expected response code $expected, got $resp_code"));
1533
1534 $expected = 'SITE CHMOD command successful';
1535 $self->assert($expected eq $resp_msg,
1536 test_msg("Expected response message '$expected', got '$resp_msg'"));
1537
1538 my $perms = ((stat($test_file))[2] &07777);
1539 $expected = 0777;
1540 $self->assert($expected == $perms,
1541 test_msg("Expected perms '$expected', got '$perms'"));
1542 };
1543 if ($@) {
1544 $ex = $@;
1545 }
1546
1547 $wfh->print("done\n");
1548 $wfh->flush();
1549
1550 } else {
1551 eval { server_wait($setup->{config_file}, $rfh) };
1552 if ($@) {
1553 warn($@);
1554 exit 1;
1555 }
1556
1557 exit 0;
1558 }
1559
1560 # Stop server
1561 server_stop($setup->{pid_file});
1562 $self->assert_child_ok($pid);
1563
1564 test_cleanup($setup->{log_file}, $ex);
1565 }
1566
1567 sub site_chmod_rel_symlink {
1568 my $self = shift;
1569 my $tmpdir = $self->{tmpdir};
1570 my $setup = test_setup($tmpdir, 'site');
1571
1572 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1573 mkpath($test_dir);
1574
1575 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
1576 if (open(my $fh, "> $test_file")) {
1577 close($fh);
1578
1579 } else {
1580 die("Can't open $test_file: $!");
1581 }
1582
1583 # Change to the test directory in order to create a relative path in the
1584 # symlink we need
1585
1586 my $cwd = getcwd();
1587 unless (chdir($test_dir)) {
1588 die("Can't chdir to $test_dir: $!");
1589 }
1590
1591 unless (symlink('./test.txt', './test.lnk')) {
1592 die("Can't symlink 'test.lnk' to './test.txt': $!");
1593 }
1594
1595 unless (chdir($cwd)) {
1596 die("Can't chdir to $cwd: $!");
1597 }
1598
1599 if ($< == 0) {
1600 unless (chmod(0755, $test_dir)) {
1601 die("Can't set perms on $test_dir to 0755: $!");
1602 }
1603
1604 unless (chown($setup->{uid}, $setup->{gid}, $test_dir, $test_file)) {
1605 die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!");
1606 }
1607 }
1608
1609 my $config = {
1610 PidFile => $setup->{pid_file},
1611 ScoreboardFile => $setup->{scoreboard_file},
1612 SystemLog => $setup->{log_file},
1613 TraceLog => $setup->{log_file},
1614 Trace => 'fileperms:10',
1615
1616 AuthUserFile => $setup->{auth_user_file},
1617 AuthGroupFile => $setup->{auth_group_file},
1618 AuthOrder => 'mod_auth_file.c',
1619
1620 IfModules => {
1621 'mod_delay.c' => {
1622 DelayEngine => 'off',
1623 },
1624 },
1625 };
1626
1627 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1628 $config);
1629
1630 # Open pipes, for use between the parent and child processes. Specifically,
1631 # the child will indicate when it's done with its test by writing a message
1632 # to the parent.
1633 my ($rfh, $wfh);
1634 unless (pipe($rfh, $wfh)) {
1635 die("Can't open pipe: $!");
1636 }
1637
1638 my $ex;
1639
1640 # Fork child
1641 $self->handle_sigchld();
1642 defined(my $pid = fork()) or die("Can't fork: $!");
1643 if ($pid) {
1644 eval {
1645 sleep(1);
1646 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1647 $client->login($setup->{user}, $setup->{passwd});
1648
1649 my $path = 'test.d/test.lnk';
1650 my ($resp_code, $resp_msg) = $client->site('CHMOD', "ugo+rwx $path");
1651 $client->quit();
1652
1653 my $expected = 200;
1654 $self->assert($expected == $resp_code,
1655 test_msg("Expected response code $expected, got $resp_code"));
1656
1657 $expected = 'SITE CHMOD command successful';
1658 $self->assert($expected eq $resp_msg,
1659 test_msg("Expected response message '$expected', got '$resp_msg'"));
1660
1661 my $perms = ((stat($test_file))[2] &07777);
1662 $expected = 0777;
1663 $self->assert($expected == $perms,
1664 test_msg("Expected perms '$expected', got '$perms'"));
1665 };
1666 if ($@) {
1667 $ex = $@;
1668 }
1669
1670 $wfh->print("done\n");
1671 $wfh->flush();
1672
1673 } else {
1674 eval { server_wait($setup->{config_file}, $rfh) };
1675 if ($@) {
1676 warn($@);
1677 exit 1;
1678 }
1679
1680 exit 0;
1681 }
1682
1683 # Stop server
1684 server_stop($setup->{pid_file});
1685 $self->assert_child_ok($pid);
1686
1687 test_cleanup($setup->{log_file}, $ex);
1688 }
1689
1690 sub site_chmod_rel_symlink_chrooted_bug4219 {
1691 my $self = shift;
1692 my $tmpdir = $self->{tmpdir};
1693 my $setup = test_setup($tmpdir, 'site');
1694
1695 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1696 mkpath($test_dir);
1697
1698 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
1699 if (open(my $fh, "> $test_file")) {
1700 close($fh);
1701
1702 } else {
1703 die("Can't open $test_file: $!");
1704 }
1705
1706 # Change to the test directory in order to create a relative path in the
1707 # symlink we need
1708
1709 my $cwd = getcwd();
1710 unless (chdir($test_dir)) {
1711 die("Can't chdir to $test_dir: $!");
1712 }
1713
1714 unless (symlink('./test.txt', './test.lnk')) {
1715 die("Can't symlink 'test.lnk' to './test.txt': $!");
1716 }
1717
1718 unless (chdir($cwd)) {
1719 die("Can't chdir to $cwd: $!");
1720 }
1721
1722 if ($< == 0) {
1723 unless (chmod(0755, $test_dir)) {
1724 die("Can't set perms on $test_dir to 0755: $!");
1725 }
1726
1727 unless (chown($setup->{uid}, $setup->{gid}, $test_dir, $test_file)) {
1728 die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!");
1729 }
1730 }
1731
1732 my $config = {
1733 PidFile => $setup->{pid_file},
1734 ScoreboardFile => $setup->{scoreboard_file},
1735 SystemLog => $setup->{log_file},
1736 TraceLog => $setup->{log_file},
1737 Trace => 'fileperms:10',
1738
1739 AuthUserFile => $setup->{auth_user_file},
1740 AuthGroupFile => $setup->{auth_group_file},
1741 AuthOrder => 'mod_auth_file.c',
1742
1743 DefaultRoot => '~',
1744
1745 IfModules => {
1746 'mod_delay.c' => {
1747 DelayEngine => 'off',
1748 },
1749 },
1750 };
1751
1752 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1753 $config);
1754
1755 # Open pipes, for use between the parent and child processes. Specifically,
1756 # the child will indicate when it's done with its test by writing a message
1757 # to the parent.
1758 my ($rfh, $wfh);
1759 unless (pipe($rfh, $wfh)) {
1760 die("Can't open pipe: $!");
1761 }
1762
1763 my $ex;
1764
1765 # Fork child
1766 $self->handle_sigchld();
1767 defined(my $pid = fork()) or die("Can't fork: $!");
1768 if ($pid) {
1769 eval {
1770 sleep(1);
1771 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1772 $client->login($setup->{user}, $setup->{passwd});
1773
1774 my $path = 'test.d/test.lnk';
1775 my ($resp_code, $resp_msg) = $client->site('CHMOD', "ugo+rwx $path");
1776 $client->quit();
1777
1778 my $expected = 200;
1779 $self->assert($expected == $resp_code,
1780 test_msg("Expected response code $expected, got $resp_code"));
1781
1782 $expected = 'SITE CHMOD command successful';
1783 $self->assert($expected eq $resp_msg,
1784 test_msg("Expected response message '$expected', got '$resp_msg'"));
1785
1786 my $perms = ((stat($test_file))[2] &07777);
1787 $expected = 0777;
1788 $self->assert($expected == $perms,
1789 test_msg("Expected perms '$expected', got '$perms'"));
1790 };
1791 if ($@) {
1792 $ex = $@;
1793 }
1794
1795 $wfh->print("done\n");
1796 $wfh->flush();
1797
1798 } else {
1799 eval { server_wait($setup->{config_file}, $rfh) };
1800 if ($@) {
1801 warn($@);
1802 exit 1;
1803 }
1804
1805 exit 0;
1806 }
1807
1808 # Stop server
1809 server_stop($setup->{pid_file});
1810 $self->assert_child_ok($pid);
1811
1812 test_cleanup($setup->{log_file}, $ex);
1813 }
1814
1815 1;