2019-04-11 13:03:30 +02:00
/*
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"
2011-02-25 23:20:06 +01:00
# include "system/filesys.h"
2010-07-08 18:00:07 +02:00
# include "smbd/globals.h"
2024-02-08 10:04:32 +01:00
# include "source3/smbd/smbXsrv_session.h"
2011-07-07 17:42:08 +02:00
# include "dbwrap/dbwrap.h"
2011-07-06 16:40:21 +02:00
# include "dbwrap/dbwrap_open.h"
2012-09-03 11:13:16 +02:00
# include "dbwrap/dbwrap_rbt.h"
2011-12-15 11:50:01 +01:00
# include "messages.h"
2019-04-11 13:03:30 +02:00
# include "conn_tdb.h"
2012-09-03 11:13:16 +02:00
# include "util_tdb.h"
2020-08-07 11:17:34 -07:00
# include "lib/util/string_wrappers.h"
2023-07-03 15:10:08 +02:00
# include "../libcli/security/session.h"
2007-05-08 13:44:36 +00:00
2012-09-03 11:13:16 +02:00
struct connections_forall_state {
struct db_context * session_by_pid ;
2021-10-26 13:56:54 +02:00
int ( * fn ) ( const struct connections_data * data ,
2010-03-01 14:28:22 +01:00
void * private_data ) ;
void * private_data ;
2012-09-03 11:13:16 +02:00
int count ;
} ;
struct connections_forall_session {
uid_t uid ;
gid_t gid ;
2013-11-10 11:56:06 +01:00
fstring machine ;
fstring addr ;
2015-11-09 17:26:51 +01:00
uint16_t cipher ;
2015-11-30 11:14:34 +01:00
uint16_t dialect ;
2021-05-06 23:55:49 +02:00
uint16_t signing ;
2023-07-03 15:10:08 +02:00
bool authenticated ;
2010-03-01 14:28:22 +01:00
} ;
2012-09-03 11:13:16 +02:00
static int collect_sessions_fn ( struct smbXsrv_session_global0 * global ,
void * connections_forall_state )
2010-03-01 14:28:22 +01:00
{
2012-09-03 11:13:16 +02: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 ;
2023-07-03 15:10:08 +02:00
enum security_user_level ul ;
2012-09-03 11:13:16 +02:00
2013-07-05 13:19:59 +02: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 11:58:58 +01:00
fstrcpy ( sess . machine , global - > channels [ 0 ] . remote_name ) ;
fstrcpy ( sess . addr , global - > channels [ 0 ] . remote_address ) ;
2015-11-09 17:26:51 +01:00
sess . cipher = global - > channels [ 0 ] . encryption_cipher ;
2021-05-06 23:55:49 +02:00
sess . signing = global - > channels [ 0 ] . signing_algo ;
2015-11-30 11:14:34 +01:00
sess . dialect = global - > connection_dialect ;
2023-07-03 15:10:08 +02:00
ul = security_session_user_level ( global - > auth_session_info , NULL ) ;
if ( ul > = SECURITY_USER ) {
sess . authenticated = true ;
} else {
sess . authenticated = false ;
}
2012-09-03 11:13:16 +02: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 14:28:22 +01:00
2012-09-03 11:13:16 +02:00
uint32_t sess_id = global - > session_global_id ;
struct connections_forall_session sess = {
. uid = - 1 ,
. gid = - 1 ,
} ;
2011-08-25 00:01:44 +02:00
2012-09-03 11:13:16 +02:00
TDB_DATA val = tdb_null ;
2013-07-08 16:31:13 +02: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 21:52:10 +02:00
* with empty share_name first in order to allocate
2013-07-08 16:31:13 +02:00
* an id , before filling in the details .
*/
return 0 ;
}
2012-09-03 11:13:16 +02: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 14:28:22 +01:00
}
2012-09-03 11:13:16 +02:00
ZERO_STRUCT ( data ) ;
2022-03-04 09:02:28 +01:00
data . pid = global - > server_id ;
data . cnum = global - > tcon_global_id ;
2022-07-07 10:08:36 +02:00
data . sess_id = sess_id ;
2013-11-10 11:57:37 +01:00
fstrcpy ( data . servicename , global - > share_name ) ;
2012-09-03 11:13:16 +02:00
data . uid = sess . uid ;
data . gid = sess . gid ;
2013-11-10 11:57:37 +01:00
fstrcpy ( data . addr , sess . addr ) ;
fstrcpy ( data . machine , sess . machine ) ;
2022-07-18 11:33:12 +02:00
data . start = global - > creation_time ;
2015-11-09 17:26:51 +01:00
data . encryption_flags = global - > encryption_flags ;
data . cipher = sess . cipher ;
2015-11-30 11:14:34 +01:00
data . dialect = sess . dialect ;
2021-05-06 23:55:49 +02:00
data . signing = sess . signing ;
2015-11-30 11:20:43 +01:00
data . signing_flags = global - > signing_flags ;
2023-07-03 15:10:08 +02:00
data . authenticated = sess . authenticated ;
2012-09-03 11:13:16 +02:00
state - > count + + ;
2021-10-26 13:56:54 +02:00
return state - > fn ( & data , state - > private_data ) ;
2010-03-01 14:28:22 +01:00
}
2021-10-26 13:56:54 +02:00
int connections_forall_read ( int ( * fn ) ( const struct connections_data * data ,
2010-03-01 14:28:22 +01:00
void * private_data ) ,
void * private_data )
{
2012-09-03 11:13:16 +02:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
struct connections_forall_state * state =
talloc_zero ( talloc_tos ( ) , struct connections_forall_state ) ;
2011-08-25 00:01:44 +02:00
NTSTATUS status ;
2012-09-03 11:13:16 +02:00
int ret = - 1 ;
2010-03-01 14:28:22 +01:00
2012-09-03 11:13:16 +02: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 14:28:22 +01:00
}
2012-09-03 11:13:16 +02:00
status = smbXsrv_tcon_global_traverse ( traverse_tcon_fn , state ) ;
2011-08-25 00:01:44 +02:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2012-09-03 11:13:16 +02:00
DEBUG ( 0 , ( " Failed to traverse tree connects: %s \n " ,
nt_errstr ( status ) ) ) ;
goto done ;
2011-08-25 00:01:44 +02:00
}
2012-09-03 11:13:16 +02:00
ret = state - > count ;
done :
talloc_free ( frame ) ;
return ret ;
2010-03-01 14:28:22 +01:00
}