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 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, int use_flags)
74 struct ldap_mod **mods;
78 /* allocate maximum number of elements needed */
79 mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
86 for (i = 0; i < msg->num_elements; i++) {
87 const struct ldb_message_element *el = &msg->elements[i];
89 mods[n] = talloc(mods, struct ldap_mod);
95 mods[n]->attrib = *el;
97 switch (el->flags & LDB_FLAG_MOD_MASK) {
98 case LDB_FLAG_MOD_ADD:
99 mods[n]->type = LDAP_MODIFY_ADD;
101 case LDB_FLAG_MOD_DELETE:
102 mods[n]->type = LDAP_MODIFY_DELETE;
104 case LDB_FLAG_MOD_REPLACE:
105 mods[n]->type = LDAP_MODIFY_REPLACE;
122 map an ildap NTSTATUS to a ldb error code
124 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
126 if (NT_STATUS_IS_OK(status)) {
129 ldb_set_errstring(ildb->ldb, ldap_errstr(ildb->ldap, status));
130 if (NT_STATUS_IS_LDAP(status)) {
131 return NT_STATUS_LDAP_CODE(status);
133 return LDB_ERR_OPERATIONS_ERROR;
136 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
137 struct timeval t, void *private_data)
139 struct ldb_handle *handle = talloc_get_type(private_data, struct ldb_handle);
140 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
142 if (ac->req->state == LDAP_REQUEST_PENDING) {
143 DLIST_REMOVE(ac->req->conn->pending, ac->req);
146 handle->status = LDB_ERR_TIME_LIMIT_EXCEEDED;
151 static void ildb_callback(struct ldap_request *req)
153 struct ldb_handle *handle = talloc_get_type(req->async.private_data, struct ldb_handle);
154 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
155 struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private);
159 handle->status = LDB_SUCCESS;
161 if (!NT_STATUS_IS_OK(req->status)) {
162 handle->status = ildb_map_error(ildb, req->status);
166 if (req->num_replies < 1) {
167 handle->status = LDB_ERR_OPERATIONS_ERROR;
173 case LDAP_TAG_ModifyRequest:
174 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
175 handle->status = LDB_ERR_PROTOCOL_ERROR;
178 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
179 handle->status = ildb_map_error(ildb, status);
180 if (ac->callback && handle->status == LDB_SUCCESS) {
181 /* FIXME: build a corresponding ares to pass on */
182 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
184 handle->state = LDB_ASYNC_DONE;
187 case LDAP_TAG_AddRequest:
188 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
189 handle->status = LDB_ERR_PROTOCOL_ERROR;
192 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
193 handle->status = ildb_map_error(ildb, status);
194 if (ac->callback && handle->status == LDB_SUCCESS) {
195 /* FIXME: build a corresponding ares to pass on */
196 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
198 handle->state = LDB_ASYNC_DONE;
201 case LDAP_TAG_DelRequest:
202 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
203 handle->status = LDB_ERR_PROTOCOL_ERROR;
206 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
207 handle->status = ildb_map_error(ildb, status);
208 if (ac->callback && handle->status == LDB_SUCCESS) {
209 /* FIXME: build a corresponding ares to pass on */
210 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
212 handle->state = LDB_ASYNC_DONE;
215 case LDAP_TAG_ModifyDNRequest:
216 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
217 handle->status = LDB_ERR_PROTOCOL_ERROR;
220 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
221 handle->status = ildb_map_error(ildb, status);
222 if (ac->callback && handle->status == LDB_SUCCESS) {
223 /* FIXME: build a corresponding ares to pass on */
224 handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
226 handle->state = LDB_ASYNC_DONE;
229 case LDAP_TAG_SearchRequest:
230 /* loop over all messages */
231 for (i = 0; i < req->num_replies; i++) {
232 struct ldap_SearchResEntry *search;
233 struct ldb_reply *ares = NULL;
234 struct ldap_message *msg;
237 ares = talloc_zero(ac, struct ldb_reply);
239 handle->status = LDB_ERR_OPERATIONS_ERROR;
243 msg = req->replies[i];
246 case LDAP_TAG_SearchResultDone:
248 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
249 if (!NT_STATUS_IS_OK(status)) {
250 handle->status = ildb_map_error(ildb, status);
254 ares->controls = talloc_move(ares, &msg->controls);
255 if (msg->r.SearchResultDone.resultcode) {
256 if (msg->r.SearchResultDone.errormessage) {
257 ldb_set_errstring(ac->module->ldb, msg->r.SearchResultDone.errormessage);
261 handle->status = msg->r.SearchResultDone.resultcode;
262 handle->state = LDB_ASYNC_DONE;
263 ares->type = LDB_REPLY_DONE;
266 case LDAP_TAG_SearchResultEntry:
269 ares->message = ldb_msg_new(ares);
270 if (!ares->message) {
271 handle->status = LDB_ERR_OPERATIONS_ERROR;
275 search = &(msg->r.SearchResultEntry);
277 ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, search->dn);
278 if ( ! ldb_dn_validate(ares->message->dn)) {
279 handle->status = LDB_ERR_OPERATIONS_ERROR;
282 ares->message->num_elements = search->num_attributes;
283 ares->message->elements = talloc_move(ares->message,
284 &search->attributes);
286 handle->status = LDB_SUCCESS;
287 handle->state = LDB_ASYNC_PENDING;
288 ares->type = LDB_REPLY_ENTRY;
291 case LDAP_TAG_SearchResultReference:
293 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
295 handle->status = LDB_SUCCESS;
296 handle->state = LDB_ASYNC_PENDING;
297 ares->type = LDB_REPLY_REFERRAL;
301 /* TAG not handled, fail ! */
302 handle->status = LDB_ERR_PROTOCOL_ERROR;
306 ret = ac->callback(ac->module->ldb, ac->context, ares);
308 handle->status = ret;
312 talloc_free(req->replies);
314 req->num_replies = 0;
319 handle->status = LDB_ERR_PROTOCOL_ERROR;
324 static struct ldb_handle *init_ildb_handle(struct ldb_module *module,
326 int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
328 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
329 struct ildb_context *ildb_ac;
330 struct ldb_handle *h;
332 h = talloc_zero(ildb->ldap, struct ldb_handle);
334 ldb_set_errstring(module->ldb, "Out of Memory");
340 ildb_ac = talloc(h, struct ildb_context);
341 if (ildb_ac == NULL) {
342 ldb_set_errstring(module->ldb, "Out of Memory");
347 h->private_data = (void *)ildb_ac;
349 h->state = LDB_ASYNC_INIT;
350 h->status = LDB_SUCCESS;
352 ildb_ac->module = module;
353 ildb_ac->context = context;
354 ildb_ac->callback = callback;
359 static int ildb_request_send(struct ildb_private *ildb, struct ldap_message *msg, struct ldb_request *r)
361 struct ldb_handle *h = init_ildb_handle(ildb->module, r->context, r->callback);
362 struct ildb_context *ildb_ac;
363 struct ldap_request *req;
366 return LDB_ERR_OPERATIONS_ERROR;
369 ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
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, h,
386 timeval_current_ofs(r->timeout, 0),
387 ildb_request_timeout, h);
390 req->async.fn = ildb_callback;
391 req->async.private_data = h;
392 ildb_ac->req = talloc_move(ildb_ac, &req);
398 static int ildb_request_noop(struct ildb_private *ildb, struct ldb_request *req)
400 struct ldb_handle *h = init_ildb_handle(ildb->module, req->context, req->callback);
401 struct ildb_context *ildb_ac;
402 int ret = LDB_SUCCESS;
405 return LDB_ERR_OPERATIONS_ERROR;
408 ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
412 if (ildb_ac->callback) {
413 ret = ildb_ac->callback(ildb->module->ldb, ildb_ac->context, NULL);
415 req->handle->state = LDB_ASYNC_DONE;
420 search for matching records using an asynchronous function
422 static int ildb_search(struct ldb_module *module, struct ldb_request *req)
424 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
425 struct ldap_message *msg;
430 if (!req->callback || !req->context) {
431 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
432 return LDB_ERR_OPERATIONS_ERROR;
435 if (req->op.search.tree == NULL) {
436 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
437 return LDB_ERR_OPERATIONS_ERROR;
440 msg = new_ldap_message(ildb);
442 ldb_set_errstring(module->ldb, "Out of Memory");
443 return LDB_ERR_OPERATIONS_ERROR;
446 msg->type = LDAP_TAG_SearchRequest;
448 if (req->op.search.base == NULL) {
449 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
451 msg->r.SearchRequest.basedn = ldb_dn_alloc_linearized(msg, req->op.search.base);
453 if (msg->r.SearchRequest.basedn == NULL) {
454 ldb_set_errstring(module->ldb, "Unable to determine baseDN");
456 return LDB_ERR_OPERATIONS_ERROR;
459 if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
460 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
462 msg->r.SearchRequest.scope = req->op.search.scope;
465 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
466 msg->r.SearchRequest.timelimit = 0;
467 msg->r.SearchRequest.sizelimit = 0;
468 msg->r.SearchRequest.attributesonly = 0;
469 msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
471 for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
472 msg->r.SearchRequest.num_attributes = n;
473 msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
474 msg->controls = req->controls;
476 return ildb_request_send(ildb, msg, req);
482 static int ildb_add(struct ldb_module *module, struct ldb_request *req)
484 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
485 struct ldap_message *msg;
486 struct ldap_mod **mods;
491 /* ignore ltdb specials */
492 if (ldb_dn_is_special(req->op.add.message->dn)) {
493 return ildb_request_noop(ildb, req);
496 msg = new_ldap_message(ildb->ldap);
498 return LDB_ERR_OPERATIONS_ERROR;
501 msg->type = LDAP_TAG_AddRequest;
503 msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
504 if (msg->r.AddRequest.dn == NULL) {
506 return LDB_ERR_INVALID_DN_SYNTAX;
509 mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
512 return LDB_ERR_OPERATIONS_ERROR;
515 msg->r.AddRequest.num_attributes = n;
516 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
517 if (msg->r.AddRequest.attributes == NULL) {
519 return LDB_ERR_OPERATIONS_ERROR;
522 for (i = 0; i < n; i++) {
523 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
526 return ildb_request_send(ildb, msg, req);
532 static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
534 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
535 struct ldap_message *msg;
536 struct ldap_mod **mods;
541 /* ignore ltdb specials */
542 if (ldb_dn_is_special(req->op.mod.message->dn)) {
543 return ildb_request_noop(ildb, req);
546 msg = new_ldap_message(ildb->ldap);
548 return LDB_ERR_OPERATIONS_ERROR;
551 msg->type = LDAP_TAG_ModifyRequest;
553 msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
554 if (msg->r.ModifyRequest.dn == NULL) {
556 return LDB_ERR_INVALID_DN_SYNTAX;
559 mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
562 return LDB_ERR_OPERATIONS_ERROR;
565 msg->r.ModifyRequest.num_mods = n;
566 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
567 if (msg->r.ModifyRequest.mods == NULL) {
569 return LDB_ERR_OPERATIONS_ERROR;
572 for (i = 0; i < n; i++) {
573 msg->r.ModifyRequest.mods[i] = *mods[i];
576 return ildb_request_send(ildb, msg, req);
582 static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
584 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
585 struct ldap_message *msg;
589 /* ignore ltdb specials */
590 if (ldb_dn_is_special(req->op.del.dn)) {
591 return ildb_request_noop(ildb, req);
594 msg = new_ldap_message(ildb->ldap);
596 return LDB_ERR_OPERATIONS_ERROR;
599 msg->type = LDAP_TAG_DelRequest;
601 msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
602 if (msg->r.DelRequest.dn == NULL) {
604 return LDB_ERR_INVALID_DN_SYNTAX;
607 return ildb_request_send(ildb, msg, req);
613 static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
615 struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
616 struct ldap_message *msg;
620 /* ignore ltdb specials */
621 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
622 return ildb_request_noop(ildb, req);
625 msg = new_ldap_message(ildb->ldap);
627 return LDB_ERR_OPERATIONS_ERROR;
630 msg->type = LDAP_TAG_ModifyDNRequest;
631 msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
632 if (msg->r.ModifyDNRequest.dn == NULL) {
634 return LDB_ERR_INVALID_DN_SYNTAX;
637 msg->r.ModifyDNRequest.newrdn =
638 talloc_asprintf(msg, "%s=%s",
639 ldb_dn_get_rdn_name(req->op.rename.newdn),
640 ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
641 if (msg->r.ModifyDNRequest.newrdn == NULL) {
643 return LDB_ERR_OPERATIONS_ERROR;
646 msg->r.ModifyDNRequest.newsuperior =
647 ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
648 if (msg->r.ModifyDNRequest.newsuperior == NULL) {
650 return LDB_ERR_INVALID_DN_SYNTAX;
653 msg->r.ModifyDNRequest.deleteolddn = True;
655 return ildb_request_send(ildb, msg, req);
658 static int ildb_start_trans(struct ldb_module *module)
660 /* TODO implement a local locking mechanism here */
665 static int ildb_end_trans(struct ldb_module *module)
667 /* TODO implement a local transaction mechanism here */
672 static int ildb_del_trans(struct ldb_module *module)
674 /* TODO implement a local locking mechanism here */
679 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
681 return LDB_ERR_OPERATIONS_ERROR;
684 static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
686 struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
688 if (handle->state == LDB_ASYNC_DONE) {
689 return handle->status;
693 return LDB_ERR_OPERATIONS_ERROR;
696 handle->state = LDB_ASYNC_INIT;
700 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
701 return LDB_ERR_OTHER;
705 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
706 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
707 return LDB_ERR_OTHER;
712 return LDB_ERR_OPERATIONS_ERROR;
715 return handle->status;
718 static const struct ldb_module_ops ildb_ops = {
720 .search = ildb_search,
722 .modify = ildb_modify,
724 .rename = ildb_rename,
725 .request = ildb_request,
726 .start_transaction = ildb_start_trans,
727 .end_transaction = ildb_end_trans,
728 .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->private_data = NULL;
753 module->ops = &ildb_ops;
755 ildb = talloc(module, struct ildb_private);
760 module->private_data = ildb;
762 ildb->module = module;
763 ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
769 if (flags & LDB_FLG_RECONNECT) {
770 ldap_set_reconn_params(ildb->ldap, 10);
773 status = ldap_connect(ildb->ldap, url);
774 if (!NT_STATUS_IS_OK(status)) {
775 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
776 url, ldap_errstr(ildb->ldap, status));
780 /* caller can optionally setup credentials using the opaque token 'credentials' */
781 creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
783 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
785 creds = session_info->credentials;
789 if (creds != NULL && cli_credentials_authentication_requested(creds)) {
790 const char *bind_dn = cli_credentials_get_bind_dn(creds);
792 const char *password = cli_credentials_get_password(creds);
793 status = ldap_bind_simple(ildb->ldap, bind_dn, password);
794 if (!NT_STATUS_IS_OK(status)) {
795 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
796 ldap_errstr(ildb->ldap, status));
800 status = ldap_bind_sasl(ildb->ldap, creds);
801 if (!NT_STATUS_IS_OK(status)) {
802 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
803 ldap_errstr(ildb->ldap, status));
817 int ldb_ildap_init(void)
819 return ldb_register_backend("ldap", ildb_connect) +
820 ldb_register_backend("ldapi", ildb_connect) +
821 ldb_register_backend("ldaps", ildb_connect);