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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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)
41 DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
42 nexts = servrec->next;
45 servrec->prev->next = servrec->next;
47 servrec->next->prev = servrec->prev;
49 if (work->serverlist == servrec)
50 work->serverlist = servrec->next;
52 ZERO_STRUCTP(servrec);
57 work->subnet->work_changed = True;
60 /***************************************************************************
61 Add a server into the a workgroup serverlist.
62 **************************************************************************/
64 static void add_server_to_workgroup(struct work_record *work,
65 struct server_record *servrec)
67 struct server_record *servrec2;
69 if (!work->serverlist)
71 work->serverlist = servrec;
77 for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next)
80 servrec2->next = servrec;
82 servrec->prev = servrec2;
83 work->subnet->work_changed = True;
86 /****************************************************************************
87 Find a server in a server list.
88 **************************************************************************/
90 struct server_record *find_server_in_workgroup(struct work_record *work, const char *name)
92 struct server_record *ret;
94 for (ret = work->serverlist; ret; ret = ret->next)
96 if (strequal(ret->serv.name,name))
103 /****************************************************************************
104 Remove a server entry from this workgroup.
105 ****************************************************************************/
107 void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
110 servrec->prev->next = servrec->next;
112 servrec->next->prev = servrec->prev;
114 if (work->serverlist == servrec)
115 work->serverlist = servrec->next;
117 ZERO_STRUCTP(servrec);
119 work->subnet->work_changed = True;
122 /****************************************************************************
123 Create a server entry on this workgroup.
124 ****************************************************************************/
126 struct server_record *create_server_on_workgroup(struct work_record *work,
127 const char *name,int servertype,
128 int ttl, const char *comment)
130 struct server_record *servrec;
134 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
139 if((servrec = find_server_in_workgroup(work, name)) != NULL)
141 DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
142 workgroup %s. This is a bug.\n", name, work->work_group));
146 if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
148 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
152 memset((char *)servrec,'\0',sizeof(*servrec));
154 servrec->subnet = work->subnet;
156 fstrcpy(servrec->serv.name,name);
157 fstrcpy(servrec->serv.comment,comment);
158 strupper_m(servrec->serv.name);
159 servrec->serv.type = servertype;
161 update_server_ttl(servrec, ttl);
163 add_server_to_workgroup(work, servrec);
165 DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
166 workgroup %s.\n", name,servertype,comment, work->work_group));
168 work->subnet->work_changed = True;
173 /*******************************************************************
174 Update the ttl field of a server record.
175 *******************************************************************/
177 void update_server_ttl(struct server_record *servrec, int ttl)
179 if(ttl > lp_max_ttl())
182 if(is_myname(servrec->serv.name))
183 servrec->death_time = PERMANENT_TTL;
185 servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
187 servrec->subnet->work_changed = True;
190 /*******************************************************************
191 Expire old servers in the serverlist. A time of -1 indicates
192 everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
193 This should only be called from expire_workgroups_and_servers().
194 ******************************************************************/
196 void expire_servers(struct work_record *work, time_t t)
198 struct server_record *servrec;
199 struct server_record *nexts;
201 for (servrec = work->serverlist; servrec; servrec = nexts)
203 nexts = servrec->next;
205 if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t)))
207 DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
208 remove_server_from_workgroup(work, servrec);
209 work->subnet->work_changed = True;
214 /*******************************************************************
215 Decide if we should write out a server record for this server.
216 We return zero if we should not. Check if we've already written
217 out this server record from an earlier subnet.
218 ******************************************************************/
220 static uint32 write_this_server_name( struct subnet_record *subrec,
221 struct work_record *work,
222 struct server_record *servrec)
224 struct subnet_record *ssub;
225 struct work_record *iwork;
227 /* Go through all the subnets we have already seen. */
228 for (ssub = FIRST_SUBNET; ssub != subrec; ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub))
230 for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next)
232 if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL)
235 * We have already written out this server record, don't
236 * do it again. This gives precedence to servers we have seen
237 * on the broadcast subnets over servers that may have been
238 * added via a sync on the unicast_subet.
240 * The correct way to do this is to have a serverlist file
241 * per subnet - this means changes to smbd as well. I may
242 * add this at a later date (JRA).
250 return servrec->serv.type;
253 /*******************************************************************
254 Decide if we should write out a workgroup record for this workgroup.
255 We return zero if we should not. Don't write out lp_workgroup() (we've
256 already done it) and also don't write out a second workgroup record
257 on the unicast subnet that we've already written out on one of the
259 ******************************************************************/
261 static uint32 write_this_workgroup_name( struct subnet_record *subrec,
262 struct work_record *work)
264 struct subnet_record *ssub;
266 if(strequal(lp_workgroup(), work->work_group))
269 /* This is a workgroup we have seen on a broadcast subnet. All
270 these have the same type. */
272 if(subrec != unicast_subnet)
273 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
275 for(ssub = FIRST_SUBNET; ssub; ssub = NEXT_SUBNET_EXCLUDING_UNICAST(ssub))
277 /* This is the unicast subnet so check if we've already written out
278 this subnet when we passed over the broadcast subnets. */
280 if(find_workgroup_on_subnet( ssub, work->work_group) != NULL)
284 /* All workgroups on the unicast subnet (except our own, which we
285 have already written out) cannot be local. */
287 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT);
290 /*******************************************************************
291 Write out the browse.dat file.
292 ******************************************************************/
294 void write_browse_list_entry(XFILE *fp, const char *name, uint32 rec_type,
295 const char *local_master_browser_name, const char *description)
299 slprintf(tmp,sizeof(tmp)-1, "\"%s\"", name);
300 x_fprintf(fp, "%-25s ", tmp);
301 x_fprintf(fp, "%08x ", rec_type);
302 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", local_master_browser_name);
303 x_fprintf(fp, "%-30s", tmp);
304 x_fprintf(fp, "\"%s\"\n", description);
307 void write_browse_list(time_t t, BOOL force_write)
309 struct subnet_record *subrec;
310 struct work_record *work;
311 struct server_record *servrec;
312 pstring fname,fnamenew;
316 BOOL list_changed = force_write;
317 static time_t lasttime = 0;
319 /* Always dump if we're being told to by a signal. */
320 if(force_write == False)
324 if (t - lasttime < 5)
330 dump_workgroups(force_write);
332 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
334 if(subrec->work_changed)
346 pstrcpy(fname,lp_lockdir());
347 trim_string(fname,NULL,"/");
349 pstrcat(fname,SERVER_LIST);
350 pstrcpy(fnamenew,fname);
351 pstrcat(fnamenew,".");
353 fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
357 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
358 fnamenew,strerror(errno)));
363 * Write out a record for our workgroup. Use the record from the first
367 if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL)
369 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
375 write_browse_list_entry(fp, work->work_group,
376 SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY,
377 work->local_master_browser_name, work->work_group);
380 * We need to do something special for our own names.
381 * This is due to the fact that we may be a local master browser on
382 * one of our broadcast subnets, and a domain master on the unicast
383 * subnet. We iterate over the subnets and only write out the name
387 for (i=0; my_netbios_names(i); i++)
390 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
392 if((work = find_workgroup_on_subnet( subrec, lp_workgroup() )) == NULL)
394 if((servrec = find_server_in_workgroup( work, my_netbios_names(i))) == NULL)
397 stype |= servrec->serv.type;
400 /* Output server details, plus what workgroup they're in. */
401 write_browse_list_entry(fp, my_netbios_names(i), stype,
402 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH), lp_workgroup());
405 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
407 subrec->work_changed = False;
409 for (work = subrec->workgrouplist; work ; work = work->next)
411 /* Write out a workgroup record for a workgroup. */
412 uint32 wg_type = write_this_workgroup_name( subrec, work);
416 write_browse_list_entry(fp, work->work_group, wg_type,
417 work->local_master_browser_name,
421 /* Now write out any server records a workgroup may have. */
423 for (servrec = work->serverlist; servrec ; servrec = servrec->next)
427 /* We have already written our names here. */
428 if(is_myname(servrec->serv.name))
431 serv_type = write_this_server_name(subrec, work, servrec);
435 /* Output server details, plus what workgroup they're in. */
436 write_browse_list_entry(fp, servrec->serv.name, serv_type,
437 servrec->serv.comment, work->work_group);
445 chmod(fnamenew,0644);
446 rename(fnamenew,fname);
447 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));