r23560: - Activate metze's schema modules (from metze's schema-loading-13 patch).
[kamenim/samba.git] / source4 / dsdb / samdb / ldb_modules / schema_fsmo.c
index 1a83e3a9bf52961eebbc1622f138a3358f2ada8c..3df887acb6337b651a4deb2240ba7ff8f2b560de 100644 (file)
@@ -4,7 +4,7 @@
    The module that handles the Schema FSMO Role Owner
    checkings, it also loads the dsdb_schema.
    
-   Copyright (C) Stefan Metzmacher 2007
+   Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -38,9 +38,11 @@ static int schema_fsmo_init(struct ldb_module *module)
        TALLOC_CTX *mem_ctx;
        struct ldb_dn *schema_dn;
        struct dsdb_schema *schema;
+       struct dsdb_schema_fsmo *schema_fsmo;
        struct ldb_result *schema_res;
        const struct ldb_val *prefix_val;
        const struct ldb_val *info_val;
+       struct ldb_val info_val_default;
        struct ldb_result *a_res;
        struct ldb_result *c_res;
        uint32_t i;
@@ -48,13 +50,18 @@ static int schema_fsmo_init(struct ldb_module *module)
        static const char *schema_attrs[] = {
                "prefixMap",
                "schemaInfo",
+               "fSMORoleOwner",
                NULL
        };
 
+       if (dsdb_get_schema(module->ldb)) {
+         return ldb_next_init(module);
+       }
+
        schema_dn = samdb_schema_dn(module->ldb);
        if (!schema_dn) {
-               ldb_debug(module->ldb, LDB_DEBUG_TRACE,
-                         "schema_fsmo_init: no schema dn present: (skip schema loading)");
+               ldb_debug(module->ldb, LDB_DEBUG_WARNING,
+                         "schema_fsmo_init: no schema dn present: (skip schema loading)\n");
                return ldb_next_init(module);
        }
 
@@ -64,6 +71,13 @@ static int schema_fsmo_init(struct ldb_module *module)
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
+       if (!schema_fsmo) {
+               ldb_oom(module->ldb);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       module->private_data = schema_fsmo;
+
        schema = talloc_zero(mem_ctx, struct dsdb_schema);
        if (!schema) {
                ldb_oom(module->ldb);
@@ -79,20 +93,20 @@ static int schema_fsmo_init(struct ldb_module *module)
                         &schema_res);
        if (ret != LDB_SUCCESS) {
                ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: failed to search the schema head: %d:%s",
+                             "schema_fsmo_init: failed to search the schema head: %d:%s\n",
                              ret, ldb_strerror(ret));
                talloc_free(mem_ctx);
                return ret;
        }
        talloc_steal(mem_ctx, schema_res);
        if (schema_res->count == 0) {
-               ldb_debug(module->ldb, LDB_DEBUG_TRACE,
-                         "schema_fsmo_init: no schema head present: (skip schema loading)");
+               ldb_debug(module->ldb, LDB_DEBUG_WARNING,
+                         "schema_fsmo_init: no schema head present: (skip schema loading)\n");
                talloc_free(mem_ctx);
                return ldb_next_init(module);
        } else if (schema_res->count > 1) {
                ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: [%u] schema heads found on a base search",
+                             "schema_fsmo_init: [%u] schema heads found on a base search\n",
                              schema_res->count);
                talloc_free(mem_ctx);
                return LDB_ERR_CONSTRAINT_VIOLATION;
@@ -101,22 +115,25 @@ static int schema_fsmo_init(struct ldb_module *module)
        prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
        if (!prefix_val) {
                ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: no prefixMap attribute found");
+                             "schema_fsmo_init: no prefixMap attribute found\n");
                talloc_free(mem_ctx);
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
        info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
        if (!info_val) {
-               ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: no schemaInfo attribute found");
-               talloc_free(mem_ctx);
-               return LDB_ERR_CONSTRAINT_VIOLATION;
+               info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
+               if (!info_val_default.data) {
+                       ldb_oom(module->ldb);
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
+               talloc_steal(mem_ctx, info_val_default.data);
+               info_val = &info_val_default;
        }
 
        status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
        if (!W_ERROR_IS_OK(status)) {
                ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: failed to load oid mappings: %s",
+                             "schema_fsmo_init: failed to load oid mappings: %s\n",
                              win_errstr(status));
                talloc_free(mem_ctx);
                return LDB_ERR_CONSTRAINT_VIOLATION;
@@ -131,7 +148,7 @@ static int schema_fsmo_init(struct ldb_module *module)
                         &a_res);
        if (ret != LDB_SUCCESS) {
                ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: failed to search attributeSchema objects: %d:%s",
+                             "schema_fsmo_init: failed to search attributeSchema objects: %d:%s\n",
                              ret, ldb_strerror(ret));
                talloc_free(mem_ctx);
                return ret;
@@ -150,7 +167,7 @@ static int schema_fsmo_init(struct ldb_module *module)
                status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
                if (!W_ERROR_IS_OK(status)) {
                        ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                                     "schema_fsmo_init: failed to load attriute definition: %s:%s",
+                                     "schema_fsmo_init: failed to load attriute definition: %s:%s\n",
                                      ldb_dn_get_linearized(a_res->msgs[i]->dn),
                                      win_errstr(status));
                        talloc_free(mem_ctx);
@@ -170,7 +187,7 @@ static int schema_fsmo_init(struct ldb_module *module)
                         &c_res);
        if (ret != LDB_SUCCESS) {
                ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: failed to search classSchema objects: %d:%s",
+                             "schema_fsmo_init: failed to search classSchema objects: %d:%s\n",
                              ret, ldb_strerror(ret));
                talloc_free(mem_ctx);
                return ret;
@@ -189,7 +206,7 @@ static int schema_fsmo_init(struct ldb_module *module)
                status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
                if (!W_ERROR_IS_OK(status)) {
                        ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                                     "schema_fsmo_init: failed to load class definition: %s:%s",
+                                     "schema_fsmo_init: failed to load class definition: %s:%s\n",
                                      ldb_dn_get_linearized(c_res->msgs[i]->dn),
                                      win_errstr(status));
                        talloc_free(mem_ctx);
@@ -200,16 +217,34 @@ static int schema_fsmo_init(struct ldb_module *module)
        }
        talloc_free(c_res);
 
+       /* dsdb_set_schema() steal schema into the ldb_context */
        ret = dsdb_set_schema(module->ldb, schema);
        if (ret != LDB_SUCCESS) {
                ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
-                             "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
+                             "schema_fsmo_init: dsdb_set_schema() failed: %d:%s\n",
                              ret, ldb_strerror(ret));
                talloc_free(mem_ctx);
                return ret;
        }
 
-       talloc_steal(module, schema);
+       schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
+       if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
+               schema_fsmo->we_are_master = true;
+       } else {
+               schema_fsmo->we_are_master = false;
+       }
+
+       if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
+               ldb_oom(module->ldb);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       talloc_steal(module, schema_fsmo);
+
+       ldb_debug(module->ldb, LDB_DEBUG_TRACE,
+                         "schema_fsmo_init: we are master: %s\n",
+                         (schema_fsmo->we_are_master?"yes":"no"));
+
        talloc_free(mem_ctx);
        return ldb_next_init(module);
 }