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