26
26
package ldap
27
27
28
28
import (
29
+ "errors"
29
30
"log"
30
31
31
32
ber "github.com/go-asn1-ber/asn1-ber"
@@ -39,6 +40,12 @@ const (
39
40
IncrementAttribute = 3 // (https://tools.ietf.org/html/rfc4525)
40
41
)
41
42
43
+ // ModifyResult holds the server's response to a modify request
44
+ type ModifyResult struct {
45
+ // Controls are the returned controls
46
+ Controls []Control
47
+ }
48
+
42
49
// PartialAttribute for a ModifyRequest as defined in https://tools.ietf.org/html/rfc4511
43
50
type PartialAttribute struct {
44
51
// Type is the type of the partial attribute
@@ -155,3 +162,62 @@ func (l *Conn) Modify(modifyRequest *ModifyRequest) error {
155
162
}
156
163
return nil
157
164
}
165
+
166
+ // ModifyWithResult performs the ModifyRequest and returns the result
167
+ func (l * Conn ) ModifyWithResult (modifyRequest * ModifyRequest ) (* ModifyResult , error ) {
168
+ packet := ber .Encode (ber .ClassUniversal , ber .TypeConstructed , ber .TagSequence , nil , "LDAP Request" )
169
+ packet .AppendChild (ber .NewInteger (ber .ClassUniversal , ber .TypePrimitive , ber .TagInteger , l .nextMessageID (), "MessageID" ))
170
+ packet .AppendChild (modifyRequest .encode ())
171
+ if len (modifyRequest .Controls ) > 0 {
172
+ packet .AppendChild (encodeControls (modifyRequest .Controls ))
173
+ }
174
+
175
+ l .Debug .PrintPacket (packet )
176
+
177
+ msgCtx , err := l .sendMessage (packet )
178
+ if err != nil {
179
+ return nil , err
180
+ }
181
+ defer l .finishMessage (msgCtx )
182
+
183
+ result := & ModifyResult {
184
+ Controls : make ([]Control , 0 ),
185
+ }
186
+
187
+ l .Debug .Printf ("%d: waiting for response" , msgCtx .id )
188
+ packetResponse , ok := <- msgCtx .responses
189
+ if ! ok {
190
+ return nil , NewError (ErrorNetwork , errors .New ("ldap: response channel closed" ))
191
+ }
192
+ packet , err = packetResponse .ReadPacket ()
193
+ l .Debug .Printf ("%d: got response %p" , msgCtx .id , packet )
194
+ if err != nil {
195
+ return nil , err
196
+ }
197
+
198
+ if l .Debug {
199
+ if err := addLDAPDescriptions (packet ); err != nil {
200
+ return nil , err
201
+ }
202
+ ber .PrintPacket (packet )
203
+ }
204
+
205
+ switch packet .Children [1 ].Tag {
206
+ case ApplicationModifyResponse :
207
+ err := GetLDAPError (packet )
208
+ if err != nil {
209
+ return nil , err
210
+ }
211
+ if len (packet .Children ) == 3 {
212
+ for _ , child := range packet .Children [2 ].Children {
213
+ decodedChild , err := DecodeControl (child )
214
+ if err != nil {
215
+ return nil , errors .New ("failed to decode child control: " + err .Error ())
216
+ }
217
+ result .Controls = append (result .Controls , decodedChild )
218
+ }
219
+ }
220
+ }
221
+ l .Debug .Printf ("%d: returning" , msgCtx .id )
222
+ return result , nil
223
+ }
0 commit comments