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