Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::RewriteHome;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9
10 use ProFTPD::TestSuite::FTP;
11 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13 $| = 1;
14
15 my $order = 0;
16
17 my $TESTS = {
18 rewritehome_ok => {
19 order => ++$order,
20 test_class => [qw(forking mod_rewrite)],
21 },
22
23 rewritehome_bad_home => {
24 order => ++$order,
25 test_class => [qw(forking mod_rewrite)],
26 },
27
28 rewritehome_chroot_bug3348 => {
29 order => ++$order,
30 test_class => [qw(bug forking mod_rewrite rootprivs)],
31 },
32
33 };
34
35 sub new {
36 return shift()->SUPER::new(@_);
37 }
38
39 sub list_tests {
40 return testsuite_get_runnable_tests($TESTS);
41 }
42
43 sub rewritehome_ok {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46
47 my $config_file = "$tmpdir/config.conf";
48 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
49 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
50
51 my $log_file = File::Spec->rel2abs('tests.log');
52
53 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
54 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
55
56 my $user = 'proftpd';
57 my $passwd = 'test';
58 my $group = 'ftpd';
59 my $home_dir = File::Spec->rel2abs($tmpdir);
60 my $uid = 500;
61 my $gid = 500;
62
63 # Make sure that, if we're running as root, that the home directory has
64 # permissions/privs set for the account we create
65 if ($< == 0) {
66 unless (chmod(0755, $home_dir)) {
67 die("Can't set perms on $home_dir to 0755: $!");
68 }
69
70 unless (chown($uid, $gid, $home_dir)) {
71 die("Can't set owner of $home_dir to $uid/$gid: $!");
72 }
73 }
74
75 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, '/foo/bar/baz',
76 '/bin/bash');
77 auth_group_write($auth_group_file, $group, $gid, $user);
78
79 my $config = {
80 PidFile => $pid_file,
81 ScoreboardFile => $scoreboard_file,
82 SystemLog => $log_file,
83
84 AuthUserFile => $auth_user_file,
85 AuthGroupFile => $auth_group_file,
86 AuthOrder => 'mod_auth_file.c',
87
88 IfModules => {
89 'mod_delay.c' => {
90 DelayEngine => 'off',
91 },
92
93 'mod_rewrite.c' => [
94 'RewriteHome on',
95 'RewriteEngine on',
96 "RewriteLog $log_file",
97 'RewriteCondition %m REWRITE_HOME',
98 "RewriteRule ^(.*) $home_dir",
99 ],
100 },
101 };
102
103 my ($port, $config_user, $config_group) = config_write($config_file, $config);
104
105 # Open pipes, for use between the parent and child processes. Specifically,
106 # the child will indicate when it's done with its test by writing a message
107 # to the parent.
108 my ($rfh, $wfh);
109 unless (pipe($rfh, $wfh)) {
110 die("Can't open pipe: $!");
111 }
112
113 my $ex;
114
115 # Fork child
116 $self->handle_sigchld();
117 defined(my $pid = fork()) or die("Can't fork: $!");
118 if ($pid) {
119 eval {
120 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
121 $client->login($user, $passwd);
122
123 my ($resp_code, $resp_msg) = $client->pwd();
124
125 my $expected;
126
127 $expected = 257;
128 $self->assert($expected == $resp_code,
129 test_msg("Expected $expected, got $resp_code"));
130
131 $expected = "\"$home_dir\" is the current directory";
132 $self->assert($expected eq $resp_msg,
133 test_msg("Expected '$expected', got '$resp_msg'"));
134 };
135
136 if ($@) {
137 $ex = $@;
138 }
139
140 $wfh->print("done\n");
141 $wfh->flush();
142
143 } else {
144 eval { server_wait($config_file, $rfh) };
145 if ($@) {
146 warn($@);
147 exit 1;
148 }
149
150 exit 0;
151 }
152
153 # Stop server
154 server_stop($pid_file);
155
156 $self->assert_child_ok($pid);
157
158 if ($ex) {
159 die($ex);
160 }
161
162 unlink($log_file);
163 }
164
165 sub rewritehome_bad_home {
166 my $self = shift;
167 my $tmpdir = $self->{tmpdir};
168
169 my $config_file = "$tmpdir/config.conf";
170 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
171 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
172
173 my $log_file = File::Spec->rel2abs('tests.log');
174
175 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
176 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
177
178 my $user = 'proftpd';
179 my $passwd = 'test';
180 my $group = 'ftpd';
181 my $home_dir = File::Spec->rel2abs($tmpdir);
182 my $uid = 500;
183 my $gid = 500;
184
185 # Make sure that, if we're running as root, that the home directory has
186 # permissions/privs set for the account we create
187 if ($< == 0) {
188 unless (chmod(0755, $home_dir)) {
189 die("Can't set perms on $home_dir to 0755: $!");
190 }
191
192 unless (chown($uid, $gid, $home_dir)) {
193 die("Can't set owner of $home_dir to $uid/$gid: $!");
194 }
195 }
196
197 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
198 '/bin/bash');
199 auth_group_write($auth_group_file, $group, $gid, $user);
200
201 my $config = {
202 PidFile => $pid_file,
203 ScoreboardFile => $scoreboard_file,
204 SystemLog => $log_file,
205
206 AuthUserFile => $auth_user_file,
207 AuthGroupFile => $auth_group_file,
208 AuthOrder => 'mod_auth_file.c',
209
210 IfModules => {
211 'mod_delay.c' => {
212 DelayEngine => 'off',
213 },
214
215 'mod_rewrite.c' => [
216 'RewriteHome on',
217 'RewriteEngine on',
218 "RewriteLog $log_file",
219 'RewriteCondition %m REWRITE_HOME',
220 'RewriteRule ^(.*) /foo/bar/baz/quxx/quzz/alef/bet',
221 ],
222 },
223 };
224
225 my ($port, $config_user, $config_group) = config_write($config_file, $config);
226
227 # Open pipes, for use between the parent and child processes. Specifically,
228 # the child will indicate when it's done with its test by writing a message
229 # to the parent.
230 my ($rfh, $wfh);
231 unless (pipe($rfh, $wfh)) {
232 die("Can't open pipe: $!");
233 }
234
235 my $ex;
236
237 # Fork child
238 $self->handle_sigchld();
239 defined(my $pid = fork()) or die("Can't fork: $!");
240 if ($pid) {
241 eval {
242 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
243
244 # Because we rewrote the home directory to one that doesn't exist,
245 # the login should not succeed.
246 eval { $client->login($user, $passwd) };
247 unless ($@) {
248 die("Login succeeded expectedly");
249 }
250 };
251
252 if ($@) {
253 $ex = $@;
254 }
255
256 $wfh->print("done\n");
257 $wfh->flush();
258
259 } else {
260 eval { server_wait($config_file, $rfh) };
261 if ($@) {
262 warn($@);
263 exit 1;
264 }
265
266 exit 0;
267 }
268
269 # Stop server
270 server_stop($pid_file);
271
272 $self->assert_child_ok($pid);
273
274 if ($ex) {
275 die($ex);
276 }
277
278 unlink($log_file);
279 }
280
281 sub rewritehome_chroot_bug3348 {
282 my $self = shift;
283 my $tmpdir = $self->{tmpdir};
284
285 my $config_file = "$tmpdir/config.conf";
286 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
287 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
288
289 my $log_file = File::Spec->rel2abs('tests.log');
290
291 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
292 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
293
294 my $user = 'proftpd';
295 my $passwd = 'test';
296 my $group = 'ftpd';
297 my $home_dir = File::Spec->rel2abs($tmpdir);
298 my $uid = 500;
299 my $gid = 500;
300
301 # Make sure that, if we're running as root, that the home directory has
302 # permissions/privs set for the account we create
303 if ($< == 0) {
304 unless (chmod(0755, $home_dir)) {
305 die("Can't set perms on $home_dir to 0755: $!");
306 }
307
308 unless (chown($uid, $gid, $home_dir)) {
309 die("Can't set owner of $home_dir to $uid/$gid: $!");
310 }
311 }
312
313 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, '/foo/bar/baz',
314 '/bin/bash');
315 auth_group_write($auth_group_file, $group, $gid, $user);
316
317 my $config = {
318 PidFile => $pid_file,
319 ScoreboardFile => $scoreboard_file,
320 SystemLog => $log_file,
321
322 AuthUserFile => $auth_user_file,
323 AuthGroupFile => $auth_group_file,
324 AuthOrder => 'mod_auth_file.c',
325
326 DefaultRoot => '~/',
327
328 IfModules => {
329 'mod_delay.c' => {
330 DelayEngine => 'off',
331 },
332
333 'mod_rewrite.c' => [
334 'RewriteHome on',
335 'RewriteEngine on',
336 "RewriteLog $log_file",
337 'RewriteCondition %m REWRITE_HOME',
338 "RewriteRule ^(.*) $home_dir",
339 ],
340 },
341 };
342
343 my ($port, $config_user, $config_group) = config_write($config_file, $config);
344
345 # Open pipes, for use between the parent and child processes. Specifically,
346 # the child will indicate when it's done with its test by writing a message
347 # to the parent.
348 my ($rfh, $wfh);
349 unless (pipe($rfh, $wfh)) {
350 die("Can't open pipe: $!");
351 }
352
353 my $ex;
354
355 # Fork child
356 $self->handle_sigchld();
357 defined(my $pid = fork()) or die("Can't fork: $!");
358 if ($pid) {
359 eval {
360 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
361 $client->login($user, $passwd);
362
363 my ($resp_code, $resp_msg) = $client->pwd();
364
365 my $expected;
366
367 $expected = 257;
368 $self->assert($expected == $resp_code,
369 test_msg("Expected $expected, got $resp_code"));
370
371 $expected = "\"/\" is the current directory";
372 $self->assert($expected eq $resp_msg,
373 test_msg("Expected '$expected', got '$resp_msg'"));
374 };
375
376 if ($@) {
377 $ex = $@;
378 }
379
380 $wfh->print("done\n");
381 $wfh->flush();
382
383 } else {
384 eval { server_wait($config_file, $rfh) };
385 if ($@) {
386 warn($@);
387 exit 1;
388 }
389
390 exit 0;
391 }
392
393 # Stop server
394 server_stop($pid_file);
395
396 $self->assert_child_ok($pid);
397
398 if ($ex) {
399 die($ex);
400 }
401
402 unlink($log_file);
403 }
404
405 1;