summaryrefslogtreecommitdiff
path: root/source3/nmbd/nmbd_namelistdb.c
blob: dfd8a80baa11578f3c1ec67eb3820c0b119ac72a (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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NBT netbios routines and daemon - version 2
   Copyright (C) Andrew Tridgell 1994-1997
   Copyright (C) Luke Kenneth Casson Leighton 1994-1997 
   Copyright (C) Jeremy Allison 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.
   
*/

#include "includes.h"

extern int DEBUGLEVEL;

extern pstring scope;
extern char **my_netbios_names;

uint16 samba_nb_type = 0; /* samba's NetBIOS name type */


/****************************************************************************
  Set Samba's NetBIOS name type.
  ****************************************************************************/

void set_samba_nb_type(void)
{
  if (lp_wins_support() || (*lp_wins_server()))
    samba_nb_type = NB_MFLAG; /* samba is a 'hybrid' node type */
  else
    samba_nb_type = NB_BFLAG; /* samba is broadcast-only node type */
}

/****************************************************************************
  Returns True if the netbios name is ^1^2__MSBROWSE__^2^1.

  Note: This name is registered if as a master browser or backup browser
  you are responsible for a workgroup (when you announce a domain by
  broadcasting on your local subnet, you announce it as coming from this
  name: see announce_host()).

  **************************************************************************/

BOOL ms_browser_name(char *name, int type)
{
  return (strequal(name,MSBROWSE) && (type == 0x01));
}

/****************************************************************************
  Add a netbios name into a namelist.
  **************************************************************************/

static void add_name_to_namelist(struct subnet_record *subrec, 
                                 struct name_record *namerec)
{
  struct name_record *namerec2;

  if (!subrec->namelist)
  {
    subrec->namelist = namerec;
    namerec->prev = NULL;
    namerec->next = NULL;
    return;
  }

  for (namerec2 = subrec->namelist; namerec2->next; namerec2 = namerec2->next) 
    ;

  namerec2->next = namerec;
  namerec->next = NULL;
  namerec->prev = namerec2;
  namerec->subnet = subrec;

  subrec->namelist_changed = True;
}

/****************************************************************************
  Remove a name from the namelist.
  **************************************************************************/

void remove_name_from_namelist(struct subnet_record *subrec, 
                               struct name_record *namerec)
{
  if (namerec->next)
    namerec->next->prev = namerec->prev;
  if (namerec->prev)
    namerec->prev->next = namerec->next;

  if(namerec == subrec->namelist)
    subrec->namelist = namerec->next;

  if(namerec->ip != NULL)
    free((char *)namerec->ip);
  free((char *)namerec);

  subrec->namelist_changed = True;
}


/****************************************************************************
  Find a name in a subnet.
  **************************************************************************/

struct name_record *find_name_on_subnet(struct subnet_record *subrec,
                                      struct nmb_name *nmbname, BOOL self_only)
{
  struct name_record *namerec = subrec->namelist;
  struct name_record *name_ret;
  
  for (name_ret = namerec; name_ret; name_ret = name_ret->next)
  {
    if (nmb_name_equal(&name_ret->name, nmbname))
    {
      /* Self names only - these include permanent names. */
      if (self_only && (name_ret->source != SELF_NAME) && 
              (name_ret->source != PERMANENT_NAME) )
      {
        continue;
      }
      DEBUG(9,("find_name_on_subnet: on subnet %s - found name %s source=%d\n", 
                subrec->subnet_name, namestr(nmbname), name_ret->source));
      return name_ret;
    }
  }
  DEBUG(9,("find_name_on_subnet: on subnet %s - name %s NOT FOUND\n", 
            subrec->subnet_name, namestr(nmbname)));
  return NULL;
}

/****************************************************************************
  Find a name over all known broadcast subnets.
**************************************************************************/

struct name_record *find_name_for_remote_broadcast_subnet( struct nmb_name *nmbname, 
                                                           BOOL self_only)
{
  struct subnet_record *subrec;
  struct name_record *namerec = NULL;

  for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
  {
    if((namerec = find_name_on_subnet(subrec, nmbname, self_only))!= NULL)
      break;
  }

  return namerec;
}    
  
/****************************************************************************
  Update the ttl of an entry in a subnet name list.
  ****************************************************************************/

void update_name_ttl(struct name_record *namerec, int ttl)
{
  time_t time_now = time(NULL);

  if(namerec->death_time != PERMANENT_TTL)
    namerec->death_time = time_now + ttl;

  namerec->refresh_time = time_now + (ttl/2);

  namerec->subnet->namelist_changed = True;
} 

/****************************************************************************
  Add an entry to a subnet name list.
  ****************************************************************************/

struct name_record *add_name_to_subnet(struct subnet_record *subrec,
		char *name, int type, uint16 nb_flags, int ttl, 
                enum name_source source, int num_ips, struct in_addr *iplist)
{
  struct name_record *namerec;
  time_t time_now = time(NULL);

  if((namerec = (struct name_record *)malloc(sizeof(*namerec))) == NULL)
  {
    DEBUG(0,("add_name_to_subnet: malloc fail.\n"));
    return NULL;
  }

  bzero((char *)namerec,sizeof(*namerec));

  namerec->subnet = subrec;

  namerec->num_ips = num_ips;
  namerec->ip = (struct in_addr *)malloc(sizeof(struct in_addr) * namerec->num_ips);
  if (!namerec->ip)
  {
     DEBUG(0,("add_name_to_subnet: malloc fail when creating ip_flgs.\n"));
     free((char *)namerec);
     return NULL;
  }

  bzero((char *)namerec->ip, sizeof(struct in_addr) * namerec->num_ips);

  memcpy(&namerec->ip[0], iplist, num_ips * sizeof(struct in_addr));

  make_nmb_name(&namerec->name,name,type,scope);

  /* Setup the death_time and refresh_time. */
  if(ttl == PERMANENT_TTL)
    namerec->death_time = PERMANENT_TTL;
  else
    namerec->death_time = time_now + ttl;

  namerec->refresh_time = time_now + (ttl/2);

  /* Enter the name as active. */
  namerec->nb_flags = nb_flags | NB_ACTIVE;

  /* If it's our primary name, flag it as so. */
  if(strequal(my_netbios_names[0],name))
    namerec->nb_flags |= NB_PERM;

  namerec->source = source;
  
  add_name_to_namelist(subrec,namerec);

  DEBUG(3,("add_name_to_subnet: Added netbios name %s with first IP %s ttl=%d nb_flags=%2x to subnet %s\n",
	   namestr(&namerec->name),inet_ntoa(*iplist),ttl,(unsigned int)nb_flags,
	   subrec->subnet_name));

  subrec->namelist_changed = True;

  return(namerec);
}

/*******************************************************************
 Utility function automatically called when a name refresh or register 
 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
 it).
 ******************************************************************/

void standard_success_register(struct subnet_record *subrec, 
                             struct userdata_struct *userdata,
                             struct nmb_name *nmbname, uint16 nb_flags, int ttl,
                             struct in_addr registered_ip)
{
  struct name_record *namerec = find_name_on_subnet(subrec, nmbname, FIND_SELF_NAME);

  if(namerec == NULL)
    add_name_to_subnet(subrec, nmbname->name, nmbname->name_type,
                     nb_flags, ttl, SELF_NAME, 1, &registered_ip);
  else
    update_name_ttl(namerec, ttl);
}

/*******************************************************************
 Utility function automatically called when a name refresh or register 
 fails.
 ******************************************************************/

void standard_fail_register(struct subnet_record *subrec, 
                             struct response_record *rrec, struct nmb_name *nmbname)
{
  struct name_record *namerec = find_name_on_subnet(subrec, nmbname, FIND_SELF_NAME);

  DEBUG(0,("standard_fail_register: Failed to register/refresh name %s on subnet %s\n",
           namestr(nmbname), subrec->subnet_name));

  /* Remove the name from the subnet. */
  if(namerec)
    remove_name_from_namelist(subrec, namerec);
}

/*******************************************************************
 Utility function to remove an IP address from a name record.
 ******************************************************************/

static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
{
  if(ind != namerec->num_ips)
    memmove( (char *)(&namerec->ip[ind]), (char *)(&namerec->ip[ind+1]), 
              ( namerec->num_ips - ind - 1) * sizeof(struct in_addr));

  namerec->num_ips--;
  namerec->subnet->namelist_changed = True;
}

/*******************************************************************
 Utility function to check if an IP address exists in a name record.
 ******************************************************************/

BOOL find_ip_in_name_record(struct name_record *namerec, struct in_addr ip)
{
  int i;

  for(i = 0; i < namerec->num_ips; i++)
    if(ip_equal( namerec->ip[i], ip))
      return True;

  return False;
}

/*******************************************************************
 Utility function to add an IP address to a name record.
 ******************************************************************/

void add_ip_to_name_record(struct name_record *namerec, struct in_addr new_ip)
{
  struct in_addr *new_list;

  /* Don't add one we already have. */
  if(find_ip_in_name_record( namerec, new_ip))
    return;
  
  if((new_list = (struct in_addr *)malloc( (namerec->num_ips + 1)*sizeof(struct in_addr)) )== NULL)
  {
    DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
    return;
  }

  memcpy((char *)new_list, (char *)namerec->ip, namerec->num_ips *sizeof(struct in_addr));
  new_list[namerec->num_ips] = new_ip;

  free((char *)namerec->ip);
  namerec->ip = new_list;
  namerec->num_ips += 1;

  namerec->subnet->namelist_changed = True;
}

/*******************************************************************
 Utility function to remove an IP address from a name record.
 ******************************************************************/

void remove_ip_from_name_record( struct name_record *namerec, struct in_addr remove_ip)
{
  /* Try and find the requested ip address - remove it. */
  int i;
  int orig_num = namerec->num_ips;

  for(i = 0; i < orig_num; i++)
    if( ip_equal( remove_ip, namerec->ip[i]) )
    {
      remove_nth_ip_in_record( namerec, i);
      break;
    }
}

/*******************************************************************
 Utility function that release_name callers can plug into as the
 success function when a name release is successful. Used to save
 duplication of success_function code.
 ******************************************************************/

void standard_success_release(struct subnet_record *subrec, 
                             struct userdata_struct *userdata,
                             struct nmb_name *nmbname, struct in_addr released_ip)
{
  struct name_record *namerec = find_name_on_subnet(subrec, nmbname, FIND_ANY_NAME);

  if(namerec == NULL)
  {
    DEBUG(0,("standard_success_release: Name release for name %s IP %s on subnet %s. Name \
was not found on subnet.\n", namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name));
    return;
  }
  else
  {
    int orig_num = namerec->num_ips;

    remove_ip_from_name_record( namerec, released_ip);

    if(namerec->num_ips == orig_num)
      DEBUG(0,("standard_success_release: Name release for name %s IP %s on subnet %s. This ip \
is not known for this name.\n", namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name ));
  }

  if (namerec->num_ips == 0)
    remove_name_from_namelist(subrec, namerec);
}

/*******************************************************************
  Expires old names in a subnet namelist.
  ******************************************************************/

void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
{
  struct name_record *namerec;
  struct name_record *next_namerec;

  for (namerec = subrec->namelist; namerec; namerec = next_namerec)
  {
    next_namerec = namerec->next;
    if ((namerec->death_time != PERMANENT_TTL) && (namerec->death_time < t))
    {
      if (namerec->source == SELF_NAME)
      {
        DEBUG(3,("expire_names_on_subnet: Subnet %s not expiring SELF name %s\n", 
             subrec->subnet_name, namestr(&namerec->name)));
        namerec->death_time += 300;
        namerec->subnet->namelist_changed = True;
        continue;
      }
      DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n", 
                 subrec->subnet_name, namestr(&namerec->name)));
  
      remove_name_from_namelist(subrec, namerec);
    }
  }
}

/*******************************************************************
  Expires old names in all subnet namelists.
  ******************************************************************/

void expire_names(time_t t)
{
  struct subnet_record *subrec;

  for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
  {
    expire_names_on_subnet(subrec, t);
  }
}

/****************************************************************************
  Add the magic samba names, useful for finding samba servers.
  These go directly into the name list for a particular subnet,
  without going through the normal registration process.
  When adding them to the unicast subnet, add them as a list of
  all broadcast subnet IP addresses.
**************************************************************************/

void add_samba_names_to_subnet(struct subnet_record *subrec)
{
  struct in_addr *iplist = &subrec->myip;
  int num_ips = 1;

  /* These names are added permanently (ttl of zero) and will NOT be
     refreshed.  */

  if((subrec == unicast_subnet) || (subrec == wins_server_subnet))
  {
    struct subnet_record *bcast_subrecs;
    int i;
    /* Create an IP list containing all our known subnets. */

    num_ips = iface_count();
    if((iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) )) == NULL)
    {
      DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
      return;
    }

    for(bcast_subrecs = FIRST_SUBNET, i = 0; bcast_subrecs; 
                 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++)
      iplist[i] = bcast_subrecs->myip;

  }

  add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
                     PERMANENT_NAME, num_ips, iplist);
  add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
                     PERMANENT_NAME, num_ips, iplist);
  add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
                   PERMANENT_NAME, num_ips, iplist);
  add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
                   PERMANENT_NAME, num_ips, iplist);

  if(iplist != &subrec->myip)
    free((char *)iplist);
}

/****************************************************************************
 Dump the contents of the namelists on all the subnets (including unicast)
 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
**************************************************************************/

static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
{
  struct name_record *namerec;
  char *src_type;
  struct tm *tm;
  int i;

  fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
  for (namerec = subrec->namelist; namerec; namerec = namerec->next)
  {
    fprintf(fp,"\tName = %s\t", namestr(&namerec->name));
    switch(namerec->source)
    {
      case LMHOSTS_NAME:
        src_type = "LMHOSTS_NAME";
        break;
      case WINS_PROXY_NAME:
        src_type = "WINS_PROXY_NAME";
        break;
      case REGISTER_NAME:
        src_type = "REGISTER_NAME";
        break;
      case SELF_NAME:
        src_type = "SELF_NAME";
        break;
      case DNS_NAME:
        src_type = "DNS_NAME";
        break;
      case DNSFAIL_NAME:
        src_type = "DNSFAIL_NAME";
        break;
      case PERMANENT_NAME:
        src_type = "PERMANENT_NAME";
        break;
      default:
        src_type = "unknown!";
        break;
    }
    fprintf(fp, "Source = %s\nb_flags = %x\t", src_type, namerec->nb_flags);

    if(namerec->death_time != PERMANENT_TTL)
    {
      tm = LocalTime(&namerec->death_time);
      fprintf(fp, "death_time = %s\t", asctime(tm));
    }
    else
      fprintf(fp, "death_time = PERMANENT\t");

    if(namerec->refresh_time != PERMANENT_TTL)
    {
      tm = LocalTime(&namerec->refresh_time);
      fprintf(fp, "refresh_time = %s\n", asctime(tm));
    }
    else
      fprintf(fp, "refresh_time = PERMANENT\n");

    fprintf(fp, "\t\tnumber of IPS = %d", namerec->num_ips);
    for(i = 0; i < namerec->num_ips; i++)
      fprintf(fp, "\t%s", inet_ntoa(namerec->ip[i]));

    fprintf(fp, "\n\n");
  }
}

/****************************************************************************
 Dump the contents of the namelists on all the subnets (including unicast)
 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
**************************************************************************/

void dump_all_namelists()
{
  fstring fname;
  FILE *fp; 
  struct subnet_record *subrec;

  pstrcpy(fname,lp_lockdir());
  trim_string(fname,NULL,"/");
  strcat(fname,"/"); 
  strcat(fname,"namelist.debug");

  fp = fopen(fname,"w");
     
  if (!fp)
  { 
    DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
              fname,strerror(errno)));
    return;
  }
      
  for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
    dump_subnet_namelist( subrec, fp);

  if(!we_are_a_wins_client())
    dump_subnet_namelist(unicast_subnet, fp);

  if(remote_broadcast_subnet->namelist != NULL)
    dump_subnet_namelist(remote_broadcast_subnet, fp);

  if(wins_server_subnet != NULL)
    dump_subnet_namelist( wins_server_subnet, fp);
  fclose(fp);
}