-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathldap.go
617 lines (481 loc) · 15.1 KB
/
ldap.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package main
import (
"fmt"
"strconv"
"strings"
"github.com/go-ldap/ldap/v3"
"github.com/kpango/glg"
)
// ldapConnect connects and binds to the configured hostURI
func ldapConnect() (*ldap.Conn, error) {
var (
con *ldap.Conn
err error
)
con, err = ldap.DialURL(*config.HostURI)
if err != nil {
return nil, err
}
// bind with given credentials
err = con.Bind(*config.UserDN, *config.UserPassword)
if err != nil {
return nil, err
}
glg.Infof("successfully connected & authenticated to LDAP")
return con, nil
}
// ldapLoadPeople loads all people objects from LDAP
func ldapLoadPeople() error {
var (
err error
sr *ldap.SearchResult
user *posixAccount
group *posixGroup
ou *organizationalUnit
i int
j int
tmpPeople posixGroup
// class will be posixAccount or posixGroup
class string
)
glg.Infof("reading people objects from LDAP")
// get a list of all existing objects within the peopleDN
sr, err = ldapCon.Search(&ldap.SearchRequest{
peopleDN,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectClass=*)",
nil,
nil,
})
if err != nil {
// check if error is only group being missing
if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
return fmt.Errorf("people_rdn doesn't seem to exist: %s", err.Error())
}
return err
}
// go through all user objects
// NOTE: this assumes the result is ordered in a way that children don't appear before its parent
for i = range sr.Entries {
if sr.Entries[i].DN == peopleDN {
// skip peopleDN object
continue
}
user = new(posixAccount)
group = new(posixGroup)
ou = new(organizationalUnit)
// set DN for all objects as it is unknown which object it is
group.dn = sr.Entries[i].DN
user.dn = sr.Entries[i].DN
ou.dn = sr.Entries[i].DN
// go through all attributes
for j = range sr.Entries[i].Attributes {
// assuming that there is only one value for all attributes
switch sr.Entries[i].Attributes[j].Name {
// there is no guarantee the class attribute is the first attribute, thus until known both structs are filled
// with information
case "objectClass":
// there is more than one objectClass
for _, class = range sr.Entries[i].Attributes[j].Values {
if class == "posixAccount" || class == "posixGroup" || class == "organizationalUnit" {
break
}
}
case "ou":
ou.cn = sr.Entries[i].Attributes[j].Values[0]
case "cn":
group.CN = sr.Entries[i].Attributes[j].Values[0]
user.UID = &sr.Entries[i].Attributes[j].Values[0]
case "description":
group.Description = sr.Entries[i].Attributes[j].Values[0]
case "gidNumber":
user.GIDNumber = new(int)
*user.GIDNumber, _ = strconv.Atoi(sr.Entries[i].Attributes[j].Values[0])
group.GIDNumber = new(int)
*group.GIDNumber, _ = strconv.Atoi(sr.Entries[i].Attributes[j].Values[0])
case "homeDirectory":
user.HomeDir = &sr.Entries[i].Attributes[j].Values[0]
case "sn":
user.Surname = &sr.Entries[i].Attributes[j].Values[0]
case "uidNumber":
user.UIDNumber = new(int)
*user.UIDNumber, _ = strconv.Atoi(sr.Entries[i].Attributes[j].Values[0])
// determine the highest used UIDNumber
if *user.UIDNumber > latestUID {
latestUID = *user.UIDNumber
}
case "displayName":
user.DisplayName = &sr.Entries[i].Attributes[j].Values[0]
case "givenName":
user.GivenName = &sr.Entries[i].Attributes[j].Values[0]
case "loginShell":
user.LoginShell = &sr.Entries[i].Attributes[j].Values[0]
case "mail":
user.Mail = &sr.Entries[i].Attributes[j].Values[0]
case "sshPublicKey":
user.SSHPublicKey = &sr.Entries[i].Attributes[j].Values[0]
case "userPassword":
user.UserPassword = &sr.Entries[i].Attributes[j].Values[0]
}
}
// check if object was a posixAccount or posixGroup
switch class {
case "posixAccount":
// add to global list of LDAP users
// NOTE: this is a workaround as append on struct member within a map is not supported
// see https://suraj.pro/post/golang_workaround/
tmpPeople = ldapPeople[strings.SplitAfterN(user.dn, ",", 2)[1]]
tmpPeople.Objects = append(tmpPeople.Objects, *user)
ldapPeople[strings.SplitAfterN(user.dn, ",", 2)[1]] = tmpPeople
glg.Debugf("found ldap posixAccount %s", user.dn)
case "posixGroup":
ldapPeople[group.dn] = *group
glg.Debugf("found ldap posixGroup %s", group.dn)
case "organizationalUnit":
ldapOUs = append(ldapOUs, ou)
glg.Debugf("found ldap intermediate OU %s", ou.dn)
default:
// using ou.dn but any other struct would work
glg.Errorf("skipping object because of unknown objectClass in %s", ou.dn)
}
}
glg.Infof("successfully loaded people objects from LDAP")
return nil
}
// ldapLoadGroups loads all group objects from LDAP
func ldapLoadGroups() error {
var (
err error
sr *ldap.SearchResult
i int
j int
k int
group *groupOfNames
ou *organizationalUnit
class string
)
// get a list of all existing objects within the groupDN
sr, err = ldapCon.Search(&ldap.SearchRequest{
groupDN,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectClass=*)",
nil,
nil,
})
if err != nil {
// check if error is only group being missing
if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
return fmt.Errorf("people_rdn doesn't seem to exist: %s", err.Error())
}
return err
}
// go through all user objects
// NOTE: this assumes the result is ordered in a way that children don't appear before its parent
for i = range sr.Entries {
if sr.Entries[i].DN == groupDN {
// skip groupDN object
continue
}
group = new(groupOfNames)
ou = new(organizationalUnit)
// set DN for all objects as it is unknown which object it is
group.dn = sr.Entries[i].DN
ou.dn = sr.Entries[i].DN
// go through all attributes
for j = range sr.Entries[i].Attributes {
// assuming that there is only one value for all attributes
switch sr.Entries[i].Attributes[j].Name {
// there is no guarantee the class attribute is the first attribute, thus until known both structs are filled
// with information
case "objectClass":
// there is more than one objectClass
for _, class = range sr.Entries[i].Attributes[j].Values {
if class == "groupOfNames" || class == "organizationalUnit" {
break
}
}
case "ou":
ou.cn = sr.Entries[i].Attributes[j].Values[0]
case "cn":
group.CN = sr.Entries[i].Attributes[j].Values[0]
case "description":
group.Description = sr.Entries[i].Attributes[j].Values[0]
case "member":
// rewrite members to UIDs only as this the format used in config files
for k = range sr.Entries[i].Attributes[j].Values {
if sr.Entries[i].Attributes[j].Values[k] == "uid=MonbanDummyMember" {
// ignoring dummy member
continue
}
group.Members = append(group.Members, strings.Split(sr.Entries[i].Attributes[j].Values[k], ",")[0][4:])
}
}
}
// check if object was a posixAccount or posixGroup
switch class {
case "groupOfNames":
// add to global list of LDAP groups
// NOTE: this is a workaround as append on struct member within a map is not supported
// see https://suraj.pro/post/golang_workaround/
ldapGroups[group.dn] = *group
glg.Debugf("found ldap groupOfNames %s", group.dn)
for i = range group.Members {
if group.Members[i] == "uid=MonbanDummyMember" {
// ignore dummy member
continue
}
glg.Debugf("found member %s", group.Members[i])
}
case "organizationalUnit":
ldapOUs = append(ldapOUs, ou)
glg.Debugf("found ldap intermediate OU %s", ou.dn)
}
}
glg.Infof("successfully loaded group objects from LDAP")
return nil
}
// ldapDeleteGroupOfNamesMember deletes a given user from a LDAP group
func ldapDeleteGroupOfNamesMember(group string, user string) error {
var (
modify *ldap.ModifyRequest
)
glg.Debugf("deleting groupdOfNames member in %s", group)
modify = ldap.NewModifyRequest(group, nil)
modify.Delete("member", []string{user})
return ldapCon.Modify(modify)
}
// ldapDeletePosixAccount delets a given posixAccount object from LDAP
func ldapDeletePosixAccount(dn string) error {
var (
err error
modify *ldap.ModifyRequest
dnFragments []string
)
glg.Debugf("deleting posixAccount %s", dn)
// delete user object
if err = ldapCon.Del(&ldap.DelRequest{
DN: dn,
Controls: nil,
}); err != nil {
return err
}
// delete memberUid reference in UnixGroup
dnFragments = strings.Split(dn, ",")
glg.Debugf("deleting posixGroup member in %s", strings.Join(dnFragments[1:], ","))
modify = ldap.NewModifyRequest(strings.Join(dnFragments[1:], ","), nil)
modify.Delete("memberUid", []string{dnFragments[0][4:]})
return ldapCon.Modify(modify)
}
// ldapCreatePosixAccount creates a new posixAccount object in LDAP
func ldapCreatePosixAccount(user *posixAccount) error {
var (
err error
add *ldap.AddRequest
modify *ldap.ModifyRequest
dnSplits []string
)
glg.Debugf("creating posixAccount %s", user.dn)
add = ldap.NewAddRequest(user.dn, nil)
add.Attribute("objectClass", []string{
"inetOrgPerson",
"ldapPublicKey",
"organizationalPerson",
"person",
"posixAccount",
"shadowAccount",
"top"})
dnSplits = strings.Split(user.dn, ",")
// UIDNumber should be set if generateUID is false
if config.GenerateUID {
latestUID++
if latestUID > config.MaxUID {
glg.Fatalf("reached max_uid limit")
}
user.UIDNumber = new(int)
*user.UIDNumber = latestUID
}
// strings
add.Attribute("cn", []string{*user.UID})
add.Attribute("gidNumber", []string{strconv.Itoa(*user.GIDNumber)})
add.Attribute("uidNumber", []string{strconv.Itoa(*user.UIDNumber)})
add.Attribute("homeDirectory", []string{*user.HomeDir})
add.Attribute("sn", []string{*user.Surname})
add.Attribute("uid", []string{*user.UID})
add.Attribute("displayName", []string{*user.DisplayName})
add.Attribute("givenName", []string{*user.GivenName})
add.Attribute("loginShell", []string{*user.LoginShell})
add.Attribute("mail", []string{*user.Mail})
add.Attribute("userPassword", []string{*user.UserPassword})
if *config.EnableSSHPublicKeys {
if user.SSHPublicKey != nil {
add.Attribute("sshPublicKey", []string{*user.SSHPublicKey})
}
}
if err = ldapCon.Add(add); err != nil {
return err
}
// create memberUid reference in UnixGroup
modify = ldap.NewModifyRequest(strings.Join(dnSplits[1:], ","), nil)
glg.Debugf("adding posixGroup member in %s", strings.Join(dnSplits[1:], ","))
modify.Add("memberUid", []string{*user.UID})
return ldapCon.Modify(modify)
}
// ldapUpdatePosixAccount updates an existing posixAccount object in LDAP
func ldapUpdatePosixAccount(user *posixAccount) error {
var (
modify *ldap.ModifyRequest
)
glg.Debugf("updating posixAccount %s", user.dn)
modify = ldap.NewModifyRequest(user.dn, nil)
if user.UID != nil {
modify.Replace("cn", []string{*user.UID})
}
if user.GIDNumber != nil {
modify.Replace("gidNumber", []string{strconv.Itoa(*user.GIDNumber)})
}
if user.UIDNumber != nil {
modify.Replace("uidNumber", []string{strconv.Itoa(*user.UIDNumber)})
}
if user.HomeDir != nil {
modify.Replace("homeDirectory", []string{*user.HomeDir})
}
if user.Surname != nil {
modify.Replace("sn", []string{*user.Surname})
}
if user.DisplayName != nil {
modify.Replace("displayName", []string{*user.DisplayName})
}
if user.GivenName != nil {
modify.Replace("givenName", []string{*user.GivenName})
}
if user.LoginShell != nil {
modify.Replace("loginShell", []string{*user.LoginShell})
}
if user.Mail != nil {
modify.Replace("mail", []string{*user.Mail})
}
if user.UserPassword != nil {
modify.Replace("userPassword", []string{*user.UserPassword})
}
if *config.EnableSSHPublicKeys {
if user.SSHPublicKey != nil {
modify.Replace("sshPublicKey", []string{*user.SSHPublicKey})
}
}
return ldapCon.Modify(modify)
}
// ldapAddGroupOfNamesMember adds a new given member to a LDAP groupOfNames
func ldapAddGroupOfNamesMember(group string, user string) error {
var (
modify *ldap.ModifyRequest
)
glg.Debugf("adding groupOfNames member in %s", group)
modify = ldap.NewModifyRequest(group, nil)
modify.Add("member", []string{user})
return ldapCon.Modify(modify)
}
// ldapCreatePosixGroup creates a new posixGroup on LDAP target
func ldapCreatePosixGroup(group posixGroup) error {
var (
add *ldap.AddRequest
)
glg.Debugf("creating posixGroup %s", group.dn)
add = ldap.NewAddRequest(group.dn, nil)
add.Attribute("objectClass", []string{
"posixGroup",
"top"})
// strings
add.Attribute("cn", []string{group.CN})
add.Attribute("gidNumber", []string{strconv.Itoa(*group.GIDNumber)})
add.Attribute("description", []string{group.Description})
return ldapCon.Add(add)
}
// ldapUpdatePosixGroup updates a given posixGroup on LDAP target
func ldapUpdatePosixGroup(group *posixGroup) error {
var (
modify *ldap.ModifyRequest
)
glg.Debugf("updating posixGroup %s", group.dn)
modify = ldap.NewModifyRequest(group.dn, nil)
if group.GIDNumber != nil {
modify.Replace("gidNumber", []string{strconv.Itoa(*group.GIDNumber)})
}
if group.Description != "" {
modify.Replace("description", []string{group.Description})
}
return ldapCon.Modify(modify)
}
// ldapDeletePosixGroup deletes a posixGroup on LDAP target
func ldapDeletePosixGroup(ou string) error {
glg.Debugf("deleting posixGroup %s", ou)
return ldapCon.Del(&ldap.DelRequest{
DN: ou,
Controls: nil,
})
}
// ldapCreateGroupOfNames creates a new user group on LDAP target
func ldapCreateGroupOfNames(group groupOfNames) error {
var (
add *ldap.AddRequest
)
glg.Debugf("creating groupOfNames %s", group.dn)
add = ldap.NewAddRequest(group.dn, nil)
add.Attribute("objectClass", []string{
"groupOfNames",
"top"})
// strings
add.Attribute("cn", []string{group.CN})
add.Attribute("member", []string{"uid=MonbanDummyMember"})
add.Attribute("description", []string{group.Description})
return ldapCon.Add(add)
}
// ldapUpdateGroupOfNames updates an existing groupOfNames object in LDAP
func ldapUpdateGroupOfNames(group *groupOfNames) error {
var (
modify *ldap.ModifyRequest
)
glg.Debugf("updating groupOfName %s", group.dn)
modify = ldap.NewModifyRequest(group.dn, nil)
if group.Description != "" {
modify.Replace("description", []string{group.Description})
}
return ldapCon.Modify(modify)
}
// ldapCreateOrganisationalUnit creates a new OU on LDAP target
func ldapCreateOrganisationalUnit(ou *organizationalUnit) error {
var (
add *ldap.AddRequest
)
glg.Debugf("creating organizationalUnit %s", ou.dn)
add = ldap.NewAddRequest(ou.dn, nil)
add.Attribute("objectClass", []string{
"organizationalUnit",
"top"})
// strings
add.Attribute("ou", []string{ou.cn})
add.Attribute("description", []string{ou.description})
return ldapCon.Add(add)
}
// ldapDeleteOrianisationalUnit deletes an OU on LDAP target
func ldapDeleteOrianisationalUnit(ou string) error {
glg.Debugf("deleting organizationalUnit %s", ou)
return ldapCon.Del(&ldap.DelRequest{
DN: ou,
Controls: nil,
})
}
func ldapDeleteGroupOfNames(ou string) error {
glg.Debugf("deleting groupOfNames %s", ou)
return ldapCon.Del(&ldap.DelRequest{
DN: ou,
Controls: nil,
})
}