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) return ret;
122 talloc_steal(req, newdn);
124 talloc_steal(req, ares->message->dn);
128 } else if (ares->type == LDB_REPLY_DONE) {
129 req = talloc(mem_ctx, struct ldb_request);
130 *req = *ac->orig_req;
138 ac->down_req = talloc_realloc(ac, ac->down_req,
139 struct ldb_request *, ac->num_requests + 1);
141 ldb_oom(ac->module->ldb);
142 return LDB_ERR_OPERATIONS_ERROR;
144 ac->down_req[ac->num_requests] = req;
147 return ldb_next_request(ac->module, req);
152 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
154 const char *attrs[] = { NULL };
155 struct ldb_request *new_req;
156 struct subtree_rename_context *ac;
158 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
159 return ldb_next_request(module, req);
162 /* This gets complex: We need to:
163 - Do a search for all entires under this entry
164 - Wait for these results to appear
165 - In the callback for each result, issue a modify request
166 - That will include this rename, we hope
167 - Wait for each modify result
171 ac = subtree_rename_init_handle(req, module);
173 return LDB_ERR_OPERATIONS_ERROR;
176 ret = ldb_build_search_req(&new_req, module->ldb, req,
177 req->op.rename.olddn,
183 subtree_rename_search_callback);
185 if (ret != LDB_SUCCESS) {
189 ac->down_req = talloc_realloc(ac, ac->down_req,
190 struct ldb_request *, ac->num_requests + 1);
192 ldb_oom(ac->module->ldb);
193 return LDB_ERR_OPERATIONS_ERROR;
195 ac->down_req[ac->num_requests] = new_req;
197 ldb_oom(ac->module->ldb);
198 return LDB_ERR_OPERATIONS_ERROR;
201 return ldb_next_request(module, new_req);
205 static int subtree_rename_wait_none(struct ldb_handle *handle) {
206 struct subtree_rename_context *ac;
207 int i, ret = LDB_ERR_OPERATIONS_ERROR;
208 if (!handle || !handle->private_data) {
209 return LDB_ERR_OPERATIONS_ERROR;
212 if (handle->state == LDB_ASYNC_DONE) {
213 return handle->status;
216 handle->state = LDB_ASYNC_PENDING;
217 handle->status = LDB_SUCCESS;
219 ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
221 for (i=0; i < ac->num_requests; i++) {
222 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
224 if (ret != LDB_SUCCESS) {
225 handle->status = ret;
228 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
229 handle->status = ac->down_req[i]->handle->status;
233 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
239 handle->state = LDB_ASYNC_DONE;
244 static int subtree_rename_wait_all(struct ldb_handle *handle) {
248 while (handle->state != LDB_ASYNC_DONE) {
249 ret = subtree_rename_wait_none(handle);
250 if (ret != LDB_SUCCESS) {
255 return handle->status;
258 static int subtree_rename_wait(struct ldb_handle *handle, enum ldb_wait_type type)
260 if (type == LDB_WAIT_ALL) {
261 return subtree_rename_wait_all(handle);
263 return subtree_rename_wait_none(handle);
267 const struct ldb_module_ops ldb_subtree_rename_module_ops = {
268 .name = "subtree_rename",
269 .rename = subtree_rename,
270 .wait = subtree_rename_wait,