33d3954a73ae00b8b99eef00244cc9fd9208cb97
[kai/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   This is a ldb backend for the internal ldap client library in
28   Samba4. By using this backend we are independent of a system ldap
29   library
30 */
31
32
33 #include "includes.h"
34 #include "ldb/include/includes.h"
35
36 #include "lib/events/events.h"
37 #include "libcli/ldap/ldap.h"
38 #include "libcli/ldap/ldap_client.h"
39 #include "lib/cmdline/popt_common.h"
40 #include "auth/auth.h"
41
42 struct ildb_private {
43         struct ldap_connection *ldap;
44         struct ldb_message *rootDSE;
45         struct ldb_context *ldb;
46 };
47
48 struct ildb_async_context {
49         struct ldb_module *module;
50         struct ldap_request *req;
51         void *context;
52         int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
53 };
54
55 /*
56   convert a ldb_message structure to a list of ldap_mod structures
57   ready for ildap_add() or ildap_modify()
58 */
59 static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
60                                           const struct ldb_message *msg, int use_flags)
61 {
62         struct ldap_mod **mods;
63         unsigned int i;
64         int n = 0;
65
66         /* allocate maximum number of elements needed */
67         mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
68         if (!mods) {
69                 errno = ENOMEM;
70                 return NULL;
71         }
72         mods[0] = NULL;
73
74         for (i = 0; i < msg->num_elements; i++) {
75                 const struct ldb_message_element *el = &msg->elements[i];
76
77                 mods[n] = talloc(mods, struct ldap_mod);
78                 if (!mods[n]) {
79                         goto failed;
80                 }
81                 mods[n + 1] = NULL;
82                 mods[n]->type = 0;
83                 mods[n]->attrib = *el;
84                 if (use_flags) {
85                         switch (el->flags & LDB_FLAG_MOD_MASK) {
86                         case LDB_FLAG_MOD_ADD:
87                                 mods[n]->type = LDAP_MODIFY_ADD;
88                                 break;
89                         case LDB_FLAG_MOD_DELETE:
90                                 mods[n]->type = LDAP_MODIFY_DELETE;
91                                 break;
92                         case LDB_FLAG_MOD_REPLACE:
93                                 mods[n]->type = LDAP_MODIFY_REPLACE;
94                                 break;
95                         }
96                 }
97                 n++;
98         }
99
100         *num_mods = n;
101         return mods;
102
103 failed:
104         talloc_free(mods);
105         return NULL;
106 }
107
108
109 /*
110   map an ildap NTSTATUS to a ldb error code
111 */
112 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
113 {
114         if (NT_STATUS_IS_OK(status)) {
115                 return LDB_SUCCESS;
116         }
117         talloc_free(ildb->ldb->err_string);
118         ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status));
119         if (NT_STATUS_IS_LDAP(status)) {
120                 return NT_STATUS_LDAP_CODE(status);
121         }
122         return LDB_ERR_OPERATIONS_ERROR;
123 }
124
125 static void ildb_request_timeout(struct event_context *ev, struct timed_event *te,
126                                  struct timeval t, void *private_data)
127 {
128         struct ldb_async_handle *handle = talloc_get_type(private_data, struct ldb_async_handle);
129         struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
130
131         if (ac->req->state == LDAP_REQUEST_PENDING) {
132                 DLIST_REMOVE(ac->req->conn->pending, ac->req);
133         }
134
135         handle->status = LDB_ERR_OPERATIONS_ERROR;
136
137         return;
138 }
139
140 static void ildb_async_callback(struct ldap_request *req)
141 {
142         struct ldb_async_handle *handle = talloc_get_type(req->async.private_data, struct ldb_async_handle);
143         struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
144         struct ildb_private *ildb = ac->module->private_data;
145         NTSTATUS status;
146         int i;
147
148         handle->status = LDB_SUCCESS;
149
150         if (!NT_STATUS_IS_OK(req->status)) {
151                 handle->status = ildb_map_error(ildb, req->status);
152                 return;
153         }
154
155         if (req->num_replies < 1) {
156                 handle->status = LDB_ERR_OPERATIONS_ERROR;
157                 return;
158         } 
159                 
160         switch (req->type) {
161
162         case LDAP_TAG_ModifyRequest:
163                 if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
164                         handle->status = LDB_ERR_PROTOCOL_ERROR;
165                         return;
166                 }
167                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
168                 handle->status = ildb_map_error(ildb, status);
169                 handle->state = LDB_ASYNC_DONE;
170                 break;
171
172         case LDAP_TAG_AddRequest:
173                 if (req->replies[0]->type != LDAP_TAG_AddResponse) {
174                         handle->status = LDB_ERR_PROTOCOL_ERROR;
175                         return;
176                 }
177                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
178                 handle->status = ildb_map_error(ildb, status);
179                 handle->state = LDB_ASYNC_DONE;
180                 break;
181
182         case LDAP_TAG_DelRequest:
183                 if (req->replies[0]->type != LDAP_TAG_DelResponse) {
184                         handle->status = LDB_ERR_PROTOCOL_ERROR;
185                         return;
186                 }
187                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
188                 handle->status = ildb_map_error(ildb, status);
189                 handle->state = LDB_ASYNC_DONE;
190                 break;
191
192         case LDAP_TAG_ModifyDNRequest:
193                 if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
194                         handle->status = LDB_ERR_PROTOCOL_ERROR;
195                         return;
196                 }
197                 status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult);
198                 handle->status = ildb_map_error(ildb, status);
199                 handle->state = LDB_ASYNC_DONE;
200                 break;
201
202         case LDAP_TAG_SearchRequest:
203                 /* loop over all messages */
204                 for (i = 0; i < req->num_replies; i++) {
205                         struct ldap_SearchResEntry *search;
206                         struct ldb_async_result *ares = NULL;
207                         struct ldap_message *msg;
208                         int ret;
209
210                         ares = talloc_zero(ac, struct ldb_async_result);
211                         if (!ares) {
212                                 handle->status = LDB_ERR_OPERATIONS_ERROR;
213                                 return;
214                         }
215
216                         msg = req->replies[i];
217                         switch (msg->type) {
218
219                         case LDAP_TAG_SearchResultDone:
220
221                                 status = ldap_check_response(req->conn, &msg->r.GeneralResult);
222                                 if (!NT_STATUS_IS_OK(status)) {
223                                         ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Error: %s\n" ,ldap_errstr(req->conn, status));
224                                         handle->status = ildb_map_error(ildb, status);
225                                         return;
226                                 }
227                                 
228                                 if (msg->controls) {
229                                         ares->controls = talloc_steal(ares, msg->controls);
230                                 }
231                                 if (msg->r.SearchResultDone.resultcode) {
232                                         if (msg->r.SearchResultDone.errormessage) {
233                                                 ldb_set_errstring(ac->module, talloc_strdup(ac->module, msg->r.SearchResultDone.errormessage));
234                                         }
235                                 }
236
237                                 handle->status = msg->r.SearchResultDone.resultcode;
238                                 handle->state = LDB_ASYNC_DONE;
239                                 ares->type = LDB_REPLY_DONE;
240                                 break;
241
242                         case LDAP_TAG_SearchResultEntry:
243
244
245                                 ares->message = ldb_msg_new(ares);
246                                 if (!ares->message) {
247                                         handle->status = LDB_ERR_OPERATIONS_ERROR;;
248                                         return;
249                                 }
250
251                                 search = &(msg->r.SearchResultEntry);
252                 
253                                 ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn);
254                                 if (ares->message->dn == NULL) {
255                                         handle->status = LDB_ERR_OPERATIONS_ERROR;
256                                         return;
257                                 }
258                                 ares->message->num_elements = search->num_attributes;
259                                 ares->message->elements = talloc_steal(ares->message, search->attributes);
260
261                                 handle->status = LDB_SUCCESS;
262                                 handle->state = LDB_ASYNC_PENDING;
263                                 ares->type = LDB_REPLY_ENTRY;
264                                 break;
265
266                         case LDAP_TAG_SearchResultReference:
267
268                                 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
269                                 
270                                 handle->status = LDB_SUCCESS;
271                                 handle->state = LDB_ASYNC_PENDING;
272                                 ares->type = LDB_REPLY_REFERRAL;
273                                 break;
274
275                         default:
276                                 /* TAG not handled, fail ! */
277                                 handle->status = LDB_ERR_PROTOCOL_ERROR;
278                                 return;
279                         }
280
281                         ret = ac->callback(ac->module->ldb, ac->context, ares);
282                         if (ret) {
283                                 handle->status = ret;
284                         }
285                 }
286
287                 talloc_free(req->replies);
288                 req->replies = NULL;
289                 req->num_replies = 0;
290
291                 break;
292                 
293         default:
294                 handle->status = LDB_ERR_PROTOCOL_ERROR;
295                 return;
296         }
297 }
298
299 static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
300                              void *context,
301                              int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
302                              int timeout,
303                              struct ldb_async_handle **handle)
304 {
305         struct ildb_private *ildb = module->private_data;
306         struct ildb_async_context *ildb_ac;
307         struct ldb_async_handle *h;
308         struct ldap_request *req;
309
310         h = talloc_zero(ildb->ldap, struct ldb_async_handle);
311         if (h == NULL) {
312                 ldb_set_errstring(module, talloc_asprintf(module, "Out of Memory"));
313                 return LDB_ERR_OPERATIONS_ERROR;
314         }
315
316         ildb_ac = talloc(h, struct ildb_async_context);
317         if (ildb_ac == NULL) {
318                 ldb_set_errstring(module, talloc_asprintf(module, "Out of Memory"));
319                 talloc_free(h);
320                 return LDB_ERR_OPERATIONS_ERROR;
321         }
322
323         h->private_data = (void *)ildb_ac;
324
325         req = ldap_request_send(ildb->ldap, msg);
326         if (req == NULL) {
327                 ldb_set_errstring(module, talloc_asprintf(module, "async send request failed"));
328                 return LDB_ERR_OPERATIONS_ERROR;
329         }
330
331         ildb_ac->req = talloc_steal(ildb_ac, req);
332         ildb_ac->module = module;
333         ildb_ac->context = context;
334         ildb_ac->callback = callback;
335
336         req->async.fn = ildb_async_callback;
337         req->async.private_data = (void *)h;
338
339         talloc_free(req->time_event);
340         req->time_event = NULL;
341         if (timeout) {
342                 req->time_event = event_add_timed(req->conn->event.event_ctx, h, 
343                                                   timeval_current_ofs(timeout, 0),
344                                                   ildb_request_timeout, ildb_ac);
345         }
346
347         *handle = h;
348
349         return LDB_SUCCESS;
350
351 }
352
353 /*
354   search for matching records using an asynchronous function
355  */
356 static int ildb_search_async(struct ldb_module *module, const struct ldb_dn *base,
357                               enum ldb_scope scope, struct ldb_parse_tree *tree,
358                               const char * const *attrs,
359                               struct ldb_control **control_req,
360                               void *context,
361                               int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
362                               int timeout,
363                               struct ldb_async_handle **handle)
364 {
365         struct ildb_private *ildb = module->private_data;
366         struct ldap_message *msg;
367         int n;
368
369         *handle = NULL;
370
371         if (!callback || !context) {
372                 ldb_set_errstring(module, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
373                 return LDB_ERR_OPERATIONS_ERROR;
374         }
375         
376         if (tree == NULL) {
377                 ldb_set_errstring(module, talloc_asprintf(module, "Invalid expression parse tree"));
378                 return LDB_ERR_OPERATIONS_ERROR;
379         }
380
381         msg = new_ldap_message(ildb);
382         if (msg == NULL) {
383                 ldb_set_errstring(module, talloc_asprintf(module, "Out of Memory"));
384                 return LDB_ERR_OPERATIONS_ERROR;
385         }
386
387         msg->type = LDAP_TAG_SearchRequest;
388
389         if (base == NULL) {
390                 if (ildb->rootDSE != NULL) {
391                         msg->r.SearchRequest.basedn =
392                                 talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", ""));
393                 } else {
394                         msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
395                 }
396         } else {
397                 msg->r.SearchRequest.basedn  = ldb_dn_linearize(msg, base);
398         }
399         if (msg->r.SearchRequest.basedn == NULL) {
400                 ldb_set_errstring(module, talloc_asprintf(module, "Unable to determine baseDN"));
401                 talloc_free(msg);
402                 return LDB_ERR_OPERATIONS_ERROR;
403         }
404
405         if (scope == LDB_SCOPE_DEFAULT) {
406                 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
407         } else {
408                 msg->r.SearchRequest.scope = scope;
409         }
410         
411         msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
412         msg->r.SearchRequest.timelimit = 0;
413         msg->r.SearchRequest.sizelimit = 0;
414         msg->r.SearchRequest.attributesonly = 0;
415         msg->r.SearchRequest.tree = tree;
416         
417         for (n = 0; attrs && attrs[n]; n++) /* noop */ ;
418         msg->r.SearchRequest.num_attributes = n;
419         msg->r.SearchRequest.attributes = discard_const(attrs);
420         msg->controls = control_req;
421
422         return ildb_request_send(module, msg, context, callback, timeout, handle);
423 }
424
425 struct ildb_sync_context {
426         struct ldb_result *res;
427         int status;
428         int done;
429 };
430
431 static int ildb_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
432 {
433         struct ldb_result *res;
434         int n;
435         
436         if (!context) {
437                 ldb_set_errstring(ldb->modules, talloc_asprintf(ldb, "NULL Context in callback"));
438                 return LDB_ERR_OPERATIONS_ERROR;
439         }       
440
441         res = *((struct ldb_result **)context);
442
443         if (!res || !ares) {
444                 goto error;
445         }
446
447         if (ares->type == LDB_REPLY_ENTRY) {
448                 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
449                 if (! res->msgs) {
450                         goto error;
451                 }
452
453                 res->msgs[res->count + 1] = NULL;
454
455                 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
456                 if (! res->msgs[res->count]) {
457                         goto error;
458                 }
459
460                 res->count++;
461         }
462
463         if (ares->type == LDB_REPLY_REFERRAL) {
464                 if (res->refs) {
465                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
466                 } else {
467                         n = 0;
468                 }
469
470                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
471                 if (! res->refs) {
472                         goto error;
473                 }
474
475                 res->refs[n] = talloc_steal(res->refs, ares->referral);
476                 res->refs[n + 1] = NULL;
477         }
478
479         if (ares->type == LDB_REPLY_DONE) {
480                 if (ares->controls) {
481                         res->controls = talloc_steal(res, ares->controls);
482                         if (! res->controls) {
483                                 goto error;
484                         }
485                 }
486         }
487
488         talloc_free(ares);
489         return LDB_SUCCESS;
490
491 error:
492         talloc_free(ares);
493         talloc_free(res);
494         *((struct ldb_result **)context) = NULL;
495         return LDB_ERR_OPERATIONS_ERROR;
496 }
497
498 /*
499   search for matching records using a synchronous function
500  */
501 static int ildb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
502                               enum ldb_scope scope, struct ldb_parse_tree *tree,
503                               const char * const *attrs,
504                               struct ldb_control **control_req,
505                               struct ldb_result **res)
506 {
507         struct ildb_private *ildb = module->private_data;
508         struct ldb_async_handle *handle;
509         int ret;
510
511         *res = talloc_zero(ildb, struct ldb_result);
512         if (! *res) {
513                 return LDB_ERR_OPERATIONS_ERROR;
514         }
515
516         ret = ildb_search_async(module, base, scope, tree, attrs, control_req,
517                                 res, &ildb_search_sync_callback, ildb->ldap->timeout, &handle);
518
519         if (ret != LDB_SUCCESS)
520                 return ret;
521
522         return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
523 }
524
525 /*
526   add a record
527 */
528 static int ildb_add_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
529                           void *context,
530                           int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
531                           int timeout,
532                           struct ldb_async_handle **handle)
533 {
534         struct ildb_private *ildb = module->private_data;
535         struct ldap_message *msg;
536         struct ldap_mod **mods;
537         int i,n;
538
539         *handle = NULL;
540
541         /* ignore ltdb specials */
542         if (ldb_dn_is_special(ldb_msg->dn)) {
543                 return LDB_SUCCESS;
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_AddRequest;
552
553         msg->r.AddRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
554         if (msg->r.AddRequest.dn == NULL) {
555                 talloc_free(msg);
556                 return LDB_ERR_INVALID_DN_SYNTAX;
557         }
558
559         mods = ildb_msg_to_mods(msg, &n, ldb_msg, 0);
560         if (mods == NULL) {
561                 talloc_free(msg);
562                 return LDB_ERR_OPERATIONS_ERROR;
563         }
564
565         msg->r.AddRequest.num_attributes = n;
566         msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
567         if (msg->r.AddRequest.attributes == NULL) {
568                 talloc_free(msg);
569                 return LDB_ERR_OPERATIONS_ERROR;
570         }
571
572         for (i = 0; i < n; i++) {
573                 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
574         }
575
576         return ildb_request_send(module, msg, context, callback, timeout, handle);
577 }
578
579 static int ildb_add(struct ldb_module *module, const struct ldb_message *msg)
580 {
581         struct ildb_private *ildb = module->private_data;
582         struct ldb_async_handle *handle;
583         int ret;
584
585         ret = ildb_add_async(module, msg,
586                                 NULL, NULL, ildb->ldap->timeout, &handle);
587
588         if (ret != LDB_SUCCESS)
589                 return ret;
590
591         return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
592 }
593
594 /*
595   modify a record
596 */
597 static int ildb_modify_async(struct ldb_module *module, const struct ldb_message *ldb_msg,
598                              void *context,
599                              int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
600                              int timeout,
601                              struct ldb_async_handle **handle)
602 {
603         struct ildb_private *ildb = module->private_data;
604         struct ldap_message *msg;
605         struct ldap_mod **mods;
606         int i,n;
607
608         *handle = NULL;
609
610         /* ignore ltdb specials */
611         if (ldb_dn_is_special(ldb_msg->dn)) {
612                 return LDB_SUCCESS;
613         }
614
615         msg = new_ldap_message(ildb->ldap);
616         if (msg == NULL) {
617                 return LDB_ERR_OPERATIONS_ERROR;
618         }
619
620         msg->type = LDAP_TAG_ModifyRequest;
621
622         msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn);
623         if (msg->r.ModifyRequest.dn == NULL) {
624                 talloc_free(msg);
625                 return LDB_ERR_INVALID_DN_SYNTAX;
626         }
627
628         mods = ildb_msg_to_mods(msg, &n, ldb_msg, 1);
629         if (mods == NULL) {
630                 talloc_free(msg);
631                 return LDB_ERR_OPERATIONS_ERROR;
632         }
633
634         msg->r.ModifyRequest.num_mods = n;
635         msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
636         if (msg->r.ModifyRequest.mods == NULL) {
637                 talloc_free(msg);
638                 return LDB_ERR_OPERATIONS_ERROR;
639         }
640
641         for (i = 0; i < n; i++) {
642                 msg->r.ModifyRequest.mods[i] = *mods[i];
643         }
644
645         return ildb_request_send(module, msg, context, callback, timeout, handle);
646 }
647
648 static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg)
649 {
650         struct ildb_private *ildb = module->private_data;
651         struct ldb_async_handle *handle;
652         int ret;
653
654         ret = ildb_modify_async(module, msg,
655                                 NULL, NULL, ildb->ldap->timeout, &handle);
656
657         if (ret != LDB_SUCCESS)
658                 return ret;
659
660         return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
661 }
662
663 /*
664   delete a record
665 */
666 static int ildb_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
667                              void *context,
668                              int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
669                              int timeout,
670                              struct ldb_async_handle **handle)
671 {
672         struct ildb_private *ildb = module->private_data;
673         struct ldap_message *msg;
674
675         *handle = NULL;
676
677         /* ignore ltdb specials */
678         if (ldb_dn_is_special(dn)) {
679                 return LDB_SUCCESS;
680         }
681
682         msg = new_ldap_message(ildb->ldap);
683         if (msg == NULL) {
684                 return LDB_ERR_OPERATIONS_ERROR;
685         }
686
687         msg->type = LDAP_TAG_DelRequest;
688         
689         msg->r.DelRequest.dn = ldb_dn_linearize(msg, dn);
690         if (msg->r.DelRequest.dn == NULL) {
691                 talloc_free(msg);
692                 return LDB_ERR_INVALID_DN_SYNTAX;
693         }
694
695         return ildb_request_send(module, msg, context, callback, timeout, handle);
696 }
697
698 static int ildb_delete(struct ldb_module *module, const struct ldb_dn *dn)
699 {
700         struct ildb_private *ildb = module->private_data;
701         struct ldb_async_handle *handle;
702         int ret;
703
704         ret = ildb_delete_async(module, dn,
705                                 NULL, NULL, ildb->ldap->timeout, &handle);
706
707         if (ret != LDB_SUCCESS)
708                 return ret;
709
710         return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
711 }
712
713 /*
714   rename a record
715 */
716 static int ildb_rename_async(struct ldb_module *module,
717                              const struct ldb_dn *olddn, const struct ldb_dn *newdn,
718                              void *context,
719                              int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
720                              int timeout,
721                              struct ldb_async_handle **handle)
722 {
723         struct ildb_private *ildb = module->private_data;
724         struct ldap_message *msg;
725
726         *handle = NULL;
727
728         /* ignore ltdb specials */
729         if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
730                 return LDB_SUCCESS;
731         }
732
733         msg = new_ldap_message(ildb->ldap);
734         if (msg == NULL) {
735                 return LDB_ERR_OPERATIONS_ERROR;
736         }
737
738         msg->type = LDAP_TAG_ModifyDNRequest;
739         msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, olddn);
740         if (msg->r.ModifyDNRequest.dn == NULL) {
741                 talloc_free(msg);
742                 return LDB_ERR_INVALID_DN_SYNTAX;
743         }
744
745         msg->r.ModifyDNRequest.newrdn = 
746                 talloc_asprintf(msg, "%s=%s",
747                                 newdn->components[0].name,
748                                 ldb_dn_escape_value(msg, newdn->components[0].value));
749         if (msg->r.ModifyDNRequest.newrdn == NULL) {
750                 talloc_free(msg);
751                 return LDB_ERR_OPERATIONS_ERROR;
752         }
753
754         msg->r.ModifyDNRequest.newsuperior =
755                 ldb_dn_linearize(msg,
756                                  ldb_dn_get_parent(msg, newdn));
757         if (msg->r.ModifyDNRequest.newsuperior == NULL) {
758                 talloc_free(msg);
759                 return LDB_ERR_INVALID_DN_SYNTAX;
760         }
761
762         msg->r.ModifyDNRequest.deleteolddn = True;
763
764         return ildb_request_send(module, msg, context, callback, timeout, handle);
765 }
766
767 static int ildb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
768 {
769         struct ildb_private *ildb = module->private_data;
770         struct ldb_async_handle *handle;
771         int ret;
772
773         ret = ildb_rename_async(module, olddn, newdn,
774                                 NULL, NULL, ildb->ldap->timeout, &handle);
775
776         if (ret != LDB_SUCCESS)
777                 return ret;
778
779         return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
780 }
781
782 static int ildb_start_trans(struct ldb_module *module)
783 {
784         /* TODO implement a local locking mechanism here */
785
786         return 0;
787 }
788
789 static int ildb_end_trans(struct ldb_module *module)
790 {
791         /* TODO implement a local transaction mechanism here */
792
793         return 0;
794 }
795
796 static int ildb_del_trans(struct ldb_module *module)
797 {
798         /* TODO implement a local locking mechanism here */
799
800         return 0;
801 }
802
803 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
804 {
805         switch (req->operation) {
806
807         case LDB_REQ_SEARCH:
808                 return ildb_search_bytree(module,
809                                           req->op.search.base,
810                                           req->op.search.scope, 
811                                           req->op.search.tree, 
812                                           req->op.search.attrs, 
813                                           req->controls,
814                                           &req->op.search.res);
815
816         case LDB_REQ_ADD:
817                 return ildb_add(module, req->op.add.message);
818
819         case LDB_REQ_MODIFY:
820                 return ildb_modify(module, req->op.mod.message);
821
822         case LDB_REQ_DELETE:
823                 return ildb_delete(module, req->op.del.dn);
824
825         case LDB_REQ_RENAME:
826                 return ildb_rename(module,
827                                         req->op.rename.olddn,
828                                         req->op.rename.newdn);
829
830         case LDB_ASYNC_SEARCH:
831                 return ildb_search_async(module,
832                                         req->op.search.base,
833                                         req->op.search.scope, 
834                                         req->op.search.tree, 
835                                         req->op.search.attrs,
836                                         req->controls,
837                                         req->async.context,
838                                         req->async.callback,
839                                         req->async.timeout,
840                                         &req->async.handle);
841
842         case LDB_ASYNC_ADD:
843                 return ildb_add_async(module,
844                                         req->op.add.message,
845                                         req->async.context,
846                                         req->async.callback,
847                                         req->async.timeout,
848                                         &req->async.handle);
849
850         case LDB_ASYNC_MODIFY:
851                 return ildb_modify_async(module,
852                                         req->op.mod.message,
853                                         req->async.context,
854                                         req->async.callback,
855                                         req->async.timeout,
856                                         &req->async.handle);
857
858         case LDB_ASYNC_DELETE:
859                 return ildb_delete_async(module,
860                                         req->op.del.dn,
861                                         req->async.context,
862                                         req->async.callback,
863                                         req->async.timeout,
864                                         &req->async.handle);
865
866         case LDB_ASYNC_RENAME:
867                 return ildb_rename_async(module,
868                                         req->op.rename.olddn,
869                                         req->op.rename.newdn,
870                                         req->async.context,
871                                         req->async.callback,
872                                         req->async.timeout,
873                                         &req->async.handle);
874
875         default:
876                 return -1;
877
878         }
879 }
880
881 /*
882   fetch the rootDSE for later use
883 */
884 static int ildb_init_2(struct ldb_module *module)
885 {
886         struct ildb_private *ildb = module->private_data;
887         struct ldb_result *res = NULL;
888         struct ldb_dn *empty_dn = ldb_dn_new(ildb);
889         int ret;
890         ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE, 
891                                  ldb_parse_tree(empty_dn, "dn=dc=rootDSE"), 
892                                  NULL, NULL, &res);
893         if (ret == LDB_SUCCESS && res->count == 1) {
894                 ildb->rootDSE = talloc_steal(ildb, res->msgs[0]);
895         }
896         if (ret == LDB_SUCCESS) talloc_free(res);
897         talloc_free(empty_dn);
898
899         return LDB_SUCCESS;
900 }
901
902 static const struct ldb_module_ops ildb_ops = {
903         .name              = "ldap",
904         .request           = ildb_request,
905         .start_transaction = ildb_start_trans,
906         .end_transaction   = ildb_end_trans,
907         .del_transaction   = ildb_del_trans,
908         .second_stage_init = ildb_init_2
909 };
910
911
912 static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
913 {
914         struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context);
915
916         if (!ac) {
917                 return LDB_ERR_OPERATIONS_ERROR;
918         }
919
920         switch(type) {
921         case LDB_WAIT_NONE:
922                 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
923                         return LDB_ERR_OTHER;
924                 }
925                 break;
926         case LDB_WAIT_ALL:
927                 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
928                         if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
929                                 return LDB_ERR_OTHER;
930                         }
931                 }
932                 break;
933         default:
934                 return LDB_ERR_OPERATIONS_ERROR;
935         }
936         
937         return handle->status;
938 }
939
940 /*
941   connect to the database
942 */
943 int ildb_connect(struct ldb_context *ldb, const char *url, 
944                  unsigned int flags, const char *options[])
945 {
946         struct ildb_private *ildb = NULL;
947         NTSTATUS status;
948         struct cli_credentials *creds;
949
950         ildb = talloc(ldb, struct ildb_private);
951         if (!ildb) {
952                 ldb_oom(ldb);
953                 goto failed;
954         }
955
956         ildb->rootDSE = NULL;
957         ildb->ldb     = ldb;
958
959         ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
960         if (!ildb->ldap) {
961                 ldb_oom(ldb);
962                 goto failed;
963         }
964
965         status = ldap_connect(ildb->ldap, url);
966         if (!NT_STATUS_IS_OK(status)) {
967                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
968                           url, ldap_errstr(ildb->ldap, status));
969                 goto failed;
970         }
971
972         ldb->modules = talloc(ldb, struct ldb_module);
973         if (!ldb->modules) {
974                 ldb_oom(ldb);
975                 goto failed;
976         }
977         ldb->modules->ldb = ldb;
978         ldb->modules->prev = ldb->modules->next = NULL;
979         ldb->modules->private_data = ildb;
980         ldb->modules->ops = &ildb_ops;
981
982         /* caller can optionally setup credentials using the opaque token 'credentials' */
983         creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
984         if (creds == NULL) {
985                 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
986                 if (session_info) {
987                         creds = session_info->credentials;
988                 }
989         }
990
991         if (creds != NULL && cli_credentials_authentication_requested(creds)) {
992                 const char *bind_dn = cli_credentials_get_bind_dn(creds);
993                 if (bind_dn) {
994                         const char *password = cli_credentials_get_password(creds);
995                         status = ldap_bind_simple(ildb->ldap, bind_dn, password);
996                         if (!NT_STATUS_IS_OK(status)) {
997                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
998                                           ldap_errstr(ildb->ldap, status));
999                                 goto failed;
1000                         }
1001                 } else {
1002                         status = ldap_bind_sasl(ildb->ldap, creds);
1003                         if (!NT_STATUS_IS_OK(status)) {
1004                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
1005                                           ldap_errstr(ildb->ldap, status));
1006                                 goto failed;
1007                         }
1008                 }
1009         }
1010
1011         ldb->async_wait = &ildb_async_wait;
1012
1013         return 0;
1014
1015 failed:
1016         if (ldb->modules) {
1017                 ldb->modules->private_data = NULL;
1018         }
1019         talloc_free(ildb);
1020         return -1;
1021 }
1022