Use struct-based rather than function-based initialization for ldb modules everywhere.
[abartlet/samba.git/.git] / source4 / dsdb / samdb / ldb_modules / subtree_delete.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 delete (prevention) module
25  *
26  *  Description: Prevent deletion of a subtree in LDB
27  *
28  *  Author: Andrew Bartlett
29  */
30
31 #include "ldb_includes.h"
32
33 struct subtree_delete_context {
34         enum sd_step {SD_SEARCH, SD_DO_DEL} step;
35
36         struct ldb_module *module;
37         struct ldb_handle *handle;
38         struct ldb_request *orig_req;
39
40         struct ldb_request *search_req;
41         struct ldb_request *down_req;
42
43         int num_children;
44 };
45
46 static struct subtree_delete_context *subtree_delete_init_handle(struct ldb_request *req, 
47                                                                  struct ldb_module *module)
48 {
49         struct subtree_delete_context *ac;
50         struct ldb_handle *h;
51
52         h = talloc_zero(req, struct ldb_handle);
53         if (h == NULL) {
54                 ldb_set_errstring(module->ldb, "Out of Memory");
55                 return NULL;
56         }
57
58         h->module = module;
59
60         ac = talloc_zero(h, struct subtree_delete_context);
61         if (ac == NULL) {
62                 ldb_set_errstring(module->ldb, "Out of Memory");
63                 talloc_free(h);
64                 return NULL;
65         }
66
67         h->private_data = ac;
68
69         ac->module = module;
70         ac->handle = h;
71         ac->orig_req = req;
72
73         req->handle = h;
74
75         return ac;
76 }
77
78 static int subtree_delete_check_for_children(struct subtree_delete_context *ac)
79 {
80         if (ac->num_children > 0) {
81                 ldb_asprintf_errstring(ac->module->ldb, "Cannot delete %s, not a leaf node (has %d children)\n",
82                                        ldb_dn_get_linearized(ac->orig_req->op.del.dn), ac->num_children);
83                 return LDB_ERR_NOT_ALLOWED_ON_NON_LEAF;
84         } else {
85                 struct ldb_request *req = talloc(ac, struct ldb_request);
86                 if (!req) {
87                         ldb_oom(ac->module->ldb);
88                         return LDB_ERR_OPERATIONS_ERROR;
89                 }
90                 *req = *ac->orig_req;
91                 
92                 /* Ensure any (io) errors during the search for
93                  * children don't propgate back in the error string */
94                 ldb_set_errstring(ac->module->ldb, NULL);
95
96                 ac->down_req = req;
97                 ac->step = SD_DO_DEL;
98                 return ldb_next_request(ac->module, req);
99         }
100 }
101
102 static int subtree_delete_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares) 
103 {
104         struct subtree_delete_context *ac = talloc_get_type(context, struct subtree_delete_context);
105         TALLOC_CTX *mem_ctx = talloc_new(ac);
106     
107         if (!mem_ctx) {
108                 ldb_oom(ac->module->ldb);
109                 return LDB_ERR_OPERATIONS_ERROR;
110         }
111         /* OK, we have one of *many* search results here:
112
113            We should also get the entry we tried to rename.  This
114            callback handles this and everything below it.
115          */
116
117         /* Only entries are interesting, and we handle the case of the parent seperatly */
118         if (ares->type == LDB_REPLY_ENTRY
119             && ldb_dn_compare(ares->message->dn, ac->orig_req->op.del.dn) != 0) {
120                 /* And it is an actual entry: now object bitterly that we are not a leaf node */
121                 ac->num_children++;
122         }
123         talloc_free(ares);
124         return LDB_SUCCESS;
125 }
126
127 /* rename */
128 static int subtree_delete(struct ldb_module *module, struct ldb_request *req)
129 {
130         const char *attrs[] = { NULL };
131         struct ldb_request *new_req;
132         struct subtree_delete_context *ac;
133         int ret;
134         if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
135                 return ldb_next_request(module, req);
136         }
137
138         /* This gets complex:  We need to:
139            - Do a search for all entires under this entry 
140            - Wait for these results to appear
141            - In the callback for each result, count the children (if any)
142            - return an error if there are any
143         */
144
145         ac = subtree_delete_init_handle(req, module);
146         if (!ac) {
147                 return LDB_ERR_OPERATIONS_ERROR;
148         }
149
150         ret = ldb_build_search_req(&new_req, module->ldb, req,
151                                    req->op.del.dn, 
152                                    LDB_SCOPE_SUBTREE,
153                                    "(objectClass=*)",
154                                    attrs,
155                                    req->controls,
156                                    ac, 
157                                    subtree_delete_search_callback);
158
159         if (ret != LDB_SUCCESS) {
160                 return ret;
161         }
162
163         ac->search_req = new_req;
164         if (req == NULL) {
165                 ldb_oom(ac->module->ldb);
166                 return LDB_ERR_OPERATIONS_ERROR;
167         }
168         return ldb_next_request(module, new_req);
169 }
170
171
172 static int subtree_delete_wait_none(struct ldb_handle *handle) {
173         struct subtree_delete_context *ac;
174         int ret = LDB_ERR_OPERATIONS_ERROR;
175         if (!handle || !handle->private_data) {
176                 return LDB_ERR_OPERATIONS_ERROR;
177         }
178
179         if (handle->state == LDB_ASYNC_DONE) {
180                 return handle->status;
181         }
182
183         handle->state = LDB_ASYNC_PENDING;
184         handle->status = LDB_SUCCESS;
185
186         ac = talloc_get_type(handle->private_data, struct subtree_delete_context);
187
188         switch (ac->step) {
189         case SD_SEARCH:
190                 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
191
192                 if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
193                         handle->status = ret;
194                         goto done;
195                 }
196                 if (ac->search_req->handle->status != LDB_SUCCESS
197                         && ac->search_req->handle->status != LDB_ERR_NO_SUCH_OBJECT) {
198                         handle->status = ac->search_req->handle->status;
199                         goto done;
200                 }
201
202                 return subtree_delete_check_for_children(ac);
203
204         case SD_DO_DEL:
205                 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
206
207                 if (ret != LDB_SUCCESS) {
208                         handle->status = ret;
209                         goto done;
210                 }
211                 if (ac->down_req->handle->status != LDB_SUCCESS) {
212                         handle->status = ac->down_req->handle->status;
213                         goto done;
214                 }
215
216                 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
217                         return LDB_SUCCESS;
218                 }
219
220                 break;
221         }
222 done:
223         handle->state = LDB_ASYNC_DONE;
224         return ret;
225 }
226
227 static int subtree_delete_wait_all(struct ldb_handle *handle) {
228
229         int ret;
230
231         while (handle->state != LDB_ASYNC_DONE) {
232                 ret = subtree_delete_wait_none(handle);
233                 if (ret != LDB_SUCCESS) {
234                         return ret;
235                 }
236         }
237
238         return handle->status;
239 }
240
241 static int subtree_delete_wait(struct ldb_handle *handle, enum ldb_wait_type type)
242 {
243         if (type == LDB_WAIT_ALL) {
244                 return subtree_delete_wait_all(handle);
245         } else {
246                 return subtree_delete_wait_none(handle);
247         }
248 }
249
250 const struct ldb_module_ops ldb_subtree_delete_module_ops = {
251         .name              = "subtree_delete",
252         .del               = subtree_delete,
253         .wait              = subtree_delete_wait,
254 };