2007-05-08 13:44:36 +00:00
/*
Unix SMB / CIFS implementation .
Low - level connections . tdb access functions
Copyright ( C ) Volker Lendecke 2007
2009-08-07 12:09:21 +02:00
2007-05-08 13:44:36 +00:00
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
2007-07-09 19:25:36 +00:00
the Free Software Foundation ; either version 3 of the License , or
2007-05-08 13:44:36 +00:00
( at your option ) any later version .
2009-08-07 12:09:21 +02:00
2007-05-08 13:44:36 +00:00
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 .
2009-08-07 12:09:21 +02:00
2007-05-08 13:44:36 +00:00
You should have received a copy of the GNU General Public License
2007-07-10 00:52:41 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2007-05-08 13:44:36 +00:00
*/
# include "includes.h"
2010-07-08 18:00:07 +02:00
# include "smbd/globals.h"
2010-08-18 18:59:23 +02:00
# include "dbwrap.h"
2007-05-08 13:44:36 +00:00
2007-10-18 17:40:25 -07:00
static struct db_context * connections_db_ctx ( bool rw )
2007-05-08 13:44:36 +00:00
{
2007-05-28 11:38:42 +00:00
static struct db_context * db_ctx ;
2010-02-25 16:31:12 +01:00
int open_flags ;
2007-05-08 13:44:36 +00:00
2007-05-28 11:38:42 +00:00
if ( db_ctx ! = NULL ) {
return db_ctx ;
2007-05-08 13:44:36 +00:00
}
2010-02-25 16:31:12 +01:00
open_flags = rw ? ( O_RDWR | O_CREAT ) : O_RDONLY ;
2007-05-08 13:44:36 +00:00
2010-02-25 16:31:12 +01:00
db_ctx = db_open ( NULL , lock_path ( " connections.tdb " ) , 0 ,
2010-09-27 05:46:07 -07:00
TDB_CLEAR_IF_FIRST | TDB_INCOMPATIBLE_HASH | TDB_DEFAULT , open_flags , 0644 ) ;
2007-05-28 11:38:42 +00:00
return db_ctx ;
}
2010-02-24 15:38:06 +01:00
static struct db_record * connections_fetch_record ( TALLOC_CTX * mem_ctx ,
TDB_DATA key )
2007-05-28 11:38:42 +00:00
{
struct db_context * ctx = connections_db_ctx ( True ) ;
if ( ctx = = NULL ) {
return NULL ;
2007-05-08 13:44:36 +00:00
}
2007-05-28 11:38:42 +00:00
return ctx - > fetch_locked ( ctx , mem_ctx , key ) ;
}
struct db_record * connections_fetch_entry ( TALLOC_CTX * mem_ctx ,
connection_struct * conn ,
const char * name )
{
struct connections_key ckey ;
TDB_DATA key ;
ZERO_STRUCT ( ckey ) ;
2010-07-08 18:00:07 +02:00
ckey . pid = sconn_server_id ( conn - > sconn ) ;
2010-08-06 23:27:36 +02:00
ckey . cnum = conn - > cnum ;
2007-05-28 11:38:42 +00:00
strlcpy ( ckey . name , name , sizeof ( ckey . name ) ) ;
key . dsize = sizeof ( ckey ) ;
key . dptr = ( uint8 * ) & ckey ;
return connections_fetch_record ( mem_ctx , key ) ;
2007-05-08 13:44:36 +00:00
}
struct conn_traverse_state {
2007-05-28 11:38:42 +00:00
int ( * fn ) ( struct db_record * rec ,
2007-05-08 13:44:36 +00:00
const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ;
void * private_data ;
} ;
2007-05-28 11:38:42 +00:00
static int conn_traverse_fn ( struct db_record * rec , void * private_data )
2007-05-08 13:44:36 +00:00
{
struct conn_traverse_state * state =
( struct conn_traverse_state * ) private_data ;
2007-05-28 11:38:42 +00:00
if ( ( rec - > key . dsize ! = sizeof ( struct connections_key ) )
| | ( rec - > value . dsize ! = sizeof ( struct connections_data ) ) ) {
2007-05-08 13:44:36 +00:00
return 0 ;
}
2007-05-28 11:38:42 +00:00
return state - > fn ( rec , ( const struct connections_key * ) rec - > key . dptr ,
( const struct connections_data * ) rec - > value . dptr ,
state - > private_data ) ;
2007-05-08 13:44:36 +00:00
}
2007-05-28 11:38:42 +00:00
int connections_traverse ( int ( * fn ) ( struct db_record * rec ,
void * private_data ) ,
2007-05-08 13:44:36 +00:00
void * private_data )
{
2007-05-28 11:38:42 +00:00
struct db_context * ctx = connections_db_ctx ( False ) ;
2007-05-08 13:44:36 +00:00
2007-05-28 11:38:42 +00:00
if ( ctx = = NULL ) {
2007-05-08 13:44:36 +00:00
return - 1 ;
}
2007-05-28 11:38:42 +00:00
return ctx - > traverse ( ctx , fn , private_data ) ;
2007-05-08 13:44:36 +00:00
}
2007-05-28 11:38:42 +00:00
int connections_forall ( int ( * fn ) ( struct db_record * rec ,
2007-05-08 13:44:36 +00:00
const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ,
void * private_data )
{
2010-03-01 13:57:36 +01:00
struct db_context * ctx ;
2007-05-08 13:44:36 +00:00
struct conn_traverse_state state ;
2010-03-01 13:57:36 +01:00
ctx = connections_db_ctx ( true ) ;
if ( ctx = = NULL ) {
return - 1 ;
}
2007-05-08 13:44:36 +00:00
state . fn = fn ;
state . private_data = private_data ;
2010-03-01 13:57:36 +01:00
return ctx - > traverse ( ctx , conn_traverse_fn , ( void * ) & state ) ;
2007-05-08 13:44:36 +00:00
}
2010-03-01 14:28:22 +01:00
struct conn_traverse_read_state {
int ( * fn ) ( const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ;
void * private_data ;
} ;
static int connections_forall_read_fn ( struct db_record * rec ,
void * private_data )
{
struct conn_traverse_read_state * state =
( struct conn_traverse_read_state * ) private_data ;
if ( ( rec - > key . dsize ! = sizeof ( struct connections_key ) )
| | ( rec - > value . dsize ! = sizeof ( struct connections_data ) ) ) {
return 0 ;
}
return state - > fn ( ( const struct connections_key * ) rec - > key . dptr ,
( const struct connections_data * ) rec - > value . dptr ,
state - > private_data ) ;
}
int connections_forall_read ( int ( * fn ) ( const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ,
void * private_data )
{
struct db_context * ctx ;
struct conn_traverse_read_state state ;
ctx = connections_db_ctx ( false ) ;
if ( ctx = = NULL ) {
return - 1 ;
}
state . fn = fn ;
state . private_data = private_data ;
return ctx - > traverse_read ( ctx , connections_forall_read_fn ,
( void * ) & state ) ;
}
2007-10-18 17:40:25 -07:00
bool connections_init ( bool rw )
2007-05-08 13:44:36 +00:00
{
2007-05-28 11:38:42 +00:00
return ( connections_db_ctx ( rw ) ! = NULL ) ;
2007-05-08 13:44:36 +00:00
}