ldb: mark the location of a lot more ldb requests
[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                 /* Substitute the attribute name to match in case */
143                 msg->elements[i].name = attr->lDAPDisplayName;
144         }
145
146         if (ac->req->operation == LDB_ADD) {
147                 ret = ldb_build_add_req(&child_req, ldb, ac,
148                                         msg, ac->req->controls,
149                                         ac, oc_op_callback, ac->req);
150                 LDB_REQ_SET_LOCATION(child_req);
151         } else {
152                 ret = ldb_build_mod_req(&child_req, ldb, ac,
153                                         msg, ac->req->controls,
154                                         ac, oc_op_callback, ac->req);
155                 LDB_REQ_SET_LOCATION(child_req);
156         }
157         if (ret != LDB_SUCCESS) {
158                 return ret;
159         }
160
161         return ldb_next_request(ac->module, child_req);
162 }
163
164 /*
165   these are attributes which are left over from old ways of doing
166   things in ldb, and are harmless
167  */
168 static const char *harmless_attrs[] = { "parentGUID", NULL };
169
170 static int attr_handler2(struct oc_context *ac)
171 {
172         struct ldb_context *ldb;
173         struct ldb_message_element *oc_element;
174         struct ldb_message *msg;
175         const char **must_contain, **may_contain, **found_must_contain;
176         const struct dsdb_attribute *attr;
177         unsigned int i;
178         bool found;
179
180         ldb = ldb_module_get_ctx(ac->module);
181
182         if (ac->search_res == NULL) {
183                 return ldb_operr(ldb);
184         }
185
186         /* We rely here on the preceding "objectclass" LDB module which did
187          * already fix up the objectclass list (inheritance, order...). */
188         oc_element = ldb_msg_find_element(ac->search_res->message,
189                                           "objectClass");
190         if (oc_element == NULL) {
191                 return ldb_operr(ldb);
192         }
193
194         must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
195                                                 DSDB_SCHEMA_ALL_MUST);
196         may_contain =  dsdb_full_attribute_list(ac, ac->schema, oc_element,
197                                                 DSDB_SCHEMA_ALL_MAY);
198         found_must_contain = const_str_list(str_list_copy(ac, must_contain));
199         if ((must_contain == NULL) || (may_contain == NULL)
200             || (found_must_contain == NULL)) {
201                 return ldb_operr(ldb);
202         }
203
204         /* Check if all specified attributes are valid in the given
205          * objectclasses and if they meet additional schema restrictions. */
206         msg = ac->search_res->message;
207         for (i = 0; i < msg->num_elements; i++) {
208                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
209                                                          msg->elements[i].name);
210                 if (attr == NULL) {
211                         return ldb_operr(ldb);
212                 }
213
214                 /* Check if they're single-valued if this is requested */
215                 if ((msg->elements[i].num_values > 1) && (attr->isSingleValued)) {
216                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is single-valued!",
217                                                msg->elements[i].name,
218                                                ldb_dn_get_linearized(msg->dn));
219                         if (ac->req->operation == LDB_ADD) {
220                                 return LDB_ERR_CONSTRAINT_VIOLATION;
221                         } else {
222                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
223                         }
224                 }
225
226                 /* We can use "str_list_check" with "strcmp" here since the
227                  * attribute informations from the schema are always equal
228                  * up-down-cased. */
229                 found = str_list_check(must_contain, attr->lDAPDisplayName);
230                 if (found) {
231                         str_list_remove(found_must_contain, attr->lDAPDisplayName);
232                 } else {
233                         found = str_list_check(may_contain, attr->lDAPDisplayName);
234                 }
235                 if (!found) {
236                         found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
237                 }
238                 if (!found) {
239                         ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
240                                                msg->elements[i].name,
241                                                ldb_dn_get_linearized(msg->dn));
242                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
243                 }
244         }
245
246         if (found_must_contain[0] != NULL) {
247                 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
248                                        found_must_contain[0],
249                                        ldb_dn_get_linearized(msg->dn));
250                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
251         }
252
253         return ldb_module_done(ac->req, ac->mod_ares->controls,
254                                ac->mod_ares->response, LDB_SUCCESS);
255 }
256
257 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
258 {
259         struct ldb_context *ldb;
260         struct oc_context *ac;
261         int ret;
262
263         ac = talloc_get_type(req->context, struct oc_context);
264         ldb = ldb_module_get_ctx(ac->module);
265
266         if (!ares) {
267                 return ldb_module_done(ac->req, NULL, NULL,
268                                        LDB_ERR_OPERATIONS_ERROR);
269         }
270         if (ares->error != LDB_SUCCESS) {
271                 return ldb_module_done(ac->req, ares->controls,
272                                        ares->response, ares->error);
273         }
274
275         ldb_reset_err_string(ldb);
276
277         switch (ares->type) {
278         case LDB_REPLY_ENTRY:
279                 if (ac->search_res != NULL) {
280                         ldb_set_errstring(ldb, "Too many results");
281                         talloc_free(ares);
282                         return ldb_module_done(ac->req, NULL, NULL,
283                                                LDB_ERR_OPERATIONS_ERROR);
284                 }
285
286                 ac->search_res = talloc_steal(ac, ares);
287                 break;
288
289         case LDB_REPLY_REFERRAL:
290                 /* ignore */
291                 talloc_free(ares);
292                 break;
293
294         case LDB_REPLY_DONE:
295                 talloc_free(ares);
296                 ret = attr_handler2(ac);
297                 if (ret != LDB_SUCCESS) {
298                         return ldb_module_done(ac->req, NULL, NULL, ret);
299                 }
300                 break;
301         }
302
303         return LDB_SUCCESS;
304 }
305
306 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
307 {
308         struct oc_context *ac;
309         struct ldb_context *ldb;
310         struct ldb_request *search_req;
311         struct ldb_dn *base_dn;
312         int ret;
313
314         ac = talloc_get_type(req->context, struct oc_context);
315         ldb = ldb_module_get_ctx(ac->module);
316
317         if (!ares) {
318                 return ldb_module_done(ac->req, NULL, NULL,
319                                        LDB_ERR_OPERATIONS_ERROR);
320         }
321
322         if (ares->type == LDB_REPLY_REFERRAL) {
323                 return ldb_module_send_referral(ac->req, ares->referral);
324         }
325
326         if (ares->error != LDB_SUCCESS) {
327                 return ldb_module_done(ac->req, ares->controls, ares->response,
328                                        ares->error);
329         }
330
331         if (ares->type != LDB_REPLY_DONE) {
332                 talloc_free(ares);
333                 return ldb_module_done(ac->req, NULL, NULL,
334                                        LDB_ERR_OPERATIONS_ERROR);
335         }
336
337         ac->search_res = NULL;
338         ac->mod_ares = talloc_steal(ac, ares);
339
340         /* This looks up all attributes of our just added/modified entry */
341         base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
342                 : ac->req->op.mod.message->dn;
343         ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
344                                    LDB_SCOPE_BASE, "(objectClass=*)",
345                                    NULL, NULL, ac,
346                                    get_search_callback, ac->req);
347         LDB_REQ_SET_LOCATION(search_req);
348         if (ret != LDB_SUCCESS) {
349                 return ldb_module_done(ac->req, NULL, NULL, ret);
350         }
351
352         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID,
353                                       true, NULL);
354         if (ret != LDB_SUCCESS) {
355                 return ldb_module_done(ac->req, NULL, NULL, ret);
356         }
357
358         ret = ldb_next_request(ac->module, search_req);
359         if (ret != LDB_SUCCESS) {
360                 return ldb_module_done(ac->req, NULL, NULL, ret);
361         }
362
363         /* "ldb_module_done" isn't called here since we need to do additional
364          * checks. It is called at the end of "attr_handler2". */
365         return LDB_SUCCESS;
366 }
367
368 static int objectclass_attrs_add(struct ldb_module *module,
369                                  struct ldb_request *req)
370 {
371         struct ldb_context *ldb;
372         struct oc_context *ac;
373
374         ldb = ldb_module_get_ctx(module);
375
376         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
377
378         /* do not manipulate our control entries */
379         if (ldb_dn_is_special(req->op.add.message->dn)) {
380                 return ldb_next_request(module, req);
381         }
382
383         ac = oc_init_context(module, req);
384         if (ac == NULL) {
385                 return ldb_operr(ldb);
386         }
387
388         /* without schema, there isn't much to do here */
389         if (ac->schema == NULL) {
390                 talloc_free(ac);
391                 return ldb_next_request(module, req);
392         }
393
394         return attr_handler(ac);
395 }
396
397 static int objectclass_attrs_modify(struct ldb_module *module,
398                                     struct ldb_request *req)
399 {
400         struct ldb_context *ldb;
401         struct oc_context *ac;
402
403         ldb = ldb_module_get_ctx(module);
404
405         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
406
407         /* do not manipulate our control entries */
408         if (ldb_dn_is_special(req->op.mod.message->dn)) {
409                 return ldb_next_request(module, req);
410         }
411
412         ac = oc_init_context(module, req);
413         if (ac == NULL) {
414                 return ldb_operr(ldb);
415         }
416
417         /* without schema, there isn't much to do here */
418         if (ac->schema == NULL) {
419                 talloc_free(ac);
420                 return ldb_next_request(module, req);
421         }
422
423         return attr_handler(ac);
424 }
425
426 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
427         .name              = "objectclass_attrs",
428         .add               = objectclass_attrs_add,
429         .modify            = objectclass_attrs_modify
430 };