MCPcopy Create free account
hub / github.com/Synzack/ldapper / NetGroupQuery

Function NetGroupQuery

Queries/QueryNetGroup.go:12–71  ·  view source on GitHub ↗
(groupInput string, baseDN string, conn *ldap.Conn)

Source from the content-addressed store, hash-verified

10)
11
12func NetGroupQuery(groupInput string, baseDN string, conn *ldap.Conn) ([]string, string) {
13
14 var queryResult []string
15 var description string
16
17 query := fmt.Sprintf("(&(objectClass=group)(cn=%s))", groupInput) // Build the query
18 searchReq := Globals.LdapSearch(baseDN, query) // Search the baseDN
19 result, err := conn.Search(searchReq) // Execute the search
20 if err != nil {
21 fmt.Printf("Query error, %s", err)
22 }
23
24 // if LdapSearch returns information
25 if len(result.Entries) > 0 {
26 if len(result.Entries[0].GetAttributeValues("description")) > 0 {
27 description = result.Entries[0].GetAttributeValues("description")[0]
28 }
29
30 for _, dn := range result.Entries[0].GetAttributeValues("member") {
31 regexCN := regexp.MustCompile(`CN=(.*?),[A-Z]{2}=`)
32 match := regexCN.FindAllStringSubmatch(dn, -1)
33 cn := match[0][1]
34
35 cn = strings.Replace(cn, "\\", "", -1)
36 cn = strings.Replace(cn, "(", ldap.EscapeFilter("("), -1) //Escape parenthesis
37 cn = strings.Replace(cn, ")", ldap.EscapeFilter(")"), -1)
38
39 query = fmt.Sprintf("(cn=%s)", cn) // build the query
40 searchReq2 := Globals.LdapSearch(baseDN, query) // search for username
41
42 result2, err := conn.Search(searchReq2) // get results
43 if err != nil {
44 fmt.Printf("Query error, %s", err)
45 }
46
47 if len(result2.Entries[0].GetAttributeValues("sAMAccountName")) > 0 {
48 samAccountName := result2.Entries[0].GetAttributeValues("sAMAccountName")[0] // get sAMAccountName
49 username := strings.Replace(samAccountName, "sAMAccountNAme: ", "", -1) // remove all sAMAccountName: from string
50
51 //Check if group
52 objectClass := result2.Entries[0].GetAttributeValues("objectClass")
53 isGroup := false
54
55 for i := 0; i < len(objectClass); i++ {
56 if result2.Entries[0].GetAttributeValues("objectClass")[i] == "group" {
57 isGroup = true
58 }
59 }
60 if isGroup {
61 username = fmt.Sprintf("%s (Group)", username)
62 }
63
64 //Append Result Array
65 queryResult = append(queryResult, username)
66 }
67 }
68 }
69

Callers 2

ReturnNestedGroupQueryFunction · 0.85
ReturnGroupQueryFunction · 0.85

Calls 1

LdapSearchFunction · 0.92

Tested by

no test coverage detected