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>
34 #include "libds/common/flags.h"
35 #include "dsdb/samdb/samdb.h"
37 struct subren_msg_store {
38 struct subren_msg_store *next;
43 struct subtree_rename_context {
44 struct ldb_module *module;
45 struct ldb_request *req;
47 struct subren_msg_store *list;
48 struct subren_msg_store *current;
51 static struct subtree_rename_context *subren_ctx_init(struct ldb_module *module,
52 struct ldb_request *req)
54 struct ldb_context *ldb;
55 struct subtree_rename_context *ac;
57 ldb = ldb_module_get_ctx(module);
59 ac = talloc_zero(req, struct subtree_rename_context);
70 static int subtree_rename_next_request(struct subtree_rename_context *ac);
72 static int subtree_rename_callback(struct ldb_request *req,
73 struct ldb_reply *ares)
75 struct ldb_context *ldb;
76 struct subtree_rename_context *ac;
79 ac = talloc_get_type(req->context, struct subtree_rename_context);
80 ldb = ldb_module_get_ctx(ac->module);
83 return ldb_module_done(ac->req, NULL, NULL,
84 LDB_ERR_OPERATIONS_ERROR);
87 if (ares->type == LDB_REPLY_REFERRAL) {
88 return ldb_module_send_referral(ac->req, ares->referral);
91 if (ares->error != LDB_SUCCESS) {
92 return ldb_module_done(ac->req, ares->controls,
93 ares->response, ares->error);
96 if (ares->type != LDB_REPLY_DONE) {
97 ldb_set_errstring(ldb, "Invalid reply type!\n");
98 return ldb_module_done(ac->req, NULL, NULL,
99 LDB_ERR_OPERATIONS_ERROR);
102 if (ac->current == NULL) {
103 /* this was the last one */
104 return ldb_module_done(ac->req, ares->controls,
105 ares->response, LDB_SUCCESS);
108 ret = subtree_rename_next_request(ac);
109 if (ret != LDB_SUCCESS) {
110 return ldb_module_done(ac->req, NULL, NULL, ret);
117 static int subtree_rename_next_request(struct subtree_rename_context *ac)
119 struct ldb_context *ldb;
120 struct ldb_request *req;
123 ldb = ldb_module_get_ctx(ac->module);
125 if (ac->current == NULL) {
126 return ldb_operr(ldb);
129 ret = ldb_build_rename_req(&req, ldb, ac->current,
133 ac, subtree_rename_callback,
135 LDB_REQ_SET_LOCATION(req);
136 if (ret != LDB_SUCCESS) {
140 ac->current = ac->current->next;
142 return ldb_next_request(ac->module, req);
145 static int check_constraints(struct ldb_message *msg,
146 struct subtree_rename_context *ac,
147 struct ldb_dn *olddn, struct ldb_dn *newdn)
149 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
150 struct ldb_dn *dn1, *dn2;
152 bool move_op = false;
153 bool rename_op = false;
155 /* Skip the checks if old and new DN are the same, or if we have the
156 * relax control specified or if the returned objects is already
157 * deleted and needs only to be moved for consistency. */
159 if (ldb_dn_compare(olddn, newdn) == 0) {
162 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) != NULL) {
165 if (ldb_msg_find_attr_as_bool(msg, "isDeleted", false)) {
169 /* Objects under CN=System */
171 dn1 = ldb_dn_copy(ac, ldb_get_default_basedn(ldb));
172 if (dn1 == NULL) return ldb_oom(ldb);
174 if ( ! ldb_dn_add_child_fmt(dn1, "CN=System")) {
176 return LDB_ERR_OPERATIONS_ERROR;
179 if ((ldb_dn_compare_base(dn1, olddn) == 0) &&
180 (ldb_dn_compare_base(dn1, newdn) != 0)) {
182 ldb_asprintf_errstring(ldb,
183 "subtree_rename: Cannot move/rename %s. Objects under CN=System have to stay under it!",
184 ldb_dn_get_linearized(olddn));
185 return LDB_ERR_OTHER;
192 if ((samdb_find_attribute(ldb, msg, "objectClass", "secret") != NULL) ||
193 (samdb_find_attribute(ldb, msg, "objectClass", "trustedDomain") != NULL)) {
194 ldb_asprintf_errstring(ldb,
195 "subtree_rename: Cannot move/rename %s. It's an LSA-specific object!",
196 ldb_dn_get_linearized(olddn));
197 return LDB_ERR_UNWILLING_TO_PERFORM;
202 dn1 = ldb_dn_get_parent(ac, olddn);
203 if (dn1 == NULL) return ldb_oom(ldb);
204 dn2 = ldb_dn_get_parent(ac, newdn);
205 if (dn2 == NULL) return ldb_oom(ldb);
207 if (ldb_dn_compare(dn1, dn2) == 0) {
216 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
218 /* the config system flags don't apply for the schema partition */
219 if ((ldb_dn_compare_base(ldb_get_config_basedn(ldb), olddn) == 0) &&
220 (ldb_dn_compare_base(ldb_get_schema_basedn(ldb), olddn) != 0)) {
222 (systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_MOVE) == 0) {
223 /* Here we have to do more: control the
224 * "ALLOW_LIMITED_MOVE" flag. This means that the
225 * grand-grand-parents of two objects have to be equal
226 * in order to perform the move (this is used for
227 * moving "server" objects in the "sites" container). */
229 systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE;
232 dn1 = ldb_dn_copy(ac, olddn);
233 if (dn1 == NULL) return ldb_oom(ldb);
234 dn2 = ldb_dn_copy(ac, newdn);
235 if (dn2 == NULL) return ldb_oom(ldb);
237 limited_move &= ldb_dn_remove_child_components(dn1, 3);
238 limited_move &= ldb_dn_remove_child_components(dn2, 3);
239 limited_move &= ldb_dn_compare(dn1, dn2) == 0;
246 ldb_asprintf_errstring(ldb,
247 "subtree_rename: Cannot move %s, it isn't permitted!",
248 ldb_dn_get_linearized(olddn));
249 return LDB_ERR_UNWILLING_TO_PERFORM;
253 (systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_RENAME) == 0) {
254 ldb_asprintf_errstring(ldb,
255 "subtree_rename: Cannot rename %s, it isn't permitted!",
256 ldb_dn_get_linearized(olddn));
257 return LDB_ERR_UNWILLING_TO_PERFORM;
260 if (ldb_dn_compare_base(ldb_get_schema_basedn(ldb), olddn) == 0) {
262 ldb_asprintf_errstring(ldb,
263 "subtree_rename: Cannot move %s, it isn't permitted!",
264 ldb_dn_get_linearized(olddn));
265 return LDB_ERR_UNWILLING_TO_PERFORM;
268 (systemFlags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) != 0) {
269 ldb_asprintf_errstring(ldb,
270 "subtree_rename: Cannot rename %s, it isn't permitted!",
271 ldb_dn_get_linearized(olddn));
272 return LDB_ERR_UNWILLING_TO_PERFORM;
275 if (ldb_dn_compare_base(ldb_get_default_basedn(ldb),
276 ac->current->olddn) == 0) {
278 (systemFlags & SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE) != 0) {
279 ldb_asprintf_errstring(ldb,
280 "subtree_rename: Cannot move %s, it isn't permitted!",
281 ldb_dn_get_linearized(olddn));
282 return LDB_ERR_UNWILLING_TO_PERFORM;
285 (systemFlags & SYSTEM_FLAG_DOMAIN_DISALLOW_RENAME) != 0) {
286 ldb_asprintf_errstring(ldb,
287 "subtree_rename: Cannot rename %s, it isn't permitted!",
288 ldb_dn_get_linearized(olddn));
289 return LDB_ERR_UNWILLING_TO_PERFORM;
296 static int subtree_rename_search_callback(struct ldb_request *req,
297 struct ldb_reply *ares)
299 struct subren_msg_store *store;
300 struct subtree_rename_context *ac;
303 ac = talloc_get_type(req->context, struct subtree_rename_context);
305 if (!ares || !ac->current) {
306 return ldb_module_done(ac->req, NULL, NULL,
307 LDB_ERR_OPERATIONS_ERROR);
309 if (ares->error != LDB_SUCCESS) {
310 return ldb_module_done(ac->req, ares->controls,
311 ares->response, ares->error);
314 switch (ares->type) {
315 case LDB_REPLY_ENTRY:
316 if (ldb_dn_compare(ares->message->dn, ac->list->olddn) == 0) {
317 /* this was already stored by the
318 * subtree_rename_search() */
320 ret = check_constraints(ares->message, ac,
323 if (ret != LDB_SUCCESS) {
324 return ldb_module_done(ac->req, NULL, NULL,
332 store = talloc_zero(ac, struct subren_msg_store);
334 return ldb_module_done(ac->req, NULL, NULL,
335 LDB_ERR_OPERATIONS_ERROR);
337 ac->current->next = store;
340 /* the first list element contains the base for the rename */
341 store->olddn = talloc_steal(store, ares->message->dn);
342 store->newdn = ldb_dn_copy(store, store->olddn);
344 if ( ! ldb_dn_remove_base_components(store->newdn,
345 ldb_dn_get_comp_num(ac->list->olddn))) {
346 return ldb_module_done(ac->req, NULL, NULL,
347 LDB_ERR_OPERATIONS_ERROR);
350 if ( ! ldb_dn_add_base(store->newdn, ac->list->newdn)) {
351 return ldb_module_done(ac->req, NULL, NULL,
352 LDB_ERR_OPERATIONS_ERROR);
355 ret = check_constraints(ares->message, ac,
356 store->olddn, store->newdn);
357 if (ret != LDB_SUCCESS) {
358 return ldb_module_done(ac->req, NULL, NULL, ret);
363 case LDB_REPLY_REFERRAL:
369 /* rewind ac->current */
370 ac->current = ac->list;
372 /* All dns set up, start with the first one */
373 ret = subtree_rename_next_request(ac);
375 if (ret != LDB_SUCCESS) {
376 return ldb_module_done(ac->req, NULL, NULL, ret);
386 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
388 struct ldb_context *ldb;
389 static const char * const attrs[] = { "objectClass", "systemFlags",
391 struct ldb_request *search_req;
392 struct subtree_rename_context *ac;
395 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
396 return ldb_next_request(module, req);
399 ldb = ldb_module_get_ctx(module);
401 /* This gets complex: We need to:
402 - Do a search for all entires under this entry
403 - Wait for these results to appear
404 - In the callback for each result, issue a modify request
405 - That will include this rename, we hope
406 - Wait for each modify result
410 ac = subren_ctx_init(module, req);
415 /* add this entry as the first to do */
416 ac->current = talloc_zero(ac, struct subren_msg_store);
417 if (ac->current == NULL) {
420 ac->current->olddn = req->op.rename.olddn;
421 ac->current->newdn = req->op.rename.newdn;
422 ac->list = ac->current;
424 ret = ldb_build_search_req(&search_req, ldb, ac,
425 req->op.rename.olddn,
431 subtree_rename_search_callback,
433 LDB_REQ_SET_LOCATION(search_req);
434 if (ret != LDB_SUCCESS) {
438 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
440 if (ret != LDB_SUCCESS) {
444 return ldb_next_request(module, search_req);
447 _PUBLIC_ const struct ldb_module_ops ldb_subtree_rename_module_ops = {
448 .name = "subtree_rename",
449 .rename = subtree_rename