summaryrefslogtreecommitdiffstats
path: root/source/nmbsync.c
blob: 97e7e02b2b9271c6823252ed93feab48363515f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NBT netbios routines to synchronise browse lists
   Copyright (C) Andrew Tridgell 1994-1997
   
   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 02139, USA.
   
*/

#include "includes.h"

extern int ClientNMB;
extern int ClientDGRAM;

extern int DEBUGLEVEL;

extern pstring myname;

extern int name_type;
extern int max_protocol;
extern struct in_addr dest_ip;
extern int pid;
extern int gid;
extern int uid;
extern int mid;
extern BOOL got_pass;
extern BOOL have_ip;
extern pstring workgroup;
extern pstring service;
extern pstring desthost;
extern BOOL connect_as_ipc;

/****************************************************************************
fudge for getpass function
****************************************************************************/
char *getsmbpass(char *pass)
{
	return "dummy"; /* return anything: it should be ignored anyway */
}

/****************************************************************************
adds information retrieved from a NetServerEnum call
****************************************************************************/
static BOOL add_info(struct subnet_record *d, struct work_record *work, int servertype)
{
  char *rparam = NULL;
  char *rdata = NULL;
  int rdrcnt,rprcnt;
  char *p;
  pstring param;
  int uLevel = 1;
  int count = -1;
  
  /* now send a SMBtrans command with api ServerEnum? */
  p = param;
  SSVAL(p,0,0x68); /* api number */
  p += 2;
  strcpy(p,"WrLehDz");
  p = skip_string(p,1);
  
  strcpy(p,"B16BBDz");
  
  p = skip_string(p,1);
  SSVAL(p,0,uLevel);
  SSVAL(p,2,0x2000); /* buf length */
  p += 4;
  SIVAL(p,0,servertype);
  p += 4;
  
  strcpy(p, work->work_group);
  p = skip_string(p,1);
  
  if (cli_call_api(PTR_DIFF(p,param),0, 8,10000,
		   &rprcnt,&rdrcnt, param,NULL,
		   &rparam,&rdata))
    {
      int res = SVAL(rparam,0);
      int converter=SVAL(rparam,2);
      int i;
      
      if (res == 0)
	{
	  count=SVAL(rparam,4);
	  p = rdata;
	  
	  for (i = 0;i < count;i++, p += 26)
	    {
	      char *sname = p;
	      uint32 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
	      int comment_offset = IVAL(p,22) & 0xFFFF;
	      char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
	      
	      struct work_record *w = work;
	      
	      DEBUG(4, ("\t%-16.16s     %08x    %s\n", sname, stype, cmnt));
	      
	      if (stype & SV_TYPE_DOMAIN_ENUM)
		{
		  /* creates workgroup on remote subnet */
		  if ((w = find_workgroupstruct(d,sname,True)))
		    {
		      announce_request(w, d->bcast_ip);
		    }
		}
	      
          if (w)
	        add_server_entry(d,w,sname,stype,lp_max_ttl(),cmnt,False);
	    }
	}
    }
  
  if (rparam) free(rparam);
  if (rdata) free(rdata);
  
  return(True);
}


/*******************************************************************
  synchronise browse lists with another browse server.

  log in on the remote server's SMB port to their IPC$ service,
  do a NetServerEnum and update our server and workgroup databases.
  ******************************************************************/
void sync_browse_lists(struct subnet_record *d, struct work_record *work,
		char *name, int nm_type, struct in_addr ip, BOOL local)
{
  uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;

  if (!d || !work ) return;

  if(d != wins_subnet) {
      DEBUG(0,
        ("sync_browse_lists: ERROR sync requested on non-WINS subnet.\n"));
      return;
  }

  pid = getpid();
  uid = getuid();
  gid = getgid();
  mid = pid + 100;
  name_type = nm_type;
  
  got_pass = True;
  
  DEBUG(0,("sync_browse_lists: Sync browse lists with %s for %s %s\n",
	    name, work->work_group, inet_ntoa(ip)));
  
  strcpy(workgroup,work->work_group);
  strcpy(desthost,name);
  dest_ip = ip;
  
  if (zero_ip(dest_ip)) return;
  have_ip = True;
  
  connect_as_ipc = True;
  
  /* connect as server and get domains, then servers */
  
  sprintf(service,"\\\\%s\\IPC$", name);
  strupper(service);
  
  if (cli_open_sockets(SMB_PORT))
    {
      if (cli_send_login(NULL,NULL,True,True))
	{
	  add_info(d, work, local_type|SV_TYPE_DOMAIN_ENUM);
	  add_info(d, work, local_type|(SV_TYPE_ALL&
                      ~(SV_TYPE_DOMAIN_ENUM|SV_TYPE_LOCAL_LIST_ONLY)));
	}
      
      close_sockets();
    }
}