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