Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::AnonRequirePassword;
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_require_password_off => {
19 order => ++$order,
20 test_class => [qw(forking rootprivs)],
21 },
22
23 anon_require_password_on => {
24 order => ++$order,
25 test_class => [qw(forking rootprivs)],
26 },
27
28
29 };
30
31 sub new {
32 return shift()->SUPER::new(@_);
33 }
34
35 sub list_tests {
36 return testsuite_get_runnable_tests($TESTS);
37 }
38
39 sub anon_require_password_off {
40 my $self = shift;
41 my $tmpdir = $self->{tmpdir};
42
43 my $config_file = "$tmpdir/config.conf";
44 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
45 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
46
47 my $log_file = File::Spec->rel2abs('tests.log');
48
49 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
50 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
51
52 my ($user, $group) = config_get_identity();
53
54 my $anon_dir = File::Spec->rel2abs($tmpdir);
55 my $uid = 500;
56 my $gid = 500;
57
58 # Make sure that, if we're running as root, that the home directory has
59 # permissions/privs set for the account we create
60 if ($< == 0) {
61 unless (chmod(0755, $anon_dir)) {
62 die("Can't set perms on $anon_dir to 0755: $!");
63 }
64
65 unless (chown($uid, $gid, $anon_dir)) {
66 die("Can't set owner of $anon_dir to $uid/$gid: $!");
67 }
68 }
69
70 auth_user_write($auth_user_file, $user, 'foo', $uid, $gid,
71 '/tmp', '/bin/bash');
72 auth_group_write($auth_group_file, $group, $gid, $user);
73
74 my $config = {
75 PidFile => $pid_file,
76 ScoreboardFile => $scoreboard_file,
77 SystemLog => $log_file,
78 TraceLog => $log_file,
79 Trace => 'DEFAULT:10 privs:10',
80
81 AuthUserFile => $auth_user_file,
82 AuthGroupFile => $auth_group_file,
83 AuthOrder => 'mod_auth_file.c',
84
85 IfModules => {
86 'mod_delay.c' => {
87 DelayEngine => 'off',
88 },
89 },
90
91 Anonymous => {
92 $anon_dir => {
93 User => $user,
94 Group => $group,
95 UserAlias => "anonymous $user",
96 AnonRequirePassword => 'off',
97 RequireValidShell => 'off',
98 },
99 },
100 };
101
102 my ($port, $config_user, $config_group) = config_write($config_file, $config);
103
104 # Open pipes, for use between the parent and child processes. Specifically,
105 # the child will indicate when it's done with its test by writing a message
106 # to the parent.
107 my ($rfh, $wfh);
108 unless (pipe($rfh, $wfh)) {
109 die("Can't open pipe: $!");
110 }
111
112 my $ex;
113
114 # Fork child
115 $self->handle_sigchld();
116 defined(my $pid = fork()) or die("Can't fork: $!");
117 if ($pid) {
118 eval {
119 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
120
121 my ($resp_code, $resp_msg) = $client->login($user, '[email protected]');
122
123 my $expected;
124
125 $expected = 230;
126 $self->assert($expected == $resp_code,
127 test_msg("Expected $expected, got $resp_code"));
128
129 $expected = 'Anonymous access granted, restrictions apply';
130 $self->assert($expected eq $resp_msg,
131 test_msg("Expected '$expected', got '$resp_msg'"));
132
133 $client->quit();
134 };
135
136 if ($@) {
137 $ex = $@;
138 }
139
140 $wfh->print("done\n");
141 $wfh->flush();
142
143 } else {
144 eval { server_wait($config_file, $rfh) };
145 if ($@) {
146 warn($@);
147 exit 1;
148 }
149
150 exit 0;
151 }
152
153 # Stop server
154 server_stop($pid_file);
155
156 $self->assert_child_ok($pid);
157
158 if ($ex) {
159 die($ex);
160 }
161
162 unlink($log_file);
163 }
164
165 sub anon_require_password_on {
166 my $self = shift;
167 my $tmpdir = $self->{tmpdir};
168
169 my $config_file = "$tmpdir/config.conf";
170 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
171 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
172
173 my $log_file = File::Spec->rel2abs('tests.log');
174
175 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
176 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
177
178 my ($user, $group) = config_get_identity();
179
180 my $anon_dir = File::Spec->rel2abs($tmpdir);
181 my $passwd = 'foobar';
182 my $uid = 500;
183 my $gid = 500;
184
185 # Make sure that, if we're running as root, that the home directory has
186 # permissions/privs set for the account we create
187 if ($< == 0) {
188 unless (chmod(0755, $anon_dir)) {
189 die("Can't set perms on $anon_dir to 0755: $!");
190 }
191
192 unless (chown($uid, $gid, $anon_dir)) {
193 die("Can't set owner of $anon_dir to $uid/$gid: $!");
194 }
195 }
196
197 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid,
198 '/tmp', '/bin/bash');
199 auth_group_write($auth_group_file, $group, $gid, $user);
200
201 my $config = {
202 PidFile => $pid_file,
203 ScoreboardFile => $scoreboard_file,
204 SystemLog => $log_file,
205 TraceLog => $log_file,
206 Trace => 'DEFAULT:10 privs:10',
207
208 AuthUserFile => $auth_user_file,
209 AuthGroupFile => $auth_group_file,
210 AuthOrder => 'mod_auth_file.c',
211
212 IfModules => {
213 'mod_delay.c' => {
214 DelayEngine => 'off',
215 },
216 },
217
218 Anonymous => {
219 $anon_dir => {
220 User => $user,
221 Group => $group,
222 UserAlias => "anonymous $user",
223 AnonRequirePassword => 'on',
224 RequireValidShell => 'off',
225 },
226 },
227 };
228
229 my ($port, $config_user, $config_group) = config_write($config_file, $config);
230
231 # Open pipes, for use between the parent and child processes. Specifically,
232 # the child will indicate when it's done with its test by writing a message
233 # to the parent.
234 my ($rfh, $wfh);
235 unless (pipe($rfh, $wfh)) {
236 die("Can't open pipe: $!");
237 }
238
239 my $ex;
240
241 # Fork child
242 $self->handle_sigchld();
243 defined(my $pid = fork()) or die("Can't fork: $!");
244 if ($pid) {
245 eval {
246 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
247
248 my ($resp_code, $resp_msg);
249
250 # First, try to login using an email address as a password
251 eval { $client->login($user, '[email protected]') };
252 unless ($@) {
253 die("Login succeeded unexpectedly");
254 }
255
256 $resp_code = $client->response_code();
257 $resp_msg = $client->response_msg();
258
259 my $expected;
260
261 $expected = 530;
262 $self->assert($expected == $resp_code,
263 test_msg("Expected $expected, got $resp_code"));
264
265 $expected = 'Login incorrect.';
266 $self->assert($expected eq $resp_msg,
267 test_msg("Expected '$expected', got '$resp_msg'"));
268
269 # Now try again, using the password
270 ($resp_code, $resp_msg) = $client->login($user, $passwd);
271
272 $expected = 230;
273 $self->assert($expected == $resp_code,
274 test_msg("Expected $expected, got $resp_code"));
275
276 $expected = 'Anonymous access granted, restrictions apply';
277 $self->assert($expected eq $resp_msg,
278 test_msg("Expected '$expected', got '$resp_msg'"));
279
280 $client->quit();
281 };
282
283 if ($@) {
284 $ex = $@;
285 }
286
287 $wfh->print("done\n");
288 $wfh->flush();
289
290 } else {
291 eval { server_wait($config_file, $rfh) };
292 if ($@) {
293 warn($@);
294 exit 1;
295 }
296
297 exit 0;
298 }
299
300 # Stop server
301 server_stop($pid_file);
302
303 $self->assert_child_ok($pid);
304
305 if ($ex) {
306 die($ex);
307 }
308
309 unlink($log_file);
310 }
311
312 1;