summaryrefslogtreecommitdiff
path: root/source3/namedbsubnet.c
blob: e3a2ea6e68fce1c64c1bfe1259457758cbaf3c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NBT netbios routines and daemon - version 2
   Copyright (C) Andrew Tridgell 1994-1997
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
   Revision History:

   14 jan 96: lkcl@pires.co.uk
   added multiple workgroup domain master support

   04 jul 96: lkcl@pires.co.uk
   created module namedbsubnet containing subnet database functions

*/

#include "includes.h"
#include "smb.h"

extern int ClientNMB;
extern int ClientDGRAM;

extern int DEBUGLEVEL;

extern struct in_addr wins_ip;
extern struct in_addr ipzero;

extern pstring myname;
extern fstring myworkgroup;

BOOL updatedlists = True;
int updatecount = 0;

/* local interfaces structure */
extern struct interface *local_interfaces;

/* this is our domain/workgroup/server database */
struct subnet_record *subnetlist = NULL;

/* WINS subnet - keep this separate so enumeration code doesn't
   run onto it by mistake. */
struct subnet_record *wins_subnet = NULL;

extern uint16 nb_type; /* samba's NetBIOS name type */

/****************************************************************************
  add a domain into the list
  **************************************************************************/
static void add_subnet(struct subnet_record *d)
{
  struct subnet_record *d2;

  if (!subnetlist)
  {
    subnetlist = d;
    d->prev = NULL;
    d->next = NULL;
    return;
  }

  for (d2 = subnetlist; d2->next; d2 = d2->next);

  d2->next = d;
  d->next = NULL;
  d->prev = d2;
}


/****************************************************************************
  find a subnet in the subnetlist - not including WINS.
  **************************************************************************/
struct subnet_record *find_subnet(struct in_addr bcast_ip)
{   
  struct subnet_record *d;
  
  /* search through subnet list for broadcast/netmask that matches
     the source ip address. */
  
  for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
    {
      if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
        return d;
    }
  
  return (NULL);
}


/****************************************************************************
  finds the appropriate subnet structure. directed packets (non-bcast) are
  assumed to come from a point-to-point (P or M node), and so the subnet we
  return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
  ****************************************************************************/
struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
{
  if (bcast)
  {
    /* identify the subnet the broadcast request came from */
    return find_subnet(*iface_bcast(ip));
  }
  /* Return the subnet with the pseudo-ip of 255.255.255.255 */
  return wins_subnet;
}

/****************************************************************************
  find a subnet in the subnetlist - if the subnet is not found
  then return the WINS subnet.
  **************************************************************************/
struct subnet_record *find_subnet_all(struct in_addr bcast_ip)
{
  struct subnet_record *d = find_subnet(bcast_ip);
  if(!d)
    return wins_subnet;
  return d;
}

/****************************************************************************
  create a domain entry
  ****************************************************************************/
static struct subnet_record *make_subnet(struct in_addr bcast_ip, struct in_addr mask_ip, BOOL add)
{
  struct subnet_record *d;
  d = (struct subnet_record *)malloc(sizeof(*d));
  
  if (!d) return(NULL);
  
  bzero((char *)d,sizeof(*d));
  
  DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
  DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
  
  d->bcast_ip = bcast_ip;
  d->mask_ip  = mask_ip;
  d->workgrouplist = NULL;
  
  if(add)
    add_subnet(d);
  
  return d;
}

/****************************************************************************
  add a domain entry. creates a workgroup, if necessary, and adds the domain
  to the named a workgroup.
  ****************************************************************************/
static struct subnet_record *add_subnet_entry(struct in_addr bcast_ip, 
				       struct in_addr mask_ip, char *name, 
                                       BOOL create_subnets, BOOL add)
{
  struct subnet_record *d = NULL;

  if (zero_ip(bcast_ip)) 
    bcast_ip = *iface_bcast(bcast_ip);
  
  /* Note that we should also add into the WINS subnet as add_subnet_entry
    should be called to add NetBIOS names and server entries on all
    interfaces, including the WINS interface
   */

  if(create_subnets == True)
  {
    /* Create new subnets. */
    if((d = make_subnet(bcast_ip, mask_ip, add)) == NULL)
    {
      DEBUG(0,("add_subnet_entry: Unable to create subnet %s\n",
               inet_ntoa(bcast_ip) ));
      return NULL;
    }
    return d;
  }
  if(ip_equal(bcast_ip, wins_ip))
    return wins_subnet;
  return find_subnet(bcast_ip);
}

/****************************************************************************
 Add a workgroup into a subnet, and if it's our primary workgroup,
 add the required names to it.
**************************************************************************/

void add_workgroup_to_subnet( struct subnet_record *d, char *group)
{
  struct work_record *w = NULL;

  DEBUG(5,("add_workgroup_to_subnet: Adding workgroup %s to subnet %s\n",
            group, inet_ntoa(d->bcast_ip)));

  /* This next statement creates the workgroup struct if it doesn't
     already exist. 
   */
  if((w = find_workgroupstruct(d, group, True)) == NULL)
  {
    DEBUG(0,("add_workgroup_to_subnet: Unable to add workgroup %s to subnet %s\n",
              group, inet_ntoa(d->bcast_ip) ));
    return;
  }

  /* add WORKGROUP(00) entries into name database
     or register with WINS server, if it's our workgroup.
   */
  if (strequal(myworkgroup, group))
  {
    add_my_name_entry(d,group,0x0 ,nb_type|NB_ACTIVE|NB_GROUP,False);
    add_my_name_entry(d,group,0x1e,nb_type|NB_ACTIVE|NB_GROUP,False);
    /* add samba server name to workgroup list. */
    add_server_entry(d,w,myname,w->ServerType|SV_TYPE_LOCAL_LIST_ONLY,0,
		lp_serverstring(),True);
    DEBUG(3,("add_workgroup_to_subnet: Added server name entry %s to subnet %s\n",
                myname, inet_ntoa(d->bcast_ip)));
  }
}

/****************************************************************************
  create subnet / workgroup / server entries
     
  - add or create the subnet lists
  - add or create the workgroup entries in each subnet entry
  - register appropriate NetBIOS names for the workgroup entries
     
**************************************************************************/
void add_my_subnets(char *group)
{    
  static BOOL create_subnets = True;
  struct subnet_record *d = NULL;
  struct interface *i = NULL;

  if (*group == '*') return;

  /* Create subnets from all the local interfaces and thread them onto
     the linked list. 
   */
  for (i = local_interfaces; i; i = i->next)
  {
    add_subnet_entry(i->bcast,i->nmask,group, create_subnets, True);
  }

  /* If we are using WINS, then we must add the workgroup to the WINS
     subnet. This is used as a place to keep collated server lists.
   */

  /* Create the WINS subnet if we are using WINS - but don't thread it
     onto the linked subnet list. 
   */    
  if (lp_wins_support() || lp_wins_server())
  {
    struct in_addr wins_nmask = ipzero;
    wins_subnet = add_subnet_entry(wins_ip, wins_nmask, group, create_subnets, False);
  }

  /* Ensure we only create the subnets once. */
  create_subnets = False;

  /* Now we have created all the subnets - we can add the names
     that make us a client member in the workgroup.
   */
  for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
    add_workgroup_to_subnet(d, group);
}

/*******************************************************************
  write out browse.dat
  ******************************************************************/
void write_browse_list(time_t t)
{
  struct subnet_record *d;
  pstring fname,fnamenew;
  FILE *f;

  static time_t lasttime = 0;

  if (!lasttime) lasttime = t;
  if (!updatedlists || t - lasttime < 5) return;
  
  lasttime = t;
  updatedlists = False;
  updatecount++;
  
  dump_names();
  dump_workgroups();
  
  strcpy(fname,lp_lockdir());
  trim_string(fname,NULL,"/");
  strcat(fname,"/");
  strcat(fname,SERVER_LIST);
  strcpy(fnamenew,fname);
  strcat(fnamenew,".");
  
  f = fopen(fnamenew,"w");
  
  if (!f)
    {
      DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
      return;
    }
  
  for (d = FIRST_SUBNET; d ; d = NEXT_SUBNET_INCLUDING_WINS(d))
    {
      struct work_record *work;
      for (work = d->workgrouplist; work ; work = work->next)
	{
	  struct server_record *s;
	  for (s = work->serverlist; s ; s = s->next)
	    {
	      fstring tmp;
	      
	      /* don't list domains I don't have a master for */
	      if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
		{
		  continue;
		}
	      
	      /* output server details, plus what workgroup/domain
		 they're in. without the domain information, the
		 combined list of all servers in all workgroups gets
		 sent to anyone asking about any workgroup! */
	      
	      sprintf(tmp, "\"%s\"", s->serv.name);
	      fprintf(f, "%-25s ", tmp);
	      fprintf(f, "%08x ", s->serv.type);
	      sprintf(tmp, "\"%s\" ", s->serv.comment);
	      fprintf(f, "%-30s", tmp);
	      fprintf(f, "\"%s\"\n", work->work_group);
	    }
	}
    }
  
  fclose(f);
  unlink(fname);
  chmod(fnamenew,0644);
  rename(fnamenew,fname);   
  DEBUG(3,("Wrote browse list %s\n",fname));
}