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"
52 #include "auth/credentials/credentials.h"
55 struct ldap_connection *ldap;
56 struct ldb_context *ldb;
57 struct ldb_module *module;
61 struct ldb_module *module;
62 struct ildb_private *ildb;
63 struct ldb_handle *handle;
64 struct ldap_request *req;
66 int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
70 convert a ldb_message structure to a list of ldap_mod structures
71 ready for ildap_add() or ildap_modify()
73 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
74 const struct ldb_message *msg, int use_flags)
76 struct ldap_mod **mods;
80 /* allocate maximum number of elements needed */
81 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
88 for (i = 0; i < msg->num_elements; i++) {
89 const struct ldb_message_element *el = &msg->elements[i];
91 mods[n] = talloc(mods, struct ldap_mod);
97 mods[n]->attrib = *el;
99 switch (el->flags & LDB_FLAG_MOD_MASK) {
100 case LDB_FLAG_MOD_ADD:
101 mods[n]->type = LDAP_MODIFY_ADD;
103 case LDB_FLAG_MOD_DELETE:
104 mods[n]->type = LDAP_MODIFY_DELETE;
106 case LDB_FLAG_MOD_REPLACE:
107 mods[n]->type = LDAP_MODIFY_REPLACE;
124 map an ildap NTSTATUS to a ldb error code
126 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
128 if (NT_STATUS_IS_OK(status)) {
131 ldb_set_errstring(ildb->ldb, ldap_errstr(ildb->ldap, status));
132 if (NT_STATUS_IS_LDAP(status)) {
133 return NT_STATUS_LDAP_CODE(status);
135 return LDB_ERR_OPERATIONS_ERROR;
138 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
139 struct timeval t, void *private_data)
141 struct ldb_handle *handle = talloc_get_type(private_data, struct ldb_handle);
142 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
144 if (ac->req->state == LDAP_REQUEST_PENDING) {
145 DLIST_REMOVE(ac->req->conn->pending, ac->req);
148 handle->status = LDB_ERR_TIME_LIMIT_EXCEEDED;
153 static void ildb_callback(struct ldap_request *req)
155 struct ldb_handle *handle = talloc_get_type(req->async.private_data, struct ldb_handle);
156 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
157 struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private);
161 handle->status = LDB_SUCCESS;
163 if (!NT_STATUS_IS_OK(req->status)) {
164 handle->status = ildb_map_error(ildb, req->status);
168 if (req->num_replies < 1) {
169 handle->status = LDB_ERR_OPERATIONS_ERROR;
175 case LDAP_TAG_ModifyRequest:
176 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
177 handle->status = LDB_ERR_PROTOCOL_ERROR;
180 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
181 handle->status = ildb_map_error(ildb, status);
182 if (ac->callback && handle->status == LDB_SUCCESS) {
183 /* FIXME: build a corresponding ares to pass on */
184 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
186 handle->state = LDB_ASYNC_DONE;
189 case LDAP_TAG_AddRequest:
190 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
191 handle->status = LDB_ERR_PROTOCOL_ERROR;
194 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
195 handle->status = ildb_map_error(ildb, status);
196 if (ac->callback && handle->status == LDB_SUCCESS) {
197 /* FIXME: build a corresponding ares to pass on */
198 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
200 handle->state = LDB_ASYNC_DONE;
203 case LDAP_TAG_DelRequest:
204 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
205 handle->status = LDB_ERR_PROTOCOL_ERROR;
208 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
209 handle->status = ildb_map_error(ildb, status);
210 if (ac->callback && handle->status == LDB_SUCCESS) {
211 /* FIXME: build a corresponding ares to pass on */
212 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
214 handle->state = LDB_ASYNC_DONE;
217 case LDAP_TAG_ModifyDNRequest:
218 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
219 handle->status = LDB_ERR_PROTOCOL_ERROR;
222 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
223 handle->status = ildb_map_error(ildb, status);
224 if (ac->callback && handle->status == LDB_SUCCESS) {
225 /* FIXME: build a corresponding ares to pass on */
226 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
228 handle->state = LDB_ASYNC_DONE;
231 case LDAP_TAG_SearchRequest:
232 /* loop over all messages */
233 for (i = 0; i < req->num_replies; i++) {
234 struct ldap_SearchResEntry *search;
235 struct ldb_reply *ares = NULL;
236 struct ldap_message *msg;
239 ares = talloc_zero(ac, struct ldb_reply);
241 handle->status = LDB_ERR_OPERATIONS_ERROR;
245 msg = req->replies[i];
248 case LDAP_TAG_SearchResultDone:
250 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
251 if (!NT_STATUS_IS_OK(status)) {
252 handle->status = ildb_map_error(ildb, status);
256 ares->controls = talloc_move(ares, &msg->controls);
257 if (msg->r.SearchResultDone.resultcode) {
258 if (msg->r.SearchResultDone.errormessage) {
259 ldb_set_errstring(ac->module->ldb, msg->r.SearchResultDone.errormessage);
263 handle->status = msg->r.SearchResultDone.resultcode;
264 handle->state = LDB_ASYNC_DONE;
265 ares->type = LDB_REPLY_DONE;
268 case LDAP_TAG_SearchResultEntry:
271 ares->message = ldb_msg_new(ares);
272 if (!ares->message) {
273 handle->status = LDB_ERR_OPERATIONS_ERROR;
277 search = &(msg->r.SearchResultEntry);
279 ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, search->dn);
280 if ( ! ldb_dn_validate(ares->message->dn)) {
281 handle->status = LDB_ERR_OPERATIONS_ERROR;
284 ares->message->num_elements = search->num_attributes;
285 ares->message->elements = talloc_move(ares->message,
286 &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 struct ildb_context *init_ildb_handle(struct ildb_private *ildb,
327 struct ldb_request *req)
329 struct ildb_context *ildb_ac;
330 struct ldb_handle *h;
332 h = talloc_zero(ildb->ldap, struct ldb_handle);
334 ldb_set_errstring(ildb->module->ldb, "Out of Memory");
338 h->module = ildb->module;
340 ildb_ac = talloc(h, struct ildb_context);
341 if (ildb_ac == NULL) {
342 ldb_set_errstring(ildb->module->ldb, "Out of Memory");
347 h->private_data = ildb_ac;
349 h->state = LDB_ASYNC_INIT;
350 h->status = LDB_SUCCESS;
352 ildb_ac->module = ildb->module;
353 ildb_ac->ildb = ildb;
355 ildb_ac->context = req->context;
356 ildb_ac->callback = req->callback;
362 static int ildb_request_send(struct ildb_private *ildb, struct ldap_message *msg, struct ldb_request *r)
364 struct ildb_context *ildb_ac = init_ildb_handle(ildb, r);
365 struct ldap_request *req;
368 return LDB_ERR_OPERATIONS_ERROR;
371 req = ldap_request_send(ildb->ldap, msg);
373 ldb_set_errstring(ildb->module->ldb, "async send request failed");
374 return LDB_ERR_OPERATIONS_ERROR;
378 ldb_set_errstring(ildb->module->ldb, "connection to remote LDAP server dropped?");
379 return LDB_ERR_OPERATIONS_ERROR;
382 talloc_free(req->time_event);
383 req->time_event = NULL;
385 req->time_event = event_add_timed(req->conn->event.event_ctx, ildb_ac->handle,
386 timeval_current_ofs(r->timeout, 0),
387 ildb_request_timeout, ildb_ac->handle);
390 req->async.fn = ildb_callback;
391 req->async.private_data = ildb_ac->handle;
392 ildb_ac->req = talloc_move(ildb_ac, &req);
397 static int ildb_request_noop(struct ildb_private *ildb, struct ldb_request *req)
399 struct ildb_context *ildb_ac = init_ildb_handle(ildb, req);
400 int ret = LDB_SUCCESS;
403 return LDB_ERR_OPERATIONS_ERROR;
406 if (ildb_ac->callback) {
407 ret = ildb_ac->callback(ildb->module->ldb, ildb_ac->context, NULL);
409 ildb_ac->handle->state = LDB_ASYNC_DONE;
414 search for matching records using an asynchronous function
416 static int ildb_search(struct ldb_module *module, struct ldb_request *req)
418 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
419 struct ldap_message *msg;
424 if (!req->callback || !req->context) {
425 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
426 return LDB_ERR_OPERATIONS_ERROR;
429 if (req->op.search.tree == NULL) {
430 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
431 return LDB_ERR_OPERATIONS_ERROR;
434 msg = new_ldap_message(ildb);
436 ldb_set_errstring(module->ldb, "Out of Memory");
437 return LDB_ERR_OPERATIONS_ERROR;
440 msg->type = LDAP_TAG_SearchRequest;
442 if (req->op.search.base == NULL) {
443 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
445 msg->r.SearchRequest.basedn = ldb_dn_alloc_linearized(msg, req->op.search.base);
447 if (msg->r.SearchRequest.basedn == NULL) {
448 ldb_set_errstring(module->ldb, "Unable to determine baseDN");
450 return LDB_ERR_OPERATIONS_ERROR;
453 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
454 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
456 msg->r.SearchRequest.scope = req->op.search.scope;
459 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
460 msg->r.SearchRequest.timelimit = 0;
461 msg->r.SearchRequest.sizelimit = 0;
462 msg->r.SearchRequest.attributesonly = 0;
463 msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
465 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
466 msg->r.SearchRequest.num_attributes = n;
467 msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
468 msg->controls = req->controls;
470 return ildb_request_send(ildb, msg, req);
476 static int ildb_add(struct ldb_module *module, struct ldb_request *req)
478 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
479 struct ldap_message *msg;
480 struct ldap_mod **mods;
485 /* ignore ltdb specials */
486 if (ldb_dn_is_special(req->op.add.message->dn)) {
487 return ildb_request_noop(ildb, req);
490 msg = new_ldap_message(ildb->ldap);
492 return LDB_ERR_OPERATIONS_ERROR;
495 msg->type = LDAP_TAG_AddRequest;
497 msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
498 if (msg->r.AddRequest.dn == NULL) {
500 return LDB_ERR_INVALID_DN_SYNTAX;
503 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
506 return LDB_ERR_OPERATIONS_ERROR;
509 msg->r.AddRequest.num_attributes = n;
510 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
511 if (msg->r.AddRequest.attributes == NULL) {
513 return LDB_ERR_OPERATIONS_ERROR;
516 for (i = 0; i < n; i++) {
517 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
520 return ildb_request_send(ildb, msg, req);
526 static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
528 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
529 struct ldap_message *msg;
530 struct ldap_mod **mods;
535 /* ignore ltdb specials */
536 if (ldb_dn_is_special(req->op.mod.message->dn)) {
537 return ildb_request_noop(ildb, req);
540 msg = new_ldap_message(ildb->ldap);
542 return LDB_ERR_OPERATIONS_ERROR;
545 msg->type = LDAP_TAG_ModifyRequest;
547 msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
548 if (msg->r.ModifyRequest.dn == NULL) {
550 return LDB_ERR_INVALID_DN_SYNTAX;
553 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
556 return LDB_ERR_OPERATIONS_ERROR;
559 msg->r.ModifyRequest.num_mods = n;
560 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
561 if (msg->r.ModifyRequest.mods == NULL) {
563 return LDB_ERR_OPERATIONS_ERROR;
566 for (i = 0; i < n; i++) {
567 msg->r.ModifyRequest.mods[i] = *mods[i];
570 return ildb_request_send(ildb, msg, req);
576 static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
578 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
579 struct ldap_message *msg;
583 /* ignore ltdb specials */
584 if (ldb_dn_is_special(req->op.del.dn)) {
585 return ildb_request_noop(ildb, req);
588 msg = new_ldap_message(ildb->ldap);
590 return LDB_ERR_OPERATIONS_ERROR;
593 msg->type = LDAP_TAG_DelRequest;
595 msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
596 if (msg->r.DelRequest.dn == NULL) {
598 return LDB_ERR_INVALID_DN_SYNTAX;
601 return ildb_request_send(ildb, msg, req);
607 static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
609 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
610 struct ldap_message *msg;
614 /* ignore ltdb specials */
615 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
616 return ildb_request_noop(ildb, req);
619 msg = new_ldap_message(ildb->ldap);
621 return LDB_ERR_OPERATIONS_ERROR;
624 msg->type = LDAP_TAG_ModifyDNRequest;
625 msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
626 if (msg->r.ModifyDNRequest.dn == NULL) {
628 return LDB_ERR_INVALID_DN_SYNTAX;
631 msg->r.ModifyDNRequest.newrdn =
632 talloc_asprintf(msg, "%s=%s",
633 ldb_dn_get_rdn_name(req->op.rename.newdn),
634 ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
635 if (msg->r.ModifyDNRequest.newrdn == NULL) {
637 return LDB_ERR_OPERATIONS_ERROR;
640 msg->r.ModifyDNRequest.newsuperior =
641 ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
642 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
644 return LDB_ERR_INVALID_DN_SYNTAX;
647 msg->r.ModifyDNRequest.deleteolddn = True;
649 return ildb_request_send(ildb, msg, req);
652 static int ildb_start_trans(struct ldb_module *module)
654 /* TODO implement a local locking mechanism here */
659 static int ildb_end_trans(struct ldb_module *module)
661 /* TODO implement a local transaction mechanism here */
666 static int ildb_del_trans(struct ldb_module *module)
668 /* TODO implement a local locking mechanism here */
673 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
675 return LDB_ERR_OPERATIONS_ERROR;
678 static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
680 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
682 if (handle->state == LDB_ASYNC_DONE) {
683 return handle->status;
687 return LDB_ERR_OPERATIONS_ERROR;
690 handle->state = LDB_ASYNC_INIT;
694 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
695 return LDB_ERR_OTHER;
699 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
700 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
701 return LDB_ERR_OTHER;
706 return LDB_ERR_OPERATIONS_ERROR;
709 return handle->status;
712 static const struct ldb_module_ops ildb_ops = {
714 .search = ildb_search,
716 .modify = ildb_modify,
718 .rename = ildb_rename,
719 .request = ildb_request,
720 .start_transaction = ildb_start_trans,
721 .end_transaction = ildb_end_trans,
722 .del_transaction = ildb_del_trans,
727 connect to the database
729 static int ildb_connect(struct ldb_context *ldb, const char *url,
730 unsigned int flags, const char *options[],
731 struct ldb_module **_module)
733 struct ldb_module *module;
734 struct ildb_private *ildb;
736 struct cli_credentials *creds;
738 module = talloc(ldb, struct ldb_module);
743 talloc_set_name_const(module, "ldb_ildap backend");
745 module->prev = module->next = NULL;
746 module->private_data = NULL;
747 module->ops = &ildb_ops;
749 ildb = talloc(module, struct ildb_private);
754 module->private_data = ildb;
756 ildb->module = module;
757 ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
763 if (flags & LDB_FLG_RECONNECT) {
764 ldap_set_reconn_params(ildb->ldap, 10);
767 status = ldap_connect(ildb->ldap, url);
768 if (!NT_STATUS_IS_OK(status)) {
769 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
770 url, ldap_errstr(ildb->ldap, status));
774 /* caller can optionally setup credentials using the opaque token 'credentials' */
775 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
777 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
779 creds = session_info->credentials;
783 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
784 const char *bind_dn = cli_credentials_get_bind_dn(creds);
786 const char *password = cli_credentials_get_password(creds);
787 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
788 if (!NT_STATUS_IS_OK(status)) {
789 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
790 ldap_errstr(ildb->ldap, status));
794 status = ldap_bind_sasl(ildb->ldap, creds);
795 if (!NT_STATUS_IS_OK(status)) {
796 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
797 ldap_errstr(ildb->ldap, status));
811 int ldb_ildap_init(void)
813 return ldb_register_backend("ldap", ildb_connect) +
814 ldb_register_backend("ldapi", ildb_connect) +
815 ldb_register_backend("ldaps", ildb_connect);