r19955: I'll leave that to others. I *knew* I should not have touchec this
[Samba.git] / source / lib / ldb / ldb_ldap / ldb_ldap.c
blobcdc1a500f80bb5f3c625a6ba81461d905ef56fa9
1 /*
2 ldb database library
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
9 ** under the LGPL
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
27 * Name: ldb_ldap
29 * Component: ldb ldap backend
31 * Description: core files for LDAP backend
33 * Author: Andrew Tridgell
35 * Modifications:
37 * - description: make the module use asyncronous calls
38 * date: Feb 2006
39 * author: Simo Sorce
42 #include "includes.h"
43 #include "ldb/include/includes.h"
45 #define LDAP_DEPRECATED 1
46 #include <ldap.h>
48 struct lldb_private {
49 LDAP *ldap;
52 struct lldb_context {
53 struct ldb_module *module;
54 int msgid;
55 int timeout;
56 time_t starttime;
57 void *context;
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 */
63 return err;
66 static struct ldb_handle *init_handle(struct lldb_private *lldb, struct ldb_module *module,
67 void *context,
68 int (*callback)(struct ldb_context *, void *, struct ldb_reply *),
69 int timeout, time_t starttime)
71 struct lldb_context *ac;
72 struct ldb_handle *h;
74 h = talloc_zero(lldb, struct ldb_handle);
75 if (h == NULL) {
76 ldb_set_errstring(module->ldb, "Out of Memory");
77 return NULL;
80 h->module = module;
82 ac = talloc(h, struct lldb_context);
83 if (ac == NULL) {
84 ldb_set_errstring(module->ldb, "Out of Memory");
85 talloc_free(h);
86 return NULL;
89 h->private_data = (void *)ac;
91 h->state = LDB_ASYNC_INIT;
92 h->status = LDB_SUCCESS;
94 ac->module = module;
95 ac->context = context;
96 ac->callback = callback;
97 ac->timeout = timeout;
98 ac->starttime = starttime;
99 ac->msgid = 0;
101 return h;
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)
109 LDAPMod **mods;
110 unsigned int i, j;
111 int num_mods = 0;
113 /* allocate maximum number of elements needed */
114 mods = talloc_array(mem_ctx, LDAPMod *, msg->num_elements+1);
115 if (!mods) {
116 errno = ENOMEM;
117 return NULL;
119 mods[0] = NULL;
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]) {
126 goto failed;
128 mods[num_mods+1] = NULL;
129 mods[num_mods]->mod_op = LDAP_MOD_BVALUES;
130 if (use_flags) {
131 switch (el->flags & LDB_FLAG_MOD_MASK) {
132 case LDB_FLAG_MOD_ADD:
133 mods[num_mods]->mod_op |= LDAP_MOD_ADD;
134 break;
135 case LDB_FLAG_MOD_DELETE:
136 mods[num_mods]->mod_op |= LDAP_MOD_DELETE;
137 break;
138 case LDB_FLAG_MOD_REPLACE:
139 mods[num_mods]->mod_op |= LDAP_MOD_REPLACE;
140 break;
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],
145 struct berval *,
146 1+el->num_values);
147 if (!mods[num_mods]->mod_vals.modv_bvals) {
148 goto failed;
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,
153 struct berval);
154 if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
155 goto failed;
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;
161 num_mods++;
164 return mods;
166 failed:
167 talloc_free(mods);
168 return 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)
178 int count, i;
179 struct ldb_message_element *el;
181 count = ldap_count_values_len(bval);
183 if (count <= 0) {
184 return -1;
187 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
188 msg->num_elements + 1);
189 if (!el) {
190 errno = ENOMEM;
191 return -1;
194 msg->elements = el;
196 el = &msg->elements[msg->num_elements];
198 el->name = talloc_strdup(msg->elements, attr);
199 if (!el->name) {
200 errno = ENOMEM;
201 return -1;
203 el->flags = 0;
205 el->num_values = 0;
206 el->values = talloc_array(msg->elements, struct ldb_val, count);
207 if (!el->values) {
208 errno = ENOMEM;
209 return -1;
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) {
217 errno = ENOMEM;
218 return -1;
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;
223 el->num_values++;
226 msg->num_elements++;
228 return 0;
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;
238 struct timeval tv;
239 int ldap_scope;
240 char *search_base;
241 char *expression;
242 int ret;
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_linearize(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) {
279 case LDB_SCOPE_BASE:
280 ldap_scope = LDAP_SCOPE_BASE;
281 break;
282 case LDB_SCOPE_ONELEVEL:
283 ldap_scope = LDAP_SCOPE_ONELEVEL;
284 break;
285 default:
286 ldap_scope = LDAP_SCOPE_SUBTREE;
287 break;
290 tv.tv_sec = req->timeout;
291 tv.tv_usec = 0;
293 ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope,
294 expression,
295 discard_const_p(char *, req->op.search.attrs),
297 NULL,
298 NULL,
299 &tv,
300 LDAP_NO_LIMIT,
301 &lldb_ac->msgid);
303 if (ret != LDAP_SUCCESS) {
304 ldb_set_errstring(module->ldb, ldap_err2string(ret));
307 return lldb_ldap_to_ldb(ret);
311 add a record
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;
317 LDAPMod **mods;
318 char *dn;
319 int ret;
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);
334 if (mods == NULL) {
335 return LDB_ERR_OPERATIONS_ERROR;
338 dn = ldb_dn_linearize(lldb_ac, req->op.add.message->dn);
339 if (dn == NULL) {
340 return LDB_ERR_OPERATIONS_ERROR;
343 ret = ldap_add_ext(lldb->ldap, dn, mods,
344 NULL,
345 NULL,
346 &lldb_ac->msgid);
348 if (ret != LDAP_SUCCESS) {
349 ldb_set_errstring(module->ldb, ldap_err2string(ret));
352 return lldb_ldap_to_ldb(ret);
356 modify a record
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;
362 LDAPMod **mods;
363 char *dn;
364 int ret;
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);
379 if (mods == NULL) {
380 return LDB_ERR_OPERATIONS_ERROR;
383 dn = ldb_dn_linearize(lldb_ac, req->op.mod.message->dn);
384 if (dn == NULL) {
385 return LDB_ERR_OPERATIONS_ERROR;
388 ret = ldap_modify_ext(lldb->ldap, dn, mods,
389 NULL,
390 NULL,
391 &lldb_ac->msgid);
393 if (ret != LDAP_SUCCESS) {
394 ldb_set_errstring(module->ldb, ldap_err2string(ret));
397 return lldb_ldap_to_ldb(ret);
401 delete a record
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;
407 char *dnstr;
408 int ret;
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_linearize(lldb_ac, req->op.del.dn);
424 ret = ldap_delete_ext(lldb->ldap, dnstr,
425 NULL,
426 NULL,
427 &lldb_ac->msgid);
429 if (ret != LDAP_SUCCESS) {
430 ldb_set_errstring(module->ldb, ldap_err2string(ret));
433 return lldb_ldap_to_ldb(ret);
437 rename a record
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;
443 char *old_dn;
444 char *newrdn;
445 char *parentdn;
446 int ret;
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_linearize(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))));
468 if (!newrdn) {
469 return LDB_ERR_OPERATIONS_ERROR;
472 parentdn = ldb_dn_linearize(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
473 if (!parentdn) {
474 return LDB_ERR_OPERATIONS_ERROR;
477 ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
478 1, NULL, NULL,
479 &lldb_ac->msgid);
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;
493 LDAPMessage *msg;
494 int type;
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);
503 handle->status = 0;
505 switch (type) {
507 case LDAP_RES_SEARCH_ENTRY:
508 msg = ldap_first_entry(lldb->ldap, result);
509 if (msg != NULL) {
510 BerElement *berptr = NULL;
511 char *attr, *dn;
513 ares = talloc_zero(ac, struct ldb_reply);
514 if (!ares) {
515 ret = LDB_ERR_OPERATIONS_ERROR;
516 goto error;
519 ares->message = ldb_msg_new(ares);
520 if (!ares->message) {
521 ret = LDB_ERR_OPERATIONS_ERROR;
522 goto error;
525 dn = ldap_get_dn(lldb->ldap, msg);
526 if (!dn) {
527 ret = LDB_ERR_OPERATIONS_ERROR;
528 goto error;
530 ares->message->dn = ldb_dn_explode_or_special(ares->message, dn);
531 if (ares->message->dn == NULL) {
532 ret = LDB_ERR_OPERATIONS_ERROR;
533 goto error;
535 ldap_memfree(dn);
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);
543 attr;
544 attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
545 struct berval **bval;
546 bval = ldap_get_values_len(lldb->ldap, msg, attr);
548 if (bval) {
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);
558 } else {
559 handle->status = LDB_ERR_PROTOCOL_ERROR;
560 handle->state = LDB_ASYNC_DONE;
562 break;
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;
569 goto error;
571 if (referralsp == NULL) {
572 handle->status = LDB_ERR_PROTOCOL_ERROR;
573 goto error;
576 ares = talloc_zero(ac, struct ldb_reply);
577 if (!ares) {
578 ret = LDB_ERR_OPERATIONS_ERROR;
579 goto error;
582 ares->referral = talloc_strdup(ares, *referralsp);
583 ares->type = LDB_REPLY_REFERRAL;
584 ret = ac->callback(ac->module->ldb, ac->context, ares);
586 break;
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;
593 goto error;
596 ares = talloc_zero(ac, struct ldb_reply);
597 if (!ares) {
598 ret = LDB_ERR_OPERATIONS_ERROR;
599 goto 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);
611 break;
613 case LDAP_RES_MODIFY:
614 case LDAP_RES_ADD:
615 case LDAP_RES_DELETE:
616 case LDAP_RES_MODDN:
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;
621 goto 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;
628 break;
630 default:
631 ret = LDB_ERR_PROTOCOL_ERROR;
632 goto 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));
641 if (errmsgp) {
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);
650 error:
651 handle->state = LDB_ASYNC_DONE;
652 ldap_msgfree(result);
653 return ret;
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;
661 LDAPMessage *result;
662 int ret, lret;
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;
675 switch(type) {
676 case LDB_WAIT_NONE:
678 if ((ac->timeout != -1) &&
679 ((ac->starttime + ac->timeout) > time(NULL))) {
680 return LDB_ERR_TIME_LIMIT_EXCEEDED;
683 timeout.tv_sec = 0;
684 timeout.tv_usec = 0;
686 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
687 if (lret == -1) {
688 return LDB_ERR_OPERATIONS_ERROR;
690 if (lret == 0) {
691 ret = LDB_SUCCESS;
692 goto done;
695 return lldb_parse_result(handle, result);
697 case LDB_WAIT_ALL:
698 timeout.tv_usec = 0;
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);
705 } else {
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);
711 if (lret == -1) {
712 return LDB_ERR_OPERATIONS_ERROR;
714 if (lret == 0) {
715 return LDB_ERR_TIME_LIMIT_EXCEEDED;
718 ret = lldb_parse_result(handle, result);
719 if (ret != LDB_SUCCESS) {
720 return ret;
724 break;
726 default:
727 handle->state = LDB_ASYNC_DONE;
728 ret = LDB_ERR_OPERATIONS_ERROR;
731 done:
732 return ret;
735 static int lldb_start_trans(struct ldb_module *module)
737 /* TODO implement a local transaction mechanism here */
739 return LDB_SUCCESS;
742 static int lldb_end_trans(struct ldb_module *module)
744 /* TODO implement a local transaction mechanism here */
746 return LDB_SUCCESS;
749 static int lldb_del_trans(struct ldb_module *module)
751 /* TODO implement a local transaction mechanism here */
753 return LDB_SUCCESS;
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 = {
762 .name = "ldap",
763 .search = lldb_search,
764 .add = lldb_add,
765 .modify = lldb_modify,
766 .del = lldb_delete,
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,
772 .wait = lldb_wait
776 static int lldb_destructor(struct lldb_private *lldb)
778 ldap_unbind(lldb->ldap);
779 return 0;
783 connect to the database
785 static int lldb_connect(struct ldb_context *ldb,
786 const char *url,
787 unsigned int flags,
788 const char *options[],
789 struct ldb_module **module)
791 struct lldb_private *lldb = NULL;
792 int version = 3;
793 int ret;
795 lldb = talloc(ldb, struct lldb_private);
796 if (!lldb) {
797 ldb_oom(ldb);
798 goto failed;
801 lldb->ldap = NULL;
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));
807 goto failed;
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));
816 goto failed;
819 *module = talloc(ldb, struct ldb_module);
820 if (*module == NULL) {
821 ldb_oom(ldb);
822 talloc_free(lldb);
823 return -1;
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;
831 return 0;
833 failed:
834 talloc_free(lldb);
835 return -1;
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);