Update 4.2 Roadmap file
[mat/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    Copyright (C) Matthias Dieter Wallnöfer <mdw@samba.org> 2010-2011
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23  *  Name: ldb
24  *
25  *  Component: ldb subtree rename module
26  *
27  *  Description: Rename a subtree in LDB
28  *
29  *  Author: Andrew Bartlett
30  */
31
32 #include "includes.h"
33 #include <ldb.h>
34 #include <ldb_module.h>
35 #include "libds/common/flags.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "dsdb/samdb/ldb_modules/util.h"
38
39 struct subtree_rename_context {
40         struct ldb_module *module;
41         struct ldb_request *req;
42 };
43
44 static struct subtree_rename_context *subren_ctx_init(struct ldb_module *module,
45                                                       struct ldb_request *req)
46 {
47         struct subtree_rename_context *ac;
48
49
50         ac = talloc_zero(req, struct subtree_rename_context);
51         if (ac == NULL) {
52                 return NULL;
53         }
54
55         ac->module = module;
56         ac->req = req;
57
58         return ac;
59 }
60
61 static int subtree_rename_callback(struct ldb_request *req,
62                                    struct ldb_reply *ares)
63 {
64         struct ldb_context *ldb;
65         struct subtree_rename_context *ac;
66
67         ac = talloc_get_type(req->context, struct subtree_rename_context);
68         ldb = ldb_module_get_ctx(ac->module);
69
70         if (!ares) {
71                 return ldb_module_done(ac->req, NULL, NULL,
72                                         LDB_ERR_OPERATIONS_ERROR);
73         }
74
75         if (ares->type == LDB_REPLY_REFERRAL) {
76                 return ldb_module_send_referral(ac->req, ares->referral);
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_asprintf_errstring(ldb, "Invalid LDB reply type %d", ares->type);
86                 return ldb_module_done(ac->req, NULL, NULL,
87                                         LDB_ERR_OPERATIONS_ERROR);
88         }
89
90         talloc_free(ares);
91         return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
92 }
93
94 static int subtree_rename_search_onelevel_callback(struct ldb_request *req,
95                                                    struct ldb_reply *ares)
96 {
97         struct subtree_rename_context *ac;
98         struct ldb_request *rename_req;
99         int ret;
100
101         ac = talloc_get_type(req->context, struct subtree_rename_context);
102
103         if (!ares) {
104                 return ldb_module_done(ac->req, NULL, NULL,
105                                         LDB_ERR_OPERATIONS_ERROR);
106         }
107         if (ares->error != LDB_SUCCESS) {
108                 return ldb_module_done(ac->req, ares->controls,
109                                         ares->response, ares->error);
110         }
111
112         switch (ares->type) {
113         case LDB_REPLY_ENTRY:
114         {
115                 struct ldb_dn *old_dn = ares->message->dn;
116                 struct ldb_dn *new_dn = ldb_dn_copy(ares, old_dn);
117                 if (!new_dn) {
118                         return ldb_module_oom(ac->module);
119                 }
120
121                 if ( ! ldb_dn_remove_base_components(new_dn,
122                                 ldb_dn_get_comp_num(ac->req->op.rename.olddn))) {
123                         return ldb_module_done(ac->req, NULL, NULL,
124                                                 LDB_ERR_OPERATIONS_ERROR);
125                 }
126
127                 if ( ! ldb_dn_add_base(new_dn, ac->req->op.rename.newdn)) {
128                         return ldb_module_done(ac->req, NULL, NULL,
129                                                 LDB_ERR_OPERATIONS_ERROR);
130                 }
131                 ret = dsdb_module_rename(ac->module, old_dn, new_dn, DSDB_FLAG_OWN_MODULE, req);
132                 if (ret != LDB_SUCCESS) {
133                         return ret;
134                 }
135
136                 talloc_free(ares);
137
138                 return LDB_SUCCESS;
139         }
140         case LDB_REPLY_REFERRAL:
141                 /* ignore */
142                 break;
143
144         case LDB_REPLY_DONE:
145
146                 ret = ldb_build_rename_req(&rename_req, ldb_module_get_ctx(ac->module), ac,
147                                            ac->req->op.rename.olddn,
148                                            ac->req->op.rename.newdn,
149                                            ac->req->controls,
150                                            ac, subtree_rename_callback,
151                                            ac->req);
152                 LDB_REQ_SET_LOCATION(req);
153                 if (ret != LDB_SUCCESS) {
154                         return ret;
155                 }
156
157                 talloc_free(ares);
158                 return ldb_next_request(ac->module, rename_req);
159         }
160
161         return LDB_SUCCESS;
162 }
163
164 /* rename */
165 static int subtree_rename(struct ldb_module *module, struct ldb_request *req)
166 {
167         struct ldb_context *ldb;
168         static const char * const no_attrs[] = {NULL};
169         struct ldb_request *search_req;
170         struct subtree_rename_context *ac;
171         int ret;
172
173         if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
174                 return ldb_next_request(module, req);
175         }
176
177         ldb = ldb_module_get_ctx(module);
178
179         /* This gets complex:  We need to:
180            - Do a search for all entires under this entry 
181            - Wait for these results to appear
182            - In the callback for each result, issue a modify request
183            - That will include this rename, we hope
184            - Wait for each modify result
185            - Regain our sanity
186         */
187
188         ac = subren_ctx_init(module, req);
189         if (!ac) {
190                 return ldb_oom(ldb);
191         }
192
193         ret = ldb_build_search_req(&search_req, ldb_module_get_ctx(ac->module), ac,
194                                    ac->req->op.rename.olddn,
195                                    LDB_SCOPE_ONELEVEL,
196                                    "(objectClass=*)",
197                                    no_attrs,
198                                    NULL,
199                                    ac,
200                                    subtree_rename_search_onelevel_callback,
201                                    req);
202         LDB_REQ_SET_LOCATION(search_req);
203         if (ret != LDB_SUCCESS) {
204                 return ret;
205         }
206
207         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
208                                       true, NULL);
209         if (ret != LDB_SUCCESS) {
210                 return ret;
211         }
212
213         return ldb_next_request(ac->module, search_req);
214 }
215
216 static const struct ldb_module_ops ldb_subtree_rename_module_ops = {
217         .name              = "subtree_rename",
218         .rename            = subtree_rename
219 };
220
221 int ldb_subtree_rename_module_init(const char *version)
222 {
223         LDB_MODULE_CHECK_VERSION(version);
224         return ldb_register_module(&ldb_subtree_rename_module_ops);
225 }