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
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.
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.
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/>.
26 * Component: objectclass attribute checking module
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.
33 * Author: Matthias Dieter Wallnöfer
37 #include "ldb_module.h"
38 #include "dsdb/samdb/samdb.h"
42 struct ldb_module *module;
43 struct ldb_request *req;
44 const struct dsdb_schema *schema;
46 struct ldb_reply *search_res;
47 struct ldb_reply *mod_ares;
50 static struct oc_context *oc_init_context(struct ldb_module *module,
51 struct ldb_request *req)
53 struct ldb_context *ldb;
54 struct oc_context *ac;
56 ldb = ldb_module_get_ctx(module);
58 ac = talloc_zero(req, struct oc_context);
66 ac->schema = dsdb_get_schema(ldb, ac);
71 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares);
73 /* checks correctness of dSHeuristics attribute
74 * as described in MS-ADTS 7.1.1.2.4.1.2 dSHeuristics */
76 static int oc_validate_dsheuristics(struct ldb_message_element *el)
78 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE ||
82 if (el->values[0].length > DS_HR_LDAP_BYPASS_UPPER_LIMIT_BOUNDS) {
83 return LDB_ERR_CONSTRAINT_VIOLATION;
84 } else if (el->values[0].length >= DS_HR_TENTH_CHAR
85 && el->values[0].data[DS_HR_TENTH_CHAR-1] != '1') {
86 return LDB_ERR_CONSTRAINT_VIOLATION;
92 static int attr_handler(struct oc_context *ac)
94 struct ldb_context *ldb;
95 struct ldb_message *msg;
96 struct ldb_request *child_req;
97 const struct dsdb_attribute *attr;
101 struct dsdb_syntax_ctx syntax_ctx;
103 ldb = ldb_module_get_ctx(ac->module);
105 if (ac->req->operation == LDB_ADD) {
106 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
108 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
114 /* initialize syntax checking context */
115 dsdb_syntax_ctx_init(&syntax_ctx, ldb, ac->schema);
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
120 for (i = 0; i < msg->num_elements; i++) {
121 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
122 msg->elements[i].name);
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;
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;
139 if (!(msg->elements[i].flags & LDB_FLAG_INTERNAL_DISABLE_VALIDATION)) {
140 werr = attr->syntax->validate_ldb(&syntax_ctx, attr,
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;
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;
157 return LDB_ERR_CONSTRAINT_VIOLATION;
161 /* "description" on AD is very special: it's nearly single-
162 * valued (only on add operations it isn't). */
163 if ((ac->req->operation == LDB_MODIFY) &&
164 (ldb_attr_cmp(attr->lDAPDisplayName, "description") == 0)) {
165 /* Multi-valued add or replace operations are always
167 if ((LDB_FLAG_MOD_TYPE(msg->elements[i].flags)
168 != LDB_FLAG_MOD_DELETE) &&
169 (msg->elements[i].num_values > 1)) {
170 ldb_asprintf_errstring(ldb,
171 "objectclass_attrs: attribute '%s' on entry '%s' is changed using a multi-valued add or replace operation!",
172 msg->elements[i].name,
173 ldb_dn_get_linearized(msg->dn));
174 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
177 /* Add operations are only allowed if no value exists */
178 if (LDB_FLAG_MOD_TYPE(msg->elements[i].flags)
179 == LDB_FLAG_MOD_ADD) {
180 const char *attrs[] = { attr->lDAPDisplayName,
182 struct ldb_result *res;
183 struct ldb_message_element *el;
185 ret = ldb_search(ldb, ac, &res, msg->dn,
186 LDB_SCOPE_BASE, attrs, NULL);
187 if (ret != LDB_SUCCESS) {
191 el = ldb_msg_find_element(res->msgs[0],
192 attr->lDAPDisplayName);
194 ldb_asprintf_errstring(ldb,
195 "objectclass_attrs: attribute '%s' on entry '%s' is changed using an add operation, but there a value already exists!",
196 msg->elements[i].name,
197 ldb_dn_get_linearized(msg->dn));
198 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
203 /* dSHeuristics syntax check */
204 if ((ac->req->operation == LDB_MODIFY) &&
205 (ldb_attr_cmp(attr->lDAPDisplayName, "dSHeuristics") == 0)) {
206 ret = oc_validate_dsheuristics(&(msg->elements[i]));
207 if (ret != LDB_SUCCESS) {
211 /* Substitute the attribute name to match in case */
212 msg->elements[i].name = attr->lDAPDisplayName;
215 if (ac->req->operation == LDB_ADD) {
216 ret = ldb_build_add_req(&child_req, ldb, ac,
217 msg, ac->req->controls,
218 ac, oc_op_callback, ac->req);
219 LDB_REQ_SET_LOCATION(child_req);
221 ret = ldb_build_mod_req(&child_req, ldb, ac,
222 msg, ac->req->controls,
223 ac, oc_op_callback, ac->req);
224 LDB_REQ_SET_LOCATION(child_req);
226 if (ret != LDB_SUCCESS) {
230 return ldb_next_request(ac->module, child_req);
234 these are attributes which are left over from old ways of doing
235 things in ldb, and are harmless
237 static const char *harmless_attrs[] = { "parentGUID", NULL };
239 static int attr_handler2(struct oc_context *ac)
241 struct ldb_context *ldb;
242 struct ldb_message_element *oc_element;
243 struct ldb_message *msg;
244 const char **must_contain, **may_contain, **found_must_contain;
245 const struct dsdb_attribute *attr;
249 ldb = ldb_module_get_ctx(ac->module);
251 if (ac->search_res == NULL) {
252 return ldb_operr(ldb);
255 /* We rely here on the preceding "objectclass" LDB module which did
256 * already fix up the objectclass list (inheritance, order...). */
257 oc_element = ldb_msg_find_element(ac->search_res->message,
259 if (oc_element == NULL) {
260 return ldb_operr(ldb);
263 must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
264 DSDB_SCHEMA_ALL_MUST);
265 may_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
266 DSDB_SCHEMA_ALL_MAY);
267 found_must_contain = const_str_list(str_list_copy(ac, must_contain));
268 if ((must_contain == NULL) || (may_contain == NULL)
269 || (found_must_contain == NULL)) {
270 return ldb_operr(ldb);
273 /* Check if all specified attributes are valid in the given
274 * objectclasses and if they meet additional schema restrictions. */
275 msg = ac->search_res->message;
276 for (i = 0; i < msg->num_elements; i++) {
277 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
278 msg->elements[i].name);
280 return ldb_operr(ldb);
283 /* Check if they're single-valued if this is requested */
284 if ((msg->elements[i].num_values > 1) && (attr->isSingleValued)) {
285 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is single-valued!",
286 msg->elements[i].name,
287 ldb_dn_get_linearized(msg->dn));
288 if (ac->req->operation == LDB_ADD) {
289 return LDB_ERR_CONSTRAINT_VIOLATION;
291 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
295 /* We can use "str_list_check" with "strcmp" here since the
296 * attribute informations from the schema are always equal
298 found = str_list_check(must_contain, attr->lDAPDisplayName);
300 str_list_remove(found_must_contain, attr->lDAPDisplayName);
302 found = str_list_check(may_contain, attr->lDAPDisplayName);
305 found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
308 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
309 msg->elements[i].name,
310 ldb_dn_get_linearized(msg->dn));
311 return LDB_ERR_OBJECT_CLASS_VIOLATION;
315 if (found_must_contain[0] != NULL) {
316 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
317 found_must_contain[0],
318 ldb_dn_get_linearized(msg->dn));
319 return LDB_ERR_OBJECT_CLASS_VIOLATION;
322 return ldb_module_done(ac->req, ac->mod_ares->controls,
323 ac->mod_ares->response, LDB_SUCCESS);
326 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
328 struct ldb_context *ldb;
329 struct oc_context *ac;
332 ac = talloc_get_type(req->context, struct oc_context);
333 ldb = ldb_module_get_ctx(ac->module);
336 return ldb_module_done(ac->req, NULL, NULL,
337 LDB_ERR_OPERATIONS_ERROR);
339 if (ares->error != LDB_SUCCESS) {
340 return ldb_module_done(ac->req, ares->controls,
341 ares->response, ares->error);
344 ldb_reset_err_string(ldb);
346 switch (ares->type) {
347 case LDB_REPLY_ENTRY:
348 if (ac->search_res != NULL) {
349 ldb_set_errstring(ldb, "Too many results");
351 return ldb_module_done(ac->req, NULL, NULL,
352 LDB_ERR_OPERATIONS_ERROR);
355 ac->search_res = talloc_steal(ac, ares);
358 case LDB_REPLY_REFERRAL:
365 ret = attr_handler2(ac);
366 if (ret != LDB_SUCCESS) {
367 return ldb_module_done(ac->req, NULL, NULL, ret);
375 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
377 struct oc_context *ac;
378 struct ldb_context *ldb;
379 struct ldb_request *search_req;
380 struct ldb_dn *base_dn;
383 ac = talloc_get_type(req->context, struct oc_context);
384 ldb = ldb_module_get_ctx(ac->module);
387 return ldb_module_done(ac->req, NULL, NULL,
388 LDB_ERR_OPERATIONS_ERROR);
391 if (ares->type == LDB_REPLY_REFERRAL) {
392 return ldb_module_send_referral(ac->req, ares->referral);
395 if (ares->error != LDB_SUCCESS) {
396 return ldb_module_done(ac->req, ares->controls, ares->response,
400 if (ares->type != LDB_REPLY_DONE) {
402 return ldb_module_done(ac->req, NULL, NULL,
403 LDB_ERR_OPERATIONS_ERROR);
406 ac->search_res = NULL;
407 ac->mod_ares = talloc_steal(ac, ares);
409 /* This looks up all attributes of our just added/modified entry */
410 base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
411 : ac->req->op.mod.message->dn;
412 ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
413 LDB_SCOPE_BASE, "(objectClass=*)",
415 get_search_callback, ac->req);
416 LDB_REQ_SET_LOCATION(search_req);
417 if (ret != LDB_SUCCESS) {
418 return ldb_module_done(ac->req, NULL, NULL, ret);
421 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
423 if (ret != LDB_SUCCESS) {
424 return ldb_module_done(ac->req, NULL, NULL, ret);
427 ret = ldb_next_request(ac->module, search_req);
428 if (ret != LDB_SUCCESS) {
429 return ldb_module_done(ac->req, NULL, NULL, ret);
432 /* "ldb_module_done" isn't called here since we need to do additional
433 * checks. It is called at the end of "attr_handler2". */
437 static int objectclass_attrs_add(struct ldb_module *module,
438 struct ldb_request *req)
440 struct ldb_context *ldb;
441 struct oc_context *ac;
443 ldb = ldb_module_get_ctx(module);
445 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
447 /* do not manipulate our control entries */
448 if (ldb_dn_is_special(req->op.add.message->dn)) {
449 return ldb_next_request(module, req);
452 ac = oc_init_context(module, req);
454 return ldb_operr(ldb);
457 /* without schema, there isn't much to do here */
458 if (ac->schema == NULL) {
460 return ldb_next_request(module, req);
463 return attr_handler(ac);
466 static int objectclass_attrs_modify(struct ldb_module *module,
467 struct ldb_request *req)
469 struct ldb_context *ldb;
470 struct oc_context *ac;
472 ldb = ldb_module_get_ctx(module);
474 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
476 /* do not manipulate our control entries */
477 if (ldb_dn_is_special(req->op.mod.message->dn)) {
478 return ldb_next_request(module, req);
481 ac = oc_init_context(module, req);
483 return ldb_operr(ldb);
486 /* without schema, there isn't much to do here */
487 if (ac->schema == NULL) {
489 return ldb_next_request(module, req);
492 return attr_handler(ac);
495 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
496 .name = "objectclass_attrs",
497 .add = objectclass_attrs_add,
498 .modify = objectclass_attrs_modify