Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_snmp/tls.pm

raw
1 package ProFTPD::Tests::Modules::mod_snmp::tls;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Data::Dumper;
8 use File::Spec;
9 use IO::Handle;
10 use POSIX qw(:fcntl_h);
11
12 use ProFTPD::TestSuite::FTP;
13 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
14
15 $| = 1;
16
17 my $order = 0;
18
19 my $TESTS = {
20 snmp_tls_v1_get_sess_counts => {
21 order => ++$order,
22 test_class => [qw(forking snmp tls)],
23 },
24
25 snmp_tls_v1_get_xfer_dirlist_counts => {
26 order => ++$order,
27 test_class => [qw(forking snmp tls)],
28 },
29
30 snmp_tls_v1_get_xfer_download_counts => {
31 order => ++$order,
32 test_class => [qw(forking snmp tls)],
33 },
34
35 snmp_tls_v1_get_xfer_upload_counts => {
36 order => ++$order,
37 test_class => [qw(forking snmp tls)],
38 },
39
40 };
41
42 sub new {
43 return shift()->SUPER::new(@_);
44 }
45
46 sub list_tests {
47 # Check for the required Perl modules:
48 #
49 # Net-SNMP
50 # Net-FTPSSL
51
52 my $required = [qw(
53 Net::SNMP
54 Net::FTPSSL
55 )];
56
57 foreach my $req (@$required) {
58 eval "use $req";
59 if ($@) {
60 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n
61 ";
62
63 if ($ENV{TEST_VERBOSE}) {
64 print STDERR "Unable to load $req: $@\n";
65 }
66
67 return qw(testsuite_empty_test);
68 }
69 }
70
71 return testsuite_get_runnable_tests($TESTS);
72 }
73
74 # Support routines
75
76 sub get_tls_sess_info {
77 my $agent_port = shift;
78 my $snmp_community = shift;
79
80 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
81 -hostname => '127.0.0.1',
82 -port => $agent_port,
83 -version => 'snmpv1',
84 -community => $snmp_community,
85 -retries => 1,
86 -timeout => 3,
87 -translate => 1,
88 );
89 unless ($snmp_sess) {
90 die("Unable to create Net::SNMP session: $snmp_err");
91 }
92
93 if ($ENV{TEST_VERBOSE}) {
94 # From the Net::SNMP debug perldocs
95 my $debug_mask = (0x02|0x10|0x20);
96 $snmp_sess->debug($debug_mask);
97 }
98
99 # tlsSessions.sessionCount
100 my $sess_count_oid = '1.3.6.1.4.1.17852.2.2.5.1.1.0';
101
102 # tlsSessions.sessionTotal
103 my $sess_total_oid = '1.3.6.1.4.1.17852.2.2.5.1.2.0';
104
105 my $oids = [
106 $sess_count_oid,
107 $sess_total_oid,
108 ];
109
110 my $snmp_resp = $snmp_sess->get_request(
111 -varbindList => $oids,
112 );
113 unless ($snmp_resp) {
114 die("No SNMP response received: " . $snmp_sess->error());
115 }
116
117 my ($sess_count, $sess_total);
118
119 # Do we have the requested OIDs in the response?
120
121 foreach my $oid (@$oids) {
122 unless (defined($snmp_resp->{$oid})) {
123 die("Missing required OID $oid in response");
124 }
125
126 my $value = $snmp_resp->{$oid};
127 if ($ENV{TEST_VERBOSE}) {
128 print STDERR "Requested OID $oid = $value\n";
129 }
130
131 if ($oid eq $sess_count_oid) {
132 $sess_count = $value;
133
134 } elsif ($oid eq $sess_total_oid) {
135 $sess_total = $value;
136 }
137 }
138
139 $snmp_sess->close();
140 $snmp_sess = undef;
141
142 return ($sess_count, $sess_total);
143 }
144
145 sub get_tls_xfer_dirlist_info {
146 my $agent_port = shift;
147 my $snmp_community = shift;
148
149 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
150 -hostname => '127.0.0.1',
151 -port => $agent_port,
152 -version => 'snmpv1',
153 -community => $snmp_community,
154 -retries => 1,
155 -timeout => 3,
156 -translate => 1,
157 );
158 unless ($snmp_sess) {
159 die("Unable to create Net::SNMP session: $snmp_err");
160 }
161
162 if ($ENV{TEST_VERBOSE}) {
163 # From the Net::SNMP debug perldocs
164 my $debug_mask = (0x02|0x10|0x20);
165 $snmp_sess->debug($debug_mask);
166 }
167
168 # tlsDataTransfers.dirListCount
169 my $dirlist_count_oid = '1.3.6.1.4.1.17852.2.2.5.3.1.0';
170
171 # tlsDataTransfers.dirListTotal
172 my $dirlist_total_oid = '1.3.6.1.4.1.17852.2.2.5.3.2.0';
173
174 my $oids = [
175 $dirlist_count_oid,
176 $dirlist_total_oid,
177 ];
178
179 my $snmp_resp = $snmp_sess->get_request(
180 -varbindList => $oids,
181 );
182 unless ($snmp_resp) {
183 die("No SNMP response received: " . $snmp_sess->error());
184 }
185
186 my ($dirlist_count, $dirlist_total);
187
188 # Do we have the requested OIDs in the response?
189
190 foreach my $oid (@$oids) {
191 unless (defined($snmp_resp->{$oid})) {
192 die("Missing required OID $oid in response");
193 }
194
195 my $value = $snmp_resp->{$oid};
196 if ($ENV{TEST_VERBOSE}) {
197 print STDERR "Requested OID $oid = $value\n";
198 }
199
200 if ($oid eq $dirlist_count_oid) {
201 $dirlist_count = $value;
202
203 } elsif ($oid eq $dirlist_total_oid) {
204 $dirlist_total = $value;
205 }
206 }
207
208 $snmp_sess->close();
209 $snmp_sess = undef;
210
211 return ($dirlist_count, $dirlist_total);
212 }
213
214 sub get_tls_xfer_download_info {
215 my $agent_port = shift;
216 my $snmp_community = shift;
217
218 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
219 -hostname => '127.0.0.1',
220 -port => $agent_port,
221 -version => 'snmpv1',
222 -community => $snmp_community,
223 -retries => 1,
224 -timeout => 3,
225 -translate => 1,
226 );
227 unless ($snmp_sess) {
228 die("Unable to create Net::SNMP session: $snmp_err");
229 }
230
231 if ($ENV{TEST_VERBOSE}) {
232 # From the Net::SNMP debug perldocs
233 my $debug_mask = (0x02|0x10|0x20);
234 $snmp_sess->debug($debug_mask);
235 }
236
237 # tlsDataTransfers.fileDownloadCount
238 my $download_file_count_oid = '1.3.6.1.4.1.17852.2.2.5.3.7.0';
239
240 # tlsDataTransfers.fileDownloadTotal
241 my $download_file_total_oid = '1.3.6.1.4.1.17852.2.2.5.3.8.0';
242
243 # tlsDataTransfers.kbDownloadTotal
244 my $download_kb_oid = '1.3.6.1.4.1.17852.2.2.5.3.11.0';
245
246 my $oids = [
247 $download_file_count_oid,
248 $download_file_total_oid,
249 $download_kb_oid
250 ];
251
252 my $snmp_resp = $snmp_sess->get_request(
253 -varbindList => $oids,
254 );
255 unless ($snmp_resp) {
256 die("No SNMP response received: " . $snmp_sess->error());
257 }
258
259 my ($file_count, $file_total, $kb_count);
260
261 # Do we have the requested OIDs in the response?
262
263 foreach my $oid (@$oids) {
264 unless (defined($snmp_resp->{$oid})) {
265 die("Missing required OID $oid in response");
266 }
267
268 my $value = $snmp_resp->{$oid};
269 if ($ENV{TEST_VERBOSE}) {
270 print STDERR "Requested OID $oid = $value\n";
271 }
272
273 if ($oid eq $download_file_count_oid) {
274 $file_count = $value;
275
276 } elsif ($oid eq $download_file_total_oid) {
277 $file_total = $value;
278
279 } elsif ($oid eq $download_kb_oid) {
280 $kb_count = $value;
281 }
282 }
283
284 $snmp_sess->close();
285 $snmp_sess = undef;
286
287 return ($file_count, $file_total, $kb_count);
288 }
289
290 sub get_tls_xfer_upload_info {
291 my $agent_port = shift;
292 my $snmp_community = shift;
293
294 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
295 -hostname => '127.0.0.1',
296 -port => $agent_port,
297 -version => 'snmpv1',
298 -community => $snmp_community,
299 -retries => 1,
300 -timeout => 3,
301 -translate => 1,
302 );
303 unless ($snmp_sess) {
304 die("Unable to create Net::SNMP session: $snmp_err");
305 }
306
307 if ($ENV{TEST_VERBOSE}) {
308 # From the Net::SNMP debug perldocs
309 my $debug_mask = (0x02|0x10|0x20);
310 $snmp_sess->debug($debug_mask);
311 }
312
313 # tlsDataTransfers.fileUploadCount
314 my $upload_file_count_oid = '1.3.6.1.4.1.17852.2.2.5.3.4.0';
315
316 # tlsDataTransfers.fileUploadTotal
317 my $upload_file_total_oid = '1.3.6.1.4.1.17852.2.2.5.3.5.0';
318
319 # tlsDataTransfers.kbUploadTotal
320 my $upload_kb_oid = '1.3.6.1.4.1.17852.2.2.5.3.10.0';
321
322 my $oids = [
323 $upload_file_count_oid,
324 $upload_file_total_oid,
325 $upload_kb_oid
326 ];
327
328 my $snmp_resp = $snmp_sess->get_request(
329 -varbindList => $oids,
330 );
331 unless ($snmp_resp) {
332 die("No SNMP response received: " . $snmp_sess->error());
333 }
334
335 my ($file_count, $file_total, $kb_count);
336
337 # Do we have the requested OIDs in the response?
338
339 foreach my $oid (@$oids) {
340 unless (defined($snmp_resp->{$oid})) {
341 die("Missing required OID $oid in response");
342 }
343
344 my $value = $snmp_resp->{$oid};
345 if ($ENV{TEST_VERBOSE}) {
346 print STDERR "Requested OID $oid = $value\n";
347 }
348
349 if ($oid eq $upload_file_count_oid) {
350 $file_count = $value;
351
352 } elsif ($oid eq $upload_file_total_oid) {
353 $file_total = $value;
354
355 } elsif ($oid eq $upload_kb_oid) {
356 $kb_count = $value;
357 }
358 }
359
360 $snmp_sess->close();
361 $snmp_sess = undef;
362
363 return ($file_count, $file_total, $kb_count);
364 }
365
366 sub list_dir {
367 my $port = shift;
368 my $user = shift;
369 my $passwd = shift;
370 my $dir_path = shift;
371
372 require Net::FTPSSL;
373
374 my $client = Net::FTPSSL->new('127.0.0.1',
375 Encryption => 'E',
376 Port => $port,
377 );
378
379 unless ($client) {
380 die("Can't connect to FTPS server: " . IO::Socket::SSL::errstr());
381 }
382
383 unless ($client->login($user, $passwd)) {
384 die("Can't login: " . $client->last_message());
385 }
386
387 unless ($client->list($dir_path)) {
388 die("Can't list directory '$dir_path': " . $client->last_message());
389 }
390
391 $client->quit();
392
393 return 1;
394 }
395
396 sub download_file {
397 my $port = shift;
398 my $user = shift;
399 my $passwd = shift;
400 my $file_path = shift;
401 my $file_kb_len = shift;
402
403 require Net::FTPSSL;
404
405 my $client = Net::FTPSSL->new('127.0.0.1',
406 Encryption => 'E',
407 Port => $port,
408 );
409
410 unless ($client) {
411 die("Can't connect to FTPS server: " . IO::Socket::SSL::errstr());
412 }
413
414 unless ($client->login($user, $passwd)) {
415 die("Can't login: " . $client->last_message());
416 }
417
418 my $dst_path = '/dev/null';
419 unless ($client->get($file_path, $dst_path)) {
420 die("Can't download '$file_path' to '$dst_path': " .
421 $client->last_message());
422 }
423
424 $client->quit();
425
426 return 1;
427 }
428
429 sub upload_file {
430 my $port = shift;
431 my $user = shift;
432 my $passwd = shift;
433 my $file_path = shift;
434 my $file_kb_len = shift;
435 my $test_file = shift;
436
437 require Net::FTPSSL;
438
439 my $client = Net::FTPSSL->new('127.0.0.1',
440 Encryption => 'E',
441 Port => $port,
442 );
443
444 unless ($client) {
445 die("Can't connect to FTPS server: " . IO::Socket::SSL::errstr());
446 }
447
448 unless ($client->login($user, $passwd)) {
449 die("Can't login: " . $client->last_message());
450 }
451
452 if (open(my $fh, "> $test_file")) {
453 my $buf = ("A" x ($file_kb_len * 1024));
454 print $fh $buf;
455
456 unless (close($fh)) {
457 die("Can't write $test_file: $!");
458 }
459
460 } else {
461 die("Can't open $test_file: $!");
462 }
463
464 unless ($client->put($test_file, $file_path)) {
465 die("Can't upload '$test_file' to '$file_path': " .
466 $client->last_message());
467 }
468
469 $client->quit();
470 return 1;
471 }
472
473 # Test cases
474
475 sub snmp_tls_v1_get_sess_counts {
476 my $self = shift;
477 my $tmpdir = $self->{tmpdir};
478
479 my $config_file = "$tmpdir/snmp.conf";
480 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
481 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
482
483 my $log_file = test_get_logfile();
484
485 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
486 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
487
488 my $user = 'proftpd';
489 my $passwd = 'test';
490 my $group = 'ftpd';
491 my $home_dir = File::Spec->rel2abs($tmpdir);
492 my $uid = 500;
493 my $gid = 500;
494
495 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
496
497 # Make sure that, if we're running as root, that the home directory has
498 # permissions/privs set for the account we create
499 if ($< == 0) {
500 unless (chmod(0755, $home_dir, $table_dir)) {
501 die("Can't set perms on $home_dir to 0755: $!");
502 }
503
504 unless (chown($uid, $gid, $home_dir, $table_dir)) {
505 die("Can't set owner of $home_dir to $uid/$gid: $!");
506 }
507 }
508
509 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
510 '/bin/bash');
511 auth_group_write($auth_group_file, $group, $gid, $user);
512
513 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
514 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
515
516 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
517 my $snmp_community = "public";
518
519 my $timeout_idle = 45;
520
521 my $config = {
522 TraceLog => $log_file,
523 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
524 PidFile => $pid_file,
525 ScoreboardFile => $scoreboard_file,
526 SystemLog => $log_file,
527
528 AuthUserFile => $auth_user_file,
529 AuthGroupFile => $auth_group_file,
530 AuthOrder => 'mod_auth_file.c',
531
532 TimeoutIdle => $timeout_idle + 1,
533
534 IfModules => {
535 'mod_delay.c' => {
536 DelayEngine => 'off',
537 },
538
539 'mod_snmp.c' => {
540 SNMPAgent => "master 127.0.0.1:$agent_port",
541 SNMPCommunity => $snmp_community,
542 SNMPEngine => 'on',
543 SNMPLog => $log_file,
544 SNMPTables => $table_dir,
545 },
546
547 'mod_tls.c' => {
548 TLSEngine => 'on',
549 TLSLog => $log_file,
550 TLSRequired => 'on',
551 TLSRSACertificateFile => $cert_file,
552 TLSCACertificateFile => $ca_file,
553 TLSOptions => 'NoSessionReuseRequired',
554 },
555
556 },
557 };
558
559 my ($port, $config_user, $config_group) = config_write($config_file, $config);
560
561 # Open pipes, for use between the parent and child processes. Specifically,
562 # the child will indicate when it's done with its test by writing a message
563 # to the parent.
564 my ($rfh, $wfh);
565 unless (pipe($rfh, $wfh)) {
566 die("Can't open pipe: $!");
567 }
568
569 require Net::SNMP;
570 require Net::FTPSSL;
571
572 my $ex;
573
574 # Fork child
575 $self->handle_sigchld();
576 defined(my $pid = fork()) or die("Can't fork: $!");
577 if ($pid) {
578 eval {
579 # Give the server time to start up
580 sleep(2);
581
582 my $expected;
583
584 # First, get the session count
585 my ($sess_count, $sess_total) = get_tls_sess_info($agent_port,
586 $snmp_community);
587
588 $expected = 0;
589 $self->assert($sess_count == $expected,
590 test_msg("Expected session count $expected, got $sess_count"));
591
592 $expected = 0;
593 $self->assert($sess_total == $expected,
594 test_msg("Expected session total $expected, got $sess_total"));
595
596 # Now, connect
597 my $client = Net::FTPSSL->new('127.0.0.1',
598 Encryption => 'E',
599 Port => $port,
600 );
601
602 unless ($client) {
603 die("Can't connect to FTPS server: " . IO::Socket::SSL::errstr());
604 }
605
606 # And get the counts again.
607 ($sess_count, $sess_total) = get_tls_sess_info($agent_port, $snmp_community);
608
609 $expected = 1;
610 $self->assert($sess_count == $expected,
611 test_msg("Expected session count $expected, got $sess_count"));
612
613 $expected = 1;
614 $self->assert($sess_total == $expected,
615 test_msg("Expected session total $expected, got $sess_total"));
616
617 # Now log out
618 $client->quit();
619
620 # And get the session counts one more time, make sure it's what we expect
621 ($sess_count, $sess_total) = get_tls_sess_info($agent_port, $snmp_community);
622
623 # We MIGHT see a value of 1 here; I suspect it is leftover from the
624 # previous test (and in anonymous memory). Tolerate it, for now.
625 $expected = 0;
626 $self->assert($sess_count == 0 || $sess_count == 1,
627 test_msg("Expected session count $expected, got $sess_count"));
628
629 $expected = 1;
630 $self->assert($sess_total == $expected,
631 test_msg("Expected session total $expected, got $sess_total"));
632 };
633
634 if ($@) {
635 $ex = $@;
636 }
637
638 $wfh->print("done\n");
639 $wfh->flush();
640
641 } else {
642 eval { server_wait($config_file, $rfh, $timeout_idle) };
643 if ($@) {
644 warn($@);
645 exit 1;
646 }
647
648 exit 0;
649 }
650
651 # Stop server
652 server_stop($pid_file);
653
654 $self->assert_child_ok($pid);
655
656 if ($ex) {
657 test_append_logfile($log_file, $ex);
658 unlink($log_file);
659
660 die($ex);
661 }
662
663 unlink($log_file);
664 }
665
666 sub snmp_tls_v1_get_xfer_dirlist_counts {
667 my $self = shift;
668 my $tmpdir = $self->{tmpdir};
669
670 my $config_file = "$tmpdir/snmp.conf";
671 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
672 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
673
674 my $log_file = test_get_logfile();
675
676 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
677 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
678
679 my $user = 'proftpd';
680 my $passwd = 'test';
681 my $group = 'ftpd';
682 my $home_dir = File::Spec->rel2abs($tmpdir);
683 my $uid = 500;
684 my $gid = 500;
685
686 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
687
688 # Make sure that, if we're running as root, that the home directory has
689 # permissions/privs set for the account we create
690 if ($< == 0) {
691 unless (chmod(0755, $home_dir, $table_dir)) {
692 die("Can't set perms on $home_dir to 0755: $!");
693 }
694
695 unless (chown($uid, $gid, $home_dir, $table_dir)) {
696 die("Can't set owner of $home_dir to $uid/$gid: $!");
697 }
698 }
699
700 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
701 '/bin/bash');
702 auth_group_write($auth_group_file, $group, $gid, $user);
703
704 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
705 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
706
707 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
708 my $snmp_community = "public";
709
710 my $timeout_idle = 45;
711
712 my $config = {
713 TraceLog => $log_file,
714 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
715 PidFile => $pid_file,
716 ScoreboardFile => $scoreboard_file,
717 SystemLog => $log_file,
718
719 AuthUserFile => $auth_user_file,
720 AuthGroupFile => $auth_group_file,
721 AuthOrder => 'mod_auth_file.c',
722
723 TimeoutIdle => $timeout_idle + 1,
724
725 IfModules => {
726 'mod_delay.c' => {
727 DelayEngine => 'off',
728 },
729
730 'mod_snmp.c' => {
731 SNMPAgent => "master 127.0.0.1:$agent_port",
732 SNMPCommunity => $snmp_community,
733 SNMPEngine => 'on',
734 SNMPLog => $log_file,
735 SNMPTables => $table_dir,
736 },
737
738 'mod_tls.c' => {
739 TLSEngine => 'on',
740 TLSLog => $log_file,
741 TLSRequired => 'on',
742 TLSRSACertificateFile => $cert_file,
743 TLSCACertificateFile => $ca_file,
744 TLSOptions => 'NoSessionReuseRequired',
745 },
746
747 },
748 };
749
750 my ($port, $config_user, $config_group) = config_write($config_file, $config);
751
752 # Open pipes, for use between the parent and child processes. Specifically,
753 # the child will indicate when it's done with its test by writing a message
754 # to the parent.
755 my ($rfh, $wfh);
756 unless (pipe($rfh, $wfh)) {
757 die("Can't open pipe: $!");
758 }
759
760 require Net::SNMP;
761 require Net::FTPSSL;
762
763 my $ex;
764
765 # Fork child
766 $self->handle_sigchld();
767 defined(my $pid = fork()) or die("Can't fork: $!");
768 if ($pid) {
769 eval {
770 my $expected;
771
772 # First, get the dirlist counts
773 my ($dirlist_count, $dirlist_total) = get_tls_xfer_dirlist_info($agent_port, $snmp_community);
774
775 $expected = 0;
776 $self->assert($dirlist_count == $expected,
777 test_msg("Expected directory list count $expected, got $dirlist_count"));
778
779 $expected = 0;
780 $self->assert($dirlist_total == $expected,
781 test_msg("Expected directory list total $expected, got $dirlist_total"));
782
783 # Next, list a directory
784 list_dir($port, $user, $passwd, '.');
785
786 # Then, get the dirlist count again
787 ($dirlist_count, $dirlist_total) = get_tls_xfer_dirlist_info($agent_port, $snmp_community);
788
789 $expected = 0;
790 $self->assert($dirlist_count == $expected,
791 test_msg("Expected directory list count $expected, got $dirlist_count"));
792
793 $expected = 1;
794 $self->assert($dirlist_total == $expected,
795 test_msg("Expected directory list total $expected, got $dirlist_total"));
796
797 # List another directory
798 list_dir($port, $user, $passwd, '.');
799
800 # Get the dirlist count one more time, make sure it's what we expect
801 ($dirlist_count, $dirlist_total) = get_tls_xfer_dirlist_info($agent_port, $snmp_community);
802
803 $expected = 0;
804 $self->assert($dirlist_count == $expected,
805 test_msg("Expected directory list count $expected, got $dirlist_count"));
806
807 $expected = 2;
808 $self->assert($dirlist_total == $expected,
809 test_msg("Expected directory list total $expected, got $dirlist_total"));
810
811 # Now wait for 5 secs, then try again (make sure the counters aren't
812 # reset somehow on the server's side).
813 sleep(5);
814
815 ($dirlist_count, $dirlist_total) = get_tls_xfer_dirlist_info($agent_port, $snmp_community);
816
817 $expected = 0;
818 $self->assert($dirlist_count == $expected,
819 test_msg("Expected directory list count $expected, got $dirlist_count"));
820
821 $expected = 2;
822 $self->assert($dirlist_total == $expected,
823 test_msg("Expected directory list total $expected, got $dirlist_total"));
824 };
825
826 if ($@) {
827 $ex = $@;
828 }
829
830 $wfh->print("done\n");
831 $wfh->flush();
832
833 } else {
834 eval { server_wait($config_file, $rfh, $timeout_idle) };
835 if ($@) {
836 warn($@);
837 exit 1;
838 }
839
840 exit 0;
841 }
842
843 # Stop server
844 server_stop($pid_file);
845
846 $self->assert_child_ok($pid);
847
848 if ($ex) {
849 test_append_logfile($log_file, $ex);
850 unlink($log_file);
851
852 die($ex);
853 }
854
855 unlink($log_file);
856 }
857
858 sub snmp_tls_v1_get_xfer_download_counts {
859 my $self = shift;
860 my $tmpdir = $self->{tmpdir};
861
862 my $config_file = "$tmpdir/snmp.conf";
863 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
864 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
865
866 my $log_file = test_get_logfile();
867
868 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
869 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
870
871 my $user = 'proftpd';
872 my $passwd = 'test';
873 my $group = 'ftpd';
874 my $home_dir = File::Spec->rel2abs($tmpdir);
875 my $uid = 500;
876 my $gid = 500;
877
878 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
879
880 # Make sure that, if we're running as root, that the home directory has
881 # permissions/privs set for the account we create
882 if ($< == 0) {
883 unless (chmod(0755, $home_dir, $table_dir)) {
884 die("Can't set perms on $home_dir to 0755: $!");
885 }
886
887 unless (chown($uid, $gid, $home_dir, $table_dir)) {
888 die("Can't set owner of $home_dir to $uid/$gid: $!");
889 }
890 }
891
892 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
893 '/bin/bash');
894 auth_group_write($auth_group_file, $group, $gid, $user);
895
896 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
897 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
898
899 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
900 my $snmp_community = "public";
901
902 my $timeout_idle = 45;
903
904 my $config = {
905 TraceLog => $log_file,
906 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
907 PidFile => $pid_file,
908 ScoreboardFile => $scoreboard_file,
909 SystemLog => $log_file,
910
911 AuthUserFile => $auth_user_file,
912 AuthGroupFile => $auth_group_file,
913 AuthOrder => 'mod_auth_file.c',
914
915 TimeoutIdle => $timeout_idle + 1,
916
917 IfModules => {
918 'mod_delay.c' => {
919 DelayEngine => 'off',
920 },
921
922 'mod_snmp.c' => {
923 SNMPAgent => "master 127.0.0.1:$agent_port",
924 SNMPCommunity => $snmp_community,
925 SNMPEngine => 'on',
926 SNMPLog => $log_file,
927 SNMPTables => $table_dir,
928 },
929
930 'mod_tls.c' => {
931 TLSEngine => 'on',
932 TLSLog => $log_file,
933 TLSRequired => 'on',
934 TLSRSACertificateFile => $cert_file,
935 TLSCACertificateFile => $ca_file,
936 TLSOptions => 'NoSessionReuseRequired',
937 },
938
939 },
940 };
941
942 my ($port, $config_user, $config_group) = config_write($config_file, $config);
943
944 # Open pipes, for use between the parent and child processes. Specifically,
945 # the child will indicate when it's done with its test by writing a message
946 # to the parent.
947 my ($rfh, $wfh);
948 unless (pipe($rfh, $wfh)) {
949 die("Can't open pipe: $!");
950 }
951
952 require Net::SNMP;
953 require Net::FTPSSL;
954
955 my $ex;
956
957 # Fork child
958 $self->handle_sigchld();
959 defined(my $pid = fork()) or die("Can't fork: $!");
960 if ($pid) {
961 eval {
962 my $expected;
963
964 # First, get the download count/KB
965 my ($file_count, $file_total, $kb_count) = get_tls_xfer_download_info($agent_port, $snmp_community);
966
967 $expected = 0;
968 $self->assert($file_count == $expected,
969 test_msg("Expected download file count $expected, got $file_count"));
970
971 $expected = 0;
972 $self->assert($file_total == $expected,
973 test_msg("Expected download file total $expected, got $file_total"));
974
975 $expected = 0;
976 $self->assert($kb_count == $expected,
977 test_msg("Expected download KB count $expected, got $kb_count"));
978
979 # Next, download a file
980 my $file_name = 'test1.txt';
981 my $file_path = File::Spec->rel2abs("$tmpdir/$file_name");
982 my $file_kb_len = 4;
983
984 if (open(my $fh, "> $file_path")) {
985 my $buf = ("A" x ($file_kb_len * 1024));
986 print $fh $buf;
987 unless (close($fh)) {
988 die("Can't write $file_path: $!");
989 }
990
991 } else {
992 die("Can't open $file_path: $!");
993 }
994
995 download_file($port, $user, $passwd, $file_name, $file_kb_len);
996
997 # Then, get the download count/KB again
998 ($file_count, $file_total, $kb_count) = get_tls_xfer_download_info($agent_port, $snmp_community);
999
1000 $expected = 0;
1001 $self->assert($file_count == $expected,
1002 test_msg("Expected download file count $expected, got $file_count"));
1003
1004 $expected = 1;
1005 $self->assert($file_total == $expected,
1006 test_msg("Expected download file total $expected, got $file_total"));
1007
1008 $expected = $file_kb_len;
1009 $self->assert($kb_count == $expected,
1010 test_msg("Expected download KB count $expected, got $kb_count"));
1011
1012 # Download another file
1013 $file_name = 'test2.txt';
1014 $file_path = File::Spec->rel2abs("$tmpdir/$file_name");
1015
1016 if (open(my $fh, "> $file_path")) {
1017 my $buf = ("A" x ($file_kb_len * 1024));
1018 print $fh $buf;
1019 unless (close($fh)) {
1020 die("Can't write $file_path: $!");
1021 }
1022
1023 } else {
1024 die("Can't open $file_path: $!");
1025 }
1026
1027 download_file($port, $user, $passwd, $file_name, $file_kb_len);
1028
1029 # Get the download count/KB one more time, make sure it's what we expect
1030 ($file_count, $file_total, $kb_count) = get_tls_xfer_download_info($agent_port, $snmp_community);
1031
1032 $expected = 0;
1033 $self->assert($file_count == $expected,
1034 test_msg("Expected download file count $expected, got $file_count"));
1035
1036 $expected = 2;
1037 $self->assert($file_total == $expected,
1038 test_msg("Expected download file total $expected, got $file_total"));
1039
1040 $expected = ($file_kb_len * 2);
1041 $self->assert($kb_count == $expected,
1042 test_msg("Expected download KB count $expected, got $kb_count"));
1043
1044 # Now wait for 5 secs, then try again (make sure the counters aren't
1045 # reset somehow on the server's side).
1046 sleep(5);
1047
1048 ($file_count, $file_total, $kb_count) = get_tls_xfer_download_info($agent_port, $snmp_community);
1049
1050 $expected = 0;
1051 $self->assert($file_count == $expected,
1052 test_msg("Expected download file count $expected, got $file_count"));
1053
1054 $expected = 2;
1055 $self->assert($file_total == $expected,
1056 test_msg("Expected upload file total $expected, got $file_total"));
1057
1058 $expected = ($file_kb_len * 2);
1059 $self->assert($kb_count == $expected,
1060 test_msg("Expected download KB count $expected, got $kb_count"));
1061 };
1062
1063 if ($@) {
1064 $ex = $@;
1065 }
1066
1067 $wfh->print("done\n");
1068 $wfh->flush();
1069
1070 } else {
1071 eval { server_wait($config_file, $rfh, $timeout_idle) };
1072 if ($@) {
1073 warn($@);
1074 exit 1;
1075 }
1076
1077 exit 0;
1078 }
1079
1080 # Stop server
1081 server_stop($pid_file);
1082
1083 $self->assert_child_ok($pid);
1084
1085 if ($ex) {
1086 test_append_logfile($log_file, $ex);
1087 unlink($log_file);
1088
1089 die($ex);
1090 }
1091
1092 unlink($log_file);
1093 }
1094
1095 sub snmp_tls_v1_get_xfer_upload_counts {
1096 my $self = shift;
1097 my $tmpdir = $self->{tmpdir};
1098
1099 my $config_file = "$tmpdir/snmp.conf";
1100 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
1101 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
1102
1103 my $log_file = test_get_logfile();
1104
1105 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
1106 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
1107
1108 my $user = 'proftpd';
1109 my $passwd = 'test';
1110 my $group = 'ftpd';
1111 my $home_dir = File::Spec->rel2abs($tmpdir);
1112 my $uid = 500;
1113 my $gid = 500;
1114
1115 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
1116
1117 # Make sure that, if we're running as root, that the home directory has
1118 # permissions/privs set for the account we create
1119 if ($< == 0) {
1120 unless (chmod(0755, $home_dir, $table_dir)) {
1121 die("Can't set perms on $home_dir to 0755: $!");
1122 }
1123
1124 unless (chown($uid, $gid, $home_dir, $table_dir)) {
1125 die("Can't set owner of $home_dir to $uid/$gid: $!");
1126 }
1127 }
1128
1129 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1130 '/bin/bash');
1131 auth_group_write($auth_group_file, $group, $gid, $user);
1132
1133 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
1134 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
1135
1136 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
1137 my $snmp_community = "public";
1138
1139 my $timeout_idle = 45;
1140
1141 my $config = {
1142 TraceLog => $log_file,
1143 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
1144 PidFile => $pid_file,
1145 ScoreboardFile => $scoreboard_file,
1146 SystemLog => $log_file,
1147
1148 AuthUserFile => $auth_user_file,
1149 AuthGroupFile => $auth_group_file,
1150 AuthOrder => 'mod_auth_file.c',
1151
1152 TimeoutIdle => $timeout_idle + 1,
1153
1154 IfModules => {
1155 'mod_delay.c' => {
1156 DelayEngine => 'off',
1157 },
1158
1159 'mod_snmp.c' => {
1160 SNMPAgent => "master 127.0.0.1:$agent_port",
1161 SNMPCommunity => $snmp_community,
1162 SNMPEngine => 'on',
1163 SNMPLog => $log_file,
1164 SNMPTables => $table_dir,
1165 },
1166
1167 'mod_tls.c' => {
1168 TLSEngine => 'on',
1169 TLSLog => $log_file,
1170 TLSRequired => 'on',
1171 TLSRSACertificateFile => $cert_file,
1172 TLSCACertificateFile => $ca_file,
1173 TLSOptions => 'NoSessionReuseRequired',
1174 },
1175
1176 },
1177 };
1178
1179 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1180
1181 # Open pipes, for use between the parent and child processes. Specifically,
1182 # the child will indicate when it's done with its test by writing a message
1183 # to the parent.
1184 my ($rfh, $wfh);
1185 unless (pipe($rfh, $wfh)) {
1186 die("Can't open pipe: $!");
1187 }
1188
1189 require Net::SNMP;
1190 require Net::FTPSSL;
1191
1192 my $ex;
1193
1194 # Fork child
1195 $self->handle_sigchld();
1196 defined(my $pid = fork()) or die("Can't fork: $!");
1197 if ($pid) {
1198 eval {
1199 my $expected;
1200
1201 # First, get the upload count/KB
1202 my ($file_count, $file_total, $kb_count) = get_tls_xfer_upload_info($agent_port, $snmp_community);
1203
1204 $expected = 0;
1205 $self->assert($file_count == $expected,
1206 test_msg("Expected upload file count $expected, got $file_count"));
1207
1208 $expected = 0;
1209 $self->assert($file_total == $expected,
1210 test_msg("Expected upload file total $expected, got $file_total"));
1211
1212 $expected = 0;
1213 $self->assert($kb_count == $expected,
1214 test_msg("Expected upload KB count $expected, got $kb_count"));
1215
1216 # Next, upload a file
1217 my $file_path = 'test1.txt';
1218 my $file_kb_len = 4;
1219 my $test_file = File::Spec->rel2abs("$tmpdir/test1.tmp");
1220 upload_file($port, $user, $passwd, $file_path, $file_kb_len, $test_file);
1221
1222 # Then, get the upload count/KB again
1223 ($file_count, $file_total, $kb_count) = get_tls_xfer_upload_info($agent_port, $snmp_community);
1224
1225 $expected = 0;
1226 $self->assert($file_count == $expected,
1227 test_msg("Expected upload file count $expected, got $file_count"));
1228
1229 $expected = 1;
1230 $self->assert($file_total == $expected,
1231 test_msg("Expected upload file total $expected, got $file_total"));
1232
1233 $expected = $file_kb_len;
1234 $self->assert($kb_count == $expected,
1235 test_msg("Expected upload KB count $expected, got $kb_count"));
1236
1237 # Upload another file
1238 $file_path = 'test2.txt';
1239 $test_file = File::Spec->rel2abs("$tmpdir/test2.tmp");
1240 upload_file($port, $user, $passwd, $file_path, $file_kb_len, $test_file);
1241
1242 # Get the upload count/KB one more time, make sure it's what we expect
1243 ($file_count, $file_total, $kb_count) = get_tls_xfer_upload_info($agent_port, $snmp_community);
1244
1245 $expected = 0;
1246 $self->assert($file_count == $expected,
1247 test_msg("Expected upload file count $expected, got $file_count"));
1248
1249 $expected = 2;
1250 $self->assert($file_total == $expected,
1251 test_msg("Expected upload file total $expected, got $file_total"));
1252
1253 $expected = ($file_kb_len * 2);
1254 $self->assert($kb_count == $expected,
1255 test_msg("Expected upload KB count $expected, got $kb_count"));
1256
1257 # Now wait for 5 secs, then try again (make sure the counters aren't
1258 # reset somehow on the server's side).
1259 sleep(5);
1260
1261 ($file_count, $file_total, $kb_count) = get_tls_xfer_upload_info($agent_port, $snmp_community);
1262
1263 $expected = 0;
1264 $self->assert($file_count == $expected,
1265 test_msg("Expected upload file count $expected, got $file_count"));
1266
1267 $expected = 2;
1268 $self->assert($file_total == $expected,
1269 test_msg("Expected upload file total $expected, got $file_total"));
1270
1271 $expected = ($file_kb_len * 2);
1272 $self->assert($kb_count == $expected,
1273 test_msg("Expected upload KB count $expected, got $kb_count"));
1274 };
1275
1276 if ($@) {
1277 $ex = $@;
1278 }
1279
1280 $wfh->print("done\n");
1281 $wfh->flush();
1282
1283 } else {
1284 eval { server_wait($config_file, $rfh, $timeout_idle) };
1285 if ($@) {
1286 warn($@);
1287 exit 1;
1288 }
1289
1290 exit 0;
1291 }
1292
1293 # Stop server
1294 server_stop($pid_file);
1295
1296 $self->assert_child_ok($pid);
1297
1298 if ($ex) {
1299 test_append_logfile($log_file, $ex);
1300 unlink($log_file);
1301
1302 die($ex);
1303 }
1304
1305 unlink($log_file);
1306 }
1307
1308 1;