Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_quotatab/copy.pm

raw
1 package ProFTPD::Tests::Modules::mod_quotatab::copy;
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 quotatab_site_copy_user_limit_bug3641 => {
20 order => ++$order,
21 test_class => [qw(bug forking mod_copy)],
22 },
23
24 quotatab_site_copy_user_limit_bytes_in_exceeded_soft_limit_bug3641 => {
25 order => ++$order,
26 test_class => [qw(bug forking mod_copy)],
27 },
28
29 quotatab_site_copy_user_limit_bytes_in_exceeded_hard_limit_bug3641 => {
30 order => ++$order,
31 test_class => [qw(bug forking mod_copy)],
32 },
33
34 quotatab_site_copy_user_limit_files_in_reached_bug3641 => {
35 order => ++$order,
36 test_class => [qw(bug forking mod_copy)],
37 },
38
39 quotatab_site_copy_user_limit_files_in_exceeded_bug3641 => {
40 order => ++$order,
41 test_class => [qw(bug forking mod_copy)],
42 },
43
44 quotatab_site_copy_src_dir_bug3641 => {
45 order => ++$order,
46 test_class => [qw(bug forking mod_copy)],
47 },
48
49 quotatab_site_copy_src_dir_bytes_in_exceeded_bug3641 => {
50 order => ++$order,
51 test_class => [qw(bug forking mod_copy)],
52 },
53
54 quotatab_site_copy_src_dir_bytes_in_exceeded_chrooted_bug3641 => {
55 order => ++$order,
56 test_class => [qw(bug forking mod_copy rootprivs)],
57 },
58
59 quotatab_site_copy_src_dir_files_in_exceeded_bug3641 => {
60 order => ++$order,
61 test_class => [qw(bug forking mod_copy)],
62 },
63
64 quotatab_site_copy_src_dir_files_in_exceeded_chrooted_bug3641 => {
65 order => ++$order,
66 test_class => [qw(bug forking mod_copy rootprivs)],
67 },
68
69 quotatab_site_copy_src_dir_directory_tally_bug3641 => {
70 order => ++$order,
71 test_class => [qw(bug forking mod_copy)],
72 },
73
74 };
75
76 sub new {
77 return shift()->SUPER::new(@_);
78 }
79
80 sub list_tests {
81 return testsuite_get_runnable_tests($TESTS);
82 }
83
84 sub get_tally {
85 my $db_file = shift;
86 my $where = shift;
87
88 my $sql = "SELECT quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies";
89 if ($where) {
90 $sql .= " WHERE $where";
91 }
92
93 my $cmd = "sqlite3 $db_file \"$sql\"";
94
95 if ($ENV{TEST_VERBOSE}) {
96 print STDERR "Executing sqlite3: $cmd\n";
97 }
98
99 my $res = join('', `$cmd`);
100
101 # The default sqlite3 delimiter is '|'
102 return split(/\|/, $res);
103 }
104
105 sub quotatab_site_copy_user_limit_bug3641 {
106 my $self = shift;
107 my $tmpdir = $self->{tmpdir};
108
109 my $config_file = "$tmpdir/quotatab.conf";
110 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
111 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
112
113 my $log_file = test_get_logfile();
114
115 my $user = 'proftpd';
116 my $passwd = 'test';
117 my $group = 'ftpd';
118 my $home_dir = File::Spec->rel2abs($tmpdir);
119 my $uid = 500;
120 my $gid = 500;
121
122 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
123
124 # Build up sqlite3 command to create users, groups tables and populate them
125 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
126
127 if (open(my $fh, "> $db_script")) {
128 print $fh <<EOS;
129 CREATE TABLE users (
130 userid TEXT,
131 passwd TEXT,
132 uid INTEGER,
133 gid INTEGER,
134 homedir TEXT,
135 shell TEXT,
136 lastdir TEXT
137 );
138 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
139
140 CREATE TABLE groups (
141 groupname TEXT,
142 gid INTEGER,
143 members TEXT
144 );
145 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
146
147 CREATE TABLE quotalimits (
148 name TEXT NOT NULL,
149 quota_type TEXT NOT NULL,
150 per_session TEXT NOT NULL,
151 limit_type TEXT NOT NULL,
152 bytes_in_avail REAL NOT NULL,
153 bytes_out_avail REAL NOT NULL,
154 bytes_xfer_avail REAL NOT NULL,
155 files_in_avail INTEGER NOT NULL,
156 files_out_avail INTEGER NOT NULL,
157 files_xfer_avail INTEGER NOT NULL
158 );
159 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 32, 0, 0, 2, 0, 0);
160
161 CREATE TABLE quotatallies (
162 name TEXT NOT NULL,
163 quota_type TEXT NOT NULL,
164 bytes_in_used REAL NOT NULL,
165 bytes_out_used REAL NOT NULL,
166 bytes_xfer_used REAL NOT NULL,
167 files_in_used INTEGER NOT NULL,
168 files_out_used INTEGER NOT NULL,
169 files_xfer_used INTEGER NOT NULL
170 );
171 EOS
172
173 unless (close($fh)) {
174 die("Can't write $db_script: $!");
175 }
176
177 } else {
178 die("Can't open $db_script: $!");
179 }
180
181 my $cmd = "sqlite3 $db_file < $db_script";
182
183 if ($ENV{TEST_VERBOSE}) {
184 print STDERR "Executing sqlite3: $cmd\n";
185 }
186
187 my @output = `$cmd`;
188 if (scalar(@output) &&
189 $ENV{TEST_VERBOSE}) {
190 print STDERR "Output: ", join('', @output), "\n";
191 }
192
193 my $src_file = File::Spec->rel2abs("$tmpdir/foo.txt");
194 if (open(my $fh, "> $src_file")) {
195 print $fh "Hello, World!\n";
196 unless (close($fh)) {
197 die("Can't write $src_file: $!");
198 }
199
200 } else {
201 die("Can't open $src_file: $!");
202 }
203
204 my $dst_file = File::Spec->rel2abs("$tmpdir/bar.txt");
205
206 my $config = {
207 PidFile => $pid_file,
208 ScoreboardFile => $scoreboard_file,
209 SystemLog => $log_file,
210
211 DefaultChdir => '~',
212
213 IfModules => {
214 'mod_delay.c' => {
215 DelayEngine => 'off',
216 },
217
218 'mod_quotatab_sql.c' => [
219 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
220 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
221 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
222 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
223
224 'QuotaEngine on',
225 "QuotaLog $log_file",
226 'QuotaLimitTable sql:/get-quota-limit',
227 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
228 ],
229
230 'mod_sql.c' => {
231 SQLAuthTypes => 'plaintext',
232 SQLBackend => 'sqlite3',
233 SQLConnectInfo => $db_file,
234 SQLLogFile => $log_file,
235 SQLMinID => '0',
236 },
237 },
238 };
239
240 my ($port, $config_user, $config_group) = config_write($config_file, $config);
241
242 # Open pipes, for use between the parent and child processes. Specifically,
243 # the child will indicate when it's done with its test by writing a message
244 # to the parent.
245 my ($rfh, $wfh);
246 unless (pipe($rfh, $wfh)) {
247 die("Can't open pipe: $!");
248 }
249
250 my $ex;
251
252 # Fork child
253 $self->handle_sigchld();
254 defined(my $pid = fork()) or die("Can't fork: $!");
255 if ($pid) {
256 eval {
257 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
258 $client->login($user, $passwd);
259
260 my ($resp_code, $resp_msg) = $client->site('CPFR', 'foo.txt');
261
262 my $expected;
263
264 $expected = 350;
265 $self->assert($expected == $resp_code,
266 test_msg("Expected $expected, got $resp_code"));
267
268 $expected = "File or directory exists, ready for destination name";
269 $self->assert($expected eq $resp_msg,
270 test_msg("Expected '$expected', got '$resp_msg'"));
271
272 ($resp_code, $resp_msg) = $client->site('CPTO', 'bar.txt');
273
274 $expected = 250;
275 $self->assert($expected == $resp_code,
276 test_msg("Expected $expected, got $resp_code"));
277
278 $expected = "Copy successful";
279 $self->assert($expected eq $resp_msg,
280 test_msg("Expected '$expected', got '$resp_msg'"));
281
282 };
283
284 if ($@) {
285 $ex = $@;
286 }
287
288 $wfh->print("done\n");
289 $wfh->flush();
290
291 } else {
292 eval { server_wait($config_file, $rfh) };
293 if ($@) {
294 warn($@);
295 exit 1;
296 }
297
298 exit 0;
299 }
300
301 # Stop server
302 server_stop($pid_file);
303
304 $self->assert_child_ok($pid);
305
306 if ($ex) {
307 test_append_logfile($log_file, $ex);
308 unlink($log_file);
309
310 die($ex);
311 }
312
313 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
314
315 my $expected;
316
317 $expected = 'user';
318 $self->assert($expected eq $quota_type,
319 test_msg("Expected '$expected', got '$quota_type'"));
320
321 $expected = '^(14.0|14)$';
322 $self->assert(qr/$expected/, $bytes_in_used,
323 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
324
325 $expected = '^(0.0|0)$';
326 $self->assert(qr/$expected/, $bytes_out_used,
327 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
328
329 $expected = '^(0.0|0)$';
330 $self->assert(qr/$expected/, $bytes_xfer_used,
331 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
332
333 $expected = 1;
334 $self->assert($expected == $files_in_used,
335 test_msg("Expected $expected files-in-used, got $files_in_used"));
336
337 $expected = 0;
338 $self->assert($expected == $files_out_used,
339 test_msg("Expected $expected files-out-used, got $files_out_used"));
340
341 $expected = 0;
342 $self->assert($expected == $files_xfer_used,
343 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
344
345 unlink($log_file);
346 }
347
348 sub quotatab_site_copy_user_limit_bytes_in_exceeded_soft_limit_bug3641 {
349 my $self = shift;
350 my $tmpdir = $self->{tmpdir};
351
352 my $config_file = "$tmpdir/quotatab.conf";
353 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
354 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
355
356 my $log_file = test_get_logfile();
357
358 my $user = 'proftpd';
359 my $passwd = 'test';
360 my $group = 'ftpd';
361 my $home_dir = File::Spec->rel2abs($tmpdir);
362 my $uid = 500;
363 my $gid = 500;
364
365 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
366
367 # Build up sqlite3 command to create users, groups tables and populate them
368 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
369
370 if (open(my $fh, "> $db_script")) {
371 print $fh <<EOS;
372 CREATE TABLE users (
373 userid TEXT,
374 passwd TEXT,
375 uid INTEGER,
376 gid INTEGER,
377 homedir TEXT,
378 shell TEXT,
379 lastdir TEXT
380 );
381 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
382
383 CREATE TABLE groups (
384 groupname TEXT,
385 gid INTEGER,
386 members TEXT
387 );
388 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
389
390 CREATE TABLE quotalimits (
391 name TEXT NOT NULL,
392 quota_type TEXT NOT NULL,
393 per_session TEXT NOT NULL,
394 limit_type TEXT NOT NULL,
395 bytes_in_avail REAL NOT NULL,
396 bytes_out_avail REAL NOT NULL,
397 bytes_xfer_avail REAL NOT NULL,
398 files_in_avail INTEGER NOT NULL,
399 files_out_avail INTEGER NOT NULL,
400 files_xfer_avail INTEGER NOT NULL
401 );
402 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 5, 0, 0, 2, 0, 0);
403
404 CREATE TABLE quotatallies (
405 name TEXT NOT NULL,
406 quota_type TEXT NOT NULL,
407 bytes_in_used REAL NOT NULL,
408 bytes_out_used REAL NOT NULL,
409 bytes_xfer_used REAL NOT NULL,
410 files_in_used INTEGER NOT NULL,
411 files_out_used INTEGER NOT NULL,
412 files_xfer_used INTEGER NOT NULL
413 );
414 EOS
415
416 unless (close($fh)) {
417 die("Can't write $db_script: $!");
418 }
419
420 } else {
421 die("Can't open $db_script: $!");
422 }
423
424 my $cmd = "sqlite3 $db_file < $db_script";
425
426 if ($ENV{TEST_VERBOSE}) {
427 print STDERR "Executing sqlite3: $cmd\n";
428 }
429
430 my @output = `$cmd`;
431 if (scalar(@output) &&
432 $ENV{TEST_VERBOSE}) {
433 print STDERR "Output: ", join('', @output), "\n";
434 }
435
436 my $src_file = File::Spec->rel2abs("$tmpdir/foo.txt");
437 if (open(my $fh, "> $src_file")) {
438 print $fh "Hello, World!\n";
439 unless (close($fh)) {
440 die("Can't write $src_file: $!");
441 }
442
443 } else {
444 die("Can't open $src_file: $!");
445 }
446
447 my $dst_file = File::Spec->rel2abs("$tmpdir/bar.txt");
448
449 my $config = {
450 PidFile => $pid_file,
451 ScoreboardFile => $scoreboard_file,
452 SystemLog => $log_file,
453
454 DefaultChdir => '~',
455
456 IfModules => {
457 'mod_delay.c' => {
458 DelayEngine => 'off',
459 },
460
461 'mod_quotatab_sql.c' => [
462 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
463 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
464 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
465 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
466
467 'QuotaEngine on',
468 "QuotaLog $log_file",
469 'QuotaLimitTable sql:/get-quota-limit',
470 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
471 ],
472
473 'mod_sql.c' => {
474 SQLAuthTypes => 'plaintext',
475 SQLBackend => 'sqlite3',
476 SQLConnectInfo => $db_file,
477 SQLLogFile => $log_file,
478 SQLMinID => '0',
479 },
480 },
481 };
482
483 my ($port, $config_user, $config_group) = config_write($config_file, $config);
484
485 # Open pipes, for use between the parent and child processes. Specifically,
486 # the child will indicate when it's done with its test by writing a message
487 # to the parent.
488 my ($rfh, $wfh);
489 unless (pipe($rfh, $wfh)) {
490 die("Can't open pipe: $!");
491 }
492
493 my $ex;
494
495 # Fork child
496 $self->handle_sigchld();
497 defined(my $pid = fork()) or die("Can't fork: $!");
498 if ($pid) {
499 eval {
500 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
501 $client->login($user, $passwd);
502
503 my ($resp_code, $resp_msg) = $client->site('CPFR', 'foo.txt');
504
505 my $expected;
506
507 $expected = 350;
508 $self->assert($expected == $resp_code,
509 test_msg("Expected $expected, got $resp_code"));
510
511 $expected = "File or directory exists, ready for destination name";
512 $self->assert($expected eq $resp_msg,
513 test_msg("Expected '$expected', got '$resp_msg'"));
514
515 ($resp_code, $resp_msg) = $client->site('CPTO', 'bar.txt');
516
517 $expected = 250;
518 $self->assert($expected == $resp_code,
519 test_msg("Expected $expected, got $resp_code"));
520
521 $expected = 'COPY: notice: quota reached: used \S+ of \S+ upload bytes';
522 $self->assert(qr/$expected/, $resp_msg,
523 test_msg("Expected '$expected', got '$resp_msg'"));
524
525 };
526
527 if ($@) {
528 $ex = $@;
529 }
530
531 $wfh->print("done\n");
532 $wfh->flush();
533
534 } else {
535 eval { server_wait($config_file, $rfh) };
536 if ($@) {
537 warn($@);
538 exit 1;
539 }
540
541 exit 0;
542 }
543
544 # Stop server
545 server_stop($pid_file);
546
547 $self->assert_child_ok($pid);
548
549 if ($ex) {
550 test_append_logfile($log_file, $ex);
551 unlink($log_file);
552
553 die($ex);
554 }
555
556 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
557
558 my $expected;
559
560 $expected = 'user';
561 $self->assert($expected eq $quota_type,
562 test_msg("Expected '$expected', got '$quota_type'"));
563
564 $expected = '^(14.0|14)$';
565 $self->assert(qr/$expected/, $bytes_in_used,
566 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
567
568 $expected = '^(0.0|0)$';
569 $self->assert(qr/$expected/, $bytes_out_used,
570 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
571
572 $expected = '^(0.0|0)$';
573 $self->assert(qr/$expected/, $bytes_xfer_used,
574 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
575
576 $expected = 1;
577 $self->assert($expected == $files_in_used,
578 test_msg("Expected $expected files-in-used, got $files_in_used"));
579
580 $expected = 0;
581 $self->assert($expected == $files_out_used,
582 test_msg("Expected $expected files-out-used, got $files_out_used"));
583
584 $expected = 0;
585 $self->assert($expected == $files_xfer_used,
586 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
587
588 unlink($log_file);
589 }
590
591 sub quotatab_site_copy_user_limit_bytes_in_exceeded_hard_limit_bug3641 {
592 my $self = shift;
593 my $tmpdir = $self->{tmpdir};
594
595 my $config_file = "$tmpdir/quotatab.conf";
596 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
597 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
598
599 my $log_file = test_get_logfile();
600
601 my $user = 'proftpd';
602 my $passwd = 'test';
603 my $group = 'ftpd';
604 my $home_dir = File::Spec->rel2abs($tmpdir);
605 my $uid = 500;
606 my $gid = 500;
607
608 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
609
610 # Build up sqlite3 command to create users, groups tables and populate them
611 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
612
613 if (open(my $fh, "> $db_script")) {
614 print $fh <<EOS;
615 CREATE TABLE users (
616 userid TEXT,
617 passwd TEXT,
618 uid INTEGER,
619 gid INTEGER,
620 homedir TEXT,
621 shell TEXT,
622 lastdir TEXT
623 );
624 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
625
626 CREATE TABLE groups (
627 groupname TEXT,
628 gid INTEGER,
629 members TEXT
630 );
631 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
632
633 CREATE TABLE quotalimits (
634 name TEXT NOT NULL,
635 quota_type TEXT NOT NULL,
636 per_session TEXT NOT NULL,
637 limit_type TEXT NOT NULL,
638 bytes_in_avail REAL NOT NULL,
639 bytes_out_avail REAL NOT NULL,
640 bytes_xfer_avail REAL NOT NULL,
641 files_in_avail INTEGER NOT NULL,
642 files_out_avail INTEGER NOT NULL,
643 files_xfer_avail INTEGER NOT NULL
644 );
645 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'hard', 5, 0, 0, 2, 0, 0);
646
647 CREATE TABLE quotatallies (
648 name TEXT NOT NULL,
649 quota_type TEXT NOT NULL,
650 bytes_in_used REAL NOT NULL,
651 bytes_out_used REAL NOT NULL,
652 bytes_xfer_used REAL NOT NULL,
653 files_in_used INTEGER NOT NULL,
654 files_out_used INTEGER NOT NULL,
655 files_xfer_used INTEGER NOT NULL
656 );
657 EOS
658
659 unless (close($fh)) {
660 die("Can't write $db_script: $!");
661 }
662
663 } else {
664 die("Can't open $db_script: $!");
665 }
666
667 my $cmd = "sqlite3 $db_file < $db_script";
668
669 if ($ENV{TEST_VERBOSE}) {
670 print STDERR "Executing sqlite3: $cmd\n";
671 }
672
673 my @output = `$cmd`;
674 if (scalar(@output) &&
675 $ENV{TEST_VERBOSE}) {
676 print STDERR "Output: ", join('', @output), "\n";
677 }
678
679 my $src_file = File::Spec->rel2abs("$tmpdir/foo.txt");
680 if (open(my $fh, "> $src_file")) {
681 print $fh "Hello, World!\n";
682 unless (close($fh)) {
683 die("Can't write $src_file: $!");
684 }
685
686 } else {
687 die("Can't open $src_file: $!");
688 }
689
690 my $dst_file = File::Spec->rel2abs("$tmpdir/bar.txt");
691
692 my $config = {
693 PidFile => $pid_file,
694 ScoreboardFile => $scoreboard_file,
695 SystemLog => $log_file,
696
697 DefaultChdir => '~',
698
699 IfModules => {
700 'mod_delay.c' => {
701 DelayEngine => 'off',
702 },
703
704 'mod_quotatab_sql.c' => [
705 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
706 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
707 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
708 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
709
710 'QuotaEngine on',
711 "QuotaLog $log_file",
712 'QuotaLimitTable sql:/get-quota-limit',
713 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
714 ],
715
716 'mod_sql.c' => {
717 SQLAuthTypes => 'plaintext',
718 SQLBackend => 'sqlite3',
719 SQLConnectInfo => $db_file,
720 SQLLogFile => $log_file,
721 SQLMinID => '0',
722 },
723 },
724 };
725
726 my ($port, $config_user, $config_group) = config_write($config_file, $config);
727
728 # Open pipes, for use between the parent and child processes. Specifically,
729 # the child will indicate when it's done with its test by writing a message
730 # to the parent.
731 my ($rfh, $wfh);
732 unless (pipe($rfh, $wfh)) {
733 die("Can't open pipe: $!");
734 }
735
736 my $ex;
737
738 # Fork child
739 $self->handle_sigchld();
740 defined(my $pid = fork()) or die("Can't fork: $!");
741 if ($pid) {
742 eval {
743 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
744 $client->login($user, $passwd);
745
746 my ($resp_code, $resp_msg) = $client->site('CPFR', 'foo.txt');
747
748 my $expected;
749
750 $expected = 350;
751 $self->assert($expected == $resp_code,
752 test_msg("Expected $expected, got $resp_code"));
753
754 $expected = "File or directory exists, ready for destination name";
755 $self->assert($expected eq $resp_msg,
756 test_msg("Expected '$expected', got '$resp_msg'"));
757
758 ($resp_code, $resp_msg) = $client->site('CPTO', 'bar.txt');
759
760 $expected = 250;
761 $self->assert($expected == $resp_code,
762 test_msg("Expected $expected, got $resp_code"));
763
764 $expected = 'COPY: notice: quota reached: used \S+ of \S+ upload bytes';
765 $self->assert(qr/$expected/, $resp_msg,
766 test_msg("Expected '$expected', got '$resp_msg'"));
767
768 $resp_msg = $client->response_msg(2);
769 $expected = "COPY: notice: quota reached: 'bar.txt' removed";
770 $self->assert($expected eq $resp_msg,
771 test_msg("Expected '$expected', got '$resp_msg'"));
772
773 $self->assert(!-f $dst_file,
774 test_msg("File $dst_file exists unexpectedly"));
775 };
776
777 if ($@) {
778 $ex = $@;
779 }
780
781 $wfh->print("done\n");
782 $wfh->flush();
783
784 } else {
785 eval { server_wait($config_file, $rfh) };
786 if ($@) {
787 warn($@);
788 exit 1;
789 }
790
791 exit 0;
792 }
793
794 # Stop server
795 server_stop($pid_file);
796
797 $self->assert_child_ok($pid);
798
799 if ($ex) {
800 test_append_logfile($log_file, $ex);
801 unlink($log_file);
802
803 die($ex);
804 }
805
806 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
807
808 my $expected;
809
810 $expected = 'user';
811 $self->assert($expected eq $quota_type,
812 test_msg("Expected '$expected', got '$quota_type'"));
813
814 $expected = '^(0.0|0)$';
815 $self->assert(qr/$expected/, $bytes_in_used,
816 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
817
818 $expected = '^(0.0|0)$';
819 $self->assert(qr/$expected/, $bytes_out_used,
820 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
821
822 $expected = '^(0.0|0)$';
823 $self->assert(qr/$expected/, $bytes_xfer_used,
824 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
825
826 $expected = 0;
827 $self->assert($expected == $files_in_used,
828 test_msg("Expected $expected files-in-used, got $files_in_used"));
829
830 $expected = 0;
831 $self->assert($expected == $files_out_used,
832 test_msg("Expected $expected files-out-used, got $files_out_used"));
833
834 $expected = 0;
835 $self->assert($expected == $files_xfer_used,
836 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
837
838 unlink($log_file);
839 }
840
841 sub quotatab_site_copy_user_limit_files_in_reached_bug3641 {
842 my $self = shift;
843 my $tmpdir = $self->{tmpdir};
844
845 my $config_file = "$tmpdir/quotatab.conf";
846 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
847 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
848
849 my $log_file = test_get_logfile();
850
851 my $user = 'proftpd';
852 my $passwd = 'test';
853 my $group = 'ftpd';
854 my $home_dir = File::Spec->rel2abs($tmpdir);
855 my $uid = 500;
856 my $gid = 500;
857
858 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
859
860 # Build up sqlite3 command to create users, groups tables and populate them
861 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
862
863 if (open(my $fh, "> $db_script")) {
864 print $fh <<EOS;
865 CREATE TABLE users (
866 userid TEXT,
867 passwd TEXT,
868 uid INTEGER,
869 gid INTEGER,
870 homedir TEXT,
871 shell TEXT,
872 lastdir TEXT
873 );
874 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
875
876 CREATE TABLE groups (
877 groupname TEXT,
878 gid INTEGER,
879 members TEXT
880 );
881 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
882
883 CREATE TABLE quotalimits (
884 name TEXT NOT NULL,
885 quota_type TEXT NOT NULL,
886 per_session TEXT NOT NULL,
887 limit_type TEXT NOT NULL,
888 bytes_in_avail REAL NOT NULL,
889 bytes_out_avail REAL NOT NULL,
890 bytes_xfer_avail REAL NOT NULL,
891 files_in_avail INTEGER NOT NULL,
892 files_out_avail INTEGER NOT NULL,
893 files_xfer_avail INTEGER NOT NULL
894 );
895 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'hard', 32, 0, 0, 1, 0, 0);
896
897 CREATE TABLE quotatallies (
898 name TEXT NOT NULL,
899 quota_type TEXT NOT NULL,
900 bytes_in_used REAL NOT NULL,
901 bytes_out_used REAL NOT NULL,
902 bytes_xfer_used REAL NOT NULL,
903 files_in_used INTEGER NOT NULL,
904 files_out_used INTEGER NOT NULL,
905 files_xfer_used INTEGER NOT NULL
906 );
907 EOS
908
909 unless (close($fh)) {
910 die("Can't write $db_script: $!");
911 }
912
913 } else {
914 die("Can't open $db_script: $!");
915 }
916
917 my $cmd = "sqlite3 $db_file < $db_script";
918
919 if ($ENV{TEST_VERBOSE}) {
920 print STDERR "Executing sqlite3: $cmd\n";
921 }
922
923 my @output = `$cmd`;
924 if (scalar(@output) &&
925 $ENV{TEST_VERBOSE}) {
926 print STDERR "Output: ", join('', @output), "\n";
927 }
928
929 my $src_file = File::Spec->rel2abs("$tmpdir/foo.txt");
930 if (open(my $fh, "> $src_file")) {
931 print $fh "Hello, World!\n";
932 unless (close($fh)) {
933 die("Can't write $src_file: $!");
934 }
935
936 } else {
937 die("Can't open $src_file: $!");
938 }
939
940 my $dst_file = File::Spec->rel2abs("$tmpdir/bar.txt");
941
942 my $config = {
943 PidFile => $pid_file,
944 ScoreboardFile => $scoreboard_file,
945 SystemLog => $log_file,
946
947 DefaultChdir => '~',
948
949 IfModules => {
950 'mod_delay.c' => {
951 DelayEngine => 'off',
952 },
953
954 'mod_quotatab_sql.c' => [
955 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
956 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
957 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
958 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
959
960 'QuotaEngine on',
961 "QuotaLog $log_file",
962 'QuotaLimitTable sql:/get-quota-limit',
963 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
964 ],
965
966 'mod_sql.c' => {
967 SQLAuthTypes => 'plaintext',
968 SQLBackend => 'sqlite3',
969 SQLConnectInfo => $db_file,
970 SQLLogFile => $log_file,
971 SQLMinID => '0',
972 },
973 },
974 };
975
976 my ($port, $config_user, $config_group) = config_write($config_file, $config);
977
978 # Open pipes, for use between the parent and child processes. Specifically,
979 # the child will indicate when it's done with its test by writing a message
980 # to the parent.
981 my ($rfh, $wfh);
982 unless (pipe($rfh, $wfh)) {
983 die("Can't open pipe: $!");
984 }
985
986 my $ex;
987
988 # Fork child
989 $self->handle_sigchld();
990 defined(my $pid = fork()) or die("Can't fork: $!");
991 if ($pid) {
992 eval {
993 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
994 $client->login($user, $passwd);
995
996 my ($resp_code, $resp_msg) = $client->site('CPFR', 'foo.txt');
997
998 my $expected;
999
1000 $expected = 350;
1001 $self->assert($expected == $resp_code,
1002 test_msg("Expected $expected, got $resp_code"));
1003
1004 $expected = "File or directory exists, ready for destination name";
1005 $self->assert($expected eq $resp_msg,
1006 test_msg("Expected '$expected', got '$resp_msg'"));
1007
1008 ($resp_code, $resp_msg) = $client->site('CPTO', 'bar.txt');
1009
1010 $expected = 250;
1011 $self->assert($expected == $resp_code,
1012 test_msg("Expected $expected, got $resp_code"));
1013
1014 $expected = 'COPY: notice: quota reached: used \S+ of \S+ upload file';
1015 $self->assert(qr/$expected/, $resp_msg,
1016 test_msg("Expected '$expected', got '$resp_msg'"));
1017
1018 $self->assert(-f $dst_file,
1019 test_msg("File $dst_file does not exist as expected"));
1020 };
1021
1022 if ($@) {
1023 $ex = $@;
1024 }
1025
1026 $wfh->print("done\n");
1027 $wfh->flush();
1028
1029 } else {
1030 eval { server_wait($config_file, $rfh) };
1031 if ($@) {
1032 warn($@);
1033 exit 1;
1034 }
1035
1036 exit 0;
1037 }
1038
1039 # Stop server
1040 server_stop($pid_file);
1041
1042 $self->assert_child_ok($pid);
1043
1044 if ($ex) {
1045 test_append_logfile($log_file, $ex);
1046 unlink($log_file);
1047
1048 die($ex);
1049 }
1050
1051 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1052
1053 my $expected;
1054
1055 $expected = 'user';
1056 $self->assert($expected eq $quota_type,
1057 test_msg("Expected '$expected', got '$quota_type'"));
1058
1059 $expected = '^(14.0|14)$';
1060 $self->assert(qr/$expected/, $bytes_in_used,
1061 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1062
1063 $expected = '^(0.0|0)$';
1064 $self->assert(qr/$expected/, $bytes_out_used,
1065 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1066
1067 $expected = '^(0.0|0)$';
1068 $self->assert(qr/$expected/, $bytes_xfer_used,
1069 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1070
1071 $expected = 1;
1072 $self->assert($expected == $files_in_used,
1073 test_msg("Expected $expected files-in-used, got $files_in_used"));
1074
1075 $expected = 0;
1076 $self->assert($expected == $files_out_used,
1077 test_msg("Expected $expected files-out-used, got $files_out_used"));
1078
1079 $expected = 0;
1080 $self->assert($expected == $files_xfer_used,
1081 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1082
1083 unlink($log_file);
1084 }
1085
1086 sub quotatab_site_copy_user_limit_files_in_exceeded_bug3641 {
1087 my $self = shift;
1088 my $tmpdir = $self->{tmpdir};
1089
1090 my $config_file = "$tmpdir/quotatab.conf";
1091 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1092 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1093
1094 my $log_file = test_get_logfile();
1095
1096 my $user = 'proftpd';
1097 my $passwd = 'test';
1098 my $group = 'ftpd';
1099 my $home_dir = File::Spec->rel2abs($tmpdir);
1100 my $uid = 500;
1101 my $gid = 500;
1102
1103 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1104
1105 # Build up sqlite3 command to create users, groups tables and populate them
1106 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1107
1108 if (open(my $fh, "> $db_script")) {
1109 print $fh <<EOS;
1110 CREATE TABLE users (
1111 userid TEXT,
1112 passwd TEXT,
1113 uid INTEGER,
1114 gid INTEGER,
1115 homedir TEXT,
1116 shell TEXT,
1117 lastdir TEXT
1118 );
1119 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1120
1121 CREATE TABLE groups (
1122 groupname TEXT,
1123 gid INTEGER,
1124 members TEXT
1125 );
1126 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1127
1128 CREATE TABLE quotalimits (
1129 name TEXT NOT NULL,
1130 quota_type TEXT NOT NULL,
1131 per_session TEXT NOT NULL,
1132 limit_type TEXT NOT NULL,
1133 bytes_in_avail REAL NOT NULL,
1134 bytes_out_avail REAL NOT NULL,
1135 bytes_xfer_avail REAL NOT NULL,
1136 files_in_avail INTEGER NOT NULL,
1137 files_out_avail INTEGER NOT NULL,
1138 files_xfer_avail INTEGER NOT NULL
1139 );
1140 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'hard', 32, 0, 0, 1, 0, 0);
1141
1142 CREATE TABLE quotatallies (
1143 name TEXT NOT NULL,
1144 quota_type TEXT NOT NULL,
1145 bytes_in_used REAL NOT NULL,
1146 bytes_out_used REAL NOT NULL,
1147 bytes_xfer_used REAL NOT NULL,
1148 files_in_used INTEGER NOT NULL,
1149 files_out_used INTEGER NOT NULL,
1150 files_xfer_used INTEGER NOT NULL
1151 );
1152 INSERT INTO quotatallies (name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used) VALUES ('$user', 'user', 5, 0, 0, 1, 0, 0);
1153 EOS
1154
1155 unless (close($fh)) {
1156 die("Can't write $db_script: $!");
1157 }
1158
1159 } else {
1160 die("Can't open $db_script: $!");
1161 }
1162
1163 my $cmd = "sqlite3 $db_file < $db_script";
1164
1165 if ($ENV{TEST_VERBOSE}) {
1166 print STDERR "Executing sqlite3: $cmd\n";
1167 }
1168
1169 my @output = `$cmd`;
1170 if (scalar(@output) &&
1171 $ENV{TEST_VERBOSE}) {
1172 print STDERR "Output: ", join('', @output), "\n";
1173 }
1174
1175 my $src_file = File::Spec->rel2abs("$tmpdir/foo.txt");
1176 if (open(my $fh, "> $src_file")) {
1177 print $fh "Hello, World!\n";
1178 unless (close($fh)) {
1179 die("Can't write $src_file: $!");
1180 }
1181
1182 } else {
1183 die("Can't open $src_file: $!");
1184 }
1185
1186 my $dst_file = File::Spec->rel2abs("$tmpdir/bar.txt");
1187
1188 my $config = {
1189 PidFile => $pid_file,
1190 ScoreboardFile => $scoreboard_file,
1191 SystemLog => $log_file,
1192
1193 DefaultChdir => '~',
1194
1195 IfModules => {
1196 'mod_delay.c' => {
1197 DelayEngine => 'off',
1198 },
1199
1200 'mod_quotatab_sql.c' => [
1201 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1202 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1203 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
1204 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1205
1206 'QuotaEngine on',
1207 "QuotaLog $log_file",
1208 'QuotaLimitTable sql:/get-quota-limit',
1209 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1210 ],
1211
1212 'mod_sql.c' => {
1213 SQLAuthTypes => 'plaintext',
1214 SQLBackend => 'sqlite3',
1215 SQLConnectInfo => $db_file,
1216 SQLLogFile => $log_file,
1217 SQLMinID => '0',
1218 },
1219 },
1220 };
1221
1222 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1223
1224 # Open pipes, for use between the parent and child processes. Specifically,
1225 # the child will indicate when it's done with its test by writing a message
1226 # to the parent.
1227 my ($rfh, $wfh);
1228 unless (pipe($rfh, $wfh)) {
1229 die("Can't open pipe: $!");
1230 }
1231
1232 my $ex;
1233
1234 # Fork child
1235 $self->handle_sigchld();
1236 defined(my $pid = fork()) or die("Can't fork: $!");
1237 if ($pid) {
1238 eval {
1239 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1240 $client->login($user, $passwd);
1241
1242 my ($resp_code, $resp_msg) = $client->site('CPFR', 'foo.txt');
1243
1244 my $expected;
1245
1246 $expected = 350;
1247 $self->assert($expected == $resp_code,
1248 test_msg("Expected $expected, got $resp_code"));
1249
1250 $expected = "File or directory exists, ready for destination name";
1251 $self->assert($expected eq $resp_msg,
1252 test_msg("Expected '$expected', got '$resp_msg'"));
1253
1254 eval { $client->site('CPTO', 'bar.txt') };
1255 unless ($@) {
1256 die("SITE CPTO succeeded unexpectedly");
1257 }
1258
1259 $resp_code = $client->response_code();
1260 $resp_msg = $client->response_msg();
1261
1262 $expected = 552;
1263 $self->assert($expected == $resp_code,
1264 test_msg("Expected $expected, got $resp_code"));
1265
1266 $expected = 'COPY denied: quota exceeded: used \S+ of \S+ upload file';
1267 $self->assert(qr/$expected/, $resp_msg,
1268 test_msg("Expected '$expected', got '$resp_msg'"));
1269
1270 $self->assert(!-f $dst_file,
1271 test_msg("File $dst_file exists unexpectedly"));
1272 };
1273
1274 if ($@) {
1275 $ex = $@;
1276 }
1277
1278 $wfh->print("done\n");
1279 $wfh->flush();
1280
1281 } else {
1282 eval { server_wait($config_file, $rfh) };
1283 if ($@) {
1284 warn($@);
1285 exit 1;
1286 }
1287
1288 exit 0;
1289 }
1290
1291 # Stop server
1292 server_stop($pid_file);
1293
1294 $self->assert_child_ok($pid);
1295
1296 if ($ex) {
1297 test_append_logfile($log_file, $ex);
1298 unlink($log_file);
1299
1300 die($ex);
1301 }
1302
1303 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1304
1305 my $expected;
1306
1307 $expected = 'user';
1308 $self->assert($expected eq $quota_type,
1309 test_msg("Expected '$expected', got '$quota_type'"));
1310
1311 $expected = '^(5.0|5)$';
1312 $self->assert(qr/$expected/, $bytes_in_used,
1313 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1314
1315 $expected = '^(0.0|0)$';
1316 $self->assert(qr/$expected/, $bytes_out_used,
1317 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1318
1319 $expected = '^(0.0|0)$';
1320 $self->assert(qr/$expected/, $bytes_xfer_used,
1321 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1322
1323 $expected = 1;
1324 $self->assert($expected == $files_in_used,
1325 test_msg("Expected $expected files-in-used, got $files_in_used"));
1326
1327 $expected = 0;
1328 $self->assert($expected == $files_out_used,
1329 test_msg("Expected $expected files-out-used, got $files_out_used"));
1330
1331 $expected = 0;
1332 $self->assert($expected == $files_xfer_used,
1333 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1334
1335 unlink($log_file);
1336 }
1337
1338 sub quotatab_site_copy_src_dir_bug3641 {
1339 my $self = shift;
1340 my $tmpdir = $self->{tmpdir};
1341
1342 my $config_file = "$tmpdir/quotatab.conf";
1343 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1344 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1345
1346 my $log_file = test_get_logfile();
1347
1348 my $user = 'proftpd';
1349 my $passwd = 'test';
1350 my $group = 'ftpd';
1351 my $home_dir = File::Spec->rel2abs($tmpdir);
1352 my $uid = 500;
1353 my $gid = 500;
1354
1355 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1356
1357 # Build up sqlite3 command to create users, groups tables and populate them
1358 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1359
1360 if (open(my $fh, "> $db_script")) {
1361 print $fh <<EOS;
1362 CREATE TABLE users (
1363 userid TEXT,
1364 passwd TEXT,
1365 uid INTEGER,
1366 gid INTEGER,
1367 homedir TEXT,
1368 shell TEXT,
1369 lastdir TEXT
1370 );
1371 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1372
1373 CREATE TABLE groups (
1374 groupname TEXT,
1375 gid INTEGER,
1376 members TEXT
1377 );
1378 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1379
1380 CREATE TABLE quotalimits (
1381 name TEXT NOT NULL,
1382 quota_type TEXT NOT NULL,
1383 per_session TEXT NOT NULL,
1384 limit_type TEXT NOT NULL,
1385 bytes_in_avail REAL NOT NULL,
1386 bytes_out_avail REAL NOT NULL,
1387 bytes_xfer_avail REAL NOT NULL,
1388 files_in_avail INTEGER NOT NULL,
1389 files_out_avail INTEGER NOT NULL,
1390 files_xfer_avail INTEGER NOT NULL
1391 );
1392 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 32, 0, 0, 2, 0, 0);
1393
1394 CREATE TABLE quotatallies (
1395 name TEXT NOT NULL,
1396 quota_type TEXT NOT NULL,
1397 bytes_in_used REAL NOT NULL,
1398 bytes_out_used REAL NOT NULL,
1399 bytes_xfer_used REAL NOT NULL,
1400 files_in_used INTEGER NOT NULL,
1401 files_out_used INTEGER NOT NULL,
1402 files_xfer_used INTEGER NOT NULL
1403 );
1404 EOS
1405
1406 unless (close($fh)) {
1407 die("Can't write $db_script: $!");
1408 }
1409
1410 } else {
1411 die("Can't open $db_script: $!");
1412 }
1413
1414 my $cmd = "sqlite3 $db_file < $db_script";
1415
1416 if ($ENV{TEST_VERBOSE}) {
1417 print STDERR "Executing sqlite3: $cmd\n";
1418 }
1419
1420 my @output = `$cmd`;
1421 if (scalar(@output) &&
1422 $ENV{TEST_VERBOSE}) {
1423 print STDERR "Output: ", join('', @output), "\n";
1424 }
1425
1426 my $src_dir = File::Spec->rel2abs("$tmpdir/src.d");
1427 mkpath($src_dir);
1428
1429 my $src_file = File::Spec->rel2abs("$tmpdir/src.d/foo.txt");
1430 if (open(my $fh, "> $src_file")) {
1431 print $fh "Hello, World!\n";
1432 unless (close($fh)) {
1433 die("Can't write $src_file: $!");
1434 }
1435
1436 } else {
1437 die("Can't open $src_file: $!");
1438 }
1439
1440 $src_file = File::Spec->rel2abs("$tmpdir/src.d/bar.txt");
1441 if (open(my $fh, "> $src_file")) {
1442 print $fh "Hello, World!\n";
1443 unless (close($fh)) {
1444 die("Can't write $src_file: $!");
1445 }
1446
1447 } else {
1448 die("Can't open $src_file: $!");
1449 }
1450
1451 my $dst_dir = File::Spec->rel2abs("$tmpdir/dst.d");
1452
1453 my $config = {
1454 PidFile => $pid_file,
1455 ScoreboardFile => $scoreboard_file,
1456 SystemLog => $log_file,
1457
1458 DefaultChdir => '~',
1459
1460 IfModules => {
1461 'mod_delay.c' => {
1462 DelayEngine => 'off',
1463 },
1464
1465 'mod_quotatab_sql.c' => [
1466 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1467 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1468 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
1469 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1470
1471 'QuotaEngine on',
1472 "QuotaLog $log_file",
1473 'QuotaLimitTable sql:/get-quota-limit',
1474 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1475 ],
1476
1477 'mod_sql.c' => {
1478 SQLAuthTypes => 'plaintext',
1479 SQLBackend => 'sqlite3',
1480 SQLConnectInfo => $db_file,
1481 SQLLogFile => $log_file,
1482 SQLMinID => '0',
1483 },
1484 },
1485 };
1486
1487 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1488
1489 # Open pipes, for use between the parent and child processes. Specifically,
1490 # the child will indicate when it's done with its test by writing a message
1491 # to the parent.
1492 my ($rfh, $wfh);
1493 unless (pipe($rfh, $wfh)) {
1494 die("Can't open pipe: $!");
1495 }
1496
1497 my $ex;
1498
1499 # Fork child
1500 $self->handle_sigchld();
1501 defined(my $pid = fork()) or die("Can't fork: $!");
1502 if ($pid) {
1503 eval {
1504 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1505 $client->login($user, $passwd);
1506
1507 my ($resp_code, $resp_msg) = $client->site('CPFR', 'src.d');
1508
1509 my $expected;
1510
1511 $expected = 350;
1512 $self->assert($expected == $resp_code,
1513 test_msg("Expected $expected, got $resp_code"));
1514
1515 $expected = "File or directory exists, ready for destination name";
1516 $self->assert($expected eq $resp_msg,
1517 test_msg("Expected '$expected', got '$resp_msg'"));
1518
1519 ($resp_code, $resp_msg) = $client->site('CPTO', 'dst.d');
1520
1521 $expected = 250;
1522 $self->assert($expected == $resp_code,
1523 test_msg("Expected $expected, got $resp_code"));
1524
1525 $expected = "Copy successful";
1526 $self->assert($expected eq $resp_msg,
1527 test_msg("Expected '$expected', got '$resp_msg'"));
1528
1529 };
1530
1531 if ($@) {
1532 $ex = $@;
1533 }
1534
1535 $wfh->print("done\n");
1536 $wfh->flush();
1537
1538 } else {
1539 eval { server_wait($config_file, $rfh) };
1540 if ($@) {
1541 warn($@);
1542 exit 1;
1543 }
1544
1545 exit 0;
1546 }
1547
1548 # Stop server
1549 server_stop($pid_file);
1550
1551 $self->assert_child_ok($pid);
1552
1553 if ($ex) {
1554 test_append_logfile($log_file, $ex);
1555 unlink($log_file);
1556
1557 die($ex);
1558 }
1559
1560 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1561
1562 my $expected;
1563
1564 $expected = 'user';
1565 $self->assert($expected eq $quota_type,
1566 test_msg("Expected '$expected', got '$quota_type'"));
1567
1568 $expected = '^(28.0|28)$';
1569 $self->assert(qr/$expected/, $bytes_in_used,
1570 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1571
1572 $expected = '^(0.0|0)$';
1573 $self->assert(qr/$expected/, $bytes_out_used,
1574 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1575
1576 $expected = '^(0.0|0)$';
1577 $self->assert(qr/$expected/, $bytes_xfer_used,
1578 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1579
1580 $expected = 2;
1581 $self->assert($expected == $files_in_used,
1582 test_msg("Expected $expected files-in-used, got $files_in_used"));
1583
1584 $expected = 0;
1585 $self->assert($expected == $files_out_used,
1586 test_msg("Expected $expected files-out-used, got $files_out_used"));
1587
1588 $expected = 0;
1589 $self->assert($expected == $files_xfer_used,
1590 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1591
1592 unlink($log_file);
1593 }
1594
1595 sub quotatab_site_copy_src_dir_bytes_in_exceeded_bug3641 {
1596 my $self = shift;
1597 my $tmpdir = $self->{tmpdir};
1598
1599 my $config_file = "$tmpdir/quotatab.conf";
1600 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1601 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1602
1603 my $log_file = test_get_logfile();
1604
1605 my $user = 'proftpd';
1606 my $passwd = 'test';
1607 my $group = 'ftpd';
1608 my $home_dir = File::Spec->rel2abs($tmpdir);
1609 my $uid = 500;
1610 my $gid = 500;
1611
1612 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1613
1614 # Build up sqlite3 command to create users, groups tables and populate them
1615 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1616
1617 if (open(my $fh, "> $db_script")) {
1618 print $fh <<EOS;
1619 CREATE TABLE users (
1620 userid TEXT,
1621 passwd TEXT,
1622 uid INTEGER,
1623 gid INTEGER,
1624 homedir TEXT,
1625 shell TEXT,
1626 lastdir TEXT
1627 );
1628 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1629
1630 CREATE TABLE groups (
1631 groupname TEXT,
1632 gid INTEGER,
1633 members TEXT
1634 );
1635 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1636
1637 CREATE TABLE quotalimits (
1638 name TEXT NOT NULL,
1639 quota_type TEXT NOT NULL,
1640 per_session TEXT NOT NULL,
1641 limit_type TEXT NOT NULL,
1642 bytes_in_avail REAL NOT NULL,
1643 bytes_out_avail REAL NOT NULL,
1644 bytes_xfer_avail REAL NOT NULL,
1645 files_in_avail INTEGER NOT NULL,
1646 files_out_avail INTEGER NOT NULL,
1647 files_xfer_avail INTEGER NOT NULL
1648 );
1649 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 24, 0, 0, 2, 0, 0);
1650
1651 CREATE TABLE quotatallies (
1652 name TEXT NOT NULL,
1653 quota_type TEXT NOT NULL,
1654 bytes_in_used REAL NOT NULL,
1655 bytes_out_used REAL NOT NULL,
1656 bytes_xfer_used REAL NOT NULL,
1657 files_in_used INTEGER NOT NULL,
1658 files_out_used INTEGER NOT NULL,
1659 files_xfer_used INTEGER NOT NULL
1660 );
1661 EOS
1662
1663 unless (close($fh)) {
1664 die("Can't write $db_script: $!");
1665 }
1666
1667 } else {
1668 die("Can't open $db_script: $!");
1669 }
1670
1671 my $cmd = "sqlite3 $db_file < $db_script";
1672
1673 if ($ENV{TEST_VERBOSE}) {
1674 print STDERR "Executing sqlite3: $cmd\n";
1675 }
1676
1677 my @output = `$cmd`;
1678 if (scalar(@output) &&
1679 $ENV{TEST_VERBOSE}) {
1680 print STDERR "Output: ", join('', @output), "\n";
1681 }
1682
1683 my $src_dir = File::Spec->rel2abs("$tmpdir/src.d");
1684 mkpath($src_dir);
1685
1686 my $src_file = File::Spec->rel2abs("$tmpdir/src.d/foo.txt");
1687 if (open(my $fh, "> $src_file")) {
1688 print $fh "Hello, World!\n";
1689 unless (close($fh)) {
1690 die("Can't write $src_file: $!");
1691 }
1692
1693 } else {
1694 die("Can't open $src_file: $!");
1695 }
1696
1697 $src_file = File::Spec->rel2abs("$tmpdir/src.d/bar.txt");
1698 if (open(my $fh, "> $src_file")) {
1699 print $fh "Hello, World!\n";
1700 unless (close($fh)) {
1701 die("Can't write $src_file: $!");
1702 }
1703
1704 } else {
1705 die("Can't open $src_file: $!");
1706 }
1707
1708 my $dst_dir = File::Spec->rel2abs("$tmpdir/dst.d");
1709
1710 my $config = {
1711 PidFile => $pid_file,
1712 ScoreboardFile => $scoreboard_file,
1713 SystemLog => $log_file,
1714 TraceLog => $log_file,
1715 Trace => 'response:20 copy:20 quotatab:20',
1716
1717 DefaultChdir => '~',
1718
1719 IfModules => {
1720 'mod_delay.c' => {
1721 DelayEngine => 'off',
1722 },
1723
1724 'mod_quotatab_sql.c' => [
1725 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1726 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1727 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
1728 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1729
1730 'QuotaEngine on',
1731 "QuotaLog $log_file",
1732 'QuotaLimitTable sql:/get-quota-limit',
1733 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1734 ],
1735
1736 'mod_sql.c' => {
1737 SQLAuthTypes => 'plaintext',
1738 SQLBackend => 'sqlite3',
1739 SQLConnectInfo => $db_file,
1740 SQLLogFile => $log_file,
1741 SQLMinID => '0',
1742 },
1743 },
1744 };
1745
1746 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1747
1748 # Open pipes, for use between the parent and child processes. Specifically,
1749 # the child will indicate when it's done with its test by writing a message
1750 # to the parent.
1751 my ($rfh, $wfh);
1752 unless (pipe($rfh, $wfh)) {
1753 die("Can't open pipe: $!");
1754 }
1755
1756 my $ex;
1757
1758 # Fork child
1759 $self->handle_sigchld();
1760 defined(my $pid = fork()) or die("Can't fork: $!");
1761 if ($pid) {
1762 eval {
1763 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1764 $client->login($user, $passwd);
1765
1766 my ($resp_code, $resp_msg) = $client->site('CPFR', 'src.d');
1767
1768 my $expected;
1769
1770 $expected = 350;
1771 $self->assert($expected == $resp_code,
1772 test_msg("Expected $expected, got $resp_code"));
1773
1774 $expected = "File or directory exists, ready for destination name";
1775 $self->assert($expected eq $resp_msg,
1776 test_msg("Expected '$expected', got '$resp_msg'"));
1777
1778 ($resp_code, $resp_msg) = $client->site('CPTO', 'dst.d');
1779
1780 $expected = 250;
1781 $self->assert($expected == $resp_code,
1782 test_msg("Expected $expected, got $resp_code"));
1783
1784 $expected = "COPY: notice: quota reached: used 28.00 of 24.00 upload bytes";
1785 $self->assert($expected eq $resp_msg,
1786 test_msg("Expected '$expected', got '$resp_msg'"));
1787
1788 };
1789
1790 if ($@) {
1791 $ex = $@;
1792 }
1793
1794 $wfh->print("done\n");
1795 $wfh->flush();
1796
1797 } else {
1798 eval { server_wait($config_file, $rfh) };
1799 if ($@) {
1800 warn($@);
1801 exit 1;
1802 }
1803
1804 exit 0;
1805 }
1806
1807 # Stop server
1808 server_stop($pid_file);
1809
1810 $self->assert_child_ok($pid);
1811
1812 if ($ex) {
1813 test_append_logfile($log_file, $ex);
1814 unlink($log_file);
1815
1816 die($ex);
1817 }
1818
1819 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1820
1821 my $expected;
1822
1823 $expected = 'user';
1824 $self->assert($expected eq $quota_type,
1825 test_msg("Expected '$expected', got '$quota_type'"));
1826
1827 $expected = '^(28.0|28)$';
1828 $self->assert(qr/$expected/, $bytes_in_used,
1829 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1830
1831 $expected = '^(0.0|0)$';
1832 $self->assert(qr/$expected/, $bytes_out_used,
1833 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1834
1835 $expected = '^(0.0|0)$';
1836 $self->assert(qr/$expected/, $bytes_xfer_used,
1837 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1838
1839 $expected = 2;
1840 $self->assert($expected == $files_in_used,
1841 test_msg("Expected $expected files-in-used, got $files_in_used"));
1842
1843 $expected = 0;
1844 $self->assert($expected == $files_out_used,
1845 test_msg("Expected $expected files-out-used, got $files_out_used"));
1846
1847 $expected = 0;
1848 $self->assert($expected == $files_xfer_used,
1849 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1850
1851 unlink($log_file);
1852 }
1853
1854 sub quotatab_site_copy_src_dir_bytes_in_exceeded_chrooted_bug3641 {
1855 my $self = shift;
1856 my $tmpdir = $self->{tmpdir};
1857
1858 my $config_file = "$tmpdir/quotatab.conf";
1859 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1860 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1861
1862 my $log_file = test_get_logfile();
1863
1864 my $user = 'proftpd';
1865 my $passwd = 'test';
1866 my $group = 'ftpd';
1867 my $home_dir = File::Spec->rel2abs($tmpdir);
1868 my $uid = 500;
1869 my $gid = 500;
1870
1871 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1872
1873 # Build up sqlite3 command to create users, groups tables and populate them
1874 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1875
1876 if (open(my $fh, "> $db_script")) {
1877 print $fh <<EOS;
1878 CREATE TABLE users (
1879 userid TEXT,
1880 passwd TEXT,
1881 uid INTEGER,
1882 gid INTEGER,
1883 homedir TEXT,
1884 shell TEXT,
1885 lastdir TEXT
1886 );
1887 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1888
1889 CREATE TABLE groups (
1890 groupname TEXT,
1891 gid INTEGER,
1892 members TEXT
1893 );
1894 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1895
1896 CREATE TABLE quotalimits (
1897 name TEXT NOT NULL,
1898 quota_type TEXT NOT NULL,
1899 per_session TEXT NOT NULL,
1900 limit_type TEXT NOT NULL,
1901 bytes_in_avail REAL NOT NULL,
1902 bytes_out_avail REAL NOT NULL,
1903 bytes_xfer_avail REAL NOT NULL,
1904 files_in_avail INTEGER NOT NULL,
1905 files_out_avail INTEGER NOT NULL,
1906 files_xfer_avail INTEGER NOT NULL
1907 );
1908 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 24, 0, 0, 2, 0, 0);
1909
1910 CREATE TABLE quotatallies (
1911 name TEXT NOT NULL,
1912 quota_type TEXT NOT NULL,
1913 bytes_in_used REAL NOT NULL,
1914 bytes_out_used REAL NOT NULL,
1915 bytes_xfer_used REAL NOT NULL,
1916 files_in_used INTEGER NOT NULL,
1917 files_out_used INTEGER NOT NULL,
1918 files_xfer_used INTEGER NOT NULL
1919 );
1920 EOS
1921
1922 unless (close($fh)) {
1923 die("Can't write $db_script: $!");
1924 }
1925
1926 } else {
1927 die("Can't open $db_script: $!");
1928 }
1929
1930 my $cmd = "sqlite3 $db_file < $db_script";
1931
1932 if ($ENV{TEST_VERBOSE}) {
1933 print STDERR "Executing sqlite3: $cmd\n";
1934 }
1935
1936 my @output = `$cmd`;
1937 if (scalar(@output) &&
1938 $ENV{TEST_VERBOSE}) {
1939 print STDERR "Output: ", join('', @output), "\n";
1940 }
1941
1942 my $src_dir = File::Spec->rel2abs("$tmpdir/src.d");
1943 mkpath($src_dir);
1944
1945 # Make sure that, if we're running as root, that the home directory has
1946 # permissions/privs set for the account we create
1947 if ($< == 0) {
1948 unless (chmod(0755, $home_dir, $src_dir)) {
1949 die("Can't set perms on $home_dir to 0755: $!");
1950 }
1951
1952 unless (chown($uid, $gid, $home_dir, $src_dir)) {
1953 die("Can't set owner of $home_dir to $uid/$gid: $!");
1954 }
1955
1956 unless (chmod(0666, $db_file)) {
1957 die("Can't set perms on $db_file to 0666: $!");
1958 }
1959 }
1960
1961 my $src_file = File::Spec->rel2abs("$tmpdir/src.d/foo.txt");
1962 if (open(my $fh, "> $src_file")) {
1963 print $fh "Hello, World!\n";
1964 unless (close($fh)) {
1965 die("Can't write $src_file: $!");
1966 }
1967
1968 } else {
1969 die("Can't open $src_file: $!");
1970 }
1971
1972 $src_file = File::Spec->rel2abs("$tmpdir/src.d/bar.txt");
1973 if (open(my $fh, "> $src_file")) {
1974 print $fh "Hello, World!\n";
1975 unless (close($fh)) {
1976 die("Can't write $src_file: $!");
1977 }
1978
1979 } else {
1980 die("Can't open $src_file: $!");
1981 }
1982
1983 my $dst_dir = File::Spec->rel2abs("$tmpdir/dst.d");
1984
1985 my $config = {
1986 PidFile => $pid_file,
1987 ScoreboardFile => $scoreboard_file,
1988 SystemLog => $log_file,
1989 TraceLog => $log_file,
1990 Trace => 'response:20 copy:20 quotatab:20',
1991
1992 DefaultRoot => '~',
1993
1994 IfModules => {
1995 'mod_delay.c' => {
1996 DelayEngine => 'off',
1997 },
1998
1999 'mod_quotatab_sql.c' => [
2000 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2001 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2002 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
2003 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2004
2005 'QuotaEngine on',
2006 "QuotaLog $log_file",
2007 'QuotaLimitTable sql:/get-quota-limit',
2008 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2009 ],
2010
2011 'mod_sql.c' => {
2012 SQLAuthTypes => 'plaintext',
2013 SQLBackend => 'sqlite3',
2014 SQLConnectInfo => $db_file,
2015 SQLLogFile => $log_file,
2016 SQLMinID => '0',
2017 },
2018 },
2019 };
2020
2021 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2022
2023 # Open pipes, for use between the parent and child processes. Specifically,
2024 # the child will indicate when it's done with its test by writing a message
2025 # to the parent.
2026 my ($rfh, $wfh);
2027 unless (pipe($rfh, $wfh)) {
2028 die("Can't open pipe: $!");
2029 }
2030
2031 my $ex;
2032
2033 # Fork child
2034 $self->handle_sigchld();
2035 defined(my $pid = fork()) or die("Can't fork: $!");
2036 if ($pid) {
2037 eval {
2038 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2039 $client->login($user, $passwd);
2040
2041 my ($resp_code, $resp_msg) = $client->site('CPFR', 'src.d');
2042
2043 my $expected;
2044
2045 $expected = 350;
2046 $self->assert($expected == $resp_code,
2047 test_msg("Expected $expected, got $resp_code"));
2048
2049 $expected = "File or directory exists, ready for destination name";
2050 $self->assert($expected eq $resp_msg,
2051 test_msg("Expected '$expected', got '$resp_msg'"));
2052
2053 ($resp_code, $resp_msg) = $client->site('CPTO', 'dst.d');
2054
2055 $expected = 250;
2056 $self->assert($expected == $resp_code,
2057 test_msg("Expected $expected, got $resp_code"));
2058
2059 $expected = "COPY: notice: quota reached: used 28.00 of 24.00 upload bytes";
2060 $self->assert($expected eq $resp_msg,
2061 test_msg("Expected '$expected', got '$resp_msg'"));
2062
2063 };
2064
2065 if ($@) {
2066 $ex = $@;
2067 }
2068
2069 $wfh->print("done\n");
2070 $wfh->flush();
2071
2072 } else {
2073 eval { server_wait($config_file, $rfh) };
2074 if ($@) {
2075 warn($@);
2076 exit 1;
2077 }
2078
2079 exit 0;
2080 }
2081
2082 # Stop server
2083 server_stop($pid_file);
2084
2085 $self->assert_child_ok($pid);
2086
2087 if ($ex) {
2088 test_append_logfile($log_file, $ex);
2089 unlink($log_file);
2090
2091 die($ex);
2092 }
2093
2094 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
2095
2096 my $expected;
2097
2098 $expected = 'user';
2099 $self->assert($expected eq $quota_type,
2100 test_msg("Expected '$expected', got '$quota_type'"));
2101
2102 $expected = '^(28.0|28)$';
2103 $self->assert(qr/$expected/, $bytes_in_used,
2104 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
2105
2106 $expected = '^(0.0|0)$';
2107 $self->assert(qr/$expected/, $bytes_out_used,
2108 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
2109
2110 $expected = '^(0.0|0)$';
2111 $self->assert(qr/$expected/, $bytes_xfer_used,
2112 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
2113
2114 $expected = 2;
2115 $self->assert($expected == $files_in_used,
2116 test_msg("Expected $expected files-in-used, got $files_in_used"));
2117
2118 $expected = 0;
2119 $self->assert($expected == $files_out_used,
2120 test_msg("Expected $expected files-out-used, got $files_out_used"));
2121
2122 $expected = 0;
2123 $self->assert($expected == $files_xfer_used,
2124 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
2125
2126 unlink($log_file);
2127 }
2128
2129 sub quotatab_site_copy_src_dir_files_in_exceeded_bug3641 {
2130 my $self = shift;
2131 my $tmpdir = $self->{tmpdir};
2132
2133 my $config_file = "$tmpdir/quotatab.conf";
2134 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2135 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2136
2137 my $log_file = test_get_logfile();
2138
2139 my $user = 'proftpd';
2140 my $passwd = 'test';
2141 my $group = 'ftpd';
2142 my $home_dir = File::Spec->rel2abs($tmpdir);
2143 my $uid = 500;
2144 my $gid = 500;
2145
2146 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2147
2148 # Build up sqlite3 command to create users, groups tables and populate them
2149 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2150
2151 if (open(my $fh, "> $db_script")) {
2152 print $fh <<EOS;
2153 CREATE TABLE users (
2154 userid TEXT,
2155 passwd TEXT,
2156 uid INTEGER,
2157 gid INTEGER,
2158 homedir TEXT,
2159 shell TEXT,
2160 lastdir TEXT
2161 );
2162 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2163
2164 CREATE TABLE groups (
2165 groupname TEXT,
2166 gid INTEGER,
2167 members TEXT
2168 );
2169 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2170
2171 CREATE TABLE quotalimits (
2172 name TEXT NOT NULL,
2173 quota_type TEXT NOT NULL,
2174 per_session TEXT NOT NULL,
2175 limit_type TEXT NOT NULL,
2176 bytes_in_avail REAL NOT NULL,
2177 bytes_out_avail REAL NOT NULL,
2178 bytes_xfer_avail REAL NOT NULL,
2179 files_in_avail INTEGER NOT NULL,
2180 files_out_avail INTEGER NOT NULL,
2181 files_xfer_avail INTEGER NOT NULL
2182 );
2183 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 64, 0, 0, 1, 0, 0);
2184
2185 CREATE TABLE quotatallies (
2186 name TEXT NOT NULL,
2187 quota_type TEXT NOT NULL,
2188 bytes_in_used REAL NOT NULL,
2189 bytes_out_used REAL NOT NULL,
2190 bytes_xfer_used REAL NOT NULL,
2191 files_in_used INTEGER NOT NULL,
2192 files_out_used INTEGER NOT NULL,
2193 files_xfer_used INTEGER NOT NULL
2194 );
2195 EOS
2196
2197 unless (close($fh)) {
2198 die("Can't write $db_script: $!");
2199 }
2200
2201 } else {
2202 die("Can't open $db_script: $!");
2203 }
2204
2205 my $cmd = "sqlite3 $db_file < $db_script";
2206
2207 if ($ENV{TEST_VERBOSE}) {
2208 print STDERR "Executing sqlite3: $cmd\n";
2209 }
2210
2211 my @output = `$cmd`;
2212 if (scalar(@output) &&
2213 $ENV{TEST_VERBOSE}) {
2214 print STDERR "Output: ", join('', @output), "\n";
2215 }
2216
2217 my $src_dir = File::Spec->rel2abs("$tmpdir/src.d");
2218 mkpath($src_dir);
2219
2220 my $src_file = File::Spec->rel2abs("$tmpdir/src.d/foo.txt");
2221 if (open(my $fh, "> $src_file")) {
2222 print $fh "Hello, World!\n";
2223 unless (close($fh)) {
2224 die("Can't write $src_file: $!");
2225 }
2226
2227 } else {
2228 die("Can't open $src_file: $!");
2229 }
2230
2231 $src_file = File::Spec->rel2abs("$tmpdir/src.d/bar.txt");
2232 if (open(my $fh, "> $src_file")) {
2233 print $fh "Hello, World!\n";
2234 unless (close($fh)) {
2235 die("Can't write $src_file: $!");
2236 }
2237
2238 } else {
2239 die("Can't open $src_file: $!");
2240 }
2241
2242 my $dst_dir = File::Spec->rel2abs("$tmpdir/dst.d");
2243
2244 my $config = {
2245 PidFile => $pid_file,
2246 ScoreboardFile => $scoreboard_file,
2247 SystemLog => $log_file,
2248 TraceLog => $log_file,
2249 Trace => 'response:20 copy:20 quotatab:20',
2250
2251 DefaultChdir => '~',
2252
2253 IfModules => {
2254 'mod_delay.c' => {
2255 DelayEngine => 'off',
2256 },
2257
2258 'mod_quotatab_sql.c' => [
2259 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2260 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2261 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
2262 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2263
2264 'QuotaEngine on',
2265 "QuotaLog $log_file",
2266 'QuotaLimitTable sql:/get-quota-limit',
2267 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2268 ],
2269
2270 'mod_sql.c' => {
2271 SQLAuthTypes => 'plaintext',
2272 SQLBackend => 'sqlite3',
2273 SQLConnectInfo => $db_file,
2274 SQLLogFile => $log_file,
2275 SQLMinID => '0',
2276 },
2277 },
2278 };
2279
2280 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2281
2282 # Open pipes, for use between the parent and child processes. Specifically,
2283 # the child will indicate when it's done with its test by writing a message
2284 # to the parent.
2285 my ($rfh, $wfh);
2286 unless (pipe($rfh, $wfh)) {
2287 die("Can't open pipe: $!");
2288 }
2289
2290 my $ex;
2291
2292 # Fork child
2293 $self->handle_sigchld();
2294 defined(my $pid = fork()) or die("Can't fork: $!");
2295 if ($pid) {
2296 eval {
2297 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2298 $client->login($user, $passwd);
2299
2300 my ($resp_code, $resp_msg) = $client->site('CPFR', 'src.d');
2301
2302 my $expected;
2303
2304 $expected = 350;
2305 $self->assert($expected == $resp_code,
2306 test_msg("Expected $expected, got $resp_code"));
2307
2308 $expected = "File or directory exists, ready for destination name";
2309 $self->assert($expected eq $resp_msg,
2310 test_msg("Expected '$expected', got '$resp_msg'"));
2311
2312 eval { $client->site('CPTO', 'dst.d') };
2313 unless ($@) {
2314 die("SITE CPTO succeeded unexpectedly");
2315 }
2316
2317 $resp_code = $client->response_code();
2318 $resp_msg = $client->response_msg();
2319
2320 $expected = 550;
2321 $self->assert($expected == $resp_code,
2322 test_msg("Expected $expected, got $resp_code"));
2323
2324 $expected = "CPTO: Disk quota exceeded";
2325 $self->assert($expected eq $resp_msg,
2326 test_msg("Expected '$expected', got '$resp_msg'"));
2327
2328 };
2329
2330 if ($@) {
2331 $ex = $@;
2332 }
2333
2334 $wfh->print("done\n");
2335 $wfh->flush();
2336
2337 } else {
2338 eval { server_wait($config_file, $rfh) };
2339 if ($@) {
2340 warn($@);
2341 exit 1;
2342 }
2343
2344 exit 0;
2345 }
2346
2347 # Stop server
2348 server_stop($pid_file);
2349
2350 $self->assert_child_ok($pid);
2351
2352 if ($ex) {
2353 test_append_logfile($log_file, $ex);
2354 unlink($log_file);
2355
2356 die($ex);
2357 }
2358
2359 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
2360
2361 my $expected;
2362
2363 $expected = 'user';
2364 $self->assert($expected eq $quota_type,
2365 test_msg("Expected '$expected', got '$quota_type'"));
2366
2367 $expected = '^(14.0|14)$';
2368 $self->assert(qr/$expected/, $bytes_in_used,
2369 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
2370
2371 $expected = '^(0.0|0)$';
2372 $self->assert(qr/$expected/, $bytes_out_used,
2373 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
2374
2375 $expected = '^(0.0|0)$';
2376 $self->assert(qr/$expected/, $bytes_xfer_used,
2377 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
2378
2379 $expected = 1;
2380 $self->assert($expected == $files_in_used,
2381 test_msg("Expected $expected files-in-used, got $files_in_used"));
2382
2383 $expected = 0;
2384 $self->assert($expected == $files_out_used,
2385 test_msg("Expected $expected files-out-used, got $files_out_used"));
2386
2387 $expected = 0;
2388 $self->assert($expected == $files_xfer_used,
2389 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
2390
2391 unlink($log_file);
2392 }
2393
2394 sub quotatab_site_copy_src_dir_files_in_exceeded_chrooted_bug3641 {
2395 my $self = shift;
2396 my $tmpdir = $self->{tmpdir};
2397
2398 my $config_file = "$tmpdir/quotatab.conf";
2399 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2400 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2401
2402 my $log_file = test_get_logfile();
2403
2404 my $user = 'proftpd';
2405 my $passwd = 'test';
2406 my $group = 'ftpd';
2407 my $home_dir = File::Spec->rel2abs($tmpdir);
2408 my $uid = 500;
2409 my $gid = 500;
2410
2411 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2412
2413 # Build up sqlite3 command to create users, groups tables and populate them
2414 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2415
2416 if (open(my $fh, "> $db_script")) {
2417 print $fh <<EOS;
2418 CREATE TABLE users (
2419 userid TEXT,
2420 passwd TEXT,
2421 uid INTEGER,
2422 gid INTEGER,
2423 homedir TEXT,
2424 shell TEXT,
2425 lastdir TEXT
2426 );
2427 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2428
2429 CREATE TABLE groups (
2430 groupname TEXT,
2431 gid INTEGER,
2432 members TEXT
2433 );
2434 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2435
2436 CREATE TABLE quotalimits (
2437 name TEXT NOT NULL,
2438 quota_type TEXT NOT NULL,
2439 per_session TEXT NOT NULL,
2440 limit_type TEXT NOT NULL,
2441 bytes_in_avail REAL NOT NULL,
2442 bytes_out_avail REAL NOT NULL,
2443 bytes_xfer_avail REAL NOT NULL,
2444 files_in_avail INTEGER NOT NULL,
2445 files_out_avail INTEGER NOT NULL,
2446 files_xfer_avail INTEGER NOT NULL
2447 );
2448 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 64, 0, 0, 1, 0, 0);
2449
2450 CREATE TABLE quotatallies (
2451 name TEXT NOT NULL,
2452 quota_type TEXT NOT NULL,
2453 bytes_in_used REAL NOT NULL,
2454 bytes_out_used REAL NOT NULL,
2455 bytes_xfer_used REAL NOT NULL,
2456 files_in_used INTEGER NOT NULL,
2457 files_out_used INTEGER NOT NULL,
2458 files_xfer_used INTEGER NOT NULL
2459 );
2460 EOS
2461
2462 unless (close($fh)) {
2463 die("Can't write $db_script: $!");
2464 }
2465
2466 } else {
2467 die("Can't open $db_script: $!");
2468 }
2469
2470 my $cmd = "sqlite3 $db_file < $db_script";
2471
2472 if ($ENV{TEST_VERBOSE}) {
2473 print STDERR "Executing sqlite3: $cmd\n";
2474 }
2475
2476 my @output = `$cmd`;
2477 if (scalar(@output) &&
2478 $ENV{TEST_VERBOSE}) {
2479 print STDERR "Output: ", join('', @output), "\n";
2480 }
2481
2482 my $src_dir = File::Spec->rel2abs("$tmpdir/src.d");
2483 mkpath($src_dir);
2484
2485 # Make sure that, if we're running as root, that the home directory has
2486 # permissions/privs set for the account we create
2487 if ($< == 0) {
2488 unless (chmod(0755, $home_dir, $src_dir)) {
2489 die("Can't set perms on $home_dir to 0755: $!");
2490 }
2491
2492 unless (chown($uid, $gid, $home_dir, $src_dir)) {
2493 die("Can't set owner of $home_dir to $uid/$gid: $!");
2494 }
2495
2496 unless (chmod(0666, $db_file)) {
2497 die("Can't set perms on $db_file to 0666: $!");
2498 }
2499 }
2500
2501 my $src_file = File::Spec->rel2abs("$tmpdir/src.d/foo.txt");
2502 if (open(my $fh, "> $src_file")) {
2503 print $fh "Hello, World!\n";
2504 unless (close($fh)) {
2505 die("Can't write $src_file: $!");
2506 }
2507
2508 } else {
2509 die("Can't open $src_file: $!");
2510 }
2511
2512 $src_file = File::Spec->rel2abs("$tmpdir/src.d/bar.txt");
2513 if (open(my $fh, "> $src_file")) {
2514 print $fh "Hello, World!\n";
2515 unless (close($fh)) {
2516 die("Can't write $src_file: $!");
2517 }
2518
2519 } else {
2520 die("Can't open $src_file: $!");
2521 }
2522
2523 my $dst_dir = File::Spec->rel2abs("$tmpdir/dst.d");
2524
2525 my $config = {
2526 PidFile => $pid_file,
2527 ScoreboardFile => $scoreboard_file,
2528 SystemLog => $log_file,
2529 TraceLog => $log_file,
2530 Trace => 'response:20 copy:20 quotatab:20',
2531
2532 DefaultRoot => '~',
2533
2534 IfModules => {
2535 'mod_delay.c' => {
2536 DelayEngine => 'off',
2537 },
2538
2539 'mod_quotatab_sql.c' => [
2540 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2541 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2542 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
2543 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2544
2545 'QuotaEngine on',
2546 "QuotaLog $log_file",
2547 'QuotaLimitTable sql:/get-quota-limit',
2548 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2549 ],
2550
2551 'mod_sql.c' => {
2552 SQLAuthTypes => 'plaintext',
2553 SQLBackend => 'sqlite3',
2554 SQLConnectInfo => $db_file,
2555 SQLLogFile => $log_file,
2556 SQLMinID => '0',
2557 },
2558 },
2559 };
2560
2561 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2562
2563 # Open pipes, for use between the parent and child processes. Specifically,
2564 # the child will indicate when it's done with its test by writing a message
2565 # to the parent.
2566 my ($rfh, $wfh);
2567 unless (pipe($rfh, $wfh)) {
2568 die("Can't open pipe: $!");
2569 }
2570
2571 my $ex;
2572
2573 # Fork child
2574 $self->handle_sigchld();
2575 defined(my $pid = fork()) or die("Can't fork: $!");
2576 if ($pid) {
2577 eval {
2578 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2579 $client->login($user, $passwd);
2580
2581 my ($resp_code, $resp_msg) = $client->site('CPFR', 'src.d');
2582
2583 my $expected;
2584
2585 $expected = 350;
2586 $self->assert($expected == $resp_code,
2587 test_msg("Expected $expected, got $resp_code"));
2588
2589 $expected = "File or directory exists, ready for destination name";
2590 $self->assert($expected eq $resp_msg,
2591 test_msg("Expected '$expected', got '$resp_msg'"));
2592
2593 eval { $client->site('CPTO', 'dst.d') };
2594 unless ($@) {
2595 die("SITE CPTO succeeded unexpectedly");
2596 }
2597
2598 $resp_code = $client->response_code();
2599 $resp_msg = $client->response_msg();
2600
2601 $expected = 550;
2602 $self->assert($expected == $resp_code,
2603 test_msg("Expected $expected, got $resp_code"));
2604
2605 $expected = "CPTO: Disk quota exceeded";
2606 $self->assert($expected eq $resp_msg,
2607 test_msg("Expected '$expected', got '$resp_msg'"));
2608
2609 };
2610
2611 if ($@) {
2612 $ex = $@;
2613 }
2614
2615 $wfh->print("done\n");
2616 $wfh->flush();
2617
2618 } else {
2619 eval { server_wait($config_file, $rfh) };
2620 if ($@) {
2621 warn($@);
2622 exit 1;
2623 }
2624
2625 exit 0;
2626 }
2627
2628 # Stop server
2629 server_stop($pid_file);
2630
2631 $self->assert_child_ok($pid);
2632
2633 if ($ex) {
2634 test_append_logfile($log_file, $ex);
2635 unlink($log_file);
2636
2637 die($ex);
2638 }
2639
2640 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
2641
2642 my $expected;
2643
2644 $expected = 'user';
2645 $self->assert($expected eq $quota_type,
2646 test_msg("Expected '$expected', got '$quota_type'"));
2647
2648 $expected = '^(14.0|14)$';
2649 $self->assert(qr/$expected/, $bytes_in_used,
2650 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
2651
2652 $expected = '^(0.0|0)$';
2653 $self->assert(qr/$expected/, $bytes_out_used,
2654 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
2655
2656 $expected = '^(0.0|0)$';
2657 $self->assert(qr/$expected/, $bytes_xfer_used,
2658 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
2659
2660 $expected = 1;
2661 $self->assert($expected == $files_in_used,
2662 test_msg("Expected $expected files-in-used, got $files_in_used"));
2663
2664 $expected = 0;
2665 $self->assert($expected == $files_out_used,
2666 test_msg("Expected $expected files-out-used, got $files_out_used"));
2667
2668 $expected = 0;
2669 $self->assert($expected == $files_xfer_used,
2670 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
2671
2672 unlink($log_file);
2673 }
2674
2675 sub quotatab_site_copy_src_dir_directory_tally_bug3641 {
2676 my $self = shift;
2677 my $tmpdir = $self->{tmpdir};
2678
2679 my $config_file = "$tmpdir/quotatab.conf";
2680 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2681 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2682
2683 my $log_file = test_get_logfile();
2684
2685 my $user = 'proftpd';
2686 my $passwd = 'test';
2687 my $group = 'ftpd';
2688 my $home_dir = File::Spec->rel2abs($tmpdir);
2689 my $uid = 500;
2690 my $gid = 500;
2691
2692 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2693
2694 # Build up sqlite3 command to create users, groups tables and populate them
2695 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2696
2697 if (open(my $fh, "> $db_script")) {
2698 print $fh <<EOS;
2699 CREATE TABLE users (
2700 userid TEXT,
2701 passwd TEXT,
2702 uid INTEGER,
2703 gid INTEGER,
2704 homedir TEXT,
2705 shell TEXT,
2706 lastdir TEXT
2707 );
2708 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2709
2710 CREATE TABLE groups (
2711 groupname TEXT,
2712 gid INTEGER,
2713 members TEXT
2714 );
2715 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2716
2717 CREATE TABLE quotalimits (
2718 name TEXT NOT NULL,
2719 quota_type TEXT NOT NULL,
2720 per_session TEXT NOT NULL,
2721 limit_type TEXT NOT NULL,
2722 bytes_in_avail REAL NOT NULL,
2723 bytes_out_avail REAL NOT NULL,
2724 bytes_xfer_avail REAL NOT NULL,
2725 files_in_avail INTEGER NOT NULL,
2726 files_out_avail INTEGER NOT NULL,
2727 files_xfer_avail INTEGER NOT NULL
2728 );
2729 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 16384, 0, 0, 3, 0, 0);
2730
2731 CREATE TABLE quotatallies (
2732 name TEXT NOT NULL,
2733 quota_type TEXT NOT NULL,
2734 bytes_in_used REAL NOT NULL,
2735 bytes_out_used REAL NOT NULL,
2736 bytes_xfer_used REAL NOT NULL,
2737 files_in_used INTEGER NOT NULL,
2738 files_out_used INTEGER NOT NULL,
2739 files_xfer_used INTEGER NOT NULL
2740 );
2741 EOS
2742
2743 unless (close($fh)) {
2744 die("Can't write $db_script: $!");
2745 }
2746
2747 } else {
2748 die("Can't open $db_script: $!");
2749 }
2750
2751 my $cmd = "sqlite3 $db_file < $db_script";
2752
2753 if ($ENV{TEST_VERBOSE}) {
2754 print STDERR "Executing sqlite3: $cmd\n";
2755 }
2756
2757 my @output = `$cmd`;
2758 if (scalar(@output) &&
2759 $ENV{TEST_VERBOSE}) {
2760 print STDERR "Output: ", join('', @output), "\n";
2761 }
2762
2763 my $src_dir = File::Spec->rel2abs("$tmpdir/src.d");
2764 mkpath($src_dir);
2765
2766 my $src_file = File::Spec->rel2abs("$tmpdir/src.d/foo.txt");
2767 if (open(my $fh, "> $src_file")) {
2768 print $fh "Hello, World!\n";
2769 unless (close($fh)) {
2770 die("Can't write $src_file: $!");
2771 }
2772
2773 } else {
2774 die("Can't open $src_file: $!");
2775 }
2776
2777 $src_file = File::Spec->rel2abs("$tmpdir/src.d/bar.txt");
2778 if (open(my $fh, "> $src_file")) {
2779 print $fh "Hello, World!\n";
2780 unless (close($fh)) {
2781 die("Can't write $src_file: $!");
2782 }
2783
2784 } else {
2785 die("Can't open $src_file: $!");
2786 }
2787
2788 my $dst_dir = File::Spec->rel2abs("$tmpdir/dst.d");
2789
2790 my $config = {
2791 PidFile => $pid_file,
2792 ScoreboardFile => $scoreboard_file,
2793 SystemLog => $log_file,
2794
2795 DefaultChdir => '~',
2796
2797 IfModules => {
2798 'mod_delay.c' => {
2799 DelayEngine => 'off',
2800 },
2801
2802 'mod_quotatab_sql.c' => [
2803 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2804 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2805 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
2806 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2807
2808 'QuotaEngine on',
2809 "QuotaLog $log_file",
2810 'QuotaDirectoryTally on',
2811 'QuotaLimitTable sql:/get-quota-limit',
2812 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2813 ],
2814
2815 'mod_sql.c' => {
2816 SQLAuthTypes => 'plaintext',
2817 SQLBackend => 'sqlite3',
2818 SQLConnectInfo => $db_file,
2819 SQLLogFile => $log_file,
2820 SQLMinID => '0',
2821 },
2822 },
2823 };
2824
2825 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2826
2827 # Open pipes, for use between the parent and child processes. Specifically,
2828 # the child will indicate when it's done with its test by writing a message
2829 # to the parent.
2830 my ($rfh, $wfh);
2831 unless (pipe($rfh, $wfh)) {
2832 die("Can't open pipe: $!");
2833 }
2834
2835 my $ex;
2836
2837 # Fork child
2838 $self->handle_sigchld();
2839 defined(my $pid = fork()) or die("Can't fork: $!");
2840 if ($pid) {
2841 eval {
2842 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2843 $client->login($user, $passwd);
2844
2845 my ($resp_code, $resp_msg) = $client->site('CPFR', 'src.d');
2846
2847 my $expected;
2848
2849 $expected = 350;
2850 $self->assert($expected == $resp_code,
2851 test_msg("Expected $expected, got $resp_code"));
2852
2853 $expected = "File or directory exists, ready for destination name";
2854 $self->assert($expected eq $resp_msg,
2855 test_msg("Expected '$expected', got '$resp_msg'"));
2856
2857 $client->site('CPTO', 'dst.d');
2858 $resp_code = $client->response_code();
2859 $resp_msg = $client->response_msg(0);
2860
2861 $expected = 250;
2862 $self->assert($expected == $resp_code,
2863 test_msg("Expected $expected, got $resp_code"));
2864
2865 $expected = "Copy successful";
2866 $self->assert($expected eq $resp_msg,
2867 test_msg("Expected '$expected', got '$resp_msg'"));
2868
2869 };
2870
2871 if ($@) {
2872 $ex = $@;
2873 }
2874
2875 $wfh->print("done\n");
2876 $wfh->flush();
2877
2878 } else {
2879 eval { server_wait($config_file, $rfh) };
2880 if ($@) {
2881 warn($@);
2882 exit 1;
2883 }
2884
2885 exit 0;
2886 }
2887
2888 # Stop server
2889 server_stop($pid_file);
2890
2891 $self->assert_child_ok($pid);
2892
2893 if ($ex) {
2894 test_append_logfile($log_file, $ex);
2895 unlink($log_file);
2896
2897 die($ex);
2898 }
2899
2900 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
2901
2902 my $expected;
2903
2904 $expected = 'user';
2905 $self->assert($expected eq $quota_type,
2906 test_msg("Expected '$expected', got '$quota_type'"));
2907
2908 $expected = '^(4124.0|4124)$';
2909 if ($^O eq 'linux') {
2910 # On Linux, the block size for a directory file might be different,
2911 # hence the different expectations of bytes-in-used.
2912 $expected = '^(8220.0|8222)$';
2913 }
2914
2915 $self->assert(qr/$expected/, $bytes_in_used,
2916 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
2917
2918 $expected = '^(0.0|0)$';
2919 $self->assert(qr/$expected/, $bytes_out_used,
2920 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
2921
2922 $expected = '^(0.0|0)$';
2923 $self->assert(qr/$expected/, $bytes_xfer_used,
2924 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
2925
2926 # For purposes of a recursive copy, a directory file counts toward the
2927 # "files uploaded" limit/tally.
2928 $expected = 3;
2929 $self->assert($expected == $files_in_used,
2930 test_msg("Expected $expected files-in-used, got $files_in_used"));
2931
2932 $expected = 0;
2933 $self->assert($expected == $files_out_used,
2934 test_msg("Expected $expected files-out-used, got $files_out_used"));
2935
2936 $expected = 0;
2937 $self->assert($expected == $files_xfer_used,
2938 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
2939
2940 unlink($log_file);
2941 }
2942
2943 1;