Re: (Net::LDAP) Automatically convert attributes into utf8 when writting

pe rl <[email protected]> Tue, 25 Aug 2015 13:37:15 +0200
Newsgroups gmane.comp.lang.perl.modules.ldap
Message-ID <[email protected]>
Thank you, I already knew utf8::encode() and utf8::decode().

They are not necessary when reading/searching in the ldap server, since=20
Net::LDAP already has a "raw" option in the constructor to automatically
encode/decode strings. It is working for us, and the only change required=
 has=20
been to add the "raw" option to the constructor.

The problem appears when writting to the ldap server. I have started to m=
odify
our code with utf8::encode(), by adding it to every attribute in all of o=
ur
functions. The problem is that it is very inefficient, since I will have =
to=20
modify every attribute that appears in our programs. We have a lot of fun=
ctions=20
that create/modify/delete entries in the ldap server, so I will have to c=
hange=20
a lot of code to manually encode attribs to utf8, and then test all of th=
e=20
changes.

It would be much simpler if Net::LDAP would encode automatically the
attributes by using the regex passed into the "raw" option of the constru=
ctor,
since the changes in our programs would be zero. In my first message I pa=
sted
the code in Net::LDAP that encodes the attributes when reading from the l=
dap
server, and it looks simple. Probably encoding attributes when writting t=
o the
ldap server could be simple as well. Probably the changes required in
Net::LDAP are minimal compared to the changes required in our code.

Thank you


25.08.2015, 13:04, "Keutel, Jochen (mlists)" <[email protected]>:
> Hello,
> =9A=9Ainstead of patching Net::LDAP you should use utf8::encode() and
> utf8::decode() in your perl code.
>
> See http://perldoc.perl.org/5.10.1/utf8.html .
>
> Regards, Jochen.
>
> Am 25.08.2015 um 12:54 schrieb pe rl:
>> =9AHi, we are using an old version of Net::LDAP (0.39) in an old perl
>> =9Ainstallation (5.10.1). Recently we have changed the ldap server, an=
d now it
>> =9Auses utf8 in the entry attributes, so we are getting problems with =
reading and
>> =9Awritting attributes with Net::LDAP.
>>
>> =9ATo solve it, I have read the documentation of Net::LDAP 0.39 ( at
>> =9Ahttps://metacpan.org/pod/release/GBARR/perl-ldap-0.39/lib/Net/LDAP.=
pod ), and
>> =9AI see there is an option ("raw") in the constructor to indicate att=
ributes
>> =9Athat should be treated as utf8. I have tested it, and it works por =
reading
>> =9Afrom the ldap server (attribute strings are marked as utf8, so they=
 a treated
>> =9Acorrectly by our programs), but it doesn't work for writting (our l=
atin1
>> =9Astrings are not being converted automatically into utf8 before bein=
g sent).
>>
>> =9ASo it looks like the "raw" option works for reading but not for wri=
tting. Is
>> =9Athere any quick way to use the "raw" regex also for writting? The a=
lternative
>> =9Awould be to review all of our code and manually encode all the valu=
es to utf8
>> =9Abefore passing them to Net::LDAP, but it would mean a lot of work. =
It would be
>> =9Abetter if we could change the Net::LDAP library itself to convert
>> =9Aautomatically attributes into utf8, same as for reading. For exampl=
e, a new
>> =9Aoption "raw_for_writing" could be added to the constructor:
>>
>> =9A=9A=9A=9A=9A=9A=9A$ldap =3D Net::LDAP->new(
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=
=9A=9A=9A=9A=9A=9A=9A=9A=9A$server,
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=
=9A=9A=9A=9A=9A=9A=9A=9A=9Aport =3D> $port,
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=
=9A=9A=9A=9A=9A=9A=9A=9A=9Araw =3D> qr/(?i:^jpegPhoto|;binary)/,
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=
=9A=9A=9A=9A=9A=9A=9A=9A=9Araw_for_writing =3D> 1,
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=
=9A=9A=9A=9A=9A)
>>
>> =9AI see that the automatic conversion for reading is done at the "dec=
ode"
>> =9Afunction of "Entry.pm":
>>
>> =9A=9A=9A=9A=9Asub decode {
>>
>> =9A=9A=9A=9A=9A=9A=9A...
>>
>> =9A=9A=9A=9A=9A=9A=9Aif (CHECK_UTF8 && $arg{raw}) {
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A$result->{objectName} =3D Encode::decode_ut=
f8($result->{objectName})
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9Aif ('dn' !~ /$arg{raw}/);
>>
>> =9A=9A=9A=9A=9A=9A=9A...
>>
>> =9A=9A=9A=9A=9A=9A=9A=9A=9Aforeach my $elem (@{$self->{asn}{attributes=
}}) {
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9Amap { $_ =3D Encode::decode_utf8($_) =
} @{$elem->{vals}}
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9Aif ($elem->{type} !~ /$arg{raw}=
/);
>> =9A=9A=9A=9A=9A=9A=9A=9A=9A}
>> =9A=9A=9A=9A=9A=9A=9A}
>>
>> =9AAnd I see that there is an "encode" function in "Entry.pm", that do=
esn't do
>> =9Athe magic:
>>
>> =9A=9A=9A=9A=9Asub encode {
>> =9A=9A=9A=9A=9A=9A=9A$LDAPEntry->encode( shift->{asn} );
>> =9A=9A=9A=9A=9A}
>>
>> =9AWould it be sufficient to add some similar code to the "Entry::enco=
de"
>> =9Afunction in order to automatically encode attributes to utf8 before=
 being sent?
>>
>> =9AAny suggestion to reduce the amount of code to be changed in our pr=
ograms?
>>
>> =9AThank you