Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_sftp/sql.pm

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