Binary value not automatically decoded into base64 string
michele-deluca opened this issue · comments
When using the methods "entry.GetAttributeValue" and "entry.GetAttributeValues" that returns a string and string array of values, if the original value in ldap was binary was returned a string of character that wa impossible to write.
the better solution is to encode this into a base64 string.
here an example of workaround that i made in my code:
::::::::::::::::::::::::::::
func containsBinaryData(s string) bool {
for _, char := range s {
if char < 32 || char > 126 {
// Non-printable character found (likely binary)
return true
}
}
return false
}
::::::::::::::::::::::::::::::
for _, entry := range result.Entries {
value := entry.GetAttributeValue("....")
if containsBinaryData(value) {
fmt.Println("Attribute is binary")
value = base64.StdEncoding.EncodeToString([]byte(value))
} else {
fmt.Println("Attribute is not binary")
}
fmt.Printf("%s\n", value)
}
::::::::::::::::::::::::::::::::::
it is possible to inglobe it into this functions to check if is binary before return its?
This is too dependant on the type of LDAP attribute you're trying to handle. Some attributes like userAccountControl
or msDS-LockoutObservationWindow
Windows Active Directory are bitshifting attributes using an uint64. The byte sequences returned from this would be out of range for your function and be wronlgy encoded as base64. This would also break backwards compatibility.
You can always fallback to the raw value if the string doesn't suit your needs and handle it the way you want to:
Lines 142 to 143 in d13d063
Ok, but I don't know if is a binary and I need to extract it in a batch and save the attribute to a backup... how we can check if is binary?
Then you're good to go to save it "as it is" if it's intended for a backup, as the add and modify operation also takes strings, and export it using the method of your choice, such as encoding/json or encoding/gob. You can simply pass the SearchResult to the encoder.
// Edit: Also please note that depending on the directory server you're using, ACLs may not be included in your export and are stored separately.