-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
278 lines (229 loc) · 6.17 KB
/
users.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
272
273
274
275
276
277
278
package ldap
import (
"errors"
"fmt"
"time"
"github.com/go-ldap/ldap/v3"
)
var (
ErrUserNotFound = errors.New("user not found")
ErrSAMAccountNameDuplicated = errors.New("sAMAccountName is not unique")
ErrMailDuplicated = errors.New("mail is not unique")
accountExpiresBase = time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC)
accountExpiresNever uint64 = 0x7FFFFFFFFFFFFFFF
userFields = []string{"memberOf", "cn", "sAMAccountName", "mail", "userAccountControl", "description"}
)
type User struct {
Object
Enabled bool
SAMAccountName string
Description string
Mail *string
// Groups is a list of CNs
Groups []string
}
func userFromEntry(entry *ldap.Entry) (*User, error) {
enabled, err := parseObjectEnabled(entry.GetAttributeValue("userAccountControl"))
if err != nil {
return nil, err
}
var mail *string
if mails := entry.GetAttributeValues("mail"); len(mails) > 0 {
mail = &mails[0]
}
return &User{
Object: objectFromEntry(entry),
Enabled: enabled,
SAMAccountName: entry.GetAttributeValue("sAMAccountName"),
Description: entry.GetAttributeValue("description"),
Mail: mail,
Groups: entry.GetAttributeValues("memberOf"),
}, nil
}
func (l *LDAP) FindUserByDN(dn string) (user *User, err error) {
c, err := l.GetConnection()
if err != nil {
return nil, err
}
defer c.Close()
r, err := c.Search(&ldap.SearchRequest{
BaseDN: dn,
Scope: ldap.ScopeBaseObject,
DerefAliases: ldap.NeverDerefAliases,
Filter: "(objectClass=user)",
Attributes: []string{"memberOf", "cn", "sAMAccountName", "userAccountControl", "description"},
})
if err != nil {
return nil, err
}
if len(r.Entries) == 0 {
return nil, ErrUserNotFound
}
if len(r.Entries) > 1 {
return nil, ErrDNDuplicated
}
if user, err = userFromEntry(r.Entries[0]); err != nil {
return nil, err
}
return
}
func (l *LDAP) FindUserBySAMAccountName(sAMAccountName string) (user *User, err error) {
c, err := l.GetConnection()
if err != nil {
return nil, err
}
defer c.Close()
r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: fmt.Sprintf("(&(objectClass=user)(sAMAccountName=%s))", ldap.EscapeFilter(sAMAccountName)),
Attributes: userFields,
})
if err != nil {
return nil, err
}
if len(r.Entries) == 0 {
return nil, ErrUserNotFound
}
if len(r.Entries) > 1 {
return nil, ErrSAMAccountNameDuplicated
}
if user, err = userFromEntry(r.Entries[0]); err != nil {
return nil, err
}
return
}
func (l *LDAP) FindUserByMail(mail string) (user *User, err error) {
c, err := l.GetConnection()
if err != nil {
return nil, err
}
defer c.Close()
r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: fmt.Sprintf("(&(objectClass=user)(mail=%s))", ldap.EscapeFilter(mail)),
Attributes: userFields,
})
if err != nil {
return nil, err
}
if len(r.Entries) == 0 {
return nil, ErrUserNotFound
}
if len(r.Entries) > 1 {
return nil, ErrMailDuplicated
}
if user, err = userFromEntry(r.Entries[0]); err != nil {
return nil, err
}
return
}
func (l *LDAP) FindUsers() (users []User, err error) {
c, err := l.GetConnection()
if err != nil {
return nil, err
}
defer c.Close()
r, err := c.Search(&ldap.SearchRequest{
BaseDN: l.config.BaseDN,
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: "(objectClass=user)",
Attributes: userFields,
})
if err != nil {
return nil, err
}
for _, entry := range r.Entries {
user, err := userFromEntry(entry)
if err != nil {
continue
}
users = append(users, *user)
}
return
}
func (l *LDAP) AddUserToGroup(dn, groupDN string) error {
c, err := l.GetConnection()
if err != nil {
return err
}
defer c.Close()
req := ldap.NewModifyRequest(groupDN, nil)
req.Add("member", []string{dn})
return c.Modify(req)
}
func (l *LDAP) RemoveUserFromGroup(dn, groupDN string) error {
c, err := l.GetConnection()
if err != nil {
return err
}
defer c.Close()
req := ldap.NewModifyRequest(groupDN, nil)
req.Delete("member", []string{dn})
return c.Modify(req)
}
type FullUser struct {
CN string
SAMAccountName *string
FirstName string
LastName string
DisplayName *string
Description *string
Email *string
ObjectClasses []string
// AccountExpires represents the expiration date of the user's account.
// When set to nil, the account never expires.
AccountExpires *time.Time
UserAccountControl UAC
Path *string
}
func (l *LDAP) CreateUser(user FullUser, password string) (string, error) {
if user.ObjectClasses == nil {
user.ObjectClasses = []string{"top", "person", "organizationalPerson", "user"}
}
if user.DisplayName == nil {
user.DisplayName = &user.CN
}
c, err := l.GetConnection()
if err != nil {
return "", err
}
defer c.Close()
baseDN := ""
if user.Path != nil {
baseDN = *user.Path + ","
}
baseDN += l.config.BaseDN
dn := fmt.Sprintf("CN=%s,%s", ldap.EscapeDN(user.CN), baseDN)
req := ldap.NewAddRequest(dn, nil)
req.Attribute("objectClass", user.ObjectClasses)
req.Attribute("cn", []string{user.CN})
req.Attribute("name", []string{user.FirstName + " " + user.LastName})
req.Attribute("givenName", []string{user.FirstName})
req.Attribute("sn", []string{user.LastName})
req.Attribute("displayName", []string{*user.DisplayName})
req.Attribute("accountExpires", []string{convertAccountExpires(user.AccountExpires)})
req.Attribute("userAccountControl", []string{fmt.Sprintf("%d", user.UserAccountControl.Uint32())})
if user.SAMAccountName != nil {
req.Attribute("sAMAccountName", []string{*user.SAMAccountName})
}
if user.Description != nil {
req.Attribute("description", []string{*user.Description})
}
if user.Email != nil {
req.Attribute("mail", []string{*user.Email})
}
return dn, c.Add(req)
}
func (l *LDAP) DeleteUser(dn string) error {
c, err := l.GetConnection()
if err != nil {
return err
}
defer c.Close()
return c.Del(&ldap.DelRequest{DN: dn})
}