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