2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1996
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 namedbwork containing workgroup database functions
29 30 July 96: David.Chappell@mail.trincoll.edu
30 Expanded multiple workgroup domain master browser support.
39 extern int DEBUGLEVEL;
41 /* this is our domain/workgroup/server database */
42 extern struct subnet_record *subnetlist;
44 extern struct in_addr ipgrp;
46 extern pstring myname;
48 /****************************************************************************
49 add a workgroup into the domain list
50 **************************************************************************/
51 static void add_workgroup(struct work_record *work, struct subnet_record *d)
53 struct work_record *w2;
55 if (!work || !d) return;
57 if (!d->workgrouplist)
59 d->workgrouplist = work;
65 for (w2 = d->workgrouplist; w2->next; w2 = w2->next);
73 /****************************************************************************
74 create a blank workgroup
75 **************************************************************************/
76 static struct work_record *make_workgroup(char *name)
78 struct work_record *work;
81 if (!name || !name[0]) return NULL;
83 /* conf_workgroup_name_to_token() gets or creates a unique index for the workgroup name */
84 if ((t = conf_workgroup_name_to_token(name, myname)) == -1)
86 DEBUG(3, ("work_record(\"%s\"): conf_workgroup_name_to_token() refuses to allow workgroup\n", name));
87 return (struct work_record *)NULL;
90 work = (struct work_record *)malloc(sizeof(*work));
91 if (!work) return(NULL);
93 StrnCpy(work->work_group,name,sizeof(work->work_group)-1);
94 work->serverlist = NULL;
96 work->ServerType = DFLT_SERVER_TYPE | SV_TYPE_POTENTIAL_BROWSER;
97 work->RunningElection = False;
98 work->ElectionCount = 0;
99 work->needelection = False;
100 work->needannounce = True;
101 work->state = MST_NONE;
104 /* WfWg uses 01040b01 */
105 /* Win95 uses 01041501 */
106 /* NTAS uses ???????? */
107 work->ElectionCriterion = (MAINTAIN_LIST<<1)|(ELECTION_VERSION<<8);
108 work->ElectionCriterion |= (lp_os_level() << 24);
110 if (conf_should_domain_master(work->token))
112 work->ElectionCriterion |= 0x80;
119 /*******************************************************************
121 ******************************************************************/
122 struct work_record *remove_workgroup(struct subnet_record *d,
123 struct work_record *work,
124 BOOL remove_all_servers)
126 struct work_record *ret_work = NULL;
128 if (!d || !work) return NULL;
130 DEBUG(3,("Removing old workgroup %s\n", work->work_group));
132 ret_work = work->next;
134 remove_old_servers(work, -1, remove_all_servers);
136 if (!work->serverlist)
138 if (work->prev) work->prev->next = work->next;
139 if (work->next) work->next->prev = work->prev;
141 if (d->workgrouplist == work) d->workgrouplist = work->next;
150 /****************************************************************************
151 find a workgroup in the workgrouplist
152 only create it if the domain allows it, or the parameter 'add' insists
153 that it get created/added anyway. this allows us to force entries in
154 lmhosts file to be added.
155 **************************************************************************/
156 struct work_record *find_workgroupstruct(struct subnet_record *d,
157 fstring name, BOOL add)
159 struct work_record *ret, *work;
163 DEBUG(4, ("workgroup search for %s: ", name));
165 if (strequal(name, "*"))
167 DEBUG(2,("add any workgroups: initiating browser search on %s\n",
168 inet_ntoa(d->bcast_ip)));
169 queue_netbios_pkt_wins(d,ClientNMB,NMB_QUERY, NAME_QUERY_FIND_MST,
170 -1,MSBROWSE,0x1,0,0,0,NULL,NULL,
171 True,False, d->bcast_ip, d->bcast_ip);
175 for (ret = d->workgrouplist; ret; ret = ret->next)
177 if (!strcmp(ret->work_group,name))
179 DEBUG(4, ("found\n"));
186 DEBUG(4, ("not found\n"));
190 DEBUG(4,("not found: creating\n"));
192 if ((work = make_workgroup(name)))
194 if (!ip_equal(d->bcast_ip, ipgrp) &&
195 lp_preferred_master() &&
196 conf_should_local_master(work->token))
198 DEBUG(3, ("preferred master startup for %s\n", work->work_group));
199 work->needelection = True;
200 work->ElectionCriterion |= (1<<3);
202 add_workgroup(work, d);
209 /****************************************************************************
210 dump a copy of the workgroup/domain database
211 **************************************************************************/
212 void dump_workgroups(void)
214 struct subnet_record *d;
216 for (d = subnetlist; d; d = d->next)
218 if (d->workgrouplist)
220 struct work_record *work;
222 DEBUG(4,("dump domain bcast=%15s: ", inet_ntoa(d->bcast_ip)));
223 DEBUG(4,(" netmask=%15s:\n", inet_ntoa(d->mask_ip)));
225 for (work = d->workgrouplist; work; work = work->next)
227 DEBUG(4,("\t%s(%d)\n", work->work_group, work->token));
228 if (work->serverlist)
230 struct server_record *s;
231 for (s = work->serverlist; s; s = s->next)
233 DEBUG(4,("\t\t%s %8x (%s)\n",
234 s->serv.name, s->serv.type, s->serv.comment));