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