Skip to content

Commit 9df2347

Browse files
committed
Merge pull request #123 from mattias-ohlsson/master
handle base64 encoded dn attr
2 parents 0dd8f56 + 2237970 commit 9df2347

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/net/ldap/dataset.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ def read_ldif(io)
136136
elsif line =~ /^version:[\s]*([0-9]+)$/i
137137
ds.version = $1
138138
yield :version, line if block_given?
139-
elsif line =~ /^dn:[\s]*/i
140-
dn = $'
139+
elsif line =~ /^dn:([\:]?)[\s]*/i
140+
# $1 is a colon if the dn-value is base-64 encoded
141+
# $' is the dn-value
142+
# Avoid the Base64 class because not all Ruby versions have it.
143+
dn = ($1 == ":") ? $'.unpack('m').shift : $'
141144
ds[dn] = Hash.new { |k,v| k[v] = [] }
142145
yield :dn, dn if block_given?
143146
elsif line.empty?

test/test_ldif.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ def test_ldif_tab_is_not_continuation
5353
assert_equal(true, ds.has_key?("key"))
5454
end
5555

56+
def test_ldif_with_base64_dn
57+
str = "dn:: Q049QmFzZTY0IGRuIHRlc3QsT1U9VGVzdCxPVT1Vbml0cyxEQz1leGFtcGxlLERDPWNvbQ==\r\n\r\n"
58+
ds = Net::LDAP::Dataset::read_ldif(StringIO.new(str))
59+
assert_equal(true, ds.has_key?("CN=Base64 dn test,OU=Test,OU=Units,DC=example,DC=com"))
60+
end
61+
62+
def test_ldif_with_base64_dn_and_continuation_lines
63+
str = "dn:: Q049QmFzZTY0IGRuIHRlc3Qgd2l0aCBjb250aW51YXRpb24gbGluZSxPVT1UZXN0LE9VPVVua\r\n XRzLERDPWV4YW1wbGUsREM9Y29t\r\n\r\n"
64+
ds = Net::LDAP::Dataset::read_ldif(StringIO.new(str))
65+
assert_equal(true, ds.has_key?("CN=Base64 dn test with continuation line,OU=Test,OU=Units,DC=example,DC=com"))
66+
end
67+
5668
# TODO, INADEQUATE. We need some more tests
5769
# to verify the content.
5870
def test_ldif

0 commit comments

Comments
 (0)