9103e939e706d95b81b3e2f40f2856989d5f9991
[samba.git] / source / libcli / ldap / ldap_client.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    LDAP protocol helper functions for SAMBA
4    
5    Copyright (C) Andrew Tridgell  2004
6    Copyright (C) Volker Lendecke 2004
7    Copyright (C) Stefan Metzmacher 2004
8    Copyright (C) Simo Sorce 2004
9     
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23    
24 */
25
26 #include "includes.h"
27 #include "libcli/util/asn_1.h"
28 #include "dlinklist.h"
29 #include "lib/events/events.h"
30 #include "lib/socket/socket.h"
31 #include "libcli/ldap/ldap.h"
32 #include "libcli/ldap/ldap_client.h"
33 #include "libcli/composite/composite.h"
34 #include "lib/stream/packet.h"
35 #include "auth/gensec/gensec.h"
36
37
38 /*
39   create a new ldap_connection stucture. The event context is optional
40 */
41 struct ldap_connection *ldap_new_connection(TALLOC_CTX *mem_ctx, 
42                                             struct event_context *ev)
43 {
44         struct ldap_connection *conn;
45
46         conn = talloc_zero(mem_ctx, struct ldap_connection);
47         if (conn == NULL) {
48                 return NULL;
49         }
50
51         if (ev == NULL) {
52                 ev = event_context_init(conn);
53                 if (ev == NULL) {
54                         talloc_free(conn);
55                         return NULL;
56                 }
57         }
58
59         conn->next_messageid  = 1;
60         conn->event.event_ctx = ev;
61
62         /* set a reasonable request timeout */
63         conn->timeout = 60;
64
65         return conn;
66 }
67
68
69 /*
70   the connection is dead
71 */
72 static void ldap_connection_dead(struct ldap_connection *conn)
73 {
74         struct ldap_request *req;
75
76         while (conn->pending) {
77                 req = conn->pending;
78                 DLIST_REMOVE(req->conn->pending, req);
79                 req->state = LDAP_REQUEST_DONE;
80                 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
81                 if (req->async.fn) {
82                         req->async.fn(req);
83                 }
84         }       
85
86         talloc_free(conn->tls);
87         conn->tls = NULL;
88 }
89
90 /*
91   handle packet errors
92 */
93 static void ldap_error_handler(void *private, NTSTATUS status)
94 {
95         struct ldap_connection *conn = talloc_get_type(private, 
96                                                        struct ldap_connection);
97         ldap_connection_dead(conn);
98 }
99
100
101 /*
102   match up with a pending message, adding to the replies list
103 */
104 static void ldap_match_message(struct ldap_connection *conn, struct ldap_message *msg)
105 {
106         struct ldap_request *req;
107
108         for (req=conn->pending; req; req=req->next) {
109                 if (req->messageid == msg->messageid) break;
110         }
111         /* match a zero message id to the last request sent.
112            It seems that servers send 0 if unable to parse */
113         if (req == NULL && msg->messageid == 0) {
114                 req = conn->pending;
115         }
116         if (req == NULL) {
117                 DEBUG(0,("ldap: no matching message id for %u\n",
118                          msg->messageid));
119                 talloc_free(msg);
120                 return;
121         }
122
123         /* add to the list of replies received */
124         talloc_steal(req, msg);
125         req->replies = talloc_realloc(req, req->replies, 
126                                       struct ldap_message *, req->num_replies+1);
127         if (req->replies == NULL) {
128                 req->status = NT_STATUS_NO_MEMORY;
129                 req->state = LDAP_REQUEST_DONE;
130                 DLIST_REMOVE(conn->pending, req);
131                 if (req->async.fn) {
132                         req->async.fn(req);
133                 }
134                 return;
135         }
136
137         req->replies[req->num_replies] = talloc_steal(req->replies, msg);
138         req->num_replies++;
139
140         if (msg->type != LDAP_TAG_SearchResultEntry &&
141             msg->type != LDAP_TAG_SearchResultReference) {
142                 /* currently only search results expect multiple
143                    replies */
144                 req->state = LDAP_REQUEST_DONE;
145                 DLIST_REMOVE(conn->pending, req);
146         }
147
148         if (req->async.fn) {
149                 req->async.fn(req);
150         }
151 }
152
153
154 /*
155   check if a blob is a complete ldap packet
156   handle wrapper or unwrapped connections
157 */
158 NTSTATUS ldap_complete_packet(void *private, DATA_BLOB blob, size_t *size)
159 {
160         struct ldap_connection *conn = talloc_get_type(private, 
161                                                        struct ldap_connection);
162         if (conn->enable_wrap) {
163                 return packet_full_request_u32(private, blob, size);
164         }
165         return ldap_full_packet(private, blob, size);
166 }
167
168 /*
169   decode/process plain data
170 */
171 static NTSTATUS ldap_decode_plain(struct ldap_connection *conn, DATA_BLOB blob)
172 {
173         struct asn1_data asn1;
174         struct ldap_message *msg = talloc(conn, struct ldap_message);
175
176         if (msg == NULL) {
177                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
178         }
179
180         if (!asn1_load(&asn1, blob)) {
181                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
182         }
183         
184         if (!ldap_decode(&asn1, msg)) {
185                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
186         }
187
188         ldap_match_message(conn, msg);
189
190         data_blob_free(&blob);
191         asn1_free(&asn1);
192         return NT_STATUS_OK;
193 }
194
195 /*
196   decode/process wrapped data
197 */
198 static NTSTATUS ldap_decode_wrapped(struct ldap_connection *conn, DATA_BLOB blob)
199 {
200         DATA_BLOB wrapped, unwrapped;
201         struct asn1_data asn1;
202         struct ldap_message *msg = talloc(conn, struct ldap_message);
203         NTSTATUS status;
204
205         if (msg == NULL) {
206                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
207         }
208
209         wrapped = data_blob_const(blob.data+4, blob.length-4);
210
211         status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
212         if (!NT_STATUS_IS_OK(status)) {
213                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
214         }
215
216         data_blob_free(&blob);
217
218         if (!asn1_load(&asn1, unwrapped)) {
219                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
220         }
221
222         while (ldap_decode(&asn1, msg)) {
223                 ldap_match_message(conn, msg);
224                 msg = talloc(conn, struct ldap_message);
225         }
226                 
227         talloc_free(msg);
228         asn1_free(&asn1);
229
230         return NT_STATUS_OK;
231 }
232
233
234 /*
235   handle ldap recv events
236 */
237 static NTSTATUS ldap_recv_handler(void *private, DATA_BLOB blob)
238 {
239         struct ldap_connection *conn = talloc_get_type(private, 
240                                                        struct ldap_connection);
241         if (conn->enable_wrap) {
242                 return ldap_decode_wrapped(conn, blob);
243         }
244
245         return ldap_decode_plain(conn, blob);
246 }
247
248
249 /*
250   handle ldap socket events
251 */
252 static void ldap_io_handler(struct event_context *ev, struct fd_event *fde, 
253                             uint16_t flags, void *private)
254 {
255         struct ldap_connection *conn = talloc_get_type(private, 
256                                                        struct ldap_connection);
257         if (flags & EVENT_FD_WRITE) {
258                 packet_queue_run(conn->packet);
259                 if (conn->tls == NULL) return;
260         }
261         if (flags & EVENT_FD_READ) {
262                 packet_recv(conn->packet);
263         }
264 }
265
266 /*
267   parse a ldap URL
268 */
269 static NTSTATUS ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
270                                      char **host, uint16_t *port, BOOL *ldaps)
271 {
272         int tmp_port = 0;
273         char protocol[11];
274         char tmp_host[255];
275         const char *p = url;
276         int ret;
277
278         /* skip leading "URL:" (if any) */
279         if (strncasecmp(p, "URL:", 4) == 0) {
280                 p += 4;
281         }
282
283         /* Paranoia check */
284         SMB_ASSERT(sizeof(protocol)>10 && sizeof(tmp_host)>254);
285                 
286         ret = sscanf(p, "%10[^:]://%254[^:/]:%d", protocol, tmp_host, &tmp_port);
287         if (ret < 2) {
288                 return NT_STATUS_INVALID_PARAMETER;
289         }
290
291         if (strequal(protocol, "ldap")) {
292                 *port = 389;
293                 *ldaps = False;
294         } else if (strequal(protocol, "ldaps")) {
295                 *port = 636;
296                 *ldaps = True;
297         } else {
298                 DEBUG(0, ("unrecognised ldap protocol (%s)!\n", protocol));
299                 return NT_STATUS_PROTOCOL_UNREACHABLE;
300         }
301
302         if (tmp_port != 0)
303                 *port = tmp_port;
304
305         *host = talloc_strdup(mem_ctx, tmp_host);
306         NT_STATUS_HAVE_NO_MEMORY(*host);
307
308         return NT_STATUS_OK;
309 }
310
311 /*
312   connect to a ldap server
313 */
314
315 struct ldap_connect_state {
316         struct composite_context *ctx;
317         struct ldap_connection *conn;
318 };
319
320 static void ldap_connect_recv_conn(struct composite_context *ctx);
321
322 struct composite_context *ldap_connect_send(struct ldap_connection *conn,
323                                             const char *url)
324 {
325         struct composite_context *result, *ctx;
326         struct ldap_connect_state *state;
327
328         result = talloc_zero(NULL, struct composite_context);
329         if (result == NULL) goto failed;
330         result->state = COMPOSITE_STATE_IN_PROGRESS;
331         result->async.fn = NULL;
332         result->event_ctx = conn->event.event_ctx;
333
334         state = talloc(result, struct ldap_connect_state);
335         if (state == NULL) goto failed;
336         state->ctx = result;
337         result->private_data = state;
338
339         state->conn = conn;
340
341         state->ctx->status = ldap_parse_basic_url(conn, url, &conn->host,
342                                                   &conn->port, &conn->ldaps);
343         if (!NT_STATUS_IS_OK(state->ctx->status)) {
344                 composite_error(state->ctx, state->ctx->status);
345                 return result;
346         }
347
348         ctx = socket_connect_multi_send(state, conn->host, 1, &conn->port,
349                                         conn->event.event_ctx);
350         if (ctx == NULL) goto failed;
351
352         ctx->async.fn = ldap_connect_recv_conn;
353         ctx->async.private_data = state;
354         return result;
355
356  failed:
357         talloc_free(result);
358         return NULL;
359 }
360
361 static void ldap_connect_recv_conn(struct composite_context *ctx)
362 {
363         struct ldap_connect_state *state =
364                 talloc_get_type(ctx->async.private_data,
365                                 struct ldap_connect_state);
366         struct ldap_connection *conn = state->conn;
367         uint16_t port;
368
369         state->ctx->status = socket_connect_multi_recv(ctx, state, &conn->sock,
370                                                        &port);
371         if (!composite_is_ok(state->ctx)) return;
372
373         /* setup a handler for events on this socket */
374         conn->event.fde = event_add_fd(conn->event.event_ctx, conn->sock, 
375                                        socket_get_fd(conn->sock), 
376                                        EVENT_FD_READ, ldap_io_handler, conn);
377         if (conn->event.fde == NULL) {
378                 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
379                 return;
380         }
381
382         conn->tls = tls_init_client(conn->sock, conn->event.fde, conn->ldaps);
383         if (conn->tls == NULL) {
384                 talloc_free(conn->sock);
385                 return;
386         }
387         talloc_steal(conn, conn->tls);
388         talloc_steal(conn->tls, conn->sock);
389
390         conn->packet = packet_init(conn);
391         if (conn->packet == NULL) {
392                 talloc_free(conn->sock);
393                 return;
394         }
395         packet_set_private(conn->packet, conn);
396         packet_set_tls(conn->packet, conn->tls);
397         packet_set_callback(conn->packet, ldap_recv_handler);
398         packet_set_full_request(conn->packet, ldap_complete_packet);
399         packet_set_error_handler(conn->packet, ldap_error_handler);
400         packet_set_event_context(conn->packet, conn->event.event_ctx);
401         packet_set_fde(conn->packet, conn->event.fde);
402         packet_set_serialise(conn->packet);
403
404         composite_done(state->ctx);
405
406         return;
407 }
408
409 NTSTATUS ldap_connect_recv(struct composite_context *ctx)
410 {
411         NTSTATUS status = composite_wait(ctx);
412         talloc_free(ctx);
413         return status;
414 }
415
416 NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
417 {
418         struct composite_context *ctx = ldap_connect_send(conn, url);
419         return ldap_connect_recv(ctx);
420 }
421
422 /* destroy an open ldap request */
423 static int ldap_request_destructor(void *ptr)
424 {
425         struct ldap_request *req = talloc_get_type(ptr, struct ldap_request);
426         if (req->state == LDAP_REQUEST_PENDING) {
427                 DLIST_REMOVE(req->conn->pending, req);
428         }
429         return 0;
430 }
431
432 /*
433   called on timeout of a ldap request
434 */
435 static void ldap_request_timeout(struct event_context *ev, struct timed_event *te, 
436                                       struct timeval t, void *private)
437 {
438         struct ldap_request *req = talloc_get_type(private, struct ldap_request);
439         req->status = NT_STATUS_IO_TIMEOUT;
440         if (req->state == LDAP_REQUEST_PENDING) {
441                 DLIST_REMOVE(req->conn->pending, req);
442         }
443         req->state = LDAP_REQUEST_DONE;
444         if (req->async.fn) {
445                 req->async.fn(req);
446         }
447 }
448
449
450 /*
451   called on completion of a one-way ldap request
452 */
453 static void ldap_request_complete(struct event_context *ev, struct timed_event *te, 
454                                   struct timeval t, void *private)
455 {
456         struct ldap_request *req = talloc_get_type(private, struct ldap_request);
457         if (req->async.fn) {
458                 req->async.fn(req);
459         }
460 }
461
462 /*
463   send a ldap message - async interface
464 */
465 struct ldap_request *ldap_request_send(struct ldap_connection *conn,
466                                        struct ldap_message *msg)
467 {
468         struct ldap_request *req;
469         NTSTATUS status;
470
471         if (conn->tls == NULL) {
472                 return NULL;
473         }
474
475         req = talloc_zero(conn, struct ldap_request);
476         if (req == NULL) goto failed;
477
478         req->state       = LDAP_REQUEST_SEND;
479         req->conn        = conn;
480         req->messageid   = conn->next_messageid++;
481         if (conn->next_messageid == 0) {
482                 conn->next_messageid = 1;
483         }
484         req->type        = msg->type;
485         if (req->messageid == -1) {
486                 goto failed;
487         }
488
489         talloc_set_destructor(req, ldap_request_destructor);
490
491         msg->messageid = req->messageid;
492
493         if (!ldap_encode(msg, &req->data, req)) {
494                 goto failed;            
495         }
496
497         /* possibly encrypt/sign the request */
498         if (conn->enable_wrap) {
499                 DATA_BLOB wrapped;
500
501                 status = gensec_wrap(conn->gensec, req, &req->data, &wrapped);
502                 if (!NT_STATUS_IS_OK(status)) {
503                         goto failed;
504                 }
505                 data_blob_free(&req->data);
506                 req->data = data_blob_talloc(req, NULL, wrapped.length + 4);
507                 if (req->data.data == NULL) {
508                         goto failed;
509                 }
510                 RSIVAL(req->data.data, 0, wrapped.length);
511                 memcpy(req->data.data+4, wrapped.data, wrapped.length);
512                 data_blob_free(&wrapped);
513         }
514
515         status = packet_send(conn->packet, req->data);
516         if (!NT_STATUS_IS_OK(status)) {
517                 goto failed;
518         }
519
520         /* some requests don't expect a reply, so don't add those to the
521            pending queue */
522         if (req->type == LDAP_TAG_AbandonRequest ||
523             req->type == LDAP_TAG_UnbindRequest) {
524                 req->status = NT_STATUS_OK;
525                 req->state = LDAP_REQUEST_DONE;
526                 /* we can't call the async callback now, as it isn't setup, so
527                    call it as next event */
528                 event_add_timed(conn->event.event_ctx, req, timeval_zero(),
529                                 ldap_request_complete, req);
530                 return req;
531         }
532
533         req->state = LDAP_REQUEST_PENDING;
534         DLIST_ADD(conn->pending, req);
535
536         /* put a timeout on the request */
537         event_add_timed(conn->event.event_ctx, req, 
538                         timeval_current_ofs(conn->timeout, 0),
539                         ldap_request_timeout, req);
540
541         return req;
542
543 failed:
544         talloc_free(req);
545         return NULL;
546 }
547
548
549 /*
550   wait for a request to complete
551   note that this does not destroy the request
552 */
553 NTSTATUS ldap_request_wait(struct ldap_request *req)
554 {
555         while (req->state != LDAP_REQUEST_DONE) {
556                 if (event_loop_once(req->conn->event.event_ctx) != 0) {
557                         req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
558                         break;
559                 }
560         }
561         return req->status;
562 }
563
564
565 /*
566   a mapping of ldap response code to strings
567 */
568 static const struct {
569         enum ldap_result_code code;
570         const char *str;
571 } ldap_code_map[] = {
572 #define _LDAP_MAP_CODE(c) { c, #c }
573         _LDAP_MAP_CODE(LDAP_SUCCESS),
574         _LDAP_MAP_CODE(LDAP_OPERATIONS_ERROR),
575         _LDAP_MAP_CODE(LDAP_PROTOCOL_ERROR),
576         _LDAP_MAP_CODE(LDAP_TIME_LIMIT_EXCEEDED),
577         _LDAP_MAP_CODE(LDAP_SIZE_LIMIT_EXCEEDED),
578         _LDAP_MAP_CODE(LDAP_COMPARE_FALSE),
579         _LDAP_MAP_CODE(LDAP_COMPARE_TRUE),
580         _LDAP_MAP_CODE(LDAP_AUTH_METHOD_NOT_SUPPORTED),
581         _LDAP_MAP_CODE(LDAP_STRONG_AUTH_REQUIRED),
582         _LDAP_MAP_CODE(LDAP_REFERRAL),
583         _LDAP_MAP_CODE(LDAP_ADMIN_LIMIT_EXCEEDED),
584         _LDAP_MAP_CODE(LDAP_UNAVAILABLE_CRITICAL_EXTENSION),
585         _LDAP_MAP_CODE(LDAP_CONFIDENTIALITY_REQUIRED),
586         _LDAP_MAP_CODE(LDAP_SASL_BIND_IN_PROGRESS),
587         _LDAP_MAP_CODE(LDAP_NO_SUCH_ATTRIBUTE),
588         _LDAP_MAP_CODE(LDAP_UNDEFINED_ATTRIBUTE_TYPE),
589         _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_MATCHING),
590         _LDAP_MAP_CODE(LDAP_CONSTRAINT_VIOLATION),
591         _LDAP_MAP_CODE(LDAP_ATTRIBUTE_OR_VALUE_EXISTS),
592         _LDAP_MAP_CODE(LDAP_INVALID_ATTRIBUTE_SYNTAX),
593         _LDAP_MAP_CODE(LDAP_NO_SUCH_OBJECT),
594         _LDAP_MAP_CODE(LDAP_ALIAS_PROBLEM),
595         _LDAP_MAP_CODE(LDAP_INVALID_DN_SYNTAX),
596         _LDAP_MAP_CODE(LDAP_ALIAS_DEREFERENCING_PROBLEM),
597         _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_AUTHENTICATION),
598         _LDAP_MAP_CODE(LDAP_INVALID_CREDENTIALS),
599         _LDAP_MAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTs),
600         _LDAP_MAP_CODE(LDAP_BUSY),
601         _LDAP_MAP_CODE(LDAP_UNAVAILABLE),
602         _LDAP_MAP_CODE(LDAP_UNWILLING_TO_PERFORM),
603         _LDAP_MAP_CODE(LDAP_LOOP_DETECT),
604         _LDAP_MAP_CODE(LDAP_NAMING_VIOLATION),
605         _LDAP_MAP_CODE(LDAP_OBJECT_CLASS_VIOLATION),
606         _LDAP_MAP_CODE(LDAP_NOT_ALLOWED_ON_NON_LEAF),
607         _LDAP_MAP_CODE(LDAP_NOT_ALLOWED_ON_RDN),
608         _LDAP_MAP_CODE(LDAP_ENTRY_ALREADY_EXISTS),
609         _LDAP_MAP_CODE(LDAP_OBJECT_CLASS_MODS_PROHIBITED),
610         _LDAP_MAP_CODE(LDAP_AFFECTS_MULTIPLE_DSAS),
611         _LDAP_MAP_CODE(LDAP_OTHER)
612 };
613
614 /*
615   used to setup the status code from a ldap response
616 */
617 NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
618 {
619         int i;
620         const char *codename = "unknown";
621
622         if (r->resultcode == LDAP_SUCCESS) {
623                 return NT_STATUS_OK;
624         }
625
626         if (conn->last_error) {
627                 talloc_free(conn->last_error);
628         }
629
630         for (i=0;i<ARRAY_SIZE(ldap_code_map);i++) {
631                 if (r->resultcode == ldap_code_map[i].code) {
632                         codename = ldap_code_map[i].str;
633                         break;
634                 }
635         }
636
637         conn->last_error = talloc_asprintf(conn, "LDAP error %u %s - %s <%s> <%s>", 
638                                            r->resultcode,
639                                            codename,
640                                            r->dn?r->dn:"(NULL)", 
641                                            r->errormessage?r->errormessage:"", 
642                                            r->referral?r->referral:"");
643         
644         return NT_STATUS_LDAP(r->resultcode);
645 }
646
647 /*
648   return error string representing the last error
649 */
650 const char *ldap_errstr(struct ldap_connection *conn, NTSTATUS status)
651 {
652         if (NT_STATUS_IS_LDAP(status) && conn->last_error != NULL) {
653                 return conn->last_error;
654         }
655         return nt_errstr(status);
656 }
657
658
659 /*
660   return the Nth result message, waiting if necessary
661 */
662 NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
663 {
664         *msg = NULL;
665
666         NT_STATUS_HAVE_NO_MEMORY(req);
667
668         while (req->state != LDAP_REQUEST_DONE && n >= req->num_replies) {
669                 if (event_loop_once(req->conn->event.event_ctx) != 0) {
670                         return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
671                 }
672         }
673
674         if (n < req->num_replies) {
675                 *msg = req->replies[n];
676                 return NT_STATUS_OK;
677         }
678
679         if (!NT_STATUS_IS_OK(req->status)) {
680                 return req->status;
681         }
682
683         return NT_STATUS_NO_MORE_ENTRIES;
684 }
685
686
687 /*
688   return a single result message, checking if it is of the expected LDAP type
689 */
690 NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
691 {
692         NTSTATUS status;
693         status = ldap_result_n(req, 0, msg);
694         if (!NT_STATUS_IS_OK(status)) {
695                 return status;
696         }
697         if ((*msg)->type != type) {
698                 *msg = NULL;
699                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
700         }
701         return status;
702 }
703
704 /*
705   a simple ldap transaction, for single result requests that only need a status code
706   this relies on single valued requests having the response type == request type + 1
707 */
708 NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
709 {
710         struct ldap_request *req = ldap_request_send(conn, msg);
711         struct ldap_message *res;
712         NTSTATUS status;
713         status = ldap_result_n(req, 0, &res);
714         if (!NT_STATUS_IS_OK(status)) {
715                 talloc_free(req);
716                 return status;
717         }
718         if (res->type != msg->type + 1) {
719                 talloc_free(req);
720                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
721         }
722         status = ldap_check_response(conn, &res->r.GeneralResult);
723         talloc_free(req);
724         return status;
725 }