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 static int attr_handler(struct oc_context *ac)
75 struct ldb_context *ldb;
76 struct ldb_message *msg;
77 struct ldb_request *child_req;
78 const struct dsdb_attribute *attr;
83 ldb = ldb_module_get_ctx(ac->module);
85 if (ac->req->operation == LDB_ADD) {
86 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
88 msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
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
97 for (i = 0; i < msg->num_elements; i++) {
98 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
99 msg->elements[i].name);
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;
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;
116 if (!(msg->elements[i].flags & LDB_FLAG_INTERNAL_DISABLE_VALIDATION)) {
117 werr = attr->syntax->validate_ldb(ldb, ac->schema, attr,
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;
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;
134 return LDB_ERR_CONSTRAINT_VIOLATION;
138 /* subsitute the attribute name to match in case */
139 msg->elements[i].name = attr->lDAPDisplayName;
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);
147 ret = ldb_build_mod_req(&child_req, ldb, ac,
148 msg, ac->req->controls,
149 ac, oc_op_callback, ac->req);
151 if (ret != LDB_SUCCESS) {
155 return ldb_next_request(ac->module, child_req);
159 these are attributes which are left over from old ways of doing
160 things in ldb, and are harmless
162 static const char *harmless_attrs[] = { "parentGUID", NULL };
164 static int attr_handler2(struct oc_context *ac)
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;
174 ldb = ldb_module_get_ctx(ac->module);
176 if (ac->search_res == NULL) {
177 return ldb_operr(ldb);
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,
184 if (oc_element == NULL) {
185 return ldb_operr(ldb);
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);
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);
205 return ldb_operr(ldb);
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;
216 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
220 /* We can use "str_list_check" with "strcmp" here since the
221 * attribute informations from the schema are always equal
223 found = str_list_check(must_contain, attr->lDAPDisplayName);
225 str_list_remove(found_must_contain, attr->lDAPDisplayName);
227 found = str_list_check(may_contain, attr->lDAPDisplayName);
230 found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
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;
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;
247 return ldb_module_done(ac->req, ac->mod_ares->controls,
248 ac->mod_ares->response, LDB_SUCCESS);
251 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
253 struct ldb_context *ldb;
254 struct oc_context *ac;
257 ac = talloc_get_type(req->context, struct oc_context);
258 ldb = ldb_module_get_ctx(ac->module);
261 return ldb_module_done(ac->req, NULL, NULL,
262 LDB_ERR_OPERATIONS_ERROR);
264 if (ares->error != LDB_SUCCESS) {
265 return ldb_module_done(ac->req, ares->controls,
266 ares->response, ares->error);
269 ldb_reset_err_string(ldb);
271 switch (ares->type) {
272 case LDB_REPLY_ENTRY:
273 if (ac->search_res != NULL) {
274 ldb_set_errstring(ldb, "Too many results");
276 return ldb_module_done(ac->req, NULL, NULL,
277 LDB_ERR_OPERATIONS_ERROR);
280 ac->search_res = talloc_steal(ac, ares);
283 case LDB_REPLY_REFERRAL:
290 ret = attr_handler2(ac);
291 if (ret != LDB_SUCCESS) {
292 return ldb_module_done(ac->req, NULL, NULL, ret);
300 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
302 struct oc_context *ac;
303 struct ldb_context *ldb;
304 struct ldb_request *search_req;
305 struct ldb_dn *base_dn;
308 ac = talloc_get_type(req->context, struct oc_context);
309 ldb = ldb_module_get_ctx(ac->module);
312 return ldb_module_done(ac->req, NULL, NULL,
313 LDB_ERR_OPERATIONS_ERROR);
316 if (ares->type == LDB_REPLY_REFERRAL) {
317 return ldb_module_send_referral(ac->req, ares->referral);
320 if (ares->error != LDB_SUCCESS) {
321 return ldb_module_done(ac->req, ares->controls, ares->response,
325 if (ares->type != LDB_REPLY_DONE) {
327 return ldb_module_done(ac->req, NULL, NULL,
328 LDB_ERR_OPERATIONS_ERROR);
331 ac->search_res = NULL;
332 ac->mod_ares = talloc_steal(ac, ares);
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=*)",
340 get_search_callback, ac->req);
341 if (ret != LDB_SUCCESS) {
342 return ldb_module_done(ac->req, NULL, NULL, ret);
345 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID,
347 if (ret != LDB_SUCCESS) {
348 return ldb_module_done(ac->req, NULL, NULL, ret);
351 ret = ldb_next_request(ac->module, search_req);
352 if (ret != LDB_SUCCESS) {
353 return ldb_module_done(ac->req, NULL, NULL, ret);
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". */
361 static int objectclass_attrs_add(struct ldb_module *module,
362 struct ldb_request *req)
364 struct ldb_context *ldb;
365 struct oc_context *ac;
367 ldb = ldb_module_get_ctx(module);
369 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
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);
376 ac = oc_init_context(module, req);
378 return ldb_operr(ldb);
381 /* without schema, there isn't much to do here */
382 if (ac->schema == NULL) {
384 return ldb_next_request(module, req);
387 return attr_handler(ac);
390 static int objectclass_attrs_modify(struct ldb_module *module,
391 struct ldb_request *req)
393 struct ldb_context *ldb;
394 struct oc_context *ac;
396 ldb = ldb_module_get_ctx(module);
398 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
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);
405 ac = oc_init_context(module, req);
407 return ldb_operr(ldb);
410 /* without schema, there isn't much to do here */
411 if (ac->schema == NULL) {
413 return ldb_next_request(module, req);
416 return attr_handler(ac);
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