s4-ldb: added LDB_FLAG_INTERNAL_DISABLE_VALIDATION
[nivanova/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_reply *search_res;
47         struct ldb_reply *mod_ares;
48 };
49
50 static struct oc_context *oc_init_context(struct ldb_module *module,
51                                           struct ldb_request *req)
52 {
53         struct ldb_context *ldb;
54         struct oc_context *ac;
55
56         ldb = ldb_module_get_ctx(module);
57
58         ac = talloc_zero(req, struct oc_context);
59         if (ac == NULL) {
60                 ldb_oom(ldb);
61                 return NULL;
62         }
63
64         ac->module = module;
65         ac->req = req;
66         ac->schema = dsdb_get_schema(ldb, ac);
67
68         return ac;
69 }
70
71 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares);
72
73 static int attr_handler(struct oc_context *ac)
74 {
75         struct ldb_context *ldb;
76         struct ldb_message *msg;
77         struct ldb_request *child_req;
78         const struct dsdb_attribute *attr;
79         unsigned int i;
80         int ret;
81         WERROR werr;
82
83         ldb = ldb_module_get_ctx(ac->module);
84
85         if (ac->req->operation == LDB_ADD) {
86                 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
87         } else {
88                 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
89         }
90         if (msg == NULL) {
91                 return ldb_oom(ldb);
92         }
93
94         /* Check if attributes exist in the schema, if the values match,
95          * if they're not operational and fix the names to the match the schema
96          * case */
97         for (i = 0; i < msg->num_elements; i++) {
98                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
99                                                          msg->elements[i].name);
100                 if (attr == NULL) {
101                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' was not found in the schema!",
102                                                msg->elements[i].name,
103                                                ldb_dn_get_linearized(msg->dn));
104                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
105                 }
106
107                 if ((attr->linkID & 1) == 1) {
108                         /* Odd is for the target.  Illegal to modify */
109                         ldb_asprintf_errstring(ldb, 
110                                                "objectclass_attrs: attribute '%s' on entry '%s' must not be modified directly, it is a linked attribute", 
111                                                msg->elements[i].name,
112                                                ldb_dn_get_linearized(msg->dn));
113                         return LDB_ERR_UNWILLING_TO_PERFORM;
114                 }
115                 
116                 if (!(msg->elements[i].flags & LDB_FLAG_INTERNAL_DISABLE_VALIDATION)) {
117                         werr = attr->syntax->validate_ldb(ldb, ac->schema, attr,
118                                                           &msg->elements[i]);
119                         if (!W_ERROR_IS_OK(werr)) {
120                                 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
121                                                        msg->elements[i].name,
122                                                        ldb_dn_get_linearized(msg->dn));
123                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
124                         }
125                 }
126
127                 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
128                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
129                                                msg->elements[i].name,
130                                                ldb_dn_get_linearized(msg->dn));
131                         if (ac->req->operation == LDB_ADD) {
132                                 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
133                         } else {
134                                 return LDB_ERR_CONSTRAINT_VIOLATION;
135                         }
136                 }
137
138                 /* subsitute the attribute name to match in case */
139                 msg->elements[i].name = attr->lDAPDisplayName;
140         }
141
142         if (ac->req->operation == LDB_ADD) {
143                 ret = ldb_build_add_req(&child_req, ldb, ac,
144                                         msg, ac->req->controls,
145                                         ac, oc_op_callback, ac->req);
146         } else {
147                 ret = ldb_build_mod_req(&child_req, ldb, ac,
148                                         msg, ac->req->controls,
149                                         ac, oc_op_callback, ac->req);
150         }
151         if (ret != LDB_SUCCESS) {
152                 return ret;
153         }
154
155         return ldb_next_request(ac->module, child_req);
156 }
157
158 /*
159   these are attributes which are left over from old ways of doing
160   things in ldb, and are harmless
161  */
162 static const char *harmless_attrs[] = { "parentGUID", NULL };
163
164 static int attr_handler2(struct oc_context *ac)
165 {
166         struct ldb_context *ldb;
167         struct ldb_message_element *oc_element;
168         struct ldb_message *msg;
169         const char **must_contain, **may_contain, **found_must_contain;
170         const struct dsdb_attribute *attr;
171         unsigned int i;
172         bool found;
173
174         ldb = ldb_module_get_ctx(ac->module);
175
176         if (ac->search_res == NULL) {
177                 return ldb_operr(ldb);
178         }
179
180         /* We rely here on the preceeding "objectclass" LDB module which did
181          * already fix up the objectclass list (inheritance, order...). */
182         oc_element = ldb_msg_find_element(ac->search_res->message,
183                                           "objectClass");
184         if (oc_element == NULL) {
185                 return ldb_operr(ldb);
186         }
187
188         must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
189                                                 DSDB_SCHEMA_ALL_MUST);
190         may_contain =  dsdb_full_attribute_list(ac, ac->schema, oc_element,
191                                                 DSDB_SCHEMA_ALL_MAY);
192         found_must_contain = const_str_list(str_list_copy(ac, must_contain));
193         if ((must_contain == NULL) || (may_contain == NULL)
194             || (found_must_contain == NULL)) {
195                 return ldb_operr(ldb);
196         }
197
198         /* Check if all specified attributes are valid in the given
199          * objectclasses and if they meet additional schema restrictions. */
200         msg = ac->search_res->message;
201         for (i = 0; i < msg->num_elements; i++) {
202                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
203                                                          msg->elements[i].name);
204                 if (attr == NULL) {
205                         return ldb_operr(ldb);
206                 }
207
208                 /* Check if they're single-valued if this is requested */
209                 if ((msg->elements[i].num_values > 1) && (attr->isSingleValued)) {
210                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is single-valued!",
211                                                msg->elements[i].name,
212                                                ldb_dn_get_linearized(msg->dn));
213                         if (ac->req->operation == LDB_ADD) {
214                                 return LDB_ERR_CONSTRAINT_VIOLATION;
215                         } else {
216                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
217                         }
218                 }
219
220                 /* We can use "str_list_check" with "strcmp" here since the
221                  * attribute informations from the schema are always equal
222                  * up-down-cased. */
223                 found = str_list_check(must_contain, attr->lDAPDisplayName);
224                 if (found) {
225                         str_list_remove(found_must_contain, attr->lDAPDisplayName);
226                 } else {
227                         found = str_list_check(may_contain, attr->lDAPDisplayName);
228                 }
229                 if (!found) {
230                         found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
231                 }
232                 if (!found) {
233                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
234                                                msg->elements[i].name,
235                                                ldb_dn_get_linearized(msg->dn));
236                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
237                 }
238         }
239
240         if (found_must_contain[0] != NULL) {
241                 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
242                                        found_must_contain[0],
243                                        ldb_dn_get_linearized(msg->dn));
244                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
245         }
246
247         return ldb_module_done(ac->req, ac->mod_ares->controls,
248                                ac->mod_ares->response, LDB_SUCCESS);
249 }
250
251 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
252 {
253         struct ldb_context *ldb;
254         struct oc_context *ac;
255         int ret;
256
257         ac = talloc_get_type(req->context, struct oc_context);
258         ldb = ldb_module_get_ctx(ac->module);
259
260         if (!ares) {
261                 return ldb_module_done(ac->req, NULL, NULL,
262                                        LDB_ERR_OPERATIONS_ERROR);
263         }
264         if (ares->error != LDB_SUCCESS) {
265                 return ldb_module_done(ac->req, ares->controls,
266                                        ares->response, ares->error);
267         }
268
269         ldb_reset_err_string(ldb);
270
271         switch (ares->type) {
272         case LDB_REPLY_ENTRY:
273                 if (ac->search_res != NULL) {
274                         ldb_set_errstring(ldb, "Too many results");
275                         talloc_free(ares);
276                         return ldb_module_done(ac->req, NULL, NULL,
277                                                LDB_ERR_OPERATIONS_ERROR);
278                 }
279
280                 ac->search_res = talloc_steal(ac, ares);
281                 break;
282
283         case LDB_REPLY_REFERRAL:
284                 /* ignore */
285                 talloc_free(ares);
286                 break;
287
288         case LDB_REPLY_DONE:
289                 talloc_free(ares);
290                 ret = attr_handler2(ac);
291                 if (ret != LDB_SUCCESS) {
292                         return ldb_module_done(ac->req, NULL, NULL, ret);
293                 }
294                 break;
295         }
296
297         return LDB_SUCCESS;
298 }
299
300 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
301 {
302         struct oc_context *ac;
303         struct ldb_context *ldb;
304         struct ldb_request *search_req;
305         struct ldb_dn *base_dn;
306         int ret;
307
308         ac = talloc_get_type(req->context, struct oc_context);
309         ldb = ldb_module_get_ctx(ac->module);
310
311         if (!ares) {
312                 return ldb_module_done(ac->req, NULL, NULL,
313                                        LDB_ERR_OPERATIONS_ERROR);
314         }
315
316         if (ares->type == LDB_REPLY_REFERRAL) {
317                 return ldb_module_send_referral(ac->req, ares->referral);
318         }
319
320         if (ares->error != LDB_SUCCESS) {
321                 return ldb_module_done(ac->req, ares->controls, ares->response,
322                                        ares->error);
323         }
324
325         if (ares->type != LDB_REPLY_DONE) {
326                 talloc_free(ares);
327                 return ldb_module_done(ac->req, NULL, NULL,
328                                        LDB_ERR_OPERATIONS_ERROR);
329         }
330
331         ac->search_res = NULL;
332         ac->mod_ares = talloc_steal(ac, ares);
333
334         /* This looks up all attributes of our just added/modified entry */
335         base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
336                 : ac->req->op.mod.message->dn;
337         ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
338                                    LDB_SCOPE_BASE, "(objectClass=*)",
339                                    NULL, NULL, ac,
340                                    get_search_callback, ac->req);
341         if (ret != LDB_SUCCESS) {
342                 return ldb_module_done(ac->req, NULL, NULL, ret);
343         }
344
345         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID,
346                                       true, NULL);
347         if (ret != LDB_SUCCESS) {
348                 return ldb_module_done(ac->req, NULL, NULL, ret);
349         }
350
351         ret = ldb_next_request(ac->module, search_req);
352         if (ret != LDB_SUCCESS) {
353                 return ldb_module_done(ac->req, NULL, NULL, ret);
354         }
355
356         /* "ldb_module_done" isn't called here since we need to do additional
357          * checks. It is called at the end of "attr_handler2". */
358         return LDB_SUCCESS;
359 }
360
361 static int objectclass_attrs_add(struct ldb_module *module,
362                                  struct ldb_request *req)
363 {
364         struct ldb_context *ldb;
365         struct oc_context *ac;
366
367         ldb = ldb_module_get_ctx(module);
368
369         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
370
371         /* do not manipulate our control entries */
372         if (ldb_dn_is_special(req->op.add.message->dn)) {
373                 return ldb_next_request(module, req);
374         }
375
376         ac = oc_init_context(module, req);
377         if (ac == NULL) {
378                 return ldb_operr(ldb);
379         }
380
381         /* without schema, there isn't much to do here */
382         if (ac->schema == NULL) {
383                 talloc_free(ac);
384                 return ldb_next_request(module, req);
385         }
386
387         return attr_handler(ac);
388 }
389
390 static int objectclass_attrs_modify(struct ldb_module *module,
391                                     struct ldb_request *req)
392 {
393         struct ldb_context *ldb;
394         struct oc_context *ac;
395
396         ldb = ldb_module_get_ctx(module);
397
398         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
399
400         /* do not manipulate our control entries */
401         if (ldb_dn_is_special(req->op.mod.message->dn)) {
402                 return ldb_next_request(module, req);
403         }
404
405         ac = oc_init_context(module, req);
406         if (ac == NULL) {
407                 return ldb_operr(ldb);
408         }
409
410         /* without schema, there isn't much to do here */
411         if (ac->schema == NULL) {
412                 talloc_free(ac);
413                 return ldb_next_request(module, req);
414         }
415
416         return attr_handler(ac);
417 }
418
419 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
420         .name              = "objectclass_attrs",
421         .add               = objectclass_attrs_add,
422         .modify            = objectclass_attrs_modify
423 };