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