Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_sql_sqlite;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Carp;
8 use File::Path qw(mkpath);
9 use File::Spec;
10 use IO::Handle;
11 use Net::Address::IP::Local;
12
13 use ProFTPD::TestSuite::FTP;
14 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
15
16 $| = 1;
17
18 my $order = 0;
19
20 my $TESTS = {
21 sql_bug2045 => {
22 order => ++$order,
23 test_class => [qw(bug forking)],
24 },
25
26 sql_bug2922 => {
27 order => ++$order,
28 test_class => [qw(bug forking rootprivs)],
29 },
30
31 sql_bug3116 => {
32 order => ++$order,
33 test_class => [qw(bug forking)],
34 },
35
36 sql_bug3124 => {
37 order => ++$order,
38 test_class => [qw(bug forking)],
39 },
40
41 sql_sqlite_bug3126 => {
42 order => ++$order,
43 test_class => [qw(bug forking)],
44 },
45
46 sql_user_where_clause_ok => {
47 order => ++$order,
48 test_class => [qw(forking)],
49 },
50
51 sql_user_where_clause_with_vars_ok => {
52 order => ++$order,
53 test_class => [qw(forking)],
54 },
55
56 sql_group_where_clause_ok => {
57 order => ++$order,
58 test_class => [qw(forking)],
59 },
60
61 sql_bug3149 => {
62 order => ++$order,
63 test_class => [qw(bug forking)],
64 },
65
66 sql_sqllog => {
67 order => ++$order,
68 test_class => [qw(forking)],
69 },
70
71 sql_sqlite_sqllog_with_chroot => {
72 order => ++$order,
73 test_class => [qw(forking rootprivs)],
74 },
75
76 sql_user_info_different_table_names => {
77 order => ++$order,
78 test_class => [qw(forking)],
79 },
80
81 sql_custom_user_info => {
82 order => ++$order,
83 test_class => [qw(forking)],
84 },
85
86 sql_custom_user_info_null_ids => {
87 order => ++$order,
88 test_class => [qw(forking)],
89 },
90
91 sql_userset_bug2434 => {
92 order => ++$order,
93 test_class => [qw(bug forking rootprivs)],
94 },
95
96 sql_usersetfast_bug2434 => {
97 order => ++$order,
98 test_class => [qw(bug forking rootprivs)],
99 },
100
101 sql_custom_group_info_bug3043 => {
102 order => ++$order,
103 test_class => [qw(bug forking)],
104 },
105
106 sql_groupset_bug3043 => {
107 order => ++$order,
108 test_class => [qw(bug forking rootprivs)],
109 },
110
111 sql_groupsetfast_bug3043 => {
112 order => ++$order,
113 test_class => [qw(bug forking rootprivs)],
114 },
115
116 sql_sqllog_var_w => {
117 order => ++$order,
118 test_class => [qw(forking)],
119 },
120
121 sql_sqllog_var_w_chrooted => {
122 order => ++$order,
123 test_class => [qw(forking rootprivs)],
124 },
125
126 sql_openssl_auth_type_md5 => {
127 order => ++$order,
128 test_class => [qw(feature_openssl forking)],
129 },
130
131 sql_openssl_auth_type_sha1 => {
132 order => ++$order,
133 test_class => [qw(feature_openssl forking)],
134 },
135
136 sql_sqllog_var_S => {
137 order => ++$order,
138 test_class => [qw(forking)],
139 },
140
141 sql_sqllog_var_S_err => {
142 order => ++$order,
143 test_class => [qw(forking)],
144 },
145
146 sql_negative_cache_bug3282 => {
147 order => ++$order,
148 test_class => [qw(bug forking)],
149 },
150
151 sql_sqllog_var_T_rnfr => {
152 order => ++$order,
153 test_class => [qw(bug forking)],
154 },
155
156 sql_sqllog_var_T_retr => {
157 order => ++$order,
158 test_class => [qw(bug forking)],
159 },
160
161 sql_sqllog_var_transfer_millisecs_retr_bug4218 => {
162 order => ++$order,
163 test_class => [qw(bug forking)],
164 },
165
166 sql_sqllog_var_R_retr_bug4218 => {
167 order => ++$order,
168 test_class => [qw(bug forking)],
169 },
170
171 sql_sqllog_exit => {
172 order => ++$order,
173 test_class => [qw(forking)],
174 },
175
176 sql_sqllog_exit_var_remote_port_bug4296 => {
177 order => ++$order,
178 test_class => [qw(bug forking)],
179 },
180
181 sql_sqllog_var_d_bug3395 => {
182 order => ++$order,
183 test_class => [qw(bug forking)],
184 },
185
186 sql_sqllog_var_d_chroot_bug3395 => {
187 order => ++$order,
188 test_class => [qw(bug forking rootprivs)],
189 },
190
191 sql_sqllog_var_uid_gid_bug3390 => {
192 order => ++$order,
193 test_class => [qw(bug forking)],
194 },
195
196 sql_sqlite_auth_type_backend_bug3511 => {
197 order => ++$order,
198 test_class => [qw(bug forking)],
199 },
200
201 sql_sqllog_pass_ok_var_s_bug3528 => {
202 order => ++$order,
203 test_class => [qw(bug forking)],
204 },
205
206 sql_sqllog_pass_failed_var_s_bug3528 => {
207 order => ++$order,
208 test_class => [qw(bug forking)],
209 },
210
211 sql_sqllog_pass_failed_bug4301 => {
212 order => ++$order,
213 test_class => [qw(bug forking)],
214 },
215
216 sql_sqlshowinfo_pass_bug3423 => {
217 order => ++$order,
218 test_class => [qw(bug forking)],
219 },
220
221 sql_sqlshowinfo_list_bug3423 => {
222 order => ++$order,
223 test_class => [qw(bug forking)],
224 },
225
226 sql_multiple_users_shared_uid_gid => {
227 order => ++$order,
228 test_class => [qw(bug forking)],
229 },
230
231 sql_resolve_tag_bug3536 => {
232 order => ++$order,
233 test_class => [qw(bug forking inprogress)],
234 },
235
236 sql_sqllog_vars_I_O_bug3554 => {
237 order => ++$order,
238 test_class => [qw(bug forking)],
239 },
240
241 sql_sqllog_note_var_unique_id_bug3572 => {
242 order => ++$order,
243 test_class => [qw(bug forking mod_unique_id)],
244 },
245
246 sql_sqllog_note_var_rewrite_bug3572 => {
247 order => ++$order,
248 test_class => [qw(bug forking mod_rewrite)],
249 },
250
251 sql_sqllog_note_sql_user_info => {
252 order => ++$order,
253 test_class => [qw(forking)],
254 },
255
256 sql_sqllog_var_E => {
257 order => ++$order,
258 test_class => [qw(forking)],
259 },
260
261 sql_named_conn_bug3262 => {
262 order => ++$order,
263 test_class => [qw(bug forking)],
264 },
265
266 sql_named_conn_sqllog_exit_bug3645 => {
267 order => ++$order,
268 test_class => [qw(bug forking rootprivs)],
269 },
270
271 sql_sqllog_vars_H_L_matching_server_bug3620 => {
272 order => ++$order,
273 test_class => [qw(bug forking)],
274 },
275
276 sql_sqllog_vars_H_L_default_server_bug3620 => {
277 order => ++$order,
278 test_class => [qw(bug forking)],
279 },
280
281 sql_sqllog_exit_ifuser => {
282 order => ++$order,
283 test_class => [qw(forking mod_ifsession)],
284 },
285
286 sql_sqllog_exit_ifgroup => {
287 order => ++$order,
288 test_class => [qw(forking mod_ifsession)],
289 },
290
291 sql_sqllog_multi_pass_ifclass_bug4025 => {
292 order => ++$order,
293 test_class => [qw(forking mod_ifsession)],
294 },
295
296 sql_opt_no_disconnect_on_error_with_extlog_bug3633 => {
297 order => ++$order,
298 test_class => [qw(bug forking)],
299 },
300
301 sql_sqllog_ignore_errors_bad_table_bug3692 => {
302 order => ++$order,
303 test_class => [qw(bug forking)],
304 },
305
306 sql_sqllog_ignore_errors_bad_db_bug3692 => {
307 order => ++$order,
308 test_class => [qw(bug forking)],
309 },
310
311 sql_sqllog_var_xfer_status_nonxfer => {
312 order => ++$order,
313 test_class => [qw(forking)],
314 },
315
316 sql_sqllog_var_xfer_status_success_download => {
317 order => ++$order,
318 test_class => [qw(forking)],
319 },
320
321 sql_sqllog_var_xfer_status_success_upload => {
322 order => ++$order,
323 test_class => [qw(forking)],
324 },
325
326 sql_sqllog_var_xfer_status_cancelled => {
327 order => ++$order,
328 test_class => [qw(forking)],
329 },
330
331 sql_sqllog_var_xfer_status_failed => {
332 order => ++$order,
333 test_class => [qw(forking)],
334 },
335
336 sql_sqllog_var_xfer_status_timeout => {
337 order => ++$order,
338 test_class => [qw(forking)],
339 },
340
341 sql_sqllog_var_xfer_status_filtered => {
342 order => ++$order,
343 test_class => [qw(forking)],
344 },
345
346 sql_sqllog_var_xfer_failure_none => {
347 order => ++$order,
348 test_class => [qw(forking)],
349 },
350
351 sql_sqllog_var_xfer_failure_reason => {
352 order => ++$order,
353 test_class => [qw(forking)],
354 },
355
356 sql_sqlshowinfo_pass_failed_bug3782 => {
357 order => ++$order,
358 test_class => [qw(bug forking)],
359 },
360
361 sql_sqllog_preauth_var_U_bug3822 => {
362 order => ++$order,
363 test_class => [qw(bug forking)],
364 },
365
366 sql_sqllog_preauth_var_u_bug3822 => {
367 order => ++$order,
368 test_class => [qw(bug forking)],
369 },
370
371 sql_sqlite_maxhostsperuser => {
372 order => ++$order,
373 test_class => [qw(forking mod_unique_id)],
374 },
375
376 sql_sqllog_var_micros_ts_bug3889 => {
377 order => ++$order,
378 test_class => [qw(bug forking)],
379 },
380
381 sql_sqllog_var_millis_ts_bug3889 => {
382 order => ++$order,
383 test_class => [qw(bug forking)],
384 },
385
386 sql_sqllog_var_iso8601_ts_bug3889 => {
387 order => ++$order,
388 test_class => [qw(bug forking)],
389 },
390
391 sql_sqllog_var_note_ftps_without_tls_issue1232 => {
392 order => ++$order,
393 test_class => [qw(bug forking)],
394 },
395
396 sql_sqllog_var_note_ftps_with_tls_issue1232 => {
397 order => ++$order,
398 test_class => [qw(bug forking mod_tls)],
399 },
400
401 sql_sqllogonevent_bug3893 => {
402 order => ++$order,
403 test_class => [qw(bug forking)],
404 },
405
406 sql_userprimarykey_bug3864 => {
407 order => ++$order,
408 test_class => [qw(bug forking)],
409 },
410
411 sql_userprimarykey_custom_bug3864 => {
412 order => ++$order,
413 test_class => [qw(bug forking)],
414 },
415
416 sql_groupprimarykey_bug3864 => {
417 order => ++$order,
418 test_class => [qw(bug forking)],
419 },
420
421 sql_groupprimarykey_custom_bug3864 => {
422 order => ++$order,
423 test_class => [qw(bug forking)],
424 },
425
426 sql_sqllog_var_basename_bug3987 => {
427 order => ++$order,
428 test_class => [qw(bug forking)],
429 },
430
431 sql_user_info_defaulthomedir_bug4083 => {
432 order => ++$order,
433 test_class => [qw(bug forking)],
434 },
435
436 sql_user_info_null_uid_gid_columns => {
437 order => ++$order,
438 test_class => [qw(forking)],
439 },
440
441 sql_64_bit_uid_bug4164 => {
442 order => ++$order,
443 test_class => [qw(bug forking)],
444 },
445
446 sql_sqllog_var_remote_port => {
447 order => ++$order,
448 test_class => [qw(forking)],
449 },
450
451 sql_userowner_issue346 => {
452 order => ++$order,
453 test_class => [qw(forking rootprivs)],
454 },
455
456 sql_sqlite_log_db_enoent_issue654 => {
457 order => ++$order,
458 test_class => [qw(forking bug)],
459 },
460
461 sql_namebased_vhost_using_host_issue882 => {
462 order => ++$order,
463 test_class => [qw(forking bug)],
464 },
465
466 sql_namebased_vhost_using_sni_issue882 => {
467 order => ++$order,
468 test_class => [qw(forking bug mod_tls)],
469 },
470
471 sql_user_info_no_suppl_groups_issue1830 => {
472 order => ++$order,
473 test_class => [qw(forking bug rootprivs)],
474 },
475
476 sql_site_chgrp_sql_injection_issue2188 => {
477 order => ++$order,
478 test_class => [qw(forking bug)],
479 },
480 };
481
482 sub new {
483 return shift()->SUPER::new(@_);
484 }
485
486 sub list_tests {
487 return testsuite_get_runnable_tests($TESTS);
488 }
489
490 sub build_db {
491 my $cmd = shift;
492 my $db_script = shift;
493 my $check_exit_status = shift;
494 $check_exit_status = 0 unless defined $check_exit_status;
495
496 if ($ENV{TEST_VERBOSE}) {
497 print STDERR "Executing sqlite3: $cmd\n";
498 }
499
500 my @output = `$cmd`;
501 my $exit_status = $?;
502
503 if ($ENV{TEST_VERBOSE}) {
504 print STDERR "Output: ", join('', @output), "\n";
505 }
506
507 if ($check_exit_status) {
508 if ($? != 0) {
509 croak("'$cmd' failed");
510 }
511 }
512
513 unlink($db_script);
514 return 1;
515 }
516
517 sub sql_bug2045 {
518 my $self = shift;
519 my $tmpdir = $self->{tmpdir};
520 my $setup = test_setup($tmpdir, 'sqlite');
521
522 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
523
524 # Build up sqlite3 command to create users, groups tables and populate them
525 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
526
527 if (open(my $fh, "> $db_script")) {
528 print $fh <<EOS;
529 CREATE TABLE users (
530 userid TEXT,
531 passwd TEXT,
532 uid INTEGER,
533 gid INTEGER,
534 homedir TEXT,
535 shell TEXT,
536 lastdir TEXT
537 );
538 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash');
539
540 CREATE TABLE groups (
541 groupname TEXT,
542 gid INTEGER,
543 members TEXT
544 );
545 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
546 EOS
547
548 unless (close($fh)) {
549 die("Can't write $db_script: $!");
550 }
551
552 } else {
553 die("Can't open $db_script: $!");
554 }
555
556 my $cmd = "sqlite3 $db_file < $db_script";
557 build_db($cmd, $db_script);
558
559 # Make sure that, if we're running as root, the database file has
560 # the permissions/privs set for use by proftpd
561 if ($< == 0) {
562 unless (chmod(0666, $db_file)) {
563 die("Can't set perms on $db_file to 0666: $!");
564 }
565 }
566
567 my $config = {
568 PidFile => $setup->{pid_file},
569 ScoreboardFile => $setup->{scoreboard_file},
570 SystemLog => $setup->{log_file},
571 TraceLog => $setup->{log_file},
572 Trace => 'sql:20',
573
574 IfModules => {
575 'mod_delay.c' => {
576 DelayEngine => 'off',
577 },
578
579 'mod_sql.c' => {
580 SQLAuthTypes => 'plaintext',
581 SQLBackend => 'sqlite3',
582 SQLConnectInfo => $db_file,
583 SQLLogFile => $setup->{log_file},
584 SQLNamedQuery => 'lastdir SELECT "lastdir FROM users WHERE userid = \'%u\'"',
585 SQLShowInfo => 'PASS "230" "\"%{lastdir}\" was the last directory"',
586 },
587 },
588 };
589
590 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
591 $config);
592
593 # Open pipes, for use between the parent and child processes. Specifically,
594 # the child will indicate when it's done with its test by writing a message
595 # to the parent.
596 my ($rfh, $wfh);
597 unless (pipe($rfh, $wfh)) {
598 die("Can't open pipe: $!");
599 }
600
601 my $ex;
602
603 # Fork child
604 $self->handle_sigchld();
605 defined(my $pid = fork()) or die("Can't fork: $!");
606 if ($pid) {
607 eval {
608 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
609 $client->login($setup->{user}, $setup->{passwd});
610
611 my $resp_msgs = $client->response_msgs();
612 my $nmsgs = scalar(@$resp_msgs);
613
614 my $expected = 1;
615 $self->assert($expected == $nmsgs, "Expected $expected, got $nmsgs");
616
617 $expected = "User proftpd logged in";
618 $self->assert($expected eq $resp_msgs->[0],
619 "Expected '$expected', got '$resp_msgs->[0]'");
620 };
621 if ($@) {
622 $ex = $@;
623 }
624
625 $wfh->print("done\n");
626 $wfh->flush();
627
628 } else {
629 eval { server_wait($setup->{config_file}, $rfh) };
630 if ($@) {
631 warn($@);
632 exit 1;
633 }
634
635 exit 0;
636 }
637
638 # Stop server
639 server_stop($setup->{pid_file});
640 $self->assert_child_ok($pid);
641
642 test_cleanup($setup->{log_file}, $ex);
643 }
644
645 sub sql_bug2922 {
646 my $self = shift;
647 my $tmpdir = $self->{tmpdir};
648
649 my $config_file = "$tmpdir/sqlite.conf";
650 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
651 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
652
653 my $log_file = test_get_logfile();
654
655 # Bug#2922 occurred because mod_sql would "authenticate" the plaintext
656 # password (if configured to do so) of a user whose info did NOT come from
657 # mod_sql.
658
659 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
660
661 # Build up sqlite3 command to create users, groups tables and populate them
662 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
663
664 if (open(my $fh, "> $db_script")) {
665 print $fh <<EOS;
666 CREATE TABLE users (
667 userid TEXT,
668 passwd TEXT,
669 uid INTEGER,
670 gid INTEGER,
671 homedir TEXT,
672 shell TEXT
673 );
674
675 CREATE TABLE groups (
676 groupname TEXT,
677 gid INTEGER,
678 members TEXT
679 );
680 EOS
681
682 unless (close($fh)) {
683 die("Can't write $db_script: $!");
684 }
685
686 } else {
687 die("Can't open $db_script: $!");
688 }
689
690 my $cmd = "sqlite3 $db_file < $db_script";
691 build_db($cmd, $db_script);
692
693 # Make sure that, if we're running as root, the database file has
694 # the permissions/privs set for use by proftpd
695 if ($< == 0) {
696 unless (chmod(0666, $db_file)) {
697 die("Can't set perms on $db_file to 0666: $!");
698 }
699 }
700
701 my $config = {
702 PidFile => $pid_file,
703 ScoreboardFile => $scoreboard_file,
704 SystemLog => $log_file,
705
706 MaxLoginAttempts => 100,
707
708 IfModules => {
709 'mod_delay.c' => {
710 DelayEngine => 'off',
711 },
712
713 'mod_sql.c' => {
714 SQLAuthTypes => 'plaintext',
715 SQLBackend => 'sqlite3',
716 SQLConnectInfo => $db_file,
717 SQLLogFile => $log_file,
718 },
719 },
720 };
721
722 my ($port, $config_user, $config_group) = config_write($config_file, $config);
723
724 # Open pipes, for use between the parent and child processes. Specifically,
725 # the child will indicate when it's done with its test by writing a message
726 # to the parent.
727 my ($rfh, $wfh);
728 unless (pipe($rfh, $wfh)) {
729 die("Can't open pipe: $!");
730 }
731
732 my $ex;
733
734 # Fork child
735 $self->handle_sigchld();
736 defined(my $pid = fork()) or die("Can't fork: $!");
737 if ($pid) {
738 eval {
739 # Bug#2922 would allow a login which should NOT happen. So if
740 # *any* of the following logins succeed, we have a problem.
741
742 my $accounts = [
743 { user => 'bin', passwd => '*' },
744 { user => 'bin', passwd => 'x' },
745 { user => 'daemon', passwd => '*' },
746 { user => 'daemon', passwd => 'x' },
747 { user => 'mysql', passwd => '*' },
748 { user => 'mysql', passwd => 'x' },
749 ];
750
751 foreach my $account (@$accounts) {
752 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
753 my $user = $account->{user};
754 my $passwd = $account->{passwd};
755
756 eval { $client->login($user, $passwd) };
757 unless ($@) {
758 die("Logged in unexpectedly");
759 }
760 }
761 };
762
763 if ($@) {
764 $ex = $@;
765 }
766
767 $wfh->print("done\n");
768 $wfh->flush();
769
770 } else {
771 eval { server_wait($config_file, $rfh, 30) };
772 if ($@) {
773 warn($@);
774 exit 1;
775 }
776
777 exit 0;
778 }
779
780 # Stop server
781 server_stop($pid_file);
782
783 $self->assert_child_ok($pid);
784
785 if ($ex) {
786 test_append_logfile($log_file, $ex);
787 unlink($log_file);
788
789 die($ex);
790 }
791
792 unlink($log_file);
793 }
794
795 sub sql_bug3116 {
796 my $self = shift;
797 my $tmpdir = $self->{tmpdir};
798
799 my $config_file = "$tmpdir/sqlite.conf";
800 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
801 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
802
803 my $log_file = test_get_logfile();
804
805 # Bug#3116 occurred because mod_sql was treating percent signs in user
806 # (and group) names as variables to be substituted.
807
808 my $user = 'proftpd%proftpd.org';
809 my $passwd = 'test';
810 my $group = 'ftpd';
811 my $home_dir = File::Spec->rel2abs($tmpdir);
812
813 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
814
815 # Build up sqlite3 command to create users, groups tables and populate them
816 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
817
818 if (open(my $fh, "> $db_script")) {
819 print $fh <<EOS;
820 CREATE TABLE users (
821 userid TEXT,
822 passwd TEXT,
823 uid INTEGER,
824 gid INTEGER,
825 homedir TEXT,
826 shell TEXT
827 );
828 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
829
830 CREATE TABLE groups (
831 groupname TEXT,
832 gid INTEGER,
833 members TEXT
834 );
835 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
836 INSERT INTO groups (groupname, gid, members) VALUES ('ftpadm', 501, '$user');
837 EOS
838
839 unless (close($fh)) {
840 die("Can't write $db_script: $!");
841 }
842
843 } else {
844 die("Can't open $db_script: $!");
845 }
846
847 my $cmd = "sqlite3 -echo $db_file < $db_script 2>&1";
848 build_db($cmd, $db_script);
849
850 # Make sure that, if we're running as root, the database file has
851 # the permissions/privs set for use by proftpd
852 if ($< == 0) {
853 unless (chmod(0666, $db_file)) {
854 die("Can't set perms on $db_file to 0666: $!");
855 }
856 }
857
858 my $config = {
859 PidFile => $pid_file,
860 ScoreboardFile => $scoreboard_file,
861 SystemLog => $log_file,
862
863 IfModules => {
864 'mod_delay.c' => {
865 DelayEngine => 'off',
866 },
867
868 'mod_sql.c' => {
869 SQLAuthTypes => 'plaintext',
870 SQLBackend => 'sqlite3',
871 SQLConnectInfo => $db_file,
872 SQLLogFile => $log_file,
873 },
874 },
875 };
876
877 my ($port, $config_user, $config_group) = config_write($config_file, $config);
878
879 # Open pipes, for use between the parent and child processes. Specifically,
880 # the child will indicate when it's done with its test by writing a message
881 # to the parent.
882 my ($rfh, $wfh);
883 unless (pipe($rfh, $wfh)) {
884 die("Can't open pipe: $!");
885 }
886
887 my $ex;
888
889 # Fork child
890 $self->handle_sigchld();
891 defined(my $pid = fork()) or die("Can't fork: $!");
892 if ($pid) {
893 eval {
894 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
895
896 # Bug#3116 would prevent a login from occurring, so we only need to
897 # attempt a login.
898 $client->login($user, $passwd, 5);
899 };
900
901 if ($@) {
902 $ex = $@;
903 }
904
905 $wfh->print("done\n");
906 $wfh->flush();
907
908 } else {
909 eval { server_wait($config_file, $rfh) };
910 if ($@) {
911 warn($@);
912 exit 1;
913 }
914
915 exit 0;
916 }
917
918 # Stop server
919 server_stop($pid_file);
920
921 $self->assert_child_ok($pid);
922
923 if ($ex) {
924 test_append_logfile($log_file, $ex);
925 unlink($log_file);
926
927 die($ex);
928 }
929
930 unlink($log_file);
931 }
932
933 sub sql_bug3124 {
934 my $self = shift;
935 my $tmpdir = $self->{tmpdir};
936
937 my $config_file = "$tmpdir/sqlite.conf";
938 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
939 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
940
941 my $log_file = test_get_logfile();
942
943 my $user = 'proftpd';
944 my $passwd = 'test';
945 my $group = 'ftpd';
946 my $home_dir = File::Spec->rel2abs($tmpdir);
947
948 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
949
950 # Build up sqlite3 command to create users, groups tables and populate them
951 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
952
953 if (open(my $fh, "> $db_script")) {
954 print $fh <<EOS;
955 CREATE TABLE users (
956 userid TEXT,
957 passwd TEXT,
958 uid INTEGER,
959 gid INTEGER,
960 homedir TEXT,
961 shell TEXT
962 );
963 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
964
965 CREATE TABLE groups (
966 groupname TEXT,
967 gid INTEGER,
968 members TEXT
969 );
970 EOS
971
972 unless (close($fh)) {
973 die("Can't write $db_script: $!");
974 }
975
976 } else {
977 die("Can't open $db_script: $!");
978 }
979
980 my $cmd = "sqlite3 $db_file < $db_script";
981 build_db($cmd, $db_script);
982
983 # Make sure that, if we're running as root, the database file has
984 # the permissions/privs set for use by proftpd
985 if ($< == 0) {
986 unless (chmod(0666, $db_file)) {
987 die("Can't set perms on $db_file to 0666: $!");
988 }
989 }
990
991 # Bug#3124 occurred when SQLNegativeCache was on, and there was no group
992 # info; it would cause a segfault.
993
994 my $config = {
995 PidFile => $pid_file,
996 ScoreboardFile => $scoreboard_file,
997 SystemLog => $log_file,
998
999 IfModules => {
1000 'mod_delay.c' => {
1001 DelayEngine => 'off',
1002 },
1003
1004 'mod_sql.c' => {
1005 SQLAuthTypes => 'plaintext',
1006 SQLBackend => 'sqlite3',
1007 SQLConnectInfo => $db_file,
1008 SQLLogFile => $log_file,
1009 SQLNegativeCache => 'on',
1010 },
1011 },
1012 };
1013
1014 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1015
1016 # Open pipes, for use between the parent and child processes. Specifically,
1017 # the child will indicate when it's done with its test by writing a message
1018 # to the parent.
1019 my ($rfh, $wfh);
1020 unless (pipe($rfh, $wfh)) {
1021 die("Can't open pipe: $!");
1022 }
1023
1024 my $ex;
1025
1026 # Fork child
1027 $self->handle_sigchld();
1028 defined(my $pid = fork()) or die("Can't fork: $!");
1029 if ($pid) {
1030 eval {
1031 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1032
1033 # Bug#3124 would prevent a login from occurring, so we only need to
1034 # attempt a login.
1035 $client->login($user, $passwd, 5);
1036 };
1037
1038 if ($@) {
1039 $ex = $@;
1040 }
1041
1042 $wfh->print("done\n");
1043 $wfh->flush();
1044
1045 } else {
1046 eval { server_wait($config_file, $rfh) };
1047 if ($@) {
1048 warn($@);
1049 exit 1;
1050 }
1051
1052 exit 0;
1053 }
1054
1055 # Stop server
1056 server_stop($pid_file);
1057
1058 $self->assert_child_ok($pid);
1059
1060 if ($ex) {
1061 test_append_logfile($log_file, $ex);
1062 unlink($log_file);
1063
1064 die($ex);
1065 }
1066
1067 unlink($log_file);
1068 }
1069
1070 sub sql_sqlite_bug3126 {
1071 my $self = shift;
1072 my $tmpdir = $self->{tmpdir};
1073
1074 my $config_file = "$tmpdir/sqlite.conf";
1075 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
1076 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
1077
1078 my $log_file = test_get_logfile();
1079
1080 my $user = 'proftpd';
1081 my $passwd = 'test';
1082 my $group = 'ftpd';
1083 my $home_dir = File::Spec->rel2abs($tmpdir);
1084
1085 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1086
1087 # Build up sqlite3 command to create users, groups tables and populate them
1088 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1089
1090 if (open(my $fh, "> $db_script")) {
1091 print $fh <<EOS;
1092 CREATE TABLE users (
1093 userid TEXT,
1094 passwd TEXT,
1095 uid INTEGER,
1096 gid INTEGER,
1097 homedir TEXT,
1098 shell TEXT
1099 );
1100 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', 500, 500, '$home_dir', '/bin/bash');
1101
1102 CREATE TABLE groups (
1103 groupname TEXT,
1104 gid INTEGER,
1105 members TEXT
1106 );
1107 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user');
1108 INSERT INTO groups (groupname, gid, members) VALUES ('ftpadm', 501, '$user');
1109 EOS
1110
1111 unless (close($fh)) {
1112 die("Can't write $db_script: $!");
1113 }
1114
1115 } else {
1116 die("Can't open $db_script: $!");
1117 }
1118
1119 my $cmd = "sqlite3 $db_file < $db_script";
1120 build_db($cmd, $db_script);
1121
1122 # Make sure that, if we're running as root, the database file has
1123 # the permissions/privs set for use by proftpd
1124 if ($< == 0) {
1125 unless (chmod(0666, $db_file)) {
1126 die("Can't set perms on $db_file to 0666: $!");
1127 }
1128 }
1129
1130 my $config = {
1131 PidFile => $pid_file,
1132 ScoreboardFile => $scoreboard_file,
1133 SystemLog => $log_file,
1134
1135 IfModules => {
1136 'mod_delay.c' => {
1137 DelayEngine => 'off',
1138 },
1139
1140 'mod_sql.c' => {
1141 SQLAuthTypes => 'plaintext',
1142 SQLBackend => 'sqlite3',
1143 SQLConnectInfo => $db_file,
1144 SQLLogFile => $log_file,
1145 },
1146 },
1147 };
1148
1149 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1150
1151 # Open pipes, for use between the parent and child processes. Specifically,
1152 # the child will indicate when it's done with its test by writing a message
1153 # to the parent.
1154 my ($rfh, $wfh);
1155 unless (pipe($rfh, $wfh)) {
1156 die("Can't open pipe: $!");
1157 }
1158
1159 my $ex;
1160
1161 # Fork child
1162 $self->handle_sigchld();
1163 defined(my $pid = fork()) or die("Can't fork: $!");
1164 if ($pid) {
1165 eval {
1166 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1167
1168 # Bug#3126 would prevent a login from occurring, so we only need to
1169 # attempt a login.
1170 $client->login($user, $passwd, 5);
1171 };
1172
1173 if ($@) {
1174 $ex = $@;
1175 }
1176
1177 $wfh->print("done\n");
1178 $wfh->flush();
1179
1180 } else {
1181 eval { server_wait($config_file, $rfh) };
1182 if ($@) {
1183 warn($@);
1184 exit 1;
1185 }
1186
1187 exit 0;
1188 }
1189
1190 # Stop server
1191 server_stop($pid_file);
1192
1193 $self->assert_child_ok($pid);
1194
1195 if ($ex) {
1196 test_append_logfile($log_file, $ex);
1197 unlink($log_file);
1198
1199 die($ex);
1200 }
1201
1202 unlink($log_file);
1203 }
1204
1205 sub sql_user_where_clause_ok {
1206 my $self = shift;
1207 my $tmpdir = $self->{tmpdir};
1208
1209 my $config_file = "$tmpdir/sqlite.conf";
1210 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
1211 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
1212
1213 my $log_file = test_get_logfile();
1214
1215 my $user1 = 'proftpd';
1216 my $user2 = 'proftpd2';
1217 my $passwd = 'test';
1218 my $group = 'ftpd';
1219 my $home_dir = File::Spec->rel2abs($tmpdir);
1220
1221 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1222
1223 # Build up sqlite3 command to create users, groups tables and populate them
1224 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1225
1226 if (open(my $fh, "> $db_script")) {
1227 print $fh <<EOS;
1228 CREATE TABLE users (
1229 userid TEXT,
1230 passwd TEXT,
1231 uid INTEGER,
1232 gid INTEGER,
1233 homedir TEXT,
1234 shell TEXT,
1235 allowed TEXT
1236 );
1237 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, allowed) VALUES ('$user1', '$passwd', 500, 500, '$home_dir', '/bin/bash', 'false');
1238 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, allowed) VALUES ('$user2', '$passwd', 500, 500, '$home_dir', '/bin/bash', 'true');
1239
1240 CREATE TABLE groups (
1241 groupname TEXT,
1242 gid INTEGER,
1243 members TEXT
1244 );
1245 INSERT INTO groups (groupname, gid, members) VALUES ('$group', 500, '$user1,$user2');
1246 EOS
1247
1248 unless (close($fh)) {
1249 die("Can't write $db_script: $!");
1250 }
1251
1252 } else {
1253 die("Can't open $db_script: $!");
1254 }
1255
1256 my $cmd = "sqlite3 $db_file < $db_script";
1257 build_db($cmd, $db_script);
1258
1259 # Make sure that, if we're running as root, the database file has
1260 # the permissions/privs set for use by proftpd
1261 if ($< == 0) {
1262 unless (chmod(0666, $db_file)) {
1263 die("Can't set perms on $db_file to 0666: $!");
1264 }
1265 }
1266
1267 my $config = {
1268 PidFile => $pid_file,
1269 ScoreboardFile => $scoreboard_file,
1270 SystemLog => $log_file,
1271
1272 IfModules => {
1273 'mod_delay.c' => {
1274 DelayEngine => 'off',
1275 },
1276
1277 'mod_sql.c' => {
1278 SQLAuthTypes => 'plaintext',
1279 SQLBackend => 'sqlite3',
1280 SQLConnectInfo => $db_file,
1281 SQLLogFile => $log_file,
1282 SQLUserWhereClause => '"allowed = \'true\'"',
1283 },
1284 },
1285 };
1286
1287 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1288
1289 # Open pipes, for use between the parent and child processes. Specifically,
1290 # the child will indicate when it's done with its test by writing a message
1291 # to the parent.
1292 my ($rfh, $wfh);
1293 unless (pipe($rfh, $wfh)) {
1294 die("Can't open pipe: $!");
1295 }
1296
1297 my $ex;
1298
1299 # Fork child
1300 $self->handle_sigchld();
1301 defined(my $pid = fork()) or die("Can't fork: $!");
1302 if ($pid) {
1303 eval {
1304 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1305
1306 # This account should not be allowed to login based on the
1307 # SQLUserWhereClause...
1308 eval { $client->login($user1, $passwd) };
1309 unless ($@) {
1310 die("Login for user '$user1' succeeded unexpectedly");
1311 }
1312
1313 # ...but this one should succeed.
1314 $client->login($user2, $passwd);
1315 };
1316
1317 if ($@) {
1318 $ex = $@;
1319 }
1320
1321 $wfh->print("done\n");
1322 $wfh->flush();
1323
1324 } else {
1325 eval { server_wait($config_file, $rfh) };
1326 if ($@) {
1327 warn($@);
1328 exit 1;
1329 }
1330
1331 exit 0;
1332 }
1333
1334 # Stop server
1335 server_stop($pid_file);
1336
1337 $self->assert_child_ok($pid);
1338
1339 if ($ex) {
1340 test_append_logfile($log_file, $ex);
1341 unlink($log_file);
1342
1343 die($ex);
1344 }
1345
1346 unlink($log_file);
1347 }
1348
1349 sub sql_user_where_clause_with_vars_ok {
1350 my $self = shift;
1351 my $tmpdir = $self->{tmpdir};
1352 my $setup = test_setup($tmpdir, 'sqlite');
1353
1354 my $user2 = 'proftpd2';
1355
1356 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1357
1358 # Build up sqlite3 command to create users, groups tables and populate them
1359 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1360
1361 if (open(my $fh, "> $db_script")) {
1362 print $fh <<EOS;
1363 CREATE TABLE users (
1364 userid TEXT,
1365 passwd TEXT,
1366 uid INTEGER,
1367 gid INTEGER,
1368 homedir TEXT,
1369 shell TEXT,
1370 allowed_ip TEXT
1371 );
1372 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, allowed_ip) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash', '1.2.3.4');
1373 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, allowed_ip) VALUES ('$user2', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash', '127.0.0.1');
1374
1375 CREATE TABLE groups (
1376 groupname TEXT,
1377 gid INTEGER,
1378 members TEXT
1379 );
1380 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user},$user2');
1381 EOS
1382
1383 unless (close($fh)) {
1384 die("Can't write $db_script: $!");
1385 }
1386
1387 } else {
1388 die("Can't open $db_script: $!");
1389 }
1390
1391 my $cmd = "sqlite3 $db_file < $db_script";
1392 build_db($cmd, $db_script);
1393
1394 # Make sure that, if we're running as root, the database file has
1395 # the permissions/privs set for use by proftpd
1396 if ($< == 0) {
1397 unless (chmod(0666, $db_file)) {
1398 die("Can't set perms on $db_file to 0666: $!");
1399 }
1400 }
1401
1402 my $config = {
1403 PidFile => $setup->{pid_file},
1404 ScoreboardFile => $setup->{scoreboard_file},
1405 SystemLog => $setup->{log_file},
1406 TraceLog => $setup->{log_file},
1407 Trace => 'jot:20 sql:20',
1408
1409 IfModules => {
1410 'mod_delay.c' => {
1411 DelayEngine => 'off',
1412 },
1413
1414 'mod_sql.c' => {
1415 SQLAuthTypes => 'plaintext',
1416 SQLBackend => 'sqlite3',
1417 SQLConnectInfo => $db_file,
1418 SQLLogFile => $setup->{log_file},
1419 SQLUserWhereClause => '"allowed_ip = \'%L\'"',
1420 },
1421 },
1422 };
1423
1424 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1425 $config);
1426
1427 # Open pipes, for use between the parent and child processes. Specifically,
1428 # the child will indicate when it's done with its test by writing a message
1429 # to the parent.
1430 my ($rfh, $wfh);
1431 unless (pipe($rfh, $wfh)) {
1432 die("Can't open pipe: $!");
1433 }
1434
1435 my $ex;
1436
1437 # Fork child
1438 $self->handle_sigchld();
1439 defined(my $pid = fork()) or die("Can't fork: $!");
1440 if ($pid) {
1441 eval {
1442 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1443
1444 # This account should not be allowed to login based on the
1445 # SQLUserWhereClause...
1446 eval { $client->login($setup->{user}, $setup->{passwd}) };
1447 unless ($@) {
1448 die("Login for user '$setup->{user}' succeeded unexpectedly");
1449 }
1450
1451 # ...but this one should succeed.
1452 $client->login($user2, $setup->{passwd});
1453 $client->quit();
1454 };
1455 if ($@) {
1456 $ex = $@;
1457 }
1458
1459 $wfh->print("done\n");
1460 $wfh->flush();
1461
1462 } else {
1463 eval { server_wait($setup->{config_file}, $rfh) };
1464 if ($@) {
1465 warn($@);
1466 exit 1;
1467 }
1468
1469 exit 0;
1470 }
1471
1472 # Stop server
1473 server_stop($setup->{pid_file});
1474 $self->assert_child_ok($pid);
1475
1476 test_cleanup($setup->{log_file}, $ex);
1477 }
1478
1479 sub sql_group_where_clause_ok {
1480 my $self = shift;
1481 my $tmpdir = $self->{tmpdir};
1482
1483 my $config_file = "$tmpdir/sqlite.conf";
1484 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
1485 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
1486
1487 my $log_file = test_get_logfile();
1488
1489 my $user = 'proftpd';
1490 my $passwd = 'test';
1491 my $group = 'ftpd';
1492 my $home_dir = File::Spec->rel2abs($tmpdir);
1493 my $uid = 500;
1494 my $gid= 500;
1495
1496 my $acl_group = 'whitelist';
1497
1498 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1499
1500 # Build up sqlite3 command to create users, groups tables and populate them
1501 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1502
1503 if (open(my $fh, "> $db_script")) {
1504 print $fh <<EOS;
1505 CREATE TABLE users (
1506 userid TEXT,
1507 passwd TEXT,
1508 uid INTEGER,
1509 gid INTEGER,
1510 homedir TEXT,
1511 shell TEXT
1512 );
1513 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1514
1515 CREATE TABLE groups (
1516 groupname TEXT,
1517 gid INTEGER,
1518 members TEXT,
1519 allowed TEXT
1520 );
1521 INSERT INTO groups (groupname, gid, members, allowed) VALUES ('$group', $gid, '$user', 'false');
1522 INSERT INTO groups (groupname, gid, members, allowed) VALUES ('$acl_group', $gid, '$user', 'true');
1523 EOS
1524
1525 unless (close($fh)) {
1526 die("Can't write $db_script: $!");
1527 }
1528
1529 } else {
1530 die("Can't open $db_script: $!");
1531 }
1532
1533 my $cmd = "sqlite3 $db_file < $db_script";
1534 build_db($cmd, $db_script);
1535
1536 # Make sure that, if we're running as root, the database file has
1537 # the permissions/privs set for use by proftpd
1538 if ($< == 0) {
1539 unless (chmod(0666, $db_file)) {
1540 die("Can't set perms on $db_file to 0666: $!");
1541 }
1542 }
1543
1544 my $config = {
1545 PidFile => $pid_file,
1546 ScoreboardFile => $scoreboard_file,
1547 SystemLog => $log_file,
1548
1549 IfModules => {
1550 'mod_delay.c' => {
1551 DelayEngine => 'off',
1552 },
1553
1554 'mod_sql.c' => {
1555 AuthOrder => 'mod_sql.c',
1556
1557 SQLAuthenticate => 'users groups',
1558 SQLAuthTypes => 'plaintext',
1559 SQLBackend => 'sqlite3',
1560 SQLConnectInfo => $db_file,
1561 SQLLogFile => $log_file,
1562 SQLGroupWhereClause => '"allowed = \'true\'"',
1563 SQLOptions => 'useNormalizedGroupSchema',
1564 },
1565 },
1566 };
1567
1568 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1569
1570 if (open(my $fh, ">> $config_file")) {
1571 print $fh <<EOC;
1572
1573 <Limit LOGIN>
1574 AllowGroup $acl_group
1575 DenyAll
1576 </Limit>
1577 EOC
1578 unless (close($fh)) {
1579 die("Can't write $config_file: $!");
1580 }
1581
1582 } else {
1583 die("Can't open $config_file: $!");
1584 }
1585
1586 # Open pipes, for use between the parent and child processes. Specifically,
1587 # the child will indicate when it's done with its test by writing a message
1588 # to the parent.
1589 my ($rfh, $wfh);
1590 unless (pipe($rfh, $wfh)) {
1591 die("Can't open pipe: $!");
1592 }
1593
1594 my $ex;
1595
1596 # Fork child
1597 $self->handle_sigchld();
1598 defined(my $pid = fork()) or die("Can't fork: $!");
1599 if ($pid) {
1600 eval {
1601 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1602 $client->login($user, $passwd);
1603 $client->quit();
1604 };
1605
1606 if ($@) {
1607 $ex = $@;
1608 }
1609
1610 $wfh->print("done\n");
1611 $wfh->flush();
1612
1613 } else {
1614 eval { server_wait($config_file, $rfh) };
1615 if ($@) {
1616 warn($@);
1617 exit 1;
1618 }
1619
1620 exit 0;
1621 }
1622
1623 # Stop server
1624 server_stop($pid_file);
1625
1626 $self->assert_child_ok($pid);
1627
1628 if ($ex) {
1629 test_append_logfile($log_file, $ex);
1630 unlink($log_file);
1631
1632 die($ex);
1633 }
1634
1635 unlink($log_file);
1636 }
1637
1638 sub sql_bug3149 {
1639 my $self = shift;
1640 my $tmpdir = $self->{tmpdir};
1641 my $setup = test_setup($tmpdir, 'sqlite');
1642
1643 my $user2 = 'proftpd2';
1644
1645 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1646
1647 # Build up sqlite3 command to create users, groups tables and populate them
1648 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1649
1650 if (open(my $fh, "> $db_script")) {
1651 print $fh <<EOS;
1652 CREATE TABLE users (
1653 userid TEXT,
1654 passwd TEXT,
1655 uid INTEGER,
1656 gid INTEGER,
1657 homedir TEXT,
1658 shell TEXT,
1659 host TEXT
1660 );
1661 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, host) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash', 'remotehost');
1662 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, host) VALUES ('$user2', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash', '127.0.0.1');
1663
1664 CREATE TABLE groups (
1665 groupname TEXT,
1666 gid INTEGER,
1667 members TEXT
1668 );
1669 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user},$user2');
1670 EOS
1671
1672 unless (close($fh)) {
1673 die("Can't write $db_script: $!");
1674 }
1675
1676 } else {
1677 die("Can't open $db_script: $!");
1678 }
1679
1680 my $cmd = "sqlite3 $db_file < $db_script";
1681 build_db($cmd, $db_script);
1682
1683 # Make sure that, if we're running as root, the database file has
1684 # the permissions/privs set for use by proftpd
1685 if ($< == 0) {
1686 unless (chmod(0666, $db_file)) {
1687 die("Can't set perms on $db_file to 0666: $!");
1688 }
1689 }
1690
1691 my $config = {
1692 PidFile => $setup->{pid_file},
1693 ScoreboardFile => $setup->{scoreboard_file},
1694 SystemLog => $setup->{log_file},
1695 TraceLog => $setup->{log_file},
1696 Trace => 'jot:20 sql:20',
1697
1698 IfModules => {
1699 'mod_delay.c' => {
1700 DelayEngine => 'off',
1701 },
1702
1703 # Bug#3149 occurred because mod_sql's resolve_short_tag() function
1704 # was not able to resolve %V properly (it was deferencing a bad pointer).
1705 'mod_sql.c' => {
1706 SQLAuthTypes => 'plaintext',
1707 SQLBackend => 'sqlite3',
1708 SQLConnectInfo => $db_file,
1709 SQLLogFile => $setup->{log_file},
1710 SQLUserWhereClause => '"host=\'%V\'"',
1711 },
1712 },
1713 };
1714
1715 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1716 $config);
1717
1718 # Open pipes, for use between the parent and child processes. Specifically,
1719 # the child will indicate when it's done with its test by writing a message
1720 # to the parent.
1721 my ($rfh, $wfh);
1722 unless (pipe($rfh, $wfh)) {
1723 die("Can't open pipe: $!");
1724 }
1725
1726 my $ex;
1727
1728 # Fork child
1729 $self->handle_sigchld();
1730 defined(my $pid = fork()) or die("Can't fork: $!");
1731 if ($pid) {
1732 eval {
1733 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1734
1735 # This account should not be allowed to login based on the
1736 # SQLUserWhereClause...
1737 eval { $client->login($setup->{user}, $setup->{passwd}) };
1738 unless ($@) {
1739 die("Login for user '$setup->{user}' succeeded unexpectedly");
1740 }
1741
1742 # ...but this one should succeed.
1743 $client->login($user2, $setup->{passwd});
1744 $client->quit();
1745 };
1746 if ($@) {
1747 $ex = $@;
1748 }
1749
1750 $wfh->print("done\n");
1751 $wfh->flush();
1752
1753 } else {
1754 eval { server_wait($setup->{config_file}, $rfh) };
1755 if ($@) {
1756 warn($@);
1757 exit 1;
1758 }
1759
1760 exit 0;
1761 }
1762
1763 # Stop server
1764 server_stop($setup->{pid_file});
1765 $self->assert_child_ok($pid);
1766
1767 test_cleanup($setup->{log_file}, $ex);
1768 }
1769
1770 sub get_sessions {
1771 my $db_file = shift;
1772 my $where = shift;
1773
1774 my $sql = "SELECT user, ip_addr, timestamp FROM ftpsessions";
1775 if ($where) {
1776 $sql .= " WHERE $where";
1777 }
1778
1779 my $cmd = "sqlite3 $db_file \"$sql\"";
1780
1781 if ($ENV{TEST_VERBOSE}) {
1782 print STDERR "Executing sqlite3: $cmd\n";
1783 }
1784
1785 my $res = join('', `$cmd`);
1786 chomp($res);
1787
1788 # The default sqlite3 delimiter is '|'
1789 return split(/\|/, $res);
1790 }
1791
1792 sub sql_sqllog {
1793 my $self = shift;
1794 my $tmpdir = $self->{tmpdir};
1795 my $setup = test_setup($tmpdir, 'sqlite');
1796
1797 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1798
1799 # Build up sqlite3 command to create users, groups tables and populate them
1800 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1801
1802 if (open(my $fh, "> $db_script")) {
1803 print $fh <<EOS;
1804 CREATE TABLE ftpsessions (
1805 user TEXT,
1806 ip_addr TEXT,
1807 timestamp TEXT
1808 );
1809 EOS
1810
1811 unless (close($fh)) {
1812 die("Can't write $db_script: $!");
1813 }
1814
1815 } else {
1816 die("Can't open $db_script: $!");
1817 }
1818
1819 my $cmd = "sqlite3 $db_file < $db_script";
1820 build_db($cmd, $db_script);
1821
1822 # Make sure that, if we're running as root, the database file has
1823 # the permissions/privs set for use by proftpd
1824 if ($< == 0) {
1825 unless (chmod(0666, $db_file)) {
1826 die("Can't set perms on $db_file to 0666: $!");
1827 }
1828 }
1829
1830 my $config = {
1831 PidFile => $setup->{pid_file},
1832 ScoreboardFile => $setup->{scoreboard_file},
1833 SystemLog => $setup->{log_file},
1834 TraceLog => $setup->{log_file},
1835 Trace => 'jot:20 sql:20',
1836
1837 AuthUserFile => $setup->{auth_user_file},
1838 AuthGroupFile => $setup->{auth_group_file},
1839 AuthOrder => 'mod_auth_file.c',
1840
1841 IfModules => {
1842 'mod_delay.c' => {
1843 DelayEngine => 'off',
1844 },
1845
1846 'mod_sql.c' => {
1847 SQLEngine => 'log',
1848 SQLBackend => 'sqlite3',
1849 SQLConnectInfo => $db_file,
1850 SQLLogFile => $setup->{log_file},
1851 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, timestamp) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')"',
1852 SQLLog => 'PASS session_start',
1853 },
1854 },
1855 };
1856
1857 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1858 $config);
1859
1860 # Open pipes, for use between the parent and child processes. Specifically,
1861 # the child will indicate when it's done with its test by writing a message
1862 # to the parent.
1863 my ($rfh, $wfh);
1864 unless (pipe($rfh, $wfh)) {
1865 die("Can't open pipe: $!");
1866 }
1867
1868 my $ex;
1869
1870 # Fork child
1871 $self->handle_sigchld();
1872 defined(my $pid = fork()) or die("Can't fork: $!");
1873 if ($pid) {
1874 eval {
1875 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1876 $client->login($setup->{user}, $setup->{passwd});
1877 $client->quit();
1878 };
1879 if ($@) {
1880 $ex = $@;
1881 }
1882
1883 $wfh->print("done\n");
1884 $wfh->flush();
1885
1886 } else {
1887 eval { server_wait($setup->{config_file}, $rfh) };
1888 if ($@) {
1889 warn($@);
1890 exit 1;
1891 }
1892
1893 exit 0;
1894 }
1895
1896 # Stop server
1897 server_stop($setup->{pid_file});
1898 $self->assert_child_ok($pid);
1899
1900 if ($ex) {
1901 test_cleanup($setup->{log_file}, $ex);
1902 }
1903
1904 eval {
1905 my ($login, $ip_addr, $timestamp) = get_sessions($db_file,
1906 "user = \'$setup->{user}\'");
1907
1908 my $expected = $setup->{user};
1909 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
1910
1911 $expected = '127.0.0.1';
1912 $self->assert($expected eq $ip_addr,
1913 "Expected '$expected', got '$ip_addr'");
1914
1915 $expected = '\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}';
1916 $self->assert(qr/$expected/, $timestamp,
1917 "Expected '$expected', got '$timestamp'");
1918 };
1919 if ($@) {
1920 $ex = $@;
1921 }
1922
1923 test_cleanup($setup->{log_file}, $ex);
1924 }
1925
1926 sub sql_sqlite_sqllog_with_chroot {
1927 my $self = shift;
1928 my $tmpdir = $self->{tmpdir};
1929
1930 my $config_file = "$tmpdir/sqlite.conf";
1931 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
1932 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
1933
1934 my $log_file = test_get_logfile();
1935
1936 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
1937 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
1938
1939 my $user = 'proftpd';
1940 my $passwd = 'test';
1941 my $group = 'ftpd';
1942 my $home_dir = File::Spec->rel2abs($tmpdir);
1943 my $uid = 500;
1944 my $gid = 500;
1945
1946 # Make sure that, if we're running as root, that the home directory has
1947 # permissions/privs set for the account we create
1948 if ($< == 0) {
1949 unless (chmod(0755, $home_dir)) {
1950 die("Can't set perms on $home_dir to 0755: $!");
1951 }
1952
1953 unless (chown($uid, $gid, $home_dir)) {
1954 die("Can't set owner of $home_dir to $uid/$gid: $!");
1955 }
1956 }
1957
1958 my $tmp_dir = File::Spec->rel2abs("$tmpdir/tmp");
1959 mkpath($tmp_dir);
1960 if ($< == 0) {
1961 unless (chmod(0777, $tmp_dir)) {
1962 die("Can't set perms on $tmp_dir to 0777: $!");
1963 }
1964 }
1965
1966 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1967 '/bin/bash');
1968 auth_group_write($auth_group_file, '$group', $gid, $user);
1969
1970 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1971
1972 # Build up sqlite3 command to create users, groups tables and populate them
1973 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1974
1975 if (open(my $fh, "> $db_script")) {
1976 print $fh <<EOS;
1977 CREATE TABLE ftpsessions (
1978 user TEXT,
1979 ip_addr TEXT,
1980 timestamp TEXT
1981 );
1982 EOS
1983
1984 unless (close($fh)) {
1985 die("Can't write $db_script: $!");
1986 }
1987
1988 } else {
1989 die("Can't open $db_script: $!");
1990 }
1991
1992 my $cmd = "sqlite3 $db_file < $db_script";
1993 build_db($cmd, $db_script);
1994
1995 # Make sure that, if we're running as root, the database file has
1996 # the permissions/privs set for use by proftpd
1997 if ($< == 0) {
1998 unless (chmod(0666, $db_file)) {
1999 die("Can't set perms on $db_file to 0666: $!");
2000 }
2001 }
2002
2003 my $config = {
2004 PidFile => $pid_file,
2005 ScoreboardFile => $scoreboard_file,
2006 SystemLog => $log_file,
2007
2008 AuthUserFile => $auth_user_file,
2009 AuthGroupFile => $auth_group_file,
2010 AuthOrder => 'mod_auth_file.c',
2011
2012 DefaultRoot => '~',
2013
2014 IfModules => {
2015 'mod_delay.c' => {
2016 DelayEngine => 'off',
2017 },
2018
2019 'mod_sql.c' => {
2020 SQLEngine => 'log',
2021 SQLBackend => 'sqlite3',
2022 SQLConnectInfo => "$db_file foo bar PERCONNECTION",
2023 SQLLogFile => $log_file,
2024 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, timestamp) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')"',
2025 SQLLog => 'PASS session_start',
2026 },
2027 },
2028 };
2029
2030 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2031
2032 # Open pipes, for use between the parent and child processes. Specifically,
2033 # the child will indicate when it's done with its test by writing a message
2034 # to the parent.
2035 my ($rfh, $wfh);
2036 unless (pipe($rfh, $wfh)) {
2037 die("Can't open pipe: $!");
2038 }
2039
2040 my $ex;
2041
2042 # Fork child
2043 $self->handle_sigchld();
2044 defined(my $pid = fork()) or die("Can't fork: $!");
2045 if ($pid) {
2046 eval {
2047 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2048
2049 $client->login($user, $passwd);
2050 };
2051
2052 if ($@) {
2053 $ex = $@;
2054 }
2055
2056 $wfh->print("done\n");
2057 $wfh->flush();
2058
2059 } else {
2060 eval { server_wait($config_file, $rfh) };
2061 if ($@) {
2062 warn($@);
2063 exit 1;
2064 }
2065
2066 exit 0;
2067 }
2068
2069 # Stop server
2070 server_stop($pid_file);
2071
2072 $self->assert_child_ok($pid);
2073
2074 if ($ex) {
2075 test_append_logfile($log_file, $ex);
2076 unlink($log_file);
2077
2078 die($ex);
2079 }
2080
2081 my ($login, $ip_addr, $timestamp) = get_sessions($db_file,
2082 "user = \'$user\'");
2083
2084 my $expected;
2085
2086 $expected = $user;
2087 $self->assert($expected eq $login,
2088 test_msg("Expected '$expected', got '$login'"));
2089
2090 $expected = '127.0.0.1';
2091 $self->assert($expected eq $ip_addr,
2092 test_msg("Expected '$expected', got '$ip_addr'"));
2093
2094 $expected = '\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}';
2095 $self->assert(qr/$expected/, $timestamp,
2096 test_msg("Expected '$expected', got '$timestamp'"));
2097
2098 unlink($log_file);
2099 }
2100
2101 sub sql_user_info_different_table_names {
2102 my $self = shift;
2103 my $tmpdir = $self->{tmpdir};
2104
2105 my $config_file = "$tmpdir/sqlite.conf";
2106 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
2107 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
2108
2109 my $log_file = test_get_logfile();
2110
2111 my $user = 'proftpd';
2112 my $passwd = 'test';
2113 my $group = 'ftpd';
2114 my $home_dir = File::Spec->rel2abs($tmpdir);
2115 my $uid = 500;
2116 my $gid = 500;
2117
2118 # Make sure that, if we're running as root, that the home directory has
2119 # permissions/privs set for the account we create
2120 if ($< == 0) {
2121 unless (chmod(0755, $home_dir)) {
2122 die("Can't set perms on $home_dir to 0755: $!");
2123 }
2124
2125 unless (chown($uid, $gid, $home_dir)) {
2126 die("Can't set owner of $home_dir to $uid/$gid: $!");
2127 }
2128 }
2129
2130 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2131
2132 # Build up sqlite3 command to create users, groups tables and populate them
2133 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2134
2135 if (open(my $fh, "> $db_script")) {
2136 print $fh <<EOS;
2137 CREATE TABLE ftpusers (
2138 userid TEXT,
2139 passwd TEXT,
2140 uid INTEGER,
2141 gid INTEGER,
2142 homedir TEXT,
2143 shell TEXT
2144 );
2145 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2146
2147 CREATE TABLE ftpgroups (
2148 groupname TEXT,
2149 gid INTEGER,
2150 members TEXT
2151 );
2152 INSERT INTO ftpgroups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2153 EOS
2154
2155 unless (close($fh)) {
2156 die("Can't write $db_script: $!");
2157 }
2158
2159 } else {
2160 die("Can't open $db_script: $!");
2161 }
2162
2163 my $cmd = "sqlite3 $db_file < $db_script";
2164 build_db($cmd, $db_script);
2165
2166 # Make sure that, if we're running as root, the database file has
2167 # the permissions/privs set for use by proftpd
2168 if ($< == 0) {
2169 unless (chmod(0666, $db_file)) {
2170 die("Can't set perms on $db_file to 0666: $!");
2171 }
2172 }
2173
2174 my $config = {
2175 PidFile => $pid_file,
2176 ScoreboardFile => $scoreboard_file,
2177 SystemLog => $log_file,
2178
2179 IfModules => {
2180 'mod_delay.c' => {
2181 DelayEngine => 'off',
2182 },
2183
2184 'mod_sql.c' => [
2185 'SQLAuthTypes plaintext',
2186 'SQLBackend sqlite3',
2187 "SQLConnectInfo $db_file",
2188 "SQLLogFile $log_file",
2189 'SQLUserInfo ftpusers userid passwd uid gid homedir shell',
2190 'SQLGroupInfo ftpgroups groupname gid members',
2191 ],
2192 },
2193
2194 };
2195
2196 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2197
2198 # Open pipes, for use between the parent and child processes. Specifically,
2199 # the child will indicate when it's done with its test by writing a message
2200 # to the parent.
2201 my ($rfh, $wfh);
2202 unless (pipe($rfh, $wfh)) {
2203 die("Can't open pipe: $!");
2204 }
2205
2206 my $ex;
2207
2208 # Fork child
2209 $self->handle_sigchld();
2210 defined(my $pid = fork()) or die("Can't fork: $!");
2211 if ($pid) {
2212 eval {
2213 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2214 $client->login($user, $passwd);
2215
2216 my $resp_msgs = $client->response_msgs();
2217 my $nmsgs = scalar(@$resp_msgs);
2218
2219 my $expected;
2220
2221 $expected = 1;
2222 $self->assert($expected == $nmsgs,
2223 test_msg("Expected $expected, got $nmsgs"));
2224
2225 $expected = "User proftpd logged in";
2226 $self->assert($expected eq $resp_msgs->[0],
2227 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
2228
2229 };
2230
2231 if ($@) {
2232 $ex = $@;
2233 }
2234
2235 $wfh->print("done\n");
2236 $wfh->flush();
2237
2238 } else {
2239 eval { server_wait($config_file, $rfh) };
2240 if ($@) {
2241 warn($@);
2242 exit 1;
2243 }
2244
2245 exit 0;
2246 }
2247
2248 # Stop server
2249 server_stop($pid_file);
2250
2251 $self->assert_child_ok($pid);
2252
2253 if ($ex) {
2254 test_append_logfile($log_file, $ex);
2255 unlink($log_file);
2256
2257 die($ex);
2258 }
2259
2260 unlink($log_file);
2261 }
2262
2263 sub sql_custom_user_info {
2264 my $self = shift;
2265 my $tmpdir = $self->{tmpdir};
2266
2267 my $config_file = "$tmpdir/sqlite.conf";
2268 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
2269 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
2270
2271 my $log_file = test_get_logfile();
2272
2273 my $user = 'proftpd';
2274 my $passwd = 'test';
2275 my $group = 'ftpd';
2276 my $home_dir = File::Spec->rel2abs($tmpdir);
2277 my $uid = 500;
2278 my $gid = 500;
2279
2280 # Make sure that, if we're running as root, that the home directory has
2281 # permissions/privs set for the account we create
2282 if ($< == 0) {
2283 unless (chmod(0755, $home_dir)) {
2284 die("Can't set perms on $home_dir to 0755: $!");
2285 }
2286
2287 unless (chown($uid, $gid, $home_dir)) {
2288 die("Can't set owner of $home_dir to $uid/$gid: $!");
2289 }
2290 }
2291
2292 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2293
2294 # Build up sqlite3 command to create users, groups tables and populate them
2295 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2296
2297 if (open(my $fh, "> $db_script")) {
2298 print $fh <<EOS;
2299 CREATE TABLE ftpusers (
2300 userid TEXT,
2301 passwd TEXT,
2302 uid INTEGER,
2303 gid INTEGER,
2304 homedir TEXT,
2305 shell TEXT,
2306 lastdir TEXT
2307 );
2308 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2309
2310 CREATE TABLE groups (
2311 groupname TEXT,
2312 gid INTEGER,
2313 members TEXT
2314 );
2315 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2316 EOS
2317
2318 unless (close($fh)) {
2319 die("Can't write $db_script: $!");
2320 }
2321
2322 } else {
2323 die("Can't open $db_script: $!");
2324 }
2325
2326 my $cmd = "sqlite3 $db_file < $db_script";
2327 build_db($cmd, $db_script);
2328
2329 # Make sure that, if we're running as root, the database file has
2330 # the permissions/privs set for use by proftpd
2331 if ($< == 0) {
2332 unless (chmod(0666, $db_file)) {
2333 die("Can't set perms on $db_file to 0666: $!");
2334 }
2335 }
2336
2337 my $config = {
2338 PidFile => $pid_file,
2339 ScoreboardFile => $scoreboard_file,
2340 SystemLog => $log_file,
2341
2342 IfModules => {
2343 'mod_delay.c' => {
2344 DelayEngine => 'off',
2345 },
2346
2347 'mod_sql.c' => [
2348 'SQLAuthTypes plaintext',
2349 'SQLBackend sqlite3',
2350 "SQLConnectInfo $db_file",
2351 "SQLLogFile $log_file",
2352 'SQLNamedQuery get-user-by-name SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE userid = \'%U\'"',
2353 'SQLNamedQuery get-user-by-id SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE uid = %{0}"',
2354 'SQLNamedQuery get-user-names SELECT "userid FROM ftpusers"',
2355 'SQLNamedQuery get-all-users SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers"',
2356 'SQLUserInfo custom:/get-user-by-name/get-user-by-id/get-user-names/get-all-users',
2357 ],
2358 },
2359
2360
2361 };
2362
2363 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2364
2365 # Open pipes, for use between the parent and child processes. Specifically,
2366 # the child will indicate when it's done with its test by writing a message
2367 # to the parent.
2368 my ($rfh, $wfh);
2369 unless (pipe($rfh, $wfh)) {
2370 die("Can't open pipe: $!");
2371 }
2372
2373 my $ex;
2374
2375 # Fork child
2376 $self->handle_sigchld();
2377 defined(my $pid = fork()) or die("Can't fork: $!");
2378 if ($pid) {
2379 eval {
2380 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2381
2382 $client->login($user, $passwd);
2383
2384 my $resp_msgs = $client->response_msgs();
2385 my $nmsgs = scalar(@$resp_msgs);
2386
2387 my $expected;
2388
2389 $expected = 1;
2390 $self->assert($expected == $nmsgs,
2391 test_msg("Expected $expected, got $nmsgs"));
2392
2393 $expected = "User proftpd logged in";
2394 $self->assert($expected eq $resp_msgs->[0],
2395 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
2396
2397 };
2398
2399 if ($@) {
2400 $ex = $@;
2401 }
2402
2403 $wfh->print("done\n");
2404 $wfh->flush();
2405
2406 } else {
2407 eval { server_wait($config_file, $rfh) };
2408 if ($@) {
2409 warn($@);
2410 exit 1;
2411 }
2412
2413 exit 0;
2414 }
2415
2416 # Stop server
2417 server_stop($pid_file);
2418
2419 $self->assert_child_ok($pid);
2420
2421 if ($ex) {
2422 test_append_logfile($log_file, $ex);
2423 unlink($log_file);
2424
2425 die($ex);
2426 }
2427
2428 unlink($log_file);
2429 }
2430
2431 sub sql_custom_user_info_null_ids {
2432 my $self = shift;
2433 my $tmpdir = $self->{tmpdir};
2434
2435 my $config_file = "$tmpdir/sqlite.conf";
2436 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
2437 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
2438
2439 my $log_file = test_get_logfile();
2440
2441 my $user = 'proftpd';
2442 my $passwd = 'test';
2443 my $group = 'ftpd';
2444 my $home_dir = File::Spec->rel2abs($tmpdir);
2445 my $uid = 500;
2446 my $gid = 500;
2447
2448 # Make sure that, if we're running as root, that the home directory has
2449 # permissions/privs set for the account we create
2450 if ($< == 0) {
2451 unless (chmod(0755, $home_dir)) {
2452 die("Can't set perms on $home_dir to 0755: $!");
2453 }
2454
2455 unless (chown($uid, $gid, $home_dir)) {
2456 die("Can't set owner of $home_dir to $uid/$gid: $!");
2457 }
2458 }
2459
2460 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2461
2462 # Build up sqlite3 command to create users, groups tables and populate them
2463 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2464
2465 if (open(my $fh, "> $db_script")) {
2466 # Note: by inserting/using NULL for the uid and gid columns, we will
2467 # force mod_sql to use the SQLDefaultUID/SQLDefaultGID values (rather than
2468 # e.g. segfaulting).
2469
2470 print $fh <<EOS;
2471 CREATE TABLE ftpusers (
2472 userid TEXT,
2473 passwd TEXT,
2474 uid INTEGER,
2475 gid INTEGER,
2476 homedir TEXT,
2477 shell TEXT,
2478 lastdir TEXT
2479 );
2480 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', NULL, NULL, '$home_dir', '/bin/bash');
2481
2482 CREATE TABLE groups (
2483 groupname TEXT,
2484 gid INTEGER,
2485 members TEXT
2486 );
2487 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2488 EOS
2489
2490 unless (close($fh)) {
2491 die("Can't write $db_script: $!");
2492 }
2493
2494 } else {
2495 die("Can't open $db_script: $!");
2496 }
2497
2498 my $cmd = "sqlite3 $db_file < $db_script";
2499 build_db($cmd, $db_script);
2500
2501 # Make sure that, if we're running as root, the database file has
2502 # the permissions/privs set for use by proftpd
2503 if ($< == 0) {
2504 unless (chmod(0666, $db_file)) {
2505 die("Can't set perms on $db_file to 0666: $!");
2506 }
2507 }
2508
2509 my $config = {
2510 PidFile => $pid_file,
2511 ScoreboardFile => $scoreboard_file,
2512 SystemLog => $log_file,
2513
2514 IfModules => {
2515 'mod_delay.c' => {
2516 DelayEngine => 'off',
2517 },
2518
2519 'mod_sql.c' => [
2520 'SQLAuthTypes plaintext',
2521 'SQLBackend sqlite3',
2522 "SQLConnectInfo $db_file",
2523 "SQLLogFile $log_file",
2524 'SQLNamedQuery get-user-by-name SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE userid = \'%U\'"',
2525 'SQLNamedQuery get-user-by-id SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE uid = %{0}"',
2526 'SQLNamedQuery get-user-names SELECT "userid FROM ftpusers"',
2527 'SQLNamedQuery get-all-users SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers"',
2528 'SQLUserInfo custom:/get-user-by-name/get-user-by-id/get-user-names/get-all-users',
2529 ],
2530 },
2531
2532
2533 };
2534
2535 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2536
2537 # Open pipes, for use between the parent and child processes. Specifically,
2538 # the child will indicate when it's done with its test by writing a message
2539 # to the parent.
2540 my ($rfh, $wfh);
2541 unless (pipe($rfh, $wfh)) {
2542 die("Can't open pipe: $!");
2543 }
2544
2545 my $ex;
2546
2547 # Fork child
2548 $self->handle_sigchld();
2549 defined(my $pid = fork()) or die("Can't fork: $!");
2550 if ($pid) {
2551 eval {
2552 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2553 $client->login($user, $passwd);
2554
2555 my $resp_msgs = $client->response_msgs();
2556 my $nmsgs = scalar(@$resp_msgs);
2557
2558 my $expected;
2559
2560 $expected = 1;
2561 $self->assert($expected == $nmsgs,
2562 test_msg("Expected $expected, got $nmsgs"));
2563
2564 $expected = "User proftpd logged in";
2565 $self->assert($expected eq $resp_msgs->[0],
2566 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
2567
2568 };
2569
2570 if ($@) {
2571 $ex = $@;
2572 }
2573
2574 $wfh->print("done\n");
2575 $wfh->flush();
2576
2577 } else {
2578 eval { server_wait($config_file, $rfh) };
2579 if ($@) {
2580 warn($@);
2581 exit 1;
2582 }
2583
2584 exit 0;
2585 }
2586
2587 # Stop server
2588 server_stop($pid_file);
2589
2590 $self->assert_child_ok($pid);
2591
2592 if ($ex) {
2593 test_append_logfile($log_file, $ex);
2594 unlink($log_file);
2595
2596 die($ex);
2597 }
2598
2599 unlink($log_file);
2600 }
2601
2602 sub sql_userset_bug2434 {
2603 my $self = shift;
2604 my $tmpdir = $self->{tmpdir};
2605
2606 my $config_file = "$tmpdir/sqlite.conf";
2607 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
2608 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
2609
2610 my $log_file = test_get_logfile();
2611
2612 my $user = 'proftpd';
2613 my $passwd = 'test';
2614 my $group = 'ftpd';
2615 my $home_dir = File::Spec->rel2abs($tmpdir);
2616 my $uid = 500;
2617 my $gid = 500;
2618
2619 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2620
2621 # Build up sqlite3 command to create users, groups tables and populate them
2622 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2623
2624 if (open(my $fh, "> $db_script")) {
2625 print $fh <<EOS;
2626 CREATE TABLE ftpusers (
2627 userid TEXT,
2628 passwd TEXT,
2629 uid INTEGER,
2630 gid INTEGER,
2631 homedir TEXT,
2632 shell TEXT,
2633 lastdir TEXT
2634 );
2635 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('foo', '$passwd', 500, 500, '$home_dir', '/bin/bash');
2636 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('bar', '$passwd', 500, 500, '$home_dir', '/bin/bash');
2637 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2638
2639 CREATE TABLE groups (
2640 groupname TEXT,
2641 gid INTEGER,
2642 members TEXT
2643 );
2644 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2645 EOS
2646
2647 unless (close($fh)) {
2648 die("Can't write $db_script: $!");
2649 }
2650
2651 } else {
2652 die("Can't open $db_script: $!");
2653 }
2654
2655 my $cmd = "sqlite3 $db_file < $db_script";
2656 build_db($cmd, $db_script);
2657
2658 # Make sure that, if we're running as root, the database file has
2659 # the permissions/privs set for use by proftpd
2660 if ($< == 0) {
2661 unless (chmod(0666, $db_file)) {
2662 die("Can't set perms on $db_file to 0666: $!");
2663 }
2664 }
2665
2666 my $config = {
2667 PidFile => $pid_file,
2668 ScoreboardFile => $scoreboard_file,
2669 SystemLog => $log_file,
2670
2671 # This bug requires that a chroot occur in order to be tickled.
2672 DefaultRoot => '~',
2673
2674 IfModules => {
2675 'mod_delay.c' => {
2676 DelayEngine => 'off',
2677 },
2678
2679 'mod_sql.c' => [
2680 'SQLAuthenticate users userset groups',
2681 'SQLAuthTypes plaintext',
2682 'SQLBackend sqlite3',
2683 "SQLConnectInfo $db_file",
2684 "SQLLogFile $log_file",
2685 'SQLNamedQuery get-all-users SELECT "userid FROM ftpusers"',
2686 'SQLNamedQuery get-user-by-name SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE userid = \'%U\'"',
2687 'SQLNamedQuery get-user-by-id SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE uid = %{0}"',
2688 'SQLUserInfo custom:/get-user-by-name/get-user-by-id/get-all-users',
2689 ],
2690 },
2691 };
2692
2693 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2694
2695 # Open pipes, for use between the parent and child processes. Specifically,
2696 # the child will indicate when it's done with its test by writing a message
2697 # to the parent.
2698 my ($rfh, $wfh);
2699 unless (pipe($rfh, $wfh)) {
2700 die("Can't open pipe: $!");
2701 }
2702
2703 my $ex;
2704
2705 # Fork child
2706 $self->handle_sigchld();
2707 defined(my $pid = fork()) or die("Can't fork: $!");
2708 if ($pid) {
2709 eval {
2710 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2711
2712 $client->login($user, $passwd);
2713
2714 my $resp_msgs = $client->response_msgs();
2715 my $nmsgs = scalar(@$resp_msgs);
2716
2717 my $expected;
2718
2719 $expected = 1;
2720 $self->assert($expected == $nmsgs,
2721 test_msg("Expected $expected, got $nmsgs"));
2722
2723 $expected = "User proftpd logged in";
2724 $self->assert($expected eq $resp_msgs->[0],
2725 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
2726
2727 };
2728
2729 if ($@) {
2730 $ex = $@;
2731 }
2732
2733 $wfh->print("done\n");
2734 $wfh->flush();
2735
2736 } else {
2737 eval { server_wait($config_file, $rfh) };
2738 if ($@) {
2739 warn($@);
2740 exit 1;
2741 }
2742
2743 exit 0;
2744 }
2745
2746 # Stop server
2747 server_stop($pid_file);
2748
2749 $self->assert_child_ok($pid);
2750
2751 if ($ex) {
2752 test_append_logfile($log_file, $ex);
2753 unlink($log_file);
2754
2755 die($ex);
2756 }
2757
2758 unlink($log_file);
2759 }
2760
2761 sub sql_usersetfast_bug2434 {
2762 my $self = shift;
2763 my $tmpdir = $self->{tmpdir};
2764
2765 my $config_file = "$tmpdir/sqlite.conf";
2766 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
2767 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
2768
2769 my $log_file = test_get_logfile();
2770
2771 my $user = 'proftpd';
2772 my $passwd = 'test';
2773 my $group = 'ftpd';
2774 my $home_dir = File::Spec->rel2abs($tmpdir);
2775 my $uid = 500;
2776 my $gid = 500;
2777
2778 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2779
2780 # Build up sqlite3 command to create users, groups tables and populate them
2781 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2782
2783 if (open(my $fh, "> $db_script")) {
2784 print $fh <<EOS;
2785 CREATE TABLE ftpusers (
2786 userid TEXT,
2787 passwd TEXT,
2788 uid INTEGER,
2789 gid INTEGER,
2790 homedir TEXT,
2791 shell TEXT,
2792 lastdir TEXT
2793 );
2794 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('foo', '$passwd', 500, 500, '$home_dir', '/bin/bash');
2795 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('bar', '$passwd', 500, 500, '$home_dir', '/bin/bash');
2796 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2797
2798 CREATE TABLE groups (
2799 groupname TEXT,
2800 gid INTEGER,
2801 members TEXT
2802 );
2803 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2804 EOS
2805
2806 unless (close($fh)) {
2807 die("Can't write $db_script: $!");
2808 }
2809
2810 } else {
2811 die("Can't open $db_script: $!");
2812 }
2813
2814 my $cmd = "sqlite3 $db_file < $db_script";
2815 build_db($cmd, $db_script);
2816
2817 # Make sure that, if we're running as root, the database file has
2818 # the permissions/privs set for use by proftpd
2819 if ($< == 0) {
2820 unless (chmod(0666, $db_file)) {
2821 die("Can't set perms on $db_file to 0666: $!");
2822 }
2823 }
2824
2825 my $config = {
2826 PidFile => $pid_file,
2827 ScoreboardFile => $scoreboard_file,
2828 SystemLog => $log_file,
2829
2830 # This bug requires that a chroot occur in order to be tickled.
2831 DefaultRoot => '~',
2832
2833 IfModules => {
2834 'mod_delay.c' => {
2835 DelayEngine => 'off',
2836 },
2837
2838 'mod_sql.c' => [
2839 'SQLAuthenticate users usersetfast groups',
2840 'SQLAuthTypes plaintext',
2841 'SQLBackend sqlite3',
2842 "SQLConnectInfo $db_file",
2843 "SQLLogFile $log_file",
2844 'SQLNamedQuery get-all-usernames SELECT "userid FROM ftpusers"',
2845 'SQLNamedQuery get-all-users SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers"',
2846 'SQLNamedQuery get-user-by-name SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE userid = \'%U\'"',
2847 'SQLNamedQuery get-user-by-id SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE uid = %{0}"',
2848 'SQLUserInfo custom:/get-user-by-name/get-user-by-id/get-all-usernames/get-all-users',
2849 ],
2850 },
2851 };
2852
2853 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2854
2855 # Open pipes, for use between the parent and child processes. Specifically,
2856 # the child will indicate when it's done with its test by writing a message
2857 # to the parent.
2858 my ($rfh, $wfh);
2859 unless (pipe($rfh, $wfh)) {
2860 die("Can't open pipe: $!");
2861 }
2862
2863 my $ex;
2864
2865 # Fork child
2866 $self->handle_sigchld();
2867 defined(my $pid = fork()) or die("Can't fork: $!");
2868 if ($pid) {
2869 eval {
2870 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2871
2872 $client->login($user, $passwd);
2873
2874 my $resp_msgs = $client->response_msgs();
2875 my $nmsgs = scalar(@$resp_msgs);
2876
2877 my $expected;
2878
2879 $expected = 1;
2880 $self->assert($expected == $nmsgs,
2881 test_msg("Expected $expected, got $nmsgs"));
2882
2883 $expected = "User proftpd logged in";
2884 $self->assert($expected eq $resp_msgs->[0],
2885 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
2886
2887 };
2888
2889 if ($@) {
2890 $ex = $@;
2891 }
2892
2893 $wfh->print("done\n");
2894 $wfh->flush();
2895
2896 } else {
2897 eval { server_wait($config_file, $rfh) };
2898 if ($@) {
2899 warn($@);
2900 exit 1;
2901 }
2902
2903 exit 0;
2904 }
2905
2906 # Stop server
2907 server_stop($pid_file);
2908
2909 $self->assert_child_ok($pid);
2910
2911 if ($ex) {
2912 test_append_logfile($log_file, $ex);
2913 unlink($log_file);
2914
2915 die($ex);
2916 }
2917
2918 unlink($log_file);
2919 }
2920
2921 sub sql_custom_group_info_bug3043 {
2922 my $self = shift;
2923 my $tmpdir = $self->{tmpdir};
2924
2925 my $config_file = "$tmpdir/sqlite.conf";
2926 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
2927 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
2928
2929 my $log_file = test_get_logfile();
2930
2931 my $user = 'proftpd';
2932 my $passwd = 'test';
2933 my $group = 'ftpd';
2934 my $home_dir = File::Spec->rel2abs($tmpdir);
2935 my $uid = 500;
2936 my $gid = 500;
2937
2938 # Make sure that, if we're running as root, that the home directory has
2939 # permissions/privs set for the account we create
2940 if ($< == 0) {
2941 unless (chmod(0755, $home_dir)) {
2942 die("Can't set perms on $home_dir to 0755: $!");
2943 }
2944
2945 unless (chown($uid, $gid, $home_dir)) {
2946 die("Can't set owner of $home_dir to $uid/$gid: $!");
2947 }
2948 }
2949
2950 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2951
2952 # Build up sqlite3 command to create users, groups tables and populate them
2953 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
2954
2955 if (open(my $fh, "> $db_script")) {
2956 print $fh <<EOS;
2957 CREATE TABLE users (
2958 userid TEXT,
2959 passwd TEXT,
2960 uid INTEGER,
2961 gid INTEGER,
2962 homedir TEXT,
2963 shell TEXT
2964 );
2965 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
2966
2967 CREATE TABLE ftpgroups (
2968 groupname TEXT,
2969 gid INTEGER,
2970 members TEXT
2971 );
2972 INSERT INTO ftpgroups (groupname, gid, members) VALUES ('$group', $gid, '$user');
2973 EOS
2974
2975 unless (close($fh)) {
2976 die("Can't write $db_script: $!");
2977 }
2978
2979 } else {
2980 die("Can't open $db_script: $!");
2981 }
2982
2983 my $cmd = "sqlite3 $db_file < $db_script";
2984 build_db($cmd, $db_script);
2985
2986 # Make sure that, if we're running as root, the database file has
2987 # the permissions/privs set for use by proftpd
2988 if ($< == 0) {
2989 unless (chmod(0666, $db_file)) {
2990 die("Can't set perms on $db_file to 0666: $!");
2991 }
2992 }
2993
2994 my $config = {
2995 PidFile => $pid_file,
2996 ScoreboardFile => $scoreboard_file,
2997 SystemLog => $log_file,
2998
2999 IfModules => {
3000 'mod_delay.c' => {
3001 DelayEngine => 'off',
3002 },
3003
3004 'mod_sql.c' => [
3005 'SQLMinID 0',
3006 'SQLAuthenticate users groups',
3007 'SQLAuthTypes plaintext',
3008 'SQLBackend sqlite3',
3009 "SQLConnectInfo $db_file",
3010 "SQLLogFile $log_file",
3011 'SQLNamedQuery get-group-by-name SELECT "groupname, gid, members FROM ftpgroups WHERE groupname = \'%{0}\'"',
3012 'SQLNamedQuery get-group-by-id SELECT "groupname, gid, members FROM ftpgroups WHERE gid = %{0}"',
3013 'SQLNamedQuery get-members-by-user SELECT "members FROM ftpgroups WHERE (members LIKE \'%%,%{0},%%\' OR members LIKE \'%{0},%%\' OR members LIKE \'%%,%{0}\')"',
3014 'SQLGroupInfo custom:/get-group-by-name/get-group-by-id/get-members-by-user',
3015 ],
3016 },
3017 };
3018
3019 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3020
3021 # Open pipes, for use between the parent and child processes. Specifically,
3022 # the child will indicate when it's done with its test by writing a message
3023 # to the parent.
3024 my ($rfh, $wfh);
3025 unless (pipe($rfh, $wfh)) {
3026 die("Can't open pipe: $!");
3027 }
3028
3029 my $ex;
3030
3031 # Fork child
3032 $self->handle_sigchld();
3033 defined(my $pid = fork()) or die("Can't fork: $!");
3034 if ($pid) {
3035 eval {
3036 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3037
3038 $client->login($user, $passwd);
3039
3040 my $resp_msgs = $client->response_msgs();
3041 my $nmsgs = scalar(@$resp_msgs);
3042
3043 my $expected;
3044
3045 $expected = 1;
3046 $self->assert($expected == $nmsgs,
3047 test_msg("Expected $expected, got $nmsgs"));
3048
3049 $expected = "User proftpd logged in";
3050 $self->assert($expected eq $resp_msgs->[0],
3051 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
3052
3053 };
3054
3055 if ($@) {
3056 $ex = $@;
3057 }
3058
3059 $wfh->print("done\n");
3060 $wfh->flush();
3061
3062 } else {
3063 eval { server_wait($config_file, $rfh) };
3064 if ($@) {
3065 warn($@);
3066 exit 1;
3067 }
3068
3069 exit 0;
3070 }
3071
3072 # Stop server
3073 server_stop($pid_file);
3074
3075 $self->assert_child_ok($pid);
3076
3077 if ($ex) {
3078 test_append_logfile($log_file, $ex);
3079 unlink($log_file);
3080
3081 die($ex);
3082 }
3083
3084 unlink($log_file);
3085 }
3086
3087 sub sql_groupset_bug3043 {
3088 my $self = shift;
3089 my $tmpdir = $self->{tmpdir};
3090
3091 my $config_file = "$tmpdir/sqlite.conf";
3092 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
3093 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
3094
3095 my $log_file = test_get_logfile();
3096
3097 my $user = 'proftpd';
3098 my $passwd = 'test';
3099 my $group = 'ftpd';
3100 my $home_dir = File::Spec->rel2abs($tmpdir);
3101 my $uid = 500;
3102 my $gid = 500;
3103
3104 # Make sure that, if we're running as root, that the home directory has
3105 # permissions/privs set for the account we create
3106 if ($< == 0) {
3107 unless (chmod(0755, $home_dir)) {
3108 die("Can't set perms on $home_dir to 0755: $!");
3109 }
3110
3111 unless (chown($uid, $gid, $home_dir)) {
3112 die("Can't set owner of $home_dir to $uid/$gid: $!");
3113 }
3114 }
3115
3116 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3117
3118 # Build up sqlite3 command to create users, groups tables and populate them
3119 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3120
3121 if (open(my $fh, "> $db_script")) {
3122 print $fh <<EOS;
3123 CREATE TABLE users (
3124 userid TEXT,
3125 passwd TEXT,
3126 uid INTEGER,
3127 gid INTEGER,
3128 homedir TEXT,
3129 shell TEXT
3130 );
3131 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
3132
3133 CREATE TABLE ftpgroups (
3134 groupname TEXT,
3135 gid INTEGER,
3136 members TEXT
3137 );
3138 INSERT INTO ftpgroups (groupname, gid, members) VALUES ('$group', $gid, '$user');
3139 EOS
3140
3141 unless (close($fh)) {
3142 die("Can't write $db_script: $!");
3143 }
3144
3145 } else {
3146 die("Can't open $db_script: $!");
3147 }
3148
3149 my $cmd = "sqlite3 $db_file < $db_script";
3150 build_db($cmd, $db_script);
3151
3152 # Make sure that, if we're running as root, the database file has
3153 # the permissions/privs set for use by proftpd
3154 if ($< == 0) {
3155 unless (chmod(0666, $db_file)) {
3156 die("Can't set perms on $db_file to 0666: $!");
3157 }
3158 }
3159
3160 my $config = {
3161 PidFile => $pid_file,
3162 ScoreboardFile => $scoreboard_file,
3163 SystemLog => $log_file,
3164
3165 IfModules => {
3166 'mod_delay.c' => {
3167 DelayEngine => 'off',
3168 },
3169
3170 'mod_sql.c' => [
3171 'SQLMinID 0',
3172 'SQLAuthenticate users groups groupset',
3173 'SQLAuthTypes plaintext',
3174 'SQLBackend sqlite3',
3175 "SQLConnectInfo $db_file",
3176 "SQLLogFile $log_file",
3177 'SQLNamedQuery get-group-by-name SELECT "groupname, gid, members FROM ftpgroups WHERE groupname = \'%{0}\'"',
3178 'SQLNamedQuery get-group-by-id SELECT "groupname, gid, members FROM ftpgroups WHERE gid = %{0}"',
3179 'SQLNamedQuery get-members-by-user SELECT "members FROM ftpgroups WHERE (members LIKE \'%%,%{0},%%\' OR members LIKE \'%{0},%%\' OR members LIKE \'%%,%{0}\')"',
3180 'SQLNamedQuery get-all-groupnames SELECT "groupname FROM ftpgroups"',
3181 'SQLGroupInfo custom:/get-group-by-name/get-group-by-id/get-members-by-user/get-all-groupnames',
3182 ],
3183 },
3184 };
3185
3186 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3187
3188 # Open pipes, for use between the parent and child processes. Specifically,
3189 # the child will indicate when it's done with its test by writing a message
3190 # to the parent.
3191 my ($rfh, $wfh);
3192 unless (pipe($rfh, $wfh)) {
3193 die("Can't open pipe: $!");
3194 }
3195
3196 my $ex;
3197
3198 # Fork child
3199 $self->handle_sigchld();
3200 defined(my $pid = fork()) or die("Can't fork: $!");
3201 if ($pid) {
3202 eval {
3203 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3204
3205 $client->login($user, $passwd);
3206
3207 my $resp_msgs = $client->response_msgs();
3208 my $nmsgs = scalar(@$resp_msgs);
3209
3210 my $expected;
3211
3212 $expected = 1;
3213 $self->assert($expected == $nmsgs,
3214 test_msg("Expected $expected, got $nmsgs"));
3215
3216 $expected = "User proftpd logged in";
3217 $self->assert($expected eq $resp_msgs->[0],
3218 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
3219
3220 };
3221
3222 if ($@) {
3223 $ex = $@;
3224 }
3225
3226 $wfh->print("done\n");
3227 $wfh->flush();
3228
3229 } else {
3230 eval { server_wait($config_file, $rfh) };
3231 if ($@) {
3232 warn($@);
3233 exit 1;
3234 }
3235
3236 exit 0;
3237 }
3238
3239 # Stop server
3240 server_stop($pid_file);
3241
3242 $self->assert_child_ok($pid);
3243
3244 if ($ex) {
3245 test_append_logfile($log_file, $ex);
3246 unlink($log_file);
3247
3248 die($ex);
3249 }
3250
3251 unlink($log_file);
3252 }
3253
3254 sub sql_groupsetfast_bug3043 {
3255 my $self = shift;
3256 my $tmpdir = $self->{tmpdir};
3257
3258 my $config_file = "$tmpdir/sqlite.conf";
3259 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
3260 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
3261
3262 my $log_file = test_get_logfile();
3263
3264 my $user = 'proftpd';
3265 my $passwd = 'test';
3266 my $group = 'ftpd';
3267 my $home_dir = File::Spec->rel2abs($tmpdir);
3268 my $uid = 500;
3269 my $gid = 500;
3270
3271 # Make sure that, if we're running as root, that the home directory has
3272 # permissions/privs set for the account we create
3273 if ($< == 0) {
3274 unless (chmod(0755, $home_dir)) {
3275 die("Can't set perms on $home_dir to 0755: $!");
3276 }
3277
3278 unless (chown($uid, $gid, $home_dir)) {
3279 die("Can't set owner of $home_dir to $uid/$gid: $!");
3280 }
3281 }
3282
3283 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3284
3285 # Build up sqlite3 command to create users, groups tables and populate them
3286 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3287
3288 if (open(my $fh, "> $db_script")) {
3289 print $fh <<EOS;
3290 CREATE TABLE users (
3291 userid TEXT,
3292 passwd TEXT,
3293 uid INTEGER,
3294 gid INTEGER,
3295 homedir TEXT,
3296 shell TEXT
3297 );
3298 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
3299
3300 CREATE TABLE ftpgroups (
3301 groupname TEXT,
3302 gid INTEGER,
3303 members TEXT
3304 );
3305 INSERT INTO ftpgroups (groupname, gid, members) VALUES ('$group', $gid, '$user');
3306 EOS
3307
3308 unless (close($fh)) {
3309 die("Can't write $db_script: $!");
3310 }
3311
3312 } else {
3313 die("Can't open $db_script: $!");
3314 }
3315
3316 my $cmd = "sqlite3 $db_file < $db_script";
3317 build_db($cmd, $db_script);
3318
3319 # Make sure that, if we're running as root, the database file has
3320 # the permissions/privs set for use by proftpd
3321 if ($< == 0) {
3322 unless (chmod(0666, $db_file)) {
3323 die("Can't set perms on $db_file to 0666: $!");
3324 }
3325 }
3326
3327 my $config = {
3328 PidFile => $pid_file,
3329 ScoreboardFile => $scoreboard_file,
3330 SystemLog => $log_file,
3331
3332 IfModules => {
3333 'mod_delay.c' => {
3334 DelayEngine => 'off',
3335 },
3336
3337 'mod_sql.c' => [
3338 'SQLMinID 0',
3339 'SQLAuthenticate users groups groupsetfast',
3340 'SQLAuthTypes plaintext',
3341 'SQLBackend sqlite3',
3342 "SQLConnectInfo $db_file",
3343 "SQLLogFile $log_file",
3344 'SQLNamedQuery get-group-by-name SELECT "groupname, gid, members FROM ftpgroups WHERE groupname = \'%{0}\'"',
3345 'SQLNamedQuery get-group-by-id SELECT "groupname, gid, members FROM ftpgroups WHERE gid = %{0}"',
3346 'SQLNamedQuery get-group-by-member SELECT "groupname, gid, members FROM ftpgroups WHERE (members LIKE \'%%,%{0},%%\' OR members LIKE \'%{0},%%\' OR members LIKE \'%%,%{0}\')"',
3347 'SQLNamedQuery get-all-groupnames SELECT "groupname FROM ftpgroups"',
3348 'SQLNamedQuery get-all-groups SELECT "groupname, gid, members FROM ftpgroups"',
3349 'SQLGroupInfo custom:/get-group-by-name/get-group-by-id/get-group-by-member/get-all-groupnames/get-all-groups',
3350 ],
3351 },
3352 };
3353
3354 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3355
3356 # Open pipes, for use between the parent and child processes. Specifically,
3357 # the child will indicate when it's done with its test by writing a message
3358 # to the parent.
3359 my ($rfh, $wfh);
3360 unless (pipe($rfh, $wfh)) {
3361 die("Can't open pipe: $!");
3362 }
3363
3364 my $ex;
3365
3366 # Fork child
3367 $self->handle_sigchld();
3368 defined(my $pid = fork()) or die("Can't fork: $!");
3369 if ($pid) {
3370 eval {
3371 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3372
3373 $client->login($user, $passwd);
3374
3375 my $resp_msgs = $client->response_msgs();
3376 my $nmsgs = scalar(@$resp_msgs);
3377
3378 my $expected;
3379
3380 $expected = 1;
3381 $self->assert($expected == $nmsgs,
3382 test_msg("Expected $expected, got $nmsgs"));
3383
3384 $expected = "User proftpd logged in";
3385 $self->assert($expected eq $resp_msgs->[0],
3386 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
3387
3388 };
3389
3390 if ($@) {
3391 $ex = $@;
3392 }
3393
3394 $wfh->print("done\n");
3395 $wfh->flush();
3396
3397 } else {
3398 eval { server_wait($config_file, $rfh) };
3399 if ($@) {
3400 warn($@);
3401 exit 1;
3402 }
3403
3404 exit 0;
3405 }
3406
3407 # Stop server
3408 server_stop($pid_file);
3409
3410 $self->assert_child_ok($pid);
3411
3412 if ($ex) {
3413 test_append_logfile($log_file, $ex);
3414 unlink($log_file);
3415
3416 die($ex);
3417 }
3418
3419 unlink($log_file);
3420 }
3421
3422 sub get_renames {
3423 my $db_file = shift;
3424 my $where = shift;
3425
3426 my $sql = "SELECT user, ip_addr, rename_from FROM ftpsessions";
3427 if ($where) {
3428 $sql .= " WHERE $where";
3429 }
3430
3431 my $cmd = "sqlite3 $db_file \"$sql\"";
3432
3433 if ($ENV{TEST_VERBOSE}) {
3434 print STDERR "Executing sqlite3: $cmd\n";
3435 }
3436
3437 my $res = join('', `$cmd`);
3438 chomp($res);
3439
3440 # The default sqlite3 delimiter is '|'
3441 return split(/\|/, $res);
3442 }
3443
3444 sub sql_sqllog_var_w {
3445 my $self = shift;
3446 my $tmpdir = $self->{tmpdir};
3447
3448 my $config_file = "$tmpdir/sqlite.conf";
3449 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
3450 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
3451
3452 my $log_file = test_get_logfile();
3453
3454 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
3455 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
3456
3457 my $user = 'proftpd';
3458 my $passwd = 'test';
3459 my $group = 'ftpd';
3460 my $home_dir = File::Spec->rel2abs($tmpdir);
3461 my $uid = 500;
3462 my $gid = 500;
3463
3464 # Make sure that, if we're running as root, that the home directory has
3465 # permissions/privs set for the account we create
3466 if ($< == 0) {
3467 unless (chmod(0755, $home_dir)) {
3468 die("Can't set perms on $home_dir to 0755: $!");
3469 }
3470
3471 unless (chown($uid, $gid, $home_dir)) {
3472 die("Can't set owner of $home_dir to $uid/$gid: $!");
3473 }
3474 }
3475
3476 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3477 '/bin/bash');
3478 auth_group_write($auth_group_file, $group, $gid, $user);
3479
3480 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3481
3482 # Build up sqlite3 command to create users, groups tables and populate them
3483 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3484
3485 if (open(my $fh, "> $db_script")) {
3486 print $fh <<EOS;
3487 CREATE TABLE ftpsessions (
3488 user TEXT,
3489 ip_addr TEXT,
3490 rename_from TEXT
3491 );
3492 EOS
3493
3494 unless (close($fh)) {
3495 die("Can't write $db_script: $!");
3496 }
3497
3498 } else {
3499 die("Can't open $db_script: $!");
3500 }
3501
3502 my $cmd = "sqlite3 $db_file < $db_script";
3503 build_db($cmd, $db_script);
3504
3505 # Make sure that, if we're running as root, the database file has
3506 # the permissions/privs set for use by proftpd
3507 if ($< == 0) {
3508 unless (chmod(0666, $db_file)) {
3509 die("Can't set perms on $db_file to 0666: $!");
3510 }
3511 }
3512
3513 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
3514 if (open(my $fh, "> $src_file")) {
3515 close($fh);
3516
3517 } else {
3518 die("Can't open $src_file: $!");
3519 }
3520
3521 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
3522
3523 my $config = {
3524 PidFile => $pid_file,
3525 ScoreboardFile => $scoreboard_file,
3526 SystemLog => $log_file,
3527
3528 AuthUserFile => $auth_user_file,
3529 AuthGroupFile => $auth_group_file,
3530 AuthOrder => 'mod_auth_file.c',
3531
3532 IfModules => {
3533 'mod_delay.c' => {
3534 DelayEngine => 'off',
3535 },
3536
3537 'mod_sql.c' => {
3538 SQLEngine => 'log',
3539 SQLBackend => 'sqlite3',
3540 SQLConnectInfo => $db_file,
3541 SQLLogFile => $log_file,
3542 SQLNamedQuery => 'rename FREEFORM "INSERT INTO ftpsessions (user, ip_addr, rename_from) VALUES (\'%u\', \'%L\', \'%w\')"',
3543 SQLLog => 'RNTO rename',
3544 },
3545 },
3546 };
3547
3548 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3549
3550 # Open pipes, for use between the parent and child processes. Specifically,
3551 # the child will indicate when it's done with its test by writing a message
3552 # to the parent.
3553 my ($rfh, $wfh);
3554 unless (pipe($rfh, $wfh)) {
3555 die("Can't open pipe: $!");
3556 }
3557
3558 my $ex;
3559
3560 # Fork child
3561 $self->handle_sigchld();
3562 defined(my $pid = fork()) or die("Can't fork: $!");
3563 if ($pid) {
3564 eval {
3565 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3566 $client->login($user, $passwd);
3567 $client->rnfr('test.txt');
3568 $client->rnto('foo.txt');
3569 };
3570
3571 if ($@) {
3572 $ex = $@;
3573 }
3574
3575 $wfh->print("done\n");
3576 $wfh->flush();
3577
3578 } else {
3579 eval { server_wait($config_file, $rfh) };
3580 if ($@) {
3581 warn($@);
3582 exit 1;
3583 }
3584
3585 exit 0;
3586 }
3587
3588 # Stop server
3589 server_stop($pid_file);
3590
3591 $self->assert_child_ok($pid);
3592
3593 if ($ex) {
3594 test_append_logfile($log_file, $ex);
3595 unlink($log_file);
3596
3597 die($ex);
3598 }
3599
3600 my ($login, $ip_addr, $rnfr_path) = get_renames($db_file, "user = \'$user\'");
3601
3602 my $expected;
3603
3604 $expected = $user;
3605 $self->assert($expected eq $login,
3606 test_msg("Expected '$expected', got '$login'"));
3607
3608 $expected = '127.0.0.1';
3609 $self->assert($expected eq $ip_addr,
3610 test_msg("Expected '$expected', got '$ip_addr'"));
3611
3612 if ($^O eq 'darwin') {
3613 # MacOSX-specific hack
3614 $src_file = '/private' . $src_file;
3615 }
3616
3617 $expected = $src_file;
3618 $self->assert($expected eq $rnfr_path,
3619 test_msg("Expected '$expected', got '$rnfr_path'"));
3620
3621 unlink($log_file);
3622 }
3623
3624 sub sql_sqllog_var_w_chrooted {
3625 my $self = shift;
3626 my $tmpdir = $self->{tmpdir};
3627
3628 my $config_file = "$tmpdir/sqlite.conf";
3629 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
3630 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
3631
3632 my $log_file = test_get_logfile();
3633
3634 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
3635 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
3636
3637 my $user = 'proftpd';
3638 my $passwd = 'test';
3639 my $group = 'ftpd';
3640 my $home_dir = File::Spec->rel2abs($tmpdir);
3641 my $uid = 500;
3642 my $gid = 500;
3643
3644 # Make sure that, if we're running as root, that the home directory has
3645 # permissions/privs set for the account we create
3646 if ($< == 0) {
3647 unless (chmod(0755, $home_dir)) {
3648 die("Can't set perms on $home_dir to 0755: $!");
3649 }
3650
3651 unless (chown($uid, $gid, $home_dir)) {
3652 die("Can't set owner of $home_dir to $uid/$gid: $!");
3653 }
3654 }
3655
3656 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3657 '/bin/bash');
3658 auth_group_write($auth_group_file, $group, $gid, $user);
3659
3660 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3661
3662 # Build up sqlite3 command to create users, groups tables and populate them
3663 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3664
3665 if (open(my $fh, "> $db_script")) {
3666 print $fh <<EOS;
3667 CREATE TABLE ftpsessions (
3668 user TEXT,
3669 ip_addr TEXT,
3670 rename_from TEXT
3671 );
3672 EOS
3673
3674 unless (close($fh)) {
3675 die("Can't write $db_script: $!");
3676 }
3677
3678 } else {
3679 die("Can't open $db_script: $!");
3680 }
3681
3682 my $cmd = "sqlite3 $db_file < $db_script";
3683 build_db($cmd, $db_script);
3684
3685 # Make sure that, if we're running as root, the database file has
3686 # the permissions/privs set for use by proftpd
3687 if ($< == 0) {
3688 unless (chmod(0666, $db_file)) {
3689 die("Can't set perms on $db_file to 0666: $!");
3690 }
3691 }
3692
3693 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
3694 if (open(my $fh, "> $src_file")) {
3695 close($fh);
3696
3697 } else {
3698 die("Can't open $src_file: $!");
3699 }
3700
3701 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
3702
3703 my $config = {
3704 PidFile => $pid_file,
3705 ScoreboardFile => $scoreboard_file,
3706 SystemLog => $log_file,
3707
3708 AuthUserFile => $auth_user_file,
3709 AuthGroupFile => $auth_group_file,
3710 AuthOrder => 'mod_auth_file.c',
3711
3712 DefaultRoot => '~',
3713
3714 IfModules => {
3715 'mod_delay.c' => {
3716 DelayEngine => 'off',
3717 },
3718
3719 'mod_sql.c' => {
3720 SQLEngine => 'log',
3721 SQLBackend => 'sqlite3',
3722 SQLConnectInfo => "$db_file foo bar PERCONNECTION",
3723 SQLLogFile => $log_file,
3724 SQLNamedQuery => 'rename FREEFORM "INSERT INTO ftpsessions (user, ip_addr, rename_from) VALUES (\'%u\', \'%L\', \'%w\')"',
3725 SQLLog => 'RNTO rename',
3726 },
3727 },
3728 };
3729
3730 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3731
3732 # Open pipes, for use between the parent and child processes. Specifically,
3733 # the child will indicate when it's done with its test by writing a message
3734 # to the parent.
3735 my ($rfh, $wfh);
3736 unless (pipe($rfh, $wfh)) {
3737 die("Can't open pipe: $!");
3738 }
3739
3740 my $ex;
3741
3742 # Fork child
3743 $self->handle_sigchld();
3744 defined(my $pid = fork()) or die("Can't fork: $!");
3745 if ($pid) {
3746 eval {
3747 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3748
3749 $client->login($user, $passwd);
3750 $client->rnfr('test.txt');
3751 $client->rnto('foo.txt');
3752 };
3753
3754 if ($@) {
3755 $ex = $@;
3756 }
3757
3758 $wfh->print("done\n");
3759 $wfh->flush();
3760
3761 } else {
3762 eval { server_wait($config_file, $rfh) };
3763 if ($@) {
3764 warn($@);
3765 exit 1;
3766 }
3767
3768 exit 0;
3769 }
3770
3771 # Stop server
3772 server_stop($pid_file);
3773
3774 $self->assert_child_ok($pid);
3775
3776 if ($ex) {
3777 test_append_logfile($log_file, $ex);
3778 unlink($log_file);
3779
3780 die($ex);
3781 }
3782
3783 my ($login, $ip_addr, $rnfr_path) = get_renames($db_file, "user = \'$user\'");
3784
3785 my $expected;
3786
3787 $expected = $user;
3788 $self->assert($expected eq $login,
3789 test_msg("Expected '$expected', got '$login'"));
3790
3791 $expected = '127.0.0.1';
3792 $self->assert($expected eq $ip_addr,
3793 test_msg("Expected '$expected', got '$ip_addr'"));
3794
3795 $expected = $src_file;
3796 $self->assert($expected eq $rnfr_path,
3797 test_msg("Expected '$expected', got '$rnfr_path'"));
3798
3799 unlink($log_file);
3800 }
3801
3802 sub sql_openssl_auth_type_md5 {
3803 my $self = shift;
3804 my $tmpdir = $self->{tmpdir};
3805
3806 my $config_file = "$tmpdir/sqlite.conf";
3807 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
3808 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
3809
3810 my $log_file = test_get_logfile();
3811
3812 my $user = 'proftpd';
3813 my $passwd = '{md5}X03MO1qnZdYdgyfeuILPmQ==';
3814 my $group = 'ftpd';
3815 my $home_dir = File::Spec->rel2abs($tmpdir);
3816 my $uid = 500;
3817 my $gid = 500;
3818
3819 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3820
3821 # Build up sqlite3 command to create users, groups tables and populate them
3822 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3823
3824 if (open(my $fh, "> $db_script")) {
3825 print $fh <<EOS;
3826 CREATE TABLE users (
3827 userid TEXT,
3828 passwd TEXT,
3829 uid INTEGER,
3830 gid INTEGER,
3831 homedir TEXT,
3832 shell TEXT,
3833 lastdir TEXT
3834 );
3835 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
3836
3837 CREATE TABLE groups (
3838 groupname TEXT,
3839 gid INTEGER,
3840 members TEXT
3841 );
3842 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
3843 EOS
3844
3845 unless (close($fh)) {
3846 die("Can't write $db_script: $!");
3847 }
3848
3849 } else {
3850 die("Can't open $db_script: $!");
3851 }
3852
3853 my $cmd = "sqlite3 $db_file < $db_script";
3854 build_db($cmd, $db_script);
3855
3856 # Make sure that, if we're running as root, the database file has
3857 # the permissions/privs set for use by proftpd
3858 if ($< == 0) {
3859 unless (chmod(0666, $db_file)) {
3860 die("Can't set perms on $db_file to 0666: $!");
3861 }
3862 }
3863
3864 my $config = {
3865 PidFile => $pid_file,
3866 ScoreboardFile => $scoreboard_file,
3867 SystemLog => $log_file,
3868
3869 IfModules => {
3870 'mod_delay.c' => {
3871 DelayEngine => 'off',
3872 },
3873
3874 'mod_sql.c' => {
3875 SQLAuthTypes => 'OpenSSL',
3876 SQLBackend => 'sqlite3',
3877 SQLConnectInfo => $db_file,
3878 SQLLogFile => $log_file,
3879 },
3880 },
3881 };
3882
3883 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3884
3885 # Open pipes, for use between the parent and child processes. Specifically,
3886 # the child will indicate when it's done with its test by writing a message
3887 # to the parent.
3888 my ($rfh, $wfh);
3889 unless (pipe($rfh, $wfh)) {
3890 die("Can't open pipe: $!");
3891 }
3892
3893 my $ex;
3894
3895 # Fork child
3896 $self->handle_sigchld();
3897 defined(my $pid = fork()) or die("Can't fork: $!");
3898 if ($pid) {
3899 eval {
3900 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
3901
3902 $client->login($user, 'password');
3903
3904 my $resp_msgs = $client->response_msgs();
3905 my $nmsgs = scalar(@$resp_msgs);
3906
3907 my $expected;
3908
3909 $expected = 1;
3910 $self->assert($expected == $nmsgs,
3911 test_msg("Expected $expected, got $nmsgs"));
3912
3913 $expected = "User proftpd logged in";
3914 $self->assert($expected eq $resp_msgs->[0],
3915 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
3916
3917 };
3918
3919 if ($@) {
3920 $ex = $@;
3921 }
3922
3923 $wfh->print("done\n");
3924 $wfh->flush();
3925
3926 } else {
3927 eval { server_wait($config_file, $rfh) };
3928 if ($@) {
3929 warn($@);
3930 exit 1;
3931 }
3932
3933 exit 0;
3934 }
3935
3936 # Stop server
3937 server_stop($pid_file);
3938
3939 $self->assert_child_ok($pid);
3940
3941 if ($ex) {
3942 test_append_logfile($log_file, $ex);
3943 unlink($log_file);
3944
3945 die($ex);
3946 }
3947
3948 unlink($log_file);
3949 }
3950
3951 sub sql_openssl_auth_type_sha1 {
3952 my $self = shift;
3953 my $tmpdir = $self->{tmpdir};
3954
3955 my $config_file = "$tmpdir/sqlite.conf";
3956 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
3957 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
3958
3959 my $log_file = test_get_logfile();
3960
3961 my $user = 'proftpd';
3962 my $passwd = '{sha1}W6ph5Mm5Pz8GgiULbPgzG37mj9g=';
3963 my $group = 'ftpd';
3964 my $home_dir = File::Spec->rel2abs($tmpdir);
3965 my $uid = 500;
3966 my $gid = 500;
3967
3968 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3969
3970 # Build up sqlite3 command to create users, groups tables and populate them
3971 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
3972
3973 if (open(my $fh, "> $db_script")) {
3974 print $fh <<EOS;
3975 CREATE TABLE users (
3976 userid TEXT,
3977 passwd TEXT,
3978 uid INTEGER,
3979 gid INTEGER,
3980 homedir TEXT,
3981 shell TEXT,
3982 lastdir TEXT
3983 );
3984 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
3985
3986 CREATE TABLE groups (
3987 groupname TEXT,
3988 gid INTEGER,
3989 members TEXT
3990 );
3991 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
3992 EOS
3993
3994 unless (close($fh)) {
3995 die("Can't write $db_script: $!");
3996 }
3997
3998 } else {
3999 die("Can't open $db_script: $!");
4000 }
4001
4002 my $cmd = "sqlite3 $db_file < $db_script";
4003 build_db($cmd, $db_script);
4004
4005 # Make sure that, if we're running as root, the database file has
4006 # the permissions/privs set for use by proftpd
4007 if ($< == 0) {
4008 unless (chmod(0666, $db_file)) {
4009 die("Can't set perms on $db_file to 0666: $!");
4010 }
4011 }
4012
4013 my $config = {
4014 PidFile => $pid_file,
4015 ScoreboardFile => $scoreboard_file,
4016 SystemLog => $log_file,
4017
4018 IfModules => {
4019 'mod_delay.c' => {
4020 DelayEngine => 'off',
4021 },
4022
4023 'mod_sql.c' => {
4024 SQLAuthTypes => 'OpenSSL',
4025 SQLBackend => 'sqlite3',
4026 SQLConnectInfo => $db_file,
4027 SQLLogFile => $log_file,
4028 },
4029 },
4030 };
4031
4032 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4033
4034 # Open pipes, for use between the parent and child processes. Specifically,
4035 # the child will indicate when it's done with its test by writing a message
4036 # to the parent.
4037 my ($rfh, $wfh);
4038 unless (pipe($rfh, $wfh)) {
4039 die("Can't open pipe: $!");
4040 }
4041
4042 my $ex;
4043
4044 # Fork child
4045 $self->handle_sigchld();
4046 defined(my $pid = fork()) or die("Can't fork: $!");
4047 if ($pid) {
4048 eval {
4049 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4050
4051 $client->login($user, 'password');
4052
4053 my $resp_msgs = $client->response_msgs();
4054 my $nmsgs = scalar(@$resp_msgs);
4055
4056 my $expected;
4057
4058 $expected = 1;
4059 $self->assert($expected == $nmsgs,
4060 test_msg("Expected $expected, got $nmsgs"));
4061
4062 $expected = "User proftpd logged in";
4063 $self->assert($expected eq $resp_msgs->[0],
4064 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
4065
4066 };
4067
4068 if ($@) {
4069 $ex = $@;
4070 }
4071
4072 $wfh->print("done\n");
4073 $wfh->flush();
4074
4075 } else {
4076 eval { server_wait($config_file, $rfh) };
4077 if ($@) {
4078 warn($@);
4079 exit 1;
4080 }
4081
4082 exit 0;
4083 }
4084
4085 # Stop server
4086 server_stop($pid_file);
4087
4088 $self->assert_child_ok($pid);
4089
4090 if ($ex) {
4091 test_append_logfile($log_file, $ex);
4092 unlink($log_file);
4093
4094 die($ex);
4095 }
4096
4097 unlink($log_file);
4098 }
4099
4100 sub get_resp_mesgs {
4101 my $db_file = shift;
4102 my $where = shift;
4103 my $cols = shift;
4104
4105 my $sql = "SELECT $cols FROM ftpsessions";
4106 if ($where) {
4107 $sql .= " WHERE $where";
4108 }
4109
4110 my $cmd = "sqlite3 $db_file \"$sql\"";
4111
4112 if ($ENV{TEST_VERBOSE}) {
4113 print STDERR "Executing sqlite3: $cmd\n";
4114 }
4115
4116 my $res = join('', `$cmd`);
4117 chomp($res);
4118
4119 # The default sqlite3 delimiter is '|'
4120 return split(/\|/, $res);
4121 }
4122
4123 sub sql_sqllog_var_S {
4124 my $self = shift;
4125 my $tmpdir = $self->{tmpdir};
4126
4127 my $config_file = "$tmpdir/sqlite.conf";
4128 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
4129 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
4130
4131 my $log_file = test_get_logfile();
4132
4133 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
4134 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
4135
4136 my $user = 'proftpd';
4137 my $passwd = 'test';
4138 my $group = 'ftpd';
4139 my $home_dir = File::Spec->rel2abs($tmpdir);
4140 my $uid = 500;
4141 my $gid = 500;
4142
4143 # Make sure that, if we're running as root, that the home directory has
4144 # permissions/privs set for the account we create
4145 if ($< == 0) {
4146 unless (chmod(0755, $home_dir)) {
4147 die("Can't set perms on $home_dir to 0755: $!");
4148 }
4149
4150 unless (chown($uid, $gid, $home_dir)) {
4151 die("Can't set owner of $home_dir to $uid/$gid: $!");
4152 }
4153 }
4154
4155 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4156 '/bin/bash');
4157 auth_group_write($auth_group_file, $group, $gid, $user);
4158
4159 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4160
4161 # Build up sqlite3 command to create users, groups tables and populate them
4162 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4163
4164 if (open(my $fh, "> $db_script")) {
4165 print $fh <<EOS;
4166 CREATE TABLE ftpsessions (
4167 user TEXT,
4168 ip_addr TEXT,
4169 resp_mesg TEXT
4170 );
4171 EOS
4172
4173 unless (close($fh)) {
4174 die("Can't write $db_script: $!");
4175 }
4176
4177 } else {
4178 die("Can't open $db_script: $!");
4179 }
4180
4181 my $cmd = "sqlite3 $db_file < $db_script";
4182 build_db($cmd, $db_script);
4183
4184 # Make sure that, if we're running as root, the database file has
4185 # the permissions/privs set for use by proftpd
4186 if ($< == 0) {
4187 unless (chmod(0666, $db_file)) {
4188 die("Can't set perms on $db_file to 0666: $!");
4189 }
4190 }
4191
4192 my $config = {
4193 PidFile => $pid_file,
4194 ScoreboardFile => $scoreboard_file,
4195 SystemLog => $log_file,
4196
4197 AuthUserFile => $auth_user_file,
4198 AuthGroupFile => $auth_group_file,
4199 AuthOrder => 'mod_auth_file.c',
4200
4201 IfModules => {
4202 'mod_delay.c' => {
4203 DelayEngine => 'off',
4204 },
4205
4206 'mod_sql.c' => {
4207 SQLEngine => 'log',
4208 SQLBackend => 'sqlite3',
4209 SQLConnectInfo => $db_file,
4210 SQLLogFile => $log_file,
4211 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, ip_addr, resp_mesg) VALUES (\'%u\', \'%L\', \'%S\')"',
4212 SQLLog => 'LIST info',
4213 },
4214 },
4215 };
4216
4217 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4218
4219 # Open pipes, for use between the parent and child processes. Specifically,
4220 # the child will indicate when it's done with its test by writing a message
4221 # to the parent.
4222 my ($rfh, $wfh);
4223 unless (pipe($rfh, $wfh)) {
4224 die("Can't open pipe: $!");
4225 }
4226
4227 my $ex;
4228
4229 # Fork child
4230 $self->handle_sigchld();
4231 defined(my $pid = fork()) or die("Can't fork: $!");
4232 if ($pid) {
4233 eval {
4234 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4235 $client->login($user, $passwd);
4236 $client->list();
4237 $client->quit();
4238 };
4239
4240 if ($@) {
4241 $ex = $@;
4242 }
4243
4244 $wfh->print("done\n");
4245 $wfh->flush();
4246
4247 } else {
4248 eval { server_wait($config_file, $rfh) };
4249 if ($@) {
4250 warn($@);
4251 exit 1;
4252 }
4253
4254 exit 0;
4255 }
4256
4257 # Stop server
4258 server_stop($pid_file);
4259
4260 $self->assert_child_ok($pid);
4261
4262 if ($ex) {
4263 test_append_logfile($log_file, $ex);
4264 unlink($log_file);
4265
4266 die($ex);
4267 }
4268
4269 my ($login, $ip_addr, $resp_mesg) = get_resp_mesgs($db_file,
4270 "user = \'$user\'", "user, ip_addr, resp_mesg");
4271
4272 my $expected;
4273
4274 $expected = $user;
4275 $self->assert($expected eq $login,
4276 test_msg("Expected '$expected', got '$login'"));
4277
4278 $expected = '127.0.0.1';
4279 $self->assert($expected eq $ip_addr,
4280 test_msg("Expected '$expected', got '$ip_addr'"));
4281
4282 $expected = 'Transfer complete';
4283 $self->assert($expected eq $resp_mesg,
4284 test_msg("Expected '$expected', got '$resp_mesg'"));
4285
4286 unlink($log_file);
4287 }
4288
4289 sub sql_sqllog_var_S_err {
4290 my $self = shift;
4291 my $tmpdir = $self->{tmpdir};
4292 my $setup = test_setup($tmpdir, 'sqlite');
4293
4294 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4295
4296 # Build up sqlite3 command to create users, groups tables and populate them
4297 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4298
4299 if (open(my $fh, "> $db_script")) {
4300 print $fh <<EOS;
4301 CREATE TABLE ftpsessions (
4302 user TEXT,
4303 ip_addr TEXT,
4304 resp_mesg TEXT
4305 );
4306 EOS
4307
4308 unless (close($fh)) {
4309 die("Can't write $db_script: $!");
4310 }
4311
4312 } else {
4313 die("Can't open $db_script: $!");
4314 }
4315
4316 my $cmd = "sqlite3 $db_file < $db_script";
4317 build_db($cmd, $db_script);
4318
4319 # Make sure that, if we're running as root, the database file has
4320 # the permissions/privs set for use by proftpd
4321 if ($< == 0) {
4322 unless (chmod(0666, $db_file)) {
4323 die("Can't set perms on $db_file to 0666: $!");
4324 }
4325 }
4326
4327 my $config = {
4328 PidFile => $setup->{pid_file},
4329 ScoreboardFile => $setup->{scoreboard_file},
4330 SystemLog => $setup->{log_file},
4331 TraceLog => $setup->{log_file},
4332 Trace => 'jot:20 sql:20',
4333
4334 AuthUserFile => $setup->{auth_user_file},
4335 AuthGroupFile => $setup->{auth_group_file},
4336 AuthOrder => 'mod_auth_file.c',
4337
4338 IfModules => {
4339 'mod_delay.c' => {
4340 DelayEngine => 'off',
4341 },
4342
4343 'mod_sql.c' => {
4344 SQLEngine => 'log',
4345 SQLBackend => 'sqlite3',
4346 SQLConnectInfo => $db_file,
4347 SQLLogFile => $setup->{log_file},
4348 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, ip_addr, resp_mesg) VALUES (\'%u\', \'%L\', \'%S\')"',
4349 SQLLog => 'ERR_* info',
4350 },
4351 },
4352 };
4353
4354 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
4355 $config);
4356
4357 # Open pipes, for use between the parent and child processes. Specifically,
4358 # the child will indicate when it's done with its test by writing a message
4359 # to the parent.
4360 my ($rfh, $wfh);
4361 unless (pipe($rfh, $wfh)) {
4362 die("Can't open pipe: $!");
4363 }
4364
4365 my $ex;
4366
4367 # Fork child
4368 $self->handle_sigchld();
4369 defined(my $pid = fork()) or die("Can't fork: $!");
4370 if ($pid) {
4371 eval {
4372 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4373 $client->login($setup->{user}, $setup->{passwd});
4374 eval { $client->quote('CLIENT', 'NcFTP 3.2.2 linux-x86_64-glibc2.3') };
4375 $client->quit();
4376 };
4377 if ($@) {
4378 $ex = $@;
4379 }
4380
4381 $wfh->print("done\n");
4382 $wfh->flush();
4383
4384 } else {
4385 eval { server_wait($setup->{config_file}, $rfh) };
4386 if ($@) {
4387 warn($@);
4388 exit 1;
4389 }
4390
4391 exit 0;
4392 }
4393
4394 # Stop server
4395 server_stop($setup->{pid_file});
4396 $self->assert_child_ok($pid);
4397
4398 if ($ex) {
4399 test_cleanup($setup->{log_file}, $ex);
4400 }
4401
4402 eval {
4403 my ($login, $ip_addr, $resp_mesg) = get_resp_mesgs($db_file,
4404 "user = \'$setup->{user}\'", "user, ip_addr, resp_mesg");
4405
4406 my $expected = $setup->{user};
4407 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
4408
4409 $expected = '127.0.0.1';
4410 $self->assert($expected eq $ip_addr,
4411 "Expected '$expected', got '$ip_addr'");
4412
4413 $expected = 'CLIENT not understood';
4414 $self->assert($expected eq $resp_mesg,
4415 "Expected '$expected', got '$resp_mesg'");
4416 };
4417 if ($@) {
4418 $ex = $@;
4419 }
4420
4421 test_cleanup($setup->{log_file}, $ex);
4422 }
4423
4424 sub sql_negative_cache_bug3282 {
4425 my $self = shift;
4426 my $tmpdir = $self->{tmpdir};
4427
4428 my $config_file = "$tmpdir/sqlite.conf";
4429 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
4430 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
4431
4432 my $log_file = test_get_logfile();
4433
4434 my $user = 'proftpd';
4435 my $passwd = 'test';
4436 my $group = 'ftpd';
4437 my $home_dir = File::Spec->rel2abs($tmpdir);
4438 my $uid = 500;
4439 my $gid = 500;
4440
4441 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4442
4443 # Build up sqlite3 command to create users, groups tables and populate them
4444 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4445
4446 if (open(my $fh, "> $db_script")) {
4447 print $fh <<EOS;
4448 CREATE TABLE users (
4449 userid TEXT,
4450 passwd TEXT,
4451 uid INTEGER,
4452 gid INTEGER,
4453 homedir TEXT,
4454 shell TEXT,
4455 lastdir TEXT
4456 );
4457 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
4458
4459 CREATE TABLE groups (
4460 groupname TEXT,
4461 gid INTEGER,
4462 members TEXT
4463 );
4464 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
4465 EOS
4466
4467 unless (close($fh)) {
4468 die("Can't write $db_script: $!");
4469 }
4470
4471 } else {
4472 die("Can't open $db_script: $!");
4473 }
4474
4475 my $cmd = "sqlite3 $db_file < $db_script";
4476 build_db($cmd, $db_script);
4477
4478 # Make sure that, if we're running as root, the database file has
4479 # the permissions/privs set for use by proftpd
4480 if ($< == 0) {
4481 unless (chmod(0666, $db_file)) {
4482 die("Can't set perms on $db_file to 0666: $!");
4483 }
4484 }
4485
4486 my $config = {
4487 PidFile => $pid_file,
4488 ScoreboardFile => $scoreboard_file,
4489 SystemLog => $log_file,
4490 TraceLog => $log_file,
4491 Trace => 'auth:10',
4492
4493 IfModules => {
4494 'mod_delay.c' => {
4495 DelayEngine => 'off',
4496 },
4497
4498 'mod_sql.c' => {
4499 SQLAuthTypes => 'plaintext',
4500 SQLBackend => 'sqlite3',
4501 SQLConnectInfo => $db_file,
4502 SQLLogFile => $log_file,
4503 SQLNegativeCache => 'on',
4504 },
4505 },
4506 };
4507
4508 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4509
4510 # Open pipes, for use between the parent and child processes. Specifically,
4511 # the child will indicate when it's done with its test by writing a message
4512 # to the parent.
4513 my ($rfh, $wfh);
4514 unless (pipe($rfh, $wfh)) {
4515 die("Can't open pipe: $!");
4516 }
4517
4518 my $ex;
4519
4520 # Fork child
4521 $self->handle_sigchld();
4522 defined(my $pid = fork()) or die("Can't fork: $!");
4523 if ($pid) {
4524 eval {
4525 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4526
4527 eval { $client->login('foo', 'foo') };
4528 unless ($@) {
4529 die("Login succeeded unexpectedly");
4530 }
4531
4532 $client->login($user, $passwd);
4533
4534 my $expected;
4535
4536 my $resp_msgs = $client->response_msgs();
4537 my $nmsgs = scalar(@$resp_msgs);
4538
4539 $expected = 1;
4540 $self->assert($expected == $nmsgs,
4541 test_msg("Expected $expected, got $nmsgs"));
4542
4543 $expected = "User proftpd logged in";
4544 $self->assert($expected eq $resp_msgs->[0],
4545 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
4546 };
4547
4548 if ($@) {
4549 $ex = $@;
4550 }
4551
4552 $wfh->print("done\n");
4553 $wfh->flush();
4554
4555 } else {
4556 eval { server_wait($config_file, $rfh) };
4557 if ($@) {
4558 warn($@);
4559 exit 1;
4560 }
4561
4562 exit 0;
4563 }
4564
4565 # Stop server
4566 server_stop($pid_file);
4567
4568 $self->assert_child_ok($pid);
4569
4570 if ($ex) {
4571 test_append_logfile($log_file, $ex);
4572 unlink($log_file);
4573
4574 die($ex);
4575 }
4576
4577 unlink($log_file);
4578 }
4579
4580 sub sql_sqllog_var_T_rnfr {
4581 my $self = shift;
4582 my $tmpdir = $self->{tmpdir};
4583 my $setup = test_setup($tmpdir, 'sqlite');
4584
4585 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4586
4587 # Build up sqlite3 command to create users, groups tables and populate them
4588 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4589
4590 if (open(my $fh, "> $db_script")) {
4591 print $fh <<EOS;
4592 CREATE TABLE ftpsessions (
4593 user TEXT,
4594 ip_addr TEXT,
4595 rename_from TEXT,
4596 rename_time FLOAT
4597 );
4598 EOS
4599
4600 unless (close($fh)) {
4601 die("Can't write $db_script: $!");
4602 }
4603
4604 } else {
4605 die("Can't open $db_script: $!");
4606 }
4607
4608 my $cmd = "sqlite3 $db_file < $db_script";
4609 build_db($cmd, $db_script);
4610
4611 # Make sure that, if we're running as root, the database file has
4612 # the permissions/privs set for use by proftpd
4613 if ($< == 0) {
4614 unless (chmod(0666, $db_file)) {
4615 die("Can't set perms on $db_file to 0666: $!");
4616 }
4617 }
4618
4619 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
4620 if (open(my $fh, "> $src_file")) {
4621 close($fh);
4622
4623 } else {
4624 die("Can't open $src_file: $!");
4625 }
4626
4627 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
4628
4629 my $config = {
4630 PidFile => $setup->{pid_file},
4631 ScoreboardFile => $setup->{scoreboard_file},
4632 SystemLog => $setup->{log_file},
4633 TraceLog => $setup->{log_file},
4634 Trace => 'jot:20 sql:20',
4635
4636 AuthUserFile => $setup->{auth_user_file},
4637 AuthGroupFile => $setup->{auth_group_file},
4638 AuthOrder => 'mod_auth_file.c',
4639
4640 IfModules => {
4641 'mod_delay.c' => {
4642 DelayEngine => 'off',
4643 },
4644
4645 'mod_sql.c' => {
4646 SQLEngine => 'log',
4647 SQLBackend => 'sqlite3',
4648 SQLConnectInfo => $db_file,
4649 SQLLogFile => $setup->{log_file},
4650 SQLNamedQuery => 'rename FREEFORM "INSERT INTO ftpsessions (user, ip_addr, rename_from, rename_time) VALUES (\'%u\', \'%L\', \'%w\', %T)"',
4651 SQLLog => 'RNTO rename',
4652 },
4653 },
4654 };
4655
4656 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
4657 $config);
4658
4659 # Open pipes, for use between the parent and child processes. Specifically,
4660 # the child will indicate when it's done with its test by writing a message
4661 # to the parent.
4662 my ($rfh, $wfh);
4663 unless (pipe($rfh, $wfh)) {
4664 die("Can't open pipe: $!");
4665 }
4666
4667 my $ex;
4668
4669 # Fork child
4670 $self->handle_sigchld();
4671 defined(my $pid = fork()) or die("Can't fork: $!");
4672 if ($pid) {
4673 eval {
4674 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
4675 $client->login($setup->{user}, $setup->{passwd});
4676 $client->rnfr('test.txt');
4677 $client->rnto('foo.txt');
4678 };
4679 if ($@) {
4680 $ex = $@;
4681 }
4682
4683 $wfh->print("done\n");
4684 $wfh->flush();
4685
4686 } else {
4687 eval { server_wait($setup->{config_file}, $rfh) };
4688 if ($@) {
4689 warn($@);
4690 exit 1;
4691 }
4692
4693 exit 0;
4694 }
4695
4696 # Stop server
4697 server_stop($setup->{pid_file});
4698 $self->assert_child_ok($pid);
4699
4700 if ($ex) {
4701 test_cleanup($setup->{log_file}, $ex);
4702 }
4703
4704 eval {
4705 my $query = "SELECT user, ip_addr, rename_from, rename_time FROM ftpsessions WHERE user = \'$setup->{user}\'";
4706 $cmd = "sqlite3 $db_file \"$query\"";
4707
4708 if ($ENV{TEST_VERBOSE}) {
4709 print STDERR "Executing sqlite3: $cmd\n";
4710 }
4711
4712 my $res = join('', `$cmd`);
4713 chomp($res);
4714
4715 my ($login, $ip_addr, $rnfr_path, $rename_time) = split(/\|/, $res);
4716
4717 my $expected = $setup->{user};
4718 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
4719
4720 $expected = '127.0.0.1';
4721 $self->assert($expected eq $ip_addr,
4722 "Expected '$expected', got '$ip_addr'");
4723
4724 if ($^O eq 'darwin') {
4725 # MacOSX-specific hack
4726 $src_file = '/private' . $src_file;
4727 }
4728
4729 $expected = $src_file;
4730 $self->assert($expected eq $rnfr_path,
4731 "Expected '$expected', got '$rnfr_path'");
4732
4733 $expected = '0.0';
4734 $self->assert($expected eq $rename_time,
4735 "Expected '$expected', got '$rename_time'");
4736 };
4737 if ($@) {
4738 $ex = $@;
4739 }
4740
4741 test_cleanup($setup->{log_file}, $ex);
4742 }
4743
4744 sub sql_sqllog_var_T_retr {
4745 my $self = shift;
4746 my $tmpdir = $self->{tmpdir};
4747 my $setup = test_setup($tmpdir, 'sqlite');
4748
4749 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4750
4751 # Build up sqlite3 command to create users, groups tables and populate them
4752 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4753
4754 if (open(my $fh, "> $db_script")) {
4755 print $fh <<EOS;
4756 CREATE TABLE ftpsessions (
4757 user TEXT PRIMARY KEY,
4758 ip_addr TEXT,
4759 download_time FLOAT
4760 );
4761 EOS
4762
4763 unless (close($fh)) {
4764 die("Can't write $db_script: $!");
4765 }
4766
4767 } else {
4768 die("Can't open $db_script: $!");
4769 }
4770
4771 my $cmd = "sqlite3 $db_file < $db_script";
4772 build_db($cmd, $db_script);
4773
4774 # Make sure that, if we're running as root, the database file has
4775 # the permissions/privs set for use by proftpd
4776 if ($< == 0) {
4777 unless (chmod(0666, $db_file)) {
4778 die("Can't set perms on $db_file to 0666: $!");
4779 }
4780 }
4781
4782 my $config = {
4783 PidFile => $setup->{pid_file},
4784 ScoreboardFile => $setup->{scoreboard_file},
4785 SystemLog => $setup->{log_file},
4786
4787 AuthUserFile => $setup->{auth_user_file},
4788 AuthGroupFile => $setup->{auth_group_file},
4789 AuthOrder => 'mod_auth_file.c',
4790
4791 IfModules => {
4792 'mod_delay.c' => {
4793 DelayEngine => 'off',
4794 },
4795
4796 'mod_sql.c' => {
4797 SQLEngine => 'log',
4798 SQLBackend => 'sqlite3',
4799 SQLConnectInfo => $db_file,
4800 SQLLogFile => $setup->{log_file},
4801 SQLNamedQuery => 'download FREEFORM "INSERT INTO ftpsessions (user, ip_addr, download_time) VALUES (\'%u\', \'%L\', %T)"',
4802 SQLLog => 'RETR download',
4803 },
4804 },
4805 };
4806
4807 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
4808 $config);
4809
4810 # Open pipes, for use between the parent and child processes. Specifically,
4811 # the child will indicate when it's done with its test by writing a message
4812 # to the parent.
4813 my ($rfh, $wfh);
4814 unless (pipe($rfh, $wfh)) {
4815 die("Can't open pipe: $!");
4816 }
4817
4818 my $ex;
4819
4820 # Fork child
4821 $self->handle_sigchld();
4822 defined(my $pid = fork()) or die("Can't fork: $!");
4823 if ($pid) {
4824 eval {
4825 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
4826 $client->login($setup->{user}, $setup->{passwd});
4827
4828 my $conn = $client->retr_raw('sqlite.conf');
4829 unless ($conn) {
4830 die("RETR sqlite.conf failed: " . $client->response_code() . " " .
4831 $client->response_msg());
4832 }
4833
4834 my $buf;
4835 while ($conn->read($buf, 8192, 30)) {
4836 }
4837 $conn->close();
4838
4839 my $resp_code = $client->response_code();
4840 my $resp_msg = $client->response_msg();
4841 $self->assert_transfer_ok($resp_code, $resp_msg);
4842
4843 $client->quit();
4844 };
4845
4846 if ($@) {
4847 $ex = $@;
4848 }
4849
4850 $wfh->print("done\n");
4851 $wfh->flush();
4852
4853 } else {
4854 eval { server_wait($setup->{config_file}, $rfh) };
4855 if ($@) {
4856 warn($@);
4857 exit 1;
4858 }
4859
4860 exit 0;
4861 }
4862
4863 # Stop server
4864 server_stop($setup->{pid_file});
4865 $self->assert_child_ok($pid);
4866
4867 eval {
4868 my $query = "SELECT user, ip_addr, download_time FROM ftpsessions WHERE user = \'$setup->{user}\'";
4869 $cmd = "sqlite3 $db_file \"$query\"";
4870
4871 if ($ENV{TEST_VERBOSE}) {
4872 print STDERR "Executing sqlite3: $cmd\n";
4873 }
4874
4875 my $res = join('', `$cmd`);
4876 chomp($res);
4877
4878 my ($login, $ip_addr, $download_time) = split(/\|/, $res);
4879
4880 if ($ENV{TEST_VERBOSE}) {
4881 print STDERR "# login = '$login', ip_addr = '$ip_addr', download_time = $download_time\n";
4882 }
4883
4884 my $expected;
4885
4886 $expected = $setup->{user};
4887 $self->assert($expected eq $login,
4888 test_msg("Expected '$expected', got '$login'"));
4889
4890 $expected = '127.0.0.1';
4891 $self->assert($expected eq $ip_addr,
4892 test_msg("Expected '$expected', got '$ip_addr'"));
4893
4894 $self->assert($download_time > 0.0,
4895 test_msg("Expected > 0.0, got $download_time"));
4896 };
4897 if ($@) {
4898 $ex = $@;
4899 }
4900
4901 test_cleanup($setup->{log_file}, $ex);
4902 }
4903
4904 sub sql_sqllog_var_transfer_millisecs_retr_bug4218 {
4905 my $self = shift;
4906 my $tmpdir = $self->{tmpdir};
4907 my $setup = test_setup($tmpdir, 'sqlite');
4908
4909 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
4910
4911 # Build up sqlite3 command to create users, groups tables and populate them
4912 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
4913
4914 if (open(my $fh, "> $db_script")) {
4915 print $fh <<EOS;
4916 CREATE TABLE ftpsessions (
4917 user TEXT PRIMARY KEY,
4918 ip_addr TEXT,
4919 download_ms INTEGER
4920 );
4921 EOS
4922
4923 unless (close($fh)) {
4924 die("Can't write $db_script: $!");
4925 }
4926
4927 } else {
4928 die("Can't open $db_script: $!");
4929 }
4930
4931 my $cmd = "sqlite3 $db_file < $db_script";
4932 build_db($cmd, $db_script);
4933
4934 # Make sure that, if we're running as root, the database file has
4935 # the permissions/privs set for use by proftpd
4936 if ($< == 0) {
4937 unless (chmod(0666, $db_file)) {
4938 die("Can't set perms on $db_file to 0666: $!");
4939 }
4940 }
4941
4942 my $config = {
4943 PidFile => $setup->{pid_file},
4944 ScoreboardFile => $setup->{scoreboard_file},
4945 SystemLog => $setup->{log_file},
4946
4947 AuthUserFile => $setup->{auth_user_file},
4948 AuthGroupFile => $setup->{auth_group_file},
4949 AuthOrder => 'mod_auth_file.c',
4950
4951 IfModules => {
4952 'mod_delay.c' => {
4953 DelayEngine => 'off',
4954 },
4955
4956 'mod_sql.c' => {
4957 SQLEngine => 'log',
4958 SQLBackend => 'sqlite3',
4959 SQLConnectInfo => $db_file,
4960 SQLLogFile => $setup->{log_file},
4961 SQLNamedQuery => 'download FREEFORM "INSERT INTO ftpsessions (user, ip_addr, download_ms) VALUES (\'%u\', \'%L\', %{transfer-millisecs})"',
4962 SQLLog => 'RETR download',
4963 },
4964 },
4965 };
4966
4967 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
4968 $config);
4969
4970 # Open pipes, for use between the parent and child processes. Specifically,
4971 # the child will indicate when it's done with its test by writing a message
4972 # to the parent.
4973 my ($rfh, $wfh);
4974 unless (pipe($rfh, $wfh)) {
4975 die("Can't open pipe: $!");
4976 }
4977
4978 my $ex;
4979
4980 # Fork child
4981 $self->handle_sigchld();
4982 defined(my $pid = fork()) or die("Can't fork: $!");
4983 if ($pid) {
4984 eval {
4985 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
4986 $client->login($setup->{user}, $setup->{passwd});
4987
4988 my $conn = $client->retr_raw('sqlite.conf');
4989 unless ($conn) {
4990 die("RETR sqlite.conf failed: " . $client->response_code() . " " .
4991 $client->response_msg());
4992 }
4993
4994 my $buf;
4995 while ($conn->read($buf, 8192, 30)) {
4996 }
4997 $conn->close();
4998
4999 my $resp_code = $client->response_code();
5000 my $resp_msg = $client->response_msg();
5001 $self->assert_transfer_ok($resp_code, $resp_msg);
5002
5003 $client->quit();
5004 };
5005
5006 if ($@) {
5007 $ex = $@;
5008 }
5009
5010 $wfh->print("done\n");
5011 $wfh->flush();
5012
5013 } else {
5014 eval { server_wait($setup->{config_file}, $rfh) };
5015 if ($@) {
5016 warn($@);
5017 exit 1;
5018 }
5019
5020 exit 0;
5021 }
5022
5023 # Stop server
5024 server_stop($setup->{pid_file});
5025 $self->assert_child_ok($pid);
5026
5027 eval {
5028 my $query = "SELECT user, ip_addr, download_ms FROM ftpsessions WHERE user = \'$setup->{user}\'";
5029 $cmd = "sqlite3 $db_file \"$query\"";
5030
5031 if ($ENV{TEST_VERBOSE}) {
5032 print STDERR "Executing sqlite3: $cmd\n";
5033 }
5034
5035 my $res = join('', `$cmd`);
5036 chomp($res);
5037
5038 my ($login, $ip_addr, $download_ms) = split(/\|/, $res);
5039
5040 if ($ENV{TEST_VERBOSE}) {
5041 print STDERR "# login = '$login', ip_addr = '$ip_addr', download_ms = $download_ms\n";
5042 }
5043
5044 my $expected;
5045
5046 $expected = $setup->{user};
5047 $self->assert($expected eq $login,
5048 test_msg("Expected '$expected', got '$login'"));
5049
5050 $expected = '127.0.0.1';
5051 $self->assert($expected eq $ip_addr,
5052 test_msg("Expected '$expected', got '$ip_addr'"));
5053
5054 $self->assert($download_ms > 0,
5055 test_msg("Expected > 0, got $download_ms"));
5056 };
5057 if ($@) {
5058 $ex = $@;
5059 }
5060
5061 test_cleanup($setup->{log_file}, $ex);
5062 }
5063
5064 sub sql_sqllog_var_R_retr_bug4218 {
5065 my $self = shift;
5066 my $tmpdir = $self->{tmpdir};
5067 my $setup = test_setup($tmpdir, 'sqlite');
5068
5069 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5070
5071 # Build up sqlite3 command to create users, groups tables and populate them
5072 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5073
5074 if (open(my $fh, "> $db_script")) {
5075 print $fh <<EOS;
5076 CREATE TABLE ftpsessions (
5077 user TEXT PRIMARY KEY,
5078 ip_addr TEXT,
5079 response_ms INTEGER
5080 );
5081 EOS
5082
5083 unless (close($fh)) {
5084 die("Can't write $db_script: $!");
5085 }
5086
5087 } else {
5088 die("Can't open $db_script: $!");
5089 }
5090
5091 my $cmd = "sqlite3 $db_file < $db_script";
5092 build_db($cmd, $db_script);
5093
5094 # Make sure that, if we're running as root, the database file has
5095 # the permissions/privs set for use by proftpd
5096 if ($< == 0) {
5097 unless (chmod(0666, $db_file)) {
5098 die("Can't set perms on $db_file to 0666: $!");
5099 }
5100 }
5101
5102 my $config = {
5103 PidFile => $setup->{pid_file},
5104 ScoreboardFile => $setup->{scoreboard_file},
5105 SystemLog => $setup->{log_file},
5106
5107 AuthUserFile => $setup->{auth_user_file},
5108 AuthGroupFile => $setup->{auth_group_file},
5109 AuthOrder => 'mod_auth_file.c',
5110
5111 IfModules => {
5112 'mod_delay.c' => {
5113 DelayEngine => 'off',
5114 },
5115
5116 'mod_sql.c' => {
5117 SQLEngine => 'log',
5118 SQLBackend => 'sqlite3',
5119 SQLConnectInfo => $db_file,
5120 SQLLogFile => $setup->{log_file},
5121 SQLNamedQuery => 'download FREEFORM "INSERT INTO ftpsessions (user, ip_addr, response_ms) VALUES (\'%u\', \'%L\', %R)"',
5122 SQLLog => 'RETR download',
5123 },
5124 },
5125 };
5126
5127 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
5128 $config);
5129
5130 # Open pipes, for use between the parent and child processes. Specifically,
5131 # the child will indicate when it's done with its test by writing a message
5132 # to the parent.
5133 my ($rfh, $wfh);
5134 unless (pipe($rfh, $wfh)) {
5135 die("Can't open pipe: $!");
5136 }
5137
5138 my $ex;
5139
5140 # Fork child
5141 $self->handle_sigchld();
5142 defined(my $pid = fork()) or die("Can't fork: $!");
5143 if ($pid) {
5144 eval {
5145 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
5146 $client->login($setup->{user}, $setup->{passwd});
5147
5148 my $conn = $client->retr_raw('sqlite.conf');
5149 unless ($conn) {
5150 die("RETR sqlite.conf failed: " . $client->response_code() . " " .
5151 $client->response_msg());
5152 }
5153
5154 my $buf;
5155 while ($conn->read($buf, 8192, 30)) {
5156 }
5157 $conn->close();
5158
5159 my $resp_code = $client->response_code();
5160 my $resp_msg = $client->response_msg();
5161 $self->assert_transfer_ok($resp_code, $resp_msg);
5162
5163 $client->quit();
5164 };
5165
5166 if ($@) {
5167 $ex = $@;
5168 }
5169
5170 $wfh->print("done\n");
5171 $wfh->flush();
5172
5173 } else {
5174 eval { server_wait($setup->{config_file}, $rfh) };
5175 if ($@) {
5176 warn($@);
5177 exit 1;
5178 }
5179
5180 exit 0;
5181 }
5182
5183 # Stop server
5184 server_stop($setup->{pid_file});
5185 $self->assert_child_ok($pid);
5186
5187 eval {
5188 my $query = "SELECT user, ip_addr, response_ms FROM ftpsessions WHERE user = \'$setup->{user}\'";
5189 $cmd = "sqlite3 $db_file \"$query\"";
5190
5191 if ($ENV{TEST_VERBOSE}) {
5192 print STDERR "Executing sqlite3: $cmd\n";
5193 }
5194
5195 my $res = join('', `$cmd`);
5196 chomp($res);
5197
5198 my ($login, $ip_addr, $response_ms) = split(/\|/, $res);
5199
5200 if ($ENV{TEST_VERBOSE}) {
5201 print STDERR "# login = '$login', ip_addr = '$ip_addr', response_ms = $response_ms\n";
5202 }
5203
5204 my $expected;
5205
5206 $expected = $setup->{user};
5207 $self->assert($expected eq $login,
5208 test_msg("Expected '$expected', got '$login'"));
5209
5210 $expected = '127.0.0.1';
5211 $self->assert($expected eq $ip_addr,
5212 test_msg("Expected '$expected', got '$ip_addr'"));
5213
5214 $self->assert($response_ms > 0,
5215 test_msg("Expected > 0, got $response_ms"));
5216 };
5217 if ($@) {
5218 $ex = $@;
5219 }
5220
5221 test_cleanup($setup->{log_file}, $ex);
5222 }
5223
5224 sub sql_sqllog_exit {
5225 my $self = shift;
5226 my $tmpdir = $self->{tmpdir};
5227
5228 my $config_file = "$tmpdir/sqlite.conf";
5229 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
5230 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
5231
5232 my $log_file = test_get_logfile();
5233
5234 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
5235 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
5236
5237 my $user = 'proftpd';
5238 my $passwd = 'test';
5239 my $group = 'ftpd';
5240 my $home_dir = File::Spec->rel2abs($tmpdir);
5241 my $uid = 500;
5242 my $gid = 500;
5243
5244 # Make sure that, if we're running as root, that the home directory has
5245 # permissions/privs set for the account we create
5246 if ($< == 0) {
5247 unless (chmod(0755, $home_dir)) {
5248 die("Can't set perms on $home_dir to 0755: $!");
5249 }
5250
5251 unless (chown($uid, $gid, $home_dir)) {
5252 die("Can't set owner of $home_dir to $uid/$gid: $!");
5253 }
5254 }
5255
5256 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
5257 '/bin/bash');
5258 auth_group_write($auth_group_file, $group, $gid, $user);
5259
5260 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5261
5262 # Build up sqlite3 command to create users, groups tables and populate them
5263 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5264
5265 if (open(my $fh, "> $db_script")) {
5266 print $fh <<EOS;
5267 CREATE TABLE ftpsessions (
5268 user TEXT,
5269 ip_addr TEXT
5270 );
5271 EOS
5272
5273 unless (close($fh)) {
5274 die("Can't write $db_script: $!");
5275 }
5276
5277 } else {
5278 die("Can't open $db_script: $!");
5279 }
5280
5281 my $cmd = "sqlite3 $db_file < $db_script";
5282 build_db($cmd, $db_script);
5283
5284 # Make sure that, if we're running as root, the database file has
5285 # the permissions/privs set for use by proftpd
5286 if ($< == 0) {
5287 unless (chmod(0666, $db_file)) {
5288 die("Can't set perms on $db_file to 0666: $!");
5289 }
5290 }
5291
5292 my $config = {
5293 PidFile => $pid_file,
5294 ScoreboardFile => $scoreboard_file,
5295 SystemLog => $log_file,
5296
5297 AuthUserFile => $auth_user_file,
5298 AuthGroupFile => $auth_group_file,
5299 AuthOrder => 'mod_auth_file.c',
5300
5301 IfModules => {
5302 'mod_delay.c' => {
5303 DelayEngine => 'off',
5304 },
5305
5306 'mod_sql.c' => {
5307 SQLEngine => 'log',
5308 SQLBackend => 'sqlite3',
5309 SQLConnectInfo => $db_file,
5310 SQLLogFile => $log_file,
5311 SQLNamedQuery => 'logout FREEFORM "INSERT INTO ftpsessions (user, ip_addr) VALUES (\'%u\', \'%L\')"',
5312 SQLLog => 'EXIT logout',
5313 },
5314 },
5315 };
5316
5317 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5318
5319 # Open pipes, for use between the parent and child processes. Specifically,
5320 # the child will indicate when it's done with its test by writing a message
5321 # to the parent.
5322 my ($rfh, $wfh);
5323 unless (pipe($rfh, $wfh)) {
5324 die("Can't open pipe: $!");
5325 }
5326
5327 my $ex;
5328
5329 # Fork child
5330 $self->handle_sigchld();
5331 defined(my $pid = fork()) or die("Can't fork: $!");
5332 if ($pid) {
5333 eval {
5334 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5335
5336 $client->login($user, $passwd);
5337 $client->quit();
5338 };
5339
5340 if ($@) {
5341 $ex = $@;
5342 }
5343
5344 $wfh->print("done\n");
5345 $wfh->flush();
5346
5347 } else {
5348 eval { server_wait($config_file, $rfh) };
5349 if ($@) {
5350 warn($@);
5351 exit 1;
5352 }
5353
5354 exit 0;
5355 }
5356
5357 # Stop server
5358 server_stop($pid_file);
5359
5360 $self->assert_child_ok($pid);
5361
5362 if ($ex) {
5363 test_append_logfile($log_file, $ex);
5364 unlink($log_file);
5365
5366 die($ex);
5367 }
5368
5369 my $query = "SELECT user, ip_addr FROM ftpsessions WHERE user = \'$user\'";
5370 $cmd = "sqlite3 $db_file \"$query\"";
5371
5372 if ($ENV{TEST_VERBOSE}) {
5373 print STDERR "Executing sqlite3: $cmd\n";
5374 }
5375
5376 my $res = join('', `$cmd`);
5377 chomp($res);
5378
5379 my ($login, $ip_addr) = split(/\|/, $res);
5380
5381 my $expected;
5382
5383 $expected = $user;
5384 $self->assert($expected eq $login,
5385 test_msg("Expected '$expected', got '$login'"));
5386
5387 $expected = '127.0.0.1';
5388 $self->assert($expected eq $ip_addr,
5389 test_msg("Expected '$expected', got '$ip_addr'"));
5390
5391 unlink($log_file);
5392 }
5393
5394 sub sql_sqllog_exit_var_remote_port_bug4296 {
5395 my $self = shift;
5396 my $tmpdir = $self->{tmpdir};
5397 my $setup = test_setup($tmpdir, 'sqlite');
5398
5399 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5400
5401 # Build up sqlite3 command to create users, groups tables and populate them
5402 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5403
5404 if (open(my $fh, "> $db_script")) {
5405 print $fh <<EOS;
5406 CREATE TABLE ftpsessions (
5407 user TEXT,
5408 ip_addr TEXT,
5409 port NUMBER
5410 );
5411 EOS
5412
5413 unless (close($fh)) {
5414 die("Can't write $db_script: $!");
5415 }
5416
5417 } else {
5418 die("Can't open $db_script: $!");
5419 }
5420
5421 my $cmd = "sqlite3 $db_file < $db_script";
5422 build_db($cmd, $db_script);
5423
5424 # Make sure that, if we're running as root, the database file has
5425 # the permissions/privs set for use by proftpd
5426 if ($< == 0) {
5427 unless (chmod(0666, $db_file)) {
5428 die("Can't set perms on $db_file to 0666: $!");
5429 }
5430 }
5431
5432 my $config = {
5433 PidFile => $setup->{pid_file},
5434 ScoreboardFile => $setup->{scoreboard_file},
5435 SystemLog => $setup->{log_file},
5436 TraceLog => $setup->{log_file},
5437 Trace => 'jot:20 sql:20',
5438
5439 AuthUserFile => $setup->{auth_user_file},
5440 AuthGroupFile => $setup->{auth_group_file},
5441 AuthOrder => 'mod_auth_file.c',
5442
5443 IfModules => {
5444 'mod_delay.c' => {
5445 DelayEngine => 'off',
5446 },
5447
5448 'mod_sql.c' => {
5449 SQLEngine => 'log',
5450 SQLBackend => 'sqlite3',
5451 SQLConnectInfo => $db_file,
5452 SQLLogFile => $setup->{log_file},
5453 SQLNamedQuery => 'logout FREEFORM "INSERT INTO ftpsessions (user, ip_addr, port) VALUES (\'%u\', \'%L\', %{remote-port})"',
5454 SQLLog => 'EXIT logout',
5455 },
5456 },
5457 };
5458
5459 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
5460 $config);
5461
5462 # Open pipes, for use between the parent and child processes. Specifically,
5463 # the child will indicate when it's done with its test by writing a message
5464 # to the parent.
5465 my ($rfh, $wfh);
5466 unless (pipe($rfh, $wfh)) {
5467 die("Can't open pipe: $!");
5468 }
5469
5470 my $ex;
5471
5472 # Fork child
5473 $self->handle_sigchld();
5474 defined(my $pid = fork()) or die("Can't fork: $!");
5475 if ($pid) {
5476 eval {
5477 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5478 $client->login($setup->{user}, $setup->{passwd});
5479 $client->quit();
5480 };
5481 if ($@) {
5482 $ex = $@;
5483 }
5484
5485 $wfh->print("done\n");
5486 $wfh->flush();
5487
5488 } else {
5489 eval { server_wait($setup->{config_file}, $rfh) };
5490 if ($@) {
5491 warn($@);
5492 exit 1;
5493 }
5494
5495 exit 0;
5496 }
5497
5498 # Stop server
5499 server_stop($setup->{pid_file});
5500 $self->assert_child_ok($pid);
5501
5502 if ($ex) {
5503 test_cleanup($setup->{log_file}, $ex);
5504 }
5505
5506 eval {
5507 my $query = "SELECT * FROM ftpsessions where user = '$setup->{user}'";
5508 $cmd = "sqlite3 $db_file \"$query\"";
5509
5510 if ($ENV{TEST_VERBOSE}) {
5511 print STDERR "Executing sqlite3: $cmd\n";
5512 }
5513
5514 my $output = `$cmd`;
5515 my $res = join('', `$cmd`);
5516 chomp($res);
5517
5518 my ($login, $ip_addr, $remote_port) = split(/\|/, $res);
5519
5520 my $expected = $setup->{user};
5521 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
5522
5523 $expected = '127.0.0.1';
5524 $self->assert($expected eq $ip_addr,
5525 "Expected '$expected', got '$ip_addr'");
5526 };
5527 if ($@) {
5528 $ex = $@;
5529 }
5530
5531 test_cleanup($setup->{log_file}, $ex);
5532 }
5533
5534 sub get_locations {
5535 my $db_file = shift;
5536 my $where = shift;
5537
5538 my $sql = "SELECT user, ip_addr, dir FROM ftpsessions";
5539 if ($where) {
5540 $sql .= " WHERE $where";
5541 }
5542
5543 my $cmd = "sqlite3 $db_file \"$sql\"";
5544
5545 if ($ENV{TEST_VERBOSE}) {
5546 print STDERR "Executing sqlite3: $cmd\n";
5547 }
5548
5549 my $res = join('', `$cmd`);
5550 chomp($res);
5551
5552 if ($ENV{TEST_VERBOSE}) {
5553 print STDERR "Results: $res\n";
5554 }
5555
5556 # The default sqlite3 delimiter is '|'
5557 return split(/\|/, $res);
5558 }
5559
5560 sub sql_sqllog_var_d_bug3395 {
5561 my $self = shift;
5562 my $tmpdir = $self->{tmpdir};
5563 my $setup = test_setup($tmpdir, 'sqlite');
5564
5565 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
5566 mkpath($sub_dir);
5567
5568 # Make sure that, if we're running as root, that the home directory has
5569 # permissions/privs set for the account we create
5570 if ($< == 0) {
5571 unless (chmod(0755, $sub_dir)) {
5572 die("Can't set perms on $sub_dir to 0755: $!");
5573 }
5574
5575 unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
5576 die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
5577 }
5578 }
5579
5580 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5581
5582 # Build up sqlite3 command to create users, groups tables and populate them
5583 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5584
5585 if (open(my $fh, "> $db_script")) {
5586 print $fh <<EOS;
5587 CREATE TABLE ftpsessions (
5588 user TEXT,
5589 ip_addr TEXT,
5590 dir TEXT
5591 );
5592 EOS
5593
5594 unless (close($fh)) {
5595 die("Can't write $db_script: $!");
5596 }
5597
5598 } else {
5599 die("Can't open $db_script: $!");
5600 }
5601
5602 my $cmd = "sqlite3 $db_file < $db_script";
5603 build_db($cmd, $db_script);
5604
5605 # Make sure that, if we're running as root, the database file has
5606 # the permissions/privs set for use by proftpd
5607 if ($< == 0) {
5608 unless (chmod(0666, $db_file)) {
5609 die("Can't set perms on $db_file to 0666: $!");
5610 }
5611 }
5612
5613 my $config = {
5614 PidFile => $setup->{pid_file},
5615 ScoreboardFile => $setup->{scoreboard_file},
5616 SystemLog => $setup->{log_file},
5617 TraceLog => $setup->{log_file},
5618 Trace => 'jot:20 sql:20',
5619
5620 AuthUserFile => $setup->{auth_user_file},
5621 AuthGroupFile => $setup->{auth_group_file},
5622 AuthOrder => 'mod_auth_file.c',
5623
5624 IfModules => {
5625 'mod_delay.c' => {
5626 DelayEngine => 'off',
5627 },
5628
5629 'mod_sql.c' => {
5630 SQLEngine => 'log',
5631 SQLBackend => 'sqlite3',
5632 SQLConnectInfo => $db_file,
5633 SQLLogFile => $setup->{log_file},
5634 SQLNamedQuery => 'location FREEFORM "INSERT INTO ftpsessions (user, ip_addr, dir) VALUES (\'%u\', \'%L\', \'%d\')"',
5635 SQLLog => 'EXIT location',
5636 },
5637 },
5638 };
5639
5640 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
5641 $config);
5642
5643 # Open pipes, for use between the parent and child processes. Specifically,
5644 # the child will indicate when it's done with its test by writing a message
5645 # to the parent.
5646 my ($rfh, $wfh);
5647 unless (pipe($rfh, $wfh)) {
5648 die("Can't open pipe: $!");
5649 }
5650
5651 my $ex;
5652
5653 # Fork child
5654 $self->handle_sigchld();
5655 defined(my $pid = fork()) or die("Can't fork: $!");
5656 if ($pid) {
5657 eval {
5658 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5659 $client->login($setup->{user}, $setup->{passwd});
5660 $client->cwd('foo');
5661 $client->quit();
5662 };
5663 if ($@) {
5664 $ex = $@;
5665 }
5666
5667 $wfh->print("done\n");
5668 $wfh->flush();
5669
5670 } else {
5671 eval { server_wait($setup->{config_file}, $rfh) };
5672 if ($@) {
5673 warn($@);
5674 exit 1;
5675 }
5676
5677 exit 0;
5678 }
5679
5680 # Stop server
5681 server_stop($setup->{pid_file});
5682 $self->assert_child_ok($pid);
5683
5684 if ($ex) {
5685 test_cleanup($setup->{log_file}, $ex);
5686 }
5687
5688 eval {
5689 my ($login, $ip_addr, $dir) = get_locations($db_file, "user = \'$setup->{user}\'");
5690
5691 my $expected = $setup->{user};
5692 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
5693
5694 $expected = '127.0.0.1';
5695 $self->assert($expected eq $ip_addr,
5696 "Expected '$expected', got '$ip_addr'");
5697
5698 if ($^O eq 'darwin') {
5699 # MacOSX-specific hack
5700 $sub_dir = '/private' . $sub_dir;
5701 }
5702
5703 $expected = $sub_dir;
5704 $self->assert($expected eq $dir, "Expected '$expected', got '$dir'");
5705 };
5706 if ($@) {
5707 $ex = $@;
5708 }
5709
5710 test_cleanup($setup->{log_file}, $ex);
5711 }
5712
5713 sub sql_sqllog_var_d_chroot_bug3395 {
5714 my $self = shift;
5715 my $tmpdir = $self->{tmpdir};
5716
5717 my $config_file = "$tmpdir/sqlite.conf";
5718 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
5719 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
5720
5721 my $log_file = test_get_logfile();
5722
5723 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
5724 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
5725
5726 my $user = 'proftpd';
5727 my $passwd = 'test';
5728 my $group = 'ftpd';
5729 my $home_dir = File::Spec->rel2abs($tmpdir);
5730 my $uid = 500;
5731 my $gid = 500;
5732
5733 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
5734 mkpath($sub_dir);
5735
5736 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
5737 '/bin/bash');
5738 auth_group_write($auth_group_file, $group, $gid, $user);
5739
5740 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5741
5742 # Build up sqlite3 command to create users, groups tables and populate them
5743 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5744
5745 if (open(my $fh, "> $db_script")) {
5746 print $fh <<EOS;
5747 CREATE TABLE ftpsessions (
5748 user TEXT,
5749 ip_addr TEXT,
5750 dir TEXT
5751 );
5752 EOS
5753
5754 unless (close($fh)) {
5755 die("Can't write $db_script: $!");
5756 }
5757
5758 } else {
5759 die("Can't open $db_script: $!");
5760 }
5761
5762 my $cmd = "sqlite3 $db_file < $db_script";
5763 build_db($cmd, $db_script);
5764
5765 # Make sure that, if we're running as root, that the home directory has
5766 # permissions/privs set for the account we create
5767 if ($< == 0) {
5768 unless (chmod(0755, $home_dir, $sub_dir)) {
5769 die("Can't set perms on $home_dir to 0755: $!");
5770 }
5771
5772 unless (chown($uid, $gid, $home_dir, $sub_dir)) {
5773 die("Can't set owner of $home_dir to $uid/$gid: $!");
5774 }
5775
5776 unless (chmod(0666, $db_file)) {
5777 die("Can't set perms on $db_file to 0666: $!");
5778 }
5779 }
5780
5781 my $config = {
5782 PidFile => $pid_file,
5783 ScoreboardFile => $scoreboard_file,
5784 SystemLog => $log_file,
5785
5786 AuthUserFile => $auth_user_file,
5787 AuthGroupFile => $auth_group_file,
5788 AuthOrder => 'mod_auth_file.c',
5789
5790 IfModules => {
5791 'mod_delay.c' => {
5792 DelayEngine => 'off',
5793 },
5794
5795 'mod_sql.c' => {
5796 SQLEngine => 'log',
5797 SQLBackend => 'sqlite3',
5798 SQLConnectInfo => "$db_file foo bar PERCONNECTION",
5799 SQLLogFile => $log_file,
5800 SQLNamedQuery => 'location FREEFORM "INSERT INTO ftpsessions (user, ip_addr, dir) VALUES (\'%u\', \'%L\', \'%d\')"',
5801 SQLLog => 'EXIT location',
5802 },
5803 },
5804 };
5805
5806 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5807
5808 # Open pipes, for use between the parent and child processes. Specifically,
5809 # the child will indicate when it's done with its test by writing a message
5810 # to the parent.
5811 my ($rfh, $wfh);
5812 unless (pipe($rfh, $wfh)) {
5813 die("Can't open pipe: $!");
5814 }
5815
5816 my $ex;
5817
5818 # Fork child
5819 $self->handle_sigchld();
5820 defined(my $pid = fork()) or die("Can't fork: $!");
5821 if ($pid) {
5822 eval {
5823 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
5824 $client->login($user, $passwd);
5825 $client->cwd('foo');
5826 $client->quit();
5827 };
5828
5829 if ($@) {
5830 $ex = $@;
5831 }
5832
5833 $wfh->print("done\n");
5834 $wfh->flush();
5835
5836 } else {
5837 eval { server_wait($config_file, $rfh) };
5838 if ($@) {
5839 warn($@);
5840 exit 1;
5841 }
5842
5843 exit 0;
5844 }
5845
5846 # Stop server
5847 server_stop($pid_file);
5848
5849 $self->assert_child_ok($pid);
5850
5851 if ($ex) {
5852 test_append_logfile($log_file, $ex);
5853 unlink($log_file);
5854
5855 die($ex);
5856 }
5857
5858 my ($login, $ip_addr, $dir) = get_locations($db_file, "user = \'$user\'");
5859
5860 my $expected;
5861
5862 $expected = $user;
5863 $self->assert($expected eq $login,
5864 test_msg("Expected '$expected', got '$login'"));
5865
5866 $expected = '127.0.0.1';
5867 $self->assert($expected eq $ip_addr,
5868 test_msg("Expected '$expected', got '$ip_addr'"));
5869
5870 $expected = $sub_dir;
5871 $self->assert($expected eq $dir,
5872 test_msg("Expected '$expected', got '$dir'"));
5873
5874 unlink($log_file);
5875 }
5876
5877 sub get_ids {
5878 my $db_file = shift;
5879 my $where = shift;
5880
5881 my $sql = "SELECT user, ip_addr, uid, gid FROM ftpsessions";
5882 if ($where) {
5883 $sql .= " WHERE $where";
5884 }
5885
5886 my $cmd = "sqlite3 $db_file \"$sql\"";
5887
5888 if ($ENV{TEST_VERBOSE}) {
5889 print STDERR "Executing sqlite3: $cmd\n";
5890 }
5891
5892 my $res = join('', `$cmd`);
5893 chomp($res);
5894
5895 # The default sqlite3 delimiter is '|'
5896 return split(/\|/, $res);
5897 }
5898
5899 sub sql_sqllog_var_uid_gid_bug3390 {
5900 my $self = shift;
5901 my $tmpdir = $self->{tmpdir};
5902
5903 my $config_file = "$tmpdir/sqlite.conf";
5904 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
5905 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
5906
5907 my $log_file = test_get_logfile();
5908
5909 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
5910 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
5911
5912 my $user = 'proftpd';
5913 my $passwd = 'test';
5914 my $group = 'ftpd';
5915 my $home_dir = File::Spec->rel2abs($tmpdir);
5916 my $uid = 500;
5917 my $gid = 500;
5918
5919 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
5920 mkpath($sub_dir);
5921
5922 # Make sure that, if we're running as root, that the home directory has
5923 # permissions/privs set for the account we create
5924 if ($< == 0) {
5925 unless (chmod(0755, $home_dir, $sub_dir)) {
5926 die("Can't set perms on $home_dir to 0755: $!");
5927 }
5928
5929 unless (chown($uid, $gid, $home_dir, $sub_dir)) {
5930 die("Can't set owner of $home_dir to $uid/$gid: $!");
5931 }
5932 }
5933
5934 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
5935 '/bin/bash');
5936 auth_group_write($auth_group_file, $group, $gid, $user);
5937
5938 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
5939
5940 # Build up sqlite3 command to create users, groups tables and populate them
5941 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
5942
5943 if (open(my $fh, "> $db_script")) {
5944 print $fh <<EOS;
5945 CREATE TABLE ftpsessions (
5946 user TEXT,
5947 ip_addr TEXT,
5948 uid NUMBER,
5949 gid NUMBER
5950 );
5951 EOS
5952
5953 unless (close($fh)) {
5954 die("Can't write $db_script: $!");
5955 }
5956
5957 } else {
5958 die("Can't open $db_script: $!");
5959 }
5960
5961 my $cmd = "sqlite3 $db_file < $db_script";
5962 build_db($cmd, $db_script);
5963
5964 # Make sure that, if we're running as root, the database file has
5965 # the permissions/privs set for use by proftpd
5966 if ($< == 0) {
5967 unless (chmod(0666, $db_file)) {
5968 die("Can't set perms on $db_file to 0666: $!");
5969 }
5970 }
5971
5972 my $config = {
5973 PidFile => $pid_file,
5974 ScoreboardFile => $scoreboard_file,
5975 SystemLog => $log_file,
5976
5977 AuthUserFile => $auth_user_file,
5978 AuthGroupFile => $auth_group_file,
5979 AuthOrder => 'mod_auth_file.c',
5980
5981 IfModules => {
5982 'mod_delay.c' => {
5983 DelayEngine => 'off',
5984 },
5985
5986 'mod_sql.c' => {
5987 SQLEngine => 'log',
5988 SQLBackend => 'sqlite3',
5989 SQLConnectInfo => "$db_file foo bar PERCONNECTION",
5990 SQLLogFile => $log_file,
5991 SQLNamedQuery => 'location FREEFORM "INSERT INTO ftpsessions (user, ip_addr, uid, gid) VALUES (\'%u\', \'%L\', %{uid}, %{gid})"',
5992 SQLLog => 'PASS location',
5993 },
5994 },
5995 };
5996
5997 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5998
5999 # Open pipes, for use between the parent and child processes. Specifically,
6000 # the child will indicate when it's done with its test by writing a message
6001 # to the parent.
6002 my ($rfh, $wfh);
6003 unless (pipe($rfh, $wfh)) {
6004 die("Can't open pipe: $!");
6005 }
6006
6007 my $ex;
6008
6009 # Fork child
6010 $self->handle_sigchld();
6011 defined(my $pid = fork()) or die("Can't fork: $!");
6012 if ($pid) {
6013 eval {
6014 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6015 $client->login($user, $passwd);
6016 $client->cwd('foo');
6017 $client->quit();
6018 };
6019
6020 if ($@) {
6021 $ex = $@;
6022 }
6023
6024 $wfh->print("done\n");
6025 $wfh->flush();
6026
6027 } else {
6028 eval { server_wait($config_file, $rfh) };
6029 if ($@) {
6030 warn($@);
6031 exit 1;
6032 }
6033
6034 exit 0;
6035 }
6036
6037 # Stop server
6038 server_stop($pid_file);
6039
6040 $self->assert_child_ok($pid);
6041
6042 if ($ex) {
6043 test_append_logfile($log_file, $ex);
6044 unlink($log_file);
6045
6046 die($ex);
6047 }
6048
6049 my ($login, $ip_addr, $sql_uid, $sql_gid) = get_ids($db_file, "user = \'$user\'");
6050
6051 my $expected;
6052
6053 $expected = $user;
6054 $self->assert($expected eq $login,
6055 test_msg("Expected '$expected', got '$login'"));
6056
6057 $expected = '127.0.0.1';
6058 $self->assert($expected eq $ip_addr,
6059 test_msg("Expected '$expected', got '$ip_addr'"));
6060
6061 $expected = $uid;
6062 $self->assert($expected == $sql_uid,
6063 test_msg("Expected $expected, got $sql_uid"));
6064
6065 $expected = $gid;
6066 $self->assert($expected == $sql_gid,
6067 test_msg("Expected $expected, got $sql_gid"));
6068
6069 unlink($log_file);
6070 }
6071
6072 sub sql_sqlite_auth_type_backend_bug3511 {
6073 my $self = shift;
6074 my $tmpdir = $self->{tmpdir};
6075 my $setup = test_setup($tmpdir, 'sqlite');
6076
6077 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6078
6079 my $config = {
6080 PidFile => $setup->{pid_file},
6081 ScoreboardFile => $setup->{scoreboard_file},
6082 SystemLog => $setup->{log_file},
6083
6084 IfModules => {
6085 'mod_delay.c' => {
6086 DelayEngine => 'off',
6087 },
6088
6089 'mod_sql.c' => {
6090 SQLAuthTypes => 'backend',
6091 SQLBackend => 'sqlite3',
6092 SQLConnectInfo => $db_file,
6093 SQLLogFile => $setup->{log_file},
6094 },
6095 },
6096 };
6097
6098 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
6099 $config);
6100
6101 # As of Bug#4281, the mod_sql module no longer supports the "Backend"
6102 # SQLAuthType itself. Which means that attempting to use it here, with
6103 # mod_sql_sqlite, will cause the server to not start up at all.
6104
6105 my $ex;
6106
6107 # This should silently fail.
6108 server_start($setup->{config_file});
6109
6110 # This is where we detect the actual problem.
6111 eval { server_stop($setup->{pid_file}) };
6112 unless ($@) {
6113 $ex = "Server start with bad config unexpectedly";
6114 }
6115
6116 test_cleanup($setup->{log_file}, $ex);
6117 }
6118
6119 sub sql_sqllog_pass_ok_var_s_bug3528 {
6120 my $self = shift;
6121 my $tmpdir = $self->{tmpdir};
6122
6123 my $config_file = "$tmpdir/sqlite.conf";
6124 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
6125 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
6126
6127 my $log_file = test_get_logfile();
6128
6129 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
6130 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
6131
6132 my $user = 'proftpd';
6133 my $passwd = 'test';
6134 my $group = 'ftpd';
6135 my $home_dir = File::Spec->rel2abs($tmpdir);
6136 my $uid = 500;
6137 my $gid = 500;
6138
6139 # Make sure that, if we're running as root, that the home directory has
6140 # permissions/privs set for the account we create
6141 if ($< == 0) {
6142 unless (chmod(0755, $home_dir)) {
6143 die("Can't set perms on $home_dir to 0755: $!");
6144 }
6145
6146 unless (chown($uid, $gid, $home_dir)) {
6147 die("Can't set owner of $home_dir to $uid/$gid: $!");
6148 }
6149 }
6150
6151 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
6152 '/bin/bash');
6153 auth_group_write($auth_group_file, $group, $gid, $user);
6154
6155 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6156
6157 # Build up sqlite3 command to create users, groups tables and populate them
6158 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6159
6160 if (open(my $fh, "> $db_script")) {
6161 print $fh <<EOS;
6162 CREATE TABLE ftpsessions (
6163 user TEXT,
6164 resp_code TEXT,
6165 resp_mesg TEXT
6166 );
6167 EOS
6168
6169 unless (close($fh)) {
6170 die("Can't write $db_script: $!");
6171 }
6172
6173 } else {
6174 die("Can't open $db_script: $!");
6175 }
6176
6177 my $cmd = "sqlite3 $db_file < $db_script";
6178 build_db($cmd, $db_script);
6179
6180 # Make sure that, if we're running as root, the database file has
6181 # the permissions/privs set for use by proftpd
6182 if ($< == 0) {
6183 unless (chmod(0666, $db_file)) {
6184 die("Can't set perms on $db_file to 0666: $!");
6185 }
6186 }
6187
6188 my $config = {
6189 PidFile => $pid_file,
6190 ScoreboardFile => $scoreboard_file,
6191 SystemLog => $log_file,
6192
6193 AuthUserFile => $auth_user_file,
6194 AuthGroupFile => $auth_group_file,
6195 AuthOrder => 'mod_auth_file.c',
6196
6197 IfModules => {
6198 'mod_delay.c' => {
6199 DelayEngine => 'off',
6200 },
6201
6202 'mod_sql.c' => {
6203 SQLEngine => 'log',
6204 SQLBackend => 'sqlite3',
6205 SQLConnectInfo => $db_file,
6206 SQLLogFile => $log_file,
6207 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, resp_code, resp_mesg) VALUES (\'%u\', \'%s\', \'%S\')"',
6208 SQLLog => 'PASS,ERR_PASS info',
6209 },
6210 },
6211 };
6212
6213 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6214
6215 # Open pipes, for use between the parent and child processes. Specifically,
6216 # the child will indicate when it's done with its test by writing a message
6217 # to the parent.
6218 my ($rfh, $wfh);
6219 unless (pipe($rfh, $wfh)) {
6220 die("Can't open pipe: $!");
6221 }
6222
6223 my $ex;
6224
6225 # Fork child
6226 $self->handle_sigchld();
6227 defined(my $pid = fork()) or die("Can't fork: $!");
6228 if ($pid) {
6229 eval {
6230 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6231 $client->login($user, $passwd);
6232 $client->quit();
6233 };
6234
6235 if ($@) {
6236 $ex = $@;
6237 }
6238
6239 $wfh->print("done\n");
6240 $wfh->flush();
6241
6242 } else {
6243 eval { server_wait($config_file, $rfh) };
6244 if ($@) {
6245 warn($@);
6246 exit 1;
6247 }
6248
6249 exit 0;
6250 }
6251
6252 # Stop server
6253 server_stop($pid_file);
6254
6255 $self->assert_child_ok($pid);
6256
6257 if ($ex) {
6258 test_append_logfile($log_file, $ex);
6259 unlink($log_file);
6260
6261 die($ex);
6262 }
6263
6264 my ($login, $resp_code, $resp_mesg) = get_resp_mesgs($db_file,
6265 undef, "user, resp_code, resp_mesg");
6266
6267 my $expected;
6268
6269 $expected = 230;
6270 $self->assert($expected eq $resp_code,
6271 test_msg("Expected '$expected', got '$resp_code'"));
6272
6273 $expected = "User $user logged in";
6274 $self->assert($expected eq $resp_mesg,
6275 test_msg("Expected '$expected', got '$resp_mesg'"));
6276
6277 unlink($log_file);
6278 }
6279
6280 sub sql_sqllog_pass_failed_var_s_bug3528 {
6281 my $self = shift;
6282 my $tmpdir = $self->{tmpdir};
6283
6284 my $config_file = "$tmpdir/sqlite.conf";
6285 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
6286 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
6287
6288 my $log_file = test_get_logfile();
6289
6290 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
6291 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
6292
6293 my $user = 'proftpd';
6294 my $passwd = 'test';
6295 my $group = 'ftpd';
6296 my $home_dir = File::Spec->rel2abs($tmpdir);
6297 my $uid = 500;
6298 my $gid = 500;
6299
6300 # Make sure that, if we're running as root, that the home directory has
6301 # permissions/privs set for the account we create
6302 if ($< == 0) {
6303 unless (chmod(0755, $home_dir)) {
6304 die("Can't set perms on $home_dir to 0755: $!");
6305 }
6306
6307 unless (chown($uid, $gid, $home_dir)) {
6308 die("Can't set owner of $home_dir to $uid/$gid: $!");
6309 }
6310 }
6311
6312 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
6313 '/bin/bash');
6314 auth_group_write($auth_group_file, $group, $gid, $user);
6315
6316 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6317
6318 # Build up sqlite3 command to create users, groups tables and populate them
6319 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6320
6321 if (open(my $fh, "> $db_script")) {
6322 print $fh <<EOS;
6323 CREATE TABLE ftpsessions (
6324 user TEXT,
6325 resp_code TEXT,
6326 resp_mesg TEXT
6327 );
6328 EOS
6329
6330 unless (close($fh)) {
6331 die("Can't write $db_script: $!");
6332 }
6333
6334 } else {
6335 die("Can't open $db_script: $!");
6336 }
6337
6338 my $cmd = "sqlite3 $db_file < $db_script";
6339 build_db($cmd, $db_script);
6340
6341 # Make sure that, if we're running as root, the database file has
6342 # the permissions/privs set for use by proftpd
6343 if ($< == 0) {
6344 unless (chmod(0666, $db_file)) {
6345 die("Can't set perms on $db_file to 0666: $!");
6346 }
6347 }
6348
6349 my $config = {
6350 PidFile => $pid_file,
6351 ScoreboardFile => $scoreboard_file,
6352 SystemLog => $log_file,
6353
6354 AuthUserFile => $auth_user_file,
6355 AuthGroupFile => $auth_group_file,
6356 AuthOrder => 'mod_auth_file.c',
6357
6358 IfModules => {
6359 'mod_delay.c' => {
6360 DelayEngine => 'off',
6361 },
6362
6363 'mod_sql.c' => {
6364 SQLEngine => 'log',
6365 SQLBackend => 'sqlite3',
6366 SQLConnectInfo => $db_file,
6367 SQLLogFile => $log_file,
6368 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, resp_code, resp_mesg) VALUES (\'%u\', \'%s\', \'%S\')"',
6369 SQLLog => 'PASS,ERR_PASS info',
6370 },
6371 },
6372 };
6373
6374 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6375
6376 # Open pipes, for use between the parent and child processes. Specifically,
6377 # the child will indicate when it's done with its test by writing a message
6378 # to the parent.
6379 my ($rfh, $wfh);
6380 unless (pipe($rfh, $wfh)) {
6381 die("Can't open pipe: $!");
6382 }
6383
6384 my $ex;
6385
6386 # Fork child
6387 $self->handle_sigchld();
6388 defined(my $pid = fork()) or die("Can't fork: $!");
6389 if ($pid) {
6390 eval {
6391 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6392 eval { $client->login($user, 'foobar') };
6393 unless ($@) {
6394 die("Login succeeded unexpectedly");
6395 }
6396
6397 $client->quit();
6398 };
6399
6400 if ($@) {
6401 $ex = $@;
6402 }
6403
6404 $wfh->print("done\n");
6405 $wfh->flush();
6406
6407 } else {
6408 eval { server_wait($config_file, $rfh) };
6409 if ($@) {
6410 warn($@);
6411 exit 1;
6412 }
6413
6414 exit 0;
6415 }
6416
6417 # Stop server
6418 server_stop($pid_file);
6419
6420 $self->assert_child_ok($pid);
6421
6422 if ($ex) {
6423 test_append_logfile($log_file, $ex);
6424 unlink($log_file);
6425
6426 die($ex);
6427 }
6428
6429 my ($login, $resp_code, $resp_mesg) = get_resp_mesgs($db_file,
6430 undef, "user, resp_code, resp_mesg");
6431
6432 my $expected;
6433
6434 $expected = 530;
6435 $self->assert($expected eq $resp_code,
6436 test_msg("Expected '$expected', got '$resp_code'"));
6437
6438 $expected = "Login incorrect.";
6439 $self->assert($expected eq $resp_mesg,
6440 test_msg("Expected '$expected', got '$resp_mesg'"));
6441
6442 unlink($log_file);
6443 }
6444
6445 sub sql_sqllog_pass_failed_bug4301 {
6446 my $self = shift;
6447 my $tmpdir = $self->{tmpdir};
6448
6449 my $config_file = "$tmpdir/sqlite.conf";
6450 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
6451 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
6452
6453 my $log_file = test_get_logfile();
6454
6455 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
6456 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
6457
6458 my $user = 'proftpd';
6459 my $passwd = 'test';
6460 my $group = 'ftpd';
6461 my $home_dir = File::Spec->rel2abs($tmpdir);
6462 my $uid = 500;
6463 my $gid = 500;
6464
6465 # Make sure that, if we're running as root, that the home directory has
6466 # permissions/privs set for the account we create
6467 if ($< == 0) {
6468 unless (chmod(0755, $home_dir)) {
6469 die("Can't set perms on $home_dir to 0755: $!");
6470 }
6471
6472 unless (chown($uid, $gid, $home_dir)) {
6473 die("Can't set owner of $home_dir to $uid/$gid: $!");
6474 }
6475 }
6476
6477 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
6478 '/bin/bash');
6479 auth_group_write($auth_group_file, $group, $gid, $user);
6480
6481 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6482
6483 # Build up sqlite3 command to create users, groups tables and populate them
6484 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6485
6486 if (open(my $fh, "> $db_script")) {
6487 print $fh <<EOS;
6488 CREATE TABLE ftpsessions (
6489 user TEXT,
6490 resp_code TEXT,
6491 resp_mesg TEXT
6492 );
6493 EOS
6494
6495 unless (close($fh)) {
6496 die("Can't write $db_script: $!");
6497 }
6498
6499 } else {
6500 die("Can't open $db_script: $!");
6501 }
6502
6503 my $cmd = "sqlite3 $db_file < $db_script";
6504 build_db($cmd, $db_script);
6505
6506 # Make sure that, if we're running as root, the database file has
6507 # the permissions/privs set for use by proftpd
6508 if ($< == 0) {
6509 unless (chmod(0666, $db_file)) {
6510 die("Can't set perms on $db_file to 0666: $!");
6511 }
6512 }
6513
6514 my $config = {
6515 PidFile => $pid_file,
6516 ScoreboardFile => $scoreboard_file,
6517 SystemLog => $log_file,
6518
6519 AuthUserFile => $auth_user_file,
6520 AuthGroupFile => $auth_group_file,
6521 AuthOrder => 'mod_auth_file.c',
6522
6523 IfModules => {
6524 'mod_delay.c' => {
6525 DelayEngine => 'off',
6526 },
6527
6528 'mod_sql.c' => {
6529 SQLEngine => 'log',
6530 SQLBackend => 'sqlite3',
6531 SQLConnectInfo => $db_file,
6532 SQLLogFile => $log_file,
6533 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, resp_code, resp_mesg) VALUES (\'%u\', \'%s\', \'%S\')"',
6534 SQLLog => 'PASS info',
6535 },
6536 },
6537 };
6538
6539 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6540
6541 # Open pipes, for use between the parent and child processes. Specifically,
6542 # the child will indicate when it's done with its test by writing a message
6543 # to the parent.
6544 my ($rfh, $wfh);
6545 unless (pipe($rfh, $wfh)) {
6546 die("Can't open pipe: $!");
6547 }
6548
6549 my $ex;
6550
6551 # Fork child
6552 $self->handle_sigchld();
6553 defined(my $pid = fork()) or die("Can't fork: $!");
6554 if ($pid) {
6555 eval {
6556 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6557 $client->user($user);
6558 eval { $client->pass('') };
6559 unless ($@) {
6560 die("PASS succeeded unexpectedly");
6561 }
6562
6563 $client->quit();
6564 };
6565
6566 if ($@) {
6567 $ex = $@;
6568 }
6569
6570 $wfh->print("done\n");
6571 $wfh->flush();
6572
6573 } else {
6574 eval { server_wait($config_file, $rfh) };
6575 if ($@) {
6576 warn($@);
6577 exit 1;
6578 }
6579
6580 exit 0;
6581 }
6582
6583 # Stop server
6584 server_stop($pid_file);
6585 $self->assert_child_ok($pid);
6586
6587 if ($ex) {
6588 test_append_logfile($log_file, $ex);
6589 unlink($log_file);
6590
6591 die($ex);
6592 }
6593
6594 # We want to ensure/assert that a failed PASS command does NOT cause
6595 # the SQLLog directive to be triggered (Bug#4301).
6596 eval {
6597 my ($login, $resp_code, $resp_mesg) = get_resp_mesgs($db_file,
6598 undef, "user, resp_code, resp_mesg");
6599 $self->assert(!defined($resp_code), "Expected undef, got $resp_code");
6600 $self->assert(!defined($resp_mesg), "Expected undef, got '$resp_mesg'");
6601 };
6602 if ($@) {
6603 $ex = $@;
6604 }
6605
6606 if ($ex) {
6607 test_append_logfile($log_file, $ex);
6608 unlink($log_file);
6609
6610 die($ex);
6611 }
6612
6613 unlink($log_file);
6614 }
6615
6616 sub sql_sqlshowinfo_pass_bug3423 {
6617 my $self = shift;
6618 my $tmpdir = $self->{tmpdir};
6619 my $setup = test_setup($tmpdir, 'sqlite');
6620
6621 my $lastdir = '/path/to/lastdir';
6622
6623 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6624
6625 # Build up sqlite3 command to create users, groups tables and populate them
6626 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6627
6628 if (open(my $fh, "> $db_script")) {
6629 print $fh <<EOS;
6630 CREATE TABLE users (
6631 userid TEXT,
6632 passwd TEXT,
6633 uid INTEGER,
6634 gid INTEGER,
6635 homedir TEXT,
6636 shell TEXT,
6637 lastdir TEXT
6638 );
6639 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, lastdir) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash', '$lastdir');
6640
6641 CREATE TABLE groups (
6642 groupname TEXT,
6643 gid INTEGER,
6644 members TEXT
6645 );
6646 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
6647 EOS
6648
6649 unless (close($fh)) {
6650 die("Can't write $db_script: $!");
6651 }
6652
6653 } else {
6654 die("Can't open $db_script: $!");
6655 }
6656
6657 my $cmd = "sqlite3 $db_file < $db_script";
6658 build_db($cmd, $db_script);
6659
6660 # Make sure that, if we're running as root, the database file has
6661 # the permissions/privs set for use by proftpd
6662 if ($< == 0) {
6663 unless (chmod(0666, $db_file)) {
6664 die("Can't set perms on $db_file to 0666: $!");
6665 }
6666 }
6667
6668 my $config = {
6669 PidFile => $setup->{pid_file},
6670 ScoreboardFile => $setup->{scoreboard_file},
6671 SystemLog => $setup->{log_file},
6672 TraceLog => $setup->{log_file},
6673 Trace => 'jot:20 sql:20',
6674
6675 IfModules => {
6676 'mod_delay.c' => {
6677 DelayEngine => 'off',
6678 },
6679
6680 'mod_sql.c' => [
6681 'AuthOrder mod_sql.c',
6682
6683 'SQLAuthenticate users groups',
6684 'SQLAuthTypes plaintext',
6685 'SQLBackend sqlite3',
6686 "SQLConnectInfo $db_file",
6687 "SQLLogFile $setup->{log_file}",
6688 'SQLMinID 100',
6689
6690 'SQLNamedQuery lastdir SELECT "lastdir FROM users WHERE userid = \'%u\'"',
6691
6692 # Configure the equivalent of a multiline DisplayLogin file
6693 # using the SQLShowInfo directive
6694 'SQLShowInfo PASS 230 " "',
6695 'SQLShowInfo PASS 230 "Greetings, %u"',
6696 'SQLShowInfo PASS 230 "Last directory: \"%{lastdir}\""',
6697 'SQLShowInfo PASS 230 "-"',
6698 ],
6699 },
6700 };
6701
6702 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
6703 $config);
6704
6705 # Open pipes, for use between the parent and child processes. Specifically,
6706 # the child will indicate when it's done with its test by writing a message
6707 # to the parent.
6708 my ($rfh, $wfh);
6709 unless (pipe($rfh, $wfh)) {
6710 die("Can't open pipe: $!");
6711 }
6712
6713 my $ex;
6714
6715 # Fork child
6716 $self->handle_sigchld();
6717 defined(my $pid = fork()) or die("Can't fork: $!");
6718 if ($pid) {
6719 eval {
6720 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6721 $client->login($setup->{user}, $setup->{passwd});
6722
6723 my $resp_msgs = $client->response_msgs();
6724 my $nmsgs = scalar(@$resp_msgs);
6725
6726 my $expected = 5;
6727 $self->assert($expected == $nmsgs, "Expected $expected, got $nmsgs");
6728
6729 $expected = " ";
6730 $self->assert($expected eq $resp_msgs->[0],
6731 "Expected '$expected', got '$resp_msgs->[0]'");
6732
6733 $expected = " Greetings, $setup->{user}";
6734 $self->assert($expected eq $resp_msgs->[1],
6735 "Expected '$expected', got '$resp_msgs->[1]'");
6736
6737 $expected = " Last directory: \"$lastdir\"";
6738 $self->assert($expected eq $resp_msgs->[2],
6739 "Expected '$expected', got '$resp_msgs->[2]'");
6740
6741 $expected = " -";
6742 $self->assert($expected eq $resp_msgs->[3],
6743 "Expected '$expected', got '$resp_msgs->[3]'");
6744
6745 $expected = "User $setup->{user} logged in";
6746 $self->assert($expected eq $resp_msgs->[4],
6747 "Expected '$expected', got '$resp_msgs->[4]'");
6748
6749 $client->quit();
6750 };
6751 if ($@) {
6752 $ex = $@;
6753 }
6754
6755 $wfh->print("done\n");
6756 $wfh->flush();
6757
6758 } else {
6759 eval { server_wait($setup->{config_file}, $rfh) };
6760 if ($@) {
6761 warn($@);
6762 exit 1;
6763 }
6764
6765 exit 0;
6766 }
6767
6768 # Stop server
6769 server_stop($setup->{pid_file});
6770 $self->assert_child_ok($pid);
6771
6772 test_cleanup($setup->{log_file}, $ex);
6773 }
6774
6775 sub sql_sqlshowinfo_list_bug3423 {
6776 my $self = shift;
6777 my $tmpdir = $self->{tmpdir};
6778 my $setup = test_setup($tmpdir, 'sqlite');
6779
6780 my $lastdir = '/path/to/lastdir';
6781
6782 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6783
6784 # Build up sqlite3 command to create users, groups tables and populate them
6785 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6786
6787 if (open(my $fh, "> $db_script")) {
6788 print $fh <<EOS;
6789 CREATE TABLE users (
6790 userid TEXT,
6791 passwd TEXT,
6792 uid INTEGER,
6793 gid INTEGER,
6794 homedir TEXT,
6795 shell TEXT,
6796 lastdir TEXT
6797 );
6798 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, lastdir) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash', '$lastdir');
6799
6800 CREATE TABLE groups (
6801 groupname TEXT,
6802 gid INTEGER,
6803 members TEXT
6804 );
6805 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
6806 EOS
6807
6808 unless (close($fh)) {
6809 die("Can't write $db_script: $!");
6810 }
6811
6812 } else {
6813 die("Can't open $db_script: $!");
6814 }
6815
6816 my $cmd = "sqlite3 $db_file < $db_script";
6817 build_db($cmd, $db_script);
6818
6819 # Make sure that, if we're running as root, the database file has
6820 # the permissions/privs set for use by proftpd
6821 if ($< == 0) {
6822 unless (chmod(0666, $db_file)) {
6823 die("Can't set perms on $db_file to 0666: $!");
6824 }
6825 }
6826
6827 my $config = {
6828 PidFile => $setup->{pid_file},
6829 ScoreboardFile => $setup->{scoreboard_file},
6830 SystemLog => $setup->{log_file},
6831 TraceLog => $setup->{log_file},
6832 Trace => 'jot:20 sql:20',
6833
6834 IfModules => {
6835 'mod_delay.c' => {
6836 DelayEngine => 'off',
6837 },
6838
6839 'mod_sql.c' => [
6840 'AuthOrder mod_sql.c',
6841
6842 'SQLAuthenticate users groups',
6843 'SQLAuthTypes plaintext',
6844 'SQLBackend sqlite3',
6845 "SQLConnectInfo $db_file",
6846 "SQLLogFile $setup->{log_file}",
6847 'SQLMinID 100',
6848
6849 # Configure the equivalent of a multiline DisplayLogin file
6850 # using the SQLShowInfo directive
6851
6852 'SQLShowInfo LIST 226 "Get that directory listing OK, %u?"',
6853 'SQLShowInfo LIST 226 "-"',
6854 ],
6855 },
6856 };
6857
6858 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
6859 $config);
6860
6861 # Open pipes, for use between the parent and child processes. Specifically,
6862 # the child will indicate when it's done with its test by writing a message
6863 # to the parent.
6864 my ($rfh, $wfh);
6865 unless (pipe($rfh, $wfh)) {
6866 die("Can't open pipe: $!");
6867 }
6868
6869 my $ex;
6870
6871 # Fork child
6872 $self->handle_sigchld();
6873 defined(my $pid = fork()) or die("Can't fork: $!");
6874 if ($pid) {
6875 eval {
6876 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
6877 $client->login($setup->{user}, $setup->{passwd});
6878
6879 my $resp_code = $client->response_code();
6880 my $resp_msg = $client->response_msg();
6881
6882 my $expected = 230;
6883 $self->assert($expected == $resp_code,
6884 "Expected response code $expected, got $resp_code");
6885
6886 $expected = "User $setup->{user} logged in";
6887 $self->assert($expected eq $resp_msg,
6888 "Expected response message '$expected', got '$resp_msg'");
6889
6890 my $conn = $client->list_raw();
6891 unless ($conn) {
6892 die("LIST failed unexpectedly: " . $client->response_code() . " " .
6893 $client->response_msg());
6894 }
6895
6896 my $buf;
6897 $conn->read($buf, 8192, 25);
6898 eval { $conn->close() };
6899
6900 $resp_code = $client->response_code();
6901
6902 $expected = 226;
6903 $self->assert($expected == $resp_code,
6904 "Expected response code $expected, got $resp_code");
6905
6906 my $resp_msgs = $client->response_msgs();
6907 my $nmsgs = scalar(@$resp_msgs);
6908
6909 $expected = 4;
6910 $self->assert($expected == $nmsgs, "Expected $expected, got $nmsgs");
6911
6912 $expected = "Opening ASCII mode data connection for file list";
6913 $self->assert($expected eq $resp_msgs->[0],
6914 "Expected response message '$expected', got '$resp_msgs->[0]'");
6915
6916 $expected = "Transfer complete";
6917 $self->assert($expected eq $resp_msgs->[1],
6918 "Expected response message '$expected', got '$resp_msgs->[1]'");
6919
6920 $expected = " Get that directory listing OK, $setup->{user}?";
6921 $self->assert($expected eq $resp_msgs->[2],
6922 "Expected response message '$expected', got '$resp_msgs->[2]'");
6923
6924 $expected = "-";
6925 $self->assert($expected eq $resp_msgs->[3],
6926 "Expected response message '$expected', got '$resp_msgs->[3]'");
6927
6928 $client->quit();
6929 };
6930 if ($@) {
6931 $ex = $@;
6932 }
6933
6934 $wfh->print("done\n");
6935 $wfh->flush();
6936
6937 } else {
6938 eval { server_wait($setup->{config_file}, $rfh) };
6939 if ($@) {
6940 warn($@);
6941 exit 1;
6942 }
6943
6944 exit 0;
6945 }
6946
6947 # Stop server
6948 server_stop($setup->{pid_file});
6949 $self->assert_child_ok($pid);
6950
6951 test_cleanup($setup->{log_file}, $ex);
6952 }
6953
6954 sub sql_multiple_users_shared_uid_gid {
6955 my $self = shift;
6956 my $tmpdir = $self->{tmpdir};
6957
6958 my $config_file = "$tmpdir/sqlite.conf";
6959 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
6960 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
6961
6962 my $log_file = test_get_logfile();
6963
6964 my $user = 'proftpd';
6965 my $passwd = 'test';
6966 my $group = 'ftpd';
6967 my $home_dir = File::Spec->rel2abs($tmpdir);
6968 my $uid = 500;
6969 my $gid = 500;
6970
6971 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
6972
6973 # Build up sqlite3 command to create users, groups tables and populate them
6974 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
6975
6976 if (open(my $fh, "> $db_script")) {
6977 print $fh <<EOU;
6978 CREATE TABLE users (
6979 userid TEXT,
6980 passwd TEXT,
6981 uid INTEGER,
6982 gid INTEGER,
6983 homedir TEXT,
6984 shell TEXT
6985 );
6986 EOU
6987
6988 for (my $i = 1; $i <= 10; $i++) {
6989 my $name = $user . $i;
6990
6991 print $fh "INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$name', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');\n";
6992 }
6993
6994 print $fh <<EOG;
6995 CREATE TABLE groups (
6996 groupname TEXT,
6997 gid INTEGER,
6998 members TEXT
6999 );
7000 EOG
7001
7002 my $group_members = ($user . '1');
7003 for (my $i = 2; $i <= 5; $i++) {
7004 my $name = $user . $i;
7005 $group_members .= ",$name";
7006 }
7007
7008 my $group_name = ($group . '1');
7009 print $fh "INSERT INTO groups (groupname, gid, members) VALUES ('$group_name', $gid, '$group_members');\n";
7010
7011 $group_members = ($user . '6');
7012 for (my $i = 7; $i <= 10; $i++) {
7013 my $name = $user . $i;
7014 $group_members .= ",$name";
7015 }
7016
7017 $group_name = ($group . '2');
7018 print $fh "INSERT INTO groups (groupname, gid, members) VALUES ('$group_name', $gid, '$group_members');\n";
7019
7020 unless (close($fh)) {
7021 die("Can't write $db_script: $!");
7022 }
7023
7024 } else {
7025 die("Can't open $db_script: $!");
7026 }
7027
7028 my $cmd = "sqlite3 $db_file < $db_script";
7029 build_db($cmd, $db_script);
7030
7031 # Make sure that, if we're running as root, the database file has
7032 # the permissions/privs set for use by proftpd
7033 if ($< == 0) {
7034 unless (chmod(0666, $db_file)) {
7035 die("Can't set perms on $db_file to 0666: $!");
7036 }
7037 }
7038
7039 my $config = {
7040 PidFile => $pid_file,
7041 ScoreboardFile => $scoreboard_file,
7042 SystemLog => $log_file,
7043
7044 IfModules => {
7045 'mod_delay.c' => {
7046 DelayEngine => 'off',
7047 },
7048
7049 'mod_sql.c' => [
7050 'AuthOrder mod_sql.c',
7051
7052 'SQLAuthenticate users groups',
7053 'SQLAuthTypes plaintext',
7054 'SQLBackend sqlite3',
7055 "SQLConnectInfo $db_file",
7056 "SQLLogFile $log_file",
7057 'SQLMinID 100',
7058 ],
7059 },
7060 };
7061
7062 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7063
7064 # Open pipes, for use between the parent and child processes. Specifically,
7065 # the child will indicate when it's done with its test by writing a message
7066 # to the parent.
7067 my ($rfh, $wfh);
7068 unless (pipe($rfh, $wfh)) {
7069 die("Can't open pipe: $!");
7070 }
7071
7072 my $ex;
7073
7074 # Fork child
7075 $self->handle_sigchld();
7076 defined(my $pid = fork()) or die("Can't fork: $!");
7077 if ($pid) {
7078 eval {
7079 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7080
7081 my $name = $user . '10';
7082 $client->login($name, $passwd);
7083 $client->quit();
7084 };
7085
7086 if ($@) {
7087 $ex = $@;
7088 }
7089
7090 $wfh->print("done\n");
7091 $wfh->flush();
7092
7093 } else {
7094 eval { server_wait($config_file, $rfh) };
7095 if ($@) {
7096 warn($@);
7097 exit 1;
7098 }
7099
7100 exit 0;
7101 }
7102
7103 # Stop server
7104 server_stop($pid_file);
7105
7106 $self->assert_child_ok($pid);
7107
7108 if ($ex) {
7109 test_append_logfile($log_file, $ex);
7110 unlink($log_file);
7111
7112 die($ex);
7113 }
7114
7115 unlink($log_file);
7116 }
7117
7118 sub get_cmds {
7119 my $db_file = shift;
7120 my $where = shift;
7121
7122 my $sql = "SELECT user, ip_addr, command, request FROM ftpsessions";
7123 if ($where) {
7124 $sql .= " WHERE $where";
7125 }
7126
7127 my $cmd = "sqlite3 $db_file \"$sql\"";
7128
7129 if ($ENV{TEST_VERBOSE}) {
7130 print STDERR "Executing sqlite3: $cmd\n";
7131 }
7132
7133 my $rows = [`$cmd`];
7134
7135 if ($ENV{TEST_VERBOSE}) {
7136 use Data::Dumper;
7137 print STDERR "Results: ", Dumper($rows), "\n";
7138 }
7139
7140 my $res;
7141
7142 # Return the last row found, for now
7143 foreach my $row (@$rows) {
7144 chomp($row);
7145
7146 # The default sqlite3 delimiter is '|'
7147 $res = [split(/\|/, $row)];
7148 }
7149
7150 return unless $res;
7151 return @$res;
7152 }
7153
7154 sub sql_resolve_tag_bug3536 {
7155 my $self = shift;
7156 my $tmpdir = $self->{tmpdir};
7157
7158 my $config_file = "$tmpdir/sqlite.conf";
7159 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
7160 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
7161
7162 my $log_file = test_get_logfile();
7163
7164 my $user = 'proftpd';
7165 my $passwd = 'test';
7166 my $group = 'ftpd';
7167 my $home_dir = File::Spec->rel2abs($tmpdir);
7168 my $uid = 500;
7169 my $gid = 500;
7170
7171 # Make sure that, if we're running as root, that the home directory has
7172 # permissions/privs set for the account we create
7173 if ($< == 0) {
7174 unless (chmod(0755, $home_dir)) {
7175 die("Can't set perms on $home_dir to 0755: $!");
7176 }
7177
7178 unless (chown($uid, $gid, $home_dir)) {
7179 die("Can't set owner of $home_dir to $uid/$gid: $!");
7180 }
7181 }
7182
7183 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7184
7185 # Build up sqlite3 command to create users, groups tables and populate them
7186 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7187
7188 if (open(my $fh, "> $db_script")) {
7189 print $fh <<EOS;
7190 CREATE TABLE users (
7191 userid TEXT,
7192 passwd TEXT,
7193 uid INTEGER,
7194 gid INTEGER,
7195 homedir TEXT,
7196 shell TEXT
7197 );
7198 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
7199
7200 CREATE TABLE groups (
7201 groupname TEXT,
7202 gid INTEGER,
7203 members TEXT
7204 );
7205 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
7206
7207 CREATE TABLE ftpsessions (
7208 user TEXT,
7209 ip_addr TEXT,
7210 command TEXT,
7211 request TEXT
7212 );
7213 EOS
7214
7215 unless (close($fh)) {
7216 die("Can't write $db_script: $!");
7217 }
7218
7219 } else {
7220 die("Can't open $db_script: $!");
7221 }
7222
7223 my $cmd = "sqlite3 $db_file < $db_script";
7224 build_db($cmd, $db_script);
7225
7226 # Make sure that, if we're running as root, the database file has
7227 # the permissions/privs set for use by proftpd
7228 if ($< == 0) {
7229 unless (chmod(0666, $db_file)) {
7230 die("Can't set perms on $db_file to 0666: $!");
7231 }
7232 }
7233
7234 my $config = {
7235 PidFile => $pid_file,
7236 ScoreboardFile => $scoreboard_file,
7237 SystemLog => $log_file,
7238 TraceLog => $log_file,
7239 Trace => 'sql:20',
7240
7241 IfModules => {
7242 'mod_delay.c' => {
7243 DelayEngine => 'off',
7244 },
7245
7246 'mod_sql.c' => {
7247 SQLAuthTypes => 'plaintext',
7248 SQLBackend => 'sqlite3',
7249 SQLConnectInfo => $db_file,
7250 SQLLogFile => $log_file,
7251 SQLNamedQuery => 'command FREEFORM "INSERT INTO ftpsessions (user, ip_addr, command, request) VALUES (\'%u\', \'%L\', \'%m\', \'%r\')"',
7252 SQLLog => '* command',
7253 },
7254 },
7255 };
7256
7257 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7258
7259 # Open pipes, for use between the parent and child processes. Specifically,
7260 # the child will indicate when it's done with its test by writing a message
7261 # to the parent.
7262 my ($rfh, $wfh);
7263 unless (pipe($rfh, $wfh)) {
7264 die("Can't open pipe: $!");
7265 }
7266
7267 my $ex;
7268
7269 # Fork child
7270 $self->handle_sigchld();
7271 defined(my $pid = fork()) or die("Can't fork: $!");
7272 if ($pid) {
7273 eval {
7274 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7275
7276 # Ignore errors for these commands
7277 eval {
7278 my $name = "AAAAAAAAAA%m%m%mA%m%m%mA%m%mAA%m%m%m%m%mA%m%Z%m%mA%m%mAA%mA%ZAA%m%m%m%m%m%mA%m%ZAAA%m%m%m%m%m%m%m%m%m%m%m%m%m%m%mAa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0A%%%m%r%m%Z";
7279 $client->user($name);
7280 };
7281 };
7282
7283 if ($@) {
7284 $ex = $@;
7285 }
7286
7287 $wfh->print("done\n");
7288 $wfh->flush();
7289
7290 } else {
7291 eval { server_wait($config_file, $rfh) };
7292 if ($@) {
7293 warn($@);
7294 exit 1;
7295 }
7296
7297 exit 0;
7298 }
7299
7300 # Stop server
7301 server_stop($pid_file);
7302
7303 $self->assert_child_ok($pid);
7304
7305 if ($ex) {
7306 test_append_logfile($log_file, $ex);
7307 unlink($log_file);
7308
7309 die($ex);
7310 }
7311
7312 my ($login, $ip_addr, $req);
7313 ($login, $ip_addr, $cmd, $req) = get_cmds($db_file, "user = \'$config_user\'");
7314
7315 $self->assert(!defined($login), test_msg("Expected undef, got '$login'"));
7316 $self->assert(!defined($ip_addr), test_msg("Expected undef, got '$ip_addr'"));
7317
7318 unlink($log_file);
7319 }
7320
7321 sub get_session_io {
7322 my $db_file = shift;
7323 my $where = shift;
7324
7325 my $sql = "SELECT user, ip_addr, bytes_in, bytes_out FROM ftpsessions";
7326 if ($where) {
7327 $sql .= " WHERE $where";
7328 }
7329
7330 my $cmd = "sqlite3 $db_file \"$sql\"";
7331
7332 if ($ENV{TEST_VERBOSE}) {
7333 print STDERR "Executing sqlite3: $cmd\n";
7334 }
7335
7336 my $res = join('', `$cmd`);
7337 chomp($res);
7338
7339 # The default sqlite3 delimiter is '|'
7340 return split(/\|/, $res);
7341 }
7342
7343 sub sql_sqllog_vars_I_O_bug3554 {
7344 my $self = shift;
7345 my $tmpdir = $self->{tmpdir};
7346 my $setup = test_setup($tmpdir, 'sqlite');
7347
7348 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7349
7350 # Build up sqlite3 command to create users, groups tables and populate them
7351 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7352
7353 if (open(my $fh, "> $db_script")) {
7354 print $fh <<EOS;
7355 CREATE TABLE ftpsessions (
7356 user TEXT,
7357 ip_addr TEXT,
7358 bytes_in NUMBER,
7359 bytes_out NUMBER
7360 );
7361 EOS
7362
7363 unless (close($fh)) {
7364 die("Can't write $db_script: $!");
7365 }
7366
7367 } else {
7368 die("Can't open $db_script: $!");
7369 }
7370
7371 my $cmd = "sqlite3 $db_file < $db_script";
7372 build_db($cmd, $db_script);
7373
7374 # Make sure that, if we're running as root, the database file has
7375 # the permissions/privs set for use by proftpd
7376 if ($< == 0) {
7377 unless (chmod(0666, $db_file)) {
7378 die("Can't set perms on $db_file to 0666: $!");
7379 }
7380 }
7381
7382 my $config = {
7383 PidFile => $setup->{pid_file},
7384 ScoreboardFile => $setup->{scoreboard_file},
7385 SystemLog => $setup->{log_file},
7386 TraceLog => $setup->{log_file},
7387 Trace => 'DEFAULT:10 jot:20 sql:20',
7388
7389 AuthUserFile => $setup->{auth_user_file},
7390 AuthGroupFile => $setup->{auth_group_file},
7391 AuthOrder => 'mod_auth_file.c',
7392
7393 IfModules => {
7394 'mod_delay.c' => {
7395 DelayEngine => 'off',
7396 },
7397
7398 'mod_sql.c' => {
7399 SQLEngine => 'log',
7400 SQLBackend => 'sqlite3',
7401 SQLConnectInfo => $db_file,
7402 SQLLogFile => $setup->{log_file},
7403 SQLNamedQuery => 'session_io FREEFORM "INSERT INTO ftpsessions (user, ip_addr, bytes_in, bytes_out) VALUES (\'%u\', \'%L\', %I, %O)"',
7404 SQLLog => 'EXIT session_io',
7405 },
7406 },
7407 };
7408
7409 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
7410 $config);
7411
7412 # Open pipes, for use between the parent and child processes. Specifically,
7413 # the child will indicate when it's done with its test by writing a message
7414 # to the parent.
7415 my ($rfh, $wfh);
7416 unless (pipe($rfh, $wfh)) {
7417 die("Can't open pipe: $!");
7418 }
7419
7420 my $ex;
7421
7422 # Fork child
7423 $self->handle_sigchld();
7424 defined(my $pid = fork()) or die("Can't fork: $!");
7425 if ($pid) {
7426 eval {
7427 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7428 $client->login($setup->{user}, $setup->{passwd});
7429 $client->type('ascii');
7430
7431 my $conn = $client->stor_raw('test.txt');
7432 unless ($conn) {
7433 die("STOR test.txt failed: " . $client->response_code() . " " .
7434 $client->response_msg());
7435 }
7436
7437 my $buf = "ABCD\n" x 8;
7438 $conn->write($buf, length($buf), 30);
7439 $conn->close();
7440
7441 my $resp_code = $client->response_code();
7442 my $resp_msg = $client->response_msg();
7443
7444 $client->quit();
7445
7446 my $expected = 226;
7447 $self->assert($expected == $resp_code,
7448 "Expected response code $expected, got $resp_code");
7449
7450 $expected = 'Transfer complete';
7451 $self->assert($expected eq $resp_msg,
7452 "Expected response message '$expected', got '$resp_msg'");
7453 };
7454 if ($@) {
7455 $ex = $@;
7456 }
7457
7458 $wfh->print("done\n");
7459 $wfh->flush();
7460
7461 } else {
7462 eval { server_wait($setup->{config_file}, $rfh) };
7463 if ($@) {
7464 warn($@);
7465 exit 1;
7466 }
7467
7468 exit 0;
7469 }
7470
7471 # Stop server
7472 server_stop($setup->{pid_file});
7473 $self->assert_child_ok($pid);
7474
7475 if ($ex) {
7476 test_cleanup($setup->{log_file}, $ex);
7477 }
7478
7479 eval {
7480 my ($login, $ip_addr, $bytes_in, $bytes_out) = get_session_io($db_file,
7481 "user = \'$setup->{user}\'");
7482
7483 my $expected = $setup->{user};
7484 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
7485
7486 $expected = '127.0.0.1';
7487 $self->assert($expected eq $ip_addr,
7488 "Expected '$expected', got '$ip_addr'");
7489
7490 $expected = 108;
7491 $self->assert($expected == $bytes_in,
7492 "Expected $expected, got $bytes_in");
7493
7494 # Why would this number vary so widely? It's because of the notation
7495 # used to express the port number in a PASV response. That port
7496 # number is ephemeral, chosen by the kernel.
7497
7498 my $expected_min = 232;
7499 my $expected_max = 236;
7500 $self->assert($expected_min <= $bytes_out ||
7501 $expected_max >= $bytes_out,
7502 "Expected $expected_min - $expected_max, got $bytes_out");
7503 };
7504 if ($@) {
7505 $ex = $@;
7506 }
7507
7508 test_cleanup($setup->{log_file}, $ex);
7509 }
7510
7511 sub get_session_id {
7512 my $db_file = shift;
7513 my $where = shift;
7514
7515 my $sql = "SELECT user, ip_addr, session_id FROM ftpsessions";
7516 if ($where) {
7517 $sql .= " WHERE $where";
7518 }
7519
7520 my $cmd = "sqlite3 $db_file \"$sql\"";
7521
7522 if ($ENV{TEST_VERBOSE}) {
7523 print STDERR "Executing sqlite3: $cmd\n";
7524 }
7525
7526 my $res = join('', `$cmd`);
7527 chomp($res);
7528
7529 # The default sqlite3 delimiter is '|'
7530 return split(/\|/, $res);
7531 }
7532
7533 sub sql_sqllog_note_var_unique_id_bug3572 {
7534 my $self = shift;
7535 my $tmpdir = $self->{tmpdir};
7536
7537 my $config_file = "$tmpdir/sqlite.conf";
7538 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
7539 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
7540
7541 my $log_file = test_get_logfile();
7542
7543 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
7544 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
7545
7546 my $user = 'proftpd';
7547 my $passwd = 'test';
7548 my $group = 'ftpd';
7549 my $home_dir = File::Spec->rel2abs($tmpdir);
7550 my $uid = 500;
7551 my $gid = 500;
7552
7553 # Make sure that, if we're running as root, that the home directory has
7554 # permissions/privs set for the account we create
7555 if ($< == 0) {
7556 unless (chmod(0755, $home_dir)) {
7557 die("Can't set perms on $home_dir to 0755: $!");
7558 }
7559
7560 unless (chown($uid, $gid, $home_dir)) {
7561 die("Can't set owner of $home_dir to $uid/$gid: $!");
7562 }
7563 }
7564
7565 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
7566 '/bin/bash');
7567 auth_group_write($auth_group_file, $group, $gid, $user);
7568
7569 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7570
7571 # Build up sqlite3 command to create users, groups tables and populate them
7572 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7573
7574 if (open(my $fh, "> $db_script")) {
7575 print $fh <<EOS;
7576 CREATE TABLE ftpsessions (
7577 user TEXT,
7578 ip_addr TEXT,
7579 session_id TEXT
7580 );
7581 EOS
7582
7583 unless (close($fh)) {
7584 die("Can't write $db_script: $!");
7585 }
7586
7587 } else {
7588 die("Can't open $db_script: $!");
7589 }
7590
7591 my $cmd = "sqlite3 $db_file < $db_script";
7592 build_db($cmd, $db_script);
7593
7594 # Make sure that, if we're running as root, the database file has
7595 # the permissions/privs set for use by proftpd
7596 if ($< == 0) {
7597 unless (chmod(0666, $db_file)) {
7598 die("Can't set perms on $db_file to 0666: $!");
7599 }
7600 }
7601
7602 my $config = {
7603 PidFile => $pid_file,
7604 ScoreboardFile => $scoreboard_file,
7605 SystemLog => $log_file,
7606 TraceLog => $log_file,
7607 Trace => 'DEFAULT:10',
7608
7609 AuthUserFile => $auth_user_file,
7610 AuthGroupFile => $auth_group_file,
7611 AuthOrder => 'mod_auth_file.c',
7612
7613 IfModules => {
7614 'mod_delay.c' => {
7615 DelayEngine => 'off',
7616 },
7617
7618 'mod_sql.c' => {
7619 SQLEngine => 'log',
7620 SQLBackend => 'sqlite3',
7621 SQLConnectInfo => $db_file,
7622 SQLLogFile => $log_file,
7623 SQLNamedQuery => 'session_id FREEFORM "INSERT INTO ftpsessions (user, ip_addr, session_id) VALUES (\'%u\', \'%L\', \'%{note:UNIQUE_ID}\')"',
7624 SQLLog => 'PASS session_id',
7625 },
7626 },
7627 };
7628
7629 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7630
7631 # Open pipes, for use between the parent and child processes. Specifically,
7632 # the child will indicate when it's done with its test by writing a message
7633 # to the parent.
7634 my ($rfh, $wfh);
7635 unless (pipe($rfh, $wfh)) {
7636 die("Can't open pipe: $!");
7637 }
7638
7639 my $ex;
7640
7641 # Fork child
7642 $self->handle_sigchld();
7643 defined(my $pid = fork()) or die("Can't fork: $!");
7644 if ($pid) {
7645 eval {
7646 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7647 $client->login($user, $passwd);
7648 $client->quit();
7649 };
7650
7651 if ($@) {
7652 $ex = $@;
7653 }
7654
7655 $wfh->print("done\n");
7656 $wfh->flush();
7657
7658 } else {
7659 eval { server_wait($config_file, $rfh) };
7660 if ($@) {
7661 warn($@);
7662 exit 1;
7663 }
7664
7665 exit 0;
7666 }
7667
7668 # Stop server
7669 server_stop($pid_file);
7670
7671 $self->assert_child_ok($pid);
7672
7673 if ($ex) {
7674 test_append_logfile($log_file, $ex);
7675 unlink($log_file);
7676
7677 die($ex);
7678 }
7679
7680 my ($login, $ip_addr, $session_id) = get_session_id($db_file,
7681 "user = \'$user\'");
7682
7683 my $expected;
7684
7685 $expected = $user;
7686 $self->assert($expected eq $login,
7687 test_msg("Expected '$expected', got '$login'"));
7688
7689 $expected = '127.0.0.1';
7690 $self->assert($expected eq $ip_addr,
7691 test_msg("Expected '$expected', got '$ip_addr'"));
7692
7693 $self->assert($session_id ne '',
7694 test_msg("Expected session ID ($session_id), got ''"));
7695
7696 unlink($log_file);
7697 }
7698
7699 sub sql_sqllog_note_var_rewrite_bug3572 {
7700 my $self = shift;
7701 my $tmpdir = $self->{tmpdir};
7702
7703 my $config_file = "$tmpdir/sqlite.conf";
7704 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
7705 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
7706
7707 my $log_file = test_get_logfile();
7708
7709 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
7710 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
7711
7712 my $user = 'proftpd';
7713 my $passwd = 'test';
7714 my $group = 'ftpd';
7715 my $home_dir = File::Spec->rel2abs($tmpdir);
7716 my $uid = 500;
7717 my $gid = 500;
7718
7719 my $domain = 'proftpd.org';
7720
7721 # Make sure that, if we're running as root, that the home directory has
7722 # permissions/privs set for the account we create
7723 if ($< == 0) {
7724 unless (chmod(0755, $home_dir)) {
7725 die("Can't set perms on $home_dir to 0755: $!");
7726 }
7727
7728 unless (chown($uid, $gid, $home_dir)) {
7729 die("Can't set owner of $home_dir to $uid/$gid: $!");
7730 }
7731 }
7732
7733 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
7734 '/bin/bash');
7735 auth_group_write($auth_group_file, $group, $gid, $user);
7736
7737 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7738
7739 # Build up sqlite3 command to create users, groups tables and populate them
7740 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7741
7742 if (open(my $fh, "> $db_script")) {
7743 print $fh <<EOS;
7744 CREATE TABLE ftpsessions (
7745 user TEXT,
7746 ip_addr TEXT,
7747 session_id TEXT
7748 );
7749 EOS
7750
7751 unless (close($fh)) {
7752 die("Can't write $db_script: $!");
7753 }
7754
7755 } else {
7756 die("Can't open $db_script: $!");
7757 }
7758
7759 my $cmd = "sqlite3 $db_file < $db_script";
7760 build_db($cmd, $db_script);
7761
7762 # Make sure that, if we're running as root, the database file has
7763 # the permissions/privs set for use by proftpd
7764 if ($< == 0) {
7765 unless (chmod(0666, $db_file)) {
7766 die("Can't set perms on $db_file to 0666: $!");
7767 }
7768 }
7769
7770 my $config = {
7771 PidFile => $pid_file,
7772 ScoreboardFile => $scoreboard_file,
7773 SystemLog => $log_file,
7774 TraceLog => $log_file,
7775 Trace => 'rewrite:10 sql:20 table:20',
7776
7777 AuthUserFile => $auth_user_file,
7778 AuthGroupFile => $auth_group_file,
7779 AuthOrder => 'mod_auth_file.c',
7780
7781 IfModules => {
7782 'mod_delay.c' => {
7783 DelayEngine => 'off',
7784 },
7785
7786 'mod_rewrite.c' => [
7787 'RewriteEngine on',
7788 "RewriteLog $log_file",
7789
7790 'RewriteCondition %m USER',
7791 'RewriteRule ^(.*#)?([-_.0-9A-Za-z]+)(@)?(.*)? $2',
7792 ],
7793
7794 'mod_sql.c' => {
7795 SQLEngine => 'log',
7796 SQLBackend => 'sqlite3',
7797 SQLConnectInfo => $db_file,
7798 SQLLogFile => $log_file,
7799 SQLNamedQuery => 'session_id FREEFORM "INSERT INTO ftpsessions (user, ip_addr, session_id) VALUES (\'%U\', \'%L\', \'%{note:mod_rewrite.$4}\')"',
7800 SQLLog => 'USER session_id',
7801 },
7802 },
7803 };
7804
7805 my ($port, $config_user, $config_group) = config_write($config_file, $config);
7806
7807 # Open pipes, for use between the parent and child processes. Specifically,
7808 # the child will indicate when it's done with its test by writing a message
7809 # to the parent.
7810 my ($rfh, $wfh);
7811 unless (pipe($rfh, $wfh)) {
7812 die("Can't open pipe: $!");
7813 }
7814
7815 my $ex;
7816
7817 # Fork child
7818 $self->handle_sigchld();
7819 defined(my $pid = fork()) or die("Can't fork: $!");
7820 if ($pid) {
7821 eval {
7822 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
7823
7824 my $login = $user . '@' . $domain;
7825 $client->login($login, $passwd);
7826 $client->quit();
7827 };
7828
7829 if ($@) {
7830 $ex = $@;
7831 }
7832
7833 $wfh->print("done\n");
7834 $wfh->flush();
7835
7836 } else {
7837 eval { server_wait($config_file, $rfh) };
7838 if ($@) {
7839 warn($@);
7840 exit 1;
7841 }
7842
7843 exit 0;
7844 }
7845
7846 # Stop server
7847 server_stop($pid_file);
7848
7849 $self->assert_child_ok($pid);
7850
7851 if ($ex) {
7852 test_append_logfile($log_file, $ex);
7853 unlink($log_file);
7854
7855 die($ex);
7856 }
7857
7858 my ($login, $ip_addr, $session_id) = get_session_id($db_file,
7859 "user = \'$user\'");
7860
7861 my $expected;
7862
7863 $expected = $user;
7864 $self->assert($expected eq $login,
7865 test_msg("Expected '$expected', got '$login'"));
7866
7867 $expected = '127.0.0.1';
7868 $self->assert($expected eq $ip_addr,
7869 test_msg("Expected '$expected', got '$ip_addr'"));
7870
7871 $expected = $domain;
7872 $self->assert($expected eq $session_id,
7873 test_msg("Expected '$expected', got '$session_id'"));
7874
7875 unlink($log_file);
7876 }
7877
7878 sub get_sql_notes {
7879 my $db_file = shift;
7880 my $where = shift;
7881
7882 my $sql = "SELECT user, primary_group, home, shell FROM ftpnotes";
7883 if ($where) {
7884 $sql .= " WHERE $where";
7885 }
7886
7887 my $cmd = "sqlite3 $db_file \"$sql\"";
7888
7889 if ($ENV{TEST_VERBOSE}) {
7890 print STDERR "Executing sqlite3: $cmd\n";
7891 }
7892
7893 my $res = join('', `$cmd`);
7894 chomp($res);
7895
7896 # The default sqlite3 delimiter is '|'
7897 return split(/\|/, $res);
7898 }
7899
7900 sub sql_sqllog_note_sql_user_info {
7901 my $self = shift;
7902 my $tmpdir = $self->{tmpdir};
7903
7904 my $config_file = "$tmpdir/sqlite.conf";
7905 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
7906 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
7907
7908 my $log_file = test_get_logfile();
7909
7910 my $user = 'proftpd';
7911 my $group = 'ftpd',
7912 my $passwd = 'test';
7913 my $home_dir = File::Spec->rel2abs($tmpdir);
7914 my $uid = 500;
7915 my $gid = 500;
7916 my $shell = '/bin/bash';
7917
7918 # Make sure that, if we're running as root, that the home directory has
7919 # permissions/privs set for the account we create
7920 if ($< == 0) {
7921 unless (chmod(0755, $home_dir)) {
7922 die("Can't set perms on $home_dir to 0755: $!");
7923 }
7924
7925 unless (chown($uid, $gid, $home_dir)) {
7926 die("Can't set owner of $home_dir to $uid/$gid: $!");
7927 }
7928 }
7929
7930 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
7931
7932 # Build up sqlite3 command to create users, groups tables and populate them
7933 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
7934
7935 if (open(my $fh, "> $db_script")) {
7936 print $fh <<EOS;
7937 CREATE TABLE users (
7938 userid TEXT,
7939 passwd TEXT,
7940 uid INTEGER,
7941 gid INTEGER,
7942 homedir TEXT,
7943 shell TEXT,
7944 lastdir TEXT
7945 );
7946 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '$shell');
7947
7948 CREATE TABLE groups (
7949 groupname TEXT,
7950 gid INTEGER,
7951 members TEXT
7952 );
7953 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
7954
7955 CREATE TABLE ftpnotes (
7956 user TEXT,
7957 primary_group TEXT,
7958 home TEXT,
7959 shell TEXT
7960 );
7961 EOS
7962
7963 unless (close($fh)) {
7964 die("Can't write $db_script: $!");
7965 }
7966
7967 } else {
7968 die("Can't open $db_script: $!");
7969 }
7970
7971 my $cmd = "sqlite3 $db_file < $db_script";
7972 build_db($cmd, $db_script);
7973
7974 # Make sure that, if we're running as root, the database file has
7975 # the permissions/privs set for use by proftpd
7976 if ($< == 0) {
7977 unless (chmod(0666, $db_file)) {
7978 die("Can't set perms on $db_file to 0666: $!");
7979 }
7980 }
7981
7982 my $config = {
7983 PidFile => $pid_file,
7984 ScoreboardFile => $scoreboard_file,
7985 SystemLog => $log_file,
7986 TraceLog => $log_file,
7987 Trace => 'DEFAULT:10 sql:20',
7988
7989 IfModules => {
7990 'mod_delay.c' => {
7991 DelayEngine => 'off',
7992 },
7993
7994 'mod_sql.c' => {
7995 SQLAuthTypes => 'plaintext',
7996 SQLBackend => 'sqlite3',
7997 SQLConnectInfo => $db_file,
7998 SQLMinID => 100,
7999 SQLLogFile => $log_file,
8000 SQLNamedQuery => 'sql_notes FREEFORM "INSERT INTO ftpnotes (user, primary_group, home, shell) VALUES (\'%u\', \'%{note:primary-group}\', \'%{note:home}\', \'%{note:shell}\')"',
8001 SQLLog => 'PASS sql_notes',
8002 },
8003 },
8004 };
8005
8006 my ($port, $config_user, $config_group) = config_write($config_file, $config);
8007
8008 # Open pipes, for use between the parent and child processes. Specifically,
8009 # the child will indicate when it's done with its test by writing a message
8010 # to the parent.
8011 my ($rfh, $wfh);
8012 unless (pipe($rfh, $wfh)) {
8013 die("Can't open pipe: $!");
8014 }
8015
8016 my $ex;
8017
8018 # Fork child
8019 $self->handle_sigchld();
8020 defined(my $pid = fork()) or die("Can't fork: $!");
8021 if ($pid) {
8022 eval {
8023 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8024 $client->login($user, $passwd);
8025 $client->quit();
8026 };
8027
8028 if ($@) {
8029 $ex = $@;
8030 }
8031
8032 $wfh->print("done\n");
8033 $wfh->flush();
8034
8035 } else {
8036 eval { server_wait($config_file, $rfh) };
8037 if ($@) {
8038 warn($@);
8039 exit 1;
8040 }
8041
8042 exit 0;
8043 }
8044
8045 # Stop server
8046 server_stop($pid_file);
8047
8048 $self->assert_child_ok($pid);
8049
8050 if ($ex) {
8051 test_append_logfile($log_file, $ex);
8052 unlink($log_file);
8053
8054 die($ex);
8055 }
8056
8057 my ($login, $sql_group, $sql_home, $sql_shell) = get_sql_notes($db_file,
8058 "user = \'$user\'");
8059
8060 my $expected;
8061
8062 $expected = $user;
8063 $self->assert($expected eq $login,
8064 test_msg("Expected '$expected', got '$login'"));
8065
8066 $expected = $group;
8067 $self->assert($expected eq $sql_group,
8068 test_msg("Expected '$expected', got '$sql_group'"));
8069
8070 $expected = $home_dir;
8071 $self->assert($expected eq $sql_home,
8072 test_msg("Expected '$expected', got '$sql_home'"));
8073
8074 $expected = $shell;
8075 $self->assert($expected eq $sql_shell,
8076 test_msg("Expected '$expected', got '$sql_shell'"));
8077
8078 unlink($log_file);
8079 }
8080
8081 sub sql_sqllog_var_E {
8082 my $self = shift;
8083 my $tmpdir = $self->{tmpdir};
8084 my $setup = test_setup($tmpdir, 'sqlite');
8085
8086 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
8087
8088 # Build up sqlite3 command to create users, groups tables and populate them
8089 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
8090
8091 if (open(my $fh, "> $db_script")) {
8092 print $fh <<EOS;
8093 CREATE TABLE ftpsessions (
8094 user TEXT,
8095 ip_addr TEXT,
8096 resp_mesg TEXT
8097 );
8098 EOS
8099
8100 unless (close($fh)) {
8101 die("Can't write $db_script: $!");
8102 }
8103
8104 } else {
8105 die("Can't open $db_script: $!");
8106 }
8107
8108 my $cmd = "sqlite3 $db_file < $db_script";
8109 build_db($cmd, $db_script);
8110
8111 # Make sure that, if we're running as root, the database file has
8112 # the permissions/privs set for use by proftpd
8113 if ($< == 0) {
8114 unless (chmod(0666, $db_file)) {
8115 die("Can't set perms on $db_file to 0666: $!");
8116 }
8117 }
8118
8119 my $config = {
8120 PidFile => $setup->{pid_file},
8121 ScoreboardFile => $setup->{scoreboard_file},
8122 SystemLog => $setup->{log_file},
8123 TraceLog => $setup->{log_file},
8124 Trace => 'jot:20 sql:20',
8125
8126 AuthUserFile => $setup->{auth_user_file},
8127 AuthGroupFile => $setup->{auth_group_file},
8128 AuthOrder => 'mod_auth_file.c',
8129
8130 IfModules => {
8131 'mod_delay.c' => {
8132 DelayEngine => 'off',
8133 },
8134
8135 'mod_sql.c' => {
8136 SQLEngine => 'log',
8137 SQLBackend => 'sqlite3',
8138 SQLConnectInfo => $db_file,
8139 SQLLogFile => $setup->{log_file},
8140 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, ip_addr, resp_mesg) VALUES (\'%u\', \'%L\', \'%E\')"',
8141 SQLLog => 'EXIT info',
8142 },
8143 },
8144 };
8145
8146 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
8147 $config);
8148
8149 # Open pipes, for use between the parent and child processes. Specifically,
8150 # the child will indicate when it's done with its test by writing a message
8151 # to the parent.
8152 my ($rfh, $wfh);
8153 unless (pipe($rfh, $wfh)) {
8154 die("Can't open pipe: $!");
8155 }
8156
8157 my $ex;
8158
8159 # Fork child
8160 $self->handle_sigchld();
8161 defined(my $pid = fork()) or die("Can't fork: $!");
8162 if ($pid) {
8163 eval {
8164 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8165 $client->login($setup->{user}, $setup->{passwd});
8166 $client->list();
8167 $client->quit();
8168 };
8169 if ($@) {
8170 $ex = $@;
8171 }
8172
8173 $wfh->print("done\n");
8174 $wfh->flush();
8175
8176 } else {
8177 eval { server_wait($setup->{config_file}, $rfh) };
8178 if ($@) {
8179 warn($@);
8180 exit 1;
8181 }
8182
8183 exit 0;
8184 }
8185
8186 # Stop server
8187 server_stop($setup->{pid_file});
8188 $self->assert_child_ok($pid);
8189
8190 if ($ex) {
8191 test_cleanup($setup->{log_file}, $ex);
8192 }
8193
8194 eval {
8195 my ($login, $ip_addr, $resp_mesg) = get_resp_mesgs($db_file,
8196 "user = \'$setup->{user}\'", "user, ip_addr, resp_mesg");
8197
8198 my $expected = $setup->{user};
8199 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
8200
8201 $expected = '127.0.0.1';
8202 $self->assert($expected eq $ip_addr,
8203 "Expected '$expected', got '$ip_addr'");
8204
8205 $expected = 'Quit';
8206 $self->assert($expected eq $resp_mesg,
8207 "Expected '$expected', got '$resp_mesg'");
8208 };
8209 if ($@) {
8210 $ex = $@;
8211 }
8212
8213 test_cleanup($setup->{log_file}, $ex);
8214 }
8215
8216 sub sql_named_conn_bug3262 {
8217 my $self = shift;
8218 my $tmpdir = $self->{tmpdir};
8219
8220 my $config_file = "$tmpdir/sqlite.conf";
8221 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
8222 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
8223
8224 my $log_file = test_get_logfile();
8225
8226 my $user = 'proftpd';
8227 my $passwd = 'test';
8228 my $group = 'ftpd';
8229 my $home_dir = File::Spec->rel2abs($tmpdir);
8230 my $uid = 500;
8231 my $gid = 500;
8232
8233 # Make sure that, if we're running as root, that the home directory has
8234 # permissions/privs set for the account we create
8235 if ($< == 0) {
8236 unless (chmod(0755, $home_dir)) {
8237 die("Can't set perms on $home_dir to 0755: $!");
8238 }
8239
8240 unless (chown($uid, $gid, $home_dir)) {
8241 die("Can't set owner of $home_dir to $uid/$gid: $!");
8242 }
8243 }
8244
8245 my $userdb_file = File::Spec->rel2abs("$tmpdir/proftpd-users.db");
8246
8247 # Build up sqlite3 command to create users, groups tables and populate them
8248 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd-users.sql");
8249
8250 if (open(my $fh, "> $db_script")) {
8251 print $fh <<EOS;
8252 CREATE TABLE ftpusers (
8253 userid TEXT,
8254 passwd TEXT,
8255 uid INTEGER,
8256 gid INTEGER,
8257 homedir TEXT,
8258 shell TEXT,
8259 lastdir TEXT
8260 );
8261 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
8262
8263 CREATE TABLE groups (
8264 groupname TEXT,
8265 gid INTEGER,
8266 members TEXT
8267 );
8268 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
8269
8270 CREATE TABLE ftpsessions (
8271 user TEXT,
8272 ip_addr TEXT,
8273 timestamp TEXT
8274 );
8275
8276 EOS
8277
8278 unless (close($fh)) {
8279 die("Can't write $db_script: $!");
8280 }
8281
8282 } else {
8283 die("Can't open $db_script: $!");
8284 }
8285
8286 my $cmd = "sqlite3 $userdb_file < $db_script";
8287 build_db($cmd, $db_script);
8288
8289 my $logdb_file = File::Spec->rel2abs("$tmpdir/proftpd-log.db");
8290
8291 # Build up sqlite3 command to create users, groups tables and populate them
8292 $db_script = File::Spec->rel2abs("$tmpdir/proftpd-log.sql");
8293
8294 if (open(my $fh, "> $db_script")) {
8295 print $fh <<EOS;
8296 CREATE TABLE ftpsessions (
8297 user TEXT,
8298 ip_addr TEXT,
8299 timestamp TEXT
8300 );
8301
8302 EOS
8303
8304 unless (close($fh)) {
8305 die("Can't write $db_script: $!");
8306 }
8307
8308 } else {
8309 die("Can't open $db_script: $!");
8310 }
8311
8312 $cmd = "sqlite3 $logdb_file < $db_script";
8313 build_db($cmd, $db_script);
8314
8315 # Make sure that, if we're running as root, the database file has
8316 # the permissions/privs set for use by proftpd
8317 if ($< == 0) {
8318 unless (chmod(0666, $userdb_file, $logdb_file)) {
8319 die("Can't set perms on $userdb_file, $logdb_file to 0666: $!");
8320 }
8321 }
8322
8323 my $config = {
8324 PidFile => $pid_file,
8325 ScoreboardFile => $scoreboard_file,
8326 SystemLog => $log_file,
8327
8328 IfModules => {
8329 'mod_delay.c' => {
8330 DelayEngine => 'off',
8331 },
8332
8333 'mod_sql.c' => [
8334 'SQLAuthTypes plaintext',
8335 'SQLBackend sqlite3',
8336 "SQLConnectInfo $userdb_file",
8337 "SQLNamedConnectInfo logdb sqlite3 $logdb_file foo bar PERSESSION",
8338 "SQLLogFile $log_file",
8339 'SQLNamedQuery get-user-by-name SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE userid = \'%U\'"',
8340 'SQLNamedQuery get-user-by-id SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE uid = %{0}"',
8341 'SQLNamedQuery get-user-names SELECT "userid FROM ftpusers"',
8342 'SQLNamedQuery get-all-users SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers"',
8343 'SQLUserInfo custom:/get-user-by-name/get-user-by-id/get-user-names/get-all-users',
8344 'SQLNamedQuery session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, timestamp) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')" logdb',
8345 'SQLLog PASS session_start',
8346 ],
8347 },
8348 };
8349
8350 my ($port, $config_user, $config_group) = config_write($config_file, $config);
8351
8352 # Open pipes, for use between the parent and child processes. Specifically,
8353 # the child will indicate when it's done with its test by writing a message
8354 # to the parent.
8355 my ($rfh, $wfh);
8356 unless (pipe($rfh, $wfh)) {
8357 die("Can't open pipe: $!");
8358 }
8359
8360 my $ex;
8361
8362 # Fork child
8363 $self->handle_sigchld();
8364 defined(my $pid = fork()) or die("Can't fork: $!");
8365 if ($pid) {
8366 eval {
8367 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8368
8369 $client->login($user, $passwd);
8370
8371 my $resp_msgs = $client->response_msgs();
8372 my $nmsgs = scalar(@$resp_msgs);
8373
8374 my $expected;
8375
8376 $expected = 1;
8377 $self->assert($expected == $nmsgs,
8378 test_msg("Expected $expected, got $nmsgs"));
8379
8380 $expected = "User proftpd logged in";
8381 $self->assert($expected eq $resp_msgs->[0],
8382 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
8383
8384 };
8385
8386 if ($@) {
8387 $ex = $@;
8388 }
8389
8390 $wfh->print("done\n");
8391 $wfh->flush();
8392
8393 } else {
8394 eval { server_wait($config_file, $rfh) };
8395 if ($@) {
8396 warn($@);
8397 exit 1;
8398 }
8399
8400 exit 0;
8401 }
8402
8403 # Stop server
8404 server_stop($pid_file);
8405
8406 $self->assert_child_ok($pid);
8407
8408 if ($ex) {
8409 test_append_logfile($log_file, $ex);
8410 unlink($log_file);
8411
8412 die($ex);
8413 }
8414
8415 my ($login, $ip_addr, $timestamp) = get_sessions($userdb_file,
8416 "user = \'$user\'");
8417
8418 $self->assert(!defined($login),
8419 test_msg("Expected undef, got '$login'"));
8420
8421 $self->assert(!defined($ip_addr),
8422 test_msg("Expected undef, got '$ip_addr'"));
8423
8424 $self->assert(!defined($timestamp),
8425 test_msg("Expected undef, got '$timestamp'"));
8426
8427 ($login, $ip_addr, $timestamp) = get_sessions($logdb_file,
8428 "user = \'$user\'");
8429
8430 my $expected;
8431
8432 $expected = $user;
8433 $self->assert($expected eq $login,
8434 test_msg("Expected '$expected', got '$login'"));
8435
8436 $expected = '127.0.0.1';
8437 $self->assert($expected eq $ip_addr,
8438 test_msg("Expected '$expected', got '$ip_addr'"));
8439
8440 $expected = '\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}';
8441 $self->assert(qr/$expected/, $timestamp,
8442 test_msg("Expected '$expected', got '$timestamp'"));
8443
8444 unlink($log_file);
8445 }
8446
8447 sub sql_named_conn_sqllog_exit_bug3645 {
8448 my $self = shift;
8449 my $tmpdir = $self->{tmpdir};
8450
8451 my $config_file = "$tmpdir/sqlite.conf";
8452 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
8453 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
8454
8455 my $log_file = test_get_logfile();
8456
8457 my $user = 'proftpd';
8458 my $passwd = 'test';
8459 my $group = 'ftpd';
8460 my $home_dir = File::Spec->rel2abs($tmpdir);
8461 my $uid = 500;
8462 my $gid = 500;
8463
8464 # Make sure that, if we're running as root, that the home directory has
8465 # permissions/privs set for the account we create
8466 if ($< == 0) {
8467 unless (chmod(0755, $home_dir)) {
8468 die("Can't set perms on $home_dir to 0755: $!");
8469 }
8470
8471 unless (chown($uid, $gid, $home_dir)) {
8472 die("Can't set owner of $home_dir to $uid/$gid: $!");
8473 }
8474 }
8475
8476 my $userdb_file = File::Spec->rel2abs("$tmpdir/proftpd-users.db");
8477
8478 # Build up sqlite3 command to create users, groups tables and populate them
8479 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd-users.sql");
8480
8481 if (open(my $fh, "> $db_script")) {
8482 print $fh <<EOS;
8483 CREATE TABLE users (
8484 userid TEXT,
8485 passwd TEXT,
8486 uid INTEGER,
8487 gid INTEGER,
8488 homedir TEXT,
8489 shell TEXT
8490 );
8491 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
8492
8493 CREATE TABLE groups (
8494 groupname TEXT,
8495 gid INTEGER,
8496 members TEXT
8497 );
8498 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
8499 EOS
8500
8501 unless (close($fh)) {
8502 die("Can't write $db_script: $!");
8503 }
8504
8505 } else {
8506 die("Can't open $db_script: $!");
8507 }
8508
8509 my $cmd = "sqlite3 $userdb_file < $db_script";
8510 build_db($cmd, $db_script);
8511
8512 my $logdb_file = File::Spec->rel2abs("$tmpdir/proftpd-log.db");
8513
8514 # Build up sqlite3 command to create users, groups tables and populate them
8515 $db_script = File::Spec->rel2abs("$tmpdir/proftpd-log.sql");
8516
8517 if (open(my $fh, "> $db_script")) {
8518 print $fh <<EOS;
8519 CREATE TABLE ftpsessions (
8520 user TEXT,
8521 ip_addr TEXT,
8522 timestamp TEXT
8523 );
8524
8525 EOS
8526
8527 unless (close($fh)) {
8528 die("Can't write $db_script: $!");
8529 }
8530
8531 } else {
8532 die("Can't open $db_script: $!");
8533 }
8534
8535 $cmd = "sqlite3 $logdb_file < $db_script";
8536 build_db($cmd, $db_script);
8537
8538 # Make sure that, if we're running as root, the database file has
8539 # the permissions/privs set for use by proftpd
8540 if ($< == 0) {
8541 unless (chmod(0666, $userdb_file, $logdb_file)) {
8542 die("Can't set perms on $userdb_file, $logdb_file to 0666: $!");
8543 }
8544 }
8545
8546 my $config = {
8547 PidFile => $pid_file,
8548 ScoreboardFile => $scoreboard_file,
8549 SystemLog => $log_file,
8550
8551 DefaultRoot => '~',
8552
8553 IfModules => {
8554 'mod_delay.c' => {
8555 DelayEngine => 'off',
8556 },
8557
8558 'mod_sql.c' => [
8559 'SQLAuthTypes plaintext',
8560 'SQLBackend sqlite3',
8561 "SQLConnectInfo $userdb_file",
8562 "SQLNamedConnectInfo logdb sqlite3 $logdb_file",
8563 "SQLLogFile $log_file",
8564 'SQLNamedQuery session_end FREEFORM "INSERT INTO ftpsessions (user, ip_addr, timestamp) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')" logdb',
8565 'SQLLog EXIT session_end',
8566 ],
8567 },
8568 };
8569
8570 my ($port, $config_user, $config_group) = config_write($config_file, $config);
8571
8572 # Open pipes, for use between the parent and child processes. Specifically,
8573 # the child will indicate when it's done with its test by writing a message
8574 # to the parent.
8575 my ($rfh, $wfh);
8576 unless (pipe($rfh, $wfh)) {
8577 die("Can't open pipe: $!");
8578 }
8579
8580 my $ex;
8581
8582 # Fork child
8583 $self->handle_sigchld();
8584 defined(my $pid = fork()) or die("Can't fork: $!");
8585 if ($pid) {
8586 eval {
8587 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
8588
8589 $client->login($user, $passwd);
8590
8591 my $resp_msgs = $client->response_msgs();
8592 my $nmsgs = scalar(@$resp_msgs);
8593
8594 my $expected;
8595
8596 $expected = 1;
8597 $self->assert($expected == $nmsgs,
8598 test_msg("Expected $expected, got $nmsgs"));
8599
8600 $expected = "User proftpd logged in";
8601 $self->assert($expected eq $resp_msgs->[0],
8602 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
8603
8604 };
8605
8606 if ($@) {
8607 $ex = $@;
8608 }
8609
8610 $wfh->print("done\n");
8611 $wfh->flush();
8612
8613 } else {
8614 eval { server_wait($config_file, $rfh) };
8615 if ($@) {
8616 warn($@);
8617 exit 1;
8618 }
8619
8620 exit 0;
8621 }
8622
8623 # Stop server
8624 server_stop($pid_file);
8625
8626 $self->assert_child_ok($pid);
8627
8628 if ($ex) {
8629 test_append_logfile($log_file, $ex);
8630 unlink($log_file);
8631
8632 die($ex);
8633 }
8634
8635 my ($login, $ip_addr, $timestamp) = get_sessions($userdb_file,
8636 "user = \'$user\'");
8637
8638 $self->assert(!defined($login),
8639 test_msg("Expected undef, got '$login'"));
8640
8641 $self->assert(!defined($ip_addr),
8642 test_msg("Expected undef, got '$ip_addr'"));
8643
8644 $self->assert(!defined($timestamp),
8645 test_msg("Expected undef, got '$timestamp'"));
8646
8647 ($login, $ip_addr, $timestamp) = get_sessions($logdb_file,
8648 "user = \'$user\'");
8649
8650 my $expected;
8651
8652 $expected = $user;
8653 $self->assert($expected eq $login,
8654 test_msg("Expected '$expected', got '$login'"));
8655
8656 $expected = '127.0.0.1';
8657 $self->assert($expected eq $ip_addr,
8658 test_msg("Expected '$expected', got '$ip_addr'"));
8659
8660 $expected = '\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}';
8661 $self->assert(qr/$expected/, $timestamp,
8662 test_msg("Expected '$expected', got '$timestamp'"));
8663
8664 unlink($log_file);
8665 }
8666
8667 sub sql_sqllog_vars_H_L_matching_server_bug3620 {
8668 my $self = shift;
8669 my $tmpdir = $self->{tmpdir};
8670 my $setup = test_setup($tmpdir, 'sqlite');
8671
8672 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
8673
8674 # Build up sqlite3 command to create users, groups tables and populate them
8675 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
8676
8677 if (open(my $fh, "> $db_script")) {
8678 print $fh <<EOS;
8679 CREATE TABLE ftpsessions (
8680 user TEXT,
8681 ip_addr TEXT,
8682 rename_from TEXT
8683 );
8684 EOS
8685
8686 unless (close($fh)) {
8687 die("Can't write $db_script: $!");
8688 }
8689
8690 } else {
8691 die("Can't open $db_script: $!");
8692 }
8693
8694 my $cmd = "sqlite3 $db_file < $db_script";
8695 build_db($cmd, $db_script);
8696
8697 # Make sure that, if we're running as root, the database file has
8698 # the permissions/privs set for use by proftpd
8699 if ($< == 0) {
8700 unless (chmod(0666, $db_file)) {
8701 die("Can't set perms on $db_file to 0666: $!");
8702 }
8703 }
8704
8705 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
8706 if (open(my $fh, "> $src_file")) {
8707 close($fh);
8708
8709 } else {
8710 die("Can't open $src_file: $!");
8711 }
8712
8713 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
8714
8715 my $config = {
8716 PidFile => $setup->{pid_file},
8717 ScoreboardFile => $setup->{scoreboard_file},
8718 SystemLog => $setup->{log_file},
8719 TraceLog => $setup->{log_file},
8720 Trace => 'jot:20 sql:20',
8721
8722 AuthUserFile => $setup->{auth_user_file},
8723 AuthGroupFile => $setup->{auth_group_file},
8724 AuthOrder => 'mod_auth_file.c',
8725
8726 DefaultServer => 'off',
8727 SocketBindTight => 'off',
8728 Port => '0',
8729
8730 IfModules => {
8731 'mod_delay.c' => {
8732 DelayEngine => 'off',
8733 },
8734 },
8735 };
8736
8737 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
8738 $config);
8739
8740 my $real_addr = Net::Address::IP::Local->public_ipv4;
8741 my $real_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
8742 my $vhost_addr = '0.0.0.0';
8743
8744 if (open(my $fh, ">> $setup->{config_file}")) {
8745 print $fh <<EOC;
8746 <VirtualHost $vhost_addr>
8747 ServerName "DefaultServer VHost"
8748 Port $real_port
8749 DefaultServer on
8750
8751 AuthUserFile $setup->{auth_user_file}
8752 AuthGroupFile $setup->{auth_group_file}
8753 AuthOrder mod_auth_file.c
8754
8755 RequireValidShell off
8756 WtmpLog off
8757
8758 <IfModule mod_sql.c>
8759 SQLEngine log
8760 SQLBackend sqlite3
8761 SQLConnectInfo $db_file
8762 SQLLogFile $setup->{log_file}
8763 SQLNamedQuery session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, rename_from) VALUES ('%u', '%L', '%H')"
8764 SQLLog PASS session_start
8765 </IfModule>
8766 </VirtualHost>
8767 EOC
8768 unless (close($fh)) {
8769 die("Can't write $setup->{config_file}: $!");
8770 }
8771
8772 } else {
8773 die("Can't open $setup->{config_file}: $!");
8774 }
8775
8776 # Open pipes, for use between the parent and child processes. Specifically,
8777 # the child will indicate when it's done with its test by writing a message
8778 # to the parent.
8779 my ($rfh, $wfh);
8780 unless (pipe($rfh, $wfh)) {
8781 die("Can't open pipe: $!");
8782 }
8783
8784 my $ex;
8785
8786 # Fork child
8787 $self->handle_sigchld();
8788 defined(my $pid = fork()) or die("Can't fork: $!");
8789 if ($pid) {
8790 eval {
8791 my $client = ProFTPD::TestSuite::FTP->new($real_addr, $real_port);
8792 $client->login($setup->{user}, $setup->{passwd});
8793 $client->quit();
8794 };
8795 if ($@) {
8796 $ex = $@;
8797 }
8798
8799 $wfh->print("done\n");
8800 $wfh->flush();
8801
8802 } else {
8803 eval { server_wait($setup->{config_file}, $rfh) };
8804 if ($@) {
8805 warn($@);
8806 exit 1;
8807 }
8808
8809 exit 0;
8810 }
8811
8812 # Stop server
8813 server_stop($setup->{pid_file});
8814 $self->assert_child_ok($pid);
8815
8816 if ($ex) {
8817 test_cleanup($setup->{log_file}, $ex);
8818 }
8819
8820 eval {
8821 my ($login, $ip_addr, $sess_vhost_addr) = get_renames($db_file, "user = \'$setup->{user}\'");
8822
8823 my $expected = $setup->{user};
8824 $self->assert($expected eq $login,
8825 test_msg("Expected '$expected', got '$login'"));
8826
8827 $expected = $real_addr;
8828 $self->assert($expected eq $ip_addr,
8829 test_msg("Expected '$expected', got '$ip_addr'"));
8830
8831 $expected = $vhost_addr;
8832 $self->assert($expected eq $sess_vhost_addr,
8833 test_msg("Expected '$expected', got '$sess_vhost_addr'"));
8834 };
8835 if ($@) {
8836 $ex = $@;
8837 }
8838
8839 test_cleanup($setup->{log_file}, $ex);
8840 }
8841
8842 sub sql_sqllog_vars_H_L_default_server_bug3620 {
8843 my $self = shift;
8844 my $tmpdir = $self->{tmpdir};
8845 my $setup = test_setup($tmpdir, 'sqlite');
8846
8847 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
8848
8849 # Build up sqlite3 command to create users, groups tables and populate them
8850 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
8851
8852 if (open(my $fh, "> $db_script")) {
8853 print $fh <<EOS;
8854 CREATE TABLE ftpsessions (
8855 user TEXT,
8856 ip_addr TEXT,
8857 rename_from TEXT
8858 );
8859 EOS
8860
8861 unless (close($fh)) {
8862 die("Can't write $db_script: $!");
8863 }
8864
8865 } else {
8866 die("Can't open $db_script: $!");
8867 }
8868
8869 my $cmd = "sqlite3 $db_file < $db_script";
8870 build_db($cmd, $db_script);
8871
8872 # Make sure that, if we're running as root, the database file has
8873 # the permissions/privs set for use by proftpd
8874 if ($< == 0) {
8875 unless (chmod(0666, $db_file)) {
8876 die("Can't set perms on $db_file to 0666: $!");
8877 }
8878 }
8879
8880 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
8881 if (open(my $fh, "> $src_file")) {
8882 close($fh);
8883
8884 } else {
8885 die("Can't open $src_file: $!");
8886 }
8887
8888 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
8889
8890 my $config = {
8891 PidFile => $setup->{pid_file},
8892 ScoreboardFile => $setup->{scoreboard_file},
8893 SystemLog => $setup->{log_file},
8894 TraceLog => $setup->{log_file},
8895 Trace => 'jot:20 sql:20',
8896
8897 AuthUserFile => $setup->{auth_user_file},
8898 AuthGroupFile => $setup->{auth_group_file},
8899 AuthOrder => 'mod_auth_file.c',
8900
8901 DefaultServer => 'off',
8902 SocketBindTight => 'off',
8903 Port => '0',
8904
8905 IfModules => {
8906 'mod_delay.c' => {
8907 DelayEngine => 'off',
8908 },
8909
8910 'mod_sql.c' => {
8911 SQLEngine => 'log',
8912 SQLBackend => 'sqlite3',
8913 SQLConnectInfo => $db_file,
8914 SQLLogFile => $setup->{log_file},
8915 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, rename_from) VALUES (\'%u\', \'%L\', \'%H\')"',
8916 SQLLog => 'PASS session_start',
8917 },
8918 },
8919 };
8920
8921 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
8922 $config);
8923
8924 my $real_addr = Net::Address::IP::Local->public_ipv4;
8925 my $real_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
8926 my $vhost_addr = '0.0.0.0';
8927
8928 if (open(my $fh, ">> $setup->{config_file}")) {
8929 print $fh <<EOC;
8930
8931 <VirtualHost $vhost_addr>
8932 ServerName "DefaultServer VHost"
8933 Port $real_port
8934 DefaultServer on
8935
8936 AuthUserFile $setup->{auth_user_file}
8937 AuthGroupFile $setup->{auth_group_file}
8938 AuthOrder mod_auth_file.c
8939
8940 RequireValidShell off
8941 WtmpLog off
8942
8943 <IfModule mod_sql.c>
8944 SQLEngine log
8945 SQLBackend sqlite3
8946 SQLConnectInfo $db_file
8947 SQLLogFile $setup->{log_file}
8948 SQLNamedQuery session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, rename_from) VALUES (\'%u\', \'%L\', \'%H\')"
8949 SQLLog PASS session_start
8950 </IfModule>
8951 </VirtualHost>
8952 EOC
8953 unless (close($fh)) {
8954 die("Can't write $setup->{config_file}: $!");
8955 }
8956
8957 } else {
8958 die("Can't open $setup->{config_file}: $!");
8959 }
8960
8961 # Open pipes, for use between the parent and child processes. Specifically,
8962 # the child will indicate when it's done with its test by writing a message
8963 # to the parent.
8964 my ($rfh, $wfh);
8965 unless (pipe($rfh, $wfh)) {
8966 die("Can't open pipe: $!");
8967 }
8968
8969 my $ex;
8970
8971 # Fork child
8972 $self->handle_sigchld();
8973 defined(my $pid = fork()) or die("Can't fork: $!");
8974 if ($pid) {
8975 eval {
8976 my $client = ProFTPD::TestSuite::FTP->new($real_addr, $real_port);
8977 $client->login($setup->{user}, $setup->{passwd});
8978 $client->quit();
8979 };
8980 if ($@) {
8981 $ex = $@;
8982 }
8983
8984 $wfh->print("done\n");
8985 $wfh->flush();
8986
8987 } else {
8988 eval { server_wait($setup->{config_file}, $rfh) };
8989 if ($@) {
8990 warn($@);
8991 exit 1;
8992 }
8993
8994 exit 0;
8995 }
8996
8997 # Stop server
8998 server_stop($setup->{pid_file});
8999 $self->assert_child_ok($pid);
9000
9001 if ($ex) {
9002 test_cleanup($setup->{log_file}, $ex);
9003 }
9004
9005 eval {
9006 my ($login, $ip_addr, $sess_addr) = get_renames($db_file, "user = \'$setup->{user}\'");
9007
9008 my $expected = $setup->{user};
9009 $self->assert($expected eq $login, "Expected '$expected', got '$login'");
9010
9011 $expected = $real_addr;
9012 $self->assert($expected eq $ip_addr,
9013 "Expected '$expected', got '$ip_addr'");
9014
9015 $expected = $vhost_addr;
9016 $self->assert($expected eq $sess_addr,
9017 "Expected '$expected', got '$sess_addr'");
9018 };
9019 if ($@) {
9020 $ex = $@;
9021 }
9022
9023 test_cleanup($setup->{log_file}, $ex);
9024 }
9025
9026 sub sql_sqllog_exit_ifuser {
9027 my $self = shift;
9028 my $tmpdir = $self->{tmpdir};
9029
9030 my $config_file = "$tmpdir/sqlite.conf";
9031 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
9032 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
9033
9034 my $log_file = test_get_logfile();
9035
9036 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
9037 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
9038
9039 my $user = 'proftpd';
9040 my $passwd = 'test';
9041 my $group = 'ftpd';
9042 my $home_dir = File::Spec->rel2abs($tmpdir);
9043 my $uid = 500;
9044 my $gid = 500;
9045
9046 # Make sure that, if we're running as root, that the home directory has
9047 # permissions/privs set for the account we create
9048 if ($< == 0) {
9049 unless (chmod(0755, $home_dir)) {
9050 die("Can't set perms on $home_dir to 0755: $!");
9051 }
9052
9053 unless (chown($uid, $gid, $home_dir)) {
9054 die("Can't set owner of $home_dir to $uid/$gid: $!");
9055 }
9056 }
9057
9058 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
9059 '/bin/bash');
9060 auth_group_write($auth_group_file, $group, $gid, $user);
9061
9062 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9063
9064 # Build up sqlite3 command to create users, groups tables and populate them
9065 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9066
9067 if (open(my $fh, "> $db_script")) {
9068 print $fh <<EOS;
9069 CREATE TABLE ftpsessions (
9070 user TEXT,
9071 ip_addr TEXT
9072 );
9073 EOS
9074
9075 unless (close($fh)) {
9076 die("Can't write $db_script: $!");
9077 }
9078
9079 } else {
9080 die("Can't open $db_script: $!");
9081 }
9082
9083 my $cmd = "sqlite3 $db_file < $db_script";
9084 build_db($cmd, $db_script);
9085
9086 # Make sure that, if we're running as root, the database file has
9087 # the permissions/privs set for use by proftpd
9088 if ($< == 0) {
9089 unless (chmod(0666, $db_file)) {
9090 die("Can't set perms on $db_file to 0666: $!");
9091 }
9092 }
9093
9094 my $config = {
9095 PidFile => $pid_file,
9096 ScoreboardFile => $scoreboard_file,
9097 SystemLog => $log_file,
9098
9099 AuthUserFile => $auth_user_file,
9100 AuthGroupFile => $auth_group_file,
9101 AuthOrder => 'mod_auth_file.c',
9102
9103 IfModules => {
9104 'mod_delay.c' => {
9105 DelayEngine => 'off',
9106 },
9107
9108 'mod_sql.c' => {
9109 SQLEngine => 'log',
9110 SQLBackend => 'sqlite3',
9111 SQLConnectInfo => $db_file,
9112 SQLLogFile => $log_file,
9113 SQLNamedQuery => 'logout FREEFORM "INSERT INTO ftpsessions (user, ip_addr) VALUES (\'%u\', \'%L\')"',
9114 },
9115 },
9116 };
9117
9118 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9119
9120 if (open(my $fh, ">> $config_file")) {
9121 print $fh <<EOC;
9122 <IfModule mod_ifsession.c>
9123 <IfUser regex .*>
9124 SQLLog EXIT logout
9125 </IfUser>
9126 </IfModule>
9127 EOC
9128 unless (close($fh)) {
9129 die("Can't write $config_file: $!");
9130 }
9131
9132 } else {
9133 die("Can't open $config_file: $!");
9134 }
9135
9136 # Open pipes, for use between the parent and child processes. Specifically,
9137 # the child will indicate when it's done with its test by writing a message
9138 # to the parent.
9139 my ($rfh, $wfh);
9140 unless (pipe($rfh, $wfh)) {
9141 die("Can't open pipe: $!");
9142 }
9143
9144 my $ex;
9145
9146 # Fork child
9147 $self->handle_sigchld();
9148 defined(my $pid = fork()) or die("Can't fork: $!");
9149 if ($pid) {
9150 eval {
9151
9152 # First, just connect and quit, without logging in
9153 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9154 $client->quit();
9155
9156 # Then connect, login, and quit
9157 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9158 $client->login($user, $passwd);
9159 $client->quit();
9160 };
9161
9162 if ($@) {
9163 $ex = $@;
9164 }
9165
9166 $wfh->print("done\n");
9167 $wfh->flush();
9168
9169 } else {
9170 eval { server_wait($config_file, $rfh) };
9171 if ($@) {
9172 warn($@);
9173 exit 1;
9174 }
9175
9176 exit 0;
9177 }
9178
9179 # Stop server
9180 server_stop($pid_file);
9181
9182 $self->assert_child_ok($pid);
9183
9184 if ($ex) {
9185 test_append_logfile($log_file, $ex);
9186 unlink($log_file);
9187
9188 die($ex);
9189 }
9190
9191 my $query = "SELECT user, ip_addr FROM ftpsessions";
9192 $cmd = "sqlite3 $db_file \"$query\"";
9193
9194 if ($ENV{TEST_VERBOSE}) {
9195 print STDERR "Executing sqlite3: $cmd\n";
9196 }
9197
9198 my @res = `$cmd`;
9199 my $res = join('', @res);
9200 chomp($res);
9201
9202 my ($login, $ip_addr) = split(/\|/, $res);
9203
9204 my $expected;
9205
9206 $expected = $user;
9207 $self->assert($expected eq $login,
9208 test_msg("Expected '$expected', got '$login'"));
9209
9210 $expected = '127.0.0.1';
9211 $self->assert($expected eq $ip_addr,
9212 test_msg("Expected '$expected', got '$ip_addr'"));
9213
9214 unlink($log_file);
9215 }
9216
9217 sub sql_sqllog_exit_ifgroup {
9218 my $self = shift;
9219 my $tmpdir = $self->{tmpdir};
9220
9221 my $config_file = "$tmpdir/sqlite.conf";
9222 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
9223 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
9224
9225 my $log_file = test_get_logfile();
9226
9227 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
9228 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
9229
9230 my $user = 'proftpd';
9231 my $passwd = 'test';
9232 my $group = 'ftpd';
9233 my $home_dir = File::Spec->rel2abs($tmpdir);
9234 my $uid = 500;
9235 my $gid = 500;
9236
9237 # Make sure that, if we're running as root, that the home directory has
9238 # permissions/privs set for the account we create
9239 if ($< == 0) {
9240 unless (chmod(0755, $home_dir)) {
9241 die("Can't set perms on $home_dir to 0755: $!");
9242 }
9243
9244 unless (chown($uid, $gid, $home_dir)) {
9245 die("Can't set owner of $home_dir to $uid/$gid: $!");
9246 }
9247 }
9248
9249 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
9250 '/bin/bash');
9251 auth_group_write($auth_group_file, $group, $gid, $user);
9252
9253 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9254
9255 # Build up sqlite3 command to create users, groups tables and populate them
9256 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9257
9258 if (open(my $fh, "> $db_script")) {
9259 print $fh <<EOS;
9260 CREATE TABLE ftpsessions (
9261 user TEXT,
9262 ip_addr TEXT
9263 );
9264 EOS
9265
9266 unless (close($fh)) {
9267 die("Can't write $db_script: $!");
9268 }
9269
9270 } else {
9271 die("Can't open $db_script: $!");
9272 }
9273
9274 my $cmd = "sqlite3 $db_file < $db_script";
9275 build_db($cmd, $db_script);
9276
9277 # Make sure that, if we're running as root, the database file has
9278 # the permissions/privs set for use by proftpd
9279 if ($< == 0) {
9280 unless (chmod(0666, $db_file)) {
9281 die("Can't set perms on $db_file to 0666: $!");
9282 }
9283 }
9284
9285 my $config = {
9286 PidFile => $pid_file,
9287 ScoreboardFile => $scoreboard_file,
9288 SystemLog => $log_file,
9289
9290 AuthUserFile => $auth_user_file,
9291 AuthGroupFile => $auth_group_file,
9292 AuthOrder => 'mod_auth_file.c',
9293
9294 IfModules => {
9295 'mod_delay.c' => {
9296 DelayEngine => 'off',
9297 },
9298
9299 'mod_sql.c' => {
9300 SQLEngine => 'log',
9301 SQLBackend => 'sqlite3',
9302 SQLConnectInfo => $db_file,
9303 SQLLogFile => $log_file,
9304 SQLNamedQuery => 'logout FREEFORM "INSERT INTO ftpsessions (user, ip_addr) VALUES (\'%u\', \'%L\')"',
9305 },
9306 },
9307 };
9308
9309 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9310
9311 if (open(my $fh, ">> $config_file")) {
9312 print $fh <<EOC;
9313 <IfModule mod_ifsession.c>
9314 <IfGroup regex .*>
9315 SQLLog EXIT logout
9316 </IfGroup>
9317 </IfModule>
9318 EOC
9319 unless (close($fh)) {
9320 die("Can't write $config_file: $!");
9321 }
9322
9323 } else {
9324 die("Can't open $config_file: $!");
9325 }
9326
9327 # Open pipes, for use between the parent and child processes. Specifically,
9328 # the child will indicate when it's done with its test by writing a message
9329 # to the parent.
9330 my ($rfh, $wfh);
9331 unless (pipe($rfh, $wfh)) {
9332 die("Can't open pipe: $!");
9333 }
9334
9335 my $ex;
9336
9337 # Fork child
9338 $self->handle_sigchld();
9339 defined(my $pid = fork()) or die("Can't fork: $!");
9340 if ($pid) {
9341 eval {
9342
9343 # First, just connect and quit, without logging in
9344 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9345 $client->quit();
9346
9347 # Then connect, login, and quit
9348 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9349 $client->login($user, $passwd);
9350 $client->quit();
9351 };
9352
9353 if ($@) {
9354 $ex = $@;
9355 }
9356
9357 $wfh->print("done\n");
9358 $wfh->flush();
9359
9360 } else {
9361 eval { server_wait($config_file, $rfh) };
9362 if ($@) {
9363 warn($@);
9364 exit 1;
9365 }
9366
9367 exit 0;
9368 }
9369
9370 # Stop server
9371 server_stop($pid_file);
9372
9373 $self->assert_child_ok($pid);
9374
9375 if ($ex) {
9376 test_append_logfile($log_file, $ex);
9377 unlink($log_file);
9378
9379 die($ex);
9380 }
9381
9382 my $query = "SELECT user, ip_addr FROM ftpsessions";
9383 $cmd = "sqlite3 $db_file \"$query\"";
9384
9385 if ($ENV{TEST_VERBOSE}) {
9386 print STDERR "Executing sqlite3: $cmd\n";
9387 }
9388
9389 my @res = `$cmd`;
9390 my $res = join('', @res);
9391 chomp($res);
9392
9393 my ($login, $ip_addr) = split(/\|/, $res);
9394
9395 my $expected;
9396
9397 $expected = $user;
9398 $self->assert($expected eq $login,
9399 test_msg("Expected '$expected', got '$login'"));
9400
9401 $expected = '127.0.0.1';
9402 $self->assert($expected eq $ip_addr,
9403 test_msg("Expected '$expected', got '$ip_addr'"));
9404
9405 unlink($log_file);
9406 }
9407
9408 sub sql_sqllog_multi_pass_ifclass_bug4025 {
9409 my $self = shift;
9410 my $tmpdir = $self->{tmpdir};
9411
9412 my $config_file = "$tmpdir/sqlite.conf";
9413 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
9414 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
9415
9416 my $log_file = test_get_logfile();
9417
9418 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
9419 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
9420
9421 my $user = 'proftpd';
9422 my $passwd = 'test';
9423 my $group = 'ftpd';
9424 my $home_dir = File::Spec->rel2abs($tmpdir);
9425 my $uid = 500;
9426 my $gid = 500;
9427
9428 # Make sure that, if we're running as root, that the home directory has
9429 # permissions/privs set for the account we create
9430 if ($< == 0) {
9431 unless (chmod(0755, $home_dir)) {
9432 die("Can't set perms on $home_dir to 0755: $!");
9433 }
9434
9435 unless (chown($uid, $gid, $home_dir)) {
9436 die("Can't set owner of $home_dir to $uid/$gid: $!");
9437 }
9438 }
9439
9440 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
9441 '/bin/bash');
9442 auth_group_write($auth_group_file, $group, $gid, $user);
9443
9444 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9445
9446 # Build up sqlite3 command to create users, groups tables and populate them
9447 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9448
9449 if (open(my $fh, "> $db_script")) {
9450 print $fh <<EOS;
9451 CREATE TABLE ftpsessions (
9452 user TEXT,
9453 ip_addr TEXT
9454 );
9455 EOS
9456
9457 unless (close($fh)) {
9458 die("Can't write $db_script: $!");
9459 }
9460
9461 } else {
9462 die("Can't open $db_script: $!");
9463 }
9464
9465 my $cmd = "sqlite3 $db_file < $db_script";
9466 build_db($cmd, $db_script);
9467
9468 # Make sure that, if we're running as root, the database file has
9469 # the permissions/privs set for use by proftpd
9470 if ($< == 0) {
9471 unless (chmod(0666, $db_file)) {
9472 die("Can't set perms on $db_file to 0666: $!");
9473 }
9474 }
9475
9476 my $config = {
9477 PidFile => $pid_file,
9478 ScoreboardFile => $scoreboard_file,
9479 SystemLog => $log_file,
9480 TraceLog => $log_file,
9481 Trace => 'DEFAULT:10 ifsession:20',
9482
9483 AuthUserFile => $auth_user_file,
9484 AuthGroupFile => $auth_group_file,
9485 AuthOrder => 'mod_auth_file.c',
9486
9487 IfModules => {
9488 'mod_delay.c' => {
9489 DelayEngine => 'off',
9490 },
9491
9492 'mod_sql.c' => [
9493 'SQLEngine log',
9494 'SQLBackend sqlite3',
9495 "SQLConnectInfo $db_file",
9496 "SQLLogFile $log_file",
9497 'SQLNamedQuery login_ip FREEFORM "INSERT INTO ftpsessions (ip_addr) VALUES (\'%L\')"',
9498 'SQLNamedQuery login_user FREEFORM "INSERT INTO ftpsessions (user) VALUES (\'%u\')"',
9499 ],
9500 },
9501 };
9502
9503 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9504
9505 if (open(my $fh, ">> $config_file")) {
9506 print $fh <<EOC;
9507 <Class test>
9508 From 127.0.0.1
9509 </Class>
9510
9511 <IfModule mod_ifsession.c>
9512 <IfClass test>
9513 SQLLog PASS login_ip
9514 SQLLog PASS login_user
9515 </IfClass>
9516 </IfModule>
9517 EOC
9518 unless (close($fh)) {
9519 die("Can't write $config_file: $!");
9520 }
9521
9522 } else {
9523 die("Can't open $config_file: $!");
9524 }
9525
9526 # Open pipes, for use between the parent and child processes. Specifically,
9527 # the child will indicate when it's done with its test by writing a message
9528 # to the parent.
9529 my ($rfh, $wfh);
9530 unless (pipe($rfh, $wfh)) {
9531 die("Can't open pipe: $!");
9532 }
9533
9534 my $ex;
9535
9536 # Fork child
9537 $self->handle_sigchld();
9538 defined(my $pid = fork()) or die("Can't fork: $!");
9539 if ($pid) {
9540 eval {
9541 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9542 $client->login($user, $passwd);
9543 $client->quit();
9544 };
9545
9546 if ($@) {
9547 $ex = $@;
9548 }
9549
9550 $wfh->print("done\n");
9551 $wfh->flush();
9552
9553 } else {
9554 eval { server_wait($config_file, $rfh) };
9555 if ($@) {
9556 warn($@);
9557 exit 1;
9558 }
9559
9560 exit 0;
9561 }
9562
9563 # Stop server
9564 server_stop($pid_file);
9565
9566 $self->assert_child_ok($pid);
9567
9568 if ($ex) {
9569 test_append_logfile($log_file, $ex);
9570 unlink($log_file);
9571
9572 die($ex);
9573 }
9574
9575 my $query = "SELECT user, ip_addr FROM ftpsessions";
9576 $cmd = "sqlite3 $db_file \"$query\"";
9577
9578 if ($ENV{TEST_VERBOSE}) {
9579 print STDERR "Executing sqlite3: $cmd\n";
9580 }
9581
9582 my @res = `$cmd`;
9583 my $res = join('', @res);
9584 $res =~ s/\n//g;
9585 chomp($res);
9586
9587 my ($login, $ip_addr) = split(/\|+/, $res);
9588
9589 my $expected;
9590
9591 $expected = $user;
9592 $self->assert($expected eq $login,
9593 test_msg("Expected '$expected', got '$login'"));
9594
9595 $expected = '127.0.0.1';
9596 $self->assert($expected eq $ip_addr,
9597 test_msg("Expected '$expected', got '$ip_addr'"));
9598
9599 unlink($log_file);
9600 }
9601
9602 sub sql_opt_no_disconnect_on_error_with_extlog_bug3633 {
9603 my $self = shift;
9604 my $tmpdir = $self->{tmpdir};
9605
9606 my $config_file = "$tmpdir/sqlite.conf";
9607 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
9608 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
9609
9610 my $log_file = test_get_logfile();
9611
9612 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
9613 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
9614
9615 my $user = 'proftpd';
9616 my $passwd = 'test';
9617 my $group = 'ftpd';
9618 my $home_dir = File::Spec->rel2abs($tmpdir);
9619 my $uid = 500;
9620 my $gid = 500;
9621
9622 # Make sure that, if we're running as root, that the home directory has
9623 # permissions/privs set for the account we create
9624 if ($< == 0) {
9625 unless (chmod(0755, $home_dir)) {
9626 die("Can't set perms on $home_dir to 0755: $!");
9627 }
9628
9629 unless (chown($uid, $gid, $home_dir)) {
9630 die("Can't set owner of $home_dir to $uid/$gid: $!");
9631 }
9632 }
9633
9634 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
9635 '/bin/bash');
9636 auth_group_write($auth_group_file, $group, $gid, $user);
9637
9638 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9639
9640 # Build up sqlite3 command to create users, groups tables and populate them
9641 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9642
9643 if (open(my $fh, "> $db_script")) {
9644 print $fh <<EOS;
9645 CREATE TABLE ftpsessions (
9646 user TEXT,
9647 ip_addr TEXT,
9648 timestamp TEXT
9649 );
9650 EOS
9651
9652 unless (close($fh)) {
9653 die("Can't write $db_script: $!");
9654 }
9655
9656 } else {
9657 die("Can't open $db_script: $!");
9658 }
9659
9660 my $cmd = "sqlite3 $db_file < $db_script";
9661 build_db($cmd, $db_script);
9662
9663 # Make sure that, if we're running as root, the database file has
9664 # the permissions/privs set for use by proftpd
9665 if ($< == 0) {
9666 unless (chmod(0666, $db_file)) {
9667 die("Can't set perms on $db_file to 0666: $!");
9668 }
9669 }
9670
9671 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
9672 if (open(my $fh, "> $test_file")) {
9673 print $fh "Hello, World!\n";
9674 unless (close($fh)) {
9675 die("Can't write $test_file: $!");
9676 }
9677
9678 } else {
9679 die("Can't open $test_file: $!");
9680 }
9681
9682 my $ext_log = File::Spec->rel2abs("$tmpdir/custom.log");
9683
9684 my $config = {
9685 PidFile => $pid_file,
9686 ScoreboardFile => $scoreboard_file,
9687 SystemLog => $log_file,
9688 TraceLog => $log_file,
9689 Trace => 'DEFAULT:10 sql:20',
9690
9691 AuthUserFile => $auth_user_file,
9692 AuthGroupFile => $auth_group_file,
9693 AuthOrder => 'mod_auth_file.c',
9694
9695 LogFormat => 'custom "%m %f"',
9696 ExtendedLog => "$ext_log ALL custom",
9697
9698 IfModules => {
9699 'mod_delay.c' => {
9700 DelayEngine => 'off',
9701 },
9702
9703 'mod_sql.c' => {
9704 SQLEngine => 'log',
9705 SQLBackend => 'sqlite3',
9706 SQLConnectInfo => $db_file,
9707 SQLLogFile => $log_file,
9708 SQLNamedQuery => 'log_dele FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ts) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')"',
9709 SQLLog => 'DELE log_dele',
9710
9711 SQLOptions => 'noDisconnectOnError',
9712 },
9713 },
9714 };
9715
9716 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9717
9718 # Open pipes, for use between the parent and child processes. Specifically,
9719 # the child will indicate when it's done with its test by writing a message
9720 # to the parent.
9721 my ($rfh, $wfh);
9722 unless (pipe($rfh, $wfh)) {
9723 die("Can't open pipe: $!");
9724 }
9725
9726 my $ex;
9727
9728 # Fork child
9729 $self->handle_sigchld();
9730 defined(my $pid = fork()) or die("Can't fork: $!");
9731 if ($pid) {
9732 eval {
9733 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9734
9735 $client->login($user, $passwd);
9736 $client->dele('test.txt');
9737 $client->quit();
9738 };
9739
9740 if ($@) {
9741 $ex = $@;
9742 }
9743
9744 $wfh->print("done\n");
9745 $wfh->flush();
9746
9747 } else {
9748 eval { server_wait($config_file, $rfh) };
9749 if ($@) {
9750 warn($@);
9751 exit 1;
9752 }
9753
9754 exit 0;
9755 }
9756
9757 # Stop server
9758 server_stop($pid_file);
9759
9760 $self->assert_child_ok($pid);
9761
9762 if ($ex) {
9763 test_append_logfile($log_file, $ex);
9764 unlink($log_file);
9765
9766 die($ex);
9767 }
9768
9769 # Now, read in the ExtendedLog, and see whether the %f variable was
9770 # properly written out.
9771 if (open(my $fh, "< $ext_log")) {
9772 my $ok = 0;
9773
9774 while (my $line = <$fh>) {
9775 chomp($line);
9776
9777 # We're only interested in the DELE log line
9778 unless ($line =~ /^DELE (.*)$/i) {
9779 next;
9780 }
9781
9782 my $name = $1;
9783
9784 if ($^O eq 'darwin') {
9785 # MacOSX-specific hack
9786 $test_file = '/private' . $test_file;
9787 }
9788
9789 my $expected = $test_file;
9790 $self->assert($expected eq $name,
9791 test_msg("Expected '$expected', got '$name'"));
9792
9793 $ok = 1;
9794 }
9795
9796 close($fh);
9797
9798 $self->assert($ok, test_msg("Expected ExtendedLog messages not found"));
9799
9800 } else {
9801 die("Can't read $ext_log: $!");
9802 }
9803
9804 unlink($log_file);
9805 }
9806
9807 sub sql_sqllog_ignore_errors_bad_table_bug3692 {
9808 my $self = shift;
9809 my $tmpdir = $self->{tmpdir};
9810
9811 my $config_file = "$tmpdir/sqlite.conf";
9812 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
9813 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
9814
9815 my $log_file = test_get_logfile();
9816
9817 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
9818 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
9819
9820 my $user = 'proftpd';
9821 my $passwd = 'test';
9822 my $group = 'ftpd';
9823 my $home_dir = File::Spec->rel2abs($tmpdir);
9824 my $uid = 500;
9825 my $gid = 500;
9826
9827 # Make sure that, if we're running as root, that the home directory has
9828 # permissions/privs set for the account we create
9829 if ($< == 0) {
9830 unless (chmod(0755, $home_dir)) {
9831 die("Can't set perms on $home_dir to 0755: $!");
9832 }
9833
9834 unless (chown($uid, $gid, $home_dir)) {
9835 die("Can't set owner of $home_dir to $uid/$gid: $!");
9836 }
9837 }
9838
9839 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
9840 '/bin/bash');
9841 auth_group_write($auth_group_file, $group, $gid, $user);
9842
9843 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9844
9845 # Build up sqlite3 command to create users, groups tables and populate them
9846 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9847
9848 if (open(my $fh, "> $db_script")) {
9849 print $fh <<EOS;
9850 CREATE TABLE ftpsessions (
9851 user TEXT,
9852 ip_addr TEXT,
9853 timestamp TEXT
9854 );
9855 EOS
9856
9857 unless (close($fh)) {
9858 die("Can't write $db_script: $!");
9859 }
9860
9861 } else {
9862 die("Can't open $db_script: $!");
9863 }
9864
9865 my $cmd = "sqlite3 $db_file < $db_script";
9866 build_db($cmd, $db_script);
9867
9868 # Make sure that, if we're running as root, the database file has
9869 # the permissions/privs set for use by proftpd
9870 if ($< == 0) {
9871 unless (chmod(0666, $db_file)) {
9872 die("Can't set perms on $db_file to 0666: $!");
9873 }
9874 }
9875
9876 my $config = {
9877 PidFile => $pid_file,
9878 ScoreboardFile => $scoreboard_file,
9879 SystemLog => $log_file,
9880
9881 AuthUserFile => $auth_user_file,
9882 AuthGroupFile => $auth_group_file,
9883 AuthOrder => 'mod_auth_file.c',
9884
9885 IfModules => {
9886 'mod_delay.c' => {
9887 DelayEngine => 'off',
9888 },
9889
9890 'mod_sql.c' => {
9891 SQLEngine => 'log',
9892 SQLBackend => 'sqlite3',
9893 SQLConnectInfo => $db_file,
9894 SQLLogFile => $log_file,
9895 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ts) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')"',
9896 SQLLog => 'PASS session_start IGNORE_ERRORS',
9897 },
9898 },
9899 };
9900
9901 my ($port, $config_user, $config_group) = config_write($config_file, $config);
9902
9903 # Open pipes, for use between the parent and child processes. Specifically,
9904 # the child will indicate when it's done with its test by writing a message
9905 # to the parent.
9906 my ($rfh, $wfh);
9907 unless (pipe($rfh, $wfh)) {
9908 die("Can't open pipe: $!");
9909 }
9910
9911 my $ex;
9912
9913 # Fork child
9914 $self->handle_sigchld();
9915 defined(my $pid = fork()) or die("Can't fork: $!");
9916 if ($pid) {
9917 eval {
9918 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
9919
9920 $client->login($user, $passwd);
9921 };
9922
9923 if ($@) {
9924 $ex = $@;
9925 }
9926
9927 $wfh->print("done\n");
9928 $wfh->flush();
9929
9930 } else {
9931 eval { server_wait($config_file, $rfh) };
9932 if ($@) {
9933 warn($@);
9934 exit 1;
9935 }
9936
9937 exit 0;
9938 }
9939
9940 # Stop server
9941 server_stop($pid_file);
9942
9943 $self->assert_child_ok($pid);
9944
9945 if ($ex) {
9946 test_append_logfile($log_file, $ex);
9947 unlink($log_file);
9948
9949 die($ex);
9950 }
9951
9952 unlink($log_file);
9953 }
9954
9955 sub sql_sqllog_ignore_errors_bad_db_bug3692 {
9956 my $self = shift;
9957 my $tmpdir = $self->{tmpdir};
9958
9959 my $config_file = "$tmpdir/sqlite.conf";
9960 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
9961 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
9962
9963 my $log_file = test_get_logfile();
9964
9965 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
9966 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
9967
9968 my $user = 'proftpd';
9969 my $passwd = 'test';
9970 my $group = 'ftpd';
9971 my $home_dir = File::Spec->rel2abs($tmpdir);
9972 my $uid = 500;
9973 my $gid = 500;
9974
9975 # Make sure that, if we're running as root, that the home directory has
9976 # permissions/privs set for the account we create
9977 if ($< == 0) {
9978 unless (chmod(0755, $home_dir)) {
9979 die("Can't set perms on $home_dir to 0755: $!");
9980 }
9981
9982 unless (chown($uid, $gid, $home_dir)) {
9983 die("Can't set owner of $home_dir to $uid/$gid: $!");
9984 }
9985 }
9986
9987 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
9988 '/bin/bash');
9989 auth_group_write($auth_group_file, $group, $gid, $user);
9990
9991 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
9992
9993 # Build up sqlite3 command to create users, groups tables and populate them
9994 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
9995
9996 if (open(my $fh, "> $db_script")) {
9997 print $fh <<EOS;
9998 CREATE TABLE ftpsessions (
9999 user TEXT,
10000 ip_addr TEXT,
10001 timestamp TEXT
10002 );
10003 EOS
10004
10005 unless (close($fh)) {
10006 die("Can't write $db_script: $!");
10007 }
10008
10009 } else {
10010 die("Can't open $db_script: $!");
10011 }
10012
10013 my $cmd = "sqlite3 $db_file < $db_script";
10014 build_db($cmd, $db_script);
10015
10016 # Make sure that, if we're running as root, the database file has
10017 # the permissions/privs set for use by proftpd
10018 if ($< == 0) {
10019 unless (chmod(0666, $db_file)) {
10020 die("Can't set perms on $db_file to 0666: $!");
10021 }
10022 }
10023
10024 my $config = {
10025 PidFile => $pid_file,
10026 ScoreboardFile => $scoreboard_file,
10027 SystemLog => $log_file,
10028
10029 AuthUserFile => $auth_user_file,
10030 AuthGroupFile => $auth_group_file,
10031 AuthOrder => 'mod_auth_file.c',
10032
10033 IfModules => {
10034 'mod_delay.c' => {
10035 DelayEngine => 'off',
10036 },
10037
10038 'mod_sql.c' => {
10039 SQLEngine => 'log',
10040 SQLBackend => 'sqlite3',
10041 SQLConnectInfo => "/foobar/$db_file",
10042 SQLLogFile => $log_file,
10043 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ts) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')"',
10044 SQLLog => 'PASS session_start IGNORE_ERRORS',
10045 },
10046 },
10047 };
10048
10049 my ($port, $config_user, $config_group) = config_write($config_file, $config);
10050
10051 # Open pipes, for use between the parent and child processes. Specifically,
10052 # the child will indicate when it's done with its test by writing a message
10053 # to the parent.
10054 my ($rfh, $wfh);
10055 unless (pipe($rfh, $wfh)) {
10056 die("Can't open pipe: $!");
10057 }
10058
10059 my $ex;
10060
10061 # Fork child
10062 $self->handle_sigchld();
10063 defined(my $pid = fork()) or die("Can't fork: $!");
10064 if ($pid) {
10065 eval {
10066 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
10067
10068 $client->login($user, $passwd);
10069 };
10070
10071 if ($@) {
10072 $ex = $@;
10073 }
10074
10075 $wfh->print("done\n");
10076 $wfh->flush();
10077
10078 } else {
10079 eval { server_wait($config_file, $rfh) };
10080 if ($@) {
10081 warn($@);
10082 exit 1;
10083 }
10084
10085 exit 0;
10086 }
10087
10088 # Stop server
10089 server_stop($pid_file);
10090
10091 $self->assert_child_ok($pid);
10092
10093 if ($ex) {
10094 test_append_logfile($log_file, $ex);
10095 unlink($log_file);
10096
10097 die($ex);
10098 }
10099
10100 unlink($log_file);
10101 }
10102
10103 sub get_xfer_status {
10104 my $db_file = shift;
10105 my $where = shift;
10106
10107 my $sql = "SELECT user, ip_addr, xfer_status, xfer_path FROM ftpsessions";
10108 if ($where) {
10109 $sql .= " WHERE $where";
10110 }
10111 $sql .= " LIMIT 1";
10112
10113 my $cmd = "sqlite3 $db_file \"$sql\"";
10114
10115 if ($ENV{TEST_VERBOSE}) {
10116 print STDERR "Executing sqlite3: $cmd\n";
10117 }
10118
10119 my $res = join('', `$cmd`);
10120 chomp($res);
10121
10122 # The default sqlite3 delimiter is '|'
10123 return map { chomp($_); $_; } split(/\|/, $res);
10124 }
10125
10126 sub sql_sqllog_var_xfer_status_nonxfer {
10127 my $self = shift;
10128 my $tmpdir = $self->{tmpdir};
10129
10130 my $config_file = "$tmpdir/sqlite.conf";
10131 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
10132 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
10133
10134 my $log_file = test_get_logfile();
10135
10136 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
10137 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
10138
10139 my $user = 'proftpd';
10140 my $passwd = 'test';
10141 my $group = 'ftpd';
10142 my $home_dir = File::Spec->rel2abs($tmpdir);
10143 my $uid = 500;
10144 my $gid = 500;
10145
10146 # Make sure that, if we're running as root, that the home directory has
10147 # permissions/privs set for the account we create
10148 if ($< == 0) {
10149 unless (chmod(0755, $home_dir)) {
10150 die("Can't set perms on $home_dir to 0755: $!");
10151 }
10152
10153 unless (chown($uid, $gid, $home_dir)) {
10154 die("Can't set owner of $home_dir to $uid/$gid: $!");
10155 }
10156 }
10157
10158 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
10159 '/bin/bash');
10160 auth_group_write($auth_group_file, $group, $gid, $user);
10161
10162 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
10163
10164 # Build up sqlite3 command to create users, groups tables and populate them
10165 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
10166
10167 if (open(my $fh, "> $db_script")) {
10168 print $fh <<EOS;
10169 CREATE TABLE ftpsessions (
10170 user TEXT,
10171 ip_addr TEXT,
10172 xfer_status TEXT,
10173 xfer_path TEXT
10174 );
10175 EOS
10176
10177 unless (close($fh)) {
10178 die("Can't write $db_script: $!");
10179 }
10180
10181 } else {
10182 die("Can't open $db_script: $!");
10183 }
10184
10185 my $cmd = "sqlite3 $db_file < $db_script";
10186 build_db($cmd, $db_script);
10187
10188 # Make sure that, if we're running as root, the database file has
10189 # the permissions/privs set for use by proftpd
10190 if ($< == 0) {
10191 unless (chmod(0666, $db_file)) {
10192 die("Can't set perms on $db_file to 0666: $!");
10193 }
10194 }
10195
10196 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
10197 if (open(my $fh, "> $test_file")) {
10198 close($fh);
10199
10200 } else {
10201 die("Can't open $test_file: $!");
10202 }
10203
10204 my $config = {
10205 PidFile => $pid_file,
10206 ScoreboardFile => $scoreboard_file,
10207 SystemLog => $log_file,
10208 TraceLog => $log_file,
10209 Trace => 'sql:20 command:20 netio:20',
10210
10211 AuthUserFile => $auth_user_file,
10212 AuthGroupFile => $auth_group_file,
10213 AuthOrder => 'mod_auth_file.c',
10214
10215 IfModules => {
10216 'mod_delay.c' => {
10217 DelayEngine => 'off',
10218 },
10219
10220 'mod_sql.c' => {
10221 SQLEngine => 'log',
10222 SQLBackend => 'sqlite3',
10223 SQLConnectInfo => $db_file,
10224 SQLLogFile => $log_file,
10225 SQLNamedQuery => 'xfer_status FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%f\')"',
10226 SQLLog => 'PWD xfer_status',
10227 },
10228 },
10229 };
10230
10231 my ($port, $config_user, $config_group) = config_write($config_file, $config);
10232
10233 # Open pipes, for use between the parent and child processes. Specifically,
10234 # the child will indicate when it's done with its test by writing a message
10235 # to the parent.
10236 my ($rfh, $wfh);
10237 unless (pipe($rfh, $wfh)) {
10238 die("Can't open pipe: $!");
10239 }
10240
10241 my $ex;
10242
10243 # Fork child
10244 $self->handle_sigchld();
10245 defined(my $pid = fork()) or die("Can't fork: $!");
10246 if ($pid) {
10247 eval {
10248 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
10249 $client->login($user, $passwd);
10250 $client->pwd();
10251 $client->quit();
10252 };
10253
10254 if ($@) {
10255 $ex = $@;
10256 }
10257
10258 $wfh->print("done\n");
10259 $wfh->flush();
10260
10261 } else {
10262 eval { server_wait($config_file, $rfh) };
10263 if ($@) {
10264 warn($@);
10265 exit 1;
10266 }
10267
10268 exit 0;
10269 }
10270
10271 # Stop server
10272 server_stop($pid_file);
10273
10274 $self->assert_child_ok($pid);
10275
10276 if ($ex) {
10277 test_append_logfile($log_file, $ex);
10278 unlink($log_file);
10279
10280 die($ex);
10281 }
10282
10283 my ($login, $ip_addr, $xfer_status, $xfer_path) = get_xfer_status($db_file, "user = \'$user\'");
10284
10285 my $expected;
10286
10287 $expected = $user;
10288 $self->assert($expected eq $login,
10289 test_msg("Expected user '$expected', got '$login'"));
10290
10291 $expected = '127.0.0.1';
10292 $self->assert($expected eq $ip_addr,
10293 test_msg("Expected IP address '$expected', got '$ip_addr'"));
10294
10295 $expected = '-';
10296 $self->assert($expected eq $xfer_status,
10297 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
10298
10299 if ($^O eq 'darwin') {
10300 # MacOSX-specific hack
10301 $home_dir = '/private' . $home_dir;
10302 }
10303
10304 $expected = $home_dir;
10305 $self->assert($expected eq $xfer_path,
10306 test_msg("Expected file path '$expected', got '$xfer_path'"));
10307
10308 unlink($log_file);
10309 }
10310
10311 sub sql_sqllog_var_xfer_status_success_download {
10312 my $self = shift;
10313 my $tmpdir = $self->{tmpdir};
10314
10315 my $config_file = "$tmpdir/sqlite.conf";
10316 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
10317 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
10318
10319 my $log_file = test_get_logfile();
10320
10321 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
10322 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
10323
10324 my $user = 'proftpd';
10325 my $passwd = 'test';
10326 my $group = 'ftpd';
10327 my $home_dir = File::Spec->rel2abs($tmpdir);
10328 my $uid = 500;
10329 my $gid = 500;
10330
10331 # Make sure that, if we're running as root, that the home directory has
10332 # permissions/privs set for the account we create
10333 if ($< == 0) {
10334 unless (chmod(0755, $home_dir)) {
10335 die("Can't set perms on $home_dir to 0755: $!");
10336 }
10337
10338 unless (chown($uid, $gid, $home_dir)) {
10339 die("Can't set owner of $home_dir to $uid/$gid: $!");
10340 }
10341 }
10342
10343 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
10344 '/bin/bash');
10345 auth_group_write($auth_group_file, $group, $gid, $user);
10346
10347 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
10348
10349 # Build up sqlite3 command to create users, groups tables and populate them
10350 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
10351
10352 if (open(my $fh, "> $db_script")) {
10353 print $fh <<EOS;
10354 CREATE TABLE ftpsessions (
10355 user TEXT,
10356 ip_addr TEXT,
10357 xfer_status TEXT,
10358 xfer_path TEXT
10359 );
10360 EOS
10361
10362 unless (close($fh)) {
10363 die("Can't write $db_script: $!");
10364 }
10365
10366 } else {
10367 die("Can't open $db_script: $!");
10368 }
10369
10370 my $cmd = "sqlite3 $db_file < $db_script";
10371 build_db($cmd, $db_script);
10372
10373 # Make sure that, if we're running as root, the database file has
10374 # the permissions/privs set for use by proftpd
10375 if ($< == 0) {
10376 unless (chmod(0666, $db_file)) {
10377 die("Can't set perms on $db_file to 0666: $!");
10378 }
10379 }
10380
10381 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
10382 if (open(my $fh, "> $test_file")) {
10383 close($fh);
10384
10385 } else {
10386 die("Can't open $test_file: $!");
10387 }
10388
10389 my $config = {
10390 PidFile => $pid_file,
10391 ScoreboardFile => $scoreboard_file,
10392 SystemLog => $log_file,
10393 TraceLog => $log_file,
10394 Trace => 'sql:20 command:20 netio:20',
10395
10396 AuthUserFile => $auth_user_file,
10397 AuthGroupFile => $auth_group_file,
10398 AuthOrder => 'mod_auth_file.c',
10399
10400 IfModules => {
10401 'mod_delay.c' => {
10402 DelayEngine => 'off',
10403 },
10404
10405 'mod_sql.c' => {
10406 SQLEngine => 'log',
10407 SQLBackend => 'sqlite3',
10408 SQLConnectInfo => $db_file,
10409 SQLLogFile => $log_file,
10410 SQLNamedQuery => 'xfer_status FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%f\')"',
10411 SQLLog => 'ERR_RETR,RETR xfer_status',
10412 },
10413 },
10414 };
10415
10416 my ($port, $config_user, $config_group) = config_write($config_file, $config);
10417
10418 # Open pipes, for use between the parent and child processes. Specifically,
10419 # the child will indicate when it's done with its test by writing a message
10420 # to the parent.
10421 my ($rfh, $wfh);
10422 unless (pipe($rfh, $wfh)) {
10423 die("Can't open pipe: $!");
10424 }
10425
10426 my $ex;
10427
10428 # Fork child
10429 $self->handle_sigchld();
10430 defined(my $pid = fork()) or die("Can't fork: $!");
10431 if ($pid) {
10432 eval {
10433 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
10434 $client->login($user, $passwd);
10435
10436 my $conn = $client->retr_raw('test.txt');
10437 unless ($conn) {
10438 die("RETR test.txt failed: " . $client->response_code() . " " .
10439 $client->response_msg());
10440 }
10441
10442 my $buf;
10443 $conn->read($buf, 8192, 25);
10444 eval { $conn->close() };
10445
10446 my $resp_code = $client->response_code();
10447 my $resp_msg = $client->response_msg();
10448
10449 $self->assert_transfer_ok($resp_code, $resp_msg);
10450
10451 $client->quit();
10452 };
10453
10454 if ($@) {
10455 $ex = $@;
10456 }
10457
10458 $wfh->print("done\n");
10459 $wfh->flush();
10460
10461 } else {
10462 eval { server_wait($config_file, $rfh) };
10463 if ($@) {
10464 warn($@);
10465 exit 1;
10466 }
10467
10468 exit 0;
10469 }
10470
10471 # Stop server
10472 server_stop($pid_file);
10473
10474 $self->assert_child_ok($pid);
10475
10476 if ($ex) {
10477 test_append_logfile($log_file, $ex);
10478 unlink($log_file);
10479
10480 die($ex);
10481 }
10482
10483 my ($login, $ip_addr, $xfer_status, $xfer_path) = get_xfer_status($db_file, "user = \'$user\'");
10484
10485 my $expected;
10486
10487 $expected = $user;
10488 $self->assert($expected eq $login,
10489 test_msg("Expected user '$expected', got '$login'"));
10490
10491 $expected = '127.0.0.1';
10492 $self->assert($expected eq $ip_addr,
10493 test_msg("Expected IP address '$expected', got '$ip_addr'"));
10494
10495 $expected = 'success';
10496 $self->assert($expected eq $xfer_status,
10497 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
10498
10499 if ($^O eq 'darwin') {
10500 # MacOSX-specific hack
10501 $test_file = '/private' . $test_file;
10502 }
10503
10504 $expected = $test_file;
10505 $self->assert($expected eq $xfer_path,
10506 test_msg("Expected file path '$expected', got '$xfer_path'"));
10507
10508 unlink($log_file);
10509 }
10510
10511 sub sql_sqllog_var_xfer_status_success_upload {
10512 my $self = shift;
10513 my $tmpdir = $self->{tmpdir};
10514
10515 my $config_file = "$tmpdir/sqlite.conf";
10516 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
10517 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
10518
10519 my $log_file = test_get_logfile();
10520
10521 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
10522 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
10523
10524 my $user = 'proftpd';
10525 my $passwd = 'test';
10526 my $group = 'ftpd';
10527 my $home_dir = File::Spec->rel2abs($tmpdir);
10528 my $uid = 500;
10529 my $gid = 500;
10530
10531 # Make sure that, if we're running as root, that the home directory has
10532 # permissions/privs set for the account we create
10533 if ($< == 0) {
10534 unless (chmod(0755, $home_dir)) {
10535 die("Can't set perms on $home_dir to 0755: $!");
10536 }
10537
10538 unless (chown($uid, $gid, $home_dir)) {
10539 die("Can't set owner of $home_dir to $uid/$gid: $!");
10540 }
10541 }
10542
10543 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
10544 '/bin/bash');
10545 auth_group_write($auth_group_file, $group, $gid, $user);
10546
10547 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
10548
10549 # Build up sqlite3 command to create users, groups tables and populate them
10550 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
10551
10552 if (open(my $fh, "> $db_script")) {
10553 print $fh <<EOS;
10554 CREATE TABLE ftpsessions (
10555 user TEXT,
10556 ip_addr TEXT,
10557 xfer_status TEXT,
10558 xfer_path TEXT
10559 );
10560 EOS
10561
10562 unless (close($fh)) {
10563 die("Can't write $db_script: $!");
10564 }
10565
10566 } else {
10567 die("Can't open $db_script: $!");
10568 }
10569
10570 my $cmd = "sqlite3 $db_file < $db_script";
10571 build_db($cmd, $db_script);
10572
10573 # Make sure that, if we're running as root, the database file has
10574 # the permissions/privs set for use by proftpd
10575 if ($< == 0) {
10576 unless (chmod(0666, $db_file)) {
10577 die("Can't set perms on $db_file to 0666: $!");
10578 }
10579 }
10580
10581 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
10582
10583 my $config = {
10584 PidFile => $pid_file,
10585 ScoreboardFile => $scoreboard_file,
10586 SystemLog => $log_file,
10587 TraceLog => $log_file,
10588 Trace => 'sql:20 command:20 netio:20',
10589
10590 AuthUserFile => $auth_user_file,
10591 AuthGroupFile => $auth_group_file,
10592 AuthOrder => 'mod_auth_file.c',
10593
10594 IfModules => {
10595 'mod_delay.c' => {
10596 DelayEngine => 'off',
10597 },
10598
10599 'mod_sql.c' => {
10600 SQLEngine => 'log',
10601 SQLBackend => 'sqlite3',
10602 SQLConnectInfo => $db_file,
10603 SQLLogFile => $log_file,
10604 SQLNamedQuery => 'xfer_status FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%f\')"',
10605 SQLLog => 'ERR_STOR,STOR xfer_status',
10606 },
10607 },
10608 };
10609
10610 my ($port, $config_user, $config_group) = config_write($config_file, $config);
10611
10612 # Open pipes, for use between the parent and child processes. Specifically,
10613 # the child will indicate when it's done with its test by writing a message
10614 # to the parent.
10615 my ($rfh, $wfh);
10616 unless (pipe($rfh, $wfh)) {
10617 die("Can't open pipe: $!");
10618 }
10619
10620 my $ex;
10621
10622 # Fork child
10623 $self->handle_sigchld();
10624 defined(my $pid = fork()) or die("Can't fork: $!");
10625 if ($pid) {
10626 eval {
10627 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
10628 $client->login($user, $passwd);
10629
10630 my $conn = $client->stor_raw('test.txt');
10631 unless ($conn) {
10632 die("STOR test.txt failed: " . $client->response_code() . " " .
10633 $client->response_msg());
10634 }
10635
10636 my $buf = "Hello, World!\n";
10637 $conn->write($buf, length($buf), 25);
10638 eval { $conn->close() };
10639
10640 my $resp_code = $client->response_code();
10641 my $resp_msg = $client->response_msg();
10642
10643 $self->assert_transfer_ok($resp_code, $resp_msg);
10644
10645 $client->quit();
10646 };
10647
10648 if ($@) {
10649 $ex = $@;
10650 }
10651
10652 $wfh->print("done\n");
10653 $wfh->flush();
10654
10655 } else {
10656 eval { server_wait($config_file, $rfh) };
10657 if ($@) {
10658 warn($@);
10659 exit 1;
10660 }
10661
10662 exit 0;
10663 }
10664
10665 # Stop server
10666 server_stop($pid_file);
10667
10668 $self->assert_child_ok($pid);
10669
10670 if ($ex) {
10671 test_append_logfile($log_file, $ex);
10672 unlink($log_file);
10673
10674 die($ex);
10675 }
10676
10677 my ($login, $ip_addr, $xfer_status, $xfer_path) = get_xfer_status($db_file, "user = \'$user\'");
10678
10679 my $expected;
10680
10681 $expected = $user;
10682 $self->assert($expected eq $login,
10683 test_msg("Expected user '$expected', got '$login'"));
10684
10685 $expected = '127.0.0.1';
10686 $self->assert($expected eq $ip_addr,
10687 test_msg("Expected IP address '$expected', got '$ip_addr'"));
10688
10689 $expected = 'success';
10690 $self->assert($expected eq $xfer_status,
10691 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
10692
10693 if ($^O eq 'darwin') {
10694 # MacOSX-specific hack
10695 $test_file = '/private' . $test_file;
10696 }
10697
10698 $expected = $test_file;
10699 $self->assert($expected eq $xfer_path,
10700 test_msg("Expected file path '$expected', got '$xfer_path'"));
10701
10702 unlink($log_file);
10703 }
10704
10705 sub sql_sqllog_var_xfer_status_cancelled {
10706 my $self = shift;
10707 my $tmpdir = $self->{tmpdir};
10708
10709 my $config_file = "$tmpdir/sqlite.conf";
10710 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
10711 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
10712
10713 my $log_file = test_get_logfile();
10714
10715 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
10716 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
10717
10718 my $user = 'proftpd';
10719 my $passwd = 'test';
10720 my $group = 'ftpd';
10721 my $home_dir = File::Spec->rel2abs($tmpdir);
10722 my $uid = 500;
10723 my $gid = 500;
10724
10725 # Make sure that, if we're running as root, that the home directory has
10726 # permissions/privs set for the account we create
10727 if ($< == 0) {
10728 unless (chmod(0755, $home_dir)) {
10729 die("Can't set perms on $home_dir to 0755: $!");
10730 }
10731
10732 unless (chown($uid, $gid, $home_dir)) {
10733 die("Can't set owner of $home_dir to $uid/$gid: $!");
10734 }
10735 }
10736
10737 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
10738 '/bin/bash');
10739 auth_group_write($auth_group_file, $group, $gid, $user);
10740
10741 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
10742
10743 # Build up sqlite3 command to create users, groups tables and populate them
10744 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
10745
10746 if (open(my $fh, "> $db_script")) {
10747 print $fh <<EOS;
10748 CREATE TABLE ftpsessions (
10749 user TEXT,
10750 ip_addr TEXT,
10751 xfer_status TEXT,
10752 xfer_path TEXT
10753 );
10754 EOS
10755
10756 unless (close($fh)) {
10757 die("Can't write $db_script: $!");
10758 }
10759
10760 } else {
10761 die("Can't open $db_script: $!");
10762 }
10763
10764 my $cmd = "sqlite3 $db_file < $db_script";
10765 build_db($cmd, $db_script);
10766
10767 # Make sure that, if we're running as root, the database file has
10768 # the permissions/privs set for use by proftpd
10769 if ($< == 0) {
10770 unless (chmod(0666, $db_file)) {
10771 die("Can't set perms on $db_file to 0666: $!");
10772 }
10773 }
10774
10775 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
10776 if (open(my $fh, "> $test_file")) {
10777 print $fh "ABCDefgh" x 32768;
10778
10779 unless (close($fh)) {
10780 die("Can't write $test_file: $!");
10781 }
10782
10783 } else {
10784 die("Can't open $test_file: $!");
10785 }
10786
10787 my $config = {
10788 PidFile => $pid_file,
10789 ScoreboardFile => $scoreboard_file,
10790 SystemLog => $log_file,
10791 TraceLog => $log_file,
10792 Trace => 'sql:20 command:20 netio:20',
10793
10794 AuthUserFile => $auth_user_file,
10795 AuthGroupFile => $auth_group_file,
10796 AuthOrder => 'mod_auth_file.c',
10797
10798 IfModules => {
10799 'mod_delay.c' => {
10800 DelayEngine => 'off',
10801 },
10802
10803 'mod_sql.c' => {
10804 SQLEngine => 'log',
10805 SQLBackend => 'sqlite3',
10806 SQLConnectInfo => $db_file,
10807 SQLLogFile => $log_file,
10808 SQLNamedQuery => 'xfer_status FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%f\')"',
10809 SQLLog => 'ERR_RETR,RETR xfer_status',
10810 },
10811 },
10812 };
10813
10814 my ($port, $config_user, $config_group) = config_write($config_file, $config);
10815
10816 # Open pipes, for use between the parent and child processes. Specifically,
10817 # the child will indicate when it's done with its test by writing a message
10818 # to the parent.
10819 my ($rfh, $wfh);
10820 unless (pipe($rfh, $wfh)) {
10821 die("Can't open pipe: $!");
10822 }
10823
10824 my $ex;
10825
10826 # Fork child
10827 $self->handle_sigchld();
10828 defined(my $pid = fork()) or die("Can't fork: $!");
10829 if ($pid) {
10830 eval {
10831 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
10832 $client->login($user, $passwd);
10833
10834 my $conn = $client->retr_raw('test.txt');
10835 unless ($conn) {
10836 die("RETR test.txt failed: " . $client->response_code() . " " .
10837 $client->response_msg());
10838 }
10839
10840 my $buf;
10841 $conn->read($buf, 4, 25);
10842 eval { $conn->abort() };
10843
10844 my $resp_code = $client->response_code();
10845 my $resp_msg = $client->response_msg();
10846
10847 $self->assert_transfer_ok($resp_code, $resp_msg, 1);
10848
10849 $client->quit();
10850 };
10851
10852 if ($@) {
10853 $ex = $@;
10854 }
10855
10856 $wfh->print("done\n");
10857 $wfh->flush();
10858
10859 } else {
10860 eval { server_wait($config_file, $rfh) };
10861 if ($@) {
10862 warn($@);
10863 exit 1;
10864 }
10865
10866 exit 0;
10867 }
10868
10869 # Stop server
10870 server_stop($pid_file);
10871
10872 $self->assert_child_ok($pid);
10873
10874 if ($ex) {
10875 test_append_logfile($log_file, $ex);
10876 unlink($log_file);
10877
10878 die($ex);
10879 }
10880
10881 my ($login, $ip_addr, $xfer_status, $xfer_path) = get_xfer_status($db_file, "user = \'$user\'");
10882
10883 my $expected;
10884
10885 $expected = $user;
10886 $self->assert($expected eq $login,
10887 test_msg("Expected user '$expected', got '$login'"));
10888
10889 $expected = '127.0.0.1';
10890 $self->assert($expected eq $ip_addr,
10891 test_msg("Expected IP address '$expected', got '$ip_addr'"));
10892
10893 $expected = 'cancelled';
10894 $self->assert($expected eq $xfer_status,
10895 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
10896
10897 if ($^O eq 'darwin') {
10898 # MacOSX-specific hack
10899 $test_file = '/private' . $test_file;
10900 }
10901
10902 $expected = $test_file;
10903 $self->assert($expected eq $xfer_path,
10904 test_msg("Expected file path '$expected', got '$xfer_path'"));
10905
10906 unlink($log_file);
10907 }
10908
10909 sub sql_sqllog_var_xfer_status_failed {
10910 my $self = shift;
10911 my $tmpdir = $self->{tmpdir};
10912
10913 my $config_file = "$tmpdir/sqlite.conf";
10914 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
10915 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
10916
10917 my $log_file = test_get_logfile();
10918
10919 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
10920 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
10921
10922 my $user = 'proftpd';
10923 my $passwd = 'test';
10924 my $group = 'ftpd';
10925 my $home_dir = File::Spec->rel2abs($tmpdir);
10926 my $uid = 500;
10927 my $gid = 500;
10928
10929 # Make sure that, if we're running as root, that the home directory has
10930 # permissions/privs set for the account we create
10931 if ($< == 0) {
10932 unless (chmod(0755, $home_dir)) {
10933 die("Can't set perms on $home_dir to 0755: $!");
10934 }
10935
10936 unless (chown($uid, $gid, $home_dir)) {
10937 die("Can't set owner of $home_dir to $uid/$gid: $!");
10938 }
10939 }
10940
10941 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
10942 '/bin/bash');
10943 auth_group_write($auth_group_file, $group, $gid, $user);
10944
10945 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
10946
10947 # Build up sqlite3 command to create users, groups tables and populate them
10948 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
10949
10950 if (open(my $fh, "> $db_script")) {
10951 print $fh <<EOS;
10952 CREATE TABLE ftpsessions (
10953 user TEXT,
10954 ip_addr TEXT,
10955 xfer_status TEXT,
10956 xfer_path TEXT
10957 );
10958 EOS
10959
10960 unless (close($fh)) {
10961 die("Can't write $db_script: $!");
10962 }
10963
10964 } else {
10965 die("Can't open $db_script: $!");
10966 }
10967
10968 my $cmd = "sqlite3 $db_file < $db_script";
10969 build_db($cmd, $db_script);
10970
10971 # Make sure that, if we're running as root, the database file has
10972 # the permissions/privs set for use by proftpd
10973 if ($< == 0) {
10974 unless (chmod(0666, $db_file)) {
10975 die("Can't set perms on $db_file to 0666: $!");
10976 }
10977 }
10978
10979 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
10980 if (open(my $fh, "> $test_file")) {
10981 print $fh "ABCDefgh" x 32768;
10982
10983 unless (close($fh)) {
10984 die("Can't write $test_file: $!");
10985 }
10986
10987 } else {
10988 die("Can't open $test_file: $!");
10989 }
10990
10991 my $config = {
10992 PidFile => $pid_file,
10993 ScoreboardFile => $scoreboard_file,
10994 SystemLog => $log_file,
10995 TraceLog => $log_file,
10996 Trace => 'sql:20 command:20 netio:20',
10997
10998 AuthUserFile => $auth_user_file,
10999 AuthGroupFile => $auth_group_file,
11000 AuthOrder => 'mod_auth_file.c',
11001
11002 # This is used to tickle the "failed" transfer status
11003 MaxRetrieveFileSize => '12 B',
11004
11005 IfModules => {
11006 'mod_delay.c' => {
11007 DelayEngine => 'off',
11008 },
11009
11010 'mod_sql.c' => {
11011 SQLEngine => 'log',
11012 SQLBackend => 'sqlite3',
11013 SQLConnectInfo => $db_file,
11014 SQLLogFile => $log_file,
11015 SQLNamedQuery => 'xfer_status FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%f\')"',
11016 SQLLog => 'ERR_RETR,RETR xfer_status',
11017 },
11018 },
11019 };
11020
11021 my ($port, $config_user, $config_group) = config_write($config_file, $config);
11022
11023 # Open pipes, for use between the parent and child processes. Specifically,
11024 # the child will indicate when it's done with its test by writing a message
11025 # to the parent.
11026 my ($rfh, $wfh);
11027 unless (pipe($rfh, $wfh)) {
11028 die("Can't open pipe: $!");
11029 }
11030
11031 my $ex;
11032
11033 # Fork child
11034 $self->handle_sigchld();
11035 defined(my $pid = fork()) or die("Can't fork: $!");
11036 if ($pid) {
11037 eval {
11038 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
11039 $client->login($user, $passwd);
11040
11041 my $conn = $client->retr_raw('test.txt');
11042 unless ($conn) {
11043 die("RETR test.txt failed: " . $client->response_code() . " " .
11044 $client->response_msg());
11045 }
11046
11047 my $buf;
11048 $conn->read($buf, 4, 25);
11049 eval { $conn->close() };
11050
11051 # Make the control connection go away uncleanly
11052 $client->quit();
11053 };
11054
11055 if ($@) {
11056 $ex = $@;
11057 }
11058
11059 $wfh->print("done\n");
11060 $wfh->flush();
11061
11062 } else {
11063 eval { server_wait($config_file, $rfh) };
11064 if ($@) {
11065 warn($@);
11066 exit 1;
11067 }
11068
11069 exit 0;
11070 }
11071
11072 # Stop server
11073 server_stop($pid_file);
11074
11075 $self->assert_child_ok($pid);
11076
11077 if ($ex) {
11078 test_append_logfile($log_file, $ex);
11079 unlink($log_file);
11080
11081 die($ex);
11082 }
11083
11084 my ($login, $ip_addr, $xfer_status, $xfer_path) = get_xfer_status($db_file, "user = \'$user\'");
11085
11086 my $expected;
11087
11088 $expected = $user;
11089 $self->assert($expected eq $login,
11090 test_msg("Expected user '$expected', got '$login'"));
11091
11092 $expected = '127.0.0.1';
11093 $self->assert($expected eq $ip_addr,
11094 test_msg("Expected IP address '$expected', got '$ip_addr'"));
11095
11096 $expected = 'failed';
11097 $self->assert($expected eq $xfer_status,
11098 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
11099
11100 if ($^O eq 'darwin') {
11101 # MacOSX-specific hack
11102 $test_file = '/private' . $test_file;
11103 }
11104
11105 $expected = $test_file;
11106 $self->assert($expected eq $xfer_path,
11107 test_msg("Expected file path '$expected', got '$xfer_path'"));
11108
11109 unlink($log_file);
11110 }
11111
11112 sub sql_sqllog_var_xfer_status_timeout {
11113 my $self = shift;
11114 my $tmpdir = $self->{tmpdir};
11115
11116 my $config_file = "$tmpdir/sqlite.conf";
11117 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
11118 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
11119
11120 my $log_file = test_get_logfile();
11121
11122 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
11123 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
11124
11125 my $user = 'proftpd';
11126 my $passwd = 'test';
11127 my $group = 'ftpd';
11128 my $home_dir = File::Spec->rel2abs($tmpdir);
11129 my $uid = 500;
11130 my $gid = 500;
11131
11132 # Make sure that, if we're running as root, that the home directory has
11133 # permissions/privs set for the account we create
11134 if ($< == 0) {
11135 unless (chmod(0755, $home_dir)) {
11136 die("Can't set perms on $home_dir to 0755: $!");
11137 }
11138
11139 unless (chown($uid, $gid, $home_dir)) {
11140 die("Can't set owner of $home_dir to $uid/$gid: $!");
11141 }
11142 }
11143
11144 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
11145 '/bin/bash');
11146 auth_group_write($auth_group_file, $group, $gid, $user);
11147
11148 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
11149
11150 # Build up sqlite3 command to create users, groups tables and populate them
11151 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
11152
11153 if (open(my $fh, "> $db_script")) {
11154 print $fh <<EOS;
11155 CREATE TABLE ftpsessions (
11156 user TEXT,
11157 ip_addr TEXT,
11158 xfer_status TEXT,
11159 xfer_path TEXT
11160 );
11161 EOS
11162
11163 unless (close($fh)) {
11164 die("Can't write $db_script: $!");
11165 }
11166
11167 } else {
11168 die("Can't open $db_script: $!");
11169 }
11170
11171 my $cmd = "sqlite3 $db_file < $db_script";
11172 build_db($cmd, $db_script);
11173
11174 # Make sure that, if we're running as root, the database file has
11175 # the permissions/privs set for use by proftpd
11176 if ($< == 0) {
11177 unless (chmod(0666, $db_file)) {
11178 die("Can't set perms on $db_file to 0666: $!");
11179 }
11180 }
11181
11182 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
11183 if (open(my $fh, "> $test_file")) {
11184 print $fh "ABCDefgh" x 32768;
11185
11186 unless (close($fh)) {
11187 die("Can't write $test_file: $!");
11188 }
11189
11190 } else {
11191 die("Can't open $test_file: $!");
11192 }
11193
11194 my $timeout_stalled = 1;
11195
11196 my $config = {
11197 PidFile => $pid_file,
11198 ScoreboardFile => $scoreboard_file,
11199 SystemLog => $log_file,
11200 TraceLog => $log_file,
11201 Trace => 'sql:20 command:20 netio:20',
11202
11203 AuthUserFile => $auth_user_file,
11204 AuthGroupFile => $auth_group_file,
11205 AuthOrder => 'mod_auth_file.c',
11206
11207 # This is used to tickle the "timeout" transfer status
11208 TimeoutStalled => $timeout_stalled,
11209
11210 IfModules => {
11211 'mod_delay.c' => {
11212 DelayEngine => 'off',
11213 },
11214
11215 'mod_sql.c' => {
11216 SQLEngine => 'log',
11217 SQLBackend => 'sqlite3',
11218 SQLConnectInfo => $db_file,
11219 SQLLogFile => $log_file,
11220 SQLNamedQuery => 'xfer_status FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%f\')"',
11221 SQLLog => 'ERR_RETR,RETR,EXIT xfer_status',
11222 },
11223 },
11224 };
11225
11226 my ($port, $config_user, $config_group) = config_write($config_file, $config);
11227
11228 # Open pipes, for use between the parent and child processes. Specifically,
11229 # the child will indicate when it's done with its test by writing a message
11230 # to the parent.
11231 my ($rfh, $wfh);
11232 unless (pipe($rfh, $wfh)) {
11233 die("Can't open pipe: $!");
11234 }
11235
11236 my $ex;
11237
11238 # Fork child
11239 $self->handle_sigchld();
11240 defined(my $pid = fork()) or die("Can't fork: $!");
11241 if ($pid) {
11242 eval {
11243 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
11244 $client->login($user, $passwd);
11245 $client->type('ascii');
11246
11247 my $conn = $client->retr_raw('test.txt');
11248 unless ($conn) {
11249 die("RETR test.xt failed: " . $client->response_code() . " " .
11250 $client->response_msg());
11251 }
11252
11253 sleep($timeout_stalled + 1);
11254
11255 my $buf;
11256 $conn->read($buf, 32, 25);
11257 sleep($timeout_stalled);
11258 eval { $conn->close() };
11259
11260 eval { $client->noop() };
11261 unless ($@) {
11262 die("NOOP succeeded unexpectedly");
11263 }
11264
11265 my $resp_code = $client->response_code();
11266 my $resp_msg = $client->response_msg();
11267
11268 # Perl's Net::Cmd module uses a very non-standard 599 code to
11269 # indicate that the connection is closed, depending on version.
11270 $self->assert($resp_code == 421 || $resp_code == 599,
11271 test_msg("Expected response code 421 or 599, got $resp_code"));
11272
11273 my $expected = "Connection closed";
11274 $self->assert(qr/$expected/, $resp_msg,
11275 test_msg("Expected response message '$expected', got '$resp_msg'"));
11276 };
11277 if ($@) {
11278 $ex = $@;
11279 }
11280
11281 $wfh->print("done\n");
11282 $wfh->flush();
11283
11284 } else {
11285 eval { server_wait($config_file, $rfh) };
11286 if ($@) {
11287 warn($@);
11288 exit 1;
11289 }
11290
11291 exit 0;
11292 }
11293
11294 # Stop server
11295 server_stop($pid_file);
11296
11297 $self->assert_child_ok($pid);
11298
11299 if ($ex) {
11300 test_append_logfile($log_file, $ex);
11301 unlink($log_file);
11302
11303 die($ex);
11304 }
11305
11306 my ($login, $ip_addr, $xfer_status, $xfer_path) = get_xfer_status($db_file, "user = \'$user\'");
11307
11308 my $expected;
11309
11310 $expected = $user;
11311 $self->assert($expected eq $login,
11312 test_msg("Expected user '$expected', got '$login'"));
11313
11314 $expected = '127.0.0.1';
11315 $self->assert($expected eq $ip_addr,
11316 test_msg("Expected IP address '$expected', got '$ip_addr'"));
11317
11318 $expected = '^(timeout|failed)$';
11319 $self->assert(qr/$expected/, $xfer_status,
11320 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
11321
11322 if ($xfer_status eq 'timeout') {
11323 if ($^O eq 'darwin') {
11324 # MacOSX-specific hack
11325 $test_file = '/private' . $test_file;
11326 }
11327
11328 $expected = $test_file;
11329 $self->assert($expected eq $xfer_path,
11330 test_msg("Expected file path '$expected', got '$xfer_path'"));
11331 }
11332
11333 unlink($log_file);
11334 }
11335
11336 sub sql_sqllog_var_xfer_status_filtered {
11337 my $self = shift;
11338 my $tmpdir = $self->{tmpdir};
11339
11340 my $config_file = "$tmpdir/sqlite.conf";
11341 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
11342 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
11343
11344 my $log_file = test_get_logfile();
11345
11346 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
11347 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
11348
11349 my $user = 'proftpd';
11350 my $passwd = 'test';
11351 my $group = 'ftpd';
11352 my $home_dir = File::Spec->rel2abs($tmpdir);
11353 my $uid = 500;
11354 my $gid = 500;
11355
11356 # Make sure that, if we're running as root, that the home directory has
11357 # permissions/privs set for the account we create
11358 if ($< == 0) {
11359 unless (chmod(0755, $home_dir)) {
11360 die("Can't set perms on $home_dir to 0755: $!");
11361 }
11362
11363 unless (chown($uid, $gid, $home_dir)) {
11364 die("Can't set owner of $home_dir to $uid/$gid: $!");
11365 }
11366 }
11367
11368 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
11369 '/bin/bash');
11370 auth_group_write($auth_group_file, $group, $gid, $user);
11371
11372 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
11373
11374 # Build up sqlite3 command to create users, groups tables and populate them
11375 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
11376
11377 if (open(my $fh, "> $db_script")) {
11378 print $fh <<EOS;
11379 CREATE TABLE ftpsessions (
11380 user TEXT,
11381 ip_addr TEXT,
11382 xfer_status TEXT,
11383 xfer_path TEXT
11384 );
11385 EOS
11386
11387 unless (close($fh)) {
11388 die("Can't write $db_script: $!");
11389 }
11390
11391 } else {
11392 die("Can't open $db_script: $!");
11393 }
11394
11395 my $cmd = "sqlite3 $db_file < $db_script";
11396 build_db($cmd, $db_script);
11397
11398 # Make sure that, if we're running as root, the database file has
11399 # the permissions/privs set for use by proftpd
11400 if ($< == 0) {
11401 unless (chmod(0666, $db_file)) {
11402 die("Can't set perms on $db_file to 0666: $!");
11403 }
11404 }
11405
11406 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
11407 if (open(my $fh, "> $test_file")) {
11408 print $fh "ABCDefgh" x 32768;
11409
11410 unless (close($fh)) {
11411 die("Can't write $test_file: $!");
11412 }
11413
11414 } else {
11415 die("Can't open $test_file: $!");
11416 }
11417
11418 my $config = {
11419 PidFile => $pid_file,
11420 ScoreboardFile => $scoreboard_file,
11421 SystemLog => $log_file,
11422 TraceLog => $log_file,
11423 Trace => 'sql:20 command:20 netio:20',
11424
11425 AuthUserFile => $auth_user_file,
11426 AuthGroupFile => $auth_group_file,
11427 AuthOrder => 'mod_auth_file.c',
11428
11429 PathDenyFilter => '^.*\.csv$',
11430
11431 IfModules => {
11432 'mod_delay.c' => {
11433 DelayEngine => 'off',
11434 },
11435
11436 'mod_sql.c' => {
11437 SQLEngine => 'log',
11438 SQLBackend => 'sqlite3',
11439 SQLConnectInfo => $db_file,
11440 SQLLogFile => $log_file,
11441 SQLNamedQuery => 'xfer_status FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%f\')"',
11442 SQLLog => 'ERR_STOR,STOR,EXIT xfer_status',
11443 },
11444 },
11445 };
11446
11447 my ($port, $config_user, $config_group) = config_write($config_file, $config);
11448
11449 # Open pipes, for use between the parent and child processes. Specifically,
11450 # the child will indicate when it's done with its test by writing a message
11451 # to the parent.
11452 my ($rfh, $wfh);
11453 unless (pipe($rfh, $wfh)) {
11454 die("Can't open pipe: $!");
11455 }
11456
11457 my $ex;
11458
11459 # Fork child
11460 $self->handle_sigchld();
11461 defined(my $pid = fork()) or die("Can't fork: $!");
11462 if ($pid) {
11463 eval {
11464 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
11465 $client->login($user, $passwd);
11466 $client->type('ascii');
11467
11468 my $conn = $client->stor_raw('test.csv');
11469 if ($conn) {
11470 die("RETR test.csv succeeded unexpectedly");
11471 }
11472
11473 my $resp_code = $client->response_code();
11474 my $resp_msg = $client->response_msg();
11475 $client->quit();
11476
11477 my $expected = 550;
11478 $self->assert($expected == $resp_code,
11479 "Expected response code $expected, got $resp_code");
11480
11481 $expected = "test.csv: Forbidden filename";
11482 $self->assert($expected eq $resp_msg,
11483 "Expected '$expected', got '$resp_msg'");
11484 };
11485
11486 if ($@) {
11487 $ex = $@;
11488 }
11489
11490 $wfh->print("done\n");
11491 $wfh->flush();
11492
11493 } else {
11494 eval { server_wait($config_file, $rfh) };
11495 if ($@) {
11496 warn($@);
11497 exit 1;
11498 }
11499
11500 exit 0;
11501 }
11502
11503 # Stop server
11504 server_stop($pid_file);
11505
11506 $self->assert_child_ok($pid);
11507
11508 if ($ex) {
11509 test_append_logfile($log_file, $ex);
11510 unlink($log_file);
11511
11512 die($ex);
11513 }
11514
11515 my ($login, $ip_addr, $xfer_status, $xfer_path) = get_xfer_status($db_file, "user = \'$user\'");
11516
11517 my $expected;
11518
11519 $expected = $user;
11520 $self->assert($expected eq $login,
11521 test_msg("Expected user '$expected', got '$login'"));
11522
11523 $expected = '127.0.0.1';
11524 $self->assert($expected eq $ip_addr,
11525 test_msg("Expected IP address '$expected', got '$ip_addr'"));
11526
11527 $expected = '^(timeout|failed)$';
11528 $self->assert(qr/$expected/, $xfer_status,
11529 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
11530
11531 if ($xfer_status eq 'timeout') {
11532 if ($^O eq 'darwin') {
11533 # MacOSX-specific hack
11534 $test_file = '/private' . $test_file;
11535 }
11536
11537 $expected = $test_file;
11538 $self->assert($expected eq $xfer_path,
11539 test_msg("Expected file path '$expected', got '$xfer_path'"));
11540 }
11541
11542 unlink($log_file);
11543 }
11544
11545 sub get_xfer_failure {
11546 my $db_file = shift;
11547 my $where = shift;
11548
11549 my $sql = "SELECT user, ip_addr, xfer_status, xfer_failure, xfer_path FROM ftpsessions";
11550 if ($where) {
11551 $sql .= " WHERE $where";
11552 }
11553 $sql .= " LIMIT 1";
11554
11555 my $cmd = "sqlite3 $db_file \"$sql\"";
11556
11557 if ($ENV{TEST_VERBOSE}) {
11558 print STDERR "Executing sqlite3: $cmd\n";
11559 }
11560
11561 my $res = join('', `$cmd`);
11562 chomp($res);
11563
11564 # The default sqlite3 delimiter is '|'
11565 return split(/\|/, $res);
11566 }
11567
11568 sub sql_sqllog_var_xfer_failure_none {
11569 my $self = shift;
11570 my $tmpdir = $self->{tmpdir};
11571
11572 my $config_file = "$tmpdir/sqlite.conf";
11573 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
11574 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
11575
11576 my $log_file = test_get_logfile();
11577
11578 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
11579 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
11580
11581 my $user = 'proftpd';
11582 my $passwd = 'test';
11583 my $group = 'ftpd';
11584 my $home_dir = File::Spec->rel2abs($tmpdir);
11585 my $uid = 500;
11586 my $gid = 500;
11587
11588 # Make sure that, if we're running as root, that the home directory has
11589 # permissions/privs set for the account we create
11590 if ($< == 0) {
11591 unless (chmod(0755, $home_dir)) {
11592 die("Can't set perms on $home_dir to 0755: $!");
11593 }
11594
11595 unless (chown($uid, $gid, $home_dir)) {
11596 die("Can't set owner of $home_dir to $uid/$gid: $!");
11597 }
11598 }
11599
11600 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
11601 '/bin/bash');
11602 auth_group_write($auth_group_file, $group, $gid, $user);
11603
11604 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
11605
11606 # Build up sqlite3 command to create users, groups tables and populate them
11607 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
11608
11609 if (open(my $fh, "> $db_script")) {
11610 print $fh <<EOS;
11611 CREATE TABLE ftpsessions (
11612 user TEXT,
11613 ip_addr TEXT,
11614 xfer_status TEXT,
11615 xfer_failure TEXT,
11616 xfer_path TEXT
11617 );
11618 EOS
11619
11620 unless (close($fh)) {
11621 die("Can't write $db_script: $!");
11622 }
11623
11624 } else {
11625 die("Can't open $db_script: $!");
11626 }
11627
11628 my $cmd = "sqlite3 $db_file < $db_script";
11629 build_db($cmd, $db_script);
11630
11631 # Make sure that, if we're running as root, the database file has
11632 # the permissions/privs set for use by proftpd
11633 if ($< == 0) {
11634 unless (chmod(0666, $db_file)) {
11635 die("Can't set perms on $db_file to 0666: $!");
11636 }
11637 }
11638
11639 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
11640 if (open(my $fh, "> $test_file")) {
11641 close($fh);
11642
11643 } else {
11644 die("Can't open $test_file: $!");
11645 }
11646
11647 my $config = {
11648 PidFile => $pid_file,
11649 ScoreboardFile => $scoreboard_file,
11650 SystemLog => $log_file,
11651 TraceLog => $log_file,
11652 Trace => 'sql:20 command:20 netio:20',
11653
11654 AuthUserFile => $auth_user_file,
11655 AuthGroupFile => $auth_group_file,
11656 AuthOrder => 'mod_auth_file.c',
11657
11658 IfModules => {
11659 'mod_delay.c' => {
11660 DelayEngine => 'off',
11661 },
11662
11663 'mod_sql.c' => {
11664 SQLEngine => 'log',
11665 SQLBackend => 'sqlite3',
11666 SQLConnectInfo => $db_file,
11667 SQLLogFile => $log_file,
11668 SQLNamedQuery => 'xfer_reason FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_failure, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%{transfer-failure}\', \'%f\')"',
11669 SQLLog => 'ERR_RETR,RETR xfer_reason',
11670 },
11671 },
11672 };
11673
11674 my ($port, $config_user, $config_group) = config_write($config_file, $config);
11675
11676 # Open pipes, for use between the parent and child processes. Specifically,
11677 # the child will indicate when it's done with its test by writing a message
11678 # to the parent.
11679 my ($rfh, $wfh);
11680 unless (pipe($rfh, $wfh)) {
11681 die("Can't open pipe: $!");
11682 }
11683
11684 my $ex;
11685
11686 # Fork child
11687 $self->handle_sigchld();
11688 defined(my $pid = fork()) or die("Can't fork: $!");
11689 if ($pid) {
11690 eval {
11691 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
11692 $client->login($user, $passwd);
11693
11694 my $conn = $client->retr_raw('test.txt');
11695 unless ($conn) {
11696 die("RETR test.txt failed: " . $client->response_code() . " " .
11697 $client->response_msg());
11698 }
11699
11700 my $buf;
11701 $conn->read($buf, 8192, 25);
11702 eval { $conn->close() };
11703
11704 my $resp_code = $client->response_code();
11705 my $resp_msg = $client->response_msg();
11706
11707 $self->assert_transfer_ok($resp_code, $resp_msg);
11708
11709 $client->quit();
11710 };
11711
11712 if ($@) {
11713 $ex = $@;
11714 }
11715
11716 $wfh->print("done\n");
11717 $wfh->flush();
11718
11719 } else {
11720 eval { server_wait($config_file, $rfh) };
11721 if ($@) {
11722 warn($@);
11723 exit 1;
11724 }
11725
11726 exit 0;
11727 }
11728
11729 # Stop server
11730 server_stop($pid_file);
11731
11732 $self->assert_child_ok($pid);
11733
11734 if ($ex) {
11735 test_append_logfile($log_file, $ex);
11736 unlink($log_file);
11737
11738 die($ex);
11739 }
11740
11741 my ($login, $ip_addr, $xfer_status, $xfer_failure, $xfer_path) = get_xfer_failure($db_file, "user = \'$user\'");
11742
11743 my $expected;
11744
11745 $expected = $user;
11746 $self->assert($expected eq $login,
11747 test_msg("Expected user '$expected', got '$login'"));
11748
11749 $expected = '127.0.0.1';
11750 $self->assert($expected eq $ip_addr,
11751 test_msg("Expected IP address '$expected', got '$ip_addr'"));
11752
11753 $expected = 'success';
11754 $self->assert($expected eq $xfer_status,
11755 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
11756
11757 $expected = '-';
11758 $self->assert($expected eq $xfer_failure,
11759 test_msg("Expected transfer failure '$expected', got '$xfer_failure'"));
11760
11761 if ($^O eq 'darwin') {
11762 # MacOSX-specific hack
11763 $test_file = '/private' . $test_file;
11764 }
11765
11766 $expected = $test_file;
11767 $self->assert($expected eq $xfer_path,
11768 test_msg("Expected file path '$expected', got '$xfer_path'"));
11769
11770 unlink($log_file);
11771 }
11772
11773 sub sql_sqllog_var_xfer_failure_reason {
11774 my $self = shift;
11775 my $tmpdir = $self->{tmpdir};
11776
11777 my $config_file = "$tmpdir/sqlite.conf";
11778 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
11779 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
11780
11781 my $log_file = test_get_logfile();
11782
11783 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
11784 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
11785
11786 my $user = 'proftpd';
11787 my $passwd = 'test';
11788 my $group = 'ftpd';
11789 my $home_dir = File::Spec->rel2abs($tmpdir);
11790 my $uid = 500;
11791 my $gid = 500;
11792
11793 # Make sure that, if we're running as root, that the home directory has
11794 # permissions/privs set for the account we create
11795 if ($< == 0) {
11796 unless (chmod(0755, $home_dir)) {
11797 die("Can't set perms on $home_dir to 0755: $!");
11798 }
11799
11800 unless (chown($uid, $gid, $home_dir)) {
11801 die("Can't set owner of $home_dir to $uid/$gid: $!");
11802 }
11803 }
11804
11805 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
11806 '/bin/bash');
11807 auth_group_write($auth_group_file, $group, $gid, $user);
11808
11809 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
11810
11811 # Build up sqlite3 command to create users, groups tables and populate them
11812 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
11813
11814 if (open(my $fh, "> $db_script")) {
11815 print $fh <<EOS;
11816 CREATE TABLE ftpsessions (
11817 user TEXT,
11818 ip_addr TEXT,
11819 xfer_status TEXT,
11820 xfer_failure TEXT,
11821 xfer_path TEXT
11822 );
11823 EOS
11824
11825 unless (close($fh)) {
11826 die("Can't write $db_script: $!");
11827 }
11828
11829 } else {
11830 die("Can't open $db_script: $!");
11831 }
11832
11833 my $cmd = "sqlite3 $db_file < $db_script";
11834 build_db($cmd, $db_script);
11835
11836 # Make sure that, if we're running as root, the database file has
11837 # the permissions/privs set for use by proftpd
11838 if ($< == 0) {
11839 unless (chmod(0666, $db_file)) {
11840 die("Can't set perms on $db_file to 0666: $!");
11841 }
11842 }
11843
11844 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
11845 if (open(my $fh, "> $test_file")) {
11846 print $fh "ABCD" x 1024;
11847 close($fh);
11848
11849 } else {
11850 die("Can't open $test_file: $!");
11851 }
11852
11853 my $config = {
11854 PidFile => $pid_file,
11855 ScoreboardFile => $scoreboard_file,
11856 SystemLog => $log_file,
11857 TraceLog => $log_file,
11858 Trace => 'sql:20 command:20 netio:20',
11859
11860 AuthUserFile => $auth_user_file,
11861 AuthGroupFile => $auth_group_file,
11862 AuthOrder => 'mod_auth_file.c',
11863
11864 # This is used to tickle the "failed" transfer status
11865 MaxRetrieveFileSize => '12 B',
11866
11867 IfModules => {
11868 'mod_delay.c' => {
11869 DelayEngine => 'off',
11870 },
11871
11872 'mod_sql.c' => {
11873 SQLEngine => 'log',
11874 SQLBackend => 'sqlite3',
11875 SQLConnectInfo => $db_file,
11876 SQLLogFile => $log_file,
11877 SQLNamedQuery => 'xfer_reason FREEFORM "INSERT INTO ftpsessions (user, ip_addr, xfer_status, xfer_failure, xfer_path) VALUES (\'%u\', \'%L\', \'%{transfer-status}\', \'%{transfer-failure}\', \'%f\')"',
11878 SQLLog => 'ERR_RETR,RETR xfer_reason',
11879 },
11880 },
11881 };
11882
11883 my ($port, $config_user, $config_group) = config_write($config_file, $config);
11884
11885 # Open pipes, for use between the parent and child processes. Specifically,
11886 # the child will indicate when it's done with its test by writing a message
11887 # to the parent.
11888 my ($rfh, $wfh);
11889 unless (pipe($rfh, $wfh)) {
11890 die("Can't open pipe: $!");
11891 }
11892
11893 my $ex;
11894
11895 # Fork child
11896 $self->handle_sigchld();
11897 defined(my $pid = fork()) or die("Can't fork: $!");
11898 if ($pid) {
11899 eval {
11900 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
11901 $client->login($user, $passwd);
11902
11903 my $conn = $client->retr_raw('test.txt');
11904 unless ($conn) {
11905 die("RETR test.txt failed: " . $client->response_code() . " " .
11906 $client->response_msg());
11907 }
11908
11909 my $buf;
11910 $conn->read($buf, 8192, 25);
11911 eval { $conn->close() };
11912
11913 my $resp_code = $client->response_code();
11914 my $resp_msg = $client->response_msg();
11915
11916 my $expected = 426;
11917 $self->assert($expected == $resp_code,
11918 test_msg("Expected response code $expected, got $resp_code"));
11919
11920 $expected = 'Transfer aborted';
11921 $self->assert(qr/$expected/, $resp_msg,
11922 test_msg("Expected response message '$expected', got '$resp_msg'"));
11923
11924 $client->quit();
11925 };
11926
11927 if ($@) {
11928 $ex = $@;
11929 }
11930
11931 $wfh->print("done\n");
11932 $wfh->flush();
11933
11934 } else {
11935 eval { server_wait($config_file, $rfh) };
11936 if ($@) {
11937 warn($@);
11938 exit 1;
11939 }
11940
11941 exit 0;
11942 }
11943
11944 # Stop server
11945 server_stop($pid_file);
11946
11947 $self->assert_child_ok($pid);
11948
11949 if ($ex) {
11950 test_append_logfile($log_file, $ex);
11951 unlink($log_file);
11952
11953 die($ex);
11954 }
11955
11956 my ($login, $ip_addr, $xfer_status, $xfer_failure, $xfer_path) = get_xfer_failure($db_file, "user = \'$user\'");
11957
11958 my $expected;
11959
11960 $expected = $user;
11961 $self->assert($expected eq $login,
11962 test_msg("Expected user '$expected', got '$login'"));
11963
11964 $expected = '127.0.0.1';
11965 $self->assert($expected eq $ip_addr,
11966 test_msg("Expected IP address '$expected', got '$ip_addr'"));
11967
11968 $expected = 'failed';
11969 $self->assert($expected eq $xfer_status,
11970 test_msg("Expected transfer status '$expected', got '$xfer_status'"));
11971
11972 $expected = 'Operation not permitted';
11973 $self->assert($expected eq $xfer_failure,
11974 test_msg("Expected transfer failure '$expected', got '$xfer_failure'"));
11975
11976 if ($^O eq 'darwin') {
11977 # MacOSX-specific hack
11978 $test_file = '/private' . $test_file;
11979 }
11980
11981 $expected = $test_file;
11982 $self->assert($expected eq $xfer_path,
11983 test_msg("Expected file path '$expected', got '$xfer_path'"));
11984
11985 unlink($log_file);
11986 }
11987
11988 sub sql_sqlshowinfo_pass_failed_bug3782 {
11989 my $self = shift;
11990 my $tmpdir = $self->{tmpdir};
11991 my $setup = test_setup($tmpdir, 'sqlite');
11992
11993 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
11994
11995 # Build up sqlite3 command to create users, groups tables and populate them
11996 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
11997
11998 if (open(my $fh, "> $db_script")) {
11999 print $fh <<EOS;
12000 CREATE TABLE users (
12001 userid TEXT,
12002 passwd TEXT,
12003 uid INTEGER,
12004 gid INTEGER,
12005 homedir TEXT,
12006 shell TEXT,
12007 secure_only INTEGER
12008 );
12009 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, secure_only) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash', 1);
12010
12011 CREATE TABLE groups (
12012 groupname TEXT,
12013 gid INTEGER,
12014 members TEXT
12015 );
12016 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
12017
12018 CREATE TABLE login_responses (
12019 secure_only INTEGER,
12020 response TEXT
12021 );
12022
12023 INSERT INTO login_responses (secure_only, response) VALUES (0, "Login failed.");
12024 INSERT INTO login_responses (secure_only, response) VALUES (1, "You may not log into this account via FTP. Please use SFTP instead.");
12025 EOS
12026
12027 unless (close($fh)) {
12028 die("Can't write $db_script: $!");
12029 }
12030
12031 } else {
12032 die("Can't open $db_script: $!");
12033 }
12034
12035 my $cmd = "sqlite3 $db_file < $db_script";
12036 build_db($cmd, $db_script);
12037
12038 # Make sure that, if we're running as root, the database file has
12039 # the permissions/privs set for use by proftpd
12040 if ($< == 0) {
12041 unless (chmod(0666, $db_file)) {
12042 die("Can't set perms on $db_file to 0666: $!");
12043 }
12044 }
12045
12046 my $config = {
12047 PidFile => $setup->{pid_file},
12048 ScoreboardFile => $setup->{scoreboard_file},
12049 SystemLog => $setup->{log_file},
12050 TraceLog => $setup->{log_file},
12051 Trace => 'jot:20 response:20 sql:20',
12052
12053 IfModules => {
12054 'mod_delay.c' => {
12055 DelayEngine => 'off',
12056 },
12057
12058 'mod_sql.c' => [
12059 'AuthOrder mod_sql.c',
12060
12061 'SQLAuthenticate users groups',
12062 'SQLAuthTypes plaintext',
12063 'SQLBackend sqlite3',
12064 "SQLConnectInfo $db_file",
12065 "SQLLogFile $setup->{log_file}",
12066 'SQLMinID 100',
12067
12068 'SQLUserWhereClause "secure_only != 1"',
12069 'SQLNamedQuery login_failure SELECT "response FROM users u, login_responses lr WHERE u.userid = \'%U\' AND u.secure_only = lr.secure_only"',
12070
12071 # Configure the equivalent of a multiline DisplayLogin file
12072 # using the SQLShowInfo directive
12073
12074 'SQLShowInfo ERR_PASS 530 "Sorry, %U"',
12075 'SQLShowInfo ERR_PASS 530 "%{login_failure}"',
12076 'SQLShowInfo ERR_PASS 530 " "',
12077 ],
12078 },
12079 };
12080
12081 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
12082 $config);
12083
12084 # Open pipes, for use between the parent and child processes. Specifically,
12085 # the child will indicate when it's done with its test by writing a message
12086 # to the parent.
12087 my ($rfh, $wfh);
12088 unless (pipe($rfh, $wfh)) {
12089 die("Can't open pipe: $!");
12090 }
12091
12092 my $ex;
12093
12094 # Fork child
12095 $self->handle_sigchld();
12096 defined(my $pid = fork()) or die("Can't fork: $!");
12097 if ($pid) {
12098 eval {
12099 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12100 eval { $client->login($setup->{user}, $setup->{passwd}) };
12101 unless ($@) {
12102 die("Login succeeded unexpectedly");
12103 }
12104
12105 my $resp_msgs = $client->response_msgs();
12106 my $nmsgs = scalar(@$resp_msgs);
12107
12108 my $expected = 4;
12109 $self->assert($expected == $nmsgs, "Expected $expected, got $nmsgs");
12110
12111 $expected = "Login incorrect.";
12112 $self->assert($expected eq $resp_msgs->[0],
12113 "Expected response '$expected', got '$resp_msgs->[0]'");
12114
12115 $expected = " Sorry, $setup->{user}";
12116 $self->assert($expected eq $resp_msgs->[1],
12117 "Expected response '$expected', got '$resp_msgs->[1]'");
12118
12119 $expected = " You may not log into this account via FTP. Please use SFTP instead.";
12120 $self->assert($expected eq $resp_msgs->[2],
12121 "Expected response '$expected', got '$resp_msgs->[2]'");
12122
12123 $client->quit();
12124 };
12125 if ($@) {
12126 $ex = $@;
12127 }
12128
12129 $wfh->print("done\n");
12130 $wfh->flush();
12131
12132 } else {
12133 eval { server_wait($setup->{config_file}, $rfh) };
12134 if ($@) {
12135 warn($@);
12136 exit 1;
12137 }
12138
12139 exit 0;
12140 }
12141
12142 # Stop server
12143 server_stop($setup->{pid_file});
12144 $self->assert_child_ok($pid);
12145
12146 test_cleanup($setup->{log_file}, $ex);
12147 }
12148
12149 sub sql_sqllog_preauth_var_U_bug3822 {
12150 my $self = shift;
12151 my $tmpdir = $self->{tmpdir};
12152
12153 my $config_file = "$tmpdir/sqlite.conf";
12154 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
12155 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
12156
12157 my $log_file = test_get_logfile();
12158
12159 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
12160 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
12161
12162 my $user = 'proftpd';
12163 my $passwd = 'test';
12164 my $group = 'ftpd';
12165 my $home_dir = File::Spec->rel2abs($tmpdir);
12166 my $uid = 500;
12167 my $gid = 500;
12168
12169 # Make sure that, if we're running as root, that the home directory has
12170 # permissions/privs set for the account we create
12171 if ($< == 0) {
12172 unless (chmod(0755, $home_dir)) {
12173 die("Can't set perms on $home_dir to 0755: $!");
12174 }
12175
12176 unless (chown($uid, $gid, $home_dir)) {
12177 die("Can't set owner of $home_dir to $uid/$gid: $!");
12178 }
12179 }
12180
12181 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
12182 '/bin/bash');
12183 auth_group_write($auth_group_file, $group, $gid, $user);
12184
12185 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
12186
12187 # Build up sqlite3 command to create users, groups tables and populate them
12188 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
12189
12190 if (open(my $fh, "> $db_script")) {
12191 print $fh <<EOS;
12192 CREATE TABLE ftpsessions (
12193 user TEXT,
12194 ip_addr TEXT,
12195 resp_mesg TEXT
12196 );
12197 EOS
12198
12199 unless (close($fh)) {
12200 die("Can't write $db_script: $!");
12201 }
12202
12203 } else {
12204 die("Can't open $db_script: $!");
12205 }
12206
12207 my $cmd = "sqlite3 $db_file < $db_script";
12208 build_db($cmd, $db_script);
12209
12210 # Make sure that, if we're running as root, the database file has
12211 # the permissions/privs set for use by proftpd
12212 if ($< == 0) {
12213 unless (chmod(0666, $db_file)) {
12214 die("Can't set perms on $db_file to 0666: $!");
12215 }
12216 }
12217
12218 my $config = {
12219 PidFile => $pid_file,
12220 ScoreboardFile => $scoreboard_file,
12221 SystemLog => $log_file,
12222
12223 AuthUserFile => $auth_user_file,
12224 AuthGroupFile => $auth_group_file,
12225 AuthOrder => 'mod_auth_file.c',
12226
12227 IfModules => {
12228 'mod_delay.c' => {
12229 DelayEngine => 'off',
12230 },
12231
12232 'mod_sql.c' => {
12233 SQLEngine => 'log',
12234 SQLBackend => 'sqlite3',
12235 SQLConnectInfo => "$db_file foo bar PERCONNECTION",
12236 SQLLogFile => $log_file,
12237 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, ip_addr, resp_mesg) VALUES (\'%U\', \'%L\', \'%S\')"',
12238 SQLLog => 'ERR_PWD info',
12239 },
12240 },
12241 };
12242
12243 my ($port, $config_user, $config_group) = config_write($config_file, $config);
12244
12245 # Open pipes, for use between the parent and child processes. Specifically,
12246 # the child will indicate when it's done with its test by writing a message
12247 # to the parent.
12248 my ($rfh, $wfh);
12249 unless (pipe($rfh, $wfh)) {
12250 die("Can't open pipe: $!");
12251 }
12252
12253 my $ex;
12254
12255 # Fork child
12256 $self->handle_sigchld();
12257 defined(my $pid = fork()) or die("Can't fork: $!");
12258 if ($pid) {
12259 eval {
12260 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12261 eval { $client->pwd() };
12262 unless ($@) {
12263 die("PWD succeeded unexpectedly");
12264 }
12265
12266 $client->login($user, $passwd);
12267 $client->quit();
12268 };
12269
12270 if ($@) {
12271 $ex = $@;
12272 }
12273
12274 $wfh->print("done\n");
12275 $wfh->flush();
12276
12277 } else {
12278 eval { server_wait($config_file, $rfh) };
12279 if ($@) {
12280 warn($@);
12281 exit 1;
12282 }
12283
12284 exit 0;
12285 }
12286
12287 # Stop server
12288 server_stop($pid_file);
12289
12290 $self->assert_child_ok($pid);
12291
12292 if ($ex) {
12293 test_append_logfile($log_file, $ex);
12294 unlink($log_file);
12295
12296 die($ex);
12297 }
12298
12299 my ($login, $ip_addr, $resp_mesg) = get_resp_mesgs($db_file,
12300 "user = '-'", "user, ip_addr, resp_mesg");
12301
12302 my $expected;
12303
12304 $expected = '-';
12305 $self->assert($expected eq $login,
12306 test_msg("Expected '$expected', got '$login'"));
12307
12308 $expected = '127.0.0.1';
12309 $self->assert($expected eq $ip_addr,
12310 test_msg("Expected '$expected', got '$ip_addr'"));
12311
12312 $expected = 'Please login with USER and PASS';
12313 $self->assert($expected eq $resp_mesg,
12314 test_msg("Expected '$expected', got '$resp_mesg'"));
12315
12316 unlink($log_file);
12317 }
12318
12319 sub sql_sqllog_preauth_var_u_bug3822 {
12320 my $self = shift;
12321 my $tmpdir = $self->{tmpdir};
12322
12323 my $config_file = "$tmpdir/sqlite.conf";
12324 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
12325 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
12326
12327 my $log_file = test_get_logfile();
12328
12329 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
12330 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
12331
12332 my $user = 'proftpd';
12333 my $passwd = 'test';
12334 my $group = 'ftpd';
12335 my $home_dir = File::Spec->rel2abs($tmpdir);
12336 my $uid = 500;
12337 my $gid = 500;
12338
12339 # Make sure that, if we're running as root, that the home directory has
12340 # permissions/privs set for the account we create
12341 if ($< == 0) {
12342 unless (chmod(0755, $home_dir)) {
12343 die("Can't set perms on $home_dir to 0755: $!");
12344 }
12345
12346 unless (chown($uid, $gid, $home_dir)) {
12347 die("Can't set owner of $home_dir to $uid/$gid: $!");
12348 }
12349 }
12350
12351 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
12352 '/bin/bash');
12353 auth_group_write($auth_group_file, $group, $gid, $user);
12354
12355 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
12356
12357 # Build up sqlite3 command to create users, groups tables and populate them
12358 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
12359
12360 if (open(my $fh, "> $db_script")) {
12361 print $fh <<EOS;
12362 CREATE TABLE ftpsessions (
12363 user TEXT,
12364 ip_addr TEXT,
12365 resp_mesg TEXT
12366 );
12367 EOS
12368
12369 unless (close($fh)) {
12370 die("Can't write $db_script: $!");
12371 }
12372
12373 } else {
12374 die("Can't open $db_script: $!");
12375 }
12376
12377 my $cmd = "sqlite3 $db_file < $db_script";
12378 build_db($cmd, $db_script);
12379
12380 # Make sure that, if we're running as root, the database file has
12381 # the permissions/privs set for use by proftpd
12382 if ($< == 0) {
12383 unless (chmod(0666, $db_file)) {
12384 die("Can't set perms on $db_file to 0666: $!");
12385 }
12386 }
12387
12388 my $config = {
12389 PidFile => $pid_file,
12390 ScoreboardFile => $scoreboard_file,
12391 SystemLog => $log_file,
12392
12393 AuthUserFile => $auth_user_file,
12394 AuthGroupFile => $auth_group_file,
12395 AuthOrder => 'mod_auth_file.c',
12396
12397 IfModules => {
12398 'mod_delay.c' => {
12399 DelayEngine => 'off',
12400 },
12401
12402 'mod_sql.c' => {
12403 SQLEngine => 'log',
12404 SQLBackend => 'sqlite3',
12405 SQLConnectInfo => "$db_file foo bar PERCONNECTION",
12406 SQLLogFile => $log_file,
12407 SQLNamedQuery => 'info FREEFORM "INSERT INTO ftpsessions (user, ip_addr, resp_mesg) VALUES (\'%u\', \'%L\', \'%S\')"',
12408 SQLLog => 'ERR_PWD info',
12409 },
12410 },
12411 };
12412
12413 my ($port, $config_user, $config_group) = config_write($config_file, $config);
12414
12415 # Open pipes, for use between the parent and child processes. Specifically,
12416 # the child will indicate when it's done with its test by writing a message
12417 # to the parent.
12418 my ($rfh, $wfh);
12419 unless (pipe($rfh, $wfh)) {
12420 die("Can't open pipe: $!");
12421 }
12422
12423 my $ex;
12424
12425 # Fork child
12426 $self->handle_sigchld();
12427 defined(my $pid = fork()) or die("Can't fork: $!");
12428 if ($pid) {
12429 eval {
12430 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12431 eval { $client->pwd() };
12432 unless ($@) {
12433 die("PWD succeeded unexpectedly");
12434 }
12435
12436 $client->login($user, $passwd);
12437 $client->quit();
12438 };
12439
12440 if ($@) {
12441 $ex = $@;
12442 }
12443
12444 $wfh->print("done\n");
12445 $wfh->flush();
12446
12447 } else {
12448 eval { server_wait($config_file, $rfh) };
12449 if ($@) {
12450 warn($@);
12451 exit 1;
12452 }
12453
12454 exit 0;
12455 }
12456
12457 # Stop server
12458 server_stop($pid_file);
12459
12460 $self->assert_child_ok($pid);
12461
12462 if ($ex) {
12463 test_append_logfile($log_file, $ex);
12464 unlink($log_file);
12465
12466 die($ex);
12467 }
12468
12469 my ($login, $ip_addr, $resp_mesg) = get_resp_mesgs($db_file,
12470 "user = '-'", "user, ip_addr, resp_mesg");
12471
12472 my $expected;
12473
12474 $expected = '-';
12475 $self->assert($expected eq $login,
12476 test_msg("Expected '$expected', got '$login'"));
12477
12478 $expected = '127.0.0.1';
12479 $self->assert($expected eq $ip_addr,
12480 test_msg("Expected '$expected', got '$ip_addr'"));
12481
12482 $expected = 'Please login with USER and PASS';
12483 $self->assert($expected eq $resp_mesg,
12484 test_msg("Expected '$expected', got '$resp_mesg'"));
12485
12486 unlink($log_file);
12487 }
12488
12489 sub sql_sqlite_maxhostsperuser {
12490 my $self = shift;
12491 my $tmpdir = $self->{tmpdir};
12492 my $setup = test_setup($tmpdir, 'sqlite');
12493
12494 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
12495
12496 # Build up sqlite3 command to create users, groups tables and populate them
12497 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
12498
12499 if (open(my $fh, "> $db_script")) {
12500 print $fh <<EOS;
12501 CREATE TABLE users (
12502 userid TEXT,
12503 passwd TEXT,
12504 uid INTEGER,
12505 gid INTEGER,
12506 homedir TEXT,
12507 shell TEXT
12508 );
12509 CREATE INDEX i_users_userid ON users.userid;
12510 CREATE INDEX i_users_uid ON users.uid;
12511 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash');
12512
12513 CREATE TABLE groups (
12514 groupname TEXT,
12515 gid INTEGER,
12516 members TEXT
12517 );
12518 CREATE INDEX i_groups_gid ON groups.gid;
12519 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
12520
12521 CREATE TABLE user_hosts (
12522 session_id TEXT,
12523 userid TEXT,
12524 host TEXT
12525 );
12526 CREATE INDEX i_user_hosts_userid ON user_hosts.userid;
12527 INSERT INTO user_hosts (session_id, userid, host) VALUES ('abc', '$setup->{user}', '127.0.0.1');
12528
12529 EOS
12530 unless (close($fh)) {
12531 die("Can't write $db_script: $!");
12532 }
12533
12534 } else {
12535 die("Can't open $db_script: $!");
12536 }
12537
12538 my $cmd = "sqlite3 $db_file < $db_script";
12539 build_db($cmd, $db_script);
12540
12541 # Make sure that, if we're running as root, the database file has
12542 # the permissions/privs set for use by proftpd
12543 if ($< == 0) {
12544 unless (chmod(0666, $db_file)) {
12545 die("Can't set perms on $db_file to 0666: $!");
12546 }
12547 }
12548
12549 my $config = {
12550 PidFile => $setup->{pid_file},
12551 ScoreboardFile => $setup->{scoreboard_file},
12552 SystemLog => $setup->{log_file},
12553 TraceLog => $setup->{log_file},
12554 Trace => 'jot:20 response:20 sql:20',
12555
12556 IfModules => {
12557 'mod_delay.c' => {
12558 DelayEngine => 'off',
12559 },
12560
12561 'mod_sql.c' => [
12562 'AuthOrder mod_sql.c',
12563
12564 'SQLAuthenticate users groups',
12565 'SQLAuthTypes plaintext',
12566 'SQLBackend sqlite3',
12567 "SQLConnectInfo $db_file",
12568 "SQLLogFile $setup->{log_file}",
12569 'SQLMinID 100',
12570
12571 # Here, the MaxHostsPerUser limit is 3; it has to be hardcoded into
12572 # the SQL query, unfortunately. (Or maybe use a config file variable?)
12573 #
12574 # The LEFT JOIN is necessary for dealing with the case where the
12575 # user_hosts table has no matching rows for the user logging in.
12576 'SQLNamedQuery get-user-by-name SELECT "u.userid, u.passwd, u.uid, u.gid, u.homedir, u.shell, COUNT(uh.session_id) AS count FROM users u JOIN user_hosts uh ON u.userid = \"%U\" GROUP BY u.userid HAVING (count >= 0 AND count < 4)"',
12577 'SQLNamedQuery get-user-by-id SELECT "userid, passwd, uid, gid, homedir, shell FROM users WHERE uid = %{0}"',
12578 'SQLUserInfo custom:/get-user-by-name/get-user-by-id',
12579
12580 'SQLNamedQuery add-user-host FREEFORM "INSERT INTO user_hosts (session_id, userid, host) VALUES (\'%{note:UNIQUE_ID}\', \'%u\', \'%a\')"',
12581 'SQLLog PASS add-user-host',
12582
12583 'SQLNamedQuery remove-user-host FREEFORM "DELETE FROM user_hosts WHERE session_id = \"%{note:UNIQUE_ID}\"',
12584 'SQLLog EXIT remove-user-host',
12585
12586 'SQLShowInfo ERR_PASS 530 "Sorry, the maximum number of hosts for this user are already connected."',
12587 ],
12588 },
12589 };
12590
12591 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
12592 $config);
12593
12594 # Open pipes, for use between the parent and child processes. Specifically,
12595 # the child will indicate when it's done with its test by writing a message
12596 # to the parent.
12597 my ($rfh, $wfh);
12598 unless (pipe($rfh, $wfh)) {
12599 die("Can't open pipe: $!");
12600 }
12601
12602 my $ex;
12603
12604 # Fork child
12605 $self->handle_sigchld();
12606 defined(my $pid = fork()) or die("Can't fork: $!");
12607 if ($pid) {
12608 eval {
12609 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12610 $client1->login($setup->{user}, $setup->{passwd});
12611
12612 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12613 $client2->login($setup->{user}, $setup->{passwd});
12614
12615 my $client3 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12616 $client3->login($setup->{user}, $setup->{passwd});
12617
12618 my $client4 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12619 eval { $client4->login($setup->{user}, $setup->{passwd}) };
12620 unless ($@) {
12621 die("Fourth login succeeded unexpectedly");
12622 }
12623
12624 my $resp_code = $client4->response_code();
12625 my $resp_msgs = $client4->response_msgs();
12626
12627 my $expected = 530;
12628 $self->assert($expected == $resp_code,
12629 test_msg("Expected response code $expected, got $resp_code"));
12630
12631 $expected = "Login incorrect.";
12632 $self->assert($expected eq $resp_msgs->[0],
12633 test_msg("Expected response '$expected', got '$resp_msgs->[0]'"));
12634
12635 $expected = "Sorry, the maximum number of hosts for this user are already connected.";
12636 $self->assert($expected eq $resp_msgs->[1],
12637 test_msg("Expected response '$expected', got '$resp_msgs->[1]'"));
12638
12639 $client4->quit();
12640 $client3->quit();
12641
12642 $client4 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12643 $client4->login($setup->{user}, $setup->{passwd});
12644 $client4->quit();
12645
12646 $client2->quit();
12647 $client1->quit();
12648 };
12649 if ($@) {
12650 $ex = $@;
12651 }
12652
12653 $wfh->print("done\n");
12654 $wfh->flush();
12655
12656 } else {
12657 eval { server_wait($setup->{config_file}, $rfh) };
12658 if ($@) {
12659 warn($@);
12660 exit 1;
12661 }
12662
12663 exit 0;
12664 }
12665
12666 # Stop server
12667 server_stop($setup->{pid_file});
12668 $self->assert_child_ok($pid);
12669
12670 test_cleanup($setup->{log_file}, $ex);
12671 }
12672
12673 sub get_logins {
12674 my $db_file = shift;
12675 my $where = shift;
12676
12677 my $sql = "SELECT user, ip_addr, ts FROM ftpsessions";
12678 if ($where) {
12679 $sql .= " WHERE $where";
12680 }
12681
12682 my $cmd = "sqlite3 $db_file \"$sql\"";
12683
12684 if ($ENV{TEST_VERBOSE}) {
12685 print STDERR "Executing sqlite3: $cmd\n";
12686 }
12687
12688 my $res = join('', `$cmd`);
12689 chomp($res);
12690
12691 # The default sqlite3 delimiter is '|'
12692 return split(/\|/, $res);
12693 }
12694
12695 sub sql_sqllog_var_micros_ts_bug3889 {
12696 my $self = shift;
12697 my $tmpdir = $self->{tmpdir};
12698
12699 my $config_file = "$tmpdir/sqlite.conf";
12700 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
12701 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
12702
12703 my $log_file = test_get_logfile();
12704
12705 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
12706 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
12707
12708 my $user = 'proftpd';
12709 my $passwd = 'test';
12710 my $group = 'ftpd';
12711 my $home_dir = File::Spec->rel2abs($tmpdir);
12712 my $uid = 500;
12713 my $gid = 500;
12714
12715 # Make sure that, if we're running as root, that the home directory has
12716 # permissions/privs set for the account we create
12717 if ($< == 0) {
12718 unless (chmod(0755, $home_dir)) {
12719 die("Can't set perms on $home_dir to 0755: $!");
12720 }
12721
12722 unless (chown($uid, $gid, $home_dir)) {
12723 die("Can't set owner of $home_dir to $uid/$gid: $!");
12724 }
12725 }
12726
12727 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
12728 '/bin/bash');
12729 auth_group_write($auth_group_file, $group, $gid, $user);
12730
12731 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
12732
12733 # Build up sqlite3 command to create users, groups tables and populate them
12734 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
12735
12736 if (open(my $fh, "> $db_script")) {
12737 print $fh <<EOS;
12738 CREATE TABLE ftpsessions (
12739 user TEXT,
12740 ip_addr TEXT,
12741 ts TEXT
12742 );
12743 EOS
12744
12745 unless (close($fh)) {
12746 die("Can't write $db_script: $!");
12747 }
12748
12749 } else {
12750 die("Can't open $db_script: $!");
12751 }
12752
12753 my $cmd = "sqlite3 $db_file < $db_script";
12754 build_db($cmd, $db_script);
12755
12756 # Make sure that, if we're running as root, the database file has
12757 # the permissions/privs set for use by proftpd
12758 if ($< == 0) {
12759 unless (chmod(0666, $db_file)) {
12760 die("Can't set perms on $db_file to 0666: $!");
12761 }
12762 }
12763
12764 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
12765 if (open(my $fh, "> $src_file")) {
12766 close($fh);
12767
12768 } else {
12769 die("Can't open $src_file: $!");
12770 }
12771
12772 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
12773
12774 my $config = {
12775 PidFile => $pid_file,
12776 ScoreboardFile => $scoreboard_file,
12777 SystemLog => $log_file,
12778
12779 AuthUserFile => $auth_user_file,
12780 AuthGroupFile => $auth_group_file,
12781 AuthOrder => 'mod_auth_file.c',
12782
12783 IfModules => {
12784 'mod_delay.c' => {
12785 DelayEngine => 'off',
12786 },
12787
12788 'mod_sql.c' => {
12789 SQLEngine => 'log',
12790 SQLBackend => 'sqlite3',
12791 SQLConnectInfo => $db_file,
12792 SQLLogFile => $log_file,
12793 SQLNamedQuery => 'login FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ts) VALUES (\'%u\', \'%L\', \'%{microsecs}\')"',
12794 SQLLog => 'PASS login',
12795 },
12796 },
12797 };
12798
12799 my ($port, $config_user, $config_group) = config_write($config_file, $config);
12800
12801 # Open pipes, for use between the parent and child processes. Specifically,
12802 # the child will indicate when it's done with its test by writing a message
12803 # to the parent.
12804 my ($rfh, $wfh);
12805 unless (pipe($rfh, $wfh)) {
12806 die("Can't open pipe: $!");
12807 }
12808
12809 my $ex;
12810
12811 # Fork child
12812 $self->handle_sigchld();
12813 defined(my $pid = fork()) or die("Can't fork: $!");
12814 if ($pid) {
12815 eval {
12816 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12817 $client->login($user, $passwd);
12818 $client->quit();
12819 };
12820
12821 if ($@) {
12822 $ex = $@;
12823 }
12824
12825 $wfh->print("done\n");
12826 $wfh->flush();
12827
12828 } else {
12829 eval { server_wait($config_file, $rfh) };
12830 if ($@) {
12831 warn($@);
12832 exit 1;
12833 }
12834
12835 exit 0;
12836 }
12837
12838 # Stop server
12839 server_stop($pid_file);
12840
12841 $self->assert_child_ok($pid);
12842
12843 if ($ex) {
12844 test_append_logfile($log_file, $ex);
12845 unlink($log_file);
12846
12847 die($ex);
12848 }
12849
12850 my ($login, $ip_addr, $ts) = get_logins($db_file, "user = \'$user\'");
12851
12852 my $expected;
12853
12854 $expected = $user;
12855 $self->assert($expected eq $login,
12856 test_msg("Expected login user '$expected', got '$login'"));
12857
12858 $expected = '127.0.0.1';
12859 $self->assert($expected eq $ip_addr,
12860 test_msg("Expected IP address '$expected', got '$ip_addr'"));
12861
12862 $expected = '\d{6}';
12863 $self->assert(qr/$expected/, $ts,
12864 test_msg("Expected timestamp '$expected', got '$ts'"));
12865
12866 unlink($log_file);
12867 }
12868
12869 sub sql_sqllog_var_millis_ts_bug3889 {
12870 my $self = shift;
12871 my $tmpdir = $self->{tmpdir};
12872
12873 my $config_file = "$tmpdir/sqlite.conf";
12874 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
12875 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
12876
12877 my $log_file = test_get_logfile();
12878
12879 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
12880 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
12881
12882 my $user = 'proftpd';
12883 my $passwd = 'test';
12884 my $group = 'ftpd';
12885 my $home_dir = File::Spec->rel2abs($tmpdir);
12886 my $uid = 500;
12887 my $gid = 500;
12888
12889 # Make sure that, if we're running as root, that the home directory has
12890 # permissions/privs set for the account we create
12891 if ($< == 0) {
12892 unless (chmod(0755, $home_dir)) {
12893 die("Can't set perms on $home_dir to 0755: $!");
12894 }
12895
12896 unless (chown($uid, $gid, $home_dir)) {
12897 die("Can't set owner of $home_dir to $uid/$gid: $!");
12898 }
12899 }
12900
12901 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
12902 '/bin/bash');
12903 auth_group_write($auth_group_file, $group, $gid, $user);
12904
12905 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
12906
12907 # Build up sqlite3 command to create users, groups tables and populate them
12908 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
12909
12910 if (open(my $fh, "> $db_script")) {
12911 print $fh <<EOS;
12912 CREATE TABLE ftpsessions (
12913 user TEXT,
12914 ip_addr TEXT,
12915 ts TEXT
12916 );
12917 EOS
12918
12919 unless (close($fh)) {
12920 die("Can't write $db_script: $!");
12921 }
12922
12923 } else {
12924 die("Can't open $db_script: $!");
12925 }
12926
12927 my $cmd = "sqlite3 $db_file < $db_script";
12928 build_db($cmd, $db_script);
12929
12930 # Make sure that, if we're running as root, the database file has
12931 # the permissions/privs set for use by proftpd
12932 if ($< == 0) {
12933 unless (chmod(0666, $db_file)) {
12934 die("Can't set perms on $db_file to 0666: $!");
12935 }
12936 }
12937
12938 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
12939 if (open(my $fh, "> $src_file")) {
12940 close($fh);
12941
12942 } else {
12943 die("Can't open $src_file: $!");
12944 }
12945
12946 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
12947
12948 my $config = {
12949 PidFile => $pid_file,
12950 ScoreboardFile => $scoreboard_file,
12951 SystemLog => $log_file,
12952
12953 AuthUserFile => $auth_user_file,
12954 AuthGroupFile => $auth_group_file,
12955 AuthOrder => 'mod_auth_file.c',
12956
12957 IfModules => {
12958 'mod_delay.c' => {
12959 DelayEngine => 'off',
12960 },
12961
12962 'mod_sql.c' => {
12963 SQLEngine => 'log',
12964 SQLBackend => 'sqlite3',
12965 SQLConnectInfo => $db_file,
12966 SQLLogFile => $log_file,
12967 SQLNamedQuery => 'login FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ts) VALUES (\'%u\', \'%L\', \'%{millisecs}\')"',
12968 SQLLog => 'PASS login',
12969 },
12970 },
12971 };
12972
12973 my ($port, $config_user, $config_group) = config_write($config_file, $config);
12974
12975 # Open pipes, for use between the parent and child processes. Specifically,
12976 # the child will indicate when it's done with its test by writing a message
12977 # to the parent.
12978 my ($rfh, $wfh);
12979 unless (pipe($rfh, $wfh)) {
12980 die("Can't open pipe: $!");
12981 }
12982
12983 my $ex;
12984
12985 # Fork child
12986 $self->handle_sigchld();
12987 defined(my $pid = fork()) or die("Can't fork: $!");
12988 if ($pid) {
12989 eval {
12990 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
12991 $client->login($user, $passwd);
12992 $client->quit();
12993 };
12994
12995 if ($@) {
12996 $ex = $@;
12997 }
12998
12999 $wfh->print("done\n");
13000 $wfh->flush();
13001
13002 } else {
13003 eval { server_wait($config_file, $rfh) };
13004 if ($@) {
13005 warn($@);
13006 exit 1;
13007 }
13008
13009 exit 0;
13010 }
13011
13012 # Stop server
13013 server_stop($pid_file);
13014
13015 $self->assert_child_ok($pid);
13016
13017 if ($ex) {
13018 test_append_logfile($log_file, $ex);
13019 unlink($log_file);
13020
13021 die($ex);
13022 }
13023
13024 my ($login, $ip_addr, $ts) = get_logins($db_file, "user = \'$user\'");
13025
13026 my $expected;
13027
13028 $expected = $user;
13029 $self->assert($expected eq $login,
13030 test_msg("Expected login user '$expected', got '$login'"));
13031
13032 $expected = '127.0.0.1';
13033 $self->assert($expected eq $ip_addr,
13034 test_msg("Expected IP address '$expected', got '$ip_addr'"));
13035
13036 $expected = '\d{3}';
13037 $self->assert(qr/$expected/, $ts,
13038 test_msg("Expected timestamp '$expected', got '$ts'"));
13039
13040 unlink($log_file);
13041 }
13042
13043 sub sql_sqllog_var_iso8601_ts_bug3889 {
13044 my $self = shift;
13045 my $tmpdir = $self->{tmpdir};
13046
13047 my $config_file = "$tmpdir/sqlite.conf";
13048 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
13049 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
13050
13051 my $log_file = test_get_logfile();
13052
13053 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
13054 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
13055
13056 my $user = 'proftpd';
13057 my $passwd = 'test';
13058 my $group = 'ftpd';
13059 my $home_dir = File::Spec->rel2abs($tmpdir);
13060 my $uid = 500;
13061 my $gid = 500;
13062
13063 # Make sure that, if we're running as root, that the home directory has
13064 # permissions/privs set for the account we create
13065 if ($< == 0) {
13066 unless (chmod(0755, $home_dir)) {
13067 die("Can't set perms on $home_dir to 0755: $!");
13068 }
13069
13070 unless (chown($uid, $gid, $home_dir)) {
13071 die("Can't set owner of $home_dir to $uid/$gid: $!");
13072 }
13073 }
13074
13075 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
13076 '/bin/bash');
13077 auth_group_write($auth_group_file, $group, $gid, $user);
13078
13079 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
13080
13081 # Build up sqlite3 command to create users, groups tables and populate them
13082 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
13083
13084 if (open(my $fh, "> $db_script")) {
13085 print $fh <<EOS;
13086 CREATE TABLE ftpsessions (
13087 user TEXT,
13088 ip_addr TEXT,
13089 ts TEXT
13090 );
13091 EOS
13092
13093 unless (close($fh)) {
13094 die("Can't write $db_script: $!");
13095 }
13096
13097 } else {
13098 die("Can't open $db_script: $!");
13099 }
13100
13101 my $cmd = "sqlite3 $db_file < $db_script";
13102 build_db($cmd, $db_script);
13103
13104 # Make sure that, if we're running as root, the database file has
13105 # the permissions/privs set for use by proftpd
13106 if ($< == 0) {
13107 unless (chmod(0666, $db_file)) {
13108 die("Can't set perms on $db_file to 0666: $!");
13109 }
13110 }
13111
13112 my $src_file = File::Spec->rel2abs("$tmpdir/test.txt");
13113 if (open(my $fh, "> $src_file")) {
13114 close($fh);
13115
13116 } else {
13117 die("Can't open $src_file: $!");
13118 }
13119
13120 my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt");
13121
13122 my $config = {
13123 PidFile => $pid_file,
13124 ScoreboardFile => $scoreboard_file,
13125 SystemLog => $log_file,
13126
13127 AuthUserFile => $auth_user_file,
13128 AuthGroupFile => $auth_group_file,
13129 AuthOrder => 'mod_auth_file.c',
13130
13131 IfModules => {
13132 'mod_delay.c' => {
13133 DelayEngine => 'off',
13134 },
13135
13136 'mod_sql.c' => {
13137 SQLEngine => 'log',
13138 SQLBackend => 'sqlite3',
13139 SQLConnectInfo => $db_file,
13140 SQLLogFile => $log_file,
13141 SQLNamedQuery => 'login FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ts) VALUES (\'%u\', \'%L\', \'%{iso8601}\')"',
13142 SQLLog => 'PASS login',
13143 },
13144 },
13145 };
13146
13147 my ($port, $config_user, $config_group) = config_write($config_file, $config);
13148
13149 # Open pipes, for use between the parent and child processes. Specifically,
13150 # the child will indicate when it's done with its test by writing a message
13151 # to the parent.
13152 my ($rfh, $wfh);
13153 unless (pipe($rfh, $wfh)) {
13154 die("Can't open pipe: $!");
13155 }
13156
13157 my $ex;
13158
13159 # Fork child
13160 $self->handle_sigchld();
13161 defined(my $pid = fork()) or die("Can't fork: $!");
13162 if ($pid) {
13163 eval {
13164 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
13165 $client->login($user, $passwd);
13166 $client->quit();
13167 };
13168
13169 if ($@) {
13170 $ex = $@;
13171 }
13172
13173 $wfh->print("done\n");
13174 $wfh->flush();
13175
13176 } else {
13177 eval { server_wait($config_file, $rfh) };
13178 if ($@) {
13179 warn($@);
13180 exit 1;
13181 }
13182
13183 exit 0;
13184 }
13185
13186 # Stop server
13187 server_stop($pid_file);
13188
13189 $self->assert_child_ok($pid);
13190
13191 if ($ex) {
13192 test_append_logfile($log_file, $ex);
13193 unlink($log_file);
13194
13195 die($ex);
13196 }
13197
13198 my ($login, $ip_addr, $ts) = get_logins($db_file, "user = \'$user\'");
13199
13200 my $expected;
13201
13202 $expected = $user;
13203 $self->assert($expected eq $login,
13204 test_msg("Expected login user '$expected', got '$login'"));
13205
13206 $expected = '127.0.0.1';
13207 $self->assert($expected eq $ip_addr,
13208 test_msg("Expected IP address '$expected', got '$ip_addr'"));
13209
13210 $expected = '\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2},\d{3}';
13211 $self->assert(qr/$expected/, $ts,
13212 test_msg("Expected timestamp '$expected', got '$ts'"));
13213
13214 unlink($log_file);
13215 }
13216
13217 sub sql_sqllogonevent_bug3893 {
13218 my $self = shift;
13219 my $tmpdir = $self->{tmpdir};
13220 my $setup = test_setup($tmpdir, 'sqlite');
13221
13222 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
13223
13224 # Build up sqlite3 command to create users, groups tables and populate them
13225 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
13226
13227 if (open(my $fh, "> $db_script")) {
13228 print $fh <<EOS;
13229 CREATE TABLE ftpsessions (
13230 user TEXT,
13231 ip_addr TEXT,
13232 timestamp TEXT
13233 );
13234 EOS
13235
13236 unless (close($fh)) {
13237 die("Can't write $db_script: $!");
13238 }
13239
13240 } else {
13241 die("Can't open $db_script: $!");
13242 }
13243
13244 my $cmd = "sqlite3 $db_file < $db_script";
13245 build_db($cmd, $db_script);
13246
13247 # Make sure that, if we're running as root, the database file has
13248 # the permissions/privs set for use by proftpd
13249 if ($< == 0) {
13250 unless (chmod(0666, $db_file)) {
13251 die("Can't set perms on $db_file to 0666: $!");
13252 }
13253 }
13254
13255 my $max_login_attempts = 2;
13256
13257 my $config = {
13258 PidFile => $setup->{pid_file},
13259 ScoreboardFile => $setup->{scoreboard_file},
13260 SystemLog => $setup->{log_file},
13261 TraceLog => $setup->{log_file},
13262 Trace => 'jot:20 sql:20 sql.sqlite:20',
13263
13264 AuthUserFile => $setup->{auth_user_file},
13265 AuthGroupFile => $setup->{auth_group_file},
13266 AuthOrder => 'mod_auth_file.c',
13267
13268 MaxLoginAttempts => $max_login_attempts,
13269
13270 IfModules => {
13271 'mod_delay.c' => {
13272 DelayEngine => 'off',
13273 },
13274
13275 'mod_sql.c' => {
13276 SQLEngine => 'log',
13277 SQLBackend => 'sqlite3',
13278 SQLConnectInfo => $db_file,
13279 SQLLogFile => $setup->{log_file},
13280 SQLNamedQuery => 'max_logins_exceeded FREEFORM "INSERT INTO ftpsessions (user, ip_addr, timestamp) VALUES (\'%U\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')"',
13281 SQLLogOnEvent => 'MaxLoginAttempts max_logins_exceeded',
13282 },
13283 },
13284 };
13285
13286 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
13287 $config);
13288
13289 # Open pipes, for use between the parent and child processes. Specifically,
13290 # the child will indicate when it's done with its test by writing a message
13291 # to the parent.
13292 my ($rfh, $wfh);
13293 unless (pipe($rfh, $wfh)) {
13294 die("Can't open pipe: $!");
13295 }
13296
13297 my $ex;
13298
13299 # Fork child
13300 $self->handle_sigchld();
13301 defined(my $pid = fork()) or die("Can't fork: $!");
13302 if ($pid) {
13303 eval {
13304 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
13305 eval { $client->login($setup->{user}, 'badpassword') };
13306 unless ($@) {
13307 die("Login succeeded unexpectedly");
13308 }
13309
13310 eval { $client->login($setup->{user}, 'badpassword') };
13311 unless ($@) {
13312 die("Login succeeded unexpectedly");
13313 }
13314 };
13315 if ($@) {
13316 $ex = $@;
13317 }
13318
13319 $wfh->print("done\n");
13320 $wfh->flush();
13321
13322 } else {
13323 eval { server_wait($setup->{config_file}, $rfh) };
13324 if ($@) {
13325 warn($@);
13326 exit 1;
13327 }
13328
13329 exit 0;
13330 }
13331
13332 # Stop server
13333 server_stop($setup->{pid_file});
13334 $self->assert_child_ok($pid);
13335
13336 if ($ex) {
13337 test_cleanup($setup->{log_file}, $ex);
13338 }
13339
13340 eval {
13341 my ($login, $ip_addr, $timestamp) = get_sessions($db_file,
13342 "user = \'$setup->{user}\'");
13343
13344 my $expected = $setup->{user};
13345 $self->assert($expected eq $login,
13346 test_msg("Expected '$expected', got '$login'"));
13347
13348 $expected = '127.0.0.1';
13349 $self->assert($expected eq $ip_addr,
13350 test_msg("Expected '$expected', got '$ip_addr'"));
13351
13352 $expected = '\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}';
13353 $self->assert(qr/$expected/, $timestamp,
13354 test_msg("Expected '$expected', got '$timestamp'"));
13355 };
13356 if ($@) {
13357 $ex = $@;
13358 }
13359
13360 test_cleanup($setup->{log_file}, $ex);
13361 }
13362
13363 sub get_session_with_primary_key {
13364 my $db_file = shift;
13365 my $where = shift;
13366
13367 my $sql = "SELECT name, ip_addr, primary_key FROM sessions";
13368 if ($where) {
13369 $sql .= " WHERE $where";
13370 }
13371
13372 my $cmd = "sqlite3 $db_file \"$sql\"";
13373
13374 if ($ENV{TEST_VERBOSE}) {
13375 print STDERR "Executing sqlite3: $cmd\n";
13376 }
13377
13378 my $res = join('', `$cmd`);
13379 chomp($res);
13380
13381 # The default sqlite3 delimiter is '|'
13382 return split(/\|/, $res);
13383 }
13384
13385 sub sql_userprimarykey_bug3864 {
13386 my $self = shift;
13387 my $tmpdir = $self->{tmpdir};
13388
13389 my $config_file = "$tmpdir/sqlite.conf";
13390 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
13391 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
13392
13393 my $log_file = test_get_logfile();
13394
13395 my $user = 'proftpd';
13396 my $passwd = 'test';
13397 my $group = 'ftpd';
13398 my $home_dir = File::Spec->rel2abs($tmpdir);
13399 my $uid = 500;
13400 my $gid = 500;
13401
13402 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
13403
13404 # Build up sqlite3 command to create users, groups tables and populate them
13405 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
13406
13407 if (open(my $fh, "> $db_script")) {
13408 print $fh <<EOS;
13409 CREATE TABLE users (
13410 userid TEXT,
13411 passwd TEXT,
13412 uid INTEGER,
13413 gid INTEGER,
13414 homedir TEXT,
13415 shell TEXT
13416 );
13417 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
13418
13419 CREATE TABLE groups (
13420 groupname TEXT,
13421 gid INTEGER,
13422 members TEXT
13423 );
13424 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
13425
13426 CREATE TABLE sessions (
13427 name TEXT,
13428 ip_addr TEXT,
13429 primary_key INTEGER
13430 );
13431 EOS
13432
13433 unless (close($fh)) {
13434 die("Can't write $db_script: $!");
13435 }
13436
13437 } else {
13438 die("Can't open $db_script: $!");
13439 }
13440
13441 my $cmd = "sqlite3 $db_file < $db_script";
13442 build_db($cmd, $db_script);
13443
13444 # Make sure that, if we're running as root, the database file has
13445 # the permissions/privs set for use by proftpd
13446 if ($< == 0) {
13447 unless (chmod(0666, $db_file)) {
13448 die("Can't set perms on $db_file to 0666: $!");
13449 }
13450 }
13451
13452 my $config = {
13453 PidFile => $pid_file,
13454 ScoreboardFile => $scoreboard_file,
13455 SystemLog => $log_file,
13456 TraceLog => $log_file,
13457 Trace => 'sql:20',
13458
13459 IfModules => {
13460 'mod_delay.c' => {
13461 DelayEngine => 'off',
13462 },
13463
13464 'mod_sql.c' => {
13465 SQLAuthTypes => 'plaintext',
13466 SQLBackend => 'sqlite3',
13467 SQLConnectInfo => $db_file,
13468 SQLLogFile => $log_file,
13469 SQLMinID => 200,
13470
13471 SQLUserPrimaryKey => 'uid',
13472
13473 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO sessions (name, ip_addr, primary_key) VALUES (\'%u\', \'%L\', %{note:sql.user-primary-key})"',
13474 SQLLog => 'PASS session_start',
13475 },
13476 },
13477 };
13478
13479 my ($port, $config_user, $config_group) = config_write($config_file, $config);
13480
13481 # Open pipes, for use between the parent and child processes. Specifically,
13482 # the child will indicate when it's done with its test by writing a message
13483 # to the parent.
13484 my ($rfh, $wfh);
13485 unless (pipe($rfh, $wfh)) {
13486 die("Can't open pipe: $!");
13487 }
13488
13489 my $ex;
13490
13491 # Fork child
13492 $self->handle_sigchld();
13493 defined(my $pid = fork()) or die("Can't fork: $!");
13494 if ($pid) {
13495 eval {
13496 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
13497 $client->login($user, $passwd);
13498 $client->quit();
13499 };
13500
13501 if ($@) {
13502 $ex = $@;
13503 }
13504
13505 $wfh->print("done\n");
13506 $wfh->flush();
13507
13508 } else {
13509 eval { server_wait($config_file, $rfh) };
13510 if ($@) {
13511 warn($@);
13512 exit 1;
13513 }
13514
13515 exit 0;
13516 }
13517
13518 # Stop server
13519 server_stop($pid_file);
13520
13521 $self->assert_child_ok($pid);
13522
13523 if ($ex) {
13524 test_append_logfile($log_file, $ex);
13525 unlink($log_file);
13526
13527 die($ex);
13528 }
13529
13530 eval {
13531 my ($name, $ip_addr, $primary_key) = get_session_with_primary_key($db_file,
13532 "name = \'$user\'");
13533
13534 my $expected;
13535
13536 $expected = $user;
13537 $self->assert($expected eq $name,
13538 test_msg("Expected name '$expected', got '$name'"));
13539
13540 $expected = '127.0.0.1';
13541 $self->assert($expected eq $ip_addr,
13542 test_msg("Expected IP address '$expected', got '$ip_addr'"));
13543
13544 $expected = $uid;
13545 $self->assert($expected == $primary_key,
13546 test_msg("Expected primary key $expected, got $primary_key"));
13547 };
13548 if ($@) {
13549 $ex = $@;
13550 }
13551
13552 if ($ex) {
13553 test_append_logfile($log_file, $ex);
13554 unlink($log_file);
13555
13556 die($ex);
13557 }
13558
13559 unlink($log_file);
13560 }
13561
13562 sub sql_userprimarykey_custom_bug3864 {
13563 my $self = shift;
13564 my $tmpdir = $self->{tmpdir};
13565
13566 my $config_file = "$tmpdir/sqlite.conf";
13567 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
13568 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
13569
13570 my $log_file = test_get_logfile();
13571
13572 my $user = 'proftpd';
13573 my $passwd = 'test';
13574 my $group = 'ftpd';
13575 my $home_dir = File::Spec->rel2abs($tmpdir);
13576 my $uid = 500;
13577 my $gid = 500;
13578
13579 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
13580
13581 # Build up sqlite3 command to create users, groups tables and populate them
13582 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
13583
13584 if (open(my $fh, "> $db_script")) {
13585 print $fh <<EOS;
13586 CREATE TABLE users (
13587 userid TEXT,
13588 passwd TEXT,
13589 uid INTEGER,
13590 gid INTEGER,
13591 homedir TEXT,
13592 shell TEXT,
13593 primary_key INTEGER
13594 );
13595 INSERT INTO users (userid, passwd, uid, gid, homedir, shell, primary_key) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash', $uid);
13596
13597 CREATE TABLE groups (
13598 groupname TEXT,
13599 gid INTEGER,
13600 members TEXT
13601 );
13602 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
13603
13604 CREATE TABLE sessions (
13605 name TEXT,
13606 ip_addr TEXT,
13607 primary_key INTEGER
13608 );
13609 EOS
13610
13611 unless (close($fh)) {
13612 die("Can't write $db_script: $!");
13613 }
13614
13615 } else {
13616 die("Can't open $db_script: $!");
13617 }
13618
13619 my $cmd = "sqlite3 $db_file < $db_script";
13620 build_db($cmd, $db_script);
13621
13622 # Make sure that, if we're running as root, the database file has
13623 # the permissions/privs set for use by proftpd
13624 if ($< == 0) {
13625 unless (chmod(0666, $db_file)) {
13626 die("Can't set perms on $db_file to 0666: $!");
13627 }
13628 }
13629
13630 my $config = {
13631 PidFile => $pid_file,
13632 ScoreboardFile => $scoreboard_file,
13633 SystemLog => $log_file,
13634 TraceLog => $log_file,
13635 Trace => 'sql:20',
13636
13637 IfModules => {
13638 'mod_delay.c' => {
13639 DelayEngine => 'off',
13640 },
13641
13642 'mod_sql.c' => [
13643 'SQLAuthTypes plaintext',
13644 'SQLBackend sqlite3',
13645 "SQLConnectInfo $db_file",
13646 "SQLLogFile $log_file",
13647 'SQLMinID 200',
13648
13649 'SQLNamedQuery get-user-primary-key SELECT "primary_key FROM users WHERE userid = \'%{0}\'"',
13650 'SQLUserPrimaryKey custom:/get-user-primary-key',
13651
13652 'SQLNamedQuery session_start FREEFORM "INSERT INTO sessions (name, ip_addr, primary_key) VALUES (\'%u\', \'%L\', %{note:sql.user-primary-key})"',
13653 'SQLLog PASS session_start',
13654 ],
13655 },
13656 };
13657
13658 my ($port, $config_user, $config_group) = config_write($config_file, $config);
13659
13660 # Open pipes, for use between the parent and child processes. Specifically,
13661 # the child will indicate when it's done with its test by writing a message
13662 # to the parent.
13663 my ($rfh, $wfh);
13664 unless (pipe($rfh, $wfh)) {
13665 die("Can't open pipe: $!");
13666 }
13667
13668 my $ex;
13669
13670 # Fork child
13671 $self->handle_sigchld();
13672 defined(my $pid = fork()) or die("Can't fork: $!");
13673 if ($pid) {
13674 eval {
13675 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
13676 $client->login($user, $passwd);
13677 $client->quit();
13678 };
13679
13680 if ($@) {
13681 $ex = $@;
13682 }
13683
13684 $wfh->print("done\n");
13685 $wfh->flush();
13686
13687 } else {
13688 eval { server_wait($config_file, $rfh) };
13689 if ($@) {
13690 warn($@);
13691 exit 1;
13692 }
13693
13694 exit 0;
13695 }
13696
13697 # Stop server
13698 server_stop($pid_file);
13699
13700 $self->assert_child_ok($pid);
13701
13702 if ($ex) {
13703 test_append_logfile($log_file, $ex);
13704 unlink($log_file);
13705
13706 die($ex);
13707 }
13708
13709 eval {
13710 my ($name, $ip_addr, $primary_key) = get_session_with_primary_key($db_file,
13711 "name = \'$user\'");
13712
13713 my $expected;
13714
13715 $expected = $user;
13716 $self->assert($expected eq $name,
13717 test_msg("Expected name '$expected', got '$name'"));
13718
13719 $expected = '127.0.0.1';
13720 $self->assert($expected eq $ip_addr,
13721 test_msg("Expected IP address '$expected', got '$ip_addr'"));
13722
13723 $expected = $uid;
13724 $self->assert($expected == $primary_key,
13725 test_msg("Expected primary key $expected, got $primary_key"));
13726 };
13727 if ($@) {
13728 $ex = $@;
13729 }
13730
13731 if ($ex) {
13732 test_append_logfile($log_file, $ex);
13733 unlink($log_file);
13734
13735 die($ex);
13736 }
13737
13738 unlink($log_file);
13739 }
13740
13741 sub sql_groupprimarykey_bug3864 {
13742 my $self = shift;
13743 my $tmpdir = $self->{tmpdir};
13744
13745 my $config_file = "$tmpdir/sqlite.conf";
13746 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
13747 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
13748
13749 my $log_file = test_get_logfile();
13750
13751 my $user = 'proftpd';
13752 my $passwd = 'test';
13753 my $group = 'ftpd';
13754 my $home_dir = File::Spec->rel2abs($tmpdir);
13755 my $uid = 500;
13756 my $gid = 500;
13757
13758 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
13759
13760 # Build up sqlite3 command to create users, groups tables and populate them
13761 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
13762
13763 if (open(my $fh, "> $db_script")) {
13764 print $fh <<EOS;
13765 CREATE TABLE users (
13766 userid TEXT,
13767 passwd TEXT,
13768 uid INTEGER,
13769 gid INTEGER,
13770 homedir TEXT,
13771 shell TEXT
13772 );
13773 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
13774
13775 CREATE TABLE groups (
13776 groupname TEXT,
13777 gid INTEGER,
13778 members TEXT
13779 );
13780 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
13781
13782 CREATE TABLE sessions (
13783 name TEXT,
13784 ip_addr TEXT,
13785 primary_key INTEGER
13786 );
13787 EOS
13788
13789 unless (close($fh)) {
13790 die("Can't write $db_script: $!");
13791 }
13792
13793 } else {
13794 die("Can't open $db_script: $!");
13795 }
13796
13797 my $cmd = "sqlite3 $db_file < $db_script";
13798 build_db($cmd, $db_script);
13799
13800 # Make sure that, if we're running as root, the database file has
13801 # the permissions/privs set for use by proftpd
13802 if ($< == 0) {
13803 unless (chmod(0666, $db_file)) {
13804 die("Can't set perms on $db_file to 0666: $!");
13805 }
13806 }
13807
13808 my $config = {
13809 PidFile => $pid_file,
13810 ScoreboardFile => $scoreboard_file,
13811 SystemLog => $log_file,
13812 TraceLog => $log_file,
13813 Trace => 'sql:20',
13814
13815 IfModules => {
13816 'mod_delay.c' => {
13817 DelayEngine => 'off',
13818 },
13819
13820 'mod_sql.c' => {
13821 SQLAuthTypes => 'plaintext',
13822 SQLBackend => 'sqlite3',
13823 SQLConnectInfo => $db_file,
13824 SQLLogFile => $log_file,
13825 SQLMinID => 200,
13826
13827 SQLGroupPrimaryKey => 'gid',
13828
13829 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO sessions (name, ip_addr, primary_key) VALUES (\'%g\', \'%L\', %{note:sql.group-primary-key})"',
13830 SQLLog => 'PASS session_start',
13831 },
13832 },
13833 };
13834
13835 my ($port, $config_user, $config_group) = config_write($config_file, $config);
13836
13837 # Open pipes, for use between the parent and child processes. Specifically,
13838 # the child will indicate when it's done with its test by writing a message
13839 # to the parent.
13840 my ($rfh, $wfh);
13841 unless (pipe($rfh, $wfh)) {
13842 die("Can't open pipe: $!");
13843 }
13844
13845 my $ex;
13846
13847 # Fork child
13848 $self->handle_sigchld();
13849 defined(my $pid = fork()) or die("Can't fork: $!");
13850 if ($pid) {
13851 eval {
13852 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
13853 $client->login($user, $passwd);
13854 $client->quit();
13855 };
13856
13857 if ($@) {
13858 $ex = $@;
13859 }
13860
13861 $wfh->print("done\n");
13862 $wfh->flush();
13863
13864 } else {
13865 eval { server_wait($config_file, $rfh) };
13866 if ($@) {
13867 warn($@);
13868 exit 1;
13869 }
13870
13871 exit 0;
13872 }
13873
13874 # Stop server
13875 server_stop($pid_file);
13876
13877 $self->assert_child_ok($pid);
13878
13879 if ($ex) {
13880 test_append_logfile($log_file, $ex);
13881 unlink($log_file);
13882
13883 die($ex);
13884 }
13885
13886 eval {
13887 my ($name, $ip_addr, $primary_key) = get_session_with_primary_key($db_file,
13888 "name = \'$group\'");
13889
13890 my $expected;
13891
13892 $expected = $group;
13893 $self->assert($expected eq $name,
13894 test_msg("Expected name '$expected', got '$name'"));
13895
13896 $expected = '127.0.0.1';
13897 $self->assert($expected eq $ip_addr,
13898 test_msg("Expected IP address '$expected', got '$ip_addr'"));
13899
13900 $expected = $uid;
13901 $self->assert($expected == $primary_key,
13902 test_msg("Expected primary key $expected, got $primary_key"));
13903 };
13904 if ($@) {
13905 $ex = $@;
13906 }
13907
13908 if ($ex) {
13909 test_append_logfile($log_file, $ex);
13910 unlink($log_file);
13911
13912 die($ex);
13913 }
13914
13915 unlink($log_file);
13916 }
13917
13918 sub sql_groupprimarykey_custom_bug3864 {
13919 my $self = shift;
13920 my $tmpdir = $self->{tmpdir};
13921 my $setup = test_setup($tmpdir, 'sqlite');
13922
13923 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
13924
13925 # Build up sqlite3 command to create users, groups tables and populate them
13926 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
13927
13928 if (open(my $fh, "> $db_script")) {
13929 print $fh <<EOS;
13930 CREATE TABLE users (
13931 userid TEXT,
13932 passwd TEXT,
13933 uid INTEGER,
13934 gid INTEGER,
13935 homedir TEXT,
13936 shell TEXT
13937 );
13938 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash');
13939
13940 CREATE TABLE groups (
13941 groupname TEXT,
13942 gid INTEGER,
13943 members TEXT,
13944 primary_key INTEGER
13945 );
13946 INSERT INTO groups (groupname, gid, members, primary_key) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}', $setup->{gid});
13947
13948 CREATE TABLE sessions (
13949 name TEXT,
13950 ip_addr TEXT,
13951 primary_key INTEGER
13952 );
13953 EOS
13954 unless (close($fh)) {
13955 die("Can't write $db_script: $!");
13956 }
13957
13958 } else {
13959 die("Can't open $db_script: $!");
13960 }
13961
13962 my $cmd = "sqlite3 $db_file < $db_script";
13963 build_db($cmd, $db_script);
13964
13965 # Make sure that, if we're running as root, the database file has
13966 # the permissions/privs set for use by proftpd
13967 if ($< == 0) {
13968 unless (chmod(0666, $db_file)) {
13969 die("Can't set perms on $db_file to 0666: $!");
13970 }
13971 }
13972
13973 my $config = {
13974 PidFile => $setup->{pid_file},
13975 ScoreboardFile => $setup->{scoreboard_file},
13976 SystemLog => $setup->{log_file},
13977 TraceLog => $setup->{log_file},
13978 Trace => 'jot:20 sql:20',
13979
13980 IfModules => {
13981 'mod_delay.c' => {
13982 DelayEngine => 'off',
13983 },
13984
13985 'mod_sql.c' => [
13986 'AuthOrder mod_sql.c',
13987 'SQLAuthTypes plaintext',
13988 'SQLBackend sqlite3',
13989 "SQLConnectInfo $db_file",
13990 "SQLLogFile $setup->{log_file}",
13991 'SQLMinID 10',
13992
13993 'SQLNamedQuery get-group-primary-key SELECT "primary_key from groups WHERE groupname = \'%{0}\'"',
13994 'SQLGroupPrimaryKey custom:/get-group-primary-key',
13995
13996 'SQLNamedQuery session_start FREEFORM "INSERT INTO sessions (name, ip_addr, primary_key) VALUES (\'%g\', \'%L\', %{note:sql.group-primary-key})"',
13997 'SQLLog PASS session_start',
13998 ],
13999 },
14000 };
14001
14002 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
14003 $config);
14004
14005 # Open pipes, for use between the parent and child processes. Specifically,
14006 # the child will indicate when it's done with its test by writing a message
14007 # to the parent.
14008 my ($rfh, $wfh);
14009 unless (pipe($rfh, $wfh)) {
14010 die("Can't open pipe: $!");
14011 }
14012
14013 my $ex;
14014
14015 # Fork child
14016 $self->handle_sigchld();
14017 defined(my $pid = fork()) or die("Can't fork: $!");
14018 if ($pid) {
14019 eval {
14020 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
14021 $client->login($setup->{user}, $setup->{passwd});
14022 $client->quit();
14023 };
14024 if ($@) {
14025 $ex = $@;
14026 }
14027
14028 $wfh->print("done\n");
14029 $wfh->flush();
14030
14031 } else {
14032 eval { server_wait($setup->{config_file}, $rfh) };
14033 if ($@) {
14034 warn($@);
14035 exit 1;
14036 }
14037
14038 exit 0;
14039 }
14040
14041 # Stop server
14042 server_stop($setup->{pid_file});
14043 $self->assert_child_ok($pid);
14044
14045 if ($ex) {
14046 test_cleanup($setup->{log_file}, $ex);
14047 }
14048
14049 eval {
14050 my ($name, $ip_addr, $primary_key) = get_session_with_primary_key($db_file,
14051 "name = \'$setup->{group}\'");
14052
14053 my $expected = $setup->{group};
14054 $self->assert($expected eq $name, "Expected name '$expected', got '$name'");
14055
14056 $expected = '127.0.0.1';
14057 $self->assert($expected eq $ip_addr,
14058 "Expected IP address '$expected', got '$ip_addr'");
14059
14060 $expected = $setup->{gid};
14061 $self->assert($expected == $primary_key,
14062 "Expected primary key $expected, got $primary_key");
14063 };
14064 if ($@) {
14065 $ex = $@;
14066 }
14067
14068 test_cleanup($setup->{log_file}, $ex);
14069 }
14070
14071 sub sql_sqllog_var_basename_bug3987 {
14072 my $self = shift;
14073 my $tmpdir = $self->{tmpdir};
14074
14075 my $config_file = "$tmpdir/sqlite.conf";
14076 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
14077 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
14078
14079 my $log_file = test_get_logfile();
14080
14081 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
14082 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
14083
14084 my $user = 'proftpd';
14085 my $passwd = 'test';
14086 my $group = 'ftpd';
14087 my $home_dir = File::Spec->rel2abs($tmpdir);
14088 my $uid = 500;
14089 my $gid = 500;
14090
14091 # Make sure that, if we're running as root, that the home directory has
14092 # permissions/privs set for the account we create
14093 if ($< == 0) {
14094 unless (chmod(0755, $home_dir)) {
14095 die("Can't set perms on $home_dir to 0755: $!");
14096 }
14097
14098 unless (chown($uid, $gid, $home_dir)) {
14099 die("Can't set owner of $home_dir to $uid/$gid: $!");
14100 }
14101 }
14102
14103 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
14104 '/bin/bash');
14105 auth_group_write($auth_group_file, $group, $gid, $user);
14106
14107 my $test_file = File::Spec->rel2abs($config_file);
14108
14109 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
14110
14111 # Build up sqlite3 command to create users, groups tables and populate them
14112 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
14113
14114 if (open(my $fh, "> $db_script")) {
14115 print $fh <<EOS;
14116 CREATE TABLE ftpsessions (
14117 user TEXT,
14118 ip_addr TEXT,
14119 dir TEXT
14120 );
14121 EOS
14122
14123 unless (close($fh)) {
14124 die("Can't write $db_script: $!");
14125 }
14126
14127 } else {
14128 die("Can't open $db_script: $!");
14129 }
14130
14131 my $cmd = "sqlite3 $db_file < $db_script";
14132 build_db($cmd, $db_script);
14133
14134 # Make sure that, if we're running as root, the database file has
14135 # the permissions/privs set for use by proftpd
14136 if ($< == 0) {
14137 unless (chmod(0666, $db_file)) {
14138 die("Can't set perms on $db_file to 0666: $!");
14139 }
14140 }
14141
14142 my $config = {
14143 PidFile => $pid_file,
14144 ScoreboardFile => $scoreboard_file,
14145 SystemLog => $log_file,
14146
14147 AuthUserFile => $auth_user_file,
14148 AuthGroupFile => $auth_group_file,
14149 AuthOrder => 'mod_auth_file.c',
14150
14151 IfModules => {
14152 'mod_delay.c' => {
14153 DelayEngine => 'off',
14154 },
14155
14156 'mod_sql.c' => {
14157 SQLEngine => 'log',
14158 SQLBackend => 'sqlite3',
14159 SQLConnectInfo => $db_file,
14160 SQLLogFile => $log_file,
14161 SQLNamedQuery => 'filename FREEFORM "INSERT INTO ftpsessions (user, ip_addr, dir) VALUES (\'%u\', \'%L\', \'%{basename}\')"',
14162 SQLLog => 'RETR filename',
14163 },
14164 },
14165 };
14166
14167 my ($port, $config_user, $config_group) = config_write($config_file, $config);
14168
14169 # Open pipes, for use between the parent and child processes. Specifically,
14170 # the child will indicate when it's done with its test by writing a message
14171 # to the parent.
14172 my ($rfh, $wfh);
14173 unless (pipe($rfh, $wfh)) {
14174 die("Can't open pipe: $!");
14175 }
14176
14177 my $ex;
14178
14179 # Fork child
14180 $self->handle_sigchld();
14181 defined(my $pid = fork()) or die("Can't fork: $!");
14182 if ($pid) {
14183 eval {
14184 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
14185 $client->login($user, $passwd);
14186
14187 my $conn = $client->retr_raw($test_file);
14188 unless ($conn) {
14189 die("Failed to RETR: " . $client->response_code() . " " .
14190 $client->response_msg());
14191 }
14192
14193 my $buf;
14194 $conn->read($buf, 8192, 30);
14195 eval { $conn->close() };
14196
14197 my $resp_code = $client->response_code();
14198 my $resp_msg = $client->response_msg();
14199 $self->assert_transfer_ok($resp_code, $resp_msg);
14200
14201 $client->quit();
14202 };
14203
14204 if ($@) {
14205 $ex = $@;
14206 }
14207
14208 $wfh->print("done\n");
14209 $wfh->flush();
14210
14211 } else {
14212 eval { server_wait($config_file, $rfh) };
14213 if ($@) {
14214 warn($@);
14215 exit 1;
14216 }
14217
14218 exit 0;
14219 }
14220
14221 # Stop server
14222 server_stop($pid_file);
14223
14224 $self->assert_child_ok($pid);
14225
14226 if ($ex) {
14227 test_append_logfile($log_file, $ex);
14228 unlink($log_file);
14229
14230 die($ex);
14231 }
14232
14233 # Note: We are simply re-using the existing get_locations() function here
14234 # for convenience/expedience.
14235 my ($login, $ip_addr, $name) = get_locations($db_file, "user = \'$user\'");
14236
14237 my $expected;
14238
14239 $expected = $user;
14240 $self->assert($expected eq $login,
14241 test_msg("Expected '$expected', got '$login'"));
14242
14243 $expected = '127.0.0.1';
14244 $self->assert($expected eq $ip_addr,
14245 test_msg("Expected '$expected', got '$ip_addr'"));
14246
14247 $expected = 'sqlite.conf';
14248 $self->assert($expected eq $name,
14249 test_msg("Expected '$expected', got '$name'"));
14250
14251 unlink($log_file);
14252 }
14253
14254 sub sql_user_info_defaulthomedir_bug4083 {
14255 my $self = shift;
14256 my $tmpdir = $self->{tmpdir};
14257
14258 my $config_file = "$tmpdir/sqlite.conf";
14259 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
14260 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
14261
14262 my $log_file = test_get_logfile();
14263
14264 my $user = 'proftpd';
14265 my $passwd = 'test';
14266 my $group = 'ftpd';
14267 my $home_dir = File::Spec->rel2abs("$tmpdir/$user");
14268 mkpath($home_dir);
14269 my $uid = 500;
14270 my $gid = 500;
14271
14272 my $default_home = File::Spec->rel2abs($tmpdir) . '/%u';
14273
14274 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
14275
14276 # Build up sqlite3 command to create users, groups tables and populate them
14277 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
14278
14279 if (open(my $fh, "> $db_script")) {
14280 print $fh <<EOS;
14281 CREATE TABLE users (
14282 userid TEXT,
14283 passwd TEXT,
14284 uid INTEGER,
14285 gid INTEGER
14286 );
14287 INSERT INTO users (userid, passwd, uid, gid) VALUES ('$user', '$passwd', $uid, $gid);
14288
14289 EOS
14290
14291 unless (close($fh)) {
14292 die("Can't write $db_script: $!");
14293 }
14294
14295 } else {
14296 die("Can't open $db_script: $!");
14297 }
14298
14299 my $cmd = "sqlite3 $db_file < $db_script";
14300 build_db($cmd, $db_script);
14301
14302 # Make sure that, if we're running as root, the database file has
14303 # the permissions/privs set for use by proftpd
14304 if ($< == 0) {
14305 unless (chmod(0666, $db_file)) {
14306 die("Can't set perms on $db_file to 0666: $!");
14307 }
14308 }
14309
14310 my $config = {
14311 PidFile => $pid_file,
14312 ScoreboardFile => $scoreboard_file,
14313 SystemLog => $log_file,
14314
14315 IfModules => {
14316 'mod_delay.c' => {
14317 DelayEngine => 'off',
14318 },
14319
14320 'mod_sql.c' => {
14321 SQLAuthenticate => 'users',
14322 SQLAuthTypes => 'plaintext',
14323 SQLBackend => 'sqlite3',
14324 SQLConnectInfo => $db_file,
14325 SQLLogFile => $log_file,
14326 SQLUserInfo => 'users userid passwd uid gid Null Null',
14327 SQLDefaultHomedir => $default_home,
14328 },
14329 },
14330 };
14331
14332 my ($port, $config_user, $config_group) = config_write($config_file, $config);
14333
14334 # Open pipes, for use between the parent and child processes. Specifically,
14335 # the child will indicate when it's done with its test by writing a message
14336 # to the parent.
14337 my ($rfh, $wfh);
14338 unless (pipe($rfh, $wfh)) {
14339 die("Can't open pipe: $!");
14340 }
14341
14342 my $ex;
14343
14344 # Fork child
14345 $self->handle_sigchld();
14346 defined(my $pid = fork()) or die("Can't fork: $!");
14347 if ($pid) {
14348 eval {
14349 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
14350 $client->login($user, $passwd);
14351
14352 my $resp_msgs = $client->response_msgs();
14353 my $nmsgs = scalar(@$resp_msgs);
14354
14355 my $expected;
14356
14357 $expected = 1;
14358 $self->assert($expected == $nmsgs,
14359 test_msg("Expected $expected, got $nmsgs"));
14360
14361 $expected = "User proftpd logged in";
14362 $self->assert($expected eq $resp_msgs->[0],
14363 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
14364 };
14365
14366 if ($@) {
14367 $ex = $@;
14368 }
14369
14370 $wfh->print("done\n");
14371 $wfh->flush();
14372
14373 } else {
14374 eval { server_wait($config_file, $rfh) };
14375 if ($@) {
14376 warn($@);
14377 exit 1;
14378 }
14379
14380 exit 0;
14381 }
14382
14383 # Stop server
14384 server_stop($pid_file);
14385
14386 $self->assert_child_ok($pid);
14387
14388 if ($ex) {
14389 test_append_logfile($log_file, $ex);
14390 unlink($log_file);
14391
14392 die($ex);
14393 }
14394
14395 unlink($log_file);
14396 }
14397
14398 sub sql_user_info_null_uid_gid_columns {
14399 my $self = shift;
14400 my $tmpdir = $self->{tmpdir};
14401
14402 my $config_file = "$tmpdir/sqlite.conf";
14403 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
14404 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
14405
14406 my $log_file = test_get_logfile();
14407
14408 my $user = 'proftpd';
14409 my $passwd = 'test';
14410 my $group = 'ftpd';
14411 my $home_dir = File::Spec->rel2abs($tmpdir);
14412 my $uid = 500;
14413 my $gid = 500;
14414
14415 my $default_home = '/tmp';
14416 my $default_uid = 14;
14417 my $default_gid = 28;
14418
14419 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
14420
14421 # Build up sqlite3 command to create users, groups tables and populate them
14422 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
14423
14424 if (open(my $fh, "> $db_script")) {
14425 print $fh <<EOS;
14426 CREATE TABLE users (
14427 userid TEXT,
14428 passwd TEXT
14429 );
14430 INSERT INTO users (userid, passwd) VALUES ('$user', '$passwd');
14431
14432 CREATE TABLE groups (
14433 groupname TEXT,
14434 gid INTEGER,
14435 members TEXT
14436 );
14437 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $default_gid, '$user');
14438
14439 EOS
14440
14441 unless (close($fh)) {
14442 die("Can't write $db_script: $!");
14443 }
14444
14445 } else {
14446 die("Can't open $db_script: $!");
14447 }
14448
14449 my $cmd = "sqlite3 $db_file < $db_script";
14450 build_db($cmd, $db_script);
14451
14452 # Make sure that, if we're running as root, the database file has
14453 # the permissions/privs set for use by proftpd
14454 if ($< == 0) {
14455 unless (chmod(0666, $db_file)) {
14456 die("Can't set perms on $db_file to 0666: $!");
14457 }
14458 }
14459
14460 my $config = {
14461 PidFile => $pid_file,
14462 ScoreboardFile => $scoreboard_file,
14463 SystemLog => $log_file,
14464
14465 IfModules => {
14466 'mod_delay.c' => {
14467 DelayEngine => 'off',
14468 },
14469
14470 'mod_sql.c' => {
14471 SQLAuthenticate => 'users groups',
14472 SQLAuthTypes => 'plaintext',
14473 SQLBackend => 'sqlite3',
14474 SQLConnectInfo => $db_file,
14475 SQLLogFile => $log_file,
14476 SQLUserInfo => 'users userid passwd Null Null Null Null',
14477 SQLDefaultUID => $default_uid,
14478 SQLDefaultGID => $default_gid,
14479 SQLDefaultHomedir => $default_home,
14480 },
14481 },
14482 };
14483
14484 my ($port, $config_user, $config_group) = config_write($config_file, $config);
14485
14486 # Open pipes, for use between the parent and child processes. Specifically,
14487 # the child will indicate when it's done with its test by writing a message
14488 # to the parent.
14489 my ($rfh, $wfh);
14490 unless (pipe($rfh, $wfh)) {
14491 die("Can't open pipe: $!");
14492 }
14493
14494 my $ex;
14495
14496 # Fork child
14497 $self->handle_sigchld();
14498 defined(my $pid = fork()) or die("Can't fork: $!");
14499 if ($pid) {
14500 eval {
14501 sleep(2);
14502
14503 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
14504 $client->login($user, $passwd);
14505
14506 my $resp_msgs = $client->response_msgs();
14507 my $nmsgs = scalar(@$resp_msgs);
14508
14509 my $expected;
14510
14511 $expected = 1;
14512 $self->assert($expected == $nmsgs,
14513 test_msg("Expected $expected, got $nmsgs"));
14514
14515 $expected = "User proftpd logged in";
14516 $self->assert($expected eq $resp_msgs->[0],
14517 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
14518 };
14519
14520 if ($@) {
14521 $ex = $@;
14522 }
14523
14524 $wfh->print("done\n");
14525 $wfh->flush();
14526
14527 } else {
14528 eval { server_wait($config_file, $rfh) };
14529 if ($@) {
14530 warn($@);
14531 exit 1;
14532 }
14533
14534 exit 0;
14535 }
14536
14537 # Stop server
14538 server_stop($pid_file);
14539
14540 $self->assert_child_ok($pid);
14541
14542 if ($ex) {
14543 test_append_logfile($log_file, $ex);
14544 unlink($log_file);
14545
14546 die($ex);
14547 }
14548
14549 unlink($log_file);
14550 }
14551
14552 sub sql_64_bit_uid_bug4164 {
14553 my $self = shift;
14554 my $tmpdir = $self->{tmpdir};
14555
14556 my $config_file = "$tmpdir/sqlite.conf";
14557 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
14558 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
14559
14560 my $log_file = test_get_logfile();
14561
14562 my $user = 'proftpd';
14563 my $passwd = 'test';
14564 my $group = 'ftpd';
14565 my $home_dir = File::Spec->rel2abs($tmpdir);
14566
14567 # See: https://github.com/proftpd/proftpd/issues/74
14568 my $uid = 25440237859;
14569 my $gid = 500;
14570
14571 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
14572
14573 # Build up sqlite3 command to create users, groups tables and populate them
14574 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
14575
14576 if (open(my $fh, "> $db_script")) {
14577 print $fh <<EOS;
14578 CREATE TABLE users (
14579 userid TEXT,
14580 passwd TEXT,
14581 uid INTEGER,
14582 gid INTEGER,
14583 homedir TEXT,
14584 shell TEXT
14585 );
14586 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
14587
14588 CREATE TABLE groups (
14589 groupname TEXT,
14590 gid INTEGER,
14591 members TEXT
14592 );
14593 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
14594 EOS
14595
14596 unless (close($fh)) {
14597 die("Can't write $db_script: $!");
14598 }
14599
14600 } else {
14601 die("Can't open $db_script: $!");
14602 }
14603
14604 my $cmd = "sqlite3 $db_file < $db_script";
14605 build_db($cmd, $db_script);
14606
14607 # Make sure that, if we're running as root, the database file has
14608 # the permissions/privs set for use by proftpd
14609 if ($< == 0) {
14610 unless (chmod(0666, $db_file)) {
14611 die("Can't set perms on $db_file to 0666: $!");
14612 }
14613 }
14614
14615 my $config = {
14616 PidFile => $pid_file,
14617 ScoreboardFile => $scoreboard_file,
14618 SystemLog => $log_file,
14619
14620 IfModules => {
14621 'mod_delay.c' => {
14622 DelayEngine => 'off',
14623 },
14624
14625 'mod_sql.c' => {
14626 SQLAuthTypes => 'plaintext',
14627 SQLBackend => 'sqlite3',
14628 SQLConnectInfo => $db_file,
14629 SQLLogFile => $log_file,
14630 SQLMinID => 100,
14631 },
14632 },
14633 };
14634
14635 my ($port, $config_user, $config_group) = config_write($config_file, $config);
14636
14637 # Open pipes, for use between the parent and child processes. Specifically,
14638 # the child will indicate when it's done with its test by writing a message
14639 # to the parent.
14640 my ($rfh, $wfh);
14641 unless (pipe($rfh, $wfh)) {
14642 die("Can't open pipe: $!");
14643 }
14644
14645 my $ex;
14646
14647 # Fork child
14648 $self->handle_sigchld();
14649 defined(my $pid = fork()) or die("Can't fork: $!");
14650 if ($pid) {
14651 eval {
14652 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
14653 $client->login($user, $passwd);
14654
14655 my $resp_msgs = $client->response_msgs();
14656 my $nmsgs = scalar(@$resp_msgs);
14657
14658 my $expected;
14659
14660 $expected = 1;
14661 $self->assert($expected == $nmsgs,
14662 test_msg("Expected $expected, got $nmsgs"));
14663
14664 $expected = "User proftpd logged in";
14665 $self->assert($expected eq $resp_msgs->[0],
14666 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
14667
14668 $client->quit();
14669 };
14670
14671 if ($@) {
14672 $ex = $@;
14673 }
14674
14675 $wfh->print("done\n");
14676 $wfh->flush();
14677
14678 } else {
14679 eval { server_wait($config_file, $rfh) };
14680 if ($@) {
14681 warn($@);
14682 exit 1;
14683 }
14684
14685 exit 0;
14686 }
14687
14688 # Stop server
14689 server_stop($pid_file);
14690
14691 $self->assert_child_ok($pid);
14692
14693 if ($ex) {
14694 test_append_logfile($log_file, $ex);
14695 unlink($log_file);
14696
14697 die($ex);
14698 }
14699
14700 unlink($log_file);
14701 }
14702
14703 sub get_port {
14704 my $db_file = shift;
14705 my $where = shift;
14706
14707 my $sql = "SELECT user, ip_addr, port FROM ftpsessions";
14708 if ($where) {
14709 $sql .= " WHERE $where";
14710 }
14711
14712 my $cmd = "sqlite3 $db_file \"$sql\"";
14713
14714 if ($ENV{TEST_VERBOSE}) {
14715 print STDERR "Executing sqlite3: $cmd\n";
14716 }
14717
14718 my $res = join('', `$cmd`);
14719 chomp($res);
14720
14721 # The default sqlite3 delimiter is '|'
14722 return split(/\|/, $res);
14723 }
14724
14725 sub sql_sqllog_var_remote_port {
14726 my $self = shift;
14727 my $tmpdir = $self->{tmpdir};
14728
14729 my $config_file = "$tmpdir/sqlite.conf";
14730 my $pid_file = File::Spec->rel2abs("$tmpdir/sqlite.pid");
14731 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sqlite.scoreboard");
14732
14733 my $log_file = test_get_logfile();
14734
14735 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sqlite.passwd");
14736 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sqlite.group");
14737
14738 my $user = 'proftpd';
14739 my $passwd = 'test';
14740 my $group = 'ftpd';
14741 my $home_dir = File::Spec->rel2abs($tmpdir);
14742 my $uid = 500;
14743 my $gid = 500;
14744
14745 # Make sure that, if we're running as root, that the home directory has
14746 # permissions/privs set for the account we create
14747 if ($< == 0) {
14748 unless (chmod(0755, $home_dir)) {
14749 die("Can't set perms on $home_dir to 0755: $!");
14750 }
14751
14752 unless (chown($uid, $gid, $home_dir)) {
14753 die("Can't set owner of $home_dir to $uid/$gid: $!");
14754 }
14755 }
14756
14757 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
14758 '/bin/bash');
14759 auth_group_write($auth_group_file, $group, $gid, $user);
14760
14761 my $test_file = File::Spec->rel2abs($config_file);
14762
14763 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
14764
14765 # Build up sqlite3 command to create users, groups tables and populate them
14766 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
14767
14768 if (open(my $fh, "> $db_script")) {
14769 print $fh <<EOS;
14770 CREATE TABLE ftpsessions (
14771 user TEXT,
14772 ip_addr TEXT,
14773 port NUMBER
14774 );
14775 EOS
14776
14777 unless (close($fh)) {
14778 die("Can't write $db_script: $!");
14779 }
14780
14781 } else {
14782 die("Can't open $db_script: $!");
14783 }
14784
14785 my $cmd = "sqlite3 $db_file < $db_script";
14786 build_db($cmd, $db_script);
14787
14788 # Make sure that, if we're running as root, the database file has
14789 # the permissions/privs set for use by proftpd
14790 if ($< == 0) {
14791 unless (chmod(0666, $db_file)) {
14792 die("Can't set perms on $db_file to 0666: $!");
14793 }
14794 }
14795
14796 my $config = {
14797 PidFile => $pid_file,
14798 ScoreboardFile => $scoreboard_file,
14799 SystemLog => $log_file,
14800
14801 AuthUserFile => $auth_user_file,
14802 AuthGroupFile => $auth_group_file,
14803 AuthOrder => 'mod_auth_file.c',
14804
14805 IfModules => {
14806 'mod_delay.c' => {
14807 DelayEngine => 'off',
14808 },
14809
14810 'mod_sql.c' => {
14811 SQLEngine => 'log',
14812 SQLBackend => 'sqlite3',
14813 SQLConnectInfo => $db_file,
14814 SQLLogFile => $log_file,
14815 SQLNamedQuery => 'filename FREEFORM "INSERT INTO ftpsessions (user, ip_addr, port) VALUES (\'%u\', \'%L\', %{remote-port})"',
14816 SQLLog => 'RETR filename',
14817 },
14818 },
14819 };
14820
14821 my ($port, $config_user, $config_group) = config_write($config_file, $config);
14822
14823 # Open pipes, for use between the parent and child processes. Specifically,
14824 # the child will indicate when it's done with its test by writing a message
14825 # to the parent.
14826 my ($rfh, $wfh);
14827 unless (pipe($rfh, $wfh)) {
14828 die("Can't open pipe: $!");
14829 }
14830
14831 my $ex;
14832
14833 # Fork child
14834 $self->handle_sigchld();
14835 defined(my $pid = fork()) or die("Can't fork: $!");
14836 if ($pid) {
14837 eval {
14838 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
14839 $client->login($user, $passwd);
14840
14841 my $conn = $client->retr_raw($test_file);
14842 unless ($conn) {
14843 die("Failed to RETR: " . $client->response_code() . " " .
14844 $client->response_msg());
14845 }
14846
14847 my $buf;
14848 $conn->read($buf, 8192, 30);
14849 eval { $conn->close() };
14850
14851 my $resp_code = $client->response_code();
14852 my $resp_msg = $client->response_msg();
14853 $self->assert_transfer_ok($resp_code, $resp_msg);
14854
14855 $client->quit();
14856 };
14857
14858 if ($@) {
14859 $ex = $@;
14860 }
14861
14862 $wfh->print("done\n");
14863 $wfh->flush();
14864
14865 } else {
14866 eval { server_wait($config_file, $rfh) };
14867 if ($@) {
14868 warn($@);
14869 exit 1;
14870 }
14871
14872 exit 0;
14873 }
14874
14875 # Stop server
14876 server_stop($pid_file);
14877 $self->assert_child_ok($pid);
14878
14879 if ($ex) {
14880 test_append_logfile($log_file, $ex);
14881 unlink($log_file);
14882
14883 die($ex);
14884 }
14885
14886 my ($login, $ip_addr);
14887 ($login, $ip_addr, $port) = get_port($db_file, "user = \'$user\'");
14888
14889 my $expected = $user;
14890 $self->assert($expected eq $login,
14891 test_msg("Expected '$expected', got '$login'"));
14892
14893 $expected = '127.0.0.1';
14894 $self->assert($expected eq $ip_addr,
14895 test_msg("Expected '$expected', got '$ip_addr'"));
14896
14897 $expected = '^\d+$';
14898 $self->assert(qr/$expected/, $port,
14899 test_msg("Expected '$expected', got '$port'"));
14900
14901 unlink($log_file);
14902 }
14903
14904 sub get_ftps {
14905 my $db_file = shift;
14906 my $where = shift;
14907
14908 my $sql = "SELECT user, ip_addr, ftps FROM ftpsessions";
14909 if ($where) {
14910 $sql .= " WHERE $where";
14911 }
14912
14913 my $cmd = "sqlite3 $db_file \"$sql\"";
14914
14915 if ($ENV{TEST_VERBOSE}) {
14916 print STDERR "Executing sqlite3: $cmd\n";
14917 }
14918
14919 my $res = join('', `$cmd`);
14920 chomp($res);
14921
14922 # The default sqlite3 delimiter is '|'
14923 return split(/\|/, $res);
14924 }
14925
14926 sub sql_sqllog_var_note_ftps_without_tls_issue1232 {
14927 my $self = shift;
14928 my $tmpdir = $self->{tmpdir};
14929 my $setup = test_setup($tmpdir, 'sqlite');
14930
14931 my $test_file = File::Spec->rel2abs($setup->{config_file});
14932 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
14933
14934 # Build up sqlite3 command to create users, groups tables and populate them
14935 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
14936
14937 if (open(my $fh, "> $db_script")) {
14938 print $fh <<EOS;
14939 CREATE TABLE ftpsessions (
14940 user TEXT,
14941 ip_addr TEXT,
14942 ftps TEXT
14943 );
14944 EOS
14945
14946 unless (close($fh)) {
14947 die("Can't write $db_script: $!");
14948 }
14949
14950 } else {
14951 die("Can't open $db_script: $!");
14952 }
14953
14954 my $cmd = "sqlite3 $db_file < $db_script";
14955 build_db($cmd, $db_script);
14956
14957 # Make sure that, if we're running as root, the database file has
14958 # the permissions/privs set for use by proftpd
14959 if ($< == 0) {
14960 unless (chmod(0666, $db_file)) {
14961 die("Can't set perms on $db_file to 0666: $!");
14962 }
14963 }
14964
14965 my $config = {
14966 PidFile => $setup->{pid_file},
14967 ScoreboardFile => $setup->{scoreboard_file},
14968 SystemLog => $setup->{log_file},
14969 TraceLog => $setup->{log_file},
14970
14971 AuthUserFile => $setup->{auth_user_file},
14972 AuthGroupFile => $setup->{auth_group_file},
14973 AuthOrder => 'mod_auth_file.c',
14974
14975 IfModules => {
14976 'mod_delay.c' => {
14977 DelayEngine => 'off',
14978 },
14979
14980 'mod_sql.c' => {
14981 SQLEngine => 'log',
14982 SQLBackend => 'sqlite3',
14983 SQLConnectInfo => $db_file,
14984 SQLLogFile => $setup->{log_file},
14985 SQLNamedQuery => 'on_login FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ftps) VALUES (\'%u\', \'%L\', \'%{note:FTPS}\')"',
14986 SQLLog => 'PASS on_login',
14987 },
14988 },
14989 };
14990
14991 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
14992 $config);
14993
14994 # Open pipes, for use between the parent and child processes. Specifically,
14995 # the child will indicate when it's done with its test by writing a message
14996 # to the parent.
14997 my ($rfh, $wfh);
14998 unless (pipe($rfh, $wfh)) {
14999 die("Can't open pipe: $!");
15000 }
15001
15002 my $ex;
15003
15004 # Fork child
15005 $self->handle_sigchld();
15006 defined(my $pid = fork()) or die("Can't fork: $!");
15007 if ($pid) {
15008 eval {
15009 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
15010 $client->login($setup->{user}, $setup->{passwd});
15011
15012 my $conn = $client->retr_raw($test_file);
15013 unless ($conn) {
15014 die("Failed to RETR: " . $client->response_code() . " " .
15015 $client->response_msg());
15016 }
15017
15018 my $buf;
15019 $conn->read($buf, 8192, 30);
15020 eval { $conn->close() };
15021
15022 my $resp_code = $client->response_code();
15023 my $resp_msg = $client->response_msg();
15024 $self->assert_transfer_ok($resp_code, $resp_msg);
15025
15026 $client->quit();
15027 };
15028 if ($@) {
15029 $ex = $@;
15030 }
15031
15032 $wfh->print("done\n");
15033 $wfh->flush();
15034
15035 } else {
15036 eval { server_wait($setup->{config_file}, $rfh) };
15037 if ($@) {
15038 warn($@);
15039 exit 1;
15040 }
15041
15042 exit 0;
15043 }
15044
15045 # Stop server
15046 server_stop($setup->{pid_file});
15047 $self->assert_child_ok($pid);
15048
15049 test_cleanup($setup->{log_file}, $ex) if $ex;
15050
15051 eval {
15052 my ($login, $ip_addr, $ftps);
15053 ($login, $ip_addr, $ftps) = get_ftps($db_file, "user = \'$setup->{user}\'");
15054
15055 my $expected = $setup->{user};
15056 $self->assert($expected eq $login,
15057 test_msg("Expected '$expected', got '$login'"));
15058
15059 $expected = '127.0.0.1';
15060 $self->assert($expected eq $ip_addr,
15061 test_msg("Expected '$expected', got '$ip_addr'"));
15062
15063 $expected = '';
15064 $self->assert(qr/$expected/, $ftps,
15065 test_msg("Expected '$expected', got '$ftps'"));
15066 };
15067 if ($@) {
15068 $ex = $@;
15069 }
15070
15071 test_cleanup($setup->{log_file}, $ex);
15072 }
15073
15074 sub sql_sqllog_var_note_ftps_with_tls_issue1232 {
15075 my $self = shift;
15076 my $tmpdir = $self->{tmpdir};
15077 my $setup = test_setup($tmpdir, 'sqlite');
15078
15079 my $test_file = File::Spec->rel2abs($setup->{config_file});
15080 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
15081
15082 # Build up sqlite3 command to create users, groups tables and populate them
15083 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
15084
15085 if (open(my $fh, "> $db_script")) {
15086 print $fh <<EOS;
15087 CREATE TABLE ftpsessions (
15088 user TEXT,
15089 ip_addr TEXT,
15090 ftps TEXT
15091 );
15092 EOS
15093
15094 unless (close($fh)) {
15095 die("Can't write $db_script: $!");
15096 }
15097
15098 } else {
15099 die("Can't open $db_script: $!");
15100 }
15101
15102 my $cmd = "sqlite3 $db_file < $db_script";
15103 build_db($cmd, $db_script);
15104
15105 # Make sure that, if we're running as root, the database file has
15106 # the permissions/privs set for use by proftpd
15107 if ($< == 0) {
15108 unless (chmod(0666, $db_file)) {
15109 die("Can't set perms on $db_file to 0666: $!");
15110 }
15111 }
15112
15113 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
15114 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
15115
15116 my $config = {
15117 PidFile => $setup->{pid_file},
15118 ScoreboardFile => $setup->{scoreboard_file},
15119 SystemLog => $setup->{log_file},
15120 TraceLog => $setup->{log_file},
15121 Trace => 'jot:30 sql:20 tls:20',
15122
15123 AuthUserFile => $setup->{auth_user_file},
15124 AuthGroupFile => $setup->{auth_group_file},
15125 AuthOrder => 'mod_auth_file.c',
15126
15127 IfModules => {
15128 'mod_delay.c' => {
15129 DelayEngine => 'off',
15130 },
15131
15132 'mod_sql.c' => {
15133 SQLEngine => 'log',
15134 SQLBackend => 'sqlite3',
15135 SQLConnectInfo => $db_file,
15136 SQLLogFile => $setup->{log_file},
15137 SQLNamedQuery => 'on_login FREEFORM "INSERT INTO ftpsessions (user, ip_addr, ftps) VALUES (\'%u\', \'%L\', \'%{note:FTPS}\')"',
15138 SQLLog => 'PASS on_login',
15139 },
15140
15141 'mod_tls.c' => {
15142 TLSEngine => 'on',
15143 TLSLog => $setup->{log_file},
15144 TLSRequired => 'on',
15145 TLSRSACertificateFile => $cert_file,
15146 TLSCACertificateFile => $ca_file,
15147 },
15148 },
15149 };
15150
15151 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
15152 $config);
15153
15154 # Open pipes, for use between the parent and child processes. Specifically,
15155 # the child will indicate when it's done with its test by writing a message
15156 # to the parent.
15157 my ($rfh, $wfh);
15158 unless (pipe($rfh, $wfh)) {
15159 die("Can't open pipe: $!");
15160 }
15161
15162 require Net::FTPSSL;
15163
15164 my $ex;
15165
15166 # Fork child
15167 $self->handle_sigchld();
15168 defined(my $pid = fork()) or die("Can't fork: $!");
15169 if ($pid) {
15170 eval {
15171 # Give the server a chance to start up
15172 sleep(2);
15173
15174 my $client = Net::FTPSSL->new('127.0.0.1',
15175 Encryption => 'E',
15176 Port => $port,
15177 );
15178
15179 unless ($client) {
15180 die("Can't connect to FTPS server: " . IO::Socket::SSL::errstr());
15181 }
15182
15183 unless ($client->login($setup->{user}, $setup->{passwd})) {
15184 die("Login failed unexpectedly: " + $client->message());
15185 }
15186
15187 $client->quit();
15188 };
15189 if ($@) {
15190 $ex = $@;
15191 }
15192
15193 $wfh->print("done\n");
15194 $wfh->flush();
15195
15196 } else {
15197 eval { server_wait($setup->{config_file}, $rfh) };
15198 if ($@) {
15199 warn($@);
15200 exit 1;
15201 }
15202
15203 exit 0;
15204 }
15205
15206 # Stop server
15207 server_stop($setup->{pid_file});
15208 $self->assert_child_ok($pid);
15209
15210 test_cleanup($setup->{log_file}, $ex) if $ex;
15211
15212 eval {
15213 my ($login, $ip_addr, $ftps);
15214 ($login, $ip_addr, $ftps) = get_ftps($db_file, "user = \'$setup->{user}\'");
15215
15216 my $expected = $setup->{user};
15217 $self->assert($expected eq $login,
15218 test_msg("Expected '$expected', got '$login'"));
15219
15220 $expected = '127.0.0.1';
15221 $self->assert($expected eq $ip_addr,
15222 test_msg("Expected '$expected', got '$ip_addr'"));
15223
15224 $expected = '1';
15225 $self->assert(qr/$expected/, $ftps,
15226 test_msg("Expected '$expected', got '$ftps'"));
15227 };
15228 if ($@) {
15229 $ex = $@;
15230 }
15231
15232 test_cleanup($setup->{log_file}, $ex);
15233 }
15234
15235 sub sql_userowner_issue346 {
15236 my $self = shift;
15237 my $tmpdir = $self->{tmpdir};
15238 my $setup = test_setup($tmpdir, 'sqlite');
15239
15240 my $uid = 0;
15241 my $gid = 0;
15242
15243 my $test_file = File::Spec->rel2abs("$tmpdir/test.dat");
15244 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
15245
15246 # Build up sqlite3 command to create users, groups tables and populate them
15247 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
15248
15249 if (open(my $fh, "> $db_script")) {
15250 print $fh <<EOS;
15251 CREATE TABLE users (
15252 userid TEXT,
15253 passwd TEXT,
15254 uid INTEGER,
15255 gid INTEGER,
15256 homedir TEXT,
15257 shell TEXT,
15258 lastdir TEXT
15259 );
15260 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $uid, $gid, '$setup->{home_dir}', '/bin/bash');
15261
15262 CREATE TABLE groups (
15263 groupname TEXT,
15264 gid INTEGER,
15265 members TEXT
15266 );
15267 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $gid, '$setup->{user}');
15268 EOS
15269
15270 unless (close($fh)) {
15271 die("Can't write $db_script: $!");
15272 }
15273
15274 } else {
15275 die("Can't open $db_script: $!");
15276 }
15277
15278 my $cmd = "sqlite3 $db_file < $db_script";
15279 build_db($cmd, $db_script);
15280
15281 # Make sure that, if we're running as root, the database file has
15282 # the permissions/privs set for use by proftpd
15283 if ($< == 0) {
15284 unless (chmod(0666, $db_file)) {
15285 die("Can't set perms on $db_file to 0666: $!");
15286 }
15287 }
15288
15289 my $config = {
15290 PidFile => $setup->{pid_file},
15291 ScoreboardFile => $setup->{scoreboard_file},
15292 SystemLog => $setup->{log_file},
15293
15294 RootLogin => 'on',
15295 RootRevoke => 'off',
15296
15297 Directory => {
15298 '/' => {
15299 UserOwner => 'www',
15300 GroupOwner => 'www',
15301 },
15302 },
15303
15304 IfModules => {
15305 'mod_delay.c' => {
15306 DelayEngine => 'off',
15307 },
15308
15309 'mod_sql.c' => {
15310 SQLAuthTypes => 'plaintext',
15311 SQLBackend => 'sqlite3',
15312 SQLConnectInfo => $db_file,
15313 SQLLogFile => $setup->{log_file},
15314 SQLMinUserUID => 0,
15315 SQLMinUserGID => 0,
15316 SQLMinID => 0,
15317 },
15318 },
15319 };
15320
15321 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
15322 $config);
15323
15324 # Open pipes, for use between the parent and child processes. Specifically,
15325 # the child will indicate when it's done with its test by writing a message
15326 # to the parent.
15327 my ($rfh, $wfh);
15328 unless (pipe($rfh, $wfh)) {
15329 die("Can't open pipe: $!");
15330 }
15331
15332 my $ex;
15333
15334 # Fork child
15335 $self->handle_sigchld();
15336 defined(my $pid = fork()) or die("Can't fork: $!");
15337 if ($pid) {
15338 eval {
15339 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0);
15340 $client->login($setup->{user}, $setup->{passwd});
15341
15342 my $conn = $client->stor_raw('test.dat');
15343 unless ($conn) {
15344 die("STOR test.dat failed: " . $client->response_code() . " " .
15345 $client->response_msg());
15346 }
15347
15348 my $text = "Hello, World!\n";
15349 $conn->write($text, length($text), 10);
15350 eval { $conn->close() };
15351
15352 my $resp_code = $client->response_code();
15353 my $resp_msg = $client->response_msg();
15354 $self->assert_transfer_ok($resp_code, $resp_msg);
15355 $client->quit();
15356
15357 $self->assert(-f $test_file,
15358 "File $test_file does not exist as expected");
15359
15360 my $file_uid = (stat($test_file))[4];
15361 my $file_gid = (stat($test_file))[5];
15362
15363 $self->assert($file_uid != 0, "Expected UID non-0, got $file_uid");
15364 $self->assert($file_gid != 0, "Expected GID non-0, got $file_gid");
15365 };
15366
15367 if ($@) {
15368 $ex = $@;
15369 }
15370
15371 $wfh->print("done\n");
15372 $wfh->flush();
15373
15374 } else {
15375 eval { server_wait($setup->{config_file}, $rfh) };
15376 if ($@) {
15377 warn($@);
15378 exit 1;
15379 }
15380
15381 exit 0;
15382 }
15383
15384 # Stop server
15385 server_stop($setup->{pid_file});
15386 $self->assert_child_ok($pid);
15387
15388 test_cleanup($setup->{log_file}, $ex);
15389 }
15390
15391 sub sql_sqlite_log_db_enoent_issue654 {
15392 my $self = shift;
15393 my $tmpdir = $self->{tmpdir};
15394 my $setup = test_setup($tmpdir, 'sqlite');
15395
15396 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
15397
15398 my $config = {
15399 PidFile => $setup->{pid_file},
15400 ScoreboardFile => $setup->{scoreboard_file},
15401 SystemLog => $setup->{log_file},
15402 TraceLog => $setup->{log_file},
15403 Trace => 'sql:20',
15404
15405 AuthUserFile => $setup->{auth_user_file},
15406 AuthGroupFile => $setup->{auth_group_file},
15407 AuthOrder => 'mod_auth_file.c',
15408
15409 IfModules => {
15410 'mod_delay.c' => {
15411 DelayEngine => 'off',
15412 },
15413
15414 'mod_sql.c' => {
15415 SQLEngine => 'log',
15416 SQLBackend => 'sqlite3',
15417 SQLConnectInfo => $db_file,
15418 SQLLogFile => $setup->{log_file},
15419 SQLNamedQuery => 'session_start FREEFORM "INSERT INTO ftpsessions (user, ip_addr, timestamp) VALUES (\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\')"',
15420 SQLLog => 'PASS session_start',
15421 },
15422 },
15423 };
15424
15425 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
15426 $config);
15427
15428 # Open pipes, for use between the parent and child processes. Specifically,
15429 # the child will indicate when it's done with its test by writing a message
15430 # to the parent.
15431 my ($rfh, $wfh);
15432 unless (pipe($rfh, $wfh)) {
15433 die("Can't open pipe: $!");
15434 }
15435
15436 my $ex;
15437
15438 # Fork child
15439 $self->handle_sigchld();
15440 defined(my $pid = fork()) or die("Can't fork: $!");
15441 if ($pid) {
15442 eval {
15443 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
15444 eval { $client->login($setup->{user}, $setup->{passwd}) };
15445 unless ($@) {
15446 $client->quit();
15447 die("Login succeeded unexpectedly");
15448 }
15449 };
15450 if ($@) {
15451 $ex = $@;
15452 }
15453
15454 $wfh->print("done\n");
15455 $wfh->flush();
15456
15457 } else {
15458 eval { server_wait($setup->{config_file}, $rfh) };
15459 if ($@) {
15460 warn($@);
15461 exit 1;
15462 }
15463
15464 exit 0;
15465 }
15466
15467 # Stop server
15468 server_stop($setup->{pid_file});
15469 $self->assert_child_ok($pid);
15470
15471 test_cleanup($setup->{log_file}, $ex);
15472 }
15473
15474 sub sql_namebased_vhost_using_host_issue882 {
15475 my $self = shift;
15476 my $tmpdir = $self->{tmpdir};
15477 my $setup = test_setup($tmpdir, 'sqlite');
15478
15479 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
15480
15481 # Build up sqlite3 command to create users, groups tables and populate them
15482 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
15483
15484 if (open(my $fh, "> $db_script")) {
15485 print $fh <<EOS;
15486 CREATE TABLE users (
15487 userid TEXT,
15488 passwd TEXT,
15489 uid INTEGER,
15490 gid INTEGER,
15491 homedir TEXT,
15492 shell TEXT,
15493 lastdir TEXT
15494 );
15495 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash');
15496 CREATE TABLE groups (
15497 groupname TEXT,
15498 gid INTEGER,
15499 members TEXT
15500 );
15501 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
15502 EOS
15503
15504 unless (close($fh)) {
15505 die("Can't write $db_script: $!");
15506 }
15507
15508 } else {
15509 die("Can't open $db_script: $!");
15510 }
15511
15512 my $cmd = "sqlite3 $db_file < $db_script";
15513 build_db($cmd, $db_script);
15514
15515 # Make sure that, if we're running as root, the database file has
15516 # the permissions/privs set for use by proftpd
15517 if ($< == 0) {
15518 unless (chmod(0666, $db_file)) {
15519 die("Can't set perms on $db_file to 0666: $!");
15520 }
15521 }
15522
15523 my $config = {
15524 PidFile => $setup->{pid_file},
15525 ScoreboardFile => $setup->{scoreboard_file},
15526 SystemLog => $setup->{log_file},
15527 TraceLog => $setup->{log_file},
15528 Trace => 'sql:20',
15529
15530 AuthUserFile => $setup->{auth_user_file},
15531 AuthGroupFile => $setup->{auth_group_file},
15532 AuthOrder => 'mod_auth_file.c',
15533
15534 Global => {
15535 AuthOrder => 'mod_sql.c',
15536 SQLAuthTypes => 'plaintext',
15537 SQLEngine => 'on',
15538 SQLBackend => 'sqlite3',
15539 SQLConnectInfo => $db_file,
15540 SQLLogFile => $setup->{log_file},
15541 },
15542
15543 IfModules => {
15544 'mod_delay.c' => {
15545 DelayEngine => 'off',
15546 },
15547 },
15548 };
15549
15550 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
15551 $config);
15552
15553 my $server_alias = 'ftp.example.com';
15554
15555 if (open(my $fh, ">> $setup->{config_file}")) {
15556 print $fh <<EOC;
15557 <VirtualHost 127.0.0.1>
15558 Port $port
15559 ServerAlias $server_alias
15560 WtmpLog off
15561 TransferLog none
15562 </VirtualHost>
15563 EOC
15564 } else {
15565 die("Can't open $setup->{config_file}: $!");
15566 }
15567
15568 # Open pipes, for use between the parent and child processes. Specifically,
15569 # the child will indicate when it's done with its test by writing a message
15570 # to the parent.
15571 my ($rfh, $wfh);
15572 unless (pipe($rfh, $wfh)) {
15573 die("Can't open pipe: $!");
15574 }
15575
15576 my $ex;
15577
15578 # Fork child
15579 $self->handle_sigchld();
15580 defined(my $pid = fork()) or die("Can't fork: $!");
15581 if ($pid) {
15582 eval {
15583 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
15584 $client->host($server_alias);
15585 $client->login($setup->{user}, $setup->{passwd});
15586 $client->quit();
15587 };
15588 if ($@) {
15589 $ex = $@;
15590 }
15591
15592 $wfh->print("done\n");
15593 $wfh->flush();
15594
15595 } else {
15596 eval { server_wait($setup->{config_file}, $rfh) };
15597 if ($@) {
15598 warn($@);
15599 exit 1;
15600 }
15601
15602 exit 0;
15603 }
15604
15605 # Stop server
15606 server_stop($setup->{pid_file});
15607 $self->assert_child_ok($pid);
15608
15609 test_cleanup($setup->{log_file}, $ex);
15610 }
15611
15612 sub sql_namebased_vhost_using_sni_issue882 {
15613 my $self = shift;
15614 my $tmpdir = $self->{tmpdir};
15615 my $setup = test_setup($tmpdir, 'sqlite');
15616
15617 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
15618
15619 # Build up sqlite3 command to create users, groups tables and populate them
15620 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
15621
15622 if (open(my $fh, "> $db_script")) {
15623 print $fh <<EOS;
15624 CREATE TABLE users (
15625 userid TEXT,
15626 passwd TEXT,
15627 uid INTEGER,
15628 gid INTEGER,
15629 homedir TEXT,
15630 shell TEXT,
15631 lastdir TEXT
15632 );
15633 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash');
15634 CREATE TABLE groups (
15635 groupname TEXT,
15636 gid INTEGER,
15637 members TEXT
15638 );
15639 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
15640 EOS
15641
15642 unless (close($fh)) {
15643 die("Can't write $db_script: $!");
15644 }
15645
15646 } else {
15647 die("Can't open $db_script: $!");
15648 }
15649
15650 my $cmd = "sqlite3 $db_file < $db_script";
15651 build_db($cmd, $db_script);
15652
15653 # Make sure that, if we're running as root, the database file has
15654 # the permissions/privs set for use by proftpd
15655 if ($< == 0) {
15656 unless (chmod(0666, $db_file)) {
15657 die("Can't set perms on $db_file to 0666: $!");
15658 }
15659 }
15660
15661 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
15662 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
15663
15664 my $config = {
15665 PidFile => $setup->{pid_file},
15666 ScoreboardFile => $setup->{scoreboard_file},
15667 SystemLog => $setup->{log_file},
15668 TraceLog => $setup->{log_file},
15669 Trace => 'command:20 event:20 sql:20 tls:20',
15670
15671 AuthUserFile => $setup->{auth_user_file},
15672 AuthGroupFile => $setup->{auth_group_file},
15673 AuthOrder => 'mod_auth_file.c',
15674
15675 Global => {
15676 AuthOrder => 'mod_sql.c',
15677 SQLAuthTypes => 'plaintext',
15678 SQLEngine => 'on',
15679 SQLBackend => 'sqlite3',
15680 SQLConnectInfo => $db_file,
15681 SQLLogFile => $setup->{log_file},
15682 },
15683
15684 IfModules => {
15685 'mod_delay.c' => {
15686 DelayEngine => 'off',
15687 },
15688
15689 'mod_tls.c' => {
15690 TLSEngine => 'on',
15691 TLSLog => $setup->{log_file},
15692 TLSRequired => 'on',
15693 TLSRSACertificateFile => $cert_file,
15694 TLSCACertificateFile => $ca_file,
15695 },
15696 },
15697 };
15698
15699 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
15700 $config);
15701
15702 my $server_alias = 'ftp.example.com';
15703
15704 if (open(my $fh, ">> $setup->{config_file}")) {
15705 print $fh <<EOC;
15706 <VirtualHost 127.0.0.1>
15707 Port $port
15708 ServerAlias $server_alias
15709 WtmpLog off
15710 TransferLog none
15711 </VirtualHost>
15712 EOC
15713 } else {
15714 die("Can't open $setup->{config_file}: $!");
15715 }
15716
15717 # Open pipes, for use between the parent and child processes. Specifically,
15718 # the child will indicate when it's done with its test by writing a message
15719 # to the parent.
15720 my ($rfh, $wfh);
15721 unless (pipe($rfh, $wfh)) {
15722 die("Can't open pipe: $!");
15723 }
15724
15725 require Net::FTPSSL;
15726
15727 my $ex;
15728
15729 # Fork child
15730 $self->handle_sigchld();
15731 defined(my $pid = fork()) or die("Can't fork: $!");
15732 if ($pid) {
15733 eval {
15734 # Give the server a chance to start up
15735 sleep(2);
15736
15737 my $client = Net::FTPSSL->new('127.0.0.1',
15738 Encryption => 'E',
15739 Port => $port,
15740 SSL_hostname => $server_alias,
15741 );
15742
15743 unless ($client) {
15744 die("Can't connect to FTPS server: " . IO::Socket::SSL::errstr());
15745 }
15746
15747 unless ($client->login($setup->{user}, $setup->{passwd})) {
15748 die("Login failed unexpectedly: " + $client->message());
15749 }
15750
15751 $client->quit();
15752 };
15753 if ($@) {
15754 $ex = $@;
15755 }
15756
15757 $wfh->print("done\n");
15758 $wfh->flush();
15759
15760 } else {
15761 eval { server_wait($setup->{config_file}, $rfh) };
15762 if ($@) {
15763 warn($@);
15764 exit 1;
15765 }
15766
15767 exit 0;
15768 }
15769
15770 # Stop server
15771 server_stop($setup->{pid_file});
15772 $self->assert_child_ok($pid);
15773
15774 test_cleanup($setup->{log_file}, $ex);
15775 }
15776
15777 sub sql_user_info_no_suppl_groups_issue1830 {
15778 my $self = shift;
15779 my $tmpdir = $self->{tmpdir};
15780 my $setup = test_setup($tmpdir, 'sqlite');
15781
15782 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
15783
15784 # Build up sqlite3 command to create users, groups tables and populate them
15785 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
15786
15787 if (open(my $fh, "> $db_script")) {
15788 print $fh <<EOS;
15789 CREATE TABLE users (
15790 userid TEXT,
15791 passwd TEXT,
15792 uid INTEGER,
15793 gid INTEGER,
15794 homedir TEXT,
15795 shell TEXT
15796 );
15797 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash');
15798
15799 CREATE TABLE groups (
15800 groupname TEXT,
15801 gid INTEGER,
15802 members TEXT
15803 );
15804 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
15805 EOS
15806
15807 unless (close($fh)) {
15808 die("Can't write $db_script: $!");
15809 }
15810
15811 } else {
15812 die("Can't open $db_script: $!");
15813 }
15814
15815 my $cmd = "sqlite3 $db_file < $db_script";
15816 build_db($cmd, $db_script);
15817
15818 # Make sure that, if we're running as root, the database file has
15819 # the permissions/privs set for use by proftpd
15820 if ($< == 0) {
15821 unless (chmod(0666, $db_file)) {
15822 die("Can't set perms on $db_file to 0666: $!");
15823 }
15824 }
15825
15826 my $config = {
15827 PidFile => $setup->{pid_file},
15828 ScoreboardFile => $setup->{scoreboard_file},
15829 SystemLog => $setup->{log_file},
15830 TraceLog => $setup->{log_file},
15831 Trace => 'auth:20 sql:20',
15832
15833 # Required for logging the expected message
15834 DebugLevel => 5,
15835
15836 IfModules => {
15837 'mod_delay.c' => {
15838 DelayEngine => 'off',
15839 },
15840
15841 'mod_sql.c' => {
15842 AuthOrder => 'mod_sql.c',
15843
15844 SQLAuthenticate => 'users',
15845 SQLAuthTypes => 'plaintext',
15846 SQLBackend => 'sqlite3',
15847 SQLConnectInfo => $db_file,
15848 SQLLogFile => $setup->{log_file},
15849
15850 # Set these, so that our lower UID/GID will be used
15851 SQLMinUserUID => 100,
15852 SQLMinUserGID => 100,
15853 },
15854 },
15855 };
15856
15857 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
15858 $config);
15859
15860 # Open pipes, for use between the parent and child processes. Specifically,
15861 # the child will indicate when it's done with its test by writing a message
15862 # to the parent.
15863 my ($rfh, $wfh);
15864 unless (pipe($rfh, $wfh)) {
15865 die("Can't open pipe: $!");
15866 }
15867
15868 my $ex;
15869
15870 # Fork child
15871 $self->handle_sigchld();
15872 defined(my $pid = fork()) or die("Can't fork: $!");
15873 if ($pid) {
15874 eval {
15875 sleep(2);
15876
15877 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
15878 $client->login($setup->{user}, $setup->{passwd});
15879
15880 my $resp_msgs = $client->response_msgs();
15881 my $nmsgs = scalar(@$resp_msgs);
15882
15883 my $expected = 1;
15884 $self->assert($expected == $nmsgs,
15885 test_msg("Expected $expected, got $nmsgs"));
15886
15887 $expected = "User $setup->{user} logged in";
15888 $self->assert($expected eq $resp_msgs->[0],
15889 test_msg("Expected response '$expected', got '$resp_msgs->[0]'"));
15890
15891 $client->quit();
15892 };
15893 if ($@) {
15894 $ex = $@;
15895 }
15896
15897 $wfh->print("done\n");
15898 $wfh->flush();
15899
15900 } else {
15901 eval { server_wait($setup->{config_file}, $rfh) };
15902 if ($@) {
15903 warn($@);
15904 exit 1;
15905 }
15906
15907 exit 0;
15908 }
15909
15910 # Stop server
15911 server_stop($setup->{pid_file});
15912 $self->assert_child_ok($pid);
15913
15914 eval {
15915 if (open(my $fh, "< $setup->{log_file}")) {
15916 my $ok = 0;
15917
15918 while (my $line = <$fh>) {
15919 chomp($line);
15920
15921 if ($ENV{TEST_VERBOSE}) {
15922 print STDERR "# $line\n";
15923 }
15924
15925 if ($line =~ /no supplemental groups found for user '$setup->{user}', using primary group/) {
15926 $ok = 1;
15927 last;
15928 }
15929 }
15930
15931 close($fh);
15932
15933 $self->assert($ok, test_msg("Did not see expected log message"));
15934
15935 } else {
15936 die("Can't read $setup->{log_file}: $!");
15937 }
15938 };
15939 if ($@) {
15940 $ex = $@ unless $ex;
15941 }
15942
15943 test_cleanup($setup->{log_file}, $ex);
15944 }
15945
15946 sub sql_site_chgrp_sql_injection_issue2188 {
15947 my $self = shift;
15948 my $tmpdir = $self->{tmpdir};
15949 my $setup = test_setup($tmpdir, 'sqlite');
15950
15951 my $test_file = File::Spec->rel2abs("$tmpdir/test.dat");
15952 if (open(my $fh, "> $test_file")) {
15953 close($fh);
15954
15955 } else {
15956 die("Can't open $test_file: $!");
15957 }
15958
15959 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
15960
15961 # Build up sqlite3 command to create users, groups tables and populate them
15962 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
15963
15964 if (open(my $fh, "> $db_script")) {
15965 print $fh <<EOS;
15966 CREATE TABLE users (
15967 userid TEXT,
15968 passwd TEXT,
15969 uid INTEGER,
15970 gid INTEGER,
15971 homedir TEXT,
15972 shell TEXT
15973 );
15974 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$setup->{user}', '$setup->{passwd}', $setup->{uid}, $setup->{gid}, '$setup->{home_dir}', '/bin/bash');
15975
15976 CREATE TABLE groups (
15977 groupname TEXT,
15978 gid INTEGER,
15979 members TEXT
15980 );
15981 INSERT INTO groups (groupname, gid, members) VALUES ('$setup->{group}', $setup->{gid}, '$setup->{user}');
15982 EOS
15983
15984 unless (close($fh)) {
15985 die("Can't write $db_script: $!");
15986 }
15987
15988 } else {
15989 die("Can't open $db_script: $!");
15990 }
15991
15992 my $cmd = "sqlite3 $db_file < $db_script";
15993 build_db($cmd, $db_script);
15994
15995 # Make sure that, if we're running as root, the database file has
15996 # the permissions/privs set for use by proftpd
15997 if ($< == 0) {
15998 unless (chmod(0666, $db_file)) {
15999 die("Can't set perms on $db_file to 0666: $!");
16000 }
16001 }
16002
16003 my $config = {
16004 PidFile => $setup->{pid_file},
16005 ScoreboardFile => $setup->{scoreboard_file},
16006 SystemLog => $setup->{log_file},
16007 TraceLog => $setup->{log_file},
16008 Trace => 'sql:20',
16009
16010 # Needed to generate the expected log message for verification
16011 DebugLevel => 9,
16012
16013 IfModules => {
16014 'mod_delay.c' => {
16015 DelayEngine => 'off',
16016 },
16017
16018 'mod_sql.c' => {
16019 SQLAuthTypes => 'plaintext',
16020 SQLBackend => 'sqlite3',
16021 SQLConnectInfo => $db_file,
16022 SQLLogFile => $setup->{log_file},
16023 AuthOrder => 'mod_sql.c',
16024 },
16025 },
16026 };
16027
16028 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
16029 $config);
16030
16031 # Open pipes, for use between the parent and child processes. Specifically,
16032 # the child will indicate when it's done with its test by writing a message
16033 # to the parent.
16034 my ($rfh, $wfh);
16035 unless (pipe($rfh, $wfh)) {
16036 die("Can't open pipe: $!");
16037 }
16038
16039 my $ex;
16040
16041 # Fork child
16042 $self->handle_sigchld();
16043 defined(my $pid = fork()) or die("Can't fork: $!");
16044 if ($pid) {
16045 eval {
16046 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
16047 $client->login($setup->{user}, $setup->{passwd});
16048
16049 my $bad_group = "');INSERT||UNION%%SELECT%%999|--'";
16050 eval { $client->site('CHGRP', $bad_group, 'test.dat') };
16051 unless ($@) {
16052 die("SITE CHGRP succeeded unexpectedly");
16053 }
16054
16055 my $resp_code = $client->response_code();
16056 my $resp_msg = $client->response_msg();
16057
16058 my $expected = 550;
16059 $self->assert($expected == $resp_code,
16060 test_msg("Expected response code $expected, got $resp_code"));
16061
16062 $expected = "test.dat: Invalid argument";
16063 $self->assert($expected eq $resp_msg,
16064 test_msg("Expected response message '$expected', got '$resp_msg'"));
16065
16066 $client->quit();
16067 };
16068 if ($@) {
16069 $ex = $@;
16070 }
16071
16072 $wfh->print("done\n");
16073 $wfh->flush();
16074
16075 } else {
16076 eval { server_wait($setup->{config_file}, $rfh) };
16077 if ($@) {
16078 warn($@);
16079 exit 1;
16080 }
16081
16082 exit 0;
16083 }
16084
16085 # Stop server
16086 server_stop($setup->{pid_file});
16087 $self->assert_child_ok($pid);
16088
16089 eval {
16090 if (open(my $fh, "< $setup->{log_file}")) {
16091 my $ok = 0;
16092
16093 while (my $line = <$fh>) {
16094 chomp($line);
16095
16096 if ($ENV{TEST_VERBOSE}) {
16097 print STDERR "# $line\n";
16098 }
16099
16100 if ($line =~ /Unable to resolve group name/) {
16101 $ok = 1;
16102 last;
16103 }
16104 }
16105
16106 close($fh);
16107 $self->assert($ok, test_msg("Did not see expected log message"));
16108
16109 } else {
16110 die("Can't read $setup->{log_file}: $!");
16111 }
16112 };
16113 if ($@) {
16114 $ex = $@;
16115 }
16116
16117 test_cleanup($setup->{log_file}, $ex);
16118 }
16119
16120 1;