Merge commit 'master/master'
[ira/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    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 subren_msg_store {
34         struct subren_msg_store *next;
35         struct ldb_dn *olddn;
36         struct ldb_dn *newdn;
37 };
38
39 struct subtree_rename_context {
40         struct ldb_module *module;
41         struct ldb_request *req;
42
43         struct subren_msg_store *list;
44         struct subren_msg_store *current;
45 };
46
47 static struct subtree_rename_context *subren_ctx_init(struct ldb_module *module,
48                                          struct ldb_request *req)
49 {
50         struct subtree_rename_context *ac;
51
52         ac = talloc_zero(req, struct subtree_rename_context);
53         if (ac == NULL) {
54                 ldb_oom(module->ldb);
55                 return NULL;
56         }
57
58         ac->module = module;
59         ac->req = req;
60
61         return ac;
62 }
63
64 static int subtree_rename_next_request(struct subtree_rename_context *ac);
65
66 static int subtree_rename_callback(struct ldb_request *req,
67                                    struct ldb_reply *ares)
68 {
69         struct subtree_rename_context *ac;
70         int ret;
71
72         ac = talloc_get_type(req->context, struct subtree_rename_context);
73
74         if (!ares) {
75                 return ldb_module_done(ac->req, NULL, NULL,
76                                         LDB_ERR_OPERATIONS_ERROR);
77         }
78
79         if (ares->error != LDB_SUCCESS) {
80                 return ldb_module_done(ac->req, ares->controls,
81                                         ares->response, ares->error);
82         }
83
84         if (ares->type != LDB_REPLY_DONE) {
85                 ldb_set_errstring(ac->module->ldb, "Invalid reply type!\n");
86                 return ldb_module_done(ac->req, NULL, NULL,
87                                         LDB_ERR_OPERATIONS_ERROR);
88         }
89
90         if (ac->current == NULL) {
91                 /* this was the last one */
92                 return ldb_module_done(ac->req, ares->controls,
93                                         ares->response, LDB_SUCCESS);
94         }
95
96         ret = subtree_rename_next_request(ac);
97         if (ret != LDB_SUCCESS) {
98                 return ldb_module_done(ac->req, NULL, NULL, ret);
99         }
100
101         talloc_free(ares);
102         return LDB_SUCCESS;
103 }
104
105 static int subtree_rename_next_request(struct subtree_rename_context *ac)
106 {
107         struct ldb_request *req;
108         int ret;
109
110         if (ac->current == NULL) {
111                 return LDB_ERR_OPERATIONS_ERROR;
112         }
113
114         ret = ldb_build_rename_req(&req, ac->module->ldb, ac->current,
115                                    ac->current->olddn,
116                                    ac->current->newdn,
117                                    ac->req->controls,
118                                    ac, subtree_rename_callback,
119                                    ac->req);
120         if (ret != LDB_SUCCESS) {
121                 return ret;
122         }
123
124         ac->current = ac->current->next;
125
126         return ldb_next_request(ac->module, req);
127 }
128
129 static int subtree_rename_search_callback(struct ldb_request *req,
130                                           struct ldb_reply *ares)
131 {
132         struct subren_msg_store *store;
133         struct subtree_rename_context *ac;
134         int ret;
135
136         ac = talloc_get_type(req->context, struct subtree_rename_context);
137
138         if (!ares || !ac->current) {
139                 return ldb_module_done(ac->req, NULL, NULL,
140                                         LDB_ERR_OPERATIONS_ERROR);
141         }
142         if (ares->error != LDB_SUCCESS) {
143                 return ldb_module_done(ac->req, ares->controls,
144                                         ares->response, ares->error);
145         }
146
147         switch (ares->type) {
148         case LDB_REPLY_ENTRY:
149
150                 if (ldb_dn_compare(ares->message->dn, ac->list->olddn) == 0) {
151                         /* this was already stored by the
152                          * subtree_rename_search() */
153                         talloc_free(ares);
154                         return LDB_SUCCESS;
155                 }
156
157                 store = talloc_zero(ac, struct subren_msg_store);
158                 if (store == NULL) {
159                         return ldb_module_done(ac->req, NULL, NULL,
160                                                 LDB_ERR_OPERATIONS_ERROR);
161                 }
162                 ac->current->next = store;
163                 ac->current = store;
164
165                 /* the first list element contains the base for the rename */
166                 store->olddn = talloc_steal(store, ares->message->dn);
167                 store->newdn = ldb_dn_copy(store, store->olddn);
168
169                 if ( ! ldb_dn_remove_base_components(store->newdn,
170                                 ldb_dn_get_comp_num(ac->list->olddn))) {
171                         return ldb_module_done(ac->req, NULL, NULL,
172                                                 LDB_ERR_OPERATIONS_ERROR);
173                 }
174
175                 if ( ! ldb_dn_add_base(store->newdn, ac->list->newdn)) {
176                         return ldb_module_done(ac->req, NULL, NULL,
177                                                 LDB_ERR_OPERATIONS_ERROR);
178                 }
179
180                 break;
181
182         case LDB_REPLY_REFERRAL:
183                 /* ignore */
184                 break;
185
186         case LDB_REPLY_DONE:
187
188                 /* rewind ac->current */
189                 ac->current = ac->list;
190
191                 /* All dns set up, start with the first one */
192                 ret = subtree_rename_next_request(ac);
193
194                 if (ret != LDB_SUCCESS) {
195                         return ldb_module_done(ac->req, NULL, NULL, ret);
196                 }
197                 break;
198         }
199
200         talloc_free(ares);
201         return LDB_SUCCESS;
202 }
203
204 /* rename */
205 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
206 {
207         static const char *attrs[2] = { "distinguishedName", NULL };
208         struct ldb_request *search_req;
209         struct subtree_rename_context *ac;
210         int ret;
211         if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
212                 return ldb_next_request(module, req);
213         }
214
215         /* This gets complex:  We need to:
216            - Do a search for all entires under this entry 
217            - Wait for these results to appear
218            - In the callback for each result, issue a modify request
219             - That will include this rename, we hope
220            - Wait for each modify result
221            - Regain our sainity 
222         */
223
224         ac = subren_ctx_init(module, req);
225         if (!ac) {
226                 return LDB_ERR_OPERATIONS_ERROR;
227         }
228
229         /* add this entry as the first to do */
230         ac->current = talloc_zero(ac, struct subren_msg_store);
231         if (ac->current == NULL) {
232                 return LDB_ERR_OPERATIONS_ERROR;
233         }
234         ac->current->olddn = req->op.rename.olddn;
235         ac->current->newdn = req->op.rename.newdn;
236         ac->list = ac->current;
237
238         ret = ldb_build_search_req(&search_req, module->ldb, ac,
239                                    req->op.rename.olddn, 
240                                    LDB_SCOPE_SUBTREE,
241                                    "(objectClass=*)",
242                                    attrs,
243                                    NULL,
244                                    ac, 
245                                    subtree_rename_search_callback,
246                                    req);
247         if (ret != LDB_SUCCESS) {
248                 return ret;
249         }
250
251         return ldb_next_request(module, search_req);
252 }
253
254 const struct ldb_module_ops ldb_subtree_rename_module_ops = {
255         .name              = "subtree_rename",
256         .rename            = subtree_rename,
257 };