r18438: I should have examined these uses of talloc_move() more
[Samba.git] / source / lib / ldb / ldb_ildap / ldb_ildap.c
blob49a8e8627a4444d8fc35ecc45a21e6e573e29761
1 /*
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
9 ** under the LGPL
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 * Name: ldb_ildap
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
37 * Modifications:
39 * - description: make the module use asyncronous calls
40 * date: Feb 2006
41 * author: Simo Sorce
45 #include "includes.h"
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"
53 struct ildb_private {
54 struct ldap_connection *ldap;
55 struct ldb_context *ldb;
58 struct ildb_context {
59 struct ldb_module *module;
60 struct ldap_request *req;
61 void *context;
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;
73 unsigned int i;
74 int n = 0;
76 /* allocate maximum number of elements needed */
77 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
78 if (!mods) {
79 errno = ENOMEM;
80 return NULL;
82 mods[0] = NULL;
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);
88 if (!mods[n]) {
89 goto failed;
91 mods[n + 1] = NULL;
92 mods[n]->type = 0;
93 mods[n]->attrib = *el;
94 if (use_flags) {
95 switch (el->flags & LDB_FLAG_MOD_MASK) {
96 case LDB_FLAG_MOD_ADD:
97 mods[n]->type = LDAP_MODIFY_ADD;
98 break;
99 case LDB_FLAG_MOD_DELETE:
100 mods[n]->type = LDAP_MODIFY_DELETE;
101 break;
102 case LDB_FLAG_MOD_REPLACE:
103 mods[n]->type = LDAP_MODIFY_REPLACE;
104 break;
107 n++;
110 *num_mods = n;
111 return mods;
113 failed:
114 talloc_free(mods);
115 return NULL;
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)) {
125 return LDB_SUCCESS;
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;
146 return;
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);
154 NTSTATUS status;
155 int i;
157 handle->status = LDB_SUCCESS;
159 if (!NT_STATUS_IS_OK(req->status)) {
160 handle->status = ildb_map_error(ildb, req->status);
161 return;
164 if (req->num_replies < 1) {
165 handle->status = LDB_ERR_OPERATIONS_ERROR;
166 return;
169 switch (req->type) {
171 case LDAP_TAG_ModifyRequest:
172 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
173 handle->status = LDB_ERR_PROTOCOL_ERROR;
174 return;
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;
183 break;
185 case LDAP_TAG_AddRequest:
186 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
187 handle->status = LDB_ERR_PROTOCOL_ERROR;
188 return;
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;
197 break;
199 case LDAP_TAG_DelRequest:
200 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
201 handle->status = LDB_ERR_PROTOCOL_ERROR;
202 return;
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;
211 break;
213 case LDAP_TAG_ModifyDNRequest:
214 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
215 handle->status = LDB_ERR_PROTOCOL_ERROR;
216 return;
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;
225 break;
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;
233 int ret;
235 ares = talloc_zero(ac, struct ldb_reply);
236 if (!ares) {
237 handle->status = LDB_ERR_OPERATIONS_ERROR;
238 return;
241 msg = req->replies[i];
242 switch (msg->type) {
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);
249 return;
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;
262 break;
264 case LDAP_TAG_SearchResultEntry:
267 ares->message = ldb_msg_new(ares);
268 if (!ares->message) {
269 handle->status = LDB_ERR_OPERATIONS_ERROR;
270 return;
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;
278 return;
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;
286 break;
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;
295 break;
297 default:
298 /* TAG not handled, fail ! */
299 handle->status = LDB_ERR_PROTOCOL_ERROR;
300 return;
303 ret = ac->callback(ac->module->ldb, ac->context, ares);
304 if (ret) {
305 handle->status = ret;
309 talloc_free(req->replies);
310 req->replies = NULL;
311 req->num_replies = 0;
313 break;
315 default:
316 handle->status = LDB_ERR_PROTOCOL_ERROR;
317 return;
321 static struct ldb_handle *init_ildb_handle(struct ldb_module *module,
322 void *context,
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);
330 if (h == NULL) {
331 ldb_set_errstring(module->ldb, "Out of Memory");
332 return NULL;
335 h->module = module;
337 ildb_ac = talloc(h, struct ildb_context);
338 if (ildb_ac == NULL) {
339 ldb_set_errstring(module->ldb, "Out of Memory");
340 talloc_free(h);
341 return NULL;
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;
353 return h;
356 static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
357 void *context,
358 int (*callback)(struct ldb_context *, void *, struct ldb_reply *),
359 int timeout,
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;
367 if (!h) {
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);
374 if (req == NULL) {
375 ldb_set_errstring(module->ldb, "async send request failed");
376 return LDB_ERR_OPERATIONS_ERROR;
379 if (!req->conn) {
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;
386 if (timeout) {
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);
396 *handle = h;
397 return LDB_SUCCESS;
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;
406 if (!h) {
407 return LDB_ERR_OPERATIONS_ERROR;
410 ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
412 req->handle = h;
414 if (ildb_ac->callback) {
415 ret = ildb_ac->callback(module->ldb, ildb_ac->context, NULL);
417 req->handle->state = LDB_ASYNC_DONE;
418 return ret;
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;
428 int n;
430 req->handle = NULL;
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);
443 if (msg == NULL) {
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, "");
452 } else {
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");
457 talloc_free(msg);
458 return LDB_ERR_OPERATIONS_ERROR;
461 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
462 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
463 } else {
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));
482 add a record
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;
489 int i,n;
491 req->handle = NULL;
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);
499 if (msg == NULL) {
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) {
507 talloc_free(msg);
508 return LDB_ERR_INVALID_DN_SYNTAX;
511 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
512 if (mods == NULL) {
513 talloc_free(msg);
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) {
520 talloc_free(msg);
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));
532 modify a record
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;
539 int i,n;
541 req->handle = NULL;
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);
549 if (msg == NULL) {
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) {
557 talloc_free(msg);
558 return LDB_ERR_INVALID_DN_SYNTAX;
561 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
562 if (mods == NULL) {
563 talloc_free(msg);
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) {
570 talloc_free(msg);
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));
582 delete a record
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;
589 req->handle = NULL;
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);
597 if (msg == NULL) {
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) {
605 talloc_free(msg);
606 return LDB_ERR_INVALID_DN_SYNTAX;
609 return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
613 rename a record
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;
620 req->handle = NULL;
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);
628 if (msg == NULL) {
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) {
635 talloc_free(msg);
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) {
644 talloc_free(msg);
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) {
652 talloc_free(msg);
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 */
665 return LDB_SUCCESS;
668 static int ildb_end_trans(struct ldb_module *module)
670 /* TODO implement a local transaction mechanism here */
672 return LDB_SUCCESS;
675 static int ildb_del_trans(struct ldb_module *module)
677 /* TODO implement a local locking mechanism here */
679 return LDB_SUCCESS;
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;
695 if (!ac) {
696 return LDB_ERR_OPERATIONS_ERROR;
699 handle->state = LDB_ASYNC_INIT;
701 switch(type) {
702 case LDB_WAIT_NONE:
703 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
704 return LDB_ERR_OTHER;
706 break;
707 case LDB_WAIT_ALL:
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;
713 break;
714 default:
715 return LDB_ERR_OPERATIONS_ERROR;
718 return handle->status;
721 static const struct ldb_module_ops ildb_ops = {
722 .name = "ldap",
723 .search = ildb_search,
724 .add = ildb_add,
725 .modify = ildb_modify,
726 .del = ildb_delete,
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,
732 .wait = ildb_wait
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;
743 NTSTATUS status;
744 struct cli_credentials *creds;
746 ildb = talloc(ldb, struct ildb_private);
747 if (!ildb) {
748 ldb_oom(ldb);
749 goto failed;
752 ildb->ldb = ldb;
754 ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
755 if (!ildb->ldap) {
756 ldb_oom(ldb);
757 goto failed;
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));
768 goto failed;
772 *module = talloc(ldb, struct ldb_module);
773 if (!module) {
774 ldb_oom(ldb);
775 talloc_free(ildb);
776 return -1;
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);
785 if (creds == NULL) {
786 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
787 if (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);
794 if (bind_dn) {
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));
800 goto failed;
802 } else {
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));
807 goto failed;
812 return 0;
814 failed:
815 talloc_free(ildb);
816 return -1;
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);