dsdb: improve debug message
[nivanova/samba-autobuild/.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         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
731         switch (req->operation) {
732         case LDB_SEARCH:
733                 ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
734                 ldb_debug_add(ldb, " dn: %s\n",
735                               ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
736                               ldb_dn_get_linearized(req->op.search.base));
737                 ldb_debug_add(ldb, " scope: %s\n", 
738                           req->op.search.scope==LDB_SCOPE_BASE?"base":
739                           req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
740                           req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
741                 ldb_debug_add(ldb, " expr: %s\n", 
742                           ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
743                 if (req->op.search.attrs == NULL) {
744                         ldb_debug_add(ldb, " attr: <ALL>\n");
745                 } else {
746                         for (i=0; req->op.search.attrs[i]; i++) {
747                                 ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
748                         }
749                 }
750                 break;
751         case LDB_DELETE:
752                 ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
753                 ldb_debug_add(ldb, " dn: %s\n", 
754                               ldb_dn_get_linearized(req->op.del.dn));
755                 break;
756         case LDB_RENAME:
757                 ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
758                 ldb_debug_add(ldb, " olddn: %s\n", 
759                               ldb_dn_get_linearized(req->op.rename.olddn));
760                 ldb_debug_add(ldb, " newdn: %s\n", 
761                               ldb_dn_get_linearized(req->op.rename.newdn));
762                 break;
763         case LDB_EXTENDED:
764                 ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
765                 ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
766                 ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
767                 break;
768         case LDB_ADD:
769                 ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
770                 ldb_debug_add(req->handle->ldb, "%s\n", 
771                               ldb_ldif_message_string(req->handle->ldb, tmp_ctx, 
772                                                       LDB_CHANGETYPE_ADD, 
773                                                       req->op.add.message));
774                 break;
775         case LDB_MODIFY:
776                 ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
777                 ldb_debug_add(req->handle->ldb, "%s\n", 
778                               ldb_ldif_message_string(req->handle->ldb, tmp_ctx, 
779                                                       LDB_CHANGETYPE_ADD, 
780                                                       req->op.mod.message));
781                 break;
782         case LDB_REQ_REGISTER_CONTROL:
783                 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
784                 ldb_debug_add(req->handle->ldb, "%s\n", 
785                               req->op.reg_control.oid);
786                 break;
787         case LDB_REQ_REGISTER_PARTITION:
788                 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
789                 ldb_debug_add(req->handle->ldb, "%s\n", 
790                               ldb_dn_get_linearized(req->op.reg_partition.dn));
791                 break;
792         default:
793                 ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n", 
794                               req->operation);
795                 break;
796         }
797
798         if (req->controls == NULL) {
799                 ldb_debug_add(ldb, " control: <NONE>\n");
800         } else {
801                 for (i=0; req->controls && req->controls[i]; i++) {
802                         if (req->controls[i]->oid) {
803                                 ldb_debug_add(ldb, " control: %s  crit:%u  data:%s\n",
804                                               req->controls[i]->oid,
805                                               req->controls[i]->critical,
806                                               req->controls[i]->data?"yes":"no");
807                         }
808                 }
809         }
810         
811         ldb_debug_end(ldb, LDB_DEBUG_TRACE);
812
813         talloc_free(tmp_ctx);
814 }
815
816 /*
817   check that the element flags don't have any internal bits set
818  */
819 static int ldb_msg_check_element_flags(struct ldb_context *ldb,
820                                        const struct ldb_message *message)
821 {
822         unsigned i;
823         for (i=0; i<message->num_elements; i++) {
824                 if (message->elements[i].flags & LDB_FLAG_INTERNAL_MASK) {
825                         ldb_asprintf_errstring(ldb, "Invalid element flags 0x%08x on element %s in %s\n",
826                                                message->elements[i].flags, message->elements[i].name,
827                                                ldb_dn_get_linearized(message->dn));
828                         return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
829                 }
830         }
831         return LDB_SUCCESS;
832 }
833
834
835 /*
836   start an ldb request
837   NOTE: the request must be a talloc context.
838   returns LDB_ERR_* on errors.
839 */
840 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
841 {
842         struct ldb_module *module;
843         int ret;
844
845         if (req->callback == NULL) {
846                 ldb_set_errstring(ldb, "Requests MUST define callbacks");
847                 return LDB_ERR_UNWILLING_TO_PERFORM;
848         }
849
850         ldb_reset_err_string(ldb);
851
852         if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
853                 ldb_trace_request(ldb, req);
854         }
855
856         /* call the first module in the chain */
857         switch (req->operation) {
858         case LDB_SEARCH:
859                 /* due to "ldb_build_search_req" base DN always != NULL */
860                 if (!ldb_dn_validate(req->op.search.base)) {
861                         ldb_asprintf_errstring(ldb, "ldb_search: invalid basedn '%s'",
862                                                ldb_dn_get_linearized(req->op.search.base));
863                         return LDB_ERR_INVALID_DN_SYNTAX;
864                 }
865                 FIRST_OP(ldb, search);
866                 ret = module->ops->search(module, req);
867                 break;
868         case LDB_ADD:
869                 if (!ldb_dn_validate(req->op.add.message->dn)) {
870                         ldb_asprintf_errstring(ldb, "ldb_add: invalid dn '%s'",
871                                                ldb_dn_get_linearized(req->op.add.message->dn));
872                         return LDB_ERR_INVALID_DN_SYNTAX;
873                 }
874                 /*
875                  * we have to normalize here, as so many places
876                  * in modules and backends assume we don't have two
877                  * elements with the same name
878                  */
879                 ret = ldb_msg_normalize(ldb, req, req->op.add.message,
880                                         discard_const(&req->op.add.message));
881                 if (ret != LDB_SUCCESS) {
882                         ldb_oom(ldb);
883                         return ret;
884                 }
885                 FIRST_OP(ldb, add);
886                 ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
887                 if (ret != LDB_SUCCESS) {
888                         /*
889                          * "ldb_msg_check_element_flags" generates an error
890                          * string
891                          */
892                         return ret;
893                 }
894                 ret = module->ops->add(module, req);
895                 break;
896         case LDB_MODIFY:
897                 if (!ldb_dn_validate(req->op.mod.message->dn)) {
898                         ldb_asprintf_errstring(ldb, "ldb_modify: invalid dn '%s'",
899                                                ldb_dn_get_linearized(req->op.mod.message->dn));
900                         return LDB_ERR_INVALID_DN_SYNTAX;
901                 }
902                 FIRST_OP(ldb, modify);
903                 ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
904                 if (ret != LDB_SUCCESS) {
905                         /*
906                          * "ldb_msg_check_element_flags" generates an error
907                          * string
908                          */
909                         return ret;
910                 }
911                 ret = module->ops->modify(module, req);
912                 break;
913         case LDB_DELETE:
914                 if (!ldb_dn_validate(req->op.del.dn)) {
915                         ldb_asprintf_errstring(ldb, "ldb_delete: invalid dn '%s'",
916                                                ldb_dn_get_linearized(req->op.del.dn));
917                         return LDB_ERR_INVALID_DN_SYNTAX;
918                 }
919                 FIRST_OP(ldb, del);
920                 ret = module->ops->del(module, req);
921                 break;
922         case LDB_RENAME:
923                 if (!ldb_dn_validate(req->op.rename.olddn)) {
924                         ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
925                                                ldb_dn_get_linearized(req->op.rename.olddn));
926                         return LDB_ERR_INVALID_DN_SYNTAX;
927                 }
928                 if (!ldb_dn_validate(req->op.rename.newdn)) {
929                         ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
930                                                ldb_dn_get_linearized(req->op.rename.newdn));
931                         return LDB_ERR_INVALID_DN_SYNTAX;
932                 }
933                 FIRST_OP(ldb, rename);
934                 ret = module->ops->rename(module, req);
935                 break;
936         case LDB_EXTENDED:
937                 FIRST_OP(ldb, extended);
938                 ret = module->ops->extended(module, req);
939                 break;
940         default:
941                 FIRST_OP(ldb, request);
942                 ret = module->ops->request(module, req);
943                 break;
944         }
945
946         if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
947                 /* if no error string was setup by the backend */
948                 ldb_asprintf_errstring(ldb, "ldb_request: %s (%d)",
949                                        ldb_strerror(ret), ret);
950         }
951
952         return ret;
953 }
954
955 int ldb_request_done(struct ldb_request *req, int status)
956 {
957         req->handle->state = LDB_ASYNC_DONE;
958         req->handle->status = status;
959         return status;
960 }
961
962 /*
963   search the database given a LDAP-like search expression
964
965   returns an LDB error code
966
967   Use talloc_free to free the ldb_message returned in 'res', if successful
968
969 */
970 int ldb_search_default_callback(struct ldb_request *req,
971                                 struct ldb_reply *ares)
972 {
973         struct ldb_result *res;
974         unsigned int n;
975
976         res = talloc_get_type(req->context, struct ldb_result);
977
978         if (!ares) {
979                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
980         }
981         if (ares->error != LDB_SUCCESS) {
982                 return ldb_request_done(req, ares->error);
983         }
984
985         switch (ares->type) {
986         case LDB_REPLY_ENTRY:
987                 res->msgs = talloc_realloc(res, res->msgs,
988                                         struct ldb_message *, res->count + 2);
989                 if (! res->msgs) {
990                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
991                 }
992
993                 res->msgs[res->count + 1] = NULL;
994
995                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
996                 res->count++;
997                 break;
998
999         case LDB_REPLY_REFERRAL:
1000                 if (res->refs) {
1001                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
1002                 } else {
1003                         n = 0;
1004                 }
1005
1006                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1007                 if (! res->refs) {
1008                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1009                 }
1010
1011                 res->refs[n] = talloc_move(res->refs, &ares->referral);
1012                 res->refs[n + 1] = NULL;
1013                 break;
1014
1015         case LDB_REPLY_DONE:
1016                 /* TODO: we should really support controls on entries
1017                  * and referrals too! */
1018                 res->controls = talloc_move(res, &ares->controls);
1019
1020                 /* this is the last message, and means the request is done */
1021                 /* we have to signal and eventual ldb_wait() waiting that the
1022                  * async request operation was completed */
1023                 talloc_free(ares);
1024                 return ldb_request_done(req, LDB_SUCCESS);
1025         }
1026
1027         talloc_free(ares);
1028
1029         return LDB_SUCCESS;
1030 }
1031
1032 int ldb_modify_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1033 {
1034         struct ldb_result *res;
1035         unsigned int n;
1036         int ret;
1037
1038         res = talloc_get_type(req->context, struct ldb_result);
1039
1040         if (!ares) {
1041                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1042         }
1043
1044         if (ares->error != LDB_SUCCESS) {
1045                 ret = ares->error;
1046                 talloc_free(ares);
1047                 return ldb_request_done(req, ret);
1048         }
1049
1050         switch (ares->type) {
1051         case LDB_REPLY_REFERRAL:
1052                 if (res->refs) {
1053                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
1054                 } else {
1055                         n = 0;
1056                 }
1057
1058                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
1059                 if (! res->refs) {
1060                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1061                 }
1062
1063                 res->refs[n] = talloc_move(res->refs, &ares->referral);
1064                 res->refs[n + 1] = NULL;
1065                 break;
1066
1067         case LDB_REPLY_DONE:
1068                 talloc_free(ares);
1069                 return ldb_request_done(req, LDB_SUCCESS);
1070         default:
1071                 talloc_free(ares);
1072                 ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1073                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1074         }
1075
1076         talloc_free(ares);
1077         return ldb_request_done(req, LDB_SUCCESS);
1078 }
1079
1080 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
1081 {
1082         int ret;
1083
1084         if (!ares) {
1085                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1086         }
1087
1088         if (ares->error != LDB_SUCCESS) {
1089                 ret = ares->error;
1090                 talloc_free(ares);
1091                 return ldb_request_done(req, ret);
1092         }
1093
1094         if (ares->type != LDB_REPLY_DONE) {
1095                 talloc_free(ares);
1096                 ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1097                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1098         }
1099
1100         talloc_free(ares);
1101         return ldb_request_done(req, LDB_SUCCESS);
1102 }
1103
1104 int ldb_build_search_req_ex(struct ldb_request **ret_req,
1105                         struct ldb_context *ldb,
1106                         TALLOC_CTX *mem_ctx,
1107                         struct ldb_dn *base,
1108                         enum ldb_scope scope,
1109                         struct ldb_parse_tree *tree,
1110                         const char * const *attrs,
1111                         struct ldb_control **controls,
1112                         void *context,
1113                         ldb_request_callback_t callback,
1114                         struct ldb_request *parent)
1115 {
1116         struct ldb_request *req;
1117
1118         *ret_req = NULL;
1119
1120         req = talloc(mem_ctx, struct ldb_request);
1121         if (req == NULL) {
1122                 ldb_oom(ldb);
1123                 return LDB_ERR_OPERATIONS_ERROR;
1124         }
1125
1126         req->operation = LDB_SEARCH;
1127         if (base == NULL) {
1128                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
1129         } else {
1130                 req->op.search.base = base;
1131         }
1132         req->op.search.scope = scope;
1133
1134         req->op.search.tree = tree;
1135         if (req->op.search.tree == NULL) {
1136                 ldb_set_errstring(ldb, "'tree' can't be NULL");
1137                 talloc_free(req);
1138                 return LDB_ERR_OPERATIONS_ERROR;
1139         }
1140
1141         req->op.search.attrs = attrs;
1142         req->controls = controls;
1143         req->context = context;
1144         req->callback = callback;
1145
1146         ldb_set_timeout_from_prev_req(ldb, parent, req);
1147
1148         req->handle = ldb_handle_new(req, ldb);
1149         if (req->handle == NULL) {
1150                 ldb_oom(ldb);
1151                 return LDB_ERR_OPERATIONS_ERROR;
1152         }
1153
1154         if (parent) {
1155                 req->handle->nesting++;
1156                 req->handle->parent = parent;
1157                 req->handle->flags = parent->handle->flags;
1158                 req->handle->custom_flags = parent->handle->custom_flags;
1159         }
1160
1161         *ret_req = req;
1162         return LDB_SUCCESS;
1163 }
1164
1165 int ldb_build_search_req(struct ldb_request **ret_req,
1166                         struct ldb_context *ldb,
1167                         TALLOC_CTX *mem_ctx,
1168                         struct ldb_dn *base,
1169                         enum ldb_scope scope,
1170                         const char *expression,
1171                         const char * const *attrs,
1172                         struct ldb_control **controls,
1173                         void *context,
1174                         ldb_request_callback_t callback,
1175                         struct ldb_request *parent)
1176 {
1177         struct ldb_parse_tree *tree;
1178         int ret;
1179
1180         tree = ldb_parse_tree(mem_ctx, expression);
1181         if (tree == NULL) {
1182                 ldb_set_errstring(ldb, "Unable to parse search expression");
1183                 return LDB_ERR_OPERATIONS_ERROR;
1184         }
1185
1186         ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
1187                                       scope, tree, attrs, controls,
1188                                       context, callback, parent);
1189         if (ret == LDB_SUCCESS) {
1190                 talloc_steal(*ret_req, tree);
1191         }
1192         return ret;
1193 }
1194
1195 int ldb_build_add_req(struct ldb_request **ret_req,
1196                         struct ldb_context *ldb,
1197                         TALLOC_CTX *mem_ctx,
1198                         const struct ldb_message *message,
1199                         struct ldb_control **controls,
1200                         void *context,
1201                         ldb_request_callback_t callback,
1202                         struct ldb_request *parent)
1203 {
1204         struct ldb_request *req;
1205
1206         *ret_req = NULL;
1207
1208         req = talloc(mem_ctx, struct ldb_request);
1209         if (req == NULL) {
1210                 ldb_set_errstring(ldb, "Out of Memory");
1211                 return LDB_ERR_OPERATIONS_ERROR;
1212         }
1213
1214         req->operation = LDB_ADD;
1215         req->op.add.message = message;
1216         req->controls = controls;
1217         req->context = context;
1218         req->callback = callback;
1219
1220         ldb_set_timeout_from_prev_req(ldb, parent, req);
1221
1222         req->handle = ldb_handle_new(req, ldb);
1223         if (req->handle == NULL) {
1224                 ldb_oom(ldb);
1225                 return LDB_ERR_OPERATIONS_ERROR;
1226         }
1227
1228         if (parent) {
1229                 req->handle->nesting++;
1230                 req->handle->parent = parent;
1231                 req->handle->flags = parent->handle->flags;
1232                 req->handle->custom_flags = parent->handle->custom_flags;
1233         }
1234
1235         *ret_req = req;
1236
1237         return LDB_SUCCESS;
1238 }
1239
1240 int ldb_build_mod_req(struct ldb_request **ret_req,
1241                         struct ldb_context *ldb,
1242                         TALLOC_CTX *mem_ctx,
1243                         const struct ldb_message *message,
1244                         struct ldb_control **controls,
1245                         void *context,
1246                         ldb_request_callback_t callback,
1247                         struct ldb_request *parent)
1248 {
1249         struct ldb_request *req;
1250
1251         *ret_req = NULL;
1252
1253         req = talloc(mem_ctx, struct ldb_request);
1254         if (req == NULL) {
1255                 ldb_set_errstring(ldb, "Out of Memory");
1256                 return LDB_ERR_OPERATIONS_ERROR;
1257         }
1258
1259         req->operation = LDB_MODIFY;
1260         req->op.mod.message = message;
1261         req->controls = controls;
1262         req->context = context;
1263         req->callback = callback;
1264
1265         ldb_set_timeout_from_prev_req(ldb, parent, req);
1266
1267         req->handle = ldb_handle_new(req, ldb);
1268         if (req->handle == NULL) {
1269                 ldb_oom(ldb);
1270                 return LDB_ERR_OPERATIONS_ERROR;
1271         }
1272
1273         if (parent) {
1274                 req->handle->nesting++;
1275                 req->handle->parent = parent;
1276                 req->handle->flags = parent->handle->flags;
1277                 req->handle->custom_flags = parent->handle->custom_flags;
1278         }
1279
1280         *ret_req = req;
1281
1282         return LDB_SUCCESS;
1283 }
1284
1285 int ldb_build_del_req(struct ldb_request **ret_req,
1286                         struct ldb_context *ldb,
1287                         TALLOC_CTX *mem_ctx,
1288                         struct ldb_dn *dn,
1289                         struct ldb_control **controls,
1290                         void *context,
1291                         ldb_request_callback_t callback,
1292                         struct ldb_request *parent)
1293 {
1294         struct ldb_request *req;
1295
1296         *ret_req = NULL;
1297
1298         req = talloc(mem_ctx, struct ldb_request);
1299         if (req == NULL) {
1300                 ldb_set_errstring(ldb, "Out of Memory");
1301                 return LDB_ERR_OPERATIONS_ERROR;
1302         }
1303
1304         req->operation = LDB_DELETE;
1305         req->op.del.dn = dn;
1306         req->controls = controls;
1307         req->context = context;
1308         req->callback = callback;
1309
1310         ldb_set_timeout_from_prev_req(ldb, parent, req);
1311
1312         req->handle = ldb_handle_new(req, ldb);
1313         if (req->handle == NULL) {
1314                 ldb_oom(ldb);
1315                 return LDB_ERR_OPERATIONS_ERROR;
1316         }
1317
1318         if (parent) {
1319                 req->handle->nesting++;
1320                 req->handle->parent = parent;
1321                 req->handle->flags = parent->handle->flags;
1322                 req->handle->custom_flags = parent->handle->custom_flags;
1323         }
1324
1325         *ret_req = req;
1326
1327         return LDB_SUCCESS;
1328 }
1329
1330 int ldb_build_rename_req(struct ldb_request **ret_req,
1331                         struct ldb_context *ldb,
1332                         TALLOC_CTX *mem_ctx,
1333                         struct ldb_dn *olddn,
1334                         struct ldb_dn *newdn,
1335                         struct ldb_control **controls,
1336                         void *context,
1337                         ldb_request_callback_t callback,
1338                         struct ldb_request *parent)
1339 {
1340         struct ldb_request *req;
1341
1342         *ret_req = NULL;
1343
1344         req = talloc(mem_ctx, struct ldb_request);
1345         if (req == NULL) {
1346                 ldb_set_errstring(ldb, "Out of Memory");
1347                 return LDB_ERR_OPERATIONS_ERROR;
1348         }
1349
1350         req->operation = LDB_RENAME;
1351         req->op.rename.olddn = olddn;
1352         req->op.rename.newdn = newdn;
1353         req->controls = controls;
1354         req->context = context;
1355         req->callback = callback;
1356
1357         ldb_set_timeout_from_prev_req(ldb, parent, req);
1358
1359         req->handle = ldb_handle_new(req, ldb);
1360         if (req->handle == NULL) {
1361                 ldb_oom(ldb);
1362                 return LDB_ERR_OPERATIONS_ERROR;
1363         }
1364
1365         if (parent) {
1366                 req->handle->nesting++;
1367                 req->handle->parent = parent;
1368                 req->handle->flags = parent->handle->flags;
1369                 req->handle->custom_flags = parent->handle->custom_flags;
1370         }
1371
1372         *ret_req = req;
1373
1374         return LDB_SUCCESS;
1375 }
1376
1377 int ldb_extended_default_callback(struct ldb_request *req,
1378                                   struct ldb_reply *ares)
1379 {
1380         struct ldb_result *res;
1381
1382         res = talloc_get_type(req->context, struct ldb_result);
1383
1384         if (!ares) {
1385                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1386         }
1387         if (ares->error != LDB_SUCCESS) {
1388                 return ldb_request_done(req, ares->error);
1389         }
1390
1391         if (ares->type == LDB_REPLY_DONE) {
1392
1393                 /* TODO: we should really support controls on entries and referrals too! */
1394                 res->extended = talloc_move(res, &ares->response);
1395                 res->controls = talloc_move(res, &ares->controls);
1396
1397                 talloc_free(ares);
1398                 return ldb_request_done(req, LDB_SUCCESS);
1399         }
1400
1401         talloc_free(ares);
1402         ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
1403         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1404 }
1405
1406 int ldb_build_extended_req(struct ldb_request **ret_req,
1407                            struct ldb_context *ldb,
1408                            TALLOC_CTX *mem_ctx,
1409                            const char *oid,
1410                            void *data,
1411                            struct ldb_control **controls,
1412                            void *context,
1413                            ldb_request_callback_t callback,
1414                            struct ldb_request *parent)
1415 {
1416         struct ldb_request *req;
1417
1418         *ret_req = NULL;
1419
1420         req = talloc(mem_ctx, struct ldb_request);
1421         if (req == NULL) {
1422                 ldb_set_errstring(ldb, "Out of Memory");
1423                 return LDB_ERR_OPERATIONS_ERROR;
1424         }
1425
1426         req->operation = LDB_EXTENDED;
1427         req->op.extended.oid = oid;
1428         req->op.extended.data = data;
1429         req->controls = controls;
1430         req->context = context;
1431         req->callback = callback;
1432
1433         ldb_set_timeout_from_prev_req(ldb, parent, req);
1434
1435         req->handle = ldb_handle_new(req, ldb);
1436         if (req->handle == NULL) {
1437                 ldb_oom(ldb);
1438                 return LDB_ERR_OPERATIONS_ERROR;
1439         }
1440
1441         if (parent) {
1442                 req->handle->nesting++;
1443                 req->handle->parent = parent;
1444                 req->handle->flags = parent->handle->flags;
1445                 req->handle->custom_flags = parent->handle->custom_flags;
1446         }
1447
1448         *ret_req = req;
1449
1450         return LDB_SUCCESS;
1451 }
1452
1453 int ldb_extended(struct ldb_context *ldb,
1454                  const char *oid,
1455                  void *data,
1456                  struct ldb_result **_res)
1457 {
1458         struct ldb_request *req;
1459         int ret;
1460         struct ldb_result *res;
1461
1462         *_res = NULL;
1463         req = NULL;
1464
1465         res = talloc_zero(ldb, struct ldb_result);
1466         if (!res) {
1467                 return LDB_ERR_OPERATIONS_ERROR;
1468         }
1469
1470         ret = ldb_build_extended_req(&req, ldb, ldb,
1471                                      oid, data, NULL,
1472                                      res, ldb_extended_default_callback,
1473                                      NULL);
1474         ldb_req_set_location(req, "ldb_extended");
1475
1476         if (ret != LDB_SUCCESS) goto done;
1477
1478         ldb_set_timeout(ldb, req, 0); /* use default timeout */
1479
1480         ret = ldb_request(ldb, req);
1481
1482         if (ret == LDB_SUCCESS) {
1483                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1484         }
1485
1486 done:
1487         if (ret != LDB_SUCCESS) {
1488                 talloc_free(res);
1489                 res = NULL;
1490         }
1491
1492         talloc_free(req);
1493
1494         *_res = res;
1495         return ret;
1496 }
1497
1498 /*
1499   note that ldb_search() will automatically replace a NULL 'base' value
1500   with the defaultNamingContext from the rootDSE if available.
1501 */
1502 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1503                 struct ldb_result **result, struct ldb_dn *base,
1504                 enum ldb_scope scope, const char * const *attrs,
1505                 const char *exp_fmt, ...)
1506 {
1507         struct ldb_request *req;
1508         struct ldb_result *res;
1509         char *expression;
1510         va_list ap;
1511         int ret;
1512
1513         expression = NULL;
1514         *result = NULL;
1515         req = NULL;
1516
1517         res = talloc_zero(mem_ctx, struct ldb_result);
1518         if (!res) {
1519                 return LDB_ERR_OPERATIONS_ERROR;
1520         }
1521
1522         if (exp_fmt) {
1523                 va_start(ap, exp_fmt);
1524                 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1525                 va_end(ap);
1526
1527                 if (!expression) {
1528                         talloc_free(res);
1529                         return LDB_ERR_OPERATIONS_ERROR;
1530                 }
1531         }
1532
1533         ret = ldb_build_search_req(&req, ldb, mem_ctx,
1534                                         base?base:ldb_get_default_basedn(ldb),
1535                                         scope,
1536                                         expression,
1537                                         attrs,
1538                                         NULL,
1539                                         res,
1540                                         ldb_search_default_callback,
1541                                         NULL);
1542         ldb_req_set_location(req, "ldb_search");
1543
1544         if (ret != LDB_SUCCESS) goto done;
1545
1546         ret = ldb_request(ldb, req);
1547
1548         if (ret == LDB_SUCCESS) {
1549                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1550         }
1551
1552 done:
1553         if (ret != LDB_SUCCESS) {
1554                 talloc_free(res);
1555                 res = NULL;
1556         }
1557
1558         talloc_free(expression);
1559         talloc_free(req);
1560
1561         *result = res;
1562         return ret;
1563 }
1564
1565 /*
1566   add a record to the database. Will fail if a record with the given class
1567   and key already exists
1568 */
1569 int ldb_add(struct ldb_context *ldb,
1570             const struct ldb_message *message)
1571 {
1572         struct ldb_request *req;
1573         int ret;
1574
1575         ret = ldb_msg_sanity_check(ldb, message);
1576         if (ret != LDB_SUCCESS) {
1577                 return ret;
1578         }
1579
1580         ret = ldb_build_add_req(&req, ldb, ldb,
1581                                         message,
1582                                         NULL,
1583                                         NULL,
1584                                         ldb_op_default_callback,
1585                                         NULL);
1586         ldb_req_set_location(req, "ldb_add");
1587
1588         if (ret != LDB_SUCCESS) return ret;
1589
1590         /* do request and autostart a transaction */
1591         ret = ldb_autotransaction_request(ldb, req);
1592
1593         talloc_free(req);
1594         return ret;
1595 }
1596
1597 /*
1598   modify the specified attributes of a record
1599 */
1600 int ldb_modify(struct ldb_context *ldb,
1601                const struct ldb_message *message)
1602 {
1603         struct ldb_request *req;
1604         int ret;
1605
1606         ret = ldb_msg_sanity_check(ldb, message);
1607         if (ret != LDB_SUCCESS) {
1608                 return ret;
1609         }
1610
1611         ret = ldb_build_mod_req(&req, ldb, ldb,
1612                                         message,
1613                                         NULL,
1614                                         NULL,
1615                                         ldb_op_default_callback,
1616                                         NULL);
1617         ldb_req_set_location(req, "ldb_modify");
1618
1619         if (ret != LDB_SUCCESS) return ret;
1620
1621         /* do request and autostart a transaction */
1622         ret = ldb_autotransaction_request(ldb, req);
1623
1624         talloc_free(req);
1625         return ret;
1626 }
1627
1628
1629 /*
1630   delete a record from the database
1631 */
1632 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1633 {
1634         struct ldb_request *req;
1635         int ret;
1636
1637         ret = ldb_build_del_req(&req, ldb, ldb,
1638                                         dn,
1639                                         NULL,
1640                                         NULL,
1641                                         ldb_op_default_callback,
1642                                         NULL);
1643         ldb_req_set_location(req, "ldb_delete");
1644
1645         if (ret != LDB_SUCCESS) return ret;
1646
1647         /* do request and autostart a transaction */
1648         ret = ldb_autotransaction_request(ldb, req);
1649
1650         talloc_free(req);
1651         return ret;
1652 }
1653
1654 /*
1655   rename a record in the database
1656 */
1657 int ldb_rename(struct ldb_context *ldb,
1658                 struct ldb_dn *olddn, struct ldb_dn *newdn)
1659 {
1660         struct ldb_request *req;
1661         int ret;
1662
1663         ret = ldb_build_rename_req(&req, ldb, ldb,
1664                                         olddn,
1665                                         newdn,
1666                                         NULL,
1667                                         NULL,
1668                                         ldb_op_default_callback,
1669                                         NULL);
1670         ldb_req_set_location(req, "ldb_rename");
1671
1672         if (ret != LDB_SUCCESS) return ret;
1673
1674         /* do request and autostart a transaction */
1675         ret = ldb_autotransaction_request(ldb, req);
1676
1677         talloc_free(req);
1678         return ret;
1679 }
1680
1681
1682 /*
1683   return the global sequence number
1684 */
1685 int ldb_sequence_number(struct ldb_context *ldb,
1686                         enum ldb_sequence_type type, uint64_t *seq_num)
1687 {
1688         struct ldb_seqnum_request *seq;
1689         struct ldb_seqnum_result *seqr;
1690         struct ldb_result *res;
1691         TALLOC_CTX *tmp_ctx;
1692         int ret;
1693
1694         *seq_num = 0;
1695
1696         tmp_ctx = talloc_zero(ldb, struct ldb_request);
1697         if (tmp_ctx == NULL) {
1698                 ldb_set_errstring(ldb, "Out of Memory");
1699                 return LDB_ERR_OPERATIONS_ERROR;
1700         }
1701         seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1702         if (seq == NULL) {
1703                 ldb_set_errstring(ldb, "Out of Memory");
1704                 ret = LDB_ERR_OPERATIONS_ERROR;
1705                 goto done;
1706         }
1707         seq->type = type;
1708
1709         ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1710         if (ret != LDB_SUCCESS) {
1711                 goto done;
1712         }
1713         talloc_steal(tmp_ctx, res);
1714
1715         if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1716                 ldb_set_errstring(ldb, "Invalid OID in reply");
1717                 ret = LDB_ERR_OPERATIONS_ERROR;
1718                 goto done;
1719         }
1720         seqr = talloc_get_type(res->extended->data,
1721                                 struct ldb_seqnum_result);
1722         *seq_num = seqr->seq_num;
1723
1724 done:
1725         talloc_free(tmp_ctx);
1726         return ret;
1727 }
1728
1729 /*
1730   return extended error information
1731 */
1732 const char *ldb_errstring(struct ldb_context *ldb)
1733 {
1734         if (ldb->err_string) {
1735                 return ldb->err_string;
1736         }
1737
1738         return NULL;
1739 }
1740
1741 /*
1742   return a string explaining what a ldb error constant meancs
1743 */
1744 const char *ldb_strerror(int ldb_err)
1745 {
1746         switch (ldb_err) {
1747         case LDB_SUCCESS:
1748                 return "Success";
1749         case LDB_ERR_OPERATIONS_ERROR:
1750                 return "Operations error";
1751         case LDB_ERR_PROTOCOL_ERROR:
1752                 return "Protocol error";
1753         case LDB_ERR_TIME_LIMIT_EXCEEDED:
1754                 return "Time limit exceeded";
1755         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1756                 return "Size limit exceeded";
1757         case LDB_ERR_COMPARE_FALSE:
1758                 return "Compare false";
1759         case LDB_ERR_COMPARE_TRUE:
1760                 return "Compare true";
1761         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1762                 return "Auth method not supported";
1763         case LDB_ERR_STRONG_AUTH_REQUIRED:
1764                 return "Strong auth required";
1765 /* 9 RESERVED */
1766         case LDB_ERR_REFERRAL:
1767                 return "Referral error";
1768         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1769                 return "Admin limit exceeded";
1770         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1771                 return "Unsupported critical extension";
1772         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1773                 return "Confidentiality required";
1774         case LDB_ERR_SASL_BIND_IN_PROGRESS:
1775                 return "SASL bind in progress";
1776         case LDB_ERR_NO_SUCH_ATTRIBUTE:
1777                 return "No such attribute";
1778         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1779                 return "Undefined attribute type";
1780         case LDB_ERR_INAPPROPRIATE_MATCHING:
1781                 return "Inappropriate matching";
1782         case LDB_ERR_CONSTRAINT_VIOLATION:
1783                 return "Constraint violation";
1784         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1785                 return "Attribute or value exists";
1786         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1787                 return "Invalid attribute syntax";
1788 /* 22-31 unused */
1789         case LDB_ERR_NO_SUCH_OBJECT:
1790                 return "No such object";
1791         case LDB_ERR_ALIAS_PROBLEM:
1792                 return "Alias problem";
1793         case LDB_ERR_INVALID_DN_SYNTAX:
1794                 return "Invalid DN syntax";
1795 /* 35 RESERVED */
1796         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1797                 return "Alias dereferencing problem";
1798 /* 37-47 unused */
1799         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1800                 return "Inappropriate authentication";
1801         case LDB_ERR_INVALID_CREDENTIALS:
1802                 return "Invalid credentials";
1803         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1804                 return "insufficient access rights";
1805         case LDB_ERR_BUSY:
1806                 return "Busy";
1807         case LDB_ERR_UNAVAILABLE:
1808                 return "Unavailable";
1809         case LDB_ERR_UNWILLING_TO_PERFORM:
1810                 return "Unwilling to perform";
1811         case LDB_ERR_LOOP_DETECT:
1812                 return "Loop detect";
1813 /* 55-63 unused */
1814         case LDB_ERR_NAMING_VIOLATION:
1815                 return "Naming violation";
1816         case LDB_ERR_OBJECT_CLASS_VIOLATION:
1817                 return "Object class violation";
1818         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1819                 return "Not allowed on non-leaf";
1820         case LDB_ERR_NOT_ALLOWED_ON_RDN:
1821                 return "Not allowed on RDN";
1822         case LDB_ERR_ENTRY_ALREADY_EXISTS:
1823                 return "Entry already exists";
1824         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1825                 return "Object class mods prohibited";
1826 /* 70 RESERVED FOR CLDAP */
1827         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1828                 return "Affects multiple DSAs";
1829 /* 72-79 unused */
1830         case LDB_ERR_OTHER:
1831                 return "Other";
1832         }
1833
1834         return "Unknown error";
1835 }
1836
1837 /*
1838   set backend specific opaque parameters
1839 */
1840 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1841 {
1842         struct ldb_opaque *o;
1843
1844         /* allow updating an existing value */
1845         for (o=ldb->opaque;o;o=o->next) {
1846                 if (strcmp(o->name, name) == 0) {
1847                         o->value = value;
1848                         return LDB_SUCCESS;
1849                 }
1850         }
1851
1852         o = talloc(ldb, struct ldb_opaque);
1853         if (o == NULL) {
1854                 ldb_oom(ldb);
1855                 return LDB_ERR_OTHER;
1856         }
1857         o->next = ldb->opaque;
1858         o->name = name;
1859         o->value = value;
1860         ldb->opaque = o;
1861         return LDB_SUCCESS;
1862 }
1863
1864 /*
1865   get a previously set opaque value
1866 */
1867 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1868 {
1869         struct ldb_opaque *o;
1870         for (o=ldb->opaque;o;o=o->next) {
1871                 if (strcmp(o->name, name) == 0) {
1872                         return o->value;
1873                 }
1874         }
1875         return NULL;
1876 }
1877
1878 int ldb_global_init(void)
1879 {
1880         /* Provided for compatibility with some older versions of ldb */
1881         return 0;
1882 }
1883
1884 /* return the ldb flags */
1885 unsigned int ldb_get_flags(struct ldb_context *ldb)
1886 {
1887         return ldb->flags;
1888 }
1889
1890 /* set the ldb flags */
1891 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
1892 {
1893         ldb->flags = flags;
1894 }
1895
1896
1897 /*
1898   set the location in a ldb request. Used for debugging
1899  */
1900 void ldb_req_set_location(struct ldb_request *req, const char *location)
1901 {
1902         if (req && req->handle) {
1903                 req->handle->location = location;
1904         }
1905 }
1906
1907 /*
1908   return the location set with dsdb_req_set_location
1909  */
1910 const char *ldb_req_location(struct ldb_request *req)
1911 {
1912         return req->handle->location;
1913 }
1914
1915 /**
1916   mark a request as untrusted. This tells the rootdse module to remove
1917   unregistered controls
1918  */
1919 void ldb_req_mark_untrusted(struct ldb_request *req)
1920 {
1921         req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
1922 }
1923
1924 /**
1925   mark a request as trusted.
1926  */
1927 void ldb_req_mark_trusted(struct ldb_request *req)
1928 {
1929         req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
1930 }
1931
1932 /**
1933   set custom flags. Those flags are set by applications using ldb,
1934   they are application dependent and the same bit can have different
1935   meaning in different application.
1936  */
1937 void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
1938 {
1939         if (req != NULL && req->handle != NULL) {
1940                 req->handle->custom_flags = flags;
1941         }
1942 }
1943
1944
1945 /**
1946   get custom flags. Those flags are set by applications using ldb,
1947   they are application dependent and the same bit can have different
1948   meaning in different application.
1949  */
1950 uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
1951 {
1952         if (req != NULL && req->handle != NULL) {
1953                 return req->handle->custom_flags;
1954         }
1955
1956         /*
1957          * 0 is not something any better or worse than
1958          * anything else as req or the handle is NULL
1959          */
1960         return 0;
1961 }
1962
1963
1964 /**
1965    return true is a request is untrusted
1966  */
1967 bool ldb_req_is_untrusted(struct ldb_request *req)
1968 {
1969         return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;
1970 }