summaryrefslogtreecommitdiff
path: root/source3/nameserv.c
blob: abad9b28c2ab4e1822645bf7491aaa8a8636251a (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* 
   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.
   
   Module name: nameserv.c

   Revision History:

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

   04 jul 96: lkcl@pires.co.uk
   module nameserv contains name server management functions
*/

#include "includes.h"

extern int ClientNMB;

extern int DEBUGLEVEL;

extern pstring scope;
extern pstring myname;
extern fstring myworkgroup;
extern struct in_addr ipzero;
extern struct in_addr wins_ip;

extern struct subnet_record *subnetlist;

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

/****************************************************************************
  remove an entry from the name list

  note: the name will _always_ be removed
  XXXX at present, the name is removed _even_ if a WINS server says keep it.

  ****************************************************************************/
void remove_name_entry(struct subnet_record *d, char *name,int type)
{
  /* XXXX BUG: if samba is offering WINS support, it should still broadcast
      a de-registration packet to the local subnet before removing the
      name from its local-subnet name database. */

  int search = FIND_SELF;
  struct name_record n;
  struct name_record *n2=NULL;
      
  make_nmb_name(&n.name,name,type,scope);

  if(d == wins_subnet)
    search |= FIND_WINS;
  else
    search |= FIND_LOCAL;

  if ((n2 = find_name_search(&d, &n.name, search, ipzero)))
  {
    /* check name isn't already being de-registered */
    if (NAME_DEREG(n2->ip_flgs[0].nb_flags))
      return;

    /* mark the name as in the process of deletion. */
    n2->ip_flgs[0].nb_flags &= NB_DEREG;
  }

  if (!n2) return;

  /* Only remove names with non-zero death times. */
  if(n2->death_time == 0)
  {
    DEBUG(5,("remove_name_entry: Name %s(%d) has zero ttl - not removing.\n",
             name, type));
    return;
  }

  /* remove the name immediately. even if the spec says we should
     first try to release them, this is too dangerous with our current
     name structures as otherwise we will end up replying to names we
     don't really own */  
  remove_netbios_name(d,name,type,SELF,n2->ip_flgs[0].ip);

  if (ip_equal(d->bcast_ip, wins_ip))
  {
    if (!lp_wins_support())
    {
      /* not a WINS server: we have to release them on the network */
      queue_netbios_pkt_wins(ClientNMB,NMB_REL,NAME_RELEASE,
			     name, type, 0, 0,0,NULL,NULL,
			     False, True, ipzero, ipzero);
    }
  }
  else
  {
      /* local interface: release them on the network */
      queue_netbios_packet(d,ClientNMB,NMB_REL,NAME_RELEASE,
			 name, type, 0, 0,0,NULL,NULL,
			 True, False, d->bcast_ip, d->bcast_ip);
  }
}


/****************************************************************************
  add an entry to the name list
 
  big note: our name will _always_ be added (if there are no objections).
  it's just a matter of when this will be done (e.g after a time-out).

  ****************************************************************************/
void add_my_name_entry(struct subnet_record *d,char *name,int type,int nb_flags)
{
  BOOL re_reg = False;
  struct nmb_name n;

  if (!d) return;

  /* not that it particularly matters, but if the SELF name already exists,
     it must be re-registered, rather than just registered */

  make_nmb_name(&n, name, type, scope);
  if (find_name(d->namelist, &n, SELF))
	re_reg = True;

  /* XXXX BUG: if samba is offering WINS support, it should still add the
     name entry to a local-subnet name database. see rfc1001.txt 15.1.1 p28
     regarding the point about M-nodes. */

  if (ip_equal(d->bcast_ip, wins_ip))
  {
    if (lp_wins_support())
    {
      /* we are a WINS server. */
      if(lp_wins_support())
      {
        DEBUG(4,("add_my_name_entry: samba as WINS server adding: "));
      }
        
      /* this will call add_netbios_entry() */
      name_register_work(d, name, type, nb_flags,0, ipzero, False);
    }
    else
    {
      DEBUG(4,("add_my_name_entry registering name %s with WINS server.\n",
                name));
      
      /* a time-to-live allows us to refresh this name with the WINS server. */
  	  queue_netbios_pkt_wins(ClientNMB,
				 re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
			     name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
			     False, True, ipzero, ipzero);
    }
  }
  else
  {
    /* broadcast the packet, but it comes from ipzero */
    queue_netbios_packet(d,ClientNMB,
	 re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
         name, type, nb_flags, GET_TTL(0),0,NULL,NULL,
	 True, False, d->bcast_ip, ipzero);
  }
}


/****************************************************************************
  add the internet group <1c> domain logon names by wins unicast and broadcast.
  ****************************************************************************/
void add_domain_logon_names(void)
{
	struct subnet_record *d;

	if (!lp_domain_logons()) return;

	for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
	{
		struct work_record *work = find_workgroupstruct(d, myworkgroup, True);

		if (work && work->log_state == LOGON_NONE)
		{
			struct nmb_name n;
			make_nmb_name(&n,myworkgroup,0x1c,scope);

			if (!find_name(d->namelist, &n, FIND_SELF))
			{
				/* logon servers are group names. don't expect failure */

				DEBUG(0,("%s attempting to become logon server for %s %s\n",
						timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));

				become_logon_server(d, work);
			}
		}
	}
}


/****************************************************************************
  add the <1b> domain master names by broadcast.
  ****************************************************************************/
void add_domain_master_bcast(void)
{
	struct subnet_record *d;

	if (!lp_domain_master()) return;

	for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
	{ 
		struct work_record *work = find_workgroupstruct(d, myworkgroup, True);

		if (work && work->dom_state == DOMAIN_NONE)
		{
			struct nmb_name n;
			make_nmb_name(&n,myworkgroup,0x1b,scope);

			if (!find_name(d->namelist, &n, FIND_SELF))
			{
				DEBUG(0,("%s add_domain_names: attempting to become domain \
master browser on workgroup %s %s\n",
				timestring(), myworkgroup, inet_ntoa(d->bcast_ip)));

				/* send out a query to establish whether there's a 
				   domain controller on the local subnet.  if not,
				   we can become a domain controller.  it's only
				   polite that we check, before claiming the
				   NetBIOS name 0x1b.
				*/

				DEBUG(0,("add_domain_names:querying subnet %s \
for domain master on workgroup %s\n", 
						  inet_ntoa(d->bcast_ip), myworkgroup));

				queue_netbios_packet(d,ClientNMB,NMB_QUERY,
									 NAME_QUERY_DOMAIN,
									 myworkgroup, 0x1b,
									 0, 0,0,NULL,NULL,
									 True, False,
									 d->bcast_ip, d->bcast_ip);
			}
		}
	}
}


/****************************************************************************
  add the <1b> domain master name by wins unicast.
  ****************************************************************************/
void add_domain_master_wins(void)
{
	struct work_record *work;

	if (!lp_domain_master() || wins_subnet == NULL) return;

	work = find_workgroupstruct(wins_subnet, myworkgroup, True);

	if (work && work->dom_state == DOMAIN_NONE)
	{
		struct nmb_name n;
		make_nmb_name(&n,myworkgroup,0x1b,scope);

		if (!find_name(wins_subnet->namelist, &n, FIND_SELF))
		{
			DEBUG(0,("%s add_domain_names: attempting to become domain \
master browser on workgroup %s %s\n",
			timestring(), myworkgroup, inet_ntoa(wins_subnet->bcast_ip)));

			if (lp_wins_support())
			{
				/* use the wins server's capabilities (indirectly).  if
				   someone has already registered the domain<1b>
				   name with the WINS server, then the WINS
				   server's job is to _check_ that the owner still
				   wants it, before giving it away.
				*/

				DEBUG(1,("%s initiate become domain master for %s\n",
						  timestring(), myworkgroup));

				become_domain_master(wins_subnet, work);
			}
			else
			{
				/* send out a query to establish whether there's a 
				   domain controller on the WINS subnet.  if not,
				   we can become a domain controller.  it's only
				   polite that we check, before claiming the
				   NetBIOS name 0x1b.
				*/

				DEBUG(0,("add_domain_names:querying WINS \
for domain master on workgroup %s\n", myworkgroup));

				queue_netbios_pkt_wins(ClientNMB,NMB_QUERY,
									   NAME_QUERY_DOMAIN,
									   myworkgroup, 0x1b,
									   0, 0,0,NULL,NULL,
									   False, True, ipzero, ipzero);
			}
		}
	}
}


/****************************************************************************
  add the domain logon server and domain master browser names 

  this code was written so that several samba servers can co-operate in
  sharing the task of (one server) being a domain master, and of being
  domain logon servers.

  **************************************************************************/
void add_domain_names(time_t t)
{
	static time_t lastrun = 0;

	if (lastrun != 0 && t < lastrun + CHECK_TIME_ADD_DOM_NAMES * 60) return;
	lastrun = t;

	/* do the "internet group" - <1c> names */
	add_domain_logon_names();

	/* do the domain master names */
	if (wins_subnet != NULL)
	{
		/* if the registration of the <1b> name is successful, then
		   add_domain_master_bcast() will be called.  this will
		   result in domain logon services being gracefully provided,
		   as opposed to the aggressive nature of 1.9.16p2 to 1.9.16p11.

		   which, due to a bug in namelogon.c from 1.9.16p2 to 1.9.16p11
		   cannot _provide_ domain master / domain logon services!!!

		 */
		add_domain_master_wins();
	}
	else
	{
		add_domain_master_bcast();
	}
}

/****************************************************************************
  add the magic samba names, useful for finding samba servers
  **************************************************************************/
void add_my_names(void)
{
  struct subnet_record *d;
  /* each subnet entry, including WINS pseudo-subnet, has SELF names */

  /* XXXX if there was a transport layer added to samba (ipx/spx etc) then
     there would be yet _another_ for-loop, this time on the transport type
   */

  for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
  {
    BOOL wins = (lp_wins_support() && (d == wins_subnet));

    add_my_name_entry(d, myname,0x20,nb_type|NB_ACTIVE);
    add_my_name_entry(d, myname,0x03,nb_type|NB_ACTIVE);
    add_my_name_entry(d, myname,0x00,nb_type|NB_ACTIVE);
    
    /* these names are added permanently (ttl of zero) and will NOT be
       refreshed with the WINS server  */
    add_netbios_entry(d,"*",0x0,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
    add_netbios_entry(d,"*",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
    add_netbios_entry(d,"__SAMBA__",0x20,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
    add_netbios_entry(d,"__SAMBA__",0x00,nb_type|NB_ACTIVE,0,SELF,d->myip,False,wins);
  }
}


/****************************************************************************
  remove all the samba names... from a WINS server if necessary.
  **************************************************************************/
void remove_my_names()
{
	struct subnet_record *d;

	for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
	{
		struct name_record *n, *next;

		for (n = d->namelist; n; n = next)
		{
			next = n->next;
			if (n->source == SELF)
			{
				/* get all SELF names removed from the WINS server's database */
				/* XXXX note: problem occurs if this removes the wrong one! */

				remove_name_entry(d,n->name.name, n->name.name_type);
			}
		}
	}
}


/*******************************************************************
  refresh my own names
  ******************************************************************/
void refresh_my_names(time_t t)
{
  struct subnet_record *d;

  for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
  {
    struct name_record *n;
	  
    for (n = d->namelist; n; n = n->next)
    {
      /* each SELF name has an individual time to be refreshed */
      if (n->source == SELF && n->refresh_time < t && 
          n->death_time != 0)
      {
        add_my_name_entry(d,n->name.name,n->name.name_type,
                          n->ip_flgs[0].nb_flags);
	/* they get a new lease on life :-) */
	n->death_time += GET_TTL(0);
	n->refresh_time += GET_TTL(0);
      }
    }
  }
}


/*******************************************************************
  queries names occasionally. an over-cautious, non-trusting WINS server!

  this function has been added because nmbd could be restarted. it
  is generally a good idea to check all the names that have been
  reloaded from file.

  XXXX which names to poll and which not can be refined at a later date.
  ******************************************************************/
void query_refresh_names(time_t t)
{
	struct name_record *n;
	struct subnet_record *d = wins_subnet;

	static time_t lasttime = 0;

	int count = 0;
	int name_refresh_time = NAME_POLL_REFRESH_TIME;
	int max_count = name_refresh_time * 2 / NAME_POLL_INTERVAL;
	if (max_count > 10) max_count = 10;

	name_refresh_time = NAME_POLL_INTERVAL * max_count / 2;

	/* if (!lp_poll_wins()) return; polling of registered names allowed */

	if (!d) return;

    if (!lasttime) lasttime = t;
	if (t - lasttime < NAME_POLL_INTERVAL) return;

    lasttime = time(NULL);

	for (n = d->namelist; n; n = n->next)
	{
		/* only do unique, registered names */

		if (n->source != REGISTER) continue;
		if (!NAME_GROUP(n->ip_flgs[0].nb_flags)) continue;

		if (n->refresh_time < t)
		{
		  DEBUG(3,("Polling name %s\n", namestr(&n->name)));
		  
    	  queue_netbios_packet(d,ClientNMB,NMB_QUERY,NAME_QUERY_CONFIRM,
				n->name.name, n->name.name_type,
				0,0,0,NULL,NULL,
				False,False,n->ip_flgs[0].ip,n->ip_flgs[0].ip);
		  count++;
		}

		if (count >= max_count)
		{
			/* don't do too many of these at once, but do enough to
			   cover everyone in the list */
			return;
		}

		/* this name will be checked on again, if it's not removed */
		n->refresh_time += name_refresh_time;
	}
}