WHATSNEW: add SmartCard/PKINIT improvements
[vlendec/samba-autobuild/.git] / lib / tdb_wrap / tdb_wrap.c
index 0994b1b79cab93d54f37c6b5ee3f45fd62af6c91..864656f204768d149d4c16e76c2cc9ae8af13078 100644 (file)
@@ -1,63 +1,36 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    TDB wrap functions
 
    Copyright (C) Andrew Tridgell 2004
    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
-   
+
    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 3 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, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
+#include "replace.h"
 #include "lib/util/dlinklist.h"
-#include "lib/tdb_wrap/tdb_wrap.h"
-#include "lib/param/param.h"
-
-/* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
-#if BUILD_TDB2
-static void tdb_wrap_log(struct tdb_context *tdb,
-                        enum tdb_log_level level,
-                        enum TDB_ERROR ecode,
-                        const char *message,
-                        void *unused)
-{
-       int dl;
-       const char *name = tdb_name(tdb);
+#include "lib/util/debug.h"
+#include "tdb_wrap.h"
 
-       switch (level) {
-       case TDB_LOG_USE_ERROR:
-       case TDB_LOG_ERROR:
-               dl = 0;
-               break;
-       case TDB_LOG_WARNING:
-               dl = 2;
-               break;
-       default:
-               dl = 0;
-       }
-
-       DEBUG(dl, ("tdb(%s):%s: %s", name ? name : "unnamed",
-                  tdb_errorstr(ecode), message));
-}
-#else
 /*
  Log tdb messages via DEBUG().
 */
-static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
+static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
                         const char *format, ...) PRINTF_ATTRIBUTE(3,4);
 
-static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
+static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
                         const char *format, ...)
 {
        va_list ap;
@@ -80,7 +53,7 @@ static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
                break;
        default:
                debuglevel = 0;
-       }               
+       }
 
        va_start(ap, format);
        ret = vasprintf(&ptr, format, ap);
@@ -92,7 +65,6 @@ static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
                free(ptr);
        }
 }
-#endif
 
 struct tdb_wrap_private {
        struct tdb_context *tdb;
@@ -108,45 +80,28 @@ static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
        tdb_close(w->tdb);
        DLIST_REMOVE(tdb_list, w);
        return 0;
-}                               
+}
 
 static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
                                                      const char *name,
                                                      int hash_size,
                                                      int tdb_flags,
                                                      int open_flags,
-                                                     mode_t mode,
-                                                     struct loadparm_context *lp_ctx)
+                                                     mode_t mode)
 {
        struct tdb_wrap_private *result;
+       struct tdb_logging_context lctx = { .log_fn = tdb_wrap_log };
 
-       result = talloc(mem_ctx, struct tdb_wrap_private);
+       result = talloc_pooled_object(mem_ctx, struct tdb_wrap_private,
+                                     1, strlen(name)+1);
        if (result == NULL) {
                return NULL;
        }
+       /* Doesn't fail, see talloc_pooled_object */
        result->name = talloc_strdup(result, name);
-       if (result->name == NULL) {
-               goto fail;
-       }
-
-       if (!lpcfg_use_mmap(lp_ctx)) {
-               tdb_flags |= TDB_NOMMAP;
-       }
-
-       if ((hash_size == 0) && (name != NULL)) {
-               const char *base;
-               base = strrchr_m(name, '/');
-
-               if (base != NULL) {
-                       base += 1;
-               } else {
-                       base = name;
-               }
-               hash_size = lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
-       }
 
-       result->tdb = tdb_open_compat(name, hash_size, tdb_flags,
-                                     open_flags, mode, tdb_wrap_log, NULL);
+       result->tdb = tdb_open_ex(name, hash_size, tdb_flags,
+                                 open_flags, mode, &lctx, NULL);
        if (result->tdb == NULL) {
                goto fail;
        }
@@ -165,12 +120,16 @@ fail:
  */
 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 loadparm_context *lp_ctx)
+                              int open_flags, mode_t mode)
 {
        struct tdb_wrap *result;
        struct tdb_wrap_private *w;
 
+       if (name == NULL) {
+               errno = EINVAL;
+               return NULL;
+       }
+
        result = talloc(mem_ctx, struct tdb_wrap);
        if (result == NULL) {
                return NULL;
@@ -183,8 +142,15 @@ struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
        }
 
        if (w == NULL) {
+
+               if (tdb_flags & TDB_MUTEX_LOCKING) {
+                       if (!tdb_runtime_check_for_robust_mutexes()) {
+                               tdb_flags &= ~TDB_MUTEX_LOCKING;
+                       }
+               }
+
                w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
-                                         open_flags, mode, lp_ctx);
+                                         open_flags, mode);
        } else {
                /*
                 * Correctly use talloc_reference: The tdb will be
@@ -209,4 +175,3 @@ fail:
        TALLOC_FREE(result);
        return NULL;
 }
-