r3005: added talloc wrappers around tdb_open() and ldb_connect(), so that the
authorAndrew Tridgell <tridge@samba.org>
Sat, 16 Oct 2004 13:47:00 +0000 (13:47 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:59:56 +0000 (12:59 -0500)
caller doesn't have to worry about the constraint of only opening a
database a single time in a process. These wrappers will ensure that
only a single open is done, and will auto-close when the last instance
is gone.

When you are finished with a database pointer, use talloc_free() to
close it.

note that this code does not take account of the threads process
model, and does not yet take account of symlinks or hard links to tdb
files.

14 files changed:
source/include/db_wrap.h [new file with mode: 0644]
source/include/includes.h
source/intl/lang_tdb.c
source/lib/basic.mk
source/lib/db_wrap.c [new file with mode: 0644]
source/lib/gencache.c
source/lib/messages.c
source/lib/tdb/config.mk
source/lib/tdb_helper.c
source/libcli/unexpected.c
source/passdb/secrets.c
source/rpc_server/netlogon/schannel_state.c
source/rpc_server/samr/samdb.c
source/smbd/rewrite.c

diff --git a/source/include/db_wrap.h b/source/include/db_wrap.h
new file mode 100644 (file)
index 0000000..50e3af6
--- /dev/null
@@ -0,0 +1,38 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   database wrap headers
+
+   Copyright (C) Andrew Tridgell 2004
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+
+struct ldb_wrap {
+       struct ldb_context *ldb;
+
+       const char *url;
+       struct ldb_wrap *next, *prev;
+};
+
+
+struct tdb_wrap {
+       TDB_CONTEXT *tdb;
+
+       const char *name;
+       struct tdb_wrap *next, *prev;
+};
+
index 86ddc81e579ed4f04104167205af1f80971858c4..85e4299efdf74c9e84570e2702b9aa1b214a7e97 100644 (file)
@@ -619,6 +619,7 @@ extern int errno;
 #include "lib/tdb/include/spinlock.h"
 #include "lib/tdb/include/tdbutil.h"
 #include "talloc.h"
+#include "db_wrap.h"
 #include "nt_status.h"
 #include "interfaces.h"
 #include "trans2.h"
index 64f020f26ab23ca0685677f52313894f17ae00bf..05e3a7ba638f5ae137fbbb3fb52bc61b284f41a7 100644 (file)
@@ -139,9 +139,9 @@ BOOL lang_tdb_init(const char *lang)
 
        asprintf(&path, "%s%s.tdb", lock_path(mem_ctx, "lang_"), lang);
 
-       tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
+       tdb = tdb_open(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
        if (!tdb) {
-               tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
+               tdb = tdb_open(path, 0, TDB_DEFAULT, O_RDONLY, 0);
                free(path);
                free(msg_path);
                talloc_destroy(mem_ctx);
index addef77a20b90b53628d622989dab14a069d4b77..93309ce68b0d317b14da797cc5b11791602b4608 100644 (file)
@@ -50,7 +50,7 @@ ADD_OBJ_FILES = \
                lib/crypto/md5.o \
                lib/crypto/hmacmd5.o \
                lib/crypto/md4.o \
-               lib/tdb_helper.o \
+               lib/db_wrap.o \
                lib/server_mutex.o 
 REQUIRED_SUBSYSTEMS = \
                LIBTDB CHARSET
diff --git a/source/lib/db_wrap.c b/source/lib/db_wrap.c
new file mode 100644 (file)
index 0000000..251741c
--- /dev/null
@@ -0,0 +1,170 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   database wrap functions
+
+   Copyright (C) Andrew Tridgell 2004
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+/*
+  the stupidity of the unix fcntl locking design forces us to never
+  allow a database file to be opened twice in the same process. These
+  wrappers provide convenient access to a tdb or ldb, taking advantage
+  of talloc destructors to ensure that only a single open is done
+*/
+
+#include "includes.h"
+
+static struct ldb_wrap *ldb_list;
+static struct tdb_wrap *tdb_list;
+
+/*
+  this is used to catch debug messages from ldb
+*/
+static void ldb_wrap_debug(void *context, enum ldb_debug_level level, 
+                          const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
+
+static void ldb_wrap_debug(void *context, enum ldb_debug_level level, 
+                          const char *fmt, va_list ap)
+{
+       char *s = NULL;
+       if (DEBUGLEVEL < 4 && level > LDB_DEBUG_WARNING) {
+               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)
+{
+       struct ldb_wrap *w = ctx;
+       ldb_close(w->ldb);
+       DLIST_REMOVE(ldb_list, w);
+       return 0;
+}                               
+
+/*
+  wrapped connection to a ldb database
+  to close just talloc_free() the ldb_wrap pointer
+ */
+struct ldb_wrap *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
+                                 const char *url,
+                                 unsigned int flags,
+                                 const char *options[])
+{
+       struct ldb_wrap *w;
+
+       for (w=ldb_list;w;w=w->next) {
+               if (strcmp(url, w->url) == 0) {
+                       return talloc_reference(mem_ctx, w);
+               }
+       }
+
+       w = talloc_p(mem_ctx, struct ldb_wrap);
+       if (w == NULL) {
+               return NULL;
+       }
+
+       w->url = talloc_strdup(w, url);
+
+       w->ldb = ldb_connect(url, flags, options);
+       if (w->ldb == NULL) {
+               talloc_free(w);
+               return NULL;
+       }
+
+       talloc_set_destructor(w, ldb_wrap_destructor);
+       ldb_set_debug(w->ldb, ldb_wrap_debug, NULL);
+
+       DLIST_ADD(ldb_list, w);
+
+       return w;
+}
+
+
+/*
+ Log tdb messages via DEBUG().
+*/
+static void tdb_wrap_log(TDB_CONTEXT *tdb, int level, 
+                        const char *format, ...) PRINTF_ATTRIBUTE(3,4);
+
+static void tdb_wrap_log(TDB_CONTEXT *tdb, int level, 
+                        const char *format, ...)
+{
+       va_list ap;
+       char *ptr = NULL;
+
+       va_start(ap, format);
+       vasprintf(&ptr, format, ap);
+       va_end(ap);
+       
+       if (ptr != NULL) {
+               DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
+               free(ptr);
+       }
+}
+
+
+/* destroy the last connection to a tdb */
+static int tdb_wrap_destructor(void *ctx)
+{
+       struct tdb_wrap *w = ctx;
+       tdb_close(w->tdb);
+       DLIST_REMOVE(tdb_list, w);
+       return 0;
+}                               
+
+/*
+  wrapped connection to a tdb database
+  to close just talloc_free() the tdb_wrap pointer
+ */
+struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
+                              const char *name, int hash_size, int tdb_flags,
+                              int open_flags, mode_t mode)
+{
+       struct tdb_wrap *w;
+
+       for (w=tdb_list;w;w=w->next) {
+               if (strcmp(name, w->name) == 0) {
+                       return talloc_reference(mem_ctx, w);
+               }
+       }
+
+       w = talloc_p(mem_ctx, struct tdb_wrap);
+       if (w == NULL) {
+               return NULL;
+       }
+
+       w->name = talloc_strdup(w, name);
+
+       w->tdb = tdb_open_ex(name, hash_size, tdb_flags, 
+                            open_flags, mode, tdb_wrap_log, NULL);
+       if (w->tdb == NULL) {
+               talloc_free(w);
+               return NULL;
+       }
+
+       talloc_set_destructor(w, tdb_wrap_destructor);
+
+       DLIST_ADD(tdb_list, w);
+
+       return w;
+}
index 1a66d8dc3d4064b457804578937614323e9ce274..a176dc5ae88c705e4cd8b1249503b47aaa8138f1 100644 (file)
@@ -29,7 +29,7 @@
 #define TIMEOUT_LEN 12
 #define CACHE_DATA_FMT "%12u/%s"
 
-static TDB_CONTEXT *cache;
+static struct tdb_wrap *cache;
 
 /**
  * @file gencache.c
@@ -62,8 +62,8 @@ BOOL gencache_init(void)
                return False;
        }
 
-       cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
-                            O_RDWR|O_CREAT, 0644);
+       cache = tdb_wrap_open(NULL, cache_fname, 0, TDB_DEFAULT,
+                             O_RDWR|O_CREAT, 0644);
 
        SAFE_FREE(cache_fname);
        if (!cache) {
@@ -83,10 +83,10 @@ BOOL gencache_init(void)
  
 BOOL gencache_shutdown(void)
 {
-       /* tdb_close routine returns -1 on error */
        if (!cache) return False;
        DEBUG(5, ("Closing cache file\n"));
-       return tdb_close(cache) != -1;
+       talloc_free(cache);
+       return True;
 }
 
 
@@ -125,7 +125,7 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
                   = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout),
                   (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past"));
                
-       ret = tdb_store(cache, keybuf, databuf, 0);
+       ret = tdb_store(cache->tdb, keybuf, databuf, 0);
        SAFE_FREE(valstr);
        SAFE_FREE(keybuf.dptr);
        SAFE_FREE(databuf.dptr);
@@ -178,7 +178,7 @@ BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
                      timeout > time(NULL) ? "ahead" : "in the past"));
 
                
-       ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE);
+       ret = tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE);
 
        SAFE_FREE(datastr);
        SAFE_FREE(old_valstr);
@@ -211,7 +211,7 @@ BOOL gencache_del(const char *keystr)
        keybuf.dptr = strdup(keystr);
        keybuf.dsize = strlen(keystr)+1;
        DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
-       ret = tdb_delete(cache, keybuf);
+       ret = tdb_delete(cache->tdb, keybuf);
        
        SAFE_FREE(keybuf.dptr);
        return ret == 0;
@@ -243,7 +243,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
        
        keybuf.dptr = strdup(keystr);
        keybuf.dsize = strlen(keystr)+1;
-       databuf = tdb_fetch(cache, keybuf);
+       databuf = tdb_fetch(cache->tdb, keybuf);
        SAFE_FREE(keybuf.dptr);
        
        if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
@@ -317,7 +317,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
        if (!gencache_init()) return;
 
        DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
-       node = tdb_search_keys(cache, keystr_pattern);
+       node = tdb_search_keys(cache->tdb, keystr_pattern);
        first_node = node;
        
        while (node) {
@@ -328,7 +328,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
                 * We don't use gencache_get function, because we need to iterate through
                 * all of the entries. Validity verification is up to fn routine.
                 */
-               databuf = tdb_fetch(cache, node->node_key);
+               databuf = tdb_fetch(cache->tdb, node->node_key);
                if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
                        SAFE_FREE(databuf.dptr);
                        SAFE_FREE(keystr);
@@ -360,7 +360,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
 
 int gencache_lock_entry( const char *key )
 {
-       return tdb_lock_bystring(cache, key, 0);
+       return tdb_lock_bystring(cache->tdb, key, 0);
 }
 
 /********************************************************************
@@ -369,8 +369,7 @@ int gencache_lock_entry( const char *key )
 
 void gencache_unlock_entry( const char *key )
 {
-       tdb_unlock_bystring(cache, key);
-       return;
+       tdb_unlock_bystring(cache->tdb, key);
 }
 
 
index b4de7c0145f6e99e80c6a9ea3fe812f6b9a85a5a..64dc2f510a4c2ca48f270d4e03476545e0504da8 100644 (file)
@@ -48,7 +48,7 @@
 #include "includes.h"
 
 /* the locking database handle */
-static TDB_CONTEXT *tdb;
+static struct tdb_wrap *tdb;
 static int received_signal;
 
 /* change the message version with any incompatible changes in the protocol */
@@ -105,9 +105,9 @@ BOOL message_init(void)
                DEBUG(0,("ERROR: No memory to initialise messages database\n"));
                return False;
        }
-       tdb = tdb_open_log(lock_path(mem_ctx, "messages.tdb"), 
-                      0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
-                      O_RDWR|O_CREAT,0600);
+       tdb = tdb_wrap_open(NULL, lock_path(mem_ctx, "messages.tdb"), 
+                           0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
+                           O_RDWR|O_CREAT,0600);
        talloc_destroy(mem_ctx);
 
        if (!tdb) {
@@ -155,7 +155,7 @@ static BOOL message_notify(pid_t pid)
        if (kill(pid, SIGUSR1) == -1) {
                if (errno == ESRCH) {
                        DEBUG(2,("pid %d doesn't exist - deleting messages record\n", (int)pid));
-                       tdb_delete(tdb, message_key_pid(pid));
+                       tdb_delete(tdb->tdb, message_key_pid(pid));
                } else {
                        DEBUG(2,("message to process %d failed - %s\n", (int)pid, strerror(errno)));
                }
@@ -209,18 +209,18 @@ static BOOL message_send_pid_internal(pid_t pid, int msg_type, const void *buf,
 
                /* lock the record for the destination */
                if (timeout) {
-                       if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
+                       if (tdb_chainlock_with_timeout(tdb->tdb, kbuf, timeout) == -1) {
                                DEBUG(0,("message_send_pid_internal: failed to get chainlock with timeout %ul.\n", timeout));
                                return False;
                        }
                } else {
-                       if (tdb_chainlock(tdb, kbuf) == -1) {
+                       if (tdb_chainlock(tdb->tdb, kbuf) == -1) {
                                DEBUG(0,("message_send_pid_internal: failed to get chainlock.\n"));
                                return False;
                        }
                }       
-               tdb_append(tdb, kbuf, dbuf);
-               tdb_chainunlock(tdb, kbuf);
+               tdb_append(tdb->tdb, kbuf, dbuf);
+               tdb_chainunlock(tdb->tdb, kbuf);
 
                SAFE_FREE(dbuf.dptr);
                errno = 0;                    /* paranoia */
@@ -229,24 +229,24 @@ static BOOL message_send_pid_internal(pid_t pid, int msg_type, const void *buf,
 
        /* lock the record for the destination */
        if (timeout) {
-               if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
+               if (tdb_chainlock_with_timeout(tdb->tdb, kbuf, timeout) == -1) {
                        DEBUG(0,("message_send_pid_internal: failed to get chainlock with timeout %ul.\n", timeout));
                        return False;
                }
        } else {
-               if (tdb_chainlock(tdb, kbuf) == -1) {
+               if (tdb_chainlock(tdb->tdb, kbuf) == -1) {
                        DEBUG(0,("message_send_pid_internal: failed to get chainlock.\n"));
                        return False;
                }
        }       
 
-       old_dbuf = tdb_fetch(tdb, kbuf);
+       old_dbuf = tdb_fetch(tdb->tdb, kbuf);
 
        if (!old_dbuf.dptr) {
                /* its a new record */
 
-               tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
-               tdb_chainunlock(tdb, kbuf);
+               tdb_store(tdb->tdb, kbuf, dbuf, TDB_REPLACE);
+               tdb_chainunlock(tdb->tdb, kbuf);
 
                SAFE_FREE(dbuf.dptr);
                errno = 0;                    /* paranoia */
@@ -264,7 +264,7 @@ static BOOL message_send_pid_internal(pid_t pid, int msg_type, const void *buf,
 
                if (!memcmp(ptr, &rec, sizeof(rec))) {
                        if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
-                               tdb_chainunlock(tdb, kbuf);
+                               tdb_chainunlock(tdb->tdb, kbuf);
                                DEBUG(10,("message_send_pid_internal: discarding duplicate message.\n"));
                                SAFE_FREE(dbuf.dptr);
                                SAFE_FREE(old_dbuf.dptr);
@@ -277,8 +277,8 @@ static BOOL message_send_pid_internal(pid_t pid, int msg_type, const void *buf,
 
        /* we're adding to an existing entry */
 
-       tdb_append(tdb, kbuf, dbuf);
-       tdb_chainunlock(tdb, kbuf);
+       tdb_append(tdb->tdb, kbuf, dbuf);
+       tdb_chainunlock(tdb->tdb, kbuf);
 
        SAFE_FREE(old_dbuf.dptr);
        SAFE_FREE(dbuf.dptr);
@@ -323,14 +323,14 @@ static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
 
        kbuf = message_key_pid(getpid());
 
-       tdb_chainlock(tdb, kbuf);
-       dbuf = tdb_fetch(tdb, kbuf);
+       tdb_chainlock(tdb->tdb, kbuf);
+       dbuf = tdb_fetch(tdb->tdb, kbuf);
        /*
         * Replace with an empty record to keep the allocated
         * space in the tdb.
         */
-       tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
-       tdb_chainunlock(tdb, kbuf);
+       tdb_store(tdb->tdb, kbuf, null_dbuf, TDB_REPLACE);
+       tdb_chainunlock(tdb->tdb, kbuf);
 
        if (dbuf.dptr == NULL || dbuf.dsize == 0) {
                SAFE_FREE(dbuf.dptr);
index fdde6e7b4f5ca4c85aed70f30b7955c4c84a4633..dba0d3e1d4de1ae767da0671d51294452001ad66 100644 (file)
@@ -28,7 +28,7 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/tdb/tools/tdbtest.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBTDB
+               LIBTDB
 REQUIRED_LIBRARIES = GDBM
 # End BINARY tdbtest
 ################################################
@@ -49,7 +49,7 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/tdb/tools/tdbtorture.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBTDB
+               LIBTDB
 # End BINARY tdbtorture
 ################################################
 
@@ -59,7 +59,7 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/tdb/tools/tdbdump.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBTDB
+               LIBTDB
 # End BINARY tdbdump
 ################################################
 
index 2698e3cc712ae18d98674475119c8bc3ba4f16d1..e29a8a72112fe3d04389678cceca676cf19f18c3 100644 (file)
    dealing with a tdb database a little less cumbersome in Samba */
 
 
-/****************************************************************************
- Log tdb messages via DEBUG().
-****************************************************************************/
-
-static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
-
-static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
-{
-       va_list ap;
-       char *ptr = NULL;
-
-       va_start(ap, format);
-       vasprintf(&ptr, format, ap);
-       va_end(ap);
-       
-       if (!ptr || !*ptr)
-               return;
-
-       DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
-       SAFE_FREE(ptr);
-}
-
-/****************************************************************************
- Like tdb_open() but also setup a logging function that redirects to
- the samba DEBUG() system.
-****************************************************************************/
-
-TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
-                         int open_flags, mode_t mode)
-{
-       TDB_CONTEXT *tdb;
-
-       if (!lp_use_mmap())
-               tdb_flags |= TDB_NOMMAP;
-
-       tdb = tdb_open_ex(name, hash_size, tdb_flags, 
-                                   open_flags, mode, tdb_log, NULL);
-       if (!tdb)
-               return NULL;
-
-       return tdb;
-}
index 22795e6c4c70ed8cf20eb701176bfe6b05cffa35..e109c9d5bf0160c95801be4d23ef0d9f6b0a717d 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "includes.h"
 
-static TDB_CONTEXT *tdbd = NULL;
+static struct tdb_wrap *tdbd = NULL;
 
 /* the key type used in the unexpeceted packet database */
 struct unexpected_key {
@@ -49,9 +49,9 @@ void unexpected_packet(struct packet_struct *p)
        if (!tdbd) {
                mem_ctx = talloc_init("receive_unexpected");
                if (!mem_ctx) return;
-               tdbd = tdb_open_log(lock_path(mem_ctx, "unexpected.tdb"), 0, 
-                              TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
-                              O_RDWR | O_CREAT, 0644);
+               tdbd = tdb_wrap_open(NULL, lock_path(mem_ctx, "unexpected.tdb"), 0, 
+                                    TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
+                                    O_RDWR | O_CREAT, 0644);
                talloc_destroy(mem_ctx);
                if (!tdbd) {
                        return;
@@ -71,7 +71,7 @@ void unexpected_packet(struct packet_struct *p)
        dbuf.dptr = buf;
        dbuf.dsize = len;
 
-       tdb_store(tdbd, kbuf, dbuf, TDB_REPLACE);
+       tdb_store(tdbd->tdb, kbuf, dbuf, TDB_REPLACE);
 }
 
 
@@ -106,7 +106,7 @@ void clear_unexpected(time_t t)
 
        lastt = t;
 
-       tdb_traverse(tdbd, traverse_fn, NULL);
+       tdb_traverse(tdbd->tdb, traverse_fn, NULL);
 }
 
 
@@ -149,23 +149,25 @@ check for a particular packet in the unexpected packet queue
 struct packet_struct *receive_unexpected(enum packet_type packet_type, int id, 
                                         const char *mailslot_name)
 {
-       TDB_CONTEXT *tdb2;
+       struct tdb_wrap *tdb2;
        TALLOC_CTX *mem_ctx;
 
        mem_ctx = talloc_init("receive_unexpected");
        if (!mem_ctx) return NULL;
-       tdb2 = tdb_open_log(lock_path(mem_ctx, "unexpected.tdb"), 0, 0, O_RDONLY, 0);
-       talloc_destroy(mem_ctx);
-       if (!tdb2) return NULL;
+       tdb2 = tdb_wrap_open(mem_ctx, lock_path(mem_ctx, "unexpected.tdb"), 0, 0, O_RDONLY, 0);
+       if (!tdb2) {
+               talloc_destroy(mem_ctx);
+               return NULL;
+       }
 
        matched_packet = NULL;
        match_id = id;
        match_type = packet_type;
        match_name = mailslot_name;
 
-       tdb_traverse(tdb2, traverse_match, NULL);
+       tdb_traverse(tdb2->tdb, traverse_match, NULL);
 
-       tdb_close(tdb2);
+       talloc_destroy(mem_ctx);
 
        return matched_packet;
 }
index 21938db154e4169d304d83ff3be7261328f6a842..60b197de933bac8dcfbbd7fed9f85aecf165fac2 100644 (file)
@@ -27,7 +27,7 @@
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
 
-static TDB_CONTEXT *tdb;
+static struct tdb_wrap *tdb;
 
 /**
  * Use a TDB to store an incrementing random seed.
@@ -41,7 +41,7 @@ static void get_rand_seed(int *new_seed)
 {
        *new_seed = getpid();
        if (tdb) {
-               tdb_change_int32_atomic(tdb, "INFO/random_seed", new_seed, 1);
+               tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
        }
 }
 
@@ -57,7 +57,7 @@ BOOL secrets_init(void)
        pstrcpy(fname, lp_private_dir());
        pstrcat(fname,"/secrets.tdb");
 
-       tdb = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
+       tdb = tdb_wrap_open(NULL, fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
 
        if (!tdb) {
                DEBUG(0,("Failed to open %s\n", fname));
@@ -89,7 +89,7 @@ static void *secrets_fetch(const char *key, size_t *size)
                return NULL;
        kbuf.dptr = strdup(key);
        kbuf.dsize = strlen(key);
-       dbuf = tdb_fetch(tdb, kbuf);
+       dbuf = tdb_fetch(tdb->tdb, kbuf);
        if (size)
                *size = dbuf.dsize;
        free(kbuf.dptr);
@@ -127,7 +127,7 @@ BOOL secrets_named_mutex(const char *name, uint_t timeout, size_t *p_ref_count)
                return False;
 
        if (ref_count == 0) {
-               ret = tdb_lock_bystring(tdb, name, timeout);
+               ret = tdb_lock_bystring(tdb->tdb, name, timeout);
                if (ret == 0)
                        DEBUG(10,("secrets_named_mutex: got mutex for %s\n", name ));
        }
@@ -150,7 +150,7 @@ void secrets_named_mutex_release(const char *name, size_t *p_ref_count)
        SMB_ASSERT(ref_count != 0);
 
        if (ref_count == 1) {
-               tdb_unlock_bystring(tdb, name);
+               tdb_unlock_bystring(tdb->tdb, name);
                DEBUG(10,("secrets_named_mutex: released mutex for %s\n", name ));
        }
 
index 88eabc6d3792dafc0fd98fa32c42e51957841112..1153e4043d2f161479e220f585e37ef7a7cece65 100644 (file)
 /*
   connect to the schannel ldb
 */
-static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
+static struct ldb_wrap *schannel_db_connect(TALLOC_CTX *mem_ctx)
 {
        char *path;
-       struct ldb_context *ldb;
+       struct ldb_wrap *ldb;
 
        path = lock_path(mem_ctx, "schannel.ldb");
        if (!path) {
                return NULL;
        }
        
-       ldb = ldb_connect(path, 0, NULL);
+       ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
        if (!ldb) {
                return NULL;
        }
 
-       ldb_set_alloc(ldb, talloc_realloc_fn, mem_ctx);
+       ldb_set_alloc(ldb->ldb, talloc_realloc_fn, mem_ctx);
        
        return ldb;
 }
@@ -56,7 +56,7 @@ NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
                                    const char *computer_name, 
                                    struct creds_CredentialState *creds)
 {
-       struct ldb_context *ldb;
+       struct ldb_wrap *ldb;
        struct ldb_message msg;
        struct ldb_val val, seed;
        char *s = NULL;
@@ -71,7 +71,7 @@ NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
        asprintf(&s, "%u", (unsigned int)expiry);
 
        if (s == NULL) {
-               ldb_close(ldb);
+               talloc_free(ldb);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -79,7 +79,7 @@ NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(msg);
        msg.dn = talloc_strdup(mem_ctx, computer_name);
        if (msg.dn == NULL) {
-               ldb_close(ldb);
+               talloc_free(ldb);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -89,21 +89,23 @@ NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
        seed.data = creds->seed.data;
        seed.length = sizeof(creds->seed.data);
 
-       ldb_msg_add_value(ldb, &msg, "sessionKey", &val);
-       ldb_msg_add_value(ldb, &msg, "seed", &seed);
-       ldb_msg_add_string(ldb, &msg, "expiry", s);
+       ldb_msg_add_value(ldb->ldb, &msg, "sessionKey", &val);
+       ldb_msg_add_value(ldb->ldb, &msg, "seed", &seed);
+       ldb_msg_add_string(ldb->ldb, &msg, "expiry", s);
 
-       ldb_delete(ldb, msg.dn);
+       ldb_delete(ldb->ldb, msg.dn);
 
-       ret = ldb_add(ldb, &msg);
+       ret = ldb_add(ldb->ldb, &msg);
 
        if (ret != 0) {
-               DEBUG(0,("Unable to add %s to session key db - %s\n", msg.dn, ldb_errstring(ldb)));
-               ldb_close(ldb);
+               DEBUG(0,("Unable to add %s to session key db - %s\n", 
+                        msg.dn, ldb_errstring(ldb->ldb)));
+               talloc_free(ldb);
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
-       ldb_close(ldb);
+       talloc_free(ldb);
+
        return NT_STATUS_OK;
 }
 
@@ -115,7 +117,7 @@ NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
                                    const char *computer_name, 
                                    struct creds_CredentialState *creds)
 {
-       struct ldb_context *ldb;
+       struct ldb_wrap *ldb;
        time_t expiry;
        struct ldb_message **res;
        int ret;
@@ -131,26 +133,26 @@ NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
 
        expr = talloc_asprintf(mem_ctx, "(dn=%s)", computer_name);
        if (expr == NULL) {
-               ldb_close(ldb);
+               talloc_free(ldb);
                return NT_STATUS_NO_MEMORY;
        }
 
-       ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
+       ret = ldb_search(ldb->ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
        if (ret != 1) {
-               ldb_close(ldb);
+               talloc_free(ldb);
                return NT_STATUS_INVALID_HANDLE;
        }
 
        expiry = ldb_msg_find_uint(res[0], "expiry", 0);
        if (expiry < time(NULL)) {
                DEBUG(1,("schannel: attempt to use expired session key for %s\n", computer_name));
-               ldb_close(ldb);
+               talloc_free(ldb);
                return NT_STATUS_INVALID_HANDLE;
        }
 
        val = ldb_msg_find_ldb_val(res[0], "sessionKey");
        if (val == NULL || val->length != 16) {
-               ldb_close(ldb);
+               talloc_free(ldb);
                return NT_STATUS_INVALID_HANDLE;
        }
 
@@ -158,13 +160,13 @@ NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
 
        val = ldb_msg_find_ldb_val(res[0], "seed");
        if (val == NULL || val->length != 8) {
-               ldb_close(ldb);
+               talloc_free(ldb);
                return NT_STATUS_INVALID_HANDLE;
        }
 
        memcpy(creds->seed.data, val->data, 8);
 
-       ldb_close(ldb);
+       talloc_free(ldb);
 
        return NT_STATUS_OK;
 }
index 330741e29d418c1ef34e02747393bf8e494d49fe..3fb8fcef1e5f0a219e889f132ee7f3c269a437ef 100644 (file)
 
 #include "includes.h"
 
-struct samdb_context {
-       struct ldb_context *ldb;
-       struct samdb_context **static_ptr;
-};
-
-
-/*
-  this is used to catch debug messages from ldb
-*/
-void samdb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)  _PRINTF_ATTRIBUTE(3,0)
-{
-       char *s = NULL;
-       if (DEBUGLEVEL < 4 && level > LDB_DEBUG_WARNING) {
-               return;
-       }
-       vasprintf(&s, fmt, ap);
-       if (!s) return;
-       DEBUG(level, ("samdb: %s\n", s));
-       free(s);
-}
-
-/* destroy the last connection to the sam */
-static int samdb_destructor(void *ctx)
-{
-       struct samdb_context *sam_ctx = ctx;
-       ldb_close(sam_ctx->ldb);
-       *(sam_ctx->static_ptr) = NULL;
-       return 0;
-}                               
-
 /*
   connect to the SAM database
   return an opaque context pointer on success, or NULL on failure
  */
 void *samdb_connect(TALLOC_CTX *mem_ctx)
 {
-       static struct samdb_context *ctx;
-       /*
-         the way that unix fcntl locking works forces us to have a
-         static ldb handle here rather than a much more sensible
-         approach of having the ldb handle as part of the
-         samr_Connect() pipe state. Otherwise we would try to open
-         the ldb more than once, and tdb would rightly refuse the
-         second open due to the broken nature of unix locking.
-       */
-       if (ctx != NULL) {
-               return talloc_reference(mem_ctx, ctx);
-       }
-
-       ctx = talloc_p(mem_ctx, struct samdb_context);
-       if (ctx == NULL) {
-               errno = ENOMEM;
-               return NULL;
-       }
-
-       ctx->static_ptr = &ctx;
-
-       ctx->ldb = ldb_connect(lp_sam_url(), 0, NULL);
-       if (ctx->ldb == NULL) {
-               talloc_free(ctx);
-               return NULL;
-       }
-
-       talloc_set_destructor(ctx, samdb_destructor);
-       ldb_set_debug(ctx->ldb, samdb_debug, NULL);
-
-       return ctx;
+       return ldb_wrap_connect(mem_ctx, lp_sam_url(), 0, NULL);
 }
 
 /*
@@ -101,7 +41,7 @@ int samdb_search(void *ctx,
                 const char * const *attrs,
                 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        va_list ap;
        int count;
 
@@ -118,7 +58,7 @@ int samdb_search(void *ctx,
 int samdb_search_free(void *ctx,
                      TALLOC_CTX *mem_ctx, struct ldb_message **res)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_search_free(sam_ctx->ldb, res);
 }
@@ -132,7 +72,7 @@ const char *samdb_search_string_v(void *ctx,
                                  const char *attr_name,
                                  const char *format, va_list ap) _PRINTF_ATTRIBUTE(5,0)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        int count;
        const char * const attrs[2] = { attr_name, NULL };
        struct ldb_message **res = NULL;
@@ -178,7 +118,7 @@ int samdb_search_count(void *ctx,
                       const char *basedn,
                       const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
 {
-       struct samdb_context *samdb_ctx = ctx;
+       struct ldb_wrap *samdb_ctx = ctx;
        va_list ap;
        struct ldb_message **res;
        const char * const attrs[] = { NULL };
@@ -202,7 +142,7 @@ uint_t samdb_search_uint(void *ctx,
                         const char *attr_name,
                         const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
 {
-       struct samdb_context *samdb_ctx = ctx;
+       struct ldb_wrap *samdb_ctx = ctx;
        va_list ap;
        int count;
        struct ldb_message **res;
@@ -229,7 +169,7 @@ int64_t samdb_search_int64(void *ctx,
                           const char *attr_name,
                           const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
 {
-       struct samdb_context *samdb_ctx = ctx;
+       struct ldb_wrap *samdb_ctx = ctx;
        va_list ap;
        int count;
        struct ldb_message **res;
@@ -257,7 +197,7 @@ int samdb_search_string_multiple(void *ctx,
                                 const char *attr_name,
                                 const char *format, ...) _PRINTF_ATTRIBUTE(6,7)
 {
-       struct samdb_context *samdb_ctx = ctx;
+       struct ldb_wrap *samdb_ctx = ctx;
        va_list ap;
        int count, i;
        const char * const attrs[2] = { attr_name, NULL };
@@ -643,7 +583,7 @@ int samdb_copy_template(void *ctx, TALLOC_CTX *mem_ctx,
 static NTSTATUS _samdb_allocate_next_id(void *ctx, TALLOC_CTX *mem_ctx, const char *dn, 
                                        const char *attr, uint32_t *id)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        struct ldb_message msg;
        int ret;
        const char *str;
@@ -740,7 +680,7 @@ NTSTATUS samdb_allocate_next_id(void *ctx, TALLOC_CTX *mem_ctx, const char *dn,
 int samdb_msg_add_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                         const char *attr_name, const char *str)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        char *s = talloc_strdup(mem_ctx, str);
        char *a = talloc_strdup(mem_ctx, attr_name);
        if (s == NULL || a == NULL) {
@@ -756,7 +696,7 @@ int samdb_msg_add_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
 int samdb_msg_add_delete(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                         const char *attr_name)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        char *a = talloc_strdup(mem_ctx, attr_name);
        if (a == NULL) {
                return -1;
@@ -803,7 +743,7 @@ int samdb_msg_add_uint64(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
 int samdb_msg_add_hash(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                       const char *attr_name, struct samr_Password hash)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        struct ldb_val val;
        val.data = talloc(mem_ctx, 16);
        val.length = 16;
@@ -821,7 +761,7 @@ int samdb_msg_add_hash(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
 int samdb_msg_add_hashes(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                         const char *attr_name, struct samr_Password *hashes, uint_t count)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        struct ldb_val val;
        int i;
        val.data = talloc(mem_ctx, count*16);
@@ -851,7 +791,7 @@ int samdb_msg_add_acct_flags(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message
 int samdb_msg_add_logon_hours(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                              const char *attr_name, struct samr_LogonHours hours)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        struct ldb_val val;
        val.length = hours.units_per_week / 8;
        val.data = hours.bitmap;
@@ -865,7 +805,7 @@ int samdb_msg_add_logon_hours(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message
 int samdb_msg_set_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                         const char *attr_name, const char *str)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
        struct ldb_message_element *el;
 
        ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
@@ -895,7 +835,7 @@ int samdb_msg_set_ldaptime(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *m
 */
 int samdb_add(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
 
        ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_add(sam_ctx->ldb, msg);
@@ -906,7 +846,7 @@ int samdb_add(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
 */
 int samdb_delete(void *ctx, TALLOC_CTX *mem_ctx, const char *dn)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
 
        ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_delete(sam_ctx->ldb, dn);
@@ -917,7 +857,7 @@ int samdb_delete(void *ctx, TALLOC_CTX *mem_ctx, const char *dn)
 */
 int samdb_modify(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
 {
-       struct samdb_context *sam_ctx = ctx;
+       struct ldb_wrap *sam_ctx = ctx;
 
        ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_modify(sam_ctx->ldb, msg);
index 53993fce6067bea5c83454c8235cc77386b6576d..3391a2d3e1b6125ad9fc2a70564352f6d39146de 100644 (file)
@@ -41,11 +41,6 @@ void smbd_process_init(void)
 
        /* Start old-style secrets subsystem */
        
-       /* We must perform secrets_init(), as it sets up important
-        * seeding for the random number generator.
-        */
-       secrets_init();
-       
        talloc_destroy(mem_ctx);
 }