c78307f66650ac6cf85f8d9acc9685aa99dc662c
[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-1996
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 ipgrp;
40 extern struct in_addr ipzero;
41
42 extern pstring myname;
43
44 BOOL updatedlists = True;
45 int updatecount = 0;
46
47 /* local interfaces structure */
48 extern struct interface *local_interfaces;
49
50 /* remote interfaces structure */
51 extern struct interface *remote_interfaces;
52
53 /* this is our domain/workgroup/server database */
54 struct subnet_record *subnetlist = NULL;
55
56
57 /****************************************************************************
58   add a domain into the list
59   **************************************************************************/
60 static void add_subnet(struct subnet_record *d)
61 {
62   struct subnet_record *d2;
63
64   if (!subnetlist)
65   {
66     subnetlist = d;
67     d->prev = NULL;
68     d->next = NULL;
69     return;
70   }
71
72   for (d2 = subnetlist; d2->next; d2 = d2->next);
73
74   d2->next = d;
75   d->next = NULL;
76   d->prev = d2;
77 }
78
79
80 /****************************************************************************
81   find a subnet in the subnetlist 
82   **************************************************************************/
83 struct subnet_record *find_subnet(struct in_addr bcast_ip)
84 {   
85   struct subnet_record *d;
86   struct in_addr wins_ip = ipgrp;
87   
88   /* search through subnet list for broadcast/netmask that matches
89      the source ip address. a subnet 255.255.255.255 represents the
90      WINS list. */
91   
92   for (d = subnetlist; d; d = d->next)
93     {
94         if (ip_equal(bcast_ip, wins_ip))
95             {
96            if (ip_equal(bcast_ip, d->bcast_ip))
97            {
98                return d;
99            }
100         }
101         else if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
102             {
103               return(d);
104             }
105     }
106   
107   return (NULL);
108 }
109
110
111 /****************************************************************************
112   finds the appropriate subnet structure. directed packets (non-bcast) are
113   assumed to come from a point-to-point (P or M node), and so the subnet we
114   return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
115   ****************************************************************************/
116 struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
117 {
118   if (bcast)
119   {
120     /* identify the subnet the broadcast request came from */
121     return find_subnet(*iface_bcast(ip));
122   }
123   /* find the subnet under the pseudo-ip of 255.255.255.255 */
124   return find_subnet(ipgrp);
125 }
126
127
128 /****************************************************************************
129   create a domain entry
130   ****************************************************************************/
131 static struct subnet_record *make_subnet(struct in_addr bcast_ip, struct in_addr mask_ip)
132 {
133   struct subnet_record *d;
134   d = (struct subnet_record *)malloc(sizeof(*d));
135   
136   if (!d) return(NULL);
137   
138   bzero((char *)d,sizeof(*d));
139   
140   DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
141   DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
142   
143   d->bcast_ip = bcast_ip;
144   d->mask_ip  = mask_ip;
145   d->workgrouplist = NULL;
146   d->my_interface = False; /* True iff the interface is on the samba host */
147   
148   add_subnet(d);
149   
150   return d;
151 }
152
153
154 /****************************************************************************
155   add the remote interfaces from lp_remote_interfaces() and lp_interfaces()
156   to the netbios subnet database.
157   ****************************************************************************/
158 void add_subnet_interfaces(void)
159 {
160         struct interface *i;
161
162         /* loop on all local interfaces */
163         for (i = local_interfaces; i; i = i->next)
164         {
165                 /* add the interface into our subnet database */
166                 if (!find_subnet(i->bcast))
167                 {
168                     struct subnet_record *d = make_subnet(i->bcast,i->nmask);
169                         if (d)
170                         {
171                                 /* short-cut method to identifying local interfaces */
172                                 d->my_interface = True;
173                         }
174                 }
175         }
176
177         /* loop on all remote interfaces */
178         for (i = remote_interfaces; i; i = i->next)
179         {
180                 /* add the interface into our subnet database */
181                 if (!find_subnet(i->bcast))
182                 {
183                     make_subnet(i->bcast,i->nmask);
184                 }
185         }
186
187         /* add the pseudo-ip interface for WINS: 255.255.255.255 */
188         if (lp_wins_support())
189     {
190                 struct in_addr wins_bcast = ipgrp;
191                 struct in_addr wins_nmask = ipzero;
192                 make_subnet(wins_bcast, wins_nmask);
193     }
194 }
195
196
197
198 /****************************************************************************
199   add the default workgroup into my domain
200   **************************************************************************/
201 void add_my_subnets(char *group)
202 {
203   struct interface *i;
204
205   /* add or find domain on our local subnet, in the default workgroup */
206   
207   if (*group == '*') return;
208
209         /* the coding choice is up to you, andrew: i can see why you don't want
210        global access to the local_interfaces structure: so it can't get
211        messed up! */
212     for (i = local_interfaces; i; i = i->next)
213     {
214       add_subnet_entry(i->bcast,i->nmask,group, True, False);
215     }
216 }
217
218
219 /****************************************************************************
220   add a domain entry. creates a workgroup, if necessary, and adds the domain
221   to the named a workgroup.
222   ****************************************************************************/
223 struct subnet_record *add_subnet_entry(struct in_addr bcast_ip, 
224                                        struct in_addr mask_ip,
225                                        char *name, BOOL add, BOOL lmhosts)
226 {
227   struct subnet_record *d;
228
229   /* XXXX andrew: struct in_addr ip appears not to be referenced at all except
230      in the DEBUG comment. i assume that the DEBUG comment below actually
231      intends to refer to bcast_ip? i don't know.
232
233   struct in_addr ip = ipgrp;
234
235   */
236
237   if (zero_ip(bcast_ip)) 
238     bcast_ip = *iface_bcast(bcast_ip);
239   
240   /* add the domain into our domain database */
241   if ((d = find_subnet(bcast_ip)) ||
242       (d = make_subnet(bcast_ip, mask_ip)))
243     {
244       struct work_record *w = find_workgroupstruct(d, name, add);
245           extern pstring ServerComment;
246       
247       if (!w) return NULL;
248
249       /* add WORKGROUP(1e) and WORKGROUP(00) entries into name database
250          or register with WINS server, if it's our workgroup */
251       if (strequal(lp_workgroup(), name) && d->my_interface)
252         {
253           add_my_name_entry(d,name,0x1e,NB_ACTIVE|NB_GROUP);
254           add_my_name_entry(d,name,0x0 ,NB_ACTIVE|NB_GROUP);
255         }
256       /* add samba server name to workgroup list. don't add
257          lmhosts server entries to local interfaces */
258       if ((strequal(lp_workgroup(), name) && d->my_interface) ||
259           (lmhosts && !d->my_interface))
260       {
261             add_server_entry(d,w,myname,w->ServerType,0,ServerComment,True);
262         DEBUG(3,("Added server name entry %s at %s\n",
263                   name,inet_ntoa(bcast_ip)));
264       }
265       
266       return d;
267     }
268   return NULL;
269 }
270
271
272 /*******************************************************************
273   write out browse.dat
274   ******************************************************************/
275 void write_browse_list(void)
276 {
277   struct subnet_record *d;
278   pstring fname,fnamenew;
279   FILE *f;
280
281   static time_t lasttime = 0;
282   time_t t = time(NULL);
283
284   if (!lasttime) lasttime = t;
285   if (!updatedlists || t - lasttime < 5) return;
286   
287   lasttime = t;
288   updatedlists = False;
289   updatecount++;
290   
291   dump_names();
292   dump_workgroups();
293   
294   strcpy(fname,lp_lockdir());
295   trim_string(fname,NULL,"/");
296   strcat(fname,"/");
297   strcat(fname,SERVER_LIST);
298   strcpy(fnamenew,fname);
299   strcat(fnamenew,".");
300   
301   f = fopen(fnamenew,"w");
302   
303   if (!f)
304     {
305       DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
306       return;
307     }
308   
309   for (d = subnetlist; d ; d = d->next)
310     {
311       struct work_record *work;
312       for (work = d->workgrouplist; work ; work = work->next)
313         {
314           struct server_record *s;
315           for (s = work->serverlist; s ; s = s->next)
316             {
317               fstring tmp;
318               
319               /* don't list domains I don't have a master for */
320               if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
321                 {
322                   continue;
323                 }
324               
325               /* output server details, plus what workgroup/domain
326                  they're in. without the domain information, the
327                  combined list of all servers in all workgroups gets
328                  sent to anyone asking about any workgroup! */
329               
330               sprintf(tmp, "\"%s\"", s->serv.name);
331               fprintf(f, "%-25s ", tmp);
332               fprintf(f, "%08x ", s->serv.type);
333               sprintf(tmp, "\"%s\" ", s->serv.comment);
334               fprintf(f, "%-30s", tmp);
335               fprintf(f, "\"%s\"\n", work->work_group);
336             }
337         }
338     }
339   
340   fclose(f);
341   unlink(fname);
342   chmod(fnamenew,0644);
343   rename(fnamenew,fname);   
344   DEBUG(3,("Wrote browse list %s\n",fname));
345 }
346