Fix more valgrind issues.
[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    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.
11    
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.
16    
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/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb subtree rename module
25  *
26  *  Description: Rename a subtree in LDB
27  *
28  *  Author: Andrew Bartlett
29  */
30
31 #include "ldb_includes.h"
32
33 struct subtree_rename_context {
34         struct ldb_module *module;
35         struct ldb_handle *handle;
36         struct ldb_request *orig_req;
37
38         struct ldb_request **down_req;
39         int num_requests;
40         int finished_requests;
41
42         int num_children;
43 };
44
45 static struct subtree_rename_context *subtree_rename_init_handle(struct ldb_request *req, 
46                                                                  struct ldb_module *module)
47 {
48         struct subtree_rename_context *ac;
49         struct ldb_handle *h;
50
51         h = talloc_zero(req, struct ldb_handle);
52         if (h == NULL) {
53                 ldb_set_errstring(module->ldb, "Out of Memory");
54                 return NULL;
55         }
56
57         h->module = module;
58
59         ac = talloc_zero(h, struct subtree_rename_context);
60         if (ac == NULL) {
61                 ldb_set_errstring(module->ldb, "Out of Memory");
62                 talloc_free(h);
63                 return NULL;
64         }
65
66         h->private_data = ac;
67
68         ac->module = module;
69         ac->handle = h;
70         ac->orig_req = req;
71
72         req->handle = h;
73
74         return ac;
75 }
76
77
78 static int subtree_rename_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares) 
79 {
80         struct ldb_request *req;
81         struct subtree_rename_context *ac = talloc_get_type(context, struct subtree_rename_context);
82         TALLOC_CTX *mem_ctx = talloc_new(ac);
83     
84         if (!mem_ctx) {
85                 ldb_oom(ac->module->ldb);
86                 return LDB_ERR_OPERATIONS_ERROR;
87         }
88         /* OK, we have one of *many* search results here:
89
90            We should also get the entry we tried to rename.  This
91            callback handles this and everything below it.
92          */
93
94         /* Only entries are interesting, and we handle the case of the parent seperatly */
95         if (ares->type == LDB_REPLY_ENTRY
96             && ldb_dn_compare(ares->message->dn, ac->orig_req->op.rename.olddn) != 0) {
97                 /* And it is an actual entry: now create a rename from it */
98                 int ret;
99
100                 struct ldb_dn *newdn = ldb_dn_copy(mem_ctx, ares->message->dn);
101                 if (!newdn) {
102                         ldb_oom(ac->module->ldb);
103                         return LDB_ERR_OPERATIONS_ERROR;
104                 }
105                         
106                 ldb_dn_remove_base_components(newdn, ldb_dn_get_comp_num(ac->orig_req->op.rename.olddn));
107
108                 if (!ldb_dn_add_base(newdn, ac->orig_req->op.rename.newdn)) {
109                         ldb_oom(ac->module->ldb);
110                         return LDB_ERR_OPERATIONS_ERROR;
111                 }
112
113                 ret = ldb_build_rename_req(&req, ldb, mem_ctx,
114                                            ares->message->dn,
115                                            newdn,
116                                            NULL,
117                                            NULL,
118                                            NULL);
119                 
120                 if (ret != LDB_SUCCESS) {
121                         return ret;
122                 }
123
124                 ret = ldb_set_timeout_from_prev_req(ldb, ac->orig_req, req);
125                 
126                 if (ret != LDB_SUCCESS) {
127                         return ret;
128                 }
129
130                 talloc_steal(req, newdn);
131
132                 talloc_steal(req, ares->message->dn);
133                 
134                 talloc_free(ares);
135                 
136         } else if (ares->type == LDB_REPLY_DONE) {
137                 req = talloc(mem_ctx, struct ldb_request);
138                 *req = *ac->orig_req;
139                 talloc_free(ares);
140
141         } else {
142                 talloc_free(ares);
143                 return LDB_SUCCESS;
144         }
145         
146         ac->down_req = talloc_realloc(ac, ac->down_req, 
147                                       struct ldb_request *, ac->num_requests + 1);
148         if (!ac->down_req) {
149                 ldb_oom(ac->module->ldb);
150                 return LDB_ERR_OPERATIONS_ERROR;
151         }
152         ac->down_req[ac->num_requests] = req;
153         ac->num_requests++;
154         
155         return ldb_next_request(ac->module, req);
156         
157 }
158
159 /* rename */
160 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
161 {
162         const char *attrs[] = { NULL };
163         struct ldb_request *new_req;
164         struct subtree_rename_context *ac;
165         int ret;
166         if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
167                 return ldb_next_request(module, req);
168         }
169
170         /* This gets complex:  We need to:
171            - Do a search for all entires under this entry 
172            - Wait for these results to appear
173            - In the callback for each result, issue a modify request
174             - That will include this rename, we hope
175            - Wait for each modify result
176            - Regain our sainity 
177         */
178
179         ac = subtree_rename_init_handle(req, module);
180         if (!ac) {
181                 return LDB_ERR_OPERATIONS_ERROR;
182         }
183
184         ret = ldb_build_search_req(&new_req, module->ldb, req,
185                                    req->op.rename.olddn, 
186                                    LDB_SCOPE_SUBTREE,
187                                    "(objectClass=*)",
188                                    attrs,
189                                    req->controls,
190                                    ac, 
191                                    subtree_rename_search_callback);
192
193         if (ret != LDB_SUCCESS) {
194                 return ret;
195         }
196
197         ret = ldb_set_timeout_from_prev_req(module->ldb, req, new_req);
198         
199         if (ret != LDB_SUCCESS) {
200                 return ret;
201         }
202
203         ac->down_req = talloc_realloc(ac, ac->down_req, 
204                                         struct ldb_request *, ac->num_requests + 1);
205         if (!ac->down_req) {
206                 ldb_oom(ac->module->ldb);
207                 return LDB_ERR_OPERATIONS_ERROR;
208         }
209         ac->down_req[ac->num_requests] = new_req;
210         if (req == NULL) {
211                 ldb_oom(ac->module->ldb);
212                 return LDB_ERR_OPERATIONS_ERROR;
213         }
214         ac->num_requests++;
215         return ldb_next_request(module, new_req);
216 }
217
218
219 static int subtree_rename_wait_none(struct ldb_handle *handle) {
220         struct subtree_rename_context *ac;
221         int i, ret = LDB_ERR_OPERATIONS_ERROR;
222         if (!handle || !handle->private_data) {
223                 return LDB_ERR_OPERATIONS_ERROR;
224         }
225
226         if (handle->state == LDB_ASYNC_DONE) {
227                 return handle->status;
228         }
229
230         handle->state = LDB_ASYNC_PENDING;
231         handle->status = LDB_SUCCESS;
232
233         ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
234
235         for (i=0; i < ac->num_requests; i++) {
236                 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
237                 
238                 if (ret != LDB_SUCCESS) {
239                         handle->status = ret;
240                         goto done;
241                 }
242                 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
243                         handle->status = ac->down_req[i]->handle->status;
244                         goto done;
245                 }
246                 
247                 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
248                         return LDB_SUCCESS;
249                 }
250         }
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 const struct ldb_module_ops ldb_subtree_rename_module_ops = {
282         .name              = "subtree_rename",
283         .rename            = subtree_rename,
284         .wait              = subtree_rename_wait,
285 };