r23798: updated old Temple Place FSF addresses to new URL
[Samba.git] / source / lib / ldb / ldb_ldap / ldb_ldap.c
blob51445d651feb99ff75de7ced2cf2f91ba531a723
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 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/>.
26 * Name: ldb_ldap
28 * Component: ldb ldap backend
30 * Description: core files for LDAP backend
32 * Author: Andrew Tridgell
34 * Modifications:
36 * - description: make the module use asyncronous calls
37 * date: Feb 2006
38 * author: Simo Sorce
41 #include "includes.h"
42 #include "ldb/include/includes.h"
44 #define LDAP_DEPRECATED 1
45 #include <ldap.h>
47 struct lldb_private {
48 LDAP *ldap;
51 struct lldb_context {
52 struct ldb_module *module;
53 int msgid;
54 int timeout;
55 time_t starttime;
56 void *context;
57 int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
60 static int lldb_ldap_to_ldb(int err) {
61 /* Ldap errors and ldb errors are defined to the same values */
62 return err;
65 static struct ldb_handle *init_handle(struct lldb_private *lldb, struct ldb_module *module,
66 void *context,
67 int (*callback)(struct ldb_context *, void *, struct ldb_reply *),
68 int timeout, time_t starttime)
70 struct lldb_context *ac;
71 struct ldb_handle *h;
73 h = talloc_zero(lldb, struct ldb_handle);
74 if (h == NULL) {
75 ldb_set_errstring(module->ldb, "Out of Memory");
76 return NULL;
79 h->module = module;
81 ac = talloc(h, struct lldb_context);
82 if (ac == NULL) {
83 ldb_set_errstring(module->ldb, "Out of Memory");
84 talloc_free(h);
85 return NULL;
88 h->private_data = (void *)ac;
90 h->state = LDB_ASYNC_INIT;
91 h->status = LDB_SUCCESS;
93 ac->module = module;
94 ac->context = context;
95 ac->callback = callback;
96 ac->timeout = timeout;
97 ac->starttime = starttime;
98 ac->msgid = 0;
100 return h;
103 convert a ldb_message structure to a list of LDAPMod structures
104 ready for ldap_add() or ldap_modify()
106 static LDAPMod **lldb_msg_to_mods(void *mem_ctx, const struct ldb_message *msg, int use_flags)
108 LDAPMod **mods;
109 unsigned int i, j;
110 int num_mods = 0;
112 /* allocate maximum number of elements needed */
113 mods = talloc_array(mem_ctx, LDAPMod *, msg->num_elements+1);
114 if (!mods) {
115 errno = ENOMEM;
116 return NULL;
118 mods[0] = NULL;
120 for (i=0;i<msg->num_elements;i++) {
121 const struct ldb_message_element *el = &msg->elements[i];
123 mods[num_mods] = talloc(mods, LDAPMod);
124 if (!mods[num_mods]) {
125 goto failed;
127 mods[num_mods+1] = NULL;
128 mods[num_mods]->mod_op = LDAP_MOD_BVALUES;
129 if (use_flags) {
130 switch (el->flags & LDB_FLAG_MOD_MASK) {
131 case LDB_FLAG_MOD_ADD:
132 mods[num_mods]->mod_op |= LDAP_MOD_ADD;
133 break;
134 case LDB_FLAG_MOD_DELETE:
135 mods[num_mods]->mod_op |= LDAP_MOD_DELETE;
136 break;
137 case LDB_FLAG_MOD_REPLACE:
138 mods[num_mods]->mod_op |= LDAP_MOD_REPLACE;
139 break;
142 mods[num_mods]->mod_type = discard_const_p(char, el->name);
143 mods[num_mods]->mod_vals.modv_bvals = talloc_array(mods[num_mods],
144 struct berval *,
145 1+el->num_values);
146 if (!mods[num_mods]->mod_vals.modv_bvals) {
147 goto failed;
150 for (j=0;j<el->num_values;j++) {
151 mods[num_mods]->mod_vals.modv_bvals[j] = talloc(mods[num_mods]->mod_vals.modv_bvals,
152 struct berval);
153 if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
154 goto failed;
156 mods[num_mods]->mod_vals.modv_bvals[j]->bv_val =
157 (char *)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 =
216 (uint8_t *)talloc_size(el->values, bval[i]->bv_len+1);
217 if (!el->values[i].data) {
218 errno = ENOMEM;
219 return -1;
221 memcpy(el->values[i].data, bval[i]->bv_val, bval[i]->bv_len);
222 el->values[i].data[bval[i]->bv_len] = 0;
223 el->values[i].length = bval[i]->bv_len;
224 el->num_values++;
227 msg->num_elements++;
229 return 0;
233 search for matching records
235 static int lldb_search(struct ldb_module *module, struct ldb_request *req)
237 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
238 struct lldb_context *lldb_ac;
239 struct timeval tv;
240 int ldap_scope;
241 char *search_base;
242 char *expression;
243 int ret;
245 if (!req->callback || !req->context) {
246 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
247 return LDB_ERR_OPERATIONS_ERROR;
250 if (req->op.search.tree == NULL) {
251 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
252 return LDB_ERR_OPERATIONS_ERROR;
255 if (req->controls != NULL) {
256 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
259 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
260 if (req->handle == NULL) {
261 return LDB_ERR_OPERATIONS_ERROR;
264 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
266 search_base = ldb_dn_linearize(lldb_ac, req->op.search.base);
267 if (req->op.search.base == NULL) {
268 search_base = talloc_strdup(lldb_ac, "");
270 if (search_base == NULL) {
271 return LDB_ERR_OPERATIONS_ERROR;
274 expression = ldb_filter_from_tree(
275 lldb_ac,
276 discard_const_p(struct ldb_parse_tree, req->op.search.tree));
277 if (expression == NULL) {
278 return LDB_ERR_OPERATIONS_ERROR;
281 switch (req->op.search.scope) {
282 case LDB_SCOPE_BASE:
283 ldap_scope = LDAP_SCOPE_BASE;
284 break;
285 case LDB_SCOPE_ONELEVEL:
286 ldap_scope = LDAP_SCOPE_ONELEVEL;
287 break;
288 default:
289 ldap_scope = LDAP_SCOPE_SUBTREE;
290 break;
293 tv.tv_sec = req->timeout;
294 tv.tv_usec = 0;
296 ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope,
297 expression,
298 discard_const_p(char *, req->op.search.attrs),
300 NULL,
301 NULL,
302 &tv,
303 LDAP_NO_LIMIT,
304 &lldb_ac->msgid);
306 if (ret != LDAP_SUCCESS) {
307 ldb_set_errstring(module->ldb, ldap_err2string(ret));
310 return lldb_ldap_to_ldb(ret);
314 add a record
316 static int lldb_add(struct ldb_module *module, struct ldb_request *req)
318 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
319 struct lldb_context *lldb_ac;
320 LDAPMod **mods;
321 char *dn;
322 int ret;
324 /* ltdb specials should not reach this point */
325 if (ldb_dn_is_special(req->op.add.message->dn)) {
326 return LDB_ERR_INVALID_DN_SYNTAX;
329 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
330 if (req->handle == NULL) {
331 return LDB_ERR_OPERATIONS_ERROR;
334 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
336 mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
337 if (mods == NULL) {
338 return LDB_ERR_OPERATIONS_ERROR;
341 dn = ldb_dn_linearize(lldb_ac, req->op.add.message->dn);
342 if (dn == NULL) {
343 return LDB_ERR_OPERATIONS_ERROR;
346 ret = ldap_add_ext(lldb->ldap, dn, mods,
347 NULL,
348 NULL,
349 &lldb_ac->msgid);
351 if (ret != LDAP_SUCCESS) {
352 ldb_set_errstring(module->ldb, ldap_err2string(ret));
355 return lldb_ldap_to_ldb(ret);
359 modify a record
361 static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
363 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
364 struct lldb_context *lldb_ac;
365 LDAPMod **mods;
366 char *dn;
367 int ret;
369 /* ltdb specials should not reach this point */
370 if (ldb_dn_is_special(req->op.mod.message->dn)) {
371 return LDB_ERR_INVALID_DN_SYNTAX;
374 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
375 if (req->handle == NULL) {
376 return LDB_ERR_OPERATIONS_ERROR;
379 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
381 mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
382 if (mods == NULL) {
383 return LDB_ERR_OPERATIONS_ERROR;
386 dn = ldb_dn_linearize(lldb_ac, req->op.mod.message->dn);
387 if (dn == NULL) {
388 return LDB_ERR_OPERATIONS_ERROR;
391 ret = ldap_modify_ext(lldb->ldap, dn, mods,
392 NULL,
393 NULL,
394 &lldb_ac->msgid);
396 if (ret != LDAP_SUCCESS) {
397 ldb_set_errstring(module->ldb, ldap_err2string(ret));
400 return lldb_ldap_to_ldb(ret);
404 delete a record
406 static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
408 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
409 struct lldb_context *lldb_ac;
410 char *dnstr;
411 int ret;
413 /* ltdb specials should not reach this point */
414 if (ldb_dn_is_special(req->op.del.dn)) {
415 return LDB_ERR_INVALID_DN_SYNTAX;
418 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
419 if (req->handle == NULL) {
420 return LDB_ERR_OPERATIONS_ERROR;
423 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
425 dnstr = ldb_dn_linearize(lldb_ac, req->op.del.dn);
427 ret = ldap_delete_ext(lldb->ldap, dnstr,
428 NULL,
429 NULL,
430 &lldb_ac->msgid);
432 if (ret != LDAP_SUCCESS) {
433 ldb_set_errstring(module->ldb, ldap_err2string(ret));
436 return lldb_ldap_to_ldb(ret);
440 rename a record
442 static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
444 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
445 struct lldb_context *lldb_ac;
446 char *old_dn;
447 char *newrdn;
448 char *parentdn;
449 int ret;
451 /* ltdb specials should not reach this point */
452 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
453 return LDB_ERR_INVALID_DN_SYNTAX;
456 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
457 if (req->handle == NULL) {
458 return LDB_ERR_OPERATIONS_ERROR;
461 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
463 old_dn = ldb_dn_linearize(lldb_ac, req->op.rename.olddn);
464 if (old_dn == NULL) {
465 return LDB_ERR_OPERATIONS_ERROR;
468 newrdn = talloc_asprintf(lldb_ac, "%s=%s",
469 ldb_dn_get_rdn_name(req->op.rename.newdn),
470 ldb_dn_escape_value(lldb, *(ldb_dn_get_rdn_val(req->op.rename.newdn))));
471 if (!newrdn) {
472 return LDB_ERR_OPERATIONS_ERROR;
475 parentdn = ldb_dn_linearize(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
476 if (!parentdn) {
477 return LDB_ERR_OPERATIONS_ERROR;
480 ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
481 1, NULL, NULL,
482 &lldb_ac->msgid);
484 if (ret != LDAP_SUCCESS) {
485 ldb_set_errstring(module->ldb, ldap_err2string(ret));
488 return lldb_ldap_to_ldb(ret);
491 static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
493 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
494 struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
495 struct ldb_reply *ares = NULL;
496 LDAPMessage *msg;
497 int type;
498 char *matcheddnp = NULL;
499 char *errmsgp = NULL;
500 char **referralsp = NULL;
501 LDAPControl **serverctrlsp = NULL;
502 int ret = LDB_SUCCESS;
504 type = ldap_msgtype(result);
506 handle->status = 0;
508 switch (type) {
510 case LDAP_RES_SEARCH_ENTRY:
511 msg = ldap_first_entry(lldb->ldap, result);
512 if (msg != NULL) {
513 BerElement *berptr = NULL;
514 char *attr, *dn;
516 ares = talloc_zero(ac, struct ldb_reply);
517 if (!ares) {
518 ret = LDB_ERR_OPERATIONS_ERROR;
519 goto error;
522 ares->message = ldb_msg_new(ares);
523 if (!ares->message) {
524 ret = LDB_ERR_OPERATIONS_ERROR;
525 goto error;
528 dn = ldap_get_dn(lldb->ldap, msg);
529 if (!dn) {
530 ret = LDB_ERR_OPERATIONS_ERROR;
531 goto error;
533 ares->message->dn = ldb_dn_explode_or_special(ares->message, dn);
534 if (ares->message->dn == NULL) {
535 ret = LDB_ERR_OPERATIONS_ERROR;
536 goto error;
538 ldap_memfree(dn);
540 ares->message->num_elements = 0;
541 ares->message->elements = NULL;
542 ares->message->private_data = NULL;
544 /* loop over all attributes */
545 for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
546 attr;
547 attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
548 struct berval **bval;
549 bval = ldap_get_values_len(lldb->ldap, msg, attr);
551 if (bval) {
552 lldb_add_msg_attr(ac->module->ldb, ares->message, attr, bval);
553 ldap_value_free_len(bval);
556 if (berptr) ber_free(berptr, 0);
559 ares->type = LDB_REPLY_ENTRY;
560 ret = ac->callback(ac->module->ldb, ac->context, ares);
561 } else {
562 handle->status = LDB_ERR_PROTOCOL_ERROR;
563 handle->state = LDB_ASYNC_DONE;
565 break;
567 case LDAP_RES_SEARCH_REFERENCE:
568 if (ldap_parse_result(lldb->ldap, result, &handle->status,
569 &matcheddnp, &errmsgp,
570 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
571 ret = LDB_ERR_OPERATIONS_ERROR;
572 goto error;
574 if (referralsp == NULL) {
575 handle->status = LDB_ERR_PROTOCOL_ERROR;
576 goto error;
579 ares = talloc_zero(ac, struct ldb_reply);
580 if (!ares) {
581 ret = LDB_ERR_OPERATIONS_ERROR;
582 goto error;
585 ares->referral = talloc_strdup(ares, *referralsp);
586 ares->type = LDB_REPLY_REFERRAL;
587 ret = ac->callback(ac->module->ldb, ac->context, ares);
589 break;
591 case LDAP_RES_SEARCH_RESULT:
592 if (ldap_parse_result(lldb->ldap, result, &handle->status,
593 &matcheddnp, &errmsgp,
594 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
595 handle->status = LDB_ERR_OPERATIONS_ERROR;
596 goto error;
599 ares = talloc_zero(ac, struct ldb_reply);
600 if (!ares) {
601 ret = LDB_ERR_OPERATIONS_ERROR;
602 goto error;
605 if (serverctrlsp != NULL) {
606 /* FIXME: transform the LDAPControl list into an ldb_control one */
607 ares->controls = NULL;
610 ares->type = LDB_REPLY_DONE;
611 handle->state = LDB_ASYNC_DONE;
612 ret = ac->callback(ac->module->ldb, ac->context, ares);
614 break;
616 case LDAP_RES_MODIFY:
617 case LDAP_RES_ADD:
618 case LDAP_RES_DELETE:
619 case LDAP_RES_MODDN:
620 if (ldap_parse_result(lldb->ldap, result, &handle->status,
621 &matcheddnp, &errmsgp,
622 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
623 handle->status = LDB_ERR_OPERATIONS_ERROR;
624 goto error;
626 if (ac->callback && handle->status == LDB_SUCCESS) {
627 ares = NULL; /* FIXME: build a corresponding ares to pass on */
628 ret = ac->callback(ac->module->ldb, ac->context, ares);
630 handle->state = LDB_ASYNC_DONE;
631 break;
633 default:
634 ret = LDB_ERR_PROTOCOL_ERROR;
635 goto error;
638 if (matcheddnp) ldap_memfree(matcheddnp);
639 if (errmsgp && *errmsgp) {
640 ldb_set_errstring(ac->module->ldb, errmsgp);
641 } else if (handle->status) {
642 ldb_set_errstring(ac->module->ldb, ldap_err2string(handle->status));
644 if (errmsgp) {
645 ldap_memfree(errmsgp);
647 if (referralsp) ldap_value_free(referralsp);
648 if (serverctrlsp) ldap_controls_free(serverctrlsp);
650 ldap_msgfree(result);
651 return lldb_ldap_to_ldb(handle->status);
653 error:
654 handle->state = LDB_ASYNC_DONE;
655 ldap_msgfree(result);
656 return ret;
659 static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
661 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
662 struct lldb_private *lldb = talloc_get_type(handle->module->private_data, struct lldb_private);
663 struct timeval timeout;
664 LDAPMessage *result;
665 int ret, lret;
667 if (handle->state == LDB_ASYNC_DONE) {
668 return handle->status;
671 if (!ac || !ac->msgid) {
672 return LDB_ERR_OPERATIONS_ERROR;
675 handle->state = LDB_ASYNC_PENDING;
676 handle->status = LDB_SUCCESS;
678 switch(type) {
679 case LDB_WAIT_NONE:
681 if ((ac->timeout != -1) &&
682 ((ac->starttime + ac->timeout) > time(NULL))) {
683 return LDB_ERR_TIME_LIMIT_EXCEEDED;
686 timeout.tv_sec = 0;
687 timeout.tv_usec = 0;
689 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
690 if (lret == -1) {
691 return LDB_ERR_OPERATIONS_ERROR;
693 if (lret == 0) {
694 ret = LDB_SUCCESS;
695 goto done;
698 return lldb_parse_result(handle, result);
700 case LDB_WAIT_ALL:
701 timeout.tv_usec = 0;
702 ret = LDB_ERR_OPERATIONS_ERROR;
704 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
706 if (ac->timeout == -1) {
707 lret = ldap_result(lldb->ldap, ac->msgid, 0, NULL, &result);
708 } else {
709 timeout.tv_sec = ac->timeout - (time(NULL) - ac->starttime);
710 if (timeout.tv_sec <= 0)
711 return LDB_ERR_TIME_LIMIT_EXCEEDED;
712 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
714 if (lret == -1) {
715 return LDB_ERR_OPERATIONS_ERROR;
717 if (lret == 0) {
718 return LDB_ERR_TIME_LIMIT_EXCEEDED;
721 ret = lldb_parse_result(handle, result);
722 if (ret != LDB_SUCCESS) {
723 return ret;
727 break;
729 default:
730 handle->state = LDB_ASYNC_DONE;
731 ret = LDB_ERR_OPERATIONS_ERROR;
734 done:
735 return ret;
738 static int lldb_start_trans(struct ldb_module *module)
740 /* TODO implement a local transaction mechanism here */
742 return LDB_SUCCESS;
745 static int lldb_end_trans(struct ldb_module *module)
747 /* TODO implement a local transaction mechanism here */
749 return LDB_SUCCESS;
752 static int lldb_del_trans(struct ldb_module *module)
754 /* TODO implement a local transaction mechanism here */
756 return LDB_SUCCESS;
759 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
761 return LDB_ERR_OPERATIONS_ERROR;
764 static const struct ldb_module_ops lldb_ops = {
765 .name = "ldap",
766 .search = lldb_search,
767 .add = lldb_add,
768 .modify = lldb_modify,
769 .del = lldb_delete,
770 .rename = lldb_rename,
771 .request = lldb_request,
772 .start_transaction = lldb_start_trans,
773 .end_transaction = lldb_end_trans,
774 .del_transaction = lldb_del_trans,
775 .wait = lldb_wait
779 static int lldb_destructor(struct lldb_private *lldb)
781 ldap_unbind(lldb->ldap);
782 return 0;
786 connect to the database
788 static int lldb_connect(struct ldb_context *ldb,
789 const char *url,
790 unsigned int flags,
791 const char *options[],
792 struct ldb_module **module)
794 struct lldb_private *lldb = NULL;
795 int version = 3;
796 int ret;
798 lldb = talloc(ldb, struct lldb_private);
799 if (!lldb) {
800 ldb_oom(ldb);
801 goto failed;
804 lldb->ldap = NULL;
806 ret = ldap_initialize(&lldb->ldap, url);
807 if (ret != LDAP_SUCCESS) {
808 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_initialize failed for URL '%s' - %s\n",
809 url, ldap_err2string(ret));
810 goto failed;
813 talloc_set_destructor(lldb, lldb_destructor);
815 ret = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
816 if (ret != LDAP_SUCCESS) {
817 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_set_option failed - %s\n",
818 ldap_err2string(ret));
819 goto failed;
822 *module = talloc(ldb, struct ldb_module);
823 if (*module == NULL) {
824 ldb_oom(ldb);
825 talloc_free(lldb);
826 return -1;
828 talloc_set_name_const(*module, "ldb_ldap backend");
829 (*module)->ldb = ldb;
830 (*module)->prev = (*module)->next = NULL;
831 (*module)->private_data = lldb;
832 (*module)->ops = &lldb_ops;
834 return 0;
836 failed:
837 talloc_free(lldb);
838 return -1;
841 int ldb_ldap_init(void)
843 return ldb_register_backend("ldap", lldb_connect) +
844 ldb_register_backend("ldapi", lldb_connect) +
845 ldb_register_backend("ldaps", lldb_connect);