4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2007
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6 Copyright (C) Matthias Dieter Wallnöfer <mdw@samba.org> 2010-2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb subtree rename module
27 * Description: Rename a subtree in LDB
29 * Author: Andrew Bartlett
34 #include <ldb_module.h>
35 #include "libds/common/flags.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "dsdb/samdb/ldb_modules/util.h"
39 struct subtree_rename_context {
40 struct ldb_module *module;
41 struct ldb_request *req;
45 static struct subtree_rename_context *subren_ctx_init(struct ldb_module *module,
46 struct ldb_request *req)
48 struct subtree_rename_context *ac;
51 ac = talloc_zero(req, struct subtree_rename_context);
58 ac->base_renamed = false;
63 static int subtree_rename_search_onelevel_callback(struct ldb_request *req,
64 struct ldb_reply *ares)
66 struct subtree_rename_context *ac;
69 ac = talloc_get_type(req->context, struct subtree_rename_context);
72 return ldb_module_done(ac->req, NULL, NULL,
73 LDB_ERR_OPERATIONS_ERROR);
75 if (ares->error != LDB_SUCCESS) {
76 return ldb_module_done(ac->req, ares->controls,
77 ares->response, ares->error);
80 if (ac->base_renamed == false) {
81 ac->base_renamed = true;
83 ret = dsdb_module_rename(ac->module,
84 ac->req->op.rename.olddn,
85 ac->req->op.rename.newdn,
86 DSDB_FLAG_NEXT_MODULE, req);
87 if (ret != LDB_SUCCESS) {
88 return ldb_module_done(ac->req, NULL, NULL, ret);
95 struct ldb_dn *old_dn = NULL;
96 struct ldb_dn *new_dn = NULL;
98 old_dn = ares->message->dn;
99 new_dn = ldb_dn_copy(ares, old_dn);
101 return ldb_module_oom(ac->module);
104 if ( ! ldb_dn_remove_base_components(new_dn,
105 ldb_dn_get_comp_num(ac->req->op.rename.olddn))) {
106 return ldb_module_done(ac->req, NULL, NULL,
107 LDB_ERR_OPERATIONS_ERROR);
110 if ( ! ldb_dn_add_base(new_dn, ac->req->op.rename.newdn)) {
111 return ldb_module_done(ac->req, NULL, NULL,
112 LDB_ERR_OPERATIONS_ERROR);
114 ret = dsdb_module_rename(ac->module, old_dn, new_dn, DSDB_FLAG_OWN_MODULE, req);
115 if (ret != LDB_SUCCESS) {
116 return ldb_module_done(ac->req, NULL, NULL, ret);
123 case LDB_REPLY_REFERRAL:
128 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
131 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
133 ldb_asprintf_errstring(ldb, "Invalid LDB reply type %d", ares->type);
134 return ldb_module_done(ac->req, NULL, NULL,
135 LDB_ERR_OPERATIONS_ERROR);
143 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
145 struct ldb_context *ldb;
146 static const char * const no_attrs[] = {NULL};
147 struct ldb_request *search_req;
148 struct subtree_rename_context *ac;
151 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
152 return ldb_next_request(module, req);
155 ldb = ldb_module_get_ctx(module);
158 * This gets complex: We need to:
159 * - Do a search for all entries under this entry
160 * - Wait for these results to appear
161 * - Do our own rename (in first callback)
162 * - In the callback for each result, issue a dsdb_module_rename()
165 ac = subren_ctx_init(module, req);
170 ret = ldb_build_search_req(&search_req, ldb_module_get_ctx(ac->module), ac,
171 ac->req->op.rename.olddn,
177 subtree_rename_search_onelevel_callback,
179 LDB_REQ_SET_LOCATION(search_req);
180 if (ret != LDB_SUCCESS) {
184 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
186 if (ret != LDB_SUCCESS) {
190 return ldb_next_request(ac->module, search_req);
193 static const struct ldb_module_ops ldb_subtree_rename_module_ops = {
194 .name = "subtree_rename",
195 .rename = subtree_rename
198 int ldb_subtree_rename_module_init(const char *version)
200 LDB_MODULE_CHECK_VERSION(version);
201 return ldb_register_module(&ldb_subtree_rename_module_ops);