JHT ==> Still getting ready for 1.9.17 release.
[samba.git] / source3 / namedbsubnet.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Andrew Tridgell 1994-1997
6    
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.
11    
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.
16    
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.
20    
21    Revision History:
22
23    14 jan 96: lkcl@pires.co.uk
24    added multiple workgroup domain master support
25
26    04 jul 96: lkcl@pires.co.uk
27    created module namedbsubnet containing subnet database functions
28
29 */
30
31 #include "includes.h"
32 #include "smb.h"
33
34 extern int ClientNMB;
35 extern int ClientDGRAM;
36
37 extern int DEBUGLEVEL;
38
39 extern struct in_addr wins_ip;
40 extern struct in_addr ipzero;
41
42 extern pstring myname;
43 extern fstring myworkgroup;
44
45 BOOL updatedlists = True;
46 int updatecount = 0;
47
48 /* local interfaces structure */
49 extern struct interface *local_interfaces;
50
51 /* this is our domain/workgroup/server database */
52 struct subnet_record *subnetlist = NULL;
53
54 /* WINS subnet - keep this separate so enumeration code doesn't
55    run onto it by mistake. */
56 struct subnet_record *wins_subnet = NULL;
57
58 extern uint16 nb_type; /* samba's NetBIOS name type */
59
60 /****************************************************************************
61   add a domain into the list
62   **************************************************************************/
63 static void add_subnet(struct subnet_record *d)
64 {
65   struct subnet_record *d2;
66
67   if (!subnetlist)
68   {
69     subnetlist = d;
70     d->prev = NULL;
71     d->next = NULL;
72     return;
73   }
74
75   for (d2 = subnetlist; d2->next; d2 = d2->next);
76
77   d2->next = d;
78   d->next = NULL;
79   d->prev = d2;
80 }
81
82
83 /****************************************************************************
84   find a subnet in the subnetlist - not including WINS.
85   **************************************************************************/
86 struct subnet_record *find_subnet(struct in_addr bcast_ip)
87 {   
88   struct subnet_record *d;
89   
90   /* search through subnet list for broadcast/netmask that matches
91      the source ip address. */
92   
93   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
94     {
95       if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
96         return d;
97     }
98   
99   return (NULL);
100 }
101
102
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)
109 {
110   if (bcast)
111   {
112     /* identify the subnet the broadcast request came from */
113     return find_subnet(*iface_bcast(ip));
114   }
115   /* Return the subnet with the pseudo-ip of 255.255.255.255 */
116   return wins_subnet;
117 }
118
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)
124 {
125   struct subnet_record *d = find_subnet(bcast_ip);
126   if(!d)
127     return wins_subnet;
128   return d;
129 }
130
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)
135 {
136   struct subnet_record *d;
137   d = (struct subnet_record *)malloc(sizeof(*d));
138   
139   if (!d) return(NULL);
140   
141   bzero((char *)d,sizeof(*d));
142   
143   DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
144   DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
145   
146   d->bcast_ip = bcast_ip;
147   d->mask_ip  = mask_ip;
148   d->workgrouplist = NULL;
149   
150   if(add)
151     add_subnet(d);
152   
153   return d;
154 }
155
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)
163 {
164   struct subnet_record *d = NULL;
165
166   if (zero_ip(bcast_ip)) 
167     bcast_ip = *iface_bcast(bcast_ip);
168   
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
172    */
173
174   if(create_subnets == True)
175   {
176     /* Create new subnets. */
177     if((d = make_subnet(bcast_ip, mask_ip, add)) == NULL)
178     {
179       DEBUG(0,("add_subnet_entry: Unable to create subnet %s\n",
180                inet_ntoa(bcast_ip) ));
181       return NULL;
182     }
183     return d;
184   }
185   if(ip_equal(bcast_ip, wins_ip))
186     return wins_subnet;
187   return find_subnet(bcast_ip);
188 }
189
190 /****************************************************************************
191  Add a workgroup into a subnet, and if it's our primary workgroup,
192  add the required names to it.
193 **************************************************************************/
194
195 void add_workgroup_to_subnet( struct subnet_record *d, char *group)
196 {
197   struct work_record *w = NULL;
198
199   DEBUG(5,("add_workgroup_to_subnet: Adding workgroup %s to subnet %s\n",
200             group, inet_ntoa(d->bcast_ip)));
201
202   /* This next statement creates the workgroup struct if it doesn't
203      already exist. 
204    */
205   if((w = find_workgroupstruct(d, group, True)) == NULL)
206   {
207     DEBUG(0,("add_workgroup_to_subnet: Unable to add workgroup %s to subnet %s\n",
208               group, inet_ntoa(d->bcast_ip) ));
209     return;
210   }
211
212   /* add WORKGROUP(00) entries into name database
213      or register with WINS server, if it's our workgroup.
214    */
215   if (strequal(myworkgroup, group))
216   {
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)));
224   }
225 }
226
227 /****************************************************************************
228   create subnet / workgroup / server entries
229      
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
233      
234 **************************************************************************/
235 void add_my_subnets(char *group)
236 {    
237   static BOOL create_subnets = True;
238   struct subnet_record *d = NULL;
239   struct interface *i = NULL;
240
241   if (*group == '*') return;
242
243   /* Create subnets from all the local interfaces and thread them onto
244      the linked list. 
245    */
246   for (i = local_interfaces; i; i = i->next)
247   {
248     add_subnet_entry(i->bcast,i->nmask,group, create_subnets, True);
249   }
250
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.
253    */
254
255   /* Create the WINS subnet if we are using WINS - but don't thread it
256      onto the linked subnet list. 
257    */    
258   if (lp_wins_support() || lp_wins_server())
259   {
260     struct in_addr wins_nmask = ipzero;
261     wins_subnet = add_subnet_entry(wins_ip, wins_nmask, group, create_subnets, False);
262   }
263
264   /* Ensure we only create the subnets once. */
265   create_subnets = False;
266
267   /* Now we have created all the subnets - we can add the names
268      that make us a client member in the workgroup.
269    */
270   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
271     add_workgroup_to_subnet(d, group);
272 }
273
274 /*******************************************************************
275   write out browse.dat
276   ******************************************************************/
277 void write_browse_list(time_t t)
278 {
279   struct subnet_record *d;
280   pstring fname,fnamenew;
281   FILE *f;
282
283   static time_t lasttime = 0;
284
285   if (!lasttime) lasttime = t;
286   if (!updatedlists || t - lasttime < 5) return;
287   
288   lasttime = t;
289   updatedlists = False;
290   updatecount++;
291   
292   dump_names();
293   dump_workgroups();
294   
295   strcpy(fname,lp_lockdir());
296   trim_string(fname,NULL,"/");
297   strcat(fname,"/");
298   strcat(fname,SERVER_LIST);
299   strcpy(fnamenew,fname);
300   strcat(fnamenew,".");
301   
302   f = fopen(fnamenew,"w");
303   
304   if (!f)
305     {
306       DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
307       return;
308     }
309   
310   for (d = FIRST_SUBNET; d ; d = NEXT_SUBNET_INCLUDING_WINS(d))
311     {
312       struct work_record *work;
313       for (work = d->workgrouplist; work ; work = work->next)
314         {
315           struct server_record *s;
316           for (s = work->serverlist; s ; s = s->next)
317             {
318               fstring tmp;
319               
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])
322                 {
323                   continue;
324                 }
325               
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! */
330               
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);
337             }
338         }
339     }
340   
341   fclose(f);
342   unlink(fname);
343   chmod(fnamenew,0644);
344   rename(fnamenew,fname);   
345   DEBUG(3,("Wrote browse list %s\n",fname));
346 }
347