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