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