r23779: Change from v2 or later to v3 or later.
[sfrench/samba-autobuild/.git] / source3 / nmbd / nmbd_namelistdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6    Copyright (C) Jeremy Allison 1994-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 3 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 void remove_name_from_namelist(struct subnet_record *subrec, 
81                                 struct name_record *namerec )
82 {
83         if (subrec == wins_server_subnet) 
84                 remove_name_from_wins_namelist(namerec);
85         else {
86                 subrec->namelist_changed = True;
87                 DLIST_REMOVE(subrec->namelist, namerec);
88         }
89
90         SAFE_FREE(namerec->data.ip);
91         ZERO_STRUCTP(namerec);
92         SAFE_FREE(namerec);
93 }
94
95 /**************************************************************************
96  Find a name in a subnet.
97 **************************************************************************/
98
99 struct name_record *find_name_on_subnet(struct subnet_record *subrec,
100                                 const struct nmb_name *nmbname,
101                                 BOOL self_only)
102 {
103         struct nmb_name uc_name;
104         struct name_record *name_ret;
105
106         upcase_name( &uc_name, nmbname );
107         
108         if (subrec == wins_server_subnet) {
109                 return find_name_on_wins_subnet(&uc_name, self_only);
110         }
111
112         for( name_ret = subrec->namelist; name_ret; name_ret = name_ret->next) {
113                 if (memcmp(&uc_name, &name_ret->name, sizeof(struct nmb_name)) == 0) {
114                         break;
115                 }
116         }
117
118         if( name_ret ) {
119                 /* Self names only - these include permanent names. */
120                 if( self_only && (name_ret->data.source != SELF_NAME) && (name_ret->data.source != PERMANENT_NAME) ) {
121                         DEBUG( 9, ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
122                                                 subrec->subnet_name, nmb_namestr(nmbname) ) );
123                         return NULL;
124                 }
125
126                 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
127                         subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) );
128
129                 return name_ret;
130         }
131
132         DEBUG( 9, ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
133                 subrec->subnet_name, nmb_namestr(nmbname) ) );
134
135         return NULL;
136 }
137
138 /**************************************************************************
139  Find a name over all known broadcast subnets.
140 ************************************************************************/
141
142 struct name_record *find_name_for_remote_broadcast_subnet(struct nmb_name *nmbname,
143                                                 BOOL self_only)
144 {
145         struct subnet_record *subrec;
146         struct name_record *namerec;
147
148         for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) ) {
149                 namerec = find_name_on_subnet(subrec, nmbname, self_only);
150                 if (namerec) {
151                         return namerec;
152                 }
153         }
154
155         return NULL;
156 }
157   
158 /**************************************************************************
159  Update the ttl of an entry in a subnet name list.
160 ***************************************************************************/
161
162 void update_name_ttl( struct name_record *namerec, int ttl )
163 {
164         time_t time_now = time(NULL);
165
166         if( namerec->data.death_time != PERMANENT_TTL) {
167                 namerec->data.death_time = time_now + ttl;
168         }
169
170         namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
171
172         if (namerec->subnet == wins_server_subnet) {
173                 wins_store_changed_namerec(namerec);
174         } else {
175                 namerec->subnet->namelist_changed = True;
176         }
177 }
178
179 /**************************************************************************
180  Add an entry to a subnet name list.
181 ***********************************************************************/
182
183 BOOL add_name_to_subnet( struct subnet_record *subrec,
184                         const 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         BOOL ret = False;
193         struct name_record *namerec;
194         time_t time_now = time(NULL);
195
196         namerec = SMB_MALLOC_P(struct name_record);
197         if( NULL == namerec ) {
198                 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
199                 return False;
200         }
201
202         memset( (char *)namerec, '\0', sizeof(*namerec) );
203         namerec->data.ip = SMB_MALLOC_ARRAY( struct in_addr, num_ips );
204         if( NULL == namerec->data.ip ) {
205                 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
206                 ZERO_STRUCTP(namerec);
207                 SAFE_FREE(namerec);
208                 return False;
209         }
210
211         namerec->subnet = subrec;
212
213         make_nmb_name(&namerec->name, name, type);
214         upcase_name(&namerec->name, NULL );
215
216         /* Enter the name as active. */
217         namerec->data.nb_flags = nb_flags | NB_ACTIVE;
218         namerec->data.wins_flags = WINS_ACTIVE;
219
220         /* If it's our primary name, flag it as so. */
221         if (strequal( my_netbios_names(0), name )) {
222                 namerec->data.nb_flags |= NB_PERM;
223         }
224
225         /* Copy the IPs. */
226         namerec->data.num_ips = num_ips;
227         memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
228
229         /* Data source. */
230         namerec->data.source = source;
231
232         /* Setup the death_time and refresh_time. */
233         if (ttl == PERMANENT_TTL) {
234                 namerec->data.death_time = PERMANENT_TTL;
235         } else {
236                 namerec->data.death_time = time_now + ttl;
237         }
238
239         namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
240
241         DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
242 ttl=%d nb_flags=%2x to subnet %s\n",
243                 nmb_namestr( &namerec->name ),
244                 inet_ntoa( *iplist ),
245                 ttl,
246                 (unsigned int)nb_flags,
247                 subrec->subnet_name ) );
248
249         /* Now add the record to the name list. */    
250
251         if (subrec == wins_server_subnet) {
252                 ret = add_name_to_wins_subnet(namerec);
253                 /* Free namerec - it's stored in the tdb. */
254                 SAFE_FREE(namerec->data.ip);
255                 SAFE_FREE(namerec);
256         } else {
257                 DLIST_ADD(subrec->namelist, namerec);
258                 subrec->namelist_changed = True;
259                 ret = True;
260         }
261
262         return ret;
263 }
264
265 /*******************************************************************
266  Utility function automatically called when a name refresh or register 
267  succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
268  it).
269  ******************************************************************/
270
271 void standard_success_register(struct subnet_record *subrec, 
272                              struct userdata_struct *userdata,
273                              struct nmb_name *nmbname, uint16 nb_flags, int ttl,
274                              struct in_addr registered_ip)
275 {
276         struct name_record *namerec;
277
278         namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME);
279         if (namerec == NULL) {
280                 unstring name;
281                 pull_ascii_nstring(name, sizeof(name), nmbname->name);
282                 add_name_to_subnet( subrec, name, nmbname->name_type,
283                         nb_flags, ttl, SELF_NAME, 1, &registered_ip );
284         } else {
285                 update_name_ttl( namerec, ttl );
286         }
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", nmb_namestr(nmbname), subrec->subnet_name) );
306
307         /* Remove the name from the subnet. */
308         if( namerec ) {
309                 remove_name_from_namelist(subrec, namerec);
310         }
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
325         namerec->data.num_ips--;
326         if (namerec->subnet == wins_server_subnet) {
327                 wins_store_changed_namerec(namerec);
328         } else {
329                 namerec->subnet->namelist_changed = True;
330         }
331 }
332
333 /*******************************************************************
334  Utility function to check if an IP address exists in a name record.
335  ******************************************************************/
336
337 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
338 {
339         int i;
340
341         for(i = 0; i < namerec->data.num_ips; i++) {
342                 if(ip_equal( namerec->data.ip[i], ip)) {
343                         return True;
344                 }
345         }
346
347         return False;
348 }
349
350 /*******************************************************************
351  Utility function to add an IP address to a name record.
352  ******************************************************************/
353
354 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
355 {
356         struct in_addr *new_list;
357
358         /* Don't add one we already have. */
359         if( find_ip_in_name_record( namerec, new_ip )) {
360                 return;
361         }
362   
363         new_list = SMB_MALLOC_ARRAY( struct in_addr, namerec->data.num_ips + 1);
364         if( NULL == new_list ) {
365                 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
366                 return;
367         }
368
369         memcpy( (char *)new_list, (char *)namerec->data.ip, namerec->data.num_ips * sizeof(struct in_addr) );
370         new_list[namerec->data.num_ips] = new_ip;
371
372         SAFE_FREE(namerec->data.ip);
373         namerec->data.ip = new_list;
374         namerec->data.num_ips += 1;
375
376         if (namerec->subnet == wins_server_subnet) {
377                 wins_store_changed_namerec(namerec);
378         } else {
379                 namerec->subnet->namelist_changed = True;
380         }
381 }
382
383 /*******************************************************************
384  Utility function to remove an IP address from a name record.
385  ******************************************************************/
386
387 void remove_ip_from_name_record( struct name_record *namerec,
388                                  struct in_addr      remove_ip )
389 {
390         /* Try and find the requested ip address - remove it. */
391         int i;
392         int orig_num = namerec->data.num_ips;
393
394         for(i = 0; i < orig_num; i++) {
395                 if( ip_equal( remove_ip, namerec->data.ip[i]) ) {
396                         remove_nth_ip_in_record( namerec, i);
397                         break;
398                 }
399         }
400 }
401
402 /*******************************************************************
403  Utility function that release_name callers can plug into as the
404  success function when a name release is successful. Used to save
405  duplication of success_function code.
406  ******************************************************************/
407
408 void standard_success_release( struct subnet_record   *subrec,
409                                struct userdata_struct *userdata,
410                                struct nmb_name        *nmbname,
411                                struct in_addr          released_ip )
412 {
413         struct name_record *namerec;
414
415         namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
416         if( namerec == NULL ) {
417                 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
418 on subnet %s. Name was not found on subnet.\n", nmb_namestr(nmbname), inet_ntoa(released_ip),
419                                 subrec->subnet_name) );
420                 return;
421         } else {
422                 int orig_num = namerec->data.num_ips;
423
424                 remove_ip_from_name_record( namerec, released_ip );
425
426                 if( namerec->data.num_ips == orig_num ) {
427                         DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
428 on subnet %s. This ip is not known for this name.\n", nmb_namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name ) );
429                 }
430         }
431
432         if( namerec->data.num_ips == 0 ) {
433                 remove_name_from_namelist( subrec, namerec );
434         }
435 }
436
437 /*******************************************************************
438  Expires old names in a subnet namelist.
439  NB. Does not touch the wins_subnet - no wins specific processing here.
440 ******************************************************************/
441
442 static void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
443 {
444         struct name_record *namerec;
445         struct name_record *next_namerec;
446
447         for( namerec = subrec->namelist; namerec; namerec = next_namerec ) {
448                 next_namerec = namerec->next;
449                 if( (namerec->data.death_time != PERMANENT_TTL) && (namerec->data.death_time < t) ) {
450                         if( namerec->data.source == SELF_NAME ) {
451                                 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
452 name %s\n", subrec->subnet_name, nmb_namestr(&namerec->name) ) );
453                                 namerec->data.death_time += 300;
454                                 namerec->subnet->namelist_changed = True;
455                                 continue;
456                         }
457
458                         DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
459                                 subrec->subnet_name, nmb_namestr(&namerec->name)));
460   
461                         remove_name_from_namelist(subrec, namerec );
462                 }
463         }
464 }
465
466 /*******************************************************************
467  Expires old names in all subnet namelists.
468  NB. Does not touch the wins_subnet.
469 ******************************************************************/
470
471 void expire_names(time_t t)
472 {
473         struct subnet_record *subrec;
474
475         for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) ) {
476                 expire_names_on_subnet( subrec, t );
477         }
478 }
479
480 /****************************************************************************
481   Add the magic samba names, useful for finding samba servers.
482   These go directly into the name list for a particular subnet,
483   without going through the normal registration process.
484   When adding them to the unicast subnet, add them as a list of
485   all broadcast subnet IP addresses.
486 **************************************************************************/
487
488 void add_samba_names_to_subnet( struct subnet_record *subrec )
489 {
490         struct in_addr *iplist = &subrec->myip;
491         int num_ips = 1;
492
493         /* These names are added permanently (ttl of zero) and will NOT be refreshed.  */
494
495         if( (subrec == unicast_subnet) || (subrec == wins_server_subnet) || (subrec == remote_broadcast_subnet) ) {
496                 struct subnet_record *bcast_subrecs;
497                 int i;
498
499                 /* Create an IP list containing all our known subnets. */
500
501                 num_ips = iface_count();
502                 iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips);
503                 if( NULL == iplist ) {
504                         DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
505                         return;
506                 }
507
508                 for( bcast_subrecs = FIRST_SUBNET, i = 0; bcast_subrecs; bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
509                         iplist[i] = bcast_subrecs->myip;
510         }
511
512         add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
513                                 PERMANENT_NAME, num_ips, iplist);
514         add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
515                                 PERMANENT_NAME, num_ips, iplist);
516         add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
517                                 PERMANENT_NAME, num_ips, iplist);
518         add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
519                                 PERMANENT_NAME, num_ips, iplist);
520
521         if(iplist != &subrec->myip) {
522                 SAFE_FREE(iplist);
523         }
524 }
525
526 /****************************************************************************
527  Dump a name_record struct.
528 **************************************************************************/
529
530 void dump_name_record( struct name_record *namerec, XFILE *fp)
531 {
532         const char *src_type;
533         struct tm *tm;
534         int i;
535
536         x_fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
537         switch(namerec->data.source) {
538                 case LMHOSTS_NAME:
539                         src_type = "LMHOSTS_NAME";
540                         break;
541                 case WINS_PROXY_NAME:
542                         src_type = "WINS_PROXY_NAME";
543                         break;
544                 case REGISTER_NAME:
545                         src_type = "REGISTER_NAME";
546                         break;
547                 case SELF_NAME:
548                         src_type = "SELF_NAME";
549                         break;
550                 case DNS_NAME:
551                         src_type = "DNS_NAME";
552                         break;
553                 case DNSFAIL_NAME:
554                         src_type = "DNSFAIL_NAME";
555                         break;
556                 case PERMANENT_NAME:
557                         src_type = "PERMANENT_NAME";
558                         break;
559                 default:
560                         src_type = "unknown!";
561                         break;
562         }
563
564         x_fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
565
566         if(namerec->data.death_time != PERMANENT_TTL) {
567                 const char *asct;
568                 tm = localtime(&namerec->data.death_time);
569                 if (!tm) {
570                         return;
571                 }
572                 asct = asctime(tm);
573                 if (!asct) {
574                         return;
575                 }
576                 x_fprintf(fp, "death_time = %s\t", asct);
577         } else {
578                 x_fprintf(fp, "death_time = PERMANENT\t");
579         }
580
581         if(namerec->data.refresh_time != PERMANENT_TTL) {
582                 const char *asct;
583                 tm = localtime(&namerec->data.refresh_time);
584                 if (!tm) {
585                         return;
586                 }
587                 asct = asctime(tm);
588                 if (!asct) {
589                         return;
590                 }
591                 x_fprintf(fp, "refresh_time = %s\n", asct);
592         } else {
593                 x_fprintf(fp, "refresh_time = PERMANENT\n");
594         }
595
596         x_fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
597         for(i = 0; i < namerec->data.num_ips; i++) {
598                 x_fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
599         }
600
601         x_fprintf(fp, "\n\n");
602         
603 }
604
605 /****************************************************************************
606  Dump the contents of the namelists on all the subnets (including unicast)
607  into a file. Initiated by SIGHUP - used to debug the state of the namelists.
608 **************************************************************************/
609
610 static void dump_subnet_namelist( struct subnet_record *subrec, XFILE *fp)
611 {
612         struct name_record *namerec;
613         x_fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
614         for( namerec = subrec->namelist; namerec; namerec = namerec->next) {
615                 dump_name_record(namerec, fp);
616         }
617 }
618
619 /****************************************************************************
620  Dump the contents of the namelists on all the subnets (including unicast)
621  into a file. Initiated by SIGHUP - used to debug the state of the namelists.
622 **************************************************************************/
623
624 void dump_all_namelists(void)
625 {
626         XFILE *fp; 
627         struct subnet_record *subrec;
628
629         fp = x_fopen(lock_path("namelist.debug"),O_WRONLY|O_CREAT|O_TRUNC, 0644);
630      
631         if (!fp) { 
632                 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
633                         "namelist.debug",strerror(errno)));
634                 return;
635         }
636       
637         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
638                 dump_subnet_namelist( subrec, fp );
639         }
640
641         if (!we_are_a_wins_client()) {
642                 dump_subnet_namelist( unicast_subnet, fp );
643         }
644
645         if (remote_broadcast_subnet->namelist != NULL) {
646                 dump_subnet_namelist( remote_broadcast_subnet, fp );
647         }
648
649         if (wins_server_subnet != NULL) {
650                 dump_wins_subnet_namelist(fp );
651         }
652
653         x_fclose( fp );
654 }