LDB ASYNC: samba4 modules
[Samba/vfs_proxy.git] / source4 / dsdb / samdb / ldb_modules / show_deleted.c
blob0e3b46debee4354131f109c3e75d19f2e53296aa
1 /*
2 ldb database library
4 Copyright (C) Simo Sorce 2005
5 Copyright (C) Stefa Metzmacher <metze@samba.org> 2007
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
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.
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.
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/>.
26 * Name: ldb
28 * Component: ldb deleted objects control module
30 * Description: this module hides deleted objects, and returns them if the right control is there
32 * Author: Stefan Metzmacher
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include "dsdb/samdb/samdb.h"
41 /* search */
42 struct show_deleted_search_request {
44 struct ldb_module *module;
45 struct ldb_request *req;
48 static int show_deleted_search_callback(struct ldb_request *req,
49 struct ldb_reply *ares)
51 struct show_deleted_search_request *ar;
53 ar = talloc_get_type(req->context, struct show_deleted_search_request);
55 if (!ares) {
56 return ldb_module_done(ar->req, NULL, NULL,
57 LDB_ERR_OPERATIONS_ERROR);
59 if (ares->error != LDB_SUCCESS) {
60 return ldb_module_done(ar->req, ares->controls,
61 ares->response, ares->error);
64 switch (ares->type) {
65 case LDB_REPLY_ENTRY:
67 return ldb_module_send_entry(ar->req, ares->message);
69 case LDB_REPLY_REFERRAL:
70 return ldb_module_send_referral(ar->req, ares->referral);
72 case LDB_REPLY_DONE:
73 return ldb_module_done(ar->req, ares->controls,
74 ares->response, LDB_SUCCESS);
77 return LDB_SUCCESS;
80 static int show_deleted_search(struct ldb_module *module, struct ldb_request *req)
82 struct ldb_control *control;
83 struct ldb_control **saved_controls;
84 struct show_deleted_search_request *ar;
85 struct ldb_request *down_req;
86 char *old_filter;
87 char *new_filter;
88 int ret;
90 ar = talloc_zero(req, struct show_deleted_search_request);
91 if (ar == NULL) {
92 return LDB_ERR_OPERATIONS_ERROR;
94 ar->module = module;
95 ar->req = req;
97 /* check if there's a show deleted control */
98 control = ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID);
100 if ( ! control) {
101 old_filter = ldb_filter_from_tree(ar, req->op.search.tree);
102 new_filter = talloc_asprintf(ar, "(&(!(isDeleted=TRUE))%s)",
103 old_filter);
105 ret = ldb_build_search_req(&down_req, module->ldb, ar,
106 req->op.search.base,
107 req->op.search.scope,
108 new_filter,
109 req->op.search.attrs,
110 req->controls,
111 ar, show_deleted_search_callback,
112 req);
114 } else {
115 ret = ldb_build_search_req_ex(&down_req, module->ldb, ar,
116 req->op.search.base,
117 req->op.search.scope,
118 req->op.search.tree,
119 req->op.search.attrs,
120 req->controls,
121 ar, show_deleted_search_callback,
122 req);
124 if (ret != LDB_SUCCESS) {
125 return ret;
128 /* if a control is there remove if from the modified request */
129 if (control && !save_controls(control, down_req, &saved_controls)) {
130 return LDB_ERR_OPERATIONS_ERROR;
133 /* perform the search */
134 return ldb_next_request(module, down_req);
137 static int show_deleted_init(struct ldb_module *module)
139 int ret;
141 ret = ldb_mod_register_control(module, LDB_CONTROL_SHOW_DELETED_OID);
142 if (ret != LDB_SUCCESS) {
143 ldb_debug(module->ldb, LDB_DEBUG_ERROR,
144 "extended_dn: Unable to register control with rootdse!\n");
145 return LDB_ERR_OPERATIONS_ERROR;
148 return ldb_next_init(module);
151 _PUBLIC_ const struct ldb_module_ops ldb_show_deleted_module_ops = {
152 .name = "show_deleted",
153 .search = show_deleted_search,
154 .init_context = show_deleted_init