Only set showOnlyInAdvancedView: TRUE when adding default values.
[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) return ret;
121
122                 talloc_steal(req, newdn);
123
124                 talloc_steal(req, ares->message->dn);
125                 
126                 talloc_free(ares);
127                 
128         } else if (ares->type == LDB_REPLY_DONE) {
129                 req = talloc(mem_ctx, struct ldb_request);
130                 *req = *ac->orig_req;
131                 talloc_free(ares);
132
133         } else {
134                 talloc_free(ares);
135                 return LDB_SUCCESS;
136         }
137         
138         ac->down_req = talloc_realloc(ac, ac->down_req, 
139                                       struct ldb_request *, ac->num_requests + 1);
140         if (!ac->down_req) {
141                 ldb_oom(ac->module->ldb);
142                 return LDB_ERR_OPERATIONS_ERROR;
143         }
144         ac->down_req[ac->num_requests] = req;
145         ac->num_requests++;
146         
147         return ldb_next_request(ac->module, req);
148         
149 }
150
151 /* rename */
152 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
153 {
154         const char *attrs[] = { NULL };
155         struct ldb_request *new_req;
156         struct subtree_rename_context *ac;
157         int ret;
158         if (ldb_dn_is_special(req->op.rename.olddn)) { /* 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         if (ret != LDB_SUCCESS) {
186                 return ret;
187         }
188
189         ac->down_req = talloc_realloc(ac, ac->down_req, 
190                                         struct ldb_request *, ac->num_requests + 1);
191         if (!ac->down_req) {
192                 ldb_oom(ac->module->ldb);
193                 return LDB_ERR_OPERATIONS_ERROR;
194         }
195         ac->down_req[ac->num_requests] = new_req;
196         if (req == NULL) {
197                 ldb_oom(ac->module->ldb);
198                 return LDB_ERR_OPERATIONS_ERROR;
199         }
200         ac->num_requests++;
201         return ldb_next_request(module, new_req);
202 }
203
204
205 static int subtree_rename_wait_none(struct ldb_handle *handle) {
206         struct subtree_rename_context *ac;
207         int i, ret = LDB_ERR_OPERATIONS_ERROR;
208         if (!handle || !handle->private_data) {
209                 return LDB_ERR_OPERATIONS_ERROR;
210         }
211
212         if (handle->state == LDB_ASYNC_DONE) {
213                 return handle->status;
214         }
215
216         handle->state = LDB_ASYNC_PENDING;
217         handle->status = LDB_SUCCESS;
218
219         ac = talloc_get_type(handle->private_data, struct subtree_rename_context);
220
221         for (i=0; i < ac->num_requests; i++) {
222                 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
223                 
224                 if (ret != LDB_SUCCESS) {
225                         handle->status = ret;
226                         goto done;
227                 }
228                 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
229                         handle->status = ac->down_req[i]->handle->status;
230                         goto done;
231                 }
232                 
233                 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
234                         return LDB_SUCCESS;
235                 }
236         }
237
238 done:
239         handle->state = LDB_ASYNC_DONE;
240         return ret;
241
242 }
243
244 static int subtree_rename_wait_all(struct ldb_handle *handle) {
245
246         int ret;
247
248         while (handle->state != LDB_ASYNC_DONE) {
249                 ret = subtree_rename_wait_none(handle);
250                 if (ret != LDB_SUCCESS) {
251                         return ret;
252                 }
253         }
254
255         return handle->status;
256 }
257
258 static int subtree_rename_wait(struct ldb_handle *handle, enum ldb_wait_type type)
259 {
260         if (type == LDB_WAIT_ALL) {
261                 return subtree_rename_wait_all(handle);
262         } else {
263                 return subtree_rename_wait_none(handle);
264         }
265 }
266
267 static const struct ldb_module_ops subtree_rename_ops = {
268         .name              = "subtree_rename",
269         .rename            = subtree_rename,
270         .wait              = subtree_rename_wait,
271 };
272
273 int ldb_subtree_rename_init(void)
274 {
275         return ldb_register_module(&subtree_rename_ops);
276 }