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;
37 extern int DEBUGLEVEL;
39 extern struct in_addr wins_ip;
40 extern struct in_addr ipzero;
42 extern pstring myname;
43 extern fstring myworkgroup;
44 extern char **my_netbios_names;
46 BOOL updatedlists = True;
49 /* local interfaces structure */
50 extern struct interface *local_interfaces;
52 /* this is our domain/workgroup/server database */
53 struct subnet_record *subnetlist = NULL;
55 /* WINS subnet - keep this separate so enumeration code doesn't
56 run onto it by mistake. */
57 struct subnet_record *wins_subnet = NULL;
59 extern uint16 nb_type; /* samba's NetBIOS name type */
61 /****************************************************************************
62 add a domain into the list
63 **************************************************************************/
64 static void add_subnet(struct subnet_record *d)
66 struct subnet_record *d2;
76 for (d2 = subnetlist; d2->next; d2 = d2->next);
84 /****************************************************************************
85 find a subnet in the subnetlist - not including WINS.
86 **************************************************************************/
87 struct subnet_record *find_subnet(struct in_addr bcast_ip)
89 struct subnet_record *d;
91 /* search through subnet list for broadcast/netmask that matches
92 the source ip address. */
94 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
96 if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
104 /****************************************************************************
105 finds the appropriate subnet structure. directed packets (non-bcast) are
106 assumed to come from a point-to-point (P or M node), and so the subnet we
107 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
108 ****************************************************************************/
109 struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
113 /* identify the subnet the broadcast request came from */
114 return find_subnet(*iface_bcast(ip));
116 /* Return the subnet with the pseudo-ip of 255.255.255.255 */
120 /****************************************************************************
121 find a subnet in the subnetlist - if the subnet is not found
122 then return the WINS subnet.
123 **************************************************************************/
124 struct subnet_record *find_subnet_all(struct in_addr bcast_ip)
126 struct subnet_record *d = find_subnet(bcast_ip);
132 /****************************************************************************
133 create a domain entry
134 ****************************************************************************/
135 static struct subnet_record *make_subnet(struct in_addr bcast_ip, struct in_addr mask_ip, BOOL add)
137 struct subnet_record *d;
138 d = (struct subnet_record *)malloc(sizeof(*d));
140 if (!d) return(NULL);
142 bzero((char *)d,sizeof(*d));
144 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
145 DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
147 d->bcast_ip = bcast_ip;
148 d->mask_ip = mask_ip;
149 d->workgrouplist = NULL;
157 /****************************************************************************
158 add a domain entry. creates a workgroup, if necessary, and adds the domain
159 to the named a workgroup.
160 ****************************************************************************/
161 static struct subnet_record *add_subnet_entry(struct in_addr bcast_ip,
162 struct in_addr mask_ip, char *name,
163 BOOL create_subnets, BOOL add)
165 struct subnet_record *d = NULL;
167 if (zero_ip(bcast_ip))
168 bcast_ip = *iface_bcast(bcast_ip);
170 /* Note that we should also add into the WINS subnet as add_subnet_entry
171 should be called to add NetBIOS names and server entries on all
172 interfaces, including the WINS interface
175 if(create_subnets == True)
177 /* Create new subnets. */
178 if((d = make_subnet(bcast_ip, mask_ip, add)) == NULL)
180 DEBUG(0,("add_subnet_entry: Unable to create subnet %s\n",
181 inet_ntoa(bcast_ip) ));
186 if(ip_equal(bcast_ip, wins_ip))
188 return find_subnet(bcast_ip);
191 /****************************************************************************
192 Add a workgroup into a subnet, and if it's our primary workgroup,
193 add the required names to it.
194 **************************************************************************/
196 void add_workgroup_to_subnet( struct subnet_record *d, char *group)
198 struct work_record *w = NULL;
200 DEBUG(5,("add_workgroup_to_subnet: Adding workgroup %s to subnet %s\n",
201 group, inet_ntoa(d->bcast_ip)));
203 /* This next statement creates the workgroup struct if it doesn't
206 if((w = find_workgroupstruct(d, group, True)) == NULL)
208 DEBUG(0,("add_workgroup_to_subnet: Unable to add workgroup %s to subnet %s\n",
209 group, inet_ntoa(d->bcast_ip) ));
213 /* add WORKGROUP(00) entries into name database
214 or register with WINS server, if it's our workgroup.
216 if (strequal(myworkgroup, group))
220 add_my_name_entry(d,group,0x0 ,nb_type|NB_ACTIVE|NB_GROUP);
222 /* Only register the WORKGROUP<0x1e> name if we could be a local master
224 if(lp_local_master())
225 add_my_name_entry(d,group,0x1e,nb_type|NB_ACTIVE|NB_GROUP);
227 /* Add all our server names to the workgroup list. We remove any
228 browser or logon server flags from all but the primary name.
230 for( n = 0; my_netbios_names[n]; n++)
232 char *name = my_netbios_names[n];
233 int stype = w->ServerType;
235 if(!strequal(myname, name))
236 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|
237 SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
239 add_server_entry(d,w,name,stype|SV_TYPE_LOCAL_LIST_ONLY,0,
240 lp_serverstring(),True);
241 DEBUG(3,("add_workgroup_to_subnet: Added server name entry %s \
242 to subnet %s\n", name, inet_ntoa(d->bcast_ip)));
247 /****************************************************************************
248 create subnet / workgroup / server entries
250 - add or create the subnet lists
251 - add or create the workgroup entries in each subnet entry
252 - register appropriate NetBIOS names for the workgroup entries
254 **************************************************************************/
255 void add_my_subnets(char *group)
257 static BOOL create_subnets = True;
258 struct subnet_record *d = NULL;
259 struct interface *i = NULL;
261 if (*group == '*') return;
263 /* Create subnets from all the local interfaces and thread them onto
266 for (i = local_interfaces; i; i = i->next)
268 add_subnet_entry(i->bcast,i->nmask,group, create_subnets, True);
271 /* If we are using WINS, then we must add the workgroup to the WINS
272 subnet. This is used as a place to keep collated server lists.
275 /* Create the WINS subnet if we are using WINS - but don't thread it
276 onto the linked subnet list.
278 if (lp_wins_support() || lp_wins_server())
280 struct in_addr wins_nmask = ipzero;
281 wins_subnet = add_subnet_entry(wins_ip, wins_nmask, group, create_subnets, False);
284 /* Ensure we only create the subnets once. */
285 create_subnets = False;
287 /* Now we have created all the subnets - we can add the names
288 that make us a client member in the workgroup.
290 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
291 add_workgroup_to_subnet(d, group);
294 /*******************************************************************
296 ******************************************************************/
297 void write_browse_list(time_t t)
299 struct subnet_record *d;
300 pstring fname,fnamenew;
303 static time_t lasttime = 0;
305 if (!lasttime) lasttime = t;
306 if (!updatedlists || t - lasttime < 5) return;
309 updatedlists = False;
315 pstrcpy(fname,lp_lockdir());
316 trim_string(fname,NULL,"/");
318 strcat(fname,SERVER_LIST);
319 pstrcpy(fnamenew,fname);
320 strcat(fnamenew,".");
322 f = fopen(fnamenew,"w");
326 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
330 for (d = FIRST_SUBNET; d ; d = NEXT_SUBNET_INCLUDING_WINS(d))
332 struct work_record *work;
333 for (work = d->workgrouplist; work ; work = work->next)
335 struct server_record *s;
336 for (s = work->serverlist; s ; s = s->next)
340 /* don't list domains I don't have a master for */
341 if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
346 /* output server details, plus what workgroup/domain
347 they're in. without the domain information, the
348 combined list of all servers in all workgroups gets
349 sent to anyone asking about any workgroup! */
351 sprintf(tmp, "\"%s\"", s->serv.name);
352 fprintf(f, "%-25s ", tmp);
353 fprintf(f, "%08x ", s->serv.type);
354 sprintf(tmp, "\"%s\" ", s->serv.comment);
355 fprintf(f, "%-30s", tmp);
356 fprintf(f, "\"%s\"\n", work->work_group);
363 chmod(fnamenew,0644);
364 rename(fnamenew,fname);
365 DEBUG(3,("Wrote browse list %s\n",fname));