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