Debian Codegraph @

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

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