dsdb: Move ldb_set_default_dns() into rootdse_get_private_data()
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / rootdse.c
index cfd75e8c437f31a774fb9f419dd271140957392f..751fe15d1a17b8de3789131de5a55e622efb737c 100644 (file)
 #include "param/param.h"
 #include "lib/messaging/irpc.h"
 #include "librpc/gen_ndr/ndr_irpc_c.h"
+#include "lib/tsocket/tsocket.h"
+#include "cldap_server/cldap_server.h"
+#include "lib/events/events.h"
 
-struct private_data {
+struct rootdse_private_data {
        unsigned int num_controls;
        char **controls;
        unsigned int num_partitions;
        struct ldb_dn **partitions;
        bool block_anonymous;
+       struct tevent_context *saved_ev;
+       struct tevent_context *private_ev;
+};
+
+struct rootdse_context {
+       struct ldb_module *module;
+       struct ldb_request *req;
+       struct ldb_val netlogon;
 };
 
 /*
@@ -131,10 +142,8 @@ static int expand_dn_in_message(struct ldb_module *module, struct ldb_message *m
                        return ret;
                }
 
-
-               ret = ldb_request_add_control(req2,
-                                       LDB_CONTROL_EXTENDED_DN_OID,
-                                       edn_control->critical, edn);
+               ret = dsdb_request_add_controls(req2, DSDB_FLAG_AS_SYSTEM |
+                                               DSDB_SEARCH_SHOW_EXTENDED_DN);
                if (ret != LDB_SUCCESS) {
                        talloc_free(tmp_ctx);
                        return ldb_error(ldb, ret, "Failed to add control");
@@ -183,7 +192,11 @@ static int dsdb_module_we_are_master(struct ldb_module *module, struct ldb_dn *d
        struct ldb_dn *owner_dn;
 
        ret = dsdb_module_search_dn(module, tmp_ctx, &res,
-                                   dn, attrs, DSDB_FLAG_NEXT_MODULE, parent);
+                                   dn, attrs,
+                                   DSDB_FLAG_NEXT_MODULE |
+                                   DSDB_FLAG_AS_SYSTEM |
+                                   DSDB_SEARCH_SHOW_EXTENDED_DN,
+                                   parent);
        if (ret != LDB_SUCCESS) {
                talloc_free(tmp_ctx);
                return ret;
@@ -197,7 +210,14 @@ static int dsdb_module_we_are_master(struct ldb_module *module, struct ldb_dn *d
                return LDB_SUCCESS;
        }
 
-       *master = (ldb_dn_compare(owner_dn, samdb_ntds_settings_dn(ldb_module_get_ctx(module))) == 0);
+       ret = samdb_dn_is_our_ntdsa(ldb_module_get_ctx(module), dn, master);
+       if (ret != LDB_SUCCESS) {
+               ldb_asprintf_errstring(ldb_module_get_ctx(module), "Failed to confirm if our ntdsDsa is %s: %s",
+                                      ldb_dn_get_linearized(owner_dn), ldb_errstring(ldb_module_get_ctx(module)));
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
        talloc_free(tmp_ctx);
        return LDB_SUCCESS;
 }
@@ -205,11 +225,11 @@ static int dsdb_module_we_are_master(struct ldb_module *module, struct ldb_dn *d
 /*
   add dynamically generated attributes to rootDSE result
 */
-static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg,
-                              const char * const *attrs, struct ldb_request *req)
+static int rootdse_add_dynamic(struct rootdse_context *ac, struct ldb_message *msg)
 {
        struct ldb_context *ldb;
-       struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
+       struct rootdse_private_data *priv = talloc_get_type(ldb_module_get_private(ac->module), struct rootdse_private_data);
+       const char * const *attrs = ac->req->op.search.attrs;
        char **server_sasl;
        const struct dsdb_schema *schema;
        int *val;
@@ -217,15 +237,20 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
        const char *dn_attrs[] = {
                "configurationNamingContext",
                "defaultNamingContext",
-               "dsServiceName",
                "rootDomainNamingContext",
                "schemaNamingContext",
                "serverName",
                "validFSMOs",
+               "namingContexts",
                NULL
        };
+       const char *guid_attrs[] = {
+               "dsServiceName",
+               NULL
+       };
+       unsigned int i;
 
-       ldb = ldb_module_get_ctx(module);
+       ldb = ldb_module_get_ctx(ac->module);
        schema = dsdb_get_schema(ldb, NULL);
 
        msg->dn = ldb_dn_new(msg, ldb, NULL);
@@ -246,12 +271,15 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
                struct ldb_result *res;
                int ret;
                const char *dns_attrs[] = { "dNSHostName", NULL };
-               ret = dsdb_module_search_dn(module, msg, &res, samdb_server_dn(ldb, msg),
-                                           dns_attrs, DSDB_FLAG_NEXT_MODULE, req);
+               ret = dsdb_module_search_dn(ac->module, msg, &res, samdb_server_dn(ldb, msg),
+                                           dns_attrs,
+                                           DSDB_FLAG_NEXT_MODULE |
+                                           DSDB_FLAG_AS_SYSTEM,
+                                           ac->req);
                if (ret == LDB_SUCCESS) {
                        const char *hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
                        if (hostname != NULL) {
-                               if (ldb_msg_add_string(msg, "dNSHostName", hostname)) {
+                               if (ldb_msg_add_string(msg, "dnsHostName", hostname)) {
                                        goto failed;
                                }
                        }
@@ -290,7 +318,6 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
        }
 
        if (priv && do_attribute(attrs, "supportedControl")) {
-               unsigned int i;
                for (i = 0; i < priv->num_controls; i++) {
                        char *control = talloc_strdup(msg, priv->controls[i]);
                        if (!control) {
@@ -304,7 +331,6 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
        }
 
        if (priv && do_attribute(attrs, "namingContexts")) {
-               unsigned int i;
                for (i = 0; i < priv->num_partitions; i++) {
                        struct ldb_dn *dn = priv->partitions[i];
                        if (ldb_msg_add_steal_string(msg, "namingContexts",
@@ -317,7 +343,6 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
        server_sasl = talloc_get_type(ldb_get_opaque(ldb, "supportedSASLMechanisms"),
                                       char *);
        if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
-               unsigned int i;
                for (i = 0; server_sasl && server_sasl[i]; i++) {
                        char *sasl_name = talloc_strdup(msg, server_sasl[i]);
                        if (!sasl_name) {
@@ -386,7 +411,7 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
 
                for (i=0; i<3; i++) {
                        bool master;
-                       int ret = dsdb_module_we_are_master(module, dns[i], &master, req);
+                       int ret = dsdb_module_we_are_master(ac->module, dns[i], &master, ac->req);
                        if (ret != LDB_SUCCESS) {
                                goto failed;
                        }
@@ -441,7 +466,6 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
        }
 
        if (do_attribute_explicit(attrs, "tokenGroups")) {
-               unsigned int i;
                /* Obtain the user's session_info */
                struct auth_session_info *session_info
                        = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
@@ -457,20 +481,76 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *ms
                }
        }
 
+       if (ac->netlogon.length > 0) {
+               if (ldb_msg_add_steal_value(msg, "netlogon", &ac->netlogon) != LDB_SUCCESS) {
+                       goto failed;
+               }
+       }
+
        /* TODO: lots more dynamic attributes should be added here */
 
-       edn_control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
+       edn_control = ldb_request_get_control(ac->req, LDB_CONTROL_EXTENDED_DN_OID);
+
+       /* convert any GUID attributes to be in the right form */
+       for (i=0; guid_attrs[i]; i++) {
+               struct ldb_result *res;
+               struct ldb_message_element *el;
+               struct ldb_dn *attr_dn;
+               const char *no_attrs[] = { NULL };
+               int ret;
+
+               if (!do_attribute(attrs, guid_attrs[i])) continue;
+
+               attr_dn = ldb_msg_find_attr_as_dn(ldb, ac->req, msg, guid_attrs[i]);
+               if (attr_dn == NULL) {
+                       continue;
+               }
+
+               ret = dsdb_module_search_dn(ac->module, ac->req, &res,
+                                           attr_dn, no_attrs,
+                                           DSDB_FLAG_NEXT_MODULE |
+                                           DSDB_FLAG_AS_SYSTEM |
+                                           DSDB_SEARCH_SHOW_EXTENDED_DN,
+                                           ac->req);
+               if (ret != LDB_SUCCESS) {
+                       return ldb_operr(ldb);
+               }
+
+               el = ldb_msg_find_element(msg, guid_attrs[i]);
+               if (el == NULL) {
+                       return ldb_operr(ldb);
+               }
+
+               talloc_steal(el->values, res->msgs[0]->dn);
+               if (edn_control) {
+                       struct ldb_extended_dn_control *edn;
+                       int edn_type = 0;
+                       edn = talloc_get_type(edn_control->data, struct ldb_extended_dn_control);
+                       if (edn != NULL) {
+                               edn_type = edn->type;
+                       }
+                       el->values[0].data  = (uint8_t *)ldb_dn_get_extended_linearized(el->values,
+                                                                                       res->msgs[0]->dn,
+                                                                                       edn_type);
+               } else {
+                       el->values[0].data  = (uint8_t *)talloc_strdup(el->values,
+                                                                      ldb_dn_get_linearized(res->msgs[0]->dn));
+               }
+               if (el->values[0].data == NULL) {
+                       return ldb_oom(ldb);
+               }
+               el->values[0].length = strlen((const char *)el->values[0].data);
+       }
 
        /* if the client sent us the EXTENDED_DN control then we need
           to expand the DNs to have GUID and SID. W2K8 join relies on
           this */
        if (edn_control) {
-               unsigned int i;
                int ret;
                for (i=0; dn_attrs[i]; i++) {
                        if (!do_attribute(attrs, dn_attrs[i])) continue;
-                       ret = expand_dn_in_message(module, msg, dn_attrs[i],
-                                                  edn_control, req);
+                       ret = expand_dn_in_message(ac->module, msg, dn_attrs[i],
+                                                  edn_control, ac->req);
                        if (ret != LDB_SUCCESS) {
                                DEBUG(0,(__location__ ": Failed to expand DN in rootDSE for %s\n",
                                         dn_attrs[i]));
@@ -489,11 +569,6 @@ failed:
   handle search requests
 */
 
-struct rootdse_context {
-       struct ldb_module *module;
-       struct ldb_request *req;
-};
-
 static struct rootdse_context *rootdse_init_context(struct ldb_module *module,
                                                    struct ldb_request *req)
 {
@@ -532,20 +607,9 @@ static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
 
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
-               /*
-                * if the client explicit asks for the 'netlogon' attribute
-                * the reply_entry needs to be skipped
-                */
-               if (ac->req->op.search.attrs &&
-                   ldb_attr_in_list(ac->req->op.search.attrs, "netlogon")) {
-                       talloc_free(ares);
-                       return LDB_SUCCESS;
-               }
-
                /* for each record returned post-process to add any dynamic
                   attributes that have been asked for */
-               ret = rootdse_add_dynamic(ac->module, ares->message,
-                                         ac->req->op.search.attrs, ac->req);
+               ret = rootdse_add_dynamic(ac, ares->message);
                if (ret != LDB_SUCCESS) {
                        talloc_free(ares);
                        return ldb_module_done(ac->req, NULL, NULL, ret);
@@ -591,7 +655,7 @@ static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
 static int rootdse_filter_controls(struct ldb_module *module, struct ldb_request *req)
 {
        unsigned int i, j;
-       struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
+       struct rootdse_private_data *priv = talloc_get_type(ldb_module_get_private(module), struct rootdse_private_data);
        bool is_untrusted;
 
        if (!req->controls) {
@@ -637,11 +701,20 @@ static int rootdse_filter_controls(struct ldb_module *module, struct ldb_request
                        continue;
                }
 
-               /* If the control is DIRSYNC control then we keep the critical
-                * flag as the dirsync module will need to act upon it
+               /*
+                * If the control is DIRSYNC, SORT or VLV then we keep the
+                * critical flag as the modules will need to act upon it.
+                *
+                * These modules have to unset the critical flag after the
+                * request has been seen by the correct module.
                 */
-               if (is_registered && strcmp(req->controls[i]->oid,
-                                       LDB_CONTROL_DIRSYNC_OID)!= 0) {
+               if (is_registered &&
+                   strcmp(req->controls[i]->oid,
+                          LDB_CONTROL_DIRSYNC_OID) != 0 &&
+                   strcmp(req->controls[i]->oid,
+                          LDB_CONTROL_VLV_REQ_OID) != 0 &&
+                   strcmp(req->controls[i]->oid,
+                          LDB_CONTROL_SERVER_SORT_OID) != 0) {
                        req->controls[i]->critical = 0;
                }
        }
@@ -654,7 +727,7 @@ static int rootdse_filter_controls(struct ldb_module *module, struct ldb_request
 static int rootdse_filter_operations(struct ldb_module *module, struct ldb_request *req)
 {
        struct auth_session_info *session_info;
-       struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
+       struct rootdse_private_data *priv = talloc_get_type(ldb_module_get_private(module), struct rootdse_private_data);
        bool is_untrusted = ldb_req_is_untrusted(req);
        bool is_anonymous = true;
        if (is_untrusted == false) {
@@ -665,11 +738,11 @@ static int rootdse_filter_operations(struct ldb_module *module, struct ldb_reque
        if (session_info) {
                is_anonymous = security_token_is_anonymous(session_info->security_token);
        }
-       
+
        if (is_anonymous == false || (priv && priv->block_anonymous == false)) {
                return LDB_SUCCESS;
        }
-       
+
        if (req->operation == LDB_SEARCH) {
                if (req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base)) {
                        return LDB_SUCCESS;
@@ -679,6 +752,62 @@ static int rootdse_filter_operations(struct ldb_module *module, struct ldb_reque
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
+static int rootdse_handle_netlogon(struct rootdse_context *ac)
+{
+       struct ldb_context *ldb;
+       struct ldb_parse_tree *tree;
+       struct loadparm_context *lp_ctx;
+       struct tsocket_address *src_addr;
+       TALLOC_CTX *tmp_ctx = talloc_new(ac->req);
+       const char *domain, *host, *user, *domain_guid;
+       char *src_addr_s = NULL;
+       struct dom_sid *domain_sid;
+       int acct_control = -1;
+       int version = -1;
+       NTSTATUS status;
+       struct netlogon_samlogon_response netlogon;
+       int ret = LDB_ERR_OPERATIONS_ERROR;
+
+       ldb = ldb_module_get_ctx(ac->module);
+       tree = ac->req->op.search.tree;
+       lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
+                                struct loadparm_context);
+       src_addr = talloc_get_type(ldb_get_opaque(ldb, "remoteAddress"),
+                                  struct tsocket_address);
+       if (src_addr) {
+               src_addr_s = tsocket_address_inet_addr_string(src_addr,
+                                                             tmp_ctx);
+       }
+
+       status = parse_netlogon_request(tree, lp_ctx, tmp_ctx,
+                                       &domain, &host, &user, &domain_guid,
+                                       &domain_sid, &acct_control, &version);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto failed;
+       }
+
+       status = fill_netlogon_samlogon_response(ldb, tmp_ctx,
+                                                domain, NULL, domain_sid,
+                                                domain_guid,
+                                                user, acct_control,
+                                                src_addr_s,
+                                                version, lp_ctx,
+                                                &netlogon, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto failed;
+       }
+
+       status = push_netlogon_samlogon_response(&ac->netlogon, ac, &netlogon);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto failed;
+       }
+
+       ret = LDB_SUCCESS;
+failed:
+       talloc_free(tmp_ctx);
+       return ret;
+}
+
 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
 {
        struct ldb_context *ldb;
@@ -709,6 +838,14 @@ static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
                return ldb_operr(ldb);
        }
 
+       if (do_attribute_explicit(req->op.search.attrs, "netlogon")) {
+               ret = rootdse_handle_netlogon(ac);
+               /* We have to return an empty result, so don't forward `ret' */
+               if (ret != LDB_SUCCESS) {
+                       return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
+               }
+       }
+
        /* in our db we store the rootDSE with a DN of @ROOTDSE */
        ret = ldb_build_search_req(&down_req, ldb, ac,
                                        ldb_dn_new(ac, ldb, "@ROOTDSE"),
@@ -726,11 +863,51 @@ static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
        return ldb_next_request(module, down_req);
 }
 
+static struct rootdse_private_data *rootdse_get_private_data(struct ldb_module *module)
+{
+       void *priv = ldb_module_get_private(module);
+       struct rootdse_private_data *data = NULL;
+       struct ldb_context *ldb
+               = ldb_module_get_ctx(module);
+
+       if (priv != NULL) {
+               data = talloc_get_type_abort(priv,
+                                            struct rootdse_private_data);
+       }
+
+       if (data != NULL) {
+               return data;
+       }
+
+       data = talloc_zero(module, struct rootdse_private_data);
+       if (data == NULL) {
+               return NULL;
+       }
+
+       data->num_controls = 0;
+       data->controls = NULL;
+       data->num_partitions = 0;
+       data->partitions = NULL;
+       data->block_anonymous = true;
+
+       ldb_module_set_private(module, data);
+
+       ldb_set_default_dns(ldb);
+
+       return data;
+}
+
+
 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
 {
-       struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
+       struct rootdse_private_data *priv =
+               rootdse_get_private_data(module);
        char **list;
 
+       if (priv == NULL) {
+               return ldb_module_oom(module);
+       }
+
        list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
        if (!list) {
                return ldb_oom(ldb_module_get_ctx(module));
@@ -749,9 +926,14 @@ static int rootdse_register_control(struct ldb_module *module, struct ldb_reques
 
 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
 {
-       struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
+       struct rootdse_private_data *priv =
+               rootdse_get_private_data(module);
        struct ldb_dn **list;
 
+       if (priv == NULL) {
+               return ldb_module_oom(module);
+       }
+
        list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
        if (!list) {
                return ldb_oom(ldb_module_get_ctx(module));
@@ -787,30 +969,21 @@ static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
 static int rootdse_init(struct ldb_module *module)
 {
        int ret;
-       struct ldb_context *ldb;
        struct ldb_result *res;
-       struct private_data *data;
        const char *attrs[] = { "msDS-Behavior-Version", NULL };
        const char *ds_attrs[] = { "dsServiceName", NULL };
        TALLOC_CTX *mem_ctx;
 
-       ldb = ldb_module_get_ctx(module);
+       struct ldb_context *ldb
+               = ldb_module_get_ctx(module);
+
+       struct rootdse_private_data *data
+               = rootdse_get_private_data(module);
 
-       data = talloc_zero(module, struct private_data);
        if (data == NULL) {
-               return ldb_oom(ldb);
+               return ldb_module_oom(module);
        }
 
-       data->num_controls = 0;
-       data->controls = NULL;
-       data->num_partitions = 0;
-       data->partitions = NULL;
-       data->block_anonymous = true;
-
-       ldb_module_set_private(module, data);
-
-       ldb_set_default_dns(ldb);
-
        ret = ldb_next_init(module);
 
        if (ret != LDB_SUCCESS) {
@@ -831,7 +1004,10 @@ static int rootdse_init(struct ldb_module *module)
        */
        ret = dsdb_module_search(module, mem_ctx, &res,
                                 ldb_get_default_basedn(ldb),
-                                LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
+                                LDB_SCOPE_BASE, attrs,
+                                DSDB_FLAG_NEXT_MODULE |
+                                DSDB_FLAG_AS_SYSTEM,
+                                NULL, NULL);
        if (ret == LDB_SUCCESS && res->count == 1) {
                int domain_behaviour_version
                        = ldb_msg_find_attr_as_int(res->msgs[0],
@@ -853,7 +1029,10 @@ static int rootdse_init(struct ldb_module *module)
 
        ret = dsdb_module_search(module, mem_ctx, &res,
                                 samdb_partitions_dn(ldb, mem_ctx),
-                                LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
+                                LDB_SCOPE_BASE, attrs,
+                                DSDB_FLAG_NEXT_MODULE |
+                                DSDB_FLAG_AS_SYSTEM,
+                                NULL, NULL);
        if (ret == LDB_SUCCESS && res->count == 1) {
                int forest_behaviour_version
                        = ldb_msg_find_attr_as_int(res->msgs[0],
@@ -877,14 +1056,20 @@ static int rootdse_init(struct ldb_module *module)
         * the @ROOTDSE record */
        ret = dsdb_module_search(module, mem_ctx, &res,
                                 ldb_dn_new(mem_ctx, ldb, "@ROOTDSE"),
-                                LDB_SCOPE_BASE, ds_attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
+                                LDB_SCOPE_BASE, ds_attrs,
+                                DSDB_FLAG_NEXT_MODULE |
+                                DSDB_FLAG_AS_SYSTEM,
+                                NULL, NULL);
        if (ret == LDB_SUCCESS && res->count == 1) {
                struct ldb_dn *ds_dn
                        = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0],
                                                  "dsServiceName");
                if (ds_dn) {
                        ret = dsdb_module_search(module, mem_ctx, &res, ds_dn,
-                                                LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
+                                                LDB_SCOPE_BASE, attrs,
+                                                DSDB_FLAG_NEXT_MODULE |
+                                                DSDB_FLAG_AS_SYSTEM,
+                                                NULL, NULL);
                        if (ret == LDB_SUCCESS && res->count == 1) {
                                int domain_controller_behaviour_version
                                        = ldb_msg_find_attr_as_int(res->msgs[0],
@@ -977,6 +1162,7 @@ static int dsdb_find_optional_feature(struct ldb_module *module, struct ldb_cont
        ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
                                 NULL,
                                 DSDB_FLAG_NEXT_MODULE |
+                                DSDB_FLAG_AS_SYSTEM |
                                 DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
                                 parent,
                                 "(&(objectClass=msDS-OptionalFeature)"
@@ -1024,7 +1210,7 @@ static int rootdse_enable_recycle_bin(struct ldb_module *module,struct ldb_conte
        }
 
        tmp_ctx = talloc_new(mem_ctx);
-       ntds_settings_dn = samdb_ntds_settings_dn(ldb);
+       ntds_settings_dn = samdb_ntds_settings_dn(ldb, tmp_ctx);
        if (!ntds_settings_dn) {
                talloc_free(tmp_ctx);
                return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "Failed to find NTDS settings DN");
@@ -1037,6 +1223,10 @@ static int rootdse_enable_recycle_bin(struct ldb_module *module,struct ldb_conte
        }
 
        msg = ldb_msg_new(tmp_ctx);
+       if (msg == NULL) {
+               talloc_free(tmp_ctx);
+               return ldb_module_oom(module);
+       }
        msg->dn = ntds_settings_dn;
 
        ldb_msg_add_linearized_dn(msg, "msDS-EnabledFeature", op_feature_msg->dn);
@@ -1152,12 +1342,57 @@ static int rootdse_schemaupdatenow(struct ldb_module *module, struct ldb_request
                return ldb_next_request(module, req);
        }
 
+       /*
+        * schemaUpdateNow has been requested. Allow this to refresh the schema
+        * even if we're currently in the middle of a transaction
+        */
+       ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_expected", (void *)1);
+       if (ret != LDB_SUCCESS) {
+               return ldb_operr(ldb);
+       }
+
        ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, schema_dn, &ext_res);
        if (ret != LDB_SUCCESS) {
+               ldb_set_opaque(ldb, "dsdb_schema_refresh_expected", (void *)0);
                return ldb_operr(ldb);
        }
 
        talloc_free(ext_res);
+
+       ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_expected", (void *)0);
+       if (ret != LDB_SUCCESS) {
+               return ldb_operr(ldb);
+       }
+
+       return ldb_module_done(req, NULL, NULL, ret);
+}
+
+static int rootdse_schemaupgradeinprogress(struct ldb_module *module, struct ldb_request *req)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       int ret = LDB_SUCCESS;
+       struct ldb_dn *schema_dn;
+
+       schema_dn = ldb_get_schema_basedn(ldb);
+       if (!schema_dn) {
+               ldb_reset_err_string(ldb);
+               ldb_debug(ldb, LDB_DEBUG_WARNING,
+                         "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
+               return ldb_next_request(module, req);
+       }
+
+       /* FIXME we have to do something in order to relax constraints for DRS
+        * setting schemaUpgradeInProgress cause the fschemaUpgradeInProgress
+        * in all LDAP connection (2K3/2K3R2) or in the current connection (2K8 and +)
+        * to be set to true.
+        */
+
+       /* from 5.113 LDAPConnections in DRSR.pdf
+        * fschemaUpgradeInProgress: A Boolean that specifies certain constraint
+        * validations are skipped when adding, updating, or removing directory
+        * objects on the opened connection. The skipped constraint validations
+        * are documented in the applicable constraint sections in [MS-ADTS].
+        */
        return ldb_module_done(req, NULL, NULL, ret);
 }
 
@@ -1187,9 +1422,71 @@ static int rootdse_add(struct ldb_module *module, struct ldb_request *req)
        return LDB_ERR_NAMING_VIOLATION;
 }
 
+static int rootdse_start_trans(struct ldb_module *module)
+{
+       int ret;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct rootdse_private_data *data = talloc_get_type_abort(ldb_module_get_private(module),
+                                                                 struct rootdse_private_data);
+       ret = ldb_next_start_trans(module);
+       if (ret == LDB_SUCCESS) {
+               if (data->private_ev != NULL) {
+                       return ldb_operr(ldb);
+               }
+               data->private_ev = s4_event_context_init(data);
+               if (data->private_ev == NULL) {
+                       return ldb_operr(ldb);
+               }
+               data->saved_ev = ldb_get_event_context(ldb);
+               ldb_set_event_context(ldb, data->private_ev);
+       }
+       return ret;
+}
+
+static int rootdse_end_trans(struct ldb_module *module)
+{
+       int ret;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct rootdse_private_data *data = talloc_get_type_abort(ldb_module_get_private(module),
+                                                                 struct rootdse_private_data);
+       ret = ldb_next_end_trans(module);
+       if (data->saved_ev == NULL) {
+               return ldb_operr(ldb);
+       }
+
+       if (data->private_ev != ldb_get_event_context(ldb)) {
+               return ldb_operr(ldb);
+       }
+       ldb_set_event_context(ldb, data->saved_ev);
+       data->saved_ev = NULL;
+       TALLOC_FREE(data->private_ev);
+       return ret;
+}
+
+static int rootdse_del_trans(struct ldb_module *module)
+{
+       int ret;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct rootdse_private_data *data = talloc_get_type_abort(ldb_module_get_private(module),
+                                                                 struct rootdse_private_data);
+       ret = ldb_next_del_trans(module);
+       if (data->saved_ev == NULL) {
+               return ldb_operr(ldb);
+       }
+
+       if (data->private_ev != ldb_get_event_context(ldb)) {
+               return ldb_operr(ldb);
+       }
+       ldb_set_event_context(ldb, data->saved_ev);
+       data->saved_ev = NULL;
+       TALLOC_FREE(data->private_ev);
+       return ret;
+}
+
 struct fsmo_transfer_state {
        struct ldb_context *ldb;
        struct ldb_request *req;
+       struct ldb_module *module;
 };
 
 /*
@@ -1200,23 +1497,40 @@ static void rootdse_fsmo_transfer_callback(struct tevent_req *treq)
        struct fsmo_transfer_state *fsmo = tevent_req_callback_data(treq, struct fsmo_transfer_state);
        NTSTATUS status;
        WERROR werr;
+       int ret;
        struct ldb_request *req = fsmo->req;
        struct ldb_context *ldb = fsmo->ldb;
+       struct ldb_module *module = fsmo->module;
 
        status = dcerpc_drepl_takeFSMORole_recv(treq, fsmo, &werr);
        talloc_free(fsmo);
        if (!NT_STATUS_IS_OK(status)) {
                ldb_asprintf_errstring(ldb, "Failed FSMO transfer: %s", nt_errstr(status));
+               /*
+                * Now that it is failed, start the transaction up
+                * again so the wrappers can close it without additional error
+                */
+               rootdse_start_trans(module);
                ldb_module_done(req, NULL, NULL, LDB_ERR_UNAVAILABLE);
                return;
        }
        if (!W_ERROR_IS_OK(werr)) {
                ldb_asprintf_errstring(ldb, "Failed FSMO transfer: %s", win_errstr(werr));
+               /*
+                * Now that it is failed, start the transaction up
+                * again so the wrappers can close it without additional error
+                */
+               rootdse_start_trans(module);
                ldb_module_done(req, NULL, NULL, LDB_ERR_UNAVAILABLE);
                return;
        }
 
-       ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
+       /*
+        * Now that it is done, start the transaction up again so the
+        * wrappers can close it without error
+        */
+       ret = rootdse_start_trans(module);
+       ldb_module_done(req, NULL, NULL, ret);
 }
 
 static int rootdse_become_master(struct ldb_module *module,
@@ -1251,7 +1565,20 @@ static int rootdse_become_master(struct ldb_module *module,
                                 "RODC cannot become a role master.");
        }
 
-       msg = imessaging_client_init(tmp_ctx, lpcfg_imessaging_path(tmp_ctx, lp_ctx),
+       /*
+        * We always delete the transaction, not commit it, because
+        * this gives the least surprise to this surprising action (as
+        * we will never record anything done to this point
+        */
+       rootdse_del_trans(module);
+
+       /*
+        * We must use the global event loop to run this IRPC in
+        * single process mode
+        */
+       ldb_handle_use_global_event_context(req->handle);
+
+       msg = imessaging_client_init(tmp_ctx, lp_ctx,
                                    ldb_get_event_context(ldb));
        if (!msg) {
                ldb_asprintf_errstring(ldb, "Failed to generate client messaging context in %s", lpcfg_imessaging_path(tmp_ctx, lp_ctx));
@@ -1269,10 +1596,20 @@ static int rootdse_become_master(struct ldb_module *module,
        }
        fsmo->ldb = ldb;
        fsmo->req = req;
+       fsmo->module = module;
 
-       /* we send the call asynchronously, as the ldap client is
+       /*
+        * we send the call asynchronously, as the ldap client is
         * expecting to get an error back if the role transfer fails
+        *
+        * We need more than the default 10 seconds IRPC allows, so
+        * set a longer timeout (default ldb timeout is 300 seconds).
+        * We send an async reply when we are done.
+        *
+        * We are the first module, so don't bother working out how
+        * long we have spent so far.
         */
+       dcerpc_binding_handle_set_timeout(irpc_handle, req->timeout);
 
        treq = dcerpc_drepl_takeFSMORole_send(req, ldb_get_event_context(ldb), irpc_handle, role);
        if (treq == NULL) {
@@ -1330,6 +1667,9 @@ static int rootdse_modify(struct ldb_module *module, struct ldb_request *req)
        if (ldb_msg_find_element(req->op.mod.message, "enableOptionalFeature")) {
                return rootdse_enableoptionalfeature(module, req);
        }
+       if (ldb_msg_find_element(req->op.mod.message, "schemaUpgradeInProgress")) {
+               return rootdse_schemaupgradeinprogress(module, req);
+       }
 
        ldb_set_errstring(ldb, "rootdse_modify: unknown attribute to change!");
        return LDB_ERR_UNWILLING_TO_PERFORM;
@@ -1405,15 +1745,18 @@ static int rootdse_extended(struct ldb_module *module, struct ldb_request *req)
 }
 
 static const struct ldb_module_ops ldb_rootdse_module_ops = {
-       .name           = "rootdse",
-       .init_context   = rootdse_init,
-       .search         = rootdse_search,
-       .request        = rootdse_request,
-       .add            = rootdse_add,
-       .modify         = rootdse_modify,
-       .rename         = rootdse_rename,
-       .extended       = rootdse_extended,
-       .del            = rootdse_delete
+       .name              = "rootdse",
+       .init_context      = rootdse_init,
+       .search            = rootdse_search,
+       .request           = rootdse_request,
+       .add               = rootdse_add,
+       .modify            = rootdse_modify,
+       .rename            = rootdse_rename,
+       .extended          = rootdse_extended,
+       .del               = rootdse_delete,
+       .start_transaction = rootdse_start_trans,
+       .end_transaction   = rootdse_end_trans,
+       .del_transaction   = rootdse_del_trans
 };
 
 int ldb_rootdse_module_init(const char *version)