2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
26 04 jul 96: lkcl@pires.co.uk
27 created module namedbsubnet containing subnet database functions
35 extern int ClientDGRAM;
36 extern int global_nmb_port;
38 extern int DEBUGLEVEL;
40 extern struct in_addr wins_ip;
41 extern struct in_addr ipzero;
43 extern pstring myname;
44 extern fstring myworkgroup;
45 extern char **my_netbios_names;
47 BOOL updatedlists = True;
50 /* local interfaces structure */
51 extern struct interface *local_interfaces;
53 /* this is our domain/workgroup/server database */
54 struct subnet_record *subnetlist = NULL;
56 /* WINS subnet - keep this separate so enumeration code doesn't
57 run onto it by mistake. */
58 struct subnet_record *wins_subnet = NULL;
60 extern uint16 nb_type; /* samba's NetBIOS name type */
62 /****************************************************************************
63 add a domain into the list
64 **************************************************************************/
65 static void add_subnet(struct subnet_record *d)
67 struct subnet_record *d2;
77 for (d2 = subnetlist; d2->next; d2 = d2->next);
85 /****************************************************************************
86 find a subnet in the subnetlist - not including WINS.
87 **************************************************************************/
88 struct subnet_record *find_subnet(struct in_addr bcast_ip)
90 struct subnet_record *d;
92 /* search through subnet list for broadcast/netmask that matches
93 the source ip address. */
95 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
97 if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
105 /****************************************************************************
106 finds the appropriate subnet structure. directed packets (non-bcast) are
107 assumed to come from a point-to-point (P or M node), and so the subnet we
108 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
109 ****************************************************************************/
110 struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
114 /* identify the subnet the broadcast request came from */
115 return find_subnet(*iface_bcast(ip));
117 /* Return the subnet with the pseudo-ip of 255.255.255.255 */
121 /****************************************************************************
122 find a subnet in the subnetlist - if the subnet is not found
123 then return the WINS subnet.
124 **************************************************************************/
125 struct subnet_record *find_subnet_all(struct in_addr bcast_ip)
127 struct subnet_record *d = find_subnet(bcast_ip);
133 /****************************************************************************
134 create a subnet entry
135 ****************************************************************************/
136 static struct subnet_record *make_subnet(struct in_addr myip, struct in_addr bcast_ip,
137 struct in_addr mask_ip, BOOL add)
139 struct subnet_record *d = NULL;
140 int nmb_sock, dgram_sock;
142 /* Check if we are creating the WINS subnet - if so don't create
143 sockets, use the ClientNMB and ClientDGRAM sockets instead.
146 if(ip_equal(bcast_ip, wins_ip))
154 * Attempt to open the sockets on port 137/138 for this interface
156 * Fail the subnet creation if this fails.
159 if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr)) == -1)
161 DEBUG(0,("make_subnet: Failed to open nmb socket on interface %s \
162 for port %d. Error was %s\n", inet_ntoa(myip), global_nmb_port, strerror(errno)));
166 if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr)) == -1)
168 DEBUG(0,("make_subnet: Failed to open dgram socket on interface %s \
169 for port %d. Error was %s\n", inet_ntoa(myip), DGRAM_PORT, strerror(errno)));
173 /* Make sure we can broadcast from these sockets. */
174 set_socket_options(nmb_sock,"SO_BROADCAST");
175 set_socket_options(dgram_sock,"SO_BROADCAST");
179 d = (struct subnet_record *)malloc(sizeof(*d));
183 DEBUG(0,("make_subnet: malloc fail !\n"));
189 bzero((char *)d,sizeof(*d));
191 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
192 DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
194 d->bcast_ip = bcast_ip;
195 d->mask_ip = mask_ip;
197 d->nmb_sock = nmb_sock;
198 d->dgram_sock = dgram_sock;
199 d->workgrouplist = NULL;
207 /****************************************************************************
208 add a domain entry. creates a workgroup, if necessary, and adds the domain
209 to the named a workgroup.
210 ****************************************************************************/
211 static struct subnet_record *add_subnet_entry(struct in_addr myip,
212 struct in_addr bcast_ip,
213 struct in_addr mask_ip, char *name,
214 BOOL create_subnets, BOOL add)
216 struct subnet_record *d = NULL;
218 if (zero_ip(bcast_ip))
219 bcast_ip = *iface_bcast(bcast_ip);
221 /* Note that we should also add into the WINS subnet as add_subnet_entry
222 should be called to add NetBIOS names and server entries on all
223 interfaces, including the WINS interface
226 if(create_subnets == True)
228 /* Create new subnets. */
229 if((d = make_subnet(myip, bcast_ip, mask_ip, add)) == NULL)
231 DEBUG(0,("add_subnet_entry: Unable to create subnet %s\n",
232 inet_ntoa(bcast_ip) ));
237 if(ip_equal(bcast_ip, wins_ip))
239 return find_subnet(bcast_ip);
242 /****************************************************************************
243 Add a workgroup into a subnet, and if it's our primary workgroup,
244 add the required names to it.
245 **************************************************************************/
247 void add_workgroup_to_subnet( struct subnet_record *d, char *group)
249 struct work_record *w = NULL;
251 DEBUG(5,("add_workgroup_to_subnet: Adding workgroup %s to subnet %s\n",
252 group, inet_ntoa(d->bcast_ip)));
254 /* This next statement creates the workgroup struct if it doesn't
257 if((w = find_workgroupstruct(d, group, True)) == NULL)
259 DEBUG(0,("add_workgroup_to_subnet: Unable to add workgroup %s to subnet %s\n",
260 group, inet_ntoa(d->bcast_ip) ));
264 /* add WORKGROUP(00) entries into name database
265 or register with WINS server, if it's our workgroup.
267 if (strequal(myworkgroup, group))
271 add_my_name_entry(d,group,0x0 ,nb_type|NB_ACTIVE|NB_GROUP);
273 /* Only register the WORKGROUP<0x1e> name if we could be a local master
275 if(lp_local_master())
276 add_my_name_entry(d,group,0x1e,nb_type|NB_ACTIVE|NB_GROUP);
278 /* Add all our server names to the workgroup list. We remove any
279 browser or logon server flags from all but the primary name.
281 for( n = 0; my_netbios_names[n]; n++)
283 char *name = my_netbios_names[n];
284 int stype = w->ServerType;
286 if(!strequal(myname, name))
287 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|
288 SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
290 add_server_entry(d,w,name,stype|SV_TYPE_LOCAL_LIST_ONLY,0,
291 lp_serverstring(),True);
292 DEBUG(3,("add_workgroup_to_subnet: Added server name entry %s \
293 to subnet %s\n", name, inet_ntoa(d->bcast_ip)));
298 /****************************************************************************
299 create subnet / workgroup / server entries
301 - add or create the subnet lists
302 - add or create the workgroup entries in each subnet entry
303 - register appropriate NetBIOS names for the workgroup entries
305 **************************************************************************/
306 void add_my_subnets(char *group)
308 static BOOL create_subnets = True;
309 struct subnet_record *d = NULL;
310 struct interface *i = NULL;
312 if (*group == '*') return;
314 /* Create subnets from all the local interfaces and thread them onto
317 for (i = local_interfaces; i; i = i->next)
319 add_subnet_entry(i->ip, i->bcast,i->nmask,group, create_subnets, True);
322 /* If we are using WINS, then we must add the workgroup to the WINS
323 subnet. This is used as a place to keep collated server lists.
326 /* Create the WINS subnet if we are using WINS - but don't thread it
327 onto the linked subnet list.
329 if (lp_wins_support() || lp_wins_server())
331 struct in_addr wins_nmask = ipzero;
332 wins_subnet = add_subnet_entry(ipzero, wins_ip, wins_nmask, group, create_subnets, False);
335 /* Ensure we only create the subnets once. */
336 create_subnets = False;
338 /* Now we have created all the subnets - we can add the names
339 that make us a client member in the workgroup.
341 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
342 add_workgroup_to_subnet(d, group);
345 /*******************************************************************
347 ******************************************************************/
348 void write_browse_list(time_t t)
350 struct subnet_record *d;
351 pstring fname,fnamenew;
354 static time_t lasttime = 0;
356 if (!lasttime) lasttime = t;
357 if (!updatedlists || t - lasttime < 5) return;
360 updatedlists = False;
366 pstrcpy(fname,lp_lockdir());
367 trim_string(fname,NULL,"/");
369 strcat(fname,SERVER_LIST);
370 pstrcpy(fnamenew,fname);
371 strcat(fnamenew,".");
373 f = fopen(fnamenew,"w");
377 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
381 for (d = FIRST_SUBNET; d ; d = NEXT_SUBNET_INCLUDING_WINS(d))
383 struct work_record *work;
384 for (work = d->workgrouplist; work ; work = work->next)
386 struct server_record *s;
387 for (s = work->serverlist; s ; s = s->next)
391 /* don't list domains I don't have a master for */
392 if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
397 /* output server details, plus what workgroup/domain
398 they're in. without the domain information, the
399 combined list of all servers in all workgroups gets
400 sent to anyone asking about any workgroup! */
402 sprintf(tmp, "\"%s\"", s->serv.name);
403 fprintf(f, "%-25s ", tmp);
404 fprintf(f, "%08x ", s->serv.type);
405 sprintf(tmp, "\"%s\" ", s->serv.comment);
406 fprintf(f, "%-30s", tmp);
407 fprintf(f, "\"%s\"\n", work->work_group);
414 chmod(fnamenew,0644);
415 rename(fnamenew,fname);
416 DEBUG(3,("Wrote browse list %s\n",fname));