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 namedbwork containing workgroup database functions
36 extern int DEBUGLEVEL
;
38 /* this is our domain/workgroup/server database */
39 extern struct subnet_record
*subnetlist
;
41 extern struct in_addr wins_ip
;
43 extern fstring myworkgroup
;
45 int workgroup_count
= 0; /* unique index key: one for each workgroup */
49 /****************************************************************************
50 add a workgroup into the domain list
51 **************************************************************************/
52 static void add_workgroup(struct work_record
*work
, struct subnet_record
*d
)
54 struct work_record
*w2
;
56 if (!work
|| !d
) return;
58 if (!d
->workgrouplist
)
60 d
->workgrouplist
= work
;
66 for (w2
= d
->workgrouplist
; w2
->next
; w2
= w2
->next
);
74 /****************************************************************************
75 create a blank workgroup
76 **************************************************************************/
77 static struct work_record
*make_workgroup(char *name
)
79 struct work_record
*work
;
80 struct subnet_record
*d
;
83 if (!name
|| !name
[0]) return NULL
;
85 work
= (struct work_record
*)malloc(sizeof(*work
));
86 if (!work
) return(NULL
);
88 StrnCpy(work
->work_group
,name
,sizeof(work
->work_group
)-1);
89 work
->serverlist
= NULL
;
91 work
->ServerType
= DFLT_SERVER_TYPE
| (lp_local_master() ?
92 SV_TYPE_POTENTIAL_BROWSER
: 0 );
93 work
->RunningElection
= False
;
94 work
->ElectionCount
= 0;
95 work
->needelection
= False
;
96 work
->needannounce
= True
;
97 work
->mst_state
= MST_POTENTIAL
;
98 work
->dom_state
= DOMAIN_NONE
;
99 work
->log_state
= LOGON_NONE
;
101 /* make sure all token representations of workgroups are unique */
103 for (d
= FIRST_SUBNET
; d
&& t
== -1; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
105 struct work_record
*w
;
106 for (w
= d
->workgrouplist
; w
&& t
== -1; w
= w
->next
)
108 if (strequal(w
->work_group
, work
->work_group
)) t
= w
->token
;
114 work
->token
= ++workgroup_count
;
122 /* WfWg uses 01040b01 */
123 /* Win95 uses 01041501 */
124 /* NTAS uses ???????? */
125 work
->ElectionCriterion
= (MAINTAIN_LIST
)|(ELECTION_VERSION
<<8);
126 work
->ElectionCriterion
|= (lp_os_level() << 24);
127 if (lp_domain_master()) {
128 work
->ElectionCriterion
|= 0x80;
135 /*******************************************************************
137 ******************************************************************/
138 struct work_record
*remove_workgroup(struct subnet_record
*d
,
139 struct work_record
*work
,
140 BOOL remove_all_servers
)
142 struct work_record
*ret_work
= NULL
;
144 if (!d
|| !work
) return NULL
;
146 DEBUG(3,("Removing old workgroup %s\n", work
->work_group
));
148 ret_work
= work
->next
;
150 remove_old_servers(work
, -1, remove_all_servers
);
152 if (!work
->serverlist
)
154 if (work
->prev
) work
->prev
->next
= work
->next
;
155 if (work
->next
) work
->next
->prev
= work
->prev
;
157 if (d
->workgrouplist
== work
) d
->workgrouplist
= work
->next
;
166 /****************************************************************************
167 find a workgroup in the workgrouplist
168 only create it if the domain allows it, or the parameter 'add' insists
169 that it get created/added anyway. this allows us to force entries in
170 lmhosts file to be added.
171 **************************************************************************/
172 struct work_record
*find_workgroupstruct(struct subnet_record
*d
,
173 fstring name
, BOOL add
)
175 struct work_record
*ret
, *work
;
179 DEBUG(4, ("workgroup search for %s: ", name
));
181 for (ret
= d
->workgrouplist
; ret
; ret
= ret
->next
) {
182 if (!strcmp(ret
->work_group
,name
)) {
183 DEBUG(4, ("found\n"));
189 DEBUG(4, ("not found\n"));
193 DEBUG(4,("not found: creating\n"));
195 if ((work
= make_workgroup(name
)))
197 if (!ip_equal(d
->bcast_ip
, wins_ip
) &&
198 lp_preferred_master() && lp_local_master() &&
199 strequal(myworkgroup
, name
))
201 DEBUG(3, ("preferred master startup for %s\n", work
->work_group
));
202 work
->needelection
= True
;
203 work
->ElectionCriterion
|= (1<<3);
205 add_workgroup(work
, d
);
212 /****************************************************************************
213 dump a copy of the workgroup/domain database
214 **************************************************************************/
215 void dump_workgroups(void)
217 struct subnet_record
*d
;
219 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
221 if (d
->workgrouplist
)
223 struct work_record
*work
;
225 DEBUG(4,("dump domain bcast=%15s: ", inet_ntoa(d
->bcast_ip
)));
226 DEBUG(4,(" netmask=%15s:\n", inet_ntoa(d
->mask_ip
)));
228 for (work
= d
->workgrouplist
; work
; work
= work
->next
)
230 DEBUG(4,("\t%s(%d)\n", work
->work_group
, work
->token
));
231 if (work
->serverlist
)
233 struct server_record
*s
;
234 for (s
= work
->serverlist
; s
; s
= s
->next
)
236 DEBUG(4,("\t\t%s %8x (%s)\n",
237 s
->serv
.name
, s
->serv
.type
, s
->serv
.comment
));