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 fstring global_myworkgroup;
31 extern char **my_netbios_names;
35 /*******************************************************************
36 Remove all the servers in a work group.
37 ******************************************************************/
39 void remove_all_servers(struct work_record *work)
41 struct server_record *servrec;
42 struct server_record *nexts;
44 for (servrec = work->serverlist; servrec; servrec = nexts)
46 DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
47 nexts = servrec->next;
50 servrec->prev->next = servrec->next;
52 servrec->next->prev = servrec->prev;
54 if (work->serverlist == servrec)
55 work->serverlist = servrec->next;
57 ZERO_STRUCTP(servrec);
62 work->subnet->work_changed = True;
65 /***************************************************************************
66 Add a server into the a workgroup serverlist.
67 **************************************************************************/
69 static void add_server_to_workgroup(struct work_record *work,
70 struct server_record *servrec)
72 struct server_record *servrec2;
74 if (!work->serverlist)
76 work->serverlist = servrec;
82 for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next)
85 servrec2->next = servrec;
87 servrec->prev = servrec2;
88 work->subnet->work_changed = True;
91 /****************************************************************************
92 Find a server in a server list.
93 **************************************************************************/
95 struct server_record *find_server_in_workgroup(struct work_record *work, char *name)
97 struct server_record *ret;
99 for (ret = work->serverlist; ret; ret = ret->next)
101 if (strequal(ret->serv.name,name))
108 /****************************************************************************
109 Remove a server entry from this workgroup.
110 ****************************************************************************/
112 void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
115 servrec->prev->next = servrec->next;
117 servrec->next->prev = servrec->prev;
119 if (work->serverlist == servrec)
120 work->serverlist = servrec->next;
122 ZERO_STRUCTP(servrec);
124 work->subnet->work_changed = True;
127 /****************************************************************************
128 Create a server entry on this workgroup.
129 ****************************************************************************/
131 struct server_record *create_server_on_workgroup(struct work_record *work,
132 char *name,int servertype,
133 int ttl,char *comment)
135 struct server_record *servrec;
139 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
144 if((servrec = find_server_in_workgroup(work, name)) != NULL)
146 DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
147 workgroup %s. This is a bug.\n", name, work->work_group));
151 if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
153 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
157 memset((char *)servrec,'\0',sizeof(*servrec));
159 servrec->subnet = work->subnet;
161 StrnCpy(servrec->serv.name,name,sizeof(servrec->serv.name)-1);
162 StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
163 strupper(servrec->serv.name);
164 servrec->serv.type = servertype;
166 update_server_ttl(servrec, ttl);
168 add_server_to_workgroup(work, servrec);
170 DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
171 workgroup %s.\n", name,servertype,comment, work->work_group));
173 work->subnet->work_changed = True;
178 /*******************************************************************
179 Update the ttl field of a server record.
180 *******************************************************************/
182 void update_server_ttl(struct server_record *servrec, int ttl)
184 if(ttl > lp_max_ttl())
187 if(is_myname(servrec->serv.name))
188 servrec->death_time = PERMANENT_TTL;
190 servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
192 servrec->subnet->work_changed = True;
195 /*******************************************************************
196 Expire old servers in the serverlist. A time of -1 indicates
197 everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
198 This should only be called from expire_workgroups_and_servers().
199 ******************************************************************/
201 void expire_servers(struct work_record *work, time_t t)
203 struct server_record *servrec;
204 struct server_record *nexts;
206 for (servrec = work->serverlist; servrec; servrec = nexts)
208 nexts = servrec->next;
210 if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t)))
212 DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
213 remove_server_from_workgroup(work, servrec);
214 work->subnet->work_changed = True;
219 /*******************************************************************
220 Decide if we should write out a server record for this server.
221 We return zero if we should not. Check if we've already written
222 out this server record from an earlier subnet.
223 ******************************************************************/
225 static uint32 write_this_server_name( struct subnet_record *subrec,
226 struct work_record *work,
227 struct server_record *servrec)
229 struct subnet_record *ssub;
230 struct work_record *iwork;
232 /* Go through all the subnets we have already seen. */
233 for (ssub = FIRST_SUBNET; ssub != subrec; ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub))
235 for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next)
237 if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL)
240 * We have already written out this server record, don't
241 * do it again. This gives precedence to servers we have seen
242 * on the broadcast subnets over servers that may have been
243 * added via a sync on the unicast_subet.
245 * The correct way to do this is to have a serverlist file
246 * per subnet - this means changes to smbd as well. I may
247 * add this at a later date (JRA).
255 return servrec->serv.type;
258 /*******************************************************************
259 Decide if we should write out a workgroup record for this workgroup.
260 We return zero if we should not. Don't write out global_myworkgroup (we've
261 already done it) and also don't write out a second workgroup record
262 on the unicast subnet that we've already written out on one of the
264 ******************************************************************/
266 static uint32 write_this_workgroup_name( struct subnet_record *subrec,
267 struct work_record *work)
269 struct subnet_record *ssub;
271 if(strequal(global_myworkgroup, work->work_group))
274 /* This is a workgroup we have seen on a broadcast subnet. All
275 these have the same type. */
277 if(subrec != unicast_subnet)
278 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
280 for(ssub = FIRST_SUBNET; ssub; ssub = NEXT_SUBNET_EXCLUDING_UNICAST(ssub))
282 /* This is the unicast subnet so check if we've already written out
283 this subnet when we passed over the broadcast subnets. */
285 if(find_workgroup_on_subnet( ssub, work->work_group) != NULL)
289 /* All workgroups on the unicast subnet (except our own, which we
290 have already written out) cannot be local. */
292 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT);
295 /*******************************************************************
296 Write out the browse.dat file.
297 ******************************************************************/
299 void write_browse_list(time_t t, BOOL force_write)
301 struct subnet_record *subrec;
302 struct work_record *work;
303 struct server_record *servrec;
304 pstring fname,fnamenew;
309 BOOL list_changed = force_write;
310 static time_t lasttime = 0;
312 /* Always dump if we're being told to by a signal. */
313 if(force_write == False)
317 if (t - lasttime < 5)
323 dump_workgroups(force_write);
325 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
327 if(subrec->work_changed)
339 pstrcpy(fname,lp_lockdir());
340 trim_string(fname,NULL,"/");
342 pstrcat(fname,SERVER_LIST);
343 pstrcpy(fnamenew,fname);
344 pstrcat(fnamenew,".");
346 fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
350 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
351 fnamenew,strerror(errno)));
356 * Write out a record for our workgroup. Use the record from the first
360 if((work = find_workgroup_on_subnet(FIRST_SUBNET, global_myworkgroup)) == NULL)
362 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
363 global_myworkgroup));
368 slprintf(tmp,sizeof(tmp)-1, "\"%s\"", work->work_group);
369 x_fprintf(fp, "%-25s ", tmp);
370 x_fprintf(fp, "%08x ", SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
371 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
372 x_fprintf(fp, "%-30s", tmp);
373 x_fprintf(fp, "\"%s\"\n", work->work_group);
376 * We need to do something special for our own names.
377 * This is due to the fact that we may be a local master browser on
378 * one of our broadcast subnets, and a domain master on the unicast
379 * subnet. We iterate over the subnets and only write out the name
383 for (i=0; my_netbios_names[i]; i++)
386 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
388 if((work = find_workgroup_on_subnet( subrec, global_myworkgroup )) == NULL)
390 if((servrec = find_server_in_workgroup( work, my_netbios_names[i])) == NULL)
393 stype |= servrec->serv.type;
396 /* Output server details, plus what workgroup they're in. */
397 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", my_netbios_names[i]);
398 x_fprintf(fp, "%-25s ", tmp);
399 x_fprintf(fp, "%08x ", stype);
400 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ",
401 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
402 x_fprintf(fp, "%-30s", tmp);
403 x_fprintf(fp, "\"%s\"\n", global_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);
417 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", work->work_group);
418 x_fprintf(fp, "%-25s ", tmp);
420 x_fprintf(fp, "%08x ", wg_type);
421 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
422 x_fprintf(fp, "%-30s", tmp);
423 x_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)
432 /* We have already written our names here. */
433 if(is_myname(servrec->serv.name))
436 serv_type = write_this_server_name(subrec, work, servrec);
440 /* Output server details, plus what workgroup they're in. */
441 slprintf(tmp, sizeof(tmp)-1, "\"%s\"", servrec->serv.name);
442 x_fprintf(fp, "%-25s ", tmp);
443 x_fprintf(fp, "%08x ", serv_type);
444 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", servrec->serv.comment);
445 x_fprintf(fp, "%-30s", tmp);
446 x_fprintf(fp, "\"%s\"\n", work->work_group);
454 chmod(fnamenew,0644);
455 rename(fnamenew,fname);
456 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));