Prevent winbindd from segfaulting due to corrupted cache tdb.
[ira/wip.git] / source3 / winbindd / winbindd.c
index b695c2e95ee984924e98c647351b9174f74de045..dfad50bf964ba9a33f6ef37a6378494c7bcd369c 100644 (file)
@@ -121,7 +121,23 @@ static void flush_caches(void)
            otherwise cached access denied errors due to restrict anonymous
            hang around until the sequence number changes. */
 
-       wcache_invalidate_cache();
+       if (wcache_invalidate_cache() < 0) {
+               DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
+               /* Close the cache to be able to valdite the cache */
+               close_winbindd_cache();
+               /*
+                * Ensure all cache and idmap caches are consistent
+                * before we initialize the cache again.
+                */
+               if (winbindd_validate_cache() < 0) {
+                       DEBUG(0, ("corrupted tdb found, trying to restore backup\n"));
+               }
+
+               /* Initialize cache again. */
+               if (!initialize_winbindd_cache()) {
+                       exit(1);
+               }
+       }
 }
 
 /* Handle the signal by unlinking socket and exiting */
@@ -1157,20 +1173,6 @@ int main(int argc, char **argv, char **envp)
 
        pidfile_create("winbindd");
 
-       /* Ensure all cache and idmap caches are consistent
-          before we startup. */
-
-       if (winbindd_validate_cache()) {
-               /* We have a bad cache, but luckily we
-                  just deleted it. Restart ourselves */
-               int i;
-               /* Ensure we have no open low fd's. */
-               for (i = 3; i < 100; i++) {
-                       close(i);
-               }
-               return execve(argv[0], argv, envp);
-       }
-
 #if HAVE_SETPGID
        /*
         * If we're interactive we want to set our own process group for
@@ -1188,7 +1190,15 @@ int main(int argc, char **argv, char **envp)
                DEBUG(0, ("unable to initialize messaging system\n"));
                exit(1);
        }
-       
+
+       /*
+        * Ensure all cache and idmap caches are consistent
+        * before we startup.
+        */
+       if (winbindd_validate_cache() < 0) {
+               DEBUG(0, ("corrupted tdb found, trying to restore backup\n"));
+       }
+
        /* Initialize cache (ensure version is correct). */
        if (!initialize_winbindd_cache()) {
                exit(1);