r20190: fix the ldb_ldap backend
[kai/samba.git] / source4 / lib / ldb / ldb_ldap / ldb_ldap.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5    Copyright (C) Simo Sorce       2006
6
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
10    
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.
15
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.
20
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
24 */
25
26 /*
27  *  Name: ldb_ldap
28  *
29  *  Component: ldb ldap backend
30  *
31  *  Description: core files for LDAP backend
32  *
33  *  Author: Andrew Tridgell
34  *
35  *  Modifications:
36  *
37  *  - description: make the module use asyncronous calls
38  *    date: Feb 2006
39  *    author: Simo Sorce
40  */
41
42 #include "includes.h"
43 #include "ldb/include/includes.h"
44
45 #define LDAP_DEPRECATED 1
46 #include <ldap.h>
47
48 struct lldb_private {
49         LDAP *ldap;
50         struct ldb_module *module;
51 };
52
53 struct lldb_context {
54         struct lldb_private *lldb;
55         struct ldb_handle *handle;
56         int msgid;
57         int timeout;
58         time_t starttime;
59         void *context;
60         int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
61 };
62
63 static int lldb_ldap_to_ldb(int err) {
64         /* Ldap errors and ldb errors are defined to the same values */
65         return err;
66 }
67
68 static struct lldb_context *init_lldb_handle(struct lldb_private *lldb, struct ldb_request *req)
69 {
70         struct lldb_context *ac;
71         struct ldb_handle *h;
72
73         h = talloc_zero(req, struct ldb_handle);
74         if (h == NULL) {
75                 ldb_set_errstring(lldb->module->ldb, "Out of Memory");
76                 return NULL;
77         }
78
79         h->module = lldb->module;
80
81         ac = talloc(h, struct lldb_context);
82         if (ac == NULL) {
83                 ldb_set_errstring(lldb->module->ldb, "Out of Memory");
84                 talloc_free(h);
85                 return NULL;
86         }
87
88         h->private_data = ac;
89
90         h->state = LDB_ASYNC_INIT;
91         h->status = LDB_SUCCESS;
92
93         ac->lldb = lldb;
94         ac->handle = h;
95         ac->context = req->context;
96         ac->callback = req->callback;
97         ac->timeout = req->timeout;
98         ac->starttime = req->starttime;
99         ac->msgid = 0;
100
101         req->handle = h;
102         return ac;
103 }
104 /*
105   convert a ldb_message structure to a list of LDAPMod structures
106   ready for ldap_add() or ldap_modify()
107 */
108 static LDAPMod **lldb_msg_to_mods(void *mem_ctx, const struct ldb_message *msg, int use_flags)
109 {
110         LDAPMod **mods;
111         unsigned int i, j;
112         int num_mods = 0;
113
114         /* allocate maximum number of elements needed */
115         mods = talloc_array(mem_ctx, LDAPMod *, msg->num_elements+1);
116         if (!mods) {
117                 errno = ENOMEM;
118                 return NULL;
119         }
120         mods[0] = NULL;
121
122         for (i=0;i<msg->num_elements;i++) {
123                 const struct ldb_message_element *el = &msg->elements[i];
124
125                 mods[num_mods] = talloc(mods, LDAPMod);
126                 if (!mods[num_mods]) {
127                         goto failed;
128                 }
129                 mods[num_mods+1] = NULL;
130                 mods[num_mods]->mod_op = LDAP_MOD_BVALUES;
131                 if (use_flags) {
132                         switch (el->flags & LDB_FLAG_MOD_MASK) {
133                         case LDB_FLAG_MOD_ADD:
134                                 mods[num_mods]->mod_op |= LDAP_MOD_ADD;
135                                 break;
136                         case LDB_FLAG_MOD_DELETE:
137                                 mods[num_mods]->mod_op |= LDAP_MOD_DELETE;
138                                 break;
139                         case LDB_FLAG_MOD_REPLACE:
140                                 mods[num_mods]->mod_op |= LDAP_MOD_REPLACE;
141                                 break;
142                         }
143                 }
144                 mods[num_mods]->mod_type = discard_const_p(char, el->name);
145                 mods[num_mods]->mod_vals.modv_bvals = talloc_array(mods[num_mods], 
146                                                                    struct berval *,
147                                                                    1+el->num_values);
148                 if (!mods[num_mods]->mod_vals.modv_bvals) {
149                         goto failed;
150                 }
151
152                 for (j=0;j<el->num_values;j++) {
153                         mods[num_mods]->mod_vals.modv_bvals[j] = talloc(mods[num_mods]->mod_vals.modv_bvals,
154                                                                         struct berval);
155                         if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
156                                 goto failed;
157                         }
158                         mods[num_mods]->mod_vals.modv_bvals[j]->bv_val = el->values[j].data;
159                         mods[num_mods]->mod_vals.modv_bvals[j]->bv_len = el->values[j].length;
160                 }
161                 mods[num_mods]->mod_vals.modv_bvals[j] = NULL;
162                 num_mods++;
163         }
164
165         return mods;
166
167 failed:
168         talloc_free(mods);
169         return NULL;
170 }
171
172 /*
173   add a single set of ldap message values to a ldb_message
174 */
175 static int lldb_add_msg_attr(struct ldb_context *ldb,
176                              struct ldb_message *msg, 
177                              const char *attr, struct berval **bval)
178 {
179         int count, i;
180         struct ldb_message_element *el;
181
182         count = ldap_count_values_len(bval);
183
184         if (count <= 0) {
185                 return -1;
186         }
187
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;
193         }
194
195         msg->elements = el;
196
197         el = &msg->elements[msg->num_elements];
198
199         el->name = talloc_strdup(msg->elements, attr);
200         if (!el->name) {
201                 errno = ENOMEM;
202                 return -1;
203         }
204         el->flags = 0;
205
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;
211         }
212
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 = talloc_size(el->values, bval[i]->bv_len+1);
217                 if (!el->values[i].data) {
218                         errno = ENOMEM;
219                         return -1;
220                 }
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++;
225         }
226
227         msg->num_elements++;
228
229         return 0;
230 }
231
232 /*
233   search for matching records
234 */
235 static int lldb_search(struct ldb_module *module, struct ldb_request *req)
236 {
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;
244
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;
248         }
249
250         if (req->op.search.tree == NULL) {
251                 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
252                 return LDB_ERR_OPERATIONS_ERROR;
253         }
254
255         if (req->controls != NULL) {
256                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
257         }
258
259         lldb_ac = init_lldb_handle(lldb, req);
260         if (lldb_ac == NULL) {
261                 return LDB_ERR_OPERATIONS_ERROR;
262         }
263
264         search_base = ldb_dn_alloc_linearized(lldb_ac, req->op.search.base);
265         if (req->op.search.base == NULL) {
266                 search_base = talloc_strdup(lldb_ac, "");
267         }
268         if (search_base == NULL) {
269                 return LDB_ERR_OPERATIONS_ERROR;
270         }
271
272         expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
273         if (expression == NULL) {
274                 return LDB_ERR_OPERATIONS_ERROR;
275         }
276
277         switch (req->op.search.scope) {
278         case LDB_SCOPE_BASE:
279                 ldap_scope = LDAP_SCOPE_BASE;
280                 break;
281         case LDB_SCOPE_ONELEVEL:
282                 ldap_scope = LDAP_SCOPE_ONELEVEL;
283                 break;
284         default:
285                 ldap_scope = LDAP_SCOPE_SUBTREE;
286                 break;
287         }
288
289         tv.tv_sec = req->timeout;
290         tv.tv_usec = 0;
291
292         ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope, 
293                             expression, 
294                             discard_const_p(char *, req->op.search.attrs), 
295                             0,
296                             NULL,
297                             NULL,
298                             &tv,
299                             LDAP_NO_LIMIT,
300                             &lldb_ac->msgid);
301
302         if (ret != LDAP_SUCCESS) {
303                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
304         }
305
306         return lldb_ldap_to_ldb(ret);
307 }
308
309 /*
310   add a record
311 */
312 static int lldb_add(struct ldb_module *module, struct ldb_request *req)
313 {
314         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
315         struct lldb_context *lldb_ac;
316         LDAPMod **mods;
317         char *dn;
318         int ret;
319
320         /* ltdb specials should not reach this point */
321         if (ldb_dn_is_special(req->op.add.message->dn)) {
322                 return LDB_ERR_INVALID_DN_SYNTAX;
323         }
324
325         lldb_ac = init_lldb_handle(lldb, req);
326         if (lldb_ac == NULL) {
327                 return LDB_ERR_OPERATIONS_ERROR;
328         }
329
330         mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
331         if (mods == NULL) {
332                 return LDB_ERR_OPERATIONS_ERROR;
333         }
334
335         dn = ldb_dn_alloc_linearized(lldb_ac, req->op.add.message->dn);
336         if (dn == NULL) {
337                 return LDB_ERR_OPERATIONS_ERROR;
338         }
339
340         ret = ldap_add_ext(lldb->ldap, dn, mods,
341                            NULL,
342                            NULL,
343                            &lldb_ac->msgid);
344
345         if (ret != LDAP_SUCCESS) {
346                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
347         }
348
349         return lldb_ldap_to_ldb(ret);
350 }
351
352 /*
353   modify a record
354 */
355 static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
356 {
357         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
358         struct lldb_context *lldb_ac;
359         LDAPMod **mods;
360         char *dn;
361         int ret;
362
363         /* ltdb specials should not reach this point */
364         if (ldb_dn_is_special(req->op.mod.message->dn)) {
365                 return LDB_ERR_INVALID_DN_SYNTAX;
366         }
367
368         lldb_ac = init_lldb_handle(lldb, req);
369         if (req->handle == NULL) {
370                 return LDB_ERR_OPERATIONS_ERROR;
371         }
372
373         mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
374         if (mods == NULL) {
375                 return LDB_ERR_OPERATIONS_ERROR;
376         }
377
378         dn = ldb_dn_alloc_linearized(lldb_ac, req->op.mod.message->dn);
379         if (dn == NULL) {
380                 return LDB_ERR_OPERATIONS_ERROR;
381         }
382
383         ret = ldap_modify_ext(lldb->ldap, dn, mods,
384                               NULL,
385                               NULL,
386                               &lldb_ac->msgid);
387
388         if (ret != LDAP_SUCCESS) {
389                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
390         }
391
392         return lldb_ldap_to_ldb(ret);
393 }
394
395 /*
396   delete a record
397 */
398 static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
399 {
400         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
401         struct lldb_context *lldb_ac;
402         char *dnstr;
403         int ret;
404         
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;
408         }
409
410         lldb_ac = init_lldb_handle(lldb, req);
411         if (lldb_ac == NULL) {
412                 return LDB_ERR_OPERATIONS_ERROR;
413         }
414
415         dnstr = ldb_dn_alloc_linearized(lldb_ac, req->op.del.dn);
416
417         ret = ldap_delete_ext(lldb->ldap, dnstr,
418                               NULL,
419                               NULL,
420                               &lldb_ac->msgid);
421
422         if (ret != LDAP_SUCCESS) {
423                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
424         }
425
426         return lldb_ldap_to_ldb(ret);
427 }
428
429 /*
430   rename a record
431 */
432 static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
433 {
434         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
435         struct lldb_context *lldb_ac;
436         char *old_dn;
437         char *newrdn;
438         char *parentdn;
439         int ret;
440         
441         /* ltdb specials should not reach this point */
442         if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
443                 return LDB_ERR_INVALID_DN_SYNTAX;
444         }
445
446         lldb_ac = init_lldb_handle(lldb, req);
447         if (lldb_ac == NULL) {
448                 return LDB_ERR_OPERATIONS_ERROR;
449         }
450
451         old_dn = ldb_dn_alloc_linearized(lldb_ac, req->op.rename.olddn);
452         if (old_dn == NULL) {
453                 return LDB_ERR_OPERATIONS_ERROR;
454         }
455
456         newrdn = talloc_asprintf(lldb_ac, "%s=%s",
457                                  ldb_dn_get_rdn_name(req->op.rename.newdn),
458                                  ldb_dn_escape_value(lldb, *(ldb_dn_get_rdn_val(req->op.rename.newdn))));
459         if (!newrdn) {
460                 return LDB_ERR_OPERATIONS_ERROR;
461         }
462
463         parentdn = ldb_dn_alloc_linearized(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
464         if (!parentdn) {
465                 return LDB_ERR_OPERATIONS_ERROR;
466         }
467
468         ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
469                           1, NULL, NULL,
470                           &lldb_ac->msgid);
471
472         if (ret != LDAP_SUCCESS) {
473                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
474         }
475
476         return lldb_ldap_to_ldb(ret);
477 }
478
479 static int lldb_parse_result(struct lldb_context *ac, LDAPMessage *result)
480 {
481         struct ldb_handle *handle = ac->handle;
482         struct lldb_private *lldb = ac->lldb;
483         struct ldb_reply *ares = NULL;
484         LDAPMessage *msg;
485         int type;
486         char *matcheddnp = NULL;
487         char *errmsgp = NULL;
488         char **referralsp = NULL;
489         LDAPControl **serverctrlsp = NULL;
490         int ret = LDB_SUCCESS;
491         
492         type = ldap_msgtype(result);
493
494         handle->status = 0;
495
496         switch (type) {
497
498         case LDAP_RES_SEARCH_ENTRY:
499                 msg = ldap_first_entry(lldb->ldap, result);
500                 if (msg != NULL) {
501                         BerElement *berptr = NULL;
502                         char *attr, *dn;
503
504                         ares = talloc_zero(ac, struct ldb_reply);
505                         if (!ares) {
506                                 ret = LDB_ERR_OPERATIONS_ERROR;
507                                 goto error;
508                         }
509
510                         ares->message = ldb_msg_new(ares);
511                         if (!ares->message) {
512                                 ret = LDB_ERR_OPERATIONS_ERROR;
513                                 goto error;
514                         }
515
516                         dn = ldap_get_dn(lldb->ldap, msg);
517                         if (!dn) {
518                                 ret = LDB_ERR_OPERATIONS_ERROR;
519                                 goto error;
520                         }
521                         ares->message->dn = ldb_dn_new(ares->message, ac->lldb->module->ldb, dn);
522                         if ( ! ldb_dn_validate(ares->message->dn)) {
523                                 ret = LDB_ERR_OPERATIONS_ERROR;
524                                 goto error;
525                         }
526                         ldap_memfree(dn);
527
528                         ares->message->num_elements = 0;
529                         ares->message->elements = NULL;
530
531                         /* loop over all attributes */
532                         for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
533                              attr;
534                              attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
535                                 struct berval **bval;
536                                 bval = ldap_get_values_len(lldb->ldap, msg, attr);
537
538                                 if (bval) {
539                                         lldb_add_msg_attr(ac->lldb->module->ldb, ares->message, attr, bval);
540                                         ldap_value_free_len(bval);
541                                 }                                         
542                         }
543                         if (berptr) ber_free(berptr, 0);
544
545
546                         ares->type = LDB_REPLY_ENTRY;
547                         ret = ac->callback(ac->lldb->module->ldb, ac->context, ares);
548                 } else {
549                         handle->status = LDB_ERR_PROTOCOL_ERROR;
550                         handle->state = LDB_ASYNC_DONE;
551                 }
552                 break;
553
554         case LDAP_RES_SEARCH_REFERENCE:
555                 if (ldap_parse_result(lldb->ldap, result, &handle->status,
556                                         &matcheddnp, &errmsgp,
557                                         &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
558                         ret = LDB_ERR_OPERATIONS_ERROR;
559                         goto error;
560                 }
561                 if (referralsp == NULL) {
562                         handle->status = LDB_ERR_PROTOCOL_ERROR;
563                         goto error;
564                 }
565
566                 ares = talloc_zero(ac, struct ldb_reply);
567                 if (!ares) {
568                         ret = LDB_ERR_OPERATIONS_ERROR;
569                         goto error;
570                 }
571
572                 ares->referral = talloc_strdup(ares, *referralsp);
573                 ares->type = LDB_REPLY_REFERRAL;
574                 ret = ac->callback(ac->lldb->module->ldb, ac->context, ares);
575
576                 break;
577
578         case LDAP_RES_SEARCH_RESULT:
579                 if (ldap_parse_result(lldb->ldap, result, &handle->status,
580                                         &matcheddnp, &errmsgp,
581                                         &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
582                         handle->status = LDB_ERR_OPERATIONS_ERROR;
583                         goto error;
584                 }
585
586                 ares = talloc_zero(ac, struct ldb_reply);
587                 if (!ares) {
588                         ret = LDB_ERR_OPERATIONS_ERROR;
589                         goto error;
590                 }
591
592                 if (serverctrlsp != NULL) {
593                         /* FIXME: transform the LDAPControl list into an ldb_control one */
594                         ares->controls = NULL;
595                 }
596                 
597                 ares->type = LDB_REPLY_DONE;
598                 handle->state = LDB_ASYNC_DONE;
599                 ret = ac->callback(ac->lldb->module->ldb, ac->context, ares);
600
601                 break;
602
603         case LDAP_RES_MODIFY:
604         case LDAP_RES_ADD:
605         case LDAP_RES_DELETE:
606         case LDAP_RES_MODDN:
607                 if (ldap_parse_result(lldb->ldap, result, &handle->status,
608                                         &matcheddnp, &errmsgp,
609                                         &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
610                         handle->status = LDB_ERR_OPERATIONS_ERROR;
611                         goto error;
612                 }
613                 if (ac->callback && handle->status == LDB_SUCCESS) {
614                         ares = NULL; /* FIXME: build a corresponding ares to pass on */
615                         ret = ac->callback(ac->lldb->module->ldb, ac->context, ares);
616                 }
617                 handle->state = LDB_ASYNC_DONE;
618                 break;
619
620         default:
621                 ret = LDB_ERR_PROTOCOL_ERROR;
622                 goto error;
623         }
624
625         if (matcheddnp) ldap_memfree(matcheddnp);
626         if (errmsgp && *errmsgp) {
627                 ldb_set_errstring(ac->lldb->module->ldb, errmsgp);
628         } else if (handle->status) {
629                 ldb_set_errstring(ac->lldb->module->ldb, ldap_err2string(handle->status));
630         }
631         if (errmsgp) {
632                 ldap_memfree(errmsgp);
633         }
634         if (referralsp) ldap_value_free(referralsp);
635         if (serverctrlsp) ldap_controls_free(serverctrlsp);
636
637         ldap_msgfree(result);
638         return lldb_ldap_to_ldb(handle->status);
639
640 error:
641         handle->state = LDB_ASYNC_DONE;
642         ldap_msgfree(result);
643         return ret;
644 }
645
646 static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
647 {
648         struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
649         struct lldb_private *lldb = ac->lldb;
650         struct timeval timeout;
651         LDAPMessage *result;
652         int ret, lret;
653
654         if (handle->state == LDB_ASYNC_DONE) {
655                 return handle->status;
656         }
657
658         if (!ac || !ac->msgid) {
659                 return LDB_ERR_OPERATIONS_ERROR;
660         }
661
662         handle->state = LDB_ASYNC_PENDING;
663         handle->status = LDB_SUCCESS;
664
665         switch(type) {
666         case LDB_WAIT_NONE:
667
668                 if ((ac->timeout != -1) &&
669                     ((ac->starttime + ac->timeout) > time(NULL))) {
670                         return LDB_ERR_TIME_LIMIT_EXCEEDED;
671                 }
672
673                 timeout.tv_sec = 0;
674                 timeout.tv_usec = 0;
675
676                 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
677                 if (lret == -1) {
678                         return LDB_ERR_OPERATIONS_ERROR;
679                 }
680                 if (lret == 0) {
681                         ret = LDB_SUCCESS;
682                         goto done;
683                 }
684
685                 return lldb_parse_result(ac, result);
686
687         case LDB_WAIT_ALL:
688                 timeout.tv_usec = 0;
689                 ret = LDB_ERR_OPERATIONS_ERROR;
690
691                 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
692
693                         if (ac->timeout == -1) {
694                                 lret = ldap_result(lldb->ldap, ac->msgid, 0, NULL, &result);
695                         } else {
696                                 timeout.tv_sec = ac->timeout - (time(NULL) - ac->starttime);
697                                 if (timeout.tv_sec <= 0)
698                                         return LDB_ERR_TIME_LIMIT_EXCEEDED;
699                                 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
700                         }
701                         if (lret == -1) {
702                                 return LDB_ERR_OPERATIONS_ERROR;
703                         }
704                         if (lret == 0) {
705                                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
706                         }
707
708                         ret = lldb_parse_result(ac, result);
709                         if (ret != LDB_SUCCESS) {
710                                 return ret;
711                         }
712                 }
713
714                 break;
715                 
716         default:
717                 handle->state = LDB_ASYNC_DONE;
718                 ret = LDB_ERR_OPERATIONS_ERROR;
719         }
720
721 done:
722         return ret;
723 }
724
725 static int lldb_start_trans(struct ldb_module *module)
726 {
727         /* TODO implement a local transaction mechanism here */
728
729         return LDB_SUCCESS;
730 }
731
732 static int lldb_end_trans(struct ldb_module *module)
733 {
734         /* TODO implement a local transaction mechanism here */
735
736         return LDB_SUCCESS;
737 }
738
739 static int lldb_del_trans(struct ldb_module *module)
740 {
741         /* TODO implement a local transaction mechanism here */
742
743         return LDB_SUCCESS;
744 }
745
746 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
747 {
748         return LDB_ERR_OPERATIONS_ERROR;
749 }
750
751 static const struct ldb_module_ops lldb_ops = {
752         .name              = "ldap",
753         .search            = lldb_search,
754         .add               = lldb_add,
755         .modify            = lldb_modify,
756         .del               = lldb_delete,
757         .rename            = lldb_rename,
758         .request           = lldb_request,
759         .start_transaction = lldb_start_trans,
760         .end_transaction   = lldb_end_trans,
761         .del_transaction   = lldb_del_trans,
762         .wait              = lldb_wait
763 };
764
765
766 static int lldb_destructor(struct lldb_private *lldb)
767 {
768         ldap_unbind(lldb->ldap);
769         return 0;
770 }
771
772 /*
773   connect to the database
774 */
775 static int lldb_connect(struct ldb_context *ldb,
776                         const char *url, 
777                         unsigned int flags, 
778                         const char *options[],
779                         struct ldb_module **_module)
780 {
781         struct ldb_module *module;
782         struct lldb_private *lldb;
783         int version = 3;
784         int ret;
785
786         module = talloc(ldb, struct ldb_module);
787         if (module == NULL) {
788                 ldb_oom(ldb);
789                 talloc_free(lldb);
790                 return -1;
791         }
792         talloc_set_name_const(module, "ldb_ldap backend");
793         module->ldb             = ldb;
794         module->prev            = module->next = NULL;
795         module->private_data    = NULL;
796         module->ops             = &lldb_ops;
797
798         lldb = talloc(module, struct lldb_private);
799         if (!lldb) {
800                 ldb_oom(ldb);
801                 goto failed;
802         }
803         module->private_data    = lldb;
804         lldb->module            = module;
805         lldb->ldap              = NULL;
806
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;
812         }
813
814         talloc_set_destructor(lldb, lldb_destructor);
815
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;
821         }
822
823         *_module = module;
824         return 0;
825
826 failed:
827         talloc_free(module);
828         return -1;
829 }
830
831 int ldb_ldap_init(void)
832 {
833         return ldb_register_backend("ldap", lldb_connect) +
834                    ldb_register_backend("ldapi", lldb_connect) + 
835                    ldb_register_backend("ldaps", lldb_connect);
836 }