From 12ea0fd34cec2d7b6d8c8374dfd95728112585b3 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 16 Oct 2004 13:47:00 +0000 Subject: r3005: added talloc wrappers around tdb_open() and ldb_connect(), so that the 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. (This used to be commit 04e1171996612ddb15f84134cadded68f0d173b2) --- source4/include/db_wrap.h | 38 ++++++ source4/include/includes.h | 1 + source4/intl/lang_tdb.c | 4 +- source4/lib/basic.mk | 2 +- source4/lib/db_wrap.c | 170 +++++++++++++++++++++++++++ source4/lib/gencache.c | 27 ++--- source4/lib/messages.c | 42 +++---- source4/lib/tdb/config.mk | 6 +- source4/lib/tdb_helper.c | 42 ------- source4/libcli/unexpected.c | 26 ++-- source4/passdb/secrets.c | 12 +- source4/rpc_server/netlogon/schannel_state.c | 48 ++++---- source4/rpc_server/samr/samdb.c | 96 +++------------ source4/smbd/rewrite.c | 5 - 14 files changed, 312 insertions(+), 207 deletions(-) create mode 100644 source4/include/db_wrap.h create mode 100644 source4/lib/db_wrap.c (limited to 'source4') diff --git a/source4/include/db_wrap.h b/source4/include/db_wrap.h new file mode 100644 index 0000000000..50e3af68bd --- /dev/null +++ b/source4/include/db_wrap.h @@ -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; +}; + diff --git a/source4/include/includes.h b/source4/include/includes.h index 86ddc81e57..85e4299efd 100644 --- a/source4/include/includes.h +++ b/source4/include/includes.h @@ -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" diff --git a/source4/intl/lang_tdb.c b/source4/intl/lang_tdb.c index 64f020f26a..05e3a7ba63 100644 --- a/source4/intl/lang_tdb.c +++ b/source4/intl/lang_tdb.c @@ -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); diff --git a/source4/lib/basic.mk b/source4/lib/basic.mk index addef77a20..93309ce68b 100644 --- a/source4/lib/basic.mk +++ b/source4/lib/basic.mk @@ -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/source4/lib/db_wrap.c b/source4/lib/db_wrap.c new file mode 100644 index 0000000000..251741c5b0 --- /dev/null +++ b/source4/lib/db_wrap.c @@ -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; +} diff --git a/source4/lib/gencache.c b/source4/lib/gencache.c index 1a66d8dc3d..a176dc5ae8 100644 --- a/source4/lib/gencache.c +++ b/source4/lib/gencache.c @@ -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); } diff --git a/source4/lib/messages.c b/source4/lib/messages.c index b4de7c0145..64dc2f510a 100644 --- a/source4/lib/messages.c +++ b/source4/lib/messages.c @@ -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); diff --git a/source4/lib/tdb/config.mk b/source4/lib/tdb/config.mk index fdde6e7b4f..dba0d3e1d4 100644 --- a/source4/lib/tdb/config.mk +++ b/source4/lib/tdb/config.mk @@ -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 ################################################ diff --git a/source4/lib/tdb_helper.c b/source4/lib/tdb_helper.c index 2698e3cc71..e29a8a7211 100644 --- a/source4/lib/tdb_helper.c +++ b/source4/lib/tdb_helper.c @@ -25,45 +25,3 @@ 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; -} diff --git a/source4/libcli/unexpected.c b/source4/libcli/unexpected.c index 22795e6c4c..e109c9d5bf 100644 --- a/source4/libcli/unexpected.c +++ b/source4/libcli/unexpected.c @@ -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; } diff --git a/source4/passdb/secrets.c b/source4/passdb/secrets.c index 21938db154..60b197de93 100644 --- a/source4/passdb/secrets.c +++ b/source4/passdb/secrets.c @@ -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 )); } diff --git a/source4/rpc_server/netlogon/schannel_state.c b/source4/rpc_server/netlogon/schannel_state.c index 88eabc6d37..1153e4043d 100644 --- a/source4/rpc_server/netlogon/schannel_state.c +++ b/source4/rpc_server/netlogon/schannel_state.c @@ -28,22 +28,22 @@ /* 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; } diff --git a/source4/rpc_server/samr/samdb.c b/source4/rpc_server/samr/samdb.c index 330741e29d..3fb8fcef1e 100644 --- a/source4/rpc_server/samr/samdb.c +++ b/source4/rpc_server/samr/samdb.c @@ -22,73 +22,13 @@ #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); diff --git a/source4/smbd/rewrite.c b/source4/smbd/rewrite.c index 53993fce60..3391a2d3e1 100644 --- a/source4/smbd/rewrite.c +++ b/source4/smbd/rewrite.c @@ -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); } -- cgit