#include "includes.h"
#define NETSAMLOGON_TDB "netsamlogon_cache.tdb"
static TDB_CONTEXT *netsamlogon_tdb = NULL;
BOOL netsamlogon_cache_init(void)
{
if (!netsamlogon_tdb) {
netsamlogon_tdb = tdb_open_log(lock_path(NETSAMLOGON_TDB), 0,
TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
}
return (netsamlogon_tdb != NULL);
}
BOOL netsamlogon_cache_shutdown(void)
{
if(netsamlogon_tdb)
return (tdb_close(netsamlogon_tdb) == 0);
return True;
}
void netsamlogon_clear_cached_user(TDB_CONTEXT *tdb, NET_USER_INFO_3 *user)
{
BOOL got_tdb = False;
DOM_SID sid;
fstring key_str, sid_string;
if (!tdb) {
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"));
return;
}
got_tdb = True;
}
sid_copy(&sid, &user->dom_sid.sid);
sid_append_rid(&sid, user->user_rid);
fstr_sprintf(key_str, "U/%s", sid_to_string(sid_string, &sid));
DEBUG(10, ("netsamlogon_clear_cached_user: clearing %s\n", key_str));
tdb_delete(tdb, string_tdb_data(key_str));
fstr_sprintf(key_str, "UG/%s", sid_to_string(sid_string, &sid));
DEBUG(10, ("netsamlogon_clear_cached_user: clearing %s\n", key_str));
tdb_delete(tdb, string_tdb_data(key_str));
if (got_tdb)
tdb_close(tdb);
}
BOOL netsamlogon_cache_store( const char *username, NET_USER_INFO_3 *user )
{
TDB_DATA data;
fstring keystr;
prs_struct ps;
BOOL result = False;
DOM_SID user_sid;
time_t t = time(NULL);
TALLOC_CTX *mem_ctx;
if (!netsamlogon_cache_init()) {
DEBUG(0,("netsamlogon_cache_store: cannot open %s for write!\n", NETSAMLOGON_TDB));
return False;
}
sid_copy( &user_sid, &user->dom_sid.sid );
sid_append_rid( &user_sid, user->user_rid );
slprintf(keystr, sizeof(keystr), "%s", sid_string_static(&user_sid));
DEBUG(10,("netsamlogon_cache_store: SID [%s]\n", keystr));
if ( !user->uni_user_name.buffer ) {
init_unistr2( &user->uni_user_name, username, UNI_STR_TERMINATE );
init_uni_hdr( &user->hdr_user_name, &user->uni_user_name );
}
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);
{
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 );
if (tdb_store_bystring(netsamlogon_tdb, keystr, data, TDB_REPLACE) != -1)
result = True;
prs_mem_free( &ps );
}
TALLOC_FREE( mem_ctx );
return result;
}
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;
prs_struct ps;
fstring keystr;
uint32 t;
if (!netsamlogon_cache_init()) {
DEBUG(0,("netsamlogon_cache_get: cannot open %s for write!\n", NETSAMLOGON_TDB));
return False;
}
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 );
if ( data.dptr ) {
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 );
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) ) {
TALLOC_FREE( user );
}
prs_mem_free( &ps );
#if 0
{
time_t now = time(NULL);
uint32 time_diff;
time_diff = now - t;
if ( (time_diff < 0 ) || (time_diff > lp_winbind_cache_time()) ) {
DEBUG(10,("netsamlogon_cache_get: cache entry expired \n"));
tdb_delete( netsamlogon_tdb, key );
TALLOC_FREE( user );
}
#endif
}
return user;
}
BOOL netsamlogon_cache_have(const DOM_SID *user_sid)
{
TALLOC_CTX *mem_ctx = talloc_init("netsamlogon_cache_have");
NET_USER_INFO_3 *user = NULL;
BOOL result;
if (!mem_ctx)
return False;
user = netsamlogon_cache_get(mem_ctx, user_sid);
result = (user != NULL);
talloc_destroy(mem_ctx);
return result;
}