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