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