Removed global_myworkgroup, global_myname, global_myscope. Added liberal
[kai/samba-autobuild/.git] / source3 / nmbd / nmbd_serverlistdb.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 int updatecount = 0;
30
31 /*******************************************************************
32   Remove all the servers in a work group.
33   ******************************************************************/
34
35 void remove_all_servers(struct work_record *work)
36 {
37   struct server_record *servrec;
38   struct server_record *nexts;
39
40   for (servrec = work->serverlist; servrec; servrec = nexts)
41   {
42     DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
43     nexts = servrec->next;
44
45     if (servrec->prev)
46       servrec->prev->next = servrec->next;
47     if (servrec->next)
48       servrec->next->prev = servrec->prev;
49
50     if (work->serverlist == servrec)
51       work->serverlist = servrec->next;
52
53     ZERO_STRUCTP(servrec);
54     SAFE_FREE(servrec);
55
56   }
57
58   work->subnet->work_changed = True;
59 }
60
61 /***************************************************************************
62   Add a server into the a workgroup serverlist.
63   **************************************************************************/
64
65 static void add_server_to_workgroup(struct work_record *work,
66                              struct server_record *servrec)
67 {
68   struct server_record *servrec2;
69
70   if (!work->serverlist)
71   {
72     work->serverlist = servrec;
73     servrec->prev = NULL;
74     servrec->next = NULL;
75     return;
76   }
77
78   for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next)
79     ;
80
81   servrec2->next = servrec;
82   servrec->next = NULL;
83   servrec->prev = servrec2;
84   work->subnet->work_changed = True;
85 }
86
87 /****************************************************************************
88   Find a server in a server list.
89   **************************************************************************/
90
91 struct server_record *find_server_in_workgroup(struct work_record *work, const char *name)
92 {
93   struct server_record *ret;
94   
95   for (ret = work->serverlist; ret; ret = ret->next)
96   {
97     if (strequal(ret->serv.name,name))
98       return ret;
99   }
100   return NULL;
101 }
102
103
104 /****************************************************************************
105   Remove a server entry from this workgroup.
106   ****************************************************************************/
107
108 void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
109 {
110   if (servrec->prev)
111     servrec->prev->next = servrec->next;
112   if (servrec->next)
113     servrec->next->prev = servrec->prev;
114
115   if (work->serverlist == servrec) 
116     work->serverlist = servrec->next; 
117
118   ZERO_STRUCTP(servrec);
119   SAFE_FREE(servrec);
120   work->subnet->work_changed = True;
121 }
122
123 /****************************************************************************
124   Create a server entry on this workgroup.
125   ****************************************************************************/
126
127 struct server_record *create_server_on_workgroup(struct work_record *work,
128                                                  const char *name,int servertype, 
129                                                  int ttl, const char *comment)
130 {
131   struct server_record *servrec;
132   
133   if (name[0] == '*')
134   {
135       DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
136              name));
137       return (NULL);
138   }
139   
140   if((servrec = find_server_in_workgroup(work, name)) != NULL)
141   {
142     DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
143 workgroup %s. This is a bug.\n", name, work->work_group));
144     return NULL;
145   }
146   
147   if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
148   {
149     DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
150     return NULL;
151   }
152
153   memset((char *)servrec,'\0',sizeof(*servrec));
154  
155   servrec->subnet = work->subnet;
156  
157   StrnCpy(servrec->serv.name,name,sizeof(servrec->serv.name)-1);
158   StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
159   strupper(servrec->serv.name);
160   servrec->serv.type  = servertype;
161
162   update_server_ttl(servrec, ttl);
163   
164   add_server_to_workgroup(work, servrec);
165       
166   DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
167 workgroup %s.\n", name,servertype,comment, work->work_group));
168  
169   work->subnet->work_changed = True;
170  
171   return(servrec);
172 }
173
174 /*******************************************************************
175  Update the ttl field of a server record.
176 *******************************************************************/
177
178 void update_server_ttl(struct server_record *servrec, int ttl)
179 {
180   if(ttl > lp_max_ttl())
181     ttl = lp_max_ttl();
182
183   if(is_myname(servrec->serv.name))
184     servrec->death_time = PERMANENT_TTL;
185   else
186     servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
187
188   servrec->subnet->work_changed = True;
189 }
190
191 /*******************************************************************
192   Expire old servers in the serverlist. A time of -1 indicates 
193   everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
194   This should only be called from expire_workgroups_and_servers().
195   ******************************************************************/
196
197 void expire_servers(struct work_record *work, time_t t)
198 {
199   struct server_record *servrec;
200   struct server_record *nexts;
201   
202   for (servrec = work->serverlist; servrec; servrec = nexts)
203   {
204     nexts = servrec->next;
205
206     if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t)))
207     {
208       DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
209       remove_server_from_workgroup(work, servrec);
210       work->subnet->work_changed = True;
211     }
212   }
213 }
214
215 /*******************************************************************
216  Decide if we should write out a server record for this server.
217  We return zero if we should not. Check if we've already written
218  out this server record from an earlier subnet.
219 ******************************************************************/
220
221 static uint32 write_this_server_name( struct subnet_record *subrec,
222                                       struct work_record *work,
223                                       struct server_record *servrec)
224 {
225   struct subnet_record *ssub;
226   struct work_record *iwork;
227
228   /* Go through all the subnets we have already seen. */
229   for (ssub = FIRST_SUBNET; ssub != subrec; ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub)) 
230   {
231     for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next)
232     {
233       if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL)
234       {
235         /*
236          * We have already written out this server record, don't
237          * do it again. This gives precedence to servers we have seen
238          * on the broadcast subnets over servers that may have been
239          * added via a sync on the unicast_subet.
240          *
241          * The correct way to do this is to have a serverlist file
242          * per subnet - this means changes to smbd as well. I may
243          * add this at a later date (JRA).
244          */
245
246         return 0;
247       }
248     }
249   }
250
251   return servrec->serv.type;
252 }
253
254 /*******************************************************************
255  Decide if we should write out a workgroup record for this workgroup.
256  We return zero if we should not. Don't write out lp_workgroup() (we've
257  already done it) and also don't write out a second workgroup record
258  on the unicast subnet that we've already written out on one of the
259  broadcast subnets.
260 ******************************************************************/
261
262 static uint32 write_this_workgroup_name( struct subnet_record *subrec, 
263                                          struct work_record *work)
264 {
265   struct subnet_record *ssub;
266
267   if(strequal(lp_workgroup(), work->work_group))
268     return 0;
269
270   /* This is a workgroup we have seen on a broadcast subnet. All
271      these have the same type. */
272
273   if(subrec != unicast_subnet)
274     return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
275
276   for(ssub = FIRST_SUBNET; ssub;  ssub = NEXT_SUBNET_EXCLUDING_UNICAST(ssub))
277   {
278     /* This is the unicast subnet so check if we've already written out
279        this subnet when we passed over the broadcast subnets. */
280
281     if(find_workgroup_on_subnet( ssub, work->work_group) != NULL)
282       return 0;
283   }
284
285   /* All workgroups on the unicast subnet (except our own, which we
286      have already written out) cannot be local. */
287
288   return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT);
289 }
290
291 /*******************************************************************
292   Write out the browse.dat file.
293   ******************************************************************/
294
295 void write_browse_list_entry(XFILE *fp, const char *name, uint32 rec_type,
296                 const char *local_master_browser_name, const char *description)
297 {
298         fstring tmp;
299
300         slprintf(tmp,sizeof(tmp)-1, "\"%s\"", name);
301         x_fprintf(fp, "%-25s ", tmp);
302         x_fprintf(fp, "%08x ", rec_type);
303         slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", local_master_browser_name);
304         x_fprintf(fp, "%-30s", tmp);
305         x_fprintf(fp, "\"%s\"\n", description);
306 }
307
308 void write_browse_list(time_t t, BOOL force_write)
309 {   
310   struct subnet_record *subrec;
311   struct work_record *work;
312   struct server_record *servrec;
313   pstring fname,fnamenew;
314   uint32 stype;
315   int i;
316   XFILE *fp;
317   BOOL list_changed = force_write;
318   static time_t lasttime = 0;
319     
320   /* Always dump if we're being told to by a signal. */
321   if(force_write == False)
322   {
323     if (!lasttime)
324       lasttime = t;
325     if (t - lasttime < 5)
326       return;
327   }
328
329   lasttime = t;
330
331   dump_workgroups(force_write);
332  
333   for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
334   {
335     if(subrec->work_changed)
336     {
337       list_changed = True;
338       break;
339     }
340   }
341
342   if(!list_changed)
343     return;
344
345   updatecount++;
346     
347   pstrcpy(fname,lp_lockdir());
348   trim_string(fname,NULL,"/");
349   pstrcat(fname,"/");
350   pstrcat(fname,SERVER_LIST);
351   pstrcpy(fnamenew,fname);
352   pstrcat(fnamenew,".");
353  
354   fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
355  
356   if (!fp)
357   {
358     DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
359               fnamenew,strerror(errno)));
360     return;
361   } 
362   
363   /*
364    * Write out a record for our workgroup. Use the record from the first
365    * subnet.
366    */
367
368   if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL)
369   { 
370     DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
371              lp_workgroup()));
372     x_fclose(fp);
373     return;
374   }
375
376   write_browse_list_entry(fp, work->work_group,
377      SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY,
378      work->local_master_browser_name, work->work_group);
379
380   /* 
381    * We need to do something special for our own names.
382    * This is due to the fact that we may be a local master browser on
383    * one of our broadcast subnets, and a domain master on the unicast
384    * subnet. We iterate over the subnets and only write out the name
385    * once.
386    */
387
388   for (i=0; my_netbios_names(i); i++)
389   {
390     stype = 0;
391     for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
392     {
393       if((work = find_workgroup_on_subnet( subrec, lp_workgroup() )) == NULL)
394         continue;
395       if((servrec = find_server_in_workgroup( work, my_netbios_names(i))) == NULL)
396         continue;
397
398       stype |= servrec->serv.type;
399     }
400
401     /* Output server details, plus what workgroup they're in. */
402     write_browse_list_entry(fp, my_netbios_names(i), stype,
403         string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH), lp_workgroup());
404   }
405       
406   for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) 
407   { 
408     subrec->work_changed = False;
409
410     for (work = subrec->workgrouplist; work ; work = work->next)
411     {
412       /* Write out a workgroup record for a workgroup. */
413       uint32 wg_type = write_this_workgroup_name( subrec, work);
414
415       if(wg_type)
416       {
417         write_browse_list_entry(fp, work->work_group, wg_type,
418                                 work->local_master_browser_name,
419                                 work->work_group);
420       }
421
422       /* Now write out any server records a workgroup may have. */
423
424       for (servrec = work->serverlist; servrec ; servrec = servrec->next)
425       {
426         uint32 serv_type;
427
428         /* We have already written our names here. */
429         if(is_myname(servrec->serv.name))
430           continue; 
431
432         serv_type = write_this_server_name(subrec, work, servrec);
433
434         if(serv_type)
435         {
436           /* Output server details, plus what workgroup they're in. */
437           write_browse_list_entry(fp, servrec->serv.name, serv_type,
438                                  servrec->serv.comment, work->work_group);
439         }
440       }
441     }
442   } 
443   
444   x_fclose(fp);
445   unlink(fname);
446   chmod(fnamenew,0644);
447   rename(fnamenew,fname);
448   DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));
449 }