forked from go-ldap/ldap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirsync.go
78 lines (62 loc) · 1.65 KB
/
dirsync.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
package ldap
import (
"errors"
"fmt"
"time"
)
func GetDirSyncRequest(baseDn, filter string, cookie []byte) *SearchRequest {
sizeLimit := 0
timeLimit := 0
typesOnly := false
dirSyncControl := NewControlDirSync(0, 1000, cookie)
dirSyncControlEx := NewControlDirSyncEx(1)
searchRequest := NewSearchRequest(
baseDn, ScopeBaseObject, NeverDerefAliases,
sizeLimit, timeLimit, typesOnly, filter,
nil,
[]Control{dirSyncControl, dirSyncControlEx},
)
return searchRequest
}
func (l *Conn) RunDirSync(searchRequest *SearchRequest, callback func([]*Entry, []byte) error) error {
dirSyncControl, err := getDirSyncControl(searchRequest.Controls)
if err != nil {
return err
}
for {
result, err := l.Search(searchRequest)
if err != nil {
return err
}
if result == nil {
return errors.New("ldap: packet not received")
}
dirSync, err := getDirSyncControl(result.Controls)
if err != nil {
return err
}
cookie := dirSync.Cookie
if len(cookie) == 0 {
return errors.New("ldap: dirsync cookie in the result is empty")
}
if err := callback(result.Entries, cookie); err != nil {
return fmt.Errorf("ldap callback returned an error: %v", err)
}
if len(result.Entries) == 0 {
time.Sleep(10 * time.Second)
} else {
dirSyncControl.SetCookie(cookie)
}
}
}
func getDirSyncControl(controls []Control) (*ControlDirSync, error) {
control := FindControl(controls, ControlTypeDirSync)
if control == nil {
return nil, errors.New("ldap: no dirsync control found")
}
dirSync, ok := control.(*ControlDirSync)
if !ok {
return nil, fmt.Errorf("ldap: expected ControlDirSync control, but got %T", control)
}
return dirSync, nil
}