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 rename module
26 * Description: Rename a subtree in LDB
28 * Author: Andrew Bartlett
31 #include "ldb_includes.h"
33 struct subtree_rename_context {
34 struct ldb_module *module;
35 struct ldb_handle *handle;
36 struct ldb_request *orig_req;
38 struct ldb_request **down_req;
40 int finished_requests;
45 static struct subtree_rename_context *subtree_rename_init_handle(struct ldb_request *req,
46 struct ldb_module *module)
48 struct subtree_rename_context *ac;
51 h = talloc_zero(req, struct ldb_handle);
53 ldb_set_errstring(module->ldb, "Out of Memory");
59 ac = talloc_zero(h, struct subtree_rename_context);
61 ldb_set_errstring(module->ldb, "Out of Memory");
78 static int subtree_rename_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
80 struct ldb_request *req;
81 struct subtree_rename_context *ac = talloc_get_type(context, struct subtree_rename_context);
82 TALLOC_CTX *mem_ctx = talloc_new(ac);
85 ldb_oom(ac->module->ldb);
86 return LDB_ERR_OPERATIONS_ERROR;
88 /* OK, we have one of *many* search results here:
90 We should also get the entry we tried to rename. This
91 callback handles this and everything below it.
94 /* Only entries are interesting, and we handle the case of the parent seperatly */
95 if (ares->type == LDB_REPLY_ENTRY
96 && ldb_dn_compare(ares->message->dn, ac->orig_req->op.rename.olddn) != 0) {
97 /* And it is an actual entry: now create a rename from it */
100 struct ldb_dn *newdn = ldb_dn_copy(mem_ctx, ares->message->dn);
102 ldb_oom(ac->module->ldb);
103 return LDB_ERR_OPERATIONS_ERROR;
106 ldb_dn_remove_base_components(newdn, ldb_dn_get_comp_num(ac->orig_req->op.rename.olddn));
108 if (!ldb_dn_add_base(newdn, ac->orig_req->op.rename.newdn)) {
109 ldb_oom(ac->module->ldb);
110 return LDB_ERR_OPERATIONS_ERROR;
113 ret = ldb_build_rename_req(&req, ldb, mem_ctx,
120 if (ret != LDB_SUCCESS) {
124 ret = ldb_set_timeout_from_prev_req(ldb, ac->orig_req, req);
126 if (ret != LDB_SUCCESS) {
130 talloc_steal(req, newdn);
132 talloc_steal(req, ares->message->dn);
136 } else if (ares->type == LDB_REPLY_DONE) {
137 req = talloc(mem_ctx, struct ldb_request);
138 *req = *ac->orig_req;
146 ac->down_req = talloc_realloc(ac, ac->down_req,
147 struct ldb_request *, ac->num_requests + 1);
149 ldb_oom(ac->module->ldb);
150 return LDB_ERR_OPERATIONS_ERROR;
152 ac->down_req[ac->num_requests] = req;
155 return ldb_next_request(ac->module, req);
160 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
162 const char *attrs[] = { NULL };
163 struct ldb_request *new_req;
164 struct subtree_rename_context *ac;
166 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
167 return ldb_next_request(module, req);
170 /* This gets complex: We need to:
171 - Do a search for all entires under this entry
172 - Wait for these results to appear
173 - In the callback for each result, issue a modify request
174 - That will include this rename, we hope
175 - Wait for each modify result
179 ac = subtree_rename_init_handle(req, module);
181 return LDB_ERR_OPERATIONS_ERROR;
184 ret = ldb_build_search_req(&new_req, module->ldb, req,
185 req->op.rename.olddn,
191 subtree_rename_search_callback);
193 if (ret != LDB_SUCCESS) {
197 ret = ldb_set_timeout_from_prev_req(module->ldb, req, new_req);
199 if (ret != LDB_SUCCESS) {
203 ac->down_req = talloc_realloc(ac, ac->down_req,
204 struct ldb_request *, ac->num_requests + 1);
206 ldb_oom(ac->module->ldb);
207 return LDB_ERR_OPERATIONS_ERROR;
209 ac->down_req[ac->num_requests] = new_req;
211 ldb_oom(ac->module->ldb);
212 return LDB_ERR_OPERATIONS_ERROR;
215 return ldb_next_request(module, new_req);
219 static int subtree_rename_wait_none(struct ldb_handle *handle) {
220 struct subtree_rename_context *ac;
221 int i, ret = LDB_ERR_OPERATIONS_ERROR;
222 if (!handle || !handle->private_data) {
223 return LDB_ERR_OPERATIONS_ERROR;
226 if (handle->state == LDB_ASYNC_DONE) {
227 return handle->status;
230 handle->state = LDB_ASYNC_PENDING;
231 handle->status = LDB_SUCCESS;
233 ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
235 for (i=0; i < ac->num_requests; i++) {
236 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
238 if (ret != LDB_SUCCESS) {
239 handle->status = ret;
242 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
243 handle->status = ac->down_req[i]->handle->status;
247 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
253 handle->state = LDB_ASYNC_DONE;
258 static int subtree_rename_wait_all(struct ldb_handle *handle) {
262 while (handle->state != LDB_ASYNC_DONE) {
263 ret = subtree_rename_wait_none(handle);
264 if (ret != LDB_SUCCESS) {
269 return handle->status;
272 static int subtree_rename_wait(struct ldb_handle *handle, enum ldb_wait_type type)
274 if (type == LDB_WAIT_ALL) {
275 return subtree_rename_wait_all(handle);
277 return subtree_rename_wait_none(handle);
281 const struct ldb_module_ops ldb_subtree_rename_module_ops = {
282 .name = "subtree_rename",
283 .rename = subtree_rename,
284 .wait = subtree_rename_wait,