Add searchByDn()

This commit is contained in:
yo 2022-11-13 16:48:22 +01:00
parent d557521a5c
commit cb6a7ffaee

20
ldap.go
View File

@ -104,6 +104,15 @@ func searchByCn(myldap *MyLdap, baseDn, cn, class, attributes string) (*ldap.Sea
return doLdapSearch(myldap, baseDn, filter, class, attributes) return doLdapSearch(myldap, baseDn, filter, class, attributes)
} }
func searchByDn(myldap *MyLdap, dn, attributes string) (*ldap.SearchResult, error) {
// We cant search for a full DN, so separate cn and base dn, then remove basedn as doLdapSearch already append it
filter := strings.Split(dn, ",")[0]
rem := strings.Split(dn, ",")[1:]
bdn := strings.Join(rem, ",")
bdn = strings.Replace(bdn, fmt.Sprintf(",%s", myldap.BaseDN), "", 1)
return doLdapSearch(myldap, bdn, filter, "ALL", "ALL")
}
func doLdapSearch(myldap *MyLdap, baseDn, filter, class, attributes string) (*ldap.SearchResult, error) { func doLdapSearch(myldap *MyLdap, baseDn, filter, class, attributes string) (*ldap.SearchResult, error) {
var fFilter string var fFilter string
var realBaseDn string var realBaseDn string
@ -203,19 +212,12 @@ func updateEntry(myldap *MyLdap, dn string, attributes map[string]interface{}) e
delete(attributes, "dn") delete(attributes, "dn")
// First get the current object so we can build a list of add, modify, delete attributes // First get the current object so we can build a list of add, modify, delete attributes
// We cant search for a full DN, so separate cn and base dn, then remove basedn as doLdapSearch already append it sr, err := searchByDn(myldap, dn, "ALL")
filter := strings.Split(dn, ",")[0]
rem := strings.Split(dn, ",")[1:]
bdn := strings.Join(rem, ",")
bdn = strings.Replace(bdn, fmt.Sprintf(",%s", myldap.BaseDN), "", 1)
sr, err := doLdapSearch(myldap, bdn, filter, "ALL", "ALL")
if err != nil { if err != nil {
return err return err
} }
if len(sr.Entries) == 0 { if len(sr.Entries) == 0 {
return fmt.Errorf("Object %s not found", filter) return fmt.Errorf("Object %s not found", dn)
} else if len(sr.Entries) > 1 {
return fmt.Errorf("More than one object (%d) found with %s", len(sr.Entries), filter)
} }
actualAttrs := marshalResultToStrMap(sr) actualAttrs := marshalResultToStrMap(sr)