Debian Codegraph @

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

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