2 Unix SMB/Netbios implementation.
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.
30 extern int DEBUGLEVEL;
32 extern fstring global_myworkgroup;
33 extern char **my_netbios_names;
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;
52 servrec->prev->next = servrec->next;
54 servrec->next->prev = servrec->prev;
56 if (work->serverlist == servrec)
57 work->serverlist = servrec->next;
59 ZERO_STRUCTP(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;
84 for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next)
87 servrec2->next = servrec;
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))
110 /****************************************************************************
111 Remove a server entry from this workgroup.
112 ****************************************************************************/
114 void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
117 servrec->prev->next = servrec->next;
119 servrec->next->prev = servrec->prev;
121 if (work->serverlist == servrec)
122 work->serverlist = servrec->next;
124 ZERO_STRUCTP(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;
141 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
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));
153 if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
155 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
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;
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())
189 if(is_myname(servrec->serv.name))
190 servrec->death_time = PERMANENT_TTL;
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).
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
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))
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)
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;
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)
319 if (t - lasttime < 5)
325 dump_workgroups(force_write);
327 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
329 if(subrec->work_changed)
341 pstrcpy(fname,lp_lockdir());
342 trim_string(fname,NULL,"/");
344 pstrcat(fname,SERVER_LIST);
345 pstrcpy(fnamenew,fname);
346 pstrcat(fnamenew,".");
348 fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
352 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
353 fnamenew,strerror(errno)));
358 * Write out a record for our workgroup. Use the record from the first
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));
370 slprintf(tmp,sizeof(tmp)-1, "\"%s\"", work->work_group);
371 x_fprintf(fp, "%-25s ", tmp);
372 x_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 x_fprintf(fp, "%-30s", tmp);
375 x_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
385 for (i=0; my_netbios_names[i]; i++)
388 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
390 if((work = find_workgroup_on_subnet( subrec, global_myworkgroup )) == NULL)
392 if((servrec = find_server_in_workgroup( work, my_netbios_names[i])) == NULL)
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 x_fprintf(fp, "%-25s ", tmp);
401 x_fprintf(fp, "%08x ", stype);
402 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ",
403 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
404 x_fprintf(fp, "%-30s", tmp);
405 x_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);
419 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", work->work_group);
420 x_fprintf(fp, "%-25s ", tmp);
422 x_fprintf(fp, "%08x ", wg_type);
423 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
424 x_fprintf(fp, "%-30s", tmp);
425 x_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)
434 /* We have already written our names here. */
435 if(is_myname(servrec->serv.name))
438 serv_type = write_this_server_name(subrec, work, servrec);
442 /* Output server details, plus what workgroup they're in. */
443 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", servrec->serv.name);
444 x_fprintf(fp, "%-25s ", tmp);
445 x_fprintf(fp, "%08x ", serv_type);
446 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", servrec->serv.comment);
447 x_fprintf(fp, "%-30s", tmp);
448 x_fprintf(fp, "\"%s\"\n", work->work_group);
456 chmod(fnamenew,0644);
457 rename(fnamenew,fname);
458 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));