-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
166 lines (145 loc) · 5 KB
/
handler.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/asn1"
"encoding/hex"
"log"
"strings"
ldap "github.com/butonic/ldapserver"
"github.com/lor00x/goldap/message"
)
func handleNotFound(w ldap.ResponseWriter, r *ldap.Message) {
switch r.ProtocolOpType() {
case ldap.ApplicationBindRequest:
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
res.SetDiagnosticMessage("Default binding behavior set to return Success")
w.Write(res)
default:
res := ldap.NewResponse(ldap.LDAPResultUnwillingToPerform)
res.SetDiagnosticMessage("Operation not implemented by server")
w.Write(res)
}
}
func handleAbandon(w ldap.ResponseWriter, m *ldap.Message) {
var req = m.GetAbandonRequest()
// retreive the request to abandon, and send a abort signal to it
if requestToAbandon, ok := m.Client.GetMessageByID(int(req)); ok {
requestToAbandon.Abandon()
log.Printf("Abandon signal sent to request processor [messageID=%d]", int(req))
}
}
func handleBind(w ldap.ResponseWriter, m *ldap.Message) {
r := m.GetBindRequest()
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
if r.AuthenticationChoice() == "simple" {
password := jsonParsed.Search(string(r.Name()), "userpassword").Data()
log.Printf("User=%s, password=%v", string(r.Name()), password)
if password == nil {
log.Printf("User=%s, has no userpassword", string(r.Name()))
} else if string(r.AuthenticationSimple()) == password.(string) {
w.Write(res)
return
}
log.Printf("Bind failed User=%s, Pass=%#v", string(r.Name()), r.Authentication())
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
res.SetDiagnosticMessage("invalid credentials")
} else {
res.SetResultCode(ldap.LDAPResultUnwillingToPerform)
res.SetDiagnosticMessage("Authentication choice not supported")
}
w.Write(res)
}
func handleExtended(w ldap.ResponseWriter, m *ldap.Message) {
r := m.GetExtendedRequest()
log.Printf("Extended request received, name=%s", r.RequestName())
log.Printf("Extended request received, value=%x", r.RequestValue())
res := ldap.NewExtendedResponse(ldap.LDAPResultSuccess)
w.Write(res)
}
func handleWhoAmI(w ldap.ResponseWriter, m *ldap.Message) {
res := ldap.NewExtendedResponse(ldap.LDAPResultSuccess)
w.Write(res)
}
type SearchControlValue struct {
Size int
Cookie string
}
func addAttributeValue(e *message.SearchResultEntry, attribute message.LDAPString, values []string) {
//log.Printf("Adding Attribute %s with values %s", attribute, values)
attributeValues := make([]message.AttributeValue, len(values))
for i, value := range values {
if strings.HasPrefix(value, "{hex}") {
bytes, err := hex.DecodeString(value[5:])
if err != nil {
log.Printf("could not decode hex string %s to bytes", value)
}
log.Printf("Adding Attribute %s with hex value %s", attribute, value)
attributeValues[i] = message.AttributeValue(bytes)
} else {
log.Printf("Adding Attribute %s with value %s", attribute, value)
attributeValues[i] = message.AttributeValue(value)
}
}
e.AddAttribute(message.AttributeDescription(attribute), attributeValues...)
}
func handleSearch(w ldap.ResponseWriter, m *ldap.Message) {
r := m.GetSearchRequest()
log.Printf("Request BaseDn=%s", r.BaseObject())
log.Printf("Request Filter=%s", r.Filter())
log.Printf("Request FilterString=%s", r.FilterString())
log.Printf("Request Attributes=%s", r.Attributes())
log.Printf("Request TimeLimit=%d", r.TimeLimit().Int())
log.Printf("Request Controls=%+v", m.Controls())
// Handle Stop Signal (server stop / client disconnected / Abandoned request....)
select {
case <-m.Done:
log.Print("Leaving handleSearch...")
return
default:
}
if m.Controls() != nil {
for _, control := range *m.Controls() {
if control.ControlType() == "1.2.840.113556.1.4.319" {
var controlValue SearchControlValue
/*rest, err := */ asn1.Unmarshal(control.ControlValue().Bytes(), &controlValue)
log.Printf("Paged search request %+v", controlValue)
// TODO implement paged search
}
}
}
children, _ := jsonParsed.ChildrenMap()
for key, child := range children {
if strings.HasSuffix(key, string(r.BaseObject())) {
log.Printf("checking node: %v\n", key)
if matches(child, r.Filter()) {
log.Printf("found match %v\n", child)
e := ldap.NewSearchResultEntry(key)
for _, ldapAttribute := range r.Attributes() {
attribute := strings.ToLower(string(ldapAttribute))
if attribute == "dn" {
continue
}
value := child.Search(attribute)
log.Printf("checking attribute: %+v for value: %+v\n", attribute, value)
if value != nil {
children, err := value.Children()
var values []string
if err != nil {
values = []string{value.Data().(string)}
} else {
values = make([]string, len(children))
for i, child := range children {
values[i] = child.Data().(string)
}
}
addAttributeValue(&e, ldapAttribute, values)
}
}
w.Write(e)
}
} else {
log.Printf("node: %v not in basedn %v\n", key, r.BaseObject())
}
}
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
w.Write(res)
}