SNMP/Security.pm

filter: Statementstmt 44.5% 33 / 74 Branchbran 11.9% 5 / 42 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 47.0% 8 / 17 Podpod 0.0% 0 / 11 totaltotal 31.4% 49 / 156 CC 26 SCAR 25.6
CC
26
Cov
33%
load_module
30.0
new
20.2
_error
20.0
CRAP 12.9
« SNMP/Security/Community.pm ↑ Summary SNMP/Transport/IPv4/UDP.pm »
1 # -*- mode: perl -*-
2 # ============================================================================
3  
4 package Net::SNMP::Security;
5  
6 # $Id: Security.pm,v 2.0 2009/09/09 15:05:33 dtown Rel $
7  
8 # Base object that implements the Net::SNMP Security Models.
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 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Net::SNMP::Message qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 :securityLevels :securityModels :versions TRUE FALSE
22 2 );
23  
24 ## Version of the Net::SNMP::Security module
25  
26 2 our $VERSION = v2.0.0;
27  
28 ## Handle importing/exporting of symbols
29  
30 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 our @EXPORT_OK = qw( DEBUG_INFO );
33  
34 2 our %EXPORT_TAGS = (
35 levels => [
36 qw( SECURITY_LEVEL_NOAUTHNOPRIV SECURITY_LEVEL_AUTHNOPRIV
37 SECURITY_LEVEL_AUTHPRIV )
38 ],
39 models => [
40 qw( SECURITY_MODEL_ANY SECURITY_MODEL_SNMPV1 SECURITY_MODEL_SNMPV2C
41 SECURITY_MODEL_USM )
42 ]
43 );
44  
45 2 Exporter::export_ok_tags( qw( levels models ) );
46  
47 2 $EXPORT_TAGS{ALL} = [ @EXPORT_OK ];
48  
49 ## Package variables
50  
51 2 our $DEBUG = FALSE; # Debug flag
52  
53 2 our $AUTOLOAD; # Used by the AUTOLOAD method
54  
55 #perl2exe_include Net::SNMP::Security::USM
56  
57 # [public methods] -----------------------------------------------------------
58  
59 sub new
60 1 ▶{
Subroutine new: called CC=9 SCAR=30.1
Pod undocumented
61 1 my ($class, %argv) = @_;
62  
63 1 my $version = SNMP_VERSION_1;
64  
65 # See if a SNMP version has been passed
66 1 for (keys %argv) {
67 1 ▶ if (/^-?version$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-?version$/i) 1 0 1
68 1 ▶ if (($argv{$_} == SNMP_VERSION_1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($argv{$_} == SNMP_VERSION_1() or $argv{$_} == SNMP_VERSION_2C() or $argv{$_} == SNMP_VERSION_3()) 1 0 1
Condition33%2/6
$argv{$_} == SNMP_VERSION_1() or $argv{$_} == SNMP_VERSION_2C() or $argv{$_} == SNMP_VERSION_3()
l!l&&r!l&&!r
100
$argv{$_} == SNMP_VERSION_1() or $argv{$_} == SNMP_VERSION_2C()
l!l&&r!l&&!r
010
MC/DC0%0/3
$argv{$_} == SNMP_VERSION_1() or $argv{$_} == SNMP_VERSION_2C() or $argv{$_} == SNMP_VERSION_3()
$argv{$_} == SNMP_VERSION_1() $argv{$_} == SNMP_VERSION_2C() $argv{$_} == SNMP_VERSION_3()
Truth table25%1/4
A or B or C
A= $argv{$_} == SNMP_VERSION_1()B= $argv{$_} == SNMP_VERSION_2C()C= $argv{$_} == SNMP_VERSION_3()
ABCresult
1XX1
01X1
0011
0000
69 ($argv{$_} == SNMP_VERSION_2C) ||
70 ($argv{$_} == SNMP_VERSION_3))
71 {
72 1 $version = $argv{$_};
73 }
74 }
75 }
76  
77 # Return the appropriate object based upon the SNMP version. To
78 # avoid consuming unnecessary resources, only load the appropriate
79 # module when requested. The Net::SNMP::Security::USM module
80 # requires four non-core modules. If any of these modules are not
81 # present, we gracefully return an error.
82  
83 1 ▶ if ($version == SNMP_VERSION_3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == SNMP_VERSION_3()) 0 1 1
84  
85 0 ▶ if (defined(my $error = load_module('Net::SNMP::Security::USM'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $error = load_module("Net::SNMP::Security::USM"))) 0 0 0
86 0 $error = 'SNMPv3 support is unavailable ' . $error;
87 0 ▶ return wantarray ? (undef, $error) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
88 }
89  
90 0 return Net::SNMP::Security::USM->new(%argv);
91 }
92  
93 # Load the default Security module without eval protection.
94  
95 1 require Net::SNMP::Security::Community;
96 1 return Net::SNMP::Security::Community->new(%argv);
97 }
98  
99 sub version
100 0 ▶{
Subroutine version: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my ($this) = @_;
102  
103 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
104 0 $this->_error_clear();
105 0 return $this->_error('The SNMP version is not modifiable');
106 }
107  
108 0 return $this->{_version};
109 }
110  
111 sub discovered
112 0 ▶{
Subroutine discovered: not called CC=1 SCAR=6.9
Pod undocumented
113 0 return TRUE;
114 }
115  
116 sub security_model
117 0 ▶{
Subroutine security_model: not called CC=1 SCAR=6.9
Pod undocumented
118 # RFC 3411 - SnmpSecurityModel::=TEXTUAL-CONVENTION
119  
120 0 return SECURITY_MODEL_ANY;
121 }
122  
123 sub security_level
124 0 ▶{
Subroutine security_level: not called CC=1 SCAR=6.9
Pod undocumented
125 # RFC 3411 - SnmpSecurityLevel::=TEXTUAL-CONVENTION
126  
127 0 return SECURITY_LEVEL_NOAUTHNOPRIV;
128 }
129  
130 sub security_name
131 0 ▶{
Subroutine security_name: not called CC=1 SCAR=6.9
Pod undocumented
132 0 return q{};
133 }
134  
135 sub debug
136 0 ▶{
Subroutine debug: not called CC=3 SCAR=24.8
Pod undocumented
137 0 ▶ return (@_ == 2) ? $DEBUG = ($_[1]) ? TRUE : FALSE : $DEBUG;
Branch: 2 branches
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
$_[1] ? : 0 0 0
138 }
139  
140 sub error
141 2 ▶{
Subroutine error: called CC=2 SCAR=7.6
Pod undocumented
142 2 ▶ return $_[0]->{_error} || q{};
Condition50%1/2
$_[0]{'_error'} || ''
l!l
02
MC/DC0%0/1
$_[0]{'_error'} || ''
$_[0]{'_error'}
Truth table50%1/2
A
A= $_[0]{'_error'}
Aresult
11
00
143 }
144  
145 sub AUTOLOAD
146 3 ▶{
Subroutine AUTOLOAD: called CC=3 SCAR=19.6
147 3 my ($this) = @_;
148  
149 3 ▶ return if $AUTOLOAD =~ /::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::SNMP::Security::AUTOLOAD =~ /::DESTROY$/ 3 0 3
150  
151 0 $AUTOLOAD =~ s/.*://;
152  
153 0 ▶ if (ref $this) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $this) { } 0 0 0
154 0 $this->_error_clear();
155 0 return $this->_error(
156 'The method "%s" is not supported by this Security Model', $AUTOLOAD
157 );
158 } else {
159 0 require Carp;
160 0 Carp::croak(sprintf 'The function "%s" is not supported', $AUTOLOAD);
161 }
162  
163 # Never get here.
164 0 return;
165 }
166  
167 # [private methods] ----------------------------------------------------------
168  
169 sub _error
170 0 ▶{
Subroutine _error: not called CC=4 SCAR=30.0
171 0 my $this = shift;
172  
173 0 ▶ if (!defined $this->{_error}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_error"}) 0 0 0
174 0 ▶ $this->{_error} = (@_ > 1) ? sprintf(shift(@_), @_) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
175 0 ▶ if ($this->debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->debug) 0 0 0
176 printf "error: [%d] %s(): %s\n",
177 0 (caller 0)[2], (caller 1)[3], $this->{_error};
178 }
179 }
180  
181 0 return;
182 }
183  
184 sub _error_clear
185 6 ▶{
Subroutine _error_clear: called CC=1 SCAR=0.0
186 6 return $_[0]->{_error} = undef;
187 }
188  
189 {
190 2 my %modules;
191  
192 sub load_module
193 0 ▶ {
Subroutine load_module: not called CC=5 SCAR=34.0
Pod undocumented
194 0 my ($module) = @_;
195  
196 # We attempt to load the required module under the protection of an
197 # eval statement. If there is a failure, typically it is due to a
198 # missing module required by the requested module and we attempt to
199 # simplify the error message by just listing that module. We also
200 # need to track failures since require() only produces an error on
201 # the first attempt to load the module.
202  
203 # NOTE: Contrary to our typical convention, a return value of "undef"
204 # actually means success and a defined value means error.
205  
206 0 ▶ return $modules{$module} if exists $modules{$module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $modules{$module} 0 0 0
207  
208 0 ▶ if (!eval "require $module") {
Branch: 1 branch
BranchTrueFalseTotal
if (not eval "require $module") { } 0 0 0
209 0 ▶ if ($@ =~ m/locate (\S+\.pm)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /locate (\S+\.pm)/) { } 0 0 0
elsif ($@ =~ /(.*)\n/) { } 0 0 0
210 0 $modules{$module} = err_msg('(Required module %s not found)', $1);
211 } elsif ($@ =~ m/(.*)\n/) {
212 0 $modules{$module} = err_msg('(%s)', $1);
213 } else {
214 0 $modules{$module} = err_msg('(%s)', $@);
215 }
216 } else {
217 0 $modules{$module} = undef;
218 }
219  
220 0 return $modules{$module};
221 }
222 }
223  
224 sub err_msg
225 2 ▶{
Subroutine err_msg: not called CC=3 SCAR=22.3
Pod undocumented
226 0 ▶ my $msg = (@_ > 1) ? sprintf(shift(@_), @_) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
227  
228 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::SNMP::Security::DEBUG) 0 0 0
229 0 printf "error: [%d] %s(): %s\n", (caller 0)[2], (caller 1)[3], $msg;
230 }
231  
232 0 return $msg;
233 }
234  
235 sub DEBUG_INFO
236 12 ▶{
Subroutine DEBUG_INFO: called CC=3 SCAR=17.3
Pod undocumented
237 12 ▶ return if (!$DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::SNMP::Security::DEBUG 12 0 12
238  
239 0 ▶ return printf
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
240 sprintf('debug: [%d] %s(): ', (caller 0)[2], (caller 1)[3]) .
241 ((@_ > 1) ? shift(@_) : '%s') .
242 "\n",
243 @_;
244 }
245  
246 # ============================================================================
247 2 1; # [end Net::SNMP::Security]
248  
« SNMP/Security/Community.pm ↑ Summary SNMP/Transport/IPv4/UDP.pm »