LCOV - code coverage report
Current view: top level - source4/dsdb/samdb/ldb_modules - subtree_delete.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 35 41 85.4 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* 
       2             :    ldb database library
       3             : 
       4             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2007
       5             :    Copyright (C) Andrew Tridgell <tridge@samba.org> 2009
       6             :    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
       7             :    Copyright (C) Simo Sorce <idra@samba.org> 2008
       8             :    Copyright (C) Matthias Dieter Wallnöfer 2010
       9             : 
      10             :    This program is free software; you can redistribute it and/or modify
      11             :    it under the terms of the GNU General Public License as published by
      12             :    the Free Software Foundation; either version 3 of the License, or
      13             :    (at your option) any later version.
      14             :    
      15             :    This program is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :    GNU General Public License for more details.
      19             :    
      20             :    You should have received a copy of the GNU General Public License
      21             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      22             : */
      23             : 
      24             : /*
      25             :  *  Name: ldb
      26             :  *
      27             :  *  Component: ldb subtree delete module
      28             :  *
      29             :  *  Description: Delete of a subtree in LDB
      30             :  *
      31             :  *  Author: Andrew Bartlett
      32             :  */
      33             : 
      34             : #include "includes.h"
      35             : #include <ldb.h>
      36             : #include <ldb_module.h>
      37             : #include "dsdb/samdb/ldb_modules/util.h"
      38             : #include "dsdb/common/util.h"
      39             : 
      40             : 
      41       72783 : static int subtree_delete(struct ldb_module *module, struct ldb_request *req)
      42             : {
      43         148 :         static const char * const attrs[] = { NULL };
      44       72783 :         struct ldb_result *res = NULL;
      45         148 :         uint32_t flags;
      46         148 :         unsigned int i;
      47         148 :         int ret;
      48             : 
      49       72783 :         if (ldb_dn_is_special(req->op.del.dn)) {
      50             :                 /* do not manipulate our control entries */
      51           1 :                 return ldb_next_request(module, req);
      52             :         }
      53             : 
      54             :         /* see if we have any children */
      55       72782 :         ret = dsdb_module_search(module, req, &res, req->op.del.dn,
      56             :                                  LDB_SCOPE_ONELEVEL, attrs,
      57             :                                  DSDB_FLAG_NEXT_MODULE,
      58             :                                  req,
      59             :                                  "(objectClass=*)");
      60       72782 :         if (ret != LDB_SUCCESS) {
      61           0 :                 talloc_free(res);
      62           0 :                 return ret;
      63             :         }
      64       72782 :         if (res->count == 0) {
      65       69849 :                 talloc_free(res);
      66       69849 :                 return ldb_next_request(module, req);
      67             :         }
      68             : 
      69        2933 :         if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID) == NULL) {
      70             :                 /* Do not add any DN outputs to this error string!
      71             :                  * Some MMC consoles (eg release 2000) have a strange
      72             :                  * bug and prevent subtree deletes afterwards. */
      73           9 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
      74             :                                        "subtree_delete: Unable to "
      75             :                                        "delete a non-leaf node "
      76             :                                        "(it has %u children)!",
      77           9 :                                        res->count);
      78           9 :                 talloc_free(res);
      79           9 :                 return LDB_ERR_NOT_ALLOWED_ON_NON_LEAF;
      80             :         }
      81             : 
      82             :         /*
      83             :          * we need to start from the top since other LDB modules could
      84             :          * enforce constraints (eg "objectclass" and "samldb" do so).
      85             :          *
      86             :          * We pass DSDB_FLAG_AS_SYSTEM as the acl module above us
      87             :          * has already checked for SEC_ADS_DELETE_TREE.
      88             :          */
      89        2924 :         flags = DSDB_FLAG_TOP_MODULE |
      90             :                 DSDB_FLAG_AS_SYSTEM |
      91             :                 DSDB_FLAG_TRUSTED |
      92             :                 DSDB_TREE_DELETE;
      93        2924 :         if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) != NULL) {
      94           0 :                 flags |= DSDB_MODIFY_RELAX;
      95             :         }
      96             : 
      97             :         /*
      98             :          * The net result of this code is that the leaf nodes are
      99             :          * deleted first, as the parent is only deleted once these
     100             :          * calls (and the delete calls recursive within these)
     101             :          * complete.
     102             :          */
     103       32255 :         for (i = 0; i < res->count; i++) {
     104       29331 :                 ret = dsdb_module_del(module, res->msgs[i]->dn, flags, req);
     105       29331 :                 if (ret != LDB_SUCCESS) {
     106           0 :                         return ret;
     107             :                 }
     108             :         }
     109             : 
     110        2924 :         talloc_free(res);
     111             : 
     112        2924 :         return ldb_next_request(module, req);
     113             : }
     114             : 
     115      182004 : static int subtree_delete_init(struct ldb_module *module)
     116             : {
     117        6016 :         struct ldb_context *ldb;
     118        6016 :         int ret;
     119             : 
     120      182004 :         ldb = ldb_module_get_ctx(module);
     121             : 
     122      182004 :         ret = ldb_mod_register_control(module, LDB_CONTROL_TREE_DELETE_OID);
     123      182004 :         if (ret != LDB_SUCCESS) {
     124           0 :                 ldb_debug(ldb, LDB_DEBUG_ERROR,
     125             :                         "subtree_delete: Unable to register control with rootdse!\n");
     126           0 :                 return ldb_operr(ldb);
     127             :         }
     128             : 
     129      182004 :         return ldb_next_init(module);
     130             : }
     131             : 
     132             : static const struct ldb_module_ops ldb_subtree_delete_module_ops = {
     133             :         .name              = "subtree_delete",
     134             :         .init_context      = subtree_delete_init,
     135             :         .del               = subtree_delete
     136             : };
     137             : 
     138        6040 : int ldb_subtree_delete_module_init(const char *version)
     139             : {
     140        6040 :         LDB_MODULE_CHECK_VERSION(version);
     141        6040 :         return ldb_register_module(&ldb_subtree_delete_module_ops);
     142             : }

Generated by: LCOV version 1.14