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
44 #include "ldb_module.h"
45 #include "dlinklist.h"
47 #include "libcli/ldap/ldap.h"
48 #include "libcli/ldap/ldap_client.h"
49 #include "auth/auth.h"
50 #include "auth/credentials/credentials.h"
53 struct ldap_connection *ldap;
54 struct tevent_context *event_ctx;
58 struct ldb_module *module;
59 struct ldb_request *req;
61 struct ildb_private *ildb;
62 struct ldap_request *ireq;
66 struct ildb_destructor_ctx *dc;
69 static void ildb_request_done(struct ildb_context *ctx,
70 struct ldb_control **ctrls, int error)
72 struct ldb_context *ldb;
73 struct ldb_reply *ares;
75 ldb = ldb_module_get_ctx(ctx->module);
79 if (ctx->req == NULL) {
80 /* if the req has been freed already just return */
84 ares = talloc_zero(ctx->req, struct ldb_reply);
87 ctx->req->callback(ctx->req, NULL);
90 ares->type = LDB_REPLY_DONE;
91 ares->controls = talloc_steal(ares, ctrls);
94 ctx->req->callback(ctx->req, ares);
97 static void ildb_auto_done_callback(struct tevent_context *ev,
98 struct tevent_timer *te,
102 struct ildb_context *ac;
104 ac = talloc_get_type(private_data, struct ildb_context);
105 ildb_request_done(ac, NULL, LDB_SUCCESS);
109 convert a ldb_message structure to a list of ldap_mod structures
110 ready for ildap_add() or ildap_modify()
112 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
113 const struct ldb_message *msg,
116 struct ldap_mod **mods;
120 /* allocate maximum number of elements needed */
121 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
128 for (i = 0; i < msg->num_elements; i++) {
129 const struct ldb_message_element *el = &msg->elements[i];
131 mods[n] = talloc(mods, struct ldap_mod);
137 mods[n]->attrib = *el;
139 switch (el->flags & LDB_FLAG_MOD_MASK) {
140 case LDB_FLAG_MOD_ADD:
141 mods[n]->type = LDAP_MODIFY_ADD;
143 case LDB_FLAG_MOD_DELETE:
144 mods[n]->type = LDAP_MODIFY_DELETE;
146 case LDB_FLAG_MOD_REPLACE:
147 mods[n]->type = LDAP_MODIFY_REPLACE;
164 map an ildap NTSTATUS to a ldb error code
166 static int ildb_map_error(struct ldb_module *module, NTSTATUS status)
168 struct ildb_private *ildb;
169 struct ldb_context *ldb;
172 ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private);
173 ldb = ldb_module_get_ctx(module);
175 if (NT_STATUS_IS_OK(status)) {
179 mem_ctx = talloc_new(ildb);
182 return LDB_ERR_OPERATIONS_ERROR;
184 ldb_set_errstring(ldb,
185 ldap_errstr(ildb->ldap, mem_ctx, status));
186 talloc_free(mem_ctx);
187 if (NT_STATUS_IS_LDAP(status)) {
188 return NT_STATUS_LDAP_CODE(status);
190 return LDB_ERR_OPERATIONS_ERROR;
193 static void ildb_request_timeout(struct tevent_context *ev, struct tevent_timer *te,
194 struct timeval t, void *private_data)
196 struct ildb_context *ac = talloc_get_type(private_data, struct ildb_context);
198 if (ac->ireq->state == LDAP_REQUEST_PENDING) {
199 DLIST_REMOVE(ac->ireq->conn->pending, ac->ireq);
202 ildb_request_done(ac, NULL, LDB_ERR_TIME_LIMIT_EXCEEDED);
205 static void ildb_callback(struct ldap_request *req)
207 struct ldb_context *ldb;
208 struct ildb_context *ac;
210 struct ldap_SearchResEntry *search;
211 struct ldap_message *msg;
212 struct ldb_control **controls;
213 struct ldb_message *ldbmsg;
215 bool callback_failed;
220 ac = talloc_get_type(req->async.private_data, struct ildb_context);
221 ldb = ldb_module_get_ctx(ac->module);
222 callback_failed = false;
223 request_done = false;
226 if (!NT_STATUS_IS_OK(req->status)) {
227 ret = ildb_map_error(ac->module, req->status);
228 ildb_request_done(ac, NULL, ret);
232 if (req->num_replies < 1) {
233 ret = LDB_ERR_OPERATIONS_ERROR;
234 ildb_request_done(ac, NULL, ret);
240 case LDAP_TAG_ModifyRequest:
241 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
242 ret = LDB_ERR_PROTOCOL_ERROR;
245 status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
246 ret = ildb_map_error(ac->module, status);
250 case LDAP_TAG_AddRequest:
251 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
252 ret = LDB_ERR_PROTOCOL_ERROR;
255 status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
256 ret = ildb_map_error(ac->module, status);
260 case LDAP_TAG_DelRequest:
261 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
262 ret = LDB_ERR_PROTOCOL_ERROR;
265 status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
266 ret = ildb_map_error(ac->module, status);
270 case LDAP_TAG_ModifyDNRequest:
271 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
272 ret = LDB_ERR_PROTOCOL_ERROR;
275 status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
276 ret = ildb_map_error(ac->module, status);
280 case LDAP_TAG_SearchRequest:
281 /* check if we are already processing this request */
282 if (req->in_dispatch_replies) {
286 req->in_dispatch_replies = true;
288 /* loop over all messages */
289 for (i = 0; i < req->num_replies; i++) {
291 msg = req->replies[i];
294 case LDAP_TAG_SearchResultDone:
296 status = ldap_check_response(ac->ireq->conn, &msg->r.GeneralResult);
297 if (!NT_STATUS_IS_OK(status)) {
298 ret = ildb_map_error(ac->module, status);
302 controls = talloc_steal(ac, msg->controls);
303 if (msg->r.SearchResultDone.resultcode) {
304 if (msg->r.SearchResultDone.errormessage) {
305 ldb_set_errstring(ldb, msg->r.SearchResultDone.errormessage);
309 ret = msg->r.SearchResultDone.resultcode;
313 case LDAP_TAG_SearchResultEntry:
315 ldbmsg = ldb_msg_new(ac);
317 ret = LDB_ERR_OPERATIONS_ERROR;
321 search = &(msg->r.SearchResultEntry);
323 ldbmsg->dn = ldb_dn_new(ldbmsg, ldb, search->dn);
324 if ( ! ldb_dn_validate(ldbmsg->dn)) {
325 ret = LDB_ERR_OPERATIONS_ERROR;
328 ldbmsg->num_elements = search->num_attributes;
329 ldbmsg->elements = talloc_move(ldbmsg, &search->attributes);
331 controls = talloc_steal(ac, msg->controls);
333 ret = ldb_module_send_entry(ac->req, ldbmsg, controls);
334 if (ret != LDB_SUCCESS) {
335 callback_failed = true;
340 case LDAP_TAG_SearchResultReference:
342 referral = talloc_strdup(ac, msg->r.SearchResultReference.referral);
344 ret = ldb_module_send_referral(ac->req, referral);
345 if (ret != LDB_SUCCESS) {
346 callback_failed = true;
352 /* TAG not handled, fail ! */
353 ret = LDB_ERR_PROTOCOL_ERROR;
357 if (ret != LDB_SUCCESS) {
362 req->in_dispatch_replies = false;
364 talloc_free(req->replies);
366 req->num_replies = 0;
371 ret = LDB_ERR_PROTOCOL_ERROR;
375 if (ret != LDB_SUCCESS) {
377 /* if the callback failed the caller will have freed the
378 * request. Just return and don't try to use it */
379 if ( ! callback_failed) {
385 ildb_request_done(ac, controls, ret);
390 static int ildb_request_send(struct ildb_context *ac, struct ldap_message *msg)
392 struct ldb_context *ldb;
393 struct ldap_request *req;
396 return LDB_ERR_OPERATIONS_ERROR;
399 ldb = ldb_module_get_ctx(ac->module);
401 req = ldap_request_send(ac->ildb->ldap, msg);
403 ldb_set_errstring(ldb, "async send request failed");
404 return LDB_ERR_OPERATIONS_ERROR;
406 ac->ireq = talloc_steal(ac, req);
408 if (!ac->ireq->conn) {
409 ldb_set_errstring(ldb, "connection to remote LDAP server dropped?");
410 return LDB_ERR_OPERATIONS_ERROR;
413 talloc_free(req->time_event);
414 req->time_event = NULL;
415 if (ac->req->timeout) {
416 req->time_event = tevent_add_timer(ac->ildb->event_ctx, ac,
417 timeval_current_ofs(ac->req->timeout, 0),
418 ildb_request_timeout, ac);
421 req->async.fn = ildb_callback;
422 req->async.private_data = ac;
428 search for matching records using an asynchronous function
430 static int ildb_search(struct ildb_context *ac)
432 struct ldb_context *ldb;
433 struct ldb_request *req = ac->req;
434 struct ldap_message *msg;
437 ldb = ldb_module_get_ctx(ac->module);
439 if (!req->callback || !req->context) {
440 ldb_set_errstring(ldb, "Async interface called with NULL callback function or NULL context");
441 return LDB_ERR_OPERATIONS_ERROR;
444 if (req->op.search.tree == NULL) {
445 ldb_set_errstring(ldb, "Invalid expression parse tree");
446 return LDB_ERR_OPERATIONS_ERROR;
449 msg = new_ldap_message(req);
451 ldb_set_errstring(ldb, "Out of Memory");
452 return LDB_ERR_OPERATIONS_ERROR;
455 msg->type = LDAP_TAG_SearchRequest;
457 if (req->op.search.base == NULL) {
458 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
460 msg->r.SearchRequest.basedn = ldb_dn_get_extended_linearized(msg, req->op.search.base, 0);
462 if (msg->r.SearchRequest.basedn == NULL) {
463 ldb_set_errstring(ldb, "Unable to determine baseDN");
465 return LDB_ERR_OPERATIONS_ERROR;
468 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
469 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
471 msg->r.SearchRequest.scope = req->op.search.scope;
474 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
475 msg->r.SearchRequest.timelimit = 0;
476 msg->r.SearchRequest.sizelimit = 0;
477 msg->r.SearchRequest.attributesonly = 0;
478 msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
480 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
481 msg->r.SearchRequest.num_attributes = n;
482 msg->r.SearchRequest.attributes = req->op.search.attrs;
483 msg->controls = req->controls;
485 return ildb_request_send(ac, msg);
491 static int ildb_add(struct ildb_context *ac)
493 struct ldb_request *req = ac->req;
494 struct ldap_message *msg;
495 struct ldap_mod **mods;
498 msg = new_ldap_message(req);
500 return LDB_ERR_OPERATIONS_ERROR;
503 msg->type = LDAP_TAG_AddRequest;
505 msg->r.AddRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.add.message->dn, 0);
506 if (msg->r.AddRequest.dn == NULL) {
508 return LDB_ERR_INVALID_DN_SYNTAX;
511 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
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) {
521 return LDB_ERR_OPERATIONS_ERROR;
524 for (i = 0; i < n; i++) {
525 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
527 msg->controls = req->controls;
529 return ildb_request_send(ac, msg);
535 static int ildb_modify(struct ildb_context *ac)
537 struct ldb_request *req = ac->req;
538 struct ldap_message *msg;
539 struct ldap_mod **mods;
542 msg = new_ldap_message(req);
544 return LDB_ERR_OPERATIONS_ERROR;
547 msg->type = LDAP_TAG_ModifyRequest;
549 msg->r.ModifyRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.mod.message->dn, 0);
550 if (msg->r.ModifyRequest.dn == NULL) {
552 return LDB_ERR_INVALID_DN_SYNTAX;
555 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
558 return LDB_ERR_OPERATIONS_ERROR;
561 msg->r.ModifyRequest.num_mods = n;
562 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
563 if (msg->r.ModifyRequest.mods == NULL) {
565 return LDB_ERR_OPERATIONS_ERROR;
568 for (i = 0; i < n; i++) {
569 msg->r.ModifyRequest.mods[i] = *mods[i];
571 msg->controls = req->controls;
572 return ildb_request_send(ac, msg);
578 static int ildb_delete(struct ildb_context *ac)
580 struct ldb_request *req = ac->req;
581 struct ldap_message *msg;
583 msg = new_ldap_message(req);
585 return LDB_ERR_OPERATIONS_ERROR;
588 msg->type = LDAP_TAG_DelRequest;
590 msg->r.DelRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.del.dn, 0);
591 if (msg->r.DelRequest.dn == NULL) {
593 return LDB_ERR_INVALID_DN_SYNTAX;
595 msg->controls = req->controls;
597 return ildb_request_send(ac, msg);
603 static int ildb_rename(struct ildb_context *ac)
605 struct ldb_request *req = ac->req;
606 struct ldap_message *msg;
607 const char *rdn_name;
608 const struct ldb_val *rdn_val;
610 msg = new_ldap_message(req);
612 return LDB_ERR_OPERATIONS_ERROR;
615 msg->type = LDAP_TAG_ModifyDNRequest;
616 msg->r.ModifyDNRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.rename.olddn, 0);
617 if (msg->r.ModifyDNRequest.dn == NULL) {
619 return LDB_ERR_INVALID_DN_SYNTAX;
622 rdn_name = ldb_dn_get_rdn_name(req->op.rename.newdn);
623 rdn_val = ldb_dn_get_rdn_val(req->op.rename.newdn);
625 if ((rdn_name != NULL) && (rdn_val != NULL)) {
626 msg->r.ModifyDNRequest.newrdn =
627 talloc_asprintf(msg, "%s=%s", rdn_name,
628 ldb_dn_escape_value(msg, *rdn_val));
630 msg->r.ModifyDNRequest.newrdn = talloc_strdup(msg, "");
632 if (msg->r.ModifyDNRequest.newrdn == NULL) {
634 return LDB_ERR_OPERATIONS_ERROR;
637 msg->r.ModifyDNRequest.newsuperior =
638 ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
639 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
641 return LDB_ERR_INVALID_DN_SYNTAX;
644 msg->r.ModifyDNRequest.deleteolddn = true;
645 msg->controls = req->controls;
647 return ildb_request_send(ac, msg);
650 static int ildb_start_trans(struct ldb_module *module)
652 /* TODO implement a local locking mechanism here */
657 static int ildb_end_trans(struct ldb_module *module)
659 /* TODO implement a local transaction mechanism here */
664 static int ildb_del_trans(struct ldb_module *module)
666 /* TODO implement a local locking mechanism here */
671 static bool ildb_dn_is_special(struct ldb_request *req)
673 struct ldb_dn *dn = NULL;
675 switch (req->operation) {
677 dn = req->op.add.message->dn;
680 dn = req->op.mod.message->dn;
686 dn = req->op.rename.olddn;
692 if (dn && ldb_dn_is_special(dn)) {
698 static int ildb_handle_request(struct ldb_module *module, struct ldb_request *req)
700 struct ldb_context *ldb;
701 struct ildb_private *ildb;
702 struct ildb_context *ac;
703 struct tevent_timer *te;
706 ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private);
707 ldb = ldb_module_get_ctx(module);
709 if (req->starttime == 0 || req->timeout == 0) {
710 ldb_set_errstring(ldb, "Invalid timeout settings");
711 return LDB_ERR_TIME_LIMIT_EXCEEDED;
714 ac = talloc_zero(req, struct ildb_context);
716 ldb_set_errstring(ldb, "Out of Memory");
717 return LDB_ERR_OPERATIONS_ERROR;
724 if (ildb_dn_is_special(req)) {
726 te = tevent_add_timer(ac->ildb->event_ctx,
728 ildb_auto_done_callback, ac);
730 return LDB_ERR_OPERATIONS_ERROR;
736 switch (ac->req->operation) {
738 ret = ildb_search(ac);
744 ret = ildb_modify(ac);
747 ret = ildb_delete(ac);
750 ret = ildb_rename(ac);
753 /* no other op supported */
754 ret = LDB_ERR_OPERATIONS_ERROR;
761 static const struct ldb_module_ops ildb_ops = {
763 .search = ildb_handle_request,
764 .add = ildb_handle_request,
765 .modify = ildb_handle_request,
766 .del = ildb_handle_request,
767 .rename = ildb_handle_request,
768 /* .request = ildb_handle_request, */
769 .start_transaction = ildb_start_trans,
770 .end_transaction = ildb_end_trans,
771 .del_transaction = ildb_del_trans,
775 connect to the database
777 static int ildb_connect(struct ldb_context *ldb, const char *url,
778 unsigned int flags, const char *options[],
779 struct ldb_module **_module)
781 struct ldb_module *module;
782 struct ildb_private *ildb;
784 struct cli_credentials *creds;
785 struct loadparm_context *lp_ctx;
787 module = ldb_module_new(ldb, ldb, "ldb_ildap backend", &ildb_ops);
788 if (!module) return LDB_ERR_OPERATIONS_ERROR;
790 ildb = talloc(module, struct ildb_private);
795 ldb_module_set_private(module, ildb);
797 ildb->event_ctx = ldb_get_event_context(ldb);
799 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
800 struct loadparm_context);
802 ildb->ldap = ldap4_new_connection(ildb, lp_ctx,
809 if (flags & LDB_FLG_RECONNECT) {
810 ldap_set_reconn_params(ildb->ldap, 10);
813 status = ldap_connect(ildb->ldap, url);
814 if (!NT_STATUS_IS_OK(status)) {
815 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s",
816 url, ldap_errstr(ildb->ldap, module, status));
820 /* caller can optionally setup credentials using the opaque token 'credentials' */
821 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
823 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
825 creds = session_info->credentials;
829 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
830 const char *bind_dn = cli_credentials_get_bind_dn(creds);
832 const char *password = cli_credentials_get_password(creds);
833 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
834 if (!NT_STATUS_IS_OK(status)) {
835 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
836 ldap_errstr(ildb->ldap, module, status));
840 status = ldap_bind_sasl(ildb->ldap, creds, lp_ctx);
841 if (!NT_STATUS_IS_OK(status)) {
842 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
843 ldap_errstr(ildb->ldap, module, status));
854 return LDB_ERR_OPERATIONS_ERROR;
857 _PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = {
859 .connect_fn = ildb_connect
862 _PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = {
864 .connect_fn = ildb_connect
867 _PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = {
869 .connect_fn = ildb_connect