s4-dsdb: it is a better pattern to mark a control as done than remove it
[samba.git] / source4 / dsdb / samdb / ldb_modules / lazy_commit.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21  *  Name: ldb
22  *
23  *  Component: ldb lazy_commit module
24  *
25  *  Description: module to pretend to support the 'lazy commit' control
26  *
27  *  Author: Andrew Bartlett
28  */
29
30 #include "ldb_module.h"
31
32 static int unlazy_op(struct ldb_module *module, struct ldb_request *req)
33 {
34         int ret;
35         struct ldb_request *new_req;
36         struct ldb_control *control = ldb_request_get_control(req, LDB_CONTROL_SERVER_LAZY_COMMIT);
37         if (!control) {
38                 return ldb_next_request(module, req);
39         } 
40         
41         switch (req->operation) {
42         case LDB_SEARCH:
43                 ret = ldb_build_search_req_ex(&new_req, ldb_module_get_ctx(module),
44                                               req,
45                                               req->op.search.base,
46                                               req->op.search.scope,
47                                               req->op.search.tree,
48                                               req->op.search.attrs,
49                                               req->controls,
50                                               req->context, req->callback,
51                                               req);
52                 break;
53         case LDB_ADD:
54                 ret = ldb_build_add_req(&new_req, ldb_module_get_ctx(module), req,
55                                         req->op.add.message,
56                                         req->controls,
57                                         req->context, req->callback,
58                                         req);
59                 break;
60         case LDB_MODIFY:
61                 ret = ldb_build_mod_req(&new_req, ldb_module_get_ctx(module), req,
62                                         req->op.mod.message,
63                                         req->controls,
64                                         req->context, req->callback,
65                                         req);
66                 break;
67         case LDB_DELETE:
68                 ret = ldb_build_del_req(&new_req, ldb_module_get_ctx(module), req,
69                                         req->op.del.dn,
70                                         req->controls,
71                                         req->context, req->callback,
72                                         req);
73                 break;
74         case LDB_RENAME:
75                 ret = ldb_build_rename_req(&new_req, ldb_module_get_ctx(module), req,
76                                            req->op.rename.olddn,
77                                            req->op.rename.newdn,
78                                            req->controls,
79                                            req->context, req->callback,
80                                            req);
81                 break;
82         case LDB_EXTENDED:
83                 ret = ldb_build_extended_req(&new_req, ldb_module_get_ctx(module),
84                                              req,
85                                              req->op.extended.oid,
86                                              req->op.extended.data,
87                                              req->controls,
88                                              req->context, req->callback,
89                                              req);
90                 break;
91         default:
92                 ldb_set_errstring(ldb_module_get_ctx(module),
93                                   "Unsupported request type!");
94                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
95         }
96
97         if (ret != LDB_SUCCESS) {
98                 return ret;
99         }
100
101         control->critical = 0;
102         return ldb_next_request(module, new_req);
103 }
104
105 static int unlazy_init(struct ldb_module *module)
106 {
107         int ret;
108         struct ldb_context *ldb;
109         ldb = ldb_module_get_ctx(module);
110
111         ret = ldb_mod_register_control(module, LDB_CONTROL_SHOW_DELETED_OID);
112         if (ret != LDB_SUCCESS) {
113                 ldb_debug(ldb, LDB_DEBUG_ERROR,
114                         "lazy_commit: Unable to register control with rootdse!\n");
115                 return LDB_ERR_OPERATIONS_ERROR;
116         }
117
118         return ldb_next_init(module);
119 }
120
121 const struct ldb_module_ops ldb_lazy_commit_module_ops = {
122         .name              = "lazy_commit",
123         .search            = unlazy_op,
124         .add               = unlazy_op,
125         .modify            = unlazy_op,
126         .del               = unlazy_op,
127         .rename            = unlazy_op,
128         .request           = unlazy_op,
129         .extended          = unlazy_op,
130         .init_context      = unlazy_init,
131 };