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
33 #include <ldb_module.h>
35 struct subren_msg_store {
36 struct subren_msg_store *next;
41 struct subtree_rename_context {
42 struct ldb_module *module;
43 struct ldb_request *req;
45 struct subren_msg_store *list;
46 struct subren_msg_store *current;
49 static struct subtree_rename_context *subren_ctx_init(struct ldb_module *module,
50 struct ldb_request *req)
52 struct ldb_context *ldb;
53 struct subtree_rename_context *ac;
55 ldb = ldb_module_get_ctx(module);
57 ac = talloc_zero(req, struct subtree_rename_context);
69 static int subtree_rename_next_request(struct subtree_rename_context *ac);
71 static int subtree_rename_callback(struct ldb_request *req,
72 struct ldb_reply *ares)
74 struct ldb_context *ldb;
75 struct subtree_rename_context *ac;
78 ac = talloc_get_type(req->context, struct subtree_rename_context);
79 ldb = ldb_module_get_ctx(ac->module);
82 return ldb_module_done(ac->req, NULL, NULL,
83 LDB_ERR_OPERATIONS_ERROR);
86 if (ares->type == LDB_REPLY_REFERRAL) {
87 return ldb_module_send_referral(ac->req, ares->referral);
90 if (ares->error != LDB_SUCCESS) {
91 return ldb_module_done(ac->req, ares->controls,
92 ares->response, ares->error);
95 if (ares->type != LDB_REPLY_DONE) {
96 ldb_set_errstring(ldb, "Invalid reply type!\n");
97 return ldb_module_done(ac->req, NULL, NULL,
98 LDB_ERR_OPERATIONS_ERROR);
101 if (ac->current == NULL) {
102 /* this was the last one */
103 return ldb_module_done(ac->req, ares->controls,
104 ares->response, LDB_SUCCESS);
107 ret = subtree_rename_next_request(ac);
108 if (ret != LDB_SUCCESS) {
109 return ldb_module_done(ac->req, NULL, NULL, ret);
116 static int subtree_rename_next_request(struct subtree_rename_context *ac)
118 struct ldb_context *ldb;
119 struct ldb_request *req;
122 ldb = ldb_module_get_ctx(ac->module);
124 if (ac->current == NULL) {
125 return ldb_operr(ldb);
128 ret = ldb_build_rename_req(&req, ldb, ac->current,
132 ac, subtree_rename_callback,
134 if (ret != LDB_SUCCESS) {
138 ac->current = ac->current->next;
140 return ldb_next_request(ac->module, req);
143 static int subtree_rename_search_callback(struct ldb_request *req,
144 struct ldb_reply *ares)
146 struct subren_msg_store *store;
147 struct subtree_rename_context *ac;
150 ac = talloc_get_type(req->context, struct subtree_rename_context);
152 if (!ares || !ac->current) {
153 return ldb_module_done(ac->req, NULL, NULL,
154 LDB_ERR_OPERATIONS_ERROR);
156 if (ares->error != LDB_SUCCESS) {
157 return ldb_module_done(ac->req, ares->controls,
158 ares->response, ares->error);
161 switch (ares->type) {
162 case LDB_REPLY_ENTRY:
164 if (ldb_dn_compare(ares->message->dn, ac->list->olddn) == 0) {
165 /* this was already stored by the
166 * subtree_rename_search() */
171 store = talloc_zero(ac, struct subren_msg_store);
173 return ldb_module_done(ac->req, NULL, NULL,
174 LDB_ERR_OPERATIONS_ERROR);
176 ac->current->next = store;
179 /* the first list element contains the base for the rename */
180 store->olddn = talloc_steal(store, ares->message->dn);
181 store->newdn = ldb_dn_copy(store, store->olddn);
183 if ( ! ldb_dn_remove_base_components(store->newdn,
184 ldb_dn_get_comp_num(ac->list->olddn))) {
185 return ldb_module_done(ac->req, NULL, NULL,
186 LDB_ERR_OPERATIONS_ERROR);
189 if ( ! ldb_dn_add_base(store->newdn, ac->list->newdn)) {
190 return ldb_module_done(ac->req, NULL, NULL,
191 LDB_ERR_OPERATIONS_ERROR);
196 case LDB_REPLY_REFERRAL:
202 /* rewind ac->current */
203 ac->current = ac->list;
205 /* All dns set up, start with the first one */
206 ret = subtree_rename_next_request(ac);
208 if (ret != LDB_SUCCESS) {
209 return ldb_module_done(ac->req, NULL, NULL, ret);
219 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
221 struct ldb_context *ldb;
222 static const char *attrs[2] = { "distinguishedName", NULL };
223 struct ldb_request *search_req;
224 struct subtree_rename_context *ac;
227 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
228 return ldb_next_request(module, req);
231 ldb = ldb_module_get_ctx(module);
233 /* This gets complex: We need to:
234 - Do a search for all entires under this entry
235 - Wait for these results to appear
236 - In the callback for each result, issue a modify request
237 - That will include this rename, we hope
238 - Wait for each modify result
242 ac = subren_ctx_init(module, req);
244 return ldb_operr(ldb);
247 /* add this entry as the first to do */
248 ac->current = talloc_zero(ac, struct subren_msg_store);
249 if (ac->current == NULL) {
252 ac->current->olddn = req->op.rename.olddn;
253 ac->current->newdn = req->op.rename.newdn;
254 ac->list = ac->current;
256 ret = ldb_build_search_req(&search_req, ldb, ac,
257 req->op.rename.olddn,
263 subtree_rename_search_callback,
265 if (ret != LDB_SUCCESS) {
269 return ldb_next_request(module, search_req);
272 _PUBLIC_ const struct ldb_module_ops ldb_subtree_rename_module_ops = {
273 .name = "subtree_rename",
274 .rename = subtree_rename