042d26b981e5fd43e5d7d5ec57492c4575ff3474
[kamenim/samba.git] / source4 / dsdb / samdb / ldb_modules / objectclass_attrs.c
1 /*
2    ldb database library
3
4    Copyright (C) Simo Sorce  2006-2008
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
6    Copyright (C) Stefan Metzmacher 2009
7    Copyright (C) Matthias Dieter Wallnöfer 2010
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 Lesser General Public
20    License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /*
24  *  Name: ldb
25  *
26  *  Component: objectclass attribute checking module
27  *
28  *  Description: this checks the attributes on a directory entry (if they're
29  *    allowed, if the syntax is correct, if mandatory ones are missing,
30  *    denies the deletion of mandatory ones...). The module contains portions
31  *    of the "objectclass" and the "validate_update" LDB module.
32  *
33  *  Author: Matthias Dieter Wallnöfer
34  */
35
36 #include "includes.h"
37 #include "ldb_module.h"
38 #include "dsdb/samdb/samdb.h"
39
40 struct oc_context {
41
42         struct ldb_module *module;
43         struct ldb_request *req;
44         const struct dsdb_schema *schema;
45
46         struct ldb_reply *search_res;
47         struct ldb_reply *mod_ares;
48 };
49
50 static struct oc_context *oc_init_context(struct ldb_module *module,
51                                           struct ldb_request *req)
52 {
53         struct ldb_context *ldb;
54         struct oc_context *ac;
55
56         ldb = ldb_module_get_ctx(module);
57
58         ac = talloc_zero(req, struct oc_context);
59         if (ac == NULL) {
60                 ldb_oom(ldb);
61                 return NULL;
62         }
63
64         ac->module = module;
65         ac->req = req;
66         ac->schema = dsdb_get_schema(ldb, ac);
67
68         return ac;
69 }
70
71 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares);
72
73 static int attr_handler(struct oc_context *ac)
74 {
75         struct ldb_context *ldb;
76         struct ldb_message *msg;
77         struct ldb_request *child_req;
78         const struct dsdb_attribute *attr;
79         unsigned int i;
80         int ret;
81         WERROR werr;
82
83         ldb = ldb_module_get_ctx(ac->module);
84
85         if (ac->req->operation == LDB_ADD) {
86                 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
87         } else {
88                 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
89         }
90         if (msg == NULL) {
91                 ldb_oom(ldb);
92                 return LDB_ERR_OPERATIONS_ERROR;
93         }
94
95         /* Check if attributes exist in the schema, if the values match,
96          * if they're not operational and fix the names to the match the schema
97          * case */
98         for (i = 0; i < msg->num_elements; i++) {
99                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
100                                                          msg->elements[i].name);
101                 if (attr == NULL) {
102                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' was not found in the schema!",
103                                                msg->elements[i].name,
104                                                ldb_dn_get_linearized(msg->dn));
105                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
106                 }
107
108                 werr = attr->syntax->validate_ldb(ldb, ac->schema, attr,
109                                                   &msg->elements[i]);
110                 if (!W_ERROR_IS_OK(werr)) {
111                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
112                                                msg->elements[i].name,
113                                                ldb_dn_get_linearized(msg->dn));
114                         return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
115                 }
116
117                 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
118                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
119                                                msg->elements[i].name,
120                                                ldb_dn_get_linearized(msg->dn));
121                         if (ac->req->operation == LDB_ADD) {
122                                 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
123                         } else {
124                                 return LDB_ERR_CONSTRAINT_VIOLATION;
125                         }
126                 }
127
128                 /* subsitute the attribute name to match in case */
129                 msg->elements[i].name = attr->lDAPDisplayName;
130         }
131
132         if (ac->req->operation == LDB_ADD) {
133                 ret = ldb_build_add_req(&child_req, ldb, ac,
134                                         msg, ac->req->controls,
135                                         ac, oc_op_callback, ac->req);
136         } else {
137                 ret = ldb_build_mod_req(&child_req, ldb, ac,
138                                         msg, ac->req->controls,
139                                         ac, oc_op_callback, ac->req);
140         }
141         if (ret != LDB_SUCCESS) {
142                 return ret;
143         }
144
145         return ldb_next_request(ac->module, child_req);
146 }
147
148 static int attr_handler2(struct oc_context *ac)
149 {
150         struct ldb_context *ldb;
151         struct ldb_message_element *oc_element;
152         struct ldb_message *msg;
153         const char **must_contain, **may_contain, **found_must_contain;
154         const struct dsdb_attribute *attr;
155         unsigned int i;
156         bool found;
157
158         ldb = ldb_module_get_ctx(ac->module);
159
160         if (ac->search_res == NULL) {
161                 return LDB_ERR_OPERATIONS_ERROR;
162         }
163
164         /* We rely here on the preceeding "objectclass" LDB module which did
165          * already fix up the objectclass list (inheritance, order...). */
166         oc_element = ldb_msg_find_element(ac->search_res->message,
167                                           "objectClass");
168         if (oc_element == NULL) {
169                 return LDB_ERR_OPERATIONS_ERROR;
170         }
171
172         must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
173                                                 DSDB_SCHEMA_ALL_MUST);
174         may_contain =  dsdb_full_attribute_list(ac, ac->schema, oc_element,
175                                                 DSDB_SCHEMA_ALL_MAY);
176         found_must_contain = const_str_list(str_list_copy(ac, must_contain));
177         if ((must_contain == NULL) || (may_contain == NULL)
178             || (found_must_contain == NULL)) {
179                 return LDB_ERR_OPERATIONS_ERROR;
180         }
181
182         /* Check if all specified attributes are valid in the given
183          * objectclasses. */
184         msg = ac->search_res->message;
185         for (i = 0; i < msg->num_elements; i++) {
186                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
187                                                          msg->elements[i].name);
188                 if (attr == NULL) {
189                         return LDB_ERR_OPERATIONS_ERROR;
190                 }
191
192                 /* We can use "str_list_check" with "strcmp" here since the
193                  * attribute informations from the schema are always equal
194                  * up-down-cased. */
195                 found = str_list_check(must_contain, attr->lDAPDisplayName);
196                 if (found) {
197                         str_list_remove(found_must_contain, attr->lDAPDisplayName);
198                 } else {
199                         found = str_list_check(may_contain, attr->lDAPDisplayName);
200                 }
201                 if (!found) {
202                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
203                                                msg->elements[i].name,
204                                                ldb_dn_get_linearized(msg->dn));
205                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
206                 }
207         }
208
209         if (found_must_contain[0] != NULL) {
210                 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
211                                        found_must_contain[0],
212                                        ldb_dn_get_linearized(msg->dn));
213                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
214         }
215
216         return ldb_module_done(ac->req, ac->mod_ares->controls,
217                                ac->mod_ares->response, LDB_SUCCESS);
218 }
219
220 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
221 {
222         struct ldb_context *ldb;
223         struct oc_context *ac;
224         int ret;
225
226         ac = talloc_get_type(req->context, struct oc_context);
227         ldb = ldb_module_get_ctx(ac->module);
228
229         if (!ares) {
230                 return ldb_module_done(ac->req, NULL, NULL,
231                                        LDB_ERR_OPERATIONS_ERROR);
232         }
233         if (ares->error != LDB_SUCCESS) {
234                 return ldb_module_done(ac->req, ares->controls,
235                                        ares->response, ares->error);
236         }
237
238         ldb_reset_err_string(ldb);
239
240         switch (ares->type) {
241         case LDB_REPLY_ENTRY:
242                 if (ac->search_res != NULL) {
243                         ldb_set_errstring(ldb, "Too many results");
244                         talloc_free(ares);
245                         return ldb_module_done(ac->req, NULL, NULL,
246                                                LDB_ERR_OPERATIONS_ERROR);
247                 }
248
249                 ac->search_res = talloc_steal(ac, ares);
250                 break;
251
252         case LDB_REPLY_REFERRAL:
253                 /* ignore */
254                 talloc_free(ares);
255                 break;
256
257         case LDB_REPLY_DONE:
258                 talloc_free(ares);
259                 ret = attr_handler2(ac);
260                 if (ret != LDB_SUCCESS) {
261                         return ldb_module_done(ac->req, NULL, NULL, ret);
262                 }
263                 break;
264         }
265
266         return LDB_SUCCESS;
267 }
268
269 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
270 {
271         struct oc_context *ac;
272         struct ldb_context *ldb;
273         struct ldb_request *search_req;
274         struct ldb_dn *base_dn;
275         int ret;
276
277         ac = talloc_get_type(req->context, struct oc_context);
278         ldb = ldb_module_get_ctx(ac->module);
279
280         if (!ares) {
281                 return ldb_module_done(ac->req, NULL, NULL,
282                                        LDB_ERR_OPERATIONS_ERROR);
283         }
284
285         if (ares->type == LDB_REPLY_REFERRAL) {
286                 return ldb_module_send_referral(ac->req, ares->referral);
287         }
288
289         if (ares->error != LDB_SUCCESS) {
290                 return ldb_module_done(ac->req, ares->controls, ares->response,
291                                        ares->error);
292         }
293
294         if (ares->type != LDB_REPLY_DONE) {
295                 talloc_free(ares);
296                 return ldb_module_done(ac->req, NULL, NULL,
297                                        LDB_ERR_OPERATIONS_ERROR);
298         }
299
300         ac->search_res = NULL;
301         ac->mod_ares = talloc_steal(ac, ares);
302
303         /* This looks up all attributes of our just added/modified entry */
304         base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
305                 : ac->req->op.mod.message->dn;
306         ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
307                                    LDB_SCOPE_BASE, "(objectClass=*)",
308                                    NULL, NULL, ac,
309                                    get_search_callback, ac->req);
310         if (ret != LDB_SUCCESS) {
311                 return ldb_module_done(ac->req, NULL, NULL, ret);
312         }
313
314         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID,
315                                       true, NULL);
316         if (ret != LDB_SUCCESS) {
317                 return ldb_module_done(ac->req, NULL, NULL, ret);
318         }
319
320         ret = ldb_next_request(ac->module, search_req);
321         if (ret != LDB_SUCCESS) {
322                 return ldb_module_done(ac->req, NULL, NULL, ret);
323         }
324
325         /* "ldb_module_done" isn't called here since we need to do additional
326          * checks. It is called at the end of "attr_handler2". */
327         return LDB_SUCCESS;
328 }
329
330 static int objectclass_attrs_add(struct ldb_module *module,
331                                  struct ldb_request *req)
332 {
333         struct ldb_context *ldb;
334         struct oc_context *ac;
335
336         ldb = ldb_module_get_ctx(module);
337
338         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
339
340         /* do not manipulate our control entries */
341         if (ldb_dn_is_special(req->op.add.message->dn)) {
342                 return ldb_next_request(module, req);
343         }
344
345         ac = oc_init_context(module, req);
346         if (ac == NULL) {
347                 return LDB_ERR_OPERATIONS_ERROR;
348         }
349
350         /* without schema, there isn't much to do here */
351         if (ac->schema == NULL) {
352                 talloc_free(ac);
353                 return ldb_next_request(module, req);
354         }
355
356         return attr_handler(ac);
357 }
358
359 static int objectclass_attrs_modify(struct ldb_module *module,
360                                     struct ldb_request *req)
361 {
362         struct ldb_context *ldb;
363         struct oc_context *ac;
364
365         ldb = ldb_module_get_ctx(module);
366
367         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
368
369         /* do not manipulate our control entries */
370         if (ldb_dn_is_special(req->op.mod.message->dn)) {
371                 return ldb_next_request(module, req);
372         }
373
374         ac = oc_init_context(module, req);
375         if (ac == NULL) {
376                 return LDB_ERR_OPERATIONS_ERROR;
377         }
378
379         /* without schema, there isn't much to do here */
380         if (ac->schema == NULL) {
381                 talloc_free(ac);
382                 return ldb_next_request(module, req);
383         }
384
385         return attr_handler(ac);
386 }
387
388 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
389         .name              = "objectclass_attrs",
390         .add               = objectclass_attrs_add,
391         .modify            = objectclass_attrs_modify
392 };