converted the passdb smbpasswd implementation to using talloc
[Samba/gbeck.git] / source / nmbd / nmbd_serverlistdb.c
blob41009bc68f0016fbcf899a4ac5c49fb05138629b
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
26 #include "smb.h"
28 extern int ClientNMB;
30 extern int DEBUGLEVEL;
32 extern fstring global_myworkgroup;
33 extern char **my_netbios_names;
35 int updatecount = 0;
37 /*******************************************************************
38 Remove all the servers in a work group.
39 ******************************************************************/
41 void remove_all_servers(struct work_record *work)
43 struct server_record *servrec;
44 struct server_record *nexts;
46 for (servrec = work->serverlist; servrec; servrec = nexts)
48 DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
49 nexts = servrec->next;
51 if (servrec->prev)
52 servrec->prev->next = servrec->next;
53 if (servrec->next)
54 servrec->next->prev = servrec->prev;
56 if (work->serverlist == servrec)
57 work->serverlist = servrec->next;
59 ZERO_STRUCTP(servrec);
60 free((char *)servrec);
64 work->subnet->work_changed = True;
67 /***************************************************************************
68 Add a server into the a workgroup serverlist.
69 **************************************************************************/
71 static void add_server_to_workgroup(struct work_record *work,
72 struct server_record *servrec)
74 struct server_record *servrec2;
76 if (!work->serverlist)
78 work->serverlist = servrec;
79 servrec->prev = NULL;
80 servrec->next = NULL;
81 return;
84 for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next)
87 servrec2->next = servrec;
88 servrec->next = NULL;
89 servrec->prev = servrec2;
90 work->subnet->work_changed = True;
93 /****************************************************************************
94 Find a server in a server list.
95 **************************************************************************/
97 struct server_record *find_server_in_workgroup(struct work_record *work, char *name)
99 struct server_record *ret;
101 for (ret = work->serverlist; ret; ret = ret->next)
103 if (strequal(ret->serv.name,name))
104 return ret;
106 return NULL;
110 /****************************************************************************
111 Remove a server entry from this workgroup.
112 ****************************************************************************/
114 void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
116 if (servrec->prev)
117 servrec->prev->next = servrec->next;
118 if (servrec->next)
119 servrec->next->prev = servrec->prev;
121 if (work->serverlist == servrec)
122 work->serverlist = servrec->next;
124 ZERO_STRUCTP(servrec);
125 free((char *)servrec);
126 work->subnet->work_changed = True;
129 /****************************************************************************
130 Create a server entry on this workgroup.
131 ****************************************************************************/
133 struct server_record *create_server_on_workgroup(struct work_record *work,
134 char *name,int servertype,
135 int ttl,char *comment)
137 struct server_record *servrec;
139 if (name[0] == '*')
141 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
142 name));
143 return (NULL);
146 if((servrec = find_server_in_workgroup(work, name)) != NULL)
148 DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
149 workgroup %s. This is a bug.\n", name, work->work_group));
150 return NULL;
153 if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
155 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
156 return NULL;
159 memset((char *)servrec,'\0',sizeof(*servrec));
161 servrec->subnet = work->subnet;
163 StrnCpy(servrec->serv.name,name,sizeof(servrec->serv.name)-1);
164 StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
165 strupper(servrec->serv.name);
166 servrec->serv.type = servertype;
168 update_server_ttl(servrec, ttl);
170 add_server_to_workgroup(work, servrec);
172 DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
173 workgroup %s.\n", name,servertype,comment, work->work_group));
175 work->subnet->work_changed = True;
177 return(servrec);
180 /*******************************************************************
181 Update the ttl field of a server record.
182 *******************************************************************/
184 void update_server_ttl(struct server_record *servrec, int ttl)
186 if(ttl > lp_max_ttl())
187 ttl = lp_max_ttl();
189 if(is_myname(servrec->serv.name))
190 servrec->death_time = PERMANENT_TTL;
191 else
192 servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
194 servrec->subnet->work_changed = True;
197 /*******************************************************************
198 Expire old servers in the serverlist. A time of -1 indicates
199 everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
200 This should only be called from expire_workgroups_and_servers().
201 ******************************************************************/
203 void expire_servers(struct work_record *work, time_t t)
205 struct server_record *servrec;
206 struct server_record *nexts;
208 for (servrec = work->serverlist; servrec; servrec = nexts)
210 nexts = servrec->next;
212 if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t)))
214 DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
215 remove_server_from_workgroup(work, servrec);
216 work->subnet->work_changed = True;
221 /*******************************************************************
222 Decide if we should write out a server record for this server.
223 We return zero if we should not. Check if we've already written
224 out this server record from an earlier subnet.
225 ******************************************************************/
227 static uint32 write_this_server_name( struct subnet_record *subrec,
228 struct work_record *work,
229 struct server_record *servrec)
231 struct subnet_record *ssub;
232 struct work_record *iwork;
234 /* Go through all the subnets we have already seen. */
235 for (ssub = FIRST_SUBNET; ssub != subrec; ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub))
237 for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next)
239 if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL)
242 * We have already written out this server record, don't
243 * do it again. This gives precedence to servers we have seen
244 * on the broadcast subnets over servers that may have been
245 * added via a sync on the unicast_subet.
247 * The correct way to do this is to have a serverlist file
248 * per subnet - this means changes to smbd as well. I may
249 * add this at a later date (JRA).
252 return 0;
257 return servrec->serv.type;
260 /*******************************************************************
261 Decide if we should write out a workgroup record for this workgroup.
262 We return zero if we should not. Don't write out global_myworkgroup (we've
263 already done it) and also don't write out a second workgroup record
264 on the unicast subnet that we've already written out on one of the
265 broadcast subnets.
266 ******************************************************************/
268 static uint32 write_this_workgroup_name( struct subnet_record *subrec,
269 struct work_record *work)
271 struct subnet_record *ssub;
273 if(strequal(global_myworkgroup, work->work_group))
274 return 0;
276 /* This is a workgroup we have seen on a broadcast subnet. All
277 these have the same type. */
279 if(subrec != unicast_subnet)
280 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
282 for(ssub = FIRST_SUBNET; ssub; ssub = NEXT_SUBNET_EXCLUDING_UNICAST(ssub))
284 /* This is the unicast subnet so check if we've already written out
285 this subnet when we passed over the broadcast subnets. */
287 if(find_workgroup_on_subnet( ssub, work->work_group) != NULL)
288 return 0;
291 /* All workgroups on the unicast subnet (except our own, which we
292 have already written out) cannot be local. */
294 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT);
297 /*******************************************************************
298 Write out the browse.dat file.
299 ******************************************************************/
301 void write_browse_list(time_t t, BOOL force_write)
303 struct subnet_record *subrec;
304 struct work_record *work;
305 struct server_record *servrec;
306 pstring fname,fnamenew;
307 uint32 stype;
308 fstring tmp;
309 int i;
310 FILE *fp;
311 BOOL list_changed = force_write;
312 static time_t lasttime = 0;
314 /* Always dump if we're being told to by a signal. */
315 if(force_write == False)
317 if (!lasttime)
318 lasttime = t;
319 if (t - lasttime < 5)
320 return;
323 lasttime = t;
325 dump_workgroups(force_write);
327 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
329 if(subrec->work_changed)
331 list_changed = True;
332 break;
336 if(!list_changed)
337 return;
339 updatecount++;
341 pstrcpy(fname,lp_lockdir());
342 trim_string(fname,NULL,"/");
343 pstrcat(fname,"/");
344 pstrcat(fname,SERVER_LIST);
345 pstrcpy(fnamenew,fname);
346 pstrcat(fnamenew,".");
348 fp = sys_fopen(fnamenew,"w");
350 if (!fp)
352 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
353 fnamenew,strerror(errno)));
354 return;
358 * Write out a record for our workgroup. Use the record from the first
359 * subnet.
362 if((work = find_workgroup_on_subnet(FIRST_SUBNET, global_myworkgroup)) == NULL)
364 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
365 global_myworkgroup));
366 fclose(fp);
367 return;
370 slprintf(tmp,sizeof(tmp)-1, "\"%s\"", work->work_group);
371 fprintf(fp, "%-25s ", tmp);
372 fprintf(fp, "%08x ", SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
373 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
374 fprintf(fp, "%-30s", tmp);
375 fprintf(fp, "\"%s\"\n", work->work_group);
378 * We need to do something special for our own names.
379 * This is due to the fact that we may be a local master browser on
380 * one of our broadcast subnets, and a domain master on the unicast
381 * subnet. We iterate over the subnets and only write out the name
382 * once.
385 for (i=0; my_netbios_names[i]; i++)
387 stype = 0;
388 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
390 if((work = find_workgroup_on_subnet( subrec, global_myworkgroup )) == NULL)
391 continue;
392 if((servrec = find_server_in_workgroup( work, my_netbios_names[i])) == NULL)
393 continue;
395 stype |= servrec->serv.type;
398 /* Output server details, plus what workgroup they're in. */
399 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", my_netbios_names[i]);
400 fprintf(fp, "%-25s ", tmp);
401 fprintf(fp, "%08x ", stype);
402 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ",
403 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
404 fprintf(fp, "%-30s", tmp);
405 fprintf(fp, "\"%s\"\n", global_myworkgroup);
408 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
410 subrec->work_changed = False;
412 for (work = subrec->workgrouplist; work ; work = work->next)
414 /* Write out a workgroup record for a workgroup. */
415 uint32 wg_type = write_this_workgroup_name( subrec, work);
417 if(wg_type)
419 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", work->work_group);
420 fprintf(fp, "%-25s ", tmp);
422 fprintf(fp, "%08x ", wg_type);
423 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
424 fprintf(fp, "%-30s", tmp);
425 fprintf(fp, "\"%s\"\n", work->work_group);
428 /* Now write out any server records a workgroup may have. */
430 for (servrec = work->serverlist; servrec ; servrec = servrec->next)
432 uint32 serv_type;
434 /* We have already written our names here. */
435 if(is_myname(servrec->serv.name))
436 continue;
438 serv_type = write_this_server_name(subrec, work, servrec);
440 if(serv_type)
442 /* Output server details, plus what workgroup they're in. */
443 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", servrec->serv.name);
444 fprintf(fp, "%-25s ", tmp);
445 fprintf(fp, "%08x ", serv_type);
446 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", servrec->serv.comment);
447 fprintf(fp, "%-30s", tmp);
448 fprintf(fp, "\"%s\"\n", work->work_group);
454 fclose(fp);
455 unlink(fname);
456 chmod(fnamenew,0644);
457 rename(fnamenew,fname);
458 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));