s4:dsdb:acl_read: Implement "List Object" mode feature
[Samba.git] / source3 / nmbd / nmbd_serverlistdb.c
blobe7cb36a2de26b1e646de92c51e265eaae560e570
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
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.
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.
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/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "../librpc/gen_ndr/svcctl.h"
26 #include "nmbd/nmbd.h"
28 int updatecount = 0;
30 /*******************************************************************
31 Remove all the servers in a work group.
32 ******************************************************************/
34 void remove_all_servers(struct work_record *work)
36 struct server_record *servrec;
37 struct server_record *nexts;
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);
47 work->subnet->work_changed = True;
50 /***************************************************************************
51 Add a server into the a workgroup serverlist.
52 **************************************************************************/
54 static void add_server_to_workgroup(struct work_record *work,
55 struct server_record *servrec)
57 DLIST_ADD_END(work->serverlist, servrec);
58 work->subnet->work_changed = True;
61 /****************************************************************************
62 Find a server in a server list.
63 **************************************************************************/
65 struct server_record *find_server_in_workgroup(struct work_record *work, const char *name)
67 struct server_record *ret;
69 for (ret = work->serverlist; ret; ret = ret->next) {
70 if (strequal(ret->serv.name,name))
71 return ret;
73 return NULL;
77 /****************************************************************************
78 Remove a server entry from this workgroup.
79 ****************************************************************************/
81 void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
83 DLIST_REMOVE(work->serverlist, servrec);
84 ZERO_STRUCTP(servrec);
85 SAFE_FREE(servrec);
86 work->subnet->work_changed = True;
89 /****************************************************************************
90 Create a server entry on this workgroup.
91 ****************************************************************************/
93 struct server_record *create_server_on_workgroup(struct work_record *work,
94 const char *name,int servertype,
95 int ttl, const char *comment)
97 struct server_record *servrec;
99 if (name[0] == '*') {
100 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
101 name));
102 return (NULL);
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;
111 if((servrec = SMB_MALLOC_P(struct server_record)) == NULL) {
112 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
113 return NULL;
116 memset((char *)servrec,'\0',sizeof(*servrec));
118 servrec->subnet = work->subnet;
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;
127 servrec->serv.type = servertype;
129 update_server_ttl(servrec, ttl);
131 add_server_to_workgroup(work, servrec);
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));
136 return(servrec);
139 /*******************************************************************
140 Update the ttl field of a server record.
141 *******************************************************************/
143 void update_server_ttl(struct server_record *servrec, int ttl)
145 if(ttl > lp_max_ttl())
146 ttl = lp_max_ttl();
148 if(is_myname(servrec->serv.name))
149 servrec->death_time = PERMANENT_TTL;
150 else
151 servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
154 /*******************************************************************
155 Expire old servers in the serverlist. A time of -1 indicates
156 everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
157 This should only be called from expire_workgroups_and_servers().
158 ******************************************************************/
160 void expire_servers(struct work_record *work, time_t t)
162 struct server_record *servrec;
163 struct server_record *nexts;
165 for (servrec = work->serverlist; servrec; servrec = nexts) {
166 nexts = servrec->next;
168 if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t))) {
169 DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
170 remove_server_from_workgroup(work, servrec);
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 ******************************************************************/
181 static uint32_t write_this_server_name( struct subnet_record *subrec,
182 struct work_record *work,
183 struct server_record *servrec)
185 struct subnet_record *ssub;
186 struct work_record *iwork;
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) {
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.
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).
203 return 0;
208 return servrec->serv.type;
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 ******************************************************************/
219 static uint32_t write_this_workgroup_name( struct subnet_record *subrec,
220 struct work_record *work)
222 struct subnet_record *ssub;
224 if(strequal(lp_workgroup(), work->work_group))
225 return 0;
227 /* This is a workgroup we have seen on a broadcast subnet. All
228 these have the same type. */
230 if(subrec != unicast_subnet)
231 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
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. */
237 if(find_workgroup_on_subnet( ssub, work->work_group) != NULL)
238 return 0;
241 /* All workgroups on the unicast subnet (except our own, which we
242 have already written out) cannot be local. */
244 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT);
247 /*******************************************************************
248 Write out the browse.dat file.
249 ******************************************************************/
251 void write_browse_list_entry(FILE *fp, const char *name, uint32_t rec_type,
252 const char *local_master_browser_name, const char *description)
254 fstring tmp;
256 slprintf(tmp,sizeof(tmp)-1, "\"%s\"", name);
257 fprintf(fp, "%-25s ", tmp);
258 fprintf(fp, "%08x ", rec_type);
259 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", local_master_browser_name);
260 fprintf(fp, "%-30s", tmp);
261 fprintf(fp, "\"%s\"\n", description);
264 void write_browse_list(time_t t, bool force_write)
266 struct subnet_record *subrec;
267 struct work_record *work;
268 struct server_record *servrec;
269 char *fname;
270 char *fnamenew;
271 uint32_t stype;
272 int i;
273 int fd;
274 FILE *fp;
275 bool list_changed = force_write;
276 static time_t lasttime = 0;
277 TALLOC_CTX *ctx = talloc_tos();
278 const struct loadparm_substitution *lp_sub =
279 loadparm_s3_global_substitution();
281 /* Always dump if we're being told to by a signal. */
282 if(force_write == False) {
283 if (!lasttime)
284 lasttime = t;
285 if (t - lasttime < 5)
286 return;
289 lasttime = t;
291 dump_workgroups(force_write);
293 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
294 if(subrec->work_changed) {
295 list_changed = True;
296 break;
300 if(!list_changed)
301 return;
303 updatecount++;
305 fname = cache_path(talloc_tos(), SERVER_LIST);
306 if (!fname) {
307 return;
309 fnamenew = talloc_asprintf(ctx, "%s.",
310 fname);
311 if (!fnamenew) {
312 talloc_free(fname);
313 return;
316 fd = open(fnamenew, O_WRONLY|O_CREAT|O_TRUNC, 0644);
317 if (fd == -1) {
318 DBG_ERR("Can't open file %s: %s\n", fnamenew,
319 strerror(errno));
320 talloc_free(fnamenew);
321 talloc_free(fname);
322 return;
325 fp = fdopen(fd, "w");
326 if (!fp) {
327 DBG_ERR("fdopen failed: %s\n", strerror(errno));
328 close(fd);
329 talloc_free(fnamenew);
330 talloc_free(fname);
331 return;
333 fd = -1;
336 * Write out a record for our workgroup. Use the record from the first
337 * subnet.
340 if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL) {
341 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
342 lp_workgroup()));
343 fclose(fp);
344 talloc_free(fnamenew);
345 talloc_free(fname);
346 return;
349 write_browse_list_entry(fp, work->work_group,
350 SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY,
351 work->local_master_browser_name, work->work_group);
354 * We need to do something special for our own names.
355 * This is due to the fact that we may be a local master browser on
356 * one of our broadcast subnets, and a domain master on the unicast
357 * subnet. We iterate over the subnets and only write out the name
358 * once.
361 for (i=0; my_netbios_names(i); i++) {
362 stype = 0;
363 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
364 if((work = find_workgroup_on_subnet( subrec, lp_workgroup() )) == NULL)
365 continue;
366 if((servrec = find_server_in_workgroup( work, my_netbios_names(i))) == NULL)
367 continue;
369 stype |= servrec->serv.type;
372 /* Output server details, plus what workgroup they're in. */
373 write_browse_list_entry(fp, my_netbios_names(i), stype,
374 string_truncate(lp_server_string(talloc_tos(), lp_sub), MAX_SERVER_STRING_LENGTH), lp_workgroup());
377 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
378 subrec->work_changed = False;
380 for (work = subrec->workgrouplist; work ; work = work->next) {
381 /* Write out a workgroup record for a workgroup. */
382 uint32_t wg_type = write_this_workgroup_name( subrec, work);
384 if(wg_type) {
385 write_browse_list_entry(fp, work->work_group, wg_type,
386 work->local_master_browser_name,
387 work->work_group);
390 /* Now write out any server records a workgroup may have. */
392 for (servrec = work->serverlist; servrec ; servrec = servrec->next) {
393 uint32_t serv_type;
395 /* We have already written our names here. */
396 if(is_myname(servrec->serv.name))
397 continue;
399 serv_type = write_this_server_name(subrec, work, servrec);
400 if(serv_type) {
401 /* Output server details, plus what workgroup they're in. */
402 write_browse_list_entry(fp, servrec->serv.name, serv_type,
403 servrec->serv.comment, work->work_group);
409 fclose(fp);
410 unlink(fname);
411 chmod(fnamenew,0644);
412 rename(fnamenew,fname);
413 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));
414 talloc_free(fnamenew);
415 talloc_free(fname);