2019-04-11 14:03:30 +03:00
/*
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"
2019-04-11 14:03:30 +03:00
# include "conn_tdb.h"
2012-09-03 13:13:16 +04:00
# include "util_tdb.h"
2020-08-07 21:17:34 +03:00
# include "lib/util/string_wrappers.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 ;
2021-10-26 14:56:54 +03:00
int ( * fn ) ( const struct connections_data * data ,
2010-03-01 16:28:22 +03:00
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 ;
2015-11-09 19:26:51 +03:00
uint16_t cipher ;
2015-11-30 13:14:34 +03:00
uint16_t dialect ;
2021-05-07 00:55:49 +03:00
uint16_t signing ;
2015-11-30 13:20:43 +03:00
uint8_t signing_flags ;
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 ) ;
2015-11-09 19:26:51 +03:00
sess . cipher = global - > channels [ 0 ] . encryption_cipher ;
2021-05-07 00:55:49 +03:00
sess . signing = global - > channels [ 0 ] . signing_algo ;
2015-11-30 13:14:34 +03:00
sess . dialect = global - > connection_dialect ;
2015-11-30 13:20:43 +03:00
sess . signing_flags = global - > signing_flags ;
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_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
2019-08-29 22:52:10 +03:00
* with empty share_name first in order to allocate
2013-07-08 18:31:13 +04:00
* 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 ( data ) ;
2022-03-04 11:02:28 +03:00
data . pid = global - > server_id ;
data . cnum = global - > tcon_global_id ;
2022-07-07 11:08:36 +03:00
data . sess_id = sess_id ;
2013-11-10 14:57:37 +04:00
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 ) ;
2022-07-18 12:33:12 +03:00
data . start = global - > creation_time ;
2015-11-09 19:26:51 +03:00
data . encryption_flags = global - > encryption_flags ;
data . cipher = sess . cipher ;
2015-11-30 13:14:34 +03:00
data . dialect = sess . dialect ;
2021-05-07 00:55:49 +03:00
data . signing = sess . signing ;
2015-11-30 13:20:43 +03:00
data . signing_flags = global - > signing_flags ;
2012-09-03 13:13:16 +04:00
state - > count + + ;
2021-10-26 14:56:54 +03:00
return state - > fn ( & data , state - > private_data ) ;
2010-03-01 16:28:22 +03:00
}
2021-10-26 14:56:54 +03:00
int connections_forall_read ( int ( * fn ) ( const struct connections_data * data ,
2010-03-01 16:28:22 +03:00
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
}