4 Copyright (C) Simo Sorce 2005-2008
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Component: ldb extended dn control module
26 * Description: this module interprets DNs of the form <SID=S-1-2-4456> into normal DNs.
34 #include <ldb_errors.h>
35 #include <ldb_module.h>
36 #include "dsdb/samdb/samdb.h"
37 #include "dsdb/samdb/ldb_modules/util.h"
40 TODO: if relax is not set then we need to reject the fancy RMD_* and
41 DELETED extended DN codes
45 struct extended_search_context {
46 struct ldb_module *module;
47 struct ldb_request *req;
48 struct ldb_dn *basedn;
50 char *wellknown_object;
54 static const char *wkattr[] = {
56 "otherWellKnownObjects",
60 static const struct ldb_module_ops ldb_extended_dn_in_openldap_module_ops;
62 /* An extra layer of indirection because LDB does not allow the original request to be altered */
64 static int extended_final_callback(struct ldb_request *req, struct ldb_reply *ares)
66 int ret = LDB_ERR_OPERATIONS_ERROR;
67 struct extended_search_context *ac;
68 ac = talloc_get_type(req->context, struct extended_search_context);
70 if (ares->error != LDB_SUCCESS) {
71 ret = ldb_module_done(ac->req, ares->controls,
72 ares->response, ares->error);
77 ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
79 case LDB_REPLY_REFERRAL:
81 ret = ldb_module_send_referral(ac->req, ares->referral);
85 ret = ldb_module_done(ac->req, ares->controls,
86 ares->response, ares->error);
93 static int extended_base_callback(struct ldb_request *req, struct ldb_reply *ares)
95 struct extended_search_context *ac;
96 struct ldb_request *down_req;
97 struct ldb_message_element *el;
102 const char *found = NULL;
104 ac = talloc_get_type(req->context, struct extended_search_context);
107 return ldb_module_done(ac->req, NULL, NULL,
108 LDB_ERR_OPERATIONS_ERROR);
110 if (ares->error != LDB_SUCCESS) {
111 return ldb_module_done(ac->req, ares->controls,
112 ares->response, ares->error);
115 switch (ares->type) {
116 case LDB_REPLY_ENTRY:
118 /* we have more than one match! This can
119 happen as S-1-5-17 appears twice in a
120 normal provision. We need to return
122 const char *str = talloc_asprintf(req, "Duplicate base-DN matches found for '%s'",
123 ldb_dn_get_extended_linearized(req, ac->dn, 1));
124 ldb_set_errstring(ldb_module_get_ctx(ac->module), str);
125 return ldb_module_done(ac->req, NULL, NULL,
126 LDB_ERR_NO_SUCH_OBJECT);
129 if (!ac->wellknown_object) {
130 ac->basedn = talloc_steal(ac, ares->message->dn);
134 wkn_len = strlen(ac->wellknown_object);
136 for (j=0; wkattr[j]; j++) {
138 el = ldb_msg_find_element(ares->message, wkattr[j]);
144 for (i=0; i < el->num_values; i++) {
145 valstr = talloc_strndup(ac,
146 (const char *)el->values[i].data,
147 el->values[i].length);
149 ldb_oom(ldb_module_get_ctx(ac->module));
150 return ldb_module_done(ac->req, NULL, NULL,
151 LDB_ERR_OPERATIONS_ERROR);
154 if (strncasecmp(valstr, ac->wellknown_object, wkn_len) != 0) {
159 found = &valstr[wkn_len];
171 ac->basedn = ldb_dn_new(ac, ldb_module_get_ctx(ac->module), found);
174 ldb_oom(ldb_module_get_ctx(ac->module));
175 return ldb_module_done(ac->req, NULL, NULL,
176 LDB_ERR_OPERATIONS_ERROR);
181 case LDB_REPLY_REFERRAL:
187 const char *str = talloc_asprintf(req, "Base-DN '%s' not found",
188 ldb_dn_get_extended_linearized(req, ac->dn, 1));
189 ldb_set_errstring(ldb_module_get_ctx(ac->module), str);
190 return ldb_module_done(ac->req, NULL, NULL,
191 LDB_ERR_NO_SUCH_OBJECT);
194 switch (ac->req->operation) {
196 ret = ldb_build_search_req_ex(&down_req,
197 ldb_module_get_ctx(ac->module), ac->req,
199 ac->req->op.search.scope,
200 ac->req->op.search.tree,
201 ac->req->op.search.attrs,
203 ac, extended_final_callback,
205 LDB_REQ_SET_LOCATION(down_req);
209 struct ldb_message *add_msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
211 ldb_oom(ldb_module_get_ctx(ac->module));
212 return ldb_module_done(ac->req, NULL, NULL,
213 LDB_ERR_OPERATIONS_ERROR);
216 add_msg->dn = ac->basedn;
218 ret = ldb_build_add_req(&down_req,
219 ldb_module_get_ctx(ac->module), ac->req,
222 ac, extended_final_callback,
224 LDB_REQ_SET_LOCATION(down_req);
229 struct ldb_message *mod_msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
231 ldb_oom(ldb_module_get_ctx(ac->module));
232 return ldb_module_done(ac->req, NULL, NULL,
233 LDB_ERR_OPERATIONS_ERROR);
236 mod_msg->dn = ac->basedn;
238 ret = ldb_build_mod_req(&down_req,
239 ldb_module_get_ctx(ac->module), ac->req,
242 ac, extended_final_callback,
244 LDB_REQ_SET_LOCATION(down_req);
248 ret = ldb_build_del_req(&down_req,
249 ldb_module_get_ctx(ac->module), ac->req,
252 ac, extended_final_callback,
254 LDB_REQ_SET_LOCATION(down_req);
257 ret = ldb_build_rename_req(&down_req,
258 ldb_module_get_ctx(ac->module), ac->req,
260 ac->req->op.rename.newdn,
262 ac, extended_final_callback,
264 LDB_REQ_SET_LOCATION(down_req);
267 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
270 if (ret != LDB_SUCCESS) {
271 return ldb_module_done(ac->req, NULL, NULL, ret);
274 return ldb_next_request(ac->module, down_req);
282 windows ldap searchs don't allow a baseDN with more
283 than one extended component, or an extended
284 component and a string DN
286 We only enforce this over ldap, not for internal
287 use, as there are just too many places where we
288 internally want to use a DN that has come from a
289 search with extended DN enabled, or comes from a DRS
292 Enforcing this would also make debugging samba much
293 harder, as we'd need to use ldb_dn_minimise() in a
294 lot of places, and that would lose the DN string
295 which is so useful for working out what a request is
298 static bool ldb_dn_match_allowed(struct ldb_dn *dn, struct ldb_request *req)
300 int num_components = ldb_dn_get_comp_num(dn);
301 int num_ex_components = ldb_dn_get_extended_comp_num(dn);
303 if (num_ex_components == 0) {
307 if ((num_components != 0 || num_ex_components != 1) &&
308 ldb_req_is_untrusted(req)) {
315 struct extended_dn_filter_ctx {
318 struct ldb_module *module;
319 struct ldb_request *req;
320 struct dsdb_schema *schema;
325 create a always non-matching node from a equality node
327 static void set_parse_tree_false(struct ldb_parse_tree *tree)
329 const char *attr = tree->u.equality.attr;
330 struct ldb_val value = tree->u.equality.value;
331 tree->operation = LDB_OP_EXTENDED;
332 tree->u.extended.attr = attr;
333 tree->u.extended.value = value;
334 tree->u.extended.rule_id = SAMBA_LDAP_MATCH_ALWAYS_FALSE;
335 tree->u.extended.dnAttributes = 0;
339 called on all nodes in the parse tree
341 static int extended_dn_filter_callback(struct ldb_parse_tree *tree, void *private_context)
343 struct extended_dn_filter_ctx *filter_ctx;
346 const struct ldb_val *sid_val, *guid_val;
347 const char *no_attrs[] = { NULL };
348 struct ldb_result *res;
349 const struct dsdb_attribute *attribute;
350 bool has_extended_component;
351 enum ldb_scope scope;
352 struct ldb_dn *base_dn;
353 const char *expression;
356 if (tree->operation != LDB_OP_EQUALITY) {
360 filter_ctx = talloc_get_type_abort(private_context, struct extended_dn_filter_ctx);
362 if (filter_ctx->test_only && filter_ctx->matched) {
363 /* the tree already matched */
367 if (!filter_ctx->schema) {
368 /* Schema not setup yet */
371 attribute = dsdb_attribute_by_lDAPDisplayName(filter_ctx->schema, tree->u.equality.attr);
372 if (attribute == NULL) {
376 if (attribute->dn_format != DSDB_NORMAL_DN) {
380 has_extended_component = (memchr(tree->u.equality.value.data, '<',
381 tree->u.equality.value.length) != NULL);
384 * Don't turn it into an extended DN if we're talking to OpenLDAP.
385 * We just check the module_ops pointer instead of adding a private
386 * pointer and a boolean to tell us the exact same thing.
388 if (!has_extended_component) {
389 if (!attribute->one_way_link ||
390 ldb_module_get_ops(filter_ctx->module) == &ldb_extended_dn_in_openldap_module_ops)
394 dn = ldb_dn_from_ldb_val(filter_ctx, ldb_module_get_ctx(filter_ctx->module), &tree->u.equality.value);
396 /* testing against windows shows that we don't raise
401 guid_val = ldb_dn_get_extended_component(dn, "GUID");
402 sid_val = ldb_dn_get_extended_component(dn, "SID");
404 if (!guid_val && !sid_val && (attribute->searchFlags & SEARCH_FLAG_ATTINDEX)) {
405 /* if it is indexed, then fixing the string DN will do
406 no good here, as we will not find the attribute in
407 the index. So for now fall through to a standard DN
408 component comparison */
412 if (filter_ctx->test_only) {
413 /* we need to copy the tree */
414 filter_ctx->matched = true;
418 if (!ldb_dn_match_allowed(dn, filter_ctx->req)) {
419 /* we need to make this element of the filter always
421 set_parse_tree_false(tree);
425 dsdb_flags = filter_ctx->dsdb_flags | DSDB_FLAG_NEXT_MODULE;
428 expression = talloc_asprintf(filter_ctx, "objectGUID=%s", ldb_binary_encode(filter_ctx, *guid_val));
429 scope = LDB_SCOPE_SUBTREE;
431 dsdb_flags |= DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
432 } else if (sid_val) {
433 expression = talloc_asprintf(filter_ctx, "objectSID=%s", ldb_binary_encode(filter_ctx, *sid_val));
434 scope = LDB_SCOPE_SUBTREE;
436 dsdb_flags |= DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
438 /* fallback to searching using the string DN as the base DN */
439 expression = "objectClass=*";
441 scope = LDB_SCOPE_BASE;
444 ret = dsdb_module_search(filter_ctx->module,
453 if (scope == LDB_SCOPE_BASE && ret == LDB_ERR_NO_SUCH_OBJECT) {
454 /* note that this will need to change for multi-domain
456 set_parse_tree_false(tree);
460 if (ret != LDB_SUCCESS) {
465 if (res->count != 1) {
469 /* replace the search expression element with the matching DN */
470 tree->u.equality.value.data = (uint8_t *)talloc_strdup(tree,
471 ldb_dn_get_extended_linearized(tree, res->msgs[0]->dn, 1));
472 if (tree->u.equality.value.data == NULL) {
473 return ldb_oom(ldb_module_get_ctx(filter_ctx->module));
475 tree->u.equality.value.length = strlen((const char *)tree->u.equality.value.data);
478 filter_ctx->matched = true;
483 fix the parse tree to change any extended DN components to their
486 static int extended_dn_fix_filter(struct ldb_module *module,
487 struct ldb_request *req,
488 uint32_t default_dsdb_flags)
490 struct extended_dn_filter_ctx *filter_ctx;
493 filter_ctx = talloc_zero(req, struct extended_dn_filter_ctx);
494 if (filter_ctx == NULL) {
495 return ldb_module_oom(module);
498 /* first pass through the existing tree to see if anything
499 needs to be modified. Filtering DNs on the input side is rare,
500 so this avoids copying the parse tree in most cases */
501 filter_ctx->test_only = true;
502 filter_ctx->matched = false;
503 filter_ctx->module = module;
504 filter_ctx->req = req;
505 filter_ctx->schema = dsdb_get_schema(ldb_module_get_ctx(module), filter_ctx);
506 filter_ctx->dsdb_flags= default_dsdb_flags;
508 ret = ldb_parse_tree_walk(req->op.search.tree, extended_dn_filter_callback, filter_ctx);
509 if (ret != LDB_SUCCESS) {
510 talloc_free(filter_ctx);
514 if (!filter_ctx->matched) {
515 /* nothing matched, no need for a new parse tree */
516 talloc_free(filter_ctx);
520 filter_ctx->test_only = false;
521 filter_ctx->matched = false;
523 req->op.search.tree = ldb_parse_tree_copy_shallow(req, req->op.search.tree);
524 if (req->op.search.tree == NULL) {
525 return ldb_oom(ldb_module_get_ctx(module));
528 ret = ldb_parse_tree_walk(req->op.search.tree, extended_dn_filter_callback, filter_ctx);
529 if (ret != LDB_SUCCESS) {
530 talloc_free(filter_ctx);
534 talloc_free(filter_ctx);
539 fix DNs and filter expressions to cope with the semantics of
542 static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn)
544 struct extended_search_context *ac;
545 struct ldb_request *down_req;
547 struct ldb_dn *base_dn = NULL;
548 enum ldb_scope base_dn_scope = LDB_SCOPE_BASE;
549 const char *base_dn_filter = NULL;
550 const char * const *base_dn_attrs = NULL;
551 char *wellknown_object = NULL;
552 static const char *no_attr[] = {
555 uint32_t dsdb_flags = DSDB_FLAG_AS_SYSTEM | DSDB_SEARCH_SHOW_EXTENDED_DN;
557 if (ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID)) {
558 dsdb_flags |= DSDB_SEARCH_SHOW_DELETED;
560 if (ldb_request_get_control(req, LDB_CONTROL_SHOW_RECYCLED_OID)) {
561 dsdb_flags |= DSDB_SEARCH_SHOW_RECYCLED;
563 if (ldb_request_get_control(req, DSDB_CONTROL_DBCHECK)) {
564 dsdb_flags |= DSDB_SEARCH_SHOW_RECYCLED;
567 if (req->operation == LDB_SEARCH) {
568 ret = extended_dn_fix_filter(module, req, dsdb_flags);
569 if (ret != LDB_SUCCESS) {
574 if (!ldb_dn_has_extended(dn)) {
575 /* Move along there isn't anything to see here */
576 return ldb_next_request(module, req);
578 /* It looks like we need to map the DN */
579 const struct ldb_val *sid_val, *guid_val, *wkguid_val;
581 if (!ldb_dn_match_allowed(dn, req)) {
582 return ldb_error(ldb_module_get_ctx(module),
583 LDB_ERR_INVALID_DN_SYNTAX, "invalid number of DN components");
586 sid_val = ldb_dn_get_extended_component(dn, "SID");
587 guid_val = ldb_dn_get_extended_component(dn, "GUID");
588 wkguid_val = ldb_dn_get_extended_component(dn, "WKGUID");
591 prioritise the GUID - we have had instances of
592 duplicate SIDs in the database in the
593 ForeignSecurityPrinciples due to provision errors
596 dsdb_flags |= DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
598 base_dn_filter = talloc_asprintf(req, "(objectGUID=%s)",
599 ldb_binary_encode(req, *guid_val));
600 if (!base_dn_filter) {
601 return ldb_oom(ldb_module_get_ctx(module));
603 base_dn_scope = LDB_SCOPE_SUBTREE;
604 base_dn_attrs = no_attr;
606 } else if (sid_val) {
607 dsdb_flags |= DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
609 base_dn_filter = talloc_asprintf(req, "(objectSid=%s)",
610 ldb_binary_encode(req, *sid_val));
611 if (!base_dn_filter) {
612 return ldb_oom(ldb_module_get_ctx(module));
614 base_dn_scope = LDB_SCOPE_SUBTREE;
615 base_dn_attrs = no_attr;
617 } else if (wkguid_val) {
622 wkguid_dup = talloc_strndup(req, (char *)wkguid_val->data, wkguid_val->length);
624 p = strchr(wkguid_dup, ',');
626 return ldb_error(ldb_module_get_ctx(module), LDB_ERR_INVALID_DN_SYNTAX,
627 "Invalid WKGUID format");
633 wellknown_object = talloc_asprintf(req, "B:32:%s:", wkguid_dup);
634 if (!wellknown_object) {
635 return ldb_oom(ldb_module_get_ctx(module));
640 base_dn = ldb_dn_new(req, ldb_module_get_ctx(module), tail_str);
641 talloc_free(wkguid_dup);
643 return ldb_oom(ldb_module_get_ctx(module));
645 base_dn_filter = talloc_strdup(req, "(objectClass=*)");
646 if (!base_dn_filter) {
647 return ldb_oom(ldb_module_get_ctx(module));
649 base_dn_scope = LDB_SCOPE_BASE;
650 base_dn_attrs = wkattr;
652 return ldb_error(ldb_module_get_ctx(module), LDB_ERR_INVALID_DN_SYNTAX,
653 "Invalid extended DN component");
656 ac = talloc_zero(req, struct extended_search_context);
658 return ldb_oom(ldb_module_get_ctx(module));
664 ac->basedn = NULL; /* Filled in if the search finds the DN by SID/GUID etc */
665 ac->wellknown_object = wellknown_object;
667 /* If the base DN was an extended DN (perhaps a well known
668 * GUID) then search for that, so we can proceed with the original operation */
670 ret = ldb_build_search_req(&down_req,
671 ldb_module_get_ctx(module), ac,
677 ac, extended_base_callback,
679 LDB_REQ_SET_LOCATION(down_req);
680 if (ret != LDB_SUCCESS) {
681 return ldb_operr(ldb_module_get_ctx(module));
684 ret = dsdb_request_add_controls(down_req, dsdb_flags);
685 if (ret != LDB_SUCCESS) {
689 /* perform the search */
690 return ldb_next_request(module, down_req);
694 static int extended_dn_in_search(struct ldb_module *module, struct ldb_request *req)
696 return extended_dn_in_fix(module, req, req->op.search.base);
699 static int extended_dn_in_modify(struct ldb_module *module, struct ldb_request *req)
701 return extended_dn_in_fix(module, req, req->op.mod.message->dn);
704 static int extended_dn_in_del(struct ldb_module *module, struct ldb_request *req)
706 return extended_dn_in_fix(module, req, req->op.del.dn);
709 static int extended_dn_in_rename(struct ldb_module *module, struct ldb_request *req)
711 return extended_dn_in_fix(module, req, req->op.rename.olddn);
714 static const struct ldb_module_ops ldb_extended_dn_in_module_ops = {
715 .name = "extended_dn_in",
716 .search = extended_dn_in_search,
717 .modify = extended_dn_in_modify,
718 .del = extended_dn_in_del,
719 .rename = extended_dn_in_rename,
722 static const struct ldb_module_ops ldb_extended_dn_in_openldap_module_ops = {
723 .name = "extended_dn_in_openldap",
724 .search = extended_dn_in_search,
725 .modify = extended_dn_in_modify,
726 .del = extended_dn_in_del,
727 .rename = extended_dn_in_rename,
730 int ldb_extended_dn_in_module_init(const char *version)
733 LDB_MODULE_CHECK_VERSION(version);
734 ret = ldb_register_module(&ldb_extended_dn_in_openldap_module_ops);
735 if (ret != LDB_SUCCESS) {
738 return ldb_register_module(&ldb_extended_dn_in_module_ops);