//TODO refactor variable names func GetFromLDAP(connect *ldap.Conn, LDAPBaseDN, LDAPFilter string, LDAPAttribute []string, LDAPPage uint32) *[]LDAPElement { searchRequest := ldap.NewSearchRequest(LDAPBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, LDAPFilter, LDAPAttribute, nil) sr, err := connect.SearchWithPaging(searchRequest, LDAPPage) CheckForError(err) //fmt.Println(len(sr.Entries)) ADElements := []LDAPElement{} for _, entry := range sr.Entries { NewADEntity := new(LDAPElement) NewADEntity.DN = entry.DN for _, attrib := range entry.Attributes { NewADEntity.attributes = append(NewADEntity.attributes, keyvalue{attrib.Name: attrib.Values}) } ADElements = append(ADElements, *NewADEntity) } return &ADElements }
func GetFromAD(connect *ldap.Conn, ADBaseDN, ADFilter string, ADAttribute []string, ADPage uint32) *[]LDAPElement { //sizelimit in searchrequest is the limit, which throws an error when the number of results exceeds the limit. searchRequest := ldap.NewSearchRequest(ADBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, ADFilter, ADAttribute, nil) sr, err := connect.SearchWithPaging(searchRequest, ADPage) CheckForError(err) //fmt.Println(len(sr.Entries)) ADElements := []LDAPElement{} for _, entry := range sr.Entries { NewADEntity := new(LDAPElement) NewADEntity.DN = entry.DN for _, attrib := range entry.Attributes { NewADEntity.attributes = append(NewADEntity.attributes, keyvalue{attrib.Name: attrib.Values}) } ADElements = append(ADElements, *NewADEntity) } return &ADElements }
func CheckAttributes(LdapConnection *ldap.Conn, LdapEntry, ADEntry *ldap.AddRequest) { var ADMapAggregated []MapADandLDAP var LDAPMapAggregated []MapADandLDAP for _, adEntries := range ADEntry.Attributes { if adEntries.Type == "memberOf" { adEntries.Vals = *ConvertAttributesToLower(&adEntries.Vals) } sort.Strings(adEntries.Vals) ADMapped := MapADandLDAP{adEntries.Type: adEntries.Vals} ADMapAggregated = append(ADMapAggregated, ADMapped) } for _, ldapEntries := range LdapEntry.Attributes { sort.Strings(ldapEntries.Vals) LDAPMapped := MapADandLDAP{ldapEntries.Type: ldapEntries.Vals} LDAPMapAggregated = append(LDAPMapAggregated, LDAPMapped) } Info.Println("Got from AD", ADMapAggregated) Info.Println("Got from LD", LDAPMapAggregated) if reflect.DeepEqual(ADMapAggregated, LDAPMapAggregated) == true { Info.Println("Both entries matches, passing...") } else { Info.Println("CHANGE DETECTED") Info.Println("AD -> ", ADMapAggregated) Info.Println("LD -> ", LDAPMapAggregated) delete := ldap.NewDelRequest(LdapEntry.DN, []ldap.Control{}) err := LdapConnection.Del(delete) if err != nil { Error.Println(err) } else { Info.Println(*delete, "Deleted") } err = LdapConnection.Add(ADEntry) if err != nil { Error.Println(err) } else { Info.Println(*ADEntry, "Added to ldap") } } }
func InitialPopulateToLdap(ADElements *[]LDAPElement, connectLDAP *ldap.Conn, ReplaceAttributes, MapAttributes *ini.Section, ReturnData bool) []*ldap.AddRequest { var ReturnConvertedData []*ldap.AddRequest userObjectClass, err := ReplaceAttributes.GetKey("userObjectClass") CheckForError(err) groupObjectClass, err := ReplaceAttributes.GetKey("groupObjectClass") CheckForError(err) mapping := make(map[string]string) //mapping of AD values to ldap values for _, i := range MapAttributes.KeyStrings() { tmpvar, err := MapAttributes.GetKey(i) CheckForError(err) mapping[i] = tmpvar.String() } //keys = AD attributes, values = ldap values to which it would be mapped Info.Println("Creating mappings for the following attributes,", mapping) Info.Println("Userobjectclass of AD will be replaced with", userObjectClass) Info.Println("Groupobjectclass of AD will be replaced with", groupObjectClass) for _, i := range *ADElements { //fmt.Println(i.DN) Add := ldap.NewAddRequest(i.DN) for _, maps := range i.attributes { for key, value := range maps { if key == "objectClass" { if StringInSlice("user", value.([]string)) { //Add.Attribute(key, []string{"posixAccount", "top", "inetOrgPerson"}) Add.Attribute(key, userObjectClass.Strings(",")) continue } if StringInSlice("group", value.([]string)) { Add.Attribute(key, groupObjectClass.Strings(",")) continue } } mappingvalue, ok := mapping[key] //uid := maps["uid"] if ok == true { if mappingvalue == "memberUid" { //members := memberTomemberUid(&value) Add.Attribute(mappingvalue, memberTomemberUid(&value, ADElements)) continue } Add.Attribute(mappingvalue, value.([]string)) continue } Add.Attribute(key, value.([]string)) } } // Info.Println(Add) if ReturnData == false { err := connectLDAP.Add(Add) Error.Println(err) } else { ReturnConvertedData = append(ReturnConvertedData, Add) } //fmt.Println(Add) //fmt.Println(err) } return ReturnConvertedData }