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