Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_ctrls;
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 ctrls_lsctrl_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 ctrls_lsctrl_system_user_ok => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 ctrls_lsctrl_access_denied_issue1114 => {
29 order => ++$order,
30 test_class => [qw(bug forking)],
31 },
32
33 ctrls_sighup_bug3756 => {
34 order => ++$order,
35 test_class => [qw(bug forking os_linux)],
36 },
37
38 ctrls_intvl_timeoutlogin_bug4298 => {
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 ftpdctl {
54 my $sock_file = shift;
55 my $ctrl_cmd = shift;
56 my $poll_interval = shift;
57 $poll_interval = 3 unless defined($poll_interval);
58
59 my $ftpdctl_bin;
60 if ($ENV{PROFTPD_TEST_PATH}) {
61 $ftpdctl_bin = "$ENV{PROFTPD_TEST_PATH}/ftpdctl";
62
63 } else {
64 $ftpdctl_bin = '../ftpdctl';
65 }
66
67 my $verbosity = '';
68 if ($ENV{TEST_VERBOSE}) {
69 $verbosity = '-v';
70 }
71
72 my $cmd = "$ftpdctl_bin -s $sock_file $verbosity $ctrl_cmd";
73
74 if ($ENV{TEST_VERBOSE}) {
75 print STDERR "Executing ftpdctl: $cmd\n";
76 }
77
78 my @lines = `$cmd`;
79 my $exit_status = $? >> 8;
80
81 return ($exit_status, \@lines);
82 }
83
84 sub get_server_pid {
85 my $pid_file = shift;
86
87 my $pid;
88 if (open(my $fh, "< $pid_file")) {
89 $pid = <$fh>;
90 chomp($pid);
91 close($fh);
92
93 } else {
94 croak("Can't read $pid_file: $!");
95 }
96
97 return $pid;
98 }
99
100 sub server_open_fds {
101 my $pid_file = shift;
102
103 my $pid = get_server_pid($pid_file);
104
105 my $proc_dir = "/proc/$pid/fd";
106 if (opendir(my $dirh, $proc_dir)) {
107 my $count = 0;
108
109 # Only count entries whose names are numbers
110 while (my $dent = readdir($dirh)) {
111 if ($dent =~ /^\d+$/) {
112 $count++;
113 }
114 }
115
116 closedir($dirh);
117 return $count;
118
119 } else {
120 croak("Can't open directory '$proc_dir': $!");
121 }
122 }
123
124 sub ctrls_lsctrl_ok {
125 my $self = shift;
126 my $tmpdir = $self->{tmpdir};
127 my $setup = test_setup($tmpdir, 'ctrls');
128
129 my $ctrls_sock = File::Spec->rel2abs("$tmpdir/ctrls.sock");
130
131 my ($user, $group) = config_get_identity();
132 my $poll_interval = 2;
133
134 my $config = {
135 PidFile => $setup->{pid_file},
136 ScoreboardFile => $setup->{scoreboard_file},
137 SystemLog => $setup->{log_file},
138 TraceLog => $setup->{log_file},
139 Trace => 'ctrls:20 event:10',
140
141 IfModules => {
142 'mod_ctrls.c' => {
143 ControlsEngine => 'on',
144 ControlsLog => $setup->{log_file},
145 ControlsSocket => $ctrls_sock,
146 ControlsACLs => "all allow user *",
147 ControlsSocketACL => "allow user *",
148 ControlsInterval => $poll_interval,
149 },
150
151 'mod_delay.c' => {
152 DelayEngine => 'off',
153 },
154 },
155 };
156
157 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
158 $config);
159
160 my $ex;
161
162 # Start server
163 server_start($setup->{config_file});
164 sleep(2);
165
166 eval {
167 my ($exit_status, $lines) = ftpdctl($ctrls_sock, 'lsctrl', $poll_interval);
168 if ($ENV{TEST_VERBOSE}) {
169 print STDERR "# ftpdctl: (exit status $exit_status)\n";
170 foreach my $line (@$lines) {
171 chomp($line);
172 print STDERR "# $line\n";
173 }
174 }
175
176 my $expected = 0;
177 $self->assert($exit_status == $expected,
178 test_msg("Expected exit status $expected, got $exit_status"));
179
180 $lines = [grep { /mod_ctrls\.c/ } @$lines];
181
182 $expected = 4;
183 my $matches = scalar(@$lines);
184 $self->assert($expected == $matches,
185 test_msg("Expected $expected, got $matches"));
186
187 my $actions = '';
188 foreach my $line (@$lines) {
189 if ($line =~ /^ftpdctl: (\S+) \S+$/) {
190 $actions .= "$1 ";
191 }
192 }
193
194 $expected = 'help insctrl lsctrl rmctrl ';
195 $self->assert($expected eq $actions,
196 test_msg("Expected '$expected', got '$actions'"));
197 };
198 if ($@) {
199 $ex = $@;
200 }
201
202 server_stop($setup->{pid_file});
203 test_cleanup($setup->{log_file}, $ex);
204 }
205
206 sub ctrls_lsctrl_system_user_ok {
207 my $self = shift;
208 my $tmpdir = $self->{tmpdir};
209 my $setup = test_setup($tmpdir, 'ctrls');
210
211 my $ctrls_sock = File::Spec->rel2abs("$tmpdir/ctrls.sock");
212
213 my ($sys_user, $sys_group) = config_get_identity();
214
215 my $config = {
216 PidFile => $setup->{pid_file},
217 ScoreboardFile => $setup->{scoreboard_file},
218 SystemLog => $setup->{log_file},
219 TraceLog => $setup->{log_file},
220 Trace => 'DEFAULT:10',
221
222 AuthUserFile => $setup->{auth_user_file},
223 AuthGroupFile => $setup->{auth_group_file},
224 AuthOrder => 'mod_auth_file.c',
225
226 IfModules => {
227 'mod_ctrls.c' => {
228 ControlsEngine => 'on',
229 ControlsLog => $setup->{log_file},
230 ControlsSocket => $ctrls_sock,
231 ControlsACLs => "all allow user root,$sys_user",
232 ControlsSocketACL => "allow user root,$sys_user",
233 },
234
235 'mod_delay.c' => {
236 DelayEngine => 'off',
237 },
238 },
239 };
240
241 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
242 $config);
243
244 my $ex;
245
246 # Start server
247 server_start($setup->{config_file});
248 sleep(1);
249
250 eval {
251 my ($exit_status, $lines) = ftpdctl($ctrls_sock, 'lsctrl');
252 if ($ENV{TEST_VERBOSE}) {
253 print STDERR "# ftpdctl: (exit status $exit_status)\n";
254 foreach my $line (@$lines) {
255 chomp($line);
256 print STDERR "# $line\n";
257 }
258 }
259
260 my $expected = 0;
261 $self->assert($exit_status == $expected,
262 test_msg("Expected exit status $expected, got $exit_status"));
263
264 $lines = [grep { /mod_ctrls\.c/ } @$lines];
265
266 $expected = 4;
267 my $matches = scalar(@$lines);
268 $self->assert($expected == $matches,
269 test_msg("Expected $expected, got $matches"));
270
271 my $actions = '';
272 foreach my $line (@$lines) {
273 if ($line =~ /^ftpdctl: (\S+) \S+$/) {
274 $actions .= "$1 ";
275 }
276 }
277
278 $expected = 'help insctrl lsctrl rmctrl ';
279 $self->assert($expected eq $actions,
280 test_msg("Expected '$expected', got '$actions'"));
281 };
282 if ($@) {
283 $ex = $@;
284 }
285
286 server_stop($setup->{pid_file});
287 test_cleanup($setup->{log_file}, $ex);
288 }
289
290 sub ctrls_lsctrl_access_denied_issue1114 {
291 my $self = shift;
292 my $tmpdir = $self->{tmpdir};
293 my $setup = test_setup($tmpdir, 'ctrls');
294
295 my $ctrls_sock = File::Spec->rel2abs("$tmpdir/ctrls.sock");
296
297 my $config = {
298 PidFile => $setup->{pid_file},
299 ScoreboardFile => $setup->{scoreboard_file},
300 SystemLog => $setup->{log_file},
301 TraceLog => $setup->{log_file},
302 Trace => 'DEFAULT:10',
303
304 AuthUserFile => $setup->{auth_user_file},
305 AuthGroupFile => $setup->{auth_group_file},
306 AuthOrder => 'mod_auth_file.c',
307
308 IfModules => {
309 'mod_ctrls.c' => {
310 ControlsEngine => 'on',
311 ControlsLog => $setup->{log_file},
312 ControlsSocket => $ctrls_sock,
313 ControlsACLs => 'all deny user *',
314 ControlsSocketACL => 'deny user *',
315 },
316
317 'mod_delay.c' => {
318 DelayEngine => 'off',
319 },
320 },
321 };
322
323 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
324 $config);
325
326 my $ex;
327
328 # Start server
329 server_start($setup->{config_file});
330 sleep(1);
331
332 eval {
333 my ($exit_status, $lines) = ftpdctl($ctrls_sock, 'lsctrl');
334 if ($ENV{TEST_VERBOSE}) {
335 print STDERR "# ftpdctl: (exit status $exit_status)\n";
336 foreach my $line (@$lines) {
337 chomp($line);
338 print STDERR "# $line\n";
339 }
340 }
341
342 my $expected = 2;
343 $self->assert($exit_status == $expected,
344 test_msg("Expected exit status $expected, got $exit_status"));
345
346 $lines = [grep { /access/ } @$lines];
347
348 $expected = 1;
349 my $matches = scalar(@$lines);
350 $self->assert($expected == $matches,
351 test_msg("Expected line count $expected, got $matches"));
352
353 my $actions = '';
354 foreach my $line (@$lines) {
355 if ($line =~ /^ftpdctl: (.*?)$/) {
356 $actions .= "$1";
357 }
358 }
359
360 $expected = 'access denied';
361 $self->assert($expected eq $actions,
362 test_msg("Expected '$expected', got '$actions'"));
363 };
364 if ($@) {
365 $ex = $@;
366 }
367
368 server_stop($setup->{pid_file});
369 test_cleanup($setup->{log_file}, $ex);
370 }
371
372 sub ctrls_sighup_bug3756 {
373 my $self = shift;
374 my $tmpdir = $self->{tmpdir};
375
376 my $config_file = "$tmpdir/ctrls.conf";
377 my $pid_file = File::Spec->rel2abs("$tmpdir/ctrls.pid");
378 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ctrls.scoreboard");
379
380 my $log_file = test_get_logfile();
381
382 my $ctrls_sock = File::Spec->rel2abs("$tmpdir/ctrls.sock");
383
384 my ($user, $group) = config_get_identity();
385
386 my $config = {
387 PidFile => $pid_file,
388 ScoreboardFile => $scoreboard_file,
389 SystemLog => $log_file,
390
391 IfModules => {
392 'mod_ctrls.c' => {
393 ControlsEngine => 'on',
394 ControlsLog => $log_file,
395 ControlsSocket => $ctrls_sock,
396 ControlsACLs => "all allow user root,$user",
397 ControlsSocketACL => "allow user root,$user",
398 },
399
400 'mod_delay.c' => {
401 DelayEngine => 'off',
402 },
403 },
404 };
405
406 my ($port, $config_user, $config_group) = config_write($config_file, $config);
407
408 my $ex;
409
410 # Start server
411 server_start($config_file);
412
413 sleep(1);
414
415 eval {
416 # Use proc(5) filesystem to count the number of open fds in the daemon
417 my $orig_nfds = server_open_fds($pid_file);
418 if ($ENV{TEST_VERBOSE}) {
419 print STDERR "Found $orig_nfds open fds after server startup\n";
420 }
421
422 # Restart the server
423 server_restart($pid_file);
424 sleep(1);
425
426 # Count the open fds again, make sure we haven't leaked any
427 my $restart_nfds = server_open_fds($pid_file);
428 if ($ENV{TEST_VERBOSE}) {
429 print STDERR "Found $restart_nfds open fds after server restart #1\n";
430 }
431
432 $self->assert($orig_nfds == $restart_nfds,
433 test_msg("Expected $orig_nfds open fds, found $restart_nfds"));
434
435 # Restart the server
436 server_restart($pid_file);
437 sleep(1);
438
439 # And count the open fds one more time, to make doubly sure we are not
440 # leaking fds.
441 $restart_nfds = server_open_fds($pid_file);
442 if ($ENV{TEST_VERBOSE}) {
443 print STDERR "Found $restart_nfds open fds after server restart #2\n";
444 }
445
446 $self->assert($orig_nfds == $restart_nfds,
447 test_msg("Expected $orig_nfds open fds, found $restart_nfds"));
448 };
449 if ($@) {
450 $ex = $@;
451 }
452
453 server_stop($pid_file);
454
455 if ($ex) {
456 test_append_logfile($log_file, $ex);
457 unlink($log_file);
458
459 die($ex);
460 }
461
462 unlink($log_file);
463 }
464
465 sub ctrls_intvl_timeoutlogin_bug4298 {
466 my $self = shift;
467 my $tmpdir = $self->{tmpdir};
468 my $setup = test_setup($tmpdir, 'ctrls');
469
470 my $ctrls_sock = File::Spec->rel2abs("$tmpdir/ctrls.sock");
471
472 my ($user, $group) = config_get_identity();
473 my $poll_interval = 2;
474
475 # Try to reproduce Bug#4298 by having the TimeoutLogin be a multiple of
476 # the Controls Interval.
477 my $timeout_login = ($poll_interval * 4);
478 my $timeout_idle = 30;
479
480 my $config = {
481 PidFile => $setup->{pid_file},
482 ScoreboardFile => $setup->{scoreboard_file},
483 SystemLog => $setup->{log_file},
484 TraceLog => $setup->{log_file},
485 Trace => 'ctrls:20 event:10 timers:20',
486
487 TimeoutIdle => $timeout_idle,
488 TimeoutLogin => $timeout_login,
489
490 IfModules => {
491 'mod_ctrls.c' => {
492 ControlsEngine => 'on',
493 ControlsLog => $setup->{log_file},
494 ControlsSocket => $ctrls_sock,
495 ControlsACLs => "all allow user *",
496 ControlsSocketACL => "allow user *",
497 ControlsInterval => $poll_interval,
498 },
499
500 'mod_delay.c' => {
501 DelayEngine => 'off',
502 },
503 },
504 };
505
506 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
507 $config);
508
509 # Open pipes, for use between the parent and child processes. Specifically,
510 # the child will indicate when it's done with its test by writing a message
511 # to the parent.
512 my ($rfh, $wfh);
513 unless (pipe($rfh, $wfh)) {
514 die("Can't open pipe: $!");
515 }
516
517 my $ex;
518
519 # Fork child
520 $self->handle_sigchld();
521 defined(my $pid = fork()) or die("Can't fork: $!");
522 if ($pid) {
523 eval {
524 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
525
526 # Wait for one second longer than the TimeoutLogin
527 if ($ENV{TEST_VERBOSE}) {
528 print STDERR "# Delaying for ", ($timeout_login + 1), " secs\n";
529 }
530
531 sleep($timeout_login + 1);
532
533 eval { $client->login($setup->{user}, $setup->{passwd}) };
534 unless ($@) {
535 $client->quit();
536 die("Login succeeded unexpectedly");
537 }
538 };
539 if ($@) {
540 $ex = $@;
541 }
542
543 $wfh->print("done\n");
544 $wfh->flush();
545
546 } else {
547 eval { server_wait($setup->{config_file}, $rfh, $timeout_idle + 3) };
548 if ($@) {
549 warn($@);
550 exit 1;
551 }
552
553 exit 0;
554 }
555
556 # Stop server
557 server_stop($setup->{pid_file});
558 $self->assert_child_ok($pid);
559
560 test_cleanup($setup->{log_file}, $ex);
561 }
562
563 1;