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
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 extern uint16 samba_nb_type
;
30 int workgroup_count
= 0; /* unique index key: one for each workgroup */
32 /****************************************************************************
33 Add a workgroup into the list.
34 **************************************************************************/
36 static void add_workgroup(struct subnet_record
*subrec
, struct work_record
*work
)
38 work
->subnet
= subrec
;
39 DLIST_ADD(subrec
->workgrouplist
, work
);
40 subrec
->work_changed
= True
;
43 /****************************************************************************
44 Create an empty workgroup.
45 **************************************************************************/
47 static struct work_record
*create_workgroup(const char *name
, int ttl
)
49 struct work_record
*work
;
50 struct subnet_record
*subrec
;
53 if((work
= (struct work_record
*)malloc(sizeof(*work
))) == NULL
)
55 DEBUG(0,("create_workgroup: malloc fail !\n"));
58 memset((char *)work
, '\0', sizeof(*work
));
60 StrnCpy(work
->work_group
,name
,sizeof(work
->work_group
)-1);
61 work
->serverlist
= NULL
;
63 work
->RunningElection
= False
;
64 work
->ElectionCount
= 0;
65 work
->announce_interval
= 0;
66 work
->needelection
= False
;
67 work
->needannounce
= True
;
68 work
->lastannounce_time
= time(NULL
);
69 work
->mst_state
= lp_local_master() ? MST_POTENTIAL
: MST_NONE
;
70 work
->dom_state
= DOMAIN_NONE
;
71 work
->log_state
= LOGON_NONE
;
73 work
->death_time
= (ttl
!= PERMANENT_TTL
) ? time(NULL
)+(ttl
*3) : PERMANENT_TTL
;
75 /* Make sure all token representations of workgroups are unique. */
77 for (subrec
= FIRST_SUBNET
; subrec
&& (t
== -1);
78 subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
80 struct work_record
*w
;
81 for (w
= subrec
->workgrouplist
; w
&& t
== -1; w
= w
->next
)
83 if (strequal(w
->work_group
, work
->work_group
))
89 work
->token
= ++workgroup_count
;
93 /* No known local master browser as yet. */
94 *work
->local_master_browser_name
= '\0';
96 /* No known domain master browser as yet. */
97 *work
->dmb_name
.name
= '\0';
98 zero_ip(&work
->dmb_addr
);
100 /* WfWg uses 01040b01 */
101 /* Win95 uses 01041501 */
102 /* NTAS uses ???????? */
103 work
->ElectionCriterion
= (MAINTAIN_LIST
)|(BROWSER_ELECTION_VERSION
<<8);
104 work
->ElectionCriterion
|= (lp_os_level() << 24);
105 if (lp_domain_master())
106 work
->ElectionCriterion
|= 0x80;
111 /*******************************************************************
113 ******************************************************************/
115 static struct work_record
*remove_workgroup_from_subnet(struct subnet_record
*subrec
,
116 struct work_record
*work
)
118 struct work_record
*ret_work
= NULL
;
120 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work
->work_group
));
122 ret_work
= work
->next
;
124 remove_all_servers(work
);
126 if (!work
->serverlist
)
129 work
->prev
->next
= work
->next
;
131 work
->next
->prev
= work
->prev
;
133 if (subrec
->workgrouplist
== work
)
134 subrec
->workgrouplist
= work
->next
;
140 subrec
->work_changed
= True
;
146 /****************************************************************************
147 Find a workgroup in the workgroup list of a subnet.
148 **************************************************************************/
150 struct work_record
*find_workgroup_on_subnet(struct subnet_record
*subrec
,
153 struct work_record
*ret
;
155 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
156 name
, subrec
->subnet_name
));
158 for (ret
= subrec
->workgrouplist
; ret
; ret
= ret
->next
)
160 if (!strcmp(ret
->work_group
,name
))
162 DEBUGADD(4, ("found.\n"));
166 DEBUGADD(4, ("not found.\n"));
170 /****************************************************************************
171 Create a workgroup in the workgroup list of the subnet.
172 **************************************************************************/
174 struct work_record
*create_workgroup_on_subnet(struct subnet_record
*subrec
,
175 const char *name
, int ttl
)
177 struct work_record
*work
= NULL
;
179 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
180 name
, subrec
->subnet_name
));
182 if ((work
= create_workgroup(name
, ttl
)))
184 add_workgroup(subrec
, work
);
186 subrec
->work_changed
= True
;
194 /****************************************************************************
195 Update a workgroup ttl.
196 **************************************************************************/
198 void update_workgroup_ttl(struct work_record
*work
, int ttl
)
200 if(work
->death_time
!= PERMANENT_TTL
)
201 work
->death_time
= time(NULL
)+(ttl
*3);
202 work
->subnet
->work_changed
= True
;
205 /****************************************************************************
206 Fail function called if we cannot register the WORKGROUP<0> and
207 WORKGROUP<1e> names on the net.
208 **************************************************************************/
210 static void fail_register(struct subnet_record
*subrec
, struct response_record
*rrec
,
211 struct nmb_name
*nmbname
)
213 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
214 nmb_namestr(nmbname
), subrec
->subnet_name
));
217 /****************************************************************************
218 If the workgroup is our primary workgroup, add the required names to it.
219 **************************************************************************/
221 void initiate_myworkgroup_startup(struct subnet_record
*subrec
, struct work_record
*work
)
225 if(!strequal(lp_workgroup(), work
->work_group
))
228 /* If this is a broadcast subnet then start elections on it
229 if we are so configured. */
231 if ((subrec
!= unicast_subnet
) && (subrec
!= remote_broadcast_subnet
) &&
232 (subrec
!= wins_server_subnet
) && lp_preferred_master() &&
235 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
236 workgroup %s on subnet %s\n", work
->work_group
, subrec
->subnet_name
));
237 work
->needelection
= True
;
238 work
->ElectionCriterion
|= (1<<3);
241 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
243 register_name(subrec
,lp_workgroup(),0x0,samba_nb_type
|NB_GROUP
,
247 register_name(subrec
,lp_workgroup(),0x1e,samba_nb_type
|NB_GROUP
,
251 for( i
= 0; my_netbios_names(i
); i
++)
253 const char *name
= my_netbios_names(i
);
254 int stype
= lp_default_server_announce() | (lp_local_master() ?
255 SV_TYPE_POTENTIAL_BROWSER
: 0 );
257 if(!strequal(global_myname(), name
))
258 stype
&= ~(SV_TYPE_MASTER_BROWSER
|SV_TYPE_POTENTIAL_BROWSER
|
259 SV_TYPE_DOMAIN_MASTER
|SV_TYPE_DOMAIN_MEMBER
);
261 create_server_on_workgroup(work
,name
,stype
|SV_TYPE_LOCAL_LIST_ONLY
,
263 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH
));
264 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
265 on subnet %s\n", name
, subrec
->subnet_name
));
269 /****************************************************************************
270 Dump a copy of the workgroup database into the log file.
271 **************************************************************************/
273 void dump_workgroups(BOOL force_write
)
275 struct subnet_record
*subrec
;
276 int debuglevel
= force_write
? 0 : 4;
278 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
280 if (subrec
->workgrouplist
)
282 struct work_record
*work
;
284 if( DEBUGLVL( debuglevel
) )
286 dbgtext( "dump_workgroups()\n " );
287 dbgtext( "dump workgroup on subnet %15s: ", subrec
->subnet_name
);
288 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec
->mask_ip
) );
291 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
)
293 DEBUGADD( debuglevel
, ( "\t%s(%d) current master browser = %s\n",
296 *work
->local_master_browser_name
297 ? work
->local_master_browser_name
: "UNKNOWN" ) );
298 if (work
->serverlist
)
300 struct server_record
*servrec
;
301 for (servrec
= work
->serverlist
; servrec
; servrec
= servrec
->next
)
303 DEBUGADD( debuglevel
, ( "\t\t%s %8x (%s)\n",
306 servrec
->serv
.comment
) );
314 /****************************************************************************
315 Expire any dead servers on all workgroups. If the workgroup has expired
317 **************************************************************************/
319 void expire_workgroups_and_servers(time_t t
)
321 struct subnet_record
*subrec
;
323 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
325 struct work_record
*work
;
326 struct work_record
*nextwork
;
328 for (work
= subrec
->workgrouplist
; work
; work
= nextwork
)
330 nextwork
= work
->next
;
331 expire_servers(work
, t
);
333 if ((work
->serverlist
== NULL
) && (work
->death_time
!= PERMANENT_TTL
) &&
334 ((t
== -1) || (work
->death_time
< t
)))
336 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
338 remove_workgroup_from_subnet(subrec
, work
);