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"
2012-09-03 13:13:16 +04:00
# include "dbwrap/dbwrap_rbt.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"
2012-09-03 13:13:16 +04:00
# include "util_tdb.h"
2007-05-08 17:44:36 +04:00
2012-09-03 13:13:16 +04:00
struct connections_forall_state {
struct db_context * session_by_pid ;
2010-03-01 16:28:22 +03:00
int ( * fn ) ( const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ;
void * private_data ;
2012-09-03 13:13:16 +04:00
int count ;
} ;
struct connections_forall_session {
uid_t uid ;
gid_t gid ;
2013-11-10 14:56:06 +04:00
fstring machine ;
fstring addr ;
2010-03-01 16:28:22 +03:00
} ;
2012-09-03 13:13:16 +04:00
static int collect_sessions_fn ( struct smbXsrv_session_global0 * global ,
void * connections_forall_state )
2010-03-01 16:28:22 +03:00
{
2012-09-03 13:13:16 +04:00
NTSTATUS status ;
struct connections_forall_state * state =
( struct connections_forall_state * ) connections_forall_state ;
uint32_t id = global - > session_global_id ;
struct connections_forall_session sess ;
2013-07-05 15:19:59 +04:00
if ( global - > auth_session_info = = NULL ) {
sess . uid = - 1 ;
sess . gid = - 1 ;
} else {
sess . uid = global - > auth_session_info - > unix_token - > uid ;
sess . gid = global - > auth_session_info - > unix_token - > gid ;
}
2013-11-10 14:58:58 +04:00
fstrcpy ( sess . machine , global - > channels [ 0 ] . remote_name ) ;
fstrcpy ( sess . addr , global - > channels [ 0 ] . remote_address ) ;
2012-09-03 13:13:16 +04:00
status = dbwrap_store ( state - > session_by_pid ,
make_tdb_data ( ( void * ) & id , sizeof ( id ) ) ,
make_tdb_data ( ( void * ) & sess , sizeof ( sess ) ) ,
TDB_INSERT ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Failed to store record: %s \n " , nt_errstr ( status ) ) ) ;
}
return 0 ;
}
static int traverse_tcon_fn ( struct smbXsrv_tcon_global0 * global ,
void * connections_forall_state )
{
NTSTATUS status ;
struct connections_forall_state * state =
( struct connections_forall_state * ) connections_forall_state ;
struct connections_key key ;
struct connections_data data ;
2010-03-01 16:28:22 +03:00
2012-09-03 13:13:16 +04:00
uint32_t sess_id = global - > session_global_id ;
struct connections_forall_session sess = {
. uid = - 1 ,
. gid = - 1 ,
} ;
2011-08-25 02:01:44 +04:00
2012-09-03 13:13:16 +04:00
TDB_DATA val = tdb_null ;
2013-07-08 18:31:13 +04:00
/*
* Note : that share_name is defined as array without a pointer .
* that ' s why it ' s always a valid pointer here .
*/
if ( strlen ( global - > share_name ) = = 0 ) {
/*
* when a smbXsrv_tcon is created it ' s created
* with emtpy share_name first in order to allocate
* an id , before filling in the details .
*/
return 0 ;
}
2012-09-03 13:13:16 +04:00
status = dbwrap_fetch ( state - > session_by_pid , state ,
make_tdb_data ( ( void * ) & sess_id , sizeof ( sess_id ) ) ,
& val ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
memcpy ( ( uint8_t * ) & sess , val . dptr , val . dsize ) ;
2010-03-01 16:28:22 +03:00
}
2012-09-03 13:13:16 +04:00
ZERO_STRUCT ( key ) ;
ZERO_STRUCT ( data ) ;
key . pid = data . pid = global - > server_id ;
key . cnum = data . cnum = global - > tcon_global_id ;
2013-11-10 14:57:37 +04:00
fstrcpy ( key . name , global - > share_name ) ;
fstrcpy ( data . servicename , global - > share_name ) ;
2012-09-03 13:13:16 +04:00
data . uid = sess . uid ;
data . gid = sess . gid ;
2013-11-10 14:57:37 +04:00
fstrcpy ( data . addr , sess . addr ) ;
fstrcpy ( data . machine , sess . machine ) ;
2012-09-03 13:13:16 +04:00
data . start = nt_time_to_unix ( global - > creation_time ) ;
state - > count + + ;
return state - > fn ( & key , & data , state - > private_data ) ;
2010-03-01 16:28:22 +03:00
}
int connections_forall_read ( int ( * fn ) ( const struct connections_key * key ,
const struct connections_data * data ,
void * private_data ) ,
void * private_data )
{
2012-09-03 13:13:16 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
struct connections_forall_state * state =
talloc_zero ( talloc_tos ( ) , struct connections_forall_state ) ;
2011-08-25 02:01:44 +04:00
NTSTATUS status ;
2012-09-03 13:13:16 +04:00
int ret = - 1 ;
2010-03-01 16:28:22 +03:00
2012-09-03 13:13:16 +04:00
state - > session_by_pid = db_open_rbt ( state ) ;
state - > fn = fn ;
state - > private_data = private_data ;
status = smbXsrv_session_global_traverse ( collect_sessions_fn , state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Failed to traverse sessions: %s \n " ,
nt_errstr ( status ) ) ) ;
goto done ;
2010-03-01 16:28:22 +03:00
}
2012-09-03 13:13:16 +04:00
status = smbXsrv_tcon_global_traverse ( traverse_tcon_fn , state ) ;
2011-08-25 02:01:44 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2012-09-03 13:13:16 +04:00
DEBUG ( 0 , ( " Failed to traverse tree connects: %s \n " ,
nt_errstr ( status ) ) ) ;
goto done ;
2011-08-25 02:01:44 +04:00
}
2012-09-03 13:13:16 +04:00
ret = state - > count ;
done :
talloc_free ( frame ) ;
return ret ;
2010-03-01 16:28:22 +03:00
}