r20122: pass ildb to ildb_request_noop()
[samba.git] / source4 / lib / ldb / ldb_ildap / ldb_ildap.c
1 /* 
2    ldb database library - ildap backend
3
4    Copyright (C) Andrew Tridgell  2005
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_ildap
28  *
29  *  Component: ldb ildap backend
30  *
31  *  Description: This is a ldb backend for the internal ldap
32  *  client library in Samba4. By using this backend we are
33  *  independent of a system ldap library
34  *
35  *  Author: Andrew Tridgell
36  *
37  *  Modifications:
38  *
39  *  - description: make the module use asyncronous calls
40  *    date: Feb 2006
41  *    author: Simo Sorce
42  */
43
44
45 #include "includes.h"
46 #include "ldb/include/includes.h"
47
48 #include "lib/events/events.h"
49 #include "libcli/ldap/ldap.h"
50 #include "libcli/ldap/ldap_client.h"
51 #include "auth/auth.h"
52 #include "auth/credentials/credentials.h"
53
54 struct ildb_private {
55         struct ldap_connection *ldap;
56         struct ldb_context *ldb;
57         struct ldb_module *module;
58 };
59
60 struct ildb_context {
61         struct ldb_module *module;
62         struct ldap_request *req;
63         void *context;
64         int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
65 };
66
67 /*
68   convert a ldb_message structure to a list of ldap_mod structures
69   ready for ildap_add() or ildap_modify()
70 */
71 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
72                                           const struct ldb_message *msg, int use_flags)
73 {
74         struct ldap_mod **mods;
75         unsigned int i;
76         int n = 0;
77
78         /* allocate maximum number of elements needed */
79         mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
80         if (!mods) {
81                 errno = ENOMEM;
82                 return NULL;
83         }
84         mods[0] = NULL;
85
86         for (i = 0; i < msg->num_elements; i++) {
87                 const struct ldb_message_element *el = &msg->elements[i];
88
89                 mods[n] = talloc(mods, struct ldap_mod);
90                 if (!mods[n]) {
91                         goto failed;
92                 }
93                 mods[n + 1] = NULL;
94                 mods[n]->type = 0;
95                 mods[n]->attrib = *el;
96                 if (use_flags) {
97                         switch (el->flags & LDB_FLAG_MOD_MASK) {
98                         case LDB_FLAG_MOD_ADD:
99                                 mods[n]->type = LDAP_MODIFY_ADD;
100                                 break;
101                         case LDB_FLAG_MOD_DELETE:
102                                 mods[n]->type = LDAP_MODIFY_DELETE;
103                                 break;
104                         case LDB_FLAG_MOD_REPLACE:
105                                 mods[n]->type = LDAP_MODIFY_REPLACE;
106                                 break;
107                         }
108                 }
109                 n++;
110         }
111
112         *num_mods = n;
113         return mods;
114
115 failed:
116         talloc_free(mods);
117         return NULL;
118 }
119
120
121 /*
122   map an ildap NTSTATUS to a ldb error code
123 */
124 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
125 {
126         if (NT_STATUS_IS_OK(status)) {
127                 return LDB_SUCCESS;
128         }
129         ldb_set_errstring(ildb->ldb, ldap_errstr(ildb->ldap, status));
130         if (NT_STATUS_IS_LDAP(status)) {
131                 return NT_STATUS_LDAP_CODE(status);
132         }
133         return LDB_ERR_OPERATIONS_ERROR;
134 }
135
136 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
137                                  struct timeval t, void *private_data)
138 {
139         struct ldb_handle *handle = talloc_get_type(private_data, struct ldb_handle);
140         struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
141
142         if (ac->req->state == LDAP_REQUEST_PENDING) {
143                 DLIST_REMOVE(ac->req->conn->pending, ac->req);
144         }
145
146         handle->status = LDB_ERR_TIME_LIMIT_EXCEEDED;
147
148         return;
149 }
150
151 static void ildb_callback(struct ldap_request *req)
152 {
153         struct ldb_handle *handle = talloc_get_type(req->async.private_data, struct ldb_handle);
154         struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
155         struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private);
156         NTSTATUS status;
157         int i;
158
159         handle->status = LDB_SUCCESS;
160
161         if (!NT_STATUS_IS_OK(req->status)) {
162                 handle->status = ildb_map_error(ildb, req->status);
163                 return;
164         }
165
166         if (req->num_replies < 1) {
167                 handle->status = LDB_ERR_OPERATIONS_ERROR;
168                 return;
169         } 
170                 
171         switch (req->type) {
172
173         case LDAP_TAG_ModifyRequest:
174                 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
175                         handle->status = LDB_ERR_PROTOCOL_ERROR;
176                         return;
177                 }
178                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
179                 handle->status = ildb_map_error(ildb, status);
180                 if (ac->callback && handle->status == LDB_SUCCESS) {
181                         /* FIXME: build a corresponding ares to pass on */
182                         handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
183                 }
184                 handle->state = LDB_ASYNC_DONE;
185                 break;
186
187         case LDAP_TAG_AddRequest:
188                 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
189                         handle->status = LDB_ERR_PROTOCOL_ERROR;
190                         return;
191                 }
192                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
193                 handle->status = ildb_map_error(ildb, status);
194                 if (ac->callback && handle->status == LDB_SUCCESS) {
195                         /* FIXME: build a corresponding ares to pass on */
196                         handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
197                 }
198                 handle->state = LDB_ASYNC_DONE;
199                 break;
200
201         case LDAP_TAG_DelRequest:
202                 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
203                         handle->status = LDB_ERR_PROTOCOL_ERROR;
204                         return;
205                 }
206                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
207                 handle->status = ildb_map_error(ildb, status);
208                 if (ac->callback && handle->status == LDB_SUCCESS) {
209                         /* FIXME: build a corresponding ares to pass on */
210                         handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
211                 }
212                 handle->state = LDB_ASYNC_DONE;
213                 break;
214
215         case LDAP_TAG_ModifyDNRequest:
216                 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
217                         handle->status = LDB_ERR_PROTOCOL_ERROR;
218                         return;
219                 }
220                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
221                 handle->status = ildb_map_error(ildb, status);
222                 if (ac->callback && handle->status == LDB_SUCCESS) {
223                         /* FIXME: build a corresponding ares to pass on */
224                         handle->status = ac->callback(ac->module->ldb, ac->context, NULL);
225                 }
226                 handle->state = LDB_ASYNC_DONE;
227                 break;
228
229         case LDAP_TAG_SearchRequest:
230                 /* loop over all messages */
231                 for (i = 0; i < req->num_replies; i++) {
232                         struct ldap_SearchResEntry *search;
233                         struct ldb_reply *ares = NULL;
234                         struct ldap_message *msg;
235                         int ret;
236
237                         ares = talloc_zero(ac, struct ldb_reply);
238                         if (!ares) {
239                                 handle->status = LDB_ERR_OPERATIONS_ERROR;
240                                 return;
241                         }
242
243                         msg = req->replies[i];
244                         switch (msg->type) {
245
246                         case LDAP_TAG_SearchResultDone:
247
248                                 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
249                                 if (!NT_STATUS_IS_OK(status)) {
250                                         handle->status = ildb_map_error(ildb, status);
251                                         return;
252                                 }
253                                 
254                                 ares->controls = talloc_move(ares, &msg->controls);
255                                 if (msg->r.SearchResultDone.resultcode) {
256                                         if (msg->r.SearchResultDone.errormessage) {
257                                                 ldb_set_errstring(ac->module->ldb, msg->r.SearchResultDone.errormessage);
258                                         }
259                                 }
260
261                                 handle->status = msg->r.SearchResultDone.resultcode;
262                                 handle->state = LDB_ASYNC_DONE;
263                                 ares->type = LDB_REPLY_DONE;
264                                 break;
265
266                         case LDAP_TAG_SearchResultEntry:
267
268
269                                 ares->message = ldb_msg_new(ares);
270                                 if (!ares->message) {
271                                         handle->status = LDB_ERR_OPERATIONS_ERROR;
272                                         return;
273                                 }
274
275                                 search = &(msg->r.SearchResultEntry);
276                 
277                                 ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, search->dn);
278                                 if ( ! ldb_dn_validate(ares->message->dn)) {
279                                         handle->status = LDB_ERR_OPERATIONS_ERROR;
280                                         return;
281                                 }
282                                 ares->message->num_elements = search->num_attributes;
283                                 ares->message->elements = talloc_move(ares->message,
284                                                                       &search->attributes);
285
286                                 handle->status = LDB_SUCCESS;
287                                 handle->state = LDB_ASYNC_PENDING;
288                                 ares->type = LDB_REPLY_ENTRY;
289                                 break;
290
291                         case LDAP_TAG_SearchResultReference:
292
293                                 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
294                                 
295                                 handle->status = LDB_SUCCESS;
296                                 handle->state = LDB_ASYNC_PENDING;
297                                 ares->type = LDB_REPLY_REFERRAL;
298                                 break;
299
300                         default:
301                                 /* TAG not handled, fail ! */
302                                 handle->status = LDB_ERR_PROTOCOL_ERROR;
303                                 return;
304                         }
305
306                         ret = ac->callback(ac->module->ldb, ac->context, ares);
307                         if (ret) {
308                                 handle->status = ret;
309                         }
310                 }
311
312                 talloc_free(req->replies);
313                 req->replies = NULL;
314                 req->num_replies = 0;
315
316                 break;
317                 
318         default:
319                 handle->status = LDB_ERR_PROTOCOL_ERROR;
320                 return;
321         }
322 }
323
324 static struct ldb_handle *init_ildb_handle(struct ldb_module *module, 
325                                            void *context,
326                                            int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
327 {
328         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
329         struct ildb_context *ildb_ac;
330         struct ldb_handle *h;
331
332         h = talloc_zero(ildb->ldap, struct ldb_handle);
333         if (h == NULL) {
334                 ldb_set_errstring(module->ldb, "Out of Memory");
335                 return NULL;
336         }
337
338         h->module = module;
339
340         ildb_ac = talloc(h, struct ildb_context);
341         if (ildb_ac == NULL) {
342                 ldb_set_errstring(module->ldb, "Out of Memory");
343                 talloc_free(h);
344                 return NULL;
345         }
346
347         h->private_data = (void *)ildb_ac;
348
349         h->state = LDB_ASYNC_INIT;
350         h->status = LDB_SUCCESS;
351
352         ildb_ac->module = module;
353         ildb_ac->context = context;
354         ildb_ac->callback = callback;
355
356         return h;
357 }
358
359 static int ildb_request_send(struct ildb_private *ildb, struct ldap_message *msg, struct ldb_request *r)
360 {
361         struct ldb_handle *h = init_ildb_handle(ildb->module, r->context, r->callback);
362         struct ildb_context *ildb_ac;
363         struct ldap_request *req;
364
365         if (!h) {
366                 return LDB_ERR_OPERATIONS_ERROR;                
367         }
368
369         ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
370
371         req = ldap_request_send(ildb->ldap, msg);
372         if (req == NULL) {
373                 ldb_set_errstring(ildb->module->ldb, "async send request failed");
374                 return LDB_ERR_OPERATIONS_ERROR;
375         }
376
377         if (!req->conn) {
378                 ldb_set_errstring(ildb->module->ldb, "connection to remote LDAP server dropped?");
379                 return LDB_ERR_OPERATIONS_ERROR;
380         }
381
382         talloc_free(req->time_event);
383         req->time_event = NULL;
384         if (r->timeout) {
385                 req->time_event = event_add_timed(req->conn->event.event_ctx, h, 
386                                                   timeval_current_ofs(r->timeout, 0),
387                                                   ildb_request_timeout, h);
388         }
389
390         req->async.fn = ildb_callback;
391         req->async.private_data = h;
392         ildb_ac->req = talloc_move(ildb_ac, &req);
393
394         r->handle = h;
395         return LDB_SUCCESS;
396 }
397
398 static int ildb_request_noop(struct ildb_private *ildb, struct ldb_request *req) 
399 {
400         struct ldb_handle *h = init_ildb_handle(ildb->module, req->context, req->callback);
401         struct ildb_context *ildb_ac;
402         int ret = LDB_SUCCESS;
403
404         if (!h) {
405                 return LDB_ERR_OPERATIONS_ERROR;                
406         }
407
408         ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
409
410         req->handle = h;
411
412         if (ildb_ac->callback) {
413                 ret = ildb_ac->callback(ildb->module->ldb, ildb_ac->context, NULL);
414         }
415         req->handle->state = LDB_ASYNC_DONE;
416         return ret;
417 }
418
419 /*
420   search for matching records using an asynchronous function
421  */
422 static int ildb_search(struct ldb_module *module, struct ldb_request *req)
423 {
424         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
425         struct ldap_message *msg;
426         int n;
427
428         req->handle = NULL;
429
430         if (!req->callback || !req->context) {
431                 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
432                 return LDB_ERR_OPERATIONS_ERROR;
433         }
434         
435         if (req->op.search.tree == NULL) {
436                 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
437                 return LDB_ERR_OPERATIONS_ERROR;
438         }
439
440         msg = new_ldap_message(ildb);
441         if (msg == NULL) {
442                 ldb_set_errstring(module->ldb, "Out of Memory");
443                 return LDB_ERR_OPERATIONS_ERROR;
444         }
445
446         msg->type = LDAP_TAG_SearchRequest;
447
448         if (req->op.search.base == NULL) {
449                 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
450         } else {
451                 msg->r.SearchRequest.basedn  = ldb_dn_alloc_linearized(msg, req->op.search.base);
452         }
453         if (msg->r.SearchRequest.basedn == NULL) {
454                 ldb_set_errstring(module->ldb, "Unable to determine baseDN");
455                 talloc_free(msg);
456                 return LDB_ERR_OPERATIONS_ERROR;
457         }
458
459         if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
460                 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
461         } else {
462                 msg->r.SearchRequest.scope = req->op.search.scope;
463         }
464         
465         msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
466         msg->r.SearchRequest.timelimit = 0;
467         msg->r.SearchRequest.sizelimit = 0;
468         msg->r.SearchRequest.attributesonly = 0;
469         msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
470         
471         for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
472         msg->r.SearchRequest.num_attributes = n;
473         msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
474         msg->controls = req->controls;
475
476         return ildb_request_send(ildb, msg, req);
477 }
478
479 /*
480   add a record
481 */
482 static int ildb_add(struct ldb_module *module, struct ldb_request *req)
483 {
484         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
485         struct ldap_message *msg;
486         struct ldap_mod **mods;
487         int i,n;
488
489         req->handle = NULL;
490
491         /* ignore ltdb specials */
492         if (ldb_dn_is_special(req->op.add.message->dn)) {
493                 return ildb_request_noop(ildb, req);
494         }
495
496         msg = new_ldap_message(ildb->ldap);
497         if (msg == NULL) {
498                 return LDB_ERR_OPERATIONS_ERROR;
499         }
500
501         msg->type = LDAP_TAG_AddRequest;
502
503         msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
504         if (msg->r.AddRequest.dn == NULL) {
505                 talloc_free(msg);
506                 return LDB_ERR_INVALID_DN_SYNTAX;
507         }
508
509         mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
510         if (mods == NULL) {
511                 talloc_free(msg);
512                 return LDB_ERR_OPERATIONS_ERROR;
513         }
514
515         msg->r.AddRequest.num_attributes = n;
516         msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
517         if (msg->r.AddRequest.attributes == NULL) {
518                 talloc_free(msg);
519                 return LDB_ERR_OPERATIONS_ERROR;
520         }
521
522         for (i = 0; i < n; i++) {
523                 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
524         }
525
526         return ildb_request_send(ildb, msg, req);
527 }
528
529 /*
530   modify a record
531 */
532 static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
533 {
534         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
535         struct ldap_message *msg;
536         struct ldap_mod **mods;
537         int i,n;
538
539         req->handle = NULL;
540
541         /* ignore ltdb specials */
542         if (ldb_dn_is_special(req->op.mod.message->dn)) {
543                 return ildb_request_noop(ildb, req);
544         }
545
546         msg = new_ldap_message(ildb->ldap);
547         if (msg == NULL) {
548                 return LDB_ERR_OPERATIONS_ERROR;
549         }
550
551         msg->type = LDAP_TAG_ModifyRequest;
552
553         msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
554         if (msg->r.ModifyRequest.dn == NULL) {
555                 talloc_free(msg);
556                 return LDB_ERR_INVALID_DN_SYNTAX;
557         }
558
559         mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
560         if (mods == NULL) {
561                 talloc_free(msg);
562                 return LDB_ERR_OPERATIONS_ERROR;
563         }
564
565         msg->r.ModifyRequest.num_mods = n;
566         msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
567         if (msg->r.ModifyRequest.mods == NULL) {
568                 talloc_free(msg);
569                 return LDB_ERR_OPERATIONS_ERROR;
570         }
571
572         for (i = 0; i < n; i++) {
573                 msg->r.ModifyRequest.mods[i] = *mods[i];
574         }
575
576         return ildb_request_send(ildb, msg, req);
577 }
578
579 /*
580   delete a record
581 */
582 static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
583 {
584         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
585         struct ldap_message *msg;
586
587         req->handle = NULL;
588
589         /* ignore ltdb specials */
590         if (ldb_dn_is_special(req->op.del.dn)) {
591                 return ildb_request_noop(ildb, req);
592         }
593
594         msg = new_ldap_message(ildb->ldap);
595         if (msg == NULL) {
596                 return LDB_ERR_OPERATIONS_ERROR;
597         }
598
599         msg->type = LDAP_TAG_DelRequest;
600         
601         msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
602         if (msg->r.DelRequest.dn == NULL) {
603                 talloc_free(msg);
604                 return LDB_ERR_INVALID_DN_SYNTAX;
605         }
606
607         return ildb_request_send(ildb, msg, req);
608 }
609
610 /*
611   rename a record
612 */
613 static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
614 {
615         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
616         struct ldap_message *msg;
617
618         req->handle = NULL;
619
620         /* ignore ltdb specials */
621         if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
622                 return ildb_request_noop(ildb, req);
623         }
624
625         msg = new_ldap_message(ildb->ldap);
626         if (msg == NULL) {
627                 return LDB_ERR_OPERATIONS_ERROR;
628         }
629
630         msg->type = LDAP_TAG_ModifyDNRequest;
631         msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
632         if (msg->r.ModifyDNRequest.dn == NULL) {
633                 talloc_free(msg);
634                 return LDB_ERR_INVALID_DN_SYNTAX;
635         }
636
637         msg->r.ModifyDNRequest.newrdn = 
638                 talloc_asprintf(msg, "%s=%s",
639                                 ldb_dn_get_rdn_name(req->op.rename.newdn),
640                                 ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
641         if (msg->r.ModifyDNRequest.newrdn == NULL) {
642                 talloc_free(msg);
643                 return LDB_ERR_OPERATIONS_ERROR;
644         }
645
646         msg->r.ModifyDNRequest.newsuperior =
647                 ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
648         if (msg->r.ModifyDNRequest.newsuperior == NULL) {
649                 talloc_free(msg);
650                 return LDB_ERR_INVALID_DN_SYNTAX;
651         }
652
653         msg->r.ModifyDNRequest.deleteolddn = True;
654
655         return ildb_request_send(ildb, msg, req);
656 }
657
658 static int ildb_start_trans(struct ldb_module *module)
659 {
660         /* TODO implement a local locking mechanism here */
661
662         return LDB_SUCCESS;
663 }
664
665 static int ildb_end_trans(struct ldb_module *module)
666 {
667         /* TODO implement a local transaction mechanism here */
668
669         return LDB_SUCCESS;
670 }
671
672 static int ildb_del_trans(struct ldb_module *module)
673 {
674         /* TODO implement a local locking mechanism here */
675
676         return LDB_SUCCESS;
677 }
678
679 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
680 {
681         return LDB_ERR_OPERATIONS_ERROR;
682 }
683
684 static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
685 {
686         struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
687
688         if (handle->state == LDB_ASYNC_DONE) {
689                 return handle->status;
690         }
691
692         if (!ac) {
693                 return LDB_ERR_OPERATIONS_ERROR;
694         }
695
696         handle->state = LDB_ASYNC_INIT;
697
698         switch(type) {
699         case LDB_WAIT_NONE:
700                 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
701                         return LDB_ERR_OTHER;
702                 }
703                 break;
704         case LDB_WAIT_ALL:
705                 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
706                         if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
707                                 return LDB_ERR_OTHER;
708                         }
709                 }
710                 break;
711         default:
712                 return LDB_ERR_OPERATIONS_ERROR;
713         }
714         
715         return handle->status;
716 }
717
718 static const struct ldb_module_ops ildb_ops = {
719         .name              = "ldap",
720         .search            = ildb_search,
721         .add               = ildb_add,
722         .modify            = ildb_modify,
723         .del               = ildb_delete,
724         .rename            = ildb_rename,
725         .request           = ildb_request,
726         .start_transaction = ildb_start_trans,
727         .end_transaction   = ildb_end_trans,
728         .del_transaction   = ildb_del_trans,
729         .wait              = ildb_wait
730 };
731
732 /*
733   connect to the database
734 */
735 static int ildb_connect(struct ldb_context *ldb, const char *url, 
736                         unsigned int flags, const char *options[],
737                         struct ldb_module **_module)
738 {
739         struct ldb_module *module;
740         struct ildb_private *ildb;
741         NTSTATUS status;
742         struct cli_credentials *creds;
743
744         module = talloc(ldb, struct ldb_module);
745         if (!module) {
746                 ldb_oom(ldb);
747                 return -1;
748         }
749         talloc_set_name_const(module, "ldb_ildap backend");
750         module->ldb             = ldb;
751         module->prev            = module->next = NULL;
752         module->private_data    = NULL;
753         module->ops             = &ildb_ops;
754
755         ildb = talloc(module, struct ildb_private);
756         if (!ildb) {
757                 ldb_oom(ldb);
758                 goto failed;
759         }
760         module->private_data    = ildb;
761         ildb->ldb               = ldb;
762         ildb->module            = module;
763         ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
764         if (!ildb->ldap) {
765                 ldb_oom(ldb);
766                 goto failed;
767         }
768
769         if (flags & LDB_FLG_RECONNECT) {
770                 ldap_set_reconn_params(ildb->ldap, 10);
771         }
772
773         status = ldap_connect(ildb->ldap, url);
774         if (!NT_STATUS_IS_OK(status)) {
775                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
776                           url, ldap_errstr(ildb->ldap, status));
777                 goto failed;
778         }
779
780         /* caller can optionally setup credentials using the opaque token 'credentials' */
781         creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
782         if (creds == NULL) {
783                 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
784                 if (session_info) {
785                         creds = session_info->credentials;
786                 }
787         }
788
789         if (creds != NULL && cli_credentials_authentication_requested(creds)) {
790                 const char *bind_dn = cli_credentials_get_bind_dn(creds);
791                 if (bind_dn) {
792                         const char *password = cli_credentials_get_password(creds);
793                         status = ldap_bind_simple(ildb->ldap, bind_dn, password);
794                         if (!NT_STATUS_IS_OK(status)) {
795                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
796                                           ldap_errstr(ildb->ldap, status));
797                                 goto failed;
798                         }
799                 } else {
800                         status = ldap_bind_sasl(ildb->ldap, creds);
801                         if (!NT_STATUS_IS_OK(status)) {
802                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
803                                           ldap_errstr(ildb->ldap, status));
804                                 goto failed;
805                         }
806                 }
807         }
808
809         *_module = module;
810         return 0;
811
812 failed:
813         talloc_free(module);
814         return -1;
815 }
816
817 int ldb_ildap_init(void)
818 {
819         return ldb_register_backend("ldap", ildb_connect) + 
820                    ldb_register_backend("ldapi", ildb_connect) + 
821                    ldb_register_backend("ldaps", ildb_connect);
822 }