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