r19362: - don't need to store the baseinfo message after cache load
[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 };
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 = 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                 /* we have to ensure this is null terminated so that
214                    ldb_msg_find_attr_as_string() can work */
215                 el->values[i].data = talloc_size(el->values, bval[i]->bv_len+1);
216                 if (!el->values[i].data) {
217                         errno = ENOMEM;
218                         return -1;
219                 }
220                 memcpy(el->values[i].data, bval[i]->bv_val, bval[i]->bv_len);
221                 el->values[i].data[bval[i]->bv_len] = 0;
222                 el->values[i].length = bval[i]->bv_len;
223                 el->num_values++;
224         }
225
226         msg->num_elements++;
227
228         return 0;
229 }
230
231 /*
232   search for matching records
233 */
234 static int lldb_search(struct ldb_module *module, struct ldb_request *req)
235 {
236         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
237         struct lldb_context *lldb_ac;
238         struct timeval tv;
239         int ldap_scope;
240         char *search_base;
241         char *expression;
242         int ret;
243
244         if (!req->callback || !req->context) {
245                 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
246                 return LDB_ERR_OPERATIONS_ERROR;
247         }
248
249         if (req->op.search.tree == NULL) {
250                 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
251                 return LDB_ERR_OPERATIONS_ERROR;
252         }
253
254         if (req->controls != NULL) {
255                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
256         }
257
258         req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
259         if (req->handle == NULL) {
260                 return LDB_ERR_OPERATIONS_ERROR;
261         }
262
263         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
264
265         search_base = ldb_dn_linearize(lldb_ac, req->op.search.base);
266         if (req->op.search.base == NULL) {
267                 search_base = talloc_strdup(lldb_ac, "");
268         }
269         if (search_base == NULL) {
270                 return LDB_ERR_OPERATIONS_ERROR;
271         }
272
273         expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
274         if (expression == NULL) {
275                 return LDB_ERR_OPERATIONS_ERROR;
276         }
277
278         switch (req->op.search.scope) {
279         case LDB_SCOPE_BASE:
280                 ldap_scope = LDAP_SCOPE_BASE;
281                 break;
282         case LDB_SCOPE_ONELEVEL:
283                 ldap_scope = LDAP_SCOPE_ONELEVEL;
284                 break;
285         default:
286                 ldap_scope = LDAP_SCOPE_SUBTREE;
287                 break;
288         }
289
290         tv.tv_sec = req->timeout;
291         tv.tv_usec = 0;
292
293         ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope, 
294                             expression, 
295                             discard_const_p(char *, req->op.search.attrs), 
296                             0,
297                             NULL,
298                             NULL,
299                             &tv,
300                             LDAP_NO_LIMIT,
301                             &lldb_ac->msgid);
302
303         if (ret != LDAP_SUCCESS) {
304                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
305         }
306
307         return lldb_ldap_to_ldb(ret);
308 }
309
310 /*
311   add a record
312 */
313 static int lldb_add(struct ldb_module *module, struct ldb_request *req)
314 {
315         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
316         struct lldb_context *lldb_ac;
317         LDAPMod **mods;
318         char *dn;
319         int ret;
320
321         /* ltdb specials should not reach this point */
322         if (ldb_dn_is_special(req->op.add.message->dn)) {
323                 return LDB_ERR_INVALID_DN_SYNTAX;
324         }
325
326         req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
327         if (req->handle == NULL) {
328                 return LDB_ERR_OPERATIONS_ERROR;
329         }
330
331         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
332
333         mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
334         if (mods == NULL) {
335                 return LDB_ERR_OPERATIONS_ERROR;
336         }
337
338         dn = ldb_dn_linearize(lldb_ac, req->op.add.message->dn);
339         if (dn == NULL) {
340                 return LDB_ERR_OPERATIONS_ERROR;
341         }
342
343         ret = ldap_add_ext(lldb->ldap, dn, mods,
344                            NULL,
345                            NULL,
346                            &lldb_ac->msgid);
347
348         if (ret != LDAP_SUCCESS) {
349                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
350         }
351
352         return lldb_ldap_to_ldb(ret);
353 }
354
355 /*
356   modify a record
357 */
358 static int lldb_modify(struct ldb_module *module, struct ldb_request *req)
359 {
360         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
361         struct lldb_context *lldb_ac;
362         LDAPMod **mods;
363         char *dn;
364         int ret;
365
366         /* ltdb specials should not reach this point */
367         if (ldb_dn_is_special(req->op.mod.message->dn)) {
368                 return LDB_ERR_INVALID_DN_SYNTAX;
369         }
370
371         req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
372         if (req->handle == NULL) {
373                 return LDB_ERR_OPERATIONS_ERROR;
374         }
375
376         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
377
378         mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
379         if (mods == NULL) {
380                 return LDB_ERR_OPERATIONS_ERROR;
381         }
382
383         dn = ldb_dn_linearize(lldb_ac, req->op.mod.message->dn);
384         if (dn == NULL) {
385                 return LDB_ERR_OPERATIONS_ERROR;
386         }
387
388         ret = ldap_modify_ext(lldb->ldap, dn, mods,
389                               NULL,
390                               NULL,
391                               &lldb_ac->msgid);
392
393         if (ret != LDAP_SUCCESS) {
394                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
395         }
396
397         return lldb_ldap_to_ldb(ret);
398 }
399
400 /*
401   delete a record
402 */
403 static int lldb_delete(struct ldb_module *module, struct ldb_request *req)
404 {
405         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
406         struct lldb_context *lldb_ac;
407         char *dnstr;
408         int ret;
409         
410         /* ltdb specials should not reach this point */
411         if (ldb_dn_is_special(req->op.del.dn)) {
412                 return LDB_ERR_INVALID_DN_SYNTAX;
413         }
414
415         req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
416         if (req->handle == NULL) {
417                 return LDB_ERR_OPERATIONS_ERROR;
418         }
419
420         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
421
422         dnstr = ldb_dn_linearize(lldb_ac, req->op.del.dn);
423
424         ret = ldap_delete_ext(lldb->ldap, dnstr,
425                               NULL,
426                               NULL,
427                               &lldb_ac->msgid);
428
429         if (ret != LDAP_SUCCESS) {
430                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
431         }
432
433         return lldb_ldap_to_ldb(ret);
434 }
435
436 /*
437   rename a record
438 */
439 static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
440 {
441         struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
442         struct lldb_context *lldb_ac;
443         char *old_dn;
444         char *newrdn;
445         char *parentdn;
446         int ret;
447         
448         /* ltdb specials should not reach this point */
449         if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
450                 return LDB_ERR_INVALID_DN_SYNTAX;
451         }
452
453         req->handle = init_handle(lldb, module, req->context, req->callback, req->timeout, req->starttime);
454         if (req->handle == NULL) {
455                 return LDB_ERR_OPERATIONS_ERROR;
456         }
457
458         lldb_ac = talloc_get_type(req->handle->private_data, struct lldb_context);
459
460         old_dn = ldb_dn_linearize(lldb_ac, req->op.rename.olddn);
461         if (old_dn == NULL) {
462                 return LDB_ERR_OPERATIONS_ERROR;
463         }
464
465         newrdn = talloc_asprintf(lldb_ac, "%s=%s",
466                                  req->op.rename.newdn->components[0].name,
467                                  ldb_dn_escape_value(lldb, req->op.rename.newdn->components[0].value));
468         if (!newrdn) {
469                 return LDB_ERR_OPERATIONS_ERROR;
470         }
471
472         parentdn = ldb_dn_linearize(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
473         if (!parentdn) {
474                 return LDB_ERR_OPERATIONS_ERROR;
475         }
476
477         ret = ldap_rename(lldb->ldap, old_dn, newrdn, parentdn,
478                           1, NULL, NULL,
479                           &lldb_ac->msgid);
480
481         if (ret != LDAP_SUCCESS) {
482                 ldb_set_errstring(module->ldb, ldap_err2string(ret));
483         }
484
485         return lldb_ldap_to_ldb(ret);
486 }
487
488 static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
489 {
490         struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
491         struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
492         struct ldb_reply *ares = NULL;
493         LDAPMessage *msg;
494         int type;
495         char *matcheddnp = NULL;
496         char *errmsgp = NULL;
497         char **referralsp = NULL;
498         LDAPControl **serverctrlsp = NULL;
499         int ret = LDB_SUCCESS;
500         
501         type = ldap_msgtype(result);
502
503         handle->status = 0;
504
505         switch (type) {
506
507         case LDAP_RES_SEARCH_ENTRY:
508                 msg = ldap_first_entry(lldb->ldap, result);
509                 if (msg != NULL) {
510                         BerElement *berptr = NULL;
511                         char *attr, *dn;
512
513                         ares = talloc_zero(ac, struct ldb_reply);
514                         if (!ares) {
515                                 ret = LDB_ERR_OPERATIONS_ERROR;
516                                 goto error;
517                         }
518
519                         ares->message = ldb_msg_new(ares);
520                         if (!ares->message) {
521                                 ret = LDB_ERR_OPERATIONS_ERROR;
522                                 goto error;
523                         }
524
525                         dn = ldap_get_dn(lldb->ldap, msg);
526                         if (!dn) {
527                                 ret = LDB_ERR_OPERATIONS_ERROR;
528                                 goto error;
529                         }
530                         ares->message->dn = ldb_dn_explode_or_special(ares->message, dn);
531                         if (ares->message->dn == NULL) {
532                                 ret = LDB_ERR_OPERATIONS_ERROR;
533                                 goto error;
534                         }
535                         ldap_memfree(dn);
536
537                         ares->message->num_elements = 0;
538                         ares->message->elements = NULL;
539                         ares->message->private_data = NULL;
540
541                         /* loop over all attributes */
542                         for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
543                              attr;
544                              attr=ldap_next_attribute(lldb->ldap, msg, berptr)) {
545                                 struct berval **bval;
546                                 bval = ldap_get_values_len(lldb->ldap, msg, attr);
547
548                                 if (bval) {
549                                         lldb_add_msg_attr(ac->module->ldb, ares->message, attr, bval);
550                                         ldap_value_free_len(bval);
551                                 }                                         
552                         }
553                         if (berptr) ber_free(berptr, 0);
554
555
556                         ares->type = LDB_REPLY_ENTRY;
557                         ret = ac->callback(ac->module->ldb, ac->context, ares);
558                 } else {
559                         handle->status = LDB_ERR_PROTOCOL_ERROR;
560                         handle->state = LDB_ASYNC_DONE;
561                 }
562                 break;
563
564         case LDAP_RES_SEARCH_REFERENCE:
565                 if (ldap_parse_result(lldb->ldap, result, &handle->status,
566                                         &matcheddnp, &errmsgp,
567                                         &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
568                         ret = LDB_ERR_OPERATIONS_ERROR;
569                         goto error;
570                 }
571                 if (referralsp == NULL) {
572                         handle->status = LDB_ERR_PROTOCOL_ERROR;
573                         goto error;
574                 }
575
576                 ares = talloc_zero(ac, struct ldb_reply);
577                 if (!ares) {
578                         ret = LDB_ERR_OPERATIONS_ERROR;
579                         goto error;
580                 }
581
582                 ares->referral = talloc_strdup(ares, *referralsp);
583                 ares->type = LDB_REPLY_REFERRAL;
584                 ret = ac->callback(ac->module->ldb, ac->context, ares);
585
586                 break;
587
588         case LDAP_RES_SEARCH_RESULT:
589                 if (ldap_parse_result(lldb->ldap, result, &handle->status,
590                                         &matcheddnp, &errmsgp,
591                                         &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
592                         handle->status = LDB_ERR_OPERATIONS_ERROR;
593                         goto error;
594                 }
595
596                 ares = talloc_zero(ac, struct ldb_reply);
597                 if (!ares) {
598                         ret = LDB_ERR_OPERATIONS_ERROR;
599                         goto error;
600                 }
601
602                 if (serverctrlsp != NULL) {
603                         /* FIXME: transform the LDAPControl list into an ldb_control one */
604                         ares->controls = NULL;
605                 }
606                 
607                 ares->type = LDB_REPLY_DONE;
608                 handle->state = LDB_ASYNC_DONE;
609                 ret = ac->callback(ac->module->ldb, ac->context, ares);
610
611                 break;
612
613         case LDAP_RES_MODIFY:
614         case LDAP_RES_ADD:
615         case LDAP_RES_DELETE:
616         case LDAP_RES_MODDN:
617                 if (ldap_parse_result(lldb->ldap, result, &handle->status,
618                                         &matcheddnp, &errmsgp,
619                                         &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
620                         handle->status = LDB_ERR_OPERATIONS_ERROR;
621                         goto error;
622                 }
623                 if (ac->callback && handle->status == LDB_SUCCESS) {
624                         ares = NULL; /* FIXME: build a corresponding ares to pass on */
625                         ret = ac->callback(ac->module->ldb, ac->context, ares);
626                 }
627                 handle->state = LDB_ASYNC_DONE;
628                 break;
629
630         default:
631                 ret = LDB_ERR_PROTOCOL_ERROR;
632                 goto error;
633         }
634
635         if (matcheddnp) ldap_memfree(matcheddnp);
636         if (errmsgp && *errmsgp) {
637                 ldb_set_errstring(ac->module->ldb, errmsgp);
638         } else if (handle->status) {
639                 ldb_set_errstring(ac->module->ldb, ldap_err2string(handle->status));
640         }
641         if (errmsgp) {
642                 ldap_memfree(errmsgp);
643         }
644         if (referralsp) ldap_value_free(referralsp);
645         if (serverctrlsp) ldap_controls_free(serverctrlsp);
646
647         ldap_msgfree(result);
648         return lldb_ldap_to_ldb(handle->status);
649
650 error:
651         handle->state = LDB_ASYNC_DONE;
652         ldap_msgfree(result);
653         return ret;
654 }
655
656 static int lldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
657 {
658         struct lldb_context *ac = talloc_get_type(handle->private_data, struct lldb_context);
659         struct lldb_private *lldb = talloc_get_type(handle->module->private_data, struct lldb_private);
660         struct timeval timeout;
661         LDAPMessage *result;
662         int ret, lret;
663
664         if (handle->state == LDB_ASYNC_DONE) {
665                 return handle->status;
666         }
667
668         if (!ac || !ac->msgid) {
669                 return LDB_ERR_OPERATIONS_ERROR;
670         }
671
672         handle->state = LDB_ASYNC_PENDING;
673         handle->status = LDB_SUCCESS;
674
675         switch(type) {
676         case LDB_WAIT_NONE:
677
678                 if ((ac->timeout != -1) &&
679                     ((ac->starttime + ac->timeout) > time(NULL))) {
680                         return LDB_ERR_TIME_LIMIT_EXCEEDED;
681                 }
682
683                 timeout.tv_sec = 0;
684                 timeout.tv_usec = 0;
685
686                 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
687                 if (lret == -1) {
688                         return LDB_ERR_OPERATIONS_ERROR;
689                 }
690                 if (lret == 0) {
691                         ret = LDB_SUCCESS;
692                         goto done;
693                 }
694
695                 return lldb_parse_result(handle, result);
696
697         case LDB_WAIT_ALL:
698                 timeout.tv_usec = 0;
699                 ret = LDB_ERR_OPERATIONS_ERROR;
700
701                 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
702
703                         if (ac->timeout == -1) {
704                                 lret = ldap_result(lldb->ldap, ac->msgid, 0, NULL, &result);
705                         } else {
706                                 timeout.tv_sec = ac->timeout - (time(NULL) - ac->starttime);
707                                 if (timeout.tv_sec <= 0)
708                                         return LDB_ERR_TIME_LIMIT_EXCEEDED;
709                                 lret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
710                         }
711                         if (lret == -1) {
712                                 return LDB_ERR_OPERATIONS_ERROR;
713                         }
714                         if (lret == 0) {
715                                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
716                         }
717
718                         ret = lldb_parse_result(handle, result);
719                         if (ret != LDB_SUCCESS) {
720                                 return ret;
721                         }
722                 }
723
724                 break;
725                 
726         default:
727                 handle->state = LDB_ASYNC_DONE;
728                 ret = LDB_ERR_OPERATIONS_ERROR;
729         }
730
731 done:
732         return ret;
733 }
734
735 static int lldb_start_trans(struct ldb_module *module)
736 {
737         /* TODO implement a local transaction mechanism here */
738
739         return LDB_SUCCESS;
740 }
741
742 static int lldb_end_trans(struct ldb_module *module)
743 {
744         /* TODO implement a local transaction mechanism here */
745
746         return LDB_SUCCESS;
747 }
748
749 static int lldb_del_trans(struct ldb_module *module)
750 {
751         /* TODO implement a local transaction mechanism here */
752
753         return LDB_SUCCESS;
754 }
755
756 static int lldb_request(struct ldb_module *module, struct ldb_request *req)
757 {
758         return LDB_ERR_OPERATIONS_ERROR;
759 }
760
761 static const struct ldb_module_ops lldb_ops = {
762         .name              = "ldap",
763         .search            = lldb_search,
764         .add               = lldb_add,
765         .modify            = lldb_modify,
766         .del               = lldb_delete,
767         .rename            = lldb_rename,
768         .request           = lldb_request,
769         .start_transaction = lldb_start_trans,
770         .end_transaction   = lldb_end_trans,
771         .del_transaction   = lldb_del_trans,
772         .wait              = lldb_wait
773 };
774
775
776 static int lldb_destructor(struct lldb_private *lldb)
777 {
778         ldap_unbind(lldb->ldap);
779         return 0;
780 }
781
782 /*
783   connect to the database
784 */
785 static int lldb_connect(struct ldb_context *ldb,
786                         const char *url, 
787                         unsigned int flags, 
788                         const char *options[],
789                         struct ldb_module **module)
790 {
791         struct lldb_private *lldb = NULL;
792         int version = 3;
793         int ret;
794
795         lldb = talloc(ldb, struct lldb_private);
796         if (!lldb) {
797                 ldb_oom(ldb);
798                 goto failed;
799         }
800
801         lldb->ldap = NULL;
802
803         ret = ldap_initialize(&lldb->ldap, url);
804         if (ret != LDAP_SUCCESS) {
805                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_initialize failed for URL '%s' - %s\n",
806                           url, ldap_err2string(ret));
807                 goto failed;
808         }
809
810         talloc_set_destructor(lldb, lldb_destructor);
811
812         ret = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
813         if (ret != LDAP_SUCCESS) {
814                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_set_option failed - %s\n",
815                           ldap_err2string(ret));
816                 goto failed;
817         }
818
819         *module = talloc(ldb, struct ldb_module);
820         if (!module) {
821                 ldb_oom(ldb);
822                 talloc_free(lldb);
823                 return -1;
824         }
825         talloc_set_name_const(*module, "ldb_ldap backend");
826         (*module)->ldb = ldb;
827         (*module)->prev = (*module)->next = NULL;
828         (*module)->private_data = lldb;
829         (*module)->ops = &lldb_ops;
830
831         return 0;
832
833 failed:
834         talloc_free(lldb);
835         return -1;
836 }
837
838 int ldb_ldap_init(void)
839 {
840         return ldb_register_backend("ldap", lldb_connect) +
841                    ldb_register_backend("ldapi", lldb_connect) + 
842                    ldb_register_backend("ldaps", lldb_connect);
843 }