LCOV - code coverage report
Current view: top level - lib/ldb/ldb_key_value - ldb_kv.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 851 1080 78.8 %
Date: 2023-11-21 12:31:41 Functions: 41 42 97.6 %

          Line data    Source code
       1             : /*
       2             :    ldb database library
       3             : 
       4             :    Copyright (C) Andrew Tridgell 2004
       5             :    Copyright (C) Stefan Metzmacher 2004
       6             :    Copyright (C) Simo Sorce 2006-2008
       7             :    Copyright (C) Matthias Dieter Wallnöfer 2009-2010
       8             : 
       9             :      ** NOTE! The following LGPL license applies to the ldb
      10             :      ** library. This does NOT imply that all of Samba is released
      11             :      ** under the LGPL
      12             : 
      13             :    This library is free software; you can redistribute it and/or
      14             :    modify it under the terms of the GNU Lesser General Public
      15             :    License as published by the Free Software Foundation; either
      16             :    version 3 of the License, or (at your option) any later version.
      17             : 
      18             :    This library is distributed in the hope that it will be useful,
      19             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      21             :    Lesser General Public License for more details.
      22             : 
      23             :    You should have received a copy of the GNU Lesser General Public
      24             :    License along with this library; if not, see <http://www.gnu.org/licenses/>.
      25             : */
      26             : 
      27             : /*
      28             :  *  Name: ldb_kv
      29             :  *
      30             :  *  Component: ldb key value backend
      31             :  *
      32             :  *  Description: core functions for ldb key value backend
      33             :  *
      34             :  *  Author: Andrew Tridgell
      35             :  *  Author: Stefan Metzmacher
      36             :  *
      37             :  *  Modifications:
      38             :  *
      39             :  *  - description: make the module use asynchronous calls
      40             :  *    date: Feb 2006
      41             :  *    Author: Simo Sorce
      42             :  *
      43             :  *  - description: make it possible to use event contexts
      44             :  *    date: Jan 2008
      45             :  *    Author: Simo Sorce
      46             :  *
      47             :  *  - description: fix up memory leaks and small bugs
      48             :  *    date: Oct 2009
      49             :  *    Author: Matthias Dieter Wallnöfer
      50             :  */
      51             : 
      52             : #include "ldb_kv.h"
      53             : #include "ldb_private.h"
      54             : #include "lib/util/attr.h"
      55             : 
      56             : /*
      57             :   prevent memory errors on callbacks
      58             : */
      59             : struct ldb_kv_req_spy {
      60             :         struct ldb_kv_context *ctx;
      61             : };
      62             : 
      63             : /*
      64             :  * Determine if this key could hold a record.  We allow the new GUID
      65             :  * index, the old DN index and a possible future ID=
      66             :  */
      67   554396949 : bool ldb_kv_key_is_normal_record(struct ldb_val key)
      68             : {
      69   554396949 :         if (key.length < 4) {
      70           0 :                 return false;
      71             :         }
      72             : 
      73             :         /*
      74             :          * @ records are not normal records, we don't want to index
      75             :          * them nor search on them
      76             :          */
      77   554396949 :         if (key.length > 4 &&
      78   554396949 :             memcmp(key.data, "DN=@", 4) == 0) {
      79   442072801 :                 return false;
      80             :         }
      81             : 
      82             :         /* All other DN= records are however */
      83   110273403 :         if (memcmp(key.data, "DN=", 3) == 0) {
      84      374852 :                 return true;
      85             :         }
      86             : 
      87   109837142 :         if (memcmp(key.data, "ID=", 3) == 0) {
      88           0 :                 return true;
      89             :         }
      90             : 
      91   109837142 :         if (key.length < sizeof(LDB_KV_GUID_KEY_PREFIX)) {
      92           0 :                 return false;
      93             :         }
      94             : 
      95   109837142 :         if (memcmp(key.data, LDB_KV_GUID_KEY_PREFIX,
      96             :                    sizeof(LDB_KV_GUID_KEY_PREFIX) - 1) == 0) {
      97   109837142 :                 return true;
      98             :         }
      99             : 
     100           0 :         return false;
     101             : }
     102             : 
     103             : /*
     104             :   form a ldb_val for a record key
     105             :   caller frees
     106             : 
     107             :   note that the key for a record can depend on whether the
     108             :   dn refers to a case sensitive index record or not
     109             : */
     110   264076047 : struct ldb_val ldb_kv_key_dn(TALLOC_CTX *mem_ctx,
     111             :                              struct ldb_dn *dn)
     112             : {
     113    10667976 :         struct ldb_val key;
     114   264076047 :         char *key_str = NULL;
     115   264076047 :         const char *dn_folded = NULL;
     116             : 
     117             :         /*
     118             :           most DNs are case insensitive. The exception is index DNs for
     119             :           case sensitive attributes
     120             : 
     121             :           there are 3 cases dealt with in this code:
     122             : 
     123             :           1) if the dn doesn't start with @ then uppercase the attribute
     124             :              names and the attributes values of case insensitive attributes
     125             :           2) if the dn starts with @ then leave it alone -
     126             :              the indexing code handles the rest
     127             :         */
     128             : 
     129   264076047 :         dn_folded = ldb_dn_get_casefold(dn);
     130   264076047 :         if (!dn_folded) {
     131           0 :                 goto failed;
     132             :         }
     133             : 
     134   264076047 :         key_str = talloc_strdup(mem_ctx, "DN=");
     135   264076047 :         if (!key_str) {
     136           0 :                 goto failed;
     137             :         }
     138             : 
     139   264076047 :         key_str = talloc_strdup_append_buffer(key_str, dn_folded);
     140   264076047 :         if (!key_str) {
     141           0 :                 goto failed;
     142             :         }
     143             : 
     144   264076047 :         key.data = (uint8_t *)key_str;
     145   264076047 :         key.length = strlen(key_str) + 1;
     146             : 
     147   264076047 :         return key;
     148             : 
     149           0 : failed:
     150           0 :         errno = ENOMEM;
     151           0 :         key.data = NULL;
     152           0 :         key.length = 0;
     153           0 :         return key;
     154             : }
     155             : 
     156             : /* The caller is to provide a correctly sized key */
     157   269097055 : int ldb_kv_guid_to_key(const struct ldb_val *GUID_val,
     158             :                        struct ldb_val *key)
     159             : {
     160   269097055 :         const char *GUID_prefix = LDB_KV_GUID_KEY_PREFIX;
     161   269097055 :         const int GUID_prefix_len = sizeof(LDB_KV_GUID_KEY_PREFIX) - 1;
     162             : 
     163   269097055 :         if (key->length != (GUID_val->length+GUID_prefix_len)) {
     164           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     165             :         }
     166             : 
     167   269097055 :         memcpy(key->data, GUID_prefix, GUID_prefix_len);
     168   269097055 :         memcpy(&key->data[GUID_prefix_len],
     169   269097055 :                GUID_val->data, GUID_val->length);
     170   269097055 :         return LDB_SUCCESS;
     171             : }
     172             : 
     173             : /*
     174             :  * The caller is to provide a correctly sized key, used only in
     175             :  * the GUID index mode
     176             :  */
     177   141445000 : int ldb_kv_idx_to_key(struct ldb_module *module,
     178             :                       struct ldb_kv_private *ldb_kv,
     179             :                       TALLOC_CTX *mem_ctx,
     180             :                       const struct ldb_val *idx_val,
     181             :                       struct ldb_val *key)
     182             : {
     183   141445000 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
     184     2615928 :         struct ldb_dn *dn;
     185             : 
     186   141445000 :         if (ldb_kv->cache->GUID_index_attribute != NULL) {
     187   141133866 :                 return ldb_kv_guid_to_key(idx_val, key);
     188             :         }
     189             : 
     190      311134 :         dn = ldb_dn_from_ldb_val(mem_ctx, ldb, idx_val);
     191      311134 :         if (dn == NULL) {
     192             :                 /*
     193             :                  * LDB_ERR_INVALID_DN_SYNTAX would just be confusing
     194             :                  * to the caller, as this in an invalid index value
     195             :                  */
     196           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     197             :         }
     198             :         /* form the key */
     199      311134 :         *key = ldb_kv_key_dn(mem_ctx, dn);
     200      311134 :         TALLOC_FREE(dn);
     201      311134 :         if (!key->data) {
     202           0 :                 return ldb_module_oom(module);
     203             :         }
     204      221762 :         return LDB_SUCCESS;
     205             : }
     206             : 
     207             : /*
     208             :   form a TDB_DATA for a record key
     209             :   caller frees mem_ctx, which may or may not have the key
     210             :   as a child.
     211             : 
     212             :   note that the key for a record can depend on whether a
     213             :   GUID index is in use, or the DN is used as the key
     214             : */
     215    22192776 : struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
     216             :                         TALLOC_CTX *mem_ctx,
     217             :                         const struct ldb_message *msg)
     218             : {
     219    22192776 :         void *data = ldb_module_get_private(module);
     220     1893980 :         struct ldb_kv_private *ldb_kv =
     221    22192776 :             talloc_get_type(data, struct ldb_kv_private);
     222     1893980 :         struct ldb_val key;
     223     1893980 :         const struct ldb_val *guid_val;
     224     1893980 :         int ret;
     225             : 
     226    22192776 :         if (ldb_kv->cache->GUID_index_attribute == NULL) {
     227      500609 :                 return ldb_kv_key_dn(mem_ctx, msg->dn);
     228             :         }
     229             : 
     230    21692167 :         if (ldb_dn_is_special(msg->dn)) {
     231    17437446 :                 return ldb_kv_key_dn(mem_ctx, msg->dn);
     232             :         }
     233             : 
     234      347091 :         guid_val =
     235     4254721 :             ldb_msg_find_ldb_val(msg, ldb_kv->cache->GUID_index_attribute);
     236     4254721 :         if (guid_val == NULL) {
     237           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
     238             :                                        "Did not find GUID attribute %s "
     239             :                                        "in %s, required for TDB record "
     240             :                                        "key in " LDB_KV_IDXGUID " mode.",
     241           0 :                                        ldb_kv->cache->GUID_index_attribute,
     242           0 :                                        ldb_dn_get_linearized(msg->dn));
     243           0 :                 errno = EINVAL;
     244           0 :                 key.data = NULL;
     245           0 :                 key.length = 0;
     246           0 :                 return key;
     247             :         }
     248             : 
     249             :         /* In this case, allocate with talloc */
     250     4254721 :         key.data = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
     251     4254721 :         if (key.data == NULL) {
     252           0 :                 errno = ENOMEM;
     253           0 :                 key.data = NULL;
     254           0 :                 key.length = 0;
     255           0 :                 return key;
     256             :         }
     257     4254721 :         key.length = talloc_get_size(key.data);
     258             : 
     259     4254721 :         ret = ldb_kv_guid_to_key(guid_val, &key);
     260             : 
     261     4254721 :         if (ret != LDB_SUCCESS) {
     262           0 :                 errno = EINVAL;
     263           0 :                 key.data = NULL;
     264           0 :                 key.length = 0;
     265           0 :                 return key;
     266             :         }
     267     4254721 :         return key;
     268             : }
     269             : 
     270             : /*
     271             :   check special dn's have valid attributes
     272             :   currently only @ATTRIBUTES is checked
     273             : */
     274     2392683 : static int ldb_kv_check_special_dn(struct ldb_module *module,
     275             :                                    const struct ldb_message *msg)
     276             : {
     277     2392683 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
     278      119852 :         unsigned int i, j;
     279             : 
     280     2395300 :         if (! ldb_dn_is_special(msg->dn) ||
     281      248801 :             ! ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
     282     2383521 :                 return LDB_SUCCESS;
     283             :         }
     284             : 
     285             :         /* we have @ATTRIBUTES, let's check attributes are fine */
     286             :         /* should we check that we deny multivalued attributes ? */
     287     1865779 :         for (i = 0; i < msg->num_elements; i++) {
     288     1856619 :                 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
     289             : 
     290     3705215 :                 for (j = 0; j < msg->elements[i].num_values; j++) {
     291     1851389 :                         if (ldb_kv_check_at_attributes_values(
     292     1851389 :                                 &msg->elements[i].values[j]) != 0) {
     293           2 :                                 ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
     294           2 :                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
     295             :                         }
     296             :                 }
     297             :         }
     298             : 
     299        8667 :         return LDB_SUCCESS;
     300             : }
     301             : 
     302             : /*
     303             :  * Called after modifies and when starting a transaction. Checks target pack
     304             :  * format version and current pack format version, which are set by cache_load,
     305             :  * and repacks if necessary.
     306             :  */
     307     1941294 : static int ldb_kv_maybe_repack(struct ldb_kv_private *ldb_kv) {
     308             :         /* Override option taken from ldb options */
     309     1941294 :         if (ldb_kv->pack_format_override != 0) {
     310           6 :                 ldb_kv->target_pack_format_version =
     311           0 :                         ldb_kv->pack_format_override;
     312             :         }
     313             : 
     314     1941294 :         if (ldb_kv->pack_format_version !=
     315     1941294 :             ldb_kv->target_pack_format_version) {
     316         200 :                 int r;
     317        3457 :                 struct ldb_context *ldb = ldb_module_get_ctx(ldb_kv->module);
     318        3457 :                 r = ldb_kv_repack(ldb_kv->module);
     319        3457 :                 if (r != LDB_SUCCESS) {
     320           0 :                         ldb_debug(ldb, LDB_DEBUG_ERROR,
     321             :                                   "Database repack failed.");
     322             :                 }
     323        3457 :                 return r;
     324             :         }
     325             : 
     326     1922180 :         return LDB_SUCCESS;
     327             : }
     328             : 
     329             : /*
     330             :   we've made a modification to a dn - possibly reindex and
     331             :   update sequence number
     332             : */
     333     5111202 : static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
     334             : {
     335     5111202 :         int ret = LDB_SUCCESS;
     336     5111202 :         struct ldb_kv_private *ldb_kv = talloc_get_type(
     337             :             ldb_module_get_private(module), struct ldb_kv_private);
     338             : 
     339             :         /* only allow modifies inside a transaction, otherwise the
     340             :          * ldb is unsafe */
     341     5111202 :         if (ldb_kv->kv_ops->transaction_active(ldb_kv) == false) {
     342           0 :                 ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
     343           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     344             :         }
     345             : 
     346     7912206 :         if (ldb_dn_is_special(dn) &&
     347     5595270 :             (ldb_dn_check_special(dn, LDB_KV_INDEXLIST) ||
     348     2794266 :              ldb_dn_check_special(dn, LDB_KV_ATTRIBUTES)) )
     349             :         {
     350       13816 :                 if (ldb_kv->warn_reindex) {
     351           0 :                         ldb_debug(ldb_module_get_ctx(module),
     352             :                                   LDB_DEBUG_ERROR,
     353             :                                   "Reindexing %s due to modification on %s",
     354           0 :                                   ldb_kv->kv_ops->name(ldb_kv),
     355             :                                   ldb_dn_get_linearized(dn));
     356             :                 }
     357       13816 :                 ret = ldb_kv_reindex(module);
     358             :         }
     359             : 
     360             :         /* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
     361     5111907 :         if (ret == LDB_SUCCESS &&
     362     7912178 :             !(ldb_dn_is_special(dn) &&
     363     2800990 :               ldb_dn_check_special(dn, LDB_KV_BASEINFO)) ) {
     364     2555594 :                 ret = ldb_kv_increase_sequence_number(module);
     365             :         }
     366             : 
     367             :         /* If the modify was to @OPTIONS, reload the cache */
     368    10222390 :         if (ret == LDB_SUCCESS &&
     369     7912178 :             ldb_dn_is_special(dn) &&
     370     2800990 :             (ldb_dn_check_special(dn, LDB_KV_OPTIONS)) ) {
     371        2660 :                 ret = ldb_kv_cache_reload(module);
     372             :         }
     373             : 
     374     5111202 :         if (ret != LDB_SUCCESS) {
     375          14 :                 ldb_kv->reindex_failed = true;
     376             :         }
     377             : 
     378     4871114 :         return ret;
     379             : }
     380             : /*
     381             :   store a record into the db
     382             : */
     383    19990742 : int ldb_kv_store(struct ldb_module *module,
     384             :                  const struct ldb_message *msg,
     385             :                  int flgs)
     386             : {
     387    19990742 :         void *data = ldb_module_get_private(module);
     388     1643682 :         struct ldb_kv_private *ldb_kv =
     389    19990742 :             talloc_get_type(data, struct ldb_kv_private);
     390     1643682 :         struct ldb_val key;
     391     1643682 :         struct ldb_val ldb_data;
     392    19990742 :         int ret = LDB_SUCCESS;
     393    19990742 :         TALLOC_CTX *key_ctx = talloc_new(module);
     394             : 
     395    19990742 :         if (key_ctx == NULL) {
     396           0 :                 return ldb_module_oom(module);
     397             :         }
     398             : 
     399    19990742 :         if (ldb_kv->read_only) {
     400           0 :                 talloc_free(key_ctx);
     401           0 :                 return LDB_ERR_UNWILLING_TO_PERFORM;
     402             :         }
     403             : 
     404    19990742 :         key = ldb_kv_key_msg(module, key_ctx, msg);
     405    19990742 :         if (key.data == NULL) {
     406           0 :                 TALLOC_FREE(key_ctx);
     407           0 :                 return LDB_ERR_OTHER;
     408             :         }
     409             : 
     410    19990742 :         ret = ldb_pack_data(ldb_module_get_ctx(module),
     411             :                             msg, &ldb_data,
     412             :                             ldb_kv->pack_format_version);
     413    19990742 :         if (ret == -1) {
     414           0 :                 TALLOC_FREE(key_ctx);
     415           0 :                 return LDB_ERR_OTHER;
     416             :         }
     417             : 
     418    19990742 :         ret = ldb_kv->kv_ops->store(ldb_kv, key, ldb_data, flgs);
     419    19990742 :         if (ret != 0) {
     420        2675 :                 bool is_special = ldb_dn_is_special(msg->dn);
     421        2675 :                 ret = ldb_kv->kv_ops->error(ldb_kv);
     422             : 
     423             :                 /*
     424             :                  * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
     425             :                  * the GUID, so re-map
     426             :                  */
     427        2675 :                 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
     428          54 :                     ldb_kv->cache->GUID_index_attribute != NULL) {
     429          24 :                         ret = LDB_ERR_CONSTRAINT_VIOLATION;
     430             :                 }
     431        2675 :                 goto done;
     432             :         }
     433             : 
     434    19988067 : done:
     435    19990742 :         TALLOC_FREE(key_ctx);
     436    19990742 :         talloc_free(ldb_data.data);
     437             : 
     438    19990742 :         return ret;
     439             : }
     440             : 
     441             : 
     442             : /*
     443             :   check if a attribute is a single valued, for a given element
     444             :  */
     445     1696372 : static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
     446             :                                  struct ldb_message_element *el)
     447             : {
     448     1695480 :         if (!a) return false;
     449     1696372 :         if (el != NULL) {
     450     1696372 :                 if (el->flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK) {
     451             :                         /* override from a ldb module, for example
     452             :                            used for the description field, which is
     453             :                            marked multi-valued in the schema but which
     454             :                            should not actually accept multiple
     455             :                            values */
     456          12 :                         return true;
     457             :                 }
     458     1696360 :                 if (el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK) {
     459             :                         /* override from a ldb module, for example used for
     460             :                            deleted linked attribute entries */
     461       56902 :                         return false;
     462             :                 }
     463             :         }
     464     1638726 :         if (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
     465          75 :                 return true;
     466             :         }
     467     1490131 :         return false;
     468             : }
     469             : 
     470             : /*
     471             :  * Starts a sub transaction if they are supported by the backend
     472             :  * and the ldb connection has not been opened in batch mode.
     473             :  */
     474     2487901 : static int ldb_kv_sub_transaction_start(struct ldb_kv_private *ldb_kv)
     475             : {
     476     2487901 :         int ret = LDB_SUCCESS;
     477             : 
     478     2487901 :         if (ldb_kv->batch_mode) {
     479      393844 :                 return ret;
     480             :         }
     481             : 
     482     2094057 :         ret = ldb_kv->kv_ops->begin_nested_write(ldb_kv);
     483     2094057 :         if (ret == LDB_SUCCESS) {
     484     2094057 :                 ret = ldb_kv_index_sub_transaction_start(ldb_kv);
     485             :         }
     486     1973084 :         return ret;
     487             : }
     488             : 
     489             : /*
     490             :  * Commits a sub transaction if they are supported by the backend
     491             :  * and the ldb connection has not been opened in batch mode.
     492             :  */
     493     2473616 : static int ldb_kv_sub_transaction_commit(struct ldb_kv_private *ldb_kv)
     494             : {
     495     2473616 :         int ret = LDB_SUCCESS;
     496             : 
     497     2473616 :         if (ldb_kv->batch_mode) {
     498      393561 :                 return ret;
     499             :         }
     500             : 
     501     2080055 :         ret = ldb_kv_index_sub_transaction_commit(ldb_kv);
     502     2080055 :         if (ret != LDB_SUCCESS) {
     503           0 :                 return ret;
     504             :         }
     505     2080055 :         ret = ldb_kv->kv_ops->finish_nested_write(ldb_kv);
     506     2080055 :         return ret;
     507             : }
     508             : 
     509             : /*
     510             :  * Cancels a sub transaction if they are supported by the backend
     511             :  * and the ldb connection has not been opened in batch mode.
     512             :  */
     513       14285 : static int ldb_kv_sub_transaction_cancel(struct ldb_kv_private *ldb_kv)
     514             : {
     515       14285 :         int ret = LDB_SUCCESS;
     516             : 
     517       14285 :         if (ldb_kv->batch_mode) {
     518         283 :                 return ret;
     519             :         }
     520             : 
     521       14002 :         ret = ldb_kv_index_sub_transaction_cancel(ldb_kv);
     522       14002 :         if (ret != LDB_SUCCESS) {
     523           0 :                 struct ldb_context *ldb = ldb_module_get_ctx(ldb_kv->module);
     524             :                 /*
     525             :                  * In the event of a failure we log the failure and continue
     526             :                  * as we need to cancel the database transaction.
     527             :                  */
     528           0 :                 ldb_debug(ldb,
     529             :                           LDB_DEBUG_ERROR,
     530             :                           __location__": ldb_kv_index_sub_transaction_cancel "
     531             :                           "failed: %s",
     532             :                           ldb_errstring(ldb));
     533             :         }
     534       14002 :         ret = ldb_kv->kv_ops->abort_nested_write(ldb_kv);
     535       14002 :         return ret;
     536             : }
     537             : 
     538     1180710 : static int ldb_kv_add_internal(struct ldb_module *module,
     539             :                                struct ldb_kv_private *ldb_kv,
     540             :                                const struct ldb_message *msg,
     541             :                                bool check_single_value)
     542             : {
     543     1180710 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
     544     1180710 :         int ret = LDB_SUCCESS;
     545       88755 :         unsigned int i;
     546     1180710 :         bool valid_dn = false;
     547             : 
     548             :         /* Check the new DN is reasonable */
     549     1180710 :         valid_dn = ldb_dn_validate(msg->dn);
     550     1180710 :         if (valid_dn == false) {
     551           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
     552             :                                        "Invalid DN in ADD: %s",
     553           0 :                                        ldb_dn_get_linearized(msg->dn));
     554           0 :                 return LDB_ERR_INVALID_DN_SYNTAX;
     555             :         }
     556             : 
     557    23314077 :         for (i=0;i<msg->num_elements;i++) {
     558    22133371 :                 struct ldb_message_element *el = &msg->elements[i];
     559    22133371 :                 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
     560             : 
     561    22133371 :                 if (el->num_values == 0) {
     562           2 :                         ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
     563           2 :                                                el->name, ldb_dn_get_linearized(msg->dn));
     564           2 :                         return LDB_ERR_CONSTRAINT_VIOLATION;
     565             :                 }
     566    23488674 :                 if (check_single_value && el->num_values > 1 &&
     567     3262985 :                     ldb_kv_single_valued(a, el)) {
     568           0 :                         ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
     569           0 :                                                el->name, ldb_dn_get_linearized(msg->dn));
     570           0 :                         return LDB_ERR_CONSTRAINT_VIOLATION;
     571             :                 }
     572             : 
     573             :                 /* Do not check "@ATTRIBUTES" for duplicated values */
     574    23467703 :                 if (ldb_dn_is_special(msg->dn) &&
     575     1334334 :                     ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
     576     1301302 :                         continue;
     577             :                 }
     578             : 
     579    20832067 :                 if (check_single_value &&
     580    19244896 :                     !(el->flags &
     581             :                       LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
     582    19237874 :                         struct ldb_val *duplicate = NULL;
     583             : 
     584    19237874 :                         ret = ldb_msg_find_duplicate_val(ldb, discard_const(msg),
     585             :                                                          el, &duplicate, 0);
     586    19237874 :                         if (ret != LDB_SUCCESS) {
     587           2 :                                 return ret;
     588             :                         }
     589    19237874 :                         if (duplicate != NULL) {
     590           4 :                                 ldb_asprintf_errstring(
     591             :                                         ldb,
     592             :                                         "attribute '%s': value '%.*s' on '%s' "
     593             :                                         "provided more than once in ADD object",
     594             :                                         el->name,
     595           2 :                                         (int)duplicate->length,
     596           2 :                                         duplicate->data,
     597           2 :                                         ldb_dn_get_linearized(msg->dn));
     598           2 :                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
     599             :                         }
     600             :                 }
     601             :         }
     602             : 
     603     1180706 :         ret = ldb_kv_store(module, msg, TDB_INSERT);
     604     1180706 :         if (ret != LDB_SUCCESS) {
     605             :                 /*
     606             :                  * Try really hard to get the right error code for
     607             :                  * a re-add situation, as this can matter!
     608             :                  */
     609        2675 :                 if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
     610           0 :                         int ret2;
     611          24 :                         struct ldb_dn *dn2 = NULL;
     612          24 :                         TALLOC_CTX *mem_ctx = talloc_new(module);
     613          24 :                         if (mem_ctx == NULL) {
     614           0 :                                 return ldb_module_operr(module);
     615             :                         }
     616           0 :                         ret2 =
     617          24 :                             ldb_kv_search_base(module, mem_ctx, msg->dn, &dn2);
     618          24 :                         TALLOC_FREE(mem_ctx);
     619          24 :                         if (ret2 == LDB_SUCCESS) {
     620           8 :                                 ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
     621             :                         }
     622             :                 }
     623        2675 :                 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
     624        2659 :                         ldb_asprintf_errstring(ldb,
     625             :                                                "Entry %s already exists",
     626        2659 :                                                ldb_dn_get_linearized(msg->dn));
     627             :                 }
     628        2675 :                 return ret;
     629             :         }
     630             : 
     631     1178031 :         ret = ldb_kv_index_add_new(module, ldb_kv, msg);
     632     1178031 :         if (ret != LDB_SUCCESS) {
     633             :                 /*
     634             :                  * If we failed to index, delete the message again.
     635             :                  *
     636             :                  * This is particularly important for the GUID index
     637             :                  * case, which will only fail for a duplicate DN
     638             :                  * in the index add.
     639             :                  *
     640             :                  * Note that the caller may not cancel the transaction
     641             :                  * and this means the above add might really show up!
     642             :                  */
     643         390 :                 ldb_kv_delete_noindex(module, msg);
     644         390 :                 return ret;
     645             :         }
     646             : 
     647     1177641 :         ret = ldb_kv_modified(module, msg->dn);
     648             : 
     649             :         /*
     650             :          * To allow testing of the error recovery code in ldb_kv_add
     651             :          * cmocka tests can define CMOCKA_UNIT_TEST_ADD_INTERNAL_FAIL
     652             :          * to inject failures at this point.
     653             :          */
     654             : #ifdef CMOCKA_UNIT_TEST_ADD_INTERNAL_FAIL
     655             :         CMOCKA_UNIT_TEST_ADD_INTERNAL_FAIL
     656             : #endif
     657             : 
     658     1177641 :         return ret;
     659             : }
     660             : 
     661             : /*
     662             :   add a record to the database
     663             : */
     664     1098734 : static int ldb_kv_add(struct ldb_kv_context *ctx)
     665             : {
     666     1098734 :         struct ldb_module *module = ctx->module;
     667     1098734 :         struct ldb_request *req = ctx->req;
     668     1098734 :         void *data = ldb_module_get_private(module);
     669       88593 :         struct ldb_kv_private *ldb_kv =
     670     1098734 :             talloc_get_type(data, struct ldb_kv_private);
     671     1098734 :         int ret = LDB_SUCCESS;
     672             : 
     673     1098734 :         if (ldb_kv->max_key_length != 0 &&
     674      330323 :             ldb_kv->cache->GUID_index_attribute == NULL &&
     675         927 :             !ldb_dn_is_special(req->op.add.message->dn)) {
     676           0 :                 ldb_set_errstring(ldb_module_get_ctx(module),
     677             :                                   "Must operate ldb_mdb in GUID "
     678             :                                   "index mode, but " LDB_KV_IDXGUID " not set.");
     679           0 :                 return LDB_ERR_UNWILLING_TO_PERFORM;
     680             :         }
     681             : 
     682     1098734 :         ret = ldb_kv_check_special_dn(module, req->op.add.message);
     683     1098734 :         if (ret != LDB_SUCCESS) {
     684           1 :                 return ret;
     685             :         }
     686             : 
     687     1098732 :         ldb_request_set_state(req, LDB_ASYNC_PENDING);
     688             : 
     689     1098732 :         if (ldb_kv_cache_load(module) != 0) {
     690           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     691             :         }
     692             : 
     693     1098732 :         ret = ldb_kv_sub_transaction_start(ldb_kv);
     694     1098732 :         if (ret != LDB_SUCCESS) {
     695           0 :                 return ret;
     696             :         }
     697     1098732 :         ret = ldb_kv_add_internal(module, ldb_kv, req->op.add.message, true);
     698     1098732 :         if (ret != LDB_SUCCESS) {
     699        3083 :                 int r = ldb_kv_sub_transaction_cancel(ldb_kv);
     700        3083 :                 if (r != LDB_SUCCESS) {
     701           0 :                         ldb_debug(
     702             :                                 ldb_module_get_ctx(module),
     703             :                                 LDB_DEBUG_FATAL,
     704             :                                 __location__
     705             :                                 ": Unable to roll back sub transaction");
     706             :                 }
     707        3083 :                 ldb_kv->operation_failed = true;
     708        3083 :                 return ret;
     709             :         }
     710     1095649 :         ret = ldb_kv_sub_transaction_commit(ldb_kv);
     711             : 
     712     1095649 :         return ret;
     713             : }
     714             : 
     715             : /*
     716             :   delete a record from the database, not updating indexes (used for deleting
     717             :   index records)
     718             : */
     719      932079 : int ldb_kv_delete_noindex(struct ldb_module *module,
     720             :                           const struct ldb_message *msg)
     721             : {
     722      932079 :         void *data = ldb_module_get_private(module);
     723      117979 :         struct ldb_kv_private *ldb_kv =
     724      932079 :             talloc_get_type(data, struct ldb_kv_private);
     725      117979 :         struct ldb_val key;
     726      117979 :         int ret;
     727      932079 :         TALLOC_CTX *tdb_key_ctx = talloc_new(module);
     728             : 
     729      932079 :         if (tdb_key_ctx == NULL) {
     730           0 :                 return ldb_module_oom(module);
     731             :         }
     732             : 
     733      932079 :         if (ldb_kv->read_only) {
     734           0 :                 talloc_free(tdb_key_ctx);
     735           0 :                 return LDB_ERR_UNWILLING_TO_PERFORM;
     736             :         }
     737             : 
     738      932079 :         key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
     739      932079 :         if (!key.data) {
     740           0 :                 TALLOC_FREE(tdb_key_ctx);
     741           0 :                 return LDB_ERR_OTHER;
     742             :         }
     743             : 
     744      932079 :         ret = ldb_kv->kv_ops->delete(ldb_kv, key);
     745      932079 :         TALLOC_FREE(tdb_key_ctx);
     746             : 
     747      932079 :         if (ret != 0) {
     748       96617 :                 ret = ldb_kv->kv_ops->error(ldb_kv);
     749             :         }
     750             : 
     751      814100 :         return ret;
     752             : }
     753             : 
     754       95220 : static int ldb_kv_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
     755             : {
     756        1122 :         struct ldb_message *msg;
     757       95220 :         int ret = LDB_SUCCESS;
     758             : 
     759       95220 :         msg = ldb_msg_new(module);
     760       95220 :         if (msg == NULL) {
     761           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     762             :         }
     763             : 
     764             :         /* in case any attribute of the message was indexed, we need
     765             :            to fetch the old record */
     766       95220 :         ret = ldb_kv_search_dn1(module, dn, msg, 0);
     767       95220 :         if (ret != LDB_SUCCESS) {
     768             :                 /* not finding the old record is an error */
     769        5745 :                 goto done;
     770             :         }
     771             : 
     772       89475 :         ret = ldb_kv_delete_noindex(module, msg);
     773       89475 :         if (ret != LDB_SUCCESS) {
     774           0 :                 goto done;
     775             :         }
     776             : 
     777             :         /* remove any indexed attributes */
     778       89475 :         ret = ldb_kv_index_delete(module, msg);
     779       89475 :         if (ret != LDB_SUCCESS) {
     780           0 :                 goto done;
     781             :         }
     782             : 
     783       89475 :         ret = ldb_kv_modified(module, dn);
     784       89475 :         if (ret != LDB_SUCCESS) {
     785           0 :                 goto done;
     786             :         }
     787             : 
     788       89475 : done:
     789       95220 :         talloc_free(msg);
     790             :         /*
     791             :          * To allow testing of the error recovery code in ldb_kv_delete
     792             :          * cmocka tests can define CMOCKA_UNIT_TEST_DELETE_INTERNAL_FAIL
     793             :          * to inject failures at this point.
     794             :          */
     795             : #ifdef CMOCKA_UNIT_TEST_DELETE_INTERNAL_FAIL
     796             :         CMOCKA_UNIT_TEST_DELETE_INTERNAL_FAIL
     797             : #endif
     798       95220 :         return ret;
     799             : }
     800             : 
     801             : /*
     802             :   delete a record from the database
     803             : */
     804       13242 : static int ldb_kv_delete(struct ldb_kv_context *ctx)
     805             : {
     806       13242 :         struct ldb_module *module = ctx->module;
     807       13242 :         struct ldb_request *req = ctx->req;
     808       13242 :         void *data = ldb_module_get_private(module);
     809         959 :         struct ldb_kv_private *ldb_kv =
     810       13242 :             talloc_get_type(data, struct ldb_kv_private);
     811       13242 :         int ret = LDB_SUCCESS;
     812             : 
     813       13242 :         ldb_request_set_state(req, LDB_ASYNC_PENDING);
     814             : 
     815       13242 :         if (ldb_kv_cache_load(module) != 0) {
     816           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     817             :         }
     818             : 
     819       13242 :         ret = ldb_kv_sub_transaction_start(ldb_kv);
     820       13242 :         if (ret != LDB_SUCCESS) {
     821           0 :                 return ret;
     822             :         }
     823       13242 :         ret = ldb_kv_delete_internal(module, req->op.del.dn);
     824       13242 :         if (ret != LDB_SUCCESS) {
     825        5745 :                 int r = ldb_kv_sub_transaction_cancel(ldb_kv);
     826        5745 :                 if (r != LDB_SUCCESS) {
     827           0 :                         ldb_debug(
     828             :                                 ldb_module_get_ctx(module),
     829             :                                 LDB_DEBUG_FATAL,
     830             :                                 __location__
     831             :                                 ": Unable to roll back sub transaction");
     832             :                 }
     833        5745 :                 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
     834           0 :                         ldb_kv->operation_failed = true;
     835             :                 }
     836        5745 :                 return ret;
     837             :         }
     838        7497 :         ret = ldb_kv_sub_transaction_commit(ldb_kv);
     839             : 
     840        7497 :         return ret;
     841             : }
     842             : 
     843             : /*
     844             :   find an element by attribute name. At the moment this does a linear search,
     845             :   it should be re-coded to use a binary search once all places that modify
     846             :   records guarantee sorted order
     847             : 
     848             :   return the index of the first matching element if found, otherwise -1
     849             : */
     850     9330272 : static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
     851             : {
     852      386540 :         unsigned int i;
     853   228213467 :         for (i=0;i<msg->num_elements;i++) {
     854   227577268 :                 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
     855     8694073 :                         return i;
     856             :                 }
     857             :         }
     858      626994 :         return -1;
     859             : }
     860             : 
     861             : 
     862             : /*
     863             :   add an element to an existing record. Assumes a elements array that we
     864             :   can call re-alloc on, and assumes that we can reuse the data pointers from
     865             :   the passed in additional values. Use with care!
     866             : 
     867             :   returns 0 on success, -1 on failure (and sets errno)
     868             : */
     869     4942441 : static int ldb_kv_msg_add_element(struct ldb_message *msg,
     870             :                                   struct ldb_message_element *el)
     871             : {
     872      189365 :         struct ldb_message_element *e2;
     873      189365 :         unsigned int i;
     874             : 
     875     4942441 :         if (el->num_values == 0) {
     876             :                 /* nothing to do here - we don't add empty elements */
     877      134123 :                 return 0;
     878             :         }
     879             : 
     880     4807800 :         e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
     881             :                               msg->num_elements+1);
     882     4807800 :         if (!e2) {
     883           0 :                 errno = ENOMEM;
     884           0 :                 return -1;
     885             :         }
     886             : 
     887     4807800 :         msg->elements = e2;
     888             : 
     889     4807800 :         e2 = &msg->elements[msg->num_elements];
     890             : 
     891     4807800 :         e2->name = el->name;
     892     4807800 :         e2->flags = el->flags;
     893     4807800 :         e2->values = talloc_array(msg->elements,
     894             :                                   struct ldb_val, el->num_values);
     895     4807800 :         if (!e2->values) {
     896           0 :                 errno = ENOMEM;
     897           0 :                 return -1;
     898             :         }
     899    12684617 :         for (i=0;i<el->num_values;i++) {
     900     7876817 :                 e2->values[i] = el->values[i];
     901             :         }
     902     4807800 :         e2->num_values = el->num_values;
     903             : 
     904     4807800 :         ++msg->num_elements;
     905             : 
     906     4807800 :         return 0;
     907             : }
     908             : 
     909             : /*
     910             :   delete all elements having a specified attribute name
     911             : */
     912     5007072 : static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
     913             :                                        struct ldb_kv_private *ldb_kv,
     914             :                                        struct ldb_message *msg,
     915             :                                        const char *name)
     916             : {
     917      185059 :         int ret;
     918      185059 :         struct ldb_message_element *el;
     919     5007072 :         bool is_special = ldb_dn_is_special(msg->dn);
     920             : 
     921     5007072 :         if (!is_special && ldb_kv->cache->GUID_index_attribute != NULL &&
     922     2142016 :             ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
     923           0 :                 struct ldb_context *ldb = ldb_module_get_ctx(module);
     924           0 :                 ldb_asprintf_errstring(ldb,
     925             :                                        "Must not modify GUID "
     926             :                                        "attribute %s (used as DB index)",
     927           0 :                                        ldb_kv->cache->GUID_index_attribute);
     928           0 :                 return LDB_ERR_CONSTRAINT_VIOLATION;
     929             :         }
     930             : 
     931     5007072 :         el = ldb_msg_find_element(msg, name);
     932     5007072 :         if (el == NULL) {
     933         251 :                 return LDB_ERR_NO_SUCH_ATTRIBUTE;
     934             :         }
     935             : 
     936     5006820 :         ret = ldb_kv_index_del_element(module, ldb_kv, msg, el);
     937     5006820 :         if (ret != LDB_SUCCESS) {
     938           0 :                 return ret;
     939             :         }
     940             : 
     941     5006820 :         talloc_free(el->values);
     942     5006820 :         ldb_msg_remove_element(msg, el);
     943     5006820 :         msg->elements = talloc_realloc(msg, msg->elements,
     944             :                                        struct ldb_message_element,
     945             :                                        msg->num_elements);
     946     5006820 :         return LDB_SUCCESS;
     947             : }
     948             : 
     949             : /*
     950             :   delete all elements matching an attribute name/value
     951             : 
     952             :   return LDB Error on failure
     953             : */
     954      188784 : static int ldb_kv_msg_delete_element(struct ldb_module *module,
     955             :                                      struct ldb_kv_private *ldb_kv,
     956             :                                      struct ldb_message *msg,
     957             :                                      const char *name,
     958             :                                      const struct ldb_val *val)
     959             : {
     960      188784 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
     961        2959 :         unsigned int i;
     962        2959 :         int found, ret;
     963        2959 :         struct ldb_message_element *el;
     964        2959 :         const struct ldb_schema_attribute *a;
     965             : 
     966      188784 :         found = ldb_kv_find_element(msg, name);
     967      188784 :         if (found == -1) {
     968         886 :                 return LDB_ERR_NO_SUCH_ATTRIBUTE;
     969             :         }
     970             : 
     971      187871 :         i = (unsigned int) found;
     972      187871 :         el = &(msg->elements[i]);
     973             : 
     974      187871 :         a = ldb_schema_attribute_by_name(ldb, el->name);
     975             : 
     976      202514 :         for (i=0;i<el->num_values;i++) {
     977        3019 :                 bool matched;
     978      199437 :                 if (a->syntax->operator_fn) {
     979      201004 :                         ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
     980      198058 :                                                      &el->values[i], val, &matched);
     981      385784 :                         if (ret != LDB_SUCCESS) return ret;
     982             :                 } else {
     983        2758 :                         matched = (a->syntax->comparison_fn(ldb, ldb,
     984        1379 :                                                             &el->values[i], val) == 0);
     985             :                 }
     986      199437 :                 if (matched) {
     987      187726 :                         if (el->num_values == 1) {
     988      166037 :                                 return ldb_kv_msg_delete_attribute(
     989             :                                     module, ldb_kv, msg, name);
     990             :                         }
     991             : 
     992         251 :                         ret =
     993       21689 :                             ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
     994       21689 :                         if (ret != LDB_SUCCESS) {
     995           0 :                                 return ret;
     996             :                         }
     997             : 
     998       21689 :                         ARRAY_DEL_ELEMENT(el->values, i, el->num_values);
     999       21689 :                         el->num_values--;
    1000             : 
    1001             :                         /* per definition we find in a canonicalised message an
    1002             :                            attribute value only once. So we are finished here */
    1003       21689 :                         return LDB_SUCCESS;
    1004             :                 }
    1005             :         }
    1006             : 
    1007             :         /* Not found */
    1008         145 :         return LDB_ERR_NO_SUCH_ATTRIBUTE;
    1009             : }
    1010             : 
    1011             : /*
    1012             :   modify a record - internal interface
    1013             : 
    1014             :   yuck - this is O(n^2). Luckily n is usually small so we probably
    1015             :   get away with it, but if we ever have really large attribute lists
    1016             :   then we'll need to look at this again
    1017             : 
    1018             :   'req' is optional, and is used to specify controls if supplied
    1019             : */
    1020     3849543 : int ldb_kv_modify_internal(struct ldb_module *module,
    1021             :                            const struct ldb_message *msg,
    1022             :                            struct ldb_request *req)
    1023             : {
    1024     3849543 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
    1025     3849543 :         void *data = ldb_module_get_private(module);
    1026      151303 :         struct ldb_kv_private *ldb_kv =
    1027     3849543 :             talloc_get_type(data, struct ldb_kv_private);
    1028      151303 :         struct ldb_message *msg2;
    1029      151303 :         unsigned int i, j;
    1030     3849543 :         int ret = LDB_SUCCESS, idx;
    1031     3849543 :         struct ldb_control *control_permissive = NULL;
    1032     3849543 :         TALLOC_CTX *mem_ctx = talloc_new(req);
    1033             : 
    1034     3849543 :         if (mem_ctx == NULL) {
    1035           0 :                 return ldb_module_oom(module);
    1036             :         }
    1037             : 
    1038     3849543 :         if (req) {
    1039     1293949 :                 control_permissive = ldb_request_get_control(req,
    1040             :                                         LDB_CONTROL_PERMISSIVE_MODIFY_OID);
    1041             :         }
    1042             : 
    1043     3849543 :         msg2 = ldb_msg_new(mem_ctx);
    1044     3849543 :         if (msg2 == NULL) {
    1045           0 :                 ret = LDB_ERR_OTHER;
    1046           0 :                 goto done;
    1047             :         }
    1048             : 
    1049     3849543 :         ret = ldb_kv_search_dn1(module, msg->dn, msg2, 0);
    1050     3849543 :         if (ret != LDB_SUCCESS) {
    1051        4016 :                 goto done;
    1052             :         }
    1053             : 
    1054    13708282 :         for (i=0; i<msg->num_elements; i++) {
    1055     9864196 :                 struct ldb_message_element *el = &msg->elements[i], *el2;
    1056      388726 :                 struct ldb_val *vals;
    1057     9864196 :                 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
    1058      388726 :                 const char *dn;
    1059     9864196 :                 uint32_t options = 0;
    1060     9864196 :                 if (control_permissive != NULL) {
    1061        2630 :                         options |= LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES;
    1062             :                 }
    1063             : 
    1064     9864196 :                 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
    1065      210917 :                 case LDB_FLAG_MOD_ADD:
    1066             : 
    1067      210917 :                         if (el->num_values == 0) {
    1068          26 :                                 ldb_asprintf_errstring(ldb,
    1069             :                                                        "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
    1070             :                                                        el->name, ldb_dn_get_linearized(msg2->dn));
    1071          26 :                                 ret = LDB_ERR_CONSTRAINT_VIOLATION;
    1072          26 :                                 goto done;
    1073             :                         }
    1074             : 
    1075             :                         /* make a copy of the array so that a permissive
    1076             :                          * control can remove duplicates without changing the
    1077             :                          * original values, but do not copy data as we do not
    1078             :                          * need to keep it around once the operation is
    1079             :                          * finished */
    1080      210891 :                         if (control_permissive) {
    1081          10 :                                 el = talloc(msg2, struct ldb_message_element);
    1082          10 :                                 if (!el) {
    1083           0 :                                         ret = LDB_ERR_OTHER;
    1084           0 :                                         goto done;
    1085             :                                 }
    1086          10 :                                 *el = msg->elements[i];
    1087          10 :                                 el->values = talloc_array(el, struct ldb_val, el->num_values);
    1088          10 :                                 if (el->values == NULL) {
    1089           0 :                                         ret = LDB_ERR_OTHER;
    1090           0 :                                         goto done;
    1091             :                                 }
    1092          41 :                                 for (j = 0; j < el->num_values; j++) {
    1093          31 :                                         el->values[j] = msg->elements[i].values[j];
    1094             :                                 }
    1095             :                         }
    1096             : 
    1097      210891 :                         if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
    1098           9 :                                 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
    1099             :                                                        el->name, ldb_dn_get_linearized(msg2->dn));
    1100           9 :                                 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
    1101           9 :                                 goto done;
    1102             :                         }
    1103             : 
    1104             :                         /* Checks if element already exists */
    1105      210882 :                         idx = ldb_kv_find_element(msg2, el->name);
    1106      210882 :                         if (idx == -1) {
    1107      183455 :                                 if (ldb_kv_msg_add_element(msg2, el) != 0) {
    1108           0 :                                         ret = LDB_ERR_OTHER;
    1109           0 :                                         goto done;
    1110             :                                 }
    1111      183455 :                                 ret = ldb_kv_index_add_element(
    1112             :                                     module, ldb_kv, msg2, el);
    1113      183455 :                                 if (ret != LDB_SUCCESS) {
    1114           0 :                                         goto done;
    1115             :                                 }
    1116             :                         } else {
    1117       27427 :                                 j = (unsigned int) idx;
    1118       27427 :                                 el2 = &(msg2->elements[j]);
    1119             : 
    1120             :                                 /* We cannot add another value on a existing one
    1121             :                                    if the attribute is single-valued */
    1122       27427 :                                 if (ldb_kv_single_valued(a, el)) {
    1123          42 :                                         ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
    1124             :                                                                el->name, ldb_dn_get_linearized(msg2->dn));
    1125          42 :                                         ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
    1126          42 :                                         goto done;
    1127             :                                 }
    1128             : 
    1129             :                                 /* Check that values don't exist yet on multi-
    1130             :                                    valued attributes or aren't provided twice */
    1131       27385 :                                 if (!(el->flags &
    1132             :                                       LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
    1133        5133 :                                         struct ldb_val *duplicate = NULL;
    1134        5133 :                                         ret = ldb_msg_find_common_values(ldb,
    1135             :                                                                          msg2,
    1136             :                                                                          el,
    1137             :                                                                          el2,
    1138             :                                                                          options);
    1139             : 
    1140        5133 :                                         if (ret ==
    1141             :                                             LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
    1142          17 :                                                 ldb_asprintf_errstring(ldb,
    1143             :                                                         "attribute '%s': value "
    1144             :                                                         "#%u on '%s' already "
    1145             :                                                         "exists", el->name, j,
    1146             :                                                         ldb_dn_get_linearized(msg2->dn));
    1147          17 :                                                 goto done;
    1148        5116 :                                         } else if (ret != LDB_SUCCESS) {
    1149           0 :                                                 goto done;
    1150             :                                         }
    1151             : 
    1152        5116 :                                         ret = ldb_msg_find_duplicate_val(
    1153             :                                                 ldb, msg2, el, &duplicate, 0);
    1154        5116 :                                         if (ret != LDB_SUCCESS) {
    1155           0 :                                                 goto done;
    1156             :                                         }
    1157        5116 :                                         if (duplicate != NULL) {
    1158           0 :                                                 ldb_asprintf_errstring(
    1159             :                                                         ldb,
    1160             :                                                         "attribute '%s': value "
    1161             :                                                         "'%.*s' on '%s' "
    1162             :                                                         "provided more than "
    1163             :                                                         "once in ADD",
    1164             :                                                         el->name,
    1165           0 :                                                         (int)duplicate->length,
    1166           0 :                                                         duplicate->data,
    1167           0 :                                                         ldb_dn_get_linearized(msg->dn));
    1168           0 :                                                 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
    1169           0 :                                                 goto done;
    1170             :                                         }
    1171             :                                 }
    1172             : 
    1173             :                                 /* Now combine existing and new values to a new
    1174             :                                    attribute record */
    1175       27368 :                                 vals = talloc_realloc(msg2->elements,
    1176             :                                                       el2->values, struct ldb_val,
    1177             :                                                       el2->num_values + el->num_values);
    1178       27368 :                                 if (vals == NULL) {
    1179           0 :                                         ldb_oom(ldb);
    1180           0 :                                         ret = LDB_ERR_OTHER;
    1181           0 :                                         goto done;
    1182             :                                 }
    1183             : 
    1184       55781 :                                 for (j=0; j<el->num_values; j++) {
    1185       28413 :                                         vals[el2->num_values + j] =
    1186       28413 :                                                 ldb_val_dup(vals, &el->values[j]);
    1187             :                                 }
    1188             : 
    1189       27368 :                                 el2->values = vals;
    1190       27368 :                                 el2->num_values += el->num_values;
    1191             : 
    1192       27368 :                                 ret = ldb_kv_index_add_element(
    1193             :                                     module, ldb_kv, msg2, el);
    1194       27368 :                                 if (ret != LDB_SUCCESS) {
    1195           0 :                                         goto done;
    1196             :                                 }
    1197             :                         }
    1198             : 
    1199      205732 :                         break;
    1200             : 
    1201     8930643 :                 case LDB_FLAG_MOD_REPLACE:
    1202             : 
    1203     8930643 :                         if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
    1204          36 :                                 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
    1205             :                                                        el->name, ldb_dn_get_linearized(msg2->dn));
    1206          36 :                                 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
    1207          36 :                                 goto done;
    1208             :                         }
    1209             : 
    1210             :                         /*
    1211             :                          * We don't need to check this if we have been
    1212             :                          * pre-screened by the repl_meta_data module
    1213             :                          * in Samba, or someone else who can claim to
    1214             :                          * know what they are doing.
    1215             :                          */
    1216     8930607 :                         if (!(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
    1217     8877141 :                                 struct ldb_val *duplicate = NULL;
    1218             : 
    1219     8877141 :                                 ret = ldb_msg_find_duplicate_val(ldb, msg2, el,
    1220             :                                                                  &duplicate, 0);
    1221     8877141 :                                 if (ret != LDB_SUCCESS) {
    1222           1 :                                         goto done;
    1223             :                                 }
    1224     8877141 :                                 if (duplicate != NULL) {
    1225           1 :                                         ldb_asprintf_errstring(
    1226             :                                                 ldb,
    1227             :                                                 "attribute '%s': value '%.*s' "
    1228             :                                                 "on '%s' provided more than "
    1229             :                                                 "once in REPLACE",
    1230             :                                                 el->name,
    1231           1 :                                                 (int)duplicate->length,
    1232           1 :                                                 duplicate->data,
    1233             :                                                 ldb_dn_get_linearized(msg2->dn));
    1234           1 :                                         ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
    1235           1 :                                         goto done;
    1236             :                                 }
    1237             :                         }
    1238             : 
    1239             :                         /* Checks if element already exists */
    1240     8930606 :                         idx = ldb_kv_find_element(msg2, el->name);
    1241     8930606 :                         if (idx != -1) {
    1242     8478775 :                                 j = (unsigned int) idx;
    1243     8478775 :                                 el2 = &(msg2->elements[j]);
    1244             : 
    1245             :                                 /* we consider two elements to be
    1246             :                                  * equal only if the order
    1247             :                                  * matches. This allows dbcheck to
    1248             :                                  * fix the ordering on attributes
    1249             :                                  * where order matters, such as
    1250             :                                  * objectClass
    1251             :                                  */
    1252     8478775 :                                 if (ldb_msg_element_equal_ordered(el, el2)) {
    1253     4171620 :                                         continue;
    1254             :                                 }
    1255             : 
    1256             :                                 /* Delete the attribute if it exists in the DB */
    1257     4307155 :                                 if (ldb_kv_msg_delete_attribute(
    1258             :                                         module, ldb_kv, msg2, el->name) != 0) {
    1259           0 :                                         ret = LDB_ERR_OTHER;
    1260           0 :                                         goto done;
    1261             :                                 }
    1262             :                         }
    1263             : 
    1264             :                         /* Recreate it with the new values */
    1265     4758986 :                         if (ldb_kv_msg_add_element(msg2, el) != 0) {
    1266           0 :                                 ret = LDB_ERR_OTHER;
    1267           0 :                                 goto done;
    1268             :                         }
    1269             : 
    1270      185166 :                         ret =
    1271     4758986 :                             ldb_kv_index_add_element(module, ldb_kv, msg2, el);
    1272     4758986 :                         if (ret != LDB_SUCCESS) {
    1273           0 :                                 goto done;
    1274             :                         }
    1275             : 
    1276     4573820 :                         break;
    1277             : 
    1278      722636 :                 case LDB_FLAG_MOD_DELETE:
    1279      722636 :                         dn = ldb_dn_get_linearized(msg2->dn);
    1280      722636 :                         if (dn == NULL) {
    1281           0 :                                 ret = LDB_ERR_OTHER;
    1282           0 :                                 goto done;
    1283             :                         }
    1284             : 
    1285      722636 :                         if (msg->elements[i].num_values == 0) {
    1286             :                                 /* Delete the whole attribute */
    1287      533880 :                                 ret = ldb_kv_msg_delete_attribute(
    1288             :                                     module,
    1289             :                                     ldb_kv,
    1290             :                                     msg2,
    1291      531689 :                                     msg->elements[i].name);
    1292      533880 :                                 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
    1293         252 :                                         if (control_permissive) {
    1294           0 :                                                 ret = LDB_SUCCESS;
    1295             :                                         } else {
    1296         252 :                                                 ldb_asprintf_errstring(ldb,
    1297             :                                                                        "attribute '%s': no such attribute for delete on '%s'",
    1298         252 :                                                                        msg->elements[i].name, dn);
    1299             :                                         }
    1300             :                                 }
    1301      533880 :                                 if (ret != LDB_SUCCESS) {
    1302         252 :                                         goto done;
    1303             :                                 }
    1304             :                         } else {
    1305             :                                 /* Delete specified values from an attribute */
    1306      376482 :                                 for (j=0; j < msg->elements[i].num_values; j++) {
    1307      191743 :                                         ret = ldb_kv_msg_delete_element(
    1308             :                                             module,
    1309             :                                             ldb_kv,
    1310             :                                             msg2,
    1311      185825 :                                             msg->elements[i].name,
    1312      188784 :                                             &msg->elements[i].values[j]);
    1313      188784 :                                         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
    1314             :                                             control_permissive) {
    1315           0 :                                                 ret = LDB_SUCCESS;
    1316      188784 :                                         } else if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
    1317        1058 :                                                 ldb_asprintf_errstring(ldb,
    1318             :                                                                        "attribute '%s': no matching attribute value while deleting attribute on '%s'",
    1319        1058 :                                                                        msg->elements[i].name, dn);
    1320             :                                         }
    1321      188784 :                                         if (ret != LDB_SUCCESS) {
    1322        1058 :                                                 goto done;
    1323             :                                         }
    1324             :                                 }
    1325             :                         }
    1326      716209 :                         break;
    1327           0 :                 default:
    1328           0 :                         ldb_asprintf_errstring(ldb,
    1329             :                                                "attribute '%s': invalid modify flags on '%s': 0x%x",
    1330           0 :                                                msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
    1331           0 :                                                msg->elements[i].flags & LDB_FLAG_MOD_MASK);
    1332           0 :                         ret = LDB_ERR_PROTOCOL_ERROR;
    1333           0 :                         goto done;
    1334             :                 }
    1335             :         }
    1336             : 
    1337     3844086 :         ret = ldb_kv_store(module, msg2, TDB_MODIFY);
    1338     3844086 :         if (ret != LDB_SUCCESS) {
    1339           0 :                 goto done;
    1340             :         }
    1341             : 
    1342     3844086 :         ret = ldb_kv_modified(module, msg2->dn);
    1343     3844086 :         if (ret != LDB_SUCCESS) {
    1344           0 :                 goto done;
    1345             :         }
    1346             : 
    1347     3844086 : done:
    1348     3849543 :         TALLOC_FREE(mem_ctx);
    1349             :         /*
    1350             :          * To allow testing of the error recovery code in ldb_kv_modify
    1351             :          * cmocka tests can define CMOCKA_UNIT_TEST_MODIFY_INTERNAL_FAIL
    1352             :          * to inject failures at this point.
    1353             :          */
    1354             : #ifdef CMOCKA_UNIT_TEST_MODIFY_INTERNAL_FAIL
    1355             :         CMOCKA_UNIT_TEST_MODIFY_INTERNAL_FAIL
    1356             : #endif
    1357     3849543 :         return ret;
    1358             : }
    1359             : 
    1360             : /*
    1361             :   modify a record
    1362             : */
    1363     1293949 : static int ldb_kv_modify(struct ldb_kv_context *ctx)
    1364             : {
    1365     1293949 :         struct ldb_module *module = ctx->module;
    1366     1293949 :         struct ldb_request *req = ctx->req;
    1367     1293949 :         void *data = ldb_module_get_private(module);
    1368       31259 :         struct ldb_kv_private *ldb_kv =
    1369     1293949 :             talloc_get_type(data, struct ldb_kv_private);
    1370     1293949 :         int ret = LDB_SUCCESS;
    1371             : 
    1372     1293949 :         ret = ldb_kv_check_special_dn(module, req->op.mod.message);
    1373     1293949 :         if (ret != LDB_SUCCESS) {
    1374           0 :                 return ret;
    1375             :         }
    1376             : 
    1377     1293949 :         ldb_request_set_state(req, LDB_ASYNC_PENDING);
    1378             : 
    1379     1293949 :         if (ldb_kv_cache_load(module) != 0) {
    1380           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1381             :         }
    1382             : 
    1383     1293949 :         ret = ldb_kv_sub_transaction_start(ldb_kv);
    1384     1293949 :         if (ret != LDB_SUCCESS) {
    1385           0 :                 return ret;
    1386             :         }
    1387     1293949 :         ret = ldb_kv_modify_internal(module, req->op.mod.message, req);
    1388     1293949 :         if (ret != LDB_SUCCESS) {
    1389        5457 :                 int r = ldb_kv_sub_transaction_cancel(ldb_kv);
    1390        5457 :                 if (r != LDB_SUCCESS) {
    1391           0 :                         ldb_debug(
    1392             :                                 ldb_module_get_ctx(module),
    1393             :                                 LDB_DEBUG_FATAL,
    1394             :                                 __location__
    1395             :                                 ": Unable to roll back sub transaction");
    1396             :                 }
    1397        5457 :                 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
    1398        1441 :                         ldb_kv->operation_failed = true;
    1399             :                 }
    1400        5457 :                 return ret;
    1401             :         }
    1402     1288492 :         ret = ldb_kv_sub_transaction_commit(ldb_kv);
    1403             : 
    1404             : 
    1405     1288492 :         return ret;
    1406             : }
    1407             : 
    1408       81978 : static int ldb_kv_rename_internal(struct ldb_module *module,
    1409             :                            struct ldb_request *req,
    1410             :                            struct ldb_message *msg)
    1411             : {
    1412       81978 :         void *data = ldb_module_get_private(module);
    1413         163 :         struct ldb_kv_private *ldb_kv =
    1414       81978 :             talloc_get_type(data, struct ldb_kv_private);
    1415       81978 :         int ret = LDB_SUCCESS;
    1416             : 
    1417             :         /* Always delete first then add, to avoid conflicts with
    1418             :          * unique indexes. We rely on the transaction to make this
    1419             :          * atomic
    1420             :          */
    1421       81978 :         ret = ldb_kv_delete_internal(module, msg->dn);
    1422       81978 :         if (ret != LDB_SUCCESS) {
    1423           0 :                 return ret;
    1424             :         }
    1425             : 
    1426       81978 :         msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
    1427       81978 :         if (msg->dn == NULL) {
    1428           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1429             :         }
    1430             : 
    1431             :         /* We don't check single value as we can have more than 1 with
    1432             :          * deleted attributes. We could go through all elements but that's
    1433             :          * maybe not the most efficient way
    1434             :          */
    1435       81978 :         ret = ldb_kv_add_internal(module, ldb_kv, msg, false);
    1436             : 
    1437             :         /*
    1438             :          * To allow testing of the error recovery code in ldb_kv_rename
    1439             :          * cmocka tests can define CMOCKA_UNIT_TEST_RENAME_INTERNAL_FAIL
    1440             :          * to inject failures at this point.
    1441             :          */
    1442             : #ifdef CMOCKA_UNIT_TEST_RENAME_INTERNAL_FAIL
    1443             :         CMOCKA_UNIT_TEST_RENAME_INTERNAL_FAIL
    1444             : #endif
    1445       81978 :         return ret;
    1446             : }
    1447             : 
    1448             : /*
    1449             :   rename a record
    1450             : */
    1451       82087 : static int ldb_kv_rename(struct ldb_kv_context *ctx)
    1452             : {
    1453       82087 :         struct ldb_module *module = ctx->module;
    1454       82087 :         void *data = ldb_module_get_private(module);
    1455         163 :         struct ldb_kv_private *ldb_kv =
    1456       82087 :             talloc_get_type(data, struct ldb_kv_private);
    1457       82087 :         struct ldb_request *req = ctx->req;
    1458         163 :         struct ldb_message *msg;
    1459       82087 :         int ret = LDB_SUCCESS;
    1460         163 :         struct ldb_val  key, key_old;
    1461         163 :         struct ldb_dn *db_dn;
    1462       82087 :         bool valid_dn = false;
    1463             : 
    1464       82087 :         ldb_request_set_state(req, LDB_ASYNC_PENDING);
    1465             : 
    1466       82087 :         if (ldb_kv_cache_load(ctx->module) != 0) {
    1467           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1468             :         }
    1469             : 
    1470       82087 :         msg = ldb_msg_new(ctx);
    1471       82087 :         if (msg == NULL) {
    1472           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1473             :         }
    1474             : 
    1475             :         /* Check the new DN is reasonable */
    1476       82087 :         valid_dn = ldb_dn_validate(req->op.rename.newdn);
    1477       82087 :         if (valid_dn == false) {
    1478           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
    1479             :                                        "Invalid New DN: %s",
    1480             :                                        ldb_dn_get_linearized(req->op.rename.newdn));
    1481           0 :                 return LDB_ERR_INVALID_DN_SYNTAX;
    1482             :         }
    1483             : 
    1484             :         /* we need to fetch the old record to re-add under the new name */
    1485       82087 :         ret = ldb_kv_search_dn1(module, req->op.rename.olddn, msg, 0);
    1486       82087 :         if (ret == LDB_ERR_INVALID_DN_SYNTAX) {
    1487           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
    1488             :                                        "Invalid Old DN: %s",
    1489             :                                        ldb_dn_get_linearized(req->op.rename.newdn));
    1490           0 :                 return ret;
    1491       82087 :         } else if (ret != LDB_SUCCESS) {
    1492             :                 /* not finding the old record is an error */
    1493          46 :                 return ret;
    1494             :         }
    1495             : 
    1496             :         /* We need to, before changing the DB, check if the new DN
    1497             :          * exists, so we can return this error to the caller with an
    1498             :          * unmodified DB
    1499             :          *
    1500             :          * Even in GUID index mode we use ltdb_key_dn() as we are
    1501             :          * trying to figure out if this is just a case rename
    1502             :          */
    1503       82041 :         key = ldb_kv_key_dn(msg, req->op.rename.newdn);
    1504       82041 :         if (!key.data) {
    1505           0 :                 talloc_free(msg);
    1506           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1507             :         }
    1508             : 
    1509       82041 :         key_old = ldb_kv_key_dn(msg, req->op.rename.olddn);
    1510       82041 :         if (!key_old.data) {
    1511           0 :                 talloc_free(msg);
    1512           0 :                 talloc_free(key.data);
    1513           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1514             :         }
    1515             : 
    1516             :         /*
    1517             :          * Only declare a conflict if the new DN already exists,
    1518             :          * and it isn't a case change on the old DN
    1519             :          */
    1520       82041 :         if (key_old.length != key.length
    1521         332 :             || memcmp(key.data, key_old.data, key.length) != 0) {
    1522       81987 :                 ret = ldb_kv_search_base(
    1523             :                     module, msg, req->op.rename.newdn, &db_dn);
    1524       81987 :                 if (ret == LDB_SUCCESS) {
    1525          63 :                         ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
    1526       81924 :                 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
    1527       81763 :                         ret = LDB_SUCCESS;
    1528             :                 }
    1529             :         }
    1530             : 
    1531             :         /* finding the new record already in the DB is an error */
    1532             : 
    1533       81880 :         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
    1534          63 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
    1535             :                                        "Entry %s already exists",
    1536             :                                        ldb_dn_get_linearized(req->op.rename.newdn));
    1537             :         }
    1538       82041 :         if (ret != LDB_SUCCESS) {
    1539          63 :                 talloc_free(key_old.data);
    1540          63 :                 talloc_free(key.data);
    1541          63 :                 talloc_free(msg);
    1542          63 :                 return ret;
    1543             :         }
    1544             : 
    1545       81978 :         talloc_free(key_old.data);
    1546       81978 :         talloc_free(key.data);
    1547             : 
    1548             : 
    1549       81978 :         ret = ldb_kv_sub_transaction_start(ldb_kv);
    1550       81978 :         if (ret != LDB_SUCCESS) {
    1551           0 :                 talloc_free(msg);
    1552           0 :                 return ret;
    1553             :         }
    1554       81978 :         ret = ldb_kv_rename_internal(module, req, msg);
    1555       81978 :         if (ret != LDB_SUCCESS) {
    1556           0 :                 int r = ldb_kv_sub_transaction_cancel(ldb_kv);
    1557           0 :                 if (r != LDB_SUCCESS) {
    1558           0 :                         ldb_debug(
    1559             :                                 ldb_module_get_ctx(module),
    1560             :                                 LDB_DEBUG_FATAL,
    1561             :                                 __location__
    1562             :                                 ": Unable to roll back sub transaction");
    1563             :                 }
    1564           0 :                 talloc_free(msg);
    1565           0 :                 ldb_kv->operation_failed = true;
    1566           0 :                 return ret;
    1567             :         }
    1568       81978 :         ret = ldb_kv_sub_transaction_commit(ldb_kv);
    1569       81978 :         talloc_free(msg);
    1570             : 
    1571       81978 :         return ret;
    1572             : }
    1573             : 
    1574     2208893 : static int ldb_kv_start_trans(struct ldb_module *module)
    1575             : {
    1576     2208893 :         void *data = ldb_module_get_private(module);
    1577       16699 :         struct ldb_kv_private *ldb_kv =
    1578     2208893 :             talloc_get_type(data, struct ldb_kv_private);
    1579             : 
    1580     2208893 :         pid_t pid = getpid();
    1581             : 
    1582     2208893 :         if (ldb_kv->pid != pid) {
    1583           2 :                 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
    1584             :                                        __location__
    1585             :                                        ": Reusing ldb opened by pid %d in "
    1586             :                                        "process %d\n",
    1587             :                                        ldb_kv->pid,
    1588             :                                        pid);
    1589           2 :                 return LDB_ERR_PROTOCOL_ERROR;
    1590             :         }
    1591             : 
    1592             :         /* Do not take out the transaction lock on a read-only DB */
    1593     2208891 :         if (ldb_kv->read_only) {
    1594          20 :                 return LDB_ERR_UNWILLING_TO_PERFORM;
    1595             :         }
    1596             : 
    1597     2208871 :         if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
    1598           0 :                 return ldb_kv->kv_ops->error(ldb_kv);
    1599             :         }
    1600             : 
    1601     2208871 :         ldb_kv_index_transaction_start(
    1602             :                 module,
    1603             :                 ldb_kv->index_transaction_cache_size);
    1604             : 
    1605     2208871 :         ldb_kv->reindex_failed = false;
    1606     2208871 :         ldb_kv->operation_failed = false;
    1607             : 
    1608     2208871 :         return LDB_SUCCESS;
    1609             : }
    1610             : 
    1611             : /*
    1612             :  * Forward declaration to allow prepare_commit to in fact abort the
    1613             :  * transaction
    1614             :  */
    1615             : static int ldb_kv_del_trans(struct ldb_module *module);
    1616             : 
    1617     1941303 : static int ldb_kv_prepare_commit(struct ldb_module *module)
    1618             : {
    1619       15857 :         int ret;
    1620     1941303 :         void *data = ldb_module_get_private(module);
    1621       15857 :         struct ldb_kv_private *ldb_kv =
    1622     1941303 :             talloc_get_type(data, struct ldb_kv_private);
    1623     1941303 :         pid_t pid = getpid();
    1624             : 
    1625     1941303 :         if (ldb_kv->pid != pid) {
    1626           2 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
    1627             :                                        __location__
    1628             :                                        ": Reusing ldb opened by pid %d in "
    1629             :                                        "process %d\n",
    1630             :                                        ldb_kv->pid,
    1631             :                                        pid);
    1632           2 :                 return LDB_ERR_PROTOCOL_ERROR;
    1633             :         }
    1634             : 
    1635     1941301 :         if (!ldb_kv->kv_ops->transaction_active(ldb_kv)) {
    1636           0 :                 ldb_set_errstring(ldb_module_get_ctx(module),
    1637             :                                   "ltdb_prepare_commit() called "
    1638             :                                   "without transaction active");
    1639           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1640             :         }
    1641             : 
    1642             :         /*
    1643             :          * Check if the last re-index failed.
    1644             :          *
    1645             :          * This can happen if for example a duplicate value was marked
    1646             :          * unique.  We must not write a partial re-index into the DB.
    1647             :          */
    1648     1941301 :         if (ldb_kv->reindex_failed) {
    1649             :                 /*
    1650             :                  * We must instead abort the transaction so we get the
    1651             :                  * old values and old index back
    1652             :                  */
    1653           7 :                 ldb_kv_del_trans(module);
    1654           7 :                 ldb_set_errstring(ldb_module_get_ctx(module),
    1655             :                                   "Failure during re-index, so "
    1656             :                                   "transaction must be aborted.");
    1657           7 :                 return LDB_ERR_OPERATIONS_ERROR;
    1658             :         }
    1659             : 
    1660     1941294 :         ret = ldb_kv_index_transaction_commit(module);
    1661     1941294 :         if (ret != LDB_SUCCESS) {
    1662           0 :                 ldb_kv->kv_ops->abort_write(ldb_kv);
    1663           0 :                 return ret;
    1664             :         }
    1665             : 
    1666             :         /*
    1667             :          * If GUID indexing was toggled in this transaction, we repack at
    1668             :          * format version 2 if GUID indexing was enabled, or version 1 if
    1669             :          * it was disabled.
    1670             :          */
    1671     1941294 :         ret = ldb_kv_maybe_repack(ldb_kv);
    1672     1941294 :         if (ret != LDB_SUCCESS) {
    1673           0 :                 ldb_kv_del_trans(module);
    1674           0 :                 ldb_set_errstring(ldb_module_get_ctx(module),
    1675             :                                   "Failure during re-pack, so "
    1676             :                                   "transaction must be aborted.");
    1677           0 :                 return ret;
    1678             :         }
    1679             : 
    1680     1941294 :         if (ldb_kv->kv_ops->prepare_write(ldb_kv) != 0) {
    1681           0 :                 ret = ldb_kv->kv_ops->error(ldb_kv);
    1682           0 :                 ldb_debug_set(ldb_module_get_ctx(module),
    1683             :                               LDB_DEBUG_FATAL,
    1684             :                               "Failure during "
    1685             :                               "prepare_write): %s -> %s",
    1686           0 :                               ldb_kv->kv_ops->errorstr(ldb_kv),
    1687             :                               ldb_strerror(ret));
    1688           0 :                 return ret;
    1689             :         }
    1690             : 
    1691     1941294 :         ldb_kv->prepared_commit = true;
    1692             : 
    1693     1941294 :         return LDB_SUCCESS;
    1694             : }
    1695             : 
    1696     1941280 : static int ldb_kv_end_trans(struct ldb_module *module)
    1697             : {
    1698       15857 :         int ret;
    1699     1941280 :         void *data = ldb_module_get_private(module);
    1700       15857 :         struct ldb_kv_private *ldb_kv =
    1701     1941280 :             talloc_get_type(data, struct ldb_kv_private);
    1702             : 
    1703             :         /*
    1704             :          * If in batch mode and there has been an operation failure
    1705             :          * rollback the transaction rather than committing it to avoid
    1706             :          * any possible corruption
    1707             :          */
    1708     1941280 :         if (ldb_kv->batch_mode && ldb_kv->operation_failed) {
    1709           3 :                 ret = ldb_kv_del_trans( module);
    1710           3 :                 if (ret != LDB_SUCCESS) {
    1711           0 :                         ldb_debug_set(ldb_module_get_ctx(module),
    1712             :                                       LDB_DEBUG_FATAL,
    1713             :                                       "An operation failed during a batch mode "
    1714             :                                       "transaction. The transaction could not"
    1715             :                                       "be rolled back, ldb_kv_del_trans "
    1716             :                                       "returned (%s, %s)",
    1717           0 :                                       ldb_kv->kv_ops->errorstr(ldb_kv),
    1718             :                                       ldb_strerror(ret));
    1719             :                 } else {
    1720           3 :                         ldb_debug_set(ldb_module_get_ctx(module),
    1721             :                                       LDB_DEBUG_FATAL,
    1722             :                                       "An operation failed during a batch mode "
    1723             :                                       "transaction, the transaction was "
    1724             :                                       "rolled back");
    1725             :                 }
    1726           3 :                 return LDB_ERR_OPERATIONS_ERROR;
    1727             :         }
    1728             : 
    1729     1941277 :         if (!ldb_kv->prepared_commit) {
    1730        2611 :                 ret = ldb_kv_prepare_commit(module);
    1731        2611 :                 if (ret != LDB_SUCCESS) {
    1732           0 :                         return ret;
    1733             :                 }
    1734             :         }
    1735             : 
    1736     1941277 :         ldb_kv->prepared_commit = false;
    1737             : 
    1738     1941277 :         if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
    1739           0 :                 ret = ldb_kv->kv_ops->error(ldb_kv);
    1740           0 :                 ldb_asprintf_errstring(
    1741             :                     ldb_module_get_ctx(module),
    1742             :                     "Failure during tdb_transaction_commit(): %s -> %s",
    1743           0 :                     ldb_kv->kv_ops->errorstr(ldb_kv),
    1744             :                     ldb_strerror(ret));
    1745           0 :                 return ret;
    1746             :         }
    1747             : 
    1748     1925420 :         return LDB_SUCCESS;
    1749             : }
    1750             : 
    1751      267572 : static int ldb_kv_del_trans(struct ldb_module *module)
    1752             : {
    1753      267572 :         void *data = ldb_module_get_private(module);
    1754         835 :         struct ldb_kv_private *ldb_kv =
    1755      267572 :             talloc_get_type(data, struct ldb_kv_private);
    1756             : 
    1757      267572 :         if (ldb_kv_index_transaction_cancel(module) != 0) {
    1758           0 :                 ldb_kv->kv_ops->abort_write(ldb_kv);
    1759           0 :                 return ldb_kv->kv_ops->error(ldb_kv);
    1760             :         }
    1761             : 
    1762      267572 :         ldb_kv->kv_ops->abort_write(ldb_kv);
    1763      267572 :         return LDB_SUCCESS;
    1764             : }
    1765             : 
    1766             : /*
    1767             :   return sequenceNumber from @BASEINFO
    1768             : */
    1769    19209605 : static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
    1770             :                                   struct ldb_extended **ext)
    1771             : {
    1772     1122412 :         struct ldb_context *ldb;
    1773    19209605 :         struct ldb_module *module = ctx->module;
    1774    19209605 :         struct ldb_request *req = ctx->req;
    1775    19209605 :         void *data = ldb_module_get_private(module);
    1776     1122412 :         struct ldb_kv_private *ldb_kv =
    1777    19209605 :             talloc_get_type(data, struct ldb_kv_private);
    1778    19209605 :         TALLOC_CTX *tmp_ctx = NULL;
    1779     1122412 :         struct ldb_seqnum_request *seq;
    1780     1122412 :         struct ldb_seqnum_result *res;
    1781    19209605 :         struct ldb_message *msg = NULL;
    1782     1122412 :         struct ldb_dn *dn;
    1783     1122412 :         const char *date;
    1784    19209605 :         int ret = LDB_SUCCESS;
    1785             : 
    1786    19209605 :         ldb = ldb_module_get_ctx(module);
    1787             : 
    1788    19209605 :         seq = talloc_get_type(req->op.extended.data,
    1789             :                                 struct ldb_seqnum_request);
    1790    19209605 :         if (seq == NULL) {
    1791           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1792             :         }
    1793             : 
    1794    19209605 :         ldb_request_set_state(req, LDB_ASYNC_PENDING);
    1795             : 
    1796    19209605 :         if (ldb_kv->kv_ops->lock_read(module) != 0) {
    1797           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1798             :         }
    1799             : 
    1800    19209605 :         res = talloc_zero(req, struct ldb_seqnum_result);
    1801    19209605 :         if (res == NULL) {
    1802           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
    1803           0 :                 goto done;
    1804             :         }
    1805             : 
    1806    19209605 :         tmp_ctx = talloc_new(req);
    1807    19209605 :         if (tmp_ctx == NULL) {
    1808           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
    1809           0 :                 goto done;
    1810             :         }
    1811             : 
    1812    19209605 :         dn = ldb_dn_new(tmp_ctx, ldb, LDB_KV_BASEINFO);
    1813    19209605 :         if (dn == NULL) {
    1814           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
    1815           0 :                 goto done;
    1816             :         }
    1817             : 
    1818    19209605 :         msg = ldb_msg_new(tmp_ctx);
    1819    19209605 :         if (msg == NULL) {
    1820           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
    1821           0 :                 goto done;
    1822             :         }
    1823             : 
    1824    19209605 :         ret = ldb_kv_search_dn1(module, dn, msg, 0);
    1825    19209605 :         if (ret != LDB_SUCCESS) {
    1826           0 :                 goto done;
    1827             :         }
    1828             : 
    1829    19209605 :         switch (seq->type) {
    1830    19206086 :         case LDB_SEQ_HIGHEST_SEQ:
    1831    19206086 :                 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
    1832    19206086 :                 break;
    1833        3519 :         case LDB_SEQ_NEXT:
    1834        3519 :                 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
    1835        3519 :                 res->seq_num++;
    1836        3519 :                 break;
    1837           0 :         case LDB_SEQ_HIGHEST_TIMESTAMP:
    1838           0 :                 date = ldb_msg_find_attr_as_string(msg, LDB_KV_MOD_TIMESTAMP, NULL);
    1839           0 :                 if (date) {
    1840           0 :                         res->seq_num = ldb_string_to_time(date);
    1841             :                 } else {
    1842           0 :                         res->seq_num = 0;
    1843             :                         /* zero is as good as anything when we don't know */
    1844             :                 }
    1845           0 :                 break;
    1846             :         }
    1847             : 
    1848    19209605 :         *ext = talloc_zero(req, struct ldb_extended);
    1849    19209605 :         if (*ext == NULL) {
    1850           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
    1851           0 :                 goto done;
    1852             :         }
    1853    19209605 :         (*ext)->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
    1854    19209605 :         (*ext)->data = talloc_steal(*ext, res);
    1855             : 
    1856    19209605 : done:
    1857    19209605 :         talloc_free(tmp_ctx);
    1858             : 
    1859    19209605 :         ldb_kv->kv_ops->unlock_read(module);
    1860    19209605 :         return ret;
    1861             : }
    1862             : 
    1863   131035262 : static void ldb_kv_request_done(struct ldb_kv_context *ctx, int error)
    1864             : {
    1865     4383555 :         struct ldb_context *ldb;
    1866     4383555 :         struct ldb_request *req;
    1867     4383555 :         struct ldb_reply *ares;
    1868             : 
    1869   131035262 :         ldb = ldb_module_get_ctx(ctx->module);
    1870   131035262 :         req = ctx->req;
    1871             : 
    1872             :         /* if we already returned an error just return */
    1873   131035262 :         if (ldb_request_get_status(req) != LDB_SUCCESS) {
    1874           0 :                 return;
    1875             :         }
    1876             : 
    1877   131035262 :         ares = talloc_zero(req, struct ldb_reply);
    1878   131035262 :         if (!ares) {
    1879           0 :                 ldb_oom(ldb);
    1880           0 :                 req->callback(req, NULL);
    1881           0 :                 return;
    1882             :         }
    1883   131035262 :         ares->type = LDB_REPLY_DONE;
    1884   131035262 :         ares->error = error;
    1885             : 
    1886   131035262 :         req->callback(req, ares);
    1887             : }
    1888             : 
    1889           0 : static void ldb_kv_timeout(_UNUSED_ struct tevent_context *ev,
    1890             :                            _UNUSED_ struct tevent_timer *te,
    1891             :                            _UNUSED_ struct timeval t,
    1892             :                            void *private_data)
    1893             : {
    1894           0 :         struct ldb_kv_context *ctx;
    1895           0 :         ctx = talloc_get_type(private_data, struct ldb_kv_context);
    1896             : 
    1897           0 :         if (!ctx->request_terminated) {
    1898             :                 /* request is done now */
    1899           0 :                 ldb_kv_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
    1900             :         }
    1901             : 
    1902           0 :         if (ctx->spy) {
    1903             :                 /* neutralize the spy */
    1904           0 :                 ctx->spy->ctx = NULL;
    1905           0 :                 ctx->spy = NULL;
    1906             :         }
    1907           0 :         talloc_free(ctx);
    1908           0 : }
    1909             : 
    1910    19209605 : static void ldb_kv_request_extended_done(struct ldb_kv_context *ctx,
    1911             :                                          struct ldb_extended *ext,
    1912             :                                          int error)
    1913             : {
    1914     1122412 :         struct ldb_context *ldb;
    1915     1122412 :         struct ldb_request *req;
    1916     1122412 :         struct ldb_reply *ares;
    1917             : 
    1918    19209605 :         ldb = ldb_module_get_ctx(ctx->module);
    1919    19209605 :         req = ctx->req;
    1920             : 
    1921             :         /* if we already returned an error just return */
    1922    19209605 :         if (ldb_request_get_status(req) != LDB_SUCCESS) {
    1923           0 :                 return;
    1924             :         }
    1925             : 
    1926    19209605 :         ares = talloc_zero(req, struct ldb_reply);
    1927    19209605 :         if (!ares) {
    1928           0 :                 ldb_oom(ldb);
    1929           0 :                 req->callback(req, NULL);
    1930           0 :                 return;
    1931             :         }
    1932    19209605 :         ares->type = LDB_REPLY_DONE;
    1933    19209605 :         ares->response = ext;
    1934    19209605 :         ares->error = error;
    1935             : 
    1936    19209605 :         req->callback(req, ares);
    1937             : }
    1938             : 
    1939    19209605 : static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
    1940             : {
    1941    19209605 :         struct ldb_extended *ext = NULL;
    1942     1122412 :         int ret;
    1943             : 
    1944    19209605 :         if (strcmp(ctx->req->op.extended.oid,
    1945             :                    LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
    1946             :                 /* get sequence number */
    1947    19209605 :                 ret = ldb_kv_sequence_number(ctx, &ext);
    1948             :         } else {
    1949             :                 /* not recognized */
    1950           0 :                 ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
    1951             :         }
    1952             : 
    1953    19209605 :         ldb_kv_request_extended_done(ctx, ext, ret);
    1954    19209605 : }
    1955             : 
    1956   150299012 : static void ldb_kv_callback(struct tevent_context *ev,
    1957             :                             struct tevent_timer *te,
    1958             :                             struct timeval t,
    1959             :                             void *private_data)
    1960             : {
    1961     5507398 :         struct ldb_kv_context *ctx;
    1962     5507398 :         int ret;
    1963             : 
    1964   150299012 :         ctx = talloc_get_type(private_data, struct ldb_kv_context);
    1965             : 
    1966   150299012 :         if (ctx->request_terminated) {
    1967           0 :                 goto done;
    1968             :         }
    1969             : 
    1970   150299012 :         switch (ctx->req->operation) {
    1971   128601395 :         case LDB_SEARCH:
    1972   128601395 :                 ret = ldb_kv_search(ctx);
    1973   128601385 :                 break;
    1974     1098734 :         case LDB_ADD:
    1975     1098734 :                 ret = ldb_kv_add(ctx);
    1976     1098734 :                 break;
    1977     1293949 :         case LDB_MODIFY:
    1978     1293949 :                 ret = ldb_kv_modify(ctx);
    1979     1293949 :                 break;
    1980       13242 :         case LDB_DELETE:
    1981       13242 :                 ret = ldb_kv_delete(ctx);
    1982       13242 :                 break;
    1983       82087 :         case LDB_RENAME:
    1984       82087 :                 ret = ldb_kv_rename(ctx);
    1985       82087 :                 break;
    1986    19209605 :         case LDB_EXTENDED:
    1987    19209605 :                 ldb_kv_handle_extended(ctx);
    1988    19209605 :                 goto done;
    1989           0 :         default:
    1990             :                 /* no other op supported */
    1991           0 :                 ret = LDB_ERR_PROTOCOL_ERROR;
    1992             :         }
    1993             : 
    1994   131089397 :         if (!ctx->request_terminated) {
    1995             :                 /* request is done now */
    1996   131035262 :                 ldb_kv_request_done(ctx, ret);
    1997             :         }
    1998             : 
    1999       54135 : done:
    2000   150299000 :         if (ctx->spy) {
    2001             :                 /* neutralize the spy */
    2002   150267494 :                 ctx->spy->ctx = NULL;
    2003   150267494 :                 ctx->spy = NULL;
    2004             :         }
    2005   150299000 :         talloc_free(ctx);
    2006   150299000 : }
    2007             : 
    2008   150299032 : static int ldb_kv_request_destructor(void *ptr)
    2009             : {
    2010     5507398 :         struct ldb_kv_req_spy *spy =
    2011   150299032 :             talloc_get_type(ptr, struct ldb_kv_req_spy);
    2012             : 
    2013   150299032 :         if (spy->ctx != NULL) {
    2014       31538 :                 spy->ctx->spy = NULL;
    2015       31538 :                 spy->ctx->request_terminated = true;
    2016       31538 :                 spy->ctx = NULL;
    2017             :         }
    2018             : 
    2019   150299032 :         return 0;
    2020             : }
    2021             : 
    2022   150299132 : static int ldb_kv_handle_request(struct ldb_module *module,
    2023             :                                  struct ldb_request *req)
    2024             : {
    2025     5507421 :         struct ldb_control *control_permissive;
    2026     5507421 :         struct ldb_context *ldb;
    2027     5507421 :         struct tevent_context *ev;
    2028     5507421 :         struct ldb_kv_context *ac;
    2029     5507421 :         struct tevent_timer *te;
    2030     5507421 :         struct timeval tv;
    2031     5507421 :         unsigned int i;
    2032             : 
    2033   150299132 :         ldb = ldb_module_get_ctx(module);
    2034             : 
    2035   150299132 :         control_permissive = ldb_request_get_control(req,
    2036             :                                         LDB_CONTROL_PERMISSIVE_MODIFY_OID);
    2037             : 
    2038   536168009 :         for (i = 0; req->controls && req->controls[i]; i++) {
    2039   380361542 :                 if (req->controls[i]->critical &&
    2040          63 :                     req->controls[i] != control_permissive) {
    2041          86 :                         ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
    2042          63 :                                                req->controls[i]->oid);
    2043          86 :                         return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
    2044             :                 }
    2045             :         }
    2046             : 
    2047   150299046 :         if (req->starttime == 0 || req->timeout == 0) {
    2048           0 :                 ldb_set_errstring(ldb, "Invalid timeout settings");
    2049           0 :                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
    2050             :         }
    2051             : 
    2052   150299046 :         ev = ldb_handle_get_event_context(req->handle);
    2053             : 
    2054   150299046 :         ac = talloc_zero(ldb, struct ldb_kv_context);
    2055   150299046 :         if (ac == NULL) {
    2056           0 :                 ldb_oom(ldb);
    2057           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    2058             :         }
    2059             : 
    2060   150299046 :         ac->module = module;
    2061   150299046 :         ac->req = req;
    2062             : 
    2063   150299046 :         tv.tv_sec = 0;
    2064   150299046 :         tv.tv_usec = 0;
    2065   150299046 :         te = tevent_add_timer(ev, ac, tv, ldb_kv_callback, ac);
    2066   150299046 :         if (NULL == te) {
    2067           0 :                 talloc_free(ac);
    2068           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    2069             :         }
    2070             : 
    2071   150299046 :         if (req->timeout > 0) {
    2072   150299046 :                 tv.tv_sec = req->starttime + req->timeout;
    2073   150299046 :                 tv.tv_usec = 0;
    2074   155806444 :                 ac->timeout_event =
    2075   150299046 :                     tevent_add_timer(ev, ac, tv, ldb_kv_timeout, ac);
    2076   150299046 :                 if (NULL == ac->timeout_event) {
    2077           0 :                         talloc_free(ac);
    2078           0 :                         return LDB_ERR_OPERATIONS_ERROR;
    2079             :                 }
    2080             :         }
    2081             : 
    2082   150299046 :         ac->timeout_timeval = tv;
    2083             : 
    2084             :         /* set a spy so that we do not try to use the request context
    2085             :          * if it is freed before ltdb_callback fires */
    2086   150299046 :         ac->spy = talloc(req, struct ldb_kv_req_spy);
    2087   150299046 :         if (NULL == ac->spy) {
    2088           0 :                 talloc_free(ac);
    2089           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    2090             :         }
    2091   150299046 :         ac->spy->ctx = ac;
    2092             : 
    2093   150299046 :         talloc_set_destructor((TALLOC_CTX *)ac->spy, ldb_kv_request_destructor);
    2094             : 
    2095   150299046 :         return LDB_SUCCESS;
    2096             : }
    2097             : 
    2098     1181392 : static int ldb_kv_init_rootdse(struct ldb_module *module)
    2099             : {
    2100             :         /* ignore errors on this - we expect it for non-sam databases */
    2101     1181392 :         ldb_mod_register_control(module, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
    2102             : 
    2103             :         /* there can be no module beyond the backend, just return */
    2104     1181392 :         return LDB_SUCCESS;
    2105             : }
    2106             : 
    2107   106176451 : static int ldb_kv_lock_read(struct ldb_module *module)
    2108             : {
    2109   106176451 :         void *data = ldb_module_get_private(module);
    2110     6070311 :         struct ldb_kv_private *ldb_kv =
    2111   106176451 :             talloc_get_type(data, struct ldb_kv_private);
    2112   106176451 :         return ldb_kv->kv_ops->lock_read(module);
    2113             : }
    2114             : 
    2115   106176437 : static int ldb_kv_unlock_read(struct ldb_module *module)
    2116             : {
    2117   106176437 :         void *data = ldb_module_get_private(module);
    2118     6070311 :         struct ldb_kv_private *ldb_kv =
    2119   106176437 :             talloc_get_type(data, struct ldb_kv_private);
    2120   106176437 :         return ldb_kv->kv_ops->unlock_read(module);
    2121             : }
    2122             : 
    2123             : static const struct ldb_module_ops ldb_kv_ops = {
    2124             :     .name = "tdb",
    2125             :     .init_context = ldb_kv_init_rootdse,
    2126             :     .search = ldb_kv_handle_request,
    2127             :     .add = ldb_kv_handle_request,
    2128             :     .modify = ldb_kv_handle_request,
    2129             :     .del = ldb_kv_handle_request,
    2130             :     .rename = ldb_kv_handle_request,
    2131             :     .extended = ldb_kv_handle_request,
    2132             :     .start_transaction = ldb_kv_start_trans,
    2133             :     .end_transaction = ldb_kv_end_trans,
    2134             :     .prepare_commit = ldb_kv_prepare_commit,
    2135             :     .del_transaction = ldb_kv_del_trans,
    2136             :     .read_lock = ldb_kv_lock_read,
    2137             :     .read_unlock = ldb_kv_unlock_read,
    2138             : };
    2139             : 
    2140     1181542 : int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
    2141             :                       const char *name,
    2142             :                       struct ldb_context *ldb,
    2143             :                       const char *options[],
    2144             :                       struct ldb_module **_module)
    2145             : {
    2146     1181542 :         if (getenv("LDB_WARN_UNINDEXED")) {
    2147           0 :                 ldb_kv->warn_unindexed = true;
    2148             :         }
    2149             : 
    2150     1181542 :         if (getenv("LDB_WARN_REINDEX")) {
    2151           0 :                 ldb_kv->warn_reindex = true;
    2152             :         }
    2153             : 
    2154     1181542 :         ldb_kv->sequence_number = 0;
    2155             : 
    2156     1181542 :         ldb_kv->pid = getpid();
    2157             : 
    2158     1181542 :         ldb_kv->pack_format_override = 0;
    2159             : 
    2160     1181542 :         ldb_kv->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
    2161     1181542 :         if (!ldb_kv->module) {
    2162           0 :                 ldb_oom(ldb);
    2163           0 :                 talloc_free(ldb_kv);
    2164           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    2165             :         }
    2166     1181542 :         ldb_module_set_private(ldb_kv->module, ldb_kv);
    2167     1181542 :         talloc_steal(ldb_kv->module, ldb_kv);
    2168             : 
    2169     1181542 :         if (ldb_kv_cache_load(ldb_kv->module) != 0) {
    2170           0 :                 ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
    2171             :                                        "records for backend '%s'", name);
    2172           0 :                 talloc_free(ldb_kv->module);
    2173           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    2174             :         }
    2175             : 
    2176     1181542 :         *_module = ldb_kv->module;
    2177             :         /*
    2178             :          * Set or override the maximum key length
    2179             :          *
    2180             :          * The ldb_mdb code will have set this to 511, but our tests
    2181             :          * set this even smaller (to make the tests more practical).
    2182             :          *
    2183             :          * This must only be used for the selftest as the length
    2184             :          * becomes encoded in the index keys.
    2185             :          */
    2186             :         {
    2187       40796 :                 const char *len_str =
    2188     1181542 :                         ldb_options_find(ldb, options,
    2189             :                                          "max_key_len_for_self_test");
    2190     1181542 :                 if (len_str != NULL) {
    2191          32 :                         unsigned len = strtoul(len_str, NULL, 0);
    2192          32 :                         ldb_kv->max_key_length = len;
    2193             :                 }
    2194             :         }
    2195             : 
    2196             :         /*
    2197             :          * Usually the presence of GUID indexing determines the pack format
    2198             :          * we use but in certain circumstances such as downgrading an
    2199             :          * MDB-backed database, we want to override the target pack format.
    2200             :          *
    2201             :          * We set/get opaques here because in the Samba partitions module,
    2202             :          * 'options' are not passed correctly so sub-databases can't see
    2203             :          * the options they need.
    2204             :          */
    2205             :         {
    2206       40796 :                 const char *pack_format_override =
    2207     1181542 :                         ldb_options_find(ldb, options, "pack_format_override");
    2208     1181542 :                 if (pack_format_override != NULL) {
    2209           6 :                         int ret;
    2210          12 :                         ldb_kv->pack_format_override =
    2211           6 :                                 strtoul(pack_format_override, NULL, 0);
    2212          12 :                         ret = ldb_set_opaque(ldb,
    2213             :                                              "pack_format_override",
    2214           6 :                              (void *)(intptr_t)ldb_kv->pack_format_override);
    2215           6 :                         if (ret != LDB_SUCCESS) {
    2216           0 :                                 talloc_free(ldb_kv->module);
    2217           0 :                                 return ldb_module_operr(ldb_kv->module);
    2218             :                         }
    2219             :                 } else {
    2220             :                         /*
    2221             :                          * NULL -> 0 is fine, otherwise we get back
    2222             :                          * the number we needed
    2223             :                          */
    2224       40790 :                         ldb_kv->pack_format_override
    2225     1181536 :                                 = (intptr_t)ldb_get_opaque(ldb,
    2226             :                                                    "pack_format_override");
    2227             :                 }
    2228             :         }
    2229             : 
    2230             :         /*
    2231             :          * Override full DB scans
    2232             :          *
    2233             :          * A full DB scan is expensive on a large database.  This
    2234             :          * option is for testing to show that the full DB scan is not
    2235             :          * triggered.
    2236             :          */
    2237             :         {
    2238       40796 :                 const char *len_str =
    2239     1181542 :                         ldb_options_find(ldb, options,
    2240             :                                          "disable_full_db_scan_for_self_test");
    2241     1181542 :                 if (len_str != NULL) {
    2242         602 :                         ldb_kv->disable_full_db_scan = true;
    2243             :                 }
    2244             :         }
    2245             : 
    2246             :         /*
    2247             :          * Set the size of the transaction index cache.
    2248             :          * If the ldb option "transaction_index_cache_size" is set use that
    2249             :          * otherwise use DEFAULT_INDEX_CACHE_SIZE
    2250             :          */
    2251     1181542 :         ldb_kv->index_transaction_cache_size = DEFAULT_INDEX_CACHE_SIZE;
    2252             :         {
    2253     1181542 :                 const char *size = ldb_options_find(
    2254             :                         ldb,
    2255             :                         options,
    2256             :                         "transaction_index_cache_size");
    2257     1181542 :                 if (size != NULL) {
    2258         341 :                         size_t cache_size = 0;
    2259         341 :                         errno = 0;
    2260             : 
    2261         341 :                         cache_size = strtoul( size, NULL, 0);
    2262         341 :                         if (cache_size == 0 || errno == ERANGE) {
    2263           2 :                                 ldb_debug(
    2264             :                                         ldb,
    2265             :                                         LDB_DEBUG_WARNING,
    2266             :                                         "Invalid transaction_index_cache_size "
    2267             :                                         "value [%s], using default(%d)\n",
    2268             :                                         size,
    2269             :                                         DEFAULT_INDEX_CACHE_SIZE);
    2270             :                         } else {
    2271         339 :                                 ldb_kv->index_transaction_cache_size =
    2272             :                                         cache_size;
    2273             :                         }
    2274             :                 }
    2275             :         }
    2276             :         /*
    2277             :          * Set batch mode operation.
    2278             :          * This disables the nested sub transactions, and increases the
    2279             :          * chance of index corruption.  If using this mode the transaction
    2280             :          * commit will be aborted if any operation fails.
    2281             :          */
    2282             :         {
    2283     1181542 :                 const char *batch_mode = ldb_options_find(
    2284             :                         ldb, options, "batch_mode");
    2285     1181542 :                 if (batch_mode != NULL) {
    2286         341 :                         ldb_kv->batch_mode = true;
    2287             :                 }
    2288             :         }
    2289             : 
    2290     1140746 :         return LDB_SUCCESS;
    2291             : }

Generated by: LCOV version 1.14