Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::AnonRejectPasswords;
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 anon_reject_passwords_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 anon_reject_passwords_flags_nocase => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 anon_reject_passwords_not_and_flags_nocase => {
29 order => ++$order,
30 test_class => [qw(forking)],
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 anon_reject_passwords_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, $group) = config_get_identity();
57
58 my $anon_dir = File::Spec->rel2abs($tmpdir);
59 my $passwd = 'foobar';
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, $anon_dir)) {
67 die("Can't set perms on $anon_dir to 0755: $!");
68 }
69
70 unless (chown($uid, $gid, $anon_dir)) {
71 die("Can't set owner of $anon_dir to $uid/$gid: $!");
72 }
73 }
74
75 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid,
76 '/tmp', '/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 TraceLog => $log_file,
84 Trace => 'DEFAULT:10 privs:10',
85
86 AuthUserFile => $auth_user_file,
87 AuthGroupFile => $auth_group_file,
88 AuthOrder => 'mod_auth_file.c',
89
90 IfModules => {
91 'mod_delay.c' => {
92 DelayEngine => 'off',
93 },
94 },
95
96 Anonymous => {
97 $anon_dir => {
98 User => $user,
99 Group => $group,
100 UserAlias => "anonymous $user",
101 AnonRejectPasswords => 'spam',
102 RequireValidShell => 'off',
103 },
104 },
105 };
106
107 my ($port, $config_user, $config_group) = config_write($config_file, $config);
108
109 # Open pipes, for use between the parent and child processes. Specifically,
110 # the child will indicate when it's done with its test by writing a message
111 # to the parent.
112 my ($rfh, $wfh);
113 unless (pipe($rfh, $wfh)) {
114 die("Can't open pipe: $!");
115 }
116
117 my $ex;
118
119 # Fork child
120 $self->handle_sigchld();
121 defined(my $pid = fork()) or die("Can't fork: $!");
122 if ($pid) {
123 eval {
124 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
125
126 # First, try to login using an email address as a password
127 eval { $client->login($user, '[email protected]') };
128 unless ($@) {
129 die("Login succeeded unexpectedly");
130 }
131
132 my $resp_code = $client->response_code();
133 my $resp_msg = $client->response_msg();
134
135 my $expected;
136
137 $expected = 530;
138 $self->assert($expected == $resp_code,
139 test_msg("Expected response code $expected, got $resp_code"));
140
141 $expected = 'Login incorrect.';
142 $self->assert($expected eq $resp_msg,
143 test_msg("Expected response message '$expected', got '$resp_msg'"));
144
145 $client->quit();
146 };
147
148 if ($@) {
149 $ex = $@;
150 }
151
152 $wfh->print("done\n");
153 $wfh->flush();
154
155 } else {
156 eval { server_wait($config_file, $rfh) };
157 if ($@) {
158 warn($@);
159 exit 1;
160 }
161
162 exit 0;
163 }
164
165 # Stop server
166 server_stop($pid_file);
167
168 $self->assert_child_ok($pid);
169
170 if ($ex) {
171 die($ex);
172 }
173
174 unlink($log_file);
175 }
176
177 sub anon_reject_passwords_flags_nocase {
178 my $self = shift;
179 my $tmpdir = $self->{tmpdir};
180
181 my $config_file = "$tmpdir/config.conf";
182 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
183 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
184
185 my $log_file = File::Spec->rel2abs('tests.log');
186
187 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
188 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
189
190 my ($user, $group) = config_get_identity();
191
192 my $anon_dir = File::Spec->rel2abs($tmpdir);
193 my $passwd = 'foobar';
194 my $uid = 500;
195 my $gid = 500;
196
197 # Make sure that, if we're running as root, that the home directory has
198 # permissions/privs set for the account we create
199 if ($< == 0) {
200 unless (chmod(0755, $anon_dir)) {
201 die("Can't set perms on $anon_dir to 0755: $!");
202 }
203
204 unless (chown($uid, $gid, $anon_dir)) {
205 die("Can't set owner of $anon_dir to $uid/$gid: $!");
206 }
207 }
208
209 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid,
210 '/tmp', '/bin/bash');
211 auth_group_write($auth_group_file, $group, $gid, $user);
212
213 my $config = {
214 PidFile => $pid_file,
215 ScoreboardFile => $scoreboard_file,
216 SystemLog => $log_file,
217 TraceLog => $log_file,
218 Trace => 'DEFAULT:10 privs:10',
219
220 AuthUserFile => $auth_user_file,
221 AuthGroupFile => $auth_group_file,
222 AuthOrder => 'mod_auth_file.c',
223
224 IfModules => {
225 'mod_delay.c' => {
226 DelayEngine => 'off',
227 },
228 },
229
230 Anonymous => {
231 $anon_dir => {
232 User => $user,
233 Group => $group,
234 UserAlias => "anonymous $user",
235 AnonRejectPasswords => 'SPAM [NC]',
236 RequireValidShell => 'off',
237 },
238 },
239 };
240
241 my ($port, $config_user, $config_group) = config_write($config_file, $config);
242
243 # Open pipes, for use between the parent and child processes. Specifically,
244 # the child will indicate when it's done with its test by writing a message
245 # to the parent.
246 my ($rfh, $wfh);
247 unless (pipe($rfh, $wfh)) {
248 die("Can't open pipe: $!");
249 }
250
251 my $ex;
252
253 # Fork child
254 $self->handle_sigchld();
255 defined(my $pid = fork()) or die("Can't fork: $!");
256 if ($pid) {
257 eval {
258 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
259
260 # First, try to login using an email address as a password
261 eval { $client->login($user, '[email protected]') };
262 unless ($@) {
263 die("Login succeeded unexpectedly");
264 }
265
266 my $resp_code = $client->response_code();
267 my $resp_msg = $client->response_msg();
268
269 my $expected;
270
271 $expected = 530;
272 $self->assert($expected == $resp_code,
273 test_msg("Expected response code $expected, got $resp_code"));
274
275 $expected = 'Login incorrect.';
276 $self->assert($expected eq $resp_msg,
277 test_msg("Expected response message '$expected', got '$resp_msg'"));
278
279 $client->quit();
280 };
281
282 if ($@) {
283 $ex = $@;
284 }
285
286 $wfh->print("done\n");
287 $wfh->flush();
288
289 } else {
290 eval { server_wait($config_file, $rfh) };
291 if ($@) {
292 warn($@);
293 exit 1;
294 }
295
296 exit 0;
297 }
298
299 # Stop server
300 server_stop($pid_file);
301
302 $self->assert_child_ok($pid);
303
304 if ($ex) {
305 die($ex);
306 }
307
308 unlink($log_file);
309 }
310
311 sub anon_reject_passwords_not_and_flags_nocase {
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 = File::Spec->rel2abs('tests.log');
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, $group) = config_get_identity();
325
326 my $anon_dir = File::Spec->rel2abs($tmpdir);
327 my $passwd = 'foobar';
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, $anon_dir)) {
335 die("Can't set perms on $anon_dir to 0755: $!");
336 }
337
338 unless (chown($uid, $gid, $anon_dir)) {
339 die("Can't set owner of $anon_dir to $uid/$gid: $!");
340 }
341 }
342
343 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid,
344 '/tmp', '/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 TraceLog => $log_file,
352 Trace => 'DEFAULT:10 privs:10',
353
354 AuthUserFile => $auth_user_file,
355 AuthGroupFile => $auth_group_file,
356 AuthOrder => 'mod_auth_file.c',
357
358 IfModules => {
359 'mod_delay.c' => {
360 DelayEngine => 'off',
361 },
362 },
363
364 Anonymous => {
365 $anon_dir => {
366 User => $user,
367 Group => $group,
368 UserAlias => "anonymous $user",
369
370 # See: http://www.regular-expressions.info/email.html
371 AnonRejectPasswords => '!^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$ [NC]',
372 RequireValidShell => 'off',
373 },
374 },
375 };
376
377 my ($port, $config_user, $config_group) = config_write($config_file, $config);
378
379 # Open pipes, for use between the parent and child processes. Specifically,
380 # the child will indicate when it's done with its test by writing a message
381 # to the parent.
382 my ($rfh, $wfh);
383 unless (pipe($rfh, $wfh)) {
384 die("Can't open pipe: $!");
385 }
386
387 my $ex;
388
389 # Fork child
390 $self->handle_sigchld();
391 defined(my $pid = fork()) or die("Can't fork: $!");
392 if ($pid) {
393 eval {
394 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
395
396 # First, try to login using an email address as a password
397 eval { $client->login($user, 'ftp@nospam') };
398 unless ($@) {
399 die("Login succeeded unexpectedly");
400 }
401
402 my $resp_code = $client->response_code();
403 my $resp_msg = $client->response_msg();
404
405 my $expected;
406
407 $expected = 530;
408 $self->assert($expected == $resp_code,
409 test_msg("Expected response code $expected, got $resp_code"));
410
411 $expected = 'Login incorrect.';
412 $self->assert($expected eq $resp_msg,
413 test_msg("Expected response message '$expected', got '$resp_msg'"));
414
415 $client->quit();
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 die($ex);
442 }
443
444 unlink($log_file);
445 }
446
447 1;