Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Logging/SystemLog.pm

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