Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::DirFakeGroup;
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 dirfakegroup_list => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 dirfakegroup_mlsd_bug3604 => {
24 order => ++$order,
25 test_class => [qw(bug forking)],
26 },
27
28 dirfakegroup_mlst_bug3604 => {
29 order => ++$order,
30 test_class => [qw(bug forking)],
31 },
32
33 dirfakegroup_mlsd_bug3715 => {
34 order => ++$order,
35 test_class => [qw(bug forking)],
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 dirfakegroup_list {
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 $fake_group = 'foo';
85
86 my $config = {
87 PidFile => $pid_file,
88 ScoreboardFile => $scoreboard_file,
89 SystemLog => $log_file,
90
91 AuthUserFile => $auth_user_file,
92 AuthGroupFile => $auth_group_file,
93 AuthOrder => 'mod_auth_file.c',
94
95 DirFakeGroup => "on $fake_group",
96
97 IfModules => {
98 'mod_delay.c' => {
99 DelayEngine => 'off',
100 },
101 },
102 };
103
104 my ($port, $config_user, $config_group) = config_write($config_file, $config);
105
106 # Open pipes, for use between the parent and child processes. Specifically,
107 # the child will indicate when it's done with its test by writing a message
108 # to the parent.
109 my ($rfh, $wfh);
110 unless (pipe($rfh, $wfh)) {
111 die("Can't open pipe: $!");
112 }
113
114 my $ex;
115
116 # Fork child
117 $self->handle_sigchld();
118 defined(my $pid = fork()) or die("Can't fork: $!");
119 if ($pid) {
120 eval {
121 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
122 $client->login($user, $passwd);
123
124 my $conn = $client->list_raw();
125 unless ($conn) {
126 die("Failed to LIST: " . $client->response_code() . " " .
127 $client->response_msg());
128 }
129
130 my $buf;
131 $conn->read($buf, 16384, 25);
132 eval { $conn->close() };
133
134 my $resp_code = $client->response_code();
135 my $resp_msg = $client->response_msg();
136 $self->assert_transfer_ok($resp_code, $resp_msg);
137
138 $client->quit();
139
140 # We have to be careful of the fact that readdir returns directory
141 # entries in an unordered fashion.
142 my $res = {};
143 my $lines = [split(/\n/, $buf)];
144 foreach my $line (@$lines) {
145 if ($line =~ /^\S+\s+\d+\s+\S+\s+(\S+)\s+.*?\s+(\S+)$/) {
146 $res->{$2} = $1;
147 }
148 }
149
150 my $expected = {
151 'config.conf' => $fake_group,
152 'config.group' => $fake_group,
153 'config.passwd' => $fake_group,
154 'config.pid' => $fake_group,
155 'config.scoreboard' => $fake_group,
156 'config.scoreboard.lck' => $fake_group,
157 };
158
159 my $file_ok = 1;
160 my $owner_ok = 1;
161
162 my $mismatch;
163 foreach my $name (keys(%$res)) {
164 unless (defined($expected->{$name})) {
165 $mismatch = $name;
166 $file_ok = 0;
167 last;
168 }
169
170 unless ($res->{$name} eq $expected->{$name}) {
171 $mismatch = "$name: $res->{$name}";
172 $owner_ok = 0;
173 last;
174 }
175 }
176
177 unless ($file_ok) {
178 die("Unexpected name '$mismatch' appeared in LIST data")
179 }
180
181 unless ($owner_ok) {
182 die("Unexpected group owner '$mismatch' appeared in LIST data")
183 }
184
185 };
186
187 if ($@) {
188 $ex = $@;
189 }
190
191 $wfh->print("done\n");
192 $wfh->flush();
193
194 } else {
195 eval { server_wait($config_file, $rfh) };
196 if ($@) {
197 warn($@);
198 exit 1;
199 }
200
201 exit 0;
202 }
203
204 # Stop server
205 server_stop($pid_file);
206
207 $self->assert_child_ok($pid);
208
209 if ($ex) {
210 test_append_logfile($log_file, $ex);
211 unlink($log_file);
212
213 die($ex);
214 }
215
216 unlink($log_file);
217 }
218
219 sub dirfakegroup_mlsd_bug3604 {
220 my $self = shift;
221 my $tmpdir = $self->{tmpdir};
222
223 my $config_file = "$tmpdir/config.conf";
224 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
225 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
226
227 my $log_file = test_get_logfile();
228
229 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
230 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
231
232 my $user = 'proftpd';
233 my $passwd = 'test';
234 my $group = 'ftpd';
235 my $home_dir = File::Spec->rel2abs($tmpdir);
236 my $uid = 500;
237 my $gid = 500;
238
239 my $fake_group = 'foo';
240 my $fake_gid = 2476;
241
242 # Make sure that, if we're running as root, that the home directory has
243 # permissions/privs set for the account we create
244 if ($< == 0) {
245 unless (chmod(0755, $home_dir)) {
246 die("Can't set perms on $home_dir to 0755: $!");
247 }
248
249 unless (chown($uid, $gid, $home_dir)) {
250 die("Can't set owner of $home_dir to $uid/$gid: $!");
251 }
252 }
253
254 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
255 '/bin/bash');
256 auth_group_write($auth_group_file, $group, $gid, $user);
257 auth_group_write($auth_group_file, $fake_group, $fake_gid);
258
259 my $config = {
260 PidFile => $pid_file,
261 ScoreboardFile => $scoreboard_file,
262 SystemLog => $log_file,
263
264 AuthUserFile => $auth_user_file,
265 AuthGroupFile => $auth_group_file,
266 AuthOrder => 'mod_auth_file.c',
267
268 DirFakeGroup => "on $fake_group",
269
270 IfModules => {
271 'mod_delay.c' => {
272 DelayEngine => 'off',
273 },
274 },
275 };
276
277 my ($port, $config_user, $config_group) = config_write($config_file, $config);
278
279 # Open pipes, for use between the parent and child processes. Specifically,
280 # the child will indicate when it's done with its test by writing a message
281 # to the parent.
282 my ($rfh, $wfh);
283 unless (pipe($rfh, $wfh)) {
284 die("Can't open pipe: $!");
285 }
286
287 my $ex;
288
289 # Fork child
290 $self->handle_sigchld();
291 defined(my $pid = fork()) or die("Can't fork: $!");
292 if ($pid) {
293 eval {
294 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
295 $client->login($user, $passwd);
296
297 my $conn = $client->mlsd_raw();
298 unless ($conn) {
299 die("Failed to MLSD: " . $client->response_code() . " " .
300 $client->response_msg());
301 }
302
303 my $buf;
304 $conn->read($buf, 16384, 25);
305 eval { $conn->close() };
306
307 my $resp_code = $client->response_code();
308 my $resp_msg = $client->response_msg();
309 $self->assert_transfer_ok($resp_code, $resp_msg);
310
311 $client->quit();
312
313 # We have to be careful of the fact that readdir returns directory
314 # entries in an unordered fashion.
315 my $res = {};
316 my $lines = [split(/\n/, $buf)];
317 foreach my $line (@$lines) {
318 if ($line =~ /^modify=\S+;perm=\S+;type=\S+;unique=\S+;UNIX\.group=(\d+);UNIX\.mode=\d+;UNIX.owner=\d+; (.*?)$/) {
319 $res->{$2} = $1;
320 }
321 }
322
323 my $expected = {
324 '.' => $fake_gid,
325 '..' => $fake_gid,
326 'config.conf' => $fake_gid,
327 'config.group' => $fake_gid,
328 'config.passwd' => $fake_gid,
329 'config.pid' => $fake_gid,
330 'config.scoreboard' => $fake_gid,
331 'config.scoreboard.lck' => $fake_gid,
332 };
333
334 my $file_ok = 1;
335 my $owner_ok = 1;
336
337 my $mismatch;
338 foreach my $name (keys(%$res)) {
339 unless (defined($expected->{$name})) {
340 $mismatch = $name;
341 $file_ok = 0;
342 last;
343 }
344
345 unless ($res->{$name} == $expected->{$name}) {
346 $mismatch = "$name: $res->{$name}";
347 $owner_ok = 0;
348 last;
349 }
350 }
351
352 unless ($file_ok) {
353 die("Unexpected name '$mismatch' appeared in LIST data")
354 }
355
356 unless ($owner_ok) {
357 die("Unexpected GID owner '$mismatch' appeared in LIST data")
358 }
359
360 };
361
362 if ($@) {
363 $ex = $@;
364 }
365
366 $wfh->print("done\n");
367 $wfh->flush();
368
369 } else {
370 eval { server_wait($config_file, $rfh) };
371 if ($@) {
372 warn($@);
373 exit 1;
374 }
375
376 exit 0;
377 }
378
379 # Stop server
380 server_stop($pid_file);
381
382 $self->assert_child_ok($pid);
383
384 if ($ex) {
385 test_append_logfile($log_file, $ex);
386 unlink($log_file);
387
388 die($ex);
389 }
390
391 unlink($log_file);
392 }
393
394 sub dirfakegroup_mlst_bug3604 {
395 my $self = shift;
396 my $tmpdir = $self->{tmpdir};
397 my $setup = test_setup($tmpdir, 'config');
398
399 my $fake_group = 'foo';
400 my $fake_gid = 2476;
401
402 auth_group_write($setup->{auth_group_file}, $fake_group, $fake_gid);
403
404 my $test_file = File::Spec->rel2abs($setup->{config_file});
405
406 my $config = {
407 PidFile => $setup->{pid_file},
408 ScoreboardFile => $setup->{scoreboard_file},
409 SystemLog => $setup->{log_file},
410
411 AuthUserFile => $setup->{auth_user_file},
412 AuthGroupFile => $setup->{auth_group_file},
413 AuthOrder => 'mod_auth_file.c',
414
415 DirFakeGroup => "on $fake_group",
416
417 IfModules => {
418 'mod_delay.c' => {
419 DelayEngine => 'off',
420 },
421 },
422 };
423
424 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
425 $config);
426
427 # Open pipes, for use between the parent and child processes. Specifically,
428 # the child will indicate when it's done with its test by writing a message
429 # to the parent.
430 my ($rfh, $wfh);
431 unless (pipe($rfh, $wfh)) {
432 die("Can't open pipe: $!");
433 }
434
435 my $ex;
436
437 # Fork child
438 $self->handle_sigchld();
439 defined(my $pid = fork()) or die("Can't fork: $!");
440 if ($pid) {
441 eval {
442 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
443 $client->login($setup->{user}, $setup->{passwd});
444
445 my ($resp_code, $resp_msg) = $client->mlst('config.conf');
446
447 my $expected = 250;
448 $self->assert($expected == $resp_code,
449 test_msg("Expected response code $expected, got $resp_code"));
450
451 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX\.group=(\d+);UNIX\.groupname=\S+;UNIX\.mode=\d+;UNIX\.owner=\d+;UNIX\.ownername=\S+; (.*?)\/config\.conf$';
452 $self->assert(qr/$expected/, $resp_msg,
453 test_msg("Expected response message '$expected', got '$resp_msg'"));
454
455 $client->quit();
456
457 $resp_msg =~ /$expected/;
458 my $file_gid = $2;
459
460 $self->assert($file_gid == $fake_gid,
461 test_msg("Expected $fake_gid, got $file_gid"));
462 };
463 if ($@) {
464 $ex = $@;
465 }
466
467 $wfh->print("done\n");
468 $wfh->flush();
469
470 } else {
471 eval { server_wait($setup->{config_file}, $rfh) };
472 if ($@) {
473 warn($@);
474 exit 1;
475 }
476
477 exit 0;
478 }
479
480 # Stop server
481 server_stop($setup->{pid_file});
482 $self->assert_child_ok($pid);
483
484 test_cleanup($setup->{log_file}, $ex);
485 }
486
487 sub dirfakegroup_mlsd_bug3715 {
488 my $self = shift;
489 my $tmpdir = $self->{tmpdir};
490
491 my $config_file = "$tmpdir/config.conf";
492 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
493 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
494
495 my $log_file = test_get_logfile();
496
497 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
498 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
499
500 my $user = 'proftpd';
501 my $passwd = 'test';
502 my $group = 'ftpd';
503 my $home_dir = File::Spec->rel2abs($tmpdir);
504 my $uid = 500;
505 my $gid = 500;
506
507 my $expected_gid = $(;
508
509 # On some systems, $( returns a space-delimited list of GIDs.
510 if ($expected_gid !~ /^\d+$/) {
511 my $gids = [split(' ', $()];
512 $expected_gid = $gids->[0];
513 }
514
515 # Make sure that, if we're running as root, that the home directory has
516 # permissions/privs set for the account we create
517 if ($< == 0) {
518 unless (chmod(0755, $home_dir)) {
519 die("Can't set perms on $home_dir to 0755: $!");
520 }
521
522 unless (chown($uid, $gid, $home_dir)) {
523 die("Can't set owner of $home_dir to $uid/$gid: $!");
524 }
525 }
526
527 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
528 '/bin/bash');
529 auth_group_write($auth_group_file, $group, $gid, $user);
530
531 my $config = {
532 PidFile => $pid_file,
533 ScoreboardFile => $scoreboard_file,
534 SystemLog => $log_file,
535
536 AuthUserFile => $auth_user_file,
537 AuthGroupFile => $auth_group_file,
538 AuthOrder => 'mod_auth_file.c',
539
540 DirFakeGroup => "off",
541
542 IfModules => {
543 'mod_delay.c' => {
544 DelayEngine => 'off',
545 },
546 },
547 };
548
549 my ($port, $config_user, $config_group) = config_write($config_file, $config);
550
551 # Open pipes, for use between the parent and child processes. Specifically,
552 # the child will indicate when it's done with its test by writing a message
553 # to the parent.
554 my ($rfh, $wfh);
555 unless (pipe($rfh, $wfh)) {
556 die("Can't open pipe: $!");
557 }
558
559 my $ex;
560
561 # Fork child
562 $self->handle_sigchld();
563 defined(my $pid = fork()) or die("Can't fork: $!");
564 if ($pid) {
565 eval {
566 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
567 $client->login($user, $passwd);
568
569 my $conn = $client->mlsd_raw();
570 unless ($conn) {
571 die("Failed to MLSD: " . $client->response_code() . " " .
572 $client->response_msg());
573 }
574
575 my $buf;
576 $conn->read($buf, 16384, 25);
577 eval { $conn->close() };
578
579 my $resp_code = $client->response_code();
580 my $resp_msg = $client->response_msg();
581 $self->assert_transfer_ok($resp_code, $resp_msg);
582
583 $client->quit();
584
585 # We have to be careful of the fact that readdir returns directory
586 # entries in an unordered fashion.
587 my $res = {};
588 my $lines = [split(/\n/, $buf)];
589 foreach my $line (@$lines) {
590 if ($line =~ /^modify=\S+;perm=\S+;type=\S+;unique=\S+;UNIX\.group=(\d+);UNIX\.mode=\d+;UNIX.owner=\d+; (.*?)$/) {
591 $res->{$2} = $1;
592 }
593 }
594
595 # Remove the '.' and '..' entries; they are relevant to this test.
596 delete($res->{'.'});
597 delete($res->{'..'});
598
599 my $expected = {
600 'config.conf' => $expected_gid,
601 'config.group' => $expected_gid,
602 'config.passwd' => $expected_gid,
603 'config.pid' => $expected_gid,
604 'config.scoreboard' => $expected_gid,
605 'config.scoreboard.lck' => $expected_gid,
606 };
607
608 my $file_ok = 1;
609 my $owner_ok = 1;
610
611 my $mismatch;
612 foreach my $name (keys(%$res)) {
613 unless (defined($expected->{$name})) {
614 $mismatch = $name;
615 $file_ok = 0;
616 last;
617 }
618
619 unless ($res->{$name} == $expected->{$name}) {
620 $mismatch = "$name: $res->{$name}";
621 $owner_ok = 0;
622 last;
623 }
624 }
625
626 unless ($file_ok) {
627 die("Unexpected name '$mismatch' appeared in LIST data")
628 }
629
630 unless ($owner_ok) {
631 die("Unexpected GID owner '$mismatch' appeared in LIST data")
632 }
633
634 };
635
636 if ($@) {
637 $ex = $@;
638 }
639
640 $wfh->print("done\n");
641 $wfh->flush();
642
643 } else {
644 eval { server_wait($config_file, $rfh) };
645 if ($@) {
646 warn($@);
647 exit 1;
648 }
649
650 exit 0;
651 }
652
653 # Stop server
654 server_stop($pid_file);
655
656 $self->assert_child_ok($pid);
657
658 if ($ex) {
659 test_append_logfile($log_file, $ex);
660 unlink($log_file);
661
662 die($ex);
663 }
664
665 unlink($log_file);
666 }
667
668 1;