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_message *rootDSE;
56 struct ldb_context *ldb;
59 struct ildb_async_context {
60 struct ldb_module *module;
61 struct ldap_request *req;
63 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
67 convert a ldb_message structure to a list of ldap_mod structures
68 ready for ildap_add() or ildap_modify()
70 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
71 const struct ldb_message *msg, int use_flags)
73 struct ldap_mod **mods;
77 /* allocate maximum number of elements needed */
78 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
85 for (i = 0; i < msg->num_elements; i++) {
86 const struct ldb_message_element *el = &msg->elements[i];
88 mods[n] = talloc(mods, struct ldap_mod);
94 mods[n]->attrib = *el;
96 switch (el->flags & LDB_FLAG_MOD_MASK) {
97 case LDB_FLAG_MOD_ADD:
98 mods[n]->type = LDAP_MODIFY_ADD;
100 case LDB_FLAG_MOD_DELETE:
101 mods[n]->type = LDAP_MODIFY_DELETE;
103 case LDB_FLAG_MOD_REPLACE:
104 mods[n]->type = LDAP_MODIFY_REPLACE;
121 map an ildap NTSTATUS to a ldb error code
123 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
125 if (NT_STATUS_IS_OK(status)) {
128 talloc_free(ildb->ldb->err_string);
129 ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status));
130 if (NT_STATUS_IS_LDAP(status)) {
131 return NT_STATUS_LDAP_CODE(status);
133 return LDB_ERR_OPERATIONS_ERROR;
136 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
137 struct timeval t, void *private_data)
139 struct ldb_async_handle *handle = talloc_get_type(private_data, struct ldb_async_handle);
140 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
142 if (ac->req->state == LDAP_REQUEST_PENDING) {
143 DLIST_REMOVE(ac->req->conn->pending, ac->req);
146 handle->status = LDB_ERR_OPERATIONS_ERROR;
151 static void ildb_async_callback(struct ldap_request *req)
153 struct ldb_async_handle *handle = talloc_get_type(req->async.private_data, struct ldb_async_handle);
154 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
155 struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private);
159 handle->status = LDB_SUCCESS;
161 if (!NT_STATUS_IS_OK(req->status)) {
162 handle->status = ildb_map_error(ildb, req->status);
166 if (req->num_replies < 1) {
167 handle->status = LDB_ERR_OPERATIONS_ERROR;
173 case LDAP_TAG_ModifyRequest:
174 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
175 handle->status = LDB_ERR_PROTOCOL_ERROR;
178 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
179 handle->status = ildb_map_error(ildb, status);
180 if (ac->callback && handle->status == LDB_SUCCESS) {
181 /* FIXME: build a corresponding ares to pass on */
182 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
184 handle->state = LDB_ASYNC_DONE;
187 case LDAP_TAG_AddRequest:
188 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
189 handle->status = LDB_ERR_PROTOCOL_ERROR;
192 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
193 handle->status = ildb_map_error(ildb, status);
194 if (ac->callback && handle->status == LDB_SUCCESS) {
195 /* FIXME: build a corresponding ares to pass on */
196 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
198 handle->state = LDB_ASYNC_DONE;
201 case LDAP_TAG_DelRequest:
202 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
203 handle->status = LDB_ERR_PROTOCOL_ERROR;
206 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
207 handle->status = ildb_map_error(ildb, status);
208 if (ac->callback && handle->status == LDB_SUCCESS) {
209 /* FIXME: build a corresponding ares to pass on */
210 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
212 handle->state = LDB_ASYNC_DONE;
215 case LDAP_TAG_ModifyDNRequest:
216 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
217 handle->status = LDB_ERR_PROTOCOL_ERROR;
220 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
221 handle->status = ildb_map_error(ildb, status);
222 if (ac->callback && handle->status == LDB_SUCCESS) {
223 /* FIXME: build a corresponding ares to pass on */
224 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
226 handle->state = LDB_ASYNC_DONE;
229 case LDAP_TAG_SearchRequest:
230 /* loop over all messages */
231 for (i = 0; i < req->num_replies; i++) {
232 struct ldap_SearchResEntry *search;
233 struct ldb_async_result *ares = NULL;
234 struct ldap_message *msg;
237 ares = talloc_zero(ac, struct ldb_async_result);
239 handle->status = LDB_ERR_OPERATIONS_ERROR;
243 msg = req->replies[i];
246 case LDAP_TAG_SearchResultDone:
248 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
249 if (!NT_STATUS_IS_OK(status)) {
250 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Error: %s\n" ,ldap_errstr(req->conn, status));
251 handle->status = ildb_map_error(ildb, status);
256 ares->controls = talloc_steal(ares, msg->controls);
258 if (msg->r.SearchResultDone.resultcode) {
259 if (msg->r.SearchResultDone.errormessage) {
260 ldb_set_errstring(ac->module->ldb, talloc_strdup(ac->module, msg->r.SearchResultDone.errormessage));
264 handle->status = msg->r.SearchResultDone.resultcode;
265 handle->state = LDB_ASYNC_DONE;
266 ares->type = LDB_REPLY_DONE;
269 case LDAP_TAG_SearchResultEntry:
272 ares->message = ldb_msg_new(ares);
273 if (!ares->message) {
274 handle->status = LDB_ERR_OPERATIONS_ERROR;;
278 search = &(msg->r.SearchResultEntry);
280 ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn);
281 if (ares->message->dn == NULL) {
282 handle->status = LDB_ERR_OPERATIONS_ERROR;
285 ares->message->num_elements = search->num_attributes;
286 ares->message->elements = talloc_steal(ares->message, search->attributes);
288 handle->status = LDB_SUCCESS;
289 handle->state = LDB_ASYNC_PENDING;
290 ares->type = LDB_REPLY_ENTRY;
293 case LDAP_TAG_SearchResultReference:
295 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
297 handle->status = LDB_SUCCESS;
298 handle->state = LDB_ASYNC_PENDING;
299 ares->type = LDB_REPLY_REFERRAL;
303 /* TAG not handled, fail ! */
304 handle->status = LDB_ERR_PROTOCOL_ERROR;
308 ret = ac->callback(ac->module->ldb, ac->context, ares);
310 handle->status = ret;
314 talloc_free(req->replies);
316 req->num_replies = 0;
321 handle->status = LDB_ERR_PROTOCOL_ERROR;
326 static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
328 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
330 struct ldb_async_handle **handle)
332 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
333 struct ildb_async_context *ildb_ac;
334 struct ldb_async_handle *h;
335 struct ldap_request *req;
337 h = talloc_zero(ildb->ldap, struct ldb_async_handle);
339 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
340 return LDB_ERR_OPERATIONS_ERROR;
345 ildb_ac = talloc(h, struct ildb_async_context);
346 if (ildb_ac == NULL) {
347 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
349 return LDB_ERR_OPERATIONS_ERROR;
352 h->private_data = (void *)ildb_ac;
354 h->state = LDB_ASYNC_INIT;
355 h->status = LDB_SUCCESS;
357 req = ldap_request_send(ildb->ldap, msg);
359 ldb_set_errstring(module->ldb, talloc_asprintf(module, "async send request failed"));
360 return LDB_ERR_OPERATIONS_ERROR;
363 ildb_ac->req = talloc_steal(ildb_ac, req);
364 ildb_ac->module = module;
365 ildb_ac->context = context;
366 ildb_ac->callback = callback;
368 req->async.fn = ildb_async_callback;
369 req->async.private_data = (void *)h;
371 talloc_free(req->time_event);
372 req->time_event = NULL;
374 req->time_event = event_add_timed(req->conn->event.event_ctx, h,
375 timeval_current_ofs(timeout, 0),
376 ildb_request_timeout, h);
386 search for matching records using an asynchronous function
388 static int ildb_search_async(struct ldb_module *module, struct ldb_request *req)
390 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
391 struct ldap_message *msg;
394 req->async.handle = NULL;
396 if (!req->async.callback || !req->async.context) {
397 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
398 return LDB_ERR_OPERATIONS_ERROR;
401 if (req->op.search.tree == NULL) {
402 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Invalid expression parse tree"));
403 return LDB_ERR_OPERATIONS_ERROR;
406 msg = new_ldap_message(ildb);
408 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
409 return LDB_ERR_OPERATIONS_ERROR;
412 msg->type = LDAP_TAG_SearchRequest;
414 if (req->op.search.base == NULL) {
415 if (ildb->rootDSE != NULL) {
416 msg->r.SearchRequest.basedn =
417 talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", ""));
419 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
422 msg->r.SearchRequest.basedn = ldb_dn_linearize(msg, req->op.search.base);
424 if (msg->r.SearchRequest.basedn == NULL) {
425 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Unable to determine baseDN"));
427 return LDB_ERR_OPERATIONS_ERROR;
430 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
431 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
433 msg->r.SearchRequest.scope = req->op.search.scope;
436 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
437 msg->r.SearchRequest.timelimit = 0;
438 msg->r.SearchRequest.sizelimit = 0;
439 msg->r.SearchRequest.attributesonly = 0;
440 msg->r.SearchRequest.tree = req->op.search.tree;
442 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
443 msg->r.SearchRequest.num_attributes = n;
444 msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
445 msg->controls = req->controls;
447 return ildb_request_send(module, msg, req->async.context, req->async.callback, req->async.timeout, &(req->async.handle));
450 static int ildb_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
452 struct ldb_result *res;
456 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback"));
457 return LDB_ERR_OPERATIONS_ERROR;
460 res = *((struct ldb_result **)context);
466 if (ares->type == LDB_REPLY_ENTRY) {
467 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
472 res->msgs[res->count + 1] = NULL;
474 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
475 if (! res->msgs[res->count]) {
482 if (ares->type == LDB_REPLY_REFERRAL) {
484 for (n = 0; res->refs[n]; n++) /*noop*/ ;
489 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
494 res->refs[n] = talloc_steal(res->refs, ares->referral);
495 res->refs[n + 1] = NULL;
498 if (ares->controls) {
499 res->controls = talloc_steal(res, ares->controls);
500 if (! res->controls) {
511 *((struct ldb_result **)context) = NULL;
512 return LDB_ERR_OPERATIONS_ERROR;
516 search for matching records using a synchronous function
518 static int ildb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
519 enum ldb_scope scope, struct ldb_parse_tree *tree,
520 const char * const *attrs,
521 struct ldb_control **control_req,
522 struct ldb_result **res)
524 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
525 struct ldb_request *req;
528 *res = talloc_zero(ildb, struct ldb_result);
530 return LDB_ERR_OPERATIONS_ERROR;
533 req = talloc_zero(ildb, struct ldb_request);
535 return LDB_ERR_OPERATIONS_ERROR;
538 req->operation = LDB_ASYNC_SEARCH;
539 req->op.search.base = base;
540 req->op.search.scope = scope;
541 req->op.search.tree = tree;
542 req->op.search.attrs = attrs;
543 req->controls = control_req;
544 req->async.context = (void *)res;
545 req->async.callback = ildb_search_sync_callback;
546 req->async.timeout = ildb->ldap->timeout;
548 ret = ildb_search_async(module, req);
550 if (ret == LDB_SUCCESS) {
551 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
555 if (ret != LDB_SUCCESS) {
565 static int ildb_add_async(struct ldb_module *module, struct ldb_request *req)
567 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
568 struct ldap_message *msg;
569 struct ldap_mod **mods;
572 req->async.handle = NULL;
574 /* ignore ltdb specials */
575 if (ldb_dn_is_special(req->op.add.message->dn)) {
579 msg = new_ldap_message(ildb->ldap);
581 return LDB_ERR_OPERATIONS_ERROR;
584 msg->type = LDAP_TAG_AddRequest;
586 msg->r.AddRequest.dn = ldb_dn_linearize(msg, req->op.add.message->dn);
587 if (msg->r.AddRequest.dn == NULL) {
589 return LDB_ERR_INVALID_DN_SYNTAX;
592 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
595 return LDB_ERR_OPERATIONS_ERROR;
598 msg->r.AddRequest.num_attributes = n;
599 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
600 if (msg->r.AddRequest.attributes == NULL) {
602 return LDB_ERR_OPERATIONS_ERROR;
605 for (i = 0; i < n; i++) {
606 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
609 return ildb_request_send(module, msg, req->async.context, req->async.callback, req->async.timeout, &(req->async.handle));
612 static int ildb_add(struct ldb_module *module, const struct ldb_message *msg)
614 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
615 struct ldb_request *req;
618 req = talloc_zero(ildb, struct ldb_request);
620 return LDB_ERR_OPERATIONS_ERROR;
623 req->operation = LDB_ASYNC_ADD;
624 req->op.add.message = msg;
625 req->controls = NULL;
626 req->async.context = NULL;
627 req->async.callback = NULL;
628 req->async.timeout = ildb->ldap->timeout;
630 ret = ildb_add_async(module, req);
632 if (ret != LDB_SUCCESS) {
637 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
646 static int ildb_modify_async(struct ldb_module *module, struct ldb_request *req)
648 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
649 struct ldap_message *msg;
650 struct ldap_mod **mods;
653 req->async.handle = NULL;
655 /* ignore ltdb specials */
656 if (ldb_dn_is_special(req->op.mod.message->dn)) {
660 msg = new_ldap_message(ildb->ldap);
662 return LDB_ERR_OPERATIONS_ERROR;
665 msg->type = LDAP_TAG_ModifyRequest;
667 msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, req->op.mod.message->dn);
668 if (msg->r.ModifyRequest.dn == NULL) {
670 return LDB_ERR_INVALID_DN_SYNTAX;
673 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
676 return LDB_ERR_OPERATIONS_ERROR;
679 msg->r.ModifyRequest.num_mods = n;
680 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
681 if (msg->r.ModifyRequest.mods == NULL) {
683 return LDB_ERR_OPERATIONS_ERROR;
686 for (i = 0; i < n; i++) {
687 msg->r.ModifyRequest.mods[i] = *mods[i];
690 return ildb_request_send(module, msg,
694 &(req->async.handle));
697 static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg)
699 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
700 struct ldb_request *req;
703 req = talloc_zero(ildb, struct ldb_request);
705 return LDB_ERR_OPERATIONS_ERROR;
708 req->operation = LDB_ASYNC_MODIFY;
709 req->op.mod.message = msg;
710 req->controls = NULL;
711 req->async.context = NULL;
712 req->async.callback = NULL;
713 req->async.timeout = ildb->ldap->timeout;
715 ret = ildb_modify_async(module, req);
717 if (ret != LDB_SUCCESS) {
722 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
731 static int ildb_delete_async(struct ldb_module *module, struct ldb_request *req)
733 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
734 struct ldap_message *msg;
736 req->async.handle = NULL;
738 /* ignore ltdb specials */
739 if (ldb_dn_is_special(req->op.del.dn)) {
743 msg = new_ldap_message(ildb->ldap);
745 return LDB_ERR_OPERATIONS_ERROR;
748 msg->type = LDAP_TAG_DelRequest;
750 msg->r.DelRequest.dn = ldb_dn_linearize(msg, req->op.del.dn);
751 if (msg->r.DelRequest.dn == NULL) {
753 return LDB_ERR_INVALID_DN_SYNTAX;
756 return ildb_request_send(module, msg,
760 &(req->async.handle));
763 static int ildb_delete(struct ldb_module *module, const struct ldb_dn *dn)
765 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
766 struct ldb_request *req;
769 req = talloc_zero(ildb, struct ldb_request);
771 return LDB_ERR_OPERATIONS_ERROR;
774 req->operation = LDB_ASYNC_DELETE;
776 req->controls = NULL;
777 req->async.context = NULL;
778 req->async.callback = NULL;
779 req->async.timeout = ildb->ldap->timeout;
781 ret = ildb_delete_async(module, req);
783 if (ret != LDB_SUCCESS) {
788 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
797 static int ildb_rename_async(struct ldb_module *module, struct ldb_request *req)
799 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
800 struct ldap_message *msg;
802 req->async.handle = NULL;
804 /* ignore ltdb specials */
805 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
809 msg = new_ldap_message(ildb->ldap);
811 return LDB_ERR_OPERATIONS_ERROR;
814 msg->type = LDAP_TAG_ModifyDNRequest;
815 msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, req->op.rename.olddn);
816 if (msg->r.ModifyDNRequest.dn == NULL) {
818 return LDB_ERR_INVALID_DN_SYNTAX;
821 msg->r.ModifyDNRequest.newrdn =
822 talloc_asprintf(msg, "%s=%s",
823 req->op.rename.newdn->components[0].name,
824 ldb_dn_escape_value(msg, req->op.rename.newdn->components[0].value));
825 if (msg->r.ModifyDNRequest.newrdn == NULL) {
827 return LDB_ERR_OPERATIONS_ERROR;
830 msg->r.ModifyDNRequest.newsuperior =
831 ldb_dn_linearize(msg,
832 ldb_dn_get_parent(msg, req->op.rename.newdn));
833 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
835 return LDB_ERR_INVALID_DN_SYNTAX;
838 msg->r.ModifyDNRequest.deleteolddn = True;
840 return ildb_request_send(module, msg,
844 &(req->async.handle));
847 static int ildb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
849 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
850 struct ldb_request *req;
853 req = talloc_zero(ildb, struct ldb_request);
855 return LDB_ERR_OPERATIONS_ERROR;
858 req->operation = LDB_ASYNC_RENAME;
859 req->op.rename.olddn = olddn;
860 req->op.rename.newdn = newdn;
861 req->controls = NULL;
862 req->async.context = NULL;
863 req->async.callback = NULL;
864 req->async.timeout = ildb->ldap->timeout;
866 ret = ildb_rename_async(module, req);
868 if (ret != LDB_SUCCESS) {
873 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
879 static int ildb_start_trans(struct ldb_module *module)
881 /* TODO implement a local locking mechanism here */
886 static int ildb_end_trans(struct ldb_module *module)
888 /* TODO implement a local transaction mechanism here */
893 static int ildb_del_trans(struct ldb_module *module)
895 /* TODO implement a local locking mechanism here */
900 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
902 switch (req->operation) {
905 return ildb_search_bytree(module,
907 req->op.search.scope,
909 req->op.search.attrs,
911 &req->op.search.res);
914 return ildb_add(module, req->op.add.message);
917 return ildb_modify(module, req->op.mod.message);
920 return ildb_delete(module, req->op.del.dn);
923 return ildb_rename(module,
924 req->op.rename.olddn,
925 req->op.rename.newdn);
933 static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
935 struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
937 if (handle->state == LDB_ASYNC_DONE) {
938 return handle->status;
942 return LDB_ERR_OPERATIONS_ERROR;
945 handle->state = LDB_ASYNC_INIT;
949 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
950 return LDB_ERR_OTHER;
954 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
955 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
956 return LDB_ERR_OTHER;
961 return LDB_ERR_OPERATIONS_ERROR;
968 fetch the rootDSE for later use
970 static int ildb_init(struct ldb_module *module)
972 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
973 struct ldb_result *res = NULL;
974 struct ldb_dn *empty_dn = ldb_dn_new(ildb);
976 ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE,
977 ldb_parse_tree(empty_dn, "dn=dc=rootDSE"),
979 if (ret == LDB_SUCCESS && res->count == 1) {
980 ildb->rootDSE = talloc_steal(ildb, res->msgs[0]);
982 if (ret == LDB_SUCCESS) talloc_free(res);
983 talloc_free(empty_dn);
988 static const struct ldb_module_ops ildb_ops = {
990 .search = ildb_search_async,
991 .add = ildb_add_async,
992 .modify = ildb_modify_async,
993 .del = ildb_delete_async,
994 .rename = ildb_rename_async,
995 .request = ildb_request,
996 .start_transaction = ildb_start_trans,
997 .end_transaction = ildb_end_trans,
998 .del_transaction = ildb_del_trans,
999 .async_wait = ildb_async_wait,
1000 .init_context = ildb_init
1004 connect to the database
1006 static int ildb_connect(struct ldb_context *ldb, const char *url,
1007 unsigned int flags, const char *options[])
1009 struct ildb_private *ildb = NULL;
1011 struct cli_credentials *creds;
1013 ildb = talloc(ldb, struct ildb_private);
1019 ildb->rootDSE = NULL;
1022 ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
1028 if (flags == LDB_FLG_RECONNECT) {
1029 ldap_set_reconn_params(ildb->ldap, 10);
1032 status = ldap_connect(ildb->ldap, url);
1033 if (!NT_STATUS_IS_OK(status)) {
1034 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
1035 url, ldap_errstr(ildb->ldap, status));
1039 ldb->modules = talloc(ldb, struct ldb_module);
1040 if (!ldb->modules) {
1044 ldb->modules->ldb = ldb;
1045 ldb->modules->prev = ldb->modules->next = NULL;
1046 ldb->modules->private_data = ildb;
1047 ldb->modules->ops = &ildb_ops;
1049 /* caller can optionally setup credentials using the opaque token 'credentials' */
1050 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
1051 if (creds == NULL) {
1052 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
1054 creds = session_info->credentials;
1058 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
1059 const char *bind_dn = cli_credentials_get_bind_dn(creds);
1061 const char *password = cli_credentials_get_password(creds);
1062 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1065 ldap_errstr(ildb->ldap, status));
1069 status = ldap_bind_sasl(ildb->ldap, creds);
1070 if (!NT_STATUS_IS_OK(status)) {
1071 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1072 ldap_errstr(ildb->ldap, status));
1082 ldb->modules->private_data = NULL;
1088 int ldb_ildap_init(void)
1090 return ldb_register_backend("ldap", ildb_connect) +
1091 ldb_register_backend("ldapi", ildb_connect) +
1092 ldb_register_backend("ldaps", ildb_connect);