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