4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2007
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Component: ldb subtree delete (prevention) module
26 * Description: Prevent deletion of a subtree in LDB
28 * Author: Andrew Bartlett
31 #include "ldb_includes.h"
33 struct subtree_delete_context {
34 enum sd_step {SD_SEARCH, SD_DO_DEL} step;
36 struct ldb_module *module;
37 struct ldb_handle *handle;
38 struct ldb_request *orig_req;
40 struct ldb_request *search_req;
41 struct ldb_request *down_req;
46 static struct subtree_delete_context *subtree_delete_init_handle(struct ldb_request *req,
47 struct ldb_module *module)
49 struct subtree_delete_context *ac;
52 h = talloc_zero(req, struct ldb_handle);
54 ldb_set_errstring(module->ldb, "Out of Memory");
60 ac = talloc_zero(h, struct subtree_delete_context);
62 ldb_set_errstring(module->ldb, "Out of Memory");
78 static int subtree_delete_check_for_children(struct subtree_delete_context *ac)
80 if (ac->num_children > 0) {
81 ldb_asprintf_errstring(ac->module->ldb, "Cannot delete %s, not a leaf node (has %d children)\n",
82 ldb_dn_get_linearized(ac->orig_req->op.del.dn), ac->num_children);
83 return LDB_ERR_NOT_ALLOWED_ON_NON_LEAF;
85 struct ldb_request *req = talloc(ac, struct ldb_request);
87 ldb_oom(ac->module->ldb);
88 return LDB_ERR_OPERATIONS_ERROR;
92 /* Ensure any (io) errors during the search for
93 * children don't propgate back in the error string */
94 ldb_set_errstring(ac->module->ldb, NULL);
98 return ldb_next_request(ac->module, req);
102 static int subtree_delete_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
104 struct subtree_delete_context *ac = talloc_get_type(context, struct subtree_delete_context);
105 TALLOC_CTX *mem_ctx = talloc_new(ac);
108 ldb_oom(ac->module->ldb);
109 return LDB_ERR_OPERATIONS_ERROR;
111 /* OK, we have one of *many* search results here:
113 We should also get the entry we tried to rename. This
114 callback handles this and everything below it.
117 /* Only entries are interesting, and we handle the case of the parent seperatly */
118 if (ares->type == LDB_REPLY_ENTRY
119 && ldb_dn_compare(ares->message->dn, ac->orig_req->op.del.dn) != 0) {
120 /* And it is an actual entry: now object bitterly that we are not a leaf node */
128 static int subtree_delete(struct ldb_module *module, struct ldb_request *req)
130 const char *attrs[] = { NULL };
131 struct ldb_request *new_req;
132 struct subtree_delete_context *ac;
134 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
135 return ldb_next_request(module, req);
138 /* This gets complex: We need to:
139 - Do a search for all entires under this entry
140 - Wait for these results to appear
141 - In the callback for each result, count the children (if any)
142 - return an error if there are any
145 ac = subtree_delete_init_handle(req, module);
147 return LDB_ERR_OPERATIONS_ERROR;
150 ret = ldb_build_search_req(&new_req, module->ldb, req,
157 subtree_delete_search_callback);
159 if (ret != LDB_SUCCESS) {
163 ret = ldb_set_timeout_from_prev_req(module->ldb, req, new_req);
165 if (ret != LDB_SUCCESS) {
169 ac->search_req = new_req;
171 ldb_oom(ac->module->ldb);
172 return LDB_ERR_OPERATIONS_ERROR;
174 return ldb_next_request(module, new_req);
178 static int subtree_delete_wait_none(struct ldb_handle *handle) {
179 struct subtree_delete_context *ac;
180 int ret = LDB_ERR_OPERATIONS_ERROR;
181 if (!handle || !handle->private_data) {
182 return LDB_ERR_OPERATIONS_ERROR;
185 if (handle->state == LDB_ASYNC_DONE) {
186 return handle->status;
189 handle->state = LDB_ASYNC_PENDING;
190 handle->status = LDB_SUCCESS;
192 ac = talloc_get_type(handle->private_data, struct subtree_delete_context);
196 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
198 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
199 handle->status = ret;
202 if (ac->search_req->handle->status != LDB_SUCCESS
203 && ac->search_req->handle->status != LDB_ERR_NO_SUCH_OBJECT) {
204 handle->status = ac->search_req->handle->status;
208 return subtree_delete_check_for_children(ac);
211 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
213 if (ret != LDB_SUCCESS) {
214 handle->status = ret;
217 if (ac->down_req->handle->status != LDB_SUCCESS) {
218 handle->status = ac->down_req->handle->status;
222 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
229 handle->state = LDB_ASYNC_DONE;
233 static int subtree_delete_wait_all(struct ldb_handle *handle) {
237 while (handle->state != LDB_ASYNC_DONE) {
238 ret = subtree_delete_wait_none(handle);
239 if (ret != LDB_SUCCESS) {
244 return handle->status;
247 static int subtree_delete_wait(struct ldb_handle *handle, enum ldb_wait_type type)
249 if (type == LDB_WAIT_ALL) {
250 return subtree_delete_wait_all(handle);
252 return subtree_delete_wait_none(handle);
256 const struct ldb_module_ops ldb_subtree_delete_module_ops = {
257 .name = "subtree_delete",
258 .del = subtree_delete,
259 .wait = subtree_delete_wait,