Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::HideNoAccess;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
8 use File::Spec;
9 use IO::Handle;
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 hidenoaccess_ok => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 hidenoaccess_with_directory_glob => {
25 order => ++$order,
26 test_class => [qw(forking)],
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 hidenoaccess_ok {
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 = test_get_logfile();
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 = 'proftpd';
53 my $passwd = 'test';
54 my $group = 'ftpd';
55 my $home_dir = File::Spec->rel2abs($tmpdir);
56 my $uid = 500;
57 my $gid = 500;
58
59 # Make sure that, if we're running as root, that the home directory has
60 # permissions/privs set for the account we create
61 if ($< == 0) {
62 unless (chmod(0755, $home_dir)) {
63 die("Can't set perms on $home_dir to 0755: $!");
64 }
65
66 unless (chown($uid, $gid, $home_dir)) {
67 die("Can't set owner of $home_dir to $uid/$gid: $!");
68 }
69 }
70
71 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
72 '/bin/bash');
73 auth_group_write($auth_group_file, $group, $gid, $user);
74
75 my $test_dir = File::Spec->rel2abs("$tmpdir/testdir");
76 mkpath($test_dir);
77
78 unless (chmod(0000, $test_dir)) {
79 die("Can't set perms on $test_dir to 0000: $!");
80 }
81
82 my $config = {
83 PidFile => $pid_file,
84 ScoreboardFile => $scoreboard_file,
85 SystemLog => $log_file,
86 TraceLog => $log_file,
87
88 AuthUserFile => $auth_user_file,
89 AuthGroupFile => $auth_group_file,
90 AuthOrder => 'mod_auth_file.c',
91
92 Directory => {
93 '~' => {
94 HideNoAccess => 'on',
95 },
96 },
97
98 IfModules => {
99 'mod_delay.c' => {
100 DelayEngine => 'off',
101 },
102 },
103 };
104
105 my ($port, $config_user, $config_group) = config_write($config_file, $config);
106
107 # Open pipes, for use between the parent and child processes. Specifically,
108 # the child will indicate when it's done with its test by writing a message
109 # to the parent.
110 my ($rfh, $wfh);
111 unless (pipe($rfh, $wfh)) {
112 die("Can't open pipe: $!");
113 }
114
115 my $ex;
116
117 # Fork child
118 $self->handle_sigchld();
119 defined(my $pid = fork()) or die("Can't fork: $!");
120 if ($pid) {
121 eval {
122 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
123 $client->login($user, $passwd);
124
125 my $conn = $client->list_raw();
126 unless ($conn) {
127 die("Failed to LIST: " . $client->response_code() . " " .
128 $client->response_msg());
129 }
130
131 my $buf;
132 $conn->read($buf, 8192, 25);
133 eval { $conn->close() };
134
135 my $resp_code = $client->response_code();
136 my $resp_msg = $client->response_msg();
137 $self->assert_transfer_ok($resp_code, $resp_msg);
138
139 $client->quit();
140
141 # We have to be careful of the fact that readdir returns directory
142 # entries in an unordered fashion.
143 my $res = {};
144 my $lines = [split(/\n/, $buf)];
145 foreach my $line (@$lines) {
146 if ($line =~ /^\S+\s+\d+\s+\S+\s+\S+\s+.*?\s+(\S+)$/) {
147 $res->{$1} = 1;
148 }
149 }
150
151 my $expected = {
152 'config.conf' => 1,
153 'config.group' => 1,
154 'config.passwd' => 1,
155 'config.pid' => 1,
156 'config.scoreboard' => 1,
157 'config.scoreboard.lck' => 1,
158 };
159
160 my $ok = 1;
161 my $mismatch;
162 foreach my $name (keys(%$res)) {
163 unless (defined($expected->{$name})) {
164 $mismatch = $name;
165 $ok = 0;
166 last;
167 }
168 }
169
170 unless ($ok) {
171 die("Unexpected name '$mismatch' appeared in LIST data")
172 }
173
174 my $count = scalar(keys(%$res));
175 unless ($count == scalar(keys(%$expected))) {
176 die("Unexpected count ($count) of names appeared in LIST data");
177 }
178
179 };
180
181 if ($@) {
182 $ex = $@;
183 }
184
185 $wfh->print("done\n");
186 $wfh->flush();
187
188 } else {
189 eval { server_wait($config_file, $rfh) };
190 if ($@) {
191 warn($@);
192 exit 1;
193 }
194
195 exit 0;
196 }
197
198 # Stop server
199 server_stop($pid_file);
200
201 $self->assert_child_ok($pid);
202
203 if ($ex) {
204 test_append_logfile($log_file, $ex);
205 unlink($log_file);
206
207 die($ex);
208 }
209
210 unlink($log_file);
211 }
212
213 sub hidenoaccess_with_directory_glob {
214 my $self = shift;
215 my $tmpdir = $self->{tmpdir};
216
217 my $config_file = "$tmpdir/config.conf";
218 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
219 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
220
221 my $log_file = test_get_logfile();
222
223 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
224 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
225
226 my $user = 'proftpd';
227 my $passwd = 'test';
228 my $group = 'ftpd';
229 my $home_dir = File::Spec->rel2abs($tmpdir);
230 my $uid = 500;
231 my $gid = 500;
232
233 # Make sure that, if we're running as root, that the home directory has
234 # permissions/privs set for the account we create
235 if ($< == 0) {
236 unless (chmod(0755, $home_dir)) {
237 die("Can't set perms on $home_dir to 0755: $!");
238 }
239
240 unless (chown($uid, $gid, $home_dir)) {
241 die("Can't set owner of $home_dir to $uid/$gid: $!");
242 }
243 }
244
245 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
246 '/bin/bash');
247 auth_group_write($auth_group_file, $group, $gid, $user);
248
249 for (my $i = 1; $i < 6; $i++) {
250 my $test_dir = File::Spec->rel2abs("$tmpdir/testdir" . $i);
251 mkpath($test_dir);
252
253 unless (chmod(0755, $test_dir)) {
254 die("Can't set perms on $test_dir to 0755: $!");
255 }
256 }
257
258 my $config = {
259 PidFile => $pid_file,
260 ScoreboardFile => $scoreboard_file,
261 SystemLog => $log_file,
262 TraceLog => $log_file,
263 Trace => 'DEFAULT:10',
264
265 AuthUserFile => $auth_user_file,
266 AuthGroupFile => $auth_group_file,
267 AuthOrder => 'mod_auth_file.c',
268
269 Directory => {
270 '/*' => {
271 HideNoAccess => 'on',
272 },
273 },
274
275 IfModules => {
276 'mod_delay.c' => {
277 DelayEngine => 'off',
278 },
279 },
280 };
281
282 my ($port, $config_user, $config_group) = config_write($config_file, $config);
283
284 # Open pipes, for use between the parent and child processes. Specifically,
285 # the child will indicate when it's done with its test by writing a message
286 # to the parent.
287 my ($rfh, $wfh);
288 unless (pipe($rfh, $wfh)) {
289 die("Can't open pipe: $!");
290 }
291
292 my $ex;
293
294 # Fork child
295 $self->handle_sigchld();
296 defined(my $pid = fork()) or die("Can't fork: $!");
297 if ($pid) {
298 eval {
299 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
300 $client->login($user, $passwd);
301
302 my $conn = $client->list_raw();
303 unless ($conn) {
304 die("Failed to LIST: " . $client->response_code() . " " .
305 $client->response_msg());
306 }
307
308 my $buf;
309 $conn->read($buf, 8192, 25);
310 eval { $conn->close() };
311
312 my $resp_code = $client->response_code();
313 my $resp_msg = $client->response_msg();
314 $self->assert_transfer_ok($resp_code, $resp_msg);
315
316 $client->quit();
317
318 # We have to be careful of the fact that readdir returns directory
319 # entries in an unordered fashion.
320 my $res = {};
321 my $lines = [split(/\n/, $buf)];
322 foreach my $line (@$lines) {
323 if ($line =~ /^\S+\s+\d+\s+\S+\s+\S+\s+.*?\s+(\S+)$/) {
324 $res->{$1} = 1;
325 }
326 }
327
328 my $expected = {
329 'config.conf' => 1,
330 'config.group' => 1,
331 'config.passwd' => 1,
332 'config.pid' => 1,
333 'config.scoreboard' => 1,
334 'config.scoreboard.lck' => 1,
335 'testdir1' => 1,
336 'testdir2' => 1,
337 'testdir3' => 1,
338 'testdir4' => 1,
339 'testdir5' => 1,
340 };
341
342 my $ok = 1;
343 my $mismatch;
344 foreach my $name (keys(%$res)) {
345 unless (defined($expected->{$name})) {
346 $mismatch = $name;
347 $ok = 0;
348 last;
349 }
350 }
351
352 unless ($ok) {
353 die("Unexpected name '$mismatch' appeared in LIST data")
354 }
355
356 my $count = scalar(keys(%$res));
357 unless ($count == scalar(keys(%$expected))) {
358 die("Unexpected count ($count) of names appeared in LIST data");
359 }
360
361 };
362
363 if ($@) {
364 $ex = $@;
365 }
366
367 $wfh->print("done\n");
368 $wfh->flush();
369
370 } else {
371 eval { server_wait($config_file, $rfh) };
372 if ($@) {
373 warn($@);
374 exit 1;
375 }
376
377 exit 0;
378 }
379
380 # Stop server
381 server_stop($pid_file);
382
383 $self->assert_child_ok($pid);
384
385 if ($ex) {
386 test_append_logfile($log_file, $ex);
387 unlink($log_file);
388
389 die($ex);
390 }
391
392 unlink($log_file);
393 }
394
395 1;