diff --git a/ldap.go b/ldap.go index 0dfbcc7..7933452 100644 --- a/ldap.go +++ b/ldap.go @@ -104,6 +104,15 @@ func searchByCn(myldap *MyLdap, baseDn, cn, class, attributes string) (*ldap.Sea 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) { var fFilter string var realBaseDn string @@ -203,19 +212,12 @@ func updateEntry(myldap *MyLdap, dn string, attributes map[string]interface{}) e delete(attributes, "dn") // 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 - 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") + sr, err := searchByDn(myldap, dn, "ALL") if err != nil { return err } if len(sr.Entries) == 0 { - return fmt.Errorf("Object %s not found", filter) - } else if len(sr.Entries) > 1 { - return fmt.Errorf("More than one object (%d) found with %s", len(sr.Entries), filter) + return fmt.Errorf("Object %s not found", dn) } actualAttrs := marshalResultToStrMap(sr)