Debian Codegraph @

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

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