1996-05-04 11:50:46 +04:00
/*
Unix SMB / Netbios implementation .
Version 1.9 .
status reporting
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1994 - 1998
1996-05-04 11:50:46 +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
the Free Software Foundation ; either version 2 of the License , or
( 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
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
1996-08-15 19:11:34 +04:00
Revision History :
12 aug 96 : Erik . Devriendt @ te6 . siemens . be
added support for shared memory implementation of share mode locking
1998-07-24 05:08:31 +04:00
21 - Jul - 1998 : rsharpe @ ns . aus . com ( Richard Sharpe )
Added - L ( locks only ) - S ( shares only ) flags and code
1996-05-04 11:50:46 +04:00
*/
/*
* This program reports current SMB connections
*/
1998-07-29 07:08:05 +04:00
# define NO_SYSLOG
1996-05-04 11:50:46 +04:00
# include "includes.h"
struct connect_record crec ;
1997-05-09 01:17:59 +04:00
struct session_record {
int pid ;
int uid ;
char machine [ 31 ] ;
time_t start ;
struct session_record * next ;
} * srecs ;
1996-05-04 11:50:46 +04:00
extern int DEBUGLEVEL ;
extern FILE * dbf ;
1996-05-05 15:34:03 +04:00
extern pstring myhostname ;
1996-05-04 11:50:46 +04:00
static pstring Ucrit_username = " " ; /* added by OH */
int Ucrit_pid [ 100 ] ; /* Ugly !!! */ /* added by OH */
int Ucrit_MaxPid = 0 ; /* added by OH */
unsigned int Ucrit_IsActive = 0 ; /* added by OH */
1998-07-24 05:08:31 +04:00
int shares_only = 0 ; /* Added by RJS */
int locks_only = 0 ; /* Added by RJS */
1997-10-20 12:46:00 +04:00
/* we need these because we link to locking*.o */
void become_root ( BOOL save_dir ) { }
void unbecome_root ( BOOL restore_dir ) { }
1997-05-20 04:32:51 +04:00
1998-08-10 05:25:32 +04:00
/* added by OH */
static void Ucrit_addUsername ( char * username )
{
pstrcpy ( Ucrit_username , username ) ;
if ( strlen ( Ucrit_username ) > 0 )
Ucrit_IsActive = 1 ;
}
static unsigned int Ucrit_checkUsername ( char * username )
{
if ( ! Ucrit_IsActive ) return 1 ;
if ( strcmp ( Ucrit_username , username ) = = 0 ) return 1 ;
return 0 ;
}
static void Ucrit_addPid ( int pid )
{
int i ;
if ( ! Ucrit_IsActive ) return ;
for ( i = 0 ; i < Ucrit_MaxPid ; i + + )
if ( pid = = Ucrit_pid [ i ] ) return ;
Ucrit_pid [ Ucrit_MaxPid + + ] = pid ;
}
static unsigned int Ucrit_checkPid ( int pid )
{
int i ;
if ( ! Ucrit_IsActive ) return 1 ;
for ( i = 0 ; i < Ucrit_MaxPid ; i + + )
if ( pid = = Ucrit_pid [ i ] ) return 1 ;
return 0 ;
}
1997-10-20 12:46:00 +04:00
static void print_share_mode ( share_mode_entry * e , char * fname )
{
static int count ;
if ( count = = 0 ) {
printf ( " Locked files: \n " ) ;
printf ( " Pid DenyMode R/W Oplock Name \n " ) ;
printf ( " -------------------------------------------------- \n " ) ;
}
count + + ;
1998-05-30 06:25:11 +04:00
if ( Ucrit_checkPid ( e - > pid ) ) {
printf ( " %-5d " , e - > pid ) ;
switch ( ( e - > share_mode > > 4 ) & 0xF ) {
case DENY_NONE : printf ( " DENY_NONE " ) ; break ;
case DENY_ALL : printf ( " DENY_ALL " ) ; break ;
case DENY_DOS : printf ( " DENY_DOS " ) ; break ;
case DENY_READ : printf ( " DENY_READ " ) ; break ;
case DENY_WRITE : printf ( " DENY_WRITE " ) ; break ;
}
switch ( e - > share_mode & 0xF ) {
case 0 : printf ( " RDONLY " ) ; break ;
case 1 : printf ( " WRONLY " ) ; break ;
case 2 : printf ( " RDWR " ) ; break ;
}
if ( ( e - > op_type &
( EXCLUSIVE_OPLOCK | BATCH_OPLOCK ) ) = =
( EXCLUSIVE_OPLOCK | BATCH_OPLOCK ) )
1997-10-20 12:46:00 +04:00
printf ( " EXCLUSIVE+BATCH " ) ;
1998-05-30 06:25:11 +04:00
else if ( e - > op_type & EXCLUSIVE_OPLOCK )
1997-10-20 12:46:00 +04:00
printf ( " EXCLUSIVE " ) ;
1998-05-30 06:25:11 +04:00
else if ( e - > op_type & BATCH_OPLOCK )
1997-10-20 12:46:00 +04:00
printf ( " BATCH " ) ;
1998-05-30 06:25:11 +04:00
else
1997-10-20 12:46:00 +04:00
printf ( " NONE " ) ;
1998-05-30 06:25:11 +04:00
printf ( " %s %s " , fname , asctime ( LocalTime ( ( time_t * ) & e - > time . tv_sec ) ) ) ;
}
1997-10-20 12:46:00 +04:00
}
1997-05-20 04:32:51 +04:00
1996-06-04 10:42:03 +04:00
int main ( int argc , char * argv [ ] )
1996-05-04 11:50:46 +04:00
{
FILE * f ;
pstring fname ;
1998-09-29 01:43:48 +04:00
int c ;
1996-05-04 11:50:46 +04:00
static pstring servicesf = CONFIGFILE ;
extern char * optarg ;
1997-05-09 01:17:59 +04:00
int verbose = 0 , brief = 0 ;
1996-05-04 11:50:46 +04:00
BOOL processes_only = False ;
int last_pid = 0 ;
1997-05-09 01:17:59 +04:00
struct session_record * ptr ;
1996-08-15 19:11:34 +04:00
1996-05-04 11:50:46 +04:00
1996-05-31 19:13:29 +04:00
TimeInit ( ) ;
1996-05-04 11:50:46 +04:00
setup_logging ( argv [ 0 ] , True ) ;
1997-07-19 00:21:32 +04:00
charset_initialise ( ) ;
1996-05-04 11:50:46 +04:00
DEBUGLEVEL = 0 ;
1998-03-22 04:31:50 +03:00
dbf = stderr ;
1996-05-04 11:50:46 +04:00
if ( getuid ( ) ! = geteuid ( ) ) {
printf ( " smbstatus should not be run setuid \n " ) ;
return ( 1 ) ;
}
1998-07-24 05:08:31 +04:00
while ( ( c = getopt ( argc , argv , " pdLSs:u:b " ) ) ! = EOF ) {
1996-05-04 11:50:46 +04:00
switch ( c ) {
1997-05-09 01:17:59 +04:00
case ' b ' :
brief = 1 ;
break ;
1996-05-04 11:50:46 +04:00
case ' d ' :
verbose = 1 ;
break ;
1998-07-24 05:08:31 +04:00
case ' L ' :
locks_only = 1 ;
break ;
1996-05-04 11:50:46 +04:00
case ' p ' :
processes_only = 1 ;
break ;
1998-07-24 05:08:31 +04:00
case ' S ' :
shares_only = 1 ;
break ;
1996-05-04 11:50:46 +04:00
case ' s ' :
1998-05-12 04:55:32 +04:00
pstrcpy ( servicesf , optarg ) ;
1996-05-04 11:50:46 +04:00
break ;
case ' u ' : /* added by OH */
Ucrit_addUsername ( optarg ) ; /* added by OH */
break ;
default :
1998-07-24 05:08:31 +04:00
fprintf ( stderr , " Usage: %s [-d] [-L] [-p] [-S] [-s configfile] [-u username] \n " , * argv ) ; /* changed by OH */
1996-05-04 11:50:46 +04:00
return ( - 1 ) ;
}
}
1997-09-06 01:32:32 +04:00
get_myname ( myhostname , NULL ) ;
1998-03-14 11:27:41 +03:00
if ( ! lp_load ( servicesf , False , False , False ) ) {
1996-05-04 11:50:46 +04:00
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " , servicesf ) ;
return ( - 1 ) ;
}
if ( verbose ) {
printf ( " using configfile = %s \n " , servicesf ) ;
printf ( " lockdir = %s \n " , * lp_lockdir ( ) ? lp_lockdir ( ) : " NULL " ) ;
}
1998-05-12 04:55:32 +04:00
pstrcpy ( fname , lp_lockdir ( ) ) ;
1996-05-04 11:50:46 +04:00
standard_sub_basic ( fname ) ;
trim_string ( fname , " " , " / " ) ;
1998-05-12 04:55:32 +04:00
pstrcat ( fname , " /STATUS..LCK " ) ;
1996-05-04 11:50:46 +04:00
1998-11-17 23:50:07 +03:00
f = sys_fopen ( fname , " r " ) ;
1996-05-04 11:50:46 +04:00
if ( ! f ) {
printf ( " Couldn't open status file %s \n " , fname ) ;
if ( ! lp_status ( - 1 ) )
printf ( " You need to have status=yes in your smb config file \n " ) ;
return ( 0 ) ;
}
1996-05-05 15:34:03 +04:00
else if ( verbose ) {
printf ( " Opened status file %s \n " , fname ) ;
}
1996-05-04 11:50:46 +04:00
1998-07-24 05:08:31 +04:00
if ( ! locks_only ) {
1996-05-04 11:50:46 +04:00
1998-07-24 05:08:31 +04:00
if ( ! processes_only ) {
printf ( " \n Samba version %s \n " , VERSION ) ;
if ( brief )
{
printf ( " PID Username Machine Time logged in \n " ) ;
printf ( " ------------------------------------------------------------------- \n " ) ;
}
else
{
printf ( " Service uid gid pid machine \n " ) ;
printf ( " ---------------------------------------------- \n " ) ;
}
1997-05-09 01:17:59 +04:00
}
1996-05-04 11:50:46 +04:00
1998-07-24 05:08:31 +04:00
while ( ! feof ( f ) )
1996-05-04 11:50:46 +04:00
{
1998-07-24 05:08:31 +04:00
if ( fread ( & crec , sizeof ( crec ) , 1 , f ) ! = 1 )
break ;
if ( crec . cnum = = - 1 ) continue ;
if ( crec . magic = = 0x280267 & & process_exists ( crec . pid )
& & Ucrit_checkUsername ( uidtoname ( crec . uid ) ) /* added by OH */
)
1997-05-09 01:17:59 +04:00
{
1998-07-24 05:08:31 +04:00
if ( brief )
{
ptr = srecs ;
while ( ptr ! = NULL )
{
if ( ( ptr - > pid = = crec . pid ) & & ( strncmp ( ptr - > machine , crec . machine , 30 ) = = 0 ) )
{
if ( ptr - > start > crec . start )
ptr - > start = crec . start ;
break ;
}
ptr = ptr - > next ;
}
if ( ptr = = NULL )
{
ptr = ( struct session_record * ) malloc ( sizeof ( struct session_record ) ) ;
ptr - > uid = crec . uid ;
ptr - > pid = crec . pid ;
ptr - > start = crec . start ;
strncpy ( ptr - > machine , crec . machine , 30 ) ;
ptr - > machine [ 30 ] = ' \0 ' ;
ptr - > next = srecs ;
srecs = ptr ;
}
}
else
{
Ucrit_addPid ( crec . pid ) ; /* added by OH */
if ( processes_only ) {
if ( last_pid ! = crec . pid )
printf ( " %d \n " , crec . pid ) ;
last_pid = crec . pid ; /* XXXX we can still get repeats, have to
1997-05-09 01:17:59 +04:00
add a sort at some time */
1998-07-24 05:08:31 +04:00
}
else
printf ( " %-10.10s %-8s %-8s %5d %-8s (%s) %s " ,
crec . name , uidtoname ( crec . uid ) , gidtoname ( crec . gid ) , crec . pid ,
crec . machine , crec . addr ,
asctime ( LocalTime ( & crec . start ) ) ) ;
}
1997-05-09 01:17:59 +04:00
}
1996-05-04 11:50:46 +04:00
}
1998-07-24 05:08:31 +04:00
fclose ( f ) ;
}
1996-05-04 11:50:46 +04:00
if ( processes_only ) exit ( 0 ) ;
1997-05-09 01:17:59 +04:00
if ( brief )
{
ptr = srecs ;
while ( ptr ! = NULL )
{
printf ( " %-8d%-10.10s%-30.30s%s " , ptr - > pid , uidtoname ( ptr - > uid ) , ptr - > machine , asctime ( LocalTime ( & ( ptr - > start ) ) ) ) ;
ptr = ptr - > next ;
}
printf ( " \n " ) ;
exit ( 0 ) ;
}
1996-05-04 11:50:46 +04:00
printf ( " \n " ) ;
1998-07-24 05:08:31 +04:00
if ( ! shares_only ) {
1998-11-05 15:40:37 +03:00
if ( ! locking_init ( 1 ) ) {
printf ( " Can't initialise shared memory - exiting \n " ) ;
exit ( 1 ) ;
}
1997-05-20 04:32:51 +04:00
1998-11-05 15:40:37 +03:00
if ( share_mode_forall ( print_share_mode ) < = 0 )
printf ( " No locked files \n " ) ;
printf ( " \n " ) ;
share_status ( stdout ) ;
locking_end ( ) ;
1998-07-24 05:08:31 +04:00
}
1996-05-04 11:50:46 +04:00
return ( 0 ) ;
}