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;
44 static struct subtree_rename_context *subren_ctx_init(struct ldb_module *module,
45 struct ldb_request *req)
47 struct subtree_rename_context *ac;
50 ac = talloc_zero(req, struct subtree_rename_context);
61 static int subtree_rename_callback(struct ldb_request *req,
62 struct ldb_reply *ares)
64 struct ldb_context *ldb;
65 struct subtree_rename_context *ac;
67 ac = talloc_get_type(req->context, struct subtree_rename_context);
68 ldb = ldb_module_get_ctx(ac->module);
71 return ldb_module_done(ac->req, NULL, NULL,
72 LDB_ERR_OPERATIONS_ERROR);
75 if (ares->type == LDB_REPLY_REFERRAL) {
76 return ldb_module_send_referral(ac->req, ares->referral);
79 if (ares->error != LDB_SUCCESS) {
80 return ldb_module_done(ac->req, ares->controls,
81 ares->response, ares->error);
84 if (ares->type != LDB_REPLY_DONE) {
85 ldb_asprintf_errstring(ldb, "Invalid LDB reply type %d", ares->type);
86 return ldb_module_done(ac->req, NULL, NULL,
87 LDB_ERR_OPERATIONS_ERROR);
91 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
94 static int subtree_rename_search_onelevel_callback(struct ldb_request *req,
95 struct ldb_reply *ares)
97 struct subtree_rename_context *ac;
98 struct ldb_request *rename_req;
101 ac = talloc_get_type(req->context, struct subtree_rename_context);
104 return ldb_module_done(ac->req, NULL, NULL,
105 LDB_ERR_OPERATIONS_ERROR);
107 if (ares->error != LDB_SUCCESS) {
108 return ldb_module_done(ac->req, ares->controls,
109 ares->response, ares->error);
112 switch (ares->type) {
113 case LDB_REPLY_ENTRY:
115 struct ldb_dn *old_dn = ares->message->dn;
116 struct ldb_dn *new_dn = ldb_dn_copy(ares, old_dn);
118 return ldb_module_oom(ac->module);
121 if ( ! ldb_dn_remove_base_components(new_dn,
122 ldb_dn_get_comp_num(ac->req->op.rename.olddn))) {
123 return ldb_module_done(ac->req, NULL, NULL,
124 LDB_ERR_OPERATIONS_ERROR);
127 if ( ! ldb_dn_add_base(new_dn, ac->req->op.rename.newdn)) {
128 return ldb_module_done(ac->req, NULL, NULL,
129 LDB_ERR_OPERATIONS_ERROR);
131 ret = dsdb_module_rename(ac->module, old_dn, new_dn, DSDB_FLAG_OWN_MODULE, req);
132 if (ret != LDB_SUCCESS) {
140 case LDB_REPLY_REFERRAL:
146 ret = ldb_build_rename_req(&rename_req, ldb_module_get_ctx(ac->module), ac,
147 ac->req->op.rename.olddn,
148 ac->req->op.rename.newdn,
150 ac, subtree_rename_callback,
152 LDB_REQ_SET_LOCATION(req);
153 if (ret != LDB_SUCCESS) {
158 return ldb_next_request(ac->module, rename_req);
165 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
167 struct ldb_context *ldb;
168 static const char * const no_attrs[] = {NULL};
169 struct ldb_request *search_req;
170 struct subtree_rename_context *ac;
173 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
174 return ldb_next_request(module, req);
177 ldb = ldb_module_get_ctx(module);
179 /* This gets complex: We need to:
180 - Do a search for all entires under this entry
181 - Wait for these results to appear
182 - In the callback for each result, issue a modify request
183 - That will include this rename, we hope
184 - Wait for each modify result
188 ac = subren_ctx_init(module, req);
193 ret = ldb_build_search_req(&search_req, ldb_module_get_ctx(ac->module), ac,
194 ac->req->op.rename.olddn,
200 subtree_rename_search_onelevel_callback,
202 LDB_REQ_SET_LOCATION(search_req);
203 if (ret != LDB_SUCCESS) {
207 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
209 if (ret != LDB_SUCCESS) {
213 return ldb_next_request(ac->module, search_req);
216 static const struct ldb_module_ops ldb_subtree_rename_module_ops = {
217 .name = "subtree_rename",
218 .rename = subtree_rename
221 int ldb_subtree_rename_module_init(const char *version)
223 LDB_MODULE_CHECK_VERSION(version);
224 return ldb_register_module(&ldb_subtree_rename_module_ops);