Debian Codegraph @

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

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