2 ldb database library - ildap backend
4 Copyright (C) Andrew Tridgell 2005
5 Copyright (C) Simo Sorce 2008
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 3 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, see <http://www.gnu.org/licenses/>.
28 * Component: ldb ildap backend
30 * Description: This is a ldb backend for the internal ldap
31 * client library in Samba4. By using this backend we are
32 * independent of a system ldap library
34 * Author: Andrew Tridgell
38 * - description: make the module use asyncronous calls
45 #include "ldb_includes.h"
47 #include "lib/events/events.h"
48 #include "libcli/ldap/ldap.h"
49 #include "libcli/ldap/ldap_client.h"
50 #include "auth/auth.h"
51 #include "auth/credentials/credentials.h"
52 #include "param/param.h"
55 struct ldap_connection *ldap;
56 struct ldb_module *module;
60 struct ildb_private *ildb;
61 struct ldb_handle *handle;
62 struct ldap_request *req;
64 int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
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,
75 struct ldap_mod **mods;
79 /* allocate maximum number of elements needed */
80 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
87 for (i = 0; i < msg->num_elements; i++) {
88 const struct ldb_message_element *el = &msg->elements[i];
90 mods[n] = talloc(mods, struct ldap_mod);
96 mods[n]->attrib = *el;
98 switch (el->flags & LDB_FLAG_MOD_MASK) {
99 case LDB_FLAG_MOD_ADD:
100 mods[n]->type = LDAP_MODIFY_ADD;
102 case LDB_FLAG_MOD_DELETE:
103 mods[n]->type = LDAP_MODIFY_DELETE;
105 case LDB_FLAG_MOD_REPLACE:
106 mods[n]->type = LDAP_MODIFY_REPLACE;
123 map an ildap NTSTATUS to a ldb error code
125 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
127 TALLOC_CTX *mem_ctx = talloc_new(ildb);
128 if (NT_STATUS_IS_OK(status)) {
132 ldb_oom(ildb->module->ldb);
133 return LDB_ERR_OPERATIONS_ERROR;
135 ldb_set_errstring(ildb->module->ldb,
136 ldap_errstr(ildb->ldap, mem_ctx, status));
137 talloc_free(mem_ctx);
138 if (NT_STATUS_IS_LDAP(status)) {
139 return NT_STATUS_LDAP_CODE(status);
141 return LDB_ERR_OPERATIONS_ERROR;
144 static void ildb_request_timeout(struct event_context *ev,
145 struct timed_event *te,
146 struct timeval t, void *private_data)
148 struct ildb_context *ac;
149 struct ldb_handle *handle;
151 ac = talloc_get_type(private_data, struct ildb_context);
154 ac = talloc_get_type(private_data, struct ildb_context);
156 if (ac->req->state == LDAP_REQUEST_PENDING) {
157 DLIST_REMOVE(ac->req->conn->pending, ac->req);
160 handle->status = LDB_ERR_TIME_LIMIT_EXCEEDED;
165 static void ildb_callback(struct ldap_request *req)
167 struct ildb_context *ac;
168 struct ldb_handle *handle;
169 struct ildb_private *ildb;
173 ac = talloc_get_type(req->async.private_data, struct ildb_context);
176 handle->status = LDB_SUCCESS;
178 if (!NT_STATUS_IS_OK(req->status)) {
179 handle->status = ildb_map_error(ildb, req->status);
183 if (req->num_replies < 1) {
184 handle->status = LDB_ERR_OPERATIONS_ERROR;
190 case LDAP_TAG_ModifyRequest:
191 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
192 handle->status = LDB_ERR_PROTOCOL_ERROR;
195 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
196 handle->status = ildb_map_error(ildb, status);
197 if (ac->callback && handle->status == LDB_SUCCESS) {
198 /* FIXME: build a corresponding ares to pass on */
199 handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
201 handle->state = LDB_ASYNC_DONE;
204 case LDAP_TAG_AddRequest:
205 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
206 handle->status = LDB_ERR_PROTOCOL_ERROR;
209 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
210 handle->status = ildb_map_error(ildb, status);
211 if (ac->callback && handle->status == LDB_SUCCESS) {
212 /* FIXME: build a corresponding ares to pass on */
213 handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
215 handle->state = LDB_ASYNC_DONE;
218 case LDAP_TAG_DelRequest:
219 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
220 handle->status = LDB_ERR_PROTOCOL_ERROR;
223 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
224 handle->status = ildb_map_error(ildb, status);
225 if (ac->callback && handle->status == LDB_SUCCESS) {
226 /* FIXME: build a corresponding ares to pass on */
227 handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
229 handle->state = LDB_ASYNC_DONE;
232 case LDAP_TAG_ModifyDNRequest:
233 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
234 handle->status = LDB_ERR_PROTOCOL_ERROR;
237 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
238 handle->status = ildb_map_error(ildb, status);
239 if (ac->callback && handle->status == LDB_SUCCESS) {
240 /* FIXME: build a corresponding ares to pass on */
241 handle->status = ac->callback(ac->ildb->module->ldb, ac->context, NULL);
243 handle->state = LDB_ASYNC_DONE;
246 case LDAP_TAG_SearchRequest:
247 /* loop over all messages */
248 for (i = 0; i < req->num_replies; i++) {
249 struct ldap_SearchResEntry *search;
250 struct ldb_reply *ares = NULL;
251 struct ldap_message *msg;
254 ares = talloc_zero(ac, struct ldb_reply);
256 handle->status = LDB_ERR_OPERATIONS_ERROR;
260 msg = req->replies[i];
263 case LDAP_TAG_SearchResultDone:
265 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
266 if (!NT_STATUS_IS_OK(status)) {
267 handle->status = ildb_map_error(ildb, status);
271 ares->controls = talloc_move(ares, &msg->controls);
272 if (msg->r.SearchResultDone.resultcode) {
273 if (msg->r.SearchResultDone.errormessage) {
274 ldb_set_errstring(ac->ildb->module->ldb, msg->r.SearchResultDone.errormessage);
278 handle->status = msg->r.SearchResultDone.resultcode;
279 handle->state = LDB_ASYNC_DONE;
280 ares->type = LDB_REPLY_DONE;
283 case LDAP_TAG_SearchResultEntry:
286 ares->message = ldb_msg_new(ares);
287 if (!ares->message) {
288 handle->status = LDB_ERR_OPERATIONS_ERROR;
292 search = &(msg->r.SearchResultEntry);
294 ares->message->dn = ldb_dn_new(ares->message, ac->ildb->module->ldb, search->dn);
295 if ( ! ldb_dn_validate(ares->message->dn)) {
296 handle->status = LDB_ERR_OPERATIONS_ERROR;
299 ares->message->num_elements = search->num_attributes;
300 ares->message->elements = talloc_move(ares->message,
301 &search->attributes);
303 handle->status = LDB_SUCCESS;
304 handle->state = LDB_ASYNC_PENDING;
305 ares->type = LDB_REPLY_ENTRY;
308 case LDAP_TAG_SearchResultReference:
310 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
312 handle->status = LDB_SUCCESS;
313 handle->state = LDB_ASYNC_PENDING;
314 ares->type = LDB_REPLY_REFERRAL;
318 /* TAG not handled, fail ! */
319 handle->status = LDB_ERR_PROTOCOL_ERROR;
323 ret = ac->callback(ac->ildb->module->ldb, ac->context, ares);
325 handle->status = ret;
329 talloc_free(req->replies);
331 req->num_replies = 0;
336 handle->status = LDB_ERR_PROTOCOL_ERROR;
341 static struct ildb_context *init_ildb_handle(struct ildb_private *ildb,
342 struct ldb_request *req)
344 struct ildb_context *ildb_ac;
345 struct ldb_handle *h;
347 h = talloc_zero(req, struct ldb_handle);
349 ldb_set_errstring(ildb->module->ldb, "Out of Memory");
353 h->module = ildb->module;
355 ildb_ac = talloc(h, struct ildb_context);
356 if (ildb_ac == NULL) {
357 ldb_set_errstring(ildb->module->ldb, "Out of Memory");
362 h->private_data = ildb_ac;
364 h->state = LDB_ASYNC_INIT;
365 h->status = LDB_SUCCESS;
367 ildb_ac->ildb = ildb;
369 ildb_ac->context = req->context;
370 ildb_ac->callback = req->callback;
376 static int ildb_request_send(struct ildb_private *ildb, struct ldap_message *msg, struct ldb_request *r)
378 struct ildb_context *ildb_ac = init_ildb_handle(ildb, r);
379 struct ldap_request *req;
382 return LDB_ERR_OPERATIONS_ERROR;
385 req = ldap_request_send(ildb->ldap, msg);
387 ldb_set_errstring(ildb->module->ldb, "async send request failed");
388 return LDB_ERR_OPERATIONS_ERROR;
390 ildb_ac->req = talloc_steal(ildb_ac, req);
393 ldb_set_errstring(ildb->module->ldb, "connection to remote LDAP server dropped?");
394 return LDB_ERR_OPERATIONS_ERROR;
397 talloc_free(req->time_event);
398 req->time_event = NULL;
400 req->time_event = event_add_timed(req->conn->event.event_ctx, ildb_ac,
401 timeval_current_ofs(r->timeout, 0),
402 ildb_request_timeout, ildb_ac);
405 req->async.fn = ildb_callback;
406 req->async.private_data = ildb_ac;
411 static int ildb_request_noop(struct ildb_private *ildb, struct ldb_request *req)
413 struct ildb_context *ildb_ac = init_ildb_handle(ildb, req);
414 int ret = LDB_SUCCESS;
417 return LDB_ERR_OPERATIONS_ERROR;
420 if (ildb_ac->callback) {
421 ret = ildb_ac->callback(ildb->module->ldb, ildb_ac->context, NULL);
423 ildb_ac->handle->state = LDB_ASYNC_DONE;
428 search for matching records using an asynchronous function
430 static int ildb_search(struct ldb_module *module, struct ldb_request *req)
432 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
433 struct ldap_message *msg;
438 if (!req->callback || !req->context) {
439 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
440 return LDB_ERR_OPERATIONS_ERROR;
443 if (req->op.search.tree == NULL) {
444 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
445 return LDB_ERR_OPERATIONS_ERROR;
448 msg = new_ldap_message(req);
450 ldb_set_errstring(module->ldb, "Out of Memory");
451 return LDB_ERR_OPERATIONS_ERROR;
454 msg->type = LDAP_TAG_SearchRequest;
456 if (req->op.search.base == NULL) {
457 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
459 msg->r.SearchRequest.basedn = ldb_dn_alloc_linearized(msg, req->op.search.base);
461 if (msg->r.SearchRequest.basedn == NULL) {
462 ldb_set_errstring(module->ldb, "Unable to determine baseDN");
464 return LDB_ERR_OPERATIONS_ERROR;
467 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
468 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
470 msg->r.SearchRequest.scope = req->op.search.scope;
473 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
474 msg->r.SearchRequest.timelimit = 0;
475 msg->r.SearchRequest.sizelimit = 0;
476 msg->r.SearchRequest.attributesonly = 0;
477 msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
479 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
480 msg->r.SearchRequest.num_attributes = n;
481 msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
482 msg->controls = req->controls;
484 return ildb_request_send(ildb, msg, req);
490 static int ildb_add(struct ldb_module *module, struct ldb_request *req)
492 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
493 struct ldap_message *msg;
494 struct ldap_mod **mods;
499 /* ignore ltdb specials */
500 if (ldb_dn_is_special(req->op.add.message->dn)) {
501 return ildb_request_noop(ildb, req);
504 msg = new_ldap_message(req);
506 return LDB_ERR_OPERATIONS_ERROR;
509 msg->type = LDAP_TAG_AddRequest;
511 msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
512 if (msg->r.AddRequest.dn == NULL) {
514 return LDB_ERR_INVALID_DN_SYNTAX;
517 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
520 return LDB_ERR_OPERATIONS_ERROR;
523 msg->r.AddRequest.num_attributes = n;
524 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
525 if (msg->r.AddRequest.attributes == NULL) {
527 return LDB_ERR_OPERATIONS_ERROR;
530 for (i = 0; i < n; i++) {
531 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
534 return ildb_request_send(ildb, msg, req);
540 static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
542 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
543 struct ldap_message *msg;
544 struct ldap_mod **mods;
549 /* ignore ltdb specials */
550 if (ldb_dn_is_special(req->op.mod.message->dn)) {
551 return ildb_request_noop(ildb, req);
554 msg = new_ldap_message(req);
556 return LDB_ERR_OPERATIONS_ERROR;
559 msg->type = LDAP_TAG_ModifyRequest;
561 msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
562 if (msg->r.ModifyRequest.dn == NULL) {
564 return LDB_ERR_INVALID_DN_SYNTAX;
567 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
570 return LDB_ERR_OPERATIONS_ERROR;
573 msg->r.ModifyRequest.num_mods = n;
574 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
575 if (msg->r.ModifyRequest.mods == NULL) {
577 return LDB_ERR_OPERATIONS_ERROR;
580 for (i = 0; i < n; i++) {
581 msg->r.ModifyRequest.mods[i] = *mods[i];
584 return ildb_request_send(ildb, msg, req);
590 static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
592 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
593 struct ldap_message *msg;
597 /* ignore ltdb specials */
598 if (ldb_dn_is_special(req->op.del.dn)) {
599 return ildb_request_noop(ildb, req);
602 msg = new_ldap_message(req);
604 return LDB_ERR_OPERATIONS_ERROR;
607 msg->type = LDAP_TAG_DelRequest;
609 msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
610 if (msg->r.DelRequest.dn == NULL) {
612 return LDB_ERR_INVALID_DN_SYNTAX;
615 return ildb_request_send(ildb, msg, req);
621 static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
623 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
624 struct ldap_message *msg;
628 /* ignore ltdb specials */
629 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
630 return ildb_request_noop(ildb, req);
633 msg = new_ldap_message(req);
635 return LDB_ERR_OPERATIONS_ERROR;
638 msg->type = LDAP_TAG_ModifyDNRequest;
639 msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
640 if (msg->r.ModifyDNRequest.dn == NULL) {
642 return LDB_ERR_INVALID_DN_SYNTAX;
645 msg->r.ModifyDNRequest.newrdn =
646 talloc_asprintf(msg, "%s=%s",
647 ldb_dn_get_rdn_name(req->op.rename.newdn),
648 ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
649 if (msg->r.ModifyDNRequest.newrdn == NULL) {
651 return LDB_ERR_OPERATIONS_ERROR;
654 msg->r.ModifyDNRequest.newsuperior =
655 ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
656 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
658 return LDB_ERR_INVALID_DN_SYNTAX;
661 msg->r.ModifyDNRequest.deleteolddn = true;
663 return ildb_request_send(ildb, msg, req);
666 static int ildb_start_trans(struct ldb_module *module)
668 /* TODO implement a local locking mechanism here */
673 static int ildb_end_trans(struct ldb_module *module)
675 /* TODO implement a local transaction mechanism here */
680 static int ildb_del_trans(struct ldb_module *module)
682 /* TODO implement a local locking mechanism here */
687 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
689 return LDB_ERR_OPERATIONS_ERROR;
692 static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
694 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
696 if (handle->state == LDB_ASYNC_DONE) {
697 return handle->status;
701 return LDB_ERR_OPERATIONS_ERROR;
704 handle->state = LDB_ASYNC_INIT;
708 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
709 return LDB_ERR_OTHER;
713 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
714 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
715 return LDB_ERR_OTHER;
720 return LDB_ERR_OPERATIONS_ERROR;
723 return handle->status;
726 static const struct ldb_module_ops ildb_ops = {
728 .search = ildb_search,
730 .modify = ildb_modify,
732 .rename = ildb_rename,
733 .request = ildb_request,
734 .start_transaction = ildb_start_trans,
735 .end_transaction = ildb_end_trans,
736 .del_transaction = ildb_del_trans,
741 connect to the database
743 static int ildb_connect(struct ldb_context *ldb, const char *url,
744 unsigned int flags, const char *options[],
745 struct ldb_module **_module)
747 struct ldb_module *module;
748 struct ildb_private *ildb;
750 struct cli_credentials *creds;
751 struct event_context *event_ctx;
753 module = talloc(ldb, struct ldb_module);
758 talloc_set_name_const(module, "ldb_ildap backend");
760 module->prev = module->next = NULL;
761 module->private_data = NULL;
762 module->ops = &ildb_ops;
764 ildb = talloc(module, struct ildb_private);
769 module->private_data = ildb;
770 ildb->module = module;
772 event_ctx = ldb_get_event_context(ldb);
774 ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "loadparm"),
781 if (flags & LDB_FLG_RECONNECT) {
782 ldap_set_reconn_params(ildb->ldap, 10);
785 status = ldap_connect(ildb->ldap, url);
786 if (!NT_STATUS_IS_OK(status)) {
787 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
788 url, ldap_errstr(ildb->ldap, module, status));
792 /* caller can optionally setup credentials using the opaque token 'credentials' */
793 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
795 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
797 creds = session_info->credentials;
801 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
802 const char *bind_dn = cli_credentials_get_bind_dn(creds);
804 const char *password = cli_credentials_get_password(creds);
805 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
806 if (!NT_STATUS_IS_OK(status)) {
807 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
808 ldap_errstr(ildb->ldap, module, status));
812 status = ldap_bind_sasl(ildb->ldap, creds, ldb_get_opaque(ldb, "loadparm"));
813 if (!NT_STATUS_IS_OK(status)) {
814 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
815 ldap_errstr(ildb->ldap, module, status));
829 _PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = {
831 .connect_fn = ildb_connect
834 _PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = {
836 .connect_fn = ildb_connect
839 _PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = {
841 .connect_fn = ildb_connect