r19917: Fix warnings
[bbaumbach/samba-autobuild/.git] / source / 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, "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, "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 =
158                                 (char *)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 =
217                         (uint8_t *)talloc_size(el->values, bval[i]->bv_len+1);
218                 if (!el->values[i].data) {
219                         errno = ENOMEM;
220                         return -1;
221                 }
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++;
226         }
227
228         msg->num_elements++;
229
230         return 0;
231 }
232
233 /*
234   search for matching records
235 */
236 static int lldb_search(struct ldb_module *module, struct ldb_request *req)
237 {
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;
245
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;
249         }
250
251         if (req->op.search.tree == NULL) {
252                 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
253                 return LDB_ERR_OPERATIONS_ERROR;
254         }
255
256         if (req->controls != NULL) {
257                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
258         }
259
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;
263         }
264
265         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
266
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, "");
270         }
271         if (search_base == NULL) {
272                 return LDB_ERR_OPERATIONS_ERROR;
273         }
274
275         expression = ldb_filter_from_tree(
276                 lldb_ac,
277                 CONST_DISCARD(struct ldb_parse_tree *, req->op.search.tree));
278         if (expression == NULL) {
279                 return LDB_ERR_OPERATIONS_ERROR;
280         }
281
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;
292         }
293
294         tv.tv_sec = req->timeout;
295         tv.tv_usec = 0;
296
297         ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope, 
298                             expression, 
299                             discard_const_p(char *, req->op.search.attrs), 
300                             0,
301                             NULL,
302                             NULL,
303                             &tv,
304                             LDAP_NO_LIMIT,
305                             &lldb_ac->msgid);
306
307         if (ret != LDAP_SUCCESS) {
308                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
309         }
310
311         return lldb_ldap_to_ldb(ret);
312 }
313
314 /*
315   add a record
316 */
317 static int lldb_add(struct ldb_module *module, struct ldb_request *req)
318 {
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;
324
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;
328         }
329
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;
333         }
334
335         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
336
337         mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
338         if (mods == NULL) {
339                 return LDB_ERR_OPERATIONS_ERROR;
340         }
341
342         dn = ldb_dn_linearize(lldb_ac, req->op.add.message->dn);
343         if (dn == NULL) {
344                 return LDB_ERR_OPERATIONS_ERROR;
345         }
346
347         ret = ldap_add_ext(lldb->ldap, dn, mods,
348                            NULL,
349                            NULL,
350                            &lldb_ac->msgid);
351
352         if (ret != LDAP_SUCCESS) {
353                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
354         }
355
356         return lldb_ldap_to_ldb(ret);
357 }
358
359 /*
360   modify a record
361 */
362 static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
363 {
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;
369
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;
373         }
374
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;
378         }
379
380         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
381
382         mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
383         if (mods == NULL) {
384                 return LDB_ERR_OPERATIONS_ERROR;
385         }
386
387         dn = ldb_dn_linearize(lldb_ac, req->op.mod.message->dn);
388         if (dn == NULL) {
389                 return LDB_ERR_OPERATIONS_ERROR;
390         }
391
392         ret = ldap_modify_ext(lldb->ldap, dn, mods,
393                               NULL,
394                               NULL,
395                               &lldb_ac->msgid);
396
397         if (ret != LDAP_SUCCESS) {
398                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
399         }
400
401         return lldb_ldap_to_ldb(ret);
402 }
403
404 /*
405   delete a record
406 */
407 static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
408 {
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;
413         
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;
417         }
418
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;
422         }
423
424         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
425
426         dnstr = ldb_dn_linearize(lldb_ac, req->op.del.dn);
427
428         ret = ldap_delete_ext(lldb->ldap, dnstr,
429                               NULL,
430                               NULL,
431                               &lldb_ac->msgid);
432
433         if (ret != LDAP_SUCCESS) {
434                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
435         }
436
437         return lldb_ldap_to_ldb(ret);
438 }
439
440 /*
441   rename a record
442 */
443 static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
444 {
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;
451         
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;
455         }
456
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;
460         }
461
462         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
463
464         old_dn = ldb_dn_linearize(lldb_ac, req->op.rename.olddn);
465         if (old_dn == NULL) {
466                 return LDB_ERR_OPERATIONS_ERROR;
467         }
468
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;
474         }
475
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;
479         }
480
481         ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
482                           1, NULL, NULL,
483                           &lldb_ac->msgid);
484
485         if (ret != LDAP_SUCCESS) {
486                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
487         }
488
489         return lldb_ldap_to_ldb(ret);
490 }
491
492 static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
493 {
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;
504         
505         type = ldap_msgtype(result);
506
507         handle->status = 0;
508
509         switch (type) {
510
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;
516
517                         ares = talloc_zero(ac, struct ldb_reply);
518                         if (!ares) {
519                                 ret = LDB_ERR_OPERATIONS_ERROR;
520                                 goto error;
521                         }
522
523                         ares->message = ldb_msg_new(ares);
524                         if (!ares->message) {
525                                 ret = LDB_ERR_OPERATIONS_ERROR;
526                                 goto error;
527                         }
528
529                         dn = ldap_get_dn(lldb->ldap, msg);
530                         if (!dn) {
531                                 ret = LDB_ERR_OPERATIONS_ERROR;
532                                 goto error;
533                         }
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;
538                         }
539                         ldap_memfree(dn);
540
541                         ares->message->num_elements = 0;
542                         ares->message->elements = NULL;
543                         ares->message->private_data = NULL;
544
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);
551
552                                 if (bval) {
553                                         lldb_add_msg_attr(ac->module->ldb, ares->message, attr, bval);
554                                         ldap_value_free_len(bval);
555                                 }                                         
556                         }
557                         if (berptr) ber_free(berptr, 0);
558
559
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;
565                 }
566                 break;
567
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;
574                 }
575                 if (referralsp == NULL) {
576                         handle->status = LDB_ERR_PROTOCOL_ERROR;
577                         goto error;
578                 }
579
580                 ares = talloc_zero(ac, struct ldb_reply);
581                 if (!ares) {
582                         ret = LDB_ERR_OPERATIONS_ERROR;
583                         goto error;
584                 }
585
586                 ares->referral = talloc_strdup(ares, *referralsp);
587                 ares->type = LDB_REPLY_REFERRAL;
588                 ret = ac->callback(ac->module->ldb, ac->context, ares);
589
590                 break;
591
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;
598                 }
599
600                 ares = talloc_zero(ac, struct ldb_reply);
601                 if (!ares) {
602                         ret = LDB_ERR_OPERATIONS_ERROR;
603                         goto error;
604                 }
605
606                 if (serverctrlsp != NULL) {
607                         /* FIXME: transform the LDAPControl list into an ldb_control one */
608                         ares->controls = NULL;
609                 }
610                 
611                 ares->type = LDB_REPLY_DONE;
612                 handle->state = LDB_ASYNC_DONE;
613                 ret = ac->callback(ac->module->ldb, ac->context, ares);
614
615                 break;
616
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;
626                 }
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);
630                 }
631                 handle->state = LDB_ASYNC_DONE;
632                 break;
633
634         default:
635                 ret = LDB_ERR_PROTOCOL_ERROR;
636                 goto error;
637         }
638
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));
644         }
645         if (errmsgp) {
646                 ldap_memfree(errmsgp);
647         }
648         if (referralsp) ldap_value_free(referralsp);
649         if (serverctrlsp) ldap_controls_free(serverctrlsp);
650
651         ldap_msgfree(result);
652         return lldb_ldap_to_ldb(handle->status);
653
654 error:
655         handle->state = LDB_ASYNC_DONE;
656         ldap_msgfree(result);
657         return ret;
658 }
659
660 static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
661 {
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;
667
668         if (handle->state == LDB_ASYNC_DONE) {
669                 return handle->status;
670         }
671
672         if (!ac || !ac->msgid) {
673                 return LDB_ERR_OPERATIONS_ERROR;
674         }
675
676         handle->state = LDB_ASYNC_PENDING;
677         handle->status = LDB_SUCCESS;
678
679         switch(type) {
680         case LDB_WAIT_NONE:
681
682                 if ((ac->timeout != -1) &&
683                     ((ac->starttime + ac->timeout) > time(NULL))) {
684                         return LDB_ERR_TIME_LIMIT_EXCEEDED;
685                 }
686
687                 timeout.tv_sec = 0;
688                 timeout.tv_usec = 0;
689
690                 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
691                 if (lret == -1) {
692                         return LDB_ERR_OPERATIONS_ERROR;
693                 }
694                 if (lret == 0) {
695                         ret = LDB_SUCCESS;
696                         goto done;
697                 }
698
699                 return lldb_parse_result(handle, result);
700
701         case LDB_WAIT_ALL:
702                 timeout.tv_usec = 0;
703                 ret = LDB_ERR_OPERATIONS_ERROR;
704
705                 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
706
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);
714                         }
715                         if (lret == -1) {
716                                 return LDB_ERR_OPERATIONS_ERROR;
717                         }
718                         if (lret == 0) {
719                                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
720                         }
721
722                         ret = lldb_parse_result(handle, result);
723                         if (ret != LDB_SUCCESS) {
724                                 return ret;
725                         }
726                 }
727
728                 break;
729                 
730         default:
731                 handle->state = LDB_ASYNC_DONE;
732                 ret = LDB_ERR_OPERATIONS_ERROR;
733         }
734
735 done:
736         return ret;
737 }
738
739 static int lldb_start_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_end_trans(struct ldb_module *module)
747 {
748         /* TODO implement a local transaction mechanism here */
749
750         return LDB_SUCCESS;
751 }
752
753 static int lldb_del_trans(struct ldb_module *module)
754 {
755         /* TODO implement a local transaction mechanism here */
756
757         return LDB_SUCCESS;
758 }
759
760 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
761 {
762         return LDB_ERR_OPERATIONS_ERROR;
763 }
764
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
777 };
778
779
780 static int lldb_destructor(struct lldb_private *lldb)
781 {
782         ldap_unbind(lldb->ldap);
783         return 0;
784 }
785
786 /*
787   connect to the database
788 */
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)
794 {
795         struct lldb_private *lldb = NULL;
796         int version = 3;
797         int ret;
798
799         lldb = talloc(ldb, struct lldb_private);
800         if (!lldb) {
801                 ldb_oom(ldb);
802                 goto failed;
803         }
804
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 = talloc(ldb, struct ldb_module);
824         if (*module == NULL) {
825                 ldb_oom(ldb);
826                 talloc_free(lldb);
827                 return -1;
828         }
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;
834
835         return 0;
836
837 failed:
838         talloc_free(lldb);
839         return -1;
840 }
841
842 int ldb_ldap_init(void)
843 {
844         return ldb_register_backend("ldap", lldb_connect) +
845                    ldb_register_backend("ldapi", lldb_connect) + 
846                    ldb_register_backend("ldaps", lldb_connect);
847 }