From 5d0aa16dfcf3047a52d3dd7e12ffb704a9725e83 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 5 Mar 2006 16:05:26 +0000 Subject: r13839: Use registration mechanism for backends as well (in the same sense 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 | 61 ++++++++++++++++++++++--------- source4/lib/ldb/common/ldb_modules.c | 16 +++++++- source4/lib/ldb/config.mk | 3 ++ source4/lib/ldb/include/ldb_private.h | 21 +++-------- source4/lib/ldb/ldb_ildap/ldb_ildap.c | 6 ++- source4/lib/ldb/ldb_ldap/ldb_ldap.c | 6 ++- source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c | 6 ++- source4/lib/ldb/ldb_tdb/ldb_tdb.c | 7 +++- source4/torture/torture.c | 3 ++ 9 files changed, 91 insertions(+), 38 deletions(-) diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c index 9d8783324c..b664ba680d 100644 --- a/source4/lib/ldb/common/ldb.c +++ b/source4/lib/ldb/common/ldb.c @@ -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; diff --git a/source4/lib/ldb/common/ldb_modules.c b/source4/lib/ldb/common/ldb_modules.c index 17c5231e54..53394e7047 100644 --- a/source4/lib/ldb/common/ldb_modules.c +++ b/source4/lib/ldb/common/ldb_modules.c @@ -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, \ diff --git a/source4/lib/ldb/config.mk b/source4/lib/ldb/config.mk index 82b98fa123..fe7b03251d 100644 --- a/source4/lib/ldb/config.mk +++ b/source4/lib/ldb/config.mk @@ -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 \ diff --git a/source4/lib/ldb/include/ldb_private.h b/source4/lib/ldb/include/ldb_private.h index ba6823559f..52ea0f833d 100644 --- a/source4/lib/ldb/include/ldb_private.h +++ b/source4/lib/ldb/include/ldb_private.h @@ -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, diff --git a/source4/lib/ldb/ldb_ildap/ldb_ildap.c b/source4/lib/ldb/ldb_ildap/ldb_ildap.c index 3467e65bf9..ab37ab7570 100644 --- a/source4/lib/ldb/ldb_ildap/ldb_ildap.c +++ b/source4/lib/ldb/ldb_ildap/ldb_ildap.c @@ -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); +} diff --git a/source4/lib/ldb/ldb_ldap/ldb_ldap.c b/source4/lib/ldb/ldb_ldap/ldb_ldap.c index d671afb953..3a5039ddc0 100644 --- a/source4/lib/ldb/ldb_ldap/ldb_ldap.c +++ b/source4/lib/ldb/ldb_ldap/ldb_ldap.c @@ -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); +} diff --git a/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c b/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c index bf6cf0951b..883b21e0b5 100644 --- a/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c +++ b/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c @@ -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); +} diff --git a/source4/lib/ldb/ldb_tdb/ldb_tdb.c b/source4/lib/ldb/ldb_tdb/ldb_tdb.c index 0825348658..5470fccb97 100644 --- a/source4/lib/ldb/ldb_tdb/ldb_tdb.c +++ b/source4/lib/ldb/ldb_tdb/ldb_tdb.c @@ -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); +} diff --git a/source4/torture/torture.c b/source4/torture/torture.c index 26137b1890..4aece9565b 100644 --- a/source4/torture/torture.c +++ b/source4/torture/torture.c @@ -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(); -- cgit