r13839: Use registration mechanism for backends as well (in the same sense
authorJelmer Vernooij <jelmer@samba.org>
Sun, 5 Mar 2006 16:05:26 +0000 (16:05 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:52:14 +0000 (13:52 -0500)
my previous patch added it for modules). This is the next step towards
LDB backends and modules as run-time loadable .so files.
(This used to be commit fb2f70de4f6c4a9b13ad590cb4d3a9c858cede49)

source4/lib/ldb/common/ldb.c
source4/lib/ldb/common/ldb_modules.c
source4/lib/ldb/config.mk
source4/lib/ldb/include/ldb_private.h
source4/lib/ldb/ldb_ildap/ldb_ildap.c
source4/lib/ldb/ldb_ldap/ldb_ldap.c
source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
source4/lib/ldb/ldb_tdb/ldb_tdb.c
source4/torture/torture.c

index 9d8783324ca5209404465a1f5d4483e0099f9ef3..b664ba680d035905d1763642ca739e0d738eac96 100644 (file)
@@ -55,6 +55,40 @@ struct ldb_context *ldb_init(void *mem_ctx)
        return ldb;
 }
 
+static struct ldb_backend {
+       const char *name;
+       ldb_connect_fn connect_fn;
+       struct ldb_backend *prev, *next;
+} *ldb_backends = NULL;
+/*
+ register a new ldb backend
+*/
+int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
+{
+       struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
+
+       /* Maybe check for duplicity here later on? */
+
+       backend->name = talloc_strdup(backend, url_prefix);
+       backend->connect_fn = connectfn;
+       DLIST_ADD(ldb_backends, backend);
+
+       return LDB_SUCCESS;
+}
+
+static ldb_connect_fn ldb_find_backend(const char *url)
+{
+       struct ldb_backend *backend;
+
+       for (backend = ldb_backends; backend; backend = backend->next) {
+               if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
+                       return backend->connect_fn;
+               }
+       }
+
+       return NULL;
+}
+
 /* 
  connect to a database. The URL can either be one of the following forms
    ldb://path
@@ -68,31 +102,22 @@ struct ldb_context *ldb_init(void *mem_ctx)
 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
 {
        int ret;
+       ldb_connect_fn fn;
 
-       if (strncmp(url, "tdb:", 4) == 0 ||
-           strchr(url, ':') == NULL) {
-               ret = ltdb_connect(ldb, url, flags, options);
+       if (strchr(url, ':') != NULL) {
+               fn = ldb_find_backend(url);
+       } else {
+               /* Default to tdb */
+               fn = ldb_find_backend("tdb:");
        }
 
-#if HAVE_ILDAP
-       else if (strncmp(url, "ldap", 4) == 0) {
-               ret = ildb_connect(ldb, url, flags, options);
-       }
-#elif HAVE_LDAP
-       else if (strncmp(url, "ldap", 4) == 0) {
-               ret = lldb_connect(ldb, url, flags, options);
-       }
-#endif
-#if HAVE_SQLITE3
-       else if (strncmp(url, "sqlite:", 7) == 0) {
-                ret = lsqlite3_connect(ldb, url, flags, options);
-       }
-#endif
-       else {
+       if (fn == NULL) {
                ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
                return LDB_ERR_OTHER;
        }
 
+       ret = fn(ldb, url, flags, options);
+
        if (ret != LDB_SUCCESS) {
                ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
                return ret;
index 17c5231e548149a60f74cca4dc78660e040f062d..53394e7047ff31f9d78dcb66e788e497fa07bbd0 100644 (file)
@@ -126,10 +126,24 @@ static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
        return NULL;
 }
 
-       
+#ifdef HAVE_LDAP
+#define LDAP_INIT ldb_ldap_init,
+#else
+#define LDAP_INIT
+#endif
+
+#ifdef HAVE_SQLITE3
+#define SQLITE3_INIT ldb_sqlite3_init,
+#else
+#define SQLITE3_INIT
+#endif
+
 #ifndef STATIC_LIBLDB_MODULES
 #define STATIC_LIBLDB_MODULES \
        {       \
+               LDAP_INIT \
+               SQLITE3_INIT \
+               ldb_tdb_init,   \
                ldb_schema_init,        \
                ldb_operational_init,   \
                ldb_rdn_name_init,      \
index 82b98fa123f51db55926bee2510123f73b2dc231..fe7b03251dd20b4990ad473927720dcb46c8d707 100644 (file)
@@ -80,6 +80,7 @@ OBJ_FILES = \
 [MODULE::libldb_ildap]
 SUBSYSTEM = LIBLDB
 OUTPUT_TYPE = MERGEDOBJ
+INIT_FUNCTION = ldb_ildap_init
 OBJ_FILES = \
                ldb_ildap/ldb_ildap.o
 REQUIRED_SUBSYSTEMS = \
@@ -112,6 +113,7 @@ OBJ_FILES = modules/skel.o
 [MODULE::libldb_sqlite3]
 SUBSYSTEM = LIBLDB
 OUTPUT_TYPE = MERGEDOBJ
+INIT_FUNCTION = ldb_sqlite3_init
 OBJ_FILES = \
                ldb_sqlite3/ldb_sqlite3.o
 REQUIRED_SUBSYSTEMS = \
@@ -124,6 +126,7 @@ NOPROTO = YES
 # Start MODULE libldb_tdb
 [MODULE::libldb_tdb]
 SUBSYSTEM = LIBLDB
+INIT_FUNCTION = ldb_tdb_init
 OUTPUT_TYPE = MERGEDOBJ
 OBJ_FILES = \
                ldb_tdb/ldb_tdb.o \
index ba6823559f112c9699e8451ca1223206edad8dac..52ea0f833d59fbbf92462ea23e820ec149bf2fec 100644 (file)
@@ -64,6 +64,7 @@ struct ldb_module_ops {
        int (*async_wait)(struct ldb_module *, struct ldb_async_handle *, enum ldb_async_wait_type);
 };
 
+typedef int (*ldb_connect_fn) (struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[]);
 
 /*
   schema related information needed for matching rules
@@ -133,6 +134,7 @@ void ldb_set_errstring(struct ldb_context *ldb, char *err_string);
 void ldb_reset_err_string(struct ldb_context *ldb);
 
 int ldb_register_module(const struct ldb_module_ops *);
+int ldb_register_backend(const char *url_prefix, ldb_connect_fn);
 
 /* The following definitions come from lib/ldb/common/ldb_debug.c  */
 void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
@@ -142,27 +144,16 @@ void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level,
 /* The following definitions come from lib/ldb/common/ldb_ldif.c  */
 int ldb_should_b64_encode(const struct ldb_val *val);
 
-int ltdb_connect(struct ldb_context *ldb, const char *url, 
-                unsigned int flags, 
-                const char *options[]);
-int lldb_connect(struct ldb_context *ldb, const char *url, 
-                unsigned int flags, 
-                const char *options[]);
-int ildb_connect(struct ldb_context *ldb,
-                const char *url, 
-                unsigned int flags, 
-                const char *options[]);
-int lsqlite3_connect(struct ldb_context *ldb,
-                    const char *url, 
-                    unsigned int flags, 
-                    const char *options[]);
-
 int ldb_objectclass_init(void);
 int ldb_operational_init(void);
 int ldb_paged_results_init(void);
 int ldb_rdn_name_init(void);
 int ldb_schema_init(void);
 int ldb_sort_init(void);
+int ldb_ldap_init(void);
+int ldb_ildap_init(void);
+int ldb_tdb_init(void);
+int ldb_sqlite3_init(void);
 
 int ldb_match_msg(struct ldb_context *ldb,
                  struct ldb_message *msg,
index 3467e65bf9f3bdf5d0f0180726b3d117fee5b914..ab37ab757049b895ad80fc9d3056c21fb9cf4063 100644 (file)
@@ -987,7 +987,7 @@ static const struct ldb_module_ops ildb_ops = {
 /*
   connect to the database
 */
-int ildb_connect(struct ldb_context *ldb, const char *url, 
+static int ildb_connect(struct ldb_context *ldb, const char *url, 
                 unsigned int flags, const char *options[])
 {
        struct ildb_private *ildb = NULL;
@@ -1065,3 +1065,7 @@ failed:
        return -1;
 }
 
+int ldb_ildap_init(void)
+{
+       return ldb_register_backend("ldap", ildb_connect);
+}
index d671afb953cfc636e2d4c26cc1eb337fbbae37ac..3a5039ddc0febe050ddfae7a7a1878021adda4eb 100644 (file)
@@ -1042,7 +1042,7 @@ static int lldb_destructor(void *p)
 /*
   connect to the database
 */
-int lldb_connect(struct ldb_context *ldb,
+static int lldb_connect(struct ldb_context *ldb,
                 const char *url, 
                 unsigned int flags, 
                 const char *options[])
@@ -1093,3 +1093,7 @@ failed:
        return -1;
 }
 
+int ldb_ldap_init(void)
+{
+       return ldb_register_backend("ldap", lldb_connect);
+}
index bf6cf0951b3f84d019dbbb50e300b81fa9b76587..883b21e0b50f6f373b1857cf1724b21274eaeffb 100644 (file)
@@ -2070,7 +2070,7 @@ static const struct ldb_module_ops lsqlite3_ops = {
 /*
  * connect to the database
  */
-int lsqlite3_connect(struct ldb_context *ldb,
+static int lsqlite3_connect(struct ldb_context *ldb,
                     const char *url, 
                     unsigned int flags, 
                     const char *options[])
@@ -2137,3 +2137,7 @@ failed:
        return -1;
 }
 
+int ldb_sqlite3_init(void)
+{
+       return ldb_register_backend("sqlite3", lsqlite3_connect);
+}
index 082534865868dae0fd1e70ba62c1602931cb89a0..5470fccb97c80d1239d91a2e33dde840107551e4 100644 (file)
@@ -1033,7 +1033,7 @@ static const struct ldb_module_ops ltdb_ops = {
 /*
   connect to the database
 */
-int ltdb_connect(struct ldb_context *ldb, const char *url, 
+static int ltdb_connect(struct ldb_context *ldb, const char *url, 
                 unsigned int flags, const char *options[])
 {
        const char *path;
@@ -1094,3 +1094,8 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 
        return 0;
 }
+
+int ldb_tdb_init(void)
+{
+       return ldb_register_backend("tdb:", ltdb_connect);
+}
index 26137b18908820016ca03a652adafe2c3deb054d..4aece9565b398a902176f2f7d779a8835421d6be 100644 (file)
@@ -26,6 +26,7 @@
 #include "system/filesys.h"
 #include "libcli/raw/ioctl.h"
 #include "libcli/libcli.h"
+#include "lib/ldb/include/ldb.h"
 #include "librpc/rpc/dcerpc_table.h"
 
 #include "torture/basic/proto.h"
@@ -2632,6 +2633,8 @@ static void max_runtime_handler(int sig)
                alarm(max_runtime);
        }
 
+       ldb_global_init();
+
        dcerpc_init();
 
        dcerpc_table_init();