Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_sftp::rewrite;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Digest::MD5;
8 use File::Copy;
9 use File::Path qw(mkpath rmtree);
10 use File::Spec;
11 use IO::Handle;
12 use IPC::Open3;
13 use POSIX qw(:fcntl_h);
14 use Socket;
15
16 use ProFTPD::TestSuite::FTP;
17 use ProFTPD::TestSuite::Utils qw(:auth :config :features :running :test :testsuite);
18
19 $| = 1;
20
21 my $order = 0;
22
23 my $TESTS = {
24 ssh2_rewrite_auth_password => {
25 order => ++$order,
26 test_class => [qw(forking mod_rewrite ssh2)],
27 },
28
29 ssh2_rewrite_auth_publickey => {
30 order => ++$order,
31 test_class => [qw(forking mod_rewrite ssh2)],
32 },
33
34 ssh2_rewrite_auth_homedir => {
35 order => ++$order,
36 test_class => [qw(forking mod_rewrite ssh2)],
37 },
38
39 sftp_rewrite_stat => {
40 order => ++$order,
41 test_class => [qw(forking mod_rewrite sftp ssh2)],
42 },
43
44 sftp_rewrite_lstat => {
45 order => ++$order,
46 test_class => [qw(forking mod_rewrite sftp ssh2)],
47 },
48
49 sftp_rewrite_setstat => {
50 order => ++$order,
51 test_class => [qw(forking mod_rewrite sftp ssh2)],
52 },
53
54 sftp_rewrite_realpath => {
55 order => ++$order,
56 test_class => [qw(forking mod_rewrite sftp ssh2)],
57 },
58
59 sftp_rewrite_realpath_backslashes_bug4017 => {
60 order => ++$order,
61 test_class => [qw(bug forking mod_rewrite sftp ssh2)],
62 },
63
64 sftp_rewrite_upload => {
65 order => ++$order,
66 test_class => [qw(forking mod_rewrite sftp ssh2)],
67 },
68
69 sftp_rewrite_download => {
70 order => ++$order,
71 test_class => [qw(forking mod_rewrite sftp ssh2)],
72 },
73
74 sftp_rewrite_readdir => {
75 order => ++$order,
76 test_class => [qw(forking mod_rewrite sftp ssh2)],
77 },
78
79 sftp_rewrite_mkdir => {
80 order => ++$order,
81 test_class => [qw(forking mod_rewrite sftp ssh2)],
82 },
83
84 sftp_rewrite_rmdir => {
85 order => ++$order,
86 test_class => [qw(forking mod_rewrite sftp ssh2)],
87 },
88
89 sftp_rewrite_remove => {
90 order => ++$order,
91 test_class => [qw(forking mod_rewrite sftp ssh2)],
92 },
93
94 sftp_rewrite_rename => {
95 order => ++$order,
96 test_class => [qw(forking mod_rewrite sftp ssh2)],
97 },
98
99 sftp_rewrite_rename_file_var_w_bug3643 => {
100 order => ++$order,
101 test_class => [qw(forking mod_rewrite sftp ssh2)],
102 },
103
104 sftp_rewrite_rename_dir_var_w_bug3643 => {
105 order => ++$order,
106 test_class => [qw(forking mod_rewrite sftp ssh2)],
107 },
108
109 sftp_rewrite_symlink => {
110 order => ++$order,
111 test_class => [qw(forking mod_rewrite sftp ssh2)],
112 },
113
114 sftp_rewrite_readlink => {
115 order => ++$order,
116 test_class => [qw(forking mod_rewrite sftp ssh2)],
117 },
118
119 sftp_rewrite_homedir => {
120 order => ++$order,
121 test_class => [qw(forking mod_rewrite sftp ssh2)],
122 },
123
124 scp_rewrite_upload => {
125 order => ++$order,
126 test_class => [qw(forking mod_rewrite scp ssh2)],
127 },
128
129 scp_rewrite_download => {
130 order => ++$order,
131 test_class => [qw(forking mod_rewrite scp ssh2)],
132 },
133
134 };
135
136 sub get_sftplog {
137 my $db_file = shift;
138
139 my $sql = "SELECT user, operation, filename, full_path, filesize, xfertime FROM sftplog";
140
141 my $cmd = "sqlite3 $db_file \"$sql\"";
142
143 if ($ENV{TEST_VERBOSE}) {
144 print STDERR "Executing sqlite3: $cmd\n";
145 }
146
147 my $res = join('', `$cmd`);
148
149 # The default sqlite3 delimiter is '|'
150 return split(/\|/, $res);
151 }
152
153 sub new {
154 return shift()->SUPER::new(@_);
155 }
156
157 sub list_tests {
158 # Check for the required Perl modules:
159 #
160 # Net-SSH2
161 # Net-SSH2-SFTP
162
163 my $required = [qw(
164 Net::SSH2
165 Net::SSH2::SFTP
166 )];
167
168 foreach my $req (@$required) {
169 eval "use $req";
170 if ($@) {
171 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
172
173 if ($ENV{TEST_VERBOSE}) {
174 print STDERR "Unable to load $req: $@\n";
175 }
176
177 return qw(testsuite_empty_test);
178 }
179 }
180
181 return testsuite_get_runnable_tests($TESTS);
182 }
183
184 sub set_up {
185 my $self = shift;
186 $self->SUPER::set_up(@_);
187
188 # Make sure that mod_sftp does not complain about permissions on the hostkey
189 # files.
190
191 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
192 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
193
194 unless (chmod(0400, $rsa_host_key, $dsa_host_key)) {
195 die("Can't set perms on $rsa_host_key, $dsa_host_key: $!");
196 }
197 }
198
199 sub ssh2_rewrite_auth_password {
200 my $self = shift;
201 my $tmpdir = $self->{tmpdir};
202 my $setup = test_setup($tmpdir, 'sftp');
203
204 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
205 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
206
207 my $config = {
208 PidFile => $setup->{pid_file},
209 ScoreboardFile => $setup->{scoreboard_file},
210 SystemLog => $setup->{log_file},
211 TraceLog => $setup->{log_file},
212 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
213
214 AuthUserFile => $setup->{auth_user_file},
215 AuthGroupFile => $setup->{auth_group_file},
216 AuthOrder => 'mod_auth_file.c',
217
218 IfModules => {
219 'mod_delay.c' => {
220 DelayEngine => 'off',
221 },
222
223 'mod_rewrite.c' => [
224 'RewriteEngine on',
225 "RewriteLog $setup->{log_file}",
226
227 'RewriteMap lowercase int:tolower',
228 'RewriteCondition %m USER',
229 'RewriteRule ^(.*)$ ${lowercase:$1}',
230 ],
231
232 'mod_sftp.c' => [
233 "SFTPEngine on",
234 "SFTPLog $setup->{log_file}",
235 "SFTPHostKey $rsa_host_key",
236 "SFTPHostKey $dsa_host_key",
237 ],
238 },
239 };
240
241 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
242 $config);
243
244 # Open pipes, for use between the parent and child processes. Specifically,
245 # the child will indicate when it's done with its test by writing a message
246 # to the parent.
247 my ($rfh, $wfh);
248 unless (pipe($rfh, $wfh)) {
249 die("Can't open pipe: $!");
250 }
251
252 require Net::SSH2;
253
254 my $ex;
255
256 # Fork child
257 $self->handle_sigchld();
258 defined(my $pid = fork()) or die("Can't fork: $!");
259 if ($pid) {
260 eval {
261 my $ssh2 = Net::SSH2->new();
262
263 sleep(1);
264
265 unless ($ssh2->connect('127.0.0.1', $port)) {
266 my ($err_code, $err_name, $err_str) = $ssh2->error();
267 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
268 }
269
270 unless ($ssh2->auth_password(uc($setup->{user}), $setup->{passwd})) {
271 my ($err_code, $err_name, $err_str) = $ssh2->error();
272 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
273 }
274
275 $ssh2->disconnect();
276 };
277 if ($@) {
278 $ex = $@;
279 }
280
281 $wfh->print("done\n");
282 $wfh->flush();
283
284 } else {
285 eval { server_wait($setup->{config_file}, $rfh) };
286 if ($@) {
287 warn($@);
288 exit 1;
289 }
290
291 exit 0;
292 }
293
294 # Stop server
295 server_stop($setup->{pid_file});
296 $self->assert_child_ok($pid);
297
298 test_cleanup($setup->{log_file}, $ex);
299 }
300
301 sub ssh2_rewrite_auth_publickey {
302 my $self = shift;
303 my $tmpdir = $self->{tmpdir};
304 my $setup = test_setup($tmpdir, 'sftp');
305
306 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
307 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
308
309 my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key');
310 my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub');
311 my $rsa_rfc4716_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/authorized_rsa_keys2');
312
313 my $authorized_keys = File::Spec->rel2abs("$tmpdir/.authorized_keys");
314 unless (copy($rsa_rfc4716_key, $authorized_keys)) {
315 die("Can't copy $rsa_rfc4716_key to $authorized_keys: $!");
316 }
317
318 my $config = {
319 PidFile => $setup->{pid_file},
320 ScoreboardFile => $setup->{scoreboard_file},
321 SystemLog => $setup->{log_file},
322 TraceLog => $setup->{log_file},
323 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
324
325 AuthUserFile => $setup->{auth_user_file},
326 AuthGroupFile => $setup->{auth_group_file},
327 AuthOrder => 'mod_auth_file.c',
328
329 IfModules => {
330 'mod_delay.c' => {
331 DelayEngine => 'off',
332 },
333
334 'mod_rewrite.c' => [
335 'RewriteEngine on',
336 "RewriteLog $setup->{log_file}",
337
338 'RewriteMap lowercase int:tolower',
339 'RewriteCondition %m USER',
340 'RewriteRule ^(.*)$ ${lowercase:$1}',
341 ],
342
343 'mod_sftp.c' => [
344 "SFTPEngine on",
345 "SFTPLog $setup->{log_file}",
346 "SFTPHostKey $rsa_host_key",
347 "SFTPHostKey $dsa_host_key",
348 "SFTPAuthorizedUserKeys file:~/.authorized_keys",
349 ],
350 },
351 };
352
353 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
354 $config);
355
356 # Open pipes, for use between the parent and child processes. Specifically,
357 # the child will indicate when it's done with its test by writing a message
358 # to the parent.
359 my ($rfh, $wfh);
360 unless (pipe($rfh, $wfh)) {
361 die("Can't open pipe: $!");
362 }
363
364 require Net::SSH2;
365
366 my $ex;
367
368 # Fork child
369 $self->handle_sigchld();
370 defined(my $pid = fork()) or die("Can't fork: $!");
371 if ($pid) {
372 eval {
373 my $ssh2 = Net::SSH2->new();
374
375 sleep(1);
376
377 unless ($ssh2->connect('127.0.0.1', $port)) {
378 my ($err_code, $err_name, $err_str) = $ssh2->error();
379 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
380 }
381
382 unless ($ssh2->auth_publickey(uc($setup->{user}), $rsa_pub_key,
383 $rsa_priv_key)) {
384 my ($err_code, $err_name, $err_str) = $ssh2->error();
385 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
386 }
387
388 $ssh2->disconnect();
389 };
390 if ($@) {
391 $ex = $@;
392 }
393
394 $wfh->print("done\n");
395 $wfh->flush();
396
397 } else {
398 eval { server_wait($setup->{config_file}, $rfh) };
399 if ($@) {
400 warn($@);
401 exit 1;
402 }
403
404 exit 0;
405 }
406
407 # Stop server
408 server_stop($setup->{pid_file});
409 $self->assert_child_ok($pid);
410
411 test_cleanup($setup->{log_file}, $ex);
412 }
413
414 sub ssh2_rewrite_auth_homedir {
415 my $self = shift;
416 my $tmpdir = $self->{tmpdir};
417 my $setup = test_setup($tmpdir, 'sftp');
418
419 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
420 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
421
422 my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key');
423 my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub');
424 my $rsa_rfc4716_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/authorized_rsa_keys2');
425
426 my $authorized_keys = File::Spec->rel2abs("$tmpdir/.authorized_keys");
427 unless (copy($rsa_rfc4716_key, $authorized_keys)) {
428 die("Can't copy $rsa_rfc4716_key to $authorized_keys: $!");
429 }
430
431 my $config = {
432 PidFile => $setup->{pid_file},
433 ScoreboardFile => $setup->{scoreboard_file},
434 SystemLog => $setup->{log_file},
435 TraceLog => $setup->{log_file},
436 Trace => 'event:20 ssh2:20 sftp:20 scp:20',
437
438 AuthUserFile => $setup->{auth_user_file},
439 AuthGroupFile => $setup->{auth_group_file},
440 AuthOrder => 'mod_auth_file.c',
441
442 RewriteHome => 'on',
443
444 IfModules => {
445 'mod_delay.c' => {
446 DelayEngine => 'off',
447 },
448
449 'mod_rewrite.c' => [
450 'RewriteEngine on',
451 "RewriteLog $setup->{log_file}",
452
453 'RewriteCondition %m REWRITE_HOME',
454 'RewriteRule (.*\/)?(.*) /opt/data/test/$2',
455
456 'RewriteMap lowercase int:tolower',
457 'RewriteCondition %m USER',
458 'RewriteRule ^(.*)$ ${lowercase:$1}',
459 ],
460
461 'mod_sftp.c' => [
462 "SFTPEngine on",
463 "SFTPLog $setup->{log_file}",
464 "SFTPHostKey $rsa_host_key",
465 "SFTPHostKey $dsa_host_key",
466 "SFTPAuthorizedUserKeys file:~/.authorized_keys",
467 ],
468 },
469 };
470
471 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
472 $config);
473
474 # Open pipes, for use between the parent and child processes. Specifically,
475 # the child will indicate when it's done with its test by writing a message
476 # to the parent.
477 my ($rfh, $wfh);
478 unless (pipe($rfh, $wfh)) {
479 die("Can't open pipe: $!");
480 }
481
482 require Net::SSH2;
483
484 my $ex;
485
486 # Fork child
487 $self->handle_sigchld();
488 defined(my $pid = fork()) or die("Can't fork: $!");
489 if ($pid) {
490 eval {
491 my $ssh2 = Net::SSH2->new();
492
493 sleep(1);
494
495 unless ($ssh2->connect('127.0.0.1', $port)) {
496 my ($err_code, $err_name, $err_str) = $ssh2->error();
497 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
498 }
499
500 if ($ssh2->auth_publickey(uc($setup->{user}), $rsa_pub_key,
501 $rsa_priv_key)) {
502 die("Publickey authentication succeeded unexpectedly");
503 }
504
505 $ssh2->disconnect();
506 };
507 if ($@) {
508 $ex = $@;
509 }
510
511 $wfh->print("done\n");
512 $wfh->flush();
513
514 } else {
515 eval { server_wait($setup->{config_file}, $rfh) };
516 if ($@) {
517 warn($@);
518 exit 1;
519 }
520
521 exit 0;
522 }
523
524 # Stop server
525 server_stop($setup->{pid_file});
526 $self->assert_child_ok($pid);
527
528 eval {
529 if (open(my $fh, "< $setup->{log_file}")) {
530 my $ok = 0;
531
532 while (my $line = <$fh>) {
533 if ($line =~ /error opening SFTPAuthorizedUserKeys .*?: No such file or directory/) {
534 $ok = 1;
535 last;
536 }
537 }
538
539 close($fh);
540
541 } else {
542 die("Can't read $setup->{log_file}: $!");
543 }
544 };
545 if ($@) {
546 $ex = $@;
547 }
548
549 test_cleanup($setup->{log_file}, $ex);
550 }
551
552 sub sftp_rewrite_stat {
553 my $self = shift;
554 my $tmpdir = $self->{tmpdir};
555
556 my $config_file = "$tmpdir/sftp.conf";
557 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
558 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
559
560 my $log_file = test_get_logfile();
561
562 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
563 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
564
565 my $user = 'proftpd';
566 my $passwd = 'test';
567 my $group = 'ftpd';
568 my $home_dir = File::Spec->rel2abs($tmpdir);
569 my $uid = 500;
570 my $gid = 500;
571
572 my $test_file = 'test_file_here.txt';
573 my $test_path = File::Spec->rel2abs("$tmpdir/$test_file");
574
575 if (open(my $fh, "> $test_path")) {
576 print $fh "Hello, World!\n";
577
578 unless (close($fh)) {
579 die("Can't write $test_path: $!");
580 }
581
582 } else {
583 die("Can't open $test_path: $!");
584 }
585
586 # Make sure that, if we're running as root, that the home directory has
587 # permissions/privs set for the account we create
588 if ($< == 0) {
589 unless (chmod(0755, $home_dir)) {
590 die("Can't set perms on $home_dir to 0755: $!");
591 }
592
593 unless (chown($uid, $gid, $home_dir)) {
594 die("Can't set owner of $home_dir to $uid/$gid: $!");
595 }
596 }
597
598 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
599 '/bin/bash');
600 auth_group_write($auth_group_file, $group, $gid, $user);
601
602 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
603 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
604
605 my $config = {
606 PidFile => $pid_file,
607 ScoreboardFile => $scoreboard_file,
608 SystemLog => $log_file,
609 TraceLog => $log_file,
610 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
611
612 AuthUserFile => $auth_user_file,
613 AuthGroupFile => $auth_group_file,
614 AuthOrder => 'mod_auth_file.c',
615
616 IfModules => {
617 'mod_delay.c' => {
618 DelayEngine => 'off',
619 },
620
621 'mod_rewrite.c' => [
622 'RewriteEngine on',
623 "RewriteLog $log_file",
624
625 'RewriteMap replace int:replaceall',
626 'RewriteCondition %m STAT',
627 'RewriteRule ^(.*) "${replace:!$1! !_}"',
628 ],
629
630 'mod_sftp.c' => [
631 "SFTPEngine on",
632 "SFTPLog $log_file",
633 "SFTPHostKey $rsa_host_key",
634 "SFTPHostKey $dsa_host_key",
635 ],
636 },
637 };
638
639 my ($port, $config_user, $config_group) = config_write($config_file, $config);
640
641 my $config_size = (stat($config_file))[7];
642
643 # Open pipes, for use between the parent and child processes. Specifically,
644 # the child will indicate when it's done with its test by writing a message
645 # to the parent.
646 my ($rfh, $wfh);
647 unless (pipe($rfh, $wfh)) {
648 die("Can't open pipe: $!");
649 }
650
651 require Net::SSH2;
652
653 my $ex;
654
655 # Fork child
656 $self->handle_sigchld();
657 defined(my $pid = fork()) or die("Can't fork: $!");
658 if ($pid) {
659 eval {
660 my $ssh2 = Net::SSH2->new();
661
662 sleep(1);
663
664 unless ($ssh2->connect('127.0.0.1', $port)) {
665 my ($err_code, $err_name, $err_str) = $ssh2->error();
666 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
667 }
668
669 unless ($ssh2->auth_password($user, $passwd)) {
670 my ($err_code, $err_name, $err_str) = $ssh2->error();
671 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
672 }
673
674 my $sftp = $ssh2->sftp();
675 unless ($sftp) {
676 my ($err_code, $err_name, $err_str) = $ssh2->error();
677 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
678 }
679
680 my $attrs = $sftp->stat('test file here.txt', 1);
681 unless ($attrs) {
682 my ($err_code, $err_name) = $sftp->error();
683 die("FXP_STAT failed: [$err_name] ($err_code)");
684 }
685
686 my $expected;
687
688 $expected = 14;
689 my $file_size = $attrs->{size};
690 $self->assert($expected == $file_size,
691 test_msg("Expected '$expected', got '$file_size'"));
692
693 $expected = $<;
694 my $file_uid = $attrs->{uid};
695 $self->assert($expected == $file_uid,
696 test_msg("Expected '$expected', got '$file_uid'"));
697
698 $expected = $(;
699 my $file_gid = $attrs->{gid};
700 $self->assert($expected == $file_gid,
701 test_msg("Expected '$expected', got '$file_gid'"));
702
703 $ssh2->disconnect();
704 };
705
706 if ($@) {
707 $ex = $@;
708 }
709
710 $wfh->print("done\n");
711 $wfh->flush();
712
713 } else {
714 eval { server_wait($config_file, $rfh) };
715 if ($@) {
716 warn($@);
717 exit 1;
718 }
719
720 exit 0;
721 }
722
723 # Stop server
724 server_stop($pid_file);
725
726 $self->assert_child_ok($pid);
727
728 if ($ex) {
729 test_append_logfile($log_file, $ex);
730 unlink($log_file);
731
732 die($ex);
733 }
734
735 unlink($log_file);
736 }
737
738 sub sftp_rewrite_lstat {
739 my $self = shift;
740 my $tmpdir = $self->{tmpdir};
741
742 my $config_file = "$tmpdir/sftp.conf";
743 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
744 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
745
746 my $log_file = test_get_logfile();
747
748 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
749 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
750
751 my $user = 'proftpd';
752 my $passwd = 'test';
753 my $group = 'ftpd';
754 my $home_dir = File::Spec->rel2abs($tmpdir);
755 my $uid = 500;
756 my $gid = 500;
757
758 # Make sure that, if we're running as root, that the home directory has
759 # permissions/privs set for the account we create
760 if ($< == 0) {
761 unless (chmod(0755, $home_dir)) {
762 die("Can't set perms on $home_dir to 0755: $!");
763 }
764
765 unless (chown($uid, $gid, $home_dir)) {
766 die("Can't set owner of $home_dir to $uid/$gid: $!");
767 }
768 }
769
770 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
771 '/bin/bash');
772 auth_group_write($auth_group_file, $group, $gid, $user);
773
774 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
775 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
776
777 my $config = {
778 PidFile => $pid_file,
779 ScoreboardFile => $scoreboard_file,
780 SystemLog => $log_file,
781 TraceLog => $log_file,
782 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
783
784 AuthUserFile => $auth_user_file,
785 AuthGroupFile => $auth_group_file,
786 AuthOrder => 'mod_auth_file.c',
787
788 IfModules => {
789 'mod_delay.c' => {
790 DelayEngine => 'off',
791 },
792
793 'mod_rewrite.c' => [
794 'RewriteEngine on',
795 "RewriteLog $log_file",
796
797 'RewriteMap replace int:replaceall',
798 'RewriteCondition %m LSTAT',
799 'RewriteRule ^(.*) "${replace:!$1! !_}"',
800 ],
801
802 'mod_sftp.c' => [
803 "SFTPEngine on",
804 "SFTPLog $log_file",
805 "SFTPHostKey $rsa_host_key",
806 "SFTPHostKey $dsa_host_key",
807 ],
808 },
809 };
810
811 my ($port, $config_user, $config_group) = config_write($config_file, $config);
812
813 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
814 if (open(my $fh, "> $test_file")) {
815 print $fh "ABCD" x 1024;
816 unless (close($fh)) {
817 die("Can't write $test_file: $!");
818 }
819
820 } else {
821 die("Can't open $test_file: $!");
822 }
823
824 my $test_symlink = File::Spec->rel2abs("$tmpdir/test_link");
825 unless (symlink($test_file, $test_symlink)) {
826 die("Can't symlink $test_symlink to $test_file: $!");
827 }
828
829 my $test_size = (lstat($test_symlink))[7];
830
831 # Open pipes, for use between the parent and child processes. Specifically,
832 # the child will indicate when it's done with its test by writing a message
833 # to the parent.
834 my ($rfh, $wfh);
835 unless (pipe($rfh, $wfh)) {
836 die("Can't open pipe: $!");
837 }
838
839 require Net::SSH2;
840
841 my $ex;
842
843 # Fork child
844 $self->handle_sigchld();
845 defined(my $pid = fork()) or die("Can't fork: $!");
846 if ($pid) {
847 eval {
848 my $ssh2 = Net::SSH2->new();
849
850 sleep(1);
851
852 unless ($ssh2->connect('127.0.0.1', $port)) {
853 my ($err_code, $err_name, $err_str) = $ssh2->error();
854 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
855 }
856
857 unless ($ssh2->auth_password($user, $passwd)) {
858 my ($err_code, $err_name, $err_str) = $ssh2->error();
859 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
860 }
861
862 my $sftp = $ssh2->sftp();
863 unless ($sftp) {
864 my ($err_code, $err_name, $err_str) = $ssh2->error();
865 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
866 }
867
868 my $attrs = $sftp->stat('test link', 0);
869 unless ($attrs) {
870 my ($err_code, $err_name) = $sftp->error();
871 die("FXP_LSTAT failed: [$err_name] ($err_code)");
872 }
873
874 my $expected;
875
876 $expected = $test_size;
877 my $file_size = $attrs->{size};
878 $self->assert($expected == $file_size,
879 test_msg("Expected '$expected', got '$file_size'"));
880
881 $expected = $<;
882 my $file_uid = $attrs->{uid};
883 $self->assert($expected == $file_uid,
884 test_msg("Expected '$expected', got '$file_uid'"));
885
886 $expected = $(;
887 my $file_gid = $attrs->{gid};
888 $self->assert($expected == $file_gid,
889 test_msg("Expected '$expected', got '$file_gid'"));
890
891 $ssh2->disconnect();
892 };
893
894 if ($@) {
895 $ex = $@;
896 }
897
898 $wfh->print("done\n");
899 $wfh->flush();
900
901 } else {
902 eval { server_wait($config_file, $rfh) };
903 if ($@) {
904 warn($@);
905 exit 1;
906 }
907
908 exit 0;
909 }
910
911 # Stop server
912 server_stop($pid_file);
913
914 $self->assert_child_ok($pid);
915
916 if ($ex) {
917 test_append_logfile($log_file, $ex);
918 unlink($log_file);
919
920 die($ex);
921 }
922
923 unlink($log_file);
924 }
925
926 sub sftp_rewrite_setstat {
927 my $self = shift;
928 my $tmpdir = $self->{tmpdir};
929
930 my $config_file = "$tmpdir/sftp.conf";
931 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
932 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
933
934 my $log_file = test_get_logfile();
935
936 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
937 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
938
939 my $user = 'proftpd';
940 my $passwd = 'test';
941 my $group = 'ftpd';
942 my $home_dir = File::Spec->rel2abs($tmpdir);
943 my $uid = 500;
944 my $gid = 500;
945
946 # Make sure that, if we're running as root, that the home directory has
947 # permissions/privs set for the account we create
948 if ($< == 0) {
949 unless (chmod(0755, $home_dir)) {
950 die("Can't set perms on $home_dir to 0755: $!");
951 }
952
953 unless (chown($uid, $gid, $home_dir)) {
954 die("Can't set owner of $home_dir to $uid/$gid: $!");
955 }
956 }
957
958 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
959 '/bin/bash');
960 auth_group_write($auth_group_file, $group, $gid, $user);
961
962 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
963 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
964
965 my $config = {
966 PidFile => $pid_file,
967 ScoreboardFile => $scoreboard_file,
968 SystemLog => $log_file,
969 TraceLog => $log_file,
970 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
971
972 AuthUserFile => $auth_user_file,
973 AuthGroupFile => $auth_group_file,
974 AuthOrder => 'mod_auth_file.c',
975
976 IfModules => {
977 'mod_delay.c' => {
978 DelayEngine => 'off',
979 },
980
981 'mod_rewrite.c' => [
982 'RewriteEngine on',
983 "RewriteLog $log_file",
984
985 'RewriteMap replace int:replaceall',
986 'RewriteCondition %m ^(STAT|SETSTAT)$',
987 'RewriteRule ^(.*) "${replace:!$1! !_}"',
988 ],
989
990 'mod_sftp.c' => [
991 "SFTPEngine on",
992 "SFTPLog $log_file",
993 "SFTPHostKey $rsa_host_key",
994 "SFTPHostKey $dsa_host_key",
995 ],
996 },
997 };
998
999 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1000
1001 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
1002 if (open(my $fh, "> $test_file")) {
1003 print $fh "ABCD" x 1024;
1004 unless (close($fh)) {
1005 die("Can't write $test_file: $!");
1006 }
1007
1008 } else {
1009 die("Can't open $test_file: $!");
1010 }
1011
1012 # Open pipes, for use between the parent and child processes. Specifically,
1013 # the child will indicate when it's done with its test by writing a message
1014 # to the parent.
1015 my ($rfh, $wfh);
1016 unless (pipe($rfh, $wfh)) {
1017 die("Can't open pipe: $!");
1018 }
1019
1020 require Net::SSH2;
1021
1022 my $ex;
1023
1024 # Fork child
1025 $self->handle_sigchld();
1026 defined(my $pid = fork()) or die("Can't fork: $!");
1027 if ($pid) {
1028 eval {
1029 my $ssh2 = Net::SSH2->new();
1030
1031 sleep(1);
1032
1033 unless ($ssh2->connect('127.0.0.1', $port)) {
1034 my ($err_code, $err_name, $err_str) = $ssh2->error();
1035 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1036 }
1037
1038 unless ($ssh2->auth_password($user, $passwd)) {
1039 my ($err_code, $err_name, $err_str) = $ssh2->error();
1040 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1041 }
1042
1043 my $sftp = $ssh2->sftp();
1044 unless ($sftp) {
1045 my ($err_code, $err_name, $err_str) = $ssh2->error();
1046 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1047 }
1048
1049 my $res = $sftp->setstat('test file.txt',
1050 atime => 0,
1051 mtime => 0,
1052 );
1053 unless ($res) {
1054 my ($err_code, $err_name) = $sftp->error();
1055 die("FXP_SETSTAT failed: [$err_name] ($err_code)");
1056 }
1057
1058 my $attrs = $sftp->stat('test file.txt');
1059 unless ($attrs) {
1060 my ($err_code, $err_name) = $sftp->error();
1061 die("FXP_STAT failed: [$err_name] ($err_code)");
1062 }
1063
1064 $ssh2->disconnect();
1065
1066 my $expected;
1067
1068 $expected = 0;
1069 my $file_atime = $attrs->{atime};
1070 $self->assert($expected == $file_atime,
1071 test_msg("Expected '$expected', got '$file_atime'"));
1072
1073 my $file_mtime = $attrs->{mtime};
1074 $self->assert($expected == $file_mtime,
1075 test_msg("Expected '$expected', got '$file_mtime'"));
1076 };
1077
1078 if ($@) {
1079 $ex = $@;
1080 }
1081
1082 $wfh->print("done\n");
1083 $wfh->flush();
1084
1085 } else {
1086 eval { server_wait($config_file, $rfh) };
1087 if ($@) {
1088 warn($@);
1089 exit 1;
1090 }
1091
1092 exit 0;
1093 }
1094
1095 # Stop server
1096 server_stop($pid_file);
1097
1098 $self->assert_child_ok($pid);
1099
1100 if ($ex) {
1101 test_append_logfile($log_file, $ex);
1102 unlink($log_file);
1103
1104 die($ex);
1105 }
1106
1107 unlink($log_file);
1108 }
1109
1110 sub sftp_rewrite_realpath {
1111 my $self = shift;
1112 my $tmpdir = $self->{tmpdir};
1113
1114 my $config_file = "$tmpdir/sftp.conf";
1115 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
1116 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
1117
1118 my $log_file = test_get_logfile();
1119
1120 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
1121 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
1122
1123 my $user = 'proftpd';
1124 my $passwd = 'test';
1125 my $group = 'ftpd';
1126 my $home_dir = File::Spec->rel2abs($tmpdir);
1127 my $uid = 500;
1128 my $gid = 500;
1129
1130 # Make sure that, if we're running as root, that the home directory has
1131 # permissions/privs set for the account we create
1132 if ($< == 0) {
1133 unless (chmod(0755, $home_dir)) {
1134 die("Can't set perms on $home_dir to 0755: $!");
1135 }
1136
1137 unless (chown($uid, $gid, $home_dir)) {
1138 die("Can't set owner of $home_dir to $uid/$gid: $!");
1139 }
1140 }
1141
1142 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1143 '/bin/bash');
1144 auth_group_write($auth_group_file, $group, $gid, $user);
1145
1146 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
1147 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
1148
1149 my $config = {
1150 PidFile => $pid_file,
1151 ScoreboardFile => $scoreboard_file,
1152 SystemLog => $log_file,
1153 TraceLog => $log_file,
1154 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
1155
1156 AuthUserFile => $auth_user_file,
1157 AuthGroupFile => $auth_group_file,
1158 AuthOrder => 'mod_auth_file.c',
1159
1160 IfModules => {
1161 'mod_delay.c' => {
1162 DelayEngine => 'off',
1163 },
1164
1165 'mod_rewrite.c' => [
1166 'RewriteEngine on',
1167 "RewriteLog $log_file",
1168
1169 'RewriteMap replace int:replaceall',
1170 'RewriteCondition %m REALPATH',
1171 'RewriteRule ^(.*) "${replace:!$1! !_}"',
1172 ],
1173
1174 'mod_sftp.c' => [
1175 "SFTPEngine on",
1176 "SFTPLog $log_file",
1177 "SFTPHostKey $rsa_host_key",
1178 "SFTPHostKey $dsa_host_key",
1179 ],
1180 },
1181 };
1182
1183 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1184
1185 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
1186 if (open(my $fh, "> $test_file")) {
1187 unless (close($fh)) {
1188 die("Can't write $test_file: $!");
1189 }
1190
1191 } else {
1192 die("Can't open $test_file: $!");
1193 }
1194
1195 # Open pipes, for use between the parent and child processes. Specifically,
1196 # the child will indicate when it's done with its test by writing a message
1197 # to the parent.
1198 my ($rfh, $wfh);
1199 unless (pipe($rfh, $wfh)) {
1200 die("Can't open pipe: $!");
1201 }
1202
1203 require Net::SSH2;
1204
1205 my $ex;
1206
1207 # Fork child
1208 $self->handle_sigchld();
1209 defined(my $pid = fork()) or die("Can't fork: $!");
1210 if ($pid) {
1211 eval {
1212 my $ssh2 = Net::SSH2->new();
1213
1214 sleep(1);
1215
1216 unless ($ssh2->connect('127.0.0.1', $port)) {
1217 my ($err_code, $err_name, $err_str) = $ssh2->error();
1218 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1219 }
1220
1221 unless ($ssh2->auth_password($user, $passwd)) {
1222 my ($err_code, $err_name, $err_str) = $ssh2->error();
1223 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1224 }
1225
1226 my $sftp = $ssh2->sftp();
1227 unless ($sftp) {
1228 my ($err_code, $err_name, $err_str) = $ssh2->error();
1229 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1230 }
1231
1232 my $resolved = $sftp->realpath('test file.txt');
1233 unless ($resolved) {
1234 my ($err_code, $err_name) = $sftp->error();
1235 die("FXP_REALPATH failed: [$err_name] ($err_code)");
1236 }
1237
1238 my $expected;
1239
1240 $expected = $test_file;
1241 if ($^O eq 'darwin') {
1242 # Mac OSX hack
1243 $expected = '/private' . $expected;
1244 }
1245
1246 $self->assert($expected eq $resolved,
1247 test_msg("Expected '$expected', got '$resolved'"));
1248
1249 $ssh2->disconnect();
1250 };
1251
1252 if ($@) {
1253 $ex = $@;
1254 }
1255
1256 $wfh->print("done\n");
1257 $wfh->flush();
1258
1259 } else {
1260 eval { server_wait($config_file, $rfh) };
1261 if ($@) {
1262 warn($@);
1263 exit 1;
1264 }
1265
1266 exit 0;
1267 }
1268
1269 # Stop server
1270 server_stop($pid_file);
1271
1272 $self->assert_child_ok($pid);
1273
1274 if ($ex) {
1275 test_append_logfile($log_file, $ex);
1276 unlink($log_file);
1277
1278 die($ex);
1279 }
1280
1281 unlink($log_file);
1282 }
1283
1284 sub sftp_rewrite_realpath_backslashes_bug4017 {
1285 my $self = shift;
1286 my $tmpdir = $self->{tmpdir};
1287
1288 my $config_file = "$tmpdir/sftp.conf";
1289 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
1290 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
1291
1292 my $log_file = test_get_logfile();
1293
1294 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
1295 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
1296
1297 my $user = 'proftpd';
1298 my $passwd = 'test';
1299 my $group = 'ftpd';
1300 my $home_dir = File::Spec->rel2abs($tmpdir);
1301 my $uid = 500;
1302 my $gid = 500;
1303
1304 # Make sure that, if we're running as root, that the home directory has
1305 # permissions/privs set for the account we create
1306 if ($< == 0) {
1307 unless (chmod(0755, $home_dir)) {
1308 die("Can't set perms on $home_dir to 0755: $!");
1309 }
1310
1311 unless (chown($uid, $gid, $home_dir)) {
1312 die("Can't set owner of $home_dir to $uid/$gid: $!");
1313 }
1314 }
1315
1316 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1317 '/bin/bash');
1318 auth_group_write($auth_group_file, $group, $gid, $user);
1319
1320 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
1321 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
1322
1323 my $config = {
1324 PidFile => $pid_file,
1325 ScoreboardFile => $scoreboard_file,
1326 SystemLog => $log_file,
1327 TraceLog => $log_file,
1328 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
1329
1330 AuthUserFile => $auth_user_file,
1331 AuthGroupFile => $auth_group_file,
1332 AuthOrder => 'mod_auth_file.c',
1333
1334 IfModules => {
1335 'mod_delay.c' => {
1336 DelayEngine => 'off',
1337 },
1338
1339 'mod_rewrite.c' => [
1340 'RewriteEngine on',
1341 "RewriteLog $log_file",
1342
1343 'RewriteMap replace int:replaceall',
1344 'RewriteCondition %m REALPATH',
1345 'RewriteRule (.*) "${replace:!$1!\\\\!/}"',
1346 ],
1347
1348 'mod_sftp.c' => [
1349 "SFTPEngine on",
1350 "SFTPLog $log_file",
1351 "SFTPHostKey $rsa_host_key",
1352 "SFTPHostKey $dsa_host_key",
1353 ],
1354 },
1355 };
1356
1357 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1358
1359 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
1360 if (open(my $fh, "> $test_file")) {
1361 unless (close($fh)) {
1362 die("Can't write $test_file: $!");
1363 }
1364
1365 } else {
1366 die("Can't open $test_file: $!");
1367 }
1368
1369 # Open pipes, for use between the parent and child processes. Specifically,
1370 # the child will indicate when it's done with its test by writing a message
1371 # to the parent.
1372 my ($rfh, $wfh);
1373 unless (pipe($rfh, $wfh)) {
1374 die("Can't open pipe: $!");
1375 }
1376
1377 require Net::SSH2;
1378
1379 my $ex;
1380
1381 # Fork child
1382 $self->handle_sigchld();
1383 defined(my $pid = fork()) or die("Can't fork: $!");
1384 if ($pid) {
1385 eval {
1386 my $ssh2 = Net::SSH2->new();
1387
1388 sleep(1);
1389
1390 unless ($ssh2->connect('127.0.0.1', $port)) {
1391 my ($err_code, $err_name, $err_str) = $ssh2->error();
1392 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1393 }
1394
1395 unless ($ssh2->auth_password($user, $passwd)) {
1396 my ($err_code, $err_name, $err_str) = $ssh2->error();
1397 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1398 }
1399
1400 my $sftp = $ssh2->sftp();
1401 unless ($sftp) {
1402 my ($err_code, $err_name, $err_str) = $ssh2->error();
1403 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1404 }
1405
1406 my $munged = $test_file;
1407 $munged =~ s/\//\\/g;
1408
1409 my $resolved = $sftp->realpath($munged);
1410 unless ($resolved) {
1411 my ($err_code, $err_name) = $sftp->error();
1412 die("FXP_REALPATH failed: [$err_name] ($err_code)");
1413 }
1414
1415 my $expected;
1416
1417 $expected = $test_file;
1418 if ($^O eq 'darwin') {
1419 # Mac OSX hack
1420 $expected = '/private' . $expected;
1421 }
1422
1423 $self->assert($expected eq $resolved,
1424 test_msg("Expected '$expected', got '$resolved'"));
1425
1426 $ssh2->disconnect();
1427 };
1428
1429 if ($@) {
1430 $ex = $@;
1431 }
1432
1433 $wfh->print("done\n");
1434 $wfh->flush();
1435
1436 } else {
1437 eval { server_wait($config_file, $rfh) };
1438 if ($@) {
1439 warn($@);
1440 exit 1;
1441 }
1442
1443 exit 0;
1444 }
1445
1446 # Stop server
1447 server_stop($pid_file);
1448
1449 $self->assert_child_ok($pid);
1450
1451 if ($ex) {
1452 test_append_logfile($log_file, $ex);
1453 unlink($log_file);
1454
1455 die($ex);
1456 }
1457
1458 unlink($log_file);
1459 }
1460
1461 sub sftp_rewrite_upload {
1462 my $self = shift;
1463 my $tmpdir = $self->{tmpdir};
1464
1465 my $config_file = "$tmpdir/sftp.conf";
1466 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
1467 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
1468
1469 my $log_file = test_get_logfile();
1470
1471 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
1472 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
1473
1474 my $user = 'proftpd';
1475 my $passwd = 'test';
1476 my $group = 'ftpd';
1477 my $home_dir = File::Spec->rel2abs($tmpdir);
1478 my $uid = 500;
1479 my $gid = 500;
1480
1481 # Make sure that, if we're running as root, that the home directory has
1482 # permissions/privs set for the account we create
1483 if ($< == 0) {
1484 unless (chmod(0755, $home_dir)) {
1485 die("Can't set perms on $home_dir to 0755: $!");
1486 }
1487
1488 unless (chown($uid, $gid, $home_dir)) {
1489 die("Can't set owner of $home_dir to $uid/$gid: $!");
1490 }
1491 }
1492
1493 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1494 '/bin/bash');
1495 auth_group_write($auth_group_file, $group, $gid, $user);
1496
1497 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
1498 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
1499
1500 my $config = {
1501 PidFile => $pid_file,
1502 ScoreboardFile => $scoreboard_file,
1503 SystemLog => $log_file,
1504 TraceLog => $log_file,
1505 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
1506
1507 AuthUserFile => $auth_user_file,
1508 AuthGroupFile => $auth_group_file,
1509 AuthOrder => 'mod_auth_file.c',
1510
1511 IfModules => {
1512 'mod_delay.c' => {
1513 DelayEngine => 'off',
1514 },
1515
1516 'mod_rewrite.c' => [
1517 'RewriteEngine on',
1518 "RewriteLog $log_file",
1519
1520 'RewriteMap replace int:replaceall',
1521 'RewriteCondition %m STOR',
1522 'RewriteRule ^(.*) "${replace:!$1! !_}"',
1523 ],
1524
1525 'mod_sftp.c' => [
1526 "SFTPEngine on",
1527 "SFTPLog $log_file",
1528 "SFTPHostKey $rsa_host_key",
1529 "SFTPHostKey $dsa_host_key",
1530 ],
1531 },
1532 };
1533
1534 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1535
1536 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
1537
1538 # Open pipes, for use between the parent and child processes. Specifically,
1539 # the child will indicate when it's done with its test by writing a message
1540 # to the parent.
1541 my ($rfh, $wfh);
1542 unless (pipe($rfh, $wfh)) {
1543 die("Can't open pipe: $!");
1544 }
1545
1546 require Net::SSH2;
1547
1548 my $ex;
1549
1550 # Ignore SIGPIPE
1551 local $SIG{PIPE} = sub { };
1552
1553 # Fork child
1554 $self->handle_sigchld();
1555 defined(my $pid = fork()) or die("Can't fork: $!");
1556 if ($pid) {
1557 eval {
1558 my $ssh2 = Net::SSH2->new();
1559
1560 sleep(1);
1561
1562 unless ($ssh2->connect('127.0.0.1', $port)) {
1563 my ($err_code, $err_name, $err_str) = $ssh2->error();
1564 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1565 }
1566
1567 unless ($ssh2->auth_password($user, $passwd)) {
1568 my ($err_code, $err_name, $err_str) = $ssh2->error();
1569 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1570 }
1571
1572 my $sftp = $ssh2->sftp();
1573 unless ($sftp) {
1574 my ($err_code, $err_name, $err_str) = $ssh2->error();
1575 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1576 }
1577
1578 my $fh = $sftp->open('test file.txt', O_WRONLY|O_CREAT|O_TRUNC, 0644);
1579 unless ($fh) {
1580 my ($err_code, $err_name) = $sftp->error();
1581 die("FXP_OPEN failed: [$err_name] ($err_code)");
1582 }
1583
1584 my $count = 20;
1585 for (my $i = 0; $i < $count; $i++) {
1586 print $fh "ABCD" x 8192;
1587 }
1588
1589 # To issue the FXP_CLOSE, we have to explicit destroy the filehandle
1590 $fh = undef;
1591
1592 $ssh2->disconnect();
1593
1594 # Make sure that the rewritten file is present
1595 $self->assert(-f $test_file, test_msg("File $test_file unexpectedly missing"));
1596 };
1597
1598 if ($@) {
1599 $ex = $@;
1600 }
1601
1602 $wfh->print("done\n");
1603 $wfh->flush();
1604
1605 } else {
1606 eval { server_wait($config_file, $rfh) };
1607 if ($@) {
1608 warn($@);
1609 exit 1;
1610 }
1611
1612 exit 0;
1613 }
1614
1615 # Stop server
1616 server_stop($pid_file);
1617
1618 $self->assert_child_ok($pid);
1619
1620 if ($ex) {
1621 test_append_logfile($log_file, $ex);
1622 unlink($log_file);
1623
1624 die($ex);
1625 }
1626
1627 unlink($log_file);
1628 }
1629
1630 sub sftp_rewrite_download {
1631 my $self = shift;
1632 my $tmpdir = $self->{tmpdir};
1633
1634 my $config_file = "$tmpdir/sftp.conf";
1635 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
1636 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
1637
1638 my $log_file = test_get_logfile();
1639
1640 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
1641 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
1642
1643 my $user = 'proftpd';
1644 my $passwd = 'test';
1645 my $group = 'ftpd';
1646 my $home_dir = File::Spec->rel2abs($tmpdir);
1647 my $uid = 500;
1648 my $gid = 500;
1649
1650 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
1651 if (open(my $fh, "> $test_file")) {
1652 my $count = 20;
1653 for (my $i = 0; $i < $count; $i++) {
1654 print $fh "ABCD" x 8192;
1655 }
1656
1657 unless (close($fh)) {
1658 die("Can't write $test_file: $!");
1659 }
1660
1661 } else {
1662 die("Can't open $test_file: $!");
1663 }
1664
1665 my $test_sz = (stat($test_file))[7];
1666
1667 # Make sure that, if we're running as root, that the home directory has
1668 # permissions/privs set for the account we create
1669 if ($< == 0) {
1670 unless (chmod(0755, $home_dir)) {
1671 die("Can't set perms on $home_dir to 0755: $!");
1672 }
1673
1674 unless (chown($uid, $gid, $home_dir)) {
1675 die("Can't set owner of $home_dir to $uid/$gid: $!");
1676 }
1677 }
1678
1679 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1680 '/bin/bash');
1681 auth_group_write($auth_group_file, $group, $gid, $user);
1682
1683 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
1684 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
1685
1686 my $config = {
1687 PidFile => $pid_file,
1688 ScoreboardFile => $scoreboard_file,
1689 SystemLog => $log_file,
1690 TraceLog => $log_file,
1691 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
1692
1693 AuthUserFile => $auth_user_file,
1694 AuthGroupFile => $auth_group_file,
1695 AuthOrder => 'mod_auth_file.c',
1696
1697 IfModules => {
1698 'mod_delay.c' => {
1699 DelayEngine => 'off',
1700 },
1701
1702 'mod_rewrite.c' => [
1703 'RewriteEngine on',
1704 "RewriteLog $log_file",
1705
1706 'RewriteMap replace int:replaceall',
1707 'RewriteCondition %m RETR',
1708 'RewriteRule ^(.*) "${replace:!$1! !_}"',
1709 ],
1710
1711 'mod_sftp.c' => [
1712 "SFTPEngine on",
1713 "SFTPLog $log_file",
1714 "SFTPHostKey $rsa_host_key",
1715 "SFTPHostKey $dsa_host_key",
1716 ],
1717 },
1718 };
1719
1720 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1721
1722 # Open pipes, for use between the parent and child processes. Specifically,
1723 # the child will indicate when it's done with its test by writing a message
1724 # to the parent.
1725 my ($rfh, $wfh);
1726 unless (pipe($rfh, $wfh)) {
1727 die("Can't open pipe: $!");
1728 }
1729
1730 require Net::SSH2;
1731
1732 my $ex;
1733
1734 # Ignore SIGPIPE
1735 local $SIG{PIPE} = sub { };
1736
1737 # Fork child
1738 $self->handle_sigchld();
1739 defined(my $pid = fork()) or die("Can't fork: $!");
1740 if ($pid) {
1741 eval {
1742 my $ssh2 = Net::SSH2->new();
1743
1744 sleep(1);
1745
1746 unless ($ssh2->connect('127.0.0.1', $port)) {
1747 my ($err_code, $err_name, $err_str) = $ssh2->error();
1748 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1749 }
1750
1751 unless ($ssh2->auth_password($user, $passwd)) {
1752 my ($err_code, $err_name, $err_str) = $ssh2->error();
1753 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1754 }
1755
1756 my $sftp = $ssh2->sftp();
1757 unless ($sftp) {
1758 my ($err_code, $err_name, $err_str) = $ssh2->error();
1759 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1760 }
1761
1762 my $fh = $sftp->open('test file.txt', O_RDONLY);
1763 unless ($fh) {
1764 my ($err_code, $err_name) = $sftp->error();
1765 die("FXP_OPEN failed: [$err_name] ($err_code)");
1766 }
1767
1768 my $buf;
1769 my $size = 0;
1770
1771 my $res = $fh->read($buf, 8192);
1772 while ($res) {
1773 $size += $res;
1774
1775 $res = $fh->read($buf, 8192);
1776 }
1777
1778 # To issue the FXP_CLOSE, we have to explicit destroy the filehandle
1779 $fh = undef;
1780
1781 $self->assert($test_sz == $size,
1782 test_msg("Expected $test_sz, got $size"));
1783
1784 $ssh2->disconnect();
1785 };
1786
1787 if ($@) {
1788 $ex = $@;
1789 }
1790
1791 $wfh->print("done\n");
1792 $wfh->flush();
1793
1794 } else {
1795 eval { server_wait($config_file, $rfh) };
1796 if ($@) {
1797 warn($@);
1798 exit 1;
1799 }
1800
1801 exit 0;
1802 }
1803
1804 # Stop server
1805 server_stop($pid_file);
1806
1807 $self->assert_child_ok($pid);
1808
1809 if ($ex) {
1810 test_append_logfile($log_file, $ex);
1811 unlink($log_file);
1812
1813 die($ex);
1814 }
1815
1816 unlink($log_file);
1817 }
1818
1819 sub sftp_rewrite_readdir {
1820 my $self = shift;
1821 my $tmpdir = $self->{tmpdir};
1822
1823 my $config_file = "$tmpdir/sftp.conf";
1824 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
1825 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
1826
1827 my $log_file = test_get_logfile();
1828
1829 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
1830 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
1831
1832 my $user = 'proftpd';
1833 my $passwd = 'test';
1834 my $group = 'ftpd';
1835 my $home_dir = File::Spec->rel2abs($tmpdir);
1836 my $uid = 500;
1837 my $gid = 500;
1838
1839 my $sub_dir = File::Spec->rel2abs("$tmpdir/sub_dir");
1840 mkpath($sub_dir);
1841
1842 my $test_file = File::Spec->rel2abs("$sub_dir/subfile.txt");
1843 if (open(my $fh, "> $test_file")) {
1844 close($fh);
1845
1846 } else {
1847 die("Can't open $test_file: $!");
1848 }
1849
1850 # Make sure that, if we're running as root, that the home directory has
1851 # permissions/privs set for the account we create
1852 if ($< == 0) {
1853 unless (chmod(0755, $home_dir)) {
1854 die("Can't set perms on $home_dir to 0755: $!");
1855 }
1856
1857 unless (chown($uid, $gid, $home_dir)) {
1858 die("Can't set owner of $home_dir to $uid/$gid: $!");
1859 }
1860 }
1861
1862 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1863 '/bin/bash');
1864 auth_group_write($auth_group_file, $group, $gid, $user);
1865
1866 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
1867 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
1868
1869 my $config = {
1870 PidFile => $pid_file,
1871 ScoreboardFile => $scoreboard_file,
1872 SystemLog => $log_file,
1873 TraceLog => $log_file,
1874 Trace => 'auth:10 ssh2:20 sftp:20 scp:20',
1875
1876 AuthUserFile => $auth_user_file,
1877 AuthGroupFile => $auth_group_file,
1878 AuthOrder => 'mod_auth_file.c',
1879
1880 IfModules => {
1881 'mod_delay.c' => {
1882 DelayEngine => 'off',
1883 },
1884
1885 'mod_rewrite.c' => [
1886 'RewriteEngine on',
1887 "RewriteLog $log_file",
1888
1889 'RewriteMap replace int:replaceall',
1890 'RewriteCondition %m OPENDIR',
1891 'RewriteRule ^(.*) "${replace:!$1! !_}"',
1892 ],
1893
1894 'mod_sftp.c' => [
1895 "SFTPEngine on",
1896 "SFTPLog $log_file",
1897 "SFTPHostKey $rsa_host_key",
1898 "SFTPHostKey $dsa_host_key",
1899 ],
1900 },
1901 };
1902
1903 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1904
1905 # Open pipes, for use between the parent and child processes. Specifically,
1906 # the child will indicate when it's done with its test by writing a message
1907 # to the parent.
1908 my ($rfh, $wfh);
1909 unless (pipe($rfh, $wfh)) {
1910 die("Can't open pipe: $!");
1911 }
1912
1913 require Net::SSH2;
1914
1915 my $ex;
1916
1917 # Fork child
1918 $self->handle_sigchld();
1919 defined(my $pid = fork()) or die("Can't fork: $!");
1920 if ($pid) {
1921 eval {
1922 my $ssh2 = Net::SSH2->new();
1923
1924 sleep(1);
1925
1926 unless ($ssh2->connect('127.0.0.1', $port)) {
1927 my ($err_code, $err_name, $err_str) = $ssh2->error();
1928 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1929 }
1930
1931 unless ($ssh2->auth_password($user, $passwd)) {
1932 my ($err_code, $err_name, $err_str) = $ssh2->error();
1933 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1934 }
1935
1936 my $sftp = $ssh2->sftp();
1937 unless ($sftp) {
1938 my ($err_code, $err_name, $err_str) = $ssh2->error();
1939 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1940 }
1941
1942 my $dir = $sftp->opendir('sub dir');
1943 unless ($dir) {
1944 my ($err_code, $err_name) = $sftp->error();
1945 die("FXP_OPENDIR failed: [$err_name] ($err_code)");
1946 }
1947
1948 my $res = {};
1949
1950 my $file = $dir->read();
1951 while ($file) {
1952 $res->{$file->{name}} = $file;
1953 $file = $dir->read();
1954 }
1955
1956 my $expected = {
1957 '.' => 1,
1958 '..' => 1,
1959 'subfile.txt' => 1,
1960 };
1961
1962 # To issue the FXP_CLOSE, we have to explicit destroy the dirhandle
1963 $dir = undef;
1964
1965 $ssh2->disconnect();
1966
1967 my $ok = 1;
1968 my $mismatch;
1969
1970 my $seen = [];
1971 foreach my $name (keys(%$res)) {
1972 push(@$seen, $name);
1973
1974 unless (defined($expected->{$name})) {
1975 $mismatch = $name;
1976 $ok = 0;
1977 last;
1978 }
1979 }
1980
1981 unless ($ok) {
1982 die("Unexpected name '$mismatch' appeared in READDIR data")
1983 }
1984
1985 # Now remove from $expected all of the paths we saw; if there are
1986 # any entries remaining in $expected, something went wrong.
1987 foreach my $name (@$seen) {
1988 delete($expected->{$name});
1989 }
1990
1991 my $remaining = scalar(keys(%$expected));
1992 $self->assert(0 == $remaining,
1993 test_msg("Expected 0, got $remaining"));
1994 };
1995
1996 if ($@) {
1997 $ex = $@;
1998 }
1999
2000 $wfh->print("done\n");
2001 $wfh->flush();
2002
2003 } else {
2004 eval { server_wait($config_file, $rfh) };
2005 if ($@) {
2006 warn($@);
2007 exit 1;
2008 }
2009
2010 exit 0;
2011 }
2012
2013 # Stop server
2014 server_stop($pid_file);
2015
2016 $self->assert_child_ok($pid);
2017
2018 if ($ex) {
2019 test_append_logfile($log_file, $ex);
2020 unlink($log_file);
2021
2022 die($ex);
2023 }
2024
2025 unlink($log_file);
2026 }
2027
2028 sub sftp_rewrite_mkdir {
2029 my $self = shift;
2030 my $tmpdir = $self->{tmpdir};
2031
2032 my $config_file = "$tmpdir/sftp.conf";
2033 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
2034 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
2035
2036 my $log_file = test_get_logfile();
2037
2038 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
2039 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
2040
2041 my $user = 'proftpd';
2042 my $passwd = 'test';
2043 my $group = 'ftpd';
2044 my $home_dir = File::Spec->rel2abs($tmpdir);
2045 my $uid = 500;
2046 my $gid = 500;
2047
2048 my $test_dir = File::Spec->rel2abs("$tmpdir/test_dir");
2049
2050 # Make sure that, if we're running as root, that the home directory has
2051 # permissions/privs set for the account we create
2052 if ($< == 0) {
2053 unless (chmod(0755, $home_dir)) {
2054 die("Can't set perms on $home_dir to 0755: $!");
2055 }
2056
2057 unless (chown($uid, $gid, $home_dir)) {
2058 die("Can't set owner of $home_dir to $uid/$gid: $!");
2059 }
2060 }
2061
2062 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2063 '/bin/bash');
2064 auth_group_write($auth_group_file, $group, $gid, $user);
2065
2066 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
2067 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
2068
2069 my $config = {
2070 PidFile => $pid_file,
2071 ScoreboardFile => $scoreboard_file,
2072 SystemLog => $log_file,
2073 TraceLog => $log_file,
2074 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
2075
2076 AuthUserFile => $auth_user_file,
2077 AuthGroupFile => $auth_group_file,
2078 AuthOrder => 'mod_auth_file.c',
2079
2080 IfModules => {
2081 'mod_delay.c' => {
2082 DelayEngine => 'off',
2083 },
2084
2085 'mod_rewrite.c' => [
2086 'RewriteEngine on',
2087 "RewriteLog $log_file",
2088
2089 'RewriteMap replace int:replaceall',
2090 'RewriteCondition %m MKDIR',
2091 'RewriteRule ^(.*) "${replace:!$1! !_}"',
2092 ],
2093
2094 'mod_sftp.c' => [
2095 "SFTPEngine on",
2096 "SFTPLog $log_file",
2097 "SFTPHostKey $rsa_host_key",
2098 "SFTPHostKey $dsa_host_key",
2099 ],
2100 },
2101 };
2102
2103 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2104
2105 # Open pipes, for use between the parent and child processes. Specifically,
2106 # the child will indicate when it's done with its test by writing a message
2107 # to the parent.
2108 my ($rfh, $wfh);
2109 unless (pipe($rfh, $wfh)) {
2110 die("Can't open pipe: $!");
2111 }
2112
2113 require Net::SSH2;
2114
2115 my $ex;
2116
2117 # Fork child
2118 $self->handle_sigchld();
2119 defined(my $pid = fork()) or die("Can't fork: $!");
2120 if ($pid) {
2121 eval {
2122 my $ssh2 = Net::SSH2->new();
2123
2124 sleep(1);
2125
2126 unless ($ssh2->connect('127.0.0.1', $port)) {
2127 my ($err_code, $err_name, $err_str) = $ssh2->error();
2128 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2129 }
2130
2131 unless ($ssh2->auth_password($user, $passwd)) {
2132 my ($err_code, $err_name, $err_str) = $ssh2->error();
2133 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
2134 }
2135
2136 my $sftp = $ssh2->sftp();
2137 unless ($sftp) {
2138 my ($err_code, $err_name, $err_str) = $ssh2->error();
2139 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
2140 }
2141
2142 my $res = $sftp->mkdir('test dir');
2143 unless ($res) {
2144 my ($err_code, $err_name) = $sftp->error();
2145 die("FXP_MKDIR failed: [$err_name] ($err_code)");
2146 }
2147
2148 $ssh2->disconnect();
2149
2150 unless (-d $test_dir) {
2151 die("$test_dir directory does not exist as expected");
2152 }
2153 };
2154
2155 if ($@) {
2156 $ex = $@;
2157 }
2158
2159 $wfh->print("done\n");
2160 $wfh->flush();
2161
2162 } else {
2163 eval { server_wait($config_file, $rfh) };
2164 if ($@) {
2165 warn($@);
2166 exit 1;
2167 }
2168
2169 exit 0;
2170 }
2171
2172 # Stop server
2173 server_stop($pid_file);
2174
2175 $self->assert_child_ok($pid);
2176
2177 if ($ex) {
2178 test_append_logfile($log_file, $ex);
2179 unlink($log_file);
2180
2181 die($ex);
2182 }
2183
2184 unlink($log_file);
2185 }
2186
2187 sub sftp_rewrite_rmdir {
2188 my $self = shift;
2189 my $tmpdir = $self->{tmpdir};
2190
2191 my $config_file = "$tmpdir/sftp.conf";
2192 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
2193 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
2194
2195 my $log_file = test_get_logfile();
2196
2197 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
2198 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
2199
2200 my $user = 'proftpd';
2201 my $passwd = 'test';
2202 my $group = 'ftpd';
2203 my $home_dir = File::Spec->rel2abs($tmpdir);
2204 my $uid = 500;
2205 my $gid = 500;
2206
2207 my $test_dir = File::Spec->rel2abs("$tmpdir/test_dir");
2208 mkpath($test_dir);
2209
2210 # Make sure that, if we're running as root, that the home directory has
2211 # permissions/privs set for the account we create
2212 if ($< == 0) {
2213 unless (chmod(0755, $home_dir)) {
2214 die("Can't set perms on $home_dir to 0755: $!");
2215 }
2216
2217 unless (chown($uid, $gid, $home_dir)) {
2218 die("Can't set owner of $home_dir to $uid/$gid: $!");
2219 }
2220 }
2221
2222 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2223 '/bin/bash');
2224 auth_group_write($auth_group_file, $group, $gid, $user);
2225
2226 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
2227 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
2228
2229 my $config = {
2230 PidFile => $pid_file,
2231 ScoreboardFile => $scoreboard_file,
2232 SystemLog => $log_file,
2233 TraceLog => $log_file,
2234 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
2235
2236 AuthUserFile => $auth_user_file,
2237 AuthGroupFile => $auth_group_file,
2238 AuthOrder => 'mod_auth_file.c',
2239
2240 IfModules => {
2241 'mod_delay.c' => {
2242 DelayEngine => 'off',
2243 },
2244
2245 'mod_rewrite.c' => [
2246 'RewriteEngine on',
2247 "RewriteLog $log_file",
2248
2249 'RewriteMap replace int:replaceall',
2250 'RewriteCondition %m RMDIR',
2251 'RewriteRule ^(.*) "${replace:!$1! !_}"',
2252 ],
2253
2254 'mod_sftp.c' => [
2255 "SFTPEngine on",
2256 "SFTPLog $log_file",
2257 "SFTPHostKey $rsa_host_key",
2258 "SFTPHostKey $dsa_host_key",
2259 ],
2260 },
2261 };
2262
2263 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2264
2265 # Open pipes, for use between the parent and child processes. Specifically,
2266 # the child will indicate when it's done with its test by writing a message
2267 # to the parent.
2268 my ($rfh, $wfh);
2269 unless (pipe($rfh, $wfh)) {
2270 die("Can't open pipe: $!");
2271 }
2272
2273 require Net::SSH2;
2274
2275 my $ex;
2276
2277 # Fork child
2278 $self->handle_sigchld();
2279 defined(my $pid = fork()) or die("Can't fork: $!");
2280 if ($pid) {
2281 eval {
2282 my $ssh2 = Net::SSH2->new();
2283
2284 sleep(1);
2285
2286 unless ($ssh2->connect('127.0.0.1', $port)) {
2287 my ($err_code, $err_name, $err_str) = $ssh2->error();
2288 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2289 }
2290
2291 unless ($ssh2->auth_password($user, $passwd)) {
2292 my ($err_code, $err_name, $err_str) = $ssh2->error();
2293 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
2294 }
2295
2296 my $sftp = $ssh2->sftp();
2297 unless ($sftp) {
2298 my ($err_code, $err_name, $err_str) = $ssh2->error();
2299 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
2300 }
2301
2302 my $res = $sftp->rmdir('test dir');
2303 unless ($res) {
2304 my ($err_code, $err_name) = $sftp->error();
2305 die("FXP_RMDIR failed: [$err_name] ($err_code)");
2306 }
2307
2308 $ssh2->disconnect();
2309
2310 if (-d $test_dir) {
2311 die("$test_dir directory exists unexpectedly");
2312 }
2313 };
2314
2315 if ($@) {
2316 $ex = $@;
2317 }
2318
2319 $wfh->print("done\n");
2320 $wfh->flush();
2321
2322 } else {
2323 eval { server_wait($config_file, $rfh) };
2324 if ($@) {
2325 warn($@);
2326 exit 1;
2327 }
2328
2329 exit 0;
2330 }
2331
2332 # Stop server
2333 server_stop($pid_file);
2334
2335 $self->assert_child_ok($pid);
2336
2337 if ($ex) {
2338 test_append_logfile($log_file, $ex);
2339 unlink($log_file);
2340
2341 die($ex);
2342 }
2343
2344 unlink($log_file);
2345 }
2346
2347 sub sftp_rewrite_remove {
2348 my $self = shift;
2349 my $tmpdir = $self->{tmpdir};
2350
2351 my $config_file = "$tmpdir/sftp.conf";
2352 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
2353 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
2354
2355 my $log_file = test_get_logfile();
2356
2357 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
2358 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
2359
2360 my $user = 'proftpd';
2361 my $passwd = 'test';
2362 my $group = 'ftpd';
2363 my $home_dir = File::Spec->rel2abs($tmpdir);
2364 my $uid = 500;
2365 my $gid = 500;
2366
2367 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
2368 if (open(my $fh, "> $test_file")) {
2369 print $fh "ABCD" x 8192;
2370
2371 unless (close($fh)) {
2372 die("Can't write $test_file: $!");
2373 }
2374
2375 } else {
2376 die("Can't open $test_file: $!");
2377 }
2378
2379 # Make sure that, if we're running as root, that the home directory has
2380 # permissions/privs set for the account we create
2381 if ($< == 0) {
2382 unless (chmod(0755, $home_dir)) {
2383 die("Can't set perms on $home_dir to 0755: $!");
2384 }
2385
2386 unless (chown($uid, $gid, $home_dir)) {
2387 die("Can't set owner of $home_dir to $uid/$gid: $!");
2388 }
2389 }
2390
2391 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2392 '/bin/bash');
2393 auth_group_write($auth_group_file, $group, $gid, $user);
2394
2395 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
2396 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
2397
2398 my $config = {
2399 PidFile => $pid_file,
2400 ScoreboardFile => $scoreboard_file,
2401 SystemLog => $log_file,
2402 TraceLog => $log_file,
2403 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
2404
2405 AuthUserFile => $auth_user_file,
2406 AuthGroupFile => $auth_group_file,
2407 AuthOrder => 'mod_auth_file.c',
2408
2409 IfModules => {
2410 'mod_delay.c' => {
2411 DelayEngine => 'off',
2412 },
2413
2414 'mod_rewrite.c' => [
2415 'RewriteEngine on',
2416 "RewriteLog $log_file",
2417
2418 'RewriteMap replace int:replaceall',
2419 'RewriteCondition %m REMOVE',
2420 'RewriteRule ^(.*) "${replace:!$1! !_}"',
2421 ],
2422
2423 'mod_sftp.c' => [
2424 "SFTPEngine on",
2425 "SFTPLog $log_file",
2426 "SFTPHostKey $rsa_host_key",
2427 "SFTPHostKey $dsa_host_key",
2428 ],
2429 },
2430 };
2431
2432 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2433
2434 # Open pipes, for use between the parent and child processes. Specifically,
2435 # the child will indicate when it's done with its test by writing a message
2436 # to the parent.
2437 my ($rfh, $wfh);
2438 unless (pipe($rfh, $wfh)) {
2439 die("Can't open pipe: $!");
2440 }
2441
2442 require Net::SSH2;
2443
2444 my $ex;
2445
2446 # Fork child
2447 $self->handle_sigchld();
2448 defined(my $pid = fork()) or die("Can't fork: $!");
2449 if ($pid) {
2450 eval {
2451 my $ssh2 = Net::SSH2->new();
2452
2453 sleep(1);
2454
2455 unless ($ssh2->connect('127.0.0.1', $port)) {
2456 my ($err_code, $err_name, $err_str) = $ssh2->error();
2457 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2458 }
2459
2460 unless ($ssh2->auth_password($user, $passwd)) {
2461 my ($err_code, $err_name, $err_str) = $ssh2->error();
2462 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
2463 }
2464
2465 my $sftp = $ssh2->sftp();
2466 unless ($sftp) {
2467 my ($err_code, $err_name, $err_str) = $ssh2->error();
2468 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
2469 }
2470
2471 my $res = $sftp->unlink('test file.txt');
2472 unless ($res) {
2473 my ($err_code, $err_name) = $sftp->error();
2474 die("FXP_REMOVE failed: [$err_name] ($err_code)");
2475 }
2476
2477 $ssh2->disconnect();
2478
2479 if (-f $test_file) {
2480 die("$test_file file exists unexpectedly");
2481 }
2482 };
2483
2484 if ($@) {
2485 $ex = $@;
2486 }
2487
2488 $wfh->print("done\n");
2489 $wfh->flush();
2490
2491 } else {
2492 eval { server_wait($config_file, $rfh) };
2493 if ($@) {
2494 warn($@);
2495 exit 1;
2496 }
2497
2498 exit 0;
2499 }
2500
2501 # Stop server
2502 server_stop($pid_file);
2503
2504 $self->assert_child_ok($pid);
2505
2506 if ($ex) {
2507 test_append_logfile($log_file, $ex);
2508 unlink($log_file);
2509
2510 die($ex);
2511 }
2512
2513 unlink($log_file);
2514 }
2515
2516 sub sftp_rewrite_rename {
2517 my $self = shift;
2518 my $tmpdir = $self->{tmpdir};
2519
2520 my $config_file = "$tmpdir/sftp.conf";
2521 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
2522 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
2523
2524 my $log_file = test_get_logfile();
2525
2526 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
2527 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
2528
2529 my $user = 'proftpd';
2530 my $passwd = 'test';
2531 my $group = 'ftpd';
2532 my $home_dir = File::Spec->rel2abs($tmpdir);
2533 my $uid = 500;
2534 my $gid = 500;
2535
2536 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
2537 if (open(my $fh, "> $test_file")) {
2538 print $fh "ABCD" x 8192;
2539
2540 unless (close($fh)) {
2541 die("Can't write $test_file: $!");
2542 }
2543
2544 } else {
2545 die("Can't open $test_file: $!");
2546 }
2547
2548 my $test_file2 = File::Spec->rel2abs("$tmpdir/test_file2.txt");
2549
2550 # Make sure that, if we're running as root, that the home directory has
2551 # permissions/privs set for the account we create
2552 if ($< == 0) {
2553 unless (chmod(0755, $home_dir)) {
2554 die("Can't set perms on $home_dir to 0755: $!");
2555 }
2556
2557 unless (chown($uid, $gid, $home_dir)) {
2558 die("Can't set owner of $home_dir to $uid/$gid: $!");
2559 }
2560 }
2561
2562 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2563 '/bin/bash');
2564 auth_group_write($auth_group_file, $group, $gid, $user);
2565
2566 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
2567 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
2568
2569 my $config = {
2570 PidFile => $pid_file,
2571 ScoreboardFile => $scoreboard_file,
2572 SystemLog => $log_file,
2573 TraceLog => $log_file,
2574 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
2575
2576 AuthUserFile => $auth_user_file,
2577 AuthGroupFile => $auth_group_file,
2578 AuthOrder => 'mod_auth_file.c',
2579
2580 IfModules => {
2581 'mod_delay.c' => {
2582 DelayEngine => 'off',
2583 },
2584
2585 'mod_rewrite.c' => [
2586 'RewriteEngine on',
2587 "RewriteLog $log_file",
2588
2589 'RewriteMap replace int:replaceall',
2590 'RewriteCondition %m ^(RNFR|RNTO)$',
2591 'RewriteRule ^(.*)$ "${replace:!$1! !_}"',
2592 ],
2593
2594 'mod_sftp.c' => [
2595 "SFTPEngine on",
2596 "SFTPLog $log_file",
2597 "SFTPHostKey $rsa_host_key",
2598 "SFTPHostKey $dsa_host_key",
2599 ],
2600 },
2601 };
2602
2603 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2604
2605 # Open pipes, for use between the parent and child processes. Specifically,
2606 # the child will indicate when it's done with its test by writing a message
2607 # to the parent.
2608 my ($rfh, $wfh);
2609 unless (pipe($rfh, $wfh)) {
2610 die("Can't open pipe: $!");
2611 }
2612
2613 require Net::SSH2;
2614
2615 my $ex;
2616
2617 # Fork child
2618 $self->handle_sigchld();
2619 defined(my $pid = fork()) or die("Can't fork: $!");
2620 if ($pid) {
2621 eval {
2622 my $ssh2 = Net::SSH2->new();
2623
2624 sleep(1);
2625
2626 unless ($ssh2->connect('127.0.0.1', $port)) {
2627 my ($err_code, $err_name, $err_str) = $ssh2->error();
2628 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2629 }
2630
2631 unless ($ssh2->auth_password($user, $passwd)) {
2632 my ($err_code, $err_name, $err_str) = $ssh2->error();
2633 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
2634 }
2635
2636 my $sftp = $ssh2->sftp();
2637 unless ($sftp) {
2638 my ($err_code, $err_name, $err_str) = $ssh2->error();
2639 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
2640 }
2641
2642 my $res = $sftp->rename('test file.txt', 'test file2.txt');
2643 unless ($res) {
2644 my ($err_code, $err_name) = $sftp->error();
2645 die("FXP_RENAME failed: [$err_name] ($err_code)");
2646 }
2647
2648 $ssh2->disconnect();
2649
2650 if (-f $test_file) {
2651 die("$test_file file exists unexpectedly");
2652 }
2653
2654 unless (-f $test_file2) {
2655 die("$test_file2 file does not exist as expected");
2656 }
2657 };
2658
2659 if ($@) {
2660 $ex = $@;
2661 }
2662
2663 $wfh->print("done\n");
2664 $wfh->flush();
2665
2666 } else {
2667 eval { server_wait($config_file, $rfh) };
2668 if ($@) {
2669 warn($@);
2670 exit 1;
2671 }
2672
2673 exit 0;
2674 }
2675
2676 # Stop server
2677 server_stop($pid_file);
2678
2679 $self->assert_child_ok($pid);
2680
2681 if ($ex) {
2682 test_append_logfile($log_file, $ex);
2683 unlink($log_file);
2684
2685 die($ex);
2686 }
2687
2688 unlink($log_file);
2689 }
2690
2691 sub sftp_rewrite_rename_file_var_w_bug3643 {
2692 my $self = shift;
2693 my $tmpdir = $self->{tmpdir};
2694
2695 my $config_file = "$tmpdir/sftp.conf";
2696 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
2697 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
2698
2699 my $log_file = test_get_logfile();
2700
2701 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
2702 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
2703
2704 my $user = 'proftpd';
2705 my $passwd = 'test';
2706 my $group = 'ftpd';
2707 my $home_dir = File::Spec->rel2abs($tmpdir);
2708 my $uid = 500;
2709 my $gid = 500;
2710
2711 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
2712 if (open(my $fh, "> $test_file")) {
2713 print $fh "ABCD" x 8192;
2714
2715 unless (close($fh)) {
2716 die("Can't write $test_file: $!");
2717 }
2718
2719 } else {
2720 die("Can't open $test_file: $!");
2721 }
2722
2723 my $test_file2 = File::Spec->rel2abs("$tmpdir/test_file2.txt");
2724
2725 # Make sure that, if we're running as root, that the home directory has
2726 # permissions/privs set for the account we create
2727 if ($< == 0) {
2728 unless (chmod(0755, $home_dir)) {
2729 die("Can't set perms on $home_dir to 0755: $!");
2730 }
2731
2732 unless (chown($uid, $gid, $home_dir)) {
2733 die("Can't set owner of $home_dir to $uid/$gid: $!");
2734 }
2735 }
2736
2737 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2738 '/bin/bash');
2739 auth_group_write($auth_group_file, $group, $gid, $user);
2740
2741 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
2742 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
2743
2744 my $config = {
2745 PidFile => $pid_file,
2746 ScoreboardFile => $scoreboard_file,
2747 SystemLog => $log_file,
2748 TraceLog => $log_file,
2749 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
2750
2751 AuthUserFile => $auth_user_file,
2752 AuthGroupFile => $auth_group_file,
2753 AuthOrder => 'mod_auth_file.c',
2754
2755 IfModules => {
2756 'mod_delay.c' => {
2757 DelayEngine => 'off',
2758 },
2759
2760 'mod_rewrite.c' => [
2761 'RewriteEngine on',
2762 "RewriteLog $log_file",
2763
2764 'RewriteMap lowercase int:tolower',
2765 'RewriteCondition %m ^RNTO$',
2766 'RewriteCondition %w -f',
2767 'RewriteRule ^(.*) ${lowercase:$0}',
2768 ],
2769
2770 'mod_sftp.c' => [
2771 "SFTPEngine on",
2772 "SFTPLog $log_file",
2773 "SFTPHostKey $rsa_host_key",
2774 "SFTPHostKey $dsa_host_key",
2775 ],
2776 },
2777 };
2778
2779 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2780
2781 # Open pipes, for use between the parent and child processes. Specifically,
2782 # the child will indicate when it's done with its test by writing a message
2783 # to the parent.
2784 my ($rfh, $wfh);
2785 unless (pipe($rfh, $wfh)) {
2786 die("Can't open pipe: $!");
2787 }
2788
2789 require Net::SSH2;
2790
2791 my $ex;
2792
2793 # Fork child
2794 $self->handle_sigchld();
2795 defined(my $pid = fork()) or die("Can't fork: $!");
2796 if ($pid) {
2797 eval {
2798 my $ssh2 = Net::SSH2->new();
2799
2800 sleep(1);
2801
2802 unless ($ssh2->connect('127.0.0.1', $port)) {
2803 my ($err_code, $err_name, $err_str) = $ssh2->error();
2804 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2805 }
2806
2807 unless ($ssh2->auth_password($user, $passwd)) {
2808 my ($err_code, $err_name, $err_str) = $ssh2->error();
2809 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
2810 }
2811
2812 my $sftp = $ssh2->sftp();
2813 unless ($sftp) {
2814 my ($err_code, $err_name, $err_str) = $ssh2->error();
2815 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
2816 }
2817
2818 my $res = $sftp->rename('test_file.txt', 'TeST_FiLe2.TxT');
2819 unless ($res) {
2820 my ($err_code, $err_name) = $sftp->error();
2821 die("FXP_RENAME failed: [$err_name] ($err_code)");
2822 }
2823
2824 $ssh2->disconnect();
2825
2826 if (-f $test_file) {
2827 die("$test_file file exists unexpectedly");
2828 }
2829
2830 unless (-f $test_file2) {
2831 die("$test_file2 file does not exist as expected");
2832 }
2833 };
2834
2835 if ($@) {
2836 $ex = $@;
2837 }
2838
2839 $wfh->print("done\n");
2840 $wfh->flush();
2841
2842 } else {
2843 eval { server_wait($config_file, $rfh) };
2844 if ($@) {
2845 warn($@);
2846 exit 1;
2847 }
2848
2849 exit 0;
2850 }
2851
2852 # Stop server
2853 server_stop($pid_file);
2854
2855 $self->assert_child_ok($pid);
2856
2857 if ($ex) {
2858 test_append_logfile($log_file, $ex);
2859 unlink($log_file);
2860
2861 die($ex);
2862 }
2863
2864 unlink($log_file);
2865 }
2866
2867 sub sftp_rewrite_rename_dir_var_w_bug3643 {
2868 my $self = shift;
2869 my $tmpdir = $self->{tmpdir};
2870
2871 my $config_file = "$tmpdir/sftp.conf";
2872 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
2873 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
2874
2875 my $log_file = test_get_logfile();
2876
2877 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
2878 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
2879
2880 my $user = 'proftpd';
2881 my $passwd = 'test';
2882 my $group = 'ftpd';
2883 my $home_dir = File::Spec->rel2abs($tmpdir);
2884 my $uid = 500;
2885 my $gid = 500;
2886
2887 my $src_dir = File::Spec->rel2abs("$tmpdir/foo.d");
2888 mkpath($src_dir);
2889
2890 my $dst_dir = File::Spec->rel2abs("$tmpdir/bar.d");
2891
2892 # Make sure that, if we're running as root, that the home directory has
2893 # permissions/privs set for the account we create
2894 if ($< == 0) {
2895 unless (chmod(0755, $home_dir)) {
2896 die("Can't set perms on $home_dir to 0755: $!");
2897 }
2898
2899 unless (chown($uid, $gid, $home_dir)) {
2900 die("Can't set owner of $home_dir to $uid/$gid: $!");
2901 }
2902 }
2903
2904 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2905 '/bin/bash');
2906 auth_group_write($auth_group_file, $group, $gid, $user);
2907
2908 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
2909 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
2910
2911 my $config = {
2912 PidFile => $pid_file,
2913 ScoreboardFile => $scoreboard_file,
2914 SystemLog => $log_file,
2915 TraceLog => $log_file,
2916 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
2917
2918 AuthUserFile => $auth_user_file,
2919 AuthGroupFile => $auth_group_file,
2920 AuthOrder => 'mod_auth_file.c',
2921
2922 IfModules => {
2923 'mod_delay.c' => {
2924 DelayEngine => 'off',
2925 },
2926
2927 'mod_rewrite.c' => [
2928 'RewriteEngine on',
2929 "RewriteLog $log_file",
2930
2931 'RewriteMap lowercase int:tolower',
2932 'RewriteCondition %m ^RNTO$',
2933 'RewriteCondition %w -d',
2934 'RewriteRule ^(.*) ${lowercase:$0}',
2935 ],
2936
2937 'mod_sftp.c' => [
2938 "SFTPEngine on",
2939 "SFTPLog $log_file",
2940 "SFTPHostKey $rsa_host_key",
2941 "SFTPHostKey $dsa_host_key",
2942 ],
2943 },
2944 };
2945
2946 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2947
2948 # Open pipes, for use between the parent and child processes. Specifically,
2949 # the child will indicate when it's done with its test by writing a message
2950 # to the parent.
2951 my ($rfh, $wfh);
2952 unless (pipe($rfh, $wfh)) {
2953 die("Can't open pipe: $!");
2954 }
2955
2956 require Net::SSH2;
2957
2958 my $ex;
2959
2960 # Fork child
2961 $self->handle_sigchld();
2962 defined(my $pid = fork()) or die("Can't fork: $!");
2963 if ($pid) {
2964 eval {
2965 my $ssh2 = Net::SSH2->new();
2966
2967 sleep(1);
2968
2969 unless ($ssh2->connect('127.0.0.1', $port)) {
2970 my ($err_code, $err_name, $err_str) = $ssh2->error();
2971 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2972 }
2973
2974 unless ($ssh2->auth_password($user, $passwd)) {
2975 my ($err_code, $err_name, $err_str) = $ssh2->error();
2976 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
2977 }
2978
2979 my $sftp = $ssh2->sftp();
2980 unless ($sftp) {
2981 my ($err_code, $err_name, $err_str) = $ssh2->error();
2982 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
2983 }
2984
2985 my $res = $sftp->rename('foo.d', 'BaR.D');
2986 unless ($res) {
2987 my ($err_code, $err_name) = $sftp->error();
2988 die("FXP_RENAME failed: [$err_name] ($err_code)");
2989 }
2990
2991 $ssh2->disconnect();
2992
2993 if (-d $src_dir) {
2994 die("$src_dir directory exists unexpectedly");
2995 }
2996
2997 unless (-d $dst_dir) {
2998 die("$dst_dir directory does not exist as expected");
2999 }
3000 };
3001
3002 if ($@) {
3003 $ex = $@;
3004 }
3005
3006 $wfh->print("done\n");
3007 $wfh->flush();
3008
3009 } else {
3010 eval { server_wait($config_file, $rfh) };
3011 if ($@) {
3012 warn($@);
3013 exit 1;
3014 }
3015
3016 exit 0;
3017 }
3018
3019 # Stop server
3020 server_stop($pid_file);
3021
3022 $self->assert_child_ok($pid);
3023
3024 if ($ex) {
3025 test_append_logfile($log_file, $ex);
3026 unlink($log_file);
3027
3028 die($ex);
3029 }
3030
3031 unlink($log_file);
3032 }
3033
3034 sub sftp_rewrite_symlink {
3035 my $self = shift;
3036 my $tmpdir = $self->{tmpdir};
3037
3038 my $config_file = "$tmpdir/sftp.conf";
3039 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
3040 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
3041
3042 my $log_file = test_get_logfile();
3043
3044 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
3045 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
3046
3047 my $user = 'proftpd';
3048 my $passwd = 'test';
3049 my $group = 'ftpd';
3050 my $home_dir = File::Spec->rel2abs($tmpdir);
3051 my $uid = 500;
3052 my $gid = 500;
3053
3054 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
3055 if (open(my $fh, "> $test_file")) {
3056 print $fh "ABCD" x 8192;
3057
3058 unless (close($fh)) {
3059 die("Can't write $test_file: $!");
3060 }
3061
3062 } else {
3063 die("Can't open $test_file: $!");
3064 }
3065
3066 my $test_symlink = File::Spec->rel2abs("$tmpdir/test_link");
3067
3068 # Make sure that, if we're running as root, that the home directory has
3069 # permissions/privs set for the account we create
3070 if ($< == 0) {
3071 unless (chmod(0755, $home_dir)) {
3072 die("Can't set perms on $home_dir to 0755: $!");
3073 }
3074
3075 unless (chown($uid, $gid, $home_dir)) {
3076 die("Can't set owner of $home_dir to $uid/$gid: $!");
3077 }
3078 }
3079
3080 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3081 '/bin/bash');
3082 auth_group_write($auth_group_file, $group, $gid, $user);
3083
3084 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
3085 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
3086
3087 my $config = {
3088 PidFile => $pid_file,
3089 ScoreboardFile => $scoreboard_file,
3090 SystemLog => $log_file,
3091 TraceLog => $log_file,
3092 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
3093
3094 AuthUserFile => $auth_user_file,
3095 AuthGroupFile => $auth_group_file,
3096 AuthOrder => 'mod_auth_file.c',
3097
3098 IfModules => {
3099 'mod_delay.c' => {
3100 DelayEngine => 'off',
3101 },
3102
3103 'mod_rewrite.c' => [
3104 'RewriteEngine on',
3105 "RewriteLog $log_file",
3106
3107 'RewriteMap replace int:replaceall',
3108 'RewriteCondition %m SYMLINK',
3109 'RewriteRule ^(.*)$ "${replace:!$1! !_}"',
3110 ],
3111
3112 'mod_sftp.c' => [
3113 "SFTPEngine on",
3114 "SFTPLog $log_file",
3115 "SFTPHostKey $rsa_host_key",
3116 "SFTPHostKey $dsa_host_key",
3117 ],
3118 },
3119 };
3120
3121 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3122
3123 # Open pipes, for use between the parent and child processes. Specifically,
3124 # the child will indicate when it's done with its test by writing a message
3125 # to the parent.
3126 my ($rfh, $wfh);
3127 unless (pipe($rfh, $wfh)) {
3128 die("Can't open pipe: $!");
3129 }
3130
3131 require Net::SSH2;
3132
3133 my $ex;
3134
3135 # Fork child
3136 $self->handle_sigchld();
3137 defined(my $pid = fork()) or die("Can't fork: $!");
3138 if ($pid) {
3139 eval {
3140 my $ssh2 = Net::SSH2->new();
3141
3142 sleep(1);
3143
3144 unless ($ssh2->connect('127.0.0.1', $port)) {
3145 my ($err_code, $err_name, $err_str) = $ssh2->error();
3146 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
3147 }
3148
3149 unless ($ssh2->auth_password($user, $passwd)) {
3150 my ($err_code, $err_name, $err_str) = $ssh2->error();
3151 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
3152 }
3153
3154 my $sftp = $ssh2->sftp();
3155 unless ($sftp) {
3156 my ($err_code, $err_name, $err_str) = $ssh2->error();
3157 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
3158 }
3159
3160 my $res = $sftp->symlink('test file.txt', 'test link');
3161 unless ($res) {
3162 my ($err_code, $err_name) = $sftp->error();
3163 die("FXP_SYMLINK failed: [$err_name] ($err_code)");
3164 }
3165
3166 # To close the SFTP channel, we have to explicitly destroy the object
3167 $sftp = undef;
3168
3169 $ssh2->disconnect();
3170
3171 unless (-l $test_symlink) {
3172 die("$test_symlink symlink does not exist as expected");
3173 }
3174 };
3175
3176 if ($@) {
3177 $ex = $@;
3178 }
3179
3180 $wfh->print("done\n");
3181 $wfh->flush();
3182
3183 } else {
3184 eval { server_wait($config_file, $rfh) };
3185 if ($@) {
3186 warn($@);
3187 exit 1;
3188 }
3189
3190 exit 0;
3191 }
3192
3193 # Stop server
3194 server_stop($pid_file);
3195
3196 $self->assert_child_ok($pid);
3197
3198 if ($ex) {
3199 test_append_logfile($log_file, $ex);
3200 unlink($log_file);
3201
3202 die($ex);
3203 }
3204
3205 unlink($log_file);
3206 }
3207
3208 sub sftp_rewrite_readlink {
3209 my $self = shift;
3210 my $tmpdir = $self->{tmpdir};
3211
3212 my $config_file = "$tmpdir/sftp.conf";
3213 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
3214 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
3215
3216 my $log_file = test_get_logfile();
3217
3218 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
3219 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
3220
3221 my $user = 'proftpd';
3222 my $passwd = 'test';
3223 my $group = 'ftpd';
3224 my $home_dir = File::Spec->rel2abs($tmpdir);
3225 my $uid = 500;
3226 my $gid = 500;
3227
3228 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
3229 if (open(my $fh, "> $test_file")) {
3230 print $fh "ABCD" x 8192;
3231
3232 unless (close($fh)) {
3233 die("Can't write $test_file: $!");
3234 }
3235
3236 } else {
3237 die("Can't open $test_file: $!");
3238 }
3239
3240 my $test_symlink = File::Spec->rel2abs("$tmpdir/test_link");
3241 unless (symlink($test_file, $test_symlink)) {
3242 die("Can't symlink $test_symlink to $test_file: $!");
3243 }
3244
3245 # Make sure that, if we're running as root, that the home directory has
3246 # permissions/privs set for the account we create
3247 if ($< == 0) {
3248 unless (chmod(0755, $home_dir)) {
3249 die("Can't set perms on $home_dir to 0755: $!");
3250 }
3251
3252 unless (chown($uid, $gid, $home_dir)) {
3253 die("Can't set owner of $home_dir to $uid/$gid: $!");
3254 }
3255 }
3256
3257 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3258 '/bin/bash');
3259 auth_group_write($auth_group_file, $group, $gid, $user);
3260
3261 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
3262 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
3263
3264 my $config = {
3265 PidFile => $pid_file,
3266 ScoreboardFile => $scoreboard_file,
3267 SystemLog => $log_file,
3268 TraceLog => $log_file,
3269 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
3270
3271 AuthUserFile => $auth_user_file,
3272 AuthGroupFile => $auth_group_file,
3273 AuthOrder => 'mod_auth_file.c',
3274
3275 IfModules => {
3276 'mod_delay.c' => {
3277 DelayEngine => 'off',
3278 },
3279
3280 'mod_rewrite.c' => [
3281 'RewriteEngine on',
3282 "RewriteLog $log_file",
3283
3284 'RewriteMap replace int:replaceall',
3285 'RewriteCondition %m READLINK',
3286 'RewriteRule ^(.*)$ "${replace:!$1! !_}"',
3287 ],
3288
3289 'mod_sftp.c' => [
3290 "SFTPEngine on",
3291 "SFTPLog $log_file",
3292 "SFTPHostKey $rsa_host_key",
3293 "SFTPHostKey $dsa_host_key",
3294 ],
3295 },
3296 };
3297
3298 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3299
3300 # Open pipes, for use between the parent and child processes. Specifically,
3301 # the child will indicate when it's done with its test by writing a message
3302 # to the parent.
3303 my ($rfh, $wfh);
3304 unless (pipe($rfh, $wfh)) {
3305 die("Can't open pipe: $!");
3306 }
3307
3308 require Net::SSH2;
3309
3310 my $ex;
3311
3312 # Fork child
3313 $self->handle_sigchld();
3314 defined(my $pid = fork()) or die("Can't fork: $!");
3315 if ($pid) {
3316 eval {
3317 my $ssh2 = Net::SSH2->new();
3318
3319 sleep(1);
3320
3321 unless ($ssh2->connect('127.0.0.1', $port)) {
3322 my ($err_code, $err_name, $err_str) = $ssh2->error();
3323 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
3324 }
3325
3326 unless ($ssh2->auth_password($user, $passwd)) {
3327 my ($err_code, $err_name, $err_str) = $ssh2->error();
3328 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
3329 }
3330
3331 my $sftp = $ssh2->sftp();
3332 unless ($sftp) {
3333 my ($err_code, $err_name, $err_str) = $ssh2->error();
3334 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
3335 }
3336
3337 my $path = $sftp->readlink('test link');
3338 unless ($path) {
3339 my ($err_code, $err_name) = $sftp->error();
3340 die("FXP_READLINK failed: [$err_name] ($err_code)");
3341 }
3342
3343 $ssh2->disconnect();
3344
3345 $self->assert($test_file eq $path,
3346 test_msg("Expected '$test_file', got '$path'"));
3347 };
3348
3349 if ($@) {
3350 $ex = $@;
3351 }
3352
3353 $wfh->print("done\n");
3354 $wfh->flush();
3355
3356 } else {
3357 eval { server_wait($config_file, $rfh) };
3358 if ($@) {
3359 warn($@);
3360 exit 1;
3361 }
3362
3363 exit 0;
3364 }
3365
3366 # Stop server
3367 server_stop($pid_file);
3368
3369 $self->assert_child_ok($pid);
3370
3371 if ($ex) {
3372 test_append_logfile($log_file, $ex);
3373 unlink($log_file);
3374
3375 die($ex);
3376 }
3377
3378 unlink($log_file);
3379 }
3380
3381 sub sftp_rewrite_homedir {
3382 my $self = shift;
3383 my $tmpdir = $self->{tmpdir};
3384
3385 my $config_file = "$tmpdir/sftp.conf";
3386 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
3387 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
3388
3389 my $log_file = test_get_logfile();
3390
3391 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
3392 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
3393
3394 my $user = 'proftpd';
3395 my $passwd = 'test';
3396 my $group = 'ftpd';
3397 my $home_dir = File::Spec->rel2abs("$tmpdir/$user");
3398 mkpath($home_dir);
3399 my $uid = 500;
3400 my $gid = 500;
3401
3402 my $abs_tmpdir = File::Spec->rel2abs($tmpdir);
3403
3404 # Make sure that, if we're running as root, that the home directory has
3405 # permissions/privs set for the account we create
3406 if ($< == 0) {
3407 unless (chmod(0755, $home_dir)) {
3408 die("Can't set perms on $home_dir to 0755: $!");
3409 }
3410
3411 unless (chown($uid, $gid, $home_dir)) {
3412 die("Can't set owner of $home_dir to $uid/$gid: $!");
3413 }
3414 }
3415
3416 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $abs_tmpdir,
3417 '/bin/bash');
3418 auth_group_write($auth_group_file, $group, $gid, $user);
3419
3420 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
3421 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
3422
3423 my $config = {
3424 PidFile => $pid_file,
3425 ScoreboardFile => $scoreboard_file,
3426 SystemLog => $log_file,
3427 TraceLog => $log_file,
3428 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
3429
3430 AuthUserFile => $auth_user_file,
3431 AuthGroupFile => $auth_group_file,
3432 AuthOrder => 'mod_auth_file.c',
3433
3434 IfModules => {
3435 'mod_delay.c' => {
3436 DelayEngine => 'off',
3437 },
3438
3439 'mod_rewrite.c' => [
3440 'RewriteEngine on',
3441 "RewriteLog $log_file",
3442 'RewriteHome on',
3443 'RewriteCondition %m REWRITE_HOME',
3444 "RewriteRule ^(.*)\$ \$1/%u",
3445 ],
3446
3447 'mod_sftp.c' => [
3448 "SFTPEngine on",
3449 "SFTPLog $log_file",
3450 "SFTPHostKey $rsa_host_key",
3451 "SFTPHostKey $dsa_host_key",
3452 ],
3453 },
3454 };
3455
3456 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3457
3458 # Open pipes, for use between the parent and child processes. Specifically,
3459 # the child will indicate when it's done with its test by writing a message
3460 # to the parent.
3461 my ($rfh, $wfh);
3462 unless (pipe($rfh, $wfh)) {
3463 die("Can't open pipe: $!");
3464 }
3465
3466 require Net::SSH2;
3467
3468 my $ex;
3469
3470 # Fork child
3471 $self->handle_sigchld();
3472 defined(my $pid = fork()) or die("Can't fork: $!");
3473 if ($pid) {
3474 eval {
3475 my $ssh2 = Net::SSH2->new();
3476
3477 sleep(1);
3478
3479 unless ($ssh2->connect('127.0.0.1', $port)) {
3480 my ($err_code, $err_name, $err_str) = $ssh2->error();
3481 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
3482 }
3483
3484 unless ($ssh2->auth_password($user, $passwd)) {
3485 my ($err_code, $err_name, $err_str) = $ssh2->error();
3486 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
3487 }
3488
3489 my $sftp = $ssh2->sftp();
3490 unless ($sftp) {
3491 my ($err_code, $err_name, $err_str) = $ssh2->error();
3492 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
3493 }
3494
3495 my $cwd = $sftp->realpath('.');
3496 unless ($cwd) {
3497 my ($err_code, $err_name) = $sftp->error();
3498 die("FXP_REALPATH failed: [$err_name] ($err_code)");
3499 }
3500
3501 $sftp = undef;
3502 $ssh2->disconnect();
3503
3504 my $expected = $home_dir;
3505 if ($^O eq 'darwin') {
3506 # Mac OSX hack
3507 $expected = '/private' . $expected;
3508 }
3509
3510 $self->assert($expected eq $cwd,
3511 test_msg("Expected '$home_dir', got '$cwd'"));
3512 };
3513
3514 if ($@) {
3515 $ex = $@;
3516 }
3517
3518 $wfh->print("done\n");
3519 $wfh->flush();
3520
3521 } else {
3522 eval { server_wait($config_file, $rfh) };
3523 if ($@) {
3524 warn($@);
3525 exit 1;
3526 }
3527
3528 exit 0;
3529 }
3530
3531 # Stop server
3532 server_stop($pid_file);
3533
3534 $self->assert_child_ok($pid);
3535
3536 if ($ex) {
3537 test_append_logfile($log_file, $ex);
3538 unlink($log_file);
3539
3540 die($ex);
3541 }
3542
3543 unlink($log_file);
3544 }
3545
3546 sub scp_rewrite_upload {
3547 my $self = shift;
3548 my $tmpdir = $self->{tmpdir};
3549
3550 my $config_file = "$tmpdir/sftp.conf";
3551 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
3552 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
3553
3554 my $log_file = test_get_logfile();
3555
3556 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
3557 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
3558
3559 my $user = 'proftpd';
3560 my $passwd = 'test';
3561 my $group = 'ftpd';
3562 my $home_dir = File::Spec->rel2abs($tmpdir);
3563 my $uid = 500;
3564 my $gid = 500;
3565
3566 # Make sure that, if we're running as root, that the home directory has
3567 # permissions/privs set for the account we create
3568 if ($< == 0) {
3569 unless (chmod(0755, $home_dir)) {
3570 die("Can't set perms on $home_dir to 0755: $!");
3571 }
3572
3573 unless (chown($uid, $gid, $home_dir)) {
3574 die("Can't set owner of $home_dir to $uid/$gid: $!");
3575 }
3576 }
3577
3578 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3579 '/bin/bash');
3580 auth_group_write($auth_group_file, $group, $gid, $user);
3581
3582 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
3583 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
3584
3585 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
3586
3587 my $config = {
3588 PidFile => $pid_file,
3589 ScoreboardFile => $scoreboard_file,
3590 SystemLog => $log_file,
3591 TraceLog => $log_file,
3592 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
3593
3594 AuthUserFile => $auth_user_file,
3595 AuthGroupFile => $auth_group_file,
3596 AuthOrder => 'mod_auth_file.c',
3597
3598 IfModules => {
3599 'mod_delay.c' => {
3600 DelayEngine => 'off',
3601 },
3602
3603 'mod_rewrite.c' => [
3604 'RewriteEngine on',
3605 "RewriteLog $log_file",
3606
3607 'RewriteMap replace int:replaceall',
3608 'RewriteCondition %m STOR',
3609 'RewriteRule ^(.*)$ "${replace:!$1!&!_}"',
3610 ],
3611
3612 'mod_sftp.c' => [
3613 "SFTPEngine on",
3614 "SFTPLog $log_file",
3615 "SFTPHostKey $rsa_host_key",
3616 "SFTPHostKey $dsa_host_key",
3617 ],
3618 },
3619 };
3620
3621 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3622
3623 # Open pipes, for use between the parent and child processes. Specifically,
3624 # the child will indicate when it's done with its test by writing a message
3625 # to the parent.
3626 my ($rfh, $wfh);
3627 unless (pipe($rfh, $wfh)) {
3628 die("Can't open pipe: $!");
3629 }
3630
3631 require Net::SSH2;
3632
3633 my $ex;
3634
3635 # Ignore SIGPIPE
3636 local $SIG{PIPE} = sub { };
3637
3638 # Fork child
3639 $self->handle_sigchld();
3640 defined(my $pid = fork()) or die("Can't fork: $!");
3641 if ($pid) {
3642 eval {
3643 my $ssh2 = Net::SSH2->new();
3644
3645 sleep(1);
3646
3647 unless ($ssh2->connect('127.0.0.1', $port)) {
3648 my ($err_code, $err_name, $err_str) = $ssh2->error();
3649 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
3650 }
3651
3652 unless ($ssh2->auth_password($user, $passwd)) {
3653 my ($err_code, $err_name, $err_str) = $ssh2->error();
3654 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
3655 }
3656
3657 my $res = $ssh2->scp_put($config_file, 'test&file.txt');
3658 unless ($res) {
3659 my ($err_code, $err_name, $err_str) = $ssh2->error();
3660 die("Can't upload $config_file to server: [$err_name] ($err_code) $err_str");
3661 }
3662
3663 $ssh2->disconnect();
3664
3665 unless (-f $test_file) {
3666 die("$test_file file does not exist as expected");
3667 }
3668 };
3669
3670 if ($@) {
3671 $ex = $@;
3672 }
3673
3674 $wfh->print("done\n");
3675 $wfh->flush();
3676
3677 } else {
3678 eval { server_wait($config_file, $rfh) };
3679 if ($@) {
3680 warn($@);
3681 exit 1;
3682 }
3683
3684 exit 0;
3685 }
3686
3687 # Stop server
3688 server_stop($pid_file);
3689
3690 $self->assert_child_ok($pid);
3691
3692 if ($ex) {
3693 test_append_logfile($log_file, $ex);
3694 unlink($log_file);
3695
3696 die($ex);
3697 }
3698
3699 unlink($log_file);
3700 }
3701
3702 sub scp_rewrite_download {
3703 my $self = shift;
3704 my $tmpdir = $self->{tmpdir};
3705
3706 my $config_file = "$tmpdir/sftp.conf";
3707 my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
3708 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
3709
3710 my $log_file = test_get_logfile();
3711
3712 my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
3713 my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
3714
3715 my $user = 'proftpd';
3716 my $passwd = 'test';
3717 my $group = 'ftpd';
3718 my $home_dir = File::Spec->rel2abs($tmpdir);
3719 my $uid = 500;
3720 my $gid = 500;
3721
3722 # Make sure that, if we're running as root, that the home directory has
3723 # permissions/privs set for the account we create
3724 if ($< == 0) {
3725 unless (chmod(0755, $home_dir)) {
3726 die("Can't set perms on $home_dir to 0755: $!");
3727 }
3728
3729 unless (chown($uid, $gid, $home_dir)) {
3730 die("Can't set owner of $home_dir to $uid/$gid: $!");
3731 }
3732 }
3733
3734 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3735 '/bin/bash');
3736 auth_group_write($auth_group_file, $group, $gid, $user);
3737
3738 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
3739 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
3740
3741 my $test_file = File::Spec->rel2abs("$tmpdir/test_file.txt");
3742 if (open(my $fh, "> $test_file")) {
3743 print $fh "Hello, World!\n";
3744 unless (close($fh)) {
3745 die("Can't write $test_file: $!");
3746 }
3747
3748 } else {
3749 die("Can't open $test_file: $!");
3750 }
3751
3752 my $output_file = File::Spec->rel2abs("$tmpdir/retrieved.txt");
3753
3754 my $config = {
3755 PidFile => $pid_file,
3756 ScoreboardFile => $scoreboard_file,
3757 SystemLog => $log_file,
3758 TraceLog => $log_file,
3759 Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
3760
3761 AuthUserFile => $auth_user_file,
3762 AuthGroupFile => $auth_group_file,
3763 AuthOrder => 'mod_auth_file.c',
3764
3765 IfModules => {
3766 'mod_delay.c' => {
3767 DelayEngine => 'off',
3768 },
3769
3770 'mod_rewrite.c' => [
3771 'RewriteEngine on',
3772 "RewriteLog $log_file",
3773
3774 'RewriteMap replace int:replaceall',
3775 'RewriteCondition %m RETR',
3776 'RewriteCondition %F &',
3777 'RewriteRule ^(.*)$ "${replace:!$1!&!_}"',
3778 ],
3779
3780 'mod_sftp.c' => [
3781 "SFTPEngine on",
3782 "SFTPLog $log_file",
3783 "SFTPHostKey $rsa_host_key",
3784 "SFTPHostKey $dsa_host_key",
3785 ],
3786 },
3787 };
3788
3789 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3790
3791 # Open pipes, for use between the parent and child processes. Specifically,
3792 # the child will indicate when it's done with its test by writing a message
3793 # to the parent.
3794 my ($rfh, $wfh);
3795 unless (pipe($rfh, $wfh)) {
3796 die("Can't open pipe: $!");
3797 }
3798
3799 require Net::SSH2;
3800
3801 my $ex;
3802
3803 # Ignore SIGPIPE
3804 local $SIG{PIPE} = sub { };
3805
3806 # Fork child
3807 $self->handle_sigchld();
3808 defined(my $pid = fork()) or die("Can't fork: $!");
3809 if ($pid) {
3810 eval {
3811 my $ssh2 = Net::SSH2->new();
3812
3813 sleep(1);
3814
3815 unless ($ssh2->connect('127.0.0.1', $port)) {
3816 my ($err_code, $err_name, $err_str) = $ssh2->error();
3817 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
3818 }
3819
3820 unless ($ssh2->auth_password($user, $passwd)) {
3821 my ($err_code, $err_name, $err_str) = $ssh2->error();
3822 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
3823 }
3824
3825 my $res = $ssh2->scp_get('test&file.txt', $output_file);
3826 unless ($res) {
3827 my ($err_code, $err_name, $err_str) = $ssh2->error();
3828 die("Can't download test&file.txt from server: [$err_name] ($err_code) $err_str");
3829 }
3830
3831 $ssh2->disconnect();
3832
3833 unless (-f $output_file) {
3834 die("$output_file file does not exist as expected");
3835 }
3836 };
3837
3838 if ($@) {
3839 $ex = $@;
3840 }
3841
3842 $wfh->print("done\n");
3843 $wfh->flush();
3844
3845 } else {
3846 eval { server_wait($config_file, $rfh) };
3847 if ($@) {
3848 warn($@);
3849 exit 1;
3850 }
3851
3852 exit 0;
3853 }
3854
3855 # Stop server
3856 server_stop($pid_file);
3857
3858 $self->assert_child_ok($pid);
3859
3860 if ($ex) {
3861 test_append_logfile($log_file, $ex);
3862 unlink($log_file);
3863
3864 die($ex);
3865 }
3866
3867 unlink($log_file);
3868 }
3869
3870 1;