r9685: Add tests for samba3sam mapping module
[sfrench/samba-autobuild/.git] / source4 / lib / ldb / common / ldb_modules.c
index 033717a62b2b90ce4e02f86396f94a053d511d1c..20e8ad061ead1065a605fbc0a6edc2df0ec30835 100644 (file)
 #include <sys/stat.h> 
 #include <unistd.h> 
 
-#define LDB_MODULE_PREFIX      "modules"
-#define LDB_MODULE_PREFIX_LEN  7
-#define LDB_MODULE_SEP         ':'
+#ifdef HAVE_DLOPEN_DISABLED
+#include <dlfcn.h>
+#endif
+
+#define LDB_MODULE_PREFIX      "modules:"
+#define LDB_MODULE_PREFIX_LEN  8
+
+static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
+{
+       int i, len;
+       char *trimmed;
+
+       trimmed = talloc_strdup(ldb, string);
+       if (!trimmed) {
+               ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
+               return NULL;
+       }
+
+       len = strlen(trimmed);
+       for (i = 0; trimmed[i] != '\0'; i++) {
+               switch (trimmed[i]) {
+               case ' ':
+               case '\t':
+               case '\n':
+                       memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
+                       break;
+               }
+       }
+
+       return trimmed;
+}
+
+
+/* modules are called in inverse order on the stack.
+   Lets place them as an admin would think the right order is.
+   Modules order is imprtant */
+static char **ldb_modules_list_from_string(struct ldb_context *ldb, const char *string)
+{
+       char **modules = NULL;
+       char *modstr, *p;
+       int i;
+
+       /* spaces not admitted */
+       modstr = talloc_strdup_no_spaces(ldb, string);
+       if ( ! modstr) {
+               return NULL;
+       }
+
+       modules = talloc_realloc(ldb, modules, char *, 2);
+       if ( ! modules ) {
+               ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
+               talloc_free(modstr);
+               return NULL;
+       }
+       talloc_steal(modules, modstr);
+
+       i = 0;
+       while ((p = strrchr(modstr, ',')) != NULL) {
+               *p = '\0';
+               p++;
+               modules[i] = p;
+
+               i++;
+               modules = talloc_realloc(ldb, modules, char *, i + 2);
+               if ( ! modules ) {
+                       ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
+                       return NULL;
+               }
+
+       }
+       modules[i] = modstr;
+
+       modules[i + 1] = NULL;
+
+       return modules;
+}
 
 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
 {
-       struct ldb_module *current;
-       char **modules;
-       char *p, *q;
-       int pn, i;
+       char **modules = NULL;
+       int i;
 
        /* find out which modules we are requested to activate */
-       modules = NULL;
-       pn = 0;
 
+       /* check if we have a custom module list passd as ldb option */
        if (options) {
                for (i = 0; options[i] != NULL; i++) {
-                       if (strncmp(options[i], LDB_MODULE_PREFIX, 
-                                   LDB_MODULE_PREFIX_LEN) == 0) {
-                               p = q = talloc_strdup(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
-                               if (*q != ':') {
-                                       talloc_free(q);
-                                       return -1;
-                               }
-                               do {
-                                       *p = '\0';
-                                       q = p + 1;
-                                       pn++;
-                                       modules = talloc_realloc_p(ldb, modules, char *, pn);
-                                       if (!modules) {
-                                               ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
-                                               return -1;
-                                       }
-                                       modules[pn - 1] = q;
-                               } while ((p = strchr(q, LDB_MODULE_SEP)));
+                       if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
+                               modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
                        }
                }
        }
 
-       if (!modules && strcmp("ldap", ldb->modules->ops->name)) { 
-               /* no modules in the options, look for @MODULES in the
-                  db (not for ldap) */
-               int ret, j, k;
-               const char * const attrs[] = { "@MODULE" , NULL};
+       /* if not overloaded by options and the backend is not ldap try to load the modules list form ldb */
+       if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
+               int ret;
+               const char * const attrs[] = { "@LIST" , NULL};
                struct ldb_message **msg = NULL;
+               struct ldb_dn *mods;
+
+               mods = ldb_dn_explode(ldb, "@MODULES");
+               if (mods == NULL) {
+                       return -1;
+               }
 
-               ret = ldb_search(ldb, "", LDB_SCOPE_BASE, "dn=@MODULES", attrs, &msg);
-               if (ret == 0) {
+               ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &msg);
+               talloc_free(mods);
+               if (ret == 0 || (ret == 1 && msg[0]->num_elements == 0)) {
                        ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
                } else {
                        if (ret < 0) {
@@ -97,110 +157,104 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
                        }
                        if (ret > 1) {
                                ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found, bailing out\n");
+                               talloc_free(msg);
                                return -1;
                        }
 
-                       for (j = 0; j < msg[0]->num_elements; j++) {
-                               for (k = 0; k < msg[0]->elements[j].num_values; k++) {
-                                       pn++;
-                                       modules = talloc_realloc_p(ldb, modules, char *, pn);
-                                       if (!modules) {
-                                               ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
-                                               return -1;
-                                       }
-                                       modules[pn - 1] = talloc_strndup(modules, msg[0]->elements[j].values[k].data, msg[0]->elements[j].values[k].length);
-                                       if (!modules[pn - 1]) {
-                                               ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
-                                               return -1;
-                                       }
-                               }
-                       }
+                       modules = ldb_modules_list_from_string(ldb, msg[0]->elements[0].values[0].data);
+
                }
+
                talloc_free(msg);
        }
 
-       if (modules) {
-               for (i = 0; i < pn; i++) {
-                       if (strcmp(modules[i], "timestamps") == 0) {
-                               current = timestamps_module_init(ldb, options);
-                               if (!current) {
-                                       ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
-                                       return -1;
-                               }
-                               DLIST_ADD(ldb->modules, current);
-                               continue;
-                       }
+       if (modules == NULL) {
+               ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
+               return 0;
+       }
 
-                       if (strcmp(modules[i], "schema") == 0) {
-                               current = schema_module_init(ldb, options);
-                               if (!current) {
-                                       ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
-                                       return -1;
-                               }
-                               DLIST_ADD(ldb->modules, current);
-                               continue;
-                       }
+       for (i = 0; modules[i] != NULL; i++) {
+               struct ldb_module *current;
 
-#ifdef HAVE_DLOPEN_DISABLED
-               {
-                       void *handle;
-                       ldb_module_init_function init;
-                       struct stat st;
-                       const char *errstr;
-
-                       if (stat(modules[i], &st) < 0) {
-                               ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
+               if (strcmp(modules[i], "schema") == 0) {
+                       current = schema_module_init(ldb, options);
+                       if (!current) {
+                               ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
                                return -1;
                        }
+                       DLIST_ADD(ldb->modules, current);
+                       continue;
+               }
 
-                       handle = dlopen(modules[i], RTLD_LAZY);
+               if (strcmp(modules[i], "timestamps") == 0) {
+                       current = timestamps_module_init(ldb, options);
+                       if (!current) {
+                               ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
+                               return -1;
+                       }
+                       DLIST_ADD(ldb->modules, current);
+                       continue;
+               }
 
-                       if (!handle) {
-                               ldb_debug(ldb, LDB_DEBUG_FATAL, "Error loading module %s [%s]\n", modules[i], dlerror());
+               if (strcmp(modules[i], "rdn_name") == 0) {
+                       current = rdn_name_module_init(ldb, options);
+                       if (!current) {
+                               ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
                                return -1;
                        }
+                       DLIST_ADD(ldb->modules, current);
+                       continue;
+               }
 
-                       init = (ldb_module_init_function)dlsym(handle, "init_module");
+#ifdef _SAMBA_BUILD_
+               if (strcmp(modules[i], "objectguid") == 0) {
+                       current = objectguid_module_init(ldb, options);
+                       if (!current) {
+                               ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
+                               return -1;
+                       }
+                       DLIST_ADD(ldb->modules, current);
+                       continue;
+               }
 
-                       errstr = dlerror();
-                       if (errstr) {
-                               ldb_debug(ldb, LDB_DEBUG_FATAL, "Error trying to resolve symbol 'init_module' in %s [%s]\n", modules[i], errstr);
+               if (strcmp(modules[i], "samldb") == 0) {
+                       current = samldb_module_init(ldb, options);
+                       if (!current) {
+                               ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
                                return -1;
                        }
+                       DLIST_ADD(ldb->modules, current);
+                       continue;
+               }
 
-                       current = init(ldb, options);
+               if (strcmp(modules[i], "samba3sam") == 0) {
+                       current = ldb_samba3sam_module_init(ldb, options);
                        if (!current) {
                                ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
                                return -1;
                        }
                        DLIST_ADD(ldb->modules, current);
+                       continue;
                }
-#else
-               ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
-               return -1;
+
 #endif
-               }
+
+               ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", modules[i]);
        }
 
+       talloc_free(modules);
        return 0; 
 }
 
 /*
    helper functions to call the next module in chain
 */
-int ldb_next_close(struct ldb_module *module)
-{
-       if (!module->next) {
-               return -1;
-       }
-       return module->next->ops->close(module->next);
-}
 
 int ldb_next_search(struct ldb_module *module, 
-              const char *base,
-              enum ldb_scope scope,
-              const char *expression,
-              const char * const *attrs, struct ldb_message ***res)
+                   const struct ldb_dn *base,
+                   enum ldb_scope scope,
+                   const char *expression,
+                   const char * const *attrs, struct ldb_message ***res)
 {
        if (!module->next) {
                return -1;
@@ -208,12 +262,16 @@ int ldb_next_search(struct ldb_module *module,
        return module->next->ops->search(module->next, base, scope, expression, attrs, res);
 }
 
-int ldb_next_search_free(struct ldb_module *module, struct ldb_message **msg)
+int ldb_next_search_bytree(struct ldb_module *module, 
+                          const struct ldb_dn *base,
+                          enum ldb_scope scope,
+                          struct ldb_parse_tree *tree,
+                          const char * const *attrs, struct ldb_message ***res)
 {
        if (!module->next) {
                return -1;
        }
-       return module->next->ops->search_free(module->next, msg);
+       return module->next->ops->search_bytree(module->next, base, scope, tree, attrs, res);
 }
 
 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
@@ -232,7 +290,7 @@ int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *
        return module->next->ops->modify_record(module->next, message);
 }
 
-int ldb_next_delete_record(struct ldb_module *module, const char *dn)
+int ldb_next_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
 {
        if (!module->next) {
                return -1;
@@ -240,7 +298,7 @@ int ldb_next_delete_record(struct ldb_module *module, const char *dn)
        return module->next->ops->delete_record(module->next, dn);
 }
 
-int ldb_next_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
+int ldb_next_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
 {
        if (!module->next) {
                return -1;