Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[metze/samba/wip.git] / source3 / nmbd / nmbd_workgroupdb.c
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
8    
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.
13    
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.
18    
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.
22    
23 */
24
25 #include "includes.h"
26 #include "smb.h"
27
28 extern int ClientNMB;
29
30 extern pstring global_myname;
31 extern fstring global_myworkgroup;
32 extern char **my_netbios_names;
33 extern uint16 samba_nb_type;
34 extern struct in_addr ipzero;
35
36 int workgroup_count = 0; /* unique index key: one for each workgroup */
37
38 /****************************************************************************
39   Add a workgroup into the list.
40   **************************************************************************/
41
42 static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
43 {
44         work->subnet = subrec;
45         DLIST_ADD(subrec->workgrouplist, work);
46         subrec->work_changed = True;
47 }
48
49 /****************************************************************************
50   Create an empty workgroup.
51   **************************************************************************/
52
53 static struct work_record *create_workgroup(const char *name, int ttl)
54 {
55   struct work_record *work;
56   struct subnet_record *subrec;
57   int t = -1;
58   
59   if((work = (struct work_record *)malloc(sizeof(*work))) == NULL)
60   {
61     DEBUG(0,("create_workgroup: malloc fail !\n"));
62     return NULL;
63   }
64   memset((char *)work, '\0', sizeof(*work));
65  
66   StrnCpy(work->work_group,name,sizeof(work->work_group)-1);
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); 
84            subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
85   {
86     struct work_record *w;
87     for (w = subrec->workgrouplist; w && t == -1; w = w->next)
88     {
89       if (strequal(w->work_group, work->work_group))
90         t = w->token;
91     }
92   }
93   
94   if (t == -1)
95     work->token = ++workgroup_count;
96   else
97     work->token = t;
98   
99   /* No known local master browser as yet. */
100   *work->local_master_browser_name = '\0';
101
102   /* No known domain master browser as yet. */
103   *work->dmb_name.name = '\0';
104   putip((char *)&work->dmb_addr, &ipzero);
105
106   /* WfWg  uses 01040b01 */
107   /* Win95 uses 01041501 */
108   /* NTAS  uses ???????? */
109   work->ElectionCriterion  = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8); 
110   work->ElectionCriterion |= (lp_os_level() << 24);
111   if (lp_domain_master())
112     work->ElectionCriterion |= 0x80;
113   
114   return work;
115 }
116
117 /*******************************************************************
118   Remove a workgroup.
119   ******************************************************************/
120
121 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec, 
122                                      struct work_record *work)
123 {
124   struct work_record *ret_work = NULL;
125   
126   DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
127   
128   ret_work = work->next;
129
130   remove_all_servers(work);
131   
132   if (!work->serverlist)
133   {
134     if (work->prev)
135       work->prev->next = work->next;
136     if (work->next)
137       work->next->prev = work->prev;
138   
139     if (subrec->workgrouplist == work)
140       subrec->workgrouplist = work->next; 
141   
142     ZERO_STRUCTP(work);
143     SAFE_FREE(work);
144   }
145   
146   subrec->work_changed = True;
147
148   return ret_work;
149 }
150
151
152 /****************************************************************************
153   Find a workgroup in the workgroup list of a subnet.
154   **************************************************************************/
155
156 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec, 
157                                              const char *name)
158 {
159   struct work_record *ret;
160   
161   DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
162             name, subrec->subnet_name));
163   
164   for (ret = subrec->workgrouplist; ret; ret = ret->next)
165   {
166     if (!strcmp(ret->work_group,name))
167     {
168       DEBUGADD(4, ("found.\n"));
169       return(ret);
170     }
171   }
172   DEBUGADD(4, ("not found.\n"));
173   return NULL;
174 }
175
176 /****************************************************************************
177   Create a workgroup in the workgroup list of the subnet.
178   **************************************************************************/
179
180 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
181                                                const char *name, int ttl)
182 {
183   struct work_record *work = NULL;
184
185   DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
186            name, subrec->subnet_name));
187   
188   if ((work = create_workgroup(name, ttl)))
189   {
190     add_workgroup(subrec, work);
191
192     subrec->work_changed = True;
193
194     return(work);
195   }
196
197   return NULL;
198 }
199
200 /****************************************************************************
201   Update a workgroup ttl.
202   **************************************************************************/
203
204 void update_workgroup_ttl(struct work_record *work, int ttl)
205 {
206   if(work->death_time != PERMANENT_TTL)
207     work->death_time = time(NULL)+(ttl*3);
208   work->subnet->work_changed = True;
209 }
210
211 /****************************************************************************
212  Fail function called if we cannot register the WORKGROUP<0> and
213  WORKGROUP<1e> names on the net.
214 **************************************************************************/
215      
216 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
217                           struct nmb_name *nmbname)
218 {  
219   DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
220             nmb_namestr(nmbname), subrec->subnet_name));
221 }  
222
223 /****************************************************************************
224  If the workgroup is our primary workgroup, add the required names to it.
225 **************************************************************************/
226
227 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
228 {
229   int i;
230
231   if(!strequal(global_myworkgroup, work->work_group))
232     return;
233
234   /* If this is a broadcast subnet then start elections on it
235      if we are so configured. */
236
237   if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
238       (subrec != wins_server_subnet) && lp_preferred_master() &&
239       lp_local_master())
240   {
241     DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
242 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
243     work->needelection = True;
244     work->ElectionCriterion |= (1<<3);
245   }  
246   
247   /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
248
249   register_name(subrec,global_myworkgroup,0x0,samba_nb_type|NB_GROUP,
250                 NULL,
251                 fail_register,NULL);
252      
253   register_name(subrec,global_myworkgroup,0x1e,samba_nb_type|NB_GROUP,
254                 NULL,
255                 fail_register,NULL);
256      
257   for( i = 0; my_netbios_names[i]; i++)
258   {
259     char *name = my_netbios_names[i];
260     int stype = lp_default_server_announce() | (lp_local_master() ?
261                         SV_TYPE_POTENTIAL_BROWSER : 0 );
262    
263     if(!strequal(global_myname, name))
264         stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|
265                    SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
266    
267     create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY,
268                                PERMANENT_TTL, 
269                                string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
270     DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
271 on subnet %s\n", name, subrec->subnet_name));
272   }
273
274
275 /****************************************************************************
276   Dump a copy of the workgroup database into the log file.
277   **************************************************************************/
278
279 void dump_workgroups(BOOL force_write)
280 {
281   struct subnet_record *subrec;
282   int debuglevel = force_write ? 0 : 4;
283  
284   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
285   {
286     if (subrec->workgrouplist)
287     {
288       struct work_record *work;
289
290       if( DEBUGLVL( debuglevel ) )
291       {
292         dbgtext( "dump_workgroups()\n " );
293         dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
294         dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
295       }
296           
297       for (work = subrec->workgrouplist; work; work = work->next)
298       {
299         DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n",
300                                 work->work_group,
301                                 work->token, 
302                                *work->local_master_browser_name
303                               ? work->local_master_browser_name : "UNKNOWN" ) );
304         if (work->serverlist)
305         {
306           struct server_record *servrec;                  
307           for (servrec = work->serverlist; servrec; servrec = servrec->next)
308           {
309             DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
310                                     servrec->serv.name,
311                                     servrec->serv.type,
312                                     servrec->serv.comment ) );
313           }
314         }
315       }
316     }
317   }
318 }
319
320 /****************************************************************************
321   Expire any dead servers on all workgroups. If the workgroup has expired
322   remove it.
323   **************************************************************************/
324
325 void expire_workgroups_and_servers(time_t t)
326 {
327   struct subnet_record *subrec;
328    
329   for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
330   {
331     struct work_record *work;
332     struct work_record *nextwork;
333
334     for (work = subrec->workgrouplist; work; work = nextwork)
335     {
336       nextwork = work->next;
337       expire_servers(work, t);
338
339       if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) && 
340                      ((t == -1) || (work->death_time < t)))
341       {
342         DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
343                   work->work_group));
344         remove_workgroup_from_subnet(subrec, work);
345       }
346     }
347   }
348 }