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