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