SNMP/Security/Community.pm

filter: Statementstmt 41.2% 26 / 63 Branchbran 17.6% 6 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 7 / 14 Podpod 0.0% 0 / 7 totaltotal 32.5% 40 / 123 CC 20 SCAR 28.9
CC
20
Cov
33%
generate_request_msg
56.0
new
15.0
process_incoming_msg
12.0
CRAP 18.0
« SNMP/Message.pm ↑ Summary SNMP/Security.pm »
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 <dtown@cpan.org>
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Net::SNMP::Security qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 SECURITY_MODEL_SNMPV1 SECURITY_MODEL_SNMPV2C DEBUG_INFO
22 1 );
23  
24 1 ▶use Net::SNMP::Message qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
25 OCTET_STRING SEQUENCE INTEGER SNMP_VERSION_1 SNMP_VERSION_2C TRUE
26 1 );
27  
28 ## Version of the Net::SNMP::Security::Community module
29  
30 1 our $VERSION = v2.0.0;
31  
32 ## Handle importing/exporting of symbols
33  
34 1 ▶use base qw( Net::SNMP::Security );
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub import
37 0 ▶{
Subroutine import: not called CC=1 SCAR=6.9
38 0 return Net::SNMP::Security->export_to_level(1, @_);
39 }
40  
41 ## RFC 3584 - snmpCommunityName::=OCTET STRING
42  
43 1 ▶sub COMMUNITY_DEFAULT { 'public' }
Subroutine COMMUNITY_DEFAULT: called CC=1 SCAR=0.0
Pod undocumented
44  
45 # [public methods] -----------------------------------------------------------
46  
47 sub new
48 1 ▶{
Subroutine new: called CC=8 SCAR=27.1
Pod undocumented
49 1 my ($class, %argv) = @_;
50  
51 # Create a new data structure for the object
52 1 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 for (keys %argv) {
61 1 ▶ if (/^-?community$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^-?community$/i) { } 0 1 1
elsif (/^-?debug$/i) { } 0 1 1
elsif (/^-?version$/i) { } 1 0 1
62 0 $this->_community($argv{$_});
63 } elsif (/^-?debug$/i) {
64 0 $this->debug($argv{$_});
65 } elsif (/^-?version$/i) {
66 1 $this->_version($argv{$_});
67 } else {
68 0 $this->_error('The argument "%s" is unknown', $_);
69 }
70  
71 1 ▶ if (defined $this->{_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_error"}) 0 1 1
72 0 ▶ return wantarray ? (undef, $this->{_error}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
73 }
74 }
75  
76 # Return the object and an empty error message (in list context)
77 1 ▶ return wantarray ? ($this, q{}) : $this;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
78 }
79  
80 sub generate_request_msg
81 0 ▶{
Subroutine generate_request_msg: not called CC=7 SCAR=40.3
Pod undocumented
82 0 my ($this, $pdu, $msg) = @_;
83  
84 # Clear any previous errors
85 0 $this->_error_clear();
86  
87 0 ▶ if (@_ < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 0 0
88 0 return $this->_error('The required PDU and/or Message object is missing');
89 }
90  
91 0 ▶ if ($pdu->version() != $this->{_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->version != $this->{"_version"}) 0 0 0
92 return $this->_error(
93 'The SNMP version %d was expected, but %d was found',
94 0 $this->{_version}, $pdu->version()
95 );
96 }
97  
98 # Append the PDU
99 0 ▶ if (!defined $msg->append($pdu->copy())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->append($pdu->copy)) 0 0 0
100 0 return $this->_error($msg->error());
101 }
102  
103 # community::=OCTET STRING
104 0 ▶ if (!defined $msg->prepare(OCTET_STRING, $this->{_community})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(OCTET_STRING(), $this->{"_community"})) 0 0 0
105 0 return $this->_error($msg->error());
106 }
107  
108 # version::=INTEGER
109 0 ▶ if (!defined $msg->prepare(INTEGER, $this->{_version})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(INTEGER(), $this->{"_version"})) 0 0 0
110 0 return $this->_error($msg->error());
111 }
112  
113 # message::=SEQUENCE
114 0 ▶ if (!defined $msg->prepare(SEQUENCE)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->prepare(SEQUENCE())) 0 0 0
115 0 return $this->_error($msg->error());
116 }
117  
118 # Return the message
119 0 return $msg;
120 }
121  
122 sub process_incoming_msg
123 0 ▶{
Subroutine process_incoming_msg: not called CC=3 SCAR=24.8
Pod undocumented
124 0 my ($this, $msg) = @_;
125  
126 # Clear any previous errors
127 0 $this->_error_clear();
128  
129 0 ▶ return $this->_error('The required Message object is missing') if (@_ < 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 0 0
130  
131 0 ▶ if ($msg->security_name() ne $this->{_community}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->security_name ne $this->{"_community"}) 0 0 0
132 return $this->_error(
133 'The community name "%s" was expected, but "%s" was found',
134 0 $this->{_community}, $msg->security_name()
135 );
136 }
137  
138 0 return TRUE;
139 }
140  
141 sub community
142 0 ▶{
Subroutine community: not called CC=1 SCAR=6.9
Pod undocumented
143 0 return $_[0]->{_community};
144 }
145  
146 sub security_model
147 0 ▶{
Subroutine security_model: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my ($this) = @_;
149  
150 # RFC 3411 - SnmpSecurityModel::=TEXTUAL-CONVENTION
151  
152 0 ▶ if ($this->{_version} == SNMP_VERSION_2C) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_2C()) 0 0 0
153 0 return SECURITY_MODEL_SNMPV2C;
154 }
155  
156 0 return SECURITY_MODEL_SNMPV1;
157 }
158  
159 sub security_name
160 0 ▶{
Subroutine security_name: not called CC=1 SCAR=6.9
Pod undocumented
161 0 return $_[0]->{_community};
162 }
163  
164 # [private methods] ----------------------------------------------------------
165  
166 sub _community
167 0 ▶{
Subroutine _community: not called CC=2 SCAR=17.9
168 0 my ($this, $community) = @_;
169  
170 0 ▶ return $this->_error('The community is not defined') if !defined $community;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $community 0 0 0
171  
172 0 $this->{_community} = $community;
173  
174 0 return TRUE;
175 }
176  
177 sub _version
178 1 ▶{
Subroutine _version: called CC=3 SCAR=12.7
179 1 my ($this, $version) = @_;
180  
181 1 ▶ if (($version != SNMP_VERSION_1) && ($version != SNMP_VERSION_2C)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version != SNMP_VERSION_1() and $version != SNMP_VERSION_2C()) 0 1 1
Condition33%1/3
$version != SNMP_VERSION_1() and $version != SNMP_VERSION_2C()
!ll&&!rl&&r
010
MC/DC0%0/2
$version != SNMP_VERSION_1() and $version != SNMP_VERSION_2C()
$version != SNMP_VERSION_1() $version != SNMP_VERSION_2C()
Truth table33%1/3
A and B
A= $version != SNMP_VERSION_1()B= $version != SNMP_VERSION_2C()
ABresult
0X0
100
111
182 0 return $this->_error('The SNMP version %s is not supported', $version);
183 }
184  
185 1 $this->{_version} = $version;
186  
187 1 return TRUE;
188 }
189  
190 # ============================================================================
191 1 1; # [end Net::SNMP::Security::Community]
192  
« SNMP/Message.pm ↑ Summary SNMP/Security.pm »