s4-schema consolidate schema handling
[amitay/samba.git] / source4 / dsdb / samdb / ldb_modules / schema_load.c
index 175fe9367d8997bfd37d4b96ee04daced3bec0dd..ec574b33e3bc6489e45909c95e71892877267b44 100644 (file)
@@ -38,7 +38,7 @@ struct schema_load_private_data {
 static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_dn, uint64_t current_usn,
                               struct dsdb_schema **schema);
 
-struct dsdb_schema *dsdb_schema_refresh(struct ldb_module *module, struct dsdb_schema *schema, bool is_global_schema)
+static struct dsdb_schema *dsdb_schema_refresh(struct ldb_module *module, struct dsdb_schema *schema, bool is_global_schema)
 {
        uint64_t current_usn;
        int ret;
@@ -121,7 +121,7 @@ struct dsdb_schema *dsdb_schema_refresh(struct ldb_module *module, struct dsdb_s
        schema->reload_seq_number = tseqr->seq_num;
        talloc_free(res);
                
-       ret = dsdb_module_load_partition_usn(module, schema->base_dn, &current_usn, NULL);
+       ret = dsdb_module_load_partition_usn(module, schema->base_dn, &current_usn, NULL, NULL);
        if (ret != LDB_SUCCESS || current_usn == schema->loaded_usn) {
                return schema;
        }
@@ -150,8 +150,7 @@ static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_
        char *error_string;
        int ret;
        struct ldb_result *schema_res;
-       struct ldb_result *a_res;
-       struct ldb_result *c_res;
+       struct ldb_result *res;
        static const char *schema_attrs[] = {
                "prefixMap",
                "schemaInfo",
@@ -174,7 +173,7 @@ static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_
         */
        ret = dsdb_module_search_dn(module, tmp_ctx, &schema_res,
                                    schema_dn, schema_attrs,
-                                   DSDB_FLAG_NEXT_MODULE);
+                                   DSDB_FLAG_NEXT_MODULE, NULL);
        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
                ldb_reset_err_string(ldb);
                ldb_debug(ldb, LDB_DEBUG_WARNING,
@@ -190,34 +189,21 @@ static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_
        /*
         * load the attribute definitions
         */
-       ret = dsdb_module_search(module, tmp_ctx, &a_res,
-                                schema_dn, LDB_SCOPE_ONELEVEL, NULL,
-                                DSDB_FLAG_NEXT_MODULE,
-                                "(objectClass=attributeSchema)");
-       if (ret != LDB_SUCCESS) {
-               ldb_asprintf_errstring(ldb, 
-                                      "dsdb_schema: failed to search attributeSchema objects: %s",
-                                      ldb_errstring(ldb));
-               goto failed;
-       }
-
-       /*
-        * load the objectClass definitions
-        */
-       ret = dsdb_module_search(module, tmp_ctx, &c_res,
+       ret = dsdb_module_search(module, tmp_ctx, &res,
                                 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
                                 DSDB_FLAG_NEXT_MODULE |
                                 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT,
-                                "(objectClass=classSchema)");
+                                NULL,
+                                "(|(objectClass=attributeSchema)(objectClass=classSchema))");
        if (ret != LDB_SUCCESS) {
                ldb_asprintf_errstring(ldb, 
-                                      "dsdb_schema: failed to search classSchema objects: %s",
+                                      "dsdb_schema: failed to search attributeSchema and classSchema objects: %s",
                                       ldb_errstring(ldb));
                goto failed;
        }
 
        ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
-                                          schema_res, a_res, c_res, schema, &error_string);
+                                          schema_res, res, schema, &error_string);
        if (ret != LDB_SUCCESS) {
                ldb_asprintf_errstring(ldb, 
                                       "dsdb_schema load failed: %s",
@@ -239,9 +225,7 @@ static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_
        /* "dsdb_set_schema()" steals schema into the ldb_context */
        ret = dsdb_set_schema(ldb, (*schema));
 
-       if (*schema != NULL) {
-               (*schema)->refresh_in_progress = false;
-       }
+       (*schema)->refresh_in_progress = false;
 
        if (ret != LDB_SUCCESS) {
                ldb_debug_set(ldb, LDB_DEBUG_FATAL,
@@ -299,7 +283,7 @@ static int schema_load_init(struct ldb_module *module)
                return LDB_SUCCESS;
        }
 
-       ret = dsdb_module_load_partition_usn(module, schema_dn, &current_usn, NULL);
+       ret = dsdb_module_load_partition_usn(module, schema_dn, &current_usn, NULL, NULL);
        if (ret != LDB_SUCCESS) {
                /* Ignore the error and just reload the DB more often */
                current_usn = 0;
@@ -318,25 +302,24 @@ static int schema_load_start_transaction(struct ldb_module *module)
        return ldb_next_start_trans(module);
 }
 
-static int schema_load_del_transaction(struct ldb_module *module)
+static int schema_load_end_transaction(struct ldb_module *module)
 {
        struct schema_load_private_data *private_data =
                talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
 
        private_data->in_transaction = false;
 
-       return ldb_next_del_trans(module);
+       return ldb_next_end_trans(module);
 }
 
-static int schema_load_prepare_commit(struct ldb_module *module)
+static int schema_load_del_transaction(struct ldb_module *module)
 {
-       int ret;
        struct schema_load_private_data *private_data =
                talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
 
-       ret = ldb_next_prepare_commit(module);
        private_data->in_transaction = false;
-       return ret;
+
+       return ldb_next_del_trans(module);
 }
 
 static int schema_load_extended(struct ldb_module *module, struct ldb_request *req)
@@ -354,11 +337,17 @@ static int schema_load_extended(struct ldb_module *module, struct ldb_request *r
 }
 
 
-_PUBLIC_ const struct ldb_module_ops ldb_schema_load_module_ops = {
+static const struct ldb_module_ops ldb_schema_load_module_ops = {
        .name           = "schema_load",
        .init_context   = schema_load_init,
        .extended       = schema_load_extended,
        .start_transaction = schema_load_start_transaction,
-       .prepare_commit    = schema_load_prepare_commit,
+       .end_transaction   = schema_load_end_transaction,
        .del_transaction   = schema_load_del_transaction,
 };
+
+int ldb_schema_load_module_init(const char *version)
+{
+       LDB_MODULE_CHECK_VERSION(version);
+       return ldb_register_module(&ldb_schema_load_module_ops);
+}