2001-07-06 14:49:34 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2001-07-06 14:49:34 +04:00
Network neighbourhood browser .
Copyright ( C ) Tim Potter 2000
2003-04-14 07:30:20 +04:00
Copyright ( C ) Jelmer Vernooij 2003
2001-07-06 14:49:34 +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 .
*/
# include "includes.h"
2001-08-20 21:38:37 +04:00
static BOOL use_bcast ;
2001-07-06 14:49:34 +04:00
/* How low can we go? */
enum tree_level { LEV_WORKGROUP , LEV_SERVER , LEV_SHARE } ;
2002-08-17 18:45:04 +04:00
static enum tree_level level = LEV_SHARE ;
2001-07-06 14:49:34 +04:00
/* Holds a list of workgroups or servers */
struct name_list {
struct name_list * prev , * next ;
pstring name , comment ;
uint32 server_type ;
} ;
static struct name_list * workgroups , * servers , * shares ;
static void free_name_list ( struct name_list * list )
{
while ( list )
DLIST_REMOVE ( list , list ) ;
}
static void add_name ( const char * machine_name , uint32 server_type ,
const char * comment , void * state )
{
struct name_list * * name_list = ( struct name_list * * ) state ;
struct name_list * new_name ;
2004-12-07 21:25:53 +03:00
new_name = SMB_MALLOC_P ( struct name_list ) ;
2001-07-06 14:49:34 +04:00
if ( ! new_name )
return ;
ZERO_STRUCTP ( new_name ) ;
pstrcpy ( new_name - > name , machine_name ) ;
pstrcpy ( new_name - > comment , comment ) ;
new_name - > server_type = server_type ;
DLIST_ADD ( * name_list , new_name ) ;
}
/****************************************************************************
display tree of smb workgroups , servers and shares
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL get_workgroups ( struct user_auth_info * user_info )
{
struct cli_state * cli ;
struct in_addr server_ip ;
pstring master_workgroup ;
/* Try to connect to a #1d name of our current workgroup. If that
doesn ' t work broadcast for a master browser and then jump off
that workgroup . */
pstrcpy ( master_workgroup , lp_workgroup ( ) ) ;
2003-04-14 07:30:20 +04:00
if ( ! use_bcast & & ! find_master_ip ( lp_workgroup ( ) , & server_ip ) ) {
DEBUG ( 4 , ( " Unable to find master browser for workgroup %s, falling back to broadcast \n " ,
2001-07-06 14:49:34 +04:00
master_workgroup ) ) ;
2003-04-14 07:30:20 +04:00
use_bcast = True ;
} else if ( ! use_bcast ) {
if ( ! ( cli = get_ipc_connect ( inet_ntoa ( server_ip ) , & server_ip , user_info ) ) )
return False ;
}
if ( ! ( cli = get_ipc_connect_master_ip_bcast ( master_workgroup , user_info ) ) ) {
2001-07-06 14:49:34 +04:00
DEBUG ( 4 , ( " Unable to find master browser by "
" broadcast \n " ) ) ;
return False ;
}
if ( ! cli_NetServerEnum ( cli , master_workgroup ,
SV_TYPE_DOMAIN_ENUM , add_name , & workgroups ) )
return False ;
return True ;
}
/* Retrieve the list of servers for a given workgroup */
static BOOL get_servers ( char * workgroup , struct user_auth_info * user_info )
{
struct cli_state * cli ;
struct in_addr server_ip ;
/* Open an IPC$ connection to the master browser for the workgroup */
if ( ! find_master_ip ( workgroup , & server_ip ) ) {
DEBUG ( 4 , ( " Cannot find master browser for workgroup %s \n " ,
workgroup ) ) ;
return False ;
}
2002-07-15 14:35:28 +04:00
if ( ! ( cli = get_ipc_connect ( inet_ntoa ( server_ip ) , & server_ip , user_info ) ) )
2001-07-06 14:49:34 +04:00
return False ;
if ( ! cli_NetServerEnum ( cli , workgroup , SV_TYPE_ALL , add_name ,
& servers ) )
return False ;
return True ;
}
static BOOL get_shares ( char * server_name , struct user_auth_info * user_info )
{
struct cli_state * cli ;
2002-07-15 14:35:28 +04:00
if ( ! ( cli = get_ipc_connect ( server_name , NULL , user_info ) ) )
2001-07-06 14:49:34 +04:00
return False ;
if ( ! cli_RNetShareEnum ( cli , add_name , & shares ) )
return False ;
return True ;
}
static BOOL print_tree ( struct user_auth_info * user_info )
{
struct name_list * wg , * sv , * sh ;
/* List workgroups */
if ( ! get_workgroups ( user_info ) )
return False ;
for ( wg = workgroups ; wg ; wg = wg - > next ) {
printf ( " %s \n " , wg - > name ) ;
/* List servers */
free_name_list ( servers ) ;
servers = NULL ;
if ( level = = LEV_WORKGROUP | |
! get_servers ( wg - > name , user_info ) )
continue ;
for ( sv = servers ; sv ; sv = sv - > next ) {
2001-07-25 08:09:44 +04:00
printf ( " \t \\ \\ %-15s \t \t %s \n " ,
sv - > name , sv - > comment ) ;
2001-07-06 14:49:34 +04:00
/* List shares */
free_name_list ( shares ) ;
shares = NULL ;
if ( level = = LEV_SERVER | |
! get_shares ( sv - > name , user_info ) )
continue ;
for ( sh = shares ; sh ; sh = sh - > next ) {
2001-07-25 08:09:44 +04:00
printf ( " \t \t \\ \\ %s \\ %-15s \t %s \n " ,
sv - > name , sh - > name , sh - > comment ) ;
2001-07-06 14:49:34 +04:00
}
}
}
return True ;
}
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int main ( int argc , char * argv [ ] )
{
2003-04-14 07:30:20 +04:00
struct poptOption long_options [ ] = {
POPT_AUTOHELP
{ " broadcast " , ' b ' , POPT_ARG_VAL , & use_bcast , True , " Use broadcast instead of using the master browser " } ,
{ " domains " , ' D ' , POPT_ARG_VAL , & level , LEV_WORKGROUP , " List only domains (workgroups) of tree " } ,
{ " servers " , ' S ' , POPT_ARG_VAL , & level , LEV_SERVER , " List domains(workgroups) and servers of tree " } ,
POPT_COMMON_SAMBA
POPT_COMMON_CREDENTIALS
POPT_TABLEEND
} ;
poptContext pc ;
2001-07-06 14:49:34 +04:00
/* Initialise samba stuff */
2005-12-29 01:48:54 +03:00
load_case_tables ( ) ;
2001-07-06 14:49:34 +04:00
setlinebuf ( stdout ) ;
2001-09-10 15:08:57 +04:00
dbf = x_stderr ;
2001-07-06 14:49:34 +04:00
setup_logging ( argv [ 0 ] , True ) ;
2003-04-14 07:30:20 +04:00
pc = poptGetContext ( " smbtree " , argc , ( const char * * ) argv , long_options ,
POPT_CONTEXT_KEEP_FIRST ) ;
while ( poptGetNextOpt ( pc ) ! = - 1 ) ;
poptFreeContext ( pc ) ;
2001-11-19 05:49:53 +03:00
lp_load ( dyn_CONFIGFILE , True , False , False ) ;
2001-07-06 14:49:34 +04:00
load_interfaces ( ) ;
/* Parse command line args */
2003-04-14 07:30:20 +04:00
if ( ! cmdline_auth_info . got_pass ) {
2001-07-06 14:49:34 +04:00
char * pass = getpass ( " Password: " ) ;
if ( pass ) {
2003-04-14 07:30:20 +04:00
pstrcpy ( cmdline_auth_info . password , pass ) ;
2001-07-06 14:49:34 +04:00
}
2003-04-14 07:30:20 +04:00
cmdline_auth_info . got_pass = True ;
2001-07-06 14:49:34 +04:00
}
/* Now do our stuff */
2003-04-14 07:30:20 +04:00
if ( ! print_tree ( & cmdline_auth_info ) )
2001-07-06 14:49:34 +04:00
return 1 ;
return 0 ;
}