s4-ldb: fixed request handling for schemaUpdateNow op
[ira/wip.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    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 deleted objects control module
26  *
27  *  Description: this module hides deleted objects, and returns them if the right control is there
28  *
29  *  Author: Stefan Metzmacher
30  */
31
32 #include "includes.h"
33 #include "ldb/include/ldb_module.h"
34 #include "dsdb/samdb/samdb.h"
35
36
37 static int show_deleted_search(struct ldb_module *module, struct ldb_request *req)
38 {
39         struct ldb_context *ldb;
40         struct ldb_control *control;
41         struct ldb_control **saved_controls;
42         struct ldb_request *down_req;
43         struct ldb_parse_tree *nodeleted_tree;
44         struct ldb_parse_tree *new_tree = req->op.search.tree;
45         int ret;
46
47         ldb = ldb_module_get_ctx(module);
48
49         /* check if there's a show deleted control */
50         control = ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID);
51
52         if (! control) {
53                 nodeleted_tree = talloc_get_type(ldb_module_get_private(module), 
54                                                  struct ldb_parse_tree);
55                 if (nodeleted_tree) {
56                         new_tree = talloc(req, struct ldb_parse_tree);
57                         if (!new_tree) {
58                                 ldb_oom(ldb);
59                                 return LDB_ERR_OPERATIONS_ERROR;
60                         }
61                         *new_tree = *nodeleted_tree;
62                         /* Replace dummy part of 'and' with the old, tree,
63                            without a parse step */
64                         new_tree->u.list.elements[0] = req->op.search.tree;
65                 }
66         }
67         
68         ret = ldb_build_search_req_ex(&down_req, ldb, req,
69                                       req->op.search.base,
70                                       req->op.search.scope,
71                                       new_tree,
72                                       req->op.search.attrs,
73                                       req->controls,
74                                       req->context, req->callback,
75                                       req);
76         if (ret != LDB_SUCCESS) {
77                 return ret;
78         }
79
80         /* if a control is there remove if from the modified request */
81         if (control && !save_controls(control, down_req, &saved_controls)) {
82                 return LDB_ERR_OPERATIONS_ERROR;
83         }
84
85         /* perform the search */
86         return ldb_next_request(module, down_req);
87 }
88
89 static int show_deleted_init(struct ldb_module *module)
90 {
91         struct ldb_context *ldb;
92         struct ldb_parse_tree *nodeleted_tree;
93         int ret;
94
95         ldb = ldb_module_get_ctx(module);
96
97         nodeleted_tree = ldb_parse_tree(module, "(&(replace=me)(!(isDeleted=TRUE)))");
98         if (!nodeleted_tree) {
99                 ldb_debug(ldb, LDB_DEBUG_ERROR,
100                         "show_deleted: Unable to parse isDeleted master expression!\n");
101                 return LDB_ERR_OPERATIONS_ERROR;
102         }
103
104         ldb_module_set_private(module, nodeleted_tree);
105
106         ret = ldb_mod_register_control(module, LDB_CONTROL_SHOW_DELETED_OID);
107         if (ret != LDB_SUCCESS) {
108                 ldb_debug(ldb, LDB_DEBUG_ERROR,
109                         "show_deleted: Unable to register control with rootdse!\n");
110                 return LDB_ERR_OPERATIONS_ERROR;
111         }
112
113         return ldb_next_init(module);
114 }
115
116 _PUBLIC_ const struct ldb_module_ops ldb_show_deleted_module_ops = {
117         .name              = "show_deleted",
118         .search            = show_deleted_search,
119         .init_context      = show_deleted_init
120 };