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