-
Notifications
You must be signed in to change notification settings - Fork 19
/
simplesearch.go
228 lines (179 loc) · 7.68 KB
/
simplesearch.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package godap
import (
"fmt"
"github.com/go-asn1-ber/asn1-ber"
)
// a simplified ldap search request
type LDAPSimpleSearchRequest struct {
Packet *ber.Packet
BaseDN string // DN under which to start searching
Scope int64 // baseObject(0), singleLevel(1), wholeSubtree(2)
DerefAliases int64 // neverDerefAliases(0),derefInSearching(1),derefFindingBaseObj(2),derefAlways(3)
SizeLimit int64 // max number of results to return
TimeLimit int64 // max time in seconds to spend processing
TypesOnly bool // if true client is expecting only type info
FilterAttr string // filter attribute name (assumed to be an equality match with just this one attribute)
FilterValue string // filter attribute value
}
var ErrNotASearchRequest = fmt.Errorf("not a search request")
var ErrSearchRequestTooComplex = fmt.Errorf("this search request is too complex to be parsed as a 'simple search'")
func ParseLDAPSimpleSearchRequestPacket(p *ber.Packet) (*LDAPSimpleSearchRequest, error) {
ret := &LDAPSimpleSearchRequest{}
if len(p.Children) < 2 {
return nil, ErrNotASearchRequest
}
err := CheckPacket(p.Children[1], ber.ClassApplication, ber.TypeConstructed, 0x3)
if err != nil {
return nil, ErrNotASearchRequest
}
rps := p.Children[1].Children
ret.BaseDN = string(rps[0].ByteValue)
ret.Scope = ForceInt64(rps[1].Value)
ret.DerefAliases = ForceInt64(rps[2].Value)
ret.SizeLimit = ForceInt64(rps[3].Value)
ret.TimeLimit = ForceInt64(rps[4].Value)
ret.TypesOnly = rps[5].Value.(bool)
// Check to see if it looks like a simple search criteria
err = CheckPacket(rps[6], ber.ClassContext, ber.TypeConstructed, 0x3)
if err == nil {
// It is, return the attribute and value
ret.FilterAttr = string(rps[6].Children[0].ByteValue)
ret.FilterValue = string(rps[6].Children[1].ByteValue)
} else {
// This is likely some sort of complex search criteria.
// Try to generate a searchFingerPrint based on the values
// You will have to understand this fingerprint in your code
var getContextValue func(p *ber.Packet) string
getContextValue = func(p *ber.Packet) string {
ret := ""
if p.Value != nil {
ret = fmt.Sprint(p.Value)
}
for _, child := range p.Children {
childVal := getContextValue(child)
if childVal != "" {
if ret != "" {
ret += ","
}
ret += childVal
}
}
return ret
}
ret.FilterAttr = "searchFingerprint"
ret.FilterValue = getContextValue(rps[6])
for index := 7; index < len(rps); index++ {
value := getContextValue(rps[index])
if value != "" {
if ret.FilterValue != "" {
ret.FilterValue += ","
}
ret.FilterValue += value
}
}
}
return ret, nil
}
// a simplified ldap search response
type LDAPSimpleSearchResultEntry struct {
DN string // DN of this search result
Attrs map[string]interface{} // map of attributes
}
func (e *LDAPSimpleSearchResultEntry) MakePacket(msgid int64) *ber.Packet {
messageId := msgid
replypacket := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Response")
replypacket.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageId, "MessageId"))
searchResult := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ber.Tag(4), nil, "Response")
searchResult.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, e.DN, "DN"))
attrs := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attrs")
for k, v := range e.Attrs {
attr := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attr")
attr.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, k, "Key"))
attrvals := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "Values")
switch v := v.(type) {
case string:
attrvals.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, v, "String Value"))
case []string:
for _, v1 := range v {
attrvals.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, v1, "String Value (from slice)"))
}
default:
ldapdebug("skipping value for key '%s' because I can't process type '%t'", k, v)
continue
}
attr.AppendChild(attrvals)
attrs.AppendChild(attr)
}
searchResult.AppendChild(attrs)
replypacket.AppendChild(searchResult)
return replypacket
}
func MakeLDAPSearchResultDonePacket(msgid int64) *ber.Packet {
messageId := msgid
replypacket := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Response")
replypacket.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageId, "MessageId"))
searchResult := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ber.Tag(5), nil, "Response")
searchResult.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, 0, "Result Code"))
// per the spec these are "matchedDN" and "diagnosticMessage", but we don't need them for this
searchResult.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "Unused"))
searchResult.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "Unused"))
replypacket.AppendChild(searchResult)
return replypacket
}
func MakeLDAPSearchResultNoSuchObjectPacket(msgid int64) *ber.Packet {
messageId := msgid
replypacket := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Response")
replypacket.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageId, "MessageId"))
searchResult := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ber.Tag(5), nil, "Response")
// 32 is "noSuchObject"
searchResult.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, 32, "Result Code"))
// per the spec these are "matchedDN" and "diagnosticMessage", but we don't need them for this
searchResult.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "Unused"))
searchResult.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "Unused"))
replypacket.AppendChild(searchResult)
return replypacket
}
// a callback function to produce search results; should return nil to mean
// we chose not to attempt to search (i.e. this request is not for us);
// or return empty slice to mean 0 results (or slice with data for results)
type LDAPSimpleSearchFunc func(*LDAPSimpleSearchRequest) []*LDAPSimpleSearchResultEntry
type LDAPSimpleSearchFuncHandler struct {
LDAPSimpleSearchFunc LDAPSimpleSearchFunc
}
func (h *LDAPSimpleSearchFuncHandler) ServeLDAP(ssn *LDAPSession, p *ber.Packet) []*ber.Packet {
req, err := ParseLDAPSimpleSearchRequestPacket(p)
if err == ErrNotASearchRequest {
return nil
} else if err == ErrSearchRequestTooComplex {
ldapdebug("Search request too complex, skipping")
return nil
} else if err != nil {
ldapdebug("Error while trying to parse search request: %v", err)
return nil
}
res := h.LDAPSimpleSearchFunc(req)
// the function is telling us it is opting not to process this search request
if res == nil {
return nil
}
msgid, err := ExtractMessageId(p)
if err != nil {
ldapdebug("Failed to extract message id")
return nil
}
// no results
if len(res) < 1 {
return []*ber.Packet{MakeLDAPSearchResultNoSuchObjectPacket(msgid)}
}
// format each result
ret := make([]*ber.Packet, 0)
for _, resitem := range res {
resultPacket := resitem.MakePacket(msgid)
// fmt.Printf("--------------------\n")
// ber.PrintPacket(resultPacket)
ret = append(ret, resultPacket)
}
// end with a done packet
ret = append(ret, MakeLDAPSearchResultDonePacket(msgid))
return ret
}