LCOV - code coverage report
Current view: top level - lib/ldb/common - ldb.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 796 1052 75.7 %
Date: 2024-04-13 12:30:31 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      721189 : static int ldb_context_destructor(void *ptr)
      40             : {
      41      721189 :         struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
      42             : 
      43      721189 :         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      721189 :         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  1026383681 : static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
      59             :                              const char *fmt, va_list ap)
      60             : {
      61  1026383681 :         struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
      62  1026383681 :         enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
      63             : 
      64  1026383681 :         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   997947934 :         case TEVENT_DEBUG_TRACE:
      75   997947934 :                 ldb_level = LDB_DEBUG_TRACE;
      76   997947934 :                 break;
      77    28435747 :         };
      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  1026383681 :         ldb_vdebug(ldb, ldb_level, fmt, ap);
      83  1026383681 : }
      84             : 
      85             : /*
      86             :    initialise a ldb context
      87             :    The mem_ctx is required
      88             :    The event_ctx is required
      89             : */
      90      760669 : struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
      91             : {
      92       13298 :         struct ldb_context *ldb;
      93       13298 :         int ret;
      94      760669 :         const char *modules_path = getenv("LDB_MODULES_PATH");
      95             : 
      96      760669 :         if (modules_path == NULL) {
      97      756753 :                 modules_path = LDB_MODULESDIR;
      98             :         }
      99             : 
     100      760669 :         ret = ldb_modules_load(modules_path, LDB_VERSION);
     101      760669 :         if (ret != LDB_SUCCESS) {
     102           0 :                 return NULL;
     103             :         }
     104             : 
     105      760669 :         ldb = talloc_zero(mem_ctx, struct ldb_context);
     106      760669 :         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      760669 :         if (ev_ctx == NULL) {
     114      319905 :                 ev_ctx = tevent_context_init(ldb);
     115      319905 :                 if (ev_ctx == NULL) {
     116           0 :                         talloc_free(ldb);
     117           0 :                         return NULL;
     118             :                 }
     119      319905 :                 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
     120      319905 :                 tevent_set_max_debug_level(ev_ctx, TEVENT_DEBUG_TRACE);
     121      319905 :                 tevent_loop_allow_nesting(ev_ctx);
     122             :         }
     123             : 
     124      760669 :         ret = ldb_setup_wellknown_attributes(ldb);
     125      760669 :         if (ret != LDB_SUCCESS) {
     126           0 :                 talloc_free(ldb);
     127           0 :                 return NULL;
     128             :         }
     129             : 
     130      760669 :         ldb_set_utf8_default(ldb);
     131      760669 :         ldb_set_create_perms(ldb, 0666);
     132      760669 :         ldb_set_modules_dir(ldb, LDB_MODULESDIR);
     133      760669 :         ldb_set_event_context(ldb, ev_ctx);
     134      760669 :         ret = ldb_register_extended_match_rules(ldb);
     135      760669 :         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      760669 :         ldb->default_timeout = 300; /* set default to 5 minutes */
     142             : 
     143      760669 :         talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
     144             : 
     145      760669 :         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     1169797 : void ldb_set_default_dns(struct ldb_context *ldb)
     154             : {
     155       20096 :         TALLOC_CTX *tmp_ctx;
     156       20096 :         int ret;
     157       20096 :         struct ldb_result *res;
     158     1169797 :         struct ldb_dn *tmp_dn=NULL;
     159       20096 :         static const char *attrs[] = {
     160             :                 "rootDomainNamingContext",
     161             :                 "configurationNamingContext",
     162             :                 "schemaNamingContext",
     163             :                 "defaultNamingContext",
     164             :                 NULL
     165             :         };
     166             : 
     167     1169797 :         tmp_ctx = talloc_new(ldb);
     168     1169797 :         ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
     169             :                          LDB_SCOPE_BASE, attrs, "(objectClass=*)");
     170     1169797 :         if (ret != LDB_SUCCESS) {
     171      320401 :                 talloc_free(tmp_ctx);
     172      326451 :                 return;
     173             :         }
     174             : 
     175      849396 :         if (res->count != 1) {
     176           6 :                 talloc_free(tmp_ctx);
     177           6 :                 return;
     178             :         }
     179             : 
     180      849390 :         if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
     181      437615 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     182             :                                                  "rootDomainNamingContext");
     183      437615 :                 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
     184             :         }
     185             : 
     186      849390 :         if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
     187      437615 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     188             :                                                  "configurationNamingContext");
     189      437615 :                 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
     190             :         }
     191             : 
     192      849390 :         if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
     193      437615 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     194             :                                                  "schemaNamingContext");
     195      437615 :                 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
     196             :         }
     197             : 
     198      849390 :         if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
     199      437615 :                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
     200             :                                                  "defaultNamingContext");
     201      437615 :                 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
     202             :         }
     203             : 
     204      849390 :         talloc_free(tmp_ctx);
     205             : }
     206             : 
     207     1762640 : struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
     208             : {
     209     1762640 :         void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
     210     1762640 :         return talloc_get_type(opaque, struct ldb_dn);
     211             : }
     212             : 
     213     5357238 : struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
     214             : {
     215     5357238 :         void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
     216     5357238 :         return talloc_get_type(opaque, struct ldb_dn);
     217             : }
     218             : 
     219     4103632 : struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
     220             : {
     221     4103632 :         void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
     222     4103632 :         return talloc_get_type(opaque, struct ldb_dn);
     223             : }
     224             : 
     225     7579975 : struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
     226             : {
     227     7579975 :         void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
     228     7579975 :         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      759664 : int ldb_connect(struct ldb_context *ldb, const char *url,
     242             :                 unsigned int flags, const char *options[])
     243             : {
     244       13139 :         int ret;
     245       13139 :         char *url2;
     246             :         /* We seem to need to do this here, or else some utilities don't
     247             :          * get ldb backends */
     248             : 
     249      759664 :         ldb->flags = flags;
     250             : 
     251      759664 :         url2 = talloc_strdup(ldb, url);
     252      759664 :         if (!url2) {
     253           0 :                 ldb_oom(ldb);
     254           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     255             :         }
     256      759664 :         ret = ldb_set_opaque(ldb, "ldb_url", url2);
     257      759664 :         if (ret != LDB_SUCCESS) {
     258           0 :                 return ret;
     259             :         }
     260             : 
     261             :         /*
     262             :          * Take a copy of the options.
     263             :          */
     264      759664 :         ldb->options = ldb_options_copy(ldb, options);
     265      759664 :         if (ldb->options == NULL && options != NULL) {
     266           0 :                 ldb_oom(ldb);
     267           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     268             :         }
     269             : 
     270      759664 :         ret = ldb_module_connect_backend(ldb, url, options, &ldb->modules);
     271      759664 :         if (ret != LDB_SUCCESS) {
     272        1631 :                 return ret;
     273             :         }
     274             : 
     275      758029 :         ret = ldb_load_modules(ldb, options);
     276      758029 :         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      758022 :         ldb_set_default_dns(ldb);
     285             : 
     286      758022 :         return LDB_SUCCESS;
     287             : }
     288             : 
     289     1281884 : void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
     290             : {
     291     1281884 :         ldb_asprintf_errstring(ldb, "%s", err_string);
     292     1281884 : }
     293             : 
     294     4866370 : void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
     295             : {
     296      475296 :         va_list ap;
     297     4866370 :         char *old_err_string = NULL;
     298     4866370 :         if (ldb->err_string) {
     299      304695 :                 old_err_string = ldb->err_string;
     300             :         }
     301             : 
     302     4866370 :         va_start(ap, format);
     303     4866370 :         ldb->err_string = talloc_vasprintf(ldb, format, ap);
     304     4866370 :         va_end(ap);
     305             : 
     306     4866370 :         TALLOC_FREE(old_err_string);
     307             :         
     308     4866370 :         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     4866370 : }
     313             : 
     314    59467868 : void ldb_reset_err_string(struct ldb_context *ldb)
     315             : {
     316    59467868 :         TALLOC_FREE(ldb->err_string);
     317    59467868 : }
     318             : 
     319             : 
     320             : 
     321             : /*
     322             :   set an ldb error based on file:line
     323             : */
     324      473452 : int ldb_error_at(struct ldb_context *ldb, int ecode,
     325             :                  const char *reason, const char *file, int line)
     326             : {
     327      473452 :         if (reason == NULL) {
     328           0 :                 reason = ldb_strerror(ecode);
     329             :         }
     330      473452 :         ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
     331      473452 :         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     1915104 : int ldb_transaction_start(struct ldb_context *ldb)
     359             : {
     360      111851 :         struct ldb_module *next_module;
     361      111851 :         int status;
     362             : 
     363     1915104 :         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     1915104 :         if (ldb->transaction_active) {
     369      818935 :                 ldb->transaction_active++;
     370      818935 :                 return LDB_SUCCESS;
     371             :         }
     372             : 
     373             :         /* start a new transaction */
     374     1096169 :         ldb->transaction_active++;
     375     1096169 :         ldb->prepare_commit_done = false;
     376             : 
     377     2394229 :         FIRST_OP(ldb, start_transaction);
     378             : 
     379     1096169 :         ldb_reset_err_string(ldb);
     380             : 
     381     1096169 :         status = next_module->ops->start_transaction(next_module);
     382     1096169 :         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     1096147 :                 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     1088198 :         return status;
     401             : }
     402             : 
     403             : /*
     404             :   prepare for transaction commit (first phase of two phase commit)
     405             : */
     406     1780686 : int ldb_transaction_prepare_commit(struct ldb_context *ldb)
     407             : {
     408      111015 :         struct ldb_module *next_module;
     409      111015 :         int status;
     410             : 
     411     1780686 :         if (ldb->prepare_commit_done) {
     412        2512 :                 return LDB_SUCCESS;
     413             :         }
     414             : 
     415             :         /* commit only when all nested transactions are complete */
     416     1778170 :         if (ldb->transaction_active > 1) {
     417      715573 :                 return LDB_SUCCESS;
     418             :         }
     419             : 
     420      958740 :         ldb->prepare_commit_done = true;
     421             : 
     422      958740 :         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     8328769 :         FIRST_OP_NOERR(ldb, prepare_commit);
     431      958740 :         if (next_module == NULL) {
     432      153462 :                 return LDB_SUCCESS;
     433             :         }
     434             : 
     435      805062 :         ldb_reset_err_string(ldb);
     436             : 
     437      805062 :         status = next_module->ops->prepare_commit(next_module);
     438      805062 :         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      798124 :         return status;
     458             : }
     459             : 
     460             : 
     461             : /*
     462             :   commit a transaction
     463             : */
     464     1776857 : int ldb_transaction_commit(struct ldb_context *ldb)
     465             : {
     466      111011 :         struct ldb_module *next_module;
     467      111011 :         int status;
     468             : 
     469     1776857 :         status = ldb_transaction_prepare_commit(ldb);
     470     1776857 :         if (status != LDB_SUCCESS) {
     471          10 :                 return status;
     472             :         }
     473             : 
     474     1776847 :         ldb->transaction_active--;
     475             : 
     476     1776847 :         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     1776847 :         if (ldb->transaction_active > 0) {
     482      714264 :                 return LDB_SUCCESS;
     483             :         }
     484             : 
     485      958726 :         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      958726 :         ldb_reset_err_string(ldb);
     493             : 
     494     2152144 :         FIRST_OP(ldb, end_transaction);
     495      958726 :         status = next_module->ops->end_transaction(next_module);
     496      958726 :         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      951572 :         return status;
     510             : }
     511             : 
     512             : 
     513             : /*
     514             :   cancel a transaction
     515             : */
     516      138204 : int ldb_transaction_cancel(struct ldb_context *ldb)
     517             : {
     518         838 :         struct ldb_module *next_module;
     519         838 :         int status;
     520             : 
     521      138204 :         ldb->transaction_active--;
     522             : 
     523      138204 :         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      138204 :         if (ldb->transaction_active > 0) {
     529         791 :                 return LDB_SUCCESS;
     530             :         }
     531             : 
     532      137390 :         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      241986 :         FIRST_OP(ldb, del_transaction);
     540             : 
     541      137390 :         status = next_module->ops->del_transaction(next_module);
     542      137390 :         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      136575 :         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      412648 : static int ldb_autotransaction_request(struct ldb_context *ldb,
     573             :                                        struct ldb_request *req)
     574             : {
     575        5822 :         int ret;
     576             : 
     577      412648 :         ret = ldb_transaction_start(ldb);
     578      412648 :         if (ret != LDB_SUCCESS) {
     579          20 :                 return ret;
     580             :         }
     581             : 
     582      412628 :         ret = ldb_request(ldb, req);
     583      412628 :         if (ret == LDB_SUCCESS) {
     584      412497 :                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
     585             :         }
     586             : 
     587      412628 :         if (ret == LDB_SUCCESS) {
     588      368542 :                 return ldb_transaction_commit(ldb);
     589             :         }
     590       44086 :         ldb_transaction_cancel(ldb);
     591             : 
     592       44086 :         return ret;
     593             : }
     594             : 
     595   113314089 : int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
     596             : {
     597     3686490 :         struct tevent_context *ev;
     598     3686490 :         int ret;
     599             : 
     600   113314089 :         if (handle == NULL) {
     601           0 :                 return LDB_ERR_UNAVAILABLE;
     602             :         }
     603             : 
     604   113314089 :         if (handle->state == LDB_ASYNC_DONE) {
     605    14172188 :                 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    14172188 :                 return handle->status;
     615             :         }
     616             : 
     617    99141901 :         ev = ldb_handle_get_event_context(handle);
     618    99141901 :         if (NULL == ev) {
     619           0 :                 return ldb_oom(handle->ldb);
     620             :         }
     621             : 
     622    99141901 :         switch (type) {
     623     3470738 :         case LDB_WAIT_NONE:
     624     3470738 :                 ret = tevent_loop_once(ev);
     625     3470738 :                 if (ret != 0) {
     626           0 :                         return ldb_operr(handle->ldb);
     627             :                 }
     628     3470738 :                 if (handle->status == LDB_SUCCESS) {
     629     3469372 :                         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    92310128 :         case LDB_WAIT_ALL:
     645   300403771 :                 while (handle->state != LDB_ASYNC_DONE) {
     646   208647716 :                         ret = tevent_loop_once(ev);
     647   208647704 :                         if (ret != 0) {
     648           0 :                                 return ldb_operr(handle->ldb);
     649             :                         }
     650   208647704 :                         if (handle->status != LDB_SUCCESS) {
     651     3915096 :                                 if (handle->ldb->err_string != NULL) {
     652     3435433 :                                         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    91756055 :                 if (handle->status == LDB_SUCCESS) {
     668    88841761 :                         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    83101462 : int ldb_set_timeout(struct ldb_context *ldb,
     690             :                     struct ldb_request *req,
     691             :                     int timeout)
     692             : {
     693    83101462 :         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
     694             : 
     695    83101462 :         if (timeout != 0) {
     696      350827 :                 req->timeout = timeout;
     697             :         } else {
     698    82750635 :                 req->timeout = ldb->default_timeout;
     699             :         }
     700    83101462 :         req->starttime = time(NULL);
     701             : 
     702    83101462 :         return LDB_SUCCESS;
     703             : }
     704             : 
     705             : /* calculates the new timeout based on the previous starttime and timeout */
     706   589937906 : int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
     707             :                                   struct ldb_request *oldreq,
     708             :                                   struct ldb_request *newreq)
     709             : {
     710   589937906 :         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
     711             : 
     712   589937906 :         if (oldreq == NULL) {
     713    69580919 :                 return ldb_set_timeout(ldb, newreq, 0);
     714             :         }
     715             : 
     716   520356987 :         newreq->starttime = oldreq->starttime;
     717   520356987 :         newreq->timeout = oldreq->timeout;
     718             : 
     719   520356987 :         return LDB_SUCCESS;
     720             : }
     721             : 
     722             : 
     723    81283343 : struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
     724             : {
     725     3039188 :         struct ldb_handle *h;
     726             : 
     727    81283343 :         h = talloc_zero(mem_ctx, struct ldb_handle);
     728    81283343 :         if (h == NULL) {
     729           0 :                 ldb_set_errstring(ldb, "Out of Memory");
     730           0 :                 return NULL;
     731             :         }
     732             : 
     733    81283343 :         h->status = LDB_SUCCESS;
     734    81283343 :         h->state = LDB_ASYNC_INIT;
     735    81283343 :         h->ldb = ldb;
     736    81283343 :         h->flags = 0;
     737    81283343 :         h->location = NULL;
     738    81283343 :         h->parent = NULL;
     739             : 
     740    81283343 :         if (h->ldb->require_private_event_context == true) {
     741    80730940 :                 h->event_context = tevent_context_init(h);
     742    80730940 :                 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    80730940 :                 tevent_set_debug(h->event_context, ldb_tevent_debug, ldb);
     749    80730940 :                 tevent_set_max_debug_level(h->event_context, TEVENT_DEBUG_TRACE);
     750    80730940 :                 tevent_loop_allow_nesting(h->event_context);
     751             :         }
     752             : 
     753    78244155 :         return h;
     754             : }
     755             : 
     756   520356987 : static struct ldb_handle *ldb_handle_new_child(TALLOC_CTX *mem_ctx,
     757             :                                                struct ldb_request *parent_req)
     758             : {
     759    18207337 :         struct ldb_handle *h;
     760             : 
     761   520356987 :         h = talloc_zero(mem_ctx, struct ldb_handle);
     762   520356987 :         if (h == NULL) {
     763           0 :                 ldb_set_errstring(parent_req->handle->ldb,
     764             :                                   "Out of Memory");
     765           0 :                 return NULL;
     766             :         }
     767             : 
     768   520356987 :         h->status = LDB_SUCCESS;
     769   520356987 :         h->state = LDB_ASYNC_INIT;
     770   520356987 :         h->ldb = parent_req->handle->ldb;
     771   520356987 :         h->parent = parent_req;
     772   520356987 :         h->nesting = parent_req->handle->nesting + 1;
     773   520356987 :         h->flags = parent_req->handle->flags;
     774   520356987 :         h->custom_flags = parent_req->handle->custom_flags;
     775   520356987 :         h->event_context = parent_req->handle->event_context;
     776             : 
     777   520356987 :         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     1513641 : void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
     785             : {
     786     1513641 :         ldb->create_perms = perms;
     787     1513641 : }
     788             : 
     789     2216047 : unsigned int ldb_get_create_perms(struct ldb_context *ldb)
     790             : {
     791     2216047 :         return ldb->create_perms;
     792             : }
     793             : 
     794     1785913 : void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
     795             : {
     796     1785913 :         ldb->ev_ctx = ev;
     797     1785913 : }
     798             : 
     799   272999283 : struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
     800             : {
     801   272999283 :         return ldb->ev_ctx;
     802             : }
     803             : 
     804   204148241 : void ldb_request_set_state(struct ldb_request *req, int state)
     805             : {
     806   204148241 :         req->handle->state = state;
     807   204148241 : }
     808             : 
     809   203490491 : int ldb_request_get_status(struct ldb_request *req)
     810             : {
     811   203490491 :         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   302738839 : struct tevent_context *ldb_handle_get_event_context(struct ldb_handle *handle)
     820             : {
     821   302738839 :         if (handle->event_context != NULL) {
     822   289670465 :                 return handle->event_context;
     823             :         }
     824     4020677 :         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     2723525 : void ldb_set_require_private_event_context(struct ldb_context *ldb)
     843             : {
     844     2723525 :         ldb->require_private_event_context = true;
     845     2723525 : }
     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     1512413 : static int ldb_msg_check_element_flags(struct ldb_context *ldb,
     963             :                                        const struct ldb_message *message)
     964             : {
     965      108624 :         unsigned i;
     966     8771012 :         for (i=0; i<message->num_elements; i++) {
     967     7258599 :                 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     1403789 :         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    28821677 : static int ldb_db_lock_destructor(struct ldb_db_lock_context *lock_context)
     994             : {
     995     1298736 :         int ret;
     996     1298736 :         struct ldb_module *next_module;
     997   132896316 :         FIRST_OP_NOERR(lock_context->ldb, read_unlock);
     998    28821677 :         if (next_module != NULL) {
     999    28821677 :                 ret = next_module->ops->read_unlock(next_module);
    1000             :         } else {
    1001           0 :                 ret = LDB_SUCCESS;
    1002             :         }
    1003             : 
    1004    28821677 :         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    28821677 :         return 0;
    1012             : }
    1013             : 
    1014    60149764 : static int ldb_lock_backend_callback(struct ldb_request *req,
    1015             :                                      struct ldb_reply *ares)
    1016             : {
    1017     2406402 :         struct ldb_db_lock_context *lock_context;
    1018     2406402 :         int ret;
    1019             : 
    1020    60149764 :         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    60149764 :         lock_context = talloc_get_type(req->context,
    1028             :                                        struct ldb_db_lock_context);
    1029             : 
    1030    60149764 :         if (!ares) {
    1031           0 :                 return ldb_module_done(lock_context->req, NULL, NULL,
    1032             :                                         LDB_ERR_OPERATIONS_ERROR);
    1033             :         }
    1034    60149764 :         if (ares->error != LDB_SUCCESS || ares->type == LDB_REPLY_DONE) {
    1035    28821638 :                 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    28821636 :                 TALLOC_FREE(req->context);
    1042    28821636 :                 return ret;
    1043             :         }
    1044             : 
    1045             :         /* Otherwise pass on the callback */
    1046    31328126 :         switch (ares->type) {
    1047    26467667 :         case LDB_REPLY_ENTRY:
    1048    26467667 :                 return ldb_module_send_entry(lock_context->req, ares->message,
    1049             :                                              ares->controls);
    1050             : 
    1051     4860459 :         case LDB_REPLY_REFERRAL:
    1052     4860459 :                 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    29142873 : 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    29142873 :         struct ldb_module *next_module = NULL;
    1068    29142873 :         struct ldb_request *down_req = NULL;
    1069     1299038 :         struct ldb_db_lock_context *lock_context;
    1070    29142873 :         struct ldb_context *ldb = ldb_module_get_ctx(lock_module);
    1071     1299038 :         int ret;
    1072             : 
    1073    29142873 :         lock_context = talloc(req, struct ldb_db_lock_context);
    1074    29142873 :         if (lock_context == NULL) {
    1075           0 :                 return ldb_oom(ldb);
    1076             :         }
    1077             : 
    1078    29142873 :         lock_context->ldb = ldb;
    1079    29142873 :         lock_context->req = req;
    1080             : 
    1081    29142873 :         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    29142873 :         LDB_REQ_SET_LOCATION(down_req);
    1091    29142873 :         if (ret != LDB_SUCCESS) {
    1092           0 :                 return ret;
    1093             :         }
    1094             : 
    1095             :         /* call DB lock */
    1096   133605267 :         FIRST_OP_NOERR(ldb, read_lock);
    1097    29142873 :         if (next_module != NULL) {
    1098    28821691 :                 ret = next_module->ops->read_lock(next_module);
    1099             :         } else {
    1100      320880 :                 ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
    1101             :         }
    1102             : 
    1103    29142571 :         if (ret == LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION) {
    1104             :                 /* We might be talking LDAP */
    1105      321182 :                 ldb_reset_err_string(ldb);
    1106      321182 :                 TALLOC_FREE(lock_context);
    1107             : 
    1108      321182 :                 return ldb_next_request(lock_module, req);
    1109    28821691 :         } 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    28821691 :                 talloc_set_destructor(lock_context, ldb_db_lock_destructor);
    1115             :         }
    1116             : 
    1117    28821691 :         if (ret != LDB_SUCCESS) {
    1118           2 :                 return ret;
    1119             :         }
    1120             : 
    1121    28821689 :         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    44476981 : int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
    1130             : {
    1131     1704474 :         struct ldb_module *next_module;
    1132     1704474 :         int ret;
    1133             : 
    1134    44476981 :         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    44476981 :         ldb_reset_err_string(ldb);
    1140             : 
    1141    44476981 :         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    44476981 :         switch (req->operation) {
    1147    29143017 :         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     1299038 :                 static const struct ldb_module_ops lock_module_ops = {
    1154             :                         .name = "lock_searches",
    1155             :                         .search = lock_search
    1156             :                 };
    1157    29143017 :                 struct ldb_module lock_module = {
    1158             :                         .ldb = ldb,
    1159    29143017 :                         .next = ldb->modules,
    1160             :                         .ops = &lock_module_ops
    1161             :                 };
    1162    29143017 :                 next_module = &lock_module;
    1163             : 
    1164             :                 /* due to "ldb_build_search_req" base DN always != NULL */
    1165    29143017 :                 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    29142873 :                 ret = next_module->ops->search(next_module, req);
    1172    29142873 :                 break;
    1173             :         }
    1174      919992 :         case LDB_ADD:
    1175      919992 :                 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     1009416 :                 ret = ldb_msg_normalize(ldb, req, req->op.add.message,
    1186      919972 :                                         discard_const(&req->op.add.message));
    1187      919972 :                 if (ret != LDB_SUCCESS) {
    1188           0 :                         ldb_oom(ldb);
    1189           0 :                         return ret;
    1190             :                 }
    1191     1525568 :                 FIRST_OP(ldb, add);
    1192      919972 :                 ret = ldb_msg_check_element_flags(ldb, req->op.add.message);
    1193      919972 :                 if (ret != LDB_SUCCESS) {
    1194             :                         /*
    1195             :                          * "ldb_msg_check_element_flags" generates an error
    1196             :                          * string
    1197             :                          */
    1198           0 :                         return ret;
    1199             :                 }
    1200      919972 :                 ret = next_module->ops->add(next_module, req);
    1201      919972 :                 break;
    1202      592441 :         case LDB_MODIFY:
    1203      592441 :                 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     1045443 :                 FIRST_OP(ldb, modify);
    1209      592441 :                 ret = ldb_msg_check_element_flags(ldb, req->op.mod.message);
    1210      592441 :                 if (ret != LDB_SUCCESS) {
    1211             :                         /*
    1212             :                          * "ldb_msg_check_element_flags" generates an error
    1213             :                          * string
    1214             :                          */
    1215           0 :                         return ret;
    1216             :                 }
    1217      592441 :                 ret = next_module->ops->modify(next_module, req);
    1218      592441 :                 break;
    1219      284203 :         case LDB_DELETE:
    1220      284203 :                 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      597567 :                 FIRST_OP(ldb, del);
    1226      284199 :                 ret = next_module->ops->del(next_module, req);
    1227      284199 :                 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     1832920 :         case LDB_EXTENDED:
    1243     5579393 :                 FIRST_OP(ldb, extended);
    1244     1832920 :                 ret = next_module->ops->extended(next_module, req);
    1245     1832920 :                 break;
    1246    11702424 :         default:
    1247    35486928 :                 FIRST_OP(ldb, request);
    1248    11382157 :                 ret = next_module->ops->request(next_module, req);
    1249    11382157 :                 break;
    1250             :         }
    1251             : 
    1252    44156499 :         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    42458071 :         return ret;
    1259             : }
    1260             : 
    1261   109854004 : int ldb_request_done(struct ldb_request *req, int status)
    1262             : {
    1263   109854004 :         req->handle->state = LDB_ASYNC_DONE;
    1264   109854004 :         req->handle->status = status;
    1265   109854004 :         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   211594400 : int ldb_search_default_callback(struct ldb_request *req,
    1277             :                                 struct ldb_reply *ares)
    1278             : {
    1279     5222822 :         struct ldb_result *res;
    1280     5222822 :         unsigned int n;
    1281             : 
    1282   211594400 :         res = talloc_get_type(req->context, struct ldb_result);
    1283             : 
    1284   211594400 :         if (!ares) {
    1285           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1286             :         }
    1287   211594400 :         if (ares->error != LDB_SUCCESS) {
    1288     2061958 :                 return ldb_request_done(req, ares->error);
    1289             :         }
    1290             : 
    1291   209532442 :         switch (ares->type) {
    1292   146723383 :         case LDB_REPLY_ENTRY:
    1293   146723383 :                 res->msgs = talloc_realloc(res, res->msgs,
    1294             :                                         struct ldb_message *, res->count + 2);
    1295   146723383 :                 if (! res->msgs) {
    1296           0 :                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1297             :                 }
    1298             : 
    1299   146723383 :                 res->msgs[res->count + 1] = NULL;
    1300             : 
    1301   146723383 :                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
    1302   146723383 :                 res->count++;
    1303   146723383 :                 break;
    1304             : 
    1305     5657270 :         case LDB_REPLY_REFERRAL:
    1306     5657270 :                 if (res->refs) {
    1307     8831900 :                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
    1308             :                 } else {
    1309     2076627 :                         n = 0;
    1310             :                 }
    1311             : 
    1312     5657270 :                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
    1313     5657270 :                 if (! res->refs) {
    1314           0 :                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1315             :                 }
    1316             : 
    1317     5657270 :                 res->refs[n] = talloc_move(res->refs, &ares->referral);
    1318     5657270 :                 res->refs[n + 1] = NULL;
    1319     5657270 :                 break;
    1320             : 
    1321    57151789 :         case LDB_REPLY_DONE:
    1322             :                 /* TODO: we should really support controls on entries
    1323             :                  * and referrals too! */
    1324    57151789 :                 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    57151789 :                 talloc_free(ares);
    1330    57151789 :                 return ldb_request_done(req, LDB_SUCCESS);
    1331             :         }
    1332             : 
    1333   152380653 :         talloc_free(ares);
    1334             : 
    1335   152380653 :         return LDB_SUCCESS;
    1336             : }
    1337             : 
    1338     2514725 : 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     2514725 :         res = talloc_get_type(req->context, struct ldb_result);
    1345             : 
    1346     2514725 :         if (!ares) {
    1347           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1348             :         }
    1349             : 
    1350     2514725 :         if (ares->error != LDB_SUCCESS) {
    1351       46993 :                 ret = ares->error;
    1352       46993 :                 talloc_free(ares);
    1353       46993 :                 return ldb_request_done(req, ret);
    1354             :         }
    1355             : 
    1356     2467732 :         switch (ares->type) {
    1357          18 :         case LDB_REPLY_REFERRAL:
    1358          18 :                 if (res->refs) {
    1359           0 :                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
    1360             :                 } else {
    1361          18 :                         n = 0;
    1362             :                 }
    1363             : 
    1364          18 :                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
    1365          18 :                 if (! res->refs) {
    1366           0 :                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1367             :                 }
    1368             : 
    1369          18 :                 res->refs[n] = talloc_move(res->refs, &ares->referral);
    1370          18 :                 res->refs[n + 1] = NULL;
    1371          18 :                 break;
    1372             : 
    1373     2467714 :         case LDB_REPLY_DONE:
    1374     2467714 :                 talloc_free(ares);
    1375     2467714 :                 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          18 :         talloc_free(ares);
    1383          18 :         return ldb_request_done(req, LDB_SUCCESS);
    1384             : }
    1385             : 
    1386    12905570 : int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
    1387             : {
    1388      303610 :         int ret;
    1389             : 
    1390    12905570 :         if (!ares) {
    1391           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1392             :         }
    1393             : 
    1394    12905570 :         if (ares->error != LDB_SUCCESS) {
    1395       93132 :                 ret = ares->error;
    1396       93132 :                 talloc_free(ares);
    1397       93132 :                 return ldb_request_done(req, ret);
    1398             :         }
    1399             : 
    1400    12812438 :         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    12812074 :         talloc_free(ares);
    1407    12812074 :         return ldb_request_done(req, LDB_SUCCESS);
    1408             : }
    1409             : 
    1410   589937906 : 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   589937906 :         struct ldb_request *req = NULL;
    1418             : 
    1419   589937906 :         req = talloc_zero(mem_ctx, struct ldb_request);
    1420   589937906 :         if (req == NULL) {
    1421           0 :                 return NULL;
    1422             :         }
    1423   589937906 :         req->controls = controls;
    1424   589937906 :         req->context = context;
    1425   589937906 :         req->callback = callback;
    1426             : 
    1427   589937906 :         ldb_set_timeout_from_prev_req(ldb, parent, req);
    1428             : 
    1429   589937906 :         if (parent != NULL) {
    1430   520356987 :                 req->handle = ldb_handle_new_child(req, parent);
    1431   520356987 :                 if (req->handle == NULL) {
    1432           0 :                         TALLOC_FREE(req);
    1433           0 :                         return NULL;
    1434             :                 }
    1435             :         } else {
    1436    69580919 :                 req->handle = ldb_handle_new(req, ldb);
    1437    69580919 :                 if (req->handle == NULL) {
    1438           0 :                         TALLOC_FREE(req);
    1439           0 :                         return NULL;
    1440             :                 }
    1441             :         }
    1442             : 
    1443   568891117 :         return req;
    1444             : }
    1445             : 
    1446   546186622 : 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    18789405 :         struct ldb_request *req;
    1459             : 
    1460   546186622 :         *ret_req = NULL;
    1461             : 
    1462   546186622 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1463             :                                    context, callback, parent);
    1464   546186622 :         if (req == NULL) {
    1465           0 :                 ldb_oom(ldb);
    1466           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1467             :         }
    1468             : 
    1469   546186622 :         req->operation = LDB_SEARCH;
    1470   546186622 :         if (base == NULL) {
    1471    26243277 :                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
    1472    26243277 :                 if (req->op.search.base == NULL) {
    1473           0 :                         ldb_oom(ldb);
    1474           0 :                         return LDB_ERR_OPERATIONS_ERROR;
    1475             :                 }
    1476             :         } else {
    1477   519943345 :                 req->op.search.base = base;
    1478             :         }
    1479   546186622 :         req->op.search.scope = scope;
    1480             : 
    1481   546186622 :         req->op.search.tree = tree;
    1482   546186622 :         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   546186622 :         req->op.search.attrs = attrs;
    1489   546186622 :         *ret_req = req;
    1490   546186622 :         return LDB_SUCCESS;
    1491             : }
    1492             : 
    1493    70209269 : 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     2616386 :         struct ldb_parse_tree *tree;
    1506     2616386 :         int ret;
    1507             : 
    1508    70209269 :         tree = ldb_parse_tree(mem_ctx, expression);
    1509    70209269 :         if (tree == NULL) {
    1510          12 :                 ldb_set_errstring(ldb, "Unable to parse search expression");
    1511          12 :                 return LDB_ERR_OPERATIONS_ERROR;
    1512             :         }
    1513             : 
    1514    70209257 :         ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
    1515             :                                       scope, tree, attrs, controls,
    1516             :                                       context, callback, parent);
    1517    70209257 :         if (ret == LDB_SUCCESS) {
    1518    70209257 :                 talloc_steal(*ret_req, tree);
    1519             :         }
    1520    67592874 :         return ret;
    1521             : }
    1522             : 
    1523     6863579 : 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      807801 :         struct ldb_request *req;
    1533             : 
    1534     6863579 :         *ret_req = NULL;
    1535             : 
    1536     6863579 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1537             :                                    context, callback, parent);
    1538     6863579 :         if (req == NULL) {
    1539           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1540           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1541             :         }
    1542             : 
    1543     6863579 :         req->operation = LDB_ADD;
    1544     6863579 :         req->op.add.message = message;
    1545     6863579 :         *ret_req = req;
    1546     6863579 :         return LDB_SUCCESS;
    1547             : }
    1548             : 
    1549     7131883 : 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      195714 :         struct ldb_request *req;
    1559             : 
    1560     7131883 :         *ret_req = NULL;
    1561             : 
    1562     7131883 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1563             :                                    context, callback, parent);
    1564     7131883 :         if (req == NULL) {
    1565           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1566           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1567             :         }
    1568             : 
    1569     7131883 :         req->operation = LDB_MODIFY;
    1570     7131883 :         req->op.mod.message = message;
    1571             : 
    1572     7131883 :         *ret_req = req;
    1573     7131883 :         return LDB_SUCCESS;
    1574             : }
    1575             : 
    1576      601666 : 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      601666 :         *ret_req = NULL;
    1588             : 
    1589      601666 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1590             :                                    context, callback, parent);
    1591      601666 :         if (req == NULL) {
    1592           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1593           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1594             :         }
    1595             : 
    1596      601666 :         req->operation = LDB_DELETE;
    1597      601666 :         req->op.del.dn = dn;
    1598      601666 :         *ret_req = req;
    1599      601666 :         return LDB_SUCCESS;
    1600             : }
    1601             : 
    1602      340529 : 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      340529 :         *ret_req = NULL;
    1615             : 
    1616      340529 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1617             :                                    context, callback, parent);
    1618      340529 :         if (req == NULL) {
    1619           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1620           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1621             :         }
    1622             : 
    1623      340529 :         req->operation = LDB_RENAME;
    1624      340529 :         req->op.rename.olddn = olddn;
    1625      340529 :         req->op.rename.newdn = newdn;
    1626      340529 :         *ret_req = req;
    1627      340529 :         return LDB_SUCCESS;
    1628             : }
    1629             : 
    1630    28808864 : int ldb_extended_default_callback(struct ldb_request *req,
    1631             :                                   struct ldb_reply *ares)
    1632             : {
    1633     1251905 :         struct ldb_result *res;
    1634             : 
    1635    28808864 :         res = talloc_get_type(req->context, struct ldb_result);
    1636             : 
    1637    28808864 :         if (!ares) {
    1638           0 :                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
    1639             :         }
    1640    28808864 :         if (ares->error != LDB_SUCCESS) {
    1641         385 :                 return ldb_request_done(req, ares->error);
    1642             :         }
    1643             : 
    1644    28808479 :         if (ares->type == LDB_REPLY_DONE) {
    1645             : 
    1646             :                 /* TODO: we should really support controls on entries and referrals too! */
    1647    28808479 :                 res->extended = talloc_move(res, &ares->response);
    1648    28808479 :                 res->controls = talloc_move(res, &ares->controls);
    1649             : 
    1650    28808479 :                 talloc_free(ares);
    1651    28808479 :                 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    28813627 : 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     1252013 :         struct ldb_request *req;
    1670             : 
    1671    28813627 :         *ret_req = NULL;
    1672             : 
    1673    28813627 :         req = ldb_build_req_common(mem_ctx, ldb, controls,
    1674             :                                    context, callback, parent);
    1675    28813627 :         if (req == NULL) {
    1676           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1677           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1678             :         }
    1679             : 
    1680    28813627 :         req->operation = LDB_EXTENDED;
    1681    28813627 :         req->op.extended.oid = oid;
    1682    28813627 :         req->op.extended.data = data;
    1683    28813627 :         *ret_req = req;
    1684    28813627 :         return LDB_SUCCESS;
    1685             : }
    1686             : 
    1687     1463440 : 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     1463440 :         *_res = NULL;
    1697     1463440 :         req = NULL;
    1698             : 
    1699     1463440 :         res = talloc_zero(ldb, struct ldb_result);
    1700     1463440 :         if (!res) {
    1701           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1702             :         }
    1703             : 
    1704     1463440 :         ret = ldb_build_extended_req(&req, ldb, ldb,
    1705             :                                      oid, data, NULL,
    1706             :                                      res, ldb_extended_default_callback,
    1707             :                                      NULL);
    1708     1463440 :         ldb_req_set_location(req, "ldb_extended");
    1709             : 
    1710     1463440 :         if (ret != LDB_SUCCESS) goto done;
    1711             : 
    1712     1463440 :         ldb_set_timeout(ldb, req, 0); /* use default timeout */
    1713             : 
    1714     1463440 :         ret = ldb_request(ldb, req);
    1715             : 
    1716     1463440 :         if (ret == LDB_SUCCESS) {
    1717     1463434 :                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
    1718             :         }
    1719             : 
    1720           6 : done:
    1721     1463440 :         if (ret != LDB_SUCCESS) {
    1722         385 :                 talloc_free(res);
    1723         385 :                 res = NULL;
    1724             :         }
    1725             : 
    1726     1463440 :         talloc_free(req);
    1727             : 
    1728     1463440 :         *_res = res;
    1729     1463440 :         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     7448861 : 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      248487 :         struct ldb_request *req;
    1742      248487 :         struct ldb_result *res;
    1743      248487 :         char *expression;
    1744      248487 :         va_list ap;
    1745      248487 :         int ret;
    1746             : 
    1747     7448861 :         expression = NULL;
    1748     7448861 :         *result = NULL;
    1749     7448861 :         req = NULL;
    1750             : 
    1751     7448861 :         res = talloc_zero(mem_ctx, struct ldb_result);
    1752     7448861 :         if (!res) {
    1753           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1754             :         }
    1755             : 
    1756     7448861 :         if (exp_fmt) {
    1757     4820478 :                 va_start(ap, exp_fmt);
    1758     4820478 :                 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
    1759     4820478 :                 va_end(ap);
    1760             : 
    1761     4820478 :                 if (!expression) {
    1762           0 :                         talloc_free(res);
    1763           0 :                         return LDB_ERR_OPERATIONS_ERROR;
    1764             :                 }
    1765             :         }
    1766             : 
    1767     9098916 :         ret = ldb_build_search_req(&req, ldb, mem_ctx,
    1768     1650055 :                                         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     7448861 :         ldb_req_set_location(req, "ldb_search");
    1777             : 
    1778     7448861 :         if (ret != LDB_SUCCESS) goto done;
    1779             : 
    1780     7448861 :         ret = ldb_request(ldb, req);
    1781             : 
    1782     7448861 :         if (ret == LDB_SUCCESS) {
    1783     7447879 :                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
    1784             :         }
    1785             : 
    1786         982 : done:
    1787     7448861 :         if (ret != LDB_SUCCESS) {
    1788      322631 :                 talloc_free(res);
    1789      322631 :                 res = NULL;
    1790             :         }
    1791             : 
    1792     7448861 :         talloc_free(expression);
    1793     7448861 :         talloc_free(req);
    1794             : 
    1795     7448861 :         *result = res;
    1796     7448861 :         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      157952 : int ldb_add(struct ldb_context *ldb,
    1804             :             const struct ldb_message *message)
    1805             : {
    1806        3365 :         struct ldb_request *req;
    1807        3365 :         int ret;
    1808             : 
    1809      157952 :         ret = ldb_msg_sanity_check(ldb, message);
    1810      157952 :         if (ret != LDB_SUCCESS) {
    1811           0 :                 return ret;
    1812             :         }
    1813             : 
    1814      157952 :         ret = ldb_build_add_req(&req, ldb, ldb,
    1815             :                                         message,
    1816             :                                         NULL,
    1817             :                                         NULL,
    1818             :                                         ldb_op_default_callback,
    1819             :                                         NULL);
    1820      157952 :         ldb_req_set_location(req, "ldb_add");
    1821             : 
    1822      157952 :         if (ret != LDB_SUCCESS) return ret;
    1823             : 
    1824             :         /* do request and autostart a transaction */
    1825      157952 :         ret = ldb_autotransaction_request(ldb, req);
    1826             : 
    1827      157952 :         talloc_free(req);
    1828      157952 :         return ret;
    1829             : }
    1830             : 
    1831             : /*
    1832             :   modify the specified attributes of a record
    1833             : */
    1834      167489 : int ldb_modify(struct ldb_context *ldb,
    1835             :                const struct ldb_message *message)
    1836             : {
    1837        1924 :         struct ldb_request *req;
    1838        1924 :         int ret;
    1839             : 
    1840      167489 :         ret = ldb_msg_sanity_check(ldb, message);
    1841      167489 :         if (ret != LDB_SUCCESS) {
    1842           0 :                 return ret;
    1843             :         }
    1844             : 
    1845      167489 :         ret = ldb_build_mod_req(&req, ldb, ldb,
    1846             :                                         message,
    1847             :                                         NULL,
    1848             :                                         NULL,
    1849             :                                         ldb_op_default_callback,
    1850             :                                         NULL);
    1851      167489 :         ldb_req_set_location(req, "ldb_modify");
    1852             : 
    1853      167489 :         if (ret != LDB_SUCCESS) return ret;
    1854             : 
    1855             :         /* do request and autostart a transaction */
    1856      167489 :         ret = ldb_autotransaction_request(ldb, req);
    1857             : 
    1858      167489 :         talloc_free(req);
    1859      167489 :         return ret;
    1860             : }
    1861             : 
    1862             : 
    1863             : /*
    1864             :   delete a record from the database
    1865             : */
    1866       87107 : int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
    1867             : {
    1868         529 :         struct ldb_request *req;
    1869         529 :         int ret;
    1870             : 
    1871       87107 :         ret = ldb_build_del_req(&req, ldb, ldb,
    1872             :                                         dn,
    1873             :                                         NULL,
    1874             :                                         NULL,
    1875             :                                         ldb_op_default_callback,
    1876             :                                         NULL);
    1877       87107 :         ldb_req_set_location(req, "ldb_delete");
    1878             : 
    1879       87107 :         if (ret != LDB_SUCCESS) return ret;
    1880             : 
    1881             :         /* do request and autostart a transaction */
    1882       87107 :         ret = ldb_autotransaction_request(ldb, req);
    1883             : 
    1884       87107 :         talloc_free(req);
    1885       87107 :         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     1457738 : 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     1457738 :         *seq_num = 0;
    1929             : 
    1930     1457738 :         tmp_ctx = talloc_zero(ldb, struct ldb_request);
    1931     1457738 :         if (tmp_ctx == NULL) {
    1932           0 :                 ldb_set_errstring(ldb, "Out of Memory");
    1933           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1934             :         }
    1935     1457738 :         seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
    1936     1457738 :         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     1457738 :         seq->type = type;
    1942             : 
    1943     1457738 :         ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
    1944     1457738 :         if (ret != LDB_SUCCESS) {
    1945         315 :                 goto done;
    1946             :         }
    1947     1457423 :         talloc_steal(tmp_ctx, res);
    1948             : 
    1949     1457423 :         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     1457423 :         seqr = talloc_get_type(res->extended->data,
    1955             :                                 struct ldb_seqnum_result);
    1956     1457423 :         *seq_num = seqr->seq_num;
    1957             : 
    1958     1457738 : done:
    1959     1457738 :         talloc_free(tmp_ctx);
    1960     1457738 :         return ret;
    1961             : }
    1962             : 
    1963             : /*
    1964             :   return extended error information
    1965             : */
    1966      494835 : const char *ldb_errstring(struct ldb_context *ldb)
    1967             : {
    1968      494835 :         if (ldb->err_string) {
    1969      307709 :                 return ldb->err_string;
    1970             :         }
    1971             : 
    1972      186886 :         return NULL;
    1973             : }
    1974             : 
    1975             : /*
    1976             :   return a string explaining what a ldb error constant meancs
    1977             : */
    1978      775548 : const char *ldb_strerror(int ldb_err)
    1979             : {
    1980      775548 :         switch (ldb_err) {
    1981      706188 :         case LDB_SUCCESS:
    1982      706188 :                 return "Success";
    1983         224 :         case LDB_ERR_OPERATIONS_ERROR:
    1984         224 :                 return "Operations error";
    1985           5 :         case LDB_ERR_PROTOCOL_ERROR:
    1986           5 :                 return "Protocol error";
    1987           9 :         case LDB_ERR_TIME_LIMIT_EXCEEDED:
    1988           9 :                 return "Time limit exceeded";
    1989           0 :         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
    1990           0 :                 return "Size limit exceeded";
    1991           0 :         case LDB_ERR_COMPARE_FALSE:
    1992           0 :                 return "Compare false";
    1993           0 :         case LDB_ERR_COMPARE_TRUE:
    1994           0 :                 return "Compare true";
    1995           0 :         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
    1996           0 :                 return "Auth method not supported";
    1997           0 :         case LDB_ERR_STRONG_AUTH_REQUIRED:
    1998           0 :                 return "Strong auth required";
    1999             : /* 9 RESERVED */
    2000           8 :         case LDB_ERR_REFERRAL:
    2001           8 :                 return "Referral error";
    2002           0 :         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
    2003           0 :                 return "Admin limit exceeded";
    2004          10 :         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
    2005          10 :                 return "Unsupported critical extension";
    2006           0 :         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
    2007           0 :                 return "Confidentiality required";
    2008           0 :         case LDB_ERR_SASL_BIND_IN_PROGRESS:
    2009           0 :                 return "SASL bind in progress";
    2010         577 :         case LDB_ERR_NO_SUCH_ATTRIBUTE:
    2011         577 :                 return "No such attribute";
    2012           2 :         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
    2013           2 :                 return "Undefined attribute type";
    2014           0 :         case LDB_ERR_INAPPROPRIATE_MATCHING:
    2015           0 :                 return "Inappropriate matching";
    2016        3981 :         case LDB_ERR_CONSTRAINT_VIOLATION:
    2017        3981 :                 return "Constraint violation";
    2018         131 :         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
    2019         131 :                 return "Attribute or value exists";
    2020          68 :         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
    2021          68 :                 return "Invalid attribute syntax";
    2022             : /* 22-31 unused */
    2023       50582 :         case LDB_ERR_NO_SUCH_OBJECT:
    2024       50582 :                 return "No such object";
    2025           0 :         case LDB_ERR_ALIAS_PROBLEM:
    2026           0 :                 return "Alias problem";
    2027         188 :         case LDB_ERR_INVALID_DN_SYNTAX:
    2028         188 :                 return "Invalid DN syntax";
    2029             : /* 35 RESERVED */
    2030           0 :         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
    2031           0 :                 return "Alias dereferencing problem";
    2032             : /* 37-47 unused */
    2033           0 :         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
    2034           0 :                 return "Inappropriate authentication";
    2035           0 :         case LDB_ERR_INVALID_CREDENTIALS:
    2036           0 :                 return "Invalid credentials";
    2037        1815 :         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
    2038        1815 :                 return "insufficient access rights";
    2039           0 :         case LDB_ERR_BUSY:
    2040           0 :                 return "Busy";
    2041           0 :         case LDB_ERR_UNAVAILABLE:
    2042           0 :                 return "Unavailable";
    2043         530 :         case LDB_ERR_UNWILLING_TO_PERFORM:
    2044         530 :                 return "Unwilling to perform";
    2045           0 :         case LDB_ERR_LOOP_DETECT:
    2046           0 :                 return "Loop detect";
    2047             : /* 55-63 unused */
    2048           5 :         case LDB_ERR_NAMING_VIOLATION:
    2049           5 :                 return "Naming violation";
    2050         421 :         case LDB_ERR_OBJECT_CLASS_VIOLATION:
    2051         421 :                 return "Object class violation";
    2052          26 :         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
    2053          26 :                 return "Not allowed on non-leaf";
    2054           2 :         case LDB_ERR_NOT_ALLOWED_ON_RDN:
    2055           2 :                 return "Not allowed on RDN";
    2056         292 :         case LDB_ERR_ENTRY_ALREADY_EXISTS:
    2057         292 :                 return "Entry already exists";
    2058           0 :         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
    2059           0 :                 return "Object class mods prohibited";
    2060             : /* 70 RESERVED FOR CLDAP */
    2061           4 :         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
    2062           4 :                 return "Affects multiple DSAs";
    2063             : /* 72-79 unused */
    2064          97 :         case LDB_ERR_OTHER:
    2065          97 :                 return "Other";
    2066             :         }
    2067             : 
    2068           3 :         return "Unknown error";
    2069             : }
    2070             : 
    2071             : /*
    2072             :   set backend specific opaque parameters
    2073             : */
    2074   441488910 : int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
    2075             : {
    2076    22080619 :         struct ldb_opaque *o;
    2077             : 
    2078             :         /* allow updating an existing value */
    2079  3892094949 :         for (o=ldb->opaque;o;o=o->next) {
    2080  3881315857 :                 if (strcmp(o->name, name) == 0) {
    2081   430709818 :                         o->value = value;
    2082   430709818 :                         return LDB_SUCCESS;
    2083             :                 }
    2084             :         }
    2085             : 
    2086    10779092 :         o = talloc(ldb, struct ldb_opaque);
    2087    10779092 :         if (o == NULL) {
    2088           0 :                 ldb_oom(ldb);
    2089           0 :                 return LDB_ERR_OTHER;
    2090             :         }
    2091    10779092 :         o->next = ldb->opaque;
    2092    10779092 :         o->name = name;
    2093    10779092 :         o->value = value;
    2094    10779092 :         ldb->opaque = o;
    2095    10779092 :         return LDB_SUCCESS;
    2096             : }
    2097             : 
    2098             : /*
    2099             :   get a previously set opaque value
    2100             : */
    2101  1177051524 : void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
    2102             : {
    2103    62643505 :         struct ldb_opaque *o;
    2104 17273604206 :         for (o=ldb->opaque;o;o=o->next) {
    2105 17056983138 :                 if (strcmp(o->name, name) == 0) {
    2106   960430456 :                         return o->value;
    2107             :                 }
    2108             :         }
    2109   205194427 :         return NULL;
    2110             : }
    2111             : 
    2112           0 : int ldb_global_init(void)
    2113             : {
    2114             :         /* Provided for compatibility with some older versions of ldb */
    2115           0 :         return 0;
    2116             : }
    2117             : 
    2118             : /* return the ldb flags */
    2119       87347 : unsigned int ldb_get_flags(struct ldb_context *ldb)
    2120             : {
    2121       87347 :         return ldb->flags;
    2122             : }
    2123             : 
    2124             : /* set the ldb flags */
    2125       54061 : void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
    2126             : {
    2127       54061 :         ldb->flags = flags;
    2128       54061 : }
    2129             : 
    2130             : 
    2131             : /*
    2132             :   set the location in a ldb request. Used for debugging
    2133             :  */
    2134   564395890 : void ldb_req_set_location(struct ldb_request *req, const char *location)
    2135             : {
    2136   564395890 :         if (req && req->handle) {
    2137   564395890 :                 req->handle->location = location;
    2138             :         }
    2139   564395890 : }
    2140             : 
    2141             : /*
    2142             :   return the location set with dsdb_req_set_location
    2143             :  */
    2144           0 : const char *ldb_req_location(struct ldb_request *req)
    2145             : {
    2146           0 :         return req->handle->location;
    2147             : }
    2148             : 
    2149             : /**
    2150             :   mark a request as untrusted. This tells the rootdse module to remove
    2151             :   unregistered controls
    2152             :  */
    2153      580669 : void ldb_req_mark_untrusted(struct ldb_request *req)
    2154             : {
    2155      580669 :         req->handle->flags |= LDB_HANDLE_FLAG_UNTRUSTED;
    2156      580669 : }
    2157             : 
    2158             : /**
    2159             :   mark a request as trusted.
    2160             :  */
    2161     1469080 : void ldb_req_mark_trusted(struct ldb_request *req)
    2162             : {
    2163     1469080 :         req->handle->flags &= ~LDB_HANDLE_FLAG_UNTRUSTED;
    2164     1469080 : }
    2165             : 
    2166             : /**
    2167             :   set custom flags. Those flags are set by applications using ldb,
    2168             :   they are application dependent and the same bit can have different
    2169             :   meaning in different application.
    2170             :  */
    2171           0 : void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags)
    2172             : {
    2173           0 :         if (req != NULL && req->handle != NULL) {
    2174           0 :                 req->handle->custom_flags = flags;
    2175             :         }
    2176           0 : }
    2177             : 
    2178             : 
    2179             : /**
    2180             :   get custom flags. Those flags are set by applications using ldb,
    2181             :   they are application dependent and the same bit can have different
    2182             :   meaning in different application.
    2183             :  */
    2184           0 : uint32_t ldb_req_get_custom_flags(struct ldb_request *req)
    2185             : {
    2186           0 :         if (req != NULL && req->handle != NULL) {
    2187           0 :                 return req->handle->custom_flags;
    2188             :         }
    2189             : 
    2190             :         /*
    2191             :          * 0 is not something any better or worse than
    2192             :          * anything else as req or the handle is NULL
    2193             :          */
    2194           0 :         return 0;
    2195             : }
    2196             : 
    2197             : 
    2198             : /**
    2199             :  * return true if a request is untrusted
    2200             :  */
    2201   100471062 : bool ldb_req_is_untrusted(struct ldb_request *req)
    2202             : {
    2203   100471062 :         return (req->handle->flags & LDB_HANDLE_FLAG_UNTRUSTED) != 0;
    2204             : }

Generated by: LCOV version 1.14