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