r18438: I should have examined these uses of talloc_move() more
[kamenim/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, search->attributes);
282
283                                 handle->status = LDB_SUCCESS;
284                                 handle->state = LDB_ASYNC_PENDING;
285                                 ares->type = LDB_REPLY_ENTRY;
286                                 break;
287
288                         case LDAP_TAG_SearchResultReference:
289
290                                 ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral);
291                                 
292                                 handle->status = LDB_SUCCESS;
293                                 handle->state = LDB_ASYNC_PENDING;
294                                 ares->type = LDB_REPLY_REFERRAL;
295                                 break;
296
297                         default:
298                                 /* TAG not handled, fail ! */
299                                 handle->status = LDB_ERR_PROTOCOL_ERROR;
300                                 return;
301                         }
302
303                         ret = ac->callback(ac->module->ldb, ac->context, ares);
304                         if (ret) {
305                                 handle->status = ret;
306                         }
307                 }
308
309                 talloc_free(req->replies);
310                 req->replies = NULL;
311                 req->num_replies = 0;
312
313                 break;
314                 
315         default:
316                 handle->status = LDB_ERR_PROTOCOL_ERROR;
317                 return;
318         }
319 }
320
321 static struct ldb_handle *init_ildb_handle(struct ldb_module *module, 
322                                            void *context,
323                                            int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
324 {
325         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
326         struct ildb_context *ildb_ac;
327         struct ldb_handle *h;
328
329         h = talloc_zero(ildb->ldap, struct ldb_handle);
330         if (h == NULL) {
331                 ldb_set_errstring(module->ldb, "Out of Memory");
332                 return NULL;
333         }
334
335         h->module = module;
336
337         ildb_ac = talloc(h, struct ildb_context);
338         if (ildb_ac == NULL) {
339                 ldb_set_errstring(module->ldb, "Out of Memory");
340                 talloc_free(h);
341                 return NULL;
342         }
343
344         h->private_data = (void *)ildb_ac;
345
346         h->state = LDB_ASYNC_INIT;
347         h->status = LDB_SUCCESS;
348
349         ildb_ac->module = module;
350         ildb_ac->context = context;
351         ildb_ac->callback = callback;
352
353         return h;
354 }
355
356 static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg,
357                              void *context,
358                              int (*callback)(struct ldb_context *, void *, struct ldb_reply *),
359                              int timeout,
360                              struct ldb_handle **handle)
361 {
362         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
363         struct ldb_handle *h = init_ildb_handle(module, context, callback);
364         struct ildb_context *ildb_ac;
365         struct ldap_request *req;
366
367         if (!h) {
368                 return LDB_ERR_OPERATIONS_ERROR;                
369         }
370
371         ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
372
373         req = ldap_request_send(ildb->ldap, msg);
374         if (req == NULL) {
375                 ldb_set_errstring(module->ldb, "async send request failed");
376                 return LDB_ERR_OPERATIONS_ERROR;
377         }
378
379         if (!req->conn) {
380                 ldb_set_errstring(module->ldb, "connection to remote LDAP server dropped?");
381                 return LDB_ERR_OPERATIONS_ERROR;
382         }
383
384         talloc_free(req->time_event);
385         req->time_event = NULL;
386         if (timeout) {
387                 req->time_event = event_add_timed(req->conn->event.event_ctx, h, 
388                                                   timeval_current_ofs(timeout, 0),
389                                                   ildb_request_timeout, h);
390         }
391
392         req->async.fn = ildb_callback;
393         req->async.private_data = (void *)h;
394         ildb_ac->req = talloc_move(ildb_ac, req);
395
396         *handle = h;
397         return LDB_SUCCESS;
398 }
399
400 static int ildb_request_noop(struct ldb_module *module, struct ldb_request *req) 
401 {
402         struct ldb_handle *h = init_ildb_handle(module, req->context, req->callback);
403         struct ildb_context *ildb_ac;
404         int ret = LDB_SUCCESS;
405
406         if (!h) {
407                 return LDB_ERR_OPERATIONS_ERROR;                
408         }
409
410         ildb_ac = talloc_get_type(h->private_data, struct ildb_context);
411         
412         req->handle = h;
413
414         if (ildb_ac->callback) {
415                 ret = ildb_ac->callback(module->ldb, ildb_ac->context, NULL);
416         }
417         req->handle->state = LDB_ASYNC_DONE;
418         return ret;
419 }
420
421 /*
422   search for matching records using an asynchronous function
423  */
424 static int ildb_search(struct ldb_module *module, struct ldb_request *req)
425 {
426         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
427         struct ldap_message *msg;
428         int n;
429
430         req->handle = NULL;
431
432         if (!req->callback || !req->context) {
433                 ldb_set_errstring(module->ldb, "Async interface called with NULL callback function or NULL context");
434                 return LDB_ERR_OPERATIONS_ERROR;
435         }
436         
437         if (req->op.search.tree == NULL) {
438                 ldb_set_errstring(module->ldb, "Invalid expression parse tree");
439                 return LDB_ERR_OPERATIONS_ERROR;
440         }
441
442         msg = new_ldap_message(ildb);
443         if (msg == NULL) {
444                 ldb_set_errstring(module->ldb, "Out of Memory");
445                 return LDB_ERR_OPERATIONS_ERROR;
446         }
447
448         msg->type = LDAP_TAG_SearchRequest;
449
450         if (req->op.search.base == NULL) {
451                 msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
452         } else {
453                 msg->r.SearchRequest.basedn  = ldb_dn_linearize(msg, req->op.search.base);
454         }
455         if (msg->r.SearchRequest.basedn == NULL) {
456                 ldb_set_errstring(module->ldb, "Unable to determine baseDN");
457                 talloc_free(msg);
458                 return LDB_ERR_OPERATIONS_ERROR;
459         }
460
461         if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
462                 msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
463         } else {
464                 msg->r.SearchRequest.scope = req->op.search.scope;
465         }
466         
467         msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
468         msg->r.SearchRequest.timelimit = 0;
469         msg->r.SearchRequest.sizelimit = 0;
470         msg->r.SearchRequest.attributesonly = 0;
471         msg->r.SearchRequest.tree = discard_const(req->op.search.tree);
472         
473         for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
474         msg->r.SearchRequest.num_attributes = n;
475         msg->r.SearchRequest.attributes = discard_const(req->op.search.attrs);
476         msg->controls = req->controls;
477
478         return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
479 }
480
481 /*
482   add a record
483 */
484 static int ildb_add(struct ldb_module *module, struct ldb_request *req)
485 {
486         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
487         struct ldap_message *msg;
488         struct ldap_mod **mods;
489         int i,n;
490
491         req->handle = NULL;
492
493         /* ignore ltdb specials */
494         if (ldb_dn_is_special(req->op.add.message->dn)) {
495                 return ildb_request_noop(module, req);
496         }
497
498         msg = new_ldap_message(ildb->ldap);
499         if (msg == NULL) {
500                 return LDB_ERR_OPERATIONS_ERROR;
501         }
502
503         msg->type = LDAP_TAG_AddRequest;
504
505         msg->r.AddRequest.dn = ldb_dn_linearize(msg, req->op.add.message->dn);
506         if (msg->r.AddRequest.dn == NULL) {
507                 talloc_free(msg);
508                 return LDB_ERR_INVALID_DN_SYNTAX;
509         }
510
511         mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
512         if (mods == NULL) {
513                 talloc_free(msg);
514                 return LDB_ERR_OPERATIONS_ERROR;
515         }
516
517         msg->r.AddRequest.num_attributes = n;
518         msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
519         if (msg->r.AddRequest.attributes == NULL) {
520                 talloc_free(msg);
521                 return LDB_ERR_OPERATIONS_ERROR;
522         }
523
524         for (i = 0; i < n; i++) {
525                 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
526         }
527
528         return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
529 }
530
531 /*
532   modify a record
533 */
534 static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
535 {
536         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
537         struct ldap_message *msg;
538         struct ldap_mod **mods;
539         int i,n;
540
541         req->handle = NULL;
542
543         /* ignore ltdb specials */
544         if (ldb_dn_is_special(req->op.mod.message->dn)) {
545                 return ildb_request_noop(module, req);
546         }
547
548         msg = new_ldap_message(ildb->ldap);
549         if (msg == NULL) {
550                 return LDB_ERR_OPERATIONS_ERROR;
551         }
552
553         msg->type = LDAP_TAG_ModifyRequest;
554
555         msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, req->op.mod.message->dn);
556         if (msg->r.ModifyRequest.dn == NULL) {
557                 talloc_free(msg);
558                 return LDB_ERR_INVALID_DN_SYNTAX;
559         }
560
561         mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
562         if (mods == NULL) {
563                 talloc_free(msg);
564                 return LDB_ERR_OPERATIONS_ERROR;
565         }
566
567         msg->r.ModifyRequest.num_mods = n;
568         msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
569         if (msg->r.ModifyRequest.mods == NULL) {
570                 talloc_free(msg);
571                 return LDB_ERR_OPERATIONS_ERROR;
572         }
573
574         for (i = 0; i < n; i++) {
575                 msg->r.ModifyRequest.mods[i] = *mods[i];
576         }
577
578         return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
579 }
580
581 /*
582   delete a record
583 */
584 static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
585 {
586         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
587         struct ldap_message *msg;
588
589         req->handle = NULL;
590
591         /* ignore ltdb specials */
592         if (ldb_dn_is_special(req->op.del.dn)) {
593                 return ildb_request_noop(module, req);
594         }
595
596         msg = new_ldap_message(ildb->ldap);
597         if (msg == NULL) {
598                 return LDB_ERR_OPERATIONS_ERROR;
599         }
600
601         msg->type = LDAP_TAG_DelRequest;
602         
603         msg->r.DelRequest.dn = ldb_dn_linearize(msg, req->op.del.dn);
604         if (msg->r.DelRequest.dn == NULL) {
605                 talloc_free(msg);
606                 return LDB_ERR_INVALID_DN_SYNTAX;
607         }
608
609         return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
610 }
611
612 /*
613   rename a record
614 */
615 static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
616 {
617         struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
618         struct ldap_message *msg;
619
620         req->handle = NULL;
621
622         /* ignore ltdb specials */
623         if (ldb_dn_is_special(req->op.rename.olddn) || ldb_dn_is_special(req->op.rename.newdn)) {
624                 return ildb_request_noop(module, req);
625         }
626
627         msg = new_ldap_message(ildb->ldap);
628         if (msg == NULL) {
629                 return LDB_ERR_OPERATIONS_ERROR;
630         }
631
632         msg->type = LDAP_TAG_ModifyDNRequest;
633         msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, req->op.rename.olddn);
634         if (msg->r.ModifyDNRequest.dn == NULL) {
635                 talloc_free(msg);
636                 return LDB_ERR_INVALID_DN_SYNTAX;
637         }
638
639         msg->r.ModifyDNRequest.newrdn = 
640                 talloc_asprintf(msg, "%s=%s",
641                                 req->op.rename.newdn->components[0].name,
642                                 ldb_dn_escape_value(msg, req->op.rename.newdn->components[0].value));
643         if (msg->r.ModifyDNRequest.newrdn == NULL) {
644                 talloc_free(msg);
645                 return LDB_ERR_OPERATIONS_ERROR;
646         }
647
648         msg->r.ModifyDNRequest.newsuperior =
649                 ldb_dn_linearize(msg,
650                                  ldb_dn_get_parent(msg, req->op.rename.newdn));
651         if (msg->r.ModifyDNRequest.newsuperior == NULL) {
652                 talloc_free(msg);
653                 return LDB_ERR_INVALID_DN_SYNTAX;
654         }
655
656         msg->r.ModifyDNRequest.deleteolddn = True;
657
658         return ildb_request_send(module, msg, req->context, req->callback, req->timeout, &(req->handle));
659 }
660
661 static int ildb_start_trans(struct ldb_module *module)
662 {
663         /* TODO implement a local locking mechanism here */
664
665         return LDB_SUCCESS;
666 }
667
668 static int ildb_end_trans(struct ldb_module *module)
669 {
670         /* TODO implement a local transaction mechanism here */
671
672         return LDB_SUCCESS;
673 }
674
675 static int ildb_del_trans(struct ldb_module *module)
676 {
677         /* TODO implement a local locking mechanism here */
678
679         return LDB_SUCCESS;
680 }
681
682 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
683 {
684         return LDB_ERR_OPERATIONS_ERROR;
685 }
686
687 static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
688 {
689         struct ildb_context *ac = talloc_get_type(handle->private_data, struct ildb_context);
690
691         if (handle->state == LDB_ASYNC_DONE) {
692                 return handle->status;
693         }
694
695         if (!ac) {
696                 return LDB_ERR_OPERATIONS_ERROR;
697         }
698
699         handle->state = LDB_ASYNC_INIT;
700
701         switch(type) {
702         case LDB_WAIT_NONE:
703                 if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
704                         return LDB_ERR_OTHER;
705                 }
706                 break;
707         case LDB_WAIT_ALL:
708                 while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
709                         if (event_loop_once(ac->req->conn->event.event_ctx) != 0) {
710                                 return LDB_ERR_OTHER;
711                         }
712                 }
713                 break;
714         default:
715                 return LDB_ERR_OPERATIONS_ERROR;
716         }
717         
718         return handle->status;
719 }
720
721 static const struct ldb_module_ops ildb_ops = {
722         .name              = "ldap",
723         .search            = ildb_search,
724         .add               = ildb_add,
725         .modify            = ildb_modify,
726         .del               = ildb_delete,
727         .rename            = ildb_rename,
728         .request           = ildb_request,
729         .start_transaction = ildb_start_trans,
730         .end_transaction   = ildb_end_trans,
731         .del_transaction   = ildb_del_trans,
732         .wait              = ildb_wait
733 };
734
735 /*
736   connect to the database
737 */
738 static int ildb_connect(struct ldb_context *ldb, const char *url, 
739                         unsigned int flags, const char *options[],
740                         struct ldb_module **module)
741 {
742         struct ildb_private *ildb = NULL;
743         NTSTATUS status;
744         struct cli_credentials *creds;
745
746         ildb = talloc(ldb, struct ildb_private);
747         if (!ildb) {
748                 ldb_oom(ldb);
749                 goto failed;
750         }
751
752         ildb->ldb     = ldb;
753
754         ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
755         if (!ildb->ldap) {
756                 ldb_oom(ldb);
757                 goto failed;
758         }
759
760         if (flags & LDB_FLG_RECONNECT) {
761                 ldap_set_reconn_params(ildb->ldap, 10);
762         }
763
764         status = ldap_connect(ildb->ldap, url);
765         if (!NT_STATUS_IS_OK(status)) {
766                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
767                           url, ldap_errstr(ildb->ldap, status));
768                 goto failed;
769         }
770
771
772         *module = talloc(ldb, struct ldb_module);
773         if (!module) {
774                 ldb_oom(ldb);
775                 talloc_free(ildb);
776                 return -1;
777         }
778         (*module)->ldb = ldb;
779         (*module)->prev = (*module)->next = NULL;
780         (*module)->private_data = ildb;
781         (*module)->ops = &ildb_ops;
782
783         /* caller can optionally setup credentials using the opaque token 'credentials' */
784         creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
785         if (creds == NULL) {
786                 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
787                 if (session_info) {
788                         creds = session_info->credentials;
789                 }
790         }
791
792         if (creds != NULL && cli_credentials_authentication_requested(creds)) {
793                 const char *bind_dn = cli_credentials_get_bind_dn(creds);
794                 if (bind_dn) {
795                         const char *password = cli_credentials_get_password(creds);
796                         status = ldap_bind_simple(ildb->ldap, bind_dn, password);
797                         if (!NT_STATUS_IS_OK(status)) {
798                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
799                                           ldap_errstr(ildb->ldap, status));
800                                 goto failed;
801                         }
802                 } else {
803                         status = ldap_bind_sasl(ildb->ldap, creds);
804                         if (!NT_STATUS_IS_OK(status)) {
805                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
806                                           ldap_errstr(ildb->ldap, status));
807                                 goto failed;
808                         }
809                 }
810         }
811
812         return 0;
813
814 failed:
815         talloc_free(ildb);
816         return -1;
817 }
818
819 int ldb_ildap_init(void)
820 {
821         return ldb_register_backend("ldap", ildb_connect) + 
822                    ldb_register_backend("ldapi", ildb_connect) + 
823                    ldb_register_backend("ldaps", ildb_connect);
824 }