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 /* 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 /* "dSHeuristics" syntax check */
162 if (ldb_attr_cmp(attr->lDAPDisplayName, "dSHeuristics") == 0) {
163 ret = oc_validate_dsheuristics(&(msg->elements[i]));
164 if (ret != LDB_SUCCESS) {
169 /* Substitute the attribute name to match in case */
170 msg->elements[i].name = attr->lDAPDisplayName;
173 if (ac->req->operation == LDB_ADD) {
174 ret = ldb_build_add_req(&child_req, ldb, ac,
175 msg, ac->req->controls,
176 ac, oc_op_callback, ac->req);
177 LDB_REQ_SET_LOCATION(child_req);
179 ret = ldb_build_mod_req(&child_req, ldb, ac,
180 msg, ac->req->controls,
181 ac, oc_op_callback, ac->req);
182 LDB_REQ_SET_LOCATION(child_req);
184 if (ret != LDB_SUCCESS) {
188 return ldb_next_request(ac->module, child_req);
192 these are attributes which are left over from old ways of doing
193 things in ldb, and are harmless
195 static const char *harmless_attrs[] = { "parentGUID", NULL };
197 static int attr_handler2(struct oc_context *ac)
199 struct ldb_context *ldb;
200 struct ldb_message_element *oc_element;
201 struct ldb_message *msg;
202 const char **must_contain, **may_contain, **found_must_contain;
203 /* There exists a hardcoded delete-protected attributes list in AD */
204 const char *del_prot_attributes[] = { "nTSecurityDescriptor",
205 "objectSid", "sAMAccountType", "sAMAccountName", "groupType",
206 "primaryGroupID", "userAccountControl", "accountExpires",
207 "badPasswordTime", "badPwdCount", "codePage", "countryCode",
208 "lastLogoff", "lastLogon", "logonCount", "pwdLastSet", NULL },
210 const struct dsdb_attribute *attr;
214 ldb = ldb_module_get_ctx(ac->module);
216 if (ac->search_res == NULL) {
217 return ldb_operr(ldb);
220 /* We rely here on the preceeding "objectclass" LDB module which did
221 * already fix up the objectclass list (inheritance, order...). */
222 oc_element = ldb_msg_find_element(ac->search_res->message,
224 if (oc_element == NULL) {
225 return ldb_operr(ldb);
228 /* LSA-specific object classes are not allowed to be created over LDAP,
229 * so we need to tell if this connection is internal (trusted) or not
232 * Hongwei Sun from Microsoft explains:
233 * The constraint in 3.1.1.5.2.2 MS-ADTS means that LSA objects cannot
234 * be added or modified through the LDAP interface, instead they can
235 * only be handled through LSA Policy API. This is also explained in
236 * 7.1.6.9.7 MS-ADTS as follows:
237 * "Despite being replicated normally between peer DCs in a domain,
238 * the process of creating or manipulating TDOs is specifically
239 * restricted to the LSA Policy APIs, as detailed in [MS-LSAD] section
240 * 3.1.1.5. Unlike other objects in the DS, TDOs may not be created or
241 * manipulated by client machines over the LDAPv3 transport."
243 if (ldb_req_is_untrusted(ac->req)) {
244 for (i = 0; i < oc_element->num_values; i++) {
245 if ((strcmp((char *)oc_element->values[i].data,
247 (strcmp((char *)oc_element->values[i].data,
248 "trustedDomain") == 0)) {
249 ldb_asprintf_errstring(ldb, "objectclass_attrs: LSA objectclasses (entry '%s') cannot be created or changed over LDAP!",
250 ldb_dn_get_linearized(ac->search_res->message->dn));
251 return LDB_ERR_UNWILLING_TO_PERFORM;
256 must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
257 DSDB_SCHEMA_ALL_MUST);
258 may_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
259 DSDB_SCHEMA_ALL_MAY);
260 found_must_contain = const_str_list(str_list_copy(ac, must_contain));
261 if ((must_contain == NULL) || (may_contain == NULL)
262 || (found_must_contain == NULL)) {
263 return ldb_operr(ldb);
266 /* Check the delete-protected attributes list */
267 msg = ac->search_res->message;
268 for (l = del_prot_attributes; *l != NULL; l++) {
269 struct ldb_message_element *el;
271 el = ldb_msg_find_element(ac->msg, *l);
274 * It was not specified in the add or modify,
275 * so it doesn't need to be in the stored record
280 found = str_list_check_ci(must_contain, *l);
282 found = str_list_check_ci(may_contain, *l);
284 if (found && (ldb_msg_find_element(msg, *l) == NULL)) {
285 ldb_asprintf_errstring(ldb, "objectclass_attrs: delete protected attribute '%s' on entry '%s' missing!",
287 ldb_dn_get_linearized(msg->dn));
288 return LDB_ERR_UNWILLING_TO_PERFORM;
292 /* Check if all specified attributes are valid in the given
293 * objectclasses and if they meet additional schema restrictions. */
294 for (i = 0; i < msg->num_elements; i++) {
295 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
296 msg->elements[i].name);
298 return ldb_operr(ldb);
301 /* We can use "str_list_check" with "strcmp" here since the
302 * attribute information from the schema are always equal
304 found = str_list_check(must_contain, attr->lDAPDisplayName);
306 str_list_remove(found_must_contain, attr->lDAPDisplayName);
308 found = str_list_check(may_contain, attr->lDAPDisplayName);
311 found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
314 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
315 msg->elements[i].name,
316 ldb_dn_get_linearized(msg->dn));
317 return LDB_ERR_OBJECT_CLASS_VIOLATION;
321 if (found_must_contain[0] != NULL) {
322 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
323 found_must_contain[0],
324 ldb_dn_get_linearized(msg->dn));
325 return LDB_ERR_OBJECT_CLASS_VIOLATION;
328 return ldb_module_done(ac->req, ac->mod_ares->controls,
329 ac->mod_ares->response, LDB_SUCCESS);
332 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
334 struct ldb_context *ldb;
335 struct oc_context *ac;
338 ac = talloc_get_type(req->context, struct oc_context);
339 ldb = ldb_module_get_ctx(ac->module);
342 return ldb_module_done(ac->req, NULL, NULL,
343 LDB_ERR_OPERATIONS_ERROR);
345 if (ares->error != LDB_SUCCESS) {
346 return ldb_module_done(ac->req, ares->controls,
347 ares->response, ares->error);
350 ldb_reset_err_string(ldb);
352 switch (ares->type) {
353 case LDB_REPLY_ENTRY:
354 if (ac->search_res != NULL) {
355 ldb_set_errstring(ldb, "Too many results");
357 return ldb_module_done(ac->req, NULL, NULL,
358 LDB_ERR_OPERATIONS_ERROR);
361 ac->search_res = talloc_steal(ac, ares);
364 case LDB_REPLY_REFERRAL:
371 ret = attr_handler2(ac);
372 if (ret != LDB_SUCCESS) {
373 return ldb_module_done(ac->req, NULL, NULL, ret);
381 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
383 struct oc_context *ac;
384 struct ldb_context *ldb;
385 struct ldb_request *search_req;
386 struct ldb_dn *base_dn;
389 ac = talloc_get_type(req->context, struct oc_context);
390 ldb = ldb_module_get_ctx(ac->module);
393 return ldb_module_done(ac->req, NULL, NULL,
394 LDB_ERR_OPERATIONS_ERROR);
397 if (ares->type == LDB_REPLY_REFERRAL) {
398 return ldb_module_send_referral(ac->req, ares->referral);
401 if (ares->error != LDB_SUCCESS) {
402 return ldb_module_done(ac->req, ares->controls, ares->response,
406 if (ares->type != LDB_REPLY_DONE) {
408 return ldb_module_done(ac->req, NULL, NULL,
409 LDB_ERR_OPERATIONS_ERROR);
412 ac->search_res = NULL;
413 ac->mod_ares = talloc_steal(ac, ares);
415 /* This looks up all attributes of our just added/modified entry */
416 base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
417 : ac->req->op.mod.message->dn;
418 ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
419 LDB_SCOPE_BASE, "(objectClass=*)",
421 get_search_callback, ac->req);
422 LDB_REQ_SET_LOCATION(search_req);
423 if (ret != LDB_SUCCESS) {
424 return ldb_module_done(ac->req, NULL, NULL, ret);
427 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
429 if (ret != LDB_SUCCESS) {
430 return ldb_module_done(ac->req, NULL, NULL, ret);
433 ret = ldb_next_request(ac->module, search_req);
434 if (ret != LDB_SUCCESS) {
435 return ldb_module_done(ac->req, NULL, NULL, ret);
438 /* "ldb_module_done" isn't called here since we need to do additional
439 * checks. It is called at the end of "attr_handler2". */
443 static int objectclass_attrs_add(struct ldb_module *module,
444 struct ldb_request *req)
446 struct ldb_context *ldb;
447 struct oc_context *ac;
449 ldb = ldb_module_get_ctx(module);
451 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
453 /* do not manipulate our control entries */
454 if (ldb_dn_is_special(req->op.add.message->dn)) {
455 return ldb_next_request(module, req);
458 ac = oc_init_context(module, req);
460 return ldb_operr(ldb);
463 /* without schema, there isn't much to do here */
464 if (ac->schema == NULL) {
466 return ldb_next_request(module, req);
469 return attr_handler(ac);
472 static int objectclass_attrs_modify(struct ldb_module *module,
473 struct ldb_request *req)
475 struct ldb_context *ldb;
476 struct oc_context *ac;
478 ldb = ldb_module_get_ctx(module);
480 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
482 /* do not manipulate our control entries */
483 if (ldb_dn_is_special(req->op.mod.message->dn)) {
484 return ldb_next_request(module, req);
487 ac = oc_init_context(module, req);
489 return ldb_operr(ldb);
492 /* without schema, there isn't much to do here */
493 if (ac->schema == NULL) {
495 return ldb_next_request(module, req);
498 return attr_handler(ac);
501 static const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
502 .name = "objectclass_attrs",
503 .add = objectclass_attrs_add,
504 .modify = objectclass_attrs_modify
507 int ldb_objectclass_attrs_module_init(const char *version)
509 LDB_MODULE_CHECK_VERSION(version);
510 return ldb_register_module(&ldb_objectclass_attrs_module_ops);