2007-10-25 01:16:54 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
NBT client - used to lookup netbios names
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1994 - 1998
2003-04-14 07:59:04 +04:00
Copyright ( C ) Jelmer Vernooij 2003 ( Conversion to popt )
2007-10-25 01:16:54 +04:00
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( at your option ) any later version .
2007-10-25 01:16:54 +04:00
1996-05-04 11:50:46 +04: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 .
2007-10-25 01:16:54 +04:00
1996-05-04 11:50:46 +04:00
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/>.
2007-10-25 01:16:54 +04:00
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2010-08-05 12:49:53 +04:00
# include "popt_common.h"
2011-03-23 16:18:59 +03:00
# include "libsmb/nmblib.h"
1996-05-04 11:50:46 +04:00
2007-10-25 01:16:54 +04:00
static bool give_flags = false ;
static bool use_bcast = true ;
static bool got_bcast = false ;
static struct sockaddr_storage bcast_addr ;
static bool recursion_desired = false ;
static bool translate_addresses = false ;
1999-12-13 16:27:58 +03:00
static int ServerFD = - 1 ;
2007-10-25 01:16:54 +04:00
static bool RootPort = false ;
static bool find_status = false ;
1997-10-04 05:07:47 +04:00
1996-05-04 11:50:46 +04:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Open the socket communication .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool open_sockets ( void )
1996-05-04 11:50:46 +04:00
{
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2012-07-27 06:56:22 +04:00
const char * sock_addr = lp_nbt_client_socket_address ( ) ;
2007-10-25 01:16:54 +04:00
if ( ! interpret_string_addr ( & ss , sock_addr ,
AI_NUMERICHOST | AI_PASSIVE ) ) {
DEBUG ( 0 , ( " open_sockets: unable to get socket address "
" from string %s " , sock_addr ) ) ;
return false ;
}
ServerFD = open_socket_in ( SOCK_DGRAM ,
( RootPort ? 137 : 0 ) ,
( RootPort ? 0 : 3 ) ,
& ss , true ) ;
1999-12-04 22:14:37 +03:00
2007-10-25 01:16:54 +04:00
if ( ServerFD = = - 1 ) {
return false ;
}
1996-05-04 11:50:46 +04:00
2007-10-25 01:16:54 +04:00
set_socket_options ( ServerFD , " SO_BROADCAST " ) ;
1999-12-13 16:27:58 +03:00
2007-10-25 01:16:54 +04:00
DEBUG ( 3 , ( " Socket opened. \n " ) ) ;
return true ;
1996-05-04 11:50:46 +04:00
}
2000-12-20 06:22:51 +03:00
/****************************************************************************
turn a node status flags field into a string
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static char * node_status_flags ( unsigned char flags )
{
static fstring ret ;
fstrcpy ( ret , " " ) ;
2007-10-25 01:16:54 +04:00
2000-12-20 06:22:51 +03:00
fstrcat ( ret , ( flags & 0x80 ) ? " <GROUP> " : " " ) ;
if ( ( flags & 0x60 ) = = 0x00 ) fstrcat ( ret , " B " ) ;
if ( ( flags & 0x60 ) = = 0x20 ) fstrcat ( ret , " P " ) ;
if ( ( flags & 0x60 ) = = 0x40 ) fstrcat ( ret , " M " ) ;
if ( ( flags & 0x60 ) = = 0x60 ) fstrcat ( ret , " H " ) ;
if ( flags & 0x10 ) fstrcat ( ret , " <DEREGISTERING> " ) ;
if ( flags & 0x08 ) fstrcat ( ret , " <CONFLICT> " ) ;
if ( flags & 0x04 ) fstrcat ( ret , " <ACTIVE> " ) ;
if ( flags & 0x02 ) fstrcat ( ret , " <PERMANENT> " ) ;
2007-10-25 01:16:54 +04:00
2000-12-20 06:22:51 +03:00
return ret ;
}
2002-07-15 14:35:28 +04:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Turn the NMB Query flags into a string .
2002-07-15 14:35:28 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2002-07-15 14:35:28 +04:00
static char * query_flags ( int flags )
{
static fstring ret1 ;
fstrcpy ( ret1 , " " ) ;
if ( flags & NM_FLAGS_RS ) fstrcat ( ret1 , " Response " ) ;
if ( flags & NM_FLAGS_AA ) fstrcat ( ret1 , " Authoritative " ) ;
if ( flags & NM_FLAGS_TC ) fstrcat ( ret1 , " Truncated " ) ;
if ( flags & NM_FLAGS_RD ) fstrcat ( ret1 , " Recursion_Desired " ) ;
if ( flags & NM_FLAGS_RA ) fstrcat ( ret1 , " Recursion_Available " ) ;
if ( flags & NM_FLAGS_B ) fstrcat ( ret1 , " Broadcast " ) ;
return ret1 ;
}
2000-12-20 06:22:51 +03:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Do a node status query .
2000-12-20 06:22:51 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2011-01-04 20:34:38 +03:00
static void do_node_status ( const char * name ,
2007-10-25 01:16:54 +04:00
int type ,
struct sockaddr_storage * pss )
2000-12-20 06:22:51 +03:00
{
struct nmb_name nname ;
int count , i , j ;
2010-12-28 14:53:12 +03:00
struct node_status * addrs ;
2004-04-16 07:57:30 +04:00
struct node_status_extra extra ;
2000-12-20 06:22:51 +03:00
fstring cleanname ;
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2010-12-28 14:53:12 +03:00
NTSTATUS status ;
2000-12-20 06:22:51 +03:00
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , pss ) ;
d_printf ( " Looking up status of %s \n " , addr ) ;
2000-12-20 06:22:51 +03:00
make_nmb_name ( & nname , name , type ) ;
2011-01-04 20:34:38 +03:00
status = node_status_query ( talloc_tos ( ) , & nname , pss ,
2010-12-28 14:53:12 +03:00
& addrs , & count , & extra ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
2000-12-20 06:22:51 +03:00
for ( i = 0 ; i < count ; i + + ) {
2010-12-28 14:53:12 +03:00
pull_ascii_fstring ( cleanname , addrs [ i ] . name ) ;
2000-12-20 06:22:51 +03:00
for ( j = 0 ; cleanname [ j ] ; j + + ) {
2007-10-25 01:16:54 +04:00
if ( ! isprint ( ( int ) cleanname [ j ] ) ) {
cleanname [ j ] = ' . ' ;
}
2000-12-20 06:22:51 +03:00
}
2001-09-08 06:59:23 +04:00
d_printf ( " \t %-15s <%02x> - %s \n " ,
2010-12-28 14:53:12 +03:00
cleanname , addrs [ i ] . type ,
node_status_flags ( addrs [ i ] . flags ) ) ;
2000-12-20 06:22:51 +03:00
}
2004-09-28 06:13:53 +04:00
d_printf ( " \n \t MAC Address = %02X-%02X-%02X-%02X-%02X-%02X \n " ,
extra . mac_addr [ 0 ] , extra . mac_addr [ 1 ] ,
extra . mac_addr [ 2 ] , extra . mac_addr [ 3 ] ,
extra . mac_addr [ 4 ] , extra . mac_addr [ 5 ] ) ;
d_printf ( " \n " ) ;
2010-12-28 14:53:12 +03:00
TALLOC_FREE ( addrs ) ;
2004-09-28 06:13:53 +04:00
} else {
2007-10-25 01:16:54 +04:00
d_printf ( " No reply from %s \n \n " , addr ) ;
2000-12-20 06:22:51 +03:00
}
}
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
/****************************************************************************
2007-10-25 01:16:54 +04:00
Send out one query .
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-25 01:16:54 +04:00
2007-10-19 04:40:25 +04:00
static bool query_one ( const char * lookup , unsigned int lookup_type )
1999-12-13 16:27:58 +03:00
{
2011-01-04 20:48:47 +03:00
int j , count ;
uint8_t flags ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage * ip_list = NULL ;
2010-12-30 14:42:50 +03:00
NTSTATUS status = NT_STATUS_NOT_FOUND ;
1999-12-13 16:27:58 +03:00
if ( got_bcast ) {
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
print_sockaddr ( addr , sizeof ( addr ) , & bcast_addr ) ;
d_printf ( " querying %s on %s \n " , lookup , addr ) ;
2011-01-04 20:48:47 +03:00
status = name_query ( lookup , lookup_type , use_bcast ,
2010-12-28 15:47:35 +03:00
use_bcast ? true : recursion_desired ,
& bcast_addr , talloc_tos ( ) ,
2011-01-04 20:48:47 +03:00
& ip_list , & count , & flags ) ;
1999-12-13 16:27:58 +03:00
} else {
2011-05-03 01:39:09 +04:00
status = name_resolve_bcast (
lookup , lookup_type ,
talloc_tos ( ) , & ip_list , & count ) ;
1999-12-13 16:27:58 +03:00
}
2010-12-28 15:47:35 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2007-10-25 01:16:54 +04:00
return false ;
}
1999-12-13 16:27:58 +03:00
2007-10-25 01:16:54 +04:00
if ( give_flags ) {
d_printf ( " Flags: %s \n " , query_flags ( flags ) ) ;
}
2002-07-15 14:35:28 +04:00
1999-12-13 16:27:58 +03:00
for ( j = 0 ; j < count ; j + + ) {
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
1999-12-13 16:27:58 +03:00
if ( translate_addresses ) {
2007-11-16 05:27:26 +03:00
char h_name [ MAX_DNS_NAME_LENGTH ] ;
2007-10-25 01:16:54 +04:00
h_name [ 0 ] = ' \0 ' ;
2007-11-02 22:05:17 +03:00
if ( sys_getnameinfo ( ( const struct sockaddr * ) & ip_list [ j ] ,
2007-10-25 01:16:54 +04:00
sizeof ( struct sockaddr_storage ) ,
h_name , sizeof ( h_name ) ,
NULL , 0 ,
NI_NAMEREQD ) ) {
continue ;
1999-12-13 16:27:58 +03:00
}
2007-10-25 01:16:54 +04:00
d_printf ( " %s, " , h_name ) ;
1999-12-13 16:27:58 +03:00
}
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & ip_list [ j ] ) ;
d_printf ( " %s %s<%02x> \n " , addr , lookup , lookup_type ) ;
2006-02-16 23:50:41 +03:00
/* We can only do find_status if the ip address returned
was valid - ie . name_query returned true .
*/
if ( find_status ) {
2011-01-04 20:34:38 +03:00
do_node_status ( lookup , lookup_type , & ip_list [ j ] ) ;
2006-02-16 23:50:41 +03:00
}
1999-12-13 16:27:58 +03:00
}
2010-12-28 15:47:35 +03:00
TALLOC_FREE ( ip_list ) ;
2000-04-25 04:46:46 +04:00
2010-12-28 15:47:35 +03:00
return NT_STATUS_IS_OK ( status ) ;
1999-12-13 16:27:58 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int main ( int argc , char * argv [ ] )
{
2007-10-25 01:16:54 +04:00
int opt ;
unsigned int lookup_type = 0x0 ;
fstring lookup ;
static bool find_master = False ;
static bool lookup_by_ip = False ;
poptContext pc ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
struct poptOption long_options [ ] = {
POPT_AUTOHELP
{ " broadcast " , ' B ' , POPT_ARG_STRING , NULL , ' B ' , " Specify address to use for broadcasts " , " BROADCAST-ADDRESS " } ,
{ " flags " , ' f ' , POPT_ARG_NONE , NULL , ' f ' , " List the NMB flags returned " } ,
{ " unicast " , ' U ' , POPT_ARG_STRING , NULL , ' U ' , " Specify address to use for unicast " } ,
{ " master-browser " , ' M ' , POPT_ARG_NONE , NULL , ' M ' , " Search for a master browser " } ,
2007-10-27 03:22:18 +04:00
{ " recursion " , ' R ' , POPT_ARG_NONE , NULL , ' R ' , " Set recursion desired in package " } ,
{ " status " , ' S ' , POPT_ARG_NONE , NULL , ' S ' , " Lookup node status as well " } ,
2007-10-25 01:16:54 +04:00
{ " translate " , ' T ' , POPT_ARG_NONE , NULL , ' T ' , " Translate IP addresses into names " } ,
2007-10-27 03:22:18 +04:00
{ " root-port " , ' r ' , POPT_ARG_NONE , NULL , ' r ' , " Use root port 137 (Win95 only replies to this) " } ,
{ " lookup-by-ip " , ' A ' , POPT_ARG_NONE , NULL , ' A ' , " Do a node status on <name> as an IP Address " } ,
2007-10-25 01:16:54 +04:00
POPT_COMMON_SAMBA
POPT_COMMON_CONNECTION
{ 0 , 0 , 0 , 0 }
} ;
* lookup = 0 ;
load_case_tables ( ) ;
2010-10-29 07:19:32 +04:00
setup_logging ( argv [ 0 ] , DEBUG_STDOUT ) ;
2007-10-25 01:16:54 +04:00
pc = poptGetContext ( " nmblookup " , argc , ( const char * * ) argv ,
long_options , POPT_CONTEXT_KEEP_FIRST ) ;
poptSetOtherOptionHelp ( pc , " <NODE> ... " ) ;
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
switch ( opt ) {
case ' f ' :
give_flags = true ;
break ;
case ' M ' :
find_master = true ;
break ;
case ' R ' :
recursion_desired = true ;
break ;
case ' S ' :
find_status = true ;
break ;
case ' r ' :
RootPort = true ;
break ;
case ' A ' :
lookup_by_ip = true ;
break ;
case ' B ' :
if ( interpret_string_addr ( & bcast_addr ,
poptGetOptArg ( pc ) ,
NI_NUMERICHOST ) ) {
got_bcast = True ;
use_bcast = True ;
}
break ;
case ' U ' :
if ( interpret_string_addr ( & bcast_addr ,
poptGetOptArg ( pc ) ,
0 ) ) {
got_bcast = True ;
use_bcast = False ;
}
break ;
case ' T ' :
translate_addresses = ! translate_addresses ;
break ;
}
}
poptGetArg ( pc ) ; /* Remove argv[0] */
if ( ! poptPeekArg ( pc ) ) {
poptPrintUsage ( pc , stderr , 0 ) ;
exit ( 1 ) ;
}
2011-07-28 12:18:55 +04:00
if ( ! lp_load_global ( get_dyn_CONFIGFILE ( ) ) ) {
2007-10-25 01:16:54 +04:00
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " ,
2007-12-10 22:30:37 +03:00
get_dyn_CONFIGFILE ( ) ) ;
2007-10-25 01:16:54 +04:00
}
load_interfaces ( ) ;
if ( ! open_sockets ( ) ) {
return ( 1 ) ;
}
while ( poptPeekArg ( pc ) ) {
char * p ;
struct in_addr ip ;
fstrcpy ( lookup , poptGetArg ( pc ) ) ;
if ( lookup_by_ip ) {
struct sockaddr_storage ss ;
2008-10-23 22:41:15 +04:00
ip = interpret_addr2 ( lookup ) ;
2007-10-25 01:16:54 +04:00
in_addr_to_sockaddr_storage ( & ss , ip ) ;
fstrcpy ( lookup , " * " ) ;
2011-01-04 20:34:38 +03:00
do_node_status ( lookup , lookup_type , & ss ) ;
2007-10-25 01:16:54 +04:00
continue ;
}
if ( find_master ) {
if ( * lookup = = ' - ' ) {
fstrcpy ( lookup , " \01 \02 __MSBROWSE__ \02 " ) ;
lookup_type = 1 ;
} else {
lookup_type = 0x1d ;
}
}
p = strchr_m ( lookup , ' # ' ) ;
if ( p ) {
* p = ' \0 ' ;
sscanf ( + + p , " %x " , & lookup_type ) ;
}
if ( ! query_one ( lookup , lookup_type ) ) {
d_printf ( " name_query failed to find name %s " , lookup ) ;
if ( 0 ! = lookup_type ) {
d_printf ( " #%02x " , lookup_type ) ;
}
d_printf ( " \n " ) ;
}
}
poptFreeContext ( pc ) ;
TALLOC_FREE ( frame ) ;
return ( 0 ) ;
1996-05-04 11:50:46 +04:00
}