Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/UserDirRoot.pm

raw
1 package ProFTPD::Tests::Config::UserDirRoot;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
8 use File::Spec;
9 use IO::Handle;
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 userdirroot_ok => {
20 order => ++$order,
21 test_class => [qw(forking rootprivs)],
22 },
23
24 userdirroot_sanitized_ok => {
25 order => ++$order,
26 test_class => [qw(forking rootprivs)],
27 },
28
29 };
30
31 sub new {
32 return shift()->SUPER::new(@_);
33 }
34
35 sub list_tests {
36 return testsuite_get_runnable_tests($TESTS);
37 }
38
39 sub userdirroot_ok {
40 my $self = shift;
41 my $tmpdir = $self->{tmpdir};
42 my $setup = test_setup($tmpdir, 'config');
43
44 my $alias = 'ftp';
45 my $test_dir = File::Spec->rel2abs("$tmpdir/$alias");
46 mkpath($test_dir);
47
48 if ($< == 0) {
49 unless (chmod(0755, $test_dir)) {
50 die("Can't set perms on $test_dir to 0755: $!");
51 }
52
53 unless (chown($setup->{uid}, $setup->{gid}, $test_dir)) {
54 die("Can't set owner of $test_dir to $setup->{uid}/$setup->{gid}: $!");
55 }
56 }
57
58 my $config = {
59 PidFile => $setup->{pid_file},
60 ScoreboardFile => $setup->{scoreboard_file},
61 SystemLog => $setup->{log_file},
62 TraceLog => $setup->{log_file},
63 Trace => 'auth:20',
64
65 AuthUserFile => $setup->{auth_user_file},
66 AuthGroupFile => $setup->{auth_group_file},
67 AuthOrder => 'mod_auth_file.c',
68
69 Anonymous => {
70 $setup->{home_dir} => {
71 User => $setup->{user},
72 Group => $setup->{group},
73 UserAlias => "$alias $setup->{user}",
74 UserDirRoot => 'on',
75 },
76 },
77
78 IfModules => {
79 'mod_delay.c' => {
80 DelayEngine => 'off',
81 },
82 },
83 };
84
85 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
86 $config);
87
88 # Open pipes, for use between the parent and child processes. Specifically,
89 # the child will indicate when it's done with its test by writing a message
90 # to the parent.
91 my ($rfh, $wfh);
92 unless (pipe($rfh, $wfh)) {
93 die("Can't open pipe: $!");
94 }
95
96 my $ex;
97
98 # Fork child
99 $self->handle_sigchld();
100 defined(my $pid = fork()) or die("Can't fork: $!");
101 if ($pid) {
102 eval {
103 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
104 $client->login($alias, 'NoPasswordNeededHereForAnon');
105 $client->quit();
106 };
107 if ($@) {
108 $ex = $@;
109 }
110
111 $wfh->print("done\n");
112 $wfh->flush();
113
114 } else {
115 eval { server_wait($setup->{config_file}, $rfh) };
116 if ($@) {
117 warn($@);
118 exit 1;
119 }
120
121 exit 0;
122 }
123
124 # Stop server
125 server_stop($setup->{pid_file});
126 $self->assert_child_ok($pid);
127
128 eval {
129 if (open(my $fh, "< $setup->{log_file}")) {
130 my $ok = 0;
131
132 while (my $line = <$fh>) {
133 chomp($line);
134
135 if ($ENV{TEST_VERBOSE}) {
136 print STDERR "# $line\n";
137 }
138
139 if ($line =~ /Preparing to chroot to directory '(.*)?'/) {
140 my $chroot_path = $1;
141
142 my $expected = $test_dir;
143 if ($^O eq 'darwin') {
144 # MacOSX-specific hack
145 $expected = '/private' . $expected;
146 }
147
148 if ($chroot_path eq $expected) {
149 $ok = 1;
150 last;
151 }
152 }
153 }
154
155 close($fh);
156 $self->assert($ok, test_msg("Did not see expected chroot directory path"));
157
158 } else {
159 die("Can't read $setup->{log_file}: $!");
160 }
161 };
162 if ($@) {
163 $ex = $@;
164 }
165
166 test_cleanup($setup->{log_file}, $ex);
167 }
168
169 sub userdirroot_sanitized_ok {
170 my $self = shift;
171 my $tmpdir = $self->{tmpdir};
172 my $setup = test_setup($tmpdir, 'config');
173
174 my $test_dir = File::Spec->rel2abs("$tmpdir/ftp");
175 mkpath($test_dir);
176
177 if ($< == 0) {
178 unless (chmod(0755, $test_dir)) {
179 die("Can't set perms on $test_dir to 0755: $!");
180 }
181
182 unless (chown($setup->{uid}, $setup->{gid}, $test_dir)) {
183 die("Can't set owner of $test_dir to $setup->{uid}/$setup->{gid}: $!");
184 }
185 }
186
187 my $config = {
188 PidFile => $setup->{pid_file},
189 ScoreboardFile => $setup->{scoreboard_file},
190 SystemLog => $setup->{log_file},
191 TraceLog => $setup->{log_file},
192 Trace => 'auth:20',
193
194 AuthUserFile => $setup->{auth_user_file},
195 AuthGroupFile => $setup->{auth_group_file},
196 AuthOrder => 'mod_auth_file.c',
197
198 Anonymous => {
199 $setup->{home_dir} => {
200 User => $setup->{user},
201 Group => $setup->{group},
202
203 # NOTE: In general, using a wildcard UserAlias is a Very Bad Idea.
204 UserAlias => "* $setup->{user}",
205
206 UserDirRoot => 'on',
207 },
208 },
209
210 IfModules => {
211 'mod_delay.c' => {
212 DelayEngine => 'off',
213 },
214 },
215 };
216
217 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
218 $config);
219
220 # Open pipes, for use between the parent and child processes. Specifically,
221 # the child will indicate when it's done with its test by writing a message
222 # to the parent.
223 my ($rfh, $wfh);
224 unless (pipe($rfh, $wfh)) {
225 die("Can't open pipe: $!");
226 }
227
228 my $ex;
229
230 # Fork child
231 $self->handle_sigchld();
232 defined(my $pid = fork()) or die("Can't fork: $!");
233 if ($pid) {
234 eval {
235 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
236 $client->login('../../././/./../ftp', 'NoPasswordNeededHereForAnon');
237 $client->quit();
238 };
239 if ($@) {
240 $ex = $@;
241 }
242
243 $wfh->print("done\n");
244 $wfh->flush();
245
246 } else {
247 eval { server_wait($setup->{config_file}, $rfh) };
248 if ($@) {
249 warn($@);
250 exit 1;
251 }
252
253 exit 0;
254 }
255
256 # Stop server
257 server_stop($setup->{pid_file});
258 $self->assert_child_ok($pid);
259
260 eval {
261 if (open(my $fh, "< $setup->{log_file}")) {
262 my $ok = 0;
263
264 while (my $line = <$fh>) {
265 chomp($line);
266
267 if ($ENV{TEST_VERBOSE}) {
268 print STDERR "# $line\n";
269 }
270
271 if ($line =~ /Preparing to chroot to directory '(.*)?'/) {
272 my $chroot_path = $1;
273
274 my $expected = $test_dir;
275 if ($^O eq 'darwin') {
276 # MacOSX-specific hack
277 $expected = '/private' . $expected;
278 }
279
280 if ($chroot_path eq $expected) {
281 $ok = 1;
282 last;
283 }
284 }
285 }
286
287 close($fh);
288 $self->assert($ok, test_msg("Did not see expected chroot directory path"));
289
290 } else {
291 die("Can't read $setup->{log_file}: $!");
292 }
293 };
294 if ($@) {
295 $ex = $@;
296 }
297
298 test_cleanup($setup->{log_file}, $ex);
299 }
300
301 1;