This commit was manufactured by cvs2svn to create tag
[Samba.git] / source / nmbd / nmbd_serverlistdb.c
blobe94cb1da6e9d5d2fd56e9103fbfa692fd14c08dd
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 pstring myname;
33 extern fstring myworkgroup;
34 extern char **my_netbios_names;
36 int updatecount = 0;
38 /*******************************************************************
39 Remove all the servers in a work group.
40 ******************************************************************/
42 void remove_all_servers(struct work_record *work)
44 struct server_record *servrec;
45 struct server_record *nexts;
47 for (servrec = work->serverlist; servrec; servrec = nexts)
49 DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
50 nexts = servrec->next;
52 if (servrec->prev)
53 servrec->prev->next = servrec->next;
54 if (servrec->next)
55 servrec->next->prev = servrec->prev;
57 if (work->serverlist == servrec)
58 work->serverlist = servrec->next;
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 free((char *)servrec);
125 work->subnet->work_changed = True;
128 /****************************************************************************
129 Create a server entry on this workgroup.
130 ****************************************************************************/
132 struct server_record *create_server_on_workgroup(struct work_record *work,
133 char *name,int servertype,
134 int ttl,char *comment)
136 struct server_record *servrec;
138 if (name[0] == '*')
140 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
141 name));
142 return (NULL);
145 if((servrec = find_server_in_workgroup(work, name)) != NULL)
147 DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
148 workgroup %s. This is a bug.\n", name, work->work_group));
149 return NULL;
152 if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
154 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
155 return NULL;
158 bzero((char *)servrec,sizeof(*servrec));
160 servrec->subnet = work->subnet;
162 StrnCpy(servrec->serv.name,name,sizeof(servrec->serv.name)-1);
163 StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
164 strupper(servrec->serv.name);
165 servrec->serv.type = servertype;
167 update_server_ttl(servrec, ttl);
169 add_server_to_workgroup(work, servrec);
171 DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
172 workgroup %s.\n", name,servertype,comment, work->work_group));
174 work->subnet->work_changed = True;
176 return(servrec);
179 /*******************************************************************
180 Update the ttl field of a server record.
181 *******************************************************************/
183 void update_server_ttl(struct server_record *servrec, int ttl)
185 if(ttl > lp_max_ttl())
186 ttl = lp_max_ttl();
188 if(is_myname(servrec->serv.name))
189 servrec->death_time = PERMANENT_TTL;
190 else
191 servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
193 servrec->subnet->work_changed = True;
196 /*******************************************************************
197 Expire old servers in the serverlist. A time of -1 indicates
198 everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
199 This should only be called from expire_workgroups_and_servers().
200 ******************************************************************/
202 void expire_servers(struct work_record *work, time_t t)
204 struct server_record *servrec;
205 struct server_record *nexts;
207 for (servrec = work->serverlist; servrec; servrec = nexts)
209 nexts = servrec->next;
211 if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t)))
213 DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
214 remove_server_from_workgroup(work, servrec);
215 work->subnet->work_changed = True;
220 /*******************************************************************
221 Decide if we should write out a server record for this server.
222 We return zero if we should not. Check if we've already written
223 out this server record from an earlier subnet.
224 ******************************************************************/
226 static uint32 write_this_server_name( struct subnet_record *subrec,
227 struct work_record *work,
228 struct server_record *servrec)
230 struct subnet_record *ssub;
231 struct work_record *iwork;
232 struct server_record *sserv;
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((sserv = 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 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(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 dump_workgroups(force_write);
325 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
327 if(subrec->work_changed)
329 list_changed = True;
330 break;
334 if(!list_changed)
335 return;
337 lasttime = t;
338 updatecount++;
340 pstrcpy(fname,lp_lockdir());
341 trim_string(fname,NULL,"/");
342 strcat(fname,"/");
343 strcat(fname,SERVER_LIST);
344 pstrcpy(fnamenew,fname);
345 strcat(fnamenew,".");
347 fp = fopen(fnamenew,"w");
349 if (!fp)
351 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
352 fnamenew,strerror(errno)));
353 return;
357 * Write out a record for our workgroup. Use the record from the first
358 * subnet.
361 if((work = find_workgroup_on_subnet(FIRST_SUBNET, myworkgroup)) == NULL)
363 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
364 myworkgroup));
365 fclose(fp);
366 return;
369 sprintf(tmp, "\"%s\"", work->work_group);
370 fprintf(fp, "%-25s ", tmp);
371 fprintf(fp, "%08x ", SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
372 sprintf(tmp, "\"%s\" ", work->local_master_browser_name);
373 fprintf(fp, "%-30s", tmp);
374 fprintf(fp, "\"%s\"\n", work->work_group);
377 * We need to do something special for our own names.
378 * This is due to the fact that we may be a local master browser on
379 * one of our broadcast subnets, and a domain master on the unicast
380 * subnet. We iterate over the subnets and only write out the name
381 * once.
384 for (i=0; my_netbios_names[i]; i++)
386 stype = 0;
387 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
389 if((work = find_workgroup_on_subnet( subrec, myworkgroup )) == NULL)
390 continue;
391 if((servrec = find_server_in_workgroup( work, my_netbios_names[i])) == NULL)
392 continue;
394 stype |= servrec->serv.type;
397 /* Output server details, plus what workgroup they're in. */
398 sprintf(tmp, "\"%s\"", my_netbios_names[i]);
399 fprintf(fp, "%-25s ", tmp);
400 fprintf(fp, "%08x ", stype);
401 sprintf(tmp, "\"%s\" ", lp_serverstring());
402 fprintf(fp, "%-30s", tmp);
403 fprintf(fp, "\"%s\"\n", myworkgroup);
406 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
408 subrec->work_changed = False;
410 for (work = subrec->workgrouplist; work ; work = work->next)
412 /* Write out a workgroup record for a workgroup. */
413 uint32 wg_type = write_this_workgroup_name( subrec, work);
415 if(wg_type)
417 sprintf(tmp, "\"%s\"", work->work_group);
418 fprintf(fp, "%-25s ", tmp);
420 fprintf(fp, "%08x ", wg_type);
421 sprintf(tmp, "\"%s\" ", work->local_master_browser_name);
422 fprintf(fp, "%-30s", tmp);
423 fprintf(fp, "\"%s\"\n", work->work_group);
426 /* Now write out any server records a workgroup may have. */
428 for (servrec = work->serverlist; servrec ; servrec = servrec->next)
430 uint32 serv_type;
432 /* We have already written our names here. */
433 if(is_myname(servrec->serv.name))
434 continue;
436 serv_type = write_this_server_name(subrec, work, servrec);
438 if(serv_type)
440 /* Output server details, plus what workgroup they're in. */
441 sprintf(tmp, "\"%s\"", servrec->serv.name);
442 fprintf(fp, "%-25s ", tmp);
443 fprintf(fp, "%08x ", serv_type);
444 sprintf(tmp, "\"%s\" ", servrec->serv.comment);
445 fprintf(fp, "%-30s", tmp);
446 fprintf(fp, "\"%s\"\n", work->work_group);
452 fclose(fp);
453 unlink(fname);
454 chmod(fnamenew,0644);
455 rename(fnamenew,fname);
456 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));