Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/MFMT.pm

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