Replace sid_string_static by sid_string_dbg in DEBUGs
[amitay/samba.git] / source3 / libsmb / trustdom_cache.c
index 8c5fb4d907135e2cc9ef5460deade29e9cece6cb..498a0221f2a372523e466a91daca8276c1fcdc80 100644 (file)
@@ -7,7 +7,7 @@
    
    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
+   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,
@@ -16,8 +16,7 @@
    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.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -48,7 +47,7 @@
  *         false if cache init failed
  **/
  
-BOOL trustdom_cache_enable(void)
+bool trustdom_cache_enable(void)
 {
        /* Init trustdom cache by calling gencache initialisation */
        if (!gencache_init()) {
@@ -68,7 +67,7 @@ BOOL trustdom_cache_enable(void)
  *         false if it failed
  **/
  
-BOOL trustdom_cache_shutdown(void)
+bool trustdom_cache_shutdown(void)
 {
        /* Close trustdom cache by calling gencache shutdown */
        if (!gencache_shutdown()) {
@@ -91,7 +90,7 @@ BOOL trustdom_cache_shutdown(void)
 static char* trustdom_cache_key(const char* name)
 {
        char* keystr = NULL;
-       asprintf(&keystr, TDOMKEY_FMT, strupper_static(name));
+       asprintf_strupper_m(&keystr, TDOMKEY_FMT, name);
        
        return keystr;
 }
@@ -99,7 +98,7 @@ static char* trustdom_cache_key(const char* name)
 
 /**
  * Store trusted domain in gencache as the domain name (key)
- * and ip address of domain controller (value)
+ * and trusted domain's SID (value)
  *
  * @param name trusted domain name
  * @param alt_name alternative trusted domain name (used in ADS domains)
@@ -109,12 +108,12 @@ static char* trustdom_cache_key(const char* name)
  *         false if store attempt failed
  **/
  
-BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
+bool trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
                           time_t timeout)
 {
        char *key, *alt_key;
        fstring sid_string;
-       BOOL ret;
+       bool ret;
 
        /*
         * we use gecache call to avoid annoying debug messages
@@ -124,7 +123,7 @@ BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
                return False;
 
        DEBUG(5, ("trustdom_store: storing SID %s of domain %s\n",
-                 sid_string_static(sid), name));
+                 sid_string_dbg(sid), name));
 
        key = trustdom_cache_key(name);
        alt_key = alt_name ? trustdom_cache_key(alt_name) : NULL;
@@ -152,7 +151,7 @@ BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
 
 
 /**
- * Fetch trusted domain's dc from the gencache.
+ * Fetch trusted domain's SID from the gencache.
  * This routine can also be used to check whether given
  * domain is currently trusted one.
  *
@@ -162,7 +161,7 @@ BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
  *         false if has expired/doesn't exist
  **/
  
-BOOL trustdom_cache_fetch(const char* name, DOM_SID* sid)
+bool trustdom_cache_fetch(const char* name, DOM_SID* sid)
 {
        char *key = NULL, *value = NULL;
        time_t timeout;
@@ -183,14 +182,13 @@ BOOL trustdom_cache_fetch(const char* name, DOM_SID* sid)
        if (!gencache_get(key, &value, &timeout)) {
                DEBUG(5, ("no entry for trusted domain %s found.\n", name));
                SAFE_FREE(key);
-               SAFE_FREE(value);
                return False;
        } else {
                SAFE_FREE(key);
                DEBUG(5, ("trusted domain %s found (%s)\n", name, value));
        }
 
-       /* convert ip string representation into in_addr structure */
+       /* convert sid string representation into DOM_SID structure */
        if(! string_to_sid(sid, value)) {
                sid = NULL;
                SAFE_FREE(value);
@@ -232,7 +230,7 @@ uint32 trustdom_cache_fetch_timestamp( void )
  store the timestamp from the last update 
 *******************************************************************/
 
-BOOL trustdom_cache_store_timestamp( uint32 t, time_t timeout )
+bool trustdom_cache_store_timestamp( uint32 t, time_t timeout )
 {
        fstring value;
 
@@ -251,24 +249,6 @@ BOOL trustdom_cache_store_timestamp( uint32 t, time_t timeout )
 }
 
 
-/*******************************************************************
- lock the timestamp entry in the trustdom_cache
-*******************************************************************/
-
-BOOL trustdom_cache_lock_timestamp( void )
-{
-       return gencache_lock_entry( TDOMTSKEY ) != -1;
-}
-
-/*******************************************************************
- unlock the timestamp entry in the trustdom_cache
-*******************************************************************/
-
-void trustdom_cache_unlock_timestamp( void )
-{
-       gencache_unlock_entry( TDOMTSKEY );
-}
-
 /**
  * Delete single trustdom entry. Look at the
  * gencache_iterate definition.
@@ -315,8 +295,7 @@ void update_trustdom_cache( void )
        time_t now = time(NULL);
        int i;
        
-       /* get the timestamp.  We have to initialise it if the last timestamp == 0 */
-       
+       /* get the timestamp.  We have to initialise it if the last timestamp == 0 */   
        if ( (last_check = trustdom_cache_fetch_timestamp()) == 0 ) 
                trustdom_cache_store_timestamp(0, now+TRUSTDOM_UPDATE_INTERVAL);
 
@@ -326,11 +305,12 @@ void update_trustdom_cache( void )
                DEBUG(10,("update_trustdom_cache: not time to update trustdom_cache yet\n"));
                return;
        }
+
+       /* note that we don't lock the timestamp. This prevents this
+          smbd from blocking all other smbd daemons while we
+          enumerate the trusted domains */
+       trustdom_cache_store_timestamp(now, now+TRUSTDOM_UPDATE_INTERVAL);
                
-       /* lock the timestamp */
-       if ( !trustdom_cache_lock_timestamp() )
-               return;
-       
        if ( !(mem_ctx = talloc_init("update_trustdom_cache")) ) {
                DEBUG(0,("update_trustdom_cache: talloc_init() failed!\n"));
                goto done;
@@ -339,20 +319,19 @@ void update_trustdom_cache( void )
        /* get the domains and store them */
        
        if ( enumerate_domain_trusts(mem_ctx, lp_workgroup(), &domain_names, 
-               &num_domains, &dom_sids) ) 
-       {
+               &num_domains, &dom_sids)) {
                for ( i=0; i<num_domains; i++ ) {
                        trustdom_cache_store( domain_names[i], NULL, &dom_sids[i], 
                                now+TRUSTDOM_UPDATE_INTERVAL);
-               }
-               
-               trustdom_cache_store_timestamp( now, now+TRUSTDOM_UPDATE_INTERVAL );
+               }               
+       } else {
+               /* we failed to fetch the list of trusted domains - restore the old
+                  timestamp */
+               trustdom_cache_store_timestamp(last_check, 
+                                              last_check+TRUSTDOM_UPDATE_INTERVAL);
        }
 
 done:  
-       /* unlock and we're done */
-       trustdom_cache_unlock_timestamp();
-       
        talloc_destroy( mem_ctx );
        
        return;