#include "includes.h"
#include "dlinklist.h"
+#include "lib/events/events.h"
+#include "lib/tdb/include/tdb.h"
#include "lib/ldb/include/ldb.h"
+#include "lib/ldb/samba/ldif_handlers.h"
+#include "db_wrap.h"
-static struct ldb_wrap *ldb_list;
static struct tdb_wrap *tdb_list;
/*
if (DEBUGLEVEL < 4 && level > LDB_DEBUG_WARNING) {
return;
}
+ if (DEBUGLEVEL < 2 && level > LDB_DEBUG_ERROR) {
+ return;
+ }
vasprintf(&s, fmt, ap);
if (!s) return;
DEBUG(level, ("ldb: %s\n", s));
free(s);
}
-
-/* destroy the last connection to a ldb */
-static int ldb_wrap_destructor(void *ctx)
+char *wrap_casefold(void *context, void *mem_ctx, const char *s)
{
- struct ldb_wrap *w = ctx;
- ldb_close(w->ldb);
- DLIST_REMOVE(ldb_list, w);
- return 0;
-}
+ return strupper_talloc(mem_ctx, s);
+}
/*
wrapped connection to a ldb database
- to close just talloc_free() the ldb_wrap pointer
+ to close just talloc_free() the returned ldb_context
*/
-struct ldb_wrap *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
- const char *url,
- unsigned int flags,
- const char *options[])
+struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
+ const char *url,
+ struct auth_session_info *session_info,
+ struct cli_credentials *credentials,
+ unsigned int flags,
+ const char *options[])
{
- struct ldb_wrap *w;
+ struct ldb_context *ldb;
+ int ret;
+ struct event_context *ev;
+ char *real_url = NULL;
- for (w=ldb_list;w;w=w->next) {
- if (strcmp(url, w->url) == 0) {
- return talloc_reference(mem_ctx, w);
- }
+ ldb = ldb_init(mem_ctx);
+ if (ldb == NULL) {
+ return NULL;
}
- w = talloc_p(mem_ctx, struct ldb_wrap);
- if (w == NULL) {
+ /* we want to use the existing event context if possible. This
+ relies on the fact that in smbd, everything is a child of
+ the main event_context */
+ ev = event_context_find(ldb);
+
+ if (ldb_set_opaque(ldb, "EventContext", ev)) {
+ talloc_free(ldb);
return NULL;
}
- w->url = talloc_strdup(w, url);
+ if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
+ talloc_free(ldb);
+ return NULL;
+ }
- w->ldb = ldb_connect(url, flags, options);
- if (w->ldb == NULL) {
- talloc_free(w);
+ if (ldb_set_opaque(ldb, "credentials", credentials)) {
+ talloc_free(ldb);
return NULL;
}
- talloc_set_destructor(w, ldb_wrap_destructor);
- ldb_set_debug(w->ldb, ldb_wrap_debug, NULL);
+ ret = ldb_register_samba_handlers(ldb);
+ if (ret == -1) {
+ talloc_free(ldb);
+ return NULL;
+ }
- DLIST_ADD(ldb_list, w);
+ real_url = private_path(ldb, url);
+ if (real_url == NULL) {
+ talloc_free(ldb);
+ return NULL;
+ }
- return w;
+ /* allow admins to force non-sync ldb for all databases */
+ if (lp_parm_bool(-1, "ldb", "nosync", False)) {
+ flags |= LDB_FLG_NOSYNC;
+ }
+
+ ret = ldb_connect(ldb, real_url, flags, options);
+ if (ret == -1) {
+ talloc_free(ldb);
+ return NULL;
+ }
+
+ talloc_free(real_url);
+
+ ldb_set_debug(ldb, ldb_wrap_debug, NULL);
+
+ ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
+
+ return ldb;
}
va_end(ap);
if (ptr != NULL) {
- DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
+ const char *name = tdb_name(tdb);
+ DEBUG(level, ("tdb(%s): %s", name ? name : "unnamed", ptr));
free(ptr);
}
}
}
}
- w = talloc_p(mem_ctx, struct tdb_wrap);
+ w = talloc(mem_ctx, struct tdb_wrap);
if (w == NULL) {
return NULL;
}