-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap-find-user.go
78 lines (66 loc) · 1.45 KB
/
ldap-find-user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"log"
"fmt"
"github.com/go-ldap/ldap/v3"
)
func main() {
if err := findUser(); err != nil {
log.Fatal(err)
}
}
func findUser() error {
ldapURL := "ldap://0.0.0.0:389"
adminusername := "cn=admin,dc=example,dc=org"
adminpassword := "adminpassword"
baseDN := "dc=example,dc=org"
l, err := ldap.DialURL(ldapURL)
if err != nil {
return err
}
l.Start()
err = l.Bind(adminusername, adminpassword)
if err != nil {
return err
}
searchAll := &ldap.SearchRequest{
BaseDN: baseDN,
Scope: ldap.ScopeWholeSubtree,
Filter: "(objectClass=*)",
}
fmt.Println("Search: (objectClass=*)")
sr, err := l.Search(searchAll)
if err != nil {
return err
}
printResult(sr.Entries)
fmt.Println("")
fmt.Println("(&(objectClass=inetOrgPerson)(uid=user01))")
searchSpecificUser := &ldap.SearchRequest{
BaseDN: baseDN,
Scope: ldap.ScopeWholeSubtree,
Filter: "(&(objectClass=inetOrgPerson)(uid=user01))",
}
sr, err = l.Search(searchSpecificUser)
if err != nil {
return err
}
if len(sr.Entries) != 1 {
fmt.Printf("%+v\n", sr)
log.Fatal("User does not exist or too many entries returned")
} else {
fmt.Printf("****** FOUND! ******")
}
return nil
}
func printResult(entries []*ldap.Entry) {
for _, entry := range entries {
fmt.Println("DN:", entry.DN)
for _, attr := range entry.Attributes {
for i := 0; i < len(attr.Values); i++ {
fmt.Printf("%s: %s\n", attr.Name, attr.Values[i])
}
}
fmt.Println()
}
}