Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::MasqueradeAddress;
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 masqaddr_pasv => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 masqaddr_pasv_delayed_resolving_bug4104 => {
24 order => ++$order,
25 test_class => [qw(bug forking)],
26 },
27
28 masqaddr_empty_addr => {
29 order => ++$order,
30 test_class => [qw(bug forking)],
31 },
32
33 # MasqueradeAddress not support for EPSV currently; see comments
34 # in mod_core.c:core_epsv() on this topic.
35
36 };
37
38 sub new {
39 return shift()->SUPER::new(@_);
40 }
41
42 sub list_tests {
43 return testsuite_get_runnable_tests($TESTS);
44 }
45
46 sub masqaddr_pasv {
47 my $self = shift;
48 my $tmpdir = $self->{tmpdir};
49
50 my $config_file = "$tmpdir/config.conf";
51 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
52 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
53
54 my $log_file = test_get_logfile();
55
56 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
57 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
58
59 my $user = 'proftpd';
60 my $passwd = 'test';
61 my $group = 'ftpd';
62 my $home_dir = File::Spec->rel2abs($tmpdir);
63 my $uid = 500;
64 my $gid = 500;
65
66 # Make sure that, if we're running as root, that the home directory has
67 # permissions/privs set for the account we create
68 if ($< == 0) {
69 unless (chmod(0755, $home_dir)) {
70 die("Can't set perms on $home_dir to 0755: $!");
71 }
72
73 unless (chown($uid, $gid, $home_dir)) {
74 die("Can't set owner of $home_dir to $uid/$gid: $!");
75 }
76 }
77
78 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
79 '/bin/bash');
80 auth_group_write($auth_group_file, $group, $gid, $user);
81
82 my $masq_addr = '1.2.3.4';
83
84 my $config = {
85 PidFile => $pid_file,
86 ScoreboardFile => $scoreboard_file,
87 SystemLog => $log_file,
88
89 AuthUserFile => $auth_user_file,
90 AuthGroupFile => $auth_group_file,
91 AuthOrder => 'mod_auth_file.c',
92
93 MasqueradeAddress => $masq_addr,
94
95 IfModules => {
96 'mod_delay.c' => {
97 DelayEngine => '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 $client->login($user, $passwd);;
121 my ($resp_code, $resp_msg) = $client->pasv();
122
123 my $expected;
124
125 $expected = 227;
126 $self->assert($expected == $resp_code,
127 test_msg("Expected $expected, got $resp_code"));
128
129 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
130 $self->assert(qr/$expected/, $resp_msg,
131 test_msg("Expected '$expected', got '$resp_msg'"));
132
133 $resp_msg =~ /\((.*)?,\d+,\d+\)/;
134 my $resp_addr = $1;
135
136 $expected = $masq_addr;
137 $expected =~ s/\./,/g;
138
139 $self->assert($expected eq $resp_addr,
140 test_msg("Expected PASV address '$expected', got '$resp_addr'"));
141 };
142
143 if ($@) {
144 $ex = $@;
145 }
146
147 $wfh->print("done\n");
148 $wfh->flush();
149
150 } else {
151 eval { server_wait($config_file, $rfh) };
152 if ($@) {
153 warn($@);
154 exit 1;
155 }
156
157 exit 0;
158 }
159
160 # Stop server
161 server_stop($pid_file);
162
163 $self->assert_child_ok($pid);
164
165 if ($ex) {
166 test_append_logfile($log_file, $ex);
167 unlink($log_file);
168
169 die($ex);
170 }
171
172 unlink($log_file);
173 }
174
175 sub masqaddr_pasv_delayed_resolving_bug4104 {
176 my $self = shift;
177 my $tmpdir = $self->{tmpdir};
178
179 my $config_file = "$tmpdir/config.conf";
180 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
181 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
182
183 my $log_file = test_get_logfile();
184
185 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
186 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
187
188 my $user = 'proftpd';
189 my $passwd = 'test';
190 my $group = 'ftpd';
191 my $home_dir = File::Spec->rel2abs($tmpdir);
192 my $uid = 500;
193 my $gid = 500;
194
195 # Make sure that, if we're running as root, that the home directory has
196 # permissions/privs set for the account we create
197 if ($< == 0) {
198 unless (chmod(0755, $home_dir)) {
199 die("Can't set perms on $home_dir to 0755: $!");
200 }
201
202 unless (chown($uid, $gid, $home_dir)) {
203 die("Can't set owner of $home_dir to $uid/$gid: $!");
204 }
205 }
206
207 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
208 '/bin/bash');
209 auth_group_write($auth_group_file, $group, $gid, $user);
210
211 # We deliberately choose a name which cannot be resolved, to test that
212 # the server still starts up (Bug#4104).
213 my $masq_addr = 'foo.bar.castaglia.example.com';
214
215 my $config = {
216 PidFile => $pid_file,
217 ScoreboardFile => $scoreboard_file,
218 SystemLog => $log_file,
219
220 AuthUserFile => $auth_user_file,
221 AuthGroupFile => $auth_group_file,
222 AuthOrder => 'mod_auth_file.c',
223
224 MasqueradeAddress => $masq_addr,
225
226 IfModules => {
227 'mod_delay.c' => {
228 DelayEngine => 'off',
229 },
230 },
231 };
232
233 my ($port, $config_user, $config_group) = config_write($config_file, $config);
234
235 # Open pipes, for use between the parent and child processes. Specifically,
236 # the child will indicate when it's done with its test by writing a message
237 # to the parent.
238 my ($rfh, $wfh);
239 unless (pipe($rfh, $wfh)) {
240 die("Can't open pipe: $!");
241 }
242
243 my $ex;
244
245 # Fork child
246 $self->handle_sigchld();
247 defined(my $pid = fork()) or die("Can't fork: $!");
248 if ($pid) {
249 eval {
250 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
251 $client->login($user, $passwd);;
252 my ($resp_code, $resp_msg) = $client->pasv();
253
254 my $expected;
255
256 $expected = 227;
257 $self->assert($expected == $resp_code,
258 test_msg("Expected $expected, got $resp_code"));
259
260 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
261 $self->assert(qr/$expected/, $resp_msg,
262 test_msg("Expected '$expected', got '$resp_msg'"));
263
264 $resp_msg =~ /\((.*)?,\d+,\d+\)/;
265 my $resp_addr = $1;
266
267 $expected = $masq_addr;
268 $expected =~ s/\./,/g;
269
270 $self->assert($expected ne $resp_addr,
271 test_msg("Expected PASV address '$expected', got '$resp_addr'"));
272 };
273
274 if ($@) {
275 $ex = $@;
276 }
277
278 $wfh->print("done\n");
279 $wfh->flush();
280
281 } else {
282 eval { server_wait($config_file, $rfh) };
283 if ($@) {
284 warn($@);
285 exit 1;
286 }
287
288 exit 0;
289 }
290
291 # Stop server
292 server_stop($pid_file);
293
294 $self->assert_child_ok($pid);
295
296 if ($ex) {
297 test_append_logfile($log_file, $ex);
298 unlink($log_file);
299
300 die($ex);
301 }
302
303 unlink($log_file);
304 }
305
306 sub masqaddr_empty_addr {
307 my $self = shift;
308 my $tmpdir = $self->{tmpdir};
309 my $setup = test_setup($tmpdir, 'config');
310
311 # We can't configure an empty address directly, but we CAN configure
312 # an environment variable that doesn't exist, which will tickle this.
313 my $masq_addr = '%{env:TEST_NON_EXISTENT_ADDR}';
314
315 my $config = {
316 PidFile => $setup->{pid_file},
317 ScoreboardFile => $setup->{scoreboard_file},
318 SystemLog => $setup->{log_file},
319
320 AuthUserFile => $setup->{auth_user_file},
321 AuthGroupFile => $setup->{auth_group_file},
322 AuthOrder => 'mod_auth_file.c',
323
324 MasqueradeAddress => $masq_addr,
325
326 IfModules => {
327 'mod_delay.c' => {
328 DelayEngine => 'off',
329 },
330 },
331 };
332
333 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
334 $config);
335
336 my $ex;
337 eval { server_start($setup->{config_file}, $setup->{pid_file}) };
338 unless ($@) {
339 server_stop($setup->{pid_file});
340 $ex = "server started unexpectedly";
341 }
342
343 test_cleanup($setup->{log_file}, $ex);
344 }
345
346 1;