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);
92 return LDB_ERR_OPERATIONS_ERROR;
95 /* Check if attributes exist in the schema, if the values match,
96 * if they're not operational and fix the names to the match the schema
98 for (i = 0; i < msg->num_elements; i++) {
99 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
100 msg->elements[i].name);
102 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' was not found in the schema!",
103 msg->elements[i].name,
104 ldb_dn_get_linearized(msg->dn));
105 return LDB_ERR_NO_SUCH_ATTRIBUTE;
108 werr = attr->syntax->validate_ldb(ldb, ac->schema, attr,
110 if (!W_ERROR_IS_OK(werr)) {
111 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
112 msg->elements[i].name,
113 ldb_dn_get_linearized(msg->dn));
114 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
117 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
118 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
119 msg->elements[i].name,
120 ldb_dn_get_linearized(msg->dn));
121 if (ac->req->operation == LDB_ADD) {
122 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
124 return LDB_ERR_CONSTRAINT_VIOLATION;
128 /* subsitute the attribute name to match in case */
129 msg->elements[i].name = attr->lDAPDisplayName;
132 if (ac->req->operation == LDB_ADD) {
133 ret = ldb_build_add_req(&child_req, ldb, ac,
134 msg, ac->req->controls,
135 ac, oc_op_callback, ac->req);
137 ret = ldb_build_mod_req(&child_req, ldb, ac,
138 msg, ac->req->controls,
139 ac, oc_op_callback, ac->req);
141 if (ret != LDB_SUCCESS) {
145 return ldb_next_request(ac->module, child_req);
148 static int attr_handler2(struct oc_context *ac)
150 struct ldb_context *ldb;
151 struct ldb_message_element *oc_element;
152 struct ldb_message *msg;
153 const char **must_contain, **may_contain, **found_must_contain;
154 const struct dsdb_attribute *attr;
158 ldb = ldb_module_get_ctx(ac->module);
160 if (ac->search_res == NULL) {
161 return LDB_ERR_OPERATIONS_ERROR;
164 /* We rely here on the preceeding "objectclass" LDB module which did
165 * already fix up the objectclass list (inheritance, order...). */
166 oc_element = ldb_msg_find_element(ac->search_res->message,
168 if (oc_element == NULL) {
169 return LDB_ERR_OPERATIONS_ERROR;
172 must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
173 DSDB_SCHEMA_ALL_MUST);
174 may_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
175 DSDB_SCHEMA_ALL_MAY);
176 found_must_contain = const_str_list(str_list_copy(ac, must_contain));
177 if ((must_contain == NULL) || (may_contain == NULL)
178 || (found_must_contain == NULL)) {
179 return LDB_ERR_OPERATIONS_ERROR;
182 /* Check if all specified attributes are valid in the given
184 msg = ac->search_res->message;
185 for (i = 0; i < msg->num_elements; i++) {
186 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
187 msg->elements[i].name);
189 return LDB_ERR_OPERATIONS_ERROR;
192 /* We can use "str_list_check" with "strcmp" here since the
193 * attribute informations from the schema are always equal
195 found = str_list_check(must_contain, attr->lDAPDisplayName);
197 str_list_remove(found_must_contain, attr->lDAPDisplayName);
199 found = str_list_check(may_contain, attr->lDAPDisplayName);
202 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
203 msg->elements[i].name,
204 ldb_dn_get_linearized(msg->dn));
205 return LDB_ERR_OBJECT_CLASS_VIOLATION;
209 if (found_must_contain[0] != NULL) {
210 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
211 found_must_contain[0],
212 ldb_dn_get_linearized(msg->dn));
213 return LDB_ERR_OBJECT_CLASS_VIOLATION;
216 return ldb_module_done(ac->req, ac->mod_ares->controls,
217 ac->mod_ares->response, LDB_SUCCESS);
220 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
222 struct ldb_context *ldb;
223 struct oc_context *ac;
226 ac = talloc_get_type(req->context, struct oc_context);
227 ldb = ldb_module_get_ctx(ac->module);
230 return ldb_module_done(ac->req, NULL, NULL,
231 LDB_ERR_OPERATIONS_ERROR);
233 if (ares->error != LDB_SUCCESS) {
234 return ldb_module_done(ac->req, ares->controls,
235 ares->response, ares->error);
238 ldb_reset_err_string(ldb);
240 switch (ares->type) {
241 case LDB_REPLY_ENTRY:
242 if (ac->search_res != NULL) {
243 ldb_set_errstring(ldb, "Too many results");
245 return ldb_module_done(ac->req, NULL, NULL,
246 LDB_ERR_OPERATIONS_ERROR);
249 ac->search_res = talloc_steal(ac, ares);
252 case LDB_REPLY_REFERRAL:
259 ret = attr_handler2(ac);
260 if (ret != LDB_SUCCESS) {
261 return ldb_module_done(ac->req, NULL, NULL, ret);
269 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
271 struct oc_context *ac;
272 struct ldb_context *ldb;
273 struct ldb_request *search_req;
274 struct ldb_dn *base_dn;
277 ac = talloc_get_type(req->context, struct oc_context);
278 ldb = ldb_module_get_ctx(ac->module);
281 return ldb_module_done(ac->req, NULL, NULL,
282 LDB_ERR_OPERATIONS_ERROR);
285 if (ares->type == LDB_REPLY_REFERRAL) {
286 return ldb_module_send_referral(ac->req, ares->referral);
289 if (ares->error != LDB_SUCCESS) {
290 return ldb_module_done(ac->req, ares->controls, ares->response,
294 if (ares->type != LDB_REPLY_DONE) {
296 return ldb_module_done(ac->req, NULL, NULL,
297 LDB_ERR_OPERATIONS_ERROR);
300 ac->search_res = NULL;
301 ac->mod_ares = talloc_steal(ac, ares);
303 /* This looks up all attributes of our just added/modified entry */
304 base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
305 : ac->req->op.mod.message->dn;
306 ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
307 LDB_SCOPE_BASE, "(objectClass=*)",
309 get_search_callback, ac->req);
310 if (ret != LDB_SUCCESS) {
311 return ldb_module_done(ac->req, NULL, NULL, ret);
314 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID,
316 if (ret != LDB_SUCCESS) {
317 return ldb_module_done(ac->req, NULL, NULL, ret);
320 ret = ldb_next_request(ac->module, search_req);
321 if (ret != LDB_SUCCESS) {
322 return ldb_module_done(ac->req, NULL, NULL, ret);
325 /* "ldb_module_done" isn't called here since we need to do additional
326 * checks. It is called at the end of "attr_handler2". */
330 static int objectclass_attrs_add(struct ldb_module *module,
331 struct ldb_request *req)
333 struct ldb_context *ldb;
334 struct oc_context *ac;
336 ldb = ldb_module_get_ctx(module);
338 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
340 /* do not manipulate our control entries */
341 if (ldb_dn_is_special(req->op.add.message->dn)) {
342 return ldb_next_request(module, req);
345 ac = oc_init_context(module, req);
347 return LDB_ERR_OPERATIONS_ERROR;
350 /* without schema, there isn't much to do here */
351 if (ac->schema == NULL) {
353 return ldb_next_request(module, req);
356 return attr_handler(ac);
359 static int objectclass_attrs_modify(struct ldb_module *module,
360 struct ldb_request *req)
362 struct ldb_context *ldb;
363 struct oc_context *ac;
365 ldb = ldb_module_get_ctx(module);
367 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
369 /* do not manipulate our control entries */
370 if (ldb_dn_is_special(req->op.mod.message->dn)) {
371 return ldb_next_request(module, req);
374 ac = oc_init_context(module, req);
376 return LDB_ERR_OPERATIONS_ERROR;
379 /* without schema, there isn't much to do here */
380 if (ac->schema == NULL) {
382 return ldb_next_request(module, req);
385 return attr_handler(ac);
388 _PUBLIC_ const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
389 .name = "objectclass_attrs",
390 .add = objectclass_attrs_add,
391 .modify = objectclass_attrs_modify