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