ldb:ldb.c/"ldb_request" - make "ldb_request" always return an error string
[obnox/samba/samba-obnox.git] / lib / ldb / common / ldb.c
1 /*
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5    Copyright (C) Simo Sorce  2005-2008
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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb core API
29  *
30  *  Description: core API routines interfacing to ldb backends
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #define TEVENT_DEPRECATED 1
36 #include "ldb_private.h"
37 #include "ldb.h"
38
39 static int ldb_context_destructor(void *ptr)
40 {
41         struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
42
43         if (ldb->transaction_active) {
44                 ldb_debug(ldb, LDB_DEBUG_FATAL,
45                           "A transaction is still active in ldb context [%p] on %s",
46                           ldb, (const char *)ldb_get_opaque(ldb, "ldb_url"));
47         }
48
49         return 0;
50 }
51
52 /*
53   this is used to catch debug messages from events
54 */
55 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
56                              const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
57
58 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
59                              const char *fmt, va_list ap)
60 {
61         struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
62         enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
63         char *s = NULL;
64
65         switch (level) {
66         case TEVENT_DEBUG_FATAL:
67                 ldb_level = LDB_DEBUG_FATAL;
68                 break;
69         case TEVENT_DEBUG_ERROR:
70                 ldb_level = LDB_DEBUG_ERROR;
71                 break;
72         case TEVENT_DEBUG_WARNING:
73                 ldb_level = LDB_DEBUG_WARNING;
74                 break;
75         case TEVENT_DEBUG_TRACE:
76                 ldb_level = LDB_DEBUG_TRACE;
77                 break;
78         };
79
80         vasprintf(&s, fmt, ap);
81         if (!s) return;
82         ldb_debug(ldb, ldb_level, "tevent: %s", s);
83         free(s);
84 }
85
86 /*
87    initialise a ldb context
88    The mem_ctx is required
89    The event_ctx is required
90 */
91 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
92 {
93         struct ldb_context *ldb;
94         int ret;
95         const char *modules_path = getenv("LDB_MODULES_PATH");
96
97         if (modules_path == NULL) {
98                 modules_path = LDB_MODULESDIR;
99         }
100
101         ret = ldb_modules_load(modules_path, LDB_VERSION);
102         if (ret != LDB_SUCCESS) {
103                 return NULL;
104         }
105
106         ldb = talloc_zero(mem_ctx, struct ldb_context);
107         if (ldb == NULL) {
108                 return NULL;
109         }
110
111         /* A new event context so that callers who don't want ldb
112          * operating on thier global event context can work without
113          * having to provide their own private one explicitly */
114         if (ev_ctx == NULL) {
115                 ev_ctx = tevent_context_init(ldb);
116                 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
117                 tevent_loop_allow_nesting(ev_ctx);
118         }
119
120         ret = ldb_setup_wellknown_attributes(ldb);
121         if (ret != LDB_SUCCESS) {
122                 talloc_free(ldb);
123                 return NULL;
124         }
125
126         ldb_set_utf8_default(ldb);
127         ldb_set_create_perms(ldb, 0666);
128         ldb_set_modules_dir(ldb, LDB_MODULESDIR);
129         ldb_set_event_context(ldb, ev_ctx);
130
131         /* TODO: get timeout from options if available there */
132         ldb->default_timeout = 300; /* set default to 5 minutes */
133
134         talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
135
136         return ldb;
137 }
138
139 /*
140   try to autodetect a basedn if none specified. This fixes one of my
141   pet hates about ldapsearch, which is that you have to get a long,
142   complex basedn right to make any use of it.
143 */
144 void ldb_set_default_dns(struct ldb_context *ldb)
145 {
146         TALLOC_CTX *tmp_ctx;
147         int ret;
148         struct ldb_result *res;
149         struct ldb_dn *tmp_dn=NULL;
150         static const char *attrs[] = {
151                 "rootDomainNamingContext",
152                 "configurationNamingContext",
153                 "schemaNamingContext",
154                 "defaultNamingContext",
155                 NULL
156         };
157
158         tmp_ctx = talloc_new(ldb);
159         ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
160                          LDB_SCOPE_BASE, attrs, "(objectClass=*)");
161         if (ret != LDB_SUCCESS) {
162                 talloc_free(tmp_ctx);
163                 return;
164         }
165
166         if (res->count != 1) {
167                 talloc_free(tmp_ctx);
168                 return;
169         }
170
171         if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
172                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
173                                                  "rootDomainNamingContext");
174                 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
175         }
176
177         if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
178                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
179                                                  "configurationNamingContext");
180                 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
181         }
182
183         if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
184                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
185                                                  "schemaNamingContext");
186                 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
187         }
188
189         if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
190                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
191                                                  "defaultNamingContext");
192                 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
193         }
194
195         talloc_free(tmp_ctx);
196 }
197
198 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
199 {
200         void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
201         return talloc_get_type(opaque, struct ldb_dn);
202 }
203
204 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
205 {
206         void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
207         return talloc_get_type(opaque, struct ldb_dn);
208 }
209
210 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
211 {
212         void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
213         return talloc_get_type(opaque, struct ldb_dn);
214 }
215
216 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
217 {
218         void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
219         return talloc_get_type(opaque, struct ldb_dn);
220 }
221
222 /*
223    connect to a database. The URL can either be one of the following forms
224    ldb://path
225    ldapi://path
226
227    flags is made up of LDB_FLG_*
228
229    the options are passed uninterpreted to the backend, and are
230    backend specific
231 */
232 int ldb_connect(struct ldb_context *ldb, const char *url,
233                 unsigned int flags, const char *options[])
234 {
235         int ret;
236         char *url2;
237         /* We seem to need to do this here, or else some utilities don't
238          * get ldb backends */
239
240         ldb->flags = flags;
241
242         url2 = talloc_strdup(ldb, url);
243         if (!url2) {
244                 ldb_oom(ldb);
245                 return LDB_ERR_OPERATIONS_ERROR;
246         }
247         ret = ldb_set_opaque(ldb, "ldb_url", url2);
248         if (ret != LDB_SUCCESS) {
249                 return ret;
250         }
251
252         ret = ldb_module_connect_backend(ldb, url, options, &ldb->modules);
253         if (ret != LDB_SUCCESS) {
254                 return ret;
255         }
256
257         if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
258                 ldb_debug(ldb, LDB_DEBUG_FATAL,
259                           "Unable to load modules for %s: %s",
260                           url, ldb_errstring(ldb));
261                 return LDB_ERR_OTHER;
262         }
263
264         /* set the default base dn */
265         ldb_set_default_dns(ldb);
266
267         return LDB_SUCCESS;
268 }
269
270 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
271 {
272         ldb_asprintf_errstring(ldb, "%s", err_string);
273 }
274
275 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
276 {
277         va_list ap;
278
279         if (ldb->err_string) {
280                 talloc_free(ldb->err_string);
281         }
282
283         va_start(ap, format);
284         ldb->err_string = talloc_vasprintf(ldb, format, ap);
285         va_end(ap);
286
287         if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
288                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_asprintf/set_errstring: %s",
289                           ldb->err_string);
290         }
291 }
292
293 void ldb_reset_err_string(struct ldb_context *ldb)
294 {
295         if (ldb->err_string) {
296                 talloc_free(ldb->err_string);
297                 ldb->err_string = NULL;
298         }
299 }
300
301
302
303 /*
304   set an ldb error based on file:line
305 */
306 int ldb_error_at(struct ldb_context *ldb, int ecode,
307                  const char *reason, const char *file, int line)
308 {
309         if (reason == NULL) {
310                 reason = ldb_strerror(ecode);
311         }
312         ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
313         return ecode;
314 }
315
316
317 #define FIRST_OP_NOERR(ldb, op) do { \
318         module = ldb->modules;                                  \
319         while (module && module->ops->op == NULL) module = module->next; \
320         if ((ldb->flags & LDB_FLG_ENABLE_TRACING) && module) { \
321                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: (%s)->" #op, \
322                           module->ops->name);                           \
323         }                                                               \
324 } while (0)
325
326 #define FIRST_OP(ldb, op) do { \
327         FIRST_OP_NOERR(ldb, op); \
328         if (module == NULL) {                                   \
329                 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
330                 return LDB_ERR_OPERATIONS_ERROR;                        \
331         } \
332 } while (0)
333
334
335 /*
336   start a transaction
337 */
338 int ldb_transaction_start(struct ldb_context *ldb)
339 {
340         struct ldb_module *module;
341         int status;
342
343         ldb_debug(ldb, LDB_DEBUG_TRACE,
344                   "start ldb transaction (nesting: %d)",
345                   ldb->transaction_active);
346
347         /* explicit transaction active, count nested requests */
348         if (ldb->transaction_active) {
349                 ldb->transaction_active++;
350                 return LDB_SUCCESS;
351         }
352
353         /* start a new transaction */
354         ldb->transaction_active++;
355         ldb->prepare_commit_done = false;
356
357         FIRST_OP(ldb, start_transaction);
358
359         ldb_reset_err_string(ldb);
360
361         status = module->ops->start_transaction(module);
362         if (status != LDB_SUCCESS) {
363                 if (ldb->err_string == NULL) {
364                         /* no error string was setup by the backend */
365                         ldb_asprintf_errstring(ldb,
366                                 "ldb transaction start: %s (%d)",
367                                 ldb_strerror(status),
368                                 status);
369                 }
370         }
371         if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
372                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "start ldb transaction error: %s", 
373                           ldb_errstring(module->ldb));                          
374         }
375         return status;
376 }
377
378 /*
379   prepare for transaction commit (first phase of two phase commit)
380 */
381 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
382 {
383         struct ldb_module *module;
384         int status;
385
386         if (ldb->prepare_commit_done) {
387                 return LDB_SUCCESS;
388         }
389
390         /* commit only when all nested transactions are complete */
391         if (ldb->transaction_active > 1) {
392                 return LDB_SUCCESS;
393         }
394
395         ldb->prepare_commit_done = true;
396
397         if (ldb->transaction_active < 0) {
398                 ldb_debug(ldb, LDB_DEBUG_FATAL,
399                           "prepare commit called but no ldb transactions are active!");
400                 ldb->transaction_active = 0;
401                 return LDB_ERR_OPERATIONS_ERROR;
402         }
403
404         /* call prepare transaction if available */
405         FIRST_OP_NOERR(ldb, prepare_commit);
406         if (module == NULL) {
407                 return LDB_SUCCESS;
408         }
409
410         status = module->ops->prepare_commit(module);
411         if (status != LDB_SUCCESS) {
412                 /* if a module fails the prepare then we need
413                    to call the end transaction for everyone */
414                 FIRST_OP(ldb, del_transaction);
415                 module->ops->del_transaction(module);
416                 if (ldb->err_string == NULL) {
417                         /* no error string was setup by the backend */
418                         ldb_asprintf_errstring(ldb,
419                                                "ldb transaction prepare commit: %s (%d)",
420                                                ldb_strerror(status),
421                                                status);
422                 }
423                 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
424                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "prepare commit transaction error: %s", 
425                                   ldb_errstring(module->ldb));                          
426                 }
427         }
428
429         return status;
430 }
431
432
433 /*
434   commit a transaction
435 */
436 int ldb_transaction_commit(struct ldb_context *ldb)
437 {
438         struct ldb_module *module;
439         int status;
440
441         status = ldb_transaction_prepare_commit(ldb);
442         if (status != LDB_SUCCESS) {
443                 return status;
444         }
445
446         ldb->transaction_active--;
447
448         ldb_debug(ldb, LDB_DEBUG_TRACE,
449                   "commit ldb transaction (nesting: %d)",
450                   ldb->transaction_active);
451
452         /* commit only when all nested transactions are complete */
453         if (ldb->transaction_active > 0) {
454                 return LDB_SUCCESS;
455         }
456
457         if (ldb->transaction_active < 0) {
458                 ldb_debug(ldb, LDB_DEBUG_FATAL,
459                           "commit called but no ldb transactions are active!");
460                 ldb->transaction_active = 0;
461                 return LDB_ERR_OPERATIONS_ERROR;
462         }
463
464         ldb_reset_err_string(ldb);
465
466         FIRST_OP(ldb, end_transaction);
467         status = module->ops->end_transaction(module);
468         if (status != LDB_SUCCESS) {
469                 if (ldb->err_string == NULL) {
470                         /* no error string was setup by the backend */
471                         ldb_asprintf_errstring(ldb,
472                                 "ldb transaction commit: %s (%d)",
473                                 ldb_strerror(status),
474                                 status);
475                 }
476                 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
477                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "commit ldb transaction error: %s", 
478                                   ldb_errstring(module->ldb));                          
479                 }
480                 /* cancel the transaction */
481                 FIRST_OP(ldb, del_transaction);
482                 module->ops->del_transaction(module);
483         }
484         return status;
485 }
486
487
488 /*
489   cancel a transaction
490 */
491 int ldb_transaction_cancel(struct ldb_context *ldb)
492 {
493         struct ldb_module *module;
494         int status;
495
496         ldb->transaction_active--;
497
498         ldb_debug(ldb, LDB_DEBUG_TRACE,
499                   "cancel ldb transaction (nesting: %d)",
500                   ldb->transaction_active);
501
502         /* really cancel only if all nested transactions are complete */
503         if (ldb->transaction_active > 0) {
504                 return LDB_SUCCESS;
505         }
506
507         if (ldb->transaction_active < 0) {
508                 ldb_debug(ldb, LDB_DEBUG_FATAL,
509                           "cancel called but no ldb transactions are active!");
510                 ldb->transaction_active = 0;
511                 return LDB_ERR_OPERATIONS_ERROR;
512         }
513
514         FIRST_OP(ldb, del_transaction);
515
516         status = module->ops->del_transaction(module);
517         if (status != LDB_SUCCESS) {
518                 if (ldb->err_string == NULL) {
519                         /* no error string was setup by the backend */
520                         ldb_asprintf_errstring(ldb,
521                                 "ldb transaction cancel: %s (%d)",
522                                 ldb_strerror(status),
523                                 status);
524                 }
525                 if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
526                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "cancel ldb transaction error: %s", 
527                                   ldb_errstring(module->ldb));                          
528                 }
529         }
530         return status;
531 }
532
533 /*
534   cancel a transaction with no error if no transaction is pending
535   used when we fork() to clear any parent transactions
536 */
537 int ldb_transaction_cancel_noerr(struct ldb_context *ldb)
538 {
539         if (ldb->transaction_active > 0) {
540                 return ldb_transaction_cancel(ldb);
541         }
542         return LDB_SUCCESS;
543 }
544
545
546 /* autostarts a transaction if none active */
547 static int ldb_autotransaction_request(struct ldb_context *ldb,
548                                        struct ldb_request *req)
549 {
550         int ret;
551
552         ret = ldb_transaction_start(ldb);
553         if (ret != LDB_SUCCESS) {
554                 return ret;
555         }
556
557         ret = ldb_request(ldb, req);
558         if (ret == LDB_SUCCESS) {
559                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
560         }
561
562         if (ret == LDB_SUCCESS) {
563                 return ldb_transaction_commit(ldb);
564         }
565         ldb_transaction_cancel(ldb);
566
567         if (ldb->err_string == NULL) {
568                 /* no error string was setup by the backend */
569                 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
570         }
571
572         return ret;
573 }
574
575 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
576 {
577         struct tevent_context *ev;
578         int ret;
579
580         if (!handle) {
581                 return LDB_ERR_UNAVAILABLE;
582         }
583
584         if (handle->state == LDB_ASYNC_DONE) {
585                 return handle->status;
586         }
587
588         ev = ldb_get_event_context(handle->ldb);
589         if (NULL == ev) {
590                 return LDB_ERR_OPERATIONS_ERROR;
591         }
592
593         switch (type) {
594         case LDB_WAIT_NONE:
595                 ret = tevent_loop_once(ev);
596                 if (ret != 0) {
597                         return LDB_ERR_OPERATIONS_ERROR;
598                 }
599                 if (handle->state == LDB_ASYNC_DONE ||
600                     handle->status != LDB_SUCCESS) {
601                         return handle->status;
602                 }
603                 break;
604
605         case LDB_WAIT_ALL:
606                 while (handle->state != LDB_ASYNC_DONE) {
607                         ret = tevent_loop_once(ev);
608                         if (ret != 0) {
609                                 return LDB_ERR_OPERATIONS_ERROR;
610                         }
611                         if (handle->status != LDB_SUCCESS) {
612                                 return handle->status;
613                         }
614                 }
615                 return handle->status;
616         }
617
618         return LDB_SUCCESS;
619 }
620
621 /* set the specified timeout or, if timeout is 0 set the default timeout */
622 int ldb_set_timeout(struct ldb_context *ldb,
623                     struct ldb_request *req,
624                     int timeout)
625 {
626         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
627
628         if (timeout != 0) {
629                 req->timeout = timeout;
630         } else {
631                 req->timeout = ldb->default_timeout;
632         }
633         req->starttime = time(NULL);
634
635         return LDB_SUCCESS;
636 }
637
638 /* calculates the new timeout based on the previous starttime and timeout */
639 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
640                                   struct ldb_request *oldreq,
641                                   struct ldb_request *newreq)
642 {
643         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
644
645         if (oldreq == NULL) {
646                 return ldb_set_timeout(ldb, newreq, 0);
647         }
648
649         newreq->starttime = oldreq->starttime;
650         newreq->timeout = oldreq->timeout;
651
652         return LDB_SUCCESS;
653 }
654
655
656 /*
657    set the permissions for new files to be passed to open() in
658    backends that use local files
659  */
660 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
661 {
662         ldb->create_perms = perms;
663 }
664
665 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
666 {
667         return ldb->create_perms;
668 }
669
670 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
671 {
672         ldb->ev_ctx = ev;
673 }
674
675 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
676 {
677         return ldb->ev_ctx;
678 }
679
680 void ldb_request_set_state(struct ldb_request *req, int state)
681 {
682         req->handle->state = state;
683 }
684
685 int ldb_request_get_status(struct ldb_request *req)
686 {
687         return req->handle->status;
688 }
689
690
691 /*
692   trace a ldb request
693 */
694 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
695 {
696         TALLOC_CTX *tmp_ctx = talloc_new(req);
697         unsigned int i;
698
699         switch (req->operation) {
700         case LDB_SEARCH:
701                 ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
702                 ldb_debug_add(ldb, " dn: %s\n",
703                               ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
704                               ldb_dn_get_linearized(req->op.search.base));
705                 ldb_debug_add(ldb, " scope: %s\n", 
706                           req->op.search.scope==LDB_SCOPE_BASE?"base":
707                           req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
708                           req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
709                 ldb_debug_add(ldb, " expr: %s\n", 
710                           ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
711                 if (req->op.search.attrs == NULL) {
712                         ldb_debug_add(ldb, " attr: <ALL>\n");
713                 } else {
714                         for (i=0; req->op.search.attrs[i]; i++) {
715                                 ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
716                         }
717                 }
718                 break;
719         case LDB_DELETE:
720                 ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
721                 ldb_debug_add(ldb, " dn: %s\n", 
722                               ldb_dn_get_linearized(req->op.del.dn));
723                 break;
724         case LDB_RENAME:
725                 ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
726                 ldb_debug_add(ldb, " olddn: %s\n", 
727                               ldb_dn_get_linearized(req->op.rename.olddn));
728                 ldb_debug_add(ldb, " newdn: %s\n", 
729                               ldb_dn_get_linearized(req->op.rename.newdn));
730                 break;
731         case LDB_EXTENDED:
732                 ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
733                 ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
734                 ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
735                 break;
736         case LDB_ADD:
737                 ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
738                 ldb_debug_add(req->handle->ldb, "%s\n", 
739                               ldb_ldif_message_string(req->handle->ldb, tmp_ctx, 
740                                                       LDB_CHANGETYPE_ADD, 
741                                                       req->op.add.message));
742                 break;
743         case LDB_MODIFY:
744                 ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
745                 ldb_debug_add(req->handle->ldb, "%s\n", 
746                               ldb_ldif_message_string(req->handle->ldb, tmp_ctx, 
747                                                       LDB_CHANGETYPE_ADD, 
748                                                       req->op.mod.message));
749                 break;
750         case LDB_REQ_REGISTER_CONTROL:
751                 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
752                 ldb_debug_add(req->handle->ldb, "%s\n", 
753                               req->op.reg_control.oid);
754                 break;
755         case LDB_REQ_REGISTER_PARTITION:
756                 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
757                 ldb_debug_add(req->handle->ldb, "%s\n", 
758                               ldb_dn_get_linearized(req->op.reg_partition.dn));
759                 break;
760         default:
761                 ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n", 
762                               req->operation);
763                 break;
764         }
765
766         if (req->controls == NULL) {
767                 ldb_debug_add(ldb, " control: <NONE>\n");
768         } else {
769                 for (i=0; req->controls && req->controls[i]; i++) {
770                         if (req->controls[i]->oid) {
771                                 ldb_debug_add(ldb, " control: %s  crit:%u  data:%s\n",
772                                               req->controls[i]->oid,
773                                               req->controls[i]->critical,
774                                               req->controls[i]->data?"yes":"no");
775                         }
776                 }
777         }
778         
779         ldb_debug_end(ldb, LDB_DEBUG_TRACE);
780
781         talloc_free(tmp_ctx);
782 }
783
784 /*
785   check that the element flags don't have any internal bits set
786  */
787 static int ldb_msg_check_element_flags(struct ldb_context *ldb,
788                                        const struct ldb_message *message)
789 {
790         unsigned i;
791         for (i=0; i<message->num_elements; i++) {
792                 if (message->elements[i].flags & LDB_FLAG_INTERNAL_MASK) {
793                         ldb_asprintf_errstring(ldb, "Invalid element flags 0x%08x on element %s in %s\n",
794                                                message->elements[i].flags, message->elements[i].name,
795                                                ldb_dn_get_linearized(message->dn));
796                         return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
797                 }
798         }
799         return LDB_SUCCESS;
800 }
801
802
803 /*
804   start an ldb request
805   NOTE: the request must be a talloc context.
806   returns LDB_ERR_* on errors.
807 */
808 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
809 {
810         struct ldb_module *module;
811         int ret;
812
813         if (req->callback == NULL) {
814                 ldb_set_errstring(ldb, "Requests MUST define callbacks");
815                 return LDB_ERR_UNWILLING_TO_PERFORM;
816         }
817
818         ldb_reset_err_string(ldb);
819
820         if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
821                 ldb_trace_request(ldb, req);
822         }
823
824         /* call the first module in the chain */
825         switch (req->operation) {
826         case LDB_SEARCH:
827                 /* due to "ldb_build_search_req" base DN always != NULL */
828                 if (!ldb_dn_validate(req->op.search.base)) {
829                         ldb_asprintf_errstring(ldb, "ldb_search: invalid basedn '%s'",
830                                                ldb_dn_get_linearized(req->op.search.base));
831                         return LDB_ERR_INVALID_DN_SYNTAX;
832                 }
833                 FIRST_OP(ldb, search);
834                 ret = module->ops->search(module, req);
835                 break;
836         case LDB_ADD:
837                 if (!ldb_dn_validate(req->op.add.message->dn)) {
838                         ldb_asprintf_errstring(ldb, "ldb_add: invalid dn '%s'",
839                                                ldb_dn_get_linearized(req->op.add.message->dn));
840                         return LDB_ERR_INVALID_DN_SYNTAX;
841                 }
842                 /*
843                  * we have to normalize here, as so many places
844                  * in modules and backends assume we don't have two
845                  * elements with the same name
846                  */
847                 ret = ldb_msg_normalize(ldb, req, req->op.add.message,
848                                         discard_const(&req->op.add.message));
849                 if (ret != LDB_SUCCESS) {
850                         ldb_oom(ldb);
851                         return ret;
852                 }
853                 FIRST_OP(ldb, add);
854                 ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
855                 if (ret != LDB_SUCCESS) {
856                         /*
857                          * "ldb_msg_check_element_flags" generates an error
858                          * string
859                          */
860                         return ret;
861                 }
862                 ret = module->ops->add(module, req);
863                 break;
864         case LDB_MODIFY:
865                 if (!ldb_dn_validate(req->op.mod.message->dn)) {
866                         ldb_asprintf_errstring(ldb, "ldb_modify: invalid dn '%s'",
867                                                ldb_dn_get_linearized(req->op.mod.message->dn));
868                         return LDB_ERR_INVALID_DN_SYNTAX;
869                 }
870                 FIRST_OP(ldb, modify);
871                 ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
872                 if (ret != LDB_SUCCESS) {
873                         /*
874                          * "ldb_msg_check_element_flags" generates an error
875                          * string
876                          */
877                         return ret;
878                 }
879                 ret = module->ops->modify(module, req);
880                 break;
881         case LDB_DELETE:
882                 if (!ldb_dn_validate(req->op.del.dn)) {
883                         ldb_asprintf_errstring(ldb, "ldb_delete: invalid dn '%s'",
884                                                ldb_dn_get_linearized(req->op.del.dn));
885                         return LDB_ERR_INVALID_DN_SYNTAX;
886                 }
887                 FIRST_OP(ldb, del);
888                 ret = module->ops->del(module, req);
889                 break;
890         case LDB_RENAME:
891                 if (!ldb_dn_validate(req->op.rename.olddn)) {
892                         ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
893                                                ldb_dn_get_linearized(req->op.rename.olddn));
894                         return LDB_ERR_INVALID_DN_SYNTAX;
895                 }
896                 if (!ldb_dn_validate(req->op.rename.newdn)) {
897                         ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
898                                                ldb_dn_get_linearized(req->op.rename.newdn));
899                         return LDB_ERR_INVALID_DN_SYNTAX;
900                 }
901                 FIRST_OP(ldb, rename);
902                 ret = module->ops->rename(module, req);
903                 break;
904         case LDB_EXTENDED:
905                 FIRST_OP(ldb, extended);
906                 ret = module->ops->extended(module, req);
907                 break;
908         default:
909                 FIRST_OP(ldb, request);
910                 ret = module->ops->request(module, req);
911                 break;
912         }
913
914         if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
915                 /* if no error string was setup by the backend */
916                 ldb_asprintf_errstring(ldb, "ldb_request: %s (%d)",
917                                        ldb_strerror(ret), ret);
918         }
919
920         return ret;
921 }
922
923 int ldb_request_done(struct ldb_request *req, int status)
924 {
925         req->handle->state = LDB_ASYNC_DONE;
926         req->handle->status = status;
927         return status;
928 }
929
930 /*
931   search the database given a LDAP-like search expression
932
933   returns an LDB error code
934
935   Use talloc_free to free the ldb_message returned in 'res', if successful
936
937 */
938 int ldb_search_default_callback(struct ldb_request *req,
939                                 struct ldb_reply *ares)
940 {
941         struct ldb_result *res;
942         unsigned int n;
943
944         res = talloc_get_type(req->context, struct ldb_result);
945
946         if (!ares) {
947                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
948         }
949         if (ares->error != LDB_SUCCESS) {
950                 return ldb_request_done(req, ares->error);
951         }
952
953         switch (ares->type) {
954         case LDB_REPLY_ENTRY:
955                 res->msgs = talloc_realloc(res, res->msgs,
956                                         struct ldb_message *, res->count + 2);
957                 if (! res->msgs) {
958                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
959                 }
960
961                 res->msgs[res->count + 1] = NULL;
962
963                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
964                 res->count++;
965                 break;
966
967         case LDB_REPLY_REFERRAL:
968                 if (res->refs) {
969                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
970                 } else {
971                         n = 0;
972                 }
973
974                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
975                 if (! res->refs) {
976                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
977                 }
978
979                 res->refs[n] = talloc_move(res->refs, &ares->referral);
980                 res->refs[n + 1] = NULL;
981                 break;
982
983         case LDB_REPLY_DONE:
984                 /* TODO: we should really support controls on entries
985                  * and referrals too! */
986                 res->controls = talloc_move(res, &ares->controls);
987
988                 /* this is the last message, and means the request is done */
989                 /* we have to signal and eventual ldb_wait() waiting that the
990                  * async request operation was completed */
991                 talloc_free(ares);
992                 return ldb_request_done(req, LDB_SUCCESS);
993         }
994
995         talloc_free(ares);
996
997         return LDB_SUCCESS;
998 }
999
1000 int ldb_modify_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1001 {
1002         struct ldb_result *res;
1003         unsigned int n;
1004         int ret;
1005
1006         res = talloc_get_type(req->context, struct ldb_result);
1007
1008         if (!ares) {
1009                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1010         }
1011
1012         if (ares->error != LDB_SUCCESS) {
1013                 ret = ares->error;
1014                 talloc_free(ares);
1015                 return ldb_request_done(req, ret);
1016         }
1017
1018         switch (ares->type) {
1019         case LDB_REPLY_REFERRAL:
1020                 if (res->refs) {
1021                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
1022                 } else {
1023                         n = 0;
1024                 }
1025
1026                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1027                 if (! res->refs) {
1028                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1029                 }
1030
1031                 res->refs[n] = talloc_move(res->refs, &ares->referral);
1032                 res->refs[n + 1] = NULL;
1033                 break;
1034
1035         case LDB_REPLY_DONE:
1036                 talloc_free(ares);
1037                 return ldb_request_done(req, LDB_SUCCESS);
1038         default:
1039                 talloc_free(ares);
1040                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1041                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1042         }
1043
1044         talloc_free(ares);
1045         return ldb_request_done(req, LDB_SUCCESS);
1046 }
1047
1048 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1049 {
1050         int ret;
1051
1052         if (!ares) {
1053                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1054         }
1055
1056         if (ares->error != LDB_SUCCESS) {
1057                 ret = ares->error;
1058                 talloc_free(ares);
1059                 return ldb_request_done(req, ret);
1060         }
1061
1062         if (ares->type != LDB_REPLY_DONE) {
1063                 talloc_free(ares);
1064                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1065                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1066         }
1067
1068         talloc_free(ares);
1069         return ldb_request_done(req, LDB_SUCCESS);
1070 }
1071
1072 int ldb_build_search_req_ex(struct ldb_request **ret_req,
1073                         struct ldb_context *ldb,
1074                         TALLOC_CTX *mem_ctx,
1075                         struct ldb_dn *base,
1076                         enum ldb_scope scope,
1077                         struct ldb_parse_tree *tree,
1078                         const char * const *attrs,
1079                         struct ldb_control **controls,
1080                         void *context,
1081                         ldb_request_callback_t callback,
1082                         struct ldb_request *parent)
1083 {
1084         struct ldb_request *req;
1085
1086         *ret_req = NULL;
1087
1088         req = talloc(mem_ctx, struct ldb_request);
1089         if (req == NULL) {
1090                 ldb_oom(ldb);
1091                 return LDB_ERR_OPERATIONS_ERROR;
1092         }
1093
1094         req->operation = LDB_SEARCH;
1095         if (base == NULL) {
1096                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
1097         } else {
1098                 req->op.search.base = base;
1099         }
1100         req->op.search.scope = scope;
1101
1102         req->op.search.tree = tree;
1103         if (req->op.search.tree == NULL) {
1104                 ldb_set_errstring(ldb, "'tree' can't be NULL");
1105                 talloc_free(req);
1106                 return LDB_ERR_OPERATIONS_ERROR;
1107         }
1108
1109         req->op.search.attrs = attrs;
1110         req->controls = controls;
1111         req->context = context;
1112         req->callback = callback;
1113
1114         ldb_set_timeout_from_prev_req(ldb, parent, req);
1115
1116         req->handle = ldb_handle_new(req, ldb);
1117         if (req->handle == NULL) {
1118                 ldb_oom(ldb);
1119                 return LDB_ERR_OPERATIONS_ERROR;
1120         }
1121
1122         if (parent) {
1123                 req->handle->nesting++;
1124                 req->handle->parent = parent;
1125                 req->handle->flags = parent->handle->flags;
1126                 req->handle->custom_flags = parent->handle->custom_flags;
1127         }
1128
1129         *ret_req = req;
1130         return LDB_SUCCESS;
1131 }
1132
1133 int ldb_build_search_req(struct ldb_request **ret_req,
1134                         struct ldb_context *ldb,
1135                         TALLOC_CTX *mem_ctx,
1136                         struct ldb_dn *base,
1137                         enum ldb_scope scope,
1138                         const char *expression,
1139                         const char * const *attrs,
1140                         struct ldb_control **controls,
1141                         void *context,
1142                         ldb_request_callback_t callback,
1143                         struct ldb_request *parent)
1144 {
1145         struct ldb_parse_tree *tree;
1146         int ret;
1147
1148         tree = ldb_parse_tree(mem_ctx, expression);
1149         if (tree == NULL) {
1150                 ldb_set_errstring(ldb, "Unable to parse search expression");
1151                 return LDB_ERR_OPERATIONS_ERROR;
1152         }
1153
1154         ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1155                                       scope, tree, attrs, controls,
1156                                       context, callback, parent);
1157         if (ret == LDB_SUCCESS) {
1158                 talloc_steal(*ret_req, tree);
1159         }
1160         return ret;
1161 }
1162
1163 int ldb_build_add_req(struct ldb_request **ret_req,
1164                         struct ldb_context *ldb,
1165                         TALLOC_CTX *mem_ctx,
1166                         const struct ldb_message *message,
1167                         struct ldb_control **controls,
1168                         void *context,
1169                         ldb_request_callback_t callback,
1170                         struct ldb_request *parent)
1171 {
1172         struct ldb_request *req;
1173
1174         *ret_req = NULL;
1175
1176         req = talloc(mem_ctx, struct ldb_request);
1177         if (req == NULL) {
1178                 ldb_set_errstring(ldb, "Out of Memory");
1179                 return LDB_ERR_OPERATIONS_ERROR;
1180         }
1181
1182         req->operation = LDB_ADD;
1183         req->op.add.message = message;
1184         req->controls = controls;
1185         req->context = context;
1186         req->callback = callback;
1187
1188         ldb_set_timeout_from_prev_req(ldb, parent, req);
1189
1190         req->handle = ldb_handle_new(req, ldb);
1191         if (req->handle == NULL) {
1192                 ldb_oom(ldb);
1193                 return LDB_ERR_OPERATIONS_ERROR;
1194         }
1195
1196         if (parent) {
1197                 req->handle->nesting++;
1198                 req->handle->parent = parent;
1199                 req->handle->flags = parent->handle->flags;
1200                 req->handle->custom_flags = parent->handle->custom_flags;
1201         }
1202
1203         *ret_req = req;
1204
1205         return LDB_SUCCESS;
1206 }
1207
1208 int ldb_build_mod_req(struct ldb_request **ret_req,
1209                         struct ldb_context *ldb,
1210                         TALLOC_CTX *mem_ctx,
1211                         const struct ldb_message *message,
1212                         struct ldb_control **controls,
1213                         void *context,
1214                         ldb_request_callback_t callback,
1215                         struct ldb_request *parent)
1216 {
1217         struct ldb_request *req;
1218
1219         *ret_req = NULL;
1220
1221         req = talloc(mem_ctx, struct ldb_request);
1222         if (req == NULL) {
1223                 ldb_set_errstring(ldb, "Out of Memory");
1224                 return LDB_ERR_OPERATIONS_ERROR;
1225         }
1226
1227         req->operation = LDB_MODIFY;
1228         req->op.mod.message = message;
1229         req->controls = controls;
1230         req->context = context;
1231         req->callback = callback;
1232
1233         ldb_set_timeout_from_prev_req(ldb, parent, req);
1234
1235         req->handle = ldb_handle_new(req, ldb);
1236         if (req->handle == NULL) {
1237                 ldb_oom(ldb);
1238                 return LDB_ERR_OPERATIONS_ERROR;
1239         }
1240
1241         if (parent) {
1242                 req->handle->nesting++;
1243                 req->handle->parent = parent;
1244                 req->handle->flags = parent->handle->flags;
1245                 req->handle->custom_flags = parent->handle->custom_flags;
1246         }
1247
1248         *ret_req = req;
1249
1250         return LDB_SUCCESS;
1251 }
1252
1253 int ldb_build_del_req(struct ldb_request **ret_req,
1254                         struct ldb_context *ldb,
1255                         TALLOC_CTX *mem_ctx,
1256                         struct ldb_dn *dn,
1257                         struct ldb_control **controls,
1258                         void *context,
1259                         ldb_request_callback_t callback,
1260                         struct ldb_request *parent)
1261 {
1262         struct ldb_request *req;
1263
1264         *ret_req = NULL;
1265
1266         req = talloc(mem_ctx, struct ldb_request);
1267         if (req == NULL) {
1268                 ldb_set_errstring(ldb, "Out of Memory");
1269                 return LDB_ERR_OPERATIONS_ERROR;
1270         }
1271
1272         req->operation = LDB_DELETE;
1273         req->op.del.dn = dn;
1274         req->controls = controls;
1275         req->context = context;
1276         req->callback = callback;
1277
1278         ldb_set_timeout_from_prev_req(ldb, parent, req);
1279
1280         req->handle = ldb_handle_new(req, ldb);
1281         if (req->handle == NULL) {
1282                 ldb_oom(ldb);
1283                 return LDB_ERR_OPERATIONS_ERROR;
1284         }
1285
1286         if (parent) {
1287                 req->handle->nesting++;
1288                 req->handle->parent = parent;
1289                 req->handle->flags = parent->handle->flags;
1290                 req->handle->custom_flags = parent->handle->custom_flags;
1291         }
1292
1293         *ret_req = req;
1294
1295         return LDB_SUCCESS;
1296 }
1297
1298 int ldb_build_rename_req(struct ldb_request **ret_req,
1299                         struct ldb_context *ldb,
1300                         TALLOC_CTX *mem_ctx,
1301                         struct ldb_dn *olddn,
1302                         struct ldb_dn *newdn,
1303                         struct ldb_control **controls,
1304                         void *context,
1305                         ldb_request_callback_t callback,
1306                         struct ldb_request *parent)
1307 {
1308         struct ldb_request *req;
1309
1310         *ret_req = NULL;
1311
1312         req = talloc(mem_ctx, struct ldb_request);
1313         if (req == NULL) {
1314                 ldb_set_errstring(ldb, "Out of Memory");
1315                 return LDB_ERR_OPERATIONS_ERROR;
1316         }
1317
1318         req->operation = LDB_RENAME;
1319         req->op.rename.olddn = olddn;
1320         req->op.rename.newdn = newdn;
1321         req->controls = controls;
1322         req->context = context;
1323         req->callback = callback;
1324
1325         ldb_set_timeout_from_prev_req(ldb, parent, req);
1326
1327         req->handle = ldb_handle_new(req, ldb);
1328         if (req->handle == NULL) {
1329                 ldb_oom(ldb);
1330                 return LDB_ERR_OPERATIONS_ERROR;
1331         }
1332
1333         if (parent) {
1334                 req->handle->nesting++;
1335                 req->handle->parent = parent;
1336                 req->handle->flags = parent->handle->flags;
1337                 req->handle->custom_flags = parent->handle->custom_flags;
1338         }
1339
1340         *ret_req = req;
1341
1342         return LDB_SUCCESS;
1343 }
1344
1345 int ldb_extended_default_callback(struct ldb_request *req,
1346                                   struct ldb_reply *ares)
1347 {
1348         struct ldb_result *res;
1349
1350         res = talloc_get_type(req->context, struct ldb_result);
1351
1352         if (!ares) {
1353                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1354         }
1355         if (ares->error != LDB_SUCCESS) {
1356                 return ldb_request_done(req, ares->error);
1357         }
1358
1359         if (ares->type == LDB_REPLY_DONE) {
1360
1361                 /* TODO: we should really support controls on entries and referrals too! */
1362                 res->extended = talloc_move(res, &ares->response);
1363                 res->controls = talloc_move(res, &ares->controls);
1364
1365                 talloc_free(ares);
1366                 return ldb_request_done(req, LDB_SUCCESS);
1367         }
1368
1369         talloc_free(ares);
1370         ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1371         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1372 }
1373
1374 int ldb_build_extended_req(struct ldb_request **ret_req,
1375                            struct ldb_context *ldb,
1376                            TALLOC_CTX *mem_ctx,
1377                            const char *oid,
1378                            void *data,
1379                            struct ldb_control **controls,
1380                            void *context,
1381                            ldb_request_callback_t callback,
1382                            struct ldb_request *parent)
1383 {
1384         struct ldb_request *req;
1385
1386         *ret_req = NULL;
1387
1388         req = talloc(mem_ctx, struct ldb_request);
1389         if (req == NULL) {
1390                 ldb_set_errstring(ldb, "Out of Memory");
1391                 return LDB_ERR_OPERATIONS_ERROR;
1392         }
1393
1394         req->operation = LDB_EXTENDED;
1395         req->op.extended.oid = oid;
1396         req->op.extended.data = data;
1397         req->controls = controls;
1398         req->context = context;
1399         req->callback = callback;
1400
1401         ldb_set_timeout_from_prev_req(ldb, parent, req);
1402
1403         req->handle = ldb_handle_new(req, ldb);
1404         if (req->handle == NULL) {
1405                 ldb_oom(ldb);
1406                 return LDB_ERR_OPERATIONS_ERROR;
1407         }
1408
1409         if (parent) {
1410                 req->handle->nesting++;
1411                 req->handle->parent = parent;
1412                 req->handle->flags = parent->handle->flags;
1413                 req->handle->custom_flags = parent->handle->custom_flags;
1414         }
1415
1416         *ret_req = req;
1417
1418         return LDB_SUCCESS;
1419 }
1420
1421 int ldb_extended(struct ldb_context *ldb,
1422                  const char *oid,
1423                  void *data,
1424                  struct ldb_result **_res)
1425 {
1426         struct ldb_request *req;
1427         int ret;
1428         struct ldb_result *res;
1429
1430         *_res = NULL;
1431         req = NULL;
1432
1433         res = talloc_zero(ldb, struct ldb_result);
1434         if (!res) {
1435                 return LDB_ERR_OPERATIONS_ERROR;
1436         }
1437
1438         ret = ldb_build_extended_req(&req, ldb, ldb,
1439                                      oid, data, NULL,
1440                                      res, ldb_extended_default_callback,
1441                                      NULL);
1442         ldb_req_set_location(req, "ldb_extended");
1443
1444         if (ret != LDB_SUCCESS) goto done;
1445
1446         ldb_set_timeout(ldb, req, 0); /* use default timeout */
1447
1448         ret = ldb_request(ldb, req);
1449
1450         if (ret == LDB_SUCCESS) {
1451                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1452         }
1453
1454 done:
1455         if (ret != LDB_SUCCESS) {
1456                 talloc_free(res);
1457                 res = NULL;
1458         }
1459
1460         talloc_free(req);
1461
1462         *_res = res;
1463         return ret;
1464 }
1465
1466 /*
1467   note that ldb_search() will automatically replace a NULL 'base' value
1468   with the defaultNamingContext from the rootDSE if available.
1469 */
1470 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1471                 struct ldb_result **result, struct ldb_dn *base,
1472                 enum ldb_scope scope, const char * const *attrs,
1473                 const char *exp_fmt, ...)
1474 {
1475         struct ldb_request *req;
1476         struct ldb_result *res;
1477         char *expression;
1478         va_list ap;
1479         int ret;
1480
1481         expression = NULL;
1482         *result = NULL;
1483         req = NULL;
1484
1485         res = talloc_zero(mem_ctx, struct ldb_result);
1486         if (!res) {
1487                 return LDB_ERR_OPERATIONS_ERROR;
1488         }
1489
1490         if (exp_fmt) {
1491                 va_start(ap, exp_fmt);
1492                 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1493                 va_end(ap);
1494
1495                 if (!expression) {
1496                         talloc_free(res);
1497                         return LDB_ERR_OPERATIONS_ERROR;
1498                 }
1499         }
1500
1501         ret = ldb_build_search_req(&req, ldb, mem_ctx,
1502                                         base?base:ldb_get_default_basedn(ldb),
1503                                         scope,
1504                                         expression,
1505                                         attrs,
1506                                         NULL,
1507                                         res,
1508                                         ldb_search_default_callback,
1509                                         NULL);
1510         ldb_req_set_location(req, "ldb_search");
1511
1512         if (ret != LDB_SUCCESS) goto done;
1513
1514         ret = ldb_request(ldb, req);
1515
1516         if (ret == LDB_SUCCESS) {
1517                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1518         }
1519
1520 done:
1521         if (ret != LDB_SUCCESS) {
1522                 talloc_free(res);
1523                 res = NULL;
1524         }
1525
1526         talloc_free(expression);
1527         talloc_free(req);
1528
1529         *result = res;
1530         return ret;
1531 }
1532
1533 /*
1534   add a record to the database. Will fail if a record with the given class
1535   and key already exists
1536 */
1537 int ldb_add(struct ldb_context *ldb,
1538             const struct ldb_message *message)
1539 {
1540         struct ldb_request *req;
1541         int ret;
1542
1543         ret = ldb_msg_sanity_check(ldb, message);
1544         if (ret != LDB_SUCCESS) {
1545                 return ret;
1546         }
1547
1548         ret = ldb_build_add_req(&req, ldb, ldb,
1549                                         message,
1550                                         NULL,
1551                                         NULL,
1552                                         ldb_op_default_callback,
1553                                         NULL);
1554         ldb_req_set_location(req, "ldb_add");
1555
1556         if (ret != LDB_SUCCESS) return ret;
1557
1558         /* do request and autostart a transaction */
1559         ret = ldb_autotransaction_request(ldb, req);
1560
1561         talloc_free(req);
1562         return ret;
1563 }
1564
1565 /*
1566   modify the specified attributes of a record
1567 */
1568 int ldb_modify(struct ldb_context *ldb,
1569                const struct ldb_message *message)
1570 {
1571         struct ldb_request *req;
1572         int ret;
1573
1574         ret = ldb_msg_sanity_check(ldb, message);
1575         if (ret != LDB_SUCCESS) {
1576                 return ret;
1577         }
1578
1579         ret = ldb_build_mod_req(&req, ldb, ldb,
1580                                         message,
1581                                         NULL,
1582                                         NULL,
1583                                         ldb_op_default_callback,
1584                                         NULL);
1585         ldb_req_set_location(req, "ldb_modify");
1586
1587         if (ret != LDB_SUCCESS) return ret;
1588
1589         /* do request and autostart a transaction */
1590         ret = ldb_autotransaction_request(ldb, req);
1591
1592         talloc_free(req);
1593         return ret;
1594 }
1595
1596
1597 /*
1598   delete a record from the database
1599 */
1600 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1601 {
1602         struct ldb_request *req;
1603         int ret;
1604
1605         ret = ldb_build_del_req(&req, ldb, ldb,
1606                                         dn,
1607                                         NULL,
1608                                         NULL,
1609                                         ldb_op_default_callback,
1610                                         NULL);
1611         ldb_req_set_location(req, "ldb_delete");
1612
1613         if (ret != LDB_SUCCESS) return ret;
1614
1615         /* do request and autostart a transaction */
1616         ret = ldb_autotransaction_request(ldb, req);
1617
1618         talloc_free(req);
1619         return ret;
1620 }
1621
1622 /*
1623   rename a record in the database
1624 */
1625 int ldb_rename(struct ldb_context *ldb,
1626                 struct ldb_dn *olddn, struct ldb_dn *newdn)
1627 {
1628         struct ldb_request *req;
1629         int ret;
1630
1631         ret = ldb_build_rename_req(&req, ldb, ldb,
1632                                         olddn,
1633                                         newdn,
1634                                         NULL,
1635                                         NULL,
1636                                         ldb_op_default_callback,
1637                                         NULL);
1638         ldb_req_set_location(req, "ldb_rename");
1639
1640         if (ret != LDB_SUCCESS) return ret;
1641
1642         /* do request and autostart a transaction */
1643         ret = ldb_autotransaction_request(ldb, req);
1644
1645         talloc_free(req);
1646         return ret;
1647 }
1648
1649
1650 /*
1651   return the global sequence number
1652 */
1653 int ldb_sequence_number(struct ldb_context *ldb,
1654                         enum ldb_sequence_type type, uint64_t *seq_num)
1655 {
1656         struct ldb_seqnum_request *seq;
1657         struct ldb_seqnum_result *seqr;
1658         struct ldb_result *res;
1659         TALLOC_CTX *tmp_ctx;
1660         int ret;
1661
1662         *seq_num = 0;
1663
1664         tmp_ctx = talloc_zero(ldb, struct ldb_request);
1665         if (tmp_ctx == NULL) {
1666                 ldb_set_errstring(ldb, "Out of Memory");
1667                 return LDB_ERR_OPERATIONS_ERROR;
1668         }
1669         seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1670         if (seq == NULL) {
1671                 ldb_set_errstring(ldb, "Out of Memory");
1672                 ret = LDB_ERR_OPERATIONS_ERROR;
1673                 goto done;
1674         }
1675         seq->type = type;
1676
1677         ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1678         if (ret != LDB_SUCCESS) {
1679                 goto done;
1680         }
1681         talloc_steal(tmp_ctx, res);
1682
1683         if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1684                 ldb_set_errstring(ldb, "Invalid OID in reply");
1685                 ret = LDB_ERR_OPERATIONS_ERROR;
1686                 goto done;
1687         }
1688         seqr = talloc_get_type(res->extended->data,
1689                                 struct ldb_seqnum_result);
1690         *seq_num = seqr->seq_num;
1691
1692 done:
1693         talloc_free(tmp_ctx);
1694         return ret;
1695 }
1696
1697 /*
1698   return extended error information
1699 */
1700 const char *ldb_errstring(struct ldb_context *ldb)
1701 {
1702         if (ldb->err_string) {
1703                 return ldb->err_string;
1704         }
1705
1706         return NULL;
1707 }
1708
1709 /*
1710   return a string explaining what a ldb error constant meancs
1711 */
1712 const char *ldb_strerror(int ldb_err)
1713 {
1714         switch (ldb_err) {
1715         case LDB_SUCCESS:
1716                 return "Success";
1717         case LDB_ERR_OPERATIONS_ERROR:
1718                 return "Operations error";
1719         case LDB_ERR_PROTOCOL_ERROR:
1720                 return "Protocol error";
1721         case LDB_ERR_TIME_LIMIT_EXCEEDED:
1722                 return "Time limit exceeded";
1723         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1724                 return "Size limit exceeded";
1725         case LDB_ERR_COMPARE_FALSE:
1726                 return "Compare false";
1727         case LDB_ERR_COMPARE_TRUE:
1728                 return "Compare true";
1729         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1730                 return "Auth method not supported";
1731         case LDB_ERR_STRONG_AUTH_REQUIRED:
1732                 return "Strong auth required";
1733 /* 9 RESERVED */
1734         case LDB_ERR_REFERRAL:
1735                 return "Referral error";
1736         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1737                 return "Admin limit exceeded";
1738         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1739                 return "Unsupported critical extension";
1740         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1741                 return "Confidentiality required";
1742         case LDB_ERR_SASL_BIND_IN_PROGRESS:
1743                 return "SASL bind in progress";
1744         case LDB_ERR_NO_SUCH_ATTRIBUTE:
1745                 return "No such attribute";
1746         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1747                 return "Undefined attribute type";
1748         case LDB_ERR_INAPPROPRIATE_MATCHING:
1749                 return "Inappropriate matching";
1750         case LDB_ERR_CONSTRAINT_VIOLATION:
1751                 return "Constraint violation";
1752         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1753                 return "Attribute or value exists";
1754         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1755                 return "Invalid attribute syntax";
1756 /* 22-31 unused */
1757         case LDB_ERR_NO_SUCH_OBJECT:
1758                 return "No such object";
1759         case LDB_ERR_ALIAS_PROBLEM:
1760                 return "Alias problem";
1761         case LDB_ERR_INVALID_DN_SYNTAX:
1762                 return "Invalid DN syntax";
1763 /* 35 RESERVED */
1764         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1765                 return "Alias dereferencing problem";
1766 /* 37-47 unused */
1767         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1768                 return "Inappropriate authentication";
1769         case LDB_ERR_INVALID_CREDENTIALS:
1770                 return "Invalid credentials";
1771         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1772                 return "insufficient access rights";
1773         case LDB_ERR_BUSY:
1774                 return "Busy";
1775         case LDB_ERR_UNAVAILABLE:
1776                 return "Unavailable";
1777         case LDB_ERR_UNWILLING_TO_PERFORM:
1778                 return "Unwilling to perform";
1779         case LDB_ERR_LOOP_DETECT:
1780                 return "Loop detect";
1781 /* 55-63 unused */
1782         case LDB_ERR_NAMING_VIOLATION:
1783                 return "Naming violation";
1784         case LDB_ERR_OBJECT_CLASS_VIOLATION:
1785                 return "Object class violation";
1786         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1787                 return "Not allowed on non-leaf";
1788         case LDB_ERR_NOT_ALLOWED_ON_RDN:
1789                 return "Not allowed on RDN";
1790         case LDB_ERR_ENTRY_ALREADY_EXISTS:
1791                 return "Entry already exists";
1792         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1793                 return "Object class mods prohibited";
1794 /* 70 RESERVED FOR CLDAP */
1795         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1796                 return "Affects multiple DSAs";
1797 /* 72-79 unused */
1798         case LDB_ERR_OTHER:
1799                 return "Other";
1800         }
1801
1802         return "Unknown error";
1803 }
1804
1805 /*
1806   set backend specific opaque parameters
1807 */
1808 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1809 {
1810         struct ldb_opaque *o;
1811
1812         /* allow updating an existing value */
1813         for (o=ldb->opaque;o;o=o->next) {
1814                 if (strcmp(o->name, name) == 0) {
1815                         o->value = value;
1816                         return LDB_SUCCESS;
1817                 }
1818         }
1819
1820         o = talloc(ldb, struct ldb_opaque);
1821         if (o == NULL) {
1822                 ldb_oom(ldb);
1823                 return LDB_ERR_OTHER;
1824         }
1825         o->next = ldb->opaque;
1826         o->name = name;
1827         o->value = value;
1828         ldb->opaque = o;
1829         return LDB_SUCCESS;
1830 }
1831
1832 /*
1833   get a previously set opaque value
1834 */
1835 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1836 {
1837         struct ldb_opaque *o;
1838         for (o=ldb->opaque;o;o=o->next) {
1839                 if (strcmp(o->name, name) == 0) {
1840                         return o->value;
1841                 }
1842         }
1843         return NULL;
1844 }
1845
1846 int ldb_global_init(void)
1847 {
1848         /* Provided for compatibility with some older versions of ldb */
1849         return 0;
1850 }
1851
1852 /* return the ldb flags */
1853 unsigned int ldb_get_flags(struct ldb_context *ldb)
1854 {
1855         return ldb->flags;
1856 }
1857
1858 /* set the ldb flags */
1859 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
1860 {
1861         ldb->flags = flags;
1862 }
1863
1864
1865 /*
1866   set the location in a ldb request. Used for debugging
1867  */
1868 void ldb_req_set_location(struct ldb_request *req, const char *location)
1869 {
1870         if (req && req->handle) {
1871                 req->handle->location = location;
1872         }
1873 }
1874
1875 /*
1876   return the location set with dsdb_req_set_location
1877  */
1878 const char *ldb_req_location(struct ldb_request *req)
1879 {
1880         return req->handle->location;
1881 }
1882
1883 /**
1884   mark a request as untrusted. This tells the rootdse module to remove
1885   unregistered controls
1886  */
1887 void ldb_req_mark_untrusted(struct ldb_request *req)
1888 {
1889         req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
1890 }
1891
1892 /**
1893   mark a request as trusted.
1894  */
1895 void ldb_req_mark_trusted(struct ldb_request *req)
1896 {
1897         req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
1898 }
1899
1900 /**
1901   set custom flags. Those flags are set by applications using ldb,
1902   they are application dependent and the same bit can have different
1903   meaning in different application.
1904  */
1905 void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
1906 {
1907         if (req != NULL && req->handle != NULL) {
1908                 req->handle->custom_flags = flags;
1909         }
1910 }
1911
1912
1913 /**
1914   get custom flags. Those flags are set by applications using ldb,
1915   they are application dependent and the same bit can have different
1916   meaning in different application.
1917  */
1918 uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
1919 {
1920         if (req != NULL && req->handle != NULL) {
1921                 return req->handle->custom_flags;
1922         }
1923
1924         /*
1925          * 0 is not something any better or worse than
1926          * anything else as req or the handle is NULL
1927          */
1928         return 0;
1929 }
1930
1931
1932 /**
1933    return true is a request is untrusted
1934  */
1935 bool ldb_req_is_untrusted(struct ldb_request *req)
1936 {
1937         return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;
1938 }