Debian Codegraph @

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

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