s4-dsdb: allow deletion of backlinks if DSDB_CONTROL_DBCHECK given
[metze/samba/wip.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_message *msg;
47
48         struct ldb_reply *search_res;
49         struct ldb_reply *mod_ares;
50 };
51
52 static struct oc_context *oc_init_context(struct ldb_module *module,
53                                           struct ldb_request *req)
54 {
55         struct ldb_context *ldb;
56         struct oc_context *ac;
57
58         ldb = ldb_module_get_ctx(module);
59
60         ac = talloc_zero(req, struct oc_context);
61         if (ac == NULL) {
62                 ldb_oom(ldb);
63                 return NULL;
64         }
65
66         ac->module = module;
67         ac->req = req;
68         ac->schema = dsdb_get_schema(ldb, ac);
69
70         return ac;
71 }
72
73 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares);
74
75 /* checks correctness of dSHeuristics attribute
76  * as described in MS-ADTS 7.1.1.2.4.1.2 dSHeuristics */
77 static int oc_validate_dsheuristics(struct ldb_message_element *el)
78 {
79         if (el->num_values > 0) {
80                 if (el->values[0].length > DS_HR_LDAP_BYPASS_UPPER_LIMIT_BOUNDS) {
81                         return LDB_ERR_CONSTRAINT_VIOLATION;
82                 } else if (el->values[0].length >= DS_HR_TENTH_CHAR
83                            && el->values[0].data[DS_HR_TENTH_CHAR-1] != '1') {
84                         return LDB_ERR_CONSTRAINT_VIOLATION;
85                 }
86         }
87
88         return LDB_SUCCESS;
89 }
90
91 /*
92   auto normalise values on input
93  */
94 static int oc_auto_normalise(struct ldb_context *ldb, const struct dsdb_attribute *attr,
95                              struct ldb_message *msg, struct ldb_message_element *el)
96 {
97         int i;
98         bool values_copied = false;
99
100         for (i=0; i<el->num_values; i++) {
101                 struct ldb_val v;
102                 int ret;
103                 ret = attr->ldb_schema_attribute->syntax->canonicalise_fn(ldb, el->values, &el->values[i], &v);
104                 if (ret != LDB_SUCCESS) {
105                         return ret;
106                 }
107                 if (data_blob_cmp(&v, &el->values[i]) == 0) {
108                         /* no need to replace it */
109                         talloc_free(v.data);
110                         continue;
111                 }
112
113                 /* we need to copy the values array on the first change */
114                 if (!values_copied) {
115                         struct ldb_val *v2;
116                         v2 = talloc_array(msg->elements, struct ldb_val, el->num_values);
117                         if (v2 == NULL) {
118                                 return ldb_oom(ldb);
119                         }
120                         memcpy(v2, el->values, sizeof(struct ldb_val) * el->num_values);
121                         el->values = v2;
122                         values_copied = true;
123                 }
124
125                 el->values[i] = v;
126         }
127         return LDB_SUCCESS;
128 }
129
130 static int attr_handler(struct oc_context *ac)
131 {
132         struct ldb_context *ldb;
133         struct ldb_message *msg;
134         struct ldb_request *child_req;
135         const struct dsdb_attribute *attr;
136         unsigned int i;
137         int ret;
138         WERROR werr;
139         struct dsdb_syntax_ctx syntax_ctx;
140
141         ldb = ldb_module_get_ctx(ac->module);
142
143         if (ac->req->operation == LDB_ADD) {
144                 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
145         } else {
146                 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
147         }
148         if (msg == NULL) {
149                 return ldb_oom(ldb);
150         }
151         ac->msg = msg;
152
153         /* initialize syntax checking context */
154         dsdb_syntax_ctx_init(&syntax_ctx, ldb, ac->schema);
155
156         /* Check if attributes exist in the schema, if the values match,
157          * if they're not operational and fix the names to the match the schema
158          * case */
159         for (i = 0; i < msg->num_elements; i++) {
160                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
161                                                          msg->elements[i].name);
162                 if (attr == NULL) {
163                         if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
164                             ac->req->operation != LDB_ADD) {
165                                 /* we allow this for dbcheck to fix
166                                    broken attributes */
167                                 goto no_attribute;
168                         }
169                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' was not found in the schema!",
170                                                msg->elements[i].name,
171                                                ldb_dn_get_linearized(msg->dn));
172                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
173                 }
174
175                 if ((attr->linkID & 1) == 1 &&
176                     !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
177                     !ldb_request_get_control(ac->req, DSDB_CONTROL_DBCHECK)) {
178                         /* Odd is for the target.  Illegal to modify */
179                         ldb_asprintf_errstring(ldb, 
180                                                "objectclass_attrs: attribute '%s' on entry '%s' must not be modified directly, it is a linked attribute", 
181                                                msg->elements[i].name,
182                                                ldb_dn_get_linearized(msg->dn));
183                         return LDB_ERR_UNWILLING_TO_PERFORM;
184                 }
185                 
186                 if (!(msg->elements[i].flags & LDB_FLAG_INTERNAL_DISABLE_VALIDATION)) {
187                         werr = attr->syntax->validate_ldb(&syntax_ctx, attr,
188                                                           &msg->elements[i]);
189                         if (!W_ERROR_IS_OK(werr) &&
190                             !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
191                                 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
192                                                        msg->elements[i].name,
193                                                        ldb_dn_get_linearized(msg->dn));
194                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
195                         }
196                 }
197
198                 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
199                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
200                                                msg->elements[i].name,
201                                                ldb_dn_get_linearized(msg->dn));
202                         if (ac->req->operation == LDB_ADD) {
203                                 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
204                         } else {
205                                 return LDB_ERR_CONSTRAINT_VIOLATION;
206                         }
207                 }
208
209                 /* "dSHeuristics" syntax check */
210                 if (ldb_attr_cmp(attr->lDAPDisplayName, "dSHeuristics") == 0) {
211                         ret = oc_validate_dsheuristics(&(msg->elements[i]));
212                         if (ret != LDB_SUCCESS) {
213                                 return ret;
214                         }
215                 }
216
217                 /* auto normalise some attribute values */
218                 if (attr->syntax->auto_normalise) {
219                         ret = oc_auto_normalise(ldb, attr, msg, &msg->elements[i]);
220                         if (ret != LDB_SUCCESS) {
221                                 return ret;
222                         }
223                 }
224
225                 /* Substitute the attribute name to match in case */
226                 msg->elements[i].name = attr->lDAPDisplayName;
227         }
228
229 no_attribute:
230         if (ac->req->operation == LDB_ADD) {
231                 ret = ldb_build_add_req(&child_req, ldb, ac,
232                                         msg, ac->req->controls,
233                                         ac, oc_op_callback, ac->req);
234                 LDB_REQ_SET_LOCATION(child_req);
235         } else {
236                 ret = ldb_build_mod_req(&child_req, ldb, ac,
237                                         msg, ac->req->controls,
238                                         ac, oc_op_callback, ac->req);
239                 LDB_REQ_SET_LOCATION(child_req);
240         }
241         if (ret != LDB_SUCCESS) {
242                 return ret;
243         }
244
245         return ldb_next_request(ac->module, child_req);
246 }
247
248 /*
249   these are attributes which are left over from old ways of doing
250   things in ldb, and are harmless
251  */
252 static const char *harmless_attrs[] = { "parentGUID", NULL };
253
254 static int attr_handler2(struct oc_context *ac)
255 {
256         struct ldb_context *ldb;
257         struct ldb_message_element *oc_element;
258         struct ldb_message *msg;
259         const char **must_contain, **may_contain, **found_must_contain;
260         /* There exists a hardcoded delete-protected attributes list in AD */
261         const char *del_prot_attributes[] = { "nTSecurityDescriptor",
262                 "objectSid", "sAMAccountType", "sAMAccountName", "groupType",
263                 "primaryGroupID", "userAccountControl", "accountExpires",
264                 "badPasswordTime", "badPwdCount", "codePage", "countryCode",
265                 "lastLogoff", "lastLogon", "logonCount", "pwdLastSet", NULL },
266                 **l;
267         const struct dsdb_attribute *attr;
268         unsigned int i;
269         bool found;
270
271         ldb = ldb_module_get_ctx(ac->module);
272
273         if (ac->search_res == NULL) {
274                 return ldb_operr(ldb);
275         }
276
277         /* We rely here on the preceeding "objectclass" LDB module which did
278          * already fix up the objectclass list (inheritance, order...). */
279         oc_element = ldb_msg_find_element(ac->search_res->message,
280                                           "objectClass");
281         if (oc_element == NULL) {
282                 return ldb_operr(ldb);
283         }
284
285         /* LSA-specific object classes are not allowed to be created over LDAP,
286          * so we need to tell if this connection is internal (trusted) or not
287          * (untrusted).
288          *
289          * Hongwei Sun from Microsoft explains:
290          * The constraint in 3.1.1.5.2.2 MS-ADTS means that LSA objects cannot
291          * be added or modified through the LDAP interface, instead they can
292          * only be handled through LSA Policy API.  This is also explained in
293          * 7.1.6.9.7 MS-ADTS as follows:
294          * "Despite being replicated normally between peer DCs in a domain,
295          * the process of creating or manipulating TDOs is specifically
296          * restricted to the LSA Policy APIs, as detailed in [MS-LSAD] section
297          * 3.1.1.5. Unlike other objects in the DS, TDOs may not be created or
298          *  manipulated by client machines over the LDAPv3 transport."
299          */
300         if (ldb_req_is_untrusted(ac->req)) {
301                 for (i = 0; i < oc_element->num_values; i++) {
302                         if ((strcmp((char *)oc_element->values[i].data,
303                                     "secret") == 0) ||
304                             (strcmp((char *)oc_element->values[i].data,
305                                     "trustedDomain") == 0)) {
306                                 ldb_asprintf_errstring(ldb, "objectclass_attrs: LSA objectclasses (entry '%s') cannot be created or changed over LDAP!",
307                                                        ldb_dn_get_linearized(ac->search_res->message->dn));
308                                 return LDB_ERR_UNWILLING_TO_PERFORM;
309                         }
310                 }
311         }
312
313         must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
314                                                 DSDB_SCHEMA_ALL_MUST);
315         may_contain =  dsdb_full_attribute_list(ac, ac->schema, oc_element,
316                                                 DSDB_SCHEMA_ALL_MAY);
317         found_must_contain = const_str_list(str_list_copy(ac, must_contain));
318         if ((must_contain == NULL) || (may_contain == NULL)
319             || (found_must_contain == NULL)) {
320                 return ldb_operr(ldb);
321         }
322
323         /* Check the delete-protected attributes list */
324         msg = ac->search_res->message;
325         for (l = del_prot_attributes; *l != NULL; l++) {
326                 struct ldb_message_element *el;
327
328                 el = ldb_msg_find_element(ac->msg, *l);
329                 if (el == NULL) {
330                         /*
331                          * It was not specified in the add or modify,
332                          * so it doesn't need to be in the stored record
333                          */
334                         continue;
335                 }
336
337                 found = str_list_check_ci(must_contain, *l);
338                 if (!found) {
339                         found = str_list_check_ci(may_contain, *l);
340                 }
341                 if (found && (ldb_msg_find_element(msg, *l) == NULL)) {
342                         ldb_asprintf_errstring(ldb, "objectclass_attrs: delete protected attribute '%s' on entry '%s' missing!",
343                                                *l,
344                                                ldb_dn_get_linearized(msg->dn));
345                         return LDB_ERR_UNWILLING_TO_PERFORM;
346                 }
347         }
348
349         /* Check if all specified attributes are valid in the given
350          * objectclasses and if they meet additional schema restrictions. */
351         for (i = 0; i < msg->num_elements; i++) {
352                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
353                                                          msg->elements[i].name);
354                 if (attr == NULL) {
355                         if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
356                                 /* allow this to make it possible for dbcheck
357                                    to remove bad attributes */
358                                 continue;
359                         }
360                         return ldb_operr(ldb);
361                 }
362
363                 /* We can use "str_list_check" with "strcmp" here since the
364                  * attribute information from the schema are always equal
365                  * up-down-cased. */
366                 found = str_list_check(must_contain, attr->lDAPDisplayName);
367                 if (found) {
368                         str_list_remove(found_must_contain, attr->lDAPDisplayName);
369                 } else {
370                         found = str_list_check(may_contain, attr->lDAPDisplayName);
371                 }
372                 if (!found) {
373                         found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
374                 }
375                 if (!found) {
376                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
377                                                msg->elements[i].name,
378                                                ldb_dn_get_linearized(msg->dn));
379                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
380                 }
381         }
382
383         if (found_must_contain[0] != NULL &&
384             ldb_msg_check_string_attribute(msg, "isDeleted", "TRUE") == 0) {
385                 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
386                                        found_must_contain[0],
387                                        ldb_dn_get_linearized(msg->dn));
388                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
389         }
390
391         return ldb_module_done(ac->req, ac->mod_ares->controls,
392                                ac->mod_ares->response, LDB_SUCCESS);
393 }
394
395 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
396 {
397         struct ldb_context *ldb;
398         struct oc_context *ac;
399         int ret;
400
401         ac = talloc_get_type(req->context, struct oc_context);
402         ldb = ldb_module_get_ctx(ac->module);
403
404         if (!ares) {
405                 return ldb_module_done(ac->req, NULL, NULL,
406                                        LDB_ERR_OPERATIONS_ERROR);
407         }
408         if (ares->error != LDB_SUCCESS) {
409                 return ldb_module_done(ac->req, ares->controls,
410                                        ares->response, ares->error);
411         }
412
413         ldb_reset_err_string(ldb);
414
415         switch (ares->type) {
416         case LDB_REPLY_ENTRY:
417                 if (ac->search_res != NULL) {
418                         ldb_set_errstring(ldb, "Too many results");
419                         talloc_free(ares);
420                         return ldb_module_done(ac->req, NULL, NULL,
421                                                LDB_ERR_OPERATIONS_ERROR);
422                 }
423
424                 ac->search_res = talloc_steal(ac, ares);
425                 break;
426
427         case LDB_REPLY_REFERRAL:
428                 /* ignore */
429                 talloc_free(ares);
430                 break;
431
432         case LDB_REPLY_DONE:
433                 talloc_free(ares);
434                 ret = attr_handler2(ac);
435                 if (ret != LDB_SUCCESS) {
436                         return ldb_module_done(ac->req, NULL, NULL, ret);
437                 }
438                 break;
439         }
440
441         return LDB_SUCCESS;
442 }
443
444 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
445 {
446         struct oc_context *ac;
447         struct ldb_context *ldb;
448         struct ldb_request *search_req;
449         struct ldb_dn *base_dn;
450         int ret;
451
452         ac = talloc_get_type(req->context, struct oc_context);
453         ldb = ldb_module_get_ctx(ac->module);
454
455         if (!ares) {
456                 return ldb_module_done(ac->req, NULL, NULL,
457                                        LDB_ERR_OPERATIONS_ERROR);
458         }
459
460         if (ares->type == LDB_REPLY_REFERRAL) {
461                 return ldb_module_send_referral(ac->req, ares->referral);
462         }
463
464         if (ares->error != LDB_SUCCESS) {
465                 return ldb_module_done(ac->req, ares->controls, ares->response,
466                                        ares->error);
467         }
468
469         if (ares->type != LDB_REPLY_DONE) {
470                 talloc_free(ares);
471                 return ldb_module_done(ac->req, NULL, NULL,
472                                        LDB_ERR_OPERATIONS_ERROR);
473         }
474
475         ac->search_res = NULL;
476         ac->mod_ares = talloc_steal(ac, ares);
477
478         /* This looks up all attributes of our just added/modified entry */
479         base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
480                 : ac->req->op.mod.message->dn;
481         ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
482                                    LDB_SCOPE_BASE, "(objectClass=*)",
483                                    NULL, NULL, ac,
484                                    get_search_callback, ac->req);
485         LDB_REQ_SET_LOCATION(search_req);
486         if (ret != LDB_SUCCESS) {
487                 return ldb_module_done(ac->req, NULL, NULL, ret);
488         }
489
490         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
491                                       true, NULL);
492         if (ret != LDB_SUCCESS) {
493                 return ldb_module_done(ac->req, NULL, NULL, ret);
494         }
495
496         ret = ldb_next_request(ac->module, search_req);
497         if (ret != LDB_SUCCESS) {
498                 return ldb_module_done(ac->req, NULL, NULL, ret);
499         }
500
501         /* "ldb_module_done" isn't called here since we need to do additional
502          * checks. It is called at the end of "attr_handler2". */
503         return LDB_SUCCESS;
504 }
505
506 static int objectclass_attrs_add(struct ldb_module *module,
507                                  struct ldb_request *req)
508 {
509         struct ldb_context *ldb;
510         struct oc_context *ac;
511
512         ldb = ldb_module_get_ctx(module);
513
514         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
515
516         /* do not manipulate our control entries */
517         if (ldb_dn_is_special(req->op.add.message->dn)) {
518                 return ldb_next_request(module, req);
519         }
520
521         ac = oc_init_context(module, req);
522         if (ac == NULL) {
523                 return ldb_operr(ldb);
524         }
525
526         /* without schema, there isn't much to do here */
527         if (ac->schema == NULL) {
528                 talloc_free(ac);
529                 return ldb_next_request(module, req);
530         }
531
532         return attr_handler(ac);
533 }
534
535 static int objectclass_attrs_modify(struct ldb_module *module,
536                                     struct ldb_request *req)
537 {
538         struct ldb_context *ldb;
539         struct oc_context *ac;
540
541         ldb = ldb_module_get_ctx(module);
542
543         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
544
545         /* do not manipulate our control entries */
546         if (ldb_dn_is_special(req->op.mod.message->dn)) {
547                 return ldb_next_request(module, req);
548         }
549
550         ac = oc_init_context(module, req);
551         if (ac == NULL) {
552                 return ldb_operr(ldb);
553         }
554
555         /* without schema, there isn't much to do here */
556         if (ac->schema == NULL) {
557                 talloc_free(ac);
558                 return ldb_next_request(module, req);
559         }
560
561         return attr_handler(ac);
562 }
563
564 static const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
565         .name              = "objectclass_attrs",
566         .add               = objectclass_attrs_add,
567         .modify            = objectclass_attrs_modify
568 };
569
570 int ldb_objectclass_attrs_module_init(const char *version)
571 {
572         LDB_MODULE_CHECK_VERSION(version);
573         return ldb_register_module(&ldb_objectclass_attrs_module_ops);
574 }