Skip to content

Commit 91631f8

Browse files
author
blackhedd
committed
Supported RFC 2251 "controls"
1 parent 84e2ccc commit 91631f8

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

lib/net/ldap/pdu.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ class LdapPdu
5050
#
5151
# initialize
5252
# An LDAP PDU always looks like a BerSequence with
53-
# two elements: an integer (message-id number), and
53+
# at least two elements: an integer (message-id number), and
5454
# an application-specific sequence.
55+
# Some LDAPv3 packets also include an optional
56+
# third element, which is a sequence of "controls"
57+
# (See RFC 2251, section 4.1.12).
5558
# The application-specific tag in the sequence tells
5659
# us what kind of packet it is, and each kind has its
5760
# own format, defined in RFC-1777.
@@ -62,6 +65,10 @@ class LdapPdu
6265
# it remains to be seen whether there are servers out
6366
# there that will not work well with our approach.
6467
#
68+
# Added a controls-processor to SearchResult.
69+
# Didn't add it everywhere because it just _feels_
70+
# like it will need to be refactored.
71+
#
6572
def initialize ber_object
6673
begin
6774
@msg_id = ber_object[0].to_i
@@ -78,6 +85,7 @@ def initialize ber_object
7885
parse_search_return ber_object[1]
7986
when SearchResult
8087
parse_ldap_result ber_object[1]
88+
parse_controls(ber_object[2]) if ber_object[2]
8189
when ModifyResponse
8290
parse_ldap_result ber_object[1]
8391
when AddResponse
@@ -102,7 +110,6 @@ def result_code code = :resultCode
102110
end
103111

104112

105-
private
106113

107114
#
108115
# parse_ldap_result
@@ -146,6 +153,28 @@ def parse_search_return sequence
146153
@search_attributes[seq[0].downcase.intern] = seq[1]
147154
}
148155
end
156+
private :parse_ldap_result
157+
158+
159+
# Per RFC 2251, an LDAP "control" is a sequence of tuples, each consisting
160+
# of an OID, a boolean criticality flag defaulting FALSE, and an OPTIONAL
161+
# Octet String. If only two fields are given, the second one may be
162+
# either criticality or data, since criticality has a default value.
163+
# Someday we may want to come back here and add support for some of
164+
# more-widely used controls. RFC-2696 is a good example.
165+
#
166+
def parse_controls sequence
167+
@ldap_controls = sequence.map do |control|
168+
o = OpenStruct.new
169+
o.oid,o.criticality,o.value = control[0],control[1],control[2]
170+
if o.criticality and o.criticality.is_a?(String)
171+
o.value = o.criticality
172+
o.criticality = false
173+
end
174+
o
175+
end
176+
end
177+
private :parse_controls
149178

150179

151180
end

0 commit comments

Comments
 (0)