Skip to content

Commit c46c937

Browse files
DavidJLeedjlee2
authored andcommitted
Added a new filter type bineq that will create an equality filter and NOT force
convert data to UTF-8. This is required for proper binary data filters in Microsoft Active Directory.
1 parent 3345c58 commit c46c937

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

Contributors.rdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Contributions since:
1919
* Derek Harmel (derekharmel)
2020
* Erik Hetzner (egh)
2121
* nowhereman
22+
* David J. Lee (DavidJLee)

lib/net/ber/core_ext/string.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def to_ber(code = 0x04)
1616
[code].pack('C') + raw_string.length.to_ber_length_encoding + raw_string
1717
end
1818

19+
##
20+
# Converts a string to a BER string but does *not* encode to UTF-8 first.
21+
# This is required for proper representation of binary data for Microsoft
22+
# Active Directory
23+
def to_ber_bin(code = 0x04)
24+
[code].pack('C') + length.to_ber_length_encoding + self
25+
end
26+
1927
def raw_utf8_encoded
2028
if self.respond_to?(:encode)
2129
# Strings should be UTF-8 encoded according to LDAP.

lib/net/ldap/filter.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class Net::LDAP::Filter
2424
##
2525
# Known filter types.
26-
FilterTypes = [ :ne, :eq, :ge, :le, :and, :or, :not, :ex ]
26+
FilterTypes = [ :ne, :eq, :ge, :le, :and, :or, :not, :ex, :bineq ]
2727

2828
def initialize(op, left, right) #:nodoc:
2929
unless FilterTypes.include?(op)
@@ -65,6 +65,23 @@ def eq(attribute, value)
6565
new(:eq, attribute, value)
6666
end
6767

68+
##
69+
# Creates a Filter object indicating a binary comparison.
70+
# this prevents the search data from being forced into a UTF-8 string.
71+
#
72+
# This is primarily used for Microsoft Active Directory to compare
73+
# GUID values.
74+
#
75+
# # for guid represented as hex charecters
76+
# guid = "6a31b4a12aa27a41aca9603f27dd5116"
77+
# guid_bin = [guid].pack("H*")
78+
# f = Net::LDAP::Filter.bineq("objectGUID", guid_bin)
79+
#
80+
# This filter does not perform any escaping.
81+
def bineq(attribute, value)
82+
new(:bineq, attribute, value)
83+
end
84+
6885
##
6986
# Creates a Filter object indicating extensible comparison. This Filter
7087
# object is currently considered EXPERIMENTAL.
@@ -399,6 +416,8 @@ def to_raw_rfc2254
399416
"!(#{@left}=#{@right})"
400417
when :eq
401418
"#{@left}=#{@right}"
419+
when :bineq
420+
"#{@left}=#{@right}"
402421
when :ex
403422
"#{@left}:=#{@right}"
404423
when :ge
@@ -508,6 +527,9 @@ def to_ber
508527
else # equality
509528
[@left.to_s.to_ber, unescape(@right).to_ber].to_ber_contextspecific(3)
510529
end
530+
when :bineq
531+
# make sure data is not forced to UTF-8
532+
[@left.to_s.to_ber, unescape(@right).to_ber_bin].to_ber_contextspecific(3)
511533
when :ex
512534
seq = []
513535

spec/unit/ber/ber_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
it "should properly encode strings encodable as UTF-8" do
8585
"teststring".encode("US-ASCII").to_ber.should == "\x04\nteststring"
8686
end
87+
it "should properly encode binary data strings using to_ber_bin" do
88+
# This is used for searching for GUIDs in Active Directory
89+
["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").to_ber_bin.should ==
90+
"\x04\x10" + "j1\xB4\xA1*\xA2zA\xAC\xA9`?'\xDDQ\x16"
91+
end
8792
it "should fail on strings that can not be converted to UTF-8" do
8893
error = Encoding::UndefinedConversionError
8994
lambda {"\x81".to_ber }.should raise_exception(error)

0 commit comments

Comments
 (0)