4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2007
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 * Component: ldb subtree rename module
30 * Description: Rename a subtree in LDB
32 * Author: Andrew Bartlett
35 #include "ldb_includes.h"
37 struct subtree_rename_context {
38 struct ldb_module *module;
39 struct ldb_handle *handle;
40 struct ldb_request *orig_req;
42 struct ldb_request **down_req;
44 int finished_requests;
49 static struct subtree_rename_context *subtree_rename_init_handle(struct ldb_request *req,
50 struct ldb_module *module)
52 struct subtree_rename_context *ac;
55 h = talloc_zero(req, struct ldb_handle);
57 ldb_set_errstring(module->ldb, "Out of Memory");
63 ac = talloc_zero(h, struct subtree_rename_context);
65 ldb_set_errstring(module->ldb, "Out of Memory");
82 static int subtree_rename_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
84 struct ldb_request *req;
85 struct subtree_rename_context *ac = talloc_get_type(context, struct subtree_rename_context);
86 TALLOC_CTX *mem_ctx = talloc_new(ac);
89 ldb_oom(ac->module->ldb);
90 return LDB_ERR_OPERATIONS_ERROR;
92 /* OK, we have one of *many* search results here:
94 We should also get the entry we tried to rename. This
95 callback handles this and everything below it.
98 /* Only entries are interesting, and we handle the case of the parent seperatly */
99 if (ares->type == LDB_REPLY_ENTRY
100 && ldb_dn_compare(ares->message->dn, ac->orig_req->op.rename.olddn) != 0) {
101 /* And it is an actual entry: now create a rename from it */
104 struct ldb_dn *newdn = ldb_dn_copy(mem_ctx, ares->message->dn);
106 ldb_oom(ac->module->ldb);
107 return LDB_ERR_OPERATIONS_ERROR;
110 ldb_dn_remove_base_components(newdn, ldb_dn_get_comp_num(ac->orig_req->op.rename.olddn));
112 if (!ldb_dn_add_base(newdn, ac->orig_req->op.rename.newdn)) {
113 ldb_oom(ac->module->ldb);
114 return LDB_ERR_OPERATIONS_ERROR;
117 ret = ldb_build_rename_req(&req, ldb, mem_ctx,
124 if (ret != LDB_SUCCESS) return ret;
126 talloc_steal(req, newdn);
128 talloc_steal(req, ares->message->dn);
132 } else if (ares->type == LDB_REPLY_DONE) {
133 req = talloc(mem_ctx, struct ldb_request);
134 *req = *ac->orig_req;
142 ac->down_req = talloc_realloc(ac, ac->down_req,
143 struct ldb_request *, ac->num_requests + 1);
145 ldb_oom(ac->module->ldb);
146 return LDB_ERR_OPERATIONS_ERROR;
148 ac->down_req[ac->num_requests] = req;
151 return ldb_next_request(ac->module, req);
156 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
158 const char *attrs[] = { NULL };
159 struct ldb_request *new_req;
160 struct subtree_rename_context *ac;
162 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
163 return ldb_next_request(module, req);
166 /* Firstly ensure we are not trying to rename it to be a child of itself */
167 if ((ldb_dn_compare_base(req->op.rename.olddn, req->op.rename.newdn) == 0)
168 && (ldb_dn_compare(req->op.rename.olddn, req->op.rename.newdn) != 0)) {
169 ldb_asprintf_errstring(module->ldb, "Cannot rename %s to be a child of itself",
170 ldb_dn_get_linearized(req->op.rename.olddn));
171 return LDB_ERR_UNWILLING_TO_PERFORM;
174 /* This gets complex: We need to:
175 - Do a search for all entires under this entry
176 - Wait for these results to appear
177 - In the callback for each result, issue a modify request
178 - That will include this rename, we hope
179 - Wait for each modify result
183 ac = subtree_rename_init_handle(req, module);
185 return LDB_ERR_OPERATIONS_ERROR;
188 ret = ldb_build_search_req(&new_req, module->ldb, req,
189 req->op.rename.olddn,
195 subtree_rename_search_callback);
197 if (ret != LDB_SUCCESS) {
201 ac->down_req = talloc_realloc(ac, ac->down_req,
202 struct ldb_request *, ac->num_requests + 1);
204 ldb_oom(ac->module->ldb);
205 return LDB_ERR_OPERATIONS_ERROR;
207 ac->down_req[ac->num_requests] = new_req;
209 ldb_oom(ac->module->ldb);
210 return LDB_ERR_OPERATIONS_ERROR;
213 return ldb_next_request(module, new_req);
217 static int subtree_delete_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
219 struct ldb_request *req;
220 struct subtree_rename_context *ac = talloc_get_type(context, struct subtree_rename_context);
221 TALLOC_CTX *mem_ctx = talloc_new(ac);
224 ldb_oom(ac->module->ldb);
225 return LDB_ERR_OPERATIONS_ERROR;
227 /* OK, we have one of *many* search results here:
229 We should also get the entry we tried to rename. This
230 callback handles this and everything below it.
233 /* Only entries are interesting, and we handle the case of the parent seperatly */
234 if (ares->type == LDB_REPLY_ENTRY
235 && ldb_dn_compare(ares->message->dn, ac->orig_req->op.del.dn) != 0) {
236 /* And it is an actual entry: now object bitterly that we are not a leaf node */
240 } else if (ares->type == LDB_REPLY_DONE) {
242 if (ac->num_children > 0) {
243 ldb_asprintf_errstring(ac->module->ldb, "Cannot delete %s, not a leaf node (has %d children)\n",
244 ldb_dn_get_linearized(ac->orig_req->op.del.dn), ac->num_children);
245 return LDB_ERR_NOT_ALLOWED_ON_NON_LEAF;
247 req = talloc(mem_ctx, struct ldb_request);
249 ldb_oom(ac->module->ldb);
250 return LDB_ERR_OPERATIONS_ERROR;
252 *req = *ac->orig_req;
254 ac->down_req = talloc_realloc(ac, ac->down_req,
255 struct ldb_request *, ac->num_requests + 1);
257 ldb_oom(ac->module->ldb);
258 return LDB_ERR_OPERATIONS_ERROR;
260 ac->down_req[ac->num_requests] = req;
263 return ldb_next_request(ac->module, req);
272 static int subtree_delete(struct ldb_module *module, struct ldb_request *req)
274 const char *attrs[] = { NULL };
275 struct ldb_request *new_req;
276 struct subtree_rename_context *ac;
278 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
279 return ldb_next_request(module, req);
282 /* This gets complex: We need to:
283 - Do a search for all entires under this entry
284 - Wait for these results to appear
285 - In the callback for each result, issue a modify request
286 - That will include this rename, we hope
287 - Wait for each modify result
291 ac = subtree_rename_init_handle(req, module);
293 return LDB_ERR_OPERATIONS_ERROR;
296 ret = ldb_build_search_req(&new_req, module->ldb, req,
303 subtree_delete_search_callback);
305 if (ret != LDB_SUCCESS) {
309 ac->down_req = talloc_realloc(ac, ac->down_req,
310 struct ldb_request *, ac->num_requests + 1);
312 ldb_oom(ac->module->ldb);
313 return LDB_ERR_OPERATIONS_ERROR;
315 ac->down_req[ac->num_requests] = new_req;
317 ldb_oom(ac->module->ldb);
318 return LDB_ERR_OPERATIONS_ERROR;
321 return ldb_next_request(module, new_req);
324 static int subtree_rename_wait_none(struct ldb_handle *handle) {
325 struct subtree_rename_context *ac;
326 int i, ret = LDB_ERR_OPERATIONS_ERROR;
327 if (!handle || !handle->private_data) {
328 return LDB_ERR_OPERATIONS_ERROR;
331 if (handle->state == LDB_ASYNC_DONE) {
332 return handle->status;
335 handle->state = LDB_ASYNC_PENDING;
336 handle->status = LDB_SUCCESS;
338 ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
340 for (i=0; i < ac->num_requests; i++) {
341 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
343 if (ret != LDB_SUCCESS) {
344 handle->status = ret;
347 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
348 handle->status = ac->down_req[i]->handle->status;
352 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
358 handle->state = LDB_ASYNC_DONE;
363 static int subtree_rename_wait_all(struct ldb_handle *handle) {
367 while (handle->state != LDB_ASYNC_DONE) {
368 ret = subtree_rename_wait_none(handle);
369 if (ret != LDB_SUCCESS) {
374 return handle->status;
377 static int subtree_rename_wait(struct ldb_handle *handle, enum ldb_wait_type type)
379 if (type == LDB_WAIT_ALL) {
380 return subtree_rename_wait_all(handle);
382 return subtree_rename_wait_none(handle);
386 static const struct ldb_module_ops subtree_rename_ops = {
387 .name = "subtree_rename",
388 .rename = subtree_rename,
389 .del = subtree_delete,
390 .wait = subtree_rename_wait,
393 int ldb_subtree_rename_init(void)
395 return ldb_register_module(&subtree_rename_ops);