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 /* loop over all messages */
282 for (i = 0; i < req->num_replies; i++) {
284 msg = req->replies[i];
287 case LDAP_TAG_SearchResultDone:
289 status = ldap_check_response(ac->ireq->conn, &msg->r.GeneralResult);
290 if (!NT_STATUS_IS_OK(status)) {
291 ret = ildb_map_error(ac->module, status);
295 controls = talloc_steal(ac, msg->controls);
296 if (msg->r.SearchResultDone.resultcode) {
297 if (msg->r.SearchResultDone.errormessage) {
298 ldb_set_errstring(ldb, msg->r.SearchResultDone.errormessage);
302 ret = msg->r.SearchResultDone.resultcode;
306 case LDAP_TAG_SearchResultEntry:
308 ldbmsg = ldb_msg_new(ac);
310 ret = LDB_ERR_OPERATIONS_ERROR;
314 search = &(msg->r.SearchResultEntry);
316 ldbmsg->dn = ldb_dn_new(ldbmsg, ldb, search->dn);
317 if ( ! ldb_dn_validate(ldbmsg->dn)) {
318 ret = LDB_ERR_OPERATIONS_ERROR;
321 ldbmsg->num_elements = search->num_attributes;
322 ldbmsg->elements = talloc_move(ldbmsg, &search->attributes);
324 controls = talloc_steal(ac, msg->controls);
326 ret = ldb_module_send_entry(ac->req, ldbmsg, controls);
327 if (ret != LDB_SUCCESS) {
328 callback_failed = true;
332 case LDAP_TAG_SearchResultReference:
334 referral = talloc_strdup(ac, msg->r.SearchResultReference.referral);
336 ret = ldb_module_send_referral(ac->req, referral);
337 if (ret != LDB_SUCCESS) {
338 callback_failed = true;
343 /* TAG not handled, fail ! */
344 ret = LDB_ERR_PROTOCOL_ERROR;
348 if (ret != LDB_SUCCESS) {
353 talloc_free(req->replies);
355 req->num_replies = 0;
360 ret = LDB_ERR_PROTOCOL_ERROR;
364 if (ret != LDB_SUCCESS) {
366 /* if the callback failed the caller will have freed the
367 * request. Just return and don't try to use it */
368 if ( ! callback_failed) {
374 ildb_request_done(ac, controls, ret);
379 static int ildb_request_send(struct ildb_context *ac, struct ldap_message *msg)
381 struct ldb_context *ldb;
382 struct ldap_request *req;
385 return LDB_ERR_OPERATIONS_ERROR;
388 ldb = ldb_module_get_ctx(ac->module);
390 req = ldap_request_send(ac->ildb->ldap, msg);
392 ldb_set_errstring(ldb, "async send request failed");
393 return LDB_ERR_OPERATIONS_ERROR;
395 ac->ireq = talloc_steal(ac, req);
397 if (!ac->ireq->conn) {
398 ldb_set_errstring(ldb, "connection to remote LDAP server dropped?");
399 return LDB_ERR_OPERATIONS_ERROR;
402 talloc_free(req->time_event);
403 req->time_event = NULL;
404 if (ac->req->timeout) {
405 req->time_event = tevent_add_timer(ac->ildb->event_ctx, ac,
406 timeval_current_ofs(ac->req->timeout, 0),
407 ildb_request_timeout, ac);
410 req->async.fn = ildb_callback;
411 req->async.private_data = ac;
417 search for matching records using an asynchronous function
419 static int ildb_search(struct ildb_context *ac)
421 struct ldb_context *ldb;
422 struct ldb_request *req = ac->req;
423 struct ldap_message *msg;
426 ldb = ldb_module_get_ctx(ac->module);
428 if (!req->callback || !req->context) {
429 ldb_set_errstring(ldb, "Async interface called with NULL callback function or NULL context");
430 return LDB_ERR_OPERATIONS_ERROR;
433 if (req->op.search.tree == NULL) {
434 ldb_set_errstring(ldb, "Invalid expression parse tree");
435 return LDB_ERR_OPERATIONS_ERROR;
438 msg = new_ldap_message(req);
440 ldb_set_errstring(ldb, "Out of Memory");
441 return LDB_ERR_OPERATIONS_ERROR;
444 msg->type = LDAP_TAG_SearchRequest;
446 if (req->op.search.base == NULL) {
447 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
449 msg->r.SearchRequest.basedn = ldb_dn_get_extended_linearized(msg, req->op.search.base, 0);
451 if (msg->r.SearchRequest.basedn == NULL) {
452 ldb_set_errstring(ldb, "Unable to determine baseDN");
454 return LDB_ERR_OPERATIONS_ERROR;
457 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
458 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
460 msg->r.SearchRequest.scope = req->op.search.scope;
463 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
464 msg->r.SearchRequest.timelimit = 0;
465 msg->r.SearchRequest.sizelimit = 0;
466 msg->r.SearchRequest.attributesonly = 0;
467 msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
469 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
470 msg->r.SearchRequest.num_attributes = n;
471 msg->r.SearchRequest.attributes = req->op.search.attrs;
472 msg->controls = req->controls;
474 return ildb_request_send(ac, msg);
480 static int ildb_add(struct ildb_context *ac)
482 struct ldb_request *req = ac->req;
483 struct ldap_message *msg;
484 struct ldap_mod **mods;
487 msg = new_ldap_message(req);
489 return LDB_ERR_OPERATIONS_ERROR;
492 msg->type = LDAP_TAG_AddRequest;
494 msg->r.AddRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.add.message->dn, 0);
495 if (msg->r.AddRequest.dn == NULL) {
497 return LDB_ERR_INVALID_DN_SYNTAX;
500 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
503 return LDB_ERR_OPERATIONS_ERROR;
506 msg->r.AddRequest.num_attributes = n;
507 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
508 if (msg->r.AddRequest.attributes == NULL) {
510 return LDB_ERR_OPERATIONS_ERROR;
513 for (i = 0; i < n; i++) {
514 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
517 return ildb_request_send(ac, msg);
523 static int ildb_modify(struct ildb_context *ac)
525 struct ldb_request *req = ac->req;
526 struct ldap_message *msg;
527 struct ldap_mod **mods;
530 msg = new_ldap_message(req);
532 return LDB_ERR_OPERATIONS_ERROR;
535 msg->type = LDAP_TAG_ModifyRequest;
537 msg->r.ModifyRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.mod.message->dn, 0);
538 if (msg->r.ModifyRequest.dn == NULL) {
540 return LDB_ERR_INVALID_DN_SYNTAX;
543 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
546 return LDB_ERR_OPERATIONS_ERROR;
549 msg->r.ModifyRequest.num_mods = n;
550 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
551 if (msg->r.ModifyRequest.mods == NULL) {
553 return LDB_ERR_OPERATIONS_ERROR;
556 for (i = 0; i < n; i++) {
557 msg->r.ModifyRequest.mods[i] = *mods[i];
560 return ildb_request_send(ac, msg);
566 static int ildb_delete(struct ildb_context *ac)
568 struct ldb_request *req = ac->req;
569 struct ldap_message *msg;
571 msg = new_ldap_message(req);
573 return LDB_ERR_OPERATIONS_ERROR;
576 msg->type = LDAP_TAG_DelRequest;
578 msg->r.DelRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.del.dn, 0);
579 if (msg->r.DelRequest.dn == NULL) {
581 return LDB_ERR_INVALID_DN_SYNTAX;
584 return ildb_request_send(ac, msg);
590 static int ildb_rename(struct ildb_context *ac)
592 struct ldb_request *req = ac->req;
593 struct ldap_message *msg;
595 msg = new_ldap_message(req);
597 return LDB_ERR_OPERATIONS_ERROR;
600 msg->type = LDAP_TAG_ModifyDNRequest;
601 msg->r.ModifyDNRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.rename.olddn, 0);
602 if (msg->r.ModifyDNRequest.dn == NULL) {
604 return LDB_ERR_INVALID_DN_SYNTAX;
607 msg->r.ModifyDNRequest.newrdn =
608 talloc_asprintf(msg, "%s=%s",
609 ldb_dn_get_rdn_name(req->op.rename.newdn),
610 ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
611 if (msg->r.ModifyDNRequest.newrdn == NULL) {
613 return LDB_ERR_OPERATIONS_ERROR;
616 msg->r.ModifyDNRequest.newsuperior =
617 ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
618 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
620 return LDB_ERR_INVALID_DN_SYNTAX;
623 msg->r.ModifyDNRequest.deleteolddn = true;
625 return ildb_request_send(ac, msg);
628 static int ildb_start_trans(struct ldb_module *module)
630 /* TODO implement a local locking mechanism here */
635 static int ildb_end_trans(struct ldb_module *module)
637 /* TODO implement a local transaction mechanism here */
642 static int ildb_del_trans(struct ldb_module *module)
644 /* TODO implement a local locking mechanism here */
649 static bool ildb_dn_is_special(struct ldb_request *req)
651 struct ldb_dn *dn = NULL;
653 switch (req->operation) {
655 dn = req->op.add.message->dn;
658 dn = req->op.mod.message->dn;
664 dn = req->op.rename.olddn;
670 if (dn && ldb_dn_is_special(dn)) {
676 static int ildb_handle_request(struct ldb_module *module, struct ldb_request *req)
678 struct ldb_context *ldb;
679 struct ildb_private *ildb;
680 struct ildb_context *ac;
681 struct tevent_timer *te;
684 ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private);
685 ldb = ldb_module_get_ctx(module);
687 if (req->starttime == 0 || req->timeout == 0) {
688 ldb_set_errstring(ldb, "Invalid timeout settings");
689 return LDB_ERR_TIME_LIMIT_EXCEEDED;
692 ac = talloc_zero(req, struct ildb_context);
694 ldb_set_errstring(ldb, "Out of Memory");
695 return LDB_ERR_OPERATIONS_ERROR;
702 if (ildb_dn_is_special(req)) {
704 te = tevent_add_timer(ac->ildb->event_ctx,
706 ildb_auto_done_callback, ac);
708 return LDB_ERR_OPERATIONS_ERROR;
714 switch (ac->req->operation) {
716 ret = ildb_search(ac);
722 ret = ildb_modify(ac);
725 ret = ildb_delete(ac);
728 ret = ildb_rename(ac);
731 /* no other op supported */
732 ret = LDB_ERR_OPERATIONS_ERROR;
739 static const struct ldb_module_ops ildb_ops = {
741 .search = ildb_handle_request,
742 .add = ildb_handle_request,
743 .modify = ildb_handle_request,
744 .del = ildb_handle_request,
745 .rename = ildb_handle_request,
746 /* .request = ildb_handle_request, */
747 .start_transaction = ildb_start_trans,
748 .end_transaction = ildb_end_trans,
749 .del_transaction = ildb_del_trans,
753 connect to the database
755 static int ildb_connect(struct ldb_context *ldb, const char *url,
756 unsigned int flags, const char *options[],
757 struct ldb_module **_module)
759 struct ldb_module *module;
760 struct ildb_private *ildb;
762 struct cli_credentials *creds;
763 struct loadparm_context *lp_ctx;
765 module = ldb_module_new(ldb, ldb, "ldb_ildap backend", &ildb_ops);
766 if (!module) return -1;
768 ildb = talloc(module, struct ildb_private);
773 ldb_module_set_private(module, ildb);
775 ildb->event_ctx = ldb_get_event_context(ldb);
777 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
778 struct loadparm_context);
780 ildb->ldap = ldap4_new_connection(ildb, lp_ctx,
787 if (flags & LDB_FLG_RECONNECT) {
788 ldap_set_reconn_params(ildb->ldap, 10);
791 status = ldap_connect(ildb->ldap, url);
792 if (!NT_STATUS_IS_OK(status)) {
793 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s",
794 url, ldap_errstr(ildb->ldap, module, status));
798 /* caller can optionally setup credentials using the opaque token 'credentials' */
799 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
801 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
803 creds = session_info->credentials;
807 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
808 const char *bind_dn = cli_credentials_get_bind_dn(creds);
810 const char *password = cli_credentials_get_password(creds);
811 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
812 if (!NT_STATUS_IS_OK(status)) {
813 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
814 ldap_errstr(ildb->ldap, module, status));
818 status = ldap_bind_sasl(ildb->ldap, creds, lp_ctx);
819 if (!NT_STATUS_IS_OK(status)) {
820 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
821 ldap_errstr(ildb->ldap, module, status));
835 _PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = {
837 .connect_fn = ildb_connect
840 _PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = {
842 .connect_fn = ildb_connect
845 _PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = {
847 .connect_fn = ildb_connect