1996-05-04 11:50:46 +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 )
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 .
*/
# include "includes.h"
2002-01-08 00:32:22 +03:00
extern BOOL AllowDebugChange ;
2002-07-15 14:35:28 +04:00
static BOOL give_flags = False ;
1999-12-13 16:27:58 +03:00
static BOOL use_bcast = True ;
static BOOL got_bcast = False ;
static struct in_addr bcast_addr ;
static BOOL recursion_desired = False ;
static BOOL translate_addresses = False ;
static int ServerFD = - 1 ;
2000-10-11 20:24:20 +04:00
static int RootPort = False ;
1999-12-13 16:27:58 +03:00
static BOOL find_status = False ;
1997-10-04 05:07:47 +04:00
1996-05-04 11:50:46 +04:00
/****************************************************************************
open the socket communication
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL open_sockets ( void )
{
1999-12-13 16:27:58 +03:00
ServerFD = open_socket_in ( SOCK_DGRAM ,
2000-10-11 20:24:20 +04:00
( RootPort ? 137 : 0 ) ,
( RootPort ? 0 : 3 ) ,
1999-12-13 16:27:58 +03:00
interpret_addr ( lp_socket_address ( ) ) , True ) ;
1999-12-04 22:14:37 +03:00
1999-12-13 16:27:58 +03:00
if ( ServerFD = = - 1 )
1996-05-04 11:50:46 +04:00
return ( False ) ;
2000-10-11 20:24:20 +04:00
set_socket_options ( ServerFD , " SO_BROADCAST " ) ;
1999-12-13 16:27:58 +03:00
DEBUG ( 3 , ( " Socket opened. \n " ) ) ;
1996-05-04 11:50:46 +04:00
return True ;
}
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 , " " ) ;
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> " ) ;
return ret ;
}
2002-07-15 14:35:28 +04:00
/****************************************************************************
turn the NMB Query flags into a string
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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
/****************************************************************************
do a node status query
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
static void do_node_status ( int fd , const char * name , int type , struct in_addr ip )
2000-12-20 06:22:51 +03:00
{
struct nmb_name nname ;
int count , i , j ;
2005-06-25 00:25:18 +04:00
NODE_STATUS_STRUCT * status ;
2004-04-16 07:57:30 +04:00
struct node_status_extra extra ;
2000-12-20 06:22:51 +03:00
fstring cleanname ;
2001-09-08 06:59:23 +04:00
d_printf ( " Looking up status of %s \n " , inet_ntoa ( ip ) ) ;
2000-12-20 06:22:51 +03:00
make_nmb_name ( & nname , name , type ) ;
2004-04-16 07:57:30 +04:00
status = node_status_query ( fd , & nname , ip , & count , & extra ) ;
2000-12-20 06:22:51 +03:00
if ( status ) {
for ( i = 0 ; i < count ; i + + ) {
2003-08-22 04:13:42 +04:00
pull_ascii_fstring ( cleanname , status [ i ] . name ) ;
2000-12-20 06:22:51 +03:00
for ( j = 0 ; cleanname [ j ] ; j + + ) {
2001-04-28 04:05:11 +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 " ,
2000-12-20 06:22:51 +03:00
cleanname , status [ i ] . type ,
node_status_flags ( status [ i ] . flags ) ) ;
}
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 " ) ;
2001-09-17 15:48:29 +04:00
SAFE_FREE ( status ) ;
2004-09-28 06:13:53 +04:00
} else {
d_printf ( " No reply from %s \n \n " , inet_ntoa ( ip ) ) ;
2000-12-20 06:22:51 +03:00
}
}
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
/****************************************************************************
send out one query
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 11:28:12 +03:00
static BOOL query_one ( const char * lookup , unsigned int lookup_type )
1999-12-13 16:27:58 +03:00
{
2002-07-15 14:35:28 +04:00
int j , count , flags = 0 ;
1999-12-13 16:27:58 +03:00
struct in_addr * ip_list = NULL ;
if ( got_bcast ) {
2001-09-08 06:59:23 +04:00
d_printf ( " querying %s on %s \n " , lookup , inet_ntoa ( bcast_addr ) ) ;
1999-12-13 16:27:58 +03:00
ip_list = name_query ( ServerFD , lookup , lookup_type , use_bcast ,
use_bcast ? True : recursion_desired ,
2002-07-15 14:35:28 +04:00
bcast_addr , & count , & flags , NULL ) ;
1999-12-13 16:27:58 +03:00
} else {
struct in_addr * bcast ;
for ( j = iface_count ( ) - 1 ;
! ip_list & & j > = 0 ;
j - - ) {
bcast = iface_n_bcast ( j ) ;
2001-09-08 06:59:23 +04:00
d_printf ( " querying %s on %s \n " ,
1999-12-13 16:27:58 +03:00
lookup , inet_ntoa ( * bcast ) ) ;
ip_list = name_query ( ServerFD , lookup , lookup_type ,
use_bcast ,
use_bcast ? True : recursion_desired ,
2002-07-15 14:35:28 +04:00
* bcast , & count , & flags , NULL ) ;
1999-12-13 16:27:58 +03:00
}
}
if ( ! ip_list ) return False ;
2002-07-15 14:35:28 +04:00
if ( give_flags )
d_printf ( " Flags: %s \n " , query_flags ( flags ) ) ;
1999-12-13 16:27:58 +03:00
for ( j = 0 ; j < count ; j + + ) {
if ( translate_addresses ) {
struct hostent * host = gethostbyaddr ( ( char * ) & ip_list [ j ] , sizeof ( ip_list [ j ] ) , AF_INET ) ;
if ( host ) {
2001-09-08 06:59:23 +04:00
d_printf ( " %s, " , host - > h_name ) ;
1999-12-13 16:27:58 +03:00
}
}
2001-09-08 06:59:23 +04:00
d_printf ( " %s %s<%02x> \n " , inet_ntoa ( ip_list [ j ] ) , lookup , lookup_type ) ;
1999-12-13 16:27:58 +03:00
}
/* We can only do find_status if the ip address returned
was valid - ie . name_query returned true .
*/
if ( find_status ) {
2000-12-20 06:22:51 +03:00
do_node_status ( ServerFD , lookup , lookup_type , ip_list [ 0 ] ) ;
1999-12-13 16:27:58 +03:00
}
2000-04-25 04:46:46 +04:00
safe_free ( ip_list ) ;
1999-12-13 16:27:58 +03:00
return ( ip_list ! = NULL ) ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int main ( int argc , char * argv [ ] )
{
int opt ;
1996-06-29 22:49:20 +04:00
unsigned int lookup_type = 0x0 ;
2003-01-14 11:53:59 +03:00
fstring lookup ;
2003-04-14 07:59:04 +04:00
static BOOL find_master = False ;
static BOOL lookup_by_ip = False ;
poptContext pc ;
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_VAL , & give_flags , True , " List the NMB flags returned " } ,
{ " unicast " , ' U ' , POPT_ARG_STRING , NULL , ' U ' , " Specify address to use for unicast " } ,
{ " master-browser " , ' M ' , POPT_ARG_VAL , & find_master , True , " Search for a master browser " } ,
{ " recursion " , ' R ' , POPT_ARG_VAL , & recursion_desired , True , " Set recursion desired in package " } ,
{ " status " , ' S ' , POPT_ARG_VAL , & find_status , True , " Lookup node status as well " } ,
{ " translate " , ' T ' , POPT_ARG_NONE , NULL , ' T ' , " Translate IP addresses into names " } ,
{ " root-port " , ' r ' , POPT_ARG_VAL , & RootPort , True , " Use root port 137 (Win95 only replies to this) " } ,
{ " lookup-by-ip " , ' A ' , POPT_ARG_VAL , & lookup_by_ip , True , " Do a node status on <name> as an IP Address " } ,
POPT_COMMON_SAMBA
POPT_COMMON_CONNECTION
{ 0 , 0 , 0 , 0 }
} ;
1996-05-04 11:50:46 +04:00
* lookup = 0 ;
2006-01-02 21:23:49 +03:00
load_case_tables ( ) ;
1996-05-04 11:50:46 +04:00
setup_logging ( argv [ 0 ] , True ) ;
2003-04-14 07:59:04 +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 ' B ' :
bcast_addr = * interpret_addr2 ( poptGetOptArg ( pc ) ) ;
got_bcast = True ;
use_bcast = True ;
break ;
case ' U ' :
bcast_addr = * interpret_addr2 ( poptGetOptArg ( pc ) ) ;
got_bcast = True ;
use_bcast = False ;
break ;
case ' T ' :
translate_addresses = ! translate_addresses ;
break ;
}
1996-05-04 11:50:46 +04:00
}
2003-04-14 07:59:04 +04:00
poptGetArg ( pc ) ; /* Remove argv[0] */
1996-10-02 18:09:22 +04:00
2003-04-14 07:59:04 +04:00
if ( ! poptPeekArg ( pc ) ) {
poptPrintUsage ( pc , stderr , 0 ) ;
exit ( 1 ) ;
}
1996-10-07 15:06:34 +04:00
2006-01-29 01:53:04 +03:00
if ( ! lp_load ( dyn_CONFIGFILE , True , False , False , True ) ) {
2003-04-14 07:59:04 +04:00
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " , dyn_CONFIGFILE ) ;
}
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
load_interfaces ( ) ;
if ( ! open_sockets ( ) ) return ( 1 ) ;
1996-05-04 11:50:46 +04:00
2003-04-14 07:59:04 +04:00
while ( poptPeekArg ( pc ) )
1996-08-01 21:49:40 +04:00
{
2003-04-14 07:59:04 +04:00
char * p ;
struct in_addr ip ;
fstrcpy ( lookup , poptGetArg ( pc ) ) ;
if ( lookup_by_ip )
{
ip = * interpret_addr2 ( lookup ) ;
fstrcpy ( lookup , " * " ) ;
do_node_status ( ServerFD , lookup , lookup_type , ip ) ;
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 " ) ;
}
1996-08-01 21:49:40 +04:00
}
2003-04-14 07:59:04 +04:00
poptFreeContext ( pc ) ;
1996-05-04 11:50:46 +04:00
return ( 0 ) ;
}