Ldap fixes

Fixes for #5517 #5518 #5520 and #5521

Please review @DeepDiver1975 @PVince81 @bantu or others, thx.