forked from SpeckiJ/UserManager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLDAPHandler.go
271 lines (241 loc) · 6.52 KB
/
LDAPHandler.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"fmt"
"github.com/pkg/errors"
"gopkg.in/ldap.v2"
"strings"
)
// pLDAPConnect connects to LDAP
func pLDAPConnect() (*ldap.Conn, error) {
l, err := ldap.Dial("tcp", configuration.LDAPServer+":"+configuration.LDAPPort)
return l, err
}
// pLDAPConnectAnon binds to LDAP anonymously (only read access)
func pLDAPConnectAnon() (*ldap.Conn, error) {
l, err := pLDAPConnect()
if err != nil {
return nil, err
}
// Bind with anonymous user
err = l.Bind("", "")
return l, err
}
// pLDAPConnectAdmin binds to LDAP with editing permissions
func pLDAPConnectAdmin() (*ldap.Conn, error) {
l, err := pLDAPConnect()
if err != nil {
return nil, err
}
// Bind with Admin credentials
err = l.Bind(configuration.LDAPAdmin, configuration.LDAPPass)
return l, err
}
// LDAPAuthenticateAdmin checks whether given user has admin permissions
func LDAPAuthenticateAdmin(admin User) (bool, error) {
// Connect to LDAP
l, err := pLDAPConnectAnon()
if err != nil {
return false, err
}
defer l.Close()
sr, err := pLDAPSearch([]string{"dn"}, fmt.Sprintf(configuration.LDAPAdminfilter, admin.Username))
if err != nil {
return false, nil
}
// User does not exist or too many entries returned
if len(sr) != 1 {
return false, nil
}
// Bind as the user to verify their password
err = l.Bind(sr[0].DN, admin.Password)
if err != nil {
return false, nil
// Wrong password
}
return true, nil
}
// LDAPAddUser adds user with given dn to LDAP
func LDAPAddUser(dn string, user User) error {
l, err := pLDAPConnectAdmin()
if err != nil {
return err
}
password, err := ldapEncodePassword(user.Password)
if err != nil {
return err
}
// Add User Entry
ar := ldap.NewAddRequest(dn)
ar.Attribute("objectclass", []string{"inetOrgPerson", "person", "top", "organizationalPerson"})
ar.Attribute("cn", []string{user.Username})
ar.Attribute("sn", []string{user.Username})
ar.Attribute("displayName", []string{user.Username})
ar.Attribute("userPassword", password)
err = l.Add(ar)
l.Close()
if err != nil {
return err
}
// Add User to appropriate Group
err = LDAPAddUserToGroup(user.Username, user.Fs)
return err
}
// LDAPAddUserToGroup adds user to Group
func LDAPAddUserToGroup(username, groupname string) error {
l, err := pLDAPConnectAdmin()
if err != nil {
return err
}
// Validate User
sr, err := pLDAPSearch([]string{"dn"}, fmt.Sprintf(configuration.LDAPUserfilter, username))
if err != nil {
return err
}
if len(sr) != 1 {
// User does not exist or too many entries returned
return errors.New("Invalid Username supplied!")
}
mr := ldap.NewModifyRequest("cn=" + groupname + "," + configuration.LDAPBaseDN)
mr.Add("member", []string{sr[0].DN})
err = l.Modify(mr)
l.Close()
return err
}
// LDAPRemoveUserFromGroup removes user from group
func LDAPRemoveUserFromGroup(username, groupname string) error {
conn, err := pLDAPConnectAdmin()
if err != nil {
return err
}
// Validate User
sr, err := pLDAPSearch([]string{"dn"}, fmt.Sprintf(configuration.LDAPUserfilter, username))
if err != nil {
return err
}
if len(sr) != 1 {
// User does not exist or too many entries returned
return errors.New("Invalid Username supplied!")
}
// Remove from group
mr := ldap.NewModifyRequest("cn=" + groupname + "," + configuration.LDAPBaseDN)
mr.Delete("member", []string{sr[0].DN})
err = conn.Modify(mr)
return err
}
// LDAPChangeUserPassword changes password of user given username and new password
func LDAPChangeUserPassword(username, password string) error {
l, err := pLDAPConnectAdmin()
if err != nil {
return err
}
defer l.Close()
// Validate User
sr, err := pLDAPSearch([]string{"dn"}, fmt.Sprintf(configuration.LDAPUserfilter, username))
if err != nil {
return err
}
if len(sr) != 1 {
// User does not exist or too many entries returned
return errors.New("Invalid Username supplied!")
}
pass, err := ldapEncodePassword(password)
if err != nil {
return err
}
mr := ldap.NewModifyRequest(sr[0].DN)
mr.Replace("userPassword", pass)
err = l.Modify(mr)
return err
}
// LDAPAddGroup adds Group with given dn to LDAP
func LDAPAddGroup(dn string) error {
l, err := pLDAPConnectAdmin()
if err != nil {
return err
}
ar := ldap.NewAddRequest(dn)
ar.Attribute("objectclass", []string{"groupOfNames", "top"})
ar.Attribute("member", []string{configuration.LDAPAdmin})
err = l.Add(ar)
l.Close()
return err
}
// LDAPDeleteDN removes given dn from LDAP
func LDAPDeleteDN(dn string) error {
l, err := pLDAPConnectAdmin()
if err != nil {
return err
}
// Delete Entry
dr := ldap.NewDelRequest(dn, []ldap.Control{})
err = l.Del(dr)
l.Close()
return err
}
// LDAPViewGroups gets dn of all groups from LDAP
func LDAPViewGroups() (groups []string, err error) {
result, err := pLDAPSearch(
[]string{"cn", "member"},
"(objectClass=groupOfNames)",
)
if err != nil {
return nil, err
}
groups = make([]string, len(result))
for i := range result {
groups[i] = result[i].DN
memberList := strings.Join(result[i].GetAttributeValues("member"), ";")
strings.Replace(memberList, ","+configuration.LDAPBaseDN, "", -1)
groups[i] = "{" + "\"name\": \"" + result[i].DN + "\"," +
"\"members\": \"" + memberList + "\"}"
groups[i] = strings.Replace(groups[i], ","+configuration.LDAPBaseDN, "", -1)
}
return groups, nil
}
// LDAPViewUsers gets dn of all users from LDAP
func LDAPViewUsers() ([]string, error) {
result, err := pLDAPSearch(
[]string{"cn", "memberOf"},
"(objectClass=organizationalPerson)",
)
if err != nil {
return nil, err
}
users := make([]string, len(result))
for i := range result {
var groupList = ""
if result[i].Attributes[1] == nil {
// Invalid
continue
}
for j := range result[i].Attributes[1].Values {
groupList += result[i].Attributes[1].Values[j] + ";"
}
groupList = groupList[:len(groupList)-1] // remove trailing `;`
users[i] = "{" + "\"name\": \"" + result[i].DN + "\"," +
"\"groups\": \"" + groupList + "\"}"
users[i] = strings.Replace(users[i], ","+configuration.LDAPBaseDN, "", -1)
}
return users, nil
}
// pLDAPSearch searches LDAP for dn with given attributes matching given filter
func pLDAPSearch(attributes []string, filter string) (result []*ldap.Entry, err error) {
l, err := pLDAPConnectAnon()
if err != nil {
return nil, err
}
defer l.Close()
// Search for the given username
searchRequest := ldap.NewSearchRequest(
configuration.LDAPBaseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
filter,
attributes,
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
return nil, err
}
return sr.Entries, nil
}