sync 3.0 into HEAD for the last time
[samba.git] / source / nmbd / nmbd_workgroupdb.c
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
7    
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.
12    
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.
17    
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.
21    
22 */
23
24 #include "includes.h"
25
26 extern int ClientNMB;
27
28 extern uint16 samba_nb_type;
29
30 int workgroup_count = 0; /* unique index key: one for each workgroup */
31
32 /****************************************************************************
33   Add a workgroup into the list.
34 **************************************************************************/
35
36 static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
37 {
38         work->subnet = subrec;
39         DLIST_ADD(subrec->workgrouplist, work);
40         subrec->work_changed = True;
41 }
42
43 /****************************************************************************
44   Create an empty workgroup.
45 **************************************************************************/
46
47 static struct work_record *create_workgroup(const char *name, int ttl)
48 {
49         struct work_record *work;
50         struct subnet_record *subrec;
51         int t = -1;
52   
53         if((work = (struct work_record *)malloc(sizeof(*work))) == NULL) {
54                 DEBUG(0,("create_workgroup: malloc fail !\n"));
55                 return NULL;
56         }
57         memset((char *)work, '\0', sizeof(*work));
58  
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);
66         }
67         work->serverlist = NULL;
68   
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;
78   
79         work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
80
81         /* Make sure all token representations of workgroups are unique. */
82   
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;
88                 }
89         }
90   
91         if (t == -1)
92                 work->token = ++workgroup_count;
93         else
94                 work->token = t;
95   
96         /* No known local master browser as yet. */
97         *work->local_master_browser_name = '\0';
98
99         /* No known domain master browser as yet. */
100         *work->dmb_name.name = '\0';
101         zero_ip(&work->dmb_addr);
102
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;
110   
111         return work;
112 }
113
114 /*******************************************************************
115   Remove a workgroup.
116 ******************************************************************/
117
118 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec, 
119                                      struct work_record *work)
120 {
121         struct work_record *ret_work = NULL;
122   
123         DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
124   
125         ret_work = work->next;
126
127         remove_all_servers(work);
128   
129         if (!work->serverlist) {
130                 if (work->prev)
131                         work->prev->next = work->next;
132                 if (work->next)
133                         work->next->prev = work->prev;
134   
135                 if (subrec->workgrouplist == work)
136                         subrec->workgrouplist = work->next; 
137   
138                 ZERO_STRUCTP(work);
139                 SAFE_FREE(work);
140         }
141   
142         subrec->work_changed = True;
143
144         return ret_work;
145 }
146
147 /****************************************************************************
148   Find a workgroup in the workgroup list of a subnet.
149 **************************************************************************/
150
151 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec, 
152                                              const char *name)
153 {
154         struct work_record *ret;
155   
156         DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
157                 name, subrec->subnet_name));
158   
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);
163                 }
164         }
165         DEBUGADD(4, ("not found.\n"));
166         return NULL;
167 }
168
169 /****************************************************************************
170   Create a workgroup in the workgroup list of the subnet.
171 **************************************************************************/
172
173 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
174                                                const char *name, int ttl)
175 {
176         struct work_record *work = NULL;
177
178         DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
179                 name, subrec->subnet_name));
180   
181         if ((work = create_workgroup(name, ttl))) {
182                 add_workgroup(subrec, work);
183                 subrec->work_changed = True;
184                 return(work);
185         }
186
187         return NULL;
188 }
189
190 /****************************************************************************
191   Update a workgroup ttl.
192 **************************************************************************/
193
194 void update_workgroup_ttl(struct work_record *work, int ttl)
195 {
196         if(work->death_time != PERMANENT_TTL)
197                 work->death_time = time(NULL)+(ttl*3);
198         work->subnet->work_changed = True;
199 }
200
201 /****************************************************************************
202  Fail function called if we cannot register the WORKGROUP<0> and
203  WORKGROUP<1e> names on the net.
204 **************************************************************************/
205      
206 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
207                           struct nmb_name *nmbname)
208 {  
209         DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
210                 nmb_namestr(nmbname), subrec->subnet_name));
211 }  
212
213 /****************************************************************************
214  If the workgroup is our primary workgroup, add the required names to it.
215 **************************************************************************/
216
217 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
218 {
219         int i;
220
221         if(!strnequal(lp_workgroup(), work->work_group,sizeof(nstring)-1))
222                 return;
223
224         /* If this is a broadcast subnet then start elections on it if we are so configured. */
225
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);
232         }
233   
234         /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
235
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);
238
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 );
242    
243                 if(!strequal(global_myname(), name))
244                         stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
245    
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));
250         }
251
252
253 /****************************************************************************
254   Dump a copy of the workgroup database into the log file.
255   **************************************************************************/
256
257 void dump_workgroups(BOOL force_write)
258 {
259         struct subnet_record *subrec;
260         int debuglevel = force_write ? 0 : 4;
261  
262         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
263                 if (subrec->workgrouplist) {
264                         struct work_record *work;
265
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) );
270                         }
271
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 ) );
282                                         }
283                                 }
284                         }
285                 }
286         }
287 }
288
289 /****************************************************************************
290   Expire any dead servers on all workgroups. If the workgroup has expired
291   remove it.
292   **************************************************************************/
293
294 void expire_workgroups_and_servers(time_t t)
295 {
296         struct subnet_record *subrec;
297    
298         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
299                 struct work_record *work;
300                 struct work_record *nextwork;
301
302                 for (work = subrec->workgrouplist; work; work = nextwork) {
303                         nextwork = work->next;
304                         expire_servers(work, t);
305
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);
311                         }
312                 }
313         }
314 }