LDB ASYNC: misc changes
[Samba/ekacnet.git] / source4 / libcli / ldap / ldap_client.c
blobfc5863b671f32ee3d8ac4d757d35cab185542f64
1 /*
2 Unix SMB/CIFS mplementation.
3 LDAP protocol helper functions for SAMBA
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Volker Lendecke 2004
7 Copyright (C) Stefan Metzmacher 2004
8 Copyright (C) Simo Sorce 2004
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 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "lib/util/asn1.h"
27 #include "lib/util/dlinklist.h"
28 #include "lib/events/events.h"
29 #include "lib/socket/socket.h"
30 #include "libcli/ldap/ldap.h"
31 #include "libcli/ldap/ldap_proto.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 #include "param/param.h"
39 #include "libcli/resolve/resolve.h"
41 /**
42 create a new ldap_connection stucture. The event context is optional
44 _PUBLIC_ struct ldap_connection *ldap4_new_connection(TALLOC_CTX *mem_ctx,
45 struct loadparm_context *lp_ctx,
46 struct event_context *ev)
48 struct ldap_connection *conn;
50 if (ev == NULL) {
51 return NULL;
54 conn = talloc_zero(mem_ctx, struct ldap_connection);
55 if (conn == NULL) {
56 return NULL;
59 conn->next_messageid = 1;
60 conn->event.event_ctx = ev;
62 conn->lp_ctx = lp_ctx;
64 /* set a reasonable request timeout */
65 conn->timeout = 60;
67 /* explicitly avoid reconnections by default */
68 conn->reconnect.max_retries = 0;
70 return conn;
74 the connection is dead
76 static void ldap_connection_dead(struct ldap_connection *conn)
78 struct ldap_request *req;
80 talloc_free(conn->sock); /* this will also free event.fde */
81 talloc_free(conn->packet);
82 conn->sock = NULL;
83 conn->event.fde = NULL;
84 conn->packet = NULL;
86 /* return an error for any pending request ... */
87 while (conn->pending) {
88 req = conn->pending;
89 DLIST_REMOVE(req->conn->pending, req);
90 req->state = LDAP_REQUEST_DONE;
91 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
92 if (req->async.fn) {
93 req->async.fn(req);
98 static void ldap_reconnect(struct ldap_connection *conn);
101 handle packet errors
103 static void ldap_error_handler(void *private_data, NTSTATUS status)
105 struct ldap_connection *conn = talloc_get_type(private_data,
106 struct ldap_connection);
107 ldap_connection_dead(conn);
109 /* but try to reconnect so that the ldb client can go on */
110 ldap_reconnect(conn);
115 match up with a pending message, adding to the replies list
117 static void ldap_match_message(struct ldap_connection *conn, struct ldap_message *msg)
119 struct ldap_request *req;
120 int i;
122 for (req=conn->pending; req; req=req->next) {
123 if (req->messageid == msg->messageid) break;
125 /* match a zero message id to the last request sent.
126 It seems that servers send 0 if unable to parse */
127 if (req == NULL && msg->messageid == 0) {
128 req = conn->pending;
130 if (req == NULL) {
131 DEBUG(0,("ldap: no matching message id for %u\n",
132 msg->messageid));
133 talloc_free(msg);
134 return;
137 /* Check for undecoded critical extensions */
138 for (i=0; msg->controls && msg->controls[i]; i++) {
139 if (!msg->controls_decoded[i] &&
140 msg->controls[i]->critical) {
141 req->status = NT_STATUS_LDAP(LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
142 req->state = LDAP_REQUEST_DONE;
143 DLIST_REMOVE(conn->pending, req);
144 if (req->async.fn) {
145 req->async.fn(req);
147 return;
151 /* add to the list of replies received */
152 talloc_steal(req, msg);
153 req->replies = talloc_realloc(req, req->replies,
154 struct ldap_message *, req->num_replies+1);
155 if (req->replies == NULL) {
156 req->status = NT_STATUS_NO_MEMORY;
157 req->state = LDAP_REQUEST_DONE;
158 DLIST_REMOVE(conn->pending, req);
159 if (req->async.fn) {
160 req->async.fn(req);
162 return;
165 req->replies[req->num_replies] = talloc_steal(req->replies, msg);
166 req->num_replies++;
168 if (msg->type != LDAP_TAG_SearchResultEntry &&
169 msg->type != LDAP_TAG_SearchResultReference) {
170 /* currently only search results expect multiple
171 replies */
172 req->state = LDAP_REQUEST_DONE;
173 DLIST_REMOVE(conn->pending, req);
176 if (req->async.fn) {
177 req->async.fn(req);
183 decode/process LDAP data
185 static NTSTATUS ldap_recv_handler(void *private_data, DATA_BLOB blob)
187 NTSTATUS status;
188 struct ldap_connection *conn = talloc_get_type(private_data,
189 struct ldap_connection);
190 struct ldap_message *msg = talloc(conn, struct ldap_message);
191 struct asn1_data *asn1 = asn1_init(conn);
193 if (asn1 == NULL || msg == NULL) {
194 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
197 if (!asn1_load(asn1, blob)) {
198 talloc_free(msg);
199 talloc_free(asn1);
200 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
203 status = ldap_decode(asn1, msg);
204 if (!NT_STATUS_IS_OK(status)) {
205 asn1_free(asn1);
206 return status;
209 ldap_match_message(conn, msg);
211 data_blob_free(&blob);
212 asn1_free(asn1);
213 return NT_STATUS_OK;
216 /* Handle read events, from the GENSEC socket callback, or real events */
217 void ldap_read_io_handler(void *private_data, uint16_t flags)
219 struct ldap_connection *conn = talloc_get_type(private_data,
220 struct ldap_connection);
221 packet_recv(conn->packet);
225 handle ldap socket events
227 static void ldap_io_handler(struct event_context *ev, struct fd_event *fde,
228 uint16_t flags, void *private_data)
230 struct ldap_connection *conn = talloc_get_type(private_data,
231 struct ldap_connection);
232 if (flags & EVENT_FD_WRITE) {
233 packet_queue_run(conn->packet);
234 if (!tls_enabled(conn->sock)) return;
236 if (flags & EVENT_FD_READ) {
237 ldap_read_io_handler(private_data, flags);
242 parse a ldap URL
244 static NTSTATUS ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
245 char **host, uint16_t *port, bool *ldaps)
247 int tmp_port = 0;
248 char protocol[11];
249 char tmp_host[1025];
250 int ret;
252 /* Paranoia check */
253 SMB_ASSERT(sizeof(protocol)>10 && sizeof(tmp_host)>254);
255 ret = sscanf(url, "%10[^:]://%254[^:/]:%d", protocol, tmp_host, &tmp_port);
256 if (ret < 2) {
257 return NT_STATUS_INVALID_PARAMETER;
260 if (strequal(protocol, "ldap")) {
261 *port = 389;
262 *ldaps = false;
263 } else if (strequal(protocol, "ldaps")) {
264 *port = 636;
265 *ldaps = true;
266 } else {
267 DEBUG(0, ("unrecognised ldap protocol (%s)!\n", protocol));
268 return NT_STATUS_PROTOCOL_UNREACHABLE;
271 if (tmp_port != 0)
272 *port = tmp_port;
274 *host = talloc_strdup(mem_ctx, tmp_host);
275 NT_STATUS_HAVE_NO_MEMORY(*host);
277 return NT_STATUS_OK;
281 connect to a ldap server
284 struct ldap_connect_state {
285 struct composite_context *ctx;
286 struct ldap_connection *conn;
289 static void ldap_connect_recv_unix_conn(struct composite_context *ctx);
290 static void ldap_connect_recv_tcp_conn(struct composite_context *ctx);
292 _PUBLIC_ struct composite_context *ldap_connect_send(struct ldap_connection *conn,
293 const char *url)
295 struct composite_context *result, *ctx;
296 struct ldap_connect_state *state;
297 char protocol[11];
298 int ret;
300 result = talloc_zero(conn, struct composite_context);
301 if (result == NULL) goto failed;
302 result->state = COMPOSITE_STATE_IN_PROGRESS;
303 result->async.fn = NULL;
304 result->event_ctx = conn->event.event_ctx;
306 state = talloc(result, struct ldap_connect_state);
307 if (state == NULL) goto failed;
308 state->ctx = result;
309 result->private_data = state;
311 state->conn = conn;
313 if (conn->reconnect.url == NULL) {
314 conn->reconnect.url = talloc_strdup(conn, url);
315 if (conn->reconnect.url == NULL) goto failed;
318 /* Paranoia check */
319 SMB_ASSERT(sizeof(protocol)>10);
321 ret = sscanf(url, "%10[^:]://", protocol);
322 if (ret < 1) {
323 return NULL;
326 if (strequal(protocol, "ldapi")) {
327 struct socket_address *unix_addr;
328 char path[1025];
330 NTSTATUS status = socket_create("unix", SOCKET_TYPE_STREAM, &conn->sock, 0);
331 if (!NT_STATUS_IS_OK(status)) {
332 return NULL;
334 talloc_steal(conn, conn->sock);
335 SMB_ASSERT(sizeof(protocol)>10);
336 SMB_ASSERT(sizeof(path)>1024);
338 /* LDAPI connections are to localhost, so give the local host name as the target for gensec */
339 conn->host = talloc_asprintf(conn, "%s.%s", lp_netbios_name(conn->lp_ctx), lp_realm(conn->lp_ctx));
340 if (composite_nomem(conn->host, state->ctx)) {
341 return result;
344 /* The %c specifier doesn't null terminate :-( */
345 ZERO_STRUCT(path);
346 ret = sscanf(url, "%10[^:]://%1025c", protocol, path);
347 if (ret < 2) {
348 composite_error(state->ctx, NT_STATUS_INVALID_PARAMETER);
349 return result;
352 rfc1738_unescape(path);
354 unix_addr = socket_address_from_strings(conn, conn->sock->backend_name,
355 path, 0);
356 if (!unix_addr) {
357 return NULL;
360 ctx = socket_connect_send(conn->sock, NULL, unix_addr,
361 0, lp_resolve_context(conn->lp_ctx), conn->event.event_ctx);
362 ctx->async.fn = ldap_connect_recv_unix_conn;
363 ctx->async.private_data = state;
364 return result;
365 } else {
366 NTSTATUS status = ldap_parse_basic_url(conn, url, &conn->host,
367 &conn->port, &conn->ldaps);
368 if (!NT_STATUS_IS_OK(state->ctx->status)) {
369 composite_error(state->ctx, status);
370 return result;
373 ctx = socket_connect_multi_send(state, conn->host, 1, &conn->port,
374 lp_resolve_context(conn->lp_ctx), conn->event.event_ctx);
375 if (ctx == NULL) goto failed;
377 ctx->async.fn = ldap_connect_recv_tcp_conn;
378 ctx->async.private_data = state;
379 return result;
381 failed:
382 talloc_free(result);
383 return NULL;
386 static void ldap_connect_got_sock(struct composite_context *ctx,
387 struct ldap_connection *conn)
389 /* setup a handler for events on this socket */
390 conn->event.fde = event_add_fd(conn->event.event_ctx, conn->sock,
391 socket_get_fd(conn->sock),
392 EVENT_FD_READ | EVENT_FD_AUTOCLOSE, ldap_io_handler, conn);
393 if (conn->event.fde == NULL) {
394 composite_error(ctx, NT_STATUS_INTERNAL_ERROR);
395 return;
398 socket_set_flags(conn->sock, SOCKET_FLAG_NOCLOSE);
400 talloc_steal(conn, conn->sock);
401 if (conn->ldaps) {
402 struct socket_context *tls_socket;
403 struct socket_context *tmp_socket;
404 char *cafile = private_path(conn->sock, conn->lp_ctx, lp_tls_cafile(conn->lp_ctx));
406 if (!cafile || !*cafile) {
407 talloc_free(conn->sock);
408 return;
411 tls_socket = tls_init_client(conn->sock, conn->event.fde, cafile);
412 talloc_free(cafile);
414 if (tls_socket == NULL) {
415 talloc_free(conn->sock);
416 return;
419 /* the original socket, must become a child of the tls socket */
420 tmp_socket = conn->sock;
421 conn->sock = talloc_steal(conn, tls_socket);
422 talloc_steal(conn->sock, tmp_socket);
425 conn->packet = packet_init(conn);
426 if (conn->packet == NULL) {
427 talloc_free(conn->sock);
428 return;
431 packet_set_private(conn->packet, conn);
432 packet_set_socket(conn->packet, conn->sock);
433 packet_set_callback(conn->packet, ldap_recv_handler);
434 packet_set_full_request(conn->packet, ldap_full_packet);
435 packet_set_error_handler(conn->packet, ldap_error_handler);
436 packet_set_event_context(conn->packet, conn->event.event_ctx);
437 packet_set_fde(conn->packet, conn->event.fde);
438 /* packet_set_serialise(conn->packet); */
440 composite_done(ctx);
443 static void ldap_connect_recv_tcp_conn(struct composite_context *ctx)
445 struct ldap_connect_state *state =
446 talloc_get_type(ctx->async.private_data,
447 struct ldap_connect_state);
448 struct ldap_connection *conn = state->conn;
449 uint16_t port;
450 NTSTATUS status = socket_connect_multi_recv(ctx, state, &conn->sock,
451 &port);
452 if (!NT_STATUS_IS_OK(status)) {
453 composite_error(state->ctx, status);
454 return;
457 ldap_connect_got_sock(state->ctx, conn);
460 static void ldap_connect_recv_unix_conn(struct composite_context *ctx)
462 struct ldap_connect_state *state =
463 talloc_get_type(ctx->async.private_data,
464 struct ldap_connect_state);
465 struct ldap_connection *conn = state->conn;
467 NTSTATUS status = socket_connect_recv(ctx);
469 if (!NT_STATUS_IS_OK(state->ctx->status)) {
470 composite_error(state->ctx, status);
471 return;
474 ldap_connect_got_sock(state->ctx, conn);
477 _PUBLIC_ NTSTATUS ldap_connect_recv(struct composite_context *ctx)
479 NTSTATUS status = composite_wait(ctx);
480 talloc_free(ctx);
481 return status;
484 _PUBLIC_ NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
486 struct composite_context *ctx = ldap_connect_send(conn, url);
487 return ldap_connect_recv(ctx);
490 /* set reconnect parameters */
492 _PUBLIC_ void ldap_set_reconn_params(struct ldap_connection *conn, int max_retries)
494 if (conn) {
495 conn->reconnect.max_retries = max_retries;
496 conn->reconnect.retries = 0;
497 conn->reconnect.previous = time(NULL);
501 /* Actually this function is NOT ASYNC safe, FIXME? */
502 static void ldap_reconnect(struct ldap_connection *conn)
504 NTSTATUS status;
505 time_t now = time(NULL);
507 /* do we have set up reconnect ? */
508 if (conn->reconnect.max_retries == 0) return;
510 /* is the retry time expired ? */
511 if (now > conn->reconnect.previous + 30) {
512 conn->reconnect.retries = 0;
513 conn->reconnect.previous = now;
516 /* are we reconnectind too often and too fast? */
517 if (conn->reconnect.retries > conn->reconnect.max_retries) return;
519 /* keep track of the number of reconnections */
520 conn->reconnect.retries++;
522 /* reconnect */
523 status = ldap_connect(conn, conn->reconnect.url);
524 if ( ! NT_STATUS_IS_OK(status)) {
525 return;
528 /* rebind */
529 status = ldap_rebind(conn);
530 if ( ! NT_STATUS_IS_OK(status)) {
531 ldap_connection_dead(conn);
535 /* destroy an open ldap request */
536 static int ldap_request_destructor(struct ldap_request *req)
538 if (req->state == LDAP_REQUEST_PENDING) {
539 DLIST_REMOVE(req->conn->pending, req);
541 return 0;
545 called on timeout of a ldap request
547 static void ldap_request_timeout(struct event_context *ev, struct timed_event *te,
548 struct timeval t, void *private_data)
550 struct ldap_request *req = talloc_get_type(private_data, struct ldap_request);
551 req->status = NT_STATUS_IO_TIMEOUT;
552 if (req->state == LDAP_REQUEST_PENDING) {
553 DLIST_REMOVE(req->conn->pending, req);
555 req->state = LDAP_REQUEST_DONE;
556 if (req->async.fn) {
557 req->async.fn(req);
563 called on completion of a one-way ldap request
565 static void ldap_request_complete(struct event_context *ev, struct timed_event *te,
566 struct timeval t, void *private_data)
568 struct ldap_request *req = talloc_get_type(private_data, struct ldap_request);
569 if (req->async.fn) {
570 req->async.fn(req);
575 send a ldap message - async interface
577 _PUBLIC_ struct ldap_request *ldap_request_send(struct ldap_connection *conn,
578 struct ldap_message *msg)
580 struct ldap_request *req;
581 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
583 req = talloc_zero(conn, struct ldap_request);
584 if (req == NULL) return NULL;
586 if (conn->sock == NULL) {
587 status = NT_STATUS_INVALID_CONNECTION;
588 goto failed;
591 req->state = LDAP_REQUEST_SEND;
592 req->conn = conn;
593 req->messageid = conn->next_messageid++;
594 if (conn->next_messageid == 0) {
595 conn->next_messageid = 1;
597 req->type = msg->type;
598 if (req->messageid == -1) {
599 goto failed;
602 talloc_set_destructor(req, ldap_request_destructor);
604 msg->messageid = req->messageid;
606 if (!ldap_encode(msg, &req->data, req)) {
607 status = NT_STATUS_INTERNAL_ERROR;
608 goto failed;
611 status = packet_send(conn->packet, req->data);
612 if (!NT_STATUS_IS_OK(status)) {
613 goto failed;
616 /* some requests don't expect a reply, so don't add those to the
617 pending queue */
618 if (req->type == LDAP_TAG_AbandonRequest ||
619 req->type == LDAP_TAG_UnbindRequest) {
620 req->status = NT_STATUS_OK;
621 req->state = LDAP_REQUEST_DONE;
622 /* we can't call the async callback now, as it isn't setup, so
623 call it as next event */
624 event_add_timed(conn->event.event_ctx, req, timeval_zero(),
625 ldap_request_complete, req);
626 return req;
629 req->state = LDAP_REQUEST_PENDING;
630 DLIST_ADD(conn->pending, req);
632 /* put a timeout on the request */
633 req->time_event = event_add_timed(conn->event.event_ctx, req,
634 timeval_current_ofs(conn->timeout, 0),
635 ldap_request_timeout, req);
637 return req;
639 failed:
640 req->status = status;
641 req->state = LDAP_REQUEST_ERROR;
642 event_add_timed(conn->event.event_ctx, req, timeval_zero(),
643 ldap_request_complete, req);
645 return req;
650 wait for a request to complete
651 note that this does not destroy the request
653 _PUBLIC_ NTSTATUS ldap_request_wait(struct ldap_request *req)
655 while (req->state < LDAP_REQUEST_DONE) {
656 if (event_loop_once(req->conn->event.event_ctx) != 0) {
657 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
658 break;
661 return req->status;
666 a mapping of ldap response code to strings
668 static const struct {
669 enum ldap_result_code code;
670 const char *str;
671 } ldap_code_map[] = {
672 #define _LDAP_MAP_CODE(c) { c, #c }
673 _LDAP_MAP_CODE(LDAP_SUCCESS),
674 _LDAP_MAP_CODE(LDAP_OPERATIONS_ERROR),
675 _LDAP_MAP_CODE(LDAP_PROTOCOL_ERROR),
676 _LDAP_MAP_CODE(LDAP_TIME_LIMIT_EXCEEDED),
677 _LDAP_MAP_CODE(LDAP_SIZE_LIMIT_EXCEEDED),
678 _LDAP_MAP_CODE(LDAP_COMPARE_FALSE),
679 _LDAP_MAP_CODE(LDAP_COMPARE_TRUE),
680 _LDAP_MAP_CODE(LDAP_AUTH_METHOD_NOT_SUPPORTED),
681 _LDAP_MAP_CODE(LDAP_STRONG_AUTH_REQUIRED),
682 _LDAP_MAP_CODE(LDAP_REFERRAL),
683 _LDAP_MAP_CODE(LDAP_ADMIN_LIMIT_EXCEEDED),
684 _LDAP_MAP_CODE(LDAP_UNAVAILABLE_CRITICAL_EXTENSION),
685 _LDAP_MAP_CODE(LDAP_CONFIDENTIALITY_REQUIRED),
686 _LDAP_MAP_CODE(LDAP_SASL_BIND_IN_PROGRESS),
687 _LDAP_MAP_CODE(LDAP_NO_SUCH_ATTRIBUTE),
688 _LDAP_MAP_CODE(LDAP_UNDEFINED_ATTRIBUTE_TYPE),
689 _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_MATCHING),
690 _LDAP_MAP_CODE(LDAP_CONSTRAINT_VIOLATION),
691 _LDAP_MAP_CODE(LDAP_ATTRIBUTE_OR_VALUE_EXISTS),
692 _LDAP_MAP_CODE(LDAP_INVALID_ATTRIBUTE_SYNTAX),
693 _LDAP_MAP_CODE(LDAP_NO_SUCH_OBJECT),
694 _LDAP_MAP_CODE(LDAP_ALIAS_PROBLEM),
695 _LDAP_MAP_CODE(LDAP_INVALID_DN_SYNTAX),
696 _LDAP_MAP_CODE(LDAP_ALIAS_DEREFERENCING_PROBLEM),
697 _LDAP_MAP_CODE(LDAP_INAPPROPRIATE_AUTHENTICATION),
698 _LDAP_MAP_CODE(LDAP_INVALID_CREDENTIALS),
699 _LDAP_MAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTS),
700 _LDAP_MAP_CODE(LDAP_BUSY),
701 _LDAP_MAP_CODE(LDAP_UNAVAILABLE),
702 _LDAP_MAP_CODE(LDAP_UNWILLING_TO_PERFORM),
703 _LDAP_MAP_CODE(LDAP_LOOP_DETECT),
704 _LDAP_MAP_CODE(LDAP_NAMING_VIOLATION),
705 _LDAP_MAP_CODE(LDAP_OBJECT_CLASS_VIOLATION),
706 _LDAP_MAP_CODE(LDAP_NOT_ALLOWED_ON_NON_LEAF),
707 _LDAP_MAP_CODE(LDAP_NOT_ALLOWED_ON_RDN),
708 _LDAP_MAP_CODE(LDAP_ENTRY_ALREADY_EXISTS),
709 _LDAP_MAP_CODE(LDAP_OBJECT_CLASS_MODS_PROHIBITED),
710 _LDAP_MAP_CODE(LDAP_AFFECTS_MULTIPLE_DSAS),
711 _LDAP_MAP_CODE(LDAP_OTHER)
715 used to setup the status code from a ldap response
717 _PUBLIC_ NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
719 int i;
720 const char *codename = "unknown";
722 if (r->resultcode == LDAP_SUCCESS) {
723 return NT_STATUS_OK;
726 if (conn->last_error) {
727 talloc_free(conn->last_error);
730 for (i=0;i<ARRAY_SIZE(ldap_code_map);i++) {
731 if (r->resultcode == ldap_code_map[i].code) {
732 codename = ldap_code_map[i].str;
733 break;
737 conn->last_error = talloc_asprintf(conn, "LDAP error %u %s - %s <%s> <%s>",
738 r->resultcode,
739 codename,
740 r->dn?r->dn:"(NULL)",
741 r->errormessage?r->errormessage:"",
742 r->referral?r->referral:"");
744 return NT_STATUS_LDAP(r->resultcode);
748 return error string representing the last error
750 _PUBLIC_ const char *ldap_errstr(struct ldap_connection *conn,
751 TALLOC_CTX *mem_ctx,
752 NTSTATUS status)
754 if (NT_STATUS_IS_LDAP(status) && conn->last_error != NULL) {
755 return talloc_strdup(mem_ctx, conn->last_error);
757 return talloc_asprintf(mem_ctx, "LDAP client internal error: %s", nt_errstr(status));
762 return the Nth result message, waiting if necessary
764 _PUBLIC_ NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
766 *msg = NULL;
768 NT_STATUS_HAVE_NO_MEMORY(req);
770 while (req->state < LDAP_REQUEST_DONE && n >= req->num_replies) {
771 if (event_loop_once(req->conn->event.event_ctx) != 0) {
772 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
776 if (n < req->num_replies) {
777 *msg = req->replies[n];
778 return NT_STATUS_OK;
781 if (!NT_STATUS_IS_OK(req->status)) {
782 return req->status;
785 return NT_STATUS_NO_MORE_ENTRIES;
790 return a single result message, checking if it is of the expected LDAP type
792 _PUBLIC_ NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
794 NTSTATUS status;
795 status = ldap_result_n(req, 0, msg);
796 if (!NT_STATUS_IS_OK(status)) {
797 return status;
799 if ((*msg)->type != type) {
800 *msg = NULL;
801 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
803 return status;
807 a simple ldap transaction, for single result requests that only need a status code
808 this relies on single valued requests having the response type == request type + 1
810 _PUBLIC_ NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
812 struct ldap_request *req = ldap_request_send(conn, msg);
813 struct ldap_message *res;
814 NTSTATUS status;
815 status = ldap_result_n(req, 0, &res);
816 if (!NT_STATUS_IS_OK(status)) {
817 talloc_free(req);
818 return status;
820 if (res->type != msg->type + 1) {
821 talloc_free(req);
822 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
824 status = ldap_check_response(conn, &res->r.GeneralResult);
825 talloc_free(req);
826 return status;