1998-03-15 05:37:52 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1998-03-15 05:37:52 +03:00
connection claim routines
Copyright ( C ) Andrew Tridgell 1998
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
1998-03-15 05:37:52 +03:00
( at your option ) any later version .
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 .
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/>.
1998-03-15 05:37:52 +03:00
*/
# include "includes.h"
1999-12-21 07:54:30 +03:00
/****************************************************************************
2001-05-14 10:15:46 +04:00
Delete a connection record .
1999-12-21 07:54:30 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-14 10:15:46 +04:00
2007-10-19 04:40:25 +04:00
bool yield_connection ( connection_struct * conn , const char * name )
1999-12-21 07:54:30 +03:00
{
2007-05-28 15:38:42 +04:00
struct db_record * rec ;
NTSTATUS status ;
1998-03-15 05:37:52 +03:00
1999-12-21 07:54:30 +03:00
DEBUG ( 3 , ( " Yielding connection to %s \n " , name ) ) ;
1998-03-15 05:37:52 +03:00
2007-05-28 15:38:42 +04:00
if ( ! ( rec = connections_fetch_entry ( NULL , conn , name ) ) ) {
DEBUG ( 0 , ( " connections_fetch_entry failed \n " ) ) ;
return False ;
}
1999-12-21 07:54:30 +03:00
2007-05-28 15:38:42 +04:00
status = rec - > delete_rec ( rec ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( NT_STATUS_EQUAL ( status , NT_STATUS_NOT_FOUND ) ? 3 : 0 ,
( " deleting connection record returned %s \n " ,
nt_errstr ( status ) ) ) ;
2001-06-09 00:26:46 +04:00
}
2000-01-12 06:09:17 +03:00
2007-05-29 22:14:54 +04:00
TALLOC_FREE ( rec ) ;
2007-05-28 15:38:42 +04:00
return NT_STATUS_IS_OK ( status ) ;
1998-03-15 05:37:52 +03:00
}
2001-05-14 10:15:46 +04:00
struct count_stat {
pid_t mypid ;
int curr_connections ;
2006-07-11 22:01:26 +04:00
const char * name ;
2007-10-19 04:40:25 +04:00
bool Clear ;
2001-05-14 10:15:46 +04:00
} ;
/****************************************************************************
Count the entries belonging to a service in the connection db .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-28 15:38:42 +04:00
static int count_fn ( struct db_record * rec ,
const struct connections_key * ckey ,
const struct connections_data * crec ,
void * udp )
2001-05-14 10:15:46 +04:00
{
struct count_stat * cs = ( struct count_stat * ) udp ;
2007-05-28 15:38:42 +04:00
if ( crec - > cnum = = - 1 ) {
2001-05-14 10:15:46 +04:00
return 0 ;
2007-04-21 01:09:44 +04:00
}
2001-05-14 10:15:46 +04:00
2001-05-17 04:24:34 +04:00
/* If the pid was not found delete the entry from connections.tdb */
2007-05-28 15:38:42 +04:00
if ( cs - > Clear & & ! process_exists ( crec - > pid ) & & ( errno = = ESRCH ) ) {
NTSTATUS status ;
2005-09-30 21:13:37 +04:00
DEBUG ( 2 , ( " pid %s doesn't exist - deleting connections %d [%s] \n " ,
2007-05-28 15:38:42 +04:00
procid_str_static ( & crec - > pid ) , crec - > cnum ,
crec - > servicename ) ) ;
status = rec - > delete_rec ( rec ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " count_fn: tdb_delete failed with error %s \n " ,
nt_errstr ( status ) ) ) ;
}
2001-05-14 10:15:46 +04:00
return 0 ;
}
2007-10-11 00:34:30 +04:00
if ( strequal ( crec - > servicename , cs - > name ) )
2001-05-14 10:15:46 +04:00
cs - > curr_connections + + ;
return 0 ;
}
1998-03-15 05:37:52 +03:00
1999-12-21 07:54:30 +03:00
/****************************************************************************
2001-05-14 10:15:46 +04:00
Claim an entry in the connections database .
1998-03-15 05:37:52 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-14 10:15:46 +04:00
2007-10-19 04:40:25 +04:00
int count_current_connections ( const char * sharename , bool clear )
1998-03-15 05:37:52 +03:00
{
2006-07-11 22:01:26 +04:00
struct count_stat cs ;
2002-09-25 19:19:00 +04:00
2006-07-11 22:01:26 +04:00
cs . mypid = sys_getpid ( ) ;
cs . curr_connections = 0 ;
cs . name = sharename ;
cs . Clear = clear ;
2001-05-14 10:15:46 +04:00
/*
2006-07-11 22:01:26 +04:00
* This has a race condition , but locking the chain before hand is worse
* as it leads to deadlock .
2001-05-14 10:15:46 +04:00
*/
2007-05-28 15:38:42 +04:00
if ( connections_forall ( count_fn , & cs ) = = - 1 ) {
DEBUG ( 0 , ( " count_current_connections: traverse of "
" connections.tdb failed \n " ) ) ;
2007-10-11 00:34:30 +04:00
return False ;
2006-07-11 22:01:26 +04:00
}
2007-04-21 01:09:44 +04:00
2006-07-11 22:01:26 +04:00
return cs . curr_connections ;
}
2001-05-14 10:15:46 +04:00
2006-07-11 22:01:26 +04:00
/****************************************************************************
Claim an entry in the connections database .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-14 10:15:46 +04:00
2007-10-19 04:40:25 +04:00
bool claim_connection ( connection_struct * conn , const char * name ,
2007-05-27 20:34:49 +04:00
uint32 msg_flags )
2006-07-11 22:01:26 +04:00
{
2007-05-28 15:38:42 +04:00
struct db_record * rec ;
2006-07-11 22:01:26 +04:00
struct connections_data crec ;
2007-05-28 15:38:42 +04:00
TDB_DATA dbuf ;
NTSTATUS status ;
2007-11-04 04:15:45 +03:00
char addr [ INET6_ADDRSTRLEN ] ;
2001-09-19 11:06:34 +04:00
2007-05-28 15:38:42 +04:00
DEBUG ( 5 , ( " claiming [%s] \n " , name ) ) ;
if ( ! ( rec = connections_fetch_entry ( NULL , conn , name ) ) ) {
DEBUG ( 0 , ( " connections_fetch_entry failed \n " ) ) ;
2007-05-08 17:44:36 +04:00
return False ;
2006-07-11 22:01:26 +04:00
}
1998-03-15 05:37:52 +03:00
/* fill in the crec */
1999-12-21 07:54:30 +03:00
ZERO_STRUCT ( crec ) ;
1998-03-15 05:37:52 +03:00
crec . magic = 0x280267 ;
2005-09-30 21:13:37 +04:00
crec . pid = procid_self ( ) ;
1999-12-21 07:54:30 +03:00
crec . cnum = conn ? conn - > cnum : - 1 ;
1998-08-14 21:38:29 +04:00
if ( conn ) {
crec . uid = conn - > uid ;
crec . gid = conn - > gid ;
2007-05-28 15:38:42 +04:00
strlcpy ( crec . servicename , lp_servicename ( SNUM ( conn ) ) ,
sizeof ( crec . servicename ) ) ;
1998-03-15 05:37:52 +03:00
}
crec . start = time ( NULL ) ;
2002-09-25 19:19:00 +04:00
crec . bcast_msg_flags = msg_flags ;
1998-03-15 05:37:52 +03:00
2007-05-28 15:38:42 +04:00
strlcpy ( crec . machine , get_remote_machine_name ( ) , sizeof ( crec . machine ) ) ;
2007-11-04 09:20:10 +03:00
strlcpy ( crec . addr , conn ? conn - > client_address :
client_addr ( get_client_fd ( ) , addr , sizeof ( addr ) ) ,
2007-05-28 15:38:42 +04:00
sizeof ( crec . addr ) ) ;
1998-03-15 05:37:52 +03:00
2007-03-29 13:35:51 +04:00
dbuf . dptr = ( uint8 * ) & crec ;
1999-12-21 07:54:30 +03:00
dbuf . dsize = sizeof ( crec ) ;
2007-05-28 15:38:42 +04:00
status = rec - > store ( rec , dbuf , TDB_REPLACE ) ;
TALLOC_FREE ( rec ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2001-05-24 01:33:43 +04:00
DEBUG ( 0 , ( " claim_connection: tdb_store failed with error %s. \n " ,
2007-05-28 15:38:42 +04:00
nt_errstr ( status ) ) ) ;
2001-09-19 11:06:34 +04:00
return False ;
2001-05-24 01:33:43 +04:00
}
1999-12-21 07:54:30 +03:00
2001-09-19 11:06:34 +04:00
return True ;
2001-05-14 22:43:49 +04:00
}
2002-09-25 19:19:00 +04:00
2007-10-19 04:40:25 +04:00
bool register_message_flags ( bool doreg , uint32 msg_flags )
2002-09-25 19:19:00 +04:00
{
2007-05-28 15:38:42 +04:00
struct db_record * rec ;
2002-09-25 19:19:00 +04:00
struct connections_data * pcrec ;
2007-05-28 15:38:42 +04:00
NTSTATUS status ;
2002-09-25 19:19:00 +04:00
DEBUG ( 10 , ( " register_message_flags: %s flags 0x%x \n " ,
doreg ? " adding " : " removing " ,
( unsigned int ) msg_flags ) ) ;
2007-08-07 17:10:21 +04:00
if ( ! ( rec = connections_fetch_entry ( NULL , NULL , " " ) ) ) {
2007-05-28 15:38:42 +04:00
DEBUG ( 0 , ( " connections_fetch_entry failed \n " ) ) ;
return False ;
}
2002-09-25 19:19:00 +04:00
2007-05-28 15:38:42 +04:00
if ( rec - > value . dsize ! = sizeof ( struct connections_data ) ) {
DEBUG ( 0 , ( " register_message_flags: Got wrong record size \n " ) ) ;
TALLOC_FREE ( rec ) ;
2002-09-25 19:19:00 +04:00
return False ;
}
2007-05-28 15:38:42 +04:00
pcrec = ( struct connections_data * ) rec - > value . dptr ;
2002-09-25 19:19:00 +04:00
if ( doreg )
pcrec - > bcast_msg_flags | = msg_flags ;
else
pcrec - > bcast_msg_flags & = ~ msg_flags ;
2007-05-28 15:38:42 +04:00
status = rec - > store ( rec , rec - > value , TDB_REPLACE ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-11-30 18:55:27 +03:00
DEBUG ( 0 , ( " register_message_flags: tdb_store failed: %s. \n " ,
2007-05-28 15:38:42 +04:00
nt_errstr ( status ) ) ) ;
2007-08-07 17:10:21 +04:00
TALLOC_FREE ( rec ) ;
2002-09-25 19:19:00 +04:00
return False ;
}
DEBUG ( 10 , ( " register_message_flags: new flags 0x%x \n " ,
( unsigned int ) pcrec - > bcast_msg_flags ) ) ;
2007-08-07 17:10:21 +04:00
TALLOC_FREE ( rec ) ;
2002-09-25 19:19:00 +04:00
return True ;
}
2006-07-11 22:01:26 +04:00
/*********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static TDB_DATA * make_pipe_rec_key ( struct pipe_open_rec * prec )
{
TDB_DATA * kbuf = NULL ;
fstring key_string ;
if ( ! prec )
return NULL ;
if ( ( kbuf = TALLOC_P ( prec , TDB_DATA ) ) = = NULL ) {
return NULL ;
}
snprintf ( key_string , sizeof ( key_string ) , " %s/%d/%d " ,
prec - > name , procid_to_pid ( & prec - > pid ) , prec - > pnum ) ;
2007-03-29 12:24:15 +04:00
* kbuf = string_term_tdb_data ( talloc_strdup ( prec , key_string ) ) ;
if ( kbuf - > dptr = = NULL )
2006-07-11 22:01:26 +04:00
return NULL ;
2007-03-29 12:24:15 +04:00
2006-07-11 22:01:26 +04:00
return kbuf ;
}
/*********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void fill_pipe_open_rec ( struct pipe_open_rec * prec , smb_np_struct * p )
{
prec - > pid = pid_to_procid ( sys_getpid ( ) ) ;
prec - > pnum = p - > pnum ;
prec - > uid = geteuid ( ) ;
fstrcpy ( prec - > name , p - > name ) ;
return ;
}
/*********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool store_pipe_opendb ( smb_np_struct * p )
2006-07-11 22:01:26 +04:00
{
2007-05-28 15:38:42 +04:00
struct db_record * dbrec ;
2006-07-11 22:01:26 +04:00
struct pipe_open_rec * prec ;
TDB_DATA * key ;
TDB_DATA data ;
2007-10-19 04:40:25 +04:00
bool ret = False ;
2006-07-11 22:01:26 +04:00
if ( ( prec = TALLOC_P ( NULL , struct pipe_open_rec ) ) = = NULL ) {
DEBUG ( 0 , ( " store_pipe_opendb: talloc failed! \n " ) ) ;
return False ;
}
fill_pipe_open_rec ( prec , p ) ;
if ( ( key = make_pipe_rec_key ( prec ) ) = = NULL ) {
goto done ;
}
2007-03-29 13:35:51 +04:00
data . dptr = ( uint8 * ) prec ;
2006-07-11 22:01:26 +04:00
data . dsize = sizeof ( struct pipe_open_rec ) ;
2007-05-28 15:38:42 +04:00
if ( ! ( dbrec = connections_fetch_record ( prec , * key ) ) ) {
DEBUG ( 0 , ( " connections_fetch_record failed \n " ) ) ;
2006-07-11 22:01:26 +04:00
goto done ;
}
2007-05-28 15:38:42 +04:00
ret = NT_STATUS_IS_OK ( dbrec - > store ( dbrec , data , TDB_REPLACE ) ) ;
2006-07-11 22:01:26 +04:00
done :
TALLOC_FREE ( prec ) ;
return ret ;
}
/*********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool delete_pipe_opendb ( smb_np_struct * p )
2006-07-11 22:01:26 +04:00
{
2007-05-28 15:38:42 +04:00
struct db_record * dbrec ;
2006-07-11 22:01:26 +04:00
struct pipe_open_rec * prec ;
TDB_DATA * key ;
2007-10-19 04:40:25 +04:00
bool ret = False ;
2006-07-11 22:01:26 +04:00
if ( ( prec = TALLOC_P ( NULL , struct pipe_open_rec ) ) = = NULL ) {
DEBUG ( 0 , ( " store_pipe_opendb: talloc failed! \n " ) ) ;
return False ;
}
fill_pipe_open_rec ( prec , p ) ;
if ( ( key = make_pipe_rec_key ( prec ) ) = = NULL ) {
goto done ;
}
2007-05-28 15:38:42 +04:00
if ( ! ( dbrec = connections_fetch_record ( prec , * key ) ) ) {
DEBUG ( 0 , ( " connections_fetch_record failed \n " ) ) ;
2006-07-11 22:01:26 +04:00
goto done ;
}
2007-05-28 15:38:42 +04:00
ret = NT_STATUS_IS_OK ( dbrec - > delete_rec ( dbrec ) ) ;
2006-07-11 22:01:26 +04:00
done :
TALLOC_FREE ( prec ) ;
return ret ;
}