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