make man
[Samba.git] / source / nmbd / nmbd_workgroupdb.c
blob1dddc2cd18c1814210328deee6740af7e1e44185
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
25 #include "includes.h"
26 #include "smb.h"
28 extern int ClientNMB;
30 extern pstring global_myname;
31 extern fstring global_myworkgroup;
32 extern char **my_netbios_names;
33 extern uint16 samba_nb_type;
35 int workgroup_count = 0; /* unique index key: one for each workgroup */
37 /****************************************************************************
38 Add a workgroup into the list.
39 **************************************************************************/
41 static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
43 work->subnet = subrec;
44 DLIST_ADD(subrec->workgrouplist, work);
45 subrec->work_changed = True;
48 /****************************************************************************
49 Create an empty workgroup.
50 **************************************************************************/
52 static struct work_record *create_workgroup(const char *name, int ttl)
54 struct work_record *work;
55 struct subnet_record *subrec;
56 int t = -1;
58 if((work = (struct work_record *)malloc(sizeof(*work))) == NULL)
60 DEBUG(0,("create_workgroup: malloc fail !\n"));
61 return NULL;
63 memset((char *)work, '\0', sizeof(*work));
65 StrnCpy(work->work_group,name,sizeof(work->work_group)-1);
66 work->serverlist = NULL;
68 work->RunningElection = False;
69 work->ElectionCount = 0;
70 work->announce_interval = 0;
71 work->needelection = False;
72 work->needannounce = True;
73 work->lastannounce_time = time(NULL);
74 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
75 work->dom_state = DOMAIN_NONE;
76 work->log_state = LOGON_NONE;
78 work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
80 /* Make sure all token representations of workgroups are unique. */
82 for (subrec = FIRST_SUBNET; subrec && (t == -1);
83 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
85 struct work_record *w;
86 for (w = subrec->workgrouplist; w && t == -1; w = w->next)
88 if (strequal(w->work_group, work->work_group))
89 t = w->token;
93 if (t == -1)
94 work->token = ++workgroup_count;
95 else
96 work->token = t;
98 /* No known local master browser as yet. */
99 *work->local_master_browser_name = '\0';
101 /* No known domain master browser as yet. */
102 *work->dmb_name.name = '\0';
103 zero_ip(&work->dmb_addr);
105 /* WfWg uses 01040b01 */
106 /* Win95 uses 01041501 */
107 /* NTAS uses ???????? */
108 work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
109 work->ElectionCriterion |= (lp_os_level() << 24);
110 if (lp_domain_master())
111 work->ElectionCriterion |= 0x80;
113 return work;
116 /*******************************************************************
117 Remove a workgroup.
118 ******************************************************************/
120 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
121 struct work_record *work)
123 struct work_record *ret_work = NULL;
125 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
127 ret_work = work->next;
129 remove_all_servers(work);
131 if (!work->serverlist)
133 if (work->prev)
134 work->prev->next = work->next;
135 if (work->next)
136 work->next->prev = work->prev;
138 if (subrec->workgrouplist == work)
139 subrec->workgrouplist = work->next;
141 ZERO_STRUCTP(work);
142 free((char *)work);
145 subrec->work_changed = True;
147 return ret_work;
151 /****************************************************************************
152 Find a workgroup in the workgroup list of a subnet.
153 **************************************************************************/
155 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
156 const char *name)
158 struct work_record *ret;
160 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
161 name, subrec->subnet_name));
163 for (ret = subrec->workgrouplist; ret; ret = ret->next)
165 if (!strcmp(ret->work_group,name))
167 DEBUGADD(4, ("found.\n"));
168 return(ret);
171 DEBUGADD(4, ("not found.\n"));
172 return NULL;
175 /****************************************************************************
176 Create a workgroup in the workgroup list of the subnet.
177 **************************************************************************/
179 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
180 const char *name, int ttl)
182 struct work_record *work = NULL;
184 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
185 name, subrec->subnet_name));
187 if ((work = create_workgroup(name, ttl)))
189 add_workgroup(subrec, work);
191 subrec->work_changed = True;
193 return(work);
196 return NULL;
199 /****************************************************************************
200 Update a workgroup ttl.
201 **************************************************************************/
203 void update_workgroup_ttl(struct work_record *work, int ttl)
205 if(work->death_time != PERMANENT_TTL)
206 work->death_time = time(NULL)+(ttl*3);
207 work->subnet->work_changed = True;
210 /****************************************************************************
211 Fail function called if we cannot register the WORKGROUP<0> and
212 WORKGROUP<1e> names on the net.
213 **************************************************************************/
215 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
216 struct nmb_name *nmbname)
218 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
219 nmb_namestr(nmbname), subrec->subnet_name));
222 /****************************************************************************
223 If the workgroup is our primary workgroup, add the required names to it.
224 **************************************************************************/
226 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
228 int i;
230 if(!strequal(global_myworkgroup, work->work_group))
231 return;
233 /* If this is a broadcast subnet then start elections on it
234 if we are so configured. */
236 if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
237 (subrec != wins_server_subnet) && lp_preferred_master() &&
238 lp_local_master())
240 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
241 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
242 work->needelection = True;
243 work->ElectionCriterion |= (1<<3);
246 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
248 register_name(subrec,global_myworkgroup,0x0,samba_nb_type|NB_GROUP,
249 NULL,
250 fail_register,NULL);
252 register_name(subrec,global_myworkgroup,0x1e,samba_nb_type|NB_GROUP,
253 NULL,
254 fail_register,NULL);
256 for( i = 0; my_netbios_names[i]; i++)
258 char *name = my_netbios_names[i];
259 int stype = lp_default_server_announce() | (lp_local_master() ?
260 SV_TYPE_POTENTIAL_BROWSER : 0 );
262 if(!strequal(global_myname, name))
263 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|
264 SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
266 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY,
267 PERMANENT_TTL,
268 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
269 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
270 on subnet %s\n", name, subrec->subnet_name));
274 /****************************************************************************
275 Dump a copy of the workgroup database into the log file.
276 **************************************************************************/
278 void dump_workgroups(BOOL force_write)
280 struct subnet_record *subrec;
281 int debuglevel = force_write ? 0 : 4;
283 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
285 if (subrec->workgrouplist)
287 struct work_record *work;
289 if( DEBUGLVL( debuglevel ) )
291 dbgtext( "dump_workgroups()\n " );
292 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
293 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
296 for (work = subrec->workgrouplist; work; work = work->next)
298 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n",
299 work->work_group,
300 work->token,
301 *work->local_master_browser_name
302 ? work->local_master_browser_name : "UNKNOWN" ) );
303 if (work->serverlist)
305 struct server_record *servrec;
306 for (servrec = work->serverlist; servrec; servrec = servrec->next)
308 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
309 servrec->serv.name,
310 servrec->serv.type,
311 servrec->serv.comment ) );
319 /****************************************************************************
320 Expire any dead servers on all workgroups. If the workgroup has expired
321 remove it.
322 **************************************************************************/
324 void expire_workgroups_and_servers(time_t t)
326 struct subnet_record *subrec;
328 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
330 struct work_record *work;
331 struct work_record *nextwork;
333 for (work = subrec->workgrouplist; work; work = nextwork)
335 nextwork = work->next;
336 expire_servers(work, t);
338 if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
339 ((t == -1) || (work->death_time < t)))
341 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
342 work->work_group));
343 remove_workgroup_from_subnet(subrec, work);