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;
344 ildb_ac = talloc(h, struct ildb_async_context);
345 if (ildb_ac == NULL) {
346 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
348 return LDB_ERR_OPERATIONS_ERROR;
351 h->private_data = (void *)ildb_ac;
353 req = ldap_request_send(ildb->ldap, msg);
355 ldb_set_errstring(module->ldb, talloc_asprintf(module, "async send request failed"));
356 return LDB_ERR_OPERATIONS_ERROR;
359 ildb_ac->req = talloc_steal(ildb_ac, req);
360 ildb_ac->module = module;
361 ildb_ac->context = context;
362 ildb_ac->callback = callback;
364 req->async.fn = ildb_async_callback;
365 req->async.private_data = (void *)h;
367 talloc_free(req->time_event);
368 req->time_event = NULL;
370 req->time_event = event_add_timed(req->conn->event.event_ctx, h,
371 timeval_current_ofs(timeout, 0),
372 ildb_request_timeout, ildb_ac);
382 search for matching records using an asynchronous function
384 static int ildb_search_async(struct ldb_module *module, const struct ldb_dn *base,
385 enum ldb_scope scope, struct ldb_parse_tree *tree,
386 const char * const *attrs,
387 struct ldb_control **control_req,
389 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
391 struct ldb_async_handle **handle)
393 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
394 struct ldap_message *msg;
399 if (!callback || !context) {
400 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
401 return LDB_ERR_OPERATIONS_ERROR;
405 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Invalid expression parse tree"));
406 return LDB_ERR_OPERATIONS_ERROR;
409 msg = new_ldap_message(ildb);
411 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
412 return LDB_ERR_OPERATIONS_ERROR;
415 msg->type = LDAP_TAG_SearchRequest;
418 if (ildb->rootDSE != NULL) {
419 msg->r.SearchRequest.basedn =
420 talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", ""));
422 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
425 msg->r.SearchRequest.basedn = ldb_dn_linearize(msg, base);
427 if (msg->r.SearchRequest.basedn == NULL) {
428 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Unable to determine baseDN"));
430 return LDB_ERR_OPERATIONS_ERROR;
433 if (scope == LDB_SCOPE_DEFAULT) {
434 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
436 msg->r.SearchRequest.scope = scope;
439 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
440 msg->r.SearchRequest.timelimit = 0;
441 msg->r.SearchRequest.sizelimit = 0;
442 msg->r.SearchRequest.attributesonly = 0;
443 msg->r.SearchRequest.tree = tree;
445 for (n = 0; attrs && attrs[n]; n++) /* noop */ ;
446 msg->r.SearchRequest.num_attributes = n;
447 msg->r.SearchRequest.attributes = discard_const(attrs);
448 msg->controls = control_req;
450 return ildb_request_send(module, msg, context, callback, timeout, handle);
453 static int ildb_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
455 struct ldb_result *res;
459 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback"));
460 return LDB_ERR_OPERATIONS_ERROR;
463 res = *((struct ldb_result **)context);
469 if (ares->type == LDB_REPLY_ENTRY) {
470 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
475 res->msgs[res->count + 1] = NULL;
477 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
478 if (! res->msgs[res->count]) {
485 if (ares->type == LDB_REPLY_REFERRAL) {
487 for (n = 0; res->refs[n]; n++) /*noop*/ ;
492 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
497 res->refs[n] = talloc_steal(res->refs, ares->referral);
498 res->refs[n + 1] = NULL;
501 if (ares->controls) {
502 res->controls = talloc_steal(res, ares->controls);
503 if (! res->controls) {
514 *((struct ldb_result **)context) = NULL;
515 return LDB_ERR_OPERATIONS_ERROR;
519 search for matching records using a synchronous function
521 static int ildb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
522 enum ldb_scope scope, struct ldb_parse_tree *tree,
523 const char * const *attrs,
524 struct ldb_control **control_req,
525 struct ldb_result **res)
527 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
528 struct ldb_async_handle *handle;
531 *res = talloc_zero(ildb, struct ldb_result);
533 return LDB_ERR_OPERATIONS_ERROR;
536 ret = ildb_search_async(module, base, scope, tree, attrs, control_req,
537 res, &ildb_search_sync_callback, ildb->ldap->timeout, &handle);
539 if (ret != LDB_SUCCESS)
542 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
551 static int ildb_add_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
553 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
555 struct ldb_async_handle **handle)
557 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
558 struct ldap_message *msg;
559 struct ldap_mod **mods;
564 /* ignore ltdb specials */
565 if (ldb_dn_is_special(ldb_msg->dn)) {
569 msg = new_ldap_message(ildb->ldap);
571 return LDB_ERR_OPERATIONS_ERROR;
574 msg->type = LDAP_TAG_AddRequest;
576 msg->r.AddRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
577 if (msg->r.AddRequest.dn == NULL) {
579 return LDB_ERR_INVALID_DN_SYNTAX;
582 mods = ildb_msg_to_mods(msg, &n, ldb_msg, 0);
585 return LDB_ERR_OPERATIONS_ERROR;
588 msg->r.AddRequest.num_attributes = n;
589 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
590 if (msg->r.AddRequest.attributes == NULL) {
592 return LDB_ERR_OPERATIONS_ERROR;
595 for (i = 0; i < n; i++) {
596 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
599 return ildb_request_send(module, msg, context, callback, timeout, handle);
602 static int ildb_add(struct ldb_module *module, const struct ldb_message *msg)
604 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
605 struct ldb_async_handle *handle;
608 ret = ildb_add_async(module, msg,
609 NULL, NULL, ildb->ldap->timeout, &handle);
611 if (ret != LDB_SUCCESS)
614 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
623 static int ildb_modify_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
625 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
627 struct ldb_async_handle **handle)
629 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
630 struct ldap_message *msg;
631 struct ldap_mod **mods;
636 /* ignore ltdb specials */
637 if (ldb_dn_is_special(ldb_msg->dn)) {
641 msg = new_ldap_message(ildb->ldap);
643 return LDB_ERR_OPERATIONS_ERROR;
646 msg->type = LDAP_TAG_ModifyRequest;
648 msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
649 if (msg->r.ModifyRequest.dn == NULL) {
651 return LDB_ERR_INVALID_DN_SYNTAX;
654 mods = ildb_msg_to_mods(msg, &n, ldb_msg, 1);
657 return LDB_ERR_OPERATIONS_ERROR;
660 msg->r.ModifyRequest.num_mods = n;
661 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
662 if (msg->r.ModifyRequest.mods == NULL) {
664 return LDB_ERR_OPERATIONS_ERROR;
667 for (i = 0; i < n; i++) {
668 msg->r.ModifyRequest.mods[i] = *mods[i];
671 return ildb_request_send(module, msg, context, callback, timeout, handle);
674 static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg)
676 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
677 struct ldb_async_handle *handle;
680 ret = ildb_modify_async(module, msg,
681 NULL, NULL, ildb->ldap->timeout, &handle);
683 if (ret != LDB_SUCCESS)
686 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
695 static int ildb_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
697 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
699 struct ldb_async_handle **handle)
701 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
702 struct ldap_message *msg;
706 /* ignore ltdb specials */
707 if (ldb_dn_is_special(dn)) {
711 msg = new_ldap_message(ildb->ldap);
713 return LDB_ERR_OPERATIONS_ERROR;
716 msg->type = LDAP_TAG_DelRequest;
718 msg->r.DelRequest.dn = ldb_dn_linearize(msg, dn);
719 if (msg->r.DelRequest.dn == NULL) {
721 return LDB_ERR_INVALID_DN_SYNTAX;
724 return ildb_request_send(module, msg, context, callback, timeout, handle);
727 static int ildb_delete(struct ldb_module *module, const struct ldb_dn *dn)
729 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
730 struct ldb_async_handle *handle;
733 ret = ildb_delete_async(module, dn,
734 NULL, NULL, ildb->ldap->timeout, &handle);
736 if (ret != LDB_SUCCESS)
739 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
748 static int ildb_rename_async(struct ldb_module *module,
749 const struct ldb_dn *olddn, const struct ldb_dn *newdn,
751 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
753 struct ldb_async_handle **handle)
755 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
756 struct ldap_message *msg;
760 /* ignore ltdb specials */
761 if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
765 msg = new_ldap_message(ildb->ldap);
767 return LDB_ERR_OPERATIONS_ERROR;
770 msg->type = LDAP_TAG_ModifyDNRequest;
771 msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, olddn);
772 if (msg->r.ModifyDNRequest.dn == NULL) {
774 return LDB_ERR_INVALID_DN_SYNTAX;
777 msg->r.ModifyDNRequest.newrdn =
778 talloc_asprintf(msg, "%s=%s",
779 newdn->components[0].name,
780 ldb_dn_escape_value(msg, newdn->components[0].value));
781 if (msg->r.ModifyDNRequest.newrdn == NULL) {
783 return LDB_ERR_OPERATIONS_ERROR;
786 msg->r.ModifyDNRequest.newsuperior =
787 ldb_dn_linearize(msg,
788 ldb_dn_get_parent(msg, newdn));
789 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
791 return LDB_ERR_INVALID_DN_SYNTAX;
794 msg->r.ModifyDNRequest.deleteolddn = True;
796 return ildb_request_send(module, msg, context, callback, timeout, handle);
799 static int ildb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
801 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
802 struct ldb_async_handle *handle;
805 ret = ildb_rename_async(module, olddn, newdn,
806 NULL, NULL, ildb->ldap->timeout, &handle);
808 if (ret != LDB_SUCCESS)
811 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
817 static int ildb_start_trans(struct ldb_module *module)
819 /* TODO implement a local locking mechanism here */
824 static int ildb_end_trans(struct ldb_module *module)
826 /* TODO implement a local transaction mechanism here */
831 static int ildb_del_trans(struct ldb_module *module)
833 /* TODO implement a local locking mechanism here */
838 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
840 switch (req->operation) {
843 return ildb_search_bytree(module,
845 req->op.search.scope,
847 req->op.search.attrs,
849 &req->op.search.res);
852 return ildb_add(module, req->op.add.message);
855 return ildb_modify(module, req->op.mod.message);
858 return ildb_delete(module, req->op.del.dn);
861 return ildb_rename(module,
862 req->op.rename.olddn,
863 req->op.rename.newdn);
865 case LDB_ASYNC_SEARCH:
866 return ildb_search_async(module,
868 req->op.search.scope,
870 req->op.search.attrs,
878 return ildb_add_async(module,
885 case LDB_ASYNC_MODIFY:
886 return ildb_modify_async(module,
893 case LDB_ASYNC_DELETE:
894 return ildb_delete_async(module,
901 case LDB_ASYNC_RENAME:
902 return ildb_rename_async(module,
903 req->op.rename.olddn,
904 req->op.rename.newdn,
916 static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
918 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
921 return LDB_ERR_OPERATIONS_ERROR;
924 handle->state = LDB_ASYNC_INIT;
928 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
929 return LDB_ERR_OTHER;
933 while (handle->status == LDB_SUCCESS && handle->state == LDB_ASYNC_INIT) {
934 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
935 return LDB_ERR_OTHER;
940 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
941 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
942 return LDB_ERR_OTHER;
947 return LDB_ERR_OPERATIONS_ERROR;
950 return handle->status;
954 fetch the rootDSE for later use
956 static int ildb_init(struct ldb_module *module)
958 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
959 struct ldb_result *res = NULL;
960 struct ldb_dn *empty_dn = ldb_dn_new(ildb);
962 ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE,
963 ldb_parse_tree(empty_dn, "dn=dc=rootDSE"),
965 if (ret == LDB_SUCCESS && res->count == 1) {
966 ildb->rootDSE = talloc_steal(ildb, res->msgs[0]);
968 if (ret == LDB_SUCCESS) talloc_free(res);
969 talloc_free(empty_dn);
974 static const struct ldb_module_ops ildb_ops = {
976 .request = ildb_request,
977 .start_transaction = ildb_start_trans,
978 .end_transaction = ildb_end_trans,
979 .del_transaction = ildb_del_trans,
980 .init_context = ildb_init
984 connect to the database
986 int ildb_connect(struct ldb_context *ldb, const char *url,
987 unsigned int flags, const char *options[])
989 struct ildb_private *ildb = NULL;
991 struct cli_credentials *creds;
993 ildb = talloc(ldb, struct ildb_private);
999 ildb->rootDSE = NULL;
1002 ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
1008 status = ldap_connect(ildb->ldap, url);
1009 if (!NT_STATUS_IS_OK(status)) {
1010 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
1011 url, ldap_errstr(ildb->ldap, status));
1015 ldb->modules = talloc(ldb, struct ldb_module);
1016 if (!ldb->modules) {
1020 ldb->modules->ldb = ldb;
1021 ldb->modules->prev = ldb->modules->next = NULL;
1022 ldb->modules->private_data = ildb;
1023 ldb->modules->ops = &ildb_ops;
1025 /* caller can optionally setup credentials using the opaque token 'credentials' */
1026 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
1027 if (creds == NULL) {
1028 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
1030 creds = session_info->credentials;
1034 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
1035 const char *bind_dn = cli_credentials_get_bind_dn(creds);
1037 const char *password = cli_credentials_get_password(creds);
1038 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
1039 if (!NT_STATUS_IS_OK(status)) {
1040 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1041 ldap_errstr(ildb->ldap, status));
1045 status = ldap_bind_sasl(ildb->ldap, creds);
1046 if (!NT_STATUS_IS_OK(status)) {
1047 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1048 ldap_errstr(ildb->ldap, status));
1054 ldb->async_wait = &ildb_async_wait;
1060 ldb->modules->private_data = NULL;