supported base64 encoding for binary attribute values

This commit is contained in:
blackhedd 2006-08-29 00:24:29 +00:00
parent f1e9861b50
commit 1deab245d6
2 changed files with 41 additions and 7 deletions

View file

@ -9,6 +9,8 @@
* Changed Net::LDAP::Entry so it can be marshalled and unmarshalled. * Changed Net::LDAP::Entry so it can be marshalled and unmarshalled.
Thanks to an anonymous feature requester who only left the name Thanks to an anonymous feature requester who only left the name
"Jammy." "Jammy."
* Added support for binary values in Net::LDAP::Entry LDIF conversions
and marshalling.
== Net::LDAP 0.0.4: August 15, 2006 == Net::LDAP 0.0.4: August 15, 2006
* Undeprecated Net::LDAP#modify. Thanks to Justin Forder for * Undeprecated Net::LDAP#modify. Thanks to Justin Forder for

View file

@ -27,6 +27,7 @@
# #
require 'base64'
module Net module Net
@ -157,18 +158,25 @@ class LDAP
def to_ldif def to_ldif
ary = [] ary = []
ary << "dn: #{dn}\n" ary << "dn: #{dn}\n"
v2 = "" # temp value, save on GC
each_attribute do |k,v| each_attribute do |k,v|
v.each {|v1| unless k == :dn
ary << "#{k}: #{v1}\n" unless k == :dn v.each {|v1|
} v2 = if (k == :userpassword) || is_attribute_value_binary?(v1)
": #{Base64.encode64(v1).chomp}"
else
" #{v1}"
end
ary << "#{k}:#{v2}\n"
}
end
end end
ary << "\n" ary << "\n"
ary.join ary.join
end end
#-- #--
# TODO, doesn't support binary representations yet (:: notation), # TODO, doesn't support broken lines.
# and it doesn't handle broken lines.
# It generates a SINGLE Entry object from an incoming LDIF stream # It generates a SINGLE Entry object from an incoming LDIF stream
# which is of course useless for big LDIF streams that encode # which is of course useless for big LDIF streams that encode
# many objects. # many objects.
@ -183,8 +191,12 @@ class LDAP
entry = Entry.new entry = Entry.new
ldif.split(/\r?\n/m).each {|line| ldif.split(/\r?\n/m).each {|line|
break if line.length == 0 break if line.length == 0
if line =~ /\A([\w]+)::?[\s]*/ if line =~ /\A([\w]+):(:?)[\s]*/
entry[$1] = $' entry[$1] <<= if $2 == ':'
Base64.decode64($')
else
$'
end
end end
} }
entry.dn ? entry : nil entry.dn ? entry : nil
@ -219,6 +231,26 @@ class LDAP
def write def write
end end
#--
# Internal convenience method. It seems like the standard
# approach in most LDAP tools to base64 encode an attribute
# value if its first or last byte is nonprintable, or if
# it's a password.
def is_attribute_value_binary? value
v = value.to_s
[v[0],v[-1]].each {|byt|
if byt.is_a?(Fixnum) and (byt < 33 or byt > 126)
return true
end
}
if v[0..0] == ':' or v[0..0] == '<'
return true
end
false
end
private :is_attribute_value_binary?
end # class Entry end # class Entry