4 Copyright (C) Andrew Tridgell 2004
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 ldap backend
31 * Description: core files for LDAP backend
33 * Author: Andrew Tridgell
37 * - description: make the module use asyncronous calls
43 #include "ldb/include/includes.h"
45 #define LDAP_DEPRECATED 1
53 struct ldb_module *module;
58 int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
61 static int lldb_ldap_to_ldb(int err) {
62 /* Ldap errors and ldb errors are defined to the same values */
66 static struct ldb_handle *init_handle(struct lldb_private *lldb, struct ldb_module *module,
68 int (*callback)(struct ldb_context *, void *, struct ldb_reply *),
69 int timeout, time_t starttime)
71 struct lldb_context *ac;
74 h = talloc_zero(lldb, struct ldb_handle);
76 ldb_set_errstring(module->ldb, "Out of Memory");
82 ac = talloc(h, struct lldb_context);
84 ldb_set_errstring(module->ldb, "Out of Memory");
89 h->private_data = (void *)ac;
91 h->state = LDB_ASYNC_INIT;
92 h->status = LDB_SUCCESS;
95 ac->context = context;
96 ac->callback = callback;
97 ac->timeout = timeout;
98 ac->starttime = starttime;
104 convert a ldb_message structure to a list of LDAPMod structures
105 ready for ldap_add() or ldap_modify()
107 static LDAPMod **lldb_msg_to_mods(void *mem_ctx, const struct ldb_message *msg, int use_flags)
113 /* allocate maximum number of elements needed */
114 mods = talloc_array(mem_ctx, LDAPMod *, msg->num_elements+1);
121 for (i=0;i<msg->num_elements;i++) {
122 const struct ldb_message_element *el = &msg->elements[i];
124 mods[num_mods] = talloc(mods, LDAPMod);
125 if (!mods[num_mods]) {
128 mods[num_mods+1] = NULL;
129 mods[num_mods]->mod_op = LDAP_MOD_BVALUES;
131 switch (el->flags & LDB_FLAG_MOD_MASK) {
132 case LDB_FLAG_MOD_ADD:
133 mods[num_mods]->mod_op |= LDAP_MOD_ADD;
135 case LDB_FLAG_MOD_DELETE:
136 mods[num_mods]->mod_op |= LDAP_MOD_DELETE;
138 case LDB_FLAG_MOD_REPLACE:
139 mods[num_mods]->mod_op |= LDAP_MOD_REPLACE;
143 mods[num_mods]->mod_type = discard_const_p(char, el->name);
144 mods[num_mods]->mod_vals.modv_bvals = talloc_array(mods[num_mods],
147 if (!mods[num_mods]->mod_vals.modv_bvals) {
151 for (j=0;j<el->num_values;j++) {
152 mods[num_mods]->mod_vals.modv_bvals[j] = talloc(mods[num_mods]->mod_vals.modv_bvals,
154 if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
157 mods[num_mods]->mod_vals.modv_bvals[j]->bv_val = el->values[j].data;
158 mods[num_mods]->mod_vals.modv_bvals[j]->bv_len = el->values[j].length;
160 mods[num_mods]->mod_vals.modv_bvals[j] = NULL;
172 add a single set of ldap message values to a ldb_message
174 static int lldb_add_msg_attr(struct ldb_context *ldb,
175 struct ldb_message *msg,
176 const char *attr, struct berval **bval)
179 struct ldb_message_element *el;
181 count = ldap_count_values_len(bval);
187 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
188 msg->num_elements + 1);
196 el = &msg->elements[msg->num_elements];
198 el->name = talloc_strdup(msg->elements, attr);
206 el->values = talloc_array(msg->elements, struct ldb_val, count);
212 for (i=0;i<count;i++) {
213 /* we have to ensure this is null terminated so that
214 ldb_msg_find_attr_as_string() can work */
215 el->values[i].data = talloc_size(el->values, bval[i]->bv_len+1);
216 if (!el->values[i].data) {
220 memcpy(el->values[i].data, bval[i]->bv_val, bval[i]->bv_len);
221 el->values[i].data[bval[i]->bv_len] = 0;
222 el->values[i].length = bval[i]->bv_len;
232 search for matching records
234 static int lldb_search(struct ldb_module *module, struct ldb_request *req)
236 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
237 struct lldb_context *lldb_ac;
244 if (!req->callback || !req->context) {
245 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
246 return LDB_ERR_OPERATIONS_ERROR;
249 if (req->op.search.tree == NULL) {
250 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
251 return LDB_ERR_OPERATIONS_ERROR;
254 if (req->controls != NULL) {
255 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
258 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
259 if (req->handle == NULL) {
260 return LDB_ERR_OPERATIONS_ERROR;
263 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
265 search_base = ldb_dn_alloc_linearized(lldb_ac, req->op.search.base);
266 if (req->op.search.base == NULL) {
267 search_base = talloc_strdup(lldb_ac, "");
269 if (search_base == NULL) {
270 return LDB_ERR_OPERATIONS_ERROR;
273 expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
274 if (expression == NULL) {
275 return LDB_ERR_OPERATIONS_ERROR;
278 switch (req->op.search.scope) {
280 ldap_scope = LDAP_SCOPE_BASE;
282 case LDB_SCOPE_ONELEVEL:
283 ldap_scope = LDAP_SCOPE_ONELEVEL;
286 ldap_scope = LDAP_SCOPE_SUBTREE;
290 tv.tv_sec = req->timeout;
293 ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope,
295 discard_const_p(char *, req->op.search.attrs),
303 if (ret != LDAP_SUCCESS) {
304 ldb_set_errstring(module->ldb, ldap_err2string(ret));
307 return lldb_ldap_to_ldb(ret);
313 static int lldb_add(struct ldb_module *module, struct ldb_request *req)
315 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
316 struct lldb_context *lldb_ac;
321 /* ltdb specials should not reach this point */
322 if (ldb_dn_is_special(req->op.add.message->dn)) {
323 return LDB_ERR_INVALID_DN_SYNTAX;
326 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
327 if (req->handle == NULL) {
328 return LDB_ERR_OPERATIONS_ERROR;
331 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
333 mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
335 return LDB_ERR_OPERATIONS_ERROR;
338 dn = ldb_dn_alloc_linearized(lldb_ac, req->op.add.message->dn);
340 return LDB_ERR_OPERATIONS_ERROR;
343 ret = ldap_add_ext(lldb->ldap, dn, mods,
348 if (ret != LDAP_SUCCESS) {
349 ldb_set_errstring(module->ldb, ldap_err2string(ret));
352 return lldb_ldap_to_ldb(ret);
358 static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
360 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
361 struct lldb_context *lldb_ac;
366 /* ltdb specials should not reach this point */
367 if (ldb_dn_is_special(req->op.mod.message->dn)) {
368 return LDB_ERR_INVALID_DN_SYNTAX;
371 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
372 if (req->handle == NULL) {
373 return LDB_ERR_OPERATIONS_ERROR;
376 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
378 mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
380 return LDB_ERR_OPERATIONS_ERROR;
383 dn = ldb_dn_alloc_linearized(lldb_ac, req->op.mod.message->dn);
385 return LDB_ERR_OPERATIONS_ERROR;
388 ret = ldap_modify_ext(lldb->ldap, dn, mods,
393 if (ret != LDAP_SUCCESS) {
394 ldb_set_errstring(module->ldb, ldap_err2string(ret));
397 return lldb_ldap_to_ldb(ret);
403 static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
405 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
406 struct lldb_context *lldb_ac;
410 /* ltdb specials should not reach this point */
411 if (ldb_dn_is_special(req->op.del.dn)) {
412 return LDB_ERR_INVALID_DN_SYNTAX;
415 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
416 if (req->handle == NULL) {
417 return LDB_ERR_OPERATIONS_ERROR;
420 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
422 dnstr = ldb_dn_alloc_linearized(lldb_ac, req->op.del.dn);
424 ret = ldap_delete_ext(lldb->ldap, dnstr,
429 if (ret != LDAP_SUCCESS) {
430 ldb_set_errstring(module->ldb, ldap_err2string(ret));
433 return lldb_ldap_to_ldb(ret);
439 static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
441 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
442 struct lldb_context *lldb_ac;
448 /* ltdb specials should not reach this point */
449 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
450 return LDB_ERR_INVALID_DN_SYNTAX;
453 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
454 if (req->handle == NULL) {
455 return LDB_ERR_OPERATIONS_ERROR;
458 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
460 old_dn = ldb_dn_alloc_linearized(lldb_ac, req->op.rename.olddn);
461 if (old_dn == NULL) {
462 return LDB_ERR_OPERATIONS_ERROR;
465 newrdn = talloc_asprintf(lldb_ac, "%s=%s",
466 ldb_dn_get_rdn_name(req->op.rename.newdn),
467 ldb_dn_escape_value(lldb, *(ldb_dn_get_rdn_val(req->op.rename.newdn))));
469 return LDB_ERR_OPERATIONS_ERROR;
472 parentdn = ldb_dn_alloc_linearized(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
474 return LDB_ERR_OPERATIONS_ERROR;
477 ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
481 if (ret != LDAP_SUCCESS) {
482 ldb_set_errstring(module->ldb, ldap_err2string(ret));
485 return lldb_ldap_to_ldb(ret);
488 static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
490 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
491 struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
492 struct ldb_reply *ares = NULL;
495 char *matcheddnp = NULL;
496 char *errmsgp = NULL;
497 char **referralsp = NULL;
498 LDAPControl **serverctrlsp = NULL;
499 int ret = LDB_SUCCESS;
501 type = ldap_msgtype(result);
507 case LDAP_RES_SEARCH_ENTRY:
508 msg = ldap_first_entry(lldb->ldap, result);
510 BerElement *berptr = NULL;
513 ares = talloc_zero(ac, struct ldb_reply);
515 ret = LDB_ERR_OPERATIONS_ERROR;
519 ares->message = ldb_msg_new(ares);
520 if (!ares->message) {
521 ret = LDB_ERR_OPERATIONS_ERROR;
525 dn = ldap_get_dn(lldb->ldap, msg);
527 ret = LDB_ERR_OPERATIONS_ERROR;
530 ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, dn);
531 if ( ! ldb_dn_validate(ares->message->dn)) {
532 ret = LDB_ERR_OPERATIONS_ERROR;
537 ares->message->num_elements = 0;
538 ares->message->elements = NULL;
539 ares->message->private_data = NULL;
541 /* loop over all attributes */
542 for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
544 attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
545 struct berval **bval;
546 bval = ldap_get_values_len(lldb->ldap, msg, attr);
549 lldb_add_msg_attr(ac->module->ldb, ares->message, attr, bval);
550 ldap_value_free_len(bval);
553 if (berptr) ber_free(berptr, 0);
556 ares->type = LDB_REPLY_ENTRY;
557 ret = ac->callback(ac->module->ldb, ac->context, ares);
559 handle->status = LDB_ERR_PROTOCOL_ERROR;
560 handle->state = LDB_ASYNC_DONE;
564 case LDAP_RES_SEARCH_REFERENCE:
565 if (ldap_parse_result(lldb->ldap, result, &handle->status,
566 &matcheddnp, &errmsgp,
567 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
568 ret = LDB_ERR_OPERATIONS_ERROR;
571 if (referralsp == NULL) {
572 handle->status = LDB_ERR_PROTOCOL_ERROR;
576 ares = talloc_zero(ac, struct ldb_reply);
578 ret = LDB_ERR_OPERATIONS_ERROR;
582 ares->referral = talloc_strdup(ares, *referralsp);
583 ares->type = LDB_REPLY_REFERRAL;
584 ret = ac->callback(ac->module->ldb, ac->context, ares);
588 case LDAP_RES_SEARCH_RESULT:
589 if (ldap_parse_result(lldb->ldap, result, &handle->status,
590 &matcheddnp, &errmsgp,
591 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
592 handle->status = LDB_ERR_OPERATIONS_ERROR;
596 ares = talloc_zero(ac, struct ldb_reply);
598 ret = LDB_ERR_OPERATIONS_ERROR;
602 if (serverctrlsp != NULL) {
603 /* FIXME: transform the LDAPControl list into an ldb_control one */
604 ares->controls = NULL;
607 ares->type = LDB_REPLY_DONE;
608 handle->state = LDB_ASYNC_DONE;
609 ret = ac->callback(ac->module->ldb, ac->context, ares);
613 case LDAP_RES_MODIFY:
615 case LDAP_RES_DELETE:
617 if (ldap_parse_result(lldb->ldap, result, &handle->status,
618 &matcheddnp, &errmsgp,
619 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
620 handle->status = LDB_ERR_OPERATIONS_ERROR;
623 if (ac->callback && handle->status == LDB_SUCCESS) {
624 ares = NULL; /* FIXME: build a corresponding ares to pass on */
625 ret = ac->callback(ac->module->ldb, ac->context, ares);
627 handle->state = LDB_ASYNC_DONE;
631 ret = LDB_ERR_PROTOCOL_ERROR;
635 if (matcheddnp) ldap_memfree(matcheddnp);
636 if (errmsgp && *errmsgp) {
637 ldb_set_errstring(ac->module->ldb, errmsgp);
638 } else if (handle->status) {
639 ldb_set_errstring(ac->module->ldb, ldap_err2string(handle->status));
642 ldap_memfree(errmsgp);
644 if (referralsp) ldap_value_free(referralsp);
645 if (serverctrlsp) ldap_controls_free(serverctrlsp);
647 ldap_msgfree(result);
648 return lldb_ldap_to_ldb(handle->status);
651 handle->state = LDB_ASYNC_DONE;
652 ldap_msgfree(result);
656 static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
658 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
659 struct lldb_private *lldb = talloc_get_type(handle->module->private_data, struct lldb_private);
660 struct timeval timeout;
664 if (handle->state == LDB_ASYNC_DONE) {
665 return handle->status;
668 if (!ac || !ac->msgid) {
669 return LDB_ERR_OPERATIONS_ERROR;
672 handle->state = LDB_ASYNC_PENDING;
673 handle->status = LDB_SUCCESS;
678 if ((ac->timeout != -1) &&
679 ((ac->starttime + ac->timeout) > time(NULL))) {
680 return LDB_ERR_TIME_LIMIT_EXCEEDED;
686 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
688 return LDB_ERR_OPERATIONS_ERROR;
695 return lldb_parse_result(handle, result);
699 ret = LDB_ERR_OPERATIONS_ERROR;
701 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
703 if (ac->timeout == -1) {
704 lret = ldap_result(lldb->ldap, ac->msgid, 0, NULL, &result);
706 timeout.tv_sec = ac->timeout - (time(NULL) - ac->starttime);
707 if (timeout.tv_sec <= 0)
708 return LDB_ERR_TIME_LIMIT_EXCEEDED;
709 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
712 return LDB_ERR_OPERATIONS_ERROR;
715 return LDB_ERR_TIME_LIMIT_EXCEEDED;
718 ret = lldb_parse_result(handle, result);
719 if (ret != LDB_SUCCESS) {
727 handle->state = LDB_ASYNC_DONE;
728 ret = LDB_ERR_OPERATIONS_ERROR;
735 static int lldb_start_trans(struct ldb_module *module)
737 /* TODO implement a local transaction mechanism here */
742 static int lldb_end_trans(struct ldb_module *module)
744 /* TODO implement a local transaction mechanism here */
749 static int lldb_del_trans(struct ldb_module *module)
751 /* TODO implement a local transaction mechanism here */
756 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
758 return LDB_ERR_OPERATIONS_ERROR;
761 static const struct ldb_module_ops lldb_ops = {
763 .search = lldb_search,
765 .modify = lldb_modify,
767 .rename = lldb_rename,
768 .request = lldb_request,
769 .start_transaction = lldb_start_trans,
770 .end_transaction = lldb_end_trans,
771 .del_transaction = lldb_del_trans,
776 static int lldb_destructor(struct lldb_private *lldb)
778 ldap_unbind(lldb->ldap);
783 connect to the database
785 static int lldb_connect(struct ldb_context *ldb,
788 const char *options[],
789 struct ldb_module **module)
791 struct lldb_private *lldb = NULL;
795 lldb = talloc(ldb, struct lldb_private);
803 ret = ldap_initialize(&lldb->ldap, url);
804 if (ret != LDAP_SUCCESS) {
805 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_initialize failed for URL '%s' - %s\n",
806 url, ldap_err2string(ret));
810 talloc_set_destructor(lldb, lldb_destructor);
812 ret = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
813 if (ret != LDAP_SUCCESS) {
814 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_set_option failed - %s\n",
815 ldap_err2string(ret));
819 *module = talloc(ldb, struct ldb_module);
820 if (*module == NULL) {
825 talloc_set_name_const(*module, "ldb_ldap backend");
826 (*module)->ldb = ldb;
827 (*module)->prev = (*module)->next = NULL;
828 (*module)->private_data = lldb;
829 (*module)->ops = &lldb_ops;
838 int ldb_ldap_init(void)
840 return ldb_register_backend("ldap", lldb_connect) +
841 ldb_register_backend("ldapi", lldb_connect) +
842 ldb_register_backend("ldaps", lldb_connect);