2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
26 04 jul 96: lkcl@pires.co.uk
27 created module namedbsubnet containing subnet database functions
35 extern int ClientDGRAM
;
37 extern int DEBUGLEVEL
;
39 extern struct in_addr wins_ip
;
40 extern struct in_addr ipzero
;
42 extern pstring myname
;
43 extern fstring myworkgroup
;
45 BOOL updatedlists
= True
;
48 /* local interfaces structure */
49 extern struct interface
*local_interfaces
;
51 /* this is our domain/workgroup/server database */
52 struct subnet_record
*subnetlist
= NULL
;
54 /* WINS subnet - keep this separate so enumeration code doesn't
55 run onto it by mistake. */
56 struct subnet_record
*wins_subnet
= NULL
;
58 extern uint16 nb_type
; /* samba's NetBIOS name type */
60 /****************************************************************************
61 add a domain into the list
62 **************************************************************************/
63 static void add_subnet(struct subnet_record
*d
)
65 struct subnet_record
*d2
;
75 for (d2
= subnetlist
; d2
->next
; d2
= d2
->next
);
83 /****************************************************************************
84 find a subnet in the subnetlist - not including WINS.
85 **************************************************************************/
86 struct subnet_record
*find_subnet(struct in_addr bcast_ip
)
88 struct subnet_record
*d
;
90 /* search through subnet list for broadcast/netmask that matches
91 the source ip address. */
93 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_EXCLUDING_WINS(d
))
95 if (same_net(bcast_ip
, d
->bcast_ip
, d
->mask_ip
))
103 /****************************************************************************
104 finds the appropriate subnet structure. directed packets (non-bcast) are
105 assumed to come from a point-to-point (P or M node), and so the subnet we
106 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
107 ****************************************************************************/
108 struct subnet_record
*find_req_subnet(struct in_addr ip
, BOOL bcast
)
112 /* identify the subnet the broadcast request came from */
113 return find_subnet(*iface_bcast(ip
));
115 /* Return the subnet with the pseudo-ip of 255.255.255.255 */
119 /****************************************************************************
120 find a subnet in the subnetlist - if the subnet is not found
121 then return the WINS subnet.
122 **************************************************************************/
123 struct subnet_record
*find_subnet_all(struct in_addr bcast_ip
)
125 struct subnet_record
*d
= find_subnet(bcast_ip
);
131 /****************************************************************************
132 create a domain entry
133 ****************************************************************************/
134 static struct subnet_record
*make_subnet(struct in_addr bcast_ip
, struct in_addr mask_ip
, BOOL add
)
136 struct subnet_record
*d
;
137 d
= (struct subnet_record
*)malloc(sizeof(*d
));
139 if (!d
) return(NULL
);
141 bzero((char *)d
,sizeof(*d
));
143 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip
)));
144 DEBUG(4, ("%s\n", inet_ntoa(mask_ip
)));
146 d
->bcast_ip
= bcast_ip
;
147 d
->mask_ip
= mask_ip
;
148 d
->workgrouplist
= NULL
;
156 /****************************************************************************
157 add a domain entry. creates a workgroup, if necessary, and adds the domain
158 to the named a workgroup.
159 ****************************************************************************/
160 static struct subnet_record
*add_subnet_entry(struct in_addr bcast_ip
,
161 struct in_addr mask_ip
, char *name
,
162 BOOL create_subnets
, BOOL add
)
164 struct subnet_record
*d
= NULL
;
166 if (zero_ip(bcast_ip
))
167 bcast_ip
= *iface_bcast(bcast_ip
);
169 /* Note that we should also add into the WINS subnet as add_subnet_entry
170 should be called to add NetBIOS names and server entries on all
171 interfaces, including the WINS interface
174 if(create_subnets
== True
)
176 /* Create new subnets. */
177 if((d
= make_subnet(bcast_ip
, mask_ip
, add
)) == NULL
)
179 DEBUG(0,("add_subnet_entry: Unable to create subnet %s\n",
180 inet_ntoa(bcast_ip
) ));
185 if(ip_equal(bcast_ip
, wins_ip
))
187 return find_subnet(bcast_ip
);
190 /****************************************************************************
191 Add a workgroup into a subnet, and if it's our primary workgroup,
192 add the required names to it.
193 **************************************************************************/
195 void add_workgroup_to_subnet( struct subnet_record
*d
, char *group
)
197 struct work_record
*w
= NULL
;
199 DEBUG(5,("add_workgroup_to_subnet: Adding workgroup %s to subnet %s\n",
200 group
, inet_ntoa(d
->bcast_ip
)));
202 /* This next statement creates the workgroup struct if it doesn't
205 if((w
= find_workgroupstruct(d
, group
, True
)) == NULL
)
207 DEBUG(0,("add_workgroup_to_subnet: Unable to add workgroup %s to subnet %s\n",
208 group
, inet_ntoa(d
->bcast_ip
) ));
212 /* add WORKGROUP(00) entries into name database
213 or register with WINS server, if it's our workgroup.
215 if (strequal(myworkgroup
, group
))
217 add_my_name_entry(d
,group
,0x0 ,nb_type
|NB_ACTIVE
|NB_GROUP
,False
);
218 add_my_name_entry(d
,group
,0x1e,nb_type
|NB_ACTIVE
|NB_GROUP
,False
);
219 /* add samba server name to workgroup list. */
220 add_server_entry(d
,w
,myname
,w
->ServerType
|SV_TYPE_LOCAL_LIST_ONLY
,0,
221 lp_serverstring(),True
);
222 DEBUG(3,("add_workgroup_to_subnet: Added server name entry %s to subnet %s\n",
223 myname
, inet_ntoa(d
->bcast_ip
)));
227 /****************************************************************************
228 create subnet / workgroup / server entries
230 - add or create the subnet lists
231 - add or create the workgroup entries in each subnet entry
232 - register appropriate NetBIOS names for the workgroup entries
234 **************************************************************************/
235 void add_my_subnets(char *group
)
237 static BOOL create_subnets
= True
;
238 struct subnet_record
*d
= NULL
;
239 struct interface
*i
= NULL
;
241 if (*group
== '*') return;
243 /* Create subnets from all the local interfaces and thread them onto
246 for (i
= local_interfaces
; i
; i
= i
->next
)
248 add_subnet_entry(i
->bcast
,i
->nmask
,group
, create_subnets
, True
);
251 /* If we are using WINS, then we must add the workgroup to the WINS
252 subnet. This is used as a place to keep collated server lists.
255 /* Create the WINS subnet if we are using WINS - but don't thread it
256 onto the linked subnet list.
258 if (lp_wins_support() || lp_wins_server())
260 struct in_addr wins_nmask
= ipzero
;
261 wins_subnet
= add_subnet_entry(wins_ip
, wins_nmask
, group
, create_subnets
, False
);
264 /* Ensure we only create the subnets once. */
265 create_subnets
= False
;
267 /* Now we have created all the subnets - we can add the names
268 that make us a client member in the workgroup.
270 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
271 add_workgroup_to_subnet(d
, group
);
274 /*******************************************************************
276 ******************************************************************/
277 void write_browse_list(time_t t
)
279 struct subnet_record
*d
;
280 pstring fname
,fnamenew
;
283 static time_t lasttime
= 0;
285 if (!lasttime
) lasttime
= t
;
286 if (!updatedlists
|| t
- lasttime
< 5) return;
289 updatedlists
= False
;
295 strcpy(fname
,lp_lockdir());
296 trim_string(fname
,NULL
,"/");
298 strcat(fname
,SERVER_LIST
);
299 strcpy(fnamenew
,fname
);
300 strcat(fnamenew
,".");
302 f
= fopen(fnamenew
,"w");
306 DEBUG(4,("Can't open %s - %s\n",fnamenew
,strerror(errno
)));
310 for (d
= FIRST_SUBNET
; d
; d
= NEXT_SUBNET_INCLUDING_WINS(d
))
312 struct work_record
*work
;
313 for (work
= d
->workgrouplist
; work
; work
= work
->next
)
315 struct server_record
*s
;
316 for (s
= work
->serverlist
; s
; s
= s
->next
)
320 /* don't list domains I don't have a master for */
321 if ((s
->serv
.type
& SV_TYPE_DOMAIN_ENUM
) && !s
->serv
.comment
[0])
326 /* output server details, plus what workgroup/domain
327 they're in. without the domain information, the
328 combined list of all servers in all workgroups gets
329 sent to anyone asking about any workgroup! */
331 sprintf(tmp
, "\"%s\"", s
->serv
.name
);
332 fprintf(f
, "%-25s ", tmp
);
333 fprintf(f
, "%08x ", s
->serv
.type
);
334 sprintf(tmp
, "\"%s\" ", s
->serv
.comment
);
335 fprintf(f
, "%-30s", tmp
);
336 fprintf(f
, "\"%s\"\n", work
->work_group
);
343 chmod(fnamenew
,0644);
344 rename(fnamenew
,fname
);
345 DEBUG(3,("Wrote browse list %s\n",fname
));