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