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
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.
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.
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/>.
27 * Component: ldb subtree delete module
29 * Description: Delete of a subtree in LDB
31 * Author: Andrew Bartlett
36 #include <ldb_module.h>
37 #include "dsdb/samdb/ldb_modules/util.h"
38 #include "dsdb/common/util.h"
41 static int subtree_delete(struct ldb_module
*module
, struct ldb_request
*req
)
43 static const char * const attrs
[] = { NULL
};
44 struct ldb_result
*res
= NULL
;
49 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
50 /* do not manipulate our control entries */
51 return ldb_next_request(module
, req
);
54 /* see if we have any children */
55 ret
= dsdb_module_search(module
, req
, &res
, req
->op
.del
.dn
,
56 LDB_SCOPE_ONELEVEL
, attrs
,
57 DSDB_FLAG_NEXT_MODULE
,
60 if (ret
!= LDB_SUCCESS
) {
65 if (ldb_request_get_control(req
, LDB_CONTROL_TREE_DELETE_OID
) == NULL
) {
66 /* Do not add any DN outputs to this error string!
67 * Some MMC consoles (eg release 2000) have a strange
68 * bug and prevent subtree deletes afterwards. */
69 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
70 "subtree_delete: Unable to "
71 "delete a non-leaf node "
72 "(it has %u children)!",
75 return LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
;
78 /* we need to start from the top since other LDB modules could
79 * enforce constraints (eg "objectclass" and "samldb" do so). */
80 flags
= DSDB_FLAG_TOP_MODULE
| DSDB_TREE_DELETE
;
81 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
) != NULL
) {
82 flags
|= DSDB_MODIFY_RELAX
;
85 for (i
= 0; i
< res
->count
; i
++) {
86 ret
= dsdb_module_del(module
, res
->msgs
[i
]->dn
, flags
, req
);
87 if (ret
!= LDB_SUCCESS
) {
94 return ldb_next_request(module
, req
);
97 static int subtree_delete_init(struct ldb_module
*module
)
99 struct ldb_context
*ldb
;
102 ldb
= ldb_module_get_ctx(module
);
104 ret
= ldb_mod_register_control(module
, LDB_CONTROL_TREE_DELETE_OID
);
105 if (ret
!= LDB_SUCCESS
) {
106 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
107 "subtree_delete: Unable to register control with rootdse!\n");
108 return ldb_operr(ldb
);
111 return ldb_next_init(module
);
114 static const struct ldb_module_ops ldb_subtree_delete_module_ops
= {
115 .name
= "subtree_delete",
116 .init_context
= subtree_delete_init
,
117 .del
= subtree_delete
120 int ldb_subtree_delete_module_init(const char *version
)
122 LDB_MODULE_CHECK_VERSION(version
);
123 return ldb_register_module(&ldb_subtree_delete_module_ops
);