I am removing these from the source code in preparation for an
[kai/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 extern int global_nmb_port;
37
38 extern int DEBUGLEVEL;
39
40 extern struct in_addr wins_ip;
41 extern struct in_addr ipzero;
42
43 extern pstring myname;
44 extern fstring myworkgroup;
45 extern char **my_netbios_names;
46
47 BOOL updatedlists = True;
48 int updatecount = 0;
49
50 /* local interfaces structure */
51 extern struct interface *local_interfaces;
52
53 /* this is our domain/workgroup/server database */
54 struct subnet_record *subnetlist = NULL;
55
56 /* WINS subnet - keep this separate so enumeration code doesn't
57    run onto it by mistake. */
58 struct subnet_record *wins_subnet = NULL;
59
60 extern uint16 nb_type; /* samba's NetBIOS name type */
61
62 /****************************************************************************
63   add a domain into the list
64   **************************************************************************/
65 static void add_subnet(struct subnet_record *d)
66 {
67   struct subnet_record *d2;
68
69   if (!subnetlist)
70   {
71     subnetlist = d;
72     d->prev = NULL;
73     d->next = NULL;
74     return;
75   }
76
77   for (d2 = subnetlist; d2->next; d2 = d2->next);
78
79   d2->next = d;
80   d->next = NULL;
81   d->prev = d2;
82 }
83
84
85 /****************************************************************************
86   find a subnet in the subnetlist - not including WINS.
87   **************************************************************************/
88 struct subnet_record *find_subnet(struct in_addr bcast_ip)
89 {   
90   struct subnet_record *d;
91   
92   /* search through subnet list for broadcast/netmask that matches
93      the source ip address. */
94   
95   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
96     {
97       if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
98         return d;
99     }
100   
101   return (NULL);
102 }
103
104
105 /****************************************************************************
106   finds the appropriate subnet structure. directed packets (non-bcast) are
107   assumed to come from a point-to-point (P or M node), and so the subnet we
108   return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
109   ****************************************************************************/
110 struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
111 {
112   if (bcast)
113   {
114     /* identify the subnet the broadcast request came from */
115     return find_subnet(*iface_bcast(ip));
116   }
117   /* Return the subnet with the pseudo-ip of 255.255.255.255 */
118   return wins_subnet;
119 }
120
121 /****************************************************************************
122   find a subnet in the subnetlist - if the subnet is not found
123   then return the WINS subnet.
124   **************************************************************************/
125 struct subnet_record *find_subnet_all(struct in_addr bcast_ip)
126 {
127   struct subnet_record *d = find_subnet(bcast_ip);
128   if(!d)
129     return wins_subnet;
130   return d;
131 }
132
133 /****************************************************************************
134   create a subnet entry
135   ****************************************************************************/
136 static struct subnet_record *make_subnet(struct in_addr myip, struct in_addr bcast_ip, 
137                                          struct in_addr mask_ip, BOOL add)
138 {
139   struct subnet_record *d = NULL;
140   int nmb_sock, dgram_sock;
141
142   /* Check if we are creating the WINS subnet - if so don't create
143      sockets, use the ClientNMB and ClientDGRAM sockets instead.
144    */
145
146   if(ip_equal(bcast_ip, wins_ip))
147   {
148     nmb_sock = -1;
149     dgram_sock = -1;
150   }
151   else
152   {
153     /*
154      * Attempt to open the sockets on port 137/138 for this interface
155      * and bind them.
156      * Fail the subnet creation if this fails.
157      */
158
159     if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr)) == -1)
160     {
161       DEBUG(0,("make_subnet: Failed to open nmb socket on interface %s \
162 for port %d. Error was %s\n", inet_ntoa(myip), global_nmb_port, strerror(errno)));
163       return NULL;
164     }
165
166     if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr)) == -1)
167     {
168       DEBUG(0,("make_subnet: Failed to open dgram socket on interface %s \
169 for port %d. Error was %s\n", inet_ntoa(myip), DGRAM_PORT, strerror(errno)));
170       return NULL;
171     }
172
173     /* Make sure we can broadcast from these sockets. */
174     set_socket_options(nmb_sock,"SO_BROADCAST");
175     set_socket_options(dgram_sock,"SO_BROADCAST");
176
177   }
178
179   d = (struct subnet_record *)malloc(sizeof(*d));
180   
181   if (!d) 
182   {
183     DEBUG(0,("make_subnet: malloc fail !\n"));
184     close(nmb_sock);
185     close(dgram_sock);
186     return(NULL);
187   }
188   
189   bzero((char *)d,sizeof(*d));
190   
191   DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
192   DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
193   
194   d->bcast_ip = bcast_ip;
195   d->mask_ip  = mask_ip;
196   d->myip = myip;
197   d->nmb_sock = nmb_sock;
198   d->dgram_sock = dgram_sock;
199   d->workgrouplist = NULL;
200   
201   if(add)
202     add_subnet(d);
203   
204   return d;
205 }
206
207 /****************************************************************************
208   add a domain entry. creates a workgroup, if necessary, and adds the domain
209   to the named a workgroup.
210   ****************************************************************************/
211 static struct subnet_record *add_subnet_entry(struct in_addr myip,
212                                        struct in_addr bcast_ip, 
213                                        struct in_addr mask_ip, char *name, 
214                                        BOOL create_subnets, BOOL add)
215 {
216   struct subnet_record *d = NULL;
217
218   if (zero_ip(bcast_ip)) 
219     bcast_ip = *iface_bcast(bcast_ip);
220   
221   /* Note that we should also add into the WINS subnet as add_subnet_entry
222     should be called to add NetBIOS names and server entries on all
223     interfaces, including the WINS interface
224    */
225
226   if(create_subnets == True)
227   {
228     /* Create new subnets. */
229     if((d = make_subnet(myip, bcast_ip, mask_ip, add)) == NULL)
230     {
231       DEBUG(0,("add_subnet_entry: Unable to create subnet %s\n",
232                inet_ntoa(bcast_ip) ));
233       return NULL;
234     }
235     return d;
236   }
237   if(ip_equal(bcast_ip, wins_ip))
238     return wins_subnet;
239   return find_subnet(bcast_ip);
240 }
241
242 /****************************************************************************
243  Add a workgroup into a subnet, and if it's our primary workgroup,
244  add the required names to it.
245 **************************************************************************/
246
247 void add_workgroup_to_subnet( struct subnet_record *d, char *group)
248 {
249   struct work_record *w = NULL;
250
251   DEBUG(5,("add_workgroup_to_subnet: Adding workgroup %s to subnet %s\n",
252             group, inet_ntoa(d->bcast_ip)));
253
254   /* This next statement creates the workgroup struct if it doesn't
255      already exist. 
256    */
257   if((w = find_workgroupstruct(d, group, True)) == NULL)
258   {
259     DEBUG(0,("add_workgroup_to_subnet: Unable to add workgroup %s to subnet %s\n",
260               group, inet_ntoa(d->bcast_ip) ));
261     return;
262   }
263
264   /* add WORKGROUP(00) entries into name database
265      or register with WINS server, if it's our workgroup.
266    */
267   if (strequal(myworkgroup, group))
268   {
269     int n;
270
271     add_my_name_entry(d,group,0x0 ,nb_type|NB_ACTIVE|NB_GROUP);
272
273     /* Only register the WORKGROUP<0x1e> name if we could be a local master
274        browser. */
275     if(lp_local_master())
276       add_my_name_entry(d,group,0x1e,nb_type|NB_ACTIVE|NB_GROUP);
277
278     /* Add all our server names to the workgroup list. We remove any
279        browser or logon server flags from all but the primary name.
280      */
281     for( n = 0; my_netbios_names[n]; n++)
282     {    
283       char *name = my_netbios_names[n];
284       int stype = w->ServerType;
285
286       if(!strequal(myname, name))
287           stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|
288                      SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
289
290       add_server_entry(d,w,name,stype|SV_TYPE_LOCAL_LIST_ONLY,0,
291                 lp_serverstring(),True);
292       DEBUG(3,("add_workgroup_to_subnet: Added server name entry %s \
293 to subnet %s\n", name, inet_ntoa(d->bcast_ip)));
294     }
295   }
296 }
297
298 /****************************************************************************
299   create subnet / workgroup / server entries
300      
301   - add or create the subnet lists
302   - add or create the workgroup entries in each subnet entry
303   - register appropriate NetBIOS names for the workgroup entries
304      
305 **************************************************************************/
306 void add_my_subnets(char *group)
307 {    
308   static BOOL create_subnets = True;
309   struct subnet_record *d = NULL;
310   struct interface *i = NULL;
311
312   if (*group == '*') return;
313
314   /* Create subnets from all the local interfaces and thread them onto
315      the linked list. 
316    */
317   for (i = local_interfaces; i; i = i->next)
318   {
319     add_subnet_entry(i->ip, i->bcast,i->nmask,group, create_subnets, True);
320   }
321
322   /* If we are using WINS, then we must add the workgroup to the WINS
323      subnet. This is used as a place to keep collated server lists.
324    */
325
326   /* Create the WINS subnet if we are using WINS - but don't thread it
327      onto the linked subnet list. 
328    */    
329   if (lp_wins_support() || lp_wins_server())
330   {
331     struct in_addr wins_nmask = ipzero;
332     wins_subnet = add_subnet_entry(ipzero, wins_ip, wins_nmask, group, create_subnets, False);
333   }
334
335   /* Ensure we only create the subnets once. */
336   create_subnets = False;
337
338   /* Now we have created all the subnets - we can add the names
339      that make us a client member in the workgroup.
340    */
341   for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
342     add_workgroup_to_subnet(d, group);
343 }
344
345 /*******************************************************************
346   write out browse.dat
347   ******************************************************************/
348 void write_browse_list(time_t t)
349 {
350   struct subnet_record *d;
351   pstring fname,fnamenew;
352   FILE *f;
353
354   static time_t lasttime = 0;
355
356   if (!lasttime) lasttime = t;
357   if (!updatedlists || t - lasttime < 5) return;
358   
359   lasttime = t;
360   updatedlists = False;
361   updatecount++;
362   
363   dump_names();
364   dump_workgroups();
365   
366   pstrcpy(fname,lp_lockdir());
367   trim_string(fname,NULL,"/");
368   strcat(fname,"/");
369   strcat(fname,SERVER_LIST);
370   pstrcpy(fnamenew,fname);
371   strcat(fnamenew,".");
372   
373   f = fopen(fnamenew,"w");
374   
375   if (!f)
376     {
377       DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
378       return;
379     }
380   
381   for (d = FIRST_SUBNET; d ; d = NEXT_SUBNET_INCLUDING_WINS(d))
382     {
383       struct work_record *work;
384       for (work = d->workgrouplist; work ; work = work->next)
385         {
386           struct server_record *s;
387           for (s = work->serverlist; s ; s = s->next)
388             {
389               fstring tmp;
390               
391               /* don't list domains I don't have a master for */
392               if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
393                 {
394                   continue;
395                 }
396               
397               /* output server details, plus what workgroup/domain
398                  they're in. without the domain information, the
399                  combined list of all servers in all workgroups gets
400                  sent to anyone asking about any workgroup! */
401               
402               sprintf(tmp, "\"%s\"", s->serv.name);
403               fprintf(f, "%-25s ", tmp);
404               fprintf(f, "%08x ", s->serv.type);
405               sprintf(tmp, "\"%s\" ", s->serv.comment);
406               fprintf(f, "%-30s", tmp);
407               fprintf(f, "\"%s\"\n", work->work_group);
408             }
409         }
410     }
411   
412   fclose(f);
413   unlink(fname);
414   chmod(fnamenew,0644);
415   rename(fnamenew,fname);   
416   DEBUG(3,("Wrote browse list %s\n",fname));
417 }
418