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