Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_sftp::exec;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Copy;
8 use File::Spec;
9 use IO::Handle;
10 use POSIX qw(:fcntl_h);
11
12 use ProFTPD::TestSuite::FTP;
13 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
14
15 $| = 1;
16
17 my $order = 0;
18
19 my $TESTS = {
20 sftp_exec_on_connect => {
21 order => ++$order,
22 test_class => [qw(forking)],
23 },
24
25 sftp_exec_on_cmd => {
26 order => ++$order,
27 test_class => [qw(forking)],
28 },
29
30 sftp_exec_on_cmd_var_total_bytes_xfer => {
31 order => ++$order,
32 test_class => [qw(forking)],
33 },
34
35 sftp_exec_on_cmd_var_bytes_xfer => {
36 order => ++$order,
37 test_class => [qw(forking)],
38 },
39
40 sftp_exec_on_exit => {
41 order => ++$order,
42 test_class => [qw(forking)],
43 },
44
45 sftp_exec_on_error => {
46 order => ++$order,
47 test_class => [qw(forking)],
48 },
49
50 sftp_exec_opt_send_stdout => {
51 order => ++$order,
52 test_class => [qw(forking)],
53 },
54
55 };
56
57 sub new {
58 return shift()->SUPER::new(@_);
59 }
60
61 sub list_tests {
62
63 # Check for the required Perl modules:
64 #
65 # Net-SSH2
66 # Net-SSH2-SFTP
67
68 my $required = [qw(
69 Net::SSH2
70 Net::SSH2::SFTP
71 )];
72
73 foreach my $req (@$required) {
74 eval "use $req";
75 if ($@) {
76 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
77
78 if ($ENV{TEST_VERBOSE}) {
79 print STDERR "Unable to load $req: $@\n";
80 }
81
82 return qw(testsuite_empty_test);
83 }
84 }
85
86 return testsuite_get_runnable_tests($TESTS);
87 }
88
89 sub set_up {
90 my $self = shift;
91 $self->SUPER::set_up(@_);
92
93 # Make sure that mod_sftp does not complain about permissions on the hostkey
94 # files.
95
96 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
97 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
98
99 unless (chmod(0400, $rsa_host_key, $dsa_host_key)) {
100 die("Can't set perms on $rsa_host_key, $dsa_host_key: $!");
101 }
102 }
103
104 sub sftp_exec_on_connect {
105 my $self = shift;
106 my $tmpdir = $self->{tmpdir};
107
108 my $config_file = "$tmpdir/exec.conf";
109 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
110 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
111
112 my $log_file = File::Spec->rel2abs('tests.log');
113
114 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
115 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
116
117 my $user = 'proftpd';
118 my $passwd = 'test';
119 my $group = 'ftpd';
120 my $home_dir = File::Spec->rel2abs($tmpdir);
121 my $uid = 500;
122 my $gid = 500;
123
124 # Make sure that, if we're running as root, that the home directory has
125 # permissions/privs set for the account we create
126 if ($< == 0) {
127 unless (chmod(0755, $home_dir)) {
128 die("Can't set perms on $home_dir to 0755: $!");
129 }
130
131 unless (chown($uid, $gid, $home_dir)) {
132 die("Can't set owner of $home_dir to $uid/$gid: $!");
133 }
134 }
135
136 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
137 '/bin/bash');
138 auth_group_write($auth_group_file, $group, $gid, $user);
139
140 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
141 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
142
143 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
144
145 my $config = {
146 PidFile => $pid_file,
147 ScoreboardFile => $scoreboard_file,
148 SystemLog => $log_file,
149
150 AuthUserFile => $auth_user_file,
151 AuthGroupFile => $auth_group_file,
152 AuthOrder => 'mod_auth_file.c',
153
154 IfModules => {
155 'mod_delay.c' => {
156 DelayEngine => 'off',
157 },
158
159 'mod_exec.c' => {
160 ExecEngine => 'on',
161 ExecLog => $log_file,
162 ExecTimeout => 1,
163 ExecOnConnect => "/bin/bash -c \"echo %a > $connect_file\"",
164 },
165
166 'mod_sftp.c' => [
167 "SFTPEngine on",
168 "SFTPLog $log_file",
169 "SFTPHostKey $rsa_host_key",
170 "SFTPHostKey $dsa_host_key",
171 ],
172 },
173 };
174
175 my ($port, $config_user, $config_group) = config_write($config_file, $config);
176
177 # Open pipes, for use between the parent and child processes. Specifically,
178 # the child will indicate when it's done with its test by writing a message
179 # to the parent.
180 my ($rfh, $wfh);
181 unless (pipe($rfh, $wfh)) {
182 die("Can't open pipe: $!");
183 }
184
185 require Net::SSH2;
186
187 my $ex;
188
189 # Fork child
190 $self->handle_sigchld();
191 defined(my $pid = fork()) or die("Can't fork: $!");
192 if ($pid) {
193 eval {
194 my $ssh2 = Net::SSH2->new();
195
196 sleep(1);
197
198 unless ($ssh2->connect('127.0.0.1', $port)) {
199 my ($err_code, $err_name, $err_str) = $ssh2->error();
200 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
201 }
202
203 unless ($ssh2->auth_password($user, $passwd)) {
204 my ($err_code, $err_name, $err_str) = $ssh2->error();
205 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
206 }
207
208 $ssh2->disconnect();
209 };
210
211 if ($@) {
212 $ex = $@;
213 }
214
215 $wfh->print("done\n");
216 $wfh->flush();
217
218 } else {
219 eval { server_wait($config_file, $rfh) };
220 if ($@) {
221 warn($@);
222 exit 1;
223 }
224
225 exit 0;
226 }
227
228 # Stop server
229 server_stop($pid_file);
230
231 $self->assert_child_ok($pid);
232
233 if ($ex) {
234 die($ex);
235 }
236
237 if (open(my $fh, "< $connect_file")) {
238 my $line = <$fh>;
239 close($fh);
240
241 chomp($line);
242
243 my $expected = '127.0.0.1';
244
245 $self->assert($expected eq $line,
246 test_msg("Expected '$expected', got '$line'"));
247
248 } else {
249 die("Can't read $connect_file: $!");
250 }
251
252 unlink($log_file);
253 }
254
255 sub sftp_exec_on_cmd {
256 my $self = shift;
257 my $tmpdir = $self->{tmpdir};
258
259 my $config_file = "$tmpdir/exec.conf";
260 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
261 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
262
263 my $log_file = File::Spec->rel2abs('tests.log');
264
265 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
266 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
267
268 my $user = 'proftpd';
269 my $passwd = 'test';
270 my $group = 'ftpd';
271 my $home_dir = File::Spec->rel2abs($tmpdir);
272 my $uid = 500;
273 my $gid = 500;
274
275 # Make sure that, if we're running as root, that the home directory has
276 # permissions/privs set for the account we create
277 if ($< == 0) {
278 unless (chmod(0755, $home_dir)) {
279 die("Can't set perms on $home_dir to 0755: $!");
280 }
281
282 unless (chown($uid, $gid, $home_dir)) {
283 die("Can't set owner of $home_dir to $uid/$gid: $!");
284 }
285 }
286
287 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
288 '/bin/bash');
289 auth_group_write($auth_group_file, $group, $gid, $user);
290
291 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
292 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
293
294 my $cmd_file = File::Spec->rel2abs("$tmpdir/cmd.txt");
295
296 my $config = {
297 PidFile => $pid_file,
298 ScoreboardFile => $scoreboard_file,
299 SystemLog => $log_file,
300
301 AuthUserFile => $auth_user_file,
302 AuthGroupFile => $auth_group_file,
303 AuthOrder => 'mod_auth_file.c',
304
305 IfModules => {
306 'mod_delay.c' => {
307 DelayEngine => 'off',
308 },
309
310 'mod_exec.c' => {
311 ExecEngine => 'on',
312 ExecLog => $log_file,
313 ExecTimeout => 1,
314 ExecOnCommand => "OPENDIR /bin/bash -c \"echo %a > $cmd_file\"",
315 },
316
317 'mod_sftp.c' => [
318 "SFTPEngine on",
319 "SFTPLog $log_file",
320 "SFTPHostKey $rsa_host_key",
321 "SFTPHostKey $dsa_host_key",
322 ],
323
324 },
325 };
326
327 my ($port, $config_user, $config_group) = config_write($config_file, $config);
328
329 # Open pipes, for use between the parent and child processes. Specifically,
330 # the child will indicate when it's done with its test by writing a message
331 # to the parent.
332 my ($rfh, $wfh);
333 unless (pipe($rfh, $wfh)) {
334 die("Can't open pipe: $!");
335 }
336
337 require Net::SSH2;
338
339 my $ex;
340
341 # Fork child
342 $self->handle_sigchld();
343 defined(my $pid = fork()) or die("Can't fork: $!");
344 if ($pid) {
345 eval {
346 my $ssh2 = Net::SSH2->new();
347
348 sleep(1);
349
350 unless ($ssh2->connect('127.0.0.1', $port)) {
351 my ($err_code, $err_name, $err_str) = $ssh2->error();
352 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
353 }
354
355 unless ($ssh2->auth_password($user, $passwd)) {
356 my ($err_code, $err_name, $err_str) = $ssh2->error();
357 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
358 }
359
360 my $sftp = $ssh2->sftp();
361 unless ($sftp) {
362 my ($err_code, $err_name, $err_str) = $ssh2->error();
363 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
364 }
365
366 my $dir = $sftp->opendir('.');
367 unless ($dir) {
368 my ($err_code, $err_name) = $sftp->error();
369 die("Can't open directory '.': [$err_name] ($err_code)");
370 }
371
372 my $res = {};
373
374 my $file = $dir->read();
375 while ($file) {
376 $res->{$file->{name}} = $file;
377 $file = $dir->read();
378 }
379
380 my $expected = {
381 '.' => 1,
382 '..' => 1,
383 'exec.conf' => 1,
384 'exec.group' => 1,
385 'exec.passwd' => 1,
386 'exec.pid' => 1,
387 'exec.scoreboard' => 1,
388 'exec.scoreboard.lck' => 1,
389 };
390
391 # To issue the FXP_CLOSE, we have to explicitly destroy the dirhandle
392 $dir = undef;
393
394 # To close the SFTP channel, we have to explicitly destroy the object
395 $sftp = undef;
396
397 $ssh2->disconnect();
398 };
399
400 if ($@) {
401 $ex = $@;
402 }
403
404 $wfh->print("done\n");
405 $wfh->flush();
406
407 } else {
408 eval { server_wait($config_file, $rfh) };
409 if ($@) {
410 warn($@);
411 exit 1;
412 }
413
414 exit 0;
415 }
416
417 # Stop server
418 server_stop($pid_file);
419
420 $self->assert_child_ok($pid);
421
422 if ($ex) {
423 die($ex);
424 }
425
426 if (open(my $fh, "< $cmd_file")) {
427 my $line = <$fh>;
428 close($fh);
429
430 chomp($line);
431
432 my $expected = '127.0.0.1';
433
434 $self->assert($expected eq $line,
435 test_msg("Expected '$expected', got '$line'"));
436
437 } else {
438 die("Can't read $cmd_file: $!");
439 }
440
441 unlink($log_file);
442 }
443
444 sub sftp_exec_on_cmd_var_total_bytes_xfer {
445 my $self = shift;
446 my $tmpdir = $self->{tmpdir};
447
448 my $config_file = "$tmpdir/exec.conf";
449 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
450 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
451
452 my $log_file = File::Spec->rel2abs('tests.log');
453
454 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
455 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
456
457 my $user = 'proftpd';
458 my $passwd = 'test';
459 my $group = 'ftpd';
460 my $home_dir = File::Spec->rel2abs($tmpdir);
461 my $uid = 500;
462 my $gid = 500;
463
464 # Make sure that, if we're running as root, that the home directory has
465 # permissions/privs set for the account we create
466 if ($< == 0) {
467 unless (chmod(0755, $home_dir)) {
468 die("Can't set perms on $home_dir to 0755: $!");
469 }
470
471 unless (chown($uid, $gid, $home_dir)) {
472 die("Can't set owner of $home_dir to $uid/$gid: $!");
473 }
474 }
475
476 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
477 '/bin/bash');
478 auth_group_write($auth_group_file, $group, $gid, $user);
479
480 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
481 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
482
483 my $cmd_file = File::Spec->rel2abs("$tmpdir/cmd.txt");
484
485 my $config = {
486 PidFile => $pid_file,
487 ScoreboardFile => $scoreboard_file,
488 SystemLog => $log_file,
489
490 AuthUserFile => $auth_user_file,
491 AuthGroupFile => $auth_group_file,
492 AuthOrder => 'mod_auth_file.c',
493
494 IfModules => {
495 'mod_delay.c' => {
496 DelayEngine => 'off',
497 },
498
499 'mod_exec.c' => {
500 ExecEngine => 'on',
501 ExecLog => $log_file,
502 ExecTimeout => 1,
503 ExecOnCommand => "CLOSE /bin/bash -c \"echo %{total_bytes_xfer} > $cmd_file\"",
504 },
505
506 'mod_sftp.c' => [
507 "SFTPEngine on",
508 "SFTPLog $log_file",
509 "SFTPHostKey $rsa_host_key",
510 "SFTPHostKey $dsa_host_key",
511 ],
512 },
513 };
514
515 my ($port, $config_user, $config_group) = config_write($config_file, $config);
516
517 # Open pipes, for use between the parent and child processes. Specifically,
518 # the child will indicate when it's done with its test by writing a message
519 # to the parent.
520 my ($rfh, $wfh);
521 unless (pipe($rfh, $wfh)) {
522 die("Can't open pipe: $!");
523 }
524
525 require Net::SSH2;
526
527 my $ex;
528
529 # Fork child
530 $self->handle_sigchld();
531 defined(my $pid = fork()) or die("Can't fork: $!");
532 if ($pid) {
533 eval {
534 my $ssh2 = Net::SSH2->new();
535
536 sleep(1);
537
538 unless ($ssh2->connect('127.0.0.1', $port)) {
539 my ($err_code, $err_name, $err_str) = $ssh2->error();
540 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
541 }
542
543 unless ($ssh2->auth_password($user, $passwd)) {
544 my ($err_code, $err_name, $err_str) = $ssh2->error();
545 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
546 }
547
548 my $sftp = $ssh2->sftp();
549 unless ($sftp) {
550 my ($err_code, $err_name, $err_str) = $ssh2->error();
551 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
552 }
553
554 my $dir = $sftp->opendir('.');
555 unless ($dir) {
556 my ($err_code, $err_name) = $sftp->error();
557 die("Can't open directory '.': [$err_name] ($err_code)");
558 }
559
560 my $res = {};
561
562 my $file = $dir->read();
563 while ($file) {
564 $res->{$file->{name}} = $file;
565 $file = $dir->read();
566 }
567
568 my $expected = {
569 '.' => 1,
570 '..' => 1,
571 'exec.conf' => 1,
572 'exec.group' => 1,
573 'exec.passwd' => 1,
574 'exec.pid' => 1,
575 'exec.scoreboard' => 1,
576 'exec.scoreboard.lck' => 1,
577 };
578
579 # To issue the FXP_CLOSE, we have to explicitly destroy the dirhandle
580 $dir = undef;
581
582 # To close the SFTP channel, we have to explicitly destroy the object
583 $sftp = undef;
584
585 $ssh2->disconnect();
586 };
587
588 if ($@) {
589 $ex = $@;
590 }
591
592 $wfh->print("done\n");
593 $wfh->flush();
594
595 } else {
596 eval { server_wait($config_file, $rfh) };
597 if ($@) {
598 warn($@);
599 exit 1;
600 }
601
602 exit 0;
603 }
604
605 # Stop server
606 server_stop($pid_file);
607
608 $self->assert_child_ok($pid);
609
610 if ($ex) {
611 die($ex);
612 }
613
614 if (open(my $fh, "< $cmd_file")) {
615 my $line = <$fh>;
616 close($fh);
617
618 chomp($line);
619
620 my $expected = '\d+';
621
622 $self->assert(qr/$expected/, $line,
623 test_msg("Expected '$expected', got '$line'"));
624
625 } else {
626 die("Can't read $cmd_file: $!");
627 }
628
629 unlink($log_file);
630 }
631
632 sub sftp_exec_on_cmd_var_bytes_xfer {
633 my $self = shift;
634 my $tmpdir = $self->{tmpdir};
635
636 my $config_file = "$tmpdir/exec.conf";
637 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
638 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
639
640 my $log_file = File::Spec->rel2abs('tests.log');
641
642 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
643 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
644
645 my $user = 'proftpd';
646 my $passwd = 'test';
647 my $group = 'ftpd';
648 my $home_dir = File::Spec->rel2abs($tmpdir);
649 my $uid = 500;
650 my $gid = 500;
651
652 # Make sure that, if we're running as root, that the home directory has
653 # permissions/privs set for the account we create
654 if ($< == 0) {
655 unless (chmod(0755, $home_dir)) {
656 die("Can't set perms on $home_dir to 0755: $!");
657 }
658
659 unless (chown($uid, $gid, $home_dir)) {
660 die("Can't set owner of $home_dir to $uid/$gid: $!");
661 }
662 }
663
664 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
665 '/bin/bash');
666 auth_group_write($auth_group_file, $group, $gid, $user);
667
668 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
669 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
670
671 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
672 if (open(my $fh, "> $test_file")) {
673 print $fh "ABCD\n" x 128;
674
675 unless (close($fh)) {
676 die("Can't write $test_file: $!");
677 }
678
679 } else {
680 die("Can't open $test_file: $!");
681 }
682
683 my $cmd_file = File::Spec->rel2abs("$tmpdir/cmd.txt");
684
685 my $config = {
686 PidFile => $pid_file,
687 ScoreboardFile => $scoreboard_file,
688 SystemLog => $log_file,
689
690 AuthUserFile => $auth_user_file,
691 AuthGroupFile => $auth_group_file,
692 AuthOrder => 'mod_auth_file.c',
693
694 IfModules => {
695 'mod_delay.c' => {
696 DelayEngine => 'off',
697 },
698
699 'mod_exec.c' => {
700 ExecEngine => 'on',
701 ExecLog => $log_file,
702 ExecTimeout => 1,
703 ExecOnCommand => "RETR /bin/bash -c \"echo %{bytes_xfer} > $cmd_file\"",
704 },
705
706 'mod_sftp.c' => [
707 "SFTPEngine on",
708 "SFTPLog $log_file",
709 "SFTPHostKey $rsa_host_key",
710 "SFTPHostKey $dsa_host_key",
711 ],
712
713 },
714 };
715
716 my ($port, $config_user, $config_group) = config_write($config_file, $config);
717
718 # Open pipes, for use between the parent and child processes. Specifically,
719 # the child will indicate when it's done with its test by writing a message
720 # to the parent.
721 my ($rfh, $wfh);
722 unless (pipe($rfh, $wfh)) {
723 die("Can't open pipe: $!");
724 }
725
726 require Net::SSH2;
727
728 my $ex;
729
730 # Fork child
731 $self->handle_sigchld();
732 defined(my $pid = fork()) or die("Can't fork: $!");
733 if ($pid) {
734 eval {
735 my $ssh2 = Net::SSH2->new();
736
737 sleep(1);
738
739 unless ($ssh2->connect('127.0.0.1', $port)) {
740 my ($err_code, $err_name, $err_str) = $ssh2->error();
741 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
742 }
743
744 unless ($ssh2->auth_password($user, $passwd)) {
745 my ($err_code, $err_name, $err_str) = $ssh2->error();
746 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
747 }
748
749 my $sftp = $ssh2->sftp();
750 unless ($sftp) {
751 my ($err_code, $err_name, $err_str) = $ssh2->error();
752 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
753 }
754
755 my $fh = $sftp->open('test.txt', O_RDONLY);
756 unless ($fh) {
757 my ($err_code, $err_name) = $sftp->error();
758 die("Can't open test.txt: [$err_name] ($err_code)");
759 }
760
761 my $buf;
762 my $size = 0;
763
764 my $res = $fh->read($buf, 8192);
765 while ($res) {
766 $size += $res;
767
768 $res = $fh->read($buf, 8192);
769 }
770
771 # To issue the FXP_CLOSE, we have to explicitly destroy the filehandle
772 $fh = undef;
773
774 # To close the SFTP channel, we have to explicitly destroy the object
775 $sftp = undef;
776
777 $ssh2->disconnect();
778 };
779
780 if ($@) {
781 $ex = $@;
782 }
783
784 $wfh->print("done\n");
785 $wfh->flush();
786
787 } else {
788 eval { server_wait($config_file, $rfh) };
789 if ($@) {
790 warn($@);
791 exit 1;
792 }
793
794 exit 0;
795 }
796
797 # Stop server
798 server_stop($pid_file);
799
800 $self->assert_child_ok($pid);
801
802 if ($ex) {
803 die($ex);
804 }
805
806 if (open(my $fh, "< $cmd_file")) {
807 my $line = <$fh>;
808 close($fh);
809
810 chomp($line);
811
812 my $expected = '\d+';
813
814 $self->assert(qr/$expected/, $line,
815 test_msg("Expected '$expected', got '$line'"));
816
817 } else {
818 die("Can't read $cmd_file: $!");
819 }
820
821 unlink($log_file);
822 }
823
824 sub sftp_exec_on_exit {
825 my $self = shift;
826 my $tmpdir = $self->{tmpdir};
827
828 my $config_file = "$tmpdir/exec.conf";
829 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
830 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
831
832 my $log_file = File::Spec->rel2abs('tests.log');
833
834 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
835 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
836
837 my $user = 'proftpd';
838 my $passwd = 'test';
839 my $group = 'ftpd';
840 my $home_dir = File::Spec->rel2abs($tmpdir);
841 my $uid = 500;
842 my $gid = 500;
843
844 # Make sure that, if we're running as root, that the home directory has
845 # permissions/privs set for the account we create
846 if ($< == 0) {
847 unless (chmod(0755, $home_dir)) {
848 die("Can't set perms on $home_dir to 0755: $!");
849 }
850
851 unless (chown($uid, $gid, $home_dir)) {
852 die("Can't set owner of $home_dir to $uid/$gid: $!");
853 }
854 }
855
856 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
857 '/bin/bash');
858 auth_group_write($auth_group_file, $group, $gid, $user);
859
860 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
861 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
862
863 my $exit_file = File::Spec->rel2abs("$tmpdir/exit.txt");
864
865 my $config = {
866 PidFile => $pid_file,
867 ScoreboardFile => $scoreboard_file,
868 SystemLog => $log_file,
869
870 AuthUserFile => $auth_user_file,
871 AuthGroupFile => $auth_group_file,
872 AuthOrder => 'mod_auth_file.c',
873
874 IfModules => {
875 'mod_delay.c' => {
876 DelayEngine => 'off',
877 },
878
879 'mod_exec.c' => {
880 ExecEngine => 'on',
881 ExecLog => $log_file,
882 ExecTimeout => 1,
883 ExecOnExit => "/bin/bash -c \"echo %a > $exit_file\"",
884 },
885
886 'mod_sftp.c' => [
887 "SFTPEngine on",
888 "SFTPLog $log_file",
889 "SFTPHostKey $rsa_host_key",
890 "SFTPHostKey $dsa_host_key",
891 ],
892 },
893 };
894
895 my ($port, $config_user, $config_group) = config_write($config_file, $config);
896
897 # Open pipes, for use between the parent and child processes. Specifically,
898 # the child will indicate when it's done with its test by writing a message
899 # to the parent.
900 my ($rfh, $wfh);
901 unless (pipe($rfh, $wfh)) {
902 die("Can't open pipe: $!");
903 }
904
905 require Net::SSH2;
906
907 my $ex;
908
909 # Fork child
910 $self->handle_sigchld();
911 defined(my $pid = fork()) or die("Can't fork: $!");
912 if ($pid) {
913 eval {
914 my $ssh2 = Net::SSH2->new();
915
916 sleep(1);
917
918 unless ($ssh2->connect('127.0.0.1', $port)) {
919 my ($err_code, $err_name, $err_str) = $ssh2->error();
920 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
921 }
922
923 unless ($ssh2->auth_password($user, $passwd)) {
924 my ($err_code, $err_name, $err_str) = $ssh2->error();
925 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
926 }
927
928 my $sftp = $ssh2->sftp();
929 unless ($sftp) {
930 my ($err_code, $err_name, $err_str) = $ssh2->error();
931 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
932 }
933
934 # To close the SFTP channel, we have to explicitly destroy the object
935 $sftp = undef;
936
937 $ssh2->disconnect();
938 };
939
940 if ($@) {
941 $ex = $@;
942 }
943
944 $wfh->print("done\n");
945 $wfh->flush();
946
947 } else {
948 eval { server_wait($config_file, $rfh) };
949 if ($@) {
950 warn($@);
951 exit 1;
952 }
953
954 exit 0;
955 }
956
957 # Stop server
958 server_stop($pid_file);
959
960 $self->assert_child_ok($pid);
961
962 if ($ex) {
963 die($ex);
964 }
965
966 if (open(my $fh, "< $exit_file")) {
967 my $line = <$fh>;
968 close($fh);
969
970 chomp($line);
971
972 my $expected = '127.0.0.1';
973
974 $self->assert($expected eq $line,
975 test_msg("Expected '$expected', got '$line'"));
976
977 } else {
978 die("Can't read $exit_file: $!");
979 }
980
981 unlink($log_file);
982 }
983
984 sub sftp_exec_on_error {
985 my $self = shift;
986 my $tmpdir = $self->{tmpdir};
987
988 my $config_file = "$tmpdir/exec.conf";
989 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
990 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
991
992 my $log_file = File::Spec->rel2abs('tests.log');
993
994 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
995 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
996
997 my $user = 'proftpd';
998 my $passwd = 'test';
999 my $group = 'ftpd';
1000 my $home_dir = File::Spec->rel2abs($tmpdir);
1001 my $uid = 500;
1002 my $gid = 500;
1003
1004 # Make sure that, if we're running as root, that the home directory has
1005 # permissions/privs set for the account we create
1006 if ($< == 0) {
1007 unless (chmod(0755, $home_dir)) {
1008 die("Can't set perms on $home_dir to 0755: $!");
1009 }
1010
1011 unless (chown($uid, $gid, $home_dir)) {
1012 die("Can't set owner of $home_dir to $uid/$gid: $!");
1013 }
1014 }
1015
1016 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1017 '/bin/bash');
1018 auth_group_write($auth_group_file, $group, $gid, $user);
1019
1020 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
1021 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
1022
1023 my $cmd_file = File::Spec->rel2abs("$tmpdir/cmd.txt");
1024
1025 my $config = {
1026 PidFile => $pid_file,
1027 ScoreboardFile => $scoreboard_file,
1028 SystemLog => $log_file,
1029
1030 AuthUserFile => $auth_user_file,
1031 AuthGroupFile => $auth_group_file,
1032 AuthOrder => 'mod_auth_file.c',
1033
1034 IfModules => {
1035 'mod_delay.c' => {
1036 DelayEngine => 'off',
1037 },
1038
1039 'mod_exec.c' => {
1040 ExecEngine => 'on',
1041 ExecLog => $log_file,
1042 ExecTimeout => 1,
1043 ExecOnError => "STOR /bin/bash -c \"echo %a > $cmd_file\"",
1044 },
1045
1046 'mod_sftp.c' => [
1047 "SFTPEngine on",
1048 "SFTPLog $log_file",
1049 "SFTPHostKey $rsa_host_key",
1050 "SFTPHostKey $dsa_host_key",
1051 ],
1052
1053 },
1054
1055 Limit => {
1056 'STOR' => {
1057 DenyAll => '',
1058 },
1059 },
1060
1061 };
1062
1063 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1064
1065 # Open pipes, for use between the parent and child processes. Specifically,
1066 # the child will indicate when it's done with its test by writing a message
1067 # to the parent.
1068 my ($rfh, $wfh);
1069 unless (pipe($rfh, $wfh)) {
1070 die("Can't open pipe: $!");
1071 }
1072
1073 require Net::SSH2;
1074
1075 my $ex;
1076
1077 # Fork child
1078 $self->handle_sigchld();
1079 defined(my $pid = fork()) or die("Can't fork: $!");
1080 if ($pid) {
1081 eval {
1082 my $ssh2 = Net::SSH2->new();
1083
1084 sleep(1);
1085
1086 unless ($ssh2->connect('127.0.0.1', $port)) {
1087 my ($err_code, $err_name, $err_str) = $ssh2->error();
1088 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1089 }
1090
1091 unless ($ssh2->auth_password($user, $passwd)) {
1092 my ($err_code, $err_name, $err_str) = $ssh2->error();
1093 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1094 }
1095
1096 my $sftp = $ssh2->sftp();
1097 unless ($sftp) {
1098 my ($err_code, $err_name, $err_str) = $ssh2->error();
1099 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1100 }
1101
1102 my $fh = $sftp->open('test.txt', O_WRONLY|O_CREAT|O_TRUNC, 0644);
1103 if ($fh) {
1104 die("SFTP upload to test.txt succeeded unexpectedly");
1105 }
1106
1107 my ($err_code, $err_name) = $sftp->error();
1108
1109 my $expected = 'SSH_FX_PERMISSION_DENIED';
1110 $self->assert($expected eq $err_name,
1111 test_msg("Expected '$expected', got '$err_name'"));
1112
1113 $sftp = undef;
1114 $ssh2->disconnect();
1115 };
1116
1117 if ($@) {
1118 $ex = $@;
1119 }
1120
1121 $wfh->print("done\n");
1122 $wfh->flush();
1123
1124 } else {
1125 eval { server_wait($config_file, $rfh) };
1126 if ($@) {
1127 warn($@);
1128 exit 1;
1129 }
1130
1131 exit 0;
1132 }
1133
1134 # Stop server
1135 server_stop($pid_file);
1136
1137 $self->assert_child_ok($pid);
1138
1139 if ($ex) {
1140 die($ex);
1141 }
1142
1143 if (open(my $fh, "< $cmd_file")) {
1144 my $line = <$fh>;
1145 close($fh);
1146
1147 chomp($line);
1148
1149 my $expected = '127.0.0.1';
1150
1151 $self->assert($expected eq $line,
1152 test_msg("Expected '$expected', got '$line'"));
1153
1154 } else {
1155 die("Can't read $cmd_file: $!");
1156 }
1157
1158 unlink($log_file);
1159 }
1160
1161 sub exec_restart {
1162 my $pid_file = shift;
1163
1164 my $pid;
1165 if (open(my $fh, "< $pid_file")) {
1166 $pid = <$fh>;
1167 chomp($pid);
1168 close($fh);
1169
1170 } else {
1171 die("Can't read $pid_file: $!");
1172 }
1173
1174 my $cmd = "kill -HUP $pid";
1175
1176 if ($ENV{TEST_VERBOSE}) {
1177 print STDERR "Restarting server: $cmd\n";
1178 }
1179
1180 my @output = `$cmd`;
1181 if ($ENV{TEST_VERBOSE}) {
1182 print STDERR "Output: ", join('', @output), "\n";
1183 }
1184 }
1185
1186 sub exec_on_restart {
1187 my $self = shift;
1188 my $tmpdir = $self->{tmpdir};
1189
1190 my $config_file = "$tmpdir/exec.conf";
1191 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
1192 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
1193
1194 my $log_file = File::Spec->rel2abs('tests.log');
1195
1196 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
1197 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
1198
1199 my $user = 'proftpd';
1200 my $passwd = 'test';
1201 my $home_dir = File::Spec->rel2abs($tmpdir);
1202 my $uid = 500;
1203 my $gid = 500;
1204
1205 # Make sure that, if we're running as root, that the home directory has
1206 # permissions/privs set for the account we create
1207 if ($< == 0) {
1208 unless (chmod(0755, $home_dir)) {
1209 die("Can't set perms on $home_dir to 0755: $!");
1210 }
1211
1212 unless (chown($uid, $gid, $home_dir)) {
1213 die("Can't set owner of $home_dir to $uid/$gid: $!");
1214 }
1215 }
1216
1217 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1218 '/bin/bash');
1219 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1220
1221 my $restart_file = File::Spec->rel2abs("$tmpdir/restart.txt");
1222
1223 my $config = {
1224 PidFile => $pid_file,
1225 ScoreboardFile => $scoreboard_file,
1226 SystemLog => $log_file,
1227 TraceLog => $log_file,
1228 Trace => 'DEFAULT:10',
1229
1230 AuthUserFile => $auth_user_file,
1231 AuthGroupFile => $auth_group_file,
1232 AuthOrder => 'mod_auth_file.c',
1233
1234 IfModules => {
1235 'mod_exec.c' => {
1236 ExecEngine => 'on',
1237 ExecLog => $log_file,
1238 ExecTimeout => 1,
1239 ExecOnRestart => "/bin/bash -c \"echo restarted > $restart_file\"",
1240 },
1241
1242 'mod_delay.c' => {
1243 DelayEngine => 'off',
1244 },
1245 },
1246
1247 };
1248
1249 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1250
1251 # Start the server
1252 server_start($config_file);
1253
1254 # Give it a second or two to start up, then send the SIGHUP signal. Wait
1255 # for a couple seconds after, to let mod_exec do its thing.
1256 sleep(2);
1257 exec_restart($pid_file);
1258 sleep(2);
1259
1260 # Stop server
1261 server_stop($pid_file);
1262
1263 if (open(my $fh, "< $restart_file")) {
1264 my $line = <$fh>;
1265 close($fh);
1266
1267 chomp($line);
1268
1269 my $expected = 'restarted';
1270
1271 $self->assert($expected eq $line,
1272 test_msg("Expected '$expected', got '$line'"));
1273
1274 } else {
1275 die("Can't read $restart_file: $!");
1276 }
1277
1278 unlink($log_file);
1279 }
1280
1281 sub exec_opt_log_stdout {
1282 my $self = shift;
1283 my $tmpdir = $self->{tmpdir};
1284
1285 my $config_file = "$tmpdir/exec.conf";
1286 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
1287 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
1288
1289 my $log_file = File::Spec->rel2abs('tests.log');
1290
1291 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
1292 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
1293
1294 my $user = 'proftpd';
1295 my $passwd = 'test';
1296 my $home_dir = File::Spec->rel2abs($tmpdir);
1297 my $uid = 500;
1298 my $gid = 500;
1299
1300 # Make sure that, if we're running as root, that the home directory has
1301 # permissions/privs set for the account we create
1302 if ($< == 0) {
1303 unless (chmod(0755, $home_dir)) {
1304 die("Can't set perms on $home_dir to 0755: $!");
1305 }
1306
1307 unless (chown($uid, $gid, $home_dir)) {
1308 die("Can't set owner of $home_dir to $uid/$gid: $!");
1309 }
1310 }
1311
1312 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1313 '/bin/bash');
1314 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1315
1316 my $script = File::Spec->rel2abs("$tmpdir/exec.pl");
1317 if (open(my $fh, "> $script")) {
1318 print $fh <<EOS;
1319 #!/usr/bin/env perl
1320 print STDOUT "\$ARGV[0]\\n";
1321 exit 0;
1322 EOS
1323 unless (close($fh)) {
1324 die("Can't write $script: $!");
1325 }
1326
1327 unless (chmod(0755, $script)) {
1328 die("Can't set perms on $script to 0755: $!");
1329 }
1330
1331 } else {
1332 die("Can't open $script: $!");
1333 }
1334
1335 my $config = {
1336 PidFile => $pid_file,
1337 ScoreboardFile => $scoreboard_file,
1338 SystemLog => $log_file,
1339
1340 AuthUserFile => $auth_user_file,
1341 AuthGroupFile => $auth_group_file,
1342 AuthOrder => 'mod_auth_file.c',
1343
1344 IfModules => {
1345 'mod_exec.c' => {
1346 ExecEngine => 'on',
1347 ExecLog => $log_file,
1348 ExecTimeout => 1,
1349 ExecOnConnect => "$script addr=%a",
1350 ExecOptions => 'logStdout',
1351 },
1352
1353 'mod_delay.c' => {
1354 DelayEngine => 'off',
1355 },
1356 },
1357 };
1358
1359 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1360
1361 # Open pipes, for use between the parent and child processes. Specifically,
1362 # the child will indicate when it's done with its test by writing a message
1363 # to the parent.
1364 my ($rfh, $wfh);
1365 unless (pipe($rfh, $wfh)) {
1366 die("Can't open pipe: $!");
1367 }
1368
1369 my $ex;
1370
1371 # Fork child
1372 $self->handle_sigchld();
1373 defined(my $pid = fork()) or die("Can't fork: $!");
1374 if ($pid) {
1375 eval {
1376 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1377 };
1378
1379 if ($@) {
1380 $ex = $@;
1381 }
1382
1383 $wfh->print("done\n");
1384 $wfh->flush();
1385
1386 } else {
1387 eval { server_wait($config_file, $rfh) };
1388 if ($@) {
1389 warn($@);
1390 exit 1;
1391 }
1392
1393 exit 0;
1394 }
1395
1396 # Stop server
1397 server_stop($pid_file);
1398
1399 $self->assert_child_ok($pid);
1400
1401 if ($ex) {
1402 die($ex);
1403 }
1404
1405 if (open(my $fh, "< $log_file")) {
1406 my $line;
1407 while ($line = <$fh>) {
1408 unless ($line =~ /stdout from '$script'/) {
1409 next;
1410 }
1411
1412 chomp($line);
1413 last;
1414 }
1415
1416 close($fh);
1417
1418 $line =~ /stdout from '$script': '(.*?)'/;
1419 my $stdout = $1;
1420
1421 my $expected = 'addr=127.0.0.1';
1422
1423 $self->assert($expected eq $stdout,
1424 test_msg("Expected '$expected', got '$stdout'"));
1425
1426 } else {
1427 die("Can't read $log_file: $!");
1428 }
1429
1430 unlink($log_file);
1431 }
1432
1433 sub exec_opt_log_stderr {
1434 my $self = shift;
1435 my $tmpdir = $self->{tmpdir};
1436
1437 my $config_file = "$tmpdir/exec.conf";
1438 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
1439 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
1440
1441 my $log_file = File::Spec->rel2abs('tests.log');
1442
1443 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
1444 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
1445
1446 my $user = 'proftpd';
1447 my $passwd = 'test';
1448 my $home_dir = File::Spec->rel2abs($tmpdir);
1449 my $uid = 500;
1450 my $gid = 500;
1451
1452 # Make sure that, if we're running as root, that the home directory has
1453 # permissions/privs set for the account we create
1454 if ($< == 0) {
1455 unless (chmod(0755, $home_dir)) {
1456 die("Can't set perms on $home_dir to 0755: $!");
1457 }
1458
1459 unless (chown($uid, $gid, $home_dir)) {
1460 die("Can't set owner of $home_dir to $uid/$gid: $!");
1461 }
1462 }
1463
1464 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1465 '/bin/bash');
1466 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1467
1468 my $script = File::Spec->rel2abs("$tmpdir/exec.pl");
1469 if (open(my $fh, "> $script")) {
1470 print $fh <<EOS;
1471 #!/usr/bin/env perl
1472 print STDERR "\$ARGV[0]\\n";
1473 exit 0;
1474 EOS
1475 unless (close($fh)) {
1476 die("Can't write $script: $!");
1477 }
1478
1479 unless (chmod(0755, $script)) {
1480 die("Can't set perms on $script to 0755: $!");
1481 }
1482
1483 } else {
1484 die("Can't open $script: $!");
1485 }
1486
1487 my $config = {
1488 PidFile => $pid_file,
1489 ScoreboardFile => $scoreboard_file,
1490 SystemLog => $log_file,
1491
1492 AuthUserFile => $auth_user_file,
1493 AuthGroupFile => $auth_group_file,
1494 AuthOrder => 'mod_auth_file.c',
1495
1496 IfModules => {
1497 'mod_exec.c' => {
1498 ExecEngine => 'on',
1499 ExecLog => $log_file,
1500 ExecTimeout => 1,
1501 ExecOnConnect => "$script addr=%a",
1502 ExecOptions => 'logStderr',
1503 },
1504
1505 'mod_delay.c' => {
1506 DelayEngine => 'off',
1507 },
1508 },
1509 };
1510
1511 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1512
1513 # Open pipes, for use between the parent and child processes. Specifically,
1514 # the child will indicate when it's done with its test by writing a message
1515 # to the parent.
1516 my ($rfh, $wfh);
1517 unless (pipe($rfh, $wfh)) {
1518 die("Can't open pipe: $!");
1519 }
1520
1521 my $ex;
1522
1523 # Fork child
1524 $self->handle_sigchld();
1525 defined(my $pid = fork()) or die("Can't fork: $!");
1526 if ($pid) {
1527 eval {
1528 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1529 };
1530
1531 if ($@) {
1532 $ex = $@;
1533 }
1534
1535 $wfh->print("done\n");
1536 $wfh->flush();
1537
1538 } else {
1539 eval { server_wait($config_file, $rfh) };
1540 if ($@) {
1541 warn($@);
1542 exit 1;
1543 }
1544
1545 exit 0;
1546 }
1547
1548 # Stop server
1549 server_stop($pid_file);
1550
1551 $self->assert_child_ok($pid);
1552
1553 if ($ex) {
1554 die($ex);
1555 }
1556
1557 if (open(my $fh, "< $log_file")) {
1558 my $line;
1559 while ($line = <$fh>) {
1560 unless ($line =~ /stderr from '$script'/) {
1561 next;
1562 }
1563
1564 chomp($line);
1565 last;
1566 }
1567
1568 close($fh);
1569
1570 $line =~ /stderr from '$script': '(.*?)'/;
1571 my $stderr = $1;
1572
1573 my $expected = 'addr=127.0.0.1';
1574
1575 $self->assert($expected eq $stderr,
1576 test_msg("Expected '$expected', got '$stderr'"));
1577
1578 } else {
1579 die("Can't read $log_file: $!");
1580 }
1581
1582 unlink($log_file);
1583 }
1584
1585 # NOTE: If this unit test is failing with an error like the following:
1586 #
1587 # not ok ERROR sftp_exec_opt_send_stdout
1588 # Test::Unit::ExceptionError:
1589 # Can't connect to SSH2 server: [LIBSSH2_ERROR_SOCKET_NONE] (-1) Unable to exchange encryption keys at t/lib/ProFTPD/Tests/Modules/mod_sftp/exec.pm line 1693.
1590 #
1591 # Then it is most likely caused by having mod_exec appear AFTER mod_sftp in
1592 # the --with-modules configure argument. This means that mod_exec's sess_init
1593 # callback would run BEFORE mod_sftp's, which in turn means that mod_exec
1594 # would not be able to detect that this is an SSH connection (and thus
1595 # would not automatically disable the sendStdout ExecOptions).
1596
1597 sub sftp_exec_opt_send_stdout {
1598 my $self = shift;
1599 my $tmpdir = $self->{tmpdir};
1600
1601 my $config_file = "$tmpdir/exec.conf";
1602 my $pid_file = File::Spec->rel2abs("$tmpdir/exec.pid");
1603 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/exec.scoreboard");
1604
1605 my $log_file = File::Spec->rel2abs('tests.log');
1606
1607 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
1608 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
1609
1610 my $user = 'proftpd';
1611 my $passwd = 'test';
1612 my $group = 'ftpd';
1613 my $home_dir = File::Spec->rel2abs($tmpdir);
1614 my $uid = 500;
1615 my $gid = 500;
1616
1617 # Make sure that, if we're running as root, that the home directory has
1618 # permissions/privs set for the account we create
1619 if ($< == 0) {
1620 unless (chmod(0755, $home_dir)) {
1621 die("Can't set perms on $home_dir to 0755: $!");
1622 }
1623
1624 unless (chown($uid, $gid, $home_dir)) {
1625 die("Can't set owner of $home_dir to $uid/$gid: $!");
1626 }
1627 }
1628
1629 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1630 '/bin/bash');
1631 auth_group_write($auth_group_file, $group, $gid, $user);
1632
1633 my $script = File::Spec->rel2abs("$tmpdir/exec.pl");
1634 if (open(my $fh, "> $script")) {
1635 print $fh <<EOS;
1636 #!/usr/bin/env perl
1637 print STDOUT "\$ARGV[0]\\n";
1638 exit 0;
1639 EOS
1640 unless (close($fh)) {
1641 die("Can't write $script: $!");
1642 }
1643
1644 unless (chmod(0755, $script)) {
1645 die("Can't set perms on $script to 0755: $!");
1646 }
1647
1648 } else {
1649 die("Can't open $script: $!");
1650 }
1651
1652 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
1653 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
1654
1655 my $config = {
1656 PidFile => $pid_file,
1657 ScoreboardFile => $scoreboard_file,
1658 SystemLog => $log_file,
1659
1660 AuthUserFile => $auth_user_file,
1661 AuthGroupFile => $auth_group_file,
1662 AuthOrder => 'mod_auth_file.c',
1663
1664 IfModules => {
1665 'mod_delay.c' => {
1666 DelayEngine => 'off',
1667 },
1668
1669 'mod_exec.c' => {
1670 ExecEngine => 'on',
1671 ExecLog => $log_file,
1672 ExecTimeout => 1,
1673 ExecOnConnect => "$script addr=%a",
1674 ExecOptions => 'sendStdout',
1675 },
1676
1677 'mod_sftp.c' => [
1678 "SFTPEngine on",
1679 "SFTPLog $log_file",
1680 "SFTPHostKey $rsa_host_key",
1681 "SFTPHostKey $dsa_host_key",
1682 ],
1683
1684 },
1685 };
1686
1687 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1688
1689 # Open pipes, for use between the parent and child processes. Specifically,
1690 # the child will indicate when it's done with its test by writing a message
1691 # to the parent.
1692 my ($rfh, $wfh);
1693 unless (pipe($rfh, $wfh)) {
1694 die("Can't open pipe: $!");
1695 }
1696
1697 require Net::SSH2;
1698
1699 my $ex;
1700
1701 # Fork child
1702 $self->handle_sigchld();
1703 defined(my $pid = fork()) or die("Can't fork: $!");
1704 if ($pid) {
1705 eval {
1706 my $ssh2 = Net::SSH2->new();
1707
1708 sleep(3);
1709
1710 unless ($ssh2->connect('127.0.0.1', $port)) {
1711 my ($err_code, $err_name, $err_str) = $ssh2->error();
1712 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1713 }
1714
1715 unless ($ssh2->auth_password($user, $passwd)) {
1716 my ($err_code, $err_name, $err_str) = $ssh2->error();
1717 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1718 }
1719
1720 my $sftp = $ssh2->sftp();
1721 unless ($sftp) {
1722 my ($err_code, $err_name, $err_str) = $ssh2->error();
1723 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1724 }
1725
1726 $sftp = undef;
1727 $ssh2->disconnect();
1728 };
1729
1730 if ($@) {
1731 $ex = $@;
1732 }
1733
1734 $wfh->print("done\n");
1735 $wfh->flush();
1736
1737 } else {
1738 eval { server_wait($config_file, $rfh) };
1739 if ($@) {
1740 warn($@);
1741 exit 1;
1742 }
1743
1744 exit 0;
1745 }
1746
1747 # Stop server
1748 server_stop($pid_file);
1749
1750 $self->assert_child_ok($pid);
1751
1752 if ($ex) {
1753 die($ex);
1754 }
1755
1756 unlink($log_file);
1757 }
1758
1759 1;