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"
54 struct ldap_connection *ldap;
55 struct event_context *event_ctx;
59 struct ldb_module *module;
60 struct ldb_request *req;
62 struct ildb_private *ildb;
63 struct ldap_request *ireq;
67 struct ildb_destructor_ctx *dc;
70 static void ildb_request_done(struct ildb_context *ctx,
71 struct ldb_control **ctrls, int error)
73 struct ldb_reply *ares;
77 if (ctx->req == NULL) {
78 /* if the req has been freed already just return */
82 ares = talloc_zero(ctx->req, struct ldb_reply);
84 ldb_oom(ctx->req->handle->ldb);
85 ctx->req->callback(ctx->req, NULL);
88 ares->type = LDB_REPLY_DONE;
89 ares->controls = talloc_steal(ares, ctrls);
92 ctx->req->callback(ctx->req, ares);
95 static void ildb_auto_done_callback(struct event_context *ev,
96 struct timed_event *te,
100 struct ildb_context *ac;
102 ac = talloc_get_type(private_data, struct ildb_context);
103 ildb_request_done(ac, NULL, LDB_SUCCESS);
107 convert a ldb_message structure to a list of ldap_mod structures
108 ready for ildap_add() or ildap_modify()
110 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
111 const struct ldb_message *msg,
114 struct ldap_mod **mods;
118 /* allocate maximum number of elements needed */
119 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
126 for (i = 0; i < msg->num_elements; i++) {
127 const struct ldb_message_element *el = &msg->elements[i];
129 mods[n] = talloc(mods, struct ldap_mod);
135 mods[n]->attrib = *el;
137 switch (el->flags & LDB_FLAG_MOD_MASK) {
138 case LDB_FLAG_MOD_ADD:
139 mods[n]->type = LDAP_MODIFY_ADD;
141 case LDB_FLAG_MOD_DELETE:
142 mods[n]->type = LDAP_MODIFY_DELETE;
144 case LDB_FLAG_MOD_REPLACE:
145 mods[n]->type = LDAP_MODIFY_REPLACE;
162 map an ildap NTSTATUS to a ldb error code
164 static int ildb_map_error(struct ldb_module *module, NTSTATUS status)
166 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
168 TALLOC_CTX *mem_ctx = talloc_new(ildb);
169 if (NT_STATUS_IS_OK(status)) {
173 ldb_oom(module->ldb);
174 return LDB_ERR_OPERATIONS_ERROR;
176 ldb_set_errstring(module->ldb,
177 ldap_errstr(ildb->ldap, mem_ctx, status));
178 talloc_free(mem_ctx);
179 if (NT_STATUS_IS_LDAP(status)) {
180 return NT_STATUS_LDAP_CODE(status);
182 return LDB_ERR_OPERATIONS_ERROR;
185 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
186 struct timeval t, void *private_data)
188 struct ildb_context *ac = talloc_get_type(private_data, struct ildb_context);
190 if (ac->ireq->state == LDAP_REQUEST_PENDING) {
191 DLIST_REMOVE(ac->ireq->conn->pending, ac->ireq);
194 ildb_request_done(ac, NULL, LDB_ERR_TIME_LIMIT_EXCEEDED);
197 static void ildb_callback(struct ldap_request *req)
199 struct ildb_context *ac;
201 struct ldap_SearchResEntry *search;
202 struct ldap_message *msg;
203 struct ldb_control **controls;
204 struct ldb_message *ldbmsg;
206 bool callback_failed;
211 ac = talloc_get_type(req->async.private_data, struct ildb_context);
212 callback_failed = false;
213 request_done = false;
216 if (!NT_STATUS_IS_OK(req->status)) {
217 ret = ildb_map_error(ac->module, req->status);
218 ildb_request_done(ac, NULL, ret);
222 if (req->num_replies < 1) {
223 ret = LDB_ERR_OPERATIONS_ERROR;
224 ildb_request_done(ac, NULL, ret);
230 case LDAP_TAG_ModifyRequest:
231 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
232 ret = LDB_ERR_PROTOCOL_ERROR;
235 status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
236 ret = ildb_map_error(ac->module, status);
240 case LDAP_TAG_AddRequest:
241 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
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_DelRequest:
251 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
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_ModifyDNRequest:
261 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
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_SearchRequest:
271 /* loop over all messages */
272 for (i = 0; i < req->num_replies; i++) {
274 msg = req->replies[i];
277 case LDAP_TAG_SearchResultDone:
279 status = ldap_check_response(ac->ireq->conn, &msg->r.GeneralResult);
280 if (!NT_STATUS_IS_OK(status)) {
281 ret = ildb_map_error(ac->module, status);
285 controls = talloc_steal(ac, msg->controls);
286 if (msg->r.SearchResultDone.resultcode) {
287 if (msg->r.SearchResultDone.errormessage) {
288 ldb_set_errstring(ac->module->ldb, msg->r.SearchResultDone.errormessage);
292 ret = msg->r.SearchResultDone.resultcode;
296 case LDAP_TAG_SearchResultEntry:
298 ldbmsg = ldb_msg_new(ac);
300 ret = LDB_ERR_OPERATIONS_ERROR;
304 search = &(msg->r.SearchResultEntry);
306 ldbmsg->dn = ldb_dn_new(ldbmsg, ac->module->ldb, search->dn);
307 if ( ! ldb_dn_validate(ldbmsg->dn)) {
308 ret = LDB_ERR_OPERATIONS_ERROR;
311 ldbmsg->num_elements = search->num_attributes;
312 ldbmsg->elements = talloc_move(ldbmsg, &search->attributes);
314 ret = ldb_module_send_entry(ac->req, ldbmsg);
315 if (ret != LDB_SUCCESS) {
316 callback_failed = true;
320 case LDAP_TAG_SearchResultReference:
322 referral = talloc_strdup(ac, msg->r.SearchResultReference.referral);
324 ret = ldb_module_send_referral(ac->req, referral);
325 if (ret != LDB_SUCCESS) {
326 callback_failed = true;
331 /* TAG not handled, fail ! */
332 ret = LDB_ERR_PROTOCOL_ERROR;
336 if (ret != LDB_SUCCESS) {
341 talloc_free(req->replies);
343 req->num_replies = 0;
348 ret = LDB_ERR_PROTOCOL_ERROR;
352 if (ret != LDB_SUCCESS) {
354 /* if the callback failed the caller will have freed the
355 * request. Just return and don't try to use it */
356 if ( ! callback_failed) {
362 ildb_request_done(ac, controls, ret);
367 static int ildb_request_send(struct ildb_context *ac, struct ldap_message *msg)
369 struct ldap_request *req;
372 return LDB_ERR_OPERATIONS_ERROR;
375 req = ldap_request_send(ac->ildb->ldap, msg);
377 ldb_set_errstring(ac->module->ldb, "async send request failed");
378 return LDB_ERR_OPERATIONS_ERROR;
380 ac->ireq = talloc_steal(ac, req);
382 if (!ac->ireq->conn) {
383 ldb_set_errstring(ac->module->ldb, "connection to remote LDAP server dropped?");
384 return LDB_ERR_OPERATIONS_ERROR;
387 talloc_free(req->time_event);
388 req->time_event = NULL;
389 if (ac->req->timeout) {
390 req->time_event = event_add_timed(ac->ildb->event_ctx, ac,
391 timeval_current_ofs(ac->req->timeout, 0),
392 ildb_request_timeout, ac);
395 req->async.fn = ildb_callback;
396 req->async.private_data = ac;
402 search for matching records using an asynchronous function
404 static int ildb_search(struct ildb_context *ac)
406 struct ldb_request *req = ac->req;
407 struct ldap_message *msg;
410 if (!req->callback || !req->context) {
411 ldb_set_errstring(ac->module->ldb, "Async interface called with NULL callback function or NULL context");
412 return LDB_ERR_OPERATIONS_ERROR;
415 if (req->op.search.tree == NULL) {
416 ldb_set_errstring(ac->module->ldb, "Invalid expression parse tree");
417 return LDB_ERR_OPERATIONS_ERROR;
420 msg = new_ldap_message(req);
422 ldb_set_errstring(ac->module->ldb, "Out of Memory");
423 return LDB_ERR_OPERATIONS_ERROR;
426 msg->type = LDAP_TAG_SearchRequest;
428 if (req->op.search.base == NULL) {
429 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
431 msg->r.SearchRequest.basedn = ldb_dn_alloc_linearized(msg, req->op.search.base);
433 if (msg->r.SearchRequest.basedn == NULL) {
434 ldb_set_errstring(ac->module->ldb, "Unable to determine baseDN");
436 return LDB_ERR_OPERATIONS_ERROR;
439 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
440 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
442 msg->r.SearchRequest.scope = req->op.search.scope;
445 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
446 msg->r.SearchRequest.timelimit = 0;
447 msg->r.SearchRequest.sizelimit = 0;
448 msg->r.SearchRequest.attributesonly = 0;
449 msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
451 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
452 msg->r.SearchRequest.num_attributes = n;
453 msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
454 msg->controls = req->controls;
456 return ildb_request_send(ac, msg);
462 static int ildb_add(struct ildb_context *ac)
464 struct ldb_request *req = ac->req;
465 struct ldap_message *msg;
466 struct ldap_mod **mods;
469 msg = new_ldap_message(req);
471 return LDB_ERR_OPERATIONS_ERROR;
474 msg->type = LDAP_TAG_AddRequest;
476 msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
477 if (msg->r.AddRequest.dn == NULL) {
479 return LDB_ERR_INVALID_DN_SYNTAX;
482 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
485 return LDB_ERR_OPERATIONS_ERROR;
488 msg->r.AddRequest.num_attributes = n;
489 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
490 if (msg->r.AddRequest.attributes == NULL) {
492 return LDB_ERR_OPERATIONS_ERROR;
495 for (i = 0; i < n; i++) {
496 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
499 return ildb_request_send(ac, msg);
505 static int ildb_modify(struct ildb_context *ac)
507 struct ldb_request *req = ac->req;
508 struct ldap_message *msg;
509 struct ldap_mod **mods;
512 msg = new_ldap_message(req);
514 return LDB_ERR_OPERATIONS_ERROR;
517 msg->type = LDAP_TAG_ModifyRequest;
519 msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
520 if (msg->r.ModifyRequest.dn == NULL) {
522 return LDB_ERR_INVALID_DN_SYNTAX;
525 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
528 return LDB_ERR_OPERATIONS_ERROR;
531 msg->r.ModifyRequest.num_mods = n;
532 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
533 if (msg->r.ModifyRequest.mods == NULL) {
535 return LDB_ERR_OPERATIONS_ERROR;
538 for (i = 0; i < n; i++) {
539 msg->r.ModifyRequest.mods[i] = *mods[i];
542 return ildb_request_send(ac, msg);
548 static int ildb_delete(struct ildb_context *ac)
550 struct ldb_request *req = ac->req;
551 struct ldap_message *msg;
553 msg = new_ldap_message(req);
555 return LDB_ERR_OPERATIONS_ERROR;
558 msg->type = LDAP_TAG_DelRequest;
560 msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
561 if (msg->r.DelRequest.dn == NULL) {
563 return LDB_ERR_INVALID_DN_SYNTAX;
566 return ildb_request_send(ac, msg);
572 static int ildb_rename(struct ildb_context *ac)
574 struct ldb_request *req = ac->req;
575 struct ldap_message *msg;
577 msg = new_ldap_message(req);
579 return LDB_ERR_OPERATIONS_ERROR;
582 msg->type = LDAP_TAG_ModifyDNRequest;
583 msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
584 if (msg->r.ModifyDNRequest.dn == NULL) {
586 return LDB_ERR_INVALID_DN_SYNTAX;
589 msg->r.ModifyDNRequest.newrdn =
590 talloc_asprintf(msg, "%s=%s",
591 ldb_dn_get_rdn_name(req->op.rename.newdn),
592 ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
593 if (msg->r.ModifyDNRequest.newrdn == NULL) {
595 return LDB_ERR_OPERATIONS_ERROR;
598 msg->r.ModifyDNRequest.newsuperior =
599 ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
600 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
602 return LDB_ERR_INVALID_DN_SYNTAX;
605 msg->r.ModifyDNRequest.deleteolddn = true;
607 return ildb_request_send(ac, msg);
610 static int ildb_start_trans(struct ldb_module *module)
612 /* TODO implement a local locking mechanism here */
617 static int ildb_end_trans(struct ldb_module *module)
619 /* TODO implement a local transaction mechanism here */
624 static int ildb_del_trans(struct ldb_module *module)
626 /* TODO implement a local locking mechanism here */
631 static bool ildb_dn_is_special(struct ldb_request *req)
633 struct ldb_dn *dn = NULL;
635 switch (req->operation) {
637 dn = req->op.add.message->dn;
640 dn = req->op.mod.message->dn;
646 dn = req->op.rename.olddn;
652 if (dn && ldb_dn_is_special(dn)) {
658 static int ildb_handle_request(struct ldb_module *module, struct ldb_request *req)
660 struct ildb_private *ildb;
661 struct ildb_context *ac;
662 struct timed_event *te;
665 ildb = talloc_get_type(module->private_data, struct ildb_private);
667 if (req->starttime == 0 || req->timeout == 0) {
668 ldb_set_errstring(module->ldb, "Invalid timeout settings");
669 return LDB_ERR_TIME_LIMIT_EXCEEDED;
672 ac = talloc_zero(req, struct ildb_context);
674 ldb_set_errstring(module->ldb, "Out of Memory");
675 return LDB_ERR_OPERATIONS_ERROR;
682 if (ildb_dn_is_special(req)) {
684 te = event_add_timed(ac->ildb->event_ctx,
686 ildb_auto_done_callback, ac);
688 return LDB_ERR_OPERATIONS_ERROR;
694 switch (ac->req->operation) {
696 ret = ildb_search(ac);
702 ret = ildb_modify(ac);
705 ret = ildb_delete(ac);
708 ret = ildb_rename(ac);
711 /* no other op supported */
712 ret = LDB_ERR_OPERATIONS_ERROR;
719 static const struct ldb_module_ops ildb_ops = {
721 .search = ildb_handle_request,
722 .add = ildb_handle_request,
723 .modify = ildb_handle_request,
724 .del = ildb_handle_request,
725 .rename = ildb_handle_request,
726 /* .request = ildb_handle_request, */
727 .start_transaction = ildb_start_trans,
728 .end_transaction = ildb_end_trans,
729 .del_transaction = ildb_del_trans,
733 connect to the database
735 static int ildb_connect(struct ldb_context *ldb, const char *url,
736 unsigned int flags, const char *options[],
737 struct ldb_module **_module)
739 struct ldb_module *module;
740 struct ildb_private *ildb;
742 struct cli_credentials *creds;
744 module = talloc(ldb, struct ldb_module);
749 talloc_set_name_const(module, "ldb_ildap backend");
751 module->prev = module->next = NULL;
752 module->ops = &ildb_ops;
754 ildb = talloc(module, struct ildb_private);
759 module->private_data = ildb;
761 ildb->event_ctx = ldb_get_event_context(ldb);
764 ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "loadparm"),
771 if (flags & LDB_FLG_RECONNECT) {
772 ldap_set_reconn_params(ildb->ldap, 10);
775 status = ldap_connect(ildb->ldap, url);
776 if (!NT_STATUS_IS_OK(status)) {
777 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
778 url, ldap_errstr(ildb->ldap, module, status));
782 /* caller can optionally setup credentials using the opaque token 'credentials' */
783 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
785 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
787 creds = session_info->credentials;
791 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
792 const char *bind_dn = cli_credentials_get_bind_dn(creds);
794 const char *password = cli_credentials_get_password(creds);
795 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
796 if (!NT_STATUS_IS_OK(status)) {
797 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
798 ldap_errstr(ildb->ldap, module, status));
802 status = ldap_bind_sasl(ildb->ldap, creds, ldb_get_opaque(ldb, "loadparm"));
803 if (!NT_STATUS_IS_OK(status)) {
804 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
805 ldap_errstr(ildb->ldap, module, status));
819 _PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = {
821 .connect_fn = ildb_connect
824 _PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = {
826 .connect_fn = ildb_connect
829 _PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = {
831 .connect_fn = ildb_connect