Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/FTPAccess/DELE.pm

raw
1 package ProFTPD::Tests::Config::FTPAccess::DELE;
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 dele_bug2321 => {
20 order => ++$order,
21 test_class => [qw(bug forking)],
22 },
23
24 };
25
26 sub new {
27 return shift()->SUPER::new(@_);
28 }
29
30 sub list_tests {
31 return testsuite_get_runnable_tests($TESTS);
32 }
33
34 sub dele_bug2321 {
35 my $self = shift;
36 my $tmpdir = $self->{tmpdir};
37
38 my $config_file = "$tmpdir/limit.conf";
39 my $pid_file = File::Spec->rel2abs("$tmpdir/limit.pid");
40 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/limit.scoreboard");
41
42 my $log_file = test_get_logfile();
43
44 my $auth_user_file = File::Spec->rel2abs("$tmpdir/limit.passwd");
45 my $auth_group_file = File::Spec->rel2abs("$tmpdir/limit.group");
46
47 # As per Bug#2321, the reproduction layout looks like:
48 #
49 # $root/web/
50 # $root/web/.ftpaccess
51 # $root/users/$user/
52 # $root/users/$user/web -> $root/web/
53 #
54 # The .ftpaccess file should contain:
55 #
56 # <Limit ALL>
57 # AllowUser $user
58 # DenyAll
59 # </Limit>
60
61 my $user = 'proftpd';
62 my $passwd = 'test';
63 my $group = 'ftpd';
64 my $home_dir = File::Spec->rel2abs("$tmpdir/users/$user");
65 my $uid = 500;
66 my $gid = 500;
67
68 mkpath($home_dir);
69
70 my $web_dir = File::Spec->rel2abs("$tmpdir/web");
71 mkpath($web_dir);
72
73 my $test_dir = File::Spec->rel2abs("$tmpdir/users/$user/web/testdir");
74 my $real_test_dir = File::Spec->rel2abs("$web_dir/testdir");
75 my $real_test_file = File::Spec->rel2abs("$web_dir/test.txt");
76
77 my $ftpaccess_file = File::Spec->rel2abs("$tmpdir/web/.ftpaccess");
78 if (open(my $fh, "> $ftpaccess_file")) {
79 print $fh <<EOL;
80 <Limit ALL>
81 AllowUser $user
82 DenyAll
83 </Limit>
84 EOL
85
86 unless (close($fh)) {
87 die("Can't write $ftpaccess_file: $!");
88 }
89
90 } else {
91 die("Can't open $ftpaccess_file: $!");
92 }
93
94 unless (symlink($web_dir, "$home_dir/web")) {
95 die("Can't symlink $web_dir to $home_dir/web: $!");
96 }
97
98 # Make sure that, if we're running as root, that the home directory has
99 # permissions/privs set for the account we create
100 if ($< == 0) {
101 unless (chmod(0755, $home_dir, $web_dir)) {
102 die("Can't set perms on $home_dir, $web_dir to 0755: $!");
103 }
104
105 unless (chown($uid, $gid, $home_dir, $web_dir)) {
106 die("Can't set owner of $home_dir, $web_dir to $uid/$gid: $!");
107 }
108 }
109
110 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
111 '/bin/bash');
112 auth_group_write($auth_group_file, $group, $gid, $user);
113
114 my $config = {
115 PidFile => $pid_file,
116 ScoreboardFile => $scoreboard_file,
117 SystemLog => $log_file,
118
119 AuthUserFile => $auth_user_file,
120 AuthGroupFile => $auth_group_file,
121
122 AllowOverride => 'on',
123
124 IfModules => {
125 'mod_delay.c' => {
126 DelayEngine => 'off',
127 },
128 },
129 };
130
131 my ($port, $config_user, $config_group) = config_write($config_file, $config);
132
133 # Open pipes, for use between the parent and child processes. Specifically,
134 # the child will indicate when it's done with its test by writing a message
135 # to the parent.
136 my ($rfh, $wfh);
137 unless (pipe($rfh, $wfh)) {
138 die("Can't open pipe: $!");
139 }
140
141 my $ex;
142
143 # Fork child
144 $self->handle_sigchld();
145 defined(my $pid = fork()) or die("Can't fork: $!");
146 if ($pid) {
147 eval {
148 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
149 $client->login($user, $passwd);
150 $client->cwd($home_dir);
151
152 # STOR web/test.txt
153 # MKD web/testdir/
154 # DELE web/test.txt
155 # RMD web/testdir/
156
157 my ($resp_code, $resp_msg);
158
159 my $conn = $client->stor_raw("web/test.txt");
160 unless ($conn) {
161 die("Failed to STOR web/test.txt: " . $client->response_code() .
162 " " . $client->response_msg());
163 }
164
165 my $buf = "Foo!\n";
166 $conn->write($buf, length($buf));
167 $conn->close();
168
169 $resp_code = $client->response_code();
170 $resp_msg = $client->response_msg();
171
172 my $expected;
173
174 $expected = 226;
175 $self->assert($expected == $resp_code,
176 test_msg("Expected $expected, got $resp_code"));
177
178 $expected = "Transfer complete";
179 $self->assert($expected eq $resp_msg,
180 test_msg("Expected '$expected', got '$resp_msg'"));
181
182 ($resp_code, $resp_msg) = $client->mkd("web/testdir");
183
184 $expected = 257;
185 $self->assert($expected == $resp_code,
186 test_msg("Expected $expected, got $resp_code"));
187
188 $expected = "\"$test_dir\" - Directory successfully created";
189 $self->assert($expected eq $resp_msg,
190 test_msg("Expected '$expected', got '$resp_msg'"));
191
192 unless (-f $real_test_file) {
193 die("File $real_test_file does not exist");
194 }
195
196 unless (-d $real_test_dir) {
197 die("Directory $real_test_dir does not exist");
198 }
199
200 ($resp_code, $resp_msg) = $client->dele("web/test.txt");
201
202 $expected = 250;
203 $self->assert($expected == $resp_code,
204 test_msg("Expected $expected, got $resp_code"));
205
206 $expected = "DELE command successful";
207 $self->assert($expected eq $resp_msg,
208 test_msg("Expected '$expected', got '$resp_msg'"));
209
210 ($resp_code, $resp_msg) = $client->rmd("web/testdir");
211
212 $expected = 250;
213 $self->assert($expected == $resp_code,
214 test_msg("Expected $expected, got $resp_code"));
215
216 $expected = "RMD command successful";
217 $self->assert($expected eq $resp_msg,
218 test_msg("Expected '$expected', got '$resp_msg'"));
219 };
220
221 if ($@) {
222 $ex = $@;
223 }
224
225 $wfh->print("done\n");
226 $wfh->flush();
227
228 } else {
229 eval { server_wait($config_file, $rfh) };
230 if ($@) {
231 warn($@);
232 exit 1;
233 }
234
235 exit 0;
236 }
237
238 # Stop server
239 server_stop($pid_file);
240
241 $self->assert_child_ok($pid);
242
243 if ($ex) {
244 test_append_logfile($log_file, $ex);
245 unlink($log_file);
246
247 die($ex);
248 }
249
250 unlink($log_file);
251 }
252
253 1;