Apply the changes that Derrell Lipman supplied ...
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_workgroupdb.c
blobb9fab4b278c12508cfc2273bc9b7b6ec5cdee5a3
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 int t = -1;
53 if((work = (struct work_record *)malloc(sizeof(*work))) == NULL) {
54 DEBUG(0,("create_workgroup: malloc fail !\n"));
55 return NULL;
57 memset((char *)work, '\0', sizeof(*work));
59 if (strlen(name)+1 > sizeof(nstring)) {
60 memcpy(work->work_group,name,sizeof(nstring)-1);
61 work->work_group[sizeof(nstring)-1] = '\0';
62 DEBUG(0,("create_workgroup: workgroup name %s is too long. Truncating to %s\n",
63 name, work->work_group ));
64 } else {
65 nstrcpy(work->work_group,name);
67 work->serverlist = NULL;
69 work->RunningElection = False;
70 work->ElectionCount = 0;
71 work->announce_interval = 0;
72 work->needelection = False;
73 work->needannounce = True;
74 work->lastannounce_time = time(NULL);
75 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
76 work->dom_state = DOMAIN_NONE;
77 work->log_state = LOGON_NONE;
79 work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
81 /* Make sure all token representations of workgroups are unique. */
83 for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
84 struct work_record *w;
85 for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
86 if (strnequal(w->work_group, work->work_group, sizeof(nstring)-1))
87 t = w->token;
91 if (t == -1)
92 work->token = ++workgroup_count;
93 else
94 work->token = t;
96 /* No known local master browser as yet. */
97 *work->local_master_browser_name = '\0';
99 /* No known domain master browser as yet. */
100 *work->dmb_name.name = '\0';
101 zero_ip(&work->dmb_addr);
103 /* WfWg uses 01040b01 */
104 /* Win95 uses 01041501 */
105 /* NTAS uses ???????? */
106 work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
107 work->ElectionCriterion |= (lp_os_level() << 24);
108 if (lp_domain_master())
109 work->ElectionCriterion |= 0x80;
111 return work;
114 /*******************************************************************
115 Remove a workgroup.
116 ******************************************************************/
118 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
119 struct work_record *work)
121 struct work_record *ret_work = NULL;
123 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
125 ret_work = work->next;
127 remove_all_servers(work);
129 if (!work->serverlist) {
130 if (work->prev)
131 work->prev->next = work->next;
132 if (work->next)
133 work->next->prev = work->prev;
135 if (subrec->workgrouplist == work)
136 subrec->workgrouplist = work->next;
138 ZERO_STRUCTP(work);
139 SAFE_FREE(work);
142 subrec->work_changed = True;
144 return ret_work;
147 /****************************************************************************
148 Find a workgroup in the workgroup list of a subnet.
149 **************************************************************************/
151 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
152 const char *name)
154 struct work_record *ret;
156 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
157 name, subrec->subnet_name));
159 for (ret = subrec->workgrouplist; ret; ret = ret->next) {
160 if (strnequal(ret->work_group,name,sizeof(nstring)-1)) {
161 DEBUGADD(4, ("found.\n"));
162 return(ret);
165 DEBUGADD(4, ("not found.\n"));
166 return NULL;
169 /****************************************************************************
170 Create a workgroup in the workgroup list of the subnet.
171 **************************************************************************/
173 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
174 const char *name, int ttl)
176 struct work_record *work = NULL;
178 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
179 name, subrec->subnet_name));
181 if ((work = create_workgroup(name, ttl))) {
182 add_workgroup(subrec, work);
183 subrec->work_changed = True;
184 return(work);
187 return NULL;
190 /****************************************************************************
191 Update a workgroup ttl.
192 **************************************************************************/
194 void update_workgroup_ttl(struct work_record *work, int ttl)
196 if(work->death_time != PERMANENT_TTL)
197 work->death_time = time(NULL)+(ttl*3);
198 work->subnet->work_changed = True;
201 /****************************************************************************
202 Fail function called if we cannot register the WORKGROUP<0> and
203 WORKGROUP<1e> names on the net.
204 **************************************************************************/
206 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
207 struct nmb_name *nmbname)
209 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
210 nmb_namestr(nmbname), subrec->subnet_name));
213 /****************************************************************************
214 If the workgroup is our primary workgroup, add the required names to it.
215 **************************************************************************/
217 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
219 int i;
221 if(!strnequal(lp_workgroup(), work->work_group,sizeof(nstring)-1))
222 return;
224 /* If this is a broadcast subnet then start elections on it if we are so configured. */
226 if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
227 (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
228 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
229 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
230 work->needelection = True;
231 work->ElectionCriterion |= (1<<3);
234 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
236 register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
237 register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
239 for( i = 0; my_netbios_names(i); i++) {
240 const char *name = my_netbios_names(i);
241 int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
243 if(!strequal(global_myname(), name))
244 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
246 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
247 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
248 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
249 on subnet %s\n", name, subrec->subnet_name));
253 /****************************************************************************
254 Dump a copy of the workgroup database into the log file.
255 **************************************************************************/
257 void dump_workgroups(BOOL force_write)
259 struct subnet_record *subrec;
260 int debuglevel = force_write ? 0 : 4;
262 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
263 if (subrec->workgrouplist) {
264 struct work_record *work;
266 if( DEBUGLVL( debuglevel ) ) {
267 dbgtext( "dump_workgroups()\n " );
268 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
269 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
272 for (work = subrec->workgrouplist; work; work = work->next) {
273 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
274 work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
275 if (work->serverlist) {
276 struct server_record *servrec;
277 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
278 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
279 servrec->serv.name,
280 servrec->serv.type,
281 servrec->serv.comment ) );
289 /****************************************************************************
290 Expire any dead servers on all workgroups. If the workgroup has expired
291 remove it.
292 **************************************************************************/
294 void expire_workgroups_and_servers(time_t t)
296 struct subnet_record *subrec;
298 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
299 struct work_record *work;
300 struct work_record *nextwork;
302 for (work = subrec->workgrouplist; work; work = nextwork) {
303 nextwork = work->next;
304 expire_servers(work, t);
306 if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
307 ((t == -1) || (work->death_time < t))) {
308 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
309 work->work_group));
310 remove_workgroup_from_subnet(subrec, work);