Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::CreateHome;
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 createhome_ok => {
20 order => ++$order,
21 test_class => [qw(forking rootprivs)],
22 },
23
24 createhome_dirmode_ok => {
25 order => ++$order,
26 test_class => [qw(forking rootprivs)],
27 },
28
29 createhome_explicit_parent_owner_ok => {
30 order => ++$order,
31 test_class => [qw(forking rootprivs)],
32 },
33
34 createhome_user_parent_owner_ok => {
35 order => ++$order,
36 test_class => [qw(forking rootprivs)],
37 },
38
39 createhome_skel_ok => {
40 order => ++$order,
41 test_class => [qw(forking rootprivs)],
42 },
43
44 createhome_homegid_bug3503 => {
45 order => ++$order,
46 test_class => [qw(bug forking rootprivs)],
47 },
48
49 createhome_dirmode_uid_gid_ok => {
50 order => ++$order,
51 test_class => [qw(forking rootprivs)],
52 },
53
54 createhome_dirmode_uid_gid_norootprivs_bug3813 => {
55 order => ++$order,
56 test_class => [qw(bug forking norootprivs)],
57 },
58
59 };
60
61 sub new {
62 return shift()->SUPER::new(@_);
63 }
64
65 sub list_tests {
66 return testsuite_get_runnable_tests($TESTS);
67 }
68
69 sub createhome_ok {
70 my $self = shift;
71 my $tmpdir = $self->{tmpdir};
72
73 my $config_file = "$tmpdir/config.conf";
74 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
75 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
76
77 my $log_file = File::Spec->rel2abs('tests.log');
78
79 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
80 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
81
82 my $user = 'proftpd';
83 my $passwd = 'test';
84 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
85 my $uid = 500;
86 my $gid = 500;
87
88 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
89 '/bin/bash');
90 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
91
92 my $config = {
93 PidFile => $pid_file,
94 ScoreboardFile => $scoreboard_file,
95 SystemLog => $log_file,
96
97 AuthUserFile => $auth_user_file,
98 AuthGroupFile => $auth_group_file,
99 AuthOrder => 'mod_auth_file.c',
100
101 CreateHome => 'on',
102
103 IfModules => {
104 'mod_delay.c' => {
105 DelayEngine => 'off',
106 },
107 },
108 };
109
110 my ($port, $config_user, $config_group) = config_write($config_file, $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($user, $passwd);
129 };
130
131 if ($@) {
132 $ex = $@;
133 }
134
135 $wfh->print("done\n");
136 $wfh->flush();
137
138 } else {
139 eval { server_wait($config_file, $rfh) };
140 if ($@) {
141 warn($@);
142 exit 1;
143 }
144
145 exit 0;
146 }
147
148 # Stop server
149 server_stop($pid_file);
150
151 $self->assert_child_ok($pid);
152
153 # Check that the home directory exists, and that the parent directory
154 # of $tmpdir/foo is owned by UID/GID root.
155 $self->assert(-d $home_dir,
156 test_msg("Expected $home_dir directory to exist"));
157
158 my $parent_dir = "$tmpdir/foo";
159 my ($uid_owner, $gid_owner) = (stat($parent_dir))[4,5];
160
161 my $expected = 0;
162 $self->assert($expected == $uid_owner,
163 test_msg("Expected $expected, got $uid_owner"));
164 $self->assert($expected == $gid_owner,
165 test_msg("Expected $expected, got $gid_owner"));
166
167 if ($ex) {
168 die($ex);
169 }
170
171 unlink($log_file);
172 }
173
174 sub createhome_dirmode_ok {
175 my $self = shift;
176 my $tmpdir = $self->{tmpdir};
177
178 my $config_file = "$tmpdir/config.conf";
179 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
180 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
181
182 my $log_file = File::Spec->rel2abs('tests.log');
183
184 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
185 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
186
187 my $user = 'proftpd';
188 my $passwd = 'test';
189 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
190 my $uid = 500;
191 my $gid = 500;
192
193 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
194 '/bin/bash');
195 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
196
197 my $config = {
198 PidFile => $pid_file,
199 ScoreboardFile => $scoreboard_file,
200 SystemLog => $log_file,
201
202 AuthUserFile => $auth_user_file,
203 AuthGroupFile => $auth_group_file,
204 AuthOrder => 'mod_auth_file.c',
205
206 CreateHome => 'on 711 dirmode 755',
207
208 IfModules => {
209 'mod_delay.c' => {
210 DelayEngine => 'off',
211 },
212 },
213 };
214
215 my ($port, $config_user, $config_group) = config_write($config_file, $config);
216
217 # Open pipes, for use between the parent and child processes. Specifically,
218 # the child will indicate when it's done with its test by writing a message
219 # to the parent.
220 my ($rfh, $wfh);
221 unless (pipe($rfh, $wfh)) {
222 die("Can't open pipe: $!");
223 }
224
225 my $ex;
226
227 # Fork child
228 $self->handle_sigchld();
229 defined(my $pid = fork()) or die("Can't fork: $!");
230 if ($pid) {
231 eval {
232 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
233 $client->login($user, $passwd);
234 };
235
236 if ($@) {
237 $ex = $@;
238 }
239
240 $wfh->print("done\n");
241 $wfh->flush();
242
243 } else {
244 eval { server_wait($config_file, $rfh) };
245 if ($@) {
246 warn($@);
247 exit 1;
248 }
249
250 exit 0;
251 }
252
253 # Stop server
254 server_stop($pid_file);
255
256 $self->assert_child_ok($pid);
257
258 # Check that the home directory exists, and that the parent directory
259 # of $tmpdir/foo is owned by UID/GID root.
260 $self->assert(-d $home_dir,
261 test_msg("Expected $home_dir directory to exist"));
262
263 my $parent_dir = "$tmpdir/foo";
264 my ($uid_owner, $gid_owner) = (stat($parent_dir))[4,5];
265
266 my $expected = 0;
267 $self->assert($expected == $uid_owner,
268 test_msg("Expected $expected, got $uid_owner"));
269 $self->assert($expected == $gid_owner,
270 test_msg("Expected $expected, got $gid_owner"));
271
272 # Make sure that the permissions on the directories match the expected modes
273 my $mode = sprintf("%04o", (stat($home_dir))[2] & 07777);
274
275 $expected = '0711';
276 $self->assert($expected eq $mode,
277 test_msg("Expected '$expected', got '$mode'"));
278
279 $mode = sprintf("%04o", (stat("$tmpdir/foo"))[2] & 07777);
280
281 $expected = '0755';
282 $self->assert($expected eq $mode,
283 test_msg("Expected '$expected', got '$mode'"));
284
285 if ($ex) {
286 die($ex);
287 }
288
289 unlink($log_file);
290 }
291
292 sub createhome_explicit_parent_owner_ok {
293 my $self = shift;
294 my $tmpdir = $self->{tmpdir};
295
296 my $config_file = "$tmpdir/config.conf";
297 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
298 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
299
300 my $log_file = File::Spec->rel2abs('tests.log');
301
302 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
303 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
304
305 my $user = 'proftpd';
306 my $passwd = 'test';
307 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
308 my $uid = 500;
309 my $gid = 500;
310
311 my $explicit_guid = 250;
312
313 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
314 '/bin/bash');
315 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
316
317 my $config = {
318 PidFile => $pid_file,
319 ScoreboardFile => $scoreboard_file,
320 SystemLog => $log_file,
321
322 AuthUserFile => $auth_user_file,
323 AuthGroupFile => $auth_group_file,
324 AuthOrder => 'mod_auth_file.c',
325
326 CreateHome => "on 711 uid $explicit_guid gid $explicit_guid",
327
328 IfModules => {
329 'mod_delay.c' => {
330 DelayEngine => 'off',
331 },
332 },
333 };
334
335 my ($port, $config_user, $config_group) = config_write($config_file, $config);
336
337 # Open pipes, for use between the parent and child processes. Specifically,
338 # the child will indicate when it's done with its test by writing a message
339 # to the parent.
340 my ($rfh, $wfh);
341 unless (pipe($rfh, $wfh)) {
342 die("Can't open pipe: $!");
343 }
344
345 my $ex;
346
347 # Fork child
348 $self->handle_sigchld();
349 defined(my $pid = fork()) or die("Can't fork: $!");
350 if ($pid) {
351 eval {
352 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
353 $client->login($user, $passwd);
354 };
355
356 if ($@) {
357 $ex = $@;
358 }
359
360 $wfh->print("done\n");
361 $wfh->flush();
362
363 } else {
364 eval { server_wait($config_file, $rfh) };
365 if ($@) {
366 warn($@);
367 exit 1;
368 }
369
370 exit 0;
371 }
372
373 # Stop server
374 server_stop($pid_file);
375
376 $self->assert_child_ok($pid);
377
378 # Check that the home directory exists, and that the parent directory
379 # of $tmpdir/foo is owned by UID/GID root.
380 $self->assert(-d $home_dir,
381 test_msg("Expected $home_dir directory to exist"));
382
383 my $parent_dir = "$tmpdir/foo";
384 my ($uid_owner, $gid_owner) = (stat($parent_dir))[4,5];
385
386 my $expected = $explicit_guid;
387 $self->assert($expected == $uid_owner,
388 test_msg("Expected $expected, got $uid_owner"));
389 $self->assert($expected == $gid_owner,
390 test_msg("Expected $expected, got $gid_owner"));
391
392 if ($ex) {
393 die($ex);
394 }
395
396 unlink($log_file);
397 }
398
399 sub createhome_user_parent_owner_ok {
400 my $self = shift;
401 my $tmpdir = $self->{tmpdir};
402
403 my $config_file = "$tmpdir/config.conf";
404 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
405 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
406
407 my $log_file = File::Spec->rel2abs('tests.log');
408
409 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
410 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
411
412 my $user = 'proftpd';
413 my $passwd = 'test';
414 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
415 my $uid = 500;
416 my $gid = 500;
417
418 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
419 '/bin/bash');
420 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
421
422 my $config = {
423 PidFile => $pid_file,
424 ScoreboardFile => $scoreboard_file,
425 SystemLog => $log_file,
426
427 AuthUserFile => $auth_user_file,
428 AuthGroupFile => $auth_group_file,
429 AuthOrder => 'mod_auth_file.c',
430
431 CreateHome => "on 711 uid ~ gid ~",
432
433 IfModules => {
434 'mod_delay.c' => {
435 DelayEngine => 'off',
436 },
437 },
438 };
439
440 my ($port, $config_user, $config_group) = config_write($config_file, $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);
458 $client->login($user, $passwd);
459 };
460
461 if ($@) {
462 $ex = $@;
463 }
464
465 $wfh->print("done\n");
466 $wfh->flush();
467
468 } else {
469 eval { server_wait($config_file, $rfh) };
470 if ($@) {
471 warn($@);
472 exit 1;
473 }
474
475 exit 0;
476 }
477
478 # Stop server
479 server_stop($pid_file);
480
481 $self->assert_child_ok($pid);
482
483 # Check that the home directory exists, and that the parent directory
484 # of $tmpdir/foo is owned by UID/GID root.
485 $self->assert(-d $home_dir,
486 test_msg("Expected $home_dir directory to exist"));
487
488 my $parent_dir = "$tmpdir/foo";
489 my ($uid_owner, $gid_owner) = (stat($parent_dir))[4,5];
490
491 my $expected = $uid;
492 $self->assert($expected == $uid_owner,
493 test_msg("Expected $expected, got $uid_owner"));
494
495 $expected = $gid;
496 $self->assert($expected == $gid_owner,
497 test_msg("Expected $expected, got $gid_owner"));
498
499 if ($ex) {
500 die($ex);
501 }
502
503 unlink($log_file);
504 }
505
506 sub createhome_skel_ok {
507 my $self = shift;
508 my $tmpdir = $self->{tmpdir};
509
510 my $config_file = "$tmpdir/config.conf";
511 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
512 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
513
514 my $log_file = File::Spec->rel2abs('tests.log');
515
516 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
517 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
518
519 my $user = 'proftpd';
520 my $passwd = 'test';
521 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
522 my $uid = 500;
523 my $gid = 500;
524
525 my $skel = File::Spec->rel2abs("$tmpdir/skel");
526 mkpath($skel);
527
528 my $skel_file = File::Spec->rel2abs("$skel/welcome.txt");
529 if (open(my $fh, "> $skel_file")) {
530 print $fh "Welcome, new user.\n";
531
532 unless (close($fh)) {
533 die("Can't write $skel_file: $!");
534 }
535
536 } else {
537 die("Can't open $skel_file: $!");
538 }
539
540 my $skel_sub = File::Spec->rel2abs("$skel/subdir");
541 mkpath($skel_sub);
542
543 $skel_file = File::Spec->rel2abs("$skel_sub/test.txt");
544 if (open(my $fh, "> $skel_file")) {
545 print $fh "More test file stuff.\n";
546
547 unless (close($fh)) {
548 die("Can't write $skel_file: $!");
549 }
550
551 } else {
552 die("Can't open $skel_file: $!");
553 }
554
555 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
556 '/bin/bash');
557 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
558
559 my $config = {
560 PidFile => $pid_file,
561 ScoreboardFile => $scoreboard_file,
562 SystemLog => $log_file,
563
564 AuthUserFile => $auth_user_file,
565 AuthGroupFile => $auth_group_file,
566 AuthOrder => 'mod_auth_file.c',
567
568 CreateHome => "on skel $skel",
569
570 IfModules => {
571 'mod_delay.c' => {
572 DelayEngine => 'off',
573 },
574 },
575 };
576
577 my ($port, $config_user, $config_group) = config_write($config_file, $config);
578
579 # Open pipes, for use between the parent and child processes. Specifically,
580 # the child will indicate when it's done with its test by writing a message
581 # to the parent.
582 my ($rfh, $wfh);
583 unless (pipe($rfh, $wfh)) {
584 die("Can't open pipe: $!");
585 }
586
587 my $ex;
588
589 # Fork child
590 $self->handle_sigchld();
591 defined(my $pid = fork()) or die("Can't fork: $!");
592 if ($pid) {
593 eval {
594 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
595 $client->login($user, $passwd);
596 };
597
598 if ($@) {
599 $ex = $@;
600 }
601
602 $wfh->print("done\n");
603 $wfh->flush();
604
605 } else {
606 eval { server_wait($config_file, $rfh) };
607 if ($@) {
608 warn($@);
609 exit 1;
610 }
611
612 exit 0;
613 }
614
615 # Stop server
616 server_stop($pid_file);
617
618 $self->assert_child_ok($pid);
619
620 # Check that the home directory exists, and that it contains the
621 # skeleton dirs/files.
622 $self->assert(-d $home_dir,
623 test_msg("Expected '$home_dir' directory to exist"));
624
625 $self->assert(-d "$home_dir/subdir",
626 test_msg("Expected '$home_dir/subdir' directory to exist"));
627
628 $self->assert(-f "$home_dir/welcome.txt",
629 test_msg("Expected '$home_dir/welcome.txt' file to exist"));
630
631 $self->assert(-f "$home_dir/subdir/test.txt",
632 test_msg("Expected '$home_dir/subdir/test.txt' file to exist"));
633
634 if ($ex) {
635 die($ex);
636 }
637
638 unlink($log_file);
639 }
640
641 sub createhome_homegid_bug3503 {
642 my $self = shift;
643 my $tmpdir = $self->{tmpdir};
644
645 my $config_file = "$tmpdir/config.conf";
646 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
647 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
648
649 my $log_file = File::Spec->rel2abs('tests.log');
650
651 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
652 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
653
654 my $user = 'proftpd';
655 my $passwd = 'test';
656 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
657 my $uid = 500;
658 my $gid = 500;
659
660 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
661 '/bin/bash');
662 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
663
664 my $home_gid = 777;
665
666 my $config = {
667 PidFile => $pid_file,
668 ScoreboardFile => $scoreboard_file,
669 SystemLog => $log_file,
670
671 AuthUserFile => $auth_user_file,
672 AuthGroupFile => $auth_group_file,
673 AuthOrder => 'mod_auth_file.c',
674
675 CreateHome => "on 711 homegid $home_gid",
676
677 IfModules => {
678 'mod_delay.c' => {
679 DelayEngine => 'off',
680 },
681 },
682 };
683
684 my ($port, $config_user, $config_group) = config_write($config_file, $config);
685
686 # Open pipes, for use between the parent and child processes. Specifically,
687 # the child will indicate when it's done with its test by writing a message
688 # to the parent.
689 my ($rfh, $wfh);
690 unless (pipe($rfh, $wfh)) {
691 die("Can't open pipe: $!");
692 }
693
694 my $ex;
695
696 # Fork child
697 $self->handle_sigchld();
698 defined(my $pid = fork()) or die("Can't fork: $!");
699 if ($pid) {
700 eval {
701 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
702 $client->login($user, $passwd);
703 };
704
705 if ($@) {
706 $ex = $@;
707 }
708
709 $wfh->print("done\n");
710 $wfh->flush();
711
712 } else {
713 eval { server_wait($config_file, $rfh) };
714 if ($@) {
715 warn($@);
716 exit 1;
717 }
718
719 exit 0;
720 }
721
722 # Stop server
723 server_stop($pid_file);
724
725 $self->assert_child_ok($pid);
726
727 # Check that the home directory exists, and that the home directory
728 # of $tmpdir/foo/bar is owned by root UID and configured GID.
729 $self->assert(-d $home_dir,
730 test_msg("Expected $home_dir directory to exist"));
731
732 my ($uid_owner, $gid_owner) = (stat($home_dir))[4,5];
733
734 my $expected = $uid;
735 $self->assert($expected == $uid_owner,
736 test_msg("Expected $expected, got $uid_owner"));
737
738 $expected = $home_gid;
739 $self->assert($expected == $gid_owner,
740 test_msg("Expected $expected, got $gid_owner"));
741
742 if ($ex) {
743 die($ex);
744 }
745
746 unlink($log_file);
747 }
748
749 sub createhome_dirmode_uid_gid_ok {
750 my $self = shift;
751 my $tmpdir = $self->{tmpdir};
752
753 my $config_file = "$tmpdir/config.conf";
754 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
755 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
756
757 my $log_file = File::Spec->rel2abs('tests.log');
758
759 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
760 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
761
762 my $user = 'proftpd';
763 my $passwd = 'test';
764 my $group = 'ftpd';
765 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
766 my $uid = 500;
767 my $gid = 500;
768
769 my $explicit_guid = 250;
770
771 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
772 '/bin/bash');
773 auth_group_write($auth_group_file, $group, $gid, $user);
774
775 # Trying to debug:
776 #
777 # http://forums.proftpd.org/smf/index.php/topic,5534.0.html
778
779 my $config = {
780 PidFile => $pid_file,
781 ScoreboardFile => $scoreboard_file,
782 SystemLog => $log_file,
783
784 AuthUserFile => $auth_user_file,
785 AuthGroupFile => $auth_group_file,
786 AuthOrder => 'mod_auth_file.c',
787
788 CreateHome => "on 700 dirmode 700 uid ~ gid ~",
789
790 IfModules => {
791 'mod_delay.c' => {
792 DelayEngine => 'off',
793 },
794 },
795 };
796
797 my ($port, $config_user, $config_group) = config_write($config_file, $config);
798
799 # Open pipes, for use between the parent and child processes. Specifically,
800 # the child will indicate when it's done with its test by writing a message
801 # to the parent.
802 my ($rfh, $wfh);
803 unless (pipe($rfh, $wfh)) {
804 die("Can't open pipe: $!");
805 }
806
807 my $ex;
808
809 # Fork child
810 $self->handle_sigchld();
811 defined(my $pid = fork()) or die("Can't fork: $!");
812 if ($pid) {
813 eval {
814 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
815 $client->login($user, $passwd);
816 };
817
818 if ($@) {
819 $ex = $@;
820 }
821
822 $wfh->print("done\n");
823 $wfh->flush();
824
825 } else {
826 eval { server_wait($config_file, $rfh) };
827 if ($@) {
828 warn($@);
829 exit 1;
830 }
831
832 exit 0;
833 }
834
835 # Stop server
836 server_stop($pid_file);
837
838 $self->assert_child_ok($pid);
839
840 # Check that the home directory exists, and that the parent directory
841 # of $tmpdir/foo is owned by UID/GID root.
842 $self->assert(-d $home_dir,
843 test_msg("Expected $home_dir directory to exist"));
844
845 my $parent_dir = "$tmpdir/foo";
846 my ($uid_owner, $gid_owner) = (stat($parent_dir))[4,5];
847
848 my $expected = $uid;
849 $self->assert($expected == $uid_owner,
850 test_msg("Expected $expected, got $uid_owner"));
851
852 $expected = $gid;
853 $self->assert($expected == $gid_owner,
854 test_msg("Expected $expected, got $gid_owner"));
855
856 # Make sure that the permissions on the directories match the expected modes
857 my $mode = sprintf("%04o", (stat($home_dir))[2] & 07777);
858
859 $expected = '0700';
860 $self->assert($expected eq $mode,
861 test_msg("Expected '$expected', got '$mode'"));
862
863 $mode = sprintf("%04o", (stat("$tmpdir/foo"))[2] & 07777);
864
865 $expected = '0700';
866 $self->assert($expected eq $mode,
867 test_msg("Expected '$expected', got '$mode'"));
868
869 if ($ex) {
870 die($ex);
871 }
872
873 unlink($log_file);
874 }
875
876 sub createhome_dirmode_uid_gid_norootprivs_bug3813 {
877 my $self = shift;
878 my $tmpdir = $self->{tmpdir};
879
880 my $config_file = "$tmpdir/config.conf";
881 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
882 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
883
884 my $log_file = File::Spec->rel2abs('tests.log');
885
886 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
887 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
888
889 my $user = 'proftpd';
890 my $passwd = 'test';
891 my $group = 'ftpd';
892 my $home_dir = File::Spec->rel2abs("$tmpdir/foo/bar");
893 my $uid = 500;
894 my $gid = 500;
895
896 my $explicit_guid = 250;
897
898 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
899 '/bin/bash');
900 auth_group_write($auth_group_file, $group, $gid, $user);
901
902 my $config = {
903 PidFile => $pid_file,
904 ScoreboardFile => $scoreboard_file,
905 SystemLog => $log_file,
906
907 AuthUserFile => $auth_user_file,
908 AuthGroupFile => $auth_group_file,
909 AuthOrder => 'mod_auth_file.c',
910
911 CreateHome => "on 700 dirmode 700 uid ~ gid ~ NoRootPrivs",
912
913 IfModules => {
914 'mod_delay.c' => {
915 DelayEngine => 'off',
916 },
917 },
918 };
919
920 my ($port, $config_user, $config_group) = config_write($config_file, $config);
921
922 # Open pipes, for use between the parent and child processes. Specifically,
923 # the child will indicate when it's done with its test by writing a message
924 # to the parent.
925 my ($rfh, $wfh);
926 unless (pipe($rfh, $wfh)) {
927 die("Can't open pipe: $!");
928 }
929
930 my $ex;
931
932 # Fork child
933 $self->handle_sigchld();
934 defined(my $pid = fork()) or die("Can't fork: $!");
935 if ($pid) {
936 eval {
937 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
938 $client->login($user, $passwd);
939 };
940
941 if ($@) {
942 $ex = $@;
943 }
944
945 $wfh->print("done\n");
946 $wfh->flush();
947
948 } else {
949 eval { server_wait($config_file, $rfh) };
950 if ($@) {
951 warn($@);
952 exit 1;
953 }
954
955 exit 0;
956 }
957
958 # Stop server
959 server_stop($pid_file);
960
961 $self->assert_child_ok($pid);
962
963 # Check that the home directory exists, and that the parent directory
964 # of $tmpdir/foo is owned by UID/GID root.
965 $self->assert(-d $home_dir,
966 test_msg("Expected $home_dir directory to exist"));
967
968 my $parent_dir = "$tmpdir/foo";
969 my ($uid_owner, $gid_owner) = (stat($parent_dir))[4,5];
970
971 my $expected = $<;
972 $self->assert($expected == $uid_owner,
973 test_msg("Expected $expected, got $uid_owner"));
974
975 $expected = $(;
976 $self->assert($expected == $gid_owner,
977 test_msg("Expected $expected, got $gid_owner"));
978
979 # Make sure that the permissions on the directories match the expected modes
980 my $mode = sprintf("%04o", (stat($home_dir))[2] & 07777);
981
982 $expected = '0700';
983 $self->assert($expected eq $mode,
984 test_msg("Expected '$expected', got '$mode'"));
985
986 $mode = sprintf("%04o", (stat("$tmpdir/foo"))[2] & 07777);
987
988 $expected = '0700';
989 $self->assert($expected eq $mode,
990 test_msg("Expected '$expected', got '$mode'"));
991
992 if ($ex) {
993 die($ex);
994 }
995
996 unlink($log_file);
997 }
998
999 1;