forked from bradleypeabody/godap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (53 loc) · 1.65 KB
/
main.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
package main
import (
"flag"
"github.com/JonasScharpf/godap/godap"
"log"
"strings"
)
func main() {
addr := flag.String("addr", ":389", "HTTP network address with port")
flag.Parse()
hs := make([]godap.LDAPRequestHandler, 0)
// use a LDAPBindFuncHandler to provide a callback function to respond
// to bind requests
hs = append(hs, &godap.LDAPBindFuncHandler{LDAPBindFunc: func(binddn string, bindpw []byte) bool {
if strings.Contains(binddn, "cn=user,") && string(bindpw) == "password" {
return true
}
return false
}})
// use a LDAPSimpleSearchFuncHandler to reply to search queries
hs = append(hs, &godap.LDAPSimpleSearchFuncHandler{LDAPSimpleSearchFunc: func(req *godap.LDAPSimpleSearchRequest) []*godap.LDAPSimpleSearchResultEntry {
ret := make([]*godap.LDAPSimpleSearchResultEntry, 0, 1)
// here we produce a single search result that matches whatever
// they are searching for
if req.FilterAttr == "uid" {
ret = append(ret, &godap.LDAPSimpleSearchResultEntry{
DN: "cn=" + req.FilterValue + "," + req.BaseDN,
Attrs: map[string]interface{}{
"sn": req.FilterValue,
"cn": req.FilterValue,
"uid": req.FilterValue,
"homeDirectory": "/home/" + req.FilterValue,
"objectClass": []string{
"top",
"posixAccount",
"inetOrgPerson",
},
},
})
} else {
log.Println("Currently nothing else than a '(uid=<name>)' query is handled")
}
return ret
}})
s := &godap.LDAPServer{
Handlers: hs,
}
log.Println("Starting mock LDAP server on", *addr)
err := s.ListenAndServe(*addr)
if err != nil {
log.Printf("Failed to start server. Error : %v", err)
}
}