Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/FTPAccess/Empty.pm

raw
1 package ProFTPD::Tests::Config::FTPAccess::Empty;
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 empty_bug3240 => {
20 order => ++$order,
21 test_class => [qw(bug forking)],
22 },
23
24 empty_bug3279 => {
25 order => ++$order,
26 test_class => [qw(bug forking rootprivs)],
27 },
28
29 empty_bug3398 => {
30 order => ++$order,
31 test_class => [qw(bug forking rootprivs)],
32 },
33
34 };
35
36 sub new {
37 return shift()->SUPER::new(@_);
38 }
39
40 sub list_tests {
41 return testsuite_get_runnable_tests($TESTS);
42 }
43
44 sub empty_bug3240 {
45 my $self = shift;
46 my $tmpdir = $self->{tmpdir};
47
48 my $config_file = "$tmpdir/config.conf";
49 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
50 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
51
52 my $log_file = test_get_logfile();
53
54 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
55 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
56
57 # As per Bug#3240, a reproduction layout looks like:
58 #
59 # $home/.ftpaccess
60 #
61 # The .ftpaccess file should be empty.
62 #
63 # The proftpd.conf should contain something testable, e.g.:
64 #
65 # <Directory />
66 # <Limit LIST>
67 # DenyAll
68 # </Limit>
69 # </Directory>
70 #
71 # Note, however, that if "<Directory ~>" is used, the empty .ftpaccess
72 # file does not have an effect; figure out why.
73
74 my $user = 'proftpd';
75 my $passwd = 'test';
76 my $group = 'ftpd';
77 my $home_dir = File::Spec->rel2abs($tmpdir);
78 my $uid = 500;
79 my $gid = 500;
80
81 my $ftpaccess_file = File::Spec->rel2abs("$tmpdir/.ftpaccess");
82 if (open(my $fh, "> $ftpaccess_file")) {
83 unless (close($fh)) {
84 die("Can't write $ftpaccess_file: $!");
85 }
86
87 } else {
88 die("Can't open $ftpaccess_file: $!");
89 }
90
91 # Make sure that, if we're running as root, that the home directory has
92 # permissions/privs set for the account we create
93 if ($< == 0) {
94 unless (chmod(0755, $home_dir)) {
95 die("Can't set perms on $home_dir to 0755: $!");
96 }
97
98 unless (chown($uid, $gid, $home_dir)) {
99 die("Can't set owner of $home_dir to $uid/$gid: $!");
100 }
101 }
102
103 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
104 '/bin/bash');
105 auth_group_write($auth_group_file, $group, $gid, $user);
106
107 my $config = {
108 PidFile => $pid_file,
109 ScoreboardFile => $scoreboard_file,
110 SystemLog => $log_file,
111 TraceLog => $log_file,
112 Trace => 'DEFAULT:10 directory:10',
113
114 AuthUserFile => $auth_user_file,
115 AuthGroupFile => $auth_group_file,
116
117 AllowOverride => 'on',
118
119 Directory => {
120 '/' => {
121 Limit => {
122 NLST => {
123 DenyAll => '',
124 },
125 },
126 },
127 },
128
129 IfModules => {
130 'mod_delay.c' => {
131 DelayEngine => 'off',
132 },
133 },
134 };
135
136 my ($port, $config_user, $config_group) = config_write($config_file, $config);
137
138 # Open pipes, for use between the parent and child processes. Specifically,
139 # the child will indicate when it's done with its test by writing a message
140 # to the parent.
141 my ($rfh, $wfh);
142 unless (pipe($rfh, $wfh)) {
143 die("Can't open pipe: $!");
144 }
145
146 my $ex;
147
148 # Fork child
149 $self->handle_sigchld();
150 defined(my $pid = fork()) or die("Can't fork: $!");
151 if ($pid) {
152 eval {
153 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
154 $client->login($user, $passwd);
155
156 eval { $client->nlst() };
157 unless ($@) {
158 die("NLST succeeded unexpectedly");
159 }
160
161 my $resp_code = $client->response_code();
162 my $resp_msg = $client->response_msg();
163
164 my $expected;
165
166 $expected = 450;
167 $self->assert($expected == $resp_code,
168 test_msg("Expected $expected, got $resp_code"));
169 };
170
171 if ($@) {
172 $ex = $@;
173 }
174
175 $wfh->print("done\n");
176 $wfh->flush();
177
178 } else {
179 eval { server_wait($config_file, $rfh) };
180 if ($@) {
181 warn($@);
182 exit 1;
183 }
184
185 exit 0;
186 }
187
188 # Stop server
189 server_stop($pid_file);
190
191 $self->assert_child_ok($pid);
192
193 if ($ex) {
194 test_append_logfile($log_file, $ex);
195 unlink($log_file);
196
197 die($ex);
198 }
199
200 unlink($log_file);
201 }
202
203 sub empty_bug3279 {
204 my $self = shift;
205 my $tmpdir = $self->{tmpdir};
206
207 my $config_file = "$tmpdir/config.conf";
208 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
209 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
210
211 my $log_file = test_get_logfile();
212
213 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
214 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
215
216 # As per Bug#3279, a reproduction layout looks like:
217 #
218 # $home/.ftpaccess
219 #
220 # The .ftpaccess file should be empty.
221 #
222 # The proftpd.conf should contain:
223 #
224 # <Directory />
225 # HideFiles "\\.ftpaccess$"
226 # </Directory>
227 #
228 # The bug report claims that using "AllowOverride off" causes this
229 # bug not to trigger. Don't know if the .ftpaccess file that should
230 # be hidden is empty or not.
231
232 my $user = 'proftpd';
233 my $passwd = 'test';
234 my $group = 'ftpd';
235 my $home_dir = File::Spec->rel2abs($tmpdir);
236 my $uid = 500;
237 my $gid = 500;
238
239 my $ftpaccess_file = File::Spec->rel2abs("$home_dir/.ftpaccess");
240 if (open(my $fh, "> $ftpaccess_file")) {
241 unless (close($fh)) {
242 die("Can't write $ftpaccess_file: $!");
243 }
244
245 } else {
246 die("Can't open $ftpaccess_file: $!");
247 }
248
249 # Make sure that, if we're running as root, that the home directory has
250 # permissions/privs set for the account we create
251 if ($< == 0) {
252 unless (chmod(0755, $home_dir)) {
253 die("Can't set perms on $home_dir to 0755: $!");
254 }
255
256 unless (chown($uid, $gid, $home_dir)) {
257 die("Can't set owner of $home_dir to $uid/$gid: $!");
258 }
259 }
260
261 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
262 '/bin/bash');
263 auth_group_write($auth_group_file, $group, $gid, $user);
264
265 my $config = {
266 PidFile => $pid_file,
267 ScoreboardFile => $scoreboard_file,
268 SystemLog => $log_file,
269 TraceLog => $log_file,
270 Trace => 'DEFAULT:10 ftpaccess:10',
271
272 AuthUserFile => $auth_user_file,
273 AuthGroupFile => $auth_group_file,
274 DefaultRoot => '~',
275
276 AllowOverride => 'on',
277
278 IfModules => {
279 'mod_delay.c' => {
280 DelayEngine => 'off',
281 },
282 },
283 };
284
285 my ($port, $config_user, $config_group) = config_write($config_file, $config);
286
287 if (open(my $fh, ">> $config_file")) {
288 print $fh <<EOD;
289 <Directory />
290 HideFiles ^\\.ftpaccess\$
291 </Directory>
292 EOD
293 unless (close($fh)) {
294 die("Can't write $config_file: $!");
295 }
296
297 } else {
298 die("Can't open $config_file: $!");
299 }
300
301 # Open pipes, for use between the parent and child processes. Specifically,
302 # the child will indicate when it's done with its test by writing a message
303 # to the parent.
304 my ($rfh, $wfh);
305 unless (pipe($rfh, $wfh)) {
306 die("Can't open pipe: $!");
307 }
308
309 my $ex;
310
311 # Fork child
312 $self->handle_sigchld();
313 defined(my $pid = fork()) or die("Can't fork: $!");
314 if ($pid) {
315 eval {
316 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
317 $client->login($user, $passwd);
318
319 my $conn = $client->list_raw("-a");
320 unless ($conn) {
321 die("Failed to LIST: " . $client->response_code() . " " .
322 $client->response_msg());
323 }
324
325 my $buf;
326 my $tmp;
327 while ($conn->read($tmp, 8192, 30)) {
328 $buf .= $tmp;
329 }
330 eval { $conn->close() };
331
332 # We have to be careful of the fact that readdir returns directory
333 # entries in an unordered fashion.
334 my $res = {};
335 my $lines = [split(/\n/, $buf)];
336 foreach my $line (@$lines) {
337 if ($line =~ /^\S+\s+\d+\s+\S+\s+\S+\s+.*?\s+(\S+)$/) {
338 $res->{$1} = 1;
339 }
340 }
341
342 unless (scalar(keys(%$res)) > 0) {
343 die("LIST returned no files unexpectedly");
344 }
345
346 my $expected = {
347 '.' => 1,
348 '..' => 1,
349 'config.conf' => 1,
350 'config.group' => 1,
351 'config.passwd' => 1,
352 'config.pid' => 1,
353 'config.scoreboard' => 1,
354 };
355
356 my $ok = 1;
357 my $mismatch;
358 foreach my $name (keys(%$res)) {
359 unless (defined($expected->{$name})) {
360 $mismatch = $name;
361 $ok = 0;
362 last;
363 }
364 }
365
366 unless ($ok) {
367 die("Unexpected name '$mismatch' appeared in LIST data")
368 }
369
370 $client->quit();
371 };
372
373 if ($@) {
374 $ex = $@;
375 }
376
377 $wfh->print("done\n");
378 $wfh->flush();
379
380 } else {
381 eval { server_wait($config_file, $rfh) };
382 if ($@) {
383 warn($@);
384 exit 1;
385 }
386
387 exit 0;
388 }
389
390 # Stop server
391 server_stop($pid_file);
392
393 $self->assert_child_ok($pid);
394
395 if ($ex) {
396 test_append_logfile($log_file, $ex);
397 unlink($log_file);
398
399 die($ex);
400 }
401
402 unlink($log_file);
403 }
404
405 sub empty_bug3398 {
406 my $self = shift;
407 my $tmpdir = $self->{tmpdir};
408
409 my $config_file = "$tmpdir/config.conf";
410 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
411 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
412
413 my $log_file = test_get_logfile();
414
415 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
416 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
417
418 # As per Bug#3398, a reproduction layout looks like:
419 #
420 # $home/.ftpaccess
421 #
422 # The .ftpaccess file should be non-empty.
423 #
424 # There should also be:
425 #
426 # $home/subdir1/.ftpaccess
427 #
428 # which *is* empty.
429
430 my $user = 'proftpd';
431 my $passwd = 'test';
432 my $group = 'ftpd';
433 my $home_dir = File::Spec->rel2abs($tmpdir);
434 my $uid = 500;
435 my $gid = 500;
436
437 my $ftpaccess_file = File::Spec->rel2abs("$home_dir/.ftpaccess");
438 if (open(my $fh, "> $ftpaccess_file")) {
439 print $fh <<EOC;
440 <Limit ALL>
441 AllowAll
442 </Limit>
443 EOC
444 unless (close($fh)) {
445 die("Can't write $ftpaccess_file: $!");
446 }
447
448 } else {
449 die("Can't open $ftpaccess_file: $!");
450 }
451
452 my $subdir1 = File::Spec->rel2abs("$home_dir/subdir1");
453 mkpath($subdir1);
454
455 $ftpaccess_file = File::Spec->rel2abs("$subdir1/.ftpaccess");
456 if (open(my $fh, "> $ftpaccess_file")) {
457 unless (close($fh)) {
458 die("Can't write $ftpaccess_file: $!");
459 }
460
461 } else {
462 die("Can't open $ftpaccess_file: $!");
463 }
464
465 my $subdir2 = File::Spec->rel2abs("$home_dir/subdir2");
466 mkpath($subdir2);
467
468 # Make sure that, if we're running as root, that the home directory has
469 # permissions/privs set for the account we create
470 if ($< == 0) {
471 unless (chmod(0755, $home_dir, $subdir1, $subdir2)) {
472 die("Can't set perms on $home_dir to 0755: $!");
473 }
474
475 unless (chown($uid, $gid, $home_dir, $subdir1, $subdir2)) {
476 die("Can't set owner of $home_dir to $uid/$gid: $!");
477 }
478 }
479
480 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
481 '/bin/bash');
482 auth_group_write($auth_group_file, $group, $gid, $user);
483
484 my $config = {
485 PidFile => $pid_file,
486 ScoreboardFile => $scoreboard_file,
487 SystemLog => $log_file,
488 TraceLog => $log_file,
489 Trace => 'DEFAULT:10 ftpaccess:10',
490
491 AuthUserFile => $auth_user_file,
492 AuthGroupFile => $auth_group_file,
493 DefaultRoot => '~',
494
495 AllowOverride => 'on',
496 ListOptions => '-la',
497
498 IfModules => {
499 'mod_delay.c' => {
500 DelayEngine => 'off',
501 },
502 },
503 };
504
505 my ($port, $config_user, $config_group) = config_write($config_file, $config);
506
507 # Open pipes, for use between the parent and child processes. Specifically,
508 # the child will indicate when it's done with its test by writing a message
509 # to the parent.
510 my ($rfh, $wfh);
511 unless (pipe($rfh, $wfh)) {
512 die("Can't open pipe: $!");
513 }
514
515 my $ex;
516
517 # Fork child
518 $self->handle_sigchld();
519 defined(my $pid = fork()) or die("Can't fork: $!");
520 if ($pid) {
521 eval {
522 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
523 $client->login($user, $passwd);
524
525 $client->pwd();
526 $client->epsv();
527 $client->list();
528
529 $client->cwd('subdir2');
530 $client->pwd();
531 $client->epsv();
532 $client->list();
533
534 $client->cwd('..');
535 $client->pwd();
536 $client->epsv();
537 $client->list();
538
539 $client->quit();
540 };
541
542 if ($@) {
543 $ex = $@;
544 }
545
546 $wfh->print("done\n");
547 $wfh->flush();
548
549 } else {
550 eval { server_wait($config_file, $rfh) };
551 if ($@) {
552 warn($@);
553 exit 1;
554 }
555
556 exit 0;
557 }
558
559 # Stop server
560 server_stop($pid_file);
561
562 $self->assert_child_ok($pid);
563
564 if ($ex) {
565 test_append_logfile($log_file, $ex);
566 unlink($log_file);
567
568 die($ex);
569 }
570
571 unlink($log_file);
572 }
573
574 1;