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