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