Debian Codegraph @

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

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