-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgroup.go
232 lines (177 loc) · 5.25 KB
/
group.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
package goldap
import (
"fmt"
"github.com/go-ldap/ldap/v3"
)
// CreateGroup creates ldap group
func (c *Client) CreateGroup(dn, name, description, groupType, managedBy string, displayName string, members []string) error {
req := ldap.NewAddRequest(dn, []ldap.Control{})
req.Attribute("objectClass", []string{"group"})
req.Attribute("sAMAccountName", []string{name})
if description != "" {
req.Attribute("description", []string{description})
}
if groupType != "" {
req.Attribute("groupType", []string{groupType})
}
if managedBy != "" {
req.Attribute("managedBy", []string{managedBy})
}
if displayName != "" {
req.Attribute("displayName", []string{displayName})
}
if len(members) > 0 {
req.Attribute("member", members)
}
return c.Conn.Add(req)
}
// SearchGroupByName searches an LDAP group by name and returns its DN
func (c *Client) SearchGroupByName(name, ou string, scope int) (string, error) {
// Request name and description attributes
req := ldap.NewSearchRequest(
ou,
scope,
ldap.NeverDerefAliases,
0,
0,
false,
fmt.Sprintf("(&(objectClass=group)(cn=%s))", name),
[]string{},
[]ldap.Control{},
)
// Search for group
sr, err := c.Conn.Search(req)
if err != nil {
return "", fmt.Errorf("searching group by name: %s", err)
}
// If no entries found, group doesn't exists, return error
if len(sr.Entries) == 0 {
return "", fmt.Errorf("group %q not found in OU %q", name, ou)
}
// If more than one entry, it's an error
if len(sr.Entries) > 1 {
return "", fmt.Errorf("more than one group found with name %q in OU %q", name, ou)
}
// Return group DN
return sr.Entries[0].DN, nil
}
// ReadGroup reads ldap group and return it's attributes on an error if the group donesn't exist
func (c *Client) ReadGroup(dn string, memberPageSize int) (attributes map[string][]string, err error) {
// Request name and description attributes
req := ldap.NewSearchRequest(
dn,
ldap.ScopeBaseObject,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectclass=group)",
[]string{"name", "description", "groupType", "managedBy", "displayName"},
[]ldap.Control{},
)
sr, err := c.Conn.Search(req)
if err != nil {
return attributes, err
}
attributes = map[string][]string{}
for _, entry := range sr.Entries {
for _, attr := range entry.Attributes {
attributes[attr.Name] = attr.Values
}
}
// Page member attribute
page := 0
attributes["member"] = []string{}
for {
fmt.Printf("member;range=%d-%d\n", memberPageSize*page, (memberPageSize-1)+memberPageSize*page)
// Request member attribute page
req := ldap.NewSearchRequest(
dn,
ldap.ScopeBaseObject,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectclass=group)",
[]string{fmt.Sprintf("member;range=%d-%d", memberPageSize*page, (memberPageSize-1)+memberPageSize*page)},
[]ldap.Control{},
)
// Search
sr, err := c.Conn.Search(req)
if err != nil {
return attributes, err
}
// If no attributes found, member doesn't exists, break
if len(sr.Entries) == 0 {
break
}
// If more than one entry, it's an error
if len(sr.Entries) > 1 {
return attributes, fmt.Errorf("more than one entry found retrieving group members")
}
// If no attributes found, member doesn't exists, break
if len(sr.Entries[0].Attributes) == 0 {
break
}
// Append member attribute
attributes["member"] = append(attributes["member"], sr.Entries[0].Attributes[0].Values...)
// If no more pages, break
if len(sr.Entries[0].Attributes[0].Values) < memberPageSize {
break
}
// Next page
page++
}
return attributes, nil
}
// UpdateGroupDescription updates ldap group description
func (c *Client) UpdateGroupDescription(dn string, description string) error {
req := ldap.NewModifyRequest(dn, []ldap.Control{})
if description == "" {
req.Delete("description", []string{})
} else {
req.Replace("description", []string{description})
}
return c.Conn.Modify(req)
}
// UpdateGroupMembers updates ldap group members
func (c *Client) UpdateGroupMembers(dn string, members []string) error {
req := ldap.NewModifyRequest(dn, []ldap.Control{})
if len(members) == 0 {
req.Delete("member", members)
} else {
req.Replace("member", members)
}
return c.Conn.Modify(req)
}
// UpdateGroupType updates ldap group type
func (c *Client) UpdateGroupType(dn string, groupType string) error {
req := ldap.NewModifyRequest(dn, []ldap.Control{})
req.Replace("groupType", []string{groupType})
return c.Conn.Modify(req)
}
// UpdateGroupManagedBy updates ldap group managedBy
func (c *Client) UpdateGroupManagedBy(dn string, managedBy string) error {
req := ldap.NewModifyRequest(dn, []ldap.Control{})
if managedBy == "" {
req.Delete("managedBy", []string{})
} else {
req.Replace("managedBy", []string{managedBy})
}
return c.Conn.Modify(req)
}
// UpdateGroupDisplayName updates ldap group displayName
func (c *Client) UpdateGroupDisplayName(dn string, displayName string) error {
req := ldap.NewModifyRequest(dn, []ldap.Control{})
if displayName == "" {
req.Delete("displayName", []string{})
} else {
req.Replace("displayName", []string{displayName})
}
return c.Conn.Modify(req)
}
// DeleteGroup deletes the specify group
func (c *Client) DeleteGroup(dn string) error {
req := ldap.NewDelRequest(dn, []ldap.Control{})
return c.Conn.Del(req)
}