Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_quotatab_sql;
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_stor_ok_user_limit => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 quotatab_appe_ok_user_limit_bytes_in_exceeded_soft_limit => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 quotatab_appe_ok_user_limit_bytes_in_exceeded_hard_limit => {
30 order => ++$order,
31 test_class => [qw(forking)],
32 },
33
34 quotatab_retr_ok_user_limit_bytes_out_exceeded => {
35 order => ++$order,
36 test_class => [qw(forking)],
37 },
38
39 quotatab_retr_ok_user_limit_files_out_exceeded => {
40 order => ++$order,
41 test_class => [qw(forking)],
42 },
43
44 quotatab_stor_ok_user_limit_bytes_in_exceeded_soft_limit => {
45 order => ++$order,
46 test_class => [qw(forking)],
47 },
48
49 quotatab_stor_ok_user_limit_bytes_in_exceeded_hard_limit => {
50 order => ++$order,
51 test_class => [qw(forking)],
52 },
53
54 quotatab_stor_ok_user_limit_bytes_in_exceeded_hard_limit_hiddenstores_issue1185 => {
55 order => ++$order,
56 test_class => [qw(forking)],
57 },
58
59 quotatab_stor_ok_user_default_limit_bytes_in_exceeded_soft_limit => {
60 order => ++$order,
61 test_class => [qw(forking)],
62 },
63
64 quotatab_stor_ok_user_default_limit_bytes_in_exceeded_hard_limit => {
65 order => ++$order,
66 test_class => [qw(forking)],
67 },
68
69 quotatab_stor_ok_user_limit_files_in_exceeded => {
70 order => ++$order,
71 test_class => [qw(forking)],
72 },
73
74 quotatab_stor_ok_user_default_with_group_limit => {
75 order => ++$order,
76 test_class => [qw(forking)],
77 },
78
79 quotatab_stor_ok_user_default_with_no_group_limit => {
80 order => ++$order,
81 test_class => [qw(forking)],
82 },
83
84 quotatab_stor_ok_group_limit => {
85 order => ++$order,
86 test_class => [qw(forking)],
87 },
88
89 quotatab_stor_ok_group_limit_bytes_in_exceeded_soft_limit => {
90 order => ++$order,
91 test_class => [qw(forking)],
92 },
93
94 quotatab_stor_ok_group_limit_bytes_in_exceeded_hard_limit => {
95 order => ++$order,
96 test_class => [qw(forking)],
97 },
98
99 quotatab_stor_ok_group_limit_files_in_exceeded => {
100 order => ++$order,
101 test_class => [qw(forking)],
102 },
103
104 quotatab_stor_ok_group_limit_with_default => {
105 order => ++$order,
106 test_class => [qw(forking)],
107 },
108
109 quotatab_stor_ok_class_limit => {
110 order => ++$order,
111 test_class => [qw(forking)],
112 },
113
114 quotatab_stor_ok_class_limit_bytes_in_exceeded_soft_limit => {
115 order => ++$order,
116 test_class => [qw(forking)],
117 },
118
119 quotatab_stor_ok_class_limit_bytes_in_exceeded_hard_limit => {
120 order => ++$order,
121 test_class => [qw(forking)],
122 },
123
124 quotatab_stor_ok_class_limit_files_in_exceeded => {
125 order => ++$order,
126 test_class => [qw(forking)],
127 },
128
129 quotatab_stor_ok_all_limit => {
130 order => ++$order,
131 test_class => [qw(forking)],
132 },
133
134 quotatab_stor_ok_all_limit_bytes_in_exceeded_soft_limit => {
135 order => ++$order,
136 test_class => [qw(forking)],
137 },
138
139 quotatab_stor_ok_all_limit_bytes_in_exceeded_hard_limit => {
140 order => ++$order,
141 test_class => [qw(forking)],
142 },
143
144 quotatab_stor_ok_all_limit_files_in_exceeded => {
145 order => ++$order,
146 test_class => [qw(forking)],
147 },
148
149 quotatab_stor_bug3164 => {
150 order => ++$order,
151 test_class => [qw(bug forking)],
152 },
153
154 quotatab_dele_ok_user_limit => {
155 order => ++$order,
156 test_class => [qw(forking)],
157 },
158
159 quotatab_dele_user_owner_bug3161 => {
160 order => ++$order,
161 test_class => [qw(bug forking rootprivs)],
162 },
163
164 quotatab_dele_group_owner_bug3161 => {
165 order => ++$order,
166 test_class => [qw(bug forking rootprivs)],
167 },
168
169 quotatab_new_tally_lock_bug3086 => {
170 order => ++$order,
171 test_class => [qw(bug forking)],
172 },
173
174 quotatab_config_exclude_filter_bug3298 => {
175 order => ++$order,
176 test_class => [qw(bug forking)],
177 },
178
179 quotatab_config_exclude_filter_chrooted_bug3298 => {
180 order => ++$order,
181 test_class => [qw(bug forking rootprivs)],
182 },
183
184 quotatab_config_exclude_filter_bug3878 => {
185 order => ++$order,
186 test_class => [qw(bug forking)],
187 },
188
189 quotatab_config_opt_scanonlogin => {
190 order => ++$order,
191 test_class => [qw(forking)],
192 },
193
194 quotatab_config_opt_scanonlogin_chrooted => {
195 order => ++$order,
196 test_class => [qw(forking rootprivs)],
197 },
198
199 quotatab_config_opt_scanonlogin_new_tally_bug3440 => {
200 order => ++$order,
201 test_class => [qw(bug forking)],
202 },
203
204 quotatab_site_bug3483 => {
205 order => ++$order,
206 test_class => [qw(bug forking)],
207 },
208
209 quotatab_dele_failed_bug3517 => {
210 order => ++$order,
211 test_class => [qw(bug forking)],
212 },
213
214 quotatab_sql_dele_bug3524 => {
215 order => ++$order,
216 test_class => [qw(bug forking)],
217 },
218
219 quotatab_stor_deleteabortedstores_conn_abor_bug3621 => {
220 order => ++$order,
221 test_class => [qw(bug forking)],
222 },
223
224 quotatab_stor_deleteabortedstores_cmd_abor_bug3621 => {
225 order => ++$order,
226 test_class => [qw(bug forking)],
227 },
228
229 quotatab_sql_addl_query_columns_bug3879 => {
230 order => ++$order,
231 test_class => [qw(bug forking)],
232 },
233
234 quotatab_sql_odbc => {
235 order => ++$order,
236 test_class => [qw(forking mod_sql_odbc)],
237 },
238
239 };
240
241 sub new {
242 return shift()->SUPER::new(@_);
243 }
244
245 sub list_tests {
246 return testsuite_get_runnable_tests($TESTS);
247 }
248
249 sub get_tally {
250 my $db_file = shift;
251 my $where = shift;
252
253 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";
254 if ($where) {
255 $sql .= " WHERE $where";
256 }
257
258 my $cmd = "sqlite3 $db_file \"$sql\"";
259
260 if ($ENV{TEST_VERBOSE}) {
261 print STDERR "Executing sqlite3: $cmd\n";
262 }
263
264 my $res = join('', `$cmd`);
265
266 # The default sqlite3 delimiter is '|'
267 return split(/\|/, $res);
268 }
269
270 my $bug3164_wait_timeout = 0;
271 sub bug3164_wait_alarm {
272 croak("Test timed out after $bug3164_wait_timeout secs");
273 }
274
275 sub bug3164_server_wait {
276 my $config_file = shift;
277 my $db_file = shift;
278 my $rfh = shift;
279 $bug3164_wait_timeout = shift;
280 $bug3164_wait_timeout = 10 unless defined($bug3164_wait_timeout);
281
282 # Start server
283 server_start($config_file);
284
285 $SIG{ALRM} = \&bug3164_wait_alarm;
286 alarm($bug3164_wait_timeout);
287
288 # Wait until we receive word from the child that it has finished its test.
289 while (my $msg = <$rfh>) {
290 chomp($msg);
291
292 if ($msg eq 'do_update') {
293 my $cmd = "sqlite3 $db_file \"UPDATE quotatallies SET bytes_in_used = 10.0, bytes_out_used = 10.0, bytes_xfer_used = 10.0, files_in_used = 2, files_out_used = 2, files_xfer_used = 2 WHERE name = 'proftpd';\"";
294 if ($ENV{TEST_VERBOSE}) {
295 print STDERR "Executing sqlite3: $cmd\n";
296 }
297
298 my @output = `$cmd`;
299 if (scalar(@output) &&
300 $ENV{TEST_VERBOSE}) {
301 print STDERR "Output: ", join('', @output), "\n";
302 }
303
304 next;
305 }
306
307 if ($msg eq 'done') {
308 last;
309 }
310 }
311
312 alarm(0);
313 $SIG{ALRM} = 'DEFAULT';
314 return 1;
315 }
316
317 sub quotatab_stor_ok_user_limit {
318 my $self = shift;
319 my $tmpdir = $self->{tmpdir};
320
321 my $config_file = "$tmpdir/quotatab.conf";
322 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
323 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
324
325 my $log_file = test_get_logfile();
326
327 my $user = 'proftpd';
328 my $passwd = 'test';
329 my $group = 'ftpd';
330 my $home_dir = File::Spec->rel2abs($tmpdir);
331 my $uid = 500;
332 my $gid = 500;
333
334 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
335
336 # Build up sqlite3 command to create users, groups tables and populate them
337 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
338
339 if (open(my $fh, "> $db_script")) {
340 print $fh <<EOS;
341 CREATE TABLE users (
342 userid TEXT PRIMARY KEY,
343 passwd TEXT,
344 uid INTEGER,
345 gid INTEGER,
346 homedir TEXT,
347 shell TEXT,
348 lastdir TEXT
349 );
350 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
351
352 CREATE TABLE groups (
353 groupname TEXT PRIMARY KEY,
354 gid INTEGER,
355 members TEXT
356 );
357 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
358
359 CREATE TABLE quotalimits (
360 name TEXT NOT NULL PRIMARY KEY,
361 quota_type TEXT NOT NULL,
362 per_session TEXT NOT NULL,
363 limit_type TEXT NOT NULL,
364 bytes_in_avail REAL NOT NULL,
365 bytes_out_avail REAL NOT NULL,
366 bytes_xfer_avail REAL NOT NULL,
367 files_in_avail INTEGER NOT NULL,
368 files_out_avail INTEGER NOT NULL,
369 files_xfer_avail INTEGER NOT NULL
370 );
371 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);
372
373 CREATE TABLE quotatallies (
374 name TEXT NOT NULL PRIMARY KEY,
375 quota_type TEXT NOT NULL,
376 bytes_in_used REAL NOT NULL,
377 bytes_out_used REAL NOT NULL,
378 bytes_xfer_used REAL NOT NULL,
379 files_in_used INTEGER NOT NULL,
380 files_out_used INTEGER NOT NULL,
381 files_xfer_used INTEGER NOT NULL
382 );
383 EOS
384
385 unless (close($fh)) {
386 die("Can't write $db_script: $!");
387 }
388
389 } else {
390 die("Can't open $db_script: $!");
391 }
392
393 my $cmd = "sqlite3 $db_file < $db_script";
394
395 if ($ENV{TEST_VERBOSE}) {
396 print STDERR "Executing sqlite3: $cmd\n";
397 }
398
399 my @output = `$cmd`;
400 if (scalar(@output) &&
401 $ENV{TEST_VERBOSE}) {
402 print STDERR "Output: ", join('', @output), "\n";
403 }
404
405 my $config = {
406 PidFile => $pid_file,
407 ScoreboardFile => $scoreboard_file,
408 SystemLog => $log_file,
409
410 DefaultChdir => '~',
411
412 IfModules => {
413 'mod_delay.c' => {
414 DelayEngine => 'off',
415 },
416
417 'mod_quotatab_sql.c' => [
418 '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}\'"',
419 '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}\'"',
420 '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',
421 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
422
423 'QuotaEngine on',
424 "QuotaLog $log_file",
425 'QuotaLimitTable sql:/get-quota-limit',
426 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
427 ],
428
429 'mod_sql.c' => {
430 SQLAuthTypes => 'plaintext',
431 SQLBackend => 'sqlite3',
432 SQLConnectInfo => $db_file,
433 SQLLogFile => $log_file,
434 SQLMinID => '0',
435 },
436 },
437 };
438
439 my ($port, $config_user, $config_group) = config_write($config_file, $config);
440
441 # Open pipes, for use between the parent and child processes. Specifically,
442 # the child will indicate when it's done with its test by writing a message
443 # to the parent.
444 my ($rfh, $wfh);
445 unless (pipe($rfh, $wfh)) {
446 die("Can't open pipe: $!");
447 }
448
449 my $ex;
450
451 # Fork child
452 $self->handle_sigchld();
453 defined(my $pid = fork()) or die("Can't fork: $!");
454 if ($pid) {
455 eval {
456 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
457 $client->login($user, $passwd);
458
459 my $conn = $client->stor_raw('test.txt');
460 unless ($conn) {
461 die("Failed to STOR test.txt: " . $client->response_code() . " " .
462 $client->response_msg());
463 }
464
465 my $buf = "Hello, World\n";
466 $conn->write($buf, length($buf), 25);
467 eval { $conn->close() };
468
469 my $resp_code = $client->response_code();
470 my $resp_msg = $client->response_msg();
471
472 $self->assert_transfer_ok($resp_code, $resp_msg);
473 };
474
475 if ($@) {
476 $ex = $@;
477 }
478
479 $wfh->print("done\n");
480 $wfh->flush();
481
482 } else {
483 eval { server_wait($config_file, $rfh) };
484 if ($@) {
485 warn($@);
486 exit 1;
487 }
488
489 exit 0;
490 }
491
492 # Stop server
493 server_stop($pid_file);
494
495 $self->assert_child_ok($pid);
496
497 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\'");
498
499 my $expected;
500
501 $expected = 'user';
502 $self->assert($expected eq $quota_type,
503 test_msg("Expected '$expected', got '$quota_type'"));
504
505 $expected = '^(13.0|13)$';
506 $self->assert(qr/$expected/, $bytes_in_used,
507 test_msg("Expected $expected, got $bytes_in_used"));
508
509 $expected = '^(0.0|0)$';
510 $self->assert(qr/$expected/, $bytes_out_used,
511 test_msg("Expected $expected, got $bytes_out_used"));
512
513 $expected = '^(0.0|0)$';
514 $self->assert(qr/$expected/, $bytes_xfer_used,
515 test_msg("Expected $expected, got $bytes_xfer_used"));
516
517 $expected = 1;
518 $self->assert($expected == $files_in_used,
519 test_msg("Expected $expected, got $files_in_used"));
520
521 $expected = 0;
522 $self->assert($expected == $files_out_used,
523 test_msg("Expected $expected, got $files_out_used"));
524
525 $expected = 0;
526 $self->assert($expected == $files_xfer_used,
527 test_msg("Expected $expected, got $files_xfer_used"));
528
529 if ($ex) {
530 test_append_logfile($log_file, $ex);
531 unlink($log_file);
532
533 die($ex);
534 }
535
536 unlink($log_file);
537 }
538
539 sub quotatab_appe_ok_user_limit_bytes_in_exceeded_soft_limit {
540 my $self = shift;
541 my $tmpdir = $self->{tmpdir};
542
543 my $config_file = "$tmpdir/quotatab.conf";
544 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
545 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
546
547 my $log_file = test_get_logfile();
548
549 my $user = 'proftpd';
550 my $passwd = 'test';
551 my $group = 'ftpd';
552 my $home_dir = File::Spec->rel2abs($tmpdir);
553 my $uid = 500;
554 my $gid = 500;
555
556 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
557
558 if (open(my $fh, "> $test_file")) {
559 close($fh);
560
561 } else {
562 die("Can't open $test_file: $!");
563 }
564
565 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
566
567 # Build up sqlite3 command to create users, groups tables and populate them
568 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
569
570 if (open(my $fh, "> $db_script")) {
571 print $fh <<EOS;
572 CREATE TABLE users (
573 userid TEXT PRIMARY KEY,
574 passwd TEXT,
575 uid INTEGER,
576 gid INTEGER,
577 homedir TEXT,
578 shell TEXT,
579 lastdir TEXT
580 );
581 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
582
583 CREATE TABLE groups (
584 groupname TEXT PRIMARY KEY,
585 gid INTEGER,
586 members TEXT
587 );
588 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
589
590 CREATE TABLE quotalimits (
591 name TEXT NOT NULL PRIMARY KEY,
592 quota_type TEXT NOT NULL,
593 per_session TEXT NOT NULL,
594 limit_type TEXT NOT NULL,
595 bytes_in_avail REAL NOT NULL,
596 bytes_out_avail REAL NOT NULL,
597 bytes_xfer_avail REAL NOT NULL,
598 files_in_avail INTEGER NOT NULL,
599 files_out_avail INTEGER NOT NULL,
600 files_xfer_avail INTEGER NOT NULL
601 );
602 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, 3, 0, 0);
603
604 CREATE TABLE quotatallies (
605 name TEXT NOT NULL PRIMARY KEY,
606 quota_type TEXT NOT NULL,
607 bytes_in_used REAL NOT NULL,
608 bytes_out_used REAL NOT NULL,
609 bytes_xfer_used REAL NOT NULL,
610 files_in_used INTEGER NOT NULL,
611 files_out_used INTEGER NOT NULL,
612 files_xfer_used INTEGER NOT NULL
613 );
614 EOS
615
616 unless (close($fh)) {
617 die("Can't write $db_script: $!");
618 }
619
620 } else {
621 die("Can't open $db_script: $!");
622 }
623
624 my $cmd = "sqlite3 $db_file < $db_script";
625
626 if ($ENV{TEST_VERBOSE}) {
627 print STDERR "Executing sqlite3: $cmd\n";
628 }
629
630 my @output = `$cmd`;
631 if (scalar(@output) &&
632 $ENV{TEST_VERBOSE}) {
633 print STDERR "Output: ", join('', @output), "\n";
634 }
635
636 my $config = {
637 PidFile => $pid_file,
638 ScoreboardFile => $scoreboard_file,
639 SystemLog => $log_file,
640
641 AllowOverwrite => 'on',
642 AllowStoreRestart => 'on',
643 DefaultChdir => '~',
644
645 IfModules => {
646 'mod_delay.c' => {
647 DelayEngine => 'off',
648 },
649
650 'mod_quotatab_sql.c' => [
651 '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}\'"',
652 '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}\'"',
653 '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',
654 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
655
656 'QuotaEngine on',
657 "QuotaLog $log_file",
658 'QuotaLimitTable sql:/get-quota-limit',
659 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
660 ],
661
662 'mod_sql.c' => {
663 SQLAuthTypes => 'plaintext',
664 SQLBackend => 'sqlite3',
665 SQLConnectInfo => $db_file,
666 SQLLogFile => $log_file,
667 SQLMinID => '0',
668 },
669 },
670 };
671
672 my ($port, $config_user, $config_group) = config_write($config_file, $config);
673
674 # Open pipes, for use between the parent and child processes. Specifically,
675 # the child will indicate when it's done with its test by writing a message
676 # to the parent.
677 my ($rfh, $wfh);
678 unless (pipe($rfh, $wfh)) {
679 die("Can't open pipe: $!");
680 }
681
682 my $ex;
683
684 # Fork child
685 $self->handle_sigchld();
686 defined(my $pid = fork()) or die("Can't fork: $!");
687 if ($pid) {
688 eval {
689 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
690 $client->login($user, $passwd);
691
692 my $conn = $client->stor_raw('foo.txt');
693 unless ($conn) {
694 die("Failed to STOR test.txt: " . $client->response_code() . " " .
695 $client->response_msg());
696 }
697
698 my $buf = "Hello, World\n";
699 $conn->write($buf, length($buf), 25);
700 eval { $conn->close() };
701
702 my $resp_code = $client->response_code();
703 my $resp_msg = $client->response_msg();
704
705 $self->assert_transfer_ok($resp_code, $resp_msg);
706
707 # We've exceeded the soft limit, so this upload should be denied
708 $conn = $client->appe_raw('test.txt');
709 if ($conn) {
710 die("APPE test.txt succeeded unexpectedly");
711 }
712
713 $resp_code = $client->response_code();
714 $resp_msg = $client->response_msg();
715
716 my $expected = 552;
717 $self->assert($expected == $resp_code,
718 test_msg("Expected $expected, got $resp_code"));
719
720 $expected = 'APPE denied: quota exceeded: used \S+ of \S+ upload bytes';
721 $self->assert(qr/$expected/, $resp_msg,
722 test_msg("Expected '$expected', got '$resp_msg'"));
723
724 $client->quit();
725 };
726
727 if ($@) {
728 $ex = $@;
729 }
730
731 $wfh->print("done\n");
732 $wfh->flush();
733
734 } else {
735 eval { server_wait($config_file, $rfh) };
736 if ($@) {
737 warn($@);
738 exit 1;
739 }
740
741 exit 0;
742 }
743
744 # Stop server
745 server_stop($pid_file);
746
747 $self->assert_child_ok($pid);
748
749 if ($ex) {
750 test_append_logfile($log_file, $ex);
751 unlink($log_file);
752
753 die($ex);
754 }
755
756 unlink($log_file);
757 }
758
759 sub quotatab_appe_ok_user_limit_bytes_in_exceeded_hard_limit {
760 my $self = shift;
761 my $tmpdir = $self->{tmpdir};
762
763 my $config_file = "$tmpdir/quotatab.conf";
764 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
765 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
766
767 my $log_file = test_get_logfile();
768
769 my $user = 'proftpd';
770 my $passwd = 'test';
771 my $group = 'ftpd';
772 my $home_dir = File::Spec->rel2abs($tmpdir);
773 my $uid = 500;
774 my $gid = 500;
775
776 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
777
778 if (open(my $fh, "> $test_file")) {
779 close($fh);
780
781 } else {
782 die("Can't open $test_file: $!");
783 }
784
785 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
786
787 # Build up sqlite3 command to create users, groups tables and populate them
788 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
789
790 if (open(my $fh, "> $db_script")) {
791 print $fh <<EOS;
792 CREATE TABLE users (
793 userid TEXT PRIMARY KEY,
794 passwd TEXT,
795 uid INTEGER,
796 gid INTEGER,
797 homedir TEXT,
798 shell TEXT,
799 lastdir TEXT
800 );
801 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
802
803 CREATE TABLE groups (
804 groupname TEXT PRIMARY KEY,
805 gid INTEGER,
806 members TEXT
807 );
808 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
809
810 CREATE TABLE quotalimits (
811 name TEXT NOT NULL PRIMARY KEY,
812 quota_type TEXT NOT NULL,
813 per_session TEXT NOT NULL,
814 limit_type TEXT NOT NULL,
815 bytes_in_avail REAL NOT NULL,
816 bytes_out_avail REAL NOT NULL,
817 bytes_xfer_avail REAL NOT NULL,
818 files_in_avail INTEGER NOT NULL,
819 files_out_avail INTEGER NOT NULL,
820 files_xfer_avail INTEGER NOT NULL
821 );
822 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, 3, 0, 0);
823
824 CREATE TABLE quotatallies (
825 name TEXT NOT NULL PRIMARY KEY,
826 quota_type TEXT NOT NULL,
827 bytes_in_used REAL NOT NULL,
828 bytes_out_used REAL NOT NULL,
829 bytes_xfer_used REAL NOT NULL,
830 files_in_used INTEGER NOT NULL,
831 files_out_used INTEGER NOT NULL,
832 files_xfer_used INTEGER NOT NULL
833 );
834 EOS
835
836 unless (close($fh)) {
837 die("Can't write $db_script: $!");
838 }
839
840 } else {
841 die("Can't open $db_script: $!");
842 }
843
844 my $cmd = "sqlite3 $db_file < $db_script";
845
846 if ($ENV{TEST_VERBOSE}) {
847 print STDERR "Executing sqlite3: $cmd\n";
848 }
849
850 my @output = `$cmd`;
851 if (scalar(@output) &&
852 $ENV{TEST_VERBOSE}) {
853 print STDERR "Output: ", join('', @output), "\n";
854 }
855
856 my $config = {
857 PidFile => $pid_file,
858 ScoreboardFile => $scoreboard_file,
859 SystemLog => $log_file,
860
861 AllowOverwrite => 'on',
862 AllowStoreRestart => 'on',
863 DefaultChdir => '~',
864
865 IfModules => {
866 'mod_delay.c' => {
867 DelayEngine => 'off',
868 },
869
870 'mod_quotatab_sql.c' => [
871 '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}\'"',
872 '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}\'"',
873 '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',
874 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
875
876 'QuotaEngine on',
877 "QuotaLog $log_file",
878 'QuotaLimitTable sql:/get-quota-limit',
879 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
880 ],
881
882 'mod_sql.c' => {
883 SQLAuthTypes => 'plaintext',
884 SQLBackend => 'sqlite3',
885 SQLConnectInfo => $db_file,
886 SQLLogFile => $log_file,
887 SQLMinID => '0',
888 },
889 },
890 };
891
892 my ($port, $config_user, $config_group) = config_write($config_file, $config);
893
894 # Open pipes, for use between the parent and child processes. Specifically,
895 # the child will indicate when it's done with its test by writing a message
896 # to the parent.
897 my ($rfh, $wfh);
898 unless (pipe($rfh, $wfh)) {
899 die("Can't open pipe: $!");
900 }
901
902 my $ex;
903
904 # Fork child
905 $self->handle_sigchld();
906 defined(my $pid = fork()) or die("Can't fork: $!");
907 if ($pid) {
908 eval {
909 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
910 $client->login($user, $passwd);
911
912 my $conn = $client->appe_raw('test.txt');
913 unless ($conn) {
914 die("Failed to APPE test.txt: " . $client->response_code() . " " .
915 $client->response_msg());
916 }
917
918 my $buf = "Hello, World\n";
919 $conn->write($buf, length($buf), 25);
920 eval { $conn->close() };
921
922 my $resp_code = $client->response_code();
923 my $resp_msg = $client->response_msg();
924
925 my $expected;
926
927 $expected = 552;
928 $self->assert($expected == $resp_code,
929 test_msg("Expected $expected, got $resp_code"));
930
931 $expected = 'Transfer aborted. (Disc|Disk) quota exceeded';
932 $self->assert(qr/$expected/, $resp_msg,
933 test_msg("Expected '$expected', got '$resp_msg'"));
934
935 $client->quit();
936
937 if (-f $test_file) {
938 die("$test_file exists, should have been deleted");
939 }
940 };
941
942 if ($@) {
943 $ex = $@;
944 }
945
946 $wfh->print("done\n");
947 $wfh->flush();
948
949 } else {
950 eval { server_wait($config_file, $rfh) };
951 if ($@) {
952 warn($@);
953 exit 1;
954 }
955
956 exit 0;
957 }
958
959 # Stop server
960 server_stop($pid_file);
961
962 $self->assert_child_ok($pid);
963
964 if ($ex) {
965 test_append_logfile($log_file, $ex);
966 unlink($log_file);
967
968 die($ex);
969 }
970
971 unlink($log_file);
972 }
973
974 sub quotatab_retr_ok_user_limit_bytes_out_exceeded {
975 my $self = shift;
976 my $tmpdir = $self->{tmpdir};
977
978 my $config_file = "$tmpdir/quotatab.conf";
979 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
980 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
981
982 my $log_file = test_get_logfile();
983
984 my $user = 'proftpd';
985 my $passwd = 'test';
986 my $group = 'ftpd';
987 my $home_dir = File::Spec->rel2abs($tmpdir);
988 my $uid = 500;
989 my $gid = 500;
990
991 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
992 if (open(my $fh, "> $test_file")) {
993 print $fh "Hello, World!\n";
994 unless (close($fh)) {
995 die("Can't write $test_file: $!");
996 }
997
998 } else {
999 die("Can't open $test_file: $!");
1000 }
1001
1002 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1003
1004 # Build up sqlite3 command to create users, groups tables and populate them
1005 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1006
1007 if (open(my $fh, "> $db_script")) {
1008 print $fh <<EOS;
1009 CREATE TABLE users (
1010 userid TEXT PRIMARY KEY,
1011 passwd TEXT,
1012 uid INTEGER,
1013 gid INTEGER,
1014 homedir TEXT,
1015 shell TEXT,
1016 lastdir TEXT
1017 );
1018 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
1019
1020 CREATE TABLE groups (
1021 groupname TEXT PRIMARY KEY,
1022 gid INTEGER,
1023 members TEXT
1024 );
1025 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
1026
1027 CREATE TABLE quotalimits (
1028 name TEXT NOT NULL PRIMARY KEY,
1029 quota_type TEXT NOT NULL,
1030 per_session TEXT NOT NULL,
1031 limit_type TEXT NOT NULL,
1032 bytes_in_avail REAL NOT NULL,
1033 bytes_out_avail REAL NOT NULL,
1034 bytes_xfer_avail REAL NOT NULL,
1035 files_in_avail INTEGER NOT NULL,
1036 files_out_avail INTEGER NOT NULL,
1037 files_xfer_avail INTEGER NOT NULL
1038 );
1039 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', 0, 5, 0, 0, 3, 0);
1040
1041 CREATE TABLE quotatallies (
1042 name TEXT NOT NULL PRIMARY KEY,
1043 quota_type TEXT NOT NULL,
1044 bytes_in_used REAL NOT NULL,
1045 bytes_out_used REAL NOT NULL,
1046 bytes_xfer_used REAL NOT NULL,
1047 files_in_used INTEGER NOT NULL,
1048 files_out_used INTEGER NOT NULL,
1049 files_xfer_used INTEGER NOT NULL
1050 );
1051 EOS
1052
1053 unless (close($fh)) {
1054 die("Can't write $db_script: $!");
1055 }
1056
1057 } else {
1058 die("Can't open $db_script: $!");
1059 }
1060
1061 my $cmd = "sqlite3 $db_file < $db_script";
1062
1063 if ($ENV{TEST_VERBOSE}) {
1064 print STDERR "Executing sqlite3: $cmd\n";
1065 }
1066
1067 my @output = `$cmd`;
1068 if (scalar(@output) &&
1069 $ENV{TEST_VERBOSE}) {
1070 print STDERR "Output: ", join('', @output), "\n";
1071 }
1072
1073 my $config = {
1074 PidFile => $pid_file,
1075 ScoreboardFile => $scoreboard_file,
1076 SystemLog => $log_file,
1077 TraceLog => $log_file,
1078 Trace => 'DEFAULT:10',
1079
1080 DefaultChdir => '~',
1081
1082 IfModules => {
1083 'mod_delay.c' => {
1084 DelayEngine => 'off',
1085 },
1086
1087 'mod_quotatab_sql.c' => [
1088 '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}\'"',
1089 '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}\'"',
1090 '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',
1091 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1092
1093 'QuotaEngine on',
1094 "QuotaLog $log_file",
1095 'QuotaLimitTable sql:/get-quota-limit',
1096 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1097 ],
1098
1099 'mod_sql.c' => {
1100 SQLAuthTypes => 'plaintext',
1101 SQLBackend => 'sqlite3',
1102 SQLConnectInfo => $db_file,
1103 SQLLogFile => $log_file,
1104 SQLMinID => '0',
1105 },
1106 },
1107 };
1108
1109 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1110
1111 # Open pipes, for use between the parent and child processes. Specifically,
1112 # the child will indicate when it's done with its test by writing a message
1113 # to the parent.
1114 my ($rfh, $wfh);
1115 unless (pipe($rfh, $wfh)) {
1116 die("Can't open pipe: $!");
1117 }
1118
1119 my $ex;
1120
1121 # Fork child
1122 $self->handle_sigchld();
1123 defined(my $pid = fork()) or die("Can't fork: $!");
1124 if ($pid) {
1125 eval {
1126 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1);
1127 $client->login($user, $passwd);
1128
1129 my $conn = $client->retr_raw('test.txt');
1130 unless ($conn) {
1131 die("Failed to RETR test.txt: " . $client->response_code() . " " .
1132 $client->response_msg());
1133 }
1134
1135 my $buf;
1136 $conn->read($buf, 8192, 25);
1137 eval { $conn->close() };
1138
1139 my $resp_code = $client->response_code(1);
1140
1141 my $msg_idx = 1;
1142 if ($resp_code == 150) {
1143 $msg_idx = 0;
1144 }
1145 my $resp_msg = $client->response_msg($msg_idx);
1146
1147 $self->assert_transfer_ok($resp_code, $resp_msg);
1148
1149 sleep(1);
1150
1151 # We've exceeded the bytes out limit, so this download should be denied
1152 $conn = $client->retr_raw('test.txt');
1153 if ($conn) {
1154 die("RETR test.txt succeeded unexpectedly");
1155 }
1156
1157 $resp_code = $client->response_code();
1158 $resp_msg = $client->response_msg();
1159
1160 my $expected = 451;
1161 $self->assert($expected == $resp_code,
1162 test_msg("Expected response code $expected, got $resp_code"));
1163
1164 $expected = 'RETR denied: quota exceeded: used \S+ of \S+ download bytes';
1165 $self->assert(qr/$expected/, $resp_msg,
1166 test_msg("Expected response message '$expected', got '$resp_msg'"));
1167
1168 $client->quit();
1169 };
1170
1171 if ($@) {
1172 $ex = $@;
1173 }
1174
1175 $wfh->print("done\n");
1176 $wfh->flush();
1177
1178 } else {
1179 eval { server_wait($config_file, $rfh) };
1180 if ($@) {
1181 warn($@);
1182 exit 1;
1183 }
1184
1185 exit 0;
1186 }
1187
1188 # Stop server
1189 server_stop($pid_file);
1190
1191 $self->assert_child_ok($pid);
1192
1193 if ($ex) {
1194 test_append_logfile($log_file, $ex);
1195 unlink($log_file);
1196
1197 die($ex);
1198 }
1199
1200 unlink($log_file);
1201 }
1202
1203 sub quotatab_retr_ok_user_limit_files_out_exceeded {
1204 my $self = shift;
1205 my $tmpdir = $self->{tmpdir};
1206
1207 my $config_file = "$tmpdir/quotatab.conf";
1208 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1209 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1210
1211 my $log_file = test_get_logfile();
1212
1213 my $user = 'proftpd';
1214 my $passwd = 'test';
1215 my $group = 'ftpd';
1216 my $home_dir = File::Spec->rel2abs($tmpdir);
1217 my $uid = 500;
1218 my $gid = 500;
1219
1220 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
1221 if (open(my $fh, "> $test_file")) {
1222 print $fh "Hello, World!\n";
1223 unless (close($fh)) {
1224 die("Can't write $test_file: $!");
1225 }
1226
1227 } else {
1228 die("Can't open $test_file: $!");
1229 }
1230
1231 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1232
1233 # Build up sqlite3 command to create users, groups tables and populate them
1234 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1235
1236 if (open(my $fh, "> $db_script")) {
1237 print $fh <<EOS;
1238 CREATE TABLE users (
1239 userid TEXT PRIMARY KEY,
1240 passwd TEXT,
1241 uid INTEGER,
1242 gid INTEGER,
1243 homedir TEXT,
1244 shell TEXT,
1245 lastdir TEXT
1246 );
1247 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
1248
1249 CREATE TABLE groups (
1250 groupname TEXT PRIMARY KEY,
1251 gid INTEGER,
1252 members TEXT
1253 );
1254 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
1255
1256 CREATE TABLE quotalimits (
1257 name TEXT NOT NULL PRIMARY KEY,
1258 quota_type TEXT NOT NULL,
1259 per_session TEXT NOT NULL,
1260 limit_type TEXT NOT NULL,
1261 bytes_in_avail REAL NOT NULL,
1262 bytes_out_avail REAL NOT NULL,
1263 bytes_xfer_avail REAL NOT NULL,
1264 files_in_avail INTEGER NOT NULL,
1265 files_out_avail INTEGER NOT NULL,
1266 files_xfer_avail INTEGER NOT NULL
1267 );
1268 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', 0, 0, 0, 0, 1, 0);
1269
1270 CREATE TABLE quotatallies (
1271 name TEXT NOT NULL PRIMARY KEY,
1272 quota_type TEXT NOT NULL,
1273 bytes_in_used REAL NOT NULL,
1274 bytes_out_used REAL NOT NULL,
1275 bytes_xfer_used REAL NOT NULL,
1276 files_in_used INTEGER NOT NULL,
1277 files_out_used INTEGER NOT NULL,
1278 files_xfer_used INTEGER NOT NULL
1279 );
1280 EOS
1281
1282 unless (close($fh)) {
1283 die("Can't write $db_script: $!");
1284 }
1285
1286 } else {
1287 die("Can't open $db_script: $!");
1288 }
1289
1290 my $cmd = "sqlite3 $db_file < $db_script";
1291
1292 if ($ENV{TEST_VERBOSE}) {
1293 print STDERR "Executing sqlite3: $cmd\n";
1294 }
1295
1296 my @output = `$cmd`;
1297 if (scalar(@output) &&
1298 $ENV{TEST_VERBOSE}) {
1299 print STDERR "Output: ", join('', @output), "\n";
1300 }
1301
1302 my $config = {
1303 PidFile => $pid_file,
1304 ScoreboardFile => $scoreboard_file,
1305 SystemLog => $log_file,
1306 TraceLog => $log_file,
1307 Trace => 'DEFAULT:10 jot:30 sql:20',
1308
1309 DefaultChdir => '~',
1310
1311 IfModules => {
1312 'mod_delay.c' => {
1313 DelayEngine => 'off',
1314 },
1315
1316 'mod_quotatab_sql.c' => [
1317 '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}\'"',
1318 '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}\'"',
1319 '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',
1320 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1321
1322 'QuotaEngine on',
1323 "QuotaLog $log_file",
1324 'QuotaLimitTable sql:/get-quota-limit',
1325 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1326 ],
1327
1328 'mod_sql.c' => {
1329 SQLAuthTypes => 'plaintext',
1330 SQLBackend => 'sqlite3',
1331 SQLConnectInfo => $db_file,
1332 SQLLogFile => $log_file,
1333 SQLMinID => '0',
1334 },
1335 },
1336 };
1337
1338 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1339
1340 # Open pipes, for use between the parent and child processes. Specifically,
1341 # the child will indicate when it's done with its test by writing a message
1342 # to the parent.
1343 my ($rfh, $wfh);
1344 unless (pipe($rfh, $wfh)) {
1345 die("Can't open pipe: $!");
1346 }
1347
1348 my $ex;
1349
1350 # Fork child
1351 $self->handle_sigchld();
1352 defined(my $pid = fork()) or die("Can't fork: $!");
1353 if ($pid) {
1354 eval {
1355 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1);
1356 $client->login($user, $passwd);
1357
1358 my $conn = $client->retr_raw('test.txt');
1359 unless ($conn) {
1360 die("Failed to RETR test.txt: " . $client->response_code() . " " .
1361 $client->response_msg());
1362 }
1363
1364 my $buf;
1365 $conn->read($buf, 8192, 25);
1366 eval { $conn->close() };
1367
1368 my $resp_code = $client->response_code();
1369 my $resp_msg = $client->response_msg();
1370
1371 $self->assert_transfer_ok($resp_code, $resp_msg);
1372
1373 # We've exceeded the files out limit, so this download should be denied
1374 $conn = $client->retr_raw('test.txt');
1375 if ($conn) {
1376 die("RETR test.txt succeeded unexpectedly");
1377 }
1378
1379 $resp_code = $client->response_code();
1380 $resp_msg = $client->response_msg();
1381
1382 my $expected = 451;
1383 $self->assert($expected == $resp_code,
1384 test_msg("Expected $expected, got $resp_code"));
1385
1386 $expected = 'RETR denied: quota exceeded: used \S+ of \S+ download file(s)?';
1387 $self->assert(qr/$expected/, $resp_msg,
1388 test_msg("Expected '$expected', got '$resp_msg'"));
1389
1390 $client->quit();
1391 };
1392
1393 if ($@) {
1394 $ex = $@;
1395 }
1396
1397 $wfh->print("done\n");
1398 $wfh->flush();
1399
1400 } else {
1401 eval { server_wait($config_file, $rfh) };
1402 if ($@) {
1403 warn($@);
1404 exit 1;
1405 }
1406
1407 exit 0;
1408 }
1409
1410 # Stop server
1411 server_stop($pid_file);
1412
1413 $self->assert_child_ok($pid);
1414
1415 if ($ex) {
1416 test_append_logfile($log_file, $ex);
1417 unlink($log_file);
1418
1419 die($ex);
1420 }
1421
1422 unlink($log_file);
1423 }
1424
1425 sub quotatab_stor_ok_user_limit_bytes_in_exceeded_soft_limit {
1426 my $self = shift;
1427 my $tmpdir = $self->{tmpdir};
1428
1429 my $config_file = "$tmpdir/quotatab.conf";
1430 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1431 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1432
1433 my $log_file = test_get_logfile();
1434
1435 my $user = 'proftpd';
1436 my $passwd = 'test';
1437 my $group = 'ftpd';
1438 my $home_dir = File::Spec->rel2abs($tmpdir);
1439 my $uid = 500;
1440 my $gid = 500;
1441
1442 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1443
1444 # Build up sqlite3 command to create users, groups tables and populate them
1445 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1446
1447 if (open(my $fh, "> $db_script")) {
1448 print $fh <<EOS;
1449 CREATE TABLE users (
1450 userid TEXT PRIMARY KEY,
1451 passwd TEXT,
1452 uid INTEGER,
1453 gid INTEGER,
1454 homedir TEXT,
1455 shell TEXT,
1456 lastdir TEXT
1457 );
1458 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
1459
1460 CREATE TABLE groups (
1461 groupname TEXT PRIMARY KEY,
1462 gid INTEGER,
1463 members TEXT
1464 );
1465 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
1466
1467 CREATE TABLE quotalimits (
1468 name TEXT NOT NULL PRIMARY KEY,
1469 quota_type TEXT NOT NULL,
1470 per_session TEXT NOT NULL,
1471 limit_type TEXT NOT NULL,
1472 bytes_in_avail REAL NOT NULL,
1473 bytes_out_avail REAL NOT NULL,
1474 bytes_xfer_avail REAL NOT NULL,
1475 files_in_avail INTEGER NOT NULL,
1476 files_out_avail INTEGER NOT NULL,
1477 files_xfer_avail INTEGER NOT NULL
1478 );
1479 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, 3, 0, 0);
1480
1481 CREATE TABLE quotatallies (
1482 name TEXT NOT NULL PRIMARY KEY,
1483 quota_type TEXT NOT NULL,
1484 bytes_in_used REAL NOT NULL,
1485 bytes_out_used REAL NOT NULL,
1486 bytes_xfer_used REAL NOT NULL,
1487 files_in_used INTEGER NOT NULL,
1488 files_out_used INTEGER NOT NULL,
1489 files_xfer_used INTEGER NOT NULL
1490 );
1491 EOS
1492
1493 unless (close($fh)) {
1494 die("Can't write $db_script: $!");
1495 }
1496
1497 } else {
1498 die("Can't open $db_script: $!");
1499 }
1500
1501 my $cmd = "sqlite3 $db_file < $db_script";
1502
1503 if ($ENV{TEST_VERBOSE}) {
1504 print STDERR "Executing sqlite3: $cmd\n";
1505 }
1506
1507 my @output = `$cmd`;
1508 if (scalar(@output) &&
1509 $ENV{TEST_VERBOSE}) {
1510 print STDERR "Output: ", join('', @output), "\n";
1511 }
1512
1513 my $config = {
1514 PidFile => $pid_file,
1515 ScoreboardFile => $scoreboard_file,
1516 SystemLog => $log_file,
1517
1518 DefaultChdir => '~',
1519
1520 IfModules => {
1521 'mod_delay.c' => {
1522 DelayEngine => 'off',
1523 },
1524
1525 'mod_quotatab_sql.c' => [
1526 '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}\'"',
1527 '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}\'"',
1528 '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',
1529 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1530
1531 'QuotaEngine on',
1532 "QuotaLog $log_file",
1533 'QuotaLimitTable sql:/get-quota-limit',
1534 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1535 ],
1536
1537 'mod_sql.c' => {
1538 SQLAuthTypes => 'plaintext',
1539 SQLBackend => 'sqlite3',
1540 SQLConnectInfo => $db_file,
1541 SQLLogFile => $log_file,
1542 SQLMinID => '0',
1543 },
1544 },
1545 };
1546
1547 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1548
1549 # Open pipes, for use between the parent and child processes. Specifically,
1550 # the child will indicate when it's done with its test by writing a message
1551 # to the parent.
1552 my ($rfh, $wfh);
1553 unless (pipe($rfh, $wfh)) {
1554 die("Can't open pipe: $!");
1555 }
1556
1557 my $ex;
1558
1559 # Fork child
1560 $self->handle_sigchld();
1561 defined(my $pid = fork()) or die("Can't fork: $!");
1562 if ($pid) {
1563 eval {
1564 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1565 $client->login($user, $passwd);
1566
1567 my $conn = $client->stor_raw('foo.txt');
1568 unless ($conn) {
1569 die("Failed to STOR test.txt: " . $client->response_code() . " " .
1570 $client->response_msg());
1571 }
1572
1573 my $buf = "Hello, World\n";
1574 $conn->write($buf, length($buf), 25);
1575 eval { $conn->close() };
1576
1577 my $resp_code = $client->response_code();
1578 my $resp_msg = $client->response_msg();
1579
1580 $self->assert_transfer_ok($resp_code, $resp_msg);
1581
1582 # We've exceeded the soft limit, so this upload should be denied
1583 $conn = $client->stor_raw('test.txt');
1584 if ($conn) {
1585 die("STOR test.txt succeeded unexpectedly");
1586 }
1587
1588 $resp_code = $client->response_code();
1589 $resp_msg = $client->response_msg();
1590
1591 my $expected = 552;
1592 $self->assert($expected == $resp_code,
1593 test_msg("Expected $expected, got $resp_code"));
1594
1595 $expected = 'STOR denied: quota exceeded: used \S+ of \S+ upload bytes';
1596 $self->assert(qr/$expected/, $resp_msg,
1597 test_msg("Expected '$expected', got '$resp_msg'"));
1598
1599 $client->quit();
1600 };
1601
1602 if ($@) {
1603 $ex = $@;
1604 }
1605
1606 $wfh->print("done\n");
1607 $wfh->flush();
1608
1609 } else {
1610 eval { server_wait($config_file, $rfh) };
1611 if ($@) {
1612 warn($@);
1613 exit 1;
1614 }
1615
1616 exit 0;
1617 }
1618
1619 # Stop server
1620 server_stop($pid_file);
1621
1622 $self->assert_child_ok($pid);
1623
1624 if ($ex) {
1625 test_append_logfile($log_file, $ex);
1626 unlink($log_file);
1627
1628 die($ex);
1629 }
1630
1631 unlink($log_file);
1632 }
1633
1634 sub quotatab_stor_ok_user_limit_bytes_in_exceeded_hard_limit {
1635 my $self = shift;
1636 my $tmpdir = $self->{tmpdir};
1637 my $setup = test_setup($tmpdir, 'quotatab');
1638
1639 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1640
1641 # Build up sqlite3 command to create users, groups tables and populate them
1642 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1643
1644 if (open(my $fh, "> $db_script")) {
1645 print $fh <<EOS;
1646 CREATE TABLE users (
1647 userid TEXT PRIMARY KEY,
1648 passwd TEXT,
1649 uid INTEGER,
1650 gid INTEGER,
1651 homedir TEXT,
1652 shell TEXT,
1653 lastdir TEXT
1654 );
1655 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', 500, 500, '$setup->{home_dir}', '/bin/bash');
1656
1657 CREATE TABLE groups (
1658 groupname TEXT PRIMARY KEY,
1659 gid INTEGER,
1660 members TEXT
1661 );
1662 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', 500, '$setup->{user}');
1663
1664 CREATE TABLE quotalimits (
1665 name TEXT NOT NULL PRIMARY KEY,
1666 quota_type TEXT NOT NULL,
1667 per_session TEXT NOT NULL,
1668 limit_type TEXT NOT NULL,
1669 bytes_in_avail REAL NOT NULL,
1670 bytes_out_avail REAL NOT NULL,
1671 bytes_xfer_avail REAL NOT NULL,
1672 files_in_avail INTEGER NOT NULL,
1673 files_out_avail INTEGER NOT NULL,
1674 files_xfer_avail INTEGER NOT NULL
1675 );
1676 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 ('$setup->{user}', 'user', 'false', 'hard', 5, 0, 0, 3, 0, 0);
1677
1678 CREATE TABLE quotatallies (
1679 name TEXT NOT NULL PRIMARY KEY,
1680 quota_type TEXT NOT NULL,
1681 bytes_in_used REAL NOT NULL,
1682 bytes_out_used REAL NOT NULL,
1683 bytes_xfer_used REAL NOT NULL,
1684 files_in_used INTEGER NOT NULL,
1685 files_out_used INTEGER NOT NULL,
1686 files_xfer_used INTEGER NOT NULL
1687 );
1688 EOS
1689
1690 unless (close($fh)) {
1691 die("Can't write $db_script: $!");
1692 }
1693
1694 } else {
1695 die("Can't open $db_script: $!");
1696 }
1697
1698 my $cmd = "sqlite3 $db_file < $db_script";
1699
1700 if ($ENV{TEST_VERBOSE}) {
1701 print STDERR "Executing sqlite3: $cmd\n";
1702 }
1703
1704 my @output = `$cmd`;
1705 if (scalar(@output) &&
1706 $ENV{TEST_VERBOSE}) {
1707 print STDERR "Output: ", join('', @output), "\n";
1708 }
1709
1710 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
1711
1712 my $config = {
1713 PidFile => $setup->{pid_file},
1714 ScoreboardFile => $setup->{scoreboard_file},
1715 SystemLog => $setup->{log_file},
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 $setup->{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 => $setup->{log_file},
1741 SQLMinID => '0',
1742 },
1743 },
1744 };
1745
1746 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1747 $config);
1748
1749 # Open pipes, for use between the parent and child processes. Specifically,
1750 # the child will indicate when it's done with its test by writing a message
1751 # to the parent.
1752 my ($rfh, $wfh);
1753 unless (pipe($rfh, $wfh)) {
1754 die("Can't open pipe: $!");
1755 }
1756
1757 my $ex;
1758
1759 # Fork child
1760 $self->handle_sigchld();
1761 defined(my $pid = fork()) or die("Can't fork: $!");
1762 if ($pid) {
1763 eval {
1764 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1765 $client->login($setup->{user}, $setup->{passwd});
1766
1767 my $conn = $client->stor_raw('test.txt');
1768 unless ($conn) {
1769 die("Failed to STOR test.txt: " . $client->response_code() . " " .
1770 $client->response_msg());
1771 }
1772
1773 my $buf = "Hello, World\n";
1774 $conn->write($buf, length($buf), 25);
1775 eval { $conn->close() };
1776
1777 my $resp_code = $client->response_code();
1778 my $resp_msg = $client->response_msg();
1779
1780 my $expected = 552;
1781 $self->assert($expected == $resp_code,
1782 test_msg("Expected response code $expected, got $resp_code"));
1783
1784 $expected = 'Transfer aborted. (Disc|Disk) quota exceeded';
1785 $self->assert(qr/$expected/, $resp_msg,
1786 test_msg("Expected response message '$expected', got '$resp_msg'"));
1787
1788 $client->quit();
1789
1790 if (-f $test_file) {
1791 die("$test_file exists, should have been deleted");
1792 }
1793 };
1794 if ($@) {
1795 $ex = $@;
1796 }
1797
1798 $wfh->print("done\n");
1799 $wfh->flush();
1800
1801 } else {
1802 eval { server_wait($setup->{config_file}, $rfh) };
1803 if ($@) {
1804 warn($@);
1805 exit 1;
1806 }
1807
1808 exit 0;
1809 }
1810
1811 # Stop server
1812 server_stop($setup->{pid_file});
1813 $self->assert_child_ok($pid);
1814
1815 test_cleanup($setup->{log_file}, $ex);
1816 }
1817
1818 sub quotatab_stor_ok_user_limit_bytes_in_exceeded_hard_limit_hiddenstores_issue1185 {
1819 my $self = shift;
1820 my $tmpdir = $self->{tmpdir};
1821 my $setup = test_setup($tmpdir, 'quotatab');
1822
1823 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1824
1825 # Build up sqlite3 command to create users, groups tables and populate them
1826 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1827
1828 if (open(my $fh, "> $db_script")) {
1829 print $fh <<EOS;
1830 CREATE TABLE users (
1831 userid TEXT PRIMARY KEY,
1832 passwd TEXT,
1833 uid INTEGER,
1834 gid INTEGER,
1835 homedir TEXT,
1836 shell TEXT,
1837 lastdir TEXT
1838 );
1839 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', 500, 500, '$setup->{home_dir}', '/bin/bash');
1840
1841 CREATE TABLE groups (
1842 groupname TEXT PRIMARY KEY,
1843 gid INTEGER,
1844 members TEXT
1845 );
1846 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', 500, '$setup->{user}');
1847
1848 CREATE TABLE quotalimits (
1849 name TEXT NOT NULL PRIMARY KEY,
1850 quota_type TEXT NOT NULL,
1851 per_session TEXT NOT NULL,
1852 limit_type TEXT NOT NULL,
1853 bytes_in_avail REAL NOT NULL,
1854 bytes_out_avail REAL NOT NULL,
1855 bytes_xfer_avail REAL NOT NULL,
1856 files_in_avail INTEGER NOT NULL,
1857 files_out_avail INTEGER NOT NULL,
1858 files_xfer_avail INTEGER NOT NULL
1859 );
1860 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 ('$setup->{user}', 'user', 'false', 'hard', 5, 0, 0, 3, 0, 0);
1861
1862 CREATE TABLE quotatallies (
1863 name TEXT NOT NULL PRIMARY KEY,
1864 quota_type TEXT NOT NULL,
1865 bytes_in_used REAL NOT NULL,
1866 bytes_out_used REAL NOT NULL,
1867 bytes_xfer_used REAL NOT NULL,
1868 files_in_used INTEGER NOT NULL,
1869 files_out_used INTEGER NOT NULL,
1870 files_xfer_used INTEGER NOT NULL
1871 );
1872 EOS
1873
1874 unless (close($fh)) {
1875 die("Can't write $db_script: $!");
1876 }
1877
1878 } else {
1879 die("Can't open $db_script: $!");
1880 }
1881
1882 my $cmd = "sqlite3 $db_file < $db_script";
1883
1884 if ($ENV{TEST_VERBOSE}) {
1885 print STDERR "Executing sqlite3: $cmd\n";
1886 }
1887
1888 my @output = `$cmd`;
1889 if (scalar(@output) &&
1890 $ENV{TEST_VERBOSE}) {
1891 print STDERR "Output: ", join('', @output), "\n";
1892 }
1893
1894 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
1895 if (open(my $fh, "> $test_file")) {
1896 print $fh "Who goes there?\n";
1897 unless (close($fh)) {
1898 die("Can't write $test_file: $!");
1899 }
1900
1901 } else {
1902 die("Can't open $test_file: $!");
1903 }
1904
1905 my $config = {
1906 PidFile => $setup->{pid_file},
1907 ScoreboardFile => $setup->{scoreboard_file},
1908 SystemLog => $setup->{log_file},
1909
1910 AllowOverwrite => 'on',
1911 DefaultChdir => '~',
1912 HiddenStores => 'on',
1913
1914 IfModules => {
1915 'mod_delay.c' => {
1916 DelayEngine => 'off',
1917 },
1918
1919 'mod_quotatab_sql.c' => [
1920 '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}\'"',
1921 '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}\'"',
1922 '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',
1923 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1924
1925 'QuotaEngine on',
1926 "QuotaLog $setup->{log_file}",
1927 'QuotaLimitTable sql:/get-quota-limit',
1928 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1929 ],
1930
1931 'mod_sql.c' => {
1932 SQLAuthTypes => 'plaintext',
1933 SQLBackend => 'sqlite3',
1934 SQLConnectInfo => $db_file,
1935 SQLLogFile => $setup->{log_file},
1936 SQLMinID => '0',
1937 },
1938 },
1939 };
1940
1941 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1942 $config);
1943
1944 # Open pipes, for use between the parent and child processes. Specifically,
1945 # the child will indicate when it's done with its test by writing a message
1946 # to the parent.
1947 my ($rfh, $wfh);
1948 unless (pipe($rfh, $wfh)) {
1949 die("Can't open pipe: $!");
1950 }
1951
1952 my $ex;
1953
1954 # Fork child
1955 $self->handle_sigchld();
1956 defined(my $pid = fork()) or die("Can't fork: $!");
1957 if ($pid) {
1958 eval {
1959 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1960 $client->login($setup->{user}, $setup->{passwd});
1961
1962 my $conn = $client->stor_raw('test.txt');
1963 unless ($conn) {
1964 die("Failed to STOR test.txt: " . $client->response_code() . " " .
1965 $client->response_msg());
1966 }
1967
1968 my $buf = "Hello, World\n";
1969 $conn->write($buf, length($buf), 25);
1970 eval { $conn->close() };
1971
1972 my $resp_code = $client->response_code();
1973 my $resp_msg = $client->response_msg();
1974
1975 my $expected = 552;
1976 $self->assert($expected == $resp_code,
1977 test_msg("Expected response code $expected, got $resp_code"));
1978
1979 $expected = 'Transfer aborted. (Disc|Disk) quota exceeded';
1980 $self->assert(qr/$expected/, $resp_msg,
1981 test_msg("Expected response message '$expected', got '$resp_msg'"));
1982
1983 $client->quit();
1984
1985 unless (-f $test_file) {
1986 die("$test_file does not exist as expected");
1987 }
1988 };
1989 if ($@) {
1990 $ex = $@;
1991 }
1992
1993 $wfh->print("done\n");
1994 $wfh->flush();
1995
1996 } else {
1997 eval { server_wait($setup->{config_file}, $rfh) };
1998 if ($@) {
1999 warn($@);
2000 exit 1;
2001 }
2002
2003 exit 0;
2004 }
2005
2006 # Stop server
2007 server_stop($setup->{pid_file});
2008 $self->assert_child_ok($pid);
2009
2010 test_cleanup($setup->{log_file}, $ex);
2011 }
2012
2013 sub quotatab_stor_ok_user_default_limit_bytes_in_exceeded_soft_limit {
2014 my $self = shift;
2015 my $tmpdir = $self->{tmpdir};
2016
2017 my $config_file = "$tmpdir/quotatab.conf";
2018 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2019 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2020
2021 my $log_file = test_get_logfile();
2022
2023 my $user = 'proftpd';
2024 my $passwd = 'test';
2025 my $group = 'ftpd';
2026 my $home_dir = File::Spec->rel2abs($tmpdir);
2027 my $uid = 500;
2028 my $gid = 500;
2029
2030 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2031
2032 # Build up sqlite3 command to create users, groups tables and populate them
2033 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2034
2035 if (open(my $fh, "> $db_script")) {
2036 print $fh <<EOS;
2037 CREATE TABLE users (
2038 userid TEXT PRIMARY KEY,
2039 passwd TEXT,
2040 uid INTEGER,
2041 gid INTEGER,
2042 homedir TEXT,
2043 shell TEXT,
2044 lastdir TEXT
2045 );
2046 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
2047
2048 CREATE TABLE groups (
2049 groupname TEXT PRIMARY KEY,
2050 gid INTEGER,
2051 members TEXT
2052 );
2053 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
2054
2055 CREATE TABLE quotalimits (
2056 name TEXT NOT NULL PRIMARY KEY,
2057 quota_type TEXT NOT NULL,
2058 per_session TEXT NOT NULL,
2059 limit_type TEXT NOT NULL,
2060 bytes_in_avail REAL NOT NULL,
2061 bytes_out_avail REAL NOT NULL,
2062 bytes_xfer_avail REAL NOT NULL,
2063 files_in_avail INTEGER NOT NULL,
2064 files_out_avail INTEGER NOT NULL,
2065 files_xfer_avail INTEGER NOT NULL
2066 );
2067
2068 CREATE TABLE quotatallies (
2069 name TEXT NOT NULL PRIMARY KEY,
2070 quota_type TEXT NOT NULL,
2071 bytes_in_used REAL NOT NULL,
2072 bytes_out_used REAL NOT NULL,
2073 bytes_xfer_used REAL NOT NULL,
2074 files_in_used INTEGER NOT NULL,
2075 files_out_used INTEGER NOT NULL,
2076 files_xfer_used INTEGER NOT NULL
2077 );
2078 EOS
2079
2080 unless (close($fh)) {
2081 die("Can't write $db_script: $!");
2082 }
2083
2084 } else {
2085 die("Can't open $db_script: $!");
2086 }
2087
2088 my $cmd = "sqlite3 $db_file < $db_script";
2089
2090 if ($ENV{TEST_VERBOSE}) {
2091 print STDERR "Executing sqlite3: $cmd\n";
2092 }
2093
2094 my @output = `$cmd`;
2095 if (scalar(@output) &&
2096 $ENV{TEST_VERBOSE}) {
2097 print STDERR "Output: ", join('', @output), "\n";
2098 }
2099
2100 my $config = {
2101 PidFile => $pid_file,
2102 ScoreboardFile => $scoreboard_file,
2103 SystemLog => $log_file,
2104
2105 DefaultChdir => '~',
2106
2107 IfModules => {
2108 'mod_delay.c' => {
2109 DelayEngine => 'off',
2110 },
2111
2112 'mod_quotatab_sql.c' => [
2113 '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}\'"',
2114 '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}\'"',
2115 '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',
2116 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2117
2118 'QuotaEngine on',
2119 "QuotaLog $log_file",
2120 'QuotaLimitTable sql:/get-quota-limit',
2121 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2122
2123 'QuotaDefault group false soft 5 0 0 3 0 0',
2124 ],
2125
2126 'mod_sql.c' => {
2127 SQLAuthTypes => 'plaintext',
2128 SQLBackend => 'sqlite3',
2129 SQLConnectInfo => $db_file,
2130 SQLLogFile => $log_file,
2131 SQLMinID => '0',
2132 },
2133 },
2134 };
2135
2136 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2137
2138 # Open pipes, for use between the parent and child processes. Specifically,
2139 # the child will indicate when it's done with its test by writing a message
2140 # to the parent.
2141 my ($rfh, $wfh);
2142 unless (pipe($rfh, $wfh)) {
2143 die("Can't open pipe: $!");
2144 }
2145
2146 my $ex;
2147
2148 # Fork child
2149 $self->handle_sigchld();
2150 defined(my $pid = fork()) or die("Can't fork: $!");
2151 if ($pid) {
2152 eval {
2153 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2154 $client->login($user, $passwd);
2155
2156 my $conn = $client->stor_raw('foo.txt');
2157 unless ($conn) {
2158 die("Failed to STOR test.txt: " . $client->response_code() . " " .
2159 $client->response_msg());
2160 }
2161
2162 my $buf = "Hello, World\n";
2163 $conn->write($buf, length($buf), 25);
2164 eval { $conn->close() };
2165
2166 my $resp_code = $client->response_code();
2167 my $resp_msg = $client->response_msg();
2168
2169 $self->assert_transfer_ok($resp_code, $resp_msg);
2170
2171 # We've exceeded the soft limit, so this upload should be denied
2172 $conn = $client->stor_raw('test.txt');
2173 if ($conn) {
2174 die("STOR test.txt succeeded unexpectedly");
2175 }
2176
2177 $resp_code = $client->response_code();
2178 $resp_msg = $client->response_msg();
2179
2180 my $expected = 552;
2181 $self->assert($expected == $resp_code,
2182 test_msg("Expected $expected, got $resp_code"));
2183
2184 $expected = 'STOR denied: quota exceeded: used \S+ of \S+ upload bytes';
2185 $self->assert(qr/$expected/, $resp_msg,
2186 test_msg("Expected '$expected', got '$resp_msg'"));
2187
2188 $client->quit();
2189 };
2190
2191 if ($@) {
2192 $ex = $@;
2193 }
2194
2195 $wfh->print("done\n");
2196 $wfh->flush();
2197
2198 } else {
2199 eval { server_wait($config_file, $rfh) };
2200 if ($@) {
2201 warn($@);
2202 exit 1;
2203 }
2204
2205 exit 0;
2206 }
2207
2208 # Stop server
2209 server_stop($pid_file);
2210
2211 $self->assert_child_ok($pid);
2212
2213 if ($ex) {
2214 test_append_logfile($log_file, $ex);
2215 unlink($log_file);
2216
2217 die($ex);
2218 }
2219
2220 unlink($log_file);
2221 }
2222
2223 sub quotatab_stor_ok_user_default_limit_bytes_in_exceeded_hard_limit {
2224 my $self = shift;
2225 my $tmpdir = $self->{tmpdir};
2226
2227 my $config_file = "$tmpdir/quotatab.conf";
2228 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2229 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2230
2231 my $log_file = test_get_logfile();
2232
2233 my $user = 'proftpd';
2234 my $passwd = 'test';
2235 my $group = 'ftpd';
2236 my $home_dir = File::Spec->rel2abs($tmpdir);
2237 my $uid = 500;
2238 my $gid = 500;
2239
2240 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2241
2242 # Build up sqlite3 command to create users, groups tables and populate them
2243 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2244
2245 if (open(my $fh, "> $db_script")) {
2246 print $fh <<EOS;
2247 CREATE TABLE users (
2248 userid TEXT PRIMARY KEY,
2249 passwd TEXT,
2250 uid INTEGER,
2251 gid INTEGER,
2252 homedir TEXT,
2253 shell TEXT,
2254 lastdir TEXT
2255 );
2256 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
2257
2258 CREATE TABLE groups (
2259 groupname TEXT PRIMARY KEY,
2260 gid INTEGER,
2261 members TEXT
2262 );
2263 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
2264
2265 CREATE TABLE quotalimits (
2266 name TEXT NOT NULL PRIMARY KEY,
2267 quota_type TEXT NOT NULL,
2268 per_session TEXT NOT NULL,
2269 limit_type TEXT NOT NULL,
2270 bytes_in_avail REAL NOT NULL,
2271 bytes_out_avail REAL NOT NULL,
2272 bytes_xfer_avail REAL NOT NULL,
2273 files_in_avail INTEGER NOT NULL,
2274 files_out_avail INTEGER NOT NULL,
2275 files_xfer_avail INTEGER NOT NULL
2276 );
2277
2278 CREATE TABLE quotatallies (
2279 name TEXT NOT NULL PRIMARY KEY,
2280 quota_type TEXT NOT NULL,
2281 bytes_in_used REAL NOT NULL,
2282 bytes_out_used REAL NOT NULL,
2283 bytes_xfer_used REAL NOT NULL,
2284 files_in_used INTEGER NOT NULL,
2285 files_out_used INTEGER NOT NULL,
2286 files_xfer_used INTEGER NOT NULL
2287 );
2288 EOS
2289
2290 unless (close($fh)) {
2291 die("Can't write $db_script: $!");
2292 }
2293
2294 } else {
2295 die("Can't open $db_script: $!");
2296 }
2297
2298 my $cmd = "sqlite3 $db_file < $db_script";
2299
2300 if ($ENV{TEST_VERBOSE}) {
2301 print STDERR "Executing sqlite3: $cmd\n";
2302 }
2303
2304 my @output = `$cmd`;
2305 if (scalar(@output) &&
2306 $ENV{TEST_VERBOSE}) {
2307 print STDERR "Output: ", join('', @output), "\n";
2308 }
2309
2310 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
2311
2312 my $config = {
2313 PidFile => $pid_file,
2314 ScoreboardFile => $scoreboard_file,
2315 SystemLog => $log_file,
2316
2317 DefaultChdir => '~',
2318
2319 IfModules => {
2320 'mod_delay.c' => {
2321 DelayEngine => 'off',
2322 },
2323
2324 'mod_quotatab_sql.c' => [
2325 '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}\'"',
2326 '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}\'"',
2327 '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',
2328 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2329
2330 'QuotaEngine on',
2331 "QuotaLog $log_file",
2332 'QuotaLimitTable sql:/get-quota-limit',
2333 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2334
2335 'QuotaDefault user false hard 5 0 0 3 0 0',
2336 ],
2337
2338 'mod_sql.c' => {
2339 SQLAuthTypes => 'plaintext',
2340 SQLBackend => 'sqlite3',
2341 SQLConnectInfo => $db_file,
2342 SQLLogFile => $log_file,
2343 SQLMinID => '0',
2344 },
2345 },
2346 };
2347
2348 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2349
2350 # Open pipes, for use between the parent and child processes. Specifically,
2351 # the child will indicate when it's done with its test by writing a message
2352 # to the parent.
2353 my ($rfh, $wfh);
2354 unless (pipe($rfh, $wfh)) {
2355 die("Can't open pipe: $!");
2356 }
2357
2358 my $ex;
2359
2360 # Fork child
2361 $self->handle_sigchld();
2362 defined(my $pid = fork()) or die("Can't fork: $!");
2363 if ($pid) {
2364 eval {
2365 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2366 $client->login($user, $passwd);
2367
2368 my $conn = $client->stor_raw('test.txt');
2369 unless ($conn) {
2370 die("Failed to STOR test.txt: " . $client->response_code() . " " .
2371 $client->response_msg());
2372 }
2373
2374 my $buf = "Hello, World\n";
2375 $conn->write($buf, length($buf), 25);
2376 eval { $conn->close() };
2377
2378 my $resp_code = $client->response_code();
2379 my $resp_msg = $client->response_msg();
2380
2381 my $expected;
2382
2383 $expected = 552;
2384 $self->assert($expected == $resp_code,
2385 test_msg("Expected $expected, got $resp_code"));
2386
2387 $expected = 'Transfer aborted. (Disc|Disk) quota exceeded';
2388 $self->assert(qr/$expected/, $resp_msg,
2389 test_msg("Expected '$expected', got '$resp_msg'"));
2390
2391 $client->quit();
2392
2393 if (-f $test_file) {
2394 die("$test_file exists, should have been deleted");
2395 }
2396 };
2397
2398 if ($@) {
2399 $ex = $@;
2400 }
2401
2402 $wfh->print("done\n");
2403 $wfh->flush();
2404
2405 } else {
2406 eval { server_wait($config_file, $rfh) };
2407 if ($@) {
2408 warn($@);
2409 exit 1;
2410 }
2411
2412 exit 0;
2413 }
2414
2415 # Stop server
2416 server_stop($pid_file);
2417
2418 $self->assert_child_ok($pid);
2419
2420 if ($ex) {
2421 test_append_logfile($log_file, $ex);
2422 unlink($log_file);
2423
2424 die($ex);
2425 }
2426
2427 unlink($log_file);
2428 }
2429
2430 sub quotatab_stor_ok_user_limit_files_in_exceeded {
2431 my $self = shift;
2432 my $tmpdir = $self->{tmpdir};
2433
2434 my $config_file = "$tmpdir/quotatab.conf";
2435 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2436 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2437
2438 my $log_file = test_get_logfile();
2439
2440 my $user = 'proftpd';
2441 my $passwd = 'test';
2442 my $group = 'ftpd';
2443 my $home_dir = File::Spec->rel2abs($tmpdir);
2444 my $uid = 500;
2445 my $gid = 500;
2446
2447 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2448
2449 # Build up sqlite3 command to create users, groups tables and populate them
2450 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2451
2452 if (open(my $fh, "> $db_script")) {
2453 print $fh <<EOS;
2454 CREATE TABLE users (
2455 userid TEXT PRIMARY KEY,
2456 passwd TEXT,
2457 uid INTEGER,
2458 gid INTEGER,
2459 homedir TEXT,
2460 shell TEXT,
2461 lastdir TEXT
2462 );
2463 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
2464
2465 CREATE TABLE groups (
2466 groupname TEXT PRIMARY KEY,
2467 gid INTEGER,
2468 members TEXT
2469 );
2470 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
2471
2472 CREATE TABLE quotalimits (
2473 name TEXT NOT NULL PRIMARY KEY,
2474 quota_type TEXT NOT NULL,
2475 per_session TEXT NOT NULL,
2476 limit_type TEXT NOT NULL,
2477 bytes_in_avail REAL NOT NULL,
2478 bytes_out_avail REAL NOT NULL,
2479 bytes_xfer_avail REAL NOT NULL,
2480 files_in_avail INTEGER NOT NULL,
2481 files_out_avail INTEGER NOT NULL,
2482 files_xfer_avail INTEGER NOT NULL
2483 );
2484 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, 1, 0, 0);
2485
2486 CREATE TABLE quotatallies (
2487 name TEXT NOT NULL PRIMARY KEY,
2488 quota_type TEXT NOT NULL,
2489 bytes_in_used REAL NOT NULL,
2490 bytes_out_used REAL NOT NULL,
2491 bytes_xfer_used REAL NOT NULL,
2492 files_in_used INTEGER NOT NULL,
2493 files_out_used INTEGER NOT NULL,
2494 files_xfer_used INTEGER NOT NULL
2495 );
2496 EOS
2497
2498 unless (close($fh)) {
2499 die("Can't write $db_script: $!");
2500 }
2501
2502 } else {
2503 die("Can't open $db_script: $!");
2504 }
2505
2506 my $cmd = "sqlite3 $db_file < $db_script";
2507
2508 if ($ENV{TEST_VERBOSE}) {
2509 print STDERR "Executing sqlite3: $cmd\n";
2510 }
2511
2512 my @output = `$cmd`;
2513 if (scalar(@output) &&
2514 $ENV{TEST_VERBOSE}) {
2515 print STDERR "Output: ", join('', @output), "\n";
2516 }
2517
2518 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
2519
2520 my $config = {
2521 PidFile => $pid_file,
2522 ScoreboardFile => $scoreboard_file,
2523 SystemLog => $log_file,
2524
2525 DefaultChdir => '~',
2526
2527 IfModules => {
2528 'mod_delay.c' => {
2529 DelayEngine => 'off',
2530 },
2531
2532 'mod_quotatab_sql.c' => [
2533 '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}\'"',
2534 '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}\'"',
2535 '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',
2536 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2537
2538 'QuotaEngine on',
2539 "QuotaLog $log_file",
2540 'QuotaLimitTable sql:/get-quota-limit',
2541 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2542 ],
2543
2544 'mod_sql.c' => {
2545 SQLAuthTypes => 'plaintext',
2546 SQLBackend => 'sqlite3',
2547 SQLConnectInfo => $db_file,
2548 SQLLogFile => $log_file,
2549 SQLMinID => '0',
2550 },
2551 },
2552 };
2553
2554 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2555
2556 # Open pipes, for use between the parent and child processes. Specifically,
2557 # the child will indicate when it's done with its test by writing a message
2558 # to the parent.
2559 my ($rfh, $wfh);
2560 unless (pipe($rfh, $wfh)) {
2561 die("Can't open pipe: $!");
2562 }
2563
2564 my $ex;
2565
2566 # Fork child
2567 $self->handle_sigchld();
2568 defined(my $pid = fork()) or die("Can't fork: $!");
2569 if ($pid) {
2570 eval {
2571 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2572 $client->login($user, $passwd);
2573
2574 my $conn = $client->stor_raw('test.txt');
2575 unless ($conn) {
2576 die("Failed to STOR test.txt: " . $client->response_code() . " " .
2577 $client->response_msg());
2578 }
2579
2580 my $buf = "Hello, World\n";
2581 $conn->write($buf, length($buf), 25);
2582 eval { $conn->close() };
2583
2584 my $resp_code = $client->response_code();
2585 my $resp_msg = $client->response_msg();
2586
2587 $self->assert_transfer_ok($resp_code, $resp_msg);
2588
2589 $conn = $client->stor_raw('test2.txt');
2590 if ($conn) {
2591 die("STOR test2.txt succeeded unexpectedly");
2592 }
2593
2594 $resp_code = $client->response_code();
2595 $resp_msg = $client->response_msg();
2596
2597 my $expected = 552;
2598 $self->assert($expected == $resp_code,
2599 test_msg("Expected $expected, got $resp_code"));
2600
2601 $expected = 'STOR: notice: quota reached: used \d+ of \d+ upload file';
2602 $self->assert(qr/$expected/, $resp_msg,
2603 test_msg("Expected '$expected', got '$resp_msg'"));
2604 };
2605
2606 if ($@) {
2607 $ex = $@;
2608 }
2609
2610 $wfh->print("done\n");
2611 $wfh->flush();
2612
2613 } else {
2614 eval { server_wait($config_file, $rfh) };
2615 if ($@) {
2616 warn($@);
2617 exit 1;
2618 }
2619
2620 exit 0;
2621 }
2622
2623 # Stop server
2624 server_stop($pid_file);
2625
2626 $self->assert_child_ok($pid);
2627
2628 if ($ex) {
2629 test_append_logfile($log_file, $ex);
2630 unlink($log_file);
2631
2632 die($ex);
2633 }
2634
2635 unlink($log_file);
2636 }
2637
2638 sub quotatab_stor_ok_user_default_with_group_limit {
2639 my $self = shift;
2640 my $tmpdir = $self->{tmpdir};
2641
2642 my $config_file = "$tmpdir/quotatab.conf";
2643 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2644 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2645
2646 my $log_file = test_get_logfile();
2647
2648 my $user1 = 'proftpd';
2649 my $group = 'ftpd';
2650 my $passwd = 'test';
2651 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
2652 mkpath($home_dir1);
2653
2654 my $uid1 = 500;
2655 my $gid = 500;
2656
2657 my $user2 = 'proftpd2';
2658 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
2659 mkpath($home_dir2);
2660
2661 my $uid2 = 1000;
2662
2663 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2664
2665 # Build up sqlite3 command to create users, groups tables and populate them
2666 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2667
2668 if (open(my $fh, "> $db_script")) {
2669 print $fh <<EOS;
2670 CREATE TABLE users (
2671 userid TEXT PRIMARY KEY,
2672 passwd TEXT,
2673 uid INTEGER,
2674 gid INTEGER,
2675 homedir TEXT,
2676 shell TEXT,
2677 lastdir TEXT
2678 );
2679 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
2680 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
2681
2682 CREATE TABLE groups (
2683 groupname TEXT PRIMARY KEY,
2684 gid INTEGER,
2685 members TEXT
2686 );
2687 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user1,$user2');
2688
2689 CREATE TABLE quotalimits (
2690 name TEXT NOT NULL PRIMARY KEY,
2691 quota_type TEXT NOT NULL,
2692 per_session TEXT NOT NULL,
2693 limit_type TEXT NOT NULL,
2694 bytes_in_avail REAL NOT NULL,
2695 bytes_out_avail REAL NOT NULL,
2696 bytes_xfer_avail REAL NOT NULL,
2697 files_in_avail INTEGER NOT NULL,
2698 files_out_avail INTEGER NOT NULL,
2699 files_xfer_avail INTEGER NOT NULL
2700 );
2701 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 ('$group', 'group', 'false', 'soft', 32, 0, 0, 2, 0, 0);
2702
2703 CREATE TABLE quotatallies (
2704 name TEXT NOT NULL PRIMARY KEY,
2705 quota_type TEXT NOT NULL,
2706 bytes_in_used REAL NOT NULL,
2707 bytes_out_used REAL NOT NULL,
2708 bytes_xfer_used REAL NOT NULL,
2709 files_in_used INTEGER NOT NULL,
2710 files_out_used INTEGER NOT NULL,
2711 files_xfer_used INTEGER NOT NULL
2712 );
2713 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 ('$group', 'group', 32, 0, 0, 2, 0, 0);
2714
2715 EOS
2716
2717 unless (close($fh)) {
2718 die("Can't write $db_script: $!");
2719 }
2720
2721 } else {
2722 die("Can't open $db_script: $!");
2723 }
2724
2725 my $cmd = "sqlite3 $db_file < $db_script";
2726
2727 if ($ENV{TEST_VERBOSE}) {
2728 print STDERR "Executing sqlite3: $cmd\n";
2729 }
2730
2731 my @output = `$cmd`;
2732 if (scalar(@output) &&
2733 $ENV{TEST_VERBOSE}) {
2734 print STDERR "Output: ", join('', @output), "\n";
2735 }
2736
2737 my $config = {
2738 PidFile => $pid_file,
2739 ScoreboardFile => $scoreboard_file,
2740 SystemLog => $log_file,
2741
2742 DefaultChdir => '~',
2743
2744 IfModules => {
2745 'mod_delay.c' => {
2746 DelayEngine => 'off',
2747 },
2748
2749 'mod_quotatab_sql.c' => [
2750 '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}\'"',
2751 '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}\'"',
2752 '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',
2753 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2754
2755 'QuotaEngine on',
2756 "QuotaLog $log_file",
2757 'QuotaLimitTable sql:/get-quota-limit',
2758 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2759 'QuotaDefault user false hard 0 1 0 3 0 0',
2760 ],
2761
2762 'mod_sql.c' => {
2763 SQLAuthTypes => 'plaintext',
2764 SQLBackend => 'sqlite3',
2765 SQLConnectInfo => $db_file,
2766 SQLLogFile => $log_file,
2767 SQLMinID => '0',
2768 SQLNamedQuery => 'get-user-info SELECT "userid, passwd, uid, gid, homedir, shell FROM users WHERE userid=\'%U\'"',
2769 SQLUserInfo => 'custom:/get-user-info',
2770 },
2771 },
2772 };
2773
2774 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2775
2776 # Open pipes, for use between the parent and child processes. Specifically,
2777 # the child will indicate when it's done with its test by writing a message
2778 # to the parent.
2779 my ($rfh, $wfh);
2780 unless (pipe($rfh, $wfh)) {
2781 die("Can't open pipe: $!");
2782 }
2783
2784 my $ex;
2785
2786 # Fork child
2787 $self->handle_sigchld();
2788 defined(my $pid = fork()) or die("Can't fork: $!");
2789 if ($pid) {
2790 eval {
2791 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2792
2793 # Login as user1, and upload a file
2794 $client1->login($user1, $passwd);
2795
2796 # These uploads should fail because they encounter the configured
2797 # group limits/tallies in the database, despite the presence of a
2798 # user QuotaDefault in the config; the group limits/tallies should
2799 # take precedence over the config default.
2800
2801 my $conn = $client1->stor_raw('test.txt');
2802 if ($conn) {
2803 die("STOR test.txt succeeded unexpectedly");
2804 }
2805
2806 my $resp_code = $client1->response_code();
2807 my $resp_msg = $client1->response_msg();
2808
2809 my $expected = 552;
2810 $self->assert($expected == $resp_code,
2811 test_msg("Expected response code $expected, got $resp_code"));
2812
2813 ($resp_code, $resp_msg) = $client1->quit();
2814
2815 $expected = 221;
2816 $self->assert($expected == $resp_code,
2817 test_msg("Expected response code $expected, got $resp_code"));
2818
2819 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2820
2821 # Login as user2, and upload a file
2822 $client2->login($user2, $passwd);
2823
2824 $conn = $client2->stor_raw('test.txt');
2825 if ($conn) {
2826 die("STOR test.txt succeeded unexpectedly");
2827 }
2828
2829 $resp_code = $client2->response_code();
2830 $resp_msg = $client2->response_msg();
2831
2832 $expected = 552;
2833 $self->assert($expected == $resp_code,
2834 test_msg("Expected response code $expected, got $resp_code"));
2835
2836 $client2->quit();
2837 };
2838
2839 if ($@) {
2840 $ex = $@;
2841 }
2842
2843 $wfh->print("done\n");
2844 $wfh->flush();
2845
2846 } else {
2847 eval { server_wait($config_file, $rfh) };
2848 if ($@) {
2849 warn($@);
2850 exit 1;
2851 }
2852
2853 exit 0;
2854 }
2855
2856 # Stop server
2857 server_stop($pid_file);
2858
2859 $self->assert_child_ok($pid);
2860
2861 eval {
2862 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 = \'$group\'");
2863
2864 my $expected = 'group';
2865 $self->assert($expected eq $quota_type,
2866 test_msg("Expected '$expected', got '$quota_type'"));
2867
2868 $expected = '^(32.0|32)$';
2869 $self->assert(qr/$expected/, $bytes_in_used,
2870 test_msg("Expected $expected, got $bytes_in_used"));
2871
2872 $expected = '^(0.0|0)$';
2873 $self->assert(qr/$expected/, $bytes_out_used,
2874 test_msg("Expected $expected, got $bytes_out_used"));
2875
2876 $expected = '^(0.0|0)$';
2877 $self->assert(qr/$expected/, $bytes_xfer_used,
2878 test_msg("Expected $expected, got $bytes_xfer_used"));
2879
2880 $expected = 2;
2881 $self->assert($expected == $files_in_used,
2882 test_msg("Expected $expected, got $files_in_used"));
2883
2884 $expected = 0;
2885 $self->assert($expected == $files_out_used,
2886 test_msg("Expected $expected, got $files_out_used"));
2887
2888 $expected = 0;
2889 $self->assert($expected == $files_xfer_used,
2890 test_msg("Expected $expected, got $files_xfer_used"));
2891 };
2892 if ($@) {
2893 $ex = $@;
2894 }
2895
2896 if ($ex) {
2897 test_append_logfile($log_file, $ex);
2898 unlink($log_file);
2899
2900 die($ex);
2901 }
2902
2903 unlink($log_file);
2904 }
2905
2906 sub quotatab_stor_ok_user_default_with_no_group_limit {
2907 my $self = shift;
2908 my $tmpdir = $self->{tmpdir};
2909
2910 my $config_file = "$tmpdir/quotatab.conf";
2911 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
2912 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
2913
2914 my $log_file = test_get_logfile();
2915
2916 my $user1 = 'proftpd';
2917 my $group = 'ftpd';
2918 my $passwd = 'test';
2919 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
2920 mkpath($home_dir1);
2921
2922 my $uid1 = 500;
2923 my $gid = 500;
2924
2925 my $user2 = 'proftpd2';
2926 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
2927 mkpath($home_dir2);
2928
2929 my $uid2 = 1000;
2930
2931 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2932
2933 # Build up sqlite3 command to create users, groups tables and populate them
2934 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2935
2936 if (open(my $fh, "> $db_script")) {
2937 print $fh <<EOS;
2938 CREATE TABLE users (
2939 userid TEXT PRIMARY KEY,
2940 passwd TEXT,
2941 uid INTEGER,
2942 gid INTEGER,
2943 homedir TEXT,
2944 shell TEXT,
2945 lastdir TEXT
2946 );
2947 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
2948 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
2949
2950 CREATE TABLE groups (
2951 groupname TEXT PRIMARY KEY,
2952 gid INTEGER,
2953 members TEXT
2954 );
2955 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user1,$user2');
2956
2957 CREATE TABLE quotalimits (
2958 name TEXT NOT NULL PRIMARY KEY,
2959 quota_type TEXT NOT NULL,
2960 per_session TEXT NOT NULL,
2961 limit_type TEXT NOT NULL,
2962 bytes_in_avail REAL NOT NULL,
2963 bytes_out_avail REAL NOT NULL,
2964 bytes_xfer_avail REAL NOT NULL,
2965 files_in_avail INTEGER NOT NULL,
2966 files_out_avail INTEGER NOT NULL,
2967 files_xfer_avail INTEGER NOT NULL
2968 );
2969
2970 CREATE TABLE quotatallies (
2971 name TEXT NOT NULL PRIMARY KEY,
2972 quota_type TEXT NOT NULL,
2973 bytes_in_used REAL NOT NULL,
2974 bytes_out_used REAL NOT NULL,
2975 bytes_xfer_used REAL NOT NULL,
2976 files_in_used INTEGER NOT NULL,
2977 files_out_used INTEGER NOT NULL,
2978 files_xfer_used INTEGER NOT NULL
2979 );
2980
2981 EOS
2982
2983 unless (close($fh)) {
2984 die("Can't write $db_script: $!");
2985 }
2986
2987 } else {
2988 die("Can't open $db_script: $!");
2989 }
2990
2991 my $cmd = "sqlite3 $db_file < $db_script";
2992
2993 if ($ENV{TEST_VERBOSE}) {
2994 print STDERR "Executing sqlite3: $cmd\n";
2995 }
2996
2997 my @output = `$cmd`;
2998 if (scalar(@output) &&
2999 $ENV{TEST_VERBOSE}) {
3000 print STDERR "Output: ", join('', @output), "\n";
3001 }
3002
3003 my $config = {
3004 PidFile => $pid_file,
3005 ScoreboardFile => $scoreboard_file,
3006 SystemLog => $log_file,
3007
3008 DefaultChdir => '~',
3009
3010 IfModules => {
3011 'mod_delay.c' => {
3012 DelayEngine => 'off',
3013 },
3014
3015 'mod_quotatab_sql.c' => [
3016 '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}\'"',
3017 '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}\'"',
3018 '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',
3019 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
3020
3021 'QuotaEngine on',
3022 "QuotaLog $log_file",
3023 'QuotaLimitTable sql:/get-quota-limit',
3024 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
3025 'QuotaDefault user false hard 0 1 0 3 0 0',
3026 ],
3027
3028 'mod_sql.c' => {
3029 SQLAuthTypes => 'plaintext',
3030 SQLBackend => 'sqlite3',
3031 SQLConnectInfo => $db_file,
3032 SQLLogFile => $log_file,
3033 SQLMinID => '0',
3034 SQLNamedQuery => 'get-user-info SELECT "userid, passwd, uid, gid, homedir, shell FROM users WHERE userid=\'%U\'"',
3035 SQLUserInfo => 'custom:/get-user-info',
3036 },
3037 },
3038 };
3039
3040 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3041
3042 # Open pipes, for use between the parent and child processes. Specifically,
3043 # the child will indicate when it's done with its test by writing a message
3044 # to the parent.
3045 my ($rfh, $wfh);
3046 unless (pipe($rfh, $wfh)) {
3047 die("Can't open pipe: $!");
3048 }
3049
3050 my $ex;
3051
3052 # Fork child
3053 $self->handle_sigchld();
3054 defined(my $pid = fork()) or die("Can't fork: $!");
3055 if ($pid) {
3056 eval {
3057 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
3058
3059 # Login as user1, and upload a file
3060 $client1->login($user1, $passwd);
3061
3062 # These uploads should fail because they encounter the configured
3063 # group limits/tallies in the database, despite the presence of a
3064 # user QuotaDefault in the config; the group limits/tallies should
3065 # take precedence over the config default.
3066
3067 my $conn = $client1->stor_raw('test.txt');
3068 unless ($conn) {
3069 die("STOR test.txt failed: " . $client1->response_code() . " " .
3070 $client1->response_msg());
3071 }
3072
3073 my $buf = "Hello, World\n";
3074 $conn->write($buf, length($buf), 25);
3075 eval { $conn->close() };
3076
3077 my $resp_code = $client1->response_code();
3078 my $resp_msg = $client1->response_msg();
3079 $self->assert_transfer_ok($resp_code, $resp_msg);
3080
3081 ($resp_code, $resp_msg) = $client1->quit();
3082
3083 my $expected = 221;
3084 $self->assert($expected == $resp_code,
3085 test_msg("Expected response code $expected, got $resp_code"));
3086
3087 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
3088
3089 # Login as user2, and upload a file
3090 $client2->login($user2, $passwd);
3091
3092 $conn = $client2->stor_raw('test.txt');
3093 unless ($conn) {
3094 die("STOR test.txt failed: " . $client2->response_code() . " " .
3095 $client2->response_msg());
3096 }
3097
3098 $buf = "Hello, World\n";
3099 $conn->write($buf, length($buf), 25);
3100 eval { $conn->close() };
3101
3102 $resp_code = $client2->response_code();
3103 $resp_msg = $client2->response_msg();
3104 $self->assert_transfer_ok($resp_code, $resp_msg);
3105
3106 $client2->quit();
3107 };
3108 if ($@) {
3109 $ex = $@;
3110 }
3111
3112 $wfh->print("done\n");
3113 $wfh->flush();
3114
3115 } else {
3116 eval { server_wait($config_file, $rfh) };
3117 if ($@) {
3118 warn($@);
3119 exit 1;
3120 }
3121
3122 exit 0;
3123 }
3124
3125 # Stop server
3126 server_stop($pid_file);
3127
3128 $self->assert_child_ok($pid);
3129
3130 eval {
3131 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 = \'$user1\'");
3132
3133 my $expected = 'user';
3134 $self->assert($expected eq $quota_type,
3135 test_msg("Expected '$expected', got '$quota_type'"));
3136
3137 $expected = '^(0.0|0)$';
3138 $self->assert(qr/$expected/, $bytes_in_used,
3139 test_msg("Expected $expected, got $bytes_in_used"));
3140
3141 $expected = '^(0.0|0)$';
3142 $self->assert(qr/$expected/, $bytes_out_used,
3143 test_msg("Expected $expected, got $bytes_out_used"));
3144
3145 $expected = '^(0.0|0)$';
3146 $self->assert(qr/$expected/, $bytes_xfer_used,
3147 test_msg("Expected $expected, got $bytes_xfer_used"));
3148
3149 $expected = 1;
3150 $self->assert($expected == $files_in_used,
3151 test_msg("Expected $expected, got $files_in_used"));
3152
3153 $expected = 0;
3154 $self->assert($expected == $files_out_used,
3155 test_msg("Expected $expected, got $files_out_used"));
3156
3157 $expected = 0;
3158 $self->assert($expected == $files_xfer_used,
3159 test_msg("Expected $expected, got $files_xfer_used"));
3160 };
3161 if ($@) {
3162 $ex = $@;
3163 }
3164
3165 if ($ex) {
3166 test_append_logfile($log_file, $ex);
3167 unlink($log_file);
3168
3169 die($ex);
3170 }
3171
3172 unlink($log_file);
3173 }
3174
3175 sub quotatab_stor_ok_group_limit {
3176 my $self = shift;
3177 my $tmpdir = $self->{tmpdir};
3178
3179 my $config_file = "$tmpdir/quotatab.conf";
3180 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
3181 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
3182
3183 my $log_file = test_get_logfile();
3184
3185 my $user1 = 'proftpd';
3186 my $group = 'ftpd';
3187 my $passwd = 'test';
3188 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
3189 mkpath($home_dir1);
3190
3191 my $uid1 = 500;
3192 my $gid = 500;
3193
3194 my $user2 = 'proftpd2';
3195 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
3196 mkpath($home_dir2);
3197
3198 my $uid2 = 1000;
3199
3200 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3201
3202 # Build up sqlite3 command to create users, groups tables and populate them
3203 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3204
3205 if (open(my $fh, "> $db_script")) {
3206 print $fh <<EOS;
3207 CREATE TABLE users (
3208 userid TEXT PRIMARY KEY,
3209 passwd TEXT,
3210 uid INTEGER,
3211 gid INTEGER,
3212 homedir TEXT,
3213 shell TEXT,
3214 lastdir TEXT
3215 );
3216 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
3217 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
3218
3219 CREATE TABLE groups (
3220 groupname TEXT PRIMARY KEY,
3221 gid INTEGER,
3222 members TEXT
3223 );
3224 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user1,$user2');
3225
3226 CREATE TABLE quotalimits (
3227 name TEXT NOT NULL PRIMARY KEY,
3228 quota_type TEXT NOT NULL,
3229 per_session TEXT NOT NULL,
3230 limit_type TEXT NOT NULL,
3231 bytes_in_avail REAL NOT NULL,
3232 bytes_out_avail REAL NOT NULL,
3233 bytes_xfer_avail REAL NOT NULL,
3234 files_in_avail INTEGER NOT NULL,
3235 files_out_avail INTEGER NOT NULL,
3236 files_xfer_avail INTEGER NOT NULL
3237 );
3238 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 ('$group', 'group', 'false', 'soft', 32, 0, 0, 2, 0, 0);
3239
3240 CREATE TABLE quotatallies (
3241 name TEXT NOT NULL PRIMARY KEY,
3242 quota_type TEXT NOT NULL,
3243 bytes_in_used REAL NOT NULL,
3244 bytes_out_used REAL NOT NULL,
3245 bytes_xfer_used REAL NOT NULL,
3246 files_in_used INTEGER NOT NULL,
3247 files_out_used INTEGER NOT NULL,
3248 files_xfer_used INTEGER NOT NULL
3249 );
3250 EOS
3251
3252 unless (close($fh)) {
3253 die("Can't write $db_script: $!");
3254 }
3255
3256 } else {
3257 die("Can't open $db_script: $!");
3258 }
3259
3260 my $cmd = "sqlite3 $db_file < $db_script";
3261
3262 if ($ENV{TEST_VERBOSE}) {
3263 print STDERR "Executing sqlite3: $cmd\n";
3264 }
3265
3266 my @output = `$cmd`;
3267 if (scalar(@output) &&
3268 $ENV{TEST_VERBOSE}) {
3269 print STDERR "Output: ", join('', @output), "\n";
3270 }
3271
3272 my $config = {
3273 PidFile => $pid_file,
3274 ScoreboardFile => $scoreboard_file,
3275 SystemLog => $log_file,
3276
3277 DefaultChdir => '~',
3278
3279 IfModules => {
3280 'mod_delay.c' => {
3281 DelayEngine => 'off',
3282 },
3283
3284 'mod_quotatab_sql.c' => [
3285 '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}\'"',
3286 '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}\'"',
3287 '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',
3288 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
3289
3290 'QuotaEngine on',
3291 "QuotaLog $log_file",
3292 'QuotaLimitTable sql:/get-quota-limit',
3293 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
3294 ],
3295
3296 'mod_sql.c' => {
3297 SQLAuthTypes => 'plaintext',
3298 SQLBackend => 'sqlite3',
3299 SQLConnectInfo => $db_file,
3300 SQLLogFile => $log_file,
3301 SQLMinID => '0',
3302 SQLNamedQuery => 'get-user-info SELECT "userid, passwd, uid, gid, homedir, shell FROM users WHERE userid=\'%U\'"',
3303 SQLUserInfo => 'custom:/get-user-info',
3304 },
3305 },
3306 };
3307
3308 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3309
3310 # Open pipes, for use between the parent and child processes. Specifically,
3311 # the child will indicate when it's done with its test by writing a message
3312 # to the parent.
3313 my ($rfh, $wfh);
3314 unless (pipe($rfh, $wfh)) {
3315 die("Can't open pipe: $!");
3316 }
3317
3318 my $ex;
3319
3320 # Fork child
3321 $self->handle_sigchld();
3322 defined(my $pid = fork()) or die("Can't fork: $!");
3323 if ($pid) {
3324 eval {
3325 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3326
3327 # Login as user1, and upload a file
3328 $client->login($user1, $passwd);
3329
3330 my $conn = $client->stor_raw('test.txt');
3331 unless ($conn) {
3332 die("Failed to STOR test.txt: " . $client->response_code() . " " .
3333 $client->response_msg());
3334 }
3335
3336 my $buf = "Hello, World\n";
3337 $conn->write($buf, length($buf), 25);
3338 eval { $conn->close() };
3339
3340 my $resp_code = $client->response_code();
3341 my $resp_msg = $client->response_msg();
3342
3343 $self->assert_transfer_ok($resp_code, $resp_msg);
3344 $client->quit();
3345
3346 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3347
3348 # Login as user2, and upload a file
3349 $client->login($user2, $passwd);
3350
3351 $conn = $client->stor_raw('test.txt');
3352 unless ($conn) {
3353 die("Failed to STOR test.txt: " . $client->response_code() . " " .
3354 $client->response_msg());
3355 }
3356
3357 $buf = "Hello, World\n";
3358 $conn->write($buf, length($buf), 25);
3359 eval { $conn->close() };
3360
3361 $resp_code = $client->response_code();
3362 $resp_msg = $client->response_msg();
3363
3364 $self->assert_transfer_ok($resp_code, $resp_msg);
3365 $client->quit();
3366 };
3367
3368 if ($@) {
3369 $ex = $@;
3370 }
3371
3372 $wfh->print("done\n");
3373 $wfh->flush();
3374
3375 } else {
3376 eval { server_wait($config_file, $rfh) };
3377 if ($@) {
3378 warn($@);
3379 exit 1;
3380 }
3381
3382 exit 0;
3383 }
3384
3385 # Stop server
3386 server_stop($pid_file);
3387
3388 $self->assert_child_ok($pid);
3389
3390 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 = \'$group\'");
3391
3392 my $expected;
3393
3394 $expected = 'group';
3395 $self->assert($expected eq $quota_type,
3396 test_msg("Expected '$expected', got '$quota_type'"));
3397
3398 $expected = '^(26.0|26)$';
3399 $self->assert(qr/$expected/, $bytes_in_used,
3400 test_msg("Expected $expected, got $bytes_in_used"));
3401
3402 $expected = '^(0.0|0)$';
3403 $self->assert(qr/$expected/, $bytes_out_used,
3404 test_msg("Expected $expected, got $bytes_out_used"));
3405
3406 $expected = '^(0.0|0)$';
3407 $self->assert(qr/$expected/, $bytes_xfer_used,
3408 test_msg("Expected $expected, got $bytes_xfer_used"));
3409
3410 $expected = 2;
3411 $self->assert($expected == $files_in_used,
3412 test_msg("Expected $expected, got $files_in_used"));
3413
3414 $expected = 0;
3415 $self->assert($expected == $files_out_used,
3416 test_msg("Expected $expected, got $files_out_used"));
3417
3418 $expected = 0;
3419 $self->assert($expected == $files_xfer_used,
3420 test_msg("Expected $expected, got $files_xfer_used"));
3421
3422 if ($ex) {
3423 test_append_logfile($log_file, $ex);
3424 unlink($log_file);
3425
3426 die($ex);
3427 }
3428
3429 unlink($log_file);
3430 }
3431
3432 sub quotatab_stor_ok_group_limit_bytes_in_exceeded_soft_limit {
3433 my $self = shift;
3434 my $tmpdir = $self->{tmpdir};
3435
3436 my $config_file = "$tmpdir/quotatab.conf";
3437 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
3438 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
3439
3440 my $log_file = test_get_logfile();
3441
3442 my $user1 = 'proftpd';
3443 my $group = 'ftpd';
3444 my $passwd = 'test';
3445 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
3446 mkpath($home_dir1);
3447
3448 my $uid1 = 500;
3449 my $gid = 500;
3450
3451 my $user2 = 'proftpd2';
3452 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
3453 mkpath($home_dir2);
3454
3455 my $uid2 = 1000;
3456
3457 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3458
3459 # Build up sqlite3 command to create users, groups tables and populate them
3460 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3461
3462 if (open(my $fh, "> $db_script")) {
3463 print $fh <<EOS;
3464 CREATE TABLE users (
3465 userid TEXT PRIMARY KEY,
3466 passwd TEXT,
3467 uid INTEGER,
3468 gid INTEGER,
3469 homedir TEXT,
3470 shell TEXT,
3471 lastdir TEXT
3472 );
3473 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
3474 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
3475
3476 CREATE TABLE groups (
3477 groupname TEXT PRIMARY KEY,
3478 gid INTEGER,
3479 members TEXT
3480 );
3481 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user1,$user2');
3482
3483 CREATE TABLE quotalimits (
3484 name TEXT NOT NULL PRIMARY KEY,
3485 quota_type TEXT NOT NULL,
3486 per_session TEXT NOT NULL,
3487 limit_type TEXT NOT NULL,
3488 bytes_in_avail REAL NOT NULL,
3489 bytes_out_avail REAL NOT NULL,
3490 bytes_xfer_avail REAL NOT NULL,
3491 files_in_avail INTEGER NOT NULL,
3492 files_out_avail INTEGER NOT NULL,
3493 files_xfer_avail INTEGER NOT NULL
3494 );
3495 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 ('$group', 'group', 'false', 'soft', 5, 0, 0, 3, 0, 0);
3496
3497 CREATE TABLE quotatallies (
3498 name TEXT NOT NULL PRIMARY KEY,
3499 quota_type TEXT NOT NULL,
3500 bytes_in_used REAL NOT NULL,
3501 bytes_out_used REAL NOT NULL,
3502 bytes_xfer_used REAL NOT NULL,
3503 files_in_used INTEGER NOT NULL,
3504 files_out_used INTEGER NOT NULL,
3505 files_xfer_used INTEGER NOT NULL
3506 );
3507 EOS
3508
3509 unless (close($fh)) {
3510 die("Can't write $db_script: $!");
3511 }
3512
3513 } else {
3514 die("Can't open $db_script: $!");
3515 }
3516
3517 my $cmd = "sqlite3 $db_file < $db_script";
3518
3519 if ($ENV{TEST_VERBOSE}) {
3520 print STDERR "Executing sqlite3: $cmd\n";
3521 }
3522
3523 my @output = `$cmd`;
3524 if (scalar(@output) &&
3525 $ENV{TEST_VERBOSE}) {
3526 print STDERR "Output: ", join('', @output), "\n";
3527 }
3528
3529 my $config = {
3530 PidFile => $pid_file,
3531 ScoreboardFile => $scoreboard_file,
3532 SystemLog => $log_file,
3533
3534 DefaultChdir => '~',
3535
3536 IfModules => {
3537 'mod_delay.c' => {
3538 DelayEngine => 'off',
3539 },
3540
3541 'mod_quotatab_sql.c' => [
3542 '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}\'"',
3543 '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}\'"',
3544 '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',
3545 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
3546
3547 'QuotaEngine on',
3548 "QuotaLog $log_file",
3549 'QuotaLimitTable sql:/get-quota-limit',
3550 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
3551 ],
3552
3553 'mod_sql.c' => {
3554 SQLAuthTypes => 'plaintext',
3555 SQLBackend => 'sqlite3',
3556 SQLConnectInfo => $db_file,
3557 SQLLogFile => $log_file,
3558 SQLMinID => '0',
3559 },
3560 },
3561 };
3562
3563 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3564
3565 # Open pipes, for use between the parent and child processes. Specifically,
3566 # the child will indicate when it's done with its test by writing a message
3567 # to the parent.
3568 my ($rfh, $wfh);
3569 unless (pipe($rfh, $wfh)) {
3570 die("Can't open pipe: $!");
3571 }
3572
3573 my $ex;
3574
3575 # Fork child
3576 $self->handle_sigchld();
3577 defined(my $pid = fork()) or die("Can't fork: $!");
3578 if ($pid) {
3579 eval {
3580 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3581
3582 # Login as user1, and upload a file
3583 $client->login($user1, $passwd);
3584
3585 my $conn = $client->stor_raw('test.txt');
3586 unless ($conn) {
3587 die("Failed to STOR test.txt: " . $client->response_code() . " " .
3588 $client->response_msg());
3589 }
3590
3591 my $buf = "Hello, World\n";
3592 $conn->write($buf, length($buf), 25);
3593 eval { $conn->close() };
3594
3595 my $resp_code = $client->response_code();
3596 my $resp_msg = $client->response_msg();
3597
3598 $self->assert_transfer_ok($resp_code, $resp_msg);
3599 $client->quit();
3600
3601 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3602
3603 # Login as user2, and upload a file
3604 $client->login($user2, $passwd);
3605
3606 $conn = $client->stor_raw('test.txt');
3607 if ($conn) {
3608 die("STOR test.txt succeeded unexpectedly");
3609 }
3610
3611 $resp_code = $client->response_code();
3612 $resp_msg = $client->response_msg();
3613
3614 my $expected = 552;
3615 $self->assert($expected == $resp_code,
3616 test_msg("Expected $expected, got $resp_code"));
3617
3618 $expected = 'STOR denied: quota exceeded: used \S+ of \S+ upload bytes';
3619 $self->assert(qr/$expected/, $resp_msg,
3620 test_msg("Expected '$expected', got '$resp_msg'"));
3621
3622 $client->quit();
3623 };
3624
3625 if ($@) {
3626 $ex = $@;
3627 }
3628
3629 $wfh->print("done\n");
3630 $wfh->flush();
3631
3632 } else {
3633 eval { server_wait($config_file, $rfh) };
3634 if ($@) {
3635 warn($@);
3636 exit 1;
3637 }
3638
3639 exit 0;
3640 }
3641
3642 # Stop server
3643 server_stop($pid_file);
3644
3645 $self->assert_child_ok($pid);
3646
3647 if ($ex) {
3648 test_append_logfile($log_file, $ex);
3649 unlink($log_file);
3650
3651 die($ex);
3652 }
3653
3654 unlink($log_file);
3655 }
3656
3657 sub quotatab_stor_ok_group_limit_bytes_in_exceeded_hard_limit {
3658 my $self = shift;
3659 my $tmpdir = $self->{tmpdir};
3660
3661 my $config_file = "$tmpdir/quotatab.conf";
3662 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
3663 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
3664
3665 my $log_file = test_get_logfile();
3666
3667 my $user1 = 'proftpd';
3668 my $group = 'ftpd';
3669 my $passwd = 'test';
3670 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
3671 mkpath($home_dir1);
3672
3673 my $uid1 = 500;
3674 my $gid = 500;
3675
3676 my $user2 = 'proftpd2';
3677 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
3678 mkpath($home_dir2);
3679
3680 my $uid2 = 1000;
3681
3682 my $test_file = File::Spec->rel2abs("$tmpdir/bar/test.txt");
3683
3684 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3685
3686 # Build up sqlite3 command to create users, groups tables and populate them
3687 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3688
3689 if (open(my $fh, "> $db_script")) {
3690 print $fh <<EOS;
3691 CREATE TABLE users (
3692 userid TEXT PRIMARY KEY,
3693 passwd TEXT,
3694 uid INTEGER,
3695 gid INTEGER,
3696 homedir TEXT,
3697 shell TEXT,
3698 lastdir TEXT
3699 );
3700 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
3701 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
3702
3703 CREATE TABLE groups (
3704 groupname TEXT PRIMARY KEY,
3705 gid INTEGER,
3706 members TEXT
3707 );
3708 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user1,$user2');
3709
3710 CREATE TABLE quotalimits (
3711 name TEXT NOT NULL PRIMARY KEY,
3712 quota_type TEXT NOT NULL,
3713 per_session TEXT NOT NULL,
3714 limit_type TEXT NOT NULL,
3715 bytes_in_avail REAL NOT NULL,
3716 bytes_out_avail REAL NOT NULL,
3717 bytes_xfer_avail REAL NOT NULL,
3718 files_in_avail INTEGER NOT NULL,
3719 files_out_avail INTEGER NOT NULL,
3720 files_xfer_avail INTEGER NOT NULL
3721 );
3722 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 ('$group', 'group', 'false', 'hard', 20, 0, 0, 3, 0, 0);
3723
3724 CREATE TABLE quotatallies (
3725 name TEXT NOT NULL PRIMARY KEY,
3726 quota_type TEXT NOT NULL,
3727 bytes_in_used REAL NOT NULL,
3728 bytes_out_used REAL NOT NULL,
3729 bytes_xfer_used REAL NOT NULL,
3730 files_in_used INTEGER NOT NULL,
3731 files_out_used INTEGER NOT NULL,
3732 files_xfer_used INTEGER NOT NULL
3733 );
3734 EOS
3735
3736 unless (close($fh)) {
3737 die("Can't write $db_script: $!");
3738 }
3739
3740 } else {
3741 die("Can't open $db_script: $!");
3742 }
3743
3744 my $cmd = "sqlite3 $db_file < $db_script";
3745
3746 if ($ENV{TEST_VERBOSE}) {
3747 print STDERR "Executing sqlite3: $cmd\n";
3748 }
3749
3750 my @output = `$cmd`;
3751 if (scalar(@output) &&
3752 $ENV{TEST_VERBOSE}) {
3753 print STDERR "Output: ", join('', @output), "\n";
3754 }
3755
3756 my $config = {
3757 PidFile => $pid_file,
3758 ScoreboardFile => $scoreboard_file,
3759 SystemLog => $log_file,
3760
3761 DefaultChdir => '~',
3762
3763 IfModules => {
3764 'mod_delay.c' => {
3765 DelayEngine => 'off',
3766 },
3767
3768 'mod_quotatab_sql.c' => [
3769 '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}\'"',
3770 '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}\'"',
3771 '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',
3772 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
3773
3774 'QuotaEngine on',
3775 "QuotaLog $log_file",
3776 'QuotaLimitTable sql:/get-quota-limit',
3777 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
3778 ],
3779
3780 'mod_sql.c' => {
3781 SQLAuthTypes => 'plaintext',
3782 SQLBackend => 'sqlite3',
3783 SQLConnectInfo => $db_file,
3784 SQLLogFile => $log_file,
3785 SQLMinID => '0',
3786 },
3787 },
3788 };
3789
3790 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3791
3792 # Open pipes, for use between the parent and child processes. Specifically,
3793 # the child will indicate when it's done with its test by writing a message
3794 # to the parent.
3795 my ($rfh, $wfh);
3796 unless (pipe($rfh, $wfh)) {
3797 die("Can't open pipe: $!");
3798 }
3799
3800 my $ex;
3801
3802 # Fork child
3803 $self->handle_sigchld();
3804 defined(my $pid = fork()) or die("Can't fork: $!");
3805 if ($pid) {
3806 eval {
3807 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3808
3809 # Login as user1, and upload a file
3810 $client->login($user1, $passwd);
3811
3812 my $conn = $client->stor_raw('test.txt');
3813 unless ($conn) {
3814 die("Failed to STOR test.txt: " . $client->response_code() . " " .
3815 $client->response_msg());
3816 }
3817
3818 my $buf = "Hello, World\n";
3819 $conn->write($buf, length($buf), 25);
3820 eval { $conn->close() };
3821
3822 my $resp_code = $client->response_code();
3823 my $resp_msg = $client->response_msg();
3824
3825 $self->assert_transfer_ok($resp_code, $resp_msg);
3826 $client->quit();
3827
3828 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3829
3830 # Login as user2, and upload a file
3831 $client->login($user2, $passwd);
3832
3833 $conn = $client->stor_raw('test.txt');
3834 unless ($conn) {
3835 die("Failed to STOR test.txt: " . $client->response_code() . " " .
3836 $client->response_msg());
3837 }
3838
3839 $buf = "Hello, World\n";
3840 $conn->write($buf, length($buf), 25);
3841 eval { $conn->close() };
3842
3843 $resp_code = $client->response_code();
3844 $resp_msg = $client->response_msg();
3845
3846 my $expected = 552;
3847 $self->assert($expected == $resp_code,
3848 test_msg("Expected $expected, got $resp_code"));
3849
3850 $expected = 'Transfer aborted. (Disc|Disk) quota exceeded';
3851 $self->assert(qr/$expected/, $resp_msg,
3852 test_msg("Expected '$expected', got '$resp_msg'"));
3853
3854 $client->quit();
3855
3856 if (-f $test_file) {
3857 die("$test_file exists, should have been deleted");
3858 }
3859 };
3860
3861 if ($@) {
3862 $ex = $@;
3863 }
3864
3865 $wfh->print("done\n");
3866 $wfh->flush();
3867
3868 } else {
3869 eval { server_wait($config_file, $rfh) };
3870 if ($@) {
3871 warn($@);
3872 exit 1;
3873 }
3874
3875 exit 0;
3876 }
3877
3878 # Stop server
3879 server_stop($pid_file);
3880
3881 $self->assert_child_ok($pid);
3882
3883 if ($ex) {
3884 test_append_logfile($log_file, $ex);
3885 unlink($log_file);
3886
3887 die($ex);
3888 }
3889
3890 unlink($log_file);
3891 }
3892
3893 sub quotatab_stor_ok_group_limit_files_in_exceeded {
3894 my $self = shift;
3895 my $tmpdir = $self->{tmpdir};
3896
3897 my $config_file = "$tmpdir/quotatab.conf";
3898 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
3899 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
3900
3901 my $log_file = test_get_logfile();
3902
3903 my $user1 = 'proftpd';
3904 my $group = 'ftpd';
3905 my $passwd = 'test';
3906 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
3907 mkpath($home_dir1);
3908
3909 my $uid1 = 500;
3910 my $gid = 500;
3911
3912 my $user2 = 'proftpd2';
3913 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
3914 mkpath($home_dir2);
3915
3916 my $uid2 = 1000;
3917
3918 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3919
3920 # Build up sqlite3 command to create users, groups tables and populate them
3921 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3922
3923 if (open(my $fh, "> $db_script")) {
3924 print $fh <<EOS;
3925 CREATE TABLE users (
3926 userid TEXT PRIMARY KEY,
3927 passwd TEXT,
3928 uid INTEGER,
3929 gid INTEGER,
3930 homedir TEXT,
3931 shell TEXT,
3932 lastdir TEXT
3933 );
3934 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
3935 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
3936
3937 CREATE TABLE groups (
3938 groupname TEXT PRIMARY KEY,
3939 gid INTEGER,
3940 members TEXT
3941 );
3942 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user1,$user2');
3943
3944 CREATE TABLE quotalimits (
3945 name TEXT NOT NULL PRIMARY KEY,
3946 quota_type TEXT NOT NULL,
3947 per_session TEXT NOT NULL,
3948 limit_type TEXT NOT NULL,
3949 bytes_in_avail REAL NOT NULL,
3950 bytes_out_avail REAL NOT NULL,
3951 bytes_xfer_avail REAL NOT NULL,
3952 files_in_avail INTEGER NOT NULL,
3953 files_out_avail INTEGER NOT NULL,
3954 files_xfer_avail INTEGER NOT NULL
3955 );
3956 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 ('$group', 'group', 'false', 'soft', 32, 0, 0, 1, 0, 0);
3957
3958 CREATE TABLE quotatallies (
3959 name TEXT NOT NULL PRIMARY KEY,
3960 quota_type TEXT NOT NULL,
3961 bytes_in_used REAL NOT NULL,
3962 bytes_out_used REAL NOT NULL,
3963 bytes_xfer_used REAL NOT NULL,
3964 files_in_used INTEGER NOT NULL,
3965 files_out_used INTEGER NOT NULL,
3966 files_xfer_used INTEGER NOT NULL
3967 );
3968 EOS
3969
3970 unless (close($fh)) {
3971 die("Can't write $db_script: $!");
3972 }
3973
3974 } else {
3975 die("Can't open $db_script: $!");
3976 }
3977
3978 my $cmd = "sqlite3 $db_file < $db_script";
3979
3980 if ($ENV{TEST_VERBOSE}) {
3981 print STDERR "Executing sqlite3: $cmd\n";
3982 }
3983
3984 my @output = `$cmd`;
3985 if (scalar(@output) &&
3986 $ENV{TEST_VERBOSE}) {
3987 print STDERR "Output: ", join('', @output), "\n";
3988 }
3989
3990 my $config = {
3991 PidFile => $pid_file,
3992 ScoreboardFile => $scoreboard_file,
3993 SystemLog => $log_file,
3994
3995 DefaultChdir => '~',
3996
3997 IfModules => {
3998 'mod_delay.c' => {
3999 DelayEngine => 'off',
4000 },
4001
4002 'mod_quotatab_sql.c' => [
4003 '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}\'"',
4004 '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}\'"',
4005 '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',
4006 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
4007
4008 'QuotaEngine on',
4009 "QuotaLog $log_file",
4010 'QuotaLimitTable sql:/get-quota-limit',
4011 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
4012 ],
4013
4014 'mod_sql.c' => {
4015 SQLAuthTypes => 'plaintext',
4016 SQLBackend => 'sqlite3',
4017 SQLConnectInfo => $db_file,
4018 SQLLogFile => $log_file,
4019 SQLMinID => '0',
4020 },
4021 },
4022 };
4023
4024 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4025
4026 # Open pipes, for use between the parent and child processes. Specifically,
4027 # the child will indicate when it's done with its test by writing a message
4028 # to the parent.
4029 my ($rfh, $wfh);
4030 unless (pipe($rfh, $wfh)) {
4031 die("Can't open pipe: $!");
4032 }
4033
4034 my $ex;
4035
4036 # Fork child
4037 $self->handle_sigchld();
4038 defined(my $pid = fork()) or die("Can't fork: $!");
4039 if ($pid) {
4040 eval {
4041 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4042
4043 # Login as user1, and upload a file
4044 $client->login($user1, $passwd);
4045
4046 my $conn = $client->stor_raw('test.txt');
4047 unless ($conn) {
4048 die("Failed to STOR test.txt: " . $client->response_code() . " " .
4049 $client->response_msg());
4050 }
4051
4052 my $buf = "Hello, World\n";
4053 $conn->write($buf, length($buf), 25);
4054 eval { $conn->close() };
4055
4056 my $resp_code = $client->response_code();
4057 my $resp_msg = $client->response_msg();
4058
4059 $self->assert_transfer_ok($resp_code, $resp_msg);
4060 $client->quit();
4061
4062 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4063
4064 # Login as user2, and upload a file
4065 $client->login($user2, $passwd);
4066
4067 $conn = $client->stor_raw('test2.txt');
4068 if ($conn) {
4069 die("STOR test2.txt succeeded unexpectedly");
4070 }
4071
4072 $resp_code = $client->response_code();
4073 $resp_msg = $client->response_msg();
4074
4075 my $expected = 552;
4076 $self->assert($expected == $resp_code,
4077 test_msg("Expected $expected, got $resp_code"));
4078
4079 $expected = 'STOR: notice: quota reached: used \d+ of \d+ upload file';
4080 $self->assert(qr/$expected/, $resp_msg,
4081 test_msg("Expected '$expected', got '$resp_msg'"));
4082 };
4083
4084 if ($@) {
4085 $ex = $@;
4086 }
4087
4088 $wfh->print("done\n");
4089 $wfh->flush();
4090
4091 } else {
4092 eval { server_wait($config_file, $rfh) };
4093 if ($@) {
4094 warn($@);
4095 exit 1;
4096 }
4097
4098 exit 0;
4099 }
4100
4101 # Stop server
4102 server_stop($pid_file);
4103
4104 $self->assert_child_ok($pid);
4105
4106 if ($ex) {
4107 test_append_logfile($log_file, $ex);
4108 unlink($log_file);
4109
4110 die($ex);
4111 }
4112
4113 unlink($log_file);
4114 }
4115
4116 sub quotatab_stor_ok_group_limit_with_default {
4117 my $self = shift;
4118 my $tmpdir = $self->{tmpdir};
4119
4120 my $config_file = "$tmpdir/quotatab.conf";
4121 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
4122 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
4123
4124 my $log_file = test_get_logfile();
4125
4126 my $user1 = 'proftpd';
4127 my $group = 'ftpd';
4128 my $passwd = 'test';
4129 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
4130 mkpath($home_dir1);
4131
4132 my $uid1 = 500;
4133 my $gid = 500;
4134
4135 my $user2 = 'proftpd2';
4136 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
4137 mkpath($home_dir2);
4138
4139 my $uid2 = 1000;
4140
4141 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4142
4143 # Build up sqlite3 command to create users, groups tables and populate them
4144 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4145
4146 if (open(my $fh, "> $db_script")) {
4147 print $fh <<EOS;
4148 CREATE TABLE users (
4149 userid TEXT PRIMARY KEY,
4150 passwd TEXT,
4151 uid INTEGER,
4152 gid INTEGER,
4153 homedir TEXT,
4154 shell TEXT,
4155 lastdir TEXT
4156 );
4157 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
4158 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
4159
4160 CREATE TABLE groups (
4161 groupname TEXT PRIMARY KEY,
4162 gid INTEGER,
4163 members TEXT
4164 );
4165 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user1,$user2');
4166
4167 CREATE TABLE quotalimits (
4168 name TEXT NOT NULL PRIMARY KEY,
4169 quota_type TEXT NOT NULL,
4170 per_session TEXT NOT NULL,
4171 limit_type TEXT NOT NULL,
4172 bytes_in_avail REAL NOT NULL,
4173 bytes_out_avail REAL NOT NULL,
4174 bytes_xfer_avail REAL NOT NULL,
4175 files_in_avail INTEGER NOT NULL,
4176 files_out_avail INTEGER NOT NULL,
4177 files_xfer_avail INTEGER NOT NULL
4178 );
4179 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 ('$group', 'group', 'false', 'soft', 32, 0, 0, 2, 0, 0);
4180
4181 CREATE TABLE quotatallies (
4182 name TEXT NOT NULL PRIMARY KEY,
4183 quota_type TEXT NOT NULL,
4184 bytes_in_used REAL NOT NULL,
4185 bytes_out_used REAL NOT NULL,
4186 bytes_xfer_used REAL NOT NULL,
4187 files_in_used INTEGER NOT NULL,
4188 files_out_used INTEGER NOT NULL,
4189 files_xfer_used INTEGER NOT NULL
4190 );
4191 EOS
4192
4193 unless (close($fh)) {
4194 die("Can't write $db_script: $!");
4195 }
4196
4197 } else {
4198 die("Can't open $db_script: $!");
4199 }
4200
4201 my $cmd = "sqlite3 $db_file < $db_script";
4202
4203 if ($ENV{TEST_VERBOSE}) {
4204 print STDERR "Executing sqlite3: $cmd\n";
4205 }
4206
4207 my @output = `$cmd`;
4208 if (scalar(@output) &&
4209 $ENV{TEST_VERBOSE}) {
4210 print STDERR "Output: ", join('', @output), "\n";
4211 }
4212
4213 my $config = {
4214 PidFile => $pid_file,
4215 ScoreboardFile => $scoreboard_file,
4216 SystemLog => $log_file,
4217
4218 DefaultChdir => '~',
4219
4220 IfModules => {
4221 'mod_delay.c' => {
4222 DelayEngine => 'off',
4223 },
4224
4225 'mod_quotatab_sql.c' => [
4226 '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}\'"',
4227 '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}\'"',
4228 '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',
4229 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
4230
4231 'QuotaEngine on',
4232 "QuotaLog $log_file",
4233 'QuotaLimitTable sql:/get-quota-limit',
4234 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
4235 'QuotaDefault group false hard 0 1 0 3 0 0',
4236 ],
4237
4238 'mod_sql.c' => {
4239 SQLAuthTypes => 'plaintext',
4240 SQLBackend => 'sqlite3',
4241 SQLConnectInfo => $db_file,
4242 SQLLogFile => $log_file,
4243 SQLMinID => '0',
4244 SQLNamedQuery => 'get-user-info SELECT "userid, passwd, uid, gid, homedir, shell FROM users WHERE userid=\'%U\'"',
4245 SQLUserInfo => 'custom:/get-user-info',
4246 },
4247 },
4248 };
4249
4250 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4251
4252 # Open pipes, for use between the parent and child processes. Specifically,
4253 # the child will indicate when it's done with its test by writing a message
4254 # to the parent.
4255 my ($rfh, $wfh);
4256 unless (pipe($rfh, $wfh)) {
4257 die("Can't open pipe: $!");
4258 }
4259
4260 my $ex;
4261
4262 # Fork child
4263 $self->handle_sigchld();
4264 defined(my $pid = fork()) or die("Can't fork: $!");
4265 if ($pid) {
4266 eval {
4267 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4268
4269 # Login as user1, and upload a file
4270 $client->login($user1, $passwd);
4271
4272 my $conn = $client->stor_raw('test.txt');
4273 unless ($conn) {
4274 die("Failed to STOR test.txt: " . $client->response_code() . " " .
4275 $client->response_msg());
4276 }
4277
4278 my $buf = "Hello, World\n";
4279 $conn->write($buf, length($buf), 25);
4280 eval { $conn->close() };
4281
4282 my $resp_code = $client->response_code();
4283 my $resp_msg = $client->response_msg();
4284
4285 $self->assert_transfer_ok($resp_code, $resp_msg);
4286 $client->quit();
4287
4288 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4289
4290 # Login as user2, and upload a file
4291 $client->login($user2, $passwd);
4292
4293 $conn = $client->stor_raw('test.txt');
4294 unless ($conn) {
4295 die("Failed to STOR test.txt: " . $client->response_code() . " " .
4296 $client->response_msg());
4297 }
4298
4299 $buf = "Hello, World\n";
4300 $conn->write($buf, length($buf), 25);
4301 eval { $conn->close() };
4302
4303 $resp_code = $client->response_code();
4304 $resp_msg = $client->response_msg();
4305
4306 $self->assert_transfer_ok($resp_code, $resp_msg);
4307 $client->quit();
4308 };
4309
4310 if ($@) {
4311 $ex = $@;
4312 }
4313
4314 $wfh->print("done\n");
4315 $wfh->flush();
4316
4317 } else {
4318 eval { server_wait($config_file, $rfh) };
4319 if ($@) {
4320 warn($@);
4321 exit 1;
4322 }
4323
4324 exit 0;
4325 }
4326
4327 # Stop server
4328 server_stop($pid_file);
4329
4330 $self->assert_child_ok($pid);
4331
4332 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 = \'$group\'");
4333
4334 my $expected;
4335
4336 $expected = 'group';
4337 $self->assert($expected eq $quota_type,
4338 test_msg("Expected '$expected', got '$quota_type'"));
4339
4340 $expected = '^(26.0|26)$';
4341 $self->assert(qr/$expected/, $bytes_in_used,
4342 test_msg("Expected $expected, got $bytes_in_used"));
4343
4344 $expected = '^(0.0|0)$';
4345 $self->assert(qr/$expected/, $bytes_out_used,
4346 test_msg("Expected $expected, got $bytes_out_used"));
4347
4348 $expected = '^(0.0|0)$';
4349 $self->assert(qr/$expected/, $bytes_xfer_used,
4350 test_msg("Expected $expected, got $bytes_xfer_used"));
4351
4352 $expected = 2;
4353 $self->assert($expected == $files_in_used,
4354 test_msg("Expected $expected, got $files_in_used"));
4355
4356 $expected = 0;
4357 $self->assert($expected == $files_out_used,
4358 test_msg("Expected $expected, got $files_out_used"));
4359
4360 $expected = 0;
4361 $self->assert($expected == $files_xfer_used,
4362 test_msg("Expected $expected, got $files_xfer_used"));
4363
4364 if ($ex) {
4365 test_append_logfile($log_file, $ex);
4366 unlink($log_file);
4367
4368 die($ex);
4369 }
4370
4371 unlink($log_file);
4372 }
4373
4374 sub quotatab_stor_ok_class_limit {
4375 my $self = shift;
4376 my $tmpdir = $self->{tmpdir};
4377
4378 my $config_file = "$tmpdir/quotatab.conf";
4379 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
4380 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
4381
4382 my $log_file = test_get_logfile();
4383
4384 my $user1 = 'proftpd';
4385 my $group = 'ftpd';
4386 my $passwd = 'test';
4387 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
4388 mkpath($home_dir1);
4389
4390 my $uid1 = 500;
4391 my $gid = 500;
4392
4393 my $user2 = 'proftpd2';
4394 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
4395 mkpath($home_dir2);
4396
4397 my $uid2 = 1000;
4398
4399 my $class = 'test';
4400
4401 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4402
4403 # Build up sqlite3 command to create users, groups tables and populate them
4404 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4405
4406 if (open(my $fh, "> $db_script")) {
4407 print $fh <<EOS;
4408 CREATE TABLE users (
4409 userid TEXT PRIMARY KEY,
4410 passwd TEXT,
4411 uid INTEGER,
4412 gid INTEGER,
4413 homedir TEXT,
4414 shell TEXT,
4415 lastdir TEXT
4416 );
4417 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
4418 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
4419
4420 CREATE TABLE groups (
4421 groupname TEXT PRIMARY KEY,
4422 gid INTEGER,
4423 members TEXT
4424 );
4425 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
4426
4427 CREATE TABLE quotalimits (
4428 name TEXT NOT NULL PRIMARY KEY,
4429 quota_type TEXT NOT NULL,
4430 per_session TEXT NOT NULL,
4431 limit_type TEXT NOT NULL,
4432 bytes_in_avail REAL NOT NULL,
4433 bytes_out_avail REAL NOT NULL,
4434 bytes_xfer_avail REAL NOT NULL,
4435 files_in_avail INTEGER NOT NULL,
4436 files_out_avail INTEGER NOT NULL,
4437 files_xfer_avail INTEGER NOT NULL
4438 );
4439 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 ('$class', 'class', 'false', 'soft', 32, 0, 0, 2, 0, 0);
4440
4441 CREATE TABLE quotatallies (
4442 name TEXT NOT NULL PRIMARY KEY,
4443 quota_type TEXT NOT NULL,
4444 bytes_in_used REAL NOT NULL,
4445 bytes_out_used REAL NOT NULL,
4446 bytes_xfer_used REAL NOT NULL,
4447 files_in_used INTEGER NOT NULL,
4448 files_out_used INTEGER NOT NULL,
4449 files_xfer_used INTEGER NOT NULL
4450 );
4451 EOS
4452
4453 unless (close($fh)) {
4454 die("Can't write $db_script: $!");
4455 }
4456
4457 } else {
4458 die("Can't open $db_script: $!");
4459 }
4460
4461 my $cmd = "sqlite3 $db_file < $db_script";
4462
4463 if ($ENV{TEST_VERBOSE}) {
4464 print STDERR "Executing sqlite3: $cmd\n";
4465 }
4466
4467 my @output = `$cmd`;
4468 if (scalar(@output) &&
4469 $ENV{TEST_VERBOSE}) {
4470 print STDERR "Output: ", join('', @output), "\n";
4471 }
4472
4473 my $config = {
4474 PidFile => $pid_file,
4475 ScoreboardFile => $scoreboard_file,
4476 SystemLog => $log_file,
4477
4478 DefaultChdir => '~',
4479
4480 Class => {
4481 $class => {
4482 From => '127.0.0.1',
4483 },
4484 },
4485
4486 IfModules => {
4487 'mod_delay.c' => {
4488 DelayEngine => 'off',
4489 },
4490
4491 'mod_quotatab_sql.c' => [
4492 '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}\'"',
4493 '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}\'"',
4494 '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',
4495 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
4496
4497 'QuotaEngine on',
4498 "QuotaLog $log_file",
4499 'QuotaLimitTable sql:/get-quota-limit',
4500 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
4501 ],
4502
4503 'mod_sql.c' => {
4504 SQLAuthTypes => 'plaintext',
4505 SQLBackend => 'sqlite3',
4506 SQLConnectInfo => $db_file,
4507 SQLLogFile => $log_file,
4508 SQLMinID => '0',
4509 },
4510 },
4511 };
4512
4513 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4514
4515 # Open pipes, for use between the parent and child processes. Specifically,
4516 # the child will indicate when it's done with its test by writing a message
4517 # to the parent.
4518 my ($rfh, $wfh);
4519 unless (pipe($rfh, $wfh)) {
4520 die("Can't open pipe: $!");
4521 }
4522
4523 my $ex;
4524
4525 # Fork child
4526 $self->handle_sigchld();
4527 defined(my $pid = fork()) or die("Can't fork: $!");
4528 if ($pid) {
4529 eval {
4530 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4531
4532 # Login as user1, and upload a file
4533 $client->login($user1, $passwd);
4534
4535 my $conn = $client->stor_raw('test.txt');
4536 unless ($conn) {
4537 die("Failed to STOR test.txt: " . $client->response_code() . " " .
4538 $client->response_msg());
4539 }
4540
4541 my $buf = "Hello, World\n";
4542 $conn->write($buf, length($buf), 25);
4543 eval { $conn->close() };
4544
4545 my $resp_code = $client->response_code();
4546 my $resp_msg = $client->response_msg();
4547
4548 $self->assert_transfer_ok($resp_code, $resp_msg);
4549 $client->quit();
4550
4551 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4552
4553 # Login as user2, and upload a file
4554 $client->login($user2, $passwd);
4555
4556 $conn = $client->stor_raw('test.txt');
4557 unless ($conn) {
4558 die("Failed to STOR test.txt: " . $client->response_code() . " " .
4559 $client->response_msg());
4560 }
4561
4562 $buf = "Hello, World\n";
4563 $conn->write($buf, length($buf), 25);
4564 eval { $conn->close() };
4565
4566 $resp_code = $client->response_code();
4567 $resp_msg = $client->response_msg();
4568
4569 $self->assert_transfer_ok($resp_code, $resp_msg);
4570 $client->quit();
4571 };
4572
4573 if ($@) {
4574 $ex = $@;
4575 }
4576
4577 $wfh->print("done\n");
4578 $wfh->flush();
4579
4580 } else {
4581 eval { server_wait($config_file, $rfh) };
4582 if ($@) {
4583 warn($@);
4584 exit 1;
4585 }
4586
4587 exit 0;
4588 }
4589
4590 # Stop server
4591 server_stop($pid_file);
4592
4593 $self->assert_child_ok($pid);
4594
4595 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 = \'$class\'");
4596
4597 my $expected;
4598
4599 $expected = 'class';
4600 $self->assert($expected eq $quota_type,
4601 test_msg("Expected '$expected', got '$quota_type'"));
4602
4603 $expected = '^(26.0|26)$';
4604 $self->assert(qr/$expected/, $bytes_in_used,
4605 test_msg("Expected $expected, got $bytes_in_used"));
4606
4607 $expected = '^(0.0|0)$';
4608 $self->assert(qr/$expected/, $bytes_out_used,
4609 test_msg("Expected $expected, got $bytes_out_used"));
4610
4611 $expected = '^(0.0|0)$';
4612 $self->assert(qr/$expected/, $bytes_xfer_used,
4613 test_msg("Expected $expected, got $bytes_xfer_used"));
4614
4615 $expected = 2;
4616 $self->assert($expected == $files_in_used,
4617 test_msg("Expected $expected, got $files_in_used"));
4618
4619 $expected = 0;
4620 $self->assert($expected == $files_out_used,
4621 test_msg("Expected $expected, got $files_out_used"));
4622
4623 $expected = 0;
4624 $self->assert($expected == $files_xfer_used,
4625 test_msg("Expected $expected, got $files_xfer_used"));
4626
4627 if ($ex) {
4628 test_append_logfile($log_file, $ex);
4629 unlink($log_file);
4630
4631 die($ex);
4632 }
4633
4634 unlink($log_file);
4635 }
4636
4637 sub quotatab_stor_ok_class_limit_bytes_in_exceeded_soft_limit {
4638 my $self = shift;
4639 my $tmpdir = $self->{tmpdir};
4640
4641 my $config_file = "$tmpdir/quotatab.conf";
4642 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
4643 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
4644
4645 my $log_file = test_get_logfile();
4646
4647 my $user1 = 'proftpd';
4648 my $group = 'ftpd';
4649 my $passwd = 'test';
4650 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
4651 mkpath($home_dir1);
4652
4653 my $uid1 = 500;
4654 my $gid = 500;
4655
4656 my $user2 = 'proftpd2';
4657 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
4658 mkpath($home_dir2);
4659
4660 my $uid2 = 1000;
4661
4662 my $class = 'test';
4663
4664 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4665
4666 # Build up sqlite3 command to create users, groups tables and populate them
4667 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4668
4669 if (open(my $fh, "> $db_script")) {
4670 print $fh <<EOS;
4671 CREATE TABLE users (
4672 userid TEXT PRIMARY KEY,
4673 passwd TEXT,
4674 uid INTEGER,
4675 gid INTEGER,
4676 homedir TEXT,
4677 shell TEXT,
4678 lastdir TEXT
4679 );
4680 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
4681 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
4682
4683 CREATE TABLE groups (
4684 groupname TEXT PRIMARY KEY,
4685 gid INTEGER,
4686 members TEXT
4687 );
4688 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
4689
4690 CREATE TABLE quotalimits (
4691 name TEXT NOT NULL PRIMARY KEY,
4692 quota_type TEXT NOT NULL,
4693 per_session TEXT NOT NULL,
4694 limit_type TEXT NOT NULL,
4695 bytes_in_avail REAL NOT NULL,
4696 bytes_out_avail REAL NOT NULL,
4697 bytes_xfer_avail REAL NOT NULL,
4698 files_in_avail INTEGER NOT NULL,
4699 files_out_avail INTEGER NOT NULL,
4700 files_xfer_avail INTEGER NOT NULL
4701 );
4702 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 ('$class', 'class', 'false', 'soft', 5, 0, 0, 3, 0, 0);
4703
4704 CREATE TABLE quotatallies (
4705 name TEXT NOT NULL PRIMARY KEY,
4706 quota_type TEXT NOT NULL,
4707 bytes_in_used REAL NOT NULL,
4708 bytes_out_used REAL NOT NULL,
4709 bytes_xfer_used REAL NOT NULL,
4710 files_in_used INTEGER NOT NULL,
4711 files_out_used INTEGER NOT NULL,
4712 files_xfer_used INTEGER NOT NULL
4713 );
4714 EOS
4715
4716 unless (close($fh)) {
4717 die("Can't write $db_script: $!");
4718 }
4719
4720 } else {
4721 die("Can't open $db_script: $!");
4722 }
4723
4724 my $cmd = "sqlite3 $db_file < $db_script";
4725
4726 if ($ENV{TEST_VERBOSE}) {
4727 print STDERR "Executing sqlite3: $cmd\n";
4728 }
4729
4730 my @output = `$cmd`;
4731 if (scalar(@output) &&
4732 $ENV{TEST_VERBOSE}) {
4733 print STDERR "Output: ", join('', @output), "\n";
4734 }
4735
4736 my $config = {
4737 PidFile => $pid_file,
4738 ScoreboardFile => $scoreboard_file,
4739 SystemLog => $log_file,
4740
4741 DefaultChdir => '~',
4742
4743 Class => {
4744 $class => {
4745 From => '127.0.0.1',
4746 },
4747 },
4748
4749 IfModules => {
4750 'mod_delay.c' => {
4751 DelayEngine => 'off',
4752 },
4753
4754 'mod_quotatab_sql.c' => [
4755 '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}\'"',
4756 '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}\'"',
4757 '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',
4758 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
4759
4760 'QuotaEngine on',
4761 "QuotaLog $log_file",
4762 'QuotaLimitTable sql:/get-quota-limit',
4763 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
4764 ],
4765
4766 'mod_sql.c' => {
4767 SQLAuthTypes => 'plaintext',
4768 SQLBackend => 'sqlite3',
4769 SQLConnectInfo => $db_file,
4770 SQLLogFile => $log_file,
4771 SQLMinID => '0',
4772 },
4773 },
4774 };
4775
4776 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4777
4778 # Open pipes, for use between the parent and child processes. Specifically,
4779 # the child will indicate when it's done with its test by writing a message
4780 # to the parent.
4781 my ($rfh, $wfh);
4782 unless (pipe($rfh, $wfh)) {
4783 die("Can't open pipe: $!");
4784 }
4785
4786 my $ex;
4787
4788 # Fork child
4789 $self->handle_sigchld();
4790 defined(my $pid = fork()) or die("Can't fork: $!");
4791 if ($pid) {
4792 eval {
4793 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4794
4795 # Login as user1, and upload a file
4796 $client->login($user1, $passwd);
4797
4798 my $conn = $client->stor_raw('test.txt');
4799 unless ($conn) {
4800 die("Failed to STOR test.txt: " . $client->response_code() . " " .
4801 $client->response_msg());
4802 }
4803
4804 my $buf = "Hello, World\n";
4805 $conn->write($buf, length($buf), 25);
4806 eval { $conn->close() };
4807
4808 my $resp_code = $client->response_code();
4809 my $resp_msg = $client->response_msg();
4810
4811 $self->assert_transfer_ok($resp_code, $resp_msg);
4812 $client->quit();
4813
4814 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4815
4816 # Login as user2, and upload a file
4817 $client->login($user2, $passwd);
4818
4819 $conn = $client->stor_raw('test.txt');
4820 if ($conn) {
4821 die("STOR test.txt succeeded unexpectedly");
4822 }
4823
4824 $resp_code = $client->response_code();
4825 $resp_msg = $client->response_msg();
4826
4827 my $expected = 552;
4828 $self->assert($expected == $resp_code,
4829 test_msg("Expected $expected, got $resp_code"));
4830
4831 $expected = 'STOR denied: quota exceeded: used \S+ of \S+ upload bytes';
4832 $self->assert(qr/$expected/, $resp_msg,
4833 test_msg("Expected '$expected', got '$resp_msg'"));
4834
4835 $client->quit();
4836 };
4837
4838 if ($@) {
4839 $ex = $@;
4840 }
4841
4842 $wfh->print("done\n");
4843 $wfh->flush();
4844
4845 } else {
4846 eval { server_wait($config_file, $rfh) };
4847 if ($@) {
4848 warn($@);
4849 exit 1;
4850 }
4851
4852 exit 0;
4853 }
4854
4855 # Stop server
4856 server_stop($pid_file);
4857
4858 $self->assert_child_ok($pid);
4859
4860 if ($ex) {
4861 test_append_logfile($log_file, $ex);
4862 unlink($log_file);
4863
4864 die($ex);
4865 }
4866
4867 unlink($log_file);
4868 }
4869
4870 sub quotatab_stor_ok_class_limit_bytes_in_exceeded_hard_limit {
4871 my $self = shift;
4872 my $tmpdir = $self->{tmpdir};
4873
4874 my $config_file = "$tmpdir/quotatab.conf";
4875 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
4876 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
4877
4878 my $log_file = test_get_logfile();
4879
4880 my $user1 = 'proftpd';
4881 my $group = 'ftpd';
4882 my $passwd = 'test';
4883 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
4884 mkpath($home_dir1);
4885
4886 my $uid1 = 500;
4887 my $gid = 500;
4888
4889 my $user2 = 'proftpd2';
4890 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
4891 mkpath($home_dir2);
4892
4893 my $uid2 = 1000;
4894
4895 my $class = 'test';
4896
4897 my $test_file = File::Spec->rel2abs("$tmpdir/bar/test.txt");
4898
4899 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4900
4901 # Build up sqlite3 command to create users, groups tables and populate them
4902 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4903
4904 if (open(my $fh, "> $db_script")) {
4905 print $fh <<EOS;
4906 CREATE TABLE users (
4907 userid TEXT PRIMARY KEY,
4908 passwd TEXT,
4909 uid INTEGER,
4910 gid INTEGER,
4911 homedir TEXT,
4912 shell TEXT,
4913 lastdir TEXT
4914 );
4915 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
4916 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
4917
4918 CREATE TABLE groups (
4919 groupname TEXT PRIMARY KEY,
4920 gid INTEGER,
4921 members TEXT
4922 );
4923 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
4924
4925 CREATE TABLE quotalimits (
4926 name TEXT NOT NULL PRIMARY KEY,
4927 quota_type TEXT NOT NULL,
4928 per_session TEXT NOT NULL,
4929 limit_type TEXT NOT NULL,
4930 bytes_in_avail REAL NOT NULL,
4931 bytes_out_avail REAL NOT NULL,
4932 bytes_xfer_avail REAL NOT NULL,
4933 files_in_avail INTEGER NOT NULL,
4934 files_out_avail INTEGER NOT NULL,
4935 files_xfer_avail INTEGER NOT NULL
4936 );
4937 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 ('$class', 'class', 'false', 'hard', 20, 0, 0, 3, 0, 0);
4938
4939 CREATE TABLE quotatallies (
4940 name TEXT NOT NULL PRIMARY KEY,
4941 quota_type TEXT NOT NULL,
4942 bytes_in_used REAL NOT NULL,
4943 bytes_out_used REAL NOT NULL,
4944 bytes_xfer_used REAL NOT NULL,
4945 files_in_used INTEGER NOT NULL,
4946 files_out_used INTEGER NOT NULL,
4947 files_xfer_used INTEGER NOT NULL
4948 );
4949 EOS
4950
4951 unless (close($fh)) {
4952 die("Can't write $db_script: $!");
4953 }
4954
4955 } else {
4956 die("Can't open $db_script: $!");
4957 }
4958
4959 my $cmd = "sqlite3 $db_file < $db_script";
4960
4961 if ($ENV{TEST_VERBOSE}) {
4962 print STDERR "Executing sqlite3: $cmd\n";
4963 }
4964
4965 my @output = `$cmd`;
4966 if (scalar(@output) &&
4967 $ENV{TEST_VERBOSE}) {
4968 print STDERR "Output: ", join('', @output), "\n";
4969 }
4970
4971 my $config = {
4972 PidFile => $pid_file,
4973 ScoreboardFile => $scoreboard_file,
4974 SystemLog => $log_file,
4975
4976 DefaultChdir => '~',
4977
4978 Class => {
4979 $class => {
4980 From => '127.0.0.1',
4981 },
4982 },
4983
4984 IfModules => {
4985 'mod_delay.c' => {
4986 DelayEngine => 'off',
4987 },
4988
4989 'mod_quotatab_sql.c' => [
4990 '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}\'"',
4991 '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}\'"',
4992 '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',
4993 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
4994
4995 'QuotaEngine on',
4996 "QuotaLog $log_file",
4997 'QuotaLimitTable sql:/get-quota-limit',
4998 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
4999 ],
5000
5001 'mod_sql.c' => {
5002 SQLAuthTypes => 'plaintext',
5003 SQLBackend => 'sqlite3',
5004 SQLConnectInfo => $db_file,
5005 SQLLogFile => $log_file,
5006 SQLMinID => '0',
5007 },
5008 },
5009 };
5010
5011 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5012
5013 # Open pipes, for use between the parent and child processes. Specifically,
5014 # the child will indicate when it's done with its test by writing a message
5015 # to the parent.
5016 my ($rfh, $wfh);
5017 unless (pipe($rfh, $wfh)) {
5018 die("Can't open pipe: $!");
5019 }
5020
5021 my $ex;
5022
5023 # Fork child
5024 $self->handle_sigchld();
5025 defined(my $pid = fork()) or die("Can't fork: $!");
5026 if ($pid) {
5027 eval {
5028 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5029
5030 # Login as user1, and upload a file
5031 $client->login($user1, $passwd);
5032
5033 my $conn = $client->stor_raw('test.txt');
5034 unless ($conn) {
5035 die("Failed to STOR test.txt: " . $client->response_code() . " " .
5036 $client->response_msg());
5037 }
5038
5039 my $buf = "Hello, World\n";
5040 $conn->write($buf, length($buf), 25);
5041 eval { $conn->close() };
5042
5043 my $resp_code = $client->response_code();
5044 my $resp_msg = $client->response_msg();
5045
5046 $self->assert_transfer_ok($resp_code, $resp_msg);
5047 $client->quit();
5048
5049 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5050
5051 # Login as user2, and upload a file
5052 $client->login($user2, $passwd);
5053
5054 $conn = $client->stor_raw('test.txt');
5055 unless ($conn) {
5056 die("Failed to STOR test.txt: " . $client->response_code() . " " .
5057 $client->response_msg());
5058 }
5059
5060 $buf = "Hello, World\n";
5061 $conn->write($buf, length($buf), 25);
5062 eval { $conn->close() };
5063
5064 $resp_code = $client->response_code();
5065 $resp_msg = $client->response_msg();
5066
5067 my $expected = 552;
5068 $self->assert($expected == $resp_code,
5069 test_msg("Expected $expected, got $resp_code"));
5070
5071 $expected = 'Transfer aborted. (Disc|Disk) quota exceeded';
5072 $self->assert(qr/$expected/, $resp_msg,
5073 test_msg("Expected '$expected', got '$resp_msg'"));
5074
5075 $client->quit();
5076
5077 if (-f $test_file) {
5078 die("$test_file exists, should have been deleted");
5079 }
5080 };
5081
5082 if ($@) {
5083 $ex = $@;
5084 }
5085
5086 $wfh->print("done\n");
5087 $wfh->flush();
5088
5089 } else {
5090 eval { server_wait($config_file, $rfh) };
5091 if ($@) {
5092 warn($@);
5093 exit 1;
5094 }
5095
5096 exit 0;
5097 }
5098
5099 # Stop server
5100 server_stop($pid_file);
5101
5102 $self->assert_child_ok($pid);
5103
5104 if ($ex) {
5105 test_append_logfile($log_file, $ex);
5106 unlink($log_file);
5107
5108 die($ex);
5109 }
5110
5111 unlink($log_file);
5112 }
5113
5114 sub quotatab_stor_ok_class_limit_files_in_exceeded {
5115 my $self = shift;
5116 my $tmpdir = $self->{tmpdir};
5117
5118 my $config_file = "$tmpdir/quotatab.conf";
5119 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
5120 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
5121
5122 my $log_file = test_get_logfile();
5123
5124 my $user1 = 'proftpd';
5125 my $group = 'ftpd';
5126 my $passwd = 'test';
5127 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
5128 mkpath($home_dir1);
5129
5130 my $uid1 = 500;
5131 my $gid = 500;
5132
5133 my $user2 = 'proftpd2';
5134 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
5135 mkpath($home_dir2);
5136
5137 my $uid2 = 1000;
5138
5139 my $class = 'test';
5140
5141 my $test_file = File::Spec->rel2abs("$tmpdir/bar/test.txt");
5142
5143 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5144
5145 # Build up sqlite3 command to create users, groups tables and populate them
5146 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5147
5148 if (open(my $fh, "> $db_script")) {
5149 print $fh <<EOS;
5150 CREATE TABLE users (
5151 userid TEXT PRIMARY KEY,
5152 passwd TEXT,
5153 uid INTEGER,
5154 gid INTEGER,
5155 homedir TEXT,
5156 shell TEXT,
5157 lastdir TEXT
5158 );
5159 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
5160 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
5161
5162 CREATE TABLE groups (
5163 groupname TEXT PRIMARY KEY,
5164 gid INTEGER,
5165 members TEXT
5166 );
5167 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
5168
5169 CREATE TABLE quotalimits (
5170 name TEXT NOT NULL PRIMARY KEY,
5171 quota_type TEXT NOT NULL,
5172 per_session TEXT NOT NULL,
5173 limit_type TEXT NOT NULL,
5174 bytes_in_avail REAL NOT NULL,
5175 bytes_out_avail REAL NOT NULL,
5176 bytes_xfer_avail REAL NOT NULL,
5177 files_in_avail INTEGER NOT NULL,
5178 files_out_avail INTEGER NOT NULL,
5179 files_xfer_avail INTEGER NOT NULL
5180 );
5181 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 ('$class', 'class', 'false', 'hard', 32, 0, 0, 1, 0, 0);
5182
5183 CREATE TABLE quotatallies (
5184 name TEXT NOT NULL PRIMARY KEY,
5185 quota_type TEXT NOT NULL,
5186 bytes_in_used REAL NOT NULL,
5187 bytes_out_used REAL NOT NULL,
5188 bytes_xfer_used REAL NOT NULL,
5189 files_in_used INTEGER NOT NULL,
5190 files_out_used INTEGER NOT NULL,
5191 files_xfer_used INTEGER NOT NULL
5192 );
5193 EOS
5194
5195 unless (close($fh)) {
5196 die("Can't write $db_script: $!");
5197 }
5198
5199 } else {
5200 die("Can't open $db_script: $!");
5201 }
5202
5203 my $cmd = "sqlite3 $db_file < $db_script";
5204
5205 if ($ENV{TEST_VERBOSE}) {
5206 print STDERR "Executing sqlite3: $cmd\n";
5207 }
5208
5209 my @output = `$cmd`;
5210 if (scalar(@output) &&
5211 $ENV{TEST_VERBOSE}) {
5212 print STDERR "Output: ", join('', @output), "\n";
5213 }
5214
5215 my $config = {
5216 PidFile => $pid_file,
5217 ScoreboardFile => $scoreboard_file,
5218 SystemLog => $log_file,
5219
5220 DefaultChdir => '~',
5221
5222 Class => {
5223 $class => {
5224 From => '127.0.0.1',
5225 },
5226 },
5227
5228 IfModules => {
5229 'mod_delay.c' => {
5230 DelayEngine => 'off',
5231 },
5232
5233 'mod_quotatab_sql.c' => [
5234 '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}\'"',
5235 '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}\'"',
5236 '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',
5237 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
5238
5239 'QuotaEngine on',
5240 "QuotaLog $log_file",
5241 'QuotaLimitTable sql:/get-quota-limit',
5242 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
5243 ],
5244
5245 'mod_sql.c' => {
5246 SQLAuthTypes => 'plaintext',
5247 SQLBackend => 'sqlite3',
5248 SQLConnectInfo => $db_file,
5249 SQLLogFile => $log_file,
5250 SQLMinID => '0',
5251 },
5252 },
5253 };
5254
5255 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5256
5257 # Open pipes, for use between the parent and child processes. Specifically,
5258 # the child will indicate when it's done with its test by writing a message
5259 # to the parent.
5260 my ($rfh, $wfh);
5261 unless (pipe($rfh, $wfh)) {
5262 die("Can't open pipe: $!");
5263 }
5264
5265 my $ex;
5266
5267 # Fork child
5268 $self->handle_sigchld();
5269 defined(my $pid = fork()) or die("Can't fork: $!");
5270 if ($pid) {
5271 eval {
5272 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5273
5274 # Login as user1, and upload a file
5275 $client->login($user1, $passwd);
5276
5277 my $conn = $client->stor_raw('test.txt');
5278 unless ($conn) {
5279 die("Failed to STOR test.txt: " . $client->response_code() . " " .
5280 $client->response_msg());
5281 }
5282
5283 my $buf = "Hello, World\n";
5284 $conn->write($buf, length($buf), 25);
5285 eval { $conn->close() };
5286
5287 my $resp_code = $client->response_code();
5288 my $resp_msg = $client->response_msg();
5289
5290 $self->assert_transfer_ok($resp_code, $resp_msg);
5291 $client->quit();
5292
5293 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5294
5295 # Login as user2, and upload a file
5296 $client->login($user2, $passwd);
5297
5298 $conn = $client->stor_raw('test2.txt');
5299 if ($conn) {
5300 die("STOR test2.txt succeeded unexpectedly");
5301 }
5302
5303 $resp_code = $client->response_code();
5304 $resp_msg = $client->response_msg();
5305
5306 my $expected = 552;
5307 $self->assert($expected == $resp_code,
5308 test_msg("Expected $expected, got $resp_code"));
5309
5310 $expected = 'STOR: notice: quota reached: used \d+ of \d+ upload file';
5311 $self->assert(qr/$expected/, $resp_msg,
5312 test_msg("Expected '$expected', got '$resp_msg'"));
5313 };
5314
5315 if ($@) {
5316 $ex = $@;
5317 }
5318
5319 $wfh->print("done\n");
5320 $wfh->flush();
5321
5322 } else {
5323 eval { server_wait($config_file, $rfh) };
5324 if ($@) {
5325 warn($@);
5326 exit 1;
5327 }
5328
5329 exit 0;
5330 }
5331
5332 # Stop server
5333 server_stop($pid_file);
5334
5335 $self->assert_child_ok($pid);
5336
5337 if ($ex) {
5338 test_append_logfile($log_file, $ex);
5339 unlink($log_file);
5340
5341 die($ex);
5342 }
5343
5344 unlink($log_file);
5345 }
5346
5347 sub quotatab_stor_ok_all_limit {
5348 my $self = shift;
5349 my $tmpdir = $self->{tmpdir};
5350
5351 my $config_file = "$tmpdir/quotatab.conf";
5352 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
5353 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
5354
5355 my $log_file = test_get_logfile();
5356
5357 my $user1 = 'proftpd';
5358 my $group = 'ftpd';
5359 my $passwd = 'test';
5360 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
5361 mkpath($home_dir1);
5362
5363 my $uid1 = 500;
5364 my $gid = 500;
5365
5366 my $user2 = 'proftpd2';
5367 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
5368 mkpath($home_dir2);
5369
5370 my $uid2 = 1000;
5371
5372 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5373
5374 # Build up sqlite3 command to create users, groups tables and populate them
5375 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5376
5377 if (open(my $fh, "> $db_script")) {
5378 print $fh <<EOS;
5379 CREATE TABLE users (
5380 userid TEXT PRIMARY KEY,
5381 passwd TEXT,
5382 uid INTEGER,
5383 gid INTEGER,
5384 homedir TEXT,
5385 shell TEXT,
5386 lastdir TEXT
5387 );
5388 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
5389 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
5390
5391 CREATE TABLE groups (
5392 groupname TEXT PRIMARY KEY,
5393 gid INTEGER,
5394 members TEXT
5395 );
5396 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
5397
5398 CREATE TABLE quotalimits (
5399 name TEXT NOT NULL PRIMARY KEY,
5400 quota_type TEXT NOT NULL,
5401 per_session TEXT NOT NULL,
5402 limit_type TEXT NOT NULL,
5403 bytes_in_avail REAL NOT NULL,
5404 bytes_out_avail REAL NOT NULL,
5405 bytes_xfer_avail REAL NOT NULL,
5406 files_in_avail INTEGER NOT NULL,
5407 files_out_avail INTEGER NOT NULL,
5408 files_xfer_avail INTEGER NOT NULL
5409 );
5410 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 ('', 'all', 'false', 'soft', 32, 0, 0, 2, 0, 0);
5411
5412 CREATE TABLE quotatallies (
5413 name TEXT NOT NULL PRIMARY KEY,
5414 quota_type TEXT NOT NULL,
5415 bytes_in_used REAL NOT NULL,
5416 bytes_out_used REAL NOT NULL,
5417 bytes_xfer_used REAL NOT NULL,
5418 files_in_used INTEGER NOT NULL,
5419 files_out_used INTEGER NOT NULL,
5420 files_xfer_used INTEGER NOT NULL
5421 );
5422 EOS
5423
5424 unless (close($fh)) {
5425 die("Can't write $db_script: $!");
5426 }
5427
5428 } else {
5429 die("Can't open $db_script: $!");
5430 }
5431
5432 my $cmd = "sqlite3 $db_file < $db_script";
5433
5434 if ($ENV{TEST_VERBOSE}) {
5435 print STDERR "Executing sqlite3: $cmd\n";
5436 }
5437
5438 my @output = `$cmd`;
5439 if (scalar(@output) &&
5440 $ENV{TEST_VERBOSE}) {
5441 print STDERR "Output: ", join('', @output), "\n";
5442 }
5443
5444 my $config = {
5445 PidFile => $pid_file,
5446 ScoreboardFile => $scoreboard_file,
5447 SystemLog => $log_file,
5448
5449 DefaultChdir => '~',
5450
5451 IfModules => {
5452 'mod_delay.c' => {
5453 DelayEngine => 'off',
5454 },
5455
5456 'mod_quotatab_sql.c' => [
5457 '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}\'"',
5458 '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}\'"',
5459 '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',
5460 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
5461
5462 'QuotaEngine on',
5463 "QuotaLog $log_file",
5464 'QuotaLimitTable sql:/get-quota-limit',
5465 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
5466 ],
5467
5468 'mod_sql.c' => {
5469 SQLAuthTypes => 'plaintext',
5470 SQLBackend => 'sqlite3',
5471 SQLConnectInfo => $db_file,
5472 SQLLogFile => $log_file,
5473 SQLMinID => '0',
5474 },
5475 },
5476 };
5477
5478 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5479
5480 # Open pipes, for use between the parent and child processes. Specifically,
5481 # the child will indicate when it's done with its test by writing a message
5482 # to the parent.
5483 my ($rfh, $wfh);
5484 unless (pipe($rfh, $wfh)) {
5485 die("Can't open pipe: $!");
5486 }
5487
5488 my $ex;
5489
5490 # Fork child
5491 $self->handle_sigchld();
5492 defined(my $pid = fork()) or die("Can't fork: $!");
5493 if ($pid) {
5494 eval {
5495 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5496
5497 # Login as user1, and upload a file
5498 $client->login($user1, $passwd);
5499
5500 my $conn = $client->stor_raw('test.txt');
5501 unless ($conn) {
5502 die("Failed to STOR test.txt: " . $client->response_code() . " " .
5503 $client->response_msg());
5504 }
5505
5506 my $buf = "Hello, World\n";
5507 $conn->write($buf, length($buf), 25);
5508 eval { $conn->close() };
5509
5510 my $resp_code = $client->response_code();
5511 my $resp_msg = $client->response_msg();
5512
5513 $self->assert_transfer_ok($resp_code, $resp_msg);
5514 $client->quit();
5515
5516 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5517
5518 # Login as user2, and upload a file
5519 $client->login($user2, $passwd);
5520
5521 $conn = $client->stor_raw('test.txt');
5522 unless ($conn) {
5523 die("Failed to STOR test.txt: " . $client->response_code() . " " .
5524 $client->response_msg());
5525 }
5526
5527 $buf = "Hello, World\n";
5528 $conn->write($buf, length($buf), 25);
5529 eval { $conn->close() };
5530
5531 $resp_code = $client->response_code();
5532 $resp_msg = $client->response_msg();
5533
5534 $self->assert_transfer_ok($resp_code, $resp_msg);
5535 $client->quit();
5536 };
5537
5538 if ($@) {
5539 $ex = $@;
5540 }
5541
5542 $wfh->print("done\n");
5543 $wfh->flush();
5544
5545 } else {
5546 eval { server_wait($config_file, $rfh) };
5547 if ($@) {
5548 warn($@);
5549 exit 1;
5550 }
5551
5552 exit 0;
5553 }
5554
5555 # Stop server
5556 server_stop($pid_file);
5557
5558 $self->assert_child_ok($pid);
5559
5560 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 = \'\'");
5561
5562 my $expected;
5563
5564 $expected = 'all';
5565 $self->assert($expected eq $quota_type,
5566 test_msg("Expected '$expected', got '$quota_type'"));
5567
5568 $expected = '^(26.0|26)$';
5569 $self->assert(qr/$expected/, $bytes_in_used,
5570 test_msg("Expected $expected, got $bytes_in_used"));
5571
5572 $expected = '^(0.0|0)$';
5573 $self->assert(qr/$expected/, $bytes_out_used,
5574 test_msg("Expected $expected, got $bytes_out_used"));
5575
5576 $expected = '^(0.0|0)$';
5577 $self->assert(qr/$expected/, $bytes_xfer_used,
5578 test_msg("Expected $expected, got $bytes_xfer_used"));
5579
5580 $expected = 2;
5581 $self->assert($expected == $files_in_used,
5582 test_msg("Expected $expected, got $files_in_used"));
5583
5584 $expected = 0;
5585 $self->assert($expected == $files_out_used,
5586 test_msg("Expected $expected, got $files_out_used"));
5587
5588 $expected = 0;
5589 $self->assert($expected == $files_xfer_used,
5590 test_msg("Expected $expected, got $files_xfer_used"));
5591
5592 if ($ex) {
5593 test_append_logfile($log_file, $ex);
5594 unlink($log_file);
5595
5596 die($ex);
5597 }
5598
5599 unlink($log_file);
5600 }
5601
5602 sub quotatab_stor_ok_all_limit_bytes_in_exceeded_soft_limit {
5603 my $self = shift;
5604 my $tmpdir = $self->{tmpdir};
5605
5606 my $config_file = "$tmpdir/quotatab.conf";
5607 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
5608 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
5609
5610 my $log_file = test_get_logfile();
5611
5612 my $user1 = 'proftpd';
5613 my $group = 'ftpd';
5614 my $passwd = 'test';
5615 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
5616 mkpath($home_dir1);
5617
5618 my $uid1 = 500;
5619 my $gid = 500;
5620
5621 my $user2 = 'proftpd2';
5622 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
5623 mkpath($home_dir2);
5624
5625 my $uid2 = 1000;
5626
5627 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5628
5629 # Build up sqlite3 command to create users, groups tables and populate them
5630 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5631
5632 if (open(my $fh, "> $db_script")) {
5633 print $fh <<EOS;
5634 CREATE TABLE users (
5635 userid TEXT PRIMARY KEY,
5636 passwd TEXT,
5637 uid INTEGER,
5638 gid INTEGER,
5639 homedir TEXT,
5640 shell TEXT,
5641 lastdir TEXT
5642 );
5643 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
5644 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
5645
5646 CREATE TABLE groups (
5647 groupname TEXT PRIMARY KEY,
5648 gid INTEGER,
5649 members TEXT
5650 );
5651 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
5652
5653 CREATE TABLE quotalimits (
5654 name TEXT NOT NULL PRIMARY KEY,
5655 quota_type TEXT NOT NULL,
5656 per_session TEXT NOT NULL,
5657 limit_type TEXT NOT NULL,
5658 bytes_in_avail REAL NOT NULL,
5659 bytes_out_avail REAL NOT NULL,
5660 bytes_xfer_avail REAL NOT NULL,
5661 files_in_avail INTEGER NOT NULL,
5662 files_out_avail INTEGER NOT NULL,
5663 files_xfer_avail INTEGER NOT NULL
5664 );
5665 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 ('', 'all', 'false', 'soft', 5, 0, 0, 3, 0, 0);
5666
5667 CREATE TABLE quotatallies (
5668 name TEXT NOT NULL PRIMARY KEY,
5669 quota_type TEXT NOT NULL,
5670 bytes_in_used REAL NOT NULL,
5671 bytes_out_used REAL NOT NULL,
5672 bytes_xfer_used REAL NOT NULL,
5673 files_in_used INTEGER NOT NULL,
5674 files_out_used INTEGER NOT NULL,
5675 files_xfer_used INTEGER NOT NULL
5676 );
5677 EOS
5678
5679 unless (close($fh)) {
5680 die("Can't write $db_script: $!");
5681 }
5682
5683 } else {
5684 die("Can't open $db_script: $!");
5685 }
5686
5687 my $cmd = "sqlite3 $db_file < $db_script";
5688
5689 if ($ENV{TEST_VERBOSE}) {
5690 print STDERR "Executing sqlite3: $cmd\n";
5691 }
5692
5693 my @output = `$cmd`;
5694 if (scalar(@output) &&
5695 $ENV{TEST_VERBOSE}) {
5696 print STDERR "Output: ", join('', @output), "\n";
5697 }
5698
5699 my $config = {
5700 PidFile => $pid_file,
5701 ScoreboardFile => $scoreboard_file,
5702 SystemLog => $log_file,
5703
5704 DefaultChdir => '~',
5705
5706 IfModules => {
5707 'mod_delay.c' => {
5708 DelayEngine => 'off',
5709 },
5710
5711 'mod_quotatab_sql.c' => [
5712 '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}\'"',
5713 '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}\'"',
5714 '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',
5715 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
5716
5717 'QuotaEngine on',
5718 "QuotaLog $log_file",
5719 'QuotaLimitTable sql:/get-quota-limit',
5720 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
5721 ],
5722
5723 'mod_sql.c' => {
5724 SQLAuthTypes => 'plaintext',
5725 SQLBackend => 'sqlite3',
5726 SQLConnectInfo => $db_file,
5727 SQLLogFile => $log_file,
5728 SQLMinID => '0',
5729 },
5730 },
5731 };
5732
5733 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5734
5735 # Open pipes, for use between the parent and child processes. Specifically,
5736 # the child will indicate when it's done with its test by writing a message
5737 # to the parent.
5738 my ($rfh, $wfh);
5739 unless (pipe($rfh, $wfh)) {
5740 die("Can't open pipe: $!");
5741 }
5742
5743 my $ex;
5744
5745 # Fork child
5746 $self->handle_sigchld();
5747 defined(my $pid = fork()) or die("Can't fork: $!");
5748 if ($pid) {
5749 eval {
5750 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5751
5752 # Login as user1, and upload a file
5753 $client->login($user1, $passwd);
5754
5755 my $conn = $client->stor_raw('test.txt');
5756 unless ($conn) {
5757 die("Failed to STOR test.txt: " . $client->response_code() . " " .
5758 $client->response_msg());
5759 }
5760
5761 my $buf = "Hello, World\n";
5762 $conn->write($buf, length($buf), 25);
5763 eval { $conn->close() };
5764
5765 my $resp_code = $client->response_code();
5766 my $resp_msg = $client->response_msg();
5767
5768 $self->assert_transfer_ok($resp_code, $resp_msg);
5769 $client->quit();
5770
5771 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5772
5773 # Login as user2, and upload a file
5774 $client->login($user2, $passwd);
5775
5776 $conn = $client->stor_raw('test.txt');
5777 if ($conn) {
5778 die("STOR test.txt succeeded unexpectedly");
5779 }
5780
5781 $resp_code = $client->response_code();
5782 $resp_msg = $client->response_msg();
5783
5784 my $expected = 552;
5785 $self->assert($expected == $resp_code,
5786 test_msg("Expected $expected, got $resp_code"));
5787
5788 $expected = 'STOR denied: quota exceeded: used \S+ of \S+ upload bytes';
5789 $self->assert(qr/$expected/, $resp_msg,
5790 test_msg("Expected '$expected', got '$resp_msg'"));
5791
5792 $client->quit();
5793 };
5794
5795 if ($@) {
5796 $ex = $@;
5797 }
5798
5799 $wfh->print("done\n");
5800 $wfh->flush();
5801
5802 } else {
5803 eval { server_wait($config_file, $rfh) };
5804 if ($@) {
5805 warn($@);
5806 exit 1;
5807 }
5808
5809 exit 0;
5810 }
5811
5812 # Stop server
5813 server_stop($pid_file);
5814
5815 $self->assert_child_ok($pid);
5816
5817 if ($ex) {
5818 test_append_logfile($log_file, $ex);
5819 unlink($log_file);
5820
5821 die($ex);
5822 }
5823
5824 unlink($log_file);
5825 }
5826
5827 sub quotatab_stor_ok_all_limit_bytes_in_exceeded_hard_limit {
5828 my $self = shift;
5829 my $tmpdir = $self->{tmpdir};
5830
5831 my $config_file = "$tmpdir/quotatab.conf";
5832 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
5833 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
5834
5835 my $log_file = test_get_logfile();
5836
5837 my $user1 = 'proftpd';
5838 my $group = 'ftpd';
5839 my $passwd = 'test';
5840 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
5841 mkpath($home_dir1);
5842
5843 my $uid1 = 500;
5844 my $gid = 500;
5845
5846 my $user2 = 'proftpd2';
5847 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
5848 mkpath($home_dir2);
5849
5850 my $uid2 = 1000;
5851
5852 my $test_file = File::Spec->rel2abs("$tmpdir/bar/test.txt");
5853
5854 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5855
5856 # Build up sqlite3 command to create users, groups tables and populate them
5857 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5858
5859 if (open(my $fh, "> $db_script")) {
5860 print $fh <<EOS;
5861 CREATE TABLE users (
5862 userid TEXT PRIMARY KEY,
5863 passwd TEXT,
5864 uid INTEGER,
5865 gid INTEGER,
5866 homedir TEXT,
5867 shell TEXT,
5868 lastdir TEXT
5869 );
5870 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
5871 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
5872
5873 CREATE TABLE groups (
5874 groupname TEXT PRIMARY KEY,
5875 gid INTEGER,
5876 members TEXT
5877 );
5878 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
5879
5880 CREATE TABLE quotalimits (
5881 name TEXT NOT NULL PRIMARY KEY,
5882 quota_type TEXT NOT NULL,
5883 per_session TEXT NOT NULL,
5884 limit_type TEXT NOT NULL,
5885 bytes_in_avail REAL NOT NULL,
5886 bytes_out_avail REAL NOT NULL,
5887 bytes_xfer_avail REAL NOT NULL,
5888 files_in_avail INTEGER NOT NULL,
5889 files_out_avail INTEGER NOT NULL,
5890 files_xfer_avail INTEGER NOT NULL
5891 );
5892 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 ('', 'all', 'false', 'hard', 20, 0, 0, 3, 0, 0);
5893
5894 CREATE TABLE quotatallies (
5895 name TEXT NOT NULL PRIMARY KEY,
5896 quota_type TEXT NOT NULL,
5897 bytes_in_used REAL NOT NULL,
5898 bytes_out_used REAL NOT NULL,
5899 bytes_xfer_used REAL NOT NULL,
5900 files_in_used INTEGER NOT NULL,
5901 files_out_used INTEGER NOT NULL,
5902 files_xfer_used INTEGER NOT NULL
5903 );
5904 EOS
5905
5906 unless (close($fh)) {
5907 die("Can't write $db_script: $!");
5908 }
5909
5910 } else {
5911 die("Can't open $db_script: $!");
5912 }
5913
5914 my $cmd = "sqlite3 $db_file < $db_script";
5915
5916 if ($ENV{TEST_VERBOSE}) {
5917 print STDERR "Executing sqlite3: $cmd\n";
5918 }
5919
5920 my @output = `$cmd`;
5921 if (scalar(@output) &&
5922 $ENV{TEST_VERBOSE}) {
5923 print STDERR "Output: ", join('', @output), "\n";
5924 }
5925
5926 my $config = {
5927 PidFile => $pid_file,
5928 ScoreboardFile => $scoreboard_file,
5929 SystemLog => $log_file,
5930
5931 DefaultChdir => '~',
5932
5933 IfModules => {
5934 'mod_delay.c' => {
5935 DelayEngine => 'off',
5936 },
5937
5938 'mod_quotatab_sql.c' => [
5939 '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}\'"',
5940 '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}\'"',
5941 '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',
5942 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
5943
5944 'QuotaEngine on',
5945 "QuotaLog $log_file",
5946 'QuotaLimitTable sql:/get-quota-limit',
5947 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
5948 ],
5949
5950 'mod_sql.c' => {
5951 SQLAuthTypes => 'plaintext',
5952 SQLBackend => 'sqlite3',
5953 SQLConnectInfo => $db_file,
5954 SQLLogFile => $log_file,
5955 SQLMinID => '0',
5956 },
5957 },
5958 };
5959
5960 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5961
5962 # Open pipes, for use between the parent and child processes. Specifically,
5963 # the child will indicate when it's done with its test by writing a message
5964 # to the parent.
5965 my ($rfh, $wfh);
5966 unless (pipe($rfh, $wfh)) {
5967 die("Can't open pipe: $!");
5968 }
5969
5970 my $ex;
5971
5972 # Fork child
5973 $self->handle_sigchld();
5974 defined(my $pid = fork()) or die("Can't fork: $!");
5975 if ($pid) {
5976 eval {
5977 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5978
5979 # Login as user1, and upload a file
5980 $client->login($user1, $passwd);
5981
5982 my $conn = $client->stor_raw('test.txt');
5983 unless ($conn) {
5984 die("Failed to STOR test.txt: " . $client->response_code() . " " .
5985 $client->response_msg());
5986 }
5987
5988 my $buf = "Hello, World\n";
5989 $conn->write($buf, length($buf), 25);
5990 eval { $conn->close() };
5991
5992 my $resp_code = $client->response_code();
5993 my $resp_msg = $client->response_msg();
5994
5995 $self->assert_transfer_ok($resp_code, $resp_msg);
5996 $client->quit();
5997
5998 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5999
6000 # Login as user2, and upload a file
6001 $client->login($user2, $passwd);
6002
6003 $conn = $client->stor_raw('test.txt');
6004 unless ($conn) {
6005 die("Failed to STOR test.txt: " . $client->response_code() . " " .
6006 $client->response_msg());
6007 }
6008
6009 $buf = "Hello, World\n";
6010 $conn->write($buf, length($buf), 25);
6011 eval { $conn->close() };
6012
6013 $resp_code = $client->response_code();
6014 $resp_msg = $client->response_msg();
6015
6016 my $expected = 552;
6017 $self->assert($expected == $resp_code,
6018 test_msg("Expected $expected, got $resp_code"));
6019
6020 $expected = 'Transfer aborted. (Disc|Disk) quota exceeded';
6021 $self->assert(qr/$expected/, $resp_msg,
6022 test_msg("Expected '$expected', got '$resp_msg'"));
6023
6024 $client->quit();
6025
6026 if (-f $test_file) {
6027 die("$test_file exists, should have been deleted");
6028 }
6029 };
6030
6031 if ($@) {
6032 $ex = $@;
6033 }
6034
6035 $wfh->print("done\n");
6036 $wfh->flush();
6037
6038 } else {
6039 eval { server_wait($config_file, $rfh) };
6040 if ($@) {
6041 warn($@);
6042 exit 1;
6043 }
6044
6045 exit 0;
6046 }
6047
6048 # Stop server
6049 server_stop($pid_file);
6050
6051 $self->assert_child_ok($pid);
6052
6053 if ($ex) {
6054 test_append_logfile($log_file, $ex);
6055 unlink($log_file);
6056
6057 die($ex);
6058 }
6059
6060 unlink($log_file);
6061 }
6062
6063 sub quotatab_stor_ok_all_limit_files_in_exceeded {
6064 my $self = shift;
6065 my $tmpdir = $self->{tmpdir};
6066
6067 my $config_file = "$tmpdir/quotatab.conf";
6068 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
6069 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
6070
6071 my $log_file = test_get_logfile();
6072
6073 my $user1 = 'proftpd';
6074 my $group = 'ftpd';
6075 my $passwd = 'test';
6076 my $home_dir1 = File::Spec->rel2abs("$tmpdir/foo");
6077 mkpath($home_dir1);
6078
6079 my $uid1 = 500;
6080 my $gid = 500;
6081
6082 my $user2 = 'proftpd2';
6083 my $home_dir2 = File::Spec->rel2abs("$tmpdir/bar");
6084 mkpath($home_dir2);
6085
6086 my $uid2 = 1000;
6087
6088 my $test_file = File::Spec->rel2abs("$tmpdir/bar/test.txt");
6089
6090 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6091
6092 # Build up sqlite3 command to create users, groups tables and populate them
6093 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6094
6095 if (open(my $fh, "> $db_script")) {
6096 print $fh <<EOS;
6097 CREATE TABLE users (
6098 userid TEXT PRIMARY KEY,
6099 passwd TEXT,
6100 uid INTEGER,
6101 gid INTEGER,
6102 homedir TEXT,
6103 shell TEXT,
6104 lastdir TEXT
6105 );
6106 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user1', '$passwd', $uid1, $gid, '$home_dir1', '/bin/bash');
6107 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user2', '$passwd', $uid2, $gid, '$home_dir2', '/bin/bash');
6108
6109 CREATE TABLE groups (
6110 groupname TEXT PRIMARY KEY,
6111 gid INTEGER,
6112 members TEXT
6113 );
6114 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '');
6115
6116 CREATE TABLE quotalimits (
6117 name TEXT NOT NULL PRIMARY KEY,
6118 quota_type TEXT NOT NULL,
6119 per_session TEXT NOT NULL,
6120 limit_type TEXT NOT NULL,
6121 bytes_in_avail REAL NOT NULL,
6122 bytes_out_avail REAL NOT NULL,
6123 bytes_xfer_avail REAL NOT NULL,
6124 files_in_avail INTEGER NOT NULL,
6125 files_out_avail INTEGER NOT NULL,
6126 files_xfer_avail INTEGER NOT NULL
6127 );
6128 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 ('', 'all', 'false', 'hard', 32, 0, 0, 1, 0, 0);
6129
6130 CREATE TABLE quotatallies (
6131 name TEXT NOT NULL PRIMARY KEY,
6132 quota_type TEXT NOT NULL,
6133 bytes_in_used REAL NOT NULL,
6134 bytes_out_used REAL NOT NULL,
6135 bytes_xfer_used REAL NOT NULL,
6136 files_in_used INTEGER NOT NULL,
6137 files_out_used INTEGER NOT NULL,
6138 files_xfer_used INTEGER NOT NULL
6139 );
6140 EOS
6141
6142 unless (close($fh)) {
6143 die("Can't write $db_script: $!");
6144 }
6145
6146 } else {
6147 die("Can't open $db_script: $!");
6148 }
6149
6150 my $cmd = "sqlite3 $db_file < $db_script";
6151
6152 if ($ENV{TEST_VERBOSE}) {
6153 print STDERR "Executing sqlite3: $cmd\n";
6154 }
6155
6156 my @output = `$cmd`;
6157 if (scalar(@output) &&
6158 $ENV{TEST_VERBOSE}) {
6159 print STDERR "Output: ", join('', @output), "\n";
6160 }
6161
6162 my $config = {
6163 PidFile => $pid_file,
6164 ScoreboardFile => $scoreboard_file,
6165 SystemLog => $log_file,
6166
6167 DefaultChdir => '~',
6168
6169 IfModules => {
6170 'mod_delay.c' => {
6171 DelayEngine => 'off',
6172 },
6173
6174 'mod_quotatab_sql.c' => [
6175 '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}\'"',
6176 '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}\'"',
6177 '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',
6178 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
6179
6180 'QuotaEngine on',
6181 "QuotaLog $log_file",
6182 'QuotaLimitTable sql:/get-quota-limit',
6183 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
6184 ],
6185
6186 'mod_sql.c' => {
6187 SQLAuthTypes => 'plaintext',
6188 SQLBackend => 'sqlite3',
6189 SQLConnectInfo => $db_file,
6190 SQLLogFile => $log_file,
6191 SQLMinID => '0',
6192 },
6193 },
6194 };
6195
6196 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6197
6198 # Open pipes, for use between the parent and child processes. Specifically,
6199 # the child will indicate when it's done with its test by writing a message
6200 # to the parent.
6201 my ($rfh, $wfh);
6202 unless (pipe($rfh, $wfh)) {
6203 die("Can't open pipe: $!");
6204 }
6205
6206 my $ex;
6207
6208 # Fork child
6209 $self->handle_sigchld();
6210 defined(my $pid = fork()) or die("Can't fork: $!");
6211 if ($pid) {
6212 eval {
6213 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6214
6215 # Login as user1, and upload a file
6216 $client->login($user1, $passwd);
6217
6218 my $conn = $client->stor_raw('test.txt');
6219 unless ($conn) {
6220 die("Failed to STOR test.txt: " . $client->response_code() . " " .
6221 $client->response_msg());
6222 }
6223
6224 my $buf = "Hello, World\n";
6225 $conn->write($buf, length($buf), 25);
6226 eval { $conn->close() };
6227
6228 my $resp_code = $client->response_code();
6229 my $resp_msg = $client->response_msg();
6230
6231 $self->assert_transfer_ok($resp_code, $resp_msg);
6232 $client->quit();
6233
6234 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6235
6236 # Login as user2, and upload a file
6237 $client->login($user2, $passwd);
6238
6239 $conn = $client->stor_raw('test2.txt');
6240 if ($conn) {
6241 die("STOR test2.txt succeeded unexpectedly");
6242 }
6243
6244 $resp_code = $client->response_code();
6245 $resp_msg = $client->response_msg();
6246
6247 my $expected = 552;
6248 $self->assert($expected == $resp_code,
6249 test_msg("Expected $expected, got $resp_code"));
6250
6251 $expected = 'STOR: notice: quota reached: used \d+ of \d+ upload file';
6252 $self->assert(qr/$expected/, $resp_msg,
6253 test_msg("Expected '$expected', got '$resp_msg'"));
6254 };
6255
6256 if ($@) {
6257 $ex = $@;
6258 }
6259
6260 $wfh->print("done\n");
6261 $wfh->flush();
6262
6263 } else {
6264 eval { server_wait($config_file, $rfh) };
6265 if ($@) {
6266 warn($@);
6267 exit 1;
6268 }
6269
6270 exit 0;
6271 }
6272
6273 # Stop server
6274 server_stop($pid_file);
6275
6276 $self->assert_child_ok($pid);
6277
6278 if ($ex) {
6279 test_append_logfile($log_file, $ex);
6280 unlink($log_file);
6281
6282 die($ex);
6283 }
6284
6285 unlink($log_file);
6286 }
6287
6288 sub quotatab_stor_bug3164 {
6289 my $self = shift;
6290 my $tmpdir = $self->{tmpdir};
6291
6292 my $config_file = "$tmpdir/quotatab.conf";
6293 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
6294 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
6295
6296 my $log_file = test_get_logfile();
6297
6298 my $user = 'proftpd';
6299 my $passwd = 'test';
6300 my $group = 'ftpd';
6301 my $home_dir = File::Spec->rel2abs($tmpdir);
6302 my $uid = 500;
6303 my $gid = 500;
6304
6305 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6306
6307 # Build up sqlite3 command to create users, groups tables and populate them
6308 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6309
6310 if (open(my $fh, "> $db_script")) {
6311 print $fh <<EOS;
6312 CREATE TABLE users (
6313 userid TEXT PRIMARY KEY,
6314 passwd TEXT,
6315 uid INTEGER,
6316 gid INTEGER,
6317 homedir TEXT,
6318 shell TEXT,
6319 lastdir TEXT
6320 );
6321 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
6322
6323 CREATE TABLE groups (
6324 groupname TEXT PRIMARY KEY,
6325 gid INTEGER,
6326 members TEXT
6327 );
6328 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
6329
6330 CREATE TABLE quotalimits (
6331 name TEXT NOT NULL PRIMARY KEY,
6332 quota_type TEXT NOT NULL,
6333 per_session TEXT NOT NULL,
6334 limit_type TEXT NOT NULL,
6335 bytes_in_avail REAL NOT NULL,
6336 bytes_out_avail REAL NOT NULL,
6337 bytes_xfer_avail REAL NOT NULL,
6338 files_in_avail INTEGER NOT NULL,
6339 files_out_avail INTEGER NOT NULL,
6340 files_xfer_avail INTEGER NOT NULL
6341 );
6342 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);
6343
6344 CREATE TABLE quotatallies (
6345 name TEXT NOT NULL PRIMARY KEY,
6346 quota_type TEXT NOT NULL,
6347 bytes_in_used REAL NOT NULL,
6348 bytes_out_used REAL NOT NULL,
6349 bytes_xfer_used REAL NOT NULL,
6350 files_in_used INTEGER NOT NULL,
6351 files_out_used INTEGER NOT NULL,
6352 files_xfer_used INTEGER NOT NULL
6353 );
6354 EOS
6355
6356 unless (close($fh)) {
6357 die("Can't write $db_script: $!");
6358 }
6359
6360 } else {
6361 die("Can't open $db_script: $!");
6362 }
6363
6364 my $cmd = "sqlite3 $db_file < $db_script";
6365
6366 if ($ENV{TEST_VERBOSE}) {
6367 print STDERR "Executing sqlite3: $cmd\n";
6368 }
6369
6370 my @output = `$cmd`;
6371 if (scalar(@output) &&
6372 $ENV{TEST_VERBOSE}) {
6373 print STDERR "Output: ", join('', @output), "\n";
6374 }
6375
6376 my $config = {
6377 PidFile => $pid_file,
6378 ScoreboardFile => $scoreboard_file,
6379 SystemLog => $log_file,
6380
6381 DefaultChdir => '~',
6382
6383 IfModules => {
6384 'mod_delay.c' => {
6385 DelayEngine => 'off',
6386 },
6387
6388 'mod_quotatab_sql.c' => [
6389 '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}\'"',
6390 '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}\'"',
6391 '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',
6392 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
6393
6394 'QuotaEngine on',
6395 "QuotaLog $log_file",
6396 'QuotaLimitTable sql:/get-quota-limit',
6397 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
6398 ],
6399
6400 'mod_sql.c' => {
6401 SQLAuthTypes => 'plaintext',
6402 SQLBackend => 'sqlite3',
6403 SQLConnectInfo => $db_file,
6404 SQLLogFile => $log_file,
6405 SQLMinID => '0',
6406 },
6407 },
6408 };
6409
6410 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6411
6412 # Open pipes, for use between the parent and child processes. Specifically,
6413 # the child will indicate when it's done with its test by writing a message
6414 # to the parent.
6415 my ($rfh, $wfh);
6416 unless (pipe($rfh, $wfh)) {
6417 die("Can't open pipe: $!");
6418 }
6419
6420 my $ex;
6421
6422 # Fork child
6423 $self->handle_sigchld();
6424 defined(my $pid = fork()) or die("Can't fork: $!");
6425 if ($pid) {
6426 eval {
6427 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6428 $client->login($user, $passwd);
6429
6430 $wfh->print("do_update\n");
6431 $wfh->flush();
6432
6433 sleep(1);
6434
6435 # This STOR should fail, since the out-of-band update filled up the
6436 # available file upload slots.
6437 my $conn = $client->stor_raw('test.txt');
6438 if ($conn) {
6439 die("STOR test.txt succeeded unexpectedly");
6440 }
6441
6442 my $resp_code = $client->response_code();
6443 my $resp_msg = $client->response_msg();
6444
6445 my $expected;
6446
6447 $expected = 552;
6448 $self->assert($expected == $resp_code,
6449 test_msg("Expected $expected, got $resp_code"));
6450
6451 $expected = "STOR: notice: quota reached: used 2 of 2 upload files";
6452 $self->assert($expected eq $resp_msg,
6453 test_msg("Expected '$expected', got '$resp_msg'"));
6454 };
6455
6456 if ($@) {
6457 $ex = $@;
6458 }
6459
6460 $wfh->print("done\n");
6461 $wfh->flush();
6462
6463 } else {
6464 eval { bug3164_server_wait($config_file, $db_file, $rfh) };
6465 if ($@) {
6466 warn($@);
6467 exit 1;
6468 }
6469
6470 exit 0;
6471 }
6472
6473 # Stop server
6474 server_stop($pid_file);
6475
6476 $self->assert_child_ok($pid);
6477
6478 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\'");
6479
6480 my $expected;
6481
6482 $expected = 'user';
6483 $self->assert($expected eq $quota_type,
6484 test_msg("Expected '$expected', got '$quota_type'"));
6485
6486 $expected = '^(10.0|10)$';
6487 $self->assert(qr/$expected/, $bytes_in_used,
6488 test_msg("Expected $expected bytes_in_used, got $bytes_in_used"));
6489
6490 $expected = '^(10.0|10)$';
6491 $self->assert(qr/$expected/, $bytes_out_used,
6492 test_msg("Expected $expected bytes_out_used, got $bytes_out_used"));
6493
6494 $expected = '^(10.0|10)$';
6495 $self->assert(qr/$expected/, $bytes_xfer_used,
6496 test_msg("Expected $expected bytes_xfer_used, got $bytes_xfer_used"));
6497
6498 $expected = 2;
6499 $self->assert($expected == $files_in_used,
6500 test_msg("Expected $expected files_in_used, got $files_in_used"));
6501
6502 $expected = 2;
6503 $self->assert($expected == $files_out_used,
6504 test_msg("Expected $expected files_out_used, got $files_out_used"));
6505
6506 $expected = 2;
6507 $self->assert($expected == $files_xfer_used,
6508 test_msg("Expected $expected files_xfer_used, got $files_xfer_used"));
6509
6510 if ($ex) {
6511 test_append_logfile($log_file, $ex);
6512 unlink($log_file);
6513
6514 die($ex);
6515 }
6516
6517 unlink($log_file);
6518 }
6519
6520 sub quotatab_dele_ok_user_limit {
6521 my $self = shift;
6522 my $tmpdir = $self->{tmpdir};
6523
6524 my $config_file = "$tmpdir/quotatab.conf";
6525 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
6526 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
6527
6528 my $log_file = test_get_logfile();
6529
6530 my $user = 'proftpd';
6531 my $passwd = 'test';
6532 my $group = 'ftpd';
6533 my $home_dir = File::Spec->rel2abs($tmpdir);
6534 my $uid = 500;
6535 my $gid = 500;
6536
6537 # Make sure that, if we're running as root, that the home directory has
6538 # permissions/privs set for the account we create
6539 if ($< == 0) {
6540 unless (chmod(0775, $home_dir)) {
6541 die("Can't set perms on $home_dir to 0775: $!");
6542 }
6543
6544 unless (chown($uid, $gid, $home_dir)) {
6545 die("Can't set owner of $home_dir to $uid/$gid: $!");
6546 }
6547 }
6548
6549 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6550
6551 # Build up sqlite3 command to create users, groups tables and populate them
6552 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6553
6554 if (open(my $fh, "> $db_script")) {
6555 print $fh <<EOS;
6556 CREATE TABLE users (
6557 userid TEXT PRIMARY KEY,
6558 passwd TEXT,
6559 uid INTEGER,
6560 gid INTEGER,
6561 homedir TEXT,
6562 shell TEXT,
6563 lastdir TEXT
6564 );
6565 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
6566
6567 CREATE TABLE groups (
6568 groupname TEXT PRIMARY KEY,
6569 gid INTEGER,
6570 members TEXT
6571 );
6572 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
6573
6574 CREATE TABLE quotalimits (
6575 name TEXT NOT NULL PRIMARY KEY,
6576 quota_type TEXT NOT NULL,
6577 per_session TEXT NOT NULL,
6578 limit_type TEXT NOT NULL,
6579 bytes_in_avail REAL NOT NULL,
6580 bytes_out_avail REAL NOT NULL,
6581 bytes_xfer_avail REAL NOT NULL,
6582 files_in_avail INTEGER NOT NULL,
6583 files_out_avail INTEGER NOT NULL,
6584 files_xfer_avail INTEGER NOT NULL
6585 );
6586 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, 32, 3, 0, 3);
6587
6588 CREATE TABLE quotatallies (
6589 name TEXT NOT NULL PRIMARY KEY,
6590 quota_type TEXT NOT NULL,
6591 bytes_in_used REAL NOT NULL,
6592 bytes_out_used REAL NOT NULL,
6593 bytes_xfer_used REAL NOT NULL,
6594 files_in_used INTEGER NOT NULL,
6595 files_out_used INTEGER NOT NULL,
6596 files_xfer_used INTEGER NOT NULL
6597 );
6598
6599 EOS
6600
6601 unless (close($fh)) {
6602 die("Can't write $db_script: $!");
6603 }
6604
6605 } else {
6606 die("Can't open $db_script: $!");
6607 }
6608
6609 my $cmd = "sqlite3 $db_file < $db_script";
6610
6611 if ($ENV{TEST_VERBOSE}) {
6612 print STDERR "Executing sqlite3: $cmd\n";
6613 }
6614
6615 my @output = `$cmd`;
6616 if (scalar(@output) &&
6617 $ENV{TEST_VERBOSE}) {
6618 print STDERR "Output: ", join('', @output), "\n";
6619 }
6620
6621 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
6622 if (open(my $fh, "> $test_file")) {
6623 print $fh "Hello, World!\n";
6624
6625 unless (close($fh)) {
6626 die("Can't write $test_file: $!");
6627 }
6628
6629 } else {
6630 die("Can't open $test_file: $!");
6631 }
6632
6633 my $test_sz = (stat($test_file))[7];
6634
6635 my $config = {
6636 PidFile => $pid_file,
6637 ScoreboardFile => $scoreboard_file,
6638 SystemLog => $log_file,
6639
6640 DefaultChdir => '~',
6641
6642 IfModules => {
6643 'mod_delay.c' => {
6644 DelayEngine => 'off',
6645 },
6646
6647 'mod_quotatab_sql.c' => [
6648 '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}\'"',
6649 '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}\'"',
6650 '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',
6651 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
6652
6653 'QuotaEngine on',
6654 "QuotaLog $log_file",
6655 'QuotaLimitTable sql:/get-quota-limit',
6656 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
6657 ],
6658
6659 'mod_sql.c' => {
6660 SQLAuthTypes => 'plaintext',
6661 SQLBackend => 'sqlite3',
6662 SQLConnectInfo => $db_file,
6663 SQLLogFile => $log_file,
6664 SQLMinID => '0',
6665 },
6666 },
6667 };
6668
6669 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6670
6671 # Open pipes, for use between the parent and child processes. Specifically,
6672 # the child will indicate when it's done with its test by writing a message
6673 # to the parent.
6674 my ($rfh, $wfh);
6675 unless (pipe($rfh, $wfh)) {
6676 die("Can't open pipe: $!");
6677 }
6678
6679 my $ex;
6680
6681 # Fork child
6682 $self->handle_sigchld();
6683 defined(my $pid = fork()) or die("Can't fork: $!");
6684 if ($pid) {
6685 eval {
6686 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6687 $client->login($user, $passwd);
6688 $client->dele('test.txt');
6689
6690 my $resp_code = $client->response_code();
6691 my $resp_msg = $client->response_msg();
6692
6693 $client->quit();
6694
6695 my $expected;
6696
6697 $expected = 250;
6698 $self->assert($expected == $resp_code,
6699 test_msg("Expected $expected, got $resp_code"));
6700
6701 $expected = "DELE command successful";
6702 $self->assert($expected eq $resp_msg,
6703 test_msg("Expected '$expected', got '$resp_msg'"));
6704 };
6705
6706 if ($@) {
6707 $ex = $@;
6708 }
6709
6710 $wfh->print("done\n");
6711 $wfh->flush();
6712
6713 } else {
6714 eval { server_wait($config_file, $rfh) };
6715 if ($@) {
6716 warn($@);
6717 exit 1;
6718 }
6719
6720 exit 0;
6721 }
6722
6723 # Stop server
6724 server_stop($pid_file);
6725
6726 $self->assert_child_ok($pid);
6727
6728 if ($ex) {
6729 test_append_logfile($log_file, $ex);
6730 unlink($log_file);
6731
6732 die($ex);
6733 }
6734
6735 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\'");
6736
6737 my $expected;
6738
6739 $expected = 'user';
6740 $self->assert($expected eq $quota_type,
6741 test_msg("Expected '$expected', got '$quota_type'"));
6742
6743 $expected = '^(-14.0|-14)$';
6744 $self->assert(qr/$expected/, $bytes_in_used,
6745 test_msg("Expected $expected, got $bytes_in_used"));
6746
6747 $expected = '^(0.0|0)$';
6748 $self->assert(qr/$expected/, $bytes_out_used,
6749 test_msg("Expected $expected, got $bytes_out_used"));
6750
6751 $expected = '^(0.0|0)$';
6752 $self->assert(qr/$expected/, $bytes_xfer_used,
6753 test_msg("Expected $expected, got $bytes_xfer_used"));
6754
6755 $expected = -1;
6756 $self->assert($expected == $files_in_used,
6757 test_msg("Expected $expected, got $files_in_used"));
6758
6759 $expected = 0;
6760 $self->assert($expected == $files_out_used,
6761 test_msg("Expected $expected, got $files_out_used"));
6762
6763 $expected = 0;
6764 $self->assert($expected == $files_xfer_used,
6765 test_msg("Expected $expected, got $files_xfer_used"));
6766
6767 unlink($log_file);
6768 }
6769
6770 sub quotatab_dele_user_owner_bug3161 {
6771 my $self = shift;
6772 my $tmpdir = $self->{tmpdir};
6773
6774 my $config_file = "$tmpdir/quotatab.conf";
6775 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
6776 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
6777
6778 my $log_file = test_get_logfile();
6779
6780 my $user = 'proftpd';
6781 my $passwd = 'test';
6782 my $group = 'ftpd';
6783 my $home_dir = File::Spec->rel2abs($tmpdir);
6784 my $uid = 500;
6785 my $gid = 500;
6786
6787 # Make sure that, if we're running as root, that the home directory has
6788 # permissions/privs set for the account we create
6789 if ($< == 0) {
6790 unless (chmod(0775, $home_dir)) {
6791 die("Can't set perms on $home_dir to 0775: $!");
6792 }
6793
6794 unless (chown($uid, $gid, $home_dir)) {
6795 die("Can't set owner of $home_dir to $uid/$gid: $!");
6796 }
6797 }
6798
6799 my $other_user = 'liz';
6800
6801 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6802
6803 # Build up sqlite3 command to create users, groups tables and populate them
6804 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6805
6806 if (open(my $fh, "> $db_script")) {
6807 print $fh <<EOS;
6808 CREATE TABLE users (
6809 userid TEXT PRIMARY KEY,
6810 passwd TEXT,
6811 uid INTEGER,
6812 gid INTEGER,
6813 homedir TEXT,
6814 shell TEXT,
6815 lastdir TEXT
6816 );
6817 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
6818 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$other_user', '$passwd', 777, 500, '$home_dir', '/bin/bash');
6819
6820 CREATE TABLE groups (
6821 groupname TEXT PRIMARY KEY,
6822 gid INTEGER,
6823 members TEXT
6824 );
6825 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user,$other_user');
6826
6827 CREATE TABLE quotalimits (
6828 name TEXT NOT NULL PRIMARY KEY,
6829 quota_type TEXT NOT NULL,
6830 per_session TEXT NOT NULL,
6831 limit_type TEXT NOT NULL,
6832 bytes_in_avail REAL NOT NULL,
6833 bytes_out_avail REAL NOT NULL,
6834 bytes_xfer_avail REAL NOT NULL,
6835 files_in_avail INTEGER NOT NULL,
6836 files_out_avail INTEGER NOT NULL,
6837 files_xfer_avail INTEGER NOT NULL
6838 );
6839 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);
6840 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 ('$other_user', 'user', 'false', 'soft', 32, 0, 0, 2, 0, 0);
6841
6842 CREATE TABLE quotatallies (
6843 name TEXT NOT NULL PRIMARY KEY,
6844 quota_type TEXT NOT NULL,
6845 bytes_in_used REAL NOT NULL,
6846 bytes_out_used REAL NOT NULL,
6847 bytes_xfer_used REAL NOT NULL,
6848 files_in_used INTEGER NOT NULL,
6849 files_out_used INTEGER NOT NULL,
6850 files_xfer_used INTEGER NOT NULL
6851 );
6852 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', 32, 0, 0, 2, 0, 0);
6853 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 ('$other_user', 'user', 32, 0, 0, 2, 0, 0);
6854
6855 EOS
6856
6857 unless (close($fh)) {
6858 die("Can't write $db_script: $!");
6859 }
6860
6861 } else {
6862 die("Can't open $db_script: $!");
6863 }
6864
6865 my $cmd = "sqlite3 $db_file < $db_script";
6866
6867 if ($ENV{TEST_VERBOSE}) {
6868 print STDERR "Executing sqlite3: $cmd\n";
6869 }
6870
6871 my @output = `$cmd`;
6872 if (scalar(@output) &&
6873 $ENV{TEST_VERBOSE}) {
6874 print STDERR "Output: ", join('', @output), "\n";
6875 }
6876
6877 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
6878 if (open(my $fh, "> $test_file")) {
6879 print $fh "Hello, World!\n";
6880
6881 unless (close($fh)) {
6882 die("Can't write $test_file: $!");
6883 }
6884
6885 } else {
6886 die("Can't open $test_file: $!");
6887 }
6888
6889 unless (chown(777, 500, $test_file)) {
6890 die("Can't chown() $test_file to UID 777, GID 500: $!");
6891 }
6892
6893 my $config = {
6894 PidFile => $pid_file,
6895 ScoreboardFile => $scoreboard_file,
6896 SystemLog => $log_file,
6897
6898 DefaultChdir => '~',
6899
6900 IfModules => {
6901 'mod_delay.c' => {
6902 DelayEngine => 'off',
6903 },
6904
6905 'mod_quotatab_sql.c' => [
6906 '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}\'"',
6907 '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}\'"',
6908 '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',
6909 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
6910
6911 'QuotaEngine on',
6912 "QuotaLog $log_file",
6913 'QuotaLimitTable sql:/get-quota-limit',
6914 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
6915 ],
6916
6917 'mod_sql.c' => {
6918 SQLAuthTypes => 'plaintext',
6919 SQLBackend => 'sqlite3',
6920 SQLConnectInfo => $db_file,
6921 SQLLogFile => $log_file,
6922 SQLMinID => '0',
6923 },
6924 },
6925 };
6926
6927 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6928
6929 # Open pipes, for use between the parent and child processes. Specifically,
6930 # the child will indicate when it's done with its test by writing a message
6931 # to the parent.
6932 my ($rfh, $wfh);
6933 unless (pipe($rfh, $wfh)) {
6934 die("Can't open pipe: $!");
6935 }
6936
6937 my $ex;
6938
6939 # Fork child
6940 $self->handle_sigchld();
6941 defined(my $pid = fork()) or die("Can't fork: $!");
6942 if ($pid) {
6943 eval {
6944 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6945 $client->login($user, $passwd);
6946 $client->dele('test.txt');
6947
6948 my $resp_code = $client->response_code();
6949 my $resp_msg = $client->response_msg();
6950
6951 $client->quit();
6952
6953 my $expected;
6954
6955 $expected = 250;
6956 $self->assert($expected == $resp_code,
6957 test_msg("Expected $expected, got $resp_code"));
6958
6959 $expected = "DELE command successful";
6960 $self->assert($expected eq $resp_msg,
6961 test_msg("Expected '$expected', got '$resp_msg'"));
6962 };
6963
6964 if ($@) {
6965 $ex = $@;
6966 }
6967
6968 $wfh->print("done\n");
6969 $wfh->flush();
6970
6971 } else {
6972 eval { server_wait($config_file, $rfh) };
6973 if ($@) {
6974 warn($@);
6975 exit 1;
6976 }
6977
6978 exit 0;
6979 }
6980
6981 # Stop server
6982 server_stop($pid_file);
6983
6984 $self->assert_child_ok($pid);
6985
6986 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\'");
6987
6988 my $expected;
6989
6990 $expected = 'user';
6991 $self->assert($expected eq $quota_type,
6992 test_msg("Expected '$expected', got '$quota_type'"));
6993
6994 $expected = '^(32.0|32)$';
6995 $self->assert(qr/$expected/, $bytes_in_used,
6996 test_msg("Expected $expected, got $bytes_in_used"));
6997
6998 $expected = '^(0.0|0)$';
6999 $self->assert(qr/$expected/, $bytes_out_used,
7000 test_msg("Expected $expected, got $bytes_out_used"));
7001
7002 $expected = '^(0.0|0)$';
7003 $self->assert(qr/$expected/, $bytes_xfer_used,
7004 test_msg("Expected $expected, got $bytes_xfer_used"));
7005
7006 $expected = 2;
7007 $self->assert($expected == $files_in_used,
7008 test_msg("Expected $expected, got $files_in_used"));
7009
7010 $expected = 0;
7011 $self->assert($expected == $files_out_used,
7012 test_msg("Expected $expected, got $files_out_used"));
7013
7014 $expected = 0;
7015 $self->assert($expected == $files_xfer_used,
7016 test_msg("Expected $expected, got $files_xfer_used"));
7017
7018 ($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 = \'$other_user\'");
7019
7020 $expected = 'user';
7021 $self->assert($expected eq $quota_type,
7022 test_msg("Expected '$expected', got '$quota_type'"));
7023
7024 $expected = '^(18.0|18)$';
7025 $self->assert(qr/$expected/, $bytes_in_used,
7026 test_msg("Expected $expected, got $bytes_in_used"));
7027
7028 $expected = '^(0.0|0)$';
7029 $self->assert(qr/$expected/, $bytes_out_used,
7030 test_msg("Expected $expected, got $bytes_out_used"));
7031
7032 $expected = '^(0.0|0)$';
7033 $self->assert(qr/$expected/, $bytes_xfer_used,
7034 test_msg("Expected $expected, got $bytes_xfer_used"));
7035
7036 $expected = 1;
7037 $self->assert($expected == $files_in_used,
7038 test_msg("Expected $expected, got $files_in_used"));
7039
7040 $expected = 0;
7041 $self->assert($expected == $files_out_used,
7042 test_msg("Expected $expected, got $files_out_used"));
7043
7044 $expected = 0;
7045 $self->assert($expected == $files_xfer_used,
7046 test_msg("Expected $expected, got $files_xfer_used"));
7047
7048 if ($ex) {
7049 test_append_logfile($log_file, $ex);
7050 unlink($log_file);
7051
7052 die($ex);
7053 }
7054
7055 unlink($log_file);
7056 }
7057
7058 sub quotatab_dele_group_owner_bug3161 {
7059 my $self = shift;
7060 my $tmpdir = $self->{tmpdir};
7061
7062 my $config_file = "$tmpdir/quotatab.conf";
7063 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
7064 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
7065
7066 my $log_file = test_get_logfile();
7067
7068 my $user = 'proftpd';
7069 my $passwd = 'test';
7070 my $group = 'ftpd';
7071 my $home_dir = File::Spec->rel2abs($tmpdir);
7072 my $uid = 500;
7073 my $gid = 500;
7074
7075 # Make sure that, if we're running as root, that the home directory has
7076 # permissions/privs set for the account we create
7077 if ($< == 0) {
7078 unless (chmod(0775, $home_dir)) {
7079 die("Can't set perms on $home_dir to 0775: $!");
7080 }
7081
7082 unless (chown($uid, $gid, $home_dir)) {
7083 die("Can't set owner of $home_dir to $uid/$gid: $!");
7084 }
7085 }
7086
7087 my $other_user = 'liz';
7088 my $other_group = 'sxsw';
7089
7090 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7091
7092 # Build up sqlite3 command to create users, groups tables and populate them
7093 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7094
7095 if (open(my $fh, "> $db_script")) {
7096 print $fh <<EOS;
7097 CREATE TABLE users (
7098 userid TEXT PRIMARY KEY,
7099 passwd TEXT,
7100 uid INTEGER,
7101 gid INTEGER,
7102 homedir TEXT,
7103 shell TEXT,
7104 lastdir TEXT
7105 );
7106 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
7107 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$other_user', '$passwd', 777, 777, '$home_dir', '/bin/bash');
7108
7109 CREATE TABLE groups (
7110 groupname TEXT PRIMARY KEY,
7111 gid INTEGER,
7112 members TEXT
7113 );
7114 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
7115 INSERT INTO groups (groupname, gid, members) VALUES ('$other_group', 777, '$other_user');
7116
7117 CREATE TABLE quotalimits (
7118 name TEXT NOT NULL PRIMARY KEY,
7119 quota_type TEXT NOT NULL,
7120 per_session TEXT NOT NULL,
7121 limit_type TEXT NOT NULL,
7122 bytes_in_avail REAL NOT NULL,
7123 bytes_out_avail REAL NOT NULL,
7124 bytes_xfer_avail REAL NOT NULL,
7125 files_in_avail INTEGER NOT NULL,
7126 files_out_avail INTEGER NOT NULL,
7127 files_xfer_avail INTEGER NOT NULL
7128 );
7129 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);
7130 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 ('$other_group', 'group', 'false', 'soft', 32, 0, 0, 2, 0, 0);
7131
7132 CREATE TABLE quotatallies (
7133 name TEXT NOT NULL PRIMARY KEY,
7134 quota_type TEXT NOT NULL,
7135 bytes_in_used REAL NOT NULL,
7136 bytes_out_used REAL NOT NULL,
7137 bytes_xfer_used REAL NOT NULL,
7138 files_in_used INTEGER NOT NULL,
7139 files_out_used INTEGER NOT NULL,
7140 files_xfer_used INTEGER NOT NULL
7141 );
7142 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', 32, 0, 0, 2, 0, 0);
7143 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 ('$other_group', 'group', 32, 0, 0, 2, 0, 0);
7144
7145 EOS
7146
7147 unless (close($fh)) {
7148 die("Can't write $db_script: $!");
7149 }
7150
7151 } else {
7152 die("Can't open $db_script: $!");
7153 }
7154
7155 my $cmd = "sqlite3 $db_file < $db_script";
7156
7157 if ($ENV{TEST_VERBOSE}) {
7158 print STDERR "Executing sqlite3: $cmd\n";
7159 }
7160
7161 my @output = `$cmd`;
7162 if (scalar(@output) &&
7163 $ENV{TEST_VERBOSE}) {
7164 print STDERR "Output: ", join('', @output), "\n";
7165 }
7166
7167 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
7168 if (open(my $fh, "> $test_file")) {
7169 print $fh "Hello, World!\n";
7170
7171 unless (close($fh)) {
7172 die("Can't write $test_file: $!");
7173 }
7174
7175 } else {
7176 die("Can't open $test_file: $!");
7177 }
7178
7179 unless (chown(777, 777, $test_file)) {
7180 die("Can't chown() $test_file to UID 777, GID 500: $!");
7181 }
7182
7183 my $config = {
7184 PidFile => $pid_file,
7185 ScoreboardFile => $scoreboard_file,
7186 SystemLog => $log_file,
7187
7188 DefaultChdir => '~',
7189
7190 IfModules => {
7191 'mod_delay.c' => {
7192 DelayEngine => 'off',
7193 },
7194
7195 'mod_quotatab_sql.c' => [
7196 '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}\'"',
7197 '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}\'"',
7198 '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',
7199 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
7200
7201 'QuotaEngine on',
7202 "QuotaLog $log_file",
7203 'QuotaLimitTable sql:/get-quota-limit',
7204 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
7205 ],
7206
7207 'mod_sql.c' => {
7208 SQLAuthTypes => 'plaintext',
7209 SQLBackend => 'sqlite3',
7210 SQLConnectInfo => $db_file,
7211 SQLLogFile => $log_file,
7212 SQLMinID => '0',
7213 },
7214 },
7215 };
7216
7217 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7218
7219 # Open pipes, for use between the parent and child processes. Specifically,
7220 # the child will indicate when it's done with its test by writing a message
7221 # to the parent.
7222 my ($rfh, $wfh);
7223 unless (pipe($rfh, $wfh)) {
7224 die("Can't open pipe: $!");
7225 }
7226
7227 my $ex;
7228
7229 # Fork child
7230 $self->handle_sigchld();
7231 defined(my $pid = fork()) or die("Can't fork: $!");
7232 if ($pid) {
7233 eval {
7234 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7235 $client->login($user, $passwd);
7236 $client->dele('test.txt');
7237
7238 my $resp_code = $client->response_code();
7239 my $resp_msg = $client->response_msg();
7240
7241 $client->quit();
7242
7243 my $expected;
7244
7245 $expected = 250;
7246 $self->assert($expected == $resp_code,
7247 test_msg("Expected $expected, got $resp_code"));
7248
7249 $expected = "DELE command successful";
7250 $self->assert($expected eq $resp_msg,
7251 test_msg("Expected '$expected', got '$resp_msg'"));
7252 };
7253
7254 if ($@) {
7255 $ex = $@;
7256 }
7257
7258 $wfh->print("done\n");
7259 $wfh->flush();
7260
7261 } else {
7262 eval { server_wait($config_file, $rfh) };
7263 if ($@) {
7264 warn($@);
7265 exit 1;
7266 }
7267
7268 exit 0;
7269 }
7270
7271 # Stop server
7272 server_stop($pid_file);
7273
7274 $self->assert_child_ok($pid);
7275
7276 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\'");
7277
7278 my $expected;
7279
7280 $expected = 'user';
7281 $self->assert($expected eq $quota_type,
7282 test_msg("Expected '$expected', got '$quota_type'"));
7283
7284 $expected = '^(32.0|32)$';
7285 $self->assert(qr/$expected/, $bytes_in_used,
7286 test_msg("Expected $expected, got $bytes_in_used"));
7287
7288 $expected = '^(0.0|0)$';
7289 $self->assert(qr/$expected/, $bytes_out_used,
7290 test_msg("Expected $expected, got $bytes_out_used"));
7291
7292 $expected = '^(0.0|0)$';
7293 $self->assert(qr/$expected/, $bytes_xfer_used,
7294 test_msg("Expected $expected, got $bytes_xfer_used"));
7295
7296 $expected = 2;
7297 $self->assert($expected == $files_in_used,
7298 test_msg("Expected $expected, got $files_in_used"));
7299
7300 $expected = 0;
7301 $self->assert($expected == $files_out_used,
7302 test_msg("Expected $expected, got $files_out_used"));
7303
7304 $expected = 0;
7305 $self->assert($expected == $files_xfer_used,
7306 test_msg("Expected $expected, got $files_xfer_used"));
7307
7308 ($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 = \'$other_group\'");
7309
7310 $expected = 'group';
7311 $self->assert($expected eq $quota_type,
7312 test_msg("Expected '$expected', got '$quota_type'"));
7313
7314 $expected = '^(18.0|18)$';
7315 $self->assert(qr/$expected/, $bytes_in_used,
7316 test_msg("Expected $expected, got $bytes_in_used"));
7317
7318 $expected = '^(0.0|0)$';
7319 $self->assert(qr/$expected/, $bytes_out_used,
7320 test_msg("Expected $expected, got $bytes_out_used"));
7321
7322 $expected = '^(0.0|0)$';
7323 $self->assert(qr/$expected/, $bytes_xfer_used,
7324 test_msg("Expected $expected, got $bytes_xfer_used"));
7325
7326 $expected = 1;
7327 $self->assert($expected == $files_in_used,
7328 test_msg("Expected $expected, got $files_in_used"));
7329
7330 $expected = 0;
7331 $self->assert($expected == $files_out_used,
7332 test_msg("Expected $expected, got $files_out_used"));
7333
7334 $expected = 0;
7335 $self->assert($expected == $files_xfer_used,
7336 test_msg("Expected $expected, got $files_xfer_used"));
7337
7338 if ($ex) {
7339 test_append_logfile($log_file, $ex);
7340 unlink($log_file);
7341
7342 die($ex);
7343 }
7344
7345 unlink($log_file);
7346 }
7347
7348 sub quotatab_new_tally_lock_bug3086 {
7349 my $self = shift;
7350 my $tmpdir = $self->{tmpdir};
7351
7352 my $config_file = "$tmpdir/quotatab.conf";
7353 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
7354 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
7355
7356 my $log_file = test_get_logfile();
7357
7358 my $user = 'proftpd';
7359 my $passwd = 'test';
7360 my $group = 'ftpd';
7361 my $home_dir = File::Spec->rel2abs($tmpdir);
7362 my $uid = 500;
7363 my $gid = 500;
7364
7365 # Make sure that, if we're running as root, that the home directory has
7366 # permissions/privs set for the account we create
7367 if ($< == 0) {
7368 unless (chmod(0775, $home_dir)) {
7369 die("Can't set perms on $home_dir to 0775: $!");
7370 }
7371
7372 unless (chown($uid, $gid, $home_dir)) {
7373 die("Can't set owner of $home_dir to $uid/$gid: $!");
7374 }
7375 }
7376
7377 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7378
7379 # Build up sqlite3 command to create users, groups tables and populate them
7380 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7381
7382 if (open(my $fh, "> $db_script")) {
7383 print $fh <<EOS;
7384 CREATE TABLE users (
7385 userid TEXT PRIMARY KEY,
7386 passwd TEXT,
7387 uid INTEGER,
7388 gid INTEGER,
7389 homedir TEXT,
7390 shell TEXT,
7391 lastdir TEXT
7392 );
7393 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
7394
7395 CREATE TABLE groups (
7396 groupname TEXT PRIMARY KEY,
7397 gid INTEGER,
7398 members TEXT
7399 );
7400 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
7401
7402 CREATE TABLE quotalimits (
7403 name TEXT NOT NULL PRIMARY KEY,
7404 quota_type TEXT NOT NULL,
7405 per_session TEXT NOT NULL,
7406 limit_type TEXT NOT NULL,
7407 bytes_in_avail REAL NOT NULL,
7408 bytes_out_avail REAL NOT NULL,
7409 bytes_xfer_avail REAL NOT NULL,
7410 files_in_avail INTEGER NOT NULL,
7411 files_out_avail INTEGER NOT NULL,
7412 files_xfer_avail INTEGER NOT NULL
7413 );
7414 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);
7415
7416 CREATE TABLE quotatallies (
7417 name TEXT NOT NULL PRIMARY KEY,
7418 quota_type TEXT NOT NULL,
7419 bytes_in_used REAL NOT NULL,
7420 bytes_out_used REAL NOT NULL,
7421 bytes_xfer_used REAL NOT NULL,
7422 files_in_used INTEGER NOT NULL,
7423 files_out_used INTEGER NOT NULL,
7424 files_xfer_used INTEGER NOT NULL
7425 );
7426 EOS
7427
7428 unless (close($fh)) {
7429 die("Can't write $db_script: $!");
7430 }
7431
7432 } else {
7433 die("Can't open $db_script: $!");
7434 }
7435
7436 my $cmd = "sqlite3 $db_file < $db_script";
7437
7438 if ($ENV{TEST_VERBOSE}) {
7439 print STDERR "Executing sqlite3: $cmd\n";
7440 }
7441
7442 my @output = `$cmd`;
7443 if (scalar(@output) &&
7444 $ENV{TEST_VERBOSE}) {
7445 print STDERR "Output: ", join('', @output), "\n";
7446 }
7447
7448 my $quota_lock = File::Spec->rel2abs("$tmpdir/quota.lock");
7449
7450 my $config = {
7451 PidFile => $pid_file,
7452 ScoreboardFile => $scoreboard_file,
7453 SystemLog => $log_file,
7454 TraceLog => $log_file,
7455 Trace => 'lock:10',
7456
7457 IfModules => {
7458 'mod_delay.c' => {
7459 DelayEngine => 'off',
7460 },
7461
7462 'mod_quotatab_sql.c' => [
7463 '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}\'"',
7464 '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}\'"',
7465 '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',
7466 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
7467
7468 'QuotaEngine on',
7469 "QuotaLog $log_file",
7470 "QuotaLock $quota_lock",
7471 'QuotaLimitTable sql:/get-quota-limit',
7472 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
7473 ],
7474
7475 'mod_sql.c' => {
7476 SQLAuthTypes => 'plaintext',
7477 SQLBackend => 'sqlite3',
7478 SQLConnectInfo => $db_file,
7479 SQLLogFile => $log_file,
7480 SQLMinID => '0',
7481 },
7482 },
7483 };
7484
7485 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7486
7487 # Open pipes, for use between the parent and child processes. Specifically,
7488 # the child will indicate when it's done with its test by writing a message
7489 # to the parent.
7490 my ($rfh, $wfh);
7491 unless (pipe($rfh, $wfh)) {
7492 die("Can't open pipe: $!");
7493 }
7494
7495 my $ex;
7496
7497 # Fork child
7498 $self->handle_sigchld();
7499 defined(my $pid = fork()) or die("Can't fork: $!");
7500 if ($pid) {
7501 eval {
7502 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7503 $client->login($user, $passwd);
7504 $client->quit();
7505
7506 # Make sure the QuotaLock file now exists. */
7507 $self->assert(-f $quota_lock,
7508 test_msg("$quota_lock file does not exist as expected"));
7509 };
7510
7511 if ($@) {
7512 $ex = $@;
7513 }
7514
7515 $wfh->print("done\n");
7516 $wfh->flush();
7517
7518 } else {
7519 eval { server_wait($config_file, $rfh) };
7520 if ($@) {
7521 warn($@);
7522 exit 1;
7523 }
7524
7525 exit 0;
7526 }
7527
7528 # Stop server
7529 server_stop($pid_file);
7530
7531 $self->assert_child_ok($pid);
7532
7533 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\'");
7534
7535 my $expected;
7536
7537 $expected = 'user';
7538 $self->assert($expected eq $quota_type,
7539 test_msg("Expected '$expected', got '$quota_type'"));
7540
7541 $expected = '^(0.0|0)$';
7542 $self->assert(qr/$expected/, $bytes_in_used,
7543 test_msg("Expected $expected, got $bytes_in_used"));
7544
7545 $expected = '^(0.0|0)$';
7546 $self->assert(qr/$expected/, $bytes_out_used,
7547 test_msg("Expected $expected, got $bytes_out_used"));
7548
7549 $expected = '^(0.0|0)$';
7550 $self->assert(qr/$expected/, $bytes_xfer_used,
7551 test_msg("Expected $expected, got $bytes_xfer_used"));
7552
7553 $expected = 0;
7554 $self->assert($expected == $files_in_used,
7555 test_msg("Expected $expected, got $files_in_used"));
7556
7557 $expected = 0;
7558 $self->assert($expected == $files_out_used,
7559 test_msg("Expected $expected, got $files_out_used"));
7560
7561 $expected = 0;
7562 $self->assert($expected == $files_xfer_used,
7563 test_msg("Expected $expected, got $files_xfer_used"));
7564
7565 if ($ex) {
7566 test_append_logfile($log_file, $ex);
7567 unlink($log_file);
7568
7569 die($ex);
7570 }
7571
7572 unlink($log_file);
7573 }
7574
7575 sub quotatab_config_exclude_filter_bug3298 {
7576 my $self = shift;
7577 my $tmpdir = $self->{tmpdir};
7578
7579 my $config_file = "$tmpdir/quotatab.conf";
7580 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
7581 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
7582
7583 my $log_file = test_get_logfile();
7584
7585 my $user = 'proftpd';
7586 my $passwd = 'test';
7587 my $group = 'ftpd';
7588 my $home_dir = File::Spec->rel2abs($tmpdir);
7589 my $uid = 500;
7590 my $gid = 500;
7591
7592 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7593
7594 # Build up sqlite3 command to create users, groups tables and populate them
7595 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7596
7597 if (open(my $fh, "> $db_script")) {
7598 print $fh <<EOS;
7599 CREATE TABLE users (
7600 userid TEXT PRIMARY KEY,
7601 passwd TEXT,
7602 uid INTEGER,
7603 gid INTEGER,
7604 homedir TEXT,
7605 shell TEXT,
7606 lastdir TEXT
7607 );
7608 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
7609
7610 CREATE TABLE groups (
7611 groupname TEXT PRIMARY KEY,
7612 gid INTEGER,
7613 members TEXT
7614 );
7615 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
7616
7617 CREATE TABLE quotalimits (
7618 name TEXT NOT NULL PRIMARY KEY,
7619 quota_type TEXT NOT NULL,
7620 per_session TEXT NOT NULL,
7621 limit_type TEXT NOT NULL,
7622 bytes_in_avail REAL NOT NULL,
7623 bytes_out_avail REAL NOT NULL,
7624 bytes_xfer_avail REAL NOT NULL,
7625 files_in_avail INTEGER NOT NULL,
7626 files_out_avail INTEGER NOT NULL,
7627 files_xfer_avail INTEGER NOT NULL
7628 );
7629 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);
7630
7631 CREATE TABLE quotatallies (
7632 name TEXT NOT NULL PRIMARY KEY,
7633 quota_type TEXT NOT NULL,
7634 bytes_in_used REAL NOT NULL,
7635 bytes_out_used REAL NOT NULL,
7636 bytes_xfer_used REAL NOT NULL,
7637 files_in_used INTEGER NOT NULL,
7638 files_out_used INTEGER NOT NULL,
7639 files_xfer_used INTEGER NOT NULL
7640 );
7641 EOS
7642
7643 unless (close($fh)) {
7644 die("Can't write $db_script: $!");
7645 }
7646
7647 } else {
7648 die("Can't open $db_script: $!");
7649 }
7650
7651 my $cmd = "sqlite3 $db_file < $db_script";
7652
7653 if ($ENV{TEST_VERBOSE}) {
7654 print STDERR "Executing sqlite3: $cmd\n";
7655 }
7656
7657 my @output = `$cmd`;
7658 if (scalar(@output) &&
7659 $ENV{TEST_VERBOSE}) {
7660 print STDERR "Output: ", join('', @output), "\n";
7661 }
7662
7663 my $exclude_filter = $tmpdir;
7664
7665 my $config = {
7666 PidFile => $pid_file,
7667 ScoreboardFile => $scoreboard_file,
7668 SystemLog => $log_file,
7669
7670 DefaultChdir => '~',
7671
7672 IfModules => {
7673 'mod_delay.c' => {
7674 DelayEngine => 'off',
7675 },
7676
7677 'mod_quotatab_sql.c' => [
7678 '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}\'"',
7679 '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}\'"',
7680 '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',
7681 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
7682
7683 'QuotaEngine on',
7684 "QuotaLog $log_file",
7685 'QuotaLimitTable sql:/get-quota-limit',
7686 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
7687 "QuotaExcludeFilter $exclude_filter",
7688 ],
7689
7690 'mod_sql.c' => {
7691 SQLAuthTypes => 'plaintext',
7692 SQLBackend => 'sqlite3',
7693 SQLConnectInfo => $db_file,
7694 SQLLogFile => $log_file,
7695 SQLMinID => '0',
7696 },
7697 },
7698 };
7699
7700 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7701
7702 # Open pipes, for use between the parent and child processes. Specifically,
7703 # the child will indicate when it's done with its test by writing a message
7704 # to the parent.
7705 my ($rfh, $wfh);
7706 unless (pipe($rfh, $wfh)) {
7707 die("Can't open pipe: $!");
7708 }
7709
7710 my $ex;
7711
7712 # Fork child
7713 $self->handle_sigchld();
7714 defined(my $pid = fork()) or die("Can't fork: $!");
7715 if ($pid) {
7716 eval {
7717 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7718 $client->login($user, $passwd);
7719
7720 my $conn = $client->stor_raw('test.txt');
7721 unless ($conn) {
7722 die("Failed to STOR test.txt: " . $client->response_code() . " " .
7723 $client->response_msg());
7724 }
7725
7726 my $buf = "Hello, World\n";
7727 $conn->write($buf, length($buf), 25);
7728 eval { $conn->close() };
7729
7730 my $resp_code = $client->response_code();
7731 my $resp_msg = $client->response_msg();
7732
7733 $self->assert_transfer_ok($resp_code, $resp_msg);
7734 };
7735
7736 if ($@) {
7737 $ex = $@;
7738 }
7739
7740 $wfh->print("done\n");
7741 $wfh->flush();
7742
7743 } else {
7744 eval { server_wait($config_file, $rfh) };
7745 if ($@) {
7746 warn($@);
7747 exit 1;
7748 }
7749
7750 exit 0;
7751 }
7752
7753 # Stop server
7754 server_stop($pid_file);
7755
7756 $self->assert_child_ok($pid);
7757
7758 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\'");
7759
7760 my $expected;
7761
7762 $expected = 'user';
7763 $self->assert($expected eq $quota_type,
7764 test_msg("Expected '$expected', got '$quota_type'"));
7765
7766 $expected = '^(0.0|0)$';
7767 $self->assert(qr/$expected/, $bytes_in_used,
7768 test_msg("Expected $expected, got $bytes_in_used"));
7769
7770 $expected = '^(0.0|0)$';
7771 $self->assert(qr/$expected/, $bytes_out_used,
7772 test_msg("Expected $expected, got $bytes_out_used"));
7773
7774 $expected = '^(0.0|0)$';
7775 $self->assert(qr/$expected/, $bytes_xfer_used,
7776 test_msg("Expected $expected, got $bytes_xfer_used"));
7777
7778 $expected = 0;
7779 $self->assert($expected == $files_in_used,
7780 test_msg("Expected $expected, got $files_in_used"));
7781
7782 $expected = 0;
7783 $self->assert($expected == $files_out_used,
7784 test_msg("Expected $expected, got $files_out_used"));
7785
7786 $expected = 0;
7787 $self->assert($expected == $files_xfer_used,
7788 test_msg("Expected $expected, got $files_xfer_used"));
7789
7790 if ($ex) {
7791 test_append_logfile($log_file, $ex);
7792 unlink($log_file);
7793
7794 die($ex);
7795 }
7796
7797 unlink($log_file);
7798 }
7799
7800 sub quotatab_config_exclude_filter_chrooted_bug3298 {
7801 my $self = shift;
7802 my $tmpdir = $self->{tmpdir};
7803
7804 my $config_file = "$tmpdir/quotatab.conf";
7805 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
7806 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
7807
7808 my $log_file = test_get_logfile();
7809
7810 my $user = 'proftpd';
7811 my $passwd = 'test';
7812 my $group = 'ftpd';
7813 my $home_dir = File::Spec->rel2abs($tmpdir);
7814 my $uid = 500;
7815 my $gid = 500;
7816
7817 # Make sure that, if we're running as root, that the home directory has
7818 # permissions/privs set for the account we create
7819 if ($< == 0) {
7820 unless (chmod(0755, $home_dir)) {
7821 die("Can't set perms on $home_dir to 0755: $!");
7822 }
7823
7824 unless (chown($uid, $gid, $home_dir)) {
7825 die("Can't set owner of $home_dir to $uid/$gid: $!");
7826 }
7827 }
7828
7829 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7830
7831 # Build up sqlite3 command to create users, groups tables and populate them
7832 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7833
7834 if (open(my $fh, "> $db_script")) {
7835 print $fh <<EOS;
7836 CREATE TABLE users (
7837 userid TEXT PRIMARY KEY,
7838 passwd TEXT,
7839 uid INTEGER,
7840 gid INTEGER,
7841 homedir TEXT,
7842 shell TEXT,
7843 lastdir TEXT
7844 );
7845 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
7846
7847 CREATE TABLE groups (
7848 groupname TEXT PRIMARY KEY,
7849 gid INTEGER,
7850 members TEXT
7851 );
7852 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
7853
7854 CREATE TABLE quotalimits (
7855 name TEXT NOT NULL PRIMARY KEY,
7856 quota_type TEXT NOT NULL,
7857 per_session TEXT NOT NULL,
7858 limit_type TEXT NOT NULL,
7859 bytes_in_avail REAL NOT NULL,
7860 bytes_out_avail REAL NOT NULL,
7861 bytes_xfer_avail REAL NOT NULL,
7862 files_in_avail INTEGER NOT NULL,
7863 files_out_avail INTEGER NOT NULL,
7864 files_xfer_avail INTEGER NOT NULL
7865 );
7866 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);
7867
7868 CREATE TABLE quotatallies (
7869 name TEXT NOT NULL PRIMARY KEY,
7870 quota_type TEXT NOT NULL,
7871 bytes_in_used REAL NOT NULL,
7872 bytes_out_used REAL NOT NULL,
7873 bytes_xfer_used REAL NOT NULL,
7874 files_in_used INTEGER NOT NULL,
7875 files_out_used INTEGER NOT NULL,
7876 files_xfer_used INTEGER NOT NULL
7877 );
7878 EOS
7879
7880 unless (close($fh)) {
7881 die("Can't write $db_script: $!");
7882 }
7883
7884 } else {
7885 die("Can't open $db_script: $!");
7886 }
7887
7888 my $cmd = "sqlite3 $db_file < $db_script";
7889
7890 if ($ENV{TEST_VERBOSE}) {
7891 print STDERR "Executing sqlite3: $cmd\n";
7892 }
7893
7894 my @output = `$cmd`;
7895 if (scalar(@output) &&
7896 $ENV{TEST_VERBOSE}) {
7897 print STDERR "Output: ", join('', @output), "\n";
7898 }
7899
7900 my $config = {
7901 PidFile => $pid_file,
7902 ScoreboardFile => $scoreboard_file,
7903 SystemLog => $log_file,
7904
7905 DefaultRoot => '~',
7906
7907 IfModules => {
7908 'mod_delay.c' => {
7909 DelayEngine => 'off',
7910 },
7911
7912 'mod_quotatab_sql.c' => [
7913 '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}\'"',
7914 '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}\'"',
7915 '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',
7916 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
7917
7918 'QuotaEngine on',
7919 "QuotaLog $log_file",
7920 'QuotaLimitTable sql:/get-quota-limit',
7921 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
7922 "QuotaExcludeFilter $tmpdir",
7923 ],
7924
7925 'mod_sql.c' => {
7926 SQLAuthTypes => 'plaintext',
7927 SQLBackend => 'sqlite3',
7928 SQLConnectInfo => $db_file,
7929 SQLLogFile => $log_file,
7930 SQLMinID => '0',
7931 },
7932 },
7933 };
7934
7935 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7936
7937 # Open pipes, for use between the parent and child processes. Specifically,
7938 # the child will indicate when it's done with its test by writing a message
7939 # to the parent.
7940 my ($rfh, $wfh);
7941 unless (pipe($rfh, $wfh)) {
7942 die("Can't open pipe: $!");
7943 }
7944
7945 my $ex;
7946
7947 # Fork child
7948 $self->handle_sigchld();
7949 defined(my $pid = fork()) or die("Can't fork: $!");
7950 if ($pid) {
7951 eval {
7952 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7953 $client->login($user, $passwd);
7954
7955 my $conn = $client->stor_raw('test.txt');
7956 unless ($conn) {
7957 die("Failed to STOR test.txt: " . $client->response_code() . " " .
7958 $client->response_msg());
7959 }
7960
7961 my $buf = "Hello, World\n";
7962 $conn->write($buf, length($buf), 25);
7963 eval { $conn->close() };
7964
7965 my $resp_code = $client->response_code();
7966 my $resp_msg = $client->response_msg();
7967
7968 $self->assert_transfer_ok($resp_code, $resp_msg);
7969 };
7970
7971 if ($@) {
7972 $ex = $@;
7973 }
7974
7975 $wfh->print("done\n");
7976 $wfh->flush();
7977
7978 } else {
7979 eval { server_wait($config_file, $rfh) };
7980 if ($@) {
7981 warn($@);
7982 exit 1;
7983 }
7984
7985 exit 0;
7986 }
7987
7988 # Stop server
7989 server_stop($pid_file);
7990
7991 $self->assert_child_ok($pid);
7992
7993 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\'");
7994
7995 my $expected;
7996
7997 $expected = 'user';
7998 $self->assert($expected eq $quota_type,
7999 test_msg("Expected '$expected', got '$quota_type'"));
8000
8001 $expected = '^(0.0|0)$';
8002 $self->assert(qr/$expected/, $bytes_in_used,
8003 test_msg("Expected $expected, got $bytes_in_used"));
8004
8005 $expected = '^(0.0|0)$';
8006 $self->assert(qr/$expected/, $bytes_out_used,
8007 test_msg("Expected $expected, got $bytes_out_used"));
8008
8009 $expected = '^(0.0|0)$';
8010 $self->assert(qr/$expected/, $bytes_xfer_used,
8011 test_msg("Expected $expected, got $bytes_xfer_used"));
8012
8013 $expected = 0;
8014 $self->assert($expected == $files_in_used,
8015 test_msg("Expected $expected, got $files_in_used"));
8016
8017 $expected = 0;
8018 $self->assert($expected == $files_out_used,
8019 test_msg("Expected $expected, got $files_out_used"));
8020
8021 $expected = 0;
8022 $self->assert($expected == $files_xfer_used,
8023 test_msg("Expected $expected, got $files_xfer_used"));
8024
8025 if ($ex) {
8026 test_append_logfile($log_file, $ex);
8027 unlink($log_file);
8028
8029 die($ex);
8030 }
8031
8032 unlink($log_file);
8033 }
8034
8035 sub quotatab_config_exclude_filter_bug3878 {
8036 my $self = shift;
8037 my $tmpdir = $self->{tmpdir};
8038
8039 my $config_file = "$tmpdir/quotatab.conf";
8040 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
8041 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
8042
8043 my $log_file = test_get_logfile();
8044
8045 my $user = 'proftpd';
8046 my $passwd = 'test';
8047 my $group = 'ftpd';
8048 my $home_dir = File::Spec->rel2abs($tmpdir);
8049 my $uid = 500;
8050 my $gid = 500;
8051
8052 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
8053
8054 # Build up sqlite3 command to create users, groups tables and populate them
8055 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
8056
8057 if (open(my $fh, "> $db_script")) {
8058 print $fh <<EOS;
8059 CREATE TABLE users (
8060 userid TEXT PRIMARY KEY,
8061 passwd TEXT,
8062 uid INTEGER,
8063 gid INTEGER,
8064 homedir TEXT,
8065 shell TEXT,
8066 lastdir TEXT
8067 );
8068 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
8069
8070 CREATE TABLE groups (
8071 groupname TEXT PRIMARY KEY,
8072 gid INTEGER,
8073 members TEXT
8074 );
8075 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
8076
8077 CREATE TABLE quotalimits (
8078 name TEXT NOT NULL PRIMARY KEY,
8079 quota_type TEXT NOT NULL,
8080 per_session TEXT NOT NULL,
8081 limit_type TEXT NOT NULL,
8082 bytes_in_avail REAL NOT NULL,
8083 bytes_out_avail REAL NOT NULL,
8084 bytes_xfer_avail REAL NOT NULL,
8085 files_in_avail INTEGER NOT NULL,
8086 files_out_avail INTEGER NOT NULL,
8087 files_xfer_avail INTEGER NOT NULL
8088 );
8089 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, 2, 0, 0);
8090
8091 CREATE TABLE quotatallies (
8092 name TEXT NOT NULL PRIMARY KEY,
8093 quota_type TEXT NOT NULL,
8094 bytes_in_used REAL NOT NULL,
8095 bytes_out_used REAL NOT NULL,
8096 bytes_xfer_used REAL NOT NULL,
8097 files_in_used INTEGER NOT NULL,
8098 files_out_used INTEGER NOT NULL,
8099 files_xfer_used INTEGER NOT NULL
8100 );
8101 EOS
8102
8103 unless (close($fh)) {
8104 die("Can't write $db_script: $!");
8105 }
8106
8107 } else {
8108 die("Can't open $db_script: $!");
8109 }
8110
8111 my $cmd = "sqlite3 $db_file < $db_script";
8112
8113 if ($ENV{TEST_VERBOSE}) {
8114 print STDERR "Executing sqlite3: $cmd\n";
8115 }
8116
8117 my @output = `$cmd`;
8118 if (scalar(@output) &&
8119 $ENV{TEST_VERBOSE}) {
8120 print STDERR "Output: ", join('', @output), "\n";
8121 }
8122
8123 my $exclude_filter = $tmpdir;
8124
8125 my $config = {
8126 PidFile => $pid_file,
8127 ScoreboardFile => $scoreboard_file,
8128 SystemLog => $log_file,
8129
8130 DefaultChdir => '~',
8131
8132 IfModules => {
8133 'mod_delay.c' => {
8134 DelayEngine => 'off',
8135 },
8136
8137 'mod_quotatab_sql.c' => [
8138 '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}\'"',
8139 '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}\'"',
8140 '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',
8141 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
8142
8143 'QuotaEngine on',
8144 "QuotaLog $log_file",
8145 'QuotaLimitTable sql:/get-quota-limit',
8146 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
8147 "QuotaExcludeFilter $exclude_filter",
8148 ],
8149
8150 'mod_sql.c' => {
8151 SQLAuthTypes => 'plaintext',
8152 SQLBackend => 'sqlite3',
8153 SQLConnectInfo => $db_file,
8154 SQLLogFile => $log_file,
8155 SQLMinID => '0',
8156 },
8157 },
8158 };
8159
8160 my ($port, $config_user, $config_group) = config_write($config_file, $config);
8161
8162 # Open pipes, for use between the parent and child processes. Specifically,
8163 # the child will indicate when it's done with its test by writing a message
8164 # to the parent.
8165 my ($rfh, $wfh);
8166 unless (pipe($rfh, $wfh)) {
8167 die("Can't open pipe: $!");
8168 }
8169
8170 my $ex;
8171
8172 # Fork child
8173 $self->handle_sigchld();
8174 defined(my $pid = fork()) or die("Can't fork: $!");
8175 if ($pid) {
8176 eval {
8177 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8178 $client->login($user, $passwd);
8179
8180 my $conn = $client->stor_raw('test.txt');
8181 unless ($conn) {
8182 die("Failed to STOR test.txt: " . $client->response_code() . " " .
8183 $client->response_msg());
8184 }
8185
8186 my $buf = "ABCD" x 9;
8187 $conn->write($buf, length($buf), 25);
8188 eval { $conn->close() };
8189
8190 my $resp_code = $client->response_code();
8191 my $resp_msg = $client->response_msg();
8192 $self->assert_transfer_ok($resp_code, $resp_msg);
8193
8194 $client->quit();
8195 };
8196
8197 if ($@) {
8198 $ex = $@;
8199 }
8200
8201 $wfh->print("done\n");
8202 $wfh->flush();
8203
8204 } else {
8205 eval { server_wait($config_file, $rfh) };
8206 if ($@) {
8207 warn($@);
8208 exit 1;
8209 }
8210
8211 exit 0;
8212 }
8213
8214 # Stop server
8215 server_stop($pid_file);
8216
8217 $self->assert_child_ok($pid);
8218
8219 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\'");
8220
8221 my $expected;
8222
8223 $expected = 'user';
8224 $self->assert($expected eq $quota_type,
8225 test_msg("Expected '$expected', got '$quota_type'"));
8226
8227 $expected = '^(0.0|0)$';
8228 $self->assert(qr/$expected/, $bytes_in_used,
8229 test_msg("Expected $expected, got $bytes_in_used"));
8230
8231 $expected = '^(0.0|0)$';
8232 $self->assert(qr/$expected/, $bytes_out_used,
8233 test_msg("Expected $expected, got $bytes_out_used"));
8234
8235 $expected = '^(0.0|0)$';
8236 $self->assert(qr/$expected/, $bytes_xfer_used,
8237 test_msg("Expected $expected, got $bytes_xfer_used"));
8238
8239 $expected = 0;
8240 $self->assert($expected == $files_in_used,
8241 test_msg("Expected $expected, got $files_in_used"));
8242
8243 $expected = 0;
8244 $self->assert($expected == $files_out_used,
8245 test_msg("Expected $expected, got $files_out_used"));
8246
8247 $expected = 0;
8248 $self->assert($expected == $files_xfer_used,
8249 test_msg("Expected $expected, got $files_xfer_used"));
8250
8251 if ($ex) {
8252 test_append_logfile($log_file, $ex);
8253 unlink($log_file);
8254
8255 die($ex);
8256 }
8257
8258 unlink($log_file);
8259 }
8260
8261 sub quotatab_config_opt_scanonlogin {
8262 my $self = shift;
8263 my $tmpdir = $self->{tmpdir};
8264
8265 my $config_file = "$tmpdir/quotatab.conf";
8266 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
8267 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
8268
8269 my $log_file = test_get_logfile();
8270
8271 my $user = 'proftpd';
8272 my $passwd = 'test';
8273 my $group = 'ftpd';
8274 my $home_dir = File::Spec->rel2abs("$tmpdir/home/$user");
8275 mkpath($home_dir);
8276 my $uid = 500;
8277 my $gid = 500;
8278
8279 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
8280
8281 # Build up sqlite3 command to create users, groups tables and populate them
8282 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
8283
8284 if (open(my $fh, "> $db_script")) {
8285 print $fh <<EOS;
8286 CREATE TABLE users (
8287 userid TEXT PRIMARY KEY,
8288 passwd TEXT,
8289 uid INTEGER,
8290 gid INTEGER,
8291 homedir TEXT,
8292 shell TEXT,
8293 lastdir TEXT
8294 );
8295 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
8296
8297 CREATE TABLE groups (
8298 groupname TEXT PRIMARY KEY,
8299 gid INTEGER,
8300 members TEXT
8301 );
8302 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
8303
8304 CREATE TABLE quotalimits (
8305 name TEXT NOT NULL PRIMARY KEY,
8306 quota_type TEXT NOT NULL,
8307 per_session TEXT NOT NULL,
8308 limit_type TEXT NOT NULL,
8309 bytes_in_avail REAL NOT NULL,
8310 bytes_out_avail REAL NOT NULL,
8311 bytes_xfer_avail REAL NOT NULL,
8312 files_in_avail INTEGER NOT NULL,
8313 files_out_avail INTEGER NOT NULL,
8314 files_xfer_avail INTEGER NOT NULL
8315 );
8316 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);
8317
8318 CREATE TABLE quotatallies (
8319 name TEXT NOT NULL PRIMARY KEY,
8320 quota_type TEXT NOT NULL,
8321 bytes_in_used REAL NOT NULL,
8322 bytes_out_used REAL NOT NULL,
8323 bytes_xfer_used REAL NOT NULL,
8324 files_in_used INTEGER NOT NULL,
8325 files_out_used INTEGER NOT NULL,
8326 files_xfer_used INTEGER NOT NULL
8327 );
8328 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', 0, 0, 0, 0, 0, 0);
8329 EOS
8330
8331 unless (close($fh)) {
8332 die("Can't write $db_script: $!");
8333 }
8334
8335 } else {
8336 die("Can't open $db_script: $!");
8337 }
8338
8339 my $cmd = "sqlite3 $db_file < $db_script";
8340
8341 if ($ENV{TEST_VERBOSE}) {
8342 print STDERR "Executing sqlite3: $cmd\n";
8343 }
8344
8345 my @output = `$cmd`;
8346 if (scalar(@output) &&
8347 $ENV{TEST_VERBOSE}) {
8348 print STDERR "Output: ", join('', @output), "\n";
8349 }
8350
8351 my $test_file = File::Spec->rel2abs("$home_dir/welcome.txt");
8352 if (open(my $fh, "> $test_file")) {
8353 print $fh <<EOH;
8354 Hello, World. This is a simple text file used in a regression
8355 test of proftpd mod_quotatab's ScanOnLogin feature.
8356 EOH
8357 unless (close($fh)) {
8358 die("Can't write $test_file: $!");
8359 }
8360
8361 } else {
8362 die("Can't open $test_file: $!");
8363 }
8364
8365 my $sub_dir = File::Spec->rel2abs("$home_dir/subdir");
8366 mkpath($sub_dir);
8367
8368 my $test_file2 = File::Spec->rel2abs("$sub_dir/test.txt");
8369 if (open(my $fh, "> $test_file2")) {
8370 print $fh "Just another test file.\n";
8371
8372 unless (close($fh)) {
8373 die("Can't write $test_file2: $!");
8374 }
8375
8376 } else {
8377 die("Can't open $test_file2: $!");
8378 }
8379
8380 # Make sure that, if we're running as root, that the home directory has
8381 # permissions/privs set for the account we create
8382 if ($< == 0) {
8383 unless (chmod(0755, $home_dir, $sub_dir, $test_file, $test_file2)) {
8384 die("Can't set perms on $home_dir to 0755: $!");
8385 }
8386
8387 unless (chown($uid, $gid, $home_dir, $sub_dir, $test_file, $test_file2)) {
8388 die("Can't set owner of $home_dir to $uid/$gid: $!");
8389 }
8390 }
8391
8392 my $config = {
8393 PidFile => $pid_file,
8394 ScoreboardFile => $scoreboard_file,
8395 SystemLog => $log_file,
8396
8397 DefaultChdir => '~',
8398
8399 IfModules => {
8400 'mod_delay.c' => {
8401 DelayEngine => 'off',
8402 },
8403
8404 'mod_quotatab_sql.c' => [
8405 '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}\'"',
8406 '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}\'"',
8407 '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',
8408 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
8409
8410 'QuotaEngine on',
8411 "QuotaLog $log_file",
8412 "QuotaOptions ScanOnLogin",
8413 'QuotaLimitTable sql:/get-quota-limit',
8414 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
8415 ],
8416
8417 'mod_sql.c' => {
8418 SQLAuthTypes => 'plaintext',
8419 SQLBackend => 'sqlite3',
8420 SQLConnectInfo => $db_file,
8421 SQLLogFile => $log_file,
8422 SQLMinID => '0',
8423 },
8424 },
8425 };
8426
8427 my ($port, $config_user, $config_group) = config_write($config_file, $config);
8428
8429 # Open pipes, for use between the parent and child processes. Specifically,
8430 # the child will indicate when it's done with its test by writing a message
8431 # to the parent.
8432 my ($rfh, $wfh);
8433 unless (pipe($rfh, $wfh)) {
8434 die("Can't open pipe: $!");
8435 }
8436
8437 my $ex;
8438
8439 # Fork child
8440 $self->handle_sigchld();
8441 defined(my $pid = fork()) or die("Can't fork: $!");
8442 if ($pid) {
8443 eval {
8444 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8445 $client->login($user, $passwd);
8446 $client->quit();
8447 };
8448
8449 if ($@) {
8450 $ex = $@;
8451 }
8452
8453 $wfh->print("done\n");
8454 $wfh->flush();
8455
8456 } else {
8457 eval { server_wait($config_file, $rfh) };
8458 if ($@) {
8459 warn($@);
8460 exit 1;
8461 }
8462
8463 exit 0;
8464 }
8465
8466 # Stop server
8467 server_stop($pid_file);
8468
8469 $self->assert_child_ok($pid);
8470
8471 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\'");
8472
8473 my $expected;
8474
8475 $expected = 'user';
8476 $self->assert($expected eq $quota_type,
8477 test_msg("Expected '$expected', got '$quota_type'"));
8478
8479 $expected = '^(139.0|139)$';
8480 $self->assert(qr/$expected/, $bytes_in_used,
8481 test_msg("Expected $expected, got $bytes_in_used"));
8482
8483 $expected = '^(0.0|0)$';
8484 $self->assert(qr/$expected/, $bytes_out_used,
8485 test_msg("Expected $expected, got $bytes_out_used"));
8486
8487 $expected = '^(0.0|0)$';
8488 $self->assert(qr/$expected/, $bytes_xfer_used,
8489 test_msg("Expected $expected, got $bytes_xfer_used"));
8490
8491 $expected = 2;
8492 $self->assert($expected == $files_in_used,
8493 test_msg("Expected $expected, got $files_in_used"));
8494
8495 $expected = 0;
8496 $self->assert($expected == $files_out_used,
8497 test_msg("Expected $expected, got $files_out_used"));
8498
8499 $expected = 0;
8500 $self->assert($expected == $files_xfer_used,
8501 test_msg("Expected $expected, got $files_xfer_used"));
8502
8503 if ($ex) {
8504 test_append_logfile($log_file, $ex);
8505 unlink($log_file);
8506
8507 die($ex);
8508 }
8509
8510 unlink($log_file);
8511 }
8512
8513 sub quotatab_config_opt_scanonlogin_chrooted {
8514 my $self = shift;
8515 my $tmpdir = $self->{tmpdir};
8516
8517 my $config_file = "$tmpdir/quotatab.conf";
8518 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
8519 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
8520
8521 my $log_file = test_get_logfile();
8522
8523 my $user = 'proftpd';
8524 my $passwd = 'test';
8525 my $group = 'ftpd';
8526 my $home_dir = File::Spec->rel2abs("$tmpdir/home/$user");
8527 mkpath($home_dir);
8528 my $uid = 500;
8529 my $gid = 500;
8530
8531 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
8532
8533 # Build up sqlite3 command to create users, groups tables and populate them
8534 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
8535
8536 if (open(my $fh, "> $db_script")) {
8537 print $fh <<EOS;
8538 CREATE TABLE users (
8539 userid TEXT PRIMARY KEY,
8540 passwd TEXT,
8541 uid INTEGER,
8542 gid INTEGER,
8543 homedir TEXT,
8544 shell TEXT,
8545 lastdir TEXT
8546 );
8547 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
8548
8549 CREATE TABLE groups (
8550 groupname TEXT PRIMARY KEY,
8551 gid INTEGER,
8552 members TEXT
8553 );
8554 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
8555
8556 CREATE TABLE quotalimits (
8557 name TEXT NOT NULL PRIMARY KEY,
8558 quota_type TEXT NOT NULL,
8559 per_session TEXT NOT NULL,
8560 limit_type TEXT NOT NULL,
8561 bytes_in_avail REAL NOT NULL,
8562 bytes_out_avail REAL NOT NULL,
8563 bytes_xfer_avail REAL NOT NULL,
8564 files_in_avail INTEGER NOT NULL,
8565 files_out_avail INTEGER NOT NULL,
8566 files_xfer_avail INTEGER NOT NULL
8567 );
8568 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);
8569
8570 CREATE TABLE quotatallies (
8571 name TEXT NOT NULL PRIMARY KEY,
8572 quota_type TEXT NOT NULL,
8573 bytes_in_used REAL NOT NULL,
8574 bytes_out_used REAL NOT NULL,
8575 bytes_xfer_used REAL NOT NULL,
8576 files_in_used INTEGER NOT NULL,
8577 files_out_used INTEGER NOT NULL,
8578 files_xfer_used INTEGER NOT NULL
8579 );
8580 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', 0, 0, 0, 0, 0, 0);
8581 EOS
8582
8583 unless (close($fh)) {
8584 die("Can't write $db_script: $!");
8585 }
8586
8587 } else {
8588 die("Can't open $db_script: $!");
8589 }
8590
8591 my $cmd = "sqlite3 $db_file < $db_script";
8592
8593 if ($ENV{TEST_VERBOSE}) {
8594 print STDERR "Executing sqlite3: $cmd\n";
8595 }
8596
8597 my @output = `$cmd`;
8598 if (scalar(@output) &&
8599 $ENV{TEST_VERBOSE}) {
8600 print STDERR "Output: ", join('', @output), "\n";
8601 }
8602
8603 my $test_file = File::Spec->rel2abs("$home_dir/welcome.txt");
8604 if (open(my $fh, "> $test_file")) {
8605 print $fh <<EOH;
8606 Hello, World. This is a simple text file used in a regression
8607 test of proftpd mod_quotatab's ScanOnLogin feature.
8608 EOH
8609 unless (close($fh)) {
8610 die("Can't write $test_file: $!");
8611 }
8612
8613 } else {
8614 die("Can't open $test_file: $!");
8615 }
8616
8617 my $sub_dir = File::Spec->rel2abs("$home_dir/subdir");
8618 mkpath($sub_dir);
8619
8620 my $test_file2 = File::Spec->rel2abs("$sub_dir/test.txt");
8621 if (open(my $fh, "> $test_file2")) {
8622 print $fh "Just another test file.\n";
8623
8624 unless (close($fh)) {
8625 die("Can't write $test_file2: $!");
8626 }
8627
8628 } else {
8629 die("Can't open $test_file2: $!");
8630 }
8631
8632 # Make sure that, if we're running as root, that the home directory has
8633 # permissions/privs set for the account we create
8634 if ($< == 0) {
8635 unless (chmod(0755, $home_dir, $sub_dir, $test_file, $test_file2)) {
8636 die("Can't set perms on $home_dir to 0755: $!");
8637 }
8638
8639 unless (chown($uid, $gid, $home_dir, $sub_dir, $test_file, $test_file2)) {
8640 die("Can't set owner of $home_dir to $uid/$gid: $!");
8641 }
8642 }
8643
8644 my $config = {
8645 PidFile => $pid_file,
8646 ScoreboardFile => $scoreboard_file,
8647 SystemLog => $log_file,
8648
8649 DefaultRoot => '~',
8650
8651 IfModules => {
8652 'mod_delay.c' => {
8653 DelayEngine => 'off',
8654 },
8655
8656 'mod_quotatab_sql.c' => [
8657 '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}\'"',
8658 '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}\'"',
8659 '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',
8660 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
8661
8662 'QuotaEngine on',
8663 "QuotaLog $log_file",
8664 "QuotaOptions ScanOnLogin",
8665 'QuotaLimitTable sql:/get-quota-limit',
8666 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
8667 ],
8668
8669 'mod_sql.c' => {
8670 SQLAuthTypes => 'plaintext',
8671 SQLBackend => 'sqlite3',
8672 SQLConnectInfo => $db_file,
8673 SQLLogFile => $log_file,
8674 SQLMinID => '0',
8675 },
8676 },
8677 };
8678
8679 my ($port, $config_user, $config_group) = config_write($config_file, $config);
8680
8681 # Open pipes, for use between the parent and child processes. Specifically,
8682 # the child will indicate when it's done with its test by writing a message
8683 # to the parent.
8684 my ($rfh, $wfh);
8685 unless (pipe($rfh, $wfh)) {
8686 die("Can't open pipe: $!");
8687 }
8688
8689 my $ex;
8690
8691 # Fork child
8692 $self->handle_sigchld();
8693 defined(my $pid = fork()) or die("Can't fork: $!");
8694 if ($pid) {
8695 eval {
8696 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8697 $client->login($user, $passwd);
8698 $client->quit();
8699 };
8700
8701 if ($@) {
8702 $ex = $@;
8703 }
8704
8705 $wfh->print("done\n");
8706 $wfh->flush();
8707
8708 } else {
8709 eval { server_wait($config_file, $rfh) };
8710 if ($@) {
8711 warn($@);
8712 exit 1;
8713 }
8714
8715 exit 0;
8716 }
8717
8718 # Stop server
8719 server_stop($pid_file);
8720
8721 $self->assert_child_ok($pid);
8722
8723 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\'");
8724
8725 my $expected;
8726
8727 $expected = 'user';
8728 $self->assert($expected eq $quota_type,
8729 test_msg("Expected '$expected', got '$quota_type'"));
8730
8731 $expected = '^(139.0|139)$';
8732 $self->assert(qr/$expected/, $bytes_in_used,
8733 test_msg("Expected $expected, got $bytes_in_used"));
8734
8735 $expected = '^(0.0|0)$';
8736 $self->assert(qr/$expected/, $bytes_out_used,
8737 test_msg("Expected $expected, got $bytes_out_used"));
8738
8739 $expected = '^(0.0|0)$';
8740 $self->assert(qr/$expected/, $bytes_xfer_used,
8741 test_msg("Expected $expected, got $bytes_xfer_used"));
8742
8743 $expected = 2;
8744 $self->assert($expected == $files_in_used,
8745 test_msg("Expected $expected, got $files_in_used"));
8746
8747 $expected = 0;
8748 $self->assert($expected == $files_out_used,
8749 test_msg("Expected $expected, got $files_out_used"));
8750
8751 $expected = 0;
8752 $self->assert($expected == $files_xfer_used,
8753 test_msg("Expected $expected, got $files_xfer_used"));
8754
8755 if ($ex) {
8756 test_append_logfile($log_file, $ex);
8757 unlink($log_file);
8758
8759 die($ex);
8760 }
8761
8762 unlink($log_file);
8763 }
8764
8765 sub quotatab_config_opt_scanonlogin_new_tally_bug3440 {
8766 my $self = shift;
8767 my $tmpdir = $self->{tmpdir};
8768
8769 my $config_file = "$tmpdir/quotatab.conf";
8770 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
8771 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
8772
8773 my $log_file = test_get_logfile();
8774
8775 my $user = 'proftpd';
8776 my $passwd = 'test';
8777 my $group = 'ftpd';
8778 my $home_dir = File::Spec->rel2abs("$tmpdir/home/$user");
8779 mkpath($home_dir);
8780 my $uid = 500;
8781 my $gid = 500;
8782
8783 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
8784
8785 # Build up sqlite3 command to create users, groups tables and populate them
8786 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
8787
8788 if (open(my $fh, "> $db_script")) {
8789 print $fh <<EOS;
8790 CREATE TABLE users (
8791 userid TEXT PRIMARY KEY,
8792 passwd TEXT,
8793 uid INTEGER,
8794 gid INTEGER,
8795 homedir TEXT,
8796 shell TEXT,
8797 lastdir TEXT
8798 );
8799 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
8800
8801 CREATE TABLE groups (
8802 groupname TEXT PRIMARY KEY,
8803 gid INTEGER,
8804 members TEXT
8805 );
8806 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
8807
8808 CREATE TABLE quotalimits (
8809 name TEXT NOT NULL PRIMARY KEY,
8810 quota_type TEXT NOT NULL,
8811 per_session TEXT NOT NULL,
8812 limit_type TEXT NOT NULL,
8813 bytes_in_avail REAL NOT NULL,
8814 bytes_out_avail REAL NOT NULL,
8815 bytes_xfer_avail REAL NOT NULL,
8816 files_in_avail INTEGER NOT NULL,
8817 files_out_avail INTEGER NOT NULL,
8818 files_xfer_avail INTEGER NOT NULL
8819 );
8820 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);
8821
8822 CREATE TABLE quotatallies (
8823 name TEXT NOT NULL PRIMARY KEY,
8824 quota_type TEXT NOT NULL,
8825 bytes_in_used REAL NOT NULL,
8826 bytes_out_used REAL NOT NULL,
8827 bytes_xfer_used REAL NOT NULL,
8828 files_in_used INTEGER NOT NULL,
8829 files_out_used INTEGER NOT NULL,
8830 files_xfer_used INTEGER NOT NULL
8831 );
8832 EOS
8833
8834 unless (close($fh)) {
8835 die("Can't write $db_script: $!");
8836 }
8837
8838 } else {
8839 die("Can't open $db_script: $!");
8840 }
8841
8842 my $cmd = "sqlite3 $db_file < $db_script";
8843
8844 if ($ENV{TEST_VERBOSE}) {
8845 print STDERR "Executing sqlite3: $cmd\n";
8846 }
8847
8848 my @output = `$cmd`;
8849 if (scalar(@output) &&
8850 $ENV{TEST_VERBOSE}) {
8851 print STDERR "Output: ", join('', @output), "\n";
8852 }
8853
8854 my $test_file = File::Spec->rel2abs("$home_dir/welcome.txt");
8855 if (open(my $fh, "> $test_file")) {
8856 print $fh <<EOH;
8857 Hello, World. This is a simple text file used in a regression
8858 test of proftpd mod_quotatab's ScanOnLogin feature.
8859 EOH
8860 unless (close($fh)) {
8861 die("Can't write $test_file: $!");
8862 }
8863
8864 } else {
8865 die("Can't open $test_file: $!");
8866 }
8867
8868 my $sub_dir = File::Spec->rel2abs("$home_dir/subdir");
8869 mkpath($sub_dir);
8870
8871 my $test_file2 = File::Spec->rel2abs("$sub_dir/test.txt");
8872 if (open(my $fh, "> $test_file2")) {
8873 print $fh "Just another test file.\n";
8874
8875 unless (close($fh)) {
8876 die("Can't write $test_file2: $!");
8877 }
8878
8879 } else {
8880 die("Can't open $test_file2: $!");
8881 }
8882
8883 # Make sure that, if we're running as root, that the home directory has
8884 # permissions/privs set for the account we create
8885 if ($< == 0) {
8886 unless (chmod(0755, $home_dir, $sub_dir, $test_file, $test_file2)) {
8887 die("Can't set perms on $home_dir to 0755: $!");
8888 }
8889
8890 unless (chown($uid, $gid, $home_dir, $sub_dir, $test_file, $test_file2)) {
8891 die("Can't set owner of $home_dir to $uid/$gid: $!");
8892 }
8893 }
8894
8895 my $config = {
8896 PidFile => $pid_file,
8897 ScoreboardFile => $scoreboard_file,
8898 SystemLog => $log_file,
8899
8900 DefaultChdir => '~',
8901
8902 IfModules => {
8903 'mod_delay.c' => {
8904 DelayEngine => 'off',
8905 },
8906
8907 'mod_quotatab_sql.c' => [
8908 '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}\'"',
8909 '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}\'"',
8910 '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',
8911 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
8912
8913 'QuotaEngine on',
8914 "QuotaLog $log_file",
8915 "QuotaOptions ScanOnLogin",
8916 'QuotaLimitTable sql:/get-quota-limit',
8917 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
8918 ],
8919
8920 'mod_sql.c' => {
8921 SQLAuthTypes => 'plaintext',
8922 SQLBackend => 'sqlite3',
8923 SQLConnectInfo => $db_file,
8924 SQLLogFile => $log_file,
8925 SQLMinID => '0',
8926 },
8927 },
8928 };
8929
8930 my ($port, $config_user, $config_group) = config_write($config_file, $config);
8931
8932 # Open pipes, for use between the parent and child processes. Specifically,
8933 # the child will indicate when it's done with its test by writing a message
8934 # to the parent.
8935 my ($rfh, $wfh);
8936 unless (pipe($rfh, $wfh)) {
8937 die("Can't open pipe: $!");
8938 }
8939
8940 my $ex;
8941
8942 # Fork child
8943 $self->handle_sigchld();
8944 defined(my $pid = fork()) or die("Can't fork: $!");
8945 if ($pid) {
8946 eval {
8947 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8948 $client->login($user, $passwd);
8949 $client->quit();
8950 };
8951
8952 if ($@) {
8953 $ex = $@;
8954 }
8955
8956 $wfh->print("done\n");
8957 $wfh->flush();
8958
8959 } else {
8960 eval { server_wait($config_file, $rfh) };
8961 if ($@) {
8962 warn($@);
8963 exit 1;
8964 }
8965
8966 exit 0;
8967 }
8968
8969 # Stop server
8970 server_stop($pid_file);
8971
8972 $self->assert_child_ok($pid);
8973
8974 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\'");
8975
8976 my $expected;
8977
8978 $expected = 'user';
8979 $self->assert($expected eq $quota_type,
8980 test_msg("Expected '$expected', got '$quota_type'"));
8981
8982 $expected = '^(139.0|139)$';
8983 $self->assert(qr/$expected/, $bytes_in_used,
8984 test_msg("Expected $expected, got $bytes_in_used"));
8985
8986 $expected = '^(0.0|0)$';
8987 $self->assert(qr/$expected/, $bytes_out_used,
8988 test_msg("Expected $expected, got $bytes_out_used"));
8989
8990 $expected = '^(0.0|0)$';
8991 $self->assert(qr/$expected/, $bytes_xfer_used,
8992 test_msg("Expected $expected, got $bytes_xfer_used"));
8993
8994 $expected = 2;
8995 $self->assert($expected == $files_in_used,
8996 test_msg("Expected $expected, got $files_in_used"));
8997
8998 $expected = 0;
8999 $self->assert($expected == $files_out_used,
9000 test_msg("Expected $expected, got $files_out_used"));
9001
9002 $expected = 0;
9003 $self->assert($expected == $files_xfer_used,
9004 test_msg("Expected $expected, got $files_xfer_used"));
9005
9006 if ($ex) {
9007 test_append_logfile($log_file, $ex);
9008 unlink($log_file);
9009
9010 die($ex);
9011 }
9012
9013 unlink($log_file);
9014 }
9015
9016 sub quotatab_site_bug3483 {
9017 my $self = shift;
9018 my $tmpdir = $self->{tmpdir};
9019
9020 my $config_file = "$tmpdir/quotatab.conf";
9021 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
9022 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
9023
9024 my $log_file = test_get_logfile();
9025
9026 my $user = 'proftpd';
9027 my $passwd = 'test';
9028 my $group = 'ftpd';
9029 my $home_dir = File::Spec->rel2abs("$tmpdir/home/$user");
9030 mkpath($home_dir);
9031 my $uid = 500;
9032 my $gid = 500;
9033
9034 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9035
9036 # Build up sqlite3 command to create users, groups tables and populate them
9037 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9038
9039 if (open(my $fh, "> $db_script")) {
9040 print $fh <<EOS;
9041 CREATE TABLE users (
9042 userid TEXT PRIMARY KEY,
9043 passwd TEXT,
9044 uid INTEGER,
9045 gid INTEGER,
9046 homedir TEXT,
9047 shell TEXT,
9048 lastdir TEXT
9049 );
9050 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
9051
9052 CREATE TABLE groups (
9053 groupname TEXT PRIMARY KEY,
9054 gid INTEGER,
9055 members TEXT
9056 );
9057 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
9058
9059 CREATE TABLE quotalimits (
9060 name TEXT NOT NULL PRIMARY KEY,
9061 quota_type TEXT NOT NULL,
9062 per_session TEXT NOT NULL,
9063 limit_type TEXT NOT NULL,
9064 bytes_in_avail REAL NOT NULL,
9065 bytes_out_avail REAL NOT NULL,
9066 bytes_xfer_avail REAL NOT NULL,
9067 files_in_avail INTEGER NOT NULL,
9068 files_out_avail INTEGER NOT NULL,
9069 files_xfer_avail INTEGER NOT NULL
9070 );
9071 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);
9072
9073 CREATE TABLE quotatallies (
9074 name TEXT NOT NULL PRIMARY KEY,
9075 quota_type TEXT NOT NULL,
9076 bytes_in_used REAL NOT NULL,
9077 bytes_out_used REAL NOT NULL,
9078 bytes_xfer_used REAL NOT NULL,
9079 files_in_used INTEGER NOT NULL,
9080 files_out_used INTEGER NOT NULL,
9081 files_xfer_used INTEGER NOT NULL
9082 );
9083 EOS
9084
9085 unless (close($fh)) {
9086 die("Can't write $db_script: $!");
9087 }
9088
9089 } else {
9090 die("Can't open $db_script: $!");
9091 }
9092
9093 my $cmd = "sqlite3 $db_file < $db_script";
9094
9095 if ($ENV{TEST_VERBOSE}) {
9096 print STDERR "Executing sqlite3: $cmd\n";
9097 }
9098
9099 my @output = `$cmd`;
9100 if (scalar(@output) &&
9101 $ENV{TEST_VERBOSE}) {
9102 print STDERR "Output: ", join('', @output), "\n";
9103 }
9104
9105 # Make sure that, if we're running as root, that the home directory has
9106 # permissions/privs set for the account we create
9107 if ($< == 0) {
9108 unless (chmod(0755, $home_dir)) {
9109 die("Can't set perms on $home_dir to 0755: $!");
9110 }
9111
9112 unless (chown($uid, $gid, $home_dir)) {
9113 die("Can't set owner of $home_dir to $uid/$gid: $!");
9114 }
9115 }
9116
9117 my $config = {
9118 PidFile => $pid_file,
9119 ScoreboardFile => $scoreboard_file,
9120 SystemLog => $log_file,
9121
9122 IfModules => {
9123 'mod_delay.c' => {
9124 DelayEngine => 'off',
9125 },
9126
9127 'mod_quotatab_sql.c' => [
9128 '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}\'"',
9129 '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}\'"',
9130 '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',
9131 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
9132
9133 'QuotaEngine on',
9134 "QuotaLog $log_file",
9135 'QuotaLimitTable sql:/get-quota-limit',
9136 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
9137 ],
9138
9139 'mod_sql.c' => {
9140 SQLAuthTypes => 'plaintext',
9141 SQLBackend => 'sqlite3',
9142 SQLConnectInfo => $db_file,
9143 SQLLogFile => $log_file,
9144 SQLMinID => '0',
9145 },
9146 },
9147 };
9148
9149 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9150
9151 # Open pipes, for use between the parent and child processes. Specifically,
9152 # the child will indicate when it's done with its test by writing a message
9153 # to the parent.
9154 my ($rfh, $wfh);
9155 unless (pipe($rfh, $wfh)) {
9156 die("Can't open pipe: $!");
9157 }
9158
9159 my $ex;
9160
9161 # Fork child
9162 $self->handle_sigchld();
9163 defined(my $pid = fork()) or die("Can't fork: $!");
9164 if ($pid) {
9165 eval {
9166 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9167 $client->login($user, $passwd);
9168
9169 eval { $client->site("") };
9170 unless ($@) {
9171 die("SITE command succeeded unexpectedly");
9172 }
9173
9174 my $resp_code = $client->response_code();
9175 my $resp_msg = $client->response_msg();
9176
9177 my $expected;
9178
9179 $expected = 500;
9180 $self->assert($expected == $resp_code,
9181 test_msg("Expected $expected, got $resp_code"));
9182
9183 $expected = "'SITE' requires parameters";
9184 $self->assert($expected eq $resp_msg,
9185 test_msg("Expected '$expected', got '$resp_msg'"));
9186
9187 $client->quit();
9188 };
9189
9190 if ($@) {
9191 $ex = $@;
9192 }
9193
9194 $wfh->print("done\n");
9195 $wfh->flush();
9196
9197 } else {
9198 eval { server_wait($config_file, $rfh) };
9199 if ($@) {
9200 warn($@);
9201 exit 1;
9202 }
9203
9204 exit 0;
9205 }
9206
9207 # Stop server
9208 server_stop($pid_file);
9209
9210 $self->assert_child_ok($pid);
9211
9212 if ($ex) {
9213 test_append_logfile($log_file, $ex);
9214 unlink($log_file);
9215
9216 die($ex);
9217 }
9218
9219 unlink($log_file);
9220 }
9221
9222 sub quotatab_dele_failed_bug3517 {
9223 my $self = shift;
9224 my $tmpdir = $self->{tmpdir};
9225
9226 my $config_file = "$tmpdir/quotatab.conf";
9227 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
9228 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
9229
9230 my $log_file = test_get_logfile();
9231
9232 my $user = 'proftpd';
9233 my $passwd = 'test';
9234 my $group = 'ftpd';
9235 my $home_dir = File::Spec->rel2abs($tmpdir);
9236 my $uid = 500;
9237 my $gid = 500;
9238
9239 # Make sure that, if we're running as root, that the home directory has
9240 # permissions/privs set for the account we create
9241 if ($< == 0) {
9242 unless (chmod(0775, $home_dir)) {
9243 die("Can't set perms on $home_dir to 0775: $!");
9244 }
9245
9246 unless (chown($uid, $gid, $home_dir)) {
9247 die("Can't set owner of $home_dir to $uid/$gid: $!");
9248 }
9249 }
9250
9251 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9252
9253 # Build up sqlite3 command to create users, groups tables and populate them
9254 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9255
9256 if (open(my $fh, "> $db_script")) {
9257 print $fh <<EOS;
9258 CREATE TABLE users (
9259 userid TEXT PRIMARY KEY,
9260 passwd TEXT,
9261 uid INTEGER,
9262 gid INTEGER,
9263 homedir TEXT,
9264 shell TEXT,
9265 lastdir TEXT
9266 );
9267 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
9268
9269 CREATE TABLE groups (
9270 groupname TEXT PRIMARY KEY,
9271 gid INTEGER,
9272 members TEXT
9273 );
9274 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
9275
9276 CREATE TABLE quotalimits (
9277 name TEXT NOT NULL PRIMARY KEY,
9278 quota_type TEXT NOT NULL,
9279 per_session TEXT NOT NULL,
9280 limit_type TEXT NOT NULL,
9281 bytes_in_avail REAL NOT NULL,
9282 bytes_out_avail REAL NOT NULL,
9283 bytes_xfer_avail REAL NOT NULL,
9284 files_in_avail INTEGER NOT NULL,
9285 files_out_avail INTEGER NOT NULL,
9286 files_xfer_avail INTEGER NOT NULL
9287 );
9288 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);
9289
9290 CREATE TABLE quotatallies (
9291 name TEXT NOT NULL PRIMARY KEY,
9292 quota_type TEXT NOT NULL,
9293 bytes_in_used REAL NOT NULL,
9294 bytes_out_used REAL NOT NULL,
9295 bytes_xfer_used REAL NOT NULL,
9296 files_in_used INTEGER NOT NULL,
9297 files_out_used INTEGER NOT NULL,
9298 files_xfer_used INTEGER NOT NULL
9299 );
9300 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', 32, 0, 0, 2, 0, 0);
9301
9302 EOS
9303
9304 unless (close($fh)) {
9305 die("Can't write $db_script: $!");
9306 }
9307
9308 } else {
9309 die("Can't open $db_script: $!");
9310 }
9311
9312 my $cmd = "sqlite3 $db_file < $db_script";
9313
9314 if ($ENV{TEST_VERBOSE}) {
9315 print STDERR "Executing sqlite3: $cmd\n";
9316 }
9317
9318 my @output = `$cmd`;
9319 if (scalar(@output) &&
9320 $ENV{TEST_VERBOSE}) {
9321 print STDERR "Output: ", join('', @output), "\n";
9322 }
9323
9324 my $config = {
9325 PidFile => $pid_file,
9326 ScoreboardFile => $scoreboard_file,
9327 SystemLog => $log_file,
9328
9329 DefaultChdir => '~',
9330
9331 IfModules => {
9332 'mod_delay.c' => {
9333 DelayEngine => 'off',
9334 },
9335
9336 'mod_quotatab_sql.c' => [
9337 '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}\'"',
9338 '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}\'"',
9339 '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',
9340 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
9341
9342 'QuotaEngine on',
9343 "QuotaLog $log_file",
9344 'QuotaLimitTable sql:/get-quota-limit',
9345 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
9346 ],
9347
9348 'mod_sql.c' => {
9349 SQLAuthTypes => 'plaintext',
9350 SQLBackend => 'sqlite3',
9351 SQLConnectInfo => $db_file,
9352 SQLLogFile => $log_file,
9353 SQLMinID => '0',
9354 },
9355 },
9356 };
9357
9358 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9359
9360 # Open pipes, for use between the parent and child processes. Specifically,
9361 # the child will indicate when it's done with its test by writing a message
9362 # to the parent.
9363 my ($rfh, $wfh);
9364 unless (pipe($rfh, $wfh)) {
9365 die("Can't open pipe: $!");
9366 }
9367
9368 my $ex;
9369
9370 # Fork child
9371 $self->handle_sigchld();
9372 defined(my $pid = fork()) or die("Can't fork: $!");
9373 if ($pid) {
9374 eval {
9375 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9376 $client->login($user, $passwd);
9377
9378 my $test_file = 'test.txt';
9379
9380 eval { $client->dele($test_file) };
9381 unless ($@) {
9382 die("DELE $test_file succeeded unexpectedly");
9383 }
9384
9385 my $resp_code = $client->response_code();
9386 my $resp_msg = $client->response_msg();
9387
9388 $client->quit();
9389
9390 my $expected;
9391
9392 $expected = 550;
9393 $self->assert($expected == $resp_code,
9394 test_msg("Expected $expected, got $resp_code"));
9395
9396 $expected = "$test_file: No such file or directory";
9397 $self->assert($expected eq $resp_msg,
9398 test_msg("Expected '$expected', got '$resp_msg'"));
9399 };
9400
9401 if ($@) {
9402 $ex = $@;
9403 }
9404
9405 $wfh->print("done\n");
9406 $wfh->flush();
9407
9408 } else {
9409 eval { server_wait($config_file, $rfh) };
9410 if ($@) {
9411 warn($@);
9412 exit 1;
9413 }
9414
9415 exit 0;
9416 }
9417
9418 # Stop server
9419 server_stop($pid_file);
9420
9421 $self->assert_child_ok($pid);
9422
9423 if ($ex) {
9424 test_append_logfile($log_file, $ex);
9425 unlink($log_file);
9426
9427 die($ex);
9428 }
9429
9430 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\'");
9431
9432 my $expected;
9433
9434 $expected = 'user';
9435 $self->assert($expected eq $quota_type,
9436 test_msg("Expected '$expected', got '$quota_type'"));
9437
9438 $expected = '^(32.0|32)$';
9439 $self->assert(qr/$expected/, $bytes_in_used,
9440 test_msg("Expected $expected, got $bytes_in_used"));
9441
9442 $expected = '^(0.0|0)$';
9443 $self->assert(qr/$expected/, $bytes_out_used,
9444 test_msg("Expected $expected, got $bytes_out_used"));
9445
9446 $expected = '^(0.0|0)$';
9447 $self->assert(qr/$expected/, $bytes_xfer_used,
9448 test_msg("Expected $expected, got $bytes_xfer_used"));
9449
9450 $expected = 2;
9451 $self->assert($expected == $files_in_used,
9452 test_msg("Expected $expected, got $files_in_used"));
9453
9454 $expected = 0;
9455 $self->assert($expected == $files_out_used,
9456 test_msg("Expected $expected, got $files_out_used"));
9457
9458 $expected = 0;
9459 $self->assert($expected == $files_xfer_used,
9460 test_msg("Expected $expected, got $files_xfer_used"));
9461
9462 unlink($log_file);
9463 }
9464
9465 sub quotatab_sql_dele_bug3524 {
9466 my $self = shift;
9467 my $tmpdir = $self->{tmpdir};
9468
9469 my $config_file = "$tmpdir/quotatab.conf";
9470 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
9471 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
9472
9473 my $log_file = test_get_logfile();
9474
9475 my $user = 'proftpd';
9476 my $passwd = 'test';
9477 my $group = 'ftpd';
9478 my $home_dir = File::Spec->rel2abs($tmpdir);
9479 my $uid = 500;
9480 my $gid = 500;
9481
9482 # Make sure that, if we're running as root, that the home directory has
9483 # permissions/privs set for the account we create
9484 if ($< == 0) {
9485 unless (chmod(0775, $home_dir)) {
9486 die("Can't set perms on $home_dir to 0775: $!");
9487 }
9488
9489 unless (chown($uid, $gid, $home_dir)) {
9490 die("Can't set owner of $home_dir to $uid/$gid: $!");
9491 }
9492 }
9493
9494 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9495
9496 # Build up sqlite3 command to create users, groups tables and populate them
9497 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9498
9499 if (open(my $fh, "> $db_script")) {
9500 print $fh <<EOS;
9501 CREATE TABLE users (
9502 userid TEXT PRIMARY KEY,
9503 passwd TEXT,
9504 uid INTEGER,
9505 gid INTEGER,
9506 homedir TEXT,
9507 shell TEXT,
9508 lastdir TEXT
9509 );
9510 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
9511
9512 CREATE TABLE groups (
9513 groupname TEXT PRIMARY KEY,
9514 gid INTEGER,
9515 members TEXT
9516 );
9517 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
9518
9519 CREATE TABLE quotalimits (
9520 name TEXT NOT NULL PRIMARY KEY,
9521 quota_type TEXT NOT NULL,
9522 per_session TEXT NOT NULL,
9523 limit_type TEXT NOT NULL,
9524 bytes_in_avail REAL NOT NULL,
9525 bytes_out_avail REAL NOT NULL,
9526 bytes_xfer_avail REAL NOT NULL,
9527 files_in_avail INTEGER NOT NULL,
9528 files_out_avail INTEGER NOT NULL,
9529 files_xfer_avail INTEGER NOT NULL
9530 );
9531 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, 32, 3, 0, 3);
9532
9533 CREATE TABLE quotatallies (
9534 name TEXT NOT NULL PRIMARY KEY,
9535 quota_type TEXT NOT NULL,
9536 bytes_in_used REAL NOT NULL,
9537 bytes_out_used REAL NOT NULL,
9538 bytes_xfer_used REAL NOT NULL,
9539 files_in_used INTEGER NOT NULL,
9540 files_out_used INTEGER NOT NULL,
9541 files_xfer_used INTEGER NOT NULL
9542 );
9543 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', 30, 0, 30, 2, 0, 2);
9544
9545 EOS
9546
9547 unless (close($fh)) {
9548 die("Can't write $db_script: $!");
9549 }
9550
9551 } else {
9552 die("Can't open $db_script: $!");
9553 }
9554
9555 my $cmd = "sqlite3 $db_file < $db_script";
9556
9557 if ($ENV{TEST_VERBOSE}) {
9558 print STDERR "Executing sqlite3: $cmd\n";
9559 }
9560
9561 my @output = `$cmd`;
9562 if (scalar(@output) &&
9563 $ENV{TEST_VERBOSE}) {
9564 print STDERR "Output: ", join('', @output), "\n";
9565 }
9566
9567 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
9568 if (open(my $fh, "> $test_file")) {
9569 print $fh "Hello, World!\n";
9570
9571 unless (close($fh)) {
9572 die("Can't write $test_file: $!");
9573 }
9574
9575 } else {
9576 die("Can't open $test_file: $!");
9577 }
9578
9579 my $test_sz = (stat($test_file))[7];
9580
9581 my $config = {
9582 PidFile => $pid_file,
9583 ScoreboardFile => $scoreboard_file,
9584 SystemLog => $log_file,
9585
9586 DefaultChdir => '~',
9587
9588 IfModules => {
9589 'mod_delay.c' => {
9590 DelayEngine => 'off',
9591 },
9592
9593 'mod_quotatab_sql.c' => [
9594 '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}\'"',
9595 '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}\'"',
9596 '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',
9597 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
9598
9599 'QuotaEngine on',
9600 "QuotaLog $log_file",
9601 'QuotaLimitTable sql:/get-quota-limit',
9602 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
9603 ],
9604
9605 'mod_sql.c' => {
9606 SQLAuthTypes => 'plaintext',
9607 SQLBackend => 'sqlite3',
9608 SQLConnectInfo => $db_file,
9609 SQLLogFile => $log_file,
9610 SQLMinID => '0',
9611 },
9612 },
9613 };
9614
9615 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9616
9617 # Open pipes, for use between the parent and child processes. Specifically,
9618 # the child will indicate when it's done with its test by writing a message
9619 # to the parent.
9620 my ($rfh, $wfh);
9621 unless (pipe($rfh, $wfh)) {
9622 die("Can't open pipe: $!");
9623 }
9624
9625 my $ex;
9626
9627 # Fork child
9628 $self->handle_sigchld();
9629 defined(my $pid = fork()) or die("Can't fork: $!");
9630 if ($pid) {
9631 eval {
9632 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9633 $client->login($user, $passwd);
9634
9635 $client->site('QUOTA');
9636 my $resp_msgs = $client->response_msgs();
9637
9638 my $expected;
9639
9640 $expected = '\s+?Uploaded bytes:\s+?30.00/32.00';
9641 my $line = $resp_msgs->[5];
9642 chomp($line);
9643 $self->assert(qr/$expected/, $line,
9644 test_msg("Expected '$expected', got '$line'"));
9645
9646 $expected = '\s+?Uploaded files:\s+?2/3';
9647 $line = $resp_msgs->[8];
9648 chomp($line);
9649 $self->assert(qr/$expected/, $line,
9650 test_msg("Expected '$expected', got '$line'"));
9651
9652 $client->dele('test.txt');
9653
9654 my $resp_code = $client->response_code();
9655 my $resp_msg = $client->response_msg();
9656
9657 $expected = 250;
9658 $self->assert($expected == $resp_code,
9659 test_msg("Expected $expected, got $resp_code"));
9660
9661 $expected = "DELE command successful";
9662 $self->assert($expected eq $resp_msg,
9663 test_msg("Expected '$expected', got '$resp_msg'"));
9664
9665 $client->site('QUOTA');
9666 $resp_msgs = $client->response_msgs();
9667
9668 $expected = '\s+?Uploaded bytes:\s+?16.00/32.00';
9669 $line = $resp_msgs->[5];
9670 chomp($line);
9671 $self->assert(qr/$expected/, $line,
9672 test_msg("Expected '$expected', got '$line"));
9673
9674 $expected = '\s+?Uploaded files:\s+?1/3';
9675 $line = $resp_msgs->[8];
9676 chomp($line);
9677 $self->assert(qr/$expected/, $line,
9678 test_msg("Expected '$expected', got '$line'"));
9679
9680 $client->quit();
9681 };
9682
9683 if ($@) {
9684 $ex = $@;
9685 }
9686
9687 $wfh->print("done\n");
9688 $wfh->flush();
9689
9690 } else {
9691 eval { server_wait($config_file, $rfh) };
9692 if ($@) {
9693 warn($@);
9694 exit 1;
9695 }
9696
9697 exit 0;
9698 }
9699
9700 # Stop server
9701 server_stop($pid_file);
9702
9703 $self->assert_child_ok($pid);
9704
9705 if ($ex) {
9706 test_append_logfile($log_file, $ex);
9707 unlink($log_file);
9708
9709 die($ex);
9710 }
9711
9712 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\'");
9713
9714 my $expected;
9715
9716 $expected = 'user';
9717 $self->assert($expected eq $quota_type,
9718 test_msg("Expected '$expected', got '$quota_type'"));
9719
9720 $expected = '^(16.0|16)$';
9721 $self->assert(qr/$expected/, $bytes_in_used,
9722 test_msg("Expected $expected bytes_in_used, got $bytes_in_used"));
9723
9724 $expected = '^(0.0|0)$';
9725 $self->assert(qr/$expected/, $bytes_out_used,
9726 test_msg("Expected $expected bytes_out_used, got $bytes_out_used"));
9727
9728 $expected = '^(30.0|30)$';
9729 $self->assert(qr/$expected/, $bytes_xfer_used,
9730 test_msg("Expected $expected bytes_xfer_used, got $bytes_xfer_used"));
9731
9732 $expected = 1;
9733 $self->assert($expected == $files_in_used,
9734 test_msg("Expected $expected files_in_used, got $files_in_used"));
9735
9736 $expected = 0;
9737 $self->assert($expected == $files_out_used,
9738 test_msg("Expected $expected files_out_used, got $files_out_used"));
9739
9740 $expected = 2;
9741 $self->assert($expected == $files_xfer_used,
9742 test_msg("Expected $expected files_xfer_used, got $files_xfer_used"));
9743
9744 unlink($log_file);
9745 }
9746
9747 sub quotatab_stor_deleteabortedstores_conn_abor_bug3621 {
9748 my $self = shift;
9749 my $tmpdir = $self->{tmpdir};
9750
9751 my $config_file = "$tmpdir/quotatab.conf";
9752 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
9753 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
9754
9755 my $log_file = test_get_logfile();
9756
9757 my $user = 'proftpd';
9758 my $passwd = 'test';
9759 my $group = 'ftpd';
9760 my $home_dir = File::Spec->rel2abs($tmpdir);
9761 my $uid = 500;
9762 my $gid = 500;
9763
9764 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9765
9766 # Build up sqlite3 command to create users, groups tables and populate them
9767 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9768
9769 if (open(my $fh, "> $db_script")) {
9770 print $fh <<EOS;
9771 CREATE TABLE users (
9772 userid TEXT PRIMARY KEY,
9773 passwd TEXT,
9774 uid INTEGER,
9775 gid INTEGER,
9776 homedir TEXT,
9777 shell TEXT,
9778 lastdir TEXT
9779 );
9780 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
9781
9782 CREATE TABLE groups (
9783 groupname TEXT PRIMARY KEY,
9784 gid INTEGER,
9785 members TEXT
9786 );
9787 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
9788
9789 CREATE TABLE quotalimits (
9790 name TEXT NOT NULL PRIMARY KEY,
9791 quota_type TEXT NOT NULL,
9792 per_session TEXT NOT NULL,
9793 limit_type TEXT NOT NULL,
9794 bytes_in_avail REAL NOT NULL,
9795 bytes_out_avail REAL NOT NULL,
9796 bytes_xfer_avail REAL NOT NULL,
9797 files_in_avail INTEGER NOT NULL,
9798 files_out_avail INTEGER NOT NULL,
9799 files_xfer_avail INTEGER NOT NULL
9800 );
9801 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);
9802
9803 CREATE TABLE quotatallies (
9804 name TEXT NOT NULL PRIMARY KEY,
9805 quota_type TEXT NOT NULL,
9806 bytes_in_used REAL NOT NULL,
9807 bytes_out_used REAL NOT NULL,
9808 bytes_xfer_used REAL NOT NULL,
9809 files_in_used INTEGER NOT NULL,
9810 files_out_used INTEGER NOT NULL,
9811 files_xfer_used INTEGER NOT NULL
9812 );
9813 EOS
9814
9815 unless (close($fh)) {
9816 die("Can't write $db_script: $!");
9817 }
9818
9819 } else {
9820 die("Can't open $db_script: $!");
9821 }
9822
9823 my $cmd = "sqlite3 $db_file < $db_script";
9824
9825 if ($ENV{TEST_VERBOSE}) {
9826 print STDERR "Executing sqlite3: $cmd\n";
9827 }
9828
9829 my @output = `$cmd`;
9830 if (scalar(@output) &&
9831 $ENV{TEST_VERBOSE}) {
9832 print STDERR "Output: ", join('', @output), "\n";
9833 }
9834
9835 my $config = {
9836 PidFile => $pid_file,
9837 ScoreboardFile => $scoreboard_file,
9838 SystemLog => $log_file,
9839
9840 DefaultChdir => '~',
9841 DeleteAbortedStores => 'on',
9842 TimeoutLinger => 0,
9843
9844 IfModules => {
9845 'mod_delay.c' => {
9846 DelayEngine => 'off',
9847 },
9848
9849 'mod_quotatab_sql.c' => [
9850 '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}\'"',
9851 '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}\'"',
9852 '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',
9853 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
9854
9855 'QuotaEngine on',
9856 "QuotaLog $log_file",
9857 'QuotaLimitTable sql:/get-quota-limit',
9858 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
9859 ],
9860
9861 'mod_sql.c' => {
9862 SQLAuthTypes => 'plaintext',
9863 SQLBackend => 'sqlite3',
9864 SQLConnectInfo => $db_file,
9865 SQLLogFile => $log_file,
9866 SQLMinID => '0',
9867 },
9868 },
9869 };
9870
9871 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9872
9873 # Open pipes, for use between the parent and child processes. Specifically,
9874 # the child will indicate when it's done with its test by writing a message
9875 # to the parent.
9876 my ($rfh, $wfh);
9877 unless (pipe($rfh, $wfh)) {
9878 die("Can't open pipe: $!");
9879 }
9880
9881 my $ex;
9882
9883 # Fork child
9884 $self->handle_sigchld();
9885 defined(my $pid = fork()) or die("Can't fork: $!");
9886 if ($pid) {
9887 eval {
9888 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9889 $client->login($user, $passwd);
9890
9891 my $conn = $client->stor_raw('test.txt');
9892 unless ($conn) {
9893 die("Failed to STOR test.txt: " . $client->response_code() . " " .
9894 $client->response_msg());
9895 }
9896
9897 my $buf = "Hello, World\n";
9898 $conn->write($buf, length($buf), 25);
9899 $conn->abort();
9900
9901 my $resp_code = $client->response_code();
9902 my $resp_msg = $client->response_msg();
9903
9904 $self->assert_transfer_ok($resp_code, $resp_msg, 1);
9905 };
9906
9907 if ($@) {
9908 $ex = $@;
9909 }
9910
9911 $wfh->print("done\n");
9912 $wfh->flush();
9913
9914 } else {
9915 eval { server_wait($config_file, $rfh) };
9916 if ($@) {
9917 warn($@);
9918 exit 1;
9919 }
9920
9921 exit 0;
9922 }
9923
9924 # Stop server
9925 server_stop($pid_file);
9926
9927 $self->assert_child_ok($pid);
9928
9929 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\'");
9930
9931 my $expected;
9932
9933 $expected = 'user';
9934 $self->assert($expected eq $quota_type,
9935 test_msg("Expected '$expected', got '$quota_type'"));
9936
9937 $expected = '^(0.0|0)$';
9938 $self->assert(qr/$expected/, $bytes_in_used,
9939 test_msg("Expected $expected, got $bytes_in_used"));
9940
9941 $expected = '^(0.0|0)$';
9942 $self->assert(qr/$expected/, $bytes_out_used,
9943 test_msg("Expected $expected, got $bytes_out_used"));
9944
9945 $expected = '^(0.0|0)$';
9946 $self->assert(qr/$expected/, $bytes_xfer_used,
9947 test_msg("Expected $expected, got $bytes_xfer_used"));
9948
9949 $expected = 0;
9950 $self->assert($expected == $files_in_used,
9951 test_msg("Expected $expected, got $files_in_used"));
9952
9953 $expected = 0;
9954 $self->assert($expected == $files_out_used,
9955 test_msg("Expected $expected, got $files_out_used"));
9956
9957 $expected = 0;
9958 $self->assert($expected == $files_xfer_used,
9959 test_msg("Expected $expected, got $files_xfer_used"));
9960
9961 if ($ex) {
9962 test_append_logfile($log_file, $ex);
9963 unlink($log_file);
9964
9965 die($ex);
9966 }
9967
9968 unlink($log_file);
9969 }
9970
9971 sub quotatab_stor_deleteabortedstores_cmd_abor_bug3621 {
9972 my $self = shift;
9973 my $tmpdir = $self->{tmpdir};
9974
9975 my $config_file = "$tmpdir/quotatab.conf";
9976 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
9977 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
9978
9979 my $log_file = test_get_logfile();
9980
9981 my $user = 'proftpd';
9982 my $passwd = 'test';
9983 my $group = 'ftpd';
9984 my $home_dir = File::Spec->rel2abs($tmpdir);
9985 my $uid = 500;
9986 my $gid = 500;
9987
9988 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9989
9990 # Build up sqlite3 command to create users, groups tables and populate them
9991 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9992
9993 if (open(my $fh, "> $db_script")) {
9994 print $fh <<EOS;
9995 CREATE TABLE users (
9996 userid TEXT PRIMARY KEY,
9997 passwd TEXT,
9998 uid INTEGER,
9999 gid INTEGER,
10000 homedir TEXT,
10001 shell TEXT,
10002 lastdir TEXT
10003 );
10004 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
10005
10006 CREATE TABLE groups (
10007 groupname TEXT PRIMARY KEY,
10008 gid INTEGER,
10009 members TEXT
10010 );
10011 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
10012
10013 CREATE TABLE quotalimits (
10014 name TEXT NOT NULL PRIMARY KEY,
10015 quota_type TEXT NOT NULL,
10016 per_session TEXT NOT NULL,
10017 limit_type TEXT NOT NULL,
10018 bytes_in_avail REAL NOT NULL,
10019 bytes_out_avail REAL NOT NULL,
10020 bytes_xfer_avail REAL NOT NULL,
10021 files_in_avail INTEGER NOT NULL,
10022 files_out_avail INTEGER NOT NULL,
10023 files_xfer_avail INTEGER NOT NULL
10024 );
10025 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);
10026
10027 CREATE TABLE quotatallies (
10028 name TEXT NOT NULL PRIMARY KEY,
10029 quota_type TEXT NOT NULL,
10030 bytes_in_used REAL NOT NULL,
10031 bytes_out_used REAL NOT NULL,
10032 bytes_xfer_used REAL NOT NULL,
10033 files_in_used INTEGER NOT NULL,
10034 files_out_used INTEGER NOT NULL,
10035 files_xfer_used INTEGER NOT NULL
10036 );
10037 EOS
10038
10039 unless (close($fh)) {
10040 die("Can't write $db_script: $!");
10041 }
10042
10043 } else {
10044 die("Can't open $db_script: $!");
10045 }
10046
10047 my $cmd = "sqlite3 $db_file < $db_script";
10048
10049 if ($ENV{TEST_VERBOSE}) {
10050 print STDERR "Executing sqlite3: $cmd\n";
10051 }
10052
10053 my @output = `$cmd`;
10054 if (scalar(@output) &&
10055 $ENV{TEST_VERBOSE}) {
10056 print STDERR "Output: ", join('', @output), "\n";
10057 }
10058
10059 my $config = {
10060 PidFile => $pid_file,
10061 ScoreboardFile => $scoreboard_file,
10062 SystemLog => $log_file,
10063
10064 DefaultChdir => '~',
10065 DeleteAbortedStores => 'on',
10066 TimeoutLinger => 0,
10067
10068 IfModules => {
10069 'mod_delay.c' => {
10070 DelayEngine => 'off',
10071 },
10072
10073 'mod_quotatab_sql.c' => [
10074 '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}\'"',
10075 '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}\'"',
10076 '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',
10077 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
10078
10079 'QuotaEngine on',
10080 "QuotaLog $log_file",
10081 'QuotaLimitTable sql:/get-quota-limit',
10082 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
10083 ],
10084
10085 'mod_sql.c' => {
10086 SQLAuthTypes => 'plaintext',
10087 SQLBackend => 'sqlite3',
10088 SQLConnectInfo => $db_file,
10089 SQLLogFile => $log_file,
10090 SQLMinID => '0',
10091 },
10092 },
10093 };
10094
10095 my ($port, $config_user, $config_group) = config_write($config_file, $config);
10096
10097 # Open pipes, for use between the parent and child processes. Specifically,
10098 # the child will indicate when it's done with its test by writing a message
10099 # to the parent.
10100 my ($rfh, $wfh);
10101 unless (pipe($rfh, $wfh)) {
10102 die("Can't open pipe: $!");
10103 }
10104
10105 my $ex;
10106
10107 # Fork child
10108 $self->handle_sigchld();
10109 defined(my $pid = fork()) or die("Can't fork: $!");
10110 if ($pid) {
10111 eval {
10112 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
10113 $client->login($user, $passwd);
10114
10115 my $conn = $client->stor_raw('test.txt');
10116 unless ($conn) {
10117 die("Failed to STOR test.txt: " . $client->response_code() . " " .
10118 $client->response_msg());
10119 }
10120
10121 my $buf = "Hello, World\n";
10122 $conn->write($buf, length($buf), 25);
10123
10124 $client->quote('ABOR');
10125
10126 $conn->write($buf, length($buf), 25);
10127 eval { $conn->close() };
10128
10129 my $resp_code = $client->response_code();
10130 my $resp_msg = $client->response_msg();
10131
10132 my $expected;
10133
10134 $expected = 426;
10135 $self->assert($expected == $resp_code,
10136 test_msg("Expected $expected, got $resp_code"));
10137
10138 $expected = "Transfer aborted. Data connection closed";
10139 $self->assert($expected eq $resp_msg,
10140 test_msg("Expected '$expected', got '$resp_msg'"));
10141 };
10142
10143 if ($@) {
10144 $ex = $@;
10145 }
10146
10147 $wfh->print("done\n");
10148 $wfh->flush();
10149
10150 } else {
10151 eval { server_wait($config_file, $rfh) };
10152 if ($@) {
10153 warn($@);
10154 exit 1;
10155 }
10156
10157 exit 0;
10158 }
10159
10160 # Stop server
10161 server_stop($pid_file);
10162
10163 $self->assert_child_ok($pid);
10164
10165 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\'");
10166
10167 my $expected;
10168
10169 $expected = 'user';
10170 $self->assert($expected eq $quota_type,
10171 test_msg("Expected '$expected', got '$quota_type'"));
10172
10173 $expected = '^(0.0|0)$';
10174 $self->assert(qr/$expected/, $bytes_in_used,
10175 test_msg("Expected $expected, got $bytes_in_used"));
10176
10177 $expected = '^(0.0|0)$';
10178 $self->assert(qr/$expected/, $bytes_out_used,
10179 test_msg("Expected $expected, got $bytes_out_used"));
10180
10181 $expected = '^(0.0|0)$';
10182 $self->assert(qr/$expected/, $bytes_xfer_used,
10183 test_msg("Expected $expected, got $bytes_xfer_used"));
10184
10185 $expected = 0;
10186 $self->assert($expected == $files_in_used,
10187 test_msg("Expected $expected, got $files_in_used"));
10188
10189 $expected = 0;
10190 $self->assert($expected == $files_out_used,
10191 test_msg("Expected $expected, got $files_out_used"));
10192
10193 $expected = 0;
10194 $self->assert($expected == $files_xfer_used,
10195 test_msg("Expected $expected, got $files_xfer_used"));
10196
10197 if ($ex) {
10198 test_append_logfile($log_file, $ex);
10199 unlink($log_file);
10200
10201 die($ex);
10202 }
10203
10204 unlink($log_file);
10205 }
10206
10207 sub quotatab_sql_addl_query_columns_bug3879 {
10208 my $self = shift;
10209 my $tmpdir = $self->{tmpdir};
10210
10211 my $config_file = "$tmpdir/quotatab.conf";
10212 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
10213 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
10214
10215 my $log_file = test_get_logfile();
10216
10217 my $user = 'proftpd';
10218 my $passwd = 'test';
10219 my $group = 'ftpd';
10220 my $home_dir = File::Spec->rel2abs($tmpdir);
10221 my $uid = 500;
10222 my $gid = 500;
10223
10224 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
10225
10226 # Build up sqlite3 command to create users, groups tables and populate them
10227 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
10228
10229 if (open(my $fh, "> $db_script")) {
10230 print $fh <<EOS;
10231 CREATE TABLE users (
10232 userid TEXT PRIMARY KEY,
10233 passwd TEXT,
10234 uid INTEGER,
10235 gid INTEGER,
10236 homedir TEXT,
10237 shell TEXT,
10238 lastdir TEXT
10239 );
10240 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
10241
10242 CREATE TABLE groups (
10243 groupname TEXT PRIMARY KEY,
10244 gid INTEGER,
10245 members TEXT
10246 );
10247 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
10248
10249 CREATE TABLE quotalimits (
10250 name TEXT NOT NULL PRIMARY KEY,
10251 quota_type TEXT NOT NULL,
10252 per_session TEXT NOT NULL,
10253 limit_type TEXT NOT NULL,
10254 bytes_in_avail REAL NOT NULL,
10255 bytes_out_avail REAL NOT NULL,
10256 bytes_xfer_avail REAL NOT NULL,
10257 files_in_avail INTEGER NOT NULL,
10258 files_out_avail INTEGER NOT NULL,
10259 files_xfer_avail INTEGER NOT NULL,
10260 ip_addr TEXT NOT NULL
10261 );
10262 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, ip_addr) VALUES ('$user', 'user', 'false', 'soft', 32, 0, 0, 2, 0, 0, '127.0.0.1');
10263
10264 CREATE TABLE quotatallies (
10265 name TEXT NOT NULL PRIMARY KEY,
10266 quota_type TEXT NOT NULL,
10267 bytes_in_used REAL NOT NULL,
10268 bytes_out_used REAL NOT NULL,
10269 bytes_xfer_used REAL NOT NULL,
10270 files_in_used INTEGER NOT NULL,
10271 files_out_used INTEGER NOT NULL,
10272 files_xfer_used INTEGER NOT NULL,
10273 ip_addr TEXT NOT NULL
10274 );
10275 EOS
10276
10277 unless (close($fh)) {
10278 die("Can't write $db_script: $!");
10279 }
10280
10281 } else {
10282 die("Can't open $db_script: $!");
10283 }
10284
10285 my $cmd = "sqlite3 $db_file < $db_script";
10286
10287 if ($ENV{TEST_VERBOSE}) {
10288 print STDERR "Executing sqlite3: $cmd\n";
10289 }
10290
10291 my @output = `$cmd`;
10292 if (scalar(@output) &&
10293 $ENV{TEST_VERBOSE}) {
10294 print STDERR "Output: ", join('', @output), "\n";
10295 }
10296
10297 my $config = {
10298 PidFile => $pid_file,
10299 ScoreboardFile => $scoreboard_file,
10300 SystemLog => $log_file,
10301
10302 DefaultChdir => '~',
10303
10304 IfModules => {
10305 'mod_delay.c' => {
10306 DelayEngine => 'off',
10307 },
10308
10309 'mod_quotatab_sql.c' => [
10310 '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, ip_addr FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\' AND ip_addr = \'%a\'"',
10311 '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, ip_addr FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\' AND ip_addr = \'%a\'"',
10312 '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}\' AND ip_addr = \'%a\'" quotatallies',
10313 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}, \'%a\'" quotatallies',
10314
10315 'QuotaEngine on',
10316 "QuotaLog $log_file",
10317 'QuotaLimitTable sql:/get-quota-limit',
10318 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
10319 ],
10320
10321 'mod_sql.c' => {
10322 SQLAuthTypes => 'plaintext',
10323 SQLBackend => 'sqlite3',
10324 SQLConnectInfo => $db_file,
10325 SQLLogFile => $log_file,
10326 SQLMinID => '0',
10327 },
10328 },
10329 };
10330
10331 my ($port, $config_user, $config_group) = config_write($config_file, $config);
10332
10333 # Open pipes, for use between the parent and child processes. Specifically,
10334 # the child will indicate when it's done with its test by writing a message
10335 # to the parent.
10336 my ($rfh, $wfh);
10337 unless (pipe($rfh, $wfh)) {
10338 die("Can't open pipe: $!");
10339 }
10340
10341 my $ex;
10342
10343 # Fork child
10344 $self->handle_sigchld();
10345 defined(my $pid = fork()) or die("Can't fork: $!");
10346 if ($pid) {
10347 eval {
10348 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
10349 $client->login($user, $passwd);
10350
10351 my $conn = $client->stor_raw('test.txt');
10352 unless ($conn) {
10353 die("Failed to STOR test.txt: " . $client->response_code() . " " .
10354 $client->response_msg());
10355 }
10356
10357 my $buf = "Hello, World\n";
10358 $conn->write($buf, length($buf), 25);
10359 eval { $conn->close() };
10360
10361 my $resp_code = $client->response_code();
10362 my $resp_msg = $client->response_msg();
10363 $self->assert_transfer_ok($resp_code, $resp_msg);
10364
10365 $client->quit();
10366 };
10367
10368 if ($@) {
10369 $ex = $@;
10370 }
10371
10372 $wfh->print("done\n");
10373 $wfh->flush();
10374
10375 } else {
10376 eval { server_wait($config_file, $rfh) };
10377 if ($@) {
10378 warn($@);
10379 exit 1;
10380 }
10381
10382 exit 0;
10383 }
10384
10385 # Stop server
10386 server_stop($pid_file);
10387
10388 $self->assert_child_ok($pid);
10389
10390 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\'");
10391
10392 my $expected;
10393
10394 $expected = 'user';
10395 $self->assert($expected eq $quota_type,
10396 test_msg("Expected '$expected', got '$quota_type'"));
10397
10398 $expected = '^(13.0|13)$';
10399 $self->assert(qr/$expected/, $bytes_in_used,
10400 test_msg("Expected $expected, got $bytes_in_used"));
10401
10402 $expected = '^(0.0|0)$';
10403 $self->assert(qr/$expected/, $bytes_out_used,
10404 test_msg("Expected $expected, got $bytes_out_used"));
10405
10406 $expected = '^(0.0|0)$';
10407 $self->assert(qr/$expected/, $bytes_xfer_used,
10408 test_msg("Expected $expected, got $bytes_xfer_used"));
10409
10410 $expected = 1;
10411 $self->assert($expected == $files_in_used,
10412 test_msg("Expected $expected, got $files_in_used"));
10413
10414 $expected = 0;
10415 $self->assert($expected == $files_out_used,
10416 test_msg("Expected $expected, got $files_out_used"));
10417
10418 $expected = 0;
10419 $self->assert($expected == $files_xfer_used,
10420 test_msg("Expected $expected, got $files_xfer_used"));
10421
10422 if ($ex) {
10423 test_append_logfile($log_file, $ex);
10424 unlink($log_file);
10425
10426 die($ex);
10427 }
10428
10429 unlink($log_file);
10430 }
10431
10432 # See:
10433 # https://forums.proftpd.org/smf/index.php/topic,11862.0.html
10434 #
10435 # Note that this is more for diagnosing ODBC issues than quotatab issues.
10436 sub quotatab_sql_odbc {
10437 my $self = shift;
10438 my $tmpdir = $self->{tmpdir};
10439 my $setup = test_setup($tmpdir, 'quotatab');
10440
10441 my $odbcini_path = File::Spec->rel2abs("t/etc/modules/mod_sql_odbc/odbc.ini");
10442 my $odbcinst_path = File::Spec->rel2abs("t/etc/modules/mod_sql_odbc/odbcinst.ini");
10443
10444 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
10445
10446 # Build up sqlite3 command to create users, groups tables and populate them
10447 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
10448
10449 if (open(my $fh, "> $db_script")) {
10450 print $fh <<EOS;
10451 CREATE TABLE quotalimits (
10452 name TEXT NOT NULL PRIMARY KEY,
10453 quota_type TEXT NOT NULL,
10454 per_session TEXT NOT NULL,
10455 limit_type TEXT NOT NULL,
10456 bytes_in_avail REAL NOT NULL,
10457 bytes_out_avail REAL NOT NULL,
10458 bytes_xfer_avail REAL NOT NULL,
10459 files_in_avail INTEGER NOT NULL,
10460 files_out_avail INTEGER NOT NULL,
10461 files_xfer_avail INTEGER NOT NULL
10462 );
10463 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 ('$setup->{user}', 'user', 'false', 'soft', 32, 0, 0, 2, 0, 0);
10464
10465 CREATE TABLE quotatallies (
10466 name TEXT NOT NULL PRIMARY KEY,
10467 quota_type TEXT NOT NULL,
10468 bytes_in_used REAL NOT NULL,
10469 bytes_out_used REAL NOT NULL,
10470 bytes_xfer_used REAL NOT NULL,
10471 files_in_used INTEGER NOT NULL,
10472 files_out_used INTEGER NOT NULL,
10473 files_xfer_used INTEGER NOT NULL
10474 );
10475 EOS
10476
10477 unless (close($fh)) {
10478 die("Can't write $db_script: $!");
10479 }
10480
10481 } else {
10482 die("Can't open $db_script: $!");
10483 }
10484
10485 my $cmd = "sqlite3 $db_file < $db_script";
10486
10487 if ($ENV{TEST_VERBOSE}) {
10488 print STDERR "Executing sqlite3: $cmd\n";
10489 }
10490
10491 my @output = `$cmd`;
10492 if (scalar(@output) &&
10493 $ENV{TEST_VERBOSE}) {
10494 print STDERR "Output: ", join('', @output), "\n";
10495 }
10496
10497 my $config = {
10498 PidFile => $setup->{pid_file},
10499 ScoreboardFile => $setup->{scoreboard_file},
10500 SystemLog => $setup->{log_file},
10501
10502 AuthUserFile => $setup->{auth_user_file},
10503 AuthGroupFile => $setup->{auth_group_file},
10504 AuthOrder => 'mod_auth_file.c',
10505
10506 DefaultChdir => '~',
10507
10508 IfModules => {
10509 'mod_delay.c' => {
10510 DelayEngine => 'off',
10511 },
10512
10513 'mod_quotatab_sql.c' => [
10514 '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}\'"',
10515 '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}\'"',
10516 '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',
10517 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
10518
10519 'QuotaEngine on',
10520 "QuotaLog $setup->{log_file}",
10521 'QuotaLimitTable sql:/get-quota-limit',
10522 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
10523 ],
10524
10525 'mod_sql.c' => {
10526 SQLAuthenticate => 'off',
10527 SQLEngine => 'on',
10528 SQLBackend => 'odbc',
10529 SQLConnectInfo => "mysql proftpd developer PERCONNECTION",
10530 SQLLogFile => $setup->{log_file},
10531 SQLOptions => 'NoDisconnectOnError',
10532 },
10533
10534 'mod_sql_odbc.c' => {
10535 SQLODBCVersion => '2',
10536 },
10537 },
10538 };
10539
10540 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
10541 $config);
10542
10543 if (open(my $fh, ">> $setup->{config_file}")) {
10544 print $fh <<EOC;
10545 # Necessary ODBC environment variables
10546 SetEnv ODBCINST $odbcinst_path
10547 SetEnv ODBCINI $odbcini_path
10548 EOC
10549 unless (close($fh)) {
10550 die("Can't write $setup->{config_file}: $!");
10551 }
10552
10553 } else {
10554 die("Can't open $setup->{config_file}: $!");
10555 }
10556
10557 # Open pipes, for use between the parent and child processes. Specifically,
10558 # the child will indicate when it's done with its test by writing a message
10559 # to the parent.
10560 my ($rfh, $wfh);
10561 unless (pipe($rfh, $wfh)) {
10562 die("Can't open pipe: $!");
10563 }
10564
10565 my $ex;
10566
10567 # Fork child
10568 $self->handle_sigchld();
10569 defined(my $pid = fork()) or die("Can't fork: $!");
10570 if ($pid) {
10571 eval {
10572 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
10573 $client->login($setup->{user}, $setup->{passwd});
10574
10575 my $conn = $client->stor_raw('test.txt');
10576 unless ($conn) {
10577 die("Failed to STOR test.txt: " . $client->response_code() . " " .
10578 $client->response_msg());
10579 }
10580
10581 my $buf = "Hello, World\n";
10582 $conn->write($buf, length($buf), 25);
10583 eval { $conn->close() };
10584
10585 my $resp_code = $client->response_code();
10586 my $resp_msg = $client->response_msg();
10587
10588 $self->assert_transfer_ok($resp_code, $resp_msg);
10589 };
10590
10591 if ($@) {
10592 $ex = $@;
10593 }
10594
10595 $wfh->print("done\n");
10596 $wfh->flush();
10597
10598 } else {
10599 eval { server_wait($setup->{config_file}, $rfh) };
10600 if ($@) {
10601 warn($@);
10602 exit 1;
10603 }
10604
10605 exit 0;
10606 }
10607
10608 # Stop server
10609 server_stop($setup->{pid_file});
10610 $self->assert_child_ok($pid);
10611
10612 test_cleanup($setup->{log_file}, $ex);
10613 }
10614
10615 1;