-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
211 lines (182 loc) · 5.08 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
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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/vjeantet/goldap/message"
ldap "github.com/vjeantet/ldapserver"
)
func main() {
sqlserver, sqluser, sqlpass, sqldb := getCreds()
log.Printf("DB Connection: Server=%s User=%s DB=%s", sqlserver, sqluser, sqldb)
err := SQLConnect(sqlserver, sqluser, sqlpass, sqldb)
if err != nil {
log.Printf("DB ERROR: %s", err.Error())
return
}
//Create a new LDAP Server
server := ldap.NewServer()
//Create routes bindings
routes := ldap.NewRouteMux()
routes.Bind(handleBind)
routes.Search(handleSearchDSE).Label("Search - Generic")
//Attach routes to server
server.Handle(routes)
// listen on 10389 and serve
go server.ListenAndServe(":10389")
// When CTRL+C, SIGINT and SIGTERM signal occurs
// Then stop server gracefully
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
close(ch)
server.Stop()
}
func getEnvVar(key, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
value = fallback
}
return value
}
func getCreds() (string, string, string, string){
sqlserver := getEnvVar("FREEPBX_SQLSERVER", "127.0.0.1:3306")
sqluser := getEnvVar("FREEPBX_SQLUSER", "root")
sqlpass := getEnvVar("FREEPBX_SQLPASS", "")
sqldb := getEnvVar("FREEPBX_SQLDB", "asterisk")
return sqlserver, sqluser, sqlpass, sqldb
}
func handleBind(w ldap.ResponseWriter, m *ldap.Message) {
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
w.Write(res)
}
func handleSearchDSE(w ldap.ResponseWriter, m *ldap.Message) {
r := m.GetSearchRequest()
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
defer w.Write(res)
log.Printf("Request BaseDn=%s", r.BaseObject())
log.Printf("Request Filter=%#v", 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 SizeLimit=%d", r.SizeLimit().Int())
sql := "SELECT name, extension FROM users"
sqlVals := []interface{}{}
swapField := func(v string) string {
switch v {
case "displayName":
return "name"
case "telephoneNumber":
return "extension"
default:
log.Printf("Invalid Field Name (%s), returned name", v)
return "name"
}
}
getSubstringSearch := func(v []message.Substring) string {
for _, fs := range v {
switch fsv := fs.(type) {
case message.SubstringInitial:
return string(fsv) + "%"
case message.SubstringAny:
return "%" + string(fsv) + "%"
case message.SubstringFinal:
return "%" + string(fsv)
}
}
return ""
}
var recursiveFilter func(filter interface{}, root bool) string
recursiveFilter = func(filter interface{}, root bool) string {
where := ""
var filterProcessSub func(vsub interface{}) string
filterProcessSub = func(vsub interface{}) string {
switch vs := vsub.(type) {
case message.FilterGreaterOrEqual:
sqlVals = append(sqlVals, vs.AssertionValue())
return swapField(string(vs.AttributeDesc())) + " >= ?"
case message.FilterLessOrEqual:
sqlVals = append(sqlVals, vs.AssertionValue())
return swapField(string(vs.AttributeDesc())) + " <= ?"
case message.FilterEqualityMatch:
sqlVals = append(sqlVals, vs.AssertionValue())
return swapField(string(vs.AttributeDesc())) + " = ?"
case message.FilterSubstrings:
sqlVals = append(sqlVals, getSubstringSearch(vs.Substrings()))
return swapField(string(vs.Type_())) + " LIKE ?"
case message.FilterAnd:
return recursiveFilter(vs, false)
case message.FilterOr:
return recursiveFilter(vs, false)
case message.FilterNot:
return " NOT ( " + filterProcessSub(vs.Filter) + " ) "
default:
return ""
}
return ""
}
switch val := filter.(type) {
case message.FilterAnd:
i := 0
for _, vsub := range val {
addWhere := func() {
if i > 0 {
where += " AND "
}
i++
}
if ret := filterProcessSub(vsub); ret != "" {
addWhere()
where += ret
}
}
case message.FilterOr:
i := 0
for _, vsub := range val {
addWhere := func() {
if i > 0 {
where += " OR "
}
i++
}
if ret := filterProcessSub(vsub); ret != "" {
addWhere()
where += ret
}
}
case message.FilterSubstrings:
if ret := filterProcessSub(val); ret != "" {
where += ret
}
default:
log.Printf("Searching without filter...")
}
if where != "" {
if root {
where = " WHERE " + where
} else {
where = " ( " + where + " ) "
}
}
return where
}
sql += " " + recursiveFilter(r.Filter(), true) + " "
sql += " ORDER BY name ASC LIMIT 0, ?"
if r.SizeLimit().Int() > 0 {
sqlVals = append(sqlVals, r.SizeLimit().Int())
} else {
sqlVals = append(sqlVals, 99)
}
log.Printf("Query SQL: %s %#v", sql, sqlVals)
result, err := SQLSearch(sql, sqlVals)
if err != nil {
log.Printf("SQL ERROR: %s", err)
}
for _, entry := range result {
e := ldap.NewSearchResultEntry("")
e.AddAttribute("displayName", message.AttributeValue(entry.Name))
e.AddAttribute("telephoneNumber", message.AttributeValue(entry.Extension))
w.Write(e)
}
}