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_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' contains at least one invalid value!",
145 msg->elements[i].name,
146 ldb_dn_get_linearized(msg->dn));
147 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
151 if ((attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) != 0) {
152 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' is constructed!",
153 msg->elements[i].name,
154 ldb_dn_get_linearized(msg->dn));
155 if (ac->req->operation == LDB_ADD) {
156 return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;
158 return LDB_ERR_CONSTRAINT_VIOLATION;
162 /* "dSHeuristics" syntax check */
163 if (ldb_attr_cmp(attr->lDAPDisplayName, "dSHeuristics") == 0) {
164 ret = oc_validate_dsheuristics(&(msg->elements[i]));
165 if (ret != LDB_SUCCESS) {
170 /* Substitute the attribute name to match in case */
171 msg->elements[i].name = attr->lDAPDisplayName;
174 if (ac->req->operation == LDB_ADD) {
175 ret = ldb_build_add_req(&child_req, ldb, ac,
176 msg, ac->req->controls,
177 ac, oc_op_callback, ac->req);
178 LDB_REQ_SET_LOCATION(child_req);
180 ret = ldb_build_mod_req(&child_req, ldb, ac,
181 msg, ac->req->controls,
182 ac, oc_op_callback, ac->req);
183 LDB_REQ_SET_LOCATION(child_req);
185 if (ret != LDB_SUCCESS) {
189 return ldb_next_request(ac->module, child_req);
193 these are attributes which are left over from old ways of doing
194 things in ldb, and are harmless
196 static const char *harmless_attrs[] = { "parentGUID", NULL };
198 static int attr_handler2(struct oc_context *ac)
200 struct ldb_context *ldb;
201 struct ldb_message_element *oc_element;
202 struct ldb_message *msg;
203 const char **must_contain, **may_contain, **found_must_contain;
204 /* There exists a hardcoded delete-protected attributes list in AD */
205 const char *del_prot_attributes[] = { "nTSecurityDescriptor",
206 "objectSid", "sAMAccountType", "sAMAccountName", "groupType",
207 "primaryGroupID", "userAccountControl", "accountExpires",
208 "badPasswordTime", "badPwdCount", "codePage", "countryCode",
209 "lastLogoff", "lastLogon", "logonCount", "pwdLastSet", NULL },
211 const struct dsdb_attribute *attr;
215 ldb = ldb_module_get_ctx(ac->module);
217 if (ac->search_res == NULL) {
218 return ldb_operr(ldb);
221 /* We rely here on the preceeding "objectclass" LDB module which did
222 * already fix up the objectclass list (inheritance, order...). */
223 oc_element = ldb_msg_find_element(ac->search_res->message,
225 if (oc_element == NULL) {
226 return ldb_operr(ldb);
229 /* LSA-specific object classes are not allowed to be created over LDAP,
230 * so we need to tell if this connection is internal (trusted) or not
233 * Hongwei Sun from Microsoft explains:
234 * The constraint in 3.1.1.5.2.2 MS-ADTS means that LSA objects cannot
235 * be added or modified through the LDAP interface, instead they can
236 * only be handled through LSA Policy API. This is also explained in
237 * 7.1.6.9.7 MS-ADTS as follows:
238 * "Despite being replicated normally between peer DCs in a domain,
239 * the process of creating or manipulating TDOs is specifically
240 * restricted to the LSA Policy APIs, as detailed in [MS-LSAD] section
241 * 3.1.1.5. Unlike other objects in the DS, TDOs may not be created or
242 * manipulated by client machines over the LDAPv3 transport."
244 if (ldb_req_is_untrusted(ac->req)) {
245 for (i = 0; i < oc_element->num_values; i++) {
246 if ((strcmp((char *)oc_element->values[i].data,
248 (strcmp((char *)oc_element->values[i].data,
249 "trustedDomain") == 0)) {
250 ldb_asprintf_errstring(ldb, "objectclass_attrs: LSA objectclasses (entry '%s') cannot be created or changed over LDAP!",
251 ldb_dn_get_linearized(ac->search_res->message->dn));
252 return LDB_ERR_UNWILLING_TO_PERFORM;
257 must_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
258 DSDB_SCHEMA_ALL_MUST);
259 may_contain = dsdb_full_attribute_list(ac, ac->schema, oc_element,
260 DSDB_SCHEMA_ALL_MAY);
261 found_must_contain = const_str_list(str_list_copy(ac, must_contain));
262 if ((must_contain == NULL) || (may_contain == NULL)
263 || (found_must_contain == NULL)) {
264 return ldb_operr(ldb);
267 /* Check the delete-protected attributes list */
268 msg = ac->search_res->message;
269 for (l = del_prot_attributes; *l != NULL; l++) {
270 struct ldb_message_element *el;
272 el = ldb_msg_find_element(ac->msg, *l);
275 * It was not specified in the add or modify,
276 * so it doesn't need to be in the stored record
281 found = str_list_check_ci(must_contain, *l);
283 found = str_list_check_ci(may_contain, *l);
285 if (found && (ldb_msg_find_element(msg, *l) == NULL)) {
286 ldb_asprintf_errstring(ldb, "objectclass_attrs: delete protected attribute '%s' on entry '%s' missing!",
288 ldb_dn_get_linearized(msg->dn));
289 return LDB_ERR_UNWILLING_TO_PERFORM;
293 /* Check if all specified attributes are valid in the given
294 * objectclasses and if they meet additional schema restrictions. */
295 for (i = 0; i < msg->num_elements; i++) {
296 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
297 msg->elements[i].name);
299 return ldb_operr(ldb);
302 /* We can use "str_list_check" with "strcmp" here since the
303 * attribute information from the schema are always equal
305 found = str_list_check(must_contain, attr->lDAPDisplayName);
307 str_list_remove(found_must_contain, attr->lDAPDisplayName);
309 found = str_list_check(may_contain, attr->lDAPDisplayName);
312 found = str_list_check(harmless_attrs, attr->lDAPDisplayName);
315 ldb_asprintf_errstring(ldb, "objectclass_attrs: attribute '%s' on entry '%s' does not exist in the specified objectclasses!",
316 msg->elements[i].name,
317 ldb_dn_get_linearized(msg->dn));
318 return LDB_ERR_OBJECT_CLASS_VIOLATION;
322 if (found_must_contain[0] != NULL) {
323 ldb_asprintf_errstring(ldb, "objectclass_attrs: at least one mandatory attribute ('%s') on entry '%s' wasn't specified!",
324 found_must_contain[0],
325 ldb_dn_get_linearized(msg->dn));
326 return LDB_ERR_OBJECT_CLASS_VIOLATION;
329 return ldb_module_done(ac->req, ac->mod_ares->controls,
330 ac->mod_ares->response, LDB_SUCCESS);
333 static int get_search_callback(struct ldb_request *req, struct ldb_reply *ares)
335 struct ldb_context *ldb;
336 struct oc_context *ac;
339 ac = talloc_get_type(req->context, struct oc_context);
340 ldb = ldb_module_get_ctx(ac->module);
343 return ldb_module_done(ac->req, NULL, NULL,
344 LDB_ERR_OPERATIONS_ERROR);
346 if (ares->error != LDB_SUCCESS) {
347 return ldb_module_done(ac->req, ares->controls,
348 ares->response, ares->error);
351 ldb_reset_err_string(ldb);
353 switch (ares->type) {
354 case LDB_REPLY_ENTRY:
355 if (ac->search_res != NULL) {
356 ldb_set_errstring(ldb, "Too many results");
358 return ldb_module_done(ac->req, NULL, NULL,
359 LDB_ERR_OPERATIONS_ERROR);
362 ac->search_res = talloc_steal(ac, ares);
365 case LDB_REPLY_REFERRAL:
372 ret = attr_handler2(ac);
373 if (ret != LDB_SUCCESS) {
374 return ldb_module_done(ac->req, NULL, NULL, ret);
382 static int oc_op_callback(struct ldb_request *req, struct ldb_reply *ares)
384 struct oc_context *ac;
385 struct ldb_context *ldb;
386 struct ldb_request *search_req;
387 struct ldb_dn *base_dn;
390 ac = talloc_get_type(req->context, struct oc_context);
391 ldb = ldb_module_get_ctx(ac->module);
394 return ldb_module_done(ac->req, NULL, NULL,
395 LDB_ERR_OPERATIONS_ERROR);
398 if (ares->type == LDB_REPLY_REFERRAL) {
399 return ldb_module_send_referral(ac->req, ares->referral);
402 if (ares->error != LDB_SUCCESS) {
403 return ldb_module_done(ac->req, ares->controls, ares->response,
407 if (ares->type != LDB_REPLY_DONE) {
409 return ldb_module_done(ac->req, NULL, NULL,
410 LDB_ERR_OPERATIONS_ERROR);
413 ac->search_res = NULL;
414 ac->mod_ares = talloc_steal(ac, ares);
416 /* This looks up all attributes of our just added/modified entry */
417 base_dn = ac->req->operation == LDB_ADD ? ac->req->op.add.message->dn
418 : ac->req->op.mod.message->dn;
419 ret = ldb_build_search_req(&search_req, ldb, ac, base_dn,
420 LDB_SCOPE_BASE, "(objectClass=*)",
422 get_search_callback, ac->req);
423 LDB_REQ_SET_LOCATION(search_req);
424 if (ret != LDB_SUCCESS) {
425 return ldb_module_done(ac->req, NULL, NULL, ret);
428 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
430 if (ret != LDB_SUCCESS) {
431 return ldb_module_done(ac->req, NULL, NULL, ret);
434 ret = ldb_next_request(ac->module, search_req);
435 if (ret != LDB_SUCCESS) {
436 return ldb_module_done(ac->req, NULL, NULL, ret);
439 /* "ldb_module_done" isn't called here since we need to do additional
440 * checks. It is called at the end of "attr_handler2". */
444 static int objectclass_attrs_add(struct ldb_module *module,
445 struct ldb_request *req)
447 struct ldb_context *ldb;
448 struct oc_context *ac;
450 ldb = ldb_module_get_ctx(module);
452 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_add\n");
454 /* do not manipulate our control entries */
455 if (ldb_dn_is_special(req->op.add.message->dn)) {
456 return ldb_next_request(module, req);
459 ac = oc_init_context(module, req);
461 return ldb_operr(ldb);
464 /* without schema, there isn't much to do here */
465 if (ac->schema == NULL) {
467 return ldb_next_request(module, req);
470 return attr_handler(ac);
473 static int objectclass_attrs_modify(struct ldb_module *module,
474 struct ldb_request *req)
476 struct ldb_context *ldb;
477 struct oc_context *ac;
479 ldb = ldb_module_get_ctx(module);
481 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectclass_attrs_modify\n");
483 /* do not manipulate our control entries */
484 if (ldb_dn_is_special(req->op.mod.message->dn)) {
485 return ldb_next_request(module, req);
488 ac = oc_init_context(module, req);
490 return ldb_operr(ldb);
493 /* without schema, there isn't much to do here */
494 if (ac->schema == NULL) {
496 return ldb_next_request(module, req);
499 return attr_handler(ac);
502 static const struct ldb_module_ops ldb_objectclass_attrs_module_ops = {
503 .name = "objectclass_attrs",
504 .add = objectclass_attrs_add,
505 .modify = objectclass_attrs_modify
508 int ldb_objectclass_attrs_module_init(const char *version)
510 LDB_MODULE_CHECK_VERSION(version);
511 return ldb_register_module(&ldb_objectclass_attrs_module_ops);