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_message *msg;
48 struct ldb_reply *search_res;
49 struct ldb_reply *mod_ares;
52 static struct oc_context *oc_init_context(struct ldb_module *module,
53 struct ldb_request *req)
55 struct ldb_context *ldb;
56 struct oc_context *ac;
58 ldb = ldb_module_get_ctx(module);
60 ac = talloc_zero(req, struct oc_context);
68 ac->schema = dsdb_get_schema(ldb, ac);
73 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares);
75 /* checks correctness of dSHeuristics attribute
76 * as described in MS-ADTS 7.1.1.2.4.1.2 dSHeuristics */
77 static int oc_validate_dsheuristics(struct ldb_message_element *el)
79 if (el->num_values > 0) {
80 if (el->values[0].length > DS_HR_LDAP_BYPASS_UPPER_LIMIT_BOUNDS) {
81 return LDB_ERR_CONSTRAINT_VIOLATION;
82 } else if (el->values[0].length >= DS_HR_TENTH_CHAR
83 && el->values[0].data[DS_HR_TENTH_CHAR-1] != '1') {
84 return LDB_ERR_CONSTRAINT_VIOLATION;
91 static int attr_handler(struct oc_context *ac)
93 struct ldb_context *ldb;
94 struct ldb_message *msg;
95 struct ldb_request *child_req;
96 const struct dsdb_attribute *attr;
100 struct dsdb_syntax_ctx syntax_ctx;
102 ldb = ldb_module_get_ctx(ac->module);
104 if (ac->req->operation == LDB_ADD) {
105 msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
107 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 !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
132 /* Odd is for the target. Illegal to modify */
133 ldb_asprintf_errstring(ldb,
134 "objectclass_attrs: attribute '%s' on entry '%s' must not be modified directly, it is a linked attribute",
135 msg->elements[i].name,
136 ldb_dn_get_linearized(msg->dn));
137 return LDB_ERR_UNWILLING_TO_PERFORM;
140 if (!(msg->elements[i].flags & LDB_FLAG_INTERNAL_DISABLE_VALIDATION)) {
141 werr = attr->syntax->validate_ldb(&syntax_ctx, attr,
143 if (!W_ERROR_IS_OK(werr) &&
144 !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
145 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
146 msg->elements[i].name,
147 ldb_dn_get_linearized(msg->dn));
148 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
152 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
153 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
154 msg->elements[i].name,
155 ldb_dn_get_linearized(msg->dn));
156 if (ac->req->operation == LDB_ADD) {
157 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
159 return LDB_ERR_CONSTRAINT_VIOLATION;
163 /* "dSHeuristics" syntax check */
164 if (ldb_attr_cmp(attr->lDAPDisplayName, "dSHeuristics") == 0) {
165 ret = oc_validate_dsheuristics(&(msg->elements[i]));
166 if (ret != LDB_SUCCESS) {
171 /* Substitute the attribute name to match in case */
172 msg->elements[i].name = attr->lDAPDisplayName;
175 if (ac->req->operation == LDB_ADD) {
176 ret = ldb_build_add_req(&child_req, ldb, ac,
177 msg, ac->req->controls,
178 ac, oc_op_callback, ac->req);
179 LDB_REQ_SET_LOCATION(child_req);
181 ret = ldb_build_mod_req(&child_req, ldb, ac,
182 msg, ac->req->controls,
183 ac, oc_op_callback, ac->req);
184 LDB_REQ_SET_LOCATION(child_req);
186 if (ret != LDB_SUCCESS) {
190 return ldb_next_request(ac->module, child_req);
194 these are attributes which are left over from old ways of doing
195 things in ldb, and are harmless
197 static const char *harmless_attrs[] = { "parentGUID", NULL };
199 static int attr_handler2(struct oc_context *ac)
201 struct ldb_context *ldb;
202 struct ldb_message_element *oc_element;
203 struct ldb_message *msg;
204 const char **must_contain, **may_contain, **found_must_contain;
205 /* There exists a hardcoded delete-protected attributes list in AD */
206 const char *del_prot_attributes[] = { "nTSecurityDescriptor",
207 "objectSid", "sAMAccountType", "sAMAccountName", "groupType",
208 "primaryGroupID", "userAccountControl", "accountExpires",
209 "badPasswordTime", "badPwdCount", "codePage", "countryCode",
210 "lastLogoff", "lastLogon", "logonCount", "pwdLastSet", NULL },
212 const struct dsdb_attribute *attr;
216 ldb = ldb_module_get_ctx(ac->module);
218 if (ac->search_res == NULL) {
219 return ldb_operr(ldb);
222 /* We rely here on the preceeding "objectclass" LDB module which did
223 * already fix up the objectclass list (inheritance, order...). */
224 oc_element = ldb_msg_find_element(ac->search_res->message,
226 if (oc_element == NULL) {
227 return ldb_operr(ldb);
230 /* LSA-specific object classes are not allowed to be created over LDAP,
231 * so we need to tell if this connection is internal (trusted) or not
234 * Hongwei Sun from Microsoft explains:
235 * The constraint in 3.1.1.5.2.2 MS-ADTS means that LSA objects cannot
236 * be added or modified through the LDAP interface, instead they can
237 * only be handled through LSA Policy API. This is also explained in
238 * 7.1.6.9.7 MS-ADTS as follows:
239 * "Despite being replicated normally between peer DCs in a domain,
240 * the process of creating or manipulating TDOs is specifically
241 * restricted to the LSA Policy APIs, as detailed in [MS-LSAD] section
242 * 3.1.1.5. Unlike other objects in the DS, TDOs may not be created or
243 * manipulated by client machines over the LDAPv3 transport."
245 if (ldb_req_is_untrusted(ac->req)) {
246 for (i = 0; i < oc_element->num_values; i++) {
247 if ((strcmp((char *)oc_element->values[i].data,
249 (strcmp((char *)oc_element->values[i].data,
250 "trustedDomain") == 0)) {
251 ldb_asprintf_errstring(ldb, "objectclass_attrs: LSA objectclasses (entry '%s') cannot be created or changed over LDAP!",
252 ldb_dn_get_linearized(ac->search_res->message->dn));
253 return LDB_ERR_UNWILLING_TO_PERFORM;
258 must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
259 DSDB_SCHEMA_ALL_MUST);
260 may_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
261 DSDB_SCHEMA_ALL_MAY);
262 found_must_contain = const_str_list(str_list_copy(ac, must_contain));
263 if ((must_contain == NULL) || (may_contain == NULL)
264 || (found_must_contain == NULL)) {
265 return ldb_operr(ldb);
268 /* Check the delete-protected attributes list */
269 msg = ac->search_res->message;
270 for (l = del_prot_attributes; *l != NULL; l++) {
271 struct ldb_message_element *el;
273 el = ldb_msg_find_element(ac->msg, *l);
276 * It was not specified in the add or modify,
277 * so it doesn't need to be in the stored record
282 found = str_list_check_ci(must_contain, *l);
284 found = str_list_check_ci(may_contain, *l);
286 if (found && (ldb_msg_find_element(msg, *l) == NULL)) {
287 ldb_asprintf_errstring(ldb, "objectclass_attrs: delete protected attribute '%s' on entry '%s' missing!",
289 ldb_dn_get_linearized(msg->dn));
290 return LDB_ERR_UNWILLING_TO_PERFORM;
294 /* Check if all specified attributes are valid in the given
295 * objectclasses and if they meet additional schema restrictions. */
296 for (i = 0; i < msg->num_elements; i++) {
297 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
298 msg->elements[i].name);
300 return ldb_operr(ldb);
303 /* We can use "str_list_check" with "strcmp" here since the
304 * attribute information from the schema are always equal
306 found = str_list_check(must_contain, attr->lDAPDisplayName);
308 str_list_remove(found_must_contain, attr->lDAPDisplayName);
310 found = str_list_check(may_contain, attr->lDAPDisplayName);
313 found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
316 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
317 msg->elements[i].name,
318 ldb_dn_get_linearized(msg->dn));
319 return LDB_ERR_OBJECT_CLASS_VIOLATION;
323 if (found_must_contain[0] != NULL) {
324 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
325 found_must_contain[0],
326 ldb_dn_get_linearized(msg->dn));
327 return LDB_ERR_OBJECT_CLASS_VIOLATION;
330 return ldb_module_done(ac->req, ac->mod_ares->controls,
331 ac->mod_ares->response, LDB_SUCCESS);
334 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
336 struct ldb_context *ldb;
337 struct oc_context *ac;
340 ac = talloc_get_type(req->context, struct oc_context);
341 ldb = ldb_module_get_ctx(ac->module);
344 return ldb_module_done(ac->req, NULL, NULL,
345 LDB_ERR_OPERATIONS_ERROR);
347 if (ares->error != LDB_SUCCESS) {
348 return ldb_module_done(ac->req, ares->controls,
349 ares->response, ares->error);
352 ldb_reset_err_string(ldb);
354 switch (ares->type) {
355 case LDB_REPLY_ENTRY:
356 if (ac->search_res != NULL) {
357 ldb_set_errstring(ldb, "Too many results");
359 return ldb_module_done(ac->req, NULL, NULL,
360 LDB_ERR_OPERATIONS_ERROR);
363 ac->search_res = talloc_steal(ac, ares);
366 case LDB_REPLY_REFERRAL:
373 ret = attr_handler2(ac);
374 if (ret != LDB_SUCCESS) {
375 return ldb_module_done(ac->req, NULL, NULL, ret);
383 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
385 struct oc_context *ac;
386 struct ldb_context *ldb;
387 struct ldb_request *search_req;
388 struct ldb_dn *base_dn;
391 ac = talloc_get_type(req->context, struct oc_context);
392 ldb = ldb_module_get_ctx(ac->module);
395 return ldb_module_done(ac->req, NULL, NULL,
396 LDB_ERR_OPERATIONS_ERROR);
399 if (ares->type == LDB_REPLY_REFERRAL) {
400 return ldb_module_send_referral(ac->req, ares->referral);
403 if (ares->error != LDB_SUCCESS) {
404 return ldb_module_done(ac->req, ares->controls, ares->response,
408 if (ares->type != LDB_REPLY_DONE) {
410 return ldb_module_done(ac->req, NULL, NULL,
411 LDB_ERR_OPERATIONS_ERROR);
414 ac->search_res = NULL;
415 ac->mod_ares = talloc_steal(ac, ares);
417 /* This looks up all attributes of our just added/modified entry */
418 base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
419 : ac->req->op.mod.message->dn;
420 ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
421 LDB_SCOPE_BASE, "(objectClass=*)",
423 get_search_callback, ac->req);
424 LDB_REQ_SET_LOCATION(search_req);
425 if (ret != LDB_SUCCESS) {
426 return ldb_module_done(ac->req, NULL, NULL, ret);
429 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
431 if (ret != LDB_SUCCESS) {
432 return ldb_module_done(ac->req, NULL, NULL, ret);
435 ret = ldb_next_request(ac->module, search_req);
436 if (ret != LDB_SUCCESS) {
437 return ldb_module_done(ac->req, NULL, NULL, ret);
440 /* "ldb_module_done" isn't called here since we need to do additional
441 * checks. It is called at the end of "attr_handler2". */
445 static int objectclass_attrs_add(struct ldb_module *module,
446 struct ldb_request *req)
448 struct ldb_context *ldb;
449 struct oc_context *ac;
451 ldb = ldb_module_get_ctx(module);
453 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
455 /* do not manipulate our control entries */
456 if (ldb_dn_is_special(req->op.add.message->dn)) {
457 return ldb_next_request(module, req);
460 ac = oc_init_context(module, req);
462 return ldb_operr(ldb);
465 /* without schema, there isn't much to do here */
466 if (ac->schema == NULL) {
468 return ldb_next_request(module, req);
471 return attr_handler(ac);
474 static int objectclass_attrs_modify(struct ldb_module *module,
475 struct ldb_request *req)
477 struct ldb_context *ldb;
478 struct oc_context *ac;
480 ldb = ldb_module_get_ctx(module);
482 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
484 /* do not manipulate our control entries */
485 if (ldb_dn_is_special(req->op.mod.message->dn)) {
486 return ldb_next_request(module, req);
489 ac = oc_init_context(module, req);
491 return ldb_operr(ldb);
494 /* without schema, there isn't much to do here */
495 if (ac->schema == NULL) {
497 return ldb_next_request(module, req);
500 return attr_handler(ac);
503 static const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
504 .name = "objectclass_attrs",
505 .add = objectclass_attrs_add,
506 .modify = objectclass_attrs_modify
509 int ldb_objectclass_attrs_module_init(const char *version)
511 LDB_MODULE_CHECK_VERSION(version);
512 return ldb_register_module(&ldb_objectclass_attrs_module_ops);