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, talloc_asprintf(module, "Out of Memory"));
82 ac = talloc(h, struct lldb_context);
84 ldb_set_errstring(module->ldb, talloc_asprintf(module, "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 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
214 if (!el->values[i].data) {
217 el->values[i].length = bval[i]->bv_len;
227 search for matching records
229 static int lldb_search(struct ldb_module *module, struct ldb_request *req)
231 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
232 struct lldb_context *lldb_ac;
239 if (!req->callback || !req->context) {
240 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
241 return LDB_ERR_OPERATIONS_ERROR;
244 if (req->op.search.tree == NULL) {
245 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Invalid expression parse tree"));
246 return LDB_ERR_OPERATIONS_ERROR;
249 if (req->controls != NULL) {
250 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
253 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
254 if (req->handle == NULL) {
255 return LDB_ERR_OPERATIONS_ERROR;
258 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
260 search_base = ldb_dn_linearize(lldb_ac, req->op.search.base);
261 if (req->op.search.base == NULL) {
262 search_base = talloc_strdup(lldb_ac, "");
264 if (search_base == NULL) {
265 return LDB_ERR_OPERATIONS_ERROR;
268 expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
269 if (expression == NULL) {
270 return LDB_ERR_OPERATIONS_ERROR;
273 switch (req->op.search.scope) {
275 ldap_scope = LDAP_SCOPE_BASE;
277 case LDB_SCOPE_ONELEVEL:
278 ldap_scope = LDAP_SCOPE_ONELEVEL;
281 ldap_scope = LDAP_SCOPE_SUBTREE;
285 tv.tv_sec = req->timeout;
288 ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope,
290 discard_const_p(char *, req->op.search.attrs),
298 if (ret != LDAP_SUCCESS) {
299 ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
302 return lldb_ldap_to_ldb(ret);
308 static int lldb_add(struct ldb_module *module, struct ldb_request *req)
310 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
311 struct lldb_context *lldb_ac;
316 /* ltdb specials should not reach this point */
317 if (ldb_dn_is_special(req->op.add.message->dn)) {
318 return LDB_ERR_INVALID_DN_SYNTAX;
321 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
322 if (req->handle == NULL) {
323 return LDB_ERR_OPERATIONS_ERROR;
326 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
328 mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
330 return LDB_ERR_OPERATIONS_ERROR;
333 dn = ldb_dn_linearize(lldb_ac, req->op.add.message->dn);
335 return LDB_ERR_OPERATIONS_ERROR;
338 ret = ldap_add_ext(lldb->ldap, dn, mods,
343 if (ret != LDAP_SUCCESS) {
344 ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
347 return lldb_ldap_to_ldb(ret);
353 static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
355 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
356 struct lldb_context *lldb_ac;
361 /* ltdb specials should not reach this point */
362 if (ldb_dn_is_special(req->op.mod.message->dn)) {
363 return LDB_ERR_INVALID_DN_SYNTAX;
366 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
367 if (req->handle == NULL) {
368 return LDB_ERR_OPERATIONS_ERROR;
371 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
373 mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
375 return LDB_ERR_OPERATIONS_ERROR;
378 dn = ldb_dn_linearize(lldb_ac, req->op.mod.message->dn);
380 return LDB_ERR_OPERATIONS_ERROR;
383 ret = ldap_modify_ext(lldb->ldap, dn, mods,
388 if (ret != LDAP_SUCCESS) {
389 ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
392 return lldb_ldap_to_ldb(ret);
398 static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
400 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
401 struct lldb_context *lldb_ac;
405 /* ltdb specials should not reach this point */
406 if (ldb_dn_is_special(req->op.del.dn)) {
407 return LDB_ERR_INVALID_DN_SYNTAX;
410 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
411 if (req->handle == NULL) {
412 return LDB_ERR_OPERATIONS_ERROR;
415 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
417 dnstr = ldb_dn_linearize(lldb_ac, req->op.del.dn);
419 ret = ldap_delete_ext(lldb->ldap, dnstr,
424 if (ret != LDAP_SUCCESS) {
425 ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
428 return lldb_ldap_to_ldb(ret);
434 static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
436 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
437 struct lldb_context *lldb_ac;
443 /* ltdb specials should not reach this point */
444 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
445 return LDB_ERR_INVALID_DN_SYNTAX;
448 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
449 if (req->handle == NULL) {
450 return LDB_ERR_OPERATIONS_ERROR;
453 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
455 old_dn = ldb_dn_linearize(lldb_ac, req->op.rename.olddn);
456 if (old_dn == NULL) {
457 return LDB_ERR_OPERATIONS_ERROR;
460 newrdn = talloc_asprintf(lldb_ac, "%s=%s",
461 req->op.rename.newdn->components[0].name,
462 ldb_dn_escape_value(lldb, req->op.rename.newdn->components[0].value));
464 return LDB_ERR_OPERATIONS_ERROR;
467 parentdn = ldb_dn_linearize(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
469 return LDB_ERR_OPERATIONS_ERROR;
472 ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
476 if (ret != LDAP_SUCCESS) {
477 ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
480 return lldb_ldap_to_ldb(ret);
483 static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
485 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
486 struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
487 struct ldb_reply *ares = NULL;
490 char *matcheddnp = NULL;
491 char *errmsgp = NULL;
492 char **referralsp = NULL;
493 LDAPControl **serverctrlsp = NULL;
494 int ret = LDB_SUCCESS;
496 type = ldap_msgtype(result);
500 case LDAP_RES_SEARCH_ENTRY:
501 msg = ldap_first_entry(lldb->ldap, result);
503 BerElement *berptr = NULL;
506 ares = talloc_zero(ac, struct ldb_reply);
508 ret = LDB_ERR_OPERATIONS_ERROR;
512 ares->message = ldb_msg_new(ares);
513 if (!ares->message) {
514 ret = LDB_ERR_OPERATIONS_ERROR;
518 dn = ldap_get_dn(lldb->ldap, msg);
520 ret = LDB_ERR_OPERATIONS_ERROR;
523 ares->message->dn = ldb_dn_explode_or_special(ares->message, dn);
524 if (ares->message->dn == NULL) {
525 ret = LDB_ERR_OPERATIONS_ERROR;
530 ares->message->num_elements = 0;
531 ares->message->elements = NULL;
532 ares->message->private_data = NULL;
534 /* loop over all attributes */
535 for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
537 attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
538 struct berval **bval;
539 bval = ldap_get_values_len(lldb->ldap, msg, attr);
542 lldb_add_msg_attr(ac->module->ldb, ares->message, attr, bval);
543 ldap_value_free_len(bval);
546 if (berptr) ber_free(berptr, 0);
549 ares->type = LDB_REPLY_ENTRY;
550 ret = ac->callback(ac->module->ldb, ac->context, ares);
552 handle->status = LDB_ERR_PROTOCOL_ERROR;
553 handle->state = LDB_ASYNC_DONE;
557 case LDAP_RES_SEARCH_REFERENCE:
558 if (ldap_parse_result(lldb->ldap, result, &handle->status,
559 &matcheddnp, &errmsgp,
560 &referralsp, &serverctrlsp, 1) != LDAP_SUCCESS) {
561 ret = LDB_ERR_OPERATIONS_ERROR;
564 if (referralsp == NULL) {
565 handle->status = LDB_ERR_PROTOCOL_ERROR;
569 ares = talloc_zero(ac, struct ldb_reply);
571 ret = LDB_ERR_OPERATIONS_ERROR;
575 ares->referral = talloc_strdup(ares, *referralsp);
576 ares->type = LDB_REPLY_REFERRAL;
577 ret = ac->callback(ac->module->ldb, ac->context, ares);
581 case LDAP_RES_SEARCH_RESULT:
582 if (ldap_parse_result(lldb->ldap, result, &handle->status,
583 &matcheddnp, &errmsgp,
584 &referralsp, &serverctrlsp, 1) != LDAP_SUCCESS) {
585 handle->status = LDB_ERR_OPERATIONS_ERROR;
589 ares = talloc_zero(ac, struct ldb_reply);
591 ret = LDB_ERR_OPERATIONS_ERROR;
595 if (serverctrlsp != NULL) {
596 /* FIXME: transform the LDAPControl list into an ldb_control one */
597 ares->controls = NULL;
600 ares->type = LDB_REPLY_DONE;
601 handle->state = LDB_ASYNC_DONE;
602 ret = ac->callback(ac->module->ldb, ac->context, ares);
606 case LDAP_RES_MODIFY:
608 case LDAP_RES_DELETE:
610 if (ldap_parse_result(lldb->ldap, result, &handle->status,
611 &matcheddnp, &errmsgp,
612 &referralsp, &serverctrlsp, 1) != LDAP_SUCCESS) {
613 handle->status = LDB_ERR_OPERATIONS_ERROR;
616 if (ac->callback && handle->status == LDB_SUCCESS) {
617 ares = NULL; /* FIXME: build a corresponding ares to pass on */
618 ret = ac->callback(ac->module->ldb, ac->context, ares);
620 handle->state = LDB_ASYNC_DONE;
624 ret = LDB_ERR_PROTOCOL_ERROR;
628 if (matcheddnp) ldap_memfree(matcheddnp);
630 ldb_set_errstring(ac->module->ldb, talloc_strdup(ac->module, errmsgp));
631 ldap_memfree(errmsgp);
633 if (referralsp) ldap_value_free(referralsp);
634 if (serverctrlsp) ldap_controls_free(serverctrlsp);
636 ldap_msgfree(result);
640 handle->state = LDB_ASYNC_DONE;
641 ldap_msgfree(result);
645 static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
647 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
648 struct lldb_private *lldb = talloc_get_type(handle->module->private_data, struct lldb_private);
649 struct timeval timeout;
653 if (handle->state == LDB_ASYNC_DONE) {
654 return handle->status;
657 if (!ac || !ac->msgid) {
658 return LDB_ERR_OPERATIONS_ERROR;
661 handle->state = LDB_ASYNC_PENDING;
662 handle->status = LDB_SUCCESS;
667 if ((ac->timeout != -1) &&
668 ((ac->starttime + ac->timeout) > time(NULL))) {
669 return LDB_ERR_TIME_LIMIT_EXCEEDED;
675 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
677 return LDB_ERR_OPERATIONS_ERROR;
684 return lldb_parse_result(handle, result);
688 ret = LDB_ERR_OPERATIONS_ERROR;
690 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
692 if (ac->timeout == -1) {
693 lret = ldap_result(lldb->ldap, ac->msgid, 0, NULL, &result);
695 timeout.tv_sec = ac->timeout - (time(NULL) - ac->starttime);
696 if (timeout.tv_sec <= 0)
697 return LDB_ERR_TIME_LIMIT_EXCEEDED;
698 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
701 return LDB_ERR_OPERATIONS_ERROR;
704 return LDB_ERR_TIME_LIMIT_EXCEEDED;
707 ret = lldb_parse_result(handle, result);
708 if (ret != LDB_SUCCESS) {
716 handle->state = LDB_ASYNC_DONE;
717 ret = LDB_ERR_OPERATIONS_ERROR;
724 static int lldb_start_trans(struct ldb_module *module)
726 /* TODO implement a local transaction mechanism here */
731 static int lldb_end_trans(struct ldb_module *module)
733 /* TODO implement a local transaction mechanism here */
738 static int lldb_del_trans(struct ldb_module *module)
740 /* TODO implement a local transaction mechanism here */
745 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
747 return LDB_ERR_OPERATIONS_ERROR;
750 static const struct ldb_module_ops lldb_ops = {
752 .search = lldb_search,
754 .modify = lldb_modify,
756 .rename = lldb_rename,
757 .request = lldb_request,
758 .start_transaction = lldb_start_trans,
759 .end_transaction = lldb_end_trans,
760 .del_transaction = lldb_del_trans,
765 static int lldb_destructor(struct lldb_private *lldb)
767 ldap_unbind(lldb->ldap);
772 connect to the database
774 static int lldb_connect(struct ldb_context *ldb,
777 const char *options[],
778 struct ldb_module **module)
780 struct lldb_private *lldb = NULL;
784 lldb = talloc(ldb, struct lldb_private);
792 ret = ldap_initialize(&lldb->ldap, url);
793 if (ret != LDAP_SUCCESS) {
794 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_initialize failed for URL '%s' - %s\n",
795 url, ldap_err2string(ret));
799 talloc_set_destructor(lldb, lldb_destructor);
801 ret = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
802 if (ret != LDAP_SUCCESS) {
803 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_set_option failed - %s\n",
804 ldap_err2string(ret));
808 *module = talloc(ldb, struct ldb_module);
814 (*module)->ldb = ldb;
815 (*module)->prev = (*module)->next = NULL;
816 (*module)->private_data = lldb;
817 (*module)->ops = &lldb_ops;
826 int ldb_ldap_init(void)
828 return ldb_register_backend("ldap", lldb_connect) +
829 ldb_register_backend("ldapi", lldb_connect) +
830 ldb_register_backend("ldaps", lldb_connect);