r23779: Change from v2 or later to v3 or later.
[ira/wip.git] / source3 / libsmb / samlogon_cache.c
index ceb7b7c35a44ef2eae5d7a303ace4dde60bdfff0..c58f3b212df525685ebfd3b53e57b20fe0855137 100644 (file)
@@ -8,7 +8,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,
@@ -59,15 +59,16 @@ BOOL netsamlogon_cache_shutdown(void)
 ***********************************************************************/
 void netsamlogon_clear_cached_user(TDB_CONTEXT *tdb, NET_USER_INFO_3 *user)
 {
-       fstring domain;
-       TDB_DATA key;
        BOOL got_tdb = False;
+       DOM_SID sid;
+       fstring key_str, sid_string;
 
        /* We may need to call this function from smbd which will not have
            winbindd_cache.tdb open.  Open the tdb if a NULL is passed. */
 
        if (!tdb) {
-               tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 5000,
+               tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 
+                                  WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
                                   TDB_DEFAULT, O_RDWR, 0600);
                if (!tdb) {
                        DEBUG(5, ("netsamlogon_clear_cached_user: failed to open cache\n"));
@@ -76,29 +77,24 @@ void netsamlogon_clear_cached_user(TDB_CONTEXT *tdb, NET_USER_INFO_3 *user)
                got_tdb = True;
        }
 
-       unistr2_to_ascii(domain, &user->uni_logon_dom, sizeof(domain) - 1);
+       sid_copy(&sid, &user->dom_sid.sid);
+       sid_append_rid(&sid, user->user_rid);
 
-       /* Clear U/DOMAIN/RID cache entry */
+       /* Clear U/SID cache entry */
 
-       asprintf(&key.dptr, "U/%s/%d", domain, user->user_rid);
-       key.dsize = strlen(key.dptr) - 1; /* keys are not NULL terminated */
+       fstr_sprintf(key_str, "U/%s", sid_to_string(sid_string, &sid));
 
-       DEBUG(10, ("netsamlogon_clear_cached_user: clearing %s\n", key.dptr));
+       DEBUG(10, ("netsamlogon_clear_cached_user: clearing %s\n", key_str));
 
-       tdb_delete(tdb, key);
+       tdb_delete(tdb, string_tdb_data(key_str));
 
-       SAFE_FREE(key.dptr);
+       /* Clear UG/SID cache entry */
 
-       /* Clear UG/DOMAIN/RID cache entry */
+       fstr_sprintf(key_str, "UG/%s", sid_to_string(sid_string, &sid));
 
-       asprintf(&key.dptr, "UG/%s/%d", domain, user->user_rid);
-       key.dsize = strlen(key.dptr) - 1; /* keys are not NULL terminated */
+       DEBUG(10, ("netsamlogon_clear_cached_user: clearing %s\n", key_str));
 
-       DEBUG(10, ("netsamlogon_clear_cached_user: clearing %s\n", key.dptr));
-
-       tdb_delete(tdb, key);
-
-       SAFE_FREE(key.dptr);
+       tdb_delete(tdb, string_tdb_data(key_str));
 
        if (got_tdb)
                tdb_close(tdb);
@@ -109,7 +105,7 @@ void netsamlogon_clear_cached_user(TDB_CONTEXT *tdb, NET_USER_INFO_3 *user)
  username should be in UTF-8 format
 ***********************************************************************/
 
-BOOL netsamlogon_cache_store(TALLOC_CTX *mem_ctx, const char * username, NET_USER_INFO_3 *user)
+BOOL netsamlogon_cache_store( const char *username, NET_USER_INFO_3 *user )
 {
        TDB_DATA        data;
         fstring        keystr;
@@ -117,6 +113,7 @@ BOOL netsamlogon_cache_store(TALLOC_CTX *mem_ctx, const char * username, NET_USE
        BOOL            result = False;
        DOM_SID         user_sid;
        time_t          t = time(NULL);
+       TALLOC_CTX      *mem_ctx;
        
 
        if (!netsamlogon_cache_init()) {
@@ -142,21 +139,31 @@ BOOL netsamlogon_cache_store(TALLOC_CTX *mem_ctx, const char * username, NET_USE
                
        /* Prepare data */
        
+       if ( !(mem_ctx = TALLOC_P( NULL, int )) ) {
+               DEBUG(0,("netsamlogon_cache_store: talloc() failed!\n"));
+               return False;
+       }
+
        prs_init( &ps, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
        
-       if ( !prs_uint32( "timestamp", &ps, 0, (uint32*)&t ) )
-               return False;
+       {
+               uint32 ts = (uint32)t;
+               if ( !prs_uint32( "timestamp", &ps, 0, &ts ) )
+                       return False;
+       }
        
        if ( net_io_user_info3("", user, &ps, 0, 3, 0) ) 
        {
                data.dsize = prs_offset( &ps );
-               data.dptr = prs_data_p( &ps );
+               data.dptr = (uint8 *)prs_data_p( &ps );
 
                if (tdb_store_bystring(netsamlogon_tdb, keystr, data, TDB_REPLACE) != -1)
                        result = True;
                
                prs_mem_free( &ps );
        }
+
+       TALLOC_FREE( mem_ctx );
                
        return result;
 }
@@ -169,38 +176,39 @@ BOOL netsamlogon_cache_store(TALLOC_CTX *mem_ctx, const char * username, NET_USE
 NET_USER_INFO_3* netsamlogon_cache_get( TALLOC_CTX *mem_ctx, const DOM_SID *user_sid)
 {
        NET_USER_INFO_3 *user = NULL;
-       TDB_DATA        data, key;
+       TDB_DATA        data;
        prs_struct      ps;
         fstring        keystr;
        uint32          t;
        
        if (!netsamlogon_cache_init()) {
-               DEBUG(0,("netsamlogon_cache_store: cannot open %s for write!\n", NETSAMLOGON_TDB));
+               DEBUG(0,("netsamlogon_cache_get: cannot open %s for write!\n", NETSAMLOGON_TDB));
                return False;
        }
 
        /* Prepare key as DOMAIN-SID/USER-RID string */
        slprintf(keystr, sizeof(keystr), "%s", sid_string_static(user_sid));
        DEBUG(10,("netsamlogon_cache_get: SID [%s]\n", keystr));
-       key.dptr = keystr;
-       key.dsize = strlen(keystr)+1;
-       data = tdb_fetch( netsamlogon_tdb, key );
+       data = tdb_fetch_bystring( netsamlogon_tdb, keystr );
        
        if ( data.dptr ) {
-               
-               if ( (user = SMB_MALLOC_P(NET_USER_INFO_3)) == NULL )
+
+               user = TALLOC_ZERO_P(mem_ctx, NET_USER_INFO_3);
+               if (user == NULL) {
                        return NULL;
-                       
+               }
+
                prs_init( &ps, 0, mem_ctx, UNMARSHALL );
-               prs_give_memory( &ps, data.dptr, data.dsize, True );
+               prs_give_memory( &ps, (char *)data.dptr, data.dsize, True );
                
                if ( !prs_uint32( "timestamp", &ps, 0, &t ) ) {
                        prs_mem_free( &ps );
+                       TALLOC_FREE(user);
                        return False;
                }
                
                if ( !net_io_user_info3("", user, &ps, 0, 3, 0) ) {
-                       SAFE_FREE( user );
+                       TALLOC_FREE( user );
                }
                        
                prs_mem_free( &ps );
@@ -219,7 +227,7 @@ NET_USER_INFO_3* netsamlogon_cache_get( TALLOC_CTX *mem_ctx, const DOM_SID *user
                if ( (time_diff < 0 ) || (time_diff > lp_winbind_cache_time()) ) {
                        DEBUG(10,("netsamlogon_cache_get: cache entry expired \n"));
                        tdb_delete( netsamlogon_tdb, key );
-                       SAFE_FREE( user );
+                       TALLOC_FREE( user );
                }
 #endif
        }
@@ -241,7 +249,6 @@ BOOL netsamlogon_cache_have(const DOM_SID *user_sid)
        result = (user != NULL);
 
        talloc_destroy(mem_ctx);
-       SAFE_FREE(user);
 
        return result;
 }