LCOV - code coverage report
Current view: top level - source4/dsdb/samdb/ldb_modules - objectguid.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 82 104 78.8 %
Date: 2023-11-21 12:31:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* 
       2             :    ldb database library
       3             : 
       4             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
       5             :    Copyright (C) Andrew Tridgell 2005
       6             :    Copyright (C) Simo Sorce  2004-2008
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : /*
      23             :  *  Name: ldb
      24             :  *
      25             :  *  Component: ldb objectguid module
      26             :  *
      27             :  *  Description: add a unique objectGUID onto every new record
      28             :  *
      29             :  *  Author: Simo Sorce
      30             :  */
      31             : 
      32             : #include "includes.h"
      33             : #include "ldb_module.h"
      34             : #include "dsdb/samdb/samdb.h"
      35             : #include "dsdb/samdb/ldb_modules/util.h"
      36             : #include "librpc/gen_ndr/ndr_misc.h"
      37             : #include "param/param.h"
      38             : 
      39             : /*
      40             :   add a time element to a record
      41             : */
      42        5815 : static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
      43             : {
      44         202 :         char *s;
      45         202 :         int ret;
      46             : 
      47        5815 :         if (ldb_msg_find_element(msg, attr) != NULL) {
      48           0 :                 return LDB_SUCCESS;
      49             :         }
      50             : 
      51        5815 :         s = ldb_timestring(msg, t);
      52        5815 :         if (s == NULL) {
      53           0 :                 return LDB_ERR_OPERATIONS_ERROR;
      54             :         }
      55             : 
      56             :         /* always set as replace. This works because on add ops, the flag
      57             :            is ignored */
      58        5815 :         ret = ldb_msg_append_string(msg, attr, s, LDB_FLAG_MOD_REPLACE);
      59        5815 :         if (ret != LDB_SUCCESS) {
      60           0 :                 return ret;
      61             :         }
      62             : 
      63        5613 :         return LDB_SUCCESS;
      64             : }
      65             : 
      66             : /*
      67             :   add a uint64_t element to a record
      68             : */
      69        5815 : static int add_uint64_element(struct ldb_context *ldb, struct ldb_message *msg,
      70             :                               const char *attr, uint64_t v)
      71             : {
      72         202 :         int ret;
      73             : 
      74        5815 :         if (ldb_msg_find_element(msg, attr) != NULL) {
      75           0 :                 return LDB_SUCCESS;
      76             :         }
      77             : 
      78             :         /* always set as replace. This works because on add ops, the flag
      79             :            is ignored */
      80        5815 :         ret = samdb_msg_append_uint64(ldb, msg, msg, attr, v, LDB_FLAG_MOD_REPLACE);
      81        5815 :         if (ret != LDB_SUCCESS) {
      82           0 :                 return ret;
      83             :         }
      84             : 
      85        5613 :         return LDB_SUCCESS;
      86             : }
      87             : 
      88             : struct og_context {
      89             :         struct ldb_module *module;
      90             :         struct ldb_request *req;
      91             : };
      92             : 
      93             : /* add_record: add objectGUID and timestamp attributes */
      94        2281 : static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
      95             : {
      96          85 :         struct ldb_context *ldb;
      97          85 :         struct ldb_request *down_req;
      98          85 :         struct ldb_message *msg;
      99          85 :         struct ldb_message_element *el;
     100          85 :         struct GUID guid;
     101          85 :         uint64_t seq_num;
     102          85 :         int ret;
     103        2281 :         time_t t = time(NULL);
     104          85 :         struct og_context *ac;
     105             : 
     106        2281 :         ldb = ldb_module_get_ctx(module);
     107             : 
     108        2281 :         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
     109             : 
     110             :         /* do not manipulate our control entries */
     111        2281 :         if (ldb_dn_is_special(req->op.add.message->dn)) {
     112           0 :                 return ldb_next_request(module, req);
     113             :         }
     114             : 
     115        2281 :         el = ldb_msg_find_element(req->op.add.message, "objectGUID");
     116        2281 :         if (el != NULL) {
     117           0 :                 ldb_set_errstring(ldb,
     118             :                                   "objectguid: objectGUID must not be specified!");
     119           0 :                 return LDB_ERR_UNWILLING_TO_PERFORM;
     120             :         }
     121             : 
     122        2281 :         ac = talloc(req, struct og_context);
     123        2281 :         if (ac == NULL) {
     124           0 :                 return ldb_oom(ldb);
     125             :         }
     126        2281 :         ac->module = module;
     127        2281 :         ac->req = req;
     128             : 
     129             :         /* we have to copy the message as the caller might have it as a const */
     130        2281 :         msg = ldb_msg_copy_shallow(ac, req->op.add.message);
     131        2281 :         if (msg == NULL) {
     132           0 :                 talloc_free(ac);
     133           0 :                 return ldb_operr(ldb);
     134             :         }
     135             : 
     136             :         /* a new GUID */
     137        2281 :         guid = GUID_random();
     138             : 
     139        2281 :         ret = dsdb_msg_add_guid(msg, &guid, "objectGUID");
     140        2281 :         if (ret != LDB_SUCCESS) {
     141           0 :                 return ret;
     142             :         }
     143             :         
     144        4562 :         if (add_time_element(msg, "whenCreated", t) != LDB_SUCCESS ||
     145        2281 :             add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
     146           0 :                 return ldb_operr(ldb);
     147             :         }
     148             : 
     149             :         /* Get a sequence number from the backend */
     150        2281 :         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
     151        2281 :         if (ret == LDB_SUCCESS) {
     152        2281 :                 if (add_uint64_element(ldb, msg, "uSNCreated",
     153        2281 :                                        seq_num) != LDB_SUCCESS ||
     154        2281 :                     add_uint64_element(ldb, msg, "uSNChanged",
     155             :                                        seq_num) != LDB_SUCCESS) {
     156           0 :                         return ldb_operr(ldb);
     157             :                 }
     158             :         }
     159             : 
     160        2281 :         ret = ldb_build_add_req(&down_req, ldb, ac,
     161             :                                 msg,
     162             :                                 req->controls,
     163             :                                 req, dsdb_next_callback,
     164             :                                 req);
     165        2281 :         LDB_REQ_SET_LOCATION(down_req);
     166        2281 :         if (ret != LDB_SUCCESS) {
     167           0 :                 return ret;
     168             :         }
     169             : 
     170             :         /* go on with the call chain */
     171        2281 :         return ldb_next_request(module, down_req);
     172             : }
     173             : 
     174             : /* modify_record: update timestamps */
     175        1260 : static int objectguid_modify(struct ldb_module *module, struct ldb_request *req)
     176             : {
     177          33 :         struct ldb_context *ldb;
     178          33 :         struct ldb_request *down_req;
     179          33 :         struct ldb_message *msg;
     180          33 :         struct ldb_message_element *el;
     181          33 :         int ret;
     182        1260 :         time_t t = time(NULL);
     183          33 :         uint64_t seq_num;
     184          33 :         struct og_context *ac;
     185             : 
     186        1260 :         ldb = ldb_module_get_ctx(module);
     187             : 
     188        1260 :         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_modify_record\n");
     189             : 
     190             :         /* do not manipulate our control entries */
     191        1260 :         if (ldb_dn_is_special(req->op.mod.message->dn)) {
     192           7 :                 return ldb_next_request(module, req);
     193             :         }
     194             : 
     195        1253 :         el = ldb_msg_find_element(req->op.mod.message, "objectGUID");
     196        1253 :         if (el != NULL) {
     197           0 :                 ldb_set_errstring(ldb,
     198             :                                   "objectguid: objectGUID must not be specified!");
     199           0 :                 return LDB_ERR_CONSTRAINT_VIOLATION;
     200             :         }
     201             : 
     202        1253 :         ac = talloc(req, struct og_context);
     203        1253 :         if (ac == NULL) {
     204           0 :                 return ldb_oom(ldb);
     205             :         }
     206        1253 :         ac->module = module;
     207        1253 :         ac->req = req;
     208             : 
     209             :         /* we have to copy the message as the caller might have it as a const */
     210        1253 :         msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
     211        1253 :         if (msg == NULL) {
     212           0 :                 return ldb_operr(ldb);
     213             :         }
     214             : 
     215        1253 :         if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
     216           0 :                 return ldb_operr(ldb);
     217             :         }
     218             : 
     219             :         /* Get a sequence number from the backend */
     220        1253 :         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
     221        1253 :         if (ret == LDB_SUCCESS) {
     222        1253 :                 if (add_uint64_element(ldb, msg, "uSNChanged",
     223             :                                        seq_num) != LDB_SUCCESS) {
     224           0 :                         return ldb_operr(ldb);
     225             :                 }
     226             :         }
     227             : 
     228        1253 :         ret = ldb_build_mod_req(&down_req, ldb, ac,
     229             :                                 msg,
     230             :                                 req->controls,
     231             :                                 req, dsdb_next_callback,
     232             :                                 req);
     233        1253 :         LDB_REQ_SET_LOCATION(down_req);
     234        1253 :         if (ret != LDB_SUCCESS) {
     235           0 :                 return ret;
     236             :         }
     237             : 
     238             :         /* go on with the call chain */
     239        1253 :         return ldb_next_request(module, down_req);
     240             : }
     241             : 
     242             : static const struct ldb_module_ops ldb_objectguid_module_ops = {
     243             :         .name          = "objectguid",
     244             :         .add           = objectguid_add,
     245             :         .modify        = objectguid_modify
     246             : };
     247             : 
     248        5834 : int ldb_objectguid_module_init(const char *version)
     249             : {
     250        5834 :         LDB_MODULE_CHECK_VERSION(version);
     251        5834 :         return ldb_register_module(&ldb_objectguid_module_ops);
     252             : }

Generated by: LCOV version 1.14