auth: Make sure that creds_out is initialized with NULL.
[metze/samba/wip.git] / libcli / auth / schannel_state_tdb.c
index 0e4ab06515929b5edd94935b677850c5a7238b19..d8d5f84cb3b848c80928fd1a6ba345cb7ccd9093 100644 (file)
 
 #include "includes.h"
 #include "system/filesys.h"
-#include <tdb.h>
+#include "../lib/tdb/include/tdb.h"
 #include "../lib/util/util_tdb.h"
-#include "../libcli/auth/libcli_auth.h"
-#include "../libcli/auth/schannel_state.h"
+#include "../lib/param/param.h"
+#include "../libcli/auth/schannel.h"
 #include "../librpc/gen_ndr/ndr_schannel.h"
-#if _SAMBA_BUILD_ == 4
-#include "tdb_wrap.h"
-#endif
+#include "lib/tdb_wrap/tdb_wrap.h"
 
 #define SECRETS_SCHANNEL_STATE "SECRETS/SCHANNEL"
 
 *******************************************************************************/
 
 struct tdb_wrap *open_schannel_session_store(TALLOC_CTX *mem_ctx,
-                                            const char *private_dir)
+                                            struct loadparm_context *lp_ctx)
 {
-       TDB_DATA vers;
-       uint32_t ver;
        struct tdb_wrap *tdb_sc = NULL;
-       char *fname = talloc_asprintf(mem_ctx, "%s/schannel_store.tdb", private_dir);
+       char *fname = lpcfg_private_path(mem_ctx, lp_ctx, "schannel_store.tdb");
 
        if (!fname) {
                return NULL;
        }
 
-       tdb_sc = tdb_wrap_open(mem_ctx, fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
+       tdb_sc = tdb_wrap_open(mem_ctx, fname, 0, TDB_CLEAR_IF_FIRST|TDB_NOSYNC, O_RDWR|O_CREAT, 0600, lp_ctx);
 
        if (!tdb_sc) {
                DEBUG(0,("open_schannel_session_store: Failed to open %s - %s\n",
@@ -60,35 +56,6 @@ struct tdb_wrap *open_schannel_session_store(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
- again:
-       vers = tdb_fetch_bystring(tdb_sc->tdb, "SCHANNEL_STORE_VERSION");
-       if (vers.dptr == NULL) {
-               /* First opener, no version. */
-               SIVAL(&ver,0,SCHANNEL_STORE_VERSION_CURRENT);
-               vers.dptr = (uint8_t *)&ver;
-               vers.dsize = 4;
-               tdb_store_bystring(tdb_sc->tdb, "SCHANNEL_STORE_VERSION", vers, TDB_REPLACE);
-               vers.dptr = NULL;
-       } else if (vers.dsize == 4) {
-               ver = IVAL(vers.dptr,0);
-               if (ver == SCHANNEL_STORE_VERSION_2) {
-                       DEBUG(0,("open_schannel_session_store: wrong version number %d in %s\n",
-                               (int)ver, fname ));
-                       tdb_wipe_all(tdb_sc->tdb);
-                       goto again;
-               }
-               if (ver != SCHANNEL_STORE_VERSION_CURRENT) {
-                       DEBUG(0,("open_schannel_session_store: wrong version number %d in %s\n",
-                               (int)ver, fname ));
-                       TALLOC_FREE(tdb_sc);
-               }
-       } else {
-               TALLOC_FREE(tdb_sc);
-               DEBUG(0,("open_schannel_session_store: wrong version number size %d in %s\n",
-                       (int)vers.dsize, fname ));
-       }
-
-       SAFE_FREE(vers.dptr);
        TALLOC_FREE(fname);
 
        return tdb_sc;
@@ -184,7 +151,7 @@ NTSTATUS schannel_fetch_session_key_tdb(struct tdb_wrap *tdb_sc,
 
        value = tdb_fetch_bystring(tdb_sc->tdb, keystr);
        if (!value.dptr) {
-               DEBUG(0,("schannel_fetch_session_key_tdb: Failed to find entry with key %s\n",
+               DEBUG(10,("schannel_fetch_session_key_tdb: Failed to find entry with key %s\n",
                        keystr ));
                status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
                goto done;
@@ -235,7 +202,7 @@ NTSTATUS schannel_fetch_session_key_tdb(struct tdb_wrap *tdb_sc,
 *******************************************************************************/
 
 NTSTATUS schannel_get_creds_state(TALLOC_CTX *mem_ctx,
-                                 const char *db_priv_dir,
+                                 struct loadparm_context *lp_ctx,
                                  const char *computer_name,
                                  struct netlogon_creds_CredentialState **_creds)
 {
@@ -249,7 +216,7 @@ NTSTATUS schannel_get_creds_state(TALLOC_CTX *mem_ctx,
                return NT_STATUS_NO_MEMORY;
        }
 
-       tdb_sc = open_schannel_session_store(tmpctx, db_priv_dir);
+       tdb_sc = open_schannel_session_store(tmpctx, lp_ctx);
        if (!tdb_sc) {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -273,7 +240,7 @@ NTSTATUS schannel_get_creds_state(TALLOC_CTX *mem_ctx,
 *******************************************************************************/
 
 NTSTATUS schannel_save_creds_state(TALLOC_CTX *mem_ctx,
-                                  const char *db_priv_dir,
+                                  struct loadparm_context *lp_ctx,
                                   struct netlogon_creds_CredentialState *creds)
 {
        TALLOC_CTX *tmpctx;
@@ -285,7 +252,7 @@ NTSTATUS schannel_save_creds_state(TALLOC_CTX *mem_ctx,
                return NT_STATUS_NO_MEMORY;
        }
 
-       tdb_sc = open_schannel_session_store(tmpctx, db_priv_dir);
+       tdb_sc = open_schannel_session_store(tmpctx, lp_ctx);
        if (!tdb_sc) {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -307,7 +274,7 @@ NTSTATUS schannel_save_creds_state(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 NTSTATUS schannel_check_creds_state(TALLOC_CTX *mem_ctx,
-                                   const char *db_priv_dir,
+                                   struct loadparm_context *lp_ctx,
                                    const char *computer_name,
                                    struct netr_Authenticator *received_authenticator,
                                    struct netr_Authenticator *return_authenticator,
@@ -319,12 +286,16 @@ NTSTATUS schannel_check_creds_state(TALLOC_CTX *mem_ctx,
        NTSTATUS status;
        int ret;
 
+       if (creds_out != NULL) {
+               *creds_out = NULL;
+       }
+
        tmpctx = talloc_named(mem_ctx, 0, "schannel_check_creds_state");
        if (!tmpctx) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       tdb_sc = open_schannel_session_store(tmpctx, db_priv_dir);
+       tdb_sc = open_schannel_session_store(tmpctx, lp_ctx);
        if (!tdb_sc) {
                status = NT_STATUS_ACCESS_DENIED;
                goto done;