s4-dsdb: convert the rest of the ldb modules to the new module type
[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_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         struct dsdb_syntax_ctx syntax_ctx;
83
84         ldb = ldb_module_get_ctx(ac->module);
85
86         if (ac->req->operation == LDB_ADD) {
87                 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
88         } else {
89                 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
90         }
91         if (msg == NULL) {
92                 return ldb_oom(ldb);
93         }
94
95         /* initialize syntax checking context */
96         dsdb_syntax_ctx_init(&syntax_ctx, ldb, ac->schema);
97
98         /* Check if attributes exist in the schema, if the values match,
99          * if they're not operational and fix the names to the match the schema
100          * case */
101         for (i = 0; i < msg->num_elements; i++) {
102                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
103                                                          msg->elements[i].name);
104                 if (attr == NULL) {
105                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' was not found in the schema!",
106                                                msg->elements[i].name,
107                                                ldb_dn_get_linearized(msg->dn));
108                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
109                 }
110
111                 if ((attr->linkID & 1) == 1) {
112                         /* Odd is for the target.  Illegal to modify */
113                         ldb_asprintf_errstring(ldb, 
114                                                "objectclass_attrs: attribute '%s' on entry '%s' must not be modified directly, it is a linked attribute", 
115                                                msg->elements[i].name,
116                                                ldb_dn_get_linearized(msg->dn));
117                         return LDB_ERR_UNWILLING_TO_PERFORM;
118                 }
119                 
120                 if (!(msg->elements[i].flags & LDB_FLAG_INTERNAL_DISABLE_VALIDATION)) {
121                         werr = attr->syntax->validate_ldb(&syntax_ctx, attr,
122                                                           &msg->elements[i]);
123                         if (!W_ERROR_IS_OK(werr)) {
124                                 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
125                                                        msg->elements[i].name,
126                                                        ldb_dn_get_linearized(msg->dn));
127                                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
128                         }
129                 }
130
131                 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
132                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
133                                                msg->elements[i].name,
134                                                ldb_dn_get_linearized(msg->dn));
135                         if (ac->req->operation == LDB_ADD) {
136                                 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
137                         } else {
138                                 return LDB_ERR_CONSTRAINT_VIOLATION;
139                         }
140                 }
141
142                 /* "description" on AD is very special: it's nearly single-
143                  * valued (only on add operations it isn't). */
144                 if ((ac->req->operation == LDB_MODIFY) &&
145                     (ldb_attr_cmp(attr->lDAPDisplayName, "description") == 0)) {
146                         /* Multi-valued add or replace operations are always
147                          * denied */
148                         if ((LDB_FLAG_MOD_TYPE(msg->elements[i].flags)
149                             != LDB_FLAG_MOD_DELETE) &&
150                             (msg->elements[i].num_values > 1)) {
151                                 ldb_asprintf_errstring(ldb,
152                                                        "objectclass_attrs: attribute '%s' on entry '%s' is changed using a multi-valued add or replace operation!",
153                                                        msg->elements[i].name,
154                                                        ldb_dn_get_linearized(msg->dn));
155                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
156                         }
157
158                         /* Add operations are only allowed if no value exists */
159                         if (LDB_FLAG_MOD_TYPE(msg->elements[i].flags)
160                             == LDB_FLAG_MOD_ADD) {
161                                 const char *attrs[] = { attr->lDAPDisplayName,
162                                                         NULL };
163                                 struct ldb_result *res;
164                                 struct ldb_message_element *el;
165
166                                 ret = ldb_search(ldb, ac, &res, msg->dn,
167                                                  LDB_SCOPE_BASE, attrs, NULL);
168                                 if (ret != LDB_SUCCESS) {
169                                         return ret;
170                                 }
171
172                                 el = ldb_msg_find_element(res->msgs[0],
173                                                           attr->lDAPDisplayName);
174                                 if (el != NULL) {
175                                         ldb_asprintf_errstring(ldb,
176                                                                "objectclass_attrs: attribute '%s' on entry '%s' is changed using an add operation, but there a value already exists!",
177                                                                msg->elements[i].name,
178                                                                ldb_dn_get_linearized(msg->dn));
179                                         return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
180                                 }
181                                 talloc_free(res);
182                         }
183                 }
184
185                 /* Substitute the attribute name to match in case */
186                 msg->elements[i].name = attr->lDAPDisplayName;
187         }
188
189         if (ac->req->operation == LDB_ADD) {
190                 ret = ldb_build_add_req(&child_req, ldb, ac,
191                                         msg, ac->req->controls,
192                                         ac, oc_op_callback, ac->req);
193                 LDB_REQ_SET_LOCATION(child_req);
194         } else {
195                 ret = ldb_build_mod_req(&child_req, ldb, ac,
196                                         msg, ac->req->controls,
197                                         ac, oc_op_callback, ac->req);
198                 LDB_REQ_SET_LOCATION(child_req);
199         }
200         if (ret != LDB_SUCCESS) {
201                 return ret;
202         }
203
204         return ldb_next_request(ac->module, child_req);
205 }
206
207 /*
208   these are attributes which are left over from old ways of doing
209   things in ldb, and are harmless
210  */
211 static const char *harmless_attrs[] = { "parentGUID", NULL };
212
213 static int attr_handler2(struct oc_context *ac)
214 {
215         struct ldb_context *ldb;
216         struct ldb_message_element *oc_element;
217         struct ldb_message *msg;
218         const char **must_contain, **may_contain, **found_must_contain;
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 preceding "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         must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
238                                                 DSDB_SCHEMA_ALL_MUST);
239         may_contain =  dsdb_full_attribute_list(ac, ac->schema, oc_element,
240                                                 DSDB_SCHEMA_ALL_MAY);
241         found_must_contain = const_str_list(str_list_copy(ac, must_contain));
242         if ((must_contain == NULL) || (may_contain == NULL)
243             || (found_must_contain == NULL)) {
244                 return ldb_operr(ldb);
245         }
246
247         /* Check if all specified attributes are valid in the given
248          * objectclasses and if they meet additional schema restrictions. */
249         msg = ac->search_res->message;
250         for (i = 0; i < msg->num_elements; i++) {
251                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
252                                                          msg->elements[i].name);
253                 if (attr == NULL) {
254                         return ldb_operr(ldb);
255                 }
256
257                 /* Check if they're single-valued if this is requested */
258                 if ((msg->elements[i].num_values > 1) && (attr->isSingleValued)) {
259                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is single-valued!",
260                                                msg->elements[i].name,
261                                                ldb_dn_get_linearized(msg->dn));
262                         if (ac->req->operation == LDB_ADD) {
263                                 return LDB_ERR_CONSTRAINT_VIOLATION;
264                         } else {
265                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
266                         }
267                 }
268
269                 /* We can use "str_list_check" with "strcmp" here since the
270                  * attribute informations from the schema are always equal
271                  * up-down-cased. */
272                 found = str_list_check(must_contain, attr->lDAPDisplayName);
273                 if (found) {
274                         str_list_remove(found_must_contain, attr->lDAPDisplayName);
275                 } else {
276                         found = str_list_check(may_contain, attr->lDAPDisplayName);
277                 }
278                 if (!found) {
279                         found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
280                 }
281                 if (!found) {
282                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
283                                                msg->elements[i].name,
284                                                ldb_dn_get_linearized(msg->dn));
285                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
286                 }
287         }
288
289         if (found_must_contain[0] != NULL) {
290                 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
291                                        found_must_contain[0],
292                                        ldb_dn_get_linearized(msg->dn));
293                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
294         }
295
296         return ldb_module_done(ac->req, ac->mod_ares->controls,
297                                ac->mod_ares->response, LDB_SUCCESS);
298 }
299
300 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
301 {
302         struct ldb_context *ldb;
303         struct oc_context *ac;
304         int ret;
305
306         ac = talloc_get_type(req->context, struct oc_context);
307         ldb = ldb_module_get_ctx(ac->module);
308
309         if (!ares) {
310                 return ldb_module_done(ac->req, NULL, NULL,
311                                        LDB_ERR_OPERATIONS_ERROR);
312         }
313         if (ares->error != LDB_SUCCESS) {
314                 return ldb_module_done(ac->req, ares->controls,
315                                        ares->response, ares->error);
316         }
317
318         ldb_reset_err_string(ldb);
319
320         switch (ares->type) {
321         case LDB_REPLY_ENTRY:
322                 if (ac->search_res != NULL) {
323                         ldb_set_errstring(ldb, "Too many results");
324                         talloc_free(ares);
325                         return ldb_module_done(ac->req, NULL, NULL,
326                                                LDB_ERR_OPERATIONS_ERROR);
327                 }
328
329                 ac->search_res = talloc_steal(ac, ares);
330                 break;
331
332         case LDB_REPLY_REFERRAL:
333                 /* ignore */
334                 talloc_free(ares);
335                 break;
336
337         case LDB_REPLY_DONE:
338                 talloc_free(ares);
339                 ret = attr_handler2(ac);
340                 if (ret != LDB_SUCCESS) {
341                         return ldb_module_done(ac->req, NULL, NULL, ret);
342                 }
343                 break;
344         }
345
346         return LDB_SUCCESS;
347 }
348
349 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
350 {
351         struct oc_context *ac;
352         struct ldb_context *ldb;
353         struct ldb_request *search_req;
354         struct ldb_dn *base_dn;
355         int ret;
356
357         ac = talloc_get_type(req->context, struct oc_context);
358         ldb = ldb_module_get_ctx(ac->module);
359
360         if (!ares) {
361                 return ldb_module_done(ac->req, NULL, NULL,
362                                        LDB_ERR_OPERATIONS_ERROR);
363         }
364
365         if (ares->type == LDB_REPLY_REFERRAL) {
366                 return ldb_module_send_referral(ac->req, ares->referral);
367         }
368
369         if (ares->error != LDB_SUCCESS) {
370                 return ldb_module_done(ac->req, ares->controls, ares->response,
371                                        ares->error);
372         }
373
374         if (ares->type != LDB_REPLY_DONE) {
375                 talloc_free(ares);
376                 return ldb_module_done(ac->req, NULL, NULL,
377                                        LDB_ERR_OPERATIONS_ERROR);
378         }
379
380         ac->search_res = NULL;
381         ac->mod_ares = talloc_steal(ac, ares);
382
383         /* This looks up all attributes of our just added/modified entry */
384         base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
385                 : ac->req->op.mod.message->dn;
386         ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
387                                    LDB_SCOPE_BASE, "(objectClass=*)",
388                                    NULL, NULL, ac,
389                                    get_search_callback, ac->req);
390         LDB_REQ_SET_LOCATION(search_req);
391         if (ret != LDB_SUCCESS) {
392                 return ldb_module_done(ac->req, NULL, NULL, ret);
393         }
394
395         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
396                                       true, NULL);
397         if (ret != LDB_SUCCESS) {
398                 return ldb_module_done(ac->req, NULL, NULL, ret);
399         }
400
401         ret = ldb_next_request(ac->module, search_req);
402         if (ret != LDB_SUCCESS) {
403                 return ldb_module_done(ac->req, NULL, NULL, ret);
404         }
405
406         /* "ldb_module_done" isn't called here since we need to do additional
407          * checks. It is called at the end of "attr_handler2". */
408         return LDB_SUCCESS;
409 }
410
411 static int objectclass_attrs_add(struct ldb_module *module,
412                                  struct ldb_request *req)
413 {
414         struct ldb_context *ldb;
415         struct oc_context *ac;
416
417         ldb = ldb_module_get_ctx(module);
418
419         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
420
421         /* do not manipulate our control entries */
422         if (ldb_dn_is_special(req->op.add.message->dn)) {
423                 return ldb_next_request(module, req);
424         }
425
426         ac = oc_init_context(module, req);
427         if (ac == NULL) {
428                 return ldb_operr(ldb);
429         }
430
431         /* without schema, there isn't much to do here */
432         if (ac->schema == NULL) {
433                 talloc_free(ac);
434                 return ldb_next_request(module, req);
435         }
436
437         return attr_handler(ac);
438 }
439
440 static int objectclass_attrs_modify(struct ldb_module *module,
441                                     struct ldb_request *req)
442 {
443         struct ldb_context *ldb;
444         struct oc_context *ac;
445
446         ldb = ldb_module_get_ctx(module);
447
448         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
449
450         /* do not manipulate our control entries */
451         if (ldb_dn_is_special(req->op.mod.message->dn)) {
452                 return ldb_next_request(module, req);
453         }
454
455         ac = oc_init_context(module, req);
456         if (ac == NULL) {
457                 return ldb_operr(ldb);
458         }
459
460         /* without schema, there isn't much to do here */
461         if (ac->schema == NULL) {
462                 talloc_free(ac);
463                 return ldb_next_request(module, req);
464         }
465
466         return attr_handler(ac);
467 }
468
469 static const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
470         .name              = "objectclass_attrs",
471         .add               = objectclass_attrs_add,
472         .modify            = objectclass_attrs_modify
473 };
474
475 int ldb_objectclass_attrs_module_init(const char *version)
476 {
477         return ldb_register_module(&ldb_objectclass_attrs_module_ops);
478 }