Debian Codegraph @

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

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