s4-dsdb: use ldb_operr() in the dsdb code
[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    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_operr(ldb);
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
227         if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
228                 return ldb_next_request(module, req);
229         }
230
231         ldb = ldb_module_get_ctx(module);
232
233         /* This gets complex:  We need to:
234            - Do a search for all entires under this entry 
235            - Wait for these results to appear
236            - In the callback for each result, issue a modify request
237            - That will include this rename, we hope
238            - Wait for each modify result
239            - Regain our sanity
240         */
241
242         ac = subren_ctx_init(module, req);
243         if (!ac) {
244                 return ldb_operr(ldb);
245         }
246
247         /* add this entry as the first to do */
248         ac->current = talloc_zero(ac, struct subren_msg_store);
249         if (ac->current == NULL) {
250                 return ldb_oom(ldb);
251         }
252         ac->current->olddn = req->op.rename.olddn;
253         ac->current->newdn = req->op.rename.newdn;
254         ac->list = ac->current;
255
256         ret = ldb_build_search_req(&search_req, ldb, ac,
257                                    req->op.rename.olddn, 
258                                    LDB_SCOPE_SUBTREE,
259                                    "(objectClass=*)",
260                                    attrs,
261                                    NULL,
262                                    ac, 
263                                    subtree_rename_search_callback,
264                                    req);
265         if (ret != LDB_SUCCESS) {
266                 return ret;
267         }
268
269         return ldb_next_request(module, search_req);
270 }
271
272 _PUBLIC_ const struct ldb_module_ops ldb_subtree_rename_module_ops = {
273         .name              = "subtree_rename",
274         .rename            = subtree_rename
275 };