r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / ldb / ldb_ldap / ldb_ldap.c
blobc45fa108f2ec78d5da5cc5c513ec1422b4a50291
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 =
158 (char *)el->values[j].data;
159 mods[num_mods]->mod_vals.modv_bvals[j]->bv_len = el->values[j].length;
161 mods[num_mods]->mod_vals.modv_bvals[j] = NULL;
162 num_mods++;
165 return mods;
167 failed:
168 talloc_free(mods);
169 return NULL;
173 add a single set of ldap message values to a ldb_message
175 static int lldb_add_msg_attr(struct ldb_context *ldb,
176 struct ldb_message *msg,
177 const char *attr, struct berval **bval)
179 int count, i;
180 struct ldb_message_element *el;
182 count = ldap_count_values_len(bval);
184 if (count <= 0) {
185 return -1;
188 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
189 msg->num_elements + 1);
190 if (!el) {
191 errno = ENOMEM;
192 return -1;
195 msg->elements = el;
197 el = &msg->elements[msg->num_elements];
199 el->name = talloc_strdup(msg->elements, attr);
200 if (!el->name) {
201 errno = ENOMEM;
202 return -1;
204 el->flags = 0;
206 el->num_values = 0;
207 el->values = talloc_array(msg->elements, struct ldb_val, count);
208 if (!el->values) {
209 errno = ENOMEM;
210 return -1;
213 for (i=0;i<count;i++) {
214 /* we have to ensure this is null terminated so that
215 ldb_msg_find_attr_as_string() can work */
216 el->values[i].data =
217 (uint8_t *)talloc_size(el->values, bval[i]->bv_len+1);
218 if (!el->values[i].data) {
219 errno = ENOMEM;
220 return -1;
222 memcpy(el->values[i].data, bval[i]->bv_val, bval[i]->bv_len);
223 el->values[i].data[bval[i]->bv_len] = 0;
224 el->values[i].length = bval[i]->bv_len;
225 el->num_values++;
228 msg->num_elements++;
230 return 0;
234 search for matching records
236 static int lldb_search(struct ldb_module *module, struct ldb_request *req)
238 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
239 struct lldb_context *lldb_ac;
240 struct timeval tv;
241 int ldap_scope;
242 char *search_base;
243 char *expression;
244 int ret;
246 if (!req->callback || !req->context) {
247 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
248 return LDB_ERR_OPERATIONS_ERROR;
251 if (req->op.search.tree == NULL) {
252 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
253 return LDB_ERR_OPERATIONS_ERROR;
256 if (req->controls != NULL) {
257 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
260 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
261 if (req->handle == NULL) {
262 return LDB_ERR_OPERATIONS_ERROR;
265 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
267 search_base = ldb_dn_linearize(lldb_ac, req->op.search.base);
268 if (req->op.search.base == NULL) {
269 search_base = talloc_strdup(lldb_ac, "");
271 if (search_base == NULL) {
272 return LDB_ERR_OPERATIONS_ERROR;
275 expression = ldb_filter_from_tree(
276 lldb_ac,
277 discard_const_p(struct ldb_parse_tree, req->op.search.tree));
278 if (expression == NULL) {
279 return LDB_ERR_OPERATIONS_ERROR;
282 switch (req->op.search.scope) {
283 case LDB_SCOPE_BASE:
284 ldap_scope = LDAP_SCOPE_BASE;
285 break;
286 case LDB_SCOPE_ONELEVEL:
287 ldap_scope = LDAP_SCOPE_ONELEVEL;
288 break;
289 default:
290 ldap_scope = LDAP_SCOPE_SUBTREE;
291 break;
294 tv.tv_sec = req->timeout;
295 tv.tv_usec = 0;
297 ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope,
298 expression,
299 discard_const_p(char *, req->op.search.attrs),
301 NULL,
302 NULL,
303 &tv,
304 LDAP_NO_LIMIT,
305 &lldb_ac->msgid);
307 if (ret != LDAP_SUCCESS) {
308 ldb_set_errstring(module->ldb, ldap_err2string(ret));
311 return lldb_ldap_to_ldb(ret);
315 add a record
317 static int lldb_add(struct ldb_module *module, struct ldb_request *req)
319 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
320 struct lldb_context *lldb_ac;
321 LDAPMod **mods;
322 char *dn;
323 int ret;
325 /* ltdb specials should not reach this point */
326 if (ldb_dn_is_special(req->op.add.message->dn)) {
327 return LDB_ERR_INVALID_DN_SYNTAX;
330 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
331 if (req->handle == NULL) {
332 return LDB_ERR_OPERATIONS_ERROR;
335 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
337 mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
338 if (mods == NULL) {
339 return LDB_ERR_OPERATIONS_ERROR;
342 dn = ldb_dn_linearize(lldb_ac, req->op.add.message->dn);
343 if (dn == NULL) {
344 return LDB_ERR_OPERATIONS_ERROR;
347 ret = ldap_add_ext(lldb->ldap, dn, mods,
348 NULL,
349 NULL,
350 &lldb_ac->msgid);
352 if (ret != LDAP_SUCCESS) {
353 ldb_set_errstring(module->ldb, ldap_err2string(ret));
356 return lldb_ldap_to_ldb(ret);
360 modify a record
362 static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
364 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
365 struct lldb_context *lldb_ac;
366 LDAPMod **mods;
367 char *dn;
368 int ret;
370 /* ltdb specials should not reach this point */
371 if (ldb_dn_is_special(req->op.mod.message->dn)) {
372 return LDB_ERR_INVALID_DN_SYNTAX;
375 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
376 if (req->handle == NULL) {
377 return LDB_ERR_OPERATIONS_ERROR;
380 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
382 mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
383 if (mods == NULL) {
384 return LDB_ERR_OPERATIONS_ERROR;
387 dn = ldb_dn_linearize(lldb_ac, req->op.mod.message->dn);
388 if (dn == NULL) {
389 return LDB_ERR_OPERATIONS_ERROR;
392 ret = ldap_modify_ext(lldb->ldap, dn, mods,
393 NULL,
394 NULL,
395 &lldb_ac->msgid);
397 if (ret != LDAP_SUCCESS) {
398 ldb_set_errstring(module->ldb, ldap_err2string(ret));
401 return lldb_ldap_to_ldb(ret);
405 delete a record
407 static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
409 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
410 struct lldb_context *lldb_ac;
411 char *dnstr;
412 int ret;
414 /* ltdb specials should not reach this point */
415 if (ldb_dn_is_special(req->op.del.dn)) {
416 return LDB_ERR_INVALID_DN_SYNTAX;
419 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
420 if (req->handle == NULL) {
421 return LDB_ERR_OPERATIONS_ERROR;
424 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
426 dnstr = ldb_dn_linearize(lldb_ac, req->op.del.dn);
428 ret = ldap_delete_ext(lldb->ldap, dnstr,
429 NULL,
430 NULL,
431 &lldb_ac->msgid);
433 if (ret != LDAP_SUCCESS) {
434 ldb_set_errstring(module->ldb, ldap_err2string(ret));
437 return lldb_ldap_to_ldb(ret);
441 rename a record
443 static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
445 struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
446 struct lldb_context *lldb_ac;
447 char *old_dn;
448 char *newrdn;
449 char *parentdn;
450 int ret;
452 /* ltdb specials should not reach this point */
453 if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
454 return LDB_ERR_INVALID_DN_SYNTAX;
457 req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
458 if (req->handle == NULL) {
459 return LDB_ERR_OPERATIONS_ERROR;
462 lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
464 old_dn = ldb_dn_linearize(lldb_ac, req->op.rename.olddn);
465 if (old_dn == NULL) {
466 return LDB_ERR_OPERATIONS_ERROR;
469 newrdn = talloc_asprintf(lldb_ac, "%s=%s",
470 ldb_dn_get_rdn_name(req->op.rename.newdn),
471 ldb_dn_escape_value(lldb, *(ldb_dn_get_rdn_val(req->op.rename.newdn))));
472 if (!newrdn) {
473 return LDB_ERR_OPERATIONS_ERROR;
476 parentdn = ldb_dn_linearize(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
477 if (!parentdn) {
478 return LDB_ERR_OPERATIONS_ERROR;
481 ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
482 1, NULL, NULL,
483 &lldb_ac->msgid);
485 if (ret != LDAP_SUCCESS) {
486 ldb_set_errstring(module->ldb, ldap_err2string(ret));
489 return lldb_ldap_to_ldb(ret);
492 static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
494 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
495 struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
496 struct ldb_reply *ares = NULL;
497 LDAPMessage *msg;
498 int type;
499 char *matcheddnp = NULL;
500 char *errmsgp = NULL;
501 char **referralsp = NULL;
502 LDAPControl **serverctrlsp = NULL;
503 int ret = LDB_SUCCESS;
505 type = ldap_msgtype(result);
507 handle->status = 0;
509 switch (type) {
511 case LDAP_RES_SEARCH_ENTRY:
512 msg = ldap_first_entry(lldb->ldap, result);
513 if (msg != NULL) {
514 BerElement *berptr = NULL;
515 char *attr, *dn;
517 ares = talloc_zero(ac, struct ldb_reply);
518 if (!ares) {
519 ret = LDB_ERR_OPERATIONS_ERROR;
520 goto error;
523 ares->message = ldb_msg_new(ares);
524 if (!ares->message) {
525 ret = LDB_ERR_OPERATIONS_ERROR;
526 goto error;
529 dn = ldap_get_dn(lldb->ldap, msg);
530 if (!dn) {
531 ret = LDB_ERR_OPERATIONS_ERROR;
532 goto error;
534 ares->message->dn = ldb_dn_explode_or_special(ares->message, dn);
535 if (ares->message->dn == NULL) {
536 ret = LDB_ERR_OPERATIONS_ERROR;
537 goto error;
539 ldap_memfree(dn);
541 ares->message->num_elements = 0;
542 ares->message->elements = NULL;
543 ares->message->private_data = NULL;
545 /* loop over all attributes */
546 for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
547 attr;
548 attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
549 struct berval **bval;
550 bval = ldap_get_values_len(lldb->ldap, msg, attr);
552 if (bval) {
553 lldb_add_msg_attr(ac->module->ldb, ares->message, attr, bval);
554 ldap_value_free_len(bval);
557 if (berptr) ber_free(berptr, 0);
560 ares->type = LDB_REPLY_ENTRY;
561 ret = ac->callback(ac->module->ldb, ac->context, ares);
562 } else {
563 handle->status = LDB_ERR_PROTOCOL_ERROR;
564 handle->state = LDB_ASYNC_DONE;
566 break;
568 case LDAP_RES_SEARCH_REFERENCE:
569 if (ldap_parse_result(lldb->ldap, result, &handle->status,
570 &matcheddnp, &errmsgp,
571 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
572 ret = LDB_ERR_OPERATIONS_ERROR;
573 goto error;
575 if (referralsp == NULL) {
576 handle->status = LDB_ERR_PROTOCOL_ERROR;
577 goto error;
580 ares = talloc_zero(ac, struct ldb_reply);
581 if (!ares) {
582 ret = LDB_ERR_OPERATIONS_ERROR;
583 goto error;
586 ares->referral = talloc_strdup(ares, *referralsp);
587 ares->type = LDB_REPLY_REFERRAL;
588 ret = ac->callback(ac->module->ldb, ac->context, ares);
590 break;
592 case LDAP_RES_SEARCH_RESULT:
593 if (ldap_parse_result(lldb->ldap, result, &handle->status,
594 &matcheddnp, &errmsgp,
595 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
596 handle->status = LDB_ERR_OPERATIONS_ERROR;
597 goto error;
600 ares = talloc_zero(ac, struct ldb_reply);
601 if (!ares) {
602 ret = LDB_ERR_OPERATIONS_ERROR;
603 goto error;
606 if (serverctrlsp != NULL) {
607 /* FIXME: transform the LDAPControl list into an ldb_control one */
608 ares->controls = NULL;
611 ares->type = LDB_REPLY_DONE;
612 handle->state = LDB_ASYNC_DONE;
613 ret = ac->callback(ac->module->ldb, ac->context, ares);
615 break;
617 case LDAP_RES_MODIFY:
618 case LDAP_RES_ADD:
619 case LDAP_RES_DELETE:
620 case LDAP_RES_MODDN:
621 if (ldap_parse_result(lldb->ldap, result, &handle->status,
622 &matcheddnp, &errmsgp,
623 &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
624 handle->status = LDB_ERR_OPERATIONS_ERROR;
625 goto error;
627 if (ac->callback && handle->status == LDB_SUCCESS) {
628 ares = NULL; /* FIXME: build a corresponding ares to pass on */
629 ret = ac->callback(ac->module->ldb, ac->context, ares);
631 handle->state = LDB_ASYNC_DONE;
632 break;
634 default:
635 ret = LDB_ERR_PROTOCOL_ERROR;
636 goto error;
639 if (matcheddnp) ldap_memfree(matcheddnp);
640 if (errmsgp && *errmsgp) {
641 ldb_set_errstring(ac->module->ldb, errmsgp);
642 } else if (handle->status) {
643 ldb_set_errstring(ac->module->ldb, ldap_err2string(handle->status));
645 if (errmsgp) {
646 ldap_memfree(errmsgp);
648 if (referralsp) ldap_value_free(referralsp);
649 if (serverctrlsp) ldap_controls_free(serverctrlsp);
651 ldap_msgfree(result);
652 return lldb_ldap_to_ldb(handle->status);
654 error:
655 handle->state = LDB_ASYNC_DONE;
656 ldap_msgfree(result);
657 return ret;
660 static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
662 struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
663 struct lldb_private *lldb = talloc_get_type(handle->module->private_data, struct lldb_private);
664 struct timeval timeout;
665 LDAPMessage *result;
666 int ret, lret;
668 if (handle->state == LDB_ASYNC_DONE) {
669 return handle->status;
672 if (!ac || !ac->msgid) {
673 return LDB_ERR_OPERATIONS_ERROR;
676 handle->state = LDB_ASYNC_PENDING;
677 handle->status = LDB_SUCCESS;
679 switch(type) {
680 case LDB_WAIT_NONE:
682 if ((ac->timeout != -1) &&
683 ((ac->starttime + ac->timeout) > time(NULL))) {
684 return LDB_ERR_TIME_LIMIT_EXCEEDED;
687 timeout.tv_sec = 0;
688 timeout.tv_usec = 0;
690 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
691 if (lret == -1) {
692 return LDB_ERR_OPERATIONS_ERROR;
694 if (lret == 0) {
695 ret = LDB_SUCCESS;
696 goto done;
699 return lldb_parse_result(handle, result);
701 case LDB_WAIT_ALL:
702 timeout.tv_usec = 0;
703 ret = LDB_ERR_OPERATIONS_ERROR;
705 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
707 if (ac->timeout == -1) {
708 lret = ldap_result(lldb->ldap, ac->msgid, 0, NULL, &result);
709 } else {
710 timeout.tv_sec = ac->timeout - (time(NULL) - ac->starttime);
711 if (timeout.tv_sec <= 0)
712 return LDB_ERR_TIME_LIMIT_EXCEEDED;
713 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
715 if (lret == -1) {
716 return LDB_ERR_OPERATIONS_ERROR;
718 if (lret == 0) {
719 return LDB_ERR_TIME_LIMIT_EXCEEDED;
722 ret = lldb_parse_result(handle, result);
723 if (ret != LDB_SUCCESS) {
724 return ret;
728 break;
730 default:
731 handle->state = LDB_ASYNC_DONE;
732 ret = LDB_ERR_OPERATIONS_ERROR;
735 done:
736 return ret;
739 static int lldb_start_trans(struct ldb_module *module)
741 /* TODO implement a local transaction mechanism here */
743 return LDB_SUCCESS;
746 static int lldb_end_trans(struct ldb_module *module)
748 /* TODO implement a local transaction mechanism here */
750 return LDB_SUCCESS;
753 static int lldb_del_trans(struct ldb_module *module)
755 /* TODO implement a local transaction mechanism here */
757 return LDB_SUCCESS;
760 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
762 return LDB_ERR_OPERATIONS_ERROR;
765 static const struct ldb_module_ops lldb_ops = {
766 .name = "ldap",
767 .search = lldb_search,
768 .add = lldb_add,
769 .modify = lldb_modify,
770 .del = lldb_delete,
771 .rename = lldb_rename,
772 .request = lldb_request,
773 .start_transaction = lldb_start_trans,
774 .end_transaction = lldb_end_trans,
775 .del_transaction = lldb_del_trans,
776 .wait = lldb_wait
780 static int lldb_destructor(struct lldb_private *lldb)
782 ldap_unbind(lldb->ldap);
783 return 0;
787 connect to the database
789 static int lldb_connect(struct ldb_context *ldb,
790 const char *url,
791 unsigned int flags,
792 const char *options[],
793 struct ldb_module **module)
795 struct lldb_private *lldb = NULL;
796 int version = 3;
797 int ret;
799 lldb = talloc(ldb, struct lldb_private);
800 if (!lldb) {
801 ldb_oom(ldb);
802 goto failed;
805 lldb->ldap = NULL;
807 ret = ldap_initialize(&lldb->ldap, url);
808 if (ret != LDAP_SUCCESS) {
809 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_initialize failed for URL '%s' - %s\n",
810 url, ldap_err2string(ret));
811 goto failed;
814 talloc_set_destructor(lldb, lldb_destructor);
816 ret = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
817 if (ret != LDAP_SUCCESS) {
818 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_set_option failed - %s\n",
819 ldap_err2string(ret));
820 goto failed;
823 *module = talloc(ldb, struct ldb_module);
824 if (*module == NULL) {
825 ldb_oom(ldb);
826 talloc_free(lldb);
827 return -1;
829 talloc_set_name_const(*module, "ldb_ldap backend");
830 (*module)->ldb = ldb;
831 (*module)->prev = (*module)->next = NULL;
832 (*module)->private_data = lldb;
833 (*module)->ops = &lldb_ops;
835 return 0;
837 failed:
838 talloc_free(lldb);
839 return -1;
842 int ldb_ldap_init(void)
844 return ldb_register_backend("ldap", lldb_connect) +
845 ldb_register_backend("ldapi", lldb_connect) +
846 ldb_register_backend("ldaps", lldb_connect);