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