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