| 1 |
package ProFTPD::Tests::Config::Limit::RMD; |
| 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 |
rmd_unremovable_subdir => { |
| 20 |
order => ++$order, |
| 21 |
test_class => [qw(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 rmd_unremovable_subdir { |
| 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 = File::Spec->rel2abs('tests.log'); |
| 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 |
my $user = 'proftpd'; |
| 48 |
my $passwd = 'test'; |
| 49 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 50 |
my $uid = 500; |
| 51 |
my $gid = 500; |
| 52 |
|
| 53 |
my $sub_dir = File::Spec->rel2abs("$tmpdir/domain.com/web/special"); |
| 54 |
mkpath($sub_dir); |
| 55 |
|
| 56 |
# Make sure that, if we're running as root, that the home directory has |
| 57 |
# permissions/privs set for the account we create |
| 58 |
if ($< == 0) { |
| 59 |
unless (chmod(0755, $home_dir)) { |
| 60 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 61 |
} |
| 62 |
|
| 63 |
unless (chown($uid, $gid, $home_dir)) { |
| 64 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 69 |
'/bin/bash'); |
| 70 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 71 |
|
| 72 |
if ($^O eq 'darwin') { |
| 73 |
# Mac OSX hack |
| 74 |
$home_dir = '/private' . $home_dir; |
| 75 |
$sub_dir = '/private' . $sub_dir; |
| 76 |
} |
| 77 |
|
| 78 |
my $config = { |
| 79 |
PidFile => $pid_file, |
| 80 |
ScoreboardFile => $scoreboard_file, |
| 81 |
SystemLog => $log_file, |
| 82 |
TraceLog => $log_file, |
| 83 |
Trace => 'command:10 directory:10', |
| 84 |
|
| 85 |
AuthUserFile => $auth_user_file, |
| 86 |
AuthGroupFile => $auth_group_file, |
| 87 |
AuthOrder => 'mod_auth_file.c', |
| 88 |
|
| 89 |
DefaultChdir => '~', |
| 90 |
|
| 91 |
IfModules => { |
| 92 |
'mod_delay.c' => { |
| 93 |
DelayEngine => 'off', |
| 94 |
}, |
| 95 |
}, |
| 96 |
|
| 97 |
}; |
| 98 |
|
| 99 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 100 |
|
| 101 |
if (open(my $fh, ">> $config_file")) { |
| 102 |
print $fh <<EOC; |
| 103 |
<Directory /> |
| 104 |
<Limit ALL SITE_CHMOD> |
| 105 |
DenyAll |
| 106 |
</Limit> |
| 107 |
|
| 108 |
<Limit DIRS> |
| 109 |
AllowAll |
| 110 |
</Limit> |
| 111 |
</Directory> |
| 112 |
|
| 113 |
<Directory /*/domain.com/web> |
| 114 |
<Limit ALL> |
| 115 |
AllowAll |
| 116 |
</Limit> |
| 117 |
|
| 118 |
<Limit RMD XRMD> |
| 119 |
DenyFilter special(/)?\$ |
| 120 |
</Limit> |
| 121 |
</Directory> |
| 122 |
|
| 123 |
<Directory /*/domain.com/web/special/*> |
| 124 |
<Limit RMD XRMD> |
| 125 |
AllowAll |
| 126 |
</Limit> |
| 127 |
</Directory> |
| 128 |
EOC |
| 129 |
unless (close($fh)) { |
| 130 |
die("Can't write $config_file: $!"); |
| 131 |
} |
| 132 |
|
| 133 |
} else { |
| 134 |
die("Can't open $config_file: $!"); |
| 135 |
} |
| 136 |
|
| 137 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 138 |
# the child will indicate when it's done with its test by writing a message |
| 139 |
# to the parent. |
| 140 |
my ($rfh, $wfh); |
| 141 |
unless (pipe($rfh, $wfh)) { |
| 142 |
die("Can't open pipe: $!"); |
| 143 |
} |
| 144 |
|
| 145 |
my $ex; |
| 146 |
|
| 147 |
# Fork child |
| 148 |
$self->handle_sigchld(); |
| 149 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 150 |
if ($pid) { |
| 151 |
eval { |
| 152 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 153 |
$client->login($user, $passwd); |
| 154 |
|
| 155 |
# We should be able to create and delete a directory under web/. |
| 156 |
# We should be able to create and delete a directory under web/special/. |
| 157 |
# We should NOT be able to delete the web/special/ directory. |
| 158 |
|
| 159 |
my ($resp_code, $resp_msg) = $client->mkd('domain.com/web/foo'); |
| 160 |
|
| 161 |
my $expected; |
| 162 |
|
| 163 |
$expected = 257; |
| 164 |
$self->assert($expected == $resp_code, |
| 165 |
test_msg("Expected response code $expected, got $resp_code")); |
| 166 |
|
| 167 |
$expected = "\"$home_dir/domain.com/web/foo\" - Directory successfully created"; |
| 168 |
$self->assert($expected eq $resp_msg, |
| 169 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 170 |
|
| 171 |
($resp_code, $resp_msg) = $client->rmd('domain.com/web/foo'); |
| 172 |
|
| 173 |
$expected = 250; |
| 174 |
$self->assert($expected == $resp_code, |
| 175 |
test_msg("Expected response code $expected, got $resp_code")); |
| 176 |
|
| 177 |
$expected = "RMD command successful"; |
| 178 |
$self->assert($expected eq $resp_msg, |
| 179 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 180 |
|
| 181 |
($resp_code, $resp_msg) = $client->mkd('domain.com/web/special/foo'); |
| 182 |
|
| 183 |
$expected = 257; |
| 184 |
$self->assert($expected == $resp_code, |
| 185 |
test_msg("Expected response code $expected, got $resp_code")); |
| 186 |
|
| 187 |
$expected = "\"$home_dir/domain.com/web/special/foo\" - Directory successfully created"; |
| 188 |
$self->assert($expected eq $resp_msg, |
| 189 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 190 |
|
| 191 |
($resp_code, $resp_msg) = $client->rmd('domain.com/web/special/foo'); |
| 192 |
|
| 193 |
$expected = 250; |
| 194 |
$self->assert($expected == $resp_code, |
| 195 |
test_msg("Expected response code $expected, got $resp_code")); |
| 196 |
|
| 197 |
$expected = "RMD command successful"; |
| 198 |
$self->assert($expected eq $resp_msg, |
| 199 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 200 |
|
| 201 |
eval { $client->rmd('domain.com/web/special') }; |
| 202 |
unless ($@) { |
| 203 |
die("RMD domain.com/web/special succeeded unexpectedly"); |
| 204 |
} |
| 205 |
|
| 206 |
$resp_code = $client->response_code(); |
| 207 |
$resp_msg = $client->response_msg(); |
| 208 |
|
| 209 |
$expected = 550; |
| 210 |
$self->assert($expected == $resp_code, |
| 211 |
test_msg("Expected response code $expected, got $resp_code")); |
| 212 |
|
| 213 |
$expected = "domain.com/web/special: Permission denied"; |
| 214 |
$self->assert($expected eq $resp_msg, |
| 215 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 216 |
}; |
| 217 |
|
| 218 |
if ($@) { |
| 219 |
$ex = $@; |
| 220 |
} |
| 221 |
|
| 222 |
$wfh->print("done\n"); |
| 223 |
$wfh->flush(); |
| 224 |
|
| 225 |
} else { |
| 226 |
eval { server_wait($config_file, $rfh) }; |
| 227 |
if ($@) { |
| 228 |
warn($@); |
| 229 |
exit 1; |
| 230 |
} |
| 231 |
|
| 232 |
exit 0; |
| 233 |
} |
| 234 |
|
| 235 |
# Stop server |
| 236 |
server_stop($pid_file); |
| 237 |
|
| 238 |
$self->assert_child_ok($pid); |
| 239 |
|
| 240 |
if ($ex) { |
| 241 |
die($ex); |
| 242 |
} |
| 243 |
|
| 244 |
unlink($log_file); |
| 245 |
} |
| 246 |
|
| 247 |
1; |