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