Modified fix for bugid #784. Based on a patch from moriyama@miraclelinux.com (MORIYAM...
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_workgroupdb.c
blobbd2737ef972773f575376fef2038b84e2b7724b2
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 int ClientNMB;
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;
51 nstring nname;
53 int t = -1;
55 if((work = (struct work_record *)malloc(sizeof(*work))) == NULL) {
56 DEBUG(0,("create_workgroup: malloc fail !\n"));
57 return NULL;
59 memset((char *)work, '\0', sizeof(*work));
61 errno = 0;
62 push_ascii_nstring(nname, name);
63 if (errno == E2BIG) {
64 fstring tname;
65 pull_ascii_nstring(tname, sizeof(tname), nname);
66 fstrcpy(work->work_group,tname);
67 DEBUG(0,("create_workgroup: workgroup name %s is too long. Truncating to %s\n",
68 name, tname));
69 } else {
70 fstrcpy(work->work_group,name);
72 work->serverlist = NULL;
74 work->RunningElection = False;
75 work->ElectionCount = 0;
76 work->announce_interval = 0;
77 work->needelection = False;
78 work->needannounce = True;
79 work->lastannounce_time = time(NULL);
80 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
81 work->dom_state = DOMAIN_NONE;
82 work->log_state = LOGON_NONE;
84 work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
86 /* Make sure all token representations of workgroups are unique. */
88 for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
89 struct work_record *w;
90 for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
91 if (strnequal(w->work_group, work->work_group, sizeof(nstring)-1))
92 t = w->token;
96 if (t == -1)
97 work->token = ++workgroup_count;
98 else
99 work->token = t;
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 zero_ip(&work->dmb_addr);
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;
116 return work;
119 /*******************************************************************
120 Remove a workgroup.
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) {
135 if (work->prev)
136 work->prev->next = work->next;
137 if (work->next)
138 work->next->prev = work->prev;
140 if (subrec->workgrouplist == work)
141 subrec->workgrouplist = work->next;
143 ZERO_STRUCTP(work);
144 SAFE_FREE(work);
147 subrec->work_changed = True;
149 return ret_work;
152 /****************************************************************************
153 Find a workgroup in the workgroup list of a subnet.
154 **************************************************************************/
156 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
157 const char *name)
159 struct work_record *ret;
161 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
162 name, subrec->subnet_name));
164 for (ret = subrec->workgrouplist; ret; ret = ret->next) {
165 if (strnequal(ret->work_group,name,sizeof(nstring)-1)) {
166 DEBUGADD(4, ("found.\n"));
167 return(ret);
170 DEBUGADD(4, ("not found.\n"));
171 return NULL;
174 /****************************************************************************
175 Create a workgroup in the workgroup list of the subnet.
176 **************************************************************************/
178 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
179 const char *name, int ttl)
181 struct work_record *work = NULL;
183 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
184 name, subrec->subnet_name));
186 if ((work = create_workgroup(name, ttl))) {
187 add_workgroup(subrec, work);
188 subrec->work_changed = True;
189 return(work);
192 return NULL;
195 /****************************************************************************
196 Update a workgroup ttl.
197 **************************************************************************/
199 void update_workgroup_ttl(struct work_record *work, int ttl)
201 if(work->death_time != PERMANENT_TTL)
202 work->death_time = time(NULL)+(ttl*3);
203 work->subnet->work_changed = True;
206 /****************************************************************************
207 Fail function called if we cannot register the WORKGROUP<0> and
208 WORKGROUP<1e> names on the net.
209 **************************************************************************/
211 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
212 struct nmb_name *nmbname)
214 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
215 nmb_namestr(nmbname), subrec->subnet_name));
218 /****************************************************************************
219 If the workgroup is our primary workgroup, add the required names to it.
220 **************************************************************************/
222 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
224 int i;
226 if(!strnequal(lp_workgroup(), work->work_group,sizeof(nstring)-1))
227 return;
229 /* If this is a broadcast subnet then start elections on it if we are so configured. */
231 if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
232 (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
233 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
234 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
235 work->needelection = True;
236 work->ElectionCriterion |= (1<<3);
239 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
241 register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
242 register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
244 for( i = 0; my_netbios_names(i); i++) {
245 const char *name = my_netbios_names(i);
246 int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
248 if(!strequal(global_myname(), name))
249 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
251 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
252 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
253 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
254 on subnet %s\n", name, subrec->subnet_name));
258 /****************************************************************************
259 Dump a copy of the workgroup database into the log file.
260 **************************************************************************/
262 void dump_workgroups(BOOL force_write)
264 struct subnet_record *subrec;
265 int debuglevel = force_write ? 0 : 4;
267 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
268 if (subrec->workgrouplist) {
269 struct work_record *work;
271 if( DEBUGLVL( debuglevel ) ) {
272 dbgtext( "dump_workgroups()\n " );
273 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
274 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
277 for (work = subrec->workgrouplist; work; work = work->next) {
278 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
279 work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
280 if (work->serverlist) {
281 struct server_record *servrec;
282 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
283 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
284 servrec->serv.name,
285 servrec->serv.type,
286 servrec->serv.comment ) );
294 /****************************************************************************
295 Expire any dead servers on all workgroups. If the workgroup has expired
296 remove it.
297 **************************************************************************/
299 void expire_workgroups_and_servers(time_t t)
301 struct subnet_record *subrec;
303 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
304 struct work_record *work;
305 struct work_record *nextwork;
307 for (work = subrec->workgrouplist; work; work = nextwork) {
308 nextwork = work->next;
309 expire_servers(work, t);
311 if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
312 ((t == -1) || (work->death_time < t))) {
313 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
314 work->work_group));
315 remove_workgroup_from_subnet(subrec, work);