-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandler_add.go
94 lines (75 loc) · 2.03 KB
/
handler_add.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
package main
import (
"context"
"log"
ldap "github.com/openstandia/ldapserver"
"golang.org/x/xerrors"
)
func handleAdd(s *Server, w ldap.ResponseWriter, m *ldap.Message) {
ctx := SetSessionContext(context.Background(), m)
r := m.GetAddRequest()
dn, err := s.NormalizeDN(string(r.Entry()))
if err != nil {
log.Printf("warn: Invalid DN: %s err: %s", r.Entry(), err)
responseAddError(w, err)
return
}
if !s.RequiredAuthz(m, AddOps, dn) {
// TODO return errror message
// ldap_add: Insufficient access (50)
// additional info: no write access to parent
responseAddError(w, NewInsufficientAccess())
return
}
// Invalid suffix
if !dn.Equal(s.Suffix) && !dn.IsSubOf(s.Suffix) {
responseAddError(w, NewNoGlobalSuperiorKnowledge())
return
}
log.Printf("debug: Start adding DN: %v", dn)
addEntry, err := mapper.LDAPMessageToAddEntry(dn, r.Attributes())
if err != nil {
responseAddError(w, err)
return
}
log.Printf("info: Adding entry: %s", r.Entry())
i := 0
Retry:
id, err := s.Repo().Insert(ctx, addEntry)
if err != nil {
var retryError *RetryError
if ok := xerrors.As(err, &retryError); ok {
if i < maxRetry {
i++
log.Printf("warn: Detect consistency error. Do retry. try_count: %d", i)
goto Retry
}
log.Printf("error: Give up to retry. try_count: %d", i)
}
responseAddError(w, err)
return
}
log.Printf("debug: Added. Id: %d, DN: %v", id, dn)
res := ldap.NewAddResponse(ldap.LDAPResultSuccess)
w.Write(res)
log.Printf("debug: End Adding entry: %s", r.Entry())
}
func responseAddError(w ldap.ResponseWriter, err error) {
var ldapErr *LDAPError
if ok := xerrors.As(err, &ldapErr); ok {
log.Printf("warn: Add LDAP error. err: %+v", err)
res := ldap.NewAddResponse(ldapErr.Code)
if ldapErr.Msg != "" {
res.SetDiagnosticMessage(ldapErr.Msg)
}
if ldapErr.MatchedDN != "" {
res.SetMatchedDN(ldapErr.MatchedDN)
}
w.Write(res)
} else {
log.Printf("error: Add error. err: %+v", err)
// TODO
res := ldap.NewAddResponse(ldap.LDAPResultProtocolError)
w.Write(res)
}
}