net-ldap 0.3.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Contributors.rdoc +4 -0
- data/Hacking.rdoc +3 -8
- data/History.rdoc +181 -0
- data/README.rdoc +44 -12
- data/lib/net-ldap.rb +1 -1
- data/lib/net/ber.rb +41 -7
- data/lib/net/ber/ber_parser.rb +21 -7
- data/lib/net/ber/core_ext.rb +11 -18
- data/lib/net/ber/core_ext/array.rb +14 -0
- data/lib/net/ber/core_ext/integer.rb +74 -0
- data/lib/net/ber/core_ext/string.rb +24 -4
- data/lib/net/ber/core_ext/true_class.rb +2 -3
- data/lib/net/ldap.rb +441 -639
- data/lib/net/ldap/auth_adapter.rb +29 -0
- data/lib/net/ldap/auth_adapter/gss_spnego.rb +41 -0
- data/lib/net/ldap/auth_adapter/sasl.rb +62 -0
- data/lib/net/ldap/auth_adapter/simple.rb +34 -0
- data/lib/net/ldap/connection.rb +716 -0
- data/lib/net/ldap/dataset.rb +23 -9
- data/lib/net/ldap/dn.rb +13 -14
- data/lib/net/ldap/entry.rb +27 -9
- data/lib/net/ldap/error.rb +49 -0
- data/lib/net/ldap/filter.rb +58 -32
- data/lib/net/ldap/instrumentation.rb +23 -0
- data/lib/net/ldap/password.rb +23 -14
- data/lib/net/ldap/pdu.rb +70 -6
- data/lib/net/ldap/version.rb +5 -0
- data/lib/net/snmp.rb +237 -241
- metadata +71 -116
- data/.autotest +0 -11
- data/.gemtest +0 -0
- data/.rspec +0 -2
- data/Manifest.txt +0 -49
- data/Rakefile +0 -74
- data/autotest/discover.rb +0 -1
- data/lib/net/ber/core_ext/bignum.rb +0 -22
- data/lib/net/ber/core_ext/fixnum.rb +0 -66
- data/net-ldap.gemspec +0 -58
- data/spec/integration/ssl_ber_spec.rb +0 -36
- data/spec/spec.opts +0 -2
- data/spec/spec_helper.rb +0 -5
- data/spec/unit/ber/ber_spec.rb +0 -109
- data/spec/unit/ber/core_ext/string_spec.rb +0 -51
- data/spec/unit/ldap/dn_spec.rb +0 -80
- data/spec/unit/ldap/entry_spec.rb +0 -51
- data/spec/unit/ldap/filter_spec.rb +0 -84
- data/spec/unit/ldap_spec.rb +0 -48
- data/test/common.rb +0 -3
- data/test/test_entry.rb +0 -59
- data/test/test_filter.rb +0 -122
- data/test/test_ldap_connection.rb +0 -24
- data/test/test_ldif.rb +0 -79
- data/test/test_password.rb +0 -17
- data/test/test_rename.rb +0 -77
- data/test/test_snmp.rb +0 -114
- data/test/testdata.ldif +0 -101
- data/testserver/ldapserver.rb +0 -210
- data/testserver/testdata.ldif +0 -101
@@ -0,0 +1,23 @@
|
|
1
|
+
module Net::LDAP::Instrumentation
|
2
|
+
attr_reader :instrumentation_service
|
3
|
+
private :instrumentation_service
|
4
|
+
|
5
|
+
# Internal: Instrument a block with the defined instrumentation service.
|
6
|
+
#
|
7
|
+
# Yields the event payload if a block is given.
|
8
|
+
#
|
9
|
+
# Skips instrumentation if no service is set.
|
10
|
+
#
|
11
|
+
# Returns the return value of the block.
|
12
|
+
def instrument(event, payload = {})
|
13
|
+
payload = (payload || {}).dup
|
14
|
+
if instrumentation_service
|
15
|
+
instrumentation_service.instrument(event, payload) do |instr_payload|
|
16
|
+
instr_payload[:result] = yield(instr_payload) if block_given?
|
17
|
+
end
|
18
|
+
else
|
19
|
+
yield(payload) if block_given?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
private :instrument
|
23
|
+
end
|
data/lib/net/ldap/password.rb
CHANGED
@@ -1,31 +1,40 @@
|
|
1
1
|
# -*- ruby encoding: utf-8 -*-
|
2
2
|
require 'digest/sha1'
|
3
|
+
require 'digest/sha2'
|
3
4
|
require 'digest/md5'
|
5
|
+
require 'base64'
|
6
|
+
require 'securerandom'
|
4
7
|
|
5
8
|
class Net::LDAP::Password
|
6
9
|
class << self
|
7
10
|
# Generate a password-hash suitable for inclusion in an LDAP attribute.
|
8
|
-
# Pass a hash type
|
11
|
+
# Pass a hash type as a symbol (:md5, :sha, :ssha) and a plaintext
|
9
12
|
# password. This function will return a hashed representation.
|
10
13
|
#
|
11
14
|
#--
|
12
15
|
# STUB: This is here to fulfill the requirements of an RFC, which
|
13
16
|
# one?
|
14
17
|
#
|
15
|
-
# TODO
|
16
|
-
#
|
17
|
-
# provide
|
18
|
+
# TODO:
|
19
|
+
# * maybe salted-md5
|
20
|
+
# * Should we provide sha1 as a synonym for sha1? I vote no because then
|
21
|
+
# should you also provide ssha1 for symmetry?
|
22
|
+
#
|
18
23
|
def generate(type, str)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
case type
|
25
|
+
when :md5
|
26
|
+
'{MD5}' + Base64.strict_encode64(Digest::MD5.digest(str))
|
27
|
+
when :sha
|
28
|
+
'{SHA}' + Base64.strict_encode64(Digest::SHA1.digest(str))
|
29
|
+
when :ssha
|
30
|
+
salt = SecureRandom.random_bytes(16)
|
31
|
+
'{SSHA}' + Base64.strict_encode64(Digest::SHA1.digest(str + salt) + salt)
|
32
|
+
when :ssha256
|
33
|
+
salt = SecureRandom.random_bytes(16)
|
34
|
+
'{SSHA256}' + Base64.strict_encode64(Digest::SHA256.digest(str + salt) + salt)
|
35
|
+
else
|
36
|
+
raise Net::LDAP::HashTypeUnsupportedError, "Unsupported password-hash type (#{type})"
|
37
|
+
end
|
29
38
|
end
|
30
39
|
end
|
31
40
|
end
|
data/lib/net/ldap/pdu.rb
CHANGED
@@ -18,24 +18,48 @@ require 'ostruct'
|
|
18
18
|
# well with our approach.
|
19
19
|
#
|
20
20
|
# Currently, we only support controls on SearchResult.
|
21
|
+
#
|
22
|
+
# http://tools.ietf.org/html/rfc4511#section-4.1.1
|
23
|
+
# http://tools.ietf.org/html/rfc4511#section-4.1.9
|
21
24
|
class Net::LDAP::PDU
|
22
25
|
class Error < RuntimeError; end
|
23
26
|
|
24
|
-
|
25
|
-
# This message packet is a bind request.
|
27
|
+
# http://tools.ietf.org/html/rfc4511#section-4.2
|
26
28
|
BindRequest = 0
|
29
|
+
# http://tools.ietf.org/html/rfc4511#section-4.2.2
|
27
30
|
BindResult = 1
|
31
|
+
# http://tools.ietf.org/html/rfc4511#section-4.3
|
28
32
|
UnbindRequest = 2
|
33
|
+
# http://tools.ietf.org/html/rfc4511#section-4.5.1
|
29
34
|
SearchRequest = 3
|
35
|
+
# http://tools.ietf.org/html/rfc4511#section-4.5.2
|
30
36
|
SearchReturnedData = 4
|
31
37
|
SearchResult = 5
|
38
|
+
# see also SearchResultReferral (19)
|
39
|
+
# http://tools.ietf.org/html/rfc4511#section-4.6
|
40
|
+
ModifyRequest = 6
|
32
41
|
ModifyResponse = 7
|
42
|
+
# http://tools.ietf.org/html/rfc4511#section-4.7
|
43
|
+
AddRequest = 8
|
33
44
|
AddResponse = 9
|
45
|
+
# http://tools.ietf.org/html/rfc4511#section-4.8
|
46
|
+
DeleteRequest = 10
|
34
47
|
DeleteResponse = 11
|
48
|
+
# http://tools.ietf.org/html/rfc4511#section-4.9
|
49
|
+
ModifyRDNRequest = 12
|
35
50
|
ModifyRDNResponse = 13
|
51
|
+
# http://tools.ietf.org/html/rfc4511#section-4.10
|
52
|
+
CompareRequest = 14
|
53
|
+
CompareResponse = 15
|
54
|
+
# http://tools.ietf.org/html/rfc4511#section-4.11
|
55
|
+
AbandonRequest = 16
|
56
|
+
# http://tools.ietf.org/html/rfc4511#section-4.5.2
|
36
57
|
SearchResultReferral = 19
|
58
|
+
# http://tools.ietf.org/html/rfc4511#section-4.12
|
37
59
|
ExtendedRequest = 23
|
38
60
|
ExtendedResponse = 24
|
61
|
+
# unused: http://tools.ietf.org/html/rfc4511#section-4.13
|
62
|
+
IntermediateResponse = 25
|
39
63
|
|
40
64
|
##
|
41
65
|
# The LDAP packet message ID.
|
@@ -50,6 +74,7 @@ class Net::LDAP::PDU
|
|
50
74
|
attr_reader :search_referrals
|
51
75
|
attr_reader :search_parameters
|
52
76
|
attr_reader :bind_parameters
|
77
|
+
attr_reader :extended_response
|
53
78
|
|
54
79
|
##
|
55
80
|
# Returns RFC-2251 Controls if any.
|
@@ -96,9 +121,9 @@ class Net::LDAP::PDU
|
|
96
121
|
when UnbindRequest
|
97
122
|
parse_unbind_request(ber_object[1])
|
98
123
|
when ExtendedResponse
|
99
|
-
|
124
|
+
parse_extended_response(ber_object[1])
|
100
125
|
else
|
101
|
-
raise
|
126
|
+
raise Error.new("unknown pdu-type: #{@app_tag}")
|
102
127
|
end
|
103
128
|
|
104
129
|
parse_controls(ber_object[2]) if ber_object[2]
|
@@ -112,6 +137,10 @@ class Net::LDAP::PDU
|
|
112
137
|
@ldap_result || {}
|
113
138
|
end
|
114
139
|
|
140
|
+
def error_message
|
141
|
+
result[:errorMessage] || ""
|
142
|
+
end
|
143
|
+
|
115
144
|
##
|
116
145
|
# This returns an LDAP result code taken from the PDU, but it will be nil
|
117
146
|
# if there wasn't a result code. That can easily happen depending on the
|
@@ -120,6 +149,18 @@ class Net::LDAP::PDU
|
|
120
149
|
@ldap_result and @ldap_result[code]
|
121
150
|
end
|
122
151
|
|
152
|
+
def status
|
153
|
+
Net::LDAP::ResultCodesNonError.include?(result_code) ? :success : :failure
|
154
|
+
end
|
155
|
+
|
156
|
+
def success?
|
157
|
+
status == :success
|
158
|
+
end
|
159
|
+
|
160
|
+
def failure?
|
161
|
+
!success?
|
162
|
+
end
|
163
|
+
|
123
164
|
##
|
124
165
|
# Return serverSaslCreds, which are only present in BindResponse packets.
|
125
166
|
#--
|
@@ -134,12 +175,35 @@ class Net::LDAP::PDU
|
|
134
175
|
@ldap_result = {
|
135
176
|
:resultCode => sequence[0],
|
136
177
|
:matchedDN => sequence[1],
|
137
|
-
:errorMessage => sequence[2]
|
178
|
+
:errorMessage => sequence[2],
|
138
179
|
}
|
139
|
-
parse_search_referral(sequence[3]) if @ldap_result[:resultCode] ==
|
180
|
+
parse_search_referral(sequence[3]) if @ldap_result[:resultCode] == Net::LDAP::ResultCodeReferral
|
140
181
|
end
|
141
182
|
private :parse_ldap_result
|
142
183
|
|
184
|
+
##
|
185
|
+
# Parse an extended response
|
186
|
+
#
|
187
|
+
# http://www.ietf.org/rfc/rfc2251.txt
|
188
|
+
#
|
189
|
+
# Each Extended operation consists of an Extended request and an
|
190
|
+
# Extended response.
|
191
|
+
#
|
192
|
+
# ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
|
193
|
+
# requestName [0] LDAPOID,
|
194
|
+
# requestValue [1] OCTET STRING OPTIONAL }
|
195
|
+
|
196
|
+
def parse_extended_response(sequence)
|
197
|
+
sequence.length >= 3 or raise Net::LDAP::PDU::Error, "Invalid LDAP result length."
|
198
|
+
@ldap_result = {
|
199
|
+
:resultCode => sequence[0],
|
200
|
+
:matchedDN => sequence[1],
|
201
|
+
:errorMessage => sequence[2],
|
202
|
+
}
|
203
|
+
@extended_response = sequence[3]
|
204
|
+
end
|
205
|
+
private :parse_extended_response
|
206
|
+
|
143
207
|
##
|
144
208
|
# A Bind Response may have an additional field, ID [7], serverSaslCreds,
|
145
209
|
# per RFC 2251 pgh 4.2.3.
|
data/lib/net/snmp.rb
CHANGED
@@ -1,268 +1,264 @@
|
|
1
1
|
# -*- ruby encoding: utf-8 -*-
|
2
|
+
require_relative 'ldap/version'
|
3
|
+
|
2
4
|
# :stopdoc:
|
3
5
|
module Net
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
}
|
25
|
-
}
|
26
|
-
})
|
27
|
-
|
28
|
-
# SNMP 32-bit counter.
|
29
|
-
# Defined in RFC1155 (Structure of Mangement Information), section 6.
|
30
|
-
# A 32-bit counter is an ASN.1 application [1] implicit unsigned integer
|
31
|
-
# with a range from 0 to 2^^32 - 1.
|
32
|
-
class Counter32
|
33
|
-
def initialize value
|
34
|
-
@value = value
|
35
|
-
end
|
36
|
-
def to_ber
|
37
|
-
@value.to_ber_application(1)
|
38
|
-
end
|
39
|
-
end
|
6
|
+
class SNMP
|
7
|
+
VERSION = Net::LDAP::VERSION
|
8
|
+
AsnSyntax = Net::BER.compile_syntax({
|
9
|
+
:application => {
|
10
|
+
:primitive => {
|
11
|
+
1 => :integer, # Counter32, (RFC2578 sec 2)
|
12
|
+
2 => :integer, # Gauge32 or Unsigned32, (RFC2578 sec 2)
|
13
|
+
3 => :integer # TimeTicks32, (RFC2578 sec 2)
|
14
|
+
},
|
15
|
+
:constructed => {},
|
16
|
+
},
|
17
|
+
:context_specific => {
|
18
|
+
:primitive => {},
|
19
|
+
:constructed => {
|
20
|
+
0 => :array, # GetRequest PDU (RFC1157 pgh 4.1.2)
|
21
|
+
1 => :array, # GetNextRequest PDU (RFC1157 pgh 4.1.3)
|
22
|
+
2 => :array # GetResponse PDU (RFC1157 pgh 4.1.4)
|
23
|
+
},
|
24
|
+
},
|
25
|
+
})
|
40
26
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
# SNMP 32-bit timer-ticks.
|
55
|
-
# Defined in RFC1155 (Structure of Mangement Information), section 6.
|
56
|
-
# A 32-bit counter is an ASN.1 application [3] implicit unsigned integer.
|
57
|
-
class TimeTicks32
|
58
|
-
def initialize value
|
59
|
-
@value = value
|
60
|
-
end
|
61
|
-
def to_ber
|
62
|
-
@value.to_ber_application(3)
|
63
|
-
end
|
64
|
-
end
|
27
|
+
# SNMP 32-bit counter.
|
28
|
+
# Defined in RFC1155 (Structure of Mangement Information), section 6.
|
29
|
+
# A 32-bit counter is an ASN.1 application [1] implicit unsigned integer
|
30
|
+
# with a range from 0 to 2^^32 - 1.
|
31
|
+
class Counter32
|
32
|
+
def initialize value
|
33
|
+
@value = value
|
34
|
+
end
|
35
|
+
def to_ber
|
36
|
+
@value.to_ber_application(1)
|
37
|
+
end
|
65
38
|
end
|
66
39
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
1 => "tooBig",
|
80
|
-
2 => "noSuchName",
|
81
|
-
3 => "badValue",
|
82
|
-
4 => "readOnly",
|
83
|
-
5 => "genErr"
|
84
|
-
}
|
40
|
+
# SNMP 32-bit gauge.
|
41
|
+
# Defined in RFC1155 (Structure of Mangement Information), section 6.
|
42
|
+
# A 32-bit counter is an ASN.1 application [2] implicit unsigned integer.
|
43
|
+
# This is also indistinguishable from Unsigned32. (Need to alias them.)
|
44
|
+
class Gauge32
|
45
|
+
def initialize value
|
46
|
+
@value = value
|
47
|
+
end
|
48
|
+
def to_ber
|
49
|
+
@value.to_ber_application(2)
|
50
|
+
end
|
51
|
+
end
|
85
52
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
53
|
+
# SNMP 32-bit timer-ticks.
|
54
|
+
# Defined in RFC1155 (Structure of Mangement Information), section 6.
|
55
|
+
# A 32-bit counter is an ASN.1 application [3] implicit unsigned integer.
|
56
|
+
class TimeTicks32
|
57
|
+
def initialize value
|
58
|
+
@value = value
|
59
|
+
end
|
60
|
+
def to_ber
|
61
|
+
@value.to_ber_application(3)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
93
65
|
|
94
|
-
|
95
|
-
|
66
|
+
class SnmpPdu
|
67
|
+
class Error < StandardError; end
|
68
|
+
PduTypes = [
|
69
|
+
:get_request,
|
70
|
+
:get_next_request,
|
71
|
+
:get_response,
|
72
|
+
:set_request,
|
73
|
+
:trap,
|
74
|
+
]
|
75
|
+
ErrorStatusCodes = { # Per RFC1157, pgh 4.1.1
|
76
|
+
0 => "noError",
|
77
|
+
1 => "tooBig",
|
78
|
+
2 => "noSuchName",
|
79
|
+
3 => "badValue",
|
80
|
+
4 => "readOnly",
|
81
|
+
5 => "genErr",
|
82
|
+
}
|
96
83
|
|
84
|
+
class << self
|
85
|
+
def parse ber_object
|
86
|
+
n = new
|
87
|
+
n.send :parse, ber_object
|
88
|
+
n
|
89
|
+
end
|
90
|
+
end
|
97
91
|
|
98
|
-
|
99
|
-
|
100
|
-
@community = args[:community] || "public"
|
101
|
-
@pdu_type = args[:pdu_type] # leave nil unless specified; there's no reasonable default value.
|
102
|
-
@error_status = args[:error_status] || 0
|
103
|
-
@error_index = args[:error_index] || 0
|
104
|
-
@variables = args[:variables] || []
|
105
|
-
end
|
92
|
+
attr_reader :version, :community, :pdu_type, :variables, :error_status
|
93
|
+
attr_accessor :request_id, :error_index
|
106
94
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
# Wrap any basic parsing error so it becomes a PDU-format error
|
116
|
-
raise Error.new( "snmp-pdu format error" )
|
117
|
-
end
|
118
|
-
end
|
119
|
-
private :parse
|
95
|
+
def initialize args={}
|
96
|
+
@version = args[:version] || 0
|
97
|
+
@community = args[:community] || "public"
|
98
|
+
@pdu_type = args[:pdu_type] # leave nil unless specified; there's no reasonable default value.
|
99
|
+
@error_status = args[:error_status] || 0
|
100
|
+
@error_index = args[:error_index] || 0
|
101
|
+
@variables = args[:variables] || []
|
102
|
+
end
|
120
103
|
|
121
|
-
|
122
|
-
|
123
|
-
|
104
|
+
#--
|
105
|
+
def parse ber_object
|
106
|
+
begin
|
107
|
+
parse_ber_object ber_object
|
108
|
+
rescue Error
|
109
|
+
# Pass through any SnmpPdu::Error instances
|
110
|
+
raise $!
|
111
|
+
rescue
|
112
|
+
# Wrap any basic parsing error so it becomes a PDU-format error
|
113
|
+
raise Error.new( "snmp-pdu format error" )
|
114
|
+
end
|
115
|
+
end
|
116
|
+
private :parse
|
124
117
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
send :pdu_type=, :get_request
|
129
|
-
parse_get_request data
|
130
|
-
when 1
|
131
|
-
send :pdu_type=, :get_next_request
|
132
|
-
# This PDU is identical to get-request except for the type.
|
133
|
-
parse_get_request data
|
134
|
-
when 2
|
135
|
-
send :pdu_type=, :get_response
|
136
|
-
# This PDU is identical to get-request except for the type,
|
137
|
-
# the error_status and error_index values are meaningful,
|
138
|
-
# and the fact that the variable bindings will be non-null.
|
139
|
-
parse_get_response data
|
140
|
-
else
|
141
|
-
raise Error.new( "unknown snmp-pdu type: #{app_tag}" )
|
142
|
-
end
|
143
|
-
end
|
144
|
-
private :parse_ber_object
|
118
|
+
def parse_ber_object ber_object
|
119
|
+
send :version=, ber_object[0].to_i
|
120
|
+
send :community=, ber_object[1].to_s
|
145
121
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
122
|
+
data = ber_object[2]
|
123
|
+
case (app_tag = data.ber_identifier & 31)
|
124
|
+
when 0
|
125
|
+
send :pdu_type=, :get_request
|
126
|
+
parse_get_request data
|
127
|
+
when 1
|
128
|
+
send :pdu_type=, :get_next_request
|
129
|
+
# This PDU is identical to get-request except for the type.
|
130
|
+
parse_get_request data
|
131
|
+
when 2
|
132
|
+
send :pdu_type=, :get_response
|
133
|
+
# This PDU is identical to get-request except for the type,
|
134
|
+
# the error_status and error_index values are meaningful,
|
135
|
+
# and the fact that the variable bindings will be non-null.
|
136
|
+
parse_get_response data
|
137
|
+
else
|
138
|
+
raise Error.new( "unknown snmp-pdu type: #{app_tag}" )
|
139
|
+
end
|
140
|
+
end
|
141
|
+
private :parse_ber_object
|
165
142
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
143
|
+
#--
|
144
|
+
# Defined in RFC1157, pgh 4.1.2.
|
145
|
+
def parse_get_request data
|
146
|
+
send :request_id=, data[0].to_i
|
147
|
+
# data[1] is error_status, always zero.
|
148
|
+
# data[2] is error_index, always zero.
|
149
|
+
send :error_status=, 0
|
150
|
+
send :error_index=, 0
|
151
|
+
data[3].each do |n, v|
|
152
|
+
# A variable-binding, of which there may be several,
|
153
|
+
# consists of an OID and a BER null.
|
154
|
+
# We're ignoring the null, we might want to verify it instead.
|
155
|
+
unless v.is_a?(Net::BER::BerIdentifiedNull)
|
156
|
+
raise Error.new(" invalid variable-binding in get-request" )
|
157
|
+
end
|
158
|
+
add_variable_binding n, nil
|
159
|
+
end
|
160
|
+
end
|
161
|
+
private :parse_get_request
|
180
162
|
|
163
|
+
#--
|
164
|
+
# Defined in RFC1157, pgh 4.1.4
|
165
|
+
def parse_get_response data
|
166
|
+
send :request_id=, data[0].to_i
|
167
|
+
send :error_status=, data[1].to_i
|
168
|
+
send :error_index=, data[2].to_i
|
169
|
+
data[3].each do |n, v|
|
170
|
+
# A variable-binding, of which there may be several,
|
171
|
+
# consists of an OID and a BER null.
|
172
|
+
# We're ignoring the null, we might want to verify it instead.
|
173
|
+
add_variable_binding n, v
|
174
|
+
end
|
175
|
+
end
|
176
|
+
private :parse_get_response
|
181
177
|
|
182
|
-
def version= ver
|
183
|
-
unless [0,2].include?(ver)
|
184
|
-
raise Error.new("unknown snmp-version: #{ver}")
|
185
|
-
end
|
186
|
-
@version = ver
|
187
|
-
end
|
188
178
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
179
|
+
def version= ver
|
180
|
+
unless [0, 2].include?(ver)
|
181
|
+
raise Error.new("unknown snmp-version: #{ver}")
|
182
|
+
end
|
183
|
+
@version = ver
|
184
|
+
end
|
195
185
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
186
|
+
def pdu_type= t
|
187
|
+
unless PduTypes.include?(t)
|
188
|
+
raise Error.new("unknown pdu-type: #{t}")
|
189
|
+
end
|
190
|
+
@pdu_type = t
|
191
|
+
end
|
202
192
|
|
203
|
-
|
204
|
-
|
205
|
-
|
193
|
+
def error_status= es
|
194
|
+
unless ErrorStatusCodes.key?(es)
|
195
|
+
raise Error.new("unknown error-status: #{es}")
|
196
|
+
end
|
197
|
+
@error_status = es
|
198
|
+
end
|
206
199
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
@variables ||= []
|
211
|
-
@variables << [name, value]
|
212
|
-
end
|
200
|
+
def community= c
|
201
|
+
@community = c.to_s
|
202
|
+
end
|
213
203
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
end
|
204
|
+
#--
|
205
|
+
# Syntactic sugar
|
206
|
+
def add_variable_binding name, value=nil
|
207
|
+
@variables ||= []
|
208
|
+
@variables << [name, value]
|
209
|
+
end
|
221
210
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
request_id.to_ber,
|
230
|
-
error_status.to_ber,
|
231
|
-
error_index.to_ber,
|
232
|
-
[
|
233
|
-
@variables.map {|n,v|
|
234
|
-
[n.to_ber_oid, Net::BER::BerIdentifiedNull.new.to_ber].to_ber_sequence
|
235
|
-
}
|
236
|
-
].to_ber_sequence
|
237
|
-
].to_ber_contextspecific(0)
|
238
|
-
when :get_next_request
|
239
|
-
[
|
240
|
-
request_id.to_ber,
|
241
|
-
error_status.to_ber,
|
242
|
-
error_index.to_ber,
|
243
|
-
[
|
244
|
-
@variables.map {|n,v|
|
245
|
-
[n.to_ber_oid, Net::BER::BerIdentifiedNull.new.to_ber].to_ber_sequence
|
246
|
-
}
|
247
|
-
].to_ber_sequence
|
248
|
-
].to_ber_contextspecific(1)
|
249
|
-
when :get_response
|
250
|
-
[
|
251
|
-
request_id.to_ber,
|
252
|
-
error_status.to_ber,
|
253
|
-
error_index.to_ber,
|
254
|
-
[
|
255
|
-
@variables.map {|n,v|
|
256
|
-
[n.to_ber_oid, v.to_ber].to_ber_sequence
|
257
|
-
}
|
258
|
-
].to_ber_sequence
|
259
|
-
].to_ber_contextspecific(2)
|
260
|
-
else
|
261
|
-
raise Error.new( "unknown pdu-type: #{pdu_type}" )
|
262
|
-
end
|
263
|
-
end
|
264
|
-
private :pdu_to_ber_string
|
211
|
+
def to_ber_string
|
212
|
+
[
|
213
|
+
version.to_ber,
|
214
|
+
community.to_ber,
|
215
|
+
pdu_to_ber_string,
|
216
|
+
].to_ber_sequence
|
217
|
+
end
|
265
218
|
|
219
|
+
#--
|
220
|
+
# Helper method that returns a PDU payload in BER form,
|
221
|
+
# depending on the PDU type.
|
222
|
+
def pdu_to_ber_string
|
223
|
+
case pdu_type
|
224
|
+
when :get_request
|
225
|
+
[
|
226
|
+
request_id.to_ber,
|
227
|
+
error_status.to_ber,
|
228
|
+
error_index.to_ber,
|
229
|
+
[
|
230
|
+
@variables.map do|n, v|
|
231
|
+
[n.to_ber_oid, Net::BER::BerIdentifiedNull.new.to_ber].to_ber_sequence
|
232
|
+
end,
|
233
|
+
].to_ber_sequence,
|
234
|
+
].to_ber_contextspecific(0)
|
235
|
+
when :get_next_request
|
236
|
+
[
|
237
|
+
request_id.to_ber,
|
238
|
+
error_status.to_ber,
|
239
|
+
error_index.to_ber,
|
240
|
+
[
|
241
|
+
@variables.map do|n, v|
|
242
|
+
[n.to_ber_oid, Net::BER::BerIdentifiedNull.new.to_ber].to_ber_sequence
|
243
|
+
end,
|
244
|
+
].to_ber_sequence,
|
245
|
+
].to_ber_contextspecific(1)
|
246
|
+
when :get_response
|
247
|
+
[
|
248
|
+
request_id.to_ber,
|
249
|
+
error_status.to_ber,
|
250
|
+
error_index.to_ber,
|
251
|
+
[
|
252
|
+
@variables.map do|n, v|
|
253
|
+
[n.to_ber_oid, v.to_ber].to_ber_sequence
|
254
|
+
end,
|
255
|
+
].to_ber_sequence,
|
256
|
+
].to_ber_contextspecific(2)
|
257
|
+
else
|
258
|
+
raise Error.new( "unknown pdu-type: #{pdu_type}" )
|
259
|
+
end
|
266
260
|
end
|
261
|
+
private :pdu_to_ber_string
|
262
|
+
end
|
267
263
|
end
|
268
264
|
# :startdoc:
|