-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandler_modify.go
120 lines (95 loc) · 2.76 KB
/
handler_modify.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
package main
import (
"context"
"database/sql"
"log"
ldap "github.com/openstandia/ldapserver"
"golang.org/x/xerrors"
)
func handleModify(s *Server, w ldap.ResponseWriter, m *ldap.Message) {
ctx := SetSessionContext(context.Background(), m)
r := m.GetModifyRequest()
dn, err := s.NormalizeDN(string(r.Object()))
if err != nil {
log.Printf("warn: Invalid dn: %s, err: %s", r.Object(), err)
// TODO return correct error
res := ldap.NewModifyResponse(ldap.LDAPResultOperationsError)
w.Write(res)
return
}
if !s.RequiredAuthz(m, ModifyOps, dn) {
responseModifyError(w, NewInsufficientAccess())
return
}
log.Printf("info: Modify entry: %s", dn.DNNormStr())
i := 0
Retry:
err = s.Repo().Update(ctx, dn, func(newEntry *ModifyEntry) error {
for _, change := range r.Changes() {
modification := change.Modification()
attrName := string(modification.Type_())
log.Printf("Modify operation: %d, attribute: %s", change.Operation(), modification.Type_())
var values []string
for _, attributeValue := range modification.Vals() {
values = append(values, string(attributeValue))
log.Printf("--> value: %s", attributeValue)
}
var err error
switch change.Operation() {
case ldap.ModifyRequestChangeOperationAdd:
err = newEntry.Add(attrName, values)
case ldap.ModifyRequestChangeOperationDelete:
err = newEntry.Delete(attrName, values)
case ldap.ModifyRequestChangeOperationReplace:
err = newEntry.Replace(attrName, values)
}
if err != nil {
return err
}
}
// Validate ObjectClass
ocs, ok := newEntry.ObjectClassesNorm()
if !ok {
return NewObjectClassViolation()
}
if err := s.schemaMap.ValidateObjectClass(ocs, newEntry.attributes); err != nil {
return err
}
return nil
})
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)
}
if err == sql.ErrNoRows {
responseModifyError(w, NewNoSuchObject())
return
}
responseModifyError(w, xerrors.Errorf("Failed to modify the entry. dn: %s, err: %w", dn.DNNormStr(), err))
return
}
res := ldap.NewModifyResponse(ldap.LDAPResultSuccess)
w.Write(res)
}
func responseModifyError(w ldap.ResponseWriter, err error) {
var ldapErr *LDAPError
if ok := xerrors.As(err, &ldapErr); ok {
log.Printf("warn: Modify LDAP error. err: %+v", err)
res := ldap.NewModifyResponse(ldapErr.Code)
if ldapErr.Msg != "" {
res.SetDiagnosticMessage(ldapErr.Msg)
}
w.Write(res)
} else {
log.Printf("error: Modify error. err: %+v", err)
// TODO
res := ldap.NewModifyResponse(ldap.LDAPResultProtocolError)
w.Write(res)
}
}