Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/Limit/LOGIN.pm

raw
1 package ProFTPD::Tests::Config::Limit::LOGIN;
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 login_limit_ip_glob_range_bug3484 => {
19 order => ++$order,
20 test_class => [qw(bug forking inprogress)],
21 },
22
23 login_limit_allowgroup_backslash => {
24 order => ++$order,
25 test_class => [qw(bug forking)],
26 },
27
28 login_limit_multiple_sections => {
29 order => ++$order,
30 test_class => [qw(bug 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 login_limit_ip_glob_range_bug3484 {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46
47 my $config_file = "$tmpdir/limit.conf";
48 my $pid_file = File::Spec->rel2abs("$tmpdir/limit.pid");
49 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/limit.scoreboard");
50
51 my $log_file = test_get_logfile();
52
53 my $auth_user_file = File::Spec->rel2abs("$tmpdir/limit.passwd");
54 my $auth_group_file = File::Spec->rel2abs("$tmpdir/limit.group");
55
56 my $user = 'proftpd';
57 my $passwd = 'test';
58 my $group ='ftpd';
59 my $home_dir = File::Spec->rel2abs($tmpdir);
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, $home_dir)) {
67 die("Can't set perms on $home_dir to 0755: $!");
68 }
69
70 unless (chown($uid, $gid, $home_dir)) {
71 die("Can't set owner of $home_dir to $uid/$gid: $!");
72 }
73 }
74
75 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
76 '/bin/bash');
77 auth_group_write($auth_group_file, $group, $gid, $user);
78
79 # See http://forums.proftpd.org/smf/index.php/topic,4774.0.html
80
81 my $config = {
82 PidFile => $pid_file,
83 ScoreboardFile => $scoreboard_file,
84 SystemLog => $log_file,
85 TraceLog => $log_file,
86 Trace => 'netacl:20 dns:20',
87
88 AuthUserFile => $auth_user_file,
89 AuthGroupFile => $auth_group_file,
90 AuthOrder => 'mod_auth_file.c',
91
92 UseIPv6 => 'off',
93
94 IfModules => {
95 'mod_delay.c' => {
96 DelayEngine => 'off',
97 },
98 },
99
100 Limit => {
101 LOGIN => {
102 Deny => '127.0.0.[0-9]',
103 },
104 },
105
106 };
107
108 my ($port, $config_user, $config_group) = config_write($config_file, $config);
109
110 # Open pipes, for use between the parent and child processes. Specifically,
111 # the child will indicate when it's done with its test by writing a message
112 # to the parent.
113 my ($rfh, $wfh);
114 unless (pipe($rfh, $wfh)) {
115 die("Can't open pipe: $!");
116 }
117
118 my $ex;
119
120 # Fork child
121 $self->handle_sigchld();
122 defined(my $pid = fork()) or die("Can't fork: $!");
123 if ($pid) {
124 eval {
125 my $client;
126
127 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
128 undef, 0) };
129 unless ($@) {
130 die("Connect succeeded unexpectedly");
131 }
132
133 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
134
135 my $expected = 'Connection refused';
136 $self->assert(qr/$expected/, $conn_ex,
137 test_msg("Expected '$expected', got '$conn_ex'"));
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 login_limit_allowgroup_backslash {
173 my $self = shift;
174 my $tmpdir = $self->{tmpdir};
175
176 my $config_file = "$tmpdir/limit.conf";
177 my $pid_file = File::Spec->rel2abs("$tmpdir/limit.pid");
178 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/limit.scoreboard");
179
180 my $log_file = test_get_logfile();
181
182 my $auth_user_file = File::Spec->rel2abs("$tmpdir/limit.passwd");
183 my $auth_group_file = File::Spec->rel2abs("$tmpdir/limit.group");
184
185 # See:
186 # https://forums.proftpd.org/smf/index.php/topic,11304.0.html
187
188 my $user = 'proftpd';
189 my $passwd = 'test';
190 my $group = 'ftpd\group';
191 my $home_dir = File::Spec->rel2abs($tmpdir);
192 my $uid = 500;
193 my $gid = 500;
194
195 # Make sure that, if we're running as root, that the home directory has
196 # permissions/privs set for the account we create
197 if ($< == 0) {
198 unless (chmod(0755, $home_dir)) {
199 die("Can't set perms on $home_dir to 0755: $!");
200 }
201
202 unless (chown($uid, $gid, $home_dir)) {
203 die("Can't set owner of $home_dir to $uid/$gid: $!");
204 }
205 }
206
207 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
208 '/bin/bash');
209 auth_group_write($auth_group_file, $group, $gid, $user);
210
211 # See http://forums.proftpd.org/smf/index.php/topic,4774.0.html
212
213 my $config = {
214 PidFile => $pid_file,
215 ScoreboardFile => $scoreboard_file,
216 SystemLog => $log_file,
217 TraceLog => $log_file,
218 Trace => 'netacl:20 dns:20',
219
220 AuthUserFile => $auth_user_file,
221 AuthGroupFile => $auth_group_file,
222 AuthOrder => 'mod_auth_file.c',
223
224 UseIPv6 => 'off',
225
226 IfModules => {
227 'mod_delay.c' => {
228 DelayEngine => 'off',
229 },
230 },
231
232 Limit => {
233 LOGIN => {
234 AllowGroup => $group,
235 DenyAll => '',
236 },
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 $client->login($user, $passwd);
260 $client->quit();
261 };
262
263 if ($@) {
264 $ex = $@;
265 }
266
267 $wfh->print("done\n");
268 $wfh->flush();
269
270 } else {
271 eval { server_wait($config_file, $rfh) };
272 if ($@) {
273 warn($@);
274 exit 1;
275 }
276
277 exit 0;
278 }
279
280 # Stop server
281 server_stop($pid_file);
282
283 $self->assert_child_ok($pid);
284
285 if ($ex) {
286 test_append_logfile($log_file, $ex);
287 unlink($log_file);
288
289 die($ex);
290 }
291
292 unlink($log_file);
293 }
294
295 sub login_limit_multiple_sections {
296 my $self = shift;
297 my $tmpdir = $self->{tmpdir};
298
299 my $config_file = "$tmpdir/limit.conf";
300 my $pid_file = File::Spec->rel2abs("$tmpdir/limit.pid");
301 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/limit.scoreboard");
302
303 my $log_file = test_get_logfile();
304
305 my $auth_user_file = File::Spec->rel2abs("$tmpdir/limit.passwd");
306 my $auth_group_file = File::Spec->rel2abs("$tmpdir/limit.group");
307
308 my $user = 'proftpd';
309 my $passwd = 'test';
310 my $group ='ftpd';
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 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
328 '/bin/bash');
329 auth_group_write($auth_group_file, $group, $gid, $user);
330
331 # See http://forums.proftpd.org/smf/index.php/topic,4774.0.html
332
333 my $config = {
334 PidFile => $pid_file,
335 ScoreboardFile => $scoreboard_file,
336 SystemLog => $log_file,
337 TraceLog => $log_file,
338 Trace => 'netacl:20 dns:20',
339
340 AuthUserFile => $auth_user_file,
341 AuthGroupFile => $auth_group_file,
342 AuthOrder => 'mod_auth_file.c',
343
344 UseIPv6 => 'off',
345
346 IfModules => {
347 'mod_delay.c' => {
348 DelayEngine => 'off',
349 },
350 },
351 };
352
353 my ($port, $config_user, $config_group) = config_write($config_file, $config);
354
355 if (open(my $fh, ">> $config_file")) {
356 # When multiple <Limit LOGIN> sections are present, which one is used:
357 # first, or last?
358 print $fh <<EOC;
359 <Limit LOGIN>
360 Order allow,deny
361 Allow 127.0.0.1
362 DenyAll
363 </Limit>
364
365 <Limit LOGIN>
366 Order deny,allow
367 Deny 127.0.0.1
368 </Limit>
369
370 EOC
371 unless (close($fh)) {
372 die("Can't write $config_file: $!");
373 }
374 } else {
375 die("Can't open $config_file: $!");
376 }
377
378 # Open pipes, for use between the parent and child processes. Specifically,
379 # the child will indicate when it's done with its test by writing a message
380 # to the parent.
381 my ($rfh, $wfh);
382 unless (pipe($rfh, $wfh)) {
383 die("Can't open pipe: $!");
384 }
385
386 my $ex;
387
388 # Fork child
389 $self->handle_sigchld();
390 defined(my $pid = fork()) or die("Can't fork: $!");
391 if ($pid) {
392 eval {
393 my $client;
394
395 eval { $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
396 undef, 0) };
397 unless ($@) {
398 die("Connect succeeded unexpectedly");
399 }
400
401 my $conn_ex = ProFTPD::TestSuite::FTP::get_connect_exception();
402
403 # Looks like the last <Limit LOGIN> section wins.
404 my $expected = 'Connection refused';
405 $self->assert(qr/$expected/, $conn_ex,
406 test_msg("Expected '$expected', got '$conn_ex'"));
407 };
408
409 if ($@) {
410 $ex = $@;
411 }
412
413 $wfh->print("done\n");
414 $wfh->flush();
415
416 } else {
417 eval { server_wait($config_file, $rfh) };
418 if ($@) {
419 warn($@);
420 exit 1;
421 }
422
423 exit 0;
424 }
425
426 # Stop server
427 server_stop($pid_file);
428
429 $self->assert_child_ok($pid);
430
431 if ($ex) {
432 test_append_logfile($log_file, $ex);
433 unlink($log_file);
434
435 die($ex);
436 }
437
438 unlink($log_file);
439 }
440
441 1;