Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::RootRevoke;
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 rootrevoke_on_active_transfer => {
20 order => ++$order,
21 test_class => [qw(forking rootprivs)],
22 },
23
24 rootrevoke_on_active_transfer_nonpriv_port => {
25 order => ++$order,
26 test_class => [qw(forking rootprivs)],
27 },
28
29 rootrevoke_usenoncompliantactivetransfer_active_transfer_bug3731 => {
30 order => ++$order,
31 test_class => [qw(forking rootprivs)],
32 },
33
34 };
35
36 sub new {
37 return shift()->SUPER::new(@_);
38 }
39
40 sub list_tests {
41 return testsuite_get_runnable_tests($TESTS);
42 }
43
44 sub rootrevoke_on_active_transfer {
45 my $self = shift;
46 my $tmpdir = $self->{tmpdir};
47
48 my $config_file = "$tmpdir/config.conf";
49 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
50 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
51
52 my $log_file = test_get_logfile();
53
54 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
55 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
56
57 my $user = 'proftpd';
58 my $passwd = 'test';
59 my $group = 'ftpd';
60 my $home_dir = File::Spec->rel2abs($tmpdir);
61 my $uid = 500;
62 my $gid = 500;
63
64 # Make sure that, if we're running as root, that the home directory has
65 # permissions/privs set for the account we create
66 if ($< == 0) {
67 unless (chmod(0755, $home_dir)) {
68 die("Can't set perms on $home_dir to 0755: $!");
69 }
70
71 unless (chown($uid, $gid, $home_dir)) {
72 die("Can't set owner of $home_dir to $uid/$gid: $!");
73 }
74 }
75
76 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
77 '/bin/bash');
78 auth_group_write($auth_group_file, $group, $gid, $user);
79
80 my $config = {
81 PidFile => $pid_file,
82 ScoreboardFile => $scoreboard_file,
83 SystemLog => $log_file,
84 Port => '21',
85
86 AuthUserFile => $auth_user_file,
87 AuthGroupFile => $auth_group_file,
88 AuthOrder => 'mod_auth_file.c',
89
90 RootRevoke => 'on',
91
92 IfModules => {
93 'mod_delay.c' => {
94 DelayEngine => 'off',
95 },
96 },
97 };
98
99 my ($port, $config_user, $config_group) = config_write($config_file, $config);
100
101 # Open pipes, for use between the parent and child processes. Specifically,
102 # the child will indicate when it's done with its test by writing a message
103 # to the parent.
104 my ($rfh, $wfh);
105 unless (pipe($rfh, $wfh)) {
106 die("Can't open pipe: $!");
107 }
108
109 my $ex;
110
111 # Fork child
112 $self->handle_sigchld();
113 defined(my $pid = fork()) or die("Can't fork: $!");
114 if ($pid) {
115 eval {
116 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1);
117 $client->login($user, $passwd);
118
119 my $conn = $client->list_raw();
120 if ($conn) {
121 die("LIST succeeded unexpectedly");
122 }
123
124 my $resp_code = $client->response_code();
125 my $resp_msg = $client->response_msg();
126
127 my $expected;
128
129 $expected = 500;
130 $self->assert($expected == $resp_code,
131 test_msg("Expected $expected, got $resp_code"));
132
133 $expected = "Unable to service PORT commands";
134 $self->assert($expected eq $resp_msg,
135 test_msg("Expected '$expected', got '$resp_msg'"));
136
137 $client->quit();
138 };
139
140 if ($@) {
141 $ex = $@;
142 }
143
144 $wfh->print("done\n");
145 $wfh->flush();
146
147 } else {
148 eval { server_wait($config_file, $rfh) };
149 if ($@) {
150 warn($@);
151 exit 1;
152 }
153
154 exit 0;
155 }
156
157 # Stop server
158 server_stop($pid_file);
159
160 $self->assert_child_ok($pid);
161
162 if ($ex) {
163 test_append_logfile($log_file, $ex);
164 unlink($log_file);
165
166 die($ex);
167 }
168
169 unlink($log_file);
170 }
171
172 sub rootrevoke_on_active_transfer_nonpriv_port {
173 my $self = shift;
174 my $tmpdir = $self->{tmpdir};
175
176 my $config_file = "$tmpdir/config.conf";
177 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
178 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
179
180 my $log_file = test_get_logfile();
181
182 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
183 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
184
185 my $user = 'proftpd';
186 my $passwd = 'test';
187 my $group = 'ftpd';
188 my $home_dir = File::Spec->rel2abs($tmpdir);
189 my $uid = 500;
190 my $gid = 500;
191
192 # Make sure that, if we're running as root, that the home directory has
193 # permissions/privs set for the account we create
194 if ($< == 0) {
195 unless (chmod(0755, $home_dir)) {
196 die("Can't set perms on $home_dir to 0755: $!");
197 }
198
199 unless (chown($uid, $gid, $home_dir)) {
200 die("Can't set owner of $home_dir to $uid/$gid: $!");
201 }
202 }
203
204 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
205 '/bin/bash');
206 auth_group_write($auth_group_file, $group, $gid, $user);
207
208 my $config = {
209 PidFile => $pid_file,
210 ScoreboardFile => $scoreboard_file,
211 SystemLog => $log_file,
212
213 AuthUserFile => $auth_user_file,
214 AuthGroupFile => $auth_group_file,
215 AuthOrder => 'mod_auth_file.c',
216
217 RootRevoke => 'on',
218
219 IfModules => {
220 'mod_delay.c' => {
221 DelayEngine => 'off',
222 },
223 },
224 };
225
226 my ($port, $config_user, $config_group) = config_write($config_file, $config);
227
228 # Open pipes, for use between the parent and child processes. Specifically,
229 # the child will indicate when it's done with its test by writing a message
230 # to the parent.
231 my ($rfh, $wfh);
232 unless (pipe($rfh, $wfh)) {
233 die("Can't open pipe: $!");
234 }
235
236 my $ex;
237
238 # Fork child
239 $self->handle_sigchld();
240 defined(my $pid = fork()) or die("Can't fork: $!");
241 if ($pid) {
242 eval {
243 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1);
244 $client->login($user, $passwd);
245
246 my $conn = $client->list_raw();
247 unless ($conn) {
248 die("LIST failed: " . $client->response_code() . " " .
249 $client->response_msg());
250 }
251
252 my $remote_port = $conn->peerport();
253
254 my $buf;
255 $conn->read($buf, 8192, 25);
256 eval { $conn->close() };
257
258 my $resp_code = $client->response_code();
259 my $resp_msg = $client->response_msg();
260
261 my $expected;
262
263 $expected = 226;
264 $self->assert($expected == $resp_code,
265 test_msg("Expected $expected, got $resp_code"));
266
267 $expected = "Transfer complete";
268 $self->assert($expected eq $resp_msg,
269 test_msg("Expected '$expected', got '$resp_msg'"));
270
271 $client->quit();
272
273 # Verify that the remote port is RFC-compliant
274 $expected = $port - 1;
275 $self->assert($remote_port == $expected,
276 test_msg("Expected remote port == $expected, got $remote_port"));
277 };
278
279 if ($@) {
280 $ex = $@;
281 }
282
283 $wfh->print("done\n");
284 $wfh->flush();
285
286 } else {
287 eval { server_wait($config_file, $rfh) };
288 if ($@) {
289 warn($@);
290 exit 1;
291 }
292
293 exit 0;
294 }
295
296 # Stop server
297 server_stop($pid_file);
298
299 $self->assert_child_ok($pid);
300
301 if ($ex) {
302 test_append_logfile($log_file, $ex);
303 unlink($log_file);
304
305 die($ex);
306 }
307
308 unlink($log_file);
309 }
310
311 sub rootrevoke_usenoncompliantactivetransfer_active_transfer_bug3731 {
312 my $self = shift;
313 my $tmpdir = $self->{tmpdir};
314
315 my $config_file = "$tmpdir/config.conf";
316 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
317 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
318
319 my $log_file = test_get_logfile();
320
321 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
322 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
323
324 my $user = 'proftpd';
325 my $passwd = 'test';
326 my $group = 'ftpd';
327 my $home_dir = File::Spec->rel2abs($tmpdir);
328 my $uid = 500;
329 my $gid = 500;
330
331 # Make sure that, if we're running as root, that the home directory has
332 # permissions/privs set for the account we create
333 if ($< == 0) {
334 unless (chmod(0755, $home_dir)) {
335 die("Can't set perms on $home_dir to 0755: $!");
336 }
337
338 unless (chown($uid, $gid, $home_dir)) {
339 die("Can't set owner of $home_dir to $uid/$gid: $!");
340 }
341 }
342
343 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
344 '/bin/bash');
345 auth_group_write($auth_group_file, $group, $gid, $user);
346
347 my $config = {
348 PidFile => $pid_file,
349 ScoreboardFile => $scoreboard_file,
350 SystemLog => $log_file,
351 Port => '21',
352
353 AuthUserFile => $auth_user_file,
354 AuthGroupFile => $auth_group_file,
355 AuthOrder => 'mod_auth_file.c',
356
357 RootRevoke => 'UseNonCompliantActiveTransfer',
358
359 IfModules => {
360 'mod_delay.c' => {
361 DelayEngine => 'off',
362 },
363 },
364 };
365
366 my ($port, $config_user, $config_group) = config_write($config_file, $config);
367
368 # Open pipes, for use between the parent and child processes. Specifically,
369 # the child will indicate when it's done with its test by writing a message
370 # to the parent.
371 my ($rfh, $wfh);
372 unless (pipe($rfh, $wfh)) {
373 die("Can't open pipe: $!");
374 }
375
376 my $ex;
377
378 # Fork child
379 $self->handle_sigchld();
380 defined(my $pid = fork()) or die("Can't fork: $!");
381 if ($pid) {
382 eval {
383 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1);
384 $client->login($user, $passwd);
385
386 my $conn = $client->list_raw();
387 unless ($conn) {
388 die("LIST failed: " . $client->response_code() . " " .
389 $client->response_msg());
390 }
391
392 my $remote_port = $conn->peerport();
393
394 my $buf;
395 $conn->read($buf, 8192, 25);
396 eval { $conn->close() };
397
398 my $resp_code = $client->response_code();
399 my $resp_msg = $client->response_msg();
400
401 my $expected;
402
403 $expected = 226;
404 $self->assert($expected == $resp_code,
405 test_msg("Expected $expected, got $resp_code"));
406
407 $expected = "Transfer complete";
408 $self->assert($expected eq $resp_msg,
409 test_msg("Expected '$expected', got '$resp_msg'"));
410
411 $client->quit();
412
413 # Verify that the remote port is a non-privileged port
414 $self->assert($remote_port >= 1024,
415 test_msg("Expected remote port >= 1024, got $remote_port"));
416 };
417
418 if ($@) {
419 $ex = $@;
420 }
421
422 $wfh->print("done\n");
423 $wfh->flush();
424
425 } else {
426 eval { server_wait($config_file, $rfh) };
427 if ($@) {
428 warn($@);
429 exit 1;
430 }
431
432 exit 0;
433 }
434
435 # Stop server
436 server_stop($pid_file);
437
438 $self->assert_child_ok($pid);
439
440 if ($ex) {
441 test_append_logfile($log_file, $ex);
442 unlink($log_file);
443
444 die($ex);
445 }
446
447 unlink($log_file);
448 }
449
450 1;