0bc5fd875a7a42861eb91a25cb9a4cc1772bebc4
[tprouty/samba.git] / source3 / nmbd / nmbd_namelistdb.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-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7    Copyright (C) Jeremy Allison 1994-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 */
24
25 #include "includes.h"
26
27 extern int DEBUGLEVEL;
28
29 extern pstring scope;
30 extern char **my_netbios_names;
31
32 uint16 samba_nb_type = 0; /* samba's NetBIOS name type */
33
34
35 /* ************************************************************************** **
36  * Set Samba's NetBIOS name type.
37  * ************************************************************************** **
38  */
39 void set_samba_nb_type(void)
40   {
41   if( lp_wins_support() || (*lp_wins_server()) )
42     samba_nb_type = NB_MFLAG;               /* samba is a 'hybrid' node type. */
43   else
44     samba_nb_type = NB_BFLAG;           /* samba is broadcast-only node type. */
45
46         DEBUG(10,("set_samba_nb_type: %x\n", samba_nb_type));
47   } /* set_samba_nb_type */
48
49 /* ************************************************************************** **
50  * Convert a NetBIOS name to upper case.
51  * ************************************************************************** **
52  */
53 static void upcase_name( struct nmb_name *target, struct nmb_name *source )
54   {
55   int i;
56
57   if( NULL != source )
58     (void)memcpy( target, source, sizeof( struct nmb_name ) );
59
60   strupper( target->name );
61   strupper( target->scope );
62
63   /* fudge... We're using a byte-by-byte compare, so we must be sure that
64    * unused space doesn't have garbage in it.
65    */
66   for( i = strlen( target->name ); i < sizeof( target->name ); i++ )
67     target->name[i] = '\0';
68   for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ )
69     target->scope[i] = '\0';
70   } /* upcase_name */
71
72 /* ************************************************************************** **
73  * Add a new or overwrite an existing namelist entry.
74  * ************************************************************************** **
75  */
76 static void update_name_in_namelist( struct subnet_record *subrec,
77                               struct name_record   *namerec )
78   {
79   struct name_record *oldrec = NULL;
80
81   (void)ubi_trInsert( subrec->namelist, namerec, &(namerec->name), &oldrec );
82   if( oldrec )
83     {
84     if( oldrec->data.ip )
85       free( oldrec->data.ip );
86     free( oldrec );
87     }
88   } /* update_name_in_namelist */
89
90 /* ************************************************************************** **
91  * Remove a name from the namelist.
92  * ************************************************************************** **
93  */
94 void remove_name_from_namelist( struct subnet_record *subrec, 
95                                 struct name_record   *namerec )
96   {
97   (void)ubi_trRemove( subrec->namelist, namerec );
98
99   if(namerec->data.ip != NULL)
100     free((char *)namerec->data.ip);
101
102   ZERO_STRUCTP(namerec);
103   free((char *)namerec);
104
105   subrec->namelist_changed = True;
106   } /* remove_name_from_namelist */
107
108 /* ************************************************************************** **
109  * Find a name in a subnet.
110  * ************************************************************************** **
111  */
112 struct name_record *find_name_on_subnet( struct subnet_record *subrec,
113                                          struct nmb_name      *nmbname,
114                                          BOOL                  self_only )
115   {
116   struct nmb_name     uc_name[1];
117   struct name_record *name_ret;
118
119   upcase_name( uc_name, nmbname );
120   name_ret = (struct name_record *)ubi_trFind( subrec->namelist, uc_name );
121   if( name_ret )
122     {
123     /* Self names only - these include permanent names. */
124     if( self_only
125      && (name_ret->data.source != SELF_NAME)
126      && (name_ret->data.source != PERMANENT_NAME) )
127       {
128       DEBUG( 9, 
129              ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
130                subrec->subnet_name, nmb_namestr(nmbname) ) );
131       return( NULL );
132       }
133     DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
134                subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) );
135     return( name_ret );
136     }
137   DEBUG( 9, 
138          ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
139            subrec->subnet_name, nmb_namestr(nmbname) ) );
140   return( NULL );
141   } /* find_name_on_subnet */
142
143 /* ************************************************************************** **
144  * Find a name over all known broadcast subnets.
145  * ************************************************************************** **
146  */
147 struct name_record *find_name_for_remote_broadcast_subnet(
148                                                    struct nmb_name *nmbname,
149                                                    BOOL             self_only )
150   {
151   struct subnet_record *subrec;
152   struct name_record   *namerec = NULL;
153
154   for( subrec = FIRST_SUBNET;
155        subrec;
156        subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
157     {
158     if( NULL != (namerec = find_name_on_subnet(subrec, nmbname, self_only)) )
159       break;
160     }
161
162   return( namerec );
163   } /* find_name_for_remote_broadcast_subnet */
164   
165 /* ************************************************************************** **
166  * Update the ttl of an entry in a subnet name list.
167  * ************************************************************************** **
168  */
169 void update_name_ttl( struct name_record *namerec, int ttl )
170 {
171   time_t time_now = time(NULL);
172
173   if( namerec->data.death_time != PERMANENT_TTL )
174     namerec->data.death_time = time_now + ttl;
175
176   namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
177
178   namerec->subnet->namelist_changed = True;
179 } /* update_name_ttl */
180
181 /* ************************************************************************** **
182  * Add an entry to a subnet name list.
183  * ************************************************************************** **
184  */
185 struct name_record *add_name_to_subnet( struct subnet_record *subrec,
186                                         char                 *name,
187                                         int                   type,
188                                         uint16                nb_flags,
189                                         int                   ttl,
190                                         enum name_source      source,
191                                         int                   num_ips,
192                                         struct in_addr       *iplist)
193 {
194   struct name_record *namerec;
195   time_t time_now = time(NULL);
196
197   namerec = (struct name_record *)malloc( sizeof(*namerec) );
198   if( NULL == namerec )
199   {
200     DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
201     return( NULL );
202   }
203
204   bzero( (char *)namerec, sizeof(*namerec) );
205   namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr) 
206                                                * num_ips );
207   if( NULL == namerec->data.ip )
208   {
209      DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
210
211      ZERO_STRUCTP(namerec);
212      free( (char *)namerec );
213      return NULL;
214   }
215
216   namerec->subnet = subrec;
217
218   make_nmb_name( &namerec->name, name, type, scope );
219   upcase_name( &namerec->name, NULL );
220
221   /* Enter the name as active. */
222   namerec->data.nb_flags = nb_flags | NB_ACTIVE;
223
224   /* If it's our primary name, flag it as so. */
225   if( strequal( my_netbios_names[0], name ) )
226     namerec->data.nb_flags |= NB_PERM;
227
228   /* Copy the IPs. */
229   namerec->data.num_ips = num_ips;
230   memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
231
232   /* Data source. */
233   namerec->data.source = source;
234
235   /* Setup the death_time and refresh_time. */
236   if( ttl == PERMANENT_TTL )
237     namerec->data.death_time = PERMANENT_TTL;
238   else
239     namerec->data.death_time = time_now + ttl;
240
241   namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
242
243   /* Now add the record to the name list. */    
244   update_name_in_namelist( subrec, namerec );
245
246   DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
247 ttl=%d nb_flags=%2x to subnet %s\n",
248             nmb_namestr( &namerec->name ),
249             inet_ntoa( *iplist ),
250             ttl,
251             (unsigned int)nb_flags,
252             subrec->subnet_name ) );
253
254   subrec->namelist_changed = True;
255
256   return(namerec);
257 }
258
259 /*******************************************************************
260  Utility function automatically called when a name refresh or register 
261  succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
262  it).
263  ******************************************************************/
264
265 void standard_success_register(struct subnet_record *subrec, 
266                              struct userdata_struct *userdata,
267                              struct nmb_name *nmbname, uint16 nb_flags, int ttl,
268                              struct in_addr registered_ip)
269 {
270   struct name_record *namerec;
271
272   namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
273   if( NULL == namerec )
274     (void)add_name_to_subnet( subrec, nmbname->name, nmbname->name_type,
275                               nb_flags, ttl, SELF_NAME, 1, &registered_ip );
276   else
277     update_name_ttl( namerec, ttl );
278 }
279
280 /*******************************************************************
281  Utility function automatically called when a name refresh or register 
282  fails. Note that this is only ever called on a broadcast subnet with
283  one IP address per name. This is why it can just delete the name 
284  without enumerating the IP adresses. JRA.
285  ******************************************************************/
286
287 void standard_fail_register( struct subnet_record   *subrec,
288                              struct response_record *rrec,
289                              struct nmb_name        *nmbname )
290 {
291   struct name_record *namerec;
292
293   namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
294
295   DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
296 on subnet %s\n",
297             nmb_namestr(nmbname), subrec->subnet_name) );
298
299   /* Remove the name from the subnet. */
300   if( namerec )
301     remove_name_from_namelist(subrec, namerec);
302 }
303
304 /*******************************************************************
305  Utility function to remove an IP address from a name record.
306  ******************************************************************/
307
308 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
309 {
310   if( ind != namerec->data.num_ips )
311     memmove( (char *)(&namerec->data.ip[ind]),
312              (char *)(&namerec->data.ip[ind+1]), 
313              ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
314
315   namerec->data.num_ips--;
316   namerec->subnet->namelist_changed = True;
317 }
318
319 /*******************************************************************
320  Utility function to check if an IP address exists in a name record.
321  ******************************************************************/
322
323 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
324 {
325   int i;
326
327   for(i = 0; i < namerec->data.num_ips; i++)
328     if(ip_equal( namerec->data.ip[i], ip))
329       return True;
330
331   return False;
332 }
333
334 /*******************************************************************
335  Utility function to add an IP address to a name record.
336  ******************************************************************/
337
338 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
339 {
340   struct in_addr *new_list;
341
342   /* Don't add one we already have. */
343   if( find_ip_in_name_record( namerec, new_ip ) )
344     return;
345   
346   new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1)
347                                        * sizeof(struct in_addr) );
348   if( NULL == new_list )
349   {
350     DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
351     return;
352   }
353
354   memcpy( (char *)new_list,
355           (char *)namerec->data.ip,
356           namerec->data.num_ips * sizeof(struct in_addr) );
357   new_list[namerec->data.num_ips] = new_ip;
358
359   free((char *)namerec->data.ip);
360   namerec->data.ip = new_list;
361   namerec->data.num_ips += 1;
362
363   namerec->subnet->namelist_changed = True;
364 }
365
366 /*******************************************************************
367  Utility function to remove an IP address from a name record.
368  ******************************************************************/
369
370 void remove_ip_from_name_record( struct name_record *namerec,
371                                  struct in_addr      remove_ip )
372 {
373   /* Try and find the requested ip address - remove it. */
374   int i;
375   int orig_num = namerec->data.num_ips;
376
377   for(i = 0; i < orig_num; i++)
378     if( ip_equal( remove_ip, namerec->data.ip[i]) )
379     {
380       remove_nth_ip_in_record( namerec, i);
381       break;
382     }
383 }
384
385 /*******************************************************************
386  Utility function that release_name callers can plug into as the
387  success function when a name release is successful. Used to save
388  duplication of success_function code.
389  ******************************************************************/
390
391 void standard_success_release( struct subnet_record   *subrec,
392                                struct userdata_struct *userdata,
393                                struct nmb_name        *nmbname,
394                                struct in_addr          released_ip )
395 {
396   struct name_record *namerec;
397
398   namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
399
400   if( namerec == NULL )
401   {
402     DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
403 on subnet %s. Name was not found on subnet.\n",
404                 nmb_namestr(nmbname),
405                 inet_ntoa(released_ip),
406                 subrec->subnet_name) );
407     return;
408   }
409   else
410   {
411     int orig_num = namerec->data.num_ips;
412
413     remove_ip_from_name_record( namerec, released_ip );
414
415     if( namerec->data.num_ips == orig_num )
416       DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
417 on subnet %s. This ip is not known for this name.\n",
418                 nmb_namestr(nmbname),
419                 inet_ntoa(released_ip),
420                 subrec->subnet_name ) );
421   }
422
423   if( namerec->data.num_ips == 0 )
424     remove_name_from_namelist( subrec, namerec );
425 }
426
427 /*******************************************************************
428   Expires old names in a subnet namelist.
429   ******************************************************************/
430
431 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
432 {
433   struct name_record *namerec;
434   struct name_record *next_namerec;
435
436   for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
437        namerec;
438        namerec = next_namerec )
439   {
440     next_namerec = (struct name_record *)ubi_trNext( namerec );
441     if( (namerec->data.death_time != PERMANENT_TTL)
442      && (namerec->data.death_time < t) )
443     {
444       if( namerec->data.source == SELF_NAME )
445       {
446         DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
447 name %s\n", 
448                     subrec->subnet_name, nmb_namestr(&namerec->name) ) );
449         namerec->data.death_time += 300;
450         namerec->subnet->namelist_changed = True;
451         continue;
452       }
453       DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
454                  subrec->subnet_name, nmb_namestr(&namerec->name)));
455   
456       remove_name_from_namelist( subrec, namerec );
457     }
458   }
459 }
460
461 /*******************************************************************
462   Expires old names in all subnet namelists.
463   ******************************************************************/
464
465 void expire_names(time_t t)
466 {
467   struct subnet_record *subrec;
468
469   for( subrec = FIRST_SUBNET;
470        subrec;
471        subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
472   {
473     expire_names_on_subnet( subrec, t );
474   }
475 }
476
477 /****************************************************************************
478   Add the magic samba names, useful for finding samba servers.
479   These go directly into the name list for a particular subnet,
480   without going through the normal registration process.
481   When adding them to the unicast subnet, add them as a list of
482   all broadcast subnet IP addresses.
483 **************************************************************************/
484
485 void add_samba_names_to_subnet( struct subnet_record *subrec )
486 {
487   struct in_addr *iplist = &subrec->myip;
488   int num_ips = 1;
489
490   /* These names are added permanently (ttl of zero) and will NOT be
491      refreshed.  */
492
493   if( (subrec == unicast_subnet)
494    || (subrec == wins_server_subnet)
495    || (subrec == remote_broadcast_subnet) )
496   {
497     struct subnet_record *bcast_subrecs;
498     int i;
499     /* Create an IP list containing all our known subnets. */
500
501     num_ips = iface_count();
502     iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) );
503     if( NULL == iplist )
504     {
505       DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
506       return;
507     }
508
509     for( bcast_subrecs = FIRST_SUBNET, i = 0;
510          bcast_subrecs; 
511          bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
512       iplist[i] = bcast_subrecs->myip;
513
514   }
515
516   (void)add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
517                            PERMANENT_NAME, num_ips, iplist);
518   (void)add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
519                            PERMANENT_NAME, num_ips, iplist);
520   (void)add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
521                            PERMANENT_NAME, num_ips, iplist);
522   (void)add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
523                            PERMANENT_NAME, num_ips, iplist);
524
525   if(iplist != &subrec->myip)
526     free((char *)iplist);
527 }
528
529 /****************************************************************************
530  Dump the contents of the namelists on all the subnets (including unicast)
531  into a file. Initiated by SIGHUP - used to debug the state of the namelists.
532 **************************************************************************/
533
534 static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
535 {
536   struct name_record *namerec;
537   char *src_type;
538   struct tm *tm;
539   int i;
540
541   fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
542   for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
543        namerec;
544        namerec = (struct name_record *)ubi_trNext( namerec ) )
545   {
546     fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
547     switch(namerec->data.source)
548     {
549       case LMHOSTS_NAME:
550         src_type = "LMHOSTS_NAME";
551         break;
552       case WINS_PROXY_NAME:
553         src_type = "WINS_PROXY_NAME";
554         break;
555       case REGISTER_NAME:
556         src_type = "REGISTER_NAME";
557         break;
558       case SELF_NAME:
559         src_type = "SELF_NAME";
560         break;
561       case DNS_NAME:
562         src_type = "DNS_NAME";
563         break;
564       case DNSFAIL_NAME:
565         src_type = "DNSFAIL_NAME";
566         break;
567       case PERMANENT_NAME:
568         src_type = "PERMANENT_NAME";
569         break;
570       default:
571         src_type = "unknown!";
572         break;
573     }
574     fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
575
576     if(namerec->data.death_time != PERMANENT_TTL)
577     {
578       tm = LocalTime(&namerec->data.death_time);
579       fprintf(fp, "death_time = %s\t", asctime(tm));
580     }
581     else
582       fprintf(fp, "death_time = PERMANENT\t");
583
584     if(namerec->data.refresh_time != PERMANENT_TTL)
585     {
586       tm = LocalTime(&namerec->data.refresh_time);
587       fprintf(fp, "refresh_time = %s\n", asctime(tm));
588     }
589     else
590       fprintf(fp, "refresh_time = PERMANENT\n");
591
592     fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
593     for(i = 0; i < namerec->data.num_ips; i++)
594       fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
595
596     fprintf(fp, "\n\n");
597   }
598 }
599
600 /****************************************************************************
601  Dump the contents of the namelists on all the subnets (including unicast)
602  into a file. Initiated by SIGHUP - used to debug the state of the namelists.
603 **************************************************************************/
604
605 void dump_all_namelists(void)
606 {
607   pstring fname;
608   FILE *fp; 
609   struct subnet_record *subrec;
610
611   pstrcpy(fname,lp_lockdir());
612   trim_string(fname,NULL,"/");
613   pstrcat(fname,"/"); 
614   pstrcat(fname,"namelist.debug");
615
616   fp = sys_fopen(fname,"w");
617      
618   if (!fp)
619   { 
620     DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
621               fname,strerror(errno)));
622     return;
623   }
624       
625   for( subrec = FIRST_SUBNET;
626        subrec;
627        subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
628     dump_subnet_namelist( subrec, fp );
629
630   if( !we_are_a_wins_client() )
631     dump_subnet_namelist( unicast_subnet, fp );
632
633   if( remote_broadcast_subnet->namelist != NULL )
634     dump_subnet_namelist( remote_broadcast_subnet, fp );
635
636   if( wins_server_subnet != NULL )
637     dump_subnet_namelist( wins_server_subnet, fp );
638   fclose( fp );
639 }