Return per-entry controls in ldb_module_send_entry()
[abartlet/samba.git/.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.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include "dsdb/samdb/samdb.h"
40
41 /* search */
42 struct show_deleted_search_request {
43
44         struct ldb_module *module;
45         struct ldb_request *req;
46 };
47
48 static int show_deleted_search_callback(struct ldb_request *req,
49                                         struct ldb_reply *ares)
50 {
51         struct show_deleted_search_request *ar;
52
53         ar = talloc_get_type(req->context, struct show_deleted_search_request);
54
55         if (!ares) {
56                 return ldb_module_done(ar->req, NULL, NULL,
57                                         LDB_ERR_OPERATIONS_ERROR);
58         }
59         if (ares->error != LDB_SUCCESS) {
60                 return ldb_module_done(ar->req, ares->controls,
61                                         ares->response, ares->error);
62         }
63
64         switch (ares->type) {
65         case LDB_REPLY_ENTRY:
66
67                 return ldb_module_send_entry(ar->req, ares->message, ares->controls);
68
69         case LDB_REPLY_REFERRAL:
70                 return ldb_module_send_referral(ar->req, ares->referral);
71
72         case LDB_REPLY_DONE:
73                 return ldb_module_done(ar->req, ares->controls,
74                                         ares->response, LDB_SUCCESS);
75
76         }
77         return LDB_SUCCESS;
78 }
79
80 static int show_deleted_search(struct ldb_module *module, struct ldb_request *req)
81 {
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;
89
90         ar = talloc_zero(req, struct show_deleted_search_request);
91         if (ar == NULL) {
92                 return LDB_ERR_OPERATIONS_ERROR;
93         }
94         ar->module = module;
95         ar->req = req;
96
97         /* check if there's a show deleted control */
98         control = ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID);
99
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);
104
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);
113
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);
123         }
124         if (ret != LDB_SUCCESS) {
125                 return ret;
126         }
127
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;
131         }
132
133         /* perform the search */
134         return ldb_next_request(module, down_req);
135 }
136
137 static int show_deleted_init(struct ldb_module *module)
138 {
139         int ret;
140
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;
146         }
147
148         return ldb_next_init(module);
149 }
150
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
155 };