r25755: Fix a couple of memory leaks, in particular a new leak onto the NULL
[jra/samba/.git] / source4 / lib / ldb / common / ldb_modules.c
index 28a619ddebd650b2b398124c8cf7b66ab98923af..87e5b2eeb35b1201f7cd22ec2d72d659a8275496 100644 (file)
@@ -1,4 +1,3 @@
-
 /* 
    ldb database library
 
@@ -11,7 +10,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,8 +18,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
  *  Author: Simo Sorce
  */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_includes.h"
 
 #if (_SAMBA_BUILD_ >= 4)
+#include "includes.h"
 #include "build.h"
-#include "dynconfig.h"
 #endif
 
 #define LDB_MODULE_PREFIX      "modules:"
 #define LDB_MODULE_PREFIX_LEN  8
 
+void ldb_set_modules_dir(struct ldb_context *ldb, const char *path)
+{
+       talloc_free(ldb->modules_dir);
+       ldb->modules_dir = talloc_strdup(ldb, path);
+}
+
 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
 {
        int i, len;
@@ -135,7 +138,7 @@ static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
        return NULL;
 }
 
-#ifndef STATIC_ldb_MODULES
+#ifndef STATIC_LIBLDB_MODULES
 
 #ifdef HAVE_LDB_LDAP
 #define LDAP_INIT ldb_ldap_init,
@@ -149,14 +152,13 @@ static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
 #define SQLITE3_INIT
 #endif
 
-#define STATIC_ldb_MODULES \
+#define STATIC_LIBLDB_MODULES \
        {       \
                LDAP_INIT \
                SQLITE3_INIT \
                ldb_tdb_init,   \
                ldb_operational_init,   \
                ldb_rdn_name_init,      \
-               ldb_objectclass_init,   \
                ldb_paged_results_init, \
                ldb_sort_init,          \
                ldb_asq_init,           \
@@ -166,7 +168,7 @@ static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
 
 int ldb_global_init(void)
 {
-       static int (*static_init_fns[])(void) = STATIC_ldb_MODULES;
+       static int (*static_init_fns[])(void) = STATIC_LIBLDB_MODULES;
 
        static int initialized = 0;
        int ret = 0, i;
@@ -206,22 +208,12 @@ int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
        char *path;
        void *handle;
        int (*init_fn) (void);
-       char *modulesdir;
-
-#ifdef HAVE_DLOPEN
-       if (getenv("LD_LDB_MODULE_PATH") != NULL) {
-               modulesdir = talloc_strdup(ldb, getenv("LD_LDB_MODULE_PATH"));
-       } else {
-#ifdef _SAMBA_BUILD_
-               modulesdir = talloc_asprintf(ldb, "%s/ldb", dyn_MODULESDIR);
-#else
-               modulesdir = talloc_strdup(ldb, MODULESDIR);
-#endif
-       }
 
-       path = talloc_asprintf(ldb, "%s/%s.%s", modulesdir, name, SHLIBEXT);
+       if (ldb->modules_dir == NULL)
+               return -1;
 
-       talloc_free(modulesdir);
+       path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name, 
+                              SHLIBEXT);
 
        ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
 
@@ -241,10 +233,6 @@ int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
        talloc_free(path);
 
        return init_fn();
-#else
-       ldb_debug(ldb, LDB_DEBUG_TRACE, "no dlopen() - not trying to load %s module\n", name);
-       return -1;
-#endif
 }
 
 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
@@ -336,7 +324,7 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
                ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
                talloc_steal(mods_dn, res);
                if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
-                       ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
+                       ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
                } else {
                        if (ret != LDB_SUCCESS) {
                                ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
@@ -359,15 +347,17 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
 
        if (modules != NULL) {
                ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
-               talloc_free(modules);
                if (ret != LDB_SUCCESS) {
+                       talloc_free(mem_ctx);
                        return ret;
                }
        } else {
-               ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
+               ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
        }
 
-       return ldb_init_module_chain(ldb, ldb->modules);
+       ret = ldb_init_module_chain(ldb, ldb->modules);
+       talloc_free(mem_ctx);
+       return ret;
 }
 
 /*
@@ -392,29 +382,49 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
 
 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
 {
+       int ret;
        switch (request->operation) {
        case LDB_SEARCH:
                FIND_OP(module, search);
-               return module->ops->search(module, request);
+               ret = module->ops->search(module, request);
+               break;
        case LDB_ADD:
                FIND_OP(module, add);
-               return module->ops->add(module, request);
+               ret = module->ops->add(module, request);
+               break;
        case LDB_MODIFY:
                FIND_OP(module, modify);
-               return module->ops->modify(module, request);
+               ret = module->ops->modify(module, request);
+               break;
        case LDB_DELETE:
                FIND_OP(module, del);
-               return module->ops->del(module, request);
+               ret = module->ops->del(module, request);
+               break;
        case LDB_RENAME:
                FIND_OP(module, rename);
-               return module->ops->rename(module, request);
+               ret = module->ops->rename(module, request);
+               break;
+       case LDB_EXTENDED:
+               FIND_OP(module, extended);
+               ret = module->ops->extended(module, request);
+               break;
        case LDB_SEQUENCE_NUMBER:
                FIND_OP(module, sequence_number);
-               return module->ops->sequence_number(module, request);
+               ret = module->ops->sequence_number(module, request);
+               break;
        default:
                FIND_OP(module, request);
-               return module->ops->request(module, request);
+               ret = module->ops->request(module, request);
+               break;
+       }
+       if (ret == LDB_SUCCESS) {
+               return ret;
        }
+       if (!ldb_errstring(module->ldb)) {
+               /* Set a default error string, to place the blame somewhere */
+               ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
+       }
+       return ret;
 }
 
 int ldb_next_init(struct ldb_module *module)