s3: let netsamlogon_cache_init() use tdb_check()
authorStefan Metzmacher <metze@samba.org>
Fri, 4 Dec 2009 15:34:08 +0000 (16:34 +0100)
committerStefan Metzmacher <metze@samba.org>
Mon, 7 Dec 2009 18:56:56 +0000 (19:56 +0100)
If the check fails we try to unlink the old file and
start with an empty cache.

metze

source3/libsmb/samlogon_cache.c

index c96f5dad8316fe8ec713aa2796f34e58408d8db5..12901826eee517b8b27992e7eb2989c3514725fd 100644 (file)
@@ -34,12 +34,50 @@ static TDB_CONTEXT *netsamlogon_tdb = NULL;
 
 bool netsamlogon_cache_init(void)
 {
-       if (!netsamlogon_tdb) {
-               netsamlogon_tdb = tdb_open_log(cache_path(NETSAMLOGON_TDB), 0,
-                                              TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
+       bool first_try = true;
+       const char *path = NULL;
+       int ret;
+       struct tdb_context *tdb;
+
+       if (netsamlogon_tdb) {
+               return true;
+       }
+
+       path = cache_path(NETSAMLOGON_TDB);
+again:
+       tdb = tdb_open_log(path, 0, TDB_DEFAULT,
+                          O_RDWR | O_CREAT, 0600);
+       if (tdb == NULL) {
+               DEBUG(0,("tdb_open_log('%s') - failed\n", path));
+               goto clear;
+       }
+
+       ret = tdb_check(tdb, NULL, NULL);
+       if (ret != 0) {
+               tdb_close(tdb);
+               DEBUG(0,("tdb_check('%s') - failed\n", path));
+               goto clear;
+       }
+
+       netsamlogon_tdb = tdb;
+       return true;
+
+clear:
+       if (!first_try) {
+               return false;
+       }
+       first_try = false;
+
+       DEBUG(0,("retry after CLEAR_IF_FIRST for '%s'\n", path));
+       tdb = tdb_open_log(path, 0, TDB_CLEAR_IF_FIRST,
+                          O_RDWR | O_CREAT, 0600);
+       if (tdb) {
+               tdb_close(tdb);
+               goto again;
        }
+       DEBUG(0,("tdb_open_log(%s) with CLEAR_IF_FIRST - failed\n", path));
 
-       return (netsamlogon_tdb != NULL);
+       return false;
 }