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