bearded / ruby-ldap

Ruby/LDAP is an extension library for Ruby. It provides the interface to some LDAP libraries (e.g. OpenLDAP, Netscape SDK and Active Directory). The common API for application development is described in RFC1823 and is supported by Ruby/LDAP.
http://rubyforge.org/projects/ruby-ldap/
Other
66 stars 34 forks source link

Results from #search are not marked as UTF-8 in Ruby 1.9.3 #12

Open keltia opened 12 years ago

keltia commented 12 years ago

When trying to convert into YAML the values I get from my LDAP server through Treequel, strings are not marked as UTF-8 in Ruby 1.9.3 resulting in everything marked as "BINARY":

522 [16:29] roberto@roberto-aw:gems/ruby-ldap-0.9.12> curl http://localhost:3000/user/roberto.yaml

---
!binary "a3Nu":
- !binary |-
  Uk9CRVJU
!binary "a2dpdmVubmFtZQ==":
- !binary |-
  T2xsaXZpZXI=
!binary "dGVsZXBob25lbnVtYmVy":
- !binary |-
  KzMzICgwKTEgNjk4ODcyOTA=
!binary "ZXVyb2NvbnRyb2xyb29taWQ=":
- !binary |-
  Q0MuMjE=
!binary "bWFpbA==":
- !binary |-
  b2xsaXZpZXIucm9iZXJ0QGV1cm9jb250cm9sLmludA==
!binary "dWlk":
- !binary |-
  cm9iZXJ0bw==
!binary "ZXVyb2NvbnRyb2x1bml0Y29kZQ==":
- !binary |-
  RFIvUE0vRlJN
!binary "ZXVyb2NvbnRyb2xncmFkZQ==":
- !binary |-
  QUQxMA==
!binary "ZXVyb2NvbnRyb2xzdGFydGRhdGU=":
- !binary |-
  MTk5Ni8wNy8wMQ==
!binary "ZW1wbG95ZWVudW1iZXI=":
- !binary |-
  MzAzNQ==
!binary "ZXVyb2NvbnRyb2xzaXRlaWQ=":
- !binary |-
  QnJldGlnbnk=

instead of the UTF8 strings.

JSON output through again Treequel is fine:

524 [16:29] roberto@roberto-aw:gems/ruby-ldap-0.9.12> curl http://localhost:3000/user/roberto.json
{"persons":[{"person":["roberto",{"ksn":["ROBERT"],"kgivenname":["Ollivier"],"telephonenumber":["+33 (0)1 69887290"],"eurocontrolroomid":["CC.21"],"mail":["ollivier.robert@eurocontrol.int"],"uid":["roberto"],"eurocontrolunitcode":["DR/PM/FRM"],"eurocontrolgrade":["AD10"],"eurocontrolstartdate":["1996/07/01"],"employeenumber":["3035"],"eurocontrolsiteid":["Bretigny"]}]}]}%          

NOTE: what is running on port 3000 is my own LDAP proxy, built with Sinatra/Treequel/ruby-ldap: https://bitbucket.org/keltia/ldap-proxy

ged commented 12 years ago

I've confirmed that data comes out of Ruby-LDAP as ASCII-8BIT, which is why Treequel's strings have that encoding:

[1] pry(main)> require 'ldap'
=> true
[2] pry(main)> c = LDAP::SSLConn.new( 'ldap.example.com', 389, true )
=> #<LDAP::SSLConn:0x007fc7c30f8420
 @args=["ldap.example.com", 389, true],
 @sasl_quiet=false>
[3] pry(main)> res = c.search_ext2( 'dc=example,dc=com', LDAP::LDAP_SCOPE_SUBTREE, '(uid=mgranger)' )
=> [{"dn"=>["uid=mgranger,ou=People,dc=example,dc=com"], [...]}]
[4] pry(main)> res.first['cn']
=> ["Michael Granger"]
[5] pry(main)> res.first['cn'].first.encoding
=> #<Encoding:ASCII-8BIT>
ghost commented 12 years ago

hi guys,

I found example of solving encoding problem here and I think that it can be applied to str here

but problem is that I'm not a C programmer and I'm not sure that it is correct way for solving this problem.

what do you think?

ghost commented 12 years ago

looks like it is bad idea... :)

1.9.3p125 :001 > require 'ldap'
 => true 
1.9.3p125 :002 > c = LDAP::Conn.new( 'localhost', 389 )
 => #<LDAP::Conn:0x007fafcb06f810> 
1.9.3p125 :003 > res = c.search_ext2( 'dc=localhost,dc=com', LDAP::LDAP_SCOPE_SUBTREE, '(cn=admin)' )
 => [{"dn"=>["cn=admin,dc=localhost,dc=com"], "objectClass"=>["\u0005\x81\u0004"], "cn"=>["\u0005A\u0001"]}] 
1.9.3p125 :004 > res.first['cn']
 => ["\u0005A\u0001"] 
1.9.3p125 :005 > res.first['cn'].first.encoding
 => #<Encoding:UTF-8>