f71a8d39480f7f6c60e55c11b6544b28b3c77d5a
[kamenim/samba.git] / source4 / dsdb / samdb / ldb_modules / instancetype.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004-2008
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /*
24  *  Name: ldb
25  *
26  *  Component: ldb instancetype module
27  *
28  *  Description: add an instanceType onto every new record
29  *
30  *  Author: Andrew Bartlett
31  */
32
33 #include "includes.h"
34 #include "ldb.h"
35 #include "ldb_module.h"
36 #include "librpc/gen_ndr/ndr_misc.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "../libds/common/flags.h"
39 #include "dsdb/samdb/ldb_modules/util.h"
40
41 struct it_context {
42         struct ldb_module *module;
43         struct ldb_request *req;
44         struct ldb_request *add_req;
45 };
46
47 static int it_add_callback(struct ldb_request *req, struct ldb_reply *ares)
48 {
49         struct ldb_context *ldb;
50         struct it_context *ac;
51
52         ac = talloc_get_type(req->context, struct it_context);
53         ldb = ldb_module_get_ctx(ac->module);
54
55         if (!ares) {
56                 return ldb_module_done(ac->req, NULL, NULL,
57                                         LDB_ERR_OPERATIONS_ERROR);
58         }
59
60         if (ares->type == LDB_REPLY_REFERRAL) {
61                 return ldb_module_send_referral(ac->req, ares->referral);
62         }
63
64         if (ares->error != LDB_SUCCESS) {
65                 return ldb_module_done(ac->req, ares->controls,
66                                         ares->response, ares->error);
67         }
68
69         if (ares->type != LDB_REPLY_DONE) {
70                 ldb_set_errstring(ldb, "Invalid reply type!");
71                 return ldb_module_done(ac->req, NULL, NULL,
72                                         LDB_ERR_OPERATIONS_ERROR);
73         }
74
75         /* Add the boilerplate entries */
76
77         return ldb_module_done(ac->req, ares->controls,
78                                ares->response, ares->error);
79 }
80
81 /* add_record: add instancetype attribute */
82 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
83 {
84         struct ldb_context *ldb;
85         struct ldb_request *down_req;
86         struct ldb_message *msg;
87         struct ldb_message_element *el;
88         struct it_context *ac;
89         uint32_t instance_type;
90         int ret;
91
92         ldb = ldb_module_get_ctx(module);
93
94         ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
95
96         /* do not manipulate our control entries */
97         if (ldb_dn_is_special(req->op.add.message->dn)) {
98                 return ldb_next_request(module, req);
99         }
100
101         el = ldb_msg_find_element(req->op.add.message, "instanceType");
102         if (el != NULL) {
103                 unsigned int instanceType;
104
105                 if (el->num_values != 1) {
106                         ldb_set_errstring(ldb, "instancetype: the 'instanceType' attribute is single-valued!");
107                         return LDB_ERR_UNWILLING_TO_PERFORM;
108                 }
109
110                 instanceType = ldb_msg_find_attr_as_uint(req->op.add.message,
111                                                          "instanceType", 0);
112                 if (!(instanceType & INSTANCE_TYPE_IS_NC_HEAD)) {
113                         return ldb_next_request(module, req);           
114                 }
115
116                 /* Forward the 'add' to the modules below, but if it
117                  * succeeds, then we might need to add the boilerplate
118                  * entries (lost+found, deleted objects) */
119                 ac = talloc(req, struct it_context);
120                 if (ac == NULL) {
121                         return LDB_ERR_OPERATIONS_ERROR;
122                 }
123                 ac->module = module;
124                 ac->req = req;
125                 
126                 ret = ldb_build_add_req(&ac->add_req, ldb_module_get_ctx(ac->module), ac,
127                                         ac->req->op.add.message,
128                                         ac->req->controls,
129                                         ac, it_add_callback,
130                                         ac->req);
131                 
132                 if (ret != LDB_SUCCESS) {
133                         return ret;
134                 }
135                 
136                 /* Do the original add */
137                 return ldb_next_request(ac->module, ac->add_req);
138         }
139
140         /* we have to copy the message as the caller might have it as a const */
141         msg = ldb_msg_copy_shallow(req, req->op.add.message);
142         if (msg == NULL) {
143                 ldb_oom(ldb);
144                 return LDB_ERR_OPERATIONS_ERROR;
145         }
146
147         /*
148          * TODO: calculate correct instance type
149          */
150         instance_type = INSTANCE_TYPE_WRITE;
151
152         ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
153         if (ret != LDB_SUCCESS) {
154                 ldb_oom(ldb);
155                 return LDB_ERR_OPERATIONS_ERROR;
156         }
157
158         ret = ldb_build_add_req(&down_req, ldb, req,
159                                 msg,
160                                 req->controls,
161                                 req, dsdb_next_callback,
162                                 req);
163         if (ret != LDB_SUCCESS) {
164                 return ret;
165         }
166
167         /* go on with the call chain */
168         return ldb_next_request(module, down_req);
169 }
170
171 /* deny instancetype modification */
172 static int instancetype_mod(struct ldb_module *module, struct ldb_request *req)
173 {
174         struct ldb_context *ldb = ldb_module_get_ctx(module);
175         struct ldb_message_element *el;
176
177         el = ldb_msg_find_element(req->op.mod.message, "instanceType");
178         if (el != NULL) {
179                 ldb_set_errstring(ldb, "instancetype: the 'instanceType' attribute can never be changed!");
180                 return LDB_ERR_CONSTRAINT_VIOLATION;
181         }
182
183         return ldb_next_request(module, req);
184 }
185
186 _PUBLIC_ const struct ldb_module_ops ldb_instancetype_module_ops = {
187         .name          = "instancetype",
188         .add           = instancetype_add,
189         .modify        = instancetype_mod
190 };