2000-01-03 06:24:23 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-01-03 06:24:23 +03:00
handle unexpected packets
Copyright ( C ) Andrew Tridgell 2000
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"
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
static TDB_CONTEXT * tdbd = NULL ;
2000-01-03 06:24:23 +03:00
/* the key type used in the unexpeceted packet database */
struct unexpected_key {
enum packet_type packet_type ;
time_t timestamp ;
int count ;
} ;
/****************************************************************************
all unexpected packets are passed in here , to be stored in a unexpected
packet database . This allows nmblookup and other tools to receive packets
erroneoously sent to the wrong port by broken MS systems
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void unexpected_packet ( struct packet_struct * p )
{
static int count ;
TDB_DATA kbuf , dbuf ;
struct unexpected_key key ;
char buf [ 1024 ] ;
int len = 0 ;
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
if ( ! tdbd ) {
2002-07-15 14:35:28 +04:00
tdbd = tdb_open_log ( lock_path ( " unexpected.tdb " ) , 0 ,
2001-09-07 02:08:19 +04:00
TDB_CLEAR_IF_FIRST | TDB_DEFAULT ,
2000-01-03 06:24:23 +03:00
O_RDWR | O_CREAT , 0644 ) ;
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
if ( ! tdbd ) {
2000-01-03 06:24:23 +03:00
DEBUG ( 0 , ( " Failed to open unexpected.tdb \n " ) ) ;
return ;
}
}
memset ( buf , ' \0 ' , sizeof ( buf ) ) ;
len = build_packet ( buf , p ) ;
key . packet_type = p - > packet_type ;
key . timestamp = p - > timestamp ;
key . count = count + + ;
kbuf . dptr = ( char * ) & key ;
kbuf . dsize = sizeof ( key ) ;
dbuf . dptr = buf ;
dbuf . dsize = len ;
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
tdb_store ( tdbd , kbuf , dbuf , TDB_REPLACE ) ;
2000-01-03 06:24:23 +03:00
}
static time_t lastt ;
/****************************************************************************
delete the record if it is too old
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
static int traverse_fn ( TDB_CONTEXT * ttdb , TDB_DATA kbuf , TDB_DATA dbuf , void * state )
2000-01-03 06:24:23 +03:00
{
struct unexpected_key key ;
memcpy ( & key , kbuf . dptr , sizeof ( key ) ) ;
if ( lastt - key . timestamp > NMBD_UNEXPECTED_TIMEOUT ) {
tdb_delete ( ttdb , kbuf ) ;
}
return 0 ;
}
/****************************************************************************
delete all old unexpected packets
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void clear_unexpected ( time_t t )
{
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
if ( ! tdbd ) return ;
2000-01-03 06:24:23 +03:00
if ( ( lastt ! = 0 ) & & ( t < lastt + NMBD_UNEXPECTED_TIMEOUT ) )
return ;
lastt = t ;
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
tdb_traverse ( tdbd , traverse_fn , NULL ) ;
2000-01-03 06:24:23 +03:00
}
static struct packet_struct * matched_packet ;
2000-01-03 09:30:50 +03:00
static int match_id ;
static enum packet_type match_type ;
2003-01-03 11:28:12 +03:00
static const char * match_name ;
2000-01-03 06:24:23 +03:00
/****************************************************************************
tdb traversal fn to find a matching 137 packet
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
static int traverse_match ( TDB_CONTEXT * ttdb , TDB_DATA kbuf , TDB_DATA dbuf , void * state )
2000-01-03 06:24:23 +03:00
{
struct unexpected_key key ;
struct packet_struct * p ;
memcpy ( & key , kbuf . dptr , sizeof ( key ) ) ;
2000-01-03 09:30:50 +03:00
if ( key . packet_type ! = match_type ) return 0 ;
2000-01-03 06:24:23 +03:00
2000-01-03 09:30:50 +03:00
p = parse_packet ( dbuf . dptr , dbuf . dsize , match_type ) ;
2000-01-03 06:24:23 +03:00
2000-01-03 09:30:50 +03:00
if ( ( match_type = = NMB_PACKET & &
p - > packet . nmb . header . name_trn_id = = match_id ) | |
( match_type = = DGRAM_PACKET & &
match_mailslot_name ( p , match_name ) ) ) {
2000-01-03 06:24:23 +03:00
matched_packet = p ;
return - 1 ;
}
free_packet ( p ) ;
return 0 ;
}
/****************************************************************************
check for a particular packet in the unexpected packet queue
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-01-03 09:30:50 +03:00
struct packet_struct * receive_unexpected ( enum packet_type packet_type , int id ,
2003-01-03 11:28:12 +03:00
const char * mailslot_name )
2000-01-03 06:24:23 +03:00
{
TDB_CONTEXT * tdb2 ;
2001-09-06 09:45:07 +04:00
tdb2 = tdb_open_log ( lock_path ( " unexpected.tdb " ) , 0 , 0 , O_RDONLY , 0 ) ;
2000-01-03 06:24:23 +03:00
if ( ! tdb2 ) return NULL ;
matched_packet = NULL ;
2000-01-03 09:30:50 +03:00
match_id = id ;
match_type = packet_type ;
match_name = mailslot_name ;
2000-01-03 06:24:23 +03:00
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
tdb_traverse ( tdb2 , traverse_match , NULL ) ;
2000-01-03 06:24:23 +03:00
tdb_close ( tdb2 ) ;
return matched_packet ;
}