s4-ldb: expose ldb_transaction_prepare_commit() in ldb
[kamenim/samba.git] / source4 / 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
38 static int ldb_context_destructor(void *ptr)
39 {
40         struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
41
42         if (ldb->transaction_active) {
43                 ldb_debug(ldb, LDB_DEBUG_FATAL,
44                           "A transaction is still active in ldb context [%p]",
45                           ldb);
46         }
47
48         return 0;
49 }
50
51 /*
52   this is used to catch debug messages from events
53 */
54 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
55                              const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
56
57 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
58                              const char *fmt, va_list ap)
59 {
60         struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
61         enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
62         char *s = NULL;
63
64         switch (level) {
65         case TEVENT_DEBUG_FATAL:
66                 ldb_level = LDB_DEBUG_FATAL;
67                 break;
68         case TEVENT_DEBUG_ERROR:
69                 ldb_level = LDB_DEBUG_ERROR;
70                 break;
71         case TEVENT_DEBUG_WARNING:
72                 ldb_level = LDB_DEBUG_WARNING;
73                 break;
74         case TEVENT_DEBUG_TRACE:
75                 ldb_level = LDB_DEBUG_TRACE;
76                 break;
77         };
78
79         vasprintf(&s, fmt, ap);
80         if (!s) return;
81         ldb_debug(ldb, ldb_level, "tevent: %s", s);
82         free(s);
83 }
84
85 /*
86    initialise a ldb context
87    The mem_ctx is required
88    The event_ctx is required
89 */
90 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
91 {
92         struct ldb_context *ldb;
93         int ret;
94
95         ldb = talloc_zero(mem_ctx, struct ldb_context);
96         /* FIXME: Hack a new event context so that CMD line utilities work
97          * until we have them all converted */
98         if (ev_ctx == NULL) {
99                 ev_ctx = tevent_context_init(talloc_autofree_context());
100                 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
101                 tevent_loop_allow_nesting(ev_ctx);
102         }
103
104         ret = ldb_setup_wellknown_attributes(ldb);
105         if (ret != 0) {
106                 talloc_free(ldb);
107                 return NULL;
108         }
109
110         ldb_set_utf8_default(ldb);
111         ldb_set_create_perms(ldb, 0666);
112         ldb_set_modules_dir(ldb, LDB_MODULESDIR);
113         ldb_set_event_context(ldb, ev_ctx);
114
115         /* TODO: get timeout from options if available there */
116         ldb->default_timeout = 300; /* set default to 5 minutes */
117
118         talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
119
120         return ldb;
121 }
122
123 /*
124   try to autodetect a basedn if none specified. This fixes one of my
125   pet hates about ldapsearch, which is that you have to get a long,
126   complex basedn right to make any use of it.
127 */
128 void ldb_set_default_dns(struct ldb_context *ldb)
129 {
130         TALLOC_CTX *tmp_ctx;
131         int ret;
132         struct ldb_result *res;
133         struct ldb_dn *tmp_dn=NULL;
134         static const char *attrs[] = {
135                 "rootDomainNamingContext",
136                 "configurationNamingContext",
137                 "schemaNamingContext",
138                 "defaultNamingContext",
139                 NULL
140         };
141
142         tmp_ctx = talloc_new(ldb);
143         ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
144                          LDB_SCOPE_BASE, attrs, "(objectClass=*)");
145         if (ret != LDB_SUCCESS) {
146                 talloc_free(tmp_ctx);
147                 return;
148         }
149
150         if (res->count != 1) {
151                 talloc_free(tmp_ctx);
152                 return;
153         }
154
155         if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
156                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
157                                                  "rootDomainNamingContext");
158                 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
159         }
160
161         if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
162                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
163                                                  "configurationNamingContext");
164                 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
165         }
166
167         if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
168                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
169                                                  "schemaNamingContext");
170                 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
171         }
172
173         if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
174                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
175                                                  "defaultNamingContext");
176                 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
177         }
178
179         talloc_free(tmp_ctx);
180 }
181
182 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
183 {
184         void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
185         return talloc_get_type(opaque, struct ldb_dn);
186 }
187
188 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
189 {
190         void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
191         return talloc_get_type(opaque, struct ldb_dn);
192 }
193
194 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
195 {
196         void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
197         return talloc_get_type(opaque, struct ldb_dn);
198 }
199
200 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
201 {
202         void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
203         return talloc_get_type(opaque, struct ldb_dn);
204 }
205
206 /*
207    connect to a database. The URL can either be one of the following forms
208    ldb://path
209    ldapi://path
210
211    flags is made up of LDB_FLG_*
212
213    the options are passed uninterpreted to the backend, and are
214    backend specific
215 */
216 int ldb_connect(struct ldb_context *ldb, const char *url,
217                 unsigned int flags, const char *options[])
218 {
219         int ret;
220         const char *url2;
221         /* We seem to need to do this here, or else some utilities don't
222          * get ldb backends */
223
224         ldb->flags = flags;
225
226         url2 = talloc_strdup(ldb, url);
227         if (!url2) {
228                 ldb_oom(ldb);
229                 return LDB_ERR_OPERATIONS_ERROR;
230         }
231         ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
232         if (ret != LDB_SUCCESS) {
233                 return ret;
234         }
235
236         ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
237         if (ret != LDB_SUCCESS) {
238                 return ret;
239         }
240
241         if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
242                 ldb_debug(ldb, LDB_DEBUG_FATAL,
243                           "Unable to load modules for %s: %s",
244                           url, ldb_errstring(ldb));
245                 return LDB_ERR_OTHER;
246         }
247
248         /* set the default base dn */
249         ldb_set_default_dns(ldb);
250
251         return LDB_SUCCESS;
252 }
253
254 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
255 {
256         if (ldb->err_string) {
257                 talloc_free(ldb->err_string);
258         }
259         ldb->err_string = talloc_strdup(ldb, err_string);
260 }
261
262 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
263 {
264         va_list ap;
265         char *old_string = NULL;
266
267         if (ldb->err_string) {
268                 old_string = ldb->err_string;
269         }
270
271         va_start(ap, format);
272         ldb->err_string = talloc_vasprintf(ldb, format, ap);
273         va_end(ap);
274         talloc_free(old_string);
275 }
276
277 void ldb_reset_err_string(struct ldb_context *ldb)
278 {
279         if (ldb->err_string) {
280                 talloc_free(ldb->err_string);
281                 ldb->err_string = NULL;
282         }
283 }
284
285 #define FIRST_OP_NOERR(ldb, op) do { \
286         module = ldb->modules;                                  \
287         while (module && module->ops->op == NULL) module = module->next; \
288 } while (0)
289
290 #define FIRST_OP(ldb, op) do { \
291         FIRST_OP_NOERR(ldb, op); \
292         if (module == NULL) {                                   \
293                 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
294                 return LDB_ERR_OPERATIONS_ERROR;                        \
295         } \
296 } while (0)
297
298
299 /*
300   start a transaction
301 */
302 int ldb_transaction_start(struct ldb_context *ldb)
303 {
304         struct ldb_module *module;
305         int status;
306
307         ldb_debug(ldb, LDB_DEBUG_TRACE,
308                   "start ldb transaction (nesting: %d)",
309                   ldb->transaction_active);
310
311         /* explicit transaction active, count nested requests */
312         if (ldb->transaction_active) {
313                 ldb->transaction_active++;
314                 return LDB_SUCCESS;
315         }
316
317         /* start a new transaction */
318         ldb->transaction_active++;
319         ldb->prepare_commit_done = false;
320
321         FIRST_OP(ldb, start_transaction);
322
323         ldb_reset_err_string(ldb);
324
325         status = module->ops->start_transaction(module);
326         if (status != LDB_SUCCESS) {
327                 if (ldb->err_string == NULL) {
328                         /* no error string was setup by the backend */
329                         ldb_asprintf_errstring(ldb,
330                                 "ldb transaction start: %s (%d)",
331                                 ldb_strerror(status),
332                                 status);
333                 }
334         }
335         return status;
336 }
337
338 /*
339   prepare for transaction commit (first phase of two phase commit)
340 */
341 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
342 {
343         struct ldb_module *module;
344         int status;
345
346         if (ldb->prepare_commit_done) {
347                 return LDB_SUCCESS;
348         }
349
350         /* commit only when all nested transactions are complete */
351         if (ldb->transaction_active > 1) {
352                 return LDB_SUCCESS;
353         }
354
355         ldb->prepare_commit_done = true;
356
357         if (ldb->transaction_active < 0) {
358                 ldb_debug(ldb, LDB_DEBUG_FATAL,
359                           "prepare commit called but no ldb transactions are active!");
360                 ldb->transaction_active = 0;
361                 return LDB_ERR_OPERATIONS_ERROR;
362         }
363
364         /* call prepare transaction if available */
365         FIRST_OP_NOERR(ldb, prepare_commit);
366         if (module == NULL) {
367                 return LDB_SUCCESS;
368         }
369
370         status = module->ops->prepare_commit(module);
371         if (status != LDB_SUCCESS) {
372                 /* if a module fails the prepare then we need
373                    to call the end transaction for everyone */
374                 FIRST_OP(ldb, end_transaction);
375                 module->ops->end_transaction(module);
376                 if (ldb->err_string == NULL) {
377                         /* no error string was setup by the backend */
378                         ldb_asprintf_errstring(ldb,
379                                                "ldb transaction prepare commit: %s (%d)",
380                                                ldb_strerror(status),
381                                                status);
382                 }
383         }
384
385         return status;
386 }
387
388
389 /*
390   commit a transaction
391 */
392 int ldb_transaction_commit(struct ldb_context *ldb)
393 {
394         struct ldb_module *module;
395         int status;
396
397         status = ldb_transaction_prepare_commit(ldb);
398         if (status != LDB_SUCCESS) {
399                 return status;
400         }
401
402         ldb->transaction_active--;
403
404         ldb_debug(ldb, LDB_DEBUG_TRACE,
405                   "commit ldb transaction (nesting: %d)",
406                   ldb->transaction_active);
407
408         /* commit only when all nested transactions are complete */
409         if (ldb->transaction_active > 0) {
410                 return LDB_SUCCESS;
411         }
412
413         if (ldb->transaction_active < 0) {
414                 ldb_debug(ldb, LDB_DEBUG_FATAL,
415                           "commit called but no ldb transactions are active!");
416                 ldb->transaction_active = 0;
417                 return LDB_ERR_OPERATIONS_ERROR;
418         }
419
420         ldb_reset_err_string(ldb);
421
422         FIRST_OP(ldb, end_transaction);
423         status = module->ops->end_transaction(module);
424         if (status != LDB_SUCCESS) {
425                 if (ldb->err_string == NULL) {
426                         /* no error string was setup by the backend */
427                         ldb_asprintf_errstring(ldb,
428                                 "ldb transaction commit: %s (%d)",
429                                 ldb_strerror(status),
430                                 status);
431                 }
432                 /* cancel the transaction */
433                 FIRST_OP(ldb, del_transaction);
434                 module->ops->del_transaction(module);
435         }
436         return status;
437 }
438
439
440 /*
441   cancel a transaction
442 */
443 int ldb_transaction_cancel(struct ldb_context *ldb)
444 {
445         struct ldb_module *module;
446         int status;
447
448         ldb->transaction_active--;
449
450         ldb_debug(ldb, LDB_DEBUG_TRACE,
451                   "cancel ldb transaction (nesting: %d)",
452                   ldb->transaction_active);
453
454         /* really cancel only if all nested transactions are complete */
455         if (ldb->transaction_active > 0) {
456                 return LDB_SUCCESS;
457         }
458
459         if (ldb->transaction_active < 0) {
460                 ldb_debug(ldb, LDB_DEBUG_FATAL,
461                           "cancel called but no ldb transactions are active!");
462                 ldb->transaction_active = 0;
463                 return LDB_ERR_OPERATIONS_ERROR;
464         }
465
466         FIRST_OP(ldb, del_transaction);
467
468         status = module->ops->del_transaction(module);
469         if (status != LDB_SUCCESS) {
470                 if (ldb->err_string == NULL) {
471                         /* no error string was setup by the backend */
472                         ldb_asprintf_errstring(ldb,
473                                 "ldb transaction cancel: %s (%d)",
474                                 ldb_strerror(status),
475                                 status);
476                 }
477         }
478         return status;
479 }
480
481 /* autostarts a transacion if none active */
482 static int ldb_autotransaction_request(struct ldb_context *ldb,
483                                        struct ldb_request *req)
484 {
485         int ret;
486
487         ret = ldb_transaction_start(ldb);
488         if (ret != LDB_SUCCESS) {
489                 return ret;
490         }
491
492         ret = ldb_request(ldb, req);
493         if (ret == LDB_SUCCESS) {
494                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
495         }
496
497         if (ret == LDB_SUCCESS) {
498                 return ldb_transaction_commit(ldb);
499         }
500         ldb_transaction_cancel(ldb);
501
502         if (ldb->err_string == NULL) {
503                 /* no error string was setup by the backend */
504                 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
505         }
506
507         return ret;
508 }
509
510 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
511 {
512         struct tevent_context *ev;
513         int ret;
514
515         if (!handle) {
516                 return LDB_ERR_UNAVAILABLE;
517         }
518
519         if (handle->state == LDB_ASYNC_DONE) {
520                 return handle->status;
521         }
522
523         ev = ldb_get_event_context(handle->ldb);
524         if (NULL == ev) {
525                 return LDB_ERR_OPERATIONS_ERROR;
526         }
527
528         switch (type) {
529         case LDB_WAIT_NONE:
530                 ret = tevent_loop_once(ev);
531                 if (ret != 0) {
532                         return LDB_ERR_OPERATIONS_ERROR;
533                 }
534                 if (handle->state == LDB_ASYNC_DONE ||
535                     handle->status != LDB_SUCCESS) {
536                         return handle->status;
537                 }
538                 break;
539
540         case LDB_WAIT_ALL:
541                 while (handle->state != LDB_ASYNC_DONE) {
542                         ret = tevent_loop_once(ev);
543                         if (ret != 0) {
544                                 return LDB_ERR_OPERATIONS_ERROR;
545                         }
546                         if (handle->status != LDB_SUCCESS) {
547                                 return handle->status;
548                         }
549                 }
550                 return handle->status;
551         }
552
553         return LDB_SUCCESS;
554 }
555
556 /* set the specified timeout or, if timeout is 0 set the default timeout */
557 int ldb_set_timeout(struct ldb_context *ldb,
558                     struct ldb_request *req,
559                     int timeout)
560 {
561         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
562
563         if (timeout != 0) {
564                 req->timeout = timeout;
565         } else {
566                 req->timeout = ldb->default_timeout;
567         }
568         req->starttime = time(NULL);
569
570         return LDB_SUCCESS;
571 }
572
573 /* calculates the new timeout based on the previous starttime and timeout */
574 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
575                                   struct ldb_request *oldreq,
576                                   struct ldb_request *newreq)
577 {
578         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
579
580         if (oldreq == NULL) {
581                 return ldb_set_timeout(ldb, newreq, 0);
582         }
583
584         newreq->starttime = oldreq->starttime;
585         newreq->timeout = oldreq->timeout;
586
587         return LDB_SUCCESS;
588 }
589
590
591 /*
592    set the permissions for new files to be passed to open() in
593    backends that use local files
594  */
595 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
596 {
597         ldb->create_perms = perms;
598 }
599
600 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
601 {
602         return ldb->create_perms;
603 }
604
605 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
606 {
607         ldb->ev_ctx = ev;
608 }
609
610 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
611 {
612         return ldb->ev_ctx;
613 }
614
615 void ldb_request_set_state(struct ldb_request *req, int state)
616 {
617         req->handle->state = state;
618 }
619
620 int ldb_request_get_status(struct ldb_request *req)
621 {
622         return req->handle->status;
623 }
624
625 /*
626   start an ldb request
627   NOTE: the request must be a talloc context.
628   returns LDB_ERR_* on errors.
629 */
630 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
631 {
632         struct ldb_module *module;
633         int ret;
634
635         if (req->callback == NULL) {
636                 ldb_set_errstring(ldb, "Requests MUST define callbacks");
637                 return LDB_ERR_UNWILLING_TO_PERFORM;
638         }
639
640         ldb_reset_err_string(ldb);
641
642         /* call the first module in the chain */
643         switch (req->operation) {
644         case LDB_SEARCH:
645                 FIRST_OP(ldb, search);
646                 ret = module->ops->search(module, req);
647                 break;
648         case LDB_ADD:
649                 FIRST_OP(ldb, add);
650                 ret = module->ops->add(module, req);
651                 break;
652         case LDB_MODIFY:
653                 FIRST_OP(ldb, modify);
654                 ret = module->ops->modify(module, req);
655                 break;
656         case LDB_DELETE:
657                 FIRST_OP(ldb, del);
658                 ret = module->ops->del(module, req);
659                 break;
660         case LDB_RENAME:
661                 FIRST_OP(ldb, rename);
662                 ret = module->ops->rename(module, req);
663                 break;
664         case LDB_EXTENDED:
665                 FIRST_OP(ldb, extended);
666                 ret = module->ops->extended(module, req);
667                 break;
668         default:
669                 FIRST_OP(ldb, request);
670                 ret = module->ops->request(module, req);
671                 break;
672         }
673
674         return ret;
675 }
676
677 int ldb_request_done(struct ldb_request *req, int status)
678 {
679         req->handle->state = LDB_ASYNC_DONE;
680         req->handle->status = status;
681         return status;
682 }
683
684 /*
685   search the database given a LDAP-like search expression
686
687   returns an LDB error code
688
689   Use talloc_free to free the ldb_message returned in 'res', if successful
690
691 */
692 int ldb_search_default_callback(struct ldb_request *req,
693                                 struct ldb_reply *ares)
694 {
695         struct ldb_result *res;
696         int n;
697
698         res = talloc_get_type(req->context, struct ldb_result);
699
700         if (!ares) {
701                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
702         }
703         if (ares->error != LDB_SUCCESS) {
704                 return ldb_request_done(req, ares->error);
705         }
706
707         switch (ares->type) {
708         case LDB_REPLY_ENTRY:
709                 res->msgs = talloc_realloc(res, res->msgs,
710                                         struct ldb_message *, res->count + 2);
711                 if (! res->msgs) {
712                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
713                 }
714
715                 res->msgs[res->count + 1] = NULL;
716
717                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
718                 res->count++;
719                 break;
720
721         case LDB_REPLY_REFERRAL:
722                 if (res->refs) {
723                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
724                 } else {
725                         n = 0;
726                 }
727
728                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
729                 if (! res->refs) {
730                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
731                 }
732
733                 res->refs[n] = talloc_move(res->refs, &ares->referral);
734                 res->refs[n + 1] = NULL;
735                 break;
736
737         case LDB_REPLY_DONE:
738                 /* TODO: we should really support controls on entries
739                  * and referrals too! */
740                 res->controls = talloc_move(res, &ares->controls);
741
742                 /* this is the last message, and means the request is done */
743                 /* we have to signal and eventual ldb_wait() waiting that the
744                  * async request operation was completed */
745                 talloc_free(ares);
746                 return ldb_request_done(req, LDB_SUCCESS);
747         }
748
749         talloc_free(ares);
750
751         return LDB_SUCCESS;
752 }
753
754 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
755 {
756         int ret;
757
758         if (!ares) {
759                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
760         }
761
762         if (ares->error != LDB_SUCCESS) {
763                 ret = ares->error;
764                 talloc_free(ares);
765                 return ldb_request_done(req, ret);
766         }
767
768         if (ares->type != LDB_REPLY_DONE) {
769                 talloc_free(ares);
770                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
771                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
772         }
773
774         talloc_free(ares);
775         return ldb_request_done(req, LDB_SUCCESS);
776 }
777
778 int ldb_build_search_req_ex(struct ldb_request **ret_req,
779                         struct ldb_context *ldb,
780                         void *mem_ctx,
781                         struct ldb_dn *base,
782                         enum ldb_scope scope,
783                         struct ldb_parse_tree *tree,
784                         const char * const *attrs,
785                         struct ldb_control **controls,
786                         void *context,
787                         ldb_request_callback_t callback,
788                         struct ldb_request *parent)
789 {
790         struct ldb_request *req;
791
792         *ret_req = NULL;
793
794         req = talloc(mem_ctx, struct ldb_request);
795         if (req == NULL) {
796                 ldb_oom(ldb);
797                 return LDB_ERR_OPERATIONS_ERROR;
798         }
799
800         req->operation = LDB_SEARCH;
801         if (base == NULL) {
802                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
803         } else {
804                 req->op.search.base = base;
805         }
806         req->op.search.scope = scope;
807
808         req->op.search.tree = tree;
809         if (req->op.search.tree == NULL) {
810                 ldb_set_errstring(ldb, "'tree' can't be NULL");
811                 talloc_free(req);
812                 return LDB_ERR_OPERATIONS_ERROR;
813         }
814
815         req->op.search.attrs = attrs;
816         req->controls = controls;
817         req->context = context;
818         req->callback = callback;
819
820         ldb_set_timeout_from_prev_req(ldb, parent, req);
821
822         req->handle = ldb_handle_new(req, ldb);
823         if (req->handle == NULL) {
824                 ldb_oom(ldb);
825                 return LDB_ERR_OPERATIONS_ERROR;
826         }
827
828         *ret_req = req;
829         return LDB_SUCCESS;
830 }
831
832 int ldb_build_search_req(struct ldb_request **ret_req,
833                         struct ldb_context *ldb,
834                         void *mem_ctx,
835                         struct ldb_dn *base,
836                         enum ldb_scope scope,
837                         const char *expression,
838                         const char * const *attrs,
839                         struct ldb_control **controls,
840                         void *context,
841                         ldb_request_callback_t callback,
842                         struct ldb_request *parent)
843 {
844         struct ldb_parse_tree *tree;
845         int ret;
846
847         tree = ldb_parse_tree(mem_ctx, expression);
848         if (tree == NULL) {
849                 ldb_set_errstring(ldb, "Unable to parse search expression");
850                 return LDB_ERR_OPERATIONS_ERROR;
851         }
852
853         ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
854                                       scope, tree, attrs, controls,
855                                       context, callback, parent);
856         if (ret == LDB_SUCCESS) {
857                 talloc_steal(*ret_req, tree);
858         }
859         return ret;
860 }
861
862 int ldb_build_add_req(struct ldb_request **ret_req,
863                         struct ldb_context *ldb,
864                         void *mem_ctx,
865                         const struct ldb_message *message,
866                         struct ldb_control **controls,
867                         void *context,
868                         ldb_request_callback_t callback,
869                         struct ldb_request *parent)
870 {
871         struct ldb_request *req;
872
873         *ret_req = NULL;
874
875         req = talloc(mem_ctx, struct ldb_request);
876         if (req == NULL) {
877                 ldb_set_errstring(ldb, "Out of Memory");
878                 return LDB_ERR_OPERATIONS_ERROR;
879         }
880
881         req->operation = LDB_ADD;
882         req->op.add.message = message;
883         req->controls = controls;
884         req->context = context;
885         req->callback = callback;
886
887         ldb_set_timeout_from_prev_req(ldb, parent, req);
888
889         req->handle = ldb_handle_new(req, ldb);
890         if (req->handle == NULL) {
891                 ldb_oom(ldb);
892                 return LDB_ERR_OPERATIONS_ERROR;
893         }
894
895         *ret_req = req;
896
897         return LDB_SUCCESS;
898 }
899
900 int ldb_build_mod_req(struct ldb_request **ret_req,
901                         struct ldb_context *ldb,
902                         void *mem_ctx,
903                         const struct ldb_message *message,
904                         struct ldb_control **controls,
905                         void *context,
906                         ldb_request_callback_t callback,
907                         struct ldb_request *parent)
908 {
909         struct ldb_request *req;
910
911         *ret_req = NULL;
912
913         req = talloc(mem_ctx, struct ldb_request);
914         if (req == NULL) {
915                 ldb_set_errstring(ldb, "Out of Memory");
916                 return LDB_ERR_OPERATIONS_ERROR;
917         }
918
919         req->operation = LDB_MODIFY;
920         req->op.mod.message = message;
921         req->controls = controls;
922         req->context = context;
923         req->callback = callback;
924
925         ldb_set_timeout_from_prev_req(ldb, parent, req);
926
927         req->handle = ldb_handle_new(req, ldb);
928         if (req->handle == NULL) {
929                 ldb_oom(ldb);
930                 return LDB_ERR_OPERATIONS_ERROR;
931         }
932
933         *ret_req = req;
934
935         return LDB_SUCCESS;
936 }
937
938 int ldb_build_del_req(struct ldb_request **ret_req,
939                         struct ldb_context *ldb,
940                         void *mem_ctx,
941                         struct ldb_dn *dn,
942                         struct ldb_control **controls,
943                         void *context,
944                         ldb_request_callback_t callback,
945                         struct ldb_request *parent)
946 {
947         struct ldb_request *req;
948
949         *ret_req = NULL;
950
951         req = talloc(mem_ctx, struct ldb_request);
952         if (req == NULL) {
953                 ldb_set_errstring(ldb, "Out of Memory");
954                 return LDB_ERR_OPERATIONS_ERROR;
955         }
956
957         req->operation = LDB_DELETE;
958         req->op.del.dn = dn;
959         req->controls = controls;
960         req->context = context;
961         req->callback = callback;
962
963         ldb_set_timeout_from_prev_req(ldb, parent, req);
964
965         req->handle = ldb_handle_new(req, ldb);
966         if (req->handle == NULL) {
967                 ldb_oom(ldb);
968                 return LDB_ERR_OPERATIONS_ERROR;
969         }
970
971         *ret_req = req;
972
973         return LDB_SUCCESS;
974 }
975
976 int ldb_build_rename_req(struct ldb_request **ret_req,
977                         struct ldb_context *ldb,
978                         void *mem_ctx,
979                         struct ldb_dn *olddn,
980                         struct ldb_dn *newdn,
981                         struct ldb_control **controls,
982                         void *context,
983                         ldb_request_callback_t callback,
984                         struct ldb_request *parent)
985 {
986         struct ldb_request *req;
987
988         *ret_req = NULL;
989
990         req = talloc(mem_ctx, struct ldb_request);
991         if (req == NULL) {
992                 ldb_set_errstring(ldb, "Out of Memory");
993                 return LDB_ERR_OPERATIONS_ERROR;
994         }
995
996         req->operation = LDB_RENAME;
997         req->op.rename.olddn = olddn;
998         req->op.rename.newdn = newdn;
999         req->controls = controls;
1000         req->context = context;
1001         req->callback = callback;
1002
1003         ldb_set_timeout_from_prev_req(ldb, parent, req);
1004
1005         req->handle = ldb_handle_new(req, ldb);
1006         if (req->handle == NULL) {
1007                 ldb_oom(ldb);
1008                 return LDB_ERR_OPERATIONS_ERROR;
1009         }
1010
1011         *ret_req = req;
1012
1013         return LDB_SUCCESS;
1014 }
1015
1016 int ldb_extended_default_callback(struct ldb_request *req,
1017                                   struct ldb_reply *ares)
1018 {
1019         struct ldb_result *res;
1020
1021         res = talloc_get_type(req->context, struct ldb_result);
1022
1023         if (!ares) {
1024                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1025         }
1026         if (ares->error != LDB_SUCCESS) {
1027                 return ldb_request_done(req, ares->error);
1028         }
1029
1030         if (ares->type == LDB_REPLY_DONE) {
1031
1032                 /* TODO: we should really support controls on entries and referrals too! */
1033                 res->extended = talloc_move(res, &ares->response);
1034                 res->controls = talloc_move(res, &ares->controls);
1035
1036                 talloc_free(ares);
1037                 return ldb_request_done(req, LDB_SUCCESS);
1038         }
1039
1040         talloc_free(ares);
1041         ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1042         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1043 }
1044
1045 int ldb_build_extended_req(struct ldb_request **ret_req,
1046                            struct ldb_context *ldb,
1047                            void *mem_ctx,
1048                            const char *oid,
1049                            void *data,
1050                            struct ldb_control **controls,
1051                            void *context,
1052                            ldb_request_callback_t callback,
1053                            struct ldb_request *parent)
1054 {
1055         struct ldb_request *req;
1056
1057         *ret_req = NULL;
1058
1059         req = talloc(mem_ctx, struct ldb_request);
1060         if (req == NULL) {
1061                 ldb_set_errstring(ldb, "Out of Memory");
1062                 return LDB_ERR_OPERATIONS_ERROR;
1063         }
1064
1065         req->operation = LDB_EXTENDED;
1066         req->op.extended.oid = oid;
1067         req->op.extended.data = data;
1068         req->controls = controls;
1069         req->context = context;
1070         req->callback = callback;
1071
1072         ldb_set_timeout_from_prev_req(ldb, parent, req);
1073
1074         req->handle = ldb_handle_new(req, ldb);
1075         if (req->handle == NULL) {
1076                 ldb_oom(ldb);
1077                 return LDB_ERR_OPERATIONS_ERROR;
1078         }
1079
1080         *ret_req = req;
1081
1082         return LDB_SUCCESS;
1083 }
1084
1085 int ldb_extended(struct ldb_context *ldb,
1086                  const char *oid,
1087                  void *data,
1088                  struct ldb_result **_res)
1089 {
1090         struct ldb_request *req;
1091         int ret;
1092         struct ldb_result *res;
1093
1094         *_res = NULL;
1095
1096         res = talloc_zero(ldb, struct ldb_result);
1097         if (!res) {
1098                 return LDB_ERR_OPERATIONS_ERROR;
1099         }
1100
1101         ret = ldb_build_extended_req(&req, ldb, ldb,
1102                                      oid, data, NULL,
1103                                      res, ldb_extended_default_callback,
1104                                      NULL);
1105         if (ret != LDB_SUCCESS) goto done;
1106
1107         ldb_set_timeout(ldb, req, 0); /* use default timeout */
1108
1109         ret = ldb_request(ldb, req);
1110
1111         if (ret == LDB_SUCCESS) {
1112                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1113         }
1114
1115         talloc_free(req);
1116
1117 done:
1118         if (ret != LDB_SUCCESS) {
1119                 talloc_free(res);
1120         }
1121
1122         *_res = res;
1123         return ret;
1124 }
1125
1126 /*
1127   note that ldb_search() will automatically replace a NULL 'base' value
1128   with the defaultNamingContext from the rootDSE if available.
1129 */
1130 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1131                 struct ldb_result **result, struct ldb_dn *base,
1132                 enum ldb_scope scope, const char * const *attrs,
1133                 const char *exp_fmt, ...)
1134 {
1135         struct ldb_request *req;
1136         struct ldb_result *res;
1137         char *expression;
1138         va_list ap;
1139         int ret;
1140
1141         expression = NULL;
1142         *result = NULL;
1143         req = NULL;
1144
1145         res = talloc_zero(mem_ctx, struct ldb_result);
1146         if (!res) {
1147                 return LDB_ERR_OPERATIONS_ERROR;
1148         }
1149
1150         if (exp_fmt) {
1151                 va_start(ap, exp_fmt);
1152                 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1153                 va_end(ap);
1154
1155                 if (!expression) {
1156                         talloc_free(res);
1157                         return LDB_ERR_OPERATIONS_ERROR;
1158                 }
1159         }
1160
1161         ret = ldb_build_search_req(&req, ldb, mem_ctx,
1162                                         base?base:ldb_get_default_basedn(ldb),
1163                                         scope,
1164                                         expression,
1165                                         attrs,
1166                                         NULL,
1167                                         res,
1168                                         ldb_search_default_callback,
1169                                         NULL);
1170
1171         if (ret != LDB_SUCCESS) goto done;
1172
1173         ret = ldb_request(ldb, req);
1174
1175         if (ret == LDB_SUCCESS) {
1176                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1177         }
1178
1179 done:
1180         if (ret != LDB_SUCCESS) {
1181                 talloc_free(res);
1182                 res = NULL;
1183         }
1184
1185         talloc_free(expression);
1186         talloc_free(req);
1187
1188         *result = res;
1189         return ret;
1190 }
1191
1192 /*
1193   add a record to the database. Will fail if a record with the given class
1194   and key already exists
1195 */
1196 int ldb_add(struct ldb_context *ldb,
1197             const struct ldb_message *message)
1198 {
1199         struct ldb_request *req;
1200         int ret;
1201
1202         ret = ldb_msg_sanity_check(ldb, message);
1203         if (ret != LDB_SUCCESS) {
1204                 return ret;
1205         }
1206
1207         ret = ldb_build_add_req(&req, ldb, ldb,
1208                                         message,
1209                                         NULL,
1210                                         NULL,
1211                                         ldb_op_default_callback,
1212                                         NULL);
1213
1214         if (ret != LDB_SUCCESS) return ret;
1215
1216         /* do request and autostart a transaction */
1217         ret = ldb_autotransaction_request(ldb, req);
1218
1219         talloc_free(req);
1220         return ret;
1221 }
1222
1223 /*
1224   modify the specified attributes of a record
1225 */
1226 int ldb_modify(struct ldb_context *ldb,
1227                const struct ldb_message *message)
1228 {
1229         struct ldb_request *req;
1230         int ret;
1231
1232         ret = ldb_msg_sanity_check(ldb, message);
1233         if (ret != LDB_SUCCESS) {
1234                 return ret;
1235         }
1236
1237         ret = ldb_build_mod_req(&req, ldb, ldb,
1238                                         message,
1239                                         NULL,
1240                                         NULL,
1241                                         ldb_op_default_callback,
1242                                         NULL);
1243
1244         if (ret != LDB_SUCCESS) return ret;
1245
1246         /* do request and autostart a transaction */
1247         ret = ldb_autotransaction_request(ldb, req);
1248
1249         talloc_free(req);
1250         return ret;
1251 }
1252
1253
1254 /*
1255   delete a record from the database
1256 */
1257 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1258 {
1259         struct ldb_request *req;
1260         int ret;
1261
1262         ret = ldb_build_del_req(&req, ldb, ldb,
1263                                         dn,
1264                                         NULL,
1265                                         NULL,
1266                                         ldb_op_default_callback,
1267                                         NULL);
1268
1269         if (ret != LDB_SUCCESS) return ret;
1270
1271         /* do request and autostart a transaction */
1272         ret = ldb_autotransaction_request(ldb, req);
1273
1274         talloc_free(req);
1275         return ret;
1276 }
1277
1278 /*
1279   rename a record in the database
1280 */
1281 int ldb_rename(struct ldb_context *ldb,
1282                 struct ldb_dn *olddn, struct ldb_dn *newdn)
1283 {
1284         struct ldb_request *req;
1285         int ret;
1286
1287         ret = ldb_build_rename_req(&req, ldb, ldb,
1288                                         olddn,
1289                                         newdn,
1290                                         NULL,
1291                                         NULL,
1292                                         ldb_op_default_callback,
1293                                         NULL);
1294
1295         if (ret != LDB_SUCCESS) return ret;
1296
1297         /* do request and autostart a transaction */
1298         ret = ldb_autotransaction_request(ldb, req);
1299
1300         talloc_free(req);
1301         return ret;
1302 }
1303
1304
1305 /*
1306   return the global sequence number
1307 */
1308 int ldb_sequence_number(struct ldb_context *ldb,
1309                         enum ldb_sequence_type type, uint64_t *seq_num)
1310 {
1311         struct ldb_seqnum_request *seq;
1312         struct ldb_seqnum_result *seqr;
1313         struct ldb_result *res;
1314         TALLOC_CTX *tmp_ctx;
1315         int ret;
1316
1317         *seq_num = 0;
1318
1319         tmp_ctx = talloc_zero(ldb, struct ldb_request);
1320         if (tmp_ctx == NULL) {
1321                 ldb_set_errstring(ldb, "Out of Memory");
1322                 return LDB_ERR_OPERATIONS_ERROR;
1323         }
1324         seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1325         if (seq == NULL) {
1326                 ldb_set_errstring(ldb, "Out of Memory");
1327                 ret = LDB_ERR_OPERATIONS_ERROR;
1328                 goto done;
1329         }
1330         seq->type = type;
1331
1332         ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1333         if (ret != LDB_SUCCESS) {
1334                 goto done;
1335         }
1336         talloc_steal(tmp_ctx, res);
1337
1338         if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1339                 ldb_set_errstring(ldb, "Invalid OID in reply");
1340                 ret = LDB_ERR_OPERATIONS_ERROR;
1341                 goto done;
1342         }
1343         seqr = talloc_get_type(res->extended->data,
1344                                 struct ldb_seqnum_result);
1345         *seq_num = seqr->seq_num;
1346
1347 done:
1348         talloc_free(tmp_ctx);
1349         return ret;
1350 }
1351
1352 /*
1353   return extended error information
1354 */
1355 const char *ldb_errstring(struct ldb_context *ldb)
1356 {
1357         if (ldb->err_string) {
1358                 return ldb->err_string;
1359         }
1360
1361         return NULL;
1362 }
1363
1364 /*
1365   return a string explaining what a ldb error constant meancs
1366 */
1367 const char *ldb_strerror(int ldb_err)
1368 {
1369         switch (ldb_err) {
1370         case LDB_SUCCESS:
1371                 return "Success";
1372         case LDB_ERR_OPERATIONS_ERROR:
1373                 return "Operations error";
1374         case LDB_ERR_PROTOCOL_ERROR:
1375                 return "Protocol error";
1376         case LDB_ERR_TIME_LIMIT_EXCEEDED:
1377                 return "Time limit exceeded";
1378         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1379                 return "Size limit exceeded";
1380         case LDB_ERR_COMPARE_FALSE:
1381                 return "Compare false";
1382         case LDB_ERR_COMPARE_TRUE:
1383                 return "Compare true";
1384         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1385                 return "Auth method not supported";
1386         case LDB_ERR_STRONG_AUTH_REQUIRED:
1387                 return "Strong auth required";
1388 /* 9 RESERVED */
1389         case LDB_ERR_REFERRAL:
1390                 return "Referral error";
1391         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1392                 return "Admin limit exceeded";
1393         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1394                 return "Unsupported critical extension";
1395         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1396                 return "Confidentiality required";
1397         case LDB_ERR_SASL_BIND_IN_PROGRESS:
1398                 return "SASL bind in progress";
1399         case LDB_ERR_NO_SUCH_ATTRIBUTE:
1400                 return "No such attribute";
1401         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1402                 return "Undefined attribute type";
1403         case LDB_ERR_INAPPROPRIATE_MATCHING:
1404                 return "Inappropriate matching";
1405         case LDB_ERR_CONSTRAINT_VIOLATION:
1406                 return "Constraint violation";
1407         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1408                 return "Attribute or value exists";
1409         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1410                 return "Invalid attribute syntax";
1411 /* 22-31 unused */
1412         case LDB_ERR_NO_SUCH_OBJECT:
1413                 return "No such object";
1414         case LDB_ERR_ALIAS_PROBLEM:
1415                 return "Alias problem";
1416         case LDB_ERR_INVALID_DN_SYNTAX:
1417                 return "Invalid DN syntax";
1418 /* 35 RESERVED */
1419         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1420                 return "Alias dereferencing problem";
1421 /* 37-47 unused */
1422         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1423                 return "Inappropriate authentication";
1424         case LDB_ERR_INVALID_CREDENTIALS:
1425                 return "Invalid credentials";
1426         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1427                 return "insufficient access rights";
1428         case LDB_ERR_BUSY:
1429                 return "Busy";
1430         case LDB_ERR_UNAVAILABLE:
1431                 return "Unavailable";
1432         case LDB_ERR_UNWILLING_TO_PERFORM:
1433                 return "Unwilling to perform";
1434         case LDB_ERR_LOOP_DETECT:
1435                 return "Loop detect";
1436 /* 55-63 unused */
1437         case LDB_ERR_NAMING_VIOLATION:
1438                 return "Naming violation";
1439         case LDB_ERR_OBJECT_CLASS_VIOLATION:
1440                 return "Object class violation";
1441         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1442                 return "Not allowed on non-leaf";
1443         case LDB_ERR_NOT_ALLOWED_ON_RDN:
1444                 return "Not allowed on RDN";
1445         case LDB_ERR_ENTRY_ALREADY_EXISTS:
1446                 return "Entry already exists";
1447         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1448                 return "Object class mods prohibited";
1449 /* 70 RESERVED FOR CLDAP */
1450         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1451                 return "Affects multiple DSAs";
1452 /* 72-79 unused */
1453         case LDB_ERR_OTHER:
1454                 return "Other";
1455         }
1456
1457         return "Unknown error";
1458 }
1459
1460 /*
1461   set backend specific opaque parameters
1462 */
1463 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1464 {
1465         struct ldb_opaque *o;
1466
1467         /* allow updating an existing value */
1468         for (o=ldb->opaque;o;o=o->next) {
1469                 if (strcmp(o->name, name) == 0) {
1470                         o->value = value;
1471                         return LDB_SUCCESS;
1472                 }
1473         }
1474
1475         o = talloc(ldb, struct ldb_opaque);
1476         if (o == NULL) {
1477                 ldb_oom(ldb);
1478                 return LDB_ERR_OTHER;
1479         }
1480         o->next = ldb->opaque;
1481         o->name = name;
1482         o->value = value;
1483         ldb->opaque = o;
1484         return LDB_SUCCESS;
1485 }
1486
1487 /*
1488   get a previously set opaque value
1489 */
1490 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1491 {
1492         struct ldb_opaque *o;
1493         for (o=ldb->opaque;o;o=o->next) {
1494                 if (strcmp(o->name, name) == 0) {
1495                         return o->value;
1496                 }
1497         }
1498         return NULL;
1499 }
1500
1501 int ldb_global_init(void)
1502 {
1503         /* Provided for compatibility with some older versions of ldb */
1504         return 0;
1505 }
1506
1507 /* return the ldb flags */
1508 unsigned int ldb_get_flags(struct ldb_context *ldb)
1509 {
1510         return ldb->flags;
1511 }