r17930: Merge noinclude branch:
[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 "lib/util/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 ldap_connect_state *state =
324                 talloc_get_type(ctx->async.private_data,
325                                 struct ldap_connect_state);
326         struct ldap_connection *conn = state->conn;
327         uint16_t port;
328
329         state->ctx->status = socket_connect_multi_recv(ctx, state, &conn->sock,
330                                                        &port);
331         if (!composite_is_ok(state->ctx)) return;
332
333         /* setup a handler for events on this socket */
334         conn->event.fde = event_add_fd(conn->event.event_ctx, conn->sock, 
335                                        socket_get_fd(conn->sock), 
336                                        EVENT_FD_READ, ldap_io_handler, conn);
337         if (conn->event.fde == NULL) {
338                 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
339                 return;
340         }
341
342         talloc_steal(conn, conn->sock);
343         if (conn->ldaps) {
344                 struct socket_context *tls_socket = tls_init_client(conn->sock, conn->event.fde);
345                 if (tls_socket == NULL) {
346                         talloc_free(conn->sock);
347                         return;
348                 }
349                 talloc_unlink(conn, conn->sock);
350                 conn->sock = tls_socket;
351                 talloc_steal(conn, conn->sock);
352         }
353
354         conn->packet = packet_init(conn);
355         if (conn->packet == NULL) {
356                 talloc_free(conn->sock);
357                 return;
358         }
359
360         packet_set_private(conn->packet, conn);
361         packet_set_socket(conn->packet, conn->sock);
362         packet_set_callback(conn->packet, ldap_recv_handler);
363         packet_set_full_request(conn->packet, ldap_full_packet);
364         packet_set_error_handler(conn->packet, ldap_error_handler);
365         packet_set_event_context(conn->packet, conn->event.event_ctx);
366         packet_set_fde(conn->packet, conn->event.fde);
367         packet_set_serialise(conn->packet);
368
369         composite_done(state->ctx);
370
371         return;
372 }
373
374 NTSTATUS ldap_connect_recv(struct composite_context *ctx)
375 {
376         NTSTATUS status = composite_wait(ctx);
377         talloc_free(ctx);
378         return status;
379 }
380
381 NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
382 {
383         struct composite_context *ctx = ldap_connect_send(conn, url);
384         return ldap_connect_recv(ctx);
385 }
386
387 /* set reconnect parameters */
388
389 void ldap_set_reconn_params(struct ldap_connection *conn, int max_retries)
390 {
391         if (conn) {
392                 conn->reconnect.max_retries = max_retries;
393                 conn->reconnect.retries = 0;
394                 conn->reconnect.previous = time(NULL);
395         }
396 }
397
398 /* Actually this function is NOT ASYNC safe, FIXME? */
399 static void ldap_reconnect(struct ldap_connection *conn)
400 {
401         NTSTATUS status;
402         time_t now = time(NULL);
403
404         /* do we have set up reconnect ? */
405         if (conn->reconnect.max_retries == 0) return;
406
407         /* is the retry time expired ? */
408         if (now > conn->reconnect.previous + 30) {
409                 conn->reconnect.retries = 0;
410                 conn->reconnect.previous = now;
411         }
412
413         /* are we reconnectind too often and too fast? */
414         if (conn->reconnect.retries > conn->reconnect.max_retries) return;
415
416         /* keep track of the number of reconnections */
417         conn->reconnect.retries++;
418
419         /* reconnect */
420         status = ldap_connect(conn, conn->reconnect.url);
421         if ( ! NT_STATUS_IS_OK(status)) {
422                 return;
423         }
424
425         /* rebind */
426         status = ldap_rebind(conn);
427         if ( ! NT_STATUS_IS_OK(status)) {
428                 ldap_connection_dead(conn);
429         }
430 }
431
432 /* destroy an open ldap request */
433 static int ldap_request_destructor(struct ldap_request *req)
434 {
435         if (req->state == LDAP_REQUEST_PENDING) {
436                 DLIST_REMOVE(req->conn->pending, req);
437         }
438         return 0;
439 }
440
441 /*
442   called on timeout of a ldap request
443 */
444 static void ldap_request_timeout(struct event_context *ev, struct timed_event *te, 
445                                       struct timeval t, void *private_data)
446 {
447         struct ldap_request *req = talloc_get_type(private_data, struct ldap_request);
448         req->status = NT_STATUS_IO_TIMEOUT;
449         if (req->state == LDAP_REQUEST_PENDING) {
450                 DLIST_REMOVE(req->conn->pending, req);
451         }
452         req->state = LDAP_REQUEST_DONE;
453         if (req->async.fn) {
454                 req->async.fn(req);
455         }
456 }
457
458
459 /*
460   called on completion of a one-way ldap request
461 */
462 static void ldap_request_complete(struct event_context *ev, struct timed_event *te, 
463                                   struct timeval t, void *private_data)
464 {
465         struct ldap_request *req = talloc_get_type(private_data, struct ldap_request);
466         if (req->async.fn) {
467                 req->async.fn(req);
468         }
469 }
470
471 /*
472   send a ldap message - async interface
473 */
474 struct ldap_request *ldap_request_send(struct ldap_connection *conn,
475                                        struct ldap_message *msg)
476 {
477         struct ldap_request *req;
478         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
479
480         req = talloc_zero(conn, struct ldap_request);
481         if (req == NULL) return NULL;
482
483         if (conn->sock == NULL) {
484                 status = NT_STATUS_INVALID_CONNECTION;
485                 goto failed;
486         }
487
488         req->state       = LDAP_REQUEST_SEND;
489         req->conn        = conn;
490         req->messageid   = conn->next_messageid++;
491         if (conn->next_messageid == 0) {
492                 conn->next_messageid = 1;
493         }
494         req->type        = msg->type;
495         if (req->messageid == -1) {
496                 goto failed;
497         }
498
499         talloc_set_destructor(req, ldap_request_destructor);
500
501         msg->messageid = req->messageid;
502
503         if (!ldap_encode(msg, &req->data, req)) {
504                 goto failed;            
505         }
506
507         status = packet_send(conn->packet, req->data);
508         if (!NT_STATUS_IS_OK(status)) {
509                 goto failed;
510         }
511
512         /* some requests don't expect a reply, so don't add those to the
513            pending queue */
514         if (req->type == LDAP_TAG_AbandonRequest ||
515             req->type == LDAP_TAG_UnbindRequest) {
516                 req->status = NT_STATUS_OK;
517                 req->state = LDAP_REQUEST_DONE;
518                 /* we can't call the async callback now, as it isn't setup, so
519                    call it as next event */
520                 event_add_timed(conn->event.event_ctx, req, timeval_zero(),
521                                 ldap_request_complete, req);
522                 return req;
523         }
524
525         req->state = LDAP_REQUEST_PENDING;
526         DLIST_ADD(conn->pending, req);
527
528         /* put a timeout on the request */
529         req->time_event = event_add_timed(conn->event.event_ctx, req, 
530                                           timeval_current_ofs(conn->timeout, 0),
531                                           ldap_request_timeout, req);
532
533         return req;
534
535 failed:
536         req->status = status;
537         req->state = LDAP_REQUEST_ERROR;
538         event_add_timed(conn->event.event_ctx, req, timeval_zero(),
539                         ldap_request_complete, req);
540
541         return req;
542 }
543
544
545 /*
546   wait for a request to complete
547   note that this does not destroy the request
548 */
549 NTSTATUS ldap_request_wait(struct ldap_request *req)
550 {
551         while (req->state < LDAP_REQUEST_DONE) {
552                 if (event_loop_once(req->conn->event.event_ctx) != 0) {
553                         req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
554                         break;
555                 }
556         }
557         return req->status;
558 }
559
560
561 /*
562   a mapping of ldap response code to strings
563 */
564 static const struct {
565         enum ldap_result_code code;
566         const char *str;
567 } ldap_code_map[] = {
568 #define _LDAP_MAP_CODE(c) { c, #c }
569         _LDAP_MAP_CODE(LDAP_SUCCESS),
570         _LDAP_MAP_CODE(LDAP_OPERATIONS_ERROR),
571         _LDAP_MAP_CODE(LDAP_PROTOCOL_ERROR),
572         _LDAP_MAP_CODE(LDAP_TIME_LIMIT_EXCEEDED),
573         _LDAP_MAP_CODE(LDAP_SIZE_LIMIT_EXCEEDED),
574         _LDAP_MAP_CODE(LDAP_COMPARE_FALSE),
575         _LDAP_MAP_CODE(LDAP_COMPARE_TRUE),
576         _LDAP_MAP_CODE(LDAP_AUTH_METHOD_NOT_SUPPORTED),
577         _LDAP_MAP_CODE(LDAP_STRONG_AUTH_REQUIRED),
578         _LDAP_MAP_CODE(LDAP_REFERRAL),
579         _LDAP_MAP_CODE(LDAP_ADMIN_LIMIT_EXCEEDED),
580         _LDAP_MAP_CODE(LDAP_UNAVAILABLE_CRITICAL_EXTENSION),
581         _LDAP_MAP_CODE(LDAP_CONFIDENTIALITY_REQUIRED),
582         _LDAP_MAP_CODE(LDAP_SASL_BIND_IN_PROGRESS),
583         _LDAP_MAP_CODE(LDAP_NO_SUCH_ATTRIBUTE),
584         _LDAP_MAP_CODE(LDAP_UNDEFINED_ATTRIBUTE_TYPE),
585         _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_MATCHING),
586         _LDAP_MAP_CODE(LDAP_CONSTRAINT_VIOLATION),
587         _LDAP_MAP_CODE(LDAP_ATTRIBUTE_OR_VALUE_EXISTS),
588         _LDAP_MAP_CODE(LDAP_INVALID_ATTRIBUTE_SYNTAX),
589         _LDAP_MAP_CODE(LDAP_NO_SUCH_OBJECT),
590         _LDAP_MAP_CODE(LDAP_ALIAS_PROBLEM),
591         _LDAP_MAP_CODE(LDAP_INVALID_DN_SYNTAX),
592         _LDAP_MAP_CODE(LDAP_ALIAS_DEREFERENCING_PROBLEM),
593         _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_AUTHENTICATION),
594         _LDAP_MAP_CODE(LDAP_INVALID_CREDENTIALS),
595         _LDAP_MAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTs),
596         _LDAP_MAP_CODE(LDAP_BUSY),
597         _LDAP_MAP_CODE(LDAP_UNAVAILABLE),
598         _LDAP_MAP_CODE(LDAP_UNWILLING_TO_PERFORM),
599         _LDAP_MAP_CODE(LDAP_LOOP_DETECT),
600         _LDAP_MAP_CODE(LDAP_NAMING_VIOLATION),
601         _LDAP_MAP_CODE(LDAP_OBJECT_CLASS_VIOLATION),
602         _LDAP_MAP_CODE(LDAP_NOT_ALLOWED_ON_NON_LEAF),
603         _LDAP_MAP_CODE(LDAP_NOT_ALLOWED_ON_RDN),
604         _LDAP_MAP_CODE(LDAP_ENTRY_ALREADY_EXISTS),
605         _LDAP_MAP_CODE(LDAP_OBJECT_CLASS_MODS_PROHIBITED),
606         _LDAP_MAP_CODE(LDAP_AFFECTS_MULTIPLE_DSAS),
607         _LDAP_MAP_CODE(LDAP_OTHER)
608 };
609
610 /*
611   used to setup the status code from a ldap response
612 */
613 NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
614 {
615         int i;
616         const char *codename = "unknown";
617
618         if (r->resultcode == LDAP_SUCCESS) {
619                 return NT_STATUS_OK;
620         }
621
622         if (conn->last_error) {
623                 talloc_free(conn->last_error);
624         }
625
626         for (i=0;i<ARRAY_SIZE(ldap_code_map);i++) {
627                 if (r->resultcode == ldap_code_map[i].code) {
628                         codename = ldap_code_map[i].str;
629                         break;
630                 }
631         }
632
633         conn->last_error = talloc_asprintf(conn, "LDAP error %u %s - %s <%s> <%s>", 
634                                            r->resultcode,
635                                            codename,
636                                            r->dn?r->dn:"(NULL)", 
637                                            r->errormessage?r->errormessage:"", 
638                                            r->referral?r->referral:"");
639         
640         return NT_STATUS_LDAP(r->resultcode);
641 }
642
643 /*
644   return error string representing the last error
645 */
646 const char *ldap_errstr(struct ldap_connection *conn, NTSTATUS status)
647 {
648         if (NT_STATUS_IS_LDAP(status) && conn->last_error != NULL) {
649                 return conn->last_error;
650         }
651         return nt_errstr(status);
652 }
653
654
655 /*
656   return the Nth result message, waiting if necessary
657 */
658 NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
659 {
660         *msg = NULL;
661
662         NT_STATUS_HAVE_NO_MEMORY(req);
663
664         while (req->state < LDAP_REQUEST_DONE && n >= req->num_replies) {
665                 if (event_loop_once(req->conn->event.event_ctx) != 0) {
666                         return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
667                 }
668         }
669
670         if (n < req->num_replies) {
671                 *msg = req->replies[n];
672                 return NT_STATUS_OK;
673         }
674
675         if (!NT_STATUS_IS_OK(req->status)) {
676                 return req->status;
677         }
678
679         return NT_STATUS_NO_MORE_ENTRIES;
680 }
681
682
683 /*
684   return a single result message, checking if it is of the expected LDAP type
685 */
686 NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
687 {
688         NTSTATUS status;
689         status = ldap_result_n(req, 0, msg);
690         if (!NT_STATUS_IS_OK(status)) {
691                 return status;
692         }
693         if ((*msg)->type != type) {
694                 *msg = NULL;
695                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
696         }
697         return status;
698 }
699
700 /*
701   a simple ldap transaction, for single result requests that only need a status code
702   this relies on single valued requests having the response type == request type + 1
703 */
704 NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
705 {
706         struct ldap_request *req = ldap_request_send(conn, msg);
707         struct ldap_message *res;
708         NTSTATUS status;
709         status = ldap_result_n(req, 0, &res);
710         if (!NT_STATUS_IS_OK(status)) {
711                 talloc_free(req);
712                 return status;
713         }
714         if (res->type != msg->type + 1) {
715                 talloc_free(req);
716                 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
717         }
718         status = ldap_check_response(conn, &res->r.GeneralResult);
719         talloc_free(req);
720         return status;
721 }