go-ldap / ldap

Basic LDAP v3 functionality for the GO programming language.
Other
2.19k stars 352 forks source link

Binary value not automatically decoded into base64 string #494

Closed michele-deluca closed 3 months ago

michele-deluca commented 3 months ago

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?

cpuschma commented 3 months ago

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: https://github.com/go-ldap/ldap/blob/d13d063a5d3976f42ce0170511d21e5b42221910/v3/search.go#L142-L143

michele-deluca commented 3 months ago

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?

cpuschma commented 3 months ago

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.