993331216eb3cbdffcc6c0dd623f8af303e6289a
[jra/samba/.git] / source4 / dsdb / samdb / ldb_modules / subtree_rename.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2007
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 3 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb subtree rename module
29  *
30  *  Description: Rename a subtree in LDB
31  *
32  *  Author: Andrew Bartlett
33  */
34
35 #include "ldb_includes.h"
36
37 struct subtree_rename_context {
38         struct ldb_module *module;
39         struct ldb_handle *handle;
40         struct ldb_request *orig_req;
41
42         struct ldb_request **down_req;
43         int num_requests;
44         int finished_requests;
45 };
46
47 struct subtree_rename_search_context {
48         struct ldb_module *module;
49         struct ldb_request *orig_req;
50         struct ldb_handle *handle;
51
52         struct ldb_request **down_req;
53         int num_requests;
54         int finished_requests;
55 };
56
57 static struct subtree_rename_context *subtree_rename_init_handle(struct ldb_request *req, 
58                                                                  struct ldb_module *module)
59 {
60         struct subtree_rename_context *ac;
61         struct ldb_handle *h;
62
63         h = talloc_zero(req, struct ldb_handle);
64         if (h == NULL) {
65                 ldb_set_errstring(module->ldb, "Out of Memory");
66                 return NULL;
67         }
68
69         h->module = module;
70
71         ac = talloc_zero(h, struct subtree_rename_context);
72         if (ac == NULL) {
73                 ldb_set_errstring(module->ldb, "Out of Memory");
74                 talloc_free(h);
75                 return NULL;
76         }
77
78         h->private_data = ac;
79
80         ac->module = module;
81         ac->handle = h;
82         ac->orig_req = req;
83
84         req->handle = h;
85
86         return ac;
87 }
88
89
90 static int subtree_rename_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares) 
91 {
92         /* OK, we have one of *many* search results here:
93
94            We should also get the entry we tried to rename.  This
95            callback handles this and everything below it.
96          */
97
98         if (ares->type == LDB_REPLY_ENTRY) {
99                 /* And it is an actual entry: now create a rename from it */
100                 struct subtree_rename_context *ac = talloc_get_type(context, struct subtree_rename_context);
101                 struct ldb_request *req;
102                 int ret;
103
104                 TALLOC_CTX *mem_ctx = talloc_new(ac);
105                 
106                 struct ldb_dn *newdn = ldb_dn_copy(mem_ctx, ares->message->dn);
107                 if (!newdn) {
108                         ldb_oom(ac->module->ldb);
109                         return LDB_ERR_OPERATIONS_ERROR;
110                 }
111                         
112                 ldb_dn_remove_base_components(newdn, ldb_dn_get_comp_num(ac->orig_req->op.rename.olddn));
113
114                 if (!ldb_dn_add_base(newdn, ac->orig_req->op.rename.newdn)) {
115                         ldb_oom(ac->module->ldb);
116                         return LDB_ERR_OPERATIONS_ERROR;
117                 }
118
119                 ret = ldb_build_rename_req(&req, ldb, mem_ctx,
120                                            ares->message->dn,
121                                            newdn,
122                                            NULL,
123                                            NULL,
124                                            NULL);
125                 
126                 if (ret != LDB_SUCCESS) return ret;
127
128                 talloc_steal(req, newdn);
129
130                 req->handle = ac->handle;
131
132                 ac->down_req = talloc_realloc(ac, ac->down_req, 
133                                               struct ldb_request *, ac->num_requests + 1);
134                 if (!ac->down_req) {
135                         ldb_oom(ac->module->ldb);
136                         return LDB_ERR_OPERATIONS_ERROR;
137                 }
138                 ac->down_req[ac->num_requests] = req;
139                 
140                 return ldb_next_request(ac->module, req);
141
142         } else {
143                 talloc_free(ares);
144         }
145
146         return LDB_SUCCESS;
147
148 }
149
150 /* rename */
151 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
152 {
153         const char *attrs[] = { NULL };
154         struct ldb_request *new_req;
155         struct subtree_rename_context *ac;
156         int ret;
157         struct ldb_search_options_control *search_options;
158         if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
159                 return ldb_next_request(module, req);
160         }
161
162         /* This gets complex:  We need to:
163            - Do a search for all entires under this entry 
164            - Wait for these results to appear
165            - In the callback for each result, issue a modify request
166             - That will include this rename, we hope
167            - Wait for each modify result
168            - Regain our sainity 
169         */
170
171         ac = subtree_rename_init_handle(req, module);
172         if (!ac) {
173                 return LDB_ERR_OPERATIONS_ERROR;
174         }
175
176         ret = ldb_build_search_req(&new_req, module->ldb, req,
177                                    req->op.rename.olddn, 
178                                    LDB_SCOPE_SUBTREE,
179                                    "objectClass=*",
180                                    attrs,
181                                    req->controls,
182                                    ac, 
183                                    subtree_rename_search_callback);
184
185         /* We want to find any partitions under this entry.  That way,
186          * if we try and rename a whole partition, the partitions
187          * module should cause us to fail the lot */
188         search_options = talloc(ac, struct ldb_search_options_control);
189         if (!search_options) {
190                 ldb_oom(ac->module->ldb);
191                 return LDB_ERR_OPERATIONS_ERROR;
192         }
193         search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
194
195         ret = ldb_request_add_control(new_req, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
196         if (ret != LDB_SUCCESS) {
197                 return ret;
198         }
199
200         new_req->handle = req->handle;
201
202         ac->down_req = talloc_realloc(ac, ac->down_req, 
203                                         struct ldb_request *, ac->num_requests + 1);
204         if (!ac->down_req) {
205                 ldb_oom(ac->module->ldb);
206                 return LDB_ERR_OPERATIONS_ERROR;
207         }
208         ac->down_req[ac->num_requests] = new_req;
209         if (req == NULL) {
210                 ldb_oom(ac->module->ldb);
211                 return LDB_ERR_OPERATIONS_ERROR;
212         }
213         
214         return ldb_next_request(module, req);
215 }
216
217 static int subtree_rename_wait_none(struct ldb_handle *handle) {
218         struct subtree_rename_context *ac;
219         int i, ret;
220         if (!handle || !handle->private_data) {
221                 return LDB_ERR_OPERATIONS_ERROR;
222         }
223
224         if (handle->state == LDB_ASYNC_DONE) {
225                 return handle->status;
226         }
227
228         handle->state = LDB_ASYNC_PENDING;
229         handle->status = LDB_SUCCESS;
230
231         ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
232
233         for (i=0; i < ac->num_requests; i++) {
234                 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
235                 
236                 if (ret != LDB_SUCCESS) {
237                         handle->status = ret;
238                         goto done;
239                 }
240                 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
241                         handle->status = ac->down_req[i]->handle->status;
242                         goto done;
243                 }
244                 
245                 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
246                         return LDB_SUCCESS;
247                 }
248         }
249
250         ret = LDB_SUCCESS;
251
252 done:
253         handle->state = LDB_ASYNC_DONE;
254         return ret;
255
256 }
257
258 static int subtree_rename_wait_all(struct ldb_handle *handle) {
259
260         int ret;
261
262         while (handle->state != LDB_ASYNC_DONE) {
263                 ret = subtree_rename_wait_none(handle);
264                 if (ret != LDB_SUCCESS) {
265                         return ret;
266                 }
267         }
268
269         return handle->status;
270 }
271
272 static int subtree_rename_wait(struct ldb_handle *handle, enum ldb_wait_type type)
273 {
274         if (type == LDB_WAIT_ALL) {
275                 return subtree_rename_wait_all(handle);
276         } else {
277                 return subtree_rename_wait_none(handle);
278         }
279 }
280
281 static const struct ldb_module_ops subtree_rename_ops = {
282         .name              = "subtree_rename",
283         .rename            = subtree_rename,
284         .wait              = subtree_rename_wait,
285 };
286
287 int ldb_subtree_rename_init(void)
288 {
289         return ldb_register_module(&subtree_rename_ops);
290 }