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 "lib/cmdline/popt_common.h"
52 #include "auth/auth.h"
55 struct ldap_connection *ldap;
56 struct ldb_message *rootDSE;
57 struct ldb_context *ldb;
60 struct ildb_async_context {
61 struct ldb_module *module;
62 struct ldap_request *req;
64 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
68 convert a ldb_message structure to a list of ldap_mod structures
69 ready for ildap_add() or ildap_modify()
71 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
72 const struct ldb_message *msg, int use_flags)
74 struct ldap_mod **mods;
78 /* allocate maximum number of elements needed */
79 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
86 for (i = 0; i < msg->num_elements; i++) {
87 const struct ldb_message_element *el = &msg->elements[i];
89 mods[n] = talloc(mods, struct ldap_mod);
95 mods[n]->attrib = *el;
97 switch (el->flags & LDB_FLAG_MOD_MASK) {
98 case LDB_FLAG_MOD_ADD:
99 mods[n]->type = LDAP_MODIFY_ADD;
101 case LDB_FLAG_MOD_DELETE:
102 mods[n]->type = LDAP_MODIFY_DELETE;
104 case LDB_FLAG_MOD_REPLACE:
105 mods[n]->type = LDAP_MODIFY_REPLACE;
122 map an ildap NTSTATUS to a ldb error code
124 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
126 if (NT_STATUS_IS_OK(status)) {
129 talloc_free(ildb->ldb->err_string);
130 ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status));
131 if (NT_STATUS_IS_LDAP(status)) {
132 return NT_STATUS_LDAP_CODE(status);
134 return LDB_ERR_OPERATIONS_ERROR;
137 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
138 struct timeval t, void *private_data)
140 struct ldb_async_handle *handle = talloc_get_type(private_data, struct ldb_async_handle);
141 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
143 if (ac->req->state == LDAP_REQUEST_PENDING) {
144 DLIST_REMOVE(ac->req->conn->pending, ac->req);
147 handle->status = LDB_ERR_OPERATIONS_ERROR;
152 static void ildb_async_callback(struct ldap_request *req)
154 struct ldb_async_handle *handle = talloc_get_type(req->async.private_data, struct ldb_async_handle);
155 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
156 struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private);
160 handle->status = LDB_SUCCESS;
162 if (!NT_STATUS_IS_OK(req->status)) {
163 handle->status = ildb_map_error(ildb, req->status);
167 if (req->num_replies < 1) {
168 handle->status = LDB_ERR_OPERATIONS_ERROR;
174 case LDAP_TAG_ModifyRequest:
175 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
176 handle->status = LDB_ERR_PROTOCOL_ERROR;
179 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
180 handle->status = ildb_map_error(ildb, status);
181 if (ac->callback && handle->status == LDB_SUCCESS) {
182 /* FIXME: build a corresponding ares to pass on */
183 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
185 handle->state = LDB_ASYNC_DONE;
188 case LDAP_TAG_AddRequest:
189 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
190 handle->status = LDB_ERR_PROTOCOL_ERROR;
193 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
194 handle->status = ildb_map_error(ildb, status);
195 if (ac->callback && handle->status == LDB_SUCCESS) {
196 /* FIXME: build a corresponding ares to pass on */
197 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
199 handle->state = LDB_ASYNC_DONE;
202 case LDAP_TAG_DelRequest:
203 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
204 handle->status = LDB_ERR_PROTOCOL_ERROR;
207 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
208 handle->status = ildb_map_error(ildb, status);
209 if (ac->callback && handle->status == LDB_SUCCESS) {
210 /* FIXME: build a corresponding ares to pass on */
211 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
213 handle->state = LDB_ASYNC_DONE;
216 case LDAP_TAG_ModifyDNRequest:
217 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
218 handle->status = LDB_ERR_PROTOCOL_ERROR;
221 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
222 handle->status = ildb_map_error(ildb, status);
223 if (ac->callback && handle->status == LDB_SUCCESS) {
224 /* FIXME: build a corresponding ares to pass on */
225 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
227 handle->state = LDB_ASYNC_DONE;
230 case LDAP_TAG_SearchRequest:
231 /* loop over all messages */
232 for (i = 0; i < req->num_replies; i++) {
233 struct ldap_SearchResEntry *search;
234 struct ldb_async_result *ares = NULL;
235 struct ldap_message *msg;
238 ares = talloc_zero(ac, struct ldb_async_result);
240 handle->status = LDB_ERR_OPERATIONS_ERROR;
244 msg = req->replies[i];
247 case LDAP_TAG_SearchResultDone:
249 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
250 if (!NT_STATUS_IS_OK(status)) {
251 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Error: %s\n" ,ldap_errstr(req->conn, status));
252 handle->status = ildb_map_error(ildb, status);
257 ares->controls = talloc_steal(ares, msg->controls);
259 if (msg->r.SearchResultDone.resultcode) {
260 if (msg->r.SearchResultDone.errormessage) {
261 ldb_set_errstring(ac->module->ldb, talloc_strdup(ac->module, msg->r.SearchResultDone.errormessage));
265 handle->status = msg->r.SearchResultDone.resultcode;
266 handle->state = LDB_ASYNC_DONE;
267 ares->type = LDB_REPLY_DONE;
270 case LDAP_TAG_SearchResultEntry:
273 ares->message = ldb_msg_new(ares);
274 if (!ares->message) {
275 handle->status = LDB_ERR_OPERATIONS_ERROR;;
279 search = &(msg->r.SearchResultEntry);
281 ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn);
282 if (ares->message->dn == NULL) {
283 handle->status = LDB_ERR_OPERATIONS_ERROR;
286 ares->message->num_elements = search->num_attributes;
287 ares->message->elements = talloc_steal(ares->message, search->attributes);
289 handle->status = LDB_SUCCESS;
290 handle->state = LDB_ASYNC_PENDING;
291 ares->type = LDB_REPLY_ENTRY;
294 case LDAP_TAG_SearchResultReference:
296 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
298 handle->status = LDB_SUCCESS;
299 handle->state = LDB_ASYNC_PENDING;
300 ares->type = LDB_REPLY_REFERRAL;
304 /* TAG not handled, fail ! */
305 handle->status = LDB_ERR_PROTOCOL_ERROR;
309 ret = ac->callback(ac->module->ldb, ac->context, ares);
311 handle->status = ret;
315 talloc_free(req->replies);
317 req->num_replies = 0;
322 handle->status = LDB_ERR_PROTOCOL_ERROR;
327 static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
329 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
331 struct ldb_async_handle **handle)
333 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
334 struct ildb_async_context *ildb_ac;
335 struct ldb_async_handle *h;
336 struct ldap_request *req;
338 h = talloc_zero(ildb->ldap, struct ldb_async_handle);
340 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
341 return LDB_ERR_OPERATIONS_ERROR;
346 ildb_ac = talloc(h, struct ildb_async_context);
347 if (ildb_ac == NULL) {
348 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
350 return LDB_ERR_OPERATIONS_ERROR;
353 h->private_data = (void *)ildb_ac;
355 req = ldap_request_send(ildb->ldap, msg);
357 ldb_set_errstring(module->ldb, talloc_asprintf(module, "async send request failed"));
358 return LDB_ERR_OPERATIONS_ERROR;
361 ildb_ac->req = talloc_steal(ildb_ac, req);
362 ildb_ac->module = module;
363 ildb_ac->context = context;
364 ildb_ac->callback = callback;
366 req->async.fn = ildb_async_callback;
367 req->async.private_data = (void *)h;
369 talloc_free(req->time_event);
370 req->time_event = NULL;
372 req->time_event = event_add_timed(req->conn->event.event_ctx, h,
373 timeval_current_ofs(timeout, 0),
374 ildb_request_timeout, ildb_ac);
384 search for matching records using an asynchronous function
386 static int ildb_search_async(struct ldb_module *module, const struct ldb_dn *base,
387 enum ldb_scope scope, struct ldb_parse_tree *tree,
388 const char * const *attrs,
389 struct ldb_control **control_req,
391 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
393 struct ldb_async_handle **handle)
395 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
396 struct ldap_message *msg;
401 if (!callback || !context) {
402 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
403 return LDB_ERR_OPERATIONS_ERROR;
407 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Invalid expression parse tree"));
408 return LDB_ERR_OPERATIONS_ERROR;
411 msg = new_ldap_message(ildb);
413 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
414 return LDB_ERR_OPERATIONS_ERROR;
417 msg->type = LDAP_TAG_SearchRequest;
420 if (ildb->rootDSE != NULL) {
421 msg->r.SearchRequest.basedn =
422 talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", ""));
424 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
427 msg->r.SearchRequest.basedn = ldb_dn_linearize(msg, base);
429 if (msg->r.SearchRequest.basedn == NULL) {
430 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Unable to determine baseDN"));
432 return LDB_ERR_OPERATIONS_ERROR;
435 if (scope == LDB_SCOPE_DEFAULT) {
436 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
438 msg->r.SearchRequest.scope = scope;
441 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
442 msg->r.SearchRequest.timelimit = 0;
443 msg->r.SearchRequest.sizelimit = 0;
444 msg->r.SearchRequest.attributesonly = 0;
445 msg->r.SearchRequest.tree = tree;
447 for (n = 0; attrs && attrs[n]; n++) /* noop */ ;
448 msg->r.SearchRequest.num_attributes = n;
449 msg->r.SearchRequest.attributes = discard_const(attrs);
450 msg->controls = control_req;
452 return ildb_request_send(module, msg, context, callback, timeout, handle);
455 static int ildb_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
457 struct ldb_result *res;
461 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback"));
462 return LDB_ERR_OPERATIONS_ERROR;
465 res = *((struct ldb_result **)context);
471 if (ares->type == LDB_REPLY_ENTRY) {
472 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
477 res->msgs[res->count + 1] = NULL;
479 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
480 if (! res->msgs[res->count]) {
487 if (ares->type == LDB_REPLY_REFERRAL) {
489 for (n = 0; res->refs[n]; n++) /*noop*/ ;
494 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
499 res->refs[n] = talloc_steal(res->refs, ares->referral);
500 res->refs[n + 1] = NULL;
503 if (ares->controls) {
504 res->controls = talloc_steal(res, ares->controls);
505 if (! res->controls) {
516 *((struct ldb_result **)context) = NULL;
517 return LDB_ERR_OPERATIONS_ERROR;
521 search for matching records using a synchronous function
523 static int ildb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
524 enum ldb_scope scope, struct ldb_parse_tree *tree,
525 const char * const *attrs,
526 struct ldb_control **control_req,
527 struct ldb_result **res)
529 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
530 struct ldb_async_handle *handle;
533 *res = talloc_zero(ildb, struct ldb_result);
535 return LDB_ERR_OPERATIONS_ERROR;
538 ret = ildb_search_async(module, base, scope, tree, attrs, control_req,
539 res, &ildb_search_sync_callback, ildb->ldap->timeout, &handle);
541 if (ret == LDB_SUCCESS) {
542 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
546 if (ret != LDB_SUCCESS) {
556 static int ildb_add_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
558 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
560 struct ldb_async_handle **handle)
562 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
563 struct ldap_message *msg;
564 struct ldap_mod **mods;
569 /* ignore ltdb specials */
570 if (ldb_dn_is_special(ldb_msg->dn)) {
574 msg = new_ldap_message(ildb->ldap);
576 return LDB_ERR_OPERATIONS_ERROR;
579 msg->type = LDAP_TAG_AddRequest;
581 msg->r.AddRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
582 if (msg->r.AddRequest.dn == NULL) {
584 return LDB_ERR_INVALID_DN_SYNTAX;
587 mods = ildb_msg_to_mods(msg, &n, ldb_msg, 0);
590 return LDB_ERR_OPERATIONS_ERROR;
593 msg->r.AddRequest.num_attributes = n;
594 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
595 if (msg->r.AddRequest.attributes == NULL) {
597 return LDB_ERR_OPERATIONS_ERROR;
600 for (i = 0; i < n; i++) {
601 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
604 return ildb_request_send(module, msg, context, callback, timeout, handle);
607 static int ildb_add(struct ldb_module *module, const struct ldb_message *msg)
609 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
610 struct ldb_async_handle *handle;
613 ret = ildb_add_async(module, msg,
614 NULL, NULL, ildb->ldap->timeout, &handle);
616 if (ret != LDB_SUCCESS)
619 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
628 static int ildb_modify_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
630 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
632 struct ldb_async_handle **handle)
634 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
635 struct ldap_message *msg;
636 struct ldap_mod **mods;
641 /* ignore ltdb specials */
642 if (ldb_dn_is_special(ldb_msg->dn)) {
646 msg = new_ldap_message(ildb->ldap);
648 return LDB_ERR_OPERATIONS_ERROR;
651 msg->type = LDAP_TAG_ModifyRequest;
653 msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
654 if (msg->r.ModifyRequest.dn == NULL) {
656 return LDB_ERR_INVALID_DN_SYNTAX;
659 mods = ildb_msg_to_mods(msg, &n, ldb_msg, 1);
662 return LDB_ERR_OPERATIONS_ERROR;
665 msg->r.ModifyRequest.num_mods = n;
666 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
667 if (msg->r.ModifyRequest.mods == NULL) {
669 return LDB_ERR_OPERATIONS_ERROR;
672 for (i = 0; i < n; i++) {
673 msg->r.ModifyRequest.mods[i] = *mods[i];
676 return ildb_request_send(module, msg, context, callback, timeout, handle);
679 static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg)
681 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
682 struct ldb_async_handle *handle;
685 ret = ildb_modify_async(module, msg,
686 NULL, NULL, ildb->ldap->timeout, &handle);
688 if (ret != LDB_SUCCESS)
691 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
700 static int ildb_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
702 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
704 struct ldb_async_handle **handle)
706 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
707 struct ldap_message *msg;
711 /* ignore ltdb specials */
712 if (ldb_dn_is_special(dn)) {
716 msg = new_ldap_message(ildb->ldap);
718 return LDB_ERR_OPERATIONS_ERROR;
721 msg->type = LDAP_TAG_DelRequest;
723 msg->r.DelRequest.dn = ldb_dn_linearize(msg, dn);
724 if (msg->r.DelRequest.dn == NULL) {
726 return LDB_ERR_INVALID_DN_SYNTAX;
729 return ildb_request_send(module, msg, context, callback, timeout, handle);
732 static int ildb_delete(struct ldb_module *module, const struct ldb_dn *dn)
734 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
735 struct ldb_async_handle *handle;
738 ret = ildb_delete_async(module, dn,
739 NULL, NULL, ildb->ldap->timeout, &handle);
741 if (ret != LDB_SUCCESS)
744 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
753 static int ildb_rename_async(struct ldb_module *module,
754 const struct ldb_dn *olddn, const struct ldb_dn *newdn,
756 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
758 struct ldb_async_handle **handle)
760 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
761 struct ldap_message *msg;
765 /* ignore ltdb specials */
766 if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
770 msg = new_ldap_message(ildb->ldap);
772 return LDB_ERR_OPERATIONS_ERROR;
775 msg->type = LDAP_TAG_ModifyDNRequest;
776 msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, olddn);
777 if (msg->r.ModifyDNRequest.dn == NULL) {
779 return LDB_ERR_INVALID_DN_SYNTAX;
782 msg->r.ModifyDNRequest.newrdn =
783 talloc_asprintf(msg, "%s=%s",
784 newdn->components[0].name,
785 ldb_dn_escape_value(msg, newdn->components[0].value));
786 if (msg->r.ModifyDNRequest.newrdn == NULL) {
788 return LDB_ERR_OPERATIONS_ERROR;
791 msg->r.ModifyDNRequest.newsuperior =
792 ldb_dn_linearize(msg,
793 ldb_dn_get_parent(msg, newdn));
794 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
796 return LDB_ERR_INVALID_DN_SYNTAX;
799 msg->r.ModifyDNRequest.deleteolddn = True;
801 return ildb_request_send(module, msg, context, callback, timeout, handle);
804 static int ildb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
806 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
807 struct ldb_async_handle *handle;
810 ret = ildb_rename_async(module, olddn, newdn,
811 NULL, NULL, ildb->ldap->timeout, &handle);
813 if (ret != LDB_SUCCESS)
816 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
822 static int ildb_start_trans(struct ldb_module *module)
824 /* TODO implement a local locking mechanism here */
829 static int ildb_end_trans(struct ldb_module *module)
831 /* TODO implement a local transaction mechanism here */
836 static int ildb_del_trans(struct ldb_module *module)
838 /* TODO implement a local locking mechanism here */
843 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
845 switch (req->operation) {
848 return ildb_search_bytree(module,
850 req->op.search.scope,
852 req->op.search.attrs,
854 &req->op.search.res);
857 return ildb_add(module, req->op.add.message);
860 return ildb_modify(module, req->op.mod.message);
863 return ildb_delete(module, req->op.del.dn);
866 return ildb_rename(module,
867 req->op.rename.olddn,
868 req->op.rename.newdn);
870 case LDB_ASYNC_SEARCH:
871 return ildb_search_async(module,
873 req->op.search.scope,
875 req->op.search.attrs,
883 return ildb_add_async(module,
890 case LDB_ASYNC_MODIFY:
891 return ildb_modify_async(module,
898 case LDB_ASYNC_DELETE:
899 return ildb_delete_async(module,
906 case LDB_ASYNC_RENAME:
907 return ildb_rename_async(module,
908 req->op.rename.olddn,
909 req->op.rename.newdn,
921 static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
923 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
926 return LDB_ERR_OPERATIONS_ERROR;
929 handle->state = LDB_ASYNC_INIT;
933 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
934 return LDB_ERR_OTHER;
938 while (handle->status == LDB_SUCCESS && handle->state == LDB_ASYNC_INIT) {
939 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
940 return LDB_ERR_OTHER;
945 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
946 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
947 return LDB_ERR_OTHER;
952 return LDB_ERR_OPERATIONS_ERROR;
955 return handle->status;
959 fetch the rootDSE for later use
961 static int ildb_init(struct ldb_module *module)
963 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
964 struct ldb_result *res = NULL;
965 struct ldb_dn *empty_dn = ldb_dn_new(ildb);
967 ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE,
968 ldb_parse_tree(empty_dn, "dn=dc=rootDSE"),
970 if (ret == LDB_SUCCESS && res->count == 1) {
971 ildb->rootDSE = talloc_steal(ildb, res->msgs[0]);
973 if (ret == LDB_SUCCESS) talloc_free(res);
974 talloc_free(empty_dn);
979 static const struct ldb_module_ops ildb_ops = {
981 .request = ildb_request,
982 .start_transaction = ildb_start_trans,
983 .end_transaction = ildb_end_trans,
984 .del_transaction = ildb_del_trans,
985 .async_wait = ildb_async_wait,
986 .init_context = ildb_init
990 connect to the database
992 static int ildb_connect(struct ldb_context *ldb, const char *url,
993 unsigned int flags, const char *options[])
995 struct ildb_private *ildb = NULL;
997 struct cli_credentials *creds;
999 ildb = talloc(ldb, struct ildb_private);
1005 ildb->rootDSE = NULL;
1008 ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
1014 status = ldap_connect(ildb->ldap, url);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
1017 url, ldap_errstr(ildb->ldap, status));
1021 ldb->modules = talloc(ldb, struct ldb_module);
1022 if (!ldb->modules) {
1026 ldb->modules->ldb = ldb;
1027 ldb->modules->prev = ldb->modules->next = NULL;
1028 ldb->modules->private_data = ildb;
1029 ldb->modules->ops = &ildb_ops;
1031 /* caller can optionally setup credentials using the opaque token 'credentials' */
1032 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
1033 if (creds == NULL) {
1034 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
1036 creds = session_info->credentials;
1040 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
1041 const char *bind_dn = cli_credentials_get_bind_dn(creds);
1043 const char *password = cli_credentials_get_password(creds);
1044 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
1045 if (!NT_STATUS_IS_OK(status)) {
1046 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1047 ldap_errstr(ildb->ldap, status));
1051 status = ldap_bind_sasl(ildb->ldap, creds);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1054 ldap_errstr(ildb->ldap, status));
1064 ldb->modules->private_data = NULL;
1070 int ldb_ildap_init(void)
1072 return ldb_register_backend("ldap", ildb_connect);