Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::AccessDenyMsg;
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 accessdenymsg_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 accessdenymsg_var_u_ok => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 accessdenymsg_limit_user => {
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 accessdenymsg_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 = 'proftpd';
57 my $passwd = 'test';
58 my $home_dir = File::Spec->rel2abs($tmpdir);
59 my $uid = 500;
60 my $gid = 500;
61
62 # Make sure that, if we're running as root, that the home directory has
63 # permissions/privs set for the account we create
64 if ($< == 0) {
65 unless (chmod(0755, $home_dir)) {
66 die("Can't set perms on $home_dir to 0755: $!");
67 }
68
69 unless (chown($uid, $gid, $home_dir)) {
70 die("Can't set owner of $home_dir to $uid/$gid: $!");
71 }
72 }
73
74 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
75 '/bin/bash');
76 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
77
78 my $access_deny_msg = '"Care to try again?"';
79
80 my $config = {
81 PidFile => $pid_file,
82 ScoreboardFile => $scoreboard_file,
83 SystemLog => $log_file,
84
85 AuthUserFile => $auth_user_file,
86 AuthGroupFile => $auth_group_file,
87 AuthOrder => 'mod_auth_file.c',
88
89 AccessDenyMsg => $access_deny_msg,
90
91 IfModules => {
92 'mod_delay.c' => {
93 DelayEngine => 'off',
94 },
95 },
96 };
97
98 my ($port, $config_user, $config_group) = config_write($config_file, $config);
99
100 # Open pipes, for use between the parent and child processes. Specifically,
101 # the child will indicate when it's done with its test by writing a message
102 # to the parent.
103 my ($rfh, $wfh);
104 unless (pipe($rfh, $wfh)) {
105 die("Can't open pipe: $!");
106 }
107
108 my $ex;
109
110 # Fork child
111 $self->handle_sigchld();
112 defined(my $pid = fork()) or die("Can't fork: $!");
113 if ($pid) {
114 eval {
115 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
116 eval { $client->login($user, 'yarr!') };
117 unless ($@) {
118 die("Unexpectedly logged in");
119 }
120
121 my $resp_code = $client->response_code();
122 my $resp_msg = $client->response_msg();
123
124 my $expected;
125
126 $expected = 530;
127 $self->assert($expected == $resp_code,
128 test_msg("Expected $expected, got $resp_code"));
129
130 $expected = "Care to try again?";
131 $self->assert($expected eq $resp_msg,
132 test_msg("Expected '$expected', got '$resp_msg'"));
133 };
134
135 if ($@) {
136 $ex = $@;
137 }
138
139 $wfh->print("done\n");
140 $wfh->flush();
141
142 } else {
143 eval { server_wait($config_file, $rfh) };
144 if ($@) {
145 warn($@);
146 exit 1;
147 }
148
149 exit 0;
150 }
151
152 # Stop server
153 server_stop($pid_file);
154
155 $self->assert_child_ok($pid);
156
157 if ($ex) {
158 die($ex);
159 }
160
161 unlink($log_file);
162 }
163
164 sub accessdenymsg_var_u_ok {
165 my $self = shift;
166 my $tmpdir = $self->{tmpdir};
167
168 my $config_file = "$tmpdir/config.conf";
169 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
170 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
171
172 my $log_file = File::Spec->rel2abs('tests.log');
173
174 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
175 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
176
177 my $user = 'proftpd';
178 my $passwd = 'test';
179 my $home_dir = File::Spec->rel2abs($tmpdir);
180 my $uid = 500;
181 my $gid = 500;
182
183 # Make sure that, if we're running as root, that the home directory has
184 # permissions/privs set for the account we create
185 if ($< == 0) {
186 unless (chmod(0755, $home_dir)) {
187 die("Can't set perms on $home_dir to 0755: $!");
188 }
189
190 unless (chown($uid, $gid, $home_dir)) {
191 die("Can't set owner of $home_dir to $uid/$gid: $!");
192 }
193 }
194
195 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
196 '/bin/bash');
197 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
198
199 my $access_deny_msg = '"Care to try again, %u?"';
200
201 my $config = {
202 PidFile => $pid_file,
203 ScoreboardFile => $scoreboard_file,
204 SystemLog => $log_file,
205
206 AuthUserFile => $auth_user_file,
207 AuthGroupFile => $auth_group_file,
208 AuthOrder => 'mod_auth_file.c',
209
210 AccessDenyMsg => $access_deny_msg,
211
212 IfModules => {
213 'mod_delay.c' => {
214 DelayEngine => 'off',
215 },
216 },
217 };
218
219 my ($port, $config_user, $config_group) = config_write($config_file, $config);
220
221 # Open pipes, for use between the parent and child processes. Specifically,
222 # the child will indicate when it's done with its test by writing a message
223 # to the parent.
224 my ($rfh, $wfh);
225 unless (pipe($rfh, $wfh)) {
226 die("Can't open pipe: $!");
227 }
228
229 my $ex;
230
231 # Fork child
232 $self->handle_sigchld();
233 defined(my $pid = fork()) or die("Can't fork: $!");
234 if ($pid) {
235 eval {
236 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
237 eval { $client->login($user, 'yarr!') };
238 unless ($@) {
239 die("Unexpectedly logged in");
240 }
241
242 my $resp_code = $client->response_code();
243 my $resp_msg = $client->response_msg();
244
245 my $expected;
246
247 $expected = 530;
248 $self->assert($expected == $resp_code,
249 test_msg("Expected $expected, got $resp_code"));
250
251 $expected = "Care to try again, $user?";
252 $self->assert($expected eq $resp_msg,
253 test_msg("Expected '$expected', got '$resp_msg'"));
254 };
255
256 if ($@) {
257 $ex = $@;
258 }
259
260 $wfh->print("done\n");
261 $wfh->flush();
262
263 } else {
264 eval { server_wait($config_file, $rfh) };
265 if ($@) {
266 warn($@);
267 exit 1;
268 }
269
270 exit 0;
271 }
272
273 # Stop server
274 server_stop($pid_file);
275
276 $self->assert_child_ok($pid);
277
278 if ($ex) {
279 die($ex);
280 }
281
282 unlink($log_file);
283 }
284
285 sub accessdenymsg_limit_user {
286 my $self = shift;
287 my $tmpdir = $self->{tmpdir};
288
289 my $config_file = "$tmpdir/config.conf";
290 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
291 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
292
293 my $log_file = File::Spec->rel2abs('tests.log');
294
295 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
296 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
297
298 my $user = 'proftpd';
299 my $passwd = 'test';
300 my $home_dir = File::Spec->rel2abs($tmpdir);
301 my $uid = 500;
302 my $gid = 500;
303
304 # Make sure that, if we're running as root, that the home directory has
305 # permissions/privs set for the account we create
306 if ($< == 0) {
307 unless (chmod(0755, $home_dir)) {
308 die("Can't set perms on $home_dir to 0755: $!");
309 }
310
311 unless (chown($uid, $gid, $home_dir)) {
312 die("Can't set owner of $home_dir to $uid/$gid: $!");
313 }
314 }
315
316 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
317 '/bin/bash');
318 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
319
320 my $access_deny_msg = '"Care to try again?"';
321
322 my $config = {
323 PidFile => $pid_file,
324 ScoreboardFile => $scoreboard_file,
325 SystemLog => $log_file,
326
327 AuthUserFile => $auth_user_file,
328 AuthGroupFile => $auth_group_file,
329 AuthOrder => 'mod_auth_file.c',
330
331 AccessDenyMsg => $access_deny_msg,
332
333 # Use a <Limit LOGIN> section to deny login access to this user, to
334 # trigger the AccessDenyMsg.
335 Limit => {
336 LOGIN => {
337 DenyUser => $user,
338 },
339 },
340
341 IfModules => {
342 'mod_delay.c' => {
343 DelayEngine => 'off',
344 },
345 },
346 };
347
348 my ($port, $config_user, $config_group) = config_write($config_file, $config);
349
350 # Open pipes, for use between the parent and child processes. Specifically,
351 # the child will indicate when it's done with its test by writing a message
352 # to the parent.
353 my ($rfh, $wfh);
354 unless (pipe($rfh, $wfh)) {
355 die("Can't open pipe: $!");
356 }
357
358 my $ex;
359
360 # Fork child
361 $self->handle_sigchld();
362 defined(my $pid = fork()) or die("Can't fork: $!");
363 if ($pid) {
364 eval {
365 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
366 eval { $client->login($user, $passwd) };
367 unless ($@) {
368 die("Unexpectedly logged in");
369 }
370
371 my $resp_code = $client->response_code();
372 my $resp_msg = $client->response_msg();
373
374 my $expected;
375
376 $expected = 530;
377 $self->assert($expected == $resp_code,
378 test_msg("Expected $expected, got $resp_code"));
379
380 $expected = "Care to try again?";
381 $self->assert($expected eq $resp_msg,
382 test_msg("Expected '$expected', got '$resp_msg'"));
383 };
384
385 if ($@) {
386 $ex = $@;
387 }
388
389 $wfh->print("done\n");
390 $wfh->flush();
391
392 } else {
393 eval { server_wait($config_file, $rfh) };
394 if ($@) {
395 warn($@);
396 exit 1;
397 }
398
399 exit 0;
400 }
401
402 # Stop server
403 server_stop($pid_file);
404
405 $self->assert_child_ok($pid);
406
407 if ($ex) {
408 die($ex);
409 }
410
411 unlink($log_file);
412 }
413
414 1;