Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_log_forensic;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
8 use File::Spec;
9 use IO::Handle;
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 forensic_failed_login => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 forensic_good_login => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 };
30
31 sub new {
32 return shift()->SUPER::new(@_);
33 }
34
35 sub list_tests {
36 return testsuite_get_runnable_tests($TESTS);
37 }
38
39 sub forensic_failed_login {
40 my $self = shift;
41 my $tmpdir = $self->{tmpdir};
42
43 my $config_file = "$tmpdir/forensic.conf";
44 my $pid_file = File::Spec->rel2abs("$tmpdir/forensic.pid");
45 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/forensic.scoreboard");
46
47 my $log_file = File::Spec->rel2abs('tests.log');
48
49 my $auth_user_file = File::Spec->rel2abs("$tmpdir/forensic.passwd");
50 my $auth_group_file = File::Spec->rel2abs("$tmpdir/forensic.group");
51
52 my $user = 'proftpd';
53 my $passwd = 'test';
54 my $group = 'ftpd';
55 my $home_dir = File::Spec->rel2abs($tmpdir);
56 my $uid = 500;
57 my $gid = 500;
58
59 # Make sure that, if we're running as root, that the home directory has
60 # permissions/privs set for the account we create
61 if ($< == 0) {
62 unless (chmod(0755, $home_dir)) {
63 die("Can't set perms on $home_dir to 0755: $!");
64 }
65
66 unless (chown($uid, $gid, $home_dir)) {
67 die("Can't set owner of $home_dir to $uid/$gid: $!");
68 }
69 }
70
71 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
72 '/bin/bash');
73 auth_group_write($auth_group_file, $group, $gid, $user);
74
75 my $forensic_log_file = File::Spec->rel2abs("$tmpdir/forensic.log");
76
77 my $config = {
78 PidFile => $pid_file,
79 ScoreboardFile => $scoreboard_file,
80 SystemLog => $log_file,
81
82 AuthUserFile => $auth_user_file,
83 AuthGroupFile => $auth_group_file,
84 AuthOrder => 'mod_auth_file.c',
85
86 AllowOverwrite => 'on',
87 AllowStoreRestart => 'on',
88
89 IfModules => {
90 'mod_delay.c' => {
91 DelayEngine => 'off',
92 },
93
94 'mod_log_forensic.c' => {
95 ForensicLogEngine => 'on',
96 ForensicLogCriteria => 'FailedLogin',
97 ForensicLogFile => $forensic_log_file,
98 },
99 },
100 };
101
102 my ($port, $config_user, $config_group) = config_write($config_file, $config);
103
104 # Open pipes, for use between the parent and child processes. Specifically,
105 # the child will indicate when it's done with its test by writing a message
106 # to the parent.
107 my ($rfh, $wfh);
108 unless (pipe($rfh, $wfh)) {
109 die("Can't open pipe: $!");
110 }
111
112 my $ex;
113
114 # Fork child
115 $self->handle_sigchld();
116 defined(my $pid = fork()) or die("Can't fork: $!");
117 if ($pid) {
118 eval {
119 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
120 eval { $client->login($user, 'foo') };
121 unless ($@) {
122 die("Login succeeded unexpectedly");
123 }
124 };
125
126 if ($@) {
127 $ex = $@;
128 }
129
130 $wfh->print("done\n");
131 $wfh->flush();
132
133 } else {
134 eval { server_wait($config_file, $rfh) };
135 if ($@) {
136 warn($@);
137 exit 1;
138 }
139
140 exit 0;
141 }
142
143 # Stop server
144 server_stop($pid_file);
145
146 $self->assert_child_ok($pid);
147
148 if ($ex) {
149 die($ex);
150 }
151
152 if (open(my $fh, "< $forensic_log_file")) {
153 my $begin_ok = 0;
154 my $end_ok = 0;
155
156 while (my $line = <$fh>) {
157 chomp($line);
158
159 if ($line =~ /^\-\-\-\-\-BEGIN FAILED LOGIN FORENSICS\-\-\-\-\-/) {
160 $begin_ok = 1;
161 next;
162 }
163
164 if ($begin_ok and
165 $line =~ /^\-\-\-\-\-END FAILED LOGIN FORENSICS\-\-\-\-\-/) {
166 $end_ok = 1;
167 last;
168 }
169 }
170
171 close($fh);
172
173 $self->assert($begin_ok and $end_ok,
174 test_msg("Expected ForensicLogFile lines did not appear"));
175
176 } else {
177 die("Can't open $forensic_log_file: $!");
178 }
179
180 unlink($log_file);
181 }
182
183 sub forensic_good_login {
184 my $self = shift;
185 my $tmpdir = $self->{tmpdir};
186
187 my $config_file = "$tmpdir/forensic.conf";
188 my $pid_file = File::Spec->rel2abs("$tmpdir/forensic.pid");
189 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/forensic.scoreboard");
190
191 my $log_file = File::Spec->rel2abs('tests.log');
192
193 my $auth_user_file = File::Spec->rel2abs("$tmpdir/forensic.passwd");
194 my $auth_group_file = File::Spec->rel2abs("$tmpdir/forensic.group");
195
196 my $user = 'proftpd';
197 my $passwd = 'test';
198 my $group = 'ftpd';
199 my $home_dir = File::Spec->rel2abs($tmpdir);
200 my $uid = 500;
201 my $gid = 500;
202
203 # Make sure that, if we're running as root, that the home directory has
204 # permissions/privs set for the account we create
205 if ($< == 0) {
206 unless (chmod(0755, $home_dir)) {
207 die("Can't set perms on $home_dir to 0755: $!");
208 }
209
210 unless (chown($uid, $gid, $home_dir)) {
211 die("Can't set owner of $home_dir to $uid/$gid: $!");
212 }
213 }
214
215 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
216 '/bin/bash');
217 auth_group_write($auth_group_file, $group, $gid, $user);
218
219 my $forensic_log_file = File::Spec->rel2abs("$tmpdir/forensic.log");
220
221 my $config = {
222 PidFile => $pid_file,
223 ScoreboardFile => $scoreboard_file,
224 SystemLog => $log_file,
225
226 AuthUserFile => $auth_user_file,
227 AuthGroupFile => $auth_group_file,
228 AuthOrder => 'mod_auth_file.c',
229
230 AllowOverwrite => 'on',
231 AllowStoreRestart => 'on',
232
233 IfModules => {
234 'mod_delay.c' => {
235 DelayEngine => 'off',
236 },
237
238 'mod_log_forensic.c' => {
239 ForensicLogEngine => 'on',
240 ForensicLogCriteria => 'FailedLogin',
241 ForensicLogFile => $forensic_log_file,
242 },
243 },
244 };
245
246 my ($port, $config_user, $config_group) = config_write($config_file, $config);
247
248 # Open pipes, for use between the parent and child processes. Specifically,
249 # the child will indicate when it's done with its test by writing a message
250 # to the parent.
251 my ($rfh, $wfh);
252 unless (pipe($rfh, $wfh)) {
253 die("Can't open pipe: $!");
254 }
255
256 my $ex;
257
258 # Fork child
259 $self->handle_sigchld();
260 defined(my $pid = fork()) or die("Can't fork: $!");
261 if ($pid) {
262 eval {
263 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
264 $client->login($user, $passwd);
265 $client->quit();
266 };
267
268 if ($@) {
269 $ex = $@;
270 }
271
272 $wfh->print("done\n");
273 $wfh->flush();
274
275 } else {
276 eval { server_wait($config_file, $rfh) };
277 if ($@) {
278 warn($@);
279 exit 1;
280 }
281
282 exit 0;
283 }
284
285 # Stop server
286 server_stop($pid_file);
287
288 $self->assert_child_ok($pid);
289
290 if ($ex) {
291 die($ex);
292 }
293
294 $self->assert(-z $forensic_log_file,
295 test_msg("ForensicLogFile $forensic_log_file unexpectedly not empty"));
296
297 unlink($log_file);
298 }
299
300 1;