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
27 This is a ldb backend for the internal ldap client library in
28 Samba4. By using this backend we are independent of a system ldap
34 #include "ldb/include/includes.h"
36 #include "lib/events/events.h"
37 #include "libcli/ldap/ldap.h"
38 #include "libcli/ldap/ldap_client.h"
39 #include "lib/cmdline/popt_common.h"
40 #include "auth/auth.h"
43 struct ldap_connection *ldap;
44 struct ldb_message *rootDSE;
45 struct ldb_context *ldb;
48 struct ildb_async_context {
49 struct ldb_module *module;
50 struct ldap_request *req;
52 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
56 convert a ldb_message structure to a list of ldap_mod structures
57 ready for ildap_add() or ildap_modify()
59 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
60 const struct ldb_message *msg, int use_flags)
62 struct ldap_mod **mods;
66 /* allocate maximum number of elements needed */
67 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
74 for (i = 0; i < msg->num_elements; i++) {
75 const struct ldb_message_element *el = &msg->elements[i];
77 mods[n] = talloc(mods, struct ldap_mod);
83 mods[n]->attrib = *el;
85 switch (el->flags & LDB_FLAG_MOD_MASK) {
86 case LDB_FLAG_MOD_ADD:
87 mods[n]->type = LDAP_MODIFY_ADD;
89 case LDB_FLAG_MOD_DELETE:
90 mods[n]->type = LDAP_MODIFY_DELETE;
92 case LDB_FLAG_MOD_REPLACE:
93 mods[n]->type = LDAP_MODIFY_REPLACE;
110 map an ildap NTSTATUS to a ldb error code
112 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
114 if (NT_STATUS_IS_OK(status)) {
117 talloc_free(ildb->ldb->err_string);
118 ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status));
119 if (NT_STATUS_IS_LDAP(status)) {
120 return NT_STATUS_LDAP_CODE(status);
122 return LDB_ERR_OPERATIONS_ERROR;
125 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
126 struct timeval t, void *private_data)
128 struct ldb_async_handle *handle = talloc_get_type(private_data, struct ldb_async_handle);
129 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
131 if (ac->req->state == LDAP_REQUEST_PENDING) {
132 DLIST_REMOVE(ac->req->conn->pending, ac->req);
135 handle->status = LDB_ERR_OPERATIONS_ERROR;
140 static void ildb_async_callback(struct ldap_request *req)
142 struct ldb_async_handle *handle = talloc_get_type(req->async.private_data, struct ldb_async_handle);
143 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
144 struct ildb_private *ildb = ac->module->private_data;
148 handle->status = LDB_SUCCESS;
150 if (!NT_STATUS_IS_OK(req->status)) {
151 handle->status = ildb_map_error(ildb, req->status);
155 if (req->num_replies < 1) {
156 handle->status = LDB_ERR_OPERATIONS_ERROR;
162 case LDAP_TAG_ModifyRequest:
163 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
164 handle->status = LDB_ERR_PROTOCOL_ERROR;
167 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
168 handle->status = ildb_map_error(ildb, status);
169 handle->state = LDB_ASYNC_DONE;
172 case LDAP_TAG_AddRequest:
173 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
174 handle->status = LDB_ERR_PROTOCOL_ERROR;
177 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
178 handle->status = ildb_map_error(ildb, status);
179 handle->state = LDB_ASYNC_DONE;
182 case LDAP_TAG_DelRequest:
183 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
184 handle->status = LDB_ERR_PROTOCOL_ERROR;
187 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
188 handle->status = ildb_map_error(ildb, status);
189 handle->state = LDB_ASYNC_DONE;
192 case LDAP_TAG_ModifyDNRequest:
193 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
194 handle->status = LDB_ERR_PROTOCOL_ERROR;
197 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
198 handle->status = ildb_map_error(ildb, status);
199 handle->state = LDB_ASYNC_DONE;
202 case LDAP_TAG_SearchRequest:
203 /* loop over all messages */
204 for (i = 0; i < req->num_replies; i++) {
205 struct ldap_SearchResEntry *search;
206 struct ldb_async_result *ares = NULL;
207 struct ldap_message *msg;
210 ares = talloc_zero(ac, struct ldb_async_result);
212 handle->status = LDB_ERR_OPERATIONS_ERROR;
216 msg = req->replies[i];
219 case LDAP_TAG_SearchResultDone:
221 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
222 if (!NT_STATUS_IS_OK(status)) {
223 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Error: %s\n" ,ldap_errstr(req->conn, status));
224 handle->status = ildb_map_error(ildb, status);
229 ares->controls = talloc_steal(ares, msg->controls);
231 if (msg->r.SearchResultDone.resultcode) {
232 if (msg->r.SearchResultDone.errormessage) {
233 ldb_set_errstring(ac->module, talloc_strdup(ac->module, msg->r.SearchResultDone.errormessage));
237 handle->status = msg->r.SearchResultDone.resultcode;
238 handle->state = LDB_ASYNC_DONE;
239 ares->type = LDB_REPLY_DONE;
242 case LDAP_TAG_SearchResultEntry:
245 ares->message = ldb_msg_new(ares);
246 if (!ares->message) {
247 handle->status = LDB_ERR_OPERATIONS_ERROR;;
251 search = &(msg->r.SearchResultEntry);
253 ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn);
254 if (ares->message->dn == NULL) {
255 handle->status = LDB_ERR_OPERATIONS_ERROR;
258 ares->message->num_elements = search->num_attributes;
259 ares->message->elements = talloc_steal(ares->message, search->attributes);
261 handle->status = LDB_SUCCESS;
262 handle->state = LDB_ASYNC_PENDING;
263 ares->type = LDB_REPLY_ENTRY;
266 case LDAP_TAG_SearchResultReference:
268 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
270 handle->status = LDB_SUCCESS;
271 handle->state = LDB_ASYNC_PENDING;
272 ares->type = LDB_REPLY_REFERRAL;
276 /* TAG not handled, fail ! */
277 handle->status = LDB_ERR_PROTOCOL_ERROR;
281 ret = ac->callback(ac->module->ldb, ac->context, ares);
283 handle->status = ret;
287 talloc_free(req->replies);
289 req->num_replies = 0;
294 handle->status = LDB_ERR_PROTOCOL_ERROR;
299 static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
301 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
303 struct ldb_async_handle **handle)
305 struct ildb_private *ildb = module->private_data;
306 struct ildb_async_context *ildb_ac;
307 struct ldb_async_handle *h;
308 struct ldap_request *req;
310 h = talloc_zero(ildb->ldap, struct ldb_async_handle);
312 ldb_set_errstring(module, talloc_asprintf(module, "Out of Memory"));
313 return LDB_ERR_OPERATIONS_ERROR;
316 ildb_ac = talloc(h, struct ildb_async_context);
317 if (ildb_ac == NULL) {
318 ldb_set_errstring(module, talloc_asprintf(module, "Out of Memory"));
320 return LDB_ERR_OPERATIONS_ERROR;
323 h->private_data = (void *)ildb_ac;
325 req = ldap_request_send(ildb->ldap, msg);
327 ldb_set_errstring(module, talloc_asprintf(module, "async send request failed"));
328 return LDB_ERR_OPERATIONS_ERROR;
331 ildb_ac->req = talloc_steal(ildb_ac, req);
332 ildb_ac->module = module;
333 ildb_ac->context = context;
334 ildb_ac->callback = callback;
336 req->async.fn = ildb_async_callback;
337 req->async.private_data = (void *)h;
339 talloc_free(req->time_event);
340 req->time_event = NULL;
342 req->time_event = event_add_timed(req->conn->event.event_ctx, h,
343 timeval_current_ofs(timeout, 0),
344 ildb_request_timeout, ildb_ac);
354 search for matching records using an asynchronous function
356 static int ildb_search_async(struct ldb_module *module, const struct ldb_dn *base,
357 enum ldb_scope scope, struct ldb_parse_tree *tree,
358 const char * const *attrs,
359 struct ldb_control **control_req,
361 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
363 struct ldb_async_handle **handle)
365 struct ildb_private *ildb = module->private_data;
366 struct ldap_message *msg;
371 if (!callback || !context) {
372 ldb_set_errstring(module, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
373 return LDB_ERR_OPERATIONS_ERROR;
377 ldb_set_errstring(module, talloc_asprintf(module, "Invalid expression parse tree"));
378 return LDB_ERR_OPERATIONS_ERROR;
381 msg = new_ldap_message(ildb);
383 ldb_set_errstring(module, talloc_asprintf(module, "Out of Memory"));
384 return LDB_ERR_OPERATIONS_ERROR;
387 msg->type = LDAP_TAG_SearchRequest;
390 if (ildb->rootDSE != NULL) {
391 msg->r.SearchRequest.basedn =
392 talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", ""));
394 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
397 msg->r.SearchRequest.basedn = ldb_dn_linearize(msg, base);
399 if (msg->r.SearchRequest.basedn == NULL) {
400 ldb_set_errstring(module, talloc_asprintf(module, "Unable to determine baseDN"));
402 return LDB_ERR_OPERATIONS_ERROR;
405 if (scope == LDB_SCOPE_DEFAULT) {
406 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
408 msg->r.SearchRequest.scope = scope;
411 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
412 msg->r.SearchRequest.timelimit = 0;
413 msg->r.SearchRequest.sizelimit = 0;
414 msg->r.SearchRequest.attributesonly = 0;
415 msg->r.SearchRequest.tree = tree;
417 for (n = 0; attrs && attrs[n]; n++) /* noop */ ;
418 msg->r.SearchRequest.num_attributes = n;
419 msg->r.SearchRequest.attributes = discard_const(attrs);
420 msg->controls = control_req;
422 return ildb_request_send(module, msg, context, callback, timeout, handle);
425 struct ildb_sync_context {
426 struct ldb_result *res;
431 static int ildb_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
433 struct ldb_result *res;
437 ldb_set_errstring(ldb->modules, talloc_asprintf(ldb, "NULL Context in callback"));
438 return LDB_ERR_OPERATIONS_ERROR;
441 res = *((struct ldb_result **)context);
447 if (ares->type == LDB_REPLY_ENTRY) {
448 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
453 res->msgs[res->count + 1] = NULL;
455 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
456 if (! res->msgs[res->count]) {
463 if (ares->type == LDB_REPLY_REFERRAL) {
465 for (n = 0; res->refs[n]; n++) /*noop*/ ;
470 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
475 res->refs[n] = talloc_steal(res->refs, ares->referral);
476 res->refs[n + 1] = NULL;
479 if (ares->type == LDB_REPLY_DONE) {
480 if (ares->controls) {
481 res->controls = talloc_steal(res, ares->controls);
482 if (! res->controls) {
494 *((struct ldb_result **)context) = NULL;
495 return LDB_ERR_OPERATIONS_ERROR;
499 search for matching records using a synchronous function
501 static int ildb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
502 enum ldb_scope scope, struct ldb_parse_tree *tree,
503 const char * const *attrs,
504 struct ldb_control **control_req,
505 struct ldb_result **res)
507 struct ildb_private *ildb = module->private_data;
508 struct ldb_async_handle *handle;
511 *res = talloc_zero(ildb, struct ldb_result);
513 return LDB_ERR_OPERATIONS_ERROR;
516 ret = ildb_search_async(module, base, scope, tree, attrs, control_req,
517 res, &ildb_search_sync_callback, ildb->ldap->timeout, &handle);
519 if (ret != LDB_SUCCESS)
522 return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
528 static int ildb_add_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
530 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
532 struct ldb_async_handle **handle)
534 struct ildb_private *ildb = module->private_data;
535 struct ldap_message *msg;
536 struct ldap_mod **mods;
541 /* ignore ltdb specials */
542 if (ldb_dn_is_special(ldb_msg->dn)) {
546 msg = new_ldap_message(ildb->ldap);
548 return LDB_ERR_OPERATIONS_ERROR;
551 msg->type = LDAP_TAG_AddRequest;
553 msg->r.AddRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
554 if (msg->r.AddRequest.dn == NULL) {
556 return LDB_ERR_INVALID_DN_SYNTAX;
559 mods = ildb_msg_to_mods(msg, &n, ldb_msg, 0);
562 return LDB_ERR_OPERATIONS_ERROR;
565 msg->r.AddRequest.num_attributes = n;
566 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
567 if (msg->r.AddRequest.attributes == NULL) {
569 return LDB_ERR_OPERATIONS_ERROR;
572 for (i = 0; i < n; i++) {
573 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
576 return ildb_request_send(module, msg, context, callback, timeout, handle);
579 static int ildb_add(struct ldb_module *module, const struct ldb_message *msg)
581 struct ildb_private *ildb = module->private_data;
582 struct ldb_async_handle *handle;
585 ret = ildb_add_async(module, msg,
586 NULL, NULL, ildb->ldap->timeout, &handle);
588 if (ret != LDB_SUCCESS)
591 return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
597 static int ildb_modify_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
599 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
601 struct ldb_async_handle **handle)
603 struct ildb_private *ildb = module->private_data;
604 struct ldap_message *msg;
605 struct ldap_mod **mods;
610 /* ignore ltdb specials */
611 if (ldb_dn_is_special(ldb_msg->dn)) {
615 msg = new_ldap_message(ildb->ldap);
617 return LDB_ERR_OPERATIONS_ERROR;
620 msg->type = LDAP_TAG_ModifyRequest;
622 msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
623 if (msg->r.ModifyRequest.dn == NULL) {
625 return LDB_ERR_INVALID_DN_SYNTAX;
628 mods = ildb_msg_to_mods(msg, &n, ldb_msg, 1);
631 return LDB_ERR_OPERATIONS_ERROR;
634 msg->r.ModifyRequest.num_mods = n;
635 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
636 if (msg->r.ModifyRequest.mods == NULL) {
638 return LDB_ERR_OPERATIONS_ERROR;
641 for (i = 0; i < n; i++) {
642 msg->r.ModifyRequest.mods[i] = *mods[i];
645 return ildb_request_send(module, msg, context, callback, timeout, handle);
648 static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg)
650 struct ildb_private *ildb = module->private_data;
651 struct ldb_async_handle *handle;
654 ret = ildb_modify_async(module, msg,
655 NULL, NULL, ildb->ldap->timeout, &handle);
657 if (ret != LDB_SUCCESS)
660 return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
666 static int ildb_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
668 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
670 struct ldb_async_handle **handle)
672 struct ildb_private *ildb = module->private_data;
673 struct ldap_message *msg;
677 /* ignore ltdb specials */
678 if (ldb_dn_is_special(dn)) {
682 msg = new_ldap_message(ildb->ldap);
684 return LDB_ERR_OPERATIONS_ERROR;
687 msg->type = LDAP_TAG_DelRequest;
689 msg->r.DelRequest.dn = ldb_dn_linearize(msg, dn);
690 if (msg->r.DelRequest.dn == NULL) {
692 return LDB_ERR_INVALID_DN_SYNTAX;
695 return ildb_request_send(module, msg, context, callback, timeout, handle);
698 static int ildb_delete(struct ldb_module *module, const struct ldb_dn *dn)
700 struct ildb_private *ildb = module->private_data;
701 struct ldb_async_handle *handle;
704 ret = ildb_delete_async(module, dn,
705 NULL, NULL, ildb->ldap->timeout, &handle);
707 if (ret != LDB_SUCCESS)
710 return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
716 static int ildb_rename_async(struct ldb_module *module,
717 const struct ldb_dn *olddn, const struct ldb_dn *newdn,
719 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
721 struct ldb_async_handle **handle)
723 struct ildb_private *ildb = module->private_data;
724 struct ldap_message *msg;
728 /* ignore ltdb specials */
729 if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
733 msg = new_ldap_message(ildb->ldap);
735 return LDB_ERR_OPERATIONS_ERROR;
738 msg->type = LDAP_TAG_ModifyDNRequest;
739 msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, olddn);
740 if (msg->r.ModifyDNRequest.dn == NULL) {
742 return LDB_ERR_INVALID_DN_SYNTAX;
745 msg->r.ModifyDNRequest.newrdn =
746 talloc_asprintf(msg, "%s=%s",
747 newdn->components[0].name,
748 ldb_dn_escape_value(msg, newdn->components[0].value));
749 if (msg->r.ModifyDNRequest.newrdn == NULL) {
751 return LDB_ERR_OPERATIONS_ERROR;
754 msg->r.ModifyDNRequest.newsuperior =
755 ldb_dn_linearize(msg,
756 ldb_dn_get_parent(msg, newdn));
757 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
759 return LDB_ERR_INVALID_DN_SYNTAX;
762 msg->r.ModifyDNRequest.deleteolddn = True;
764 return ildb_request_send(module, msg, context, callback, timeout, handle);
767 static int ildb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
769 struct ildb_private *ildb = module->private_data;
770 struct ldb_async_handle *handle;
773 ret = ildb_rename_async(module, olddn, newdn,
774 NULL, NULL, ildb->ldap->timeout, &handle);
776 if (ret != LDB_SUCCESS)
779 return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
782 static int ildb_start_trans(struct ldb_module *module)
784 /* TODO implement a local locking mechanism here */
789 static int ildb_end_trans(struct ldb_module *module)
791 /* TODO implement a local transaction mechanism here */
796 static int ildb_del_trans(struct ldb_module *module)
798 /* TODO implement a local locking mechanism here */
803 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
805 switch (req->operation) {
808 return ildb_search_bytree(module,
810 req->op.search.scope,
812 req->op.search.attrs,
814 &req->op.search.res);
817 return ildb_add(module, req->op.add.message);
820 return ildb_modify(module, req->op.mod.message);
823 return ildb_delete(module, req->op.del.dn);
826 return ildb_rename(module,
827 req->op.rename.olddn,
828 req->op.rename.newdn);
830 case LDB_ASYNC_SEARCH:
831 return ildb_search_async(module,
833 req->op.search.scope,
835 req->op.search.attrs,
843 return ildb_add_async(module,
850 case LDB_ASYNC_MODIFY:
851 return ildb_modify_async(module,
858 case LDB_ASYNC_DELETE:
859 return ildb_delete_async(module,
866 case LDB_ASYNC_RENAME:
867 return ildb_rename_async(module,
868 req->op.rename.olddn,
869 req->op.rename.newdn,
882 fetch the rootDSE for later use
884 static int ildb_init_2(struct ldb_module *module)
886 struct ildb_private *ildb = module->private_data;
887 struct ldb_result *res = NULL;
888 struct ldb_dn *empty_dn = ldb_dn_new(ildb);
890 ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE,
891 ldb_parse_tree(empty_dn, "dn=dc=rootDSE"),
893 if (ret == LDB_SUCCESS && res->count == 1) {
894 ildb->rootDSE = talloc_steal(ildb, res->msgs[0]);
896 if (ret == LDB_SUCCESS) talloc_free(res);
897 talloc_free(empty_dn);
902 static const struct ldb_module_ops ildb_ops = {
904 .request = ildb_request,
905 .start_transaction = ildb_start_trans,
906 .end_transaction = ildb_end_trans,
907 .del_transaction = ildb_del_trans,
908 .second_stage_init = ildb_init_2
912 static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
914 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
917 return LDB_ERR_OPERATIONS_ERROR;
922 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
923 return LDB_ERR_OTHER;
927 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
928 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
929 return LDB_ERR_OTHER;
934 return LDB_ERR_OPERATIONS_ERROR;
937 return handle->status;
941 connect to the database
943 int ildb_connect(struct ldb_context *ldb, const char *url,
944 unsigned int flags, const char *options[])
946 struct ildb_private *ildb = NULL;
948 struct cli_credentials *creds;
950 ildb = talloc(ldb, struct ildb_private);
956 ildb->rootDSE = NULL;
959 ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
965 status = ldap_connect(ildb->ldap, url);
966 if (!NT_STATUS_IS_OK(status)) {
967 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
968 url, ldap_errstr(ildb->ldap, status));
972 ldb->modules = talloc(ldb, struct ldb_module);
977 ldb->modules->ldb = ldb;
978 ldb->modules->prev = ldb->modules->next = NULL;
979 ldb->modules->private_data = ildb;
980 ldb->modules->ops = &ildb_ops;
982 /* caller can optionally setup credentials using the opaque token 'credentials' */
983 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
985 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
987 creds = session_info->credentials;
991 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
992 const char *bind_dn = cli_credentials_get_bind_dn(creds);
994 const char *password = cli_credentials_get_password(creds);
995 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
996 if (!NT_STATUS_IS_OK(status)) {
997 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
998 ldap_errstr(ildb->ldap, status));
1002 status = ldap_bind_sasl(ildb->ldap, creds);
1003 if (!NT_STATUS_IS_OK(status)) {
1004 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1005 ldap_errstr(ildb->ldap, status));
1011 ldb->async_wait = &ildb_async_wait;
1017 ldb->modules->private_data = NULL;