r15837: starting sync up for 3.0.23rc1 (in sync with SAMBA_3_0 r15822)
[Samba.git] / source / nmbd / nmbd_workgroupdb.c
blobca665bdf4f5ef5366a99dd22b584061293f6ecc7
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
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.
24 #include "includes.h"
26 extern uint16 samba_nb_type;
28 int workgroup_count = 0; /* unique index key: one for each workgroup */
30 /****************************************************************************
31 Add a workgroup into the list.
32 **************************************************************************/
34 static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
36 work->subnet = subrec;
37 DLIST_ADD(subrec->workgrouplist, work);
38 subrec->work_changed = True;
41 /****************************************************************************
42 Copy name to unstring. Used by create_workgroup() and find_workgroup_on_subnet().
43 **************************************************************************/
45 static void name_to_unstring(unstring unname, const char *name)
47 nstring nname;
49 errno = 0;
50 push_ascii_nstring(nname, name);
51 if (errno == E2BIG) {
52 unstring tname;
53 pull_ascii_nstring(tname, sizeof(tname), nname);
54 unstrcpy(unname, tname);
55 DEBUG(0,("name_to_nstring: workgroup name %s is too long. Truncating to %s\n",
56 name, tname));
57 } else {
58 unstrcpy(unname, name);
62 /****************************************************************************
63 Create an empty workgroup.
64 **************************************************************************/
66 static struct work_record *create_workgroup(const char *name, int ttl)
68 struct work_record *work;
69 struct subnet_record *subrec;
70 int t = -1;
72 if((work = SMB_MALLOC_P(struct work_record)) == NULL) {
73 DEBUG(0,("create_workgroup: malloc fail !\n"));
74 return NULL;
76 memset((char *)work, '\0', sizeof(*work));
78 name_to_unstring(work->work_group, name);
80 work->serverlist = NULL;
82 work->RunningElection = False;
83 work->ElectionCount = 0;
84 work->announce_interval = 0;
85 work->needelection = False;
86 work->needannounce = True;
87 work->lastannounce_time = time(NULL);
88 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
89 work->dom_state = DOMAIN_NONE;
90 work->log_state = LOGON_NONE;
92 work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
94 /* Make sure all token representations of workgroups are unique. */
96 for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
97 struct work_record *w;
98 for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
99 if (strequal(w->work_group, work->work_group))
100 t = w->token;
104 if (t == -1)
105 work->token = ++workgroup_count;
106 else
107 work->token = t;
109 /* No known local master browser as yet. */
110 *work->local_master_browser_name = '\0';
112 /* No known domain master browser as yet. */
113 *work->dmb_name.name = '\0';
114 zero_ip(&work->dmb_addr);
116 /* WfWg uses 01040b01 */
117 /* Win95 uses 01041501 */
118 /* NTAS uses ???????? */
119 work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
120 work->ElectionCriterion |= (lp_os_level() << 24);
121 if (lp_domain_master())
122 work->ElectionCriterion |= 0x80;
124 return work;
127 /*******************************************************************
128 Remove a workgroup.
129 ******************************************************************/
131 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
132 struct work_record *work)
134 struct work_record *ret_work = NULL;
136 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
138 ret_work = work->next;
140 remove_all_servers(work);
142 if (!work->serverlist) {
143 if (work->prev)
144 work->prev->next = work->next;
145 if (work->next)
146 work->next->prev = work->prev;
148 if (subrec->workgrouplist == work)
149 subrec->workgrouplist = work->next;
151 ZERO_STRUCTP(work);
152 SAFE_FREE(work);
155 subrec->work_changed = True;
157 return ret_work;
160 /****************************************************************************
161 Find a workgroup in the workgroup list of a subnet.
162 **************************************************************************/
164 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
165 const char *name)
167 struct work_record *ret;
168 unstring un_name;
170 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
171 name, subrec->subnet_name));
173 name_to_unstring(un_name, name);
175 for (ret = subrec->workgrouplist; ret; ret = ret->next) {
176 if (strequal(ret->work_group,un_name)) {
177 DEBUGADD(4, ("found.\n"));
178 return(ret);
181 DEBUGADD(4, ("not found.\n"));
182 return NULL;
185 /****************************************************************************
186 Create a workgroup in the workgroup list of the subnet.
187 **************************************************************************/
189 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
190 const char *name, int ttl)
192 struct work_record *work = NULL;
194 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
195 name, subrec->subnet_name));
197 if ((work = create_workgroup(name, ttl))) {
198 add_workgroup(subrec, work);
199 subrec->work_changed = True;
200 return(work);
203 return NULL;
206 /****************************************************************************
207 Update a workgroup ttl.
208 **************************************************************************/
210 void update_workgroup_ttl(struct work_record *work, int ttl)
212 if(work->death_time != PERMANENT_TTL)
213 work->death_time = time(NULL)+(ttl*3);
214 work->subnet->work_changed = True;
217 /****************************************************************************
218 Fail function called if we cannot register the WORKGROUP<0> and
219 WORKGROUP<1e> names on the net.
220 **************************************************************************/
222 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
223 struct nmb_name *nmbname)
225 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
226 nmb_namestr(nmbname), subrec->subnet_name));
229 /****************************************************************************
230 If the workgroup is our primary workgroup, add the required names to it.
231 **************************************************************************/
233 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
235 int i;
237 if(!strequal(lp_workgroup(), work->work_group))
238 return;
240 /* If this is a broadcast subnet then start elections on it if we are so configured. */
242 if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
243 (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
244 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
245 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
246 work->needelection = True;
247 work->ElectionCriterion |= (1<<3);
250 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
252 register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
253 register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
255 for( i = 0; my_netbios_names(i); i++) {
256 const char *name = my_netbios_names(i);
257 int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
259 if(!strequal(global_myname(), name))
260 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
262 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
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)) {
279 if (subrec->workgrouplist) {
280 struct work_record *work;
282 if( DEBUGLVL( debuglevel ) ) {
283 dbgtext( "dump_workgroups()\n " );
284 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
285 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
288 for (work = subrec->workgrouplist; work; work = work->next) {
289 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
290 work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
291 if (work->serverlist) {
292 struct server_record *servrec;
293 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
294 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
295 servrec->serv.name,
296 servrec->serv.type,
297 servrec->serv.comment ) );
305 /****************************************************************************
306 Expire any dead servers on all workgroups. If the workgroup has expired
307 remove it.
308 **************************************************************************/
310 void expire_workgroups_and_servers(time_t t)
312 struct subnet_record *subrec;
314 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
315 struct work_record *work;
316 struct work_record *nextwork;
318 for (work = subrec->workgrouplist; work; work = nextwork) {
319 nextwork = work->next;
320 expire_servers(work, t);
322 if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
323 ((t == (time_t)-1) || (work->death_time < t))) {
324 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
325 work->work_group));
326 remove_workgroup_from_subnet(subrec, work);