s4:dsdb Rework show_deleted module not to liniearise the LDAP filter
[samba.git] / source4 / dsdb / samdb / ldb_modules / show_deleted.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
7
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb deleted objects control module
30  *
31  *  Description: this module hides deleted objects, and returns them if the right control is there
32  *
33  *  Author: Stefan Metzmacher
34  */
35
36 #include "includes.h"
37 #include "ldb/include/ldb_module.h"
38 #include "dsdb/samdb/samdb.h"
39
40
41 static int show_deleted_search(struct ldb_module *module, struct ldb_request *req)
42 {
43         struct ldb_context *ldb;
44         struct ldb_control *control;
45         struct ldb_control **saved_controls;
46         struct ldb_request *down_req;
47         struct ldb_parse_tree *nodeleted_tree;
48         struct ldb_parse_tree *new_tree = req->op.search.tree;
49         int ret;
50
51         ldb = ldb_module_get_ctx(module);
52
53         /* check if there's a show deleted control */
54         control = ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID);
55
56         if (! control) {
57                 nodeleted_tree = talloc_get_type(ldb_module_get_private(module), 
58                                                  struct ldb_parse_tree);
59                 if (nodeleted_tree) {
60                         new_tree = talloc(req, struct ldb_parse_tree);
61                         if (!new_tree) {
62                                 ldb_oom(ldb);
63                                 return LDB_ERR_OPERATIONS_ERROR;
64                         }
65                         *new_tree = *nodeleted_tree;
66                         /* Replace dummy part of 'and' with the old, tree,
67                            without a parse step */
68                         new_tree->u.list.elements[0] = req->op.search.tree;
69                 }
70         }
71         
72         ret = ldb_build_search_req_ex(&down_req, ldb, req,
73                                       req->op.search.base,
74                                       req->op.search.scope,
75                                       new_tree,
76                                       req->op.search.attrs,
77                                       req->controls,
78                                       req->context, req->callback,
79                                       req);
80         if (ret != LDB_SUCCESS) {
81                 return ret;
82         }
83
84         /* if a control is there remove if from the modified request */
85         if (control && !save_controls(control, down_req, &saved_controls)) {
86                 return LDB_ERR_OPERATIONS_ERROR;
87         }
88
89         /* perform the search */
90         return ldb_next_request(module, down_req);
91 }
92
93 static int show_deleted_init(struct ldb_module *module)
94 {
95         struct ldb_context *ldb;
96         struct ldb_parse_tree *nodeleted_tree;
97         int ret;
98
99         ldb = ldb_module_get_ctx(module);
100
101         nodeleted_tree = ldb_parse_tree(module, "(&(replace=me)(!(isDeleted=TRUE)))");
102         if (!nodeleted_tree) {
103                 ldb_debug(ldb, LDB_DEBUG_ERROR,
104                         "show_deleted: Unable to parse isDeleted master expression!\n");
105                 return LDB_ERR_OPERATIONS_ERROR;
106         }
107
108         ldb_module_set_private(module, nodeleted_tree);
109
110         ret = ldb_mod_register_control(module, LDB_CONTROL_SHOW_DELETED_OID);
111         if (ret != LDB_SUCCESS) {
112                 ldb_debug(ldb, LDB_DEBUG_ERROR,
113                         "show_deleted: Unable to register control with rootdse!\n");
114                 return LDB_ERR_OPERATIONS_ERROR;
115         }
116
117         return ldb_next_init(module);
118 }
119
120 _PUBLIC_ const struct ldb_module_ops ldb_show_deleted_module_ops = {
121         .name              = "show_deleted",
122         .search            = show_deleted_search,
123         .init_context      = show_deleted_init
124 };