1998-08-30 19:58:17 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1998-08-30 19:58:17 +04:00
NBT netbios routines and daemon - version 2
Copyright ( C ) Andrew Tridgell 1994 - 1998
Copyright ( C ) Luke Kenneth Casson Leighton 1994 - 1998
Copyright ( C ) Jeremy Allison 1994 - 1998
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 .
*/
/* this file handles asynchronous browse synchronisation requests. The
requests are done by forking and putting the result in a file in the
locks directory . We do it this way because we don ' t want nmbd to be
blocked waiting for some server to respond on a TCP connection . This
also allows us to have more than 1 sync going at once ( tridge ) */
# include "includes.h"
struct sync_record {
struct sync_record * next , * prev ;
fstring workgroup ;
fstring server ;
pstring fname ;
struct in_addr ip ;
1999-12-13 16:27:58 +03:00
pid_t pid ;
1998-08-30 19:58:17 +04:00
} ;
/* a linked list of current sync connections */
static struct sync_record * syncs ;
2001-09-10 17:09:54 +04:00
static XFILE * fp ;
1998-08-30 19:58:17 +04:00
/*******************************************************************
This is the NetServerEnum callback .
2000-10-07 05:15:07 +04:00
Note sname and comment are in UNIX codepage format .
1998-08-30 19:58:17 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-01-04 14:35:55 +03:00
static void callback ( const char * sname , uint32 stype ,
const char * comment , void * state )
1998-08-30 19:58:17 +04:00
{
2001-09-10 17:09:54 +04:00
x_fprintf ( fp , " \" %s \" %08X \" %s \" \n " , sname , stype , comment ) ;
1998-08-30 19:58:17 +04:00
}
/*******************************************************************
Synchronise browse lists with another browse server .
Log in on the remote server ' s SMB port to their IPC $ service ,
do a NetServerEnum and record the results in fname
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void sync_child ( char * name , int nm_type ,
char * workgroup ,
struct in_addr ip , BOOL local , BOOL servers ,
char * fname )
{
extern fstring local_machine ;
2000-10-07 05:15:07 +04:00
fstring unix_workgroup ;
1998-08-30 19:58:17 +04:00
static struct cli_state cli ;
uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0 ;
1998-09-26 01:01:52 +04:00
struct nmb_name called , calling ;
1998-08-30 19:58:17 +04:00
2002-09-25 19:19:00 +04:00
/* W2K DMB's return empty browse lists on port 445. Use 139.
* Patch from Andy Levine andyl @ epicrealm . com .
*/
if ( ! cli_initialise ( & cli ) | | ! cli_set_port ( & cli , 139 ) | | ! cli_connect ( & cli , name , & ip ) ) {
1998-08-30 19:58:17 +04:00
return ;
}
2000-01-07 09:55:36 +03:00
make_nmb_name ( & calling , local_machine , 0x0 ) ;
make_nmb_name ( & called , name , nm_type ) ;
1998-09-26 01:01:52 +04:00
if ( ! cli_session_request ( & cli , & calling , & called ) )
{
1998-08-30 19:58:17 +04:00
cli_shutdown ( & cli ) ;
return ;
}
if ( ! cli_negprot ( & cli ) ) {
cli_shutdown ( & cli ) ;
return ;
}
1999-12-13 16:27:58 +03:00
if ( ! cli_session_setup ( & cli , " " , " " , 1 , " " , 0 , workgroup ) ) {
1998-08-30 19:58:17 +04:00
cli_shutdown ( & cli ) ;
return ;
}
if ( ! cli_send_tconX ( & cli , " IPC$ " , " IPC " , " " , 1 ) ) {
cli_shutdown ( & cli ) ;
return ;
}
2000-10-07 05:15:07 +04:00
/* All the cli_XX functions take UNIX character set. */
fstrcpy ( unix_workgroup , cli . server_domain ? cli . server_domain : workgroup ) ;
1998-08-30 19:58:17 +04:00
/* Fetch a workgroup list. */
2000-10-07 05:15:07 +04:00
cli_NetServerEnum ( & cli , unix_workgroup ,
2001-01-04 14:35:55 +03:00
local_type | SV_TYPE_DOMAIN_ENUM ,
callback , NULL ) ;
1998-08-30 19:58:17 +04:00
/* Now fetch a server list. */
if ( servers ) {
2000-10-07 05:15:07 +04:00
fstrcpy ( unix_workgroup , workgroup ) ;
cli_NetServerEnum ( & cli , unix_workgroup ,
1998-08-30 19:58:17 +04:00
local ? SV_TYPE_LOCAL_LIST_ONLY : SV_TYPE_ALL ,
2001-01-04 14:35:55 +03:00
callback , NULL ) ;
1998-08-30 19:58:17 +04:00
}
cli_shutdown ( & cli ) ;
}
/*******************************************************************
initialise a browse sync with another browse server . Log in on the
remote server ' s SMB port to their IPC $ service , do a NetServerEnum
and record the results
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void sync_browse_lists ( struct work_record * work ,
char * name , int nm_type ,
struct in_addr ip , BOOL local , BOOL servers )
{
struct sync_record * s ;
static int counter ;
2001-09-05 22:43:55 +04:00
START_PROFILE ( sync_browse_lists ) ;
1998-08-30 19:58:17 +04:00
/* Check we're not trying to sync with ourselves. This can
happen if we are a domain * and * a local master browser . */
if ( ismyip ( ip ) ) {
2001-09-05 22:43:55 +04:00
done :
END_PROFILE ( sync_browse_lists ) ;
1998-08-30 19:58:17 +04:00
return ;
}
s = ( struct sync_record * ) malloc ( sizeof ( * s ) ) ;
2001-09-05 22:43:55 +04:00
if ( ! s ) goto done ;
1998-08-30 19:58:17 +04:00
ZERO_STRUCTP ( s ) ;
fstrcpy ( s - > workgroup , work - > work_group ) ;
fstrcpy ( s - > server , name ) ;
s - > ip = ip ;
slprintf ( s - > fname , sizeof ( pstring ) - 1 ,
" %s/sync.%d " , lp_lockdir ( ) , counter + + ) ;
1999-12-13 16:27:58 +03:00
all_string_sub ( s - > fname , " // " , " / " , 0 ) ;
1998-08-30 19:58:17 +04:00
DLIST_ADD ( syncs , s ) ;
/* the parent forks and returns, leaving the child to do the
2001-09-05 22:43:55 +04:00
actual sync and call END_PROFILE */
1998-08-30 19:58:17 +04:00
CatchChild ( ) ;
2000-05-02 06:23:41 +04:00
if ( ( s - > pid = sys_fork ( ) ) ) return ;
1998-08-30 19:58:17 +04:00
1998-08-31 07:11:42 +04:00
BlockSignals ( False , SIGTERM ) ;
1998-08-30 19:58:17 +04:00
DEBUG ( 2 , ( " Initiating browse sync for %s to %s(%s) \n " ,
work - > work_group , name , inet_ntoa ( ip ) ) ) ;
2001-09-10 17:09:54 +04:00
fp = x_fopen ( s - > fname , O_WRONLY | O_CREAT | O_TRUNC , 0644 ) ;
2001-09-05 22:43:55 +04:00
if ( ! fp ) {
END_PROFILE ( sync_browse_lists ) ;
_exit ( 1 ) ;
}
1998-08-30 19:58:17 +04:00
sync_child ( name , nm_type , work - > work_group , ip , local , servers ,
s - > fname ) ;
2001-09-10 17:09:54 +04:00
x_fclose ( fp ) ;
2001-09-05 22:43:55 +04:00
END_PROFILE ( sync_browse_lists ) ;
1998-08-30 19:58:17 +04:00
_exit ( 0 ) ;
}
/**********************************************************************
handle one line from a completed sync file
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void complete_one ( struct sync_record * s ,
char * sname , uint32 stype , char * comment )
{
struct work_record * work ;
struct server_record * servrec ;
stype & = ~ SV_TYPE_LOCAL_LIST_ONLY ;
if ( stype & SV_TYPE_DOMAIN_ENUM ) {
/* See if we can find the workgroup on this subnet. */
if ( ( work = find_workgroup_on_subnet ( unicast_subnet , sname ) ) ) {
/* We already know about this workgroup -
update the ttl . */
update_workgroup_ttl ( work , lp_max_ttl ( ) ) ;
} else {
/* Create the workgroup on the subnet. */
work = create_workgroup_on_subnet ( unicast_subnet ,
sname , lp_max_ttl ( ) ) ;
if ( work ) {
/* remember who the master is */
fstrcpy ( work - > local_master_browser_name ,
comment ) ;
}
}
return ;
}
work = find_workgroup_on_subnet ( unicast_subnet , s - > workgroup ) ;
if ( ! work ) {
DEBUG ( 3 , ( " workgroup %s doesn't exist on unicast subnet? \n " ,
s - > workgroup ) ) ;
return ;
}
if ( ( servrec = find_server_in_workgroup ( work , sname ) ) ) {
/* Check that this is not a locally known
server - if so ignore the entry . */
if ( ! ( servrec - > serv . type & SV_TYPE_LOCAL_LIST_ONLY ) ) {
/* We already know about this server - update
the ttl . */
update_server_ttl ( servrec , lp_max_ttl ( ) ) ;
/* Update the type. */
servrec - > serv . type = stype ;
}
return ;
}
/* Create the server in the workgroup. */
create_server_on_workgroup ( work , sname , stype , lp_max_ttl ( ) , comment ) ;
}
/**********************************************************************
read the completed sync info
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void complete_sync ( struct sync_record * s )
{
2001-09-10 16:46:42 +04:00
XFILE * f ;
1998-08-30 19:58:17 +04:00
fstring server , type_str ;
unsigned type ;
pstring comment ;
pstring line ;
2002-11-13 02:20:50 +03:00
const char * ptr ;
1998-08-30 19:58:17 +04:00
int count = 0 ;
2001-09-10 16:46:42 +04:00
f = x_fopen ( s - > fname , O_RDONLY , 0 ) ;
1998-09-17 12:35:07 +04:00
if ( ! f ) return ;
1998-08-30 19:58:17 +04:00
2001-09-10 16:46:42 +04:00
while ( ! x_feof ( f ) ) {
1998-08-30 19:58:17 +04:00
if ( ! fgets_slash ( line , sizeof ( pstring ) , f ) ) continue ;
ptr = line ;
1998-08-31 07:11:42 +04:00
if ( ! next_token ( & ptr , server , NULL , sizeof ( server ) ) | |
! next_token ( & ptr , type_str , NULL , sizeof ( type_str ) ) | |
! next_token ( & ptr , comment , NULL , sizeof ( comment ) ) ) {
1998-08-30 19:58:17 +04:00
continue ;
}
sscanf ( type_str , " %X " , & type ) ;
complete_one ( s , server , type , comment ) ;
count + + ;
}
2001-09-10 16:46:42 +04:00
x_fclose ( f ) ;
1998-08-30 19:58:17 +04:00
unlink ( s - > fname ) ;
DEBUG ( 2 , ( " sync with %s(%s) for workgroup %s completed (%d records) \n " ,
s - > server , inet_ntoa ( s - > ip ) , s - > workgroup , count ) ) ;
}
/**********************************************************************
check for completion of any of the child processes
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void sync_check_completion ( void )
{
struct sync_record * s , * next ;
for ( s = syncs ; s ; s = next ) {
next = s - > next ;
if ( ! process_exists ( s - > pid ) ) {
/* it has completed - grab the info */
complete_sync ( s ) ;
DLIST_REMOVE ( syncs , s ) ;
ZERO_STRUCTP ( s ) ;
2001-09-17 08:35:51 +04:00
SAFE_FREE ( s ) ;
1998-08-30 19:58:17 +04:00
}
}
}