2 ldb database library - ildap backend
4 Copyright (C) Andrew Tridgell 2005
5 Copyright (C) Simo Sorce 2006
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb ildap backend
31 * Description: This is a ldb backend for the internal ldap
32 * client library in Samba4. By using this backend we are
33 * independent of a system ldap library
35 * Author: Andrew Tridgell
39 * - description: make the module use asyncronous calls
46 #include "ldb/include/includes.h"
48 #include "lib/events/events.h"
49 #include "libcli/ldap/ldap.h"
50 #include "libcli/ldap/ldap_client.h"
51 #include "auth/auth.h"
54 struct ldap_connection *ldap;
55 struct ldb_context *ldb;
59 struct ldb_module *module;
60 struct ldap_request *req;
62 int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
66 convert a ldb_message structure to a list of ldap_mod structures
67 ready for ildap_add() or ildap_modify()
69 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
70 const struct ldb_message *msg, int use_flags)
72 struct ldap_mod **mods;
76 /* allocate maximum number of elements needed */
77 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
84 for (i = 0; i < msg->num_elements; i++) {
85 const struct ldb_message_element *el = &msg->elements[i];
87 mods[n] = talloc(mods, struct ldap_mod);
93 mods[n]->attrib = *el;
95 switch (el->flags & LDB_FLAG_MOD_MASK) {
96 case LDB_FLAG_MOD_ADD:
97 mods[n]->type = LDAP_MODIFY_ADD;
99 case LDB_FLAG_MOD_DELETE:
100 mods[n]->type = LDAP_MODIFY_DELETE;
102 case LDB_FLAG_MOD_REPLACE:
103 mods[n]->type = LDAP_MODIFY_REPLACE;
120 map an ildap NTSTATUS to a ldb error code
122 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
124 if (NT_STATUS_IS_OK(status)) {
127 ldb_set_errstring(ildb->ldb, ldap_errstr(ildb->ldap, status));
128 if (NT_STATUS_IS_LDAP(status)) {
129 return NT_STATUS_LDAP_CODE(status);
131 return LDB_ERR_OPERATIONS_ERROR;
134 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
135 struct timeval t, void *private_data)
137 struct ldb_handle *handle = talloc_get_type(private_data, struct ldb_handle);
138 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
140 if (ac->req->state == LDAP_REQUEST_PENDING) {
141 DLIST_REMOVE(ac->req->conn->pending, ac->req);
144 handle->status = LDB_ERR_TIME_LIMIT_EXCEEDED;
149 static void ildb_callback(struct ldap_request *req)
151 struct ldb_handle *handle = talloc_get_type(req->async.private_data, struct ldb_handle);
152 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
153 struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private);
157 handle->status = LDB_SUCCESS;
159 if (!NT_STATUS_IS_OK(req->status)) {
160 handle->status = ildb_map_error(ildb, req->status);
164 if (req->num_replies < 1) {
165 handle->status = LDB_ERR_OPERATIONS_ERROR;
171 case LDAP_TAG_ModifyRequest:
172 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
173 handle->status = LDB_ERR_PROTOCOL_ERROR;
176 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
177 handle->status = ildb_map_error(ildb, status);
178 if (ac->callback && handle->status == LDB_SUCCESS) {
179 /* FIXME: build a corresponding ares to pass on */
180 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
182 handle->state = LDB_ASYNC_DONE;
185 case LDAP_TAG_AddRequest:
186 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
187 handle->status = LDB_ERR_PROTOCOL_ERROR;
190 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
191 handle->status = ildb_map_error(ildb, status);
192 if (ac->callback && handle->status == LDB_SUCCESS) {
193 /* FIXME: build a corresponding ares to pass on */
194 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
196 handle->state = LDB_ASYNC_DONE;
199 case LDAP_TAG_DelRequest:
200 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
201 handle->status = LDB_ERR_PROTOCOL_ERROR;
204 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
205 handle->status = ildb_map_error(ildb, status);
206 if (ac->callback && handle->status == LDB_SUCCESS) {
207 /* FIXME: build a corresponding ares to pass on */
208 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
210 handle->state = LDB_ASYNC_DONE;
213 case LDAP_TAG_ModifyDNRequest:
214 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
215 handle->status = LDB_ERR_PROTOCOL_ERROR;
218 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
219 handle->status = ildb_map_error(ildb, status);
220 if (ac->callback && handle->status == LDB_SUCCESS) {
221 /* FIXME: build a corresponding ares to pass on */
222 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
224 handle->state = LDB_ASYNC_DONE;
227 case LDAP_TAG_SearchRequest:
228 /* loop over all messages */
229 for (i = 0; i < req->num_replies; i++) {
230 struct ldap_SearchResEntry *search;
231 struct ldb_reply *ares = NULL;
232 struct ldap_message *msg;
235 ares = talloc_zero(ac, struct ldb_reply);
237 handle->status = LDB_ERR_OPERATIONS_ERROR;
241 msg = req->replies[i];
244 case LDAP_TAG_SearchResultDone:
246 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
247 if (!NT_STATUS_IS_OK(status)) {
248 handle->status = ildb_map_error(ildb, status);
252 ares->controls = talloc_move(ares, msg->controls);
253 if (msg->r.SearchResultDone.resultcode) {
254 if (msg->r.SearchResultDone.errormessage) {
255 ldb_set_errstring(ac->module->ldb, msg->r.SearchResultDone.errormessage);
259 handle->status = msg->r.SearchResultDone.resultcode;
260 handle->state = LDB_ASYNC_DONE;
261 ares->type = LDB_REPLY_DONE;
264 case LDAP_TAG_SearchResultEntry:
267 ares->message = ldb_msg_new(ares);
268 if (!ares->message) {
269 handle->status = LDB_ERR_OPERATIONS_ERROR;
273 search = &(msg->r.SearchResultEntry);
275 ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn);
276 if (ares->message->dn == NULL) {
277 handle->status = LDB_ERR_OPERATIONS_ERROR;
280 ares->message->num_elements = search->num_attributes;
281 ares->message->elements = talloc_move(ares->message, search->attributes);
283 handle->status = LDB_SUCCESS;
284 handle->state = LDB_ASYNC_PENDING;
285 ares->type = LDB_REPLY_ENTRY;
288 case LDAP_TAG_SearchResultReference:
290 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
292 handle->status = LDB_SUCCESS;
293 handle->state = LDB_ASYNC_PENDING;
294 ares->type = LDB_REPLY_REFERRAL;
298 /* TAG not handled, fail ! */
299 handle->status = LDB_ERR_PROTOCOL_ERROR;
303 ret = ac->callback(ac->module->ldb, ac->context, ares);
305 handle->status = ret;
309 talloc_free(req->replies);
311 req->num_replies = 0;
316 handle->status = LDB_ERR_PROTOCOL_ERROR;
321 static struct ldb_handle *init_ildb_handle(struct ldb_module *module,
323 int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
325 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
326 struct ildb_context *ildb_ac;
327 struct ldb_handle *h;
329 h = talloc_zero(ildb->ldap, struct ldb_handle);
331 ldb_set_errstring(module->ldb, "Out of Memory");
337 ildb_ac = talloc(h, struct ildb_context);
338 if (ildb_ac == NULL) {
339 ldb_set_errstring(module->ldb, "Out of Memory");
344 h->private_data = (void *)ildb_ac;
346 h->state = LDB_ASYNC_INIT;
347 h->status = LDB_SUCCESS;
349 ildb_ac->module = module;
350 ildb_ac->context = context;
351 ildb_ac->callback = callback;
356 static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
358 int (*callback)(struct ldb_context *, void *, struct ldb_reply *),
360 struct ldb_handle **handle)
362 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
363 struct ldb_handle *h = init_ildb_handle(module, context, callback);
364 struct ildb_context *ildb_ac;
365 struct ldap_request *req;
368 return LDB_ERR_OPERATIONS_ERROR;
371 ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
373 req = ldap_request_send(ildb->ldap, msg);
375 ldb_set_errstring(module->ldb, "async send request failed");
376 return LDB_ERR_OPERATIONS_ERROR;
380 ldb_set_errstring(module->ldb, "connection to remote LDAP server dropped?");
381 return LDB_ERR_OPERATIONS_ERROR;
384 talloc_free(req->time_event);
385 req->time_event = NULL;
387 req->time_event = event_add_timed(req->conn->event.event_ctx, h,
388 timeval_current_ofs(timeout, 0),
389 ildb_request_timeout, h);
392 req->async.fn = ildb_callback;
393 req->async.private_data = (void *)h;
394 ildb_ac->req = talloc_move(ildb_ac, req);
400 static int ildb_request_noop(struct ldb_module *module, struct ldb_request *req)
402 struct ldb_handle *h = init_ildb_handle(module, req->context, req->callback);
403 struct ildb_context *ildb_ac;
404 int ret = LDB_SUCCESS;
407 return LDB_ERR_OPERATIONS_ERROR;
410 ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
414 if (ildb_ac->callback) {
415 ret = ildb_ac->callback(module->ldb, ildb_ac->context, NULL);
417 req->handle->state = LDB_ASYNC_DONE;
422 search for matching records using an asynchronous function
424 static int ildb_search(struct ldb_module *module, struct ldb_request *req)
426 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
427 struct ldap_message *msg;
432 if (!req->callback || !req->context) {
433 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
434 return LDB_ERR_OPERATIONS_ERROR;
437 if (req->op.search.tree == NULL) {
438 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
439 return LDB_ERR_OPERATIONS_ERROR;
442 msg = new_ldap_message(ildb);
444 ldb_set_errstring(module->ldb, "Out of Memory");
445 return LDB_ERR_OPERATIONS_ERROR;
448 msg->type = LDAP_TAG_SearchRequest;
450 if (req->op.search.base == NULL) {
451 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
453 msg->r.SearchRequest.basedn = ldb_dn_linearize(msg, req->op.search.base);
455 if (msg->r.SearchRequest.basedn == NULL) {
456 ldb_set_errstring(module->ldb, "Unable to determine baseDN");
458 return LDB_ERR_OPERATIONS_ERROR;
461 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
462 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
464 msg->r.SearchRequest.scope = req->op.search.scope;
467 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
468 msg->r.SearchRequest.timelimit = 0;
469 msg->r.SearchRequest.sizelimit = 0;
470 msg->r.SearchRequest.attributesonly = 0;
471 msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
473 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
474 msg->r.SearchRequest.num_attributes = n;
475 msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
476 msg->controls = req->controls;
478 return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
484 static int ildb_add(struct ldb_module *module, struct ldb_request *req)
486 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
487 struct ldap_message *msg;
488 struct ldap_mod **mods;
493 /* ignore ltdb specials */
494 if (ldb_dn_is_special(req->op.add.message->dn)) {
495 return ildb_request_noop(module, req);
498 msg = new_ldap_message(ildb->ldap);
500 return LDB_ERR_OPERATIONS_ERROR;
503 msg->type = LDAP_TAG_AddRequest;
505 msg->r.AddRequest.dn = ldb_dn_linearize(msg, req->op.add.message->dn);
506 if (msg->r.AddRequest.dn == NULL) {
508 return LDB_ERR_INVALID_DN_SYNTAX;
511 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
514 return LDB_ERR_OPERATIONS_ERROR;
517 msg->r.AddRequest.num_attributes = n;
518 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
519 if (msg->r.AddRequest.attributes == NULL) {
521 return LDB_ERR_OPERATIONS_ERROR;
524 for (i = 0; i < n; i++) {
525 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
528 return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
534 static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
536 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
537 struct ldap_message *msg;
538 struct ldap_mod **mods;
543 /* ignore ltdb specials */
544 if (ldb_dn_is_special(req->op.mod.message->dn)) {
545 return ildb_request_noop(module, req);
548 msg = new_ldap_message(ildb->ldap);
550 return LDB_ERR_OPERATIONS_ERROR;
553 msg->type = LDAP_TAG_ModifyRequest;
555 msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, req->op.mod.message->dn);
556 if (msg->r.ModifyRequest.dn == NULL) {
558 return LDB_ERR_INVALID_DN_SYNTAX;
561 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
564 return LDB_ERR_OPERATIONS_ERROR;
567 msg->r.ModifyRequest.num_mods = n;
568 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
569 if (msg->r.ModifyRequest.mods == NULL) {
571 return LDB_ERR_OPERATIONS_ERROR;
574 for (i = 0; i < n; i++) {
575 msg->r.ModifyRequest.mods[i] = *mods[i];
578 return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
584 static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
586 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
587 struct ldap_message *msg;
591 /* ignore ltdb specials */
592 if (ldb_dn_is_special(req->op.del.dn)) {
593 return ildb_request_noop(module, req);
596 msg = new_ldap_message(ildb->ldap);
598 return LDB_ERR_OPERATIONS_ERROR;
601 msg->type = LDAP_TAG_DelRequest;
603 msg->r.DelRequest.dn = ldb_dn_linearize(msg, req->op.del.dn);
604 if (msg->r.DelRequest.dn == NULL) {
606 return LDB_ERR_INVALID_DN_SYNTAX;
609 return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
615 static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
617 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
618 struct ldap_message *msg;
622 /* ignore ltdb specials */
623 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
624 return ildb_request_noop(module, req);
627 msg = new_ldap_message(ildb->ldap);
629 return LDB_ERR_OPERATIONS_ERROR;
632 msg->type = LDAP_TAG_ModifyDNRequest;
633 msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, req->op.rename.olddn);
634 if (msg->r.ModifyDNRequest.dn == NULL) {
636 return LDB_ERR_INVALID_DN_SYNTAX;
639 msg->r.ModifyDNRequest.newrdn =
640 talloc_asprintf(msg, "%s=%s",
641 req->op.rename.newdn->components[0].name,
642 ldb_dn_escape_value(msg, req->op.rename.newdn->components[0].value));
643 if (msg->r.ModifyDNRequest.newrdn == NULL) {
645 return LDB_ERR_OPERATIONS_ERROR;
648 msg->r.ModifyDNRequest.newsuperior =
649 ldb_dn_linearize(msg,
650 ldb_dn_get_parent(msg, req->op.rename.newdn));
651 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
653 return LDB_ERR_INVALID_DN_SYNTAX;
656 msg->r.ModifyDNRequest.deleteolddn = True;
658 return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
661 static int ildb_start_trans(struct ldb_module *module)
663 /* TODO implement a local locking mechanism here */
668 static int ildb_end_trans(struct ldb_module *module)
670 /* TODO implement a local transaction mechanism here */
675 static int ildb_del_trans(struct ldb_module *module)
677 /* TODO implement a local locking mechanism here */
682 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
684 return LDB_ERR_OPERATIONS_ERROR;
687 static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
689 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
691 if (handle->state == LDB_ASYNC_DONE) {
692 return handle->status;
696 return LDB_ERR_OPERATIONS_ERROR;
699 handle->state = LDB_ASYNC_INIT;
703 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
704 return LDB_ERR_OTHER;
708 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
709 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
710 return LDB_ERR_OTHER;
715 return LDB_ERR_OPERATIONS_ERROR;
718 return handle->status;
721 static const struct ldb_module_ops ildb_ops = {
723 .search = ildb_search,
725 .modify = ildb_modify,
727 .rename = ildb_rename,
728 .request = ildb_request,
729 .start_transaction = ildb_start_trans,
730 .end_transaction = ildb_end_trans,
731 .del_transaction = ildb_del_trans,
736 connect to the database
738 static int ildb_connect(struct ldb_context *ldb, const char *url,
739 unsigned int flags, const char *options[],
740 struct ldb_module **module)
742 struct ildb_private *ildb = NULL;
744 struct cli_credentials *creds;
746 ildb = talloc(ldb, struct ildb_private);
754 ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
760 if (flags & LDB_FLG_RECONNECT) {
761 ldap_set_reconn_params(ildb->ldap, 10);
764 status = ldap_connect(ildb->ldap, url);
765 if (!NT_STATUS_IS_OK(status)) {
766 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
767 url, ldap_errstr(ildb->ldap, status));
772 *module = talloc(ldb, struct ldb_module);
778 (*module)->ldb = ldb;
779 (*module)->prev = (*module)->next = NULL;
780 (*module)->private_data = ildb;
781 (*module)->ops = &ildb_ops;
783 /* caller can optionally setup credentials using the opaque token 'credentials' */
784 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
786 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
788 creds = session_info->credentials;
792 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
793 const char *bind_dn = cli_credentials_get_bind_dn(creds);
795 const char *password = cli_credentials_get_password(creds);
796 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
797 if (!NT_STATUS_IS_OK(status)) {
798 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
799 ldap_errstr(ildb->ldap, status));
803 status = ldap_bind_sasl(ildb->ldap, creds);
804 if (!NT_STATUS_IS_OK(status)) {
805 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
806 ldap_errstr(ildb->ldap, status));
819 int ldb_ildap_init(void)
821 return ldb_register_backend("ldap", ildb_connect) +
822 ldb_register_backend("ldapi", ildb_connect) +
823 ldb_register_backend("ldaps", ildb_connect);