Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/SITE/CHMOD.pm

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