This is *not* a big change (although it looks like one).
[ira/wip.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 pstring myname;
33 extern fstring myworkgroup;
34 extern char **my_netbios_names;
35
36 int updatecount = 0;
37
38 /*******************************************************************
39   Remove all the servers in a work group.
40   ******************************************************************/
41
42 void remove_all_servers(struct work_record *work)
43 {
44   struct server_record *servrec;
45   struct server_record *nexts;
46
47   for (servrec = work->serverlist; servrec; servrec = nexts)
48   {
49     DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
50     nexts = servrec->next;
51
52     if (servrec->prev)
53       servrec->prev->next = servrec->next;
54     if (servrec->next)
55       servrec->next->prev = servrec->prev;
56
57     if (work->serverlist == servrec)
58       work->serverlist = servrec->next;
59
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   free((char *)servrec);
125   work->subnet->work_changed = True;
126 }
127
128 /****************************************************************************
129   Create a server entry on this workgroup.
130   ****************************************************************************/
131
132 struct server_record *create_server_on_workgroup(struct work_record *work,
133                                                  char *name,int servertype, 
134                                                  int ttl,char *comment)
135 {
136   struct server_record *servrec;
137   
138   if (name[0] == '*')
139   {
140       DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
141              name));
142       return (NULL);
143   }
144   
145   if((servrec = find_server_in_workgroup(work, name)) != NULL)
146   {
147     DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
148 workgroup %s. This is a bug.\n", name, work->work_group));
149     return NULL;
150   }
151   
152   if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
153   {
154     DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
155     return NULL;
156   }
157
158   bzero((char *)servrec,sizeof(*servrec));
159  
160   servrec->subnet = work->subnet;
161  
162   StrnCpy(servrec->serv.name,name,sizeof(servrec->serv.name)-1);
163   StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
164   strupper(servrec->serv.name);
165   servrec->serv.type  = servertype;
166
167   update_server_ttl(servrec, ttl);
168   
169   add_server_to_workgroup(work, servrec);
170       
171   DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
172 workgroup %s.\n", name,servertype,comment, work->work_group));
173  
174   work->subnet->work_changed = True;
175  
176   return(servrec);
177 }
178
179 /*******************************************************************
180  Update the ttl field of a server record.
181 *******************************************************************/
182
183 void update_server_ttl(struct server_record *servrec, int ttl)
184 {
185   if(ttl > lp_max_ttl())
186     ttl = lp_max_ttl();
187
188   if(is_myname(servrec->serv.name))
189     servrec->death_time = PERMANENT_TTL;
190   else
191     servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
192
193   servrec->subnet->work_changed = True;
194 }
195
196 /*******************************************************************
197   Expire old servers in the serverlist. A time of -1 indicates 
198   everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
199   This should only be called from expire_workgroups_and_servers().
200   ******************************************************************/
201
202 void expire_servers(struct work_record *work, time_t t)
203 {
204   struct server_record *servrec;
205   struct server_record *nexts;
206   
207   for (servrec = work->serverlist; servrec; servrec = nexts)
208   {
209     nexts = servrec->next;
210
211     if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t)))
212     {
213       DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
214       remove_server_from_workgroup(work, servrec);
215       work->subnet->work_changed = True;
216     }
217   }
218 }
219
220 /*******************************************************************
221  Decide if we should write out a server record for this server.
222  We return zero if we should not. Check if we've already written
223  out this server record from an earlier subnet.
224 ******************************************************************/
225
226 static uint32 write_this_server_name( struct subnet_record *subrec,
227                                       struct work_record *work,
228                                       struct server_record *servrec)
229 {
230   struct subnet_record *ssub;
231   struct work_record *iwork;
232   struct server_record *sserv;
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((sserv = 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 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(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   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   lasttime = t;
338   updatecount++;
339     
340   pstrcpy(fname,lp_lockdir());
341   trim_string(fname,NULL,"/");
342   strcat(fname,"/");
343   strcat(fname,SERVER_LIST);
344   pstrcpy(fnamenew,fname);
345   strcat(fnamenew,".");
346  
347   fp = fopen(fnamenew,"w");
348  
349   if (!fp)
350   {
351     DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
352               fnamenew,strerror(errno)));
353     return;
354   } 
355   
356   /*
357    * Write out a record for our workgroup. Use the record from the first
358    * subnet.
359    */
360
361   if((work = find_workgroup_on_subnet(FIRST_SUBNET, myworkgroup)) == NULL)
362   { 
363     DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
364              myworkgroup));
365     fclose(fp);
366     return;
367   }
368
369   sprintf(tmp, "\"%s\"", work->work_group);
370   fprintf(fp, "%-25s ", tmp);
371   fprintf(fp, "%08x ", SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
372   sprintf(tmp, "\"%s\" ", work->local_master_browser_name);
373   fprintf(fp, "%-30s", tmp);
374   fprintf(fp, "\"%s\"\n", work->work_group);
375
376   /* 
377    * We need to do something special for our own names.
378    * This is due to the fact that we may be a local master browser on
379    * one of our broadcast subnets, and a domain master on the unicast
380    * subnet. We iterate over the subnets and only write out the name
381    * once.
382    */
383
384   for (i=0; my_netbios_names[i]; i++)
385   {
386     stype = 0;
387     for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
388     {
389       if((work = find_workgroup_on_subnet( subrec, myworkgroup )) == NULL)
390         continue;
391       if((servrec = find_server_in_workgroup( work, my_netbios_names[i])) == NULL)
392         continue;
393
394       stype |= servrec->serv.type;
395     }
396
397     /* Output server details, plus what workgroup they're in. */
398     sprintf(tmp, "\"%s\"", my_netbios_names[i]);
399     fprintf(fp, "%-25s ", tmp);
400     fprintf(fp, "%08x ", stype);
401     sprintf(tmp, "\"%s\" ", lp_serverstring());
402     fprintf(fp, "%-30s", tmp);
403     fprintf(fp, "\"%s\"\n", 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         sprintf(tmp, "\"%s\"", work->work_group);
418         fprintf(fp, "%-25s ", tmp);
419
420         fprintf(fp, "%08x ", wg_type);
421         sprintf(tmp, "\"%s\" ", work->local_master_browser_name);
422         fprintf(fp, "%-30s", tmp);
423         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           sprintf(tmp, "\"%s\"", servrec->serv.name);
442           fprintf(fp, "%-25s ", tmp);
443           fprintf(fp, "%08x ", serv_type);
444           sprintf(tmp, "\"%s\" ", servrec->serv.comment);
445           fprintf(fp, "%-30s", tmp);
446           fprintf(fp, "\"%s\"\n", work->work_group);
447         }
448       }
449     }
450   } 
451   
452   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 }