summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/ldb_ildap/ldb_ildap.c
blob: 352b7696d698fa47492bfb6b93e2de4f54e1368f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
   ldb database library - ildap backend

   Copyright (C) Andrew Tridgell  2005
   Copyright (C) Simo Sorce       2008

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

/*
 *  Name: ldb_ildap
 *
 *  Component: ldb ildap backend
 *
 *  Description: This is a ldb backend for the internal ldap
 *  client library in Samba4. By using this backend we are
 *  independent of a system ldap library
 *
 *  Author: Andrew Tridgell
 *
 *  Modifications:
 *
 *  - description: make the module use asyncronous calls
 *    date: Feb 2006
 *    author: Simo Sorce
 */

#include "includes.h"
#include "ldb_module.h"
#include "dlinklist.h"

#include "libcli/ldap/ldap.h"
#include "libcli/ldap/ldap_client.h"
#include "auth/auth.h"
#include "auth/credentials/credentials.h"

struct ildb_private {
	struct ldap_connection *ldap;
	struct tevent_context *event_ctx;
};

struct ildb_context {
	struct ldb_module *module;
	struct ldb_request *req;

	struct ildb_private *ildb;
	struct ldap_request *ireq;

	bool done;

	struct ildb_destructor_ctx *dc;
};

static void ildb_request_done(struct ildb_context *ctx,
			      struct ldb_control **ctrls, int error)
{
	struct ldb_context *ldb;
	struct ldb_reply *ares;

	ldb = ldb_module_get_ctx(ctx->module);

	ctx->done = true;

	if (ctx->req == NULL) {
		/* if the req has been freed already just return */
		return;
	}

	ares = talloc_zero(ctx->req, struct ldb_reply);
	if (!ares) {
		ldb_oom(ldb);
		ctx->req->callback(ctx->req, NULL);
		return;
	}
	ares->type = LDB_REPLY_DONE;
	ares->controls = talloc_steal(ares, ctrls);
	ares->error = error;

	ctx->req->callback(ctx->req, ares);
}

static void ildb_auto_done_callback(struct tevent_context *ev,
				    struct tevent_timer *te,
				    struct timeval t,
				    void *private_data)
{
	struct ildb_context *ac;

	ac = talloc_get_type(private_data, struct ildb_context);
	ildb_request_done(ac, NULL, LDB_SUCCESS);
}

/*
  convert a ldb_message structure to a list of ldap_mod structures
  ready for ildap_add() or ildap_modify()
*/
static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods,
					  const struct ldb_message *msg,
					  int use_flags)
{
	struct ldap_mod **mods;
	unsigned int i;
	int n = 0;

	/* allocate maximum number of elements needed */
	mods = talloc_array(mem_ctx, struct ldap_mod *, msg->num_elements+1);
	if (!mods) {
		errno = ENOMEM;
		return NULL;
	}
	mods[0] = NULL;

	for (i = 0; i < msg->num_elements; i++) {
		const struct ldb_message_element *el = &msg->elements[i];

		mods[n] = talloc(mods, struct ldap_mod);
		if (!mods[n]) {
			goto failed;
		}
		mods[n + 1] = NULL;
		mods[n]->type = 0;
		mods[n]->attrib = *el;
		if (use_flags) {
			switch (el->flags & LDB_FLAG_MOD_MASK) {
			case LDB_FLAG_MOD_ADD:
				mods[n]->type = LDAP_MODIFY_ADD;
				break;
			case LDB_FLAG_MOD_DELETE:
				mods[n]->type = LDAP_MODIFY_DELETE;
				break;
			case LDB_FLAG_MOD_REPLACE:
				mods[n]->type = LDAP_MODIFY_REPLACE;
				break;
			}
		}
		n++;
	}

	*num_mods = n;
	return mods;

failed:
	talloc_free(mods);
	return NULL;
}


/*
  map an ildap NTSTATUS to a ldb error code
*/
static int ildb_map_error(struct ldb_module *module, NTSTATUS status)
{
	struct ildb_private *ildb;
	struct ldb_context *ldb;
	TALLOC_CTX *mem_ctx;

	ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private);
	ldb = ldb_module_get_ctx(module);

	if (NT_STATUS_IS_OK(status)) {
		return LDB_SUCCESS;
	}

	mem_ctx = talloc_new(ildb);
	if (!mem_ctx) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}
	ldb_set_errstring(ldb,
			  ldap_errstr(ildb->ldap, mem_ctx, status));
	talloc_free(mem_ctx);
	if (NT_STATUS_IS_LDAP(status)) {
		return NT_STATUS_LDAP_CODE(status);
	}
	return LDB_ERR_OPERATIONS_ERROR;
}

static void ildb_request_timeout(struct tevent_context *ev, struct tevent_timer *te,
				 struct timeval t, void *private_data)
{
	struct ildb_context *ac = talloc_get_type(private_data, struct ildb_context);

	if (ac->ireq->state == LDAP_REQUEST_PENDING) {
		DLIST_REMOVE(ac->ireq->conn->pending, ac->ireq);
	}

	ildb_request_done(ac, NULL, LDB_ERR_TIME_LIMIT_EXCEEDED);
}

static void ildb_callback(struct ldap_request *req)
{
	struct ldb_context *ldb;
	struct ildb_context *ac;
	NTSTATUS status;
	struct ldap_SearchResEntry *search;
	struct ldap_message *msg;
	struct ldb_control **controls;
	struct ldb_message *ldbmsg;
	char *referral;
	bool callback_failed;
	bool request_done;
	int ret;
	int i;

	ac = talloc_get_type(req->async.private_data, struct ildb_context);
	ldb = ldb_module_get_ctx(ac->module);
	callback_failed = false;
	request_done = false;
	controls = NULL;

	if (!NT_STATUS_IS_OK(req->status)) {
		ret = ildb_map_error(ac->module, req->status);
		ildb_request_done(ac, NULL, ret);
		return;
	}

	if (req->num_replies < 1) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		ildb_request_done(ac, NULL, ret);
		return;
	}

	switch (req->type) {

	case LDAP_TAG_ModifyRequest:
		if (req->replies[0]->type != LDAP_TAG_ModifyResponse) {
			ret = LDB_ERR_PROTOCOL_ERROR;
			break;
		}
		status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
		ret = ildb_map_error(ac->module, status);
		request_done = true;
		break;

	case LDAP_TAG_AddRequest:
		if (req->replies[0]->type != LDAP_TAG_AddResponse) {
			ret = LDB_ERR_PROTOCOL_ERROR;
			return;
		}
		status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
		ret = ildb_map_error(ac->module, status);
		request_done = true;
		break;

	case LDAP_TAG_DelRequest:
		if (req->replies[0]->type != LDAP_TAG_DelResponse) {
			ret = LDB_ERR_PROTOCOL_ERROR;
			return;
		}
		status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
		ret = ildb_map_error(ac->module, status);
		request_done = true;
		break;

	case LDAP_TAG_ModifyDNRequest:
		if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) {
			ret = LDB_ERR_PROTOCOL_ERROR;
			return;
		}
		status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult);
		ret = ildb_map_error(ac->module, status);
		request_done = true;
		break;

	case LDAP_TAG_SearchRequest:
		/* loop over all messages */
		for (i = 0; i < req->num_replies; i++) {

			msg = req->replies[i];
			switch (msg->type) {

			case LDAP_TAG_SearchResultDone:

				status = ldap_check_response(ac->ireq->conn, &msg->r.GeneralResult);
				if (!NT_STATUS_IS_OK(status)) {
					ret = ildb_map_error(ac->module, status);
					break;
				}

				controls = talloc_steal(ac, msg->controls);
				if (msg->r.SearchResultDone.resultcode) {
					if (msg->r.SearchResultDone.errormessage) {
						ldb_set_errstring(ldb, msg->r.SearchResultDone.errormessage);
					}
				}

				ret = msg->r.SearchResultDone.resultcode;
				request_done = true;
				break;

			case LDAP_TAG_SearchResultEntry:

				ldbmsg = ldb_msg_new(ac);
				if (!ldbmsg) {
					ret = LDB_ERR_OPERATIONS_ERROR;
					break;
				}

				search = &(msg->r.SearchResultEntry);

				ldbmsg->dn = ldb_dn_new(ldbmsg, ldb, search->dn);
				if ( ! ldb_dn_validate(ldbmsg->dn)) {
					ret = LDB_ERR_OPERATIONS_ERROR;
					break;
				}
				ldbmsg->num_elements = search->num_attributes;
				ldbmsg->elements = talloc_move(ldbmsg, &search->attributes);

				controls = talloc_steal(ac, msg->controls);
				
				ret = ldb_module_send_entry(ac->req, ldbmsg, controls);
				if (ret != LDB_SUCCESS) {
					callback_failed = true;
				}
				break;

			case LDAP_TAG_SearchResultReference:

				referral = talloc_strdup(ac, msg->r.SearchResultReference.referral);

				ret = ldb_module_send_referral(ac->req, referral);
				if (ret != LDB_SUCCESS) {
					callback_failed = true;
				}
				break;

			default:
				/* TAG not handled, fail ! */
				ret = LDB_ERR_PROTOCOL_ERROR;
				break;
			}

			if (ret != LDB_SUCCESS) {
				break;
			}
		}

		talloc_free(req->replies);
		req->replies = NULL;
		req->num_replies = 0;

		break;

	default:
		ret = LDB_ERR_PROTOCOL_ERROR;
		break;
	}

	if (ret != LDB_SUCCESS) {

		/* if the callback failed the caller will have freed the
		 * request. Just return and don't try to use it */
		if ( ! callback_failed) {
			request_done = true;
		}
	}

	if (request_done) {
		ildb_request_done(ac, controls, ret);
	}
	return;
}

static int ildb_request_send(struct ildb_context *ac, struct ldap_message *msg)
{
	struct ldb_context *ldb;
	struct ldap_request *req;

	if (!ac) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	ldb = ldb_module_get_ctx(ac->module);

	req = ldap_request_send(ac->ildb->ldap, msg);
	if (req == NULL) {
		ldb_set_errstring(ldb, "async send request failed");
		return LDB_ERR_OPERATIONS_ERROR;
	}
	ac->ireq = talloc_steal(ac, req);

	if (!ac->ireq->conn) {
		ldb_set_errstring(ldb, "connection to remote LDAP server dropped?");
		return LDB_ERR_OPERATIONS_ERROR;
	}

	talloc_free(req->time_event);
	req->time_event = NULL;
	if (ac->req->timeout) {
		req->time_event = tevent_add_timer(ac->ildb->event_ctx, ac,
						   timeval_current_ofs(ac->req->timeout, 0),
						   ildb_request_timeout, ac);
	}

	req->async.fn = ildb_callback;
	req->async.private_data = ac;

	return LDB_SUCCESS;
}

/*
  search for matching records using an asynchronous function
 */
static int ildb_search(struct ildb_context *ac)
{
	struct ldb_context *ldb;
	struct ldb_request *req = ac->req;
	struct ldap_message *msg;
	int n;

	ldb = ldb_module_get_ctx(ac->module);

	if (!req->callback || !req->context) {
		ldb_set_errstring(ldb, "Async interface called with NULL callback function or NULL context");
		return LDB_ERR_OPERATIONS_ERROR;
	}

	if (req->op.search.tree == NULL) {
		ldb_set_errstring(ldb, "Invalid expression parse tree");
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg = new_ldap_message(req);
	if (msg == NULL) {
		ldb_set_errstring(ldb, "Out of Memory");
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->type = LDAP_TAG_SearchRequest;

	if (req->op.search.base == NULL) {
		msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
	} else {
		msg->r.SearchRequest.basedn  = ldb_dn_get_extended_linearized(msg, req->op.search.base, 0);
	}
	if (msg->r.SearchRequest.basedn == NULL) {
		ldb_set_errstring(ldb, "Unable to determine baseDN");
		talloc_free(msg);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	if (req->op.search.scope == LDB_SCOPE_DEFAULT) {
		msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE;
	} else {
		msg->r.SearchRequest.scope = req->op.search.scope;
	}

	msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
	msg->r.SearchRequest.timelimit = 0;
	msg->r.SearchRequest.sizelimit = 0;
	msg->r.SearchRequest.attributesonly = 0;
	msg->r.SearchRequest.tree = discard_const(req->op.search.tree);

	for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ;
	msg->r.SearchRequest.num_attributes = n;
	msg->r.SearchRequest.attributes = req->op.search.attrs;
	msg->controls = req->controls;

	return ildb_request_send(ac, msg);
}

/*
  add a record
*/
static int ildb_add(struct ildb_context *ac)
{
	struct ldb_request *req = ac->req;
	struct ldap_message *msg;
	struct ldap_mod **mods;
	int i,n;

	msg = new_ldap_message(req);
	if (msg == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->type = LDAP_TAG_AddRequest;

	msg->r.AddRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.add.message->dn, 0);
	if (msg->r.AddRequest.dn == NULL) {
		talloc_free(msg);
		return LDB_ERR_INVALID_DN_SYNTAX;
	}

	mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0);
	if (mods == NULL) {
		talloc_free(msg);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->r.AddRequest.num_attributes = n;
	msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
	if (msg->r.AddRequest.attributes == NULL) {
		talloc_free(msg);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	for (i = 0; i < n; i++) {
		msg->r.AddRequest.attributes[i] = mods[i]->attrib;
	}

	return ildb_request_send(ac, msg);
}

/*
  modify a record
*/
static int ildb_modify(struct ildb_context *ac)
{
	struct ldb_request *req = ac->req;
	struct ldap_message *msg;
	struct ldap_mod **mods;
	int i,n;

	msg = new_ldap_message(req);
	if (msg == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->type = LDAP_TAG_ModifyRequest;

	msg->r.ModifyRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.mod.message->dn, 0);
	if (msg->r.ModifyRequest.dn == NULL) {
		talloc_free(msg);
		return LDB_ERR_INVALID_DN_SYNTAX;
	}

	mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1);
	if (mods == NULL) {
		talloc_free(msg);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->r.ModifyRequest.num_mods = n;
	msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
	if (msg->r.ModifyRequest.mods == NULL) {
		talloc_free(msg);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	for (i = 0; i < n; i++) {
		msg->r.ModifyRequest.mods[i] = *mods[i];
	}

	return ildb_request_send(ac, msg);
}

/*
  delete a record
*/
static int ildb_delete(struct ildb_context *ac)
{
	struct ldb_request *req = ac->req;
	struct ldap_message *msg;

	msg = new_ldap_message(req);
	if (msg == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->type = LDAP_TAG_DelRequest;

	msg->r.DelRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.del.dn, 0);
	if (msg->r.DelRequest.dn == NULL) {
		talloc_free(msg);
		return LDB_ERR_INVALID_DN_SYNTAX;
	}

	return ildb_request_send(ac, msg);
}

/*
  rename a record
*/
static int ildb_rename(struct ildb_context *ac)
{
	struct ldb_request *req = ac->req;
	struct ldap_message *msg;
	const char *rdn_name;
	const struct ldb_val *rdn_val;

	msg = new_ldap_message(req);
	if (msg == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->type = LDAP_TAG_ModifyDNRequest;
	msg->r.ModifyDNRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.rename.olddn, 0);
	if (msg->r.ModifyDNRequest.dn == NULL) {
		talloc_free(msg);
		return LDB_ERR_INVALID_DN_SYNTAX;
	}

	rdn_name = ldb_dn_get_rdn_name(req->op.rename.newdn);
	rdn_val = ldb_dn_get_rdn_val(req->op.rename.newdn);

	if ((rdn_name != NULL) && (rdn_val != NULL)) {
		msg->r.ModifyDNRequest.newrdn =
			talloc_asprintf(msg, "%s=%s", rdn_name,
				ldb_dn_escape_value(msg, *rdn_val));
	} else {
		msg->r.ModifyDNRequest.newrdn = talloc_strdup(msg, "");
	}
	if (msg->r.ModifyDNRequest.newrdn == NULL) {
		talloc_free(msg);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	msg->r.ModifyDNRequest.newsuperior =
		ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
	if (msg->r.ModifyDNRequest.newsuperior == NULL) {
		talloc_free(msg);
		return LDB_ERR_INVALID_DN_SYNTAX;
	}

	msg->r.ModifyDNRequest.deleteolddn = true;

	return ildb_request_send(ac, msg);
}

static int ildb_start_trans(struct ldb_module *module)
{
	/* TODO implement a local locking mechanism here */

	return LDB_SUCCESS;
}

static int ildb_end_trans(struct ldb_module *module)
{
	/* TODO implement a local transaction mechanism here */

	return LDB_SUCCESS;
}

static int ildb_del_trans(struct ldb_module *module)
{
	/* TODO implement a local locking mechanism here */

	return LDB_SUCCESS;
}

static bool ildb_dn_is_special(struct ldb_request *req)
{
	struct ldb_dn *dn = NULL;

	switch (req->operation) {
	case LDB_ADD:
		dn = req->op.add.message->dn;
		break;
	case LDB_MODIFY:
		dn = req->op.mod.message->dn;
		break;
	case LDB_DELETE:
		dn = req->op.del.dn;
		break;
	case LDB_RENAME:
		dn = req->op.rename.olddn;
		break;
	default:
		break;
	}

	if (dn && ldb_dn_is_special(dn)) {
		return true;
	}
	return false;
}

static int ildb_handle_request(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_context *ldb;
	struct ildb_private *ildb;
	struct ildb_context *ac;
	struct tevent_timer *te;
	int ret;

	ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private);
	ldb = ldb_module_get_ctx(module);

	if (req->starttime == 0 || req->timeout == 0) {
		ldb_set_errstring(ldb, "Invalid timeout settings");
		return LDB_ERR_TIME_LIMIT_EXCEEDED;
	}

	ac = talloc_zero(req, struct ildb_context);
	if (ac == NULL) {
		ldb_set_errstring(ldb, "Out of Memory");
		return LDB_ERR_OPERATIONS_ERROR;
	}

	ac->module = module;
	ac->req = req;
	ac->ildb = ildb;

	if (ildb_dn_is_special(req)) {

		te = tevent_add_timer(ac->ildb->event_ctx,
				      ac, timeval_zero(),
				      ildb_auto_done_callback, ac);
		if (NULL == te) {
			return LDB_ERR_OPERATIONS_ERROR;
		}

		return LDB_SUCCESS;
	}

	switch (ac->req->operation) {
	case LDB_SEARCH:
		ret = ildb_search(ac);
		break;
	case LDB_ADD:
		ret = ildb_add(ac);
		break;
	case LDB_MODIFY:
		ret = ildb_modify(ac);
		break;
	case LDB_DELETE:
		ret = ildb_delete(ac);
		break;
	case LDB_RENAME:
		ret = ildb_rename(ac);
		break;
	default:
		/* no other op supported */
		ret = LDB_ERR_OPERATIONS_ERROR;
		break;
	}

	return ret;
}

static const struct ldb_module_ops ildb_ops = {
	.name              = "ldap",
	.search            = ildb_handle_request,
	.add               = ildb_handle_request,
	.modify            = ildb_handle_request,
	.del               = ildb_handle_request,
	.rename            = ildb_handle_request,
/*	.request           = ildb_handle_request, */
	.start_transaction = ildb_start_trans,
	.end_transaction   = ildb_end_trans,
	.del_transaction   = ildb_del_trans,
};

/*
  connect to the database
*/
static int ildb_connect(struct ldb_context *ldb, const char *url,
			unsigned int flags, const char *options[],
			struct ldb_module **_module)
{
	struct ldb_module *module;
	struct ildb_private *ildb;
	NTSTATUS status;
	struct cli_credentials *creds;
	struct loadparm_context *lp_ctx;

	module = ldb_module_new(ldb, ldb, "ldb_ildap backend", &ildb_ops);
	if (!module) return -1;

	ildb = talloc(module, struct ildb_private);
	if (!ildb) {
		ldb_oom(ldb);
		goto failed;
	}
	ldb_module_set_private(module, ildb);

	ildb->event_ctx = ldb_get_event_context(ldb);

	lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
				 struct loadparm_context);

	ildb->ldap = ldap4_new_connection(ildb, lp_ctx,
					  ildb->event_ctx);
	if (!ildb->ldap) {
		ldb_oom(ldb);
		goto failed;
	}

	if (flags & LDB_FLG_RECONNECT) {
		ldap_set_reconn_params(ildb->ldap, 10);
	}

	status = ldap_connect(ildb->ldap, url);
	if (!NT_STATUS_IS_OK(status)) {
		ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s",
			  url, ldap_errstr(ildb->ldap, module, status));
		goto failed;
	}

	/* caller can optionally setup credentials using the opaque token 'credentials' */
	creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
	if (creds == NULL) {
		struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
		if (session_info) {
			creds = session_info->credentials;
		}
	}

	if (creds != NULL && cli_credentials_authentication_requested(creds)) {
		const char *bind_dn = cli_credentials_get_bind_dn(creds);
		if (bind_dn) {
			const char *password = cli_credentials_get_password(creds);
			status = ldap_bind_simple(ildb->ldap, bind_dn, password);
			if (!NT_STATUS_IS_OK(status)) {
				ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
					  ldap_errstr(ildb->ldap, module, status));
				goto failed;
			}
		} else {
			status = ldap_bind_sasl(ildb->ldap, creds, lp_ctx);
			if (!NT_STATUS_IS_OK(status)) {
				ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s",
					  ldap_errstr(ildb->ldap, module, status));
				goto failed;
			}
		}
	}

	*_module = module;
	return 0;

failed:
	talloc_free(module);
	return -1;
}

_PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = {
	.name = "ldap",
	.connect_fn = ildb_connect
};

_PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = {
	.name = "ldapi",
	.connect_fn = ildb_connect
};

_PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = {
	.name = "ldaps",
	.connect_fn = ildb_connect
};