LCOV - code coverage report
Current view: top level - lib/ldb/common - ldb.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 794 1050 75.6 %
Date: 2024-02-28 12:06:22 Functions: 67 73 91.8 %

          Line data    Source code
       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      709339 : static int ldb_context_destructor(void *ptr)
      40             : {
      41      709339 :         struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
      42             : 
      43      709339 :         if (ldb->transaction_active) {
      44          21 :                 ldb_debug(ldb, LDB_DEBUG_FATAL,
      45             :                           "A transaction is still active in ldb context [%p] on %s",
      46          21 :                           ldb, (const char *)ldb_get_opaque(ldb, "ldb_url"));
      47             :         }
      48             : 
      49      709339 :         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  1026650492 : static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
      59             :                              const char *fmt, va_list ap)
      60             : {
      61  1026650492 :         struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
      62  1026650492 :         enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
      63             : 
      64  1026650492 :         switch (level) {
      65           0 :         case TEVENT_DEBUG_FATAL:
      66           0 :                 ldb_level = LDB_DEBUG_FATAL;
      67           0 :                 break;
      68           0 :         case TEVENT_DEBUG_ERROR:
      69           0 :                 ldb_level = LDB_DEBUG_ERROR;
      70           0 :                 break;
      71           0 :         case TEVENT_DEBUG_WARNING:
      72           0 :                 ldb_level = LDB_DEBUG_WARNING;
      73           0 :                 break;
      74   997836510 :         case TEVENT_DEBUG_TRACE:
      75   997836510 :                 ldb_level = LDB_DEBUG_TRACE;
      76   997836510 :                 break;
      77    28813982 :         };
      78             : 
      79             :         /* There isn't a tevent: prefix here because to add it means
      80             :          * actually printing the string, and most of the time we don't
      81             :          * want to show it */
      82  1026650492 :         ldb_vdebug(ldb, ldb_level, fmt, ap);
      83  1026650492 : }
      84             : 
      85             : /*
      86             :    initialise a ldb context
      87             :    The mem_ctx is required
      88             :    The event_ctx is required
      89             : */
      90      748381 : struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
      91             : {
      92       13279 :         struct ldb_context *ldb;
      93       13279 :         int ret;
      94      748381 :         const char *modules_path = getenv("LDB_MODULES_PATH");
      95             : 
      96      748381 :         if (modules_path == NULL) {
      97      744465 :                 modules_path = LDB_MODULESDIR;
      98             :         }
      99             : 
     100      748381 :         ret = ldb_modules_load(modules_path, LDB_VERSION);
     101      748381 :         if (ret != LDB_SUCCESS) {
     102           0 :                 return NULL;
     103             :         }
     104             : 
     105      748381 :         ldb = talloc_zero(mem_ctx, struct ldb_context);
     106      748381 :         if (ldb == NULL) {
     107           0 :                 return NULL;
     108             :         }
     109             : 
     110             :         /* A new event context so that callers who don't want ldb
     111             :          * operating on their global event context can work without
     112             :          * having to provide their own private one explicitly */
     113      748381 :         if (ev_ctx == NULL) {
     114      318303 :                 ev_ctx = tevent_context_init(ldb);
     115      318303 :                 if (ev_ctx == NULL) {
     116           0 :                         talloc_free(ldb);
     117           0 :                         return NULL;
     118             :                 }
     119      318303 :                 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
     120      318303 :                 tevent_set_max_debug_level(ev_ctx, TEVENT_DEBUG_TRACE);
     121      318303 :                 tevent_loop_allow_nesting(ev_ctx);
     122             :         }
     123             : 
     124      748381 :         ret = ldb_setup_wellknown_attributes(ldb);
     125      748381 :         if (ret != LDB_SUCCESS) {
     126           0 :                 talloc_free(ldb);
     127           0 :                 return NULL;
     128             :         }
     129             : 
     130      748381 :         ldb_set_utf8_default(ldb);
     131      748381 :         ldb_set_create_perms(ldb, 0666);
     132      748381 :         ldb_set_modules_dir(ldb, LDB_MODULESDIR);
     133      748381 :         ldb_set_event_context(ldb, ev_ctx);
     134      748381 :         ret = ldb_register_extended_match_rules(ldb);
     135      748381 :         if (ret != LDB_SUCCESS) {
     136           0 :                 talloc_free(ldb);
     137           0 :                 return NULL;
     138             :         }
     139             : 
     140             :         /* TODO: get timeout from options if available there */
     141      748381 :         ldb->default_timeout = 300; /* set default to 5 minutes */
     142             : 
     143      748381 :         talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
     144             : 
     145      748381 :         return ldb;
     146             : }
     147             : 
     148             : /*
     149             :   try to autodetect a basedn if none specified. This fixes one of my
     150             :   pet hates about ldapsearch, which is that you have to get a long,
     151             :   complex basedn right to make any use of it.
     152             : */
     153     1147081 : void ldb_set_default_dns(struct ldb_context *ldb)
     154             : {
     155       20058 :         TALLOC_CTX *tmp_ctx;
     156       20058 :         int ret;
     157       20058 :         struct ldb_result *res;
     158     1147081 :         struct ldb_dn *tmp_dn=NULL;
     159       20058 :         static const char *attrs[] = {
     160             :                 "rootDomainNamingContext",
     161             :                 "configurationNamingContext",
     162             :                 "schemaNamingContext",
     163             :                 "defaultNamingContext",
     164             :                 NULL
     165             :         };
     166             : 
     167     1147081 :         tmp_ctx = talloc_new(ldb);
     168     1147081 :         ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
     169             :                          LDB_SCOPE_BASE, attrs, "(objectClass=*)");
     170     1147081 :         if (ret != LDB_SUCCESS) {
     171      318539 :                 talloc_free(tmp_ctx);
     172      324589 :                 return;
     173             :         }
     174             : 
     175      828542 :         if (res->count != 1) {
     176           6 :                 talloc_free(tmp_ctx);
     177           6 :                 return;
     178             :         }
     179             : 
     180      828536 :         if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
     181      427195 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     182             :                                                  "rootDomainNamingContext");
     183      427195 :                 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
     184             :         }
     185             : 
     186      828536 :         if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
     187      427195 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     188             :                                                  "configurationNamingContext");
     189      427195 :                 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
     190             :         }
     191             : 
     192      828536 :         if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
     193      427195 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     194             :                                                  "schemaNamingContext");
     195      427195 :                 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
     196             :         }
     197             : 
     198      828536 :         if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
     199      427195 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     200             :                                                  "defaultNamingContext");
     201      427195 :                 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
     202             :         }
     203             : 
     204      828536 :         talloc_free(tmp_ctx);
     205             : }
     206             : 
     207     1752366 : struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
     208             : {
     209     1752366 :         void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
     210     1752366 :         return talloc_get_type(opaque, struct ldb_dn);
     211             : }
     212             : 
     213     5296605 : struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
     214             : {
     215     5296605 :         void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
     216     5296605 :         return talloc_get_type(opaque, struct ldb_dn);
     217             : }
     218             : 
     219     4054021 : struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
     220             : {
     221     4054021 :         void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
     222     4054021 :         return talloc_get_type(opaque, struct ldb_dn);
     223             : }
     224             : 
     225     7509924 : struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
     226             : {
     227     7509924 :         void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
     228     7509924 :         return talloc_get_type(opaque, struct ldb_dn);
     229             : }
     230             : 
     231             : /*
     232             :    connect to a database. The URL can either be one of the following forms
     233             :    ldb://path
     234             :    ldapi://path
     235             : 
     236             :    flags is made up of LDB_FLG_*
     237             : 
     238             :    the options are passed uninterpreted to the backend, and are
     239             :    backend specific
     240             : */
     241      747376 : int ldb_connect(struct ldb_context *ldb, const char *url,
     242             :                 unsigned int flags, const char *options[])
     243             : {
     244       13120 :         int ret;
     245       13120 :         char *url2;
     246             :         /* We seem to need to do this here, or else some utilities don't
     247             :          * get ldb backends */
     248             : 
     249      747376 :         ldb->flags = flags;
     250             : 
     251      747376 :         url2 = talloc_strdup(ldb, url);
     252      747376 :         if (!url2) {
     253           0 :                 ldb_oom(ldb);
     254           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     255             :         }
     256      747376 :         ret = ldb_set_opaque(ldb, "ldb_url", url2);
     257      747376 :         if (ret != LDB_SUCCESS) {
     258           0 :                 return ret;
     259             :         }
     260             : 
     261             :         /*
     262             :          * Take a copy of the options.
     263             :          */
     264      747376 :         ldb->options = ldb_options_copy(ldb, options);
     265      747376 :         if (ldb->options == NULL && options != NULL) {
     266           0 :                 ldb_oom(ldb);
     267           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     268             :         }
     269             : 
     270      747376 :         ret = ldb_module_connect_backend(ldb, url, options, &ldb->modules);
     271      747376 :         if (ret != LDB_SUCCESS) {
     272        1625 :                 return ret;
     273             :         }
     274             : 
     275      745747 :         ret = ldb_load_modules(ldb, options);
     276      745747 :         if (ret != LDB_SUCCESS) {
     277           7 :                 ldb_debug(ldb, LDB_DEBUG_FATAL,
     278             :                           "Unable to load modules for %s: %s",
     279             :                           url, ldb_errstring(ldb));
     280           7 :                 return ret;
     281             :         }
     282             : 
     283             :         /* set the default base dn */
     284      745740 :         ldb_set_default_dns(ldb);
     285             : 
     286      745740 :         return LDB_SUCCESS;
     287             : }
     288             : 
     289     1268203 : void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
     290             : {
     291     1268203 :         ldb_asprintf_errstring(ldb, "%s", err_string);
     292     1268203 : }
     293             : 
     294     4845490 : void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
     295             : {
     296      480203 :         va_list ap;
     297     4845490 :         char *old_err_string = NULL;
     298     4845490 :         if (ldb->err_string) {
     299      302931 :                 old_err_string = ldb->err_string;
     300             :         }
     301             : 
     302     4845490 :         va_start(ap, format);
     303     4845490 :         ldb->err_string = talloc_vasprintf(ldb, format, ap);
     304     4845490 :         va_end(ap);
     305             : 
     306     4845490 :         TALLOC_FREE(old_err_string);
     307             : 
     308     4845490 :         if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
     309           0 :                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_asprintf/set_errstring: %s",
     310             :                           ldb->err_string);
     311             :         }
     312     4845490 : }
     313             : 
     314    59223518 : void ldb_reset_err_string(struct ldb_context *ldb)
     315             : {
     316    59223518 :         TALLOC_FREE(ldb->err_string);
     317    59223518 : }
     318             : 
     319             : 
     320             : 
     321             : /*
     322             :   set an ldb error based on file:line
     323             : */
     324      471537 : int ldb_error_at(struct ldb_context *ldb, int ecode,
     325             :                  const char *reason, const char *file, int line)
     326             : {
     327      471537 :         if (reason == NULL) {
     328           0 :                 reason = ldb_strerror(ecode);
     329             :         }
     330      471537 :         ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
     331      471537 :         return ecode;
     332             : }
     333             : 
     334             : 
     335             : #define FIRST_OP_NOERR(ldb, op) do { \
     336             :         next_module = ldb->modules;                                  \
     337             :         while (next_module && next_module->ops->op == NULL) {             \
     338             :                 next_module = next_module->next;                         \
     339             :         };                                                          \
     340             :         if ((ldb->flags & LDB_FLG_ENABLE_TRACING) && next_module) { \
     341             :                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: (%s)->" #op, \
     342             :                           next_module->ops->name);                                \
     343             :         }                                                               \
     344             : } while (0)
     345             : 
     346             : #define FIRST_OP(ldb, op) do { \
     347             :         FIRST_OP_NOERR(ldb, op); \
     348             :         if (next_module == NULL) {                                      \
     349             :                 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
     350             :                 return LDB_ERR_OPERATIONS_ERROR;                        \
     351             :         } \
     352             : } while (0)
     353             : 
     354             : 
     355             : /*
     356             :   start a transaction
     357             : */
     358     1922966 : int ldb_transaction_start(struct ldb_context *ldb)
     359             : {
     360      120263 :         struct ldb_module *next_module;
     361      120263 :         int status;
     362             : 
     363     1922966 :         ldb_debug(ldb, LDB_DEBUG_TRACE,
     364             :                   "start ldb transaction (nesting: %d)",
     365             :                   ldb->transaction_active);
     366             : 
     367             :         /* explicit transaction active, count nested requests */
     368     1922966 :         if (ldb->transaction_active) {
     369      826960 :                 ldb->transaction_active++;
     370      826960 :                 return LDB_SUCCESS;
     371             :         }
     372             : 
     373             :         /* start a new transaction */
     374     1096006 :         ldb->transaction_active++;
     375     1096006 :         ldb->prepare_commit_done = false;
     376             : 
     377     2385649 :         FIRST_OP(ldb, start_transaction);
     378             : 
     379     1096006 :         ldb_reset_err_string(ldb);
     380             : 
     381     1096006 :         status = next_module->ops->start_transaction(next_module);
     382     1096006 :         if (status != LDB_SUCCESS) {
     383          22 :                 if (ldb->err_string == NULL) {
     384             :                         /* no error string was setup by the backend */
     385          20 :                         ldb_asprintf_errstring(ldb,
     386             :                                 "ldb transaction start: %s (%d)",
     387             :                                 ldb_strerror(status),
     388             :                                 status);
     389          20 :                 ldb->transaction_active--;
     390             :                 }
     391          22 :                 if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
     392           0 :                         ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "start ldb transaction error: %s",
     393             :                                   ldb_errstring(next_module->ldb));
     394             :                 }
     395             :         } else {
     396     1095984 :                 if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
     397           0 :                         ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "start ldb transaction success");
     398             :                 }
     399             :         }
     400     1085231 :         return status;
     401             : }
     402             : 
     403             : /*
     404             :   prepare for transaction commit (first phase of two phase commit)
     405             : */
     406     1788607 : int ldb_transaction_prepare_commit(struct ldb_context *ldb)
     407             : {
     408      119427 :         struct ldb_module *next_module;
     409      119427 :         int status;
     410             : 
     411     1788607 :         if (ldb->prepare_commit_done) {
     412        2535 :                 return LDB_SUCCESS;
     413             :         }
     414             : 
     415             :         /* commit only when all nested transactions are complete */
     416     1786068 :         if (ldb->transaction_active > 1) {
     417      717983 :                 return LDB_SUCCESS;
     418             :         }
     419             : 
     420      958620 :         ldb->prepare_commit_done = true;
     421             : 
     422      958620 :         if (ldb->transaction_active < 0) {
     423           0 :                 ldb_debug(ldb, LDB_DEBUG_FATAL,
     424             :                           "prepare commit called but no ldb transactions are active!");
     425           0 :                 ldb->transaction_active = 0;
     426           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     427             :         }
     428             : 
     429             :         /* call prepare transaction if available */
     430     8265741 :         FIRST_OP_NOERR(ldb, prepare_commit);
     431      958620 :         if (next_module == NULL) {
     432      153484 :                 return LDB_SUCCESS;
     433             :         }
     434             : 
     435      804920 :         ldb_reset_err_string(ldb);
     436             : 
     437      804920 :         status = next_module->ops->prepare_commit(next_module);
     438      804920 :         if (status != LDB_SUCCESS) {
     439          10 :                 ldb->transaction_active--;
     440             :                 /* if a next_module fails the prepare then we need
     441             :                    to call the end transaction for everyone */
     442          12 :                 FIRST_OP(ldb, del_transaction);
     443          10 :                 next_module->ops->del_transaction(next_module);
     444          10 :                 if (ldb->err_string == NULL) {
     445             :                         /* no error string was setup by the backend */
     446           0 :                         ldb_asprintf_errstring(ldb,
     447             :                                                "ldb transaction prepare commit: %s (%d)",
     448             :                                                ldb_strerror(status),
     449             :                                                status);
     450             :                 }
     451          10 :                 if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
     452           0 :                         ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "prepare commit transaction error: %s",
     453             :                                   ldb_errstring(next_module->ldb));
     454             :                 }
     455             :         }
     456             : 
     457      795178 :         return status;
     458             : }
     459             : 
     460             : 
     461             : /*
     462             :   commit a transaction
     463             : */
     464     1784772 : int ldb_transaction_commit(struct ldb_context *ldb)
     465             : {
     466      119423 :         struct ldb_module *next_module;
     467      119423 :         int status;
     468             : 
     469     1784772 :         status = ldb_transaction_prepare_commit(ldb);
     470     1784772 :         if (status != LDB_SUCCESS) {
     471          10 :                 return status;
     472             :         }
     473             : 
     474     1784762 :         ldb->transaction_active--;
     475             : 
     476     1784762 :         ldb_debug(ldb, LDB_DEBUG_TRACE,
     477             :                   "commit ldb transaction (nesting: %d)",
     478             :                   ldb->transaction_active);
     479             : 
     480             :         /* commit only when all nested transactions are complete */
     481     1784762 :         if (ldb->transaction_active > 0) {
     482      716691 :                 return LDB_SUCCESS;
     483             :         }
     484             : 
     485      958606 :         if (ldb->transaction_active < 0) {
     486           0 :                 ldb_debug(ldb, LDB_DEBUG_FATAL,
     487             :                           "commit called but no ldb transactions are active!");
     488           0 :                 ldb->transaction_active = 0;
     489           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     490             :         }
     491             : 
     492      958606 :         ldb_reset_err_string(ldb);
     493             : 
     494     2143642 :         FIRST_OP(ldb, end_transaction);
     495      958606 :         status = next_module->ops->end_transaction(next_module);
     496      958606 :         if (status != LDB_SUCCESS) {
     497           3 :                 if (ldb->err_string == NULL) {
     498             :                         /* no error string was setup by the backend */
     499           0 :                         ldb_asprintf_errstring(ldb,
     500             :                                 "ldb transaction commit: %s (%d)",
     501             :                                 ldb_strerror(status),
     502             :                                 status);
     503             :                 }
     504           3 :                 if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
     505           0 :                         ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "commit ldb transaction error: %s",
     506             :                                   ldb_errstring(next_module->ldb));
     507             :                 }
     508             :         }
     509      948648 :         return status;
     510             : }
     511             : 
     512             : 
     513             : /*
     514             :   cancel a transaction
     515             : */
     516      138151 : int ldb_transaction_cancel(struct ldb_context *ldb)
     517             : {
     518         838 :         struct ldb_module *next_module;
     519         838 :         int status;
     520             : 
     521      138151 :         ldb->transaction_active--;
     522             : 
     523      138151 :         ldb_debug(ldb, LDB_DEBUG_TRACE,
     524             :                   "cancel ldb transaction (nesting: %d)",
     525             :                   ldb->transaction_active);
     526             : 
     527             :         /* really cancel only if all nested transactions are complete */
     528      138151 :         if (ldb->transaction_active > 0) {
     529         781 :                 return LDB_SUCCESS;
     530             :         }
     531             : 
     532      137347 :         if (ldb->transaction_active < 0) {
     533           0 :                 ldb_debug(ldb, LDB_DEBUG_FATAL,
     534             :                           "cancel called but no ldb transactions are active!");
     535           0 :                 ldb->transaction_active = 0;
     536           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     537             :         }
     538             : 
     539      241908 :         FIRST_OP(ldb, del_transaction);
     540             : 
     541      137347 :         status = next_module->ops->del_transaction(next_module);
     542      137347 :         if (status != LDB_SUCCESS) {
     543           0 :                 if (ldb->err_string == NULL) {
     544             :                         /* no error string was setup by the backend */
     545           0 :                         ldb_asprintf_errstring(ldb,
     546             :                                 "ldb transaction cancel: %s (%d)",
     547             :                                 ldb_strerror(status),
     548             :                                 status);
     549             :                 }
     550           0 :                 if ((next_module && next_module->ldb->flags & LDB_FLG_ENABLE_TRACING)) {
     551           0 :                         ldb_debug(next_module->ldb, LDB_DEBUG_TRACE, "cancel ldb transaction error: %s",
     552             :                                   ldb_errstring(next_module->ldb));
     553             :                 }
     554             :         }
     555      136532 :         return status;
     556             : }
     557             : 
     558             : /*
     559             :   cancel a transaction with no error if no transaction is pending
     560             :   used when we fork() to clear any parent transactions
     561             : */
     562           0 : int ldb_transaction_cancel_noerr(struct ldb_context *ldb)
     563             : {
     564           0 :         if (ldb->transaction_active > 0) {
     565           0 :                 return ldb_transaction_cancel(ldb);
     566             :         }
     567           0 :         return LDB_SUCCESS;
     568             : }
     569             : 
     570             : 
     571             : /* autostarts a transaction if none active */
     572      417277 : static int ldb_autotransaction_request(struct ldb_context *ldb,
     573             :                                        struct ldb_request *req)
     574             : {
     575       11430 :         int ret;
     576             : 
     577      417277 :         ret = ldb_transaction_start(ldb);
     578      417277 :         if (ret != LDB_SUCCESS) {
     579          20 :                 return ret;
     580             :         }
     581             : 
     582      417257 :         ret = ldb_request(ldb, req);
     583      417257 :         if (ret == LDB_SUCCESS) {
     584      417126 :                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
     585             :         }
     586             : 
     587      417257 :         if (ret == LDB_SUCCESS) {
     588      373189 :                 return ldb_transaction_commit(ldb);
     589             :         }
     590       44068 :         ldb_transaction_cancel(ldb);
     591             : 
     592       44068 :         return ret;
     593             : }
     594             : 
     595   112730068 : int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
     596             : {
     597     3734207 :         struct tevent_context *ev;
     598     3734207 :         int ret;
     599             : 
     600   112730068 :         if (handle == NULL) {
     601           0 :                 return LDB_ERR_UNAVAILABLE;
     602             :         }
     603             : 
     604   112730068 :         if (handle->state == LDB_ASYNC_DONE) {
     605    13869377 :                 if ((handle->status != LDB_SUCCESS) &&
     606           1 :                     (handle->ldb->err_string == NULL)) {
     607             :                         /* if no error string was setup by the backend */
     608           0 :                         ldb_asprintf_errstring(handle->ldb,
     609             :                                                "ldb_wait from %s with LDB_ASYNC_DONE: %s (%d)",
     610             :                                                handle->location,
     611             :                                                ldb_strerror(handle->status),
     612             :                                                handle->status);
     613             :                 }
     614    13869377 :                 return handle->status;
     615             :         }
     616             : 
     617    98860691 :         ev = ldb_handle_get_event_context(handle);
     618    98860691 :         if (NULL == ev) {
     619           0 :                 return ldb_oom(handle->ldb);
     620             :         }
     621             : 
     622    98860691 :         switch (type) {
     623     3470787 :         case LDB_WAIT_NONE:
     624     3470787 :                 ret = tevent_loop_once(ev);
     625     3470787 :                 if (ret != 0) {
     626           0 :                         return ldb_operr(handle->ldb);
     627             :                 }
     628     3470787 :                 if (handle->status == LDB_SUCCESS) {
     629     3469421 :                         return LDB_SUCCESS;
     630             :                 }
     631        1366 :                 if (handle->ldb->err_string != NULL) {
     632        1357 :                         return handle->status;
     633             :                 }
     634             :                 /*
     635             :                  * if no error string was setup by the backend
     636             :                  */
     637           9 :                 ldb_asprintf_errstring(handle->ldb,
     638             :                                        "ldb_wait from %s with LDB_WAIT_NONE: %s (%d)",
     639             :                                        handle->location,
     640             :                                        ldb_strerror(handle->status),
     641             :                                        handle->status);
     642           9 :                 return handle->status;
     643             : 
     644    91980620 :         case LDB_WAIT_ALL:
     645   300237806 :                 while (handle->state != LDB_ASYNC_DONE) {
     646   208746122 :                         ret = tevent_loop_once(ev);
     647   208746110 :                         if (ret != 0) {
     648           0 :                                 return ldb_operr(handle->ldb);
     649             :                         }
     650   208746110 :                         if (handle->status != LDB_SUCCESS) {
     651     3898208 :                                 if (handle->ldb->err_string != NULL) {
     652     3418545 :                                         return handle->status;
     653             :                                 }
     654             :                                 /*
     655             :                                  * if no error string was setup by the
     656             :                                  * backend
     657             :                                  */
     658       33766 :                                 ldb_asprintf_errstring(handle->ldb,
     659             :                                                        "ldb_wait from %s with "
     660             :                                                        "LDB_WAIT_ALL: %s (%d)",
     661             :                                                        handle->location,
     662             :                                                        ldb_strerror(handle->status),
     663             :                                                        handle->status);
     664       33766 :                                 return handle->status;
     665             :                         }
     666             :                 }
     667    91491684 :                 if (handle->status == LDB_SUCCESS) {
     668    88529141 :                         return LDB_SUCCESS;
     669             :                 }
     670           0 :                 if (handle->ldb->err_string != NULL) {
     671           0 :                         return handle->status;
     672             :                 }
     673             :                 /*
     674             :                  * if no error string was setup by the backend
     675             :                  */
     676           0 :                 ldb_asprintf_errstring(handle->ldb,
     677             :                                        "ldb_wait from %s with LDB_WAIT_ALL,"
     678             :                                        " LDB_ASYNC_DONE: %s (%d)",
     679             :                                        handle->location,
     680             :                                        ldb_strerror(handle->status),
     681             :                                        handle->status);
     682           0 :                 return handle->status;
     683             :         }
     684             : 
     685           0 :         return LDB_SUCCESS;
     686             : }
     687             : 
     688             : /* set the specified timeout or, if timeout is 0 set the default timeout */
     689    82434007 : int ldb_set_timeout(struct ldb_context *ldb,
     690             :                     struct ldb_request *req,
     691             :                     int timeout)
     692             : {
     693    82434007 :         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
     694             : 
     695    82434007 :         if (timeout != 0) {
     696      364809 :                 req->timeout = timeout;
     697             :         } else {
     698    82069198 :                 req->timeout = ldb->default_timeout;
     699             :         }
     700    82434007 :         req->starttime = time(NULL);
     701             : 
     702    82434007 :         return LDB_SUCCESS;
     703             : }
     704             : 
     705             : /* calculates the new timeout based on the previous starttime and timeout */
     706   588539597 : int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
     707             :                                   struct ldb_request *oldreq,
     708             :                                   struct ldb_request *newreq)
     709             : {
     710   588539597 :         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
     711             : 
     712   588539597 :         if (oldreq == NULL) {
     713    69202439 :                 return ldb_set_timeout(ldb, newreq, 0);
     714             :         }
     715             : 
     716   519337158 :         newreq->starttime = oldreq->starttime;
     717   519337158 :         newreq->timeout = oldreq->timeout;
     718             : 
     719   519337158 :         return LDB_SUCCESS;
     720             : }
     721             : 
     722             : 
     723    80610917 : struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
     724             : {
     725     3080050 :         struct ldb_handle *h;
     726             : 
     727    80610917 :         h = talloc_zero(mem_ctx, struct ldb_handle);
     728    80610917 :         if (h == NULL) {
     729           0 :                 ldb_set_errstring(ldb, "Out of Memory");
     730           0 :                 return NULL;
     731             :         }
     732             : 
     733    80610917 :         h->status = LDB_SUCCESS;
     734    80610917 :         h->state = LDB_ASYNC_INIT;
     735    80610917 :         h->ldb = ldb;
     736    80610917 :         h->flags = 0;
     737    80610917 :         h->location = NULL;
     738    80610917 :         h->parent = NULL;
     739             : 
     740    80610917 :         if (h->ldb->require_private_event_context == true) {
     741    80044545 :                 h->event_context = tevent_context_init(h);
     742    80044545 :                 if (h->event_context == NULL) {
     743           0 :                         ldb_set_errstring(ldb,
     744             :                                           "Out of Memory allocating "
     745             :                                           "event context for new handle");
     746           0 :                         return NULL;
     747             :                 }
     748    80044545 :                 tevent_set_debug(h->event_context, ldb_tevent_debug, ldb);
     749    80044545 :                 tevent_set_max_debug_level(h->event_context, TEVENT_DEBUG_TRACE);
     750    80044545 :                 tevent_loop_allow_nesting(h->event_context);
     751             :         }
     752             : 
     753    77530867 :         return h;
     754             : }
     755             : 
     756   519337158 : static struct ldb_handle *ldb_handle_new_child(TALLOC_CTX *mem_ctx,
     757             :                                                struct ldb_request *parent_req)
     758             : {
     759    18340905 :         struct ldb_handle *h;
     760             : 
     761   519337158 :         h = talloc_zero(mem_ctx, struct ldb_handle);
     762   519337158 :         if (h == NULL) {
     763           0 :                 ldb_set_errstring(parent_req->handle->ldb,
     764             :                                   "Out of Memory");
     765           0 :                 return NULL;
     766             :         }
     767             : 
     768   519337158 :         h->status = LDB_SUCCESS;
     769   519337158 :         h->state = LDB_ASYNC_INIT;
     770   519337158 :         h->ldb = parent_req->handle->ldb;
     771   519337158 :         h->parent = parent_req;
     772   519337158 :         h->nesting = parent_req->handle->nesting + 1;
     773   519337158 :         h->flags = parent_req->handle->flags;
     774   519337158 :         h->custom_flags = parent_req->handle->custom_flags;
     775   519337158 :         h->event_context = parent_req->handle->event_context;
     776             : 
     777   519337158 :         return h;
     778             : }
     779             : 
     780             : /*
     781             :    set the permissions for new files to be passed to open() in
     782             :    backends that use local files
     783             :  */
     784     1489065 : void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
     785             : {
     786     1489065 :         ldb->create_perms = perms;
     787     1489065 : }
     788             : 
     789     2158019 : unsigned int ldb_get_create_perms(struct ldb_context *ldb)
     790             : {
     791     2158019 :         return ldb->create_perms;
     792             : }
     793             : 
     794     1765195 : void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
     795             : {
     796     1765195 :         ldb->ev_ctx = ev;
     797     1765195 : }
     798             : 
     799   272151100 : struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
     800             : {
     801   272151100 :         return ldb->ev_ctx;
     802             : }
     803             : 
     804   204176294 : void ldb_request_set_state(struct ldb_request *req, int state)
     805             : {
     806   204176294 :         req->handle->state = state;
     807   204176294 : }
     808             : 
     809   203505970 : int ldb_request_get_status(struct ldb_request *req)
     810             : {
     811   203505970 :         return req->handle->status;
     812             : }
     813             : 
     814             : /*
     815             :  * This function obtains the private event context for the handle,
     816             :  * which may have been created to avoid nested event loops during
     817             :  * ldb_tdb with the locks held
     818             :  */
     819   302471713 : struct tevent_context *ldb_handle_get_event_context(struct ldb_handle *handle)
     820             : {
     821   302471713 :         if (handle->event_context != NULL) {
     822   289265425 :                 return handle->event_context;
     823             :         }
     824     4034695 :         return ldb_get_event_context(handle->ldb);
     825             : }
     826             : 
     827             : /*
     828             :  * This function forces a specific ldb handle to use the global event
     829             :  * context.  This allows a nested event loop to operate, so any open
     830             :  * transaction also needs to be aborted.
     831             :  *
     832             :  * Any events on this event context will be lost
     833             :  *
     834             :  * This is used in Samba when sending an IRPC to another part of the
     835             :  * same process instead of making a local DB modification.
     836             :  */
     837          46 : void ldb_handle_use_global_event_context(struct ldb_handle *handle)
     838             : {
     839          46 :         TALLOC_FREE(handle->event_context);
     840          46 : }
     841             : 
     842     2659087 : void ldb_set_require_private_event_context(struct ldb_context *ldb)
     843             : {
     844     2659087 :         ldb->require_private_event_context = true;
     845     2659087 : }
     846             : 
     847             : /*
     848             :   trace a ldb request
     849             : */
     850           0 : static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
     851             : {
     852           0 :         TALLOC_CTX *tmp_ctx = talloc_new(req);
     853           0 :         unsigned int i;
     854           0 :         struct ldb_ldif ldif;
     855             : 
     856           0 :         switch (req->operation) {
     857           0 :         case LDB_SEARCH:
     858           0 :                 ldb_debug_add(ldb, "ldb_trace_request: SEARCH\n");
     859           0 :                 ldb_debug_add(ldb, " dn: %s\n",
     860           0 :                               ldb_dn_is_null(req->op.search.base)?"<rootDSE>":
     861           0 :                               ldb_dn_get_linearized(req->op.search.base));
     862           0 :                 ldb_debug_add(ldb, " scope: %s\n",
     863           0 :                           req->op.search.scope==LDB_SCOPE_BASE?"base":
     864           0 :                           req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
     865           0 :                           req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
     866           0 :                 ldb_debug_add(ldb, " expr: %s\n",
     867           0 :                           ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
     868           0 :                 if (req->op.search.attrs == NULL) {
     869           0 :                         ldb_debug_add(ldb, " attr: <ALL>\n");
     870             :                 } else {
     871           0 :                         for (i=0; req->op.search.attrs[i]; i++) {
     872           0 :                                 ldb_debug_add(ldb, " attr: %s\n", req->op.search.attrs[i]);
     873             :                         }
     874             :                 }
     875           0 :                 break;
     876           0 :         case LDB_DELETE:
     877           0 :                 ldb_debug_add(ldb, "ldb_trace_request: DELETE\n");
     878           0 :                 ldb_debug_add(ldb, " dn: %s\n",
     879             :                               ldb_dn_get_linearized(req->op.del.dn));
     880           0 :                 break;
     881           0 :         case LDB_RENAME:
     882           0 :                 ldb_debug_add(ldb, "ldb_trace_request: RENAME\n");
     883           0 :                 ldb_debug_add(ldb, " olddn: %s\n",
     884             :                               ldb_dn_get_linearized(req->op.rename.olddn));
     885           0 :                 ldb_debug_add(ldb, " newdn: %s\n",
     886             :                               ldb_dn_get_linearized(req->op.rename.newdn));
     887           0 :                 break;
     888           0 :         case LDB_EXTENDED:
     889           0 :                 ldb_debug_add(ldb, "ldb_trace_request: EXTENDED\n");
     890           0 :                 ldb_debug_add(ldb, " oid: %s\n", req->op.extended.oid);
     891           0 :                 ldb_debug_add(ldb, " data: %s\n", req->op.extended.data?"yes":"no");
     892           0 :                 break;
     893           0 :         case LDB_ADD:
     894           0 :                 ldif.changetype = LDB_CHANGETYPE_ADD;
     895           0 :                 ldif.msg = discard_const_p(struct ldb_message, req->op.add.message);
     896             : 
     897           0 :                 ldb_debug_add(ldb, "ldb_trace_request: ADD\n");
     898             : 
     899             :                 /*
     900             :                  * The choice to call
     901             :                  * ldb_ldif_write_redacted_trace_string() is CRITICAL
     902             :                  * for security.  It ensures that we do not output
     903             :                  * passwords into debug logs
     904             :                  */
     905             : 
     906           0 :                 ldb_debug_add(req->handle->ldb, "%s\n",
     907           0 :                               ldb_ldif_write_redacted_trace_string(req->handle->ldb, tmp_ctx, &ldif));
     908           0 :                 break;
     909           0 :         case LDB_MODIFY:
     910           0 :                 ldif.changetype = LDB_CHANGETYPE_MODIFY;
     911           0 :                 ldif.msg = discard_const_p(struct ldb_message, req->op.mod.message);
     912             : 
     913           0 :                 ldb_debug_add(ldb, "ldb_trace_request: MODIFY\n");
     914             : 
     915             :                 /*
     916             :                  * The choice to call
     917             :                  * ldb_ldif_write_redacted_trace_string() is CRITICAL
     918             :                  * for security.  It ensures that we do not output
     919             :                  * passwords into debug logs
     920             :                  */
     921             : 
     922           0 :                 ldb_debug_add(req->handle->ldb, "%s\n",
     923           0 :                               ldb_ldif_write_redacted_trace_string(req->handle->ldb, tmp_ctx, &ldif));
     924           0 :                 break;
     925           0 :         case LDB_REQ_REGISTER_CONTROL:
     926           0 :                 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_CONTROL\n");
     927           0 :                 ldb_debug_add(req->handle->ldb, "%s\n",
     928             :                               req->op.reg_control.oid);
     929           0 :                 break;
     930           0 :         case LDB_REQ_REGISTER_PARTITION:
     931           0 :                 ldb_debug_add(ldb, "ldb_trace_request: REGISTER_PARTITION\n");
     932           0 :                 ldb_debug_add(req->handle->ldb, "%s\n",
     933             :                               ldb_dn_get_linearized(req->op.reg_partition.dn));
     934           0 :                 break;
     935           0 :         default:
     936           0 :                 ldb_debug_add(ldb, "ldb_trace_request: UNKNOWN(%u)\n",
     937           0 :                               req->operation);
     938           0 :                 break;
     939             :         }
     940             : 
     941           0 :         if (req->controls == NULL) {
     942           0 :                 ldb_debug_add(ldb, " control: <NONE>\n");
     943             :         } else {
     944           0 :                 for (i=0; req->controls && req->controls[i]; i++) {
     945           0 :                         if (req->controls[i]->oid) {
     946           0 :                                 ldb_debug_add(ldb, " control: %s  crit:%u  data:%s\n",
     947           0 :                                               req->controls[i]->oid,
     948           0 :                                               req->controls[i]->critical,
     949           0 :                                               req->controls[i]->data?"yes":"no");
     950             :                         }
     951             :                 }
     952             :         }
     953             : 
     954           0 :         ldb_debug_end(ldb, LDB_DEBUG_TRACE);
     955             : 
     956           0 :         talloc_free(tmp_ctx);
     957           0 : }
     958             : 
     959             : /*
     960             :   check that the element flags don't have any internal bits set
     961             :  */
     962     1516887 : static int ldb_msg_check_element_flags(struct ldb_context *ldb,
     963             :                                        const struct ldb_message *message)
     964             : {
     965      114232 :         unsigned i;
     966     8800411 :         for (i=0; i<message->num_elements; i++) {
     967     7283524 :                 if (message->elements[i].flags & LDB_FLAG_INTERNAL_MASK) {
     968           0 :                         ldb_asprintf_errstring(ldb, "Invalid element flags 0x%08x on element %s in %s\n",
     969           0 :                                                message->elements[i].flags, message->elements[i].name,
     970           0 :                                                ldb_dn_get_linearized(message->dn));
     971           0 :                         return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
     972             :                 }
     973             :         }
     974     1402655 :         return LDB_SUCCESS;
     975             : }
     976             : 
     977             : /*
     978             :  * This context allows us to make the unlock be a talloc destructor
     979             :  *
     980             :  * This ensures that a request started, but not waited on, will still
     981             :  * unlock.
     982             :  */
     983             : struct ldb_db_lock_context {
     984             :         struct ldb_request *req;
     985             :         struct ldb_context *ldb;
     986             : };
     987             : 
     988             : /*
     989             :  * We have to have the unlock on a destructor so that we unlock the
     990             :  * DB if a caller calls talloc_free(req).  We trust that the ldb
     991             :  * context has not already gone away.
     992             :  */
     993    28748033 : static int ldb_db_lock_destructor(struct ldb_db_lock_context *lock_context)
     994             : {
     995     1323368 :         int ret;
     996     1323368 :         struct ldb_module *next_module;
     997   132567351 :         FIRST_OP_NOERR(lock_context->ldb, read_unlock);
     998    28748033 :         if (next_module != NULL) {
     999    28748033 :                 ret = next_module->ops->read_unlock(next_module);
    1000             :         } else {
    1001           0 :                 ret = LDB_SUCCESS;
    1002             :         }
    1003             : 
    1004    28748033 :         if (ret != LDB_SUCCESS) {
    1005           2 :                 ldb_debug(lock_context->ldb,
    1006             :                           LDB_DEBUG_FATAL,
    1007             :                           "Failed to unlock db: %s / %s",
    1008             :                           ldb_errstring(lock_context->ldb),
    1009             :                           ldb_strerror(ret));
    1010             :         }
    1011    28748033 :         return 0;
    1012             : }
    1013             : 
    1014    59917287 : static int ldb_lock_backend_callback(struct ldb_request *req,
    1015             :                                      struct ldb_reply *ares)
    1016             : {
    1017     2459853 :         struct ldb_db_lock_context *lock_context;
    1018     2459853 :         int ret;
    1019             : 
    1020    59917287 :         if (req->context == NULL) {
    1021             :                 /*
    1022             :                  * The usual way to get here is to ignore the return codes
    1023             :                  * and continuing processing after an error.
    1024             :                  */
    1025           0 :                 abort();
    1026             :         }
    1027    59917287 :         lock_context = talloc_get_type(req->context,
    1028             :                                        struct ldb_db_lock_context);
    1029             : 
    1030    59917287 :         if (!ares) {
    1031           0 :                 return ldb_module_done(lock_context->req, NULL, NULL,
    1032             :                                         LDB_ERR_OPERATIONS_ERROR);
    1033             :         }
    1034    59917287 :         if (ares->error != LDB_SUCCESS || ares->type == LDB_REPLY_DONE) {
    1035    28747994 :                 ret = ldb_module_done(lock_context->req, ares->controls,
    1036             :                                       ares->response, ares->error);
    1037             :                 /*
    1038             :                  * If this is a LDB_REPLY_DONE or an error, unlock the
    1039             :                  * DB by calling the destructor on this context
    1040             :                  */
    1041    28747992 :                 TALLOC_FREE(req->context);
    1042    28747992 :                 return ret;
    1043             :         }
    1044             : 
    1045             :         /* Otherwise pass on the callback */
    1046    31169293 :         switch (ares->type) {
    1047    26387865 :         case LDB_REPLY_ENTRY:
    1048    26387865 :                 return ldb_module_send_entry(lock_context->req, ares->message,
    1049             :                                              ares->controls);
    1050             : 
    1051     4781428 :         case LDB_REPLY_REFERRAL:
    1052     4781428 :                 return ldb_module_send_referral(lock_context->req,
    1053             :                                                 ares->referral);
    1054           0 :         default:
    1055             :                 /* Can't happen */
    1056           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1057             :         }
    1058             : }
    1059             : 
    1060             : /*
    1061             :  * Do an ldb_search() with a lock held, but release it if the request
    1062             :  * is freed with talloc_free()
    1063             :  */
    1064    29083182 : static int lock_search(struct ldb_module *lock_module, struct ldb_request *req)
    1065             : {
    1066             :         /* Used in FIRST_OP_NOERR to find where to send the lock request */
    1067    29083182 :         struct ldb_module *next_module = NULL;
    1068    29083182 :         struct ldb_request *down_req = NULL;
    1069     1323670 :         struct ldb_db_lock_context *lock_context;
    1070    29083182 :         struct ldb_context *ldb = ldb_module_get_ctx(lock_module);
    1071     1323670 :         int ret;
    1072             : 
    1073    29083182 :         lock_context = talloc(req, struct ldb_db_lock_context);
    1074    29083182 :         if (lock_context == NULL) {
    1075           0 :                 return ldb_oom(ldb);
    1076             :         }
    1077             : 
    1078    29083182 :         lock_context->ldb = ldb;
    1079    29083182 :         lock_context->req = req;
    1080             : 
    1081    29083182 :         ret = ldb_build_search_req_ex(&down_req, ldb, req,
    1082             :                                       req->op.search.base,
    1083             :                                       req->op.search.scope,
    1084             :                                       req->op.search.tree,
    1085             :                                       req->op.search.attrs,
    1086             :                                       req->controls,
    1087             :                                       lock_context,
    1088             :                                       ldb_lock_backend_callback,
    1089             :                                       req);
    1090    29083182 :         LDB_REQ_SET_LOCATION(down_req);
    1091    29083182 :         if (ret != LDB_SUCCESS) {
    1092           0 :                 return ret;
    1093             :         }
    1094             : 
    1095             :         /* call DB lock */
    1096   133307802 :         FIRST_OP_NOERR(ldb, read_lock);
    1097    29083182 :         if (next_module != NULL) {
    1098    28748047 :                 ret = next_module->ops->read_lock(next_module);
    1099             :         } else {
    1100      334833 :                 ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
    1101             :         }
    1102             : 
    1103    29082880 :         if (ret == LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION) {
    1104             :                 /* We might be talking LDAP */
    1105      335135 :                 ldb_reset_err_string(ldb);
    1106      335135 :                 TALLOC_FREE(lock_context);
    1107             : 
    1108      335135 :                 return ldb_next_request(lock_module, req);
    1109    28748047 :         } else if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
    1110             :                 /* if no error string was setup by the backend */
    1111           0 :                 ldb_asprintf_errstring(ldb, "Failed to get DB lock: %s (%d)",
    1112             :                                        ldb_strerror(ret), ret);
    1113             :         } else {
    1114    28748047 :                 talloc_set_destructor(lock_context, ldb_db_lock_destructor);
    1115             :         }
    1116             : 
    1117    28748047 :         if (ret != LDB_SUCCESS) {
    1118           2 :                 return ret;
    1119             :         }
    1120             : 
    1121    28748045 :         return ldb_next_request(lock_module, down_req);
    1122             : }
    1123             : 
    1124             : /*
    1125             :   start an ldb request
    1126             :   NOTE: the request must be a talloc context.
    1127             :   returns LDB_ERR_* on errors.
    1128             : */
    1129    44115665 : int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
    1130             : {
    1131     1734182 :         struct ldb_module *next_module;
    1132     1734182 :         int ret;
    1133             : 
    1134    44115665 :         if (req->callback == NULL) {
    1135           0 :                 ldb_set_errstring(ldb, "Requests MUST define callbacks");
    1136           0 :                 return LDB_ERR_UNWILLING_TO_PERFORM;
    1137             :         }
    1138             : 
    1139    44115665 :         ldb_reset_err_string(ldb);
    1140             : 
    1141    44115665 :         if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
    1142           0 :                 ldb_trace_request(ldb, req);
    1143             :         }
    1144             : 
    1145             :         /* call the first module in the chain */
    1146    44115665 :         switch (req->operation) {
    1147    29083326 :         case LDB_SEARCH:
    1148             :         {
    1149             :                 /*
    1150             :                  * A fake module to allow ldb_next_request() to be
    1151             :                  * re-used and to keep the locking out of this function.
    1152             :                  */
    1153     1323670 :                 static const struct ldb_module_ops lock_module_ops = {
    1154             :                         .name = "lock_searches",
    1155             :                         .search = lock_search
    1156             :                 };
    1157    29083326 :                 struct ldb_module lock_module = {
    1158             :                         .ldb = ldb,
    1159    29083326 :                         .next = ldb->modules,
    1160             :                         .ops = &lock_module_ops
    1161             :                 };
    1162    29083326 :                 next_module = &lock_module;
    1163             : 
    1164             :                 /* due to "ldb_build_search_req" base DN always != NULL */
    1165    29083326 :                 if (!ldb_dn_validate(req->op.search.base)) {
    1166         144 :                         ldb_asprintf_errstring(ldb, "ldb_search: invalid basedn '%s'",
    1167             :                                                ldb_dn_get_linearized(req->op.search.base));
    1168         144 :                         return LDB_ERR_INVALID_DN_SYNTAX;
    1169             :                 }
    1170             : 
    1171    29083182 :                 ret = next_module->ops->search(next_module, req);
    1172    29083182 :                 break;
    1173             :         }
    1174      923595 :         case LDB_ADD:
    1175      923595 :                 if (!ldb_dn_validate(req->op.add.message->dn)) {
    1176          20 :                         ldb_asprintf_errstring(ldb, "ldb_add: invalid dn '%s'",
    1177          20 :                                                ldb_dn_get_linearized(req->op.add.message->dn));
    1178          20 :                         return LDB_ERR_INVALID_DN_SYNTAX;
    1179             :                 }
    1180             :                 /*
    1181             :                  * we have to normalize here, as so many places
    1182             :                  * in modules and backends assume we don't have two
    1183             :                  * elements with the same name
    1184             :                  */
    1185     1015823 :                 ret = ldb_msg_normalize(ldb, req, req->op.add.message,
    1186      923575 :                                         discard_const(&req->op.add.message));
    1187      923575 :                 if (ret != LDB_SUCCESS) {
    1188           0 :                         ldb_oom(ldb);
    1189           0 :                         return ret;
    1190             :                 }
    1191     1528741 :                 FIRST_OP(ldb, add);
    1192      923575 :                 ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
    1193      923575 :                 if (ret != LDB_SUCCESS) {
    1194             :                         /*
    1195             :                          * "ldb_msg_check_element_flags" generates an error
    1196             :                          * string
    1197             :                          */
    1198           0 :                         return ret;
    1199             :                 }
    1200      923575 :                 ret = next_module->ops->add(next_module, req);
    1201      923575 :                 break;
    1202      593312 :         case LDB_MODIFY:
    1203      593312 :                 if (!ldb_dn_validate(req->op.mod.message->dn)) {
    1204           0 :                         ldb_asprintf_errstring(ldb, "ldb_modify: invalid dn '%s'",
    1205           0 :                                                ldb_dn_get_linearized(req->op.mod.message->dn));
    1206           0 :                         return LDB_ERR_INVALID_DN_SYNTAX;
    1207             :                 }
    1208     1043125 :                 FIRST_OP(ldb, modify);
    1209      593312 :                 ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
    1210      593312 :                 if (ret != LDB_SUCCESS) {
    1211             :                         /*
    1212             :                          * "ldb_msg_check_element_flags" generates an error
    1213             :                          * string
    1214             :                          */
    1215           0 :                         return ret;
    1216             :                 }
    1217      593312 :                 ret = next_module->ops->modify(next_module, req);
    1218      593312 :                 break;
    1219      283519 :         case LDB_DELETE:
    1220      283519 :                 if (!ldb_dn_validate(req->op.del.dn)) {
    1221           4 :                         ldb_asprintf_errstring(ldb, "ldb_delete: invalid dn '%s'",
    1222             :                                                ldb_dn_get_linearized(req->op.del.dn));
    1223           4 :                         return LDB_ERR_INVALID_DN_SYNTAX;
    1224             :                 }
    1225      595503 :                 FIRST_OP(ldb, del);
    1226      283515 :                 ret = next_module->ops->del(next_module, req);
    1227      283515 :                 break;
    1228        1984 :         case LDB_RENAME:
    1229        1984 :                 if (!ldb_dn_validate(req->op.rename.olddn)) {
    1230          22 :                         ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
    1231             :                                                ldb_dn_get_linearized(req->op.rename.olddn));
    1232          22 :                         return LDB_ERR_INVALID_DN_SYNTAX;
    1233             :                 }
    1234        1962 :                 if (!ldb_dn_validate(req->op.rename.newdn)) {
    1235          25 :                         ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
    1236             :                                                ldb_dn_get_linearized(req->op.rename.newdn));
    1237          25 :                         return LDB_ERR_INVALID_DN_SYNTAX;
    1238             :                 }
    1239        4630 :                 FIRST_OP(ldb, rename);
    1240        1937 :                 ret = next_module->ops->rename(next_module, req);
    1241        1937 :                 break;
    1242     1821451 :         case LDB_EXTENDED:
    1243     5544986 :                 FIRST_OP(ldb, extended);
    1244     1821451 :                 ret = next_module->ops->extended(next_module, req);
    1245     1821451 :                 break;
    1246    11408478 :         default:
    1247    34601487 :                 FIRST_OP(ldb, request);
    1248    11090073 :                 ret = next_module->ops->request(next_module, req);
    1249    11090073 :                 break;
    1250             :         }
    1251             : 
    1252    43797045 :         if ((ret != LDB_SUCCESS) && (ldb->err_string == NULL)) {
    1253             :                 /* if no error string was setup by the backend */
    1254           7 :                 ldb_asprintf_errstring(ldb, "ldb_request: %s (%d)",
    1255             :                                        ldb_strerror(ret), ret);
    1256             :         }
    1257             : 
    1258    42068909 :         return ret;
    1259             : }
    1260             : 
    1261   109269934 : int ldb_request_done(struct ldb_request *req, int status)
    1262             : {
    1263   109269934 :         req->handle->state = LDB_ASYNC_DONE;
    1264   109269934 :         req->handle->status = status;
    1265   109269934 :         return status;
    1266             : }
    1267             : 
    1268             : /*
    1269             :   search the database given a LDAP-like search expression
    1270             : 
    1271             :   returns an LDB error code
    1272             : 
    1273             :   Use talloc_free to free the ldb_message returned in 'res', if successful
    1274             : 
    1275             : */
    1276   210834144 : int ldb_search_default_callback(struct ldb_request *req,
    1277             :                                 struct ldb_reply *ares)
    1278             : {
    1279     5289223 :         struct ldb_result *res;
    1280     5289223 :         unsigned int n;
    1281             : 
    1282   210834144 :         res = talloc_get_type(req->context, struct ldb_result);
    1283             : 
    1284   210834144 :         if (!ares) {
    1285           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1286             :         }
    1287   210834144 :         if (ares->error != LDB_SUCCESS) {
    1288     2046217 :                 return ldb_request_done(req, ares->error);
    1289             :         }
    1290             : 
    1291   208787927 :         switch (ares->type) {
    1292   146206198 :         case LDB_REPLY_ENTRY:
    1293   146206198 :                 res->msgs = talloc_realloc(res, res->msgs,
    1294             :                                         struct ldb_message *, res->count + 2);
    1295   146206198 :                 if (! res->msgs) {
    1296           0 :                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1297             :                 }
    1298             : 
    1299   146206198 :                 res->msgs[res->count + 1] = NULL;
    1300             : 
    1301   146206198 :                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
    1302   146206198 :                 res->count++;
    1303   146206198 :                 break;
    1304             : 
    1305     5575686 :         case LDB_REPLY_REFERRAL:
    1306     5575686 :                 if (res->refs) {
    1307     8691765 :                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
    1308             :                 } else {
    1309     2046209 :                         n = 0;
    1310             :                 }
    1311             : 
    1312     5575686 :                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
    1313     5575686 :                 if (! res->refs) {
    1314           0 :                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1315             :                 }
    1316             : 
    1317     5575686 :                 res->refs[n] = talloc_move(res->refs, &ares->referral);
    1318     5575686 :                 res->refs[n + 1] = NULL;
    1319     5575686 :                 break;
    1320             : 
    1321    57006043 :         case LDB_REPLY_DONE:
    1322             :                 /* TODO: we should really support controls on entries
    1323             :                  * and referrals too! */
    1324    57006043 :                 res->controls = talloc_move(res, &ares->controls);
    1325             : 
    1326             :                 /* this is the last message, and means the request is done */
    1327             :                 /* we have to signal and eventual ldb_wait() waiting that the
    1328             :                  * async request operation was completed */
    1329    57006043 :                 talloc_free(ares);
    1330    57006043 :                 return ldb_request_done(req, LDB_SUCCESS);
    1331             :         }
    1332             : 
    1333   151781884 :         talloc_free(ares);
    1334             : 
    1335   151781884 :         return LDB_SUCCESS;
    1336             : }
    1337             : 
    1338     2489554 : int ldb_modify_default_callback(struct ldb_request *req, struct ldb_reply *ares)
    1339             : {
    1340       50154 :         struct ldb_result *res;
    1341       50154 :         unsigned int n;
    1342       50154 :         int ret;
    1343             : 
    1344     2489554 :         res = talloc_get_type(req->context, struct ldb_result);
    1345             : 
    1346     2489554 :         if (!ares) {
    1347           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1348             :         }
    1349             : 
    1350     2489554 :         if (ares->error != LDB_SUCCESS) {
    1351       46979 :                 ret = ares->error;
    1352       46979 :                 talloc_free(ares);
    1353       46979 :                 return ldb_request_done(req, ret);
    1354             :         }
    1355             : 
    1356     2442575 :         switch (ares->type) {
    1357           9 :         case LDB_REPLY_REFERRAL:
    1358           9 :                 if (res->refs) {
    1359           0 :                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
    1360             :                 } else {
    1361           9 :                         n = 0;
    1362             :                 }
    1363             : 
    1364           9 :                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
    1365           9 :                 if (! res->refs) {
    1366           0 :                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1367             :                 }
    1368             : 
    1369           9 :                 res->refs[n] = talloc_move(res->refs, &ares->referral);
    1370           9 :                 res->refs[n + 1] = NULL;
    1371           9 :                 break;
    1372             : 
    1373     2442566 :         case LDB_REPLY_DONE:
    1374     2442566 :                 talloc_free(ares);
    1375     2442566 :                 return ldb_request_done(req, LDB_SUCCESS);
    1376           0 :         default:
    1377           0 :                 talloc_free(ares);
    1378           0 :                 ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
    1379           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1380             :         }
    1381             : 
    1382           9 :         talloc_free(ares);
    1383           9 :         return ldb_request_done(req, LDB_SUCCESS);
    1384             : }
    1385             : 
    1386    12617576 : int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
    1387             : {
    1388      308686 :         int ret;
    1389             : 
    1390    12617576 :         if (!ares) {
    1391           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1392             :         }
    1393             : 
    1394    12617576 :         if (ares->error != LDB_SUCCESS) {
    1395       93096 :                 ret = ares->error;
    1396       93096 :                 talloc_free(ares);
    1397       93096 :                 return ldb_request_done(req, ret);
    1398             :         }
    1399             : 
    1400    12524480 :         if (ares->type != LDB_REPLY_DONE) {
    1401         364 :                 ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
    1402         364 :                 TALLOC_FREE(ares);
    1403         364 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1404             :         }
    1405             : 
    1406    12524116 :         talloc_free(ares);
    1407    12524116 :         return ldb_request_done(req, LDB_SUCCESS);
    1408             : }
    1409             : 
    1410   588539597 : static struct ldb_request *ldb_build_req_common(TALLOC_CTX *mem_ctx,
    1411             :                                 struct ldb_context *ldb,
    1412             :                                 struct ldb_control **controls,
    1413             :                                 void *context,
    1414             :                                 ldb_request_callback_t callback,
    1415             :                                 struct ldb_request *parent)
    1416             : {
    1417   588539597 :         struct ldb_request *req = NULL;
    1418             : 
    1419   588539597 :         req = talloc_zero(mem_ctx, struct ldb_request);
    1420   588539597 :         if (req == NULL) {
    1421           0 :                 return NULL;
    1422             :         }
    1423   588539597 :         req->controls = controls;
    1424   588539597 :         req->context = context;
    1425   588539597 :         req->callback = callback;
    1426             : 
    1427   588539597 :         ldb_set_timeout_from_prev_req(ldb, parent, req);
    1428             : 
    1429   588539597 :         if (parent != NULL) {
    1430   519337158 :                 req->handle = ldb_handle_new_child(req, parent);
    1431   519337158 :                 if (req->handle == NULL) {
    1432           0 :                         TALLOC_FREE(req);
    1433           0 :                         return NULL;
    1434             :                 }
    1435             :         } else {
    1436    69202439 :                 req->handle = ldb_handle_new(req, ldb);
    1437    69202439 :                 if (req->handle == NULL) {
    1438           0 :                         TALLOC_FREE(req);
    1439           0 :                         return NULL;
    1440             :                 }
    1441             :         }
    1442             : 
    1443   567317846 :         return req;
    1444             : }
    1445             : 
    1446   544985786 : int ldb_build_search_req_ex(struct ldb_request **ret_req,
    1447             :                         struct ldb_context *ldb,
    1448             :                         TALLOC_CTX *mem_ctx,
    1449             :                         struct ldb_dn *base,
    1450             :                         enum ldb_scope scope,
    1451             :                         struct ldb_parse_tree *tree,
    1452             :                         const char * const *attrs,
    1453             :                         struct ldb_control **controls,
    1454             :                         void *context,
    1455             :                         ldb_request_callback_t callback,
    1456             :                         struct ldb_request *parent)
    1457             : {
    1458    18947225 :         struct ldb_request *req;
    1459             : 
    1460   544985786 :         *ret_req = NULL;
    1461             : 
    1462   544985786 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1463             :                                    context, callback, parent);
    1464   544985786 :         if (req == NULL) {
    1465           0 :                 ldb_oom(ldb);
    1466           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1467             :         }
    1468             : 
    1469   544985786 :         req->operation = LDB_SEARCH;
    1470   544985786 :         if (base == NULL) {
    1471    26321912 :                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
    1472    26321912 :                 if (req->op.search.base == NULL) {
    1473           0 :                         ldb_oom(ldb);
    1474           0 :                         return LDB_ERR_OPERATIONS_ERROR;
    1475             :                 }
    1476             :         } else {
    1477   518663874 :                 req->op.search.base = base;
    1478             :         }
    1479   544985786 :         req->op.search.scope = scope;
    1480             : 
    1481   544985786 :         req->op.search.tree = tree;
    1482   544985786 :         if (req->op.search.tree == NULL) {
    1483           0 :                 ldb_set_errstring(ldb, "'tree' can't be NULL");
    1484           0 :                 talloc_free(req);
    1485           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1486             :         }
    1487             : 
    1488   544985786 :         req->op.search.attrs = attrs;
    1489   544985786 :         *ret_req = req;
    1490   544985786 :         return LDB_SUCCESS;
    1491             : }
    1492             : 
    1493    69679973 : int ldb_build_search_req(struct ldb_request **ret_req,
    1494             :                         struct ldb_context *ldb,
    1495             :                         TALLOC_CTX *mem_ctx,
    1496             :                         struct ldb_dn *base,
    1497             :                         enum ldb_scope scope,
    1498             :                         const char *expression,
    1499             :                         const char * const *attrs,
    1500             :                         struct ldb_control **controls,
    1501             :                         void *context,
    1502             :                         ldb_request_callback_t callback,
    1503             :                         struct ldb_request *parent)
    1504             : {
    1505     2640576 :         struct ldb_parse_tree *tree;
    1506     2640576 :         int ret;
    1507             : 
    1508    69679973 :         tree = ldb_parse_tree(mem_ctx, expression);
    1509    69679973 :         if (tree == NULL) {
    1510          12 :                 ldb_set_errstring(ldb, "Unable to parse search expression");
    1511          12 :                 return LDB_ERR_OPERATIONS_ERROR;
    1512             :         }
    1513             : 
    1514    69679961 :         ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
    1515             :                                       scope, tree, attrs, controls,
    1516             :                                       context, callback, parent);
    1517    69679961 :         if (ret == LDB_SUCCESS) {
    1518    69679961 :                 talloc_steal(*ret_req, tree);
    1519             :         }
    1520    67039388 :         return ret;
    1521             : }
    1522             : 
    1523     6858318 : int ldb_build_add_req(struct ldb_request **ret_req,
    1524             :                         struct ldb_context *ldb,
    1525             :                         TALLOC_CTX *mem_ctx,
    1526             :                         const struct ldb_message *message,
    1527             :                         struct ldb_control **controls,
    1528             :                         void *context,
    1529             :                         ldb_request_callback_t callback,
    1530             :                         struct ldb_request *parent)
    1531             : {
    1532      810605 :         struct ldb_request *req;
    1533             : 
    1534     6858318 :         *ret_req = NULL;
    1535             : 
    1536     6858318 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1537             :                                    context, callback, parent);
    1538     6858318 :         if (req == NULL) {
    1539           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1540           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1541             :         }
    1542             : 
    1543     6858318 :         req->operation = LDB_ADD;
    1544     6858318 :         req->op.add.message = message;
    1545     6858318 :         *ret_req = req;
    1546     6858318 :         return LDB_SUCCESS;
    1547             : }
    1548             : 
    1549     7043098 : int ldb_build_mod_req(struct ldb_request **ret_req,
    1550             :                         struct ldb_context *ldb,
    1551             :                         TALLOC_CTX *mem_ctx,
    1552             :                         const struct ldb_message *message,
    1553             :                         struct ldb_control **controls,
    1554             :                         void *context,
    1555             :                         ldb_request_callback_t callback,
    1556             :                         struct ldb_request *parent)
    1557             : {
    1558      198518 :         struct ldb_request *req;
    1559             : 
    1560     7043098 :         *ret_req = NULL;
    1561             : 
    1562     7043098 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1563             :                                    context, callback, parent);
    1564     7043098 :         if (req == NULL) {
    1565           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1566           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1567             :         }
    1568             : 
    1569     7043098 :         req->operation = LDB_MODIFY;
    1570     7043098 :         req->op.mod.message = message;
    1571             : 
    1572     7043098 :         *ret_req = req;
    1573     7043098 :         return LDB_SUCCESS;
    1574             : }
    1575             : 
    1576      599495 : int ldb_build_del_req(struct ldb_request **ret_req,
    1577             :                         struct ldb_context *ldb,
    1578             :                         TALLOC_CTX *mem_ctx,
    1579             :                         struct ldb_dn *dn,
    1580             :                         struct ldb_control **controls,
    1581             :                         void *context,
    1582             :                         ldb_request_callback_t callback,
    1583             :                         struct ldb_request *parent)
    1584             : {
    1585        1349 :         struct ldb_request *req;
    1586             : 
    1587      599495 :         *ret_req = NULL;
    1588             : 
    1589      599495 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1590             :                                    context, callback, parent);
    1591      599495 :         if (req == NULL) {
    1592           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1593           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1594             :         }
    1595             : 
    1596      599495 :         req->operation = LDB_DELETE;
    1597      599495 :         req->op.del.dn = dn;
    1598      599495 :         *ret_req = req;
    1599      599495 :         return LDB_SUCCESS;
    1600             : }
    1601             : 
    1602      336947 : int ldb_build_rename_req(struct ldb_request **ret_req,
    1603             :                         struct ldb_context *ldb,
    1604             :                         TALLOC_CTX *mem_ctx,
    1605             :                         struct ldb_dn *olddn,
    1606             :                         struct ldb_dn *newdn,
    1607             :                         struct ldb_control **controls,
    1608             :                         void *context,
    1609             :                         ldb_request_callback_t callback,
    1610             :                         struct ldb_request *parent)
    1611             : {
    1612         507 :         struct ldb_request *req;
    1613             : 
    1614      336947 :         *ret_req = NULL;
    1615             : 
    1616      336947 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1617             :                                    context, callback, parent);
    1618      336947 :         if (req == NULL) {
    1619           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1620           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1621             :         }
    1622             : 
    1623      336947 :         req->operation = LDB_RENAME;
    1624      336947 :         req->op.rename.olddn = olddn;
    1625      336947 :         req->op.rename.newdn = newdn;
    1626      336947 :         *ret_req = req;
    1627      336947 :         return LDB_SUCCESS;
    1628             : }
    1629             : 
    1630    28711180 : int ldb_extended_default_callback(struct ldb_request *req,
    1631             :                                   struct ldb_reply *ares)
    1632             : {
    1633     1263439 :         struct ldb_result *res;
    1634             : 
    1635    28711180 :         res = talloc_get_type(req->context, struct ldb_result);
    1636             : 
    1637    28711180 :         if (!ares) {
    1638           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1639             :         }
    1640    28711180 :         if (ares->error != LDB_SUCCESS) {
    1641         389 :                 return ldb_request_done(req, ares->error);
    1642             :         }
    1643             : 
    1644    28710791 :         if (ares->type == LDB_REPLY_DONE) {
    1645             : 
    1646             :                 /* TODO: we should really support controls on entries and referrals too! */
    1647    28710791 :                 res->extended = talloc_move(res, &ares->response);
    1648    28710791 :                 res->controls = talloc_move(res, &ares->controls);
    1649             : 
    1650    28710791 :                 talloc_free(ares);
    1651    28710791 :                 return ldb_request_done(req, LDB_SUCCESS);
    1652             :         }
    1653             : 
    1654           0 :         talloc_free(ares);
    1655           0 :         ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type);
    1656           0 :         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1657             : }
    1658             : 
    1659    28715953 : int ldb_build_extended_req(struct ldb_request **ret_req,
    1660             :                            struct ldb_context *ldb,
    1661             :                            TALLOC_CTX *mem_ctx,
    1662             :                            const char *oid,
    1663             :                            void *data,
    1664             :                            struct ldb_control **controls,
    1665             :                            void *context,
    1666             :                            ldb_request_callback_t callback,
    1667             :                            struct ldb_request *parent)
    1668             : {
    1669     1263547 :         struct ldb_request *req;
    1670             : 
    1671    28715953 :         *ret_req = NULL;
    1672             : 
    1673    28715953 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1674             :                                    context, callback, parent);
    1675    28715953 :         if (req == NULL) {
    1676           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1677           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1678             :         }
    1679             : 
    1680    28715953 :         req->operation = LDB_EXTENDED;
    1681    28715953 :         req->op.extended.oid = oid;
    1682    28715953 :         req->op.extended.data = data;
    1683    28715953 :         *ret_req = req;
    1684    28715953 :         return LDB_SUCCESS;
    1685             : }
    1686             : 
    1687     1454423 : int ldb_extended(struct ldb_context *ldb,
    1688             :                  const char *oid,
    1689             :                  void *data,
    1690             :                  struct ldb_result **_res)
    1691             : {
    1692       95571 :         struct ldb_request *req;
    1693       95571 :         int ret;
    1694       95571 :         struct ldb_result *res;
    1695             : 
    1696     1454423 :         *_res = NULL;
    1697     1454423 :         req = NULL;
    1698             : 
    1699     1454423 :         res = talloc_zero(ldb, struct ldb_result);
    1700     1454423 :         if (!res) {
    1701           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1702             :         }
    1703             : 
    1704     1454423 :         ret = ldb_build_extended_req(&req, ldb, ldb,
    1705             :                                      oid, data, NULL,
    1706             :                                      res, ldb_extended_default_callback,
    1707             :                                      NULL);
    1708     1454423 :         ldb_req_set_location(req, "ldb_extended");
    1709             : 
    1710     1454423 :         if (ret != LDB_SUCCESS) goto done;
    1711             : 
    1712     1454423 :         ldb_set_timeout(ldb, req, 0); /* use default timeout */
    1713             : 
    1714     1454423 :         ret = ldb_request(ldb, req);
    1715             : 
    1716     1454423 :         if (ret == LDB_SUCCESS) {
    1717     1454417 :                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
    1718             :         }
    1719             : 
    1720           6 : done:
    1721     1454423 :         if (ret != LDB_SUCCESS) {
    1722         389 :                 talloc_free(res);
    1723         389 :                 res = NULL;
    1724             :         }
    1725             : 
    1726     1454423 :         talloc_free(req);
    1727             : 
    1728     1454423 :         *_res = res;
    1729     1454423 :         return ret;
    1730             : }
    1731             : 
    1732             : /*
    1733             :   note that ldb_search() will automatically replace a NULL 'base' value
    1734             :   with the defaultNamingContext from the rootDSE if available.
    1735             : */
    1736     7389823 : int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
    1737             :                 struct ldb_result **result, struct ldb_dn *base,
    1738             :                 enum ldb_scope scope, const char * const *attrs,
    1739             :                 const char *exp_fmt, ...)
    1740             : {
    1741      261433 :         struct ldb_request *req;
    1742      261433 :         struct ldb_result *res;
    1743      261433 :         char *expression;
    1744      261433 :         va_list ap;
    1745      261433 :         int ret;
    1746             : 
    1747     7389823 :         expression = NULL;
    1748     7389823 :         *result = NULL;
    1749     7389823 :         req = NULL;
    1750             : 
    1751     7389823 :         res = talloc_zero(mem_ctx, struct ldb_result);
    1752     7389823 :         if (!res) {
    1753           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1754             :         }
    1755             : 
    1756     7389823 :         if (exp_fmt) {
    1757     4762654 :                 va_start(ap, exp_fmt);
    1758     4762654 :                 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
    1759     4762654 :                 va_end(ap);
    1760             : 
    1761     4762654 :                 if (!expression) {
    1762           0 :                         talloc_free(res);
    1763           0 :                         return LDB_ERR_OPERATIONS_ERROR;
    1764             :                 }
    1765             :         }
    1766             : 
    1767     9033381 :         ret = ldb_build_search_req(&req, ldb, mem_ctx,
    1768     1643558 :                                         base?base:ldb_get_default_basedn(ldb),
    1769             :                                         scope,
    1770             :                                         expression,
    1771             :                                         attrs,
    1772             :                                         NULL,
    1773             :                                         res,
    1774             :                                         ldb_search_default_callback,
    1775             :                                         NULL);
    1776     7389823 :         ldb_req_set_location(req, "ldb_search");
    1777             : 
    1778     7389823 :         if (ret != LDB_SUCCESS) goto done;
    1779             : 
    1780     7389823 :         ret = ldb_request(ldb, req);
    1781             : 
    1782     7389823 :         if (ret == LDB_SUCCESS) {
    1783     7388841 :                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
    1784             :         }
    1785             : 
    1786         982 : done:
    1787     7389823 :         if (ret != LDB_SUCCESS) {
    1788      320769 :                 talloc_free(res);
    1789      320769 :                 res = NULL;
    1790             :         }
    1791             : 
    1792     7389823 :         talloc_free(expression);
    1793     7389823 :         talloc_free(req);
    1794             : 
    1795     7389823 :         *result = res;
    1796     7389823 :         return ret;
    1797             : }
    1798             : 
    1799             : /*
    1800             :   add a record to the database. Will fail if a record with the given class
    1801             :   and key already exists
    1802             : */
    1803      161414 : int ldb_add(struct ldb_context *ldb,
    1804             :             const struct ldb_message *message)
    1805             : {
    1806        6169 :         struct ldb_request *req;
    1807        6169 :         int ret;
    1808             : 
    1809      161414 :         ret = ldb_msg_sanity_check(ldb, message);
    1810      161414 :         if (ret != LDB_SUCCESS) {
    1811           0 :                 return ret;
    1812             :         }
    1813             : 
    1814      161414 :         ret = ldb_build_add_req(&req, ldb, ldb,
    1815             :                                         message,
    1816             :                                         NULL,
    1817             :                                         NULL,
    1818             :                                         ldb_op_default_callback,
    1819             :                                         NULL);
    1820      161414 :         ldb_req_set_location(req, "ldb_add");
    1821             : 
    1822      161414 :         if (ret != LDB_SUCCESS) return ret;
    1823             : 
    1824             :         /* do request and autostart a transaction */
    1825      161414 :         ret = ldb_autotransaction_request(ldb, req);
    1826             : 
    1827      161414 :         talloc_free(req);
    1828      161414 :         return ret;
    1829             : }
    1830             : 
    1831             : /*
    1832             :   modify the specified attributes of a record
    1833             : */
    1834      169207 : int ldb_modify(struct ldb_context *ldb,
    1835             :                const struct ldb_message *message)
    1836             : {
    1837        4728 :         struct ldb_request *req;
    1838        4728 :         int ret;
    1839             : 
    1840      169207 :         ret = ldb_msg_sanity_check(ldb, message);
    1841      169207 :         if (ret != LDB_SUCCESS) {
    1842           0 :                 return ret;
    1843             :         }
    1844             : 
    1845      169207 :         ret = ldb_build_mod_req(&req, ldb, ldb,
    1846             :                                         message,
    1847             :                                         NULL,
    1848             :                                         NULL,
    1849             :                                         ldb_op_default_callback,
    1850             :                                         NULL);
    1851      169207 :         ldb_req_set_location(req, "ldb_modify");
    1852             : 
    1853      169207 :         if (ret != LDB_SUCCESS) return ret;
    1854             : 
    1855             :         /* do request and autostart a transaction */
    1856      169207 :         ret = ldb_autotransaction_request(ldb, req);
    1857             : 
    1858      169207 :         talloc_free(req);
    1859      169207 :         return ret;
    1860             : }
    1861             : 
    1862             : 
    1863             : /*
    1864             :   delete a record from the database
    1865             : */
    1866       86556 : int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
    1867             : {
    1868         529 :         struct ldb_request *req;
    1869         529 :         int ret;
    1870             : 
    1871       86556 :         ret = ldb_build_del_req(&req, ldb, ldb,
    1872             :                                         dn,
    1873             :                                         NULL,
    1874             :                                         NULL,
    1875             :                                         ldb_op_default_callback,
    1876             :                                         NULL);
    1877       86556 :         ldb_req_set_location(req, "ldb_delete");
    1878             : 
    1879       86556 :         if (ret != LDB_SUCCESS) return ret;
    1880             : 
    1881             :         /* do request and autostart a transaction */
    1882       86556 :         ret = ldb_autotransaction_request(ldb, req);
    1883             : 
    1884       86556 :         talloc_free(req);
    1885       86556 :         return ret;
    1886             : }
    1887             : 
    1888             : /*
    1889             :   rename a record in the database
    1890             : */
    1891         100 : int ldb_rename(struct ldb_context *ldb,
    1892             :                 struct ldb_dn *olddn, struct ldb_dn *newdn)
    1893             : {
    1894           4 :         struct ldb_request *req;
    1895           4 :         int ret;
    1896             : 
    1897         100 :         ret = ldb_build_rename_req(&req, ldb, ldb,
    1898             :                                         olddn,
    1899             :                                         newdn,
    1900             :                                         NULL,
    1901             :                                         NULL,
    1902             :                                         ldb_op_default_callback,
    1903             :                                         NULL);
    1904         100 :         ldb_req_set_location(req, "ldb_rename");
    1905             : 
    1906         100 :         if (ret != LDB_SUCCESS) return ret;
    1907             : 
    1908             :         /* do request and autostart a transaction */
    1909         100 :         ret = ldb_autotransaction_request(ldb, req);
    1910             : 
    1911         100 :         talloc_free(req);
    1912         100 :         return ret;
    1913             : }
    1914             : 
    1915             : 
    1916             : /*
    1917             :   return the global sequence number
    1918             : */
    1919     1448721 : int ldb_sequence_number(struct ldb_context *ldb,
    1920             :                         enum ldb_sequence_type type, uint64_t *seq_num)
    1921             : {
    1922       95571 :         struct ldb_seqnum_request *seq;
    1923       95571 :         struct ldb_seqnum_result *seqr;
    1924       95571 :         struct ldb_result *res;
    1925       95571 :         TALLOC_CTX *tmp_ctx;
    1926       95571 :         int ret;
    1927             : 
    1928     1448721 :         *seq_num = 0;
    1929             : 
    1930     1448721 :         tmp_ctx = talloc_zero(ldb, struct ldb_request);
    1931     1448721 :         if (tmp_ctx == NULL) {
    1932           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1933           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1934             :         }
    1935     1448721 :         seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
    1936     1448721 :         if (seq == NULL) {
    1937           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1938           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
    1939           0 :                 goto done;
    1940             :         }
    1941     1448721 :         seq->type = type;
    1942             : 
    1943     1448721 :         ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
    1944     1448721 :         if (ret != LDB_SUCCESS) {
    1945         315 :                 goto done;
    1946             :         }
    1947     1448406 :         talloc_steal(tmp_ctx, res);
    1948             : 
    1949     1448406 :         if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
    1950           0 :                 ldb_set_errstring(ldb, "Invalid OID in reply");
    1951           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
    1952           0 :                 goto done;
    1953             :         }
    1954     1448406 :         seqr = talloc_get_type(res->extended->data,
    1955             :                                 struct ldb_seqnum_result);
    1956     1448406 :         *seq_num = seqr->seq_num;
    1957             : 
    1958     1448721 : done:
    1959     1448721 :         talloc_free(tmp_ctx);
    1960     1448721 :         return ret;
    1961             : }
    1962             : 
    1963             : /*
    1964             :   return extended error information
    1965             : */
    1966      494803 : const char *ldb_errstring(struct ldb_context *ldb)
    1967             : {
    1968      494803 :         return ldb->err_string;
    1969             : }
    1970             : 
    1971             : /*
    1972             :   return a string explaining what a ldb error constant means
    1973             : */
    1974      770833 : const char *ldb_strerror(int ldb_err)
    1975             : {
    1976      770833 :         switch (ldb_err) {
    1977      701481 :         case LDB_SUCCESS:
    1978      701481 :                 return "Success";
    1979         225 :         case LDB_ERR_OPERATIONS_ERROR:
    1980         225 :                 return "Operations error";
    1981           5 :         case LDB_ERR_PROTOCOL_ERROR:
    1982           5 :                 return "Protocol error";
    1983           9 :         case LDB_ERR_TIME_LIMIT_EXCEEDED:
    1984           9 :                 return "Time limit exceeded";
    1985           0 :         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
    1986           0 :                 return "Size limit exceeded";
    1987           0 :         case LDB_ERR_COMPARE_FALSE:
    1988           0 :                 return "Compare false";
    1989           0 :         case LDB_ERR_COMPARE_TRUE:
    1990           0 :                 return "Compare true";
    1991           0 :         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
    1992           0 :                 return "Auth method not supported";
    1993           0 :         case LDB_ERR_STRONG_AUTH_REQUIRED:
    1994           0 :                 return "Strong auth required";
    1995             : /* 9 RESERVED */
    1996           8 :         case LDB_ERR_REFERRAL:
    1997           8 :                 return "Referral error";
    1998           0 :         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
    1999           0 :                 return "Admin limit exceeded";
    2000          10 :         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
    2001          10 :                 return "Unsupported critical extension";
    2002           0 :         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
    2003           0 :                 return "Confidentiality required";
    2004           0 :         case LDB_ERR_SASL_BIND_IN_PROGRESS:
    2005           0 :                 return "SASL bind in progress";
    2006         577 :         case LDB_ERR_NO_SUCH_ATTRIBUTE:
    2007         577 :                 return "No such attribute";
    2008           2 :         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
    2009           2 :                 return "Undefined attribute type";
    2010           0 :         case LDB_ERR_INAPPROPRIATE_MATCHING:
    2011           0 :                 return "Inappropriate matching";
    2012        3972 :         case LDB_ERR_CONSTRAINT_VIOLATION:
    2013        3972 :                 return "Constraint violation";
    2014         131 :         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
    2015         131 :                 return "Attribute or value exists";
    2016          68 :         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
    2017          68 :                 return "Invalid attribute syntax";
    2018             : /* 22-31 unused */
    2019       50582 :         case LDB_ERR_NO_SUCH_OBJECT:
    2020       50582 :                 return "No such object";
    2021           0 :         case LDB_ERR_ALIAS_PROBLEM:
    2022           0 :                 return "Alias problem";
    2023         188 :         case LDB_ERR_INVALID_DN_SYNTAX:
    2024         188 :                 return "Invalid DN syntax";
    2025             : /* 35 RESERVED */
    2026           0 :         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
    2027           0 :                 return "Alias dereferencing problem";
    2028             : /* 37-47 unused */
    2029           0 :         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
    2030           0 :                 return "Inappropriate authentication";
    2031           0 :         case LDB_ERR_INVALID_CREDENTIALS:
    2032           0 :                 return "Invalid credentials";
    2033        1815 :         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
    2034        1815 :                 return "insufficient access rights";
    2035           0 :         case LDB_ERR_BUSY:
    2036           0 :                 return "Busy";
    2037           0 :         case LDB_ERR_UNAVAILABLE:
    2038           0 :                 return "Unavailable";
    2039         530 :         case LDB_ERR_UNWILLING_TO_PERFORM:
    2040         530 :                 return "Unwilling to perform";
    2041           0 :         case LDB_ERR_LOOP_DETECT:
    2042           0 :                 return "Loop detect";
    2043             : /* 55-63 unused */
    2044           5 :         case LDB_ERR_NAMING_VIOLATION:
    2045           5 :                 return "Naming violation";
    2046         421 :         case LDB_ERR_OBJECT_CLASS_VIOLATION:
    2047         421 :                 return "Object class violation";
    2048          26 :         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
    2049          26 :                 return "Not allowed on non-leaf";
    2050           2 :         case LDB_ERR_NOT_ALLOWED_ON_RDN:
    2051           2 :                 return "Not allowed on RDN";
    2052         292 :         case LDB_ERR_ENTRY_ALREADY_EXISTS:
    2053         292 :                 return "Entry already exists";
    2054           0 :         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
    2055           0 :                 return "Object class mods prohibited";
    2056             : /* 70 RESERVED FOR CLDAP */
    2057           4 :         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
    2058           4 :                 return "Affects multiple DSAs";
    2059             : /* 72-79 unused */
    2060          97 :         case LDB_ERR_OTHER:
    2061          97 :                 return "Other";
    2062             :         }
    2063             : 
    2064           3 :         return "Unknown error";
    2065             : }
    2066             : 
    2067             : /*
    2068             :   set backend specific opaque parameters
    2069             : */
    2070   439726080 : int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
    2071             : {
    2072    22159862 :         struct ldb_opaque *o;
    2073             : 
    2074             :         /* allow updating an existing value */
    2075  3878132326 :         for (o=ldb->opaque;o;o=o->next) {
    2076  3867574321 :                 if (strcmp(o->name, name) == 0) {
    2077   429168075 :                         o->value = value;
    2078   429168075 :                         return LDB_SUCCESS;
    2079             :                 }
    2080             :         }
    2081             : 
    2082    10558005 :         o = talloc(ldb, struct ldb_opaque);
    2083    10558005 :         if (o == NULL) {
    2084           0 :                 ldb_oom(ldb);
    2085           0 :                 return LDB_ERR_OTHER;
    2086             :         }
    2087    10558005 :         o->next = ldb->opaque;
    2088    10558005 :         o->name = name;
    2089    10558005 :         o->value = value;
    2090    10558005 :         ldb->opaque = o;
    2091    10558005 :         return LDB_SUCCESS;
    2092             : }
    2093             : 
    2094             : /*
    2095             :   get a previously set opaque value
    2096             : */
    2097  1171746855 : void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
    2098             : {
    2099    62916597 :         struct ldb_opaque *o;
    2100 17190008399 :         for (o=ldb->opaque;o;o=o->next) {
    2101 16974573412 :                 if (strcmp(o->name, name) == 0) {
    2102   956311868 :                         return o->value;
    2103             :                 }
    2104             :         }
    2105   203935674 :         return NULL;
    2106             : }
    2107             : 
    2108           0 : int ldb_global_init(void)
    2109             : {
    2110             :         /* Provided for compatibility with some older versions of ldb */
    2111           0 :         return 0;
    2112             : }
    2113             : 
    2114             : /* return the ldb flags */
    2115       87173 : unsigned int ldb_get_flags(struct ldb_context *ldb)
    2116             : {
    2117       87173 :         return ldb->flags;
    2118             : }
    2119             : 
    2120             : /* set the ldb flags */
    2121       53887 : void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
    2122             : {
    2123       53887 :         ldb->flags = flags;
    2124       53887 : }
    2125             : 
    2126             : 
    2127             : /*
    2128             :   set the location in a ldb request. Used for debugging
    2129             :  */
    2130   562999316 : void ldb_req_set_location(struct ldb_request *req, const char *location)
    2131             : {
    2132   562999316 :         if (req && req->handle) {
    2133   562999316 :                 req->handle->location = location;
    2134             :         }
    2135   562999316 : }
    2136             : 
    2137             : /*
    2138             :   return the location set with dsdb_req_set_location
    2139             :  */
    2140           0 : const char *ldb_req_location(struct ldb_request *req)
    2141             : {
    2142           0 :         return req->handle->location;
    2143             : }
    2144             : 
    2145             : /**
    2146             :   mark a request as untrusted. This tells the rootdse module to remove
    2147             :   unregistered controls
    2148             :  */
    2149      594656 : void ldb_req_mark_untrusted(struct ldb_request *req)
    2150             : {
    2151      594656 :         req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
    2152      594656 : }
    2153             : 
    2154             : /**
    2155             :   mark a request as trusted.
    2156             :  */
    2157     1449530 : void ldb_req_mark_trusted(struct ldb_request *req)
    2158             : {
    2159     1449530 :         req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
    2160     1449530 : }
    2161             : 
    2162             : /**
    2163             :   set custom flags. Those flags are set by applications using ldb,
    2164             :   they are application dependent and the same bit can have different
    2165             :   meaning in different application.
    2166             :  */
    2167           0 : void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
    2168             : {
    2169           0 :         if (req != NULL && req->handle != NULL) {
    2170           0 :                 req->handle->custom_flags = flags;
    2171             :         }
    2172           0 : }
    2173             : 
    2174             : 
    2175             : /**
    2176             :   get custom flags. Those flags are set by applications using ldb,
    2177             :   they are application dependent and the same bit can have different
    2178             :   meaning in different application.
    2179             :  */
    2180           0 : uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
    2181             : {
    2182           0 :         if (req != NULL && req->handle != NULL) {
    2183           0 :                 return req->handle->custom_flags;
    2184             :         }
    2185             : 
    2186             :         /*
    2187             :          * 0 is not something any better or worse than
    2188             :          * anything else as req or the handle is NULL
    2189             :          */
    2190           0 :         return 0;
    2191             : }
    2192             : 
    2193             : 
    2194             : /**
    2195             :  * return true if a request is untrusted
    2196             :  */
    2197   100103716 : bool ldb_req_is_untrusted(struct ldb_request *req)
    2198             : {
    2199   100103716 :         return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;
    2200             : }

Generated by: LCOV version 1.14