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_entry(FILE *fp
, fstring name
, uint32 rec_type
,
300 fstring local_master_browser_name
, fstring description
)
304 slprintf(tmp
,sizeof(tmp
)-1, "\"%s\"", name
);
305 fprintf(fp
, "%-25s ", tmp
);
306 fprintf(fp
, "%08x ", rec_type
);
307 slprintf(tmp
, sizeof(tmp
)-1, "\"%s\" ", local_master_browser_name
);
308 fprintf(fp
, "%-30s", tmp
);
309 fprintf(fp
, "\"%s\"\n", description
);
312 void write_browse_list(time_t t
, BOOL force_write
)
314 struct subnet_record
*subrec
;
315 struct work_record
*work
;
316 struct server_record
*servrec
;
317 pstring fname
,fnamenew
;
321 BOOL list_changed
= force_write
;
322 static time_t lasttime
= 0;
324 /* Always dump if we're being told to by a signal. */
325 if(force_write
== False
)
329 if (t
- lasttime
< 5)
335 dump_workgroups(force_write
);
337 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
339 if(subrec
->work_changed
)
351 pstrcpy(fname
,lp_lockdir());
352 trim_string(fname
,NULL
,"/");
354 pstrcat(fname
,SERVER_LIST
);
355 pstrcpy(fnamenew
,fname
);
356 pstrcat(fnamenew
,".");
358 fp
= sys_fopen(fnamenew
,"w");
362 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
363 fnamenew
,strerror(errno
)));
368 * Write out a record for our workgroup. Use the record from the first
372 if((work
= find_workgroup_on_subnet(FIRST_SUBNET
, global_myworkgroup
)) == NULL
)
374 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
375 global_myworkgroup
));
380 write_browse_list_entry(fp
, work
->work_group
, SV_TYPE_DOMAIN_ENUM
|SV_TYPE_NT
|SV_TYPE_LOCAL_LIST_ONLY
,
381 work
->local_master_browser_name
, work
->work_group
);
384 * We need to do something special for our own names.
385 * This is due to the fact that we may be a local master browser on
386 * one of our broadcast subnets, and a domain master on the unicast
387 * subnet. We iterate over the subnets and only write out the name
391 for (i
=0; my_netbios_names
[i
]; i
++)
394 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
396 if((work
= find_workgroup_on_subnet( subrec
, global_myworkgroup
)) == NULL
)
398 if((servrec
= find_server_in_workgroup( work
, my_netbios_names
[i
])) == NULL
)
401 stype
|= servrec
->serv
.type
;
404 /* Output server details, plus what workgroup they're in. */
405 write_browse_list_entry(fp
, my_netbios_names
[i
], stype
,
406 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH
),
411 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
))
413 subrec
->work_changed
= False
;
415 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
)
417 /* Write out a workgroup record for a workgroup. */
418 uint32 wg_type
= write_this_workgroup_name( subrec
, work
);
421 write_browse_list_entry(fp
, work
->work_group
, wg_type
,
422 work
->local_master_browser_name
,
425 /* Now write out any server records a workgroup may have. */
427 for (servrec
= work
->serverlist
; servrec
; servrec
= servrec
->next
)
431 /* We have already written our names here. */
432 if(is_myname(servrec
->serv
.name
))
435 serv_type
= write_this_server_name(subrec
, work
, servrec
);
437 /* Output server details, plus what workgroup they're in. */
439 write_browse_list_entry(fp
, servrec
->serv
.name
, serv_type
,
440 servrec
->serv
.comment
, work
->work_group
);
447 chmod(fnamenew
,0644);
448 rename(fnamenew
,fname
);
449 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname
));