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