Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::UseFtpUsers;
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 useftpusers_off_unlisted_user => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 useftpusers_off_listed_user => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 useftpusers_on_unlisted_user => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 useftpusers_on_listed_user => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37 };
38
39 sub new {
40 return shift()->SUPER::new(@_);
41 }
42
43 sub list_tests {
44 # These tests require that the test system have an /etc/ftpusers file.
45 unless (-e '/etc/ftpusers') {
46 if ($ENV{TEST_VERBOSE}) {
47 print STDERR "No /etc/ftpusers file present, skipping UseFtpUsers tests\n";
48 }
49
50 return qw(testsuite_empty_test);
51 }
52
53 return testsuite_get_runnable_tests($TESTS);
54 }
55
56 sub get_user {
57 my $use_unlisted_user = shift;
58 my $users = {};
59
60 if (open(my $fh, "< /etc/ftpusers")) {
61 while (my $line = <$fh>) {
62 chomp($line);
63
64 next if $line =~ /^#/;
65 next if $line =~ /^\s+$/;
66 next if length($line) == 0;
67
68 $users->{$line} = 1;
69 }
70
71 close($fh);
72
73 } else {
74 die("Can't read /etc/ftpusers: $!");
75 }
76
77 if ($use_unlisted_user) {
78 my $unlisted_users = [qw(proftpd test ftp foobarbazquxxquzz)];
79
80 foreach my $unlisted_user (@$unlisted_users) {
81 unless (defined($users->{$unlisted_user})) {
82 return $unlisted_user;
83 }
84 }
85
86 return "unknown";
87
88 } else {
89 return (keys(%$users))[0];
90 }
91 }
92
93 sub useftpusers_off_unlisted_user {
94 my $self = shift;
95 my $tmpdir = $self->{tmpdir};
96
97 my $config_file = "$tmpdir/config.conf";
98 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
99 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
100
101 my $log_file = File::Spec->rel2abs('tests.log');
102
103 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
104 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
105
106 my $passwd = 'test';
107 my $home_dir = File::Spec->rel2abs($tmpdir);
108 my $uid = 500;
109 my $gid = 500;
110
111 # Make sure that, if we're running as root, that the home directory has
112 # permissions/privs set for the account we create
113 if ($< == 0) {
114 unless (chmod(0755, $home_dir)) {
115 die("Can't set perms on $home_dir to 0755: $!");
116 }
117
118 unless (chown($uid, $gid, $home_dir)) {
119 die("Can't set owner of $home_dir to $uid/$gid: $!");
120 }
121 }
122
123 my $user = get_user(1);
124
125 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
126 '/bin/bash');
127 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
128
129 my $config = {
130 PidFile => $pid_file,
131 ScoreboardFile => $scoreboard_file,
132 SystemLog => $log_file,
133
134 AuthUserFile => $auth_user_file,
135 AuthGroupFile => $auth_group_file,
136 AuthOrder => 'mod_auth_file.c',
137
138 UseFtpUsers => 'off',
139
140 IfModules => {
141 'mod_delay.c' => {
142 DelayEngine => 'off',
143 },
144 },
145 };
146
147 my ($port, $config_user, $config_group) = config_write($config_file, $config);
148
149 # Open pipes, for use between the parent and child processes. Specifically,
150 # the child will indicate when it's done with its test by writing a message
151 # to the parent.
152 my ($rfh, $wfh);
153 unless (pipe($rfh, $wfh)) {
154 die("Can't open pipe: $!");
155 }
156
157 my $ex;
158
159 # Fork child
160 $self->handle_sigchld();
161 defined(my $pid = fork()) or die("Can't fork: $!");
162 if ($pid) {
163 eval {
164 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
165
166 $client->login($user, $passwd);
167 };
168
169 if ($@) {
170 $ex = $@;
171 }
172
173 $wfh->print("done\n");
174 $wfh->flush();
175
176 } else {
177 eval { server_wait($config_file, $rfh) };
178 if ($@) {
179 warn($@);
180 exit 1;
181 }
182
183 exit 0;
184 }
185
186 # Stop server
187 server_stop($pid_file);
188
189 $self->assert_child_ok($pid);
190
191 if ($ex) {
192 die($ex);
193 }
194
195 unlink($log_file);
196 }
197
198 sub useftpusers_off_listed_user {
199 my $self = shift;
200 my $tmpdir = $self->{tmpdir};
201
202 my $config_file = "$tmpdir/config.conf";
203 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
204 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
205
206 my $log_file = File::Spec->rel2abs('tests.log');
207
208 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
209 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
210
211 my $passwd = 'test';
212 my $home_dir = File::Spec->rel2abs($tmpdir);
213 my $uid = 500;
214 my $gid = 500;
215
216 # Make sure that, if we're running as root, that the home directory has
217 # permissions/privs set for the account we create
218 if ($< == 0) {
219 unless (chmod(0755, $home_dir)) {
220 die("Can't set perms on $home_dir to 0755: $!");
221 }
222
223 unless (chown($uid, $gid, $home_dir)) {
224 die("Can't set owner of $home_dir to $uid/$gid: $!");
225 }
226 }
227
228 my $user = get_user(0);
229
230 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
231 '/bin/bash');
232 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
233
234 my $config = {
235 PidFile => $pid_file,
236 ScoreboardFile => $scoreboard_file,
237 SystemLog => $log_file,
238
239 AuthUserFile => $auth_user_file,
240 AuthGroupFile => $auth_group_file,
241 AuthOrder => 'mod_auth_file.c',
242
243 UseFtpUsers => 'off',
244
245 IfModules => {
246 'mod_delay.c' => {
247 DelayEngine => 'off',
248 },
249 },
250 };
251
252 my ($port, $config_user, $config_group) = config_write($config_file, $config);
253
254 # Open pipes, for use between the parent and child processes. Specifically,
255 # the child will indicate when it's done with its test by writing a message
256 # to the parent.
257 my ($rfh, $wfh);
258 unless (pipe($rfh, $wfh)) {
259 die("Can't open pipe: $!");
260 }
261
262 my $ex;
263
264 # Fork child
265 $self->handle_sigchld();
266 defined(my $pid = fork()) or die("Can't fork: $!");
267 if ($pid) {
268 eval {
269 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
270
271 $client->login($user, $passwd);
272 };
273
274 if ($@) {
275 $ex = $@;
276 }
277
278 $wfh->print("done\n");
279 $wfh->flush();
280
281 } else {
282 eval { server_wait($config_file, $rfh) };
283 if ($@) {
284 warn($@);
285 exit 1;
286 }
287
288 exit 0;
289 }
290
291 # Stop server
292 server_stop($pid_file);
293
294 $self->assert_child_ok($pid);
295
296 if ($ex) {
297 die($ex);
298 }
299
300 unlink($log_file);
301 }
302
303 sub useftpusers_on_unlisted_user {
304 my $self = shift;
305 my $tmpdir = $self->{tmpdir};
306
307 my $config_file = "$tmpdir/config.conf";
308 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
309 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
310
311 my $log_file = File::Spec->rel2abs('tests.log');
312
313 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
314 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
315
316 my $passwd = 'test';
317 my $home_dir = File::Spec->rel2abs($tmpdir);
318 my $uid = 500;
319 my $gid = 500;
320
321 # Make sure that, if we're running as root, that the home directory has
322 # permissions/privs set for the account we create
323 if ($< == 0) {
324 unless (chmod(0755, $home_dir)) {
325 die("Can't set perms on $home_dir to 0755: $!");
326 }
327
328 unless (chown($uid, $gid, $home_dir)) {
329 die("Can't set owner of $home_dir to $uid/$gid: $!");
330 }
331 }
332
333 my $user = get_user(1);
334
335 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
336 '/bin/bash');
337 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
338
339 my $config = {
340 PidFile => $pid_file,
341 ScoreboardFile => $scoreboard_file,
342 SystemLog => $log_file,
343
344 AuthUserFile => $auth_user_file,
345 AuthGroupFile => $auth_group_file,
346 AuthOrder => 'mod_auth_file.c',
347
348 UseFtpUsers => 'on',
349
350 IfModules => {
351 'mod_delay.c' => {
352 DelayEngine => 'off',
353 },
354 },
355 };
356
357 my ($port, $config_user, $config_group) = config_write($config_file, $config);
358
359 # Open pipes, for use between the parent and child processes. Specifically,
360 # the child will indicate when it's done with its test by writing a message
361 # to the parent.
362 my ($rfh, $wfh);
363 unless (pipe($rfh, $wfh)) {
364 die("Can't open pipe: $!");
365 }
366
367 my $ex;
368
369 # Fork child
370 $self->handle_sigchld();
371 defined(my $pid = fork()) or die("Can't fork: $!");
372 if ($pid) {
373 eval {
374 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
375
376 $client->login($user, $passwd);
377 };
378
379 if ($@) {
380 $ex = $@;
381 }
382
383 $wfh->print("done\n");
384 $wfh->flush();
385
386 } else {
387 eval { server_wait($config_file, $rfh) };
388 if ($@) {
389 warn($@);
390 exit 1;
391 }
392
393 exit 0;
394 }
395
396 # Stop server
397 server_stop($pid_file);
398
399 $self->assert_child_ok($pid);
400
401 if ($ex) {
402 die($ex);
403 }
404
405 unlink($log_file);
406 }
407
408 sub useftpusers_on_listed_user {
409 my $self = shift;
410 my $tmpdir = $self->{tmpdir};
411
412 my $config_file = "$tmpdir/config.conf";
413 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
414 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
415
416 my $log_file = File::Spec->rel2abs('tests.log');
417
418 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
419 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
420
421 my $passwd = 'test';
422 my $home_dir = File::Spec->rel2abs($tmpdir);
423 my $uid = 500;
424 my $gid = 500;
425
426 # Make sure that, if we're running as root, that the home directory has
427 # permissions/privs set for the account we create
428 if ($< == 0) {
429 unless (chmod(0755, $home_dir)) {
430 die("Can't set perms on $home_dir to 0755: $!");
431 }
432
433 unless (chown($uid, $gid, $home_dir)) {
434 die("Can't set owner of $home_dir to $uid/$gid: $!");
435 }
436 }
437
438 my $user = get_user(0);
439
440 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
441 '/bin/bash');
442 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
443
444 my $config = {
445 PidFile => $pid_file,
446 ScoreboardFile => $scoreboard_file,
447 SystemLog => $log_file,
448
449 AuthUserFile => $auth_user_file,
450 AuthGroupFile => $auth_group_file,
451 AuthOrder => 'mod_auth_file.c',
452
453 UseFtpUsers => 'on',
454
455 IfModules => {
456 'mod_delay.c' => {
457 DelayEngine => 'off',
458 },
459 },
460 };
461
462 my ($port, $config_user, $config_group) = config_write($config_file, $config);
463
464 # Open pipes, for use between the parent and child processes. Specifically,
465 # the child will indicate when it's done with its test by writing a message
466 # to the parent.
467 my ($rfh, $wfh);
468 unless (pipe($rfh, $wfh)) {
469 die("Can't open pipe: $!");
470 }
471
472 my $ex;
473
474 # Fork child
475 $self->handle_sigchld();
476 defined(my $pid = fork()) or die("Can't fork: $!");
477 if ($pid) {
478 eval {
479 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
480
481 my ($resp_code, $resp_msg);
482
483 eval { $client->login($user, $passwd) };
484 unless ($@) {
485 die("Login succeeded unexpectedly");
486
487 } else {
488 $resp_code = $client->response_code();
489 $resp_msg = $client->response_msg();
490 }
491
492 my $expected;
493
494 $expected = 530;
495 $self->assert($expected == $resp_code,
496 test_msg("Expected $expected, got $resp_code"));
497
498 $expected = "Login incorrect.";
499 $self->assert($expected eq $resp_msg,
500 test_msg("Expected '$expected', got '$resp_msg'"));
501 };
502
503 if ($@) {
504 $ex = $@;
505 }
506
507 $wfh->print("done\n");
508 $wfh->flush();
509
510 } else {
511 eval { server_wait($config_file, $rfh) };
512 if ($@) {
513 warn($@);
514 exit 1;
515 }
516
517 exit 0;
518 }
519
520 # Stop server
521 server_stop($pid_file);
522
523 $self->assert_child_ok($pid);
524
525 if ($ex) {
526 die($ex);
527 }
528
529 unlink($log_file);
530 }
531
532 1;