r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / nmbd / nmbd_namelistdb.c
blob75259a3672b05b43ed4bc456e61d06431cd861a5
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
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.
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.
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/>.
23 #include "includes.h"
25 uint16 samba_nb_type = 0; /* samba's NetBIOS name type */
28 /**************************************************************************
29 Set Samba's NetBIOS name type.
30 ***************************************************************************/
32 void set_samba_nb_type(void)
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. */
41 /***************************************************************************
42 Convert a NetBIOS name to upper case.
43 ***************************************************************************/
45 static void upcase_name( struct nmb_name *target, const struct nmb_name *source )
47 int i;
48 unstring targ;
49 fstring scope;
51 if( NULL != source ) {
52 memcpy( target, source, sizeof( struct nmb_name ) );
55 pull_ascii_nstring(targ, sizeof(targ), target->name);
56 strupper_m( targ );
57 push_ascii_nstring( target->name, targ);
59 pull_ascii(scope, target->scope, 64, -1, STR_TERMINATE);
60 strupper_m( scope );
61 push_ascii(target->scope, scope, 64, STR_TERMINATE);
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.
67 for( i = strlen( target->name ); i < sizeof( target->name ); i++ ) {
68 target->name[i] = '\0';
70 for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ ) {
71 target->scope[i] = '\0';
75 /**************************************************************************
76 Remove a name from the namelist.
77 ***************************************************************************/
79 void remove_name_from_namelist(struct subnet_record *subrec,
80 struct name_record *namerec )
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);
89 SAFE_FREE(namerec->data.ip);
90 ZERO_STRUCTP(namerec);
91 SAFE_FREE(namerec);
94 /**************************************************************************
95 Find a name in a subnet.
96 **************************************************************************/
98 struct name_record *find_name_on_subnet(struct subnet_record *subrec,
99 const struct nmb_name *nmbname,
100 BOOL self_only)
102 struct nmb_name uc_name;
103 struct name_record *name_ret;
105 upcase_name( &uc_name, nmbname );
107 if (subrec == wins_server_subnet) {
108 return find_name_on_wins_subnet(&uc_name, self_only);
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;
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;
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) );
128 return name_ret;
131 DEBUG( 9, ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
132 subrec->subnet_name, nmb_namestr(nmbname) ) );
134 return NULL;
137 /**************************************************************************
138 Find a name over all known broadcast subnets.
139 ************************************************************************/
141 struct name_record *find_name_for_remote_broadcast_subnet(struct nmb_name *nmbname,
142 BOOL self_only)
144 struct subnet_record *subrec;
145 struct name_record *namerec;
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;
154 return NULL;
157 /**************************************************************************
158 Update the ttl of an entry in a subnet name list.
159 ***************************************************************************/
161 void update_name_ttl( struct name_record *namerec, int ttl )
163 time_t time_now = time(NULL);
165 if( namerec->data.death_time != PERMANENT_TTL) {
166 namerec->data.death_time = time_now + ttl;
169 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
171 if (namerec->subnet == wins_server_subnet) {
172 wins_store_changed_namerec(namerec);
173 } else {
174 namerec->subnet->namelist_changed = True;
178 /**************************************************************************
179 Add an entry to a subnet name list.
180 ***********************************************************************/
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)
191 BOOL ret = False;
192 struct name_record *namerec;
193 time_t time_now = time(NULL);
195 namerec = SMB_MALLOC_P(struct name_record);
196 if( NULL == namerec ) {
197 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
198 return False;
201 memset( (char *)namerec, '\0', sizeof(*namerec) );
202 namerec->data.ip = SMB_MALLOC_ARRAY( struct in_addr, num_ips );
203 if( NULL == namerec->data.ip ) {
204 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
205 ZERO_STRUCTP(namerec);
206 SAFE_FREE(namerec);
207 return False;
210 namerec->subnet = subrec;
212 make_nmb_name(&namerec->name, name, type);
213 upcase_name(&namerec->name, NULL );
215 /* Enter the name as active. */
216 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
217 namerec->data.wins_flags = WINS_ACTIVE;
219 /* If it's our primary name, flag it as so. */
220 if (strequal( my_netbios_names(0), name )) {
221 namerec->data.nb_flags |= NB_PERM;
224 /* Copy the IPs. */
225 namerec->data.num_ips = num_ips;
226 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
228 /* Data source. */
229 namerec->data.source = source;
231 /* Setup the death_time and refresh_time. */
232 if (ttl == PERMANENT_TTL) {
233 namerec->data.death_time = PERMANENT_TTL;
234 } else {
235 namerec->data.death_time = time_now + ttl;
238 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
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 ) );
248 /* Now add the record to the name list. */
250 if (subrec == wins_server_subnet) {
251 ret = add_name_to_wins_subnet(namerec);
252 /* Free namerec - it's stored in the tdb. */
253 SAFE_FREE(namerec->data.ip);
254 SAFE_FREE(namerec);
255 } else {
256 DLIST_ADD(subrec->namelist, namerec);
257 subrec->namelist_changed = True;
258 ret = True;
261 return ret;
264 /*******************************************************************
265 Utility function automatically called when a name refresh or register
266 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
267 it).
268 ******************************************************************/
270 void standard_success_register(struct subnet_record *subrec,
271 struct userdata_struct *userdata,
272 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
273 struct in_addr registered_ip)
275 struct name_record *namerec;
277 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME);
278 if (namerec == NULL) {
279 unstring name;
280 pull_ascii_nstring(name, sizeof(name), nmbname->name);
281 add_name_to_subnet( subrec, name, nmbname->name_type,
282 nb_flags, ttl, SELF_NAME, 1, &registered_ip );
283 } else {
284 update_name_ttl( namerec, ttl );
288 /*******************************************************************
289 Utility function automatically called when a name refresh or register
290 fails. Note that this is only ever called on a broadcast subnet with
291 one IP address per name. This is why it can just delete the name
292 without enumerating the IP adresses. JRA.
293 ******************************************************************/
295 void standard_fail_register( struct subnet_record *subrec,
296 struct response_record *rrec,
297 struct nmb_name *nmbname )
299 struct name_record *namerec;
301 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME);
303 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
304 on subnet %s\n", nmb_namestr(nmbname), subrec->subnet_name) );
306 /* Remove the name from the subnet. */
307 if( namerec ) {
308 remove_name_from_namelist(subrec, namerec);
312 /*******************************************************************
313 Utility function to remove an IP address from a name record.
314 ******************************************************************/
316 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
318 if( ind != namerec->data.num_ips ) {
319 memmove( (char *)(&namerec->data.ip[ind]),
320 (char *)(&namerec->data.ip[ind+1]),
321 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
324 namerec->data.num_ips--;
325 if (namerec->subnet == wins_server_subnet) {
326 wins_store_changed_namerec(namerec);
327 } else {
328 namerec->subnet->namelist_changed = True;
332 /*******************************************************************
333 Utility function to check if an IP address exists in a name record.
334 ******************************************************************/
336 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
338 int i;
340 for(i = 0; i < namerec->data.num_ips; i++) {
341 if(ip_equal( namerec->data.ip[i], ip)) {
342 return True;
346 return False;
349 /*******************************************************************
350 Utility function to add an IP address to a name record.
351 ******************************************************************/
353 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
355 struct in_addr *new_list;
357 /* Don't add one we already have. */
358 if( find_ip_in_name_record( namerec, new_ip )) {
359 return;
362 new_list = SMB_MALLOC_ARRAY( struct in_addr, namerec->data.num_ips + 1);
363 if( NULL == new_list ) {
364 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
365 return;
368 memcpy( (char *)new_list, (char *)namerec->data.ip, namerec->data.num_ips * sizeof(struct in_addr) );
369 new_list[namerec->data.num_ips] = new_ip;
371 SAFE_FREE(namerec->data.ip);
372 namerec->data.ip = new_list;
373 namerec->data.num_ips += 1;
375 if (namerec->subnet == wins_server_subnet) {
376 wins_store_changed_namerec(namerec);
377 } else {
378 namerec->subnet->namelist_changed = True;
382 /*******************************************************************
383 Utility function to remove an IP address from a name record.
384 ******************************************************************/
386 void remove_ip_from_name_record( struct name_record *namerec,
387 struct in_addr remove_ip )
389 /* Try and find the requested ip address - remove it. */
390 int i;
391 int orig_num = namerec->data.num_ips;
393 for(i = 0; i < orig_num; i++) {
394 if( ip_equal( remove_ip, namerec->data.ip[i]) ) {
395 remove_nth_ip_in_record( namerec, i);
396 break;
401 /*******************************************************************
402 Utility function that release_name callers can plug into as the
403 success function when a name release is successful. Used to save
404 duplication of success_function code.
405 ******************************************************************/
407 void standard_success_release( struct subnet_record *subrec,
408 struct userdata_struct *userdata,
409 struct nmb_name *nmbname,
410 struct in_addr released_ip )
412 struct name_record *namerec;
414 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
415 if( namerec == NULL ) {
416 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
417 on subnet %s. Name was not found on subnet.\n", nmb_namestr(nmbname), inet_ntoa(released_ip),
418 subrec->subnet_name) );
419 return;
420 } else {
421 int orig_num = namerec->data.num_ips;
423 remove_ip_from_name_record( namerec, released_ip );
425 if( namerec->data.num_ips == orig_num ) {
426 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
427 on subnet %s. This ip is not known for this name.\n", nmb_namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name ) );
431 if( namerec->data.num_ips == 0 ) {
432 remove_name_from_namelist( subrec, namerec );
436 /*******************************************************************
437 Expires old names in a subnet namelist.
438 NB. Does not touch the wins_subnet - no wins specific processing here.
439 ******************************************************************/
441 static void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
443 struct name_record *namerec;
444 struct name_record *next_namerec;
446 for( namerec = subrec->namelist; namerec; namerec = next_namerec ) {
447 next_namerec = namerec->next;
448 if( (namerec->data.death_time != PERMANENT_TTL) && (namerec->data.death_time < t) ) {
449 if( namerec->data.source == SELF_NAME ) {
450 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
451 name %s\n", subrec->subnet_name, nmb_namestr(&namerec->name) ) );
452 namerec->data.death_time += 300;
453 namerec->subnet->namelist_changed = True;
454 continue;
457 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
458 subrec->subnet_name, nmb_namestr(&namerec->name)));
460 remove_name_from_namelist(subrec, namerec );
465 /*******************************************************************
466 Expires old names in all subnet namelists.
467 NB. Does not touch the wins_subnet.
468 ******************************************************************/
470 void expire_names(time_t t)
472 struct subnet_record *subrec;
474 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) ) {
475 expire_names_on_subnet( subrec, t );
479 /****************************************************************************
480 Add the magic samba names, useful for finding samba servers.
481 These go directly into the name list for a particular subnet,
482 without going through the normal registration process.
483 When adding them to the unicast subnet, add them as a list of
484 all broadcast subnet IP addresses.
485 **************************************************************************/
487 void add_samba_names_to_subnet( struct subnet_record *subrec )
489 struct in_addr *iplist = &subrec->myip;
490 int num_ips = 1;
492 /* These names are added permanently (ttl of zero) and will NOT be refreshed. */
494 if( (subrec == unicast_subnet) || (subrec == wins_server_subnet) || (subrec == remote_broadcast_subnet) ) {
495 struct subnet_record *bcast_subrecs;
496 int i;
498 /* Create an IP list containing all our known subnets. */
500 num_ips = iface_count();
501 iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips);
502 if( NULL == iplist ) {
503 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
504 return;
507 for( bcast_subrecs = FIRST_SUBNET, i = 0; bcast_subrecs; bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
508 iplist[i] = bcast_subrecs->myip;
511 add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
512 PERMANENT_NAME, num_ips, iplist);
513 add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
514 PERMANENT_NAME, num_ips, iplist);
515 add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
516 PERMANENT_NAME, num_ips, iplist);
517 add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
518 PERMANENT_NAME, num_ips, iplist);
520 if(iplist != &subrec->myip) {
521 SAFE_FREE(iplist);
525 /****************************************************************************
526 Dump a name_record struct.
527 **************************************************************************/
529 void dump_name_record( struct name_record *namerec, XFILE *fp)
531 const char *src_type;
532 struct tm *tm;
533 int i;
535 x_fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
536 switch(namerec->data.source) {
537 case LMHOSTS_NAME:
538 src_type = "LMHOSTS_NAME";
539 break;
540 case WINS_PROXY_NAME:
541 src_type = "WINS_PROXY_NAME";
542 break;
543 case REGISTER_NAME:
544 src_type = "REGISTER_NAME";
545 break;
546 case SELF_NAME:
547 src_type = "SELF_NAME";
548 break;
549 case DNS_NAME:
550 src_type = "DNS_NAME";
551 break;
552 case DNSFAIL_NAME:
553 src_type = "DNSFAIL_NAME";
554 break;
555 case PERMANENT_NAME:
556 src_type = "PERMANENT_NAME";
557 break;
558 default:
559 src_type = "unknown!";
560 break;
563 x_fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
565 if(namerec->data.death_time != PERMANENT_TTL) {
566 const char *asct;
567 tm = localtime(&namerec->data.death_time);
568 if (!tm) {
569 return;
571 asct = asctime(tm);
572 if (!asct) {
573 return;
575 x_fprintf(fp, "death_time = %s\t", asct);
576 } else {
577 x_fprintf(fp, "death_time = PERMANENT\t");
580 if(namerec->data.refresh_time != PERMANENT_TTL) {
581 const char *asct;
582 tm = localtime(&namerec->data.refresh_time);
583 if (!tm) {
584 return;
586 asct = asctime(tm);
587 if (!asct) {
588 return;
590 x_fprintf(fp, "refresh_time = %s\n", asct);
591 } else {
592 x_fprintf(fp, "refresh_time = PERMANENT\n");
595 x_fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
596 for(i = 0; i < namerec->data.num_ips; i++) {
597 x_fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
600 x_fprintf(fp, "\n\n");
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 **************************************************************************/
609 static void dump_subnet_namelist( struct subnet_record *subrec, XFILE *fp)
611 struct name_record *namerec;
612 x_fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
613 for( namerec = subrec->namelist; namerec; namerec = namerec->next) {
614 dump_name_record(namerec, fp);
618 /****************************************************************************
619 Dump the contents of the namelists on all the subnets (including unicast)
620 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
621 **************************************************************************/
623 void dump_all_namelists(void)
625 XFILE *fp;
626 struct subnet_record *subrec;
628 fp = x_fopen(lock_path("namelist.debug"),O_WRONLY|O_CREAT|O_TRUNC, 0644);
630 if (!fp) {
631 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
632 "namelist.debug",strerror(errno)));
633 return;
636 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
637 dump_subnet_namelist( subrec, fp );
640 if (!we_are_a_wins_client()) {
641 dump_subnet_namelist( unicast_subnet, fp );
644 if (remote_broadcast_subnet->namelist != NULL) {
645 dump_subnet_namelist( remote_broadcast_subnet, fp );
648 if (wins_server_subnet != NULL) {
649 dump_wins_subnet_namelist(fp );
652 x_fclose( fp );