2001-07-06 10:49:34 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
2001-07-06 10:49:34 +00:00
Network neighbourhood browser .
Copyright ( C ) Tim Potter 2000
2003-04-14 03:30:20 +00:00
Copyright ( C ) Jelmer Vernooij 2003
2001-07-06 10:49:34 +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
2001-07-06 10:49:34 +00: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 00:52:41 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2001-07-06 10:49:34 +00:00
*/
# include "includes.h"
2018-08-13 15:39:08 -07:00
# include "popt_common_cmdline.h"
2011-02-28 10:19:44 +01:00
# include "rpc_client/cli_pipe.h"
2011-01-12 13:01:57 +01:00
# include "../librpc/gen_ndr/ndr_srvsvc_c.h"
2011-05-06 11:47:43 +02:00
# include "libsmb/libsmb.h"
2018-03-10 15:31:11 +01:00
# include "libsmb/namequery.h"
2011-02-24 10:46:55 +01:00
# include "libsmb/clirap.h"
2019-10-29 12:11:49 -07:00
# include "../libcli/smb/smbXcli_base.h"
2020-04-05 13:01:07 +02:00
# include "nameserv.h"
2020-04-11 19:54:11 +02:00
# include "libsmbclient.h"
2001-08-20 17:38:37 +00:00
2001-07-06 10:49:34 +00:00
/* How low can we go? */
enum tree_level { LEV_WORKGROUP , LEV_SERVER , LEV_SHARE } ;
2002-08-17 14:45:04 +00:00
static enum tree_level level = LEV_SHARE ;
2001-07-06 10:49:34 +00:00
2020-04-11 19:54:11 +02:00
static void get_auth_data_with_context_fn (
SMBCCTX * context ,
const char * server ,
const char * share ,
char * domain ,
int domain_len ,
char * user ,
int user_len ,
char * password ,
int password_len )
2001-07-06 10:49:34 +00:00
{
2020-04-11 19:54:11 +02:00
struct user_auth_info * auth = popt_get_cmdline_auth_info ( ) ;
size_t len ;
2001-07-06 10:49:34 +00:00
2020-04-11 19:54:11 +02:00
if ( auth = = NULL ) {
2007-12-03 18:48:41 -08:00
return ;
}
2020-04-11 19:54:11 +02:00
len = strlcpy ( domain , get_cmdline_auth_info_domain ( auth ) , domain_len ) ;
if ( ( int ) len > = domain_len ) {
return ;
2006-05-16 01:21:16 +00:00
}
2020-04-11 19:54:11 +02:00
len = strlcpy (
user , get_cmdline_auth_info_username ( auth ) , user_len ) ;
if ( ( int ) len > = user_len ) {
return ;
2006-05-16 01:21:16 +00:00
}
2020-04-11 19:54:11 +02:00
len = strlcpy (
password , get_cmdline_auth_info_password ( auth ) , password_len ) ;
if ( ( int ) len > = password_len ) {
/* pointless, but what can you do... */
return ;
2019-10-29 12:11:49 -07:00
}
2001-07-06 10:49:34 +00:00
}
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2014-02-26 20:16:26 +01:00
int main ( int argc , char * argv [ ] )
2001-07-06 10:49:34 +00:00
{
2007-09-04 05:39:06 +00:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2014-02-26 20:16:26 +01:00
const char * * argv_const = discard_const_p ( const char * , argv ) ;
2003-04-14 03:30:20 +00:00
struct poptOption long_options [ ] = {
POPT_AUTOHELP
2019-01-11 14:53:36 +01:00
{
. longName = " domains " ,
. shortName = ' D ' ,
. argInfo = POPT_ARG_VAL ,
. arg = & level ,
. val = LEV_WORKGROUP ,
. descrip = " List only domains (workgroups) of tree " ,
} ,
{
. longName = " servers " ,
. shortName = ' S ' ,
. argInfo = POPT_ARG_VAL ,
. arg = & level ,
. val = LEV_SERVER ,
. descrip = " List domains(workgroups) and servers of tree " ,
} ,
2003-04-14 03:30:20 +00:00
POPT_COMMON_SAMBA
POPT_COMMON_CREDENTIALS
POPT_TABLEEND
} ;
poptContext pc ;
2020-04-11 19:54:11 +02:00
SMBCCTX * ctx = NULL ;
SMBCFILE * workgroups = NULL ;
struct smbc_dirent * dirent = NULL ;
bool ok ;
int ret , result = 1 ;
int debuglevel ;
2007-12-03 18:48:41 -08:00
2001-07-06 10:49:34 +00:00
/* Initialise samba stuff */
2015-03-21 20:00:06 +01:00
smb_init_locale ( ) ;
2001-07-06 10:49:34 +00:00
setlinebuf ( stdout ) ;
2010-10-29 14:19:32 +11:00
setup_logging ( argv [ 0 ] , DEBUG_STDERR ) ;
2001-07-06 10:49:34 +00:00
2016-12-09 16:04:38 +01:00
popt_common_credentials_set_ignore_missing_conf ( ) ;
2014-02-26 20:16:26 +01:00
pc = poptGetContext ( " smbtree " , argc , argv_const , long_options ,
POPT_CONTEXT_KEEP_FIRST ) ;
2003-04-14 03:30:20 +00:00
while ( poptGetNextOpt ( pc ) ! = - 1 ) ;
2012-11-06 09:27:43 +01:00
popt_burn_cmdline_password ( argc , argv ) ;
2003-04-14 03:30:20 +00:00
2020-04-11 19:54:11 +02:00
debuglevel = DEBUGLEVEL ;
ctx = smbc_new_context ( ) ;
if ( ctx = = NULL ) {
perror ( " smbc_new_context " ) ;
goto fail ;
}
ret = smbc_setConfiguration ( ctx , get_dyn_CONFIGFILE ( ) ) ;
if ( ret = = - 1 ) {
perror ( " smbc_setConfiguration " ) ;
goto fail ;
}
smbc_setDebug ( ctx , debuglevel ) ;
ok = smbc_setOptionProtocols ( ctx , NULL , " NT1 " ) ;
if ( ! ok ) {
perror ( " smbc_setOptionProtocols " ) ;
goto fail ;
}
smbc_setFunctionAuthDataWithContext (
ctx , get_auth_data_with_context_fn ) ;
2001-07-06 10:49:34 +00:00
2020-04-11 19:54:11 +02:00
ok = smbc_init_context ( ctx ) ;
if ( ! ok ) {
perror ( " smbc_init_context " ) ;
goto fail ;
}
workgroups = smbc_getFunctionOpendir ( ctx ) ( ctx , " smb:// " ) ;
if ( workgroups = = NULL ) {
perror ( " smbc_opendir " ) ;
goto fail ;
}
while ( ( dirent = smbc_getFunctionReaddir ( ctx ) ( ctx , workgroups ) )
! = NULL ) {
char * url = NULL ;
SMBCFILE * servers = NULL ;
if ( dirent - > smbc_type ! = SMBC_WORKGROUP ) {
continue ;
}
printf ( " %s \n " , dirent - > name ) ;
if ( level = = LEV_WORKGROUP ) {
continue ;
}
url = talloc_asprintf (
talloc_tos ( ) , " smb://%s/ " , dirent - > name ) ;
if ( url = = NULL ) {
perror ( " talloc_asprintf " ) ;
goto fail ;
}
servers = smbc_getFunctionOpendir ( ctx ) ( ctx , url ) ;
if ( servers = = NULL ) {
perror ( " smbc_opendir " ) ;
goto fail ;
}
TALLOC_FREE ( url ) ;
while ( ( dirent = smbc_getFunctionReaddir ( ctx ) ( ctx , servers ) )
! = NULL ) {
SMBCFILE * shares = NULL ;
char * servername = NULL ;
if ( dirent - > smbc_type ! = SMBC_SERVER ) {
continue ;
}
printf ( " \t \\ \\ %-15s \t \t %s \n " ,
dirent - > name ,
dirent - > comment ) ;
if ( level = = LEV_SERVER ) {
continue ;
}
/*
* The subsequent readdir for shares will
* overwrite the " server " readdir
*/
servername = talloc_strdup ( talloc_tos ( ) , dirent - > name ) ;
if ( servername = = NULL ) {
continue ;
}
url = talloc_asprintf (
talloc_tos ( ) , " smb://%s/ " , servername ) ;
if ( url = = NULL ) {
perror ( " talloc_asprintf " ) ;
goto fail ;
}
shares = smbc_getFunctionOpendir ( ctx ) ( ctx , url ) ;
if ( shares = = NULL ) {
perror ( " smbc_opendir " ) ;
goto fail ;
}
while ( ( dirent = smbc_getFunctionReaddir (
ctx ) ( ctx , shares ) )
! = NULL ) {
printf ( " \t \t \\ \\ %s \\ %-15s \t %s \n " ,
servername ,
dirent - > name ,
dirent - > comment ) ;
}
ret = smbc_getFunctionClosedir ( ctx ) ( ctx , shares ) ;
if ( ret = = - 1 ) {
perror ( " smbc_closedir " ) ;
goto fail ;
}
TALLOC_FREE ( servername ) ;
TALLOC_FREE ( url ) ;
}
ret = smbc_getFunctionClosedir ( ctx ) ( ctx , servers ) ;
if ( ret = = - 1 ) {
perror ( " smbc_closedir " ) ;
goto fail ;
}
}
ret = smbc_getFunctionClosedir ( ctx ) ( ctx , workgroups ) ;
if ( ret = = - 1 ) {
perror ( " smbc_closedir " ) ;
2020-04-11 19:03:39 +02:00
goto fail ;
2007-09-04 05:39:06 +00:00
}
2001-07-06 10:49:34 +00:00
2017-04-25 17:08:30 -07:00
popt_free_cmdline_auth_info ( ) ;
2020-04-11 19:03:39 +02:00
result = 0 ;
fail :
2020-04-11 19:54:11 +02:00
if ( ctx ! = NULL ) {
smbc_free_context ( ctx , 0 ) ;
ctx = NULL ;
}
2019-08-19 13:29:03 +02:00
poptFreeContext ( pc ) ;
2007-09-04 05:39:06 +00:00
TALLOC_FREE ( frame ) ;
2020-04-11 19:03:39 +02:00
return result ;
2001-07-06 10:49:34 +00:00
}