#include "includes.h"
extern fstring local_machine;
struct sync_record {
struct sync_record *next, *prev;
unstring workgroup;
unstring server;
pstring fname;
struct in_addr ip;
pid_t pid;
};
static struct sync_record *syncs;
static XFILE *fp;
static void callback(const char *sname, uint32 stype,
const char *comment, void *state)
{
x_fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
}
static void sync_child(char *name, int nm_type,
char *workgroup,
struct in_addr ip, BOOL local, BOOL servers,
char *fname)
{
fstring unix_workgroup;
struct cli_state *cli;
uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
struct nmb_name called, calling;
NTSTATUS status;
cli = cli_initialise();
if (!cli) {
return;
}
if (!cli_set_port(cli, 139)) {
return;
}
status = cli_connect(cli, name, &ip);
if (!NT_STATUS_IS_OK(status)) {
return;
}
make_nmb_name(&calling, local_machine, 0x0);
make_nmb_name(&called , name, nm_type);
if (!cli_session_request(cli, &calling, &called)) {
cli_shutdown(cli);
return;
}
if (!cli_negprot(cli)) {
cli_shutdown(cli);
return;
}
if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 1, "", 0,
workgroup))) {
cli_shutdown(cli);
return;
}
if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
cli_shutdown(cli);
return;
}
fstrcpy(unix_workgroup, cli->server_domain ? cli->server_domain : workgroup);
cli_NetServerEnum(cli, unix_workgroup,
local_type|SV_TYPE_DOMAIN_ENUM,
callback, NULL);
if (servers) {
fstrcpy(unix_workgroup, workgroup);
cli_NetServerEnum(cli, unix_workgroup,
local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
callback, NULL);
}
cli_shutdown(cli);
}
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;
START_PROFILE(sync_browse_lists);
if (ismyip(ip)) {
done:
END_PROFILE(sync_browse_lists);
return;
}
s = SMB_MALLOC_P(struct sync_record);
if (!s) goto done;
ZERO_STRUCTP(s);
unstrcpy(s->workgroup, work->work_group);
unstrcpy(s->server, name);
s->ip = ip;
slprintf(s->fname, sizeof(pstring)-1,
"%s/sync.%d", lp_lockdir(), counter++);
all_string_sub(s->fname,"//", "/", 0);
DLIST_ADD(syncs, s);
CatchChild();
if ((s->pid = sys_fork())) return;
BlockSignals( False, SIGTERM );
DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
work->work_group, name, inet_ntoa(ip)));
fp = x_fopen(s->fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (!fp) {
END_PROFILE(sync_browse_lists);
_exit(1);
}
sync_child(name, nm_type, work->work_group, ip, local, servers,
s->fname);
x_fclose(fp);
END_PROFILE(sync_browse_lists);
_exit(0);
}
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) {
if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
update_workgroup_ttl(work,lp_max_ttl());
} else {
work = create_workgroup_on_subnet(unicast_subnet,
sname, lp_max_ttl());
if (work) {
unstrcpy(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))) {
if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
update_server_ttl(servrec, lp_max_ttl());
servrec->serv.type = stype;
}
return;
}
create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
}
static void complete_sync(struct sync_record *s)
{
XFILE *f;
unstring server, type_str;
unsigned type;
pstring comment;
pstring line;
const char *ptr;
int count=0;
f = x_fopen(s->fname,O_RDONLY, 0);
if (!f)
return;
while (!x_feof(f)) {
if (!fgets_slash(line,sizeof(pstring),f))
continue;
ptr = line;
if (!next_token(&ptr,server,NULL,sizeof(server)) ||
!next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
!next_token(&ptr,comment,NULL, sizeof(comment))) {
continue;
}
sscanf(type_str, "%X", &type);
complete_one(s, server, type, comment);
count++;
}
x_fclose(f);
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));
}
void sync_check_completion(void)
{
struct sync_record *s, *next;
for (s=syncs;s;s=next) {
next = s->next;
if (!process_exists_by_pid(s->pid)) {
complete_sync(s);
DLIST_REMOVE(syncs, s);
ZERO_STRUCTP(s);
SAFE_FREE(s);
}
}
}