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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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"
40 create a new ldap_connection stucture. The event context is optional
42 struct ldap_connection *ldap_new_connection(TALLOC_CTX *mem_ctx,
43 struct event_context *ev)
45 struct ldap_connection *conn;
47 conn = talloc_zero(mem_ctx, struct ldap_connection);
53 ev = event_context_init(conn);
60 conn->next_messageid = 1;
61 conn->event.event_ctx = ev;
63 /* set a reasonable request timeout */
71 the connection is dead
73 static void ldap_connection_dead(struct ldap_connection *conn)
75 struct ldap_request *req;
77 while (conn->pending) {
79 DLIST_REMOVE(req->conn->pending, req);
80 req->state = LDAP_REQUEST_DONE;
81 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
87 talloc_free(conn->tls);
94 static void ldap_error_handler(void *private, NTSTATUS status)
96 struct ldap_connection *conn = talloc_get_type(private,
97 struct ldap_connection);
98 ldap_connection_dead(conn);
103 match up with a pending message, adding to the replies list
105 static void ldap_match_message(struct ldap_connection *conn, struct ldap_message *msg)
107 struct ldap_request *req;
109 for (req=conn->pending; req; req=req->next) {
110 if (req->messageid == msg->messageid) break;
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) {
118 DEBUG(0,("ldap: no matching message id for %u\n",
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);
138 req->replies[req->num_replies] = talloc_steal(req->replies, msg);
141 if (msg->type != LDAP_TAG_SearchResultEntry &&
142 msg->type != LDAP_TAG_SearchResultReference) {
143 /* currently only search results expect multiple
145 req->state = LDAP_REQUEST_DONE;
146 DLIST_REMOVE(conn->pending, req);
156 check if a blob is a complete ldap packet
157 handle wrapper or unwrapped connections
159 NTSTATUS ldap_complete_packet(void *private, DATA_BLOB blob, size_t *size)
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);
166 return ldap_full_packet(private, blob, size);
170 decode/process plain data
172 static NTSTATUS ldap_decode_plain(struct ldap_connection *conn, DATA_BLOB blob)
174 struct asn1_data asn1;
175 struct ldap_message *msg = talloc(conn, struct ldap_message);
178 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
181 if (!asn1_load(&asn1, blob)) {
182 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
185 if (!ldap_decode(&asn1, msg)) {
186 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
189 ldap_match_message(conn, msg);
191 data_blob_free(&blob);
197 decode/process wrapped data
199 static NTSTATUS ldap_decode_wrapped(struct ldap_connection *conn, DATA_BLOB blob)
201 DATA_BLOB wrapped, unwrapped;
202 struct asn1_data asn1;
203 struct ldap_message *msg = talloc(conn, struct ldap_message);
207 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
210 wrapped = data_blob_const(blob.data+4, blob.length-4);
212 status = gensec_unwrap(conn->gensec, msg, &wrapped, &unwrapped);
213 if (!NT_STATUS_IS_OK(status)) {
214 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
217 data_blob_free(&blob);
219 if (!asn1_load(&asn1, unwrapped)) {
220 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
223 while (ldap_decode(&asn1, msg)) {
224 ldap_match_message(conn, msg);
225 msg = talloc(conn, struct ldap_message);
236 handle ldap recv events
238 static NTSTATUS ldap_recv_handler(void *private, DATA_BLOB blob)
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);
246 return ldap_decode_plain(conn, blob);
251 handle ldap socket events
253 static void ldap_io_handler(struct event_context *ev, struct fd_event *fde,
254 uint16_t flags, void *private)
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;
262 if (flags & EVENT_FD_READ) {
263 packet_recv(conn->packet);
270 static NTSTATUS ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
271 char **host, uint16_t *port, BOOL *ldaps)
279 /* skip leading "URL:" (if any) */
280 if (strncasecmp(p, "URL:", 4) == 0) {
285 SMB_ASSERT(sizeof(protocol)>10 && sizeof(tmp_host)>254);
287 ret = sscanf(p, "%10[^:]://%254[^:/]:%d", protocol, tmp_host, &tmp_port);
289 return NT_STATUS_INVALID_PARAMETER;
292 if (strequal(protocol, "ldap")) {
295 } else if (strequal(protocol, "ldaps")) {
299 DEBUG(0, ("unrecognised ldap protocol (%s)!\n", protocol));
300 return NT_STATUS_PROTOCOL_UNREACHABLE;
306 *host = talloc_strdup(mem_ctx, tmp_host);
307 NT_STATUS_HAVE_NO_MEMORY(*host);
313 connect to a ldap server
316 struct ldap_connect_state {
317 struct composite_context *ctx;
318 struct ldap_connection *conn;
321 static void ldap_connect_recv_conn(struct composite_context *ctx);
323 struct composite_context *ldap_connect_send(struct ldap_connection *conn,
326 struct composite_context *result, *ctx;
327 struct ldap_connect_state *state;
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;
335 state = talloc(result, struct ldap_connect_state);
336 if (state == NULL) goto failed;
338 result->private_data = state;
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);
349 ctx = socket_connect_multi_send(state, conn->host, 1, &conn->port,
350 conn->event.event_ctx);
351 if (ctx == NULL) goto failed;
353 ctx->async.fn = ldap_connect_recv_conn;
354 ctx->async.private_data = state;
362 static void ldap_connect_recv_conn(struct composite_context *ctx)
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;
370 state->ctx->status = socket_connect_multi_recv(ctx, state, &conn->sock,
372 if (!composite_is_ok(state->ctx)) return;
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);
383 conn->tls = tls_init_client(conn->sock, conn->event.fde, conn->ldaps);
384 if (conn->tls == NULL) {
385 talloc_free(conn->sock);
388 talloc_steal(conn, conn->tls);
389 talloc_steal(conn->tls, conn->sock);
391 conn->packet = packet_init(conn);
392 if (conn->packet == NULL) {
393 talloc_free(conn->sock);
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);
405 composite_done(state->ctx);
410 NTSTATUS ldap_connect_recv(struct composite_context *ctx)
412 NTSTATUS status = composite_wait(ctx);
417 NTSTATUS ldap_connect(struct ldap_connection *conn, const char *url)
419 struct composite_context *ctx = ldap_connect_send(conn, url);
420 return ldap_connect_recv(ctx);
423 /* destroy an open ldap request */
424 static int ldap_request_destructor(void *ptr)
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);
434 called on timeout of a ldap request
436 static void ldap_request_timeout(struct event_context *ev, struct timed_event *te,
437 struct timeval t, void *private)
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);
444 req->state = LDAP_REQUEST_DONE;
452 called on completion of a one-way ldap request
454 static void ldap_request_complete(struct event_context *ev, struct timed_event *te,
455 struct timeval t, void *private)
457 struct ldap_request *req = talloc_get_type(private, struct ldap_request);
464 send a ldap message - async interface
466 struct ldap_request *ldap_request_send(struct ldap_connection *conn,
467 struct ldap_message *msg)
469 struct ldap_request *req;
472 if (conn->tls == NULL) {
476 req = talloc_zero(conn, struct ldap_request);
477 if (req == NULL) goto failed;
479 req->state = LDAP_REQUEST_SEND;
481 req->messageid = conn->next_messageid++;
482 if (conn->next_messageid == 0) {
483 conn->next_messageid = 1;
485 req->type = msg->type;
486 if (req->messageid == -1) {
490 talloc_set_destructor(req, ldap_request_destructor);
492 msg->messageid = req->messageid;
494 if (!ldap_encode(msg, &req->data, req)) {
498 /* possibly encrypt/sign the request */
499 if (conn->enable_wrap) {
502 status = gensec_wrap(conn->gensec, req, &req->data, &wrapped);
503 if (!NT_STATUS_IS_OK(status)) {
506 data_blob_free(&req->data);
507 req->data = data_blob_talloc(req, NULL, wrapped.length + 4);
508 if (req->data.data == NULL) {
511 RSIVAL(req->data.data, 0, wrapped.length);
512 memcpy(req->data.data+4, wrapped.data, wrapped.length);
513 data_blob_free(&wrapped);
516 status = packet_send(conn->packet, req->data);
517 if (!NT_STATUS_IS_OK(status)) {
521 /* some requests don't expect a reply, so don't add those to the
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);
534 req->state = LDAP_REQUEST_PENDING;
535 DLIST_ADD(conn->pending, req);
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);
551 wait for a request to complete
552 note that this does not destroy the request
554 NTSTATUS ldap_request_wait(struct ldap_request *req)
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;
567 a mapping of ldap response code to strings
569 static const struct {
570 enum ldap_result_code code;
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)
616 used to setup the status code from a ldap response
618 NTSTATUS ldap_check_response(struct ldap_connection *conn, struct ldap_Result *r)
621 const char *codename = "unknown";
623 if (r->resultcode == LDAP_SUCCESS) {
627 if (conn->last_error) {
628 talloc_free(conn->last_error);
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;
638 conn->last_error = talloc_asprintf(conn, "LDAP error %u %s - %s <%s> <%s>",
641 r->dn?r->dn:"(NULL)",
642 r->errormessage?r->errormessage:"",
643 r->referral?r->referral:"");
645 return NT_STATUS_LDAP(r->resultcode);
649 return error string representing the last error
651 const char *ldap_errstr(struct ldap_connection *conn, NTSTATUS status)
653 if (NT_STATUS_IS_LDAP(status) && conn->last_error != NULL) {
654 return conn->last_error;
656 return nt_errstr(status);
661 return the Nth result message, waiting if necessary
663 NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **msg)
667 NT_STATUS_HAVE_NO_MEMORY(req);
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;
675 if (n < req->num_replies) {
676 *msg = req->replies[n];
680 if (!NT_STATUS_IS_OK(req->status)) {
684 return NT_STATUS_NO_MORE_ENTRIES;
689 return a single result message, checking if it is of the expected LDAP type
691 NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, int type)
694 status = ldap_result_n(req, 0, msg);
695 if (!NT_STATUS_IS_OK(status)) {
698 if ((*msg)->type != type) {
700 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
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
709 NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
711 struct ldap_request *req = ldap_request_send(conn, msg);
712 struct ldap_message *res;
714 status = ldap_result_n(req, 0, &res);
715 if (!NT_STATUS_IS_OK(status)) {
719 if (res->type != msg->type + 1) {
721 return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
723 status = ldap_check_response(conn, &res->r.GeneralResult);