267892cf5814e4a7d9f7713ed86e57cf31b6cc82
[metze/samba/wip.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 static struct subtree_rename_context *subtree_rename_init_handle(struct ldb_request *req, 
48                                                                  struct ldb_module *module)
49 {
50         struct subtree_rename_context *ac;
51         struct ldb_handle *h;
52
53         h = talloc_zero(req, struct ldb_handle);
54         if (h == NULL) {
55                 ldb_set_errstring(module->ldb, "Out of Memory");
56                 return NULL;
57         }
58
59         h->module = module;
60
61         ac = talloc_zero(h, struct subtree_rename_context);
62         if (ac == NULL) {
63                 ldb_set_errstring(module->ldb, "Out of Memory");
64                 talloc_free(h);
65                 return NULL;
66         }
67
68         h->private_data = ac;
69
70         ac->module = module;
71         ac->handle = h;
72         ac->orig_req = req;
73
74         req->handle = h;
75
76         return ac;
77 }
78
79
80 static int subtree_rename_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares) 
81 {
82         struct ldb_request *req;
83         struct subtree_rename_context *ac = talloc_get_type(context, struct subtree_rename_context);
84         TALLOC_CTX *mem_ctx = talloc_new(ac);
85     
86         if (!mem_ctx) {
87                 ldb_oom(ac->module->ldb);
88                 return LDB_ERR_OPERATIONS_ERROR;
89         }
90         /* OK, we have one of *many* search results here:
91
92            We should also get the entry we tried to rename.  This
93            callback handles this and everything below it.
94          */
95
96         /* Only entries are interesting, and we handle the case of the parent seperatly */
97         if (ares->type == LDB_REPLY_ENTRY
98             && ldb_dn_compare(ares->message->dn, ac->orig_req->op.rename.olddn) != 0) {
99                 /* And it is an actual entry: now create a rename from it */
100                 int ret;
101
102                 struct ldb_dn *newdn = ldb_dn_copy(mem_ctx, ares->message->dn);
103                 if (!newdn) {
104                         ldb_oom(ac->module->ldb);
105                         return LDB_ERR_OPERATIONS_ERROR;
106                 }
107                         
108                 ldb_dn_remove_base_components(newdn, ldb_dn_get_comp_num(ac->orig_req->op.rename.olddn));
109
110                 if (!ldb_dn_add_base(newdn, ac->orig_req->op.rename.newdn)) {
111                         ldb_oom(ac->module->ldb);
112                         return LDB_ERR_OPERATIONS_ERROR;
113                 }
114
115                 ret = ldb_build_rename_req(&req, ldb, mem_ctx,
116                                            ares->message->dn,
117                                            newdn,
118                                            NULL,
119                                            NULL,
120                                            NULL);
121                 
122                 if (ret != LDB_SUCCESS) return ret;
123
124                 talloc_steal(req, newdn);
125
126                 talloc_steal(req, ares->message->dn);
127                 
128                 talloc_free(ares);
129                 
130         } else if (ares->type == LDB_REPLY_DONE) {
131                 req = talloc(mem_ctx, struct ldb_request);
132                 *req = *ac->orig_req;
133                 talloc_free(ares);
134
135         } else {
136                 talloc_free(ares);
137                 return LDB_SUCCESS;
138         }
139         
140         ac->down_req = talloc_realloc(ac, ac->down_req, 
141                                       struct ldb_request *, ac->num_requests + 1);
142         if (!ac->down_req) {
143                 ldb_oom(ac->module->ldb);
144                 return LDB_ERR_OPERATIONS_ERROR;
145         }
146         ac->down_req[ac->num_requests] = req;
147         ac->num_requests++;
148         
149         return ldb_next_request(ac->module, req);
150         
151 }
152
153 /* rename */
154 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
155 {
156         const char *attrs[] = { NULL };
157         struct ldb_request *new_req;
158         struct subtree_rename_context *ac;
159         int ret;
160         struct ldb_search_options_control *search_options;
161         if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
162                 return ldb_next_request(module, req);
163         }
164
165         /* This gets complex:  We need to:
166            - Do a search for all entires under this entry 
167            - Wait for these results to appear
168            - In the callback for each result, issue a modify request
169             - That will include this rename, we hope
170            - Wait for each modify result
171            - Regain our sainity 
172         */
173
174         ac = subtree_rename_init_handle(req, module);
175         if (!ac) {
176                 return LDB_ERR_OPERATIONS_ERROR;
177         }
178
179         ret = ldb_build_search_req(&new_req, module->ldb, req,
180                                    req->op.rename.olddn, 
181                                    LDB_SCOPE_SUBTREE,
182                                    "(objectClass=*)",
183                                    attrs,
184                                    req->controls,
185                                    ac, 
186                                    subtree_rename_search_callback);
187
188         if (ret != LDB_SUCCESS) {
189                 return ret;
190         }
191
192         /* We want to find any partitions under this entry.  That way,
193          * if we try and rename a whole partition, the partitions
194          * module should cause us to fail the lot */
195         search_options = talloc(ac, struct ldb_search_options_control);
196         if (!search_options) {
197                 ldb_oom(ac->module->ldb);
198                 return LDB_ERR_OPERATIONS_ERROR;
199         }
200         search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
201
202         ret = ldb_request_add_control(new_req, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
203         if (ret != LDB_SUCCESS) {
204                 return ret;
205         }
206
207         ac->down_req = talloc_realloc(ac, ac->down_req, 
208                                         struct ldb_request *, ac->num_requests + 1);
209         if (!ac->down_req) {
210                 ldb_oom(ac->module->ldb);
211                 return LDB_ERR_OPERATIONS_ERROR;
212         }
213         ac->down_req[ac->num_requests] = new_req;
214         if (req == NULL) {
215                 ldb_oom(ac->module->ldb);
216                 return LDB_ERR_OPERATIONS_ERROR;
217         }
218         ac->num_requests++;
219         return ldb_next_request(module, new_req);
220 }
221
222 static int subtree_rename_wait_none(struct ldb_handle *handle) {
223         struct subtree_rename_context *ac;
224         int i, ret;
225         if (!handle || !handle->private_data) {
226                 return LDB_ERR_OPERATIONS_ERROR;
227         }
228
229         if (handle->state == LDB_ASYNC_DONE) {
230                 return handle->status;
231         }
232
233         handle->state = LDB_ASYNC_PENDING;
234         handle->status = LDB_SUCCESS;
235
236         ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
237
238         for (i=0; i < ac->num_requests; i++) {
239                 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
240                 
241                 if (ret != LDB_SUCCESS) {
242                         handle->status = ret;
243                         goto done;
244                 }
245                 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
246                         handle->status = ac->down_req[i]->handle->status;
247                         goto done;
248                 }
249                 
250                 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
251                         return LDB_SUCCESS;
252                 }
253         }
254
255 done:
256         handle->state = LDB_ASYNC_DONE;
257         return ret;
258
259 }
260
261 static int subtree_rename_wait_all(struct ldb_handle *handle) {
262
263         int ret;
264
265         while (handle->state != LDB_ASYNC_DONE) {
266                 ret = subtree_rename_wait_none(handle);
267                 if (ret != LDB_SUCCESS) {
268                         return ret;
269                 }
270         }
271
272         return handle->status;
273 }
274
275 static int subtree_rename_wait(struct ldb_handle *handle, enum ldb_wait_type type)
276 {
277         if (type == LDB_WAIT_ALL) {
278                 return subtree_rename_wait_all(handle);
279         } else {
280                 return subtree_rename_wait_none(handle);
281         }
282 }
283
284 static const struct ldb_module_ops subtree_rename_ops = {
285         .name              = "subtree_rename",
286         .rename            = subtree_rename,
287         .wait              = subtree_rename_wait,
288 };
289
290 int ldb_subtree_rename_init(void)
291 {
292         return ldb_register_module(&subtree_rename_ops);
293 }