Debian Codegraph @

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

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