s4:dsdb/subtree_delete: delete from the leafs to the root (bug #7711)
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / subtree_delete.c
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 static int subtree_delete_sort(struct ldb_message **m1,
42                                struct ldb_message **m2,
43                                void *private_data)
44 {
45         struct ldb_dn *dn1 = (*m1)->dn;
46         struct ldb_dn *dn2 = (*m2)->dn;
47
48         /*
49          * This sorts in tree order, children first
50          */
51         return ldb_dn_compare(dn1, dn2);
52 }
53
54 static int subtree_delete(struct ldb_module *module, struct ldb_request *req)
55 {
56         static const char * const attrs[] = { NULL };
57         struct ldb_result *res = NULL;
58         uint32_t flags;
59         unsigned int i;
60         int ret;
61
62         if (ldb_dn_is_special(req->op.del.dn)) {
63                 /* do not manipulate our control entries */
64                 return ldb_next_request(module, req);
65         }
66
67         /* see if we have any children */
68         ret = dsdb_module_search(module, req, &res, req->op.del.dn,
69                                  LDB_SCOPE_ONELEVEL, attrs,
70                                  DSDB_FLAG_NEXT_MODULE,
71                                  req,
72                                  "(objectClass=*)");
73         if (ret != LDB_SUCCESS) {
74                 talloc_free(res);
75                 return ret;
76         }
77         if (res->count == 0) {
78                 talloc_free(res);
79                 return ldb_next_request(module, req);
80         }
81
82         if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID) == NULL) {
83                 /* Do not add any DN outputs to this error string!
84                  * Some MMC consoles (eg release 2000) have a strange
85                  * bug and prevent subtree deletes afterwards. */
86                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
87                                        "subtree_delete: Unable to "
88                                        "delete a non-leaf node "
89                                        "(it has %u children)!",
90                                        res->count);
91                 talloc_free(res);
92                 return LDB_ERR_NOT_ALLOWED_ON_NON_LEAF;
93         }
94
95         /*
96          * First we sort the results from the leaf to the root
97          */
98         LDB_TYPESAFE_QSORT(res->msgs, res->count, NULL,
99                            subtree_delete_sort);
100
101         /*
102          * we need to start from the top since other LDB modules could
103          * enforce constraints (eg "objectclass" and "samldb" do so).
104          *
105          * We pass DSDB_FLAG_AS_SYSTEM as the acl module above us
106          * has already checked for SEC_ADS_DELETE_TREE.
107          */
108         flags = DSDB_FLAG_TOP_MODULE |
109                 DSDB_FLAG_AS_SYSTEM |
110                 DSDB_FLAG_TRUSTED |
111                 DSDB_TREE_DELETE;
112         if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) != NULL) {
113                 flags |= DSDB_MODIFY_RELAX;
114         }
115
116         for (i = 0; i < res->count; i++) {
117                 ret = dsdb_module_del(module, res->msgs[i]->dn, flags, req);
118                 if (ret != LDB_SUCCESS) {
119                         return ret;
120                 }
121         }
122
123         talloc_free(res);
124
125         return ldb_next_request(module, req);
126 }
127
128 static int subtree_delete_init(struct ldb_module *module)
129 {
130         struct ldb_context *ldb;
131         int ret;
132
133         ldb = ldb_module_get_ctx(module);
134
135         ret = ldb_mod_register_control(module, LDB_CONTROL_TREE_DELETE_OID);
136         if (ret != LDB_SUCCESS) {
137                 ldb_debug(ldb, LDB_DEBUG_ERROR,
138                         "subtree_delete: Unable to register control with rootdse!\n");
139                 return ldb_operr(ldb);
140         }
141
142         return ldb_next_init(module);
143 }
144
145 static const struct ldb_module_ops ldb_subtree_delete_module_ops = {
146         .name              = "subtree_delete",
147         .init_context      = subtree_delete_init,
148         .del               = subtree_delete
149 };
150
151 int ldb_subtree_delete_module_init(const char *version)
152 {
153         LDB_MODULE_CHECK_VERSION(version);
154         return ldb_register_module(&ldb_subtree_delete_module_ops);
155 }