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;
47 static struct subtree_rename_context *subtree_rename_init_handle(struct ldb_request *req,
48 struct ldb_module *module)
50 struct subtree_rename_context *ac;
53 h = talloc_zero(req, struct ldb_handle);
55 ldb_set_errstring(module->ldb, "Out of Memory");
61 ac = talloc_zero(h, struct subtree_rename_context);
63 ldb_set_errstring(module->ldb, "Out of Memory");
80 static int subtree_rename_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
82 struct ldb_request *req;
83 struct subtree_rename_context *ac = talloc_get_type(context, struct subtree_rename_context);
84 TALLOC_CTX *mem_ctx = talloc_new(ac);
87 ldb_oom(ac->module->ldb);
88 return LDB_ERR_OPERATIONS_ERROR;
90 /* OK, we have one of *many* search results here:
92 We should also get the entry we tried to rename. This
93 callback handles this and everything below it.
96 /* Only entries are interesting, and we handle the case of the parent seperatly */
97 if (ares->type == LDB_REPLY_ENTRY
98 && ldb_dn_compare(ares->message->dn, ac->orig_req->op.rename.olddn) != 0) {
99 /* And it is an actual entry: now create a rename from it */
102 struct ldb_dn *newdn = ldb_dn_copy(mem_ctx, ares->message->dn);
104 ldb_oom(ac->module->ldb);
105 return LDB_ERR_OPERATIONS_ERROR;
108 ldb_dn_remove_base_components(newdn, ldb_dn_get_comp_num(ac->orig_req->op.rename.olddn));
110 if (!ldb_dn_add_base(newdn, ac->orig_req->op.rename.newdn)) {
111 ldb_oom(ac->module->ldb);
112 return LDB_ERR_OPERATIONS_ERROR;
115 ret = ldb_build_rename_req(&req, ldb, mem_ctx,
122 if (ret != LDB_SUCCESS) return ret;
124 talloc_steal(req, newdn);
126 talloc_steal(req, ares->message->dn);
130 } else if (ares->type == LDB_REPLY_DONE) {
131 req = talloc(mem_ctx, struct ldb_request);
132 *req = *ac->orig_req;
140 ac->down_req = talloc_realloc(ac, ac->down_req,
141 struct ldb_request *, ac->num_requests + 1);
143 ldb_oom(ac->module->ldb);
144 return LDB_ERR_OPERATIONS_ERROR;
146 ac->down_req[ac->num_requests] = req;
149 return ldb_next_request(ac->module, req);
154 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
156 const char *attrs[] = { NULL };
157 struct ldb_request *new_req;
158 struct subtree_rename_context *ac;
160 struct ldb_search_options_control *search_options;
161 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
162 return ldb_next_request(module, req);
165 /* This gets complex: We need to:
166 - Do a search for all entires under this entry
167 - Wait for these results to appear
168 - In the callback for each result, issue a modify request
169 - That will include this rename, we hope
170 - Wait for each modify result
174 ac = subtree_rename_init_handle(req, module);
176 return LDB_ERR_OPERATIONS_ERROR;
179 ret = ldb_build_search_req(&new_req, module->ldb, req,
180 req->op.rename.olddn,
186 subtree_rename_search_callback);
188 if (ret != LDB_SUCCESS) {
192 /* We want to find any partitions under this entry. That way,
193 * if we try and rename a whole partition, the partitions
194 * module should cause us to fail the lot */
195 search_options = talloc(ac, struct ldb_search_options_control);
196 if (!search_options) {
197 ldb_oom(ac->module->ldb);
198 return LDB_ERR_OPERATIONS_ERROR;
200 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
202 ret = ldb_request_add_control(new_req, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
203 if (ret != LDB_SUCCESS) {
207 ac->down_req = talloc_realloc(ac, ac->down_req,
208 struct ldb_request *, ac->num_requests + 1);
210 ldb_oom(ac->module->ldb);
211 return LDB_ERR_OPERATIONS_ERROR;
213 ac->down_req[ac->num_requests] = new_req;
215 ldb_oom(ac->module->ldb);
216 return LDB_ERR_OPERATIONS_ERROR;
219 return ldb_next_request(module, new_req);
222 static int subtree_rename_wait_none(struct ldb_handle *handle) {
223 struct subtree_rename_context *ac;
225 if (!handle || !handle->private_data) {
226 return LDB_ERR_OPERATIONS_ERROR;
229 if (handle->state == LDB_ASYNC_DONE) {
230 return handle->status;
233 handle->state = LDB_ASYNC_PENDING;
234 handle->status = LDB_SUCCESS;
236 ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
238 for (i=0; i < ac->num_requests; i++) {
239 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
241 if (ret != LDB_SUCCESS) {
242 handle->status = ret;
245 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
246 handle->status = ac->down_req[i]->handle->status;
250 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
256 handle->state = LDB_ASYNC_DONE;
261 static int subtree_rename_wait_all(struct ldb_handle *handle) {
265 while (handle->state != LDB_ASYNC_DONE) {
266 ret = subtree_rename_wait_none(handle);
267 if (ret != LDB_SUCCESS) {
272 return handle->status;
275 static int subtree_rename_wait(struct ldb_handle *handle, enum ldb_wait_type type)
277 if (type == LDB_WAIT_ALL) {
278 return subtree_rename_wait_all(handle);
280 return subtree_rename_wait_none(handle);
284 static const struct ldb_module_ops subtree_rename_ops = {
285 .name = "subtree_rename",
286 .rename = subtree_rename,
287 .wait = subtree_rename_wait,
290 int ldb_subtree_rename_init(void)
292 return ldb_register_module(&subtree_rename_ops);