2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 extern int DEBUGLEVEL
;
32 extern pstring global_myname
;
33 extern fstring global_myworkgroup
;
34 extern char **my_netbios_names
;
35 extern uint16 samba_nb_type
;
36 extern struct in_addr ipzero
;
38 int workgroup_count
= 0; /* unique index key: one for each workgroup */
40 /****************************************************************************
41 Add a workgroup into the list.
42 **************************************************************************/
44 static void add_workgroup(struct subnet_record
*subrec
, struct work_record
*work
)
46 work
->subnet
= subrec
;
47 DLIST_ADD(subrec
->workgrouplist
, work
);
48 subrec
->work_changed
= True
;
51 /****************************************************************************
52 Create an empty workgroup.
53 **************************************************************************/
55 static struct work_record
*create_workgroup(char *name
, int ttl
)
57 struct work_record
*work
;
58 struct subnet_record
*subrec
;
61 if((work
= (struct work_record
*)malloc(sizeof(*work
))) == NULL
)
63 DEBUG(0,("create_workgroup: malloc fail !\n"));
66 memset((char *)work
, '\0', sizeof(*work
));
68 StrnCpy(work
->work_group
,name
,sizeof(work
->work_group
)-1);
69 work
->serverlist
= NULL
;
71 work
->RunningElection
= False
;
72 work
->ElectionCount
= 0;
73 work
->announce_interval
= 0;
74 work
->needelection
= False
;
75 work
->needannounce
= True
;
76 work
->lastannounce_time
= time(NULL
);
77 work
->mst_state
= lp_local_master() ? MST_POTENTIAL
: MST_NONE
;
78 work
->dom_state
= DOMAIN_NONE
;
79 work
->log_state
= LOGON_NONE
;
81 work
->death_time
= (ttl
!= PERMANENT_TTL
) ? time(NULL
)+(ttl
*3) : PERMANENT_TTL
;
83 /* Make sure all token representations of workgroups are unique. */
85 for (subrec
= FIRST_SUBNET
; subrec
&& (t
== -1);
86 subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
88 struct work_record
*w
;
89 for (w
= subrec
->workgrouplist
; w
&& t
== -1; w
= w
->next
)
91 if (strequal(w
->work_group
, work
->work_group
))
97 work
->token
= ++workgroup_count
;
101 /* No known local master browser as yet. */
102 *work
->local_master_browser_name
= '\0';
104 /* No known domain master browser as yet. */
105 *work
->dmb_name
.name
= '\0';
106 putip((char *)&work
->dmb_addr
, &ipzero
);
108 /* WfWg uses 01040b01 */
109 /* Win95 uses 01041501 */
110 /* NTAS uses ???????? */
111 work
->ElectionCriterion
= (MAINTAIN_LIST
)|(BROWSER_ELECTION_VERSION
<<8);
112 work
->ElectionCriterion
|= (lp_os_level() << 24);
113 if (lp_domain_master())
114 work
->ElectionCriterion
|= 0x80;
119 /*******************************************************************
121 ******************************************************************/
123 static struct work_record
*remove_workgroup_from_subnet(struct subnet_record
*subrec
,
124 struct work_record
*work
)
126 struct work_record
*ret_work
= NULL
;
128 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work
->work_group
));
130 ret_work
= work
->next
;
132 remove_all_servers(work
);
134 if (!work
->serverlist
)
137 work
->prev
->next
= work
->next
;
139 work
->next
->prev
= work
->prev
;
141 if (subrec
->workgrouplist
== work
)
142 subrec
->workgrouplist
= work
->next
;
148 subrec
->work_changed
= True
;
154 /****************************************************************************
155 Find a workgroup in the workgroup list of a subnet.
156 **************************************************************************/
158 struct work_record
*find_workgroup_on_subnet(struct subnet_record
*subrec
,
161 struct work_record
*ret
;
163 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
164 name
, subrec
->subnet_name
));
166 for (ret
= subrec
->workgrouplist
; ret
; ret
= ret
->next
)
168 if (!strcmp(ret
->work_group
,name
))
170 DEBUGADD(4, ("found.\n"));
174 DEBUGADD(4, ("not found.\n"));
178 /****************************************************************************
179 Create a workgroup in the workgroup list of the subnet.
180 **************************************************************************/
182 struct work_record
*create_workgroup_on_subnet(struct subnet_record
*subrec
,
183 fstring name
, int ttl
)
185 struct work_record
*work
= NULL
;
187 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
188 name
, subrec
->subnet_name
));
190 if ((work
= create_workgroup(name
, ttl
)))
192 add_workgroup(subrec
, work
);
194 subrec
->work_changed
= True
;
202 /****************************************************************************
203 Update a workgroup ttl.
204 **************************************************************************/
206 void update_workgroup_ttl(struct work_record
*work
, int ttl
)
208 if(work
->death_time
!= PERMANENT_TTL
)
209 work
->death_time
= time(NULL
)+(ttl
*3);
210 work
->subnet
->work_changed
= True
;
213 /****************************************************************************
214 Fail function called if we cannot register the WORKGROUP<0> and
215 WORKGROUP<1e> names on the net.
216 **************************************************************************/
218 static void fail_register(struct subnet_record
*subrec
, struct response_record
*rrec
,
219 struct nmb_name
*nmbname
)
221 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
222 nmb_namestr(nmbname
), subrec
->subnet_name
));
225 /****************************************************************************
226 If the workgroup is our primary workgroup, add the required names to it.
227 **************************************************************************/
229 void initiate_myworkgroup_startup(struct subnet_record
*subrec
, struct work_record
*work
)
233 if(!strequal(global_myworkgroup
, work
->work_group
))
236 /* If this is a broadcast subnet then start elections on it
237 if we are so configured. */
239 if ((subrec
!= unicast_subnet
) && (subrec
!= remote_broadcast_subnet
) &&
240 (subrec
!= wins_server_subnet
) && lp_preferred_master() &&
243 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
244 workgroup %s on subnet %s\n", work
->work_group
, subrec
->subnet_name
));
245 work
->needelection
= True
;
246 work
->ElectionCriterion
|= (1<<3);
249 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
251 register_name(subrec
,global_myworkgroup
,0x0,samba_nb_type
|NB_GROUP
,
255 register_name(subrec
,global_myworkgroup
,0x1e,samba_nb_type
|NB_GROUP
,
259 for( i
= 0; my_netbios_names
[i
]; i
++)
261 char *name
= my_netbios_names
[i
];
262 int stype
= lp_default_server_announce() | (lp_local_master() ?
263 SV_TYPE_POTENTIAL_BROWSER
: 0 );
265 if(!strequal(global_myname
, name
))
266 stype
&= ~(SV_TYPE_MASTER_BROWSER
|SV_TYPE_POTENTIAL_BROWSER
|
267 SV_TYPE_DOMAIN_MASTER
|SV_TYPE_DOMAIN_MEMBER
);
269 create_server_on_workgroup(work
,name
,stype
|SV_TYPE_LOCAL_LIST_ONLY
,
271 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH
));
272 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
273 on subnet %s\n", name
, subrec
->subnet_name
));
277 /****************************************************************************
278 Dump a copy of the workgroup database into the log file.
279 **************************************************************************/
281 void dump_workgroups(BOOL force_write
)
283 struct subnet_record
*subrec
;
284 int debuglevel
= force_write
? 1 : 4;
286 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
288 if (subrec
->workgrouplist
)
290 struct work_record
*work
;
292 if( DEBUGLVL( debuglevel
) )
294 dbgtext( "dump_workgroups()\n " );
295 dbgtext( "dump workgroup on subnet %15s: ", subrec
->subnet_name
);
296 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec
->mask_ip
) );
299 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
)
301 DEBUGADD( debuglevel
, ( "\t%s(%d) current master browser = %s\n",
304 *work
->local_master_browser_name
305 ? work
->local_master_browser_name
: "UNKNOWN" ) );
306 if (work
->serverlist
)
308 struct server_record
*servrec
;
309 for (servrec
= work
->serverlist
; servrec
; servrec
= servrec
->next
)
311 DEBUGADD( debuglevel
, ( "\t\t%s %8x (%s)\n",
314 servrec
->serv
.comment
) );
322 /****************************************************************************
323 Expire any dead servers on all workgroups. If the workgroup has expired
325 **************************************************************************/
327 void expire_workgroups_and_servers(time_t t
)
329 struct subnet_record
*subrec
;
331 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
333 struct work_record
*work
;
334 struct work_record
*nextwork
;
336 for (work
= subrec
->workgrouplist
; work
; work
= nextwork
)
338 nextwork
= work
->next
;
339 expire_servers(work
, t
);
341 if ((work
->serverlist
== NULL
) && (work
->death_time
!= PERMANENT_TTL
) &&
342 ((t
== -1) || (work
->death_time
< t
)))
344 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
346 remove_workgroup_from_subnet(subrec
, work
);