Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Telnet.pm

raw
1 package ProFTPD::Tests::Telnet;
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 use IO::Socket::INET;
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 telnet_iac_bug3521 => {
20 order => ++$order,
21 test_class => [qw(bug forking)],
22 },
23
24 telnet_iac_bug3697 => {
25 order => ++$order,
26 test_class => [qw(bug forking)],
27 },
28
29 telnet_initial_crlf_issue1527 => {
30 order => ++$order,
31 test_class => [qw(bug forking)],
32 },
33
34 telnet_nonascii_uppercase_issue2019 => {
35 order => ++$order,
36 test_class => [qw(bug forking)],
37 },
38 };
39
40 sub new {
41 return shift()->SUPER::new(@_);
42 }
43
44 sub list_tests {
45 # Check for the required Perl modules:
46 #
47 # Net-Telnet
48
49 my $required = [qw(
50 Net::Telnet
51 )];
52
53 foreach my $req (@$required) {
54 eval "use $req";
55 if ($@) {
56 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
57
58 if ($ENV{TEST_VERBOSE}) {
59 print STDERR "Unable to load $req: $@\n";
60 }
61
62 return qw(testsuite_empty_test);
63 }
64 }
65
66 return testsuite_get_runnable_tests($TESTS);
67 }
68
69 sub telnet_iac_bug3521 {
70 my $self = shift;
71 my $tmpdir = $self->{tmpdir};
72
73 my $config_file = "$tmpdir/telnet.conf";
74 my $pid_file = File::Spec->rel2abs("$tmpdir/telnet.pid");
75 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/telnet.scoreboard");
76
77 my $log_file = File::Spec->rel2abs("tests.log");
78
79 my $auth_user_file = File::Spec->rel2abs("$tmpdir/telnet.passwd");
80 my $auth_group_file = File::Spec->rel2abs("$tmpdir/telnet.group");
81
82 my $user = 'proftpd';
83 my $passwd = 'test';
84 my $home_dir = File::Spec->rel2abs($tmpdir);
85 my $uid = 500;
86 my $gid = 500;
87
88 # Make sure that, if we're running as root, that the home directory has
89 # permissions/privs set for the account we create
90 if ($< == 0) {
91 unless (chmod(0755, $home_dir)) {
92 die("Can't set perms on $home_dir to 0755: $!");
93 }
94
95 unless (chown($uid, $gid, $home_dir)) {
96 die("Can't set owner of $home_dir to $uid/$gid: $!");
97 }
98 }
99
100 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
101 '/bin/bash');
102 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
103
104 my $config = {
105 PidFile => $pid_file,
106 ScoreboardFile => $scoreboard_file,
107 SystemLog => $log_file,
108
109 AuthUserFile => $auth_user_file,
110 AuthGroupFile => $auth_group_file,
111 AuthOrder => 'mod_auth_file.c',
112
113 IfModules => {
114 'mod_delay.c' => {
115 DelayEngine => 'off',
116 },
117 },
118 };
119
120 my ($port, $config_user, $config_group) = config_write($config_file, $config);
121
122 # Open pipes, for use between the parent and child processes. Specifically,
123 # the child will indicate when it's done with its test by writing a message
124 # to the parent.
125 my ($rfh, $wfh);
126 unless (pipe($rfh, $wfh)) {
127 die("Can't open pipe: $!");
128 }
129
130 require Net::Telnet;
131
132 my $ex;
133
134 # Fork child
135 $self->handle_sigchld();
136 defined(my $pid = fork()) or die("Can't fork: $!");
137 if ($pid) {
138 eval {
139 sleep(1);
140
141 # To reproduce Bug#3521, we only need to connect to the server,
142 # then issue a Telnet IAC at the right time.
143
144 my $client = Net::Telnet->new(
145 Host => '127.0.0.1',
146 Port => $port,
147 Timeout => 3,
148 Errmode => 'return',
149 );
150
151 my $buf = "USER ";
152
153 # In proftpd-1.3.2, the size would be 1016:
154 # PR_TUNABLE_BUFFER_SIZE (1024) - 3 - 5 ("USER ")
155 #
156 # I.e.: $buf .= ("A" x 1016);
157
158 $buf .= ("A" x 4096);
159
160 $buf .= chr(Net::Telnet::TELNET_IAC());
161 $buf .= "Z" x 64;
162
163 my $res = $client->cmd(
164 String => $buf,
165 Prompt => "/$user/",
166 );
167
168 if ($res) {
169 die("USER succeeded expectedly");
170 }
171
172 my $ex = $client->errmsg();
173 unless ($ex =~ /^command\s+timed/) {
174 die("Unexpected exception thrown: $ex");
175 }
176 };
177
178 if ($@) {
179 $ex = $@;
180 }
181
182 $wfh->print("done\n");
183 $wfh->flush();
184
185 } else {
186 eval { server_wait($config_file, $rfh) };
187 if ($@) {
188 warn($@);
189 exit 1;
190 }
191
192 exit 0;
193 }
194
195 # Stop server
196 server_stop($pid_file);
197
198 $self->assert_child_ok($pid);
199
200 if ($ex) {
201 die($ex);
202 }
203
204 unlink($log_file);
205 }
206
207 sub telnet_iac_bug3697 {
208 my $self = shift;
209 my $tmpdir = $self->{tmpdir};
210
211 my $config_file = "$tmpdir/telnet.conf";
212 my $pid_file = File::Spec->rel2abs("$tmpdir/telnet.pid");
213 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/telnet.scoreboard");
214
215 my $log_file = File::Spec->rel2abs("tests.log");
216
217 my $auth_user_file = File::Spec->rel2abs("$tmpdir/telnet.passwd");
218 my $auth_group_file = File::Spec->rel2abs("$tmpdir/telnet.group");
219
220 my $user = 'proftpd';
221 my $passwd = 'test';
222 my $group = 'ftpd';
223 my $home_dir = File::Spec->rel2abs($tmpdir);
224 my $uid = 500;
225 my $gid = 500;
226
227 # Make sure that, if we're running as root, that the home directory has
228 # permissions/privs set for the account we create
229 if ($< == 0) {
230 unless (chmod(0755, $home_dir)) {
231 die("Can't set perms on $home_dir to 0755: $!");
232 }
233
234 unless (chown($uid, $gid, $home_dir)) {
235 die("Can't set owner of $home_dir to $uid/$gid: $!");
236 }
237 }
238
239 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
240 '/bin/bash');
241 auth_group_write($auth_group_file, $group, $gid, $user);
242
243 # To reproduce Bug#3697, we expect a filename with a single \377 (decimal 255)
244 # character in it.
245
246 my $filename = "test";
247 $filename .= chr(Net::Telnet::TELNET_IAC());
248 $filename .= ".txt";
249
250 my $test_file = File::Spec->rel2abs("$tmpdir/$filename");
251
252 my $config = {
253 PidFile => $pid_file,
254 ScoreboardFile => $scoreboard_file,
255 SystemLog => $log_file,
256
257 AuthUserFile => $auth_user_file,
258 AuthGroupFile => $auth_group_file,
259 AuthOrder => 'mod_auth_file.c',
260
261 IfModules => {
262 'mod_delay.c' => {
263 DelayEngine => 'off',
264 },
265 },
266 };
267
268 my ($port, $config_user, $config_group) = config_write($config_file, $config);
269
270 # Open pipes, for use between the parent and child processes. Specifically,
271 # the child will indicate when it's done with its test by writing a message
272 # to the parent.
273 my ($rfh, $wfh);
274 unless (pipe($rfh, $wfh)) {
275 die("Can't open pipe: $!");
276 }
277
278 require Net::Telnet;
279
280 my $ex;
281
282 # Fork child
283 $self->handle_sigchld();
284 defined(my $pid = fork()) or die("Can't fork: $!");
285 if ($pid) {
286 eval {
287 sleep(1);
288
289 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
290 $client->login($user, $passwd);
291
292 my $name = "test";
293 $name .= chr(Net::Telnet::TELNET_IAC());
294 $name .= chr(Net::Telnet::TELNET_IAC());
295 $name .= ".txt";
296
297 my $conn = $client->stor_raw($name);
298 unless ($conn) {
299 die("STOR failed: " . $client->response_code() . " " .
300 $client->response_msg());
301 }
302
303 my $buf = "Foo!\n";
304 $conn->write($buf, length($buf));
305 $conn->close();
306
307 my ($resp_code, $resp_msg);
308 $resp_code = $client->response_code();
309 $resp_msg = $client->response_msg();
310
311 my $expected;
312
313 $expected = 226;
314 $self->assert($expected == $resp_code,
315 test_msg("Expected $expected, got $resp_code"));
316
317 $expected = "Transfer complete";
318 $self->assert($expected eq $resp_msg,
319 test_msg("Expected '$expected', got '$resp_msg'"));
320
321 $self->assert(-f $test_file,
322 test_msg("File $test_file does not exist as expected"));
323 };
324
325 if ($@) {
326 $ex = $@;
327 }
328
329 $wfh->print("done\n");
330 $wfh->flush();
331
332 } else {
333 eval { server_wait($config_file, $rfh) };
334 if ($@) {
335 warn($@);
336 exit 1;
337 }
338
339 exit 0;
340 }
341
342 # Stop server
343 server_stop($pid_file);
344
345 $self->assert_child_ok($pid);
346
347 if ($ex) {
348 die($ex);
349 }
350
351 unlink($log_file);
352 }
353
354 sub telnet_initial_crlf_issue1527 {
355 my $self = shift;
356 my $tmpdir = $self->{tmpdir};
357 my $setup = test_setup($tmpdir, 'telnet');
358
359 # To reproduce Issue #1527, we first send "\r\n", followed by USER.
360
361 my $config = {
362 PidFile => $setup->{pid_file},
363 ScoreboardFile => $setup->{scoreboard_file},
364 SystemLog => $setup->{log_file},
365 TraceLog => $setup->{log_file},
366 Trace => 'command:20 netio:20 response:20',
367
368 AuthUserFile => $setup->{auth_user_file},
369 AuthGroupFile => $setup->{auth_group_file},
370 AuthOrder => 'mod_auth_file.c',
371
372 IfModules => {
373 'mod_delay.c' => {
374 DelayEngine => 'off',
375 },
376 },
377 };
378
379 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
380 $config);
381
382 # Open pipes, for use between the parent and child processes. Specifically,
383 # the child will indicate when it's done with its test by writing a message
384 # to the parent.
385 my ($rfh, $wfh);
386 unless (pipe($rfh, $wfh)) {
387 die("Can't open pipe: $!");
388 }
389
390 require Net::Telnet;
391
392 my $ex;
393
394 # Fork child
395 $self->handle_sigchld();
396 defined(my $pid = fork()) or die("Can't fork: $!");
397 if ($pid) {
398 eval {
399 # Allow for server startup
400 sleep(1);
401
402 my $client = IO::Socket::INET->new(
403 PeerHost => '127.0.0.1',
404 PeerPort => $port,
405 Proto => 'tcp',
406 Type => SOCK_STREAM,
407 Timeout => 10
408 );
409 unless ($client) {
410 croak("Can't connect to 127.0.0.1:$port: $!");
411 }
412
413 # Send the initial CRLF
414 my $cmd = "\r\n";
415 if ($ENV{TEST_VERBOSE}) {
416 print STDERR "# Sending CRLF\n";
417 }
418
419 $client->print($cmd);
420 $client->flush();
421
422 # Read the banner
423 my $banner = <$client>;
424 chomp($banner);
425 if ($ENV{TEST_VERBOSE}) {
426 print STDERR "# Received banner: $banner\n";
427 }
428
429 # Read the CRLF response
430 my $resp = <$client>;
431 chomp($resp);
432 if ($ENV{TEST_VERBOSE}) {
433 print STDERR "# Received response: $resp\n";
434 }
435
436 # Send the USER command
437 $cmd = "USER $setup->{user}\r\n";
438 if ($ENV{TEST_VERBOSE}) {
439 print STDERR "# Sending command: $cmd";
440 }
441
442 $client->print($cmd);
443 $client->flush();
444
445 # Read the USER response
446 $resp = <$client>;
447 chomp($resp);
448 if ($ENV{TEST_VERBOSE}) {
449 print STDERR "# Received response: $resp\n";
450 }
451
452 my $expected = '^331 Password';
453 $self->assert(qr/$expected/, $resp,
454 test_msg("Expected response '$expected', got '$resp'"));
455
456 # Send the USER command again
457 if ($ENV{TEST_VERBOSE}) {
458 print STDERR "# Sending command: $cmd";
459 }
460
461 $client->print($cmd);
462 $client->flush();
463
464 # Read the USER response
465 $resp = <$client>;
466 chomp($resp);
467 if ($ENV{TEST_VERBOSE}) {
468 print STDERR "# Received response: $resp\n";
469 }
470
471 $client->close();
472
473 $self->assert(qr/$expected/, $resp,
474 test_msg("Expected response '$expected', got '$resp'"));
475 };
476 if ($@) {
477 $ex = $@;
478 }
479
480 $wfh->print("done\n");
481 $wfh->flush();
482
483 } else {
484 eval { server_wait($setup->{config_file}, $rfh) };
485 if ($@) {
486 warn($@);
487 exit 1;
488 }
489
490 exit 0;
491 }
492
493 # Stop server
494 server_stop($setup->{pid_file});
495 $self->assert_child_ok($pid);
496
497 test_cleanup($setup->{log_file}, $ex);
498 }
499
500 sub telnet_nonascii_uppercase_issue2019 {
501 my $self = shift;
502 my $tmpdir = $self->{tmpdir};
503 my $setup = test_setup($tmpdir, 'telnet');
504
505 my $config = {
506 PidFile => $setup->{pid_file},
507 ScoreboardFile => $setup->{scoreboard_file},
508 SystemLog => $setup->{log_file},
509 TraceLog => $setup->{log_file},
510 Trace => 'command:20 netio:30',
511
512 AuthUserFile => $setup->{auth_user_file},
513 AuthGroupFile => $setup->{auth_group_file},
514 AuthOrder => 'mod_auth_file.c',
515
516 IfModules => {
517 'mod_delay.c' => {
518 DelayEngine => 'off',
519 },
520 },
521 };
522
523 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
524 $config);
525
526 # Open pipes, for use between the parent and child processes. Specifically,
527 # the child will indicate when it's done with its test by writing a message
528 # to the parent.
529 my ($rfh, $wfh);
530 unless (pipe($rfh, $wfh)) {
531 die("Can't open pipe: $!");
532 }
533
534 require Net::Telnet;
535
536 my $ex;
537
538 # Fork child
539 $self->handle_sigchld();
540 defined(my $pid = fork()) or die("Can't fork: $!");
541 if ($pid) {
542 eval {
543 sleep(1);
544
545 # To reproduce Issue#2019, we only need to connect to the server,
546 # then send non-ASCII bytes.
547
548 my $client = Net::Telnet->new(
549 Host => '127.0.0.1',
550 Port => $port,
551 Timeout => 3,
552 Errmode => 'return',
553 );
554
555 my $buf = "\xe3\xe3\xe3\r\n";
556
557 my $res = $client->cmd(
558 String => $buf,
559 Prompt => "/$setup->{user}/",
560 );
561
562 if ($res) {
563 die("USER succeeded expectedly");
564 }
565
566 my $ex = $client->errmsg();
567 unless ($ex =~ /^command\s+timed/) {
568 die("Unexpected exception thrown: $ex");
569 }
570 };
571 if ($@) {
572 $ex = $@;
573 }
574
575 $wfh->print("done\n");
576 $wfh->flush();
577
578 } else {
579 eval { server_wait($setup->{config_file}, $rfh) };
580 if ($@) {
581 warn($@);
582 exit 1;
583 }
584
585 exit 0;
586 }
587
588 # Stop server
589 server_stop($setup->{pid_file});
590 $self->assert_child_ok($pid);
591
592 test_cleanup($setup->{log_file}, $ex);
593 }
594
595 1;