Fix the mess with ldb includes.
[metze/samba/wip.git] / source4 / lib / ldb / ldb_tdb / ldb_cache.c
index 6bbf31dee766440d5e171aa26db576451021b78f..43b965f239a0c419b8d5475267d12a41ae4c40bc 100644 (file)
  *  Author: Andrew Tridgell
  */
 
-#include "ldb_includes.h"
-
 #include "ldb_tdb.h"
 
 #define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
 #define LTDB_FLAG_INTEGER          (1<<1)
 #define LTDB_FLAG_HIDDEN           (1<<2)
-#define LTDB_FLAG_OBJECTCLASS      (1<<3)
 
 /* valid attribute flags */
 static const struct {
@@ -58,10 +55,14 @@ static const struct {
 */
 static void ltdb_attributes_unload(struct ldb_module *module)
 {
-       struct ltdb_private *ltdb = module->private_data;
+       struct ldb_context *ldb;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
        struct ldb_message *msg;
        int i;
 
+       ldb = ldb_module_get_ctx(module);
+
        if (ltdb->cache->attributes == NULL) {
                /* no previously loaded attributes */
                return;
@@ -69,7 +70,7 @@ static void ltdb_attributes_unload(struct ldb_module *module)
 
        msg = ltdb->cache->attributes;
        for (i=0;i<msg->num_elements;i++) {
-               ldb_schema_attribute_remove(module->ldb, msg->elements[i].name);
+               ldb_schema_attribute_remove(ldb, msg->elements[i].name);
        }
 
        talloc_free(ltdb->cache->attributes);
@@ -105,20 +106,26 @@ static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
 */
 static int ltdb_attributes_load(struct ldb_module *module)
 {
-       struct ltdb_private *ltdb = module->private_data;
+       struct ldb_context *ldb;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
        struct ldb_message *msg = ltdb->cache->attributes;
        struct ldb_dn *dn;
        int i, r;
 
-       dn = ldb_dn_new(module, module->ldb, LTDB_ATTRIBUTES);
+       ldb = ldb_module_get_ctx(module);
+
+       dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
        if (dn == NULL) goto failed;
 
        r = ltdb_search_dn1(module, dn, msg);
+       talloc_free(dn);
        if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
-               talloc_free(dn);
                goto failed;
        }
-       talloc_free(dn);
+       if (r == LDB_ERR_NO_SUCH_OBJECT) {
+               return 0;
+       }
        /* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
           but its close enough for now */
        for (i=0;i<msg->num_elements;i++) {
@@ -127,7 +134,7 @@ static int ltdb_attributes_load(struct ldb_module *module)
                const struct ldb_schema_syntax *s;
 
                if (ltdb_attributes_flags(&msg->elements[i], &flags) != 0) {
-                       ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
+                       ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid @ATTRIBUTES element for '%s'\n", msg->elements[i].name);
                        goto failed;
                }
                switch (flags & ~LTDB_FLAG_HIDDEN) {
@@ -141,22 +148,22 @@ static int ltdb_attributes_load(struct ldb_module *module)
                        syntax = LDB_SYNTAX_INTEGER;
                        break;
                default:
-                       ldb_debug(module->ldb, LDB_DEBUG_ERROR, 
+                       ldb_debug(ldb, LDB_DEBUG_ERROR, 
                                  "Invalid flag combination 0x%x for '%s' in @ATTRIBUTES\n",
                                  flags, msg->elements[i].name);
                        goto failed;
                }
 
-               s = ldb_standard_syntax_by_name(module->ldb, syntax);
+               s = ldb_standard_syntax_by_name(ldb, syntax);
                if (s == NULL) {
-                       ldb_debug(module->ldb, LDB_DEBUG_ERROR, 
+                       ldb_debug(ldb, LDB_DEBUG_ERROR, 
                                  "Invalid attribute syntax '%s' for '%s' in @ATTRIBUTES\n",
                                  syntax, msg->elements[i].name);
                        goto failed;
                }
 
                flags |= LDB_ATTR_FLAG_ALLOCATED;
-               if (ldb_schema_attribute_add_with_syntax(module->ldb, msg->elements[i].name, flags, s) != 0) {
+               if (ldb_schema_attribute_add_with_syntax(ldb, msg->elements[i].name, flags, s) != 0) {
                        goto failed;
                }
        }
@@ -167,72 +174,14 @@ failed:
 }
 
 
-/*
-  register any subclasses from @SUBCLASSES
-*/
-static int ltdb_subclasses_load(struct ldb_module *module)
-{
-       struct ltdb_private *ltdb = module->private_data;
-       struct ldb_message *msg = ltdb->cache->subclasses;
-       struct ldb_dn *dn;
-       int i, j, r;
-
-       dn = ldb_dn_new(module, module->ldb, LTDB_SUBCLASSES);
-       if (dn == NULL) goto failed;
-
-       r = ltdb_search_dn1(module, dn, msg);
-       if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
-               talloc_free(dn);
-               goto failed;
-       }
-       talloc_free(dn);
-
-       for (i=0;i<msg->num_elements;i++) {
-               struct ldb_message_element *el = &msg->elements[i];
-               for (j=0;j<el->num_values;j++) {
-                       if (ldb_subclass_add(module->ldb, el->name, 
-                                            (char *)el->values[j].data) != 0) {
-                               goto failed;
-                       }
-               }
-       }
-
-       return 0;
-failed:
-       return -1;
-}
-
-
-/*
-  de-register any @SUBCLASSES
-*/
-static void ltdb_subclasses_unload(struct ldb_module *module)
-{
-       struct ltdb_private *ltdb = module->private_data;
-       struct ldb_message *msg;
-       int i;
-
-       if (ltdb->cache->subclasses == NULL) {
-               /* no previously loaded subclasses */
-               return;
-       }
-
-       msg = ltdb->cache->subclasses;
-       for (i=0;i<msg->num_elements;i++) {
-               ldb_subclass_remove(module->ldb, msg->elements[i].name);
-       }
-
-       talloc_free(ltdb->cache->subclasses);
-       ltdb->cache->subclasses = NULL;
-}
-
-
 /*
   initialise the baseinfo record
 */
 static int ltdb_baseinfo_init(struct ldb_module *module)
 {
-       struct ltdb_private *ltdb = module->private_data;
+       struct ldb_context *ldb;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
        struct ldb_message *msg;
        struct ldb_message_element el;
        struct ldb_val val;
@@ -242,6 +191,8 @@ static int ltdb_baseinfo_init(struct ldb_module *module)
           out. */
        const char *initial_sequence_number = "1";
 
+       ldb = ldb_module_get_ctx(module);
+
        ltdb->sequence_number = atof(initial_sequence_number);
 
        msg = talloc(ltdb, struct ldb_message);
@@ -251,7 +202,7 @@ static int ltdb_baseinfo_init(struct ldb_module *module)
 
        msg->num_elements = 1;
        msg->elements = &el;
-       msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
+       msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
        if (!msg->dn) {
                goto failed;
        }
@@ -285,7 +236,8 @@ failed:
  */
 static void ltdb_cache_free(struct ldb_module *module)
 {
-       struct ltdb_private *ltdb = module->private_data;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
 
        ltdb->sequence_number = 0;
        talloc_free(ltdb->cache);
@@ -298,7 +250,6 @@ static void ltdb_cache_free(struct ldb_module *module)
 int ltdb_cache_reload(struct ldb_module *module)
 {
        ltdb_attributes_unload(module);
-       ltdb_subclasses_unload(module);
        ltdb_cache_free(module);
        return ltdb_cache_load(module);
 }
@@ -308,13 +259,17 @@ int ltdb_cache_reload(struct ldb_module *module)
 */
 int ltdb_cache_load(struct ldb_module *module)
 {
-       struct ltdb_private *ltdb = module->private_data;
-       struct ldb_dn *baseinfo_dn = NULL;
+       struct ldb_context *ldb;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+       struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
        struct ldb_dn *indexlist_dn = NULL;
        uint64_t seq;
-       struct ldb_message *baseinfo = NULL;
+       struct ldb_message *baseinfo = NULL, *options = NULL;
        int r;
 
+       ldb = ldb_module_get_ctx(module);
+
        /* a very fast check to avoid extra database reads */
        if (ltdb->cache != NULL && 
            tdb_get_seqnum(ltdb->tdb) == ltdb->tdb_seqnum) {
@@ -325,10 +280,8 @@ int ltdb_cache_load(struct ldb_module *module)
                ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
                if (ltdb->cache == NULL) goto failed;
                ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
-               ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
                ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
                if (ltdb->cache->indexlist == NULL ||
-                   ltdb->cache->subclasses == NULL ||
                    ltdb->cache->attributes == NULL) {
                        goto failed;
                }
@@ -337,7 +290,7 @@ int ltdb_cache_load(struct ldb_module *module)
        baseinfo = talloc(ltdb->cache, struct ldb_message);
        if (baseinfo == NULL) goto failed;
 
-       baseinfo_dn = ldb_dn_new(module, module->ldb, LTDB_BASEINFO);
+       baseinfo_dn = ldb_dn_new(module, ldb, LTDB_BASEINFO);
        if (baseinfo_dn == NULL) goto failed;
 
        r= ltdb_search_dn1(module, baseinfo_dn, baseinfo);
@@ -346,7 +299,7 @@ int ltdb_cache_load(struct ldb_module *module)
        }
        
        /* possibly initialise the baseinfo */
-       if (!baseinfo->dn) {
+       if (r == LDB_ERR_NO_SUCH_OBJECT) {
                if (ltdb_baseinfo_init(module) != LDB_SUCCESS) {
                        goto failed;
                }
@@ -365,25 +318,40 @@ int ltdb_cache_load(struct ldb_module *module)
        }
        ltdb->sequence_number = seq;
 
+       /* Read an interpret database options */
+       options = talloc(ltdb->cache, struct ldb_message);
+       if (options == NULL) goto failed;
+
+       options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
+       if (options_dn == NULL) goto failed;
+
+       r= ltdb_search_dn1(module, options_dn, options);
+       if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
+               goto failed;
+       }
+       
+       /* set flag for checking base DN on searches */
+       if (r == LDB_SUCCESS) {
+               ltdb->check_base = ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
+       } else {
+               ltdb->check_base = false;
+       }
+
        talloc_free(ltdb->cache->last_attribute.name);
        memset(&ltdb->cache->last_attribute, 0, sizeof(ltdb->cache->last_attribute));
 
        ltdb_attributes_unload(module);
-       ltdb_subclasses_unload(module);
 
        talloc_free(ltdb->cache->indexlist);
-       talloc_free(ltdb->cache->subclasses);
 
        ltdb->cache->indexlist = talloc_zero(ltdb->cache, struct ldb_message);
-       ltdb->cache->subclasses = talloc_zero(ltdb->cache, struct ldb_message);
        ltdb->cache->attributes = talloc_zero(ltdb->cache, struct ldb_message);
        if (ltdb->cache->indexlist == NULL ||
-           ltdb->cache->subclasses == NULL ||
            ltdb->cache->attributes == NULL) {
                goto failed;
        }
            
-       indexlist_dn = ldb_dn_new(module, module->ldb, LTDB_INDEXLIST);
+       indexlist_dn = ldb_dn_new(module, ldb, LTDB_INDEXLIST);
        if (indexlist_dn == NULL) goto failed;
 
        r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist);
@@ -394,17 +362,16 @@ int ltdb_cache_load(struct ldb_module *module)
        if (ltdb_attributes_load(module) == -1) {
                goto failed;
        }
-       if (ltdb_subclasses_load(module) == -1) {
-               goto failed;
-       }
 
 done:
+       talloc_free(options);
        talloc_free(baseinfo);
        talloc_free(baseinfo_dn);
        talloc_free(indexlist_dn);
        return 0;
 
 failed:
+       talloc_free(options);
        talloc_free(baseinfo);
        talloc_free(baseinfo_dn);
        talloc_free(indexlist_dn);
@@ -417,7 +384,9 @@ failed:
 */
 int ltdb_increase_sequence_number(struct ldb_module *module)
 {
-       struct ltdb_private *ltdb = module->private_data;
+       struct ldb_context *ldb;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
        struct ldb_message *msg;
        struct ldb_message_element el[2];
        struct ldb_val val;
@@ -426,6 +395,8 @@ int ltdb_increase_sequence_number(struct ldb_module *module)
        char *s = NULL;
        int ret;
 
+       ldb = ldb_module_get_ctx(module);
+
        msg = talloc(ltdb, struct ldb_message);
        if (msg == NULL) {
                errno = ENOMEM;
@@ -440,7 +411,7 @@ int ltdb_increase_sequence_number(struct ldb_module *module)
 
        msg->num_elements = ARRAY_SIZE(el);
        msg->elements = el;
-       msg->dn = ldb_dn_new(msg, module->ldb, LTDB_BASEINFO);
+       msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
        if (msg->dn == NULL) {
                talloc_free(msg);
                errno = ENOMEM;