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