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 3 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, see <http://www.gnu.org/licenses/>.
27 /*******************************************************************
28 Remove all the servers in a work group.
29 ******************************************************************/
31 void remove_all_servers(struct work_record
*work
)
33 struct server_record
*servrec
;
34 struct server_record
*nexts
;
36 for (servrec
= work
->serverlist
; servrec
; servrec
= nexts
) {
37 DEBUG(7,("remove_all_servers: Removing server %s\n",servrec
->serv
.name
));
38 nexts
= servrec
->next
;
39 DLIST_REMOVE(work
->serverlist
, servrec
);
40 ZERO_STRUCTP(servrec
);
44 work
->subnet
->work_changed
= True
;
47 /***************************************************************************
48 Add a server into the a workgroup serverlist.
49 **************************************************************************/
51 static void add_server_to_workgroup(struct work_record
*work
,
52 struct server_record
*servrec
)
54 DLIST_ADD_END(work
->serverlist
, servrec
, struct server_record
*);
55 work
->subnet
->work_changed
= True
;
58 /****************************************************************************
59 Find a server in a server list.
60 **************************************************************************/
62 struct server_record
*find_server_in_workgroup(struct work_record
*work
, const char *name
)
64 struct server_record
*ret
;
66 for (ret
= work
->serverlist
; ret
; ret
= ret
->next
) {
67 if (strequal(ret
->serv
.name
,name
))
74 /****************************************************************************
75 Remove a server entry from this workgroup.
76 ****************************************************************************/
78 void remove_server_from_workgroup(struct work_record
*work
, struct server_record
*servrec
)
80 DLIST_REMOVE(work
->serverlist
, servrec
);
81 ZERO_STRUCTP(servrec
);
83 work
->subnet
->work_changed
= True
;
86 /****************************************************************************
87 Create a server entry on this workgroup.
88 ****************************************************************************/
90 struct server_record
*create_server_on_workgroup(struct work_record
*work
,
91 const char *name
,int servertype
,
92 int ttl
, const char *comment
)
94 struct server_record
*servrec
;
97 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
102 if(find_server_in_workgroup(work
, name
) != NULL
) {
103 DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
104 workgroup %s. This is a bug.\n", name
, work
->work_group
));
108 if((servrec
= SMB_MALLOC_P(struct server_record
)) == NULL
) {
109 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
113 memset((char *)servrec
,'\0',sizeof(*servrec
));
115 servrec
->subnet
= work
->subnet
;
117 fstrcpy(servrec
->serv
.name
,name
);
118 fstrcpy(servrec
->serv
.comment
,comment
);
119 strupper_m(servrec
->serv
.name
);
120 servrec
->serv
.type
= servertype
;
122 update_server_ttl(servrec
, ttl
);
124 add_server_to_workgroup(work
, servrec
);
126 DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
127 workgroup %s.\n", name
,servertype
,comment
, work
->work_group
));
129 work
->subnet
->work_changed
= True
;
134 /*******************************************************************
135 Update the ttl field of a server record.
136 *******************************************************************/
138 void update_server_ttl(struct server_record
*servrec
, int ttl
)
140 if(ttl
> lp_max_ttl())
143 if(is_myname(servrec
->serv
.name
))
144 servrec
->death_time
= PERMANENT_TTL
;
146 servrec
->death_time
= (ttl
!= PERMANENT_TTL
) ? time(NULL
)+(ttl
*3) : PERMANENT_TTL
;
148 servrec
->subnet
->work_changed
= True
;
151 /*******************************************************************
152 Expire old servers in the serverlist. A time of -1 indicates
153 everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
154 This should only be called from expire_workgroups_and_servers().
155 ******************************************************************/
157 void expire_servers(struct work_record
*work
, time_t t
)
159 struct server_record
*servrec
;
160 struct server_record
*nexts
;
162 for (servrec
= work
->serverlist
; servrec
; servrec
= nexts
) {
163 nexts
= servrec
->next
;
165 if ((servrec
->death_time
!= PERMANENT_TTL
) && ((t
== -1) || (servrec
->death_time
< t
))) {
166 DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec
->serv
.name
));
167 remove_server_from_workgroup(work
, servrec
);
168 work
->subnet
->work_changed
= True
;
173 /*******************************************************************
174 Decide if we should write out a server record for this server.
175 We return zero if we should not. Check if we've already written
176 out this server record from an earlier subnet.
177 ******************************************************************/
179 static uint32
write_this_server_name( struct subnet_record
*subrec
,
180 struct work_record
*work
,
181 struct server_record
*servrec
)
183 struct subnet_record
*ssub
;
184 struct work_record
*iwork
;
186 /* Go through all the subnets we have already seen. */
187 for (ssub
= FIRST_SUBNET
; ssub
&& (ssub
!= subrec
); ssub
= NEXT_SUBNET_INCLUDING_UNICAST(ssub
)) {
188 for(iwork
= ssub
->workgrouplist
; iwork
; iwork
= iwork
->next
) {
189 if(find_server_in_workgroup( iwork
, servrec
->serv
.name
) != NULL
) {
191 * We have already written out this server record, don't
192 * do it again. This gives precedence to servers we have seen
193 * on the broadcast subnets over servers that may have been
194 * added via a sync on the unicast_subet.
196 * The correct way to do this is to have a serverlist file
197 * per subnet - this means changes to smbd as well. I may
198 * add this at a later date (JRA).
206 return servrec
->serv
.type
;
209 /*******************************************************************
210 Decide if we should write out a workgroup record for this workgroup.
211 We return zero if we should not. Don't write out lp_workgroup() (we've
212 already done it) and also don't write out a second workgroup record
213 on the unicast subnet that we've already written out on one of the
215 ******************************************************************/
217 static uint32
write_this_workgroup_name( struct subnet_record
*subrec
,
218 struct work_record
*work
)
220 struct subnet_record
*ssub
;
222 if(strequal(lp_workgroup(), work
->work_group
))
225 /* This is a workgroup we have seen on a broadcast subnet. All
226 these have the same type. */
228 if(subrec
!= unicast_subnet
)
229 return (SV_TYPE_DOMAIN_ENUM
|SV_TYPE_NT
|SV_TYPE_LOCAL_LIST_ONLY
);
231 for(ssub
= FIRST_SUBNET
; ssub
; ssub
= NEXT_SUBNET_EXCLUDING_UNICAST(ssub
)) {
232 /* This is the unicast subnet so check if we've already written out
233 this subnet when we passed over the broadcast subnets. */
235 if(find_workgroup_on_subnet( ssub
, work
->work_group
) != NULL
)
239 /* All workgroups on the unicast subnet (except our own, which we
240 have already written out) cannot be local. */
242 return (SV_TYPE_DOMAIN_ENUM
|SV_TYPE_NT
);
245 /*******************************************************************
246 Write out the browse.dat file.
247 ******************************************************************/
249 void write_browse_list_entry(XFILE
*fp
, const char *name
, uint32 rec_type
,
250 const char *local_master_browser_name
, const char *description
)
254 slprintf(tmp
,sizeof(tmp
)-1, "\"%s\"", name
);
255 x_fprintf(fp
, "%-25s ", tmp
);
256 x_fprintf(fp
, "%08x ", rec_type
);
257 slprintf(tmp
, sizeof(tmp
)-1, "\"%s\" ", local_master_browser_name
);
258 x_fprintf(fp
, "%-30s", tmp
);
259 x_fprintf(fp
, "\"%s\"\n", description
);
262 void write_browse_list(time_t t
, bool force_write
)
264 struct subnet_record
*subrec
;
265 struct work_record
*work
;
266 struct server_record
*servrec
;
272 bool list_changed
= force_write
;
273 static time_t lasttime
= 0;
274 TALLOC_CTX
*ctx
= talloc_tos();
276 /* Always dump if we're being told to by a signal. */
277 if(force_write
== False
) {
280 if (t
- lasttime
< 5)
286 dump_workgroups(force_write
);
288 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
)) {
289 if(subrec
->work_changed
) {
300 fname
= cache_path(SERVER_LIST
);
304 fnamenew
= talloc_asprintf(ctx
, "%s.",
310 fp
= x_fopen(fnamenew
,O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
313 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
314 fnamenew
,strerror(errno
)));
319 * Write out a record for our workgroup. Use the record from the first
323 if((work
= find_workgroup_on_subnet(FIRST_SUBNET
, lp_workgroup())) == NULL
) {
324 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
330 write_browse_list_entry(fp
, work
->work_group
,
331 SV_TYPE_DOMAIN_ENUM
|SV_TYPE_NT
|SV_TYPE_LOCAL_LIST_ONLY
,
332 work
->local_master_browser_name
, work
->work_group
);
335 * We need to do something special for our own names.
336 * This is due to the fact that we may be a local master browser on
337 * one of our broadcast subnets, and a domain master on the unicast
338 * subnet. We iterate over the subnets and only write out the name
342 for (i
=0; my_netbios_names(i
); i
++) {
344 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
)) {
345 if((work
= find_workgroup_on_subnet( subrec
, lp_workgroup() )) == NULL
)
347 if((servrec
= find_server_in_workgroup( work
, my_netbios_names(i
))) == NULL
)
350 stype
|= servrec
->serv
.type
;
353 /* Output server details, plus what workgroup they're in. */
354 write_browse_list_entry(fp
, my_netbios_names(i
), stype
,
355 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH
), lp_workgroup());
358 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
)) {
359 subrec
->work_changed
= False
;
361 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
) {
362 /* Write out a workgroup record for a workgroup. */
363 uint32 wg_type
= write_this_workgroup_name( subrec
, work
);
366 write_browse_list_entry(fp
, work
->work_group
, wg_type
,
367 work
->local_master_browser_name
,
371 /* Now write out any server records a workgroup may have. */
373 for (servrec
= work
->serverlist
; servrec
; servrec
= servrec
->next
) {
376 /* We have already written our names here. */
377 if(is_myname(servrec
->serv
.name
))
380 serv_type
= write_this_server_name(subrec
, work
, servrec
);
382 /* Output server details, plus what workgroup they're in. */
383 write_browse_list_entry(fp
, servrec
->serv
.name
, serv_type
,
384 servrec
->serv
.comment
, work
->work_group
);
392 chmod(fnamenew
,0644);
393 rename(fnamenew
,fname
);
394 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname
));