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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1998-08-30 19:58:17 +04:00
( 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
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1998-08-30 19:58:17 +04:00
*/
/* 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"
2005-04-06 20:28:04 +04:00
extern fstring local_machine ;
1998-08-30 19:58:17 +04:00
struct sync_record {
struct sync_record * next , * prev ;
2004-03-16 00:45:45 +03:00
unstring workgroup ;
unstring server ;
1998-08-30 19:58:17 +04:00
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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-27 05:25:01 +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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-27 05:25:01 +04:00
1998-08-30 19:58:17 +04:00
static void sync_child ( char * name , int nm_type ,
char * workgroup ,
2007-10-19 04:40:25 +04:00
struct in_addr ip , bool local , bool servers ,
1998-08-30 19:58:17 +04:00
char * fname )
{
2000-10-07 05:15:07 +04:00
fstring unix_workgroup ;
2006-07-11 22:01:26 +04:00
struct cli_state * cli ;
1998-08-30 19:58:17 +04:00
uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0 ;
1998-09-26 01:01:52 +04:00
struct nmb_name called , calling ;
2007-06-20 21:38:42 +04:00
NTSTATUS status ;
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 .
*/
2006-07-11 22:01:26 +04:00
cli = cli_initialise ( ) ;
if ( ! cli ) {
return ;
}
2007-06-20 21:38:42 +04:00
if ( ! cli_set_port ( cli , 139 ) ) {
return ;
}
status = cli_connect ( cli , name , & ip ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
1998-08-30 19:58:17 +04:00
return ;
}
2000-01-07 09:55:36 +03:00
make_nmb_name ( & calling , local_machine , 0x0 ) ;
2003-08-27 05:25:01 +04:00
make_nmb_name ( & called , name , nm_type ) ;
1998-09-26 01:01:52 +04:00
2006-07-11 22:01:26 +04:00
if ( ! cli_session_request ( cli , & calling , & called ) ) {
cli_shutdown ( cli ) ;
1998-08-30 19:58:17 +04:00
return ;
}
2006-07-11 22:01:26 +04:00
if ( ! cli_negprot ( cli ) ) {
cli_shutdown ( cli ) ;
1998-08-30 19:58:17 +04:00
return ;
}
2006-08-16 21:14:16 +04:00
if ( ! NT_STATUS_IS_OK ( cli_session_setup ( cli , " " , " " , 1 , " " , 0 ,
workgroup ) ) ) {
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
1998-08-30 19:58:17 +04:00
return ;
}
2006-07-11 22:01:26 +04:00
if ( ! cli_send_tconX ( cli , " IPC$ " , " IPC " , " " , 1 ) ) {
cli_shutdown ( cli ) ;
1998-08-30 19:58:17 +04:00
return ;
}
2000-10-07 05:15:07 +04:00
/* All the cli_XX functions take UNIX character set. */
2006-07-11 22:01:26 +04:00
fstrcpy ( unix_workgroup , cli - > server_domain ? cli - > server_domain : workgroup ) ;
2000-10-07 05:15:07 +04:00
1998-08-30 19:58:17 +04:00
/* Fetch a workgroup list. */
2006-07-11 22:01:26 +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 ) ;
2006-07-11 22:01:26 +04:00
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
}
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
1998-08-30 19:58:17 +04:00
}
/*******************************************************************
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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-27 05:25:01 +04:00
1998-08-30 19:58:17 +04:00
void sync_browse_lists ( struct work_record * work ,
char * name , int nm_type ,
2007-10-19 04:40:25 +04:00
struct in_addr ip , bool local , bool servers )
1998-08-30 19:58:17 +04:00
{
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 . */
2007-10-11 05:25:16 +04:00
if ( ismyip_v4 ( ip ) ) {
2001-09-05 22:43:55 +04:00
done :
END_PROFILE ( sync_browse_lists ) ;
1998-08-30 19:58:17 +04:00
return ;
}
2004-12-07 21:25:53 +03:00
s = SMB_MALLOC_P ( struct sync_record ) ;
2001-09-05 22:43:55 +04:00
if ( ! s ) goto done ;
1998-08-30 19:58:17 +04:00
ZERO_STRUCTP ( s ) ;
2004-03-16 00:45:45 +03:00
unstrcpy ( s - > workgroup , work - > work_group ) ;
unstrcpy ( s - > server , name ) ;
1998-08-30 19:58:17 +04:00
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 ) ;
}
/**********************************************************************
2003-08-27 05:25:01 +04:00
Handle one line from a completed sync file .
1998-08-30 19:58:17 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-27 05:25:01 +04:00
1998-08-30 19:58:17 +04:00
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 */
2004-03-16 00:45:45 +03:00
unstrcpy ( work - > local_master_browser_name , comment ) ;
1998-08-30 19:58:17 +04:00
}
}
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 ) ;
}
/**********************************************************************
2003-08-27 05:25:01 +04:00
Read the completed sync info .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-08-30 19:58:17 +04:00
static void complete_sync ( struct sync_record * s )
{
2001-09-10 16:46:42 +04:00
XFILE * f ;
2004-03-16 00:45:45 +03:00
unstring server , type_str ;
1998-08-30 19:58:17 +04:00
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
2003-08-27 05:25:01 +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
2003-08-27 05:25:01 +04:00
if ( ! fgets_slash ( line , sizeof ( pstring ) , f ) )
continue ;
1998-08-30 19:58:17 +04:00
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 ) ) ;
}
/**********************************************************************
2003-08-27 05:25:01 +04:00
Check for completion of any of the child processes .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-08-30 19:58:17 +04:00
void sync_check_completion ( void )
{
struct sync_record * s , * next ;
for ( s = syncs ; s ; s = next ) {
next = s - > next ;
2005-09-30 21:13:37 +04:00
if ( ! process_exists_by_pid ( s - > pid ) ) {
1998-08-30 19:58:17 +04:00
/* 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
}
}
}