Skip to content

Commit 1deab24

Browse files
author
blackhedd
committed
supported base64 encoding for binary attribute values
1 parent f1e9861 commit 1deab24

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* Changed Net::LDAP::Entry so it can be marshalled and unmarshalled.
1010
Thanks to an anonymous feature requester who only left the name
1111
"Jammy."
12+
* Added support for binary values in Net::LDAP::Entry LDIF conversions
13+
and marshalling.
1214

1315
== Net::LDAP 0.0.4: August 15, 2006
1416
* Undeprecated Net::LDAP#modify. Thanks to Justin Forder for

lib/net/ldap/entry.rb

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#
2828

2929

30+
require 'base64'
3031

3132

3233
module Net
@@ -157,18 +158,25 @@ def each
157158
def to_ldif
158159
ary = []
159160
ary << "dn: #{dn}\n"
161+
v2 = "" # temp value, save on GC
160162
each_attribute do |k,v|
161-
v.each {|v1|
162-
ary << "#{k}: #{v1}\n" unless k == :dn
163-
}
163+
unless k == :dn
164+
v.each {|v1|
165+
v2 = if (k == :userpassword) || is_attribute_value_binary?(v1)
166+
": #{Base64.encode64(v1).chomp}"
167+
else
168+
" #{v1}"
169+
end
170+
ary << "#{k}:#{v2}\n"
171+
}
172+
end
164173
end
165174
ary << "\n"
166175
ary.join
167176
end
168177

169178
#--
170-
# TODO, doesn't support binary representations yet (:: notation),
171-
# and it doesn't handle broken lines.
179+
# TODO, doesn't support broken lines.
172180
# It generates a SINGLE Entry object from an incoming LDIF stream
173181
# which is of course useless for big LDIF streams that encode
174182
# many objects.
@@ -183,8 +191,12 @@ def from_single_ldif_string ldif
183191
entry = Entry.new
184192
ldif.split(/\r?\n/m).each {|line|
185193
break if line.length == 0
186-
if line =~ /\A([\w]+)::?[\s]*/
187-
entry[$1] = $'
194+
if line =~ /\A([\w]+):(:?)[\s]*/
195+
entry[$1] <<= if $2 == ':'
196+
Base64.decode64($')
197+
else
198+
$'
199+
end
188200
end
189201
}
190202
entry.dn ? entry : nil
@@ -219,6 +231,26 @@ def method_missing *args, &block # :nodoc:
219231
def write
220232
end
221233

234+
235+
#--
236+
# Internal convenience method. It seems like the standard
237+
# approach in most LDAP tools to base64 encode an attribute
238+
# value if its first or last byte is nonprintable, or if
239+
# it's a password.
240+
def is_attribute_value_binary? value
241+
v = value.to_s
242+
[v[0],v[-1]].each {|byt|
243+
if byt.is_a?(Fixnum) and (byt < 33 or byt > 126)
244+
return true
245+
end
246+
}
247+
if v[0..0] == ':' or v[0..0] == '<'
248+
return true
249+
end
250+
false
251+
end
252+
private :is_attribute_value_binary?
253+
222254
end # class Entry
223255

224256

0 commit comments

Comments
 (0)