Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_snmp;
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
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :features :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 snmp_start_existing_dirs => {
20 order => ++$order,
21 test_class => [qw(forking snmp)],
22 },
23
24 snmp_v1_get_unknown => {
25 order => ++$order,
26 test_class => [qw(forking snmp)],
27 },
28
29 snmp_v1_get_wrong_community => {
30 order => ++$order,
31 test_class => [qw(forking snmp)],
32 },
33
34 snmp_v1_get_daemon_software => {
35 order => ++$order,
36 test_class => [qw(forking snmp)],
37 },
38
39 snmp_v1_get_daemon_version => {
40 order => ++$order,
41 test_class => [qw(forking snmp)],
42 },
43
44 snmp_v1_get_daemon_admin => {
45 order => ++$order,
46 test_class => [qw(forking snmp)],
47 },
48
49 snmp_v1_get_daemon_admin_with_config => {
50 order => ++$order,
51 test_class => [qw(forking snmp)],
52 },
53
54 snmp_v1_get_daemon_uptime => {
55 order => ++$order,
56 test_class => [qw(forking snmp)],
57 },
58
59 snmp_v1_get_daemon_vhost_count => {
60 order => ++$order,
61 test_class => [qw(forking snmp)],
62 },
63
64 # XXX Add snmp_v1_get_daemon_vhost_count w/ 2 vhosts in config
65
66 snmp_v1_get_daemon_conn_counts => {
67 order => ++$order,
68 test_class => [qw(forking snmp)],
69 },
70
71 snmp_v1_get_daemon_restart_count => {
72 order => ++$order,
73 test_class => [qw(forking snmp)],
74 },
75
76 snmp_v1_get_daemon_segfault_count => {
77 order => ++$order,
78 test_class => [qw(forking snmp)],
79 },
80
81 snmp_v1_get_daemon_maxinsts_count => {
82 order => ++$order,
83 test_class => [qw(forking snmp)],
84 },
85
86 snmp_v1_get_daemon_connrefused_count => {
87 order => ++$order,
88 test_class => [qw(forking snmp)],
89 },
90
91 # XXX Need unit tests for the ftp.sessions, ftp.logins, ftp.dataTransfers
92 snmp_v1_get_ftp_sess_counts => {
93 order => ++$order,
94 test_class => [qw(forking snmp)],
95 },
96
97 snmp_v1_get_ftp_xfer_upload_counts => {
98 order => ++$order,
99 test_class => [qw(forking snmp)],
100 },
101
102 snmp_v1_get_multi => {
103 order => ++$order,
104 test_class => [qw(forking snmp)],
105 },
106
107 snmp_v1_get_multi_with_unknown => {
108 order => ++$order,
109 test_class => [qw(forking snmp)],
110 },
111
112 snmp_v1_get_next => {
113 order => ++$order,
114 test_class => [qw(forking snmp)],
115 },
116
117 snmp_v1_get_next_unknown => {
118 order => ++$order,
119 test_class => [qw(forking snmp)],
120 },
121
122 snmp_v1_get_next_missing_instance_id => {
123 order => ++$order,
124 test_class => [qw(forking snmp)],
125 },
126
127 snmp_v1_get_next_end_of_mib_view => {
128 order => ++$order,
129 test_class => [qw(forking snmp)],
130 },
131
132 snmp_v1_get_next_multi => {
133 order => ++$order,
134 test_class => [qw(forking snmp)],
135 },
136
137 snmp_v1_get_next_multi_with_unknown => {
138 order => ++$order,
139 test_class => [qw(forking snmp)],
140 },
141
142 snmp_v1_set => {
143 order => ++$order,
144 test_class => [qw(forking snmp)],
145 },
146
147 # snmp_v1_trap
148 # XXX Net::SNMP's session->trap() method seems buggy; often crashes/
149 # unit tests.
150
151 snmp_v2_get_unknown => {
152 order => ++$order,
153 test_class => [qw(forking snmp)],
154 },
155
156 snmp_v2_get_missing_instance_id => {
157 order => ++$order,
158 test_class => [qw(forking snmp)],
159 },
160
161 snmp_v2_get_next_end_of_mib_view => {
162 order => ++$order,
163 test_class => [qw(forking snmp)],
164 },
165
166 snmp_v2_get_bulk => {
167 order => ++$order,
168 test_class => [qw(forking snmp)],
169 },
170
171 snmp_v2_get_bulk_max_repetitions_only => {
172 order => ++$order,
173 test_class => [qw(forking snmp)],
174 },
175
176 snmp_v2_get_bulk_end_of_mib_view => {
177 order => ++$order,
178 test_class => [qw(forking snmp)],
179 },
180
181 snmp_v2_set_no_access => {
182 order => ++$order,
183 test_class => [qw(forking snmp)],
184 },
185
186 # snmp_v3_XXX
187
188 snmp_config_limit => {
189 order => ++$order,
190 test_class => [qw(forking snmp)],
191 },
192
193 snmp_config_max_variables => {
194 order => ++$order,
195 test_class => [qw(forking snmp)],
196 },
197 };
198
199 sub new {
200 return shift()->SUPER::new(@_);
201 }
202
203 sub list_tests {
204 # Check for the required Perl modules:
205 #
206 # Net-SNMP
207
208 my $required = [qw(
209 Net::SNMP
210 )];
211
212 foreach my $req (@$required) {
213 eval "use $req";
214 if ($@) {
215 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n
216 ";
217
218 if ($ENV{TEST_VERBOSE}) {
219 print STDERR "Unable to load $req: $@\n";
220 }
221
222 return qw(testsuite_empty_test);
223 }
224 }
225
226 return testsuite_get_runnable_tests($TESTS);
227 }
228
229 # Support routines
230
231 sub get_conn_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 # connectionCount
255 my $conn_count_oid = '1.3.6.1.4.1.17852.2.2.1.6.0';
256
257 # connectionTotal
258 my $conn_total_oid = '1.3.6.1.4.1.17852.2.2.1.7.0';
259
260 my $oids = [$conn_count_oid, $conn_total_oid];
261
262 my $snmp_resp = $snmp_sess->get_request(
263 -varbindList => $oids,
264 );
265 unless ($snmp_resp) {
266 die("No SNMP response received: " . $snmp_sess->error());
267 }
268
269 my ($conn_count, $conn_total);
270
271 # Do we have the requested OIDs in the response?
272
273 foreach my $oid (@$oids) {
274 unless (defined($snmp_resp->{$oid})) {
275 die("Missing required OID $oid in response");
276 }
277
278 my $value = $snmp_resp->{$oid};
279 if ($ENV{TEST_VERBOSE}) {
280 print STDERR "Requested OID $oid = $value\n";
281 }
282
283 if ($oid eq $conn_count_oid) {
284 $conn_count = $value;
285
286 } elsif ($oid eq $conn_total_oid) {
287 $conn_total = $value;
288 }
289 }
290
291 $snmp_sess->close();
292 $snmp_sess = undef;
293
294 return ($conn_count, $conn_total);
295 }
296
297 sub get_ftp_sess_info {
298 my $agent_port = shift;
299 my $snmp_community = shift;
300
301 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
302 -hostname => '127.0.0.1',
303 -port => $agent_port,
304 -version => 'snmpv1',
305 -community => $snmp_community,
306 -retries => 1,
307 -timeout => 3,
308 -translate => 1,
309 );
310 unless ($snmp_sess) {
311 die("Unable to create Net::SNMP session: $snmp_err");
312 }
313
314 if ($ENV{TEST_VERBOSE}) {
315 # From the Net::SNMP debug perldocs
316 my $debug_mask = (0x02|0x10|0x20);
317 $snmp_sess->debug($debug_mask);
318 }
319
320 # sessionCount
321 my $sess_count_oid = '1.3.6.1.4.1.17852.2.2.3.1.1.0';
322
323 # fileUploadTotal
324 my $sess_total_oid = '1.3.6.1.4.1.17852.2.2.3.1.2.0';
325
326 my $oids = [
327 $sess_count_oid,
328 $sess_total_oid,
329 ];
330
331 my $snmp_resp = $snmp_sess->get_request(
332 -varbindList => $oids,
333 );
334 unless ($snmp_resp) {
335 die("No SNMP response received: " . $snmp_sess->error());
336 }
337
338 my ($sess_count, $sess_total);
339
340 # Do we have the requested OIDs in the response?
341
342 foreach my $oid (@$oids) {
343 unless (defined($snmp_resp->{$oid})) {
344 die("Missing required OID $oid in response");
345 }
346
347 my $value = $snmp_resp->{$oid};
348 if ($ENV{TEST_VERBOSE}) {
349 print STDERR "Requested OID $oid = $value\n";
350 }
351
352 if ($oid eq $sess_count_oid) {
353 $sess_count = $value;
354
355 } elsif ($oid eq $sess_total_oid) {
356 $sess_total = $value;
357 }
358 }
359
360 $snmp_sess->close();
361 $snmp_sess = undef;
362
363 return ($sess_count, $sess_total);
364 }
365
366 sub get_ftp_xfer_upload_info {
367 my $agent_port = shift;
368 my $snmp_community = shift;
369
370 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
371 -hostname => '127.0.0.1',
372 -port => $agent_port,
373 -version => 'snmpv1',
374 -community => $snmp_community,
375 -retries => 1,
376 -timeout => 3,
377 -translate => 1,
378 );
379 unless ($snmp_sess) {
380 die("Unable to create Net::SNMP session: $snmp_err");
381 }
382
383 if ($ENV{TEST_VERBOSE}) {
384 # From the Net::SNMP debug perldocs
385 my $debug_mask = (0x02|0x10|0x20);
386 $snmp_sess->debug($debug_mask);
387 }
388
389 # fileUploadCount
390 my $upload_file_count_oid = '1.3.6.1.4.1.17852.2.2.3.3.4.0';
391
392 # fileUploadTotal
393 my $upload_file_total_oid = '1.3.6.1.4.1.17852.2.2.3.3.5.0';
394
395 # kbUploadTotal
396 my $upload_kb_oid = '1.3.6.1.4.1.17852.2.2.3.3.10.0';
397
398 my $oids = [
399 $upload_file_count_oid,
400 $upload_file_total_oid,
401 $upload_kb_oid
402 ];
403
404 my $snmp_resp = $snmp_sess->get_request(
405 -varbindList => $oids,
406 );
407 unless ($snmp_resp) {
408 die("No SNMP response received: " . $snmp_sess->error());
409 }
410
411 my ($file_count, $file_total, $kb_count);
412
413 # Do we have the requested OIDs in the response?
414
415 foreach my $oid (@$oids) {
416 unless (defined($snmp_resp->{$oid})) {
417 die("Missing required OID $oid in response");
418 }
419
420 my $value = $snmp_resp->{$oid};
421 if ($ENV{TEST_VERBOSE}) {
422 print STDERR "Requested OID $oid = $value\n";
423 }
424
425 if ($oid eq $upload_file_count_oid) {
426 $file_count = $value;
427
428 } elsif ($oid eq $upload_file_total_oid) {
429 $file_total = $value;
430
431 } elsif ($oid eq $upload_kb_oid) {
432 $kb_count = $value;
433 }
434 }
435
436 $snmp_sess->close();
437 $snmp_sess = undef;
438
439 return ($file_count, $file_total, $kb_count);
440 }
441
442 sub upload_file {
443 my $port = shift;
444 my $user = shift;
445 my $passwd = shift;
446 my $file_path = shift;
447 my $file_kb_len = shift;
448
449 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
450
451 $client->login($user, $passwd);
452
453 my $conn = $client->stor_raw($file_path);
454 unless ($conn) {
455 die("Failed to STOR: " . $client->response_code() . " " .
456 $client->response_msg());
457 }
458
459 my $buf = ("A" x ($file_kb_len * 1024));
460 $conn->write($buf, length($buf), 25);
461 eval { $conn->close() };
462
463 my ($resp_code, $resp_msg);
464 $resp_code = $client->response_code();
465 $resp_msg = $client->response_msg();
466
467 $client->quit();
468 $client = undef;
469
470 my $expected;
471 $expected = 226;
472 unless ($expected == $resp_code) {
473 die("Unexpected response code $resp_code (expected $expected)");
474 }
475
476 $expected = "Transfer complete";
477 unless ($expected eq $resp_msg) {
478 die("Unexpected response message $resp_msg (expected $expected)");
479 }
480
481 return 1;
482 }
483
484 # Test cases
485
486 sub snmp_start_existing_dirs {
487 my $self = shift;
488 my $tmpdir = $self->{tmpdir};
489
490 my $config_file = "$tmpdir/snmp.conf";
491 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
492 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
493
494 my $log_file = test_get_logfile();
495
496 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
497 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
498
499 my $user = 'proftpd';
500 my $passwd = 'test';
501 my $group = 'ftpd';
502 my $home_dir = File::Spec->rel2abs($tmpdir);
503 my $uid = 500;
504 my $gid = 500;
505
506 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
507
508 # Make sure that, if we're running as root, that the home directory has
509 # permissions/privs set for the account we create
510 if ($< == 0) {
511 unless (chmod(0755, $home_dir, $table_dir)) {
512 die("Can't set perms on $home_dir to 0755: $!");
513 }
514
515 unless (chown($uid, $gid, $home_dir, $table_dir)) {
516 die("Can't set owner of $home_dir to $uid/$gid: $!");
517 }
518 }
519
520 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
521 '/bin/bash');
522 auth_group_write($auth_group_file, $group, $gid, $user);
523
524 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
525 my $snmp_community = "public";
526
527 my $config = {
528 TraceLog => $log_file,
529 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
530 PidFile => $pid_file,
531 ScoreboardFile => $scoreboard_file,
532 SystemLog => $log_file,
533
534 AuthUserFile => $auth_user_file,
535 AuthGroupFile => $auth_group_file,
536 AuthOrder => 'mod_auth_file.c',
537
538 IfModules => {
539 'mod_delay.c' => {
540 DelayEngine => 'off',
541 },
542
543 'mod_snmp.c' => {
544 SNMPAgent => "master 127.0.0.1:$agent_port",
545 SNMPCommunity => $snmp_community,
546 SNMPEngine => 'on',
547 SNMPLog => $log_file,
548 SNMPTables => $table_dir,
549 },
550 },
551 };
552
553 my ($port, $config_user, $config_group) = config_write($config_file, $config);
554
555 my $ex;
556
557 # First, start the server
558 server_start($config_file);
559
560 # ...then stop the server. This means mod_snmp will have created all
561 # the necessary directories, etc.
562 sleep(2);
563 server_stop($pid_file);
564
565 # Now start the server again. Time time, mod_snmp will double-check
566 # permissions et al on the already-existing mod_snmp directories that it
567 # created the first time.
568 sleep(2);
569 server_start($config_file);
570
571 # Stop server
572 sleep(2);
573 eval { server_stop($pid_file) };
574 if ($@) {
575 $ex = $@;
576 }
577
578 if ($ex) {
579 test_append_logfile($log_file, $ex);
580 unlink($log_file);
581
582 die($ex);
583 }
584
585 unlink($log_file);
586 }
587
588 sub snmp_v1_get_unknown {
589 my $self = shift;
590 my $tmpdir = $self->{tmpdir};
591
592 my $config_file = "$tmpdir/snmp.conf";
593 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
594 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
595
596 my $log_file = test_get_logfile();
597
598 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
599 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
600
601 my $user = 'proftpd';
602 my $passwd = 'test';
603 my $group = 'ftpd';
604 my $home_dir = File::Spec->rel2abs($tmpdir);
605 my $uid = 500;
606 my $gid = 500;
607
608 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
609
610 # Make sure that, if we're running as root, that the home directory has
611 # permissions/privs set for the account we create
612 if ($< == 0) {
613 unless (chmod(0755, $home_dir, $table_dir)) {
614 die("Can't set perms on $home_dir to 0755: $!");
615 }
616
617 unless (chown($uid, $gid, $home_dir, $table_dir)) {
618 die("Can't set owner of $home_dir to $uid/$gid: $!");
619 }
620 }
621
622 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
623 '/bin/bash');
624 auth_group_write($auth_group_file, $group, $gid, $user);
625
626 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
627 my $snmp_community = "public";
628
629 my $request_oid = '1.3.6.1.4.1.17852.1.0';
630
631 my $config = {
632 TraceLog => $log_file,
633 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
634 PidFile => $pid_file,
635 ScoreboardFile => $scoreboard_file,
636 SystemLog => $log_file,
637
638 AuthUserFile => $auth_user_file,
639 AuthGroupFile => $auth_group_file,
640 AuthOrder => 'mod_auth_file.c',
641
642 IfModules => {
643 'mod_delay.c' => {
644 DelayEngine => 'off',
645 },
646
647 'mod_snmp.c' => {
648 SNMPAgent => "master 127.0.0.1:$agent_port",
649 SNMPCommunity => $snmp_community,
650 SNMPEngine => 'on',
651 SNMPLog => $log_file,
652 SNMPTables => $table_dir,
653 },
654 },
655 };
656
657 my ($port, $config_user, $config_group) = config_write($config_file, $config);
658
659 # Open pipes, for use between the parent and child processes. Specifically,
660 # the child will indicate when it's done with its test by writing a message
661 # to the parent.
662 my ($rfh, $wfh);
663 unless (pipe($rfh, $wfh)) {
664 die("Can't open pipe: $!");
665 }
666
667 require Net::SNMP;
668
669 my $ex;
670
671 # Fork child
672 $self->handle_sigchld();
673 defined(my $pid = fork()) or die("Can't fork: $!");
674 if ($pid) {
675 eval {
676 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
677 -hostname => '127.0.0.1',
678 -port => $agent_port,
679 -version => 'snmpv1',
680 -community => $snmp_community,
681 -retries => 1,
682 -timeout => 3,
683 -translate => 1,
684 );
685 unless ($snmp_sess) {
686 die("Unable to create Net::SNMP session: $snmp_err");
687 }
688
689 if ($ENV{TEST_VERBOSE}) {
690 # From the Net::SNMP debug perldocs
691 my $debug_mask = (0x02|0x10|0x20);
692 $snmp_sess->debug($debug_mask);
693 }
694
695 my $oids = [$request_oid];
696
697 my $snmp_resp = $snmp_sess->get_request(
698 -varbindList => $oids,
699 );
700 if ($snmp_resp) {
701 die("SNMP response received unexpectedly");
702 }
703
704 my $err = $snmp_sess->error();
705 my $expected = 'Received noSuchName(2) error-status at error-index 1';
706
707 $self->assert($expected eq $err,
708 test_msg("Expected error '$expected' for OID, got '$err'"));
709
710 $snmp_sess->close();
711 $snmp_sess = undef;
712 };
713
714 if ($@) {
715 $ex = $@;
716 }
717
718 $wfh->print("done\n");
719 $wfh->flush();
720
721 } else {
722 eval { server_wait($config_file, $rfh) };
723 if ($@) {
724 warn($@);
725 exit 1;
726 }
727
728 exit 0;
729 }
730
731 # Stop server
732 server_stop($pid_file);
733
734 $self->assert_child_ok($pid);
735
736 if ($ex) {
737 test_append_logfile($log_file, $ex);
738 unlink($log_file);
739
740 die($ex);
741 }
742
743 unlink($log_file);
744 }
745
746 sub snmp_v1_get_wrong_community {
747 my $self = shift;
748 my $tmpdir = $self->{tmpdir};
749
750 my $config_file = "$tmpdir/snmp.conf";
751 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
752 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
753
754 my $log_file = test_get_logfile();
755
756 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
757 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
758
759 my $user = 'proftpd';
760 my $passwd = 'test';
761 my $group = 'ftpd';
762 my $home_dir = File::Spec->rel2abs($tmpdir);
763 my $uid = 500;
764 my $gid = 500;
765
766 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
767
768 # Make sure that, if we're running as root, that the home directory has
769 # permissions/privs set for the account we create
770 if ($< == 0) {
771 unless (chmod(0755, $home_dir, $table_dir)) {
772 die("Can't set perms on $home_dir to 0755: $!");
773 }
774
775 unless (chown($uid, $gid, $home_dir, $table_dir)) {
776 die("Can't set owner of $home_dir to $uid/$gid: $!");
777 }
778 }
779
780 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
781 '/bin/bash');
782 auth_group_write($auth_group_file, $group, $gid, $user);
783
784 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
785 my $snmp_community = "public";
786
787 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.1.0';
788
789 my $config = {
790 TraceLog => $log_file,
791 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
792 PidFile => $pid_file,
793 ScoreboardFile => $scoreboard_file,
794 SystemLog => $log_file,
795
796 AuthUserFile => $auth_user_file,
797 AuthGroupFile => $auth_group_file,
798 AuthOrder => 'mod_auth_file.c',
799
800 IfModules => {
801 'mod_delay.c' => {
802 DelayEngine => 'off',
803 },
804
805 'mod_snmp.c' => {
806 SNMPAgent => "master 127.0.0.1:$agent_port",
807 SNMPCommunity => $snmp_community,
808 SNMPEngine => 'on',
809 SNMPLog => $log_file,
810 SNMPTables => $table_dir,
811 },
812 },
813 };
814
815 my ($port, $config_user, $config_group) = config_write($config_file, $config);
816
817 # Open pipes, for use between the parent and child processes. Specifically,
818 # the child will indicate when it's done with its test by writing a message
819 # to the parent.
820 my ($rfh, $wfh);
821 unless (pipe($rfh, $wfh)) {
822 die("Can't open pipe: $!");
823 }
824
825 require Net::SNMP;
826
827 my $ex;
828
829 # Fork child
830 $self->handle_sigchld();
831 defined(my $pid = fork()) or die("Can't fork: $!");
832 if ($pid) {
833 eval {
834 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
835 -hostname => '127.0.0.1',
836 -port => $agent_port,
837 -version => 'snmpv1',
838
839 # Deliberately configure the wrong community name
840 -community => 'foo',
841
842 -retries => 1,
843 -timeout => 3,
844 -translate => 1,
845 );
846 unless ($snmp_sess) {
847 die("Unable to create Net::SNMP session: $snmp_err");
848 }
849
850 if ($ENV{TEST_VERBOSE}) {
851 # From the Net::SNMP debug perldocs
852 my $debug_mask = (0x02|0x10|0x20);
853 $snmp_sess->debug($debug_mask);
854 }
855
856 my $oids = [$request_oid];
857
858 my $snmp_resp = $snmp_sess->get_request(
859 -varbindList => $oids,
860 );
861 if ($snmp_resp) {
862 die("SNMP response received unexpectedly");
863 }
864
865 # We don't expect to receive a response here; the community info is
866 # wrong. The only indication *might* be a trap that's generated, and
867 # sent somewhere else.
868
869 my $err = $snmp_sess->error();
870 my $expected = 'No response from remote host "127.0.0.1"';
871
872 $self->assert($expected eq $err,
873 test_msg("Expected error '$expected' for OID, got '$err'"));
874
875 $snmp_sess->close();
876 $snmp_sess = undef;
877 };
878
879 if ($@) {
880 $ex = $@;
881 }
882
883 $wfh->print("done\n");
884 $wfh->flush();
885
886 } else {
887 eval { server_wait($config_file, $rfh) };
888 if ($@) {
889 warn($@);
890 exit 1;
891 }
892
893 exit 0;
894 }
895
896 # Stop server
897 server_stop($pid_file);
898
899 $self->assert_child_ok($pid);
900
901 if ($ex) {
902 test_append_logfile($log_file, $ex);
903 unlink($log_file);
904
905 die($ex);
906 }
907
908 unlink($log_file);
909 }
910
911 sub snmp_v1_get_daemon_software {
912 my $self = shift;
913 my $tmpdir = $self->{tmpdir};
914
915 my $config_file = "$tmpdir/snmp.conf";
916 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
917 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
918
919 my $log_file = test_get_logfile();
920
921 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
922 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
923
924 my $user = 'proftpd';
925 my $passwd = 'test';
926 my $group = 'ftpd';
927 my $home_dir = File::Spec->rel2abs($tmpdir);
928 my $uid = 500;
929 my $gid = 500;
930
931 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
932
933 # Make sure that, if we're running as root, that the home directory has
934 # permissions/privs set for the account we create
935 if ($< == 0) {
936 unless (chmod(0755, $home_dir, $table_dir)) {
937 die("Can't set perms on $home_dir to 0755: $!");
938 }
939
940 unless (chown($uid, $gid, $home_dir, $table_dir)) {
941 die("Can't set owner of $home_dir to $uid/$gid: $!");
942 }
943 }
944
945 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
946 '/bin/bash');
947 auth_group_write($auth_group_file, $group, $gid, $user);
948
949 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
950 my $snmp_community = "public";
951
952 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.1.0';
953
954 my $config = {
955 TraceLog => $log_file,
956 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
957 PidFile => $pid_file,
958 ScoreboardFile => $scoreboard_file,
959 SystemLog => $log_file,
960
961 AuthUserFile => $auth_user_file,
962 AuthGroupFile => $auth_group_file,
963 AuthOrder => 'mod_auth_file.c',
964
965 IfModules => {
966 'mod_delay.c' => {
967 DelayEngine => 'off',
968 },
969
970 'mod_snmp.c' => {
971 SNMPAgent => "master 127.0.0.1:$agent_port",
972 SNMPCommunity => $snmp_community,
973 SNMPEngine => 'on',
974 SNMPLog => $log_file,
975 SNMPTables => $table_dir,
976 },
977 },
978 };
979
980 my ($port, $config_user, $config_group) = config_write($config_file, $config);
981
982 # Open pipes, for use between the parent and child processes. Specifically,
983 # the child will indicate when it's done with its test by writing a message
984 # to the parent.
985 my ($rfh, $wfh);
986 unless (pipe($rfh, $wfh)) {
987 die("Can't open pipe: $!");
988 }
989
990 require Net::SNMP;
991
992 my $ex;
993
994 # Fork child
995 $self->handle_sigchld();
996 defined(my $pid = fork()) or die("Can't fork: $!");
997 if ($pid) {
998 eval {
999 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
1000 -hostname => '127.0.0.1',
1001 -port => $agent_port,
1002 -version => 'snmpv1',
1003 -community => $snmp_community,
1004 -retries => 1,
1005 -timeout => 3,
1006 -translate => 1,
1007 );
1008 unless ($snmp_sess) {
1009 die("Unable to create Net::SNMP session: $snmp_err");
1010 }
1011
1012 if ($ENV{TEST_VERBOSE}) {
1013 # From the Net::SNMP debug perldocs
1014 my $debug_mask = (0x02|0x10|0x20);
1015 $snmp_sess->debug($debug_mask);
1016 }
1017
1018 my $oids = [$request_oid];
1019
1020 my $snmp_resp = $snmp_sess->get_request(
1021 -varbindList => $oids,
1022 );
1023 unless ($snmp_resp) {
1024 die("No SNMP response received: " . $snmp_sess->error());
1025 }
1026
1027 # Do we have the requested OID in the response?
1028 unless (defined($snmp_resp->{$request_oid})) {
1029 die("Missing required OID $request_oid in response");
1030 }
1031
1032 my $value = $snmp_resp->{$request_oid};
1033
1034 if ($ENV{TEST_VERBOSE}) {
1035 print STDERR "Requested OID $request_oid = $value\n";
1036 }
1037
1038 my $expected = 'proftpd';
1039
1040 $self->assert($expected eq $value,
1041 test_msg("Expected value '$expected' for OID, got '$value'"));
1042
1043 $snmp_sess->close();
1044 $snmp_sess = undef;
1045 };
1046
1047 if ($@) {
1048 $ex = $@;
1049 }
1050
1051 $wfh->print("done\n");
1052 $wfh->flush();
1053
1054 } else {
1055 eval { server_wait($config_file, $rfh) };
1056 if ($@) {
1057 warn($@);
1058 exit 1;
1059 }
1060
1061 exit 0;
1062 }
1063
1064 # Stop server
1065 server_stop($pid_file);
1066
1067 $self->assert_child_ok($pid);
1068
1069 if ($ex) {
1070 test_append_logfile($log_file, $ex);
1071 unlink($log_file);
1072
1073 die($ex);
1074 }
1075
1076 unlink($log_file);
1077 }
1078
1079 sub snmp_v1_get_daemon_version {
1080 my $self = shift;
1081 my $tmpdir = $self->{tmpdir};
1082
1083 my $config_file = "$tmpdir/snmp.conf";
1084 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
1085 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
1086
1087 my $log_file = test_get_logfile();
1088
1089 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
1090 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
1091
1092 my $user = 'proftpd';
1093 my $passwd = 'test';
1094 my $group = 'ftpd';
1095 my $home_dir = File::Spec->rel2abs($tmpdir);
1096 my $uid = 500;
1097 my $gid = 500;
1098
1099 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
1100
1101 # Make sure that, if we're running as root, that the home directory has
1102 # permissions/privs set for the account we create
1103 if ($< == 0) {
1104 unless (chmod(0755, $home_dir, $table_dir)) {
1105 die("Can't set perms on $home_dir to 0755: $!");
1106 }
1107
1108 unless (chown($uid, $gid, $home_dir, $table_dir)) {
1109 die("Can't set owner of $home_dir to $uid/$gid: $!");
1110 }
1111 }
1112
1113 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1114 '/bin/bash');
1115 auth_group_write($auth_group_file, $group, $gid, $user);
1116
1117 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
1118 my $snmp_community = "public";
1119
1120 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.2.0';
1121
1122 my $config = {
1123 TraceLog => $log_file,
1124 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
1125 PidFile => $pid_file,
1126 ScoreboardFile => $scoreboard_file,
1127 SystemLog => $log_file,
1128
1129 AuthUserFile => $auth_user_file,
1130 AuthGroupFile => $auth_group_file,
1131 AuthOrder => 'mod_auth_file.c',
1132
1133 IfModules => {
1134 'mod_delay.c' => {
1135 DelayEngine => 'off',
1136 },
1137
1138 'mod_snmp.c' => {
1139 SNMPAgent => "master 127.0.0.1:$agent_port",
1140 SNMPCommunity => $snmp_community,
1141 SNMPEngine => 'on',
1142 SNMPLog => $log_file,
1143 SNMPTables => $table_dir,
1144 },
1145 },
1146 };
1147
1148 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1149
1150 # Open pipes, for use between the parent and child processes. Specifically,
1151 # the child will indicate when it's done with its test by writing a message
1152 # to the parent.
1153 my ($rfh, $wfh);
1154 unless (pipe($rfh, $wfh)) {
1155 die("Can't open pipe: $!");
1156 }
1157
1158 require Net::SNMP;
1159
1160 my $ex;
1161
1162 # Fork child
1163 $self->handle_sigchld();
1164 defined(my $pid = fork()) or die("Can't fork: $!");
1165 if ($pid) {
1166 eval {
1167 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
1168 -hostname => '127.0.0.1',
1169 -port => $agent_port,
1170 -version => 'snmpv1',
1171 -community => $snmp_community,
1172 -retries => 1,
1173 -timeout => 3,
1174 -translate => 1,
1175 );
1176 unless ($snmp_sess) {
1177 die("Unable to create Net::SNMP session: $snmp_err");
1178 }
1179
1180 if ($ENV{TEST_VERBOSE}) {
1181 # From the Net::SNMP debug perldocs
1182 my $debug_mask = (0x02|0x10|0x20);
1183 $snmp_sess->debug($debug_mask);
1184 }
1185
1186 my $oids = [$request_oid];
1187
1188 my $snmp_resp = $snmp_sess->get_request(
1189 -varbindList => $oids,
1190 );
1191 unless ($snmp_resp) {
1192 die("No SNMP response received: " . $snmp_sess->error());
1193 }
1194
1195 # Do we have the requested OID in the response?
1196 unless (defined($snmp_resp->{$request_oid})) {
1197 die("Missing required OID $request_oid in response");
1198 }
1199
1200 my $value = $snmp_resp->{$request_oid};
1201
1202 if ($ENV{TEST_VERBOSE}) {
1203 print STDERR "Requested OID $request_oid = $value\n";
1204 }
1205
1206 my $expected = '^ProFTPD Version';
1207
1208 $self->assert(qr/$expected/, $value,
1209 test_msg("Expected value '$expected' for OID, got '$value'"));
1210
1211 $snmp_sess->close();
1212 $snmp_sess = undef;
1213 };
1214
1215 if ($@) {
1216 $ex = $@;
1217 }
1218
1219 $wfh->print("done\n");
1220 $wfh->flush();
1221
1222 } else {
1223 eval { server_wait($config_file, $rfh) };
1224 if ($@) {
1225 warn($@);
1226 exit 1;
1227 }
1228
1229 exit 0;
1230 }
1231
1232 # Stop server
1233 server_stop($pid_file);
1234
1235 $self->assert_child_ok($pid);
1236
1237 if ($ex) {
1238 test_append_logfile($log_file, $ex);
1239 unlink($log_file);
1240
1241 die($ex);
1242 }
1243
1244 unlink($log_file);
1245 }
1246
1247 sub snmp_v1_get_daemon_admin {
1248 my $self = shift;
1249 my $tmpdir = $self->{tmpdir};
1250
1251 my $config_file = "$tmpdir/snmp.conf";
1252 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
1253 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
1254
1255 my $log_file = test_get_logfile();
1256
1257 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
1258 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
1259
1260 my $user = 'proftpd';
1261 my $passwd = 'test';
1262 my $group = 'ftpd';
1263 my $home_dir = File::Spec->rel2abs($tmpdir);
1264 my $uid = 500;
1265 my $gid = 500;
1266
1267 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
1268
1269 # Make sure that, if we're running as root, that the home directory has
1270 # permissions/privs set for the account we create
1271 if ($< == 0) {
1272 unless (chmod(0755, $home_dir, $table_dir)) {
1273 die("Can't set perms on $home_dir to 0755: $!");
1274 }
1275
1276 unless (chown($uid, $gid, $home_dir, $table_dir)) {
1277 die("Can't set owner of $home_dir to $uid/$gid: $!");
1278 }
1279 }
1280
1281 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1282 '/bin/bash');
1283 auth_group_write($auth_group_file, $group, $gid, $user);
1284
1285 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
1286 my $snmp_community = "public";
1287
1288 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.3.0';
1289
1290 my $config = {
1291 TraceLog => $log_file,
1292 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
1293 PidFile => $pid_file,
1294 ScoreboardFile => $scoreboard_file,
1295 SystemLog => $log_file,
1296
1297 AuthUserFile => $auth_user_file,
1298 AuthGroupFile => $auth_group_file,
1299 AuthOrder => 'mod_auth_file.c',
1300
1301 IfModules => {
1302 'mod_delay.c' => {
1303 DelayEngine => 'off',
1304 },
1305
1306 'mod_snmp.c' => {
1307 SNMPAgent => "master 127.0.0.1:$agent_port",
1308 SNMPCommunity => $snmp_community,
1309 SNMPEngine => 'on',
1310 SNMPLog => $log_file,
1311 SNMPTables => $table_dir,
1312 },
1313 },
1314 };
1315
1316 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1317
1318 # Open pipes, for use between the parent and child processes. Specifically,
1319 # the child will indicate when it's done with its test by writing a message
1320 # to the parent.
1321 my ($rfh, $wfh);
1322 unless (pipe($rfh, $wfh)) {
1323 die("Can't open pipe: $!");
1324 }
1325
1326 require Net::SNMP;
1327
1328 my $ex;
1329
1330 # Fork child
1331 $self->handle_sigchld();
1332 defined(my $pid = fork()) or die("Can't fork: $!");
1333 if ($pid) {
1334 eval {
1335 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
1336 -hostname => '127.0.0.1',
1337 -port => $agent_port,
1338 -version => 'snmpv1',
1339 -community => $snmp_community,
1340 -retries => 1,
1341 -timeout => 3,
1342 -translate => 1,
1343 );
1344 unless ($snmp_sess) {
1345 die("Unable to create Net::SNMP session: $snmp_err");
1346 }
1347
1348 if ($ENV{TEST_VERBOSE}) {
1349 # From the Net::SNMP debug perldocs
1350 my $debug_mask = (0x02|0x10|0x20);
1351 $snmp_sess->debug($debug_mask);
1352 }
1353
1354 my $oids = [$request_oid];
1355
1356 my $snmp_resp = $snmp_sess->get_request(
1357 -varbindList => $oids,
1358 );
1359 unless ($snmp_resp) {
1360 die("No SNMP response received: " . $snmp_sess->error());
1361 }
1362
1363 # Do we have the requested OID in the response?
1364 unless (defined($snmp_resp->{$request_oid})) {
1365 die("Missing required OID $request_oid in response");
1366 }
1367
1368 my $value = $snmp_resp->{$request_oid};
1369
1370 if ($ENV{TEST_VERBOSE}) {
1371 print STDERR "Requested OID $request_oid = $value\n";
1372 }
1373
1374 # The default proftpd string, without ServerAdmin configured.
1375 my $expected = '[email protected]';
1376
1377 $self->assert(qr/$expected/, $value,
1378 test_msg("Expected value '$expected' for OID, got '$value'"));
1379
1380 $snmp_sess->close();
1381 $snmp_sess = undef;
1382 };
1383
1384 if ($@) {
1385 $ex = $@;
1386 }
1387
1388 $wfh->print("done\n");
1389 $wfh->flush();
1390
1391 } else {
1392 eval { server_wait($config_file, $rfh) };
1393 if ($@) {
1394 warn($@);
1395 exit 1;
1396 }
1397
1398 exit 0;
1399 }
1400
1401 # Stop server
1402 server_stop($pid_file);
1403
1404 $self->assert_child_ok($pid);
1405
1406 if ($ex) {
1407 test_append_logfile($log_file, $ex);
1408 unlink($log_file);
1409
1410 die($ex);
1411 }
1412
1413 unlink($log_file);
1414 }
1415
1416 sub snmp_v1_get_daemon_admin_with_config {
1417 my $self = shift;
1418 my $tmpdir = $self->{tmpdir};
1419
1420 my $config_file = "$tmpdir/snmp.conf";
1421 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
1422 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
1423
1424 my $log_file = test_get_logfile();
1425
1426 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
1427 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
1428
1429 my $user = 'proftpd';
1430 my $passwd = 'test';
1431 my $group = 'ftpd';
1432 my $home_dir = File::Spec->rel2abs($tmpdir);
1433 my $uid = 500;
1434 my $gid = 500;
1435
1436 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
1437
1438 # Make sure that, if we're running as root, that the home directory has
1439 # permissions/privs set for the account we create
1440 if ($< == 0) {
1441 unless (chmod(0755, $home_dir, $table_dir)) {
1442 die("Can't set perms on $home_dir to 0755: $!");
1443 }
1444
1445 unless (chown($uid, $gid, $home_dir, $table_dir)) {
1446 die("Can't set owner of $home_dir to $uid/$gid: $!");
1447 }
1448 }
1449
1450 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1451 '/bin/bash');
1452 auth_group_write($auth_group_file, $group, $gid, $user);
1453
1454 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
1455 my $snmp_community = "public";
1456
1457 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.3.0';
1458 my $server_admin = '[email protected]';
1459
1460 my $config = {
1461 TraceLog => $log_file,
1462 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
1463 PidFile => $pid_file,
1464 ScoreboardFile => $scoreboard_file,
1465 SystemLog => $log_file,
1466
1467 AuthUserFile => $auth_user_file,
1468 AuthGroupFile => $auth_group_file,
1469 AuthOrder => 'mod_auth_file.c',
1470
1471 ServerAdmin => $server_admin,
1472
1473 IfModules => {
1474 'mod_delay.c' => {
1475 DelayEngine => 'off',
1476 },
1477
1478 'mod_snmp.c' => {
1479 SNMPAgent => "master 127.0.0.1:$agent_port",
1480 SNMPCommunity => $snmp_community,
1481 SNMPEngine => 'on',
1482 SNMPLog => $log_file,
1483 SNMPTables => $table_dir,
1484 },
1485 },
1486 };
1487
1488 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1489
1490 # Open pipes, for use between the parent and child processes. Specifically,
1491 # the child will indicate when it's done with its test by writing a message
1492 # to the parent.
1493 my ($rfh, $wfh);
1494 unless (pipe($rfh, $wfh)) {
1495 die("Can't open pipe: $!");
1496 }
1497
1498 require Net::SNMP;
1499
1500 my $ex;
1501
1502 # Fork child
1503 $self->handle_sigchld();
1504 defined(my $pid = fork()) or die("Can't fork: $!");
1505 if ($pid) {
1506 eval {
1507 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
1508 -hostname => '127.0.0.1',
1509 -port => $agent_port,
1510 -version => 'snmpv1',
1511 -community => $snmp_community,
1512 -retries => 1,
1513 -timeout => 3,
1514 -translate => 1,
1515 );
1516 unless ($snmp_sess) {
1517 die("Unable to create Net::SNMP session: $snmp_err");
1518 }
1519
1520 if ($ENV{TEST_VERBOSE}) {
1521 # From the Net::SNMP debug perldocs
1522 my $debug_mask = (0x02|0x10|0x20);
1523 $snmp_sess->debug($debug_mask);
1524 }
1525
1526 my $oids = [$request_oid];
1527
1528 my $snmp_resp = $snmp_sess->get_request(
1529 -varbindList => $oids,
1530 );
1531 unless ($snmp_resp) {
1532 die("No SNMP response received: " . $snmp_sess->error());
1533 }
1534
1535 # Do we have the requested OID in the response?
1536 unless (defined($snmp_resp->{$request_oid})) {
1537 die("Missing required OID $request_oid in response");
1538 }
1539
1540 my $value = $snmp_resp->{$request_oid};
1541
1542 if ($ENV{TEST_VERBOSE}) {
1543 print STDERR "Requested OID $request_oid = $value\n";
1544 }
1545
1546 my $expected = $server_admin;
1547
1548 $self->assert(qr/$expected/, $value,
1549 test_msg("Expected value '$expected' for OID, got '$value'"));
1550
1551 $snmp_sess->close();
1552 $snmp_sess = undef;
1553 };
1554
1555 if ($@) {
1556 $ex = $@;
1557 }
1558
1559 $wfh->print("done\n");
1560 $wfh->flush();
1561
1562 } else {
1563 eval { server_wait($config_file, $rfh) };
1564 if ($@) {
1565 warn($@);
1566 exit 1;
1567 }
1568
1569 exit 0;
1570 }
1571
1572 # Stop server
1573 server_stop($pid_file);
1574
1575 $self->assert_child_ok($pid);
1576
1577 if ($ex) {
1578 test_append_logfile($log_file, $ex);
1579 unlink($log_file);
1580
1581 die($ex);
1582 }
1583
1584 unlink($log_file);
1585 }
1586
1587 sub snmp_v1_get_daemon_uptime {
1588 my $self = shift;
1589 my $tmpdir = $self->{tmpdir};
1590
1591 my $config_file = "$tmpdir/snmp.conf";
1592 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
1593 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
1594
1595 my $log_file = test_get_logfile();
1596
1597 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
1598 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
1599
1600 my $user = 'proftpd';
1601 my $passwd = 'test';
1602 my $group = 'ftpd';
1603 my $home_dir = File::Spec->rel2abs($tmpdir);
1604 my $uid = 500;
1605 my $gid = 500;
1606
1607 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
1608
1609 # Make sure that, if we're running as root, that the home directory has
1610 # permissions/privs set for the account we create
1611 if ($< == 0) {
1612 unless (chmod(0755, $home_dir, $table_dir)) {
1613 die("Can't set perms on $home_dir to 0755: $!");
1614 }
1615
1616 unless (chown($uid, $gid, $home_dir, $table_dir)) {
1617 die("Can't set owner of $home_dir to $uid/$gid: $!");
1618 }
1619 }
1620
1621 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1622 '/bin/bash');
1623 auth_group_write($auth_group_file, $group, $gid, $user);
1624
1625 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
1626 my $snmp_community = "public";
1627
1628 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.4.0';
1629
1630 my $config = {
1631 TraceLog => $log_file,
1632 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
1633 PidFile => $pid_file,
1634 ScoreboardFile => $scoreboard_file,
1635 SystemLog => $log_file,
1636
1637 AuthUserFile => $auth_user_file,
1638 AuthGroupFile => $auth_group_file,
1639 AuthOrder => 'mod_auth_file.c',
1640
1641 IfModules => {
1642 'mod_delay.c' => {
1643 DelayEngine => 'off',
1644 },
1645
1646 'mod_snmp.c' => {
1647 SNMPAgent => "master 127.0.0.1:$agent_port",
1648 SNMPCommunity => $snmp_community,
1649 SNMPEngine => 'on',
1650 SNMPLog => $log_file,
1651 SNMPTables => $table_dir,
1652 },
1653 },
1654 };
1655
1656 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1657
1658 # Open pipes, for use between the parent and child processes. Specifically,
1659 # the child will indicate when it's done with its test by writing a message
1660 # to the parent.
1661 my ($rfh, $wfh);
1662 unless (pipe($rfh, $wfh)) {
1663 die("Can't open pipe: $!");
1664 }
1665
1666 require Net::SNMP;
1667
1668 my $ex;
1669
1670 # Fork child
1671 $self->handle_sigchld();
1672 defined(my $pid = fork()) or die("Can't fork: $!");
1673 if ($pid) {
1674 eval {
1675 # Wait a couple of secs, so that the uptime has a non-zero value
1676 sleep(2);
1677
1678 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
1679 -hostname => '127.0.0.1',
1680 -port => $agent_port,
1681 -version => 'snmpv1',
1682 -community => $snmp_community,
1683 -retries => 1,
1684 -timeout => 3,
1685 -translate => 1,
1686 );
1687 unless ($snmp_sess) {
1688 die("Unable to create Net::SNMP session: $snmp_err");
1689 }
1690
1691 if ($ENV{TEST_VERBOSE}) {
1692 # From the Net::SNMP debug perldocs
1693 my $debug_mask = (0x02|0x10|0x20);
1694 $snmp_sess->debug($debug_mask);
1695 }
1696
1697 my $oids = [$request_oid];
1698
1699 my $snmp_resp = $snmp_sess->get_request(
1700 -varbindList => $oids,
1701 );
1702 unless ($snmp_resp) {
1703 die("No SNMP response received: " . $snmp_sess->error());
1704 }
1705
1706 # Do we have the requested OID in the response?
1707 unless (defined($snmp_resp->{$request_oid})) {
1708 die("Missing required OID $request_oid in response");
1709 }
1710
1711 my $value = $snmp_resp->{$request_oid};
1712
1713 if ($ENV{TEST_VERBOSE}) {
1714 print STDERR "Requested OID $request_oid = $value\n";
1715 }
1716
1717 my $expected = '(^\d+ days, )?.*?\d+\.\d+';
1718
1719 $self->assert(qr/$expected/, $value,
1720 test_msg("Expected value '$expected' for OID, got '$value'"));
1721
1722 $snmp_sess->close();
1723 $snmp_sess = undef;
1724 };
1725
1726 if ($@) {
1727 $ex = $@;
1728 }
1729
1730 $wfh->print("done\n");
1731 $wfh->flush();
1732
1733 } else {
1734 eval { server_wait($config_file, $rfh) };
1735 if ($@) {
1736 warn($@);
1737 exit 1;
1738 }
1739
1740 exit 0;
1741 }
1742
1743 # Stop server
1744 server_stop($pid_file);
1745
1746 $self->assert_child_ok($pid);
1747
1748 if ($ex) {
1749 test_append_logfile($log_file, $ex);
1750 unlink($log_file);
1751
1752 die($ex);
1753 }
1754
1755 unlink($log_file);
1756 }
1757
1758 sub snmp_v1_get_daemon_vhost_count {
1759 my $self = shift;
1760 my $tmpdir = $self->{tmpdir};
1761
1762 my $config_file = "$tmpdir/snmp.conf";
1763 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
1764 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
1765
1766 my $log_file = test_get_logfile();
1767
1768 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
1769 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
1770
1771 my $user = 'proftpd';
1772 my $passwd = 'test';
1773 my $group = 'ftpd';
1774 my $home_dir = File::Spec->rel2abs($tmpdir);
1775 my $uid = 500;
1776 my $gid = 500;
1777
1778 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
1779
1780 # Make sure that, if we're running as root, that the home directory has
1781 # permissions/privs set for the account we create
1782 if ($< == 0) {
1783 unless (chmod(0755, $home_dir, $table_dir)) {
1784 die("Can't set perms on $home_dir to 0755: $!");
1785 }
1786
1787 unless (chown($uid, $gid, $home_dir, $table_dir)) {
1788 die("Can't set owner of $home_dir to $uid/$gid: $!");
1789 }
1790 }
1791
1792 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1793 '/bin/bash');
1794 auth_group_write($auth_group_file, $group, $gid, $user);
1795
1796 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
1797 my $snmp_community = "public";
1798
1799 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.5.0';
1800
1801 my $config = {
1802 TraceLog => $log_file,
1803 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
1804 PidFile => $pid_file,
1805 ScoreboardFile => $scoreboard_file,
1806 SystemLog => $log_file,
1807
1808 AuthUserFile => $auth_user_file,
1809 AuthGroupFile => $auth_group_file,
1810 AuthOrder => 'mod_auth_file.c',
1811
1812 IfModules => {
1813 'mod_delay.c' => {
1814 DelayEngine => 'off',
1815 },
1816
1817 'mod_snmp.c' => {
1818 SNMPAgent => "master 127.0.0.1:$agent_port",
1819 SNMPCommunity => $snmp_community,
1820 SNMPEngine => 'on',
1821 SNMPLog => $log_file,
1822 SNMPTables => $table_dir,
1823 },
1824 },
1825 };
1826
1827 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1828
1829 # Open pipes, for use between the parent and child processes. Specifically,
1830 # the child will indicate when it's done with its test by writing a message
1831 # to the parent.
1832 my ($rfh, $wfh);
1833 unless (pipe($rfh, $wfh)) {
1834 die("Can't open pipe: $!");
1835 }
1836
1837 require Net::SNMP;
1838
1839 my $ex;
1840
1841 # Fork child
1842 $self->handle_sigchld();
1843 defined(my $pid = fork()) or die("Can't fork: $!");
1844 if ($pid) {
1845 eval {
1846 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
1847 -hostname => '127.0.0.1',
1848 -port => $agent_port,
1849 -version => 'snmpv1',
1850 -community => $snmp_community,
1851 -retries => 1,
1852 -timeout => 3,
1853 -translate => 1,
1854 );
1855 unless ($snmp_sess) {
1856 die("Unable to create Net::SNMP session: $snmp_err");
1857 }
1858
1859 if ($ENV{TEST_VERBOSE}) {
1860 # From the Net::SNMP debug perldocs
1861 my $debug_mask = (0x02|0x10|0x20);
1862 $snmp_sess->debug($debug_mask);
1863 }
1864
1865 my $oids = [$request_oid];
1866
1867 my $snmp_resp = $snmp_sess->get_request(
1868 -varbindList => $oids,
1869 );
1870 unless ($snmp_resp) {
1871 die("No SNMP response received: " . $snmp_sess->error());
1872 }
1873
1874 # Do we have the requested OID in the response?
1875 unless (defined($snmp_resp->{$request_oid})) {
1876 die("Missing required OID $request_oid in response");
1877 }
1878
1879 my $value = $snmp_resp->{$request_oid};
1880
1881 if ($ENV{TEST_VERBOSE}) {
1882 print STDERR "Requested OID $request_oid = $value\n";
1883 }
1884
1885 my $expected = 1;
1886
1887 $self->assert($expected == $value,
1888 test_msg("Expected value $expected for OID, got $value"));
1889
1890 $snmp_sess->close();
1891 $snmp_sess = undef;
1892 };
1893
1894 if ($@) {
1895 $ex = $@;
1896 }
1897
1898 $wfh->print("done\n");
1899 $wfh->flush();
1900
1901 } else {
1902 eval { server_wait($config_file, $rfh) };
1903 if ($@) {
1904 warn($@);
1905 exit 1;
1906 }
1907
1908 exit 0;
1909 }
1910
1911 # Stop server
1912 server_stop($pid_file);
1913
1914 $self->assert_child_ok($pid);
1915
1916 if ($ex) {
1917 test_append_logfile($log_file, $ex);
1918 unlink($log_file);
1919
1920 die($ex);
1921 }
1922
1923 unlink($log_file);
1924 }
1925
1926 sub snmp_v1_get_daemon_conn_counts {
1927 my $self = shift;
1928 my $tmpdir = $self->{tmpdir};
1929
1930 my $config_file = "$tmpdir/snmp.conf";
1931 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
1932 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
1933
1934 my $log_file = test_get_logfile();
1935
1936 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
1937 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
1938
1939 my $user = 'proftpd';
1940 my $passwd = 'test';
1941 my $group = 'ftpd';
1942 my $home_dir = File::Spec->rel2abs($tmpdir);
1943 my $uid = 500;
1944 my $gid = 500;
1945
1946 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
1947
1948 # Make sure that, if we're running as root, that the home directory has
1949 # permissions/privs set for the account we create
1950 if ($< == 0) {
1951 unless (chmod(0755, $home_dir, $table_dir)) {
1952 die("Can't set perms on $home_dir to 0755: $!");
1953 }
1954
1955 unless (chown($uid, $gid, $home_dir, $table_dir)) {
1956 die("Can't set owner of $home_dir to $uid/$gid: $!");
1957 }
1958 }
1959
1960 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1961 '/bin/bash');
1962 auth_group_write($auth_group_file, $group, $gid, $user);
1963
1964 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
1965 my $snmp_community = "public";
1966
1967 my $delay_nsecs = 45;
1968 my $use_delay = 0;
1969
1970 my $config = {
1971 TraceLog => $log_file,
1972 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
1973 PidFile => $pid_file,
1974 ScoreboardFile => $scoreboard_file,
1975 SystemLog => $log_file,
1976
1977 AuthUserFile => $auth_user_file,
1978 AuthGroupFile => $auth_group_file,
1979 AuthOrder => 'mod_auth_file.c',
1980
1981 IfModules => {
1982 'mod_delay.c' => {
1983 DelayEngine => 'off',
1984 },
1985
1986 'mod_snmp.c' => {
1987 SNMPAgent => "master 127.0.0.1:$agent_port",
1988 SNMPCommunity => $snmp_community,
1989 SNMPEngine => 'on',
1990 SNMPLog => $log_file,
1991 SNMPTables => $table_dir,
1992 },
1993 },
1994 };
1995
1996 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1997
1998 # Open pipes, for use between the parent and child processes. Specifically,
1999 # the child will indicate when it's done with its test by writing a message
2000 # to the parent.
2001 my ($rfh, $wfh);
2002 unless (pipe($rfh, $wfh)) {
2003 die("Can't open pipe: $!");
2004 }
2005
2006 require Net::SNMP;
2007
2008 my $ex;
2009
2010 # Fork child
2011 $self->handle_sigchld();
2012 defined(my $pid = fork()) or die("Can't fork: $!");
2013 if ($pid) {
2014 eval {
2015 my $expected;
2016
2017 # First, get the conn count/total
2018 my ($conn_count, $conn_total) = get_conn_info($agent_port,
2019 $snmp_community);
2020
2021 $expected = 0;
2022 $self->assert($conn_count == $expected,
2023 test_msg("Expected connection count $expected, got $conn_count"));
2024
2025 $expected = 0;
2026 $self->assert($conn_total == $expected,
2027 test_msg("Expected connection total $expected, got $conn_total"));
2028
2029 # Now, connect to the server
2030 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2031
2032 # Then, get the connection counts again
2033 ($conn_count, $conn_total) = get_conn_info($agent_port, $snmp_community);
2034
2035 $expected = 1;
2036 $self->assert($conn_count == $expected,
2037 test_msg("Expected connection count $expected, got $conn_count"));
2038
2039 $self->assert($conn_total == $expected,
2040 test_msg("Expected connection total $expected, got $conn_total"));
2041
2042 # Disconnect from the server.
2043 $client->quit();
2044 $client = undef;
2045
2046 # Get the connection counts one more time, make sure they're what we
2047 # expect.
2048 ($conn_count, $conn_total) = get_conn_info($agent_port, $snmp_community);
2049
2050 $expected = 0;
2051 $self->assert($conn_count == $expected,
2052 test_msg("Expected connection count $expected, got $conn_count"));
2053
2054 $expected = 1;
2055 $self->assert($conn_total == $expected,
2056 test_msg("Expected connection total $expected, got $conn_total"));
2057
2058 # Now wait for 2 secs, then try again (make sure the counters aren't
2059 # reset somehow on the server's side).
2060 sleep(2);
2061
2062 ($conn_count, $conn_total) = get_conn_info($agent_port, $snmp_community);
2063
2064 $expected = 0;
2065 $self->assert($conn_count == $expected,
2066 test_msg("Expected connection count $expected, got $conn_count"));
2067
2068 $expected = 1;
2069 $self->assert($conn_total == $expected,
2070 test_msg("Expected connection total $expected, got $conn_total"));
2071
2072 # Now connect several more times
2073 my $nconnects = 10;
2074 for (my $i = 0; $i < $nconnects; $i++) {
2075 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2076 $client->quit();
2077 }
2078
2079 if ($use_delay) {
2080 # Wait for more than 45 secs; that's when the resets are
2081 # usually seen
2082 if ($ENV{TEST_VERBOSE}) {
2083 print STDERR "# Waiting for $delay_nsecs secs\n";
2084 }
2085 sleep($delay_nsecs);
2086 }
2087
2088 ($conn_count, $conn_total) = get_conn_info($agent_port, $snmp_community);
2089
2090 $expected = 0;
2091 $self->assert($conn_count == $expected,
2092 test_msg("Expected connection count $expected, got $conn_count"));
2093
2094 $expected = $nconnects + 1;
2095 $self->assert($conn_total == $expected,
2096 test_msg("Expected connection total $expected, got $conn_total"));
2097 };
2098
2099 if ($@) {
2100 $ex = $@;
2101 }
2102
2103 $wfh->print("done\n");
2104 $wfh->flush();
2105
2106 } else {
2107 eval { server_wait($config_file, $rfh, $delay_nsecs + 10) };
2108 if ($@) {
2109 warn($@);
2110 exit 1;
2111 }
2112
2113 exit 0;
2114 }
2115
2116 # Stop server
2117 server_stop($pid_file);
2118
2119 $self->assert_child_ok($pid);
2120
2121 if ($ex) {
2122 test_append_logfile($log_file, $ex);
2123 unlink($log_file);
2124
2125 die($ex);
2126 }
2127
2128 unlink($log_file);
2129 }
2130
2131 sub snmp_v1_get_daemon_restart_count {
2132 my $self = shift;
2133 my $tmpdir = $self->{tmpdir};
2134
2135 my $config_file = "$tmpdir/snmp.conf";
2136 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
2137 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
2138
2139 my $log_file = test_get_logfile();
2140
2141 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
2142 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
2143
2144 my $user = 'proftpd';
2145 my $passwd = 'test';
2146 my $group = 'ftpd';
2147 my $home_dir = File::Spec->rel2abs($tmpdir);
2148 my $uid = 500;
2149 my $gid = 500;
2150
2151 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
2152
2153 # Make sure that, if we're running as root, that the home directory has
2154 # permissions/privs set for the account we create
2155 if ($< == 0) {
2156 unless (chmod(0755, $home_dir, $table_dir)) {
2157 die("Can't set perms on $home_dir to 0755: $!");
2158 }
2159
2160 unless (chown($uid, $gid, $home_dir, $table_dir)) {
2161 die("Can't set owner of $home_dir to $uid/$gid: $!");
2162 }
2163 }
2164
2165 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2166 '/bin/bash');
2167 auth_group_write($auth_group_file, $group, $gid, $user);
2168
2169 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
2170 my $snmp_community = "public";
2171
2172 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.6.0';
2173
2174 my $config = {
2175 TraceLog => $log_file,
2176 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
2177 PidFile => $pid_file,
2178 ScoreboardFile => $scoreboard_file,
2179 SystemLog => $log_file,
2180
2181 AuthUserFile => $auth_user_file,
2182 AuthGroupFile => $auth_group_file,
2183 AuthOrder => 'mod_auth_file.c',
2184
2185 IfModules => {
2186 'mod_delay.c' => {
2187 DelayEngine => 'off',
2188 },
2189
2190 'mod_snmp.c' => {
2191 SNMPAgent => "master 127.0.0.1:$agent_port",
2192 SNMPCommunity => $snmp_community,
2193 SNMPEngine => 'on',
2194 SNMPLog => $log_file,
2195 SNMPTables => $table_dir,
2196 },
2197 },
2198 };
2199
2200 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2201
2202 # Open pipes, for use between the parent and child processes. Specifically,
2203 # the child will indicate when it's done with its test by writing a message
2204 # to the parent.
2205 my ($rfh, $wfh);
2206 unless (pipe($rfh, $wfh)) {
2207 die("Can't open pipe: $!");
2208 }
2209
2210 require Net::SNMP;
2211
2212 my $ex;
2213
2214 # Fork child
2215 $self->handle_sigchld();
2216 defined(my $pid = fork()) or die("Can't fork: $!");
2217 if ($pid) {
2218 eval {
2219 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
2220 -hostname => '127.0.0.1',
2221 -port => $agent_port,
2222 -version => 'snmpv1',
2223 -community => $snmp_community,
2224 -retries => 1,
2225 -timeout => 3,
2226 -translate => 1,
2227 );
2228 unless ($snmp_sess) {
2229 die("Unable to create Net::SNMP session: $snmp_err");
2230 }
2231
2232 if ($ENV{TEST_VERBOSE}) {
2233 # From the Net::SNMP debug perldocs
2234 my $debug_mask = (0x02|0x10|0x20);
2235 $snmp_sess->debug($debug_mask);
2236 }
2237
2238 my $oids = [$request_oid];
2239
2240 my $snmp_resp = $snmp_sess->get_request(
2241 -varbindList => $oids,
2242 );
2243 unless ($snmp_resp) {
2244 die("No SNMP response received: " . $snmp_sess->error());
2245 }
2246
2247 # Do we have the requested OID in the response?
2248 unless (defined($snmp_resp->{$request_oid})) {
2249 die("Missing required OID $request_oid in response");
2250 }
2251
2252 my $value = $snmp_resp->{$request_oid};
2253
2254 if ($ENV{TEST_VERBOSE}) {
2255 print STDERR "Requested OID $request_oid = $value\n";
2256 }
2257
2258 my $expected = 0;
2259
2260 $self->assert($expected == $value,
2261 test_msg("Expected value $expected for OID, got $value"));
2262
2263 $snmp_sess->close();
2264 $snmp_sess = undef;
2265 };
2266
2267 if ($@) {
2268 $ex = $@;
2269 }
2270
2271 $wfh->print("done\n");
2272 $wfh->flush();
2273
2274 } else {
2275 eval { server_wait($config_file, $rfh) };
2276 if ($@) {
2277 warn($@);
2278 exit 1;
2279 }
2280
2281 exit 0;
2282 }
2283
2284 # Stop server
2285 server_stop($pid_file);
2286
2287 $self->assert_child_ok($pid);
2288
2289 if ($ex) {
2290 test_append_logfile($log_file, $ex);
2291 unlink($log_file);
2292
2293 die($ex);
2294 }
2295
2296 unlink($log_file);
2297 }
2298
2299 sub snmp_v1_get_daemon_segfault_count {
2300 my $self = shift;
2301 my $tmpdir = $self->{tmpdir};
2302 my $setup = test_setup($tmpdir, 'snmp');
2303
2304 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
2305
2306 # Make sure that, if we're running as root, that the home directory has
2307 # permissions/privs set for the account we create
2308 if ($< == 0) {
2309 unless (chmod(0755, $table_dir)) {
2310 die("Can't set perms on $table_dir to 0755: $!");
2311 }
2312
2313 unless (chown($setup->{uid}, $setup->{gid}, $table_dir)) {
2314 die("Can't set owner of $table_dir to $setup->{uid}/$setup->{gid}: $!");
2315 }
2316 }
2317
2318 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
2319 my $snmp_community = "public";
2320
2321 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.10.0';
2322
2323 my $config = {
2324 PidFile => $setup->{pid_file},
2325 ScoreboardFile => $setup->{scoreboard_file},
2326 SystemLog => $setup->{log_file},
2327 TraceLog => $setup->{log_file},
2328 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
2329
2330 AuthUserFile => $setup->{auth_user_file},
2331 AuthGroupFile => $setup->{auth_group_file},
2332 AuthOrder => 'mod_auth_file.c',
2333
2334 IfModules => {
2335 'mod_delay.c' => {
2336 DelayEngine => 'off',
2337 },
2338
2339 'mod_snmp.c' => {
2340 SNMPAgent => "master 127.0.0.1:$agent_port",
2341 SNMPCommunity => $snmp_community,
2342 SNMPEngine => 'on',
2343 SNMPLog => $setup->{log_file},
2344 SNMPTables => $table_dir,
2345 },
2346 },
2347 };
2348
2349 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2350 $config);
2351
2352 # Open pipes, for use between the parent and child processes. Specifically,
2353 # the child will indicate when it's done with its test by writing a message
2354 # to the parent.
2355 my ($rfh, $wfh);
2356 unless (pipe($rfh, $wfh)) {
2357 die("Can't open pipe: $!");
2358 }
2359
2360 require Net::SNMP;
2361
2362 my $ex;
2363
2364 # Fork child
2365 $self->handle_sigchld();
2366 defined(my $pid = fork()) or die("Can't fork: $!");
2367 if ($pid) {
2368 eval {
2369 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
2370 -hostname => '127.0.0.1',
2371 -port => $agent_port,
2372 -version => 'snmpv1',
2373 -community => $snmp_community,
2374 -retries => 1,
2375 -timeout => 3,
2376 -translate => 1,
2377 );
2378 unless ($snmp_sess) {
2379 die("Unable to create Net::SNMP session: $snmp_err");
2380 }
2381
2382 if ($ENV{TEST_VERBOSE}) {
2383 # From the Net::SNMP debug perldocs
2384 my $debug_mask = (0x02|0x10|0x20);
2385 $snmp_sess->debug($debug_mask);
2386 }
2387
2388 my $oids = [$request_oid];
2389
2390 my $snmp_resp = $snmp_sess->get_request(
2391 -varbindList => $oids,
2392 );
2393 unless ($snmp_resp) {
2394 die("No SNMP response received: " . $snmp_sess->error());
2395 }
2396
2397 # Do we have the requested OID in the response?
2398 unless (defined($snmp_resp->{$request_oid})) {
2399 die("Missing required OID $request_oid in response");
2400 }
2401
2402 my $value = $snmp_resp->{$request_oid};
2403
2404 if ($ENV{TEST_VERBOSE}) {
2405 print STDERR "Requested OID $request_oid = $value\n";
2406 }
2407
2408 my $expected = 0;
2409
2410 $self->assert($expected == $value,
2411 test_msg("Expected value $expected for OID, got $value"));
2412
2413 $snmp_sess->close();
2414 $snmp_sess = undef;
2415 };
2416 if ($@) {
2417 $ex = $@;
2418 }
2419
2420 $wfh->print("done\n");
2421 $wfh->flush();
2422
2423 } else {
2424 eval { server_wait($setup->{config_file}, $rfh) };
2425 if ($@) {
2426 warn($@);
2427 exit 1;
2428 }
2429
2430 exit 0;
2431 }
2432
2433 # Stop server
2434 server_stop($setup->{pid_file});
2435 $self->assert_child_ok($pid);
2436
2437 test_cleanup($setup->{log_file}, $ex);
2438 }
2439
2440 sub snmp_v1_get_daemon_maxinsts_count {
2441 my $self = shift;
2442 my $tmpdir = $self->{tmpdir};
2443 my $setup = test_setup($tmpdir, 'snmp');
2444
2445 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
2446
2447 # Make sure that, if we're running as root, that the home directory has
2448 # permissions/privs set for the account we create
2449 if ($< == 0) {
2450 unless (chmod(0755, $table_dir)) {
2451 die("Can't set perms on $table_dir to 0755: $!");
2452 }
2453
2454 unless (chown($setup->{uid}, $setup->{gid}, $table_dir)) {
2455 die("Can't set owner of $table_dir to $setup->{uid}/$setup->{gid}: $!");
2456 }
2457 }
2458
2459 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
2460 my $snmp_community = "public";
2461
2462 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.11.0';
2463
2464 my $max_instances = 1;
2465
2466 my $config = {
2467 PidFile => $setup->{pid_file},
2468 ScoreboardFile => $setup->{scoreboard_file},
2469 SystemLog => $setup->{log_file},
2470 TraceLog => $setup->{log_file},
2471 Trace => 'event:20 snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
2472
2473 AuthUserFile => $setup->{auth_user_file},
2474 AuthGroupFile => $setup->{auth_group_file},
2475 AuthOrder => 'mod_auth_file.c',
2476
2477 MaxInstances => $max_instances,
2478
2479 IfModules => {
2480 'mod_delay.c' => {
2481 DelayEngine => 'off',
2482 },
2483
2484 'mod_snmp.c' => {
2485 SNMPAgent => "master 127.0.0.1:$agent_port",
2486 SNMPCommunity => $snmp_community,
2487 SNMPEngine => 'on',
2488 SNMPLog => $setup->{log_file},
2489 SNMPTables => $table_dir,
2490 },
2491 },
2492 };
2493
2494 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2495 $config);
2496
2497 # Open pipes, for use between the parent and child processes. Specifically,
2498 # the child will indicate when it's done with its test by writing a message
2499 # to the parent.
2500 my ($rfh, $wfh);
2501 unless (pipe($rfh, $wfh)) {
2502 die("Can't open pipe: $!");
2503 }
2504
2505 require Net::SNMP;
2506
2507 my $ex;
2508
2509 # Fork child
2510 $self->handle_sigchld();
2511 defined(my $pid = fork()) or die("Can't fork: $!");
2512 if ($pid) {
2513 eval {
2514 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2515
2516 # Since we set "MaxInstances 1", we should not be able to connect any
2517 # more clients.
2518 for (my $i = 0; $i < 3; $i++) {
2519 eval { ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1) };
2520 unless ($@) {
2521 die("Unexpectedly succeeded in connecting (attempt #$i)");
2522 }
2523 }
2524
2525 $client->quit();
2526
2527 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
2528 -hostname => '127.0.0.1',
2529 -port => $agent_port,
2530 -version => 'snmpv1',
2531 -community => $snmp_community,
2532 -retries => 1,
2533 -timeout => 3,
2534 -translate => 1,
2535 );
2536 unless ($snmp_sess) {
2537 die("Unable to create Net::SNMP session: $snmp_err");
2538 }
2539
2540 if ($ENV{TEST_VERBOSE}) {
2541 # From the Net::SNMP debug perldocs
2542 my $debug_mask = (0x02|0x10|0x20);
2543 $snmp_sess->debug($debug_mask);
2544 }
2545
2546 my $oids = [$request_oid];
2547
2548 my $snmp_resp = $snmp_sess->get_request(
2549 -varbindList => $oids,
2550 );
2551 unless ($snmp_resp) {
2552 die("No SNMP response received: " . $snmp_sess->error());
2553 }
2554
2555 # Do we have the requested OID in the response?
2556 unless (defined($snmp_resp->{$request_oid})) {
2557 die("Missing required OID $request_oid in response");
2558 }
2559
2560 my $value = $snmp_resp->{$request_oid};
2561
2562 if ($ENV{TEST_VERBOSE}) {
2563 print STDERR "Requested OID $request_oid = $value\n";
2564 }
2565
2566 my $expected = 6;
2567
2568 $self->assert($expected == $value,
2569 test_msg("Expected value $expected for OID, got $value"));
2570
2571 $snmp_sess->close();
2572 $snmp_sess = undef;
2573 };
2574 if ($@) {
2575 $ex = $@;
2576 }
2577
2578 $wfh->print("done\n");
2579 $wfh->flush();
2580
2581 } else {
2582 eval { server_wait($setup->{config_file}, $rfh) };
2583 if ($@) {
2584 warn($@);
2585 exit 1;
2586 }
2587
2588 exit 0;
2589 }
2590
2591 # Stop server
2592 server_stop($setup->{pid_file});
2593 $self->assert_child_ok($pid);
2594
2595 test_cleanup($setup->{log_file}, $ex);
2596 }
2597
2598 sub snmp_v1_get_daemon_connrefused_count {
2599 my $self = shift;
2600 my $tmpdir = $self->{tmpdir};
2601 my $setup = test_setup($tmpdir, 'snmp');
2602
2603 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
2604
2605 # Make sure that, if we're running as root, that the home directory has
2606 # permissions/privs set for the account we create
2607 if ($< == 0) {
2608 unless (chmod(0755, $table_dir)) {
2609 die("Can't set perms on $table_dir to 0755: $!");
2610 }
2611
2612 unless (chown($setup->{uid}, $setup->{gid}, $table_dir)) {
2613 die("Can't set owner of $table_dir to $setup->{uid}/$setup->{gid}: $!");
2614 }
2615 }
2616
2617 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
2618 my $snmp_community = "public";
2619
2620 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.8.0';
2621
2622 my $config = {
2623 PidFile => $setup->{pid_file},
2624 ScoreboardFile => $setup->{scoreboard_file},
2625 SystemLog => $setup->{log_file},
2626 TraceLog => $setup->{log_file},
2627 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
2628
2629 AuthUserFile => $setup->{auth_user_file},
2630 AuthGroupFile => $setup->{auth_group_file},
2631 AuthOrder => 'mod_auth_file.c',
2632
2633 IfModules => {
2634 'mod_delay.c' => {
2635 DelayEngine => 'off',
2636 },
2637
2638 'mod_snmp.c' => {
2639 SNMPAgent => "master 127.0.0.1:$agent_port",
2640 SNMPCommunity => $snmp_community,
2641 SNMPEngine => 'on',
2642 SNMPLog => $setup->{log_file},
2643 SNMPTables => $table_dir,
2644 },
2645 },
2646
2647 Limit => {
2648 LOGIN => {
2649 DenyAll => '',
2650 },
2651 },
2652 };
2653
2654 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2655 $config);
2656
2657 # Open pipes, for use between the parent and child processes. Specifically,
2658 # the child will indicate when it's done with its test by writing a message
2659 # to the parent.
2660 my ($rfh, $wfh);
2661 unless (pipe($rfh, $wfh)) {
2662 die("Can't open pipe: $!");
2663 }
2664
2665 require Net::SNMP;
2666
2667 my $ex;
2668
2669 # Fork child
2670 $self->handle_sigchld();
2671 defined(my $pid = fork()) or die("Can't fork: $!");
2672 if ($pid) {
2673 eval {
2674 # Since we deny all logins, we should not be able to connect any
2675 # clients.
2676 for (my $i = 0; $i < 3; $i++) {
2677 eval { ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1) };
2678 unless ($@) {
2679 die("Unexpectedly succeeded in connecting (attempt #$i)");
2680 }
2681 }
2682
2683 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
2684 -hostname => '127.0.0.1',
2685 -port => $agent_port,
2686 -version => 'snmpv1',
2687 -community => $snmp_community,
2688 -retries => 1,
2689 -timeout => 3,
2690 -translate => 1,
2691 );
2692 unless ($snmp_sess) {
2693 die("Unable to create Net::SNMP session: $snmp_err");
2694 }
2695
2696 if ($ENV{TEST_VERBOSE}) {
2697 # From the Net::SNMP debug perldocs
2698 my $debug_mask = (0x02|0x10|0x20);
2699 $snmp_sess->debug($debug_mask);
2700 }
2701
2702 my $oids = [$request_oid];
2703
2704 my $snmp_resp = $snmp_sess->get_request(
2705 -varbindList => $oids,
2706 );
2707 unless ($snmp_resp) {
2708 die("No SNMP response received: " . $snmp_sess->error());
2709 }
2710
2711 # Do we have the requested OID in the response?
2712 unless (defined($snmp_resp->{$request_oid})) {
2713 die("Missing required OID $request_oid in response");
2714 }
2715
2716 my $value = $snmp_resp->{$request_oid};
2717
2718 if ($ENV{TEST_VERBOSE}) {
2719 print STDERR "Requested OID $request_oid = $value\n";
2720 }
2721
2722 my $expected = 5;
2723
2724 $self->assert($expected == $value,
2725 test_msg("Expected value $expected for OID, got $value"));
2726
2727 $snmp_sess->close();
2728 $snmp_sess = undef;
2729 };
2730 if ($@) {
2731 $ex = $@;
2732 }
2733
2734 $wfh->print("done\n");
2735 $wfh->flush();
2736
2737 } else {
2738 eval { server_wait($setup->{config_file}, $rfh) };
2739 if ($@) {
2740 warn($@);
2741 exit 1;
2742 }
2743
2744 exit 0;
2745 }
2746
2747 # Stop server
2748 server_stop($setup->{pid_file});
2749 $self->assert_child_ok($pid);
2750
2751 test_cleanup($setup->{log_file}, $ex);
2752 }
2753
2754 sub snmp_v1_get_ftp_sess_counts {
2755 my $self = shift;
2756 my $tmpdir = $self->{tmpdir};
2757
2758 my $config_file = "$tmpdir/snmp.conf";
2759 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
2760 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
2761
2762 my $log_file = test_get_logfile();
2763
2764 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
2765 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
2766
2767 my $user = 'proftpd';
2768 my $passwd = 'test';
2769 my $group = 'ftpd';
2770 my $home_dir = File::Spec->rel2abs($tmpdir);
2771 my $uid = 500;
2772 my $gid = 500;
2773
2774 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
2775
2776 # Make sure that, if we're running as root, that the home directory has
2777 # permissions/privs set for the account we create
2778 if ($< == 0) {
2779 unless (chmod(0755, $home_dir, $table_dir)) {
2780 die("Can't set perms on $home_dir to 0755: $!");
2781 }
2782
2783 unless (chown($uid, $gid, $home_dir, $table_dir)) {
2784 die("Can't set owner of $home_dir to $uid/$gid: $!");
2785 }
2786 }
2787
2788 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2789 '/bin/bash');
2790 auth_group_write($auth_group_file, $group, $gid, $user);
2791
2792 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
2793 my $snmp_community = "public";
2794
2795 my $timeout_idle = 45;
2796
2797 my $config = {
2798 TraceLog => $log_file,
2799 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
2800 PidFile => $pid_file,
2801 ScoreboardFile => $scoreboard_file,
2802 SystemLog => $log_file,
2803
2804 AuthUserFile => $auth_user_file,
2805 AuthGroupFile => $auth_group_file,
2806 AuthOrder => 'mod_auth_file.c',
2807
2808 TimeoutIdle => $timeout_idle + 1,
2809
2810 IfModules => {
2811 'mod_delay.c' => {
2812 DelayEngine => 'off',
2813 },
2814
2815 'mod_snmp.c' => {
2816 SNMPAgent => "master 127.0.0.1:$agent_port",
2817 SNMPCommunity => $snmp_community,
2818 SNMPEngine => 'on',
2819 SNMPLog => $log_file,
2820 SNMPTables => $table_dir,
2821 },
2822 },
2823 };
2824
2825 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2826
2827 # Open pipes, for use between the parent and child processes. Specifically,
2828 # the child will indicate when it's done with its test by writing a message
2829 # to the parent.
2830 my ($rfh, $wfh);
2831 unless (pipe($rfh, $wfh)) {
2832 die("Can't open pipe: $!");
2833 }
2834
2835 require Net::SNMP;
2836
2837 my $ex;
2838
2839 # Fork child
2840 $self->handle_sigchld();
2841 defined(my $pid = fork()) or die("Can't fork: $!");
2842 if ($pid) {
2843 eval {
2844 my $expected;
2845
2846 # First, get the session count
2847 my ($sess_count, $sess_total) = get_ftp_sess_info($agent_port,
2848 $snmp_community);
2849
2850 $expected = 0;
2851 $self->assert($sess_count == $expected,
2852 test_msg("Expected session count $expected, got $sess_count"));
2853
2854 $expected = 0;
2855 $self->assert($sess_total == $expected,
2856 test_msg("Expected session total $expected, got $sess_total"));
2857
2858 # Now, login
2859 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2860
2861 # Get the session counts again. We've only connected, not logged in,
2862 # so the session counts should not have changed.
2863 ($sess_count, $sess_total) = get_ftp_sess_info($agent_port, $snmp_community);
2864
2865 $expected = 0;
2866 $self->assert($sess_count == $expected,
2867 test_msg("Expected session count $expected, got $sess_count"));
2868
2869 $expected = 0;
2870 $self->assert($sess_total == $expected,
2871 test_msg("Expected session total $expected, got $sess_total"));
2872
2873 # Login.
2874 $client->login($user, $passwd);
2875
2876 # Then, get the session counts again
2877 ($sess_count, $sess_total) = get_ftp_sess_info($agent_port, $snmp_community);
2878
2879 $expected = 1;
2880 $self->assert($sess_count == $expected,
2881 test_msg("Expected session count $expected, got $sess_count"));
2882
2883 $expected = 1;
2884 $self->assert($sess_total == $expected,
2885 test_msg("Expected session total $expected, got $sess_total"));
2886
2887 # Now log out
2888 $client->quit();
2889 $client = undef;
2890
2891 # And get the session counts one more time, make sure it's what we expect
2892 ($sess_count, $sess_total) = get_ftp_sess_info($agent_port, $snmp_community);
2893
2894 # We MIGHT see a value of 1 here; I suspect it is leftover from the
2895 # previous test (and in anonymous memory). Tolerate it, for now.
2896 $expected = 0;
2897 $self->assert($sess_count == 0 || $sess_count == 1,
2898 test_msg("Expected session count $expected, got $sess_count"));
2899
2900 $expected = 1;
2901 $self->assert($sess_total == $expected,
2902 test_msg("Expected session total $expected, got $sess_total"));
2903 };
2904
2905 if ($@) {
2906 $ex = $@;
2907 }
2908
2909 $wfh->print("done\n");
2910 $wfh->flush();
2911
2912 } else {
2913 eval { server_wait($config_file, $rfh, $timeout_idle) };
2914 if ($@) {
2915 warn($@);
2916 exit 1;
2917 }
2918
2919 exit 0;
2920 }
2921
2922 # Stop server
2923 server_stop($pid_file);
2924
2925 $self->assert_child_ok($pid);
2926
2927 if ($ex) {
2928 test_append_logfile($log_file, $ex);
2929 unlink($log_file);
2930
2931 die($ex);
2932 }
2933
2934 unlink($log_file);
2935 }
2936
2937 sub snmp_v1_get_ftp_xfer_upload_counts {
2938 my $self = shift;
2939 my $tmpdir = $self->{tmpdir};
2940
2941 my $config_file = "$tmpdir/snmp.conf";
2942 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
2943 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
2944
2945 my $log_file = test_get_logfile();
2946
2947 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
2948 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
2949
2950 my $user = 'proftpd';
2951 my $passwd = 'test';
2952 my $group = 'ftpd';
2953 my $home_dir = File::Spec->rel2abs($tmpdir);
2954 my $uid = 500;
2955 my $gid = 500;
2956
2957 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
2958
2959 # Make sure that, if we're running as root, that the home directory has
2960 # permissions/privs set for the account we create
2961 if ($< == 0) {
2962 unless (chmod(0755, $home_dir, $table_dir)) {
2963 die("Can't set perms on $home_dir to 0755: $!");
2964 }
2965
2966 unless (chown($uid, $gid, $home_dir, $table_dir)) {
2967 die("Can't set owner of $home_dir to $uid/$gid: $!");
2968 }
2969 }
2970
2971 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2972 '/bin/bash');
2973 auth_group_write($auth_group_file, $group, $gid, $user);
2974
2975 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
2976 my $snmp_community = "public";
2977
2978 my $timeout_idle = 45;
2979
2980 my $config = {
2981 TraceLog => $log_file,
2982 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
2983 PidFile => $pid_file,
2984 ScoreboardFile => $scoreboard_file,
2985 SystemLog => $log_file,
2986
2987 AuthUserFile => $auth_user_file,
2988 AuthGroupFile => $auth_group_file,
2989 AuthOrder => 'mod_auth_file.c',
2990
2991 TimeoutIdle => $timeout_idle + 1,
2992
2993 IfModules => {
2994 'mod_delay.c' => {
2995 DelayEngine => 'off',
2996 },
2997
2998 'mod_snmp.c' => {
2999 SNMPAgent => "master 127.0.0.1:$agent_port",
3000 SNMPCommunity => $snmp_community,
3001 SNMPEngine => 'on',
3002 SNMPLog => $log_file,
3003 SNMPTables => $table_dir,
3004 },
3005 },
3006 };
3007
3008 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3009
3010 # Open pipes, for use between the parent and child processes. Specifically,
3011 # the child will indicate when it's done with its test by writing a message
3012 # to the parent.
3013 my ($rfh, $wfh);
3014 unless (pipe($rfh, $wfh)) {
3015 die("Can't open pipe: $!");
3016 }
3017
3018 require Net::SNMP;
3019
3020 my $ex;
3021
3022 # Fork child
3023 $self->handle_sigchld();
3024 defined(my $pid = fork()) or die("Can't fork: $!");
3025 if ($pid) {
3026 eval {
3027 my $expected;
3028
3029 # First, get the upload count/KB
3030 my ($file_count, $file_total, $kb_count) = get_ftp_xfer_upload_info($agent_port,
3031 $snmp_community);
3032
3033 $expected = 0;
3034 $self->assert($file_count == $expected,
3035 test_msg("Expected upload file count $expected, got $file_count"));
3036
3037 $expected = 0;
3038 $self->assert($file_total == $expected,
3039 test_msg("Expected upload file total $expected, got $file_total"));
3040
3041 $expected = 0;
3042 $self->assert($kb_count == $expected,
3043 test_msg("Expected upload KB count $expected, got $kb_count"));
3044
3045 # Next, upload a file
3046 my $file_path = 'test1.txt';
3047 my $file_kb_len = 4;
3048 upload_file($port, $user, $passwd, $file_path, $file_kb_len);
3049
3050 # Then, get the upload count/KB again
3051 ($file_count, $file_total, $kb_count) = get_ftp_xfer_upload_info($agent_port, $snmp_community);
3052
3053 $expected = 0;
3054 $self->assert($file_count == $expected,
3055 test_msg("Expected upload file count $expected, got $file_count"));
3056
3057 $expected = 1;
3058 $self->assert($file_total == $expected,
3059 test_msg("Expected upload file total $expected, got $file_total"));
3060
3061 $expected = $file_kb_len;
3062 $self->assert($kb_count == $expected,
3063 test_msg("Expected upload KB count $expected, got $kb_count"));
3064
3065 # Upload another file
3066 $file_path = 'test2.txt';
3067 upload_file($port, $user, $passwd, $file_path, $file_kb_len);
3068
3069 # Get the upload count/KB one more time, make sure it's what we expect
3070 ($file_count, $file_total, $kb_count) = get_ftp_xfer_upload_info($agent_port, $snmp_community);
3071
3072 $expected = 0;
3073 $self->assert($file_count == $expected,
3074 test_msg("Expected upload file count $expected, got $file_count"));
3075
3076 $expected = 2;
3077 $self->assert($file_total == $expected,
3078 test_msg("Expected upload file total $expected, got $file_total"));
3079
3080 $expected = ($file_kb_len * 2);
3081 $self->assert($kb_count == $expected,
3082 test_msg("Expected upload KB count $expected, got $kb_count"));
3083
3084 # Now wait for 5 secs, then try again (make sure the counters aren't
3085 # reset somehow on the server's side).
3086 sleep(5);
3087
3088 ($file_count, $file_total, $kb_count) = get_ftp_xfer_upload_info($agent_port, $snmp_community);
3089
3090 $expected = 0;
3091 $self->assert($file_count == $expected,
3092 test_msg("Expected upload file count $expected, got $file_count"));
3093
3094 $expected = 2;
3095 $self->assert($file_total == $expected,
3096 test_msg("Expected upload file total $expected, got $file_total"));
3097
3098 $expected = ($file_kb_len * 2);
3099 $self->assert($kb_count == $expected,
3100 test_msg("Expected upload KB count $expected, got $kb_count"));
3101 };
3102
3103 if ($@) {
3104 $ex = $@;
3105 }
3106
3107 $wfh->print("done\n");
3108 $wfh->flush();
3109
3110 } else {
3111 eval { server_wait($config_file, $rfh, $timeout_idle) };
3112 if ($@) {
3113 warn($@);
3114 exit 1;
3115 }
3116
3117 exit 0;
3118 }
3119
3120 # Stop server
3121 server_stop($pid_file);
3122
3123 $self->assert_child_ok($pid);
3124
3125 if ($ex) {
3126 test_append_logfile($log_file, $ex);
3127 unlink($log_file);
3128
3129 die($ex);
3130 }
3131
3132 unlink($log_file);
3133 }
3134
3135 sub snmp_v1_get_multi {
3136 my $self = shift;
3137 my $tmpdir = $self->{tmpdir};
3138
3139 my $config_file = "$tmpdir/snmp.conf";
3140 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
3141 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
3142
3143 my $log_file = test_get_logfile();
3144
3145 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
3146 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
3147
3148 my $user = 'proftpd';
3149 my $passwd = 'test';
3150 my $group = 'ftpd';
3151 my $home_dir = File::Spec->rel2abs($tmpdir);
3152 my $uid = 500;
3153 my $gid = 500;
3154
3155 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
3156
3157 # Make sure that, if we're running as root, that the home directory has
3158 # permissions/privs set for the account we create
3159 if ($< == 0) {
3160 unless (chmod(0755, $home_dir, $table_dir)) {
3161 die("Can't set perms on $home_dir to 0755: $!");
3162 }
3163
3164 unless (chown($uid, $gid, $home_dir, $table_dir)) {
3165 die("Can't set owner of $home_dir to $uid/$gid: $!");
3166 }
3167 }
3168
3169 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3170 '/bin/bash');
3171 auth_group_write($auth_group_file, $group, $gid, $user);
3172
3173 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
3174 my $snmp_community = "public";
3175
3176 my $request_oids = [
3177 '1.3.6.1.4.1.17852.2.2.1.1.0',
3178 '1.3.6.1.4.1.17852.2.2.1.3.0',
3179 ];
3180
3181 my $expected_oid_values = {
3182 '1.3.6.1.4.1.17852.2.2.1.1.0' => 'proftpd',
3183 '1.3.6.1.4.1.17852.2.2.1.3.0' => '[email protected]',
3184 };
3185
3186 my $config = {
3187 TraceLog => $log_file,
3188 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
3189 PidFile => $pid_file,
3190 ScoreboardFile => $scoreboard_file,
3191 SystemLog => $log_file,
3192
3193 AuthUserFile => $auth_user_file,
3194 AuthGroupFile => $auth_group_file,
3195 AuthOrder => 'mod_auth_file.c',
3196
3197 IfModules => {
3198 'mod_delay.c' => {
3199 DelayEngine => 'off',
3200 },
3201
3202 'mod_snmp.c' => {
3203 SNMPAgent => "master 127.0.0.1:$agent_port",
3204 SNMPCommunity => $snmp_community,
3205 SNMPEngine => 'on',
3206 SNMPLog => $log_file,
3207 SNMPTables => $table_dir,
3208 },
3209 },
3210 };
3211
3212 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3213
3214 # Open pipes, for use between the parent and child processes. Specifically,
3215 # the child will indicate when it's done with its test by writing a message
3216 # to the parent.
3217 my ($rfh, $wfh);
3218 unless (pipe($rfh, $wfh)) {
3219 die("Can't open pipe: $!");
3220 }
3221
3222 require Net::SNMP;
3223
3224 my $ex;
3225
3226 # Fork child
3227 $self->handle_sigchld();
3228 defined(my $pid = fork()) or die("Can't fork: $!");
3229 if ($pid) {
3230 eval {
3231 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
3232 -hostname => '127.0.0.1',
3233 -port => $agent_port,
3234 -version => 'snmpv1',
3235 -community => $snmp_community,
3236 -retries => 1,
3237 -timeout => 3,
3238 -translate => 1,
3239 );
3240 unless ($snmp_sess) {
3241 die("Unable to create Net::SNMP session: $snmp_err");
3242 }
3243
3244 if ($ENV{TEST_VERBOSE}) {
3245 # From the Net::SNMP debug perldocs
3246 my $debug_mask = (0x02|0x10|0x20);
3247 $snmp_sess->debug($debug_mask);
3248 }
3249
3250 my $snmp_resp = $snmp_sess->get_request(
3251 -varbindList => $request_oids,
3252 );
3253 unless ($snmp_resp) {
3254 die("No SNMP response received: " . $snmp_sess->error());
3255 }
3256
3257 # Do we have the requested OIDs in the response?
3258 foreach my $request_oid (@$request_oids) {
3259 unless (defined($snmp_resp->{$request_oid})) {
3260 die("Missing required OID $request_oid in response");
3261 }
3262
3263 my $value = $snmp_resp->{$request_oid};
3264
3265 if ($ENV{TEST_VERBOSE}) {
3266 print STDERR "Requested OID $request_oid = $value\n";
3267 }
3268
3269 my $expected = $expected_oid_values->{$request_oid};
3270
3271 $self->assert($expected eq $value,
3272 test_msg("Expected value '$expected' for OID, got '$value'"));
3273 }
3274
3275 $snmp_sess->close();
3276 $snmp_sess = undef;
3277 };
3278
3279 if ($@) {
3280 $ex = $@;
3281 }
3282
3283 $wfh->print("done\n");
3284 $wfh->flush();
3285
3286 } else {
3287 eval { server_wait($config_file, $rfh) };
3288 if ($@) {
3289 warn($@);
3290 exit 1;
3291 }
3292
3293 exit 0;
3294 }
3295
3296 # Stop server
3297 server_stop($pid_file);
3298
3299 $self->assert_child_ok($pid);
3300
3301 if ($ex) {
3302 test_append_logfile($log_file, $ex);
3303 unlink($log_file);
3304
3305 die($ex);
3306 }
3307
3308 unlink($log_file);
3309 }
3310
3311 sub snmp_v1_get_multi_with_unknown {
3312 my $self = shift;
3313 my $tmpdir = $self->{tmpdir};
3314
3315 my $config_file = "$tmpdir/snmp.conf";
3316 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
3317 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
3318
3319 my $log_file = test_get_logfile();
3320
3321 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
3322 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
3323
3324 my $user = 'proftpd';
3325 my $passwd = 'test';
3326 my $group = 'ftpd';
3327 my $home_dir = File::Spec->rel2abs($tmpdir);
3328 my $uid = 500;
3329 my $gid = 500;
3330
3331 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
3332
3333 # Make sure that, if we're running as root, that the home directory has
3334 # permissions/privs set for the account we create
3335 if ($< == 0) {
3336 unless (chmod(0755, $home_dir, $table_dir)) {
3337 die("Can't set perms on $home_dir to 0755: $!");
3338 }
3339
3340 unless (chown($uid, $gid, $home_dir, $table_dir)) {
3341 die("Can't set owner of $home_dir to $uid/$gid: $!");
3342 }
3343 }
3344
3345 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3346 '/bin/bash');
3347 auth_group_write($auth_group_file, $group, $gid, $user);
3348
3349 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
3350 my $snmp_community = "public";
3351
3352 my $request_oids = [
3353 '1.3.6.1.4.1.17852.2.2.1.1.0',
3354 '1.3.6.1.4.1.17852.1.1',
3355 ];
3356
3357 my $config = {
3358 TraceLog => $log_file,
3359 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
3360 PidFile => $pid_file,
3361 ScoreboardFile => $scoreboard_file,
3362 SystemLog => $log_file,
3363
3364 AuthUserFile => $auth_user_file,
3365 AuthGroupFile => $auth_group_file,
3366 AuthOrder => 'mod_auth_file.c',
3367
3368 IfModules => {
3369 'mod_delay.c' => {
3370 DelayEngine => 'off',
3371 },
3372
3373 'mod_snmp.c' => {
3374 SNMPAgent => "master 127.0.0.1:$agent_port",
3375 SNMPCommunity => $snmp_community,
3376 SNMPEngine => 'on',
3377 SNMPLog => $log_file,
3378 SNMPTables => $table_dir,
3379 },
3380 },
3381 };
3382
3383 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3384
3385 # Open pipes, for use between the parent and child processes. Specifically,
3386 # the child will indicate when it's done with its test by writing a message
3387 # to the parent.
3388 my ($rfh, $wfh);
3389 unless (pipe($rfh, $wfh)) {
3390 die("Can't open pipe: $!");
3391 }
3392
3393 require Net::SNMP;
3394
3395 my $ex;
3396
3397 # Fork child
3398 $self->handle_sigchld();
3399 defined(my $pid = fork()) or die("Can't fork: $!");
3400 if ($pid) {
3401 eval {
3402 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
3403 -hostname => '127.0.0.1',
3404 -port => $agent_port,
3405 -version => 'snmpv1',
3406 -community => $snmp_community,
3407 -retries => 1,
3408 -timeout => 3,
3409 -translate => 1,
3410 );
3411 unless ($snmp_sess) {
3412 die("Unable to create Net::SNMP session: $snmp_err");
3413 }
3414
3415 if ($ENV{TEST_VERBOSE}) {
3416 # From the Net::SNMP debug perldocs
3417 my $debug_mask = (0x02|0x10|0x20);
3418 $snmp_sess->debug($debug_mask);
3419 }
3420
3421 my $snmp_resp = $snmp_sess->get_request(
3422 -varbindList => $request_oids,
3423 );
3424 if ($snmp_resp) {
3425 die("SNMP response received unexpectedly");
3426 }
3427
3428 my $err = $snmp_sess->error();
3429 my $expected = 'Received noSuchName(2) error-status at error-index 2';
3430
3431 $self->assert($expected eq $err,
3432 test_msg("Expected error '$expected' for OIDs, got '$err'"));
3433
3434 $snmp_sess->close();
3435 $snmp_sess = undef;
3436 };
3437
3438 if ($@) {
3439 $ex = $@;
3440 }
3441
3442 $wfh->print("done\n");
3443 $wfh->flush();
3444
3445 } else {
3446 eval { server_wait($config_file, $rfh) };
3447 if ($@) {
3448 warn($@);
3449 exit 1;
3450 }
3451
3452 exit 0;
3453 }
3454
3455 # Stop server
3456 server_stop($pid_file);
3457
3458 $self->assert_child_ok($pid);
3459
3460 if ($ex) {
3461 test_append_logfile($log_file, $ex);
3462 unlink($log_file);
3463
3464 die($ex);
3465 }
3466
3467 unlink($log_file);
3468 }
3469
3470 sub snmp_v1_get_next {
3471 my $self = shift;
3472 my $tmpdir = $self->{tmpdir};
3473
3474 my $config_file = "$tmpdir/snmp.conf";
3475 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
3476 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
3477
3478 my $log_file = test_get_logfile();
3479
3480 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
3481 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
3482
3483 my $user = 'proftpd';
3484 my $passwd = 'test';
3485 my $group = 'ftpd';
3486 my $home_dir = File::Spec->rel2abs($tmpdir);
3487 my $uid = 500;
3488 my $gid = 500;
3489
3490 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
3491
3492 # Make sure that, if we're running as root, that the home directory has
3493 # permissions/privs set for the account we create
3494 if ($< == 0) {
3495 unless (chmod(0755, $home_dir, $table_dir)) {
3496 die("Can't set perms on $home_dir to 0755: $!");
3497 }
3498
3499 unless (chown($uid, $gid, $home_dir, $table_dir)) {
3500 die("Can't set owner of $home_dir to $uid/$gid: $!");
3501 }
3502 }
3503
3504 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3505 '/bin/bash');
3506 auth_group_write($auth_group_file, $group, $gid, $user);
3507
3508 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
3509 my $snmp_community = "public";
3510
3511 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.2.0';
3512 my $next_oid = '1.3.6.1.4.1.17852.2.2.1.3.0';
3513
3514 my $config = {
3515 TraceLog => $log_file,
3516 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
3517 PidFile => $pid_file,
3518 ScoreboardFile => $scoreboard_file,
3519 SystemLog => $log_file,
3520
3521 AuthUserFile => $auth_user_file,
3522 AuthGroupFile => $auth_group_file,
3523 AuthOrder => 'mod_auth_file.c',
3524
3525 IfModules => {
3526 'mod_delay.c' => {
3527 DelayEngine => 'off',
3528 },
3529
3530 'mod_snmp.c' => {
3531 SNMPAgent => "master 127.0.0.1:$agent_port",
3532 SNMPCommunity => $snmp_community,
3533 SNMPEngine => 'on',
3534 SNMPLog => $log_file,
3535 SNMPTables => $table_dir,
3536 },
3537 },
3538 };
3539
3540 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3541
3542 # Open pipes, for use between the parent and child processes. Specifically,
3543 # the child will indicate when it's done with its test by writing a message
3544 # to the parent.
3545 my ($rfh, $wfh);
3546 unless (pipe($rfh, $wfh)) {
3547 die("Can't open pipe: $!");
3548 }
3549
3550 require Net::SNMP;
3551
3552 my $ex;
3553
3554 # Fork child
3555 $self->handle_sigchld();
3556 defined(my $pid = fork()) or die("Can't fork: $!");
3557 if ($pid) {
3558 eval {
3559 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
3560 -hostname => '127.0.0.1',
3561 -port => $agent_port,
3562 -version => 'snmpv1',
3563 -community => $snmp_community,
3564 -retries => 1,
3565 -timeout => 3,
3566 -translate => 1,
3567 );
3568 unless ($snmp_sess) {
3569 die("Unable to create Net::SNMP session: $snmp_err");
3570 }
3571
3572 if ($ENV{TEST_VERBOSE}) {
3573 # From the Net::SNMP debug perldocs
3574 my $debug_mask = (0x02|0x10|0x20);
3575 $snmp_sess->debug($debug_mask);
3576 }
3577
3578 my $oids = [$request_oid];
3579
3580 my $snmp_resp = $snmp_sess->get_next_request(
3581 -varbindList => $oids,
3582 );
3583 unless ($snmp_resp) {
3584 die("No SNMP response received: " . $snmp_sess->error());
3585 }
3586
3587 # Do we have the next OID of the requested OID in the response?
3588 unless (defined($snmp_resp->{$next_oid})) {
3589 die("Missing required OID $next_oid in response");
3590 }
3591
3592 my $value = $snmp_resp->{$next_oid};
3593
3594 if ($ENV{TEST_VERBOSE}) {
3595 print STDERR "Requested OID $next_oid = $value\n";
3596 }
3597
3598 # The default proftpd string, without ServerAdmin configured.
3599 my $expected = '[email protected]';
3600
3601 $self->assert(qr/$expected/, $value,
3602 test_msg("Expected value '$expected' for OID, got '$value'"));
3603
3604 $snmp_sess->close();
3605 $snmp_sess = undef;
3606 };
3607
3608 if ($@) {
3609 $ex = $@;
3610 }
3611
3612 $wfh->print("done\n");
3613 $wfh->flush();
3614
3615 } else {
3616 eval { server_wait($config_file, $rfh) };
3617 if ($@) {
3618 warn($@);
3619 exit 1;
3620 }
3621
3622 exit 0;
3623 }
3624
3625 # Stop server
3626 server_stop($pid_file);
3627
3628 $self->assert_child_ok($pid);
3629
3630 if ($ex) {
3631 test_append_logfile($log_file, $ex);
3632 unlink($log_file);
3633
3634 die($ex);
3635 }
3636
3637 unlink($log_file);
3638 }
3639
3640 sub snmp_v1_get_next_unknown {
3641 my $self = shift;
3642 my $tmpdir = $self->{tmpdir};
3643
3644 my $config_file = "$tmpdir/snmp.conf";
3645 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
3646 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
3647
3648 my $log_file = test_get_logfile();
3649
3650 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
3651 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
3652
3653 my $user = 'proftpd';
3654 my $passwd = 'test';
3655 my $group = 'ftpd';
3656 my $home_dir = File::Spec->rel2abs($tmpdir);
3657 my $uid = 500;
3658 my $gid = 500;
3659
3660 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
3661
3662 # Make sure that, if we're running as root, that the home directory has
3663 # permissions/privs set for the account we create
3664 if ($< == 0) {
3665 unless (chmod(0755, $home_dir, $table_dir)) {
3666 die("Can't set perms on $home_dir to 0755: $!");
3667 }
3668
3669 unless (chown($uid, $gid, $home_dir, $table_dir)) {
3670 die("Can't set owner of $home_dir to $uid/$gid: $!");
3671 }
3672 }
3673
3674 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3675 '/bin/bash');
3676 auth_group_write($auth_group_file, $group, $gid, $user);
3677
3678 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
3679 my $snmp_community = "public";
3680
3681 my $request_oid = '1.3.6.1.4.1.17852.999.0.1';
3682
3683 my $config = {
3684 TraceLog => $log_file,
3685 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
3686 PidFile => $pid_file,
3687 ScoreboardFile => $scoreboard_file,
3688 SystemLog => $log_file,
3689
3690 AuthUserFile => $auth_user_file,
3691 AuthGroupFile => $auth_group_file,
3692 AuthOrder => 'mod_auth_file.c',
3693
3694 IfModules => {
3695 'mod_delay.c' => {
3696 DelayEngine => 'off',
3697 },
3698
3699 'mod_snmp.c' => {
3700 SNMPAgent => "master 127.0.0.1:$agent_port",
3701 SNMPCommunity => $snmp_community,
3702 SNMPEngine => 'on',
3703 SNMPLog => $log_file,
3704 SNMPTables => $table_dir,
3705 },
3706 },
3707 };
3708
3709 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3710
3711 # Open pipes, for use between the parent and child processes. Specifically,
3712 # the child will indicate when it's done with its test by writing a message
3713 # to the parent.
3714 my ($rfh, $wfh);
3715 unless (pipe($rfh, $wfh)) {
3716 die("Can't open pipe: $!");
3717 }
3718
3719 require Net::SNMP;
3720
3721 my $ex;
3722
3723 # Fork child
3724 $self->handle_sigchld();
3725 defined(my $pid = fork()) or die("Can't fork: $!");
3726 if ($pid) {
3727 eval {
3728 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
3729 -hostname => '127.0.0.1',
3730 -port => $agent_port,
3731 -version => 'snmpv1',
3732 -community => $snmp_community,
3733 -retries => 1,
3734 -timeout => 3,
3735 -translate => 1,
3736 );
3737 unless ($snmp_sess) {
3738 die("Unable to create Net::SNMP session: $snmp_err");
3739 }
3740
3741 if ($ENV{TEST_VERBOSE}) {
3742 # From the Net::SNMP debug perldocs
3743 my $debug_mask = (0x02|0x10|0x20);
3744 $snmp_sess->debug($debug_mask);
3745 }
3746
3747 my $oids = [$request_oid];
3748
3749 my $snmp_resp = $snmp_sess->get_next_request(
3750 -varbindList => $oids,
3751 );
3752 if ($snmp_resp) {
3753 die("SNMP response received unexpectedly");
3754 }
3755
3756 my $err = $snmp_sess->error();
3757 my $expected = 'Received noSuchName(2) error-status at error-index 1';
3758
3759 $self->assert($expected eq $err,
3760 test_msg("Expected error '$expected' for OID, got '$err'"));
3761
3762 $snmp_sess->close();
3763 $snmp_sess = undef;
3764 };
3765
3766 if ($@) {
3767 $ex = $@;
3768 }
3769
3770 $wfh->print("done\n");
3771 $wfh->flush();
3772
3773 } else {
3774 eval { server_wait($config_file, $rfh) };
3775 if ($@) {
3776 warn($@);
3777 exit 1;
3778 }
3779
3780 exit 0;
3781 }
3782
3783 # Stop server
3784 server_stop($pid_file);
3785
3786 $self->assert_child_ok($pid);
3787
3788 if ($ex) {
3789 test_append_logfile($log_file, $ex);
3790 unlink($log_file);
3791
3792 die($ex);
3793 }
3794
3795 unlink($log_file);
3796 }
3797
3798 sub snmp_v1_get_next_missing_instance_id {
3799 my $self = shift;
3800 my $tmpdir = $self->{tmpdir};
3801
3802 my $config_file = "$tmpdir/snmp.conf";
3803 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
3804 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
3805
3806 my $log_file = test_get_logfile();
3807
3808 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
3809 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
3810
3811 my $user = 'proftpd';
3812 my $passwd = 'test';
3813 my $group = 'ftpd';
3814 my $home_dir = File::Spec->rel2abs($tmpdir);
3815 my $uid = 500;
3816 my $gid = 500;
3817
3818 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
3819
3820 # Make sure that, if we're running as root, that the home directory has
3821 # permissions/privs set for the account we create
3822 if ($< == 0) {
3823 unless (chmod(0755, $home_dir, $table_dir)) {
3824 die("Can't set perms on $home_dir to 0755: $!");
3825 }
3826
3827 unless (chown($uid, $gid, $home_dir, $table_dir)) {
3828 die("Can't set owner of $home_dir to $uid/$gid: $!");
3829 }
3830 }
3831
3832 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
3833 '/bin/bash');
3834 auth_group_write($auth_group_file, $group, $gid, $user);
3835
3836 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
3837 my $snmp_community = "public";
3838
3839 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.1';
3840 my $next_oid = '1.3.6.1.4.1.17852.2.2.1.1.0';
3841
3842 my $config = {
3843 TraceLog => $log_file,
3844 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
3845 PidFile => $pid_file,
3846 ScoreboardFile => $scoreboard_file,
3847 SystemLog => $log_file,
3848
3849 AuthUserFile => $auth_user_file,
3850 AuthGroupFile => $auth_group_file,
3851 AuthOrder => 'mod_auth_file.c',
3852
3853 IfModules => {
3854 'mod_delay.c' => {
3855 DelayEngine => 'off',
3856 },
3857
3858 'mod_snmp.c' => {
3859 SNMPAgent => "master 127.0.0.1:$agent_port",
3860 SNMPCommunity => $snmp_community,
3861 SNMPEngine => 'on',
3862 SNMPLog => $log_file,
3863 SNMPTables => $table_dir,
3864 },
3865 },
3866 };
3867
3868 my ($port, $config_user, $config_group) = config_write($config_file, $config);
3869
3870 # Open pipes, for use between the parent and child processes. Specifically,
3871 # the child will indicate when it's done with its test by writing a message
3872 # to the parent.
3873 my ($rfh, $wfh);
3874 unless (pipe($rfh, $wfh)) {
3875 die("Can't open pipe: $!");
3876 }
3877
3878 require Net::SNMP;
3879
3880 my $ex;
3881
3882 # Fork child
3883 $self->handle_sigchld();
3884 defined(my $pid = fork()) or die("Can't fork: $!");
3885 if ($pid) {
3886 eval {
3887 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
3888 -hostname => '127.0.0.1',
3889 -port => $agent_port,
3890 -version => 'snmpv1',
3891 -community => $snmp_community,
3892 -retries => 1,
3893 -timeout => 3,
3894 -translate => 1,
3895 );
3896 unless ($snmp_sess) {
3897 die("Unable to create Net::SNMP session: $snmp_err");
3898 }
3899
3900 if ($ENV{TEST_VERBOSE}) {
3901 # From the Net::SNMP debug perldocs
3902 my $debug_mask = (0x02|0x10|0x20);
3903 $snmp_sess->debug($debug_mask);
3904 }
3905
3906 my $oids = [$request_oid];
3907
3908 my $snmp_resp = $snmp_sess->get_next_request(
3909 -varbindList => $oids,
3910 );
3911 unless ($snmp_resp) {
3912 die("No SNMP response received: " . $snmp_sess->error());
3913 }
3914
3915 # Do we have the next OID of the requested OID in the response?
3916 unless (defined($snmp_resp->{$next_oid})) {
3917 die("Missing required OID $next_oid in response");
3918 }
3919
3920 my $value = $snmp_resp->{$next_oid};
3921
3922 if ($ENV{TEST_VERBOSE}) {
3923 print STDERR "Requested OID $next_oid = $value\n";
3924 }
3925
3926 my $expected = 'proftpd';
3927
3928 $self->assert(qr/$expected/, $value,
3929 test_msg("Expected value '$expected' for OID, got '$value'"));
3930
3931 $snmp_sess->close();
3932 $snmp_sess = undef;
3933 };
3934
3935 if ($@) {
3936 $ex = $@;
3937 }
3938
3939 $wfh->print("done\n");
3940 $wfh->flush();
3941
3942 } else {
3943 eval { server_wait($config_file, $rfh) };
3944 if ($@) {
3945 warn($@);
3946 exit 1;
3947 }
3948
3949 exit 0;
3950 }
3951
3952 # Stop server
3953 server_stop($pid_file);
3954
3955 $self->assert_child_ok($pid);
3956
3957 if ($ex) {
3958 test_append_logfile($log_file, $ex);
3959 unlink($log_file);
3960
3961 die($ex);
3962 }
3963
3964 unlink($log_file);
3965 }
3966
3967 sub snmp_v1_get_next_end_of_mib_view {
3968 my $self = shift;
3969 my $tmpdir = $self->{tmpdir};
3970 my $setup = test_setup($tmpdir, 'snmp');
3971
3972 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
3973
3974 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
3975 my $snmp_community = "public";
3976
3977 # Deliberately request an OID (within the proftpd.snmpModulen.snmp arc)
3978 # which does NOT exist.
3979 my $request_oid = '1.3.6.1.4.1.17852.2.2.4.6.0';
3980 my $timeout = 30;
3981
3982 my $config = {
3983 PidFile => $setup->{pid_file},
3984 ScoreboardFile => $setup->{scoreboard_file},
3985 SystemLog => $setup->{log_file},
3986 TraceLog => $setup->{log_file},
3987 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
3988
3989 AuthUserFile => $setup->{auth_user_file},
3990 AuthGroupFile => $setup->{auth_group_file},
3991 AuthOrder => 'mod_auth_file.c',
3992
3993 IfModules => {
3994 'mod_delay.c' => {
3995 DelayEngine => 'off',
3996 },
3997
3998 'mod_snmp.c' => {
3999 SNMPAgent => "master 127.0.0.1:$agent_port",
4000 SNMPCommunity => $snmp_community,
4001 SNMPEngine => 'on',
4002 SNMPLog => $setup->{log_file},
4003 SNMPTables => $table_dir,
4004 },
4005 },
4006 };
4007
4008 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
4009 $config);
4010
4011 # Open pipes, for use between the parent and child processes. Specifically,
4012 # the child will indicate when it's done with its test by writing a message
4013 # to the parent.
4014 my ($rfh, $wfh);
4015 unless (pipe($rfh, $wfh)) {
4016 die("Can't open pipe: $!");
4017 }
4018
4019 require Net::SNMP;
4020
4021 my $ex;
4022
4023 # Fork child
4024 $self->handle_sigchld();
4025 defined(my $pid = fork()) or die("Can't fork: $!");
4026 if ($pid) {
4027 eval {
4028 # Allow server to start up
4029 sleep(1);
4030
4031 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
4032 -hostname => '127.0.0.1',
4033 -port => $agent_port,
4034 -version => 'snmpv1',
4035 -community => $snmp_community,
4036 -retries => 1,
4037 -timeout => 3,
4038 -translate => 1,
4039 );
4040 unless ($snmp_sess) {
4041 die("Unable to create Net::SNMP session: $snmp_err");
4042 }
4043
4044 if ($ENV{TEST_VERBOSE}) {
4045 # From the Net::SNMP debug perldocs
4046 my $debug_mask = (0x02|0x10|0x20);
4047 $snmp_sess->debug($debug_mask);
4048 }
4049
4050 my $oids = [$request_oid];
4051
4052 my $snmp_resp = $snmp_sess->get_next_request(
4053 -varbindList => $oids,
4054 );
4055 if ($snmp_resp) {
4056 die("SNMP response received unexpectedly");
4057 }
4058
4059 my $err = $snmp_sess->error();
4060 my $expected = 'Received noSuchName(2) error-status at error-index 1';
4061
4062 $self->assert($expected eq $err,
4063 test_msg("Expected error '$expected' for OID, got '$err'"));
4064
4065 $snmp_sess->close();
4066 $snmp_sess = undef;
4067 };
4068
4069 if ($@) {
4070 $ex = $@;
4071 }
4072
4073 $wfh->print("done\n");
4074 $wfh->flush();
4075
4076 } else {
4077 eval { server_wait($setup->{config_file}, $rfh, $timeout) };
4078 if ($@) {
4079 warn($@);
4080 exit 1;
4081 }
4082
4083 exit 0;
4084 }
4085
4086 # Stop server
4087 server_stop($setup->{pid_file});
4088 $self->assert_child_ok($pid);
4089
4090 test_cleanup($setup->{log_file}, $ex);
4091 }
4092
4093 sub snmp_v1_get_next_multi {
4094 my $self = shift;
4095 my $tmpdir = $self->{tmpdir};
4096
4097 my $config_file = "$tmpdir/snmp.conf";
4098 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
4099 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
4100
4101 my $log_file = test_get_logfile();
4102
4103 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
4104 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
4105
4106 my $user = 'proftpd';
4107 my $passwd = 'test';
4108 my $group = 'ftpd';
4109 my $home_dir = File::Spec->rel2abs($tmpdir);
4110 my $uid = 500;
4111 my $gid = 500;
4112
4113 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
4114
4115 # Make sure that, if we're running as root, that the home directory has
4116 # permissions/privs set for the account we create
4117 if ($< == 0) {
4118 unless (chmod(0755, $home_dir, $table_dir)) {
4119 die("Can't set perms on $home_dir to 0755: $!");
4120 }
4121
4122 unless (chown($uid, $gid, $home_dir, $table_dir)) {
4123 die("Can't set owner of $home_dir to $uid/$gid: $!");
4124 }
4125 }
4126
4127 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4128 '/bin/bash');
4129 auth_group_write($auth_group_file, $group, $gid, $user);
4130
4131 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
4132 my $snmp_community = "public";
4133
4134 my $request_oids = [
4135 '1.3.6.1.4.1.17852.2.2.1.2.0',
4136 '1.3.6.1.4.1.17852.2.2.1.4.0',
4137 ];
4138
4139 my $next_oid_values = {
4140 '1.3.6.1.4.1.17852.2.2.1.3.0' => '[email protected]',
4141 '1.3.6.1.4.1.17852.2.2.1.5.0' => '1',
4142 };
4143
4144 my $config = {
4145 TraceLog => $log_file,
4146 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
4147 PidFile => $pid_file,
4148 ScoreboardFile => $scoreboard_file,
4149 SystemLog => $log_file,
4150
4151 AuthUserFile => $auth_user_file,
4152 AuthGroupFile => $auth_group_file,
4153 AuthOrder => 'mod_auth_file.c',
4154
4155 IfModules => {
4156 'mod_delay.c' => {
4157 DelayEngine => 'off',
4158 },
4159
4160 'mod_snmp.c' => {
4161 SNMPAgent => "master 127.0.0.1:$agent_port",
4162 SNMPCommunity => $snmp_community,
4163 SNMPEngine => 'on',
4164 SNMPLog => $log_file,
4165 SNMPTables => $table_dir,
4166 },
4167 },
4168 };
4169
4170 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4171
4172 # Open pipes, for use between the parent and child processes. Specifically,
4173 # the child will indicate when it's done with its test by writing a message
4174 # to the parent.
4175 my ($rfh, $wfh);
4176 unless (pipe($rfh, $wfh)) {
4177 die("Can't open pipe: $!");
4178 }
4179
4180 require Net::SNMP;
4181
4182 my $ex;
4183
4184 # Fork child
4185 $self->handle_sigchld();
4186 defined(my $pid = fork()) or die("Can't fork: $!");
4187 if ($pid) {
4188 eval {
4189 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
4190 -hostname => '127.0.0.1',
4191 -port => $agent_port,
4192 -version => 'snmpv1',
4193 -community => $snmp_community,
4194 -retries => 1,
4195 -timeout => 3,
4196 -translate => 1,
4197 );
4198 unless ($snmp_sess) {
4199 die("Unable to create Net::SNMP session: $snmp_err");
4200 }
4201
4202 if ($ENV{TEST_VERBOSE}) {
4203 # From the Net::SNMP debug perldocs
4204 my $debug_mask = (0x02|0x10|0x20);
4205 $snmp_sess->debug($debug_mask);
4206 }
4207
4208 my $snmp_resp = $snmp_sess->get_next_request(
4209 -varbindList => $request_oids,
4210 );
4211 unless ($snmp_resp) {
4212 die("No SNMP response received: " . $snmp_sess->error());
4213 }
4214
4215 # Do we have the expected OIDs in the response?
4216 foreach my $next_oid (keys(%$next_oid_values)) {
4217 unless (defined($snmp_resp->{$next_oid})) {
4218 die("Missing required OID $next_oid in response");
4219 }
4220
4221 my $value = $snmp_resp->{$next_oid};
4222
4223 if ($ENV{TEST_VERBOSE}) {
4224 print STDERR "Requested OID $next_oid = $value\n";
4225 }
4226
4227 my $expected = $next_oid_values->{$next_oid};
4228
4229 $self->assert($expected eq $value,
4230 test_msg("Expected value '$expected' for OID, got '$value'"));
4231 }
4232
4233 $snmp_sess->close();
4234 $snmp_sess = undef;
4235 };
4236
4237 if ($@) {
4238 $ex = $@;
4239 }
4240
4241 $wfh->print("done\n");
4242 $wfh->flush();
4243
4244 } else {
4245 eval { server_wait($config_file, $rfh) };
4246 if ($@) {
4247 warn($@);
4248 exit 1;
4249 }
4250
4251 exit 0;
4252 }
4253
4254 # Stop server
4255 server_stop($pid_file);
4256
4257 $self->assert_child_ok($pid);
4258
4259 if ($ex) {
4260 test_append_logfile($log_file, $ex);
4261 unlink($log_file);
4262
4263 die($ex);
4264 }
4265
4266 unlink($log_file);
4267 }
4268
4269 sub snmp_v1_get_next_multi_with_unknown {
4270 my $self = shift;
4271 my $tmpdir = $self->{tmpdir};
4272
4273 my $config_file = "$tmpdir/snmp.conf";
4274 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
4275 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
4276
4277 my $log_file = test_get_logfile();
4278
4279 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
4280 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
4281
4282 my $user = 'proftpd';
4283 my $passwd = 'test';
4284 my $group = 'ftpd';
4285 my $home_dir = File::Spec->rel2abs($tmpdir);
4286 my $uid = 500;
4287 my $gid = 500;
4288
4289 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
4290
4291 # Make sure that, if we're running as root, that the home directory has
4292 # permissions/privs set for the account we create
4293 if ($< == 0) {
4294 unless (chmod(0755, $home_dir, $table_dir)) {
4295 die("Can't set perms on $home_dir to 0755: $!");
4296 }
4297
4298 unless (chown($uid, $gid, $home_dir, $table_dir)) {
4299 die("Can't set owner of $home_dir to $uid/$gid: $!");
4300 }
4301 }
4302
4303 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4304 '/bin/bash');
4305 auth_group_write($auth_group_file, $group, $gid, $user);
4306
4307 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
4308 my $snmp_community = "public";
4309
4310 my $request_oids = [
4311 '1.3.6.1.4.1.17852.2.2.1.2.0',
4312 '1.3.6.1.4.1.17852.1.1.0',
4313 ];
4314
4315 my $config = {
4316 TraceLog => $log_file,
4317 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
4318 PidFile => $pid_file,
4319 ScoreboardFile => $scoreboard_file,
4320 SystemLog => $log_file,
4321
4322 AuthUserFile => $auth_user_file,
4323 AuthGroupFile => $auth_group_file,
4324 AuthOrder => 'mod_auth_file.c',
4325
4326 IfModules => {
4327 'mod_delay.c' => {
4328 DelayEngine => 'off',
4329 },
4330
4331 'mod_snmp.c' => {
4332 SNMPAgent => "master 127.0.0.1:$agent_port",
4333 SNMPCommunity => $snmp_community,
4334 SNMPEngine => 'on',
4335 SNMPLog => $log_file,
4336 SNMPTables => $table_dir,
4337 },
4338 },
4339 };
4340
4341 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4342
4343 # Open pipes, for use between the parent and child processes. Specifically,
4344 # the child will indicate when it's done with its test by writing a message
4345 # to the parent.
4346 my ($rfh, $wfh);
4347 unless (pipe($rfh, $wfh)) {
4348 die("Can't open pipe: $!");
4349 }
4350
4351 require Net::SNMP;
4352
4353 my $ex;
4354
4355 # Fork child
4356 $self->handle_sigchld();
4357 defined(my $pid = fork()) or die("Can't fork: $!");
4358 if ($pid) {
4359 eval {
4360 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
4361 -hostname => '127.0.0.1',
4362 -port => $agent_port,
4363 -version => 'snmpv1',
4364 -community => $snmp_community,
4365 -retries => 1,
4366 -timeout => 3,
4367 -translate => 1,
4368 );
4369 unless ($snmp_sess) {
4370 die("Unable to create Net::SNMP session: $snmp_err");
4371 }
4372
4373 if ($ENV{TEST_VERBOSE}) {
4374 # From the Net::SNMP debug perldocs
4375 my $debug_mask = (0x02|0x10|0x20);
4376 $snmp_sess->debug($debug_mask);
4377 }
4378
4379 my $snmp_resp = $snmp_sess->get_next_request(
4380 -varbindList => $request_oids,
4381 );
4382 if ($snmp_resp) {
4383 die("SNMP response received unexpectedly");
4384 }
4385
4386 my $err = $snmp_sess->error();
4387 my $expected = 'Received noSuchName(2) error-status at error-index 2';
4388
4389 $self->assert($expected eq $err,
4390 test_msg("Expected error '$expected', got '$err'"));
4391
4392 $snmp_sess->close();
4393 $snmp_sess = undef;
4394 };
4395
4396 if ($@) {
4397 $ex = $@;
4398 }
4399
4400 $wfh->print("done\n");
4401 $wfh->flush();
4402
4403 } else {
4404 eval { server_wait($config_file, $rfh) };
4405 if ($@) {
4406 warn($@);
4407 exit 1;
4408 }
4409
4410 exit 0;
4411 }
4412
4413 # Stop server
4414 server_stop($pid_file);
4415
4416 $self->assert_child_ok($pid);
4417
4418 if ($ex) {
4419 test_append_logfile($log_file, $ex);
4420 unlink($log_file);
4421
4422 die($ex);
4423 }
4424
4425 unlink($log_file);
4426 }
4427
4428 sub snmp_v1_set {
4429 my $self = shift;
4430 my $tmpdir = $self->{tmpdir};
4431
4432 my $config_file = "$tmpdir/snmp.conf";
4433 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
4434 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
4435
4436 my $log_file = test_get_logfile();
4437
4438 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
4439 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
4440
4441 my $user = 'proftpd';
4442 my $passwd = 'test';
4443 my $group = 'ftpd';
4444 my $home_dir = File::Spec->rel2abs($tmpdir);
4445 my $uid = 500;
4446 my $gid = 500;
4447
4448 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
4449
4450 # Make sure that, if we're running as root, that the home directory has
4451 # permissions/privs set for the account we create
4452 if ($< == 0) {
4453 unless (chmod(0755, $home_dir, $table_dir)) {
4454 die("Can't set perms on $home_dir to 0755: $!");
4455 }
4456
4457 unless (chown($uid, $gid, $home_dir, $table_dir)) {
4458 die("Can't set owner of $home_dir to $uid/$gid: $!");
4459 }
4460 }
4461
4462 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4463 '/bin/bash');
4464 auth_group_write($auth_group_file, $group, $gid, $user);
4465
4466 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
4467 my $snmp_community = "public";
4468
4469 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.1.0';
4470
4471 my $config = {
4472 TraceLog => $log_file,
4473 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
4474 PidFile => $pid_file,
4475 ScoreboardFile => $scoreboard_file,
4476 SystemLog => $log_file,
4477
4478 AuthUserFile => $auth_user_file,
4479 AuthGroupFile => $auth_group_file,
4480 AuthOrder => 'mod_auth_file.c',
4481
4482 IfModules => {
4483 'mod_delay.c' => {
4484 DelayEngine => 'off',
4485 },
4486
4487 'mod_snmp.c' => {
4488 SNMPAgent => "master 127.0.0.1:$agent_port",
4489 SNMPCommunity => $snmp_community,
4490 SNMPEngine => 'on',
4491 SNMPLog => $log_file,
4492 SNMPTables => $table_dir,
4493 },
4494 },
4495 };
4496
4497 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4498
4499 # Open pipes, for use between the parent and child processes. Specifically,
4500 # the child will indicate when it's done with its test by writing a message
4501 # to the parent.
4502 my ($rfh, $wfh);
4503 unless (pipe($rfh, $wfh)) {
4504 die("Can't open pipe: $!");
4505 }
4506
4507 require Net::SNMP;
4508 require Net::SNMP::Message;
4509
4510 my $ex;
4511
4512 # Fork child
4513 $self->handle_sigchld();
4514 defined(my $pid = fork()) or die("Can't fork: $!");
4515 if ($pid) {
4516 eval {
4517 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
4518 -hostname => '127.0.0.1',
4519 -port => $agent_port,
4520 -version => 'snmpv1',
4521 -community => $snmp_community,
4522 -retries => 1,
4523 -timeout => 3,
4524 -translate => 1,
4525 );
4526 unless ($snmp_sess) {
4527 die("Unable to create Net::SNMP session: $snmp_err");
4528 }
4529
4530 if ($ENV{TEST_VERBOSE}) {
4531 # From the Net::SNMP debug perldocs
4532 my $debug_mask = (0x02|0x10|0x20);
4533 $snmp_sess->debug($debug_mask);
4534 }
4535
4536 my $snmp_resp = $snmp_sess->set_request(
4537 -varbindList => [ $request_oid, Net::SNMP::Message::OCTET_STRING(),
4538 'mod_smp' ],
4539 );
4540 if ($snmp_resp) {
4541 die("SNMP response received unexpectedly");
4542 }
4543
4544 my $err = $snmp_sess->error();
4545 my $expected = 'Received noSuchName(2) error-status at error-index 1';
4546
4547 $self->assert($expected eq $err,
4548 test_msg("Expected error '$expected' for OID, got '$err'"));
4549
4550 $snmp_sess->close();
4551 $snmp_sess = undef;
4552 };
4553
4554 if ($@) {
4555 $ex = $@;
4556 }
4557
4558 $wfh->print("done\n");
4559 $wfh->flush();
4560
4561 } else {
4562 eval { server_wait($config_file, $rfh, 10) };
4563 if ($@) {
4564 warn($@);
4565 exit 1;
4566 }
4567
4568 exit 0;
4569 }
4570
4571 # Stop server
4572 server_stop($pid_file);
4573
4574 $self->assert_child_ok($pid);
4575
4576 if ($ex) {
4577 test_append_logfile($log_file, $ex);
4578 unlink($log_file);
4579
4580 die($ex);
4581 }
4582
4583 unlink($log_file);
4584 }
4585
4586 sub snmp_v1_trap {
4587 my $self = shift;
4588 my $tmpdir = $self->{tmpdir};
4589
4590 my $config_file = "$tmpdir/snmp.conf";
4591 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
4592 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
4593
4594 my $log_file = test_get_logfile();
4595
4596 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
4597 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
4598
4599 my $user = 'proftpd';
4600 my $passwd = 'test';
4601 my $group = 'ftpd';
4602 my $home_dir = File::Spec->rel2abs($tmpdir);
4603 my $uid = 500;
4604 my $gid = 500;
4605
4606 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
4607
4608 # Make sure that, if we're running as root, that the home directory has
4609 # permissions/privs set for the account we create
4610 if ($< == 0) {
4611 unless (chmod(0755, $home_dir, $table_dir)) {
4612 die("Can't set perms on $home_dir to 0755: $!");
4613 }
4614
4615 unless (chown($uid, $gid, $home_dir, $table_dir)) {
4616 die("Can't set owner of $home_dir to $uid/$gid: $!");
4617 }
4618 }
4619
4620 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4621 '/bin/bash');
4622 auth_group_write($auth_group_file, $group, $gid, $user);
4623
4624 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
4625 my $snmp_community = "public";
4626
4627 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.1.0';
4628
4629 my $config = {
4630 TraceLog => $log_file,
4631 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
4632 PidFile => $pid_file,
4633 ScoreboardFile => $scoreboard_file,
4634 SystemLog => $log_file,
4635
4636 AuthUserFile => $auth_user_file,
4637 AuthGroupFile => $auth_group_file,
4638 AuthOrder => 'mod_auth_file.c',
4639
4640 IfModules => {
4641 'mod_delay.c' => {
4642 DelayEngine => 'off',
4643 },
4644
4645 'mod_snmp.c' => {
4646 SNMPAgent => "master 127.0.0.1:$agent_port",
4647 SNMPCommunity => $snmp_community,
4648 SNMPEngine => 'on',
4649 SNMPLog => $log_file,
4650 SNMPTables => $table_dir,
4651 },
4652 },
4653 };
4654
4655 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4656
4657 # Open pipes, for use between the parent and child processes. Specifically,
4658 # the child will indicate when it's done with its test by writing a message
4659 # to the parent.
4660 my ($rfh, $wfh);
4661 unless (pipe($rfh, $wfh)) {
4662 die("Can't open pipe: $!");
4663 }
4664
4665 require Net::SNMP;
4666 require Net::SNMP::Message;
4667
4668 my $ex;
4669
4670 # Fork child
4671 $self->handle_sigchld();
4672 defined(my $pid = fork()) or die("Can't fork: $!");
4673 if ($pid) {
4674 eval {
4675 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
4676 -hostname => '127.0.0.1',
4677 -port => $agent_port,
4678 -version => 'snmpv1',
4679 -community => $snmp_community,
4680 -retries => 1,
4681 -timeout => 3,
4682 -translate => 1,
4683 );
4684 unless ($snmp_sess) {
4685 die("Unable to create Net::SNMP session: $snmp_err");
4686 }
4687
4688 if ($ENV{TEST_VERBOSE}) {
4689 # From the Net::SNMP debug perldocs
4690 my $debug_mask = (0x02|0x10|0x20);
4691 $snmp_sess->debug($debug_mask);
4692 }
4693
4694 my $snmp_resp = $snmp_sess->trap(
4695 -varbindList => [ $request_oid, Net::SNMP::Message::OCTET_STRING(),
4696 'mod_smp' ],
4697 );
4698 if ($snmp_resp) {
4699 die("SNMP response received unexpectedly");
4700 }
4701
4702 my $err = $snmp_sess->error();
4703 my $expected = 'Received noSuchName(2) error-status at error-index 1';
4704
4705 $self->assert($expected eq $err,
4706 test_msg("Expected error '$expected' for OID, got '$err'"));
4707
4708 $snmp_sess->close();
4709 $snmp_sess = undef;
4710 };
4711
4712 if ($@) {
4713 $ex = $@;
4714 }
4715
4716 $wfh->print("done\n");
4717 $wfh->flush();
4718
4719 } else {
4720 eval { server_wait($config_file, $rfh, 10) };
4721 if ($@) {
4722 warn($@);
4723 exit 1;
4724 }
4725
4726 exit 0;
4727 }
4728
4729 # Stop server
4730 server_stop($pid_file);
4731
4732 $self->assert_child_ok($pid);
4733
4734 if ($ex) {
4735 test_append_logfile($log_file, $ex);
4736 unlink($log_file);
4737
4738 die($ex);
4739 }
4740
4741 unlink($log_file);
4742 }
4743
4744 sub snmp_v2_get_unknown {
4745 my $self = shift;
4746 my $tmpdir = $self->{tmpdir};
4747
4748 my $config_file = "$tmpdir/snmp.conf";
4749 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
4750 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
4751
4752 my $log_file = test_get_logfile();
4753
4754 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
4755 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
4756
4757 my $user = 'proftpd';
4758 my $passwd = 'test';
4759 my $group = 'ftpd';
4760 my $home_dir = File::Spec->rel2abs($tmpdir);
4761 my $uid = 500;
4762 my $gid = 500;
4763
4764 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
4765
4766 # Make sure that, if we're running as root, that the home directory has
4767 # permissions/privs set for the account we create
4768 if ($< == 0) {
4769 unless (chmod(0755, $home_dir, $table_dir)) {
4770 die("Can't set perms on $home_dir to 0755: $!");
4771 }
4772
4773 unless (chown($uid, $gid, $home_dir, $table_dir)) {
4774 die("Can't set owner of $home_dir to $uid/$gid: $!");
4775 }
4776 }
4777
4778 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4779 '/bin/bash');
4780 auth_group_write($auth_group_file, $group, $gid, $user);
4781
4782 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
4783 my $snmp_community = "public";
4784
4785 my $request_oid = '1.3.6.1.4.1.17852.1.0';
4786
4787 my $config = {
4788 TraceLog => $log_file,
4789 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
4790 PidFile => $pid_file,
4791 ScoreboardFile => $scoreboard_file,
4792 SystemLog => $log_file,
4793
4794 AuthUserFile => $auth_user_file,
4795 AuthGroupFile => $auth_group_file,
4796 AuthOrder => 'mod_auth_file.c',
4797
4798 IfModules => {
4799 'mod_delay.c' => {
4800 DelayEngine => 'off',
4801 },
4802
4803 'mod_snmp.c' => {
4804 SNMPAgent => "master 127.0.0.1:$agent_port",
4805 SNMPCommunity => $snmp_community,
4806 SNMPEngine => 'on',
4807 SNMPLog => $log_file,
4808 SNMPTables => $table_dir,
4809 },
4810 },
4811 };
4812
4813 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4814
4815 # Open pipes, for use between the parent and child processes. Specifically,
4816 # the child will indicate when it's done with its test by writing a message
4817 # to the parent.
4818 my ($rfh, $wfh);
4819 unless (pipe($rfh, $wfh)) {
4820 die("Can't open pipe: $!");
4821 }
4822
4823 require Net::SNMP;
4824
4825 my $ex;
4826
4827 # Fork child
4828 $self->handle_sigchld();
4829 defined(my $pid = fork()) or die("Can't fork: $!");
4830 if ($pid) {
4831 eval {
4832 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
4833 -hostname => '127.0.0.1',
4834 -port => $agent_port,
4835 -version => 'snmpv2c',
4836 -community => $snmp_community,
4837 -retries => 1,
4838 -timeout => 3,
4839 -translate => 1,
4840 );
4841 unless ($snmp_sess) {
4842 die("Unable to create Net::SNMP session: $snmp_err");
4843 }
4844
4845 if ($ENV{TEST_VERBOSE}) {
4846 # From the Net::SNMP debug perldocs
4847 my $debug_mask = (0x02|0x10|0x20);
4848 $snmp_sess->debug($debug_mask);
4849 }
4850
4851 my $oids = [$request_oid];
4852
4853 my $snmp_resp = $snmp_sess->get_request(
4854 -varbindList => $oids,
4855 );
4856 unless ($snmp_resp) {
4857 die("No SNMP response received: " . $snmp_sess->error());
4858 }
4859
4860 # Do we have the requested OID in the response?
4861 unless (defined($snmp_resp->{$request_oid})) {
4862 die("Missing required OID $request_oid in response");
4863 }
4864
4865 my $value = $snmp_resp->{$request_oid};
4866
4867 if ($ENV{TEST_VERBOSE}) {
4868 print STDERR "Requested OID $request_oid = $value\n";
4869 }
4870
4871 my $expected = 'noSuchObject';
4872
4873 $self->assert($expected eq $value,
4874 test_msg("Expected value $expected for OID, got $value"));
4875
4876 $snmp_sess->close();
4877 $snmp_sess = undef;
4878 };
4879
4880 if ($@) {
4881 $ex = $@;
4882 }
4883
4884 $wfh->print("done\n");
4885 $wfh->flush();
4886
4887 } else {
4888 eval { server_wait($config_file, $rfh) };
4889 if ($@) {
4890 warn($@);
4891 exit 1;
4892 }
4893
4894 exit 0;
4895 }
4896
4897 # Stop server
4898 server_stop($pid_file);
4899
4900 $self->assert_child_ok($pid);
4901
4902 if ($ex) {
4903 test_append_logfile($log_file, $ex);
4904 unlink($log_file);
4905
4906 die($ex);
4907 }
4908
4909 unlink($log_file);
4910 }
4911
4912 sub snmp_v2_get_missing_instance_id {
4913 my $self = shift;
4914 my $tmpdir = $self->{tmpdir};
4915
4916 my $config_file = "$tmpdir/snmp.conf";
4917 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
4918 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
4919
4920 my $log_file = test_get_logfile();
4921
4922 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
4923 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
4924
4925 my $user = 'proftpd';
4926 my $passwd = 'test';
4927 my $group = 'ftpd';
4928 my $home_dir = File::Spec->rel2abs($tmpdir);
4929 my $uid = 500;
4930 my $gid = 500;
4931
4932 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
4933
4934 # Make sure that, if we're running as root, that the home directory has
4935 # permissions/privs set for the account we create
4936 if ($< == 0) {
4937 unless (chmod(0755, $home_dir, $table_dir)) {
4938 die("Can't set perms on $home_dir to 0755: $!");
4939 }
4940
4941 unless (chown($uid, $gid, $home_dir, $table_dir)) {
4942 die("Can't set owner of $home_dir to $uid/$gid: $!");
4943 }
4944 }
4945
4946 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
4947 '/bin/bash');
4948 auth_group_write($auth_group_file, $group, $gid, $user);
4949
4950 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
4951 my $snmp_community = "public";
4952
4953 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.1';
4954
4955 my $config = {
4956 TraceLog => $log_file,
4957 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
4958 PidFile => $pid_file,
4959 ScoreboardFile => $scoreboard_file,
4960 SystemLog => $log_file,
4961
4962 AuthUserFile => $auth_user_file,
4963 AuthGroupFile => $auth_group_file,
4964 AuthOrder => 'mod_auth_file.c',
4965
4966 IfModules => {
4967 'mod_delay.c' => {
4968 DelayEngine => 'off',
4969 },
4970
4971 'mod_snmp.c' => {
4972 SNMPAgent => "master 127.0.0.1:$agent_port",
4973 SNMPCommunity => $snmp_community,
4974 SNMPEngine => 'on',
4975 SNMPLog => $log_file,
4976 SNMPTables => $table_dir,
4977 },
4978 },
4979 };
4980
4981 my ($port, $config_user, $config_group) = config_write($config_file, $config);
4982
4983 # Open pipes, for use between the parent and child processes. Specifically,
4984 # the child will indicate when it's done with its test by writing a message
4985 # to the parent.
4986 my ($rfh, $wfh);
4987 unless (pipe($rfh, $wfh)) {
4988 die("Can't open pipe: $!");
4989 }
4990
4991 require Net::SNMP;
4992
4993 my $ex;
4994
4995 # Fork child
4996 $self->handle_sigchld();
4997 defined(my $pid = fork()) or die("Can't fork: $!");
4998 if ($pid) {
4999 eval {
5000 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
5001 -hostname => '127.0.0.1',
5002 -port => $agent_port,
5003 -version => 'snmpv2c',
5004 -community => $snmp_community,
5005 -retries => 1,
5006 -timeout => 3,
5007 -translate => 1,
5008 );
5009 unless ($snmp_sess) {
5010 die("Unable to create Net::SNMP session: $snmp_err");
5011 }
5012
5013 if ($ENV{TEST_VERBOSE}) {
5014 # From the Net::SNMP debug perldocs
5015 my $debug_mask = (0x02|0x10|0x20);
5016 $snmp_sess->debug($debug_mask);
5017 }
5018
5019 my $oids = [$request_oid];
5020
5021 my $snmp_resp = $snmp_sess->get_request(
5022 -varbindList => $oids,
5023 );
5024 unless ($snmp_resp) {
5025 die("No SNMP response received: " . $snmp_sess->error());
5026 }
5027
5028 # Do we have the requested OID in the response?
5029 unless (defined($snmp_resp->{$request_oid})) {
5030 die("Missing required OID $request_oid in response");
5031 }
5032
5033 my $value = $snmp_resp->{$request_oid};
5034
5035 if ($ENV{TEST_VERBOSE}) {
5036 print STDERR "Requested OID $request_oid = $value\n";
5037 }
5038
5039 my $expected = 'noSuchInstance';
5040
5041 $self->assert($expected eq $value,
5042 test_msg("Expected value '$expected' for OID, got '$value'"));
5043
5044 $snmp_sess->close();
5045 $snmp_sess = undef;
5046 };
5047
5048 if ($@) {
5049 $ex = $@;
5050 }
5051
5052 $wfh->print("done\n");
5053 $wfh->flush();
5054
5055 } else {
5056 eval { server_wait($config_file, $rfh) };
5057 if ($@) {
5058 warn($@);
5059 exit 1;
5060 }
5061
5062 exit 0;
5063 }
5064
5065 # Stop server
5066 server_stop($pid_file);
5067
5068 $self->assert_child_ok($pid);
5069
5070 if ($ex) {
5071 test_append_logfile($log_file, $ex);
5072 unlink($log_file);
5073
5074 die($ex);
5075 }
5076
5077 unlink($log_file);
5078 }
5079
5080 sub snmp_v2_get_next_end_of_mib_view {
5081 my $self = shift;
5082 my $tmpdir = $self->{tmpdir};
5083 my $setup = test_setup($tmpdir, 'snmp');
5084
5085 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
5086
5087 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
5088 my $snmp_community = "public";
5089
5090 # The "last" OID in the entire arc depends on the modules loaded.
5091 my $request_oid = '1.3.6.1.4.1.17852.2.2.4.5.0';
5092 if (feature_have_module_compiled('mod_tls.c')) {
5093 # End of the proftpd.snmpModule.ftps arc
5094 $request_oid = '1.3.6.1.4.1.17852.2.2.5.3.11.0';
5095 }
5096 if (feature_have_module_compiled('mod_sftp.c')) {
5097 # End of the proftpd.snmpModule.scp arc
5098 $request_oid = '1.3.6.1.4.1.17852.2.2.8.2.8.0';
5099 }
5100 if (feature_have_module_compiled('mod_ban.c')) {
5101 # End of the proftpd.snmpModule.ban arc
5102 $request_oid = '1.3.6.1.4.1.17852.2.2.9.2.8.0';
5103 }
5104
5105 my $config = {
5106 PidFile => $setup->{pid_file},
5107 ScoreboardFile => $setup->{scoreboard_file},
5108 SystemLog => $setup->{log_file},
5109 TraceLog => $setup->{log_file},
5110 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
5111
5112 AuthUserFile => $setup->{auth_user_file},
5113 AuthGroupFile => $setup->{auth_group_file},
5114 AuthOrder => 'mod_auth_file.c',
5115
5116 IfModules => {
5117 'mod_delay.c' => {
5118 DelayEngine => 'off',
5119 },
5120
5121 'mod_snmp.c' => {
5122 SNMPAgent => "master 127.0.0.1:$agent_port",
5123 SNMPCommunity => $snmp_community,
5124 SNMPEngine => 'on',
5125 SNMPLog => $setup->{log_file},
5126 SNMPTables => $table_dir,
5127 },
5128 },
5129 };
5130
5131 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
5132 $config);
5133
5134 # Open pipes, for use between the parent and child processes. Specifically,
5135 # the child will indicate when it's done with its test by writing a message
5136 # to the parent.
5137 my ($rfh, $wfh);
5138 unless (pipe($rfh, $wfh)) {
5139 die("Can't open pipe: $!");
5140 }
5141
5142 require Net::SNMP;
5143
5144 my $ex;
5145
5146 # Fork child
5147 $self->handle_sigchld();
5148 defined(my $pid = fork()) or die("Can't fork: $!");
5149 if ($pid) {
5150 eval {
5151 # Allow server to start up
5152 sleep(1);
5153
5154 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
5155 -hostname => '127.0.0.1',
5156 -port => $agent_port,
5157 -version => 'snmpv2c',
5158 -community => $snmp_community,
5159 -retries => 1,
5160 -timeout => 3,
5161 -translate => 1,
5162 );
5163 unless ($snmp_sess) {
5164 die("Unable to create Net::SNMP session: $snmp_err");
5165 }
5166
5167 if ($ENV{TEST_VERBOSE}) {
5168 # From the Net::SNMP debug perldocs
5169 my $debug_mask = (0x02|0x10|0x20);
5170 $snmp_sess->debug($debug_mask);
5171 }
5172
5173 my $oids = [$request_oid];
5174
5175 my $snmp_resp = $snmp_sess->get_next_request(
5176 -varbindList => $oids,
5177 );
5178 unless ($snmp_resp) {
5179 die("No SNMP response received: " . $snmp_sess->error());
5180 }
5181
5182 # Do we have the requested OID in the response?
5183 unless (defined($snmp_resp->{$request_oid})) {
5184 die("Missing required OID $request_oid in response");
5185 }
5186
5187 my $value = $snmp_resp->{$request_oid};
5188
5189 if ($ENV{TEST_VERBOSE}) {
5190 print STDERR "Requested OID $request_oid = $value\n";
5191 }
5192
5193 my $expected = 'endOfMibView';
5194
5195 $self->assert($expected eq $value,
5196 test_msg("Expected value '$expected' for OID, got '$value'"));
5197
5198 $snmp_sess->close();
5199 $snmp_sess = undef;
5200 };
5201
5202 if ($@) {
5203 $ex = $@;
5204 }
5205
5206 $wfh->print("done\n");
5207 $wfh->flush();
5208
5209 } else {
5210 eval { server_wait($setup->{config_file}, $rfh) };
5211 if ($@) {
5212 warn($@);
5213 exit 1;
5214 }
5215
5216 exit 0;
5217 }
5218
5219 # Stop server
5220 server_stop($setup->{pid_file});
5221 $self->assert_child_ok($pid);
5222
5223 test_cleanup($setup->{log_file}, $ex);
5224 }
5225
5226 sub snmp_v2_get_bulk {
5227 my $self = shift;
5228 my $tmpdir = $self->{tmpdir};
5229
5230 my $config_file = "$tmpdir/snmp.conf";
5231 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
5232 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
5233
5234 my $log_file = test_get_logfile();
5235
5236 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
5237 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
5238
5239 my $user = 'proftpd';
5240 my $passwd = 'test';
5241 my $group = 'ftpd';
5242 my $home_dir = File::Spec->rel2abs($tmpdir);
5243 my $uid = 500;
5244 my $gid = 500;
5245
5246 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
5247
5248 # Make sure that, if we're running as root, that the home directory has
5249 # permissions/privs set for the account we create
5250 if ($< == 0) {
5251 unless (chmod(0755, $home_dir, $table_dir)) {
5252 die("Can't set perms on $home_dir to 0755: $!");
5253 }
5254
5255 unless (chown($uid, $gid, $home_dir, $table_dir)) {
5256 die("Can't set owner of $home_dir to $uid/$gid: $!");
5257 }
5258 }
5259
5260 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
5261 '/bin/bash');
5262 auth_group_write($auth_group_file, $group, $gid, $user);
5263
5264 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
5265 my $snmp_community = "public";
5266
5267 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.2.0';
5268 my $next_oid = '1.3.6.1.4.1.17852.2.2.1.3.0';
5269
5270 my $config = {
5271 TraceLog => $log_file,
5272 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
5273 PidFile => $pid_file,
5274 ScoreboardFile => $scoreboard_file,
5275 SystemLog => $log_file,
5276
5277 AuthUserFile => $auth_user_file,
5278 AuthGroupFile => $auth_group_file,
5279 AuthOrder => 'mod_auth_file.c',
5280
5281 IfModules => {
5282 'mod_delay.c' => {
5283 DelayEngine => 'off',
5284 },
5285
5286 'mod_snmp.c' => {
5287 SNMPAgent => "master 127.0.0.1:$agent_port",
5288 SNMPCommunity => $snmp_community,
5289 SNMPEngine => 'on',
5290 SNMPLog => $log_file,
5291 SNMPTables => $table_dir,
5292 },
5293 },
5294 };
5295
5296 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5297
5298 # Open pipes, for use between the parent and child processes. Specifically,
5299 # the child will indicate when it's done with its test by writing a message
5300 # to the parent.
5301 my ($rfh, $wfh);
5302 unless (pipe($rfh, $wfh)) {
5303 die("Can't open pipe: $!");
5304 }
5305
5306 require Net::SNMP;
5307
5308 my $ex;
5309
5310 # Fork child
5311 $self->handle_sigchld();
5312 defined(my $pid = fork()) or die("Can't fork: $!");
5313 if ($pid) {
5314 eval {
5315 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
5316 -hostname => '127.0.0.1',
5317 -port => $agent_port,
5318 -version => 'snmpv2c',
5319 -community => $snmp_community,
5320 -retries => 1,
5321 -timeout => 3,
5322 -translate => 1,
5323 );
5324 unless ($snmp_sess) {
5325 die("Unable to create Net::SNMP session: $snmp_err");
5326 }
5327
5328 if ($ENV{TEST_VERBOSE}) {
5329 # From the Net::SNMP debug perldocs
5330 my $debug_mask = (0x02|0x10|0x20);
5331 $snmp_sess->debug($debug_mask);
5332 }
5333
5334 my $oids = [$request_oid];
5335
5336 my $snmp_resp = $snmp_sess->get_bulk_request(
5337 -nonrepeaters => 1,
5338 -varbindList => $oids,
5339 );
5340 unless ($snmp_resp) {
5341 die("No SNMP response received: " . $snmp_sess->error());
5342 }
5343
5344 # Do we have the next OID of the requested OID in the response?
5345 unless (defined($snmp_resp->{$next_oid})) {
5346 die("Missing required OID $next_oid in response");
5347 }
5348
5349 my $value = $snmp_resp->{$next_oid};
5350
5351 if ($ENV{TEST_VERBOSE}) {
5352 print STDERR "Requested OID $next_oid = $value\n";
5353 }
5354
5355 # The default proftpd string, without ServerAdmin configured.
5356 my $expected = '[email protected]';
5357
5358 $self->assert(qr/$expected/, $value,
5359 test_msg("Expected value '$expected' for OID, got '$value'"));
5360
5361 $snmp_sess->close();
5362 $snmp_sess = undef;
5363 };
5364
5365 if ($@) {
5366 $ex = $@;
5367 }
5368
5369 $wfh->print("done\n");
5370 $wfh->flush();
5371
5372 } else {
5373 eval { server_wait($config_file, $rfh) };
5374 if ($@) {
5375 warn($@);
5376 exit 1;
5377 }
5378
5379 exit 0;
5380 }
5381
5382 # Stop server
5383 server_stop($pid_file);
5384
5385 $self->assert_child_ok($pid);
5386
5387 if ($ex) {
5388 test_append_logfile($log_file, $ex);
5389 unlink($log_file);
5390
5391 die($ex);
5392 }
5393
5394 unlink($log_file);
5395 }
5396
5397 sub snmp_v2_get_bulk_max_repetitions_only {
5398 my $self = shift;
5399 my $tmpdir = $self->{tmpdir};
5400
5401 my $config_file = "$tmpdir/snmp.conf";
5402 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
5403 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
5404
5405 my $log_file = test_get_logfile();
5406
5407 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
5408 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
5409
5410 my $user = 'proftpd';
5411 my $passwd = 'test';
5412 my $group = 'ftpd';
5413 my $home_dir = File::Spec->rel2abs($tmpdir);
5414 my $uid = 500;
5415 my $gid = 500;
5416
5417 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
5418
5419 # Make sure that, if we're running as root, that the home directory has
5420 # permissions/privs set for the account we create
5421 if ($< == 0) {
5422 unless (chmod(0755, $home_dir, $table_dir)) {
5423 die("Can't set perms on $home_dir to 0755: $!");
5424 }
5425
5426 unless (chown($uid, $gid, $home_dir, $table_dir)) {
5427 die("Can't set owner of $home_dir to $uid/$gid: $!");
5428 }
5429 }
5430
5431 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
5432 '/bin/bash');
5433 auth_group_write($auth_group_file, $group, $gid, $user);
5434
5435 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
5436 my $snmp_community = "public";
5437
5438 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.2.0';
5439
5440 my $next_oids = {
5441 '1.3.6.1.4.1.17852.2.2.1.3.0' => '[email protected]',
5442 '1.3.6.1.4.1.17852.2.2.1.4.0' => '\d+\.\d+',
5443 };
5444
5445 my $config = {
5446 TraceLog => $log_file,
5447 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
5448 PidFile => $pid_file,
5449 ScoreboardFile => $scoreboard_file,
5450 SystemLog => $log_file,
5451
5452 AuthUserFile => $auth_user_file,
5453 AuthGroupFile => $auth_group_file,
5454 AuthOrder => 'mod_auth_file.c',
5455
5456 IfModules => {
5457 'mod_delay.c' => {
5458 DelayEngine => 'off',
5459 },
5460
5461 'mod_snmp.c' => {
5462 SNMPAgent => "master 127.0.0.1:$agent_port",
5463 SNMPCommunity => $snmp_community,
5464 SNMPEngine => 'on',
5465 SNMPLog => $log_file,
5466 SNMPTables => $table_dir,
5467 },
5468 },
5469 };
5470
5471 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5472
5473 # Open pipes, for use between the parent and child processes. Specifically,
5474 # the child will indicate when it's done with its test by writing a message
5475 # to the parent.
5476 my ($rfh, $wfh);
5477 unless (pipe($rfh, $wfh)) {
5478 die("Can't open pipe: $!");
5479 }
5480
5481 require Net::SNMP;
5482
5483 my $ex;
5484
5485 # Fork child
5486 $self->handle_sigchld();
5487 defined(my $pid = fork()) or die("Can't fork: $!");
5488 if ($pid) {
5489 eval {
5490 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
5491 -hostname => '127.0.0.1',
5492 -port => $agent_port,
5493 -version => 'snmpv2c',
5494 -community => $snmp_community,
5495 -retries => 1,
5496 -timeout => 3,
5497 -translate => 1,
5498 );
5499 unless ($snmp_sess) {
5500 die("Unable to create Net::SNMP session: $snmp_err");
5501 }
5502
5503 if ($ENV{TEST_VERBOSE}) {
5504 # From the Net::SNMP debug perldocs
5505 my $debug_mask = (0x02|0x10|0x20);
5506 $snmp_sess->debug($debug_mask);
5507 }
5508
5509 my $oids = [$request_oid];
5510
5511 my $snmp_resp = $snmp_sess->get_bulk_request(
5512 -maxrepetitions => 2,
5513 -varbindList => $oids,
5514 );
5515 unless ($snmp_resp) {
5516 die("No SNMP response received: " . $snmp_sess->error());
5517 }
5518
5519 foreach my $next_oid (keys(%$next_oids)) {
5520 # Do we have the next OID of the requested OID in the response?
5521 unless (defined($snmp_resp->{$next_oid})) {
5522 die("Missing required OID $next_oid in response");
5523 }
5524
5525 my $value = $snmp_resp->{$next_oid};
5526
5527 if ($ENV{TEST_VERBOSE}) {
5528 print STDERR "Requested OID $next_oid = $value\n";
5529 }
5530
5531 my $expected = $next_oids->{$next_oid};
5532
5533 $self->assert(qr/$expected/, $value,
5534 test_msg("Expected value '$expected' for OID, got '$value'"));
5535 }
5536
5537 $snmp_sess->close();
5538 $snmp_sess = undef;
5539 };
5540
5541 if ($@) {
5542 $ex = $@;
5543 }
5544
5545 $wfh->print("done\n");
5546 $wfh->flush();
5547
5548 } else {
5549 eval { server_wait($config_file, $rfh) };
5550 if ($@) {
5551 warn($@);
5552 exit 1;
5553 }
5554
5555 exit 0;
5556 }
5557
5558 # Stop server
5559 server_stop($pid_file);
5560
5561 $self->assert_child_ok($pid);
5562
5563 if ($ex) {
5564 test_append_logfile($log_file, $ex);
5565 unlink($log_file);
5566
5567 die($ex);
5568 }
5569
5570 unlink($log_file);
5571 }
5572
5573 sub snmp_v2_get_bulk_end_of_mib_view {
5574 my $self = shift;
5575 my $tmpdir = $self->{tmpdir};
5576 my $setup = test_setup($tmpdir, 'snmp');
5577
5578 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
5579
5580 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
5581 my $snmp_community = "public";
5582
5583 # These OIDs to request depend on the modules loaded.
5584 my $request_oid = '1.3.6.1.4.1.17852.2.2.4.3.0';
5585
5586 my $next_oids = {
5587 '1.3.6.1.4.1.17852.2.2.4.4.0' => '\d+',
5588 '1.3.6.1.4.1.17852.2.2.4.5.0' => '\d+',
5589 '1.3.6.1.4.1.17852.2.2.4.5.0 ' => 'endOfMibView',
5590 };
5591
5592 if (feature_have_module_compiled('mod_tls.c')) {
5593 $request_oid = '1.3.6.1.4.1.17852.2.2.5.3.9.0';
5594 $next_oids = {
5595 '1.3.6.1.4.1.17852.2.2.5.3.10.0' => '\d+',
5596 '1.3.6.1.4.1.17852.2.2.5.3.11.0' => '\d+',
5597 '1.3.6.1.4.1.17852.2.2.5.3.11.0 ' => 'endOfMibView',
5598 };
5599 }
5600
5601 if (feature_have_module_compiled('mod_sftp.c')) {
5602 $request_oid = '1.3.6.1.4.1.17852.2.2.8.2.6.0';
5603 $next_oids = {
5604 '1.3.6.1.4.1.17852.2.2.8.2.7.0' => '\d+',
5605 '1.3.6.1.4.1.17852.2.2.8.2.8.0' => '\d+',
5606 '1.3.6.1.4.1.17852.2.2.8.2.8.0 ' => 'endOfMibView',
5607 };
5608 }
5609
5610 if (feature_have_module_compiled('mod_ban.c')) {
5611 $request_oid = '1.3.6.1.4.1.17852.2.2.9.2.6.0';
5612 $next_oids = {
5613 '1.3.6.1.4.1.17852.2.2.9.2.7.0' => '\d+',
5614 '1.3.6.1.4.1.17852.2.2.9.2.8.0' => '\d+',
5615 '1.3.6.1.4.1.17852.2.2.9.2.8.0 ' => 'endOfMibView',
5616 };
5617 }
5618
5619 my $config = {
5620 PidFile => $setup->{pid_file},
5621 ScoreboardFile => $setup->{scoreboard_file},
5622 SystemLog => $setup->{log_file},
5623 TraceLog => $setup->{log_file},
5624 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
5625
5626 AuthUserFile => $setup->{auth_user_file},
5627 AuthGroupFile => $setup->{auth_group_file},
5628 AuthOrder => 'mod_auth_file.c',
5629
5630 IfModules => {
5631 'mod_delay.c' => {
5632 DelayEngine => 'off',
5633 },
5634
5635 'mod_snmp.c' => {
5636 SNMPAgent => "master 127.0.0.1:$agent_port",
5637 SNMPCommunity => $snmp_community,
5638 SNMPEngine => 'on',
5639 SNMPLog => $setup->{log_file},
5640 SNMPTables => $table_dir,
5641 },
5642 },
5643 };
5644
5645 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
5646 $config);
5647
5648 # Open pipes, for use between the parent and child processes. Specifically,
5649 # the child will indicate when it's done with its test by writing a message
5650 # to the parent.
5651 my ($rfh, $wfh);
5652 unless (pipe($rfh, $wfh)) {
5653 die("Can't open pipe: $!");
5654 }
5655
5656 require Net::SNMP;
5657
5658 my $ex;
5659
5660 # Fork child
5661 $self->handle_sigchld();
5662 defined(my $pid = fork()) or die("Can't fork: $!");
5663 if ($pid) {
5664 eval {
5665 # Allow the server to start up
5666 sleep(1);
5667
5668 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
5669 -hostname => '127.0.0.1',
5670 -port => $agent_port,
5671 -version => 'snmpv2c',
5672 -community => $snmp_community,
5673 -retries => 1,
5674 -timeout => 3,
5675 -translate => 1,
5676 );
5677 unless ($snmp_sess) {
5678 die("Unable to create Net::SNMP session: $snmp_err");
5679 }
5680
5681 if ($ENV{TEST_VERBOSE}) {
5682 # From the Net::SNMP debug perldocs
5683 my $debug_mask = (0x02|0x10|0x20);
5684 $snmp_sess->debug($debug_mask);
5685 }
5686
5687 my $oids = [$request_oid];
5688
5689 my $snmp_resp = $snmp_sess->get_bulk_request(
5690 -maxrepetitions => 5,
5691 -varbindList => $oids,
5692 );
5693 unless ($snmp_resp) {
5694 die("No SNMP response received: " . $snmp_sess->error());
5695 }
5696
5697 if ($ENV{TEST_VERBOSE}) {
5698 use Data::Dumper;
5699 print STDERR "# SNMP response: ", Dumper($snmp_resp), "\n";
5700 }
5701
5702 foreach my $next_oid (keys(%$next_oids)) {
5703 # Do we have the next OID of the requested OID in the response?
5704 unless (defined($snmp_resp->{$next_oid})) {
5705 die("Missing required OID $next_oid in response");
5706 }
5707
5708 my $value = $snmp_resp->{$next_oid};
5709
5710 if ($ENV{TEST_VERBOSE}) {
5711 print STDERR "Requested OID $next_oid = $value\n";
5712 }
5713
5714 my $expected = $next_oids->{$next_oid};
5715
5716 $self->assert(qr/$expected/, $value,
5717 test_msg("Expected value '$expected' for OID, got '$value'"));
5718 }
5719
5720 $snmp_sess->close();
5721 $snmp_sess = undef;
5722 };
5723
5724 if ($@) {
5725 $ex = $@;
5726 }
5727
5728 $wfh->print("done\n");
5729 $wfh->flush();
5730
5731 } else {
5732 eval { server_wait($setup->{config_file}, $rfh) };
5733 if ($@) {
5734 warn($@);
5735 exit 1;
5736 }
5737
5738 exit 0;
5739 }
5740
5741 # Stop server
5742 server_stop($setup->{pid_file});
5743 $self->assert_child_ok($pid);
5744
5745 test_cleanup($setup->{log_file}, $ex);
5746 }
5747
5748 sub snmp_v2_set_no_access {
5749 my $self = shift;
5750 my $tmpdir = $self->{tmpdir};
5751
5752 my $config_file = "$tmpdir/snmp.conf";
5753 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
5754 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
5755
5756 my $log_file = test_get_logfile();
5757
5758 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
5759 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
5760
5761 my $user = 'proftpd';
5762 my $passwd = 'test';
5763 my $group = 'ftpd';
5764 my $home_dir = File::Spec->rel2abs($tmpdir);
5765 my $uid = 500;
5766 my $gid = 500;
5767
5768 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
5769
5770 # Make sure that, if we're running as root, that the home directory has
5771 # permissions/privs set for the account we create
5772 if ($< == 0) {
5773 unless (chmod(0755, $home_dir, $table_dir)) {
5774 die("Can't set perms on $home_dir to 0755: $!");
5775 }
5776
5777 unless (chown($uid, $gid, $home_dir, $table_dir)) {
5778 die("Can't set owner of $home_dir to $uid/$gid: $!");
5779 }
5780 }
5781
5782 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
5783 '/bin/bash');
5784 auth_group_write($auth_group_file, $group, $gid, $user);
5785
5786 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
5787 my $snmp_community = "public";
5788
5789 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.1.0';
5790
5791 my $config = {
5792 TraceLog => $log_file,
5793 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
5794 PidFile => $pid_file,
5795 ScoreboardFile => $scoreboard_file,
5796 SystemLog => $log_file,
5797
5798 AuthUserFile => $auth_user_file,
5799 AuthGroupFile => $auth_group_file,
5800 AuthOrder => 'mod_auth_file.c',
5801
5802 IfModules => {
5803 'mod_delay.c' => {
5804 DelayEngine => 'off',
5805 },
5806
5807 'mod_snmp.c' => {
5808 SNMPAgent => "master 127.0.0.1:$agent_port",
5809 SNMPCommunity => $snmp_community,
5810 SNMPEngine => 'on',
5811 SNMPLog => $log_file,
5812 SNMPTables => $table_dir,
5813 },
5814 },
5815 };
5816
5817 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5818
5819 # Open pipes, for use between the parent and child processes. Specifically,
5820 # the child will indicate when it's done with its test by writing a message
5821 # to the parent.
5822 my ($rfh, $wfh);
5823 unless (pipe($rfh, $wfh)) {
5824 die("Can't open pipe: $!");
5825 }
5826
5827 require Net::SNMP;
5828 require Net::SNMP::Message;
5829
5830 my $ex;
5831
5832 # Fork child
5833 $self->handle_sigchld();
5834 defined(my $pid = fork()) or die("Can't fork: $!");
5835 if ($pid) {
5836 eval {
5837 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
5838 -hostname => '127.0.0.1',
5839 -port => $agent_port,
5840 -version => 'snmpv2c',
5841 -community => $snmp_community,
5842 -retries => 1,
5843 -timeout => 3,
5844 -translate => 1,
5845 );
5846 unless ($snmp_sess) {
5847 die("Unable to create Net::SNMP session: $snmp_err");
5848 }
5849
5850 if ($ENV{TEST_VERBOSE}) {
5851 # From the Net::SNMP debug perldocs
5852 my $debug_mask = (0x02|0x10|0x20);
5853 $snmp_sess->debug($debug_mask);
5854 }
5855
5856 my $snmp_resp = $snmp_sess->set_request(
5857 -varbindList => [ $request_oid, Net::SNMP::Message::OCTET_STRING(),
5858 'mod_smp' ],
5859 );
5860 if ($snmp_resp) {
5861 die("SNMP response received unexpectedly");
5862 }
5863
5864 my $err = $snmp_sess->error();
5865 my $expected = 'Received noAccess(6) error-status at error-index 1';
5866
5867 $self->assert($expected eq $err,
5868 test_msg("Expected error '$expected' for OID, got '$err'"));
5869
5870 $snmp_sess->close();
5871 $snmp_sess = undef;
5872 };
5873
5874 if ($@) {
5875 $ex = $@;
5876 }
5877
5878 $wfh->print("done\n");
5879 $wfh->flush();
5880
5881 } else {
5882 eval { server_wait($config_file, $rfh, 10) };
5883 if ($@) {
5884 warn($@);
5885 exit 1;
5886 }
5887
5888 exit 0;
5889 }
5890
5891 # Stop server
5892 server_stop($pid_file);
5893
5894 $self->assert_child_ok($pid);
5895
5896 if ($ex) {
5897 test_append_logfile($log_file, $ex);
5898 unlink($log_file);
5899
5900 die($ex);
5901 }
5902
5903 unlink($log_file);
5904 }
5905
5906 sub snmp_config_limit {
5907 my $self = shift;
5908 my $tmpdir = $self->{tmpdir};
5909
5910 my $config_file = "$tmpdir/snmp.conf";
5911 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
5912 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
5913
5914 my $log_file = test_get_logfile();
5915
5916 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
5917 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
5918
5919 my $user = 'proftpd';
5920 my $passwd = 'test';
5921 my $group = 'ftpd';
5922 my $home_dir = File::Spec->rel2abs($tmpdir);
5923 my $uid = 500;
5924 my $gid = 500;
5925
5926 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
5927
5928 # Make sure that, if we're running as root, that the home directory has
5929 # permissions/privs set for the account we create
5930 if ($< == 0) {
5931 unless (chmod(0755, $home_dir, $table_dir)) {
5932 die("Can't set perms on $home_dir to 0755: $!");
5933 }
5934
5935 unless (chown($uid, $gid, $home_dir, $table_dir)) {
5936 die("Can't set owner of $home_dir to $uid/$gid: $!");
5937 }
5938 }
5939
5940 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
5941 '/bin/bash');
5942 auth_group_write($auth_group_file, $group, $gid, $user);
5943
5944 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
5945 my $snmp_community = "public";
5946
5947 my $request_oid = '1.3.6.1.4.1.17852.2.2.1.3.0';
5948
5949 my $config = {
5950 TraceLog => $log_file,
5951 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
5952 PidFile => $pid_file,
5953 ScoreboardFile => $scoreboard_file,
5954 SystemLog => $log_file,
5955
5956 AuthUserFile => $auth_user_file,
5957 AuthGroupFile => $auth_group_file,
5958 AuthOrder => 'mod_auth_file.c',
5959
5960 IfModules => {
5961 'mod_delay.c' => {
5962 DelayEngine => 'off',
5963 },
5964
5965 'mod_snmp.c' => {
5966 SNMPAgent => "master 127.0.0.1:$agent_port",
5967 SNMPCommunity => $snmp_community,
5968 SNMPEngine => 'on',
5969 SNMPLog => $log_file,
5970 SNMPTables => $table_dir,
5971 },
5972 },
5973
5974 Limit => {
5975 SNMP => {
5976 Deny => '127.0.0.1',
5977 },
5978 },
5979 };
5980
5981 my ($port, $config_user, $config_group) = config_write($config_file, $config);
5982
5983 # Open pipes, for use between the parent and child processes. Specifically,
5984 # the child will indicate when it's done with its test by writing a message
5985 # to the parent.
5986 my ($rfh, $wfh);
5987 unless (pipe($rfh, $wfh)) {
5988 die("Can't open pipe: $!");
5989 }
5990
5991 require Net::SNMP;
5992
5993 my $ex;
5994
5995 # Fork child
5996 $self->handle_sigchld();
5997 defined(my $pid = fork()) or die("Can't fork: $!");
5998 if ($pid) {
5999 eval {
6000 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
6001 -hostname => '127.0.0.1',
6002 -port => $agent_port,
6003 -version => 'snmpv1',
6004 -community => $snmp_community,
6005 -retries => 1,
6006 -timeout => 3,
6007 -translate => 1,
6008 );
6009 unless ($snmp_sess) {
6010 die("Unable to create Net::SNMP session: $snmp_err");
6011 }
6012
6013 if ($ENV{TEST_VERBOSE}) {
6014 # From the Net::SNMP debug perldocs
6015 my $debug_mask = (0x02|0x10|0x20);
6016 $snmp_sess->debug($debug_mask);
6017 }
6018
6019 my $oids = [$request_oid];
6020
6021 my $snmp_resp = $snmp_sess->get_request(
6022 -varbindList => $oids,
6023 );
6024 if ($snmp_resp) {
6025 die("SNMP response received unexpectedly");
6026 }
6027
6028 my $err = $snmp_sess->error();
6029 my $expected = '^No response from remote host';
6030
6031 $self->assert(qr/$expected/, $err,
6032 test_msg("Expected error '$expected', got '$err'"));
6033
6034 $snmp_sess->close();
6035 $snmp_sess = undef;
6036 };
6037
6038 if ($@) {
6039 $ex = $@;
6040 }
6041
6042 $wfh->print("done\n");
6043 $wfh->flush();
6044
6045 } else {
6046 eval { server_wait($config_file, $rfh) };
6047 if ($@) {
6048 warn($@);
6049 exit 1;
6050 }
6051
6052 exit 0;
6053 }
6054
6055 # Stop server
6056 server_stop($pid_file);
6057
6058 $self->assert_child_ok($pid);
6059
6060 if ($ex) {
6061 test_append_logfile($log_file, $ex);
6062 unlink($log_file);
6063
6064 die($ex);
6065 }
6066
6067 unlink($log_file);
6068 }
6069
6070 sub snmp_config_max_variables {
6071 my $self = shift;
6072 my $tmpdir = $self->{tmpdir};
6073
6074 my $config_file = "$tmpdir/snmp.conf";
6075 my $pid_file = File::Spec->rel2abs("$tmpdir/snmp.pid");
6076 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/snmp.scoreboard");
6077
6078 my $log_file = test_get_logfile();
6079
6080 my $auth_user_file = File::Spec->rel2abs("$tmpdir/snmp.passwd");
6081 my $auth_group_file = File::Spec->rel2abs("$tmpdir/snmp.group");
6082
6083 my $user = 'proftpd';
6084 my $passwd = 'test';
6085 my $group = 'ftpd';
6086 my $home_dir = File::Spec->rel2abs($tmpdir);
6087 my $uid = 500;
6088 my $gid = 500;
6089
6090 my $table_dir = File::Spec->rel2abs("$tmpdir/var/snmp");
6091
6092 # Make sure that, if we're running as root, that the home directory has
6093 # permissions/privs set for the account we create
6094 if ($< == 0) {
6095 unless (chmod(0755, $home_dir, $table_dir)) {
6096 die("Can't set perms on $home_dir to 0755: $!");
6097 }
6098
6099 unless (chown($uid, $gid, $home_dir, $table_dir)) {
6100 die("Can't set owner of $home_dir to $uid/$gid: $!");
6101 }
6102 }
6103
6104 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
6105 '/bin/bash');
6106 auth_group_write($auth_group_file, $group, $gid, $user);
6107
6108 my $agent_port = ProFTPD::TestSuite::Utils::get_high_numbered_port();
6109 my $snmp_community = "public";
6110
6111 my $request_oids = [
6112 '1.3.6.1.4.1.17852.2.2.1.1.0',
6113 '1.3.6.1.4.1.17852.2.2.1.3.0',
6114 ];
6115
6116 my $expected_oid_values = {
6117 '1.3.6.1.4.1.17852.2.2.1.1.0' => 'proftpd',
6118 '1.3.6.1.4.1.17852.2.2.1.3.0' => '[email protected]',
6119 };
6120
6121 my $config = {
6122 TraceLog => $log_file,
6123 Trace => 'snmp:20 snmp.asn1:20 snmp.db:20 snmp.msg:20 snmp.pdu:20 snmp.smi:20',
6124 PidFile => $pid_file,
6125 ScoreboardFile => $scoreboard_file,
6126 SystemLog => $log_file,
6127
6128 AuthUserFile => $auth_user_file,
6129 AuthGroupFile => $auth_group_file,
6130 AuthOrder => 'mod_auth_file.c',
6131
6132 IfModules => {
6133 'mod_delay.c' => {
6134 DelayEngine => 'off',
6135 },
6136
6137 'mod_snmp.c' => {
6138 SNMPAgent => "master 127.0.0.1:$agent_port",
6139 SNMPCommunity => $snmp_community,
6140 SNMPEngine => 'on',
6141 SNMPLog => $log_file,
6142 SNMPTables => $table_dir,
6143
6144 SNMPMaxVariables => 1,
6145 },
6146 },
6147 };
6148
6149 my ($port, $config_user, $config_group) = config_write($config_file, $config);
6150
6151 # Open pipes, for use between the parent and child processes. Specifically,
6152 # the child will indicate when it's done with its test by writing a message
6153 # to the parent.
6154 my ($rfh, $wfh);
6155 unless (pipe($rfh, $wfh)) {
6156 die("Can't open pipe: $!");
6157 }
6158
6159 require Net::SNMP;
6160
6161 my $ex;
6162
6163 # Fork child
6164 $self->handle_sigchld();
6165 defined(my $pid = fork()) or die("Can't fork: $!");
6166 if ($pid) {
6167 eval {
6168 my ($snmp_sess, $snmp_err) = Net::SNMP->session(
6169 -hostname => '127.0.0.1',
6170 -port => $agent_port,
6171 -version => 'snmpv1',
6172 -community => $snmp_community,
6173 -retries => 1,
6174 -timeout => 3,
6175 -translate => 1,
6176 );
6177 unless ($snmp_sess) {
6178 die("Unable to create Net::SNMP session: $snmp_err");
6179 }
6180
6181 if ($ENV{TEST_VERBOSE}) {
6182 # From the Net::SNMP debug perldocs
6183 my $debug_mask = (0x02|0x10|0x20);
6184 $snmp_sess->debug($debug_mask);
6185 }
6186
6187 my $snmp_resp = $snmp_sess->get_request(
6188 -varbindList => $request_oids,
6189 );
6190 if ($snmp_resp) {
6191 die("SNMP response received unexpectedly");
6192 }
6193
6194 my $err = $snmp_sess->error();
6195 my $expected = 'Received tooBig(1) error-status at error-index 0';
6196
6197 $self->assert($expected eq $err,
6198 test_msg("Expected error '$expected', got '$err'"));
6199
6200 $snmp_sess->close();
6201 $snmp_sess = undef;
6202 };
6203
6204 if ($@) {
6205 $ex = $@;
6206 }
6207
6208 $wfh->print("done\n");
6209 $wfh->flush();
6210
6211 } else {
6212 eval { server_wait($config_file, $rfh) };
6213 if ($@) {
6214 warn($@);
6215 exit 1;
6216 }
6217
6218 exit 0;
6219 }
6220
6221 # Stop server
6222 server_stop($pid_file);
6223
6224 $self->assert_child_ok($pid);
6225
6226 if ($ex) {
6227 test_append_logfile($log_file, $ex);
6228 unlink($log_file);
6229
6230 die($ex);
6231 }
6232
6233 unlink($log_file);
6234 }
6235
6236 1;