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