dsdb: Only parse SAMBA_LDAP_MATCH_RULE_TRANSITIVE_EVAL as a DN
[obnox/samba/samba-obnox.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_in.c
index c21a1ea69bc0edb40facff48fcad71a034f4af13..b7ca636598f6842ea403cd348d986f409b6db2e0 100644 (file)
@@ -35,6 +35,7 @@
 #include <ldb_module.h>
 #include "dsdb/samdb/samdb.h"
 #include "dsdb/samdb/ldb_modules/util.h"
+#include "lib/ldb-samba/ldb_matching_rules.h"
 
 /*
   TODO: if relax is not set then we need to reject the fancy RMD_* and
@@ -56,6 +57,9 @@ static const char *wkattr[] = {
        "otherWellKnownObjects",
        NULL
 };
+
+static const struct ldb_module_ops ldb_extended_dn_in_openldap_module_ops;
+
 /* An extra layer of indirection because LDB does not allow the original request to be altered */
 
 static int extended_final_callback(struct ldb_request *req, struct ldb_reply *ares)
@@ -315,6 +319,7 @@ struct extended_dn_filter_ctx {
        struct ldb_module *module;
        struct ldb_request *req;
        struct dsdb_schema *schema;
+       uint32_t dsdb_flags;
 };
 
 /*
@@ -338,18 +343,18 @@ static int extended_dn_filter_callback(struct ldb_parse_tree *tree, void *privat
 {
        struct extended_dn_filter_ctx *filter_ctx;
        int ret;
-       struct ldb_dn *dn;
+       struct ldb_dn *dn = NULL;
        const struct ldb_val *sid_val, *guid_val;
        const char *no_attrs[] = { NULL };
        struct ldb_result *res;
-       const struct dsdb_attribute *attribute;
-       bool has_extended_component;
+       const struct dsdb_attribute *attribute = NULL;
+       bool has_extended_component = false;
        enum ldb_scope scope;
        struct ldb_dn *base_dn;
        const char *expression;
        uint32_t dsdb_flags;
 
-       if (tree->operation != LDB_OP_EQUALITY) {
+       if (tree->operation != LDB_OP_EQUALITY && tree->operation != LDB_OP_EXTENDED) {
                return LDB_SUCCESS;
        }
 
@@ -364,7 +369,11 @@ static int extended_dn_filter_callback(struct ldb_parse_tree *tree, void *privat
                /* Schema not setup yet */
                return LDB_SUCCESS;
        }
-       attribute = dsdb_attribute_by_lDAPDisplayName(filter_ctx->schema, tree->u.equality.attr);
+       if (tree->operation == LDB_OP_EQUALITY) {
+               attribute = dsdb_attribute_by_lDAPDisplayName(filter_ctx->schema, tree->u.equality.attr);
+       } else if (tree->operation == LDB_OP_EXTENDED) {
+               attribute = dsdb_attribute_by_lDAPDisplayName(filter_ctx->schema, tree->u.extended.attr);
+       }
        if (attribute == NULL) {
                return LDB_SUCCESS;
        }
@@ -373,14 +382,35 @@ static int extended_dn_filter_callback(struct ldb_parse_tree *tree, void *privat
                return LDB_SUCCESS;
        }
 
-       has_extended_component = (memchr(tree->u.equality.value.data, '<',
-                                        tree->u.equality.value.length) != NULL);
+       if (tree->operation == LDB_OP_EQUALITY) {
+               has_extended_component = (memchr(tree->u.equality.value.data, '<',
+                                                tree->u.equality.value.length) != NULL);
+       } else if (tree->operation == LDB_OP_EXTENDED) {
+               has_extended_component = (memchr(tree->u.extended.value.data, '<',
+                                                tree->u.extended.value.length) != NULL);
+       }
 
-       if (!attribute->one_way_link && !has_extended_component) {
-               return LDB_SUCCESS;
+       /*
+        * Don't turn it into an extended DN if we're talking to OpenLDAP.
+        * We just check the module_ops pointer instead of adding a private
+        * pointer and a boolean to tell us the exact same thing.
+        */
+       if (!has_extended_component) {
+               if (!attribute->one_way_link) {
+                       return LDB_SUCCESS;
+               }
+
+               if (ldb_module_get_ops(filter_ctx->module) == &ldb_extended_dn_in_openldap_module_ops) {
+                       return LDB_SUCCESS;
+               }
        }
 
-       dn = ldb_dn_from_ldb_val(filter_ctx, ldb_module_get_ctx(filter_ctx->module), &tree->u.equality.value);
+       if (tree->operation == LDB_OP_EQUALITY) {
+               dn = ldb_dn_from_ldb_val(filter_ctx, ldb_module_get_ctx(filter_ctx->module), &tree->u.equality.value);
+       } else if (tree->operation == LDB_OP_EXTENDED
+                  && (strcmp(tree->u.extended.rule_id, SAMBA_LDAP_MATCH_RULE_TRANSITIVE_EVAL) == 0)) {
+               dn = ldb_dn_from_ldb_val(filter_ctx, ldb_module_get_ctx(filter_ctx->module), &tree->u.extended.value);
+       }
        if (dn == NULL) {
                /* testing against windows shows that we don't raise
                   an error here */
@@ -411,9 +441,7 @@ static int extended_dn_filter_callback(struct ldb_parse_tree *tree, void *privat
                return LDB_SUCCESS;
        }
 
-       dsdb_flags = DSDB_FLAG_NEXT_MODULE |
-               DSDB_SEARCH_SHOW_DELETED |
-               DSDB_SEARCH_SHOW_EXTENDED_DN;
+       dsdb_flags = filter_ctx->dsdb_flags | DSDB_FLAG_NEXT_MODULE;
 
        if (guid_val) {
                expression = talloc_asprintf(filter_ctx, "objectGUID=%s", ldb_binary_encode(filter_ctx, *guid_val));
@@ -458,12 +486,21 @@ static int extended_dn_filter_callback(struct ldb_parse_tree *tree, void *privat
        }
 
        /* replace the search expression element with the matching DN */
-       tree->u.equality.value.data = (uint8_t *)talloc_strdup(tree,
-                                                              ldb_dn_get_extended_linearized(tree, res->msgs[0]->dn, 1));
-       if (tree->u.equality.value.data == NULL) {
-               return ldb_oom(ldb_module_get_ctx(filter_ctx->module));
+       if (tree->operation == LDB_OP_EQUALITY) {
+               tree->u.equality.value.data =
+                       (uint8_t *)talloc_strdup(tree, ldb_dn_get_extended_linearized(tree, res->msgs[0]->dn, 1));
+               if (tree->u.equality.value.data == NULL) {
+                       return ldb_oom(ldb_module_get_ctx(filter_ctx->module));
+               }
+               tree->u.equality.value.length = strlen((const char *)tree->u.equality.value.data);
+       } else if (tree->operation == LDB_OP_EXTENDED) {
+               tree->u.extended.value.data =
+                       (uint8_t *)talloc_strdup(tree, ldb_dn_get_extended_linearized(tree, res->msgs[0]->dn, 1));
+               if (tree->u.extended.value.data == NULL) {
+                       return ldb_oom(ldb_module_get_ctx(filter_ctx->module));
+               }
+               tree->u.extended.value.length = strlen((const char *)tree->u.extended.value.data);
        }
-       tree->u.equality.value.length = strlen((const char *)tree->u.equality.value.data);
        talloc_free(res);
 
        filter_ctx->matched = true;
@@ -474,7 +511,9 @@ static int extended_dn_filter_callback(struct ldb_parse_tree *tree, void *privat
   fix the parse tree to change any extended DN components to their
   caconical form
  */
-static int extended_dn_fix_filter(struct ldb_module *module, struct ldb_request *req)
+static int extended_dn_fix_filter(struct ldb_module *module,
+                                 struct ldb_request *req,
+                                 uint32_t default_dsdb_flags)
 {
        struct extended_dn_filter_ctx *filter_ctx;
        int ret;
@@ -492,6 +531,7 @@ static int extended_dn_fix_filter(struct ldb_module *module, struct ldb_request
        filter_ctx->module    = module;
        filter_ctx->req       = req;
        filter_ctx->schema    = dsdb_get_schema(ldb_module_get_ctx(module), filter_ctx);
+       filter_ctx->dsdb_flags= default_dsdb_flags;
 
        ret = ldb_parse_tree_walk(req->op.search.tree, extended_dn_filter_callback, filter_ctx);
        if (ret != LDB_SUCCESS) {
@@ -540,10 +580,20 @@ static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req
        static const char *no_attr[] = {
                NULL
        };
-       bool all_partitions = false;
+       uint32_t dsdb_flags = DSDB_FLAG_AS_SYSTEM | DSDB_SEARCH_SHOW_EXTENDED_DN;
+
+       if (ldb_request_get_control(req, LDB_CONTROL_SHOW_DELETED_OID)) {
+               dsdb_flags |= DSDB_SEARCH_SHOW_DELETED;
+       }
+       if (ldb_request_get_control(req, LDB_CONTROL_SHOW_RECYCLED_OID)) {
+               dsdb_flags |= DSDB_SEARCH_SHOW_RECYCLED;
+       }
+       if (ldb_request_get_control(req, DSDB_CONTROL_DBCHECK)) {
+               dsdb_flags |= DSDB_SEARCH_SHOW_RECYCLED;
+       }
 
        if (req->operation == LDB_SEARCH) {
-               ret = extended_dn_fix_filter(module, req);
+               ret = extended_dn_fix_filter(module, req, dsdb_flags);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
@@ -571,7 +621,7 @@ static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req
                  ForeignSecurityPrinciples due to provision errors
                 */
                if (guid_val) {
-                       all_partitions = true;
+                       dsdb_flags |= DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
                        base_dn = NULL;
                        base_dn_filter = talloc_asprintf(req, "(objectGUID=%s)",
                                                         ldb_binary_encode(req, *guid_val));
@@ -582,7 +632,7 @@ static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req
                        base_dn_attrs = no_attr;
 
                } else if (sid_val) {
-                       all_partitions = true;
+                       dsdb_flags |= DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
                        base_dn = NULL;
                        base_dn_filter = talloc_asprintf(req, "(objectSid=%s)",
                                                         ldb_binary_encode(req, *sid_val));
@@ -651,7 +701,7 @@ static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req
                                           base_dn_scope,
                                           base_dn_filter,
                                           base_dn_attrs,
-                                          req->controls,
+                                          NULL,
                                           ac, extended_base_callback,
                                           req);
                LDB_REQ_SET_LOCATION(down_req);
@@ -659,17 +709,9 @@ static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req
                        return ldb_operr(ldb_module_get_ctx(module));
                }
 
-               if (all_partitions) {
-                       struct ldb_search_options_control *control;
-                       control = talloc(down_req, struct ldb_search_options_control);
-                       control->search_options = 2;
-                       ret = ldb_request_replace_control(down_req,
-                                                     LDB_CONTROL_SEARCH_OPTIONS_OID,
-                                                     true, control);
-                       if (ret != LDB_SUCCESS) {
-                               ldb_oom(ldb_module_get_ctx(module));
-                               return ret;
-                       }
+               ret = dsdb_request_add_controls(down_req, dsdb_flags);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
                }
 
                /* perform the search */
@@ -705,8 +747,21 @@ static const struct ldb_module_ops ldb_extended_dn_in_module_ops = {
        .rename            = extended_dn_in_rename,
 };
 
+static const struct ldb_module_ops ldb_extended_dn_in_openldap_module_ops = {
+       .name              = "extended_dn_in_openldap",
+       .search            = extended_dn_in_search,
+       .modify            = extended_dn_in_modify,
+       .del               = extended_dn_in_del,
+       .rename            = extended_dn_in_rename,
+};
+
 int ldb_extended_dn_in_module_init(const char *version)
 {
+       int ret;
        LDB_MODULE_CHECK_VERSION(version);
+       ret = ldb_register_module(&ldb_extended_dn_in_openldap_module_ops);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
        return ldb_register_module(&ldb_extended_dn_in_module_ops);
 }