2010-03-01 18:18:23 +03:00
/*
Unix SMB / CIFS implementation .
Low - level sessionid . tdb access functions
Copyright ( C ) Volker Lendecke 2010
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 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
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 , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2011-07-07 19:42:08 +04:00
# include "dbwrap/dbwrap.h"
2011-07-06 18:40:21 +04:00
# include "dbwrap/dbwrap_open.h"
2011-02-25 01:14:15 +03:00
# include "session.h"
2011-05-05 13:25:29 +04:00
# include "util_tdb.h"
2010-03-01 18:18:23 +03:00
static struct db_context * session_db_ctx ( void )
{
static struct db_context * session_db_ctx_ptr ;
if ( session_db_ctx_ptr ! = NULL ) {
return session_db_ctx_ptr ;
}
session_db_ctx_ptr = db_open ( NULL , lock_path ( " sessionid.tdb " ) , 0 ,
2010-09-27 16:46:07 +04:00
TDB_CLEAR_IF_FIRST | TDB_DEFAULT | TDB_INCOMPATIBLE_HASH ,
2012-01-06 20:19:54 +04:00
O_RDWR | O_CREAT , 0644 ,
DBWRAP_LOCK_ORDER_1 ) ;
2010-03-01 18:18:23 +03:00
return session_db_ctx_ptr ;
}
bool sessionid_init ( void )
{
if ( session_db_ctx ( ) = = NULL ) {
DEBUG ( 1 , ( " session_init: failed to open sessionid tdb \n " ) ) ;
return False ;
}
return True ;
}
struct db_record * sessionid_fetch_record ( TALLOC_CTX * mem_ctx , const char * key )
{
struct db_context * db ;
db = session_db_ctx ( ) ;
if ( db = = NULL ) {
return NULL ;
}
2011-08-25 02:08:36 +04:00
return dbwrap_fetch_locked ( db , mem_ctx , string_term_tdb_data ( key ) ) ;
2010-03-01 18:18:23 +03:00
}
struct sessionid_traverse_state {
int ( * fn ) ( struct db_record * rec , const char * key ,
struct sessionid * session , void * private_data ) ;
void * private_data ;
} ;
static int sessionid_traverse_fn ( struct db_record * rec , void * private_data )
{
2011-08-25 02:08:36 +04:00
TDB_DATA key ;
TDB_DATA value ;
2010-03-01 18:18:23 +03:00
struct sessionid_traverse_state * state =
( struct sessionid_traverse_state * ) private_data ;
struct sessionid session ;
2011-08-25 02:08:36 +04:00
key = dbwrap_record_get_key ( rec ) ;
value = dbwrap_record_get_value ( rec ) ;
if ( ( key . dptr [ key . dsize - 1 ] ! = ' \0 ' )
| | ( value . dsize ! = sizeof ( struct sessionid ) ) ) {
2010-03-01 18:18:23 +03:00
DEBUG ( 1 , ( " Found invalid record in sessionid.tdb \n " ) ) ;
return 0 ;
}
2011-08-25 02:08:36 +04:00
memcpy ( & session , value . dptr , sizeof ( session ) ) ;
2010-03-01 18:18:23 +03:00
2011-08-25 02:08:36 +04:00
return state - > fn ( rec , ( char * ) key . dptr , & session ,
2010-03-01 18:18:23 +03:00
state - > private_data ) ;
}
2011-08-17 12:20:46 +04:00
NTSTATUS sessionid_traverse ( int ( * fn ) ( struct db_record * rec , const char * key ,
struct sessionid * session ,
void * private_data ) ,
void * private_data )
2010-03-01 18:18:23 +03:00
{
struct db_context * db ;
struct sessionid_traverse_state state ;
2011-08-17 12:20:46 +04:00
NTSTATUS status ;
2010-03-01 18:18:23 +03:00
db = session_db_ctx ( ) ;
if ( db = = NULL ) {
2011-08-17 12:20:46 +04:00
return NT_STATUS_UNSUCCESSFUL ;
2010-03-01 18:18:23 +03:00
}
state . fn = fn ;
state . private_data = private_data ;
2011-08-17 12:20:46 +04:00
status = dbwrap_traverse ( db , sessionid_traverse_fn , & state , NULL ) ;
return status ;
2010-03-01 18:18:23 +03:00
}
struct sessionid_traverse_read_state {
int ( * fn ) ( const char * key , struct sessionid * session ,
void * private_data ) ;
void * private_data ;
} ;
static int sessionid_traverse_read_fn ( struct db_record * rec ,
void * private_data )
{
2011-08-25 02:08:36 +04:00
TDB_DATA key ;
TDB_DATA value ;
2010-03-01 18:18:23 +03:00
struct sessionid_traverse_read_state * state =
( struct sessionid_traverse_read_state * ) private_data ;
struct sessionid session ;
2011-08-25 02:08:36 +04:00
key = dbwrap_record_get_key ( rec ) ;
value = dbwrap_record_get_value ( rec ) ;
if ( ( key . dptr [ key . dsize - 1 ] ! = ' \0 ' )
| | ( value . dsize ! = sizeof ( struct sessionid ) ) ) {
2010-03-01 18:18:23 +03:00
DEBUG ( 1 , ( " Found invalid record in sessionid.tdb \n " ) ) ;
return 0 ;
}
2011-08-25 02:08:36 +04:00
memcpy ( & session , value . dptr , sizeof ( session ) ) ;
2010-03-01 18:18:23 +03:00
2011-08-25 02:08:36 +04:00
return state - > fn ( ( char * ) key . dptr , & session ,
2010-03-01 18:18:23 +03:00
state - > private_data ) ;
}
2011-08-17 12:33:58 +04:00
NTSTATUS sessionid_traverse_read ( int ( * fn ) ( const char * key ,
struct sessionid * session ,
void * private_data ) ,
void * private_data )
2010-03-01 18:18:23 +03:00
{
struct db_context * db ;
struct sessionid_traverse_read_state state ;
2011-08-17 12:33:58 +04:00
NTSTATUS status ;
2010-03-01 18:18:23 +03:00
db = session_db_ctx ( ) ;
if ( db = = NULL ) {
2011-08-17 12:33:58 +04:00
return NT_STATUS_UNSUCCESSFUL ;
2010-03-01 18:18:23 +03:00
}
state . fn = fn ;
state . private_data = private_data ;
2011-08-17 12:33:58 +04:00
status = dbwrap_traverse_read ( db , sessionid_traverse_read_fn , & state ,
NULL ) ;
return status ;
2010-03-01 18:18:23 +03:00
}