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