line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: perl -*- |
2
|
|
|
|
|
|
|
# ============================================================================ |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Net::SNMP::Security::Community; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# $Id: Community.pm,v 2.0 2009/09/09 15:05:33 dtown Rel $ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Object that implements the SNMPv1/v2c Community-based Security Model. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Copyright (c) 2001-2009 David M. Town |
11
|
|
|
|
|
|
|
# All rights reserved. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# This program is free software; you may redistribute it and/or modify it |
14
|
|
|
|
|
|
|
# under the same terms as the Perl 5 programming language system itself. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# ============================================================================ |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
46
|
|
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
80
|
use Net::SNMP::Security qw( |
21
|
|
|
|
|
|
|
SECURITY_MODEL_SNMPV1 SECURITY_MODEL_SNMPV2C DEBUG_INFO |
22
|
1
|
|
|
1
|
|
7
|
); |
|
1
|
|
|
|
|
2
|
|
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
85
|
use Net::SNMP::Message qw( |
25
|
|
|
|
|
|
|
OCTET_STRING SEQUENCE INTEGER SNMP_VERSION_1 SNMP_VERSION_2C TRUE |
26
|
1
|
|
|
1
|
|
6
|
); |
|
1
|
|
|
|
|
1
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
## Version of the Net::SNMP::Security::Community module |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
our $VERSION = v2.0.0; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
## Handle importing/exporting of symbols |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
1
|
|
5
|
use base qw( Net::SNMP::Security ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
813
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub import |
37
|
|
|
|
|
|
|
{ |
38
|
0
|
|
|
0
|
|
0
|
return Net::SNMP::Security->export_to_level(1, @_); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
## RFC 3584 - snmpCommunityName::=OCTET STRING |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
1
|
0
|
6
|
sub COMMUNITY_DEFAULT { 'public' } |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# [public methods] ----------------------------------------------------------- |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub new |
48
|
|
|
|
|
|
|
{ |
49
|
1
|
|
|
1
|
0
|
3
|
my ($class, %argv) = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Create a new data structure for the object |
52
|
1
|
|
|
|
|
3
|
my $this = bless { |
53
|
|
|
|
|
|
|
'_error' => undef, # Error message |
54
|
|
|
|
|
|
|
'_version' => SNMP_VERSION_1, # SNMP version |
55
|
|
|
|
|
|
|
'_community' => COMMUNITY_DEFAULT, # Community name |
56
|
|
|
|
|
|
|
}, $class; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Now validate the passed arguments |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
4
|
for (keys %argv) { |
61
|
1
|
50
|
|
|
|
10
|
if (/^-?community$/i) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
$this->_community($argv{$_}); |
63
|
|
|
|
|
|
|
} elsif (/^-?debug$/i) { |
64
|
0
|
|
|
|
|
0
|
$this->debug($argv{$_}); |
65
|
|
|
|
|
|
|
} elsif (/^-?version$/i) { |
66
|
1
|
|
|
|
|
4
|
$this->_version($argv{$_}); |
67
|
|
|
|
|
|
|
} else { |
68
|
0
|
|
|
|
|
0
|
$this->_error('The argument "%s" is unknown', $_); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
1
|
50
|
|
|
|
4
|
if (defined $this->{_error}) { |
72
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $this->{_error}) : undef; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Return the object and an empty error message (in list context) |
77
|
1
|
50
|
|
|
|
7
|
return wantarray ? ($this, q{}) : $this; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub generate_request_msg |
81
|
|
|
|
|
|
|
{ |
82
|
1
|
|
|
1
|
0
|
2
|
my ($this, $pdu, $msg) = @_; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Clear any previous errors |
85
|
1
|
|
|
|
|
11
|
$this->_error_clear(); |
86
|
|
|
|
|
|
|
|
87
|
1
|
50
|
|
|
|
4
|
if (@_ < 3) { |
88
|
0
|
|
|
|
|
0
|
return $this->_error('The required PDU and/or Message object is missing'); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
1
|
50
|
|
|
|
3
|
if ($pdu->version() != $this->{_version}) { |
92
|
0
|
|
|
|
|
0
|
return $this->_error( |
93
|
|
|
|
|
|
|
'The SNMP version %d was expected, but %d was found', |
94
|
|
|
|
|
|
|
$this->{_version}, $pdu->version() |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Append the PDU |
99
|
1
|
50
|
|
|
|
7
|
if (!defined $msg->append($pdu->copy())) { |
100
|
0
|
|
|
|
|
0
|
return $this->_error($msg->error()); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# community::=OCTET STRING |
104
|
1
|
50
|
|
|
|
5
|
if (!defined $msg->prepare(OCTET_STRING, $this->{_community})) { |
105
|
0
|
|
|
|
|
0
|
return $this->_error($msg->error()); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# version::=INTEGER |
109
|
1
|
50
|
|
|
|
4
|
if (!defined $msg->prepare(INTEGER, $this->{_version})) { |
110
|
0
|
|
|
|
|
0
|
return $this->_error($msg->error()); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# message::=SEQUENCE |
114
|
1
|
50
|
|
|
|
4
|
if (!defined $msg->prepare(SEQUENCE)) { |
115
|
0
|
|
|
|
|
0
|
return $this->_error($msg->error()); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Return the message |
119
|
1
|
|
|
|
|
6
|
return $msg; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub process_incoming_msg |
123
|
|
|
|
|
|
|
{ |
124
|
0
|
|
|
0
|
0
|
0
|
my ($this, $msg) = @_; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# Clear any previous errors |
127
|
0
|
|
|
|
|
0
|
$this->_error_clear(); |
128
|
|
|
|
|
|
|
|
129
|
0
|
0
|
|
|
|
0
|
return $this->_error('The required Message object is missing') if (@_ < 2); |
130
|
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
0
|
if ($msg->security_name() ne $this->{_community}) { |
132
|
0
|
|
|
|
|
0
|
return $this->_error( |
133
|
|
|
|
|
|
|
'The community name "%s" was expected, but "%s" was found', |
134
|
|
|
|
|
|
|
$this->{_community}, $msg->security_name() |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
0
|
return TRUE; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub community |
142
|
|
|
|
|
|
|
{ |
143
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->{_community}; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub security_model |
147
|
|
|
|
|
|
|
{ |
148
|
0
|
|
|
0
|
0
|
0
|
my ($this) = @_; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# RFC 3411 - SnmpSecurityModel::=TEXTUAL-CONVENTION |
151
|
|
|
|
|
|
|
|
152
|
0
|
0
|
|
|
|
0
|
if ($this->{_version} == SNMP_VERSION_2C) { |
153
|
0
|
|
|
|
|
0
|
return SECURITY_MODEL_SNMPV2C; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
0
|
return SECURITY_MODEL_SNMPV1; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub security_name |
160
|
|
|
|
|
|
|
{ |
161
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->{_community}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# [private methods] ---------------------------------------------------------- |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub _community |
167
|
|
|
|
|
|
|
{ |
168
|
0
|
|
|
0
|
|
0
|
my ($this, $community) = @_; |
169
|
|
|
|
|
|
|
|
170
|
0
|
0
|
|
|
|
0
|
return $this->_error('The community is not defined') if !defined $community; |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
0
|
$this->{_community} = $community; |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
0
|
return TRUE; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _version |
178
|
|
|
|
|
|
|
{ |
179
|
1
|
|
|
1
|
|
2
|
my ($this, $version) = @_; |
180
|
|
|
|
|
|
|
|
181
|
1
|
50
|
33
|
|
|
3
|
if (($version != SNMP_VERSION_1) && ($version != SNMP_VERSION_2C)) { |
182
|
0
|
|
|
|
|
0
|
return $this->_error('The SNMP version %s is not supported', $version); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
1
|
|
|
|
|
7
|
$this->{_version} = $version; |
186
|
|
|
|
|
|
|
|
187
|
1
|
|
|
|
|
4
|
return TRUE; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# ============================================================================ |
191
|
|
|
|
|
|
|
1; # [end Net::SNMP::Security::Community] |
192
|
|
|
|
|
|
|
|