Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : NBT netbios routines and daemon - version 2
4 : Copyright (C) Andrew Tridgell 1994-1998
5 : Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 : Copyright (C) Jeremy Allison 1994-1998
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 :
21 : */
22 :
23 : #include "includes.h"
24 : #include "../librpc/gen_ndr/svcctl.h"
25 : #include "nmbd/nmbd.h"
26 : #include "lib/util/string_wrappers.h"
27 :
28 : extern uint16_t samba_nb_type;
29 :
30 : int workgroup_count = 0; /* unique index key: one for each workgroup */
31 :
32 : /****************************************************************************
33 : Add a workgroup into the list.
34 : **************************************************************************/
35 :
36 37 : static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
37 : {
38 37 : work->subnet = subrec;
39 37 : DLIST_ADD(subrec->workgrouplist, work);
40 37 : subrec->work_changed = True;
41 37 : }
42 :
43 : /****************************************************************************
44 : Copy name to unstring. Used by create_workgroup() and find_workgroup_on_subnet().
45 : **************************************************************************/
46 :
47 6234 : static void name_to_unstring(unstring unname, const char *name)
48 : {
49 : nstring nname;
50 :
51 6234 : errno = 0;
52 6234 : push_ascii_nstring(nname, name);
53 6234 : if (errno == E2BIG) {
54 : unstring tname;
55 0 : pull_ascii_nstring(tname, sizeof(tname), nname);
56 0 : strlcpy(unname, tname, sizeof(nname));
57 0 : DEBUG(0,("name_to_nstring: workgroup name %s is too long. Truncating to %s\n",
58 : name, tname));
59 : } else {
60 6234 : unstrcpy(unname, name);
61 : }
62 6234 : }
63 :
64 : /****************************************************************************
65 : Create an empty workgroup.
66 : **************************************************************************/
67 :
68 37 : static struct work_record *create_workgroup(const char *name, int ttl)
69 : {
70 : struct work_record *work;
71 : struct subnet_record *subrec;
72 37 : int t = -1;
73 :
74 37 : if((work = SMB_MALLOC_P(struct work_record)) == NULL) {
75 0 : DEBUG(0,("create_workgroup: malloc fail !\n"));
76 0 : return NULL;
77 : }
78 37 : memset((char *)work, '\0', sizeof(*work));
79 :
80 37 : name_to_unstring(work->work_group, name);
81 :
82 37 : work->serverlist = NULL;
83 :
84 37 : work->RunningElection = False;
85 37 : work->ElectionCount = 0;
86 37 : work->announce_interval = 0;
87 37 : work->needelection = False;
88 37 : work->needannounce = True;
89 37 : work->lastannounce_time = time(NULL);
90 37 : work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
91 37 : work->dom_state = DOMAIN_NONE;
92 37 : work->log_state = LOGON_NONE;
93 :
94 37 : work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
95 :
96 : /* Make sure all token representations of workgroups are unique. */
97 :
98 74 : for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
99 : struct work_record *w;
100 54 : for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
101 17 : if (strequal(w->work_group, work->work_group))
102 0 : t = w->token;
103 : }
104 : }
105 :
106 37 : if (t == -1)
107 37 : work->token = ++workgroup_count;
108 : else
109 0 : work->token = t;
110 :
111 : /* No known local master browser as yet. */
112 37 : *work->local_master_browser_name = '\0';
113 :
114 : /* No known domain master browser as yet. */
115 37 : *work->dmb_name.name = '\0';
116 37 : zero_ip_v4(&work->dmb_addr);
117 :
118 : /* WfWg uses 01040b01 */
119 : /* Win95 uses 01041501 */
120 : /* NTAS uses ???????? */
121 37 : work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
122 37 : work->ElectionCriterion |= (lp_os_level() << 24);
123 37 : if (lp_domain_master())
124 2 : work->ElectionCriterion |= 0x80;
125 :
126 37 : return work;
127 : }
128 :
129 : /*******************************************************************
130 : Remove a workgroup.
131 : ******************************************************************/
132 :
133 0 : static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
134 : struct work_record *work)
135 : {
136 0 : struct work_record *ret_work = NULL;
137 :
138 0 : DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
139 :
140 0 : ret_work = work->next;
141 :
142 0 : remove_all_servers(work);
143 :
144 0 : if (!work->serverlist) {
145 0 : DLIST_REMOVE(subrec->workgrouplist, work);
146 0 : ZERO_STRUCTP(work);
147 0 : SAFE_FREE(work);
148 : }
149 :
150 0 : subrec->work_changed = True;
151 :
152 0 : return ret_work;
153 : }
154 :
155 : /****************************************************************************
156 : Find a workgroup in the workgroup list of a subnet.
157 : **************************************************************************/
158 :
159 6197 : struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
160 : const char *name)
161 : {
162 : struct work_record *ret;
163 : unstring un_name;
164 :
165 6197 : DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
166 : name, subrec->subnet_name));
167 :
168 6197 : name_to_unstring(un_name, name);
169 :
170 7367 : for (ret = subrec->workgrouplist; ret; ret = ret->next) {
171 7353 : if (strequal(ret->work_group,un_name)) {
172 6183 : DEBUGADD(4, ("found.\n"));
173 6183 : return(ret);
174 : }
175 : }
176 14 : DEBUGADD(4, ("not found.\n"));
177 14 : return NULL;
178 : }
179 :
180 : /****************************************************************************
181 : Create a workgroup in the workgroup list of the subnet.
182 : **************************************************************************/
183 :
184 37 : struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
185 : const char *name, int ttl)
186 : {
187 37 : struct work_record *work = NULL;
188 :
189 37 : DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
190 : name, subrec->subnet_name));
191 :
192 37 : if ((work = create_workgroup(name, ttl))) {
193 37 : add_workgroup(subrec, work);
194 37 : subrec->work_changed = True;
195 37 : return(work);
196 : }
197 :
198 0 : return NULL;
199 : }
200 :
201 : /****************************************************************************
202 : Update a workgroup ttl.
203 : **************************************************************************/
204 :
205 3 : void update_workgroup_ttl(struct work_record *work, int ttl)
206 : {
207 3 : if(work->death_time != PERMANENT_TTL)
208 3 : work->death_time = time(NULL)+(ttl*3);
209 3 : work->subnet->work_changed = True;
210 3 : }
211 :
212 : /****************************************************************************
213 : Fail function called if we cannot register the WORKGROUP<0> and
214 : WORKGROUP<1e> names on the net.
215 : **************************************************************************/
216 :
217 0 : static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
218 : struct nmb_name *nmbname)
219 : {
220 0 : DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
221 : nmb_namestr(nmbname), subrec->subnet_name));
222 0 : }
223 :
224 : /****************************************************************************
225 : If the workgroup is our primary workgroup, add the required names to it.
226 : **************************************************************************/
227 :
228 23 : void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
229 : {
230 15 : const struct loadparm_substitution *lp_sub =
231 8 : loadparm_s3_global_substitution();
232 : int i;
233 :
234 23 : if(!strequal(lp_workgroup(), work->work_group))
235 0 : return;
236 :
237 : /* If this is a broadcast subnet then start elections on it if we are so configured. */
238 :
239 38 : if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
240 38 : (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
241 2 : DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
242 : workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
243 2 : work->needelection = True;
244 2 : work->ElectionCriterion |= (1<<3);
245 : }
246 :
247 : /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
248 :
249 23 : register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
250 23 : register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
251 :
252 50 : for( i = 0; my_netbios_names(i); i++) {
253 27 : const char *name = my_netbios_names(i);
254 27 : int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
255 :
256 27 : if(!strequal(lp_netbios_name(), name))
257 4 : stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
258 :
259 27 : create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
260 27 : string_truncate(lp_server_string(talloc_tos(), lp_sub), MAX_SERVER_STRING_LENGTH));
261 27 : DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
262 : on subnet %s\n", name, subrec->subnet_name));
263 : }
264 : }
265 :
266 : /****************************************************************************
267 : Dump a copy of the workgroup database into the log file.
268 : **************************************************************************/
269 :
270 471 : void dump_workgroups(bool force_write)
271 : {
272 : struct subnet_record *subrec;
273 471 : int debuglevel = force_write ? 0 : 4;
274 :
275 942 : for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
276 471 : if (subrec->workgrouplist) {
277 : struct work_record *work;
278 :
279 471 : if( DEBUGLVL( debuglevel ) ) {
280 3 : dbgtext( "dump_workgroups()\n " );
281 3 : dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
282 3 : dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
283 : }
284 :
285 1091 : for (work = subrec->workgrouplist; work; work = work->next) {
286 620 : DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
287 : work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
288 620 : if (work->serverlist) {
289 : struct server_record *servrec;
290 1391 : for (servrec = work->serverlist; servrec; servrec = servrec->next) {
291 920 : DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
292 : servrec->serv.name,
293 : servrec->serv.type,
294 : servrec->serv.comment ) );
295 : }
296 : }
297 : }
298 : }
299 : }
300 471 : }
301 :
302 : /****************************************************************************
303 : Expire any dead servers on all workgroups. If the workgroup has expired
304 : remove it.
305 : **************************************************************************/
306 :
307 442 : void expire_workgroups_and_servers(time_t t)
308 : {
309 : struct subnet_record *subrec;
310 :
311 884 : for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
312 : struct work_record *work;
313 : struct work_record *nextwork;
314 :
315 1032 : for (work = subrec->workgrouplist; work; work = nextwork) {
316 590 : nextwork = work->next;
317 590 : expire_servers(work, t);
318 :
319 590 : if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
320 148 : ((t == (time_t)-1) || (work->death_time < t))) {
321 0 : DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
322 : work->work_group));
323 0 : remove_workgroup_from_subnet(subrec, work);
324 : }
325 : }
326 : }
327 442 : }
|