d619558c214e5119ea06df45c44493f2b9fdb18b
[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) Stefa Metzmacher <metze@samba.org> 2007
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 3 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb deleted objects control module
29  *
30  *  Description: this module hides deleted objects, and returns them if the right control is there
31  *
32  *  Author: Stefan Metzmacher
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb_module.h"
37 #include "dsdb/samdb/samdb.h"
38
39 /* search */
40 struct show_deleted_search_request {
41
42         struct ldb_module *module;
43         struct ldb_request *req;
44 };
45
46 static int show_deleted_search_callback(struct ldb_request *req,
47                                         struct ldb_reply *ares)
48 {
49         struct show_deleted_search_request *ar;
50
51         ar = talloc_get_type(req->context, struct show_deleted_search_request);
52
53         if (!ares) {
54                 return ldb_module_done(ar->req, NULL, NULL,
55                                         LDB_ERR_OPERATIONS_ERROR);
56         }
57         if (ares->error != LDB_SUCCESS) {
58                 return ldb_module_done(ar->req, ares->controls,
59                                         ares->response, ares->error);
60         }
61
62         switch (ares->type) {
63         case LDB_REPLY_ENTRY:
64
65                 return ldb_module_send_entry(ar->req, ares->message, ares->controls);
66
67         case LDB_REPLY_REFERRAL:
68                 return ldb_module_send_referral(ar->req, ares->referral);
69
70         case LDB_REPLY_DONE:
71                 return ldb_module_done(ar->req, ares->controls,
72                                         ares->response, LDB_SUCCESS);
73
74         }
75         return LDB_SUCCESS;
76 }
77
78 static int show_deleted_search(struct ldb_module *module, struct ldb_request *req)
79 {
80         struct ldb_context *ldb;
81         struct ldb_control *control;
82         struct ldb_control **saved_controls;
83         struct show_deleted_search_request *ar;
84         struct ldb_request *down_req;
85         char *old_filter;
86         char *new_filter;
87         int ret;
88
89         ldb = ldb_module_get_ctx(module);
90
91         ar = talloc_zero(req, struct show_deleted_search_request);
92         if (ar == NULL) {
93                 return LDB_ERR_OPERATIONS_ERROR;
94         }
95         ar->module = module;
96         ar->req = req;
97
98         /* check if there's a show deleted control */
99         control = ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID);
100
101         if ( ! control) {
102                 old_filter = ldb_filter_from_tree(ar, req->op.search.tree);
103                 new_filter = talloc_asprintf(ar, "(&(!(isDeleted=TRUE))%s)",
104                                                  old_filter);
105
106                 ret = ldb_build_search_req(&down_req, ldb, ar,
107                                            req->op.search.base,
108                                            req->op.search.scope,
109                                            new_filter,
110                                            req->op.search.attrs,
111                                            req->controls,
112                                            ar, show_deleted_search_callback,
113                                            req);
114
115         } else {
116                 ret = ldb_build_search_req_ex(&down_req, ldb, ar,
117                                               req->op.search.base,
118                                               req->op.search.scope,
119                                               req->op.search.tree,
120                                               req->op.search.attrs,
121                                               req->controls,
122                                               ar, show_deleted_search_callback,
123                                               req);
124         }
125         if (ret != LDB_SUCCESS) {
126                 return ret;
127         }
128
129         /* if a control is there remove if from the modified request */
130         if (control && !save_controls(control, down_req, &saved_controls)) {
131                 return LDB_ERR_OPERATIONS_ERROR;
132         }
133
134         /* perform the search */
135         return ldb_next_request(module, down_req);
136 }
137
138 static int show_deleted_init(struct ldb_module *module)
139 {
140         struct ldb_context *ldb;
141         int ret;
142
143         ldb = ldb_module_get_ctx(module);
144
145         ret = ldb_mod_register_control(module, LDB_CONTROL_SHOW_DELETED_OID);
146         if (ret != LDB_SUCCESS) {
147                 ldb_debug(ldb, LDB_DEBUG_ERROR,
148                         "extended_dn: Unable to register control with rootdse!\n");
149                 return LDB_ERR_OPERATIONS_ERROR;
150         }
151
152         return ldb_next_init(module);
153 }
154
155 _PUBLIC_ const struct ldb_module_ops ldb_show_deleted_module_ops = {
156         .name              = "show_deleted",
157         .search            = show_deleted_search,
158         .init_context      = show_deleted_init
159 };