s4-dsdb: use ldb_operr() in the dsdb code
[jlayton/samba.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) Andrew Tridgell <tridge@samba.org> 2009
6    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
7    Copyright (C) Simo Sorce <idra@samba.org> 2008
8    Copyright (C) Matthias Dieter Wallnöfer 2010
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb subtree delete module
28  *
29  *  Description: Delete of a subtree in LDB
30  *
31  *  Author: Andrew Bartlett
32  */
33
34 #include "includes.h"
35 #include <ldb.h>
36 #include <ldb_module.h>
37 #include "dsdb/samdb/ldb_modules/util.h"
38
39
40 static int subtree_delete(struct ldb_module *module, struct ldb_request *req)
41 {
42         static const char * const attrs[] = { NULL };
43         struct ldb_result *res = NULL;
44         uint32_t flags;
45         unsigned int i;
46         int ret;
47
48         if (ldb_dn_is_special(req->op.del.dn)) {
49                 /* do not manipulate our control entries */
50                 return ldb_next_request(module, req);
51         }
52
53         /* see if we have any children */
54         ret = dsdb_module_search(module, req, &res, req->op.del.dn,
55                                  LDB_SCOPE_ONELEVEL, attrs,
56                                  DSDB_FLAG_NEXT_MODULE |
57                                  DSDB_SEARCH_SHOW_DELETED,
58                                  "(objectClass=*)");
59         if (ret != LDB_SUCCESS) {
60                 talloc_free(res);
61                 return ret;
62         }
63         if (res->count > 0) {
64                 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID) == NULL) {
65                         ldb_asprintf_errstring(ldb_module_get_ctx(module),
66                                                "Cannot delete %s, not a leaf node "
67                                                "(has %d children)\n",
68                                                ldb_dn_get_linearized(req->op.del.dn),
69                                                res->count);
70                         talloc_free(res);
71                         return LDB_ERR_NOT_ALLOWED_ON_NON_LEAF;
72                 }
73
74                 /* we need to start from the top since other LDB modules could
75                  * enforce constraints (eg "objectclass" and "samldb" do so). */
76                 flags = DSDB_FLAG_TOP_MODULE | DSDB_TREE_DELETE;
77                 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) != NULL) {
78                         flags |= DSDB_MODIFY_RELAX;
79                 }
80
81                 for (i = 0; i < res->count; i++) {
82                         ret = dsdb_module_del(module, res->msgs[i]->dn, flags);
83                         if (ret != LDB_SUCCESS) {
84                                 return ret;
85                         }
86                 }
87         }
88         talloc_free(res);
89
90         return ldb_next_request(module, req);
91 }
92
93 static int subtree_delete_init(struct ldb_module *module)
94 {
95         struct ldb_context *ldb;
96         int ret;
97
98         ldb = ldb_module_get_ctx(module);
99
100         ret = ldb_mod_register_control(module, LDB_CONTROL_TREE_DELETE_OID);
101         if (ret != LDB_SUCCESS) {
102                 ldb_debug(ldb, LDB_DEBUG_ERROR,
103                         "subtree_delete: Unable to register control with rootdse!\n");
104                 return ldb_operr(ldb);
105         }
106
107         return ldb_next_init(module);
108 }
109
110 _PUBLIC_ const struct ldb_module_ops ldb_subtree_delete_module_ops = {
111         .name              = "subtree_delete",
112         .init_context      = subtree_delete_init,
113         .del               = subtree_delete
114 };