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