2005-02-10 06:22:47 +03:00
/*
Unix SMB / CIFS implementation .
NBT WINS server testing
Copyright ( C ) Andrew Tridgell 2005
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"
2005-02-10 09:59:29 +03:00
# include "lib/socket/socket.h"
2006-03-07 14:07:23 +03:00
# include "libcli/resolve/resolve.h"
# include "system/network.h"
# include "netif/netif.h"
2006-03-16 03:23:11 +03:00
# include "librpc/gen_ndr/ndr_nbt.h"
2005-02-10 06:22:47 +03:00
# define CHECK_VALUE(v, correct) do { \
if ( ( v ) ! = ( correct ) ) { \
2005-12-31 11:42:01 +03:00
printf ( " (%s) Incorrect value %s=%d (0x%X) - should be %d (0x%X) \n " , \
__location__ , # v , v , v , correct , correct ) ; \
2005-02-10 06:22:47 +03:00
ret = False ; \
} } while ( 0 )
# define CHECK_STRING(v, correct) do { \
2005-02-11 09:58:07 +03:00
if ( ( v ) ! = ( correct ) & & \
2005-08-30 15:55:05 +04:00
( ( v ) = = NULL | | ( correct ) = = NULL | | strcasecmp_m ( v , correct ) ! = 0 ) ) { \
2005-02-10 06:22:47 +03:00
printf ( " (%s) Incorrect value %s='%s' - should be '%s' \n " , \
__location__ , # v , v , correct ) ; \
ret = False ; \
} } while ( 0 )
2005-02-11 09:58:07 +03:00
# define CHECK_NAME(_name, correct) do { \
CHECK_STRING ( ( _name ) . name , ( correct ) . name ) ; \
2005-12-31 11:42:01 +03:00
CHECK_VALUE ( ( uint8_t ) ( _name ) . type , ( uint8_t ) ( correct ) . type ) ; \
2005-02-11 09:58:07 +03:00
CHECK_STRING ( ( _name ) . scope , ( correct ) . scope ) ; \
} while ( 0 )
2005-02-10 06:22:47 +03:00
2005-02-14 06:39:25 +03:00
2005-02-10 06:22:47 +03:00
/*
test operations against a WINS server
*/
2005-02-11 09:58:07 +03:00
static BOOL nbt_test_wins_name ( TALLOC_CTX * mem_ctx , const char * address ,
2005-02-14 06:39:25 +03:00
struct nbt_name * name , uint16_t nb_flags )
2005-02-10 06:22:47 +03:00
{
struct nbt_name_register_wins io ;
struct nbt_name_query query ;
struct nbt_name_refresh_wins refresh ;
struct nbt_name_release release ;
NTSTATUS status ;
struct nbt_name_socket * nbtsock = nbt_name_socket_init ( mem_ctx , NULL ) ;
BOOL ret = True ;
2005-02-16 04:48:11 +03:00
const char * myaddress = talloc_strdup ( mem_ctx , iface_best_ip ( address ) ) ;
2006-01-10 01:12:53 +03:00
struct socket_address * socket_address ;
socket_address = socket_address_from_strings ( mem_ctx ,
nbtsock - > sock - > backend_name ,
myaddress , 0 ) ;
if ( ! socket_address ) {
return False ;
}
2005-02-10 06:22:47 +03:00
/* we do the listen here to ensure the WINS server receives the packets from
the right IP */
2006-01-10 01:12:53 +03:00
status = socket_listen ( nbtsock - > sock , socket_address , 0 , 0 ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " socket_listen for WINS failed: %s \n " , nt_errstr ( status ) ) ;
return False ;
}
talloc_free ( socket_address ) ;
2005-02-10 06:22:47 +03:00
2005-02-14 06:39:25 +03:00
printf ( " Testing name registration to WINS with name %s at %s nb_flags=0x%x \n " ,
nbt_name_string ( mem_ctx , name ) , myaddress , nb_flags ) ;
2005-02-10 06:22:47 +03:00
2005-02-11 13:37:59 +03:00
printf ( " release the name \n " ) ;
release . in . name = * name ;
release . in . dest_addr = address ;
release . in . address = myaddress ;
2005-02-14 06:39:25 +03:00
release . in . nb_flags = nb_flags ;
2005-02-11 13:37:59 +03:00
release . in . broadcast = False ;
release . in . timeout = 3 ;
release . in . retries = 0 ;
status = nbt_name_release ( nbtsock , mem_ctx , & release ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name release \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Bad response from %s for name query - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
CHECK_VALUE ( release . out . rcode , 0 ) ;
printf ( " register the name \n " ) ;
2005-02-11 09:58:07 +03:00
io . in . name = * name ;
2005-02-10 06:22:47 +03:00
io . in . wins_servers = str_list_make ( mem_ctx , address , NULL ) ;
io . in . addresses = str_list_make ( mem_ctx , myaddress , NULL ) ;
2005-02-14 06:39:25 +03:00
io . in . nb_flags = nb_flags ;
2005-02-10 06:22:47 +03:00
io . in . ttl = 300000 ;
status = nbt_name_register_wins ( nbtsock , mem_ctx , & io ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name register \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Bad response from %s for name register - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
CHECK_STRING ( io . out . wins_server , address ) ;
CHECK_VALUE ( io . out . rcode , 0 ) ;
2005-12-22 02:05:32 +03:00
if ( name - > type ! = NBT_NAME_MASTER & &
2005-02-15 14:14:04 +03:00
name - > type ! = NBT_NAME_LOGON & &
2005-12-22 02:05:32 +03:00
name - > type ! = NBT_NAME_BROWSER & &
2005-02-15 14:14:04 +03:00
( nb_flags & NBT_NM_GROUP ) ) {
2005-02-14 06:39:25 +03:00
printf ( " Try to register as non-group \n " ) ;
io . in . nb_flags & = ~ NBT_NM_GROUP ;
status = nbt_name_register_wins ( nbtsock , mem_ctx , & io ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Bad response from %s for name register - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
CHECK_VALUE ( io . out . rcode , NBT_RCODE_ACT ) ;
}
2005-02-10 06:22:47 +03:00
printf ( " query the name to make sure its there \n " ) ;
2005-02-11 09:58:07 +03:00
query . in . name = * name ;
2005-02-10 06:22:47 +03:00
query . in . dest_addr = address ;
query . in . broadcast = False ;
query . in . wins_lookup = True ;
query . in . timeout = 3 ;
query . in . retries = 0 ;
status = nbt_name_query ( nbtsock , mem_ctx , & query ) ;
2005-02-14 15:46:03 +03:00
if ( name - > type = = NBT_NAME_MASTER ) {
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ) {
printf ( " Bad response from %s for name query - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
return ret ;
}
2005-02-10 06:22:47 +03:00
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name query \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Bad response from %s for name query - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
2005-02-11 09:58:07 +03:00
CHECK_NAME ( query . out . name , * name ) ;
2005-02-10 06:22:47 +03:00
CHECK_VALUE ( query . out . num_addrs , 1 ) ;
2005-02-15 14:14:04 +03:00
if ( name - > type ! = NBT_NAME_LOGON & &
( nb_flags & NBT_NM_GROUP ) ) {
2005-02-14 06:39:25 +03:00
CHECK_STRING ( query . out . reply_addrs [ 0 ] , " 255.255.255.255 " ) ;
} else {
CHECK_STRING ( query . out . reply_addrs [ 0 ] , myaddress ) ;
}
2005-02-10 06:22:47 +03:00
2005-02-11 10:54:20 +03:00
query . in . name . name = strupper_talloc ( mem_ctx , name - > name ) ;
if ( query . in . name . name & &
strcmp ( query . in . name . name , name - > name ) ! = 0 ) {
printf ( " check case sensitivity \n " ) ;
status = nbt_name_query ( nbtsock , mem_ctx , & query ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name query \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ) {
printf ( " Bad response from %s for name query - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
}
query . in . name = * name ;
if ( name - > scope ) {
query . in . name . scope = strupper_talloc ( mem_ctx , name - > scope ) ;
}
if ( query . in . name . scope & &
strcmp ( query . in . name . scope , name - > scope ) ! = 0 ) {
printf ( " check case sensitivity on scope \n " ) ;
status = nbt_name_query ( nbtsock , mem_ctx , & query ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name query \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ) {
printf ( " Bad response from %s for name query - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
}
2005-02-10 06:22:47 +03:00
printf ( " refresh the name \n " ) ;
2005-02-11 09:58:07 +03:00
refresh . in . name = * name ;
2005-02-10 06:22:47 +03:00
refresh . in . wins_servers = str_list_make ( mem_ctx , address , NULL ) ;
refresh . in . addresses = str_list_make ( mem_ctx , myaddress , NULL ) ;
2005-02-14 06:39:25 +03:00
refresh . in . nb_flags = nb_flags ;
2005-02-10 06:22:47 +03:00
refresh . in . ttl = 12345 ;
status = nbt_name_refresh_wins ( nbtsock , mem_ctx , & refresh ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name refresh \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Bad response from %s for name refresh - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
2005-02-14 06:39:25 +03:00
CHECK_STRING ( refresh . out . wins_server , address ) ;
CHECK_VALUE ( refresh . out . rcode , 0 ) ;
2005-02-10 06:22:47 +03:00
printf ( " release the name \n " ) ;
2005-02-11 09:58:07 +03:00
release . in . name = * name ;
2005-02-10 06:22:47 +03:00
release . in . dest_addr = address ;
release . in . address = myaddress ;
2005-02-14 06:39:25 +03:00
release . in . nb_flags = nb_flags ;
2005-02-10 06:22:47 +03:00
release . in . broadcast = False ;
release . in . timeout = 3 ;
release . in . retries = 0 ;
status = nbt_name_release ( nbtsock , mem_ctx , & release ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name release \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Bad response from %s for name query - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
2005-02-11 09:58:07 +03:00
CHECK_NAME ( release . out . name , * name ) ;
2005-02-10 06:22:47 +03:00
CHECK_VALUE ( release . out . rcode , 0 ) ;
2005-02-11 09:58:07 +03:00
2005-02-10 06:22:47 +03:00
printf ( " release again \n " ) ;
status = nbt_name_release ( nbtsock , mem_ctx , & release ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " No response from %s for name release \n " , address ) ;
return False ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Bad response from %s for name query - %s \n " ,
address , nt_errstr ( status ) ) ;
return False ;
}
2005-02-11 09:58:07 +03:00
CHECK_NAME ( release . out . name , * name ) ;
2005-02-10 06:22:47 +03:00
CHECK_VALUE ( release . out . rcode , 0 ) ;
printf ( " query the name to make sure its gone \n " ) ;
2005-02-11 10:54:20 +03:00
query . in . name = * name ;
2005-02-10 06:22:47 +03:00
status = nbt_name_query ( nbtsock , mem_ctx , & query ) ;
2005-02-15 14:14:04 +03:00
if ( name - > type ! = NBT_NAME_LOGON & &
( nb_flags & NBT_NM_GROUP ) ) {
2005-02-14 06:39:25 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " ERROR: Name query failed after group release - %s \n " ,
nt_errstr ( status ) ) ;
return False ;
}
} else {
if ( NT_STATUS_IS_OK ( status ) ) {
printf ( " ERROR: Name query success after release \n " ) ;
return False ;
}
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ) {
printf ( " Incorrect response to name query - %s \n " , nt_errstr ( status ) ) ;
return False ;
}
2005-02-10 06:22:47 +03:00
}
return ret ;
}
2005-02-11 09:58:07 +03:00
/*
test operations against a WINS server
*/
static BOOL nbt_test_wins ( TALLOC_CTX * mem_ctx , const char * address )
{
struct nbt_name name ;
BOOL ret = True ;
2005-12-22 02:05:32 +03:00
uint32_t r = ( uint32_t ) ( random ( ) % ( 100000 ) ) ;
2005-02-11 09:58:07 +03:00
name . name = talloc_asprintf ( mem_ctx , " _TORTURE-%5u " , r ) ;
2005-12-31 11:42:01 +03:00
2005-02-11 09:58:07 +03:00
name . type = NBT_NAME_CLIENT ;
name . scope = NULL ;
2005-02-14 06:39:25 +03:00
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-14 15:46:03 +03:00
name . type = NBT_NAME_MASTER ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H | NBT_NM_GROUP ) ;
2005-12-31 11:42:01 +03:00
name . type = NBT_NAME_SERVER ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-15 14:14:04 +03:00
name . type = NBT_NAME_LOGON ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H | NBT_NM_GROUP ) ;
2005-12-22 02:05:32 +03:00
name . type = NBT_NAME_BROWSER ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H | NBT_NM_GROUP ) ;
2005-12-31 11:42:01 +03:00
name . type = NBT_NAME_PDC ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
name . type = 0xBF ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
name . type = 0xBE ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-14 06:39:25 +03:00
name . scope = " example " ;
name . type = 0x72 ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-11 09:58:07 +03:00
name . scope = " example " ;
name . type = 0x71 ;
2005-02-14 06:39:25 +03:00
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H | NBT_NM_GROUP ) ;
2005-02-11 09:58:07 +03:00
name . scope = " foo.example.com " ;
2005-02-14 06:39:25 +03:00
name . type = 0x72 ;
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-11 09:58:07 +03:00
name . name = talloc_asprintf ( mem_ctx , " _T \01 -%5u.foo " , r ) ;
2005-02-14 06:39:25 +03:00
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-11 09:58:07 +03:00
name . name = " " ;
2005-02-14 06:39:25 +03:00
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-11 09:58:07 +03:00
name . name = talloc_asprintf ( mem_ctx , " . " ) ;
2005-02-14 06:39:25 +03:00
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-11 09:58:07 +03:00
2005-02-11 10:54:20 +03:00
name . name = talloc_asprintf ( mem_ctx , " %5u- \377 \200 \300 FOO " , r ) ;
2005-02-14 06:39:25 +03:00
ret & = nbt_test_wins_name ( mem_ctx , address , & name , NBT_NODE_H ) ;
2005-02-11 10:54:20 +03:00
2005-02-11 09:58:07 +03:00
return ret ;
}
2005-02-10 06:22:47 +03:00
/*
test WINS operations
*/
BOOL torture_nbt_wins ( void )
{
const char * address ;
struct nbt_name name ;
TALLOC_CTX * mem_ctx = talloc_new ( NULL ) ;
NTSTATUS status ;
BOOL ret = True ;
2005-05-22 14:23:01 +04:00
make_nbt_name_server ( & name , lp_parm_string ( - 1 , " torture " , " host " ) ) ;
2005-02-10 06:22:47 +03:00
/* do an initial name resolution to find its IP */
2005-08-28 06:37:14 +04:00
status = resolve_name ( & name , mem_ctx , & address , NULL ) ;
2005-02-10 06:22:47 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " Failed to resolve %s - %s \n " ,
name . name , nt_errstr ( status ) ) ;
talloc_free ( mem_ctx ) ;
return False ;
}
2005-02-11 09:58:07 +03:00
ret & = nbt_test_wins ( mem_ctx , address ) ;
2005-02-10 06:22:47 +03:00
talloc_free ( mem_ctx ) ;
return ret ;
}