Debian Codegraph @

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

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