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