s4-dsdb: Implemented value restrictions for the dSHeuristcs attribute
[nivanova/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 /* checks correctness of dSHeuristics attribute
74  * as described in MS-ADTS 7.1.1.2.4.1.2 dSHeuristics */
75
76 static int oc_validate_dsheuristics(struct ldb_message_element *el)
77 {
78         if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE ||
79             el->num_values < 1) {
80                 return LDB_SUCCESS;
81         }
82         if (el->values[0].length > DS_HR_LDAP_BYPASS_UPPER_LIMIT_BOUNDS) {
83                 return LDB_ERR_CONSTRAINT_VIOLATION;
84         } else if (el->values[0].length >= DS_HR_TENTH_CHAR
85                    && el->values[0].data[DS_HR_TENTH_CHAR-1] != '1') {
86                 return LDB_ERR_CONSTRAINT_VIOLATION;
87         } else {
88                 return LDB_SUCCESS;
89         }
90 }
91
92 static int attr_handler(struct oc_context *ac)
93 {
94         struct ldb_context *ldb;
95         struct ldb_message *msg;
96         struct ldb_request *child_req;
97         const struct dsdb_attribute *attr;
98         unsigned int i;
99         int ret;
100         WERROR werr;
101         struct dsdb_syntax_ctx syntax_ctx;
102
103         ldb = ldb_module_get_ctx(ac->module);
104
105         if (ac->req->operation == LDB_ADD) {
106                 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
107         } else {
108                 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
109         }
110         if (msg == NULL) {
111                 return ldb_oom(ldb);
112         }
113
114         /* initialize syntax checking context */
115         dsdb_syntax_ctx_init(&syntax_ctx, ldb, ac->schema);
116
117         /* Check if attributes exist in the schema, if the values match,
118          * if they're not operational and fix the names to the match the schema
119          * case */
120         for (i = 0; i < msg->num_elements; i++) {
121                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
122                                                          msg->elements[i].name);
123                 if (attr == NULL) {
124                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' was not found in the schema!",
125                                                msg->elements[i].name,
126                                                ldb_dn_get_linearized(msg->dn));
127                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
128                 }
129
130                 if ((attr->linkID & 1) == 1) {
131                         /* Odd is for the target.  Illegal to modify */
132                         ldb_asprintf_errstring(ldb, 
133                                                "objectclass_attrs: attribute '%s' on entry '%s' must not be modified directly, it is a linked attribute", 
134                                                msg->elements[i].name,
135                                                ldb_dn_get_linearized(msg->dn));
136                         return LDB_ERR_UNWILLING_TO_PERFORM;
137                 }
138                 
139                 if (!(msg->elements[i].flags & LDB_FLAG_INTERNAL_DISABLE_VALIDATION)) {
140                         werr = attr->syntax->validate_ldb(&syntax_ctx, attr,
141                                                           &msg->elements[i]);
142                         if (!W_ERROR_IS_OK(werr)) {
143                                 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
144                                                        msg->elements[i].name,
145                                                        ldb_dn_get_linearized(msg->dn));
146                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
147                         }
148                 }
149
150                 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
151                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
152                                                msg->elements[i].name,
153                                                ldb_dn_get_linearized(msg->dn));
154                         if (ac->req->operation == LDB_ADD) {
155                                 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
156                         } else {
157                                 return LDB_ERR_CONSTRAINT_VIOLATION;
158                         }
159                 }
160
161                 /* "description" on AD is very special: it's nearly single-
162                  * valued (only on add operations it isn't). */
163                 if ((ac->req->operation == LDB_MODIFY) &&
164                     (ldb_attr_cmp(attr->lDAPDisplayName, "description") == 0)) {
165                         /* Multi-valued add or replace operations are always
166                          * denied */
167                         if ((LDB_FLAG_MOD_TYPE(msg->elements[i].flags)
168                             != LDB_FLAG_MOD_DELETE) &&
169                             (msg->elements[i].num_values > 1)) {
170                                 ldb_asprintf_errstring(ldb,
171                                                        "objectclass_attrs: attribute '%s' on entry '%s' is changed using a multi-valued add or replace operation!",
172                                                        msg->elements[i].name,
173                                                        ldb_dn_get_linearized(msg->dn));
174                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
175                         }
176
177                         /* Add operations are only allowed if no value exists */
178                         if (LDB_FLAG_MOD_TYPE(msg->elements[i].flags)
179                             == LDB_FLAG_MOD_ADD) {
180                                 const char *attrs[] = { attr->lDAPDisplayName,
181                                                         NULL };
182                                 struct ldb_result *res;
183                                 struct ldb_message_element *el;
184
185                                 ret = ldb_search(ldb, ac, &res, msg->dn,
186                                                  LDB_SCOPE_BASE, attrs, NULL);
187                                 if (ret != LDB_SUCCESS) {
188                                         return ret;
189                                 }
190
191                                 el = ldb_msg_find_element(res->msgs[0],
192                                                           attr->lDAPDisplayName);
193                                 if (el != NULL) {
194                                         ldb_asprintf_errstring(ldb,
195                                                                "objectclass_attrs: attribute '%s' on entry '%s' is changed using an add operation, but there a value already exists!",
196                                                                msg->elements[i].name,
197                                                                ldb_dn_get_linearized(msg->dn));
198                                         return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
199                                 }
200                                 talloc_free(res);
201                         }
202                 }
203 /* dSHeuristics syntax check */
204                 if ((ac->req->operation == LDB_MODIFY) &&
205                     (ldb_attr_cmp(attr->lDAPDisplayName, "dSHeuristics") == 0)) {
206                         ret = oc_validate_dsheuristics(&(msg->elements[i]));
207                         if (ret != LDB_SUCCESS) {
208                                 return ret;
209                         }
210                 }
211                 /* Substitute the attribute name to match in case */
212                 msg->elements[i].name = attr->lDAPDisplayName;
213         }
214
215         if (ac->req->operation == LDB_ADD) {
216                 ret = ldb_build_add_req(&child_req, ldb, ac,
217                                         msg, ac->req->controls,
218                                         ac, oc_op_callback, ac->req);
219                 LDB_REQ_SET_LOCATION(child_req);
220         } else {
221                 ret = ldb_build_mod_req(&child_req, ldb, ac,
222                                         msg, ac->req->controls,
223                                         ac, oc_op_callback, ac->req);
224                 LDB_REQ_SET_LOCATION(child_req);
225         }
226         if (ret != LDB_SUCCESS) {
227                 return ret;
228         }
229
230         return ldb_next_request(ac->module, child_req);
231 }
232
233 /*
234   these are attributes which are left over from old ways of doing
235   things in ldb, and are harmless
236  */
237 static const char *harmless_attrs[] = { "parentGUID", NULL };
238
239 static int attr_handler2(struct oc_context *ac)
240 {
241         struct ldb_context *ldb;
242         struct ldb_message_element *oc_element;
243         struct ldb_message *msg;
244         const char **must_contain, **may_contain, **found_must_contain;
245         const struct dsdb_attribute *attr;
246         unsigned int i;
247         bool found;
248
249         ldb = ldb_module_get_ctx(ac->module);
250
251         if (ac->search_res == NULL) {
252                 return ldb_operr(ldb);
253         }
254
255         /* We rely here on the preceding "objectclass" LDB module which did
256          * already fix up the objectclass list (inheritance, order...). */
257         oc_element = ldb_msg_find_element(ac->search_res->message,
258                                           "objectClass");
259         if (oc_element == NULL) {
260                 return ldb_operr(ldb);
261         }
262
263         must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
264                                                 DSDB_SCHEMA_ALL_MUST);
265         may_contain =  dsdb_full_attribute_list(ac, ac->schema, oc_element,
266                                                 DSDB_SCHEMA_ALL_MAY);
267         found_must_contain = const_str_list(str_list_copy(ac, must_contain));
268         if ((must_contain == NULL) || (may_contain == NULL)
269             || (found_must_contain == NULL)) {
270                 return ldb_operr(ldb);
271         }
272
273         /* Check if all specified attributes are valid in the given
274          * objectclasses and if they meet additional schema restrictions. */
275         msg = ac->search_res->message;
276         for (i = 0; i < msg->num_elements; i++) {
277                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
278                                                          msg->elements[i].name);
279                 if (attr == NULL) {
280                         return ldb_operr(ldb);
281                 }
282
283                 /* Check if they're single-valued if this is requested */
284                 if ((msg->elements[i].num_values > 1) && (attr->isSingleValued)) {
285                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is single-valued!",
286                                                msg->elements[i].name,
287                                                ldb_dn_get_linearized(msg->dn));
288                         if (ac->req->operation == LDB_ADD) {
289                                 return LDB_ERR_CONSTRAINT_VIOLATION;
290                         } else {
291                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
292                         }
293                 }
294
295                 /* We can use "str_list_check" with "strcmp" here since the
296                  * attribute informations from the schema are always equal
297                  * up-down-cased. */
298                 found = str_list_check(must_contain, attr->lDAPDisplayName);
299                 if (found) {
300                         str_list_remove(found_must_contain, attr->lDAPDisplayName);
301                 } else {
302                         found = str_list_check(may_contain, attr->lDAPDisplayName);
303                 }
304                 if (!found) {
305                         found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
306                 }
307                 if (!found) {
308                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
309                                                msg->elements[i].name,
310                                                ldb_dn_get_linearized(msg->dn));
311                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
312                 }
313         }
314
315         if (found_must_contain[0] != NULL) {
316                 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
317                                        found_must_contain[0],
318                                        ldb_dn_get_linearized(msg->dn));
319                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
320         }
321
322         return ldb_module_done(ac->req, ac->mod_ares->controls,
323                                ac->mod_ares->response, LDB_SUCCESS);
324 }
325
326 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
327 {
328         struct ldb_context *ldb;
329         struct oc_context *ac;
330         int ret;
331
332         ac = talloc_get_type(req->context, struct oc_context);
333         ldb = ldb_module_get_ctx(ac->module);
334
335         if (!ares) {
336                 return ldb_module_done(ac->req, NULL, NULL,
337                                        LDB_ERR_OPERATIONS_ERROR);
338         }
339         if (ares->error != LDB_SUCCESS) {
340                 return ldb_module_done(ac->req, ares->controls,
341                                        ares->response, ares->error);
342         }
343
344         ldb_reset_err_string(ldb);
345
346         switch (ares->type) {
347         case LDB_REPLY_ENTRY:
348                 if (ac->search_res != NULL) {
349                         ldb_set_errstring(ldb, "Too many results");
350                         talloc_free(ares);
351                         return ldb_module_done(ac->req, NULL, NULL,
352                                                LDB_ERR_OPERATIONS_ERROR);
353                 }
354
355                 ac->search_res = talloc_steal(ac, ares);
356                 break;
357
358         case LDB_REPLY_REFERRAL:
359                 /* ignore */
360                 talloc_free(ares);
361                 break;
362
363         case LDB_REPLY_DONE:
364                 talloc_free(ares);
365                 ret = attr_handler2(ac);
366                 if (ret != LDB_SUCCESS) {
367                         return ldb_module_done(ac->req, NULL, NULL, ret);
368                 }
369                 break;
370         }
371
372         return LDB_SUCCESS;
373 }
374
375 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
376 {
377         struct oc_context *ac;
378         struct ldb_context *ldb;
379         struct ldb_request *search_req;
380         struct ldb_dn *base_dn;
381         int ret;
382
383         ac = talloc_get_type(req->context, struct oc_context);
384         ldb = ldb_module_get_ctx(ac->module);
385
386         if (!ares) {
387                 return ldb_module_done(ac->req, NULL, NULL,
388                                        LDB_ERR_OPERATIONS_ERROR);
389         }
390
391         if (ares->type == LDB_REPLY_REFERRAL) {
392                 return ldb_module_send_referral(ac->req, ares->referral);
393         }
394
395         if (ares->error != LDB_SUCCESS) {
396                 return ldb_module_done(ac->req, ares->controls, ares->response,
397                                        ares->error);
398         }
399
400         if (ares->type != LDB_REPLY_DONE) {
401                 talloc_free(ares);
402                 return ldb_module_done(ac->req, NULL, NULL,
403                                        LDB_ERR_OPERATIONS_ERROR);
404         }
405
406         ac->search_res = NULL;
407         ac->mod_ares = talloc_steal(ac, ares);
408
409         /* This looks up all attributes of our just added/modified entry */
410         base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
411                 : ac->req->op.mod.message->dn;
412         ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
413                                    LDB_SCOPE_BASE, "(objectClass=*)",
414                                    NULL, NULL, ac,
415                                    get_search_callback, ac->req);
416         LDB_REQ_SET_LOCATION(search_req);
417         if (ret != LDB_SUCCESS) {
418                 return ldb_module_done(ac->req, NULL, NULL, ret);
419         }
420
421         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
422                                       true, NULL);
423         if (ret != LDB_SUCCESS) {
424                 return ldb_module_done(ac->req, NULL, NULL, ret);
425         }
426
427         ret = ldb_next_request(ac->module, search_req);
428         if (ret != LDB_SUCCESS) {
429                 return ldb_module_done(ac->req, NULL, NULL, ret);
430         }
431
432         /* "ldb_module_done" isn't called here since we need to do additional
433          * checks. It is called at the end of "attr_handler2". */
434         return LDB_SUCCESS;
435 }
436
437 static int objectclass_attrs_add(struct ldb_module *module,
438                                  struct ldb_request *req)
439 {
440         struct ldb_context *ldb;
441         struct oc_context *ac;
442
443         ldb = ldb_module_get_ctx(module);
444
445         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
446
447         /* do not manipulate our control entries */
448         if (ldb_dn_is_special(req->op.add.message->dn)) {
449                 return ldb_next_request(module, req);
450         }
451
452         ac = oc_init_context(module, req);
453         if (ac == NULL) {
454                 return ldb_operr(ldb);
455         }
456
457         /* without schema, there isn't much to do here */
458         if (ac->schema == NULL) {
459                 talloc_free(ac);
460                 return ldb_next_request(module, req);
461         }
462
463         return attr_handler(ac);
464 }
465
466 static int objectclass_attrs_modify(struct ldb_module *module,
467                                     struct ldb_request *req)
468 {
469         struct ldb_context *ldb;
470         struct oc_context *ac;
471
472         ldb = ldb_module_get_ctx(module);
473
474         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
475
476         /* do not manipulate our control entries */
477         if (ldb_dn_is_special(req->op.mod.message->dn)) {
478                 return ldb_next_request(module, req);
479         }
480
481         ac = oc_init_context(module, req);
482         if (ac == NULL) {
483                 return ldb_operr(ldb);
484         }
485
486         /* without schema, there isn't much to do here */
487         if (ac->schema == NULL) {
488                 talloc_free(ac);
489                 return ldb_next_request(module, req);
490         }
491
492         return attr_handler(ac);
493 }
494
495 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
496         .name              = "objectclass_attrs",
497         .add               = objectclass_attrs_add,
498         .modify            = objectclass_attrs_modify
499 };