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 if (ret != LDB_SUCCESS) {
139 ac->current = ac->current->next;
141 return ldb_next_request(ac->module, req);
144 static int check_constraints(struct ldb_message *msg,
145 struct subtree_rename_context *ac,
146 struct ldb_dn *olddn, struct ldb_dn *newdn)
148 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
149 struct ldb_dn *dn1, *dn2;
151 bool move_op = false;
152 bool rename_op = false;
154 /* Skip the checks if old and new DN are the same or if we relax */
156 if (ldb_dn_compare(olddn, newdn) == 0) {
159 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) != NULL) {
163 /* Objects under CN=System */
165 dn1 = ldb_dn_copy(ac, ldb_get_default_basedn(ldb));
166 if (dn1 == NULL) return ldb_oom(ldb);
168 if ( ! ldb_dn_add_child_fmt(dn1, "CN=System")) {
170 return LDB_ERR_OPERATIONS_ERROR;
173 if ((ldb_dn_compare_base(dn1, olddn) == 0) &&
174 (ldb_dn_compare_base(dn1, newdn) != 0)) {
176 ldb_asprintf_errstring(ldb,
177 "subtree_rename: Cannot move/rename %s. Objects under CN=System have to stay under it!",
178 ldb_dn_get_linearized(olddn));
179 return LDB_ERR_OTHER;
186 if ((samdb_find_attribute(ldb, msg, "objectClass", "secret") != NULL) ||
187 (samdb_find_attribute(ldb, msg, "objectClass", "trustedDomain") != NULL)) {
188 ldb_asprintf_errstring(ldb,
189 "subtree_rename: Cannot move/rename %s. It's an LSA-specific object!",
190 ldb_dn_get_linearized(olddn));
191 return LDB_ERR_UNWILLING_TO_PERFORM;
196 dn1 = ldb_dn_get_parent(ac, olddn);
197 if (dn1 == NULL) return ldb_oom(ldb);
198 dn2 = ldb_dn_get_parent(ac, newdn);
199 if (dn2 == NULL) return ldb_oom(ldb);
201 if (ldb_dn_compare(dn1, dn2) == 0) {
210 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
212 /* the config system flags don't apply for the schema partition */
213 if ((ldb_dn_compare_base(ldb_get_config_basedn(ldb), olddn) == 0) &&
214 (ldb_dn_compare_base(ldb_get_schema_basedn(ldb), olddn) != 0)) {
216 (systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_MOVE) == 0) {
217 /* Here we have to do more: control the
218 * "ALLOW_LIMITED_MOVE" flag. This means that the
219 * grand-grand-parents of two objects have to be equal
220 * in order to perform the move (this is used for
221 * moving "server" objects in the "sites" container). */
223 systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE;
226 dn1 = ldb_dn_copy(ac, olddn);
227 if (dn1 == NULL) return ldb_oom(ldb);
228 dn2 = ldb_dn_copy(ac, newdn);
229 if (dn2 == NULL) return ldb_oom(ldb);
231 limited_move &= ldb_dn_remove_child_components(dn1, 3);
232 limited_move &= ldb_dn_remove_child_components(dn2, 3);
233 limited_move &= ldb_dn_compare(dn1, dn2) == 0;
240 ldb_asprintf_errstring(ldb,
241 "subtree_rename: Cannot move %s, it isn't permitted!",
242 ldb_dn_get_linearized(olddn));
243 return LDB_ERR_UNWILLING_TO_PERFORM;
247 (systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_RENAME) == 0) {
248 ldb_asprintf_errstring(ldb,
249 "subtree_rename: Cannot rename %s, it isn't permitted!",
250 ldb_dn_get_linearized(olddn));
251 return LDB_ERR_UNWILLING_TO_PERFORM;
254 if (ldb_dn_compare_base(ldb_get_schema_basedn(ldb), olddn) == 0) {
256 ldb_asprintf_errstring(ldb,
257 "subtree_rename: Cannot move %s, it isn't permitted!",
258 ldb_dn_get_linearized(olddn));
259 return LDB_ERR_UNWILLING_TO_PERFORM;
262 (systemFlags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) != 0) {
263 ldb_asprintf_errstring(ldb,
264 "subtree_rename: Cannot rename %s, it isn't permitted!",
265 ldb_dn_get_linearized(olddn));
266 return LDB_ERR_UNWILLING_TO_PERFORM;
269 if (ldb_dn_compare_base(ldb_get_default_basedn(ldb),
270 ac->current->olddn) == 0) {
272 (systemFlags & SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE) != 0) {
273 ldb_asprintf_errstring(ldb,
274 "subtree_rename: Cannot move %s, it isn't permitted!",
275 ldb_dn_get_linearized(olddn));
276 return LDB_ERR_UNWILLING_TO_PERFORM;
279 (systemFlags & SYSTEM_FLAG_DOMAIN_DISALLOW_RENAME) != 0) {
280 ldb_asprintf_errstring(ldb,
281 "subtree_rename: Cannot rename %s, it isn't permitted!",
282 ldb_dn_get_linearized(olddn));
283 return LDB_ERR_UNWILLING_TO_PERFORM;
290 static int subtree_rename_search_callback(struct ldb_request *req,
291 struct ldb_reply *ares)
293 struct subren_msg_store *store;
294 struct subtree_rename_context *ac;
297 ac = talloc_get_type(req->context, struct subtree_rename_context);
299 if (!ares || !ac->current) {
300 return ldb_module_done(ac->req, NULL, NULL,
301 LDB_ERR_OPERATIONS_ERROR);
303 if (ares->error != LDB_SUCCESS) {
304 return ldb_module_done(ac->req, ares->controls,
305 ares->response, ares->error);
308 switch (ares->type) {
309 case LDB_REPLY_ENTRY:
310 if (ldb_dn_compare(ares->message->dn, ac->list->olddn) == 0) {
311 /* this was already stored by the
312 * subtree_rename_search() */
314 ret = check_constraints(ares->message, ac,
317 if (ret != LDB_SUCCESS) {
318 return ldb_module_done(ac->req, NULL, NULL,
326 store = talloc_zero(ac, struct subren_msg_store);
328 return ldb_module_done(ac->req, NULL, NULL,
329 LDB_ERR_OPERATIONS_ERROR);
331 ac->current->next = store;
334 /* the first list element contains the base for the rename */
335 store->olddn = talloc_steal(store, ares->message->dn);
336 store->newdn = ldb_dn_copy(store, store->olddn);
338 if ( ! ldb_dn_remove_base_components(store->newdn,
339 ldb_dn_get_comp_num(ac->list->olddn))) {
340 return ldb_module_done(ac->req, NULL, NULL,
341 LDB_ERR_OPERATIONS_ERROR);
344 if ( ! ldb_dn_add_base(store->newdn, ac->list->newdn)) {
345 return ldb_module_done(ac->req, NULL, NULL,
346 LDB_ERR_OPERATIONS_ERROR);
349 ret = check_constraints(ares->message, ac,
350 store->olddn, store->newdn);
351 if (ret != LDB_SUCCESS) {
352 return ldb_module_done(ac->req, NULL, NULL, ret);
357 case LDB_REPLY_REFERRAL:
363 /* rewind ac->current */
364 ac->current = ac->list;
366 /* All dns set up, start with the first one */
367 ret = subtree_rename_next_request(ac);
369 if (ret != LDB_SUCCESS) {
370 return ldb_module_done(ac->req, NULL, NULL, ret);
380 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
382 struct ldb_context *ldb;
383 static const char * const attrs[] = { "objectClass", "systemFlags",
385 struct ldb_request *search_req;
386 struct subtree_rename_context *ac;
389 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
390 return ldb_next_request(module, req);
393 ldb = ldb_module_get_ctx(module);
395 /* This gets complex: We need to:
396 - Do a search for all entires under this entry
397 - Wait for these results to appear
398 - In the callback for each result, issue a modify request
399 - That will include this rename, we hope
400 - Wait for each modify result
404 ac = subren_ctx_init(module, req);
409 /* add this entry as the first to do */
410 ac->current = talloc_zero(ac, struct subren_msg_store);
411 if (ac->current == NULL) {
414 ac->current->olddn = req->op.rename.olddn;
415 ac->current->newdn = req->op.rename.newdn;
416 ac->list = ac->current;
418 ret = ldb_build_search_req(&search_req, ldb, ac,
419 req->op.rename.olddn,
425 subtree_rename_search_callback,
427 if (ret != LDB_SUCCESS) {
431 return ldb_next_request(module, search_req);
434 _PUBLIC_ const struct ldb_module_ops ldb_subtree_rename_module_ops = {
435 .name = "subtree_rename",
436 .rename = subtree_rename